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 mutex_lock(&instance
->completion_mutex
);
215 while (instance
->completion_insert
==
216 (instance
->completion_remove
+ MAX_COMPLETIONS
)) {
217 /* Out of space - wait for the client */
218 DEBUG_TRACE(SERVICE_CALLBACK_LINE
);
219 vchiq_log_trace(vchiq_arm_log_level
,
220 "add_completion - completion queue full");
221 DEBUG_COUNT(COMPLETION_QUEUE_FULL_COUNT
);
223 mutex_unlock(&instance
->completion_mutex
);
224 if (down_interruptible(&instance
->remove_event
) != 0) {
225 vchiq_log_info(vchiq_arm_log_level
,
226 "service_callback interrupted");
230 mutex_lock(&instance
->completion_mutex
);
231 if (instance
->closing
) {
232 mutex_unlock(&instance
->completion_mutex
);
233 vchiq_log_info(vchiq_arm_log_level
,
234 "service_callback closing");
235 return VCHIQ_SUCCESS
;
237 DEBUG_TRACE(SERVICE_CALLBACK_LINE
);
241 &instance
->completions
[instance
->completion_insert
&
242 (MAX_COMPLETIONS
- 1)];
244 completion
->header
= header
;
245 completion
->reason
= reason
;
246 /* N.B. service_userdata is updated while processing AWAIT_COMPLETION */
247 completion
->service_userdata
= user_service
->service
;
248 completion
->bulk_userdata
= bulk_userdata
;
250 if (reason
== VCHIQ_SERVICE_CLOSED
) {
251 /* Take an extra reference, to be held until
252 this CLOSED notification is delivered. */
253 lock_service(user_service
->service
);
254 if (instance
->use_close_delivered
)
255 user_service
->close_pending
= 1;
258 /* A write barrier is needed here to ensure that the entire completion
259 record is written out before the insert point. */
262 if (reason
== VCHIQ_MESSAGE_AVAILABLE
)
263 user_service
->message_available_pos
=
264 instance
->completion_insert
;
266 instance
->completion_insert
++;
268 mutex_unlock(&instance
->completion_mutex
);
270 up(&instance
->insert_event
);
272 return VCHIQ_SUCCESS
;
275 /****************************************************************************
279 ***************************************************************************/
281 static VCHIQ_STATUS_T
282 service_callback(VCHIQ_REASON_T reason
, VCHIQ_HEADER_T
*header
,
283 VCHIQ_SERVICE_HANDLE_T handle
, void *bulk_userdata
)
285 /* How do we ensure the callback goes to the right client?
286 ** The service_user data points to a USER_SERVICE_T record containing
287 ** the original callback and the user state structure, which contains a
288 ** circular buffer for completion records.
290 USER_SERVICE_T
*user_service
;
291 VCHIQ_SERVICE_T
*service
;
292 VCHIQ_INSTANCE_T instance
;
293 int skip_completion
= 0;
294 DEBUG_INITIALISE(g_state
.local
)
296 DEBUG_TRACE(SERVICE_CALLBACK_LINE
);
298 service
= handle_to_service(handle
);
300 user_service
= (USER_SERVICE_T
*)service
->base
.userdata
;
301 instance
= user_service
->instance
;
303 if (!instance
|| instance
->closing
)
304 return VCHIQ_SUCCESS
;
306 vchiq_log_trace(vchiq_arm_log_level
,
307 "service_callback - service %lx(%d,%p), reason %d, header %lx, "
308 "instance %lx, bulk_userdata %lx",
309 (unsigned long)user_service
,
310 service
->localport
, user_service
->userdata
,
311 reason
, (unsigned long)header
,
312 (unsigned long)instance
, (unsigned long)bulk_userdata
);
314 if (header
&& user_service
->is_vchi
) {
315 spin_lock(&msg_queue_spinlock
);
316 while (user_service
->msg_insert
==
317 (user_service
->msg_remove
+ MSG_QUEUE_SIZE
)) {
318 spin_unlock(&msg_queue_spinlock
);
319 DEBUG_TRACE(SERVICE_CALLBACK_LINE
);
320 DEBUG_COUNT(MSG_QUEUE_FULL_COUNT
);
321 vchiq_log_trace(vchiq_arm_log_level
,
322 "service_callback - msg queue full");
323 /* If there is no MESSAGE_AVAILABLE in the completion
326 if ((user_service
->message_available_pos
-
327 instance
->completion_remove
) < 0) {
328 VCHIQ_STATUS_T status
;
329 vchiq_log_info(vchiq_arm_log_level
,
330 "Inserting extra MESSAGE_AVAILABLE");
331 DEBUG_TRACE(SERVICE_CALLBACK_LINE
);
332 status
= add_completion(instance
, reason
,
333 NULL
, user_service
, bulk_userdata
);
334 if (status
!= VCHIQ_SUCCESS
) {
335 DEBUG_TRACE(SERVICE_CALLBACK_LINE
);
340 DEBUG_TRACE(SERVICE_CALLBACK_LINE
);
341 if (down_interruptible(&user_service
->remove_event
)
343 vchiq_log_info(vchiq_arm_log_level
,
344 "service_callback interrupted");
345 DEBUG_TRACE(SERVICE_CALLBACK_LINE
);
347 } else if (instance
->closing
) {
348 vchiq_log_info(vchiq_arm_log_level
,
349 "service_callback closing");
350 DEBUG_TRACE(SERVICE_CALLBACK_LINE
);
353 DEBUG_TRACE(SERVICE_CALLBACK_LINE
);
354 spin_lock(&msg_queue_spinlock
);
357 user_service
->msg_queue
[user_service
->msg_insert
&
358 (MSG_QUEUE_SIZE
- 1)] = header
;
359 user_service
->msg_insert
++;
361 /* If there is a thread waiting in DEQUEUE_MESSAGE, or if
362 ** there is a MESSAGE_AVAILABLE in the completion queue then
363 ** bypass the completion queue.
365 if (((user_service
->message_available_pos
-
366 instance
->completion_remove
) >= 0) ||
367 user_service
->dequeue_pending
) {
368 user_service
->dequeue_pending
= 0;
372 spin_unlock(&msg_queue_spinlock
);
374 up(&user_service
->insert_event
);
379 if (skip_completion
) {
380 DEBUG_TRACE(SERVICE_CALLBACK_LINE
);
381 return VCHIQ_SUCCESS
;
384 DEBUG_TRACE(SERVICE_CALLBACK_LINE
);
386 return add_completion(instance
, reason
, header
, user_service
,
390 /****************************************************************************
394 ***************************************************************************/
396 user_service_free(void *userdata
)
401 /****************************************************************************
405 ***************************************************************************/
406 static void close_delivered(USER_SERVICE_T
*user_service
)
408 vchiq_log_info(vchiq_arm_log_level
,
409 "close_delivered(handle=%x)",
410 user_service
->service
->handle
);
412 if (user_service
->close_pending
) {
413 /* Allow the underlying service to be culled */
414 unlock_service(user_service
->service
);
416 /* Wake the user-thread blocked in close_ or remove_service */
417 up(&user_service
->close_event
);
419 user_service
->close_pending
= 0;
423 /****************************************************************************
427 ***************************************************************************/
429 vchiq_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
)
431 VCHIQ_INSTANCE_T instance
= file
->private_data
;
432 VCHIQ_STATUS_T status
= VCHIQ_SUCCESS
;
433 VCHIQ_SERVICE_T
*service
= NULL
;
436 DEBUG_INITIALISE(g_state
.local
)
438 vchiq_log_trace(vchiq_arm_log_level
,
439 "vchiq_ioctl - instance %pK, cmd %s, arg %lx",
441 ((_IOC_TYPE(cmd
) == VCHIQ_IOC_MAGIC
) &&
442 (_IOC_NR(cmd
) <= VCHIQ_IOC_MAX
)) ?
443 ioctl_names
[_IOC_NR(cmd
)] : "<invalid>", arg
);
446 case VCHIQ_IOC_SHUTDOWN
:
447 if (!instance
->connected
)
450 /* Remove all services */
452 while ((service
= next_service_by_instance(instance
->state
,
453 instance
, &i
)) != NULL
) {
454 status
= vchiq_remove_service(service
->handle
);
455 unlock_service(service
);
456 if (status
!= VCHIQ_SUCCESS
)
461 if (status
== VCHIQ_SUCCESS
) {
462 /* Wake the completion thread and ask it to exit */
463 instance
->closing
= 1;
464 up(&instance
->insert_event
);
469 case VCHIQ_IOC_CONNECT
:
470 if (instance
->connected
) {
474 rc
= mutex_lock_killable(&instance
->state
->mutex
);
476 vchiq_log_error(vchiq_arm_log_level
,
477 "vchiq: connect: could not lock mutex for "
479 instance
->state
->id
, rc
);
483 status
= vchiq_connect_internal(instance
->state
, instance
);
484 mutex_unlock(&instance
->state
->mutex
);
486 if (status
== VCHIQ_SUCCESS
)
487 instance
->connected
= 1;
489 vchiq_log_error(vchiq_arm_log_level
,
490 "vchiq: could not connect: %d", status
);
493 case VCHIQ_IOC_CREATE_SERVICE
: {
494 VCHIQ_CREATE_SERVICE_T args
;
495 USER_SERVICE_T
*user_service
= NULL
;
500 (&args
, (const void __user
*)arg
,
501 sizeof(args
)) != 0) {
506 user_service
= kmalloc(sizeof(USER_SERVICE_T
), GFP_KERNEL
);
513 if (!instance
->connected
) {
518 srvstate
= VCHIQ_SRVSTATE_OPENING
;
521 instance
->connected
?
522 VCHIQ_SRVSTATE_LISTENING
:
523 VCHIQ_SRVSTATE_HIDDEN
;
526 userdata
= args
.params
.userdata
;
527 args
.params
.callback
= service_callback
;
528 args
.params
.userdata
= user_service
;
529 service
= vchiq_add_service_internal(
531 &args
.params
, srvstate
,
532 instance
, user_service_free
);
534 if (service
!= NULL
) {
535 user_service
->service
= service
;
536 user_service
->userdata
= userdata
;
537 user_service
->instance
= instance
;
538 user_service
->is_vchi
= (args
.is_vchi
!= 0);
539 user_service
->dequeue_pending
= 0;
540 user_service
->close_pending
= 0;
541 user_service
->message_available_pos
=
542 instance
->completion_remove
- 1;
543 user_service
->msg_insert
= 0;
544 user_service
->msg_remove
= 0;
545 sema_init(&user_service
->insert_event
, 0);
546 sema_init(&user_service
->remove_event
, 0);
547 sema_init(&user_service
->close_event
, 0);
550 status
= vchiq_open_service_internal
551 (service
, instance
->pid
);
552 if (status
!= VCHIQ_SUCCESS
) {
553 vchiq_remove_service(service
->handle
);
555 ret
= (status
== VCHIQ_RETRY
) ?
561 if (copy_to_user((void __user
*)
562 &(((VCHIQ_CREATE_SERVICE_T __user
*)
564 (const void *)&service
->handle
,
565 sizeof(service
->handle
)) != 0) {
567 vchiq_remove_service(service
->handle
);
577 case VCHIQ_IOC_CLOSE_SERVICE
: {
578 VCHIQ_SERVICE_HANDLE_T handle
= (VCHIQ_SERVICE_HANDLE_T
)arg
;
580 service
= find_service_for_instance(instance
, handle
);
581 if (service
!= NULL
) {
582 USER_SERVICE_T
*user_service
=
583 (USER_SERVICE_T
*)service
->base
.userdata
;
584 /* close_pending is false on first entry, and when the
585 wait in vchiq_close_service has been interrupted. */
586 if (!user_service
->close_pending
) {
587 status
= vchiq_close_service(service
->handle
);
588 if (status
!= VCHIQ_SUCCESS
)
592 /* close_pending is true once the underlying service
593 has been closed until the client library calls the
594 CLOSE_DELIVERED ioctl, signalling close_event. */
595 if (user_service
->close_pending
&&
596 down_interruptible(&user_service
->close_event
))
597 status
= VCHIQ_RETRY
;
603 case VCHIQ_IOC_REMOVE_SERVICE
: {
604 VCHIQ_SERVICE_HANDLE_T handle
= (VCHIQ_SERVICE_HANDLE_T
)arg
;
606 service
= find_service_for_instance(instance
, handle
);
607 if (service
!= NULL
) {
608 USER_SERVICE_T
*user_service
=
609 (USER_SERVICE_T
*)service
->base
.userdata
;
610 /* close_pending is false on first entry, and when the
611 wait in vchiq_close_service has been interrupted. */
612 if (!user_service
->close_pending
) {
613 status
= vchiq_remove_service(service
->handle
);
614 if (status
!= VCHIQ_SUCCESS
)
618 /* close_pending is true once the underlying service
619 has been closed until the client library calls the
620 CLOSE_DELIVERED ioctl, signalling close_event. */
621 if (user_service
->close_pending
&&
622 down_interruptible(&user_service
->close_event
))
623 status
= VCHIQ_RETRY
;
629 case VCHIQ_IOC_USE_SERVICE
:
630 case VCHIQ_IOC_RELEASE_SERVICE
: {
631 VCHIQ_SERVICE_HANDLE_T handle
= (VCHIQ_SERVICE_HANDLE_T
)arg
;
633 service
= find_service_for_instance(instance
, handle
);
634 if (service
!= NULL
) {
635 status
= (cmd
== VCHIQ_IOC_USE_SERVICE
) ?
636 vchiq_use_service_internal(service
) :
637 vchiq_release_service_internal(service
);
638 if (status
!= VCHIQ_SUCCESS
) {
639 vchiq_log_error(vchiq_susp_log_level
,
640 "%s: cmd %s returned error %d for "
641 "service %c%c%c%c:%03d",
643 (cmd
== VCHIQ_IOC_USE_SERVICE
) ?
644 "VCHIQ_IOC_USE_SERVICE" :
645 "VCHIQ_IOC_RELEASE_SERVICE",
647 VCHIQ_FOURCC_AS_4CHARS(
648 service
->base
.fourcc
),
656 case VCHIQ_IOC_QUEUE_MESSAGE
: {
657 VCHIQ_QUEUE_MESSAGE_T args
;
659 (&args
, (const void __user
*)arg
,
660 sizeof(args
)) != 0) {
665 service
= find_service_for_instance(instance
, args
.handle
);
667 if ((service
!= NULL
) && (args
.count
<= MAX_ELEMENTS
)) {
668 /* Copy elements into kernel space */
669 VCHIQ_ELEMENT_T elements
[MAX_ELEMENTS
];
670 if (copy_from_user(elements
, args
.elements
,
671 args
.count
* sizeof(VCHIQ_ELEMENT_T
)) == 0)
672 status
= vchiq_queue_message
674 elements
, args
.count
);
682 case VCHIQ_IOC_QUEUE_BULK_TRANSMIT
:
683 case VCHIQ_IOC_QUEUE_BULK_RECEIVE
: {
684 VCHIQ_QUEUE_BULK_TRANSFER_T args
;
685 struct bulk_waiter_node
*waiter
= NULL
;
686 VCHIQ_BULK_DIR_T dir
=
687 (cmd
== VCHIQ_IOC_QUEUE_BULK_TRANSMIT
) ?
688 VCHIQ_BULK_TRANSMIT
: VCHIQ_BULK_RECEIVE
;
691 (&args
, (const void __user
*)arg
,
692 sizeof(args
)) != 0) {
697 service
= find_service_for_instance(instance
, args
.handle
);
703 if (args
.mode
== VCHIQ_BULK_MODE_BLOCKING
) {
704 waiter
= kzalloc(sizeof(struct bulk_waiter_node
),
710 args
.userdata
= &waiter
->bulk_waiter
;
711 } else if (args
.mode
== VCHIQ_BULK_MODE_WAITING
) {
712 struct list_head
*pos
;
713 mutex_lock(&instance
->bulk_waiter_list_mutex
);
714 list_for_each(pos
, &instance
->bulk_waiter_list
) {
715 if (list_entry(pos
, struct bulk_waiter_node
,
716 list
)->pid
== current
->pid
) {
717 waiter
= list_entry(pos
,
718 struct bulk_waiter_node
,
725 mutex_unlock(&instance
->bulk_waiter_list_mutex
);
727 vchiq_log_error(vchiq_arm_log_level
,
728 "no bulk_waiter found for pid %d",
733 vchiq_log_info(vchiq_arm_log_level
,
734 "found bulk_waiter %pK for pid %d", waiter
,
736 args
.userdata
= &waiter
->bulk_waiter
;
738 status
= vchiq_bulk_transfer
740 VCHI_MEM_HANDLE_INVALID
,
741 args
.data
, args
.size
,
742 args
.userdata
, args
.mode
,
746 if ((status
!= VCHIQ_RETRY
) || fatal_signal_pending(current
) ||
747 !waiter
->bulk_waiter
.bulk
) {
748 if (waiter
->bulk_waiter
.bulk
) {
749 /* Cancel the signal when the transfer
751 spin_lock(&bulk_waiter_spinlock
);
752 waiter
->bulk_waiter
.bulk
->userdata
= NULL
;
753 spin_unlock(&bulk_waiter_spinlock
);
757 const VCHIQ_BULK_MODE_T mode_waiting
=
758 VCHIQ_BULK_MODE_WAITING
;
759 waiter
->pid
= current
->pid
;
760 mutex_lock(&instance
->bulk_waiter_list_mutex
);
761 list_add(&waiter
->list
, &instance
->bulk_waiter_list
);
762 mutex_unlock(&instance
->bulk_waiter_list_mutex
);
763 vchiq_log_info(vchiq_arm_log_level
,
764 "saved bulk_waiter %pK for pid %d",
765 waiter
, current
->pid
);
767 if (copy_to_user((void __user
*)
768 &(((VCHIQ_QUEUE_BULK_TRANSFER_T __user
*)
770 (const void *)&mode_waiting
,
771 sizeof(mode_waiting
)) != 0)
776 case VCHIQ_IOC_AWAIT_COMPLETION
: {
777 VCHIQ_AWAIT_COMPLETION_T args
;
779 DEBUG_TRACE(AWAIT_COMPLETION_LINE
);
780 if (!instance
->connected
) {
785 if (copy_from_user(&args
, (const void __user
*)arg
,
786 sizeof(args
)) != 0) {
791 mutex_lock(&instance
->completion_mutex
);
793 DEBUG_TRACE(AWAIT_COMPLETION_LINE
);
794 while ((instance
->completion_remove
==
795 instance
->completion_insert
)
796 && !instance
->closing
) {
798 DEBUG_TRACE(AWAIT_COMPLETION_LINE
);
799 mutex_unlock(&instance
->completion_mutex
);
800 rc
= down_interruptible(&instance
->insert_event
);
801 mutex_lock(&instance
->completion_mutex
);
803 DEBUG_TRACE(AWAIT_COMPLETION_LINE
);
804 vchiq_log_info(vchiq_arm_log_level
,
805 "AWAIT_COMPLETION interrupted");
810 DEBUG_TRACE(AWAIT_COMPLETION_LINE
);
812 /* A read memory barrier is needed to stop prefetch of a stale
818 int msgbufcount
= args
.msgbufcount
;
819 for (ret
= 0; ret
< args
.count
; ret
++) {
820 VCHIQ_COMPLETION_DATA_T
*completion
;
821 VCHIQ_SERVICE_T
*service
;
822 USER_SERVICE_T
*user_service
;
823 VCHIQ_HEADER_T
*header
;
824 if (instance
->completion_remove
==
825 instance
->completion_insert
)
827 completion
= &instance
->completions
[
828 instance
->completion_remove
&
829 (MAX_COMPLETIONS
- 1)];
831 service
= completion
->service_userdata
;
832 user_service
= service
->base
.userdata
;
833 completion
->service_userdata
=
834 user_service
->userdata
;
836 header
= completion
->header
;
841 msglen
= header
->size
+
842 sizeof(VCHIQ_HEADER_T
);
843 /* This must be a VCHIQ-style service */
844 if (args
.msgbufsize
< msglen
) {
847 "header %pK: msgbufsize %x < msglen %x",
848 header
, args
.msgbufsize
,
850 WARN(1, "invalid message "
856 if (msgbufcount
<= 0)
857 /* Stall here for lack of a
858 ** buffer for the message. */
860 /* Get the pointer from user space */
862 if (copy_from_user(&msgbuf
,
863 (const void __user
*)
864 &args
.msgbufs
[msgbufcount
],
865 sizeof(msgbuf
)) != 0) {
871 /* Copy the message to user space */
872 if (copy_to_user(msgbuf
, header
,
879 /* Now it has been copied, the message
880 ** can be released. */
881 vchiq_release_message(service
->handle
,
884 /* The completion must point to the
886 completion
->header
= msgbuf
;
889 if ((completion
->reason
==
890 VCHIQ_SERVICE_CLOSED
) &&
891 !instance
->use_close_delivered
)
892 unlock_service(service
);
894 if (copy_to_user((void __user
*)(
896 ret
* sizeof(VCHIQ_COMPLETION_DATA_T
)),
898 sizeof(VCHIQ_COMPLETION_DATA_T
)) != 0) {
904 instance
->completion_remove
++;
907 if (msgbufcount
!= args
.msgbufcount
) {
908 if (copy_to_user((void __user
*)
909 &((VCHIQ_AWAIT_COMPLETION_T
*)arg
)->
912 sizeof(msgbufcount
)) != 0) {
919 up(&instance
->remove_event
);
920 mutex_unlock(&instance
->completion_mutex
);
921 DEBUG_TRACE(AWAIT_COMPLETION_LINE
);
924 case VCHIQ_IOC_DEQUEUE_MESSAGE
: {
925 VCHIQ_DEQUEUE_MESSAGE_T args
;
926 USER_SERVICE_T
*user_service
;
927 VCHIQ_HEADER_T
*header
;
929 DEBUG_TRACE(DEQUEUE_MESSAGE_LINE
);
931 (&args
, (const void __user
*)arg
,
932 sizeof(args
)) != 0) {
936 service
= find_service_for_instance(instance
, args
.handle
);
941 user_service
= (USER_SERVICE_T
*)service
->base
.userdata
;
942 if (user_service
->is_vchi
== 0) {
947 spin_lock(&msg_queue_spinlock
);
948 if (user_service
->msg_remove
== user_service
->msg_insert
) {
949 if (!args
.blocking
) {
950 spin_unlock(&msg_queue_spinlock
);
951 DEBUG_TRACE(DEQUEUE_MESSAGE_LINE
);
955 user_service
->dequeue_pending
= 1;
957 spin_unlock(&msg_queue_spinlock
);
958 DEBUG_TRACE(DEQUEUE_MESSAGE_LINE
);
959 if (down_interruptible(
960 &user_service
->insert_event
) != 0) {
961 vchiq_log_info(vchiq_arm_log_level
,
962 "DEQUEUE_MESSAGE interrupted");
966 spin_lock(&msg_queue_spinlock
);
967 } while (user_service
->msg_remove
==
968 user_service
->msg_insert
);
974 BUG_ON((int)(user_service
->msg_insert
-
975 user_service
->msg_remove
) < 0);
977 header
= user_service
->msg_queue
[user_service
->msg_remove
&
978 (MSG_QUEUE_SIZE
- 1)];
979 user_service
->msg_remove
++;
980 spin_unlock(&msg_queue_spinlock
);
982 up(&user_service
->remove_event
);
985 else if (header
->size
<= args
.bufsize
) {
986 /* Copy to user space if msgbuf is not NULL */
987 if ((args
.buf
== NULL
) ||
988 (copy_to_user((void __user
*)args
.buf
,
990 header
->size
) == 0)) {
992 vchiq_release_message(
998 vchiq_log_error(vchiq_arm_log_level
,
999 "header %pK: bufsize %x < size %x",
1000 header
, args
.bufsize
, header
->size
);
1001 WARN(1, "invalid size\n");
1004 DEBUG_TRACE(DEQUEUE_MESSAGE_LINE
);
1007 case VCHIQ_IOC_GET_CLIENT_ID
: {
1008 VCHIQ_SERVICE_HANDLE_T handle
= (VCHIQ_SERVICE_HANDLE_T
)arg
;
1010 ret
= vchiq_get_client_id(handle
);
1013 case VCHIQ_IOC_GET_CONFIG
: {
1014 VCHIQ_GET_CONFIG_T args
;
1015 VCHIQ_CONFIG_T config
;
1017 if (copy_from_user(&args
, (const void __user
*)arg
,
1018 sizeof(args
)) != 0) {
1022 if (args
.config_size
> sizeof(config
)) {
1026 status
= vchiq_get_config(instance
, args
.config_size
, &config
);
1027 if (status
== VCHIQ_SUCCESS
) {
1028 if (copy_to_user((void __user
*)args
.pconfig
,
1029 &config
, args
.config_size
) != 0) {
1036 case VCHIQ_IOC_SET_SERVICE_OPTION
: {
1037 VCHIQ_SET_SERVICE_OPTION_T args
;
1040 &args
, (const void __user
*)arg
,
1041 sizeof(args
)) != 0) {
1046 service
= find_service_for_instance(instance
, args
.handle
);
1052 status
= vchiq_set_service_option(
1053 args
.handle
, args
.option
, args
.value
);
1056 case VCHIQ_IOC_DUMP_PHYS_MEM
: {
1057 VCHIQ_DUMP_MEM_T args
;
1060 (&args
, (const void __user
*)arg
,
1061 sizeof(args
)) != 0) {
1065 dump_phys_mem(args
.virt_addr
, args
.num_bytes
);
1068 case VCHIQ_IOC_LIB_VERSION
: {
1069 unsigned int lib_version
= (unsigned int)arg
;
1071 if (lib_version
< VCHIQ_VERSION_MIN
)
1073 else if (lib_version
>= VCHIQ_VERSION_CLOSE_DELIVERED
)
1074 instance
->use_close_delivered
= 1;
1077 case VCHIQ_IOC_CLOSE_DELIVERED
: {
1078 VCHIQ_SERVICE_HANDLE_T handle
= (VCHIQ_SERVICE_HANDLE_T
)arg
;
1080 service
= find_closed_service_for_instance(instance
, handle
);
1081 if (service
!= NULL
) {
1082 USER_SERVICE_T
*user_service
=
1083 (USER_SERVICE_T
*)service
->base
.userdata
;
1084 close_delivered(user_service
);
1096 unlock_service(service
);
1099 if (status
== VCHIQ_ERROR
)
1101 else if (status
== VCHIQ_RETRY
)
1105 if ((status
== VCHIQ_SUCCESS
) && (ret
< 0) && (ret
!= -EINTR
) &&
1106 (ret
!= -EWOULDBLOCK
))
1107 vchiq_log_info(vchiq_arm_log_level
,
1108 " ioctl instance %lx, cmd %s -> status %d, %ld",
1109 (unsigned long)instance
,
1110 (_IOC_NR(cmd
) <= VCHIQ_IOC_MAX
) ?
1111 ioctl_names
[_IOC_NR(cmd
)] :
1115 vchiq_log_trace(vchiq_arm_log_level
,
1116 " ioctl instance %lx, cmd %s -> status %d, %ld",
1117 (unsigned long)instance
,
1118 (_IOC_NR(cmd
) <= VCHIQ_IOC_MAX
) ?
1119 ioctl_names
[_IOC_NR(cmd
)] :
1126 /****************************************************************************
1130 ***************************************************************************/
1133 vchiq_open(struct inode
*inode
, struct file
*file
)
1135 int dev
= iminor(inode
) & 0x0f;
1136 vchiq_log_info(vchiq_arm_log_level
, "vchiq_open");
1140 VCHIQ_STATE_T
*state
= vchiq_get_state();
1141 VCHIQ_INSTANCE_T instance
;
1144 vchiq_log_error(vchiq_arm_log_level
,
1145 "vchiq has no connection to VideoCore");
1149 instance
= kzalloc(sizeof(*instance
), GFP_KERNEL
);
1153 instance
->state
= state
;
1154 instance
->pid
= current
->tgid
;
1156 ret
= vchiq_debugfs_add_instance(instance
);
1162 sema_init(&instance
->insert_event
, 0);
1163 sema_init(&instance
->remove_event
, 0);
1164 mutex_init(&instance
->completion_mutex
);
1165 mutex_init(&instance
->bulk_waiter_list_mutex
);
1166 INIT_LIST_HEAD(&instance
->bulk_waiter_list
);
1168 file
->private_data
= instance
;
1172 vchiq_log_error(vchiq_arm_log_level
,
1173 "Unknown minor device: %d", dev
);
1180 /****************************************************************************
1184 ***************************************************************************/
1187 vchiq_release(struct inode
*inode
, struct file
*file
)
1189 int dev
= iminor(inode
) & 0x0f;
1193 VCHIQ_INSTANCE_T instance
= file
->private_data
;
1194 VCHIQ_STATE_T
*state
= vchiq_get_state();
1195 VCHIQ_SERVICE_T
*service
;
1198 vchiq_log_info(vchiq_arm_log_level
,
1199 "vchiq_release: instance=%lx",
1200 (unsigned long)instance
);
1207 /* Ensure videocore is awake to allow termination. */
1208 vchiq_use_internal(instance
->state
, NULL
,
1211 mutex_lock(&instance
->completion_mutex
);
1213 /* Wake the completion thread and ask it to exit */
1214 instance
->closing
= 1;
1215 up(&instance
->insert_event
);
1217 mutex_unlock(&instance
->completion_mutex
);
1219 /* Wake the slot handler if the completion queue is full. */
1220 up(&instance
->remove_event
);
1222 /* Mark all services for termination... */
1224 while ((service
= next_service_by_instance(state
, instance
,
1226 USER_SERVICE_T
*user_service
= service
->base
.userdata
;
1228 /* Wake the slot handler if the msg queue is full. */
1229 up(&user_service
->remove_event
);
1231 vchiq_terminate_service_internal(service
);
1232 unlock_service(service
);
1235 /* ...and wait for them to die */
1237 while ((service
= next_service_by_instance(state
, instance
, &i
))
1239 USER_SERVICE_T
*user_service
= service
->base
.userdata
;
1241 down(&service
->remove_event
);
1243 BUG_ON(service
->srvstate
!= VCHIQ_SRVSTATE_FREE
);
1245 spin_lock(&msg_queue_spinlock
);
1247 while (user_service
->msg_remove
!=
1248 user_service
->msg_insert
) {
1249 VCHIQ_HEADER_T
*header
= user_service
->
1250 msg_queue
[user_service
->msg_remove
&
1251 (MSG_QUEUE_SIZE
- 1)];
1252 user_service
->msg_remove
++;
1253 spin_unlock(&msg_queue_spinlock
);
1256 vchiq_release_message(
1259 spin_lock(&msg_queue_spinlock
);
1262 spin_unlock(&msg_queue_spinlock
);
1264 unlock_service(service
);
1267 /* Release any closed services */
1268 while (instance
->completion_remove
!=
1269 instance
->completion_insert
) {
1270 VCHIQ_COMPLETION_DATA_T
*completion
;
1271 VCHIQ_SERVICE_T
*service
;
1272 completion
= &instance
->completions
[
1273 instance
->completion_remove
&
1274 (MAX_COMPLETIONS
- 1)];
1275 service
= completion
->service_userdata
;
1276 if (completion
->reason
== VCHIQ_SERVICE_CLOSED
)
1278 USER_SERVICE_T
*user_service
=
1279 service
->base
.userdata
;
1281 /* Wake any blocked user-thread */
1282 if (instance
->use_close_delivered
)
1283 up(&user_service
->close_event
);
1284 unlock_service(service
);
1286 instance
->completion_remove
++;
1289 /* Release the PEER service count. */
1290 vchiq_release_internal(instance
->state
, NULL
);
1293 struct list_head
*pos
, *next
;
1294 list_for_each_safe(pos
, next
,
1295 &instance
->bulk_waiter_list
) {
1296 struct bulk_waiter_node
*waiter
;
1297 waiter
= list_entry(pos
,
1298 struct bulk_waiter_node
,
1301 vchiq_log_info(vchiq_arm_log_level
,
1302 "bulk_waiter - cleaned up %pK for pid %d",
1303 waiter
, waiter
->pid
);
1308 vchiq_debugfs_remove_instance(instance
);
1311 file
->private_data
= NULL
;
1315 vchiq_log_error(vchiq_arm_log_level
,
1316 "Unknown minor device: %d", dev
);
1324 /****************************************************************************
1328 ***************************************************************************/
1331 vchiq_dump(void *dump_context
, const char *str
, int len
)
1333 DUMP_CONTEXT_T
*context
= (DUMP_CONTEXT_T
*)dump_context
;
1335 if (context
->actual
< context
->space
) {
1337 if (context
->offset
> 0) {
1338 int skip_bytes
= min(len
, (int)context
->offset
);
1341 context
->offset
-= skip_bytes
;
1342 if (context
->offset
> 0)
1345 copy_bytes
= min(len
, (int)(context
->space
- context
->actual
));
1346 if (copy_bytes
== 0)
1348 if (copy_to_user(context
->buf
+ context
->actual
, str
,
1350 context
->actual
= -EFAULT
;
1351 context
->actual
+= copy_bytes
;
1354 /* If tne terminating NUL is included in the length, then it
1355 ** marks the end of a line and should be replaced with a
1356 ** carriage return. */
1357 if ((len
== 0) && (str
[copy_bytes
- 1] == '\0')) {
1359 if (copy_to_user(context
->buf
+ context
->actual
- 1,
1361 context
->actual
= -EFAULT
;
1366 /****************************************************************************
1368 * vchiq_dump_platform_instance_state
1370 ***************************************************************************/
1373 vchiq_dump_platform_instances(void *dump_context
)
1375 VCHIQ_STATE_T
*state
= vchiq_get_state();
1380 /* There is no list of instances, so instead scan all services,
1381 marking those that have been dumped. */
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
;
1394 for (i
= 0; i
< state
->unused_service
; i
++) {
1395 VCHIQ_SERVICE_T
*service
= state
->services
[i
];
1396 VCHIQ_INSTANCE_T instance
;
1398 if (service
&& (service
->base
.callback
== service_callback
)) {
1399 instance
= service
->instance
;
1400 if (instance
&& !instance
->mark
) {
1401 len
= snprintf(buf
, sizeof(buf
),
1402 "Instance %pK: pid %d,%s completions %d/%d",
1403 instance
, instance
->pid
,
1404 instance
->connected
? " connected, " :
1406 instance
->completion_insert
-
1407 instance
->completion_remove
,
1410 vchiq_dump(dump_context
, buf
, len
+ 1);
1418 /****************************************************************************
1420 * vchiq_dump_platform_service_state
1422 ***************************************************************************/
1425 vchiq_dump_platform_service_state(void *dump_context
, VCHIQ_SERVICE_T
*service
)
1427 USER_SERVICE_T
*user_service
= (USER_SERVICE_T
*)service
->base
.userdata
;
1431 len
= snprintf(buf
, sizeof(buf
), " instance %pK", service
->instance
);
1433 if ((service
->base
.callback
== service_callback
) &&
1434 user_service
->is_vchi
) {
1435 len
+= snprintf(buf
+ len
, sizeof(buf
) - len
,
1437 user_service
->msg_insert
- user_service
->msg_remove
,
1440 if (user_service
->dequeue_pending
)
1441 len
+= snprintf(buf
+ len
, sizeof(buf
) - len
,
1442 " (dequeue pending)");
1445 vchiq_dump(dump_context
, buf
, len
+ 1);
1448 /****************************************************************************
1452 ***************************************************************************/
1455 dump_phys_mem(void *virt_addr
, uint32_t num_bytes
)
1458 uint8_t *end_virt_addr
= virt_addr
+ num_bytes
;
1465 struct page
**pages
;
1466 uint8_t *kmapped_virt_ptr
;
1468 /* Align virtAddr and endVirtAddr to 16 byte boundaries. */
1470 virt_addr
= (void *)((unsigned long)virt_addr
& ~0x0fuL
);
1471 end_virt_addr
= (void *)(((unsigned long)end_virt_addr
+ 15uL) &
1474 offset
= (int)(long)virt_addr
& (PAGE_SIZE
- 1);
1475 end_offset
= (int)(long)end_virt_addr
& (PAGE_SIZE
- 1);
1477 num_pages
= (offset
+ num_bytes
+ PAGE_SIZE
- 1) / PAGE_SIZE
;
1479 pages
= kmalloc(sizeof(struct page
*) * num_pages
, GFP_KERNEL
);
1480 if (pages
== NULL
) {
1481 vchiq_log_error(vchiq_arm_log_level
,
1482 "Unable to allocation memory for %d pages\n",
1487 down_read(¤t
->mm
->mmap_sem
);
1488 rc
= get_user_pages(
1489 (unsigned long)virt_addr
, /* start */
1490 num_pages
, /* len */
1492 pages
, /* pages (array of page pointers) */
1494 up_read(¤t
->mm
->mmap_sem
);
1500 vchiq_log_error(vchiq_arm_log_level
,
1501 "Failed to get user pages: %d\n", rc
);
1505 while (offset
< end_offset
) {
1507 int page_offset
= offset
% PAGE_SIZE
;
1508 page_idx
= offset
/ PAGE_SIZE
;
1510 if (page_idx
!= prev_idx
) {
1514 page
= pages
[page_idx
];
1515 kmapped_virt_ptr
= kmap(page
);
1517 prev_idx
= page_idx
;
1520 if (vchiq_arm_log_level
>= VCHIQ_LOG_TRACE
)
1521 vchiq_log_dump_mem("ph",
1522 (uint32_t)(unsigned long)&kmapped_virt_ptr
[
1524 &kmapped_virt_ptr
[page_offset
], 16);
1533 for (page_idx
= 0; page_idx
< num_pages
; page_idx
++)
1534 put_page(pages
[page_idx
]);
1539 /****************************************************************************
1543 ***************************************************************************/
1546 vchiq_read(struct file
*file
, char __user
*buf
,
1547 size_t count
, loff_t
*ppos
)
1549 DUMP_CONTEXT_T context
;
1552 context
.space
= count
;
1553 context
.offset
= *ppos
;
1555 vchiq_dump_state(&context
, &g_state
);
1557 *ppos
+= context
.actual
;
1559 return context
.actual
;
1563 vchiq_get_state(void)
1566 if (g_state
.remote
== NULL
)
1567 printk(KERN_ERR
"%s: g_state.remote == NULL\n", __func__
);
1568 else if (g_state
.remote
->initialised
!= 1)
1569 printk(KERN_NOTICE
"%s: g_state.remote->initialised != 1 (%d)\n",
1570 __func__
, g_state
.remote
->initialised
);
1572 return ((g_state
.remote
!= NULL
) &&
1573 (g_state
.remote
->initialised
== 1)) ? &g_state
: NULL
;
1576 static const struct file_operations
1578 .owner
= THIS_MODULE
,
1579 .unlocked_ioctl
= vchiq_ioctl
,
1581 .release
= vchiq_release
,
1586 * Autosuspend related functionality
1590 vchiq_videocore_wanted(VCHIQ_STATE_T
*state
)
1592 VCHIQ_ARM_STATE_T
*arm_state
= vchiq_platform_get_arm_state(state
);
1594 /* autosuspend not supported - always return wanted */
1596 else if (arm_state
->blocked_count
)
1598 else if (!arm_state
->videocore_use_count
)
1599 /* usage count zero - check for override unless we're forcing */
1600 if (arm_state
->resume_blocked
)
1603 return vchiq_platform_videocore_wanted(state
);
1605 /* non-zero usage count - videocore still required */
1609 static VCHIQ_STATUS_T
1610 vchiq_keepalive_vchiq_callback(VCHIQ_REASON_T reason
,
1611 VCHIQ_HEADER_T
*header
,
1612 VCHIQ_SERVICE_HANDLE_T service_user
,
1615 vchiq_log_error(vchiq_susp_log_level
,
1616 "%s callback reason %d", __func__
, reason
);
1621 vchiq_keepalive_thread_func(void *v
)
1623 VCHIQ_STATE_T
*state
= (VCHIQ_STATE_T
*) v
;
1624 VCHIQ_ARM_STATE_T
*arm_state
= vchiq_platform_get_arm_state(state
);
1626 VCHIQ_STATUS_T status
;
1627 VCHIQ_INSTANCE_T instance
;
1628 VCHIQ_SERVICE_HANDLE_T ka_handle
;
1630 VCHIQ_SERVICE_PARAMS_T params
= {
1631 .fourcc
= VCHIQ_MAKE_FOURCC('K', 'E', 'E', 'P'),
1632 .callback
= vchiq_keepalive_vchiq_callback
,
1633 .version
= KEEPALIVE_VER
,
1634 .version_min
= KEEPALIVE_VER_MIN
1637 status
= vchiq_initialise(&instance
);
1638 if (status
!= VCHIQ_SUCCESS
) {
1639 vchiq_log_error(vchiq_susp_log_level
,
1640 "%s vchiq_initialise failed %d", __func__
, status
);
1644 status
= vchiq_connect(instance
);
1645 if (status
!= VCHIQ_SUCCESS
) {
1646 vchiq_log_error(vchiq_susp_log_level
,
1647 "%s vchiq_connect failed %d", __func__
, status
);
1651 status
= vchiq_add_service(instance
, ¶ms
, &ka_handle
);
1652 if (status
!= VCHIQ_SUCCESS
) {
1653 vchiq_log_error(vchiq_susp_log_level
,
1654 "%s vchiq_open_service failed %d", __func__
, status
);
1659 long rc
= 0, uc
= 0;
1660 if (wait_for_completion_interruptible(&arm_state
->ka_evt
)
1662 vchiq_log_error(vchiq_susp_log_level
,
1663 "%s interrupted", __func__
);
1664 flush_signals(current
);
1668 /* read and clear counters. Do release_count then use_count to
1669 * prevent getting more releases than uses */
1670 rc
= atomic_xchg(&arm_state
->ka_release_count
, 0);
1671 uc
= atomic_xchg(&arm_state
->ka_use_count
, 0);
1673 /* Call use/release service the requisite number of times.
1674 * Process use before release so use counts don't go negative */
1676 atomic_inc(&arm_state
->ka_use_ack_count
);
1677 status
= vchiq_use_service(ka_handle
);
1678 if (status
!= VCHIQ_SUCCESS
) {
1679 vchiq_log_error(vchiq_susp_log_level
,
1680 "%s vchiq_use_service error %d",
1685 status
= vchiq_release_service(ka_handle
);
1686 if (status
!= VCHIQ_SUCCESS
) {
1687 vchiq_log_error(vchiq_susp_log_level
,
1688 "%s vchiq_release_service error %d",
1695 vchiq_shutdown(instance
);
1703 vchiq_arm_init_state(VCHIQ_STATE_T
*state
, VCHIQ_ARM_STATE_T
*arm_state
)
1706 rwlock_init(&arm_state
->susp_res_lock
);
1708 init_completion(&arm_state
->ka_evt
);
1709 atomic_set(&arm_state
->ka_use_count
, 0);
1710 atomic_set(&arm_state
->ka_use_ack_count
, 0);
1711 atomic_set(&arm_state
->ka_release_count
, 0);
1713 init_completion(&arm_state
->vc_suspend_complete
);
1715 init_completion(&arm_state
->vc_resume_complete
);
1716 /* Initialise to 'done' state. We only want to block on resume
1717 * completion while videocore is suspended. */
1718 set_resume_state(arm_state
, VC_RESUME_RESUMED
);
1720 init_completion(&arm_state
->resume_blocker
);
1721 /* Initialise to 'done' state. We only want to block on this
1722 * completion while resume is blocked */
1723 complete_all(&arm_state
->resume_blocker
);
1725 init_completion(&arm_state
->blocked_blocker
);
1726 /* Initialise to 'done' state. We only want to block on this
1727 * completion while things are waiting on the resume blocker */
1728 complete_all(&arm_state
->blocked_blocker
);
1730 arm_state
->suspend_timer_timeout
= SUSPEND_TIMER_TIMEOUT_MS
;
1731 arm_state
->suspend_timer_running
= 0;
1732 setup_timer(&arm_state
->suspend_timer
, suspend_timer_callback
,
1733 (unsigned long)(state
));
1735 arm_state
->first_connect
= 0;
1738 return VCHIQ_SUCCESS
;
1742 ** Functions to modify the state variables;
1743 ** set_suspend_state
1746 ** There are more state variables than we might like, so ensure they remain in
1747 ** step. Suspend and resume state are maintained separately, since most of
1748 ** these state machines can operate independently. However, there are a few
1749 ** states where state transitions in one state machine cause a reset to the
1750 ** other state machine. In addition, there are some completion events which
1751 ** need to occur on state machine reset and end-state(s), so these are also
1752 ** dealt with in these functions.
1754 ** In all states we set the state variable according to the input, but in some
1755 ** cases we perform additional steps outlined below;
1757 ** VC_SUSPEND_IDLE - Initialise the suspend completion at the same time.
1758 ** The suspend completion is completed after any suspend
1759 ** attempt. When we reset the state machine we also reset
1760 ** the completion. This reset occurs when videocore is
1761 ** resumed, and also if we initiate suspend after a suspend
1764 ** VC_SUSPEND_IN_PROGRESS - This state is considered the point of no return for
1765 ** suspend - ie from this point on we must try to suspend
1766 ** before resuming can occur. We therefore also reset the
1767 ** resume state machine to VC_RESUME_IDLE in this state.
1769 ** VC_SUSPEND_SUSPENDED - Suspend has completed successfully. Also call
1770 ** complete_all on the suspend completion to notify
1771 ** anything waiting for suspend to happen.
1773 ** VC_SUSPEND_REJECTED - Videocore rejected suspend. Videocore will also
1774 ** initiate resume, so no need to alter resume state.
1775 ** We call complete_all on the suspend completion to notify
1776 ** of suspend rejection.
1778 ** VC_SUSPEND_FAILED - We failed to initiate videocore suspend. We notify the
1779 ** suspend completion and reset the resume state machine.
1781 ** VC_RESUME_IDLE - Initialise the resume completion at the same time. The
1782 ** resume completion is in it's 'done' state whenever
1783 ** videcore is running. Therefore, the VC_RESUME_IDLE
1784 ** state implies that videocore is suspended.
1785 ** Hence, any thread which needs to wait until videocore is
1786 ** running can wait on this completion - it will only block
1787 ** if videocore is suspended.
1789 ** VC_RESUME_RESUMED - Resume has completed successfully. Videocore is running.
1790 ** Call complete_all on the resume completion to unblock
1791 ** any threads waiting for resume. Also reset the suspend
1792 ** state machine to it's idle state.
1794 ** VC_RESUME_FAILED - Currently unused - no mechanism to fail resume exists.
1798 set_suspend_state(VCHIQ_ARM_STATE_T
*arm_state
,
1799 enum vc_suspend_status new_state
)
1801 /* set the state in all cases */
1802 arm_state
->vc_suspend_state
= new_state
;
1804 /* state specific additional actions */
1805 switch (new_state
) {
1806 case VC_SUSPEND_FORCE_CANCELED
:
1807 complete_all(&arm_state
->vc_suspend_complete
);
1809 case VC_SUSPEND_REJECTED
:
1810 complete_all(&arm_state
->vc_suspend_complete
);
1812 case VC_SUSPEND_FAILED
:
1813 complete_all(&arm_state
->vc_suspend_complete
);
1814 arm_state
->vc_resume_state
= VC_RESUME_RESUMED
;
1815 complete_all(&arm_state
->vc_resume_complete
);
1817 case VC_SUSPEND_IDLE
:
1818 reinit_completion(&arm_state
->vc_suspend_complete
);
1820 case VC_SUSPEND_REQUESTED
:
1822 case VC_SUSPEND_IN_PROGRESS
:
1823 set_resume_state(arm_state
, VC_RESUME_IDLE
);
1825 case VC_SUSPEND_SUSPENDED
:
1826 complete_all(&arm_state
->vc_suspend_complete
);
1835 set_resume_state(VCHIQ_ARM_STATE_T
*arm_state
,
1836 enum vc_resume_status new_state
)
1838 /* set the state in all cases */
1839 arm_state
->vc_resume_state
= new_state
;
1841 /* state specific additional actions */
1842 switch (new_state
) {
1843 case VC_RESUME_FAILED
:
1845 case VC_RESUME_IDLE
:
1846 reinit_completion(&arm_state
->vc_resume_complete
);
1848 case VC_RESUME_REQUESTED
:
1850 case VC_RESUME_IN_PROGRESS
:
1852 case VC_RESUME_RESUMED
:
1853 complete_all(&arm_state
->vc_resume_complete
);
1854 set_suspend_state(arm_state
, VC_SUSPEND_IDLE
);
1863 /* should be called with the write lock held */
1865 start_suspend_timer(VCHIQ_ARM_STATE_T
*arm_state
)
1867 del_timer(&arm_state
->suspend_timer
);
1868 arm_state
->suspend_timer
.expires
= jiffies
+
1869 msecs_to_jiffies(arm_state
->
1870 suspend_timer_timeout
);
1871 add_timer(&arm_state
->suspend_timer
);
1872 arm_state
->suspend_timer_running
= 1;
1875 /* should be called with the write lock held */
1877 stop_suspend_timer(VCHIQ_ARM_STATE_T
*arm_state
)
1879 if (arm_state
->suspend_timer_running
) {
1880 del_timer(&arm_state
->suspend_timer
);
1881 arm_state
->suspend_timer_running
= 0;
1886 need_resume(VCHIQ_STATE_T
*state
)
1888 VCHIQ_ARM_STATE_T
*arm_state
= vchiq_platform_get_arm_state(state
);
1889 return (arm_state
->vc_suspend_state
> VC_SUSPEND_IDLE
) &&
1890 (arm_state
->vc_resume_state
< VC_RESUME_REQUESTED
) &&
1891 vchiq_videocore_wanted(state
);
1895 block_resume(VCHIQ_ARM_STATE_T
*arm_state
)
1897 int status
= VCHIQ_SUCCESS
;
1898 const unsigned long timeout_val
=
1899 msecs_to_jiffies(FORCE_SUSPEND_TIMEOUT_MS
);
1900 int resume_count
= 0;
1902 /* Allow any threads which were blocked by the last force suspend to
1903 * complete if they haven't already. Only give this one shot; if
1904 * blocked_count is incremented after blocked_blocker is completed
1905 * (which only happens when blocked_count hits 0) then those threads
1906 * will have to wait until next time around */
1907 if (arm_state
->blocked_count
) {
1908 reinit_completion(&arm_state
->blocked_blocker
);
1909 write_unlock_bh(&arm_state
->susp_res_lock
);
1910 vchiq_log_info(vchiq_susp_log_level
, "%s wait for previously "
1911 "blocked clients", __func__
);
1912 if (wait_for_completion_interruptible_timeout(
1913 &arm_state
->blocked_blocker
, timeout_val
)
1915 vchiq_log_error(vchiq_susp_log_level
, "%s wait for "
1916 "previously blocked clients failed" , __func__
);
1917 status
= VCHIQ_ERROR
;
1918 write_lock_bh(&arm_state
->susp_res_lock
);
1921 vchiq_log_info(vchiq_susp_log_level
, "%s previously blocked "
1922 "clients resumed", __func__
);
1923 write_lock_bh(&arm_state
->susp_res_lock
);
1926 /* We need to wait for resume to complete if it's in process */
1927 while (arm_state
->vc_resume_state
!= VC_RESUME_RESUMED
&&
1928 arm_state
->vc_resume_state
> VC_RESUME_IDLE
) {
1929 if (resume_count
> 1) {
1930 status
= VCHIQ_ERROR
;
1931 vchiq_log_error(vchiq_susp_log_level
, "%s waited too "
1932 "many times for resume" , __func__
);
1935 write_unlock_bh(&arm_state
->susp_res_lock
);
1936 vchiq_log_info(vchiq_susp_log_level
, "%s wait for resume",
1938 if (wait_for_completion_interruptible_timeout(
1939 &arm_state
->vc_resume_complete
, timeout_val
)
1941 vchiq_log_error(vchiq_susp_log_level
, "%s wait for "
1942 "resume failed (%s)", __func__
,
1943 resume_state_names
[arm_state
->vc_resume_state
+
1944 VC_RESUME_NUM_OFFSET
]);
1945 status
= VCHIQ_ERROR
;
1946 write_lock_bh(&arm_state
->susp_res_lock
);
1949 vchiq_log_info(vchiq_susp_log_level
, "%s resumed", __func__
);
1950 write_lock_bh(&arm_state
->susp_res_lock
);
1953 reinit_completion(&arm_state
->resume_blocker
);
1954 arm_state
->resume_blocked
= 1;
1961 unblock_resume(VCHIQ_ARM_STATE_T
*arm_state
)
1963 complete_all(&arm_state
->resume_blocker
);
1964 arm_state
->resume_blocked
= 0;
1967 /* Initiate suspend via slot handler. Should be called with the write lock
1970 vchiq_arm_vcsuspend(VCHIQ_STATE_T
*state
)
1972 VCHIQ_STATUS_T status
= VCHIQ_ERROR
;
1973 VCHIQ_ARM_STATE_T
*arm_state
= vchiq_platform_get_arm_state(state
);
1978 vchiq_log_trace(vchiq_susp_log_level
, "%s", __func__
);
1979 status
= VCHIQ_SUCCESS
;
1982 switch (arm_state
->vc_suspend_state
) {
1983 case VC_SUSPEND_REQUESTED
:
1984 vchiq_log_info(vchiq_susp_log_level
, "%s: suspend already "
1985 "requested", __func__
);
1987 case VC_SUSPEND_IN_PROGRESS
:
1988 vchiq_log_info(vchiq_susp_log_level
, "%s: suspend already in "
1989 "progress", __func__
);
1993 /* We don't expect to be in other states, so log but continue
1995 vchiq_log_error(vchiq_susp_log_level
,
1996 "%s unexpected suspend state %s", __func__
,
1997 suspend_state_names
[arm_state
->vc_suspend_state
+
1998 VC_SUSPEND_NUM_OFFSET
]);
2000 case VC_SUSPEND_REJECTED
:
2001 case VC_SUSPEND_FAILED
:
2002 /* Ensure any idle state actions have been run */
2003 set_suspend_state(arm_state
, VC_SUSPEND_IDLE
);
2005 case VC_SUSPEND_IDLE
:
2006 vchiq_log_info(vchiq_susp_log_level
,
2007 "%s: suspending", __func__
);
2008 set_suspend_state(arm_state
, VC_SUSPEND_REQUESTED
);
2009 /* kick the slot handler thread to initiate suspend */
2010 request_poll(state
, NULL
, 0);
2015 vchiq_log_trace(vchiq_susp_log_level
, "%s exit %d", __func__
, status
);
2020 vchiq_platform_check_suspend(VCHIQ_STATE_T
*state
)
2022 VCHIQ_ARM_STATE_T
*arm_state
= vchiq_platform_get_arm_state(state
);
2028 vchiq_log_trace(vchiq_susp_log_level
, "%s", __func__
);
2030 write_lock_bh(&arm_state
->susp_res_lock
);
2031 if (arm_state
->vc_suspend_state
== VC_SUSPEND_REQUESTED
&&
2032 arm_state
->vc_resume_state
== VC_RESUME_RESUMED
) {
2033 set_suspend_state(arm_state
, VC_SUSPEND_IN_PROGRESS
);
2036 write_unlock_bh(&arm_state
->susp_res_lock
);
2039 vchiq_platform_suspend(state
);
2042 vchiq_log_trace(vchiq_susp_log_level
, "%s exit", __func__
);
2048 output_timeout_error(VCHIQ_STATE_T
*state
)
2050 VCHIQ_ARM_STATE_T
*arm_state
= vchiq_platform_get_arm_state(state
);
2052 int vc_use_count
= arm_state
->videocore_use_count
;
2053 int active_services
= state
->unused_service
;
2056 if (!arm_state
->videocore_use_count
) {
2057 snprintf(err
, sizeof(err
), " Videocore usecount is 0");
2060 for (i
= 0; i
< active_services
; i
++) {
2061 VCHIQ_SERVICE_T
*service_ptr
= state
->services
[i
];
2062 if (service_ptr
&& service_ptr
->service_use_count
&&
2063 (service_ptr
->srvstate
!= VCHIQ_SRVSTATE_FREE
)) {
2064 snprintf(err
, sizeof(err
), " %c%c%c%c(%d) service has "
2065 "use count %d%s", VCHIQ_FOURCC_AS_4CHARS(
2066 service_ptr
->base
.fourcc
),
2067 service_ptr
->client_id
,
2068 service_ptr
->service_use_count
,
2069 service_ptr
->service_use_count
==
2070 vc_use_count
? "" : " (+ more)");
2076 vchiq_log_error(vchiq_susp_log_level
,
2077 "timed out waiting for vc suspend (%d).%s",
2078 arm_state
->autosuspend_override
, err
);
2082 /* Try to get videocore into suspended state, regardless of autosuspend state.
2083 ** We don't actually force suspend, since videocore may get into a bad state
2084 ** if we force suspend at a bad time. Instead, we wait for autosuspend to
2085 ** determine a good point to suspend. If this doesn't happen within 100ms we
2088 ** Returns VCHIQ_SUCCESS if videocore suspended successfully, VCHIQ_RETRY if
2089 ** videocore failed to suspend in time or VCHIQ_ERROR if interrupted.
2092 vchiq_arm_force_suspend(VCHIQ_STATE_T
*state
)
2094 VCHIQ_ARM_STATE_T
*arm_state
= vchiq_platform_get_arm_state(state
);
2095 VCHIQ_STATUS_T status
= VCHIQ_ERROR
;
2102 vchiq_log_trace(vchiq_susp_log_level
, "%s", __func__
);
2104 write_lock_bh(&arm_state
->susp_res_lock
);
2106 status
= block_resume(arm_state
);
2107 if (status
!= VCHIQ_SUCCESS
)
2109 if (arm_state
->vc_suspend_state
== VC_SUSPEND_SUSPENDED
) {
2110 /* Already suspended - just block resume and exit */
2111 vchiq_log_info(vchiq_susp_log_level
, "%s already suspended",
2113 status
= VCHIQ_SUCCESS
;
2115 } else if (arm_state
->vc_suspend_state
<= VC_SUSPEND_IDLE
) {
2116 /* initiate suspend immediately in the case that we're waiting
2117 * for the timeout */
2118 stop_suspend_timer(arm_state
);
2119 if (!vchiq_videocore_wanted(state
)) {
2120 vchiq_log_info(vchiq_susp_log_level
, "%s videocore "
2121 "idle, initiating suspend", __func__
);
2122 status
= vchiq_arm_vcsuspend(state
);
2123 } else if (arm_state
->autosuspend_override
<
2124 FORCE_SUSPEND_FAIL_MAX
) {
2125 vchiq_log_info(vchiq_susp_log_level
, "%s letting "
2126 "videocore go idle", __func__
);
2127 status
= VCHIQ_SUCCESS
;
2129 vchiq_log_warning(vchiq_susp_log_level
, "%s failed too "
2130 "many times - attempting suspend", __func__
);
2131 status
= vchiq_arm_vcsuspend(state
);
2134 vchiq_log_info(vchiq_susp_log_level
, "%s videocore suspend "
2135 "in progress - wait for completion", __func__
);
2136 status
= VCHIQ_SUCCESS
;
2139 /* Wait for suspend to happen due to system idle (not forced..) */
2140 if (status
!= VCHIQ_SUCCESS
)
2141 goto unblock_resume
;
2144 write_unlock_bh(&arm_state
->susp_res_lock
);
2146 rc
= wait_for_completion_interruptible_timeout(
2147 &arm_state
->vc_suspend_complete
,
2148 msecs_to_jiffies(FORCE_SUSPEND_TIMEOUT_MS
));
2150 write_lock_bh(&arm_state
->susp_res_lock
);
2152 vchiq_log_warning(vchiq_susp_log_level
, "%s "
2153 "interrupted waiting for suspend", __func__
);
2154 status
= VCHIQ_ERROR
;
2155 goto unblock_resume
;
2156 } else if (rc
== 0) {
2157 if (arm_state
->vc_suspend_state
> VC_SUSPEND_IDLE
) {
2158 /* Repeat timeout once if in progress */
2164 arm_state
->autosuspend_override
++;
2165 output_timeout_error(state
);
2167 status
= VCHIQ_RETRY
;
2168 goto unblock_resume
;
2170 } while (0 < (repeat
--));
2172 /* Check and report state in case we need to abort ARM suspend */
2173 if (arm_state
->vc_suspend_state
!= VC_SUSPEND_SUSPENDED
) {
2174 status
= VCHIQ_RETRY
;
2175 vchiq_log_error(vchiq_susp_log_level
,
2176 "%s videocore suspend failed (state %s)", __func__
,
2177 suspend_state_names
[arm_state
->vc_suspend_state
+
2178 VC_SUSPEND_NUM_OFFSET
]);
2179 /* Reset the state only if it's still in an error state.
2180 * Something could have already initiated another suspend. */
2181 if (arm_state
->vc_suspend_state
< VC_SUSPEND_IDLE
)
2182 set_suspend_state(arm_state
, VC_SUSPEND_IDLE
);
2184 goto unblock_resume
;
2187 /* successfully suspended - unlock and exit */
2191 /* all error states need to unblock resume before exit */
2192 unblock_resume(arm_state
);
2195 write_unlock_bh(&arm_state
->susp_res_lock
);
2198 vchiq_log_trace(vchiq_susp_log_level
, "%s exit %d", __func__
, status
);
2203 vchiq_check_suspend(VCHIQ_STATE_T
*state
)
2205 VCHIQ_ARM_STATE_T
*arm_state
= vchiq_platform_get_arm_state(state
);
2210 vchiq_log_trace(vchiq_susp_log_level
, "%s", __func__
);
2212 write_lock_bh(&arm_state
->susp_res_lock
);
2213 if (arm_state
->vc_suspend_state
!= VC_SUSPEND_SUSPENDED
&&
2214 arm_state
->first_connect
&&
2215 !vchiq_videocore_wanted(state
)) {
2216 vchiq_arm_vcsuspend(state
);
2218 write_unlock_bh(&arm_state
->susp_res_lock
);
2221 vchiq_log_trace(vchiq_susp_log_level
, "%s exit", __func__
);
2227 vchiq_arm_allow_resume(VCHIQ_STATE_T
*state
)
2229 VCHIQ_ARM_STATE_T
*arm_state
= vchiq_platform_get_arm_state(state
);
2236 vchiq_log_trace(vchiq_susp_log_level
, "%s", __func__
);
2238 write_lock_bh(&arm_state
->susp_res_lock
);
2239 unblock_resume(arm_state
);
2240 resume
= vchiq_check_resume(state
);
2241 write_unlock_bh(&arm_state
->susp_res_lock
);
2244 if (wait_for_completion_interruptible(
2245 &arm_state
->vc_resume_complete
) < 0) {
2246 vchiq_log_error(vchiq_susp_log_level
,
2247 "%s interrupted", __func__
);
2248 /* failed, cannot accurately derive suspend
2249 * state, so exit early. */
2254 read_lock_bh(&arm_state
->susp_res_lock
);
2255 if (arm_state
->vc_suspend_state
== VC_SUSPEND_SUSPENDED
) {
2256 vchiq_log_info(vchiq_susp_log_level
,
2257 "%s: Videocore remains suspended", __func__
);
2259 vchiq_log_info(vchiq_susp_log_level
,
2260 "%s: Videocore resumed", __func__
);
2263 read_unlock_bh(&arm_state
->susp_res_lock
);
2265 vchiq_log_trace(vchiq_susp_log_level
, "%s exit %d", __func__
, ret
);
2269 /* This function should be called with the write lock held */
2271 vchiq_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 if (need_resume(state
)) {
2282 set_resume_state(arm_state
, VC_RESUME_REQUESTED
);
2283 request_poll(state
, NULL
, 0);
2288 vchiq_log_trace(vchiq_susp_log_level
, "%s exit", __func__
);
2293 vchiq_platform_check_resume(VCHIQ_STATE_T
*state
)
2295 VCHIQ_ARM_STATE_T
*arm_state
= vchiq_platform_get_arm_state(state
);
2301 vchiq_log_trace(vchiq_susp_log_level
, "%s", __func__
);
2303 write_lock_bh(&arm_state
->susp_res_lock
);
2304 if (arm_state
->wake_address
== 0) {
2305 vchiq_log_info(vchiq_susp_log_level
,
2306 "%s: already awake", __func__
);
2309 if (arm_state
->vc_resume_state
== VC_RESUME_IN_PROGRESS
) {
2310 vchiq_log_info(vchiq_susp_log_level
,
2311 "%s: already resuming", __func__
);
2315 if (arm_state
->vc_resume_state
== VC_RESUME_REQUESTED
) {
2316 set_resume_state(arm_state
, VC_RESUME_IN_PROGRESS
);
2319 vchiq_log_trace(vchiq_susp_log_level
,
2320 "%s: not resuming (resume state %s)", __func__
,
2321 resume_state_names
[arm_state
->vc_resume_state
+
2322 VC_RESUME_NUM_OFFSET
]);
2325 write_unlock_bh(&arm_state
->susp_res_lock
);
2328 vchiq_platform_resume(state
);
2331 vchiq_log_trace(vchiq_susp_log_level
, "%s exit", __func__
);
2339 vchiq_use_internal(VCHIQ_STATE_T
*state
, VCHIQ_SERVICE_T
*service
,
2340 enum USE_TYPE_E use_type
)
2342 VCHIQ_ARM_STATE_T
*arm_state
= vchiq_platform_get_arm_state(state
);
2343 VCHIQ_STATUS_T ret
= VCHIQ_SUCCESS
;
2346 int local_uc
, local_entity_uc
;
2351 vchiq_log_trace(vchiq_susp_log_level
, "%s", __func__
);
2353 if (use_type
== USE_TYPE_VCHIQ
) {
2354 sprintf(entity
, "VCHIQ: ");
2355 entity_uc
= &arm_state
->peer_use_count
;
2356 } else if (service
) {
2357 sprintf(entity
, "%c%c%c%c:%03d",
2358 VCHIQ_FOURCC_AS_4CHARS(service
->base
.fourcc
),
2359 service
->client_id
);
2360 entity_uc
= &service
->service_use_count
;
2362 vchiq_log_error(vchiq_susp_log_level
, "%s null service "
2368 write_lock_bh(&arm_state
->susp_res_lock
);
2369 while (arm_state
->resume_blocked
) {
2370 /* If we call 'use' while force suspend is waiting for suspend,
2371 * then we're about to block the thread which the force is
2372 * waiting to complete, so we're bound to just time out. In this
2373 * case, set the suspend state such that the wait will be
2374 * canceled, so we can complete as quickly as possible. */
2375 if (arm_state
->resume_blocked
&& arm_state
->vc_suspend_state
==
2377 set_suspend_state(arm_state
, VC_SUSPEND_FORCE_CANCELED
);
2380 /* If suspend is already in progress then we need to block */
2381 if (!try_wait_for_completion(&arm_state
->resume_blocker
)) {
2382 /* Indicate that there are threads waiting on the resume
2383 * blocker. These need to be allowed to complete before
2384 * a _second_ call to force suspend can complete,
2385 * otherwise low priority threads might never actually
2387 arm_state
->blocked_count
++;
2388 write_unlock_bh(&arm_state
->susp_res_lock
);
2389 vchiq_log_info(vchiq_susp_log_level
, "%s %s resume "
2390 "blocked - waiting...", __func__
, entity
);
2391 if (wait_for_completion_killable(
2392 &arm_state
->resume_blocker
) != 0) {
2393 vchiq_log_error(vchiq_susp_log_level
, "%s %s "
2394 "wait for resume blocker interrupted",
2397 write_lock_bh(&arm_state
->susp_res_lock
);
2398 arm_state
->blocked_count
--;
2399 write_unlock_bh(&arm_state
->susp_res_lock
);
2402 vchiq_log_info(vchiq_susp_log_level
, "%s %s resume "
2403 "unblocked", __func__
, entity
);
2404 write_lock_bh(&arm_state
->susp_res_lock
);
2405 if (--arm_state
->blocked_count
== 0)
2406 complete_all(&arm_state
->blocked_blocker
);
2410 stop_suspend_timer(arm_state
);
2412 local_uc
= ++arm_state
->videocore_use_count
;
2413 local_entity_uc
= ++(*entity_uc
);
2415 /* If there's a pending request which hasn't yet been serviced then
2416 * just clear it. If we're past VC_SUSPEND_REQUESTED state then
2417 * vc_resume_complete will block until we either resume or fail to
2419 if (arm_state
->vc_suspend_state
<= VC_SUSPEND_REQUESTED
)
2420 set_suspend_state(arm_state
, VC_SUSPEND_IDLE
);
2422 if ((use_type
!= USE_TYPE_SERVICE_NO_RESUME
) && need_resume(state
)) {
2423 set_resume_state(arm_state
, VC_RESUME_REQUESTED
);
2424 vchiq_log_info(vchiq_susp_log_level
,
2425 "%s %s count %d, state count %d",
2426 __func__
, entity
, local_entity_uc
, local_uc
);
2427 request_poll(state
, NULL
, 0);
2429 vchiq_log_trace(vchiq_susp_log_level
,
2430 "%s %s count %d, state count %d",
2431 __func__
, entity
, *entity_uc
, local_uc
);
2434 write_unlock_bh(&arm_state
->susp_res_lock
);
2436 /* Completion is in a done state when we're not suspended, so this won't
2437 * block for the non-suspended case. */
2438 if (!try_wait_for_completion(&arm_state
->vc_resume_complete
)) {
2439 vchiq_log_info(vchiq_susp_log_level
, "%s %s wait for resume",
2441 if (wait_for_completion_killable(
2442 &arm_state
->vc_resume_complete
) != 0) {
2443 vchiq_log_error(vchiq_susp_log_level
, "%s %s wait for "
2444 "resume interrupted", __func__
, entity
);
2448 vchiq_log_info(vchiq_susp_log_level
, "%s %s resumed", __func__
,
2452 if (ret
== VCHIQ_SUCCESS
) {
2453 VCHIQ_STATUS_T status
= VCHIQ_SUCCESS
;
2454 long ack_cnt
= atomic_xchg(&arm_state
->ka_use_ack_count
, 0);
2455 while (ack_cnt
&& (status
== VCHIQ_SUCCESS
)) {
2456 /* Send the use notify to videocore */
2457 status
= vchiq_send_remote_use_active(state
);
2458 if (status
== VCHIQ_SUCCESS
)
2462 &arm_state
->ka_use_ack_count
);
2467 vchiq_log_trace(vchiq_susp_log_level
, "%s exit %d", __func__
, ret
);
2472 vchiq_release_internal(VCHIQ_STATE_T
*state
, VCHIQ_SERVICE_T
*service
)
2474 VCHIQ_ARM_STATE_T
*arm_state
= vchiq_platform_get_arm_state(state
);
2475 VCHIQ_STATUS_T ret
= VCHIQ_SUCCESS
;
2478 int local_uc
, local_entity_uc
;
2483 vchiq_log_trace(vchiq_susp_log_level
, "%s", __func__
);
2486 sprintf(entity
, "%c%c%c%c:%03d",
2487 VCHIQ_FOURCC_AS_4CHARS(service
->base
.fourcc
),
2488 service
->client_id
);
2489 entity_uc
= &service
->service_use_count
;
2491 sprintf(entity
, "PEER: ");
2492 entity_uc
= &arm_state
->peer_use_count
;
2495 write_lock_bh(&arm_state
->susp_res_lock
);
2496 if (!arm_state
->videocore_use_count
|| !(*entity_uc
)) {
2497 /* Don't use BUG_ON - don't allow user thread to crash kernel */
2498 WARN_ON(!arm_state
->videocore_use_count
);
2499 WARN_ON(!(*entity_uc
));
2503 local_uc
= --arm_state
->videocore_use_count
;
2504 local_entity_uc
= --(*entity_uc
);
2506 if (!vchiq_videocore_wanted(state
)) {
2507 if (vchiq_platform_use_suspend_timer() &&
2508 !arm_state
->resume_blocked
) {
2509 /* Only use the timer if we're not trying to force
2510 * suspend (=> resume_blocked) */
2511 start_suspend_timer(arm_state
);
2513 vchiq_log_info(vchiq_susp_log_level
,
2514 "%s %s count %d, state count %d - suspending",
2515 __func__
, entity
, *entity_uc
,
2516 arm_state
->videocore_use_count
);
2517 vchiq_arm_vcsuspend(state
);
2520 vchiq_log_trace(vchiq_susp_log_level
,
2521 "%s %s count %d, state count %d",
2522 __func__
, entity
, *entity_uc
,
2523 arm_state
->videocore_use_count
);
2526 write_unlock_bh(&arm_state
->susp_res_lock
);
2529 vchiq_log_trace(vchiq_susp_log_level
, "%s exit %d", __func__
, ret
);
2534 vchiq_on_remote_use(VCHIQ_STATE_T
*state
)
2536 VCHIQ_ARM_STATE_T
*arm_state
= vchiq_platform_get_arm_state(state
);
2537 vchiq_log_trace(vchiq_susp_log_level
, "%s", __func__
);
2538 atomic_inc(&arm_state
->ka_use_count
);
2539 complete(&arm_state
->ka_evt
);
2543 vchiq_on_remote_release(VCHIQ_STATE_T
*state
)
2545 VCHIQ_ARM_STATE_T
*arm_state
= vchiq_platform_get_arm_state(state
);
2546 vchiq_log_trace(vchiq_susp_log_level
, "%s", __func__
);
2547 atomic_inc(&arm_state
->ka_release_count
);
2548 complete(&arm_state
->ka_evt
);
2552 vchiq_use_service_internal(VCHIQ_SERVICE_T
*service
)
2554 return vchiq_use_internal(service
->state
, service
, USE_TYPE_SERVICE
);
2558 vchiq_release_service_internal(VCHIQ_SERVICE_T
*service
)
2560 return vchiq_release_internal(service
->state
, service
);
2563 VCHIQ_DEBUGFS_NODE_T
*
2564 vchiq_instance_get_debugfs_node(VCHIQ_INSTANCE_T instance
)
2566 return &instance
->debugfs_node
;
2570 vchiq_instance_get_use_count(VCHIQ_INSTANCE_T instance
)
2572 VCHIQ_SERVICE_T
*service
;
2573 int use_count
= 0, i
;
2575 while ((service
= next_service_by_instance(instance
->state
,
2576 instance
, &i
)) != NULL
) {
2577 use_count
+= service
->service_use_count
;
2578 unlock_service(service
);
2584 vchiq_instance_get_pid(VCHIQ_INSTANCE_T instance
)
2586 return instance
->pid
;
2590 vchiq_instance_get_trace(VCHIQ_INSTANCE_T instance
)
2592 return instance
->trace
;
2596 vchiq_instance_set_trace(VCHIQ_INSTANCE_T instance
, int trace
)
2598 VCHIQ_SERVICE_T
*service
;
2601 while ((service
= next_service_by_instance(instance
->state
,
2602 instance
, &i
)) != NULL
) {
2603 service
->trace
= trace
;
2604 unlock_service(service
);
2606 instance
->trace
= (trace
!= 0);
2609 static void suspend_timer_callback(unsigned long context
)
2611 VCHIQ_STATE_T
*state
= (VCHIQ_STATE_T
*)context
;
2612 VCHIQ_ARM_STATE_T
*arm_state
= vchiq_platform_get_arm_state(state
);
2615 vchiq_log_info(vchiq_susp_log_level
,
2616 "%s - suspend timer expired - check suspend", __func__
);
2617 vchiq_check_suspend(state
);
2623 vchiq_use_service_no_resume(VCHIQ_SERVICE_HANDLE_T handle
)
2625 VCHIQ_STATUS_T ret
= VCHIQ_ERROR
;
2626 VCHIQ_SERVICE_T
*service
= find_service_by_handle(handle
);
2628 ret
= vchiq_use_internal(service
->state
, service
,
2629 USE_TYPE_SERVICE_NO_RESUME
);
2630 unlock_service(service
);
2636 vchiq_use_service(VCHIQ_SERVICE_HANDLE_T handle
)
2638 VCHIQ_STATUS_T ret
= VCHIQ_ERROR
;
2639 VCHIQ_SERVICE_T
*service
= find_service_by_handle(handle
);
2641 ret
= vchiq_use_internal(service
->state
, service
,
2643 unlock_service(service
);
2649 vchiq_release_service(VCHIQ_SERVICE_HANDLE_T handle
)
2651 VCHIQ_STATUS_T ret
= VCHIQ_ERROR
;
2652 VCHIQ_SERVICE_T
*service
= find_service_by_handle(handle
);
2654 ret
= vchiq_release_internal(service
->state
, service
);
2655 unlock_service(service
);
2661 vchiq_dump_service_use_state(VCHIQ_STATE_T
*state
)
2663 VCHIQ_ARM_STATE_T
*arm_state
= vchiq_platform_get_arm_state(state
);
2665 /* Only dump 64 services */
2666 static const int local_max_services
= 64;
2667 /* If there's more than 64 services, only dump ones with
2668 * non-zero counts */
2669 int only_nonzero
= 0;
2670 static const char *nz
= "<-- preventing suspend";
2672 enum vc_suspend_status vc_suspend_state
;
2673 enum vc_resume_status vc_resume_state
;
2676 int active_services
;
2677 struct service_data_struct
{
2681 } service_data
[local_max_services
];
2686 read_lock_bh(&arm_state
->susp_res_lock
);
2687 vc_suspend_state
= arm_state
->vc_suspend_state
;
2688 vc_resume_state
= arm_state
->vc_resume_state
;
2689 peer_count
= arm_state
->peer_use_count
;
2690 vc_use_count
= arm_state
->videocore_use_count
;
2691 active_services
= state
->unused_service
;
2692 if (active_services
> local_max_services
)
2695 for (i
= 0; (i
< active_services
) && (j
< local_max_services
); i
++) {
2696 VCHIQ_SERVICE_T
*service_ptr
= state
->services
[i
];
2700 if (only_nonzero
&& !service_ptr
->service_use_count
)
2703 if (service_ptr
->srvstate
!= VCHIQ_SRVSTATE_FREE
) {
2704 service_data
[j
].fourcc
= service_ptr
->base
.fourcc
;
2705 service_data
[j
].clientid
= service_ptr
->client_id
;
2706 service_data
[j
++].use_count
= service_ptr
->
2711 read_unlock_bh(&arm_state
->susp_res_lock
);
2713 vchiq_log_warning(vchiq_susp_log_level
,
2714 "-- Videcore suspend state: %s --",
2715 suspend_state_names
[vc_suspend_state
+ VC_SUSPEND_NUM_OFFSET
]);
2716 vchiq_log_warning(vchiq_susp_log_level
,
2717 "-- Videcore resume state: %s --",
2718 resume_state_names
[vc_resume_state
+ VC_RESUME_NUM_OFFSET
]);
2721 vchiq_log_warning(vchiq_susp_log_level
, "Too many active "
2722 "services (%d). Only dumping up to first %d services "
2723 "with non-zero use-count", active_services
,
2724 local_max_services
);
2726 for (i
= 0; i
< j
; i
++) {
2727 vchiq_log_warning(vchiq_susp_log_level
,
2728 "----- %c%c%c%c:%d service count %d %s",
2729 VCHIQ_FOURCC_AS_4CHARS(service_data
[i
].fourcc
),
2730 service_data
[i
].clientid
,
2731 service_data
[i
].use_count
,
2732 service_data
[i
].use_count
? nz
: "");
2734 vchiq_log_warning(vchiq_susp_log_level
,
2735 "----- VCHIQ use count count %d", peer_count
);
2736 vchiq_log_warning(vchiq_susp_log_level
,
2737 "--- Overall vchiq instance use count %d", vc_use_count
);
2739 vchiq_dump_platform_use_state(state
);
2743 vchiq_check_service(VCHIQ_SERVICE_T
*service
)
2745 VCHIQ_ARM_STATE_T
*arm_state
;
2746 VCHIQ_STATUS_T ret
= VCHIQ_ERROR
;
2748 if (!service
|| !service
->state
)
2751 vchiq_log_trace(vchiq_susp_log_level
, "%s", __func__
);
2753 arm_state
= vchiq_platform_get_arm_state(service
->state
);
2755 read_lock_bh(&arm_state
->susp_res_lock
);
2756 if (service
->service_use_count
)
2757 ret
= VCHIQ_SUCCESS
;
2758 read_unlock_bh(&arm_state
->susp_res_lock
);
2760 if (ret
== VCHIQ_ERROR
) {
2761 vchiq_log_error(vchiq_susp_log_level
,
2762 "%s ERROR - %c%c%c%c:%d service count %d, "
2763 "state count %d, videocore suspend state %s", __func__
,
2764 VCHIQ_FOURCC_AS_4CHARS(service
->base
.fourcc
),
2765 service
->client_id
, service
->service_use_count
,
2766 arm_state
->videocore_use_count
,
2767 suspend_state_names
[arm_state
->vc_suspend_state
+
2768 VC_SUSPEND_NUM_OFFSET
]);
2769 vchiq_dump_service_use_state(service
->state
);
2775 /* stub functions */
2776 void vchiq_on_remote_use_active(VCHIQ_STATE_T
*state
)
2781 void vchiq_platform_conn_state_changed(VCHIQ_STATE_T
*state
,
2782 VCHIQ_CONNSTATE_T oldstate
, VCHIQ_CONNSTATE_T newstate
)
2784 VCHIQ_ARM_STATE_T
*arm_state
= vchiq_platform_get_arm_state(state
);
2785 vchiq_log_info(vchiq_susp_log_level
, "%d: %s->%s", state
->id
,
2786 get_conn_state_name(oldstate
), get_conn_state_name(newstate
));
2787 if (state
->conn_state
== VCHIQ_CONNSTATE_CONNECTED
) {
2788 write_lock_bh(&arm_state
->susp_res_lock
);
2789 if (!arm_state
->first_connect
) {
2790 char threadname
[10];
2791 arm_state
->first_connect
= 1;
2792 write_unlock_bh(&arm_state
->susp_res_lock
);
2793 snprintf(threadname
, sizeof(threadname
), "VCHIQka-%d",
2795 arm_state
->ka_thread
= kthread_create(
2796 &vchiq_keepalive_thread_func
,
2799 if (IS_ERR(arm_state
->ka_thread
)) {
2800 vchiq_log_error(vchiq_susp_log_level
,
2801 "vchiq: FATAL: couldn't create thread %s",
2804 wake_up_process(arm_state
->ka_thread
);
2807 write_unlock_bh(&arm_state
->susp_res_lock
);
2811 static int vchiq_probe(struct platform_device
*pdev
)
2813 struct device_node
*fw_node
;
2814 struct rpi_firmware
*fw
;
2818 fw_node
= of_parse_phandle(pdev
->dev
.of_node
, "firmware", 0);
2820 dev_err(&pdev
->dev
, "Missing firmware node\n");
2824 fw
= rpi_firmware_get(fw_node
);
2825 of_node_put(fw_node
);
2827 return -EPROBE_DEFER
;
2829 platform_set_drvdata(pdev
, fw
);
2831 err
= vchiq_platform_init(pdev
, &g_state
);
2833 goto failed_platform_init
;
2835 err
= alloc_chrdev_region(&vchiq_devid
, VCHIQ_MINOR
, 1, DEVICE_NAME
);
2837 vchiq_log_error(vchiq_arm_log_level
,
2838 "Unable to allocate device number");
2839 goto failed_platform_init
;
2841 cdev_init(&vchiq_cdev
, &vchiq_fops
);
2842 vchiq_cdev
.owner
= THIS_MODULE
;
2843 err
= cdev_add(&vchiq_cdev
, vchiq_devid
, 1);
2845 vchiq_log_error(vchiq_arm_log_level
,
2846 "Unable to register device");
2847 goto failed_cdev_add
;
2850 /* create sysfs entries */
2851 vchiq_class
= class_create(THIS_MODULE
, DEVICE_NAME
);
2852 ptr_err
= vchiq_class
;
2853 if (IS_ERR(ptr_err
))
2854 goto failed_class_create
;
2856 vchiq_dev
= device_create(vchiq_class
, NULL
,
2857 vchiq_devid
, NULL
, "vchiq");
2858 ptr_err
= vchiq_dev
;
2859 if (IS_ERR(ptr_err
))
2860 goto failed_device_create
;
2862 /* create debugfs entries */
2863 err
= vchiq_debugfs_init();
2865 goto failed_debugfs_init
;
2867 vchiq_log_info(vchiq_arm_log_level
,
2868 "vchiq: initialised - version %d (min %d), device %d.%d",
2869 VCHIQ_VERSION
, VCHIQ_VERSION_MIN
,
2870 MAJOR(vchiq_devid
), MINOR(vchiq_devid
));
2874 failed_debugfs_init
:
2875 device_destroy(vchiq_class
, vchiq_devid
);
2876 failed_device_create
:
2877 class_destroy(vchiq_class
);
2878 failed_class_create
:
2879 cdev_del(&vchiq_cdev
);
2880 err
= PTR_ERR(ptr_err
);
2882 unregister_chrdev_region(vchiq_devid
, 1);
2883 failed_platform_init
:
2884 vchiq_log_warning(vchiq_arm_log_level
, "could not load vchiq");
2888 static int vchiq_remove(struct platform_device
*pdev
)
2890 vchiq_debugfs_deinit();
2891 device_destroy(vchiq_class
, vchiq_devid
);
2892 class_destroy(vchiq_class
);
2893 cdev_del(&vchiq_cdev
);
2894 unregister_chrdev_region(vchiq_devid
, 1);
2899 static const struct of_device_id vchiq_of_match
[] = {
2900 { .compatible
= "brcm,bcm2835-vchiq", },
2903 MODULE_DEVICE_TABLE(of
, vchiq_of_match
);
2905 static struct platform_driver vchiq_driver
= {
2907 .name
= "bcm2835_vchiq",
2908 .of_match_table
= vchiq_of_match
,
2910 .probe
= vchiq_probe
,
2911 .remove
= vchiq_remove
,
2913 module_platform_driver(vchiq_driver
);
2915 MODULE_LICENSE("GPL");
2916 MODULE_DESCRIPTION("Videocore VCHIQ driver");
2917 MODULE_AUTHOR("Broadcom Corporation");