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