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((sizeof(ioctl_names
)/sizeof(ioctl_names
[0])) ==
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 DEBUG_INITIALISE(g_state
.local
)
284 DEBUG_TRACE(SERVICE_CALLBACK_LINE
);
286 service
= handle_to_service(handle
);
288 user_service
= (USER_SERVICE_T
*)service
->base
.userdata
;
289 instance
= user_service
->instance
;
291 if (!instance
|| instance
->closing
)
292 return VCHIQ_SUCCESS
;
294 vchiq_log_trace(vchiq_arm_log_level
,
295 "service_callback - service %lx(%d,%p), reason %d, header %lx, "
296 "instance %lx, bulk_userdata %lx",
297 (unsigned long)user_service
,
298 service
->localport
, user_service
->userdata
,
299 reason
, (unsigned long)header
,
300 (unsigned long)instance
, (unsigned long)bulk_userdata
);
302 if (header
&& user_service
->is_vchi
) {
303 spin_lock(&msg_queue_spinlock
);
304 while (user_service
->msg_insert
==
305 (user_service
->msg_remove
+ MSG_QUEUE_SIZE
)) {
306 spin_unlock(&msg_queue_spinlock
);
307 DEBUG_TRACE(SERVICE_CALLBACK_LINE
);
308 DEBUG_COUNT(MSG_QUEUE_FULL_COUNT
);
309 vchiq_log_trace(vchiq_arm_log_level
,
310 "service_callback - msg queue full");
311 /* If there is no MESSAGE_AVAILABLE in the completion
314 if ((user_service
->message_available_pos
-
315 instance
->completion_remove
) < 0) {
316 VCHIQ_STATUS_T status
;
317 vchiq_log_info(vchiq_arm_log_level
,
318 "Inserting extra MESSAGE_AVAILABLE");
319 DEBUG_TRACE(SERVICE_CALLBACK_LINE
);
320 status
= add_completion(instance
, reason
,
321 NULL
, user_service
, bulk_userdata
);
322 if (status
!= VCHIQ_SUCCESS
) {
323 DEBUG_TRACE(SERVICE_CALLBACK_LINE
);
328 DEBUG_TRACE(SERVICE_CALLBACK_LINE
);
329 if (down_interruptible(&user_service
->remove_event
)
331 vchiq_log_info(vchiq_arm_log_level
,
332 "service_callback interrupted");
333 DEBUG_TRACE(SERVICE_CALLBACK_LINE
);
335 } else if (instance
->closing
) {
336 vchiq_log_info(vchiq_arm_log_level
,
337 "service_callback closing");
338 DEBUG_TRACE(SERVICE_CALLBACK_LINE
);
341 DEBUG_TRACE(SERVICE_CALLBACK_LINE
);
342 spin_lock(&msg_queue_spinlock
);
345 user_service
->msg_queue
[user_service
->msg_insert
&
346 (MSG_QUEUE_SIZE
- 1)] = header
;
347 user_service
->msg_insert
++;
348 spin_unlock(&msg_queue_spinlock
);
350 up(&user_service
->insert_event
);
352 /* If there is a thread waiting in DEQUEUE_MESSAGE, or if
353 ** there is a MESSAGE_AVAILABLE in the completion queue then
354 ** bypass the completion queue.
356 if (((user_service
->message_available_pos
-
357 instance
->completion_remove
) >= 0) ||
358 user_service
->dequeue_pending
) {
359 DEBUG_TRACE(SERVICE_CALLBACK_LINE
);
360 user_service
->dequeue_pending
= 0;
361 return VCHIQ_SUCCESS
;
366 DEBUG_TRACE(SERVICE_CALLBACK_LINE
);
368 return add_completion(instance
, reason
, header
, user_service
,
372 /****************************************************************************
376 ***************************************************************************/
378 user_service_free(void *userdata
)
383 /****************************************************************************
387 ***************************************************************************/
388 static void close_delivered(USER_SERVICE_T
*user_service
)
390 vchiq_log_info(vchiq_arm_log_level
,
391 "close_delivered(handle=%x)",
392 user_service
->service
->handle
);
394 if (user_service
->close_pending
) {
395 /* Allow the underlying service to be culled */
396 unlock_service(user_service
->service
);
398 /* Wake the user-thread blocked in close_ or remove_service */
399 up(&user_service
->close_event
);
401 user_service
->close_pending
= 0;
405 /****************************************************************************
409 ***************************************************************************/
411 vchiq_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
413 VCHIQ_INSTANCE_T instance
= file
->private_data
;
414 VCHIQ_STATUS_T status
= VCHIQ_SUCCESS
;
415 VCHIQ_SERVICE_T
*service
= NULL
;
418 DEBUG_INITIALISE(g_state
.local
)
420 vchiq_log_trace(vchiq_arm_log_level
,
421 "vchiq_ioctl - instance %pK, cmd %s, arg %lx",
423 ((_IOC_TYPE(cmd
) == VCHIQ_IOC_MAGIC
) &&
424 (_IOC_NR(cmd
) <= VCHIQ_IOC_MAX
)) ?
425 ioctl_names
[_IOC_NR(cmd
)] : "<invalid>", arg
);
428 case VCHIQ_IOC_SHUTDOWN
:
429 if (!instance
->connected
)
432 /* Remove all services */
434 while ((service
= next_service_by_instance(instance
->state
,
435 instance
, &i
)) != NULL
) {
436 status
= vchiq_remove_service(service
->handle
);
437 unlock_service(service
);
438 if (status
!= VCHIQ_SUCCESS
)
443 if (status
== VCHIQ_SUCCESS
) {
444 /* Wake the completion thread and ask it to exit */
445 instance
->closing
= 1;
446 up(&instance
->insert_event
);
451 case VCHIQ_IOC_CONNECT
:
452 if (instance
->connected
) {
456 rc
= mutex_lock_interruptible(&instance
->state
->mutex
);
458 vchiq_log_error(vchiq_arm_log_level
,
459 "vchiq: connect: could not lock mutex for "
461 instance
->state
->id
, rc
);
465 status
= vchiq_connect_internal(instance
->state
, instance
);
466 mutex_unlock(&instance
->state
->mutex
);
468 if (status
== VCHIQ_SUCCESS
)
469 instance
->connected
= 1;
471 vchiq_log_error(vchiq_arm_log_level
,
472 "vchiq: could not connect: %d", status
);
475 case VCHIQ_IOC_CREATE_SERVICE
: {
476 VCHIQ_CREATE_SERVICE_T args
;
477 USER_SERVICE_T
*user_service
= NULL
;
482 (&args
, (const void __user
*)arg
,
483 sizeof(args
)) != 0) {
488 user_service
= kmalloc(sizeof(USER_SERVICE_T
), GFP_KERNEL
);
495 if (!instance
->connected
) {
500 srvstate
= VCHIQ_SRVSTATE_OPENING
;
503 instance
->connected
?
504 VCHIQ_SRVSTATE_LISTENING
:
505 VCHIQ_SRVSTATE_HIDDEN
;
508 userdata
= args
.params
.userdata
;
509 args
.params
.callback
= service_callback
;
510 args
.params
.userdata
= user_service
;
511 service
= vchiq_add_service_internal(
513 &args
.params
, srvstate
,
514 instance
, user_service_free
);
516 if (service
!= NULL
) {
517 user_service
->service
= service
;
518 user_service
->userdata
= userdata
;
519 user_service
->instance
= instance
;
520 user_service
->is_vchi
= (args
.is_vchi
!= 0);
521 user_service
->dequeue_pending
= 0;
522 user_service
->close_pending
= 0;
523 user_service
->message_available_pos
=
524 instance
->completion_remove
- 1;
525 user_service
->msg_insert
= 0;
526 user_service
->msg_remove
= 0;
527 sema_init(&user_service
->insert_event
, 0);
528 sema_init(&user_service
->remove_event
, 0);
529 sema_init(&user_service
->close_event
, 0);
532 status
= vchiq_open_service_internal
533 (service
, instance
->pid
);
534 if (status
!= VCHIQ_SUCCESS
) {
535 vchiq_remove_service(service
->handle
);
537 ret
= (status
== VCHIQ_RETRY
) ?
543 if (copy_to_user((void __user
*)
544 &(((VCHIQ_CREATE_SERVICE_T __user
*)
546 (const void *)&service
->handle
,
547 sizeof(service
->handle
)) != 0) {
549 vchiq_remove_service(service
->handle
);
559 case VCHIQ_IOC_CLOSE_SERVICE
: {
560 VCHIQ_SERVICE_HANDLE_T handle
= (VCHIQ_SERVICE_HANDLE_T
)arg
;
562 service
= find_service_for_instance(instance
, handle
);
563 if (service
!= NULL
) {
564 USER_SERVICE_T
*user_service
=
565 (USER_SERVICE_T
*)service
->base
.userdata
;
566 /* close_pending is false on first entry, and when the
567 wait in vchiq_close_service has been interrupted. */
568 if (!user_service
->close_pending
) {
569 status
= vchiq_close_service(service
->handle
);
570 if (status
!= VCHIQ_SUCCESS
)
574 /* close_pending is true once the underlying service
575 has been closed until the client library calls the
576 CLOSE_DELIVERED ioctl, signalling close_event. */
577 if (user_service
->close_pending
&&
578 down_interruptible(&user_service
->close_event
))
579 status
= VCHIQ_RETRY
;
585 case VCHIQ_IOC_REMOVE_SERVICE
: {
586 VCHIQ_SERVICE_HANDLE_T handle
= (VCHIQ_SERVICE_HANDLE_T
)arg
;
588 service
= find_service_for_instance(instance
, handle
);
589 if (service
!= NULL
) {
590 USER_SERVICE_T
*user_service
=
591 (USER_SERVICE_T
*)service
->base
.userdata
;
592 /* close_pending is false on first entry, and when the
593 wait in vchiq_close_service has been interrupted. */
594 if (!user_service
->close_pending
) {
595 status
= vchiq_remove_service(service
->handle
);
596 if (status
!= VCHIQ_SUCCESS
)
600 /* close_pending is true once the underlying service
601 has been closed until the client library calls the
602 CLOSE_DELIVERED ioctl, signalling close_event. */
603 if (user_service
->close_pending
&&
604 down_interruptible(&user_service
->close_event
))
605 status
= VCHIQ_RETRY
;
611 case VCHIQ_IOC_USE_SERVICE
:
612 case VCHIQ_IOC_RELEASE_SERVICE
: {
613 VCHIQ_SERVICE_HANDLE_T handle
= (VCHIQ_SERVICE_HANDLE_T
)arg
;
615 service
= find_service_for_instance(instance
, handle
);
616 if (service
!= NULL
) {
617 status
= (cmd
== VCHIQ_IOC_USE_SERVICE
) ?
618 vchiq_use_service_internal(service
) :
619 vchiq_release_service_internal(service
);
620 if (status
!= VCHIQ_SUCCESS
) {
621 vchiq_log_error(vchiq_susp_log_level
,
622 "%s: cmd %s returned error %d for "
623 "service %c%c%c%c:%03d",
625 (cmd
== VCHIQ_IOC_USE_SERVICE
) ?
626 "VCHIQ_IOC_USE_SERVICE" :
627 "VCHIQ_IOC_RELEASE_SERVICE",
629 VCHIQ_FOURCC_AS_4CHARS(
630 service
->base
.fourcc
),
638 case VCHIQ_IOC_QUEUE_MESSAGE
: {
639 VCHIQ_QUEUE_MESSAGE_T args
;
641 (&args
, (const void __user
*)arg
,
642 sizeof(args
)) != 0) {
647 service
= find_service_for_instance(instance
, args
.handle
);
649 if ((service
!= NULL
) && (args
.count
<= MAX_ELEMENTS
)) {
650 /* Copy elements into kernel space */
651 VCHIQ_ELEMENT_T elements
[MAX_ELEMENTS
];
652 if (copy_from_user(elements
, args
.elements
,
653 args
.count
* sizeof(VCHIQ_ELEMENT_T
)) == 0)
654 status
= vchiq_queue_message
656 elements
, args
.count
);
664 case VCHIQ_IOC_QUEUE_BULK_TRANSMIT
:
665 case VCHIQ_IOC_QUEUE_BULK_RECEIVE
: {
666 VCHIQ_QUEUE_BULK_TRANSFER_T args
;
667 struct bulk_waiter_node
*waiter
= NULL
;
668 VCHIQ_BULK_DIR_T dir
=
669 (cmd
== VCHIQ_IOC_QUEUE_BULK_TRANSMIT
) ?
670 VCHIQ_BULK_TRANSMIT
: VCHIQ_BULK_RECEIVE
;
673 (&args
, (const void __user
*)arg
,
674 sizeof(args
)) != 0) {
679 service
= find_service_for_instance(instance
, args
.handle
);
685 if (args
.mode
== VCHIQ_BULK_MODE_BLOCKING
) {
686 waiter
= kzalloc(sizeof(struct bulk_waiter_node
),
692 args
.userdata
= &waiter
->bulk_waiter
;
693 } else if (args
.mode
== VCHIQ_BULK_MODE_WAITING
) {
694 struct list_head
*pos
;
695 mutex_lock(&instance
->bulk_waiter_list_mutex
);
696 list_for_each(pos
, &instance
->bulk_waiter_list
) {
697 if (list_entry(pos
, struct bulk_waiter_node
,
698 list
)->pid
== current
->pid
) {
699 waiter
= list_entry(pos
,
700 struct bulk_waiter_node
,
707 mutex_unlock(&instance
->bulk_waiter_list_mutex
);
709 vchiq_log_error(vchiq_arm_log_level
,
710 "no bulk_waiter found for pid %d",
715 vchiq_log_info(vchiq_arm_log_level
,
716 "found bulk_waiter %pK for pid %d", waiter
,
718 args
.userdata
= &waiter
->bulk_waiter
;
720 status
= vchiq_bulk_transfer
722 VCHI_MEM_HANDLE_INVALID
,
723 args
.data
, args
.size
,
724 args
.userdata
, args
.mode
,
728 if ((status
!= VCHIQ_RETRY
) || fatal_signal_pending(current
) ||
729 !waiter
->bulk_waiter
.bulk
) {
730 if (waiter
->bulk_waiter
.bulk
) {
731 /* Cancel the signal when the transfer
733 spin_lock(&bulk_waiter_spinlock
);
734 waiter
->bulk_waiter
.bulk
->userdata
= NULL
;
735 spin_unlock(&bulk_waiter_spinlock
);
739 const VCHIQ_BULK_MODE_T mode_waiting
=
740 VCHIQ_BULK_MODE_WAITING
;
741 waiter
->pid
= current
->pid
;
742 mutex_lock(&instance
->bulk_waiter_list_mutex
);
743 list_add(&waiter
->list
, &instance
->bulk_waiter_list
);
744 mutex_unlock(&instance
->bulk_waiter_list_mutex
);
745 vchiq_log_info(vchiq_arm_log_level
,
746 "saved bulk_waiter %pK for pid %d",
747 waiter
, current
->pid
);
749 if (copy_to_user((void __user
*)
750 &(((VCHIQ_QUEUE_BULK_TRANSFER_T __user
*)
752 (const void *)&mode_waiting
,
753 sizeof(mode_waiting
)) != 0)
758 case VCHIQ_IOC_AWAIT_COMPLETION
: {
759 VCHIQ_AWAIT_COMPLETION_T args
;
761 DEBUG_TRACE(AWAIT_COMPLETION_LINE
);
762 if (!instance
->connected
) {
767 if (copy_from_user(&args
, (const void __user
*)arg
,
768 sizeof(args
)) != 0) {
773 mutex_lock(&instance
->completion_mutex
);
775 DEBUG_TRACE(AWAIT_COMPLETION_LINE
);
776 while ((instance
->completion_remove
==
777 instance
->completion_insert
)
778 && !instance
->closing
) {
780 DEBUG_TRACE(AWAIT_COMPLETION_LINE
);
781 mutex_unlock(&instance
->completion_mutex
);
782 rc
= down_interruptible(&instance
->insert_event
);
783 mutex_lock(&instance
->completion_mutex
);
785 DEBUG_TRACE(AWAIT_COMPLETION_LINE
);
786 vchiq_log_info(vchiq_arm_log_level
,
787 "AWAIT_COMPLETION interrupted");
792 DEBUG_TRACE(AWAIT_COMPLETION_LINE
);
794 /* A read memory barrier is needed to stop prefetch of a stale
800 int msgbufcount
= args
.msgbufcount
;
801 for (ret
= 0; ret
< args
.count
; ret
++) {
802 VCHIQ_COMPLETION_DATA_T
*completion
;
803 VCHIQ_SERVICE_T
*service
;
804 USER_SERVICE_T
*user_service
;
805 VCHIQ_HEADER_T
*header
;
806 if (instance
->completion_remove
==
807 instance
->completion_insert
)
809 completion
= &instance
->completions
[
810 instance
->completion_remove
&
811 (MAX_COMPLETIONS
- 1)];
813 service
= completion
->service_userdata
;
814 user_service
= service
->base
.userdata
;
815 completion
->service_userdata
=
816 user_service
->userdata
;
818 header
= completion
->header
;
823 msglen
= header
->size
+
824 sizeof(VCHIQ_HEADER_T
);
825 /* This must be a VCHIQ-style service */
826 if (args
.msgbufsize
< msglen
) {
829 "header %pK: msgbufsize %x < msglen %x",
830 header
, args
.msgbufsize
,
832 WARN(1, "invalid message "
838 if (msgbufcount
<= 0)
839 /* Stall here for lack of a
840 ** buffer for the message. */
842 /* Get the pointer from user space */
844 if (copy_from_user(&msgbuf
,
845 (const void __user
*)
846 &args
.msgbufs
[msgbufcount
],
847 sizeof(msgbuf
)) != 0) {
853 /* Copy the message to user space */
854 if (copy_to_user(msgbuf
, header
,
861 /* Now it has been copied, the message
862 ** can be released. */
863 vchiq_release_message(service
->handle
,
866 /* The completion must point to the
868 completion
->header
= msgbuf
;
871 if ((completion
->reason
==
872 VCHIQ_SERVICE_CLOSED
) &&
873 !instance
->use_close_delivered
)
874 unlock_service(service
);
876 if (copy_to_user((void __user
*)(
878 ret
* sizeof(VCHIQ_COMPLETION_DATA_T
)),
880 sizeof(VCHIQ_COMPLETION_DATA_T
)) != 0) {
886 instance
->completion_remove
++;
889 if (msgbufcount
!= args
.msgbufcount
) {
890 if (copy_to_user((void __user
*)
891 &((VCHIQ_AWAIT_COMPLETION_T
*)arg
)->
894 sizeof(msgbufcount
)) != 0) {
901 up(&instance
->remove_event
);
902 mutex_unlock(&instance
->completion_mutex
);
903 DEBUG_TRACE(AWAIT_COMPLETION_LINE
);
906 case VCHIQ_IOC_DEQUEUE_MESSAGE
: {
907 VCHIQ_DEQUEUE_MESSAGE_T args
;
908 USER_SERVICE_T
*user_service
;
909 VCHIQ_HEADER_T
*header
;
911 DEBUG_TRACE(DEQUEUE_MESSAGE_LINE
);
913 (&args
, (const void __user
*)arg
,
914 sizeof(args
)) != 0) {
918 service
= find_service_for_instance(instance
, args
.handle
);
923 user_service
= (USER_SERVICE_T
*)service
->base
.userdata
;
924 if (user_service
->is_vchi
== 0) {
929 spin_lock(&msg_queue_spinlock
);
930 if (user_service
->msg_remove
== user_service
->msg_insert
) {
931 if (!args
.blocking
) {
932 spin_unlock(&msg_queue_spinlock
);
933 DEBUG_TRACE(DEQUEUE_MESSAGE_LINE
);
937 user_service
->dequeue_pending
= 1;
939 spin_unlock(&msg_queue_spinlock
);
940 DEBUG_TRACE(DEQUEUE_MESSAGE_LINE
);
941 if (down_interruptible(
942 &user_service
->insert_event
) != 0) {
943 vchiq_log_info(vchiq_arm_log_level
,
944 "DEQUEUE_MESSAGE interrupted");
948 spin_lock(&msg_queue_spinlock
);
949 } while (user_service
->msg_remove
==
950 user_service
->msg_insert
);
956 BUG_ON((int)(user_service
->msg_insert
-
957 user_service
->msg_remove
) < 0);
959 header
= user_service
->msg_queue
[user_service
->msg_remove
&
960 (MSG_QUEUE_SIZE
- 1)];
961 user_service
->msg_remove
++;
962 spin_unlock(&msg_queue_spinlock
);
964 up(&user_service
->remove_event
);
967 else if (header
->size
<= args
.bufsize
) {
968 /* Copy to user space if msgbuf is not NULL */
969 if ((args
.buf
== NULL
) ||
970 (copy_to_user((void __user
*)args
.buf
,
972 header
->size
) == 0)) {
974 vchiq_release_message(
980 vchiq_log_error(vchiq_arm_log_level
,
981 "header %pK: bufsize %x < size %x",
982 header
, args
.bufsize
, header
->size
);
983 WARN(1, "invalid size\n");
986 DEBUG_TRACE(DEQUEUE_MESSAGE_LINE
);
989 case VCHIQ_IOC_GET_CLIENT_ID
: {
990 VCHIQ_SERVICE_HANDLE_T handle
= (VCHIQ_SERVICE_HANDLE_T
)arg
;
992 ret
= vchiq_get_client_id(handle
);
995 case VCHIQ_IOC_GET_CONFIG
: {
996 VCHIQ_GET_CONFIG_T args
;
997 VCHIQ_CONFIG_T config
;
999 if (copy_from_user(&args
, (const void __user
*)arg
,
1000 sizeof(args
)) != 0) {
1004 if (args
.config_size
> sizeof(config
)) {
1008 status
= vchiq_get_config(instance
, args
.config_size
, &config
);
1009 if (status
== VCHIQ_SUCCESS
) {
1010 if (copy_to_user((void __user
*)args
.pconfig
,
1011 &config
, args
.config_size
) != 0) {
1018 case VCHIQ_IOC_SET_SERVICE_OPTION
: {
1019 VCHIQ_SET_SERVICE_OPTION_T args
;
1022 &args
, (const void __user
*)arg
,
1023 sizeof(args
)) != 0) {
1028 service
= find_service_for_instance(instance
, args
.handle
);
1034 status
= vchiq_set_service_option(
1035 args
.handle
, args
.option
, args
.value
);
1038 case VCHIQ_IOC_DUMP_PHYS_MEM
: {
1039 VCHIQ_DUMP_MEM_T args
;
1042 (&args
, (const void __user
*)arg
,
1043 sizeof(args
)) != 0) {
1047 dump_phys_mem(args
.virt_addr
, args
.num_bytes
);
1050 case VCHIQ_IOC_LIB_VERSION
: {
1051 unsigned int lib_version
= (unsigned int)arg
;
1053 if (lib_version
< VCHIQ_VERSION_MIN
)
1055 else if (lib_version
>= VCHIQ_VERSION_CLOSE_DELIVERED
)
1056 instance
->use_close_delivered
= 1;
1059 case VCHIQ_IOC_CLOSE_DELIVERED
: {
1060 VCHIQ_SERVICE_HANDLE_T handle
= (VCHIQ_SERVICE_HANDLE_T
)arg
;
1062 service
= find_closed_service_for_instance(instance
, handle
);
1063 if (service
!= NULL
) {
1064 USER_SERVICE_T
*user_service
=
1065 (USER_SERVICE_T
*)service
->base
.userdata
;
1066 close_delivered(user_service
);
1078 unlock_service(service
);
1081 if (status
== VCHIQ_ERROR
)
1083 else if (status
== VCHIQ_RETRY
)
1087 if ((status
== VCHIQ_SUCCESS
) && (ret
< 0) && (ret
!= -EINTR
) &&
1088 (ret
!= -EWOULDBLOCK
))
1089 vchiq_log_info(vchiq_arm_log_level
,
1090 " ioctl instance %lx, cmd %s -> status %d, %ld",
1091 (unsigned long)instance
,
1092 (_IOC_NR(cmd
) <= VCHIQ_IOC_MAX
) ?
1093 ioctl_names
[_IOC_NR(cmd
)] :
1097 vchiq_log_trace(vchiq_arm_log_level
,
1098 " ioctl instance %lx, cmd %s -> status %d, %ld",
1099 (unsigned long)instance
,
1100 (_IOC_NR(cmd
) <= VCHIQ_IOC_MAX
) ?
1101 ioctl_names
[_IOC_NR(cmd
)] :
1108 /****************************************************************************
1112 ***************************************************************************/
1115 vchiq_open(struct inode
*inode
, struct file
*file
)
1117 int dev
= iminor(inode
) & 0x0f;
1118 vchiq_log_info(vchiq_arm_log_level
, "vchiq_open");
1122 VCHIQ_STATE_T
*state
= vchiq_get_state();
1123 VCHIQ_INSTANCE_T instance
;
1126 vchiq_log_error(vchiq_arm_log_level
,
1127 "vchiq has no connection to VideoCore");
1131 instance
= kzalloc(sizeof(*instance
), GFP_KERNEL
);
1135 instance
->state
= state
;
1136 instance
->pid
= current
->tgid
;
1138 ret
= vchiq_debugfs_add_instance(instance
);
1144 sema_init(&instance
->insert_event
, 0);
1145 sema_init(&instance
->remove_event
, 0);
1146 mutex_init(&instance
->completion_mutex
);
1147 mutex_init(&instance
->bulk_waiter_list_mutex
);
1148 INIT_LIST_HEAD(&instance
->bulk_waiter_list
);
1150 file
->private_data
= instance
;
1154 vchiq_log_error(vchiq_arm_log_level
,
1155 "Unknown minor device: %d", dev
);
1162 /****************************************************************************
1166 ***************************************************************************/
1169 vchiq_release(struct inode
*inode
, struct file
*file
)
1171 int dev
= iminor(inode
) & 0x0f;
1175 VCHIQ_INSTANCE_T instance
= file
->private_data
;
1176 VCHIQ_STATE_T
*state
= vchiq_get_state();
1177 VCHIQ_SERVICE_T
*service
;
1180 vchiq_log_info(vchiq_arm_log_level
,
1181 "vchiq_release: instance=%lx",
1182 (unsigned long)instance
);
1189 /* Ensure videocore is awake to allow termination. */
1190 vchiq_use_internal(instance
->state
, NULL
,
1193 mutex_lock(&instance
->completion_mutex
);
1195 /* Wake the completion thread and ask it to exit */
1196 instance
->closing
= 1;
1197 up(&instance
->insert_event
);
1199 mutex_unlock(&instance
->completion_mutex
);
1201 /* Wake the slot handler if the completion queue is full. */
1202 up(&instance
->remove_event
);
1204 /* Mark all services for termination... */
1206 while ((service
= next_service_by_instance(state
, instance
,
1208 USER_SERVICE_T
*user_service
= service
->base
.userdata
;
1210 /* Wake the slot handler if the msg queue is full. */
1211 up(&user_service
->remove_event
);
1213 vchiq_terminate_service_internal(service
);
1214 unlock_service(service
);
1217 /* ...and wait for them to die */
1219 while ((service
= next_service_by_instance(state
, instance
, &i
))
1221 USER_SERVICE_T
*user_service
= service
->base
.userdata
;
1223 down(&service
->remove_event
);
1225 BUG_ON(service
->srvstate
!= VCHIQ_SRVSTATE_FREE
);
1227 spin_lock(&msg_queue_spinlock
);
1229 while (user_service
->msg_remove
!=
1230 user_service
->msg_insert
) {
1231 VCHIQ_HEADER_T
*header
= user_service
->
1232 msg_queue
[user_service
->msg_remove
&
1233 (MSG_QUEUE_SIZE
- 1)];
1234 user_service
->msg_remove
++;
1235 spin_unlock(&msg_queue_spinlock
);
1238 vchiq_release_message(
1241 spin_lock(&msg_queue_spinlock
);
1244 spin_unlock(&msg_queue_spinlock
);
1246 unlock_service(service
);
1249 /* Release any closed services */
1250 while (instance
->completion_remove
!=
1251 instance
->completion_insert
) {
1252 VCHIQ_COMPLETION_DATA_T
*completion
;
1253 VCHIQ_SERVICE_T
*service
;
1254 completion
= &instance
->completions
[
1255 instance
->completion_remove
&
1256 (MAX_COMPLETIONS
- 1)];
1257 service
= completion
->service_userdata
;
1258 if (completion
->reason
== VCHIQ_SERVICE_CLOSED
)
1260 USER_SERVICE_T
*user_service
=
1261 service
->base
.userdata
;
1263 /* Wake any blocked user-thread */
1264 if (instance
->use_close_delivered
)
1265 up(&user_service
->close_event
);
1266 unlock_service(service
);
1268 instance
->completion_remove
++;
1271 /* Release the PEER service count. */
1272 vchiq_release_internal(instance
->state
, NULL
);
1275 struct list_head
*pos
, *next
;
1276 list_for_each_safe(pos
, next
,
1277 &instance
->bulk_waiter_list
) {
1278 struct bulk_waiter_node
*waiter
;
1279 waiter
= list_entry(pos
,
1280 struct bulk_waiter_node
,
1283 vchiq_log_info(vchiq_arm_log_level
,
1284 "bulk_waiter - cleaned up %pK for pid %d",
1285 waiter
, waiter
->pid
);
1290 vchiq_debugfs_remove_instance(instance
);
1293 file
->private_data
= NULL
;
1297 vchiq_log_error(vchiq_arm_log_level
,
1298 "Unknown minor device: %d", dev
);
1306 /****************************************************************************
1310 ***************************************************************************/
1313 vchiq_dump(void *dump_context
, const char *str
, int len
)
1315 DUMP_CONTEXT_T
*context
= (DUMP_CONTEXT_T
*)dump_context
;
1317 if (context
->actual
< context
->space
) {
1319 if (context
->offset
> 0) {
1320 int skip_bytes
= min(len
, (int)context
->offset
);
1323 context
->offset
-= skip_bytes
;
1324 if (context
->offset
> 0)
1327 copy_bytes
= min(len
, (int)(context
->space
- context
->actual
));
1328 if (copy_bytes
== 0)
1330 if (copy_to_user(context
->buf
+ context
->actual
, str
,
1332 context
->actual
= -EFAULT
;
1333 context
->actual
+= copy_bytes
;
1336 /* If tne terminating NUL is included in the length, then it
1337 ** marks the end of a line and should be replaced with a
1338 ** carriage return. */
1339 if ((len
== 0) && (str
[copy_bytes
- 1] == '\0')) {
1341 if (copy_to_user(context
->buf
+ context
->actual
- 1,
1343 context
->actual
= -EFAULT
;
1348 /****************************************************************************
1350 * vchiq_dump_platform_instance_state
1352 ***************************************************************************/
1355 vchiq_dump_platform_instances(void *dump_context
)
1357 VCHIQ_STATE_T
*state
= vchiq_get_state();
1362 /* There is no list of instances, so instead scan all services,
1363 marking those that have been dumped. */
1365 for (i
= 0; i
< state
->unused_service
; i
++) {
1366 VCHIQ_SERVICE_T
*service
= state
->services
[i
];
1367 VCHIQ_INSTANCE_T instance
;
1369 if (service
&& (service
->base
.callback
== service_callback
)) {
1370 instance
= service
->instance
;
1376 for (i
= 0; i
< state
->unused_service
; i
++) {
1377 VCHIQ_SERVICE_T
*service
= state
->services
[i
];
1378 VCHIQ_INSTANCE_T instance
;
1380 if (service
&& (service
->base
.callback
== service_callback
)) {
1381 instance
= service
->instance
;
1382 if (instance
&& !instance
->mark
) {
1383 len
= snprintf(buf
, sizeof(buf
),
1384 "Instance %pK: pid %d,%s completions %d/%d",
1385 instance
, instance
->pid
,
1386 instance
->connected
? " connected, " :
1388 instance
->completion_insert
-
1389 instance
->completion_remove
,
1392 vchiq_dump(dump_context
, buf
, len
+ 1);
1400 /****************************************************************************
1402 * vchiq_dump_platform_service_state
1404 ***************************************************************************/
1407 vchiq_dump_platform_service_state(void *dump_context
, VCHIQ_SERVICE_T
*service
)
1409 USER_SERVICE_T
*user_service
= (USER_SERVICE_T
*)service
->base
.userdata
;
1413 len
= snprintf(buf
, sizeof(buf
), " instance %pK", service
->instance
);
1415 if ((service
->base
.callback
== service_callback
) &&
1416 user_service
->is_vchi
) {
1417 len
+= snprintf(buf
+ len
, sizeof(buf
) - len
,
1419 user_service
->msg_insert
- user_service
->msg_remove
,
1422 if (user_service
->dequeue_pending
)
1423 len
+= snprintf(buf
+ len
, sizeof(buf
) - len
,
1424 " (dequeue pending)");
1427 vchiq_dump(dump_context
, buf
, len
+ 1);
1430 /****************************************************************************
1434 ***************************************************************************/
1437 dump_phys_mem(void *virt_addr
, uint32_t num_bytes
)
1440 uint8_t *end_virt_addr
= virt_addr
+ num_bytes
;
1447 struct page
**pages
;
1448 uint8_t *kmapped_virt_ptr
;
1450 /* Align virtAddr and endVirtAddr to 16 byte boundaries. */
1452 virt_addr
= (void *)((unsigned long)virt_addr
& ~0x0fuL
);
1453 end_virt_addr
= (void *)(((unsigned long)end_virt_addr
+ 15uL) &
1456 offset
= (int)(long)virt_addr
& (PAGE_SIZE
- 1);
1457 end_offset
= (int)(long)end_virt_addr
& (PAGE_SIZE
- 1);
1459 num_pages
= (offset
+ num_bytes
+ PAGE_SIZE
- 1) / PAGE_SIZE
;
1461 pages
= kmalloc(sizeof(struct page
*) * num_pages
, GFP_KERNEL
);
1462 if (pages
== NULL
) {
1463 vchiq_log_error(vchiq_arm_log_level
,
1464 "Unable to allocation memory for %d pages\n",
1469 down_read(¤t
->mm
->mmap_sem
);
1470 rc
= get_user_pages(
1471 (unsigned long)virt_addr
, /* start */
1472 num_pages
, /* len */
1475 pages
, /* pages (array of page pointers) */
1477 up_read(¤t
->mm
->mmap_sem
);
1482 while (offset
< end_offset
) {
1484 int page_offset
= offset
% PAGE_SIZE
;
1485 page_idx
= offset
/ PAGE_SIZE
;
1487 if (page_idx
!= prev_idx
) {
1491 page
= pages
[page_idx
];
1492 kmapped_virt_ptr
= kmap(page
);
1494 prev_idx
= page_idx
;
1497 if (vchiq_arm_log_level
>= VCHIQ_LOG_TRACE
)
1498 vchiq_log_dump_mem("ph",
1499 (uint32_t)(unsigned long)&kmapped_virt_ptr
[
1501 &kmapped_virt_ptr
[page_offset
], 16);
1508 for (page_idx
= 0; page_idx
< num_pages
; page_idx
++)
1509 put_page(pages
[page_idx
]);
1514 /****************************************************************************
1518 ***************************************************************************/
1521 vchiq_read(struct file
*file
, char __user
*buf
,
1522 size_t count
, loff_t
*ppos
)
1524 DUMP_CONTEXT_T context
;
1527 context
.space
= count
;
1528 context
.offset
= *ppos
;
1530 vchiq_dump_state(&context
, &g_state
);
1532 *ppos
+= context
.actual
;
1534 return context
.actual
;
1538 vchiq_get_state(void)
1541 if (g_state
.remote
== NULL
)
1542 printk(KERN_ERR
"%s: g_state.remote == NULL\n", __func__
);
1543 else if (g_state
.remote
->initialised
!= 1)
1544 printk(KERN_NOTICE
"%s: g_state.remote->initialised != 1 (%d)\n",
1545 __func__
, g_state
.remote
->initialised
);
1547 return ((g_state
.remote
!= NULL
) &&
1548 (g_state
.remote
->initialised
== 1)) ? &g_state
: NULL
;
1551 static const struct file_operations
1553 .owner
= THIS_MODULE
,
1554 .unlocked_ioctl
= vchiq_ioctl
,
1556 .release
= vchiq_release
,
1561 * Autosuspend related functionality
1565 vchiq_videocore_wanted(VCHIQ_STATE_T
*state
)
1567 VCHIQ_ARM_STATE_T
*arm_state
= vchiq_platform_get_arm_state(state
);
1569 /* autosuspend not supported - always return wanted */
1571 else if (arm_state
->blocked_count
)
1573 else if (!arm_state
->videocore_use_count
)
1574 /* usage count zero - check for override unless we're forcing */
1575 if (arm_state
->resume_blocked
)
1578 return vchiq_platform_videocore_wanted(state
);
1580 /* non-zero usage count - videocore still required */
1584 static VCHIQ_STATUS_T
1585 vchiq_keepalive_vchiq_callback(VCHIQ_REASON_T reason
,
1586 VCHIQ_HEADER_T
*header
,
1587 VCHIQ_SERVICE_HANDLE_T service_user
,
1590 vchiq_log_error(vchiq_susp_log_level
,
1591 "%s callback reason %d", __func__
, reason
);
1596 vchiq_keepalive_thread_func(void *v
)
1598 VCHIQ_STATE_T
*state
= (VCHIQ_STATE_T
*) v
;
1599 VCHIQ_ARM_STATE_T
*arm_state
= vchiq_platform_get_arm_state(state
);
1601 VCHIQ_STATUS_T status
;
1602 VCHIQ_INSTANCE_T instance
;
1603 VCHIQ_SERVICE_HANDLE_T ka_handle
;
1605 VCHIQ_SERVICE_PARAMS_T params
= {
1606 .fourcc
= VCHIQ_MAKE_FOURCC('K', 'E', 'E', 'P'),
1607 .callback
= vchiq_keepalive_vchiq_callback
,
1608 .version
= KEEPALIVE_VER
,
1609 .version_min
= KEEPALIVE_VER_MIN
1612 status
= vchiq_initialise(&instance
);
1613 if (status
!= VCHIQ_SUCCESS
) {
1614 vchiq_log_error(vchiq_susp_log_level
,
1615 "%s vchiq_initialise failed %d", __func__
, status
);
1619 status
= vchiq_connect(instance
);
1620 if (status
!= VCHIQ_SUCCESS
) {
1621 vchiq_log_error(vchiq_susp_log_level
,
1622 "%s vchiq_connect failed %d", __func__
, status
);
1626 status
= vchiq_add_service(instance
, ¶ms
, &ka_handle
);
1627 if (status
!= VCHIQ_SUCCESS
) {
1628 vchiq_log_error(vchiq_susp_log_level
,
1629 "%s vchiq_open_service failed %d", __func__
, status
);
1634 long rc
= 0, uc
= 0;
1635 if (wait_for_completion_interruptible(&arm_state
->ka_evt
)
1637 vchiq_log_error(vchiq_susp_log_level
,
1638 "%s interrupted", __func__
);
1639 flush_signals(current
);
1643 /* read and clear counters. Do release_count then use_count to
1644 * prevent getting more releases than uses */
1645 rc
= atomic_xchg(&arm_state
->ka_release_count
, 0);
1646 uc
= atomic_xchg(&arm_state
->ka_use_count
, 0);
1648 /* Call use/release service the requisite number of times.
1649 * Process use before release so use counts don't go negative */
1651 atomic_inc(&arm_state
->ka_use_ack_count
);
1652 status
= vchiq_use_service(ka_handle
);
1653 if (status
!= VCHIQ_SUCCESS
) {
1654 vchiq_log_error(vchiq_susp_log_level
,
1655 "%s vchiq_use_service error %d",
1660 status
= vchiq_release_service(ka_handle
);
1661 if (status
!= VCHIQ_SUCCESS
) {
1662 vchiq_log_error(vchiq_susp_log_level
,
1663 "%s vchiq_release_service error %d",
1670 vchiq_shutdown(instance
);
1678 vchiq_arm_init_state(VCHIQ_STATE_T
*state
, VCHIQ_ARM_STATE_T
*arm_state
)
1680 VCHIQ_STATUS_T status
= VCHIQ_SUCCESS
;
1683 rwlock_init(&arm_state
->susp_res_lock
);
1685 init_completion(&arm_state
->ka_evt
);
1686 atomic_set(&arm_state
->ka_use_count
, 0);
1687 atomic_set(&arm_state
->ka_use_ack_count
, 0);
1688 atomic_set(&arm_state
->ka_release_count
, 0);
1690 init_completion(&arm_state
->vc_suspend_complete
);
1692 init_completion(&arm_state
->vc_resume_complete
);
1693 /* Initialise to 'done' state. We only want to block on resume
1694 * completion while videocore is suspended. */
1695 set_resume_state(arm_state
, VC_RESUME_RESUMED
);
1697 init_completion(&arm_state
->resume_blocker
);
1698 /* Initialise to 'done' state. We only want to block on this
1699 * completion while resume is blocked */
1700 complete_all(&arm_state
->resume_blocker
);
1702 init_completion(&arm_state
->blocked_blocker
);
1703 /* Initialise to 'done' state. We only want to block on this
1704 * completion while things are waiting on the resume blocker */
1705 complete_all(&arm_state
->blocked_blocker
);
1707 arm_state
->suspend_timer_timeout
= SUSPEND_TIMER_TIMEOUT_MS
;
1708 arm_state
->suspend_timer_running
= 0;
1709 init_timer(&arm_state
->suspend_timer
);
1710 arm_state
->suspend_timer
.data
= (unsigned long)(state
);
1711 arm_state
->suspend_timer
.function
= suspend_timer_callback
;
1713 arm_state
->first_connect
= 0;
1720 ** Functions to modify the state variables;
1721 ** set_suspend_state
1724 ** There are more state variables than we might like, so ensure they remain in
1725 ** step. Suspend and resume state are maintained separately, since most of
1726 ** these state machines can operate independently. However, there are a few
1727 ** states where state transitions in one state machine cause a reset to the
1728 ** other state machine. In addition, there are some completion events which
1729 ** need to occur on state machine reset and end-state(s), so these are also
1730 ** dealt with in these functions.
1732 ** In all states we set the state variable according to the input, but in some
1733 ** cases we perform additional steps outlined below;
1735 ** VC_SUSPEND_IDLE - Initialise the suspend completion at the same time.
1736 ** The suspend completion is completed after any suspend
1737 ** attempt. When we reset the state machine we also reset
1738 ** the completion. This reset occurs when videocore is
1739 ** resumed, and also if we initiate suspend after a suspend
1742 ** VC_SUSPEND_IN_PROGRESS - This state is considered the point of no return for
1743 ** suspend - ie from this point on we must try to suspend
1744 ** before resuming can occur. We therefore also reset the
1745 ** resume state machine to VC_RESUME_IDLE in this state.
1747 ** VC_SUSPEND_SUSPENDED - Suspend has completed successfully. Also call
1748 ** complete_all on the suspend completion to notify
1749 ** anything waiting for suspend to happen.
1751 ** VC_SUSPEND_REJECTED - Videocore rejected suspend. Videocore will also
1752 ** initiate resume, so no need to alter resume state.
1753 ** We call complete_all on the suspend completion to notify
1754 ** of suspend rejection.
1756 ** VC_SUSPEND_FAILED - We failed to initiate videocore suspend. We notify the
1757 ** suspend completion and reset the resume state machine.
1759 ** VC_RESUME_IDLE - Initialise the resume completion at the same time. The
1760 ** resume completion is in it's 'done' state whenever
1761 ** videcore is running. Therfore, the VC_RESUME_IDLE state
1762 ** implies that videocore is suspended.
1763 ** Hence, any thread which needs to wait until videocore is
1764 ** running can wait on this completion - it will only block
1765 ** if videocore is suspended.
1767 ** VC_RESUME_RESUMED - Resume has completed successfully. Videocore is running.
1768 ** Call complete_all on the resume completion to unblock
1769 ** any threads waiting for resume. Also reset the suspend
1770 ** state machine to it's idle state.
1772 ** VC_RESUME_FAILED - Currently unused - no mechanism to fail resume exists.
1776 set_suspend_state(VCHIQ_ARM_STATE_T
*arm_state
,
1777 enum vc_suspend_status new_state
)
1779 /* set the state in all cases */
1780 arm_state
->vc_suspend_state
= new_state
;
1782 /* state specific additional actions */
1783 switch (new_state
) {
1784 case VC_SUSPEND_FORCE_CANCELED
:
1785 complete_all(&arm_state
->vc_suspend_complete
);
1787 case VC_SUSPEND_REJECTED
:
1788 complete_all(&arm_state
->vc_suspend_complete
);
1790 case VC_SUSPEND_FAILED
:
1791 complete_all(&arm_state
->vc_suspend_complete
);
1792 arm_state
->vc_resume_state
= VC_RESUME_RESUMED
;
1793 complete_all(&arm_state
->vc_resume_complete
);
1795 case VC_SUSPEND_IDLE
:
1796 reinit_completion(&arm_state
->vc_suspend_complete
);
1798 case VC_SUSPEND_REQUESTED
:
1800 case VC_SUSPEND_IN_PROGRESS
:
1801 set_resume_state(arm_state
, VC_RESUME_IDLE
);
1803 case VC_SUSPEND_SUSPENDED
:
1804 complete_all(&arm_state
->vc_suspend_complete
);
1813 set_resume_state(VCHIQ_ARM_STATE_T
*arm_state
,
1814 enum vc_resume_status new_state
)
1816 /* set the state in all cases */
1817 arm_state
->vc_resume_state
= new_state
;
1819 /* state specific additional actions */
1820 switch (new_state
) {
1821 case VC_RESUME_FAILED
:
1823 case VC_RESUME_IDLE
:
1824 reinit_completion(&arm_state
->vc_resume_complete
);
1826 case VC_RESUME_REQUESTED
:
1828 case VC_RESUME_IN_PROGRESS
:
1830 case VC_RESUME_RESUMED
:
1831 complete_all(&arm_state
->vc_resume_complete
);
1832 set_suspend_state(arm_state
, VC_SUSPEND_IDLE
);
1841 /* should be called with the write lock held */
1843 start_suspend_timer(VCHIQ_ARM_STATE_T
*arm_state
)
1845 del_timer(&arm_state
->suspend_timer
);
1846 arm_state
->suspend_timer
.expires
= jiffies
+
1847 msecs_to_jiffies(arm_state
->
1848 suspend_timer_timeout
);
1849 add_timer(&arm_state
->suspend_timer
);
1850 arm_state
->suspend_timer_running
= 1;
1853 /* should be called with the write lock held */
1855 stop_suspend_timer(VCHIQ_ARM_STATE_T
*arm_state
)
1857 if (arm_state
->suspend_timer_running
) {
1858 del_timer(&arm_state
->suspend_timer
);
1859 arm_state
->suspend_timer_running
= 0;
1864 need_resume(VCHIQ_STATE_T
*state
)
1866 VCHIQ_ARM_STATE_T
*arm_state
= vchiq_platform_get_arm_state(state
);
1867 return (arm_state
->vc_suspend_state
> VC_SUSPEND_IDLE
) &&
1868 (arm_state
->vc_resume_state
< VC_RESUME_REQUESTED
) &&
1869 vchiq_videocore_wanted(state
);
1873 block_resume(VCHIQ_ARM_STATE_T
*arm_state
)
1875 int status
= VCHIQ_SUCCESS
;
1876 const unsigned long timeout_val
=
1877 msecs_to_jiffies(FORCE_SUSPEND_TIMEOUT_MS
);
1878 int resume_count
= 0;
1880 /* Allow any threads which were blocked by the last force suspend to
1881 * complete if they haven't already. Only give this one shot; if
1882 * blocked_count is incremented after blocked_blocker is completed
1883 * (which only happens when blocked_count hits 0) then those threads
1884 * will have to wait until next time around */
1885 if (arm_state
->blocked_count
) {
1886 reinit_completion(&arm_state
->blocked_blocker
);
1887 write_unlock_bh(&arm_state
->susp_res_lock
);
1888 vchiq_log_info(vchiq_susp_log_level
, "%s wait for previously "
1889 "blocked clients", __func__
);
1890 if (wait_for_completion_interruptible_timeout(
1891 &arm_state
->blocked_blocker
, timeout_val
)
1893 vchiq_log_error(vchiq_susp_log_level
, "%s wait for "
1894 "previously blocked clients failed" , __func__
);
1895 status
= VCHIQ_ERROR
;
1896 write_lock_bh(&arm_state
->susp_res_lock
);
1899 vchiq_log_info(vchiq_susp_log_level
, "%s previously blocked "
1900 "clients resumed", __func__
);
1901 write_lock_bh(&arm_state
->susp_res_lock
);
1904 /* We need to wait for resume to complete if it's in process */
1905 while (arm_state
->vc_resume_state
!= VC_RESUME_RESUMED
&&
1906 arm_state
->vc_resume_state
> VC_RESUME_IDLE
) {
1907 if (resume_count
> 1) {
1908 status
= VCHIQ_ERROR
;
1909 vchiq_log_error(vchiq_susp_log_level
, "%s waited too "
1910 "many times for resume" , __func__
);
1913 write_unlock_bh(&arm_state
->susp_res_lock
);
1914 vchiq_log_info(vchiq_susp_log_level
, "%s wait for resume",
1916 if (wait_for_completion_interruptible_timeout(
1917 &arm_state
->vc_resume_complete
, timeout_val
)
1919 vchiq_log_error(vchiq_susp_log_level
, "%s wait for "
1920 "resume failed (%s)", __func__
,
1921 resume_state_names
[arm_state
->vc_resume_state
+
1922 VC_RESUME_NUM_OFFSET
]);
1923 status
= VCHIQ_ERROR
;
1924 write_lock_bh(&arm_state
->susp_res_lock
);
1927 vchiq_log_info(vchiq_susp_log_level
, "%s resumed", __func__
);
1928 write_lock_bh(&arm_state
->susp_res_lock
);
1931 reinit_completion(&arm_state
->resume_blocker
);
1932 arm_state
->resume_blocked
= 1;
1939 unblock_resume(VCHIQ_ARM_STATE_T
*arm_state
)
1941 complete_all(&arm_state
->resume_blocker
);
1942 arm_state
->resume_blocked
= 0;
1945 /* Initiate suspend via slot handler. Should be called with the write lock
1948 vchiq_arm_vcsuspend(VCHIQ_STATE_T
*state
)
1950 VCHIQ_STATUS_T status
= VCHIQ_ERROR
;
1951 VCHIQ_ARM_STATE_T
*arm_state
= vchiq_platform_get_arm_state(state
);
1956 vchiq_log_trace(vchiq_susp_log_level
, "%s", __func__
);
1957 status
= VCHIQ_SUCCESS
;
1960 switch (arm_state
->vc_suspend_state
) {
1961 case VC_SUSPEND_REQUESTED
:
1962 vchiq_log_info(vchiq_susp_log_level
, "%s: suspend already "
1963 "requested", __func__
);
1965 case VC_SUSPEND_IN_PROGRESS
:
1966 vchiq_log_info(vchiq_susp_log_level
, "%s: suspend already in "
1967 "progress", __func__
);
1971 /* We don't expect to be in other states, so log but continue
1973 vchiq_log_error(vchiq_susp_log_level
,
1974 "%s unexpected suspend state %s", __func__
,
1975 suspend_state_names
[arm_state
->vc_suspend_state
+
1976 VC_SUSPEND_NUM_OFFSET
]);
1978 case VC_SUSPEND_REJECTED
:
1979 case VC_SUSPEND_FAILED
:
1980 /* Ensure any idle state actions have been run */
1981 set_suspend_state(arm_state
, VC_SUSPEND_IDLE
);
1983 case VC_SUSPEND_IDLE
:
1984 vchiq_log_info(vchiq_susp_log_level
,
1985 "%s: suspending", __func__
);
1986 set_suspend_state(arm_state
, VC_SUSPEND_REQUESTED
);
1987 /* kick the slot handler thread to initiate suspend */
1988 request_poll(state
, NULL
, 0);
1993 vchiq_log_trace(vchiq_susp_log_level
, "%s exit %d", __func__
, status
);
1998 vchiq_platform_check_suspend(VCHIQ_STATE_T
*state
)
2000 VCHIQ_ARM_STATE_T
*arm_state
= vchiq_platform_get_arm_state(state
);
2006 vchiq_log_trace(vchiq_susp_log_level
, "%s", __func__
);
2008 write_lock_bh(&arm_state
->susp_res_lock
);
2009 if (arm_state
->vc_suspend_state
== VC_SUSPEND_REQUESTED
&&
2010 arm_state
->vc_resume_state
== VC_RESUME_RESUMED
) {
2011 set_suspend_state(arm_state
, VC_SUSPEND_IN_PROGRESS
);
2014 write_unlock_bh(&arm_state
->susp_res_lock
);
2017 vchiq_platform_suspend(state
);
2020 vchiq_log_trace(vchiq_susp_log_level
, "%s exit", __func__
);
2026 output_timeout_error(VCHIQ_STATE_T
*state
)
2028 VCHIQ_ARM_STATE_T
*arm_state
= vchiq_platform_get_arm_state(state
);
2029 char service_err
[50] = "";
2030 int vc_use_count
= arm_state
->videocore_use_count
;
2031 int active_services
= state
->unused_service
;
2034 if (!arm_state
->videocore_use_count
) {
2035 snprintf(service_err
, 50, " Videocore usecount is 0");
2038 for (i
= 0; i
< active_services
; i
++) {
2039 VCHIQ_SERVICE_T
*service_ptr
= state
->services
[i
];
2040 if (service_ptr
&& service_ptr
->service_use_count
&&
2041 (service_ptr
->srvstate
!= VCHIQ_SRVSTATE_FREE
)) {
2042 snprintf(service_err
, 50, " %c%c%c%c(%d) service has "
2043 "use count %d%s", VCHIQ_FOURCC_AS_4CHARS(
2044 service_ptr
->base
.fourcc
),
2045 service_ptr
->client_id
,
2046 service_ptr
->service_use_count
,
2047 service_ptr
->service_use_count
==
2048 vc_use_count
? "" : " (+ more)");
2054 vchiq_log_error(vchiq_susp_log_level
,
2055 "timed out waiting for vc suspend (%d).%s",
2056 arm_state
->autosuspend_override
, service_err
);
2060 /* Try to get videocore into suspended state, regardless of autosuspend state.
2061 ** We don't actually force suspend, since videocore may get into a bad state
2062 ** if we force suspend at a bad time. Instead, we wait for autosuspend to
2063 ** determine a good point to suspend. If this doesn't happen within 100ms we
2066 ** Returns VCHIQ_SUCCESS if videocore suspended successfully, VCHIQ_RETRY if
2067 ** videocore failed to suspend in time or VCHIQ_ERROR if interrupted.
2070 vchiq_arm_force_suspend(VCHIQ_STATE_T
*state
)
2072 VCHIQ_ARM_STATE_T
*arm_state
= vchiq_platform_get_arm_state(state
);
2073 VCHIQ_STATUS_T status
= VCHIQ_ERROR
;
2080 vchiq_log_trace(vchiq_susp_log_level
, "%s", __func__
);
2082 write_lock_bh(&arm_state
->susp_res_lock
);
2084 status
= block_resume(arm_state
);
2085 if (status
!= VCHIQ_SUCCESS
)
2087 if (arm_state
->vc_suspend_state
== VC_SUSPEND_SUSPENDED
) {
2088 /* Already suspended - just block resume and exit */
2089 vchiq_log_info(vchiq_susp_log_level
, "%s already suspended",
2091 status
= VCHIQ_SUCCESS
;
2093 } else if (arm_state
->vc_suspend_state
<= VC_SUSPEND_IDLE
) {
2094 /* initiate suspend immediately in the case that we're waiting
2095 * for the timeout */
2096 stop_suspend_timer(arm_state
);
2097 if (!vchiq_videocore_wanted(state
)) {
2098 vchiq_log_info(vchiq_susp_log_level
, "%s videocore "
2099 "idle, initiating suspend", __func__
);
2100 status
= vchiq_arm_vcsuspend(state
);
2101 } else if (arm_state
->autosuspend_override
<
2102 FORCE_SUSPEND_FAIL_MAX
) {
2103 vchiq_log_info(vchiq_susp_log_level
, "%s letting "
2104 "videocore go idle", __func__
);
2105 status
= VCHIQ_SUCCESS
;
2107 vchiq_log_warning(vchiq_susp_log_level
, "%s failed too "
2108 "many times - attempting suspend", __func__
);
2109 status
= vchiq_arm_vcsuspend(state
);
2112 vchiq_log_info(vchiq_susp_log_level
, "%s videocore suspend "
2113 "in progress - wait for completion", __func__
);
2114 status
= VCHIQ_SUCCESS
;
2117 /* Wait for suspend to happen due to system idle (not forced..) */
2118 if (status
!= VCHIQ_SUCCESS
)
2119 goto unblock_resume
;
2122 write_unlock_bh(&arm_state
->susp_res_lock
);
2124 rc
= wait_for_completion_interruptible_timeout(
2125 &arm_state
->vc_suspend_complete
,
2126 msecs_to_jiffies(FORCE_SUSPEND_TIMEOUT_MS
));
2128 write_lock_bh(&arm_state
->susp_res_lock
);
2130 vchiq_log_warning(vchiq_susp_log_level
, "%s "
2131 "interrupted waiting for suspend", __func__
);
2132 status
= VCHIQ_ERROR
;
2133 goto unblock_resume
;
2134 } else if (rc
== 0) {
2135 if (arm_state
->vc_suspend_state
> VC_SUSPEND_IDLE
) {
2136 /* Repeat timeout once if in progress */
2142 arm_state
->autosuspend_override
++;
2143 output_timeout_error(state
);
2145 status
= VCHIQ_RETRY
;
2146 goto unblock_resume
;
2148 } while (0 < (repeat
--));
2150 /* Check and report state in case we need to abort ARM suspend */
2151 if (arm_state
->vc_suspend_state
!= VC_SUSPEND_SUSPENDED
) {
2152 status
= VCHIQ_RETRY
;
2153 vchiq_log_error(vchiq_susp_log_level
,
2154 "%s videocore suspend failed (state %s)", __func__
,
2155 suspend_state_names
[arm_state
->vc_suspend_state
+
2156 VC_SUSPEND_NUM_OFFSET
]);
2157 /* Reset the state only if it's still in an error state.
2158 * Something could have already initiated another suspend. */
2159 if (arm_state
->vc_suspend_state
< VC_SUSPEND_IDLE
)
2160 set_suspend_state(arm_state
, VC_SUSPEND_IDLE
);
2162 goto unblock_resume
;
2165 /* successfully suspended - unlock and exit */
2169 /* all error states need to unblock resume before exit */
2170 unblock_resume(arm_state
);
2173 write_unlock_bh(&arm_state
->susp_res_lock
);
2176 vchiq_log_trace(vchiq_susp_log_level
, "%s exit %d", __func__
, status
);
2181 vchiq_check_suspend(VCHIQ_STATE_T
*state
)
2183 VCHIQ_ARM_STATE_T
*arm_state
= vchiq_platform_get_arm_state(state
);
2188 vchiq_log_trace(vchiq_susp_log_level
, "%s", __func__
);
2190 write_lock_bh(&arm_state
->susp_res_lock
);
2191 if (arm_state
->vc_suspend_state
!= VC_SUSPEND_SUSPENDED
&&
2192 arm_state
->first_connect
&&
2193 !vchiq_videocore_wanted(state
)) {
2194 vchiq_arm_vcsuspend(state
);
2196 write_unlock_bh(&arm_state
->susp_res_lock
);
2199 vchiq_log_trace(vchiq_susp_log_level
, "%s exit", __func__
);
2205 vchiq_arm_allow_resume(VCHIQ_STATE_T
*state
)
2207 VCHIQ_ARM_STATE_T
*arm_state
= vchiq_platform_get_arm_state(state
);
2214 vchiq_log_trace(vchiq_susp_log_level
, "%s", __func__
);
2216 write_lock_bh(&arm_state
->susp_res_lock
);
2217 unblock_resume(arm_state
);
2218 resume
= vchiq_check_resume(state
);
2219 write_unlock_bh(&arm_state
->susp_res_lock
);
2222 if (wait_for_completion_interruptible(
2223 &arm_state
->vc_resume_complete
) < 0) {
2224 vchiq_log_error(vchiq_susp_log_level
,
2225 "%s interrupted", __func__
);
2226 /* failed, cannot accurately derive suspend
2227 * state, so exit early. */
2232 read_lock_bh(&arm_state
->susp_res_lock
);
2233 if (arm_state
->vc_suspend_state
== VC_SUSPEND_SUSPENDED
) {
2234 vchiq_log_info(vchiq_susp_log_level
,
2235 "%s: Videocore remains suspended", __func__
);
2237 vchiq_log_info(vchiq_susp_log_level
,
2238 "%s: Videocore resumed", __func__
);
2241 read_unlock_bh(&arm_state
->susp_res_lock
);
2243 vchiq_log_trace(vchiq_susp_log_level
, "%s exit %d", __func__
, ret
);
2247 /* This function should be called with the write lock held */
2249 vchiq_check_resume(VCHIQ_STATE_T
*state
)
2251 VCHIQ_ARM_STATE_T
*arm_state
= vchiq_platform_get_arm_state(state
);
2257 vchiq_log_trace(vchiq_susp_log_level
, "%s", __func__
);
2259 if (need_resume(state
)) {
2260 set_resume_state(arm_state
, VC_RESUME_REQUESTED
);
2261 request_poll(state
, NULL
, 0);
2266 vchiq_log_trace(vchiq_susp_log_level
, "%s exit", __func__
);
2271 vchiq_platform_check_resume(VCHIQ_STATE_T
*state
)
2273 VCHIQ_ARM_STATE_T
*arm_state
= vchiq_platform_get_arm_state(state
);
2279 vchiq_log_trace(vchiq_susp_log_level
, "%s", __func__
);
2281 write_lock_bh(&arm_state
->susp_res_lock
);
2282 if (arm_state
->wake_address
== 0) {
2283 vchiq_log_info(vchiq_susp_log_level
,
2284 "%s: already awake", __func__
);
2287 if (arm_state
->vc_resume_state
== VC_RESUME_IN_PROGRESS
) {
2288 vchiq_log_info(vchiq_susp_log_level
,
2289 "%s: already resuming", __func__
);
2293 if (arm_state
->vc_resume_state
== VC_RESUME_REQUESTED
) {
2294 set_resume_state(arm_state
, VC_RESUME_IN_PROGRESS
);
2297 vchiq_log_trace(vchiq_susp_log_level
,
2298 "%s: not resuming (resume state %s)", __func__
,
2299 resume_state_names
[arm_state
->vc_resume_state
+
2300 VC_RESUME_NUM_OFFSET
]);
2303 write_unlock_bh(&arm_state
->susp_res_lock
);
2306 vchiq_platform_resume(state
);
2309 vchiq_log_trace(vchiq_susp_log_level
, "%s exit", __func__
);
2317 vchiq_use_internal(VCHIQ_STATE_T
*state
, VCHIQ_SERVICE_T
*service
,
2318 enum USE_TYPE_E use_type
)
2320 VCHIQ_ARM_STATE_T
*arm_state
= vchiq_platform_get_arm_state(state
);
2321 VCHIQ_STATUS_T ret
= VCHIQ_SUCCESS
;
2324 int local_uc
, local_entity_uc
;
2329 vchiq_log_trace(vchiq_susp_log_level
, "%s", __func__
);
2331 if (use_type
== USE_TYPE_VCHIQ
) {
2332 sprintf(entity
, "VCHIQ: ");
2333 entity_uc
= &arm_state
->peer_use_count
;
2334 } else if (service
) {
2335 sprintf(entity
, "%c%c%c%c:%03d",
2336 VCHIQ_FOURCC_AS_4CHARS(service
->base
.fourcc
),
2337 service
->client_id
);
2338 entity_uc
= &service
->service_use_count
;
2340 vchiq_log_error(vchiq_susp_log_level
, "%s null service "
2346 write_lock_bh(&arm_state
->susp_res_lock
);
2347 while (arm_state
->resume_blocked
) {
2348 /* If we call 'use' while force suspend is waiting for suspend,
2349 * then we're about to block the thread which the force is
2350 * waiting to complete, so we're bound to just time out. In this
2351 * case, set the suspend state such that the wait will be
2352 * canceled, so we can complete as quickly as possible. */
2353 if (arm_state
->resume_blocked
&& arm_state
->vc_suspend_state
==
2355 set_suspend_state(arm_state
, VC_SUSPEND_FORCE_CANCELED
);
2358 /* If suspend is already in progress then we need to block */
2359 if (!try_wait_for_completion(&arm_state
->resume_blocker
)) {
2360 /* Indicate that there are threads waiting on the resume
2361 * blocker. These need to be allowed to complete before
2362 * a _second_ call to force suspend can complete,
2363 * otherwise low priority threads might never actually
2365 arm_state
->blocked_count
++;
2366 write_unlock_bh(&arm_state
->susp_res_lock
);
2367 vchiq_log_info(vchiq_susp_log_level
, "%s %s resume "
2368 "blocked - waiting...", __func__
, entity
);
2369 if (wait_for_completion_killable(
2370 &arm_state
->resume_blocker
) != 0) {
2371 vchiq_log_error(vchiq_susp_log_level
, "%s %s "
2372 "wait for resume blocker interrupted",
2375 write_lock_bh(&arm_state
->susp_res_lock
);
2376 arm_state
->blocked_count
--;
2377 write_unlock_bh(&arm_state
->susp_res_lock
);
2380 vchiq_log_info(vchiq_susp_log_level
, "%s %s resume "
2381 "unblocked", __func__
, entity
);
2382 write_lock_bh(&arm_state
->susp_res_lock
);
2383 if (--arm_state
->blocked_count
== 0)
2384 complete_all(&arm_state
->blocked_blocker
);
2388 stop_suspend_timer(arm_state
);
2390 local_uc
= ++arm_state
->videocore_use_count
;
2391 local_entity_uc
= ++(*entity_uc
);
2393 /* If there's a pending request which hasn't yet been serviced then
2394 * just clear it. If we're past VC_SUSPEND_REQUESTED state then
2395 * vc_resume_complete will block until we either resume or fail to
2397 if (arm_state
->vc_suspend_state
<= VC_SUSPEND_REQUESTED
)
2398 set_suspend_state(arm_state
, VC_SUSPEND_IDLE
);
2400 if ((use_type
!= USE_TYPE_SERVICE_NO_RESUME
) && need_resume(state
)) {
2401 set_resume_state(arm_state
, VC_RESUME_REQUESTED
);
2402 vchiq_log_info(vchiq_susp_log_level
,
2403 "%s %s count %d, state count %d",
2404 __func__
, entity
, local_entity_uc
, local_uc
);
2405 request_poll(state
, NULL
, 0);
2407 vchiq_log_trace(vchiq_susp_log_level
,
2408 "%s %s count %d, state count %d",
2409 __func__
, entity
, *entity_uc
, local_uc
);
2412 write_unlock_bh(&arm_state
->susp_res_lock
);
2414 /* Completion is in a done state when we're not suspended, so this won't
2415 * block for the non-suspended case. */
2416 if (!try_wait_for_completion(&arm_state
->vc_resume_complete
)) {
2417 vchiq_log_info(vchiq_susp_log_level
, "%s %s wait for resume",
2419 if (wait_for_completion_killable(
2420 &arm_state
->vc_resume_complete
) != 0) {
2421 vchiq_log_error(vchiq_susp_log_level
, "%s %s wait for "
2422 "resume interrupted", __func__
, entity
);
2426 vchiq_log_info(vchiq_susp_log_level
, "%s %s resumed", __func__
,
2430 if (ret
== VCHIQ_SUCCESS
) {
2431 VCHIQ_STATUS_T status
= VCHIQ_SUCCESS
;
2432 long ack_cnt
= atomic_xchg(&arm_state
->ka_use_ack_count
, 0);
2433 while (ack_cnt
&& (status
== VCHIQ_SUCCESS
)) {
2434 /* Send the use notify to videocore */
2435 status
= vchiq_send_remote_use_active(state
);
2436 if (status
== VCHIQ_SUCCESS
)
2440 &arm_state
->ka_use_ack_count
);
2445 vchiq_log_trace(vchiq_susp_log_level
, "%s exit %d", __func__
, ret
);
2450 vchiq_release_internal(VCHIQ_STATE_T
*state
, VCHIQ_SERVICE_T
*service
)
2452 VCHIQ_ARM_STATE_T
*arm_state
= vchiq_platform_get_arm_state(state
);
2453 VCHIQ_STATUS_T ret
= VCHIQ_SUCCESS
;
2456 int local_uc
, local_entity_uc
;
2461 vchiq_log_trace(vchiq_susp_log_level
, "%s", __func__
);
2464 sprintf(entity
, "%c%c%c%c:%03d",
2465 VCHIQ_FOURCC_AS_4CHARS(service
->base
.fourcc
),
2466 service
->client_id
);
2467 entity_uc
= &service
->service_use_count
;
2469 sprintf(entity
, "PEER: ");
2470 entity_uc
= &arm_state
->peer_use_count
;
2473 write_lock_bh(&arm_state
->susp_res_lock
);
2474 if (!arm_state
->videocore_use_count
|| !(*entity_uc
)) {
2475 /* Don't use BUG_ON - don't allow user thread to crash kernel */
2476 WARN_ON(!arm_state
->videocore_use_count
);
2477 WARN_ON(!(*entity_uc
));
2481 local_uc
= --arm_state
->videocore_use_count
;
2482 local_entity_uc
= --(*entity_uc
);
2484 if (!vchiq_videocore_wanted(state
)) {
2485 if (vchiq_platform_use_suspend_timer() &&
2486 !arm_state
->resume_blocked
) {
2487 /* Only use the timer if we're not trying to force
2488 * suspend (=> resume_blocked) */
2489 start_suspend_timer(arm_state
);
2491 vchiq_log_info(vchiq_susp_log_level
,
2492 "%s %s count %d, state count %d - suspending",
2493 __func__
, entity
, *entity_uc
,
2494 arm_state
->videocore_use_count
);
2495 vchiq_arm_vcsuspend(state
);
2498 vchiq_log_trace(vchiq_susp_log_level
,
2499 "%s %s count %d, state count %d",
2500 __func__
, entity
, *entity_uc
,
2501 arm_state
->videocore_use_count
);
2504 write_unlock_bh(&arm_state
->susp_res_lock
);
2507 vchiq_log_trace(vchiq_susp_log_level
, "%s exit %d", __func__
, ret
);
2512 vchiq_on_remote_use(VCHIQ_STATE_T
*state
)
2514 VCHIQ_ARM_STATE_T
*arm_state
= vchiq_platform_get_arm_state(state
);
2515 vchiq_log_trace(vchiq_susp_log_level
, "%s", __func__
);
2516 atomic_inc(&arm_state
->ka_use_count
);
2517 complete(&arm_state
->ka_evt
);
2521 vchiq_on_remote_release(VCHIQ_STATE_T
*state
)
2523 VCHIQ_ARM_STATE_T
*arm_state
= vchiq_platform_get_arm_state(state
);
2524 vchiq_log_trace(vchiq_susp_log_level
, "%s", __func__
);
2525 atomic_inc(&arm_state
->ka_release_count
);
2526 complete(&arm_state
->ka_evt
);
2530 vchiq_use_service_internal(VCHIQ_SERVICE_T
*service
)
2532 return vchiq_use_internal(service
->state
, service
, USE_TYPE_SERVICE
);
2536 vchiq_release_service_internal(VCHIQ_SERVICE_T
*service
)
2538 return vchiq_release_internal(service
->state
, service
);
2541 VCHIQ_DEBUGFS_NODE_T
*
2542 vchiq_instance_get_debugfs_node(VCHIQ_INSTANCE_T instance
)
2544 return &instance
->debugfs_node
;
2548 vchiq_instance_get_use_count(VCHIQ_INSTANCE_T instance
)
2550 VCHIQ_SERVICE_T
*service
;
2551 int use_count
= 0, i
;
2553 while ((service
= next_service_by_instance(instance
->state
,
2554 instance
, &i
)) != NULL
) {
2555 use_count
+= service
->service_use_count
;
2556 unlock_service(service
);
2562 vchiq_instance_get_pid(VCHIQ_INSTANCE_T instance
)
2564 return instance
->pid
;
2568 vchiq_instance_get_trace(VCHIQ_INSTANCE_T instance
)
2570 return instance
->trace
;
2574 vchiq_instance_set_trace(VCHIQ_INSTANCE_T instance
, int trace
)
2576 VCHIQ_SERVICE_T
*service
;
2579 while ((service
= next_service_by_instance(instance
->state
,
2580 instance
, &i
)) != NULL
) {
2581 service
->trace
= trace
;
2582 unlock_service(service
);
2584 instance
->trace
= (trace
!= 0);
2587 static void suspend_timer_callback(unsigned long context
)
2589 VCHIQ_STATE_T
*state
= (VCHIQ_STATE_T
*)context
;
2590 VCHIQ_ARM_STATE_T
*arm_state
= vchiq_platform_get_arm_state(state
);
2593 vchiq_log_info(vchiq_susp_log_level
,
2594 "%s - suspend timer expired - check suspend", __func__
);
2595 vchiq_check_suspend(state
);
2601 vchiq_use_service_no_resume(VCHIQ_SERVICE_HANDLE_T handle
)
2603 VCHIQ_STATUS_T ret
= VCHIQ_ERROR
;
2604 VCHIQ_SERVICE_T
*service
= find_service_by_handle(handle
);
2606 ret
= vchiq_use_internal(service
->state
, service
,
2607 USE_TYPE_SERVICE_NO_RESUME
);
2608 unlock_service(service
);
2614 vchiq_use_service(VCHIQ_SERVICE_HANDLE_T handle
)
2616 VCHIQ_STATUS_T ret
= VCHIQ_ERROR
;
2617 VCHIQ_SERVICE_T
*service
= find_service_by_handle(handle
);
2619 ret
= vchiq_use_internal(service
->state
, service
,
2621 unlock_service(service
);
2627 vchiq_release_service(VCHIQ_SERVICE_HANDLE_T handle
)
2629 VCHIQ_STATUS_T ret
= VCHIQ_ERROR
;
2630 VCHIQ_SERVICE_T
*service
= find_service_by_handle(handle
);
2632 ret
= vchiq_release_internal(service
->state
, service
);
2633 unlock_service(service
);
2639 vchiq_dump_service_use_state(VCHIQ_STATE_T
*state
)
2641 VCHIQ_ARM_STATE_T
*arm_state
= vchiq_platform_get_arm_state(state
);
2643 /* Only dump 64 services */
2644 static const int local_max_services
= 64;
2645 /* If there's more than 64 services, only dump ones with
2646 * non-zero counts */
2647 int only_nonzero
= 0;
2648 static const char *nz
= "<-- preventing suspend";
2650 enum vc_suspend_status vc_suspend_state
;
2651 enum vc_resume_status vc_resume_state
;
2654 int active_services
;
2655 struct service_data_struct
{
2659 } service_data
[local_max_services
];
2664 read_lock_bh(&arm_state
->susp_res_lock
);
2665 vc_suspend_state
= arm_state
->vc_suspend_state
;
2666 vc_resume_state
= arm_state
->vc_resume_state
;
2667 peer_count
= arm_state
->peer_use_count
;
2668 vc_use_count
= arm_state
->videocore_use_count
;
2669 active_services
= state
->unused_service
;
2670 if (active_services
> local_max_services
)
2673 for (i
= 0; (i
< active_services
) && (j
< local_max_services
); i
++) {
2674 VCHIQ_SERVICE_T
*service_ptr
= state
->services
[i
];
2678 if (only_nonzero
&& !service_ptr
->service_use_count
)
2681 if (service_ptr
->srvstate
!= VCHIQ_SRVSTATE_FREE
) {
2682 service_data
[j
].fourcc
= service_ptr
->base
.fourcc
;
2683 service_data
[j
].clientid
= service_ptr
->client_id
;
2684 service_data
[j
++].use_count
= service_ptr
->
2689 read_unlock_bh(&arm_state
->susp_res_lock
);
2691 vchiq_log_warning(vchiq_susp_log_level
,
2692 "-- Videcore suspend state: %s --",
2693 suspend_state_names
[vc_suspend_state
+ VC_SUSPEND_NUM_OFFSET
]);
2694 vchiq_log_warning(vchiq_susp_log_level
,
2695 "-- Videcore resume state: %s --",
2696 resume_state_names
[vc_resume_state
+ VC_RESUME_NUM_OFFSET
]);
2699 vchiq_log_warning(vchiq_susp_log_level
, "Too many active "
2700 "services (%d). Only dumping up to first %d services "
2701 "with non-zero use-count", active_services
,
2702 local_max_services
);
2704 for (i
= 0; i
< j
; i
++) {
2705 vchiq_log_warning(vchiq_susp_log_level
,
2706 "----- %c%c%c%c:%d service count %d %s",
2707 VCHIQ_FOURCC_AS_4CHARS(service_data
[i
].fourcc
),
2708 service_data
[i
].clientid
,
2709 service_data
[i
].use_count
,
2710 service_data
[i
].use_count
? nz
: "");
2712 vchiq_log_warning(vchiq_susp_log_level
,
2713 "----- VCHIQ use count count %d", peer_count
);
2714 vchiq_log_warning(vchiq_susp_log_level
,
2715 "--- Overall vchiq instance use count %d", vc_use_count
);
2717 vchiq_dump_platform_use_state(state
);
2721 vchiq_check_service(VCHIQ_SERVICE_T
*service
)
2723 VCHIQ_ARM_STATE_T
*arm_state
;
2724 VCHIQ_STATUS_T ret
= VCHIQ_ERROR
;
2726 if (!service
|| !service
->state
)
2729 vchiq_log_trace(vchiq_susp_log_level
, "%s", __func__
);
2731 arm_state
= vchiq_platform_get_arm_state(service
->state
);
2733 read_lock_bh(&arm_state
->susp_res_lock
);
2734 if (service
->service_use_count
)
2735 ret
= VCHIQ_SUCCESS
;
2736 read_unlock_bh(&arm_state
->susp_res_lock
);
2738 if (ret
== VCHIQ_ERROR
) {
2739 vchiq_log_error(vchiq_susp_log_level
,
2740 "%s ERROR - %c%c%c%c:%d service count %d, "
2741 "state count %d, videocore suspend state %s", __func__
,
2742 VCHIQ_FOURCC_AS_4CHARS(service
->base
.fourcc
),
2743 service
->client_id
, service
->service_use_count
,
2744 arm_state
->videocore_use_count
,
2745 suspend_state_names
[arm_state
->vc_suspend_state
+
2746 VC_SUSPEND_NUM_OFFSET
]);
2747 vchiq_dump_service_use_state(service
->state
);
2753 /* stub functions */
2754 void vchiq_on_remote_use_active(VCHIQ_STATE_T
*state
)
2759 void vchiq_platform_conn_state_changed(VCHIQ_STATE_T
*state
,
2760 VCHIQ_CONNSTATE_T oldstate
, VCHIQ_CONNSTATE_T newstate
)
2762 VCHIQ_ARM_STATE_T
*arm_state
= vchiq_platform_get_arm_state(state
);
2763 vchiq_log_info(vchiq_susp_log_level
, "%d: %s->%s", state
->id
,
2764 get_conn_state_name(oldstate
), get_conn_state_name(newstate
));
2765 if (state
->conn_state
== VCHIQ_CONNSTATE_CONNECTED
) {
2766 write_lock_bh(&arm_state
->susp_res_lock
);
2767 if (!arm_state
->first_connect
) {
2768 char threadname
[10];
2769 arm_state
->first_connect
= 1;
2770 write_unlock_bh(&arm_state
->susp_res_lock
);
2771 snprintf(threadname
, sizeof(threadname
), "VCHIQka-%d",
2773 arm_state
->ka_thread
= kthread_create(
2774 &vchiq_keepalive_thread_func
,
2777 if (IS_ERR(arm_state
->ka_thread
)) {
2778 vchiq_log_error(vchiq_susp_log_level
,
2779 "vchiq: FATAL: couldn't create thread %s",
2782 wake_up_process(arm_state
->ka_thread
);
2785 write_unlock_bh(&arm_state
->susp_res_lock
);
2789 static int vchiq_probe(struct platform_device
*pdev
)
2791 struct device_node
*fw_node
;
2792 struct rpi_firmware
*fw
;
2796 fw_node
= of_parse_phandle(pdev
->dev
.of_node
, "firmware", 0);
2797 /* Remove comment when booting without Device Tree is no longer supported
2799 dev_err(&pdev->dev, "Missing firmware node\n");
2803 fw
= rpi_firmware_get(fw_node
);
2805 return -EPROBE_DEFER
;
2807 platform_set_drvdata(pdev
, fw
);
2809 /* create debugfs entries */
2810 err
= vchiq_debugfs_init();
2812 goto failed_debugfs_init
;
2814 err
= alloc_chrdev_region(&vchiq_devid
, VCHIQ_MINOR
, 1, DEVICE_NAME
);
2816 vchiq_log_error(vchiq_arm_log_level
,
2817 "Unable to allocate device number");
2818 goto failed_alloc_chrdev
;
2820 cdev_init(&vchiq_cdev
, &vchiq_fops
);
2821 vchiq_cdev
.owner
= THIS_MODULE
;
2822 err
= cdev_add(&vchiq_cdev
, vchiq_devid
, 1);
2824 vchiq_log_error(vchiq_arm_log_level
,
2825 "Unable to register device");
2826 goto failed_cdev_add
;
2829 /* create sysfs entries */
2830 vchiq_class
= class_create(THIS_MODULE
, DEVICE_NAME
);
2831 ptr_err
= vchiq_class
;
2832 if (IS_ERR(ptr_err
))
2833 goto failed_class_create
;
2835 vchiq_dev
= device_create(vchiq_class
, NULL
,
2836 vchiq_devid
, NULL
, "vchiq");
2837 ptr_err
= vchiq_dev
;
2838 if (IS_ERR(ptr_err
))
2839 goto failed_device_create
;
2841 err
= vchiq_platform_init(pdev
, &g_state
);
2843 goto failed_platform_init
;
2845 vchiq_log_info(vchiq_arm_log_level
,
2846 "vchiq: initialised - version %d (min %d), device %d.%d",
2847 VCHIQ_VERSION
, VCHIQ_VERSION_MIN
,
2848 MAJOR(vchiq_devid
), MINOR(vchiq_devid
));
2852 failed_platform_init
:
2853 device_destroy(vchiq_class
, vchiq_devid
);
2854 failed_device_create
:
2855 class_destroy(vchiq_class
);
2856 failed_class_create
:
2857 cdev_del(&vchiq_cdev
);
2858 err
= PTR_ERR(ptr_err
);
2860 unregister_chrdev_region(vchiq_devid
, 1);
2861 failed_alloc_chrdev
:
2862 vchiq_debugfs_deinit();
2863 failed_debugfs_init
:
2864 vchiq_log_warning(vchiq_arm_log_level
, "could not load vchiq");
2868 static int vchiq_remove(struct platform_device
*pdev
)
2870 device_destroy(vchiq_class
, vchiq_devid
);
2871 class_destroy(vchiq_class
);
2872 cdev_del(&vchiq_cdev
);
2873 unregister_chrdev_region(vchiq_devid
, 1);
2878 static const struct of_device_id vchiq_of_match
[] = {
2879 { .compatible
= "brcm,bcm2835-vchiq", },
2882 MODULE_DEVICE_TABLE(of
, vchiq_of_match
);
2884 static struct platform_driver vchiq_driver
= {
2886 .name
= "bcm2835_vchiq",
2887 .of_match_table
= vchiq_of_match
,
2889 .probe
= vchiq_probe
,
2890 .remove
= vchiq_remove
,
2892 module_platform_driver(vchiq_driver
);
2894 MODULE_LICENSE("GPL");
2895 MODULE_DESCRIPTION("Videocore VCHIQ driver");
2896 MODULE_AUTHOR("Broadcom Corporation");