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 struct vchiq_2835_state {
64 VCHIQ_ARM_STATE_T arm_state;
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 static DEFINE_SEMAPHORE(g_free_fragments_mutex);
90 vchiq_doorbell_irq(int irq, void *dev_id);
92 static struct vchiq_pagelist_info *
93 create_pagelist(char __user *buf, size_t count, unsigned short type);
96 free_pagelist(struct vchiq_pagelist_info *pagelistinfo,
99 int vchiq_platform_init(struct platform_device *pdev, VCHIQ_STATE_T *state)
101 struct device *dev = &pdev->dev;
102 struct rpi_firmware *fw = platform_get_drvdata(pdev);
103 VCHIQ_SLOT_ZERO_T *vchiq_slot_zero;
104 struct resource *res;
106 dma_addr_t slot_phys;
108 int slot_mem_size, frag_mem_size;
112 * VCHI messages between the CPU and firmware use
113 * 32-bit bus addresses.
115 err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
120 err = of_property_read_u32(dev->of_node, "cache-line-size",
124 dev_err(dev, "Missing cache-line-size property\n");
128 g_fragments_size = 2 * g_cache_line_size;
130 /* Allocate space for the channels in coherent memory */
131 slot_mem_size = PAGE_ALIGN(TOTAL_SLOTS * VCHIQ_SLOT_SIZE);
132 frag_mem_size = PAGE_ALIGN(g_fragments_size * MAX_FRAGMENTS);
134 slot_mem = dmam_alloc_coherent(dev, slot_mem_size + frag_mem_size,
135 &slot_phys, GFP_KERNEL);
137 dev_err(dev, "could not allocate DMA memory\n");
141 WARN_ON(((unsigned long)slot_mem & (PAGE_SIZE - 1)) != 0);
143 vchiq_slot_zero = vchiq_init_slots(slot_mem, slot_mem_size);
144 if (!vchiq_slot_zero)
147 vchiq_slot_zero->platform_data[VCHIQ_PLATFORM_FRAGMENTS_OFFSET_IDX] =
148 (int)slot_phys + slot_mem_size;
149 vchiq_slot_zero->platform_data[VCHIQ_PLATFORM_FRAGMENTS_COUNT_IDX] =
152 g_fragments_base = (char *)slot_mem + slot_mem_size;
154 g_free_fragments = g_fragments_base;
155 for (i = 0; i < (MAX_FRAGMENTS - 1); i++) {
156 *(char **)&g_fragments_base[i*g_fragments_size] =
157 &g_fragments_base[(i + 1)*g_fragments_size];
159 *(char **)&g_fragments_base[i * g_fragments_size] = NULL;
160 sema_init(&g_free_fragments_sema, MAX_FRAGMENTS);
162 if (vchiq_init_state(state, vchiq_slot_zero, 0) != VCHIQ_SUCCESS)
165 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
166 g_regs = devm_ioremap_resource(&pdev->dev, res);
168 return PTR_ERR(g_regs);
170 irq = platform_get_irq(pdev, 0);
172 dev_err(dev, "failed to get IRQ\n");
176 err = devm_request_irq(dev, irq, vchiq_doorbell_irq, IRQF_IRQPOLL,
177 "VCHIQ doorbell", state);
179 dev_err(dev, "failed to register irq=%d\n", irq);
183 /* Send the base address of the slots to VideoCore */
184 channelbase = slot_phys;
185 err = rpi_firmware_property(fw, RPI_FIRMWARE_VCHIQ_INIT,
186 &channelbase, sizeof(channelbase));
187 if (err || channelbase) {
188 dev_err(dev, "failed to set channelbase\n");
189 return err ? : -ENXIO;
193 vchiq_log_info(vchiq_arm_log_level,
194 "vchiq_init - done (slots %pK, phys %pad)",
195 vchiq_slot_zero, &slot_phys);
197 vchiq_call_connected_callbacks();
203 vchiq_platform_init_state(VCHIQ_STATE_T *state)
205 VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
206 struct vchiq_2835_state *platform_state;
208 state->platform_state = kzalloc(sizeof(*platform_state), GFP_KERNEL);
209 platform_state = (struct vchiq_2835_state *)state->platform_state;
211 platform_state->inited = 1;
212 status = vchiq_arm_init_state(state, &platform_state->arm_state);
214 if (status != VCHIQ_SUCCESS)
215 platform_state->inited = 0;
221 vchiq_platform_get_arm_state(VCHIQ_STATE_T *state)
223 struct vchiq_2835_state *platform_state;
225 platform_state = (struct vchiq_2835_state *)state->platform_state;
227 if (!platform_state->inited)
230 return &platform_state->arm_state;
234 remote_event_signal(REMOTE_EVENT_T *event)
240 dsb(sy); /* data barrier operation */
243 writel(0, g_regs + BELL2); /* trigger vc interrupt */
247 vchiq_prepare_bulk_data(VCHIQ_BULK_T *bulk, VCHI_MEM_HANDLE_T memhandle,
248 void *offset, int size, int dir)
250 struct vchiq_pagelist_info *pagelistinfo;
252 WARN_ON(memhandle != VCHI_MEM_HANDLE_INVALID);
254 pagelistinfo = create_pagelist((char __user *)offset, size,
255 (dir == VCHIQ_BULK_RECEIVE)
262 bulk->handle = memhandle;
263 bulk->data = (void *)(unsigned long)pagelistinfo->dma_addr;
266 * Store the pagelistinfo address in remote_data,
267 * which isn't used by the slave.
269 bulk->remote_data = pagelistinfo;
271 return VCHIQ_SUCCESS;
275 vchiq_complete_bulk(VCHIQ_BULK_T *bulk)
277 if (bulk && bulk->remote_data && bulk->actual)
278 free_pagelist((struct vchiq_pagelist_info *)bulk->remote_data,
283 vchiq_transfer_bulk(VCHIQ_BULK_T *bulk)
286 * This should only be called on the master (VideoCore) side, but
287 * provide an implementation to avoid the need for ifdefery.
293 vchiq_dump_platform_state(void *dump_context)
298 len = snprintf(buf, sizeof(buf),
299 " Platform: 2835 (VC master)");
300 vchiq_dump(dump_context, buf, len + 1);
304 vchiq_platform_suspend(VCHIQ_STATE_T *state)
310 vchiq_platform_resume(VCHIQ_STATE_T *state)
312 return VCHIQ_SUCCESS;
316 vchiq_platform_paused(VCHIQ_STATE_T *state)
321 vchiq_platform_resumed(VCHIQ_STATE_T *state)
326 vchiq_platform_videocore_wanted(VCHIQ_STATE_T *state)
328 return 1; // autosuspend not supported - videocore always wanted
332 vchiq_platform_use_suspend_timer(void)
337 vchiq_dump_platform_use_state(VCHIQ_STATE_T *state)
339 vchiq_log_info(vchiq_arm_log_level, "Suspend timer not in use");
342 vchiq_platform_handle_timeout(VCHIQ_STATE_T *state)
351 vchiq_doorbell_irq(int irq, void *dev_id)
353 VCHIQ_STATE_T *state = dev_id;
354 irqreturn_t ret = IRQ_NONE;
357 /* Read (and clear) the doorbell */
358 status = readl(g_regs + BELL0);
360 if (status & 0x4) { /* Was the doorbell rung? */
361 remote_event_pollall(state);
369 cleanup_pagelistinfo(struct vchiq_pagelist_info *pagelistinfo)
371 if (pagelistinfo->scatterlist_mapped) {
372 dma_unmap_sg(g_dev, pagelistinfo->scatterlist,
373 pagelistinfo->num_pages, pagelistinfo->dma_dir);
376 if (pagelistinfo->pages_need_release) {
379 for (i = 0; i < pagelistinfo->num_pages; i++)
380 put_page(pagelistinfo->pages[i]);
383 dma_free_coherent(g_dev, pagelistinfo->pagelist_buffer_size,
384 pagelistinfo->pagelist, pagelistinfo->dma_addr);
387 /* There is a potential problem with partial cache lines (pages?)
388 * at the ends of the block when reading. If the CPU accessed anything in
389 * the same line (page?) then it may have pulled old data into the cache,
390 * obscuring the new data underneath. We can solve this by transferring the
391 * partial cache lines separately, and allowing the ARM to copy into the
395 static struct vchiq_pagelist_info *
396 create_pagelist(char __user *buf, size_t count, unsigned short type)
398 PAGELIST_T *pagelist;
399 struct vchiq_pagelist_info *pagelistinfo;
402 unsigned int num_pages, offset, i, k;
404 size_t pagelist_size;
405 struct scatterlist *scatterlist, *sg;
409 offset = ((unsigned int)(unsigned long)buf & (PAGE_SIZE - 1));
410 num_pages = DIV_ROUND_UP(count + offset, PAGE_SIZE);
412 pagelist_size = sizeof(PAGELIST_T) +
413 (num_pages * sizeof(u32)) +
414 (num_pages * sizeof(pages[0]) +
415 (num_pages * sizeof(struct scatterlist))) +
416 sizeof(struct vchiq_pagelist_info);
418 /* Allocate enough storage to hold the page pointers and the page
421 pagelist = dma_zalloc_coherent(g_dev,
426 vchiq_log_trace(vchiq_arm_log_level, "%s - %pK", __func__, pagelist);
431 addrs = pagelist->addrs;
432 pages = (struct page **)(addrs + num_pages);
433 scatterlist = (struct scatterlist *)(pages + num_pages);
434 pagelistinfo = (struct vchiq_pagelist_info *)
435 (scatterlist + num_pages);
437 pagelist->length = count;
438 pagelist->type = type;
439 pagelist->offset = offset;
441 /* Populate the fields of the pagelistinfo structure */
442 pagelistinfo->pagelist = pagelist;
443 pagelistinfo->pagelist_buffer_size = pagelist_size;
444 pagelistinfo->dma_addr = dma_addr;
445 pagelistinfo->dma_dir = (type == PAGELIST_WRITE) ?
446 DMA_TO_DEVICE : DMA_FROM_DEVICE;
447 pagelistinfo->num_pages = num_pages;
448 pagelistinfo->pages_need_release = 0;
449 pagelistinfo->pages = pages;
450 pagelistinfo->scatterlist = scatterlist;
451 pagelistinfo->scatterlist_mapped = 0;
453 if (is_vmalloc_addr(buf)) {
454 unsigned long length = count;
455 unsigned int off = offset;
457 for (actual_pages = 0; actual_pages < num_pages;
459 struct page *pg = vmalloc_to_page(buf + (actual_pages *
461 size_t bytes = PAGE_SIZE - off;
464 cleanup_pagelistinfo(pagelistinfo);
470 pages[actual_pages] = pg;
474 /* do not try and release vmalloc pages */
476 actual_pages = get_user_pages_fast(
477 (unsigned long)buf & PAGE_MASK,
479 type == PAGELIST_READ,
482 if (actual_pages != num_pages) {
483 vchiq_log_info(vchiq_arm_log_level,
484 "%s - only %d/%d pages locked",
485 __func__, actual_pages, num_pages);
487 /* This is probably due to the process being killed */
488 while (actual_pages > 0)
491 put_page(pages[actual_pages]);
493 cleanup_pagelistinfo(pagelistinfo);
496 /* release user pages */
497 pagelistinfo->pages_need_release = 1;
501 * Initialize the scatterlist so that the magic cookie
502 * is filled if debugging is enabled
504 sg_init_table(scatterlist, num_pages);
505 /* Now set the pages for each scatterlist */
506 for (i = 0; i < num_pages; i++) {
507 unsigned int len = PAGE_SIZE - offset;
511 sg_set_page(scatterlist + i, pages[i], len, offset);
516 dma_buffers = dma_map_sg(g_dev,
519 pagelistinfo->dma_dir);
521 if (dma_buffers == 0) {
522 cleanup_pagelistinfo(pagelistinfo);
526 pagelistinfo->scatterlist_mapped = 1;
528 /* Combine adjacent blocks for performance */
530 for_each_sg(scatterlist, sg, dma_buffers, i) {
531 u32 len = sg_dma_len(sg);
532 u32 addr = sg_dma_address(sg);
534 /* Note: addrs is the address + page_count - 1
535 * The firmware expects blocks after the first to be page-
536 * aligned and a multiple of the page size
539 WARN_ON(i && (i != (dma_buffers - 1)) && (len & ~PAGE_MASK));
540 WARN_ON(i && (addr & ~PAGE_MASK));
542 ((addrs[k - 1] & PAGE_MASK) +
543 (((addrs[k - 1] & ~PAGE_MASK) + 1) << PAGE_SHIFT))
544 == (addr & PAGE_MASK))
545 addrs[k - 1] += ((len + PAGE_SIZE - 1) >> PAGE_SHIFT);
547 addrs[k++] = (addr & PAGE_MASK) |
548 (((len + PAGE_SIZE - 1) >> PAGE_SHIFT) - 1);
551 /* Partial cache lines (fragments) require special measures */
552 if ((type == PAGELIST_READ) &&
553 ((pagelist->offset & (g_cache_line_size - 1)) ||
554 ((pagelist->offset + pagelist->length) &
555 (g_cache_line_size - 1)))) {
558 if (down_interruptible(&g_free_fragments_sema) != 0) {
559 cleanup_pagelistinfo(pagelistinfo);
563 WARN_ON(g_free_fragments == NULL);
565 down(&g_free_fragments_mutex);
566 fragments = g_free_fragments;
567 WARN_ON(fragments == NULL);
568 g_free_fragments = *(char **) g_free_fragments;
569 up(&g_free_fragments_mutex);
570 pagelist->type = PAGELIST_READ_WITH_FRAGMENTS +
571 (fragments - g_fragments_base) / g_fragments_size;
578 free_pagelist(struct vchiq_pagelist_info *pagelistinfo,
581 PAGELIST_T *pagelist = pagelistinfo->pagelist;
582 struct page **pages = pagelistinfo->pages;
583 unsigned int num_pages = pagelistinfo->num_pages;
585 vchiq_log_trace(vchiq_arm_log_level, "free_pagelist - %pK, %d",
586 pagelistinfo->pagelist, actual);
589 * NOTE: dma_unmap_sg must be called before the
590 * cpu can touch any of the data/pages.
592 dma_unmap_sg(g_dev, pagelistinfo->scatterlist,
593 pagelistinfo->num_pages, pagelistinfo->dma_dir);
594 pagelistinfo->scatterlist_mapped = 0;
596 /* Deal with any partial cache lines (fragments) */
597 if (pagelist->type >= PAGELIST_READ_WITH_FRAGMENTS) {
598 char *fragments = g_fragments_base +
599 (pagelist->type - PAGELIST_READ_WITH_FRAGMENTS) *
601 int head_bytes, tail_bytes;
603 head_bytes = (g_cache_line_size - pagelist->offset) &
604 (g_cache_line_size - 1);
605 tail_bytes = (pagelist->offset + actual) &
606 (g_cache_line_size - 1);
608 if ((actual >= 0) && (head_bytes != 0)) {
609 if (head_bytes > actual)
612 memcpy((char *)kmap(pages[0]) +
618 if ((actual >= 0) && (head_bytes < actual) &&
620 memcpy((char *)kmap(pages[num_pages - 1]) +
621 ((pagelist->offset + actual) &
622 (PAGE_SIZE - 1) & ~(g_cache_line_size - 1)),
623 fragments + g_cache_line_size,
625 kunmap(pages[num_pages - 1]);
628 down(&g_free_fragments_mutex);
629 *(char **)fragments = g_free_fragments;
630 g_free_fragments = fragments;
631 up(&g_free_fragments_mutex);
632 up(&g_free_fragments_sema);
635 /* Need to mark all the pages dirty. */
636 if (pagelist->type != PAGELIST_WRITE &&
637 pagelistinfo->pages_need_release) {
640 for (i = 0; i < num_pages; i++)
641 set_page_dirty(pages[i]);
644 cleanup_pagelistinfo(pagelistinfo);