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