1 // SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
3 * Copyright (c) 2014 Raspberry Pi (Trading) Ltd. All rights reserved.
4 * Copyright (c) 2010-2012 Broadcom. All rights reserved.
7 #include <linux/kernel.h>
8 #include <linux/module.h>
9 #include <linux/sched/signal.h>
10 #include <linux/types.h>
11 #include <linux/errno.h>
12 #include <linux/cdev.h>
14 #include <linux/device.h>
16 #include <linux/highmem.h>
17 #include <linux/pagemap.h>
18 #include <linux/bug.h>
19 #include <linux/completion.h>
20 #include <linux/list.h>
22 #include <linux/platform_device.h>
23 #include <linux/compat.h>
24 #include <linux/dma-mapping.h>
25 #include <linux/rcupdate.h>
26 #include <linux/delay.h>
27 #include <linux/slab.h>
28 #include <linux/interrupt.h>
30 #include <linux/uaccess.h>
31 #include <soc/bcm2835/raspberrypi-firmware.h>
33 #include "vchiq_core.h"
34 #include "vchiq_ioctl.h"
35 #include "vchiq_arm.h"
36 #include "vchiq_debugfs.h"
37 #include "vchiq_connected.h"
38 #include "vchiq_pagelist.h"
40 #define DEVICE_NAME "vchiq"
42 #define TOTAL_SLOTS (VCHIQ_SLOT_ZERO_SLOTS + 2 * 32)
44 #define MAX_FRAGMENTS (VCHIQ_NUM_CURRENT_BULKS * 2)
46 #define VCHIQ_PLATFORM_FRAGMENTS_OFFSET_IDX 0
47 #define VCHIQ_PLATFORM_FRAGMENTS_COUNT_IDX 1
52 #define ARM_DS_ACTIVE BIT(2)
54 /* Override the default prefix, which would be vchiq_arm (from the filename) */
55 #undef MODULE_PARAM_PREFIX
56 #define MODULE_PARAM_PREFIX DEVICE_NAME "."
58 #define KEEPALIVE_VER 1
59 #define KEEPALIVE_VER_MIN KEEPALIVE_VER
61 /* Run time control of log level, based on KERN_XXX level. */
62 int vchiq_arm_log_level = VCHIQ_LOG_DEFAULT;
63 int vchiq_susp_log_level = VCHIQ_LOG_ERROR;
65 DEFINE_SPINLOCK(msg_queue_spinlock);
66 struct vchiq_state g_state;
68 static struct platform_device *bcm2835_camera;
69 static struct platform_device *bcm2835_audio;
71 struct vchiq_drvdata {
72 const unsigned int cache_line_size;
73 struct rpi_firmware *fw;
76 static struct vchiq_drvdata bcm2835_drvdata = {
77 .cache_line_size = 32,
80 static struct vchiq_drvdata bcm2836_drvdata = {
81 .cache_line_size = 64,
84 struct vchiq_arm_state {
85 /* Keepalive-related data */
86 struct task_struct *ka_thread;
87 struct completion ka_evt;
88 atomic_t ka_use_count;
89 atomic_t ka_use_ack_count;
90 atomic_t ka_release_count;
92 rwlock_t susp_res_lock;
94 struct vchiq_state *state;
97 * Global use count for videocore.
98 * This is equal to the sum of the use counts for all services. When
99 * this hits zero the videocore suspend procedure will be initiated.
101 int videocore_use_count;
104 * Use count to track requests from videocore peer.
105 * This use count is not associated with a service, so needs to be
106 * tracked separately with the state.
111 * Flag to indicate that the first vchiq connect has made it through.
112 * This means that both sides should be fully ready, and we should
113 * be able to suspend after this point.
118 struct vchiq_2835_state {
120 struct vchiq_arm_state arm_state;
123 struct vchiq_pagelist_info {
124 struct pagelist *pagelist;
125 size_t pagelist_buffer_size;
127 enum dma_data_direction dma_dir;
128 unsigned int num_pages;
129 unsigned int pages_need_release;
131 struct scatterlist *scatterlist;
132 unsigned int scatterlist_mapped;
135 static void __iomem *g_regs;
136 /* This value is the size of the L2 cache lines as understood by the
137 * VPU firmware, which determines the required alignment of the
138 * offsets/sizes in pagelists.
140 * Modern VPU firmware looks for a DT "cache-line-size" property in
141 * the VCHIQ node and will overwrite it with the actual L2 cache size,
142 * which the kernel must then respect. That property was rejected
143 * upstream, so we have to use the VPU firmware's compatibility value
146 static unsigned int g_cache_line_size = 32;
147 static unsigned int g_fragments_size;
148 static char *g_fragments_base;
149 static char *g_free_fragments;
150 static struct semaphore g_free_fragments_sema;
152 static DEFINE_SEMAPHORE(g_free_fragments_mutex, 1);
155 vchiq_blocking_bulk_transfer(struct vchiq_instance *instance, unsigned int handle, void *data,
156 unsigned int size, enum vchiq_bulk_dir dir);
159 vchiq_doorbell_irq(int irq, void *dev_id)
161 struct vchiq_state *state = dev_id;
162 irqreturn_t ret = IRQ_NONE;
165 /* Read (and clear) the doorbell */
166 status = readl(g_regs + BELL0);
168 if (status & ARM_DS_ACTIVE) { /* Was the doorbell rung? */
169 remote_event_pollall(state);
177 cleanup_pagelistinfo(struct vchiq_instance *instance, struct vchiq_pagelist_info *pagelistinfo)
179 if (pagelistinfo->scatterlist_mapped) {
180 dma_unmap_sg(instance->state->dev, pagelistinfo->scatterlist,
181 pagelistinfo->num_pages, pagelistinfo->dma_dir);
184 if (pagelistinfo->pages_need_release)
185 unpin_user_pages(pagelistinfo->pages, pagelistinfo->num_pages);
187 dma_free_coherent(instance->state->dev, pagelistinfo->pagelist_buffer_size,
188 pagelistinfo->pagelist, pagelistinfo->dma_addr);
192 is_adjacent_block(u32 *addrs, u32 addr, unsigned int k)
199 tmp = (addrs[k - 1] & PAGE_MASK) +
200 (((addrs[k - 1] & ~PAGE_MASK) + 1) << PAGE_SHIFT);
202 return tmp == (addr & PAGE_MASK);
205 /* There is a potential problem with partial cache lines (pages?)
206 * at the ends of the block when reading. If the CPU accessed anything in
207 * the same line (page?) then it may have pulled old data into the cache,
208 * obscuring the new data underneath. We can solve this by transferring the
209 * partial cache lines separately, and allowing the ARM to copy into the
213 static struct vchiq_pagelist_info *
214 create_pagelist(struct vchiq_instance *instance, char *buf, char __user *ubuf,
215 size_t count, unsigned short type)
217 struct pagelist *pagelist;
218 struct vchiq_pagelist_info *pagelistinfo;
221 unsigned int num_pages, offset, i, k;
223 size_t pagelist_size;
224 struct scatterlist *scatterlist, *sg;
228 if (count >= INT_MAX - PAGE_SIZE)
232 offset = (uintptr_t)buf & (PAGE_SIZE - 1);
234 offset = (uintptr_t)ubuf & (PAGE_SIZE - 1);
235 num_pages = DIV_ROUND_UP(count + offset, PAGE_SIZE);
237 if ((size_t)num_pages > (SIZE_MAX - sizeof(struct pagelist) -
238 sizeof(struct vchiq_pagelist_info)) /
239 (sizeof(u32) + sizeof(pages[0]) +
240 sizeof(struct scatterlist)))
243 pagelist_size = sizeof(struct pagelist) +
244 (num_pages * sizeof(u32)) +
245 (num_pages * sizeof(pages[0]) +
246 (num_pages * sizeof(struct scatterlist))) +
247 sizeof(struct vchiq_pagelist_info);
249 /* Allocate enough storage to hold the page pointers and the page
252 pagelist = dma_alloc_coherent(instance->state->dev, pagelist_size, &dma_addr,
255 vchiq_log_trace(vchiq_arm_log_level, "%s - %pK", __func__, pagelist);
260 addrs = pagelist->addrs;
261 pages = (struct page **)(addrs + num_pages);
262 scatterlist = (struct scatterlist *)(pages + num_pages);
263 pagelistinfo = (struct vchiq_pagelist_info *)
264 (scatterlist + num_pages);
266 pagelist->length = count;
267 pagelist->type = type;
268 pagelist->offset = offset;
270 /* Populate the fields of the pagelistinfo structure */
271 pagelistinfo->pagelist = pagelist;
272 pagelistinfo->pagelist_buffer_size = pagelist_size;
273 pagelistinfo->dma_addr = dma_addr;
274 pagelistinfo->dma_dir = (type == PAGELIST_WRITE) ?
275 DMA_TO_DEVICE : DMA_FROM_DEVICE;
276 pagelistinfo->num_pages = num_pages;
277 pagelistinfo->pages_need_release = 0;
278 pagelistinfo->pages = pages;
279 pagelistinfo->scatterlist = scatterlist;
280 pagelistinfo->scatterlist_mapped = 0;
283 unsigned long length = count;
284 unsigned int off = offset;
286 for (actual_pages = 0; actual_pages < num_pages;
289 vmalloc_to_page((buf +
290 (actual_pages * PAGE_SIZE)));
291 size_t bytes = PAGE_SIZE - off;
294 cleanup_pagelistinfo(instance, pagelistinfo);
300 pages[actual_pages] = pg;
304 /* do not try and release vmalloc pages */
306 actual_pages = pin_user_pages_fast((unsigned long)ubuf & PAGE_MASK, num_pages,
307 type == PAGELIST_READ, pages);
309 if (actual_pages != num_pages) {
310 vchiq_log_info(vchiq_arm_log_level,
311 "%s - only %d/%d pages locked",
312 __func__, actual_pages, num_pages);
314 /* This is probably due to the process being killed */
315 if (actual_pages > 0)
316 unpin_user_pages(pages, actual_pages);
317 cleanup_pagelistinfo(instance, pagelistinfo);
320 /* release user pages */
321 pagelistinfo->pages_need_release = 1;
325 * Initialize the scatterlist so that the magic cookie
326 * is filled if debugging is enabled
328 sg_init_table(scatterlist, num_pages);
329 /* Now set the pages for each scatterlist */
330 for (i = 0; i < num_pages; i++) {
331 unsigned int len = PAGE_SIZE - offset;
335 sg_set_page(scatterlist + i, pages[i], len, offset);
340 dma_buffers = dma_map_sg(instance->state->dev,
343 pagelistinfo->dma_dir);
345 if (dma_buffers == 0) {
346 cleanup_pagelistinfo(instance, pagelistinfo);
350 pagelistinfo->scatterlist_mapped = 1;
352 /* Combine adjacent blocks for performance */
354 for_each_sg(scatterlist, sg, dma_buffers, i) {
355 u32 len = sg_dma_len(sg);
356 u32 addr = sg_dma_address(sg);
358 /* Note: addrs is the address + page_count - 1
359 * The firmware expects blocks after the first to be page-
360 * aligned and a multiple of the page size
363 WARN_ON(i && (i != (dma_buffers - 1)) && (len & ~PAGE_MASK));
364 WARN_ON(i && (addr & ~PAGE_MASK));
365 if (is_adjacent_block(addrs, addr, k))
366 addrs[k - 1] += ((len + PAGE_SIZE - 1) >> PAGE_SHIFT);
368 addrs[k++] = (addr & PAGE_MASK) |
369 (((len + PAGE_SIZE - 1) >> PAGE_SHIFT) - 1);
372 /* Partial cache lines (fragments) require special measures */
373 if ((type == PAGELIST_READ) &&
374 ((pagelist->offset & (g_cache_line_size - 1)) ||
375 ((pagelist->offset + pagelist->length) &
376 (g_cache_line_size - 1)))) {
379 if (down_interruptible(&g_free_fragments_sema)) {
380 cleanup_pagelistinfo(instance, pagelistinfo);
384 WARN_ON(!g_free_fragments);
386 down(&g_free_fragments_mutex);
387 fragments = g_free_fragments;
389 g_free_fragments = *(char **)g_free_fragments;
390 up(&g_free_fragments_mutex);
391 pagelist->type = PAGELIST_READ_WITH_FRAGMENTS +
392 (fragments - g_fragments_base) / g_fragments_size;
399 free_pagelist(struct vchiq_instance *instance, struct vchiq_pagelist_info *pagelistinfo,
402 struct pagelist *pagelist = pagelistinfo->pagelist;
403 struct page **pages = pagelistinfo->pages;
404 unsigned int num_pages = pagelistinfo->num_pages;
406 vchiq_log_trace(vchiq_arm_log_level, "%s - %pK, %d",
407 __func__, pagelistinfo->pagelist, actual);
410 * NOTE: dma_unmap_sg must be called before the
411 * cpu can touch any of the data/pages.
413 dma_unmap_sg(instance->state->dev, pagelistinfo->scatterlist,
414 pagelistinfo->num_pages, pagelistinfo->dma_dir);
415 pagelistinfo->scatterlist_mapped = 0;
417 /* Deal with any partial cache lines (fragments) */
418 if (pagelist->type >= PAGELIST_READ_WITH_FRAGMENTS) {
419 char *fragments = g_fragments_base +
420 (pagelist->type - PAGELIST_READ_WITH_FRAGMENTS) *
422 int head_bytes, tail_bytes;
424 head_bytes = (g_cache_line_size - pagelist->offset) &
425 (g_cache_line_size - 1);
426 tail_bytes = (pagelist->offset + actual) &
427 (g_cache_line_size - 1);
429 if ((actual >= 0) && (head_bytes != 0)) {
430 if (head_bytes > actual)
433 memcpy_to_page(pages[0],
438 if ((actual >= 0) && (head_bytes < actual) &&
440 memcpy_to_page(pages[num_pages - 1],
441 (pagelist->offset + actual) &
442 (PAGE_SIZE - 1) & ~(g_cache_line_size - 1),
443 fragments + g_cache_line_size,
446 down(&g_free_fragments_mutex);
447 *(char **)fragments = g_free_fragments;
448 g_free_fragments = fragments;
449 up(&g_free_fragments_mutex);
450 up(&g_free_fragments_sema);
453 /* Need to mark all the pages dirty. */
454 if (pagelist->type != PAGELIST_WRITE &&
455 pagelistinfo->pages_need_release) {
458 for (i = 0; i < num_pages; i++)
459 set_page_dirty(pages[i]);
462 cleanup_pagelistinfo(instance, pagelistinfo);
465 int vchiq_platform_init(struct platform_device *pdev, struct vchiq_state *state)
467 struct device *dev = &pdev->dev;
468 struct vchiq_drvdata *drvdata = platform_get_drvdata(pdev);
469 struct rpi_firmware *fw = drvdata->fw;
470 struct vchiq_slot_zero *vchiq_slot_zero;
472 dma_addr_t slot_phys;
474 int slot_mem_size, frag_mem_size;
478 * VCHI messages between the CPU and firmware use
479 * 32-bit bus addresses.
481 err = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
486 g_cache_line_size = drvdata->cache_line_size;
487 g_fragments_size = 2 * g_cache_line_size;
489 /* Allocate space for the channels in coherent memory */
490 slot_mem_size = PAGE_ALIGN(TOTAL_SLOTS * VCHIQ_SLOT_SIZE);
491 frag_mem_size = PAGE_ALIGN(g_fragments_size * MAX_FRAGMENTS);
493 slot_mem = dmam_alloc_coherent(dev, slot_mem_size + frag_mem_size,
494 &slot_phys, GFP_KERNEL);
496 dev_err(dev, "could not allocate DMA memory\n");
500 WARN_ON(((unsigned long)slot_mem & (PAGE_SIZE - 1)) != 0);
502 vchiq_slot_zero = vchiq_init_slots(slot_mem, slot_mem_size);
503 if (!vchiq_slot_zero)
506 vchiq_slot_zero->platform_data[VCHIQ_PLATFORM_FRAGMENTS_OFFSET_IDX] =
507 (int)slot_phys + slot_mem_size;
508 vchiq_slot_zero->platform_data[VCHIQ_PLATFORM_FRAGMENTS_COUNT_IDX] =
511 g_fragments_base = (char *)slot_mem + slot_mem_size;
513 g_free_fragments = g_fragments_base;
514 for (i = 0; i < (MAX_FRAGMENTS - 1); i++) {
515 *(char **)&g_fragments_base[i * g_fragments_size] =
516 &g_fragments_base[(i + 1) * g_fragments_size];
518 *(char **)&g_fragments_base[i * g_fragments_size] = NULL;
519 sema_init(&g_free_fragments_sema, MAX_FRAGMENTS);
521 err = vchiq_init_state(state, vchiq_slot_zero, dev);
525 g_regs = devm_platform_ioremap_resource(pdev, 0);
527 return PTR_ERR(g_regs);
529 irq = platform_get_irq(pdev, 0);
533 err = devm_request_irq(dev, irq, vchiq_doorbell_irq, IRQF_IRQPOLL,
534 "VCHIQ doorbell", state);
536 dev_err(dev, "failed to register irq=%d\n", irq);
540 /* Send the base address of the slots to VideoCore */
541 channelbase = slot_phys;
542 err = rpi_firmware_property(fw, RPI_FIRMWARE_VCHIQ_INIT,
543 &channelbase, sizeof(channelbase));
545 dev_err(dev, "failed to send firmware property: %d\n", err);
550 dev_err(dev, "failed to set channelbase (response: %x)\n",
555 vchiq_log_info(vchiq_arm_log_level, "vchiq_init - done (slots %pK, phys %pad)",
556 vchiq_slot_zero, &slot_phys);
558 vchiq_call_connected_callbacks();
564 vchiq_arm_init_state(struct vchiq_state *state,
565 struct vchiq_arm_state *arm_state)
568 rwlock_init(&arm_state->susp_res_lock);
570 init_completion(&arm_state->ka_evt);
571 atomic_set(&arm_state->ka_use_count, 0);
572 atomic_set(&arm_state->ka_use_ack_count, 0);
573 atomic_set(&arm_state->ka_release_count, 0);
575 arm_state->state = state;
576 arm_state->first_connect = 0;
581 vchiq_platform_init_state(struct vchiq_state *state)
583 struct vchiq_2835_state *platform_state;
585 state->platform_state = kzalloc(sizeof(*platform_state), GFP_KERNEL);
586 if (!state->platform_state)
589 platform_state = (struct vchiq_2835_state *)state->platform_state;
591 platform_state->inited = 1;
592 vchiq_arm_init_state(state, &platform_state->arm_state);
597 static struct vchiq_arm_state *vchiq_platform_get_arm_state(struct vchiq_state *state)
599 struct vchiq_2835_state *platform_state;
601 platform_state = (struct vchiq_2835_state *)state->platform_state;
603 WARN_ON_ONCE(!platform_state->inited);
605 return &platform_state->arm_state;
609 remote_event_signal(struct remote_event *event)
612 * Ensure that all writes to shared data structures have completed
613 * before signalling the peer.
619 dsb(sy); /* data barrier operation */
622 writel(0, g_regs + BELL2); /* trigger vc interrupt */
626 vchiq_prepare_bulk_data(struct vchiq_instance *instance, struct vchiq_bulk *bulk, void *offset,
627 void __user *uoffset, int size, int dir)
629 struct vchiq_pagelist_info *pagelistinfo;
631 pagelistinfo = create_pagelist(instance, offset, uoffset, size,
632 (dir == VCHIQ_BULK_RECEIVE)
639 bulk->data = pagelistinfo->dma_addr;
642 * Store the pagelistinfo address in remote_data,
643 * which isn't used by the slave.
645 bulk->remote_data = pagelistinfo;
651 vchiq_complete_bulk(struct vchiq_instance *instance, struct vchiq_bulk *bulk)
653 if (bulk && bulk->remote_data && bulk->actual)
654 free_pagelist(instance, (struct vchiq_pagelist_info *)bulk->remote_data,
658 int vchiq_dump_platform_state(void *dump_context)
663 len = snprintf(buf, sizeof(buf), " Platform: 2835 (VC master)");
664 return vchiq_dump(dump_context, buf, len + 1);
667 #define VCHIQ_INIT_RETRIES 10
668 int vchiq_initialise(struct vchiq_instance **instance_out)
670 struct vchiq_state *state;
671 struct vchiq_instance *instance = NULL;
675 * VideoCore may not be ready due to boot up timing.
676 * It may never be ready if kernel and firmware are mismatched,so don't
679 for (i = 0; i < VCHIQ_INIT_RETRIES; i++) {
680 state = vchiq_get_state();
683 usleep_range(500, 600);
685 if (i == VCHIQ_INIT_RETRIES) {
686 vchiq_log_error(vchiq_core_log_level, "%s: videocore not initialized\n", __func__);
690 vchiq_log_warning(vchiq_core_log_level,
691 "%s: videocore initialized after %d retries\n", __func__, i);
694 instance = kzalloc(sizeof(*instance), GFP_KERNEL);
696 vchiq_log_error(vchiq_core_log_level,
697 "%s: error allocating vchiq instance\n", __func__);
702 instance->connected = 0;
703 instance->state = state;
704 mutex_init(&instance->bulk_waiter_list_mutex);
705 INIT_LIST_HEAD(&instance->bulk_waiter_list);
707 *instance_out = instance;
712 vchiq_log_trace(vchiq_core_log_level, "%s(%p): returning %d", __func__, instance, ret);
716 EXPORT_SYMBOL(vchiq_initialise);
718 void free_bulk_waiter(struct vchiq_instance *instance)
720 struct bulk_waiter_node *waiter, *next;
722 list_for_each_entry_safe(waiter, next,
723 &instance->bulk_waiter_list, list) {
724 list_del(&waiter->list);
725 vchiq_log_info(vchiq_arm_log_level, "bulk_waiter - cleaned up %pK for pid %d",
726 waiter, waiter->pid);
731 int vchiq_shutdown(struct vchiq_instance *instance)
734 struct vchiq_state *state = instance->state;
736 if (mutex_lock_killable(&state->mutex))
739 /* Remove all services */
740 vchiq_shutdown_internal(state, instance);
742 mutex_unlock(&state->mutex);
744 vchiq_log_trace(vchiq_core_log_level, "%s(%p): returning %d", __func__, instance, status);
746 free_bulk_waiter(instance);
751 EXPORT_SYMBOL(vchiq_shutdown);
753 static int vchiq_is_connected(struct vchiq_instance *instance)
755 return instance->connected;
758 int vchiq_connect(struct vchiq_instance *instance)
761 struct vchiq_state *state = instance->state;
763 if (mutex_lock_killable(&state->mutex)) {
764 vchiq_log_trace(vchiq_core_log_level, "%s: call to mutex_lock failed", __func__);
768 status = vchiq_connect_internal(state, instance);
771 instance->connected = 1;
773 mutex_unlock(&state->mutex);
776 vchiq_log_trace(vchiq_core_log_level, "%s(%p): returning %d", __func__, instance, status);
780 EXPORT_SYMBOL(vchiq_connect);
783 vchiq_add_service(struct vchiq_instance *instance,
784 const struct vchiq_service_params_kernel *params,
785 unsigned int *phandle)
788 struct vchiq_state *state = instance->state;
789 struct vchiq_service *service = NULL;
792 *phandle = VCHIQ_SERVICE_HANDLE_INVALID;
794 srvstate = vchiq_is_connected(instance)
795 ? VCHIQ_SRVSTATE_LISTENING
796 : VCHIQ_SRVSTATE_HIDDEN;
798 service = vchiq_add_service_internal(state, params, srvstate, instance, NULL);
801 *phandle = service->handle;
807 vchiq_log_trace(vchiq_core_log_level, "%s(%p): returning %d", __func__, instance, status);
813 vchiq_open_service(struct vchiq_instance *instance,
814 const struct vchiq_service_params_kernel *params,
815 unsigned int *phandle)
817 int status = -EINVAL;
818 struct vchiq_state *state = instance->state;
819 struct vchiq_service *service = NULL;
821 *phandle = VCHIQ_SERVICE_HANDLE_INVALID;
823 if (!vchiq_is_connected(instance))
826 service = vchiq_add_service_internal(state, params, VCHIQ_SRVSTATE_OPENING, instance, NULL);
829 *phandle = service->handle;
830 status = vchiq_open_service_internal(service, current->pid);
832 vchiq_remove_service(instance, service->handle);
833 *phandle = VCHIQ_SERVICE_HANDLE_INVALID;
838 vchiq_log_trace(vchiq_core_log_level, "%s(%p): returning %d", __func__, instance, status);
842 EXPORT_SYMBOL(vchiq_open_service);
845 vchiq_bulk_transmit(struct vchiq_instance *instance, unsigned int handle, const void *data,
846 unsigned int size, void *userdata, enum vchiq_bulk_mode mode)
852 case VCHIQ_BULK_MODE_NOCALLBACK:
853 case VCHIQ_BULK_MODE_CALLBACK:
854 status = vchiq_bulk_transfer(instance, handle,
856 size, userdata, mode,
857 VCHIQ_BULK_TRANSMIT);
859 case VCHIQ_BULK_MODE_BLOCKING:
860 status = vchiq_blocking_bulk_transfer(instance, handle, (void *)data, size,
861 VCHIQ_BULK_TRANSMIT);
868 * vchiq_*_bulk_transfer() may return -EAGAIN, so we need
869 * to implement a retry mechanism since this function is
870 * supposed to block until queued
872 if (status != -EAGAIN)
880 EXPORT_SYMBOL(vchiq_bulk_transmit);
882 int vchiq_bulk_receive(struct vchiq_instance *instance, unsigned int handle,
883 void *data, unsigned int size, void *userdata,
884 enum vchiq_bulk_mode mode)
890 case VCHIQ_BULK_MODE_NOCALLBACK:
891 case VCHIQ_BULK_MODE_CALLBACK:
892 status = vchiq_bulk_transfer(instance, handle, data, NULL,
894 mode, VCHIQ_BULK_RECEIVE);
896 case VCHIQ_BULK_MODE_BLOCKING:
897 status = vchiq_blocking_bulk_transfer(instance, handle, (void *)data, size,
905 * vchiq_*_bulk_transfer() may return -EAGAIN, so we need
906 * to implement a retry mechanism since this function is
907 * supposed to block until queued
909 if (status != -EAGAIN)
917 EXPORT_SYMBOL(vchiq_bulk_receive);
920 vchiq_blocking_bulk_transfer(struct vchiq_instance *instance, unsigned int handle, void *data,
921 unsigned int size, enum vchiq_bulk_dir dir)
923 struct vchiq_service *service;
925 struct bulk_waiter_node *waiter = NULL, *iter;
927 service = find_service_by_handle(instance, handle);
931 vchiq_service_put(service);
933 mutex_lock(&instance->bulk_waiter_list_mutex);
934 list_for_each_entry(iter, &instance->bulk_waiter_list, list) {
935 if (iter->pid == current->pid) {
936 list_del(&iter->list);
941 mutex_unlock(&instance->bulk_waiter_list_mutex);
944 struct vchiq_bulk *bulk = waiter->bulk_waiter.bulk;
947 /* This thread has an outstanding bulk transfer. */
948 /* FIXME: why compare a dma address to a pointer? */
949 if ((bulk->data != (dma_addr_t)(uintptr_t)data) || (bulk->size != size)) {
951 * This is not a retry of the previous one.
952 * Cancel the signal when the transfer completes.
954 spin_lock(&bulk_waiter_spinlock);
955 bulk->userdata = NULL;
956 spin_unlock(&bulk_waiter_spinlock);
960 waiter = kzalloc(sizeof(*waiter), GFP_KERNEL);
962 vchiq_log_error(vchiq_core_log_level, "%s - out of memory", __func__);
967 status = vchiq_bulk_transfer(instance, handle, data, NULL, size,
968 &waiter->bulk_waiter,
969 VCHIQ_BULK_MODE_BLOCKING, dir);
970 if ((status != -EAGAIN) || fatal_signal_pending(current) || !waiter->bulk_waiter.bulk) {
971 struct vchiq_bulk *bulk = waiter->bulk_waiter.bulk;
974 /* Cancel the signal when the transfer completes. */
975 spin_lock(&bulk_waiter_spinlock);
976 bulk->userdata = NULL;
977 spin_unlock(&bulk_waiter_spinlock);
981 waiter->pid = current->pid;
982 mutex_lock(&instance->bulk_waiter_list_mutex);
983 list_add(&waiter->list, &instance->bulk_waiter_list);
984 mutex_unlock(&instance->bulk_waiter_list_mutex);
985 vchiq_log_info(vchiq_arm_log_level, "saved bulk_waiter %pK for pid %d", waiter,
993 add_completion(struct vchiq_instance *instance, enum vchiq_reason reason,
994 struct vchiq_header *header, struct user_service *user_service,
997 struct vchiq_completion_data_kernel *completion;
1000 DEBUG_INITIALISE(g_state.local);
1002 insert = instance->completion_insert;
1003 while ((insert - instance->completion_remove) >= MAX_COMPLETIONS) {
1004 /* Out of space - wait for the client */
1005 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
1006 vchiq_log_trace(vchiq_arm_log_level, "%s - completion queue full", __func__);
1007 DEBUG_COUNT(COMPLETION_QUEUE_FULL_COUNT);
1008 if (wait_for_completion_interruptible(&instance->remove_event)) {
1009 vchiq_log_info(vchiq_arm_log_level, "service_callback interrupted");
1011 } else if (instance->closing) {
1012 vchiq_log_info(vchiq_arm_log_level, "service_callback closing");
1015 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
1018 completion = &instance->completions[insert & (MAX_COMPLETIONS - 1)];
1020 completion->header = header;
1021 completion->reason = reason;
1022 /* N.B. service_userdata is updated while processing AWAIT_COMPLETION */
1023 completion->service_userdata = user_service->service;
1024 completion->bulk_userdata = bulk_userdata;
1026 if (reason == VCHIQ_SERVICE_CLOSED) {
1028 * Take an extra reference, to be held until
1029 * this CLOSED notification is delivered.
1031 vchiq_service_get(user_service->service);
1032 if (instance->use_close_delivered)
1033 user_service->close_pending = 1;
1037 * A write barrier is needed here to ensure that the entire completion
1038 * record is written out before the insert point.
1042 if (reason == VCHIQ_MESSAGE_AVAILABLE)
1043 user_service->message_available_pos = insert;
1046 instance->completion_insert = insert;
1048 complete(&instance->insert_event);
1054 service_callback(struct vchiq_instance *instance, enum vchiq_reason reason,
1055 struct vchiq_header *header, unsigned int handle, void *bulk_userdata)
1058 * How do we ensure the callback goes to the right client?
1059 * The service_user data points to a user_service record
1060 * containing the original callback and the user state structure, which
1061 * contains a circular buffer for completion records.
1063 struct user_service *user_service;
1064 struct vchiq_service *service;
1065 bool skip_completion = false;
1067 DEBUG_INITIALISE(g_state.local);
1069 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
1072 service = handle_to_service(instance, handle);
1073 if (WARN_ON(!service)) {
1078 user_service = (struct user_service *)service->base.userdata;
1080 if (!instance || instance->closing) {
1086 * As hopping around different synchronization mechanism,
1087 * taking an extra reference results in simpler implementation.
1089 vchiq_service_get(service);
1092 vchiq_log_trace(vchiq_arm_log_level,
1093 "%s - service %lx(%d,%p), reason %d, header %lx, instance %lx, bulk_userdata %lx",
1094 __func__, (unsigned long)user_service, service->localport,
1095 user_service->userdata, reason, (unsigned long)header,
1096 (unsigned long)instance, (unsigned long)bulk_userdata);
1098 if (header && user_service->is_vchi) {
1099 spin_lock(&msg_queue_spinlock);
1100 while (user_service->msg_insert ==
1101 (user_service->msg_remove + MSG_QUEUE_SIZE)) {
1102 spin_unlock(&msg_queue_spinlock);
1103 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
1104 DEBUG_COUNT(MSG_QUEUE_FULL_COUNT);
1105 vchiq_log_trace(vchiq_arm_log_level, "%s - msg queue full", __func__);
1107 * If there is no MESSAGE_AVAILABLE in the completion
1110 if ((user_service->message_available_pos -
1111 instance->completion_remove) < 0) {
1114 vchiq_log_info(vchiq_arm_log_level,
1115 "Inserting extra MESSAGE_AVAILABLE");
1116 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
1117 status = add_completion(instance, reason, NULL, user_service,
1120 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
1121 vchiq_service_put(service);
1126 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
1127 if (wait_for_completion_interruptible(&user_service->remove_event)) {
1128 vchiq_log_info(vchiq_arm_log_level, "%s interrupted", __func__);
1129 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
1130 vchiq_service_put(service);
1132 } else if (instance->closing) {
1133 vchiq_log_info(vchiq_arm_log_level, "%s closing", __func__);
1134 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
1135 vchiq_service_put(service);
1138 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
1139 spin_lock(&msg_queue_spinlock);
1142 user_service->msg_queue[user_service->msg_insert &
1143 (MSG_QUEUE_SIZE - 1)] = header;
1144 user_service->msg_insert++;
1147 * If there is a thread waiting in DEQUEUE_MESSAGE, or if
1148 * there is a MESSAGE_AVAILABLE in the completion queue then
1149 * bypass the completion queue.
1151 if (((user_service->message_available_pos -
1152 instance->completion_remove) >= 0) ||
1153 user_service->dequeue_pending) {
1154 user_service->dequeue_pending = 0;
1155 skip_completion = true;
1158 spin_unlock(&msg_queue_spinlock);
1159 complete(&user_service->insert_event);
1163 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
1164 vchiq_service_put(service);
1166 if (skip_completion)
1169 return add_completion(instance, reason, header, user_service,
1173 int vchiq_dump(void *dump_context, const char *str, int len)
1175 struct dump_context *context = (struct dump_context *)dump_context;
1178 if (context->actual >= context->space)
1181 if (context->offset > 0) {
1182 int skip_bytes = min_t(int, len, context->offset);
1186 context->offset -= skip_bytes;
1187 if (context->offset > 0)
1190 copy_bytes = min_t(int, len, context->space - context->actual);
1191 if (copy_bytes == 0)
1193 if (copy_to_user(context->buf + context->actual, str,
1196 context->actual += copy_bytes;
1200 * If the terminating NUL is included in the length, then it
1201 * marks the end of a line and should be replaced with a
1204 if ((len == 0) && (str[copy_bytes - 1] == '\0')) {
1207 if (copy_to_user(context->buf + context->actual - 1,
1214 int vchiq_dump_platform_instances(void *dump_context)
1216 struct vchiq_state *state = vchiq_get_state();
1225 * There is no list of instances, so instead scan all services,
1226 * marking those that have been dumped.
1230 for (i = 0; i < state->unused_service; i++) {
1231 struct vchiq_service *service;
1232 struct vchiq_instance *instance;
1234 service = rcu_dereference(state->services[i]);
1235 if (!service || service->base.callback != service_callback)
1238 instance = service->instance;
1244 for (i = 0; i < state->unused_service; i++) {
1245 struct vchiq_service *service;
1246 struct vchiq_instance *instance;
1250 service = rcu_dereference(state->services[i]);
1251 if (!service || service->base.callback != service_callback) {
1256 instance = service->instance;
1257 if (!instance || instance->mark) {
1263 len = snprintf(buf, sizeof(buf),
1264 "Instance %pK: pid %d,%s completions %d/%d",
1265 instance, instance->pid,
1266 instance->connected ? " connected, " :
1268 instance->completion_insert -
1269 instance->completion_remove,
1271 err = vchiq_dump(dump_context, buf, len + 1);
1279 int vchiq_dump_platform_service_state(void *dump_context,
1280 struct vchiq_service *service)
1282 struct user_service *user_service =
1283 (struct user_service *)service->base.userdata;
1287 len = scnprintf(buf, sizeof(buf), " instance %pK", service->instance);
1289 if ((service->base.callback == service_callback) && user_service->is_vchi) {
1290 len += scnprintf(buf + len, sizeof(buf) - len, ", %d/%d messages",
1291 user_service->msg_insert - user_service->msg_remove,
1294 if (user_service->dequeue_pending)
1295 len += scnprintf(buf + len, sizeof(buf) - len,
1296 " (dequeue pending)");
1299 return vchiq_dump(dump_context, buf, len + 1);
1302 struct vchiq_state *
1303 vchiq_get_state(void)
1305 if (!g_state.remote) {
1306 pr_err("%s: g_state.remote == NULL\n", __func__);
1310 if (g_state.remote->initialised != 1) {
1311 pr_notice("%s: g_state.remote->initialised != 1 (%d)\n",
1312 __func__, g_state.remote->initialised);
1320 * Autosuspend related functionality
1324 vchiq_keepalive_vchiq_callback(struct vchiq_instance *instance,
1325 enum vchiq_reason reason,
1326 struct vchiq_header *header,
1327 unsigned int service_user, void *bulk_user)
1329 vchiq_log_error(vchiq_susp_log_level, "%s callback reason %d", __func__, reason);
1334 vchiq_keepalive_thread_func(void *v)
1336 struct vchiq_state *state = (struct vchiq_state *)v;
1337 struct vchiq_arm_state *arm_state = vchiq_platform_get_arm_state(state);
1340 struct vchiq_instance *instance;
1341 unsigned int ka_handle;
1344 struct vchiq_service_params_kernel params = {
1345 .fourcc = VCHIQ_MAKE_FOURCC('K', 'E', 'E', 'P'),
1346 .callback = vchiq_keepalive_vchiq_callback,
1347 .version = KEEPALIVE_VER,
1348 .version_min = KEEPALIVE_VER_MIN
1351 ret = vchiq_initialise(&instance);
1353 vchiq_log_error(vchiq_susp_log_level, "%s vchiq_initialise failed %d", __func__,
1358 status = vchiq_connect(instance);
1360 vchiq_log_error(vchiq_susp_log_level, "%s vchiq_connect failed %d", __func__,
1365 status = vchiq_add_service(instance, ¶ms, &ka_handle);
1367 vchiq_log_error(vchiq_susp_log_level, "%s vchiq_open_service failed %d", __func__,
1373 long rc = 0, uc = 0;
1375 if (wait_for_completion_interruptible(&arm_state->ka_evt)) {
1376 vchiq_log_error(vchiq_susp_log_level, "%s interrupted", __func__);
1377 flush_signals(current);
1382 * read and clear counters. Do release_count then use_count to
1383 * prevent getting more releases than uses
1385 rc = atomic_xchg(&arm_state->ka_release_count, 0);
1386 uc = atomic_xchg(&arm_state->ka_use_count, 0);
1389 * Call use/release service the requisite number of times.
1390 * Process use before release so use counts don't go negative
1393 atomic_inc(&arm_state->ka_use_ack_count);
1394 status = vchiq_use_service(instance, ka_handle);
1396 vchiq_log_error(vchiq_susp_log_level,
1397 "%s vchiq_use_service error %d", __func__, status);
1401 status = vchiq_release_service(instance, ka_handle);
1403 vchiq_log_error(vchiq_susp_log_level,
1404 "%s vchiq_release_service error %d", __func__,
1411 vchiq_shutdown(instance);
1417 vchiq_use_internal(struct vchiq_state *state, struct vchiq_service *service,
1418 enum USE_TYPE_E use_type)
1420 struct vchiq_arm_state *arm_state = vchiq_platform_get_arm_state(state);
1431 if (use_type == USE_TYPE_VCHIQ) {
1432 sprintf(entity, "VCHIQ: ");
1433 entity_uc = &arm_state->peer_use_count;
1434 } else if (service) {
1435 sprintf(entity, "%c%c%c%c:%03d",
1436 VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
1437 service->client_id);
1438 entity_uc = &service->service_use_count;
1440 vchiq_log_error(vchiq_susp_log_level, "%s null service ptr", __func__);
1445 write_lock_bh(&arm_state->susp_res_lock);
1446 local_uc = ++arm_state->videocore_use_count;
1449 vchiq_log_trace(vchiq_susp_log_level, "%s %s count %d, state count %d", __func__, entity,
1450 *entity_uc, local_uc);
1452 write_unlock_bh(&arm_state->susp_res_lock);
1456 long ack_cnt = atomic_xchg(&arm_state->ka_use_ack_count, 0);
1458 while (ack_cnt && !status) {
1459 /* Send the use notify to videocore */
1460 status = vchiq_send_remote_use_active(state);
1464 atomic_add(ack_cnt, &arm_state->ka_use_ack_count);
1469 vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, ret);
1474 vchiq_release_internal(struct vchiq_state *state, struct vchiq_service *service)
1476 struct vchiq_arm_state *arm_state = vchiq_platform_get_arm_state(state);
1487 sprintf(entity, "%c%c%c%c:%03d",
1488 VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
1489 service->client_id);
1490 entity_uc = &service->service_use_count;
1492 sprintf(entity, "PEER: ");
1493 entity_uc = &arm_state->peer_use_count;
1496 write_lock_bh(&arm_state->susp_res_lock);
1497 if (!arm_state->videocore_use_count || !(*entity_uc)) {
1498 /* Don't use BUG_ON - don't allow user thread to crash kernel */
1499 WARN_ON(!arm_state->videocore_use_count);
1500 WARN_ON(!(*entity_uc));
1504 --arm_state->videocore_use_count;
1507 vchiq_log_trace(vchiq_susp_log_level, "%s %s count %d, state count %d", __func__, entity,
1508 *entity_uc, arm_state->videocore_use_count);
1511 write_unlock_bh(&arm_state->susp_res_lock);
1514 vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, ret);
1519 vchiq_on_remote_use(struct vchiq_state *state)
1521 struct vchiq_arm_state *arm_state = vchiq_platform_get_arm_state(state);
1523 atomic_inc(&arm_state->ka_use_count);
1524 complete(&arm_state->ka_evt);
1528 vchiq_on_remote_release(struct vchiq_state *state)
1530 struct vchiq_arm_state *arm_state = vchiq_platform_get_arm_state(state);
1532 atomic_inc(&arm_state->ka_release_count);
1533 complete(&arm_state->ka_evt);
1537 vchiq_use_service_internal(struct vchiq_service *service)
1539 return vchiq_use_internal(service->state, service, USE_TYPE_SERVICE);
1543 vchiq_release_service_internal(struct vchiq_service *service)
1545 return vchiq_release_internal(service->state, service);
1548 struct vchiq_debugfs_node *
1549 vchiq_instance_get_debugfs_node(struct vchiq_instance *instance)
1551 return &instance->debugfs_node;
1555 vchiq_instance_get_use_count(struct vchiq_instance *instance)
1557 struct vchiq_service *service;
1558 int use_count = 0, i;
1562 while ((service = __next_service_by_instance(instance->state,
1564 use_count += service->service_use_count;
1570 vchiq_instance_get_pid(struct vchiq_instance *instance)
1572 return instance->pid;
1576 vchiq_instance_get_trace(struct vchiq_instance *instance)
1578 return instance->trace;
1582 vchiq_instance_set_trace(struct vchiq_instance *instance, int trace)
1584 struct vchiq_service *service;
1589 while ((service = __next_service_by_instance(instance->state,
1591 service->trace = trace;
1593 instance->trace = (trace != 0);
1597 vchiq_use_service(struct vchiq_instance *instance, unsigned int handle)
1600 struct vchiq_service *service = find_service_by_handle(instance, handle);
1603 ret = vchiq_use_internal(service->state, service, USE_TYPE_SERVICE);
1604 vchiq_service_put(service);
1608 EXPORT_SYMBOL(vchiq_use_service);
1611 vchiq_release_service(struct vchiq_instance *instance, unsigned int handle)
1614 struct vchiq_service *service = find_service_by_handle(instance, handle);
1617 ret = vchiq_release_internal(service->state, service);
1618 vchiq_service_put(service);
1622 EXPORT_SYMBOL(vchiq_release_service);
1624 struct service_data_struct {
1631 vchiq_dump_service_use_state(struct vchiq_state *state)
1633 struct vchiq_arm_state *arm_state = vchiq_platform_get_arm_state(state);
1634 struct service_data_struct *service_data;
1637 * If there's more than 64 services, only dump ones with
1640 int only_nonzero = 0;
1641 static const char *nz = "<-- preventing suspend";
1645 int active_services;
1650 service_data = kmalloc_array(MAX_SERVICES, sizeof(*service_data),
1655 read_lock_bh(&arm_state->susp_res_lock);
1656 peer_count = arm_state->peer_use_count;
1657 vc_use_count = arm_state->videocore_use_count;
1658 active_services = state->unused_service;
1659 if (active_services > MAX_SERVICES)
1663 for (i = 0; i < active_services; i++) {
1664 struct vchiq_service *service_ptr =
1665 rcu_dereference(state->services[i]);
1670 if (only_nonzero && !service_ptr->service_use_count)
1673 if (service_ptr->srvstate == VCHIQ_SRVSTATE_FREE)
1676 service_data[found].fourcc = service_ptr->base.fourcc;
1677 service_data[found].clientid = service_ptr->client_id;
1678 service_data[found].use_count = service_ptr->service_use_count;
1680 if (found >= MAX_SERVICES)
1685 read_unlock_bh(&arm_state->susp_res_lock);
1688 vchiq_log_warning(vchiq_susp_log_level, "Too many active services (%d). Only dumping up to first %d services with non-zero use-count",
1689 active_services, found);
1691 for (i = 0; i < found; i++) {
1692 vchiq_log_warning(vchiq_susp_log_level, "----- %c%c%c%c:%d service count %d %s",
1693 VCHIQ_FOURCC_AS_4CHARS(service_data[i].fourcc),
1694 service_data[i].clientid, service_data[i].use_count,
1695 service_data[i].use_count ? nz : "");
1697 vchiq_log_warning(vchiq_susp_log_level, "----- VCHIQ use count %d", peer_count);
1698 vchiq_log_warning(vchiq_susp_log_level, "--- Overall vchiq instance use count %d",
1701 kfree(service_data);
1705 vchiq_check_service(struct vchiq_service *service)
1707 struct vchiq_arm_state *arm_state;
1710 if (!service || !service->state)
1713 arm_state = vchiq_platform_get_arm_state(service->state);
1715 read_lock_bh(&arm_state->susp_res_lock);
1716 if (service->service_use_count)
1718 read_unlock_bh(&arm_state->susp_res_lock);
1721 vchiq_log_error(vchiq_susp_log_level,
1722 "%s ERROR - %c%c%c%c:%d service count %d, state count %d", __func__,
1723 VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc), service->client_id,
1724 service->service_use_count, arm_state->videocore_use_count);
1725 vchiq_dump_service_use_state(service->state);
1731 void vchiq_platform_conn_state_changed(struct vchiq_state *state,
1732 enum vchiq_connstate oldstate,
1733 enum vchiq_connstate newstate)
1735 struct vchiq_arm_state *arm_state = vchiq_platform_get_arm_state(state);
1736 char threadname[16];
1738 vchiq_log_info(vchiq_susp_log_level, "%d: %s->%s", state->id,
1739 get_conn_state_name(oldstate), get_conn_state_name(newstate));
1740 if (state->conn_state != VCHIQ_CONNSTATE_CONNECTED)
1743 write_lock_bh(&arm_state->susp_res_lock);
1744 if (arm_state->first_connect) {
1745 write_unlock_bh(&arm_state->susp_res_lock);
1749 arm_state->first_connect = 1;
1750 write_unlock_bh(&arm_state->susp_res_lock);
1751 snprintf(threadname, sizeof(threadname), "vchiq-keep/%d",
1753 arm_state->ka_thread = kthread_create(&vchiq_keepalive_thread_func,
1756 if (IS_ERR(arm_state->ka_thread)) {
1757 vchiq_log_error(vchiq_susp_log_level,
1758 "vchiq: FATAL: couldn't create thread %s",
1761 wake_up_process(arm_state->ka_thread);
1765 static const struct of_device_id vchiq_of_match[] = {
1766 { .compatible = "brcm,bcm2835-vchiq", .data = &bcm2835_drvdata },
1767 { .compatible = "brcm,bcm2836-vchiq", .data = &bcm2836_drvdata },
1770 MODULE_DEVICE_TABLE(of, vchiq_of_match);
1772 static struct platform_device *
1773 vchiq_register_child(struct platform_device *pdev, const char *name)
1775 struct platform_device_info pdevinfo;
1776 struct platform_device *child;
1778 memset(&pdevinfo, 0, sizeof(pdevinfo));
1780 pdevinfo.parent = &pdev->dev;
1781 pdevinfo.name = name;
1782 pdevinfo.id = PLATFORM_DEVID_NONE;
1783 pdevinfo.dma_mask = DMA_BIT_MASK(32);
1785 child = platform_device_register_full(&pdevinfo);
1786 if (IS_ERR(child)) {
1787 dev_warn(&pdev->dev, "%s not registered\n", name);
1794 static int vchiq_probe(struct platform_device *pdev)
1796 struct device_node *fw_node;
1797 const struct of_device_id *of_id;
1798 struct vchiq_drvdata *drvdata;
1801 of_id = of_match_node(vchiq_of_match, pdev->dev.of_node);
1802 drvdata = (struct vchiq_drvdata *)of_id->data;
1806 fw_node = of_find_compatible_node(NULL, NULL,
1807 "raspberrypi,bcm2835-firmware");
1809 dev_err(&pdev->dev, "Missing firmware node\n");
1813 drvdata->fw = devm_rpi_firmware_get(&pdev->dev, fw_node);
1814 of_node_put(fw_node);
1816 return -EPROBE_DEFER;
1818 platform_set_drvdata(pdev, drvdata);
1820 err = vchiq_platform_init(pdev, &g_state);
1822 goto failed_platform_init;
1824 vchiq_debugfs_init();
1826 vchiq_log_info(vchiq_arm_log_level,
1827 "vchiq: platform initialised - version %d (min %d)",
1828 VCHIQ_VERSION, VCHIQ_VERSION_MIN);
1831 * Simply exit on error since the function handles cleanup in
1834 err = vchiq_register_chrdev(&pdev->dev);
1836 vchiq_log_warning(vchiq_arm_log_level,
1837 "Failed to initialize vchiq cdev");
1841 bcm2835_camera = vchiq_register_child(pdev, "bcm2835-camera");
1842 bcm2835_audio = vchiq_register_child(pdev, "bcm2835_audio");
1846 failed_platform_init:
1847 vchiq_log_warning(vchiq_arm_log_level, "could not initialize vchiq platform");
1852 static void vchiq_remove(struct platform_device *pdev)
1854 platform_device_unregister(bcm2835_audio);
1855 platform_device_unregister(bcm2835_camera);
1856 vchiq_debugfs_deinit();
1857 vchiq_deregister_chrdev();
1860 static struct platform_driver vchiq_driver = {
1862 .name = "bcm2835_vchiq",
1863 .of_match_table = vchiq_of_match,
1865 .probe = vchiq_probe,
1866 .remove_new = vchiq_remove,
1869 static int __init vchiq_driver_init(void)
1873 ret = platform_driver_register(&vchiq_driver);
1875 pr_err("Failed to register vchiq driver\n");
1879 module_init(vchiq_driver_init);
1881 static void __exit vchiq_driver_exit(void)
1883 platform_driver_unregister(&vchiq_driver);
1885 module_exit(vchiq_driver_exit);
1887 MODULE_LICENSE("Dual BSD/GPL");
1888 MODULE_DESCRIPTION("Videocore VCHIQ driver");
1889 MODULE_AUTHOR("Broadcom Corporation");