]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - drivers/staging/vc04_services/interface/vchiq_arm/vchiq_arm.c
6611b0c8fe2de19c0dae9bf2168b5c42b027f7e3
[mirror_ubuntu-zesty-kernel.git] / drivers / staging / vc04_services / interface / vchiq_arm / vchiq_arm.c
1 /**
2 * Copyright (c) 2014 Raspberry Pi (Trading) Ltd. All rights reserved.
3 * Copyright (c) 2010-2012 Broadcom. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
17 *
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.
21 *
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.
33 */
34
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>
40 #include <linux/fs.h>
41 #include <linux/device.h>
42 #include <linux/mm.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>
48 #include <linux/of.h>
49 #include <linux/platform_device.h>
50 #include <soc/bcm2835/raspberrypi-firmware.h>
51
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"
57
58 #define DEVICE_NAME "vchiq"
59
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 "."
63
64 #define VCHIQ_MINOR 0
65
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
71
72 #define KEEPALIVE_VER 1
73 #define KEEPALIVE_VER_MIN KEEPALIVE_VER
74
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;
78
79 #define SUSPEND_TIMER_TIMEOUT_MS 100
80 #define SUSPEND_RETRY_TIMER_TIMEOUT_MS 1000
81
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",
86 "VC_SUSPEND_FAILED",
87 "VC_SUSPEND_IDLE",
88 "VC_SUSPEND_REQUESTED",
89 "VC_SUSPEND_IN_PROGRESS",
90 "VC_SUSPEND_SUSPENDED"
91 };
92 #define VC_RESUME_NUM_OFFSET 1 /* number of values before idle which are -ve */
93 static const char *const resume_state_names[] = {
94 "VC_RESUME_FAILED",
95 "VC_RESUME_IDLE",
96 "VC_RESUME_REQUESTED",
97 "VC_RESUME_IN_PROGRESS",
98 "VC_RESUME_RESUMED"
99 };
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.
103 */
104 #define FORCE_SUSPEND_FAIL_MAX 8
105
106 /* The time in ms allowed for videocore to go idle when force suspend has been
107 * requested */
108 #define FORCE_SUSPEND_TIMEOUT_MS 200
109
110
111 static void suspend_timer_callback(unsigned long context);
112
113
114 typedef struct user_service_struct {
115 VCHIQ_SERVICE_T *service;
116 void *userdata;
117 VCHIQ_INSTANCE_T instance;
118 char is_vchi;
119 char dequeue_pending;
120 char close_pending;
121 int message_available_pos;
122 int msg_insert;
123 int msg_remove;
124 struct semaphore insert_event;
125 struct semaphore remove_event;
126 struct semaphore close_event;
127 VCHIQ_HEADER_T * msg_queue[MSG_QUEUE_SIZE];
128 } USER_SERVICE_T;
129
130 struct bulk_waiter_node {
131 struct bulk_waiter bulk_waiter;
132 int pid;
133 struct list_head list;
134 };
135
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;
144
145 int connected;
146 int closing;
147 int pid;
148 int mark;
149 int use_close_delivered;
150 int trace;
151
152 struct list_head bulk_waiter_list;
153 struct mutex bulk_waiter_list_mutex;
154
155 VCHIQ_DEBUGFS_NODE_T debugfs_node;
156 };
157
158 typedef struct dump_context_struct {
159 char __user *buf;
160 size_t actual;
161 size_t space;
162 loff_t offset;
163 } DUMP_CONTEXT_T;
164
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);
171
172 static const char *const ioctl_names[] = {
173 "CONNECT",
174 "SHUTDOWN",
175 "CREATE_SERVICE",
176 "REMOVE_SERVICE",
177 "QUEUE_MESSAGE",
178 "QUEUE_BULK_TRANSMIT",
179 "QUEUE_BULK_RECEIVE",
180 "AWAIT_COMPLETION",
181 "DEQUEUE_MESSAGE",
182 "GET_CLIENT_ID",
183 "GET_CONFIG",
184 "CLOSE_SERVICE",
185 "USE_SERVICE",
186 "RELEASE_SERVICE",
187 "SET_SERVICE_OPTION",
188 "DUMP_PHYS_MEM",
189 "LIB_VERSION",
190 "CLOSE_DELIVERED"
191 };
192
193 vchiq_static_assert(ARRAY_SIZE(ioctl_names) ==
194 (VCHIQ_IOC_MAX + 1));
195
196 static void
197 dump_phys_mem(void *virt_addr, uint32_t num_bytes);
198
199 /****************************************************************************
200 *
201 * add_completion
202 *
203 ***************************************************************************/
204
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,
208 void *bulk_userdata)
209 {
210 VCHIQ_COMPLETION_DATA_T *completion;
211 DEBUG_INITIALISE(g_state.local)
212
213 mutex_lock(&instance->completion_mutex);
214
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);
222
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");
227 return VCHIQ_RETRY;
228 }
229
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;
236 }
237 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
238 }
239
240 completion =
241 &instance->completions[instance->completion_insert &
242 (MAX_COMPLETIONS - 1)];
243
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;
249
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;
256 }
257
258 /* A write barrier is needed here to ensure that the entire completion
259 record is written out before the insert point. */
260 wmb();
261
262 if (reason == VCHIQ_MESSAGE_AVAILABLE)
263 user_service->message_available_pos =
264 instance->completion_insert;
265
266 instance->completion_insert++;
267
268 mutex_unlock(&instance->completion_mutex);
269
270 up(&instance->insert_event);
271
272 return VCHIQ_SUCCESS;
273 }
274
275 /****************************************************************************
276 *
277 * service_callback
278 *
279 ***************************************************************************/
280
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)
284 {
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.
289 */
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)
295
296 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
297
298 service = handle_to_service(handle);
299 BUG_ON(!service);
300 user_service = (USER_SERVICE_T *)service->base.userdata;
301 instance = user_service->instance;
302
303 if (!instance || instance->closing)
304 return VCHIQ_SUCCESS;
305
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);
313
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
324 ** queue, add one
325 */
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);
336 return status;
337 }
338 }
339
340 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
341 if (down_interruptible(&user_service->remove_event)
342 != 0) {
343 vchiq_log_info(vchiq_arm_log_level,
344 "service_callback interrupted");
345 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
346 return VCHIQ_RETRY;
347 } else if (instance->closing) {
348 vchiq_log_info(vchiq_arm_log_level,
349 "service_callback closing");
350 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
351 return VCHIQ_ERROR;
352 }
353 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
354 spin_lock(&msg_queue_spinlock);
355 }
356
357 user_service->msg_queue[user_service->msg_insert &
358 (MSG_QUEUE_SIZE - 1)] = header;
359 user_service->msg_insert++;
360
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.
364 */
365 if (((user_service->message_available_pos -
366 instance->completion_remove) >= 0) ||
367 user_service->dequeue_pending) {
368 user_service->dequeue_pending = 0;
369 skip_completion = 1;
370 }
371
372 spin_unlock(&msg_queue_spinlock);
373
374 up(&user_service->insert_event);
375
376 header = NULL;
377 }
378
379 if (skip_completion) {
380 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
381 return VCHIQ_SUCCESS;
382 }
383
384 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
385
386 return add_completion(instance, reason, header, user_service,
387 bulk_userdata);
388 }
389
390 /****************************************************************************
391 *
392 * user_service_free
393 *
394 ***************************************************************************/
395 static void
396 user_service_free(void *userdata)
397 {
398 kfree(userdata);
399 }
400
401 /****************************************************************************
402 *
403 * close_delivered
404 *
405 ***************************************************************************/
406 static void close_delivered(USER_SERVICE_T *user_service)
407 {
408 vchiq_log_info(vchiq_arm_log_level,
409 "close_delivered(handle=%x)",
410 user_service->service->handle);
411
412 if (user_service->close_pending) {
413 /* Allow the underlying service to be culled */
414 unlock_service(user_service->service);
415
416 /* Wake the user-thread blocked in close_ or remove_service */
417 up(&user_service->close_event);
418
419 user_service->close_pending = 0;
420 }
421 }
422
423 /****************************************************************************
424 *
425 * vchiq_ioctl
426 *
427 ***************************************************************************/
428 static long
429 vchiq_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
430 {
431 VCHIQ_INSTANCE_T instance = file->private_data;
432 VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
433 VCHIQ_SERVICE_T *service = NULL;
434 long ret = 0;
435 int i, rc;
436 DEBUG_INITIALISE(g_state.local)
437
438 vchiq_log_trace(vchiq_arm_log_level,
439 "vchiq_ioctl - instance %pK, cmd %s, arg %lx",
440 instance,
441 ((_IOC_TYPE(cmd) == VCHIQ_IOC_MAGIC) &&
442 (_IOC_NR(cmd) <= VCHIQ_IOC_MAX)) ?
443 ioctl_names[_IOC_NR(cmd)] : "<invalid>", arg);
444
445 switch (cmd) {
446 case VCHIQ_IOC_SHUTDOWN:
447 if (!instance->connected)
448 break;
449
450 /* Remove all services */
451 i = 0;
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)
457 break;
458 }
459 service = NULL;
460
461 if (status == VCHIQ_SUCCESS) {
462 /* Wake the completion thread and ask it to exit */
463 instance->closing = 1;
464 up(&instance->insert_event);
465 }
466
467 break;
468
469 case VCHIQ_IOC_CONNECT:
470 if (instance->connected) {
471 ret = -EINVAL;
472 break;
473 }
474 rc = mutex_lock_killable(&instance->state->mutex);
475 if (rc != 0) {
476 vchiq_log_error(vchiq_arm_log_level,
477 "vchiq: connect: could not lock mutex for "
478 "state %d: %d",
479 instance->state->id, rc);
480 ret = -EINTR;
481 break;
482 }
483 status = vchiq_connect_internal(instance->state, instance);
484 mutex_unlock(&instance->state->mutex);
485
486 if (status == VCHIQ_SUCCESS)
487 instance->connected = 1;
488 else
489 vchiq_log_error(vchiq_arm_log_level,
490 "vchiq: could not connect: %d", status);
491 break;
492
493 case VCHIQ_IOC_CREATE_SERVICE: {
494 VCHIQ_CREATE_SERVICE_T args;
495 USER_SERVICE_T *user_service = NULL;
496 void *userdata;
497 int srvstate;
498
499 if (copy_from_user
500 (&args, (const void __user *)arg,
501 sizeof(args)) != 0) {
502 ret = -EFAULT;
503 break;
504 }
505
506 user_service = kmalloc(sizeof(USER_SERVICE_T), GFP_KERNEL);
507 if (!user_service) {
508 ret = -ENOMEM;
509 break;
510 }
511
512 if (args.is_open) {
513 if (!instance->connected) {
514 ret = -ENOTCONN;
515 kfree(user_service);
516 break;
517 }
518 srvstate = VCHIQ_SRVSTATE_OPENING;
519 } else {
520 srvstate =
521 instance->connected ?
522 VCHIQ_SRVSTATE_LISTENING :
523 VCHIQ_SRVSTATE_HIDDEN;
524 }
525
526 userdata = args.params.userdata;
527 args.params.callback = service_callback;
528 args.params.userdata = user_service;
529 service = vchiq_add_service_internal(
530 instance->state,
531 &args.params, srvstate,
532 instance, user_service_free);
533
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);
548
549 if (args.is_open) {
550 status = vchiq_open_service_internal
551 (service, instance->pid);
552 if (status != VCHIQ_SUCCESS) {
553 vchiq_remove_service(service->handle);
554 service = NULL;
555 ret = (status == VCHIQ_RETRY) ?
556 -EINTR : -EIO;
557 break;
558 }
559 }
560
561 if (copy_to_user((void __user *)
562 &(((VCHIQ_CREATE_SERVICE_T __user *)
563 arg)->handle),
564 (const void *)&service->handle,
565 sizeof(service->handle)) != 0) {
566 ret = -EFAULT;
567 vchiq_remove_service(service->handle);
568 }
569
570 service = NULL;
571 } else {
572 ret = -EEXIST;
573 kfree(user_service);
574 }
575 } break;
576
577 case VCHIQ_IOC_CLOSE_SERVICE: {
578 VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
579
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)
589 break;
590 }
591
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;
598 }
599 else
600 ret = -EINVAL;
601 } break;
602
603 case VCHIQ_IOC_REMOVE_SERVICE: {
604 VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
605
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)
615 break;
616 }
617
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;
624 }
625 else
626 ret = -EINVAL;
627 } break;
628
629 case VCHIQ_IOC_USE_SERVICE:
630 case VCHIQ_IOC_RELEASE_SERVICE: {
631 VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
632
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",
642 __func__,
643 (cmd == VCHIQ_IOC_USE_SERVICE) ?
644 "VCHIQ_IOC_USE_SERVICE" :
645 "VCHIQ_IOC_RELEASE_SERVICE",
646 status,
647 VCHIQ_FOURCC_AS_4CHARS(
648 service->base.fourcc),
649 service->client_id);
650 ret = -EINVAL;
651 }
652 } else
653 ret = -EINVAL;
654 } break;
655
656 case VCHIQ_IOC_QUEUE_MESSAGE: {
657 VCHIQ_QUEUE_MESSAGE_T args;
658 if (copy_from_user
659 (&args, (const void __user *)arg,
660 sizeof(args)) != 0) {
661 ret = -EFAULT;
662 break;
663 }
664
665 service = find_service_for_instance(instance, args.handle);
666
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
673 (args.handle,
674 elements, args.count);
675 else
676 ret = -EFAULT;
677 } else {
678 ret = -EINVAL;
679 }
680 } break;
681
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;
689
690 if (copy_from_user
691 (&args, (const void __user *)arg,
692 sizeof(args)) != 0) {
693 ret = -EFAULT;
694 break;
695 }
696
697 service = find_service_for_instance(instance, args.handle);
698 if (!service) {
699 ret = -EINVAL;
700 break;
701 }
702
703 if (args.mode == VCHIQ_BULK_MODE_BLOCKING) {
704 waiter = kzalloc(sizeof(struct bulk_waiter_node),
705 GFP_KERNEL);
706 if (!waiter) {
707 ret = -ENOMEM;
708 break;
709 }
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,
719 list);
720 list_del(pos);
721 break;
722 }
723
724 }
725 mutex_unlock(&instance->bulk_waiter_list_mutex);
726 if (!waiter) {
727 vchiq_log_error(vchiq_arm_log_level,
728 "no bulk_waiter found for pid %d",
729 current->pid);
730 ret = -ESRCH;
731 break;
732 }
733 vchiq_log_info(vchiq_arm_log_level,
734 "found bulk_waiter %pK for pid %d", waiter,
735 current->pid);
736 args.userdata = &waiter->bulk_waiter;
737 }
738 status = vchiq_bulk_transfer
739 (args.handle,
740 VCHI_MEM_HANDLE_INVALID,
741 args.data, args.size,
742 args.userdata, args.mode,
743 dir);
744 if (!waiter)
745 break;
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
750 ** completes. */
751 spin_lock(&bulk_waiter_spinlock);
752 waiter->bulk_waiter.bulk->userdata = NULL;
753 spin_unlock(&bulk_waiter_spinlock);
754 }
755 kfree(waiter);
756 } else {
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);
766
767 if (copy_to_user((void __user *)
768 &(((VCHIQ_QUEUE_BULK_TRANSFER_T __user *)
769 arg)->mode),
770 (const void *)&mode_waiting,
771 sizeof(mode_waiting)) != 0)
772 ret = -EFAULT;
773 }
774 } break;
775
776 case VCHIQ_IOC_AWAIT_COMPLETION: {
777 VCHIQ_AWAIT_COMPLETION_T args;
778
779 DEBUG_TRACE(AWAIT_COMPLETION_LINE);
780 if (!instance->connected) {
781 ret = -ENOTCONN;
782 break;
783 }
784
785 if (copy_from_user(&args, (const void __user *)arg,
786 sizeof(args)) != 0) {
787 ret = -EFAULT;
788 break;
789 }
790
791 mutex_lock(&instance->completion_mutex);
792
793 DEBUG_TRACE(AWAIT_COMPLETION_LINE);
794 while ((instance->completion_remove ==
795 instance->completion_insert)
796 && !instance->closing) {
797 int rc;
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);
802 if (rc != 0) {
803 DEBUG_TRACE(AWAIT_COMPLETION_LINE);
804 vchiq_log_info(vchiq_arm_log_level,
805 "AWAIT_COMPLETION interrupted");
806 ret = -EINTR;
807 break;
808 }
809 }
810 DEBUG_TRACE(AWAIT_COMPLETION_LINE);
811
812 /* A read memory barrier is needed to stop prefetch of a stale
813 ** completion record
814 */
815 rmb();
816
817 if (ret == 0) {
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)
826 break;
827 completion = &instance->completions[
828 instance->completion_remove &
829 (MAX_COMPLETIONS - 1)];
830
831 service = completion->service_userdata;
832 user_service = service->base.userdata;
833 completion->service_userdata =
834 user_service->userdata;
835
836 header = completion->header;
837 if (header) {
838 void __user *msgbuf;
839 int msglen;
840
841 msglen = header->size +
842 sizeof(VCHIQ_HEADER_T);
843 /* This must be a VCHIQ-style service */
844 if (args.msgbufsize < msglen) {
845 vchiq_log_error(
846 vchiq_arm_log_level,
847 "header %pK: msgbufsize %x < msglen %x",
848 header, args.msgbufsize,
849 msglen);
850 WARN(1, "invalid message "
851 "size\n");
852 if (ret == 0)
853 ret = -EMSGSIZE;
854 break;
855 }
856 if (msgbufcount <= 0)
857 /* Stall here for lack of a
858 ** buffer for the message. */
859 break;
860 /* Get the pointer from user space */
861 msgbufcount--;
862 if (copy_from_user(&msgbuf,
863 (const void __user *)
864 &args.msgbufs[msgbufcount],
865 sizeof(msgbuf)) != 0) {
866 if (ret == 0)
867 ret = -EFAULT;
868 break;
869 }
870
871 /* Copy the message to user space */
872 if (copy_to_user(msgbuf, header,
873 msglen) != 0) {
874 if (ret == 0)
875 ret = -EFAULT;
876 break;
877 }
878
879 /* Now it has been copied, the message
880 ** can be released. */
881 vchiq_release_message(service->handle,
882 header);
883
884 /* The completion must point to the
885 ** msgbuf. */
886 completion->header = msgbuf;
887 }
888
889 if ((completion->reason ==
890 VCHIQ_SERVICE_CLOSED) &&
891 !instance->use_close_delivered)
892 unlock_service(service);
893
894 if (copy_to_user((void __user *)(
895 (size_t)args.buf +
896 ret * sizeof(VCHIQ_COMPLETION_DATA_T)),
897 completion,
898 sizeof(VCHIQ_COMPLETION_DATA_T)) != 0) {
899 if (ret == 0)
900 ret = -EFAULT;
901 break;
902 }
903
904 instance->completion_remove++;
905 }
906
907 if (msgbufcount != args.msgbufcount) {
908 if (copy_to_user((void __user *)
909 &((VCHIQ_AWAIT_COMPLETION_T *)arg)->
910 msgbufcount,
911 &msgbufcount,
912 sizeof(msgbufcount)) != 0) {
913 ret = -EFAULT;
914 }
915 }
916 }
917
918 if (ret != 0)
919 up(&instance->remove_event);
920 mutex_unlock(&instance->completion_mutex);
921 DEBUG_TRACE(AWAIT_COMPLETION_LINE);
922 } break;
923
924 case VCHIQ_IOC_DEQUEUE_MESSAGE: {
925 VCHIQ_DEQUEUE_MESSAGE_T args;
926 USER_SERVICE_T *user_service;
927 VCHIQ_HEADER_T *header;
928
929 DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
930 if (copy_from_user
931 (&args, (const void __user *)arg,
932 sizeof(args)) != 0) {
933 ret = -EFAULT;
934 break;
935 }
936 service = find_service_for_instance(instance, args.handle);
937 if (!service) {
938 ret = -EINVAL;
939 break;
940 }
941 user_service = (USER_SERVICE_T *)service->base.userdata;
942 if (user_service->is_vchi == 0) {
943 ret = -EINVAL;
944 break;
945 }
946
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);
952 ret = -EWOULDBLOCK;
953 break;
954 }
955 user_service->dequeue_pending = 1;
956 do {
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");
963 ret = -EINTR;
964 break;
965 }
966 spin_lock(&msg_queue_spinlock);
967 } while (user_service->msg_remove ==
968 user_service->msg_insert);
969
970 if (ret)
971 break;
972 }
973
974 BUG_ON((int)(user_service->msg_insert -
975 user_service->msg_remove) < 0);
976
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);
981
982 up(&user_service->remove_event);
983 if (header == NULL)
984 ret = -ENOTCONN;
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,
989 header->data,
990 header->size) == 0)) {
991 ret = header->size;
992 vchiq_release_message(
993 service->handle,
994 header);
995 } else
996 ret = -EFAULT;
997 } else {
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");
1002 ret = -EMSGSIZE;
1003 }
1004 DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
1005 } break;
1006
1007 case VCHIQ_IOC_GET_CLIENT_ID: {
1008 VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
1009
1010 ret = vchiq_get_client_id(handle);
1011 } break;
1012
1013 case VCHIQ_IOC_GET_CONFIG: {
1014 VCHIQ_GET_CONFIG_T args;
1015 VCHIQ_CONFIG_T config;
1016
1017 if (copy_from_user(&args, (const void __user *)arg,
1018 sizeof(args)) != 0) {
1019 ret = -EFAULT;
1020 break;
1021 }
1022 if (args.config_size > sizeof(config)) {
1023 ret = -EINVAL;
1024 break;
1025 }
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) {
1030 ret = -EFAULT;
1031 break;
1032 }
1033 }
1034 } break;
1035
1036 case VCHIQ_IOC_SET_SERVICE_OPTION: {
1037 VCHIQ_SET_SERVICE_OPTION_T args;
1038
1039 if (copy_from_user(
1040 &args, (const void __user *)arg,
1041 sizeof(args)) != 0) {
1042 ret = -EFAULT;
1043 break;
1044 }
1045
1046 service = find_service_for_instance(instance, args.handle);
1047 if (!service) {
1048 ret = -EINVAL;
1049 break;
1050 }
1051
1052 status = vchiq_set_service_option(
1053 args.handle, args.option, args.value);
1054 } break;
1055
1056 case VCHIQ_IOC_DUMP_PHYS_MEM: {
1057 VCHIQ_DUMP_MEM_T args;
1058
1059 if (copy_from_user
1060 (&args, (const void __user *)arg,
1061 sizeof(args)) != 0) {
1062 ret = -EFAULT;
1063 break;
1064 }
1065 dump_phys_mem(args.virt_addr, args.num_bytes);
1066 } break;
1067
1068 case VCHIQ_IOC_LIB_VERSION: {
1069 unsigned int lib_version = (unsigned int)arg;
1070
1071 if (lib_version < VCHIQ_VERSION_MIN)
1072 ret = -EINVAL;
1073 else if (lib_version >= VCHIQ_VERSION_CLOSE_DELIVERED)
1074 instance->use_close_delivered = 1;
1075 } break;
1076
1077 case VCHIQ_IOC_CLOSE_DELIVERED: {
1078 VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
1079
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);
1085 }
1086 else
1087 ret = -EINVAL;
1088 } break;
1089
1090 default:
1091 ret = -ENOTTY;
1092 break;
1093 }
1094
1095 if (service)
1096 unlock_service(service);
1097
1098 if (ret == 0) {
1099 if (status == VCHIQ_ERROR)
1100 ret = -EIO;
1101 else if (status == VCHIQ_RETRY)
1102 ret = -EINTR;
1103 }
1104
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)] :
1112 "<invalid>",
1113 status, ret);
1114 else
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)] :
1120 "<invalid>",
1121 status, ret);
1122
1123 return ret;
1124 }
1125
1126 /****************************************************************************
1127 *
1128 * vchiq_open
1129 *
1130 ***************************************************************************/
1131
1132 static int
1133 vchiq_open(struct inode *inode, struct file *file)
1134 {
1135 int dev = iminor(inode) & 0x0f;
1136 vchiq_log_info(vchiq_arm_log_level, "vchiq_open");
1137 switch (dev) {
1138 case VCHIQ_MINOR: {
1139 int ret;
1140 VCHIQ_STATE_T *state = vchiq_get_state();
1141 VCHIQ_INSTANCE_T instance;
1142
1143 if (!state) {
1144 vchiq_log_error(vchiq_arm_log_level,
1145 "vchiq has no connection to VideoCore");
1146 return -ENOTCONN;
1147 }
1148
1149 instance = kzalloc(sizeof(*instance), GFP_KERNEL);
1150 if (!instance)
1151 return -ENOMEM;
1152
1153 instance->state = state;
1154 instance->pid = current->tgid;
1155
1156 ret = vchiq_debugfs_add_instance(instance);
1157 if (ret != 0) {
1158 kfree(instance);
1159 return ret;
1160 }
1161
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);
1167
1168 file->private_data = instance;
1169 } break;
1170
1171 default:
1172 vchiq_log_error(vchiq_arm_log_level,
1173 "Unknown minor device: %d", dev);
1174 return -ENXIO;
1175 }
1176
1177 return 0;
1178 }
1179
1180 /****************************************************************************
1181 *
1182 * vchiq_release
1183 *
1184 ***************************************************************************/
1185
1186 static int
1187 vchiq_release(struct inode *inode, struct file *file)
1188 {
1189 int dev = iminor(inode) & 0x0f;
1190 int ret = 0;
1191 switch (dev) {
1192 case VCHIQ_MINOR: {
1193 VCHIQ_INSTANCE_T instance = file->private_data;
1194 VCHIQ_STATE_T *state = vchiq_get_state();
1195 VCHIQ_SERVICE_T *service;
1196 int i;
1197
1198 vchiq_log_info(vchiq_arm_log_level,
1199 "vchiq_release: instance=%lx",
1200 (unsigned long)instance);
1201
1202 if (!state) {
1203 ret = -EPERM;
1204 goto out;
1205 }
1206
1207 /* Ensure videocore is awake to allow termination. */
1208 vchiq_use_internal(instance->state, NULL,
1209 USE_TYPE_VCHIQ);
1210
1211 mutex_lock(&instance->completion_mutex);
1212
1213 /* Wake the completion thread and ask it to exit */
1214 instance->closing = 1;
1215 up(&instance->insert_event);
1216
1217 mutex_unlock(&instance->completion_mutex);
1218
1219 /* Wake the slot handler if the completion queue is full. */
1220 up(&instance->remove_event);
1221
1222 /* Mark all services for termination... */
1223 i = 0;
1224 while ((service = next_service_by_instance(state, instance,
1225 &i)) != NULL) {
1226 USER_SERVICE_T *user_service = service->base.userdata;
1227
1228 /* Wake the slot handler if the msg queue is full. */
1229 up(&user_service->remove_event);
1230
1231 vchiq_terminate_service_internal(service);
1232 unlock_service(service);
1233 }
1234
1235 /* ...and wait for them to die */
1236 i = 0;
1237 while ((service = next_service_by_instance(state, instance, &i))
1238 != NULL) {
1239 USER_SERVICE_T *user_service = service->base.userdata;
1240
1241 down(&service->remove_event);
1242
1243 BUG_ON(service->srvstate != VCHIQ_SRVSTATE_FREE);
1244
1245 spin_lock(&msg_queue_spinlock);
1246
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);
1254
1255 if (header)
1256 vchiq_release_message(
1257 service->handle,
1258 header);
1259 spin_lock(&msg_queue_spinlock);
1260 }
1261
1262 spin_unlock(&msg_queue_spinlock);
1263
1264 unlock_service(service);
1265 }
1266
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)
1277 {
1278 USER_SERVICE_T *user_service =
1279 service->base.userdata;
1280
1281 /* Wake any blocked user-thread */
1282 if (instance->use_close_delivered)
1283 up(&user_service->close_event);
1284 unlock_service(service);
1285 }
1286 instance->completion_remove++;
1287 }
1288
1289 /* Release the PEER service count. */
1290 vchiq_release_internal(instance->state, NULL);
1291
1292 {
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,
1299 list);
1300 list_del(pos);
1301 vchiq_log_info(vchiq_arm_log_level,
1302 "bulk_waiter - cleaned up %pK for pid %d",
1303 waiter, waiter->pid);
1304 kfree(waiter);
1305 }
1306 }
1307
1308 vchiq_debugfs_remove_instance(instance);
1309
1310 kfree(instance);
1311 file->private_data = NULL;
1312 } break;
1313
1314 default:
1315 vchiq_log_error(vchiq_arm_log_level,
1316 "Unknown minor device: %d", dev);
1317 ret = -ENXIO;
1318 }
1319
1320 out:
1321 return ret;
1322 }
1323
1324 /****************************************************************************
1325 *
1326 * vchiq_dump
1327 *
1328 ***************************************************************************/
1329
1330 void
1331 vchiq_dump(void *dump_context, const char *str, int len)
1332 {
1333 DUMP_CONTEXT_T *context = (DUMP_CONTEXT_T *)dump_context;
1334
1335 if (context->actual < context->space) {
1336 int copy_bytes;
1337 if (context->offset > 0) {
1338 int skip_bytes = min(len, (int)context->offset);
1339 str += skip_bytes;
1340 len -= skip_bytes;
1341 context->offset -= skip_bytes;
1342 if (context->offset > 0)
1343 return;
1344 }
1345 copy_bytes = min(len, (int)(context->space - context->actual));
1346 if (copy_bytes == 0)
1347 return;
1348 if (copy_to_user(context->buf + context->actual, str,
1349 copy_bytes))
1350 context->actual = -EFAULT;
1351 context->actual += copy_bytes;
1352 len -= copy_bytes;
1353
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')) {
1358 char cr = '\n';
1359 if (copy_to_user(context->buf + context->actual - 1,
1360 &cr, 1))
1361 context->actual = -EFAULT;
1362 }
1363 }
1364 }
1365
1366 /****************************************************************************
1367 *
1368 * vchiq_dump_platform_instance_state
1369 *
1370 ***************************************************************************/
1371
1372 void
1373 vchiq_dump_platform_instances(void *dump_context)
1374 {
1375 VCHIQ_STATE_T *state = vchiq_get_state();
1376 char buf[80];
1377 int len;
1378 int i;
1379
1380 /* There is no list of instances, so instead scan all services,
1381 marking those that have been dumped. */
1382
1383 for (i = 0; i < state->unused_service; i++) {
1384 VCHIQ_SERVICE_T *service = state->services[i];
1385 VCHIQ_INSTANCE_T instance;
1386
1387 if (service && (service->base.callback == service_callback)) {
1388 instance = service->instance;
1389 if (instance)
1390 instance->mark = 0;
1391 }
1392 }
1393
1394 for (i = 0; i < state->unused_service; i++) {
1395 VCHIQ_SERVICE_T *service = state->services[i];
1396 VCHIQ_INSTANCE_T instance;
1397
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, " :
1405 "",
1406 instance->completion_insert -
1407 instance->completion_remove,
1408 MAX_COMPLETIONS);
1409
1410 vchiq_dump(dump_context, buf, len + 1);
1411
1412 instance->mark = 1;
1413 }
1414 }
1415 }
1416 }
1417
1418 /****************************************************************************
1419 *
1420 * vchiq_dump_platform_service_state
1421 *
1422 ***************************************************************************/
1423
1424 void
1425 vchiq_dump_platform_service_state(void *dump_context, VCHIQ_SERVICE_T *service)
1426 {
1427 USER_SERVICE_T *user_service = (USER_SERVICE_T *)service->base.userdata;
1428 char buf[80];
1429 int len;
1430
1431 len = snprintf(buf, sizeof(buf), " instance %pK", service->instance);
1432
1433 if ((service->base.callback == service_callback) &&
1434 user_service->is_vchi) {
1435 len += snprintf(buf + len, sizeof(buf) - len,
1436 ", %d/%d messages",
1437 user_service->msg_insert - user_service->msg_remove,
1438 MSG_QUEUE_SIZE);
1439
1440 if (user_service->dequeue_pending)
1441 len += snprintf(buf + len, sizeof(buf) - len,
1442 " (dequeue pending)");
1443 }
1444
1445 vchiq_dump(dump_context, buf, len + 1);
1446 }
1447
1448 /****************************************************************************
1449 *
1450 * dump_user_mem
1451 *
1452 ***************************************************************************/
1453
1454 static void
1455 dump_phys_mem(void *virt_addr, uint32_t num_bytes)
1456 {
1457 int rc;
1458 uint8_t *end_virt_addr = virt_addr + num_bytes;
1459 int num_pages;
1460 int offset;
1461 int end_offset;
1462 int page_idx;
1463 int prev_idx;
1464 struct page *page;
1465 struct page **pages;
1466 uint8_t *kmapped_virt_ptr;
1467
1468 /* Align virtAddr and endVirtAddr to 16 byte boundaries. */
1469
1470 virt_addr = (void *)((unsigned long)virt_addr & ~0x0fuL);
1471 end_virt_addr = (void *)(((unsigned long)end_virt_addr + 15uL) &
1472 ~0x0fuL);
1473
1474 offset = (int)(long)virt_addr & (PAGE_SIZE - 1);
1475 end_offset = (int)(long)end_virt_addr & (PAGE_SIZE - 1);
1476
1477 num_pages = (offset + num_bytes + PAGE_SIZE - 1) / PAGE_SIZE;
1478
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",
1483 num_pages);
1484 return;
1485 }
1486
1487 down_read(&current->mm->mmap_sem);
1488 rc = get_user_pages(
1489 (unsigned long)virt_addr, /* start */
1490 num_pages, /* len */
1491 0, /* gup_flags */
1492 pages, /* pages (array of page pointers) */
1493 NULL); /* vmas */
1494 up_read(&current->mm->mmap_sem);
1495
1496 prev_idx = -1;
1497 page = NULL;
1498
1499 if (rc < 0) {
1500 vchiq_log_error(vchiq_arm_log_level,
1501 "Failed to get user pages: %d\n", rc);
1502 goto out;
1503 }
1504
1505 while (offset < end_offset) {
1506
1507 int page_offset = offset % PAGE_SIZE;
1508 page_idx = offset / PAGE_SIZE;
1509
1510 if (page_idx != prev_idx) {
1511
1512 if (page != NULL)
1513 kunmap(page);
1514 page = pages[page_idx];
1515 kmapped_virt_ptr = kmap(page);
1516
1517 prev_idx = page_idx;
1518 }
1519
1520 if (vchiq_arm_log_level >= VCHIQ_LOG_TRACE)
1521 vchiq_log_dump_mem("ph",
1522 (uint32_t)(unsigned long)&kmapped_virt_ptr[
1523 page_offset],
1524 &kmapped_virt_ptr[page_offset], 16);
1525
1526 offset += 16;
1527 }
1528
1529 out:
1530 if (page != NULL)
1531 kunmap(page);
1532
1533 for (page_idx = 0; page_idx < num_pages; page_idx++)
1534 put_page(pages[page_idx]);
1535
1536 kfree(pages);
1537 }
1538
1539 /****************************************************************************
1540 *
1541 * vchiq_read
1542 *
1543 ***************************************************************************/
1544
1545 static ssize_t
1546 vchiq_read(struct file *file, char __user *buf,
1547 size_t count, loff_t *ppos)
1548 {
1549 DUMP_CONTEXT_T context;
1550 context.buf = buf;
1551 context.actual = 0;
1552 context.space = count;
1553 context.offset = *ppos;
1554
1555 vchiq_dump_state(&context, &g_state);
1556
1557 *ppos += context.actual;
1558
1559 return context.actual;
1560 }
1561
1562 VCHIQ_STATE_T *
1563 vchiq_get_state(void)
1564 {
1565
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);
1571
1572 return ((g_state.remote != NULL) &&
1573 (g_state.remote->initialised == 1)) ? &g_state : NULL;
1574 }
1575
1576 static const struct file_operations
1577 vchiq_fops = {
1578 .owner = THIS_MODULE,
1579 .unlocked_ioctl = vchiq_ioctl,
1580 .open = vchiq_open,
1581 .release = vchiq_release,
1582 .read = vchiq_read
1583 };
1584
1585 /*
1586 * Autosuspend related functionality
1587 */
1588
1589 int
1590 vchiq_videocore_wanted(VCHIQ_STATE_T *state)
1591 {
1592 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
1593 if (!arm_state)
1594 /* autosuspend not supported - always return wanted */
1595 return 1;
1596 else if (arm_state->blocked_count)
1597 return 1;
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)
1601 return 0;
1602 else
1603 return vchiq_platform_videocore_wanted(state);
1604 else
1605 /* non-zero usage count - videocore still required */
1606 return 1;
1607 }
1608
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,
1613 void *bulk_user)
1614 {
1615 vchiq_log_error(vchiq_susp_log_level,
1616 "%s callback reason %d", __func__, reason);
1617 return 0;
1618 }
1619
1620 static int
1621 vchiq_keepalive_thread_func(void *v)
1622 {
1623 VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
1624 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
1625
1626 VCHIQ_STATUS_T status;
1627 VCHIQ_INSTANCE_T instance;
1628 VCHIQ_SERVICE_HANDLE_T ka_handle;
1629
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
1635 };
1636
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);
1641 goto exit;
1642 }
1643
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);
1648 goto shutdown;
1649 }
1650
1651 status = vchiq_add_service(instance, &params, &ka_handle);
1652 if (status != VCHIQ_SUCCESS) {
1653 vchiq_log_error(vchiq_susp_log_level,
1654 "%s vchiq_open_service failed %d", __func__, status);
1655 goto shutdown;
1656 }
1657
1658 while (1) {
1659 long rc = 0, uc = 0;
1660 if (wait_for_completion_interruptible(&arm_state->ka_evt)
1661 != 0) {
1662 vchiq_log_error(vchiq_susp_log_level,
1663 "%s interrupted", __func__);
1664 flush_signals(current);
1665 continue;
1666 }
1667
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);
1672
1673 /* Call use/release service the requisite number of times.
1674 * Process use before release so use counts don't go negative */
1675 while (uc--) {
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",
1681 __func__, status);
1682 }
1683 }
1684 while (rc--) {
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",
1689 __func__, status);
1690 }
1691 }
1692 }
1693
1694 shutdown:
1695 vchiq_shutdown(instance);
1696 exit:
1697 return 0;
1698 }
1699
1700
1701
1702 VCHIQ_STATUS_T
1703 vchiq_arm_init_state(VCHIQ_STATE_T *state, VCHIQ_ARM_STATE_T *arm_state)
1704 {
1705 if (arm_state) {
1706 rwlock_init(&arm_state->susp_res_lock);
1707
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);
1712
1713 init_completion(&arm_state->vc_suspend_complete);
1714
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);
1719
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);
1724
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);
1729
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));
1734
1735 arm_state->first_connect = 0;
1736
1737 }
1738 return VCHIQ_SUCCESS;
1739 }
1740
1741 /*
1742 ** Functions to modify the state variables;
1743 ** set_suspend_state
1744 ** set_resume_state
1745 **
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.
1753 **
1754 ** In all states we set the state variable according to the input, but in some
1755 ** cases we perform additional steps outlined below;
1756 **
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
1762 ** failure.
1763 **
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.
1768 **
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.
1772 **
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.
1777 **
1778 ** VC_SUSPEND_FAILED - We failed to initiate videocore suspend. We notify the
1779 ** suspend completion and reset the resume state machine.
1780 **
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.
1788 **
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.
1793 **
1794 ** VC_RESUME_FAILED - Currently unused - no mechanism to fail resume exists.
1795 */
1796
1797 void
1798 set_suspend_state(VCHIQ_ARM_STATE_T *arm_state,
1799 enum vc_suspend_status new_state)
1800 {
1801 /* set the state in all cases */
1802 arm_state->vc_suspend_state = new_state;
1803
1804 /* state specific additional actions */
1805 switch (new_state) {
1806 case VC_SUSPEND_FORCE_CANCELED:
1807 complete_all(&arm_state->vc_suspend_complete);
1808 break;
1809 case VC_SUSPEND_REJECTED:
1810 complete_all(&arm_state->vc_suspend_complete);
1811 break;
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);
1816 break;
1817 case VC_SUSPEND_IDLE:
1818 reinit_completion(&arm_state->vc_suspend_complete);
1819 break;
1820 case VC_SUSPEND_REQUESTED:
1821 break;
1822 case VC_SUSPEND_IN_PROGRESS:
1823 set_resume_state(arm_state, VC_RESUME_IDLE);
1824 break;
1825 case VC_SUSPEND_SUSPENDED:
1826 complete_all(&arm_state->vc_suspend_complete);
1827 break;
1828 default:
1829 BUG();
1830 break;
1831 }
1832 }
1833
1834 void
1835 set_resume_state(VCHIQ_ARM_STATE_T *arm_state,
1836 enum vc_resume_status new_state)
1837 {
1838 /* set the state in all cases */
1839 arm_state->vc_resume_state = new_state;
1840
1841 /* state specific additional actions */
1842 switch (new_state) {
1843 case VC_RESUME_FAILED:
1844 break;
1845 case VC_RESUME_IDLE:
1846 reinit_completion(&arm_state->vc_resume_complete);
1847 break;
1848 case VC_RESUME_REQUESTED:
1849 break;
1850 case VC_RESUME_IN_PROGRESS:
1851 break;
1852 case VC_RESUME_RESUMED:
1853 complete_all(&arm_state->vc_resume_complete);
1854 set_suspend_state(arm_state, VC_SUSPEND_IDLE);
1855 break;
1856 default:
1857 BUG();
1858 break;
1859 }
1860 }
1861
1862
1863 /* should be called with the write lock held */
1864 inline void
1865 start_suspend_timer(VCHIQ_ARM_STATE_T *arm_state)
1866 {
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;
1873 }
1874
1875 /* should be called with the write lock held */
1876 static inline void
1877 stop_suspend_timer(VCHIQ_ARM_STATE_T *arm_state)
1878 {
1879 if (arm_state->suspend_timer_running) {
1880 del_timer(&arm_state->suspend_timer);
1881 arm_state->suspend_timer_running = 0;
1882 }
1883 }
1884
1885 static inline int
1886 need_resume(VCHIQ_STATE_T *state)
1887 {
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);
1892 }
1893
1894 static int
1895 block_resume(VCHIQ_ARM_STATE_T *arm_state)
1896 {
1897 int status = VCHIQ_SUCCESS;
1898 const unsigned long timeout_val =
1899 msecs_to_jiffies(FORCE_SUSPEND_TIMEOUT_MS);
1900 int resume_count = 0;
1901
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)
1914 <= 0) {
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);
1919 goto out;
1920 }
1921 vchiq_log_info(vchiq_susp_log_level, "%s previously blocked "
1922 "clients resumed", __func__);
1923 write_lock_bh(&arm_state->susp_res_lock);
1924 }
1925
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__);
1933 goto out;
1934 }
1935 write_unlock_bh(&arm_state->susp_res_lock);
1936 vchiq_log_info(vchiq_susp_log_level, "%s wait for resume",
1937 __func__);
1938 if (wait_for_completion_interruptible_timeout(
1939 &arm_state->vc_resume_complete, timeout_val)
1940 <= 0) {
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);
1947 goto out;
1948 }
1949 vchiq_log_info(vchiq_susp_log_level, "%s resumed", __func__);
1950 write_lock_bh(&arm_state->susp_res_lock);
1951 resume_count++;
1952 }
1953 reinit_completion(&arm_state->resume_blocker);
1954 arm_state->resume_blocked = 1;
1955
1956 out:
1957 return status;
1958 }
1959
1960 static inline void
1961 unblock_resume(VCHIQ_ARM_STATE_T *arm_state)
1962 {
1963 complete_all(&arm_state->resume_blocker);
1964 arm_state->resume_blocked = 0;
1965 }
1966
1967 /* Initiate suspend via slot handler. Should be called with the write lock
1968 * held */
1969 VCHIQ_STATUS_T
1970 vchiq_arm_vcsuspend(VCHIQ_STATE_T *state)
1971 {
1972 VCHIQ_STATUS_T status = VCHIQ_ERROR;
1973 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
1974
1975 if (!arm_state)
1976 goto out;
1977
1978 vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
1979 status = VCHIQ_SUCCESS;
1980
1981
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__);
1986 break;
1987 case VC_SUSPEND_IN_PROGRESS:
1988 vchiq_log_info(vchiq_susp_log_level, "%s: suspend already in "
1989 "progress", __func__);
1990 break;
1991
1992 default:
1993 /* We don't expect to be in other states, so log but continue
1994 * anyway */
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]);
1999 /* fall through */
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);
2004 /* fall through */
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);
2011 break;
2012 }
2013
2014 out:
2015 vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, status);
2016 return status;
2017 }
2018
2019 void
2020 vchiq_platform_check_suspend(VCHIQ_STATE_T *state)
2021 {
2022 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
2023 int susp = 0;
2024
2025 if (!arm_state)
2026 goto out;
2027
2028 vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
2029
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);
2034 susp = 1;
2035 }
2036 write_unlock_bh(&arm_state->susp_res_lock);
2037
2038 if (susp)
2039 vchiq_platform_suspend(state);
2040
2041 out:
2042 vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
2043 return;
2044 }
2045
2046
2047 static void
2048 output_timeout_error(VCHIQ_STATE_T *state)
2049 {
2050 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
2051 char err[50] = "";
2052 int vc_use_count = arm_state->videocore_use_count;
2053 int active_services = state->unused_service;
2054 int i;
2055
2056 if (!arm_state->videocore_use_count) {
2057 snprintf(err, sizeof(err), " Videocore usecount is 0");
2058 goto output_msg;
2059 }
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)");
2071 break;
2072 }
2073 }
2074
2075 output_msg:
2076 vchiq_log_error(vchiq_susp_log_level,
2077 "timed out waiting for vc suspend (%d).%s",
2078 arm_state->autosuspend_override, err);
2079
2080 }
2081
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
2086 ** report failure.
2087 **
2088 ** Returns VCHIQ_SUCCESS if videocore suspended successfully, VCHIQ_RETRY if
2089 ** videocore failed to suspend in time or VCHIQ_ERROR if interrupted.
2090 */
2091 VCHIQ_STATUS_T
2092 vchiq_arm_force_suspend(VCHIQ_STATE_T *state)
2093 {
2094 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
2095 VCHIQ_STATUS_T status = VCHIQ_ERROR;
2096 long rc = 0;
2097 int repeat = -1;
2098
2099 if (!arm_state)
2100 goto out;
2101
2102 vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
2103
2104 write_lock_bh(&arm_state->susp_res_lock);
2105
2106 status = block_resume(arm_state);
2107 if (status != VCHIQ_SUCCESS)
2108 goto unlock;
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",
2112 __func__);
2113 status = VCHIQ_SUCCESS;
2114 goto unlock;
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;
2128 } else {
2129 vchiq_log_warning(vchiq_susp_log_level, "%s failed too "
2130 "many times - attempting suspend", __func__);
2131 status = vchiq_arm_vcsuspend(state);
2132 }
2133 } else {
2134 vchiq_log_info(vchiq_susp_log_level, "%s videocore suspend "
2135 "in progress - wait for completion", __func__);
2136 status = VCHIQ_SUCCESS;
2137 }
2138
2139 /* Wait for suspend to happen due to system idle (not forced..) */
2140 if (status != VCHIQ_SUCCESS)
2141 goto unblock_resume;
2142
2143 do {
2144 write_unlock_bh(&arm_state->susp_res_lock);
2145
2146 rc = wait_for_completion_interruptible_timeout(
2147 &arm_state->vc_suspend_complete,
2148 msecs_to_jiffies(FORCE_SUSPEND_TIMEOUT_MS));
2149
2150 write_lock_bh(&arm_state->susp_res_lock);
2151 if (rc < 0) {
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 */
2159 if (repeat < 0) {
2160 repeat = 1;
2161 continue;
2162 }
2163 }
2164 arm_state->autosuspend_override++;
2165 output_timeout_error(state);
2166
2167 status = VCHIQ_RETRY;
2168 goto unblock_resume;
2169 }
2170 } while (0 < (repeat--));
2171
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);
2183
2184 goto unblock_resume;
2185 }
2186
2187 /* successfully suspended - unlock and exit */
2188 goto unlock;
2189
2190 unblock_resume:
2191 /* all error states need to unblock resume before exit */
2192 unblock_resume(arm_state);
2193
2194 unlock:
2195 write_unlock_bh(&arm_state->susp_res_lock);
2196
2197 out:
2198 vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, status);
2199 return status;
2200 }
2201
2202 void
2203 vchiq_check_suspend(VCHIQ_STATE_T *state)
2204 {
2205 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
2206
2207 if (!arm_state)
2208 goto out;
2209
2210 vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
2211
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);
2217 }
2218 write_unlock_bh(&arm_state->susp_res_lock);
2219
2220 out:
2221 vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
2222 return;
2223 }
2224
2225
2226 int
2227 vchiq_arm_allow_resume(VCHIQ_STATE_T *state)
2228 {
2229 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
2230 int resume = 0;
2231 int ret = -1;
2232
2233 if (!arm_state)
2234 goto out;
2235
2236 vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
2237
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);
2242
2243 if (resume) {
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. */
2250 goto out;
2251 }
2252 }
2253
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__);
2258 } else {
2259 vchiq_log_info(vchiq_susp_log_level,
2260 "%s: Videocore resumed", __func__);
2261 ret = 0;
2262 }
2263 read_unlock_bh(&arm_state->susp_res_lock);
2264 out:
2265 vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, ret);
2266 return ret;
2267 }
2268
2269 /* This function should be called with the write lock held */
2270 int
2271 vchiq_check_resume(VCHIQ_STATE_T *state)
2272 {
2273 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
2274 int resume = 0;
2275
2276 if (!arm_state)
2277 goto out;
2278
2279 vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
2280
2281 if (need_resume(state)) {
2282 set_resume_state(arm_state, VC_RESUME_REQUESTED);
2283 request_poll(state, NULL, 0);
2284 resume = 1;
2285 }
2286
2287 out:
2288 vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
2289 return resume;
2290 }
2291
2292 void
2293 vchiq_platform_check_resume(VCHIQ_STATE_T *state)
2294 {
2295 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
2296 int res = 0;
2297
2298 if (!arm_state)
2299 goto out;
2300
2301 vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
2302
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__);
2307 goto unlock;
2308 }
2309 if (arm_state->vc_resume_state == VC_RESUME_IN_PROGRESS) {
2310 vchiq_log_info(vchiq_susp_log_level,
2311 "%s: already resuming", __func__);
2312 goto unlock;
2313 }
2314
2315 if (arm_state->vc_resume_state == VC_RESUME_REQUESTED) {
2316 set_resume_state(arm_state, VC_RESUME_IN_PROGRESS);
2317 res = 1;
2318 } else
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]);
2323
2324 unlock:
2325 write_unlock_bh(&arm_state->susp_res_lock);
2326
2327 if (res)
2328 vchiq_platform_resume(state);
2329
2330 out:
2331 vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
2332 return;
2333
2334 }
2335
2336
2337
2338 VCHIQ_STATUS_T
2339 vchiq_use_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service,
2340 enum USE_TYPE_E use_type)
2341 {
2342 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
2343 VCHIQ_STATUS_T ret = VCHIQ_SUCCESS;
2344 char entity[16];
2345 int *entity_uc;
2346 int local_uc, local_entity_uc;
2347
2348 if (!arm_state)
2349 goto out;
2350
2351 vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
2352
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;
2361 } else {
2362 vchiq_log_error(vchiq_susp_log_level, "%s null service "
2363 "ptr", __func__);
2364 ret = VCHIQ_ERROR;
2365 goto out;
2366 }
2367
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 ==
2376 VC_SUSPEND_IDLE) {
2377 set_suspend_state(arm_state, VC_SUSPEND_FORCE_CANCELED);
2378 break;
2379 }
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
2386 * continue */
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",
2395 __func__, entity);
2396 ret = VCHIQ_ERROR;
2397 write_lock_bh(&arm_state->susp_res_lock);
2398 arm_state->blocked_count--;
2399 write_unlock_bh(&arm_state->susp_res_lock);
2400 goto out;
2401 }
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);
2407 }
2408 }
2409
2410 stop_suspend_timer(arm_state);
2411
2412 local_uc = ++arm_state->videocore_use_count;
2413 local_entity_uc = ++(*entity_uc);
2414
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
2418 * suspend */
2419 if (arm_state->vc_suspend_state <= VC_SUSPEND_REQUESTED)
2420 set_suspend_state(arm_state, VC_SUSPEND_IDLE);
2421
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);
2428 } else
2429 vchiq_log_trace(vchiq_susp_log_level,
2430 "%s %s count %d, state count %d",
2431 __func__, entity, *entity_uc, local_uc);
2432
2433
2434 write_unlock_bh(&arm_state->susp_res_lock);
2435
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",
2440 __func__, entity);
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);
2445 ret = VCHIQ_ERROR;
2446 goto out;
2447 }
2448 vchiq_log_info(vchiq_susp_log_level, "%s %s resumed", __func__,
2449 entity);
2450 }
2451
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)
2459 ack_cnt--;
2460 else
2461 atomic_add(ack_cnt,
2462 &arm_state->ka_use_ack_count);
2463 }
2464 }
2465
2466 out:
2467 vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, ret);
2468 return ret;
2469 }
2470
2471 VCHIQ_STATUS_T
2472 vchiq_release_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service)
2473 {
2474 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
2475 VCHIQ_STATUS_T ret = VCHIQ_SUCCESS;
2476 char entity[16];
2477 int *entity_uc;
2478 int local_uc, local_entity_uc;
2479
2480 if (!arm_state)
2481 goto out;
2482
2483 vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
2484
2485 if (service) {
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;
2490 } else {
2491 sprintf(entity, "PEER: ");
2492 entity_uc = &arm_state->peer_use_count;
2493 }
2494
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));
2500 ret = VCHIQ_ERROR;
2501 goto unlock;
2502 }
2503 local_uc = --arm_state->videocore_use_count;
2504 local_entity_uc = --(*entity_uc);
2505
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);
2512 } else {
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);
2518 }
2519 } else
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);
2524
2525 unlock:
2526 write_unlock_bh(&arm_state->susp_res_lock);
2527
2528 out:
2529 vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, ret);
2530 return ret;
2531 }
2532
2533 void
2534 vchiq_on_remote_use(VCHIQ_STATE_T *state)
2535 {
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);
2540 }
2541
2542 void
2543 vchiq_on_remote_release(VCHIQ_STATE_T *state)
2544 {
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);
2549 }
2550
2551 VCHIQ_STATUS_T
2552 vchiq_use_service_internal(VCHIQ_SERVICE_T *service)
2553 {
2554 return vchiq_use_internal(service->state, service, USE_TYPE_SERVICE);
2555 }
2556
2557 VCHIQ_STATUS_T
2558 vchiq_release_service_internal(VCHIQ_SERVICE_T *service)
2559 {
2560 return vchiq_release_internal(service->state, service);
2561 }
2562
2563 VCHIQ_DEBUGFS_NODE_T *
2564 vchiq_instance_get_debugfs_node(VCHIQ_INSTANCE_T instance)
2565 {
2566 return &instance->debugfs_node;
2567 }
2568
2569 int
2570 vchiq_instance_get_use_count(VCHIQ_INSTANCE_T instance)
2571 {
2572 VCHIQ_SERVICE_T *service;
2573 int use_count = 0, i;
2574 i = 0;
2575 while ((service = next_service_by_instance(instance->state,
2576 instance, &i)) != NULL) {
2577 use_count += service->service_use_count;
2578 unlock_service(service);
2579 }
2580 return use_count;
2581 }
2582
2583 int
2584 vchiq_instance_get_pid(VCHIQ_INSTANCE_T instance)
2585 {
2586 return instance->pid;
2587 }
2588
2589 int
2590 vchiq_instance_get_trace(VCHIQ_INSTANCE_T instance)
2591 {
2592 return instance->trace;
2593 }
2594
2595 void
2596 vchiq_instance_set_trace(VCHIQ_INSTANCE_T instance, int trace)
2597 {
2598 VCHIQ_SERVICE_T *service;
2599 int i;
2600 i = 0;
2601 while ((service = next_service_by_instance(instance->state,
2602 instance, &i)) != NULL) {
2603 service->trace = trace;
2604 unlock_service(service);
2605 }
2606 instance->trace = (trace != 0);
2607 }
2608
2609 static void suspend_timer_callback(unsigned long context)
2610 {
2611 VCHIQ_STATE_T *state = (VCHIQ_STATE_T *)context;
2612 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
2613 if (!arm_state)
2614 goto out;
2615 vchiq_log_info(vchiq_susp_log_level,
2616 "%s - suspend timer expired - check suspend", __func__);
2617 vchiq_check_suspend(state);
2618 out:
2619 return;
2620 }
2621
2622 VCHIQ_STATUS_T
2623 vchiq_use_service_no_resume(VCHIQ_SERVICE_HANDLE_T handle)
2624 {
2625 VCHIQ_STATUS_T ret = VCHIQ_ERROR;
2626 VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
2627 if (service) {
2628 ret = vchiq_use_internal(service->state, service,
2629 USE_TYPE_SERVICE_NO_RESUME);
2630 unlock_service(service);
2631 }
2632 return ret;
2633 }
2634
2635 VCHIQ_STATUS_T
2636 vchiq_use_service(VCHIQ_SERVICE_HANDLE_T handle)
2637 {
2638 VCHIQ_STATUS_T ret = VCHIQ_ERROR;
2639 VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
2640 if (service) {
2641 ret = vchiq_use_internal(service->state, service,
2642 USE_TYPE_SERVICE);
2643 unlock_service(service);
2644 }
2645 return ret;
2646 }
2647
2648 VCHIQ_STATUS_T
2649 vchiq_release_service(VCHIQ_SERVICE_HANDLE_T handle)
2650 {
2651 VCHIQ_STATUS_T ret = VCHIQ_ERROR;
2652 VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
2653 if (service) {
2654 ret = vchiq_release_internal(service->state, service);
2655 unlock_service(service);
2656 }
2657 return ret;
2658 }
2659
2660 void
2661 vchiq_dump_service_use_state(VCHIQ_STATE_T *state)
2662 {
2663 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
2664 int i, j = 0;
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";
2671
2672 enum vc_suspend_status vc_suspend_state;
2673 enum vc_resume_status vc_resume_state;
2674 int peer_count;
2675 int vc_use_count;
2676 int active_services;
2677 struct service_data_struct {
2678 int fourcc;
2679 int clientid;
2680 int use_count;
2681 } service_data[local_max_services];
2682
2683 if (!arm_state)
2684 return;
2685
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)
2693 only_nonzero = 1;
2694
2695 for (i = 0; (i < active_services) && (j < local_max_services); i++) {
2696 VCHIQ_SERVICE_T *service_ptr = state->services[i];
2697 if (!service_ptr)
2698 continue;
2699
2700 if (only_nonzero && !service_ptr->service_use_count)
2701 continue;
2702
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->
2707 service_use_count;
2708 }
2709 }
2710
2711 read_unlock_bh(&arm_state->susp_res_lock);
2712
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]);
2719
2720 if (only_nonzero)
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);
2725
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 : "");
2733 }
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);
2738
2739 vchiq_dump_platform_use_state(state);
2740 }
2741
2742 VCHIQ_STATUS_T
2743 vchiq_check_service(VCHIQ_SERVICE_T *service)
2744 {
2745 VCHIQ_ARM_STATE_T *arm_state;
2746 VCHIQ_STATUS_T ret = VCHIQ_ERROR;
2747
2748 if (!service || !service->state)
2749 goto out;
2750
2751 vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
2752
2753 arm_state = vchiq_platform_get_arm_state(service->state);
2754
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);
2759
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);
2770 }
2771 out:
2772 return ret;
2773 }
2774
2775 /* stub functions */
2776 void vchiq_on_remote_use_active(VCHIQ_STATE_T *state)
2777 {
2778 (void)state;
2779 }
2780
2781 void vchiq_platform_conn_state_changed(VCHIQ_STATE_T *state,
2782 VCHIQ_CONNSTATE_T oldstate, VCHIQ_CONNSTATE_T newstate)
2783 {
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",
2794 state->id);
2795 arm_state->ka_thread = kthread_create(
2796 &vchiq_keepalive_thread_func,
2797 (void *)state,
2798 threadname);
2799 if (IS_ERR(arm_state->ka_thread)) {
2800 vchiq_log_error(vchiq_susp_log_level,
2801 "vchiq: FATAL: couldn't create thread %s",
2802 threadname);
2803 } else {
2804 wake_up_process(arm_state->ka_thread);
2805 }
2806 } else
2807 write_unlock_bh(&arm_state->susp_res_lock);
2808 }
2809 }
2810
2811 static int vchiq_probe(struct platform_device *pdev)
2812 {
2813 struct device_node *fw_node;
2814 struct rpi_firmware *fw;
2815 int err;
2816 void *ptr_err;
2817
2818 fw_node = of_parse_phandle(pdev->dev.of_node, "firmware", 0);
2819 if (!fw_node) {
2820 dev_err(&pdev->dev, "Missing firmware node\n");
2821 return -ENOENT;
2822 }
2823
2824 fw = rpi_firmware_get(fw_node);
2825 of_node_put(fw_node);
2826 if (!fw)
2827 return -EPROBE_DEFER;
2828
2829 platform_set_drvdata(pdev, fw);
2830
2831 err = vchiq_platform_init(pdev, &g_state);
2832 if (err != 0)
2833 goto failed_platform_init;
2834
2835 err = alloc_chrdev_region(&vchiq_devid, VCHIQ_MINOR, 1, DEVICE_NAME);
2836 if (err != 0) {
2837 vchiq_log_error(vchiq_arm_log_level,
2838 "Unable to allocate device number");
2839 goto failed_platform_init;
2840 }
2841 cdev_init(&vchiq_cdev, &vchiq_fops);
2842 vchiq_cdev.owner = THIS_MODULE;
2843 err = cdev_add(&vchiq_cdev, vchiq_devid, 1);
2844 if (err != 0) {
2845 vchiq_log_error(vchiq_arm_log_level,
2846 "Unable to register device");
2847 goto failed_cdev_add;
2848 }
2849
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;
2855
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;
2861
2862 /* create debugfs entries */
2863 err = vchiq_debugfs_init();
2864 if (err != 0)
2865 goto failed_debugfs_init;
2866
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));
2871
2872 return 0;
2873
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);
2881 failed_cdev_add:
2882 unregister_chrdev_region(vchiq_devid, 1);
2883 failed_platform_init:
2884 vchiq_log_warning(vchiq_arm_log_level, "could not load vchiq");
2885 return err;
2886 }
2887
2888 static int vchiq_remove(struct platform_device *pdev)
2889 {
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);
2895
2896 return 0;
2897 }
2898
2899 static const struct of_device_id vchiq_of_match[] = {
2900 { .compatible = "brcm,bcm2835-vchiq", },
2901 {},
2902 };
2903 MODULE_DEVICE_TABLE(of, vchiq_of_match);
2904
2905 static struct platform_driver vchiq_driver = {
2906 .driver = {
2907 .name = "bcm2835_vchiq",
2908 .of_match_table = vchiq_of_match,
2909 },
2910 .probe = vchiq_probe,
2911 .remove = vchiq_remove,
2912 };
2913 module_platform_driver(vchiq_driver);
2914
2915 MODULE_LICENSE("GPL");
2916 MODULE_DESCRIPTION("Videocore VCHIQ driver");
2917 MODULE_AUTHOR("Broadcom Corporation");