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