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/types.h>
38 #include <linux/errno.h>
39 #include <linux/cdev.h>
41 #include <linux/device.h>
43 #include <linux/highmem.h>
44 #include <linux/pagemap.h>
45 #include <linux/bug.h>
46 #include <linux/semaphore.h>
47 #include <linux/list.h>
49 #include <linux/platform_device.h>
50 #include <soc/bcm2835/raspberrypi-firmware.h>
52 #include "vchiq_core.h"
53 #include "vchiq_ioctl.h"
54 #include "vchiq_arm.h"
55 #include "vchiq_debugfs.h"
56 #include "vchiq_killable.h"
58 #define DEVICE_NAME "vchiq"
60 /* Override the default prefix, which would be vchiq_arm (from the filename) */
61 #undef MODULE_PARAM_PREFIX
62 #define MODULE_PARAM_PREFIX DEVICE_NAME "."
66 /* Some per-instance constants */
67 #define MAX_COMPLETIONS 16
68 #define MAX_SERVICES 64
69 #define MAX_ELEMENTS 8
70 #define MSG_QUEUE_SIZE 64
72 #define KEEPALIVE_VER 1
73 #define KEEPALIVE_VER_MIN KEEPALIVE_VER
75 /* Run time control of log level, based on KERN_XXX level. */
76 int vchiq_arm_log_level = VCHIQ_LOG_DEFAULT;
77 int vchiq_susp_log_level = VCHIQ_LOG_ERROR;
79 #define SUSPEND_TIMER_TIMEOUT_MS 100
80 #define SUSPEND_RETRY_TIMER_TIMEOUT_MS 1000
82 #define VC_SUSPEND_NUM_OFFSET 3 /* number of values before idle which are -ve */
83 static const char *const suspend_state_names[] = {
84 "VC_SUSPEND_FORCE_CANCELED",
85 "VC_SUSPEND_REJECTED",
88 "VC_SUSPEND_REQUESTED",
89 "VC_SUSPEND_IN_PROGRESS",
90 "VC_SUSPEND_SUSPENDED"
92 #define VC_RESUME_NUM_OFFSET 1 /* number of values before idle which are -ve */
93 static const char *const resume_state_names[] = {
96 "VC_RESUME_REQUESTED",
97 "VC_RESUME_IN_PROGRESS",
100 /* The number of times we allow force suspend to timeout before actually
101 ** _forcing_ suspend. This is to cater for SW which fails to release vchiq
102 ** correctly - we don't want to prevent ARM suspend indefinitely in this case.
104 #define FORCE_SUSPEND_FAIL_MAX 8
106 /* The time in ms allowed for videocore to go idle when force suspend has been
108 #define FORCE_SUSPEND_TIMEOUT_MS 200
111 static void suspend_timer_callback(unsigned long context);
114 typedef struct user_service_struct {
115 VCHIQ_SERVICE_T *service;
117 VCHIQ_INSTANCE_T instance;
119 char dequeue_pending;
121 int message_available_pos;
124 struct semaphore insert_event;
125 struct semaphore remove_event;
126 struct semaphore close_event;
127 VCHIQ_HEADER_T * msg_queue[MSG_QUEUE_SIZE];
130 struct bulk_waiter_node {
131 struct bulk_waiter bulk_waiter;
133 struct list_head list;
136 struct vchiq_instance_struct {
137 VCHIQ_STATE_T *state;
138 VCHIQ_COMPLETION_DATA_T completions[MAX_COMPLETIONS];
139 int completion_insert;
140 int completion_remove;
141 struct semaphore insert_event;
142 struct semaphore remove_event;
143 struct mutex completion_mutex;
149 int use_close_delivered;
152 struct list_head bulk_waiter_list;
153 struct mutex bulk_waiter_list_mutex;
155 VCHIQ_DEBUGFS_NODE_T debugfs_node;
158 typedef struct dump_context_struct {
165 static struct cdev vchiq_cdev;
166 static dev_t vchiq_devid;
167 static VCHIQ_STATE_T g_state;
168 static struct class *vchiq_class;
169 static struct device *vchiq_dev;
170 static DEFINE_SPINLOCK(msg_queue_spinlock);
172 static const char *const ioctl_names[] = {
178 "QUEUE_BULK_TRANSMIT",
179 "QUEUE_BULK_RECEIVE",
187 "SET_SERVICE_OPTION",
193 vchiq_static_assert(ARRAY_SIZE(ioctl_names) ==
194 (VCHIQ_IOC_MAX + 1));
197 dump_phys_mem(void *virt_addr, u32 num_bytes);
199 /****************************************************************************
203 ***************************************************************************/
205 static VCHIQ_STATUS_T
206 add_completion(VCHIQ_INSTANCE_T instance, VCHIQ_REASON_T reason,
207 VCHIQ_HEADER_T *header, USER_SERVICE_T *user_service,
210 VCHIQ_COMPLETION_DATA_T *completion;
211 DEBUG_INITIALISE(g_state.local)
213 while (instance->completion_insert ==
214 (instance->completion_remove + MAX_COMPLETIONS)) {
215 /* Out of space - wait for the client */
216 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
217 vchiq_log_trace(vchiq_arm_log_level,
218 "add_completion - completion queue full");
219 DEBUG_COUNT(COMPLETION_QUEUE_FULL_COUNT);
220 if (down_interruptible(&instance->remove_event) != 0) {
221 vchiq_log_info(vchiq_arm_log_level,
222 "service_callback interrupted");
224 } else if (instance->closing) {
225 vchiq_log_info(vchiq_arm_log_level,
226 "service_callback closing");
229 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
233 &instance->completions[instance->completion_insert &
234 (MAX_COMPLETIONS - 1)];
236 completion->header = header;
237 completion->reason = reason;
238 /* N.B. service_userdata is updated while processing AWAIT_COMPLETION */
239 completion->service_userdata = user_service->service;
240 completion->bulk_userdata = bulk_userdata;
242 if (reason == VCHIQ_SERVICE_CLOSED) {
243 /* Take an extra reference, to be held until
244 this CLOSED notification is delivered. */
245 lock_service(user_service->service);
246 if (instance->use_close_delivered)
247 user_service->close_pending = 1;
250 /* A write barrier is needed here to ensure that the entire completion
251 record is written out before the insert point. */
254 if (reason == VCHIQ_MESSAGE_AVAILABLE)
255 user_service->message_available_pos =
256 instance->completion_insert;
257 instance->completion_insert++;
259 up(&instance->insert_event);
261 return VCHIQ_SUCCESS;
264 /****************************************************************************
268 ***************************************************************************/
270 static VCHIQ_STATUS_T
271 service_callback(VCHIQ_REASON_T reason, VCHIQ_HEADER_T *header,
272 VCHIQ_SERVICE_HANDLE_T handle, void *bulk_userdata)
274 /* How do we ensure the callback goes to the right client?
275 ** The service_user data points to a USER_SERVICE_T record containing
276 ** the original callback and the user state structure, which contains a
277 ** circular buffer for completion records.
279 USER_SERVICE_T *user_service;
280 VCHIQ_SERVICE_T *service;
281 VCHIQ_INSTANCE_T instance;
282 bool skip_completion = false;
283 DEBUG_INITIALISE(g_state.local)
285 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
287 service = handle_to_service(handle);
289 user_service = (USER_SERVICE_T *)service->base.userdata;
290 instance = user_service->instance;
292 if (!instance || instance->closing)
293 return VCHIQ_SUCCESS;
295 vchiq_log_trace(vchiq_arm_log_level,
296 "service_callback - service %lx(%d,%p), reason %d, header %lx, "
297 "instance %lx, bulk_userdata %lx",
298 (unsigned long)user_service,
299 service->localport, user_service->userdata,
300 reason, (unsigned long)header,
301 (unsigned long)instance, (unsigned long)bulk_userdata);
303 if (header && user_service->is_vchi) {
304 spin_lock(&msg_queue_spinlock);
305 while (user_service->msg_insert ==
306 (user_service->msg_remove + MSG_QUEUE_SIZE)) {
307 spin_unlock(&msg_queue_spinlock);
308 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
309 DEBUG_COUNT(MSG_QUEUE_FULL_COUNT);
310 vchiq_log_trace(vchiq_arm_log_level,
311 "service_callback - msg queue full");
312 /* If there is no MESSAGE_AVAILABLE in the completion
315 if ((user_service->message_available_pos -
316 instance->completion_remove) < 0) {
317 VCHIQ_STATUS_T status;
318 vchiq_log_info(vchiq_arm_log_level,
319 "Inserting extra MESSAGE_AVAILABLE");
320 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
321 status = add_completion(instance, reason,
322 NULL, user_service, bulk_userdata);
323 if (status != VCHIQ_SUCCESS) {
324 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
329 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
330 if (down_interruptible(&user_service->remove_event)
332 vchiq_log_info(vchiq_arm_log_level,
333 "service_callback interrupted");
334 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
336 } else if (instance->closing) {
337 vchiq_log_info(vchiq_arm_log_level,
338 "service_callback closing");
339 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
342 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
343 spin_lock(&msg_queue_spinlock);
346 user_service->msg_queue[user_service->msg_insert &
347 (MSG_QUEUE_SIZE - 1)] = header;
348 user_service->msg_insert++;
350 /* If there is a thread waiting in DEQUEUE_MESSAGE, or if
351 ** there is a MESSAGE_AVAILABLE in the completion queue then
352 ** bypass the completion queue.
354 if (((user_service->message_available_pos -
355 instance->completion_remove) >= 0) ||
356 user_service->dequeue_pending) {
357 user_service->dequeue_pending = 0;
358 skip_completion = true;
361 spin_unlock(&msg_queue_spinlock);
362 up(&user_service->insert_event);
366 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
369 return VCHIQ_SUCCESS;
371 return add_completion(instance, reason, header, user_service,
375 /****************************************************************************
379 ***************************************************************************/
381 user_service_free(void *userdata)
386 /****************************************************************************
390 ***************************************************************************/
391 static void close_delivered(USER_SERVICE_T *user_service)
393 vchiq_log_info(vchiq_arm_log_level,
394 "close_delivered(handle=%x)",
395 user_service->service->handle);
397 if (user_service->close_pending) {
398 /* Allow the underlying service to be culled */
399 unlock_service(user_service->service);
401 /* Wake the user-thread blocked in close_ or remove_service */
402 up(&user_service->close_event);
404 user_service->close_pending = 0;
408 struct vchiq_io_copy_callback_context {
409 VCHIQ_ELEMENT_T *current_element;
410 size_t current_element_offset;
411 unsigned long elements_to_go;
412 size_t current_offset;
416 vchiq_ioc_copy_element_data(
423 size_t bytes_this_round;
424 struct vchiq_io_copy_callback_context *copy_context =
425 (struct vchiq_io_copy_callback_context *)context;
427 if (offset != copy_context->current_offset)
430 if (!copy_context->elements_to_go)
434 * Complex logic here to handle the case of 0 size elements
435 * in the middle of the array of elements.
437 * Need to skip over these 0 size elements.
440 bytes_this_round = min(copy_context->current_element->size -
441 copy_context->current_element_offset,
444 if (bytes_this_round)
447 copy_context->elements_to_go--;
448 copy_context->current_element++;
449 copy_context->current_element_offset = 0;
451 if (!copy_context->elements_to_go)
455 res = copy_from_user(dest,
456 copy_context->current_element->data +
457 copy_context->current_element_offset,
463 copy_context->current_element_offset += bytes_this_round;
464 copy_context->current_offset += bytes_this_round;
467 * Check if done with current element, and if so advance to the next.
469 if (copy_context->current_element_offset ==
470 copy_context->current_element->size) {
471 copy_context->elements_to_go--;
472 copy_context->current_element++;
473 copy_context->current_element_offset = 0;
476 return bytes_this_round;
479 /**************************************************************************
481 * vchiq_ioc_queue_message
483 **************************************************************************/
484 static VCHIQ_STATUS_T
485 vchiq_ioc_queue_message(VCHIQ_SERVICE_HANDLE_T handle,
486 VCHIQ_ELEMENT_T *elements,
489 struct vchiq_io_copy_callback_context context;
491 size_t total_size = 0;
493 context.current_element = elements;
494 context.current_element_offset = 0;
495 context.elements_to_go = count;
496 context.current_offset = 0;
498 for (i = 0; i < count; i++) {
499 if (!elements[i].data && elements[i].size != 0)
502 total_size += elements[i].size;
505 return vchiq_queue_message(handle, vchiq_ioc_copy_element_data,
506 &context, total_size);
509 /****************************************************************************
513 ***************************************************************************/
515 vchiq_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
517 VCHIQ_INSTANCE_T instance = file->private_data;
518 VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
519 VCHIQ_SERVICE_T *service = NULL;
522 DEBUG_INITIALISE(g_state.local)
524 vchiq_log_trace(vchiq_arm_log_level,
525 "vchiq_ioctl - instance %pK, cmd %s, arg %lx",
527 ((_IOC_TYPE(cmd) == VCHIQ_IOC_MAGIC) &&
528 (_IOC_NR(cmd) <= VCHIQ_IOC_MAX)) ?
529 ioctl_names[_IOC_NR(cmd)] : "<invalid>", arg);
532 case VCHIQ_IOC_SHUTDOWN:
533 if (!instance->connected)
536 /* Remove all services */
538 while ((service = next_service_by_instance(instance->state,
539 instance, &i)) != NULL) {
540 status = vchiq_remove_service(service->handle);
541 unlock_service(service);
542 if (status != VCHIQ_SUCCESS)
547 if (status == VCHIQ_SUCCESS) {
548 /* Wake the completion thread and ask it to exit */
549 instance->closing = 1;
550 up(&instance->insert_event);
555 case VCHIQ_IOC_CONNECT:
556 if (instance->connected) {
560 rc = mutex_lock_killable(&instance->state->mutex);
562 vchiq_log_error(vchiq_arm_log_level,
563 "vchiq: connect: could not lock mutex for "
565 instance->state->id, rc);
569 status = vchiq_connect_internal(instance->state, instance);
570 mutex_unlock(&instance->state->mutex);
572 if (status == VCHIQ_SUCCESS)
573 instance->connected = 1;
575 vchiq_log_error(vchiq_arm_log_level,
576 "vchiq: could not connect: %d", status);
579 case VCHIQ_IOC_CREATE_SERVICE: {
580 VCHIQ_CREATE_SERVICE_T args;
581 USER_SERVICE_T *user_service = NULL;
586 (&args, (const void __user *)arg,
587 sizeof(args)) != 0) {
592 user_service = kmalloc(sizeof(USER_SERVICE_T), GFP_KERNEL);
599 if (!instance->connected) {
604 srvstate = VCHIQ_SRVSTATE_OPENING;
607 instance->connected ?
608 VCHIQ_SRVSTATE_LISTENING :
609 VCHIQ_SRVSTATE_HIDDEN;
612 userdata = args.params.userdata;
613 args.params.callback = service_callback;
614 args.params.userdata = user_service;
615 service = vchiq_add_service_internal(
617 &args.params, srvstate,
618 instance, user_service_free);
620 if (service != NULL) {
621 user_service->service = service;
622 user_service->userdata = userdata;
623 user_service->instance = instance;
624 user_service->is_vchi = (args.is_vchi != 0);
625 user_service->dequeue_pending = 0;
626 user_service->close_pending = 0;
627 user_service->message_available_pos =
628 instance->completion_remove - 1;
629 user_service->msg_insert = 0;
630 user_service->msg_remove = 0;
631 sema_init(&user_service->insert_event, 0);
632 sema_init(&user_service->remove_event, 0);
633 sema_init(&user_service->close_event, 0);
636 status = vchiq_open_service_internal
637 (service, instance->pid);
638 if (status != VCHIQ_SUCCESS) {
639 vchiq_remove_service(service->handle);
641 ret = (status == VCHIQ_RETRY) ?
647 if (copy_to_user((void __user *)
648 &(((VCHIQ_CREATE_SERVICE_T __user *)
650 (const void *)&service->handle,
651 sizeof(service->handle)) != 0) {
653 vchiq_remove_service(service->handle);
663 case VCHIQ_IOC_CLOSE_SERVICE: {
664 VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
666 service = find_service_for_instance(instance, handle);
667 if (service != NULL) {
668 USER_SERVICE_T *user_service =
669 (USER_SERVICE_T *)service->base.userdata;
670 /* close_pending is false on first entry, and when the
671 wait in vchiq_close_service has been interrupted. */
672 if (!user_service->close_pending) {
673 status = vchiq_close_service(service->handle);
674 if (status != VCHIQ_SUCCESS)
678 /* close_pending is true once the underlying service
679 has been closed until the client library calls the
680 CLOSE_DELIVERED ioctl, signalling close_event. */
681 if (user_service->close_pending &&
682 down_interruptible(&user_service->close_event))
683 status = VCHIQ_RETRY;
689 case VCHIQ_IOC_REMOVE_SERVICE: {
690 VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
692 service = find_service_for_instance(instance, handle);
693 if (service != NULL) {
694 USER_SERVICE_T *user_service =
695 (USER_SERVICE_T *)service->base.userdata;
696 /* close_pending is false on first entry, and when the
697 wait in vchiq_close_service has been interrupted. */
698 if (!user_service->close_pending) {
699 status = vchiq_remove_service(service->handle);
700 if (status != VCHIQ_SUCCESS)
704 /* close_pending is true once the underlying service
705 has been closed until the client library calls the
706 CLOSE_DELIVERED ioctl, signalling close_event. */
707 if (user_service->close_pending &&
708 down_interruptible(&user_service->close_event))
709 status = VCHIQ_RETRY;
715 case VCHIQ_IOC_USE_SERVICE:
716 case VCHIQ_IOC_RELEASE_SERVICE: {
717 VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
719 service = find_service_for_instance(instance, handle);
720 if (service != NULL) {
721 status = (cmd == VCHIQ_IOC_USE_SERVICE) ?
722 vchiq_use_service_internal(service) :
723 vchiq_release_service_internal(service);
724 if (status != VCHIQ_SUCCESS) {
725 vchiq_log_error(vchiq_susp_log_level,
726 "%s: cmd %s returned error %d for "
727 "service %c%c%c%c:%03d",
729 (cmd == VCHIQ_IOC_USE_SERVICE) ?
730 "VCHIQ_IOC_USE_SERVICE" :
731 "VCHIQ_IOC_RELEASE_SERVICE",
733 VCHIQ_FOURCC_AS_4CHARS(
734 service->base.fourcc),
742 case VCHIQ_IOC_QUEUE_MESSAGE: {
743 VCHIQ_QUEUE_MESSAGE_T args;
745 (&args, (const void __user *)arg,
746 sizeof(args)) != 0) {
751 service = find_service_for_instance(instance, args.handle);
753 if ((service != NULL) && (args.count <= MAX_ELEMENTS)) {
754 /* Copy elements into kernel space */
755 VCHIQ_ELEMENT_T elements[MAX_ELEMENTS];
756 if (copy_from_user(elements, args.elements,
757 args.count * sizeof(VCHIQ_ELEMENT_T)) == 0)
758 status = vchiq_ioc_queue_message
760 elements, args.count);
768 case VCHIQ_IOC_QUEUE_BULK_TRANSMIT:
769 case VCHIQ_IOC_QUEUE_BULK_RECEIVE: {
770 VCHIQ_QUEUE_BULK_TRANSFER_T args;
771 struct bulk_waiter_node *waiter = NULL;
772 VCHIQ_BULK_DIR_T dir =
773 (cmd == VCHIQ_IOC_QUEUE_BULK_TRANSMIT) ?
774 VCHIQ_BULK_TRANSMIT : VCHIQ_BULK_RECEIVE;
777 (&args, (const void __user *)arg,
778 sizeof(args)) != 0) {
783 service = find_service_for_instance(instance, args.handle);
789 if (args.mode == VCHIQ_BULK_MODE_BLOCKING) {
790 waiter = kzalloc(sizeof(struct bulk_waiter_node),
796 args.userdata = &waiter->bulk_waiter;
797 } else if (args.mode == VCHIQ_BULK_MODE_WAITING) {
798 struct list_head *pos;
799 mutex_lock(&instance->bulk_waiter_list_mutex);
800 list_for_each(pos, &instance->bulk_waiter_list) {
801 if (list_entry(pos, struct bulk_waiter_node,
802 list)->pid == current->pid) {
803 waiter = list_entry(pos,
804 struct bulk_waiter_node,
811 mutex_unlock(&instance->bulk_waiter_list_mutex);
813 vchiq_log_error(vchiq_arm_log_level,
814 "no bulk_waiter found for pid %d",
819 vchiq_log_info(vchiq_arm_log_level,
820 "found bulk_waiter %pK for pid %d", waiter,
822 args.userdata = &waiter->bulk_waiter;
824 status = vchiq_bulk_transfer
826 VCHI_MEM_HANDLE_INVALID,
827 args.data, args.size,
828 args.userdata, args.mode,
832 if ((status != VCHIQ_RETRY) || fatal_signal_pending(current) ||
833 !waiter->bulk_waiter.bulk) {
834 if (waiter->bulk_waiter.bulk) {
835 /* Cancel the signal when the transfer
837 spin_lock(&bulk_waiter_spinlock);
838 waiter->bulk_waiter.bulk->userdata = NULL;
839 spin_unlock(&bulk_waiter_spinlock);
843 const VCHIQ_BULK_MODE_T mode_waiting =
844 VCHIQ_BULK_MODE_WAITING;
845 waiter->pid = current->pid;
846 mutex_lock(&instance->bulk_waiter_list_mutex);
847 list_add(&waiter->list, &instance->bulk_waiter_list);
848 mutex_unlock(&instance->bulk_waiter_list_mutex);
849 vchiq_log_info(vchiq_arm_log_level,
850 "saved bulk_waiter %pK for pid %d",
851 waiter, current->pid);
853 if (copy_to_user((void __user *)
854 &(((VCHIQ_QUEUE_BULK_TRANSFER_T __user *)
856 (const void *)&mode_waiting,
857 sizeof(mode_waiting)) != 0)
862 case VCHIQ_IOC_AWAIT_COMPLETION: {
863 VCHIQ_AWAIT_COMPLETION_T args;
865 DEBUG_TRACE(AWAIT_COMPLETION_LINE);
866 if (!instance->connected) {
871 if (copy_from_user(&args, (const void __user *)arg,
872 sizeof(args)) != 0) {
877 mutex_lock(&instance->completion_mutex);
879 DEBUG_TRACE(AWAIT_COMPLETION_LINE);
880 while ((instance->completion_remove ==
881 instance->completion_insert)
882 && !instance->closing) {
884 DEBUG_TRACE(AWAIT_COMPLETION_LINE);
885 mutex_unlock(&instance->completion_mutex);
886 rc = down_interruptible(&instance->insert_event);
887 mutex_lock(&instance->completion_mutex);
889 DEBUG_TRACE(AWAIT_COMPLETION_LINE);
890 vchiq_log_info(vchiq_arm_log_level,
891 "AWAIT_COMPLETION interrupted");
896 DEBUG_TRACE(AWAIT_COMPLETION_LINE);
898 /* A read memory barrier is needed to stop prefetch of a stale
904 int msgbufcount = args.msgbufcount;
905 for (ret = 0; ret < args.count; ret++) {
906 VCHIQ_COMPLETION_DATA_T *completion;
907 VCHIQ_SERVICE_T *service;
908 USER_SERVICE_T *user_service;
909 VCHIQ_HEADER_T *header;
910 if (instance->completion_remove ==
911 instance->completion_insert)
913 completion = &instance->completions[
914 instance->completion_remove &
915 (MAX_COMPLETIONS - 1)];
917 service = completion->service_userdata;
918 user_service = service->base.userdata;
919 completion->service_userdata =
920 user_service->userdata;
922 header = completion->header;
927 msglen = header->size +
928 sizeof(VCHIQ_HEADER_T);
929 /* This must be a VCHIQ-style service */
930 if (args.msgbufsize < msglen) {
933 "header %pK: msgbufsize %x < msglen %x",
934 header, args.msgbufsize,
936 WARN(1, "invalid message "
942 if (msgbufcount <= 0)
943 /* Stall here for lack of a
944 ** buffer for the message. */
946 /* Get the pointer from user space */
948 if (copy_from_user(&msgbuf,
949 (const void __user *)
950 &args.msgbufs[msgbufcount],
951 sizeof(msgbuf)) != 0) {
957 /* Copy the message to user space */
958 if (copy_to_user(msgbuf, header,
965 /* Now it has been copied, the message
966 ** can be released. */
967 vchiq_release_message(service->handle,
970 /* The completion must point to the
972 completion->header = msgbuf;
975 if ((completion->reason ==
976 VCHIQ_SERVICE_CLOSED) &&
977 !instance->use_close_delivered)
978 unlock_service(service);
980 if (copy_to_user((void __user *)(
982 ret * sizeof(VCHIQ_COMPLETION_DATA_T)),
984 sizeof(VCHIQ_COMPLETION_DATA_T)) != 0) {
990 instance->completion_remove++;
993 if (msgbufcount != args.msgbufcount) {
994 if (copy_to_user((void __user *)
995 &((VCHIQ_AWAIT_COMPLETION_T *)arg)->
998 sizeof(msgbufcount)) != 0) {
1005 up(&instance->remove_event);
1006 mutex_unlock(&instance->completion_mutex);
1007 DEBUG_TRACE(AWAIT_COMPLETION_LINE);
1010 case VCHIQ_IOC_DEQUEUE_MESSAGE: {
1011 VCHIQ_DEQUEUE_MESSAGE_T args;
1012 USER_SERVICE_T *user_service;
1013 VCHIQ_HEADER_T *header;
1015 DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
1017 (&args, (const void __user *)arg,
1018 sizeof(args)) != 0) {
1022 service = find_service_for_instance(instance, args.handle);
1027 user_service = (USER_SERVICE_T *)service->base.userdata;
1028 if (user_service->is_vchi == 0) {
1033 spin_lock(&msg_queue_spinlock);
1034 if (user_service->msg_remove == user_service->msg_insert) {
1035 if (!args.blocking) {
1036 spin_unlock(&msg_queue_spinlock);
1037 DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
1041 user_service->dequeue_pending = 1;
1043 spin_unlock(&msg_queue_spinlock);
1044 DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
1045 if (down_interruptible(
1046 &user_service->insert_event) != 0) {
1047 vchiq_log_info(vchiq_arm_log_level,
1048 "DEQUEUE_MESSAGE interrupted");
1052 spin_lock(&msg_queue_spinlock);
1053 } while (user_service->msg_remove ==
1054 user_service->msg_insert);
1060 BUG_ON((int)(user_service->msg_insert -
1061 user_service->msg_remove) < 0);
1063 header = user_service->msg_queue[user_service->msg_remove &
1064 (MSG_QUEUE_SIZE - 1)];
1065 user_service->msg_remove++;
1066 spin_unlock(&msg_queue_spinlock);
1068 up(&user_service->remove_event);
1071 else if (header->size <= args.bufsize) {
1072 /* Copy to user space if msgbuf is not NULL */
1073 if ((args.buf == NULL) ||
1074 (copy_to_user((void __user *)args.buf,
1076 header->size) == 0)) {
1078 vchiq_release_message(
1084 vchiq_log_error(vchiq_arm_log_level,
1085 "header %pK: bufsize %x < size %x",
1086 header, args.bufsize, header->size);
1087 WARN(1, "invalid size\n");
1090 DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
1093 case VCHIQ_IOC_GET_CLIENT_ID: {
1094 VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
1096 ret = vchiq_get_client_id(handle);
1099 case VCHIQ_IOC_GET_CONFIG: {
1100 VCHIQ_GET_CONFIG_T args;
1101 VCHIQ_CONFIG_T config;
1103 if (copy_from_user(&args, (const void __user *)arg,
1104 sizeof(args)) != 0) {
1108 if (args.config_size > sizeof(config)) {
1112 status = vchiq_get_config(instance, args.config_size, &config);
1113 if (status == VCHIQ_SUCCESS) {
1114 if (copy_to_user((void __user *)args.pconfig,
1115 &config, args.config_size) != 0) {
1122 case VCHIQ_IOC_SET_SERVICE_OPTION: {
1123 VCHIQ_SET_SERVICE_OPTION_T args;
1126 &args, (const void __user *)arg,
1127 sizeof(args)) != 0) {
1132 service = find_service_for_instance(instance, args.handle);
1138 status = vchiq_set_service_option(
1139 args.handle, args.option, args.value);
1142 case VCHIQ_IOC_DUMP_PHYS_MEM: {
1143 VCHIQ_DUMP_MEM_T args;
1146 (&args, (const void __user *)arg,
1147 sizeof(args)) != 0) {
1151 dump_phys_mem(args.virt_addr, args.num_bytes);
1154 case VCHIQ_IOC_LIB_VERSION: {
1155 unsigned int lib_version = (unsigned int)arg;
1157 if (lib_version < VCHIQ_VERSION_MIN)
1159 else if (lib_version >= VCHIQ_VERSION_CLOSE_DELIVERED)
1160 instance->use_close_delivered = 1;
1163 case VCHIQ_IOC_CLOSE_DELIVERED: {
1164 VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
1166 service = find_closed_service_for_instance(instance, handle);
1167 if (service != NULL) {
1168 USER_SERVICE_T *user_service =
1169 (USER_SERVICE_T *)service->base.userdata;
1170 close_delivered(user_service);
1182 unlock_service(service);
1185 if (status == VCHIQ_ERROR)
1187 else if (status == VCHIQ_RETRY)
1191 if ((status == VCHIQ_SUCCESS) && (ret < 0) && (ret != -EINTR) &&
1192 (ret != -EWOULDBLOCK))
1193 vchiq_log_info(vchiq_arm_log_level,
1194 " ioctl instance %lx, cmd %s -> status %d, %ld",
1195 (unsigned long)instance,
1196 (_IOC_NR(cmd) <= VCHIQ_IOC_MAX) ?
1197 ioctl_names[_IOC_NR(cmd)] :
1201 vchiq_log_trace(vchiq_arm_log_level,
1202 " ioctl instance %lx, cmd %s -> status %d, %ld",
1203 (unsigned long)instance,
1204 (_IOC_NR(cmd) <= VCHIQ_IOC_MAX) ?
1205 ioctl_names[_IOC_NR(cmd)] :
1212 /****************************************************************************
1216 ***************************************************************************/
1219 vchiq_open(struct inode *inode, struct file *file)
1221 int dev = iminor(inode) & 0x0f;
1222 vchiq_log_info(vchiq_arm_log_level, "vchiq_open");
1226 VCHIQ_STATE_T *state = vchiq_get_state();
1227 VCHIQ_INSTANCE_T instance;
1230 vchiq_log_error(vchiq_arm_log_level,
1231 "vchiq has no connection to VideoCore");
1235 instance = kzalloc(sizeof(*instance), GFP_KERNEL);
1239 instance->state = state;
1240 instance->pid = current->tgid;
1242 ret = vchiq_debugfs_add_instance(instance);
1248 sema_init(&instance->insert_event, 0);
1249 sema_init(&instance->remove_event, 0);
1250 mutex_init(&instance->completion_mutex);
1251 mutex_init(&instance->bulk_waiter_list_mutex);
1252 INIT_LIST_HEAD(&instance->bulk_waiter_list);
1254 file->private_data = instance;
1258 vchiq_log_error(vchiq_arm_log_level,
1259 "Unknown minor device: %d", dev);
1266 /****************************************************************************
1270 ***************************************************************************/
1273 vchiq_release(struct inode *inode, struct file *file)
1275 int dev = iminor(inode) & 0x0f;
1279 VCHIQ_INSTANCE_T instance = file->private_data;
1280 VCHIQ_STATE_T *state = vchiq_get_state();
1281 VCHIQ_SERVICE_T *service;
1284 vchiq_log_info(vchiq_arm_log_level,
1285 "vchiq_release: instance=%lx",
1286 (unsigned long)instance);
1293 /* Ensure videocore is awake to allow termination. */
1294 vchiq_use_internal(instance->state, NULL,
1297 mutex_lock(&instance->completion_mutex);
1299 /* Wake the completion thread and ask it to exit */
1300 instance->closing = 1;
1301 up(&instance->insert_event);
1303 mutex_unlock(&instance->completion_mutex);
1305 /* Wake the slot handler if the completion queue is full. */
1306 up(&instance->remove_event);
1308 /* Mark all services for termination... */
1310 while ((service = next_service_by_instance(state, instance,
1312 USER_SERVICE_T *user_service = service->base.userdata;
1314 /* Wake the slot handler if the msg queue is full. */
1315 up(&user_service->remove_event);
1317 vchiq_terminate_service_internal(service);
1318 unlock_service(service);
1321 /* ...and wait for them to die */
1323 while ((service = next_service_by_instance(state, instance, &i))
1325 USER_SERVICE_T *user_service = service->base.userdata;
1327 down(&service->remove_event);
1329 BUG_ON(service->srvstate != VCHIQ_SRVSTATE_FREE);
1331 spin_lock(&msg_queue_spinlock);
1333 while (user_service->msg_remove !=
1334 user_service->msg_insert) {
1335 VCHIQ_HEADER_T *header = user_service->
1336 msg_queue[user_service->msg_remove &
1337 (MSG_QUEUE_SIZE - 1)];
1338 user_service->msg_remove++;
1339 spin_unlock(&msg_queue_spinlock);
1342 vchiq_release_message(
1345 spin_lock(&msg_queue_spinlock);
1348 spin_unlock(&msg_queue_spinlock);
1350 unlock_service(service);
1353 /* Release any closed services */
1354 while (instance->completion_remove !=
1355 instance->completion_insert) {
1356 VCHIQ_COMPLETION_DATA_T *completion;
1357 VCHIQ_SERVICE_T *service;
1358 completion = &instance->completions[
1359 instance->completion_remove &
1360 (MAX_COMPLETIONS - 1)];
1361 service = completion->service_userdata;
1362 if (completion->reason == VCHIQ_SERVICE_CLOSED)
1364 USER_SERVICE_T *user_service =
1365 service->base.userdata;
1367 /* Wake any blocked user-thread */
1368 if (instance->use_close_delivered)
1369 up(&user_service->close_event);
1370 unlock_service(service);
1372 instance->completion_remove++;
1375 /* Release the PEER service count. */
1376 vchiq_release_internal(instance->state, NULL);
1379 struct list_head *pos, *next;
1380 list_for_each_safe(pos, next,
1381 &instance->bulk_waiter_list) {
1382 struct bulk_waiter_node *waiter;
1383 waiter = list_entry(pos,
1384 struct bulk_waiter_node,
1387 vchiq_log_info(vchiq_arm_log_level,
1388 "bulk_waiter - cleaned up %pK for pid %d",
1389 waiter, waiter->pid);
1394 vchiq_debugfs_remove_instance(instance);
1397 file->private_data = NULL;
1401 vchiq_log_error(vchiq_arm_log_level,
1402 "Unknown minor device: %d", dev);
1410 /****************************************************************************
1414 ***************************************************************************/
1417 vchiq_dump(void *dump_context, const char *str, int len)
1419 DUMP_CONTEXT_T *context = (DUMP_CONTEXT_T *)dump_context;
1421 if (context->actual < context->space) {
1423 if (context->offset > 0) {
1424 int skip_bytes = min(len, (int)context->offset);
1427 context->offset -= skip_bytes;
1428 if (context->offset > 0)
1431 copy_bytes = min(len, (int)(context->space - context->actual));
1432 if (copy_bytes == 0)
1434 if (copy_to_user(context->buf + context->actual, str,
1436 context->actual = -EFAULT;
1437 context->actual += copy_bytes;
1440 /* If tne terminating NUL is included in the length, then it
1441 ** marks the end of a line and should be replaced with a
1442 ** carriage return. */
1443 if ((len == 0) && (str[copy_bytes - 1] == '\0')) {
1445 if (copy_to_user(context->buf + context->actual - 1,
1447 context->actual = -EFAULT;
1452 /****************************************************************************
1454 * vchiq_dump_platform_instance_state
1456 ***************************************************************************/
1459 vchiq_dump_platform_instances(void *dump_context)
1461 VCHIQ_STATE_T *state = vchiq_get_state();
1466 /* There is no list of instances, so instead scan all services,
1467 marking those that have been dumped. */
1469 for (i = 0; i < state->unused_service; i++) {
1470 VCHIQ_SERVICE_T *service = state->services[i];
1471 VCHIQ_INSTANCE_T instance;
1473 if (service && (service->base.callback == service_callback)) {
1474 instance = service->instance;
1480 for (i = 0; i < state->unused_service; i++) {
1481 VCHIQ_SERVICE_T *service = state->services[i];
1482 VCHIQ_INSTANCE_T instance;
1484 if (service && (service->base.callback == service_callback)) {
1485 instance = service->instance;
1486 if (instance && !instance->mark) {
1487 len = snprintf(buf, sizeof(buf),
1488 "Instance %pK: pid %d,%s completions %d/%d",
1489 instance, instance->pid,
1490 instance->connected ? " connected, " :
1492 instance->completion_insert -
1493 instance->completion_remove,
1496 vchiq_dump(dump_context, buf, len + 1);
1504 /****************************************************************************
1506 * vchiq_dump_platform_service_state
1508 ***************************************************************************/
1511 vchiq_dump_platform_service_state(void *dump_context, VCHIQ_SERVICE_T *service)
1513 USER_SERVICE_T *user_service = (USER_SERVICE_T *)service->base.userdata;
1517 len = snprintf(buf, sizeof(buf), " instance %pK", service->instance);
1519 if ((service->base.callback == service_callback) &&
1520 user_service->is_vchi) {
1521 len += snprintf(buf + len, sizeof(buf) - len,
1523 user_service->msg_insert - user_service->msg_remove,
1526 if (user_service->dequeue_pending)
1527 len += snprintf(buf + len, sizeof(buf) - len,
1528 " (dequeue pending)");
1531 vchiq_dump(dump_context, buf, len + 1);
1534 /****************************************************************************
1538 ***************************************************************************/
1541 dump_phys_mem(void *virt_addr, u32 num_bytes)
1544 u8 *end_virt_addr = virt_addr + num_bytes;
1551 struct page **pages;
1552 u8 *kmapped_virt_ptr;
1554 /* Align virtAddr and endVirtAddr to 16 byte boundaries. */
1556 virt_addr = (void *)((unsigned long)virt_addr & ~0x0fuL);
1557 end_virt_addr = (void *)(((unsigned long)end_virt_addr + 15uL) &
1560 offset = (int)(long)virt_addr & (PAGE_SIZE - 1);
1561 end_offset = (int)(long)end_virt_addr & (PAGE_SIZE - 1);
1563 num_pages = (offset + num_bytes + PAGE_SIZE - 1) / PAGE_SIZE;
1565 pages = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
1566 if (pages == NULL) {
1567 vchiq_log_error(vchiq_arm_log_level,
1568 "Unable to allocation memory for %d pages\n",
1573 down_read(¤t->mm->mmap_sem);
1574 rc = get_user_pages(
1575 (unsigned long)virt_addr, /* start */
1576 num_pages, /* len */
1578 pages, /* pages (array of page pointers) */
1580 up_read(¤t->mm->mmap_sem);
1586 vchiq_log_error(vchiq_arm_log_level,
1587 "Failed to get user pages: %d\n", rc);
1591 while (offset < end_offset) {
1593 int page_offset = offset % PAGE_SIZE;
1594 page_idx = offset / PAGE_SIZE;
1596 if (page_idx != prev_idx) {
1600 page = pages[page_idx];
1601 kmapped_virt_ptr = kmap(page);
1603 prev_idx = page_idx;
1606 if (vchiq_arm_log_level >= VCHIQ_LOG_TRACE)
1607 vchiq_log_dump_mem("ph",
1608 (u32)(unsigned long)&kmapped_virt_ptr[
1610 &kmapped_virt_ptr[page_offset], 16);
1619 for (page_idx = 0; page_idx < num_pages; page_idx++)
1620 put_page(pages[page_idx]);
1625 /****************************************************************************
1629 ***************************************************************************/
1632 vchiq_read(struct file *file, char __user *buf,
1633 size_t count, loff_t *ppos)
1635 DUMP_CONTEXT_T context;
1638 context.space = count;
1639 context.offset = *ppos;
1641 vchiq_dump_state(&context, &g_state);
1643 *ppos += context.actual;
1645 return context.actual;
1649 vchiq_get_state(void)
1652 if (g_state.remote == NULL)
1653 printk(KERN_ERR "%s: g_state.remote == NULL\n", __func__);
1654 else if (g_state.remote->initialised != 1)
1655 printk(KERN_NOTICE "%s: g_state.remote->initialised != 1 (%d)\n",
1656 __func__, g_state.remote->initialised);
1658 return ((g_state.remote != NULL) &&
1659 (g_state.remote->initialised == 1)) ? &g_state : NULL;
1662 static const struct file_operations
1664 .owner = THIS_MODULE,
1665 .unlocked_ioctl = vchiq_ioctl,
1667 .release = vchiq_release,
1672 * Autosuspend related functionality
1676 vchiq_videocore_wanted(VCHIQ_STATE_T *state)
1678 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
1680 /* autosuspend not supported - always return wanted */
1682 else if (arm_state->blocked_count)
1684 else if (!arm_state->videocore_use_count)
1685 /* usage count zero - check for override unless we're forcing */
1686 if (arm_state->resume_blocked)
1689 return vchiq_platform_videocore_wanted(state);
1691 /* non-zero usage count - videocore still required */
1695 static VCHIQ_STATUS_T
1696 vchiq_keepalive_vchiq_callback(VCHIQ_REASON_T reason,
1697 VCHIQ_HEADER_T *header,
1698 VCHIQ_SERVICE_HANDLE_T service_user,
1701 vchiq_log_error(vchiq_susp_log_level,
1702 "%s callback reason %d", __func__, reason);
1707 vchiq_keepalive_thread_func(void *v)
1709 VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
1710 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
1712 VCHIQ_STATUS_T status;
1713 VCHIQ_INSTANCE_T instance;
1714 VCHIQ_SERVICE_HANDLE_T ka_handle;
1716 VCHIQ_SERVICE_PARAMS_T params = {
1717 .fourcc = VCHIQ_MAKE_FOURCC('K', 'E', 'E', 'P'),
1718 .callback = vchiq_keepalive_vchiq_callback,
1719 .version = KEEPALIVE_VER,
1720 .version_min = KEEPALIVE_VER_MIN
1723 status = vchiq_initialise(&instance);
1724 if (status != VCHIQ_SUCCESS) {
1725 vchiq_log_error(vchiq_susp_log_level,
1726 "%s vchiq_initialise failed %d", __func__, status);
1730 status = vchiq_connect(instance);
1731 if (status != VCHIQ_SUCCESS) {
1732 vchiq_log_error(vchiq_susp_log_level,
1733 "%s vchiq_connect failed %d", __func__, status);
1737 status = vchiq_add_service(instance, ¶ms, &ka_handle);
1738 if (status != VCHIQ_SUCCESS) {
1739 vchiq_log_error(vchiq_susp_log_level,
1740 "%s vchiq_open_service failed %d", __func__, status);
1745 long rc = 0, uc = 0;
1746 if (wait_for_completion_interruptible(&arm_state->ka_evt)
1748 vchiq_log_error(vchiq_susp_log_level,
1749 "%s interrupted", __func__);
1750 flush_signals(current);
1754 /* read and clear counters. Do release_count then use_count to
1755 * prevent getting more releases than uses */
1756 rc = atomic_xchg(&arm_state->ka_release_count, 0);
1757 uc = atomic_xchg(&arm_state->ka_use_count, 0);
1759 /* Call use/release service the requisite number of times.
1760 * Process use before release so use counts don't go negative */
1762 atomic_inc(&arm_state->ka_use_ack_count);
1763 status = vchiq_use_service(ka_handle);
1764 if (status != VCHIQ_SUCCESS) {
1765 vchiq_log_error(vchiq_susp_log_level,
1766 "%s vchiq_use_service error %d",
1771 status = vchiq_release_service(ka_handle);
1772 if (status != VCHIQ_SUCCESS) {
1773 vchiq_log_error(vchiq_susp_log_level,
1774 "%s vchiq_release_service error %d",
1781 vchiq_shutdown(instance);
1789 vchiq_arm_init_state(VCHIQ_STATE_T *state, VCHIQ_ARM_STATE_T *arm_state)
1792 rwlock_init(&arm_state->susp_res_lock);
1794 init_completion(&arm_state->ka_evt);
1795 atomic_set(&arm_state->ka_use_count, 0);
1796 atomic_set(&arm_state->ka_use_ack_count, 0);
1797 atomic_set(&arm_state->ka_release_count, 0);
1799 init_completion(&arm_state->vc_suspend_complete);
1801 init_completion(&arm_state->vc_resume_complete);
1802 /* Initialise to 'done' state. We only want to block on resume
1803 * completion while videocore is suspended. */
1804 set_resume_state(arm_state, VC_RESUME_RESUMED);
1806 init_completion(&arm_state->resume_blocker);
1807 /* Initialise to 'done' state. We only want to block on this
1808 * completion while resume is blocked */
1809 complete_all(&arm_state->resume_blocker);
1811 init_completion(&arm_state->blocked_blocker);
1812 /* Initialise to 'done' state. We only want to block on this
1813 * completion while things are waiting on the resume blocker */
1814 complete_all(&arm_state->blocked_blocker);
1816 arm_state->suspend_timer_timeout = SUSPEND_TIMER_TIMEOUT_MS;
1817 arm_state->suspend_timer_running = 0;
1818 setup_timer(&arm_state->suspend_timer, suspend_timer_callback,
1819 (unsigned long)(state));
1821 arm_state->first_connect = 0;
1824 return VCHIQ_SUCCESS;
1828 ** Functions to modify the state variables;
1829 ** set_suspend_state
1832 ** There are more state variables than we might like, so ensure they remain in
1833 ** step. Suspend and resume state are maintained separately, since most of
1834 ** these state machines can operate independently. However, there are a few
1835 ** states where state transitions in one state machine cause a reset to the
1836 ** other state machine. In addition, there are some completion events which
1837 ** need to occur on state machine reset and end-state(s), so these are also
1838 ** dealt with in these functions.
1840 ** In all states we set the state variable according to the input, but in some
1841 ** cases we perform additional steps outlined below;
1843 ** VC_SUSPEND_IDLE - Initialise the suspend completion at the same time.
1844 ** The suspend completion is completed after any suspend
1845 ** attempt. When we reset the state machine we also reset
1846 ** the completion. This reset occurs when videocore is
1847 ** resumed, and also if we initiate suspend after a suspend
1850 ** VC_SUSPEND_IN_PROGRESS - This state is considered the point of no return for
1851 ** suspend - ie from this point on we must try to suspend
1852 ** before resuming can occur. We therefore also reset the
1853 ** resume state machine to VC_RESUME_IDLE in this state.
1855 ** VC_SUSPEND_SUSPENDED - Suspend has completed successfully. Also call
1856 ** complete_all on the suspend completion to notify
1857 ** anything waiting for suspend to happen.
1859 ** VC_SUSPEND_REJECTED - Videocore rejected suspend. Videocore will also
1860 ** initiate resume, so no need to alter resume state.
1861 ** We call complete_all on the suspend completion to notify
1862 ** of suspend rejection.
1864 ** VC_SUSPEND_FAILED - We failed to initiate videocore suspend. We notify the
1865 ** suspend completion and reset the resume state machine.
1867 ** VC_RESUME_IDLE - Initialise the resume completion at the same time. The
1868 ** resume completion is in it's 'done' state whenever
1869 ** videcore is running. Therfore, the VC_RESUME_IDLE state
1870 ** implies that videocore is suspended.
1871 ** Hence, any thread which needs to wait until videocore is
1872 ** running can wait on this completion - it will only block
1873 ** if videocore is suspended.
1875 ** VC_RESUME_RESUMED - Resume has completed successfully. Videocore is running.
1876 ** Call complete_all on the resume completion to unblock
1877 ** any threads waiting for resume. Also reset the suspend
1878 ** state machine to it's idle state.
1880 ** VC_RESUME_FAILED - Currently unused - no mechanism to fail resume exists.
1884 set_suspend_state(VCHIQ_ARM_STATE_T *arm_state,
1885 enum vc_suspend_status new_state)
1887 /* set the state in all cases */
1888 arm_state->vc_suspend_state = new_state;
1890 /* state specific additional actions */
1891 switch (new_state) {
1892 case VC_SUSPEND_FORCE_CANCELED:
1893 complete_all(&arm_state->vc_suspend_complete);
1895 case VC_SUSPEND_REJECTED:
1896 complete_all(&arm_state->vc_suspend_complete);
1898 case VC_SUSPEND_FAILED:
1899 complete_all(&arm_state->vc_suspend_complete);
1900 arm_state->vc_resume_state = VC_RESUME_RESUMED;
1901 complete_all(&arm_state->vc_resume_complete);
1903 case VC_SUSPEND_IDLE:
1904 reinit_completion(&arm_state->vc_suspend_complete);
1906 case VC_SUSPEND_REQUESTED:
1908 case VC_SUSPEND_IN_PROGRESS:
1909 set_resume_state(arm_state, VC_RESUME_IDLE);
1911 case VC_SUSPEND_SUSPENDED:
1912 complete_all(&arm_state->vc_suspend_complete);
1921 set_resume_state(VCHIQ_ARM_STATE_T *arm_state,
1922 enum vc_resume_status new_state)
1924 /* set the state in all cases */
1925 arm_state->vc_resume_state = new_state;
1927 /* state specific additional actions */
1928 switch (new_state) {
1929 case VC_RESUME_FAILED:
1931 case VC_RESUME_IDLE:
1932 reinit_completion(&arm_state->vc_resume_complete);
1934 case VC_RESUME_REQUESTED:
1936 case VC_RESUME_IN_PROGRESS:
1938 case VC_RESUME_RESUMED:
1939 complete_all(&arm_state->vc_resume_complete);
1940 set_suspend_state(arm_state, VC_SUSPEND_IDLE);
1949 /* should be called with the write lock held */
1951 start_suspend_timer(VCHIQ_ARM_STATE_T *arm_state)
1953 del_timer(&arm_state->suspend_timer);
1954 arm_state->suspend_timer.expires = jiffies +
1955 msecs_to_jiffies(arm_state->
1956 suspend_timer_timeout);
1957 add_timer(&arm_state->suspend_timer);
1958 arm_state->suspend_timer_running = 1;
1961 /* should be called with the write lock held */
1963 stop_suspend_timer(VCHIQ_ARM_STATE_T *arm_state)
1965 if (arm_state->suspend_timer_running) {
1966 del_timer(&arm_state->suspend_timer);
1967 arm_state->suspend_timer_running = 0;
1972 need_resume(VCHIQ_STATE_T *state)
1974 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
1975 return (arm_state->vc_suspend_state > VC_SUSPEND_IDLE) &&
1976 (arm_state->vc_resume_state < VC_RESUME_REQUESTED) &&
1977 vchiq_videocore_wanted(state);
1981 block_resume(VCHIQ_ARM_STATE_T *arm_state)
1983 int status = VCHIQ_SUCCESS;
1984 const unsigned long timeout_val =
1985 msecs_to_jiffies(FORCE_SUSPEND_TIMEOUT_MS);
1986 int resume_count = 0;
1988 /* Allow any threads which were blocked by the last force suspend to
1989 * complete if they haven't already. Only give this one shot; if
1990 * blocked_count is incremented after blocked_blocker is completed
1991 * (which only happens when blocked_count hits 0) then those threads
1992 * will have to wait until next time around */
1993 if (arm_state->blocked_count) {
1994 reinit_completion(&arm_state->blocked_blocker);
1995 write_unlock_bh(&arm_state->susp_res_lock);
1996 vchiq_log_info(vchiq_susp_log_level, "%s wait for previously "
1997 "blocked clients", __func__);
1998 if (wait_for_completion_interruptible_timeout(
1999 &arm_state->blocked_blocker, timeout_val)
2001 vchiq_log_error(vchiq_susp_log_level, "%s wait for "
2002 "previously blocked clients failed", __func__);
2003 status = VCHIQ_ERROR;
2004 write_lock_bh(&arm_state->susp_res_lock);
2007 vchiq_log_info(vchiq_susp_log_level, "%s previously blocked "
2008 "clients resumed", __func__);
2009 write_lock_bh(&arm_state->susp_res_lock);
2012 /* We need to wait for resume to complete if it's in process */
2013 while (arm_state->vc_resume_state != VC_RESUME_RESUMED &&
2014 arm_state->vc_resume_state > VC_RESUME_IDLE) {
2015 if (resume_count > 1) {
2016 status = VCHIQ_ERROR;
2017 vchiq_log_error(vchiq_susp_log_level, "%s waited too "
2018 "many times for resume", __func__);
2021 write_unlock_bh(&arm_state->susp_res_lock);
2022 vchiq_log_info(vchiq_susp_log_level, "%s wait for resume",
2024 if (wait_for_completion_interruptible_timeout(
2025 &arm_state->vc_resume_complete, timeout_val)
2027 vchiq_log_error(vchiq_susp_log_level, "%s wait for "
2028 "resume failed (%s)", __func__,
2029 resume_state_names[arm_state->vc_resume_state +
2030 VC_RESUME_NUM_OFFSET]);
2031 status = VCHIQ_ERROR;
2032 write_lock_bh(&arm_state->susp_res_lock);
2035 vchiq_log_info(vchiq_susp_log_level, "%s resumed", __func__);
2036 write_lock_bh(&arm_state->susp_res_lock);
2039 reinit_completion(&arm_state->resume_blocker);
2040 arm_state->resume_blocked = 1;
2047 unblock_resume(VCHIQ_ARM_STATE_T *arm_state)
2049 complete_all(&arm_state->resume_blocker);
2050 arm_state->resume_blocked = 0;
2053 /* Initiate suspend via slot handler. Should be called with the write lock
2056 vchiq_arm_vcsuspend(VCHIQ_STATE_T *state)
2058 VCHIQ_STATUS_T status = VCHIQ_ERROR;
2059 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
2064 vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
2065 status = VCHIQ_SUCCESS;
2068 switch (arm_state->vc_suspend_state) {
2069 case VC_SUSPEND_REQUESTED:
2070 vchiq_log_info(vchiq_susp_log_level, "%s: suspend already "
2071 "requested", __func__);
2073 case VC_SUSPEND_IN_PROGRESS:
2074 vchiq_log_info(vchiq_susp_log_level, "%s: suspend already in "
2075 "progress", __func__);
2079 /* We don't expect to be in other states, so log but continue
2081 vchiq_log_error(vchiq_susp_log_level,
2082 "%s unexpected suspend state %s", __func__,
2083 suspend_state_names[arm_state->vc_suspend_state +
2084 VC_SUSPEND_NUM_OFFSET]);
2086 case VC_SUSPEND_REJECTED:
2087 case VC_SUSPEND_FAILED:
2088 /* Ensure any idle state actions have been run */
2089 set_suspend_state(arm_state, VC_SUSPEND_IDLE);
2091 case VC_SUSPEND_IDLE:
2092 vchiq_log_info(vchiq_susp_log_level,
2093 "%s: suspending", __func__);
2094 set_suspend_state(arm_state, VC_SUSPEND_REQUESTED);
2095 /* kick the slot handler thread to initiate suspend */
2096 request_poll(state, NULL, 0);
2101 vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, status);
2106 vchiq_platform_check_suspend(VCHIQ_STATE_T *state)
2108 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
2114 vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
2116 write_lock_bh(&arm_state->susp_res_lock);
2117 if (arm_state->vc_suspend_state == VC_SUSPEND_REQUESTED &&
2118 arm_state->vc_resume_state == VC_RESUME_RESUMED) {
2119 set_suspend_state(arm_state, VC_SUSPEND_IN_PROGRESS);
2122 write_unlock_bh(&arm_state->susp_res_lock);
2125 vchiq_platform_suspend(state);
2128 vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
2134 output_timeout_error(VCHIQ_STATE_T *state)
2136 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
2138 int vc_use_count = arm_state->videocore_use_count;
2139 int active_services = state->unused_service;
2142 if (!arm_state->videocore_use_count) {
2143 snprintf(err, sizeof(err), " Videocore usecount is 0");
2146 for (i = 0; i < active_services; i++) {
2147 VCHIQ_SERVICE_T *service_ptr = state->services[i];
2148 if (service_ptr && service_ptr->service_use_count &&
2149 (service_ptr->srvstate != VCHIQ_SRVSTATE_FREE)) {
2150 snprintf(err, sizeof(err), " %c%c%c%c(%d) service has "
2151 "use count %d%s", VCHIQ_FOURCC_AS_4CHARS(
2152 service_ptr->base.fourcc),
2153 service_ptr->client_id,
2154 service_ptr->service_use_count,
2155 service_ptr->service_use_count ==
2156 vc_use_count ? "" : " (+ more)");
2162 vchiq_log_error(vchiq_susp_log_level,
2163 "timed out waiting for vc suspend (%d).%s",
2164 arm_state->autosuspend_override, err);
2168 /* Try to get videocore into suspended state, regardless of autosuspend state.
2169 ** We don't actually force suspend, since videocore may get into a bad state
2170 ** if we force suspend at a bad time. Instead, we wait for autosuspend to
2171 ** determine a good point to suspend. If this doesn't happen within 100ms we
2174 ** Returns VCHIQ_SUCCESS if videocore suspended successfully, VCHIQ_RETRY if
2175 ** videocore failed to suspend in time or VCHIQ_ERROR if interrupted.
2178 vchiq_arm_force_suspend(VCHIQ_STATE_T *state)
2180 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
2181 VCHIQ_STATUS_T status = VCHIQ_ERROR;
2188 vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
2190 write_lock_bh(&arm_state->susp_res_lock);
2192 status = block_resume(arm_state);
2193 if (status != VCHIQ_SUCCESS)
2195 if (arm_state->vc_suspend_state == VC_SUSPEND_SUSPENDED) {
2196 /* Already suspended - just block resume and exit */
2197 vchiq_log_info(vchiq_susp_log_level, "%s already suspended",
2199 status = VCHIQ_SUCCESS;
2201 } else if (arm_state->vc_suspend_state <= VC_SUSPEND_IDLE) {
2202 /* initiate suspend immediately in the case that we're waiting
2203 * for the timeout */
2204 stop_suspend_timer(arm_state);
2205 if (!vchiq_videocore_wanted(state)) {
2206 vchiq_log_info(vchiq_susp_log_level, "%s videocore "
2207 "idle, initiating suspend", __func__);
2208 status = vchiq_arm_vcsuspend(state);
2209 } else if (arm_state->autosuspend_override <
2210 FORCE_SUSPEND_FAIL_MAX) {
2211 vchiq_log_info(vchiq_susp_log_level, "%s letting "
2212 "videocore go idle", __func__);
2213 status = VCHIQ_SUCCESS;
2215 vchiq_log_warning(vchiq_susp_log_level, "%s failed too "
2216 "many times - attempting suspend", __func__);
2217 status = vchiq_arm_vcsuspend(state);
2220 vchiq_log_info(vchiq_susp_log_level, "%s videocore suspend "
2221 "in progress - wait for completion", __func__);
2222 status = VCHIQ_SUCCESS;
2225 /* Wait for suspend to happen due to system idle (not forced..) */
2226 if (status != VCHIQ_SUCCESS)
2227 goto unblock_resume;
2230 write_unlock_bh(&arm_state->susp_res_lock);
2232 rc = wait_for_completion_interruptible_timeout(
2233 &arm_state->vc_suspend_complete,
2234 msecs_to_jiffies(FORCE_SUSPEND_TIMEOUT_MS));
2236 write_lock_bh(&arm_state->susp_res_lock);
2238 vchiq_log_warning(vchiq_susp_log_level, "%s "
2239 "interrupted waiting for suspend", __func__);
2240 status = VCHIQ_ERROR;
2241 goto unblock_resume;
2242 } else if (rc == 0) {
2243 if (arm_state->vc_suspend_state > VC_SUSPEND_IDLE) {
2244 /* Repeat timeout once if in progress */
2250 arm_state->autosuspend_override++;
2251 output_timeout_error(state);
2253 status = VCHIQ_RETRY;
2254 goto unblock_resume;
2256 } while (0 < (repeat--));
2258 /* Check and report state in case we need to abort ARM suspend */
2259 if (arm_state->vc_suspend_state != VC_SUSPEND_SUSPENDED) {
2260 status = VCHIQ_RETRY;
2261 vchiq_log_error(vchiq_susp_log_level,
2262 "%s videocore suspend failed (state %s)", __func__,
2263 suspend_state_names[arm_state->vc_suspend_state +
2264 VC_SUSPEND_NUM_OFFSET]);
2265 /* Reset the state only if it's still in an error state.
2266 * Something could have already initiated another suspend. */
2267 if (arm_state->vc_suspend_state < VC_SUSPEND_IDLE)
2268 set_suspend_state(arm_state, VC_SUSPEND_IDLE);
2270 goto unblock_resume;
2273 /* successfully suspended - unlock and exit */
2277 /* all error states need to unblock resume before exit */
2278 unblock_resume(arm_state);
2281 write_unlock_bh(&arm_state->susp_res_lock);
2284 vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, status);
2289 vchiq_check_suspend(VCHIQ_STATE_T *state)
2291 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
2296 vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
2298 write_lock_bh(&arm_state->susp_res_lock);
2299 if (arm_state->vc_suspend_state != VC_SUSPEND_SUSPENDED &&
2300 arm_state->first_connect &&
2301 !vchiq_videocore_wanted(state)) {
2302 vchiq_arm_vcsuspend(state);
2304 write_unlock_bh(&arm_state->susp_res_lock);
2307 vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
2313 vchiq_arm_allow_resume(VCHIQ_STATE_T *state)
2315 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
2322 vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
2324 write_lock_bh(&arm_state->susp_res_lock);
2325 unblock_resume(arm_state);
2326 resume = vchiq_check_resume(state);
2327 write_unlock_bh(&arm_state->susp_res_lock);
2330 if (wait_for_completion_interruptible(
2331 &arm_state->vc_resume_complete) < 0) {
2332 vchiq_log_error(vchiq_susp_log_level,
2333 "%s interrupted", __func__);
2334 /* failed, cannot accurately derive suspend
2335 * state, so exit early. */
2340 read_lock_bh(&arm_state->susp_res_lock);
2341 if (arm_state->vc_suspend_state == VC_SUSPEND_SUSPENDED) {
2342 vchiq_log_info(vchiq_susp_log_level,
2343 "%s: Videocore remains suspended", __func__);
2345 vchiq_log_info(vchiq_susp_log_level,
2346 "%s: Videocore resumed", __func__);
2349 read_unlock_bh(&arm_state->susp_res_lock);
2351 vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, ret);
2355 /* This function should be called with the write lock held */
2357 vchiq_check_resume(VCHIQ_STATE_T *state)
2359 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
2365 vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
2367 if (need_resume(state)) {
2368 set_resume_state(arm_state, VC_RESUME_REQUESTED);
2369 request_poll(state, NULL, 0);
2374 vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
2379 vchiq_use_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service,
2380 enum USE_TYPE_E use_type)
2382 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
2383 VCHIQ_STATUS_T ret = VCHIQ_SUCCESS;
2386 int local_uc, local_entity_uc;
2391 vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
2393 if (use_type == USE_TYPE_VCHIQ) {
2394 sprintf(entity, "VCHIQ: ");
2395 entity_uc = &arm_state->peer_use_count;
2396 } else if (service) {
2397 sprintf(entity, "%c%c%c%c:%03d",
2398 VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
2399 service->client_id);
2400 entity_uc = &service->service_use_count;
2402 vchiq_log_error(vchiq_susp_log_level, "%s null service "
2408 write_lock_bh(&arm_state->susp_res_lock);
2409 while (arm_state->resume_blocked) {
2410 /* If we call 'use' while force suspend is waiting for suspend,
2411 * then we're about to block the thread which the force is
2412 * waiting to complete, so we're bound to just time out. In this
2413 * case, set the suspend state such that the wait will be
2414 * canceled, so we can complete as quickly as possible. */
2415 if (arm_state->resume_blocked && arm_state->vc_suspend_state ==
2417 set_suspend_state(arm_state, VC_SUSPEND_FORCE_CANCELED);
2420 /* If suspend is already in progress then we need to block */
2421 if (!try_wait_for_completion(&arm_state->resume_blocker)) {
2422 /* Indicate that there are threads waiting on the resume
2423 * blocker. These need to be allowed to complete before
2424 * a _second_ call to force suspend can complete,
2425 * otherwise low priority threads might never actually
2427 arm_state->blocked_count++;
2428 write_unlock_bh(&arm_state->susp_res_lock);
2429 vchiq_log_info(vchiq_susp_log_level, "%s %s resume "
2430 "blocked - waiting...", __func__, entity);
2431 if (wait_for_completion_killable(
2432 &arm_state->resume_blocker) != 0) {
2433 vchiq_log_error(vchiq_susp_log_level, "%s %s "
2434 "wait for resume blocker interrupted",
2437 write_lock_bh(&arm_state->susp_res_lock);
2438 arm_state->blocked_count--;
2439 write_unlock_bh(&arm_state->susp_res_lock);
2442 vchiq_log_info(vchiq_susp_log_level, "%s %s resume "
2443 "unblocked", __func__, entity);
2444 write_lock_bh(&arm_state->susp_res_lock);
2445 if (--arm_state->blocked_count == 0)
2446 complete_all(&arm_state->blocked_blocker);
2450 stop_suspend_timer(arm_state);
2452 local_uc = ++arm_state->videocore_use_count;
2453 local_entity_uc = ++(*entity_uc);
2455 /* If there's a pending request which hasn't yet been serviced then
2456 * just clear it. If we're past VC_SUSPEND_REQUESTED state then
2457 * vc_resume_complete will block until we either resume or fail to
2459 if (arm_state->vc_suspend_state <= VC_SUSPEND_REQUESTED)
2460 set_suspend_state(arm_state, VC_SUSPEND_IDLE);
2462 if ((use_type != USE_TYPE_SERVICE_NO_RESUME) && need_resume(state)) {
2463 set_resume_state(arm_state, VC_RESUME_REQUESTED);
2464 vchiq_log_info(vchiq_susp_log_level,
2465 "%s %s count %d, state count %d",
2466 __func__, entity, local_entity_uc, local_uc);
2467 request_poll(state, NULL, 0);
2469 vchiq_log_trace(vchiq_susp_log_level,
2470 "%s %s count %d, state count %d",
2471 __func__, entity, *entity_uc, local_uc);
2474 write_unlock_bh(&arm_state->susp_res_lock);
2476 /* Completion is in a done state when we're not suspended, so this won't
2477 * block for the non-suspended case. */
2478 if (!try_wait_for_completion(&arm_state->vc_resume_complete)) {
2479 vchiq_log_info(vchiq_susp_log_level, "%s %s wait for resume",
2481 if (wait_for_completion_killable(
2482 &arm_state->vc_resume_complete) != 0) {
2483 vchiq_log_error(vchiq_susp_log_level, "%s %s wait for "
2484 "resume interrupted", __func__, entity);
2488 vchiq_log_info(vchiq_susp_log_level, "%s %s resumed", __func__,
2492 if (ret == VCHIQ_SUCCESS) {
2493 VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
2494 long ack_cnt = atomic_xchg(&arm_state->ka_use_ack_count, 0);
2495 while (ack_cnt && (status == VCHIQ_SUCCESS)) {
2496 /* Send the use notify to videocore */
2497 status = vchiq_send_remote_use_active(state);
2498 if (status == VCHIQ_SUCCESS)
2502 &arm_state->ka_use_ack_count);
2507 vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, ret);
2512 vchiq_release_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service)
2514 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
2515 VCHIQ_STATUS_T ret = VCHIQ_SUCCESS;
2518 int local_uc, local_entity_uc;
2523 vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
2526 sprintf(entity, "%c%c%c%c:%03d",
2527 VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
2528 service->client_id);
2529 entity_uc = &service->service_use_count;
2531 sprintf(entity, "PEER: ");
2532 entity_uc = &arm_state->peer_use_count;
2535 write_lock_bh(&arm_state->susp_res_lock);
2536 if (!arm_state->videocore_use_count || !(*entity_uc)) {
2537 /* Don't use BUG_ON - don't allow user thread to crash kernel */
2538 WARN_ON(!arm_state->videocore_use_count);
2539 WARN_ON(!(*entity_uc));
2543 local_uc = --arm_state->videocore_use_count;
2544 local_entity_uc = --(*entity_uc);
2546 if (!vchiq_videocore_wanted(state)) {
2547 if (vchiq_platform_use_suspend_timer() &&
2548 !arm_state->resume_blocked) {
2549 /* Only use the timer if we're not trying to force
2550 * suspend (=> resume_blocked) */
2551 start_suspend_timer(arm_state);
2553 vchiq_log_info(vchiq_susp_log_level,
2554 "%s %s count %d, state count %d - suspending",
2555 __func__, entity, *entity_uc,
2556 arm_state->videocore_use_count);
2557 vchiq_arm_vcsuspend(state);
2560 vchiq_log_trace(vchiq_susp_log_level,
2561 "%s %s count %d, state count %d",
2562 __func__, entity, *entity_uc,
2563 arm_state->videocore_use_count);
2566 write_unlock_bh(&arm_state->susp_res_lock);
2569 vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, ret);
2574 vchiq_on_remote_use(VCHIQ_STATE_T *state)
2576 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
2577 vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
2578 atomic_inc(&arm_state->ka_use_count);
2579 complete(&arm_state->ka_evt);
2583 vchiq_on_remote_release(VCHIQ_STATE_T *state)
2585 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
2586 vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
2587 atomic_inc(&arm_state->ka_release_count);
2588 complete(&arm_state->ka_evt);
2592 vchiq_use_service_internal(VCHIQ_SERVICE_T *service)
2594 return vchiq_use_internal(service->state, service, USE_TYPE_SERVICE);
2598 vchiq_release_service_internal(VCHIQ_SERVICE_T *service)
2600 return vchiq_release_internal(service->state, service);
2603 VCHIQ_DEBUGFS_NODE_T *
2604 vchiq_instance_get_debugfs_node(VCHIQ_INSTANCE_T instance)
2606 return &instance->debugfs_node;
2610 vchiq_instance_get_use_count(VCHIQ_INSTANCE_T instance)
2612 VCHIQ_SERVICE_T *service;
2613 int use_count = 0, i;
2615 while ((service = next_service_by_instance(instance->state,
2616 instance, &i)) != NULL) {
2617 use_count += service->service_use_count;
2618 unlock_service(service);
2624 vchiq_instance_get_pid(VCHIQ_INSTANCE_T instance)
2626 return instance->pid;
2630 vchiq_instance_get_trace(VCHIQ_INSTANCE_T instance)
2632 return instance->trace;
2636 vchiq_instance_set_trace(VCHIQ_INSTANCE_T instance, int trace)
2638 VCHIQ_SERVICE_T *service;
2641 while ((service = next_service_by_instance(instance->state,
2642 instance, &i)) != NULL) {
2643 service->trace = trace;
2644 unlock_service(service);
2646 instance->trace = (trace != 0);
2649 static void suspend_timer_callback(unsigned long context)
2651 VCHIQ_STATE_T *state = (VCHIQ_STATE_T *)context;
2652 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
2655 vchiq_log_info(vchiq_susp_log_level,
2656 "%s - suspend timer expired - check suspend", __func__);
2657 vchiq_check_suspend(state);
2663 vchiq_use_service_no_resume(VCHIQ_SERVICE_HANDLE_T handle)
2665 VCHIQ_STATUS_T ret = VCHIQ_ERROR;
2666 VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
2668 ret = vchiq_use_internal(service->state, service,
2669 USE_TYPE_SERVICE_NO_RESUME);
2670 unlock_service(service);
2676 vchiq_use_service(VCHIQ_SERVICE_HANDLE_T handle)
2678 VCHIQ_STATUS_T ret = VCHIQ_ERROR;
2679 VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
2681 ret = vchiq_use_internal(service->state, service,
2683 unlock_service(service);
2689 vchiq_release_service(VCHIQ_SERVICE_HANDLE_T handle)
2691 VCHIQ_STATUS_T ret = VCHIQ_ERROR;
2692 VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
2694 ret = vchiq_release_internal(service->state, service);
2695 unlock_service(service);
2701 vchiq_dump_service_use_state(VCHIQ_STATE_T *state)
2703 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
2705 /* Only dump 64 services */
2706 static const int local_max_services = 64;
2707 /* If there's more than 64 services, only dump ones with
2708 * non-zero counts */
2709 int only_nonzero = 0;
2710 static const char *nz = "<-- preventing suspend";
2712 enum vc_suspend_status vc_suspend_state;
2713 enum vc_resume_status vc_resume_state;
2716 int active_services;
2717 struct service_data_struct {
2721 } service_data[local_max_services];
2726 read_lock_bh(&arm_state->susp_res_lock);
2727 vc_suspend_state = arm_state->vc_suspend_state;
2728 vc_resume_state = arm_state->vc_resume_state;
2729 peer_count = arm_state->peer_use_count;
2730 vc_use_count = arm_state->videocore_use_count;
2731 active_services = state->unused_service;
2732 if (active_services > local_max_services)
2735 for (i = 0; (i < active_services) && (j < local_max_services); i++) {
2736 VCHIQ_SERVICE_T *service_ptr = state->services[i];
2740 if (only_nonzero && !service_ptr->service_use_count)
2743 if (service_ptr->srvstate != VCHIQ_SRVSTATE_FREE) {
2744 service_data[j].fourcc = service_ptr->base.fourcc;
2745 service_data[j].clientid = service_ptr->client_id;
2746 service_data[j++].use_count = service_ptr->
2751 read_unlock_bh(&arm_state->susp_res_lock);
2753 vchiq_log_warning(vchiq_susp_log_level,
2754 "-- Videcore suspend state: %s --",
2755 suspend_state_names[vc_suspend_state + VC_SUSPEND_NUM_OFFSET]);
2756 vchiq_log_warning(vchiq_susp_log_level,
2757 "-- Videcore resume state: %s --",
2758 resume_state_names[vc_resume_state + VC_RESUME_NUM_OFFSET]);
2761 vchiq_log_warning(vchiq_susp_log_level, "Too many active "
2762 "services (%d). Only dumping up to first %d services "
2763 "with non-zero use-count", active_services,
2764 local_max_services);
2766 for (i = 0; i < j; i++) {
2767 vchiq_log_warning(vchiq_susp_log_level,
2768 "----- %c%c%c%c:%d service count %d %s",
2769 VCHIQ_FOURCC_AS_4CHARS(service_data[i].fourcc),
2770 service_data[i].clientid,
2771 service_data[i].use_count,
2772 service_data[i].use_count ? nz : "");
2774 vchiq_log_warning(vchiq_susp_log_level,
2775 "----- VCHIQ use count count %d", peer_count);
2776 vchiq_log_warning(vchiq_susp_log_level,
2777 "--- Overall vchiq instance use count %d", vc_use_count);
2779 vchiq_dump_platform_use_state(state);
2783 vchiq_check_service(VCHIQ_SERVICE_T *service)
2785 VCHIQ_ARM_STATE_T *arm_state;
2786 VCHIQ_STATUS_T ret = VCHIQ_ERROR;
2788 if (!service || !service->state)
2791 vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
2793 arm_state = vchiq_platform_get_arm_state(service->state);
2795 read_lock_bh(&arm_state->susp_res_lock);
2796 if (service->service_use_count)
2797 ret = VCHIQ_SUCCESS;
2798 read_unlock_bh(&arm_state->susp_res_lock);
2800 if (ret == VCHIQ_ERROR) {
2801 vchiq_log_error(vchiq_susp_log_level,
2802 "%s ERROR - %c%c%c%c:%d service count %d, "
2803 "state count %d, videocore suspend state %s", __func__,
2804 VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
2805 service->client_id, service->service_use_count,
2806 arm_state->videocore_use_count,
2807 suspend_state_names[arm_state->vc_suspend_state +
2808 VC_SUSPEND_NUM_OFFSET]);
2809 vchiq_dump_service_use_state(service->state);
2815 /* stub functions */
2816 void vchiq_on_remote_use_active(VCHIQ_STATE_T *state)
2821 void vchiq_platform_conn_state_changed(VCHIQ_STATE_T *state,
2822 VCHIQ_CONNSTATE_T oldstate, VCHIQ_CONNSTATE_T newstate)
2824 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
2825 vchiq_log_info(vchiq_susp_log_level, "%d: %s->%s", state->id,
2826 get_conn_state_name(oldstate), get_conn_state_name(newstate));
2827 if (state->conn_state == VCHIQ_CONNSTATE_CONNECTED) {
2828 write_lock_bh(&arm_state->susp_res_lock);
2829 if (!arm_state->first_connect) {
2830 char threadname[16];
2831 arm_state->first_connect = 1;
2832 write_unlock_bh(&arm_state->susp_res_lock);
2833 snprintf(threadname, sizeof(threadname), "vchiq-keep/%d",
2835 arm_state->ka_thread = kthread_create(
2836 &vchiq_keepalive_thread_func,
2839 if (IS_ERR(arm_state->ka_thread)) {
2840 vchiq_log_error(vchiq_susp_log_level,
2841 "vchiq: FATAL: couldn't create thread %s",
2844 wake_up_process(arm_state->ka_thread);
2847 write_unlock_bh(&arm_state->susp_res_lock);
2851 static int vchiq_probe(struct platform_device *pdev)
2853 struct device_node *fw_node;
2854 struct rpi_firmware *fw;
2858 fw_node = of_parse_phandle(pdev->dev.of_node, "firmware", 0);
2860 dev_err(&pdev->dev, "Missing firmware node\n");
2864 fw = rpi_firmware_get(fw_node);
2865 of_node_put(fw_node);
2867 return -EPROBE_DEFER;
2869 platform_set_drvdata(pdev, fw);
2871 err = vchiq_platform_init(pdev, &g_state);
2873 goto failed_platform_init;
2875 err = alloc_chrdev_region(&vchiq_devid, VCHIQ_MINOR, 1, DEVICE_NAME);
2877 vchiq_log_error(vchiq_arm_log_level,
2878 "Unable to allocate device number");
2879 goto failed_platform_init;
2881 cdev_init(&vchiq_cdev, &vchiq_fops);
2882 vchiq_cdev.owner = THIS_MODULE;
2883 err = cdev_add(&vchiq_cdev, vchiq_devid, 1);
2885 vchiq_log_error(vchiq_arm_log_level,
2886 "Unable to register device");
2887 goto failed_cdev_add;
2890 /* create sysfs entries */
2891 vchiq_class = class_create(THIS_MODULE, DEVICE_NAME);
2892 ptr_err = vchiq_class;
2893 if (IS_ERR(ptr_err))
2894 goto failed_class_create;
2896 vchiq_dev = device_create(vchiq_class, NULL,
2897 vchiq_devid, NULL, "vchiq");
2898 ptr_err = vchiq_dev;
2899 if (IS_ERR(ptr_err))
2900 goto failed_device_create;
2902 /* create debugfs entries */
2903 err = vchiq_debugfs_init();
2905 goto failed_debugfs_init;
2907 vchiq_log_info(vchiq_arm_log_level,
2908 "vchiq: initialised - version %d (min %d), device %d.%d",
2909 VCHIQ_VERSION, VCHIQ_VERSION_MIN,
2910 MAJOR(vchiq_devid), MINOR(vchiq_devid));
2914 failed_debugfs_init:
2915 device_destroy(vchiq_class, vchiq_devid);
2916 failed_device_create:
2917 class_destroy(vchiq_class);
2918 failed_class_create:
2919 cdev_del(&vchiq_cdev);
2920 err = PTR_ERR(ptr_err);
2922 unregister_chrdev_region(vchiq_devid, 1);
2923 failed_platform_init:
2924 vchiq_log_warning(vchiq_arm_log_level, "could not load vchiq");
2928 static int vchiq_remove(struct platform_device *pdev)
2930 vchiq_debugfs_deinit();
2931 device_destroy(vchiq_class, vchiq_devid);
2932 class_destroy(vchiq_class);
2933 cdev_del(&vchiq_cdev);
2934 unregister_chrdev_region(vchiq_devid, 1);
2939 static const struct of_device_id vchiq_of_match[] = {
2940 { .compatible = "brcm,bcm2835-vchiq", },
2943 MODULE_DEVICE_TABLE(of, vchiq_of_match);
2945 static struct platform_driver vchiq_driver = {
2947 .name = "bcm2835_vchiq",
2948 .of_match_table = vchiq_of_match,
2950 .probe = vchiq_probe,
2951 .remove = vchiq_remove,
2953 module_platform_driver(vchiq_driver);
2955 MODULE_LICENSE("GPL");
2956 MODULE_DESCRIPTION("Videocore VCHIQ driver");
2957 MODULE_AUTHOR("Broadcom Corporation");