2 * Copyright (c) 2014 Raspberry Pi (Trading) Ltd. All rights reserved.
3 * Copyright (c) 2010-2012 Broadcom. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions, and the following disclaimer,
10 * without modification.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. The names of the above-listed copyright holders may not be used
15 * to endorse or promote products derived from this software without
16 * specific prior written permission.
18 * ALTERNATIVELY, this software may be distributed under the terms of the
19 * GNU General Public License ("GPL") version 2, as published by the Free
20 * Software Foundation.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
23 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
25 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
26 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
27 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
28 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
29 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
31 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
32 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <linux/kernel.h>
36 #include <linux/module.h>
37 #include <linux/sched/signal.h>
38 #include <linux/types.h>
39 #include <linux/errno.h>
40 #include <linux/cdev.h>
42 #include <linux/device.h>
44 #include <linux/highmem.h>
45 #include <linux/pagemap.h>
46 #include <linux/bug.h>
47 #include <linux/semaphore.h>
48 #include <linux/list.h>
50 #include <linux/platform_device.h>
51 #include <linux/compat.h>
52 #include <soc/bcm2835/raspberrypi-firmware.h>
54 #include "vchiq_core.h"
55 #include "vchiq_ioctl.h"
56 #include "vchiq_arm.h"
57 #include "vchiq_debugfs.h"
58 #include "vchiq_killable.h"
60 #define DEVICE_NAME "vchiq"
62 /* Override the default prefix, which would be vchiq_arm (from the filename) */
63 #undef MODULE_PARAM_PREFIX
64 #define MODULE_PARAM_PREFIX DEVICE_NAME "."
68 /* Some per-instance constants */
69 #define MAX_COMPLETIONS 128
70 #define MAX_SERVICES 64
71 #define MAX_ELEMENTS 8
72 #define MSG_QUEUE_SIZE 128
74 #define KEEPALIVE_VER 1
75 #define KEEPALIVE_VER_MIN KEEPALIVE_VER
77 /* Run time control of log level, based on KERN_XXX level. */
78 int vchiq_arm_log_level = VCHIQ_LOG_DEFAULT;
79 int vchiq_susp_log_level = VCHIQ_LOG_ERROR;
81 #define SUSPEND_TIMER_TIMEOUT_MS 100
82 #define SUSPEND_RETRY_TIMER_TIMEOUT_MS 1000
84 #define VC_SUSPEND_NUM_OFFSET 3 /* number of values before idle which are -ve */
85 static const char *const suspend_state_names[] = {
86 "VC_SUSPEND_FORCE_CANCELED",
87 "VC_SUSPEND_REJECTED",
90 "VC_SUSPEND_REQUESTED",
91 "VC_SUSPEND_IN_PROGRESS",
92 "VC_SUSPEND_SUSPENDED"
94 #define VC_RESUME_NUM_OFFSET 1 /* number of values before idle which are -ve */
95 static const char *const resume_state_names[] = {
98 "VC_RESUME_REQUESTED",
99 "VC_RESUME_IN_PROGRESS",
102 /* The number of times we allow force suspend to timeout before actually
103 ** _forcing_ suspend. This is to cater for SW which fails to release vchiq
104 ** correctly - we don't want to prevent ARM suspend indefinitely in this case.
106 #define FORCE_SUSPEND_FAIL_MAX 8
108 /* The time in ms allowed for videocore to go idle when force suspend has been
110 #define FORCE_SUSPEND_TIMEOUT_MS 200
113 static void suspend_timer_callback(unsigned long context);
116 typedef struct user_service_struct {
117 VCHIQ_SERVICE_T *service;
119 VCHIQ_INSTANCE_T instance;
121 char dequeue_pending;
123 int message_available_pos;
126 struct semaphore insert_event;
127 struct semaphore remove_event;
128 struct semaphore close_event;
129 VCHIQ_HEADER_T * msg_queue[MSG_QUEUE_SIZE];
132 struct bulk_waiter_node {
133 struct bulk_waiter bulk_waiter;
135 struct list_head list;
138 struct vchiq_instance_struct {
139 VCHIQ_STATE_T *state;
140 VCHIQ_COMPLETION_DATA_T completions[MAX_COMPLETIONS];
141 int completion_insert;
142 int completion_remove;
143 struct semaphore insert_event;
144 struct semaphore remove_event;
145 struct mutex completion_mutex;
151 int use_close_delivered;
154 struct list_head bulk_waiter_list;
155 struct mutex bulk_waiter_list_mutex;
157 VCHIQ_DEBUGFS_NODE_T debugfs_node;
160 typedef struct dump_context_struct {
167 static struct cdev vchiq_cdev;
168 static dev_t vchiq_devid;
169 static VCHIQ_STATE_T g_state;
170 static struct class *vchiq_class;
171 static struct device *vchiq_dev;
172 static DEFINE_SPINLOCK(msg_queue_spinlock);
174 static const char *const ioctl_names[] = {
180 "QUEUE_BULK_TRANSMIT",
181 "QUEUE_BULK_RECEIVE",
189 "SET_SERVICE_OPTION",
195 vchiq_static_assert(ARRAY_SIZE(ioctl_names) ==
196 (VCHIQ_IOC_MAX + 1));
198 #if defined(CONFIG_BCM2835_VCHIQ_SUPPORT_MEMDUMP)
200 dump_phys_mem(void *virt_addr, u32 num_bytes);
203 /****************************************************************************
207 ***************************************************************************/
209 static VCHIQ_STATUS_T
210 add_completion(VCHIQ_INSTANCE_T instance, VCHIQ_REASON_T reason,
211 VCHIQ_HEADER_T *header, USER_SERVICE_T *user_service,
214 VCHIQ_COMPLETION_DATA_T *completion;
217 DEBUG_INITIALISE(g_state.local)
219 insert = instance->completion_insert;
220 while ((insert - instance->completion_remove) >= MAX_COMPLETIONS) {
221 /* Out of space - wait for the client */
222 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
223 vchiq_log_trace(vchiq_arm_log_level,
224 "add_completion - completion queue full");
225 DEBUG_COUNT(COMPLETION_QUEUE_FULL_COUNT);
226 if (down_interruptible(&instance->remove_event) != 0) {
227 vchiq_log_info(vchiq_arm_log_level,
228 "service_callback interrupted");
230 } else if (instance->closing) {
231 vchiq_log_info(vchiq_arm_log_level,
232 "service_callback closing");
233 return VCHIQ_SUCCESS;
235 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
238 completion = &instance->completions[insert & (MAX_COMPLETIONS - 1)];
240 completion->header = header;
241 completion->reason = reason;
242 /* N.B. service_userdata is updated while processing AWAIT_COMPLETION */
243 completion->service_userdata = user_service->service;
244 completion->bulk_userdata = bulk_userdata;
246 if (reason == VCHIQ_SERVICE_CLOSED) {
247 /* Take an extra reference, to be held until
248 this CLOSED notification is delivered. */
249 lock_service(user_service->service);
250 if (instance->use_close_delivered)
251 user_service->close_pending = 1;
254 /* A write barrier is needed here to ensure that the entire completion
255 record is written out before the insert point. */
258 if (reason == VCHIQ_MESSAGE_AVAILABLE)
259 user_service->message_available_pos = insert;
262 instance->completion_insert = insert;
264 up(&instance->insert_event);
266 return VCHIQ_SUCCESS;
269 /****************************************************************************
273 ***************************************************************************/
275 static VCHIQ_STATUS_T
276 service_callback(VCHIQ_REASON_T reason, VCHIQ_HEADER_T *header,
277 VCHIQ_SERVICE_HANDLE_T handle, void *bulk_userdata)
279 /* How do we ensure the callback goes to the right client?
280 ** The service_user data points to a USER_SERVICE_T record containing
281 ** the original callback and the user state structure, which contains a
282 ** circular buffer for completion records.
284 USER_SERVICE_T *user_service;
285 VCHIQ_SERVICE_T *service;
286 VCHIQ_INSTANCE_T instance;
287 bool skip_completion = false;
289 DEBUG_INITIALISE(g_state.local)
291 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
293 service = handle_to_service(handle);
295 user_service = (USER_SERVICE_T *)service->base.userdata;
296 instance = user_service->instance;
298 if (!instance || instance->closing)
299 return VCHIQ_SUCCESS;
301 vchiq_log_trace(vchiq_arm_log_level,
302 "service_callback - service %lx(%d,%p), reason %d, header %lx, "
303 "instance %lx, bulk_userdata %lx",
304 (unsigned long)user_service,
305 service->localport, user_service->userdata,
306 reason, (unsigned long)header,
307 (unsigned long)instance, (unsigned long)bulk_userdata);
309 if (header && user_service->is_vchi) {
310 spin_lock(&msg_queue_spinlock);
311 while (user_service->msg_insert ==
312 (user_service->msg_remove + MSG_QUEUE_SIZE)) {
313 spin_unlock(&msg_queue_spinlock);
314 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
315 DEBUG_COUNT(MSG_QUEUE_FULL_COUNT);
316 vchiq_log_trace(vchiq_arm_log_level,
317 "service_callback - msg queue full");
318 /* If there is no MESSAGE_AVAILABLE in the completion
321 if ((user_service->message_available_pos -
322 instance->completion_remove) < 0) {
323 VCHIQ_STATUS_T status;
325 vchiq_log_info(vchiq_arm_log_level,
326 "Inserting extra MESSAGE_AVAILABLE");
327 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
328 status = add_completion(instance, reason,
329 NULL, user_service, bulk_userdata);
330 if (status != VCHIQ_SUCCESS) {
331 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
336 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
337 if (down_interruptible(&user_service->remove_event)
339 vchiq_log_info(vchiq_arm_log_level,
340 "service_callback interrupted");
341 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
343 } else if (instance->closing) {
344 vchiq_log_info(vchiq_arm_log_level,
345 "service_callback closing");
346 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
349 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
350 spin_lock(&msg_queue_spinlock);
353 user_service->msg_queue[user_service->msg_insert &
354 (MSG_QUEUE_SIZE - 1)] = header;
355 user_service->msg_insert++;
357 /* If there is a thread waiting in DEQUEUE_MESSAGE, or if
358 ** there is a MESSAGE_AVAILABLE in the completion queue then
359 ** bypass the completion queue.
361 if (((user_service->message_available_pos -
362 instance->completion_remove) >= 0) ||
363 user_service->dequeue_pending) {
364 user_service->dequeue_pending = 0;
365 skip_completion = true;
368 spin_unlock(&msg_queue_spinlock);
369 up(&user_service->insert_event);
373 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
376 return VCHIQ_SUCCESS;
378 return add_completion(instance, reason, header, user_service,
382 /****************************************************************************
386 ***************************************************************************/
388 user_service_free(void *userdata)
393 /****************************************************************************
397 ***************************************************************************/
398 static void close_delivered(USER_SERVICE_T *user_service)
400 vchiq_log_info(vchiq_arm_log_level,
401 "close_delivered(handle=%x)",
402 user_service->service->handle);
404 if (user_service->close_pending) {
405 /* Allow the underlying service to be culled */
406 unlock_service(user_service->service);
408 /* Wake the user-thread blocked in close_ or remove_service */
409 up(&user_service->close_event);
411 user_service->close_pending = 0;
415 struct vchiq_io_copy_callback_context {
416 struct vchiq_element *current_element;
417 size_t current_element_offset;
418 unsigned long elements_to_go;
419 size_t current_offset;
423 vchiq_ioc_copy_element_data(
430 size_t bytes_this_round;
431 struct vchiq_io_copy_callback_context *copy_context =
432 (struct vchiq_io_copy_callback_context *)context;
434 if (offset != copy_context->current_offset)
437 if (!copy_context->elements_to_go)
441 * Complex logic here to handle the case of 0 size elements
442 * in the middle of the array of elements.
444 * Need to skip over these 0 size elements.
447 bytes_this_round = min(copy_context->current_element->size -
448 copy_context->current_element_offset,
451 if (bytes_this_round)
454 copy_context->elements_to_go--;
455 copy_context->current_element++;
456 copy_context->current_element_offset = 0;
458 if (!copy_context->elements_to_go)
462 res = copy_from_user(dest,
463 copy_context->current_element->data +
464 copy_context->current_element_offset,
470 copy_context->current_element_offset += bytes_this_round;
471 copy_context->current_offset += bytes_this_round;
474 * Check if done with current element, and if so advance to the next.
476 if (copy_context->current_element_offset ==
477 copy_context->current_element->size) {
478 copy_context->elements_to_go--;
479 copy_context->current_element++;
480 copy_context->current_element_offset = 0;
483 return bytes_this_round;
486 /**************************************************************************
488 * vchiq_ioc_queue_message
490 **************************************************************************/
491 static VCHIQ_STATUS_T
492 vchiq_ioc_queue_message(VCHIQ_SERVICE_HANDLE_T handle,
493 struct vchiq_element *elements,
496 struct vchiq_io_copy_callback_context context;
498 size_t total_size = 0;
500 context.current_element = elements;
501 context.current_element_offset = 0;
502 context.elements_to_go = count;
503 context.current_offset = 0;
505 for (i = 0; i < count; i++) {
506 if (!elements[i].data && elements[i].size != 0)
509 total_size += elements[i].size;
512 return vchiq_queue_message(handle, vchiq_ioc_copy_element_data,
513 &context, total_size);
516 /****************************************************************************
520 ***************************************************************************/
522 vchiq_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
524 VCHIQ_INSTANCE_T instance = file->private_data;
525 VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
526 VCHIQ_SERVICE_T *service = NULL;
530 DEBUG_INITIALISE(g_state.local)
532 vchiq_log_trace(vchiq_arm_log_level,
533 "vchiq_ioctl - instance %pK, cmd %s, arg %lx",
535 ((_IOC_TYPE(cmd) == VCHIQ_IOC_MAGIC) &&
536 (_IOC_NR(cmd) <= VCHIQ_IOC_MAX)) ?
537 ioctl_names[_IOC_NR(cmd)] : "<invalid>", arg);
540 case VCHIQ_IOC_SHUTDOWN:
541 if (!instance->connected)
544 /* Remove all services */
546 while ((service = next_service_by_instance(instance->state,
547 instance, &i)) != NULL) {
548 status = vchiq_remove_service(service->handle);
549 unlock_service(service);
550 if (status != VCHIQ_SUCCESS)
555 if (status == VCHIQ_SUCCESS) {
556 /* Wake the completion thread and ask it to exit */
557 instance->closing = 1;
558 up(&instance->insert_event);
563 case VCHIQ_IOC_CONNECT:
564 if (instance->connected) {
568 rc = mutex_lock_killable(&instance->state->mutex);
570 vchiq_log_error(vchiq_arm_log_level,
571 "vchiq: connect: could not lock mutex for "
573 instance->state->id, rc);
577 status = vchiq_connect_internal(instance->state, instance);
578 mutex_unlock(&instance->state->mutex);
580 if (status == VCHIQ_SUCCESS)
581 instance->connected = 1;
583 vchiq_log_error(vchiq_arm_log_level,
584 "vchiq: could not connect: %d", status);
587 case VCHIQ_IOC_CREATE_SERVICE: {
588 VCHIQ_CREATE_SERVICE_T args;
589 USER_SERVICE_T *user_service = NULL;
594 (&args, (const void __user *)arg,
595 sizeof(args)) != 0) {
600 user_service = kmalloc(sizeof(USER_SERVICE_T), GFP_KERNEL);
607 if (!instance->connected) {
612 srvstate = VCHIQ_SRVSTATE_OPENING;
615 instance->connected ?
616 VCHIQ_SRVSTATE_LISTENING :
617 VCHIQ_SRVSTATE_HIDDEN;
620 userdata = args.params.userdata;
621 args.params.callback = service_callback;
622 args.params.userdata = user_service;
623 service = vchiq_add_service_internal(
625 &args.params, srvstate,
626 instance, user_service_free);
628 if (service != NULL) {
629 user_service->service = service;
630 user_service->userdata = userdata;
631 user_service->instance = instance;
632 user_service->is_vchi = (args.is_vchi != 0);
633 user_service->dequeue_pending = 0;
634 user_service->close_pending = 0;
635 user_service->message_available_pos =
636 instance->completion_remove - 1;
637 user_service->msg_insert = 0;
638 user_service->msg_remove = 0;
639 sema_init(&user_service->insert_event, 0);
640 sema_init(&user_service->remove_event, 0);
641 sema_init(&user_service->close_event, 0);
644 status = vchiq_open_service_internal
645 (service, instance->pid);
646 if (status != VCHIQ_SUCCESS) {
647 vchiq_remove_service(service->handle);
649 ret = (status == VCHIQ_RETRY) ?
655 if (copy_to_user((void __user *)
656 &(((VCHIQ_CREATE_SERVICE_T __user *)
658 (const void *)&service->handle,
659 sizeof(service->handle)) != 0) {
661 vchiq_remove_service(service->handle);
671 case VCHIQ_IOC_CLOSE_SERVICE: {
672 VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
674 service = find_service_for_instance(instance, handle);
675 if (service != NULL) {
676 USER_SERVICE_T *user_service =
677 (USER_SERVICE_T *)service->base.userdata;
678 /* close_pending is false on first entry, and when the
679 wait in vchiq_close_service has been interrupted. */
680 if (!user_service->close_pending) {
681 status = vchiq_close_service(service->handle);
682 if (status != VCHIQ_SUCCESS)
686 /* close_pending is true once the underlying service
687 has been closed until the client library calls the
688 CLOSE_DELIVERED ioctl, signalling close_event. */
689 if (user_service->close_pending &&
690 down_interruptible(&user_service->close_event))
691 status = VCHIQ_RETRY;
697 case VCHIQ_IOC_REMOVE_SERVICE: {
698 VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
700 service = find_service_for_instance(instance, handle);
701 if (service != NULL) {
702 USER_SERVICE_T *user_service =
703 (USER_SERVICE_T *)service->base.userdata;
704 /* close_pending is false on first entry, and when the
705 wait in vchiq_close_service has been interrupted. */
706 if (!user_service->close_pending) {
707 status = vchiq_remove_service(service->handle);
708 if (status != VCHIQ_SUCCESS)
712 /* close_pending is true once the underlying service
713 has been closed until the client library calls the
714 CLOSE_DELIVERED ioctl, signalling close_event. */
715 if (user_service->close_pending &&
716 down_interruptible(&user_service->close_event))
717 status = VCHIQ_RETRY;
723 case VCHIQ_IOC_USE_SERVICE:
724 case VCHIQ_IOC_RELEASE_SERVICE: {
725 VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
727 service = find_service_for_instance(instance, handle);
728 if (service != NULL) {
729 status = (cmd == VCHIQ_IOC_USE_SERVICE) ?
730 vchiq_use_service_internal(service) :
731 vchiq_release_service_internal(service);
732 if (status != VCHIQ_SUCCESS) {
733 vchiq_log_error(vchiq_susp_log_level,
734 "%s: cmd %s returned error %d for "
735 "service %c%c%c%c:%03d",
737 (cmd == VCHIQ_IOC_USE_SERVICE) ?
738 "VCHIQ_IOC_USE_SERVICE" :
739 "VCHIQ_IOC_RELEASE_SERVICE",
741 VCHIQ_FOURCC_AS_4CHARS(
742 service->base.fourcc),
750 case VCHIQ_IOC_QUEUE_MESSAGE: {
751 VCHIQ_QUEUE_MESSAGE_T args;
754 (&args, (const void __user *)arg,
755 sizeof(args)) != 0) {
760 service = find_service_for_instance(instance, args.handle);
762 if ((service != NULL) && (args.count <= MAX_ELEMENTS)) {
763 /* Copy elements into kernel space */
764 struct vchiq_element elements[MAX_ELEMENTS];
766 if (copy_from_user(elements, args.elements,
767 args.count * sizeof(struct vchiq_element)) == 0)
768 status = vchiq_ioc_queue_message
770 elements, args.count);
778 case VCHIQ_IOC_QUEUE_BULK_TRANSMIT:
779 case VCHIQ_IOC_QUEUE_BULK_RECEIVE: {
780 VCHIQ_QUEUE_BULK_TRANSFER_T args;
781 struct bulk_waiter_node *waiter = NULL;
783 VCHIQ_BULK_DIR_T dir =
784 (cmd == VCHIQ_IOC_QUEUE_BULK_TRANSMIT) ?
785 VCHIQ_BULK_TRANSMIT : VCHIQ_BULK_RECEIVE;
788 (&args, (const void __user *)arg,
789 sizeof(args)) != 0) {
794 service = find_service_for_instance(instance, args.handle);
800 if (args.mode == VCHIQ_BULK_MODE_BLOCKING) {
801 waiter = kzalloc(sizeof(struct bulk_waiter_node),
807 args.userdata = &waiter->bulk_waiter;
808 } else if (args.mode == VCHIQ_BULK_MODE_WAITING) {
809 struct list_head *pos;
811 mutex_lock(&instance->bulk_waiter_list_mutex);
812 list_for_each(pos, &instance->bulk_waiter_list) {
813 if (list_entry(pos, struct bulk_waiter_node,
814 list)->pid == current->pid) {
815 waiter = list_entry(pos,
816 struct bulk_waiter_node,
823 mutex_unlock(&instance->bulk_waiter_list_mutex);
825 vchiq_log_error(vchiq_arm_log_level,
826 "no bulk_waiter found for pid %d",
831 vchiq_log_info(vchiq_arm_log_level,
832 "found bulk_waiter %pK for pid %d", waiter,
834 args.userdata = &waiter->bulk_waiter;
836 status = vchiq_bulk_transfer
838 VCHI_MEM_HANDLE_INVALID,
839 args.data, args.size,
840 args.userdata, args.mode,
844 if ((status != VCHIQ_RETRY) || fatal_signal_pending(current) ||
845 !waiter->bulk_waiter.bulk) {
846 if (waiter->bulk_waiter.bulk) {
847 /* Cancel the signal when the transfer
849 spin_lock(&bulk_waiter_spinlock);
850 waiter->bulk_waiter.bulk->userdata = NULL;
851 spin_unlock(&bulk_waiter_spinlock);
855 const VCHIQ_BULK_MODE_T mode_waiting =
856 VCHIQ_BULK_MODE_WAITING;
857 waiter->pid = current->pid;
858 mutex_lock(&instance->bulk_waiter_list_mutex);
859 list_add(&waiter->list, &instance->bulk_waiter_list);
860 mutex_unlock(&instance->bulk_waiter_list_mutex);
861 vchiq_log_info(vchiq_arm_log_level,
862 "saved bulk_waiter %pK for pid %d",
863 waiter, current->pid);
865 if (copy_to_user((void __user *)
866 &(((VCHIQ_QUEUE_BULK_TRANSFER_T __user *)
868 (const void *)&mode_waiting,
869 sizeof(mode_waiting)) != 0)
874 case VCHIQ_IOC_AWAIT_COMPLETION: {
875 VCHIQ_AWAIT_COMPLETION_T args;
877 DEBUG_TRACE(AWAIT_COMPLETION_LINE);
878 if (!instance->connected) {
883 if (copy_from_user(&args, (const void __user *)arg,
884 sizeof(args)) != 0) {
889 mutex_lock(&instance->completion_mutex);
891 DEBUG_TRACE(AWAIT_COMPLETION_LINE);
892 while ((instance->completion_remove ==
893 instance->completion_insert)
894 && !instance->closing) {
897 DEBUG_TRACE(AWAIT_COMPLETION_LINE);
898 mutex_unlock(&instance->completion_mutex);
899 rc = down_interruptible(&instance->insert_event);
900 mutex_lock(&instance->completion_mutex);
902 DEBUG_TRACE(AWAIT_COMPLETION_LINE);
903 vchiq_log_info(vchiq_arm_log_level,
904 "AWAIT_COMPLETION interrupted");
909 DEBUG_TRACE(AWAIT_COMPLETION_LINE);
912 int msgbufcount = args.msgbufcount;
913 int remove = instance->completion_remove;
915 for (ret = 0; ret < args.count; ret++) {
916 VCHIQ_COMPLETION_DATA_T *completion;
917 VCHIQ_SERVICE_T *service;
918 USER_SERVICE_T *user_service;
919 VCHIQ_HEADER_T *header;
921 if (remove == instance->completion_insert)
924 completion = &instance->completions[
925 remove & (MAX_COMPLETIONS - 1)];
928 * A read memory barrier is needed to stop
929 * prefetch of a stale completion record
933 service = completion->service_userdata;
934 user_service = service->base.userdata;
935 completion->service_userdata =
936 user_service->userdata;
938 header = completion->header;
943 msglen = header->size +
944 sizeof(VCHIQ_HEADER_T);
945 /* This must be a VCHIQ-style service */
946 if (args.msgbufsize < msglen) {
949 "header %pK: msgbufsize %x < msglen %x",
950 header, args.msgbufsize,
952 WARN(1, "invalid message "
958 if (msgbufcount <= 0)
959 /* Stall here for lack of a
960 ** buffer for the message. */
962 /* Get the pointer from user space */
964 if (copy_from_user(&msgbuf,
965 (const void __user *)
966 &args.msgbufs[msgbufcount],
967 sizeof(msgbuf)) != 0) {
973 /* Copy the message to user space */
974 if (copy_to_user(msgbuf, header,
981 /* Now it has been copied, the message
982 ** can be released. */
983 vchiq_release_message(service->handle,
986 /* The completion must point to the
988 completion->header = msgbuf;
991 if ((completion->reason ==
992 VCHIQ_SERVICE_CLOSED) &&
993 !instance->use_close_delivered)
994 unlock_service(service);
996 if (copy_to_user((void __user *)(
998 ret * sizeof(VCHIQ_COMPLETION_DATA_T)),
1000 sizeof(VCHIQ_COMPLETION_DATA_T)) != 0) {
1007 * Ensure that the above copy has completed
1008 * before advancing the remove pointer.
1012 instance->completion_remove = remove;
1015 if (msgbufcount != args.msgbufcount) {
1016 if (copy_to_user((void __user *)
1017 &((VCHIQ_AWAIT_COMPLETION_T *)arg)->
1020 sizeof(msgbufcount)) != 0) {
1027 up(&instance->remove_event);
1028 mutex_unlock(&instance->completion_mutex);
1029 DEBUG_TRACE(AWAIT_COMPLETION_LINE);
1032 case VCHIQ_IOC_DEQUEUE_MESSAGE: {
1033 VCHIQ_DEQUEUE_MESSAGE_T args;
1034 USER_SERVICE_T *user_service;
1035 VCHIQ_HEADER_T *header;
1037 DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
1039 (&args, (const void __user *)arg,
1040 sizeof(args)) != 0) {
1044 service = find_service_for_instance(instance, args.handle);
1049 user_service = (USER_SERVICE_T *)service->base.userdata;
1050 if (user_service->is_vchi == 0) {
1055 spin_lock(&msg_queue_spinlock);
1056 if (user_service->msg_remove == user_service->msg_insert) {
1057 if (!args.blocking) {
1058 spin_unlock(&msg_queue_spinlock);
1059 DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
1063 user_service->dequeue_pending = 1;
1065 spin_unlock(&msg_queue_spinlock);
1066 DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
1067 if (down_interruptible(
1068 &user_service->insert_event) != 0) {
1069 vchiq_log_info(vchiq_arm_log_level,
1070 "DEQUEUE_MESSAGE interrupted");
1074 spin_lock(&msg_queue_spinlock);
1075 } while (user_service->msg_remove ==
1076 user_service->msg_insert);
1082 BUG_ON((int)(user_service->msg_insert -
1083 user_service->msg_remove) < 0);
1085 header = user_service->msg_queue[user_service->msg_remove &
1086 (MSG_QUEUE_SIZE - 1)];
1087 user_service->msg_remove++;
1088 spin_unlock(&msg_queue_spinlock);
1090 up(&user_service->remove_event);
1093 else if (header->size <= args.bufsize) {
1094 /* Copy to user space if msgbuf is not NULL */
1095 if ((args.buf == NULL) ||
1096 (copy_to_user((void __user *)args.buf,
1098 header->size) == 0)) {
1100 vchiq_release_message(
1106 vchiq_log_error(vchiq_arm_log_level,
1107 "header %pK: bufsize %x < size %x",
1108 header, args.bufsize, header->size);
1109 WARN(1, "invalid size\n");
1112 DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
1115 case VCHIQ_IOC_GET_CLIENT_ID: {
1116 VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
1118 ret = vchiq_get_client_id(handle);
1121 case VCHIQ_IOC_GET_CONFIG: {
1122 VCHIQ_GET_CONFIG_T args;
1123 VCHIQ_CONFIG_T config;
1125 if (copy_from_user(&args, (const void __user *)arg,
1126 sizeof(args)) != 0) {
1130 if (args.config_size > sizeof(config)) {
1134 status = vchiq_get_config(instance, args.config_size, &config);
1135 if (status == VCHIQ_SUCCESS) {
1136 if (copy_to_user((void __user *)args.pconfig,
1137 &config, args.config_size) != 0) {
1144 case VCHIQ_IOC_SET_SERVICE_OPTION: {
1145 VCHIQ_SET_SERVICE_OPTION_T args;
1148 &args, (const void __user *)arg,
1149 sizeof(args)) != 0) {
1154 service = find_service_for_instance(instance, args.handle);
1160 status = vchiq_set_service_option(
1161 args.handle, args.option, args.value);
1164 #if defined(CONFIG_BCM2835_VCHIQ_SUPPORT_MEMDUMP)
1165 case VCHIQ_IOC_DUMP_PHYS_MEM: {
1166 VCHIQ_DUMP_MEM_T args;
1169 (&args, (const void __user *)arg,
1170 sizeof(args)) != 0) {
1174 dump_phys_mem(args.virt_addr, args.num_bytes);
1178 case VCHIQ_IOC_LIB_VERSION: {
1179 unsigned int lib_version = (unsigned int)arg;
1181 if (lib_version < VCHIQ_VERSION_MIN)
1183 else if (lib_version >= VCHIQ_VERSION_CLOSE_DELIVERED)
1184 instance->use_close_delivered = 1;
1187 case VCHIQ_IOC_CLOSE_DELIVERED: {
1188 VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
1190 service = find_closed_service_for_instance(instance, handle);
1191 if (service != NULL) {
1192 USER_SERVICE_T *user_service =
1193 (USER_SERVICE_T *)service->base.userdata;
1194 close_delivered(user_service);
1206 unlock_service(service);
1209 if (status == VCHIQ_ERROR)
1211 else if (status == VCHIQ_RETRY)
1215 if ((status == VCHIQ_SUCCESS) && (ret < 0) && (ret != -EINTR) &&
1216 (ret != -EWOULDBLOCK))
1217 vchiq_log_info(vchiq_arm_log_level,
1218 " ioctl instance %lx, cmd %s -> status %d, %ld",
1219 (unsigned long)instance,
1220 (_IOC_NR(cmd) <= VCHIQ_IOC_MAX) ?
1221 ioctl_names[_IOC_NR(cmd)] :
1225 vchiq_log_trace(vchiq_arm_log_level,
1226 " ioctl instance %lx, cmd %s -> status %d, %ld",
1227 (unsigned long)instance,
1228 (_IOC_NR(cmd) <= VCHIQ_IOC_MAX) ?
1229 ioctl_names[_IOC_NR(cmd)] :
1236 #if defined(CONFIG_COMPAT)
1238 struct vchiq_service_params32 {
1240 compat_uptr_t callback;
1241 compat_uptr_t userdata;
1242 short version; /* Increment for non-trivial changes */
1243 short version_min; /* Update for incompatible changes */
1246 struct vchiq_create_service32 {
1247 struct vchiq_service_params32 params;
1250 unsigned int handle; /* OUT */
1253 #define VCHIQ_IOC_CREATE_SERVICE32 \
1254 _IOWR(VCHIQ_IOC_MAGIC, 2, struct vchiq_create_service32)
1257 vchiq_compat_ioctl_create_service(
1262 VCHIQ_CREATE_SERVICE_T __user *args;
1263 struct vchiq_create_service32 __user *ptrargs32 =
1264 (struct vchiq_create_service32 __user *)arg;
1265 struct vchiq_create_service32 args32;
1268 args = compat_alloc_user_space(sizeof(*args));
1272 if (copy_from_user(&args32,
1273 (struct vchiq_create_service32 __user *)arg,
1277 if (put_user(args32.params.fourcc, &args->params.fourcc) ||
1278 put_user(compat_ptr(args32.params.callback),
1279 &args->params.callback) ||
1280 put_user(compat_ptr(args32.params.userdata),
1281 &args->params.userdata) ||
1282 put_user(args32.params.version, &args->params.version) ||
1283 put_user(args32.params.version_min,
1284 &args->params.version_min) ||
1285 put_user(args32.is_open, &args->is_open) ||
1286 put_user(args32.is_vchi, &args->is_vchi) ||
1287 put_user(args32.handle, &args->handle))
1290 ret = vchiq_ioctl(file, VCHIQ_IOC_CREATE_SERVICE, (unsigned long)args);
1295 if (get_user(args32.handle, &args->handle))
1298 if (copy_to_user(&ptrargs32->handle,
1300 sizeof(args32.handle)))
1306 struct vchiq_element32 {
1311 struct vchiq_queue_message32 {
1312 unsigned int handle;
1314 compat_uptr_t elements;
1317 #define VCHIQ_IOC_QUEUE_MESSAGE32 \
1318 _IOW(VCHIQ_IOC_MAGIC, 4, struct vchiq_queue_message32)
1321 vchiq_compat_ioctl_queue_message(struct file *file,
1325 VCHIQ_QUEUE_MESSAGE_T *args;
1326 struct vchiq_element *elements;
1327 struct vchiq_queue_message32 args32;
1330 if (copy_from_user(&args32,
1331 (struct vchiq_queue_message32 __user *)arg,
1335 args = compat_alloc_user_space(sizeof(*args) +
1336 (sizeof(*elements) * MAX_ELEMENTS));
1341 if (put_user(args32.handle, &args->handle) ||
1342 put_user(args32.count, &args->count) ||
1343 put_user(compat_ptr(args32.elements), &args->elements))
1346 if (args32.count > MAX_ELEMENTS)
1349 if (args32.elements && args32.count) {
1350 struct vchiq_element32 tempelement32[MAX_ELEMENTS];
1352 elements = (struct vchiq_element __user *)(args + 1);
1354 if (copy_from_user(&tempelement32,
1355 compat_ptr(args32.elements),
1356 sizeof(tempelement32)))
1359 for (count = 0; count < args32.count; count++) {
1360 if (put_user(compat_ptr(tempelement32[count].data),
1361 &elements[count].data) ||
1362 put_user(tempelement32[count].size,
1363 &elements[count].size))
1367 if (put_user(elements, &args->elements))
1371 return vchiq_ioctl(file, VCHIQ_IOC_QUEUE_MESSAGE, (unsigned long)args);
1374 struct vchiq_queue_bulk_transfer32 {
1375 unsigned int handle;
1378 compat_uptr_t userdata;
1379 VCHIQ_BULK_MODE_T mode;
1382 #define VCHIQ_IOC_QUEUE_BULK_TRANSMIT32 \
1383 _IOWR(VCHIQ_IOC_MAGIC, 5, struct vchiq_queue_bulk_transfer32)
1384 #define VCHIQ_IOC_QUEUE_BULK_RECEIVE32 \
1385 _IOWR(VCHIQ_IOC_MAGIC, 6, struct vchiq_queue_bulk_transfer32)
1388 vchiq_compat_ioctl_queue_bulk(struct file *file,
1392 VCHIQ_QUEUE_BULK_TRANSFER_T *args;
1393 struct vchiq_queue_bulk_transfer32 args32;
1394 struct vchiq_queue_bulk_transfer32 *ptrargs32 =
1395 (struct vchiq_queue_bulk_transfer32 *)arg;
1398 args = compat_alloc_user_space(sizeof(*args));
1402 if (copy_from_user(&args32,
1403 (struct vchiq_queue_bulk_transfer32 __user *)arg,
1407 if (put_user(args32.handle, &args->handle) ||
1408 put_user(compat_ptr(args32.data), &args->data) ||
1409 put_user(args32.size, &args->size) ||
1410 put_user(compat_ptr(args32.userdata), &args->userdata) ||
1411 put_user(args32.mode, &args->mode))
1414 if (cmd == VCHIQ_IOC_QUEUE_BULK_TRANSMIT32)
1415 cmd = VCHIQ_IOC_QUEUE_BULK_TRANSMIT;
1417 cmd = VCHIQ_IOC_QUEUE_BULK_RECEIVE;
1419 ret = vchiq_ioctl(file, cmd, (unsigned long)args);
1424 if (get_user(args32.mode, &args->mode))
1427 if (copy_to_user(&ptrargs32->mode,
1429 sizeof(args32.mode)))
1435 struct vchiq_completion_data32 {
1436 VCHIQ_REASON_T reason;
1437 compat_uptr_t header;
1438 compat_uptr_t service_userdata;
1439 compat_uptr_t bulk_userdata;
1442 struct vchiq_await_completion32 {
1445 unsigned int msgbufsize;
1446 unsigned int msgbufcount; /* IN/OUT */
1447 compat_uptr_t msgbufs;
1450 #define VCHIQ_IOC_AWAIT_COMPLETION32 \
1451 _IOWR(VCHIQ_IOC_MAGIC, 7, struct vchiq_await_completion32)
1454 vchiq_compat_ioctl_await_completion(struct file *file,
1458 VCHIQ_AWAIT_COMPLETION_T *args;
1459 VCHIQ_COMPLETION_DATA_T *completion;
1460 VCHIQ_COMPLETION_DATA_T completiontemp;
1461 struct vchiq_await_completion32 args32;
1462 struct vchiq_completion_data32 completion32;
1463 unsigned int *msgbufcount32;
1464 unsigned int msgbufcount_native;
1465 compat_uptr_t msgbuf32;
1470 args = compat_alloc_user_space(sizeof(*args) +
1471 sizeof(*completion) +
1472 sizeof(*msgbufptr));
1476 completion = (VCHIQ_COMPLETION_DATA_T *)(args + 1);
1477 msgbufptr = (void __user **)(completion + 1);
1479 if (copy_from_user(&args32,
1480 (struct vchiq_completion_data32 *)arg,
1484 if (put_user(args32.count, &args->count) ||
1485 put_user(compat_ptr(args32.buf), &args->buf) ||
1486 put_user(args32.msgbufsize, &args->msgbufsize) ||
1487 put_user(args32.msgbufcount, &args->msgbufcount) ||
1488 put_user(compat_ptr(args32.msgbufs), &args->msgbufs))
1491 /* These are simple cases, so just fall into the native handler */
1492 if (!args32.count || !args32.buf || !args32.msgbufcount)
1493 return vchiq_ioctl(file,
1494 VCHIQ_IOC_AWAIT_COMPLETION,
1495 (unsigned long)args);
1498 * These are the more complex cases. Typical applications of this
1499 * ioctl will use a very large count, with a very large msgbufcount.
1500 * Since the native ioctl can asynchronously fill in the returned
1501 * buffers and the application can in theory begin processing messages
1502 * even before the ioctl returns, a bit of a trick is used here.
1504 * By forcing both count and msgbufcount to be 1, it forces the native
1505 * ioctl to only claim at most 1 message is available. This tricks
1506 * the calling application into thinking only 1 message was actually
1507 * available in the queue so like all good applications it will retry
1508 * waiting until all the required messages are received.
1510 * This trick has been tested and proven to work with vchiq_test,
1511 * Minecraft_PI, the "hello pi" examples, and various other
1512 * applications that are included in Raspbian.
1515 if (copy_from_user(&msgbuf32,
1516 compat_ptr(args32.msgbufs) +
1517 (sizeof(compat_uptr_t) *
1518 (args32.msgbufcount - 1)),
1522 msgbuf = compat_ptr(msgbuf32);
1524 if (copy_to_user(msgbufptr,
1529 if (copy_to_user(&args->msgbufs,
1534 if (put_user(1U, &args->count) ||
1535 put_user(completion, &args->buf) ||
1536 put_user(1U, &args->msgbufcount))
1539 ret = vchiq_ioctl(file,
1540 VCHIQ_IOC_AWAIT_COMPLETION,
1541 (unsigned long)args);
1544 * An return value of 0 here means that no messages where available
1545 * in the message queue. In this case the native ioctl does not
1546 * return any data to the application at all. Not even to update
1547 * msgbufcount. This functionality needs to be kept here for
1550 * Of course, < 0 means that an error occurred and no data is being
1553 * Since count and msgbufcount was forced to 1, that means
1554 * the only other possible return value is 1. Meaning that 1 message
1555 * was available, so that multiple message case does not need to be
1561 if (copy_from_user(&completiontemp, completion, sizeof(*completion)))
1564 completion32.reason = completiontemp.reason;
1565 completion32.header = ptr_to_compat(completiontemp.header);
1566 completion32.service_userdata =
1567 ptr_to_compat(completiontemp.service_userdata);
1568 completion32.bulk_userdata =
1569 ptr_to_compat(completiontemp.bulk_userdata);
1571 if (copy_to_user(compat_ptr(args32.buf),
1573 sizeof(completion32)))
1576 if (get_user(msgbufcount_native, &args->msgbufcount))
1579 if (!msgbufcount_native)
1580 args32.msgbufcount--;
1583 &((struct vchiq_await_completion32 __user *)arg)->msgbufcount;
1585 if (copy_to_user(msgbufcount32,
1586 &args32.msgbufcount,
1587 sizeof(args32.msgbufcount)))
1593 struct vchiq_dequeue_message32 {
1594 unsigned int handle;
1596 unsigned int bufsize;
1600 #define VCHIQ_IOC_DEQUEUE_MESSAGE32 \
1601 _IOWR(VCHIQ_IOC_MAGIC, 8, struct vchiq_dequeue_message32)
1604 vchiq_compat_ioctl_dequeue_message(struct file *file,
1608 VCHIQ_DEQUEUE_MESSAGE_T *args;
1609 struct vchiq_dequeue_message32 args32;
1611 args = compat_alloc_user_space(sizeof(*args));
1615 if (copy_from_user(&args32,
1616 (struct vchiq_dequeue_message32 *)arg,
1620 if (put_user(args32.handle, &args->handle) ||
1621 put_user(args32.blocking, &args->blocking) ||
1622 put_user(args32.bufsize, &args->bufsize) ||
1623 put_user(compat_ptr(args32.buf), &args->buf))
1626 return vchiq_ioctl(file, VCHIQ_IOC_DEQUEUE_MESSAGE,
1627 (unsigned long)args);
1630 struct vchiq_get_config32 {
1631 unsigned int config_size;
1632 compat_uptr_t pconfig;
1635 #define VCHIQ_IOC_GET_CONFIG32 \
1636 _IOWR(VCHIQ_IOC_MAGIC, 10, struct vchiq_get_config32)
1639 vchiq_compat_ioctl_get_config(struct file *file,
1643 VCHIQ_GET_CONFIG_T *args;
1644 struct vchiq_get_config32 args32;
1646 args = compat_alloc_user_space(sizeof(*args));
1650 if (copy_from_user(&args32,
1651 (struct vchiq_get_config32 *)arg,
1655 if (put_user(args32.config_size, &args->config_size) ||
1656 put_user(compat_ptr(args32.pconfig), &args->pconfig))
1659 return vchiq_ioctl(file, VCHIQ_IOC_GET_CONFIG, (unsigned long)args);
1662 #if defined(CONFIG_BCM2835_VCHIQ_SUPPORT_MEMDUMP)
1664 struct vchiq_dump_mem32 {
1665 compat_uptr_t virt_addr;
1669 #define VCHIQ_IOC_DUMP_PHYS_MEM32 \
1670 _IOW(VCHIQ_IOC_MAGIC, 15, struct vchiq_dump_mem32)
1673 vchiq_compat_ioctl_dump_phys_mem(struct file *file,
1677 VCHIQ_DUMP_MEM_T *args;
1678 struct vchiq_dump_mem32 args32;
1680 args = compat_alloc_user_space(sizeof(*args));
1684 if (copy_from_user(&args32,
1685 (struct vchiq_dump_mem32 *)arg,
1689 if (put_user(compat_ptr(args32.virt_addr), &args->virt_addr) ||
1690 put_user(args32.num_bytes, &args->num_bytes))
1693 return vchiq_ioctl(file, VCHIQ_IOC_DUMP_PHYS_MEM, (unsigned long)args);
1699 vchiq_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
1702 case VCHIQ_IOC_CREATE_SERVICE32:
1703 return vchiq_compat_ioctl_create_service(file, cmd, arg);
1704 case VCHIQ_IOC_QUEUE_MESSAGE32:
1705 return vchiq_compat_ioctl_queue_message(file, cmd, arg);
1706 case VCHIQ_IOC_QUEUE_BULK_TRANSMIT32:
1707 case VCHIQ_IOC_QUEUE_BULK_RECEIVE32:
1708 return vchiq_compat_ioctl_queue_bulk(file, cmd, arg);
1709 case VCHIQ_IOC_AWAIT_COMPLETION32:
1710 return vchiq_compat_ioctl_await_completion(file, cmd, arg);
1711 case VCHIQ_IOC_DEQUEUE_MESSAGE32:
1712 return vchiq_compat_ioctl_dequeue_message(file, cmd, arg);
1713 case VCHIQ_IOC_GET_CONFIG32:
1714 return vchiq_compat_ioctl_get_config(file, cmd, arg);
1715 #if defined(CONFIG_BCM2835_VCHIQ_SUPPORT_MEMDUMP)
1716 case VCHIQ_IOC_DUMP_PHYS_MEM32:
1717 return vchiq_compat_ioctl_dump_phys_mem(file, cmd, arg);
1720 return vchiq_ioctl(file, cmd, arg);
1726 /****************************************************************************
1730 ***************************************************************************/
1733 vchiq_open(struct inode *inode, struct file *file)
1735 int dev = iminor(inode) & 0x0f;
1737 vchiq_log_info(vchiq_arm_log_level, "vchiq_open");
1741 VCHIQ_STATE_T *state = vchiq_get_state();
1742 VCHIQ_INSTANCE_T instance;
1745 vchiq_log_error(vchiq_arm_log_level,
1746 "vchiq has no connection to VideoCore");
1750 instance = kzalloc(sizeof(*instance), GFP_KERNEL);
1754 instance->state = state;
1755 instance->pid = current->tgid;
1757 ret = vchiq_debugfs_add_instance(instance);
1763 sema_init(&instance->insert_event, 0);
1764 sema_init(&instance->remove_event, 0);
1765 mutex_init(&instance->completion_mutex);
1766 mutex_init(&instance->bulk_waiter_list_mutex);
1767 INIT_LIST_HEAD(&instance->bulk_waiter_list);
1769 file->private_data = instance;
1773 vchiq_log_error(vchiq_arm_log_level,
1774 "Unknown minor device: %d", dev);
1781 /****************************************************************************
1785 ***************************************************************************/
1788 vchiq_release(struct inode *inode, struct file *file)
1790 int dev = iminor(inode) & 0x0f;
1795 VCHIQ_INSTANCE_T instance = file->private_data;
1796 VCHIQ_STATE_T *state = vchiq_get_state();
1797 VCHIQ_SERVICE_T *service;
1800 vchiq_log_info(vchiq_arm_log_level,
1801 "vchiq_release: instance=%lx",
1802 (unsigned long)instance);
1809 /* Ensure videocore is awake to allow termination. */
1810 vchiq_use_internal(instance->state, NULL,
1813 mutex_lock(&instance->completion_mutex);
1815 /* Wake the completion thread and ask it to exit */
1816 instance->closing = 1;
1817 up(&instance->insert_event);
1819 mutex_unlock(&instance->completion_mutex);
1821 /* Wake the slot handler if the completion queue is full. */
1822 up(&instance->remove_event);
1824 /* Mark all services for termination... */
1826 while ((service = next_service_by_instance(state, instance,
1828 USER_SERVICE_T *user_service = service->base.userdata;
1830 /* Wake the slot handler if the msg queue is full. */
1831 up(&user_service->remove_event);
1833 vchiq_terminate_service_internal(service);
1834 unlock_service(service);
1837 /* ...and wait for them to die */
1839 while ((service = next_service_by_instance(state, instance, &i))
1841 USER_SERVICE_T *user_service = service->base.userdata;
1843 down(&service->remove_event);
1845 BUG_ON(service->srvstate != VCHIQ_SRVSTATE_FREE);
1847 spin_lock(&msg_queue_spinlock);
1849 while (user_service->msg_remove !=
1850 user_service->msg_insert) {
1851 VCHIQ_HEADER_T *header = user_service->
1852 msg_queue[user_service->msg_remove &
1853 (MSG_QUEUE_SIZE - 1)];
1854 user_service->msg_remove++;
1855 spin_unlock(&msg_queue_spinlock);
1858 vchiq_release_message(
1861 spin_lock(&msg_queue_spinlock);
1864 spin_unlock(&msg_queue_spinlock);
1866 unlock_service(service);
1869 /* Release any closed services */
1870 while (instance->completion_remove !=
1871 instance->completion_insert) {
1872 VCHIQ_COMPLETION_DATA_T *completion;
1873 VCHIQ_SERVICE_T *service;
1875 completion = &instance->completions[
1876 instance->completion_remove &
1877 (MAX_COMPLETIONS - 1)];
1878 service = completion->service_userdata;
1879 if (completion->reason == VCHIQ_SERVICE_CLOSED)
1881 USER_SERVICE_T *user_service =
1882 service->base.userdata;
1884 /* Wake any blocked user-thread */
1885 if (instance->use_close_delivered)
1886 up(&user_service->close_event);
1887 unlock_service(service);
1889 instance->completion_remove++;
1892 /* Release the PEER service count. */
1893 vchiq_release_internal(instance->state, NULL);
1896 struct list_head *pos, *next;
1898 list_for_each_safe(pos, next,
1899 &instance->bulk_waiter_list) {
1900 struct bulk_waiter_node *waiter;
1902 waiter = list_entry(pos,
1903 struct bulk_waiter_node,
1906 vchiq_log_info(vchiq_arm_log_level,
1907 "bulk_waiter - cleaned up %pK for pid %d",
1908 waiter, waiter->pid);
1913 vchiq_debugfs_remove_instance(instance);
1916 file->private_data = NULL;
1920 vchiq_log_error(vchiq_arm_log_level,
1921 "Unknown minor device: %d", dev);
1929 /****************************************************************************
1933 ***************************************************************************/
1936 vchiq_dump(void *dump_context, const char *str, int len)
1938 DUMP_CONTEXT_T *context = (DUMP_CONTEXT_T *)dump_context;
1940 if (context->actual < context->space) {
1943 if (context->offset > 0) {
1944 int skip_bytes = min(len, (int)context->offset);
1948 context->offset -= skip_bytes;
1949 if (context->offset > 0)
1952 copy_bytes = min(len, (int)(context->space - context->actual));
1953 if (copy_bytes == 0)
1955 if (copy_to_user(context->buf + context->actual, str,
1957 context->actual = -EFAULT;
1958 context->actual += copy_bytes;
1961 /* If tne terminating NUL is included in the length, then it
1962 ** marks the end of a line and should be replaced with a
1963 ** carriage return. */
1964 if ((len == 0) && (str[copy_bytes - 1] == '\0')) {
1967 if (copy_to_user(context->buf + context->actual - 1,
1969 context->actual = -EFAULT;
1974 /****************************************************************************
1976 * vchiq_dump_platform_instance_state
1978 ***************************************************************************/
1981 vchiq_dump_platform_instances(void *dump_context)
1983 VCHIQ_STATE_T *state = vchiq_get_state();
1988 /* There is no list of instances, so instead scan all services,
1989 marking those that have been dumped. */
1991 for (i = 0; i < state->unused_service; i++) {
1992 VCHIQ_SERVICE_T *service = state->services[i];
1993 VCHIQ_INSTANCE_T instance;
1995 if (service && (service->base.callback == service_callback)) {
1996 instance = service->instance;
2002 for (i = 0; i < state->unused_service; i++) {
2003 VCHIQ_SERVICE_T *service = state->services[i];
2004 VCHIQ_INSTANCE_T instance;
2006 if (service && (service->base.callback == service_callback)) {
2007 instance = service->instance;
2008 if (instance && !instance->mark) {
2009 len = snprintf(buf, sizeof(buf),
2010 "Instance %pK: pid %d,%s completions %d/%d",
2011 instance, instance->pid,
2012 instance->connected ? " connected, " :
2014 instance->completion_insert -
2015 instance->completion_remove,
2018 vchiq_dump(dump_context, buf, len + 1);
2026 /****************************************************************************
2028 * vchiq_dump_platform_service_state
2030 ***************************************************************************/
2033 vchiq_dump_platform_service_state(void *dump_context, VCHIQ_SERVICE_T *service)
2035 USER_SERVICE_T *user_service = (USER_SERVICE_T *)service->base.userdata;
2039 len = snprintf(buf, sizeof(buf), " instance %pK", service->instance);
2041 if ((service->base.callback == service_callback) &&
2042 user_service->is_vchi) {
2043 len += snprintf(buf + len, sizeof(buf) - len,
2045 user_service->msg_insert - user_service->msg_remove,
2048 if (user_service->dequeue_pending)
2049 len += snprintf(buf + len, sizeof(buf) - len,
2050 " (dequeue pending)");
2053 vchiq_dump(dump_context, buf, len + 1);
2056 /****************************************************************************
2060 ***************************************************************************/
2062 #if defined(CONFIG_BCM2835_VCHIQ_SUPPORT_MEMDUMP)
2065 dump_phys_mem(void *virt_addr, u32 num_bytes)
2068 u8 *end_virt_addr = virt_addr + num_bytes;
2075 struct page **pages;
2076 u8 *kmapped_virt_ptr;
2078 /* Align virt_addr and end_virt_addr to 16 byte boundaries. */
2080 virt_addr = (void *)((unsigned long)virt_addr & ~0x0fuL);
2081 end_virt_addr = (void *)(((unsigned long)end_virt_addr + 15uL) &
2084 offset = (int)(long)virt_addr & (PAGE_SIZE - 1);
2085 end_offset = (int)(long)end_virt_addr & (PAGE_SIZE - 1);
2087 num_pages = DIV_ROUND_UP(offset + num_bytes, PAGE_SIZE);
2089 pages = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
2091 vchiq_log_error(vchiq_arm_log_level,
2092 "Unable to allocation memory for %d pages\n",
2097 down_read(¤t->mm->mmap_sem);
2098 rc = get_user_pages(
2099 (unsigned long)virt_addr, /* start */
2100 num_pages, /* len */
2102 pages, /* pages (array of page pointers) */
2104 up_read(¤t->mm->mmap_sem);
2110 vchiq_log_error(vchiq_arm_log_level,
2111 "Failed to get user pages: %d\n", rc);
2115 while (offset < end_offset) {
2116 int page_offset = offset % PAGE_SIZE;
2118 page_idx = offset / PAGE_SIZE;
2119 if (page_idx != prev_idx) {
2122 page = pages[page_idx];
2123 kmapped_virt_ptr = kmap(page);
2124 prev_idx = page_idx;
2127 if (vchiq_arm_log_level >= VCHIQ_LOG_TRACE)
2128 vchiq_log_dump_mem("ph",
2129 (u32)(unsigned long)&kmapped_virt_ptr[
2131 &kmapped_virt_ptr[page_offset], 16);
2140 for (page_idx = 0; page_idx < num_pages; page_idx++)
2141 put_page(pages[page_idx]);
2148 /****************************************************************************
2152 ***************************************************************************/
2155 vchiq_read(struct file *file, char __user *buf,
2156 size_t count, loff_t *ppos)
2158 DUMP_CONTEXT_T context;
2162 context.space = count;
2163 context.offset = *ppos;
2165 vchiq_dump_state(&context, &g_state);
2167 *ppos += context.actual;
2169 return context.actual;
2173 vchiq_get_state(void)
2176 if (g_state.remote == NULL)
2177 printk(KERN_ERR "%s: g_state.remote == NULL\n", __func__);
2178 else if (g_state.remote->initialised != 1)
2179 printk(KERN_NOTICE "%s: g_state.remote->initialised != 1 (%d)\n",
2180 __func__, g_state.remote->initialised);
2182 return ((g_state.remote != NULL) &&
2183 (g_state.remote->initialised == 1)) ? &g_state : NULL;
2186 static const struct file_operations
2188 .owner = THIS_MODULE,
2189 .unlocked_ioctl = vchiq_ioctl,
2190 #if defined(CONFIG_COMPAT)
2191 .compat_ioctl = vchiq_compat_ioctl,
2194 .release = vchiq_release,
2199 * Autosuspend related functionality
2203 vchiq_videocore_wanted(VCHIQ_STATE_T *state)
2205 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
2208 /* autosuspend not supported - always return wanted */
2210 else if (arm_state->blocked_count)
2212 else if (!arm_state->videocore_use_count)
2213 /* usage count zero - check for override unless we're forcing */
2214 if (arm_state->resume_blocked)
2217 return vchiq_platform_videocore_wanted(state);
2219 /* non-zero usage count - videocore still required */
2223 static VCHIQ_STATUS_T
2224 vchiq_keepalive_vchiq_callback(VCHIQ_REASON_T reason,
2225 VCHIQ_HEADER_T *header,
2226 VCHIQ_SERVICE_HANDLE_T service_user,
2229 vchiq_log_error(vchiq_susp_log_level,
2230 "%s callback reason %d", __func__, reason);
2235 vchiq_keepalive_thread_func(void *v)
2237 VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
2238 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
2240 VCHIQ_STATUS_T status;
2241 VCHIQ_INSTANCE_T instance;
2242 VCHIQ_SERVICE_HANDLE_T ka_handle;
2244 VCHIQ_SERVICE_PARAMS_T params = {
2245 .fourcc = VCHIQ_MAKE_FOURCC('K', 'E', 'E', 'P'),
2246 .callback = vchiq_keepalive_vchiq_callback,
2247 .version = KEEPALIVE_VER,
2248 .version_min = KEEPALIVE_VER_MIN
2251 status = vchiq_initialise(&instance);
2252 if (status != VCHIQ_SUCCESS) {
2253 vchiq_log_error(vchiq_susp_log_level,
2254 "%s vchiq_initialise failed %d", __func__, status);
2258 status = vchiq_connect(instance);
2259 if (status != VCHIQ_SUCCESS) {
2260 vchiq_log_error(vchiq_susp_log_level,
2261 "%s vchiq_connect failed %d", __func__, status);
2265 status = vchiq_add_service(instance, ¶ms, &ka_handle);
2266 if (status != VCHIQ_SUCCESS) {
2267 vchiq_log_error(vchiq_susp_log_level,
2268 "%s vchiq_open_service failed %d", __func__, status);
2273 long rc = 0, uc = 0;
2275 if (wait_for_completion_interruptible(&arm_state->ka_evt)
2277 vchiq_log_error(vchiq_susp_log_level,
2278 "%s interrupted", __func__);
2279 flush_signals(current);
2283 /* read and clear counters. Do release_count then use_count to
2284 * prevent getting more releases than uses */
2285 rc = atomic_xchg(&arm_state->ka_release_count, 0);
2286 uc = atomic_xchg(&arm_state->ka_use_count, 0);
2288 /* Call use/release service the requisite number of times.
2289 * Process use before release so use counts don't go negative */
2291 atomic_inc(&arm_state->ka_use_ack_count);
2292 status = vchiq_use_service(ka_handle);
2293 if (status != VCHIQ_SUCCESS) {
2294 vchiq_log_error(vchiq_susp_log_level,
2295 "%s vchiq_use_service error %d",
2300 status = vchiq_release_service(ka_handle);
2301 if (status != VCHIQ_SUCCESS) {
2302 vchiq_log_error(vchiq_susp_log_level,
2303 "%s vchiq_release_service error %d",
2310 vchiq_shutdown(instance);
2318 vchiq_arm_init_state(VCHIQ_STATE_T *state, VCHIQ_ARM_STATE_T *arm_state)
2321 rwlock_init(&arm_state->susp_res_lock);
2323 init_completion(&arm_state->ka_evt);
2324 atomic_set(&arm_state->ka_use_count, 0);
2325 atomic_set(&arm_state->ka_use_ack_count, 0);
2326 atomic_set(&arm_state->ka_release_count, 0);
2328 init_completion(&arm_state->vc_suspend_complete);
2330 init_completion(&arm_state->vc_resume_complete);
2331 /* Initialise to 'done' state. We only want to block on resume
2332 * completion while videocore is suspended. */
2333 set_resume_state(arm_state, VC_RESUME_RESUMED);
2335 init_completion(&arm_state->resume_blocker);
2336 /* Initialise to 'done' state. We only want to block on this
2337 * completion while resume is blocked */
2338 complete_all(&arm_state->resume_blocker);
2340 init_completion(&arm_state->blocked_blocker);
2341 /* Initialise to 'done' state. We only want to block on this
2342 * completion while things are waiting on the resume blocker */
2343 complete_all(&arm_state->blocked_blocker);
2345 arm_state->suspend_timer_timeout = SUSPEND_TIMER_TIMEOUT_MS;
2346 arm_state->suspend_timer_running = 0;
2347 setup_timer(&arm_state->suspend_timer, suspend_timer_callback,
2348 (unsigned long)(state));
2350 arm_state->first_connect = 0;
2353 return VCHIQ_SUCCESS;
2357 ** Functions to modify the state variables;
2358 ** set_suspend_state
2361 ** There are more state variables than we might like, so ensure they remain in
2362 ** step. Suspend and resume state are maintained separately, since most of
2363 ** these state machines can operate independently. However, there are a few
2364 ** states where state transitions in one state machine cause a reset to the
2365 ** other state machine. In addition, there are some completion events which
2366 ** need to occur on state machine reset and end-state(s), so these are also
2367 ** dealt with in these functions.
2369 ** In all states we set the state variable according to the input, but in some
2370 ** cases we perform additional steps outlined below;
2372 ** VC_SUSPEND_IDLE - Initialise the suspend completion at the same time.
2373 ** The suspend completion is completed after any suspend
2374 ** attempt. When we reset the state machine we also reset
2375 ** the completion. This reset occurs when videocore is
2376 ** resumed, and also if we initiate suspend after a suspend
2379 ** VC_SUSPEND_IN_PROGRESS - This state is considered the point of no return for
2380 ** suspend - ie from this point on we must try to suspend
2381 ** before resuming can occur. We therefore also reset the
2382 ** resume state machine to VC_RESUME_IDLE in this state.
2384 ** VC_SUSPEND_SUSPENDED - Suspend has completed successfully. Also call
2385 ** complete_all on the suspend completion to notify
2386 ** anything waiting for suspend to happen.
2388 ** VC_SUSPEND_REJECTED - Videocore rejected suspend. Videocore will also
2389 ** initiate resume, so no need to alter resume state.
2390 ** We call complete_all on the suspend completion to notify
2391 ** of suspend rejection.
2393 ** VC_SUSPEND_FAILED - We failed to initiate videocore suspend. We notify the
2394 ** suspend completion and reset the resume state machine.
2396 ** VC_RESUME_IDLE - Initialise the resume completion at the same time. The
2397 ** resume completion is in it's 'done' state whenever
2398 ** videcore is running. Therefore, the VC_RESUME_IDLE
2399 ** state implies that videocore is suspended.
2400 ** Hence, any thread which needs to wait until videocore is
2401 ** running can wait on this completion - it will only block
2402 ** if videocore is suspended.
2404 ** VC_RESUME_RESUMED - Resume has completed successfully. Videocore is running.
2405 ** Call complete_all on the resume completion to unblock
2406 ** any threads waiting for resume. Also reset the suspend
2407 ** state machine to it's idle state.
2409 ** VC_RESUME_FAILED - Currently unused - no mechanism to fail resume exists.
2413 set_suspend_state(VCHIQ_ARM_STATE_T *arm_state,
2414 enum vc_suspend_status new_state)
2416 /* set the state in all cases */
2417 arm_state->vc_suspend_state = new_state;
2419 /* state specific additional actions */
2420 switch (new_state) {
2421 case VC_SUSPEND_FORCE_CANCELED:
2422 complete_all(&arm_state->vc_suspend_complete);
2424 case VC_SUSPEND_REJECTED:
2425 complete_all(&arm_state->vc_suspend_complete);
2427 case VC_SUSPEND_FAILED:
2428 complete_all(&arm_state->vc_suspend_complete);
2429 arm_state->vc_resume_state = VC_RESUME_RESUMED;
2430 complete_all(&arm_state->vc_resume_complete);
2432 case VC_SUSPEND_IDLE:
2433 reinit_completion(&arm_state->vc_suspend_complete);
2435 case VC_SUSPEND_REQUESTED:
2437 case VC_SUSPEND_IN_PROGRESS:
2438 set_resume_state(arm_state, VC_RESUME_IDLE);
2440 case VC_SUSPEND_SUSPENDED:
2441 complete_all(&arm_state->vc_suspend_complete);
2450 set_resume_state(VCHIQ_ARM_STATE_T *arm_state,
2451 enum vc_resume_status new_state)
2453 /* set the state in all cases */
2454 arm_state->vc_resume_state = new_state;
2456 /* state specific additional actions */
2457 switch (new_state) {
2458 case VC_RESUME_FAILED:
2460 case VC_RESUME_IDLE:
2461 reinit_completion(&arm_state->vc_resume_complete);
2463 case VC_RESUME_REQUESTED:
2465 case VC_RESUME_IN_PROGRESS:
2467 case VC_RESUME_RESUMED:
2468 complete_all(&arm_state->vc_resume_complete);
2469 set_suspend_state(arm_state, VC_SUSPEND_IDLE);
2478 /* should be called with the write lock held */
2480 start_suspend_timer(VCHIQ_ARM_STATE_T *arm_state)
2482 del_timer(&arm_state->suspend_timer);
2483 arm_state->suspend_timer.expires = jiffies +
2484 msecs_to_jiffies(arm_state->
2485 suspend_timer_timeout);
2486 add_timer(&arm_state->suspend_timer);
2487 arm_state->suspend_timer_running = 1;
2490 /* should be called with the write lock held */
2492 stop_suspend_timer(VCHIQ_ARM_STATE_T *arm_state)
2494 if (arm_state->suspend_timer_running) {
2495 del_timer(&arm_state->suspend_timer);
2496 arm_state->suspend_timer_running = 0;
2501 need_resume(VCHIQ_STATE_T *state)
2503 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
2505 return (arm_state->vc_suspend_state > VC_SUSPEND_IDLE) &&
2506 (arm_state->vc_resume_state < VC_RESUME_REQUESTED) &&
2507 vchiq_videocore_wanted(state);
2511 block_resume(VCHIQ_ARM_STATE_T *arm_state)
2513 int status = VCHIQ_SUCCESS;
2514 const unsigned long timeout_val =
2515 msecs_to_jiffies(FORCE_SUSPEND_TIMEOUT_MS);
2516 int resume_count = 0;
2518 /* Allow any threads which were blocked by the last force suspend to
2519 * complete if they haven't already. Only give this one shot; if
2520 * blocked_count is incremented after blocked_blocker is completed
2521 * (which only happens when blocked_count hits 0) then those threads
2522 * will have to wait until next time around */
2523 if (arm_state->blocked_count) {
2524 reinit_completion(&arm_state->blocked_blocker);
2525 write_unlock_bh(&arm_state->susp_res_lock);
2526 vchiq_log_info(vchiq_susp_log_level, "%s wait for previously "
2527 "blocked clients", __func__);
2528 if (wait_for_completion_interruptible_timeout(
2529 &arm_state->blocked_blocker, timeout_val)
2531 vchiq_log_error(vchiq_susp_log_level, "%s wait for "
2532 "previously blocked clients failed", __func__);
2533 status = VCHIQ_ERROR;
2534 write_lock_bh(&arm_state->susp_res_lock);
2537 vchiq_log_info(vchiq_susp_log_level, "%s previously blocked "
2538 "clients resumed", __func__);
2539 write_lock_bh(&arm_state->susp_res_lock);
2542 /* We need to wait for resume to complete if it's in process */
2543 while (arm_state->vc_resume_state != VC_RESUME_RESUMED &&
2544 arm_state->vc_resume_state > VC_RESUME_IDLE) {
2545 if (resume_count > 1) {
2546 status = VCHIQ_ERROR;
2547 vchiq_log_error(vchiq_susp_log_level, "%s waited too "
2548 "many times for resume", __func__);
2551 write_unlock_bh(&arm_state->susp_res_lock);
2552 vchiq_log_info(vchiq_susp_log_level, "%s wait for resume",
2554 if (wait_for_completion_interruptible_timeout(
2555 &arm_state->vc_resume_complete, timeout_val)
2557 vchiq_log_error(vchiq_susp_log_level, "%s wait for "
2558 "resume failed (%s)", __func__,
2559 resume_state_names[arm_state->vc_resume_state +
2560 VC_RESUME_NUM_OFFSET]);
2561 status = VCHIQ_ERROR;
2562 write_lock_bh(&arm_state->susp_res_lock);
2565 vchiq_log_info(vchiq_susp_log_level, "%s resumed", __func__);
2566 write_lock_bh(&arm_state->susp_res_lock);
2569 reinit_completion(&arm_state->resume_blocker);
2570 arm_state->resume_blocked = 1;
2577 unblock_resume(VCHIQ_ARM_STATE_T *arm_state)
2579 complete_all(&arm_state->resume_blocker);
2580 arm_state->resume_blocked = 0;
2583 /* Initiate suspend via slot handler. Should be called with the write lock
2586 vchiq_arm_vcsuspend(VCHIQ_STATE_T *state)
2588 VCHIQ_STATUS_T status = VCHIQ_ERROR;
2589 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
2594 vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
2595 status = VCHIQ_SUCCESS;
2598 switch (arm_state->vc_suspend_state) {
2599 case VC_SUSPEND_REQUESTED:
2600 vchiq_log_info(vchiq_susp_log_level, "%s: suspend already "
2601 "requested", __func__);
2603 case VC_SUSPEND_IN_PROGRESS:
2604 vchiq_log_info(vchiq_susp_log_level, "%s: suspend already in "
2605 "progress", __func__);
2609 /* We don't expect to be in other states, so log but continue
2611 vchiq_log_error(vchiq_susp_log_level,
2612 "%s unexpected suspend state %s", __func__,
2613 suspend_state_names[arm_state->vc_suspend_state +
2614 VC_SUSPEND_NUM_OFFSET]);
2616 case VC_SUSPEND_REJECTED:
2617 case VC_SUSPEND_FAILED:
2618 /* Ensure any idle state actions have been run */
2619 set_suspend_state(arm_state, VC_SUSPEND_IDLE);
2621 case VC_SUSPEND_IDLE:
2622 vchiq_log_info(vchiq_susp_log_level,
2623 "%s: suspending", __func__);
2624 set_suspend_state(arm_state, VC_SUSPEND_REQUESTED);
2625 /* kick the slot handler thread to initiate suspend */
2626 request_poll(state, NULL, 0);
2631 vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, status);
2636 vchiq_platform_check_suspend(VCHIQ_STATE_T *state)
2638 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
2644 vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
2646 write_lock_bh(&arm_state->susp_res_lock);
2647 if (arm_state->vc_suspend_state == VC_SUSPEND_REQUESTED &&
2648 arm_state->vc_resume_state == VC_RESUME_RESUMED) {
2649 set_suspend_state(arm_state, VC_SUSPEND_IN_PROGRESS);
2652 write_unlock_bh(&arm_state->susp_res_lock);
2655 vchiq_platform_suspend(state);
2658 vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
2664 output_timeout_error(VCHIQ_STATE_T *state)
2666 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
2668 int vc_use_count = arm_state->videocore_use_count;
2669 int active_services = state->unused_service;
2672 if (!arm_state->videocore_use_count) {
2673 snprintf(err, sizeof(err), " Videocore usecount is 0");
2676 for (i = 0; i < active_services; i++) {
2677 VCHIQ_SERVICE_T *service_ptr = state->services[i];
2679 if (service_ptr && service_ptr->service_use_count &&
2680 (service_ptr->srvstate != VCHIQ_SRVSTATE_FREE)) {
2681 snprintf(err, sizeof(err), " %c%c%c%c(%d) service has "
2682 "use count %d%s", VCHIQ_FOURCC_AS_4CHARS(
2683 service_ptr->base.fourcc),
2684 service_ptr->client_id,
2685 service_ptr->service_use_count,
2686 service_ptr->service_use_count ==
2687 vc_use_count ? "" : " (+ more)");
2693 vchiq_log_error(vchiq_susp_log_level,
2694 "timed out waiting for vc suspend (%d).%s",
2695 arm_state->autosuspend_override, err);
2699 /* Try to get videocore into suspended state, regardless of autosuspend state.
2700 ** We don't actually force suspend, since videocore may get into a bad state
2701 ** if we force suspend at a bad time. Instead, we wait for autosuspend to
2702 ** determine a good point to suspend. If this doesn't happen within 100ms we
2705 ** Returns VCHIQ_SUCCESS if videocore suspended successfully, VCHIQ_RETRY if
2706 ** videocore failed to suspend in time or VCHIQ_ERROR if interrupted.
2709 vchiq_arm_force_suspend(VCHIQ_STATE_T *state)
2711 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
2712 VCHIQ_STATUS_T status = VCHIQ_ERROR;
2719 vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
2721 write_lock_bh(&arm_state->susp_res_lock);
2723 status = block_resume(arm_state);
2724 if (status != VCHIQ_SUCCESS)
2726 if (arm_state->vc_suspend_state == VC_SUSPEND_SUSPENDED) {
2727 /* Already suspended - just block resume and exit */
2728 vchiq_log_info(vchiq_susp_log_level, "%s already suspended",
2730 status = VCHIQ_SUCCESS;
2732 } else if (arm_state->vc_suspend_state <= VC_SUSPEND_IDLE) {
2733 /* initiate suspend immediately in the case that we're waiting
2734 * for the timeout */
2735 stop_suspend_timer(arm_state);
2736 if (!vchiq_videocore_wanted(state)) {
2737 vchiq_log_info(vchiq_susp_log_level, "%s videocore "
2738 "idle, initiating suspend", __func__);
2739 status = vchiq_arm_vcsuspend(state);
2740 } else if (arm_state->autosuspend_override <
2741 FORCE_SUSPEND_FAIL_MAX) {
2742 vchiq_log_info(vchiq_susp_log_level, "%s letting "
2743 "videocore go idle", __func__);
2744 status = VCHIQ_SUCCESS;
2746 vchiq_log_warning(vchiq_susp_log_level, "%s failed too "
2747 "many times - attempting suspend", __func__);
2748 status = vchiq_arm_vcsuspend(state);
2751 vchiq_log_info(vchiq_susp_log_level, "%s videocore suspend "
2752 "in progress - wait for completion", __func__);
2753 status = VCHIQ_SUCCESS;
2756 /* Wait for suspend to happen due to system idle (not forced..) */
2757 if (status != VCHIQ_SUCCESS)
2758 goto unblock_resume;
2761 write_unlock_bh(&arm_state->susp_res_lock);
2763 rc = wait_for_completion_interruptible_timeout(
2764 &arm_state->vc_suspend_complete,
2765 msecs_to_jiffies(FORCE_SUSPEND_TIMEOUT_MS));
2767 write_lock_bh(&arm_state->susp_res_lock);
2769 vchiq_log_warning(vchiq_susp_log_level, "%s "
2770 "interrupted waiting for suspend", __func__);
2771 status = VCHIQ_ERROR;
2772 goto unblock_resume;
2773 } else if (rc == 0) {
2774 if (arm_state->vc_suspend_state > VC_SUSPEND_IDLE) {
2775 /* Repeat timeout once if in progress */
2781 arm_state->autosuspend_override++;
2782 output_timeout_error(state);
2784 status = VCHIQ_RETRY;
2785 goto unblock_resume;
2787 } while (0 < (repeat--));
2789 /* Check and report state in case we need to abort ARM suspend */
2790 if (arm_state->vc_suspend_state != VC_SUSPEND_SUSPENDED) {
2791 status = VCHIQ_RETRY;
2792 vchiq_log_error(vchiq_susp_log_level,
2793 "%s videocore suspend failed (state %s)", __func__,
2794 suspend_state_names[arm_state->vc_suspend_state +
2795 VC_SUSPEND_NUM_OFFSET]);
2796 /* Reset the state only if it's still in an error state.
2797 * Something could have already initiated another suspend. */
2798 if (arm_state->vc_suspend_state < VC_SUSPEND_IDLE)
2799 set_suspend_state(arm_state, VC_SUSPEND_IDLE);
2801 goto unblock_resume;
2804 /* successfully suspended - unlock and exit */
2808 /* all error states need to unblock resume before exit */
2809 unblock_resume(arm_state);
2812 write_unlock_bh(&arm_state->susp_res_lock);
2815 vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, status);
2820 vchiq_check_suspend(VCHIQ_STATE_T *state)
2822 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
2827 vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
2829 write_lock_bh(&arm_state->susp_res_lock);
2830 if (arm_state->vc_suspend_state != VC_SUSPEND_SUSPENDED &&
2831 arm_state->first_connect &&
2832 !vchiq_videocore_wanted(state)) {
2833 vchiq_arm_vcsuspend(state);
2835 write_unlock_bh(&arm_state->susp_res_lock);
2838 vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
2844 vchiq_arm_allow_resume(VCHIQ_STATE_T *state)
2846 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
2853 vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
2855 write_lock_bh(&arm_state->susp_res_lock);
2856 unblock_resume(arm_state);
2857 resume = vchiq_check_resume(state);
2858 write_unlock_bh(&arm_state->susp_res_lock);
2861 if (wait_for_completion_interruptible(
2862 &arm_state->vc_resume_complete) < 0) {
2863 vchiq_log_error(vchiq_susp_log_level,
2864 "%s interrupted", __func__);
2865 /* failed, cannot accurately derive suspend
2866 * state, so exit early. */
2871 read_lock_bh(&arm_state->susp_res_lock);
2872 if (arm_state->vc_suspend_state == VC_SUSPEND_SUSPENDED) {
2873 vchiq_log_info(vchiq_susp_log_level,
2874 "%s: Videocore remains suspended", __func__);
2876 vchiq_log_info(vchiq_susp_log_level,
2877 "%s: Videocore resumed", __func__);
2880 read_unlock_bh(&arm_state->susp_res_lock);
2882 vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, ret);
2886 /* This function should be called with the write lock held */
2888 vchiq_check_resume(VCHIQ_STATE_T *state)
2890 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
2896 vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
2898 if (need_resume(state)) {
2899 set_resume_state(arm_state, VC_RESUME_REQUESTED);
2900 request_poll(state, NULL, 0);
2905 vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
2910 vchiq_use_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service,
2911 enum USE_TYPE_E use_type)
2913 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
2914 VCHIQ_STATUS_T ret = VCHIQ_SUCCESS;
2917 int local_uc, local_entity_uc;
2922 vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
2924 if (use_type == USE_TYPE_VCHIQ) {
2925 sprintf(entity, "VCHIQ: ");
2926 entity_uc = &arm_state->peer_use_count;
2927 } else if (service) {
2928 sprintf(entity, "%c%c%c%c:%03d",
2929 VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
2930 service->client_id);
2931 entity_uc = &service->service_use_count;
2933 vchiq_log_error(vchiq_susp_log_level, "%s null service "
2939 write_lock_bh(&arm_state->susp_res_lock);
2940 while (arm_state->resume_blocked) {
2941 /* If we call 'use' while force suspend is waiting for suspend,
2942 * then we're about to block the thread which the force is
2943 * waiting to complete, so we're bound to just time out. In this
2944 * case, set the suspend state such that the wait will be
2945 * canceled, so we can complete as quickly as possible. */
2946 if (arm_state->resume_blocked && arm_state->vc_suspend_state ==
2948 set_suspend_state(arm_state, VC_SUSPEND_FORCE_CANCELED);
2951 /* If suspend is already in progress then we need to block */
2952 if (!try_wait_for_completion(&arm_state->resume_blocker)) {
2953 /* Indicate that there are threads waiting on the resume
2954 * blocker. These need to be allowed to complete before
2955 * a _second_ call to force suspend can complete,
2956 * otherwise low priority threads might never actually
2958 arm_state->blocked_count++;
2959 write_unlock_bh(&arm_state->susp_res_lock);
2960 vchiq_log_info(vchiq_susp_log_level, "%s %s resume "
2961 "blocked - waiting...", __func__, entity);
2962 if (wait_for_completion_killable(
2963 &arm_state->resume_blocker) != 0) {
2964 vchiq_log_error(vchiq_susp_log_level, "%s %s "
2965 "wait for resume blocker interrupted",
2968 write_lock_bh(&arm_state->susp_res_lock);
2969 arm_state->blocked_count--;
2970 write_unlock_bh(&arm_state->susp_res_lock);
2973 vchiq_log_info(vchiq_susp_log_level, "%s %s resume "
2974 "unblocked", __func__, entity);
2975 write_lock_bh(&arm_state->susp_res_lock);
2976 if (--arm_state->blocked_count == 0)
2977 complete_all(&arm_state->blocked_blocker);
2981 stop_suspend_timer(arm_state);
2983 local_uc = ++arm_state->videocore_use_count;
2984 local_entity_uc = ++(*entity_uc);
2986 /* If there's a pending request which hasn't yet been serviced then
2987 * just clear it. If we're past VC_SUSPEND_REQUESTED state then
2988 * vc_resume_complete will block until we either resume or fail to
2990 if (arm_state->vc_suspend_state <= VC_SUSPEND_REQUESTED)
2991 set_suspend_state(arm_state, VC_SUSPEND_IDLE);
2993 if ((use_type != USE_TYPE_SERVICE_NO_RESUME) && need_resume(state)) {
2994 set_resume_state(arm_state, VC_RESUME_REQUESTED);
2995 vchiq_log_info(vchiq_susp_log_level,
2996 "%s %s count %d, state count %d",
2997 __func__, entity, local_entity_uc, local_uc);
2998 request_poll(state, NULL, 0);
3000 vchiq_log_trace(vchiq_susp_log_level,
3001 "%s %s count %d, state count %d",
3002 __func__, entity, *entity_uc, local_uc);
3005 write_unlock_bh(&arm_state->susp_res_lock);
3007 /* Completion is in a done state when we're not suspended, so this won't
3008 * block for the non-suspended case. */
3009 if (!try_wait_for_completion(&arm_state->vc_resume_complete)) {
3010 vchiq_log_info(vchiq_susp_log_level, "%s %s wait for resume",
3012 if (wait_for_completion_killable(
3013 &arm_state->vc_resume_complete) != 0) {
3014 vchiq_log_error(vchiq_susp_log_level, "%s %s wait for "
3015 "resume interrupted", __func__, entity);
3019 vchiq_log_info(vchiq_susp_log_level, "%s %s resumed", __func__,
3023 if (ret == VCHIQ_SUCCESS) {
3024 VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
3025 long ack_cnt = atomic_xchg(&arm_state->ka_use_ack_count, 0);
3027 while (ack_cnt && (status == VCHIQ_SUCCESS)) {
3028 /* Send the use notify to videocore */
3029 status = vchiq_send_remote_use_active(state);
3030 if (status == VCHIQ_SUCCESS)
3034 &arm_state->ka_use_ack_count);
3039 vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, ret);
3044 vchiq_release_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service)
3046 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
3047 VCHIQ_STATUS_T ret = VCHIQ_SUCCESS;
3050 int local_uc, local_entity_uc;
3055 vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
3058 sprintf(entity, "%c%c%c%c:%03d",
3059 VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
3060 service->client_id);
3061 entity_uc = &service->service_use_count;
3063 sprintf(entity, "PEER: ");
3064 entity_uc = &arm_state->peer_use_count;
3067 write_lock_bh(&arm_state->susp_res_lock);
3068 if (!arm_state->videocore_use_count || !(*entity_uc)) {
3069 /* Don't use BUG_ON - don't allow user thread to crash kernel */
3070 WARN_ON(!arm_state->videocore_use_count);
3071 WARN_ON(!(*entity_uc));
3075 local_uc = --arm_state->videocore_use_count;
3076 local_entity_uc = --(*entity_uc);
3078 if (!vchiq_videocore_wanted(state)) {
3079 if (vchiq_platform_use_suspend_timer() &&
3080 !arm_state->resume_blocked) {
3081 /* Only use the timer if we're not trying to force
3082 * suspend (=> resume_blocked) */
3083 start_suspend_timer(arm_state);
3085 vchiq_log_info(vchiq_susp_log_level,
3086 "%s %s count %d, state count %d - suspending",
3087 __func__, entity, *entity_uc,
3088 arm_state->videocore_use_count);
3089 vchiq_arm_vcsuspend(state);
3092 vchiq_log_trace(vchiq_susp_log_level,
3093 "%s %s count %d, state count %d",
3094 __func__, entity, *entity_uc,
3095 arm_state->videocore_use_count);
3098 write_unlock_bh(&arm_state->susp_res_lock);
3101 vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, ret);
3106 vchiq_on_remote_use(VCHIQ_STATE_T *state)
3108 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
3110 vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
3111 atomic_inc(&arm_state->ka_use_count);
3112 complete(&arm_state->ka_evt);
3116 vchiq_on_remote_release(VCHIQ_STATE_T *state)
3118 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
3120 vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
3121 atomic_inc(&arm_state->ka_release_count);
3122 complete(&arm_state->ka_evt);
3126 vchiq_use_service_internal(VCHIQ_SERVICE_T *service)
3128 return vchiq_use_internal(service->state, service, USE_TYPE_SERVICE);
3132 vchiq_release_service_internal(VCHIQ_SERVICE_T *service)
3134 return vchiq_release_internal(service->state, service);
3137 VCHIQ_DEBUGFS_NODE_T *
3138 vchiq_instance_get_debugfs_node(VCHIQ_INSTANCE_T instance)
3140 return &instance->debugfs_node;
3144 vchiq_instance_get_use_count(VCHIQ_INSTANCE_T instance)
3146 VCHIQ_SERVICE_T *service;
3147 int use_count = 0, i;
3150 while ((service = next_service_by_instance(instance->state,
3151 instance, &i)) != NULL) {
3152 use_count += service->service_use_count;
3153 unlock_service(service);
3159 vchiq_instance_get_pid(VCHIQ_INSTANCE_T instance)
3161 return instance->pid;
3165 vchiq_instance_get_trace(VCHIQ_INSTANCE_T instance)
3167 return instance->trace;
3171 vchiq_instance_set_trace(VCHIQ_INSTANCE_T instance, int trace)
3173 VCHIQ_SERVICE_T *service;
3177 while ((service = next_service_by_instance(instance->state,
3178 instance, &i)) != NULL) {
3179 service->trace = trace;
3180 unlock_service(service);
3182 instance->trace = (trace != 0);
3185 static void suspend_timer_callback(unsigned long context)
3187 VCHIQ_STATE_T *state = (VCHIQ_STATE_T *)context;
3188 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
3192 vchiq_log_info(vchiq_susp_log_level,
3193 "%s - suspend timer expired - check suspend", __func__);
3194 vchiq_check_suspend(state);
3200 vchiq_use_service_no_resume(VCHIQ_SERVICE_HANDLE_T handle)
3202 VCHIQ_STATUS_T ret = VCHIQ_ERROR;
3203 VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
3206 ret = vchiq_use_internal(service->state, service,
3207 USE_TYPE_SERVICE_NO_RESUME);
3208 unlock_service(service);
3214 vchiq_use_service(VCHIQ_SERVICE_HANDLE_T handle)
3216 VCHIQ_STATUS_T ret = VCHIQ_ERROR;
3217 VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
3220 ret = vchiq_use_internal(service->state, service,
3222 unlock_service(service);
3228 vchiq_release_service(VCHIQ_SERVICE_HANDLE_T handle)
3230 VCHIQ_STATUS_T ret = VCHIQ_ERROR;
3231 VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
3234 ret = vchiq_release_internal(service->state, service);
3235 unlock_service(service);
3241 vchiq_dump_service_use_state(VCHIQ_STATE_T *state)
3243 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
3245 /* Only dump 64 services */
3246 static const int local_max_services = 64;
3247 /* If there's more than 64 services, only dump ones with
3248 * non-zero counts */
3249 int only_nonzero = 0;
3250 static const char *nz = "<-- preventing suspend";
3252 enum vc_suspend_status vc_suspend_state;
3253 enum vc_resume_status vc_resume_state;
3256 int active_services;
3257 struct service_data_struct {
3261 } service_data[local_max_services];
3266 read_lock_bh(&arm_state->susp_res_lock);
3267 vc_suspend_state = arm_state->vc_suspend_state;
3268 vc_resume_state = arm_state->vc_resume_state;
3269 peer_count = arm_state->peer_use_count;
3270 vc_use_count = arm_state->videocore_use_count;
3271 active_services = state->unused_service;
3272 if (active_services > local_max_services)
3275 for (i = 0; (i < active_services) && (j < local_max_services); i++) {
3276 VCHIQ_SERVICE_T *service_ptr = state->services[i];
3281 if (only_nonzero && !service_ptr->service_use_count)
3284 if (service_ptr->srvstate == VCHIQ_SRVSTATE_FREE)
3287 service_data[j].fourcc = service_ptr->base.fourcc;
3288 service_data[j].clientid = service_ptr->client_id;
3289 service_data[j++].use_count = service_ptr->service_use_count;
3292 read_unlock_bh(&arm_state->susp_res_lock);
3294 vchiq_log_warning(vchiq_susp_log_level,
3295 "-- Videcore suspend state: %s --",
3296 suspend_state_names[vc_suspend_state + VC_SUSPEND_NUM_OFFSET]);
3297 vchiq_log_warning(vchiq_susp_log_level,
3298 "-- Videcore resume state: %s --",
3299 resume_state_names[vc_resume_state + VC_RESUME_NUM_OFFSET]);
3302 vchiq_log_warning(vchiq_susp_log_level, "Too many active "
3303 "services (%d). Only dumping up to first %d services "
3304 "with non-zero use-count", active_services,
3305 local_max_services);
3307 for (i = 0; i < j; i++) {
3308 vchiq_log_warning(vchiq_susp_log_level,
3309 "----- %c%c%c%c:%d service count %d %s",
3310 VCHIQ_FOURCC_AS_4CHARS(service_data[i].fourcc),
3311 service_data[i].clientid,
3312 service_data[i].use_count,
3313 service_data[i].use_count ? nz : "");
3315 vchiq_log_warning(vchiq_susp_log_level,
3316 "----- VCHIQ use count count %d", peer_count);
3317 vchiq_log_warning(vchiq_susp_log_level,
3318 "--- Overall vchiq instance use count %d", vc_use_count);
3320 vchiq_dump_platform_use_state(state);
3324 vchiq_check_service(VCHIQ_SERVICE_T *service)
3326 VCHIQ_ARM_STATE_T *arm_state;
3327 VCHIQ_STATUS_T ret = VCHIQ_ERROR;
3329 if (!service || !service->state)
3332 vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
3334 arm_state = vchiq_platform_get_arm_state(service->state);
3336 read_lock_bh(&arm_state->susp_res_lock);
3337 if (service->service_use_count)
3338 ret = VCHIQ_SUCCESS;
3339 read_unlock_bh(&arm_state->susp_res_lock);
3341 if (ret == VCHIQ_ERROR) {
3342 vchiq_log_error(vchiq_susp_log_level,
3343 "%s ERROR - %c%c%c%c:%d service count %d, "
3344 "state count %d, videocore suspend state %s", __func__,
3345 VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
3346 service->client_id, service->service_use_count,
3347 arm_state->videocore_use_count,
3348 suspend_state_names[arm_state->vc_suspend_state +
3349 VC_SUSPEND_NUM_OFFSET]);
3350 vchiq_dump_service_use_state(service->state);
3356 /* stub functions */
3357 void vchiq_on_remote_use_active(VCHIQ_STATE_T *state)
3362 void vchiq_platform_conn_state_changed(VCHIQ_STATE_T *state,
3363 VCHIQ_CONNSTATE_T oldstate, VCHIQ_CONNSTATE_T newstate)
3365 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
3367 vchiq_log_info(vchiq_susp_log_level, "%d: %s->%s", state->id,
3368 get_conn_state_name(oldstate), get_conn_state_name(newstate));
3369 if (state->conn_state == VCHIQ_CONNSTATE_CONNECTED) {
3370 write_lock_bh(&arm_state->susp_res_lock);
3371 if (!arm_state->first_connect) {
3372 char threadname[16];
3374 arm_state->first_connect = 1;
3375 write_unlock_bh(&arm_state->susp_res_lock);
3376 snprintf(threadname, sizeof(threadname), "vchiq-keep/%d",
3378 arm_state->ka_thread = kthread_create(
3379 &vchiq_keepalive_thread_func,
3382 if (IS_ERR(arm_state->ka_thread)) {
3383 vchiq_log_error(vchiq_susp_log_level,
3384 "vchiq: FATAL: couldn't create thread %s",
3387 wake_up_process(arm_state->ka_thread);
3390 write_unlock_bh(&arm_state->susp_res_lock);
3394 static int vchiq_probe(struct platform_device *pdev)
3396 struct device_node *fw_node;
3397 struct rpi_firmware *fw;
3400 fw_node = of_parse_phandle(pdev->dev.of_node, "firmware", 0);
3402 dev_err(&pdev->dev, "Missing firmware node\n");
3406 fw = rpi_firmware_get(fw_node);
3407 of_node_put(fw_node);
3409 return -EPROBE_DEFER;
3411 platform_set_drvdata(pdev, fw);
3413 err = vchiq_platform_init(pdev, &g_state);
3415 goto failed_platform_init;
3417 err = alloc_chrdev_region(&vchiq_devid, VCHIQ_MINOR, 1, DEVICE_NAME);
3419 vchiq_log_error(vchiq_arm_log_level,
3420 "Unable to allocate device number");
3421 goto failed_platform_init;
3423 cdev_init(&vchiq_cdev, &vchiq_fops);
3424 vchiq_cdev.owner = THIS_MODULE;
3425 err = cdev_add(&vchiq_cdev, vchiq_devid, 1);
3427 vchiq_log_error(vchiq_arm_log_level,
3428 "Unable to register device");
3429 goto failed_cdev_add;
3432 /* create sysfs entries */
3433 vchiq_class = class_create(THIS_MODULE, DEVICE_NAME);
3434 err = PTR_ERR(vchiq_class);
3435 if (IS_ERR(vchiq_class))
3436 goto failed_class_create;
3438 vchiq_dev = device_create(vchiq_class, NULL,
3439 vchiq_devid, NULL, "vchiq");
3440 err = PTR_ERR(vchiq_dev);
3441 if (IS_ERR(vchiq_dev))
3442 goto failed_device_create;
3444 /* create debugfs entries */
3445 err = vchiq_debugfs_init();
3447 goto failed_debugfs_init;
3449 vchiq_log_info(vchiq_arm_log_level,
3450 "vchiq: initialised - version %d (min %d), device %d.%d",
3451 VCHIQ_VERSION, VCHIQ_VERSION_MIN,
3452 MAJOR(vchiq_devid), MINOR(vchiq_devid));
3456 failed_debugfs_init:
3457 device_destroy(vchiq_class, vchiq_devid);
3458 failed_device_create:
3459 class_destroy(vchiq_class);
3460 failed_class_create:
3461 cdev_del(&vchiq_cdev);
3463 unregister_chrdev_region(vchiq_devid, 1);
3464 failed_platform_init:
3465 vchiq_log_warning(vchiq_arm_log_level, "could not load vchiq");
3469 static int vchiq_remove(struct platform_device *pdev)
3471 vchiq_debugfs_deinit();
3472 device_destroy(vchiq_class, vchiq_devid);
3473 class_destroy(vchiq_class);
3474 cdev_del(&vchiq_cdev);
3475 unregister_chrdev_region(vchiq_devid, 1);
3480 static const struct of_device_id vchiq_of_match[] = {
3481 { .compatible = "brcm,bcm2835-vchiq", },
3484 MODULE_DEVICE_TABLE(of, vchiq_of_match);
3486 static struct platform_driver vchiq_driver = {
3488 .name = "bcm2835_vchiq",
3489 .of_match_table = vchiq_of_match,
3491 .probe = vchiq_probe,
3492 .remove = vchiq_remove,
3494 module_platform_driver(vchiq_driver);
3496 MODULE_LICENSE("Dual BSD/GPL");
3497 MODULE_DESCRIPTION("Videocore VCHIQ driver");
3498 MODULE_AUTHOR("Broadcom Corporation");