vchiq_arm.c revision 1.6 1 /**
2 * Copyright (c) 2010-2012 Broadcom. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions, and the following disclaimer,
9 * without modification.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The names of the above-listed copyright holders may not be used
14 * to endorse or promote products derived from this software without
15 * specific prior written permission.
16 *
17 * ALTERNATIVELY, this software may be distributed under the terms of the
18 * GNU General Public License ("GPL") version 2, as published by the Free
19 * Software Foundation.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
22 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
25 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34 #include <sys/cdefs.h>
35 #include <sys/systm.h>
36 #include <sys/device.h>
37 #include <sys/file.h>
38 #include <sys/filedesc.h>
39
40 #include "vchiq_core.h"
41 #include "vchiq_ioctl.h"
42 #include "vchiq_arm.h"
43
44 #define DEVICE_NAME "vchiq"
45
46 /* Override the default prefix, which would be vchiq_arm (from the filename) */
47 #undef MODULE_PARAM_PREFIX
48 #define MODULE_PARAM_PREFIX DEVICE_NAME "."
49
50 #define VCHIQ_MINOR 0
51
52 /* Some per-instance constants */
53 #define MAX_COMPLETIONS 16
54 #define MAX_SERVICES 64
55 #define MAX_ELEMENTS 8
56 #define MSG_QUEUE_SIZE 64
57
58 #define KEEPALIVE_VER 1
59 #define KEEPALIVE_VER_MIN KEEPALIVE_VER
60
61 MALLOC_DEFINE(M_VCHIQ, "vchiq_cdev", "VideoCore cdev memory");
62
63 /* Run time control of log level, based on KERN_XXX level. */
64 int vchiq_arm_log_level = VCHIQ_LOG_DEFAULT;
65 int vchiq_susp_log_level = VCHIQ_LOG_ERROR;
66
67 #define SUSPEND_TIMER_TIMEOUT_MS 100
68 #define SUSPEND_RETRY_TIMER_TIMEOUT_MS 1000
69
70 #define VC_SUSPEND_NUM_OFFSET 3 /* number of values before idle which are -ve */
71 static const char *const suspend_state_names[] = {
72 "VC_SUSPEND_FORCE_CANCELED",
73 "VC_SUSPEND_REJECTED",
74 "VC_SUSPEND_FAILED",
75 "VC_SUSPEND_IDLE",
76 "VC_SUSPEND_REQUESTED",
77 "VC_SUSPEND_IN_PROGRESS",
78 "VC_SUSPEND_SUSPENDED"
79 };
80 #define VC_RESUME_NUM_OFFSET 1 /* number of values before idle which are -ve */
81 static const char *const resume_state_names[] = {
82 "VC_RESUME_FAILED",
83 "VC_RESUME_IDLE",
84 "VC_RESUME_REQUESTED",
85 "VC_RESUME_IN_PROGRESS",
86 "VC_RESUME_RESUMED"
87 };
88 /* The number of times we allow force suspend to timeout before actually
89 ** _forcing_ suspend. This is to cater for SW which fails to release vchiq
90 ** correctly - we don't want to prevent ARM suspend indefinitely in this case.
91 */
92 #define FORCE_SUSPEND_FAIL_MAX 8
93
94 /* The time in ms allowed for videocore to go idle when force suspend has been
95 * requested */
96 #define FORCE_SUSPEND_TIMEOUT_MS 200
97
98
99 static void suspend_timer_callback(unsigned long context);
100 #ifdef notyet
101 static int vchiq_proc_add_instance(VCHIQ_INSTANCE_T instance);
102 static void vchiq_proc_remove_instance(VCHIQ_INSTANCE_T instance);
103 #endif
104
105
106 typedef struct user_service_struct {
107 VCHIQ_SERVICE_T *service;
108 void *userdata;
109 VCHIQ_INSTANCE_T instance;
110 int is_vchi;
111 int dequeue_pending;
112 int message_available_pos;
113 int msg_insert;
114 int msg_remove;
115 struct semaphore insert_event;
116 struct semaphore remove_event;
117 VCHIQ_HEADER_T * msg_queue[MSG_QUEUE_SIZE];
118 } USER_SERVICE_T;
119
120 struct bulk_waiter_node {
121 struct bulk_waiter bulk_waiter;
122 struct lwp *l;
123 struct list_head list;
124 };
125
126 struct vchiq_instance_struct {
127 VCHIQ_STATE_T *state;
128 VCHIQ_COMPLETION_DATA_T completions[MAX_COMPLETIONS];
129 int completion_insert;
130 int completion_remove;
131 struct semaphore insert_event;
132 struct semaphore remove_event;
133 struct mutex completion_mutex;
134
135 int connected;
136 int closing;
137 struct lwp *l;
138 int mark;
139
140 struct list_head bulk_waiter_list;
141 struct mutex bulk_waiter_list_mutex;
142
143 struct proc_dir_entry *proc_entry;
144 };
145
146 typedef struct dump_context_struct {
147 char __user *buf;
148 size_t actual;
149 size_t space;
150 loff_t offset;
151 } DUMP_CONTEXT_T;
152
153 VCHIQ_STATE_T g_state;
154 static DEFINE_SPINLOCK(msg_queue_spinlock);
155
156 static const char *const ioctl_names[] = {
157 "CONNECT",
158 "SHUTDOWN",
159 "CREATE_SERVICE",
160 "REMOVE_SERVICE",
161 "QUEUE_MESSAGE",
162 "QUEUE_BULK_TRANSMIT",
163 "QUEUE_BULK_RECEIVE",
164 "AWAIT_COMPLETION",
165 "DEQUEUE_MESSAGE",
166 "GET_CLIENT_ID",
167 "GET_CONFIG",
168 "CLOSE_SERVICE",
169 "USE_SERVICE",
170 "RELEASE_SERVICE",
171 "SET_SERVICE_OPTION",
172 "DUMP_PHYS_MEM"
173 };
174
175 vchiq_static_assert((sizeof(ioctl_names)/sizeof(ioctl_names[0])) ==
176 (VCHIQ_IOC_MAX + 1));
177
178 static dev_type_open(vchiq_open);
179
180 struct cdevsw vchiq_cdevsw = {
181 .d_open = vchiq_open,
182 .d_close = noclose,
183 .d_read = noread,
184 .d_write = nowrite,
185 .d_ioctl = noioctl,
186 .d_stop = nostop,
187 .d_tty = notty,
188 .d_poll = nopoll,
189 .d_mmap = nommap,
190 .d_kqfilter = nokqfilter,
191 .d_flag = D_OTHER|D_MPSAFE,
192 };
193
194 extern struct cfdriver vchiq_cd;
195
196 static int vchiq_ioctl(struct file *, u_long, void *);
197 static int vchiq_close(struct file *);
198
199 static const struct fileops vchiq_fileops = {
200 .fo_read = fbadop_read,
201 .fo_write = fbadop_write,
202 .fo_ioctl = vchiq_ioctl,
203 .fo_fcntl = fnullop_fcntl,
204 .fo_poll = fnullop_poll,
205 .fo_stat = fbadop_stat,
206 .fo_close = vchiq_close,
207 .fo_kqfilter = fnullop_kqfilter,
208 };
209
210 #if 0
211 static void
212 dump_phys_mem(void *virt_addr, uint32_t num_bytes);
213 #endif
214
215 /****************************************************************************
216 *
217 * add_completion
218 *
219 ***************************************************************************/
220
221 static VCHIQ_STATUS_T
222 add_completion(VCHIQ_INSTANCE_T instance, VCHIQ_REASON_T reason,
223 VCHIQ_HEADER_T *header, USER_SERVICE_T *user_service,
224 void *bulk_userdata)
225 {
226 VCHIQ_COMPLETION_DATA_T *completion;
227 DEBUG_INITIALISE(g_state.local)
228
229 while (instance->completion_insert ==
230 (instance->completion_remove + MAX_COMPLETIONS)) {
231 /* Out of space - wait for the client */
232 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
233 vchiq_log_trace(vchiq_arm_log_level,
234 "add_completion - completion queue full");
235 DEBUG_COUNT(COMPLETION_QUEUE_FULL_COUNT);
236 if (down_interruptible(&instance->remove_event) != 0) {
237 vchiq_log_info(vchiq_arm_log_level,
238 "service_callback interrupted");
239 return VCHIQ_RETRY;
240 } else if (instance->closing) {
241 vchiq_log_info(vchiq_arm_log_level,
242 "service_callback closing");
243 return VCHIQ_ERROR;
244 }
245 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
246 }
247
248 completion =
249 &instance->completions[instance->completion_insert &
250 (MAX_COMPLETIONS - 1)];
251
252 completion->header = header;
253 completion->reason = reason;
254 /* N.B. service_userdata is updated while processing AWAIT_COMPLETION */
255 completion->service_userdata = user_service->service;
256 completion->bulk_userdata = bulk_userdata;
257
258 if (reason == VCHIQ_SERVICE_CLOSED)
259 /* Take an extra reference, to be held until
260 this CLOSED notification is delivered. */
261 lock_service(user_service->service);
262
263 /* A write barrier is needed here to ensure that the entire completion
264 record is written out before the insert point. */
265 wmb();
266
267 if (reason == VCHIQ_MESSAGE_AVAILABLE)
268 user_service->message_available_pos =
269 instance->completion_insert;
270 instance->completion_insert++;
271
272 up(&instance->insert_event);
273
274 return VCHIQ_SUCCESS;
275 }
276
277 /****************************************************************************
278 *
279 * service_callback
280 *
281 ***************************************************************************/
282
283 static VCHIQ_STATUS_T
284 service_callback(VCHIQ_REASON_T reason, VCHIQ_HEADER_T *header,
285 VCHIQ_SERVICE_HANDLE_T handle, void *bulk_userdata)
286 {
287 /* How do we ensure the callback goes to the right client?
288 ** The service_user data points to a USER_SERVICE_T record containing
289 ** the original callback and the user state structure, which contains a
290 ** circular buffer for completion records.
291 */
292 USER_SERVICE_T *user_service;
293 VCHIQ_SERVICE_T *service;
294 VCHIQ_INSTANCE_T instance;
295 DEBUG_INITIALISE(g_state.local)
296
297 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
298
299 service = handle_to_service(handle);
300 BUG_ON(!service);
301 user_service = (USER_SERVICE_T *)service->base.userdata;
302 instance = user_service->instance;
303
304 if (!instance || instance->closing)
305 return VCHIQ_SUCCESS;
306
307 vchiq_log_trace(vchiq_arm_log_level,
308 "service_callback - service %lx(%d), handle %x, reason %d, header %lx, "
309 "instance %lx, bulk_userdata %lx",
310 (unsigned long)user_service,
311 service->localport, service->handle,
312 reason, (unsigned long)header,
313 (unsigned long)instance, (unsigned long)bulk_userdata);
314
315 if (header && user_service->is_vchi) {
316 spin_lock(&msg_queue_spinlock);
317 while (user_service->msg_insert ==
318 (user_service->msg_remove + MSG_QUEUE_SIZE)) {
319 spin_unlock(&msg_queue_spinlock);
320 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
321 DEBUG_COUNT(MSG_QUEUE_FULL_COUNT);
322 vchiq_log_trace(vchiq_arm_log_level,
323 "service_callback - msg queue full");
324 /* If there is no MESSAGE_AVAILABLE in the completion
325 ** queue, add one
326 */
327 if ((user_service->message_available_pos -
328 instance->completion_remove) < 0) {
329 VCHIQ_STATUS_T status;
330 vchiq_log_info(vchiq_arm_log_level,
331 "Inserting extra MESSAGE_AVAILABLE");
332 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
333 status = add_completion(instance, reason,
334 NULL, user_service, bulk_userdata);
335 if (status != VCHIQ_SUCCESS) {
336 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
337 return status;
338 }
339 }
340
341 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
342 if (down_interruptible(&user_service->remove_event)
343 != 0) {
344 vchiq_log_info(vchiq_arm_log_level,
345 "service_callback interrupted");
346 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
347 return VCHIQ_RETRY;
348 } else if (instance->closing) {
349 vchiq_log_info(vchiq_arm_log_level,
350 "service_callback closing");
351 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
352 return VCHIQ_ERROR;
353 }
354 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
355 spin_lock(&msg_queue_spinlock);
356 }
357
358 user_service->msg_queue[user_service->msg_insert &
359 (MSG_QUEUE_SIZE - 1)] = header;
360 user_service->msg_insert++;
361 spin_unlock(&msg_queue_spinlock);
362
363 up(&user_service->insert_event);
364
365 /* If there is a thread waiting in DEQUEUE_MESSAGE, or if
366 ** there is a MESSAGE_AVAILABLE in the completion queue then
367 ** bypass the completion queue.
368 */
369 if (((user_service->message_available_pos -
370 instance->completion_remove) >= 0) ||
371 user_service->dequeue_pending) {
372 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
373 user_service->dequeue_pending = 0;
374 return VCHIQ_SUCCESS;
375 }
376
377 header = NULL;
378 }
379 DEBUG_TRACE(SERVICE_CALLBACK_LINE);
380
381 return add_completion(instance, reason, header, user_service,
382 bulk_userdata);
383 }
384
385 /****************************************************************************
386 *
387 * vchiq_ioctl
388 *
389 ***************************************************************************/
390
391 static int
392 vchiq_ioctl(struct file *fp, u_long cmd, void *arg)
393 {
394 VCHIQ_INSTANCE_T instance;
395 VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
396 VCHIQ_SERVICE_T *service = NULL;
397 int ret = 0;
398 int i, rc;
399 DEBUG_INITIALISE(g_state.local)
400
401 instance = fp->f_data;
402
403 /* XXXBSD: HACK! */
404 #define _IOC_NR(x) ((x) & 0xff)
405 #define _IOC_TYPE(x) IOCGROUP(x)
406
407 vchiq_log_trace(vchiq_arm_log_level,
408 "vchiq_ioctl - instance %x, cmd %s, arg %p",
409 (unsigned int)instance,
410 ((_IOC_TYPE(cmd) == VCHIQ_IOC_MAGIC) &&
411 (_IOC_NR(cmd) <= VCHIQ_IOC_MAX)) ?
412 ioctl_names[_IOC_NR(cmd)] : "<invalid>", arg);
413
414 switch (cmd) {
415 case VCHIQ_IOC_SHUTDOWN:
416 if (!instance->connected)
417 break;
418
419 /* Remove all services */
420 i = 0;
421 while ((service = next_service_by_instance(instance->state,
422 instance, &i)) != NULL) {
423 status = vchiq_remove_service(service->handle);
424 unlock_service(service);
425 if (status != VCHIQ_SUCCESS)
426 break;
427 }
428 service = NULL;
429
430 if (status == VCHIQ_SUCCESS) {
431 /* Wake the completion thread and ask it to exit */
432 instance->closing = 1;
433 up(&instance->insert_event);
434 }
435
436 break;
437
438 case VCHIQ_IOC_CONNECT:
439 if (instance->connected) {
440 ret = -EINVAL;
441 break;
442 }
443 rc = lmutex_lock_interruptible(&instance->state->mutex);
444 if (rc != 0) {
445 vchiq_log_error(vchiq_arm_log_level,
446 "vchiq: connect: could not lock mutex for "
447 "state %d: %d",
448 instance->state->id, rc);
449 ret = -EINTR;
450 break;
451 }
452 status = vchiq_connect_internal(instance->state, instance);
453 lmutex_unlock(&instance->state->mutex);
454
455 if (status == VCHIQ_SUCCESS)
456 instance->connected = 1;
457 else
458 vchiq_log_error(vchiq_arm_log_level,
459 "vchiq: could not connect: %d", status);
460 break;
461
462 case VCHIQ_IOC_CREATE_SERVICE: {
463 VCHIQ_CREATE_SERVICE_T *pargs = arg;
464 USER_SERVICE_T *user_service = NULL;
465 void *userdata;
466 int srvstate;
467
468 user_service = kmalloc(sizeof(USER_SERVICE_T), GFP_KERNEL);
469 if (!user_service) {
470 ret = -ENOMEM;
471 break;
472 }
473
474 if (pargs->is_open) {
475 if (!instance->connected) {
476 ret = -ENOTCONN;
477 kfree(user_service);
478 break;
479 }
480 srvstate = VCHIQ_SRVSTATE_OPENING;
481 } else {
482 srvstate =
483 instance->connected ?
484 VCHIQ_SRVSTATE_LISTENING :
485 VCHIQ_SRVSTATE_HIDDEN;
486 }
487
488 userdata = pargs->params.userdata;
489 pargs->params.callback = service_callback;
490 pargs->params.userdata = user_service;
491 service = vchiq_add_service_internal(
492 instance->state,
493 &pargs->params, srvstate,
494 instance);
495
496 if (service != NULL) {
497 user_service->service = service;
498 user_service->userdata = userdata;
499 user_service->instance = instance;
500 user_service->is_vchi = pargs->is_vchi;
501 user_service->dequeue_pending = 0;
502 user_service->message_available_pos =
503 instance->completion_remove - 1;
504 user_service->msg_insert = 0;
505 user_service->msg_remove = 0;
506 _sema_init(&user_service->insert_event, 0);
507 _sema_init(&user_service->remove_event, 0);
508
509 if (pargs->is_open) {
510 status = vchiq_open_service_internal
511 (service, (uintptr_t)instance->l);
512 if (status != VCHIQ_SUCCESS) {
513 vchiq_remove_service(service->handle);
514 service = NULL;
515 ret = (status == VCHIQ_RETRY) ?
516 -EINTR : -EIO;
517 user_service->service = NULL;
518 user_service->instance = NULL;
519 break;
520 }
521 }
522
523 #ifdef VCHIQ_IOCTL_DEBUG
524 printf("%s: [CREATE SERVICE] handle = %08x\n", __func__, service->handle);
525 #endif
526 pargs->handle = service->handle;
527
528 service = NULL;
529 } else {
530 ret = -EEXIST;
531 kfree(user_service);
532 }
533 } break;
534
535 case VCHIQ_IOC_CLOSE_SERVICE: {
536 VCHIQ_SERVICE_HANDLE_T handle = *(VCHIQ_SERVICE_HANDLE_T *)arg;
537
538 #ifdef VCHIQ_IOCTL_DEBUG
539 printf("%s: [CLOSE SERVICE] handle = %08x\n", __func__, handle);
540 #endif
541
542 service = find_service_for_instance(instance, handle);
543 if (service != NULL)
544 status = vchiq_close_service(service->handle);
545 else
546 ret = -EINVAL;
547 } break;
548
549 case VCHIQ_IOC_REMOVE_SERVICE: {
550 VCHIQ_SERVICE_HANDLE_T handle = *(VCHIQ_SERVICE_HANDLE_T *)arg;
551
552 #ifdef VCHIQ_IOCTL_DEBUG
553 printf("%s: [REMOVE SERVICE] handle = %08x\n", __func__, handle);
554 #endif
555
556 service = find_service_for_instance(instance, handle);
557 if (service != NULL)
558 status = vchiq_remove_service(service->handle);
559 else
560 ret = -EINVAL;
561 } break;
562
563 case VCHIQ_IOC_USE_SERVICE:
564 case VCHIQ_IOC_RELEASE_SERVICE: {
565 VCHIQ_SERVICE_HANDLE_T handle = *(VCHIQ_SERVICE_HANDLE_T *)arg;
566
567 #ifdef VCHIQ_IOCTL_DEBUG
568 printf("%s: [%s SERVICE] handle = %08x\n", __func__,
569 cmd == VCHIQ_IOC_USE_SERVICE ? "USE" : "RELEASE", handle);
570 #endif
571
572 service = find_service_for_instance(instance, handle);
573 if (service != NULL) {
574 status = (cmd == VCHIQ_IOC_USE_SERVICE) ?
575 vchiq_use_service_internal(service) :
576 vchiq_release_service_internal(service);
577 if (status != VCHIQ_SUCCESS) {
578 vchiq_log_error(vchiq_susp_log_level,
579 "%s: cmd %s returned error %d for "
580 "service %c%c%c%c:%03d",
581 __func__,
582 (cmd == VCHIQ_IOC_USE_SERVICE) ?
583 "VCHIQ_IOC_USE_SERVICE" :
584 "VCHIQ_IOC_RELEASE_SERVICE",
585 status,
586 VCHIQ_FOURCC_AS_4CHARS(
587 service->base.fourcc),
588 service->client_id);
589 ret = -EINVAL;
590 }
591 } else
592 ret = -EINVAL;
593 } break;
594
595 case VCHIQ_IOC_QUEUE_MESSAGE: {
596 VCHIQ_QUEUE_MESSAGE_T *pargs = arg;
597
598 #ifdef VCHIQ_IOCTL_DEBUG
599 printf("%s: [QUEUE MESSAGE] handle = %08x\n", __func__, pargs->handle);
600 #endif
601
602 service = find_service_for_instance(instance, pargs->handle);
603
604 if ((service != NULL) && (pargs->count <= MAX_ELEMENTS)) {
605 /* Copy elements into kernel space */
606 VCHIQ_ELEMENT_T elements[MAX_ELEMENTS];
607 if (copy_from_user(elements, pargs->elements,
608 pargs->count * sizeof(VCHIQ_ELEMENT_T)) == 0)
609 status = vchiq_queue_message
610 (pargs->handle,
611 elements, pargs->count);
612 else
613 ret = -EFAULT;
614 } else {
615 ret = -EINVAL;
616 }
617 } break;
618
619 case VCHIQ_IOC_QUEUE_BULK_TRANSMIT:
620 case VCHIQ_IOC_QUEUE_BULK_RECEIVE: {
621 VCHIQ_QUEUE_BULK_TRANSFER_T *pargs = arg;
622 struct bulk_waiter_node *waiter = NULL;
623 VCHIQ_BULK_DIR_T dir =
624 (cmd == VCHIQ_IOC_QUEUE_BULK_TRANSMIT) ?
625 VCHIQ_BULK_TRANSMIT : VCHIQ_BULK_RECEIVE;
626
627 service = find_service_for_instance(instance, pargs->handle);
628 if (!service) {
629 ret = -EINVAL;
630 break;
631 }
632
633 if (pargs->mode == VCHIQ_BULK_MODE_BLOCKING) {
634 waiter = kzalloc(sizeof(struct bulk_waiter_node),
635 GFP_KERNEL);
636 if (!waiter) {
637 ret = -ENOMEM;
638 break;
639 }
640 pargs->userdata = &waiter->bulk_waiter;
641 } else if (pargs->mode == VCHIQ_BULK_MODE_WAITING) {
642 struct list_head *pos;
643 lmutex_lock(&instance->bulk_waiter_list_mutex);
644 list_for_each(pos, &instance->bulk_waiter_list) {
645 if (list_entry(pos, struct bulk_waiter_node,
646 list)->l == current) {
647 waiter = list_entry(pos,
648 struct bulk_waiter_node,
649 list);
650 list_del(pos);
651 break;
652 }
653
654 }
655 lmutex_unlock(&instance->bulk_waiter_list_mutex);
656 if (!waiter) {
657 vchiq_log_error(vchiq_arm_log_level,
658 "no bulk_waiter found for pid %d",
659 current->l_proc->p_pid);
660 ret = -ESRCH;
661 break;
662 }
663 vchiq_log_info(vchiq_arm_log_level,
664 "found bulk_waiter %x for pid %d",
665 (unsigned int)waiter, current->l_proc->p_pid);
666 pargs->userdata = &waiter->bulk_waiter;
667 }
668 status = vchiq_bulk_transfer
669 (pargs->handle,
670 VCHI_MEM_HANDLE_INVALID,
671 pargs->data, pargs->size,
672 pargs->userdata, pargs->mode,
673 dir);
674 if (!waiter)
675 break;
676 if ((status != VCHIQ_RETRY) || fatal_signal_pending(current) ||
677 !waiter->bulk_waiter.bulk) {
678 if (waiter->bulk_waiter.bulk) {
679 /* Cancel the signal when the transfer
680 ** completes. */
681 spin_lock(&bulk_waiter_spinlock);
682 waiter->bulk_waiter.bulk->userdata = NULL;
683 spin_unlock(&bulk_waiter_spinlock);
684 }
685 _sema_destroy(&waiter->bulk_waiter.event);
686 kfree(waiter);
687 } else {
688 const VCHIQ_BULK_MODE_T mode_waiting =
689 VCHIQ_BULK_MODE_WAITING;
690 waiter->l = current;
691 lmutex_lock(&instance->bulk_waiter_list_mutex);
692 list_add(&waiter->list, &instance->bulk_waiter_list);
693 lmutex_unlock(&instance->bulk_waiter_list_mutex);
694 vchiq_log_info(vchiq_arm_log_level,
695 "saved bulk_waiter %x for pid %d",
696 (unsigned int)waiter, current->l_proc->p_pid);
697
698 pargs->mode = mode_waiting;
699 }
700 } break;
701
702 case VCHIQ_IOC_AWAIT_COMPLETION: {
703 VCHIQ_AWAIT_COMPLETION_T *pargs = arg;
704
705 DEBUG_TRACE(AWAIT_COMPLETION_LINE);
706 if (!instance->connected) {
707 ret = -ENOTCONN;
708 break;
709 }
710
711 lmutex_lock(&instance->completion_mutex);
712
713 DEBUG_TRACE(AWAIT_COMPLETION_LINE);
714 while ((instance->completion_remove ==
715 instance->completion_insert)
716 && !instance->closing) {
717 DEBUG_TRACE(AWAIT_COMPLETION_LINE);
718 lmutex_unlock(&instance->completion_mutex);
719 rc = down_interruptible(&instance->insert_event);
720 lmutex_lock(&instance->completion_mutex);
721 if (rc != 0) {
722 DEBUG_TRACE(AWAIT_COMPLETION_LINE);
723 vchiq_log_info(vchiq_arm_log_level,
724 "AWAIT_COMPLETION interrupted");
725 ret = -EINTR;
726 break;
727 }
728 }
729 DEBUG_TRACE(AWAIT_COMPLETION_LINE);
730
731 /* A read memory barrier is needed to stop prefetch of a stale
732 ** completion record
733 */
734 rmb();
735
736 if (ret == 0) {
737 int msgbufcount = pargs->msgbufcount;
738 int count = 0;
739 for (count = 0; count < pargs->count; count++) {
740 VCHIQ_COMPLETION_DATA_T *completion;
741 VCHIQ_SERVICE_T *service1;
742 USER_SERVICE_T *user_service;
743 VCHIQ_HEADER_T *header;
744 if (instance->completion_remove ==
745 instance->completion_insert)
746 break;
747 completion = &instance->completions[
748 instance->completion_remove &
749 (MAX_COMPLETIONS - 1)];
750
751 service1 = completion->service_userdata;
752 user_service = service1->base.userdata;
753 completion->service_userdata =
754 user_service->userdata;
755
756 header = completion->header;
757 if (header) {
758 void __user *msgbuf;
759 int msglen;
760
761 msglen = header->size +
762 sizeof(VCHIQ_HEADER_T);
763 /* This must be a VCHIQ-style service */
764 if (pargs->msgbufsize < msglen) {
765 vchiq_log_error(
766 vchiq_arm_log_level,
767 "header %x: msgbufsize"
768 " %x < msglen %x",
769 (unsigned int)header,
770 pargs->msgbufsize,
771 msglen);
772 WARN(1, "invalid message "
773 "size\n");
774 if (count == 0)
775 ret = -EMSGSIZE;
776 break;
777 }
778 if (msgbufcount <= 0)
779 /* Stall here for lack of a
780 ** buffer for the message. */
781 break;
782 /* Get the pointer from user space */
783 msgbufcount--;
784 if (copy_from_user(&msgbuf,
785 (const void __user *)
786 &pargs->msgbufs[msgbufcount],
787 sizeof(msgbuf)) != 0) {
788 if (count == 0)
789 ret = -EFAULT;
790 break;
791 }
792
793 /* Copy the message to user space */
794 if (copy_to_user(msgbuf, header,
795 msglen) != 0) {
796 if (count == 0)
797 ret = -EFAULT;
798 break;
799 }
800
801 /* Now it has been copied, the message
802 ** can be released. */
803 vchiq_release_message(service1->handle,
804 header);
805
806 /* The completion must point to the
807 ** msgbuf. */
808 completion->header = msgbuf;
809 }
810
811 if (completion->reason ==
812 VCHIQ_SERVICE_CLOSED) {
813 unlock_service(service1);
814 _sema_destroy(&user_service->insert_event);
815 _sema_destroy(&user_service->remove_event);
816 kfree(user_service);
817 }
818
819 if (copy_to_user((void __user *)(
820 (size_t)pargs->buf +
821 count * sizeof(VCHIQ_COMPLETION_DATA_T)),
822 completion,
823 sizeof(VCHIQ_COMPLETION_DATA_T)) != 0) {
824 if (ret == 0)
825 ret = -EFAULT;
826 break;
827 }
828
829 instance->completion_remove++;
830 }
831
832 pargs->msgbufcount = msgbufcount;
833 pargs->count = count;
834 }
835
836 if (ret != 0)
837 up(&instance->remove_event);
838 lmutex_unlock(&instance->completion_mutex);
839 DEBUG_TRACE(AWAIT_COMPLETION_LINE);
840 } break;
841
842 case VCHIQ_IOC_DEQUEUE_MESSAGE: {
843 VCHIQ_DEQUEUE_MESSAGE_T *pargs = arg;
844 USER_SERVICE_T *user_service;
845 VCHIQ_HEADER_T *header;
846
847 DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
848 service = find_service_for_instance(instance, pargs->handle);
849 if (!service) {
850 ret = -EINVAL;
851 break;
852 }
853 user_service = (USER_SERVICE_T *)service->base.userdata;
854 if (user_service->is_vchi == 0) {
855 ret = -EINVAL;
856 break;
857 }
858
859 spin_lock(&msg_queue_spinlock);
860 if (user_service->msg_remove == user_service->msg_insert) {
861 if (!pargs->blocking) {
862 spin_unlock(&msg_queue_spinlock);
863 DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
864 ret = -EWOULDBLOCK;
865 break;
866 }
867 user_service->dequeue_pending = 1;
868 do {
869 spin_unlock(&msg_queue_spinlock);
870 DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
871 if (down_interruptible(
872 &user_service->insert_event) != 0) {
873 vchiq_log_info(vchiq_arm_log_level,
874 "DEQUEUE_MESSAGE interrupted");
875 ret = -EINTR;
876 break;
877 }
878 spin_lock(&msg_queue_spinlock);
879 } while (user_service->msg_remove ==
880 user_service->msg_insert);
881
882 if (ret)
883 break;
884 }
885
886 BUG_ON((int)(user_service->msg_insert -
887 user_service->msg_remove) < 0);
888
889 header = user_service->msg_queue[user_service->msg_remove &
890 (MSG_QUEUE_SIZE - 1)];
891 user_service->msg_remove++;
892 spin_unlock(&msg_queue_spinlock);
893
894 up(&user_service->remove_event);
895 if (header == NULL)
896 ret = -ENOTCONN;
897 else if (header->size <= pargs->bufsize) {
898 /* Copy to user space if msgbuf is not NULL */
899 if ((pargs->buf == NULL) ||
900 (copy_to_user((void __user *)pargs->buf,
901 header->data,
902 header->size) == 0)) {
903 pargs->bufsize = header->size;
904 vchiq_release_message(
905 service->handle,
906 header);
907 } else
908 ret = -EFAULT;
909 } else {
910 vchiq_log_error(vchiq_arm_log_level,
911 "header %x: bufsize %x < size %x",
912 (unsigned int)header, pargs->bufsize,
913 header->size);
914 WARN(1, "invalid size\n");
915 ret = -EMSGSIZE;
916 }
917 DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
918 } break;
919
920 case VCHIQ_IOC_GET_CLIENT_ID: {
921 VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
922
923 ret = vchiq_get_client_id(handle);
924 } break;
925
926 case VCHIQ_IOC_GET_CONFIG: {
927 VCHIQ_GET_CONFIG_T *pargs = arg;
928 VCHIQ_CONFIG_T config;
929
930 if (pargs->config_size > sizeof(config)) {
931 ret = -EINVAL;
932 break;
933 }
934 status = vchiq_get_config(instance, pargs->config_size, &config);
935 if (status == VCHIQ_SUCCESS) {
936 if (copy_to_user((void __user *)pargs->pconfig,
937 &config, pargs->config_size) != 0) {
938 ret = -EFAULT;
939 break;
940 }
941 }
942 } break;
943
944 case VCHIQ_IOC_SET_SERVICE_OPTION: {
945 VCHIQ_SET_SERVICE_OPTION_T *pargs = arg;
946
947 service = find_service_for_instance(instance, pargs->handle);
948 if (!service) {
949 ret = -EINVAL;
950 break;
951 }
952
953 status = vchiq_set_service_option(
954 pargs->handle, pargs->option, pargs->value);
955 } break;
956
957 case VCHIQ_IOC_DUMP_PHYS_MEM: {
958 #if 0
959 VCHIQ_DUMP_MEM_T *pargs = arg;
960 #endif
961
962 printf("IMPLEMENT ME: %s:%d\n", __FILE__, __LINE__);
963 #if 0
964 dump_phys_mem(pargs->virt_addr, pargs->num_bytes);
965 #endif
966 } break;
967
968 default:
969 ret = -ENOTTY;
970 break;
971 }
972
973 if (service)
974 unlock_service(service);
975
976 if (ret == 0) {
977 if (status == VCHIQ_ERROR)
978 ret = -EIO;
979 else if (status == VCHIQ_RETRY)
980 ret = -EINTR;
981 }
982
983 if ((status == VCHIQ_SUCCESS) && (ret < 0) && (ret != -EINTR) &&
984 (ret != -EWOULDBLOCK))
985 vchiq_log_info(vchiq_arm_log_level,
986 " ioctl instance %lx, cmd %s -> status %d, %d",
987 (unsigned long)instance,
988 (_IOC_NR(cmd) <= VCHIQ_IOC_MAX) ?
989 ioctl_names[_IOC_NR(cmd)] :
990 "<invalid>",
991 status, ret);
992 else
993 vchiq_log_trace(vchiq_arm_log_level,
994 " ioctl instance %lx, cmd %s -> status %d, %d",
995 (unsigned long)instance,
996 (_IOC_NR(cmd) <= VCHIQ_IOC_MAX) ?
997 ioctl_names[_IOC_NR(cmd)] :
998 "<invalid>",
999 status, ret);
1000
1001 /* XXXBSD: report BSD-style error to userland */
1002 if (ret < 0)
1003 ret = -ret;
1004
1005 return ret;
1006 }
1007
1008 #if notyet
1009 static void
1010 instance_dtr(void *data)
1011 {
1012
1013 free(data, M_VCHIQ);
1014 }
1015 #endif
1016
1017 /****************************************************************************
1018 *
1019 * vchiq_open
1020 *
1021 ***************************************************************************/
1022
1023 static int
1024 vchiq_open(dev_t dev, int flags, int mode, lwp_t *l)
1025 {
1026 VCHIQ_INSTANCE_T instance = NULL;
1027 struct file *fp;
1028 int err, fd;
1029
1030 vchiq_log_info(vchiq_arm_log_level, "vchiq_open");
1031
1032 /* XXXBSD: do we really need this check? */
1033 if (device_lookup_private(&vchiq_cd, minor(dev)) != NULL) {
1034 VCHIQ_STATE_T *state = vchiq_get_state();
1035
1036 if (!state) {
1037 vchiq_log_error(vchiq_arm_log_level,
1038 "vchiq has no connection to VideoCore");
1039 return -ENOTCONN;
1040 }
1041
1042 instance = kmalloc(sizeof(*instance), GFP_KERNEL);
1043 if (!instance)
1044 return -ENOMEM;
1045
1046 err = fd_allocfile(&fp, &fd);
1047 if (err) {
1048 kfree(instance);
1049 return -err;
1050 }
1051
1052 instance->state = state;
1053 instance->l = l;
1054
1055 #ifdef notyet
1056 ret = vchiq_proc_add_instance(instance);
1057 if (ret != 0) {
1058 kfree(instance);
1059 return ret;
1060 }
1061 #endif
1062
1063 _sema_init(&instance->insert_event, 0);
1064 _sema_init(&instance->remove_event, 0);
1065 lmutex_init(&instance->completion_mutex);
1066 lmutex_init(&instance->bulk_waiter_list_mutex);
1067 INIT_LIST_HEAD(&instance->bulk_waiter_list);
1068
1069 }
1070 else {
1071 vchiq_log_error(vchiq_arm_log_level,
1072 "Unknown minor device");
1073 return -ENXIO;
1074 }
1075
1076 return fd_clone(fp, fd, flags, &vchiq_fileops, instance);
1077 }
1078
1079 /****************************************************************************
1080 *
1081 * vchiq_release
1082 *
1083 ***************************************************************************/
1084
1085 static int
1086 vchiq_close(struct file *fp)
1087 {
1088 int ret = 0;
1089 if (1) {
1090 VCHIQ_INSTANCE_T instance;
1091 VCHIQ_STATE_T *state = vchiq_get_state();
1092 VCHIQ_SERVICE_T *service;
1093 int i;
1094
1095 instance = fp->f_data;
1096
1097 vchiq_log_info(vchiq_arm_log_level,
1098 "vchiq_release: instance=%lx",
1099 (unsigned long)instance);
1100
1101 if (!state) {
1102 ret = -EPERM;
1103 goto out;
1104 }
1105
1106 /* Ensure videocore is awake to allow termination. */
1107 vchiq_use_internal(instance->state, NULL,
1108 USE_TYPE_VCHIQ);
1109
1110 lmutex_lock(&instance->completion_mutex);
1111
1112 /* Wake the completion thread and ask it to exit */
1113 instance->closing = 1;
1114 up(&instance->insert_event);
1115
1116 lmutex_unlock(&instance->completion_mutex);
1117
1118 /* Wake the slot handler if the completion queue is full. */
1119 up(&instance->remove_event);
1120
1121 /* Mark all services for termination... */
1122 i = 0;
1123 while ((service = next_service_by_instance(state, instance,
1124 &i)) != NULL) {
1125 USER_SERVICE_T *user_service = service->base.userdata;
1126
1127 /* Wake the slot handler if the msg queue is full. */
1128 up(&user_service->remove_event);
1129
1130 vchiq_terminate_service_internal(service);
1131 unlock_service(service);
1132 }
1133
1134 /* ...and wait for them to die */
1135 i = 0;
1136 while ((service = next_service_by_instance(state, instance, &i))
1137 != NULL) {
1138 USER_SERVICE_T *user_service = service->base.userdata;
1139
1140 down(&service->remove_event);
1141
1142 BUG_ON(service->srvstate != VCHIQ_SRVSTATE_FREE);
1143
1144 spin_lock(&msg_queue_spinlock);
1145
1146 while (user_service->msg_remove !=
1147 user_service->msg_insert) {
1148 VCHIQ_HEADER_T *header = user_service->
1149 msg_queue[user_service->msg_remove &
1150 (MSG_QUEUE_SIZE - 1)];
1151 user_service->msg_remove++;
1152 spin_unlock(&msg_queue_spinlock);
1153
1154 if (header)
1155 vchiq_release_message(
1156 service->handle,
1157 header);
1158 spin_lock(&msg_queue_spinlock);
1159 }
1160
1161 spin_unlock(&msg_queue_spinlock);
1162
1163 unlock_service(service);
1164 _sema_destroy(&user_service->insert_event);
1165 _sema_destroy(&user_service->remove_event);
1166 kfree(user_service);
1167 }
1168
1169 /* Release any closed services */
1170 while (instance->completion_remove !=
1171 instance->completion_insert) {
1172 VCHIQ_COMPLETION_DATA_T *completion;
1173 VCHIQ_SERVICE_T *service1;
1174 completion = &instance->completions[
1175 instance->completion_remove &
1176 (MAX_COMPLETIONS - 1)];
1177 service1 = completion->service_userdata;
1178 if (completion->reason == VCHIQ_SERVICE_CLOSED)
1179 unlock_service(service1);
1180 instance->completion_remove++;
1181 }
1182
1183 /* Release the PEER service count. */
1184 vchiq_release_internal(instance->state, NULL);
1185
1186 {
1187 struct list_head *pos, *next;
1188 list_for_each_safe(pos, next,
1189 &instance->bulk_waiter_list) {
1190 struct bulk_waiter_node *waiter;
1191 waiter = list_entry(pos,
1192 struct bulk_waiter_node,
1193 list);
1194 list_del(pos);
1195 vchiq_log_info(vchiq_arm_log_level,
1196 "bulk_waiter - cleaned up %x "
1197 "for lwp %p",
1198 (unsigned int)waiter, waiter->l);
1199 _sema_destroy(&waiter->bulk_waiter.event);
1200 kfree(waiter);
1201 }
1202 }
1203
1204 }
1205 else {
1206 vchiq_log_error(vchiq_arm_log_level,
1207 "Unknown minor device");
1208 ret = -ENXIO;
1209 }
1210
1211 out:
1212 return ret;
1213 }
1214
1215 /****************************************************************************
1216 *
1217 * vchiq_dump
1218 *
1219 ***************************************************************************/
1220
1221 void
1222 vchiq_dump(void *dump_context, const char *str, int len)
1223 {
1224 DUMP_CONTEXT_T *context = (DUMP_CONTEXT_T *)dump_context;
1225
1226 if (context->actual < context->space) {
1227 int copy_bytes;
1228 if (context->offset > 0) {
1229 int skip_bytes = min(len, (int)context->offset);
1230 str += skip_bytes;
1231 len -= skip_bytes;
1232 context->offset -= skip_bytes;
1233 if (context->offset > 0)
1234 return;
1235 }
1236 copy_bytes = min(len, (int)(context->space - context->actual));
1237 if (copy_bytes == 0)
1238 return;
1239 if (copy_to_user(context->buf + context->actual, str,
1240 copy_bytes))
1241 context->actual = -EFAULT;
1242 context->actual += copy_bytes;
1243 len -= copy_bytes;
1244
1245 /* If tne terminating NUL is included in the length, then it
1246 ** marks the end of a line and should be replaced with a
1247 ** carriage return. */
1248 if ((len == 0) && (str[copy_bytes - 1] == '\0')) {
1249 char cr = '\n';
1250 if (copy_to_user(context->buf + context->actual - 1,
1251 &cr, 1))
1252 context->actual = -EFAULT;
1253 }
1254 }
1255 }
1256
1257 /****************************************************************************
1258 *
1259 * vchiq_dump_platform_instance_state
1260 *
1261 ***************************************************************************/
1262
1263 void
1264 vchiq_dump_platform_instances(void *dump_context)
1265 {
1266 VCHIQ_STATE_T *state = vchiq_get_state();
1267 char buf[80];
1268 int len;
1269 int i;
1270
1271 /* There is no list of instances, so instead scan all services,
1272 marking those that have been dumped. */
1273
1274 for (i = 0; i < state->unused_service; i++) {
1275 VCHIQ_SERVICE_T *service = state->services[i];
1276 VCHIQ_INSTANCE_T instance;
1277
1278 if (service && (service->base.callback == service_callback)) {
1279 instance = service->instance;
1280 if (instance)
1281 instance->mark = 0;
1282 }
1283 }
1284
1285 for (i = 0; i < state->unused_service; i++) {
1286 VCHIQ_SERVICE_T *service = state->services[i];
1287 VCHIQ_INSTANCE_T instance;
1288
1289 if (service && (service->base.callback == service_callback)) {
1290 instance = service->instance;
1291 if (instance && !instance->mark) {
1292 len = snprintf(buf, sizeof(buf),
1293 "Instance %x: lwp %p,%s completions "
1294 "%d/%d",
1295 (unsigned int)instance, instance->l,
1296 instance->connected ? " connected, " :
1297 "",
1298 instance->completion_insert -
1299 instance->completion_remove,
1300 MAX_COMPLETIONS);
1301
1302 vchiq_dump(dump_context, buf, len + 1);
1303
1304 instance->mark = 1;
1305 }
1306 }
1307 }
1308 }
1309
1310 /****************************************************************************
1311 *
1312 * vchiq_dump_platform_service_state
1313 *
1314 ***************************************************************************/
1315
1316 void
1317 vchiq_dump_platform_service_state(void *dump_context, VCHIQ_SERVICE_T *service)
1318 {
1319 USER_SERVICE_T *user_service = (USER_SERVICE_T *)service->base.userdata;
1320 char buf[80];
1321 int len;
1322
1323 len = snprintf(buf, sizeof(buf), " instance %x",
1324 (unsigned int)service->instance);
1325
1326 if ((service->base.callback == service_callback) &&
1327 user_service->is_vchi) {
1328 len += snprintf(buf + len, sizeof(buf) - len,
1329 ", %d/%d messages",
1330 user_service->msg_insert - user_service->msg_remove,
1331 MSG_QUEUE_SIZE);
1332
1333 if (user_service->dequeue_pending)
1334 len += snprintf(buf + len, sizeof(buf) - len,
1335 " (dequeue pending)");
1336 }
1337
1338 vchiq_dump(dump_context, buf, len + 1);
1339 }
1340
1341 #ifdef notyet
1342 /****************************************************************************
1343 *
1344 * dump_user_mem
1345 *
1346 ***************************************************************************/
1347
1348 static void
1349 dump_phys_mem(void *virt_addr, uint32_t num_bytes)
1350 {
1351 int rc;
1352 uint8_t *end_virt_addr = virt_addr + num_bytes;
1353 int num_pages;
1354 int offset;
1355 int end_offset;
1356 int page_idx;
1357 int prev_idx;
1358 struct page *page;
1359 struct page **pages;
1360 uint8_t *kmapped_virt_ptr;
1361
1362 /* Align virtAddr and endVirtAddr to 16 byte boundaries. */
1363
1364 virt_addr = (void *)((unsigned long)virt_addr & ~0x0fuL);
1365 end_virt_addr = (void *)(((unsigned long)end_virt_addr + 15uL) &
1366 ~0x0fuL);
1367
1368 offset = (int)(long)virt_addr & (PAGE_SIZE - 1);
1369 end_offset = (int)(long)end_virt_addr & (PAGE_SIZE - 1);
1370
1371 num_pages = (offset + num_bytes + PAGE_SIZE - 1) / PAGE_SIZE;
1372
1373 pages = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
1374 if (pages == NULL) {
1375 vchiq_log_error(vchiq_arm_log_level,
1376 "Unable to allocation memory for %d pages\n",
1377 num_pages);
1378 return;
1379 }
1380
1381 down_read(¤t->mm->mmap_sem);
1382 rc = get_user_pages(current, /* task */
1383 current->mm, /* mm */
1384 (unsigned long)virt_addr, /* start */
1385 num_pages, /* len */
1386 0, /* write */
1387 0, /* force */
1388 pages, /* pages (array of page pointers) */
1389 NULL); /* vmas */
1390 up_read(¤t->mm->mmap_sem);
1391
1392 prev_idx = -1;
1393 page = NULL;
1394
1395 while (offset < end_offset) {
1396
1397 int page_offset = offset % PAGE_SIZE;
1398 page_idx = offset / PAGE_SIZE;
1399
1400 if (page_idx != prev_idx) {
1401
1402 if (page != NULL)
1403 kunmap(page);
1404 page = pages[page_idx];
1405 kmapped_virt_ptr = kmap(page);
1406
1407 prev_idx = page_idx;
1408 }
1409
1410 if (vchiq_arm_log_level >= VCHIQ_LOG_TRACE)
1411 vchiq_log_dump_mem("ph",
1412 (uint32_t)(unsigned long)&kmapped_virt_ptr[
1413 page_offset],
1414 &kmapped_virt_ptr[page_offset], 16);
1415
1416 offset += 16;
1417 }
1418 if (page != NULL)
1419 kunmap(page);
1420
1421 for (page_idx = 0; page_idx < num_pages; page_idx++)
1422 page_cache_release(pages[page_idx]);
1423
1424 kfree(pages);
1425 }
1426
1427 /****************************************************************************
1428 *
1429 * vchiq_read
1430 *
1431 ***************************************************************************/
1432
1433 static ssize_t
1434 vchiq_read(struct file *file, char __user *buf,
1435 size_t count, loff_t *ppos)
1436 {
1437 DUMP_CONTEXT_T context;
1438 context.buf = buf;
1439 context.actual = 0;
1440 context.space = count;
1441 context.offset = *ppos;
1442
1443 vchiq_dump_state(&context, &g_state);
1444
1445 *ppos += context.actual;
1446
1447 return context.actual;
1448 }
1449 #endif
1450
1451 VCHIQ_STATE_T *
1452 vchiq_get_state(void)
1453 {
1454
1455 if (g_state.remote == NULL)
1456 printk(KERN_ERR "%s: g_state.remote == NULL\n", __func__);
1457 else if (g_state.remote->initialised != 1)
1458 printk(KERN_NOTICE "%s: g_state.remote->initialised != 1 (%d)\n",
1459 __func__, g_state.remote->initialised);
1460
1461 return ((g_state.remote != NULL) &&
1462 (g_state.remote->initialised == 1)) ? &g_state : NULL;
1463 }
1464
1465 /*
1466 * Autosuspend related functionality
1467 */
1468
1469 int
1470 vchiq_videocore_wanted(VCHIQ_STATE_T *state)
1471 {
1472 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
1473 if (!arm_state)
1474 /* autosuspend not supported - always return wanted */
1475 return 1;
1476 else if (arm_state->blocked_count)
1477 return 1;
1478 else if (!arm_state->videocore_use_count)
1479 /* usage count zero - check for override unless we're forcing */
1480 if (arm_state->resume_blocked)
1481 return 0;
1482 else
1483 return vchiq_platform_videocore_wanted(state);
1484 else
1485 /* non-zero usage count - videocore still required */
1486 return 1;
1487 }
1488
1489 static VCHIQ_STATUS_T
1490 vchiq_keepalive_vchiq_callback(VCHIQ_REASON_T reason,
1491 VCHIQ_HEADER_T *header,
1492 VCHIQ_SERVICE_HANDLE_T service_user,
1493 void *bulk_user)
1494 {
1495 vchiq_log_error(vchiq_susp_log_level,
1496 "%s callback reason %d", __func__, reason);
1497 return 0;
1498 }
1499
1500 static int
1501 vchiq_keepalive_thread_func(void *v)
1502 {
1503 VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
1504 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
1505
1506 VCHIQ_STATUS_T status;
1507 VCHIQ_INSTANCE_T instance;
1508 VCHIQ_SERVICE_HANDLE_T ka_handle;
1509
1510 VCHIQ_SERVICE_PARAMS_T params = {
1511 .fourcc = VCHIQ_MAKE_FOURCC('K', 'E', 'E', 'P'),
1512 .callback = vchiq_keepalive_vchiq_callback,
1513 .version = KEEPALIVE_VER,
1514 .version_min = KEEPALIVE_VER_MIN
1515 };
1516
1517 status = vchiq_initialise(&instance);
1518 if (status != VCHIQ_SUCCESS) {
1519 vchiq_log_error(vchiq_susp_log_level,
1520 "%s vchiq_initialise failed %d", __func__, status);
1521 goto exit;
1522 }
1523
1524 status = vchiq_connect(instance);
1525 if (status != VCHIQ_SUCCESS) {
1526 vchiq_log_error(vchiq_susp_log_level,
1527 "%s vchiq_connect failed %d", __func__, status);
1528 goto shutdown;
1529 }
1530
1531 status = vchiq_add_service(instance, ¶ms, &ka_handle);
1532 if (status != VCHIQ_SUCCESS) {
1533 vchiq_log_error(vchiq_susp_log_level,
1534 "%s vchiq_open_service failed %d", __func__, status);
1535 goto shutdown;
1536 }
1537
1538 while (1) {
1539 long rc = 0, uc = 0;
1540 if (wait_for_completion_interruptible(&arm_state->ka_evt)
1541 != 0) {
1542 vchiq_log_error(vchiq_susp_log_level,
1543 "%s interrupted", __func__);
1544 flush_signals(current);
1545 continue;
1546 }
1547
1548 /* read and clear counters. Do release_count then use_count to
1549 * prevent getting more releases than uses */
1550 rc = atomic_xchg(&arm_state->ka_release_count, 0);
1551 uc = atomic_xchg(&arm_state->ka_use_count, 0);
1552
1553 /* Call use/release service the requisite number of times.
1554 * Process use before release so use counts don't go negative */
1555 while (uc--) {
1556 atomic_inc(&arm_state->ka_use_ack_count);
1557 status = vchiq_use_service(ka_handle);
1558 if (status != VCHIQ_SUCCESS) {
1559 vchiq_log_error(vchiq_susp_log_level,
1560 "%s vchiq_use_service error %d",
1561 __func__, status);
1562 }
1563 }
1564 while (rc--) {
1565 status = vchiq_release_service(ka_handle);
1566 if (status != VCHIQ_SUCCESS) {
1567 vchiq_log_error(vchiq_susp_log_level,
1568 "%s vchiq_release_service error %d",
1569 __func__, status);
1570 }
1571 }
1572 }
1573
1574 shutdown:
1575 vchiq_shutdown(instance);
1576 exit:
1577 return 0;
1578 }
1579
1580 VCHIQ_STATUS_T
1581 vchiq_arm_init_state(VCHIQ_STATE_T *state, VCHIQ_ARM_STATE_T *arm_state)
1582 {
1583 VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
1584
1585 if (arm_state) {
1586 rwlock_init(&arm_state->susp_res_lock);
1587
1588 init_completion(&arm_state->ka_evt);
1589 atomic_set(&arm_state->ka_use_count, 0);
1590 atomic_set(&arm_state->ka_use_ack_count, 0);
1591 atomic_set(&arm_state->ka_release_count, 0);
1592
1593 init_completion(&arm_state->vc_suspend_complete);
1594
1595 init_completion(&arm_state->vc_resume_complete);
1596 /* Initialise to 'done' state. We only want to block on resume
1597 * completion while videocore is suspended. */
1598 set_resume_state(arm_state, VC_RESUME_RESUMED);
1599
1600 init_completion(&arm_state->resume_blocker);
1601 /* Initialise to 'done' state. We only want to block on this
1602 * completion while resume is blocked */
1603 complete_all(&arm_state->resume_blocker);
1604
1605 init_completion(&arm_state->blocked_blocker);
1606 /* Initialise to 'done' state. We only want to block on this
1607 * completion while things are waiting on the resume blocker */
1608 complete_all(&arm_state->blocked_blocker);
1609
1610 arm_state->suspend_timer_timeout = SUSPEND_TIMER_TIMEOUT_MS;
1611 arm_state->suspend_timer_running = 0;
1612 init_timer(&arm_state->suspend_timer);
1613 arm_state->suspend_timer.data = (unsigned long)(state);
1614 arm_state->suspend_timer.function = suspend_timer_callback;
1615
1616 arm_state->first_connect = 0;
1617
1618 }
1619 return status;
1620 }
1621
1622 /*
1623 ** Functions to modify the state variables;
1624 ** set_suspend_state
1625 ** set_resume_state
1626 **
1627 ** There are more state variables than we might like, so ensure they remain in
1628 ** step. Suspend and resume state are maintained separately, since most of
1629 ** these state machines can operate independently. However, there are a few
1630 ** states where state transitions in one state machine cause a reset to the
1631 ** other state machine. In addition, there are some completion events which
1632 ** need to occur on state machine reset and end-state(s), so these are also
1633 ** dealt with in these functions.
1634 **
1635 ** In all states we set the state variable according to the input, but in some
1636 ** cases we perform additional steps outlined below;
1637 **
1638 ** VC_SUSPEND_IDLE - Initialise the suspend completion at the same time.
1639 ** The suspend completion is completed after any suspend
1640 ** attempt. When we reset the state machine we also reset
1641 ** the completion. This reset occurs when videocore is
1642 ** resumed, and also if we initiate suspend after a suspend
1643 ** failure.
1644 **
1645 ** VC_SUSPEND_IN_PROGRESS - This state is considered the point of no return for
1646 ** suspend - ie from this point on we must try to suspend
1647 ** before resuming can occur. We therefore also reset the
1648 ** resume state machine to VC_RESUME_IDLE in this state.
1649 **
1650 ** VC_SUSPEND_SUSPENDED - Suspend has completed successfully. Also call
1651 ** complete_all on the suspend completion to notify
1652 ** anything waiting for suspend to happen.
1653 **
1654 ** VC_SUSPEND_REJECTED - Videocore rejected suspend. Videocore will also
1655 ** initiate resume, so no need to alter resume state.
1656 ** We call complete_all on the suspend completion to notify
1657 ** of suspend rejection.
1658 **
1659 ** VC_SUSPEND_FAILED - We failed to initiate videocore suspend. We notify the
1660 ** suspend completion and reset the resume state machine.
1661 **
1662 ** VC_RESUME_IDLE - Initialise the resume completion at the same time. The
1663 ** resume completion is in it's 'done' state whenever
1664 ** videcore is running. Therfore, the VC_RESUME_IDLE state
1665 ** implies that videocore is suspended.
1666 ** Hence, any thread which needs to wait until videocore is
1667 ** running can wait on this completion - it will only block
1668 ** if videocore is suspended.
1669 **
1670 ** VC_RESUME_RESUMED - Resume has completed successfully. Videocore is running.
1671 ** Call complete_all on the resume completion to unblock
1672 ** any threads waiting for resume. Also reset the suspend
1673 ** state machine to it's idle state.
1674 **
1675 ** VC_RESUME_FAILED - Currently unused - no mechanism to fail resume exists.
1676 */
1677
1678 inline void
1679 set_suspend_state(VCHIQ_ARM_STATE_T *arm_state,
1680 enum vc_suspend_status new_state)
1681 {
1682 /* set the state in all cases */
1683 arm_state->vc_suspend_state = new_state;
1684
1685 /* state specific additional actions */
1686 switch (new_state) {
1687 case VC_SUSPEND_FORCE_CANCELED:
1688 complete_all(&arm_state->vc_suspend_complete);
1689 break;
1690 case VC_SUSPEND_REJECTED:
1691 complete_all(&arm_state->vc_suspend_complete);
1692 break;
1693 case VC_SUSPEND_FAILED:
1694 complete_all(&arm_state->vc_suspend_complete);
1695 arm_state->vc_resume_state = VC_RESUME_RESUMED;
1696 complete_all(&arm_state->vc_resume_complete);
1697 break;
1698 case VC_SUSPEND_IDLE:
1699 INIT_COMPLETION(arm_state->vc_suspend_complete);
1700 break;
1701 case VC_SUSPEND_REQUESTED:
1702 break;
1703 case VC_SUSPEND_IN_PROGRESS:
1704 set_resume_state(arm_state, VC_RESUME_IDLE);
1705 break;
1706 case VC_SUSPEND_SUSPENDED:
1707 complete_all(&arm_state->vc_suspend_complete);
1708 break;
1709 default:
1710 BUG();
1711 break;
1712 }
1713 }
1714
1715 inline void
1716 set_resume_state(VCHIQ_ARM_STATE_T *arm_state,
1717 enum vc_resume_status new_state)
1718 {
1719 /* set the state in all cases */
1720 arm_state->vc_resume_state = new_state;
1721
1722 /* state specific additional actions */
1723 switch (new_state) {
1724 case VC_RESUME_FAILED:
1725 break;
1726 case VC_RESUME_IDLE:
1727 INIT_COMPLETION(arm_state->vc_resume_complete);
1728 break;
1729 case VC_RESUME_REQUESTED:
1730 break;
1731 case VC_RESUME_IN_PROGRESS:
1732 break;
1733 case VC_RESUME_RESUMED:
1734 complete_all(&arm_state->vc_resume_complete);
1735 set_suspend_state(arm_state, VC_SUSPEND_IDLE);
1736 break;
1737 default:
1738 BUG();
1739 break;
1740 }
1741 }
1742
1743
1744 /* should be called with the write lock held */
1745 inline void
1746 start_suspend_timer(VCHIQ_ARM_STATE_T *arm_state)
1747 {
1748 del_timer(&arm_state->suspend_timer);
1749 arm_state->suspend_timer.expires = jiffies +
1750 msecs_to_jiffies(arm_state->
1751 suspend_timer_timeout);
1752 add_timer(&arm_state->suspend_timer);
1753 arm_state->suspend_timer_running = 1;
1754 }
1755
1756 /* should be called with the write lock held */
1757 static inline void
1758 stop_suspend_timer(VCHIQ_ARM_STATE_T *arm_state)
1759 {
1760 if (arm_state->suspend_timer_running) {
1761 del_timer(&arm_state->suspend_timer);
1762 arm_state->suspend_timer_running = 0;
1763 }
1764 }
1765
1766 static inline int
1767 need_resume(VCHIQ_STATE_T *state)
1768 {
1769 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
1770 return (arm_state->vc_suspend_state > VC_SUSPEND_IDLE) &&
1771 (arm_state->vc_resume_state < VC_RESUME_REQUESTED) &&
1772 vchiq_videocore_wanted(state);
1773 }
1774
1775 static int
1776 block_resume(VCHIQ_ARM_STATE_T *arm_state)
1777 {
1778 int status = VCHIQ_SUCCESS;
1779 const unsigned long timeout_val =
1780 msecs_to_jiffies(FORCE_SUSPEND_TIMEOUT_MS);
1781 int resume_count = 0;
1782
1783 /* Allow any threads which were blocked by the last force suspend to
1784 * complete if they haven't already. Only give this one shot; if
1785 * blocked_count is incremented after blocked_blocker is completed
1786 * (which only happens when blocked_count hits 0) then those threads
1787 * will have to wait until next time around */
1788 if (arm_state->blocked_count) {
1789 INIT_COMPLETION(arm_state->blocked_blocker);
1790 write_unlock_bh(&arm_state->susp_res_lock);
1791 vchiq_log_info(vchiq_susp_log_level, "%s wait for previously "
1792 "blocked clients", __func__);
1793 if (wait_for_completion_interruptible_timeout(
1794 &arm_state->blocked_blocker, timeout_val)
1795 <= 0) {
1796 vchiq_log_error(vchiq_susp_log_level, "%s wait for "
1797 "previously blocked clients failed" , __func__);
1798 status = VCHIQ_ERROR;
1799 write_lock_bh(&arm_state->susp_res_lock);
1800 goto out;
1801 }
1802 vchiq_log_info(vchiq_susp_log_level, "%s previously blocked "
1803 "clients resumed", __func__);
1804 write_lock_bh(&arm_state->susp_res_lock);
1805 }
1806
1807 /* We need to wait for resume to complete if it's in process */
1808 while (arm_state->vc_resume_state != VC_RESUME_RESUMED &&
1809 arm_state->vc_resume_state > VC_RESUME_IDLE) {
1810 if (resume_count > 1) {
1811 status = VCHIQ_ERROR;
1812 vchiq_log_error(vchiq_susp_log_level, "%s waited too "
1813 "many times for resume" , __func__);
1814 goto out;
1815 }
1816 write_unlock_bh(&arm_state->susp_res_lock);
1817 vchiq_log_info(vchiq_susp_log_level, "%s wait for resume",
1818 __func__);
1819 if (wait_for_completion_interruptible_timeout(
1820 &arm_state->vc_resume_complete, timeout_val)
1821 <= 0) {
1822 vchiq_log_error(vchiq_susp_log_level, "%s wait for "
1823 "resume failed (%s)", __func__,
1824 resume_state_names[arm_state->vc_resume_state +
1825 VC_RESUME_NUM_OFFSET]);
1826 status = VCHIQ_ERROR;
1827 write_lock_bh(&arm_state->susp_res_lock);
1828 goto out;
1829 }
1830 vchiq_log_info(vchiq_susp_log_level, "%s resumed", __func__);
1831 write_lock_bh(&arm_state->susp_res_lock);
1832 resume_count++;
1833 }
1834 INIT_COMPLETION(arm_state->resume_blocker);
1835 arm_state->resume_blocked = 1;
1836
1837 out:
1838 return status;
1839 }
1840
1841 static inline void
1842 unblock_resume(VCHIQ_ARM_STATE_T *arm_state)
1843 {
1844 complete_all(&arm_state->resume_blocker);
1845 arm_state->resume_blocked = 0;
1846 }
1847
1848 /* Initiate suspend via slot handler. Should be called with the write lock
1849 * held */
1850 VCHIQ_STATUS_T
1851 vchiq_arm_vcsuspend(VCHIQ_STATE_T *state)
1852 {
1853 VCHIQ_STATUS_T status = VCHIQ_ERROR;
1854 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
1855
1856 if (!arm_state)
1857 goto out;
1858
1859 vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
1860 status = VCHIQ_SUCCESS;
1861
1862
1863 switch (arm_state->vc_suspend_state) {
1864 case VC_SUSPEND_REQUESTED:
1865 vchiq_log_info(vchiq_susp_log_level, "%s: suspend already "
1866 "requested", __func__);
1867 break;
1868 case VC_SUSPEND_IN_PROGRESS:
1869 vchiq_log_info(vchiq_susp_log_level, "%s: suspend already in "
1870 "progress", __func__);
1871 break;
1872
1873 default:
1874 /* We don't expect to be in other states, so log but continue
1875 * anyway */
1876 vchiq_log_error(vchiq_susp_log_level,
1877 "%s unexpected suspend state %s", __func__,
1878 suspend_state_names[arm_state->vc_suspend_state +
1879 VC_SUSPEND_NUM_OFFSET]);
1880 /* fall through */
1881 case VC_SUSPEND_REJECTED:
1882 case VC_SUSPEND_FAILED:
1883 /* Ensure any idle state actions have been run */
1884 set_suspend_state(arm_state, VC_SUSPEND_IDLE);
1885 /* fall through */
1886 case VC_SUSPEND_IDLE:
1887 vchiq_log_info(vchiq_susp_log_level,
1888 "%s: suspending", __func__);
1889 set_suspend_state(arm_state, VC_SUSPEND_REQUESTED);
1890 /* kick the slot handler thread to initiate suspend */
1891 request_poll(state, NULL, 0);
1892 break;
1893 }
1894
1895 out:
1896 vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, status);
1897 return status;
1898 }
1899
1900 void
1901 vchiq_platform_check_suspend(VCHIQ_STATE_T *state)
1902 {
1903 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
1904 int susp = 0;
1905
1906 if (!arm_state)
1907 goto out;
1908
1909 vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
1910
1911 write_lock_bh(&arm_state->susp_res_lock);
1912 if (arm_state->vc_suspend_state == VC_SUSPEND_REQUESTED &&
1913 arm_state->vc_resume_state == VC_RESUME_RESUMED) {
1914 set_suspend_state(arm_state, VC_SUSPEND_IN_PROGRESS);
1915 susp = 1;
1916 }
1917 write_unlock_bh(&arm_state->susp_res_lock);
1918
1919 if (susp)
1920 vchiq_platform_suspend(state);
1921
1922 out:
1923 vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
1924 return;
1925 }
1926
1927
1928 static void
1929 output_timeout_error(VCHIQ_STATE_T *state)
1930 {
1931 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
1932 char service_err[50] = "";
1933 int vc_use_count = arm_state->videocore_use_count;
1934 int active_services = state->unused_service;
1935 int i;
1936
1937 if (!arm_state->videocore_use_count) {
1938 snprintf(service_err, 50, " Videocore usecount is 0");
1939 goto output_msg;
1940 }
1941 for (i = 0; i < active_services; i++) {
1942 VCHIQ_SERVICE_T *service_ptr = state->services[i];
1943 if (service_ptr && service_ptr->service_use_count &&
1944 (service_ptr->srvstate != VCHIQ_SRVSTATE_FREE)) {
1945 snprintf(service_err, 50, " %c%c%c%c(%d) service has "
1946 "use count %d%s", VCHIQ_FOURCC_AS_4CHARS(
1947 service_ptr->base.fourcc),
1948 service_ptr->client_id,
1949 service_ptr->service_use_count,
1950 service_ptr->service_use_count ==
1951 vc_use_count ? "" : " (+ more)");
1952 break;
1953 }
1954 }
1955
1956 output_msg:
1957 vchiq_log_error(vchiq_susp_log_level,
1958 "timed out waiting for vc suspend (%d).%s",
1959 arm_state->autosuspend_override, service_err);
1960
1961 }
1962
1963 /* Try to get videocore into suspended state, regardless of autosuspend state.
1964 ** We don't actually force suspend, since videocore may get into a bad state
1965 ** if we force suspend at a bad time. Instead, we wait for autosuspend to
1966 ** determine a good point to suspend. If this doesn't happen within 100ms we
1967 ** report failure.
1968 **
1969 ** Returns VCHIQ_SUCCESS if videocore suspended successfully, VCHIQ_RETRY if
1970 ** videocore failed to suspend in time or VCHIQ_ERROR if interrupted.
1971 */
1972 VCHIQ_STATUS_T
1973 vchiq_arm_force_suspend(VCHIQ_STATE_T *state)
1974 {
1975 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
1976 VCHIQ_STATUS_T status = VCHIQ_ERROR;
1977 long rc = 0;
1978 int repeat = -1;
1979
1980 if (!arm_state)
1981 goto out;
1982
1983 vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
1984
1985 write_lock_bh(&arm_state->susp_res_lock);
1986
1987 status = block_resume(arm_state);
1988 if (status != VCHIQ_SUCCESS)
1989 goto unlock;
1990 if (arm_state->vc_suspend_state == VC_SUSPEND_SUSPENDED) {
1991 /* Already suspended - just block resume and exit */
1992 vchiq_log_info(vchiq_susp_log_level, "%s already suspended",
1993 __func__);
1994 status = VCHIQ_SUCCESS;
1995 goto unlock;
1996 } else if (arm_state->vc_suspend_state <= VC_SUSPEND_IDLE) {
1997 /* initiate suspend immediately in the case that we're waiting
1998 * for the timeout */
1999 stop_suspend_timer(arm_state);
2000 if (!vchiq_videocore_wanted(state)) {
2001 vchiq_log_info(vchiq_susp_log_level, "%s videocore "
2002 "idle, initiating suspend", __func__);
2003 status = vchiq_arm_vcsuspend(state);
2004 } else if (arm_state->autosuspend_override <
2005 FORCE_SUSPEND_FAIL_MAX) {
2006 vchiq_log_info(vchiq_susp_log_level, "%s letting "
2007 "videocore go idle", __func__);
2008 status = VCHIQ_SUCCESS;
2009 } else {
2010 vchiq_log_warning(vchiq_susp_log_level, "%s failed too "
2011 "many times - attempting suspend", __func__);
2012 status = vchiq_arm_vcsuspend(state);
2013 }
2014 } else {
2015 vchiq_log_info(vchiq_susp_log_level, "%s videocore suspend "
2016 "in progress - wait for completion", __func__);
2017 status = VCHIQ_SUCCESS;
2018 }
2019
2020 /* Wait for suspend to happen due to system idle (not forced..) */
2021 if (status != VCHIQ_SUCCESS)
2022 goto unblock_resume;
2023
2024 do {
2025 write_unlock_bh(&arm_state->susp_res_lock);
2026
2027 rc = wait_for_completion_interruptible_timeout(
2028 &arm_state->vc_suspend_complete,
2029 msecs_to_jiffies(FORCE_SUSPEND_TIMEOUT_MS));
2030
2031 write_lock_bh(&arm_state->susp_res_lock);
2032 if (rc < 0) {
2033 vchiq_log_warning(vchiq_susp_log_level, "%s "
2034 "interrupted waiting for suspend", __func__);
2035 status = VCHIQ_ERROR;
2036 goto unblock_resume;
2037 } else if (rc == 0) {
2038 if (arm_state->vc_suspend_state > VC_SUSPEND_IDLE) {
2039 /* Repeat timeout once if in progress */
2040 if (repeat < 0) {
2041 repeat = 1;
2042 continue;
2043 }
2044 }
2045 arm_state->autosuspend_override++;
2046 output_timeout_error(state);
2047
2048 status = VCHIQ_RETRY;
2049 goto unblock_resume;
2050 }
2051 } while (0 < (repeat--));
2052
2053 /* Check and report state in case we need to abort ARM suspend */
2054 if (arm_state->vc_suspend_state != VC_SUSPEND_SUSPENDED) {
2055 status = VCHIQ_RETRY;
2056 vchiq_log_error(vchiq_susp_log_level,
2057 "%s videocore suspend failed (state %s)", __func__,
2058 suspend_state_names[arm_state->vc_suspend_state +
2059 VC_SUSPEND_NUM_OFFSET]);
2060 /* Reset the state only if it's still in an error state.
2061 * Something could have already initiated another suspend. */
2062 if (arm_state->vc_suspend_state < VC_SUSPEND_IDLE)
2063 set_suspend_state(arm_state, VC_SUSPEND_IDLE);
2064
2065 goto unblock_resume;
2066 }
2067
2068 /* successfully suspended - unlock and exit */
2069 goto unlock;
2070
2071 unblock_resume:
2072 /* all error states need to unblock resume before exit */
2073 unblock_resume(arm_state);
2074
2075 unlock:
2076 write_unlock_bh(&arm_state->susp_res_lock);
2077
2078 out:
2079 vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, status);
2080 return status;
2081 }
2082
2083 void
2084 vchiq_check_suspend(VCHIQ_STATE_T *state)
2085 {
2086 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
2087
2088 if (!arm_state)
2089 goto out;
2090
2091 vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
2092
2093 write_lock_bh(&arm_state->susp_res_lock);
2094 if (arm_state->vc_suspend_state != VC_SUSPEND_SUSPENDED &&
2095 arm_state->first_connect &&
2096 !vchiq_videocore_wanted(state)) {
2097 vchiq_arm_vcsuspend(state);
2098 }
2099 write_unlock_bh(&arm_state->susp_res_lock);
2100
2101 out:
2102 vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
2103 return;
2104 }
2105
2106
2107 int
2108 vchiq_arm_allow_resume(VCHIQ_STATE_T *state)
2109 {
2110 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
2111 int resume = 0;
2112 int ret = -1;
2113
2114 if (!arm_state)
2115 goto out;
2116
2117 vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
2118
2119 write_lock_bh(&arm_state->susp_res_lock);
2120 unblock_resume(arm_state);
2121 resume = vchiq_check_resume(state);
2122 write_unlock_bh(&arm_state->susp_res_lock);
2123
2124 if (resume) {
2125 if (wait_for_completion_interruptible(
2126 &arm_state->vc_resume_complete) < 0) {
2127 vchiq_log_error(vchiq_susp_log_level,
2128 "%s interrupted", __func__);
2129 /* failed, cannot accurately derive suspend
2130 * state, so exit early. */
2131 goto out;
2132 }
2133 }
2134
2135 read_lock_bh(&arm_state->susp_res_lock);
2136 if (arm_state->vc_suspend_state == VC_SUSPEND_SUSPENDED) {
2137 vchiq_log_info(vchiq_susp_log_level,
2138 "%s: Videocore remains suspended", __func__);
2139 } else {
2140 vchiq_log_info(vchiq_susp_log_level,
2141 "%s: Videocore resumed", __func__);
2142 ret = 0;
2143 }
2144 read_unlock_bh(&arm_state->susp_res_lock);
2145 out:
2146 vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, ret);
2147 return ret;
2148 }
2149
2150 /* This function should be called with the write lock held */
2151 int
2152 vchiq_check_resume(VCHIQ_STATE_T *state)
2153 {
2154 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
2155 int resume = 0;
2156
2157 if (!arm_state)
2158 goto out;
2159
2160 vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
2161
2162 if (need_resume(state)) {
2163 set_resume_state(arm_state, VC_RESUME_REQUESTED);
2164 request_poll(state, NULL, 0);
2165 resume = 1;
2166 }
2167
2168 out:
2169 vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
2170 return resume;
2171 }
2172
2173 #ifdef notyet
2174 void
2175 vchiq_platform_check_resume(VCHIQ_STATE_T *state)
2176 {
2177 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
2178 int res = 0;
2179
2180 if (!arm_state)
2181 goto out;
2182
2183 vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
2184
2185 write_lock_bh(&arm_state->susp_res_lock);
2186 if (arm_state->wake_address == 0) {
2187 vchiq_log_info(vchiq_susp_log_level,
2188 "%s: already awake", __func__);
2189 goto unlock;
2190 }
2191 if (arm_state->vc_resume_state == VC_RESUME_IN_PROGRESS) {
2192 vchiq_log_info(vchiq_susp_log_level,
2193 "%s: already resuming", __func__);
2194 goto unlock;
2195 }
2196
2197 if (arm_state->vc_resume_state == VC_RESUME_REQUESTED) {
2198 set_resume_state(arm_state, VC_RESUME_IN_PROGRESS);
2199 res = 1;
2200 } else
2201 vchiq_log_trace(vchiq_susp_log_level,
2202 "%s: not resuming (resume state %s)", __func__,
2203 resume_state_names[arm_state->vc_resume_state +
2204 VC_RESUME_NUM_OFFSET]);
2205
2206 unlock:
2207 write_unlock_bh(&arm_state->susp_res_lock);
2208
2209 if (res)
2210 vchiq_platform_resume(state);
2211
2212 out:
2213 vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
2214 return;
2215
2216 }
2217 #endif
2218
2219
2220
2221 VCHIQ_STATUS_T
2222 vchiq_use_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service,
2223 enum USE_TYPE_E use_type)
2224 {
2225 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
2226 VCHIQ_STATUS_T ret = VCHIQ_SUCCESS;
2227 char entity[16];
2228 int *entity_uc;
2229 int local_uc, local_entity_uc;
2230
2231 if (!arm_state)
2232 goto out;
2233
2234 vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
2235
2236 if (use_type == USE_TYPE_VCHIQ) {
2237 sprintf(entity, "VCHIQ: ");
2238 entity_uc = &arm_state->peer_use_count;
2239 } else if (service) {
2240 sprintf(entity, "%c%c%c%c:%03d",
2241 VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
2242 service->client_id);
2243 entity_uc = &service->service_use_count;
2244 } else {
2245 vchiq_log_error(vchiq_susp_log_level, "%s null service "
2246 "ptr", __func__);
2247 ret = VCHIQ_ERROR;
2248 goto out;
2249 }
2250
2251 write_lock_bh(&arm_state->susp_res_lock);
2252 while (arm_state->resume_blocked) {
2253 /* If we call 'use' while force suspend is waiting for suspend,
2254 * then we're about to block the thread which the force is
2255 * waiting to complete, so we're bound to just time out. In this
2256 * case, set the suspend state such that the wait will be
2257 * canceled, so we can complete as quickly as possible. */
2258 if (arm_state->resume_blocked && arm_state->vc_suspend_state ==
2259 VC_SUSPEND_IDLE) {
2260 set_suspend_state(arm_state, VC_SUSPEND_FORCE_CANCELED);
2261 break;
2262 }
2263 /* If suspend is already in progress then we need to block */
2264 if (!try_wait_for_completion(&arm_state->resume_blocker)) {
2265 /* Indicate that there are threads waiting on the resume
2266 * blocker. These need to be allowed to complete before
2267 * a _second_ call to force suspend can complete,
2268 * otherwise low priority threads might never actually
2269 * continue */
2270 arm_state->blocked_count++;
2271 write_unlock_bh(&arm_state->susp_res_lock);
2272 vchiq_log_info(vchiq_susp_log_level, "%s %s resume "
2273 "blocked - waiting...", __func__, entity);
2274 if (wait_for_completion_killable(
2275 &arm_state->resume_blocker) != 0) {
2276 vchiq_log_error(vchiq_susp_log_level, "%s %s "
2277 "wait for resume blocker interrupted",
2278 __func__, entity);
2279 ret = VCHIQ_ERROR;
2280 write_lock_bh(&arm_state->susp_res_lock);
2281 arm_state->blocked_count--;
2282 write_unlock_bh(&arm_state->susp_res_lock);
2283 goto out;
2284 }
2285 vchiq_log_info(vchiq_susp_log_level, "%s %s resume "
2286 "unblocked", __func__, entity);
2287 write_lock_bh(&arm_state->susp_res_lock);
2288 if (--arm_state->blocked_count == 0)
2289 complete_all(&arm_state->blocked_blocker);
2290 }
2291 }
2292
2293 stop_suspend_timer(arm_state);
2294
2295 local_uc = ++arm_state->videocore_use_count;
2296 local_entity_uc = ++(*entity_uc);
2297
2298 /* If there's a pending request which hasn't yet been serviced then
2299 * just clear it. If we're past VC_SUSPEND_REQUESTED state then
2300 * vc_resume_complete will block until we either resume or fail to
2301 * suspend */
2302 if (arm_state->vc_suspend_state <= VC_SUSPEND_REQUESTED)
2303 set_suspend_state(arm_state, VC_SUSPEND_IDLE);
2304
2305 if ((use_type != USE_TYPE_SERVICE_NO_RESUME) && need_resume(state)) {
2306 set_resume_state(arm_state, VC_RESUME_REQUESTED);
2307 vchiq_log_info(vchiq_susp_log_level,
2308 "%s %s count %d, state count %d",
2309 __func__, entity, local_entity_uc, local_uc);
2310 request_poll(state, NULL, 0);
2311 } else
2312 vchiq_log_trace(vchiq_susp_log_level,
2313 "%s %s count %d, state count %d",
2314 __func__, entity, *entity_uc, local_uc);
2315
2316
2317 write_unlock_bh(&arm_state->susp_res_lock);
2318
2319 /* Completion is in a done state when we're not suspended, so this won't
2320 * block for the non-suspended case. */
2321 if (!try_wait_for_completion(&arm_state->vc_resume_complete)) {
2322 vchiq_log_info(vchiq_susp_log_level, "%s %s wait for resume",
2323 __func__, entity);
2324 if (wait_for_completion_killable(
2325 &arm_state->vc_resume_complete) != 0) {
2326 vchiq_log_error(vchiq_susp_log_level, "%s %s wait for "
2327 "resume interrupted", __func__, entity);
2328 ret = VCHIQ_ERROR;
2329 goto out;
2330 }
2331 vchiq_log_info(vchiq_susp_log_level, "%s %s resumed", __func__,
2332 entity);
2333 }
2334
2335 if (ret == VCHIQ_SUCCESS) {
2336 VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
2337 long ack_cnt = atomic_xchg(&arm_state->ka_use_ack_count, 0);
2338 while (ack_cnt && (status == VCHIQ_SUCCESS)) {
2339 /* Send the use notify to videocore */
2340 status = vchiq_send_remote_use_active(state);
2341 if (status == VCHIQ_SUCCESS)
2342 ack_cnt--;
2343 else
2344 atomic_add(ack_cnt,
2345 &arm_state->ka_use_ack_count);
2346 }
2347 }
2348
2349 out:
2350 vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, ret);
2351 return ret;
2352 }
2353
2354 VCHIQ_STATUS_T
2355 vchiq_release_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service)
2356 {
2357 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
2358 VCHIQ_STATUS_T ret = VCHIQ_SUCCESS;
2359 char entity[16];
2360 int *entity_uc;
2361 int local_uc, local_entity_uc;
2362
2363 if (!arm_state)
2364 goto out;
2365
2366 vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
2367
2368 if (service) {
2369 sprintf(entity, "%c%c%c%c:%03d",
2370 VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
2371 service->client_id);
2372 entity_uc = &service->service_use_count;
2373 } else {
2374 sprintf(entity, "PEER: ");
2375 entity_uc = &arm_state->peer_use_count;
2376 }
2377
2378 write_lock_bh(&arm_state->susp_res_lock);
2379 if (!arm_state->videocore_use_count || !(*entity_uc)) {
2380 /* Don't use BUG_ON - don't allow user thread to crash kernel */
2381 WARN_ON(!arm_state->videocore_use_count);
2382 WARN_ON(!(*entity_uc));
2383 ret = VCHIQ_ERROR;
2384 goto unlock;
2385 }
2386 local_uc = --arm_state->videocore_use_count;
2387 local_entity_uc = --(*entity_uc);
2388
2389 if (!vchiq_videocore_wanted(state)) {
2390 if (vchiq_platform_use_suspend_timer() &&
2391 !arm_state->resume_blocked) {
2392 /* Only use the timer if we're not trying to force
2393 * suspend (=> resume_blocked) */
2394 start_suspend_timer(arm_state);
2395 } else {
2396 vchiq_log_info(vchiq_susp_log_level,
2397 "%s %s count %d, state count %d - suspending",
2398 __func__, entity, *entity_uc,
2399 arm_state->videocore_use_count);
2400 vchiq_arm_vcsuspend(state);
2401 }
2402 } else
2403 vchiq_log_trace(vchiq_susp_log_level,
2404 "%s %s count %d, state count %d",
2405 __func__, entity, *entity_uc,
2406 arm_state->videocore_use_count);
2407
2408 unlock:
2409 write_unlock_bh(&arm_state->susp_res_lock);
2410
2411 out:
2412 vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, ret);
2413 return ret;
2414 }
2415
2416 void
2417 vchiq_on_remote_use(VCHIQ_STATE_T *state)
2418 {
2419 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
2420 vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
2421 atomic_inc(&arm_state->ka_use_count);
2422 complete(&arm_state->ka_evt);
2423 }
2424
2425 void
2426 vchiq_on_remote_release(VCHIQ_STATE_T *state)
2427 {
2428 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
2429 vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
2430 atomic_inc(&arm_state->ka_release_count);
2431 complete(&arm_state->ka_evt);
2432 }
2433
2434 VCHIQ_STATUS_T
2435 vchiq_use_service_internal(VCHIQ_SERVICE_T *service)
2436 {
2437 return vchiq_use_internal(service->state, service, USE_TYPE_SERVICE);
2438 }
2439
2440 VCHIQ_STATUS_T
2441 vchiq_release_service_internal(VCHIQ_SERVICE_T *service)
2442 {
2443 return vchiq_release_internal(service->state, service);
2444 }
2445
2446 static void suspend_timer_callback(unsigned long context)
2447 {
2448 VCHIQ_STATE_T *state = (VCHIQ_STATE_T *)context;
2449 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
2450 if (!arm_state)
2451 goto out;
2452 vchiq_log_info(vchiq_susp_log_level,
2453 "%s - suspend timer expired - check suspend", __func__);
2454 vchiq_check_suspend(state);
2455 out:
2456 return;
2457 }
2458
2459 VCHIQ_STATUS_T
2460 vchiq_use_service_no_resume(VCHIQ_SERVICE_HANDLE_T handle)
2461 {
2462 VCHIQ_STATUS_T ret = VCHIQ_ERROR;
2463 VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
2464 if (service) {
2465 ret = vchiq_use_internal(service->state, service,
2466 USE_TYPE_SERVICE_NO_RESUME);
2467 unlock_service(service);
2468 }
2469 return ret;
2470 }
2471
2472 VCHIQ_STATUS_T
2473 vchiq_use_service(VCHIQ_SERVICE_HANDLE_T handle)
2474 {
2475 VCHIQ_STATUS_T ret = VCHIQ_ERROR;
2476 VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
2477 if (service) {
2478 ret = vchiq_use_internal(service->state, service,
2479 USE_TYPE_SERVICE);
2480 unlock_service(service);
2481 }
2482 return ret;
2483 }
2484
2485 VCHIQ_STATUS_T
2486 vchiq_release_service(VCHIQ_SERVICE_HANDLE_T handle)
2487 {
2488 VCHIQ_STATUS_T ret = VCHIQ_ERROR;
2489 VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
2490 if (service) {
2491 ret = vchiq_release_internal(service->state, service);
2492 unlock_service(service);
2493 }
2494 return ret;
2495 }
2496
2497 void
2498 vchiq_dump_service_use_state(VCHIQ_STATE_T *state)
2499 {
2500 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
2501 int i, j = 0;
2502 /* Only dump 64 services */
2503 static const int local_max_services = 64;
2504 /* If there's more than 64 services, only dump ones with
2505 * non-zero counts */
2506 int only_nonzero = 0;
2507 static const char *nz = "<-- preventing suspend";
2508
2509 enum vc_suspend_status vc_suspend_state;
2510 enum vc_resume_status vc_resume_state;
2511 int peer_count;
2512 int vc_use_count;
2513 int active_services;
2514 struct service_data_struct {
2515 int fourcc;
2516 int clientid;
2517 int use_count;
2518 } service_data[local_max_services];
2519
2520 if (!arm_state)
2521 return;
2522
2523 read_lock_bh(&arm_state->susp_res_lock);
2524 vc_suspend_state = arm_state->vc_suspend_state;
2525 vc_resume_state = arm_state->vc_resume_state;
2526 peer_count = arm_state->peer_use_count;
2527 vc_use_count = arm_state->videocore_use_count;
2528 active_services = state->unused_service;
2529 if (active_services > local_max_services)
2530 only_nonzero = 1;
2531
2532 for (i = 0; (i < active_services) && (j < local_max_services); i++) {
2533 VCHIQ_SERVICE_T *service_ptr = state->services[i];
2534 if (!service_ptr)
2535 continue;
2536
2537 if (only_nonzero && !service_ptr->service_use_count)
2538 continue;
2539
2540 if (service_ptr->srvstate != VCHIQ_SRVSTATE_FREE) {
2541 service_data[j].fourcc = service_ptr->base.fourcc;
2542 service_data[j].clientid = service_ptr->client_id;
2543 service_data[j++].use_count = service_ptr->
2544 service_use_count;
2545 }
2546 }
2547
2548 read_unlock_bh(&arm_state->susp_res_lock);
2549
2550 vchiq_log_warning(vchiq_susp_log_level,
2551 "-- Videcore suspend state: %s --",
2552 suspend_state_names[vc_suspend_state + VC_SUSPEND_NUM_OFFSET]);
2553 vchiq_log_warning(vchiq_susp_log_level,
2554 "-- Videcore resume state: %s --",
2555 resume_state_names[vc_resume_state + VC_RESUME_NUM_OFFSET]);
2556
2557 if (only_nonzero)
2558 vchiq_log_warning(vchiq_susp_log_level, "Too many active "
2559 "services (%d). Only dumping up to first %d services "
2560 "with non-zero use-count", active_services,
2561 local_max_services);
2562
2563 for (i = 0; i < j; i++) {
2564 vchiq_log_warning(vchiq_susp_log_level,
2565 "----- %c%c%c%c:%d service count %d %s",
2566 VCHIQ_FOURCC_AS_4CHARS(service_data[i].fourcc),
2567 service_data[i].clientid,
2568 service_data[i].use_count,
2569 service_data[i].use_count ? nz : "");
2570 }
2571 vchiq_log_warning(vchiq_susp_log_level,
2572 "----- VCHIQ use count count %d", peer_count);
2573 vchiq_log_warning(vchiq_susp_log_level,
2574 "--- Overall vchiq instance use count %d", vc_use_count);
2575
2576 vchiq_dump_platform_use_state(state);
2577 }
2578
2579 VCHIQ_STATUS_T
2580 vchiq_check_service(VCHIQ_SERVICE_T *service)
2581 {
2582 VCHIQ_ARM_STATE_T *arm_state;
2583 VCHIQ_STATUS_T ret = VCHIQ_ERROR;
2584
2585 if (!service || !service->state)
2586 goto out;
2587
2588 vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
2589
2590 arm_state = vchiq_platform_get_arm_state(service->state);
2591
2592 read_lock_bh(&arm_state->susp_res_lock);
2593 if (service->service_use_count)
2594 ret = VCHIQ_SUCCESS;
2595 read_unlock_bh(&arm_state->susp_res_lock);
2596
2597 if (ret == VCHIQ_ERROR) {
2598 vchiq_log_error(vchiq_susp_log_level,
2599 "%s ERROR - %c%c%c%c:%d service count %d, "
2600 "state count %d, videocore suspend state %s", __func__,
2601 VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
2602 service->client_id, service->service_use_count,
2603 arm_state->videocore_use_count,
2604 suspend_state_names[arm_state->vc_suspend_state +
2605 VC_SUSPEND_NUM_OFFSET]);
2606 vchiq_dump_service_use_state(service->state);
2607 }
2608 out:
2609 return ret;
2610 }
2611
2612 /* stub functions */
2613 void vchiq_on_remote_use_active(VCHIQ_STATE_T *state)
2614 {
2615 (void)state;
2616 }
2617
2618 void vchiq_platform_conn_state_changed(VCHIQ_STATE_T *state,
2619 VCHIQ_CONNSTATE_T oldstate, VCHIQ_CONNSTATE_T newstate)
2620 {
2621 VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
2622 vchiq_log_info(vchiq_susp_log_level, "%d: %s->%s", state->id,
2623 get_conn_state_name(oldstate), get_conn_state_name(newstate));
2624 if (state->conn_state == VCHIQ_CONNSTATE_CONNECTED) {
2625 write_lock_bh(&arm_state->susp_res_lock);
2626 if (!arm_state->first_connect) {
2627 char threadname[10];
2628 arm_state->first_connect = 1;
2629 write_unlock_bh(&arm_state->susp_res_lock);
2630 snprintf(threadname, sizeof(threadname), "VCHIQka-%d",
2631 state->id);
2632 arm_state->ka_thread = vchiq_thread_create(
2633 &vchiq_keepalive_thread_func,
2634 (void *)state,
2635 threadname);
2636 if (arm_state->ka_thread == NULL) {
2637 vchiq_log_error(vchiq_susp_log_level,
2638 "vchiq: FATAL: couldn't create thread %s",
2639 threadname);
2640 } else {
2641 wake_up_process(arm_state->ka_thread);
2642 }
2643 } else
2644 write_unlock_bh(&arm_state->susp_res_lock);
2645 }
2646 }
2647
2648 /****************************************************************************
2649 *
2650 * vchiq_init - called when the module is loaded.
2651 *
2652 ***************************************************************************/
2653
2654 int __init vchiq_init(void);
2655 int __init
2656 vchiq_init(void)
2657 {
2658 int err;
2659
2660 #ifdef notyet
2661 /* create proc entries */
2662 err = vchiq_proc_init();
2663 if (err != 0)
2664 goto failed_proc_init;
2665 #endif
2666
2667 spin_lock_init(&msg_queue_spinlock);
2668
2669 err = vchiq_platform_init(&g_state);
2670 if (err != 0)
2671 goto failed_platform_init;
2672
2673 vchiq_log_info(vchiq_arm_log_level,
2674 "vchiq: initialised - version %d (min %d)",
2675 VCHIQ_VERSION, VCHIQ_VERSION_MIN);
2676
2677 return 0;
2678
2679 failed_platform_init:
2680 vchiq_log_warning(vchiq_arm_log_level, "could not load vchiq");
2681 return err;
2682 }
2683
2684 #ifdef notyet
2685 static int vchiq_instance_get_use_count(VCHIQ_INSTANCE_T instance)
2686 {
2687 VCHIQ_SERVICE_T *service;
2688 int use_count = 0, i;
2689 i = 0;
2690 while ((service = next_service_by_instance(instance->state,
2691 instance, &i)) != NULL) {
2692 use_count += service->service_use_count;
2693 unlock_service(service);
2694 }
2695 return use_count;
2696 }
2697
2698 /* read the per-process use-count */
2699 static int proc_read_use_count(char *page, char **start,
2700 off_t off, int count,
2701 int *eof, void *data)
2702 {
2703 VCHIQ_INSTANCE_T instance = data;
2704 int len, use_count;
2705
2706 use_count = vchiq_instance_get_use_count(instance);
2707 len = snprintf(page+off, count, "%d\n", use_count);
2708
2709 return len;
2710 }
2711
2712 /* add an instance (process) to the proc entries */
2713 static int vchiq_proc_add_instance(VCHIQ_INSTANCE_T instance)
2714 {
2715 char pidstr[32];
2716 struct proc_dir_entry *top, *use_count;
2717 struct proc_dir_entry *clients = vchiq_clients_top();
2718 int pid = instance->pid;
2719
2720 snprintf(pidstr, sizeof(pidstr), "%d", pid);
2721 top = proc_mkdir(pidstr, clients);
2722 if (!top)
2723 goto fail_top;
2724
2725 use_count = create_proc_read_entry("use_count",
2726 0444, top,
2727 proc_read_use_count,
2728 instance);
2729 if (!use_count)
2730 goto fail_use_count;
2731
2732 instance->proc_entry = top;
2733
2734 return 0;
2735
2736 fail_use_count:
2737 remove_proc_entry(top->name, clients);
2738 fail_top:
2739 return -ENOMEM;
2740 }
2741
2742 static void vchiq_proc_remove_instance(VCHIQ_INSTANCE_T instance)
2743 {
2744 struct proc_dir_entry *clients = vchiq_clients_top();
2745 remove_proc_entry("use_count", instance->proc_entry);
2746 remove_proc_entry(instance->proc_entry->name, clients);
2747 }
2748
2749 #endif
2750
2751 /****************************************************************************
2752 *
2753 * vchiq_exit - called when the module is unloaded.
2754 *
2755 ***************************************************************************/
2756
2757 void vchiq_exit(void);
2758 void
2759 vchiq_exit(void)
2760 {
2761 vchiq_platform_exit(&g_state);
2762 }
2763