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
, uint32_t 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 int skip_completion
= 0;
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;
361 spin_unlock(&msg_queue_spinlock
);
363 up(&user_service
->insert_event
);
368 if (skip_completion
) {
369 DEBUG_TRACE(SERVICE_CALLBACK_LINE
);
370 return VCHIQ_SUCCESS
;
373 DEBUG_TRACE(SERVICE_CALLBACK_LINE
);
375 return add_completion(instance
, reason
, header
, user_service
,
379 /****************************************************************************
383 ***************************************************************************/
385 user_service_free(void *userdata
)
390 /****************************************************************************
394 ***************************************************************************/
395 static void close_delivered(USER_SERVICE_T
*user_service
)
397 vchiq_log_info(vchiq_arm_log_level
,
398 "close_delivered(handle=%x)",
399 user_service
->service
->handle
);
401 if (user_service
->close_pending
) {
402 /* Allow the underlying service to be culled */
403 unlock_service(user_service
->service
);
405 /* Wake the user-thread blocked in close_ or remove_service */
406 up(&user_service
->close_event
);
408 user_service
->close_pending
= 0;
412 /****************************************************************************
416 ***************************************************************************/
418 vchiq_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
420 VCHIQ_INSTANCE_T instance
= file
->private_data
;
421 VCHIQ_STATUS_T status
= VCHIQ_SUCCESS
;
422 VCHIQ_SERVICE_T
*service
= NULL
;
425 DEBUG_INITIALISE(g_state
.local
)
427 vchiq_log_trace(vchiq_arm_log_level
,
428 "vchiq_ioctl - instance %pK, cmd %s, arg %lx",
430 ((_IOC_TYPE(cmd
) == VCHIQ_IOC_MAGIC
) &&
431 (_IOC_NR(cmd
) <= VCHIQ_IOC_MAX
)) ?
432 ioctl_names
[_IOC_NR(cmd
)] : "<invalid>", arg
);
435 case VCHIQ_IOC_SHUTDOWN
:
436 if (!instance
->connected
)
439 /* Remove all services */
441 while ((service
= next_service_by_instance(instance
->state
,
442 instance
, &i
)) != NULL
) {
443 status
= vchiq_remove_service(service
->handle
);
444 unlock_service(service
);
445 if (status
!= VCHIQ_SUCCESS
)
450 if (status
== VCHIQ_SUCCESS
) {
451 /* Wake the completion thread and ask it to exit */
452 instance
->closing
= 1;
453 up(&instance
->insert_event
);
458 case VCHIQ_IOC_CONNECT
:
459 if (instance
->connected
) {
463 rc
= mutex_lock_killable(&instance
->state
->mutex
);
465 vchiq_log_error(vchiq_arm_log_level
,
466 "vchiq: connect: could not lock mutex for "
468 instance
->state
->id
, rc
);
472 status
= vchiq_connect_internal(instance
->state
, instance
);
473 mutex_unlock(&instance
->state
->mutex
);
475 if (status
== VCHIQ_SUCCESS
)
476 instance
->connected
= 1;
478 vchiq_log_error(vchiq_arm_log_level
,
479 "vchiq: could not connect: %d", status
);
482 case VCHIQ_IOC_CREATE_SERVICE
: {
483 VCHIQ_CREATE_SERVICE_T args
;
484 USER_SERVICE_T
*user_service
= NULL
;
489 (&args
, (const void __user
*)arg
,
490 sizeof(args
)) != 0) {
495 user_service
= kmalloc(sizeof(USER_SERVICE_T
), GFP_KERNEL
);
502 if (!instance
->connected
) {
507 srvstate
= VCHIQ_SRVSTATE_OPENING
;
510 instance
->connected
?
511 VCHIQ_SRVSTATE_LISTENING
:
512 VCHIQ_SRVSTATE_HIDDEN
;
515 userdata
= args
.params
.userdata
;
516 args
.params
.callback
= service_callback
;
517 args
.params
.userdata
= user_service
;
518 service
= vchiq_add_service_internal(
520 &args
.params
, srvstate
,
521 instance
, user_service_free
);
523 if (service
!= NULL
) {
524 user_service
->service
= service
;
525 user_service
->userdata
= userdata
;
526 user_service
->instance
= instance
;
527 user_service
->is_vchi
= (args
.is_vchi
!= 0);
528 user_service
->dequeue_pending
= 0;
529 user_service
->close_pending
= 0;
530 user_service
->message_available_pos
=
531 instance
->completion_remove
- 1;
532 user_service
->msg_insert
= 0;
533 user_service
->msg_remove
= 0;
534 sema_init(&user_service
->insert_event
, 0);
535 sema_init(&user_service
->remove_event
, 0);
536 sema_init(&user_service
->close_event
, 0);
539 status
= vchiq_open_service_internal
540 (service
, instance
->pid
);
541 if (status
!= VCHIQ_SUCCESS
) {
542 vchiq_remove_service(service
->handle
);
544 ret
= (status
== VCHIQ_RETRY
) ?
550 if (copy_to_user((void __user
*)
551 &(((VCHIQ_CREATE_SERVICE_T __user
*)
553 (const void *)&service
->handle
,
554 sizeof(service
->handle
)) != 0) {
556 vchiq_remove_service(service
->handle
);
566 case VCHIQ_IOC_CLOSE_SERVICE
: {
567 VCHIQ_SERVICE_HANDLE_T handle
= (VCHIQ_SERVICE_HANDLE_T
)arg
;
569 service
= find_service_for_instance(instance
, handle
);
570 if (service
!= NULL
) {
571 USER_SERVICE_T
*user_service
=
572 (USER_SERVICE_T
*)service
->base
.userdata
;
573 /* close_pending is false on first entry, and when the
574 wait in vchiq_close_service has been interrupted. */
575 if (!user_service
->close_pending
) {
576 status
= vchiq_close_service(service
->handle
);
577 if (status
!= VCHIQ_SUCCESS
)
581 /* close_pending is true once the underlying service
582 has been closed until the client library calls the
583 CLOSE_DELIVERED ioctl, signalling close_event. */
584 if (user_service
->close_pending
&&
585 down_interruptible(&user_service
->close_event
))
586 status
= VCHIQ_RETRY
;
592 case VCHIQ_IOC_REMOVE_SERVICE
: {
593 VCHIQ_SERVICE_HANDLE_T handle
= (VCHIQ_SERVICE_HANDLE_T
)arg
;
595 service
= find_service_for_instance(instance
, handle
);
596 if (service
!= NULL
) {
597 USER_SERVICE_T
*user_service
=
598 (USER_SERVICE_T
*)service
->base
.userdata
;
599 /* close_pending is false on first entry, and when the
600 wait in vchiq_close_service has been interrupted. */
601 if (!user_service
->close_pending
) {
602 status
= vchiq_remove_service(service
->handle
);
603 if (status
!= VCHIQ_SUCCESS
)
607 /* close_pending is true once the underlying service
608 has been closed until the client library calls the
609 CLOSE_DELIVERED ioctl, signalling close_event. */
610 if (user_service
->close_pending
&&
611 down_interruptible(&user_service
->close_event
))
612 status
= VCHIQ_RETRY
;
618 case VCHIQ_IOC_USE_SERVICE
:
619 case VCHIQ_IOC_RELEASE_SERVICE
: {
620 VCHIQ_SERVICE_HANDLE_T handle
= (VCHIQ_SERVICE_HANDLE_T
)arg
;
622 service
= find_service_for_instance(instance
, handle
);
623 if (service
!= NULL
) {
624 status
= (cmd
== VCHIQ_IOC_USE_SERVICE
) ?
625 vchiq_use_service_internal(service
) :
626 vchiq_release_service_internal(service
);
627 if (status
!= VCHIQ_SUCCESS
) {
628 vchiq_log_error(vchiq_susp_log_level
,
629 "%s: cmd %s returned error %d for "
630 "service %c%c%c%c:%03d",
632 (cmd
== VCHIQ_IOC_USE_SERVICE
) ?
633 "VCHIQ_IOC_USE_SERVICE" :
634 "VCHIQ_IOC_RELEASE_SERVICE",
636 VCHIQ_FOURCC_AS_4CHARS(
637 service
->base
.fourcc
),
645 case VCHIQ_IOC_QUEUE_MESSAGE
: {
646 VCHIQ_QUEUE_MESSAGE_T args
;
648 (&args
, (const void __user
*)arg
,
649 sizeof(args
)) != 0) {
654 service
= find_service_for_instance(instance
, args
.handle
);
656 if ((service
!= NULL
) && (args
.count
<= MAX_ELEMENTS
)) {
657 /* Copy elements into kernel space */
658 VCHIQ_ELEMENT_T elements
[MAX_ELEMENTS
];
659 if (copy_from_user(elements
, args
.elements
,
660 args
.count
* sizeof(VCHIQ_ELEMENT_T
)) == 0)
661 status
= vchiq_queue_message
663 elements
, args
.count
);
671 case VCHIQ_IOC_QUEUE_BULK_TRANSMIT
:
672 case VCHIQ_IOC_QUEUE_BULK_RECEIVE
: {
673 VCHIQ_QUEUE_BULK_TRANSFER_T args
;
674 struct bulk_waiter_node
*waiter
= NULL
;
675 VCHIQ_BULK_DIR_T dir
=
676 (cmd
== VCHIQ_IOC_QUEUE_BULK_TRANSMIT
) ?
677 VCHIQ_BULK_TRANSMIT
: VCHIQ_BULK_RECEIVE
;
680 (&args
, (const void __user
*)arg
,
681 sizeof(args
)) != 0) {
686 service
= find_service_for_instance(instance
, args
.handle
);
692 if (args
.mode
== VCHIQ_BULK_MODE_BLOCKING
) {
693 waiter
= kzalloc(sizeof(struct bulk_waiter_node
),
699 args
.userdata
= &waiter
->bulk_waiter
;
700 } else if (args
.mode
== VCHIQ_BULK_MODE_WAITING
) {
701 struct list_head
*pos
;
702 mutex_lock(&instance
->bulk_waiter_list_mutex
);
703 list_for_each(pos
, &instance
->bulk_waiter_list
) {
704 if (list_entry(pos
, struct bulk_waiter_node
,
705 list
)->pid
== current
->pid
) {
706 waiter
= list_entry(pos
,
707 struct bulk_waiter_node
,
714 mutex_unlock(&instance
->bulk_waiter_list_mutex
);
716 vchiq_log_error(vchiq_arm_log_level
,
717 "no bulk_waiter found for pid %d",
722 vchiq_log_info(vchiq_arm_log_level
,
723 "found bulk_waiter %pK for pid %d", waiter
,
725 args
.userdata
= &waiter
->bulk_waiter
;
727 status
= vchiq_bulk_transfer
729 VCHI_MEM_HANDLE_INVALID
,
730 args
.data
, args
.size
,
731 args
.userdata
, args
.mode
,
735 if ((status
!= VCHIQ_RETRY
) || fatal_signal_pending(current
) ||
736 !waiter
->bulk_waiter
.bulk
) {
737 if (waiter
->bulk_waiter
.bulk
) {
738 /* Cancel the signal when the transfer
740 spin_lock(&bulk_waiter_spinlock
);
741 waiter
->bulk_waiter
.bulk
->userdata
= NULL
;
742 spin_unlock(&bulk_waiter_spinlock
);
746 const VCHIQ_BULK_MODE_T mode_waiting
=
747 VCHIQ_BULK_MODE_WAITING
;
748 waiter
->pid
= current
->pid
;
749 mutex_lock(&instance
->bulk_waiter_list_mutex
);
750 list_add(&waiter
->list
, &instance
->bulk_waiter_list
);
751 mutex_unlock(&instance
->bulk_waiter_list_mutex
);
752 vchiq_log_info(vchiq_arm_log_level
,
753 "saved bulk_waiter %pK for pid %d",
754 waiter
, current
->pid
);
756 if (copy_to_user((void __user
*)
757 &(((VCHIQ_QUEUE_BULK_TRANSFER_T __user
*)
759 (const void *)&mode_waiting
,
760 sizeof(mode_waiting
)) != 0)
765 case VCHIQ_IOC_AWAIT_COMPLETION
: {
766 VCHIQ_AWAIT_COMPLETION_T args
;
768 DEBUG_TRACE(AWAIT_COMPLETION_LINE
);
769 if (!instance
->connected
) {
774 if (copy_from_user(&args
, (const void __user
*)arg
,
775 sizeof(args
)) != 0) {
780 mutex_lock(&instance
->completion_mutex
);
782 DEBUG_TRACE(AWAIT_COMPLETION_LINE
);
783 while ((instance
->completion_remove
==
784 instance
->completion_insert
)
785 && !instance
->closing
) {
787 DEBUG_TRACE(AWAIT_COMPLETION_LINE
);
788 mutex_unlock(&instance
->completion_mutex
);
789 rc
= down_interruptible(&instance
->insert_event
);
790 mutex_lock(&instance
->completion_mutex
);
792 DEBUG_TRACE(AWAIT_COMPLETION_LINE
);
793 vchiq_log_info(vchiq_arm_log_level
,
794 "AWAIT_COMPLETION interrupted");
799 DEBUG_TRACE(AWAIT_COMPLETION_LINE
);
801 /* A read memory barrier is needed to stop prefetch of a stale
807 int msgbufcount
= args
.msgbufcount
;
808 for (ret
= 0; ret
< args
.count
; ret
++) {
809 VCHIQ_COMPLETION_DATA_T
*completion
;
810 VCHIQ_SERVICE_T
*service
;
811 USER_SERVICE_T
*user_service
;
812 VCHIQ_HEADER_T
*header
;
813 if (instance
->completion_remove
==
814 instance
->completion_insert
)
816 completion
= &instance
->completions
[
817 instance
->completion_remove
&
818 (MAX_COMPLETIONS
- 1)];
820 service
= completion
->service_userdata
;
821 user_service
= service
->base
.userdata
;
822 completion
->service_userdata
=
823 user_service
->userdata
;
825 header
= completion
->header
;
830 msglen
= header
->size
+
831 sizeof(VCHIQ_HEADER_T
);
832 /* This must be a VCHIQ-style service */
833 if (args
.msgbufsize
< msglen
) {
836 "header %pK: msgbufsize %x < msglen %x",
837 header
, args
.msgbufsize
,
839 WARN(1, "invalid message "
845 if (msgbufcount
<= 0)
846 /* Stall here for lack of a
847 ** buffer for the message. */
849 /* Get the pointer from user space */
851 if (copy_from_user(&msgbuf
,
852 (const void __user
*)
853 &args
.msgbufs
[msgbufcount
],
854 sizeof(msgbuf
)) != 0) {
860 /* Copy the message to user space */
861 if (copy_to_user(msgbuf
, header
,
868 /* Now it has been copied, the message
869 ** can be released. */
870 vchiq_release_message(service
->handle
,
873 /* The completion must point to the
875 completion
->header
= msgbuf
;
878 if ((completion
->reason
==
879 VCHIQ_SERVICE_CLOSED
) &&
880 !instance
->use_close_delivered
)
881 unlock_service(service
);
883 if (copy_to_user((void __user
*)(
885 ret
* sizeof(VCHIQ_COMPLETION_DATA_T
)),
887 sizeof(VCHIQ_COMPLETION_DATA_T
)) != 0) {
893 instance
->completion_remove
++;
896 if (msgbufcount
!= args
.msgbufcount
) {
897 if (copy_to_user((void __user
*)
898 &((VCHIQ_AWAIT_COMPLETION_T
*)arg
)->
901 sizeof(msgbufcount
)) != 0) {
908 up(&instance
->remove_event
);
909 mutex_unlock(&instance
->completion_mutex
);
910 DEBUG_TRACE(AWAIT_COMPLETION_LINE
);
913 case VCHIQ_IOC_DEQUEUE_MESSAGE
: {
914 VCHIQ_DEQUEUE_MESSAGE_T args
;
915 USER_SERVICE_T
*user_service
;
916 VCHIQ_HEADER_T
*header
;
918 DEBUG_TRACE(DEQUEUE_MESSAGE_LINE
);
920 (&args
, (const void __user
*)arg
,
921 sizeof(args
)) != 0) {
925 service
= find_service_for_instance(instance
, args
.handle
);
930 user_service
= (USER_SERVICE_T
*)service
->base
.userdata
;
931 if (user_service
->is_vchi
== 0) {
936 spin_lock(&msg_queue_spinlock
);
937 if (user_service
->msg_remove
== user_service
->msg_insert
) {
938 if (!args
.blocking
) {
939 spin_unlock(&msg_queue_spinlock
);
940 DEBUG_TRACE(DEQUEUE_MESSAGE_LINE
);
944 user_service
->dequeue_pending
= 1;
946 spin_unlock(&msg_queue_spinlock
);
947 DEBUG_TRACE(DEQUEUE_MESSAGE_LINE
);
948 if (down_interruptible(
949 &user_service
->insert_event
) != 0) {
950 vchiq_log_info(vchiq_arm_log_level
,
951 "DEQUEUE_MESSAGE interrupted");
955 spin_lock(&msg_queue_spinlock
);
956 } while (user_service
->msg_remove
==
957 user_service
->msg_insert
);
963 BUG_ON((int)(user_service
->msg_insert
-
964 user_service
->msg_remove
) < 0);
966 header
= user_service
->msg_queue
[user_service
->msg_remove
&
967 (MSG_QUEUE_SIZE
- 1)];
968 user_service
->msg_remove
++;
969 spin_unlock(&msg_queue_spinlock
);
971 up(&user_service
->remove_event
);
974 else if (header
->size
<= args
.bufsize
) {
975 /* Copy to user space if msgbuf is not NULL */
976 if ((args
.buf
== NULL
) ||
977 (copy_to_user((void __user
*)args
.buf
,
979 header
->size
) == 0)) {
981 vchiq_release_message(
987 vchiq_log_error(vchiq_arm_log_level
,
988 "header %pK: bufsize %x < size %x",
989 header
, args
.bufsize
, header
->size
);
990 WARN(1, "invalid size\n");
993 DEBUG_TRACE(DEQUEUE_MESSAGE_LINE
);
996 case VCHIQ_IOC_GET_CLIENT_ID
: {
997 VCHIQ_SERVICE_HANDLE_T handle
= (VCHIQ_SERVICE_HANDLE_T
)arg
;
999 ret
= vchiq_get_client_id(handle
);
1002 case VCHIQ_IOC_GET_CONFIG
: {
1003 VCHIQ_GET_CONFIG_T args
;
1004 VCHIQ_CONFIG_T config
;
1006 if (copy_from_user(&args
, (const void __user
*)arg
,
1007 sizeof(args
)) != 0) {
1011 if (args
.config_size
> sizeof(config
)) {
1015 status
= vchiq_get_config(instance
, args
.config_size
, &config
);
1016 if (status
== VCHIQ_SUCCESS
) {
1017 if (copy_to_user((void __user
*)args
.pconfig
,
1018 &config
, args
.config_size
) != 0) {
1025 case VCHIQ_IOC_SET_SERVICE_OPTION
: {
1026 VCHIQ_SET_SERVICE_OPTION_T args
;
1029 &args
, (const void __user
*)arg
,
1030 sizeof(args
)) != 0) {
1035 service
= find_service_for_instance(instance
, args
.handle
);
1041 status
= vchiq_set_service_option(
1042 args
.handle
, args
.option
, args
.value
);
1045 case VCHIQ_IOC_DUMP_PHYS_MEM
: {
1046 VCHIQ_DUMP_MEM_T args
;
1049 (&args
, (const void __user
*)arg
,
1050 sizeof(args
)) != 0) {
1054 dump_phys_mem(args
.virt_addr
, args
.num_bytes
);
1057 case VCHIQ_IOC_LIB_VERSION
: {
1058 unsigned int lib_version
= (unsigned int)arg
;
1060 if (lib_version
< VCHIQ_VERSION_MIN
)
1062 else if (lib_version
>= VCHIQ_VERSION_CLOSE_DELIVERED
)
1063 instance
->use_close_delivered
= 1;
1066 case VCHIQ_IOC_CLOSE_DELIVERED
: {
1067 VCHIQ_SERVICE_HANDLE_T handle
= (VCHIQ_SERVICE_HANDLE_T
)arg
;
1069 service
= find_closed_service_for_instance(instance
, handle
);
1070 if (service
!= NULL
) {
1071 USER_SERVICE_T
*user_service
=
1072 (USER_SERVICE_T
*)service
->base
.userdata
;
1073 close_delivered(user_service
);
1085 unlock_service(service
);
1088 if (status
== VCHIQ_ERROR
)
1090 else if (status
== VCHIQ_RETRY
)
1094 if ((status
== VCHIQ_SUCCESS
) && (ret
< 0) && (ret
!= -EINTR
) &&
1095 (ret
!= -EWOULDBLOCK
))
1096 vchiq_log_info(vchiq_arm_log_level
,
1097 " ioctl instance %lx, cmd %s -> status %d, %ld",
1098 (unsigned long)instance
,
1099 (_IOC_NR(cmd
) <= VCHIQ_IOC_MAX
) ?
1100 ioctl_names
[_IOC_NR(cmd
)] :
1104 vchiq_log_trace(vchiq_arm_log_level
,
1105 " ioctl instance %lx, cmd %s -> status %d, %ld",
1106 (unsigned long)instance
,
1107 (_IOC_NR(cmd
) <= VCHIQ_IOC_MAX
) ?
1108 ioctl_names
[_IOC_NR(cmd
)] :
1115 /****************************************************************************
1119 ***************************************************************************/
1122 vchiq_open(struct inode
*inode
, struct file
*file
)
1124 int dev
= iminor(inode
) & 0x0f;
1125 vchiq_log_info(vchiq_arm_log_level
, "vchiq_open");
1129 VCHIQ_STATE_T
*state
= vchiq_get_state();
1130 VCHIQ_INSTANCE_T instance
;
1133 vchiq_log_error(vchiq_arm_log_level
,
1134 "vchiq has no connection to VideoCore");
1138 instance
= kzalloc(sizeof(*instance
), GFP_KERNEL
);
1142 instance
->state
= state
;
1143 instance
->pid
= current
->tgid
;
1145 ret
= vchiq_debugfs_add_instance(instance
);
1151 sema_init(&instance
->insert_event
, 0);
1152 sema_init(&instance
->remove_event
, 0);
1153 mutex_init(&instance
->completion_mutex
);
1154 mutex_init(&instance
->bulk_waiter_list_mutex
);
1155 INIT_LIST_HEAD(&instance
->bulk_waiter_list
);
1157 file
->private_data
= instance
;
1161 vchiq_log_error(vchiq_arm_log_level
,
1162 "Unknown minor device: %d", dev
);
1169 /****************************************************************************
1173 ***************************************************************************/
1176 vchiq_release(struct inode
*inode
, struct file
*file
)
1178 int dev
= iminor(inode
) & 0x0f;
1182 VCHIQ_INSTANCE_T instance
= file
->private_data
;
1183 VCHIQ_STATE_T
*state
= vchiq_get_state();
1184 VCHIQ_SERVICE_T
*service
;
1187 vchiq_log_info(vchiq_arm_log_level
,
1188 "vchiq_release: instance=%lx",
1189 (unsigned long)instance
);
1196 /* Ensure videocore is awake to allow termination. */
1197 vchiq_use_internal(instance
->state
, NULL
,
1200 mutex_lock(&instance
->completion_mutex
);
1202 /* Wake the completion thread and ask it to exit */
1203 instance
->closing
= 1;
1204 up(&instance
->insert_event
);
1206 mutex_unlock(&instance
->completion_mutex
);
1208 /* Wake the slot handler if the completion queue is full. */
1209 up(&instance
->remove_event
);
1211 /* Mark all services for termination... */
1213 while ((service
= next_service_by_instance(state
, instance
,
1215 USER_SERVICE_T
*user_service
= service
->base
.userdata
;
1217 /* Wake the slot handler if the msg queue is full. */
1218 up(&user_service
->remove_event
);
1220 vchiq_terminate_service_internal(service
);
1221 unlock_service(service
);
1224 /* ...and wait for them to die */
1226 while ((service
= next_service_by_instance(state
, instance
, &i
))
1228 USER_SERVICE_T
*user_service
= service
->base
.userdata
;
1230 down(&service
->remove_event
);
1232 BUG_ON(service
->srvstate
!= VCHIQ_SRVSTATE_FREE
);
1234 spin_lock(&msg_queue_spinlock
);
1236 while (user_service
->msg_remove
!=
1237 user_service
->msg_insert
) {
1238 VCHIQ_HEADER_T
*header
= user_service
->
1239 msg_queue
[user_service
->msg_remove
&
1240 (MSG_QUEUE_SIZE
- 1)];
1241 user_service
->msg_remove
++;
1242 spin_unlock(&msg_queue_spinlock
);
1245 vchiq_release_message(
1248 spin_lock(&msg_queue_spinlock
);
1251 spin_unlock(&msg_queue_spinlock
);
1253 unlock_service(service
);
1256 /* Release any closed services */
1257 while (instance
->completion_remove
!=
1258 instance
->completion_insert
) {
1259 VCHIQ_COMPLETION_DATA_T
*completion
;
1260 VCHIQ_SERVICE_T
*service
;
1261 completion
= &instance
->completions
[
1262 instance
->completion_remove
&
1263 (MAX_COMPLETIONS
- 1)];
1264 service
= completion
->service_userdata
;
1265 if (completion
->reason
== VCHIQ_SERVICE_CLOSED
)
1267 USER_SERVICE_T
*user_service
=
1268 service
->base
.userdata
;
1270 /* Wake any blocked user-thread */
1271 if (instance
->use_close_delivered
)
1272 up(&user_service
->close_event
);
1273 unlock_service(service
);
1275 instance
->completion_remove
++;
1278 /* Release the PEER service count. */
1279 vchiq_release_internal(instance
->state
, NULL
);
1282 struct list_head
*pos
, *next
;
1283 list_for_each_safe(pos
, next
,
1284 &instance
->bulk_waiter_list
) {
1285 struct bulk_waiter_node
*waiter
;
1286 waiter
= list_entry(pos
,
1287 struct bulk_waiter_node
,
1290 vchiq_log_info(vchiq_arm_log_level
,
1291 "bulk_waiter - cleaned up %pK for pid %d",
1292 waiter
, waiter
->pid
);
1297 vchiq_debugfs_remove_instance(instance
);
1300 file
->private_data
= NULL
;
1304 vchiq_log_error(vchiq_arm_log_level
,
1305 "Unknown minor device: %d", dev
);
1313 /****************************************************************************
1317 ***************************************************************************/
1320 vchiq_dump(void *dump_context
, const char *str
, int len
)
1322 DUMP_CONTEXT_T
*context
= (DUMP_CONTEXT_T
*)dump_context
;
1324 if (context
->actual
< context
->space
) {
1326 if (context
->offset
> 0) {
1327 int skip_bytes
= min(len
, (int)context
->offset
);
1330 context
->offset
-= skip_bytes
;
1331 if (context
->offset
> 0)
1334 copy_bytes
= min(len
, (int)(context
->space
- context
->actual
));
1335 if (copy_bytes
== 0)
1337 if (copy_to_user(context
->buf
+ context
->actual
, str
,
1339 context
->actual
= -EFAULT
;
1340 context
->actual
+= copy_bytes
;
1343 /* If tne terminating NUL is included in the length, then it
1344 ** marks the end of a line and should be replaced with a
1345 ** carriage return. */
1346 if ((len
== 0) && (str
[copy_bytes
- 1] == '\0')) {
1348 if (copy_to_user(context
->buf
+ context
->actual
- 1,
1350 context
->actual
= -EFAULT
;
1355 /****************************************************************************
1357 * vchiq_dump_platform_instance_state
1359 ***************************************************************************/
1362 vchiq_dump_platform_instances(void *dump_context
)
1364 VCHIQ_STATE_T
*state
= vchiq_get_state();
1369 /* There is no list of instances, so instead scan all services,
1370 marking those that have been dumped. */
1372 for (i
= 0; i
< state
->unused_service
; i
++) {
1373 VCHIQ_SERVICE_T
*service
= state
->services
[i
];
1374 VCHIQ_INSTANCE_T instance
;
1376 if (service
&& (service
->base
.callback
== service_callback
)) {
1377 instance
= service
->instance
;
1383 for (i
= 0; i
< state
->unused_service
; i
++) {
1384 VCHIQ_SERVICE_T
*service
= state
->services
[i
];
1385 VCHIQ_INSTANCE_T instance
;
1387 if (service
&& (service
->base
.callback
== service_callback
)) {
1388 instance
= service
->instance
;
1389 if (instance
&& !instance
->mark
) {
1390 len
= snprintf(buf
, sizeof(buf
),
1391 "Instance %pK: pid %d,%s completions %d/%d",
1392 instance
, instance
->pid
,
1393 instance
->connected
? " connected, " :
1395 instance
->completion_insert
-
1396 instance
->completion_remove
,
1399 vchiq_dump(dump_context
, buf
, len
+ 1);
1407 /****************************************************************************
1409 * vchiq_dump_platform_service_state
1411 ***************************************************************************/
1414 vchiq_dump_platform_service_state(void *dump_context
, VCHIQ_SERVICE_T
*service
)
1416 USER_SERVICE_T
*user_service
= (USER_SERVICE_T
*)service
->base
.userdata
;
1420 len
= snprintf(buf
, sizeof(buf
), " instance %pK", service
->instance
);
1422 if ((service
->base
.callback
== service_callback
) &&
1423 user_service
->is_vchi
) {
1424 len
+= snprintf(buf
+ len
, sizeof(buf
) - len
,
1426 user_service
->msg_insert
- user_service
->msg_remove
,
1429 if (user_service
->dequeue_pending
)
1430 len
+= snprintf(buf
+ len
, sizeof(buf
) - len
,
1431 " (dequeue pending)");
1434 vchiq_dump(dump_context
, buf
, len
+ 1);
1437 /****************************************************************************
1441 ***************************************************************************/
1444 dump_phys_mem(void *virt_addr
, uint32_t num_bytes
)
1447 uint8_t *end_virt_addr
= virt_addr
+ num_bytes
;
1454 struct page
**pages
;
1455 uint8_t *kmapped_virt_ptr
;
1457 /* Align virtAddr and endVirtAddr to 16 byte boundaries. */
1459 virt_addr
= (void *)((unsigned long)virt_addr
& ~0x0fuL
);
1460 end_virt_addr
= (void *)(((unsigned long)end_virt_addr
+ 15uL) &
1463 offset
= (int)(long)virt_addr
& (PAGE_SIZE
- 1);
1464 end_offset
= (int)(long)end_virt_addr
& (PAGE_SIZE
- 1);
1466 num_pages
= (offset
+ num_bytes
+ PAGE_SIZE
- 1) / PAGE_SIZE
;
1468 pages
= kmalloc(sizeof(struct page
*) * num_pages
, GFP_KERNEL
);
1469 if (pages
== NULL
) {
1470 vchiq_log_error(vchiq_arm_log_level
,
1471 "Unable to allocation memory for %d pages\n",
1476 down_read(¤t
->mm
->mmap_sem
);
1477 rc
= get_user_pages(
1478 (unsigned long)virt_addr
, /* start */
1479 num_pages
, /* len */
1481 pages
, /* pages (array of page pointers) */
1483 up_read(¤t
->mm
->mmap_sem
);
1489 vchiq_log_error(vchiq_arm_log_level
,
1490 "Failed to get user pages: %d\n", rc
);
1494 while (offset
< end_offset
) {
1496 int page_offset
= offset
% PAGE_SIZE
;
1497 page_idx
= offset
/ PAGE_SIZE
;
1499 if (page_idx
!= prev_idx
) {
1503 page
= pages
[page_idx
];
1504 kmapped_virt_ptr
= kmap(page
);
1506 prev_idx
= page_idx
;
1509 if (vchiq_arm_log_level
>= VCHIQ_LOG_TRACE
)
1510 vchiq_log_dump_mem("ph",
1511 (uint32_t)(unsigned long)&kmapped_virt_ptr
[
1513 &kmapped_virt_ptr
[page_offset
], 16);
1522 for (page_idx
= 0; page_idx
< num_pages
; page_idx
++)
1523 put_page(pages
[page_idx
]);
1528 /****************************************************************************
1532 ***************************************************************************/
1535 vchiq_read(struct file
*file
, char __user
*buf
,
1536 size_t count
, loff_t
*ppos
)
1538 DUMP_CONTEXT_T context
;
1541 context
.space
= count
;
1542 context
.offset
= *ppos
;
1544 vchiq_dump_state(&context
, &g_state
);
1546 *ppos
+= context
.actual
;
1548 return context
.actual
;
1552 vchiq_get_state(void)
1555 if (g_state
.remote
== NULL
)
1556 printk(KERN_ERR
"%s: g_state.remote == NULL\n", __func__
);
1557 else if (g_state
.remote
->initialised
!= 1)
1558 printk(KERN_NOTICE
"%s: g_state.remote->initialised != 1 (%d)\n",
1559 __func__
, g_state
.remote
->initialised
);
1561 return ((g_state
.remote
!= NULL
) &&
1562 (g_state
.remote
->initialised
== 1)) ? &g_state
: NULL
;
1565 static const struct file_operations
1567 .owner
= THIS_MODULE
,
1568 .unlocked_ioctl
= vchiq_ioctl
,
1570 .release
= vchiq_release
,
1575 * Autosuspend related functionality
1579 vchiq_videocore_wanted(VCHIQ_STATE_T
*state
)
1581 VCHIQ_ARM_STATE_T
*arm_state
= vchiq_platform_get_arm_state(state
);
1583 /* autosuspend not supported - always return wanted */
1585 else if (arm_state
->blocked_count
)
1587 else if (!arm_state
->videocore_use_count
)
1588 /* usage count zero - check for override unless we're forcing */
1589 if (arm_state
->resume_blocked
)
1592 return vchiq_platform_videocore_wanted(state
);
1594 /* non-zero usage count - videocore still required */
1598 static VCHIQ_STATUS_T
1599 vchiq_keepalive_vchiq_callback(VCHIQ_REASON_T reason
,
1600 VCHIQ_HEADER_T
*header
,
1601 VCHIQ_SERVICE_HANDLE_T service_user
,
1604 vchiq_log_error(vchiq_susp_log_level
,
1605 "%s callback reason %d", __func__
, reason
);
1610 vchiq_keepalive_thread_func(void *v
)
1612 VCHIQ_STATE_T
*state
= (VCHIQ_STATE_T
*) v
;
1613 VCHIQ_ARM_STATE_T
*arm_state
= vchiq_platform_get_arm_state(state
);
1615 VCHIQ_STATUS_T status
;
1616 VCHIQ_INSTANCE_T instance
;
1617 VCHIQ_SERVICE_HANDLE_T ka_handle
;
1619 VCHIQ_SERVICE_PARAMS_T params
= {
1620 .fourcc
= VCHIQ_MAKE_FOURCC('K', 'E', 'E', 'P'),
1621 .callback
= vchiq_keepalive_vchiq_callback
,
1622 .version
= KEEPALIVE_VER
,
1623 .version_min
= KEEPALIVE_VER_MIN
1626 status
= vchiq_initialise(&instance
);
1627 if (status
!= VCHIQ_SUCCESS
) {
1628 vchiq_log_error(vchiq_susp_log_level
,
1629 "%s vchiq_initialise failed %d", __func__
, status
);
1633 status
= vchiq_connect(instance
);
1634 if (status
!= VCHIQ_SUCCESS
) {
1635 vchiq_log_error(vchiq_susp_log_level
,
1636 "%s vchiq_connect failed %d", __func__
, status
);
1640 status
= vchiq_add_service(instance
, ¶ms
, &ka_handle
);
1641 if (status
!= VCHIQ_SUCCESS
) {
1642 vchiq_log_error(vchiq_susp_log_level
,
1643 "%s vchiq_open_service failed %d", __func__
, status
);
1648 long rc
= 0, uc
= 0;
1649 if (wait_for_completion_interruptible(&arm_state
->ka_evt
)
1651 vchiq_log_error(vchiq_susp_log_level
,
1652 "%s interrupted", __func__
);
1653 flush_signals(current
);
1657 /* read and clear counters. Do release_count then use_count to
1658 * prevent getting more releases than uses */
1659 rc
= atomic_xchg(&arm_state
->ka_release_count
, 0);
1660 uc
= atomic_xchg(&arm_state
->ka_use_count
, 0);
1662 /* Call use/release service the requisite number of times.
1663 * Process use before release so use counts don't go negative */
1665 atomic_inc(&arm_state
->ka_use_ack_count
);
1666 status
= vchiq_use_service(ka_handle
);
1667 if (status
!= VCHIQ_SUCCESS
) {
1668 vchiq_log_error(vchiq_susp_log_level
,
1669 "%s vchiq_use_service error %d",
1674 status
= vchiq_release_service(ka_handle
);
1675 if (status
!= VCHIQ_SUCCESS
) {
1676 vchiq_log_error(vchiq_susp_log_level
,
1677 "%s vchiq_release_service error %d",
1684 vchiq_shutdown(instance
);
1692 vchiq_arm_init_state(VCHIQ_STATE_T
*state
, VCHIQ_ARM_STATE_T
*arm_state
)
1695 rwlock_init(&arm_state
->susp_res_lock
);
1697 init_completion(&arm_state
->ka_evt
);
1698 atomic_set(&arm_state
->ka_use_count
, 0);
1699 atomic_set(&arm_state
->ka_use_ack_count
, 0);
1700 atomic_set(&arm_state
->ka_release_count
, 0);
1702 init_completion(&arm_state
->vc_suspend_complete
);
1704 init_completion(&arm_state
->vc_resume_complete
);
1705 /* Initialise to 'done' state. We only want to block on resume
1706 * completion while videocore is suspended. */
1707 set_resume_state(arm_state
, VC_RESUME_RESUMED
);
1709 init_completion(&arm_state
->resume_blocker
);
1710 /* Initialise to 'done' state. We only want to block on this
1711 * completion while resume is blocked */
1712 complete_all(&arm_state
->resume_blocker
);
1714 init_completion(&arm_state
->blocked_blocker
);
1715 /* Initialise to 'done' state. We only want to block on this
1716 * completion while things are waiting on the resume blocker */
1717 complete_all(&arm_state
->blocked_blocker
);
1719 arm_state
->suspend_timer_timeout
= SUSPEND_TIMER_TIMEOUT_MS
;
1720 arm_state
->suspend_timer_running
= 0;
1721 setup_timer(&arm_state
->suspend_timer
, suspend_timer_callback
,
1722 (unsigned long)(state
));
1724 arm_state
->first_connect
= 0;
1727 return VCHIQ_SUCCESS
;
1731 ** Functions to modify the state variables;
1732 ** set_suspend_state
1735 ** There are more state variables than we might like, so ensure they remain in
1736 ** step. Suspend and resume state are maintained separately, since most of
1737 ** these state machines can operate independently. However, there are a few
1738 ** states where state transitions in one state machine cause a reset to the
1739 ** other state machine. In addition, there are some completion events which
1740 ** need to occur on state machine reset and end-state(s), so these are also
1741 ** dealt with in these functions.
1743 ** In all states we set the state variable according to the input, but in some
1744 ** cases we perform additional steps outlined below;
1746 ** VC_SUSPEND_IDLE - Initialise the suspend completion at the same time.
1747 ** The suspend completion is completed after any suspend
1748 ** attempt. When we reset the state machine we also reset
1749 ** the completion. This reset occurs when videocore is
1750 ** resumed, and also if we initiate suspend after a suspend
1753 ** VC_SUSPEND_IN_PROGRESS - This state is considered the point of no return for
1754 ** suspend - ie from this point on we must try to suspend
1755 ** before resuming can occur. We therefore also reset the
1756 ** resume state machine to VC_RESUME_IDLE in this state.
1758 ** VC_SUSPEND_SUSPENDED - Suspend has completed successfully. Also call
1759 ** complete_all on the suspend completion to notify
1760 ** anything waiting for suspend to happen.
1762 ** VC_SUSPEND_REJECTED - Videocore rejected suspend. Videocore will also
1763 ** initiate resume, so no need to alter resume state.
1764 ** We call complete_all on the suspend completion to notify
1765 ** of suspend rejection.
1767 ** VC_SUSPEND_FAILED - We failed to initiate videocore suspend. We notify the
1768 ** suspend completion and reset the resume state machine.
1770 ** VC_RESUME_IDLE - Initialise the resume completion at the same time. The
1771 ** resume completion is in it's 'done' state whenever
1772 ** videcore is running. Therefore, the VC_RESUME_IDLE
1773 ** state implies that videocore is suspended.
1774 ** Hence, any thread which needs to wait until videocore is
1775 ** running can wait on this completion - it will only block
1776 ** if videocore is suspended.
1778 ** VC_RESUME_RESUMED - Resume has completed successfully. Videocore is running.
1779 ** Call complete_all on the resume completion to unblock
1780 ** any threads waiting for resume. Also reset the suspend
1781 ** state machine to it's idle state.
1783 ** VC_RESUME_FAILED - Currently unused - no mechanism to fail resume exists.
1787 set_suspend_state(VCHIQ_ARM_STATE_T
*arm_state
,
1788 enum vc_suspend_status new_state
)
1790 /* set the state in all cases */
1791 arm_state
->vc_suspend_state
= new_state
;
1793 /* state specific additional actions */
1794 switch (new_state
) {
1795 case VC_SUSPEND_FORCE_CANCELED
:
1796 complete_all(&arm_state
->vc_suspend_complete
);
1798 case VC_SUSPEND_REJECTED
:
1799 complete_all(&arm_state
->vc_suspend_complete
);
1801 case VC_SUSPEND_FAILED
:
1802 complete_all(&arm_state
->vc_suspend_complete
);
1803 arm_state
->vc_resume_state
= VC_RESUME_RESUMED
;
1804 complete_all(&arm_state
->vc_resume_complete
);
1806 case VC_SUSPEND_IDLE
:
1807 reinit_completion(&arm_state
->vc_suspend_complete
);
1809 case VC_SUSPEND_REQUESTED
:
1811 case VC_SUSPEND_IN_PROGRESS
:
1812 set_resume_state(arm_state
, VC_RESUME_IDLE
);
1814 case VC_SUSPEND_SUSPENDED
:
1815 complete_all(&arm_state
->vc_suspend_complete
);
1824 set_resume_state(VCHIQ_ARM_STATE_T
*arm_state
,
1825 enum vc_resume_status new_state
)
1827 /* set the state in all cases */
1828 arm_state
->vc_resume_state
= new_state
;
1830 /* state specific additional actions */
1831 switch (new_state
) {
1832 case VC_RESUME_FAILED
:
1834 case VC_RESUME_IDLE
:
1835 reinit_completion(&arm_state
->vc_resume_complete
);
1837 case VC_RESUME_REQUESTED
:
1839 case VC_RESUME_IN_PROGRESS
:
1841 case VC_RESUME_RESUMED
:
1842 complete_all(&arm_state
->vc_resume_complete
);
1843 set_suspend_state(arm_state
, VC_SUSPEND_IDLE
);
1852 /* should be called with the write lock held */
1854 start_suspend_timer(VCHIQ_ARM_STATE_T
*arm_state
)
1856 del_timer(&arm_state
->suspend_timer
);
1857 arm_state
->suspend_timer
.expires
= jiffies
+
1858 msecs_to_jiffies(arm_state
->
1859 suspend_timer_timeout
);
1860 add_timer(&arm_state
->suspend_timer
);
1861 arm_state
->suspend_timer_running
= 1;
1864 /* should be called with the write lock held */
1866 stop_suspend_timer(VCHIQ_ARM_STATE_T
*arm_state
)
1868 if (arm_state
->suspend_timer_running
) {
1869 del_timer(&arm_state
->suspend_timer
);
1870 arm_state
->suspend_timer_running
= 0;
1875 need_resume(VCHIQ_STATE_T
*state
)
1877 VCHIQ_ARM_STATE_T
*arm_state
= vchiq_platform_get_arm_state(state
);
1878 return (arm_state
->vc_suspend_state
> VC_SUSPEND_IDLE
) &&
1879 (arm_state
->vc_resume_state
< VC_RESUME_REQUESTED
) &&
1880 vchiq_videocore_wanted(state
);
1884 block_resume(VCHIQ_ARM_STATE_T
*arm_state
)
1886 int status
= VCHIQ_SUCCESS
;
1887 const unsigned long timeout_val
=
1888 msecs_to_jiffies(FORCE_SUSPEND_TIMEOUT_MS
);
1889 int resume_count
= 0;
1891 /* Allow any threads which were blocked by the last force suspend to
1892 * complete if they haven't already. Only give this one shot; if
1893 * blocked_count is incremented after blocked_blocker is completed
1894 * (which only happens when blocked_count hits 0) then those threads
1895 * will have to wait until next time around */
1896 if (arm_state
->blocked_count
) {
1897 reinit_completion(&arm_state
->blocked_blocker
);
1898 write_unlock_bh(&arm_state
->susp_res_lock
);
1899 vchiq_log_info(vchiq_susp_log_level
, "%s wait for previously "
1900 "blocked clients", __func__
);
1901 if (wait_for_completion_interruptible_timeout(
1902 &arm_state
->blocked_blocker
, timeout_val
)
1904 vchiq_log_error(vchiq_susp_log_level
, "%s wait for "
1905 "previously blocked clients failed" , __func__
);
1906 status
= VCHIQ_ERROR
;
1907 write_lock_bh(&arm_state
->susp_res_lock
);
1910 vchiq_log_info(vchiq_susp_log_level
, "%s previously blocked "
1911 "clients resumed", __func__
);
1912 write_lock_bh(&arm_state
->susp_res_lock
);
1915 /* We need to wait for resume to complete if it's in process */
1916 while (arm_state
->vc_resume_state
!= VC_RESUME_RESUMED
&&
1917 arm_state
->vc_resume_state
> VC_RESUME_IDLE
) {
1918 if (resume_count
> 1) {
1919 status
= VCHIQ_ERROR
;
1920 vchiq_log_error(vchiq_susp_log_level
, "%s waited too "
1921 "many times for resume" , __func__
);
1924 write_unlock_bh(&arm_state
->susp_res_lock
);
1925 vchiq_log_info(vchiq_susp_log_level
, "%s wait for resume",
1927 if (wait_for_completion_interruptible_timeout(
1928 &arm_state
->vc_resume_complete
, timeout_val
)
1930 vchiq_log_error(vchiq_susp_log_level
, "%s wait for "
1931 "resume failed (%s)", __func__
,
1932 resume_state_names
[arm_state
->vc_resume_state
+
1933 VC_RESUME_NUM_OFFSET
]);
1934 status
= VCHIQ_ERROR
;
1935 write_lock_bh(&arm_state
->susp_res_lock
);
1938 vchiq_log_info(vchiq_susp_log_level
, "%s resumed", __func__
);
1939 write_lock_bh(&arm_state
->susp_res_lock
);
1942 reinit_completion(&arm_state
->resume_blocker
);
1943 arm_state
->resume_blocked
= 1;
1950 unblock_resume(VCHIQ_ARM_STATE_T
*arm_state
)
1952 complete_all(&arm_state
->resume_blocker
);
1953 arm_state
->resume_blocked
= 0;
1956 /* Initiate suspend via slot handler. Should be called with the write lock
1959 vchiq_arm_vcsuspend(VCHIQ_STATE_T
*state
)
1961 VCHIQ_STATUS_T status
= VCHIQ_ERROR
;
1962 VCHIQ_ARM_STATE_T
*arm_state
= vchiq_platform_get_arm_state(state
);
1967 vchiq_log_trace(vchiq_susp_log_level
, "%s", __func__
);
1968 status
= VCHIQ_SUCCESS
;
1971 switch (arm_state
->vc_suspend_state
) {
1972 case VC_SUSPEND_REQUESTED
:
1973 vchiq_log_info(vchiq_susp_log_level
, "%s: suspend already "
1974 "requested", __func__
);
1976 case VC_SUSPEND_IN_PROGRESS
:
1977 vchiq_log_info(vchiq_susp_log_level
, "%s: suspend already in "
1978 "progress", __func__
);
1982 /* We don't expect to be in other states, so log but continue
1984 vchiq_log_error(vchiq_susp_log_level
,
1985 "%s unexpected suspend state %s", __func__
,
1986 suspend_state_names
[arm_state
->vc_suspend_state
+
1987 VC_SUSPEND_NUM_OFFSET
]);
1989 case VC_SUSPEND_REJECTED
:
1990 case VC_SUSPEND_FAILED
:
1991 /* Ensure any idle state actions have been run */
1992 set_suspend_state(arm_state
, VC_SUSPEND_IDLE
);
1994 case VC_SUSPEND_IDLE
:
1995 vchiq_log_info(vchiq_susp_log_level
,
1996 "%s: suspending", __func__
);
1997 set_suspend_state(arm_state
, VC_SUSPEND_REQUESTED
);
1998 /* kick the slot handler thread to initiate suspend */
1999 request_poll(state
, NULL
, 0);
2004 vchiq_log_trace(vchiq_susp_log_level
, "%s exit %d", __func__
, status
);
2009 vchiq_platform_check_suspend(VCHIQ_STATE_T
*state
)
2011 VCHIQ_ARM_STATE_T
*arm_state
= vchiq_platform_get_arm_state(state
);
2017 vchiq_log_trace(vchiq_susp_log_level
, "%s", __func__
);
2019 write_lock_bh(&arm_state
->susp_res_lock
);
2020 if (arm_state
->vc_suspend_state
== VC_SUSPEND_REQUESTED
&&
2021 arm_state
->vc_resume_state
== VC_RESUME_RESUMED
) {
2022 set_suspend_state(arm_state
, VC_SUSPEND_IN_PROGRESS
);
2025 write_unlock_bh(&arm_state
->susp_res_lock
);
2028 vchiq_platform_suspend(state
);
2031 vchiq_log_trace(vchiq_susp_log_level
, "%s exit", __func__
);
2037 output_timeout_error(VCHIQ_STATE_T
*state
)
2039 VCHIQ_ARM_STATE_T
*arm_state
= vchiq_platform_get_arm_state(state
);
2041 int vc_use_count
= arm_state
->videocore_use_count
;
2042 int active_services
= state
->unused_service
;
2045 if (!arm_state
->videocore_use_count
) {
2046 snprintf(err
, sizeof(err
), " Videocore usecount is 0");
2049 for (i
= 0; i
< active_services
; i
++) {
2050 VCHIQ_SERVICE_T
*service_ptr
= state
->services
[i
];
2051 if (service_ptr
&& service_ptr
->service_use_count
&&
2052 (service_ptr
->srvstate
!= VCHIQ_SRVSTATE_FREE
)) {
2053 snprintf(err
, sizeof(err
), " %c%c%c%c(%d) service has "
2054 "use count %d%s", VCHIQ_FOURCC_AS_4CHARS(
2055 service_ptr
->base
.fourcc
),
2056 service_ptr
->client_id
,
2057 service_ptr
->service_use_count
,
2058 service_ptr
->service_use_count
==
2059 vc_use_count
? "" : " (+ more)");
2065 vchiq_log_error(vchiq_susp_log_level
,
2066 "timed out waiting for vc suspend (%d).%s",
2067 arm_state
->autosuspend_override
, err
);
2071 /* Try to get videocore into suspended state, regardless of autosuspend state.
2072 ** We don't actually force suspend, since videocore may get into a bad state
2073 ** if we force suspend at a bad time. Instead, we wait for autosuspend to
2074 ** determine a good point to suspend. If this doesn't happen within 100ms we
2077 ** Returns VCHIQ_SUCCESS if videocore suspended successfully, VCHIQ_RETRY if
2078 ** videocore failed to suspend in time or VCHIQ_ERROR if interrupted.
2081 vchiq_arm_force_suspend(VCHIQ_STATE_T
*state
)
2083 VCHIQ_ARM_STATE_T
*arm_state
= vchiq_platform_get_arm_state(state
);
2084 VCHIQ_STATUS_T status
= VCHIQ_ERROR
;
2091 vchiq_log_trace(vchiq_susp_log_level
, "%s", __func__
);
2093 write_lock_bh(&arm_state
->susp_res_lock
);
2095 status
= block_resume(arm_state
);
2096 if (status
!= VCHIQ_SUCCESS
)
2098 if (arm_state
->vc_suspend_state
== VC_SUSPEND_SUSPENDED
) {
2099 /* Already suspended - just block resume and exit */
2100 vchiq_log_info(vchiq_susp_log_level
, "%s already suspended",
2102 status
= VCHIQ_SUCCESS
;
2104 } else if (arm_state
->vc_suspend_state
<= VC_SUSPEND_IDLE
) {
2105 /* initiate suspend immediately in the case that we're waiting
2106 * for the timeout */
2107 stop_suspend_timer(arm_state
);
2108 if (!vchiq_videocore_wanted(state
)) {
2109 vchiq_log_info(vchiq_susp_log_level
, "%s videocore "
2110 "idle, initiating suspend", __func__
);
2111 status
= vchiq_arm_vcsuspend(state
);
2112 } else if (arm_state
->autosuspend_override
<
2113 FORCE_SUSPEND_FAIL_MAX
) {
2114 vchiq_log_info(vchiq_susp_log_level
, "%s letting "
2115 "videocore go idle", __func__
);
2116 status
= VCHIQ_SUCCESS
;
2118 vchiq_log_warning(vchiq_susp_log_level
, "%s failed too "
2119 "many times - attempting suspend", __func__
);
2120 status
= vchiq_arm_vcsuspend(state
);
2123 vchiq_log_info(vchiq_susp_log_level
, "%s videocore suspend "
2124 "in progress - wait for completion", __func__
);
2125 status
= VCHIQ_SUCCESS
;
2128 /* Wait for suspend to happen due to system idle (not forced..) */
2129 if (status
!= VCHIQ_SUCCESS
)
2130 goto unblock_resume
;
2133 write_unlock_bh(&arm_state
->susp_res_lock
);
2135 rc
= wait_for_completion_interruptible_timeout(
2136 &arm_state
->vc_suspend_complete
,
2137 msecs_to_jiffies(FORCE_SUSPEND_TIMEOUT_MS
));
2139 write_lock_bh(&arm_state
->susp_res_lock
);
2141 vchiq_log_warning(vchiq_susp_log_level
, "%s "
2142 "interrupted waiting for suspend", __func__
);
2143 status
= VCHIQ_ERROR
;
2144 goto unblock_resume
;
2145 } else if (rc
== 0) {
2146 if (arm_state
->vc_suspend_state
> VC_SUSPEND_IDLE
) {
2147 /* Repeat timeout once if in progress */
2153 arm_state
->autosuspend_override
++;
2154 output_timeout_error(state
);
2156 status
= VCHIQ_RETRY
;
2157 goto unblock_resume
;
2159 } while (0 < (repeat
--));
2161 /* Check and report state in case we need to abort ARM suspend */
2162 if (arm_state
->vc_suspend_state
!= VC_SUSPEND_SUSPENDED
) {
2163 status
= VCHIQ_RETRY
;
2164 vchiq_log_error(vchiq_susp_log_level
,
2165 "%s videocore suspend failed (state %s)", __func__
,
2166 suspend_state_names
[arm_state
->vc_suspend_state
+
2167 VC_SUSPEND_NUM_OFFSET
]);
2168 /* Reset the state only if it's still in an error state.
2169 * Something could have already initiated another suspend. */
2170 if (arm_state
->vc_suspend_state
< VC_SUSPEND_IDLE
)
2171 set_suspend_state(arm_state
, VC_SUSPEND_IDLE
);
2173 goto unblock_resume
;
2176 /* successfully suspended - unlock and exit */
2180 /* all error states need to unblock resume before exit */
2181 unblock_resume(arm_state
);
2184 write_unlock_bh(&arm_state
->susp_res_lock
);
2187 vchiq_log_trace(vchiq_susp_log_level
, "%s exit %d", __func__
, status
);
2192 vchiq_check_suspend(VCHIQ_STATE_T
*state
)
2194 VCHIQ_ARM_STATE_T
*arm_state
= vchiq_platform_get_arm_state(state
);
2199 vchiq_log_trace(vchiq_susp_log_level
, "%s", __func__
);
2201 write_lock_bh(&arm_state
->susp_res_lock
);
2202 if (arm_state
->vc_suspend_state
!= VC_SUSPEND_SUSPENDED
&&
2203 arm_state
->first_connect
&&
2204 !vchiq_videocore_wanted(state
)) {
2205 vchiq_arm_vcsuspend(state
);
2207 write_unlock_bh(&arm_state
->susp_res_lock
);
2210 vchiq_log_trace(vchiq_susp_log_level
, "%s exit", __func__
);
2216 vchiq_arm_allow_resume(VCHIQ_STATE_T
*state
)
2218 VCHIQ_ARM_STATE_T
*arm_state
= vchiq_platform_get_arm_state(state
);
2225 vchiq_log_trace(vchiq_susp_log_level
, "%s", __func__
);
2227 write_lock_bh(&arm_state
->susp_res_lock
);
2228 unblock_resume(arm_state
);
2229 resume
= vchiq_check_resume(state
);
2230 write_unlock_bh(&arm_state
->susp_res_lock
);
2233 if (wait_for_completion_interruptible(
2234 &arm_state
->vc_resume_complete
) < 0) {
2235 vchiq_log_error(vchiq_susp_log_level
,
2236 "%s interrupted", __func__
);
2237 /* failed, cannot accurately derive suspend
2238 * state, so exit early. */
2243 read_lock_bh(&arm_state
->susp_res_lock
);
2244 if (arm_state
->vc_suspend_state
== VC_SUSPEND_SUSPENDED
) {
2245 vchiq_log_info(vchiq_susp_log_level
,
2246 "%s: Videocore remains suspended", __func__
);
2248 vchiq_log_info(vchiq_susp_log_level
,
2249 "%s: Videocore resumed", __func__
);
2252 read_unlock_bh(&arm_state
->susp_res_lock
);
2254 vchiq_log_trace(vchiq_susp_log_level
, "%s exit %d", __func__
, ret
);
2258 /* This function should be called with the write lock held */
2260 vchiq_check_resume(VCHIQ_STATE_T
*state
)
2262 VCHIQ_ARM_STATE_T
*arm_state
= vchiq_platform_get_arm_state(state
);
2268 vchiq_log_trace(vchiq_susp_log_level
, "%s", __func__
);
2270 if (need_resume(state
)) {
2271 set_resume_state(arm_state
, VC_RESUME_REQUESTED
);
2272 request_poll(state
, NULL
, 0);
2277 vchiq_log_trace(vchiq_susp_log_level
, "%s exit", __func__
);
2282 vchiq_platform_check_resume(VCHIQ_STATE_T
*state
)
2284 VCHIQ_ARM_STATE_T
*arm_state
= vchiq_platform_get_arm_state(state
);
2290 vchiq_log_trace(vchiq_susp_log_level
, "%s", __func__
);
2292 write_lock_bh(&arm_state
->susp_res_lock
);
2293 if (arm_state
->wake_address
== 0) {
2294 vchiq_log_info(vchiq_susp_log_level
,
2295 "%s: already awake", __func__
);
2298 if (arm_state
->vc_resume_state
== VC_RESUME_IN_PROGRESS
) {
2299 vchiq_log_info(vchiq_susp_log_level
,
2300 "%s: already resuming", __func__
);
2304 if (arm_state
->vc_resume_state
== VC_RESUME_REQUESTED
) {
2305 set_resume_state(arm_state
, VC_RESUME_IN_PROGRESS
);
2308 vchiq_log_trace(vchiq_susp_log_level
,
2309 "%s: not resuming (resume state %s)", __func__
,
2310 resume_state_names
[arm_state
->vc_resume_state
+
2311 VC_RESUME_NUM_OFFSET
]);
2314 write_unlock_bh(&arm_state
->susp_res_lock
);
2317 vchiq_platform_resume(state
);
2320 vchiq_log_trace(vchiq_susp_log_level
, "%s exit", __func__
);
2328 vchiq_use_internal(VCHIQ_STATE_T
*state
, VCHIQ_SERVICE_T
*service
,
2329 enum USE_TYPE_E use_type
)
2331 VCHIQ_ARM_STATE_T
*arm_state
= vchiq_platform_get_arm_state(state
);
2332 VCHIQ_STATUS_T ret
= VCHIQ_SUCCESS
;
2335 int local_uc
, local_entity_uc
;
2340 vchiq_log_trace(vchiq_susp_log_level
, "%s", __func__
);
2342 if (use_type
== USE_TYPE_VCHIQ
) {
2343 sprintf(entity
, "VCHIQ: ");
2344 entity_uc
= &arm_state
->peer_use_count
;
2345 } else if (service
) {
2346 sprintf(entity
, "%c%c%c%c:%03d",
2347 VCHIQ_FOURCC_AS_4CHARS(service
->base
.fourcc
),
2348 service
->client_id
);
2349 entity_uc
= &service
->service_use_count
;
2351 vchiq_log_error(vchiq_susp_log_level
, "%s null service "
2357 write_lock_bh(&arm_state
->susp_res_lock
);
2358 while (arm_state
->resume_blocked
) {
2359 /* If we call 'use' while force suspend is waiting for suspend,
2360 * then we're about to block the thread which the force is
2361 * waiting to complete, so we're bound to just time out. In this
2362 * case, set the suspend state such that the wait will be
2363 * canceled, so we can complete as quickly as possible. */
2364 if (arm_state
->resume_blocked
&& arm_state
->vc_suspend_state
==
2366 set_suspend_state(arm_state
, VC_SUSPEND_FORCE_CANCELED
);
2369 /* If suspend is already in progress then we need to block */
2370 if (!try_wait_for_completion(&arm_state
->resume_blocker
)) {
2371 /* Indicate that there are threads waiting on the resume
2372 * blocker. These need to be allowed to complete before
2373 * a _second_ call to force suspend can complete,
2374 * otherwise low priority threads might never actually
2376 arm_state
->blocked_count
++;
2377 write_unlock_bh(&arm_state
->susp_res_lock
);
2378 vchiq_log_info(vchiq_susp_log_level
, "%s %s resume "
2379 "blocked - waiting...", __func__
, entity
);
2380 if (wait_for_completion_killable(
2381 &arm_state
->resume_blocker
) != 0) {
2382 vchiq_log_error(vchiq_susp_log_level
, "%s %s "
2383 "wait for resume blocker interrupted",
2386 write_lock_bh(&arm_state
->susp_res_lock
);
2387 arm_state
->blocked_count
--;
2388 write_unlock_bh(&arm_state
->susp_res_lock
);
2391 vchiq_log_info(vchiq_susp_log_level
, "%s %s resume "
2392 "unblocked", __func__
, entity
);
2393 write_lock_bh(&arm_state
->susp_res_lock
);
2394 if (--arm_state
->blocked_count
== 0)
2395 complete_all(&arm_state
->blocked_blocker
);
2399 stop_suspend_timer(arm_state
);
2401 local_uc
= ++arm_state
->videocore_use_count
;
2402 local_entity_uc
= ++(*entity_uc
);
2404 /* If there's a pending request which hasn't yet been serviced then
2405 * just clear it. If we're past VC_SUSPEND_REQUESTED state then
2406 * vc_resume_complete will block until we either resume or fail to
2408 if (arm_state
->vc_suspend_state
<= VC_SUSPEND_REQUESTED
)
2409 set_suspend_state(arm_state
, VC_SUSPEND_IDLE
);
2411 if ((use_type
!= USE_TYPE_SERVICE_NO_RESUME
) && need_resume(state
)) {
2412 set_resume_state(arm_state
, VC_RESUME_REQUESTED
);
2413 vchiq_log_info(vchiq_susp_log_level
,
2414 "%s %s count %d, state count %d",
2415 __func__
, entity
, local_entity_uc
, local_uc
);
2416 request_poll(state
, NULL
, 0);
2418 vchiq_log_trace(vchiq_susp_log_level
,
2419 "%s %s count %d, state count %d",
2420 __func__
, entity
, *entity_uc
, local_uc
);
2423 write_unlock_bh(&arm_state
->susp_res_lock
);
2425 /* Completion is in a done state when we're not suspended, so this won't
2426 * block for the non-suspended case. */
2427 if (!try_wait_for_completion(&arm_state
->vc_resume_complete
)) {
2428 vchiq_log_info(vchiq_susp_log_level
, "%s %s wait for resume",
2430 if (wait_for_completion_killable(
2431 &arm_state
->vc_resume_complete
) != 0) {
2432 vchiq_log_error(vchiq_susp_log_level
, "%s %s wait for "
2433 "resume interrupted", __func__
, entity
);
2437 vchiq_log_info(vchiq_susp_log_level
, "%s %s resumed", __func__
,
2441 if (ret
== VCHIQ_SUCCESS
) {
2442 VCHIQ_STATUS_T status
= VCHIQ_SUCCESS
;
2443 long ack_cnt
= atomic_xchg(&arm_state
->ka_use_ack_count
, 0);
2444 while (ack_cnt
&& (status
== VCHIQ_SUCCESS
)) {
2445 /* Send the use notify to videocore */
2446 status
= vchiq_send_remote_use_active(state
);
2447 if (status
== VCHIQ_SUCCESS
)
2451 &arm_state
->ka_use_ack_count
);
2456 vchiq_log_trace(vchiq_susp_log_level
, "%s exit %d", __func__
, ret
);
2461 vchiq_release_internal(VCHIQ_STATE_T
*state
, VCHIQ_SERVICE_T
*service
)
2463 VCHIQ_ARM_STATE_T
*arm_state
= vchiq_platform_get_arm_state(state
);
2464 VCHIQ_STATUS_T ret
= VCHIQ_SUCCESS
;
2467 int local_uc
, local_entity_uc
;
2472 vchiq_log_trace(vchiq_susp_log_level
, "%s", __func__
);
2475 sprintf(entity
, "%c%c%c%c:%03d",
2476 VCHIQ_FOURCC_AS_4CHARS(service
->base
.fourcc
),
2477 service
->client_id
);
2478 entity_uc
= &service
->service_use_count
;
2480 sprintf(entity
, "PEER: ");
2481 entity_uc
= &arm_state
->peer_use_count
;
2484 write_lock_bh(&arm_state
->susp_res_lock
);
2485 if (!arm_state
->videocore_use_count
|| !(*entity_uc
)) {
2486 /* Don't use BUG_ON - don't allow user thread to crash kernel */
2487 WARN_ON(!arm_state
->videocore_use_count
);
2488 WARN_ON(!(*entity_uc
));
2492 local_uc
= --arm_state
->videocore_use_count
;
2493 local_entity_uc
= --(*entity_uc
);
2495 if (!vchiq_videocore_wanted(state
)) {
2496 if (vchiq_platform_use_suspend_timer() &&
2497 !arm_state
->resume_blocked
) {
2498 /* Only use the timer if we're not trying to force
2499 * suspend (=> resume_blocked) */
2500 start_suspend_timer(arm_state
);
2502 vchiq_log_info(vchiq_susp_log_level
,
2503 "%s %s count %d, state count %d - suspending",
2504 __func__
, entity
, *entity_uc
,
2505 arm_state
->videocore_use_count
);
2506 vchiq_arm_vcsuspend(state
);
2509 vchiq_log_trace(vchiq_susp_log_level
,
2510 "%s %s count %d, state count %d",
2511 __func__
, entity
, *entity_uc
,
2512 arm_state
->videocore_use_count
);
2515 write_unlock_bh(&arm_state
->susp_res_lock
);
2518 vchiq_log_trace(vchiq_susp_log_level
, "%s exit %d", __func__
, ret
);
2523 vchiq_on_remote_use(VCHIQ_STATE_T
*state
)
2525 VCHIQ_ARM_STATE_T
*arm_state
= vchiq_platform_get_arm_state(state
);
2526 vchiq_log_trace(vchiq_susp_log_level
, "%s", __func__
);
2527 atomic_inc(&arm_state
->ka_use_count
);
2528 complete(&arm_state
->ka_evt
);
2532 vchiq_on_remote_release(VCHIQ_STATE_T
*state
)
2534 VCHIQ_ARM_STATE_T
*arm_state
= vchiq_platform_get_arm_state(state
);
2535 vchiq_log_trace(vchiq_susp_log_level
, "%s", __func__
);
2536 atomic_inc(&arm_state
->ka_release_count
);
2537 complete(&arm_state
->ka_evt
);
2541 vchiq_use_service_internal(VCHIQ_SERVICE_T
*service
)
2543 return vchiq_use_internal(service
->state
, service
, USE_TYPE_SERVICE
);
2547 vchiq_release_service_internal(VCHIQ_SERVICE_T
*service
)
2549 return vchiq_release_internal(service
->state
, service
);
2552 VCHIQ_DEBUGFS_NODE_T
*
2553 vchiq_instance_get_debugfs_node(VCHIQ_INSTANCE_T instance
)
2555 return &instance
->debugfs_node
;
2559 vchiq_instance_get_use_count(VCHIQ_INSTANCE_T instance
)
2561 VCHIQ_SERVICE_T
*service
;
2562 int use_count
= 0, i
;
2564 while ((service
= next_service_by_instance(instance
->state
,
2565 instance
, &i
)) != NULL
) {
2566 use_count
+= service
->service_use_count
;
2567 unlock_service(service
);
2573 vchiq_instance_get_pid(VCHIQ_INSTANCE_T instance
)
2575 return instance
->pid
;
2579 vchiq_instance_get_trace(VCHIQ_INSTANCE_T instance
)
2581 return instance
->trace
;
2585 vchiq_instance_set_trace(VCHIQ_INSTANCE_T instance
, int trace
)
2587 VCHIQ_SERVICE_T
*service
;
2590 while ((service
= next_service_by_instance(instance
->state
,
2591 instance
, &i
)) != NULL
) {
2592 service
->trace
= trace
;
2593 unlock_service(service
);
2595 instance
->trace
= (trace
!= 0);
2598 static void suspend_timer_callback(unsigned long context
)
2600 VCHIQ_STATE_T
*state
= (VCHIQ_STATE_T
*)context
;
2601 VCHIQ_ARM_STATE_T
*arm_state
= vchiq_platform_get_arm_state(state
);
2604 vchiq_log_info(vchiq_susp_log_level
,
2605 "%s - suspend timer expired - check suspend", __func__
);
2606 vchiq_check_suspend(state
);
2612 vchiq_use_service_no_resume(VCHIQ_SERVICE_HANDLE_T handle
)
2614 VCHIQ_STATUS_T ret
= VCHIQ_ERROR
;
2615 VCHIQ_SERVICE_T
*service
= find_service_by_handle(handle
);
2617 ret
= vchiq_use_internal(service
->state
, service
,
2618 USE_TYPE_SERVICE_NO_RESUME
);
2619 unlock_service(service
);
2625 vchiq_use_service(VCHIQ_SERVICE_HANDLE_T handle
)
2627 VCHIQ_STATUS_T ret
= VCHIQ_ERROR
;
2628 VCHIQ_SERVICE_T
*service
= find_service_by_handle(handle
);
2630 ret
= vchiq_use_internal(service
->state
, service
,
2632 unlock_service(service
);
2638 vchiq_release_service(VCHIQ_SERVICE_HANDLE_T handle
)
2640 VCHIQ_STATUS_T ret
= VCHIQ_ERROR
;
2641 VCHIQ_SERVICE_T
*service
= find_service_by_handle(handle
);
2643 ret
= vchiq_release_internal(service
->state
, service
);
2644 unlock_service(service
);
2650 vchiq_dump_service_use_state(VCHIQ_STATE_T
*state
)
2652 VCHIQ_ARM_STATE_T
*arm_state
= vchiq_platform_get_arm_state(state
);
2654 /* Only dump 64 services */
2655 static const int local_max_services
= 64;
2656 /* If there's more than 64 services, only dump ones with
2657 * non-zero counts */
2658 int only_nonzero
= 0;
2659 static const char *nz
= "<-- preventing suspend";
2661 enum vc_suspend_status vc_suspend_state
;
2662 enum vc_resume_status vc_resume_state
;
2665 int active_services
;
2666 struct service_data_struct
{
2670 } service_data
[local_max_services
];
2675 read_lock_bh(&arm_state
->susp_res_lock
);
2676 vc_suspend_state
= arm_state
->vc_suspend_state
;
2677 vc_resume_state
= arm_state
->vc_resume_state
;
2678 peer_count
= arm_state
->peer_use_count
;
2679 vc_use_count
= arm_state
->videocore_use_count
;
2680 active_services
= state
->unused_service
;
2681 if (active_services
> local_max_services
)
2684 for (i
= 0; (i
< active_services
) && (j
< local_max_services
); i
++) {
2685 VCHIQ_SERVICE_T
*service_ptr
= state
->services
[i
];
2689 if (only_nonzero
&& !service_ptr
->service_use_count
)
2692 if (service_ptr
->srvstate
!= VCHIQ_SRVSTATE_FREE
) {
2693 service_data
[j
].fourcc
= service_ptr
->base
.fourcc
;
2694 service_data
[j
].clientid
= service_ptr
->client_id
;
2695 service_data
[j
++].use_count
= service_ptr
->
2700 read_unlock_bh(&arm_state
->susp_res_lock
);
2702 vchiq_log_warning(vchiq_susp_log_level
,
2703 "-- Videcore suspend state: %s --",
2704 suspend_state_names
[vc_suspend_state
+ VC_SUSPEND_NUM_OFFSET
]);
2705 vchiq_log_warning(vchiq_susp_log_level
,
2706 "-- Videcore resume state: %s --",
2707 resume_state_names
[vc_resume_state
+ VC_RESUME_NUM_OFFSET
]);
2710 vchiq_log_warning(vchiq_susp_log_level
, "Too many active "
2711 "services (%d). Only dumping up to first %d services "
2712 "with non-zero use-count", active_services
,
2713 local_max_services
);
2715 for (i
= 0; i
< j
; i
++) {
2716 vchiq_log_warning(vchiq_susp_log_level
,
2717 "----- %c%c%c%c:%d service count %d %s",
2718 VCHIQ_FOURCC_AS_4CHARS(service_data
[i
].fourcc
),
2719 service_data
[i
].clientid
,
2720 service_data
[i
].use_count
,
2721 service_data
[i
].use_count
? nz
: "");
2723 vchiq_log_warning(vchiq_susp_log_level
,
2724 "----- VCHIQ use count count %d", peer_count
);
2725 vchiq_log_warning(vchiq_susp_log_level
,
2726 "--- Overall vchiq instance use count %d", vc_use_count
);
2728 vchiq_dump_platform_use_state(state
);
2732 vchiq_check_service(VCHIQ_SERVICE_T
*service
)
2734 VCHIQ_ARM_STATE_T
*arm_state
;
2735 VCHIQ_STATUS_T ret
= VCHIQ_ERROR
;
2737 if (!service
|| !service
->state
)
2740 vchiq_log_trace(vchiq_susp_log_level
, "%s", __func__
);
2742 arm_state
= vchiq_platform_get_arm_state(service
->state
);
2744 read_lock_bh(&arm_state
->susp_res_lock
);
2745 if (service
->service_use_count
)
2746 ret
= VCHIQ_SUCCESS
;
2747 read_unlock_bh(&arm_state
->susp_res_lock
);
2749 if (ret
== VCHIQ_ERROR
) {
2750 vchiq_log_error(vchiq_susp_log_level
,
2751 "%s ERROR - %c%c%c%c:%d service count %d, "
2752 "state count %d, videocore suspend state %s", __func__
,
2753 VCHIQ_FOURCC_AS_4CHARS(service
->base
.fourcc
),
2754 service
->client_id
, service
->service_use_count
,
2755 arm_state
->videocore_use_count
,
2756 suspend_state_names
[arm_state
->vc_suspend_state
+
2757 VC_SUSPEND_NUM_OFFSET
]);
2758 vchiq_dump_service_use_state(service
->state
);
2764 /* stub functions */
2765 void vchiq_on_remote_use_active(VCHIQ_STATE_T
*state
)
2770 void vchiq_platform_conn_state_changed(VCHIQ_STATE_T
*state
,
2771 VCHIQ_CONNSTATE_T oldstate
, VCHIQ_CONNSTATE_T newstate
)
2773 VCHIQ_ARM_STATE_T
*arm_state
= vchiq_platform_get_arm_state(state
);
2774 vchiq_log_info(vchiq_susp_log_level
, "%d: %s->%s", state
->id
,
2775 get_conn_state_name(oldstate
), get_conn_state_name(newstate
));
2776 if (state
->conn_state
== VCHIQ_CONNSTATE_CONNECTED
) {
2777 write_lock_bh(&arm_state
->susp_res_lock
);
2778 if (!arm_state
->first_connect
) {
2779 char threadname
[10];
2780 arm_state
->first_connect
= 1;
2781 write_unlock_bh(&arm_state
->susp_res_lock
);
2782 snprintf(threadname
, sizeof(threadname
), "VCHIQka-%d",
2784 arm_state
->ka_thread
= kthread_create(
2785 &vchiq_keepalive_thread_func
,
2788 if (IS_ERR(arm_state
->ka_thread
)) {
2789 vchiq_log_error(vchiq_susp_log_level
,
2790 "vchiq: FATAL: couldn't create thread %s",
2793 wake_up_process(arm_state
->ka_thread
);
2796 write_unlock_bh(&arm_state
->susp_res_lock
);
2800 static int vchiq_probe(struct platform_device
*pdev
)
2802 struct device_node
*fw_node
;
2803 struct rpi_firmware
*fw
;
2807 fw_node
= of_parse_phandle(pdev
->dev
.of_node
, "firmware", 0);
2809 dev_err(&pdev
->dev
, "Missing firmware node\n");
2813 fw
= rpi_firmware_get(fw_node
);
2814 of_node_put(fw_node
);
2816 return -EPROBE_DEFER
;
2818 platform_set_drvdata(pdev
, fw
);
2820 err
= vchiq_platform_init(pdev
, &g_state
);
2822 goto failed_platform_init
;
2824 err
= alloc_chrdev_region(&vchiq_devid
, VCHIQ_MINOR
, 1, DEVICE_NAME
);
2826 vchiq_log_error(vchiq_arm_log_level
,
2827 "Unable to allocate device number");
2828 goto failed_platform_init
;
2830 cdev_init(&vchiq_cdev
, &vchiq_fops
);
2831 vchiq_cdev
.owner
= THIS_MODULE
;
2832 err
= cdev_add(&vchiq_cdev
, vchiq_devid
, 1);
2834 vchiq_log_error(vchiq_arm_log_level
,
2835 "Unable to register device");
2836 goto failed_cdev_add
;
2839 /* create sysfs entries */
2840 vchiq_class
= class_create(THIS_MODULE
, DEVICE_NAME
);
2841 ptr_err
= vchiq_class
;
2842 if (IS_ERR(ptr_err
))
2843 goto failed_class_create
;
2845 vchiq_dev
= device_create(vchiq_class
, NULL
,
2846 vchiq_devid
, NULL
, "vchiq");
2847 ptr_err
= vchiq_dev
;
2848 if (IS_ERR(ptr_err
))
2849 goto failed_device_create
;
2851 /* create debugfs entries */
2852 err
= vchiq_debugfs_init();
2854 goto failed_debugfs_init
;
2856 vchiq_log_info(vchiq_arm_log_level
,
2857 "vchiq: initialised - version %d (min %d), device %d.%d",
2858 VCHIQ_VERSION
, VCHIQ_VERSION_MIN
,
2859 MAJOR(vchiq_devid
), MINOR(vchiq_devid
));
2863 failed_debugfs_init
:
2864 device_destroy(vchiq_class
, vchiq_devid
);
2865 failed_device_create
:
2866 class_destroy(vchiq_class
);
2867 failed_class_create
:
2868 cdev_del(&vchiq_cdev
);
2869 err
= PTR_ERR(ptr_err
);
2871 unregister_chrdev_region(vchiq_devid
, 1);
2872 failed_platform_init
:
2873 vchiq_log_warning(vchiq_arm_log_level
, "could not load vchiq");
2877 static int vchiq_remove(struct platform_device
*pdev
)
2879 vchiq_debugfs_deinit();
2880 device_destroy(vchiq_class
, vchiq_devid
);
2881 class_destroy(vchiq_class
);
2882 cdev_del(&vchiq_cdev
);
2883 unregister_chrdev_region(vchiq_devid
, 1);
2888 static const struct of_device_id vchiq_of_match
[] = {
2889 { .compatible
= "brcm,bcm2835-vchiq", },
2892 MODULE_DEVICE_TABLE(of
, vchiq_of_match
);
2894 static struct platform_driver vchiq_driver
= {
2896 .name
= "bcm2835_vchiq",
2897 .of_match_table
= vchiq_of_match
,
2899 .probe
= vchiq_probe
,
2900 .remove
= vchiq_remove
,
2902 module_platform_driver(vchiq_driver
);
2904 MODULE_LICENSE("GPL");
2905 MODULE_DESCRIPTION("Videocore VCHIQ driver");
2906 MODULE_AUTHOR("Broadcom Corporation");