2 * Copyright (c) 2010-2012 Broadcom. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions, and the following disclaimer,
9 * without modification.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The names of the above-listed copyright holders may not be used
14 * to endorse or promote products derived from this software without
15 * specific prior written permission.
17 * ALTERNATIVELY, this software may be distributed under the terms of the
18 * GNU General Public License ("GPL") version 2, as published by the Free
19 * Software Foundation.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
22 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
25 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <linux/kernel.h>
35 #include <linux/types.h>
36 #include <linux/errno.h>
37 #include <linux/interrupt.h>
38 #include <linux/pagemap.h>
39 #include <linux/dma-mapping.h>
41 #include <linux/platform_device.h>
42 #include <linux/uaccess.h>
45 #include <soc/bcm2835/raspberrypi-firmware.h>
47 #define TOTAL_SLOTS (VCHIQ_SLOT_ZERO_SLOTS + 2 * 32)
49 #include "vchiq_arm.h"
50 #include "vchiq_connected.h"
51 #include "vchiq_killable.h"
52 #include "vchiq_pagelist.h"
54 #define MAX_FRAGMENTS (VCHIQ_NUM_CURRENT_BULKS * 2)
56 #define VCHIQ_PLATFORM_FRAGMENTS_OFFSET_IDX 0
57 #define VCHIQ_PLATFORM_FRAGMENTS_COUNT_IDX 1
62 typedef struct vchiq_2835_state_struct {
64 VCHIQ_ARM_STATE_T arm_state;
65 } VCHIQ_2835_ARM_STATE_T;
67 struct vchiq_pagelist_info {
69 size_t pagelist_buffer_size;
71 enum dma_data_direction dma_dir;
72 unsigned int num_pages;
73 unsigned int pages_need_release;
75 struct scatterlist *scatterlist;
76 unsigned int scatterlist_mapped;
79 static void __iomem *g_regs;
80 static unsigned int g_cache_line_size = sizeof(CACHE_LINE_SIZE);
81 static unsigned int g_fragments_size;
82 static char *g_fragments_base;
83 static char *g_free_fragments;
84 static struct semaphore g_free_fragments_sema;
85 static struct device *g_dev;
87 extern int vchiq_arm_log_level;
89 static DEFINE_SEMAPHORE(g_free_fragments_mutex);
92 vchiq_doorbell_irq(int irq, void *dev_id);
94 static struct vchiq_pagelist_info *
95 create_pagelist(char __user *buf, size_t count, unsigned short type);
98 free_pagelist(struct vchiq_pagelist_info *pagelistinfo,
101 int vchiq_platform_init(struct platform_device *pdev, VCHIQ_STATE_T *state)
103 struct device *dev = &pdev->dev;
104 struct rpi_firmware *fw = platform_get_drvdata(pdev);
105 VCHIQ_SLOT_ZERO_T *vchiq_slot_zero;
106 struct resource *res;
108 dma_addr_t slot_phys;
110 int slot_mem_size, frag_mem_size;
114 * VCHI messages between the CPU and firmware use
115 * 32-bit bus addresses.
117 err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
122 err = of_property_read_u32(dev->of_node, "cache-line-size",
126 dev_err(dev, "Missing cache-line-size property\n");
130 g_fragments_size = 2 * g_cache_line_size;
132 /* Allocate space for the channels in coherent memory */
133 slot_mem_size = PAGE_ALIGN(TOTAL_SLOTS * VCHIQ_SLOT_SIZE);
134 frag_mem_size = PAGE_ALIGN(g_fragments_size * MAX_FRAGMENTS);
136 slot_mem = dmam_alloc_coherent(dev, slot_mem_size + frag_mem_size,
137 &slot_phys, GFP_KERNEL);
139 dev_err(dev, "could not allocate DMA memory\n");
143 WARN_ON(((unsigned long)slot_mem & (PAGE_SIZE - 1)) != 0);
145 vchiq_slot_zero = vchiq_init_slots(slot_mem, slot_mem_size);
146 if (!vchiq_slot_zero)
149 vchiq_slot_zero->platform_data[VCHIQ_PLATFORM_FRAGMENTS_OFFSET_IDX] =
150 (int)slot_phys + slot_mem_size;
151 vchiq_slot_zero->platform_data[VCHIQ_PLATFORM_FRAGMENTS_COUNT_IDX] =
154 g_fragments_base = (char *)slot_mem + slot_mem_size;
156 g_free_fragments = g_fragments_base;
157 for (i = 0; i < (MAX_FRAGMENTS - 1); i++) {
158 *(char **)&g_fragments_base[i*g_fragments_size] =
159 &g_fragments_base[(i + 1)*g_fragments_size];
161 *(char **)&g_fragments_base[i * g_fragments_size] = NULL;
162 sema_init(&g_free_fragments_sema, MAX_FRAGMENTS);
164 if (vchiq_init_state(state, vchiq_slot_zero, 0) != VCHIQ_SUCCESS)
167 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
168 g_regs = devm_ioremap_resource(&pdev->dev, res);
170 return PTR_ERR(g_regs);
172 irq = platform_get_irq(pdev, 0);
174 dev_err(dev, "failed to get IRQ\n");
178 err = devm_request_irq(dev, irq, vchiq_doorbell_irq, IRQF_IRQPOLL,
179 "VCHIQ doorbell", state);
181 dev_err(dev, "failed to register irq=%d\n", irq);
185 /* Send the base address of the slots to VideoCore */
186 channelbase = slot_phys;
187 err = rpi_firmware_property(fw, RPI_FIRMWARE_VCHIQ_INIT,
188 &channelbase, sizeof(channelbase));
189 if (err || channelbase) {
190 dev_err(dev, "failed to set channelbase\n");
191 return err ? : -ENXIO;
195 vchiq_log_info(vchiq_arm_log_level,
196 "vchiq_init - done (slots %pK, phys %pad)",
197 vchiq_slot_zero, &slot_phys);
199 vchiq_call_connected_callbacks();
205 vchiq_platform_init_state(VCHIQ_STATE_T *state)
207 VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
209 state->platform_state = kzalloc(sizeof(VCHIQ_2835_ARM_STATE_T), GFP_KERNEL);
210 ((VCHIQ_2835_ARM_STATE_T *)state->platform_state)->inited = 1;
211 status = vchiq_arm_init_state(state, &((VCHIQ_2835_ARM_STATE_T *)state->platform_state)->arm_state);
212 if (status != VCHIQ_SUCCESS)
214 ((VCHIQ_2835_ARM_STATE_T *)state->platform_state)->inited = 0;
220 vchiq_platform_get_arm_state(VCHIQ_STATE_T *state)
222 if (!((VCHIQ_2835_ARM_STATE_T *)state->platform_state)->inited)
226 return &((VCHIQ_2835_ARM_STATE_T *)state->platform_state)->arm_state;
230 remote_event_signal(REMOTE_EVENT_T *event)
236 dsb(sy); /* data barrier operation */
239 writel(0, g_regs + BELL2); /* trigger vc interrupt */
243 vchiq_prepare_bulk_data(VCHIQ_BULK_T *bulk, VCHI_MEM_HANDLE_T memhandle,
244 void *offset, int size, int dir)
246 struct vchiq_pagelist_info *pagelistinfo;
248 WARN_ON(memhandle != VCHI_MEM_HANDLE_INVALID);
250 pagelistinfo = create_pagelist((char __user *)offset, size,
251 (dir == VCHIQ_BULK_RECEIVE)
258 bulk->handle = memhandle;
259 bulk->data = (void *)(unsigned long)pagelistinfo->dma_addr;
262 * Store the pagelistinfo address in remote_data,
263 * which isn't used by the slave.
265 bulk->remote_data = pagelistinfo;
267 return VCHIQ_SUCCESS;
271 vchiq_complete_bulk(VCHIQ_BULK_T *bulk)
273 if (bulk && bulk->remote_data && bulk->actual)
274 free_pagelist((struct vchiq_pagelist_info *)bulk->remote_data,
279 vchiq_transfer_bulk(VCHIQ_BULK_T *bulk)
282 * This should only be called on the master (VideoCore) side, but
283 * provide an implementation to avoid the need for ifdefery.
289 vchiq_dump_platform_state(void *dump_context)
294 len = snprintf(buf, sizeof(buf),
295 " Platform: 2835 (VC master)");
296 vchiq_dump(dump_context, buf, len + 1);
300 vchiq_platform_suspend(VCHIQ_STATE_T *state)
306 vchiq_platform_resume(VCHIQ_STATE_T *state)
308 return VCHIQ_SUCCESS;
312 vchiq_platform_paused(VCHIQ_STATE_T *state)
317 vchiq_platform_resumed(VCHIQ_STATE_T *state)
322 vchiq_platform_videocore_wanted(VCHIQ_STATE_T *state)
324 return 1; // autosuspend not supported - videocore always wanted
328 vchiq_platform_use_suspend_timer(void)
333 vchiq_dump_platform_use_state(VCHIQ_STATE_T *state)
335 vchiq_log_info(vchiq_arm_log_level, "Suspend timer not in use");
338 vchiq_platform_handle_timeout(VCHIQ_STATE_T *state)
347 vchiq_doorbell_irq(int irq, void *dev_id)
349 VCHIQ_STATE_T *state = dev_id;
350 irqreturn_t ret = IRQ_NONE;
353 /* Read (and clear) the doorbell */
354 status = readl(g_regs + BELL0);
356 if (status & 0x4) { /* Was the doorbell rung? */
357 remote_event_pollall(state);
365 cleanup_pagelistinfo(struct vchiq_pagelist_info *pagelistinfo)
367 if (pagelistinfo->scatterlist_mapped) {
368 dma_unmap_sg(g_dev, pagelistinfo->scatterlist,
369 pagelistinfo->num_pages, pagelistinfo->dma_dir);
372 if (pagelistinfo->pages_need_release) {
375 for (i = 0; i < pagelistinfo->num_pages; i++)
376 put_page(pagelistinfo->pages[i]);
379 dma_free_coherent(g_dev, pagelistinfo->pagelist_buffer_size,
380 pagelistinfo->pagelist, pagelistinfo->dma_addr);
383 /* There is a potential problem with partial cache lines (pages?)
384 ** at the ends of the block when reading. If the CPU accessed anything in
385 ** the same line (page?) then it may have pulled old data into the cache,
386 ** obscuring the new data underneath. We can solve this by transferring the
387 ** partial cache lines separately, and allowing the ARM to copy into the
391 static struct vchiq_pagelist_info *
392 create_pagelist(char __user *buf, size_t count, unsigned short type)
394 PAGELIST_T *pagelist;
395 struct vchiq_pagelist_info *pagelistinfo;
398 unsigned int num_pages, offset, i, k;
400 size_t pagelist_size;
401 struct scatterlist *scatterlist, *sg;
405 offset = ((unsigned int)(unsigned long)buf & (PAGE_SIZE - 1));
406 num_pages = DIV_ROUND_UP(count + offset, PAGE_SIZE);
408 pagelist_size = sizeof(PAGELIST_T) +
409 (num_pages * sizeof(u32)) +
410 (num_pages * sizeof(pages[0]) +
411 (num_pages * sizeof(struct scatterlist))) +
412 sizeof(struct vchiq_pagelist_info);
414 /* Allocate enough storage to hold the page pointers and the page
417 pagelist = dma_zalloc_coherent(g_dev,
422 vchiq_log_trace(vchiq_arm_log_level, "create_pagelist - %pK",
427 addrs = pagelist->addrs;
428 pages = (struct page **)(addrs + num_pages);
429 scatterlist = (struct scatterlist *)(pages + num_pages);
430 pagelistinfo = (struct vchiq_pagelist_info *)
431 (scatterlist + num_pages);
433 pagelist->length = count;
434 pagelist->type = type;
435 pagelist->offset = offset;
437 /* Populate the fields of the pagelistinfo structure */
438 pagelistinfo->pagelist = pagelist;
439 pagelistinfo->pagelist_buffer_size = pagelist_size;
440 pagelistinfo->dma_addr = dma_addr;
441 pagelistinfo->dma_dir = (type == PAGELIST_WRITE) ?
442 DMA_TO_DEVICE : DMA_FROM_DEVICE;
443 pagelistinfo->num_pages = num_pages;
444 pagelistinfo->pages_need_release = 0;
445 pagelistinfo->pages = pages;
446 pagelistinfo->scatterlist = scatterlist;
447 pagelistinfo->scatterlist_mapped = 0;
449 if (is_vmalloc_addr(buf)) {
450 unsigned long length = count;
451 unsigned int off = offset;
453 for (actual_pages = 0; actual_pages < num_pages;
455 struct page *pg = vmalloc_to_page(buf + (actual_pages *
457 size_t bytes = PAGE_SIZE - off;
460 cleanup_pagelistinfo(pagelistinfo);
466 pages[actual_pages] = pg;
470 /* do not try and release vmalloc pages */
472 actual_pages = get_user_pages_fast(
473 (unsigned long)buf & PAGE_MASK,
475 type == PAGELIST_READ,
478 if (actual_pages != num_pages) {
479 vchiq_log_info(vchiq_arm_log_level,
480 "create_pagelist - only %d/%d pages locked",
484 /* This is probably due to the process being killed */
485 while (actual_pages > 0)
488 put_page(pages[actual_pages]);
490 cleanup_pagelistinfo(pagelistinfo);
493 /* release user pages */
494 pagelistinfo->pages_need_release = 1;
498 * Initialize the scatterlist so that the magic cookie
499 * is filled if debugging is enabled
501 sg_init_table(scatterlist, num_pages);
502 /* Now set the pages for each scatterlist */
503 for (i = 0; i < num_pages; i++) {
504 unsigned int len = PAGE_SIZE - offset;
508 sg_set_page(scatterlist + i, pages[i], len, offset);
513 dma_buffers = dma_map_sg(g_dev,
516 pagelistinfo->dma_dir);
518 if (dma_buffers == 0) {
519 cleanup_pagelistinfo(pagelistinfo);
523 pagelistinfo->scatterlist_mapped = 1;
525 /* Combine adjacent blocks for performance */
527 for_each_sg(scatterlist, sg, dma_buffers, i) {
528 u32 len = sg_dma_len(sg);
529 u32 addr = sg_dma_address(sg);
531 /* Note: addrs is the address + page_count - 1
532 * The firmware expects blocks after the first to be page-
533 * aligned and a multiple of the page size
536 WARN_ON(i && (i != (dma_buffers - 1)) && (len & ~PAGE_MASK));
537 WARN_ON(i && (addr & ~PAGE_MASK));
539 ((addrs[k - 1] & PAGE_MASK) +
540 (((addrs[k - 1] & ~PAGE_MASK) + 1) << PAGE_SHIFT))
541 == (addr & PAGE_MASK))
542 addrs[k - 1] += ((len + PAGE_SIZE - 1) >> PAGE_SHIFT);
544 addrs[k++] = (addr & PAGE_MASK) |
545 (((len + PAGE_SIZE - 1) >> PAGE_SHIFT) - 1);
548 /* Partial cache lines (fragments) require special measures */
549 if ((type == PAGELIST_READ) &&
550 ((pagelist->offset & (g_cache_line_size - 1)) ||
551 ((pagelist->offset + pagelist->length) &
552 (g_cache_line_size - 1)))) {
555 if (down_interruptible(&g_free_fragments_sema) != 0) {
556 cleanup_pagelistinfo(pagelistinfo);
560 WARN_ON(g_free_fragments == NULL);
562 down(&g_free_fragments_mutex);
563 fragments = g_free_fragments;
564 WARN_ON(fragments == NULL);
565 g_free_fragments = *(char **) g_free_fragments;
566 up(&g_free_fragments_mutex);
567 pagelist->type = PAGELIST_READ_WITH_FRAGMENTS +
568 (fragments - g_fragments_base) / g_fragments_size;
575 free_pagelist(struct vchiq_pagelist_info *pagelistinfo,
578 PAGELIST_T *pagelist = pagelistinfo->pagelist;
579 struct page **pages = pagelistinfo->pages;
580 unsigned int num_pages = pagelistinfo->num_pages;
582 vchiq_log_trace(vchiq_arm_log_level, "free_pagelist - %pK, %d",
583 pagelistinfo->pagelist, actual);
586 * NOTE: dma_unmap_sg must be called before the
587 * cpu can touch any of the data/pages.
589 dma_unmap_sg(g_dev, pagelistinfo->scatterlist,
590 pagelistinfo->num_pages, pagelistinfo->dma_dir);
591 pagelistinfo->scatterlist_mapped = 0;
593 /* Deal with any partial cache lines (fragments) */
594 if (pagelist->type >= PAGELIST_READ_WITH_FRAGMENTS) {
595 char *fragments = g_fragments_base +
596 (pagelist->type - PAGELIST_READ_WITH_FRAGMENTS) *
598 int head_bytes, tail_bytes;
600 head_bytes = (g_cache_line_size - pagelist->offset) &
601 (g_cache_line_size - 1);
602 tail_bytes = (pagelist->offset + actual) &
603 (g_cache_line_size - 1);
605 if ((actual >= 0) && (head_bytes != 0)) {
606 if (head_bytes > actual)
609 memcpy((char *)page_address(pages[0]) +
614 if ((actual >= 0) && (head_bytes < actual) &&
616 memcpy((char *)page_address(pages[num_pages - 1]) +
617 ((pagelist->offset + actual) &
618 (PAGE_SIZE - 1) & ~(g_cache_line_size - 1)),
619 fragments + g_cache_line_size,
623 down(&g_free_fragments_mutex);
624 *(char **)fragments = g_free_fragments;
625 g_free_fragments = fragments;
626 up(&g_free_fragments_mutex);
627 up(&g_free_fragments_sema);
630 /* Need to mark all the pages dirty. */
631 if (pagelist->type != PAGELIST_WRITE &&
632 pagelistinfo->pages_need_release) {
635 for (i = 0; i < num_pages; i++)
636 set_page_dirty(pages[i]);
639 cleanup_pagelistinfo(pagelistinfo);