1 From c9e2d1daa32fd2267d3a61ae3afc2f429746a01f Mon Sep 17 00:00:00 2001
2 From: popcornmix <popcornmix@gmail.com>
3 Date: Tue, 2 Jul 2013 23:42:01 +0100
4 Subject: [PATCH 06/54] bcm2708 vchiq driver
6 Signed-off-by: popcornmix <popcornmix@gmail.com>
8 vchiq: create_pagelist copes with vmalloc memory
10 Signed-off-by: Daniel Stone <daniels@collabora.com>
12 vchiq: fix the shim message release
14 Signed-off-by: Daniel Stone <daniels@collabora.com>
16 vchiq: export additional symbols
18 Signed-off-by: Daniel Stone <daniels@collabora.com>
20 drivers/misc/Kconfig | 1 +
21 drivers/misc/Makefile | 1 +
22 drivers/misc/vc04_services/Kconfig | 9 +
23 drivers/misc/vc04_services/Makefile | 17 +
24 .../interface/vchi/connections/connection.h | 328 ++
25 .../interface/vchi/message_drivers/message.h | 204 ++
26 drivers/misc/vc04_services/interface/vchi/vchi.h | 373 ++
27 .../misc/vc04_services/interface/vchi/vchi_cfg.h | 224 ++
28 .../interface/vchi/vchi_cfg_internal.h | 71 +
29 .../vc04_services/interface/vchi/vchi_common.h | 163 +
30 .../misc/vc04_services/interface/vchi/vchi_mh.h | 42 +
31 .../misc/vc04_services/interface/vchiq_arm/vchiq.h | 40 +
32 .../vc04_services/interface/vchiq_arm/vchiq_2835.h | 42 +
33 .../interface/vchiq_arm/vchiq_2835_arm.c | 561 +++
34 .../vc04_services/interface/vchiq_arm/vchiq_arm.c | 2813 ++++++++++++++
35 .../vc04_services/interface/vchiq_arm/vchiq_arm.h | 212 ++
36 .../interface/vchiq_arm/vchiq_build_info.h | 37 +
37 .../vc04_services/interface/vchiq_arm/vchiq_cfg.h | 60 +
38 .../interface/vchiq_arm/vchiq_connected.c | 119 +
39 .../interface/vchiq_arm/vchiq_connected.h | 50 +
40 .../vc04_services/interface/vchiq_arm/vchiq_core.c | 3824 ++++++++++++++++++++
41 .../vc04_services/interface/vchiq_arm/vchiq_core.h | 706 ++++
42 .../interface/vchiq_arm/vchiq_genversion | 87 +
43 .../vc04_services/interface/vchiq_arm/vchiq_if.h | 188 +
44 .../interface/vchiq_arm/vchiq_ioctl.h | 129 +
45 .../interface/vchiq_arm/vchiq_kern_lib.c | 456 +++
46 .../interface/vchiq_arm/vchiq_memdrv.h | 71 +
47 .../interface/vchiq_arm/vchiq_pagelist.h | 58 +
48 .../vc04_services/interface/vchiq_arm/vchiq_proc.c | 253 ++
49 .../vc04_services/interface/vchiq_arm/vchiq_shim.c | 828 +++++
50 .../vc04_services/interface/vchiq_arm/vchiq_util.c | 151 +
51 .../vc04_services/interface/vchiq_arm/vchiq_util.h | 81 +
52 .../interface/vchiq_arm/vchiq_version.c | 59 +
53 33 files changed, 12258 insertions(+)
54 create mode 100644 drivers/misc/vc04_services/Kconfig
55 create mode 100644 drivers/misc/vc04_services/Makefile
56 create mode 100644 drivers/misc/vc04_services/interface/vchi/connections/connection.h
57 create mode 100644 drivers/misc/vc04_services/interface/vchi/message_drivers/message.h
58 create mode 100644 drivers/misc/vc04_services/interface/vchi/vchi.h
59 create mode 100644 drivers/misc/vc04_services/interface/vchi/vchi_cfg.h
60 create mode 100644 drivers/misc/vc04_services/interface/vchi/vchi_cfg_internal.h
61 create mode 100644 drivers/misc/vc04_services/interface/vchi/vchi_common.h
62 create mode 100644 drivers/misc/vc04_services/interface/vchi/vchi_mh.h
63 create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq.h
64 create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835.h
65 create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835_arm.c
66 create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.c
67 create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.h
68 create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_build_info.h
69 create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_cfg.h
70 create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.c
71 create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.h
72 create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.c
73 create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.h
74 create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_genversion
75 create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_if.h
76 create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_ioctl.h
77 create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_kern_lib.c
78 create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_memdrv.h
79 create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_pagelist.h
80 create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_proc.c
81 create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_shim.c
82 create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.c
83 create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.h
84 create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_version.c
86 diff --git a/drivers/misc/Kconfig b/drivers/misc/Kconfig
87 index 6cb388e..56867fa 100644
88 --- a/drivers/misc/Kconfig
89 +++ b/drivers/misc/Kconfig
90 @@ -524,6 +524,7 @@ source "drivers/misc/carma/Kconfig"
91 source "drivers/misc/altera-stapl/Kconfig"
92 source "drivers/misc/mei/Kconfig"
93 source "drivers/misc/vmw_vmci/Kconfig"
94 +source "drivers/misc/vc04_services/Kconfig"
95 source "drivers/misc/mic/Kconfig"
96 source "drivers/misc/genwqe/Kconfig"
98 diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile
99 index 99b9424..ad85616 100644
100 --- a/drivers/misc/Makefile
101 +++ b/drivers/misc/Makefile
102 @@ -52,5 +52,6 @@ obj-$(CONFIG_INTEL_MEI) += mei/
103 obj-$(CONFIG_VMWARE_VMCI) += vmw_vmci/
104 obj-$(CONFIG_LATTICE_ECP3_CONFIG) += lattice-ecp3-config.o
105 obj-$(CONFIG_SRAM) += sram.o
106 +obj-y += vc04_services/
108 obj-$(CONFIG_GENWQE) += genwqe/
109 diff --git a/drivers/misc/vc04_services/Kconfig b/drivers/misc/vc04_services/Kconfig
111 index 0000000..2663933
113 +++ b/drivers/misc/vc04_services/Kconfig
115 +config BCM2708_VCHIQ
116 + tristate "Videocore VCHIQ"
117 + depends on MACH_BCM2708
120 + Kernel to VideoCore communication interface for the
121 + BCM2708 family of products.
122 + Defaults to Y when the Broadcom Videocore services
123 + are included in the build, N otherwise.
124 diff --git a/drivers/misc/vc04_services/Makefile b/drivers/misc/vc04_services/Makefile
126 index 0000000..4224f58
128 +++ b/drivers/misc/vc04_services/Makefile
130 +ifeq ($(CONFIG_MACH_BCM2708),y)
132 +obj-$(CONFIG_BCM2708_VCHIQ) += vchiq.o
135 + interface/vchiq_arm/vchiq_core.o \
136 + interface/vchiq_arm/vchiq_arm.o \
137 + interface/vchiq_arm/vchiq_kern_lib.o \
138 + interface/vchiq_arm/vchiq_2835_arm.o \
139 + interface/vchiq_arm/vchiq_proc.o \
140 + interface/vchiq_arm/vchiq_shim.o \
141 + interface/vchiq_arm/vchiq_util.o \
142 + interface/vchiq_arm/vchiq_connected.o \
144 +ccflags-y += -DVCOS_VERIFY_BKPTS=1 -Idrivers/misc/vc04_services -DUSE_VCHIQ_ARM -D__VCCOREVER__=0x04000000
147 diff --git a/drivers/misc/vc04_services/interface/vchi/connections/connection.h b/drivers/misc/vc04_services/interface/vchi/connections/connection.h
149 index 0000000..fef6ac3
151 +++ b/drivers/misc/vc04_services/interface/vchi/connections/connection.h
154 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
156 + * Redistribution and use in source and binary forms, with or without
157 + * modification, are permitted provided that the following conditions
159 + * 1. Redistributions of source code must retain the above copyright
160 + * notice, this list of conditions, and the following disclaimer,
161 + * without modification.
162 + * 2. Redistributions in binary form must reproduce the above copyright
163 + * notice, this list of conditions and the following disclaimer in the
164 + * documentation and/or other materials provided with the distribution.
165 + * 3. The names of the above-listed copyright holders may not be used
166 + * to endorse or promote products derived from this software without
167 + * specific prior written permission.
169 + * ALTERNATIVELY, this software may be distributed under the terms of the
170 + * GNU General Public License ("GPL") version 2, as published by the Free
171 + * Software Foundation.
173 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
174 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
175 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
176 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
177 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
178 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
179 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
180 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
181 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
182 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
183 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
186 +#ifndef CONNECTION_H_
187 +#define CONNECTION_H_
189 +#include <linux/kernel.h>
190 +#include <linux/types.h>
191 +#include <linux/semaphore.h>
193 +#include "interface/vchi/vchi_cfg_internal.h"
194 +#include "interface/vchi/vchi_common.h"
195 +#include "interface/vchi/message_drivers/message.h"
197 +/******************************************************************************
199 + *****************************************************************************/
201 +// Opaque handle for a connection / service pair
202 +typedef struct opaque_vchi_connection_connected_service_handle_t *VCHI_CONNECTION_SERVICE_HANDLE_T;
204 +// opaque handle to the connection state information
205 +typedef struct opaque_vchi_connection_info_t VCHI_CONNECTION_STATE_T;
207 +typedef struct vchi_connection_t VCHI_CONNECTION_T;
210 +/******************************************************************************
212 + *****************************************************************************/
214 +// Routine to init a connection with a particular low level driver
215 +typedef VCHI_CONNECTION_STATE_T * (*VCHI_CONNECTION_INIT_T)( struct vchi_connection_t * connection,
216 + const VCHI_MESSAGE_DRIVER_T * driver );
218 +// Routine to control CRC enabling at a connection level
219 +typedef int32_t (*VCHI_CONNECTION_CRC_CONTROL_T)( VCHI_CONNECTION_STATE_T *state_handle,
220 + VCHI_CRC_CONTROL_T control );
222 +// Routine to create a service
223 +typedef int32_t (*VCHI_CONNECTION_SERVICE_CONNECT_T)( VCHI_CONNECTION_STATE_T *state_handle,
224 + int32_t service_id,
225 + uint32_t rx_fifo_size,
226 + uint32_t tx_fifo_size,
228 + VCHI_CALLBACK_T callback,
229 + void *callback_param,
231 + int32_t want_unaligned_bulk_rx,
232 + int32_t want_unaligned_bulk_tx,
233 + VCHI_CONNECTION_SERVICE_HANDLE_T *service_handle );
235 +// Routine to close a service
236 +typedef int32_t (*VCHI_CONNECTION_SERVICE_DISCONNECT_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle );
238 +// Routine to queue a message
239 +typedef int32_t (*VCHI_CONNECTION_SERVICE_QUEUE_MESSAGE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
241 + uint32_t data_size,
242 + VCHI_FLAGS_T flags,
243 + void *msg_handle );
245 +// scatter-gather (vector) message queueing
246 +typedef int32_t (*VCHI_CONNECTION_SERVICE_QUEUE_MESSAGEV_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
247 + VCHI_MSG_VECTOR_T *vector,
249 + VCHI_FLAGS_T flags,
250 + void *msg_handle );
252 +// Routine to dequeue a message
253 +typedef int32_t (*VCHI_CONNECTION_SERVICE_DEQUEUE_MESSAGE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
255 + uint32_t max_data_size_to_read,
256 + uint32_t *actual_msg_size,
257 + VCHI_FLAGS_T flags );
259 +// Routine to peek at a message
260 +typedef int32_t (*VCHI_CONNECTION_SERVICE_PEEK_MESSAGE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
262 + uint32_t *msg_size,
263 + VCHI_FLAGS_T flags );
265 +// Routine to hold a message
266 +typedef int32_t (*VCHI_CONNECTION_SERVICE_HOLD_MESSAGE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
268 + uint32_t *msg_size,
269 + VCHI_FLAGS_T flags,
270 + void **message_handle );
272 +// Routine to initialise a received message iterator
273 +typedef int32_t (*VCHI_CONNECTION_SERVICE_LOOKAHEAD_MESSAGE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
274 + VCHI_MSG_ITER_T *iter,
275 + VCHI_FLAGS_T flags );
277 +// Routine to release a held message
278 +typedef int32_t (*VCHI_CONNECTION_HELD_MSG_RELEASE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
279 + void *message_handle );
281 +// Routine to get info on a held message
282 +typedef int32_t (*VCHI_CONNECTION_HELD_MSG_INFO_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
283 + void *message_handle,
286 + uint32_t *tx_timestamp,
287 + uint32_t *rx_timestamp );
289 +// Routine to check whether the iterator has a next message
290 +typedef int32_t (*VCHI_CONNECTION_MSG_ITER_HAS_NEXT_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service,
291 + const VCHI_MSG_ITER_T *iter );
293 +// Routine to advance the iterator
294 +typedef int32_t (*VCHI_CONNECTION_MSG_ITER_NEXT_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service,
295 + VCHI_MSG_ITER_T *iter,
297 + uint32_t *msg_size );
299 +// Routine to remove the last message returned by the iterator
300 +typedef int32_t (*VCHI_CONNECTION_MSG_ITER_REMOVE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service,
301 + VCHI_MSG_ITER_T *iter );
303 +// Routine to hold the last message returned by the iterator
304 +typedef int32_t (*VCHI_CONNECTION_MSG_ITER_HOLD_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service,
305 + VCHI_MSG_ITER_T *iter,
306 + void **msg_handle );
308 +// Routine to transmit bulk data
309 +typedef int32_t (*VCHI_CONNECTION_BULK_QUEUE_TRANSMIT_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
310 + const void *data_src,
311 + uint32_t data_size,
312 + VCHI_FLAGS_T flags,
313 + void *bulk_handle );
315 +// Routine to receive data
316 +typedef int32_t (*VCHI_CONNECTION_BULK_QUEUE_RECEIVE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
318 + uint32_t data_size,
319 + VCHI_FLAGS_T flags,
320 + void *bulk_handle );
322 +// Routine to report if a server is available
323 +typedef int32_t (*VCHI_CONNECTION_SERVER_PRESENT)( VCHI_CONNECTION_STATE_T *state, int32_t service_id, int32_t peer_flags );
325 +// Routine to report the number of RX slots available
326 +typedef int (*VCHI_CONNECTION_RX_SLOTS_AVAILABLE)( const VCHI_CONNECTION_STATE_T *state );
328 +// Routine to report the RX slot size
329 +typedef uint32_t (*VCHI_CONNECTION_RX_SLOT_SIZE)( const VCHI_CONNECTION_STATE_T *state );
331 +// Callback to indicate that the other side has added a buffer to the rx bulk DMA FIFO
332 +typedef void (*VCHI_CONNECTION_RX_BULK_BUFFER_ADDED)(VCHI_CONNECTION_STATE_T *state,
335 + MESSAGE_TX_CHANNEL_T channel,
336 + uint32_t channel_params,
337 + uint32_t data_length,
338 + uint32_t data_offset);
340 +// Callback to inform a service that a Xon or Xoff message has been received
341 +typedef void (*VCHI_CONNECTION_FLOW_CONTROL)(VCHI_CONNECTION_STATE_T *state, int32_t service_id, int32_t xoff);
343 +// Callback to inform a service that a server available reply message has been received
344 +typedef void (*VCHI_CONNECTION_SERVER_AVAILABLE_REPLY)(VCHI_CONNECTION_STATE_T *state, int32_t service_id, uint32_t flags);
346 +// Callback to indicate that bulk auxiliary messages have arrived
347 +typedef void (*VCHI_CONNECTION_BULK_AUX_RECEIVED)(VCHI_CONNECTION_STATE_T *state);
349 +// Callback to indicate that bulk auxiliary messages have arrived
350 +typedef void (*VCHI_CONNECTION_BULK_AUX_TRANSMITTED)(VCHI_CONNECTION_STATE_T *state, void *handle);
352 +// Callback with all the connection info you require
353 +typedef void (*VCHI_CONNECTION_INFO)(VCHI_CONNECTION_STATE_T *state, uint32_t protocol_version, uint32_t slot_size, uint32_t num_slots, uint32_t min_bulk_size);
355 +// Callback to inform of a disconnect
356 +typedef void (*VCHI_CONNECTION_DISCONNECT)(VCHI_CONNECTION_STATE_T *state, uint32_t flags);
358 +// Callback to inform of a power control request
359 +typedef void (*VCHI_CONNECTION_POWER_CONTROL)(VCHI_CONNECTION_STATE_T *state, MESSAGE_TX_CHANNEL_T channel, int32_t enable);
361 +// allocate memory suitably aligned for this connection
362 +typedef void * (*VCHI_BUFFER_ALLOCATE)(VCHI_CONNECTION_SERVICE_HANDLE_T service_handle, uint32_t * length);
364 +// free memory allocated by buffer_allocate
365 +typedef void (*VCHI_BUFFER_FREE)(VCHI_CONNECTION_SERVICE_HANDLE_T service_handle, void * address);
368 +/******************************************************************************
369 + System driver struct
370 + *****************************************************************************/
372 +struct opaque_vchi_connection_api_t
374 + // Routine to init the connection
375 + VCHI_CONNECTION_INIT_T init;
377 + // Connection-level CRC control
378 + VCHI_CONNECTION_CRC_CONTROL_T crc_control;
380 + // Routine to connect to or create service
381 + VCHI_CONNECTION_SERVICE_CONNECT_T service_connect;
383 + // Routine to disconnect from a service
384 + VCHI_CONNECTION_SERVICE_DISCONNECT_T service_disconnect;
386 + // Routine to queue a message
387 + VCHI_CONNECTION_SERVICE_QUEUE_MESSAGE_T service_queue_msg;
389 + // scatter-gather (vector) message queue
390 + VCHI_CONNECTION_SERVICE_QUEUE_MESSAGEV_T service_queue_msgv;
392 + // Routine to dequeue a message
393 + VCHI_CONNECTION_SERVICE_DEQUEUE_MESSAGE_T service_dequeue_msg;
395 + // Routine to peek at a message
396 + VCHI_CONNECTION_SERVICE_PEEK_MESSAGE_T service_peek_msg;
398 + // Routine to hold a message
399 + VCHI_CONNECTION_SERVICE_HOLD_MESSAGE_T service_hold_msg;
401 + // Routine to initialise a received message iterator
402 + VCHI_CONNECTION_SERVICE_LOOKAHEAD_MESSAGE_T service_look_ahead_msg;
404 + // Routine to release a message
405 + VCHI_CONNECTION_HELD_MSG_RELEASE_T held_msg_release;
407 + // Routine to get information on a held message
408 + VCHI_CONNECTION_HELD_MSG_INFO_T held_msg_info;
410 + // Routine to check for next message on iterator
411 + VCHI_CONNECTION_MSG_ITER_HAS_NEXT_T msg_iter_has_next;
413 + // Routine to get next message on iterator
414 + VCHI_CONNECTION_MSG_ITER_NEXT_T msg_iter_next;
416 + // Routine to remove the last message returned by iterator
417 + VCHI_CONNECTION_MSG_ITER_REMOVE_T msg_iter_remove;
419 + // Routine to hold the last message returned by iterator
420 + VCHI_CONNECTION_MSG_ITER_HOLD_T msg_iter_hold;
422 + // Routine to transmit bulk data
423 + VCHI_CONNECTION_BULK_QUEUE_TRANSMIT_T bulk_queue_transmit;
425 + // Routine to receive data
426 + VCHI_CONNECTION_BULK_QUEUE_RECEIVE_T bulk_queue_receive;
428 + // Routine to report the available servers
429 + VCHI_CONNECTION_SERVER_PRESENT server_present;
431 + // Routine to report the number of RX slots available
432 + VCHI_CONNECTION_RX_SLOTS_AVAILABLE connection_rx_slots_available;
434 + // Routine to report the RX slot size
435 + VCHI_CONNECTION_RX_SLOT_SIZE connection_rx_slot_size;
437 + // Callback to indicate that the other side has added a buffer to the rx bulk DMA FIFO
438 + VCHI_CONNECTION_RX_BULK_BUFFER_ADDED rx_bulk_buffer_added;
440 + // Callback to inform a service that a Xon or Xoff message has been received
441 + VCHI_CONNECTION_FLOW_CONTROL flow_control;
443 + // Callback to inform a service that a server available reply message has been received
444 + VCHI_CONNECTION_SERVER_AVAILABLE_REPLY server_available_reply;
446 + // Callback to indicate that bulk auxiliary messages have arrived
447 + VCHI_CONNECTION_BULK_AUX_RECEIVED bulk_aux_received;
449 + // Callback to indicate that a bulk auxiliary message has been transmitted
450 + VCHI_CONNECTION_BULK_AUX_TRANSMITTED bulk_aux_transmitted;
452 + // Callback to provide information about the connection
453 + VCHI_CONNECTION_INFO connection_info;
455 + // Callback to notify that peer has requested disconnect
456 + VCHI_CONNECTION_DISCONNECT disconnect;
458 + // Callback to notify that peer has requested power change
459 + VCHI_CONNECTION_POWER_CONTROL power_control;
461 + // allocate memory suitably aligned for this connection
462 + VCHI_BUFFER_ALLOCATE buffer_allocate;
464 + // free memory allocated by buffer_allocate
465 + VCHI_BUFFER_FREE buffer_free;
469 +struct vchi_connection_t {
470 + const VCHI_CONNECTION_API_T *api;
471 + VCHI_CONNECTION_STATE_T *state;
472 +#ifdef VCHI_COARSE_LOCKING
473 + struct semaphore sem;
478 +#endif /* CONNECTION_H_ */
480 +/****************************** End of file **********************************/
481 diff --git a/drivers/misc/vc04_services/interface/vchi/message_drivers/message.h b/drivers/misc/vc04_services/interface/vchi/message_drivers/message.h
483 index 0000000..8b3f767
485 +++ b/drivers/misc/vc04_services/interface/vchi/message_drivers/message.h
488 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
490 + * Redistribution and use in source and binary forms, with or without
491 + * modification, are permitted provided that the following conditions
493 + * 1. Redistributions of source code must retain the above copyright
494 + * notice, this list of conditions, and the following disclaimer,
495 + * without modification.
496 + * 2. Redistributions in binary form must reproduce the above copyright
497 + * notice, this list of conditions and the following disclaimer in the
498 + * documentation and/or other materials provided with the distribution.
499 + * 3. The names of the above-listed copyright holders may not be used
500 + * to endorse or promote products derived from this software without
501 + * specific prior written permission.
503 + * ALTERNATIVELY, this software may be distributed under the terms of the
504 + * GNU General Public License ("GPL") version 2, as published by the Free
505 + * Software Foundation.
507 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
508 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
509 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
510 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
511 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
512 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
513 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
514 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
515 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
516 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
517 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
520 +#ifndef _VCHI_MESSAGE_H_
521 +#define _VCHI_MESSAGE_H_
523 +#include <linux/kernel.h>
524 +#include <linux/types.h>
525 +#include <linux/semaphore.h>
527 +#include "interface/vchi/vchi_cfg_internal.h"
528 +#include "interface/vchi/vchi_common.h"
531 +typedef enum message_event_type {
532 + MESSAGE_EVENT_NONE,
534 + MESSAGE_EVENT_MESSAGE,
535 + MESSAGE_EVENT_SLOT_COMPLETE,
536 + MESSAGE_EVENT_RX_BULK_PAUSED,
537 + MESSAGE_EVENT_RX_BULK_COMPLETE,
538 + MESSAGE_EVENT_TX_COMPLETE,
539 + MESSAGE_EVENT_MSG_DISCARDED
540 +} MESSAGE_EVENT_TYPE_T;
542 +typedef enum vchi_msg_flags
544 + VCHI_MSG_FLAGS_NONE = 0x0,
545 + VCHI_MSG_FLAGS_TERMINATE_DMA = 0x1
548 +typedef enum message_tx_channel
550 + MESSAGE_TX_CHANNEL_MESSAGE = 0,
551 + MESSAGE_TX_CHANNEL_BULK = 1 // drivers may provide multiple bulk channels, from 1 upwards
552 +} MESSAGE_TX_CHANNEL_T;
554 +// Macros used for cycling through bulk channels
555 +#define MESSAGE_TX_CHANNEL_BULK_PREV(c) (MESSAGE_TX_CHANNEL_BULK+((c)-MESSAGE_TX_CHANNEL_BULK+VCHI_MAX_BULK_TX_CHANNELS_PER_CONNECTION-1)%VCHI_MAX_BULK_TX_CHANNELS_PER_CONNECTION)
556 +#define MESSAGE_TX_CHANNEL_BULK_NEXT(c) (MESSAGE_TX_CHANNEL_BULK+((c)-MESSAGE_TX_CHANNEL_BULK+1)%VCHI_MAX_BULK_TX_CHANNELS_PER_CONNECTION)
558 +typedef enum message_rx_channel
560 + MESSAGE_RX_CHANNEL_MESSAGE = 0,
561 + MESSAGE_RX_CHANNEL_BULK = 1 // drivers may provide multiple bulk channels, from 1 upwards
562 +} MESSAGE_RX_CHANNEL_T;
564 +// Message receive slot information
565 +typedef struct rx_msg_slot_info {
567 + struct rx_msg_slot_info *next;
568 + //struct slot_info *prev;
569 +#if !defined VCHI_COARSE_LOCKING
570 + struct semaphore sem;
573 + uint8_t *addr; // base address of slot
574 + uint32_t len; // length of slot in bytes
576 + uint32_t write_ptr; // hardware causes this to advance
577 + uint32_t read_ptr; // this module does the reading
578 + int active; // is this slot in the hardware dma fifo?
579 + uint32_t msgs_parsed; // count how many messages are in this slot
580 + uint32_t msgs_released; // how many messages have been released
581 + void *state; // connection state information
582 + uint8_t ref_count[VCHI_MAX_SERVICES_PER_CONNECTION]; // reference count for slots held by services
583 +} RX_MSG_SLOTINFO_T;
585 +// The message driver no longer needs to know about the fields of RX_BULK_SLOTINFO_T - sort this out.
586 +// In particular, it mustn't use addr and len - they're the client buffer, but the message
587 +// driver will be tasked with sending the aligned core section.
588 +typedef struct rx_bulk_slotinfo_t {
589 + struct rx_bulk_slotinfo_t *next;
591 + struct semaphore *blocking;
597 + // needed for the callback
600 + VCHI_FLAGS_T flags;
601 +} RX_BULK_SLOTINFO_T;
604 +/* ----------------------------------------------------------------------
605 + * each connection driver will have a pool of the following struct.
607 + * the pool will be managed by vchi_qman_*
608 + * this means there will be multiple queues (single linked lists)
609 + * a given struct message_info will be on exactly one of these queues
611 + * -------------------------------------------------------------------- */
612 +typedef struct rx_message_info {
614 + struct message_info *next;
615 + //struct message_info *prev;
619 + RX_MSG_SLOTINFO_T *slot; // points to whichever slot contains this message
620 + uint32_t tx_timestamp;
621 + uint32_t rx_timestamp;
623 +} RX_MESSAGE_INFO_T;
626 + MESSAGE_EVENT_TYPE_T type;
630 + void *addr; // address of message
631 + uint16_t slot_delta; // whether this message indicated slot delta
632 + uint32_t len; // length of message
633 + RX_MSG_SLOTINFO_T *slot; // slot this message is in
634 + int32_t service; // service id this message is destined for
635 + uint32_t tx_timestamp; // timestamp from the header
636 + uint32_t rx_timestamp; // timestamp when we parsed it
639 + // FIXME: cleanup slot reporting...
640 + RX_MSG_SLOTINFO_T *rx_msg;
641 + RX_BULK_SLOTINFO_T *rx_bulk;
643 + MESSAGE_TX_CHANNEL_T tx_channel;
649 +typedef void VCHI_MESSAGE_DRIVER_EVENT_CALLBACK_T( void *state );
652 + VCHI_MESSAGE_DRIVER_EVENT_CALLBACK_T *event_callback;
653 +} VCHI_MESSAGE_DRIVER_OPEN_T;
656 +// handle to this instance of message driver (as returned by ->open)
657 +typedef struct opaque_mhandle_t *VCHI_MDRIVER_HANDLE_T;
659 +struct opaque_vchi_message_driver_t {
660 + VCHI_MDRIVER_HANDLE_T *(*open)( VCHI_MESSAGE_DRIVER_OPEN_T *params, void *state );
661 + int32_t (*suspending)( VCHI_MDRIVER_HANDLE_T *handle );
662 + int32_t (*resumed)( VCHI_MDRIVER_HANDLE_T *handle );
663 + int32_t (*power_control)( VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_TX_CHANNEL_T, int32_t enable );
664 + int32_t (*add_msg_rx_slot)( VCHI_MDRIVER_HANDLE_T *handle, RX_MSG_SLOTINFO_T *slot ); // rx message
665 + int32_t (*add_bulk_rx)( VCHI_MDRIVER_HANDLE_T *handle, void *data, uint32_t len, RX_BULK_SLOTINFO_T *slot ); // rx data (bulk)
666 + int32_t (*send)( VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_TX_CHANNEL_T channel, const void *data, uint32_t len, VCHI_MSG_FLAGS_T flags, void *send_handle ); // tx (message & bulk)
667 + void (*next_event)( VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_EVENT_T *event ); // get the next event from message_driver
668 + int32_t (*enable)( VCHI_MDRIVER_HANDLE_T *handle );
669 + int32_t (*form_message)( VCHI_MDRIVER_HANDLE_T *handle, int32_t service_id, VCHI_MSG_VECTOR_T *vector, uint32_t count, void
670 + *address, uint32_t length_avail, uint32_t max_total_length, int32_t pad_to_fill, int32_t allow_partial );
672 + int32_t (*update_message)( VCHI_MDRIVER_HANDLE_T *handle, void *dest, int16_t *slot_count );
673 + int32_t (*buffer_aligned)( VCHI_MDRIVER_HANDLE_T *handle, int tx, int uncached, const void *address, const uint32_t length );
674 + void * (*allocate_buffer)( VCHI_MDRIVER_HANDLE_T *handle, uint32_t *length );
675 + void (*free_buffer)( VCHI_MDRIVER_HANDLE_T *handle, void *address );
676 + int (*rx_slot_size)( VCHI_MDRIVER_HANDLE_T *handle, int msg_size );
677 + int (*tx_slot_size)( VCHI_MDRIVER_HANDLE_T *handle, int msg_size );
679 + int32_t (*tx_supports_terminate)( const VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_TX_CHANNEL_T channel );
680 + uint32_t (*tx_bulk_chunk_size)( const VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_TX_CHANNEL_T channel );
681 + int (*tx_alignment)( const VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_TX_CHANNEL_T channel );
682 + int (*rx_alignment)( const VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_RX_CHANNEL_T channel );
683 + void (*form_bulk_aux)( VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_TX_CHANNEL_T channel, const void *data, uint32_t len, uint32_t chunk_size, const void **aux_data, int32_t *aux_len );
684 + void (*debug)( VCHI_MDRIVER_HANDLE_T *handle );
688 +#endif // _VCHI_MESSAGE_H_
690 +/****************************** End of file ***********************************/
691 diff --git a/drivers/misc/vc04_services/interface/vchi/vchi.h b/drivers/misc/vc04_services/interface/vchi/vchi.h
693 index 0000000..cee5291
695 +++ b/drivers/misc/vc04_services/interface/vchi/vchi.h
698 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
700 + * Redistribution and use in source and binary forms, with or without
701 + * modification, are permitted provided that the following conditions
703 + * 1. Redistributions of source code must retain the above copyright
704 + * notice, this list of conditions, and the following disclaimer,
705 + * without modification.
706 + * 2. Redistributions in binary form must reproduce the above copyright
707 + * notice, this list of conditions and the following disclaimer in the
708 + * documentation and/or other materials provided with the distribution.
709 + * 3. The names of the above-listed copyright holders may not be used
710 + * to endorse or promote products derived from this software without
711 + * specific prior written permission.
713 + * ALTERNATIVELY, this software may be distributed under the terms of the
714 + * GNU General Public License ("GPL") version 2, as published by the Free
715 + * Software Foundation.
717 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
718 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
719 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
720 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
721 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
722 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
723 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
724 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
725 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
726 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
727 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
733 +#include "interface/vchi/vchi_cfg.h"
734 +#include "interface/vchi/vchi_common.h"
735 +#include "interface/vchi/connections/connection.h"
736 +#include "vchi_mh.h"
739 +/******************************************************************************
741 + *****************************************************************************/
743 +#define VCHI_BULK_ROUND_UP(x) ((((unsigned long)(x))+VCHI_BULK_ALIGN-1) & ~(VCHI_BULK_ALIGN-1))
744 +#define VCHI_BULK_ROUND_DOWN(x) (((unsigned long)(x)) & ~(VCHI_BULK_ALIGN-1))
745 +#define VCHI_BULK_ALIGN_NBYTES(x) (VCHI_BULK_ALIGNED(x) ? 0 : (VCHI_BULK_ALIGN - ((unsigned long)(x) & (VCHI_BULK_ALIGN-1))))
747 +#ifdef USE_VCHIQ_ARM
748 +#define VCHI_BULK_ALIGNED(x) 1
750 +#define VCHI_BULK_ALIGNED(x) (((unsigned long)(x) & (VCHI_BULK_ALIGN-1)) == 0)
753 +struct vchi_version {
755 + uint32_t version_min;
757 +#define VCHI_VERSION(v_) { v_, v_ }
758 +#define VCHI_VERSION_EX(v_, m_) { v_, m_ }
765 +} VCHI_MSG_VECTOR_TYPE_T;
767 +typedef struct vchi_msg_vector_ex {
769 + VCHI_MSG_VECTOR_TYPE_T type;
775 + VCHI_MEM_HANDLE_T handle;
780 + // an ordinary data pointer
783 + const void *vec_base;
787 + // a nested vector list
790 + struct vchi_msg_vector_ex *vec;
794 +} VCHI_MSG_VECTOR_EX_T;
797 +// Construct an entry in a msg vector for a pointer (p) of length (l)
798 +#define VCHI_VEC_POINTER(p,l) VCHI_VEC_POINTER, { { (VCHI_MEM_HANDLE_T)(p), (l) } }
800 +// Construct an entry in a msg vector for a message handle (h), starting at offset (o) of length (l)
801 +#define VCHI_VEC_HANDLE(h,o,l) VCHI_VEC_HANDLE, { { (h), (o), (l) } }
803 +// Macros to manipulate 'FOURCC' values
804 +#define MAKE_FOURCC(x) ((int32_t)( (x[0] << 24) | (x[1] << 16) | (x[2] << 8) | x[3] ))
805 +#define FOURCC_TO_CHAR(x) (x >> 24) & 0xFF,(x >> 16) & 0xFF,(x >> 8) & 0xFF, x & 0xFF
808 +// Opaque service information
809 +struct opaque_vchi_service_t;
811 +// Descriptor for a held message. Allocated by client, initialised by vchi_msg_hold,
812 +// vchi_msg_iter_hold or vchi_msg_iter_hold_next. Fields are for internal VCHI use only.
815 + struct opaque_vchi_service_t *service;
821 +// structure used to provide the information needed to open a server or a client
823 + struct vchi_version version;
824 + int32_t service_id;
825 + VCHI_CONNECTION_T *connection;
826 + uint32_t rx_fifo_size;
827 + uint32_t tx_fifo_size;
828 + VCHI_CALLBACK_T callback;
829 + void *callback_param;
830 + /* client intends to receive bulk transfers of
831 + odd lengths or into unaligned buffers */
832 + int32_t want_unaligned_bulk_rx;
833 + /* client intends to transmit bulk transfers of
834 + odd lengths or out of unaligned buffers */
835 + int32_t want_unaligned_bulk_tx;
836 + /* client wants to check CRCs on (bulk) xfers.
837 + Only needs to be set at 1 end - will do both directions. */
839 +} SERVICE_CREATION_T;
841 +// Opaque handle for a VCHI instance
842 +typedef struct opaque_vchi_instance_handle_t *VCHI_INSTANCE_T;
844 +// Opaque handle for a server or client
845 +typedef struct opaque_vchi_service_handle_t *VCHI_SERVICE_HANDLE_T;
847 +// Service registration & startup
848 +typedef void (*VCHI_SERVICE_INIT)(VCHI_INSTANCE_T initialise_instance, VCHI_CONNECTION_T **connections, uint32_t num_connections);
850 +typedef struct service_info_tag {
851 + const char * const vll_filename; /* VLL to load to start this service. This is an empty string if VLL is "static" */
852 + VCHI_SERVICE_INIT init; /* Service initialisation function */
853 + void *vll_handle; /* VLL handle; NULL when unloaded or a "static VLL" in build */
856 +/******************************************************************************
857 + Global funcs - implementation is specific to which side you are on (local / remote)
858 + *****************************************************************************/
864 +extern /*@observer@*/ VCHI_CONNECTION_T * vchi_create_connection( const VCHI_CONNECTION_API_T * function_table,
865 + const VCHI_MESSAGE_DRIVER_T * low_level);
868 +// Routine used to initialise the vchi on both local + remote connections
869 +extern int32_t vchi_initialise( VCHI_INSTANCE_T *instance_handle );
871 +extern int32_t vchi_exit( void );
873 +extern int32_t vchi_connect( VCHI_CONNECTION_T **connections,
874 + const uint32_t num_connections,
875 + VCHI_INSTANCE_T instance_handle );
877 +//When this is called, ensure that all services have no data pending.
878 +//Bulk transfers can remain 'queued'
879 +extern int32_t vchi_disconnect( VCHI_INSTANCE_T instance_handle );
881 +// Global control over bulk CRC checking
882 +extern int32_t vchi_crc_control( VCHI_CONNECTION_T *connection,
883 + VCHI_CRC_CONTROL_T control );
886 +extern void * vchi_allocate_buffer(VCHI_SERVICE_HANDLE_T handle, uint32_t *length);
887 +extern void vchi_free_buffer(VCHI_SERVICE_HANDLE_T handle, void *address);
888 +extern uint32_t vchi_current_time(VCHI_INSTANCE_T instance_handle);
891 +/******************************************************************************
893 + *****************************************************************************/
894 +// Routine to create a named service
895 +extern int32_t vchi_service_create( VCHI_INSTANCE_T instance_handle,
896 + SERVICE_CREATION_T *setup,
897 + VCHI_SERVICE_HANDLE_T *handle );
899 +// Routine to destory a service
900 +extern int32_t vchi_service_destroy( const VCHI_SERVICE_HANDLE_T handle );
902 +// Routine to open a named service
903 +extern int32_t vchi_service_open( VCHI_INSTANCE_T instance_handle,
904 + SERVICE_CREATION_T *setup,
905 + VCHI_SERVICE_HANDLE_T *handle);
907 +extern int32_t vchi_get_peer_version( const VCHI_SERVICE_HANDLE_T handle,
908 + short *peer_version );
910 +// Routine to close a named service
911 +extern int32_t vchi_service_close( const VCHI_SERVICE_HANDLE_T handle );
913 +// Routine to increment ref count on a named service
914 +extern int32_t vchi_service_use( const VCHI_SERVICE_HANDLE_T handle );
916 +// Routine to decrement ref count on a named service
917 +extern int32_t vchi_service_release( const VCHI_SERVICE_HANDLE_T handle );
919 +// Routine to send a message accross a service
920 +extern int32_t vchi_msg_queue( VCHI_SERVICE_HANDLE_T handle,
922 + uint32_t data_size,
923 + VCHI_FLAGS_T flags,
924 + void *msg_handle );
926 +// scatter-gather (vector) and send message
927 +int32_t vchi_msg_queuev_ex( VCHI_SERVICE_HANDLE_T handle,
928 + VCHI_MSG_VECTOR_EX_T *vector,
930 + VCHI_FLAGS_T flags,
931 + void *msg_handle );
933 +// legacy scatter-gather (vector) and send message, only handles pointers
934 +int32_t vchi_msg_queuev( VCHI_SERVICE_HANDLE_T handle,
935 + VCHI_MSG_VECTOR_T *vector,
937 + VCHI_FLAGS_T flags,
938 + void *msg_handle );
940 +// Routine to receive a msg from a service
941 +// Dequeue is equivalent to hold, copy into client buffer, release
942 +extern int32_t vchi_msg_dequeue( VCHI_SERVICE_HANDLE_T handle,
944 + uint32_t max_data_size_to_read,
945 + uint32_t *actual_msg_size,
946 + VCHI_FLAGS_T flags );
948 +// Routine to look at a message in place.
949 +// The message is not dequeued, so a subsequent call to peek or dequeue
950 +// will return the same message.
951 +extern int32_t vchi_msg_peek( VCHI_SERVICE_HANDLE_T handle,
953 + uint32_t *msg_size,
954 + VCHI_FLAGS_T flags );
956 +// Routine to remove a message after it has been read in place with peek
957 +// The first message on the queue is dequeued.
958 +extern int32_t vchi_msg_remove( VCHI_SERVICE_HANDLE_T handle );
960 +// Routine to look at a message in place.
961 +// The message is dequeued, so the caller is left holding it; the descriptor is
962 +// filled in and must be released when the user has finished with the message.
963 +extern int32_t vchi_msg_hold( VCHI_SERVICE_HANDLE_T handle,
964 + void **data, // } may be NULL, as info can be
965 + uint32_t *msg_size, // } obtained from HELD_MSG_T
966 + VCHI_FLAGS_T flags,
967 + VCHI_HELD_MSG_T *message_descriptor );
969 +// Initialise an iterator to look through messages in place
970 +extern int32_t vchi_msg_look_ahead( VCHI_SERVICE_HANDLE_T handle,
971 + VCHI_MSG_ITER_T *iter,
972 + VCHI_FLAGS_T flags );
974 +/******************************************************************************
975 + Global service support API - operations on held messages and message iterators
976 + *****************************************************************************/
978 +// Routine to get the address of a held message
979 +extern void *vchi_held_msg_ptr( const VCHI_HELD_MSG_T *message );
981 +// Routine to get the size of a held message
982 +extern int32_t vchi_held_msg_size( const VCHI_HELD_MSG_T *message );
984 +// Routine to get the transmit timestamp as written into the header by the peer
985 +extern uint32_t vchi_held_msg_tx_timestamp( const VCHI_HELD_MSG_T *message );
987 +// Routine to get the reception timestamp, written as we parsed the header
988 +extern uint32_t vchi_held_msg_rx_timestamp( const VCHI_HELD_MSG_T *message );
990 +// Routine to release a held message after it has been processed
991 +extern int32_t vchi_held_msg_release( VCHI_HELD_MSG_T *message );
993 +// Indicates whether the iterator has a next message.
994 +extern int32_t vchi_msg_iter_has_next( const VCHI_MSG_ITER_T *iter );
996 +// Return the pointer and length for the next message and advance the iterator.
997 +extern int32_t vchi_msg_iter_next( VCHI_MSG_ITER_T *iter,
999 + uint32_t *msg_size );
1001 +// Remove the last message returned by vchi_msg_iter_next.
1002 +// Can only be called once after each call to vchi_msg_iter_next.
1003 +extern int32_t vchi_msg_iter_remove( VCHI_MSG_ITER_T *iter );
1005 +// Hold the last message returned by vchi_msg_iter_next.
1006 +// Can only be called once after each call to vchi_msg_iter_next.
1007 +extern int32_t vchi_msg_iter_hold( VCHI_MSG_ITER_T *iter,
1008 + VCHI_HELD_MSG_T *message );
1010 +// Return information for the next message, and hold it, advancing the iterator.
1011 +extern int32_t vchi_msg_iter_hold_next( VCHI_MSG_ITER_T *iter,
1012 + void **data, // } may be NULL
1013 + uint32_t *msg_size, // }
1014 + VCHI_HELD_MSG_T *message );
1017 +/******************************************************************************
1019 + *****************************************************************************/
1021 +// Routine to prepare interface for a transfer from the other side
1022 +extern int32_t vchi_bulk_queue_receive( VCHI_SERVICE_HANDLE_T handle,
1024 + uint32_t data_size,
1025 + VCHI_FLAGS_T flags,
1026 + void *transfer_handle );
1029 +// Prepare interface for a transfer from the other side into relocatable memory.
1030 +int32_t vchi_bulk_queue_receive_reloc( const VCHI_SERVICE_HANDLE_T handle,
1031 + VCHI_MEM_HANDLE_T h_dst,
1033 + uint32_t data_size,
1034 + const VCHI_FLAGS_T flags,
1035 + void * const bulk_handle );
1037 +// Routine to queue up data ready for transfer to the other (once they have signalled they are ready)
1038 +extern int32_t vchi_bulk_queue_transmit( VCHI_SERVICE_HANDLE_T handle,
1039 + const void *data_src,
1040 + uint32_t data_size,
1041 + VCHI_FLAGS_T flags,
1042 + void *transfer_handle );
1045 +/******************************************************************************
1046 + Configuration plumbing
1047 + *****************************************************************************/
1049 +// function prototypes for the different mid layers (the state info gives the different physical connections)
1050 +extern const VCHI_CONNECTION_API_T *single_get_func_table( void );
1051 +//extern const VCHI_CONNECTION_API_T *local_server_get_func_table( void );
1052 +//extern const VCHI_CONNECTION_API_T *local_client_get_func_table( void );
1054 +// declare all message drivers here
1055 +const VCHI_MESSAGE_DRIVER_T *vchi_mphi_message_driver_func_table( void );
1061 +extern int32_t vchi_bulk_queue_transmit_reloc( VCHI_SERVICE_HANDLE_T handle,
1062 + VCHI_MEM_HANDLE_T h_src,
1064 + uint32_t data_size,
1065 + VCHI_FLAGS_T flags,
1066 + void *transfer_handle );
1067 +#endif /* VCHI_H_ */
1069 +/****************************** End of file **********************************/
1070 diff --git a/drivers/misc/vc04_services/interface/vchi/vchi_cfg.h b/drivers/misc/vc04_services/interface/vchi/vchi_cfg.h
1071 new file mode 100644
1072 index 0000000..26bc2d3
1074 +++ b/drivers/misc/vc04_services/interface/vchi/vchi_cfg.h
1077 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
1079 + * Redistribution and use in source and binary forms, with or without
1080 + * modification, are permitted provided that the following conditions
1082 + * 1. Redistributions of source code must retain the above copyright
1083 + * notice, this list of conditions, and the following disclaimer,
1084 + * without modification.
1085 + * 2. Redistributions in binary form must reproduce the above copyright
1086 + * notice, this list of conditions and the following disclaimer in the
1087 + * documentation and/or other materials provided with the distribution.
1088 + * 3. The names of the above-listed copyright holders may not be used
1089 + * to endorse or promote products derived from this software without
1090 + * specific prior written permission.
1092 + * ALTERNATIVELY, this software may be distributed under the terms of the
1093 + * GNU General Public License ("GPL") version 2, as published by the Free
1094 + * Software Foundation.
1096 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
1097 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
1098 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1099 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
1100 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
1101 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
1102 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
1103 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
1104 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
1105 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
1106 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1109 +#ifndef VCHI_CFG_H_
1110 +#define VCHI_CFG_H_
1112 +/****************************************************************************************
1113 + * Defines in this first section are part of the VCHI API and may be examined by VCHI
1115 + ***************************************************************************************/
1117 +/* Required alignment of base addresses for bulk transfer, if unaligned transfers are not enabled */
1118 +/* Really determined by the message driver, and should be available from a run-time call. */
1119 +#ifndef VCHI_BULK_ALIGN
1120 +# if __VCCOREVER__ >= 0x04000000
1121 +# define VCHI_BULK_ALIGN 32 // Allows for the need to do cache cleans
1123 +# define VCHI_BULK_ALIGN 16
1127 +/* Required length multiple for bulk transfers, if unaligned transfers are not enabled */
1128 +/* May be less than or greater than VCHI_BULK_ALIGN */
1129 +/* Really determined by the message driver, and should be available from a run-time call. */
1130 +#ifndef VCHI_BULK_GRANULARITY
1131 +# if __VCCOREVER__ >= 0x04000000
1132 +# define VCHI_BULK_GRANULARITY 32 // Allows for the need to do cache cleans
1134 +# define VCHI_BULK_GRANULARITY 16
1138 +/* The largest possible message to be queued with vchi_msg_queue. */
1139 +#ifndef VCHI_MAX_MSG_SIZE
1140 +# if defined VCHI_LOCAL_HOST_PORT
1141 +# define VCHI_MAX_MSG_SIZE 16384 // makes file transfers fast, but should they be using bulk?
1143 +# define VCHI_MAX_MSG_SIZE 4096 // NOTE: THIS MUST BE LARGER THAN OR EQUAL TO THE SIZE OF THE KHRONOS MERGE BUFFER!!
1147 +/******************************************************************************************
1148 + * Defines below are system configuration options, and should not be used by VCHI services.
1149 + *****************************************************************************************/
1151 +/* How many connections can we support? A localhost implementation uses 2 connections,
1152 + * 1 for host-app, 1 for VMCS, and these are hooked together by a loopback MPHI VCFW
1154 +#ifndef VCHI_MAX_NUM_CONNECTIONS
1155 +# define VCHI_MAX_NUM_CONNECTIONS 3
1158 +/* How many services can we open per connection? Extending this doesn't cost processing time, just a small
1159 + * amount of static memory. */
1160 +#ifndef VCHI_MAX_SERVICES_PER_CONNECTION
1161 +# define VCHI_MAX_SERVICES_PER_CONNECTION 36
1164 +/* Adjust if using a message driver that supports more logical TX channels */
1165 +#ifndef VCHI_MAX_BULK_TX_CHANNELS_PER_CONNECTION
1166 +# define VCHI_MAX_BULK_TX_CHANNELS_PER_CONNECTION 9 // 1 MPHI + 8 CCP2 logical channels
1169 +/* Adjust if using a message driver that supports more logical RX channels */
1170 +#ifndef VCHI_MAX_BULK_RX_CHANNELS_PER_CONNECTION
1171 +# define VCHI_MAX_BULK_RX_CHANNELS_PER_CONNECTION 1 // 1 MPHI
1174 +/* How many receive slots do we use. This times VCHI_MAX_MSG_SIZE gives the effective
1175 + * receive queue space, less message headers. */
1176 +#ifndef VCHI_NUM_READ_SLOTS
1177 +# if defined(VCHI_LOCAL_HOST_PORT)
1178 +# define VCHI_NUM_READ_SLOTS 4
1180 +# define VCHI_NUM_READ_SLOTS 48
1184 +/* Do we utilise overrun facility for receive message slots? Can aid peer transmit
1185 + * performance. Only define on VideoCore end, talking to host.
1187 +//#define VCHI_MSG_RX_OVERRUN
1189 +/* How many transmit slots do we use. Generally don't need many, as the hardware driver
1190 + * underneath VCHI will usually have its own buffering. */
1191 +#ifndef VCHI_NUM_WRITE_SLOTS
1192 +# define VCHI_NUM_WRITE_SLOTS 4
1195 +/* If a service has held or queued received messages in VCHI_XOFF_THRESHOLD or more slots,
1196 + * then it's taking up too much buffer space, and the peer service will be told to stop
1197 + * transmitting with an XOFF message. For this to be effective, the VCHI_NUM_READ_SLOTS
1198 + * needs to be considerably bigger than VCHI_NUM_WRITE_SLOTS, or the transmit latency
1200 +#ifndef VCHI_XOFF_THRESHOLD
1201 +# define VCHI_XOFF_THRESHOLD (VCHI_NUM_READ_SLOTS / 2)
1204 +/* After we've sent an XOFF, the peer will be told to resume transmission once the local
1205 + * service has dequeued/released enough messages that it's now occupying
1206 + * VCHI_XON_THRESHOLD slots or fewer. */
1207 +#ifndef VCHI_XON_THRESHOLD
1208 +# define VCHI_XON_THRESHOLD (VCHI_NUM_READ_SLOTS / 4)
1211 +/* A size below which a bulk transfer omits the handshake completely and always goes
1212 + * via the message channel, if bulk auxiliary is being sent on that service. (The user
1213 + * can guarantee this by enabling unaligned transmits).
1215 +#ifndef VCHI_MIN_BULK_SIZE
1216 +# define VCHI_MIN_BULK_SIZE ( VCHI_MAX_MSG_SIZE / 2 < 4096 ? VCHI_MAX_MSG_SIZE / 2 : 4096 )
1219 +/* Maximum size of bulk transmission chunks, for each interface type. A trade-off between
1220 + * speed and latency; the smaller the chunk size the better change of messages and other
1221 + * bulk transmissions getting in when big bulk transfers are happening. Set to 0 to not
1222 + * break transmissions into chunks.
1224 +#ifndef VCHI_MAX_BULK_CHUNK_SIZE_MPHI
1225 +# define VCHI_MAX_BULK_CHUNK_SIZE_MPHI (16 * 1024)
1228 +/* NB Chunked CCP2 transmissions violate the letter of the CCP2 spec by using "JPEG8" mode
1229 + * with multiple-line frames. Only use if the receiver can cope. */
1230 +#ifndef VCHI_MAX_BULK_CHUNK_SIZE_CCP2
1231 +# define VCHI_MAX_BULK_CHUNK_SIZE_CCP2 0
1234 +/* How many TX messages can we have pending in our transmit slots. Once exhausted,
1235 + * vchi_msg_queue will be blocked. */
1236 +#ifndef VCHI_TX_MSG_QUEUE_SIZE
1237 +# define VCHI_TX_MSG_QUEUE_SIZE 256
1240 +/* How many RX messages can we have parsed in the receive slots. Once exhausted, parsing
1241 + * will be suspended until older messages are dequeued/released. */
1242 +#ifndef VCHI_RX_MSG_QUEUE_SIZE
1243 +# define VCHI_RX_MSG_QUEUE_SIZE 256
1246 +/* Really should be able to cope if we run out of received message descriptors, by
1247 + * suspending parsing as the comment above says, but we don't. This sweeps the issue
1248 + * under the carpet. */
1249 +#if VCHI_RX_MSG_QUEUE_SIZE < (VCHI_MAX_MSG_SIZE/16 + 1) * VCHI_NUM_READ_SLOTS
1250 +# undef VCHI_RX_MSG_QUEUE_SIZE
1251 +# define VCHI_RX_MSG_QUEUE_SIZE (VCHI_MAX_MSG_SIZE/16 + 1) * VCHI_NUM_READ_SLOTS
1254 +/* How many bulk transmits can we have pending. Once exhausted, vchi_bulk_queue_transmit
1255 + * will be blocked. */
1256 +#ifndef VCHI_TX_BULK_QUEUE_SIZE
1257 +# define VCHI_TX_BULK_QUEUE_SIZE 64
1260 +/* How many bulk receives can we have pending. Once exhausted, vchi_bulk_queue_receive
1261 + * will be blocked. */
1262 +#ifndef VCHI_RX_BULK_QUEUE_SIZE
1263 +# define VCHI_RX_BULK_QUEUE_SIZE 64
1266 +/* A limit on how many outstanding bulk requests we expect the peer to give us. If
1267 + * the peer asks for more than this, VCHI will fail and assert. The number is determined
1268 + * by the peer's hardware - it's the number of outstanding requests that can be queued
1269 + * on all bulk channels. VC3's MPHI peripheral allows 16. */
1270 +#ifndef VCHI_MAX_PEER_BULK_REQUESTS
1271 +# define VCHI_MAX_PEER_BULK_REQUESTS 32
1274 +/* Define VCHI_CCP2TX_MANUAL_POWER if the host tells us when to turn the CCP2
1275 + * transmitter on and off.
1277 +/*#define VCHI_CCP2TX_MANUAL_POWER*/
1279 +#ifndef VCHI_CCP2TX_MANUAL_POWER
1281 +/* Timeout (in milliseconds) for putting the CCP2TX interface into IDLE state. Set
1282 + * negative for no IDLE.
1284 +# ifndef VCHI_CCP2TX_IDLE_TIMEOUT
1285 +# define VCHI_CCP2TX_IDLE_TIMEOUT 5
1288 +/* Timeout (in milliseconds) for putting the CCP2TX interface into OFF state. Set
1289 + * negative for no OFF.
1291 +# ifndef VCHI_CCP2TX_OFF_TIMEOUT
1292 +# define VCHI_CCP2TX_OFF_TIMEOUT 1000
1295 +#endif /* VCHI_CCP2TX_MANUAL_POWER */
1297 +#endif /* VCHI_CFG_H_ */
1299 +/****************************** End of file **********************************/
1300 diff --git a/drivers/misc/vc04_services/interface/vchi/vchi_cfg_internal.h b/drivers/misc/vc04_services/interface/vchi/vchi_cfg_internal.h
1301 new file mode 100644
1302 index 0000000..35dcba4
1304 +++ b/drivers/misc/vc04_services/interface/vchi/vchi_cfg_internal.h
1307 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
1309 + * Redistribution and use in source and binary forms, with or without
1310 + * modification, are permitted provided that the following conditions
1312 + * 1. Redistributions of source code must retain the above copyright
1313 + * notice, this list of conditions, and the following disclaimer,
1314 + * without modification.
1315 + * 2. Redistributions in binary form must reproduce the above copyright
1316 + * notice, this list of conditions and the following disclaimer in the
1317 + * documentation and/or other materials provided with the distribution.
1318 + * 3. The names of the above-listed copyright holders may not be used
1319 + * to endorse or promote products derived from this software without
1320 + * specific prior written permission.
1322 + * ALTERNATIVELY, this software may be distributed under the terms of the
1323 + * GNU General Public License ("GPL") version 2, as published by the Free
1324 + * Software Foundation.
1326 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
1327 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
1328 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1329 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
1330 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
1331 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
1332 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
1333 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
1334 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
1335 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
1336 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1339 +#ifndef VCHI_CFG_INTERNAL_H_
1340 +#define VCHI_CFG_INTERNAL_H_
1342 +/****************************************************************************************
1343 + * Control optimisation attempts.
1344 + ***************************************************************************************/
1346 +// Don't use lots of short-term locks - use great long ones, reducing the overall locks-per-second
1347 +#define VCHI_COARSE_LOCKING
1349 +// Avoid lock then unlock on exit from blocking queue operations (msg tx, bulk rx/tx)
1350 +// (only relevant if VCHI_COARSE_LOCKING)
1351 +#define VCHI_ELIDE_BLOCK_EXIT_LOCK
1353 +// Avoid lock on non-blocking peek
1354 +// (only relevant if VCHI_COARSE_LOCKING)
1355 +#define VCHI_AVOID_PEEK_LOCK
1357 +// Use one slot-handler thread per connection, rather than 1 thread dealing with all connections in rotation.
1358 +#define VCHI_MULTIPLE_HANDLER_THREADS
1360 +// Put free descriptors onto the head of the free queue, rather than the tail, so that we don't thrash
1361 +// our way through the pool of descriptors.
1362 +#define VCHI_PUSH_FREE_DESCRIPTORS_ONTO_HEAD
1364 +// Don't issue a MSG_AVAILABLE callback for every single message. Possibly only safe if VCHI_COARSE_LOCKING.
1365 +#define VCHI_FEWER_MSG_AVAILABLE_CALLBACKS
1367 +// Don't use message descriptors for TX messages that don't need them
1368 +#define VCHI_MINIMISE_TX_MSG_DESCRIPTORS
1370 +// Nano-locks for multiqueue
1371 +//#define VCHI_MQUEUE_NANOLOCKS
1373 +// Lock-free(er) dequeuing
1374 +//#define VCHI_RX_NANOLOCKS
1376 +#endif /*VCHI_CFG_INTERNAL_H_*/
1377 diff --git a/drivers/misc/vc04_services/interface/vchi/vchi_common.h b/drivers/misc/vc04_services/interface/vchi/vchi_common.h
1378 new file mode 100644
1379 index 0000000..9e6c00e
1381 +++ b/drivers/misc/vc04_services/interface/vchi/vchi_common.h
1384 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
1386 + * Redistribution and use in source and binary forms, with or without
1387 + * modification, are permitted provided that the following conditions
1389 + * 1. Redistributions of source code must retain the above copyright
1390 + * notice, this list of conditions, and the following disclaimer,
1391 + * without modification.
1392 + * 2. Redistributions in binary form must reproduce the above copyright
1393 + * notice, this list of conditions and the following disclaimer in the
1394 + * documentation and/or other materials provided with the distribution.
1395 + * 3. The names of the above-listed copyright holders may not be used
1396 + * to endorse or promote products derived from this software without
1397 + * specific prior written permission.
1399 + * ALTERNATIVELY, this software may be distributed under the terms of the
1400 + * GNU General Public License ("GPL") version 2, as published by the Free
1401 + * Software Foundation.
1403 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
1404 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
1405 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1406 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
1407 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
1408 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
1409 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
1410 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
1411 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
1412 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
1413 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1416 +#ifndef VCHI_COMMON_H_
1417 +#define VCHI_COMMON_H_
1420 +//flags used when sending messages (must be bitmapped)
1423 + VCHI_FLAGS_NONE = 0x0,
1424 + VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE = 0x1, // waits for message to be received, or sent (NB. not the same as being seen on other side)
1425 + VCHI_FLAGS_CALLBACK_WHEN_OP_COMPLETE = 0x2, // run a callback when message sent
1426 + VCHI_FLAGS_BLOCK_UNTIL_QUEUED = 0x4, // return once the transfer is in a queue ready to go
1427 + VCHI_FLAGS_ALLOW_PARTIAL = 0x8,
1428 + VCHI_FLAGS_BLOCK_UNTIL_DATA_READ = 0x10,
1429 + VCHI_FLAGS_CALLBACK_WHEN_DATA_READ = 0x20,
1431 + VCHI_FLAGS_ALIGN_SLOT = 0x000080, // internal use only
1432 + VCHI_FLAGS_BULK_AUX_QUEUED = 0x010000, // internal use only
1433 + VCHI_FLAGS_BULK_AUX_COMPLETE = 0x020000, // internal use only
1434 + VCHI_FLAGS_BULK_DATA_QUEUED = 0x040000, // internal use only
1435 + VCHI_FLAGS_BULK_DATA_COMPLETE = 0x080000, // internal use only
1436 + VCHI_FLAGS_INTERNAL = 0xFF0000
1439 +// constants for vchi_crc_control()
1441 + VCHI_CRC_NOTHING = -1,
1442 + VCHI_CRC_PER_SERVICE = 0,
1443 + VCHI_CRC_EVERYTHING = 1,
1444 +} VCHI_CRC_CONTROL_T;
1446 +//callback reasons when an event occurs on a service
1449 + VCHI_CALLBACK_REASON_MIN,
1451 + //This indicates that there is data available
1452 + //handle is the msg id that was transmitted with the data
1453 + // When a message is received and there was no FULL message available previously, send callback
1454 + // Tasks get kicked by the callback, reset their event and try and read from the fifo until it fails
1455 + VCHI_CALLBACK_MSG_AVAILABLE,
1456 + VCHI_CALLBACK_MSG_SENT,
1457 + VCHI_CALLBACK_MSG_SPACE_AVAILABLE, // XXX not yet implemented
1459 + // This indicates that a transfer from the other side has completed
1460 + VCHI_CALLBACK_BULK_RECEIVED,
1461 + //This indicates that data queued up to be sent has now gone
1462 + //handle is the msg id that was used when sending the data
1463 + VCHI_CALLBACK_BULK_SENT,
1464 + VCHI_CALLBACK_BULK_RX_SPACE_AVAILABLE, // XXX not yet implemented
1465 + VCHI_CALLBACK_BULK_TX_SPACE_AVAILABLE, // XXX not yet implemented
1467 + VCHI_CALLBACK_SERVICE_CLOSED,
1469 + // this side has sent XOFF to peer due to lack of data consumption by service
1470 + // (suggests the service may need to take some recovery action if it has
1471 + // been deliberately holding off consuming data)
1472 + VCHI_CALLBACK_SENT_XOFF,
1473 + VCHI_CALLBACK_SENT_XON,
1475 + // indicates that a bulk transfer has finished reading the source buffer
1476 + VCHI_CALLBACK_BULK_DATA_READ,
1478 + // power notification events (currently host side only)
1479 + VCHI_CALLBACK_PEER_OFF,
1480 + VCHI_CALLBACK_PEER_SUSPENDED,
1481 + VCHI_CALLBACK_PEER_ON,
1482 + VCHI_CALLBACK_PEER_RESUMED,
1483 + VCHI_CALLBACK_FORCED_POWER_OFF,
1485 +#ifdef USE_VCHIQ_ARM
1486 + // some extra notifications provided by vchiq_arm
1487 + VCHI_CALLBACK_SERVICE_OPENED,
1488 + VCHI_CALLBACK_BULK_RECEIVE_ABORTED,
1489 + VCHI_CALLBACK_BULK_TRANSMIT_ABORTED,
1492 + VCHI_CALLBACK_REASON_MAX
1493 +} VCHI_CALLBACK_REASON_T;
1495 +//Calback used by all services / bulk transfers
1496 +typedef void (*VCHI_CALLBACK_T)( void *callback_param, //my service local param
1497 + VCHI_CALLBACK_REASON_T reason,
1498 + void *handle ); //for transmitting msg's only
1503 + * Define vector struct for scatter-gather (vector) operations
1504 + * Vectors can be nested - if a vector element has negative length, then
1505 + * the data pointer is treated as pointing to another vector array, with
1506 + * '-vec_len' elements. Thus to append a header onto an existing vector,
1507 + * you can do this:
1509 + * void foo(const VCHI_MSG_VECTOR_T *v, int n)
1511 + * VCHI_MSG_VECTOR_T nv[2];
1512 + * nv[0].vec_base = my_header;
1513 + * nv[0].vec_len = sizeof my_header;
1514 + * nv[1].vec_base = v;
1515 + * nv[1].vec_len = -n;
1519 +typedef struct vchi_msg_vector {
1520 + const void *vec_base;
1522 +} VCHI_MSG_VECTOR_T;
1524 +// Opaque type for a connection API
1525 +typedef struct opaque_vchi_connection_api_t VCHI_CONNECTION_API_T;
1527 +// Opaque type for a message driver
1528 +typedef struct opaque_vchi_message_driver_t VCHI_MESSAGE_DRIVER_T;
1531 +// Iterator structure for reading ahead through received message queue. Allocated by client,
1532 +// initialised by vchi_msg_look_ahead. Fields are for internal VCHI use only.
1533 +// Iterates over messages in queue at the instant of the call to vchi_msg_lookahead -
1534 +// will not proceed to messages received since. Behaviour is undefined if an iterator
1535 +// is used again after messages for that service are removed/dequeued by any
1536 +// means other than vchi_msg_iter_... calls on the iterator itself.
1538 + struct opaque_vchi_service_t *service;
1545 +#endif // VCHI_COMMON_H_
1546 diff --git a/drivers/misc/vc04_services/interface/vchi/vchi_mh.h b/drivers/misc/vc04_services/interface/vchi/vchi_mh.h
1547 new file mode 100644
1548 index 0000000..198bd07
1550 +++ b/drivers/misc/vc04_services/interface/vchi/vchi_mh.h
1553 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
1555 + * Redistribution and use in source and binary forms, with or without
1556 + * modification, are permitted provided that the following conditions
1558 + * 1. Redistributions of source code must retain the above copyright
1559 + * notice, this list of conditions, and the following disclaimer,
1560 + * without modification.
1561 + * 2. Redistributions in binary form must reproduce the above copyright
1562 + * notice, this list of conditions and the following disclaimer in the
1563 + * documentation and/or other materials provided with the distribution.
1564 + * 3. The names of the above-listed copyright holders may not be used
1565 + * to endorse or promote products derived from this software without
1566 + * specific prior written permission.
1568 + * ALTERNATIVELY, this software may be distributed under the terms of the
1569 + * GNU General Public License ("GPL") version 2, as published by the Free
1570 + * Software Foundation.
1572 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
1573 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
1574 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1575 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
1576 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
1577 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
1578 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
1579 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
1580 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
1581 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
1582 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1588 +#include <linux/types.h>
1590 +typedef int32_t VCHI_MEM_HANDLE_T;
1591 +#define VCHI_MEM_HANDLE_INVALID 0
1594 diff --git a/drivers/misc/vc04_services/interface/vchiq_arm/vchiq.h b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq.h
1595 new file mode 100644
1596 index 0000000..ad398ba
1598 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq.h
1601 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
1603 + * Redistribution and use in source and binary forms, with or without
1604 + * modification, are permitted provided that the following conditions
1606 + * 1. Redistributions of source code must retain the above copyright
1607 + * notice, this list of conditions, and the following disclaimer,
1608 + * without modification.
1609 + * 2. Redistributions in binary form must reproduce the above copyright
1610 + * notice, this list of conditions and the following disclaimer in the
1611 + * documentation and/or other materials provided with the distribution.
1612 + * 3. The names of the above-listed copyright holders may not be used
1613 + * to endorse or promote products derived from this software without
1614 + * specific prior written permission.
1616 + * ALTERNATIVELY, this software may be distributed under the terms of the
1617 + * GNU General Public License ("GPL") version 2, as published by the Free
1618 + * Software Foundation.
1620 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
1621 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
1622 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1623 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
1624 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
1625 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
1626 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
1627 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
1628 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
1629 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
1630 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1633 +#ifndef VCHIQ_VCHIQ_H
1634 +#define VCHIQ_VCHIQ_H
1636 +#include "vchiq_if.h"
1637 +#include "vchiq_util.h"
1640 diff --git a/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835.h b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835.h
1641 new file mode 100644
1642 index 0000000..7ea5c64
1644 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835.h
1647 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
1649 + * Redistribution and use in source and binary forms, with or without
1650 + * modification, are permitted provided that the following conditions
1652 + * 1. Redistributions of source code must retain the above copyright
1653 + * notice, this list of conditions, and the following disclaimer,
1654 + * without modification.
1655 + * 2. Redistributions in binary form must reproduce the above copyright
1656 + * notice, this list of conditions and the following disclaimer in the
1657 + * documentation and/or other materials provided with the distribution.
1658 + * 3. The names of the above-listed copyright holders may not be used
1659 + * to endorse or promote products derived from this software without
1660 + * specific prior written permission.
1662 + * ALTERNATIVELY, this software may be distributed under the terms of the
1663 + * GNU General Public License ("GPL") version 2, as published by the Free
1664 + * Software Foundation.
1666 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
1667 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
1668 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1669 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
1670 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
1671 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
1672 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
1673 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
1674 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
1675 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
1676 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1679 +#ifndef VCHIQ_2835_H
1680 +#define VCHIQ_2835_H
1682 +#include "vchiq_pagelist.h"
1684 +#define VCHIQ_PLATFORM_FRAGMENTS_OFFSET_IDX 0
1685 +#define VCHIQ_PLATFORM_FRAGMENTS_COUNT_IDX 1
1687 +#endif /* VCHIQ_2835_H */
1688 diff --git a/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835_arm.c b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835_arm.c
1689 new file mode 100644
1690 index 0000000..b3bdaa2
1692 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835_arm.c
1695 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
1697 + * Redistribution and use in source and binary forms, with or without
1698 + * modification, are permitted provided that the following conditions
1700 + * 1. Redistributions of source code must retain the above copyright
1701 + * notice, this list of conditions, and the following disclaimer,
1702 + * without modification.
1703 + * 2. Redistributions in binary form must reproduce the above copyright
1704 + * notice, this list of conditions and the following disclaimer in the
1705 + * documentation and/or other materials provided with the distribution.
1706 + * 3. The names of the above-listed copyright holders may not be used
1707 + * to endorse or promote products derived from this software without
1708 + * specific prior written permission.
1710 + * ALTERNATIVELY, this software may be distributed under the terms of the
1711 + * GNU General Public License ("GPL") version 2, as published by the Free
1712 + * Software Foundation.
1714 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
1715 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
1716 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1717 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
1718 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
1719 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
1720 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
1721 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
1722 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
1723 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
1724 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1727 +#include <linux/kernel.h>
1728 +#include <linux/types.h>
1729 +#include <linux/errno.h>
1730 +#include <linux/interrupt.h>
1731 +#include <linux/irq.h>
1732 +#include <linux/pagemap.h>
1733 +#include <linux/dma-mapping.h>
1734 +#include <linux/version.h>
1735 +#include <linux/io.h>
1736 +#include <linux/uaccess.h>
1737 +#include <asm/pgtable.h>
1739 +#include <mach/irqs.h>
1741 +#include <mach/platform.h>
1742 +#include <mach/vcio.h>
1744 +#define TOTAL_SLOTS (VCHIQ_SLOT_ZERO_SLOTS + 2 * 32)
1746 +#define VCHIQ_DOORBELL_IRQ IRQ_ARM_DOORBELL_0
1747 +#define VCHIQ_ARM_ADDRESS(x) ((void *)__virt_to_bus((unsigned)x))
1749 +#include "vchiq_arm.h"
1750 +#include "vchiq_2835.h"
1751 +#include "vchiq_connected.h"
1753 +#define MAX_FRAGMENTS (VCHIQ_NUM_CURRENT_BULKS * 2)
1755 +typedef struct vchiq_2835_state_struct {
1757 + VCHIQ_ARM_STATE_T arm_state;
1758 +} VCHIQ_2835_ARM_STATE_T;
1760 +static char *g_slot_mem;
1761 +static int g_slot_mem_size;
1762 +dma_addr_t g_slot_phys;
1763 +static FRAGMENTS_T *g_fragments_base;
1764 +static FRAGMENTS_T *g_free_fragments;
1765 +struct semaphore g_free_fragments_sema;
1767 +extern int vchiq_arm_log_level;
1769 +static DEFINE_SEMAPHORE(g_free_fragments_mutex);
1772 +vchiq_doorbell_irq(int irq, void *dev_id);
1775 +create_pagelist(char __user *buf, size_t count, unsigned short type,
1776 + struct task_struct *task, PAGELIST_T ** ppagelist);
1779 +free_pagelist(PAGELIST_T *pagelist, int actual);
1782 +vchiq_platform_init(VCHIQ_STATE_T *state)
1784 + VCHIQ_SLOT_ZERO_T *vchiq_slot_zero;
1785 + int frag_mem_size;
1789 + /* Allocate space for the channels in coherent memory */
1790 + g_slot_mem_size = PAGE_ALIGN(TOTAL_SLOTS * VCHIQ_SLOT_SIZE);
1791 + frag_mem_size = PAGE_ALIGN(sizeof(FRAGMENTS_T) * MAX_FRAGMENTS);
1793 + g_slot_mem = dma_alloc_coherent(NULL, g_slot_mem_size + frag_mem_size,
1794 + &g_slot_phys, GFP_ATOMIC);
1796 + if (!g_slot_mem) {
1797 + vchiq_log_error(vchiq_arm_log_level,
1798 + "Unable to allocate channel memory");
1800 + goto failed_alloc;
1803 + WARN_ON(((int)g_slot_mem & (PAGE_SIZE - 1)) != 0);
1805 + vchiq_slot_zero = vchiq_init_slots(g_slot_mem, g_slot_mem_size);
1806 + if (!vchiq_slot_zero) {
1808 + goto failed_init_slots;
1811 + vchiq_slot_zero->platform_data[VCHIQ_PLATFORM_FRAGMENTS_OFFSET_IDX] =
1812 + (int)g_slot_phys + g_slot_mem_size;
1813 + vchiq_slot_zero->platform_data[VCHIQ_PLATFORM_FRAGMENTS_COUNT_IDX] =
1816 + g_fragments_base = (FRAGMENTS_T *)(g_slot_mem + g_slot_mem_size);
1817 + g_slot_mem_size += frag_mem_size;
1819 + g_free_fragments = g_fragments_base;
1820 + for (i = 0; i < (MAX_FRAGMENTS - 1); i++) {
1821 + *(FRAGMENTS_T **)&g_fragments_base[i] =
1822 + &g_fragments_base[i + 1];
1824 + *(FRAGMENTS_T **)&g_fragments_base[i] = NULL;
1825 + sema_init(&g_free_fragments_sema, MAX_FRAGMENTS);
1827 + if (vchiq_init_state(state, vchiq_slot_zero, 0/*slave*/) !=
1830 + goto failed_vchiq_init;
1833 + err = request_irq(VCHIQ_DOORBELL_IRQ, vchiq_doorbell_irq,
1834 + IRQF_IRQPOLL, "VCHIQ doorbell",
1837 + vchiq_log_error(vchiq_arm_log_level, "%s: failed to register "
1838 + "irq=%d err=%d", __func__,
1839 + VCHIQ_DOORBELL_IRQ, err);
1840 + goto failed_request_irq;
1843 + /* Send the base address of the slots to VideoCore */
1845 + dsb(); /* Ensure all writes have completed */
1847 + bcm_mailbox_write(MBOX_CHAN_VCHIQ, (unsigned int)g_slot_phys);
1849 + vchiq_log_info(vchiq_arm_log_level,
1850 + "vchiq_init - done (slots %x, phys %x)",
1851 + (unsigned int)vchiq_slot_zero, g_slot_phys);
1853 + vchiq_call_connected_callbacks();
1857 +failed_request_irq:
1860 + dma_free_coherent(NULL, g_slot_mem_size, g_slot_mem, g_slot_phys);
1867 +vchiq_platform_exit(VCHIQ_STATE_T *state)
1869 + free_irq(VCHIQ_DOORBELL_IRQ, state);
1870 + dma_free_coherent(NULL, g_slot_mem_size,
1871 + g_slot_mem, g_slot_phys);
1876 +vchiq_platform_init_state(VCHIQ_STATE_T *state)
1878 + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
1879 + state->platform_state = kzalloc(sizeof(VCHIQ_2835_ARM_STATE_T), GFP_KERNEL);
1880 + ((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->inited = 1;
1881 + status = vchiq_arm_init_state(state, &((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->arm_state);
1882 + if(status != VCHIQ_SUCCESS)
1884 + ((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->inited = 0;
1890 +vchiq_platform_get_arm_state(VCHIQ_STATE_T *state)
1892 + if(!((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->inited)
1896 + return &((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->arm_state;
1900 +remote_event_signal(REMOTE_EVENT_T *event)
1906 + dsb(); /* data barrier operation */
1908 + if (event->armed) {
1909 + /* trigger vc interrupt */
1911 + writel(0, __io_address(ARM_0_BELL2));
1916 +vchiq_copy_from_user(void *dst, const void *src, int size)
1918 + if ((uint32_t)src < TASK_SIZE) {
1919 + return copy_from_user(dst, src, size);
1921 + memcpy(dst, src, size);
1927 +vchiq_prepare_bulk_data(VCHIQ_BULK_T *bulk, VCHI_MEM_HANDLE_T memhandle,
1928 + void *offset, int size, int dir)
1930 + PAGELIST_T *pagelist;
1933 + WARN_ON(memhandle != VCHI_MEM_HANDLE_INVALID);
1935 + ret = create_pagelist((char __user *)offset, size,
1936 + (dir == VCHIQ_BULK_RECEIVE)
1942 + return VCHIQ_ERROR;
1944 + bulk->handle = memhandle;
1945 + bulk->data = VCHIQ_ARM_ADDRESS(pagelist);
1947 + /* Store the pagelist address in remote_data, which isn't used by the
1949 + bulk->remote_data = pagelist;
1951 + return VCHIQ_SUCCESS;
1955 +vchiq_complete_bulk(VCHIQ_BULK_T *bulk)
1957 + if (bulk && bulk->remote_data && bulk->actual)
1958 + free_pagelist((PAGELIST_T *)bulk->remote_data, bulk->actual);
1962 +vchiq_transfer_bulk(VCHIQ_BULK_T *bulk)
1965 + * This should only be called on the master (VideoCore) side, but
1966 + * provide an implementation to avoid the need for ifdefery.
1972 +vchiq_dump_platform_state(void *dump_context)
1976 + len = snprintf(buf, sizeof(buf),
1977 + " Platform: 2835 (VC master)");
1978 + vchiq_dump(dump_context, buf, len + 1);
1982 +vchiq_platform_suspend(VCHIQ_STATE_T *state)
1984 + return VCHIQ_ERROR;
1988 +vchiq_platform_resume(VCHIQ_STATE_T *state)
1990 + return VCHIQ_SUCCESS;
1994 +vchiq_platform_paused(VCHIQ_STATE_T *state)
1999 +vchiq_platform_resumed(VCHIQ_STATE_T *state)
2004 +vchiq_platform_videocore_wanted(VCHIQ_STATE_T* state)
2006 + return 1; // autosuspend not supported - videocore always wanted
2010 +vchiq_platform_use_suspend_timer(void)
2015 +vchiq_dump_platform_use_state(VCHIQ_STATE_T *state)
2017 + vchiq_log_info((vchiq_arm_log_level>=VCHIQ_LOG_INFO),"Suspend timer not in use");
2020 +vchiq_platform_handle_timeout(VCHIQ_STATE_T *state)
2029 +vchiq_doorbell_irq(int irq, void *dev_id)
2031 + VCHIQ_STATE_T *state = dev_id;
2032 + irqreturn_t ret = IRQ_NONE;
2033 + unsigned int status;
2035 + /* Read (and clear) the doorbell */
2036 + status = readl(__io_address(ARM_0_BELL0));
2038 + if (status & 0x4) { /* Was the doorbell rung? */
2039 + remote_event_pollall(state);
2040 + ret = IRQ_HANDLED;
2046 +/* There is a potential problem with partial cache lines (pages?)
2047 +** at the ends of the block when reading. If the CPU accessed anything in
2048 +** the same line (page?) then it may have pulled old data into the cache,
2049 +** obscuring the new data underneath. We can solve this by transferring the
2050 +** partial cache lines separately, and allowing the ARM to copy into the
2053 +** N.B. This implementation plays slightly fast and loose with the Linux
2054 +** driver programming rules, e.g. its use of __virt_to_bus instead of
2055 +** dma_map_single, but it isn't a multi-platform driver and it benefits
2056 +** from increased speed as a result.
2060 +create_pagelist(char __user *buf, size_t count, unsigned short type,
2061 + struct task_struct *task, PAGELIST_T ** ppagelist)
2063 + PAGELIST_T *pagelist;
2064 + struct page **pages;
2065 + struct page *page;
2066 + unsigned long *addrs;
2067 + unsigned int num_pages, offset, i;
2068 + char *addr, *base_addr, *next_addr;
2069 + int run, addridx, actual_pages;
2070 + unsigned long *need_release;
2072 + offset = (unsigned int)buf & (PAGE_SIZE - 1);
2073 + num_pages = (count + offset + PAGE_SIZE - 1) / PAGE_SIZE;
2075 + *ppagelist = NULL;
2077 + /* Allocate enough storage to hold the page pointers and the page
2080 + pagelist = kmalloc(sizeof(PAGELIST_T) +
2081 + (num_pages * sizeof(unsigned long)) +
2082 + sizeof(unsigned long) +
2083 + (num_pages * sizeof(pages[0])),
2086 + vchiq_log_trace(vchiq_arm_log_level,
2087 + "create_pagelist - %x", (unsigned int)pagelist);
2091 + addrs = pagelist->addrs;
2092 + need_release = (unsigned long *)(addrs + num_pages);
2093 + pages = (struct page **)(addrs + num_pages + 1);
2095 + if (is_vmalloc_addr(buf)) {
2096 + for (actual_pages = 0; actual_pages < num_pages; actual_pages++) {
2097 + pages[actual_pages] = vmalloc_to_page(buf + (actual_pages * PAGE_SIZE));
2099 + *need_release = 0; /* do not try and release vmalloc pages */
2101 + down_read(&task->mm->mmap_sem);
2102 + actual_pages = get_user_pages(task, task->mm,
2103 + (unsigned long)buf & ~(PAGE_SIZE - 1),
2105 + (type == PAGELIST_READ) /*Write */ ,
2109 + up_read(&task->mm->mmap_sem);
2111 + if (actual_pages != num_pages) {
2112 + vchiq_log_info(vchiq_arm_log_level,
2113 + "create_pagelist - only %d/%d pages locked",
2117 + /* This is probably due to the process being killed */
2118 + while (actual_pages > 0)
2121 + page_cache_release(pages[actual_pages]);
2124 + if (actual_pages == 0)
2125 + actual_pages = -ENOMEM;
2126 + return actual_pages;
2128 + *need_release = 1; /* release user pages */
2131 + pagelist->length = count;
2132 + pagelist->type = type;
2133 + pagelist->offset = offset;
2135 + /* Group the pages into runs of contiguous pages */
2137 + base_addr = VCHIQ_ARM_ADDRESS(page_address(pages[0]));
2138 + next_addr = base_addr + PAGE_SIZE;
2142 + for (i = 1; i < num_pages; i++) {
2143 + addr = VCHIQ_ARM_ADDRESS(page_address(pages[i]));
2144 + if ((addr == next_addr) && (run < (PAGE_SIZE - 1))) {
2145 + next_addr += PAGE_SIZE;
2148 + addrs[addridx] = (unsigned long)base_addr + run;
2151 + next_addr = addr + PAGE_SIZE;
2156 + addrs[addridx] = (unsigned long)base_addr + run;
2159 + /* Partial cache lines (fragments) require special measures */
2160 + if ((type == PAGELIST_READ) &&
2161 + ((pagelist->offset & (CACHE_LINE_SIZE - 1)) ||
2162 + ((pagelist->offset + pagelist->length) &
2163 + (CACHE_LINE_SIZE - 1)))) {
2164 + FRAGMENTS_T *fragments;
2166 + if (down_interruptible(&g_free_fragments_sema) != 0) {
2171 + WARN_ON(g_free_fragments == NULL);
2173 + down(&g_free_fragments_mutex);
2174 + fragments = (FRAGMENTS_T *) g_free_fragments;
2175 + WARN_ON(fragments == NULL);
2176 + g_free_fragments = *(FRAGMENTS_T **) g_free_fragments;
2177 + up(&g_free_fragments_mutex);
2179 + PAGELIST_READ_WITH_FRAGMENTS + (fragments -
2180 + g_fragments_base);
2183 + for (page = virt_to_page(pagelist);
2184 + page <= virt_to_page(addrs + num_pages - 1); page++) {
2185 + flush_dcache_page(page);
2188 + *ppagelist = pagelist;
2194 +free_pagelist(PAGELIST_T *pagelist, int actual)
2196 + unsigned long *need_release;
2197 + struct page **pages;
2198 + unsigned int num_pages, i;
2200 + vchiq_log_trace(vchiq_arm_log_level,
2201 + "free_pagelist - %x, %d", (unsigned int)pagelist, actual);
2204 + (pagelist->length + pagelist->offset + PAGE_SIZE - 1) /
2207 + need_release = (unsigned long *)(pagelist->addrs + num_pages);
2208 + pages = (struct page **)(pagelist->addrs + num_pages + 1);
2210 + /* Deal with any partial cache lines (fragments) */
2211 + if (pagelist->type >= PAGELIST_READ_WITH_FRAGMENTS) {
2212 + FRAGMENTS_T *fragments = g_fragments_base +
2213 + (pagelist->type - PAGELIST_READ_WITH_FRAGMENTS);
2214 + int head_bytes, tail_bytes;
2215 + head_bytes = (CACHE_LINE_SIZE - pagelist->offset) &
2216 + (CACHE_LINE_SIZE - 1);
2217 + tail_bytes = (pagelist->offset + actual) &
2218 + (CACHE_LINE_SIZE - 1);
2220 + if ((actual >= 0) && (head_bytes != 0)) {
2221 + if (head_bytes > actual)
2222 + head_bytes = actual;
2224 + memcpy((char *)page_address(pages[0]) +
2226 + fragments->headbuf,
2229 + if ((actual >= 0) && (head_bytes < actual) &&
2230 + (tail_bytes != 0)) {
2231 + memcpy((char *)page_address(pages[num_pages - 1]) +
2232 + ((pagelist->offset + actual) &
2233 + (PAGE_SIZE - 1) & ~(CACHE_LINE_SIZE - 1)),
2234 + fragments->tailbuf, tail_bytes);
2237 + down(&g_free_fragments_mutex);
2238 + *(FRAGMENTS_T **) fragments = g_free_fragments;
2239 + g_free_fragments = fragments;
2240 + up(&g_free_fragments_mutex);
2241 + up(&g_free_fragments_sema);
2244 + if (*need_release) {
2245 + for (i = 0; i < num_pages; i++) {
2246 + if (pagelist->type != PAGELIST_WRITE)
2247 + set_page_dirty(pages[i]);
2249 + page_cache_release(pages[i]);
2255 diff --git a/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.c b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.c
2256 new file mode 100644
2257 index 0000000..c1fb8c3
2259 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.c
2262 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
2264 + * Redistribution and use in source and binary forms, with or without
2265 + * modification, are permitted provided that the following conditions
2267 + * 1. Redistributions of source code must retain the above copyright
2268 + * notice, this list of conditions, and the following disclaimer,
2269 + * without modification.
2270 + * 2. Redistributions in binary form must reproduce the above copyright
2271 + * notice, this list of conditions and the following disclaimer in the
2272 + * documentation and/or other materials provided with the distribution.
2273 + * 3. The names of the above-listed copyright holders may not be used
2274 + * to endorse or promote products derived from this software without
2275 + * specific prior written permission.
2277 + * ALTERNATIVELY, this software may be distributed under the terms of the
2278 + * GNU General Public License ("GPL") version 2, as published by the Free
2279 + * Software Foundation.
2281 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
2282 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
2283 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2284 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
2285 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
2286 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
2287 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
2288 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
2289 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
2290 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
2291 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2294 +#include <linux/kernel.h>
2295 +#include <linux/module.h>
2296 +#include <linux/types.h>
2297 +#include <linux/errno.h>
2298 +#include <linux/cdev.h>
2299 +#include <linux/fs.h>
2300 +#include <linux/device.h>
2301 +#include <linux/mm.h>
2302 +#include <linux/highmem.h>
2303 +#include <linux/pagemap.h>
2304 +#include <linux/bug.h>
2305 +#include <linux/semaphore.h>
2306 +#include <linux/list.h>
2307 +#include <linux/proc_fs.h>
2309 +#include "vchiq_core.h"
2310 +#include "vchiq_ioctl.h"
2311 +#include "vchiq_arm.h"
2313 +#define DEVICE_NAME "vchiq"
2315 +/* Override the default prefix, which would be vchiq_arm (from the filename) */
2316 +#undef MODULE_PARAM_PREFIX
2317 +#define MODULE_PARAM_PREFIX DEVICE_NAME "."
2319 +#define VCHIQ_MINOR 0
2321 +/* Some per-instance constants */
2322 +#define MAX_COMPLETIONS 16
2323 +#define MAX_SERVICES 64
2324 +#define MAX_ELEMENTS 8
2325 +#define MSG_QUEUE_SIZE 64
2327 +#define KEEPALIVE_VER 1
2328 +#define KEEPALIVE_VER_MIN KEEPALIVE_VER
2330 +/* Run time control of log level, based on KERN_XXX level. */
2331 +int vchiq_arm_log_level = VCHIQ_LOG_DEFAULT;
2332 +int vchiq_susp_log_level = VCHIQ_LOG_ERROR;
2334 +#define SUSPEND_TIMER_TIMEOUT_MS 100
2335 +#define SUSPEND_RETRY_TIMER_TIMEOUT_MS 1000
2337 +#define VC_SUSPEND_NUM_OFFSET 3 /* number of values before idle which are -ve */
2338 +static const char *const suspend_state_names[] = {
2339 + "VC_SUSPEND_FORCE_CANCELED",
2340 + "VC_SUSPEND_REJECTED",
2341 + "VC_SUSPEND_FAILED",
2342 + "VC_SUSPEND_IDLE",
2343 + "VC_SUSPEND_REQUESTED",
2344 + "VC_SUSPEND_IN_PROGRESS",
2345 + "VC_SUSPEND_SUSPENDED"
2347 +#define VC_RESUME_NUM_OFFSET 1 /* number of values before idle which are -ve */
2348 +static const char *const resume_state_names[] = {
2349 + "VC_RESUME_FAILED",
2351 + "VC_RESUME_REQUESTED",
2352 + "VC_RESUME_IN_PROGRESS",
2353 + "VC_RESUME_RESUMED"
2355 +/* The number of times we allow force suspend to timeout before actually
2356 +** _forcing_ suspend. This is to cater for SW which fails to release vchiq
2357 +** correctly - we don't want to prevent ARM suspend indefinitely in this case.
2359 +#define FORCE_SUSPEND_FAIL_MAX 8
2361 +/* The time in ms allowed for videocore to go idle when force suspend has been
2363 +#define FORCE_SUSPEND_TIMEOUT_MS 200
2366 +static void suspend_timer_callback(unsigned long context);
2367 +static int vchiq_proc_add_instance(VCHIQ_INSTANCE_T instance);
2368 +static void vchiq_proc_remove_instance(VCHIQ_INSTANCE_T instance);
2371 +typedef struct user_service_struct {
2372 + VCHIQ_SERVICE_T *service;
2374 + VCHIQ_INSTANCE_T instance;
2376 + int dequeue_pending;
2377 + int message_available_pos;
2380 + struct semaphore insert_event;
2381 + struct semaphore remove_event;
2382 + VCHIQ_HEADER_T * msg_queue[MSG_QUEUE_SIZE];
2385 +struct bulk_waiter_node {
2386 + struct bulk_waiter bulk_waiter;
2388 + struct list_head list;
2391 +struct vchiq_instance_struct {
2392 + VCHIQ_STATE_T *state;
2393 + VCHIQ_COMPLETION_DATA_T completions[MAX_COMPLETIONS];
2394 + int completion_insert;
2395 + int completion_remove;
2396 + struct semaphore insert_event;
2397 + struct semaphore remove_event;
2398 + struct mutex completion_mutex;
2405 + struct list_head bulk_waiter_list;
2406 + struct mutex bulk_waiter_list_mutex;
2408 + struct proc_dir_entry *proc_entry;
2411 +typedef struct dump_context_struct {
2418 +static struct cdev vchiq_cdev;
2419 +static dev_t vchiq_devid;
2420 +static VCHIQ_STATE_T g_state;
2421 +static struct class *vchiq_class;
2422 +static struct device *vchiq_dev;
2423 +static DEFINE_SPINLOCK(msg_queue_spinlock);
2425 +static const char *const ioctl_names[] = {
2431 + "QUEUE_BULK_TRANSMIT",
2432 + "QUEUE_BULK_RECEIVE",
2433 + "AWAIT_COMPLETION",
2434 + "DEQUEUE_MESSAGE",
2439 + "RELEASE_SERVICE",
2440 + "SET_SERVICE_OPTION",
2444 +vchiq_static_assert((sizeof(ioctl_names)/sizeof(ioctl_names[0])) ==
2445 + (VCHIQ_IOC_MAX + 1));
2448 +dump_phys_mem(void *virt_addr, uint32_t num_bytes);
2450 +/****************************************************************************
2454 +***************************************************************************/
2456 +static VCHIQ_STATUS_T
2457 +add_completion(VCHIQ_INSTANCE_T instance, VCHIQ_REASON_T reason,
2458 + VCHIQ_HEADER_T *header, USER_SERVICE_T *user_service,
2459 + void *bulk_userdata)
2461 + VCHIQ_COMPLETION_DATA_T *completion;
2462 + DEBUG_INITIALISE(g_state.local)
2464 + while (instance->completion_insert ==
2465 + (instance->completion_remove + MAX_COMPLETIONS)) {
2466 + /* Out of space - wait for the client */
2467 + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
2468 + vchiq_log_trace(vchiq_arm_log_level,
2469 + "add_completion - completion queue full");
2470 + DEBUG_COUNT(COMPLETION_QUEUE_FULL_COUNT);
2471 + if (down_interruptible(&instance->remove_event) != 0) {
2472 + vchiq_log_info(vchiq_arm_log_level,
2473 + "service_callback interrupted");
2474 + return VCHIQ_RETRY;
2475 + } else if (instance->closing) {
2476 + vchiq_log_info(vchiq_arm_log_level,
2477 + "service_callback closing");
2478 + return VCHIQ_ERROR;
2480 + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
2484 + &instance->completions[instance->completion_insert &
2485 + (MAX_COMPLETIONS - 1)];
2487 + completion->header = header;
2488 + completion->reason = reason;
2489 + /* N.B. service_userdata is updated while processing AWAIT_COMPLETION */
2490 + completion->service_userdata = user_service->service;
2491 + completion->bulk_userdata = bulk_userdata;
2493 + if (reason == VCHIQ_SERVICE_CLOSED)
2494 + /* Take an extra reference, to be held until
2495 + this CLOSED notification is delivered. */
2496 + lock_service(user_service->service);
2498 + /* A write barrier is needed here to ensure that the entire completion
2499 + record is written out before the insert point. */
2502 + if (reason == VCHIQ_MESSAGE_AVAILABLE)
2503 + user_service->message_available_pos =
2504 + instance->completion_insert;
2505 + instance->completion_insert++;
2507 + up(&instance->insert_event);
2509 + return VCHIQ_SUCCESS;
2512 +/****************************************************************************
2516 +***************************************************************************/
2518 +static VCHIQ_STATUS_T
2519 +service_callback(VCHIQ_REASON_T reason, VCHIQ_HEADER_T *header,
2520 + VCHIQ_SERVICE_HANDLE_T handle, void *bulk_userdata)
2522 + /* How do we ensure the callback goes to the right client?
2523 + ** The service_user data points to a USER_SERVICE_T record containing
2524 + ** the original callback and the user state structure, which contains a
2525 + ** circular buffer for completion records.
2527 + USER_SERVICE_T *user_service;
2528 + VCHIQ_SERVICE_T *service;
2529 + VCHIQ_INSTANCE_T instance;
2530 + DEBUG_INITIALISE(g_state.local)
2532 + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
2534 + service = handle_to_service(handle);
2536 + user_service = (USER_SERVICE_T *)service->base.userdata;
2537 + instance = user_service->instance;
2539 + if (!instance || instance->closing)
2540 + return VCHIQ_SUCCESS;
2542 + vchiq_log_trace(vchiq_arm_log_level,
2543 + "service_callback - service %lx(%d), reason %d, header %lx, "
2544 + "instance %lx, bulk_userdata %lx",
2545 + (unsigned long)user_service,
2546 + service->localport,
2547 + reason, (unsigned long)header,
2548 + (unsigned long)instance, (unsigned long)bulk_userdata);
2550 + if (header && user_service->is_vchi) {
2551 + spin_lock(&msg_queue_spinlock);
2552 + while (user_service->msg_insert ==
2553 + (user_service->msg_remove + MSG_QUEUE_SIZE)) {
2554 + spin_unlock(&msg_queue_spinlock);
2555 + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
2556 + DEBUG_COUNT(MSG_QUEUE_FULL_COUNT);
2557 + vchiq_log_trace(vchiq_arm_log_level,
2558 + "service_callback - msg queue full");
2559 + /* If there is no MESSAGE_AVAILABLE in the completion
2562 + if ((user_service->message_available_pos -
2563 + instance->completion_remove) < 0) {
2564 + VCHIQ_STATUS_T status;
2565 + vchiq_log_info(vchiq_arm_log_level,
2566 + "Inserting extra MESSAGE_AVAILABLE");
2567 + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
2568 + status = add_completion(instance, reason,
2569 + NULL, user_service, bulk_userdata);
2570 + if (status != VCHIQ_SUCCESS) {
2571 + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
2576 + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
2577 + if (down_interruptible(&user_service->remove_event)
2579 + vchiq_log_info(vchiq_arm_log_level,
2580 + "service_callback interrupted");
2581 + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
2582 + return VCHIQ_RETRY;
2583 + } else if (instance->closing) {
2584 + vchiq_log_info(vchiq_arm_log_level,
2585 + "service_callback closing");
2586 + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
2587 + return VCHIQ_ERROR;
2589 + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
2590 + spin_lock(&msg_queue_spinlock);
2593 + user_service->msg_queue[user_service->msg_insert &
2594 + (MSG_QUEUE_SIZE - 1)] = header;
2595 + user_service->msg_insert++;
2596 + spin_unlock(&msg_queue_spinlock);
2598 + up(&user_service->insert_event);
2600 + /* If there is a thread waiting in DEQUEUE_MESSAGE, or if
2601 + ** there is a MESSAGE_AVAILABLE in the completion queue then
2602 + ** bypass the completion queue.
2604 + if (((user_service->message_available_pos -
2605 + instance->completion_remove) >= 0) ||
2606 + user_service->dequeue_pending) {
2607 + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
2608 + user_service->dequeue_pending = 0;
2609 + return VCHIQ_SUCCESS;
2614 + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
2616 + return add_completion(instance, reason, header, user_service,
2620 +/****************************************************************************
2622 +* user_service_free
2624 +***************************************************************************/
2626 +user_service_free(void *userdata)
2631 +/****************************************************************************
2635 +***************************************************************************/
2638 +vchiq_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2640 + VCHIQ_INSTANCE_T instance = file->private_data;
2641 + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
2642 + VCHIQ_SERVICE_T *service = NULL;
2645 + DEBUG_INITIALISE(g_state.local)
2647 + vchiq_log_trace(vchiq_arm_log_level,
2648 + "vchiq_ioctl - instance %x, cmd %s, arg %lx",
2649 + (unsigned int)instance,
2650 + ((_IOC_TYPE(cmd) == VCHIQ_IOC_MAGIC) &&
2651 + (_IOC_NR(cmd) <= VCHIQ_IOC_MAX)) ?
2652 + ioctl_names[_IOC_NR(cmd)] : "<invalid>", arg);
2655 + case VCHIQ_IOC_SHUTDOWN:
2656 + if (!instance->connected)
2659 + /* Remove all services */
2661 + while ((service = next_service_by_instance(instance->state,
2662 + instance, &i)) != NULL) {
2663 + status = vchiq_remove_service(service->handle);
2664 + unlock_service(service);
2665 + if (status != VCHIQ_SUCCESS)
2670 + if (status == VCHIQ_SUCCESS) {
2671 + /* Wake the completion thread and ask it to exit */
2672 + instance->closing = 1;
2673 + up(&instance->insert_event);
2678 + case VCHIQ_IOC_CONNECT:
2679 + if (instance->connected) {
2683 + rc = mutex_lock_interruptible(&instance->state->mutex);
2685 + vchiq_log_error(vchiq_arm_log_level,
2686 + "vchiq: connect: could not lock mutex for "
2688 + instance->state->id, rc);
2692 + status = vchiq_connect_internal(instance->state, instance);
2693 + mutex_unlock(&instance->state->mutex);
2695 + if (status == VCHIQ_SUCCESS)
2696 + instance->connected = 1;
2698 + vchiq_log_error(vchiq_arm_log_level,
2699 + "vchiq: could not connect: %d", status);
2702 + case VCHIQ_IOC_CREATE_SERVICE: {
2703 + VCHIQ_CREATE_SERVICE_T args;
2704 + USER_SERVICE_T *user_service = NULL;
2708 + if (copy_from_user
2709 + (&args, (const void __user *)arg,
2710 + sizeof(args)) != 0) {
2715 + user_service = kmalloc(sizeof(USER_SERVICE_T), GFP_KERNEL);
2716 + if (!user_service) {
2721 + if (args.is_open) {
2722 + if (!instance->connected) {
2724 + kfree(user_service);
2727 + srvstate = VCHIQ_SRVSTATE_OPENING;
2730 + instance->connected ?
2731 + VCHIQ_SRVSTATE_LISTENING :
2732 + VCHIQ_SRVSTATE_HIDDEN;
2735 + userdata = args.params.userdata;
2736 + args.params.callback = service_callback;
2737 + args.params.userdata = user_service;
2738 + service = vchiq_add_service_internal(
2740 + &args.params, srvstate,
2741 + instance, user_service_free);
2743 + if (service != NULL) {
2744 + user_service->service = service;
2745 + user_service->userdata = userdata;
2746 + user_service->instance = instance;
2747 + user_service->is_vchi = args.is_vchi;
2748 + user_service->dequeue_pending = 0;
2749 + user_service->message_available_pos =
2750 + instance->completion_remove - 1;
2751 + user_service->msg_insert = 0;
2752 + user_service->msg_remove = 0;
2753 + sema_init(&user_service->insert_event, 0);
2754 + sema_init(&user_service->remove_event, 0);
2756 + if (args.is_open) {
2757 + status = vchiq_open_service_internal
2758 + (service, instance->pid);
2759 + if (status != VCHIQ_SUCCESS) {
2760 + vchiq_remove_service(service->handle);
2762 + ret = (status == VCHIQ_RETRY) ?
2768 + if (copy_to_user((void __user *)
2769 + &(((VCHIQ_CREATE_SERVICE_T __user *)
2771 + (const void *)&service->handle,
2772 + sizeof(service->handle)) != 0) {
2774 + vchiq_remove_service(service->handle);
2780 + kfree(user_service);
2784 + case VCHIQ_IOC_CLOSE_SERVICE: {
2785 + VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
2787 + service = find_service_for_instance(instance, handle);
2788 + if (service != NULL)
2789 + status = vchiq_close_service(service->handle);
2794 + case VCHIQ_IOC_REMOVE_SERVICE: {
2795 + VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
2797 + service = find_service_for_instance(instance, handle);
2798 + if (service != NULL)
2799 + status = vchiq_remove_service(service->handle);
2804 + case VCHIQ_IOC_USE_SERVICE:
2805 + case VCHIQ_IOC_RELEASE_SERVICE: {
2806 + VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
2808 + service = find_service_for_instance(instance, handle);
2809 + if (service != NULL) {
2810 + status = (cmd == VCHIQ_IOC_USE_SERVICE) ?
2811 + vchiq_use_service_internal(service) :
2812 + vchiq_release_service_internal(service);
2813 + if (status != VCHIQ_SUCCESS) {
2814 + vchiq_log_error(vchiq_susp_log_level,
2815 + "%s: cmd %s returned error %d for "
2816 + "service %c%c%c%c:%03d",
2818 + (cmd == VCHIQ_IOC_USE_SERVICE) ?
2819 + "VCHIQ_IOC_USE_SERVICE" :
2820 + "VCHIQ_IOC_RELEASE_SERVICE",
2822 + VCHIQ_FOURCC_AS_4CHARS(
2823 + service->base.fourcc),
2824 + service->client_id);
2831 + case VCHIQ_IOC_QUEUE_MESSAGE: {
2832 + VCHIQ_QUEUE_MESSAGE_T args;
2833 + if (copy_from_user
2834 + (&args, (const void __user *)arg,
2835 + sizeof(args)) != 0) {
2840 + service = find_service_for_instance(instance, args.handle);
2842 + if ((service != NULL) && (args.count <= MAX_ELEMENTS)) {
2843 + /* Copy elements into kernel space */
2844 + VCHIQ_ELEMENT_T elements[MAX_ELEMENTS];
2845 + if (copy_from_user(elements, args.elements,
2846 + args.count * sizeof(VCHIQ_ELEMENT_T)) == 0)
2847 + status = vchiq_queue_message
2849 + elements, args.count);
2857 + case VCHIQ_IOC_QUEUE_BULK_TRANSMIT:
2858 + case VCHIQ_IOC_QUEUE_BULK_RECEIVE: {
2859 + VCHIQ_QUEUE_BULK_TRANSFER_T args;
2860 + struct bulk_waiter_node *waiter = NULL;
2861 + VCHIQ_BULK_DIR_T dir =
2862 + (cmd == VCHIQ_IOC_QUEUE_BULK_TRANSMIT) ?
2863 + VCHIQ_BULK_TRANSMIT : VCHIQ_BULK_RECEIVE;
2865 + if (copy_from_user
2866 + (&args, (const void __user *)arg,
2867 + sizeof(args)) != 0) {
2872 + service = find_service_for_instance(instance, args.handle);
2878 + if (args.mode == VCHIQ_BULK_MODE_BLOCKING) {
2879 + waiter = kzalloc(sizeof(struct bulk_waiter_node),
2885 + args.userdata = &waiter->bulk_waiter;
2886 + } else if (args.mode == VCHIQ_BULK_MODE_WAITING) {
2887 + struct list_head *pos;
2888 + mutex_lock(&instance->bulk_waiter_list_mutex);
2889 + list_for_each(pos, &instance->bulk_waiter_list) {
2890 + if (list_entry(pos, struct bulk_waiter_node,
2891 + list)->pid == current->pid) {
2892 + waiter = list_entry(pos,
2893 + struct bulk_waiter_node,
2900 + mutex_unlock(&instance->bulk_waiter_list_mutex);
2902 + vchiq_log_error(vchiq_arm_log_level,
2903 + "no bulk_waiter found for pid %d",
2908 + vchiq_log_info(vchiq_arm_log_level,
2909 + "found bulk_waiter %x for pid %d",
2910 + (unsigned int)waiter, current->pid);
2911 + args.userdata = &waiter->bulk_waiter;
2913 + status = vchiq_bulk_transfer
2915 + VCHI_MEM_HANDLE_INVALID,
2916 + args.data, args.size,
2917 + args.userdata, args.mode,
2921 + if ((status != VCHIQ_RETRY) || fatal_signal_pending(current) ||
2922 + !waiter->bulk_waiter.bulk) {
2923 + if (waiter->bulk_waiter.bulk) {
2924 + /* Cancel the signal when the transfer
2926 + spin_lock(&bulk_waiter_spinlock);
2927 + waiter->bulk_waiter.bulk->userdata = NULL;
2928 + spin_unlock(&bulk_waiter_spinlock);
2932 + const VCHIQ_BULK_MODE_T mode_waiting =
2933 + VCHIQ_BULK_MODE_WAITING;
2934 + waiter->pid = current->pid;
2935 + mutex_lock(&instance->bulk_waiter_list_mutex);
2936 + list_add(&waiter->list, &instance->bulk_waiter_list);
2937 + mutex_unlock(&instance->bulk_waiter_list_mutex);
2938 + vchiq_log_info(vchiq_arm_log_level,
2939 + "saved bulk_waiter %x for pid %d",
2940 + (unsigned int)waiter, current->pid);
2942 + if (copy_to_user((void __user *)
2943 + &(((VCHIQ_QUEUE_BULK_TRANSFER_T __user *)
2945 + (const void *)&mode_waiting,
2946 + sizeof(mode_waiting)) != 0)
2951 + case VCHIQ_IOC_AWAIT_COMPLETION: {
2952 + VCHIQ_AWAIT_COMPLETION_T args;
2954 + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
2955 + if (!instance->connected) {
2960 + if (copy_from_user(&args, (const void __user *)arg,
2961 + sizeof(args)) != 0) {
2966 + mutex_lock(&instance->completion_mutex);
2968 + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
2969 + while ((instance->completion_remove ==
2970 + instance->completion_insert)
2971 + && !instance->closing) {
2973 + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
2974 + mutex_unlock(&instance->completion_mutex);
2975 + rc = down_interruptible(&instance->insert_event);
2976 + mutex_lock(&instance->completion_mutex);
2978 + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
2979 + vchiq_log_info(vchiq_arm_log_level,
2980 + "AWAIT_COMPLETION interrupted");
2985 + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
2987 + /* A read memory barrier is needed to stop prefetch of a stale
2988 + ** completion record
2993 + int msgbufcount = args.msgbufcount;
2994 + for (ret = 0; ret < args.count; ret++) {
2995 + VCHIQ_COMPLETION_DATA_T *completion;
2996 + VCHIQ_SERVICE_T *service;
2997 + USER_SERVICE_T *user_service;
2998 + VCHIQ_HEADER_T *header;
2999 + if (instance->completion_remove ==
3000 + instance->completion_insert)
3002 + completion = &instance->completions[
3003 + instance->completion_remove &
3004 + (MAX_COMPLETIONS - 1)];
3006 + service = completion->service_userdata;
3007 + user_service = service->base.userdata;
3008 + completion->service_userdata =
3009 + user_service->userdata;
3011 + header = completion->header;
3013 + void __user *msgbuf;
3016 + msglen = header->size +
3017 + sizeof(VCHIQ_HEADER_T);
3018 + /* This must be a VCHIQ-style service */
3019 + if (args.msgbufsize < msglen) {
3021 + vchiq_arm_log_level,
3022 + "header %x: msgbufsize"
3023 + " %x < msglen %x",
3024 + (unsigned int)header,
3027 + WARN(1, "invalid message "
3033 + if (msgbufcount <= 0)
3034 + /* Stall here for lack of a
3035 + ** buffer for the message. */
3037 + /* Get the pointer from user space */
3039 + if (copy_from_user(&msgbuf,
3040 + (const void __user *)
3041 + &args.msgbufs[msgbufcount],
3042 + sizeof(msgbuf)) != 0) {
3048 + /* Copy the message to user space */
3049 + if (copy_to_user(msgbuf, header,
3056 + /* Now it has been copied, the message
3057 + ** can be released. */
3058 + vchiq_release_message(service->handle,
3061 + /* The completion must point to the
3063 + completion->header = msgbuf;
3066 + if (completion->reason ==
3067 + VCHIQ_SERVICE_CLOSED)
3068 + unlock_service(service);
3070 + if (copy_to_user((void __user *)(
3071 + (size_t)args.buf +
3072 + ret * sizeof(VCHIQ_COMPLETION_DATA_T)),
3074 + sizeof(VCHIQ_COMPLETION_DATA_T)) != 0) {
3080 + instance->completion_remove++;
3083 + if (msgbufcount != args.msgbufcount) {
3084 + if (copy_to_user((void __user *)
3085 + &((VCHIQ_AWAIT_COMPLETION_T *)arg)->
3088 + sizeof(msgbufcount)) != 0) {
3095 + up(&instance->remove_event);
3096 + mutex_unlock(&instance->completion_mutex);
3097 + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
3100 + case VCHIQ_IOC_DEQUEUE_MESSAGE: {
3101 + VCHIQ_DEQUEUE_MESSAGE_T args;
3102 + USER_SERVICE_T *user_service;
3103 + VCHIQ_HEADER_T *header;
3105 + DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
3106 + if (copy_from_user
3107 + (&args, (const void __user *)arg,
3108 + sizeof(args)) != 0) {
3112 + service = find_service_for_instance(instance, args.handle);
3117 + user_service = (USER_SERVICE_T *)service->base.userdata;
3118 + if (user_service->is_vchi == 0) {
3123 + spin_lock(&msg_queue_spinlock);
3124 + if (user_service->msg_remove == user_service->msg_insert) {
3125 + if (!args.blocking) {
3126 + spin_unlock(&msg_queue_spinlock);
3127 + DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
3128 + ret = -EWOULDBLOCK;
3131 + user_service->dequeue_pending = 1;
3133 + spin_unlock(&msg_queue_spinlock);
3134 + DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
3135 + if (down_interruptible(
3136 + &user_service->insert_event) != 0) {
3137 + vchiq_log_info(vchiq_arm_log_level,
3138 + "DEQUEUE_MESSAGE interrupted");
3142 + spin_lock(&msg_queue_spinlock);
3143 + } while (user_service->msg_remove ==
3144 + user_service->msg_insert);
3150 + BUG_ON((int)(user_service->msg_insert -
3151 + user_service->msg_remove) < 0);
3153 + header = user_service->msg_queue[user_service->msg_remove &
3154 + (MSG_QUEUE_SIZE - 1)];
3155 + user_service->msg_remove++;
3156 + spin_unlock(&msg_queue_spinlock);
3158 + up(&user_service->remove_event);
3159 + if (header == NULL)
3161 + else if (header->size <= args.bufsize) {
3162 + /* Copy to user space if msgbuf is not NULL */
3163 + if ((args.buf == NULL) ||
3164 + (copy_to_user((void __user *)args.buf,
3166 + header->size) == 0)) {
3167 + ret = header->size;
3168 + vchiq_release_message(
3174 + vchiq_log_error(vchiq_arm_log_level,
3175 + "header %x: bufsize %x < size %x",
3176 + (unsigned int)header, args.bufsize,
3178 + WARN(1, "invalid size\n");
3181 + DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
3184 + case VCHIQ_IOC_GET_CLIENT_ID: {
3185 + VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
3187 + ret = vchiq_get_client_id(handle);
3190 + case VCHIQ_IOC_GET_CONFIG: {
3191 + VCHIQ_GET_CONFIG_T args;
3192 + VCHIQ_CONFIG_T config;
3194 + if (copy_from_user(&args, (const void __user *)arg,
3195 + sizeof(args)) != 0) {
3199 + if (args.config_size > sizeof(config)) {
3203 + status = vchiq_get_config(instance, args.config_size, &config);
3204 + if (status == VCHIQ_SUCCESS) {
3205 + if (copy_to_user((void __user *)args.pconfig,
3206 + &config, args.config_size) != 0) {
3213 + case VCHIQ_IOC_SET_SERVICE_OPTION: {
3214 + VCHIQ_SET_SERVICE_OPTION_T args;
3216 + if (copy_from_user(
3217 + &args, (const void __user *)arg,
3218 + sizeof(args)) != 0) {
3223 + service = find_service_for_instance(instance, args.handle);
3229 + status = vchiq_set_service_option(
3230 + args.handle, args.option, args.value);
3233 + case VCHIQ_IOC_DUMP_PHYS_MEM: {
3234 + VCHIQ_DUMP_MEM_T args;
3236 + if (copy_from_user
3237 + (&args, (const void __user *)arg,
3238 + sizeof(args)) != 0) {
3242 + dump_phys_mem(args.virt_addr, args.num_bytes);
3251 + unlock_service(service);
3254 + if (status == VCHIQ_ERROR)
3256 + else if (status == VCHIQ_RETRY)
3260 + if ((status == VCHIQ_SUCCESS) && (ret < 0) && (ret != -EINTR) &&
3261 + (ret != -EWOULDBLOCK))
3262 + vchiq_log_info(vchiq_arm_log_level,
3263 + " ioctl instance %lx, cmd %s -> status %d, %ld",
3264 + (unsigned long)instance,
3265 + (_IOC_NR(cmd) <= VCHIQ_IOC_MAX) ?
3266 + ioctl_names[_IOC_NR(cmd)] :
3270 + vchiq_log_trace(vchiq_arm_log_level,
3271 + " ioctl instance %lx, cmd %s -> status %d, %ld",
3272 + (unsigned long)instance,
3273 + (_IOC_NR(cmd) <= VCHIQ_IOC_MAX) ?
3274 + ioctl_names[_IOC_NR(cmd)] :
3281 +/****************************************************************************
3285 +***************************************************************************/
3288 +vchiq_open(struct inode *inode, struct file *file)
3290 + int dev = iminor(inode) & 0x0f;
3291 + vchiq_log_info(vchiq_arm_log_level, "vchiq_open");
3293 + case VCHIQ_MINOR: {
3295 + VCHIQ_STATE_T *state = vchiq_get_state();
3296 + VCHIQ_INSTANCE_T instance;
3299 + vchiq_log_error(vchiq_arm_log_level,
3300 + "vchiq has no connection to VideoCore");
3304 + instance = kzalloc(sizeof(*instance), GFP_KERNEL);
3308 + instance->state = state;
3309 + instance->pid = current->tgid;
3311 + ret = vchiq_proc_add_instance(instance);
3317 + sema_init(&instance->insert_event, 0);
3318 + sema_init(&instance->remove_event, 0);
3319 + mutex_init(&instance->completion_mutex);
3320 + mutex_init(&instance->bulk_waiter_list_mutex);
3321 + INIT_LIST_HEAD(&instance->bulk_waiter_list);
3323 + file->private_data = instance;
3327 + vchiq_log_error(vchiq_arm_log_level,
3328 + "Unknown minor device: %d", dev);
3335 +/****************************************************************************
3339 +***************************************************************************/
3342 +vchiq_release(struct inode *inode, struct file *file)
3344 + int dev = iminor(inode) & 0x0f;
3347 + case VCHIQ_MINOR: {
3348 + VCHIQ_INSTANCE_T instance = file->private_data;
3349 + VCHIQ_STATE_T *state = vchiq_get_state();
3350 + VCHIQ_SERVICE_T *service;
3353 + vchiq_log_info(vchiq_arm_log_level,
3354 + "vchiq_release: instance=%lx",
3355 + (unsigned long)instance);
3362 + /* Ensure videocore is awake to allow termination. */
3363 + vchiq_use_internal(instance->state, NULL,
3366 + mutex_lock(&instance->completion_mutex);
3368 + /* Wake the completion thread and ask it to exit */
3369 + instance->closing = 1;
3370 + up(&instance->insert_event);
3372 + mutex_unlock(&instance->completion_mutex);
3374 + /* Wake the slot handler if the completion queue is full. */
3375 + up(&instance->remove_event);
3377 + /* Mark all services for termination... */
3379 + while ((service = next_service_by_instance(state, instance,
3381 + USER_SERVICE_T *user_service = service->base.userdata;
3383 + /* Wake the slot handler if the msg queue is full. */
3384 + up(&user_service->remove_event);
3386 + vchiq_terminate_service_internal(service);
3387 + unlock_service(service);
3390 + /* ...and wait for them to die */
3392 + while ((service = next_service_by_instance(state, instance, &i))
3394 + USER_SERVICE_T *user_service = service->base.userdata;
3396 + down(&service->remove_event);
3398 + BUG_ON(service->srvstate != VCHIQ_SRVSTATE_FREE);
3400 + spin_lock(&msg_queue_spinlock);
3402 + while (user_service->msg_remove !=
3403 + user_service->msg_insert) {
3404 + VCHIQ_HEADER_T *header = user_service->
3405 + msg_queue[user_service->msg_remove &
3406 + (MSG_QUEUE_SIZE - 1)];
3407 + user_service->msg_remove++;
3408 + spin_unlock(&msg_queue_spinlock);
3411 + vchiq_release_message(
3414 + spin_lock(&msg_queue_spinlock);
3417 + spin_unlock(&msg_queue_spinlock);
3419 + unlock_service(service);
3422 + /* Release any closed services */
3423 + while (instance->completion_remove !=
3424 + instance->completion_insert) {
3425 + VCHIQ_COMPLETION_DATA_T *completion;
3426 + VCHIQ_SERVICE_T *service;
3427 + completion = &instance->completions[
3428 + instance->completion_remove &
3429 + (MAX_COMPLETIONS - 1)];
3430 + service = completion->service_userdata;
3431 + if (completion->reason == VCHIQ_SERVICE_CLOSED)
3432 + unlock_service(service);
3433 + instance->completion_remove++;
3436 + /* Release the PEER service count. */
3437 + vchiq_release_internal(instance->state, NULL);
3440 + struct list_head *pos, *next;
3441 + list_for_each_safe(pos, next,
3442 + &instance->bulk_waiter_list) {
3443 + struct bulk_waiter_node *waiter;
3444 + waiter = list_entry(pos,
3445 + struct bulk_waiter_node,
3448 + vchiq_log_info(vchiq_arm_log_level,
3449 + "bulk_waiter - cleaned up %x "
3451 + (unsigned int)waiter, waiter->pid);
3456 + vchiq_proc_remove_instance(instance);
3459 + file->private_data = NULL;
3463 + vchiq_log_error(vchiq_arm_log_level,
3464 + "Unknown minor device: %d", dev);
3472 +/****************************************************************************
3476 +***************************************************************************/
3479 +vchiq_dump(void *dump_context, const char *str, int len)
3481 + DUMP_CONTEXT_T *context = (DUMP_CONTEXT_T *)dump_context;
3483 + if (context->actual < context->space) {
3485 + if (context->offset > 0) {
3486 + int skip_bytes = min(len, (int)context->offset);
3487 + str += skip_bytes;
3488 + len -= skip_bytes;
3489 + context->offset -= skip_bytes;
3490 + if (context->offset > 0)
3493 + copy_bytes = min(len, (int)(context->space - context->actual));
3494 + if (copy_bytes == 0)
3496 + if (copy_to_user(context->buf + context->actual, str,
3498 + context->actual = -EFAULT;
3499 + context->actual += copy_bytes;
3500 + len -= copy_bytes;
3502 + /* If tne terminating NUL is included in the length, then it
3503 + ** marks the end of a line and should be replaced with a
3504 + ** carriage return. */
3505 + if ((len == 0) && (str[copy_bytes - 1] == '\0')) {
3507 + if (copy_to_user(context->buf + context->actual - 1,
3509 + context->actual = -EFAULT;
3514 +/****************************************************************************
3516 +* vchiq_dump_platform_instance_state
3518 +***************************************************************************/
3521 +vchiq_dump_platform_instances(void *dump_context)
3523 + VCHIQ_STATE_T *state = vchiq_get_state();
3528 + /* There is no list of instances, so instead scan all services,
3529 + marking those that have been dumped. */
3531 + for (i = 0; i < state->unused_service; i++) {
3532 + VCHIQ_SERVICE_T *service = state->services[i];
3533 + VCHIQ_INSTANCE_T instance;
3535 + if (service && (service->base.callback == service_callback)) {
3536 + instance = service->instance;
3538 + instance->mark = 0;
3542 + for (i = 0; i < state->unused_service; i++) {
3543 + VCHIQ_SERVICE_T *service = state->services[i];
3544 + VCHIQ_INSTANCE_T instance;
3546 + if (service && (service->base.callback == service_callback)) {
3547 + instance = service->instance;
3548 + if (instance && !instance->mark) {
3549 + len = snprintf(buf, sizeof(buf),
3550 + "Instance %x: pid %d,%s completions "
3552 + (unsigned int)instance, instance->pid,
3553 + instance->connected ? " connected, " :
3555 + instance->completion_insert -
3556 + instance->completion_remove,
3559 + vchiq_dump(dump_context, buf, len + 1);
3561 + instance->mark = 1;
3567 +/****************************************************************************
3569 +* vchiq_dump_platform_service_state
3571 +***************************************************************************/
3574 +vchiq_dump_platform_service_state(void *dump_context, VCHIQ_SERVICE_T *service)
3576 + USER_SERVICE_T *user_service = (USER_SERVICE_T *)service->base.userdata;
3580 + len = snprintf(buf, sizeof(buf), " instance %x",
3581 + (unsigned int)service->instance);
3583 + if ((service->base.callback == service_callback) &&
3584 + user_service->is_vchi) {
3585 + len += snprintf(buf + len, sizeof(buf) - len,
3586 + ", %d/%d messages",
3587 + user_service->msg_insert - user_service->msg_remove,
3590 + if (user_service->dequeue_pending)
3591 + len += snprintf(buf + len, sizeof(buf) - len,
3592 + " (dequeue pending)");
3595 + vchiq_dump(dump_context, buf, len + 1);
3598 +/****************************************************************************
3602 +***************************************************************************/
3605 +dump_phys_mem(void *virt_addr, uint32_t num_bytes)
3608 + uint8_t *end_virt_addr = virt_addr + num_bytes;
3614 + struct page *page;
3615 + struct page **pages;
3616 + uint8_t *kmapped_virt_ptr;
3618 + /* Align virtAddr and endVirtAddr to 16 byte boundaries. */
3620 + virt_addr = (void *)((unsigned long)virt_addr & ~0x0fuL);
3621 + end_virt_addr = (void *)(((unsigned long)end_virt_addr + 15uL) &
3624 + offset = (int)(long)virt_addr & (PAGE_SIZE - 1);
3625 + end_offset = (int)(long)end_virt_addr & (PAGE_SIZE - 1);
3627 + num_pages = (offset + num_bytes + PAGE_SIZE - 1) / PAGE_SIZE;
3629 + pages = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
3630 + if (pages == NULL) {
3631 + vchiq_log_error(vchiq_arm_log_level,
3632 + "Unable to allocation memory for %d pages\n",
3637 + down_read(¤t->mm->mmap_sem);
3638 + rc = get_user_pages(current, /* task */
3639 + current->mm, /* mm */
3640 + (unsigned long)virt_addr, /* start */
3641 + num_pages, /* len */
3644 + pages, /* pages (array of page pointers) */
3646 + up_read(¤t->mm->mmap_sem);
3651 + while (offset < end_offset) {
3653 + int page_offset = offset % PAGE_SIZE;
3654 + page_idx = offset / PAGE_SIZE;
3656 + if (page_idx != prev_idx) {
3660 + page = pages[page_idx];
3661 + kmapped_virt_ptr = kmap(page);
3663 + prev_idx = page_idx;
3666 + if (vchiq_arm_log_level >= VCHIQ_LOG_TRACE)
3667 + vchiq_log_dump_mem("ph",
3668 + (uint32_t)(unsigned long)&kmapped_virt_ptr[
3670 + &kmapped_virt_ptr[page_offset], 16);
3677 + for (page_idx = 0; page_idx < num_pages; page_idx++)
3678 + page_cache_release(pages[page_idx]);
3683 +/****************************************************************************
3687 +***************************************************************************/
3690 +vchiq_read(struct file *file, char __user *buf,
3691 + size_t count, loff_t *ppos)
3693 + DUMP_CONTEXT_T context;
3694 + context.buf = buf;
3695 + context.actual = 0;
3696 + context.space = count;
3697 + context.offset = *ppos;
3699 + vchiq_dump_state(&context, &g_state);
3701 + *ppos += context.actual;
3703 + return context.actual;
3707 +vchiq_get_state(void)
3710 + if (g_state.remote == NULL)
3711 + printk(KERN_ERR "%s: g_state.remote == NULL\n", __func__);
3712 + else if (g_state.remote->initialised != 1)
3713 + printk(KERN_NOTICE "%s: g_state.remote->initialised != 1 (%d)\n",
3714 + __func__, g_state.remote->initialised);
3716 + return ((g_state.remote != NULL) &&
3717 + (g_state.remote->initialised == 1)) ? &g_state : NULL;
3720 +static const struct file_operations
3722 + .owner = THIS_MODULE,
3723 + .unlocked_ioctl = vchiq_ioctl,
3724 + .open = vchiq_open,
3725 + .release = vchiq_release,
3726 + .read = vchiq_read
3730 + * Autosuspend related functionality
3734 +vchiq_videocore_wanted(VCHIQ_STATE_T *state)
3736 + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
3738 + /* autosuspend not supported - always return wanted */
3740 + else if (arm_state->blocked_count)
3742 + else if (!arm_state->videocore_use_count)
3743 + /* usage count zero - check for override unless we're forcing */
3744 + if (arm_state->resume_blocked)
3747 + return vchiq_platform_videocore_wanted(state);
3749 + /* non-zero usage count - videocore still required */
3753 +static VCHIQ_STATUS_T
3754 +vchiq_keepalive_vchiq_callback(VCHIQ_REASON_T reason,
3755 + VCHIQ_HEADER_T *header,
3756 + VCHIQ_SERVICE_HANDLE_T service_user,
3759 + vchiq_log_error(vchiq_susp_log_level,
3760 + "%s callback reason %d", __func__, reason);
3765 +vchiq_keepalive_thread_func(void *v)
3767 + VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
3768 + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
3770 + VCHIQ_STATUS_T status;
3771 + VCHIQ_INSTANCE_T instance;
3772 + VCHIQ_SERVICE_HANDLE_T ka_handle;
3774 + VCHIQ_SERVICE_PARAMS_T params = {
3775 + .fourcc = VCHIQ_MAKE_FOURCC('K', 'E', 'E', 'P'),
3776 + .callback = vchiq_keepalive_vchiq_callback,
3777 + .version = KEEPALIVE_VER,
3778 + .version_min = KEEPALIVE_VER_MIN
3781 + status = vchiq_initialise(&instance);
3782 + if (status != VCHIQ_SUCCESS) {
3783 + vchiq_log_error(vchiq_susp_log_level,
3784 + "%s vchiq_initialise failed %d", __func__, status);
3788 + status = vchiq_connect(instance);
3789 + if (status != VCHIQ_SUCCESS) {
3790 + vchiq_log_error(vchiq_susp_log_level,
3791 + "%s vchiq_connect failed %d", __func__, status);
3795 + status = vchiq_add_service(instance, ¶ms, &ka_handle);
3796 + if (status != VCHIQ_SUCCESS) {
3797 + vchiq_log_error(vchiq_susp_log_level,
3798 + "%s vchiq_open_service failed %d", __func__, status);
3803 + long rc = 0, uc = 0;
3804 + if (wait_for_completion_interruptible(&arm_state->ka_evt)
3806 + vchiq_log_error(vchiq_susp_log_level,
3807 + "%s interrupted", __func__);
3808 + flush_signals(current);
3812 + /* read and clear counters. Do release_count then use_count to
3813 + * prevent getting more releases than uses */
3814 + rc = atomic_xchg(&arm_state->ka_release_count, 0);
3815 + uc = atomic_xchg(&arm_state->ka_use_count, 0);
3817 + /* Call use/release service the requisite number of times.
3818 + * Process use before release so use counts don't go negative */
3820 + atomic_inc(&arm_state->ka_use_ack_count);
3821 + status = vchiq_use_service(ka_handle);
3822 + if (status != VCHIQ_SUCCESS) {
3823 + vchiq_log_error(vchiq_susp_log_level,
3824 + "%s vchiq_use_service error %d",
3825 + __func__, status);
3829 + status = vchiq_release_service(ka_handle);
3830 + if (status != VCHIQ_SUCCESS) {
3831 + vchiq_log_error(vchiq_susp_log_level,
3832 + "%s vchiq_release_service error %d",
3833 + __func__, status);
3839 + vchiq_shutdown(instance);
3847 +vchiq_arm_init_state(VCHIQ_STATE_T *state, VCHIQ_ARM_STATE_T *arm_state)
3849 + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
3852 + rwlock_init(&arm_state->susp_res_lock);
3854 + init_completion(&arm_state->ka_evt);
3855 + atomic_set(&arm_state->ka_use_count, 0);
3856 + atomic_set(&arm_state->ka_use_ack_count, 0);
3857 + atomic_set(&arm_state->ka_release_count, 0);
3859 + init_completion(&arm_state->vc_suspend_complete);
3861 + init_completion(&arm_state->vc_resume_complete);
3862 + /* Initialise to 'done' state. We only want to block on resume
3863 + * completion while videocore is suspended. */
3864 + set_resume_state(arm_state, VC_RESUME_RESUMED);
3866 + init_completion(&arm_state->resume_blocker);
3867 + /* Initialise to 'done' state. We only want to block on this
3868 + * completion while resume is blocked */
3869 + complete_all(&arm_state->resume_blocker);
3871 + init_completion(&arm_state->blocked_blocker);
3872 + /* Initialise to 'done' state. We only want to block on this
3873 + * completion while things are waiting on the resume blocker */
3874 + complete_all(&arm_state->blocked_blocker);
3876 + arm_state->suspend_timer_timeout = SUSPEND_TIMER_TIMEOUT_MS;
3877 + arm_state->suspend_timer_running = 0;
3878 + init_timer(&arm_state->suspend_timer);
3879 + arm_state->suspend_timer.data = (unsigned long)(state);
3880 + arm_state->suspend_timer.function = suspend_timer_callback;
3882 + arm_state->first_connect = 0;
3889 +** Functions to modify the state variables;
3890 +** set_suspend_state
3891 +** set_resume_state
3893 +** There are more state variables than we might like, so ensure they remain in
3894 +** step. Suspend and resume state are maintained separately, since most of
3895 +** these state machines can operate independently. However, there are a few
3896 +** states where state transitions in one state machine cause a reset to the
3897 +** other state machine. In addition, there are some completion events which
3898 +** need to occur on state machine reset and end-state(s), so these are also
3899 +** dealt with in these functions.
3901 +** In all states we set the state variable according to the input, but in some
3902 +** cases we perform additional steps outlined below;
3904 +** VC_SUSPEND_IDLE - Initialise the suspend completion at the same time.
3905 +** The suspend completion is completed after any suspend
3906 +** attempt. When we reset the state machine we also reset
3907 +** the completion. This reset occurs when videocore is
3908 +** resumed, and also if we initiate suspend after a suspend
3911 +** VC_SUSPEND_IN_PROGRESS - This state is considered the point of no return for
3912 +** suspend - ie from this point on we must try to suspend
3913 +** before resuming can occur. We therefore also reset the
3914 +** resume state machine to VC_RESUME_IDLE in this state.
3916 +** VC_SUSPEND_SUSPENDED - Suspend has completed successfully. Also call
3917 +** complete_all on the suspend completion to notify
3918 +** anything waiting for suspend to happen.
3920 +** VC_SUSPEND_REJECTED - Videocore rejected suspend. Videocore will also
3921 +** initiate resume, so no need to alter resume state.
3922 +** We call complete_all on the suspend completion to notify
3923 +** of suspend rejection.
3925 +** VC_SUSPEND_FAILED - We failed to initiate videocore suspend. We notify the
3926 +** suspend completion and reset the resume state machine.
3928 +** VC_RESUME_IDLE - Initialise the resume completion at the same time. The
3929 +** resume completion is in it's 'done' state whenever
3930 +** videcore is running. Therfore, the VC_RESUME_IDLE state
3931 +** implies that videocore is suspended.
3932 +** Hence, any thread which needs to wait until videocore is
3933 +** running can wait on this completion - it will only block
3934 +** if videocore is suspended.
3936 +** VC_RESUME_RESUMED - Resume has completed successfully. Videocore is running.
3937 +** Call complete_all on the resume completion to unblock
3938 +** any threads waiting for resume. Also reset the suspend
3939 +** state machine to it's idle state.
3941 +** VC_RESUME_FAILED - Currently unused - no mechanism to fail resume exists.
3945 +set_suspend_state(VCHIQ_ARM_STATE_T *arm_state,
3946 + enum vc_suspend_status new_state)
3948 + /* set the state in all cases */
3949 + arm_state->vc_suspend_state = new_state;
3951 + /* state specific additional actions */
3952 + switch (new_state) {
3953 + case VC_SUSPEND_FORCE_CANCELED:
3954 + complete_all(&arm_state->vc_suspend_complete);
3956 + case VC_SUSPEND_REJECTED:
3957 + complete_all(&arm_state->vc_suspend_complete);
3959 + case VC_SUSPEND_FAILED:
3960 + complete_all(&arm_state->vc_suspend_complete);
3961 + arm_state->vc_resume_state = VC_RESUME_RESUMED;
3962 + complete_all(&arm_state->vc_resume_complete);
3964 + case VC_SUSPEND_IDLE:
3965 + reinit_completion(&arm_state->vc_suspend_complete);
3967 + case VC_SUSPEND_REQUESTED:
3969 + case VC_SUSPEND_IN_PROGRESS:
3970 + set_resume_state(arm_state, VC_RESUME_IDLE);
3972 + case VC_SUSPEND_SUSPENDED:
3973 + complete_all(&arm_state->vc_suspend_complete);
3982 +set_resume_state(VCHIQ_ARM_STATE_T *arm_state,
3983 + enum vc_resume_status new_state)
3985 + /* set the state in all cases */
3986 + arm_state->vc_resume_state = new_state;
3988 + /* state specific additional actions */
3989 + switch (new_state) {
3990 + case VC_RESUME_FAILED:
3992 + case VC_RESUME_IDLE:
3993 + reinit_completion(&arm_state->vc_resume_complete);
3995 + case VC_RESUME_REQUESTED:
3997 + case VC_RESUME_IN_PROGRESS:
3999 + case VC_RESUME_RESUMED:
4000 + complete_all(&arm_state->vc_resume_complete);
4001 + set_suspend_state(arm_state, VC_SUSPEND_IDLE);
4010 +/* should be called with the write lock held */
4012 +start_suspend_timer(VCHIQ_ARM_STATE_T *arm_state)
4014 + del_timer(&arm_state->suspend_timer);
4015 + arm_state->suspend_timer.expires = jiffies +
4016 + msecs_to_jiffies(arm_state->
4017 + suspend_timer_timeout);
4018 + add_timer(&arm_state->suspend_timer);
4019 + arm_state->suspend_timer_running = 1;
4022 +/* should be called with the write lock held */
4024 +stop_suspend_timer(VCHIQ_ARM_STATE_T *arm_state)
4026 + if (arm_state->suspend_timer_running) {
4027 + del_timer(&arm_state->suspend_timer);
4028 + arm_state->suspend_timer_running = 0;
4033 +need_resume(VCHIQ_STATE_T *state)
4035 + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
4036 + return (arm_state->vc_suspend_state > VC_SUSPEND_IDLE) &&
4037 + (arm_state->vc_resume_state < VC_RESUME_REQUESTED) &&
4038 + vchiq_videocore_wanted(state);
4042 +block_resume(VCHIQ_ARM_STATE_T *arm_state)
4044 + int status = VCHIQ_SUCCESS;
4045 + const unsigned long timeout_val =
4046 + msecs_to_jiffies(FORCE_SUSPEND_TIMEOUT_MS);
4047 + int resume_count = 0;
4049 + /* Allow any threads which were blocked by the last force suspend to
4050 + * complete if they haven't already. Only give this one shot; if
4051 + * blocked_count is incremented after blocked_blocker is completed
4052 + * (which only happens when blocked_count hits 0) then those threads
4053 + * will have to wait until next time around */
4054 + if (arm_state->blocked_count) {
4055 + reinit_completion(&arm_state->blocked_blocker);
4056 + write_unlock_bh(&arm_state->susp_res_lock);
4057 + vchiq_log_info(vchiq_susp_log_level, "%s wait for previously "
4058 + "blocked clients", __func__);
4059 + if (wait_for_completion_interruptible_timeout(
4060 + &arm_state->blocked_blocker, timeout_val)
4062 + vchiq_log_error(vchiq_susp_log_level, "%s wait for "
4063 + "previously blocked clients failed" , __func__);
4064 + status = VCHIQ_ERROR;
4065 + write_lock_bh(&arm_state->susp_res_lock);
4068 + vchiq_log_info(vchiq_susp_log_level, "%s previously blocked "
4069 + "clients resumed", __func__);
4070 + write_lock_bh(&arm_state->susp_res_lock);
4073 + /* We need to wait for resume to complete if it's in process */
4074 + while (arm_state->vc_resume_state != VC_RESUME_RESUMED &&
4075 + arm_state->vc_resume_state > VC_RESUME_IDLE) {
4076 + if (resume_count > 1) {
4077 + status = VCHIQ_ERROR;
4078 + vchiq_log_error(vchiq_susp_log_level, "%s waited too "
4079 + "many times for resume" , __func__);
4082 + write_unlock_bh(&arm_state->susp_res_lock);
4083 + vchiq_log_info(vchiq_susp_log_level, "%s wait for resume",
4085 + if (wait_for_completion_interruptible_timeout(
4086 + &arm_state->vc_resume_complete, timeout_val)
4088 + vchiq_log_error(vchiq_susp_log_level, "%s wait for "
4089 + "resume failed (%s)", __func__,
4090 + resume_state_names[arm_state->vc_resume_state +
4091 + VC_RESUME_NUM_OFFSET]);
4092 + status = VCHIQ_ERROR;
4093 + write_lock_bh(&arm_state->susp_res_lock);
4096 + vchiq_log_info(vchiq_susp_log_level, "%s resumed", __func__);
4097 + write_lock_bh(&arm_state->susp_res_lock);
4100 + reinit_completion(&arm_state->resume_blocker);
4101 + arm_state->resume_blocked = 1;
4108 +unblock_resume(VCHIQ_ARM_STATE_T *arm_state)
4110 + complete_all(&arm_state->resume_blocker);
4111 + arm_state->resume_blocked = 0;
4114 +/* Initiate suspend via slot handler. Should be called with the write lock
4117 +vchiq_arm_vcsuspend(VCHIQ_STATE_T *state)
4119 + VCHIQ_STATUS_T status = VCHIQ_ERROR;
4120 + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
4125 + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
4126 + status = VCHIQ_SUCCESS;
4129 + switch (arm_state->vc_suspend_state) {
4130 + case VC_SUSPEND_REQUESTED:
4131 + vchiq_log_info(vchiq_susp_log_level, "%s: suspend already "
4132 + "requested", __func__);
4134 + case VC_SUSPEND_IN_PROGRESS:
4135 + vchiq_log_info(vchiq_susp_log_level, "%s: suspend already in "
4136 + "progress", __func__);
4140 + /* We don't expect to be in other states, so log but continue
4142 + vchiq_log_error(vchiq_susp_log_level,
4143 + "%s unexpected suspend state %s", __func__,
4144 + suspend_state_names[arm_state->vc_suspend_state +
4145 + VC_SUSPEND_NUM_OFFSET]);
4146 + /* fall through */
4147 + case VC_SUSPEND_REJECTED:
4148 + case VC_SUSPEND_FAILED:
4149 + /* Ensure any idle state actions have been run */
4150 + set_suspend_state(arm_state, VC_SUSPEND_IDLE);
4151 + /* fall through */
4152 + case VC_SUSPEND_IDLE:
4153 + vchiq_log_info(vchiq_susp_log_level,
4154 + "%s: suspending", __func__);
4155 + set_suspend_state(arm_state, VC_SUSPEND_REQUESTED);
4156 + /* kick the slot handler thread to initiate suspend */
4157 + request_poll(state, NULL, 0);
4162 + vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, status);
4167 +vchiq_platform_check_suspend(VCHIQ_STATE_T *state)
4169 + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
4175 + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
4177 + write_lock_bh(&arm_state->susp_res_lock);
4178 + if (arm_state->vc_suspend_state == VC_SUSPEND_REQUESTED &&
4179 + arm_state->vc_resume_state == VC_RESUME_RESUMED) {
4180 + set_suspend_state(arm_state, VC_SUSPEND_IN_PROGRESS);
4183 + write_unlock_bh(&arm_state->susp_res_lock);
4186 + vchiq_platform_suspend(state);
4189 + vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
4195 +output_timeout_error(VCHIQ_STATE_T *state)
4197 + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
4198 + char service_err[50] = "";
4199 + int vc_use_count = arm_state->videocore_use_count;
4200 + int active_services = state->unused_service;
4203 + if (!arm_state->videocore_use_count) {
4204 + snprintf(service_err, 50, " Videocore usecount is 0");
4207 + for (i = 0; i < active_services; i++) {
4208 + VCHIQ_SERVICE_T *service_ptr = state->services[i];
4209 + if (service_ptr && service_ptr->service_use_count &&
4210 + (service_ptr->srvstate != VCHIQ_SRVSTATE_FREE)) {
4211 + snprintf(service_err, 50, " %c%c%c%c(%d) service has "
4212 + "use count %d%s", VCHIQ_FOURCC_AS_4CHARS(
4213 + service_ptr->base.fourcc),
4214 + service_ptr->client_id,
4215 + service_ptr->service_use_count,
4216 + service_ptr->service_use_count ==
4217 + vc_use_count ? "" : " (+ more)");
4223 + vchiq_log_error(vchiq_susp_log_level,
4224 + "timed out waiting for vc suspend (%d).%s",
4225 + arm_state->autosuspend_override, service_err);
4229 +/* Try to get videocore into suspended state, regardless of autosuspend state.
4230 +** We don't actually force suspend, since videocore may get into a bad state
4231 +** if we force suspend at a bad time. Instead, we wait for autosuspend to
4232 +** determine a good point to suspend. If this doesn't happen within 100ms we
4235 +** Returns VCHIQ_SUCCESS if videocore suspended successfully, VCHIQ_RETRY if
4236 +** videocore failed to suspend in time or VCHIQ_ERROR if interrupted.
4239 +vchiq_arm_force_suspend(VCHIQ_STATE_T *state)
4241 + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
4242 + VCHIQ_STATUS_T status = VCHIQ_ERROR;
4249 + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
4251 + write_lock_bh(&arm_state->susp_res_lock);
4253 + status = block_resume(arm_state);
4254 + if (status != VCHIQ_SUCCESS)
4256 + if (arm_state->vc_suspend_state == VC_SUSPEND_SUSPENDED) {
4257 + /* Already suspended - just block resume and exit */
4258 + vchiq_log_info(vchiq_susp_log_level, "%s already suspended",
4260 + status = VCHIQ_SUCCESS;
4262 + } else if (arm_state->vc_suspend_state <= VC_SUSPEND_IDLE) {
4263 + /* initiate suspend immediately in the case that we're waiting
4264 + * for the timeout */
4265 + stop_suspend_timer(arm_state);
4266 + if (!vchiq_videocore_wanted(state)) {
4267 + vchiq_log_info(vchiq_susp_log_level, "%s videocore "
4268 + "idle, initiating suspend", __func__);
4269 + status = vchiq_arm_vcsuspend(state);
4270 + } else if (arm_state->autosuspend_override <
4271 + FORCE_SUSPEND_FAIL_MAX) {
4272 + vchiq_log_info(vchiq_susp_log_level, "%s letting "
4273 + "videocore go idle", __func__);
4274 + status = VCHIQ_SUCCESS;
4276 + vchiq_log_warning(vchiq_susp_log_level, "%s failed too "
4277 + "many times - attempting suspend", __func__);
4278 + status = vchiq_arm_vcsuspend(state);
4281 + vchiq_log_info(vchiq_susp_log_level, "%s videocore suspend "
4282 + "in progress - wait for completion", __func__);
4283 + status = VCHIQ_SUCCESS;
4286 + /* Wait for suspend to happen due to system idle (not forced..) */
4287 + if (status != VCHIQ_SUCCESS)
4288 + goto unblock_resume;
4291 + write_unlock_bh(&arm_state->susp_res_lock);
4293 + rc = wait_for_completion_interruptible_timeout(
4294 + &arm_state->vc_suspend_complete,
4295 + msecs_to_jiffies(FORCE_SUSPEND_TIMEOUT_MS));
4297 + write_lock_bh(&arm_state->susp_res_lock);
4299 + vchiq_log_warning(vchiq_susp_log_level, "%s "
4300 + "interrupted waiting for suspend", __func__);
4301 + status = VCHIQ_ERROR;
4302 + goto unblock_resume;
4303 + } else if (rc == 0) {
4304 + if (arm_state->vc_suspend_state > VC_SUSPEND_IDLE) {
4305 + /* Repeat timeout once if in progress */
4311 + arm_state->autosuspend_override++;
4312 + output_timeout_error(state);
4314 + status = VCHIQ_RETRY;
4315 + goto unblock_resume;
4317 + } while (0 < (repeat--));
4319 + /* Check and report state in case we need to abort ARM suspend */
4320 + if (arm_state->vc_suspend_state != VC_SUSPEND_SUSPENDED) {
4321 + status = VCHIQ_RETRY;
4322 + vchiq_log_error(vchiq_susp_log_level,
4323 + "%s videocore suspend failed (state %s)", __func__,
4324 + suspend_state_names[arm_state->vc_suspend_state +
4325 + VC_SUSPEND_NUM_OFFSET]);
4326 + /* Reset the state only if it's still in an error state.
4327 + * Something could have already initiated another suspend. */
4328 + if (arm_state->vc_suspend_state < VC_SUSPEND_IDLE)
4329 + set_suspend_state(arm_state, VC_SUSPEND_IDLE);
4331 + goto unblock_resume;
4334 + /* successfully suspended - unlock and exit */
4338 + /* all error states need to unblock resume before exit */
4339 + unblock_resume(arm_state);
4342 + write_unlock_bh(&arm_state->susp_res_lock);
4345 + vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, status);
4350 +vchiq_check_suspend(VCHIQ_STATE_T *state)
4352 + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
4357 + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
4359 + write_lock_bh(&arm_state->susp_res_lock);
4360 + if (arm_state->vc_suspend_state != VC_SUSPEND_SUSPENDED &&
4361 + arm_state->first_connect &&
4362 + !vchiq_videocore_wanted(state)) {
4363 + vchiq_arm_vcsuspend(state);
4365 + write_unlock_bh(&arm_state->susp_res_lock);
4368 + vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
4374 +vchiq_arm_allow_resume(VCHIQ_STATE_T *state)
4376 + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
4383 + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
4385 + write_lock_bh(&arm_state->susp_res_lock);
4386 + unblock_resume(arm_state);
4387 + resume = vchiq_check_resume(state);
4388 + write_unlock_bh(&arm_state->susp_res_lock);
4391 + if (wait_for_completion_interruptible(
4392 + &arm_state->vc_resume_complete) < 0) {
4393 + vchiq_log_error(vchiq_susp_log_level,
4394 + "%s interrupted", __func__);
4395 + /* failed, cannot accurately derive suspend
4396 + * state, so exit early. */
4401 + read_lock_bh(&arm_state->susp_res_lock);
4402 + if (arm_state->vc_suspend_state == VC_SUSPEND_SUSPENDED) {
4403 + vchiq_log_info(vchiq_susp_log_level,
4404 + "%s: Videocore remains suspended", __func__);
4406 + vchiq_log_info(vchiq_susp_log_level,
4407 + "%s: Videocore resumed", __func__);
4410 + read_unlock_bh(&arm_state->susp_res_lock);
4412 + vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, ret);
4416 +/* This function should be called with the write lock held */
4418 +vchiq_check_resume(VCHIQ_STATE_T *state)
4420 + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
4426 + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
4428 + if (need_resume(state)) {
4429 + set_resume_state(arm_state, VC_RESUME_REQUESTED);
4430 + request_poll(state, NULL, 0);
4435 + vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
4440 +vchiq_platform_check_resume(VCHIQ_STATE_T *state)
4442 + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
4448 + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
4450 + write_lock_bh(&arm_state->susp_res_lock);
4451 + if (arm_state->wake_address == 0) {
4452 + vchiq_log_info(vchiq_susp_log_level,
4453 + "%s: already awake", __func__);
4456 + if (arm_state->vc_resume_state == VC_RESUME_IN_PROGRESS) {
4457 + vchiq_log_info(vchiq_susp_log_level,
4458 + "%s: already resuming", __func__);
4462 + if (arm_state->vc_resume_state == VC_RESUME_REQUESTED) {
4463 + set_resume_state(arm_state, VC_RESUME_IN_PROGRESS);
4466 + vchiq_log_trace(vchiq_susp_log_level,
4467 + "%s: not resuming (resume state %s)", __func__,
4468 + resume_state_names[arm_state->vc_resume_state +
4469 + VC_RESUME_NUM_OFFSET]);
4472 + write_unlock_bh(&arm_state->susp_res_lock);
4475 + vchiq_platform_resume(state);
4478 + vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
4486 +vchiq_use_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service,
4487 + enum USE_TYPE_E use_type)
4489 + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
4490 + VCHIQ_STATUS_T ret = VCHIQ_SUCCESS;
4493 + int local_uc, local_entity_uc;
4498 + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
4500 + if (use_type == USE_TYPE_VCHIQ) {
4501 + sprintf(entity, "VCHIQ: ");
4502 + entity_uc = &arm_state->peer_use_count;
4503 + } else if (service) {
4504 + sprintf(entity, "%c%c%c%c:%03d",
4505 + VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
4506 + service->client_id);
4507 + entity_uc = &service->service_use_count;
4509 + vchiq_log_error(vchiq_susp_log_level, "%s null service "
4511 + ret = VCHIQ_ERROR;
4515 + write_lock_bh(&arm_state->susp_res_lock);
4516 + while (arm_state->resume_blocked) {
4517 + /* If we call 'use' while force suspend is waiting for suspend,
4518 + * then we're about to block the thread which the force is
4519 + * waiting to complete, so we're bound to just time out. In this
4520 + * case, set the suspend state such that the wait will be
4521 + * canceled, so we can complete as quickly as possible. */
4522 + if (arm_state->resume_blocked && arm_state->vc_suspend_state ==
4523 + VC_SUSPEND_IDLE) {
4524 + set_suspend_state(arm_state, VC_SUSPEND_FORCE_CANCELED);
4527 + /* If suspend is already in progress then we need to block */
4528 + if (!try_wait_for_completion(&arm_state->resume_blocker)) {
4529 + /* Indicate that there are threads waiting on the resume
4530 + * blocker. These need to be allowed to complete before
4531 + * a _second_ call to force suspend can complete,
4532 + * otherwise low priority threads might never actually
4534 + arm_state->blocked_count++;
4535 + write_unlock_bh(&arm_state->susp_res_lock);
4536 + vchiq_log_info(vchiq_susp_log_level, "%s %s resume "
4537 + "blocked - waiting...", __func__, entity);
4538 + if (wait_for_completion_killable(
4539 + &arm_state->resume_blocker) != 0) {
4540 + vchiq_log_error(vchiq_susp_log_level, "%s %s "
4541 + "wait for resume blocker interrupted",
4542 + __func__, entity);
4543 + ret = VCHIQ_ERROR;
4544 + write_lock_bh(&arm_state->susp_res_lock);
4545 + arm_state->blocked_count--;
4546 + write_unlock_bh(&arm_state->susp_res_lock);
4549 + vchiq_log_info(vchiq_susp_log_level, "%s %s resume "
4550 + "unblocked", __func__, entity);
4551 + write_lock_bh(&arm_state->susp_res_lock);
4552 + if (--arm_state->blocked_count == 0)
4553 + complete_all(&arm_state->blocked_blocker);
4557 + stop_suspend_timer(arm_state);
4559 + local_uc = ++arm_state->videocore_use_count;
4560 + local_entity_uc = ++(*entity_uc);
4562 + /* If there's a pending request which hasn't yet been serviced then
4563 + * just clear it. If we're past VC_SUSPEND_REQUESTED state then
4564 + * vc_resume_complete will block until we either resume or fail to
4566 + if (arm_state->vc_suspend_state <= VC_SUSPEND_REQUESTED)
4567 + set_suspend_state(arm_state, VC_SUSPEND_IDLE);
4569 + if ((use_type != USE_TYPE_SERVICE_NO_RESUME) && need_resume(state)) {
4570 + set_resume_state(arm_state, VC_RESUME_REQUESTED);
4571 + vchiq_log_info(vchiq_susp_log_level,
4572 + "%s %s count %d, state count %d",
4573 + __func__, entity, local_entity_uc, local_uc);
4574 + request_poll(state, NULL, 0);
4576 + vchiq_log_trace(vchiq_susp_log_level,
4577 + "%s %s count %d, state count %d",
4578 + __func__, entity, *entity_uc, local_uc);
4581 + write_unlock_bh(&arm_state->susp_res_lock);
4583 + /* Completion is in a done state when we're not suspended, so this won't
4584 + * block for the non-suspended case. */
4585 + if (!try_wait_for_completion(&arm_state->vc_resume_complete)) {
4586 + vchiq_log_info(vchiq_susp_log_level, "%s %s wait for resume",
4587 + __func__, entity);
4588 + if (wait_for_completion_killable(
4589 + &arm_state->vc_resume_complete) != 0) {
4590 + vchiq_log_error(vchiq_susp_log_level, "%s %s wait for "
4591 + "resume interrupted", __func__, entity);
4592 + ret = VCHIQ_ERROR;
4595 + vchiq_log_info(vchiq_susp_log_level, "%s %s resumed", __func__,
4599 + if (ret == VCHIQ_SUCCESS) {
4600 + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
4601 + long ack_cnt = atomic_xchg(&arm_state->ka_use_ack_count, 0);
4602 + while (ack_cnt && (status == VCHIQ_SUCCESS)) {
4603 + /* Send the use notify to videocore */
4604 + status = vchiq_send_remote_use_active(state);
4605 + if (status == VCHIQ_SUCCESS)
4608 + atomic_add(ack_cnt,
4609 + &arm_state->ka_use_ack_count);
4614 + vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, ret);
4619 +vchiq_release_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service)
4621 + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
4622 + VCHIQ_STATUS_T ret = VCHIQ_SUCCESS;
4625 + int local_uc, local_entity_uc;
4630 + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
4633 + sprintf(entity, "%c%c%c%c:%03d",
4634 + VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
4635 + service->client_id);
4636 + entity_uc = &service->service_use_count;
4638 + sprintf(entity, "PEER: ");
4639 + entity_uc = &arm_state->peer_use_count;
4642 + write_lock_bh(&arm_state->susp_res_lock);
4643 + if (!arm_state->videocore_use_count || !(*entity_uc)) {
4644 + /* Don't use BUG_ON - don't allow user thread to crash kernel */
4645 + WARN_ON(!arm_state->videocore_use_count);
4646 + WARN_ON(!(*entity_uc));
4647 + ret = VCHIQ_ERROR;
4650 + local_uc = --arm_state->videocore_use_count;
4651 + local_entity_uc = --(*entity_uc);
4653 + if (!vchiq_videocore_wanted(state)) {
4654 + if (vchiq_platform_use_suspend_timer() &&
4655 + !arm_state->resume_blocked) {
4656 + /* Only use the timer if we're not trying to force
4657 + * suspend (=> resume_blocked) */
4658 + start_suspend_timer(arm_state);
4660 + vchiq_log_info(vchiq_susp_log_level,
4661 + "%s %s count %d, state count %d - suspending",
4662 + __func__, entity, *entity_uc,
4663 + arm_state->videocore_use_count);
4664 + vchiq_arm_vcsuspend(state);
4667 + vchiq_log_trace(vchiq_susp_log_level,
4668 + "%s %s count %d, state count %d",
4669 + __func__, entity, *entity_uc,
4670 + arm_state->videocore_use_count);
4673 + write_unlock_bh(&arm_state->susp_res_lock);
4676 + vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, ret);
4681 +vchiq_on_remote_use(VCHIQ_STATE_T *state)
4683 + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
4684 + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
4685 + atomic_inc(&arm_state->ka_use_count);
4686 + complete(&arm_state->ka_evt);
4690 +vchiq_on_remote_release(VCHIQ_STATE_T *state)
4692 + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
4693 + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
4694 + atomic_inc(&arm_state->ka_release_count);
4695 + complete(&arm_state->ka_evt);
4699 +vchiq_use_service_internal(VCHIQ_SERVICE_T *service)
4701 + return vchiq_use_internal(service->state, service, USE_TYPE_SERVICE);
4705 +vchiq_release_service_internal(VCHIQ_SERVICE_T *service)
4707 + return vchiq_release_internal(service->state, service);
4710 +static void suspend_timer_callback(unsigned long context)
4712 + VCHIQ_STATE_T *state = (VCHIQ_STATE_T *)context;
4713 + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
4716 + vchiq_log_info(vchiq_susp_log_level,
4717 + "%s - suspend timer expired - check suspend", __func__);
4718 + vchiq_check_suspend(state);
4724 +vchiq_use_service_no_resume(VCHIQ_SERVICE_HANDLE_T handle)
4726 + VCHIQ_STATUS_T ret = VCHIQ_ERROR;
4727 + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
4729 + ret = vchiq_use_internal(service->state, service,
4730 + USE_TYPE_SERVICE_NO_RESUME);
4731 + unlock_service(service);
4737 +vchiq_use_service(VCHIQ_SERVICE_HANDLE_T handle)
4739 + VCHIQ_STATUS_T ret = VCHIQ_ERROR;
4740 + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
4742 + ret = vchiq_use_internal(service->state, service,
4743 + USE_TYPE_SERVICE);
4744 + unlock_service(service);
4750 +vchiq_release_service(VCHIQ_SERVICE_HANDLE_T handle)
4752 + VCHIQ_STATUS_T ret = VCHIQ_ERROR;
4753 + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
4755 + ret = vchiq_release_internal(service->state, service);
4756 + unlock_service(service);
4762 +vchiq_dump_service_use_state(VCHIQ_STATE_T *state)
4764 + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
4766 + /* Only dump 64 services */
4767 + static const int local_max_services = 64;
4768 + /* If there's more than 64 services, only dump ones with
4769 + * non-zero counts */
4770 + int only_nonzero = 0;
4771 + static const char *nz = "<-- preventing suspend";
4773 + enum vc_suspend_status vc_suspend_state;
4774 + enum vc_resume_status vc_resume_state;
4777 + int active_services;
4778 + struct service_data_struct {
4782 + } service_data[local_max_services];
4787 + read_lock_bh(&arm_state->susp_res_lock);
4788 + vc_suspend_state = arm_state->vc_suspend_state;
4789 + vc_resume_state = arm_state->vc_resume_state;
4790 + peer_count = arm_state->peer_use_count;
4791 + vc_use_count = arm_state->videocore_use_count;
4792 + active_services = state->unused_service;
4793 + if (active_services > local_max_services)
4796 + for (i = 0; (i < active_services) && (j < local_max_services); i++) {
4797 + VCHIQ_SERVICE_T *service_ptr = state->services[i];
4801 + if (only_nonzero && !service_ptr->service_use_count)
4804 + if (service_ptr->srvstate != VCHIQ_SRVSTATE_FREE) {
4805 + service_data[j].fourcc = service_ptr->base.fourcc;
4806 + service_data[j].clientid = service_ptr->client_id;
4807 + service_data[j++].use_count = service_ptr->
4808 + service_use_count;
4812 + read_unlock_bh(&arm_state->susp_res_lock);
4814 + vchiq_log_warning(vchiq_susp_log_level,
4815 + "-- Videcore suspend state: %s --",
4816 + suspend_state_names[vc_suspend_state + VC_SUSPEND_NUM_OFFSET]);
4817 + vchiq_log_warning(vchiq_susp_log_level,
4818 + "-- Videcore resume state: %s --",
4819 + resume_state_names[vc_resume_state + VC_RESUME_NUM_OFFSET]);
4822 + vchiq_log_warning(vchiq_susp_log_level, "Too many active "
4823 + "services (%d). Only dumping up to first %d services "
4824 + "with non-zero use-count", active_services,
4825 + local_max_services);
4827 + for (i = 0; i < j; i++) {
4828 + vchiq_log_warning(vchiq_susp_log_level,
4829 + "----- %c%c%c%c:%d service count %d %s",
4830 + VCHIQ_FOURCC_AS_4CHARS(service_data[i].fourcc),
4831 + service_data[i].clientid,
4832 + service_data[i].use_count,
4833 + service_data[i].use_count ? nz : "");
4835 + vchiq_log_warning(vchiq_susp_log_level,
4836 + "----- VCHIQ use count count %d", peer_count);
4837 + vchiq_log_warning(vchiq_susp_log_level,
4838 + "--- Overall vchiq instance use count %d", vc_use_count);
4840 + vchiq_dump_platform_use_state(state);
4844 +vchiq_check_service(VCHIQ_SERVICE_T *service)
4846 + VCHIQ_ARM_STATE_T *arm_state;
4847 + VCHIQ_STATUS_T ret = VCHIQ_ERROR;
4849 + if (!service || !service->state)
4852 + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
4854 + arm_state = vchiq_platform_get_arm_state(service->state);
4856 + read_lock_bh(&arm_state->susp_res_lock);
4857 + if (service->service_use_count)
4858 + ret = VCHIQ_SUCCESS;
4859 + read_unlock_bh(&arm_state->susp_res_lock);
4861 + if (ret == VCHIQ_ERROR) {
4862 + vchiq_log_error(vchiq_susp_log_level,
4863 + "%s ERROR - %c%c%c%c:%d service count %d, "
4864 + "state count %d, videocore suspend state %s", __func__,
4865 + VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
4866 + service->client_id, service->service_use_count,
4867 + arm_state->videocore_use_count,
4868 + suspend_state_names[arm_state->vc_suspend_state +
4869 + VC_SUSPEND_NUM_OFFSET]);
4870 + vchiq_dump_service_use_state(service->state);
4876 +/* stub functions */
4877 +void vchiq_on_remote_use_active(VCHIQ_STATE_T *state)
4882 +void vchiq_platform_conn_state_changed(VCHIQ_STATE_T *state,
4883 + VCHIQ_CONNSTATE_T oldstate, VCHIQ_CONNSTATE_T newstate)
4885 + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
4886 + vchiq_log_info(vchiq_susp_log_level, "%d: %s->%s", state->id,
4887 + get_conn_state_name(oldstate), get_conn_state_name(newstate));
4888 + if (state->conn_state == VCHIQ_CONNSTATE_CONNECTED) {
4889 + write_lock_bh(&arm_state->susp_res_lock);
4890 + if (!arm_state->first_connect) {
4891 + char threadname[10];
4892 + arm_state->first_connect = 1;
4893 + write_unlock_bh(&arm_state->susp_res_lock);
4894 + snprintf(threadname, sizeof(threadname), "VCHIQka-%d",
4896 + arm_state->ka_thread = kthread_create(
4897 + &vchiq_keepalive_thread_func,
4900 + if (arm_state->ka_thread == NULL) {
4901 + vchiq_log_error(vchiq_susp_log_level,
4902 + "vchiq: FATAL: couldn't create thread %s",
4905 + wake_up_process(arm_state->ka_thread);
4908 + write_unlock_bh(&arm_state->susp_res_lock);
4913 +/****************************************************************************
4915 +* vchiq_init - called when the module is loaded.
4917 +***************************************************************************/
4925 + /* create proc entries */
4926 + err = vchiq_proc_init();
4928 + goto failed_proc_init;
4930 + err = alloc_chrdev_region(&vchiq_devid, VCHIQ_MINOR, 1, DEVICE_NAME);
4932 + vchiq_log_error(vchiq_arm_log_level,
4933 + "Unable to allocate device number");
4934 + goto failed_alloc_chrdev;
4936 + cdev_init(&vchiq_cdev, &vchiq_fops);
4937 + vchiq_cdev.owner = THIS_MODULE;
4938 + err = cdev_add(&vchiq_cdev, vchiq_devid, 1);
4940 + vchiq_log_error(vchiq_arm_log_level,
4941 + "Unable to register device");
4942 + goto failed_cdev_add;
4945 + /* create sysfs entries */
4946 + vchiq_class = class_create(THIS_MODULE, DEVICE_NAME);
4947 + ptr_err = vchiq_class;
4948 + if (IS_ERR(ptr_err))
4949 + goto failed_class_create;
4951 + vchiq_dev = device_create(vchiq_class, NULL,
4952 + vchiq_devid, NULL, "vchiq");
4953 + ptr_err = vchiq_dev;
4954 + if (IS_ERR(ptr_err))
4955 + goto failed_device_create;
4957 + err = vchiq_platform_init(&g_state);
4959 + goto failed_platform_init;
4961 + vchiq_log_info(vchiq_arm_log_level,
4962 + "vchiq: initialised - version %d (min %d), device %d.%d",
4963 + VCHIQ_VERSION, VCHIQ_VERSION_MIN,
4964 + MAJOR(vchiq_devid), MINOR(vchiq_devid));
4968 +failed_platform_init:
4969 + device_destroy(vchiq_class, vchiq_devid);
4970 +failed_device_create:
4971 + class_destroy(vchiq_class);
4972 +failed_class_create:
4973 + cdev_del(&vchiq_cdev);
4974 + err = PTR_ERR(ptr_err);
4976 + unregister_chrdev_region(vchiq_devid, 1);
4977 +failed_alloc_chrdev:
4978 + vchiq_proc_deinit();
4980 + vchiq_log_warning(vchiq_arm_log_level, "could not load vchiq");
4984 +static int vchiq_instance_get_use_count(VCHIQ_INSTANCE_T instance)
4986 + VCHIQ_SERVICE_T *service;
4987 + int use_count = 0, i;
4989 + while ((service = next_service_by_instance(instance->state,
4990 + instance, &i)) != NULL) {
4991 + use_count += service->service_use_count;
4992 + unlock_service(service);
4997 +/* read the per-process use-count */
4998 +static int proc_read_use_count(char *page, char **start,
4999 + off_t off, int count,
5000 + int *eof, void *data)
5002 + VCHIQ_INSTANCE_T instance = data;
5003 + int len, use_count;
5005 + use_count = vchiq_instance_get_use_count(instance);
5006 + len = snprintf(page+off, count, "%d\n", use_count);
5011 +/* add an instance (process) to the proc entries */
5012 +static int vchiq_proc_add_instance(VCHIQ_INSTANCE_T instance)
5018 + struct proc_dir_entry *top, *use_count;
5019 + struct proc_dir_entry *clients = vchiq_clients_top();
5020 + int pid = instance->pid;
5022 + snprintf(pidstr, sizeof(pidstr), "%d", pid);
5023 + top = proc_mkdir(pidstr, clients);
5027 + use_count = create_proc_read_entry("use_count",
5029 + proc_read_use_count,
5032 + goto fail_use_count;
5034 + instance->proc_entry = top;
5039 + remove_proc_entry(top->name, clients);
5045 +static void vchiq_proc_remove_instance(VCHIQ_INSTANCE_T instance)
5048 + struct proc_dir_entry *clients = vchiq_clients_top();
5049 + remove_proc_entry("use_count", instance->proc_entry);
5050 + remove_proc_entry(instance->proc_entry->name, clients);
5054 +/****************************************************************************
5056 +* vchiq_exit - called when the module is unloaded.
5058 +***************************************************************************/
5063 + vchiq_platform_exit(&g_state);
5064 + device_destroy(vchiq_class, vchiq_devid);
5065 + class_destroy(vchiq_class);
5066 + cdev_del(&vchiq_cdev);
5067 + unregister_chrdev_region(vchiq_devid, 1);
5070 +module_init(vchiq_init);
5071 +module_exit(vchiq_exit);
5072 +MODULE_LICENSE("GPL");
5073 +MODULE_AUTHOR("Broadcom Corporation");
5074 diff --git a/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.h b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.h
5075 new file mode 100644
5076 index 0000000..75ad4c6
5078 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.h
5081 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
5083 + * Redistribution and use in source and binary forms, with or without
5084 + * modification, are permitted provided that the following conditions
5086 + * 1. Redistributions of source code must retain the above copyright
5087 + * notice, this list of conditions, and the following disclaimer,
5088 + * without modification.
5089 + * 2. Redistributions in binary form must reproduce the above copyright
5090 + * notice, this list of conditions and the following disclaimer in the
5091 + * documentation and/or other materials provided with the distribution.
5092 + * 3. The names of the above-listed copyright holders may not be used
5093 + * to endorse or promote products derived from this software without
5094 + * specific prior written permission.
5096 + * ALTERNATIVELY, this software may be distributed under the terms of the
5097 + * GNU General Public License ("GPL") version 2, as published by the Free
5098 + * Software Foundation.
5100 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
5101 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
5102 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
5103 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
5104 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
5105 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
5106 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
5107 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
5108 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
5109 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
5110 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5113 +#ifndef VCHIQ_ARM_H
5114 +#define VCHIQ_ARM_H
5116 +#include <linux/mutex.h>
5117 +#include <linux/semaphore.h>
5118 +#include <linux/atomic.h>
5119 +#include "vchiq_core.h"
5122 +enum vc_suspend_status {
5123 + VC_SUSPEND_FORCE_CANCELED = -3, /* Force suspend canceled, too busy */
5124 + VC_SUSPEND_REJECTED = -2, /* Videocore rejected suspend request */
5125 + VC_SUSPEND_FAILED = -1, /* Videocore suspend failed */
5126 + VC_SUSPEND_IDLE = 0, /* VC active, no suspend actions */
5127 + VC_SUSPEND_REQUESTED, /* User has requested suspend */
5128 + VC_SUSPEND_IN_PROGRESS, /* Slot handler has recvd suspend request */
5129 + VC_SUSPEND_SUSPENDED /* Videocore suspend succeeded */
5132 +enum vc_resume_status {
5133 + VC_RESUME_FAILED = -1, /* Videocore resume failed */
5134 + VC_RESUME_IDLE = 0, /* VC suspended, no resume actions */
5135 + VC_RESUME_REQUESTED, /* User has requested resume */
5136 + VC_RESUME_IN_PROGRESS, /* Slot handler has received resume request */
5137 + VC_RESUME_RESUMED /* Videocore resumed successfully (active) */
5143 + USE_TYPE_SERVICE_NO_RESUME,
5149 +typedef struct vchiq_arm_state_struct {
5150 + /* Keepalive-related data */
5151 + struct task_struct *ka_thread;
5152 + struct completion ka_evt;
5153 + atomic_t ka_use_count;
5154 + atomic_t ka_use_ack_count;
5155 + atomic_t ka_release_count;
5157 + struct completion vc_suspend_complete;
5158 + struct completion vc_resume_complete;
5160 + rwlock_t susp_res_lock;
5161 + enum vc_suspend_status vc_suspend_state;
5162 + enum vc_resume_status vc_resume_state;
5164 + unsigned int wake_address;
5166 + struct timer_list suspend_timer;
5167 + int suspend_timer_timeout;
5168 + int suspend_timer_running;
5170 + /* Global use count for videocore.
5171 + ** This is equal to the sum of the use counts for all services. When
5172 + ** this hits zero the videocore suspend procedure will be initiated.
5174 + int videocore_use_count;
5176 + /* Use count to track requests from videocore peer.
5177 + ** This use count is not associated with a service, so needs to be
5178 + ** tracked separately with the state.
5180 + int peer_use_count;
5182 + /* Flag to indicate whether resume is blocked. This happens when the
5183 + ** ARM is suspending
5185 + struct completion resume_blocker;
5186 + int resume_blocked;
5187 + struct completion blocked_blocker;
5188 + int blocked_count;
5190 + int autosuspend_override;
5192 + /* Flag to indicate that the first vchiq connect has made it through.
5193 + ** This means that both sides should be fully ready, and we should
5194 + ** be able to suspend after this point.
5196 + int first_connect;
5198 + unsigned long long suspend_start_time;
5199 + unsigned long long sleep_start_time;
5200 + unsigned long long resume_start_time;
5201 + unsigned long long last_wake_time;
5203 +} VCHIQ_ARM_STATE_T;
5205 +extern int vchiq_arm_log_level;
5206 +extern int vchiq_susp_log_level;
5209 +vchiq_platform_init(VCHIQ_STATE_T *state);
5212 +vchiq_platform_exit(VCHIQ_STATE_T *state);
5214 +extern VCHIQ_STATE_T *
5215 +vchiq_get_state(void);
5217 +extern VCHIQ_STATUS_T
5218 +vchiq_arm_vcsuspend(VCHIQ_STATE_T *state);
5220 +extern VCHIQ_STATUS_T
5221 +vchiq_arm_force_suspend(VCHIQ_STATE_T *state);
5224 +vchiq_arm_allow_resume(VCHIQ_STATE_T *state);
5226 +extern VCHIQ_STATUS_T
5227 +vchiq_arm_vcresume(VCHIQ_STATE_T *state);
5229 +extern VCHIQ_STATUS_T
5230 +vchiq_arm_init_state(VCHIQ_STATE_T *state, VCHIQ_ARM_STATE_T *arm_state);
5233 +vchiq_check_resume(VCHIQ_STATE_T *state);
5236 +vchiq_check_suspend(VCHIQ_STATE_T *state);
5238 +extern VCHIQ_STATUS_T
5239 +vchiq_use_service(VCHIQ_SERVICE_HANDLE_T handle);
5241 +extern VCHIQ_STATUS_T
5242 +vchiq_release_service(VCHIQ_SERVICE_HANDLE_T handle);
5244 +extern VCHIQ_STATUS_T
5245 +vchiq_check_service(VCHIQ_SERVICE_T *service);
5247 +extern VCHIQ_STATUS_T
5248 +vchiq_platform_suspend(VCHIQ_STATE_T *state);
5251 +vchiq_platform_videocore_wanted(VCHIQ_STATE_T *state);
5254 +vchiq_platform_use_suspend_timer(void);
5257 +vchiq_dump_platform_use_state(VCHIQ_STATE_T *state);
5260 +vchiq_dump_service_use_state(VCHIQ_STATE_T *state);
5262 +extern VCHIQ_ARM_STATE_T*
5263 +vchiq_platform_get_arm_state(VCHIQ_STATE_T *state);
5266 +vchiq_videocore_wanted(VCHIQ_STATE_T *state);
5268 +extern VCHIQ_STATUS_T
5269 +vchiq_use_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service,
5270 + enum USE_TYPE_E use_type);
5271 +extern VCHIQ_STATUS_T
5272 +vchiq_release_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service);
5275 +set_suspend_state(VCHIQ_ARM_STATE_T *arm_state,
5276 + enum vc_suspend_status new_state);
5279 +set_resume_state(VCHIQ_ARM_STATE_T *arm_state,
5280 + enum vc_resume_status new_state);
5283 +start_suspend_timer(VCHIQ_ARM_STATE_T *arm_state);
5285 +extern int vchiq_proc_init(void);
5286 +extern void vchiq_proc_deinit(void);
5287 +extern struct proc_dir_entry *vchiq_proc_top(void);
5288 +extern struct proc_dir_entry *vchiq_clients_top(void);
5291 +#endif /* VCHIQ_ARM_H */
5292 diff --git a/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_build_info.h b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_build_info.h
5293 new file mode 100644
5294 index 0000000..df64581
5296 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_build_info.h
5299 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
5301 + * Redistribution and use in source and binary forms, with or without
5302 + * modification, are permitted provided that the following conditions
5304 + * 1. Redistributions of source code must retain the above copyright
5305 + * notice, this list of conditions, and the following disclaimer,
5306 + * without modification.
5307 + * 2. Redistributions in binary form must reproduce the above copyright
5308 + * notice, this list of conditions and the following disclaimer in the
5309 + * documentation and/or other materials provided with the distribution.
5310 + * 3. The names of the above-listed copyright holders may not be used
5311 + * to endorse or promote products derived from this software without
5312 + * specific prior written permission.
5314 + * ALTERNATIVELY, this software may be distributed under the terms of the
5315 + * GNU General Public License ("GPL") version 2, as published by the Free
5316 + * Software Foundation.
5318 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
5319 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
5320 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
5321 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
5322 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
5323 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
5324 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
5325 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
5326 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
5327 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
5328 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5331 +const char *vchiq_get_build_hostname(void);
5332 +const char *vchiq_get_build_version(void);
5333 +const char *vchiq_get_build_time(void);
5334 +const char *vchiq_get_build_date(void);
5335 diff --git a/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_cfg.h b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_cfg.h
5336 new file mode 100644
5337 index 0000000..493c86c
5339 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_cfg.h
5342 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
5344 + * Redistribution and use in source and binary forms, with or without
5345 + * modification, are permitted provided that the following conditions
5347 + * 1. Redistributions of source code must retain the above copyright
5348 + * notice, this list of conditions, and the following disclaimer,
5349 + * without modification.
5350 + * 2. Redistributions in binary form must reproduce the above copyright
5351 + * notice, this list of conditions and the following disclaimer in the
5352 + * documentation and/or other materials provided with the distribution.
5353 + * 3. The names of the above-listed copyright holders may not be used
5354 + * to endorse or promote products derived from this software without
5355 + * specific prior written permission.
5357 + * ALTERNATIVELY, this software may be distributed under the terms of the
5358 + * GNU General Public License ("GPL") version 2, as published by the Free
5359 + * Software Foundation.
5361 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
5362 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
5363 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
5364 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
5365 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
5366 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
5367 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
5368 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
5369 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
5370 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
5371 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5374 +#ifndef VCHIQ_CFG_H
5375 +#define VCHIQ_CFG_H
5377 +#define VCHIQ_MAGIC VCHIQ_MAKE_FOURCC('V', 'C', 'H', 'I')
5378 +/* The version of VCHIQ - change with any non-trivial change */
5379 +#define VCHIQ_VERSION 6
5380 +/* The minimum compatible version - update to match VCHIQ_VERSION with any
5381 +** incompatible change */
5382 +#define VCHIQ_VERSION_MIN 3
5384 +#define VCHIQ_MAX_STATES 1
5385 +#define VCHIQ_MAX_SERVICES 4096
5386 +#define VCHIQ_MAX_SLOTS 128
5387 +#define VCHIQ_MAX_SLOTS_PER_SIDE 64
5389 +#define VCHIQ_NUM_CURRENT_BULKS 32
5390 +#define VCHIQ_NUM_SERVICE_BULKS 4
5392 +#ifndef VCHIQ_ENABLE_DEBUG
5393 +#define VCHIQ_ENABLE_DEBUG 1
5396 +#ifndef VCHIQ_ENABLE_STATS
5397 +#define VCHIQ_ENABLE_STATS 1
5400 +#endif /* VCHIQ_CFG_H */
5401 diff --git a/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.c b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.c
5402 new file mode 100644
5403 index 0000000..65f4b52
5405 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.c
5408 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
5410 + * Redistribution and use in source and binary forms, with or without
5411 + * modification, are permitted provided that the following conditions
5413 + * 1. Redistributions of source code must retain the above copyright
5414 + * notice, this list of conditions, and the following disclaimer,
5415 + * without modification.
5416 + * 2. Redistributions in binary form must reproduce the above copyright
5417 + * notice, this list of conditions and the following disclaimer in the
5418 + * documentation and/or other materials provided with the distribution.
5419 + * 3. The names of the above-listed copyright holders may not be used
5420 + * to endorse or promote products derived from this software without
5421 + * specific prior written permission.
5423 + * ALTERNATIVELY, this software may be distributed under the terms of the
5424 + * GNU General Public License ("GPL") version 2, as published by the Free
5425 + * Software Foundation.
5427 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
5428 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
5429 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
5430 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
5431 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
5432 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
5433 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
5434 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
5435 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
5436 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
5437 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5440 +#include "vchiq_connected.h"
5441 +#include "vchiq_core.h"
5442 +#include <linux/module.h>
5443 +#include <linux/mutex.h>
5445 +#define MAX_CALLBACKS 10
5447 +static int g_connected;
5448 +static int g_num_deferred_callbacks;
5449 +static VCHIQ_CONNECTED_CALLBACK_T g_deferred_callback[MAX_CALLBACKS];
5450 +static int g_once_init;
5451 +static struct mutex g_connected_mutex;
5453 +/****************************************************************************
5455 +* Function to initialize our lock.
5457 +***************************************************************************/
5459 +static void connected_init(void)
5461 + if (!g_once_init) {
5462 + mutex_init(&g_connected_mutex);
5467 +/****************************************************************************
5469 +* This function is used to defer initialization until the vchiq stack is
5470 +* initialized. If the stack is already initialized, then the callback will
5471 +* be made immediately, otherwise it will be deferred until
5472 +* vchiq_call_connected_callbacks is called.
5474 +***************************************************************************/
5476 +void vchiq_add_connected_callback(VCHIQ_CONNECTED_CALLBACK_T callback)
5480 + if (mutex_lock_interruptible(&g_connected_mutex) != 0)
5484 + /* We're already connected. Call the callback immediately. */
5488 + if (g_num_deferred_callbacks >= MAX_CALLBACKS)
5489 + vchiq_log_error(vchiq_core_log_level,
5490 + "There already %d callback registered - "
5491 + "please increase MAX_CALLBACKS",
5492 + g_num_deferred_callbacks);
5494 + g_deferred_callback[g_num_deferred_callbacks] =
5496 + g_num_deferred_callbacks++;
5499 + mutex_unlock(&g_connected_mutex);
5502 +/****************************************************************************
5504 +* This function is called by the vchiq stack once it has been connected to
5505 +* the videocore and clients can start to use the stack.
5507 +***************************************************************************/
5509 +void vchiq_call_connected_callbacks(void)
5515 + if (mutex_lock_interruptible(&g_connected_mutex) != 0)
5518 + for (i = 0; i < g_num_deferred_callbacks; i++)
5519 + g_deferred_callback[i]();
5521 + g_num_deferred_callbacks = 0;
5523 + mutex_unlock(&g_connected_mutex);
5525 +EXPORT_SYMBOL(vchiq_add_connected_callback);
5526 diff --git a/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.h b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.h
5527 new file mode 100644
5528 index 0000000..863b3e3
5530 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.h
5533 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
5535 + * Redistribution and use in source and binary forms, with or without
5536 + * modification, are permitted provided that the following conditions
5538 + * 1. Redistributions of source code must retain the above copyright
5539 + * notice, this list of conditions, and the following disclaimer,
5540 + * without modification.
5541 + * 2. Redistributions in binary form must reproduce the above copyright
5542 + * notice, this list of conditions and the following disclaimer in the
5543 + * documentation and/or other materials provided with the distribution.
5544 + * 3. The names of the above-listed copyright holders may not be used
5545 + * to endorse or promote products derived from this software without
5546 + * specific prior written permission.
5548 + * ALTERNATIVELY, this software may be distributed under the terms of the
5549 + * GNU General Public License ("GPL") version 2, as published by the Free
5550 + * Software Foundation.
5552 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
5553 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
5554 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
5555 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
5556 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
5557 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
5558 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
5559 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
5560 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
5561 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
5562 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5565 +#ifndef VCHIQ_CONNECTED_H
5566 +#define VCHIQ_CONNECTED_H
5568 +/* ---- Include Files ----------------------------------------------------- */
5570 +/* ---- Constants and Types ---------------------------------------------- */
5572 +typedef void (*VCHIQ_CONNECTED_CALLBACK_T)(void);
5574 +/* ---- Variable Externs ------------------------------------------------- */
5576 +/* ---- Function Prototypes ---------------------------------------------- */
5578 +void vchiq_add_connected_callback(VCHIQ_CONNECTED_CALLBACK_T callback);
5579 +void vchiq_call_connected_callbacks(void);
5581 +#endif /* VCHIQ_CONNECTED_H */
5582 diff --git a/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.c b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.c
5583 new file mode 100644
5584 index 0000000..f35ed4f
5586 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.c
5589 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
5591 + * Redistribution and use in source and binary forms, with or without
5592 + * modification, are permitted provided that the following conditions
5594 + * 1. Redistributions of source code must retain the above copyright
5595 + * notice, this list of conditions, and the following disclaimer,
5596 + * without modification.
5597 + * 2. Redistributions in binary form must reproduce the above copyright
5598 + * notice, this list of conditions and the following disclaimer in the
5599 + * documentation and/or other materials provided with the distribution.
5600 + * 3. The names of the above-listed copyright holders may not be used
5601 + * to endorse or promote products derived from this software without
5602 + * specific prior written permission.
5604 + * ALTERNATIVELY, this software may be distributed under the terms of the
5605 + * GNU General Public License ("GPL") version 2, as published by the Free
5606 + * Software Foundation.
5608 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
5609 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
5610 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
5611 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
5612 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
5613 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
5614 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
5615 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
5616 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
5617 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
5618 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5621 +#include "vchiq_core.h"
5623 +#define VCHIQ_SLOT_HANDLER_STACK 8192
5625 +#define HANDLE_STATE_SHIFT 12
5627 +#define SLOT_INFO_FROM_INDEX(state, index) (state->slot_info + (index))
5628 +#define SLOT_DATA_FROM_INDEX(state, index) (state->slot_data + (index))
5629 +#define SLOT_INDEX_FROM_DATA(state, data) \
5630 + (((unsigned int)((char *)data - (char *)state->slot_data)) / \
5632 +#define SLOT_INDEX_FROM_INFO(state, info) \
5633 + ((unsigned int)(info - state->slot_info))
5634 +#define SLOT_QUEUE_INDEX_FROM_POS(pos) \
5635 + ((int)((unsigned int)(pos) / VCHIQ_SLOT_SIZE))
5638 +#define BULK_INDEX(x) (x & (VCHIQ_NUM_SERVICE_BULKS - 1))
5641 +struct vchiq_open_payload {
5645 + short version_min;
5648 +struct vchiq_openack_payload {
5652 +/* we require this for consistency between endpoints */
5653 +vchiq_static_assert(sizeof(VCHIQ_HEADER_T) == 8);
5654 +vchiq_static_assert(IS_POW2(sizeof(VCHIQ_HEADER_T)));
5655 +vchiq_static_assert(IS_POW2(VCHIQ_NUM_CURRENT_BULKS));
5656 +vchiq_static_assert(IS_POW2(VCHIQ_NUM_SERVICE_BULKS));
5657 +vchiq_static_assert(IS_POW2(VCHIQ_MAX_SERVICES));
5658 +vchiq_static_assert(VCHIQ_VERSION >= VCHIQ_VERSION_MIN);
5660 +/* Run time control of log level, based on KERN_XXX level. */
5661 +int vchiq_core_log_level = VCHIQ_LOG_DEFAULT;
5662 +int vchiq_core_msg_log_level = VCHIQ_LOG_DEFAULT;
5663 +int vchiq_sync_log_level = VCHIQ_LOG_DEFAULT;
5665 +static atomic_t pause_bulks_count = ATOMIC_INIT(0);
5667 +static DEFINE_SPINLOCK(service_spinlock);
5668 +DEFINE_SPINLOCK(bulk_waiter_spinlock);
5669 +DEFINE_SPINLOCK(quota_spinlock);
5671 +VCHIQ_STATE_T *vchiq_states[VCHIQ_MAX_STATES];
5672 +static unsigned int handle_seq;
5674 +static const char *const srvstate_names[] = {
5687 +static const char *const reason_names[] = {
5690 + "MESSAGE_AVAILABLE",
5691 + "BULK_TRANSMIT_DONE",
5692 + "BULK_RECEIVE_DONE",
5693 + "BULK_TRANSMIT_ABORTED",
5694 + "BULK_RECEIVE_ABORTED"
5697 +static const char *const conn_state_names[] = {
5711 +release_message_sync(VCHIQ_STATE_T *state, VCHIQ_HEADER_T *header);
5713 +static const char *msg_type_str(unsigned int msg_type)
5715 + switch (msg_type) {
5716 + case VCHIQ_MSG_PADDING: return "PADDING";
5717 + case VCHIQ_MSG_CONNECT: return "CONNECT";
5718 + case VCHIQ_MSG_OPEN: return "OPEN";
5719 + case VCHIQ_MSG_OPENACK: return "OPENACK";
5720 + case VCHIQ_MSG_CLOSE: return "CLOSE";
5721 + case VCHIQ_MSG_DATA: return "DATA";
5722 + case VCHIQ_MSG_BULK_RX: return "BULK_RX";
5723 + case VCHIQ_MSG_BULK_TX: return "BULK_TX";
5724 + case VCHIQ_MSG_BULK_RX_DONE: return "BULK_RX_DONE";
5725 + case VCHIQ_MSG_BULK_TX_DONE: return "BULK_TX_DONE";
5726 + case VCHIQ_MSG_PAUSE: return "PAUSE";
5727 + case VCHIQ_MSG_RESUME: return "RESUME";
5728 + case VCHIQ_MSG_REMOTE_USE: return "REMOTE_USE";
5729 + case VCHIQ_MSG_REMOTE_RELEASE: return "REMOTE_RELEASE";
5730 + case VCHIQ_MSG_REMOTE_USE_ACTIVE: return "REMOTE_USE_ACTIVE";
5736 +vchiq_set_service_state(VCHIQ_SERVICE_T *service, int newstate)
5738 + vchiq_log_info(vchiq_core_log_level, "%d: srv:%d %s->%s",
5739 + service->state->id, service->localport,
5740 + srvstate_names[service->srvstate],
5741 + srvstate_names[newstate]);
5742 + service->srvstate = newstate;
5746 +find_service_by_handle(VCHIQ_SERVICE_HANDLE_T handle)
5748 + VCHIQ_SERVICE_T *service;
5750 + spin_lock(&service_spinlock);
5751 + service = handle_to_service(handle);
5752 + if (service && (service->srvstate != VCHIQ_SRVSTATE_FREE) &&
5753 + (service->handle == handle)) {
5754 + BUG_ON(service->ref_count == 0);
5755 + service->ref_count++;
5758 + spin_unlock(&service_spinlock);
5761 + vchiq_log_info(vchiq_core_log_level,
5762 + "Invalid service handle 0x%x", handle);
5768 +find_service_by_port(VCHIQ_STATE_T *state, int localport)
5770 + VCHIQ_SERVICE_T *service = NULL;
5771 + if ((unsigned int)localport <= VCHIQ_PORT_MAX) {
5772 + spin_lock(&service_spinlock);
5773 + service = state->services[localport];
5774 + if (service && (service->srvstate != VCHIQ_SRVSTATE_FREE)) {
5775 + BUG_ON(service->ref_count == 0);
5776 + service->ref_count++;
5779 + spin_unlock(&service_spinlock);
5783 + vchiq_log_info(vchiq_core_log_level,
5784 + "Invalid port %d", localport);
5790 +find_service_for_instance(VCHIQ_INSTANCE_T instance,
5791 + VCHIQ_SERVICE_HANDLE_T handle) {
5792 + VCHIQ_SERVICE_T *service;
5794 + spin_lock(&service_spinlock);
5795 + service = handle_to_service(handle);
5796 + if (service && (service->srvstate != VCHIQ_SRVSTATE_FREE) &&
5797 + (service->handle == handle) &&
5798 + (service->instance == instance)) {
5799 + BUG_ON(service->ref_count == 0);
5800 + service->ref_count++;
5803 + spin_unlock(&service_spinlock);
5806 + vchiq_log_info(vchiq_core_log_level,
5807 + "Invalid service handle 0x%x", handle);
5813 +next_service_by_instance(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance,
5816 + VCHIQ_SERVICE_T *service = NULL;
5819 + spin_lock(&service_spinlock);
5820 + while (idx < state->unused_service) {
5821 + VCHIQ_SERVICE_T *srv = state->services[idx++];
5822 + if (srv && (srv->srvstate != VCHIQ_SRVSTATE_FREE) &&
5823 + (srv->instance == instance)) {
5825 + BUG_ON(service->ref_count == 0);
5826 + service->ref_count++;
5830 + spin_unlock(&service_spinlock);
5838 +lock_service(VCHIQ_SERVICE_T *service)
5840 + spin_lock(&service_spinlock);
5841 + BUG_ON(!service || (service->ref_count == 0));
5843 + service->ref_count++;
5844 + spin_unlock(&service_spinlock);
5848 +unlock_service(VCHIQ_SERVICE_T *service)
5850 + VCHIQ_STATE_T *state = service->state;
5851 + spin_lock(&service_spinlock);
5852 + BUG_ON(!service || (service->ref_count == 0));
5853 + if (service && service->ref_count) {
5854 + service->ref_count--;
5855 + if (!service->ref_count) {
5856 + BUG_ON(service->srvstate != VCHIQ_SRVSTATE_FREE);
5857 + state->services[service->localport] = NULL;
5861 + spin_unlock(&service_spinlock);
5863 + if (service && service->userdata_term)
5864 + service->userdata_term(service->base.userdata);
5870 +vchiq_get_client_id(VCHIQ_SERVICE_HANDLE_T handle)
5872 + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
5875 + id = service ? service->client_id : 0;
5877 + unlock_service(service);
5883 +vchiq_get_service_userdata(VCHIQ_SERVICE_HANDLE_T handle)
5885 + VCHIQ_SERVICE_T *service = handle_to_service(handle);
5887 + return service ? service->base.userdata : NULL;
5891 +vchiq_get_service_fourcc(VCHIQ_SERVICE_HANDLE_T handle)
5893 + VCHIQ_SERVICE_T *service = handle_to_service(handle);
5895 + return service ? service->base.fourcc : 0;
5899 +mark_service_closing_internal(VCHIQ_SERVICE_T *service, int sh_thread)
5901 + VCHIQ_STATE_T *state = service->state;
5902 + VCHIQ_SERVICE_QUOTA_T *service_quota;
5904 + service->closing = 1;
5906 + /* Synchronise with other threads. */
5907 + mutex_lock(&state->recycle_mutex);
5908 + mutex_unlock(&state->recycle_mutex);
5909 + if (!sh_thread || (state->conn_state != VCHIQ_CONNSTATE_PAUSE_SENT)) {
5910 + /* If we're pausing then the slot_mutex is held until resume
5911 + * by the slot handler. Therefore don't try to acquire this
5912 + * mutex if we're the slot handler and in the pause sent state.
5913 + * We don't need to in this case anyway. */
5914 + mutex_lock(&state->slot_mutex);
5915 + mutex_unlock(&state->slot_mutex);
5918 + /* Unblock any sending thread. */
5919 + service_quota = &state->service_quotas[service->localport];
5920 + up(&service_quota->quota_event);
5924 +mark_service_closing(VCHIQ_SERVICE_T *service)
5926 + mark_service_closing_internal(service, 0);
5929 +static inline VCHIQ_STATUS_T
5930 +make_service_callback(VCHIQ_SERVICE_T *service, VCHIQ_REASON_T reason,
5931 + VCHIQ_HEADER_T *header, void *bulk_userdata)
5933 + VCHIQ_STATUS_T status;
5934 + vchiq_log_trace(vchiq_core_log_level, "%d: callback:%d (%s, %x, %x)",
5935 + service->state->id, service->localport, reason_names[reason],
5936 + (unsigned int)header, (unsigned int)bulk_userdata);
5937 + status = service->base.callback(reason, header, service->handle,
5939 + if (status == VCHIQ_ERROR) {
5940 + vchiq_log_warning(vchiq_core_log_level,
5941 + "%d: ignoring ERROR from callback to service %x",
5942 + service->state->id, service->handle);
5943 + status = VCHIQ_SUCCESS;
5949 +vchiq_set_conn_state(VCHIQ_STATE_T *state, VCHIQ_CONNSTATE_T newstate)
5951 + VCHIQ_CONNSTATE_T oldstate = state->conn_state;
5952 + vchiq_log_info(vchiq_core_log_level, "%d: %s->%s", state->id,
5953 + conn_state_names[oldstate],
5954 + conn_state_names[newstate]);
5955 + state->conn_state = newstate;
5956 + vchiq_platform_conn_state_changed(state, oldstate, newstate);
5960 +remote_event_create(REMOTE_EVENT_T *event)
5963 + /* Don't clear the 'fired' flag because it may already have been set
5964 + ** by the other side. */
5965 + sema_init(event->event, 0);
5969 +remote_event_destroy(REMOTE_EVENT_T *event)
5975 +remote_event_wait(REMOTE_EVENT_T *event)
5977 + if (!event->fired) {
5980 + if (!event->fired) {
5981 + if (down_interruptible(event->event) != 0) {
5995 +remote_event_signal_local(REMOTE_EVENT_T *event)
6002 +remote_event_poll(REMOTE_EVENT_T *event)
6004 + if (event->fired && event->armed)
6005 + remote_event_signal_local(event);
6009 +remote_event_pollall(VCHIQ_STATE_T *state)
6011 + remote_event_poll(&state->local->sync_trigger);
6012 + remote_event_poll(&state->local->sync_release);
6013 + remote_event_poll(&state->local->trigger);
6014 + remote_event_poll(&state->local->recycle);
6017 +/* Round up message sizes so that any space at the end of a slot is always big
6018 +** enough for a header. This relies on header size being a power of two, which
6019 +** has been verified earlier by a static assertion. */
6021 +static inline unsigned int
6022 +calc_stride(unsigned int size)
6024 + /* Allow room for the header */
6025 + size += sizeof(VCHIQ_HEADER_T);
6028 + return (size + sizeof(VCHIQ_HEADER_T) - 1) & ~(sizeof(VCHIQ_HEADER_T)
6032 +/* Called by the slot handler thread */
6033 +static VCHIQ_SERVICE_T *
6034 +get_listening_service(VCHIQ_STATE_T *state, int fourcc)
6038 + WARN_ON(fourcc == VCHIQ_FOURCC_INVALID);
6040 + for (i = 0; i < state->unused_service; i++) {
6041 + VCHIQ_SERVICE_T *service = state->services[i];
6043 + (service->public_fourcc == fourcc) &&
6044 + ((service->srvstate == VCHIQ_SRVSTATE_LISTENING) ||
6045 + ((service->srvstate == VCHIQ_SRVSTATE_OPEN) &&
6046 + (service->remoteport == VCHIQ_PORT_FREE)))) {
6047 + lock_service(service);
6055 +/* Called by the slot handler thread */
6056 +static VCHIQ_SERVICE_T *
6057 +get_connected_service(VCHIQ_STATE_T *state, unsigned int port)
6060 + for (i = 0; i < state->unused_service; i++) {
6061 + VCHIQ_SERVICE_T *service = state->services[i];
6062 + if (service && (service->srvstate == VCHIQ_SRVSTATE_OPEN)
6063 + && (service->remoteport == port)) {
6064 + lock_service(service);
6072 +request_poll(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service, int poll_type)
6078 + value = atomic_read(&service->poll_flags);
6079 + } while (atomic_cmpxchg(&service->poll_flags, value,
6080 + value | (1 << poll_type)) != value);
6083 + value = atomic_read(&state->poll_services[
6084 + service->localport>>5]);
6085 + } while (atomic_cmpxchg(
6086 + &state->poll_services[service->localport>>5],
6087 + value, value | (1 << (service->localport & 0x1f)))
6091 + state->poll_needed = 1;
6094 + /* ... and ensure the slot handler runs. */
6095 + remote_event_signal_local(&state->local->trigger);
6098 +/* Called from queue_message, by the slot handler and application threads,
6099 +** with slot_mutex held */
6100 +static VCHIQ_HEADER_T *
6101 +reserve_space(VCHIQ_STATE_T *state, int space, int is_blocking)
6103 + VCHIQ_SHARED_STATE_T *local = state->local;
6104 + int tx_pos = state->local_tx_pos;
6105 + int slot_space = VCHIQ_SLOT_SIZE - (tx_pos & VCHIQ_SLOT_MASK);
6107 + if (space > slot_space) {
6108 + VCHIQ_HEADER_T *header;
6109 + /* Fill the remaining space with padding */
6110 + WARN_ON(state->tx_data == NULL);
6111 + header = (VCHIQ_HEADER_T *)
6112 + (state->tx_data + (tx_pos & VCHIQ_SLOT_MASK));
6113 + header->msgid = VCHIQ_MSGID_PADDING;
6114 + header->size = slot_space - sizeof(VCHIQ_HEADER_T);
6116 + tx_pos += slot_space;
6119 + /* If necessary, get the next slot. */
6120 + if ((tx_pos & VCHIQ_SLOT_MASK) == 0) {
6123 + /* If there is no free slot... */
6125 + if (down_trylock(&state->slot_available_event) != 0) {
6126 + /* ...wait for one. */
6128 + VCHIQ_STATS_INC(state, slot_stalls);
6130 + /* But first, flush through the last slot. */
6131 + state->local_tx_pos = tx_pos;
6132 + local->tx_pos = tx_pos;
6133 + remote_event_signal(&state->remote->trigger);
6135 + if (!is_blocking ||
6136 + (down_interruptible(
6137 + &state->slot_available_event) != 0))
6138 + return NULL; /* No space available */
6142 + (state->slot_queue_available * VCHIQ_SLOT_SIZE));
6144 + slot_index = local->slot_queue[
6145 + SLOT_QUEUE_INDEX_FROM_POS(tx_pos) &
6146 + VCHIQ_SLOT_QUEUE_MASK];
6148 + (char *)SLOT_DATA_FROM_INDEX(state, slot_index);
6151 + state->local_tx_pos = tx_pos + space;
6153 + return (VCHIQ_HEADER_T *)(state->tx_data + (tx_pos & VCHIQ_SLOT_MASK));
6156 +/* Called by the recycle thread. */
6158 +process_free_queue(VCHIQ_STATE_T *state)
6160 + VCHIQ_SHARED_STATE_T *local = state->local;
6161 + BITSET_T service_found[BITSET_SIZE(VCHIQ_MAX_SERVICES)];
6162 + int slot_queue_available;
6164 + /* Use a read memory barrier to ensure that any state that may have
6165 + ** been modified by another thread is not masked by stale prefetched
6169 + /* Find slots which have been freed by the other side, and return them
6170 + ** to the available queue. */
6171 + slot_queue_available = state->slot_queue_available;
6173 + while (slot_queue_available != local->slot_queue_recycle) {
6175 + int slot_index = local->slot_queue[slot_queue_available++ &
6176 + VCHIQ_SLOT_QUEUE_MASK];
6177 + char *data = (char *)SLOT_DATA_FROM_INDEX(state, slot_index);
6178 + int data_found = 0;
6180 + vchiq_log_trace(vchiq_core_log_level, "%d: pfq %d=%x %x %x",
6181 + state->id, slot_index, (unsigned int)data,
6182 + local->slot_queue_recycle, slot_queue_available);
6184 + /* Initialise the bitmask for services which have used this
6186 + BITSET_ZERO(service_found);
6190 + while (pos < VCHIQ_SLOT_SIZE) {
6191 + VCHIQ_HEADER_T *header =
6192 + (VCHIQ_HEADER_T *)(data + pos);
6193 + int msgid = header->msgid;
6194 + if (VCHIQ_MSG_TYPE(msgid) == VCHIQ_MSG_DATA) {
6195 + int port = VCHIQ_MSG_SRCPORT(msgid);
6196 + VCHIQ_SERVICE_QUOTA_T *service_quota =
6197 + &state->service_quotas[port];
6199 + spin_lock("a_spinlock);
6200 + count = service_quota->message_use_count;
6202 + service_quota->message_use_count =
6204 + spin_unlock("a_spinlock);
6206 + if (count == service_quota->message_quota)
6207 + /* Signal the service that it
6208 + ** has dropped below its quota
6210 + up(&service_quota->quota_event);
6211 + else if (count == 0) {
6212 + vchiq_log_error(vchiq_core_log_level,
6214 + "message_use_count=%d "
6215 + "(header %x, msgid %x, "
6216 + "header->msgid %x, "
6217 + "header->size %x)",
6220 + message_use_count,
6221 + (unsigned int)header, msgid,
6224 + WARN(1, "invalid message use count\n");
6226 + if (!BITSET_IS_SET(service_found, port)) {
6227 + /* Set the found bit for this service */
6228 + BITSET_SET(service_found, port);
6230 + spin_lock("a_spinlock);
6231 + count = service_quota->slot_use_count;
6233 + service_quota->slot_use_count =
6235 + spin_unlock("a_spinlock);
6238 + /* Signal the service in case
6239 + ** it has dropped below its
6241 + up(&service_quota->quota_event);
6243 + vchiq_core_log_level,
6244 + "%d: pfq:%d %x@%x - "
6248 + (unsigned int)header,
6252 + vchiq_core_log_level,
6261 + (unsigned int)header,
6265 + WARN(1, "bad slot use count\n");
6272 + pos += calc_stride(header->size);
6273 + if (pos > VCHIQ_SLOT_SIZE) {
6274 + vchiq_log_error(vchiq_core_log_level,
6275 + "pfq - pos %x: header %x, msgid %x, "
6276 + "header->msgid %x, header->size %x",
6277 + pos, (unsigned int)header, msgid,
6278 + header->msgid, header->size);
6279 + WARN(1, "invalid slot position\n");
6285 + spin_lock("a_spinlock);
6286 + count = state->data_use_count;
6288 + state->data_use_count =
6290 + spin_unlock("a_spinlock);
6291 + if (count == state->data_quota)
6292 + up(&state->data_quota_event);
6295 + state->slot_queue_available = slot_queue_available;
6296 + up(&state->slot_available_event);
6300 +/* Called by the slot handler and application threads */
6301 +static VCHIQ_STATUS_T
6302 +queue_message(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service,
6303 + int msgid, const VCHIQ_ELEMENT_T *elements,
6304 + int count, int size, int is_blocking)
6306 + VCHIQ_SHARED_STATE_T *local;
6307 + VCHIQ_SERVICE_QUOTA_T *service_quota = NULL;
6308 + VCHIQ_HEADER_T *header;
6309 + int type = VCHIQ_MSG_TYPE(msgid);
6311 + unsigned int stride;
6313 + local = state->local;
6315 + stride = calc_stride(size);
6317 + WARN_ON(!(stride <= VCHIQ_SLOT_SIZE));
6319 + if ((type != VCHIQ_MSG_RESUME) &&
6320 + (mutex_lock_interruptible(&state->slot_mutex) != 0))
6321 + return VCHIQ_RETRY;
6323 + if (type == VCHIQ_MSG_DATA) {
6328 + if (service->closing) {
6329 + /* The service has been closed */
6330 + mutex_unlock(&state->slot_mutex);
6331 + return VCHIQ_ERROR;
6334 + service_quota = &state->service_quotas[service->localport];
6336 + spin_lock("a_spinlock);
6338 + /* Ensure this service doesn't use more than its quota of
6339 + ** messages or slots */
6340 + tx_end_index = SLOT_QUEUE_INDEX_FROM_POS(
6341 + state->local_tx_pos + stride - 1);
6343 + /* Ensure data messages don't use more than their quota of
6345 + while ((tx_end_index != state->previous_data_index) &&
6346 + (state->data_use_count == state->data_quota)) {
6347 + VCHIQ_STATS_INC(state, data_stalls);
6348 + spin_unlock("a_spinlock);
6349 + mutex_unlock(&state->slot_mutex);
6351 + if (down_interruptible(&state->data_quota_event)
6353 + return VCHIQ_RETRY;
6355 + mutex_lock(&state->slot_mutex);
6356 + spin_lock("a_spinlock);
6357 + tx_end_index = SLOT_QUEUE_INDEX_FROM_POS(
6358 + state->local_tx_pos + stride - 1);
6359 + if ((tx_end_index == state->previous_data_index) ||
6360 + (state->data_use_count < state->data_quota)) {
6361 + /* Pass the signal on to other waiters */
6362 + up(&state->data_quota_event);
6367 + while ((service_quota->message_use_count ==
6368 + service_quota->message_quota) ||
6369 + ((tx_end_index != service_quota->previous_tx_index) &&
6370 + (service_quota->slot_use_count ==
6371 + service_quota->slot_quota))) {
6372 + spin_unlock("a_spinlock);
6373 + vchiq_log_trace(vchiq_core_log_level,
6374 + "%d: qm:%d %s,%x - quota stall "
6375 + "(msg %d, slot %d)",
6376 + state->id, service->localport,
6377 + msg_type_str(type), size,
6378 + service_quota->message_use_count,
6379 + service_quota->slot_use_count);
6380 + VCHIQ_SERVICE_STATS_INC(service, quota_stalls);
6381 + mutex_unlock(&state->slot_mutex);
6382 + if (down_interruptible(&service_quota->quota_event)
6384 + return VCHIQ_RETRY;
6385 + if (service->closing)
6386 + return VCHIQ_ERROR;
6387 + if (mutex_lock_interruptible(&state->slot_mutex) != 0)
6388 + return VCHIQ_RETRY;
6389 + if (service->srvstate != VCHIQ_SRVSTATE_OPEN) {
6390 + /* The service has been closed */
6391 + mutex_unlock(&state->slot_mutex);
6392 + return VCHIQ_ERROR;
6394 + spin_lock("a_spinlock);
6395 + tx_end_index = SLOT_QUEUE_INDEX_FROM_POS(
6396 + state->local_tx_pos + stride - 1);
6399 + spin_unlock("a_spinlock);
6402 + header = reserve_space(state, stride, is_blocking);
6406 + VCHIQ_SERVICE_STATS_INC(service, slot_stalls);
6407 + mutex_unlock(&state->slot_mutex);
6408 + return VCHIQ_RETRY;
6411 + if (type == VCHIQ_MSG_DATA) {
6414 + int slot_use_count;
6416 + vchiq_log_info(vchiq_core_log_level,
6417 + "%d: qm %s@%x,%x (%d->%d)",
6419 + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
6420 + (unsigned int)header, size,
6421 + VCHIQ_MSG_SRCPORT(msgid),
6422 + VCHIQ_MSG_DSTPORT(msgid));
6426 + for (i = 0, pos = 0; i < (unsigned int)count;
6427 + pos += elements[i++].size)
6428 + if (elements[i].size) {
6429 + if (vchiq_copy_from_user
6430 + (header->data + pos, elements[i].data,
6431 + (size_t) elements[i].size) !=
6433 + mutex_unlock(&state->slot_mutex);
6434 + VCHIQ_SERVICE_STATS_INC(service,
6436 + return VCHIQ_ERROR;
6439 + if (vchiq_core_msg_log_level >=
6441 + vchiq_log_dump_mem("Sent", 0,
6442 + header->data + pos,
6444 + elements[0].size));
6448 + spin_lock("a_spinlock);
6449 + service_quota->message_use_count++;
6452 + SLOT_QUEUE_INDEX_FROM_POS(state->local_tx_pos - 1);
6454 + /* If this transmission can't fit in the last slot used by any
6455 + ** service, the data_use_count must be increased. */
6456 + if (tx_end_index != state->previous_data_index) {
6457 + state->previous_data_index = tx_end_index;
6458 + state->data_use_count++;
6461 + /* If this isn't the same slot last used by this service,
6462 + ** the service's slot_use_count must be increased. */
6463 + if (tx_end_index != service_quota->previous_tx_index) {
6464 + service_quota->previous_tx_index = tx_end_index;
6465 + slot_use_count = ++service_quota->slot_use_count;
6467 + slot_use_count = 0;
6470 + spin_unlock("a_spinlock);
6472 + if (slot_use_count)
6473 + vchiq_log_trace(vchiq_core_log_level,
6474 + "%d: qm:%d %s,%x - slot_use->%d (hdr %p)",
6475 + state->id, service->localport,
6476 + msg_type_str(VCHIQ_MSG_TYPE(msgid)), size,
6477 + slot_use_count, header);
6479 + VCHIQ_SERVICE_STATS_INC(service, ctrl_tx_count);
6480 + VCHIQ_SERVICE_STATS_ADD(service, ctrl_tx_bytes, size);
6482 + vchiq_log_info(vchiq_core_log_level,
6483 + "%d: qm %s@%x,%x (%d->%d)", state->id,
6484 + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
6485 + (unsigned int)header, size,
6486 + VCHIQ_MSG_SRCPORT(msgid),
6487 + VCHIQ_MSG_DSTPORT(msgid));
6489 + WARN_ON(!((count == 1) && (size == elements[0].size)));
6490 + memcpy(header->data, elements[0].data,
6491 + elements[0].size);
6493 + VCHIQ_STATS_INC(state, ctrl_tx_count);
6496 + header->msgid = msgid;
6497 + header->size = size;
6502 + svc_fourcc = service
6503 + ? service->base.fourcc
6504 + : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
6506 + vchiq_log_info(vchiq_core_msg_log_level,
6507 + "Sent Msg %s(%u) to %c%c%c%c s:%u d:%d len:%d",
6508 + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
6509 + VCHIQ_MSG_TYPE(msgid),
6510 + VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
6511 + VCHIQ_MSG_SRCPORT(msgid),
6512 + VCHIQ_MSG_DSTPORT(msgid),
6516 + /* Make sure the new header is visible to the peer. */
6519 + /* Make the new tx_pos visible to the peer. */
6520 + local->tx_pos = state->local_tx_pos;
6523 + if (service && (type == VCHIQ_MSG_CLOSE))
6524 + vchiq_set_service_state(service, VCHIQ_SRVSTATE_CLOSESENT);
6526 + if (VCHIQ_MSG_TYPE(msgid) != VCHIQ_MSG_PAUSE)
6527 + mutex_unlock(&state->slot_mutex);
6529 + remote_event_signal(&state->remote->trigger);
6531 + return VCHIQ_SUCCESS;
6534 +/* Called by the slot handler and application threads */
6535 +static VCHIQ_STATUS_T
6536 +queue_message_sync(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service,
6537 + int msgid, const VCHIQ_ELEMENT_T *elements,
6538 + int count, int size, int is_blocking)
6540 + VCHIQ_SHARED_STATE_T *local;
6541 + VCHIQ_HEADER_T *header;
6543 + local = state->local;
6545 + if ((VCHIQ_MSG_TYPE(msgid) != VCHIQ_MSG_RESUME) &&
6546 + (mutex_lock_interruptible(&state->sync_mutex) != 0))
6547 + return VCHIQ_RETRY;
6549 + remote_event_wait(&local->sync_release);
6553 + header = (VCHIQ_HEADER_T *)SLOT_DATA_FROM_INDEX(state,
6554 + local->slot_sync);
6557 + int oldmsgid = header->msgid;
6558 + if (oldmsgid != VCHIQ_MSGID_PADDING)
6559 + vchiq_log_error(vchiq_core_log_level,
6560 + "%d: qms - msgid %x, not PADDING",
6561 + state->id, oldmsgid);
6567 + vchiq_log_info(vchiq_sync_log_level,
6568 + "%d: qms %s@%x,%x (%d->%d)", state->id,
6569 + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
6570 + (unsigned int)header, size,
6571 + VCHIQ_MSG_SRCPORT(msgid),
6572 + VCHIQ_MSG_DSTPORT(msgid));
6574 + for (i = 0, pos = 0; i < (unsigned int)count;
6575 + pos += elements[i++].size)
6576 + if (elements[i].size) {
6577 + if (vchiq_copy_from_user
6578 + (header->data + pos, elements[i].data,
6579 + (size_t) elements[i].size) !=
6581 + mutex_unlock(&state->sync_mutex);
6582 + VCHIQ_SERVICE_STATS_INC(service,
6584 + return VCHIQ_ERROR;
6587 + if (vchiq_sync_log_level >=
6589 + vchiq_log_dump_mem("Sent Sync",
6590 + 0, header->data + pos,
6592 + elements[0].size));
6596 + VCHIQ_SERVICE_STATS_INC(service, ctrl_tx_count);
6597 + VCHIQ_SERVICE_STATS_ADD(service, ctrl_tx_bytes, size);
6599 + vchiq_log_info(vchiq_sync_log_level,
6600 + "%d: qms %s@%x,%x (%d->%d)", state->id,
6601 + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
6602 + (unsigned int)header, size,
6603 + VCHIQ_MSG_SRCPORT(msgid),
6604 + VCHIQ_MSG_DSTPORT(msgid));
6606 + WARN_ON(!((count == 1) && (size == elements[0].size)));
6607 + memcpy(header->data, elements[0].data,
6608 + elements[0].size);
6610 + VCHIQ_STATS_INC(state, ctrl_tx_count);
6613 + header->size = size;
6614 + header->msgid = msgid;
6616 + if (vchiq_sync_log_level >= VCHIQ_LOG_TRACE) {
6619 + svc_fourcc = service
6620 + ? service->base.fourcc
6621 + : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
6623 + vchiq_log_trace(vchiq_sync_log_level,
6624 + "Sent Sync Msg %s(%u) to %c%c%c%c s:%u d:%d len:%d",
6625 + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
6626 + VCHIQ_MSG_TYPE(msgid),
6627 + VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
6628 + VCHIQ_MSG_SRCPORT(msgid),
6629 + VCHIQ_MSG_DSTPORT(msgid),
6633 + /* Make sure the new header is visible to the peer. */
6636 + remote_event_signal(&state->remote->sync_trigger);
6638 + if (VCHIQ_MSG_TYPE(msgid) != VCHIQ_MSG_PAUSE)
6639 + mutex_unlock(&state->sync_mutex);
6641 + return VCHIQ_SUCCESS;
6645 +claim_slot(VCHIQ_SLOT_INFO_T *slot)
6647 + slot->use_count++;
6651 +release_slot(VCHIQ_STATE_T *state, VCHIQ_SLOT_INFO_T *slot_info,
6652 + VCHIQ_HEADER_T *header, VCHIQ_SERVICE_T *service)
6654 + int release_count;
6656 + mutex_lock(&state->recycle_mutex);
6659 + int msgid = header->msgid;
6660 + if (((msgid & VCHIQ_MSGID_CLAIMED) == 0) ||
6661 + (service && service->closing)) {
6662 + mutex_unlock(&state->recycle_mutex);
6666 + /* Rewrite the message header to prevent a double
6668 + header->msgid = msgid & ~VCHIQ_MSGID_CLAIMED;
6671 + release_count = slot_info->release_count;
6672 + slot_info->release_count = ++release_count;
6674 + if (release_count == slot_info->use_count) {
6675 + int slot_queue_recycle;
6676 + /* Add to the freed queue */
6678 + /* A read barrier is necessary here to prevent speculative
6679 + ** fetches of remote->slot_queue_recycle from overtaking the
6683 + slot_queue_recycle = state->remote->slot_queue_recycle;
6684 + state->remote->slot_queue[slot_queue_recycle &
6685 + VCHIQ_SLOT_QUEUE_MASK] =
6686 + SLOT_INDEX_FROM_INFO(state, slot_info);
6687 + state->remote->slot_queue_recycle = slot_queue_recycle + 1;
6688 + vchiq_log_info(vchiq_core_log_level,
6689 + "%d: release_slot %d - recycle->%x",
6690 + state->id, SLOT_INDEX_FROM_INFO(state, slot_info),
6691 + state->remote->slot_queue_recycle);
6693 + /* A write barrier is necessary, but remote_event_signal
6694 + ** contains one. */
6695 + remote_event_signal(&state->remote->recycle);
6698 + mutex_unlock(&state->recycle_mutex);
6701 +/* Called by the slot handler - don't hold the bulk mutex */
6702 +static VCHIQ_STATUS_T
6703 +notify_bulks(VCHIQ_SERVICE_T *service, VCHIQ_BULK_QUEUE_T *queue,
6706 + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
6708 + vchiq_log_trace(vchiq_core_log_level,
6709 + "%d: nb:%d %cx - p=%x rn=%x r=%x",
6710 + service->state->id, service->localport,
6711 + (queue == &service->bulk_tx) ? 't' : 'r',
6712 + queue->process, queue->remote_notify, queue->remove);
6714 + if (service->state->is_master) {
6715 + while (queue->remote_notify != queue->process) {
6716 + VCHIQ_BULK_T *bulk =
6717 + &queue->bulks[BULK_INDEX(queue->remote_notify)];
6718 + int msgtype = (bulk->dir == VCHIQ_BULK_TRANSMIT) ?
6719 + VCHIQ_MSG_BULK_RX_DONE : VCHIQ_MSG_BULK_TX_DONE;
6720 + int msgid = VCHIQ_MAKE_MSG(msgtype, service->localport,
6721 + service->remoteport);
6722 + VCHIQ_ELEMENT_T element = { &bulk->actual, 4 };
6723 + /* Only reply to non-dummy bulk requests */
6724 + if (bulk->remote_data) {
6725 + status = queue_message(service->state, NULL,
6726 + msgid, &element, 1, 4, 0);
6727 + if (status != VCHIQ_SUCCESS)
6730 + queue->remote_notify++;
6733 + queue->remote_notify = queue->process;
6736 + if (status == VCHIQ_SUCCESS) {
6737 + while (queue->remove != queue->remote_notify) {
6738 + VCHIQ_BULK_T *bulk =
6739 + &queue->bulks[BULK_INDEX(queue->remove)];
6741 + /* Only generate callbacks for non-dummy bulk
6742 + ** requests, and non-terminated services */
6743 + if (bulk->data && service->instance) {
6744 + if (bulk->actual != VCHIQ_BULK_ACTUAL_ABORTED) {
6745 + if (bulk->dir == VCHIQ_BULK_TRANSMIT) {
6746 + VCHIQ_SERVICE_STATS_INC(service,
6748 + VCHIQ_SERVICE_STATS_ADD(service,
6752 + VCHIQ_SERVICE_STATS_INC(service,
6754 + VCHIQ_SERVICE_STATS_ADD(service,
6759 + VCHIQ_SERVICE_STATS_INC(service,
6760 + bulk_aborted_count);
6762 + if (bulk->mode == VCHIQ_BULK_MODE_BLOCKING) {
6763 + struct bulk_waiter *waiter;
6764 + spin_lock(&bulk_waiter_spinlock);
6765 + waiter = bulk->userdata;
6767 + waiter->actual = bulk->actual;
6768 + up(&waiter->event);
6770 + spin_unlock(&bulk_waiter_spinlock);
6771 + } else if (bulk->mode ==
6772 + VCHIQ_BULK_MODE_CALLBACK) {
6773 + VCHIQ_REASON_T reason = (bulk->dir ==
6774 + VCHIQ_BULK_TRANSMIT) ?
6776 + VCHIQ_BULK_ACTUAL_ABORTED) ?
6777 + VCHIQ_BULK_TRANSMIT_ABORTED :
6778 + VCHIQ_BULK_TRANSMIT_DONE) :
6780 + VCHIQ_BULK_ACTUAL_ABORTED) ?
6781 + VCHIQ_BULK_RECEIVE_ABORTED :
6782 + VCHIQ_BULK_RECEIVE_DONE);
6783 + status = make_service_callback(service,
6784 + reason, NULL, bulk->userdata);
6785 + if (status == VCHIQ_RETRY)
6791 + up(&service->bulk_remove_event);
6794 + status = VCHIQ_SUCCESS;
6797 + if (status == VCHIQ_RETRY)
6798 + request_poll(service->state, service,
6799 + (queue == &service->bulk_tx) ?
6800 + VCHIQ_POLL_TXNOTIFY : VCHIQ_POLL_RXNOTIFY);
6805 +/* Called by the slot handler thread */
6807 +poll_services(VCHIQ_STATE_T *state)
6811 + for (group = 0; group < BITSET_SIZE(state->unused_service); group++) {
6813 + flags = atomic_xchg(&state->poll_services[group], 0);
6814 + for (i = 0; flags; i++) {
6815 + if (flags & (1 << i)) {
6816 + VCHIQ_SERVICE_T *service =
6817 + find_service_by_port(state,
6819 + uint32_t service_flags;
6820 + flags &= ~(1 << i);
6824 + atomic_xchg(&service->poll_flags, 0);
6825 + if (service_flags &
6826 + (1 << VCHIQ_POLL_REMOVE)) {
6827 + vchiq_log_info(vchiq_core_log_level,
6828 + "%d: ps - remove %d<->%d",
6829 + state->id, service->localport,
6830 + service->remoteport);
6832 + /* Make it look like a client, because
6833 + it must be removed and not left in
6834 + the LISTENING state. */
6835 + service->public_fourcc =
6836 + VCHIQ_FOURCC_INVALID;
6838 + if (vchiq_close_service_internal(
6839 + service, 0/*!close_recvd*/) !=
6841 + request_poll(state, service,
6842 + VCHIQ_POLL_REMOVE);
6843 + } else if (service_flags &
6844 + (1 << VCHIQ_POLL_TERMINATE)) {
6845 + vchiq_log_info(vchiq_core_log_level,
6846 + "%d: ps - terminate %d<->%d",
6847 + state->id, service->localport,
6848 + service->remoteport);
6849 + if (vchiq_close_service_internal(
6850 + service, 0/*!close_recvd*/) !=
6852 + request_poll(state, service,
6853 + VCHIQ_POLL_TERMINATE);
6855 + if (service_flags & (1 << VCHIQ_POLL_TXNOTIFY))
6856 + notify_bulks(service,
6857 + &service->bulk_tx,
6859 + if (service_flags & (1 << VCHIQ_POLL_RXNOTIFY))
6860 + notify_bulks(service,
6861 + &service->bulk_rx,
6863 + unlock_service(service);
6869 +/* Called by the slot handler or application threads, holding the bulk mutex. */
6871 +resolve_bulks(VCHIQ_SERVICE_T *service, VCHIQ_BULK_QUEUE_T *queue)
6873 + VCHIQ_STATE_T *state = service->state;
6877 + while ((queue->process != queue->local_insert) &&
6878 + (queue->process != queue->remote_insert)) {
6879 + VCHIQ_BULK_T *bulk = &queue->bulks[BULK_INDEX(queue->process)];
6881 + vchiq_log_trace(vchiq_core_log_level,
6882 + "%d: rb:%d %cx - li=%x ri=%x p=%x",
6883 + state->id, service->localport,
6884 + (queue == &service->bulk_tx) ? 't' : 'r',
6885 + queue->local_insert, queue->remote_insert,
6888 + WARN_ON(!((int)(queue->local_insert - queue->process) > 0));
6889 + WARN_ON(!((int)(queue->remote_insert - queue->process) > 0));
6891 + rc = mutex_lock_interruptible(&state->bulk_transfer_mutex);
6895 + vchiq_transfer_bulk(bulk);
6896 + mutex_unlock(&state->bulk_transfer_mutex);
6898 + if (vchiq_core_msg_log_level >= VCHIQ_LOG_INFO) {
6899 + const char *header = (queue == &service->bulk_tx) ?
6900 + "Send Bulk to" : "Recv Bulk from";
6901 + if (bulk->actual != VCHIQ_BULK_ACTUAL_ABORTED)
6902 + vchiq_log_info(vchiq_core_msg_log_level,
6903 + "%s %c%c%c%c d:%d len:%d %x<->%x",
6905 + VCHIQ_FOURCC_AS_4CHARS(
6906 + service->base.fourcc),
6907 + service->remoteport,
6909 + (unsigned int)bulk->data,
6910 + (unsigned int)bulk->remote_data);
6912 + vchiq_log_info(vchiq_core_msg_log_level,
6913 + "%s %c%c%c%c d:%d ABORTED - tx len:%d,"
6914 + " rx len:%d %x<->%x",
6916 + VCHIQ_FOURCC_AS_4CHARS(
6917 + service->base.fourcc),
6918 + service->remoteport,
6920 + bulk->remote_size,
6921 + (unsigned int)bulk->data,
6922 + (unsigned int)bulk->remote_data);
6925 + vchiq_complete_bulk(bulk);
6932 +/* Called with the bulk_mutex held */
6934 +abort_outstanding_bulks(VCHIQ_SERVICE_T *service, VCHIQ_BULK_QUEUE_T *queue)
6936 + int is_tx = (queue == &service->bulk_tx);
6937 + vchiq_log_trace(vchiq_core_log_level,
6938 + "%d: aob:%d %cx - li=%x ri=%x p=%x",
6939 + service->state->id, service->localport, is_tx ? 't' : 'r',
6940 + queue->local_insert, queue->remote_insert, queue->process);
6942 + WARN_ON(!((int)(queue->local_insert - queue->process) >= 0));
6943 + WARN_ON(!((int)(queue->remote_insert - queue->process) >= 0));
6945 + while ((queue->process != queue->local_insert) ||
6946 + (queue->process != queue->remote_insert)) {
6947 + VCHIQ_BULK_T *bulk = &queue->bulks[BULK_INDEX(queue->process)];
6949 + if (queue->process == queue->remote_insert) {
6950 + /* fabricate a matching dummy bulk */
6951 + bulk->remote_data = NULL;
6952 + bulk->remote_size = 0;
6953 + queue->remote_insert++;
6956 + if (queue->process != queue->local_insert) {
6957 + vchiq_complete_bulk(bulk);
6959 + vchiq_log_info(vchiq_core_msg_log_level,
6960 + "%s %c%c%c%c d:%d ABORTED - tx len:%d, "
6962 + is_tx ? "Send Bulk to" : "Recv Bulk from",
6963 + VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
6964 + service->remoteport,
6966 + bulk->remote_size);
6968 + /* fabricate a matching dummy bulk */
6969 + bulk->data = NULL;
6971 + bulk->actual = VCHIQ_BULK_ACTUAL_ABORTED;
6972 + bulk->dir = is_tx ? VCHIQ_BULK_TRANSMIT :
6973 + VCHIQ_BULK_RECEIVE;
6974 + queue->local_insert++;
6981 +/* Called from the slot handler thread */
6983 +pause_bulks(VCHIQ_STATE_T *state)
6985 + if (unlikely(atomic_inc_return(&pause_bulks_count) != 1)) {
6987 + atomic_set(&pause_bulks_count, 1);
6991 + /* Block bulk transfers from all services */
6992 + mutex_lock(&state->bulk_transfer_mutex);
6995 +/* Called from the slot handler thread */
6997 +resume_bulks(VCHIQ_STATE_T *state)
7000 + if (unlikely(atomic_dec_return(&pause_bulks_count) != 0)) {
7002 + atomic_set(&pause_bulks_count, 0);
7006 + /* Allow bulk transfers from all services */
7007 + mutex_unlock(&state->bulk_transfer_mutex);
7009 + if (state->deferred_bulks == 0)
7012 + /* Deal with any bulks which had to be deferred due to being in
7013 + * paused state. Don't try to match up to number of deferred bulks
7014 + * in case we've had something come and close the service in the
7015 + * interim - just process all bulk queues for all services */
7016 + vchiq_log_info(vchiq_core_log_level, "%s: processing %d deferred bulks",
7017 + __func__, state->deferred_bulks);
7019 + for (i = 0; i < state->unused_service; i++) {
7020 + VCHIQ_SERVICE_T *service = state->services[i];
7021 + int resolved_rx = 0;
7022 + int resolved_tx = 0;
7023 + if (!service || (service->srvstate != VCHIQ_SRVSTATE_OPEN))
7026 + mutex_lock(&service->bulk_mutex);
7027 + resolved_rx = resolve_bulks(service, &service->bulk_rx);
7028 + resolved_tx = resolve_bulks(service, &service->bulk_tx);
7029 + mutex_unlock(&service->bulk_mutex);
7031 + notify_bulks(service, &service->bulk_rx, 1);
7033 + notify_bulks(service, &service->bulk_tx, 1);
7035 + state->deferred_bulks = 0;
7039 +parse_open(VCHIQ_STATE_T *state, VCHIQ_HEADER_T *header)
7041 + VCHIQ_SERVICE_T *service = NULL;
7044 + unsigned int localport, remoteport;
7046 + msgid = header->msgid;
7047 + size = header->size;
7048 + type = VCHIQ_MSG_TYPE(msgid);
7049 + localport = VCHIQ_MSG_DSTPORT(msgid);
7050 + remoteport = VCHIQ_MSG_SRCPORT(msgid);
7051 + if (size >= sizeof(struct vchiq_open_payload)) {
7052 + const struct vchiq_open_payload *payload =
7053 + (struct vchiq_open_payload *)header->data;
7054 + unsigned int fourcc;
7056 + fourcc = payload->fourcc;
7057 + vchiq_log_info(vchiq_core_log_level,
7058 + "%d: prs OPEN@%x (%d->'%c%c%c%c')",
7059 + state->id, (unsigned int)header,
7061 + VCHIQ_FOURCC_AS_4CHARS(fourcc));
7063 + service = get_listening_service(state, fourcc);
7066 + /* A matching service exists */
7067 + short version = payload->version;
7068 + short version_min = payload->version_min;
7069 + if ((service->version < version_min) ||
7070 + (version < service->version_min)) {
7071 + /* Version mismatch */
7072 + vchiq_loud_error_header();
7073 + vchiq_loud_error("%d: service %d (%c%c%c%c) "
7074 + "version mismatch - local (%d, min %d)"
7075 + " vs. remote (%d, min %d)",
7076 + state->id, service->localport,
7077 + VCHIQ_FOURCC_AS_4CHARS(fourcc),
7078 + service->version, service->version_min,
7079 + version, version_min);
7080 + vchiq_loud_error_footer();
7081 + unlock_service(service);
7085 + service->peer_version = version;
7087 + if (service->srvstate == VCHIQ_SRVSTATE_LISTENING) {
7088 + struct vchiq_openack_payload ack_payload = {
7091 + VCHIQ_ELEMENT_T body = {
7093 + sizeof(ack_payload)
7096 + /* Acknowledge the OPEN */
7097 + if (service->sync) {
7098 + if (queue_message_sync(state, NULL,
7100 + VCHIQ_MSG_OPENACK,
7101 + service->localport,
7103 + &body, 1, sizeof(ack_payload),
7104 + 0) == VCHIQ_RETRY)
7105 + goto bail_not_ready;
7107 + if (queue_message(state, NULL,
7109 + VCHIQ_MSG_OPENACK,
7110 + service->localport,
7112 + &body, 1, sizeof(ack_payload),
7113 + 0) == VCHIQ_RETRY)
7114 + goto bail_not_ready;
7117 + /* The service is now open */
7118 + vchiq_set_service_state(service,
7119 + service->sync ? VCHIQ_SRVSTATE_OPENSYNC
7120 + : VCHIQ_SRVSTATE_OPEN);
7123 + service->remoteport = remoteport;
7124 + service->client_id = ((int *)header->data)[1];
7125 + if (make_service_callback(service, VCHIQ_SERVICE_OPENED,
7126 + NULL, NULL) == VCHIQ_RETRY) {
7127 + /* Bail out if not ready */
7128 + service->remoteport = VCHIQ_PORT_FREE;
7129 + goto bail_not_ready;
7132 + /* Success - the message has been dealt with */
7133 + unlock_service(service);
7139 + /* No available service, or an invalid request - send a CLOSE */
7140 + if (queue_message(state, NULL,
7141 + VCHIQ_MAKE_MSG(VCHIQ_MSG_CLOSE, 0, VCHIQ_MSG_SRCPORT(msgid)),
7142 + NULL, 0, 0, 0) == VCHIQ_RETRY)
7143 + goto bail_not_ready;
7149 + unlock_service(service);
7154 +/* Called by the slot handler thread */
7156 +parse_rx_slots(VCHIQ_STATE_T *state)
7158 + VCHIQ_SHARED_STATE_T *remote = state->remote;
7159 + VCHIQ_SERVICE_T *service = NULL;
7161 + DEBUG_INITIALISE(state->local)
7163 + tx_pos = remote->tx_pos;
7165 + while (state->rx_pos != tx_pos) {
7166 + VCHIQ_HEADER_T *header;
7169 + unsigned int localport, remoteport;
7171 + DEBUG_TRACE(PARSE_LINE);
7172 + if (!state->rx_data) {
7174 + WARN_ON(!((state->rx_pos & VCHIQ_SLOT_MASK) == 0));
7175 + rx_index = remote->slot_queue[
7176 + SLOT_QUEUE_INDEX_FROM_POS(state->rx_pos) &
7177 + VCHIQ_SLOT_QUEUE_MASK];
7178 + state->rx_data = (char *)SLOT_DATA_FROM_INDEX(state,
7180 + state->rx_info = SLOT_INFO_FROM_INDEX(state, rx_index);
7182 + /* Initialise use_count to one, and increment
7183 + ** release_count at the end of the slot to avoid
7184 + ** releasing the slot prematurely. */
7185 + state->rx_info->use_count = 1;
7186 + state->rx_info->release_count = 0;
7189 + header = (VCHIQ_HEADER_T *)(state->rx_data +
7190 + (state->rx_pos & VCHIQ_SLOT_MASK));
7191 + DEBUG_VALUE(PARSE_HEADER, (int)header);
7192 + msgid = header->msgid;
7193 + DEBUG_VALUE(PARSE_MSGID, msgid);
7194 + size = header->size;
7195 + type = VCHIQ_MSG_TYPE(msgid);
7196 + localport = VCHIQ_MSG_DSTPORT(msgid);
7197 + remoteport = VCHIQ_MSG_SRCPORT(msgid);
7199 + if (type != VCHIQ_MSG_DATA)
7200 + VCHIQ_STATS_INC(state, ctrl_rx_count);
7203 + case VCHIQ_MSG_OPENACK:
7204 + case VCHIQ_MSG_CLOSE:
7205 + case VCHIQ_MSG_DATA:
7206 + case VCHIQ_MSG_BULK_RX:
7207 + case VCHIQ_MSG_BULK_TX:
7208 + case VCHIQ_MSG_BULK_RX_DONE:
7209 + case VCHIQ_MSG_BULK_TX_DONE:
7210 + service = find_service_by_port(state, localport);
7211 + if ((!service || service->remoteport != remoteport) &&
7212 + (localport == 0) &&
7213 + (type == VCHIQ_MSG_CLOSE)) {
7214 + /* This could be a CLOSE from a client which
7215 + hadn't yet received the OPENACK - look for
7216 + the connected service */
7218 + unlock_service(service);
7219 + service = get_connected_service(state,
7222 + vchiq_log_warning(vchiq_core_log_level,
7223 + "%d: prs %s@%x (%d->%d) - "
7224 + "found connected service %d",
7225 + state->id, msg_type_str(type),
7226 + (unsigned int)header,
7227 + remoteport, localport,
7228 + service->localport);
7232 + vchiq_log_error(vchiq_core_log_level,
7233 + "%d: prs %s@%x (%d->%d) - "
7234 + "invalid/closed service %d",
7235 + state->id, msg_type_str(type),
7236 + (unsigned int)header,
7237 + remoteport, localport, localport);
7238 + goto skip_message;
7245 + if (vchiq_core_msg_log_level >= VCHIQ_LOG_INFO) {
7248 + svc_fourcc = service
7249 + ? service->base.fourcc
7250 + : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
7251 + vchiq_log_info(vchiq_core_msg_log_level,
7252 + "Rcvd Msg %s(%u) from %c%c%c%c s:%d d:%d "
7254 + msg_type_str(type), type,
7255 + VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
7256 + remoteport, localport, size);
7258 + vchiq_log_dump_mem("Rcvd", 0, header->data,
7262 + if (((unsigned int)header & VCHIQ_SLOT_MASK) + calc_stride(size)
7263 + > VCHIQ_SLOT_SIZE) {
7264 + vchiq_log_error(vchiq_core_log_level,
7265 + "header %x (msgid %x) - size %x too big for "
7267 + (unsigned int)header, (unsigned int)msgid,
7268 + (unsigned int)size);
7269 + WARN(1, "oversized for slot\n");
7273 + case VCHIQ_MSG_OPEN:
7274 + WARN_ON(!(VCHIQ_MSG_DSTPORT(msgid) == 0));
7275 + if (!parse_open(state, header))
7276 + goto bail_not_ready;
7278 + case VCHIQ_MSG_OPENACK:
7279 + if (size >= sizeof(struct vchiq_openack_payload)) {
7280 + const struct vchiq_openack_payload *payload =
7281 + (struct vchiq_openack_payload *)
7283 + service->peer_version = payload->version;
7285 + vchiq_log_info(vchiq_core_log_level,
7286 + "%d: prs OPENACK@%x,%x (%d->%d) v:%d",
7287 + state->id, (unsigned int)header, size,
7288 + remoteport, localport, service->peer_version);
7289 + if (service->srvstate ==
7290 + VCHIQ_SRVSTATE_OPENING) {
7291 + service->remoteport = remoteport;
7292 + vchiq_set_service_state(service,
7293 + VCHIQ_SRVSTATE_OPEN);
7294 + up(&service->remove_event);
7296 + vchiq_log_error(vchiq_core_log_level,
7297 + "OPENACK received in state %s",
7298 + srvstate_names[service->srvstate]);
7300 + case VCHIQ_MSG_CLOSE:
7301 + WARN_ON(size != 0); /* There should be no data */
7303 + vchiq_log_info(vchiq_core_log_level,
7304 + "%d: prs CLOSE@%x (%d->%d)",
7305 + state->id, (unsigned int)header,
7306 + remoteport, localport);
7308 + mark_service_closing_internal(service, 1);
7310 + if (vchiq_close_service_internal(service,
7311 + 1/*close_recvd*/) == VCHIQ_RETRY)
7312 + goto bail_not_ready;
7314 + vchiq_log_info(vchiq_core_log_level,
7315 + "Close Service %c%c%c%c s:%u d:%d",
7316 + VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
7317 + service->localport,
7318 + service->remoteport);
7320 + case VCHIQ_MSG_DATA:
7321 + vchiq_log_trace(vchiq_core_log_level,
7322 + "%d: prs DATA@%x,%x (%d->%d)",
7323 + state->id, (unsigned int)header, size,
7324 + remoteport, localport);
7326 + if ((service->remoteport == remoteport)
7327 + && (service->srvstate ==
7328 + VCHIQ_SRVSTATE_OPEN)) {
7329 + header->msgid = msgid | VCHIQ_MSGID_CLAIMED;
7330 + claim_slot(state->rx_info);
7331 + DEBUG_TRACE(PARSE_LINE);
7332 + if (make_service_callback(service,
7333 + VCHIQ_MESSAGE_AVAILABLE, header,
7334 + NULL) == VCHIQ_RETRY) {
7335 + DEBUG_TRACE(PARSE_LINE);
7336 + goto bail_not_ready;
7338 + VCHIQ_SERVICE_STATS_INC(service, ctrl_rx_count);
7339 + VCHIQ_SERVICE_STATS_ADD(service, ctrl_rx_bytes,
7342 + VCHIQ_STATS_INC(state, error_count);
7345 + case VCHIQ_MSG_CONNECT:
7346 + vchiq_log_info(vchiq_core_log_level,
7347 + "%d: prs CONNECT@%x",
7348 + state->id, (unsigned int)header);
7349 + up(&state->connect);
7351 + case VCHIQ_MSG_BULK_RX:
7352 + case VCHIQ_MSG_BULK_TX: {
7353 + VCHIQ_BULK_QUEUE_T *queue;
7354 + WARN_ON(!state->is_master);
7355 + queue = (type == VCHIQ_MSG_BULK_RX) ?
7356 + &service->bulk_tx : &service->bulk_rx;
7357 + if ((service->remoteport == remoteport)
7358 + && (service->srvstate ==
7359 + VCHIQ_SRVSTATE_OPEN)) {
7360 + VCHIQ_BULK_T *bulk;
7363 + DEBUG_TRACE(PARSE_LINE);
7364 + if (mutex_lock_interruptible(
7365 + &service->bulk_mutex) != 0) {
7366 + DEBUG_TRACE(PARSE_LINE);
7367 + goto bail_not_ready;
7370 + WARN_ON(!(queue->remote_insert < queue->remove +
7371 + VCHIQ_NUM_SERVICE_BULKS));
7372 + bulk = &queue->bulks[
7373 + BULK_INDEX(queue->remote_insert)];
7374 + bulk->remote_data =
7375 + (void *)((int *)header->data)[0];
7376 + bulk->remote_size = ((int *)header->data)[1];
7379 + vchiq_log_info(vchiq_core_log_level,
7380 + "%d: prs %s@%x (%d->%d) %x@%x",
7381 + state->id, msg_type_str(type),
7382 + (unsigned int)header,
7383 + remoteport, localport,
7384 + bulk->remote_size,
7385 + (unsigned int)bulk->remote_data);
7387 + queue->remote_insert++;
7389 + if (atomic_read(&pause_bulks_count)) {
7390 + state->deferred_bulks++;
7391 + vchiq_log_info(vchiq_core_log_level,
7392 + "%s: deferring bulk (%d)",
7394 + state->deferred_bulks);
7395 + if (state->conn_state !=
7396 + VCHIQ_CONNSTATE_PAUSE_SENT)
7398 + vchiq_core_log_level,
7399 + "%s: bulks paused in "
7400 + "unexpected state %s",
7403 + state->conn_state]);
7404 + } else if (state->conn_state ==
7405 + VCHIQ_CONNSTATE_CONNECTED) {
7406 + DEBUG_TRACE(PARSE_LINE);
7407 + resolved = resolve_bulks(service,
7411 + mutex_unlock(&service->bulk_mutex);
7413 + notify_bulks(service, queue,
7417 + case VCHIQ_MSG_BULK_RX_DONE:
7418 + case VCHIQ_MSG_BULK_TX_DONE:
7419 + WARN_ON(state->is_master);
7420 + if ((service->remoteport == remoteport)
7421 + && (service->srvstate !=
7422 + VCHIQ_SRVSTATE_FREE)) {
7423 + VCHIQ_BULK_QUEUE_T *queue;
7424 + VCHIQ_BULK_T *bulk;
7426 + queue = (type == VCHIQ_MSG_BULK_RX_DONE) ?
7427 + &service->bulk_rx : &service->bulk_tx;
7429 + DEBUG_TRACE(PARSE_LINE);
7430 + if (mutex_lock_interruptible(
7431 + &service->bulk_mutex) != 0) {
7432 + DEBUG_TRACE(PARSE_LINE);
7433 + goto bail_not_ready;
7435 + if ((int)(queue->remote_insert -
7436 + queue->local_insert) >= 0) {
7437 + vchiq_log_error(vchiq_core_log_level,
7438 + "%d: prs %s@%x (%d->%d) "
7439 + "unexpected (ri=%d,li=%d)",
7440 + state->id, msg_type_str(type),
7441 + (unsigned int)header,
7442 + remoteport, localport,
7443 + queue->remote_insert,
7444 + queue->local_insert);
7445 + mutex_unlock(&service->bulk_mutex);
7449 + BUG_ON(queue->process == queue->local_insert);
7450 + BUG_ON(queue->process != queue->remote_insert);
7452 + bulk = &queue->bulks[
7453 + BULK_INDEX(queue->remote_insert)];
7454 + bulk->actual = *(int *)header->data;
7455 + queue->remote_insert++;
7457 + vchiq_log_info(vchiq_core_log_level,
7458 + "%d: prs %s@%x (%d->%d) %x@%x",
7459 + state->id, msg_type_str(type),
7460 + (unsigned int)header,
7461 + remoteport, localport,
7462 + bulk->actual, (unsigned int)bulk->data);
7464 + vchiq_log_trace(vchiq_core_log_level,
7465 + "%d: prs:%d %cx li=%x ri=%x p=%x",
7466 + state->id, localport,
7467 + (type == VCHIQ_MSG_BULK_RX_DONE) ?
7469 + queue->local_insert,
7470 + queue->remote_insert, queue->process);
7472 + DEBUG_TRACE(PARSE_LINE);
7473 + WARN_ON(queue->process == queue->local_insert);
7474 + vchiq_complete_bulk(bulk);
7476 + mutex_unlock(&service->bulk_mutex);
7477 + DEBUG_TRACE(PARSE_LINE);
7478 + notify_bulks(service, queue, 1/*retry_poll*/);
7479 + DEBUG_TRACE(PARSE_LINE);
7482 + case VCHIQ_MSG_PADDING:
7483 + vchiq_log_trace(vchiq_core_log_level,
7484 + "%d: prs PADDING@%x,%x",
7485 + state->id, (unsigned int)header, size);
7487 + case VCHIQ_MSG_PAUSE:
7488 + /* If initiated, signal the application thread */
7489 + vchiq_log_trace(vchiq_core_log_level,
7490 + "%d: prs PAUSE@%x,%x",
7491 + state->id, (unsigned int)header, size);
7492 + if (state->conn_state == VCHIQ_CONNSTATE_PAUSED) {
7493 + vchiq_log_error(vchiq_core_log_level,
7494 + "%d: PAUSE received in state PAUSED",
7498 + if (state->conn_state != VCHIQ_CONNSTATE_PAUSE_SENT) {
7499 + /* Send a PAUSE in response */
7500 + if (queue_message(state, NULL,
7501 + VCHIQ_MAKE_MSG(VCHIQ_MSG_PAUSE, 0, 0),
7502 + NULL, 0, 0, 0) == VCHIQ_RETRY)
7503 + goto bail_not_ready;
7504 + if (state->is_master)
7505 + pause_bulks(state);
7507 + /* At this point slot_mutex is held */
7508 + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_PAUSED);
7509 + vchiq_platform_paused(state);
7511 + case VCHIQ_MSG_RESUME:
7512 + vchiq_log_trace(vchiq_core_log_level,
7513 + "%d: prs RESUME@%x,%x",
7514 + state->id, (unsigned int)header, size);
7515 + /* Release the slot mutex */
7516 + mutex_unlock(&state->slot_mutex);
7517 + if (state->is_master)
7518 + resume_bulks(state);
7519 + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_CONNECTED);
7520 + vchiq_platform_resumed(state);
7523 + case VCHIQ_MSG_REMOTE_USE:
7524 + vchiq_on_remote_use(state);
7526 + case VCHIQ_MSG_REMOTE_RELEASE:
7527 + vchiq_on_remote_release(state);
7529 + case VCHIQ_MSG_REMOTE_USE_ACTIVE:
7530 + vchiq_on_remote_use_active(state);
7534 + vchiq_log_error(vchiq_core_log_level,
7535 + "%d: prs invalid msgid %x@%x,%x",
7536 + state->id, msgid, (unsigned int)header, size);
7537 + WARN(1, "invalid message\n");
7543 + unlock_service(service);
7547 + state->rx_pos += calc_stride(size);
7549 + DEBUG_TRACE(PARSE_LINE);
7550 + /* Perform some housekeeping when the end of the slot is
7552 + if ((state->rx_pos & VCHIQ_SLOT_MASK) == 0) {
7553 + /* Remove the extra reference count. */
7554 + release_slot(state, state->rx_info, NULL, NULL);
7555 + state->rx_data = NULL;
7561 + unlock_service(service);
7564 +/* Called by the slot handler thread */
7566 +slot_handler_func(void *v)
7568 + VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
7569 + VCHIQ_SHARED_STATE_T *local = state->local;
7570 + DEBUG_INITIALISE(local)
7573 + DEBUG_COUNT(SLOT_HANDLER_COUNT);
7574 + DEBUG_TRACE(SLOT_HANDLER_LINE);
7575 + remote_event_wait(&local->trigger);
7579 + DEBUG_TRACE(SLOT_HANDLER_LINE);
7580 + if (state->poll_needed) {
7581 + /* Check if we need to suspend - may change our
7583 + vchiq_platform_check_suspend(state);
7585 + state->poll_needed = 0;
7587 + /* Handle service polling and other rare conditions here
7588 + ** out of the mainline code */
7589 + switch (state->conn_state) {
7590 + case VCHIQ_CONNSTATE_CONNECTED:
7591 + /* Poll the services as requested */
7592 + poll_services(state);
7595 + case VCHIQ_CONNSTATE_PAUSING:
7596 + if (state->is_master)
7597 + pause_bulks(state);
7598 + if (queue_message(state, NULL,
7599 + VCHIQ_MAKE_MSG(VCHIQ_MSG_PAUSE, 0, 0),
7600 + NULL, 0, 0, 0) != VCHIQ_RETRY) {
7601 + vchiq_set_conn_state(state,
7602 + VCHIQ_CONNSTATE_PAUSE_SENT);
7604 + if (state->is_master)
7605 + resume_bulks(state);
7607 + state->poll_needed = 1;
7611 + case VCHIQ_CONNSTATE_PAUSED:
7612 + vchiq_platform_resume(state);
7615 + case VCHIQ_CONNSTATE_RESUMING:
7616 + if (queue_message(state, NULL,
7617 + VCHIQ_MAKE_MSG(VCHIQ_MSG_RESUME, 0, 0),
7618 + NULL, 0, 0, 0) != VCHIQ_RETRY) {
7619 + if (state->is_master)
7620 + resume_bulks(state);
7621 + vchiq_set_conn_state(state,
7622 + VCHIQ_CONNSTATE_CONNECTED);
7623 + vchiq_platform_resumed(state);
7625 + /* This should really be impossible,
7626 + ** since the PAUSE should have flushed
7627 + ** through outstanding messages. */
7628 + vchiq_log_error(vchiq_core_log_level,
7629 + "Failed to send RESUME "
7635 + case VCHIQ_CONNSTATE_PAUSE_TIMEOUT:
7636 + case VCHIQ_CONNSTATE_RESUME_TIMEOUT:
7637 + vchiq_platform_handle_timeout(state);
7646 + DEBUG_TRACE(SLOT_HANDLER_LINE);
7647 + parse_rx_slots(state);
7653 +/* Called by the recycle thread */
7655 +recycle_func(void *v)
7657 + VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
7658 + VCHIQ_SHARED_STATE_T *local = state->local;
7661 + remote_event_wait(&local->recycle);
7663 + process_free_queue(state);
7669 +/* Called by the sync thread */
7673 + VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
7674 + VCHIQ_SHARED_STATE_T *local = state->local;
7675 + VCHIQ_HEADER_T *header = (VCHIQ_HEADER_T *)SLOT_DATA_FROM_INDEX(state,
7676 + state->remote->slot_sync);
7679 + VCHIQ_SERVICE_T *service;
7682 + unsigned int localport, remoteport;
7684 + remote_event_wait(&local->sync_trigger);
7688 + msgid = header->msgid;
7689 + size = header->size;
7690 + type = VCHIQ_MSG_TYPE(msgid);
7691 + localport = VCHIQ_MSG_DSTPORT(msgid);
7692 + remoteport = VCHIQ_MSG_SRCPORT(msgid);
7694 + service = find_service_by_port(state, localport);
7697 + vchiq_log_error(vchiq_sync_log_level,
7698 + "%d: sf %s@%x (%d->%d) - "
7699 + "invalid/closed service %d",
7700 + state->id, msg_type_str(type),
7701 + (unsigned int)header,
7702 + remoteport, localport, localport);
7703 + release_message_sync(state, header);
7707 + if (vchiq_sync_log_level >= VCHIQ_LOG_TRACE) {
7710 + svc_fourcc = service
7711 + ? service->base.fourcc
7712 + : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
7713 + vchiq_log_trace(vchiq_sync_log_level,
7714 + "Rcvd Msg %s from %c%c%c%c s:%d d:%d len:%d",
7715 + msg_type_str(type),
7716 + VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
7717 + remoteport, localport, size);
7719 + vchiq_log_dump_mem("Rcvd", 0, header->data,
7724 + case VCHIQ_MSG_OPENACK:
7725 + if (size >= sizeof(struct vchiq_openack_payload)) {
7726 + const struct vchiq_openack_payload *payload =
7727 + (struct vchiq_openack_payload *)
7729 + service->peer_version = payload->version;
7731 + vchiq_log_info(vchiq_sync_log_level,
7732 + "%d: sf OPENACK@%x,%x (%d->%d) v:%d",
7733 + state->id, (unsigned int)header, size,
7734 + remoteport, localport, service->peer_version);
7735 + if (service->srvstate == VCHIQ_SRVSTATE_OPENING) {
7736 + service->remoteport = remoteport;
7737 + vchiq_set_service_state(service,
7738 + VCHIQ_SRVSTATE_OPENSYNC);
7739 + up(&service->remove_event);
7741 + release_message_sync(state, header);
7744 + case VCHIQ_MSG_DATA:
7745 + vchiq_log_trace(vchiq_sync_log_level,
7746 + "%d: sf DATA@%x,%x (%d->%d)",
7747 + state->id, (unsigned int)header, size,
7748 + remoteport, localport);
7750 + if ((service->remoteport == remoteport) &&
7751 + (service->srvstate ==
7752 + VCHIQ_SRVSTATE_OPENSYNC)) {
7753 + if (make_service_callback(service,
7754 + VCHIQ_MESSAGE_AVAILABLE, header,
7755 + NULL) == VCHIQ_RETRY)
7756 + vchiq_log_error(vchiq_sync_log_level,
7757 + "synchronous callback to "
7758 + "service %d returns "
7765 + vchiq_log_error(vchiq_sync_log_level,
7766 + "%d: sf unexpected msgid %x@%x,%x",
7767 + state->id, msgid, (unsigned int)header, size);
7768 + release_message_sync(state, header);
7772 + unlock_service(service);
7780 +init_bulk_queue(VCHIQ_BULK_QUEUE_T *queue)
7782 + queue->local_insert = 0;
7783 + queue->remote_insert = 0;
7784 + queue->process = 0;
7785 + queue->remote_notify = 0;
7786 + queue->remove = 0;
7790 +inline const char *
7791 +get_conn_state_name(VCHIQ_CONNSTATE_T conn_state)
7793 + return conn_state_names[conn_state];
7797 +VCHIQ_SLOT_ZERO_T *
7798 +vchiq_init_slots(void *mem_base, int mem_size)
7800 + int mem_align = (VCHIQ_SLOT_SIZE - (int)mem_base) & VCHIQ_SLOT_MASK;
7801 + VCHIQ_SLOT_ZERO_T *slot_zero =
7802 + (VCHIQ_SLOT_ZERO_T *)((char *)mem_base + mem_align);
7803 + int num_slots = (mem_size - mem_align)/VCHIQ_SLOT_SIZE;
7804 + int first_data_slot = VCHIQ_SLOT_ZERO_SLOTS;
7806 + /* Ensure there is enough memory to run an absolutely minimum system */
7807 + num_slots -= first_data_slot;
7809 + if (num_slots < 4) {
7810 + vchiq_log_error(vchiq_core_log_level,
7811 + "vchiq_init_slots - insufficient memory %x bytes",
7816 + memset(slot_zero, 0, sizeof(VCHIQ_SLOT_ZERO_T));
7818 + slot_zero->magic = VCHIQ_MAGIC;
7819 + slot_zero->version = VCHIQ_VERSION;
7820 + slot_zero->version_min = VCHIQ_VERSION_MIN;
7821 + slot_zero->slot_zero_size = sizeof(VCHIQ_SLOT_ZERO_T);
7822 + slot_zero->slot_size = VCHIQ_SLOT_SIZE;
7823 + slot_zero->max_slots = VCHIQ_MAX_SLOTS;
7824 + slot_zero->max_slots_per_side = VCHIQ_MAX_SLOTS_PER_SIDE;
7826 + slot_zero->master.slot_sync = first_data_slot;
7827 + slot_zero->master.slot_first = first_data_slot + 1;
7828 + slot_zero->master.slot_last = first_data_slot + (num_slots/2) - 1;
7829 + slot_zero->slave.slot_sync = first_data_slot + (num_slots/2);
7830 + slot_zero->slave.slot_first = first_data_slot + (num_slots/2) + 1;
7831 + slot_zero->slave.slot_last = first_data_slot + num_slots - 1;
7837 +vchiq_init_state(VCHIQ_STATE_T *state, VCHIQ_SLOT_ZERO_T *slot_zero,
7840 + VCHIQ_SHARED_STATE_T *local;
7841 + VCHIQ_SHARED_STATE_T *remote;
7842 + VCHIQ_STATUS_T status;
7843 + char threadname[10];
7847 + vchiq_log_warning(vchiq_core_log_level,
7848 + "%s: slot_zero = 0x%08lx, is_master = %d",
7849 + __func__, (unsigned long)slot_zero, is_master);
7851 + /* Check the input configuration */
7853 + if (slot_zero->magic != VCHIQ_MAGIC) {
7854 + vchiq_loud_error_header();
7855 + vchiq_loud_error("Invalid VCHIQ magic value found.");
7856 + vchiq_loud_error("slot_zero=%x: magic=%x (expected %x)",
7857 + (unsigned int)slot_zero, slot_zero->magic, VCHIQ_MAGIC);
7858 + vchiq_loud_error_footer();
7859 + return VCHIQ_ERROR;
7862 + if (slot_zero->version < VCHIQ_VERSION_MIN) {
7863 + vchiq_loud_error_header();
7864 + vchiq_loud_error("Incompatible VCHIQ versions found.");
7865 + vchiq_loud_error("slot_zero=%x: VideoCore version=%d "
7867 + (unsigned int)slot_zero, slot_zero->version,
7868 + VCHIQ_VERSION_MIN);
7869 + vchiq_loud_error("Restart with a newer VideoCore image.");
7870 + vchiq_loud_error_footer();
7871 + return VCHIQ_ERROR;
7874 + if (VCHIQ_VERSION < slot_zero->version_min) {
7875 + vchiq_loud_error_header();
7876 + vchiq_loud_error("Incompatible VCHIQ versions found.");
7877 + vchiq_loud_error("slot_zero=%x: version=%d (VideoCore "
7879 + (unsigned int)slot_zero, VCHIQ_VERSION,
7880 + slot_zero->version_min);
7881 + vchiq_loud_error("Restart with a newer kernel.");
7882 + vchiq_loud_error_footer();
7883 + return VCHIQ_ERROR;
7886 + if ((slot_zero->slot_zero_size != sizeof(VCHIQ_SLOT_ZERO_T)) ||
7887 + (slot_zero->slot_size != VCHIQ_SLOT_SIZE) ||
7888 + (slot_zero->max_slots != VCHIQ_MAX_SLOTS) ||
7889 + (slot_zero->max_slots_per_side != VCHIQ_MAX_SLOTS_PER_SIDE)) {
7890 + vchiq_loud_error_header();
7891 + if (slot_zero->slot_zero_size != sizeof(VCHIQ_SLOT_ZERO_T))
7892 + vchiq_loud_error("slot_zero=%x: slot_zero_size=%x "
7894 + (unsigned int)slot_zero,
7895 + slot_zero->slot_zero_size,
7896 + sizeof(VCHIQ_SLOT_ZERO_T));
7897 + if (slot_zero->slot_size != VCHIQ_SLOT_SIZE)
7898 + vchiq_loud_error("slot_zero=%x: slot_size=%d "
7900 + (unsigned int)slot_zero, slot_zero->slot_size,
7902 + if (slot_zero->max_slots != VCHIQ_MAX_SLOTS)
7903 + vchiq_loud_error("slot_zero=%x: max_slots=%d "
7905 + (unsigned int)slot_zero, slot_zero->max_slots,
7907 + if (slot_zero->max_slots_per_side != VCHIQ_MAX_SLOTS_PER_SIDE)
7908 + vchiq_loud_error("slot_zero=%x: max_slots_per_side=%d "
7910 + (unsigned int)slot_zero,
7911 + slot_zero->max_slots_per_side,
7912 + VCHIQ_MAX_SLOTS_PER_SIDE);
7913 + vchiq_loud_error_footer();
7914 + return VCHIQ_ERROR;
7918 + local = &slot_zero->master;
7919 + remote = &slot_zero->slave;
7921 + local = &slot_zero->slave;
7922 + remote = &slot_zero->master;
7925 + if (local->initialised) {
7926 + vchiq_loud_error_header();
7927 + if (remote->initialised)
7928 + vchiq_loud_error("local state has already been "
7931 + vchiq_loud_error("master/slave mismatch - two %ss",
7932 + is_master ? "master" : "slave");
7933 + vchiq_loud_error_footer();
7934 + return VCHIQ_ERROR;
7937 + memset(state, 0, sizeof(VCHIQ_STATE_T));
7940 + state->is_master = is_master;
7943 + initialize shared state pointers
7946 + state->local = local;
7947 + state->remote = remote;
7948 + state->slot_data = (VCHIQ_SLOT_T *)slot_zero;
7951 + initialize events and mutexes
7954 + sema_init(&state->connect, 0);
7955 + mutex_init(&state->mutex);
7956 + sema_init(&state->trigger_event, 0);
7957 + sema_init(&state->recycle_event, 0);
7958 + sema_init(&state->sync_trigger_event, 0);
7959 + sema_init(&state->sync_release_event, 0);
7961 + mutex_init(&state->slot_mutex);
7962 + mutex_init(&state->recycle_mutex);
7963 + mutex_init(&state->sync_mutex);
7964 + mutex_init(&state->bulk_transfer_mutex);
7966 + sema_init(&state->slot_available_event, 0);
7967 + sema_init(&state->slot_remove_event, 0);
7968 + sema_init(&state->data_quota_event, 0);
7970 + state->slot_queue_available = 0;
7972 + for (i = 0; i < VCHIQ_MAX_SERVICES; i++) {
7973 + VCHIQ_SERVICE_QUOTA_T *service_quota =
7974 + &state->service_quotas[i];
7975 + sema_init(&service_quota->quota_event, 0);
7978 + for (i = local->slot_first; i <= local->slot_last; i++) {
7979 + local->slot_queue[state->slot_queue_available++] = i;
7980 + up(&state->slot_available_event);
7983 + state->default_slot_quota = state->slot_queue_available/2;
7984 + state->default_message_quota =
7985 + min((unsigned short)(state->default_slot_quota * 256),
7986 + (unsigned short)~0);
7988 + state->previous_data_index = -1;
7989 + state->data_use_count = 0;
7990 + state->data_quota = state->slot_queue_available - 1;
7992 + local->trigger.event = &state->trigger_event;
7993 + remote_event_create(&local->trigger);
7994 + local->tx_pos = 0;
7996 + local->recycle.event = &state->recycle_event;
7997 + remote_event_create(&local->recycle);
7998 + local->slot_queue_recycle = state->slot_queue_available;
8000 + local->sync_trigger.event = &state->sync_trigger_event;
8001 + remote_event_create(&local->sync_trigger);
8003 + local->sync_release.event = &state->sync_release_event;
8004 + remote_event_create(&local->sync_release);
8006 + /* At start-of-day, the slot is empty and available */
8007 + ((VCHIQ_HEADER_T *)SLOT_DATA_FROM_INDEX(state, local->slot_sync))->msgid
8008 + = VCHIQ_MSGID_PADDING;
8009 + remote_event_signal_local(&local->sync_release);
8011 + local->debug[DEBUG_ENTRIES] = DEBUG_MAX;
8013 + status = vchiq_platform_init_state(state);
8016 + bring up slot handler thread
8018 + snprintf(threadname, sizeof(threadname), "VCHIQ-%d", state->id);
8019 + state->slot_handler_thread = kthread_create(&slot_handler_func,
8023 + if (state->slot_handler_thread == NULL) {
8024 + vchiq_loud_error_header();
8025 + vchiq_loud_error("couldn't create thread %s", threadname);
8026 + vchiq_loud_error_footer();
8027 + return VCHIQ_ERROR;
8029 + set_user_nice(state->slot_handler_thread, -19);
8030 + wake_up_process(state->slot_handler_thread);
8032 + snprintf(threadname, sizeof(threadname), "VCHIQr-%d", state->id);
8033 + state->recycle_thread = kthread_create(&recycle_func,
8036 + if (state->recycle_thread == NULL) {
8037 + vchiq_loud_error_header();
8038 + vchiq_loud_error("couldn't create thread %s", threadname);
8039 + vchiq_loud_error_footer();
8040 + return VCHIQ_ERROR;
8042 + set_user_nice(state->recycle_thread, -19);
8043 + wake_up_process(state->recycle_thread);
8045 + snprintf(threadname, sizeof(threadname), "VCHIQs-%d", state->id);
8046 + state->sync_thread = kthread_create(&sync_func,
8049 + if (state->sync_thread == NULL) {
8050 + vchiq_loud_error_header();
8051 + vchiq_loud_error("couldn't create thread %s", threadname);
8052 + vchiq_loud_error_footer();
8053 + return VCHIQ_ERROR;
8055 + set_user_nice(state->sync_thread, -20);
8056 + wake_up_process(state->sync_thread);
8058 + BUG_ON(state->id >= VCHIQ_MAX_STATES);
8059 + vchiq_states[state->id] = state;
8061 + /* Indicate readiness to the other side */
8062 + local->initialised = 1;
8067 +/* Called from application thread when a client or server service is created. */
8069 +vchiq_add_service_internal(VCHIQ_STATE_T *state,
8070 + const VCHIQ_SERVICE_PARAMS_T *params, int srvstate,
8071 + VCHIQ_INSTANCE_T instance, VCHIQ_USERDATA_TERM_T userdata_term)
8073 + VCHIQ_SERVICE_T *service;
8075 + service = kmalloc(sizeof(VCHIQ_SERVICE_T), GFP_KERNEL);
8077 + service->base.fourcc = params->fourcc;
8078 + service->base.callback = params->callback;
8079 + service->base.userdata = params->userdata;
8080 + service->handle = VCHIQ_SERVICE_HANDLE_INVALID;
8081 + service->ref_count = 1;
8082 + service->srvstate = VCHIQ_SRVSTATE_FREE;
8083 + service->userdata_term = userdata_term;
8084 + service->localport = VCHIQ_PORT_FREE;
8085 + service->remoteport = VCHIQ_PORT_FREE;
8087 + service->public_fourcc = (srvstate == VCHIQ_SRVSTATE_OPENING) ?
8088 + VCHIQ_FOURCC_INVALID : params->fourcc;
8089 + service->client_id = 0;
8090 + service->auto_close = 1;
8091 + service->sync = 0;
8092 + service->closing = 0;
8093 + atomic_set(&service->poll_flags, 0);
8094 + service->version = params->version;
8095 + service->version_min = params->version_min;
8096 + service->state = state;
8097 + service->instance = instance;
8098 + service->service_use_count = 0;
8099 + init_bulk_queue(&service->bulk_tx);
8100 + init_bulk_queue(&service->bulk_rx);
8101 + sema_init(&service->remove_event, 0);
8102 + sema_init(&service->bulk_remove_event, 0);
8103 + mutex_init(&service->bulk_mutex);
8104 + memset(&service->stats, 0, sizeof(service->stats));
8106 + vchiq_log_error(vchiq_core_log_level,
8111 + VCHIQ_SERVICE_T **pservice = NULL;
8114 + /* Although it is perfectly possible to use service_spinlock
8115 + ** to protect the creation of services, it is overkill as it
8116 + ** disables interrupts while the array is searched.
8117 + ** The only danger is of another thread trying to create a
8118 + ** service - service deletion is safe.
8119 + ** Therefore it is preferable to use state->mutex which,
8120 + ** although slower to claim, doesn't block interrupts while
8124 + mutex_lock(&state->mutex);
8126 + /* Prepare to use a previously unused service */
8127 + if (state->unused_service < VCHIQ_MAX_SERVICES)
8128 + pservice = &state->services[state->unused_service];
8130 + if (srvstate == VCHIQ_SRVSTATE_OPENING) {
8131 + for (i = 0; i < state->unused_service; i++) {
8132 + VCHIQ_SERVICE_T *srv = state->services[i];
8134 + pservice = &state->services[i];
8139 + for (i = (state->unused_service - 1); i >= 0; i--) {
8140 + VCHIQ_SERVICE_T *srv = state->services[i];
8142 + pservice = &state->services[i];
8143 + else if ((srv->public_fourcc == params->fourcc)
8144 + && ((srv->instance != instance) ||
8145 + (srv->base.callback !=
8146 + params->callback))) {
8147 + /* There is another server using this
8148 + ** fourcc which doesn't match. */
8156 + service->localport = (pservice - state->services);
8158 + handle_seq = VCHIQ_MAX_STATES *
8159 + VCHIQ_MAX_SERVICES;
8160 + service->handle = handle_seq |
8161 + (state->id * VCHIQ_MAX_SERVICES) |
8162 + service->localport;
8163 + handle_seq += VCHIQ_MAX_STATES * VCHIQ_MAX_SERVICES;
8164 + *pservice = service;
8165 + if (pservice == &state->services[state->unused_service])
8166 + state->unused_service++;
8169 + mutex_unlock(&state->mutex);
8178 + VCHIQ_SERVICE_QUOTA_T *service_quota =
8179 + &state->service_quotas[service->localport];
8180 + service_quota->slot_quota = state->default_slot_quota;
8181 + service_quota->message_quota = state->default_message_quota;
8182 + if (service_quota->slot_use_count == 0)
8183 + service_quota->previous_tx_index =
8184 + SLOT_QUEUE_INDEX_FROM_POS(state->local_tx_pos)
8187 + /* Bring this service online */
8188 + vchiq_set_service_state(service, srvstate);
8190 + vchiq_log_info(vchiq_core_msg_log_level,
8191 + "%s Service %c%c%c%c SrcPort:%d",
8192 + (srvstate == VCHIQ_SRVSTATE_OPENING)
8194 + VCHIQ_FOURCC_AS_4CHARS(params->fourcc),
8195 + service->localport);
8198 + /* Don't unlock the service - leave it with a ref_count of 1. */
8204 +vchiq_open_service_internal(VCHIQ_SERVICE_T *service, int client_id)
8206 + struct vchiq_open_payload payload = {
8207 + service->base.fourcc,
8210 + service->version_min
8212 + VCHIQ_ELEMENT_T body = { &payload, sizeof(payload) };
8213 + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
8215 + service->client_id = client_id;
8216 + vchiq_use_service_internal(service);
8217 + status = queue_message(service->state, NULL,
8218 + VCHIQ_MAKE_MSG(VCHIQ_MSG_OPEN, service->localport, 0),
8219 + &body, 1, sizeof(payload), 1);
8220 + if (status == VCHIQ_SUCCESS) {
8221 + if (down_interruptible(&service->remove_event) != 0) {
8222 + status = VCHIQ_RETRY;
8223 + vchiq_release_service_internal(service);
8224 + } else if ((service->srvstate != VCHIQ_SRVSTATE_OPEN) &&
8225 + (service->srvstate != VCHIQ_SRVSTATE_OPENSYNC)) {
8226 + if (service->srvstate != VCHIQ_SRVSTATE_CLOSEWAIT)
8227 + vchiq_log_error(vchiq_core_log_level,
8228 + "%d: osi - srvstate = %s (ref %d)",
8229 + service->state->id,
8230 + srvstate_names[service->srvstate],
8231 + service->ref_count);
8232 + status = VCHIQ_ERROR;
8233 + VCHIQ_SERVICE_STATS_INC(service, error_count);
8234 + vchiq_release_service_internal(service);
8241 +release_service_messages(VCHIQ_SERVICE_T *service)
8243 + VCHIQ_STATE_T *state = service->state;
8244 + int slot_last = state->remote->slot_last;
8247 + /* Release any claimed messages */
8248 + for (i = state->remote->slot_first; i <= slot_last; i++) {
8249 + VCHIQ_SLOT_INFO_T *slot_info =
8250 + SLOT_INFO_FROM_INDEX(state, i);
8251 + if (slot_info->release_count != slot_info->use_count) {
8253 + (char *)SLOT_DATA_FROM_INDEX(state, i);
8254 + unsigned int pos, end;
8256 + end = VCHIQ_SLOT_SIZE;
8257 + if (data == state->rx_data)
8258 + /* This buffer is still being read from - stop
8259 + ** at the current read position */
8260 + end = state->rx_pos & VCHIQ_SLOT_MASK;
8264 + while (pos < end) {
8265 + VCHIQ_HEADER_T *header =
8266 + (VCHIQ_HEADER_T *)(data + pos);
8267 + int msgid = header->msgid;
8268 + int port = VCHIQ_MSG_DSTPORT(msgid);
8269 + if ((port == service->localport) &&
8270 + (msgid & VCHIQ_MSGID_CLAIMED)) {
8271 + vchiq_log_info(vchiq_core_log_level,
8273 + (unsigned int)header);
8274 + release_slot(state, slot_info, header,
8277 + pos += calc_stride(header->size);
8278 + if (pos > VCHIQ_SLOT_SIZE) {
8279 + vchiq_log_error(vchiq_core_log_level,
8280 + "fsi - pos %x: header %x, "
8281 + "msgid %x, header->msgid %x, "
8282 + "header->size %x",
8283 + pos, (unsigned int)header,
8284 + msgid, header->msgid,
8286 + WARN(1, "invalid slot position\n");
8294 +do_abort_bulks(VCHIQ_SERVICE_T *service)
8296 + VCHIQ_STATUS_T status;
8298 + /* Abort any outstanding bulk transfers */
8299 + if (mutex_lock_interruptible(&service->bulk_mutex) != 0)
8301 + abort_outstanding_bulks(service, &service->bulk_tx);
8302 + abort_outstanding_bulks(service, &service->bulk_rx);
8303 + mutex_unlock(&service->bulk_mutex);
8305 + status = notify_bulks(service, &service->bulk_tx, 0/*!retry_poll*/);
8306 + if (status == VCHIQ_SUCCESS)
8307 + status = notify_bulks(service, &service->bulk_rx,
8308 + 0/*!retry_poll*/);
8309 + return (status == VCHIQ_SUCCESS);
8312 +static VCHIQ_STATUS_T
8313 +close_service_complete(VCHIQ_SERVICE_T *service, int failstate)
8315 + VCHIQ_STATUS_T status;
8316 + int is_server = (service->public_fourcc != VCHIQ_FOURCC_INVALID);
8319 + switch (service->srvstate) {
8320 + case VCHIQ_SRVSTATE_OPEN:
8321 + case VCHIQ_SRVSTATE_CLOSESENT:
8322 + case VCHIQ_SRVSTATE_CLOSERECVD:
8324 + if (service->auto_close) {
8325 + service->client_id = 0;
8326 + service->remoteport = VCHIQ_PORT_FREE;
8327 + newstate = VCHIQ_SRVSTATE_LISTENING;
8329 + newstate = VCHIQ_SRVSTATE_CLOSEWAIT;
8331 + newstate = VCHIQ_SRVSTATE_CLOSED;
8332 + vchiq_set_service_state(service, newstate);
8334 + case VCHIQ_SRVSTATE_LISTENING:
8337 + vchiq_log_error(vchiq_core_log_level,
8338 + "close_service_complete(%x) called in state %s",
8339 + service->handle, srvstate_names[service->srvstate]);
8340 + WARN(1, "close_service_complete in unexpected state\n");
8341 + return VCHIQ_ERROR;
8344 + status = make_service_callback(service,
8345 + VCHIQ_SERVICE_CLOSED, NULL, NULL);
8347 + if (status != VCHIQ_RETRY) {
8348 + int uc = service->service_use_count;
8350 + /* Complete the close process */
8351 + for (i = 0; i < uc; i++)
8352 + /* cater for cases where close is forced and the
8353 + ** client may not close all it's handles */
8354 + vchiq_release_service_internal(service);
8356 + service->client_id = 0;
8357 + service->remoteport = VCHIQ_PORT_FREE;
8359 + if (service->srvstate == VCHIQ_SRVSTATE_CLOSED)
8360 + vchiq_free_service_internal(service);
8361 + else if (service->srvstate != VCHIQ_SRVSTATE_CLOSEWAIT) {
8363 + service->closing = 0;
8365 + up(&service->remove_event);
8368 + vchiq_set_service_state(service, failstate);
8373 +/* Called by the slot handler */
8375 +vchiq_close_service_internal(VCHIQ_SERVICE_T *service, int close_recvd)
8377 + VCHIQ_STATE_T *state = service->state;
8378 + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
8379 + int is_server = (service->public_fourcc != VCHIQ_FOURCC_INVALID);
8381 + vchiq_log_info(vchiq_core_log_level, "%d: csi:%d,%d (%s)",
8382 + service->state->id, service->localport, close_recvd,
8383 + srvstate_names[service->srvstate]);
8385 + switch (service->srvstate) {
8386 + case VCHIQ_SRVSTATE_CLOSED:
8387 + case VCHIQ_SRVSTATE_HIDDEN:
8388 + case VCHIQ_SRVSTATE_LISTENING:
8389 + case VCHIQ_SRVSTATE_CLOSEWAIT:
8391 + vchiq_log_error(vchiq_core_log_level,
8392 + "vchiq_close_service_internal(1) called "
8394 + srvstate_names[service->srvstate]);
8395 + else if (is_server) {
8396 + if (service->srvstate == VCHIQ_SRVSTATE_LISTENING) {
8397 + status = VCHIQ_ERROR;
8399 + service->client_id = 0;
8400 + service->remoteport = VCHIQ_PORT_FREE;
8401 + if (service->srvstate ==
8402 + VCHIQ_SRVSTATE_CLOSEWAIT)
8403 + vchiq_set_service_state(service,
8404 + VCHIQ_SRVSTATE_LISTENING);
8406 + up(&service->remove_event);
8408 + vchiq_free_service_internal(service);
8410 + case VCHIQ_SRVSTATE_OPENING:
8411 + if (close_recvd) {
8412 + /* The open was rejected - tell the user */
8413 + vchiq_set_service_state(service,
8414 + VCHIQ_SRVSTATE_CLOSEWAIT);
8415 + up(&service->remove_event);
8417 + /* Shutdown mid-open - let the other side know */
8418 + status = queue_message(state, service,
8421 + service->localport,
8422 + VCHIQ_MSG_DSTPORT(service->remoteport)),
8427 + case VCHIQ_SRVSTATE_OPENSYNC:
8428 + mutex_lock(&state->sync_mutex);
8429 + /* Drop through */
8431 + case VCHIQ_SRVSTATE_OPEN:
8432 + if (state->is_master || close_recvd) {
8433 + if (!do_abort_bulks(service))
8434 + status = VCHIQ_RETRY;
8437 + release_service_messages(service);
8439 + if (status == VCHIQ_SUCCESS)
8440 + status = queue_message(state, service,
8443 + service->localport,
8444 + VCHIQ_MSG_DSTPORT(service->remoteport)),
8447 + if (status == VCHIQ_SUCCESS) {
8450 + } else if (service->srvstate == VCHIQ_SRVSTATE_OPENSYNC) {
8451 + mutex_unlock(&state->sync_mutex);
8456 + status = close_service_complete(service,
8457 + VCHIQ_SRVSTATE_CLOSERECVD);
8460 + case VCHIQ_SRVSTATE_CLOSESENT:
8462 + /* This happens when a process is killed mid-close */
8465 + if (!state->is_master) {
8466 + if (!do_abort_bulks(service)) {
8467 + status = VCHIQ_RETRY;
8472 + if (status == VCHIQ_SUCCESS)
8473 + status = close_service_complete(service,
8474 + VCHIQ_SRVSTATE_CLOSERECVD);
8477 + case VCHIQ_SRVSTATE_CLOSERECVD:
8478 + if (!close_recvd && is_server)
8479 + /* Force into LISTENING mode */
8480 + vchiq_set_service_state(service,
8481 + VCHIQ_SRVSTATE_LISTENING);
8482 + status = close_service_complete(service,
8483 + VCHIQ_SRVSTATE_CLOSERECVD);
8487 + vchiq_log_error(vchiq_core_log_level,
8488 + "vchiq_close_service_internal(%d) called in state %s",
8489 + close_recvd, srvstate_names[service->srvstate]);
8496 +/* Called from the application process upon process death */
8498 +vchiq_terminate_service_internal(VCHIQ_SERVICE_T *service)
8500 + VCHIQ_STATE_T *state = service->state;
8502 + vchiq_log_info(vchiq_core_log_level, "%d: tsi - (%d<->%d)",
8503 + state->id, service->localport, service->remoteport);
8505 + mark_service_closing(service);
8507 + /* Mark the service for removal by the slot handler */
8508 + request_poll(state, service, VCHIQ_POLL_REMOVE);
8511 +/* Called from the slot handler */
8513 +vchiq_free_service_internal(VCHIQ_SERVICE_T *service)
8515 + VCHIQ_STATE_T *state = service->state;
8517 + vchiq_log_info(vchiq_core_log_level, "%d: fsi - (%d)",
8518 + state->id, service->localport);
8520 + switch (service->srvstate) {
8521 + case VCHIQ_SRVSTATE_OPENING:
8522 + case VCHIQ_SRVSTATE_CLOSED:
8523 + case VCHIQ_SRVSTATE_HIDDEN:
8524 + case VCHIQ_SRVSTATE_LISTENING:
8525 + case VCHIQ_SRVSTATE_CLOSEWAIT:
8528 + vchiq_log_error(vchiq_core_log_level,
8529 + "%d: fsi - (%d) in state %s",
8530 + state->id, service->localport,
8531 + srvstate_names[service->srvstate]);
8535 + vchiq_set_service_state(service, VCHIQ_SRVSTATE_FREE);
8537 + up(&service->remove_event);
8539 + /* Release the initial lock */
8540 + unlock_service(service);
8544 +vchiq_connect_internal(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance)
8546 + VCHIQ_SERVICE_T *service;
8549 + /* Find all services registered to this client and enable them. */
8551 + while ((service = next_service_by_instance(state, instance,
8553 + if (service->srvstate == VCHIQ_SRVSTATE_HIDDEN)
8554 + vchiq_set_service_state(service,
8555 + VCHIQ_SRVSTATE_LISTENING);
8556 + unlock_service(service);
8559 + if (state->conn_state == VCHIQ_CONNSTATE_DISCONNECTED) {
8560 + if (queue_message(state, NULL,
8561 + VCHIQ_MAKE_MSG(VCHIQ_MSG_CONNECT, 0, 0), NULL, 0,
8562 + 0, 1) == VCHIQ_RETRY)
8563 + return VCHIQ_RETRY;
8565 + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_CONNECTING);
8568 + if (state->conn_state == VCHIQ_CONNSTATE_CONNECTING) {
8569 + if (down_interruptible(&state->connect) != 0)
8570 + return VCHIQ_RETRY;
8572 + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_CONNECTED);
8573 + up(&state->connect);
8576 + return VCHIQ_SUCCESS;
8580 +vchiq_shutdown_internal(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance)
8582 + VCHIQ_SERVICE_T *service;
8585 + /* Find all services registered to this client and enable them. */
8587 + while ((service = next_service_by_instance(state, instance,
8589 + (void)vchiq_remove_service(service->handle);
8590 + unlock_service(service);
8593 + return VCHIQ_SUCCESS;
8597 +vchiq_pause_internal(VCHIQ_STATE_T *state)
8599 + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
8601 + switch (state->conn_state) {
8602 + case VCHIQ_CONNSTATE_CONNECTED:
8603 + /* Request a pause */
8604 + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_PAUSING);
8605 + request_poll(state, NULL, 0);
8608 + vchiq_log_error(vchiq_core_log_level,
8609 + "vchiq_pause_internal in state %s\n",
8610 + conn_state_names[state->conn_state]);
8611 + status = VCHIQ_ERROR;
8612 + VCHIQ_STATS_INC(state, error_count);
8620 +vchiq_resume_internal(VCHIQ_STATE_T *state)
8622 + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
8624 + if (state->conn_state == VCHIQ_CONNSTATE_PAUSED) {
8625 + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_RESUMING);
8626 + request_poll(state, NULL, 0);
8628 + status = VCHIQ_ERROR;
8629 + VCHIQ_STATS_INC(state, error_count);
8636 +vchiq_close_service(VCHIQ_SERVICE_HANDLE_T handle)
8638 + /* Unregister the service */
8639 + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
8640 + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
8643 + return VCHIQ_ERROR;
8645 + vchiq_log_info(vchiq_core_log_level,
8646 + "%d: close_service:%d",
8647 + service->state->id, service->localport);
8649 + if ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
8650 + (service->srvstate == VCHIQ_SRVSTATE_LISTENING) ||
8651 + (service->srvstate == VCHIQ_SRVSTATE_HIDDEN)) {
8652 + unlock_service(service);
8653 + return VCHIQ_ERROR;
8656 + mark_service_closing(service);
8658 + if (current == service->state->slot_handler_thread) {
8659 + status = vchiq_close_service_internal(service,
8660 + 0/*!close_recvd*/);
8661 + BUG_ON(status == VCHIQ_RETRY);
8663 + /* Mark the service for termination by the slot handler */
8664 + request_poll(service->state, service, VCHIQ_POLL_TERMINATE);
8668 + if (down_interruptible(&service->remove_event) != 0) {
8669 + status = VCHIQ_RETRY;
8673 + if ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
8674 + (service->srvstate == VCHIQ_SRVSTATE_LISTENING) ||
8675 + (service->srvstate == VCHIQ_SRVSTATE_OPEN))
8678 + vchiq_log_warning(vchiq_core_log_level,
8679 + "%d: close_service:%d - waiting in state %s",
8680 + service->state->id, service->localport,
8681 + srvstate_names[service->srvstate]);
8684 + if ((status == VCHIQ_SUCCESS) &&
8685 + (service->srvstate != VCHIQ_SRVSTATE_FREE) &&
8686 + (service->srvstate != VCHIQ_SRVSTATE_LISTENING))
8687 + status = VCHIQ_ERROR;
8689 + unlock_service(service);
8695 +vchiq_remove_service(VCHIQ_SERVICE_HANDLE_T handle)
8697 + /* Unregister the service */
8698 + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
8699 + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
8702 + return VCHIQ_ERROR;
8704 + vchiq_log_info(vchiq_core_log_level,
8705 + "%d: remove_service:%d",
8706 + service->state->id, service->localport);
8708 + if (service->srvstate == VCHIQ_SRVSTATE_FREE) {
8709 + unlock_service(service);
8710 + return VCHIQ_ERROR;
8713 + mark_service_closing(service);
8715 + if ((service->srvstate == VCHIQ_SRVSTATE_HIDDEN) ||
8716 + (current == service->state->slot_handler_thread)) {
8717 + /* Make it look like a client, because it must be removed and
8718 + not left in the LISTENING state. */
8719 + service->public_fourcc = VCHIQ_FOURCC_INVALID;
8721 + status = vchiq_close_service_internal(service,
8722 + 0/*!close_recvd*/);
8723 + BUG_ON(status == VCHIQ_RETRY);
8725 + /* Mark the service for removal by the slot handler */
8726 + request_poll(service->state, service, VCHIQ_POLL_REMOVE);
8729 + if (down_interruptible(&service->remove_event) != 0) {
8730 + status = VCHIQ_RETRY;
8734 + if ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
8735 + (service->srvstate == VCHIQ_SRVSTATE_OPEN))
8738 + vchiq_log_warning(vchiq_core_log_level,
8739 + "%d: remove_service:%d - waiting in state %s",
8740 + service->state->id, service->localport,
8741 + srvstate_names[service->srvstate]);
8744 + if ((status == VCHIQ_SUCCESS) &&
8745 + (service->srvstate != VCHIQ_SRVSTATE_FREE))
8746 + status = VCHIQ_ERROR;
8748 + unlock_service(service);
8754 +/* This function may be called by kernel threads or user threads.
8755 + * User threads may receive VCHIQ_RETRY to indicate that a signal has been
8756 + * received and the call should be retried after being returned to user
8758 + * When called in blocking mode, the userdata field points to a bulk_waiter
8762 +vchiq_bulk_transfer(VCHIQ_SERVICE_HANDLE_T handle,
8763 + VCHI_MEM_HANDLE_T memhandle, void *offset, int size, void *userdata,
8764 + VCHIQ_BULK_MODE_T mode, VCHIQ_BULK_DIR_T dir)
8766 + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
8767 + VCHIQ_BULK_QUEUE_T *queue;
8768 + VCHIQ_BULK_T *bulk;
8769 + VCHIQ_STATE_T *state;
8770 + struct bulk_waiter *bulk_waiter = NULL;
8771 + const char dir_char = (dir == VCHIQ_BULK_TRANSMIT) ? 't' : 'r';
8772 + const int dir_msgtype = (dir == VCHIQ_BULK_TRANSMIT) ?
8773 + VCHIQ_MSG_BULK_TX : VCHIQ_MSG_BULK_RX;
8774 + VCHIQ_STATUS_T status = VCHIQ_ERROR;
8777 + (service->srvstate != VCHIQ_SRVSTATE_OPEN) ||
8778 + ((memhandle == VCHI_MEM_HANDLE_INVALID) && (offset == NULL)) ||
8779 + (vchiq_check_service(service) != VCHIQ_SUCCESS))
8783 + case VCHIQ_BULK_MODE_NOCALLBACK:
8784 + case VCHIQ_BULK_MODE_CALLBACK:
8786 + case VCHIQ_BULK_MODE_BLOCKING:
8787 + bulk_waiter = (struct bulk_waiter *)userdata;
8788 + sema_init(&bulk_waiter->event, 0);
8789 + bulk_waiter->actual = 0;
8790 + bulk_waiter->bulk = NULL;
8792 + case VCHIQ_BULK_MODE_WAITING:
8793 + bulk_waiter = (struct bulk_waiter *)userdata;
8794 + bulk = bulk_waiter->bulk;
8800 + state = service->state;
8802 + queue = (dir == VCHIQ_BULK_TRANSMIT) ?
8803 + &service->bulk_tx : &service->bulk_rx;
8805 + if (mutex_lock_interruptible(&service->bulk_mutex) != 0) {
8806 + status = VCHIQ_RETRY;
8810 + if (queue->local_insert == queue->remove + VCHIQ_NUM_SERVICE_BULKS) {
8811 + VCHIQ_SERVICE_STATS_INC(service, bulk_stalls);
8813 + mutex_unlock(&service->bulk_mutex);
8814 + if (down_interruptible(&service->bulk_remove_event)
8816 + status = VCHIQ_RETRY;
8819 + if (mutex_lock_interruptible(&service->bulk_mutex)
8821 + status = VCHIQ_RETRY;
8824 + } while (queue->local_insert == queue->remove +
8825 + VCHIQ_NUM_SERVICE_BULKS);
8828 + bulk = &queue->bulks[BULK_INDEX(queue->local_insert)];
8830 + bulk->mode = mode;
8832 + bulk->userdata = userdata;
8833 + bulk->size = size;
8834 + bulk->actual = VCHIQ_BULK_ACTUAL_ABORTED;
8836 + if (vchiq_prepare_bulk_data(bulk, memhandle, offset, size, dir) !=
8838 + goto unlock_error_exit;
8842 + vchiq_log_info(vchiq_core_log_level,
8843 + "%d: bt (%d->%d) %cx %x@%x %x",
8845 + service->localport, service->remoteport, dir_char,
8846 + size, (unsigned int)bulk->data, (unsigned int)userdata);
8848 + if (state->is_master) {
8849 + queue->local_insert++;
8850 + if (resolve_bulks(service, queue))
8851 + request_poll(state, service,
8852 + (dir == VCHIQ_BULK_TRANSMIT) ?
8853 + VCHIQ_POLL_TXNOTIFY : VCHIQ_POLL_RXNOTIFY);
8855 + int payload[2] = { (int)bulk->data, bulk->size };
8856 + VCHIQ_ELEMENT_T element = { payload, sizeof(payload) };
8858 + status = queue_message(state, NULL,
8859 + VCHIQ_MAKE_MSG(dir_msgtype,
8860 + service->localport, service->remoteport),
8861 + &element, 1, sizeof(payload), 1);
8862 + if (status != VCHIQ_SUCCESS) {
8863 + vchiq_complete_bulk(bulk);
8864 + goto unlock_error_exit;
8866 + queue->local_insert++;
8869 + mutex_unlock(&service->bulk_mutex);
8871 + vchiq_log_trace(vchiq_core_log_level,
8872 + "%d: bt:%d %cx li=%x ri=%x p=%x",
8874 + service->localport, dir_char,
8875 + queue->local_insert, queue->remote_insert, queue->process);
8878 + unlock_service(service);
8880 + status = VCHIQ_SUCCESS;
8882 + if (bulk_waiter) {
8883 + bulk_waiter->bulk = bulk;
8884 + if (down_interruptible(&bulk_waiter->event) != 0)
8885 + status = VCHIQ_RETRY;
8886 + else if (bulk_waiter->actual == VCHIQ_BULK_ACTUAL_ABORTED)
8887 + status = VCHIQ_ERROR;
8893 + mutex_unlock(&service->bulk_mutex);
8897 + unlock_service(service);
8902 +vchiq_queue_message(VCHIQ_SERVICE_HANDLE_T handle,
8903 + const VCHIQ_ELEMENT_T *elements, unsigned int count)
8905 + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
8906 + VCHIQ_STATUS_T status = VCHIQ_ERROR;
8908 + unsigned int size = 0;
8912 + (vchiq_check_service(service) != VCHIQ_SUCCESS))
8915 + for (i = 0; i < (unsigned int)count; i++) {
8916 + if (elements[i].size) {
8917 + if (elements[i].data == NULL) {
8918 + VCHIQ_SERVICE_STATS_INC(service, error_count);
8921 + size += elements[i].size;
8925 + if (size > VCHIQ_MAX_MSG_SIZE) {
8926 + VCHIQ_SERVICE_STATS_INC(service, error_count);
8930 + switch (service->srvstate) {
8931 + case VCHIQ_SRVSTATE_OPEN:
8932 + status = queue_message(service->state, service,
8933 + VCHIQ_MAKE_MSG(VCHIQ_MSG_DATA,
8934 + service->localport,
8935 + service->remoteport),
8936 + elements, count, size, 1);
8938 + case VCHIQ_SRVSTATE_OPENSYNC:
8939 + status = queue_message_sync(service->state, service,
8940 + VCHIQ_MAKE_MSG(VCHIQ_MSG_DATA,
8941 + service->localport,
8942 + service->remoteport),
8943 + elements, count, size, 1);
8946 + status = VCHIQ_ERROR;
8952 + unlock_service(service);
8958 +vchiq_release_message(VCHIQ_SERVICE_HANDLE_T handle, VCHIQ_HEADER_T *header)
8960 + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
8961 + VCHIQ_SHARED_STATE_T *remote;
8962 + VCHIQ_STATE_T *state;
8968 + state = service->state;
8969 + remote = state->remote;
8971 + slot_index = SLOT_INDEX_FROM_DATA(state, (void *)header);
8973 + if ((slot_index >= remote->slot_first) &&
8974 + (slot_index <= remote->slot_last)) {
8975 + int msgid = header->msgid;
8976 + if (msgid & VCHIQ_MSGID_CLAIMED) {
8977 + VCHIQ_SLOT_INFO_T *slot_info =
8978 + SLOT_INFO_FROM_INDEX(state, slot_index);
8980 + release_slot(state, slot_info, header, service);
8982 + } else if (slot_index == remote->slot_sync)
8983 + release_message_sync(state, header);
8985 + unlock_service(service);
8989 +release_message_sync(VCHIQ_STATE_T *state, VCHIQ_HEADER_T *header)
8991 + header->msgid = VCHIQ_MSGID_PADDING;
8993 + remote_event_signal(&state->remote->sync_release);
8997 +vchiq_get_peer_version(VCHIQ_SERVICE_HANDLE_T handle, short *peer_version)
8999 + VCHIQ_STATUS_T status = VCHIQ_ERROR;
9000 + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
9003 + (vchiq_check_service(service) != VCHIQ_SUCCESS) ||
9006 + *peer_version = service->peer_version;
9007 + status = VCHIQ_SUCCESS;
9011 + unlock_service(service);
9016 +vchiq_get_config(VCHIQ_INSTANCE_T instance,
9017 + int config_size, VCHIQ_CONFIG_T *pconfig)
9019 + VCHIQ_CONFIG_T config;
9023 + config.max_msg_size = VCHIQ_MAX_MSG_SIZE;
9024 + config.bulk_threshold = VCHIQ_MAX_MSG_SIZE;
9025 + config.max_outstanding_bulks = VCHIQ_NUM_SERVICE_BULKS;
9026 + config.max_services = VCHIQ_MAX_SERVICES;
9027 + config.version = VCHIQ_VERSION;
9028 + config.version_min = VCHIQ_VERSION_MIN;
9030 + if (config_size > sizeof(VCHIQ_CONFIG_T))
9031 + return VCHIQ_ERROR;
9033 + memcpy(pconfig, &config,
9034 + min(config_size, (int)(sizeof(VCHIQ_CONFIG_T))));
9036 + return VCHIQ_SUCCESS;
9040 +vchiq_set_service_option(VCHIQ_SERVICE_HANDLE_T handle,
9041 + VCHIQ_SERVICE_OPTION_T option, int value)
9043 + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
9044 + VCHIQ_STATUS_T status = VCHIQ_ERROR;
9048 + case VCHIQ_SERVICE_OPTION_AUTOCLOSE:
9049 + service->auto_close = value;
9050 + status = VCHIQ_SUCCESS;
9053 + case VCHIQ_SERVICE_OPTION_SLOT_QUOTA: {
9054 + VCHIQ_SERVICE_QUOTA_T *service_quota =
9055 + &service->state->service_quotas[
9056 + service->localport];
9058 + value = service->state->default_slot_quota;
9059 + if ((value >= service_quota->slot_use_count) &&
9060 + (value < (unsigned short)~0)) {
9061 + service_quota->slot_quota = value;
9062 + if ((value >= service_quota->slot_use_count) &&
9063 + (service_quota->message_quota >=
9064 + service_quota->message_use_count)) {
9065 + /* Signal the service that it may have
9066 + ** dropped below its quota */
9067 + up(&service_quota->quota_event);
9069 + status = VCHIQ_SUCCESS;
9073 + case VCHIQ_SERVICE_OPTION_MESSAGE_QUOTA: {
9074 + VCHIQ_SERVICE_QUOTA_T *service_quota =
9075 + &service->state->service_quotas[
9076 + service->localport];
9078 + value = service->state->default_message_quota;
9079 + if ((value >= service_quota->message_use_count) &&
9080 + (value < (unsigned short)~0)) {
9081 + service_quota->message_quota = value;
9083 + service_quota->message_use_count) &&
9084 + (service_quota->slot_quota >=
9085 + service_quota->slot_use_count))
9086 + /* Signal the service that it may have
9087 + ** dropped below its quota */
9088 + up(&service_quota->quota_event);
9089 + status = VCHIQ_SUCCESS;
9093 + case VCHIQ_SERVICE_OPTION_SYNCHRONOUS:
9094 + if ((service->srvstate == VCHIQ_SRVSTATE_HIDDEN) ||
9095 + (service->srvstate ==
9096 + VCHIQ_SRVSTATE_LISTENING)) {
9097 + service->sync = value;
9098 + status = VCHIQ_SUCCESS;
9105 + unlock_service(service);
9112 +vchiq_dump_shared_state(void *dump_context, VCHIQ_STATE_T *state,
9113 + VCHIQ_SHARED_STATE_T *shared, const char *label)
9115 + static const char *const debug_names[] = {
9117 + "SLOT_HANDLER_COUNT",
9118 + "SLOT_HANDLER_LINE",
9122 + "AWAIT_COMPLETION_LINE",
9123 + "DEQUEUE_MESSAGE_LINE",
9124 + "SERVICE_CALLBACK_LINE",
9125 + "MSG_QUEUE_FULL_COUNT",
9126 + "COMPLETION_QUEUE_FULL_COUNT"
9132 + len = snprintf(buf, sizeof(buf),
9133 + " %s: slots %d-%d tx_pos=%x recycle=%x",
9134 + label, shared->slot_first, shared->slot_last,
9135 + shared->tx_pos, shared->slot_queue_recycle);
9136 + vchiq_dump(dump_context, buf, len + 1);
9138 + len = snprintf(buf, sizeof(buf),
9139 + " Slots claimed:");
9140 + vchiq_dump(dump_context, buf, len + 1);
9142 + for (i = shared->slot_first; i <= shared->slot_last; i++) {
9143 + VCHIQ_SLOT_INFO_T slot_info = *SLOT_INFO_FROM_INDEX(state, i);
9144 + if (slot_info.use_count != slot_info.release_count) {
9145 + len = snprintf(buf, sizeof(buf),
9146 + " %d: %d/%d", i, slot_info.use_count,
9147 + slot_info.release_count);
9148 + vchiq_dump(dump_context, buf, len + 1);
9152 + for (i = 1; i < shared->debug[DEBUG_ENTRIES]; i++) {
9153 + len = snprintf(buf, sizeof(buf), " DEBUG: %s = %d(%x)",
9154 + debug_names[i], shared->debug[i], shared->debug[i]);
9155 + vchiq_dump(dump_context, buf, len + 1);
9160 +vchiq_dump_state(void *dump_context, VCHIQ_STATE_T *state)
9166 + len = snprintf(buf, sizeof(buf), "State %d: %s", state->id,
9167 + conn_state_names[state->conn_state]);
9168 + vchiq_dump(dump_context, buf, len + 1);
9170 + len = snprintf(buf, sizeof(buf),
9171 + " tx_pos=%x(@%x), rx_pos=%x(@%x)",
9172 + state->local->tx_pos,
9173 + (uint32_t)state->tx_data +
9174 + (state->local_tx_pos & VCHIQ_SLOT_MASK),
9176 + (uint32_t)state->rx_data +
9177 + (state->rx_pos & VCHIQ_SLOT_MASK));
9178 + vchiq_dump(dump_context, buf, len + 1);
9180 + len = snprintf(buf, sizeof(buf),
9181 + " Version: %d (min %d)",
9182 + VCHIQ_VERSION, VCHIQ_VERSION_MIN);
9183 + vchiq_dump(dump_context, buf, len + 1);
9185 + if (VCHIQ_ENABLE_STATS) {
9186 + len = snprintf(buf, sizeof(buf),
9187 + " Stats: ctrl_tx_count=%d, ctrl_rx_count=%d, "
9189 + state->stats.ctrl_tx_count, state->stats.ctrl_rx_count,
9190 + state->stats.error_count);
9191 + vchiq_dump(dump_context, buf, len + 1);
9194 + len = snprintf(buf, sizeof(buf),
9195 + " Slots: %d available (%d data), %d recyclable, %d stalls "
9197 + ((state->slot_queue_available * VCHIQ_SLOT_SIZE) -
9198 + state->local_tx_pos) / VCHIQ_SLOT_SIZE,
9199 + state->data_quota - state->data_use_count,
9200 + state->local->slot_queue_recycle - state->slot_queue_available,
9201 + state->stats.slot_stalls, state->stats.data_stalls);
9202 + vchiq_dump(dump_context, buf, len + 1);
9204 + vchiq_dump_platform_state(dump_context);
9206 + vchiq_dump_shared_state(dump_context, state, state->local, "Local");
9207 + vchiq_dump_shared_state(dump_context, state, state->remote, "Remote");
9209 + vchiq_dump_platform_instances(dump_context);
9211 + for (i = 0; i < state->unused_service; i++) {
9212 + VCHIQ_SERVICE_T *service = find_service_by_port(state, i);
9215 + vchiq_dump_service_state(dump_context, service);
9216 + unlock_service(service);
9222 +vchiq_dump_service_state(void *dump_context, VCHIQ_SERVICE_T *service)
9227 + len = snprintf(buf, sizeof(buf), "Service %d: %s (ref %u)",
9228 + service->localport, srvstate_names[service->srvstate],
9229 + service->ref_count - 1); /*Don't include the lock just taken*/
9231 + if (service->srvstate != VCHIQ_SRVSTATE_FREE) {
9232 + char remoteport[30];
9233 + VCHIQ_SERVICE_QUOTA_T *service_quota =
9234 + &service->state->service_quotas[service->localport];
9235 + int fourcc = service->base.fourcc;
9236 + int tx_pending, rx_pending;
9237 + if (service->remoteport != VCHIQ_PORT_FREE) {
9238 + int len2 = snprintf(remoteport, sizeof(remoteport),
9239 + "%d", service->remoteport);
9240 + if (service->public_fourcc != VCHIQ_FOURCC_INVALID)
9241 + snprintf(remoteport + len2,
9242 + sizeof(remoteport) - len2,
9243 + " (client %x)", service->client_id);
9245 + strcpy(remoteport, "n/a");
9247 + len += snprintf(buf + len, sizeof(buf) - len,
9248 + " '%c%c%c%c' remote %s (msg use %d/%d, slot use %d/%d)",
9249 + VCHIQ_FOURCC_AS_4CHARS(fourcc),
9251 + service_quota->message_use_count,
9252 + service_quota->message_quota,
9253 + service_quota->slot_use_count,
9254 + service_quota->slot_quota);
9256 + vchiq_dump(dump_context, buf, len + 1);
9258 + tx_pending = service->bulk_tx.local_insert -
9259 + service->bulk_tx.remote_insert;
9261 + rx_pending = service->bulk_rx.local_insert -
9262 + service->bulk_rx.remote_insert;
9264 + len = snprintf(buf, sizeof(buf),
9265 + " Bulk: tx_pending=%d (size %d),"
9266 + " rx_pending=%d (size %d)",
9268 + tx_pending ? service->bulk_tx.bulks[
9269 + BULK_INDEX(service->bulk_tx.remove)].size : 0,
9271 + rx_pending ? service->bulk_rx.bulks[
9272 + BULK_INDEX(service->bulk_rx.remove)].size : 0);
9274 + if (VCHIQ_ENABLE_STATS) {
9275 + vchiq_dump(dump_context, buf, len + 1);
9277 + len = snprintf(buf, sizeof(buf),
9278 + " Ctrl: tx_count=%d, tx_bytes=%llu, "
9279 + "rx_count=%d, rx_bytes=%llu",
9280 + service->stats.ctrl_tx_count,
9281 + service->stats.ctrl_tx_bytes,
9282 + service->stats.ctrl_rx_count,
9283 + service->stats.ctrl_rx_bytes);
9284 + vchiq_dump(dump_context, buf, len + 1);
9286 + len = snprintf(buf, sizeof(buf),
9287 + " Bulk: tx_count=%d, tx_bytes=%llu, "
9288 + "rx_count=%d, rx_bytes=%llu",
9289 + service->stats.bulk_tx_count,
9290 + service->stats.bulk_tx_bytes,
9291 + service->stats.bulk_rx_count,
9292 + service->stats.bulk_rx_bytes);
9293 + vchiq_dump(dump_context, buf, len + 1);
9295 + len = snprintf(buf, sizeof(buf),
9296 + " %d quota stalls, %d slot stalls, "
9297 + "%d bulk stalls, %d aborted, %d errors",
9298 + service->stats.quota_stalls,
9299 + service->stats.slot_stalls,
9300 + service->stats.bulk_stalls,
9301 + service->stats.bulk_aborted_count,
9302 + service->stats.error_count);
9306 + vchiq_dump(dump_context, buf, len + 1);
9308 + if (service->srvstate != VCHIQ_SRVSTATE_FREE)
9309 + vchiq_dump_platform_service_state(dump_context, service);
9314 +vchiq_loud_error_header(void)
9316 + vchiq_log_error(vchiq_core_log_level,
9317 + "============================================================"
9318 + "================");
9319 + vchiq_log_error(vchiq_core_log_level,
9320 + "============================================================"
9321 + "================");
9322 + vchiq_log_error(vchiq_core_log_level, "=====");
9326 +vchiq_loud_error_footer(void)
9328 + vchiq_log_error(vchiq_core_log_level, "=====");
9329 + vchiq_log_error(vchiq_core_log_level,
9330 + "============================================================"
9331 + "================");
9332 + vchiq_log_error(vchiq_core_log_level,
9333 + "============================================================"
9334 + "================");
9338 +VCHIQ_STATUS_T vchiq_send_remote_use(VCHIQ_STATE_T *state)
9340 + VCHIQ_STATUS_T status = VCHIQ_RETRY;
9341 + if (state->conn_state != VCHIQ_CONNSTATE_DISCONNECTED)
9342 + status = queue_message(state, NULL,
9343 + VCHIQ_MAKE_MSG(VCHIQ_MSG_REMOTE_USE, 0, 0),
9348 +VCHIQ_STATUS_T vchiq_send_remote_release(VCHIQ_STATE_T *state)
9350 + VCHIQ_STATUS_T status = VCHIQ_RETRY;
9351 + if (state->conn_state != VCHIQ_CONNSTATE_DISCONNECTED)
9352 + status = queue_message(state, NULL,
9353 + VCHIQ_MAKE_MSG(VCHIQ_MSG_REMOTE_RELEASE, 0, 0),
9358 +VCHIQ_STATUS_T vchiq_send_remote_use_active(VCHIQ_STATE_T *state)
9360 + VCHIQ_STATUS_T status = VCHIQ_RETRY;
9361 + if (state->conn_state != VCHIQ_CONNSTATE_DISCONNECTED)
9362 + status = queue_message(state, NULL,
9363 + VCHIQ_MAKE_MSG(VCHIQ_MSG_REMOTE_USE_ACTIVE, 0, 0),
9368 +void vchiq_log_dump_mem(const char *label, uint32_t addr, const void *voidMem,
9371 + const uint8_t *mem = (const uint8_t *)voidMem;
9373 + char lineBuf[100];
9376 + while (numBytes > 0) {
9379 + for (offset = 0; offset < 16; offset++) {
9380 + if (offset < numBytes)
9381 + s += snprintf(s, 4, "%02x ", mem[offset]);
9383 + s += snprintf(s, 4, " ");
9386 + for (offset = 0; offset < 16; offset++) {
9387 + if (offset < numBytes) {
9388 + uint8_t ch = mem[offset];
9390 + if ((ch < ' ') || (ch > '~'))
9397 + if ((label != NULL) && (*label != '\0'))
9398 + vchiq_log_trace(VCHIQ_LOG_TRACE,
9399 + "%s: %08x: %s", label, addr, lineBuf);
9401 + vchiq_log_trace(VCHIQ_LOG_TRACE,
9402 + "%08x: %s", addr, lineBuf);
9406 + if (numBytes > 16)
9412 diff --git a/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.h b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.h
9413 new file mode 100644
9414 index 0000000..47cdf27
9416 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.h
9419 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
9421 + * Redistribution and use in source and binary forms, with or without
9422 + * modification, are permitted provided that the following conditions
9424 + * 1. Redistributions of source code must retain the above copyright
9425 + * notice, this list of conditions, and the following disclaimer,
9426 + * without modification.
9427 + * 2. Redistributions in binary form must reproduce the above copyright
9428 + * notice, this list of conditions and the following disclaimer in the
9429 + * documentation and/or other materials provided with the distribution.
9430 + * 3. The names of the above-listed copyright holders may not be used
9431 + * to endorse or promote products derived from this software without
9432 + * specific prior written permission.
9434 + * ALTERNATIVELY, this software may be distributed under the terms of the
9435 + * GNU General Public License ("GPL") version 2, as published by the Free
9436 + * Software Foundation.
9438 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
9439 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
9440 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
9441 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
9442 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
9443 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
9444 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
9445 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
9446 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
9447 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
9448 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
9451 +#ifndef VCHIQ_CORE_H
9452 +#define VCHIQ_CORE_H
9454 +#include <linux/mutex.h>
9455 +#include <linux/semaphore.h>
9456 +#include <linux/kthread.h>
9458 +#include "vchiq_cfg.h"
9462 +/* Run time control of log level, based on KERN_XXX level. */
9463 +#define VCHIQ_LOG_DEFAULT 4
9464 +#define VCHIQ_LOG_ERROR 3
9465 +#define VCHIQ_LOG_WARNING 4
9466 +#define VCHIQ_LOG_INFO 6
9467 +#define VCHIQ_LOG_TRACE 7
9469 +#define VCHIQ_LOG_PREFIX KERN_INFO "vchiq: "
9471 +#ifndef vchiq_log_error
9472 +#define vchiq_log_error(cat, fmt, ...) \
9473 + do { if (cat >= VCHIQ_LOG_ERROR) \
9474 + printk(VCHIQ_LOG_PREFIX fmt "\n", ##__VA_ARGS__); } while (0)
9476 +#ifndef vchiq_log_warning
9477 +#define vchiq_log_warning(cat, fmt, ...) \
9478 + do { if (cat >= VCHIQ_LOG_WARNING) \
9479 + printk(VCHIQ_LOG_PREFIX fmt "\n", ##__VA_ARGS__); } while (0)
9481 +#ifndef vchiq_log_info
9482 +#define vchiq_log_info(cat, fmt, ...) \
9483 + do { if (cat >= VCHIQ_LOG_INFO) \
9484 + printk(VCHIQ_LOG_PREFIX fmt "\n", ##__VA_ARGS__); } while (0)
9486 +#ifndef vchiq_log_trace
9487 +#define vchiq_log_trace(cat, fmt, ...) \
9488 + do { if (cat >= VCHIQ_LOG_TRACE) \
9489 + printk(VCHIQ_LOG_PREFIX fmt "\n", ##__VA_ARGS__); } while (0)
9492 +#define vchiq_loud_error(...) \
9493 + vchiq_log_error(vchiq_core_log_level, "===== " __VA_ARGS__)
9495 +#ifndef vchiq_static_assert
9496 +#define vchiq_static_assert(cond) __attribute__((unused)) \
9497 + extern int vchiq_static_assert[(cond) ? 1 : -1]
9500 +#define IS_POW2(x) (x && ((x & (x - 1)) == 0))
9502 +/* Ensure that the slot size and maximum number of slots are powers of 2 */
9503 +vchiq_static_assert(IS_POW2(VCHIQ_SLOT_SIZE));
9504 +vchiq_static_assert(IS_POW2(VCHIQ_MAX_SLOTS));
9505 +vchiq_static_assert(IS_POW2(VCHIQ_MAX_SLOTS_PER_SIDE));
9507 +#define VCHIQ_SLOT_MASK (VCHIQ_SLOT_SIZE - 1)
9508 +#define VCHIQ_SLOT_QUEUE_MASK (VCHIQ_MAX_SLOTS_PER_SIDE - 1)
9509 +#define VCHIQ_SLOT_ZERO_SLOTS ((sizeof(VCHIQ_SLOT_ZERO_T) + \
9510 + VCHIQ_SLOT_SIZE - 1) / VCHIQ_SLOT_SIZE)
9512 +#define VCHIQ_MSG_PADDING 0 /* - */
9513 +#define VCHIQ_MSG_CONNECT 1 /* - */
9514 +#define VCHIQ_MSG_OPEN 2 /* + (srcport, -), fourcc, client_id */
9515 +#define VCHIQ_MSG_OPENACK 3 /* + (srcport, dstport) */
9516 +#define VCHIQ_MSG_CLOSE 4 /* + (srcport, dstport) */
9517 +#define VCHIQ_MSG_DATA 5 /* + (srcport, dstport) */
9518 +#define VCHIQ_MSG_BULK_RX 6 /* + (srcport, dstport), data, size */
9519 +#define VCHIQ_MSG_BULK_TX 7 /* + (srcport, dstport), data, size */
9520 +#define VCHIQ_MSG_BULK_RX_DONE 8 /* + (srcport, dstport), actual */
9521 +#define VCHIQ_MSG_BULK_TX_DONE 9 /* + (srcport, dstport), actual */
9522 +#define VCHIQ_MSG_PAUSE 10 /* - */
9523 +#define VCHIQ_MSG_RESUME 11 /* - */
9524 +#define VCHIQ_MSG_REMOTE_USE 12 /* - */
9525 +#define VCHIQ_MSG_REMOTE_RELEASE 13 /* - */
9526 +#define VCHIQ_MSG_REMOTE_USE_ACTIVE 14 /* - */
9528 +#define VCHIQ_PORT_MAX (VCHIQ_MAX_SERVICES - 1)
9529 +#define VCHIQ_PORT_FREE 0x1000
9530 +#define VCHIQ_PORT_IS_VALID(port) (port < VCHIQ_PORT_FREE)
9531 +#define VCHIQ_MAKE_MSG(type, srcport, dstport) \
9532 + ((type<<24) | (srcport<<12) | (dstport<<0))
9533 +#define VCHIQ_MSG_TYPE(msgid) ((unsigned int)msgid >> 24)
9534 +#define VCHIQ_MSG_SRCPORT(msgid) \
9535 + (unsigned short)(((unsigned int)msgid >> 12) & 0xfff)
9536 +#define VCHIQ_MSG_DSTPORT(msgid) \
9537 + ((unsigned short)msgid & 0xfff)
9539 +#define VCHIQ_FOURCC_AS_4CHARS(fourcc) \
9540 + ((fourcc) >> 24) & 0xff, \
9541 + ((fourcc) >> 16) & 0xff, \
9542 + ((fourcc) >> 8) & 0xff, \
9545 +/* Ensure the fields are wide enough */
9546 +vchiq_static_assert(VCHIQ_MSG_SRCPORT(VCHIQ_MAKE_MSG(0, 0, VCHIQ_PORT_MAX))
9548 +vchiq_static_assert(VCHIQ_MSG_TYPE(VCHIQ_MAKE_MSG(0, VCHIQ_PORT_MAX, 0)) == 0);
9549 +vchiq_static_assert((unsigned int)VCHIQ_PORT_MAX <
9550 + (unsigned int)VCHIQ_PORT_FREE);
9552 +#define VCHIQ_MSGID_PADDING VCHIQ_MAKE_MSG(VCHIQ_MSG_PADDING, 0, 0)
9553 +#define VCHIQ_MSGID_CLAIMED 0x40000000
9555 +#define VCHIQ_FOURCC_INVALID 0x00000000
9556 +#define VCHIQ_FOURCC_IS_LEGAL(fourcc) (fourcc != VCHIQ_FOURCC_INVALID)
9558 +#define VCHIQ_BULK_ACTUAL_ABORTED -1
9560 +typedef uint32_t BITSET_T;
9562 +vchiq_static_assert((sizeof(BITSET_T) * 8) == 32);
9564 +#define BITSET_SIZE(b) ((b + 31) >> 5)
9565 +#define BITSET_WORD(b) (b >> 5)
9566 +#define BITSET_BIT(b) (1 << (b & 31))
9567 +#define BITSET_ZERO(bs) memset(bs, 0, sizeof(bs))
9568 +#define BITSET_IS_SET(bs, b) (bs[BITSET_WORD(b)] & BITSET_BIT(b))
9569 +#define BITSET_SET(bs, b) (bs[BITSET_WORD(b)] |= BITSET_BIT(b))
9570 +#define BITSET_CLR(bs, b) (bs[BITSET_WORD(b)] &= ~BITSET_BIT(b))
9572 +#if VCHIQ_ENABLE_STATS
9573 +#define VCHIQ_STATS_INC(state, stat) (state->stats. stat++)
9574 +#define VCHIQ_SERVICE_STATS_INC(service, stat) (service->stats. stat++)
9575 +#define VCHIQ_SERVICE_STATS_ADD(service, stat, addend) \
9576 + (service->stats. stat += addend)
9578 +#define VCHIQ_STATS_INC(state, stat) ((void)0)
9579 +#define VCHIQ_SERVICE_STATS_INC(service, stat) ((void)0)
9580 +#define VCHIQ_SERVICE_STATS_ADD(service, stat, addend) ((void)0)
9585 +#if VCHIQ_ENABLE_DEBUG
9586 + DEBUG_SLOT_HANDLER_COUNT,
9587 + DEBUG_SLOT_HANDLER_LINE,
9589 + DEBUG_PARSE_HEADER,
9590 + DEBUG_PARSE_MSGID,
9591 + DEBUG_AWAIT_COMPLETION_LINE,
9592 + DEBUG_DEQUEUE_MESSAGE_LINE,
9593 + DEBUG_SERVICE_CALLBACK_LINE,
9594 + DEBUG_MSG_QUEUE_FULL_COUNT,
9595 + DEBUG_COMPLETION_QUEUE_FULL_COUNT,
9600 +#if VCHIQ_ENABLE_DEBUG
9602 +#define DEBUG_INITIALISE(local) int *debug_ptr = (local)->debug;
9603 +#define DEBUG_TRACE(d) \
9604 + do { debug_ptr[DEBUG_ ## d] = __LINE__; dsb(); } while (0)
9605 +#define DEBUG_VALUE(d, v) \
9606 + do { debug_ptr[DEBUG_ ## d] = (v); dsb(); } while (0)
9607 +#define DEBUG_COUNT(d) \
9608 + do { debug_ptr[DEBUG_ ## d]++; dsb(); } while (0)
9610 +#else /* VCHIQ_ENABLE_DEBUG */
9612 +#define DEBUG_INITIALISE(local)
9613 +#define DEBUG_TRACE(d)
9614 +#define DEBUG_VALUE(d, v)
9615 +#define DEBUG_COUNT(d)
9617 +#endif /* VCHIQ_ENABLE_DEBUG */
9620 + VCHIQ_CONNSTATE_DISCONNECTED,
9621 + VCHIQ_CONNSTATE_CONNECTING,
9622 + VCHIQ_CONNSTATE_CONNECTED,
9623 + VCHIQ_CONNSTATE_PAUSING,
9624 + VCHIQ_CONNSTATE_PAUSE_SENT,
9625 + VCHIQ_CONNSTATE_PAUSED,
9626 + VCHIQ_CONNSTATE_RESUMING,
9627 + VCHIQ_CONNSTATE_PAUSE_TIMEOUT,
9628 + VCHIQ_CONNSTATE_RESUME_TIMEOUT
9629 +} VCHIQ_CONNSTATE_T;
9632 + VCHIQ_SRVSTATE_FREE,
9633 + VCHIQ_SRVSTATE_HIDDEN,
9634 + VCHIQ_SRVSTATE_LISTENING,
9635 + VCHIQ_SRVSTATE_OPENING,
9636 + VCHIQ_SRVSTATE_OPEN,
9637 + VCHIQ_SRVSTATE_OPENSYNC,
9638 + VCHIQ_SRVSTATE_CLOSESENT,
9639 + VCHIQ_SRVSTATE_CLOSERECVD,
9640 + VCHIQ_SRVSTATE_CLOSEWAIT,
9641 + VCHIQ_SRVSTATE_CLOSED
9645 + VCHIQ_POLL_TERMINATE,
9646 + VCHIQ_POLL_REMOVE,
9647 + VCHIQ_POLL_TXNOTIFY,
9648 + VCHIQ_POLL_RXNOTIFY,
9653 + VCHIQ_BULK_TRANSMIT,
9654 + VCHIQ_BULK_RECEIVE
9655 +} VCHIQ_BULK_DIR_T;
9657 +typedef void (*VCHIQ_USERDATA_TERM_T)(void *userdata);
9659 +typedef struct vchiq_bulk_struct {
9663 + VCHI_MEM_HANDLE_T handle;
9666 + void *remote_data;
9671 +typedef struct vchiq_bulk_queue_struct {
9672 + int local_insert; /* Where to insert the next local bulk */
9673 + int remote_insert; /* Where to insert the next remote bulk (master) */
9674 + int process; /* Bulk to transfer next */
9675 + int remote_notify; /* Bulk to notify the remote client of next (mstr) */
9676 + int remove; /* Bulk to notify the local client of, and remove,
9678 + VCHIQ_BULK_T bulks[VCHIQ_NUM_SERVICE_BULKS];
9679 +} VCHIQ_BULK_QUEUE_T;
9681 +typedef struct remote_event_struct {
9684 + struct semaphore *event;
9687 +typedef struct opaque_platform_state_t *VCHIQ_PLATFORM_STATE_T;
9689 +typedef struct vchiq_state_struct VCHIQ_STATE_T;
9691 +typedef struct vchiq_slot_struct {
9692 + char data[VCHIQ_SLOT_SIZE];
9695 +typedef struct vchiq_slot_info_struct {
9696 + /* Use two counters rather than one to avoid the need for a mutex. */
9698 + short release_count;
9699 +} VCHIQ_SLOT_INFO_T;
9701 +typedef struct vchiq_service_struct {
9702 + VCHIQ_SERVICE_BASE_T base;
9703 + VCHIQ_SERVICE_HANDLE_T handle;
9704 + unsigned int ref_count;
9706 + VCHIQ_USERDATA_TERM_T userdata_term;
9707 + unsigned int localport;
9708 + unsigned int remoteport;
9709 + int public_fourcc;
9714 + atomic_t poll_flags;
9716 + short version_min;
9717 + short peer_version;
9719 + VCHIQ_STATE_T *state;
9720 + VCHIQ_INSTANCE_T instance;
9722 + int service_use_count;
9724 + VCHIQ_BULK_QUEUE_T bulk_tx;
9725 + VCHIQ_BULK_QUEUE_T bulk_rx;
9727 + struct semaphore remove_event;
9728 + struct semaphore bulk_remove_event;
9729 + struct mutex bulk_mutex;
9731 + struct service_stats_struct {
9736 + int ctrl_tx_count;
9737 + int ctrl_rx_count;
9738 + int bulk_tx_count;
9739 + int bulk_rx_count;
9740 + int bulk_aborted_count;
9741 + uint64_t ctrl_tx_bytes;
9742 + uint64_t ctrl_rx_bytes;
9743 + uint64_t bulk_tx_bytes;
9744 + uint64_t bulk_rx_bytes;
9748 +/* The quota information is outside VCHIQ_SERVICE_T so that it can be
9749 + statically allocated, since for accounting reasons a service's slot
9750 + usage is carried over between users of the same port number.
9752 +typedef struct vchiq_service_quota_struct {
9753 + unsigned short slot_quota;
9754 + unsigned short slot_use_count;
9755 + unsigned short message_quota;
9756 + unsigned short message_use_count;
9757 + struct semaphore quota_event;
9758 + int previous_tx_index;
9759 +} VCHIQ_SERVICE_QUOTA_T;
9761 +typedef struct vchiq_shared_state_struct {
9763 + /* A non-zero value here indicates that the content is valid. */
9766 + /* The first and last (inclusive) slots allocated to the owner. */
9770 + /* The slot allocated to synchronous messages from the owner. */
9773 + /* Signalling this event indicates that owner's slot handler thread
9775 + REMOTE_EVENT_T trigger;
9777 + /* Indicates the byte position within the stream where the next message
9778 + ** will be written. The least significant bits are an index into the
9779 + ** slot. The next bits are the index of the slot in slot_queue. */
9782 + /* This event should be signalled when a slot is recycled. */
9783 + REMOTE_EVENT_T recycle;
9785 + /* The slot_queue index where the next recycled slot will be written. */
9786 + int slot_queue_recycle;
9788 + /* This event should be signalled when a synchronous message is sent. */
9789 + REMOTE_EVENT_T sync_trigger;
9791 + /* This event should be signalled when a synchronous message has been
9793 + REMOTE_EVENT_T sync_release;
9795 + /* A circular buffer of slot indexes. */
9796 + int slot_queue[VCHIQ_MAX_SLOTS_PER_SIDE];
9798 + /* Debugging state */
9799 + int debug[DEBUG_MAX];
9800 +} VCHIQ_SHARED_STATE_T;
9802 +typedef struct vchiq_slot_zero_struct {
9805 + short version_min;
9806 + int slot_zero_size;
9809 + int max_slots_per_side;
9810 + int platform_data[2];
9811 + VCHIQ_SHARED_STATE_T master;
9812 + VCHIQ_SHARED_STATE_T slave;
9813 + VCHIQ_SLOT_INFO_T slots[VCHIQ_MAX_SLOTS];
9814 +} VCHIQ_SLOT_ZERO_T;
9816 +struct vchiq_state_struct {
9819 + VCHIQ_CONNSTATE_T conn_state;
9822 + VCHIQ_SHARED_STATE_T *local;
9823 + VCHIQ_SHARED_STATE_T *remote;
9824 + VCHIQ_SLOT_T *slot_data;
9826 + unsigned short default_slot_quota;
9827 + unsigned short default_message_quota;
9829 + /* Event indicating connect message received */
9830 + struct semaphore connect;
9832 + /* Mutex protecting services */
9833 + struct mutex mutex;
9834 + VCHIQ_INSTANCE_T *instance;
9836 + /* Processes incoming messages */
9837 + struct task_struct *slot_handler_thread;
9839 + /* Processes recycled slots */
9840 + struct task_struct *recycle_thread;
9842 + /* Processes synchronous messages */
9843 + struct task_struct *sync_thread;
9845 + /* Local implementation of the trigger remote event */
9846 + struct semaphore trigger_event;
9848 + /* Local implementation of the recycle remote event */
9849 + struct semaphore recycle_event;
9851 + /* Local implementation of the sync trigger remote event */
9852 + struct semaphore sync_trigger_event;
9854 + /* Local implementation of the sync release remote event */
9855 + struct semaphore sync_release_event;
9859 + VCHIQ_SLOT_INFO_T *rx_info;
9861 + struct mutex slot_mutex;
9863 + struct mutex recycle_mutex;
9865 + struct mutex sync_mutex;
9867 + struct mutex bulk_transfer_mutex;
9869 + /* Indicates the byte position within the stream from where the next
9870 + ** message will be read. The least significant bits are an index into
9871 + ** the slot.The next bits are the index of the slot in
9872 + ** remote->slot_queue. */
9875 + /* A cached copy of local->tx_pos. Only write to local->tx_pos, and read
9876 + from remote->tx_pos. */
9879 + /* The slot_queue index of the slot to become available next. */
9880 + int slot_queue_available;
9882 + /* A flag to indicate if any poll has been requested */
9885 + /* Ths index of the previous slot used for data messages. */
9886 + int previous_data_index;
9888 + /* The number of slots occupied by data messages. */
9889 + unsigned short data_use_count;
9891 + /* The maximum number of slots to be occupied by data messages. */
9892 + unsigned short data_quota;
9894 + /* An array of bit sets indicating which services must be polled. */
9895 + atomic_t poll_services[BITSET_SIZE(VCHIQ_MAX_SERVICES)];
9897 + /* The number of the first unused service */
9898 + int unused_service;
9900 + /* Signalled when a free slot becomes available. */
9901 + struct semaphore slot_available_event;
9903 + struct semaphore slot_remove_event;
9905 + /* Signalled when a free data slot becomes available. */
9906 + struct semaphore data_quota_event;
9908 + /* Incremented when there are bulk transfers which cannot be processed
9909 + * whilst paused and must be processed on resume */
9910 + int deferred_bulks;
9912 + struct state_stats_struct {
9915 + int ctrl_tx_count;
9916 + int ctrl_rx_count;
9920 + VCHIQ_SERVICE_T * services[VCHIQ_MAX_SERVICES];
9921 + VCHIQ_SERVICE_QUOTA_T service_quotas[VCHIQ_MAX_SERVICES];
9922 + VCHIQ_SLOT_INFO_T slot_info[VCHIQ_MAX_SLOTS];
9924 + VCHIQ_PLATFORM_STATE_T platform_state;
9927 +struct bulk_waiter {
9928 + VCHIQ_BULK_T *bulk;
9929 + struct semaphore event;
9933 +extern spinlock_t bulk_waiter_spinlock;
9935 +extern int vchiq_core_log_level;
9936 +extern int vchiq_core_msg_log_level;
9937 +extern int vchiq_sync_log_level;
9939 +extern VCHIQ_STATE_T *vchiq_states[VCHIQ_MAX_STATES];
9941 +extern const char *
9942 +get_conn_state_name(VCHIQ_CONNSTATE_T conn_state);
9944 +extern VCHIQ_SLOT_ZERO_T *
9945 +vchiq_init_slots(void *mem_base, int mem_size);
9947 +extern VCHIQ_STATUS_T
9948 +vchiq_init_state(VCHIQ_STATE_T *state, VCHIQ_SLOT_ZERO_T *slot_zero,
9951 +extern VCHIQ_STATUS_T
9952 +vchiq_connect_internal(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance);
9954 +extern VCHIQ_SERVICE_T *
9955 +vchiq_add_service_internal(VCHIQ_STATE_T *state,
9956 + const VCHIQ_SERVICE_PARAMS_T *params, int srvstate,
9957 + VCHIQ_INSTANCE_T instance, VCHIQ_USERDATA_TERM_T userdata_term);
9959 +extern VCHIQ_STATUS_T
9960 +vchiq_open_service_internal(VCHIQ_SERVICE_T *service, int client_id);
9962 +extern VCHIQ_STATUS_T
9963 +vchiq_close_service_internal(VCHIQ_SERVICE_T *service, int close_recvd);
9966 +vchiq_terminate_service_internal(VCHIQ_SERVICE_T *service);
9969 +vchiq_free_service_internal(VCHIQ_SERVICE_T *service);
9971 +extern VCHIQ_STATUS_T
9972 +vchiq_shutdown_internal(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance);
9974 +extern VCHIQ_STATUS_T
9975 +vchiq_pause_internal(VCHIQ_STATE_T *state);
9977 +extern VCHIQ_STATUS_T
9978 +vchiq_resume_internal(VCHIQ_STATE_T *state);
9981 +remote_event_pollall(VCHIQ_STATE_T *state);
9983 +extern VCHIQ_STATUS_T
9984 +vchiq_bulk_transfer(VCHIQ_SERVICE_HANDLE_T handle,
9985 + VCHI_MEM_HANDLE_T memhandle, void *offset, int size, void *userdata,
9986 + VCHIQ_BULK_MODE_T mode, VCHIQ_BULK_DIR_T dir);
9989 +vchiq_dump_state(void *dump_context, VCHIQ_STATE_T *state);
9992 +vchiq_dump_service_state(void *dump_context, VCHIQ_SERVICE_T *service);
9995 +vchiq_loud_error_header(void);
9998 +vchiq_loud_error_footer(void);
10001 +request_poll(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service, int poll_type);
10003 +static inline VCHIQ_SERVICE_T *
10004 +handle_to_service(VCHIQ_SERVICE_HANDLE_T handle)
10006 + VCHIQ_STATE_T *state = vchiq_states[(handle / VCHIQ_MAX_SERVICES) &
10007 + (VCHIQ_MAX_STATES - 1)];
10011 + return state->services[handle & (VCHIQ_MAX_SERVICES - 1)];
10014 +extern VCHIQ_SERVICE_T *
10015 +find_service_by_handle(VCHIQ_SERVICE_HANDLE_T handle);
10017 +extern VCHIQ_SERVICE_T *
10018 +find_service_by_port(VCHIQ_STATE_T *state, int localport);
10020 +extern VCHIQ_SERVICE_T *
10021 +find_service_for_instance(VCHIQ_INSTANCE_T instance,
10022 + VCHIQ_SERVICE_HANDLE_T handle);
10024 +extern VCHIQ_SERVICE_T *
10025 +next_service_by_instance(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance,
10029 +lock_service(VCHIQ_SERVICE_T *service);
10032 +unlock_service(VCHIQ_SERVICE_T *service);
10034 +/* The following functions are called from vchiq_core, and external
10035 +** implementations must be provided. */
10037 +extern VCHIQ_STATUS_T
10038 +vchiq_prepare_bulk_data(VCHIQ_BULK_T *bulk,
10039 + VCHI_MEM_HANDLE_T memhandle, void *offset, int size, int dir);
10042 +vchiq_transfer_bulk(VCHIQ_BULK_T *bulk);
10045 +vchiq_complete_bulk(VCHIQ_BULK_T *bulk);
10047 +extern VCHIQ_STATUS_T
10048 +vchiq_copy_from_user(void *dst, const void *src, int size);
10051 +remote_event_signal(REMOTE_EVENT_T *event);
10054 +vchiq_platform_check_suspend(VCHIQ_STATE_T *state);
10057 +vchiq_platform_paused(VCHIQ_STATE_T *state);
10059 +extern VCHIQ_STATUS_T
10060 +vchiq_platform_resume(VCHIQ_STATE_T *state);
10063 +vchiq_platform_resumed(VCHIQ_STATE_T *state);
10066 +vchiq_dump(void *dump_context, const char *str, int len);
10069 +vchiq_dump_platform_state(void *dump_context);
10072 +vchiq_dump_platform_instances(void *dump_context);
10075 +vchiq_dump_platform_service_state(void *dump_context,
10076 + VCHIQ_SERVICE_T *service);
10078 +extern VCHIQ_STATUS_T
10079 +vchiq_use_service_internal(VCHIQ_SERVICE_T *service);
10081 +extern VCHIQ_STATUS_T
10082 +vchiq_release_service_internal(VCHIQ_SERVICE_T *service);
10085 +vchiq_on_remote_use(VCHIQ_STATE_T *state);
10088 +vchiq_on_remote_release(VCHIQ_STATE_T *state);
10090 +extern VCHIQ_STATUS_T
10091 +vchiq_platform_init_state(VCHIQ_STATE_T *state);
10093 +extern VCHIQ_STATUS_T
10094 +vchiq_check_service(VCHIQ_SERVICE_T *service);
10097 +vchiq_on_remote_use_active(VCHIQ_STATE_T *state);
10099 +extern VCHIQ_STATUS_T
10100 +vchiq_send_remote_use(VCHIQ_STATE_T *state);
10102 +extern VCHIQ_STATUS_T
10103 +vchiq_send_remote_release(VCHIQ_STATE_T *state);
10105 +extern VCHIQ_STATUS_T
10106 +vchiq_send_remote_use_active(VCHIQ_STATE_T *state);
10109 +vchiq_platform_conn_state_changed(VCHIQ_STATE_T *state,
10110 + VCHIQ_CONNSTATE_T oldstate, VCHIQ_CONNSTATE_T newstate);
10113 +vchiq_platform_handle_timeout(VCHIQ_STATE_T *state);
10116 +vchiq_set_conn_state(VCHIQ_STATE_T *state, VCHIQ_CONNSTATE_T newstate);
10120 +vchiq_log_dump_mem(const char *label, uint32_t addr, const void *voidMem,
10121 + size_t numBytes);
10124 diff --git a/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_genversion b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_genversion
10125 new file mode 100644
10126 index 0000000..9f5b634
10128 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_genversion
10130 +#!/usr/bin/perl -w
10135 +# Generate a version from available information
10138 +my $prefix = shift @ARGV;
10139 +my $root = shift @ARGV;
10142 +if ( not defined $root ) {
10143 + die "usage: $0 prefix root-dir\n";
10146 +if ( ! -d $root ) {
10147 + die "root directory $root not found\n";
10150 +my $version = "unknown";
10153 +if ( -d "$root/.git" ) {
10154 + # attempt to work out git version. only do so
10155 + # on a linux build host, as cygwin builds are
10156 + # already slow enough
10158 + if ( -f "/usr/bin/git" || -f "/usr/local/bin/git" ) {
10159 + if (not open(F, "git --git-dir $root/.git rev-parse --verify HEAD|")) {
10160 + $version = "no git version";
10164 + $version =~ s/[ \r\n]*$//; # chomp may not be enough (cygwin).
10165 + $version =~ s/^[ \r\n]*//; # chomp may not be enough (cygwin).
10168 + if (open(G, "git --git-dir $root/.git status --porcelain|")) {
10170 + $tainted =~ s/[ \r\n]*$//; # chomp may not be enough (cygwin).
10171 + $tainted =~ s/^[ \r\n]*//; # chomp may not be enough (cygwin).
10172 + if (length $tainted) {
10173 + $version = join ' ', $version, "(tainted)";
10176 + $version = join ' ', $version, "(clean)";
10182 +my $hostname = `hostname`;
10183 +$hostname =~ s/[ \r\n]*$//; # chomp may not be enough (cygwin).
10184 +$hostname =~ s/^[ \r\n]*//; # chomp may not be enough (cygwin).
10187 +print STDERR "Version $version\n";
10189 +#include "${prefix}_build_info.h"
10190 +#include <linux/broadcom/vc_debug_sym.h>
10192 +VC_DEBUG_DECLARE_STRING_VAR( ${prefix}_build_hostname, "$hostname" );
10193 +VC_DEBUG_DECLARE_STRING_VAR( ${prefix}_build_version, "$version" );
10194 +VC_DEBUG_DECLARE_STRING_VAR( ${prefix}_build_time, __TIME__ );
10195 +VC_DEBUG_DECLARE_STRING_VAR( ${prefix}_build_date, __DATE__ );
10197 +const char *vchiq_get_build_hostname( void )
10199 + return vchiq_build_hostname;
10202 +const char *vchiq_get_build_version( void )
10204 + return vchiq_build_version;
10207 +const char *vchiq_get_build_date( void )
10209 + return vchiq_build_date;
10212 +const char *vchiq_get_build_time( void )
10214 + return vchiq_build_time;
10217 diff --git a/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_if.h b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_if.h
10218 new file mode 100644
10219 index 0000000..50359b0
10221 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_if.h
10224 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
10226 + * Redistribution and use in source and binary forms, with or without
10227 + * modification, are permitted provided that the following conditions
10229 + * 1. Redistributions of source code must retain the above copyright
10230 + * notice, this list of conditions, and the following disclaimer,
10231 + * without modification.
10232 + * 2. Redistributions in binary form must reproduce the above copyright
10233 + * notice, this list of conditions and the following disclaimer in the
10234 + * documentation and/or other materials provided with the distribution.
10235 + * 3. The names of the above-listed copyright holders may not be used
10236 + * to endorse or promote products derived from this software without
10237 + * specific prior written permission.
10239 + * ALTERNATIVELY, this software may be distributed under the terms of the
10240 + * GNU General Public License ("GPL") version 2, as published by the Free
10241 + * Software Foundation.
10243 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
10244 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
10245 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
10246 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
10247 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
10248 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
10249 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
10250 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
10251 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
10252 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
10253 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
10256 +#ifndef VCHIQ_IF_H
10257 +#define VCHIQ_IF_H
10259 +#include "interface/vchi/vchi_mh.h"
10261 +#define VCHIQ_SERVICE_HANDLE_INVALID 0
10263 +#define VCHIQ_SLOT_SIZE 4096
10264 +#define VCHIQ_MAX_MSG_SIZE (VCHIQ_SLOT_SIZE - sizeof(VCHIQ_HEADER_T))
10265 +#define VCHIQ_CHANNEL_SIZE VCHIQ_MAX_MSG_SIZE /* For backwards compatibility */
10267 +#define VCHIQ_MAKE_FOURCC(x0, x1, x2, x3) \
10268 + (((x0) << 24) | ((x1) << 16) | ((x2) << 8) | (x3))
10269 +#define VCHIQ_GET_SERVICE_USERDATA(service) vchiq_get_service_userdata(service)
10270 +#define VCHIQ_GET_SERVICE_FOURCC(service) vchiq_get_service_fourcc(service)
10273 + VCHIQ_SERVICE_OPENED, /* service, -, - */
10274 + VCHIQ_SERVICE_CLOSED, /* service, -, - */
10275 + VCHIQ_MESSAGE_AVAILABLE, /* service, header, - */
10276 + VCHIQ_BULK_TRANSMIT_DONE, /* service, -, bulk_userdata */
10277 + VCHIQ_BULK_RECEIVE_DONE, /* service, -, bulk_userdata */
10278 + VCHIQ_BULK_TRANSMIT_ABORTED, /* service, -, bulk_userdata */
10279 + VCHIQ_BULK_RECEIVE_ABORTED /* service, -, bulk_userdata */
10283 + VCHIQ_ERROR = -1,
10284 + VCHIQ_SUCCESS = 0,
10289 + VCHIQ_BULK_MODE_CALLBACK,
10290 + VCHIQ_BULK_MODE_BLOCKING,
10291 + VCHIQ_BULK_MODE_NOCALLBACK,
10292 + VCHIQ_BULK_MODE_WAITING /* Reserved for internal use */
10293 +} VCHIQ_BULK_MODE_T;
10296 + VCHIQ_SERVICE_OPTION_AUTOCLOSE,
10297 + VCHIQ_SERVICE_OPTION_SLOT_QUOTA,
10298 + VCHIQ_SERVICE_OPTION_MESSAGE_QUOTA,
10299 + VCHIQ_SERVICE_OPTION_SYNCHRONOUS
10300 +} VCHIQ_SERVICE_OPTION_T;
10302 +typedef struct vchiq_header_struct {
10303 + /* The message identifier - opaque to applications. */
10306 + /* Size of message data. */
10307 + unsigned int size;
10309 + char data[0]; /* message */
10313 + const void *data;
10314 + unsigned int size;
10315 +} VCHIQ_ELEMENT_T;
10317 +typedef unsigned int VCHIQ_SERVICE_HANDLE_T;
10319 +typedef VCHIQ_STATUS_T (*VCHIQ_CALLBACK_T)(VCHIQ_REASON_T, VCHIQ_HEADER_T *,
10320 + VCHIQ_SERVICE_HANDLE_T, void *);
10322 +typedef struct vchiq_service_base_struct {
10324 + VCHIQ_CALLBACK_T callback;
10326 +} VCHIQ_SERVICE_BASE_T;
10328 +typedef struct vchiq_service_params_struct {
10330 + VCHIQ_CALLBACK_T callback;
10332 + short version; /* Increment for non-trivial changes */
10333 + short version_min; /* Update for incompatible changes */
10334 +} VCHIQ_SERVICE_PARAMS_T;
10336 +typedef struct vchiq_config_struct {
10337 + unsigned int max_msg_size;
10338 + unsigned int bulk_threshold; /* The message size above which it
10339 + is better to use a bulk transfer
10340 + (<= max_msg_size) */
10341 + unsigned int max_outstanding_bulks;
10342 + unsigned int max_services;
10343 + short version; /* The version of VCHIQ */
10344 + short version_min; /* The minimum compatible version of VCHIQ */
10347 +typedef struct vchiq_instance_struct *VCHIQ_INSTANCE_T;
10348 +typedef void (*VCHIQ_REMOTE_USE_CALLBACK_T)(void *cb_arg);
10350 +extern VCHIQ_STATUS_T vchiq_initialise(VCHIQ_INSTANCE_T *pinstance);
10351 +extern VCHIQ_STATUS_T vchiq_shutdown(VCHIQ_INSTANCE_T instance);
10352 +extern VCHIQ_STATUS_T vchiq_connect(VCHIQ_INSTANCE_T instance);
10353 +extern VCHIQ_STATUS_T vchiq_add_service(VCHIQ_INSTANCE_T instance,
10354 + const VCHIQ_SERVICE_PARAMS_T *params,
10355 + VCHIQ_SERVICE_HANDLE_T *pservice);
10356 +extern VCHIQ_STATUS_T vchiq_open_service(VCHIQ_INSTANCE_T instance,
10357 + const VCHIQ_SERVICE_PARAMS_T *params,
10358 + VCHIQ_SERVICE_HANDLE_T *pservice);
10359 +extern VCHIQ_STATUS_T vchiq_close_service(VCHIQ_SERVICE_HANDLE_T service);
10360 +extern VCHIQ_STATUS_T vchiq_remove_service(VCHIQ_SERVICE_HANDLE_T service);
10361 +extern VCHIQ_STATUS_T vchiq_use_service(VCHIQ_SERVICE_HANDLE_T service);
10362 +extern VCHIQ_STATUS_T vchiq_use_service_no_resume(
10363 + VCHIQ_SERVICE_HANDLE_T service);
10364 +extern VCHIQ_STATUS_T vchiq_release_service(VCHIQ_SERVICE_HANDLE_T service);
10366 +extern VCHIQ_STATUS_T vchiq_queue_message(VCHIQ_SERVICE_HANDLE_T service,
10367 + const VCHIQ_ELEMENT_T *elements, unsigned int count);
10368 +extern void vchiq_release_message(VCHIQ_SERVICE_HANDLE_T service,
10369 + VCHIQ_HEADER_T *header);
10370 +extern VCHIQ_STATUS_T vchiq_queue_bulk_transmit(VCHIQ_SERVICE_HANDLE_T service,
10371 + const void *data, unsigned int size, void *userdata);
10372 +extern VCHIQ_STATUS_T vchiq_queue_bulk_receive(VCHIQ_SERVICE_HANDLE_T service,
10373 + void *data, unsigned int size, void *userdata);
10374 +extern VCHIQ_STATUS_T vchiq_queue_bulk_transmit_handle(
10375 + VCHIQ_SERVICE_HANDLE_T service, VCHI_MEM_HANDLE_T handle,
10376 + const void *offset, unsigned int size, void *userdata);
10377 +extern VCHIQ_STATUS_T vchiq_queue_bulk_receive_handle(
10378 + VCHIQ_SERVICE_HANDLE_T service, VCHI_MEM_HANDLE_T handle,
10379 + void *offset, unsigned int size, void *userdata);
10380 +extern VCHIQ_STATUS_T vchiq_bulk_transmit(VCHIQ_SERVICE_HANDLE_T service,
10381 + const void *data, unsigned int size, void *userdata,
10382 + VCHIQ_BULK_MODE_T mode);
10383 +extern VCHIQ_STATUS_T vchiq_bulk_receive(VCHIQ_SERVICE_HANDLE_T service,
10384 + void *data, unsigned int size, void *userdata,
10385 + VCHIQ_BULK_MODE_T mode);
10386 +extern VCHIQ_STATUS_T vchiq_bulk_transmit_handle(VCHIQ_SERVICE_HANDLE_T service,
10387 + VCHI_MEM_HANDLE_T handle, const void *offset, unsigned int size,
10388 + void *userdata, VCHIQ_BULK_MODE_T mode);
10389 +extern VCHIQ_STATUS_T vchiq_bulk_receive_handle(VCHIQ_SERVICE_HANDLE_T service,
10390 + VCHI_MEM_HANDLE_T handle, void *offset, unsigned int size,
10391 + void *userdata, VCHIQ_BULK_MODE_T mode);
10392 +extern int vchiq_get_client_id(VCHIQ_SERVICE_HANDLE_T service);
10393 +extern void *vchiq_get_service_userdata(VCHIQ_SERVICE_HANDLE_T service);
10394 +extern int vchiq_get_service_fourcc(VCHIQ_SERVICE_HANDLE_T service);
10395 +extern VCHIQ_STATUS_T vchiq_get_config(VCHIQ_INSTANCE_T instance,
10396 + int config_size, VCHIQ_CONFIG_T *pconfig);
10397 +extern VCHIQ_STATUS_T vchiq_set_service_option(VCHIQ_SERVICE_HANDLE_T service,
10398 + VCHIQ_SERVICE_OPTION_T option, int value);
10400 +extern VCHIQ_STATUS_T vchiq_remote_use(VCHIQ_INSTANCE_T instance,
10401 + VCHIQ_REMOTE_USE_CALLBACK_T callback, void *cb_arg);
10402 +extern VCHIQ_STATUS_T vchiq_remote_release(VCHIQ_INSTANCE_T instance);
10404 +extern VCHIQ_STATUS_T vchiq_dump_phys_mem(VCHIQ_SERVICE_HANDLE_T service,
10405 + void *ptr, size_t num_bytes);
10407 +extern VCHIQ_STATUS_T vchiq_get_peer_version(VCHIQ_SERVICE_HANDLE_T handle,
10408 + short *peer_version);
10410 +#endif /* VCHIQ_IF_H */
10411 diff --git a/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_ioctl.h b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_ioctl.h
10412 new file mode 100644
10413 index 0000000..e248037
10415 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_ioctl.h
10418 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
10420 + * Redistribution and use in source and binary forms, with or without
10421 + * modification, are permitted provided that the following conditions
10423 + * 1. Redistributions of source code must retain the above copyright
10424 + * notice, this list of conditions, and the following disclaimer,
10425 + * without modification.
10426 + * 2. Redistributions in binary form must reproduce the above copyright
10427 + * notice, this list of conditions and the following disclaimer in the
10428 + * documentation and/or other materials provided with the distribution.
10429 + * 3. The names of the above-listed copyright holders may not be used
10430 + * to endorse or promote products derived from this software without
10431 + * specific prior written permission.
10433 + * ALTERNATIVELY, this software may be distributed under the terms of the
10434 + * GNU General Public License ("GPL") version 2, as published by the Free
10435 + * Software Foundation.
10437 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
10438 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
10439 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
10440 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
10441 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
10442 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
10443 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
10444 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
10445 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
10446 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
10447 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
10450 +#ifndef VCHIQ_IOCTLS_H
10451 +#define VCHIQ_IOCTLS_H
10453 +#include <linux/ioctl.h>
10454 +#include "vchiq_if.h"
10456 +#define VCHIQ_IOC_MAGIC 0xc4
10457 +#define VCHIQ_INVALID_HANDLE (~0)
10460 + VCHIQ_SERVICE_PARAMS_T params;
10463 + unsigned int handle; /* OUT */
10464 +} VCHIQ_CREATE_SERVICE_T;
10467 + unsigned int handle;
10468 + unsigned int count;
10469 + const VCHIQ_ELEMENT_T *elements;
10470 +} VCHIQ_QUEUE_MESSAGE_T;
10473 + unsigned int handle;
10475 + unsigned int size;
10477 + VCHIQ_BULK_MODE_T mode;
10478 +} VCHIQ_QUEUE_BULK_TRANSFER_T;
10481 + VCHIQ_REASON_T reason;
10482 + VCHIQ_HEADER_T *header;
10483 + void *service_userdata;
10484 + void *bulk_userdata;
10485 +} VCHIQ_COMPLETION_DATA_T;
10488 + unsigned int count;
10489 + VCHIQ_COMPLETION_DATA_T *buf;
10490 + unsigned int msgbufsize;
10491 + unsigned int msgbufcount; /* IN/OUT */
10493 +} VCHIQ_AWAIT_COMPLETION_T;
10496 + unsigned int handle;
10498 + unsigned int bufsize;
10500 +} VCHIQ_DEQUEUE_MESSAGE_T;
10503 + unsigned int config_size;
10504 + VCHIQ_CONFIG_T *pconfig;
10505 +} VCHIQ_GET_CONFIG_T;
10508 + unsigned int handle;
10509 + VCHIQ_SERVICE_OPTION_T option;
10511 +} VCHIQ_SET_SERVICE_OPTION_T;
10515 + size_t num_bytes;
10516 +} VCHIQ_DUMP_MEM_T;
10518 +#define VCHIQ_IOC_CONNECT _IO(VCHIQ_IOC_MAGIC, 0)
10519 +#define VCHIQ_IOC_SHUTDOWN _IO(VCHIQ_IOC_MAGIC, 1)
10520 +#define VCHIQ_IOC_CREATE_SERVICE \
10521 + _IOWR(VCHIQ_IOC_MAGIC, 2, VCHIQ_CREATE_SERVICE_T)
10522 +#define VCHIQ_IOC_REMOVE_SERVICE _IO(VCHIQ_IOC_MAGIC, 3)
10523 +#define VCHIQ_IOC_QUEUE_MESSAGE \
10524 + _IOW(VCHIQ_IOC_MAGIC, 4, VCHIQ_QUEUE_MESSAGE_T)
10525 +#define VCHIQ_IOC_QUEUE_BULK_TRANSMIT \
10526 + _IOWR(VCHIQ_IOC_MAGIC, 5, VCHIQ_QUEUE_BULK_TRANSFER_T)
10527 +#define VCHIQ_IOC_QUEUE_BULK_RECEIVE \
10528 + _IOWR(VCHIQ_IOC_MAGIC, 6, VCHIQ_QUEUE_BULK_TRANSFER_T)
10529 +#define VCHIQ_IOC_AWAIT_COMPLETION \
10530 + _IOWR(VCHIQ_IOC_MAGIC, 7, VCHIQ_AWAIT_COMPLETION_T)
10531 +#define VCHIQ_IOC_DEQUEUE_MESSAGE \
10532 + _IOWR(VCHIQ_IOC_MAGIC, 8, VCHIQ_DEQUEUE_MESSAGE_T)
10533 +#define VCHIQ_IOC_GET_CLIENT_ID _IO(VCHIQ_IOC_MAGIC, 9)
10534 +#define VCHIQ_IOC_GET_CONFIG \
10535 + _IOWR(VCHIQ_IOC_MAGIC, 10, VCHIQ_GET_CONFIG_T)
10536 +#define VCHIQ_IOC_CLOSE_SERVICE _IO(VCHIQ_IOC_MAGIC, 11)
10537 +#define VCHIQ_IOC_USE_SERVICE _IO(VCHIQ_IOC_MAGIC, 12)
10538 +#define VCHIQ_IOC_RELEASE_SERVICE _IO(VCHIQ_IOC_MAGIC, 13)
10539 +#define VCHIQ_IOC_SET_SERVICE_OPTION \
10540 + _IOW(VCHIQ_IOC_MAGIC, 14, VCHIQ_SET_SERVICE_OPTION_T)
10541 +#define VCHIQ_IOC_DUMP_PHYS_MEM \
10542 + _IOW(VCHIQ_IOC_MAGIC, 15, VCHIQ_DUMP_MEM_T)
10543 +#define VCHIQ_IOC_MAX 15
10546 diff --git a/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_kern_lib.c b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_kern_lib.c
10547 new file mode 100644
10548 index 0000000..be9735f
10550 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_kern_lib.c
10553 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
10555 + * Redistribution and use in source and binary forms, with or without
10556 + * modification, are permitted provided that the following conditions
10558 + * 1. Redistributions of source code must retain the above copyright
10559 + * notice, this list of conditions, and the following disclaimer,
10560 + * without modification.
10561 + * 2. Redistributions in binary form must reproduce the above copyright
10562 + * notice, this list of conditions and the following disclaimer in the
10563 + * documentation and/or other materials provided with the distribution.
10564 + * 3. The names of the above-listed copyright holders may not be used
10565 + * to endorse or promote products derived from this software without
10566 + * specific prior written permission.
10568 + * ALTERNATIVELY, this software may be distributed under the terms of the
10569 + * GNU General Public License ("GPL") version 2, as published by the Free
10570 + * Software Foundation.
10572 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
10573 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
10574 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
10575 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
10576 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
10577 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
10578 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
10579 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
10580 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
10581 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
10582 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
10585 +/* ---- Include Files ---------------------------------------------------- */
10587 +#include <linux/kernel.h>
10588 +#include <linux/module.h>
10589 +#include <linux/mutex.h>
10591 +#include "vchiq_core.h"
10592 +#include "vchiq_arm.h"
10594 +/* ---- Public Variables ------------------------------------------------- */
10596 +/* ---- Private Constants and Types -------------------------------------- */
10598 +struct bulk_waiter_node {
10599 + struct bulk_waiter bulk_waiter;
10601 + struct list_head list;
10604 +struct vchiq_instance_struct {
10605 + VCHIQ_STATE_T *state;
10609 + struct list_head bulk_waiter_list;
10610 + struct mutex bulk_waiter_list_mutex;
10613 +static VCHIQ_STATUS_T
10614 +vchiq_blocking_bulk_transfer(VCHIQ_SERVICE_HANDLE_T handle, void *data,
10615 + unsigned int size, VCHIQ_BULK_DIR_T dir);
10617 +/****************************************************************************
10619 +* vchiq_initialise
10621 +***************************************************************************/
10622 +#define VCHIQ_INIT_RETRIES 10
10623 +VCHIQ_STATUS_T vchiq_initialise(VCHIQ_INSTANCE_T *instanceOut)
10625 + VCHIQ_STATUS_T status = VCHIQ_ERROR;
10626 + VCHIQ_STATE_T *state;
10627 + VCHIQ_INSTANCE_T instance = NULL;
10630 + vchiq_log_trace(vchiq_core_log_level, "%s called", __func__);
10632 + /* VideoCore may not be ready due to boot up timing.
10633 + It may never be ready if kernel and firmware are mismatched, so don't block forever. */
10634 + for (i=0; i<VCHIQ_INIT_RETRIES; i++) {
10635 + state = vchiq_get_state();
10640 + if (i==VCHIQ_INIT_RETRIES) {
10641 + vchiq_log_error(vchiq_core_log_level,
10642 + "%s: videocore not initialized\n", __func__);
10644 + } else if (i>0) {
10645 + vchiq_log_warning(vchiq_core_log_level,
10646 + "%s: videocore initialized after %d retries\n", __func__, i);
10649 + instance = kzalloc(sizeof(*instance), GFP_KERNEL);
10651 + vchiq_log_error(vchiq_core_log_level,
10652 + "%s: error allocating vchiq instance\n", __func__);
10656 + instance->connected = 0;
10657 + instance->state = state;
10658 + mutex_init(&instance->bulk_waiter_list_mutex);
10659 + INIT_LIST_HEAD(&instance->bulk_waiter_list);
10661 + *instanceOut = instance;
10663 + status = VCHIQ_SUCCESS;
10666 + vchiq_log_trace(vchiq_core_log_level,
10667 + "%s(%p): returning %d", __func__, instance, status);
10671 +EXPORT_SYMBOL(vchiq_initialise);
10673 +/****************************************************************************
10677 +***************************************************************************/
10679 +VCHIQ_STATUS_T vchiq_shutdown(VCHIQ_INSTANCE_T instance)
10681 + VCHIQ_STATUS_T status;
10682 + VCHIQ_STATE_T *state = instance->state;
10684 + vchiq_log_trace(vchiq_core_log_level,
10685 + "%s(%p) called", __func__, instance);
10687 + if (mutex_lock_interruptible(&state->mutex) != 0)
10688 + return VCHIQ_RETRY;
10690 + /* Remove all services */
10691 + status = vchiq_shutdown_internal(state, instance);
10693 + mutex_unlock(&state->mutex);
10695 + vchiq_log_trace(vchiq_core_log_level,
10696 + "%s(%p): returning %d", __func__, instance, status);
10698 + if (status == VCHIQ_SUCCESS) {
10699 + struct list_head *pos, *next;
10700 + list_for_each_safe(pos, next,
10701 + &instance->bulk_waiter_list) {
10702 + struct bulk_waiter_node *waiter;
10703 + waiter = list_entry(pos,
10704 + struct bulk_waiter_node,
10707 + vchiq_log_info(vchiq_arm_log_level,
10708 + "bulk_waiter - cleaned up %x "
10710 + (unsigned int)waiter, waiter->pid);
10718 +EXPORT_SYMBOL(vchiq_shutdown);
10720 +/****************************************************************************
10722 +* vchiq_is_connected
10724 +***************************************************************************/
10726 +int vchiq_is_connected(VCHIQ_INSTANCE_T instance)
10728 + return instance->connected;
10731 +/****************************************************************************
10735 +***************************************************************************/
10737 +VCHIQ_STATUS_T vchiq_connect(VCHIQ_INSTANCE_T instance)
10739 + VCHIQ_STATUS_T status;
10740 + VCHIQ_STATE_T *state = instance->state;
10742 + vchiq_log_trace(vchiq_core_log_level,
10743 + "%s(%p) called", __func__, instance);
10745 + if (mutex_lock_interruptible(&state->mutex) != 0) {
10746 + vchiq_log_trace(vchiq_core_log_level,
10747 + "%s: call to mutex_lock failed", __func__);
10748 + status = VCHIQ_RETRY;
10751 + status = vchiq_connect_internal(state, instance);
10753 + if (status == VCHIQ_SUCCESS)
10754 + instance->connected = 1;
10756 + mutex_unlock(&state->mutex);
10759 + vchiq_log_trace(vchiq_core_log_level,
10760 + "%s(%p): returning %d", __func__, instance, status);
10764 +EXPORT_SYMBOL(vchiq_connect);
10766 +/****************************************************************************
10768 +* vchiq_add_service
10770 +***************************************************************************/
10772 +VCHIQ_STATUS_T vchiq_add_service(
10773 + VCHIQ_INSTANCE_T instance,
10774 + const VCHIQ_SERVICE_PARAMS_T *params,
10775 + VCHIQ_SERVICE_HANDLE_T *phandle)
10777 + VCHIQ_STATUS_T status;
10778 + VCHIQ_STATE_T *state = instance->state;
10779 + VCHIQ_SERVICE_T *service = NULL;
10782 + vchiq_log_trace(vchiq_core_log_level,
10783 + "%s(%p) called", __func__, instance);
10785 + *phandle = VCHIQ_SERVICE_HANDLE_INVALID;
10787 + srvstate = vchiq_is_connected(instance)
10788 + ? VCHIQ_SRVSTATE_LISTENING
10789 + : VCHIQ_SRVSTATE_HIDDEN;
10791 + service = vchiq_add_service_internal(
10799 + *phandle = service->handle;
10800 + status = VCHIQ_SUCCESS;
10802 + status = VCHIQ_ERROR;
10804 + vchiq_log_trace(vchiq_core_log_level,
10805 + "%s(%p): returning %d", __func__, instance, status);
10809 +EXPORT_SYMBOL(vchiq_add_service);
10811 +/****************************************************************************
10813 +* vchiq_open_service
10815 +***************************************************************************/
10817 +VCHIQ_STATUS_T vchiq_open_service(
10818 + VCHIQ_INSTANCE_T instance,
10819 + const VCHIQ_SERVICE_PARAMS_T *params,
10820 + VCHIQ_SERVICE_HANDLE_T *phandle)
10822 + VCHIQ_STATUS_T status = VCHIQ_ERROR;
10823 + VCHIQ_STATE_T *state = instance->state;
10824 + VCHIQ_SERVICE_T *service = NULL;
10826 + vchiq_log_trace(vchiq_core_log_level,
10827 + "%s(%p) called", __func__, instance);
10829 + *phandle = VCHIQ_SERVICE_HANDLE_INVALID;
10831 + if (!vchiq_is_connected(instance))
10834 + service = vchiq_add_service_internal(state,
10836 + VCHIQ_SRVSTATE_OPENING,
10841 + status = vchiq_open_service_internal(service, current->pid);
10842 + if (status == VCHIQ_SUCCESS)
10843 + *phandle = service->handle;
10845 + vchiq_remove_service(service->handle);
10849 + vchiq_log_trace(vchiq_core_log_level,
10850 + "%s(%p): returning %d", __func__, instance, status);
10854 +EXPORT_SYMBOL(vchiq_open_service);
10857 +vchiq_queue_bulk_transmit(VCHIQ_SERVICE_HANDLE_T handle,
10858 + const void *data, unsigned int size, void *userdata)
10860 + return vchiq_bulk_transfer(handle,
10861 + VCHI_MEM_HANDLE_INVALID, (void *)data, size, userdata,
10862 + VCHIQ_BULK_MODE_CALLBACK, VCHIQ_BULK_TRANSMIT);
10864 +EXPORT_SYMBOL(vchiq_queue_bulk_transmit);
10867 +vchiq_queue_bulk_receive(VCHIQ_SERVICE_HANDLE_T handle, void *data,
10868 + unsigned int size, void *userdata)
10870 + return vchiq_bulk_transfer(handle,
10871 + VCHI_MEM_HANDLE_INVALID, data, size, userdata,
10872 + VCHIQ_BULK_MODE_CALLBACK, VCHIQ_BULK_RECEIVE);
10874 +EXPORT_SYMBOL(vchiq_queue_bulk_receive);
10877 +vchiq_bulk_transmit(VCHIQ_SERVICE_HANDLE_T handle, const void *data,
10878 + unsigned int size, void *userdata, VCHIQ_BULK_MODE_T mode)
10880 + VCHIQ_STATUS_T status;
10883 + case VCHIQ_BULK_MODE_NOCALLBACK:
10884 + case VCHIQ_BULK_MODE_CALLBACK:
10885 + status = vchiq_bulk_transfer(handle,
10886 + VCHI_MEM_HANDLE_INVALID, (void *)data, size, userdata,
10887 + mode, VCHIQ_BULK_TRANSMIT);
10889 + case VCHIQ_BULK_MODE_BLOCKING:
10890 + status = vchiq_blocking_bulk_transfer(handle,
10891 + (void *)data, size, VCHIQ_BULK_TRANSMIT);
10894 + return VCHIQ_ERROR;
10899 +EXPORT_SYMBOL(vchiq_bulk_transmit);
10902 +vchiq_bulk_receive(VCHIQ_SERVICE_HANDLE_T handle, void *data,
10903 + unsigned int size, void *userdata, VCHIQ_BULK_MODE_T mode)
10905 + VCHIQ_STATUS_T status;
10908 + case VCHIQ_BULK_MODE_NOCALLBACK:
10909 + case VCHIQ_BULK_MODE_CALLBACK:
10910 + status = vchiq_bulk_transfer(handle,
10911 + VCHI_MEM_HANDLE_INVALID, data, size, userdata,
10912 + mode, VCHIQ_BULK_RECEIVE);
10914 + case VCHIQ_BULK_MODE_BLOCKING:
10915 + status = vchiq_blocking_bulk_transfer(handle,
10916 + (void *)data, size, VCHIQ_BULK_RECEIVE);
10919 + return VCHIQ_ERROR;
10924 +EXPORT_SYMBOL(vchiq_bulk_receive);
10926 +static VCHIQ_STATUS_T
10927 +vchiq_blocking_bulk_transfer(VCHIQ_SERVICE_HANDLE_T handle, void *data,
10928 + unsigned int size, VCHIQ_BULK_DIR_T dir)
10930 + VCHIQ_INSTANCE_T instance;
10931 + VCHIQ_SERVICE_T *service;
10932 + VCHIQ_STATUS_T status;
10933 + struct bulk_waiter_node *waiter = NULL;
10934 + struct list_head *pos;
10936 + service = find_service_by_handle(handle);
10938 + return VCHIQ_ERROR;
10940 + instance = service->instance;
10942 + unlock_service(service);
10944 + mutex_lock(&instance->bulk_waiter_list_mutex);
10945 + list_for_each(pos, &instance->bulk_waiter_list) {
10946 + if (list_entry(pos, struct bulk_waiter_node,
10947 + list)->pid == current->pid) {
10948 + waiter = list_entry(pos,
10949 + struct bulk_waiter_node,
10955 + mutex_unlock(&instance->bulk_waiter_list_mutex);
10958 + VCHIQ_BULK_T *bulk = waiter->bulk_waiter.bulk;
10960 + /* This thread has an outstanding bulk transfer. */
10961 + if ((bulk->data != data) ||
10962 + (bulk->size != size)) {
10963 + /* This is not a retry of the previous one.
10964 + ** Cancel the signal when the transfer
10966 + spin_lock(&bulk_waiter_spinlock);
10967 + bulk->userdata = NULL;
10968 + spin_unlock(&bulk_waiter_spinlock);
10974 + waiter = kzalloc(sizeof(struct bulk_waiter_node), GFP_KERNEL);
10976 + vchiq_log_error(vchiq_core_log_level,
10977 + "%s - out of memory", __func__);
10978 + return VCHIQ_ERROR;
10982 + status = vchiq_bulk_transfer(handle, VCHI_MEM_HANDLE_INVALID,
10983 + data, size, &waiter->bulk_waiter, VCHIQ_BULK_MODE_BLOCKING,
10985 + if ((status != VCHIQ_RETRY) || fatal_signal_pending(current) ||
10986 + !waiter->bulk_waiter.bulk) {
10987 + VCHIQ_BULK_T *bulk = waiter->bulk_waiter.bulk;
10989 + /* Cancel the signal when the transfer
10991 + spin_lock(&bulk_waiter_spinlock);
10992 + bulk->userdata = NULL;
10993 + spin_unlock(&bulk_waiter_spinlock);
10997 + waiter->pid = current->pid;
10998 + mutex_lock(&instance->bulk_waiter_list_mutex);
10999 + list_add(&waiter->list, &instance->bulk_waiter_list);
11000 + mutex_unlock(&instance->bulk_waiter_list_mutex);
11001 + vchiq_log_info(vchiq_arm_log_level,
11002 + "saved bulk_waiter %x for pid %d",
11003 + (unsigned int)waiter, current->pid);
11008 diff --git a/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_memdrv.h b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_memdrv.h
11009 new file mode 100644
11010 index 0000000..d02e776
11012 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_memdrv.h
11015 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
11017 + * Redistribution and use in source and binary forms, with or without
11018 + * modification, are permitted provided that the following conditions
11020 + * 1. Redistributions of source code must retain the above copyright
11021 + * notice, this list of conditions, and the following disclaimer,
11022 + * without modification.
11023 + * 2. Redistributions in binary form must reproduce the above copyright
11024 + * notice, this list of conditions and the following disclaimer in the
11025 + * documentation and/or other materials provided with the distribution.
11026 + * 3. The names of the above-listed copyright holders may not be used
11027 + * to endorse or promote products derived from this software without
11028 + * specific prior written permission.
11030 + * ALTERNATIVELY, this software may be distributed under the terms of the
11031 + * GNU General Public License ("GPL") version 2, as published by the Free
11032 + * Software Foundation.
11034 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
11035 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
11036 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
11037 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
11038 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
11039 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
11040 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
11041 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
11042 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
11043 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
11044 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
11047 +#ifndef VCHIQ_MEMDRV_H
11048 +#define VCHIQ_MEMDRV_H
11050 +/* ---- Include Files ----------------------------------------------------- */
11052 +#include <linux/kernel.h>
11053 +#include "vchiq_if.h"
11055 +/* ---- Constants and Types ---------------------------------------------- */
11058 + void *armSharedMemVirt;
11059 + dma_addr_t armSharedMemPhys;
11060 + size_t armSharedMemSize;
11062 + void *vcSharedMemVirt;
11063 + dma_addr_t vcSharedMemPhys;
11064 + size_t vcSharedMemSize;
11065 +} VCHIQ_SHARED_MEM_INFO_T;
11067 +/* ---- Variable Externs ------------------------------------------------- */
11069 +/* ---- Function Prototypes ---------------------------------------------- */
11071 +void vchiq_get_shared_mem_info(VCHIQ_SHARED_MEM_INFO_T *info);
11073 +VCHIQ_STATUS_T vchiq_memdrv_initialise(void);
11075 +VCHIQ_STATUS_T vchiq_userdrv_create_instance(
11076 + const VCHIQ_PLATFORM_DATA_T * platform_data);
11078 +VCHIQ_STATUS_T vchiq_userdrv_suspend(
11079 + const VCHIQ_PLATFORM_DATA_T * platform_data);
11081 +VCHIQ_STATUS_T vchiq_userdrv_resume(
11082 + const VCHIQ_PLATFORM_DATA_T * platform_data);
11085 diff --git a/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_pagelist.h b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_pagelist.h
11086 new file mode 100644
11087 index 0000000..54a3ece
11089 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_pagelist.h
11092 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
11094 + * Redistribution and use in source and binary forms, with or without
11095 + * modification, are permitted provided that the following conditions
11097 + * 1. Redistributions of source code must retain the above copyright
11098 + * notice, this list of conditions, and the following disclaimer,
11099 + * without modification.
11100 + * 2. Redistributions in binary form must reproduce the above copyright
11101 + * notice, this list of conditions and the following disclaimer in the
11102 + * documentation and/or other materials provided with the distribution.
11103 + * 3. The names of the above-listed copyright holders may not be used
11104 + * to endorse or promote products derived from this software without
11105 + * specific prior written permission.
11107 + * ALTERNATIVELY, this software may be distributed under the terms of the
11108 + * GNU General Public License ("GPL") version 2, as published by the Free
11109 + * Software Foundation.
11111 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
11112 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
11113 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
11114 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
11115 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
11116 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
11117 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
11118 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
11119 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
11120 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
11121 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
11124 +#ifndef VCHIQ_PAGELIST_H
11125 +#define VCHIQ_PAGELIST_H
11128 +#define PAGE_SIZE 4096
11130 +#define CACHE_LINE_SIZE 32
11131 +#define PAGELIST_WRITE 0
11132 +#define PAGELIST_READ 1
11133 +#define PAGELIST_READ_WITH_FRAGMENTS 2
11135 +typedef struct pagelist_struct {
11136 + unsigned long length;
11137 + unsigned short type;
11138 + unsigned short offset;
11139 + unsigned long addrs[1]; /* N.B. 12 LSBs hold the number of following
11140 + pages at consecutive addresses. */
11143 +typedef struct fragments_struct {
11144 + char headbuf[CACHE_LINE_SIZE];
11145 + char tailbuf[CACHE_LINE_SIZE];
11148 +#endif /* VCHIQ_PAGELIST_H */
11149 diff --git a/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_proc.c b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_proc.c
11150 new file mode 100644
11151 index 0000000..8e59676
11153 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_proc.c
11156 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
11158 + * Redistribution and use in source and binary forms, with or without
11159 + * modification, are permitted provided that the following conditions
11161 + * 1. Redistributions of source code must retain the above copyright
11162 + * notice, this list of conditions, and the following disclaimer,
11163 + * without modification.
11164 + * 2. Redistributions in binary form must reproduce the above copyright
11165 + * notice, this list of conditions and the following disclaimer in the
11166 + * documentation and/or other materials provided with the distribution.
11167 + * 3. The names of the above-listed copyright holders may not be used
11168 + * to endorse or promote products derived from this software without
11169 + * specific prior written permission.
11171 + * ALTERNATIVELY, this software may be distributed under the terms of the
11172 + * GNU General Public License ("GPL") version 2, as published by the Free
11173 + * Software Foundation.
11175 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
11176 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
11177 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
11178 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
11179 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
11180 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
11181 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
11182 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
11183 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
11184 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
11185 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
11189 +#include <linux/proc_fs.h>
11190 +#include "vchiq_core.h"
11191 +#include "vchiq_arm.h"
11195 +int vchiq_proc_init(void)
11200 +void vchiq_proc_deinit(void)
11206 +struct vchiq_proc_info {
11207 + /* Global 'vc' proc entry used by all instances */
11208 + struct proc_dir_entry *vc_cfg_dir;
11210 + /* one entry per client process */
11211 + struct proc_dir_entry *clients;
11213 + /* log categories */
11214 + struct proc_dir_entry *log_categories;
11217 +static struct vchiq_proc_info proc_info;
11219 +struct proc_dir_entry *vchiq_proc_top(void)
11221 + BUG_ON(proc_info.vc_cfg_dir == NULL);
11222 + return proc_info.vc_cfg_dir;
11225 +/****************************************************************************
11227 +* log category entries
11229 +***************************************************************************/
11230 +#define PROC_WRITE_BUF_SIZE 256
11232 +#define VCHIQ_LOG_ERROR_STR "error"
11233 +#define VCHIQ_LOG_WARNING_STR "warning"
11234 +#define VCHIQ_LOG_INFO_STR "info"
11235 +#define VCHIQ_LOG_TRACE_STR "trace"
11237 +static int log_cfg_read(char *buffer,
11245 + char *log_value = NULL;
11247 + switch (*((int *)data)) {
11248 + case VCHIQ_LOG_ERROR:
11249 + log_value = VCHIQ_LOG_ERROR_STR;
11251 + case VCHIQ_LOG_WARNING:
11252 + log_value = VCHIQ_LOG_WARNING_STR;
11254 + case VCHIQ_LOG_INFO:
11255 + log_value = VCHIQ_LOG_INFO_STR;
11257 + case VCHIQ_LOG_TRACE:
11258 + log_value = VCHIQ_LOG_TRACE_STR;
11264 + len += sprintf(buffer + len,
11266 + log_value ? log_value : "(null)");
11272 +static int log_cfg_write(struct file *file,
11273 + const char __user *buffer,
11274 + unsigned long count,
11277 + int *log_module = data;
11278 + char kbuf[PROC_WRITE_BUF_SIZE + 1];
11282 + memset(kbuf, 0, PROC_WRITE_BUF_SIZE + 1);
11283 + if (count >= PROC_WRITE_BUF_SIZE)
11284 + count = PROC_WRITE_BUF_SIZE;
11286 + if (copy_from_user(kbuf,
11290 + kbuf[count - 1] = 0;
11292 + if (strncmp("error", kbuf, strlen("error")) == 0)
11293 + *log_module = VCHIQ_LOG_ERROR;
11294 + else if (strncmp("warning", kbuf, strlen("warning")) == 0)
11295 + *log_module = VCHIQ_LOG_WARNING;
11296 + else if (strncmp("info", kbuf, strlen("info")) == 0)
11297 + *log_module = VCHIQ_LOG_INFO;
11298 + else if (strncmp("trace", kbuf, strlen("trace")) == 0)
11299 + *log_module = VCHIQ_LOG_TRACE;
11301 + *log_module = VCHIQ_LOG_DEFAULT;
11306 +/* Log category proc entries */
11307 +struct vchiq_proc_log_entry {
11308 + const char *name;
11310 + struct proc_dir_entry *dir;
11313 +static struct vchiq_proc_log_entry vchiq_proc_log_entries[] = {
11314 + { "core", &vchiq_core_log_level },
11315 + { "msg", &vchiq_core_msg_log_level },
11316 + { "sync", &vchiq_sync_log_level },
11317 + { "susp", &vchiq_susp_log_level },
11318 + { "arm", &vchiq_arm_log_level },
11320 +static int n_log_entries =
11321 + sizeof(vchiq_proc_log_entries)/sizeof(vchiq_proc_log_entries[0]);
11323 +/* create an entry under /proc/vc/log for each log category */
11324 +static int vchiq_proc_create_log_entries(struct proc_dir_entry *top)
11326 + struct proc_dir_entry *dir;
11329 + dir = proc_mkdir("log", proc_info.vc_cfg_dir);
11332 + proc_info.log_categories = dir;
11334 + for (i = 0; i < n_log_entries; i++) {
11335 + dir = create_proc_entry(vchiq_proc_log_entries[i].name,
11337 + proc_info.log_categories);
11343 + dir->read_proc = &log_cfg_read;
11344 + dir->write_proc = &log_cfg_write;
11345 + dir->data = (void *)vchiq_proc_log_entries[i].plevel;
11347 + vchiq_proc_log_entries[i].dir = dir;
11353 +int vchiq_proc_init(void)
11355 + BUG_ON(proc_info.vc_cfg_dir != NULL);
11357 + proc_info.vc_cfg_dir = proc_mkdir("vc", NULL);
11358 + if (proc_info.vc_cfg_dir == NULL)
11361 + proc_info.clients = proc_mkdir("clients",
11362 + proc_info.vc_cfg_dir);
11363 + if (!proc_info.clients)
11366 + if (vchiq_proc_create_log_entries(proc_info.vc_cfg_dir) != 0)
11372 + vchiq_proc_deinit();
11373 + vchiq_log_error(vchiq_arm_log_level,
11374 + "%s: failed to create proc directory",
11380 +/* remove all the proc entries */
11381 +void vchiq_proc_deinit(void)
11383 + /* log category entries */
11384 + if (proc_info.log_categories) {
11386 + for (i = 0; i < n_log_entries; i++)
11387 + if (vchiq_proc_log_entries[i].dir)
11388 + remove_proc_entry(
11389 + vchiq_proc_log_entries[i].name,
11390 + proc_info.log_categories);
11392 + remove_proc_entry(proc_info.log_categories->name,
11393 + proc_info.vc_cfg_dir);
11395 + if (proc_info.clients)
11396 + remove_proc_entry(proc_info.clients->name,
11397 + proc_info.vc_cfg_dir);
11398 + if (proc_info.vc_cfg_dir)
11399 + remove_proc_entry(proc_info.vc_cfg_dir->name, NULL);
11402 +struct proc_dir_entry *vchiq_clients_top(void)
11404 + return proc_info.clients;
11408 diff --git a/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_shim.c b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_shim.c
11409 new file mode 100644
11410 index 0000000..a0b069d
11412 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_shim.c
11415 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
11417 + * Redistribution and use in source and binary forms, with or without
11418 + * modification, are permitted provided that the following conditions
11420 + * 1. Redistributions of source code must retain the above copyright
11421 + * notice, this list of conditions, and the following disclaimer,
11422 + * without modification.
11423 + * 2. Redistributions in binary form must reproduce the above copyright
11424 + * notice, this list of conditions and the following disclaimer in the
11425 + * documentation and/or other materials provided with the distribution.
11426 + * 3. The names of the above-listed copyright holders may not be used
11427 + * to endorse or promote products derived from this software without
11428 + * specific prior written permission.
11430 + * ALTERNATIVELY, this software may be distributed under the terms of the
11431 + * GNU General Public License ("GPL") version 2, as published by the Free
11432 + * Software Foundation.
11434 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
11435 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
11436 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
11437 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
11438 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
11439 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
11440 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
11441 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
11442 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
11443 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
11444 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
11446 +#include <linux/module.h>
11447 +#include <linux/types.h>
11449 +#include "interface/vchi/vchi.h"
11450 +#include "vchiq.h"
11451 +#include "vchiq_core.h"
11453 +#include "vchiq_util.h"
11455 +#include <stddef.h>
11457 +#define vchiq_status_to_vchi(status) ((int32_t)status)
11460 + VCHIQ_SERVICE_HANDLE_T handle;
11462 + VCHIU_QUEUE_T queue;
11464 + VCHI_CALLBACK_T callback;
11465 + void *callback_param;
11468 +/* ----------------------------------------------------------------------
11469 + * return pointer to the mphi message driver function table
11470 + * -------------------------------------------------------------------- */
11471 +const VCHI_MESSAGE_DRIVER_T *
11472 +vchi_mphi_message_driver_func_table(void)
11477 +/* ----------------------------------------------------------------------
11478 + * return a pointer to the 'single' connection driver fops
11479 + * -------------------------------------------------------------------- */
11480 +const VCHI_CONNECTION_API_T *
11481 +single_get_func_table(void)
11486 +VCHI_CONNECTION_T *vchi_create_connection(
11487 + const VCHI_CONNECTION_API_T *function_table,
11488 + const VCHI_MESSAGE_DRIVER_T *low_level)
11490 + (void)function_table;
11495 +/***********************************************************
11496 + * Name: vchi_msg_peek
11498 + * Arguments: const VCHI_SERVICE_HANDLE_T handle,
11500 + * uint32_t *msg_size,
11503 + * VCHI_FLAGS_T flags
11505 + * Description: Routine to return a pointer to the current message (to allow in
11506 + * place processing). The message can be removed using
11507 + * vchi_msg_remove when you're finished
11509 + * Returns: int32_t - success == 0
11511 + ***********************************************************/
11512 +int32_t vchi_msg_peek(VCHI_SERVICE_HANDLE_T handle,
11514 + uint32_t *msg_size,
11515 + VCHI_FLAGS_T flags)
11517 + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
11518 + VCHIQ_HEADER_T *header;
11520 + WARN_ON((flags != VCHI_FLAGS_NONE) &&
11521 + (flags != VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE));
11523 + if (flags == VCHI_FLAGS_NONE)
11524 + if (vchiu_queue_is_empty(&service->queue))
11527 + header = vchiu_queue_peek(&service->queue);
11529 + *data = header->data;
11530 + *msg_size = header->size;
11534 +EXPORT_SYMBOL(vchi_msg_peek);
11536 +/***********************************************************
11537 + * Name: vchi_msg_remove
11539 + * Arguments: const VCHI_SERVICE_HANDLE_T handle,
11541 + * Description: Routine to remove a message (after it has been read with
11544 + * Returns: int32_t - success == 0
11546 + ***********************************************************/
11547 +int32_t vchi_msg_remove(VCHI_SERVICE_HANDLE_T handle)
11549 + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
11550 + VCHIQ_HEADER_T *header;
11552 + header = vchiu_queue_pop(&service->queue);
11554 + vchiq_release_message(service->handle, header);
11558 +EXPORT_SYMBOL(vchi_msg_remove);
11560 +/***********************************************************
11561 + * Name: vchi_msg_queue
11563 + * Arguments: VCHI_SERVICE_HANDLE_T handle,
11564 + * const void *data,
11565 + * uint32_t data_size,
11566 + * VCHI_FLAGS_T flags,
11567 + * void *msg_handle,
11569 + * Description: Thin wrapper to queue a message onto a connection
11571 + * Returns: int32_t - success == 0
11573 + ***********************************************************/
11574 +int32_t vchi_msg_queue(VCHI_SERVICE_HANDLE_T handle,
11575 + const void *data,
11576 + uint32_t data_size,
11577 + VCHI_FLAGS_T flags,
11578 + void *msg_handle)
11580 + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
11581 + VCHIQ_ELEMENT_T element = {data, data_size};
11582 + VCHIQ_STATUS_T status;
11584 + (void)msg_handle;
11586 + WARN_ON(flags != VCHI_FLAGS_BLOCK_UNTIL_QUEUED);
11588 + status = vchiq_queue_message(service->handle, &element, 1);
11590 + /* vchiq_queue_message() may return VCHIQ_RETRY, so we need to
11591 + ** implement a retry mechanism since this function is supposed
11592 + ** to block until queued
11594 + while (status == VCHIQ_RETRY) {
11596 + status = vchiq_queue_message(service->handle, &element, 1);
11599 + return vchiq_status_to_vchi(status);
11601 +EXPORT_SYMBOL(vchi_msg_queue);
11603 +/***********************************************************
11604 + * Name: vchi_bulk_queue_receive
11606 + * Arguments: VCHI_BULK_HANDLE_T handle,
11607 + * void *data_dst,
11608 + * const uint32_t data_size,
11609 + * VCHI_FLAGS_T flags
11610 + * void *bulk_handle
11612 + * Description: Routine to setup a rcv buffer
11614 + * Returns: int32_t - success == 0
11616 + ***********************************************************/
11617 +int32_t vchi_bulk_queue_receive(VCHI_SERVICE_HANDLE_T handle,
11619 + uint32_t data_size,
11620 + VCHI_FLAGS_T flags,
11621 + void *bulk_handle)
11623 + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
11624 + VCHIQ_BULK_MODE_T mode;
11625 + VCHIQ_STATUS_T status;
11627 + switch ((int)flags) {
11628 + case VCHI_FLAGS_CALLBACK_WHEN_OP_COMPLETE
11629 + | VCHI_FLAGS_BLOCK_UNTIL_QUEUED:
11630 + WARN_ON(!service->callback);
11631 + mode = VCHIQ_BULK_MODE_CALLBACK;
11633 + case VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE:
11634 + mode = VCHIQ_BULK_MODE_BLOCKING;
11636 + case VCHI_FLAGS_BLOCK_UNTIL_QUEUED:
11637 + case VCHI_FLAGS_NONE:
11638 + mode = VCHIQ_BULK_MODE_NOCALLBACK;
11641 + WARN(1, "unsupported message\n");
11642 + return vchiq_status_to_vchi(VCHIQ_ERROR);
11645 + status = vchiq_bulk_receive(service->handle, data_dst, data_size,
11646 + bulk_handle, mode);
11648 + /* vchiq_bulk_receive() may return VCHIQ_RETRY, so we need to
11649 + ** implement a retry mechanism since this function is supposed
11650 + ** to block until queued
11652 + while (status == VCHIQ_RETRY) {
11654 + status = vchiq_bulk_receive(service->handle, data_dst,
11655 + data_size, bulk_handle, mode);
11658 + return vchiq_status_to_vchi(status);
11660 +EXPORT_SYMBOL(vchi_bulk_queue_receive);
11662 +/***********************************************************
11663 + * Name: vchi_bulk_queue_transmit
11665 + * Arguments: VCHI_BULK_HANDLE_T handle,
11666 + * const void *data_src,
11667 + * uint32_t data_size,
11668 + * VCHI_FLAGS_T flags,
11669 + * void *bulk_handle
11671 + * Description: Routine to transmit some data
11673 + * Returns: int32_t - success == 0
11675 + ***********************************************************/
11676 +int32_t vchi_bulk_queue_transmit(VCHI_SERVICE_HANDLE_T handle,
11677 + const void *data_src,
11678 + uint32_t data_size,
11679 + VCHI_FLAGS_T flags,
11680 + void *bulk_handle)
11682 + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
11683 + VCHIQ_BULK_MODE_T mode;
11684 + VCHIQ_STATUS_T status;
11686 + switch ((int)flags) {
11687 + case VCHI_FLAGS_CALLBACK_WHEN_OP_COMPLETE
11688 + | VCHI_FLAGS_BLOCK_UNTIL_QUEUED:
11689 + WARN_ON(!service->callback);
11690 + mode = VCHIQ_BULK_MODE_CALLBACK;
11692 + case VCHI_FLAGS_BLOCK_UNTIL_DATA_READ:
11693 + case VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE:
11694 + mode = VCHIQ_BULK_MODE_BLOCKING;
11696 + case VCHI_FLAGS_BLOCK_UNTIL_QUEUED:
11697 + case VCHI_FLAGS_NONE:
11698 + mode = VCHIQ_BULK_MODE_NOCALLBACK;
11701 + WARN(1, "unsupported message\n");
11702 + return vchiq_status_to_vchi(VCHIQ_ERROR);
11705 + status = vchiq_bulk_transmit(service->handle, data_src, data_size,
11706 + bulk_handle, mode);
11708 + /* vchiq_bulk_transmit() may return VCHIQ_RETRY, so we need to
11709 + ** implement a retry mechanism since this function is supposed
11710 + ** to block until queued
11712 + while (status == VCHIQ_RETRY) {
11714 + status = vchiq_bulk_transmit(service->handle, data_src,
11715 + data_size, bulk_handle, mode);
11718 + return vchiq_status_to_vchi(status);
11720 +EXPORT_SYMBOL(vchi_bulk_queue_transmit);
11722 +/***********************************************************
11723 + * Name: vchi_msg_dequeue
11725 + * Arguments: VCHI_SERVICE_HANDLE_T handle,
11727 + * uint32_t max_data_size_to_read,
11728 + * uint32_t *actual_msg_size
11729 + * VCHI_FLAGS_T flags
11731 + * Description: Routine to dequeue a message into the supplied buffer
11733 + * Returns: int32_t - success == 0
11735 + ***********************************************************/
11736 +int32_t vchi_msg_dequeue(VCHI_SERVICE_HANDLE_T handle,
11738 + uint32_t max_data_size_to_read,
11739 + uint32_t *actual_msg_size,
11740 + VCHI_FLAGS_T flags)
11742 + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
11743 + VCHIQ_HEADER_T *header;
11745 + WARN_ON((flags != VCHI_FLAGS_NONE) &&
11746 + (flags != VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE));
11748 + if (flags == VCHI_FLAGS_NONE)
11749 + if (vchiu_queue_is_empty(&service->queue))
11752 + header = vchiu_queue_pop(&service->queue);
11754 + memcpy(data, header->data, header->size < max_data_size_to_read ?
11755 + header->size : max_data_size_to_read);
11757 + *actual_msg_size = header->size;
11759 + vchiq_release_message(service->handle, header);
11763 +EXPORT_SYMBOL(vchi_msg_dequeue);
11765 +/***********************************************************
11766 + * Name: vchi_msg_queuev
11768 + * Arguments: VCHI_SERVICE_HANDLE_T handle,
11769 + * VCHI_MSG_VECTOR_T *vector,
11770 + * uint32_t count,
11771 + * VCHI_FLAGS_T flags,
11772 + * void *msg_handle
11774 + * Description: Thin wrapper to queue a message onto a connection
11776 + * Returns: int32_t - success == 0
11778 + ***********************************************************/
11780 +vchiq_static_assert(sizeof(VCHI_MSG_VECTOR_T) == sizeof(VCHIQ_ELEMENT_T));
11781 +vchiq_static_assert(offsetof(VCHI_MSG_VECTOR_T, vec_base) ==
11782 + offsetof(VCHIQ_ELEMENT_T, data));
11783 +vchiq_static_assert(offsetof(VCHI_MSG_VECTOR_T, vec_len) ==
11784 + offsetof(VCHIQ_ELEMENT_T, size));
11786 +int32_t vchi_msg_queuev(VCHI_SERVICE_HANDLE_T handle,
11787 + VCHI_MSG_VECTOR_T *vector,
11789 + VCHI_FLAGS_T flags,
11790 + void *msg_handle)
11792 + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
11794 + (void)msg_handle;
11796 + WARN_ON(flags != VCHI_FLAGS_BLOCK_UNTIL_QUEUED);
11798 + return vchiq_status_to_vchi(vchiq_queue_message(service->handle,
11799 + (const VCHIQ_ELEMENT_T *)vector, count));
11801 +EXPORT_SYMBOL(vchi_msg_queuev);
11803 +/***********************************************************
11804 + * Name: vchi_held_msg_release
11806 + * Arguments: VCHI_HELD_MSG_T *message
11808 + * Description: Routine to release a held message (after it has been read with
11811 + * Returns: int32_t - success == 0
11813 + ***********************************************************/
11814 +int32_t vchi_held_msg_release(VCHI_HELD_MSG_T *message)
11816 + vchiq_release_message((VCHIQ_SERVICE_HANDLE_T)message->service,
11817 + (VCHIQ_HEADER_T *)message->message);
11821 +EXPORT_SYMBOL(vchi_held_msg_release);
11823 +/***********************************************************
11824 + * Name: vchi_msg_hold
11826 + * Arguments: VCHI_SERVICE_HANDLE_T handle,
11828 + * uint32_t *msg_size,
11829 + * VCHI_FLAGS_T flags,
11830 + * VCHI_HELD_MSG_T *message_handle
11832 + * Description: Routine to return a pointer to the current message (to allow
11833 + * in place processing). The message is dequeued - don't forget
11834 + * to release the message using vchi_held_msg_release when you're
11837 + * Returns: int32_t - success == 0
11839 + ***********************************************************/
11840 +int32_t vchi_msg_hold(VCHI_SERVICE_HANDLE_T handle,
11842 + uint32_t *msg_size,
11843 + VCHI_FLAGS_T flags,
11844 + VCHI_HELD_MSG_T *message_handle)
11846 + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
11847 + VCHIQ_HEADER_T *header;
11849 + WARN_ON((flags != VCHI_FLAGS_NONE) &&
11850 + (flags != VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE));
11852 + if (flags == VCHI_FLAGS_NONE)
11853 + if (vchiu_queue_is_empty(&service->queue))
11856 + header = vchiu_queue_pop(&service->queue);
11858 + *data = header->data;
11859 + *msg_size = header->size;
11861 + message_handle->service =
11862 + (struct opaque_vchi_service_t *)service->handle;
11863 + message_handle->message = header;
11867 +EXPORT_SYMBOL(vchi_msg_hold);
11869 +/***********************************************************
11870 + * Name: vchi_initialise
11872 + * Arguments: VCHI_INSTANCE_T *instance_handle
11873 + * VCHI_CONNECTION_T **connections
11874 + * const uint32_t num_connections
11876 + * Description: Initialises the hardware but does not transmit anything
11877 + * When run as a Host App this will be called twice hence the need
11878 + * to malloc the state information
11880 + * Returns: 0 if successful, failure otherwise
11882 + ***********************************************************/
11884 +int32_t vchi_initialise(VCHI_INSTANCE_T *instance_handle)
11886 + VCHIQ_INSTANCE_T instance;
11887 + VCHIQ_STATUS_T status;
11889 + status = vchiq_initialise(&instance);
11891 + *instance_handle = (VCHI_INSTANCE_T)instance;
11893 + return vchiq_status_to_vchi(status);
11895 +EXPORT_SYMBOL(vchi_initialise);
11897 +/***********************************************************
11898 + * Name: vchi_connect
11900 + * Arguments: VCHI_CONNECTION_T **connections
11901 + * const uint32_t num_connections
11902 + * VCHI_INSTANCE_T instance_handle)
11904 + * Description: Starts the command service on each connection,
11905 + * causing INIT messages to be pinged back and forth
11907 + * Returns: 0 if successful, failure otherwise
11909 + ***********************************************************/
11910 +int32_t vchi_connect(VCHI_CONNECTION_T **connections,
11911 + const uint32_t num_connections,
11912 + VCHI_INSTANCE_T instance_handle)
11914 + VCHIQ_INSTANCE_T instance = (VCHIQ_INSTANCE_T)instance_handle;
11916 + (void)connections;
11917 + (void)num_connections;
11919 + return vchiq_connect(instance);
11921 +EXPORT_SYMBOL(vchi_connect);
11924 +/***********************************************************
11925 + * Name: vchi_disconnect
11927 + * Arguments: VCHI_INSTANCE_T instance_handle
11929 + * Description: Stops the command service on each connection,
11930 + * causing DE-INIT messages to be pinged back and forth
11932 + * Returns: 0 if successful, failure otherwise
11934 + ***********************************************************/
11935 +int32_t vchi_disconnect(VCHI_INSTANCE_T instance_handle)
11937 + VCHIQ_INSTANCE_T instance = (VCHIQ_INSTANCE_T)instance_handle;
11938 + return vchiq_status_to_vchi(vchiq_shutdown(instance));
11940 +EXPORT_SYMBOL(vchi_disconnect);
11943 +/***********************************************************
11944 + * Name: vchi_service_open
11945 + * Name: vchi_service_create
11947 + * Arguments: VCHI_INSTANCE_T *instance_handle
11948 + * SERVICE_CREATION_T *setup,
11949 + * VCHI_SERVICE_HANDLE_T *handle
11951 + * Description: Routine to open a service
11953 + * Returns: int32_t - success == 0
11955 + ***********************************************************/
11957 +static VCHIQ_STATUS_T shim_callback(VCHIQ_REASON_T reason,
11958 + VCHIQ_HEADER_T *header, VCHIQ_SERVICE_HANDLE_T handle, void *bulk_user)
11960 + SHIM_SERVICE_T *service =
11961 + (SHIM_SERVICE_T *)VCHIQ_GET_SERVICE_USERDATA(handle);
11963 + if (!service->callback)
11966 + switch (reason) {
11967 + case VCHIQ_MESSAGE_AVAILABLE:
11968 + vchiu_queue_push(&service->queue, header);
11970 + service->callback(service->callback_param,
11971 + VCHI_CALLBACK_MSG_AVAILABLE, NULL);
11976 + case VCHIQ_BULK_TRANSMIT_DONE:
11977 + service->callback(service->callback_param,
11978 + VCHI_CALLBACK_BULK_SENT, bulk_user);
11981 + case VCHIQ_BULK_RECEIVE_DONE:
11982 + service->callback(service->callback_param,
11983 + VCHI_CALLBACK_BULK_RECEIVED, bulk_user);
11986 + case VCHIQ_SERVICE_CLOSED:
11987 + service->callback(service->callback_param,
11988 + VCHI_CALLBACK_SERVICE_CLOSED, NULL);
11991 + case VCHIQ_SERVICE_OPENED:
11992 + /* No equivalent VCHI reason */
11995 + case VCHIQ_BULK_TRANSMIT_ABORTED:
11996 + service->callback(service->callback_param,
11997 + VCHI_CALLBACK_BULK_TRANSMIT_ABORTED,
12001 + case VCHIQ_BULK_RECEIVE_ABORTED:
12002 + service->callback(service->callback_param,
12003 + VCHI_CALLBACK_BULK_RECEIVE_ABORTED,
12008 + WARN(1, "not supported\n");
12013 + vchiq_release_message(service->handle, header);
12015 + return VCHIQ_SUCCESS;
12018 +static SHIM_SERVICE_T *service_alloc(VCHIQ_INSTANCE_T instance,
12019 + SERVICE_CREATION_T *setup)
12021 + SHIM_SERVICE_T *service = kzalloc(sizeof(SHIM_SERVICE_T), GFP_KERNEL);
12026 + if (vchiu_queue_init(&service->queue, 64)) {
12027 + service->callback = setup->callback;
12028 + service->callback_param = setup->callback_param;
12038 +static void service_free(SHIM_SERVICE_T *service)
12041 + vchiu_queue_delete(&service->queue);
12046 +int32_t vchi_service_open(VCHI_INSTANCE_T instance_handle,
12047 + SERVICE_CREATION_T *setup,
12048 + VCHI_SERVICE_HANDLE_T *handle)
12050 + VCHIQ_INSTANCE_T instance = (VCHIQ_INSTANCE_T)instance_handle;
12051 + SHIM_SERVICE_T *service = service_alloc(instance, setup);
12053 + VCHIQ_SERVICE_PARAMS_T params;
12054 + VCHIQ_STATUS_T status;
12056 + memset(¶ms, 0, sizeof(params));
12057 + params.fourcc = setup->service_id;
12058 + params.callback = shim_callback;
12059 + params.userdata = service;
12060 + params.version = setup->version.version;
12061 + params.version_min = setup->version.version_min;
12063 + status = vchiq_open_service(instance, ¶ms,
12064 + &service->handle);
12065 + if (status != VCHIQ_SUCCESS) {
12066 + service_free(service);
12071 + *handle = (VCHI_SERVICE_HANDLE_T)service;
12073 + return (service != NULL) ? 0 : -1;
12075 +EXPORT_SYMBOL(vchi_service_open);
12077 +int32_t vchi_service_create(VCHI_INSTANCE_T instance_handle,
12078 + SERVICE_CREATION_T *setup,
12079 + VCHI_SERVICE_HANDLE_T *handle)
12081 + VCHIQ_INSTANCE_T instance = (VCHIQ_INSTANCE_T)instance_handle;
12082 + SHIM_SERVICE_T *service = service_alloc(instance, setup);
12084 + VCHIQ_SERVICE_PARAMS_T params;
12085 + VCHIQ_STATUS_T status;
12087 + memset(¶ms, 0, sizeof(params));
12088 + params.fourcc = setup->service_id;
12089 + params.callback = shim_callback;
12090 + params.userdata = service;
12091 + params.version = setup->version.version;
12092 + params.version_min = setup->version.version_min;
12093 + status = vchiq_add_service(instance, ¶ms, &service->handle);
12095 + if (status != VCHIQ_SUCCESS) {
12096 + service_free(service);
12101 + *handle = (VCHI_SERVICE_HANDLE_T)service;
12103 + return (service != NULL) ? 0 : -1;
12105 +EXPORT_SYMBOL(vchi_service_create);
12107 +int32_t vchi_service_close(const VCHI_SERVICE_HANDLE_T handle)
12109 + int32_t ret = -1;
12110 + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
12112 + VCHIQ_STATUS_T status = vchiq_close_service(service->handle);
12113 + if (status == VCHIQ_SUCCESS) {
12114 + service_free(service);
12118 + ret = vchiq_status_to_vchi(status);
12122 +EXPORT_SYMBOL(vchi_service_close);
12124 +int32_t vchi_service_destroy(const VCHI_SERVICE_HANDLE_T handle)
12126 + int32_t ret = -1;
12127 + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
12129 + VCHIQ_STATUS_T status = vchiq_remove_service(service->handle);
12130 + if (status == VCHIQ_SUCCESS) {
12131 + service_free(service);
12135 + ret = vchiq_status_to_vchi(status);
12139 +EXPORT_SYMBOL(vchi_service_destroy);
12141 +int32_t vchi_get_peer_version( const VCHI_SERVICE_HANDLE_T handle, short *peer_version )
12143 + int32_t ret = -1;
12144 + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
12147 + VCHIQ_STATUS_T status = vchiq_get_peer_version(service->handle, peer_version);
12148 + ret = vchiq_status_to_vchi( status );
12152 +EXPORT_SYMBOL(vchi_get_peer_version);
12154 +/* ----------------------------------------------------------------------
12155 + * read a uint32_t from buffer.
12156 + * network format is defined to be little endian
12157 + * -------------------------------------------------------------------- */
12159 +vchi_readbuf_uint32(const void *_ptr)
12161 + const unsigned char *ptr = _ptr;
12162 + return ptr[0] | (ptr[1] << 8) | (ptr[2] << 16) | (ptr[3] << 24);
12165 +/* ----------------------------------------------------------------------
12166 + * write a uint32_t to buffer.
12167 + * network format is defined to be little endian
12168 + * -------------------------------------------------------------------- */
12170 +vchi_writebuf_uint32(void *_ptr, uint32_t value)
12172 + unsigned char *ptr = _ptr;
12173 + ptr[0] = (unsigned char)((value >> 0) & 0xFF);
12174 + ptr[1] = (unsigned char)((value >> 8) & 0xFF);
12175 + ptr[2] = (unsigned char)((value >> 16) & 0xFF);
12176 + ptr[3] = (unsigned char)((value >> 24) & 0xFF);
12179 +/* ----------------------------------------------------------------------
12180 + * read a uint16_t from buffer.
12181 + * network format is defined to be little endian
12182 + * -------------------------------------------------------------------- */
12184 +vchi_readbuf_uint16(const void *_ptr)
12186 + const unsigned char *ptr = _ptr;
12187 + return ptr[0] | (ptr[1] << 8);
12190 +/* ----------------------------------------------------------------------
12191 + * write a uint16_t into the buffer.
12192 + * network format is defined to be little endian
12193 + * -------------------------------------------------------------------- */
12195 +vchi_writebuf_uint16(void *_ptr, uint16_t value)
12197 + unsigned char *ptr = _ptr;
12198 + ptr[0] = (value >> 0) & 0xFF;
12199 + ptr[1] = (value >> 8) & 0xFF;
12202 +/***********************************************************
12203 + * Name: vchi_service_use
12205 + * Arguments: const VCHI_SERVICE_HANDLE_T handle
12207 + * Description: Routine to increment refcount on a service
12211 + ***********************************************************/
12212 +int32_t vchi_service_use(const VCHI_SERVICE_HANDLE_T handle)
12214 + int32_t ret = -1;
12215 + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
12217 + ret = vchiq_status_to_vchi(vchiq_use_service(service->handle));
12220 +EXPORT_SYMBOL(vchi_service_use);
12222 +/***********************************************************
12223 + * Name: vchi_service_release
12225 + * Arguments: const VCHI_SERVICE_HANDLE_T handle
12227 + * Description: Routine to decrement refcount on a service
12231 + ***********************************************************/
12232 +int32_t vchi_service_release(const VCHI_SERVICE_HANDLE_T handle)
12234 + int32_t ret = -1;
12235 + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
12237 + ret = vchiq_status_to_vchi(
12238 + vchiq_release_service(service->handle));
12241 +EXPORT_SYMBOL(vchi_service_release);
12242 diff --git a/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.c b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.c
12243 new file mode 100644
12244 index 0000000..c2eefef
12246 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.c
12249 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
12251 + * Redistribution and use in source and binary forms, with or without
12252 + * modification, are permitted provided that the following conditions
12254 + * 1. Redistributions of source code must retain the above copyright
12255 + * notice, this list of conditions, and the following disclaimer,
12256 + * without modification.
12257 + * 2. Redistributions in binary form must reproduce the above copyright
12258 + * notice, this list of conditions and the following disclaimer in the
12259 + * documentation and/or other materials provided with the distribution.
12260 + * 3. The names of the above-listed copyright holders may not be used
12261 + * to endorse or promote products derived from this software without
12262 + * specific prior written permission.
12264 + * ALTERNATIVELY, this software may be distributed under the terms of the
12265 + * GNU General Public License ("GPL") version 2, as published by the Free
12266 + * Software Foundation.
12268 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
12269 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
12270 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
12271 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
12272 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
12273 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
12274 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
12275 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
12276 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
12277 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
12278 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
12281 +#include "vchiq_util.h"
12283 +static inline int is_pow2(int i)
12285 + return i && !(i & (i - 1));
12288 +int vchiu_queue_init(VCHIU_QUEUE_T *queue, int size)
12290 + WARN_ON(!is_pow2(size));
12292 + queue->size = size;
12294 + queue->write = 0;
12296 + sema_init(&queue->pop, 0);
12297 + sema_init(&queue->push, 0);
12299 + queue->storage = kzalloc(size * sizeof(VCHIQ_HEADER_T *), GFP_KERNEL);
12300 + if (queue->storage == NULL) {
12301 + vchiu_queue_delete(queue);
12307 +void vchiu_queue_delete(VCHIU_QUEUE_T *queue)
12309 + if (queue->storage != NULL)
12310 + kfree(queue->storage);
12313 +int vchiu_queue_is_empty(VCHIU_QUEUE_T *queue)
12315 + return queue->read == queue->write;
12318 +int vchiu_queue_is_full(VCHIU_QUEUE_T *queue)
12320 + return queue->write == queue->read + queue->size;
12323 +void vchiu_queue_push(VCHIU_QUEUE_T *queue, VCHIQ_HEADER_T *header)
12325 + while (queue->write == queue->read + queue->size) {
12326 + if (down_interruptible(&queue->pop) != 0) {
12327 + flush_signals(current);
12332 + * Write to queue->storage must be visible after read from
12337 + queue->storage[queue->write & (queue->size - 1)] = header;
12340 + * Write to queue->storage must be visible before write to
12347 + up(&queue->push);
12350 +VCHIQ_HEADER_T *vchiu_queue_peek(VCHIU_QUEUE_T *queue)
12352 + while (queue->write == queue->read) {
12353 + if (down_interruptible(&queue->push) != 0) {
12354 + flush_signals(current);
12358 + up(&queue->push); // We haven't removed anything from the queue.
12361 + * Read from queue->storage must be visible after read from
12366 + return queue->storage[queue->read & (queue->size - 1)];
12369 +VCHIQ_HEADER_T *vchiu_queue_pop(VCHIU_QUEUE_T *queue)
12371 + VCHIQ_HEADER_T *header;
12373 + while (queue->write == queue->read) {
12374 + if (down_interruptible(&queue->push) != 0) {
12375 + flush_signals(current);
12380 + * Read from queue->storage must be visible after read from
12385 + header = queue->storage[queue->read & (queue->size - 1)];
12388 + * Read from queue->storage must be visible before write to
12399 diff --git a/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.h b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.h
12400 new file mode 100644
12401 index 0000000..f4d0b66
12403 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.h
12406 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
12408 + * Redistribution and use in source and binary forms, with or without
12409 + * modification, are permitted provided that the following conditions
12411 + * 1. Redistributions of source code must retain the above copyright
12412 + * notice, this list of conditions, and the following disclaimer,
12413 + * without modification.
12414 + * 2. Redistributions in binary form must reproduce the above copyright
12415 + * notice, this list of conditions and the following disclaimer in the
12416 + * documentation and/or other materials provided with the distribution.
12417 + * 3. The names of the above-listed copyright holders may not be used
12418 + * to endorse or promote products derived from this software without
12419 + * specific prior written permission.
12421 + * ALTERNATIVELY, this software may be distributed under the terms of the
12422 + * GNU General Public License ("GPL") version 2, as published by the Free
12423 + * Software Foundation.
12425 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
12426 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
12427 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
12428 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
12429 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
12430 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
12431 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
12432 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
12433 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
12434 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
12435 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
12438 +#ifndef VCHIQ_UTIL_H
12439 +#define VCHIQ_UTIL_H
12441 +#include <linux/types.h>
12442 +#include <linux/semaphore.h>
12443 +#include <linux/mutex.h>
12444 +#include <linux/bitops.h>
12445 +#include <linux/kthread.h>
12446 +#include <linux/wait.h>
12447 +#include <linux/vmalloc.h>
12448 +#include <linux/jiffies.h>
12449 +#include <linux/delay.h>
12450 +#include <linux/string.h>
12451 +#include <linux/types.h>
12452 +#include <linux/interrupt.h>
12453 +#include <linux/random.h>
12454 +#include <linux/sched.h>
12455 +#include <linux/ctype.h>
12456 +#include <linux/uaccess.h>
12457 +#include <linux/time.h> /* for time_t */
12458 +#include <linux/slab.h>
12459 +#include <linux/vmalloc.h>
12461 +#include "vchiq_if.h"
12468 + struct semaphore pop;
12469 + struct semaphore push;
12471 + VCHIQ_HEADER_T **storage;
12474 +extern int vchiu_queue_init(VCHIU_QUEUE_T *queue, int size);
12475 +extern void vchiu_queue_delete(VCHIU_QUEUE_T *queue);
12477 +extern int vchiu_queue_is_empty(VCHIU_QUEUE_T *queue);
12478 +extern int vchiu_queue_is_full(VCHIU_QUEUE_T *queue);
12480 +extern void vchiu_queue_push(VCHIU_QUEUE_T *queue, VCHIQ_HEADER_T *header);
12482 +extern VCHIQ_HEADER_T *vchiu_queue_peek(VCHIU_QUEUE_T *queue);
12483 +extern VCHIQ_HEADER_T *vchiu_queue_pop(VCHIU_QUEUE_T *queue);
12486 diff --git a/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_version.c b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_version.c
12487 new file mode 100644
12488 index 0000000..b6bfa21
12490 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_version.c
12493 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
12495 + * Redistribution and use in source and binary forms, with or without
12496 + * modification, are permitted provided that the following conditions
12498 + * 1. Redistributions of source code must retain the above copyright
12499 + * notice, this list of conditions, and the following disclaimer,
12500 + * without modification.
12501 + * 2. Redistributions in binary form must reproduce the above copyright
12502 + * notice, this list of conditions and the following disclaimer in the
12503 + * documentation and/or other materials provided with the distribution.
12504 + * 3. The names of the above-listed copyright holders may not be used
12505 + * to endorse or promote products derived from this software without
12506 + * specific prior written permission.
12508 + * ALTERNATIVELY, this software may be distributed under the terms of the
12509 + * GNU General Public License ("GPL") version 2, as published by the Free
12510 + * Software Foundation.
12512 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
12513 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
12514 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
12515 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
12516 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
12517 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
12518 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
12519 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
12520 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
12521 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
12522 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
12524 +#include "vchiq_build_info.h"
12525 +#include <linux/broadcom/vc_debug_sym.h>
12527 +VC_DEBUG_DECLARE_STRING_VAR( vchiq_build_hostname, "dc4-arm-01" );
12528 +VC_DEBUG_DECLARE_STRING_VAR( vchiq_build_version, "9245b4c35b99b3870e1f7dc598c5692b3c66a6f0 (tainted)" );
12529 +VC_DEBUG_DECLARE_STRING_VAR( vchiq_build_time, __TIME__ );
12530 +VC_DEBUG_DECLARE_STRING_VAR( vchiq_build_date, __DATE__ );
12532 +const char *vchiq_get_build_hostname( void )
12534 + return vchiq_build_hostname;
12537 +const char *vchiq_get_build_version( void )
12539 + return vchiq_build_version;
12542 +const char *vchiq_get_build_date( void )
12544 + return vchiq_build_date;
12547 +const char *vchiq_get_build_time( void )
12549 + return vchiq_build_time;