1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
2 /* Copyright (c) 2010-2012 Broadcom. All rights reserved. */
4 #include <linux/kernel.h>
5 #include <linux/types.h>
6 #include <linux/errno.h>
7 #include <linux/interrupt.h>
8 #include <linux/pagemap.h>
9 #include <linux/dma-mapping.h>
11 #include <linux/platform_device.h>
12 #include <linux/uaccess.h>
15 #include <linux/slab.h>
16 #include <soc/bcm2835/raspberrypi-firmware.h>
18 #define TOTAL_SLOTS (VCHIQ_SLOT_ZERO_SLOTS + 2 * 32)
20 #include "vchiq_arm.h"
21 #include "vchiq_connected.h"
22 #include "vchiq_pagelist.h"
24 #define MAX_FRAGMENTS (VCHIQ_NUM_CURRENT_BULKS * 2)
26 #define VCHIQ_PLATFORM_FRAGMENTS_OFFSET_IDX 0
27 #define VCHIQ_PLATFORM_FRAGMENTS_COUNT_IDX 1
32 struct vchiq_2835_state {
34 struct vchiq_arm_state arm_state;
37 struct vchiq_pagelist_info {
38 struct pagelist *pagelist;
39 size_t pagelist_buffer_size;
41 enum dma_data_direction dma_dir;
42 unsigned int num_pages;
43 unsigned int pages_need_release;
45 struct scatterlist *scatterlist;
46 unsigned int scatterlist_mapped;
49 static void __iomem *g_regs;
50 /* This value is the size of the L2 cache lines as understood by the
51 * VPU firmware, which determines the required alignment of the
52 * offsets/sizes in pagelists.
54 * Modern VPU firmware looks for a DT "cache-line-size" property in
55 * the VCHIQ node and will overwrite it with the actual L2 cache size,
56 * which the kernel must then respect. That property was rejected
57 * upstream, so we have to use the VPU firmware's compatibility value
60 static unsigned int g_cache_line_size = 32;
61 static unsigned int g_fragments_size;
62 static char *g_fragments_base;
63 static char *g_free_fragments;
64 static struct semaphore g_free_fragments_sema;
65 static struct device *g_dev;
67 static DEFINE_SEMAPHORE(g_free_fragments_mutex);
70 vchiq_doorbell_irq(int irq, void *dev_id);
72 static struct vchiq_pagelist_info *
73 create_pagelist(char *buf, char __user *ubuf, size_t count, unsigned short type);
76 free_pagelist(struct vchiq_pagelist_info *pagelistinfo,
79 int vchiq_platform_init(struct platform_device *pdev, struct vchiq_state *state)
81 struct device *dev = &pdev->dev;
82 struct vchiq_drvdata *drvdata = platform_get_drvdata(pdev);
83 struct rpi_firmware *fw = drvdata->fw;
84 struct vchiq_slot_zero *vchiq_slot_zero;
88 int slot_mem_size, frag_mem_size;
92 * VCHI messages between the CPU and firmware use
93 * 32-bit bus addresses.
95 err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
100 g_cache_line_size = drvdata->cache_line_size;
101 g_fragments_size = 2 * g_cache_line_size;
103 /* Allocate space for the channels in coherent memory */
104 slot_mem_size = PAGE_ALIGN(TOTAL_SLOTS * VCHIQ_SLOT_SIZE);
105 frag_mem_size = PAGE_ALIGN(g_fragments_size * MAX_FRAGMENTS);
107 slot_mem = dmam_alloc_coherent(dev, slot_mem_size + frag_mem_size,
108 &slot_phys, GFP_KERNEL);
110 dev_err(dev, "could not allocate DMA memory\n");
114 WARN_ON(((unsigned long)slot_mem & (PAGE_SIZE - 1)) != 0);
116 vchiq_slot_zero = vchiq_init_slots(slot_mem, slot_mem_size);
117 if (!vchiq_slot_zero)
120 vchiq_slot_zero->platform_data[VCHIQ_PLATFORM_FRAGMENTS_OFFSET_IDX] =
121 (int)slot_phys + slot_mem_size;
122 vchiq_slot_zero->platform_data[VCHIQ_PLATFORM_FRAGMENTS_COUNT_IDX] =
125 g_fragments_base = (char *)slot_mem + slot_mem_size;
127 g_free_fragments = g_fragments_base;
128 for (i = 0; i < (MAX_FRAGMENTS - 1); i++) {
129 *(char **)&g_fragments_base[i*g_fragments_size] =
130 &g_fragments_base[(i + 1)*g_fragments_size];
132 *(char **)&g_fragments_base[i * g_fragments_size] = NULL;
133 sema_init(&g_free_fragments_sema, MAX_FRAGMENTS);
135 if (vchiq_init_state(state, vchiq_slot_zero) != VCHIQ_SUCCESS)
138 g_regs = devm_platform_ioremap_resource(pdev, 0);
140 return PTR_ERR(g_regs);
142 irq = platform_get_irq(pdev, 0);
146 err = devm_request_irq(dev, irq, vchiq_doorbell_irq, IRQF_IRQPOLL,
147 "VCHIQ doorbell", state);
149 dev_err(dev, "failed to register irq=%d\n", irq);
153 /* Send the base address of the slots to VideoCore */
154 channelbase = slot_phys;
155 err = rpi_firmware_property(fw, RPI_FIRMWARE_VCHIQ_INIT,
156 &channelbase, sizeof(channelbase));
157 if (err || channelbase) {
158 dev_err(dev, "failed to set channelbase\n");
159 return err ? : -ENXIO;
163 vchiq_log_info(vchiq_arm_log_level,
164 "vchiq_init - done (slots %pK, phys %pad)",
165 vchiq_slot_zero, &slot_phys);
167 vchiq_call_connected_callbacks();
173 vchiq_platform_init_state(struct vchiq_state *state)
175 struct vchiq_2835_state *platform_state;
177 state->platform_state = kzalloc(sizeof(*platform_state), GFP_KERNEL);
178 if (!state->platform_state)
181 platform_state = (struct vchiq_2835_state *)state->platform_state;
183 platform_state->inited = 1;
184 vchiq_arm_init_state(state, &platform_state->arm_state);
189 struct vchiq_arm_state*
190 vchiq_platform_get_arm_state(struct vchiq_state *state)
192 struct vchiq_2835_state *platform_state;
194 platform_state = (struct vchiq_2835_state *)state->platform_state;
196 WARN_ON_ONCE(!platform_state->inited);
198 return &platform_state->arm_state;
202 remote_event_signal(struct remote_event *event)
208 dsb(sy); /* data barrier operation */
211 writel(0, g_regs + BELL2); /* trigger vc interrupt */
215 vchiq_prepare_bulk_data(struct vchiq_bulk *bulk, void *offset,
216 void __user *uoffset, int size, int dir)
218 struct vchiq_pagelist_info *pagelistinfo;
220 pagelistinfo = create_pagelist(offset, uoffset, size,
221 (dir == VCHIQ_BULK_RECEIVE)
228 bulk->data = pagelistinfo->dma_addr;
231 * Store the pagelistinfo address in remote_data,
232 * which isn't used by the slave.
234 bulk->remote_data = pagelistinfo;
240 vchiq_complete_bulk(struct vchiq_bulk *bulk)
242 if (bulk && bulk->remote_data && bulk->actual)
243 free_pagelist((struct vchiq_pagelist_info *)bulk->remote_data,
247 int vchiq_dump_platform_state(void *dump_context)
252 len = snprintf(buf, sizeof(buf),
253 " Platform: 2835 (VC master)");
254 return vchiq_dump(dump_context, buf, len + 1);
262 vchiq_doorbell_irq(int irq, void *dev_id)
264 struct vchiq_state *state = dev_id;
265 irqreturn_t ret = IRQ_NONE;
268 /* Read (and clear) the doorbell */
269 status = readl(g_regs + BELL0);
271 if (status & 0x4) { /* Was the doorbell rung? */
272 remote_event_pollall(state);
280 cleanup_pagelistinfo(struct vchiq_pagelist_info *pagelistinfo)
282 if (pagelistinfo->scatterlist_mapped) {
283 dma_unmap_sg(g_dev, pagelistinfo->scatterlist,
284 pagelistinfo->num_pages, pagelistinfo->dma_dir);
287 if (pagelistinfo->pages_need_release)
288 unpin_user_pages(pagelistinfo->pages, pagelistinfo->num_pages);
290 dma_free_coherent(g_dev, pagelistinfo->pagelist_buffer_size,
291 pagelistinfo->pagelist, pagelistinfo->dma_addr);
294 /* There is a potential problem with partial cache lines (pages?)
295 * at the ends of the block when reading. If the CPU accessed anything in
296 * the same line (page?) then it may have pulled old data into the cache,
297 * obscuring the new data underneath. We can solve this by transferring the
298 * partial cache lines separately, and allowing the ARM to copy into the
302 static struct vchiq_pagelist_info *
303 create_pagelist(char *buf, char __user *ubuf,
304 size_t count, unsigned short type)
306 struct pagelist *pagelist;
307 struct vchiq_pagelist_info *pagelistinfo;
310 unsigned int num_pages, offset, i, k;
312 size_t pagelist_size;
313 struct scatterlist *scatterlist, *sg;
317 if (count >= INT_MAX - PAGE_SIZE)
321 offset = (uintptr_t)buf & (PAGE_SIZE - 1);
323 offset = (uintptr_t)ubuf & (PAGE_SIZE - 1);
324 num_pages = DIV_ROUND_UP(count + offset, PAGE_SIZE);
326 if (num_pages > (SIZE_MAX - sizeof(struct pagelist) -
327 sizeof(struct vchiq_pagelist_info)) /
328 (sizeof(u32) + sizeof(pages[0]) +
329 sizeof(struct scatterlist)))
332 pagelist_size = sizeof(struct pagelist) +
333 (num_pages * sizeof(u32)) +
334 (num_pages * sizeof(pages[0]) +
335 (num_pages * sizeof(struct scatterlist))) +
336 sizeof(struct vchiq_pagelist_info);
338 /* Allocate enough storage to hold the page pointers and the page
341 pagelist = dma_alloc_coherent(g_dev, pagelist_size, &dma_addr,
344 vchiq_log_trace(vchiq_arm_log_level, "%s - %pK", __func__, pagelist);
349 addrs = pagelist->addrs;
350 pages = (struct page **)(addrs + num_pages);
351 scatterlist = (struct scatterlist *)(pages + num_pages);
352 pagelistinfo = (struct vchiq_pagelist_info *)
353 (scatterlist + num_pages);
355 pagelist->length = count;
356 pagelist->type = type;
357 pagelist->offset = offset;
359 /* Populate the fields of the pagelistinfo structure */
360 pagelistinfo->pagelist = pagelist;
361 pagelistinfo->pagelist_buffer_size = pagelist_size;
362 pagelistinfo->dma_addr = dma_addr;
363 pagelistinfo->dma_dir = (type == PAGELIST_WRITE) ?
364 DMA_TO_DEVICE : DMA_FROM_DEVICE;
365 pagelistinfo->num_pages = num_pages;
366 pagelistinfo->pages_need_release = 0;
367 pagelistinfo->pages = pages;
368 pagelistinfo->scatterlist = scatterlist;
369 pagelistinfo->scatterlist_mapped = 0;
372 unsigned long length = count;
373 unsigned int off = offset;
375 for (actual_pages = 0; actual_pages < num_pages;
378 vmalloc_to_page((buf +
379 (actual_pages * PAGE_SIZE)));
380 size_t bytes = PAGE_SIZE - off;
383 cleanup_pagelistinfo(pagelistinfo);
389 pages[actual_pages] = pg;
393 /* do not try and release vmalloc pages */
395 actual_pages = pin_user_pages_fast(
396 (unsigned long)ubuf & PAGE_MASK,
398 type == PAGELIST_READ,
401 if (actual_pages != num_pages) {
402 vchiq_log_info(vchiq_arm_log_level,
403 "%s - only %d/%d pages locked",
404 __func__, actual_pages, num_pages);
406 /* This is probably due to the process being killed */
407 if (actual_pages > 0)
408 unpin_user_pages(pages, actual_pages);
409 cleanup_pagelistinfo(pagelistinfo);
412 /* release user pages */
413 pagelistinfo->pages_need_release = 1;
417 * Initialize the scatterlist so that the magic cookie
418 * is filled if debugging is enabled
420 sg_init_table(scatterlist, num_pages);
421 /* Now set the pages for each scatterlist */
422 for (i = 0; i < num_pages; i++) {
423 unsigned int len = PAGE_SIZE - offset;
427 sg_set_page(scatterlist + i, pages[i], len, offset);
432 dma_buffers = dma_map_sg(g_dev,
435 pagelistinfo->dma_dir);
437 if (dma_buffers == 0) {
438 cleanup_pagelistinfo(pagelistinfo);
442 pagelistinfo->scatterlist_mapped = 1;
444 /* Combine adjacent blocks for performance */
446 for_each_sg(scatterlist, sg, dma_buffers, i) {
447 u32 len = sg_dma_len(sg);
448 u32 addr = sg_dma_address(sg);
450 /* Note: addrs is the address + page_count - 1
451 * The firmware expects blocks after the first to be page-
452 * aligned and a multiple of the page size
455 WARN_ON(i && (i != (dma_buffers - 1)) && (len & ~PAGE_MASK));
456 WARN_ON(i && (addr & ~PAGE_MASK));
458 ((addrs[k - 1] & PAGE_MASK) +
459 (((addrs[k - 1] & ~PAGE_MASK) + 1) << PAGE_SHIFT))
460 == (addr & PAGE_MASK))
461 addrs[k - 1] += ((len + PAGE_SIZE - 1) >> PAGE_SHIFT);
463 addrs[k++] = (addr & PAGE_MASK) |
464 (((len + PAGE_SIZE - 1) >> PAGE_SHIFT) - 1);
467 /* Partial cache lines (fragments) require special measures */
468 if ((type == PAGELIST_READ) &&
469 ((pagelist->offset & (g_cache_line_size - 1)) ||
470 ((pagelist->offset + pagelist->length) &
471 (g_cache_line_size - 1)))) {
474 if (down_interruptible(&g_free_fragments_sema)) {
475 cleanup_pagelistinfo(pagelistinfo);
479 WARN_ON(!g_free_fragments);
481 down(&g_free_fragments_mutex);
482 fragments = g_free_fragments;
484 g_free_fragments = *(char **) g_free_fragments;
485 up(&g_free_fragments_mutex);
486 pagelist->type = PAGELIST_READ_WITH_FRAGMENTS +
487 (fragments - g_fragments_base) / g_fragments_size;
494 free_pagelist(struct vchiq_pagelist_info *pagelistinfo,
497 struct pagelist *pagelist = pagelistinfo->pagelist;
498 struct page **pages = pagelistinfo->pages;
499 unsigned int num_pages = pagelistinfo->num_pages;
501 vchiq_log_trace(vchiq_arm_log_level, "%s - %pK, %d",
502 __func__, pagelistinfo->pagelist, actual);
505 * NOTE: dma_unmap_sg must be called before the
506 * cpu can touch any of the data/pages.
508 dma_unmap_sg(g_dev, pagelistinfo->scatterlist,
509 pagelistinfo->num_pages, pagelistinfo->dma_dir);
510 pagelistinfo->scatterlist_mapped = 0;
512 /* Deal with any partial cache lines (fragments) */
513 if (pagelist->type >= PAGELIST_READ_WITH_FRAGMENTS) {
514 char *fragments = g_fragments_base +
515 (pagelist->type - PAGELIST_READ_WITH_FRAGMENTS) *
517 int head_bytes, tail_bytes;
519 head_bytes = (g_cache_line_size - pagelist->offset) &
520 (g_cache_line_size - 1);
521 tail_bytes = (pagelist->offset + actual) &
522 (g_cache_line_size - 1);
524 if ((actual >= 0) && (head_bytes != 0)) {
525 if (head_bytes > actual)
528 memcpy((char *)kmap(pages[0]) +
534 if ((actual >= 0) && (head_bytes < actual) &&
536 memcpy((char *)kmap(pages[num_pages - 1]) +
537 ((pagelist->offset + actual) &
538 (PAGE_SIZE - 1) & ~(g_cache_line_size - 1)),
539 fragments + g_cache_line_size,
541 kunmap(pages[num_pages - 1]);
544 down(&g_free_fragments_mutex);
545 *(char **)fragments = g_free_fragments;
546 g_free_fragments = fragments;
547 up(&g_free_fragments_mutex);
548 up(&g_free_fragments_sema);
551 /* Need to mark all the pages dirty. */
552 if (pagelist->type != PAGELIST_WRITE &&
553 pagelistinfo->pages_need_release) {
556 for (i = 0; i < num_pages; i++)
557 set_page_dirty(pages[i]);
560 cleanup_pagelistinfo(pagelistinfo);