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 #define ARM_DS_ACTIVE BIT(2)
34 struct vchiq_2835_state {
36 struct vchiq_arm_state arm_state;
39 struct vchiq_pagelist_info {
40 struct pagelist *pagelist;
41 size_t pagelist_buffer_size;
43 enum dma_data_direction dma_dir;
44 unsigned int num_pages;
45 unsigned int pages_need_release;
47 struct scatterlist *scatterlist;
48 unsigned int scatterlist_mapped;
51 static void __iomem *g_regs;
52 /* This value is the size of the L2 cache lines as understood by the
53 * VPU firmware, which determines the required alignment of the
54 * offsets/sizes in pagelists.
56 * Modern VPU firmware looks for a DT "cache-line-size" property in
57 * the VCHIQ node and will overwrite it with the actual L2 cache size,
58 * which the kernel must then respect. That property was rejected
59 * upstream, so we have to use the VPU firmware's compatibility value
62 static unsigned int g_cache_line_size = 32;
63 static unsigned int g_fragments_size;
64 static char *g_fragments_base;
65 static char *g_free_fragments;
66 static struct semaphore g_free_fragments_sema;
67 static struct device *g_dev;
69 static DEFINE_SEMAPHORE(g_free_fragments_mutex);
72 vchiq_doorbell_irq(int irq, void *dev_id);
74 static struct vchiq_pagelist_info *
75 create_pagelist(char *buf, char __user *ubuf, size_t count, unsigned short type);
78 free_pagelist(struct vchiq_pagelist_info *pagelistinfo,
81 int vchiq_platform_init(struct platform_device *pdev, struct vchiq_state *state)
83 struct device *dev = &pdev->dev;
84 struct vchiq_drvdata *drvdata = platform_get_drvdata(pdev);
85 struct rpi_firmware *fw = drvdata->fw;
86 struct vchiq_slot_zero *vchiq_slot_zero;
90 int slot_mem_size, frag_mem_size;
94 * VCHI messages between the CPU and firmware use
95 * 32-bit bus addresses.
97 err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
102 g_cache_line_size = drvdata->cache_line_size;
103 g_fragments_size = 2 * g_cache_line_size;
105 /* Allocate space for the channels in coherent memory */
106 slot_mem_size = PAGE_ALIGN(TOTAL_SLOTS * VCHIQ_SLOT_SIZE);
107 frag_mem_size = PAGE_ALIGN(g_fragments_size * MAX_FRAGMENTS);
109 slot_mem = dmam_alloc_coherent(dev, slot_mem_size + frag_mem_size,
110 &slot_phys, GFP_KERNEL);
112 dev_err(dev, "could not allocate DMA memory\n");
116 WARN_ON(((unsigned long)slot_mem & (PAGE_SIZE - 1)) != 0);
118 vchiq_slot_zero = vchiq_init_slots(slot_mem, slot_mem_size);
119 if (!vchiq_slot_zero)
122 vchiq_slot_zero->platform_data[VCHIQ_PLATFORM_FRAGMENTS_OFFSET_IDX] =
123 (int)slot_phys + slot_mem_size;
124 vchiq_slot_zero->platform_data[VCHIQ_PLATFORM_FRAGMENTS_COUNT_IDX] =
127 g_fragments_base = (char *)slot_mem + slot_mem_size;
129 g_free_fragments = g_fragments_base;
130 for (i = 0; i < (MAX_FRAGMENTS - 1); i++) {
131 *(char **)&g_fragments_base[i*g_fragments_size] =
132 &g_fragments_base[(i + 1)*g_fragments_size];
134 *(char **)&g_fragments_base[i * g_fragments_size] = NULL;
135 sema_init(&g_free_fragments_sema, MAX_FRAGMENTS);
137 err = vchiq_init_state(state, vchiq_slot_zero);
141 g_regs = devm_platform_ioremap_resource(pdev, 0);
143 return PTR_ERR(g_regs);
145 irq = platform_get_irq(pdev, 0);
149 err = devm_request_irq(dev, irq, vchiq_doorbell_irq, IRQF_IRQPOLL,
150 "VCHIQ doorbell", state);
152 dev_err(dev, "failed to register irq=%d\n", irq);
156 /* Send the base address of the slots to VideoCore */
157 channelbase = slot_phys;
158 err = rpi_firmware_property(fw, RPI_FIRMWARE_VCHIQ_INIT,
159 &channelbase, sizeof(channelbase));
160 if (err || channelbase) {
161 dev_err(dev, "failed to set channelbase\n");
162 return err ? : -ENXIO;
166 vchiq_log_info(vchiq_arm_log_level,
167 "vchiq_init - done (slots %pK, phys %pad)",
168 vchiq_slot_zero, &slot_phys);
170 vchiq_call_connected_callbacks();
176 vchiq_platform_init_state(struct vchiq_state *state)
178 struct vchiq_2835_state *platform_state;
180 state->platform_state = kzalloc(sizeof(*platform_state), GFP_KERNEL);
181 if (!state->platform_state)
184 platform_state = (struct vchiq_2835_state *)state->platform_state;
186 platform_state->inited = 1;
187 vchiq_arm_init_state(state, &platform_state->arm_state);
192 struct vchiq_arm_state*
193 vchiq_platform_get_arm_state(struct vchiq_state *state)
195 struct vchiq_2835_state *platform_state;
197 platform_state = (struct vchiq_2835_state *)state->platform_state;
199 WARN_ON_ONCE(!platform_state->inited);
201 return &platform_state->arm_state;
205 remote_event_signal(struct remote_event *event)
211 dsb(sy); /* data barrier operation */
214 writel(0, g_regs + BELL2); /* trigger vc interrupt */
218 vchiq_prepare_bulk_data(struct vchiq_bulk *bulk, void *offset,
219 void __user *uoffset, int size, int dir)
221 struct vchiq_pagelist_info *pagelistinfo;
223 pagelistinfo = create_pagelist(offset, uoffset, size,
224 (dir == VCHIQ_BULK_RECEIVE)
231 bulk->data = pagelistinfo->dma_addr;
234 * Store the pagelistinfo address in remote_data,
235 * which isn't used by the slave.
237 bulk->remote_data = pagelistinfo;
243 vchiq_complete_bulk(struct vchiq_bulk *bulk)
245 if (bulk && bulk->remote_data && bulk->actual)
246 free_pagelist((struct vchiq_pagelist_info *)bulk->remote_data,
250 int vchiq_dump_platform_state(void *dump_context)
255 len = snprintf(buf, sizeof(buf),
256 " Platform: 2835 (VC master)");
257 return vchiq_dump(dump_context, buf, len + 1);
265 vchiq_doorbell_irq(int irq, void *dev_id)
267 struct vchiq_state *state = dev_id;
268 irqreturn_t ret = IRQ_NONE;
271 /* Read (and clear) the doorbell */
272 status = readl(g_regs + BELL0);
274 if (status & ARM_DS_ACTIVE) { /* Was the doorbell rung? */
275 remote_event_pollall(state);
283 cleanup_pagelistinfo(struct vchiq_pagelist_info *pagelistinfo)
285 if (pagelistinfo->scatterlist_mapped) {
286 dma_unmap_sg(g_dev, pagelistinfo->scatterlist,
287 pagelistinfo->num_pages, pagelistinfo->dma_dir);
290 if (pagelistinfo->pages_need_release)
291 unpin_user_pages(pagelistinfo->pages, pagelistinfo->num_pages);
293 dma_free_coherent(g_dev, pagelistinfo->pagelist_buffer_size,
294 pagelistinfo->pagelist, pagelistinfo->dma_addr);
297 /* There is a potential problem with partial cache lines (pages?)
298 * at the ends of the block when reading. If the CPU accessed anything in
299 * the same line (page?) then it may have pulled old data into the cache,
300 * obscuring the new data underneath. We can solve this by transferring the
301 * partial cache lines separately, and allowing the ARM to copy into the
305 static struct vchiq_pagelist_info *
306 create_pagelist(char *buf, char __user *ubuf,
307 size_t count, unsigned short type)
309 struct pagelist *pagelist;
310 struct vchiq_pagelist_info *pagelistinfo;
313 unsigned int num_pages, offset, i, k;
315 size_t pagelist_size;
316 struct scatterlist *scatterlist, *sg;
320 if (count >= INT_MAX - PAGE_SIZE)
324 offset = (uintptr_t)buf & (PAGE_SIZE - 1);
326 offset = (uintptr_t)ubuf & (PAGE_SIZE - 1);
327 num_pages = DIV_ROUND_UP(count + offset, PAGE_SIZE);
329 if (num_pages > (SIZE_MAX - sizeof(struct pagelist) -
330 sizeof(struct vchiq_pagelist_info)) /
331 (sizeof(u32) + sizeof(pages[0]) +
332 sizeof(struct scatterlist)))
335 pagelist_size = sizeof(struct pagelist) +
336 (num_pages * sizeof(u32)) +
337 (num_pages * sizeof(pages[0]) +
338 (num_pages * sizeof(struct scatterlist))) +
339 sizeof(struct vchiq_pagelist_info);
341 /* Allocate enough storage to hold the page pointers and the page
344 pagelist = dma_alloc_coherent(g_dev, pagelist_size, &dma_addr,
347 vchiq_log_trace(vchiq_arm_log_level, "%s - %pK", __func__, pagelist);
352 addrs = pagelist->addrs;
353 pages = (struct page **)(addrs + num_pages);
354 scatterlist = (struct scatterlist *)(pages + num_pages);
355 pagelistinfo = (struct vchiq_pagelist_info *)
356 (scatterlist + num_pages);
358 pagelist->length = count;
359 pagelist->type = type;
360 pagelist->offset = offset;
362 /* Populate the fields of the pagelistinfo structure */
363 pagelistinfo->pagelist = pagelist;
364 pagelistinfo->pagelist_buffer_size = pagelist_size;
365 pagelistinfo->dma_addr = dma_addr;
366 pagelistinfo->dma_dir = (type == PAGELIST_WRITE) ?
367 DMA_TO_DEVICE : DMA_FROM_DEVICE;
368 pagelistinfo->num_pages = num_pages;
369 pagelistinfo->pages_need_release = 0;
370 pagelistinfo->pages = pages;
371 pagelistinfo->scatterlist = scatterlist;
372 pagelistinfo->scatterlist_mapped = 0;
375 unsigned long length = count;
376 unsigned int off = offset;
378 for (actual_pages = 0; actual_pages < num_pages;
381 vmalloc_to_page((buf +
382 (actual_pages * PAGE_SIZE)));
383 size_t bytes = PAGE_SIZE - off;
386 cleanup_pagelistinfo(pagelistinfo);
392 pages[actual_pages] = pg;
396 /* do not try and release vmalloc pages */
398 actual_pages = pin_user_pages_fast(
399 (unsigned long)ubuf & PAGE_MASK,
401 type == PAGELIST_READ,
404 if (actual_pages != num_pages) {
405 vchiq_log_info(vchiq_arm_log_level,
406 "%s - only %d/%d pages locked",
407 __func__, actual_pages, num_pages);
409 /* This is probably due to the process being killed */
410 if (actual_pages > 0)
411 unpin_user_pages(pages, actual_pages);
412 cleanup_pagelistinfo(pagelistinfo);
415 /* release user pages */
416 pagelistinfo->pages_need_release = 1;
420 * Initialize the scatterlist so that the magic cookie
421 * is filled if debugging is enabled
423 sg_init_table(scatterlist, num_pages);
424 /* Now set the pages for each scatterlist */
425 for (i = 0; i < num_pages; i++) {
426 unsigned int len = PAGE_SIZE - offset;
430 sg_set_page(scatterlist + i, pages[i], len, offset);
435 dma_buffers = dma_map_sg(g_dev,
438 pagelistinfo->dma_dir);
440 if (dma_buffers == 0) {
441 cleanup_pagelistinfo(pagelistinfo);
445 pagelistinfo->scatterlist_mapped = 1;
447 /* Combine adjacent blocks for performance */
449 for_each_sg(scatterlist, sg, dma_buffers, i) {
450 u32 len = sg_dma_len(sg);
451 u32 addr = sg_dma_address(sg);
453 /* Note: addrs is the address + page_count - 1
454 * The firmware expects blocks after the first to be page-
455 * aligned and a multiple of the page size
458 WARN_ON(i && (i != (dma_buffers - 1)) && (len & ~PAGE_MASK));
459 WARN_ON(i && (addr & ~PAGE_MASK));
461 ((addrs[k - 1] & PAGE_MASK) +
462 (((addrs[k - 1] & ~PAGE_MASK) + 1) << PAGE_SHIFT))
463 == (addr & PAGE_MASK))
464 addrs[k - 1] += ((len + PAGE_SIZE - 1) >> PAGE_SHIFT);
466 addrs[k++] = (addr & PAGE_MASK) |
467 (((len + PAGE_SIZE - 1) >> PAGE_SHIFT) - 1);
470 /* Partial cache lines (fragments) require special measures */
471 if ((type == PAGELIST_READ) &&
472 ((pagelist->offset & (g_cache_line_size - 1)) ||
473 ((pagelist->offset + pagelist->length) &
474 (g_cache_line_size - 1)))) {
477 if (down_interruptible(&g_free_fragments_sema)) {
478 cleanup_pagelistinfo(pagelistinfo);
482 WARN_ON(!g_free_fragments);
484 down(&g_free_fragments_mutex);
485 fragments = g_free_fragments;
487 g_free_fragments = *(char **) g_free_fragments;
488 up(&g_free_fragments_mutex);
489 pagelist->type = PAGELIST_READ_WITH_FRAGMENTS +
490 (fragments - g_fragments_base) / g_fragments_size;
497 free_pagelist(struct vchiq_pagelist_info *pagelistinfo,
500 struct pagelist *pagelist = pagelistinfo->pagelist;
501 struct page **pages = pagelistinfo->pages;
502 unsigned int num_pages = pagelistinfo->num_pages;
504 vchiq_log_trace(vchiq_arm_log_level, "%s - %pK, %d",
505 __func__, pagelistinfo->pagelist, actual);
508 * NOTE: dma_unmap_sg must be called before the
509 * cpu can touch any of the data/pages.
511 dma_unmap_sg(g_dev, pagelistinfo->scatterlist,
512 pagelistinfo->num_pages, pagelistinfo->dma_dir);
513 pagelistinfo->scatterlist_mapped = 0;
515 /* Deal with any partial cache lines (fragments) */
516 if (pagelist->type >= PAGELIST_READ_WITH_FRAGMENTS) {
517 char *fragments = g_fragments_base +
518 (pagelist->type - PAGELIST_READ_WITH_FRAGMENTS) *
520 int head_bytes, tail_bytes;
522 head_bytes = (g_cache_line_size - pagelist->offset) &
523 (g_cache_line_size - 1);
524 tail_bytes = (pagelist->offset + actual) &
525 (g_cache_line_size - 1);
527 if ((actual >= 0) && (head_bytes != 0)) {
528 if (head_bytes > actual)
531 memcpy((char *)kmap(pages[0]) +
537 if ((actual >= 0) && (head_bytes < actual) &&
539 memcpy((char *)kmap(pages[num_pages - 1]) +
540 ((pagelist->offset + actual) &
541 (PAGE_SIZE - 1) & ~(g_cache_line_size - 1)),
542 fragments + g_cache_line_size,
544 kunmap(pages[num_pages - 1]);
547 down(&g_free_fragments_mutex);
548 *(char **)fragments = g_free_fragments;
549 g_free_fragments = fragments;
550 up(&g_free_fragments_mutex);
551 up(&g_free_fragments_sema);
554 /* Need to mark all the pages dirty. */
555 if (pagelist->type != PAGELIST_WRITE &&
556 pagelistinfo->pages_need_release) {
559 for (i = 0; i < num_pages; i++)
560 set_page_dirty(pages[i]);
563 cleanup_pagelistinfo(pagelistinfo);