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