1 From adee2a81f0be488e079498ac457bf01c954a029e 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 010/114] 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 VCHIQ: Make service closure fully synchronous (drv)
22 This is one half of a two-part patch, the other half of which is to
23 the vchiq_lib user library. With these patches, calls to
24 vchiq_close_service and vchiq_remove_service won't return until any
25 associated callbacks have been delivered to the callback thread.
27 VCHIQ: Add per-service tracing
29 The new service option VCHIQ_SERVICE_OPTION_TRACE is a boolean that
30 toggles tracing for the specified service.
32 This commit also introduces vchi_service_set_option and the associated
33 option VCHI_SERVICE_OPTION_TRACE.
35 vchiq: Make the synchronous-CLOSE logic more tolerant
37 vchiq: Move logging control into debugfs
39 vchiq: Take care of a corner case tickled by VCSM
41 Closing a connection that isn't fully open requires care, since one
42 side does not know the other side's port number. Code was present to
43 handle the case where a CLOSE is sent immediately after an OPEN, i.e.
44 before the OPENACK has been received, but this was incorrectly being
45 used when an OPEN from a client using port 0 was rejected.
47 (In the observed failure, the host was attempting to use the VCSM
48 service, which isn't present in the 'cutdown' firmware. The failure
49 was intermittent because sometimes the keepalive service would
52 This case can be distinguished because the client's remoteport will
53 still be VCHIQ_PORT_FREE, and the srvstate will be OPENING. Either
54 condition is sufficient to differentiate it from the special case
57 drivers/misc/Kconfig | 1 +
58 drivers/misc/Makefile | 1 +
59 drivers/misc/vc04_services/Kconfig | 9 +
60 drivers/misc/vc04_services/Makefile | 17 +
61 .../interface/vchi/connections/connection.h | 328 ++
62 .../interface/vchi/message_drivers/message.h | 204 ++
63 drivers/misc/vc04_services/interface/vchi/vchi.h | 378 ++
64 .../misc/vc04_services/interface/vchi/vchi_cfg.h | 224 ++
65 .../interface/vchi/vchi_cfg_internal.h | 71 +
66 .../vc04_services/interface/vchi/vchi_common.h | 174 +
67 .../misc/vc04_services/interface/vchi/vchi_mh.h | 42 +
68 .../misc/vc04_services/interface/vchiq_arm/vchiq.h | 40 +
69 .../vc04_services/interface/vchiq_arm/vchiq_2835.h | 42 +
70 .../interface/vchiq_arm/vchiq_2835_arm.c | 561 +++
71 .../vc04_services/interface/vchiq_arm/vchiq_arm.c | 2883 +++++++++++++++
72 .../vc04_services/interface/vchiq_arm/vchiq_arm.h | 223 ++
73 .../interface/vchiq_arm/vchiq_build_info.h | 37 +
74 .../vc04_services/interface/vchiq_arm/vchiq_cfg.h | 66 +
75 .../interface/vchiq_arm/vchiq_connected.c | 119 +
76 .../interface/vchiq_arm/vchiq_connected.h | 50 +
77 .../vc04_services/interface/vchiq_arm/vchiq_core.c | 3861 ++++++++++++++++++++
78 .../vc04_services/interface/vchiq_arm/vchiq_core.h | 711 ++++
79 .../interface/vchiq_arm/vchiq_debugfs.c | 383 ++
80 .../interface/vchiq_arm/vchiq_debugfs.h | 52 +
81 .../interface/vchiq_arm/vchiq_genversion | 87 +
82 .../vc04_services/interface/vchiq_arm/vchiq_if.h | 189 +
83 .../interface/vchiq_arm/vchiq_ioctl.h | 131 +
84 .../interface/vchiq_arm/vchiq_kern_lib.c | 456 +++
85 .../interface/vchiq_arm/vchiq_memdrv.h | 71 +
86 .../interface/vchiq_arm/vchiq_pagelist.h | 58 +
87 .../vc04_services/interface/vchiq_arm/vchiq_shim.c | 853 +++++
88 .../vc04_services/interface/vchiq_arm/vchiq_util.c | 151 +
89 .../vc04_services/interface/vchiq_arm/vchiq_util.h | 81 +
90 .../interface/vchiq_arm/vchiq_version.c | 59 +
91 34 files changed, 12613 insertions(+)
92 create mode 100644 drivers/misc/vc04_services/Kconfig
93 create mode 100644 drivers/misc/vc04_services/Makefile
94 create mode 100644 drivers/misc/vc04_services/interface/vchi/connections/connection.h
95 create mode 100644 drivers/misc/vc04_services/interface/vchi/message_drivers/message.h
96 create mode 100644 drivers/misc/vc04_services/interface/vchi/vchi.h
97 create mode 100644 drivers/misc/vc04_services/interface/vchi/vchi_cfg.h
98 create mode 100644 drivers/misc/vc04_services/interface/vchi/vchi_cfg_internal.h
99 create mode 100644 drivers/misc/vc04_services/interface/vchi/vchi_common.h
100 create mode 100644 drivers/misc/vc04_services/interface/vchi/vchi_mh.h
101 create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq.h
102 create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835.h
103 create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835_arm.c
104 create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.c
105 create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.h
106 create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_build_info.h
107 create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_cfg.h
108 create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.c
109 create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.h
110 create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.c
111 create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.h
112 create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_debugfs.c
113 create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_debugfs.h
114 create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_genversion
115 create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_if.h
116 create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_ioctl.h
117 create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_kern_lib.c
118 create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_memdrv.h
119 create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_pagelist.h
120 create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_shim.c
121 create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.c
122 create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.h
123 create mode 100644 drivers/misc/vc04_services/interface/vchiq_arm/vchiq_version.c
125 diff --git a/drivers/misc/Kconfig b/drivers/misc/Kconfig
126 index bbeb451..b6109a2 100644
127 --- a/drivers/misc/Kconfig
128 +++ b/drivers/misc/Kconfig
129 @@ -524,6 +524,7 @@ source "drivers/misc/carma/Kconfig"
130 source "drivers/misc/altera-stapl/Kconfig"
131 source "drivers/misc/mei/Kconfig"
132 source "drivers/misc/vmw_vmci/Kconfig"
133 +source "drivers/misc/vc04_services/Kconfig"
134 source "drivers/misc/mic/Kconfig"
135 source "drivers/misc/genwqe/Kconfig"
136 source "drivers/misc/echo/Kconfig"
137 diff --git a/drivers/misc/Makefile b/drivers/misc/Makefile
138 index 7d5c4cd..c085ede 100644
139 --- a/drivers/misc/Makefile
140 +++ b/drivers/misc/Makefile
141 @@ -51,6 +51,7 @@ obj-$(CONFIG_INTEL_MEI) += mei/
142 obj-$(CONFIG_VMWARE_VMCI) += vmw_vmci/
143 obj-$(CONFIG_LATTICE_ECP3_CONFIG) += lattice-ecp3-config.o
144 obj-$(CONFIG_SRAM) += sram.o
145 +obj-y += vc04_services/
147 obj-$(CONFIG_GENWQE) += genwqe/
148 obj-$(CONFIG_ECHO) += echo/
149 diff --git a/drivers/misc/vc04_services/Kconfig b/drivers/misc/vc04_services/Kconfig
151 index 0000000..2663933
153 +++ b/drivers/misc/vc04_services/Kconfig
155 +config BCM2708_VCHIQ
156 + tristate "Videocore VCHIQ"
157 + depends on MACH_BCM2708
160 + Kernel to VideoCore communication interface for the
161 + BCM2708 family of products.
162 + Defaults to Y when the Broadcom Videocore services
163 + are included in the build, N otherwise.
164 diff --git a/drivers/misc/vc04_services/Makefile b/drivers/misc/vc04_services/Makefile
166 index 0000000..0c82520
168 +++ b/drivers/misc/vc04_services/Makefile
170 +ifeq ($(CONFIG_MACH_BCM2708),y)
172 +obj-$(CONFIG_BCM2708_VCHIQ) += vchiq.o
175 + interface/vchiq_arm/vchiq_core.o \
176 + interface/vchiq_arm/vchiq_arm.o \
177 + interface/vchiq_arm/vchiq_kern_lib.o \
178 + interface/vchiq_arm/vchiq_2835_arm.o \
179 + interface/vchiq_arm/vchiq_debugfs.o \
180 + interface/vchiq_arm/vchiq_shim.o \
181 + interface/vchiq_arm/vchiq_util.o \
182 + interface/vchiq_arm/vchiq_connected.o \
184 +ccflags-y += -DVCOS_VERIFY_BKPTS=1 -Idrivers/misc/vc04_services -DUSE_VCHIQ_ARM -D__VCCOREVER__=0x04000000
187 diff --git a/drivers/misc/vc04_services/interface/vchi/connections/connection.h b/drivers/misc/vc04_services/interface/vchi/connections/connection.h
189 index 0000000..fef6ac3
191 +++ b/drivers/misc/vc04_services/interface/vchi/connections/connection.h
194 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
196 + * Redistribution and use in source and binary forms, with or without
197 + * modification, are permitted provided that the following conditions
199 + * 1. Redistributions of source code must retain the above copyright
200 + * notice, this list of conditions, and the following disclaimer,
201 + * without modification.
202 + * 2. Redistributions in binary form must reproduce the above copyright
203 + * notice, this list of conditions and the following disclaimer in the
204 + * documentation and/or other materials provided with the distribution.
205 + * 3. The names of the above-listed copyright holders may not be used
206 + * to endorse or promote products derived from this software without
207 + * specific prior written permission.
209 + * ALTERNATIVELY, this software may be distributed under the terms of the
210 + * GNU General Public License ("GPL") version 2, as published by the Free
211 + * Software Foundation.
213 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
214 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
215 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
216 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
217 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
218 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
219 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
220 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
221 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
222 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
223 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
226 +#ifndef CONNECTION_H_
227 +#define CONNECTION_H_
229 +#include <linux/kernel.h>
230 +#include <linux/types.h>
231 +#include <linux/semaphore.h>
233 +#include "interface/vchi/vchi_cfg_internal.h"
234 +#include "interface/vchi/vchi_common.h"
235 +#include "interface/vchi/message_drivers/message.h"
237 +/******************************************************************************
239 + *****************************************************************************/
241 +// Opaque handle for a connection / service pair
242 +typedef struct opaque_vchi_connection_connected_service_handle_t *VCHI_CONNECTION_SERVICE_HANDLE_T;
244 +// opaque handle to the connection state information
245 +typedef struct opaque_vchi_connection_info_t VCHI_CONNECTION_STATE_T;
247 +typedef struct vchi_connection_t VCHI_CONNECTION_T;
250 +/******************************************************************************
252 + *****************************************************************************/
254 +// Routine to init a connection with a particular low level driver
255 +typedef VCHI_CONNECTION_STATE_T * (*VCHI_CONNECTION_INIT_T)( struct vchi_connection_t * connection,
256 + const VCHI_MESSAGE_DRIVER_T * driver );
258 +// Routine to control CRC enabling at a connection level
259 +typedef int32_t (*VCHI_CONNECTION_CRC_CONTROL_T)( VCHI_CONNECTION_STATE_T *state_handle,
260 + VCHI_CRC_CONTROL_T control );
262 +// Routine to create a service
263 +typedef int32_t (*VCHI_CONNECTION_SERVICE_CONNECT_T)( VCHI_CONNECTION_STATE_T *state_handle,
264 + int32_t service_id,
265 + uint32_t rx_fifo_size,
266 + uint32_t tx_fifo_size,
268 + VCHI_CALLBACK_T callback,
269 + void *callback_param,
271 + int32_t want_unaligned_bulk_rx,
272 + int32_t want_unaligned_bulk_tx,
273 + VCHI_CONNECTION_SERVICE_HANDLE_T *service_handle );
275 +// Routine to close a service
276 +typedef int32_t (*VCHI_CONNECTION_SERVICE_DISCONNECT_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle );
278 +// Routine to queue a message
279 +typedef int32_t (*VCHI_CONNECTION_SERVICE_QUEUE_MESSAGE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
281 + uint32_t data_size,
282 + VCHI_FLAGS_T flags,
283 + void *msg_handle );
285 +// scatter-gather (vector) message queueing
286 +typedef int32_t (*VCHI_CONNECTION_SERVICE_QUEUE_MESSAGEV_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
287 + VCHI_MSG_VECTOR_T *vector,
289 + VCHI_FLAGS_T flags,
290 + void *msg_handle );
292 +// Routine to dequeue a message
293 +typedef int32_t (*VCHI_CONNECTION_SERVICE_DEQUEUE_MESSAGE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
295 + uint32_t max_data_size_to_read,
296 + uint32_t *actual_msg_size,
297 + VCHI_FLAGS_T flags );
299 +// Routine to peek at a message
300 +typedef int32_t (*VCHI_CONNECTION_SERVICE_PEEK_MESSAGE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
302 + uint32_t *msg_size,
303 + VCHI_FLAGS_T flags );
305 +// Routine to hold a message
306 +typedef int32_t (*VCHI_CONNECTION_SERVICE_HOLD_MESSAGE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
308 + uint32_t *msg_size,
309 + VCHI_FLAGS_T flags,
310 + void **message_handle );
312 +// Routine to initialise a received message iterator
313 +typedef int32_t (*VCHI_CONNECTION_SERVICE_LOOKAHEAD_MESSAGE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
314 + VCHI_MSG_ITER_T *iter,
315 + VCHI_FLAGS_T flags );
317 +// Routine to release a held message
318 +typedef int32_t (*VCHI_CONNECTION_HELD_MSG_RELEASE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
319 + void *message_handle );
321 +// Routine to get info on a held message
322 +typedef int32_t (*VCHI_CONNECTION_HELD_MSG_INFO_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
323 + void *message_handle,
326 + uint32_t *tx_timestamp,
327 + uint32_t *rx_timestamp );
329 +// Routine to check whether the iterator has a next message
330 +typedef int32_t (*VCHI_CONNECTION_MSG_ITER_HAS_NEXT_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service,
331 + const VCHI_MSG_ITER_T *iter );
333 +// Routine to advance the iterator
334 +typedef int32_t (*VCHI_CONNECTION_MSG_ITER_NEXT_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service,
335 + VCHI_MSG_ITER_T *iter,
337 + uint32_t *msg_size );
339 +// Routine to remove the last message returned by the iterator
340 +typedef int32_t (*VCHI_CONNECTION_MSG_ITER_REMOVE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service,
341 + VCHI_MSG_ITER_T *iter );
343 +// Routine to hold the last message returned by the iterator
344 +typedef int32_t (*VCHI_CONNECTION_MSG_ITER_HOLD_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service,
345 + VCHI_MSG_ITER_T *iter,
346 + void **msg_handle );
348 +// Routine to transmit bulk data
349 +typedef int32_t (*VCHI_CONNECTION_BULK_QUEUE_TRANSMIT_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
350 + const void *data_src,
351 + uint32_t data_size,
352 + VCHI_FLAGS_T flags,
353 + void *bulk_handle );
355 +// Routine to receive data
356 +typedef int32_t (*VCHI_CONNECTION_BULK_QUEUE_RECEIVE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
358 + uint32_t data_size,
359 + VCHI_FLAGS_T flags,
360 + void *bulk_handle );
362 +// Routine to report if a server is available
363 +typedef int32_t (*VCHI_CONNECTION_SERVER_PRESENT)( VCHI_CONNECTION_STATE_T *state, int32_t service_id, int32_t peer_flags );
365 +// Routine to report the number of RX slots available
366 +typedef int (*VCHI_CONNECTION_RX_SLOTS_AVAILABLE)( const VCHI_CONNECTION_STATE_T *state );
368 +// Routine to report the RX slot size
369 +typedef uint32_t (*VCHI_CONNECTION_RX_SLOT_SIZE)( const VCHI_CONNECTION_STATE_T *state );
371 +// Callback to indicate that the other side has added a buffer to the rx bulk DMA FIFO
372 +typedef void (*VCHI_CONNECTION_RX_BULK_BUFFER_ADDED)(VCHI_CONNECTION_STATE_T *state,
375 + MESSAGE_TX_CHANNEL_T channel,
376 + uint32_t channel_params,
377 + uint32_t data_length,
378 + uint32_t data_offset);
380 +// Callback to inform a service that a Xon or Xoff message has been received
381 +typedef void (*VCHI_CONNECTION_FLOW_CONTROL)(VCHI_CONNECTION_STATE_T *state, int32_t service_id, int32_t xoff);
383 +// Callback to inform a service that a server available reply message has been received
384 +typedef void (*VCHI_CONNECTION_SERVER_AVAILABLE_REPLY)(VCHI_CONNECTION_STATE_T *state, int32_t service_id, uint32_t flags);
386 +// Callback to indicate that bulk auxiliary messages have arrived
387 +typedef void (*VCHI_CONNECTION_BULK_AUX_RECEIVED)(VCHI_CONNECTION_STATE_T *state);
389 +// Callback to indicate that bulk auxiliary messages have arrived
390 +typedef void (*VCHI_CONNECTION_BULK_AUX_TRANSMITTED)(VCHI_CONNECTION_STATE_T *state, void *handle);
392 +// Callback with all the connection info you require
393 +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);
395 +// Callback to inform of a disconnect
396 +typedef void (*VCHI_CONNECTION_DISCONNECT)(VCHI_CONNECTION_STATE_T *state, uint32_t flags);
398 +// Callback to inform of a power control request
399 +typedef void (*VCHI_CONNECTION_POWER_CONTROL)(VCHI_CONNECTION_STATE_T *state, MESSAGE_TX_CHANNEL_T channel, int32_t enable);
401 +// allocate memory suitably aligned for this connection
402 +typedef void * (*VCHI_BUFFER_ALLOCATE)(VCHI_CONNECTION_SERVICE_HANDLE_T service_handle, uint32_t * length);
404 +// free memory allocated by buffer_allocate
405 +typedef void (*VCHI_BUFFER_FREE)(VCHI_CONNECTION_SERVICE_HANDLE_T service_handle, void * address);
408 +/******************************************************************************
409 + System driver struct
410 + *****************************************************************************/
412 +struct opaque_vchi_connection_api_t
414 + // Routine to init the connection
415 + VCHI_CONNECTION_INIT_T init;
417 + // Connection-level CRC control
418 + VCHI_CONNECTION_CRC_CONTROL_T crc_control;
420 + // Routine to connect to or create service
421 + VCHI_CONNECTION_SERVICE_CONNECT_T service_connect;
423 + // Routine to disconnect from a service
424 + VCHI_CONNECTION_SERVICE_DISCONNECT_T service_disconnect;
426 + // Routine to queue a message
427 + VCHI_CONNECTION_SERVICE_QUEUE_MESSAGE_T service_queue_msg;
429 + // scatter-gather (vector) message queue
430 + VCHI_CONNECTION_SERVICE_QUEUE_MESSAGEV_T service_queue_msgv;
432 + // Routine to dequeue a message
433 + VCHI_CONNECTION_SERVICE_DEQUEUE_MESSAGE_T service_dequeue_msg;
435 + // Routine to peek at a message
436 + VCHI_CONNECTION_SERVICE_PEEK_MESSAGE_T service_peek_msg;
438 + // Routine to hold a message
439 + VCHI_CONNECTION_SERVICE_HOLD_MESSAGE_T service_hold_msg;
441 + // Routine to initialise a received message iterator
442 + VCHI_CONNECTION_SERVICE_LOOKAHEAD_MESSAGE_T service_look_ahead_msg;
444 + // Routine to release a message
445 + VCHI_CONNECTION_HELD_MSG_RELEASE_T held_msg_release;
447 + // Routine to get information on a held message
448 + VCHI_CONNECTION_HELD_MSG_INFO_T held_msg_info;
450 + // Routine to check for next message on iterator
451 + VCHI_CONNECTION_MSG_ITER_HAS_NEXT_T msg_iter_has_next;
453 + // Routine to get next message on iterator
454 + VCHI_CONNECTION_MSG_ITER_NEXT_T msg_iter_next;
456 + // Routine to remove the last message returned by iterator
457 + VCHI_CONNECTION_MSG_ITER_REMOVE_T msg_iter_remove;
459 + // Routine to hold the last message returned by iterator
460 + VCHI_CONNECTION_MSG_ITER_HOLD_T msg_iter_hold;
462 + // Routine to transmit bulk data
463 + VCHI_CONNECTION_BULK_QUEUE_TRANSMIT_T bulk_queue_transmit;
465 + // Routine to receive data
466 + VCHI_CONNECTION_BULK_QUEUE_RECEIVE_T bulk_queue_receive;
468 + // Routine to report the available servers
469 + VCHI_CONNECTION_SERVER_PRESENT server_present;
471 + // Routine to report the number of RX slots available
472 + VCHI_CONNECTION_RX_SLOTS_AVAILABLE connection_rx_slots_available;
474 + // Routine to report the RX slot size
475 + VCHI_CONNECTION_RX_SLOT_SIZE connection_rx_slot_size;
477 + // Callback to indicate that the other side has added a buffer to the rx bulk DMA FIFO
478 + VCHI_CONNECTION_RX_BULK_BUFFER_ADDED rx_bulk_buffer_added;
480 + // Callback to inform a service that a Xon or Xoff message has been received
481 + VCHI_CONNECTION_FLOW_CONTROL flow_control;
483 + // Callback to inform a service that a server available reply message has been received
484 + VCHI_CONNECTION_SERVER_AVAILABLE_REPLY server_available_reply;
486 + // Callback to indicate that bulk auxiliary messages have arrived
487 + VCHI_CONNECTION_BULK_AUX_RECEIVED bulk_aux_received;
489 + // Callback to indicate that a bulk auxiliary message has been transmitted
490 + VCHI_CONNECTION_BULK_AUX_TRANSMITTED bulk_aux_transmitted;
492 + // Callback to provide information about the connection
493 + VCHI_CONNECTION_INFO connection_info;
495 + // Callback to notify that peer has requested disconnect
496 + VCHI_CONNECTION_DISCONNECT disconnect;
498 + // Callback to notify that peer has requested power change
499 + VCHI_CONNECTION_POWER_CONTROL power_control;
501 + // allocate memory suitably aligned for this connection
502 + VCHI_BUFFER_ALLOCATE buffer_allocate;
504 + // free memory allocated by buffer_allocate
505 + VCHI_BUFFER_FREE buffer_free;
509 +struct vchi_connection_t {
510 + const VCHI_CONNECTION_API_T *api;
511 + VCHI_CONNECTION_STATE_T *state;
512 +#ifdef VCHI_COARSE_LOCKING
513 + struct semaphore sem;
518 +#endif /* CONNECTION_H_ */
520 +/****************************** End of file **********************************/
521 diff --git a/drivers/misc/vc04_services/interface/vchi/message_drivers/message.h b/drivers/misc/vc04_services/interface/vchi/message_drivers/message.h
523 index 0000000..8b3f767
525 +++ b/drivers/misc/vc04_services/interface/vchi/message_drivers/message.h
528 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
530 + * Redistribution and use in source and binary forms, with or without
531 + * modification, are permitted provided that the following conditions
533 + * 1. Redistributions of source code must retain the above copyright
534 + * notice, this list of conditions, and the following disclaimer,
535 + * without modification.
536 + * 2. Redistributions in binary form must reproduce the above copyright
537 + * notice, this list of conditions and the following disclaimer in the
538 + * documentation and/or other materials provided with the distribution.
539 + * 3. The names of the above-listed copyright holders may not be used
540 + * to endorse or promote products derived from this software without
541 + * specific prior written permission.
543 + * ALTERNATIVELY, this software may be distributed under the terms of the
544 + * GNU General Public License ("GPL") version 2, as published by the Free
545 + * Software Foundation.
547 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
548 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
549 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
550 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
551 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
552 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
553 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
554 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
555 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
556 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
557 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
560 +#ifndef _VCHI_MESSAGE_H_
561 +#define _VCHI_MESSAGE_H_
563 +#include <linux/kernel.h>
564 +#include <linux/types.h>
565 +#include <linux/semaphore.h>
567 +#include "interface/vchi/vchi_cfg_internal.h"
568 +#include "interface/vchi/vchi_common.h"
571 +typedef enum message_event_type {
572 + MESSAGE_EVENT_NONE,
574 + MESSAGE_EVENT_MESSAGE,
575 + MESSAGE_EVENT_SLOT_COMPLETE,
576 + MESSAGE_EVENT_RX_BULK_PAUSED,
577 + MESSAGE_EVENT_RX_BULK_COMPLETE,
578 + MESSAGE_EVENT_TX_COMPLETE,
579 + MESSAGE_EVENT_MSG_DISCARDED
580 +} MESSAGE_EVENT_TYPE_T;
582 +typedef enum vchi_msg_flags
584 + VCHI_MSG_FLAGS_NONE = 0x0,
585 + VCHI_MSG_FLAGS_TERMINATE_DMA = 0x1
588 +typedef enum message_tx_channel
590 + MESSAGE_TX_CHANNEL_MESSAGE = 0,
591 + MESSAGE_TX_CHANNEL_BULK = 1 // drivers may provide multiple bulk channels, from 1 upwards
592 +} MESSAGE_TX_CHANNEL_T;
594 +// Macros used for cycling through bulk channels
595 +#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)
596 +#define MESSAGE_TX_CHANNEL_BULK_NEXT(c) (MESSAGE_TX_CHANNEL_BULK+((c)-MESSAGE_TX_CHANNEL_BULK+1)%VCHI_MAX_BULK_TX_CHANNELS_PER_CONNECTION)
598 +typedef enum message_rx_channel
600 + MESSAGE_RX_CHANNEL_MESSAGE = 0,
601 + MESSAGE_RX_CHANNEL_BULK = 1 // drivers may provide multiple bulk channels, from 1 upwards
602 +} MESSAGE_RX_CHANNEL_T;
604 +// Message receive slot information
605 +typedef struct rx_msg_slot_info {
607 + struct rx_msg_slot_info *next;
608 + //struct slot_info *prev;
609 +#if !defined VCHI_COARSE_LOCKING
610 + struct semaphore sem;
613 + uint8_t *addr; // base address of slot
614 + uint32_t len; // length of slot in bytes
616 + uint32_t write_ptr; // hardware causes this to advance
617 + uint32_t read_ptr; // this module does the reading
618 + int active; // is this slot in the hardware dma fifo?
619 + uint32_t msgs_parsed; // count how many messages are in this slot
620 + uint32_t msgs_released; // how many messages have been released
621 + void *state; // connection state information
622 + uint8_t ref_count[VCHI_MAX_SERVICES_PER_CONNECTION]; // reference count for slots held by services
623 +} RX_MSG_SLOTINFO_T;
625 +// The message driver no longer needs to know about the fields of RX_BULK_SLOTINFO_T - sort this out.
626 +// In particular, it mustn't use addr and len - they're the client buffer, but the message
627 +// driver will be tasked with sending the aligned core section.
628 +typedef struct rx_bulk_slotinfo_t {
629 + struct rx_bulk_slotinfo_t *next;
631 + struct semaphore *blocking;
637 + // needed for the callback
640 + VCHI_FLAGS_T flags;
641 +} RX_BULK_SLOTINFO_T;
644 +/* ----------------------------------------------------------------------
645 + * each connection driver will have a pool of the following struct.
647 + * the pool will be managed by vchi_qman_*
648 + * this means there will be multiple queues (single linked lists)
649 + * a given struct message_info will be on exactly one of these queues
651 + * -------------------------------------------------------------------- */
652 +typedef struct rx_message_info {
654 + struct message_info *next;
655 + //struct message_info *prev;
659 + RX_MSG_SLOTINFO_T *slot; // points to whichever slot contains this message
660 + uint32_t tx_timestamp;
661 + uint32_t rx_timestamp;
663 +} RX_MESSAGE_INFO_T;
666 + MESSAGE_EVENT_TYPE_T type;
670 + void *addr; // address of message
671 + uint16_t slot_delta; // whether this message indicated slot delta
672 + uint32_t len; // length of message
673 + RX_MSG_SLOTINFO_T *slot; // slot this message is in
674 + int32_t service; // service id this message is destined for
675 + uint32_t tx_timestamp; // timestamp from the header
676 + uint32_t rx_timestamp; // timestamp when we parsed it
679 + // FIXME: cleanup slot reporting...
680 + RX_MSG_SLOTINFO_T *rx_msg;
681 + RX_BULK_SLOTINFO_T *rx_bulk;
683 + MESSAGE_TX_CHANNEL_T tx_channel;
689 +typedef void VCHI_MESSAGE_DRIVER_EVENT_CALLBACK_T( void *state );
692 + VCHI_MESSAGE_DRIVER_EVENT_CALLBACK_T *event_callback;
693 +} VCHI_MESSAGE_DRIVER_OPEN_T;
696 +// handle to this instance of message driver (as returned by ->open)
697 +typedef struct opaque_mhandle_t *VCHI_MDRIVER_HANDLE_T;
699 +struct opaque_vchi_message_driver_t {
700 + VCHI_MDRIVER_HANDLE_T *(*open)( VCHI_MESSAGE_DRIVER_OPEN_T *params, void *state );
701 + int32_t (*suspending)( VCHI_MDRIVER_HANDLE_T *handle );
702 + int32_t (*resumed)( VCHI_MDRIVER_HANDLE_T *handle );
703 + int32_t (*power_control)( VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_TX_CHANNEL_T, int32_t enable );
704 + int32_t (*add_msg_rx_slot)( VCHI_MDRIVER_HANDLE_T *handle, RX_MSG_SLOTINFO_T *slot ); // rx message
705 + int32_t (*add_bulk_rx)( VCHI_MDRIVER_HANDLE_T *handle, void *data, uint32_t len, RX_BULK_SLOTINFO_T *slot ); // rx data (bulk)
706 + 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)
707 + void (*next_event)( VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_EVENT_T *event ); // get the next event from message_driver
708 + int32_t (*enable)( VCHI_MDRIVER_HANDLE_T *handle );
709 + int32_t (*form_message)( VCHI_MDRIVER_HANDLE_T *handle, int32_t service_id, VCHI_MSG_VECTOR_T *vector, uint32_t count, void
710 + *address, uint32_t length_avail, uint32_t max_total_length, int32_t pad_to_fill, int32_t allow_partial );
712 + int32_t (*update_message)( VCHI_MDRIVER_HANDLE_T *handle, void *dest, int16_t *slot_count );
713 + int32_t (*buffer_aligned)( VCHI_MDRIVER_HANDLE_T *handle, int tx, int uncached, const void *address, const uint32_t length );
714 + void * (*allocate_buffer)( VCHI_MDRIVER_HANDLE_T *handle, uint32_t *length );
715 + void (*free_buffer)( VCHI_MDRIVER_HANDLE_T *handle, void *address );
716 + int (*rx_slot_size)( VCHI_MDRIVER_HANDLE_T *handle, int msg_size );
717 + int (*tx_slot_size)( VCHI_MDRIVER_HANDLE_T *handle, int msg_size );
719 + int32_t (*tx_supports_terminate)( const VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_TX_CHANNEL_T channel );
720 + uint32_t (*tx_bulk_chunk_size)( const VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_TX_CHANNEL_T channel );
721 + int (*tx_alignment)( const VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_TX_CHANNEL_T channel );
722 + int (*rx_alignment)( const VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_RX_CHANNEL_T channel );
723 + 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 );
724 + void (*debug)( VCHI_MDRIVER_HANDLE_T *handle );
728 +#endif // _VCHI_MESSAGE_H_
730 +/****************************** End of file ***********************************/
731 diff --git a/drivers/misc/vc04_services/interface/vchi/vchi.h b/drivers/misc/vc04_services/interface/vchi/vchi.h
733 index 0000000..1b17e98
735 +++ b/drivers/misc/vc04_services/interface/vchi/vchi.h
738 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
740 + * Redistribution and use in source and binary forms, with or without
741 + * modification, are permitted provided that the following conditions
743 + * 1. Redistributions of source code must retain the above copyright
744 + * notice, this list of conditions, and the following disclaimer,
745 + * without modification.
746 + * 2. Redistributions in binary form must reproduce the above copyright
747 + * notice, this list of conditions and the following disclaimer in the
748 + * documentation and/or other materials provided with the distribution.
749 + * 3. The names of the above-listed copyright holders may not be used
750 + * to endorse or promote products derived from this software without
751 + * specific prior written permission.
753 + * ALTERNATIVELY, this software may be distributed under the terms of the
754 + * GNU General Public License ("GPL") version 2, as published by the Free
755 + * Software Foundation.
757 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
758 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
759 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
760 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
761 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
762 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
763 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
764 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
765 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
766 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
767 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
773 +#include "interface/vchi/vchi_cfg.h"
774 +#include "interface/vchi/vchi_common.h"
775 +#include "interface/vchi/connections/connection.h"
776 +#include "vchi_mh.h"
779 +/******************************************************************************
781 + *****************************************************************************/
783 +#define VCHI_BULK_ROUND_UP(x) ((((unsigned long)(x))+VCHI_BULK_ALIGN-1) & ~(VCHI_BULK_ALIGN-1))
784 +#define VCHI_BULK_ROUND_DOWN(x) (((unsigned long)(x)) & ~(VCHI_BULK_ALIGN-1))
785 +#define VCHI_BULK_ALIGN_NBYTES(x) (VCHI_BULK_ALIGNED(x) ? 0 : (VCHI_BULK_ALIGN - ((unsigned long)(x) & (VCHI_BULK_ALIGN-1))))
787 +#ifdef USE_VCHIQ_ARM
788 +#define VCHI_BULK_ALIGNED(x) 1
790 +#define VCHI_BULK_ALIGNED(x) (((unsigned long)(x) & (VCHI_BULK_ALIGN-1)) == 0)
793 +struct vchi_version {
795 + uint32_t version_min;
797 +#define VCHI_VERSION(v_) { v_, v_ }
798 +#define VCHI_VERSION_EX(v_, m_) { v_, m_ }
805 +} VCHI_MSG_VECTOR_TYPE_T;
807 +typedef struct vchi_msg_vector_ex {
809 + VCHI_MSG_VECTOR_TYPE_T type;
815 + VCHI_MEM_HANDLE_T handle;
820 + // an ordinary data pointer
823 + const void *vec_base;
827 + // a nested vector list
830 + struct vchi_msg_vector_ex *vec;
834 +} VCHI_MSG_VECTOR_EX_T;
837 +// Construct an entry in a msg vector for a pointer (p) of length (l)
838 +#define VCHI_VEC_POINTER(p,l) VCHI_VEC_POINTER, { { (VCHI_MEM_HANDLE_T)(p), (l) } }
840 +// Construct an entry in a msg vector for a message handle (h), starting at offset (o) of length (l)
841 +#define VCHI_VEC_HANDLE(h,o,l) VCHI_VEC_HANDLE, { { (h), (o), (l) } }
843 +// Macros to manipulate 'FOURCC' values
844 +#define MAKE_FOURCC(x) ((int32_t)( (x[0] << 24) | (x[1] << 16) | (x[2] << 8) | x[3] ))
845 +#define FOURCC_TO_CHAR(x) (x >> 24) & 0xFF,(x >> 16) & 0xFF,(x >> 8) & 0xFF, x & 0xFF
848 +// Opaque service information
849 +struct opaque_vchi_service_t;
851 +// Descriptor for a held message. Allocated by client, initialised by vchi_msg_hold,
852 +// vchi_msg_iter_hold or vchi_msg_iter_hold_next. Fields are for internal VCHI use only.
855 + struct opaque_vchi_service_t *service;
861 +// structure used to provide the information needed to open a server or a client
863 + struct vchi_version version;
864 + int32_t service_id;
865 + VCHI_CONNECTION_T *connection;
866 + uint32_t rx_fifo_size;
867 + uint32_t tx_fifo_size;
868 + VCHI_CALLBACK_T callback;
869 + void *callback_param;
870 + /* client intends to receive bulk transfers of
871 + odd lengths or into unaligned buffers */
872 + int32_t want_unaligned_bulk_rx;
873 + /* client intends to transmit bulk transfers of
874 + odd lengths or out of unaligned buffers */
875 + int32_t want_unaligned_bulk_tx;
876 + /* client wants to check CRCs on (bulk) xfers.
877 + Only needs to be set at 1 end - will do both directions. */
879 +} SERVICE_CREATION_T;
881 +// Opaque handle for a VCHI instance
882 +typedef struct opaque_vchi_instance_handle_t *VCHI_INSTANCE_T;
884 +// Opaque handle for a server or client
885 +typedef struct opaque_vchi_service_handle_t *VCHI_SERVICE_HANDLE_T;
887 +// Service registration & startup
888 +typedef void (*VCHI_SERVICE_INIT)(VCHI_INSTANCE_T initialise_instance, VCHI_CONNECTION_T **connections, uint32_t num_connections);
890 +typedef struct service_info_tag {
891 + const char * const vll_filename; /* VLL to load to start this service. This is an empty string if VLL is "static" */
892 + VCHI_SERVICE_INIT init; /* Service initialisation function */
893 + void *vll_handle; /* VLL handle; NULL when unloaded or a "static VLL" in build */
896 +/******************************************************************************
897 + Global funcs - implementation is specific to which side you are on (local / remote)
898 + *****************************************************************************/
904 +extern /*@observer@*/ VCHI_CONNECTION_T * vchi_create_connection( const VCHI_CONNECTION_API_T * function_table,
905 + const VCHI_MESSAGE_DRIVER_T * low_level);
908 +// Routine used to initialise the vchi on both local + remote connections
909 +extern int32_t vchi_initialise( VCHI_INSTANCE_T *instance_handle );
911 +extern int32_t vchi_exit( void );
913 +extern int32_t vchi_connect( VCHI_CONNECTION_T **connections,
914 + const uint32_t num_connections,
915 + VCHI_INSTANCE_T instance_handle );
917 +//When this is called, ensure that all services have no data pending.
918 +//Bulk transfers can remain 'queued'
919 +extern int32_t vchi_disconnect( VCHI_INSTANCE_T instance_handle );
921 +// Global control over bulk CRC checking
922 +extern int32_t vchi_crc_control( VCHI_CONNECTION_T *connection,
923 + VCHI_CRC_CONTROL_T control );
926 +extern void * vchi_allocate_buffer(VCHI_SERVICE_HANDLE_T handle, uint32_t *length);
927 +extern void vchi_free_buffer(VCHI_SERVICE_HANDLE_T handle, void *address);
928 +extern uint32_t vchi_current_time(VCHI_INSTANCE_T instance_handle);
931 +/******************************************************************************
933 + *****************************************************************************/
934 +// Routine to create a named service
935 +extern int32_t vchi_service_create( VCHI_INSTANCE_T instance_handle,
936 + SERVICE_CREATION_T *setup,
937 + VCHI_SERVICE_HANDLE_T *handle );
939 +// Routine to destory a service
940 +extern int32_t vchi_service_destroy( const VCHI_SERVICE_HANDLE_T handle );
942 +// Routine to open a named service
943 +extern int32_t vchi_service_open( VCHI_INSTANCE_T instance_handle,
944 + SERVICE_CREATION_T *setup,
945 + VCHI_SERVICE_HANDLE_T *handle);
947 +extern int32_t vchi_get_peer_version( const VCHI_SERVICE_HANDLE_T handle,
948 + short *peer_version );
950 +// Routine to close a named service
951 +extern int32_t vchi_service_close( const VCHI_SERVICE_HANDLE_T handle );
953 +// Routine to increment ref count on a named service
954 +extern int32_t vchi_service_use( const VCHI_SERVICE_HANDLE_T handle );
956 +// Routine to decrement ref count on a named service
957 +extern int32_t vchi_service_release( const VCHI_SERVICE_HANDLE_T handle );
959 +// Routine to set a control option for a named service
960 +extern int32_t vchi_service_set_option( const VCHI_SERVICE_HANDLE_T handle,
961 + VCHI_SERVICE_OPTION_T option,
964 +// Routine to send a message across a service
965 +extern int32_t vchi_msg_queue( VCHI_SERVICE_HANDLE_T handle,
967 + uint32_t data_size,
968 + VCHI_FLAGS_T flags,
969 + void *msg_handle );
971 +// scatter-gather (vector) and send message
972 +int32_t vchi_msg_queuev_ex( VCHI_SERVICE_HANDLE_T handle,
973 + VCHI_MSG_VECTOR_EX_T *vector,
975 + VCHI_FLAGS_T flags,
976 + void *msg_handle );
978 +// legacy scatter-gather (vector) and send message, only handles pointers
979 +int32_t vchi_msg_queuev( VCHI_SERVICE_HANDLE_T handle,
980 + VCHI_MSG_VECTOR_T *vector,
982 + VCHI_FLAGS_T flags,
983 + void *msg_handle );
985 +// Routine to receive a msg from a service
986 +// Dequeue is equivalent to hold, copy into client buffer, release
987 +extern int32_t vchi_msg_dequeue( VCHI_SERVICE_HANDLE_T handle,
989 + uint32_t max_data_size_to_read,
990 + uint32_t *actual_msg_size,
991 + VCHI_FLAGS_T flags );
993 +// Routine to look at a message in place.
994 +// The message is not dequeued, so a subsequent call to peek or dequeue
995 +// will return the same message.
996 +extern int32_t vchi_msg_peek( VCHI_SERVICE_HANDLE_T handle,
998 + uint32_t *msg_size,
999 + VCHI_FLAGS_T flags );
1001 +// Routine to remove a message after it has been read in place with peek
1002 +// The first message on the queue is dequeued.
1003 +extern int32_t vchi_msg_remove( VCHI_SERVICE_HANDLE_T handle );
1005 +// Routine to look at a message in place.
1006 +// The message is dequeued, so the caller is left holding it; the descriptor is
1007 +// filled in and must be released when the user has finished with the message.
1008 +extern int32_t vchi_msg_hold( VCHI_SERVICE_HANDLE_T handle,
1009 + void **data, // } may be NULL, as info can be
1010 + uint32_t *msg_size, // } obtained from HELD_MSG_T
1011 + VCHI_FLAGS_T flags,
1012 + VCHI_HELD_MSG_T *message_descriptor );
1014 +// Initialise an iterator to look through messages in place
1015 +extern int32_t vchi_msg_look_ahead( VCHI_SERVICE_HANDLE_T handle,
1016 + VCHI_MSG_ITER_T *iter,
1017 + VCHI_FLAGS_T flags );
1019 +/******************************************************************************
1020 + Global service support API - operations on held messages and message iterators
1021 + *****************************************************************************/
1023 +// Routine to get the address of a held message
1024 +extern void *vchi_held_msg_ptr( const VCHI_HELD_MSG_T *message );
1026 +// Routine to get the size of a held message
1027 +extern int32_t vchi_held_msg_size( const VCHI_HELD_MSG_T *message );
1029 +// Routine to get the transmit timestamp as written into the header by the peer
1030 +extern uint32_t vchi_held_msg_tx_timestamp( const VCHI_HELD_MSG_T *message );
1032 +// Routine to get the reception timestamp, written as we parsed the header
1033 +extern uint32_t vchi_held_msg_rx_timestamp( const VCHI_HELD_MSG_T *message );
1035 +// Routine to release a held message after it has been processed
1036 +extern int32_t vchi_held_msg_release( VCHI_HELD_MSG_T *message );
1038 +// Indicates whether the iterator has a next message.
1039 +extern int32_t vchi_msg_iter_has_next( const VCHI_MSG_ITER_T *iter );
1041 +// Return the pointer and length for the next message and advance the iterator.
1042 +extern int32_t vchi_msg_iter_next( VCHI_MSG_ITER_T *iter,
1044 + uint32_t *msg_size );
1046 +// Remove the last message returned by vchi_msg_iter_next.
1047 +// Can only be called once after each call to vchi_msg_iter_next.
1048 +extern int32_t vchi_msg_iter_remove( VCHI_MSG_ITER_T *iter );
1050 +// Hold the last message returned by vchi_msg_iter_next.
1051 +// Can only be called once after each call to vchi_msg_iter_next.
1052 +extern int32_t vchi_msg_iter_hold( VCHI_MSG_ITER_T *iter,
1053 + VCHI_HELD_MSG_T *message );
1055 +// Return information for the next message, and hold it, advancing the iterator.
1056 +extern int32_t vchi_msg_iter_hold_next( VCHI_MSG_ITER_T *iter,
1057 + void **data, // } may be NULL
1058 + uint32_t *msg_size, // }
1059 + VCHI_HELD_MSG_T *message );
1062 +/******************************************************************************
1064 + *****************************************************************************/
1066 +// Routine to prepare interface for a transfer from the other side
1067 +extern int32_t vchi_bulk_queue_receive( VCHI_SERVICE_HANDLE_T handle,
1069 + uint32_t data_size,
1070 + VCHI_FLAGS_T flags,
1071 + void *transfer_handle );
1074 +// Prepare interface for a transfer from the other side into relocatable memory.
1075 +int32_t vchi_bulk_queue_receive_reloc( const VCHI_SERVICE_HANDLE_T handle,
1076 + VCHI_MEM_HANDLE_T h_dst,
1078 + uint32_t data_size,
1079 + const VCHI_FLAGS_T flags,
1080 + void * const bulk_handle );
1082 +// Routine to queue up data ready for transfer to the other (once they have signalled they are ready)
1083 +extern int32_t vchi_bulk_queue_transmit( VCHI_SERVICE_HANDLE_T handle,
1084 + const void *data_src,
1085 + uint32_t data_size,
1086 + VCHI_FLAGS_T flags,
1087 + void *transfer_handle );
1090 +/******************************************************************************
1091 + Configuration plumbing
1092 + *****************************************************************************/
1094 +// function prototypes for the different mid layers (the state info gives the different physical connections)
1095 +extern const VCHI_CONNECTION_API_T *single_get_func_table( void );
1096 +//extern const VCHI_CONNECTION_API_T *local_server_get_func_table( void );
1097 +//extern const VCHI_CONNECTION_API_T *local_client_get_func_table( void );
1099 +// declare all message drivers here
1100 +const VCHI_MESSAGE_DRIVER_T *vchi_mphi_message_driver_func_table( void );
1106 +extern int32_t vchi_bulk_queue_transmit_reloc( VCHI_SERVICE_HANDLE_T handle,
1107 + VCHI_MEM_HANDLE_T h_src,
1109 + uint32_t data_size,
1110 + VCHI_FLAGS_T flags,
1111 + void *transfer_handle );
1112 +#endif /* VCHI_H_ */
1114 +/****************************** End of file **********************************/
1115 diff --git a/drivers/misc/vc04_services/interface/vchi/vchi_cfg.h b/drivers/misc/vc04_services/interface/vchi/vchi_cfg.h
1116 new file mode 100644
1117 index 0000000..26bc2d3
1119 +++ b/drivers/misc/vc04_services/interface/vchi/vchi_cfg.h
1122 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
1124 + * Redistribution and use in source and binary forms, with or without
1125 + * modification, are permitted provided that the following conditions
1127 + * 1. Redistributions of source code must retain the above copyright
1128 + * notice, this list of conditions, and the following disclaimer,
1129 + * without modification.
1130 + * 2. Redistributions in binary form must reproduce the above copyright
1131 + * notice, this list of conditions and the following disclaimer in the
1132 + * documentation and/or other materials provided with the distribution.
1133 + * 3. The names of the above-listed copyright holders may not be used
1134 + * to endorse or promote products derived from this software without
1135 + * specific prior written permission.
1137 + * ALTERNATIVELY, this software may be distributed under the terms of the
1138 + * GNU General Public License ("GPL") version 2, as published by the Free
1139 + * Software Foundation.
1141 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
1142 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
1143 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1144 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
1145 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
1146 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
1147 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
1148 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
1149 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
1150 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
1151 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1154 +#ifndef VCHI_CFG_H_
1155 +#define VCHI_CFG_H_
1157 +/****************************************************************************************
1158 + * Defines in this first section are part of the VCHI API and may be examined by VCHI
1160 + ***************************************************************************************/
1162 +/* Required alignment of base addresses for bulk transfer, if unaligned transfers are not enabled */
1163 +/* Really determined by the message driver, and should be available from a run-time call. */
1164 +#ifndef VCHI_BULK_ALIGN
1165 +# if __VCCOREVER__ >= 0x04000000
1166 +# define VCHI_BULK_ALIGN 32 // Allows for the need to do cache cleans
1168 +# define VCHI_BULK_ALIGN 16
1172 +/* Required length multiple for bulk transfers, if unaligned transfers are not enabled */
1173 +/* May be less than or greater than VCHI_BULK_ALIGN */
1174 +/* Really determined by the message driver, and should be available from a run-time call. */
1175 +#ifndef VCHI_BULK_GRANULARITY
1176 +# if __VCCOREVER__ >= 0x04000000
1177 +# define VCHI_BULK_GRANULARITY 32 // Allows for the need to do cache cleans
1179 +# define VCHI_BULK_GRANULARITY 16
1183 +/* The largest possible message to be queued with vchi_msg_queue. */
1184 +#ifndef VCHI_MAX_MSG_SIZE
1185 +# if defined VCHI_LOCAL_HOST_PORT
1186 +# define VCHI_MAX_MSG_SIZE 16384 // makes file transfers fast, but should they be using bulk?
1188 +# define VCHI_MAX_MSG_SIZE 4096 // NOTE: THIS MUST BE LARGER THAN OR EQUAL TO THE SIZE OF THE KHRONOS MERGE BUFFER!!
1192 +/******************************************************************************************
1193 + * Defines below are system configuration options, and should not be used by VCHI services.
1194 + *****************************************************************************************/
1196 +/* How many connections can we support? A localhost implementation uses 2 connections,
1197 + * 1 for host-app, 1 for VMCS, and these are hooked together by a loopback MPHI VCFW
1199 +#ifndef VCHI_MAX_NUM_CONNECTIONS
1200 +# define VCHI_MAX_NUM_CONNECTIONS 3
1203 +/* How many services can we open per connection? Extending this doesn't cost processing time, just a small
1204 + * amount of static memory. */
1205 +#ifndef VCHI_MAX_SERVICES_PER_CONNECTION
1206 +# define VCHI_MAX_SERVICES_PER_CONNECTION 36
1209 +/* Adjust if using a message driver that supports more logical TX channels */
1210 +#ifndef VCHI_MAX_BULK_TX_CHANNELS_PER_CONNECTION
1211 +# define VCHI_MAX_BULK_TX_CHANNELS_PER_CONNECTION 9 // 1 MPHI + 8 CCP2 logical channels
1214 +/* Adjust if using a message driver that supports more logical RX channels */
1215 +#ifndef VCHI_MAX_BULK_RX_CHANNELS_PER_CONNECTION
1216 +# define VCHI_MAX_BULK_RX_CHANNELS_PER_CONNECTION 1 // 1 MPHI
1219 +/* How many receive slots do we use. This times VCHI_MAX_MSG_SIZE gives the effective
1220 + * receive queue space, less message headers. */
1221 +#ifndef VCHI_NUM_READ_SLOTS
1222 +# if defined(VCHI_LOCAL_HOST_PORT)
1223 +# define VCHI_NUM_READ_SLOTS 4
1225 +# define VCHI_NUM_READ_SLOTS 48
1229 +/* Do we utilise overrun facility for receive message slots? Can aid peer transmit
1230 + * performance. Only define on VideoCore end, talking to host.
1232 +//#define VCHI_MSG_RX_OVERRUN
1234 +/* How many transmit slots do we use. Generally don't need many, as the hardware driver
1235 + * underneath VCHI will usually have its own buffering. */
1236 +#ifndef VCHI_NUM_WRITE_SLOTS
1237 +# define VCHI_NUM_WRITE_SLOTS 4
1240 +/* If a service has held or queued received messages in VCHI_XOFF_THRESHOLD or more slots,
1241 + * then it's taking up too much buffer space, and the peer service will be told to stop
1242 + * transmitting with an XOFF message. For this to be effective, the VCHI_NUM_READ_SLOTS
1243 + * needs to be considerably bigger than VCHI_NUM_WRITE_SLOTS, or the transmit latency
1245 +#ifndef VCHI_XOFF_THRESHOLD
1246 +# define VCHI_XOFF_THRESHOLD (VCHI_NUM_READ_SLOTS / 2)
1249 +/* After we've sent an XOFF, the peer will be told to resume transmission once the local
1250 + * service has dequeued/released enough messages that it's now occupying
1251 + * VCHI_XON_THRESHOLD slots or fewer. */
1252 +#ifndef VCHI_XON_THRESHOLD
1253 +# define VCHI_XON_THRESHOLD (VCHI_NUM_READ_SLOTS / 4)
1256 +/* A size below which a bulk transfer omits the handshake completely and always goes
1257 + * via the message channel, if bulk auxiliary is being sent on that service. (The user
1258 + * can guarantee this by enabling unaligned transmits).
1260 +#ifndef VCHI_MIN_BULK_SIZE
1261 +# define VCHI_MIN_BULK_SIZE ( VCHI_MAX_MSG_SIZE / 2 < 4096 ? VCHI_MAX_MSG_SIZE / 2 : 4096 )
1264 +/* Maximum size of bulk transmission chunks, for each interface type. A trade-off between
1265 + * speed and latency; the smaller the chunk size the better change of messages and other
1266 + * bulk transmissions getting in when big bulk transfers are happening. Set to 0 to not
1267 + * break transmissions into chunks.
1269 +#ifndef VCHI_MAX_BULK_CHUNK_SIZE_MPHI
1270 +# define VCHI_MAX_BULK_CHUNK_SIZE_MPHI (16 * 1024)
1273 +/* NB Chunked CCP2 transmissions violate the letter of the CCP2 spec by using "JPEG8" mode
1274 + * with multiple-line frames. Only use if the receiver can cope. */
1275 +#ifndef VCHI_MAX_BULK_CHUNK_SIZE_CCP2
1276 +# define VCHI_MAX_BULK_CHUNK_SIZE_CCP2 0
1279 +/* How many TX messages can we have pending in our transmit slots. Once exhausted,
1280 + * vchi_msg_queue will be blocked. */
1281 +#ifndef VCHI_TX_MSG_QUEUE_SIZE
1282 +# define VCHI_TX_MSG_QUEUE_SIZE 256
1285 +/* How many RX messages can we have parsed in the receive slots. Once exhausted, parsing
1286 + * will be suspended until older messages are dequeued/released. */
1287 +#ifndef VCHI_RX_MSG_QUEUE_SIZE
1288 +# define VCHI_RX_MSG_QUEUE_SIZE 256
1291 +/* Really should be able to cope if we run out of received message descriptors, by
1292 + * suspending parsing as the comment above says, but we don't. This sweeps the issue
1293 + * under the carpet. */
1294 +#if VCHI_RX_MSG_QUEUE_SIZE < (VCHI_MAX_MSG_SIZE/16 + 1) * VCHI_NUM_READ_SLOTS
1295 +# undef VCHI_RX_MSG_QUEUE_SIZE
1296 +# define VCHI_RX_MSG_QUEUE_SIZE (VCHI_MAX_MSG_SIZE/16 + 1) * VCHI_NUM_READ_SLOTS
1299 +/* How many bulk transmits can we have pending. Once exhausted, vchi_bulk_queue_transmit
1300 + * will be blocked. */
1301 +#ifndef VCHI_TX_BULK_QUEUE_SIZE
1302 +# define VCHI_TX_BULK_QUEUE_SIZE 64
1305 +/* How many bulk receives can we have pending. Once exhausted, vchi_bulk_queue_receive
1306 + * will be blocked. */
1307 +#ifndef VCHI_RX_BULK_QUEUE_SIZE
1308 +# define VCHI_RX_BULK_QUEUE_SIZE 64
1311 +/* A limit on how many outstanding bulk requests we expect the peer to give us. If
1312 + * the peer asks for more than this, VCHI will fail and assert. The number is determined
1313 + * by the peer's hardware - it's the number of outstanding requests that can be queued
1314 + * on all bulk channels. VC3's MPHI peripheral allows 16. */
1315 +#ifndef VCHI_MAX_PEER_BULK_REQUESTS
1316 +# define VCHI_MAX_PEER_BULK_REQUESTS 32
1319 +/* Define VCHI_CCP2TX_MANUAL_POWER if the host tells us when to turn the CCP2
1320 + * transmitter on and off.
1322 +/*#define VCHI_CCP2TX_MANUAL_POWER*/
1324 +#ifndef VCHI_CCP2TX_MANUAL_POWER
1326 +/* Timeout (in milliseconds) for putting the CCP2TX interface into IDLE state. Set
1327 + * negative for no IDLE.
1329 +# ifndef VCHI_CCP2TX_IDLE_TIMEOUT
1330 +# define VCHI_CCP2TX_IDLE_TIMEOUT 5
1333 +/* Timeout (in milliseconds) for putting the CCP2TX interface into OFF state. Set
1334 + * negative for no OFF.
1336 +# ifndef VCHI_CCP2TX_OFF_TIMEOUT
1337 +# define VCHI_CCP2TX_OFF_TIMEOUT 1000
1340 +#endif /* VCHI_CCP2TX_MANUAL_POWER */
1342 +#endif /* VCHI_CFG_H_ */
1344 +/****************************** End of file **********************************/
1345 diff --git a/drivers/misc/vc04_services/interface/vchi/vchi_cfg_internal.h b/drivers/misc/vc04_services/interface/vchi/vchi_cfg_internal.h
1346 new file mode 100644
1347 index 0000000..35dcba4
1349 +++ b/drivers/misc/vc04_services/interface/vchi/vchi_cfg_internal.h
1352 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
1354 + * Redistribution and use in source and binary forms, with or without
1355 + * modification, are permitted provided that the following conditions
1357 + * 1. Redistributions of source code must retain the above copyright
1358 + * notice, this list of conditions, and the following disclaimer,
1359 + * without modification.
1360 + * 2. Redistributions in binary form must reproduce the above copyright
1361 + * notice, this list of conditions and the following disclaimer in the
1362 + * documentation and/or other materials provided with the distribution.
1363 + * 3. The names of the above-listed copyright holders may not be used
1364 + * to endorse or promote products derived from this software without
1365 + * specific prior written permission.
1367 + * ALTERNATIVELY, this software may be distributed under the terms of the
1368 + * GNU General Public License ("GPL") version 2, as published by the Free
1369 + * Software Foundation.
1371 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
1372 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
1373 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1374 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
1375 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
1376 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
1377 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
1378 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
1379 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
1380 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
1381 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1384 +#ifndef VCHI_CFG_INTERNAL_H_
1385 +#define VCHI_CFG_INTERNAL_H_
1387 +/****************************************************************************************
1388 + * Control optimisation attempts.
1389 + ***************************************************************************************/
1391 +// Don't use lots of short-term locks - use great long ones, reducing the overall locks-per-second
1392 +#define VCHI_COARSE_LOCKING
1394 +// Avoid lock then unlock on exit from blocking queue operations (msg tx, bulk rx/tx)
1395 +// (only relevant if VCHI_COARSE_LOCKING)
1396 +#define VCHI_ELIDE_BLOCK_EXIT_LOCK
1398 +// Avoid lock on non-blocking peek
1399 +// (only relevant if VCHI_COARSE_LOCKING)
1400 +#define VCHI_AVOID_PEEK_LOCK
1402 +// Use one slot-handler thread per connection, rather than 1 thread dealing with all connections in rotation.
1403 +#define VCHI_MULTIPLE_HANDLER_THREADS
1405 +// Put free descriptors onto the head of the free queue, rather than the tail, so that we don't thrash
1406 +// our way through the pool of descriptors.
1407 +#define VCHI_PUSH_FREE_DESCRIPTORS_ONTO_HEAD
1409 +// Don't issue a MSG_AVAILABLE callback for every single message. Possibly only safe if VCHI_COARSE_LOCKING.
1410 +#define VCHI_FEWER_MSG_AVAILABLE_CALLBACKS
1412 +// Don't use message descriptors for TX messages that don't need them
1413 +#define VCHI_MINIMISE_TX_MSG_DESCRIPTORS
1415 +// Nano-locks for multiqueue
1416 +//#define VCHI_MQUEUE_NANOLOCKS
1418 +// Lock-free(er) dequeuing
1419 +//#define VCHI_RX_NANOLOCKS
1421 +#endif /*VCHI_CFG_INTERNAL_H_*/
1422 diff --git a/drivers/misc/vc04_services/interface/vchi/vchi_common.h b/drivers/misc/vc04_services/interface/vchi/vchi_common.h
1423 new file mode 100644
1424 index 0000000..d76118c
1426 +++ b/drivers/misc/vc04_services/interface/vchi/vchi_common.h
1429 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
1431 + * Redistribution and use in source and binary forms, with or without
1432 + * modification, are permitted provided that the following conditions
1434 + * 1. Redistributions of source code must retain the above copyright
1435 + * notice, this list of conditions, and the following disclaimer,
1436 + * without modification.
1437 + * 2. Redistributions in binary form must reproduce the above copyright
1438 + * notice, this list of conditions and the following disclaimer in the
1439 + * documentation and/or other materials provided with the distribution.
1440 + * 3. The names of the above-listed copyright holders may not be used
1441 + * to endorse or promote products derived from this software without
1442 + * specific prior written permission.
1444 + * ALTERNATIVELY, this software may be distributed under the terms of the
1445 + * GNU General Public License ("GPL") version 2, as published by the Free
1446 + * Software Foundation.
1448 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
1449 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
1450 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1451 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
1452 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
1453 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
1454 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
1455 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
1456 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
1457 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
1458 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1461 +#ifndef VCHI_COMMON_H_
1462 +#define VCHI_COMMON_H_
1465 +//flags used when sending messages (must be bitmapped)
1468 + VCHI_FLAGS_NONE = 0x0,
1469 + 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)
1470 + VCHI_FLAGS_CALLBACK_WHEN_OP_COMPLETE = 0x2, // run a callback when message sent
1471 + VCHI_FLAGS_BLOCK_UNTIL_QUEUED = 0x4, // return once the transfer is in a queue ready to go
1472 + VCHI_FLAGS_ALLOW_PARTIAL = 0x8,
1473 + VCHI_FLAGS_BLOCK_UNTIL_DATA_READ = 0x10,
1474 + VCHI_FLAGS_CALLBACK_WHEN_DATA_READ = 0x20,
1476 + VCHI_FLAGS_ALIGN_SLOT = 0x000080, // internal use only
1477 + VCHI_FLAGS_BULK_AUX_QUEUED = 0x010000, // internal use only
1478 + VCHI_FLAGS_BULK_AUX_COMPLETE = 0x020000, // internal use only
1479 + VCHI_FLAGS_BULK_DATA_QUEUED = 0x040000, // internal use only
1480 + VCHI_FLAGS_BULK_DATA_COMPLETE = 0x080000, // internal use only
1481 + VCHI_FLAGS_INTERNAL = 0xFF0000
1484 +// constants for vchi_crc_control()
1486 + VCHI_CRC_NOTHING = -1,
1487 + VCHI_CRC_PER_SERVICE = 0,
1488 + VCHI_CRC_EVERYTHING = 1,
1489 +} VCHI_CRC_CONTROL_T;
1491 +//callback reasons when an event occurs on a service
1494 + VCHI_CALLBACK_REASON_MIN,
1496 + //This indicates that there is data available
1497 + //handle is the msg id that was transmitted with the data
1498 + // When a message is received and there was no FULL message available previously, send callback
1499 + // Tasks get kicked by the callback, reset their event and try and read from the fifo until it fails
1500 + VCHI_CALLBACK_MSG_AVAILABLE,
1501 + VCHI_CALLBACK_MSG_SENT,
1502 + VCHI_CALLBACK_MSG_SPACE_AVAILABLE, // XXX not yet implemented
1504 + // This indicates that a transfer from the other side has completed
1505 + VCHI_CALLBACK_BULK_RECEIVED,
1506 + //This indicates that data queued up to be sent has now gone
1507 + //handle is the msg id that was used when sending the data
1508 + VCHI_CALLBACK_BULK_SENT,
1509 + VCHI_CALLBACK_BULK_RX_SPACE_AVAILABLE, // XXX not yet implemented
1510 + VCHI_CALLBACK_BULK_TX_SPACE_AVAILABLE, // XXX not yet implemented
1512 + VCHI_CALLBACK_SERVICE_CLOSED,
1514 + // this side has sent XOFF to peer due to lack of data consumption by service
1515 + // (suggests the service may need to take some recovery action if it has
1516 + // been deliberately holding off consuming data)
1517 + VCHI_CALLBACK_SENT_XOFF,
1518 + VCHI_CALLBACK_SENT_XON,
1520 + // indicates that a bulk transfer has finished reading the source buffer
1521 + VCHI_CALLBACK_BULK_DATA_READ,
1523 + // power notification events (currently host side only)
1524 + VCHI_CALLBACK_PEER_OFF,
1525 + VCHI_CALLBACK_PEER_SUSPENDED,
1526 + VCHI_CALLBACK_PEER_ON,
1527 + VCHI_CALLBACK_PEER_RESUMED,
1528 + VCHI_CALLBACK_FORCED_POWER_OFF,
1530 +#ifdef USE_VCHIQ_ARM
1531 + // some extra notifications provided by vchiq_arm
1532 + VCHI_CALLBACK_SERVICE_OPENED,
1533 + VCHI_CALLBACK_BULK_RECEIVE_ABORTED,
1534 + VCHI_CALLBACK_BULK_TRANSMIT_ABORTED,
1537 + VCHI_CALLBACK_REASON_MAX
1538 +} VCHI_CALLBACK_REASON_T;
1540 +// service control options
1543 + VCHI_SERVICE_OPTION_MIN,
1545 + VCHI_SERVICE_OPTION_TRACE,
1547 + VCHI_SERVICE_OPTION_MAX
1548 +} VCHI_SERVICE_OPTION_T;
1551 +//Callback used by all services / bulk transfers
1552 +typedef void (*VCHI_CALLBACK_T)( void *callback_param, //my service local param
1553 + VCHI_CALLBACK_REASON_T reason,
1554 + void *handle ); //for transmitting msg's only
1559 + * Define vector struct for scatter-gather (vector) operations
1560 + * Vectors can be nested - if a vector element has negative length, then
1561 + * the data pointer is treated as pointing to another vector array, with
1562 + * '-vec_len' elements. Thus to append a header onto an existing vector,
1563 + * you can do this:
1565 + * void foo(const VCHI_MSG_VECTOR_T *v, int n)
1567 + * VCHI_MSG_VECTOR_T nv[2];
1568 + * nv[0].vec_base = my_header;
1569 + * nv[0].vec_len = sizeof my_header;
1570 + * nv[1].vec_base = v;
1571 + * nv[1].vec_len = -n;
1575 +typedef struct vchi_msg_vector {
1576 + const void *vec_base;
1578 +} VCHI_MSG_VECTOR_T;
1580 +// Opaque type for a connection API
1581 +typedef struct opaque_vchi_connection_api_t VCHI_CONNECTION_API_T;
1583 +// Opaque type for a message driver
1584 +typedef struct opaque_vchi_message_driver_t VCHI_MESSAGE_DRIVER_T;
1587 +// Iterator structure for reading ahead through received message queue. Allocated by client,
1588 +// initialised by vchi_msg_look_ahead. Fields are for internal VCHI use only.
1589 +// Iterates over messages in queue at the instant of the call to vchi_msg_lookahead -
1590 +// will not proceed to messages received since. Behaviour is undefined if an iterator
1591 +// is used again after messages for that service are removed/dequeued by any
1592 +// means other than vchi_msg_iter_... calls on the iterator itself.
1594 + struct opaque_vchi_service_t *service;
1601 +#endif // VCHI_COMMON_H_
1602 diff --git a/drivers/misc/vc04_services/interface/vchi/vchi_mh.h b/drivers/misc/vc04_services/interface/vchi/vchi_mh.h
1603 new file mode 100644
1604 index 0000000..198bd07
1606 +++ b/drivers/misc/vc04_services/interface/vchi/vchi_mh.h
1609 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
1611 + * Redistribution and use in source and binary forms, with or without
1612 + * modification, are permitted provided that the following conditions
1614 + * 1. Redistributions of source code must retain the above copyright
1615 + * notice, this list of conditions, and the following disclaimer,
1616 + * without modification.
1617 + * 2. Redistributions in binary form must reproduce the above copyright
1618 + * notice, this list of conditions and the following disclaimer in the
1619 + * documentation and/or other materials provided with the distribution.
1620 + * 3. The names of the above-listed copyright holders may not be used
1621 + * to endorse or promote products derived from this software without
1622 + * specific prior written permission.
1624 + * ALTERNATIVELY, this software may be distributed under the terms of the
1625 + * GNU General Public License ("GPL") version 2, as published by the Free
1626 + * Software Foundation.
1628 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
1629 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
1630 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1631 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
1632 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
1633 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
1634 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
1635 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
1636 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
1637 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
1638 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1644 +#include <linux/types.h>
1646 +typedef int32_t VCHI_MEM_HANDLE_T;
1647 +#define VCHI_MEM_HANDLE_INVALID 0
1650 diff --git a/drivers/misc/vc04_services/interface/vchiq_arm/vchiq.h b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq.h
1651 new file mode 100644
1652 index 0000000..ad398ba
1654 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq.h
1657 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
1659 + * Redistribution and use in source and binary forms, with or without
1660 + * modification, are permitted provided that the following conditions
1662 + * 1. Redistributions of source code must retain the above copyright
1663 + * notice, this list of conditions, and the following disclaimer,
1664 + * without modification.
1665 + * 2. Redistributions in binary form must reproduce the above copyright
1666 + * notice, this list of conditions and the following disclaimer in the
1667 + * documentation and/or other materials provided with the distribution.
1668 + * 3. The names of the above-listed copyright holders may not be used
1669 + * to endorse or promote products derived from this software without
1670 + * specific prior written permission.
1672 + * ALTERNATIVELY, this software may be distributed under the terms of the
1673 + * GNU General Public License ("GPL") version 2, as published by the Free
1674 + * Software Foundation.
1676 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
1677 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
1678 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1679 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
1680 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
1681 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
1682 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
1683 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
1684 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
1685 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
1686 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1689 +#ifndef VCHIQ_VCHIQ_H
1690 +#define VCHIQ_VCHIQ_H
1692 +#include "vchiq_if.h"
1693 +#include "vchiq_util.h"
1696 diff --git a/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835.h b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835.h
1697 new file mode 100644
1698 index 0000000..7ea5c64
1700 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835.h
1703 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
1705 + * Redistribution and use in source and binary forms, with or without
1706 + * modification, are permitted provided that the following conditions
1708 + * 1. Redistributions of source code must retain the above copyright
1709 + * notice, this list of conditions, and the following disclaimer,
1710 + * without modification.
1711 + * 2. Redistributions in binary form must reproduce the above copyright
1712 + * notice, this list of conditions and the following disclaimer in the
1713 + * documentation and/or other materials provided with the distribution.
1714 + * 3. The names of the above-listed copyright holders may not be used
1715 + * to endorse or promote products derived from this software without
1716 + * specific prior written permission.
1718 + * ALTERNATIVELY, this software may be distributed under the terms of the
1719 + * GNU General Public License ("GPL") version 2, as published by the Free
1720 + * Software Foundation.
1722 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
1723 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
1724 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1725 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
1726 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
1727 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
1728 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
1729 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
1730 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
1731 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
1732 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1735 +#ifndef VCHIQ_2835_H
1736 +#define VCHIQ_2835_H
1738 +#include "vchiq_pagelist.h"
1740 +#define VCHIQ_PLATFORM_FRAGMENTS_OFFSET_IDX 0
1741 +#define VCHIQ_PLATFORM_FRAGMENTS_COUNT_IDX 1
1743 +#endif /* VCHIQ_2835_H */
1744 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
1745 new file mode 100644
1746 index 0000000..b3bdaa2
1748 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835_arm.c
1751 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
1753 + * Redistribution and use in source and binary forms, with or without
1754 + * modification, are permitted provided that the following conditions
1756 + * 1. Redistributions of source code must retain the above copyright
1757 + * notice, this list of conditions, and the following disclaimer,
1758 + * without modification.
1759 + * 2. Redistributions in binary form must reproduce the above copyright
1760 + * notice, this list of conditions and the following disclaimer in the
1761 + * documentation and/or other materials provided with the distribution.
1762 + * 3. The names of the above-listed copyright holders may not be used
1763 + * to endorse or promote products derived from this software without
1764 + * specific prior written permission.
1766 + * ALTERNATIVELY, this software may be distributed under the terms of the
1767 + * GNU General Public License ("GPL") version 2, as published by the Free
1768 + * Software Foundation.
1770 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
1771 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
1772 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1773 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
1774 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
1775 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
1776 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
1777 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
1778 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
1779 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
1780 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1783 +#include <linux/kernel.h>
1784 +#include <linux/types.h>
1785 +#include <linux/errno.h>
1786 +#include <linux/interrupt.h>
1787 +#include <linux/irq.h>
1788 +#include <linux/pagemap.h>
1789 +#include <linux/dma-mapping.h>
1790 +#include <linux/version.h>
1791 +#include <linux/io.h>
1792 +#include <linux/uaccess.h>
1793 +#include <asm/pgtable.h>
1795 +#include <mach/irqs.h>
1797 +#include <mach/platform.h>
1798 +#include <mach/vcio.h>
1800 +#define TOTAL_SLOTS (VCHIQ_SLOT_ZERO_SLOTS + 2 * 32)
1802 +#define VCHIQ_DOORBELL_IRQ IRQ_ARM_DOORBELL_0
1803 +#define VCHIQ_ARM_ADDRESS(x) ((void *)__virt_to_bus((unsigned)x))
1805 +#include "vchiq_arm.h"
1806 +#include "vchiq_2835.h"
1807 +#include "vchiq_connected.h"
1809 +#define MAX_FRAGMENTS (VCHIQ_NUM_CURRENT_BULKS * 2)
1811 +typedef struct vchiq_2835_state_struct {
1813 + VCHIQ_ARM_STATE_T arm_state;
1814 +} VCHIQ_2835_ARM_STATE_T;
1816 +static char *g_slot_mem;
1817 +static int g_slot_mem_size;
1818 +dma_addr_t g_slot_phys;
1819 +static FRAGMENTS_T *g_fragments_base;
1820 +static FRAGMENTS_T *g_free_fragments;
1821 +struct semaphore g_free_fragments_sema;
1823 +extern int vchiq_arm_log_level;
1825 +static DEFINE_SEMAPHORE(g_free_fragments_mutex);
1828 +vchiq_doorbell_irq(int irq, void *dev_id);
1831 +create_pagelist(char __user *buf, size_t count, unsigned short type,
1832 + struct task_struct *task, PAGELIST_T ** ppagelist);
1835 +free_pagelist(PAGELIST_T *pagelist, int actual);
1838 +vchiq_platform_init(VCHIQ_STATE_T *state)
1840 + VCHIQ_SLOT_ZERO_T *vchiq_slot_zero;
1841 + int frag_mem_size;
1845 + /* Allocate space for the channels in coherent memory */
1846 + g_slot_mem_size = PAGE_ALIGN(TOTAL_SLOTS * VCHIQ_SLOT_SIZE);
1847 + frag_mem_size = PAGE_ALIGN(sizeof(FRAGMENTS_T) * MAX_FRAGMENTS);
1849 + g_slot_mem = dma_alloc_coherent(NULL, g_slot_mem_size + frag_mem_size,
1850 + &g_slot_phys, GFP_ATOMIC);
1852 + if (!g_slot_mem) {
1853 + vchiq_log_error(vchiq_arm_log_level,
1854 + "Unable to allocate channel memory");
1856 + goto failed_alloc;
1859 + WARN_ON(((int)g_slot_mem & (PAGE_SIZE - 1)) != 0);
1861 + vchiq_slot_zero = vchiq_init_slots(g_slot_mem, g_slot_mem_size);
1862 + if (!vchiq_slot_zero) {
1864 + goto failed_init_slots;
1867 + vchiq_slot_zero->platform_data[VCHIQ_PLATFORM_FRAGMENTS_OFFSET_IDX] =
1868 + (int)g_slot_phys + g_slot_mem_size;
1869 + vchiq_slot_zero->platform_data[VCHIQ_PLATFORM_FRAGMENTS_COUNT_IDX] =
1872 + g_fragments_base = (FRAGMENTS_T *)(g_slot_mem + g_slot_mem_size);
1873 + g_slot_mem_size += frag_mem_size;
1875 + g_free_fragments = g_fragments_base;
1876 + for (i = 0; i < (MAX_FRAGMENTS - 1); i++) {
1877 + *(FRAGMENTS_T **)&g_fragments_base[i] =
1878 + &g_fragments_base[i + 1];
1880 + *(FRAGMENTS_T **)&g_fragments_base[i] = NULL;
1881 + sema_init(&g_free_fragments_sema, MAX_FRAGMENTS);
1883 + if (vchiq_init_state(state, vchiq_slot_zero, 0/*slave*/) !=
1886 + goto failed_vchiq_init;
1889 + err = request_irq(VCHIQ_DOORBELL_IRQ, vchiq_doorbell_irq,
1890 + IRQF_IRQPOLL, "VCHIQ doorbell",
1893 + vchiq_log_error(vchiq_arm_log_level, "%s: failed to register "
1894 + "irq=%d err=%d", __func__,
1895 + VCHIQ_DOORBELL_IRQ, err);
1896 + goto failed_request_irq;
1899 + /* Send the base address of the slots to VideoCore */
1901 + dsb(); /* Ensure all writes have completed */
1903 + bcm_mailbox_write(MBOX_CHAN_VCHIQ, (unsigned int)g_slot_phys);
1905 + vchiq_log_info(vchiq_arm_log_level,
1906 + "vchiq_init - done (slots %x, phys %x)",
1907 + (unsigned int)vchiq_slot_zero, g_slot_phys);
1909 + vchiq_call_connected_callbacks();
1913 +failed_request_irq:
1916 + dma_free_coherent(NULL, g_slot_mem_size, g_slot_mem, g_slot_phys);
1923 +vchiq_platform_exit(VCHIQ_STATE_T *state)
1925 + free_irq(VCHIQ_DOORBELL_IRQ, state);
1926 + dma_free_coherent(NULL, g_slot_mem_size,
1927 + g_slot_mem, g_slot_phys);
1932 +vchiq_platform_init_state(VCHIQ_STATE_T *state)
1934 + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
1935 + state->platform_state = kzalloc(sizeof(VCHIQ_2835_ARM_STATE_T), GFP_KERNEL);
1936 + ((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->inited = 1;
1937 + status = vchiq_arm_init_state(state, &((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->arm_state);
1938 + if(status != VCHIQ_SUCCESS)
1940 + ((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->inited = 0;
1946 +vchiq_platform_get_arm_state(VCHIQ_STATE_T *state)
1948 + if(!((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->inited)
1952 + return &((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->arm_state;
1956 +remote_event_signal(REMOTE_EVENT_T *event)
1962 + dsb(); /* data barrier operation */
1964 + if (event->armed) {
1965 + /* trigger vc interrupt */
1967 + writel(0, __io_address(ARM_0_BELL2));
1972 +vchiq_copy_from_user(void *dst, const void *src, int size)
1974 + if ((uint32_t)src < TASK_SIZE) {
1975 + return copy_from_user(dst, src, size);
1977 + memcpy(dst, src, size);
1983 +vchiq_prepare_bulk_data(VCHIQ_BULK_T *bulk, VCHI_MEM_HANDLE_T memhandle,
1984 + void *offset, int size, int dir)
1986 + PAGELIST_T *pagelist;
1989 + WARN_ON(memhandle != VCHI_MEM_HANDLE_INVALID);
1991 + ret = create_pagelist((char __user *)offset, size,
1992 + (dir == VCHIQ_BULK_RECEIVE)
1998 + return VCHIQ_ERROR;
2000 + bulk->handle = memhandle;
2001 + bulk->data = VCHIQ_ARM_ADDRESS(pagelist);
2003 + /* Store the pagelist address in remote_data, which isn't used by the
2005 + bulk->remote_data = pagelist;
2007 + return VCHIQ_SUCCESS;
2011 +vchiq_complete_bulk(VCHIQ_BULK_T *bulk)
2013 + if (bulk && bulk->remote_data && bulk->actual)
2014 + free_pagelist((PAGELIST_T *)bulk->remote_data, bulk->actual);
2018 +vchiq_transfer_bulk(VCHIQ_BULK_T *bulk)
2021 + * This should only be called on the master (VideoCore) side, but
2022 + * provide an implementation to avoid the need for ifdefery.
2028 +vchiq_dump_platform_state(void *dump_context)
2032 + len = snprintf(buf, sizeof(buf),
2033 + " Platform: 2835 (VC master)");
2034 + vchiq_dump(dump_context, buf, len + 1);
2038 +vchiq_platform_suspend(VCHIQ_STATE_T *state)
2040 + return VCHIQ_ERROR;
2044 +vchiq_platform_resume(VCHIQ_STATE_T *state)
2046 + return VCHIQ_SUCCESS;
2050 +vchiq_platform_paused(VCHIQ_STATE_T *state)
2055 +vchiq_platform_resumed(VCHIQ_STATE_T *state)
2060 +vchiq_platform_videocore_wanted(VCHIQ_STATE_T* state)
2062 + return 1; // autosuspend not supported - videocore always wanted
2066 +vchiq_platform_use_suspend_timer(void)
2071 +vchiq_dump_platform_use_state(VCHIQ_STATE_T *state)
2073 + vchiq_log_info((vchiq_arm_log_level>=VCHIQ_LOG_INFO),"Suspend timer not in use");
2076 +vchiq_platform_handle_timeout(VCHIQ_STATE_T *state)
2085 +vchiq_doorbell_irq(int irq, void *dev_id)
2087 + VCHIQ_STATE_T *state = dev_id;
2088 + irqreturn_t ret = IRQ_NONE;
2089 + unsigned int status;
2091 + /* Read (and clear) the doorbell */
2092 + status = readl(__io_address(ARM_0_BELL0));
2094 + if (status & 0x4) { /* Was the doorbell rung? */
2095 + remote_event_pollall(state);
2096 + ret = IRQ_HANDLED;
2102 +/* There is a potential problem with partial cache lines (pages?)
2103 +** at the ends of the block when reading. If the CPU accessed anything in
2104 +** the same line (page?) then it may have pulled old data into the cache,
2105 +** obscuring the new data underneath. We can solve this by transferring the
2106 +** partial cache lines separately, and allowing the ARM to copy into the
2109 +** N.B. This implementation plays slightly fast and loose with the Linux
2110 +** driver programming rules, e.g. its use of __virt_to_bus instead of
2111 +** dma_map_single, but it isn't a multi-platform driver and it benefits
2112 +** from increased speed as a result.
2116 +create_pagelist(char __user *buf, size_t count, unsigned short type,
2117 + struct task_struct *task, PAGELIST_T ** ppagelist)
2119 + PAGELIST_T *pagelist;
2120 + struct page **pages;
2121 + struct page *page;
2122 + unsigned long *addrs;
2123 + unsigned int num_pages, offset, i;
2124 + char *addr, *base_addr, *next_addr;
2125 + int run, addridx, actual_pages;
2126 + unsigned long *need_release;
2128 + offset = (unsigned int)buf & (PAGE_SIZE - 1);
2129 + num_pages = (count + offset + PAGE_SIZE - 1) / PAGE_SIZE;
2131 + *ppagelist = NULL;
2133 + /* Allocate enough storage to hold the page pointers and the page
2136 + pagelist = kmalloc(sizeof(PAGELIST_T) +
2137 + (num_pages * sizeof(unsigned long)) +
2138 + sizeof(unsigned long) +
2139 + (num_pages * sizeof(pages[0])),
2142 + vchiq_log_trace(vchiq_arm_log_level,
2143 + "create_pagelist - %x", (unsigned int)pagelist);
2147 + addrs = pagelist->addrs;
2148 + need_release = (unsigned long *)(addrs + num_pages);
2149 + pages = (struct page **)(addrs + num_pages + 1);
2151 + if (is_vmalloc_addr(buf)) {
2152 + for (actual_pages = 0; actual_pages < num_pages; actual_pages++) {
2153 + pages[actual_pages] = vmalloc_to_page(buf + (actual_pages * PAGE_SIZE));
2155 + *need_release = 0; /* do not try and release vmalloc pages */
2157 + down_read(&task->mm->mmap_sem);
2158 + actual_pages = get_user_pages(task, task->mm,
2159 + (unsigned long)buf & ~(PAGE_SIZE - 1),
2161 + (type == PAGELIST_READ) /*Write */ ,
2165 + up_read(&task->mm->mmap_sem);
2167 + if (actual_pages != num_pages) {
2168 + vchiq_log_info(vchiq_arm_log_level,
2169 + "create_pagelist - only %d/%d pages locked",
2173 + /* This is probably due to the process being killed */
2174 + while (actual_pages > 0)
2177 + page_cache_release(pages[actual_pages]);
2180 + if (actual_pages == 0)
2181 + actual_pages = -ENOMEM;
2182 + return actual_pages;
2184 + *need_release = 1; /* release user pages */
2187 + pagelist->length = count;
2188 + pagelist->type = type;
2189 + pagelist->offset = offset;
2191 + /* Group the pages into runs of contiguous pages */
2193 + base_addr = VCHIQ_ARM_ADDRESS(page_address(pages[0]));
2194 + next_addr = base_addr + PAGE_SIZE;
2198 + for (i = 1; i < num_pages; i++) {
2199 + addr = VCHIQ_ARM_ADDRESS(page_address(pages[i]));
2200 + if ((addr == next_addr) && (run < (PAGE_SIZE - 1))) {
2201 + next_addr += PAGE_SIZE;
2204 + addrs[addridx] = (unsigned long)base_addr + run;
2207 + next_addr = addr + PAGE_SIZE;
2212 + addrs[addridx] = (unsigned long)base_addr + run;
2215 + /* Partial cache lines (fragments) require special measures */
2216 + if ((type == PAGELIST_READ) &&
2217 + ((pagelist->offset & (CACHE_LINE_SIZE - 1)) ||
2218 + ((pagelist->offset + pagelist->length) &
2219 + (CACHE_LINE_SIZE - 1)))) {
2220 + FRAGMENTS_T *fragments;
2222 + if (down_interruptible(&g_free_fragments_sema) != 0) {
2227 + WARN_ON(g_free_fragments == NULL);
2229 + down(&g_free_fragments_mutex);
2230 + fragments = (FRAGMENTS_T *) g_free_fragments;
2231 + WARN_ON(fragments == NULL);
2232 + g_free_fragments = *(FRAGMENTS_T **) g_free_fragments;
2233 + up(&g_free_fragments_mutex);
2235 + PAGELIST_READ_WITH_FRAGMENTS + (fragments -
2236 + g_fragments_base);
2239 + for (page = virt_to_page(pagelist);
2240 + page <= virt_to_page(addrs + num_pages - 1); page++) {
2241 + flush_dcache_page(page);
2244 + *ppagelist = pagelist;
2250 +free_pagelist(PAGELIST_T *pagelist, int actual)
2252 + unsigned long *need_release;
2253 + struct page **pages;
2254 + unsigned int num_pages, i;
2256 + vchiq_log_trace(vchiq_arm_log_level,
2257 + "free_pagelist - %x, %d", (unsigned int)pagelist, actual);
2260 + (pagelist->length + pagelist->offset + PAGE_SIZE - 1) /
2263 + need_release = (unsigned long *)(pagelist->addrs + num_pages);
2264 + pages = (struct page **)(pagelist->addrs + num_pages + 1);
2266 + /* Deal with any partial cache lines (fragments) */
2267 + if (pagelist->type >= PAGELIST_READ_WITH_FRAGMENTS) {
2268 + FRAGMENTS_T *fragments = g_fragments_base +
2269 + (pagelist->type - PAGELIST_READ_WITH_FRAGMENTS);
2270 + int head_bytes, tail_bytes;
2271 + head_bytes = (CACHE_LINE_SIZE - pagelist->offset) &
2272 + (CACHE_LINE_SIZE - 1);
2273 + tail_bytes = (pagelist->offset + actual) &
2274 + (CACHE_LINE_SIZE - 1);
2276 + if ((actual >= 0) && (head_bytes != 0)) {
2277 + if (head_bytes > actual)
2278 + head_bytes = actual;
2280 + memcpy((char *)page_address(pages[0]) +
2282 + fragments->headbuf,
2285 + if ((actual >= 0) && (head_bytes < actual) &&
2286 + (tail_bytes != 0)) {
2287 + memcpy((char *)page_address(pages[num_pages - 1]) +
2288 + ((pagelist->offset + actual) &
2289 + (PAGE_SIZE - 1) & ~(CACHE_LINE_SIZE - 1)),
2290 + fragments->tailbuf, tail_bytes);
2293 + down(&g_free_fragments_mutex);
2294 + *(FRAGMENTS_T **) fragments = g_free_fragments;
2295 + g_free_fragments = fragments;
2296 + up(&g_free_fragments_mutex);
2297 + up(&g_free_fragments_sema);
2300 + if (*need_release) {
2301 + for (i = 0; i < num_pages; i++) {
2302 + if (pagelist->type != PAGELIST_WRITE)
2303 + set_page_dirty(pages[i]);
2305 + page_cache_release(pages[i]);
2311 diff --git a/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.c b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.c
2312 new file mode 100644
2313 index 0000000..2596818
2315 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.c
2318 + * Copyright (c) 2014 Raspberry Pi (Trading) Ltd. All rights reserved.
2319 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
2321 + * Redistribution and use in source and binary forms, with or without
2322 + * modification, are permitted provided that the following conditions
2324 + * 1. Redistributions of source code must retain the above copyright
2325 + * notice, this list of conditions, and the following disclaimer,
2326 + * without modification.
2327 + * 2. Redistributions in binary form must reproduce the above copyright
2328 + * notice, this list of conditions and the following disclaimer in the
2329 + * documentation and/or other materials provided with the distribution.
2330 + * 3. The names of the above-listed copyright holders may not be used
2331 + * to endorse or promote products derived from this software without
2332 + * specific prior written permission.
2334 + * ALTERNATIVELY, this software may be distributed under the terms of the
2335 + * GNU General Public License ("GPL") version 2, as published by the Free
2336 + * Software Foundation.
2338 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
2339 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
2340 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2341 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
2342 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
2343 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
2344 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
2345 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
2346 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
2347 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
2348 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2351 +#include <linux/kernel.h>
2352 +#include <linux/module.h>
2353 +#include <linux/types.h>
2354 +#include <linux/errno.h>
2355 +#include <linux/cdev.h>
2356 +#include <linux/fs.h>
2357 +#include <linux/device.h>
2358 +#include <linux/mm.h>
2359 +#include <linux/highmem.h>
2360 +#include <linux/pagemap.h>
2361 +#include <linux/bug.h>
2362 +#include <linux/semaphore.h>
2363 +#include <linux/list.h>
2365 +#include "vchiq_core.h"
2366 +#include "vchiq_ioctl.h"
2367 +#include "vchiq_arm.h"
2368 +#include "vchiq_debugfs.h"
2370 +#define DEVICE_NAME "vchiq"
2372 +/* Override the default prefix, which would be vchiq_arm (from the filename) */
2373 +#undef MODULE_PARAM_PREFIX
2374 +#define MODULE_PARAM_PREFIX DEVICE_NAME "."
2376 +#define VCHIQ_MINOR 0
2378 +/* Some per-instance constants */
2379 +#define MAX_COMPLETIONS 16
2380 +#define MAX_SERVICES 64
2381 +#define MAX_ELEMENTS 8
2382 +#define MSG_QUEUE_SIZE 64
2384 +#define KEEPALIVE_VER 1
2385 +#define KEEPALIVE_VER_MIN KEEPALIVE_VER
2387 +/* Run time control of log level, based on KERN_XXX level. */
2388 +int vchiq_arm_log_level = VCHIQ_LOG_DEFAULT;
2389 +int vchiq_susp_log_level = VCHIQ_LOG_ERROR;
2391 +#define SUSPEND_TIMER_TIMEOUT_MS 100
2392 +#define SUSPEND_RETRY_TIMER_TIMEOUT_MS 1000
2394 +#define VC_SUSPEND_NUM_OFFSET 3 /* number of values before idle which are -ve */
2395 +static const char *const suspend_state_names[] = {
2396 + "VC_SUSPEND_FORCE_CANCELED",
2397 + "VC_SUSPEND_REJECTED",
2398 + "VC_SUSPEND_FAILED",
2399 + "VC_SUSPEND_IDLE",
2400 + "VC_SUSPEND_REQUESTED",
2401 + "VC_SUSPEND_IN_PROGRESS",
2402 + "VC_SUSPEND_SUSPENDED"
2404 +#define VC_RESUME_NUM_OFFSET 1 /* number of values before idle which are -ve */
2405 +static const char *const resume_state_names[] = {
2406 + "VC_RESUME_FAILED",
2408 + "VC_RESUME_REQUESTED",
2409 + "VC_RESUME_IN_PROGRESS",
2410 + "VC_RESUME_RESUMED"
2412 +/* The number of times we allow force suspend to timeout before actually
2413 +** _forcing_ suspend. This is to cater for SW which fails to release vchiq
2414 +** correctly - we don't want to prevent ARM suspend indefinitely in this case.
2416 +#define FORCE_SUSPEND_FAIL_MAX 8
2418 +/* The time in ms allowed for videocore to go idle when force suspend has been
2420 +#define FORCE_SUSPEND_TIMEOUT_MS 200
2423 +static void suspend_timer_callback(unsigned long context);
2426 +typedef struct user_service_struct {
2427 + VCHIQ_SERVICE_T *service;
2429 + VCHIQ_INSTANCE_T instance;
2431 + char dequeue_pending;
2432 + char close_pending;
2433 + int message_available_pos;
2436 + struct semaphore insert_event;
2437 + struct semaphore remove_event;
2438 + struct semaphore close_event;
2439 + VCHIQ_HEADER_T * msg_queue[MSG_QUEUE_SIZE];
2442 +struct bulk_waiter_node {
2443 + struct bulk_waiter bulk_waiter;
2445 + struct list_head list;
2448 +struct vchiq_instance_struct {
2449 + VCHIQ_STATE_T *state;
2450 + VCHIQ_COMPLETION_DATA_T completions[MAX_COMPLETIONS];
2451 + int completion_insert;
2452 + int completion_remove;
2453 + struct semaphore insert_event;
2454 + struct semaphore remove_event;
2455 + struct mutex completion_mutex;
2461 + int use_close_delivered;
2464 + struct list_head bulk_waiter_list;
2465 + struct mutex bulk_waiter_list_mutex;
2467 + VCHIQ_DEBUGFS_NODE_T debugfs_node;
2470 +typedef struct dump_context_struct {
2477 +static struct cdev vchiq_cdev;
2478 +static dev_t vchiq_devid;
2479 +static VCHIQ_STATE_T g_state;
2480 +static struct class *vchiq_class;
2481 +static struct device *vchiq_dev;
2482 +static DEFINE_SPINLOCK(msg_queue_spinlock);
2484 +static const char *const ioctl_names[] = {
2490 + "QUEUE_BULK_TRANSMIT",
2491 + "QUEUE_BULK_RECEIVE",
2492 + "AWAIT_COMPLETION",
2493 + "DEQUEUE_MESSAGE",
2498 + "RELEASE_SERVICE",
2499 + "SET_SERVICE_OPTION",
2505 +vchiq_static_assert((sizeof(ioctl_names)/sizeof(ioctl_names[0])) ==
2506 + (VCHIQ_IOC_MAX + 1));
2509 +dump_phys_mem(void *virt_addr, uint32_t num_bytes);
2511 +/****************************************************************************
2515 +***************************************************************************/
2517 +static VCHIQ_STATUS_T
2518 +add_completion(VCHIQ_INSTANCE_T instance, VCHIQ_REASON_T reason,
2519 + VCHIQ_HEADER_T *header, USER_SERVICE_T *user_service,
2520 + void *bulk_userdata)
2522 + VCHIQ_COMPLETION_DATA_T *completion;
2523 + DEBUG_INITIALISE(g_state.local)
2525 + while (instance->completion_insert ==
2526 + (instance->completion_remove + MAX_COMPLETIONS)) {
2527 + /* Out of space - wait for the client */
2528 + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
2529 + vchiq_log_trace(vchiq_arm_log_level,
2530 + "add_completion - completion queue full");
2531 + DEBUG_COUNT(COMPLETION_QUEUE_FULL_COUNT);
2532 + if (down_interruptible(&instance->remove_event) != 0) {
2533 + vchiq_log_info(vchiq_arm_log_level,
2534 + "service_callback interrupted");
2535 + return VCHIQ_RETRY;
2536 + } else if (instance->closing) {
2537 + vchiq_log_info(vchiq_arm_log_level,
2538 + "service_callback closing");
2539 + return VCHIQ_ERROR;
2541 + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
2545 + &instance->completions[instance->completion_insert &
2546 + (MAX_COMPLETIONS - 1)];
2548 + completion->header = header;
2549 + completion->reason = reason;
2550 + /* N.B. service_userdata is updated while processing AWAIT_COMPLETION */
2551 + completion->service_userdata = user_service->service;
2552 + completion->bulk_userdata = bulk_userdata;
2554 + if (reason == VCHIQ_SERVICE_CLOSED) {
2555 + /* Take an extra reference, to be held until
2556 + this CLOSED notification is delivered. */
2557 + lock_service(user_service->service);
2558 + if (instance->use_close_delivered)
2559 + user_service->close_pending = 1;
2562 + /* A write barrier is needed here to ensure that the entire completion
2563 + record is written out before the insert point. */
2566 + if (reason == VCHIQ_MESSAGE_AVAILABLE)
2567 + user_service->message_available_pos =
2568 + instance->completion_insert;
2569 + instance->completion_insert++;
2571 + up(&instance->insert_event);
2573 + return VCHIQ_SUCCESS;
2576 +/****************************************************************************
2580 +***************************************************************************/
2582 +static VCHIQ_STATUS_T
2583 +service_callback(VCHIQ_REASON_T reason, VCHIQ_HEADER_T *header,
2584 + VCHIQ_SERVICE_HANDLE_T handle, void *bulk_userdata)
2586 + /* How do we ensure the callback goes to the right client?
2587 + ** The service_user data points to a USER_SERVICE_T record containing
2588 + ** the original callback and the user state structure, which contains a
2589 + ** circular buffer for completion records.
2591 + USER_SERVICE_T *user_service;
2592 + VCHIQ_SERVICE_T *service;
2593 + VCHIQ_INSTANCE_T instance;
2594 + DEBUG_INITIALISE(g_state.local)
2596 + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
2598 + service = handle_to_service(handle);
2600 + user_service = (USER_SERVICE_T *)service->base.userdata;
2601 + instance = user_service->instance;
2603 + if (!instance || instance->closing)
2604 + return VCHIQ_SUCCESS;
2606 + vchiq_log_trace(vchiq_arm_log_level,
2607 + "service_callback - service %lx(%d,%p), reason %d, header %lx, "
2608 + "instance %lx, bulk_userdata %lx",
2609 + (unsigned long)user_service,
2610 + service->localport, user_service->userdata,
2611 + reason, (unsigned long)header,
2612 + (unsigned long)instance, (unsigned long)bulk_userdata);
2614 + if (header && user_service->is_vchi) {
2615 + spin_lock(&msg_queue_spinlock);
2616 + while (user_service->msg_insert ==
2617 + (user_service->msg_remove + MSG_QUEUE_SIZE)) {
2618 + spin_unlock(&msg_queue_spinlock);
2619 + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
2620 + DEBUG_COUNT(MSG_QUEUE_FULL_COUNT);
2621 + vchiq_log_trace(vchiq_arm_log_level,
2622 + "service_callback - msg queue full");
2623 + /* If there is no MESSAGE_AVAILABLE in the completion
2626 + if ((user_service->message_available_pos -
2627 + instance->completion_remove) < 0) {
2628 + VCHIQ_STATUS_T status;
2629 + vchiq_log_info(vchiq_arm_log_level,
2630 + "Inserting extra MESSAGE_AVAILABLE");
2631 + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
2632 + status = add_completion(instance, reason,
2633 + NULL, user_service, bulk_userdata);
2634 + if (status != VCHIQ_SUCCESS) {
2635 + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
2640 + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
2641 + if (down_interruptible(&user_service->remove_event)
2643 + vchiq_log_info(vchiq_arm_log_level,
2644 + "service_callback interrupted");
2645 + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
2646 + return VCHIQ_RETRY;
2647 + } else if (instance->closing) {
2648 + vchiq_log_info(vchiq_arm_log_level,
2649 + "service_callback closing");
2650 + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
2651 + return VCHIQ_ERROR;
2653 + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
2654 + spin_lock(&msg_queue_spinlock);
2657 + user_service->msg_queue[user_service->msg_insert &
2658 + (MSG_QUEUE_SIZE - 1)] = header;
2659 + user_service->msg_insert++;
2660 + spin_unlock(&msg_queue_spinlock);
2662 + up(&user_service->insert_event);
2664 + /* If there is a thread waiting in DEQUEUE_MESSAGE, or if
2665 + ** there is a MESSAGE_AVAILABLE in the completion queue then
2666 + ** bypass the completion queue.
2668 + if (((user_service->message_available_pos -
2669 + instance->completion_remove) >= 0) ||
2670 + user_service->dequeue_pending) {
2671 + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
2672 + user_service->dequeue_pending = 0;
2673 + return VCHIQ_SUCCESS;
2678 + DEBUG_TRACE(SERVICE_CALLBACK_LINE);
2680 + return add_completion(instance, reason, header, user_service,
2684 +/****************************************************************************
2686 +* user_service_free
2688 +***************************************************************************/
2690 +user_service_free(void *userdata)
2695 +/****************************************************************************
2699 +***************************************************************************/
2700 +static void close_delivered(USER_SERVICE_T *user_service)
2702 + vchiq_log_info(vchiq_arm_log_level,
2703 + "close_delivered(handle=%x)",
2704 + user_service->service->handle);
2706 + if (user_service->close_pending) {
2707 + /* Allow the underlying service to be culled */
2708 + unlock_service(user_service->service);
2710 + /* Wake the user-thread blocked in close_ or remove_service */
2711 + up(&user_service->close_event);
2713 + user_service->close_pending = 0;
2717 +/****************************************************************************
2721 +***************************************************************************/
2723 +vchiq_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2725 + VCHIQ_INSTANCE_T instance = file->private_data;
2726 + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
2727 + VCHIQ_SERVICE_T *service = NULL;
2730 + DEBUG_INITIALISE(g_state.local)
2732 + vchiq_log_trace(vchiq_arm_log_level,
2733 + "vchiq_ioctl - instance %x, cmd %s, arg %lx",
2734 + (unsigned int)instance,
2735 + ((_IOC_TYPE(cmd) == VCHIQ_IOC_MAGIC) &&
2736 + (_IOC_NR(cmd) <= VCHIQ_IOC_MAX)) ?
2737 + ioctl_names[_IOC_NR(cmd)] : "<invalid>", arg);
2740 + case VCHIQ_IOC_SHUTDOWN:
2741 + if (!instance->connected)
2744 + /* Remove all services */
2746 + while ((service = next_service_by_instance(instance->state,
2747 + instance, &i)) != NULL) {
2748 + status = vchiq_remove_service(service->handle);
2749 + unlock_service(service);
2750 + if (status != VCHIQ_SUCCESS)
2755 + if (status == VCHIQ_SUCCESS) {
2756 + /* Wake the completion thread and ask it to exit */
2757 + instance->closing = 1;
2758 + up(&instance->insert_event);
2763 + case VCHIQ_IOC_CONNECT:
2764 + if (instance->connected) {
2768 + rc = mutex_lock_interruptible(&instance->state->mutex);
2770 + vchiq_log_error(vchiq_arm_log_level,
2771 + "vchiq: connect: could not lock mutex for "
2773 + instance->state->id, rc);
2777 + status = vchiq_connect_internal(instance->state, instance);
2778 + mutex_unlock(&instance->state->mutex);
2780 + if (status == VCHIQ_SUCCESS)
2781 + instance->connected = 1;
2783 + vchiq_log_error(vchiq_arm_log_level,
2784 + "vchiq: could not connect: %d", status);
2787 + case VCHIQ_IOC_CREATE_SERVICE: {
2788 + VCHIQ_CREATE_SERVICE_T args;
2789 + USER_SERVICE_T *user_service = NULL;
2793 + if (copy_from_user
2794 + (&args, (const void __user *)arg,
2795 + sizeof(args)) != 0) {
2800 + user_service = kmalloc(sizeof(USER_SERVICE_T), GFP_KERNEL);
2801 + if (!user_service) {
2806 + if (args.is_open) {
2807 + if (!instance->connected) {
2809 + kfree(user_service);
2812 + srvstate = VCHIQ_SRVSTATE_OPENING;
2815 + instance->connected ?
2816 + VCHIQ_SRVSTATE_LISTENING :
2817 + VCHIQ_SRVSTATE_HIDDEN;
2820 + userdata = args.params.userdata;
2821 + args.params.callback = service_callback;
2822 + args.params.userdata = user_service;
2823 + service = vchiq_add_service_internal(
2825 + &args.params, srvstate,
2826 + instance, user_service_free);
2828 + if (service != NULL) {
2829 + user_service->service = service;
2830 + user_service->userdata = userdata;
2831 + user_service->instance = instance;
2832 + user_service->is_vchi = (args.is_vchi != 0);
2833 + user_service->dequeue_pending = 0;
2834 + user_service->close_pending = 0;
2835 + user_service->message_available_pos =
2836 + instance->completion_remove - 1;
2837 + user_service->msg_insert = 0;
2838 + user_service->msg_remove = 0;
2839 + sema_init(&user_service->insert_event, 0);
2840 + sema_init(&user_service->remove_event, 0);
2841 + sema_init(&user_service->close_event, 0);
2843 + if (args.is_open) {
2844 + status = vchiq_open_service_internal
2845 + (service, instance->pid);
2846 + if (status != VCHIQ_SUCCESS) {
2847 + vchiq_remove_service(service->handle);
2849 + ret = (status == VCHIQ_RETRY) ?
2855 + if (copy_to_user((void __user *)
2856 + &(((VCHIQ_CREATE_SERVICE_T __user *)
2858 + (const void *)&service->handle,
2859 + sizeof(service->handle)) != 0) {
2861 + vchiq_remove_service(service->handle);
2867 + kfree(user_service);
2871 + case VCHIQ_IOC_CLOSE_SERVICE: {
2872 + VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
2874 + service = find_service_for_instance(instance, handle);
2875 + if (service != NULL) {
2876 + USER_SERVICE_T *user_service =
2877 + (USER_SERVICE_T *)service->base.userdata;
2878 + /* close_pending is false on first entry, and when the
2879 + wait in vchiq_close_service has been interrupted. */
2880 + if (!user_service->close_pending) {
2881 + status = vchiq_close_service(service->handle);
2882 + if (status != VCHIQ_SUCCESS)
2886 + /* close_pending is true once the underlying service
2887 + has been closed until the client library calls the
2888 + CLOSE_DELIVERED ioctl, signalling close_event. */
2889 + if (user_service->close_pending &&
2890 + down_interruptible(&user_service->close_event))
2891 + status = VCHIQ_RETRY;
2897 + case VCHIQ_IOC_REMOVE_SERVICE: {
2898 + VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
2900 + service = find_service_for_instance(instance, handle);
2901 + if (service != NULL) {
2902 + USER_SERVICE_T *user_service =
2903 + (USER_SERVICE_T *)service->base.userdata;
2904 + /* close_pending is false on first entry, and when the
2905 + wait in vchiq_close_service has been interrupted. */
2906 + if (!user_service->close_pending) {
2907 + status = vchiq_remove_service(service->handle);
2908 + if (status != VCHIQ_SUCCESS)
2912 + /* close_pending is true once the underlying service
2913 + has been closed until the client library calls the
2914 + CLOSE_DELIVERED ioctl, signalling close_event. */
2915 + if (user_service->close_pending &&
2916 + down_interruptible(&user_service->close_event))
2917 + status = VCHIQ_RETRY;
2923 + case VCHIQ_IOC_USE_SERVICE:
2924 + case VCHIQ_IOC_RELEASE_SERVICE: {
2925 + VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
2927 + service = find_service_for_instance(instance, handle);
2928 + if (service != NULL) {
2929 + status = (cmd == VCHIQ_IOC_USE_SERVICE) ?
2930 + vchiq_use_service_internal(service) :
2931 + vchiq_release_service_internal(service);
2932 + if (status != VCHIQ_SUCCESS) {
2933 + vchiq_log_error(vchiq_susp_log_level,
2934 + "%s: cmd %s returned error %d for "
2935 + "service %c%c%c%c:%03d",
2937 + (cmd == VCHIQ_IOC_USE_SERVICE) ?
2938 + "VCHIQ_IOC_USE_SERVICE" :
2939 + "VCHIQ_IOC_RELEASE_SERVICE",
2941 + VCHIQ_FOURCC_AS_4CHARS(
2942 + service->base.fourcc),
2943 + service->client_id);
2950 + case VCHIQ_IOC_QUEUE_MESSAGE: {
2951 + VCHIQ_QUEUE_MESSAGE_T args;
2952 + if (copy_from_user
2953 + (&args, (const void __user *)arg,
2954 + sizeof(args)) != 0) {
2959 + service = find_service_for_instance(instance, args.handle);
2961 + if ((service != NULL) && (args.count <= MAX_ELEMENTS)) {
2962 + /* Copy elements into kernel space */
2963 + VCHIQ_ELEMENT_T elements[MAX_ELEMENTS];
2964 + if (copy_from_user(elements, args.elements,
2965 + args.count * sizeof(VCHIQ_ELEMENT_T)) == 0)
2966 + status = vchiq_queue_message
2968 + elements, args.count);
2976 + case VCHIQ_IOC_QUEUE_BULK_TRANSMIT:
2977 + case VCHIQ_IOC_QUEUE_BULK_RECEIVE: {
2978 + VCHIQ_QUEUE_BULK_TRANSFER_T args;
2979 + struct bulk_waiter_node *waiter = NULL;
2980 + VCHIQ_BULK_DIR_T dir =
2981 + (cmd == VCHIQ_IOC_QUEUE_BULK_TRANSMIT) ?
2982 + VCHIQ_BULK_TRANSMIT : VCHIQ_BULK_RECEIVE;
2984 + if (copy_from_user
2985 + (&args, (const void __user *)arg,
2986 + sizeof(args)) != 0) {
2991 + service = find_service_for_instance(instance, args.handle);
2997 + if (args.mode == VCHIQ_BULK_MODE_BLOCKING) {
2998 + waiter = kzalloc(sizeof(struct bulk_waiter_node),
3004 + args.userdata = &waiter->bulk_waiter;
3005 + } else if (args.mode == VCHIQ_BULK_MODE_WAITING) {
3006 + struct list_head *pos;
3007 + mutex_lock(&instance->bulk_waiter_list_mutex);
3008 + list_for_each(pos, &instance->bulk_waiter_list) {
3009 + if (list_entry(pos, struct bulk_waiter_node,
3010 + list)->pid == current->pid) {
3011 + waiter = list_entry(pos,
3012 + struct bulk_waiter_node,
3019 + mutex_unlock(&instance->bulk_waiter_list_mutex);
3021 + vchiq_log_error(vchiq_arm_log_level,
3022 + "no bulk_waiter found for pid %d",
3027 + vchiq_log_info(vchiq_arm_log_level,
3028 + "found bulk_waiter %x for pid %d",
3029 + (unsigned int)waiter, current->pid);
3030 + args.userdata = &waiter->bulk_waiter;
3032 + status = vchiq_bulk_transfer
3034 + VCHI_MEM_HANDLE_INVALID,
3035 + args.data, args.size,
3036 + args.userdata, args.mode,
3040 + if ((status != VCHIQ_RETRY) || fatal_signal_pending(current) ||
3041 + !waiter->bulk_waiter.bulk) {
3042 + if (waiter->bulk_waiter.bulk) {
3043 + /* Cancel the signal when the transfer
3045 + spin_lock(&bulk_waiter_spinlock);
3046 + waiter->bulk_waiter.bulk->userdata = NULL;
3047 + spin_unlock(&bulk_waiter_spinlock);
3051 + const VCHIQ_BULK_MODE_T mode_waiting =
3052 + VCHIQ_BULK_MODE_WAITING;
3053 + waiter->pid = current->pid;
3054 + mutex_lock(&instance->bulk_waiter_list_mutex);
3055 + list_add(&waiter->list, &instance->bulk_waiter_list);
3056 + mutex_unlock(&instance->bulk_waiter_list_mutex);
3057 + vchiq_log_info(vchiq_arm_log_level,
3058 + "saved bulk_waiter %x for pid %d",
3059 + (unsigned int)waiter, current->pid);
3061 + if (copy_to_user((void __user *)
3062 + &(((VCHIQ_QUEUE_BULK_TRANSFER_T __user *)
3064 + (const void *)&mode_waiting,
3065 + sizeof(mode_waiting)) != 0)
3070 + case VCHIQ_IOC_AWAIT_COMPLETION: {
3071 + VCHIQ_AWAIT_COMPLETION_T args;
3073 + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
3074 + if (!instance->connected) {
3079 + if (copy_from_user(&args, (const void __user *)arg,
3080 + sizeof(args)) != 0) {
3085 + mutex_lock(&instance->completion_mutex);
3087 + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
3088 + while ((instance->completion_remove ==
3089 + instance->completion_insert)
3090 + && !instance->closing) {
3092 + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
3093 + mutex_unlock(&instance->completion_mutex);
3094 + rc = down_interruptible(&instance->insert_event);
3095 + mutex_lock(&instance->completion_mutex);
3097 + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
3098 + vchiq_log_info(vchiq_arm_log_level,
3099 + "AWAIT_COMPLETION interrupted");
3104 + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
3106 + /* A read memory barrier is needed to stop prefetch of a stale
3107 + ** completion record
3112 + int msgbufcount = args.msgbufcount;
3113 + for (ret = 0; ret < args.count; ret++) {
3114 + VCHIQ_COMPLETION_DATA_T *completion;
3115 + VCHIQ_SERVICE_T *service;
3116 + USER_SERVICE_T *user_service;
3117 + VCHIQ_HEADER_T *header;
3118 + if (instance->completion_remove ==
3119 + instance->completion_insert)
3121 + completion = &instance->completions[
3122 + instance->completion_remove &
3123 + (MAX_COMPLETIONS - 1)];
3125 + service = completion->service_userdata;
3126 + user_service = service->base.userdata;
3127 + completion->service_userdata =
3128 + user_service->userdata;
3130 + header = completion->header;
3132 + void __user *msgbuf;
3135 + msglen = header->size +
3136 + sizeof(VCHIQ_HEADER_T);
3137 + /* This must be a VCHIQ-style service */
3138 + if (args.msgbufsize < msglen) {
3140 + vchiq_arm_log_level,
3141 + "header %x: msgbufsize"
3142 + " %x < msglen %x",
3143 + (unsigned int)header,
3146 + WARN(1, "invalid message "
3152 + if (msgbufcount <= 0)
3153 + /* Stall here for lack of a
3154 + ** buffer for the message. */
3156 + /* Get the pointer from user space */
3158 + if (copy_from_user(&msgbuf,
3159 + (const void __user *)
3160 + &args.msgbufs[msgbufcount],
3161 + sizeof(msgbuf)) != 0) {
3167 + /* Copy the message to user space */
3168 + if (copy_to_user(msgbuf, header,
3175 + /* Now it has been copied, the message
3176 + ** can be released. */
3177 + vchiq_release_message(service->handle,
3180 + /* The completion must point to the
3182 + completion->header = msgbuf;
3185 + if ((completion->reason ==
3186 + VCHIQ_SERVICE_CLOSED) &&
3187 + !instance->use_close_delivered)
3188 + unlock_service(service);
3190 + if (copy_to_user((void __user *)(
3191 + (size_t)args.buf +
3192 + ret * sizeof(VCHIQ_COMPLETION_DATA_T)),
3194 + sizeof(VCHIQ_COMPLETION_DATA_T)) != 0) {
3200 + instance->completion_remove++;
3203 + if (msgbufcount != args.msgbufcount) {
3204 + if (copy_to_user((void __user *)
3205 + &((VCHIQ_AWAIT_COMPLETION_T *)arg)->
3208 + sizeof(msgbufcount)) != 0) {
3215 + up(&instance->remove_event);
3216 + mutex_unlock(&instance->completion_mutex);
3217 + DEBUG_TRACE(AWAIT_COMPLETION_LINE);
3220 + case VCHIQ_IOC_DEQUEUE_MESSAGE: {
3221 + VCHIQ_DEQUEUE_MESSAGE_T args;
3222 + USER_SERVICE_T *user_service;
3223 + VCHIQ_HEADER_T *header;
3225 + DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
3226 + if (copy_from_user
3227 + (&args, (const void __user *)arg,
3228 + sizeof(args)) != 0) {
3232 + service = find_service_for_instance(instance, args.handle);
3237 + user_service = (USER_SERVICE_T *)service->base.userdata;
3238 + if (user_service->is_vchi == 0) {
3243 + spin_lock(&msg_queue_spinlock);
3244 + if (user_service->msg_remove == user_service->msg_insert) {
3245 + if (!args.blocking) {
3246 + spin_unlock(&msg_queue_spinlock);
3247 + DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
3248 + ret = -EWOULDBLOCK;
3251 + user_service->dequeue_pending = 1;
3253 + spin_unlock(&msg_queue_spinlock);
3254 + DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
3255 + if (down_interruptible(
3256 + &user_service->insert_event) != 0) {
3257 + vchiq_log_info(vchiq_arm_log_level,
3258 + "DEQUEUE_MESSAGE interrupted");
3262 + spin_lock(&msg_queue_spinlock);
3263 + } while (user_service->msg_remove ==
3264 + user_service->msg_insert);
3270 + BUG_ON((int)(user_service->msg_insert -
3271 + user_service->msg_remove) < 0);
3273 + header = user_service->msg_queue[user_service->msg_remove &
3274 + (MSG_QUEUE_SIZE - 1)];
3275 + user_service->msg_remove++;
3276 + spin_unlock(&msg_queue_spinlock);
3278 + up(&user_service->remove_event);
3279 + if (header == NULL)
3281 + else if (header->size <= args.bufsize) {
3282 + /* Copy to user space if msgbuf is not NULL */
3283 + if ((args.buf == NULL) ||
3284 + (copy_to_user((void __user *)args.buf,
3286 + header->size) == 0)) {
3287 + ret = header->size;
3288 + vchiq_release_message(
3294 + vchiq_log_error(vchiq_arm_log_level,
3295 + "header %x: bufsize %x < size %x",
3296 + (unsigned int)header, args.bufsize,
3298 + WARN(1, "invalid size\n");
3301 + DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
3304 + case VCHIQ_IOC_GET_CLIENT_ID: {
3305 + VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
3307 + ret = vchiq_get_client_id(handle);
3310 + case VCHIQ_IOC_GET_CONFIG: {
3311 + VCHIQ_GET_CONFIG_T args;
3312 + VCHIQ_CONFIG_T config;
3314 + if (copy_from_user(&args, (const void __user *)arg,
3315 + sizeof(args)) != 0) {
3319 + if (args.config_size > sizeof(config)) {
3323 + status = vchiq_get_config(instance, args.config_size, &config);
3324 + if (status == VCHIQ_SUCCESS) {
3325 + if (copy_to_user((void __user *)args.pconfig,
3326 + &config, args.config_size) != 0) {
3333 + case VCHIQ_IOC_SET_SERVICE_OPTION: {
3334 + VCHIQ_SET_SERVICE_OPTION_T args;
3336 + if (copy_from_user(
3337 + &args, (const void __user *)arg,
3338 + sizeof(args)) != 0) {
3343 + service = find_service_for_instance(instance, args.handle);
3349 + status = vchiq_set_service_option(
3350 + args.handle, args.option, args.value);
3353 + case VCHIQ_IOC_DUMP_PHYS_MEM: {
3354 + VCHIQ_DUMP_MEM_T args;
3356 + if (copy_from_user
3357 + (&args, (const void __user *)arg,
3358 + sizeof(args)) != 0) {
3362 + dump_phys_mem(args.virt_addr, args.num_bytes);
3365 + case VCHIQ_IOC_LIB_VERSION: {
3366 + unsigned int lib_version = (unsigned int)arg;
3368 + if (lib_version < VCHIQ_VERSION_MIN)
3370 + else if (lib_version >= VCHIQ_VERSION_CLOSE_DELIVERED)
3371 + instance->use_close_delivered = 1;
3374 + case VCHIQ_IOC_CLOSE_DELIVERED: {
3375 + VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
3377 + service = find_closed_service_for_instance(instance, handle);
3378 + if (service != NULL) {
3379 + USER_SERVICE_T *user_service =
3380 + (USER_SERVICE_T *)service->base.userdata;
3381 + close_delivered(user_service);
3393 + unlock_service(service);
3396 + if (status == VCHIQ_ERROR)
3398 + else if (status == VCHIQ_RETRY)
3402 + if ((status == VCHIQ_SUCCESS) && (ret < 0) && (ret != -EINTR) &&
3403 + (ret != -EWOULDBLOCK))
3404 + vchiq_log_info(vchiq_arm_log_level,
3405 + " ioctl instance %lx, cmd %s -> status %d, %ld",
3406 + (unsigned long)instance,
3407 + (_IOC_NR(cmd) <= VCHIQ_IOC_MAX) ?
3408 + ioctl_names[_IOC_NR(cmd)] :
3412 + vchiq_log_trace(vchiq_arm_log_level,
3413 + " ioctl instance %lx, cmd %s -> status %d, %ld",
3414 + (unsigned long)instance,
3415 + (_IOC_NR(cmd) <= VCHIQ_IOC_MAX) ?
3416 + ioctl_names[_IOC_NR(cmd)] :
3423 +/****************************************************************************
3427 +***************************************************************************/
3430 +vchiq_open(struct inode *inode, struct file *file)
3432 + int dev = iminor(inode) & 0x0f;
3433 + vchiq_log_info(vchiq_arm_log_level, "vchiq_open");
3435 + case VCHIQ_MINOR: {
3437 + VCHIQ_STATE_T *state = vchiq_get_state();
3438 + VCHIQ_INSTANCE_T instance;
3441 + vchiq_log_error(vchiq_arm_log_level,
3442 + "vchiq has no connection to VideoCore");
3446 + instance = kzalloc(sizeof(*instance), GFP_KERNEL);
3450 + instance->state = state;
3451 + instance->pid = current->tgid;
3453 + ret = vchiq_debugfs_add_instance(instance);
3459 + sema_init(&instance->insert_event, 0);
3460 + sema_init(&instance->remove_event, 0);
3461 + mutex_init(&instance->completion_mutex);
3462 + mutex_init(&instance->bulk_waiter_list_mutex);
3463 + INIT_LIST_HEAD(&instance->bulk_waiter_list);
3465 + file->private_data = instance;
3469 + vchiq_log_error(vchiq_arm_log_level,
3470 + "Unknown minor device: %d", dev);
3477 +/****************************************************************************
3481 +***************************************************************************/
3484 +vchiq_release(struct inode *inode, struct file *file)
3486 + int dev = iminor(inode) & 0x0f;
3489 + case VCHIQ_MINOR: {
3490 + VCHIQ_INSTANCE_T instance = file->private_data;
3491 + VCHIQ_STATE_T *state = vchiq_get_state();
3492 + VCHIQ_SERVICE_T *service;
3495 + vchiq_log_info(vchiq_arm_log_level,
3496 + "vchiq_release: instance=%lx",
3497 + (unsigned long)instance);
3504 + /* Ensure videocore is awake to allow termination. */
3505 + vchiq_use_internal(instance->state, NULL,
3508 + mutex_lock(&instance->completion_mutex);
3510 + /* Wake the completion thread and ask it to exit */
3511 + instance->closing = 1;
3512 + up(&instance->insert_event);
3514 + mutex_unlock(&instance->completion_mutex);
3516 + /* Wake the slot handler if the completion queue is full. */
3517 + up(&instance->remove_event);
3519 + /* Mark all services for termination... */
3521 + while ((service = next_service_by_instance(state, instance,
3523 + USER_SERVICE_T *user_service = service->base.userdata;
3525 + /* Wake the slot handler if the msg queue is full. */
3526 + up(&user_service->remove_event);
3528 + vchiq_terminate_service_internal(service);
3529 + unlock_service(service);
3532 + /* ...and wait for them to die */
3534 + while ((service = next_service_by_instance(state, instance, &i))
3536 + USER_SERVICE_T *user_service = service->base.userdata;
3538 + down(&service->remove_event);
3540 + BUG_ON(service->srvstate != VCHIQ_SRVSTATE_FREE);
3542 + spin_lock(&msg_queue_spinlock);
3544 + while (user_service->msg_remove !=
3545 + user_service->msg_insert) {
3546 + VCHIQ_HEADER_T *header = user_service->
3547 + msg_queue[user_service->msg_remove &
3548 + (MSG_QUEUE_SIZE - 1)];
3549 + user_service->msg_remove++;
3550 + spin_unlock(&msg_queue_spinlock);
3553 + vchiq_release_message(
3556 + spin_lock(&msg_queue_spinlock);
3559 + spin_unlock(&msg_queue_spinlock);
3561 + unlock_service(service);
3564 + /* Release any closed services */
3565 + while (instance->completion_remove !=
3566 + instance->completion_insert) {
3567 + VCHIQ_COMPLETION_DATA_T *completion;
3568 + VCHIQ_SERVICE_T *service;
3569 + completion = &instance->completions[
3570 + instance->completion_remove &
3571 + (MAX_COMPLETIONS - 1)];
3572 + service = completion->service_userdata;
3573 + if (completion->reason == VCHIQ_SERVICE_CLOSED)
3575 + USER_SERVICE_T *user_service =
3576 + service->base.userdata;
3578 + /* Wake any blocked user-thread */
3579 + if (instance->use_close_delivered)
3580 + up(&user_service->close_event);
3581 + unlock_service(service);
3583 + instance->completion_remove++;
3586 + /* Release the PEER service count. */
3587 + vchiq_release_internal(instance->state, NULL);
3590 + struct list_head *pos, *next;
3591 + list_for_each_safe(pos, next,
3592 + &instance->bulk_waiter_list) {
3593 + struct bulk_waiter_node *waiter;
3594 + waiter = list_entry(pos,
3595 + struct bulk_waiter_node,
3598 + vchiq_log_info(vchiq_arm_log_level,
3599 + "bulk_waiter - cleaned up %x "
3601 + (unsigned int)waiter, waiter->pid);
3606 + vchiq_debugfs_remove_instance(instance);
3609 + file->private_data = NULL;
3613 + vchiq_log_error(vchiq_arm_log_level,
3614 + "Unknown minor device: %d", dev);
3622 +/****************************************************************************
3626 +***************************************************************************/
3629 +vchiq_dump(void *dump_context, const char *str, int len)
3631 + DUMP_CONTEXT_T *context = (DUMP_CONTEXT_T *)dump_context;
3633 + if (context->actual < context->space) {
3635 + if (context->offset > 0) {
3636 + int skip_bytes = min(len, (int)context->offset);
3637 + str += skip_bytes;
3638 + len -= skip_bytes;
3639 + context->offset -= skip_bytes;
3640 + if (context->offset > 0)
3643 + copy_bytes = min(len, (int)(context->space - context->actual));
3644 + if (copy_bytes == 0)
3646 + if (copy_to_user(context->buf + context->actual, str,
3648 + context->actual = -EFAULT;
3649 + context->actual += copy_bytes;
3650 + len -= copy_bytes;
3652 + /* If tne terminating NUL is included in the length, then it
3653 + ** marks the end of a line and should be replaced with a
3654 + ** carriage return. */
3655 + if ((len == 0) && (str[copy_bytes - 1] == '\0')) {
3657 + if (copy_to_user(context->buf + context->actual - 1,
3659 + context->actual = -EFAULT;
3664 +/****************************************************************************
3666 +* vchiq_dump_platform_instance_state
3668 +***************************************************************************/
3671 +vchiq_dump_platform_instances(void *dump_context)
3673 + VCHIQ_STATE_T *state = vchiq_get_state();
3678 + /* There is no list of instances, so instead scan all services,
3679 + marking those that have been dumped. */
3681 + for (i = 0; i < state->unused_service; i++) {
3682 + VCHIQ_SERVICE_T *service = state->services[i];
3683 + VCHIQ_INSTANCE_T instance;
3685 + if (service && (service->base.callback == service_callback)) {
3686 + instance = service->instance;
3688 + instance->mark = 0;
3692 + for (i = 0; i < state->unused_service; i++) {
3693 + VCHIQ_SERVICE_T *service = state->services[i];
3694 + VCHIQ_INSTANCE_T instance;
3696 + if (service && (service->base.callback == service_callback)) {
3697 + instance = service->instance;
3698 + if (instance && !instance->mark) {
3699 + len = snprintf(buf, sizeof(buf),
3700 + "Instance %x: pid %d,%s completions "
3702 + (unsigned int)instance, instance->pid,
3703 + instance->connected ? " connected, " :
3705 + instance->completion_insert -
3706 + instance->completion_remove,
3709 + vchiq_dump(dump_context, buf, len + 1);
3711 + instance->mark = 1;
3717 +/****************************************************************************
3719 +* vchiq_dump_platform_service_state
3721 +***************************************************************************/
3724 +vchiq_dump_platform_service_state(void *dump_context, VCHIQ_SERVICE_T *service)
3726 + USER_SERVICE_T *user_service = (USER_SERVICE_T *)service->base.userdata;
3730 + len = snprintf(buf, sizeof(buf), " instance %x",
3731 + (unsigned int)service->instance);
3733 + if ((service->base.callback == service_callback) &&
3734 + user_service->is_vchi) {
3735 + len += snprintf(buf + len, sizeof(buf) - len,
3736 + ", %d/%d messages",
3737 + user_service->msg_insert - user_service->msg_remove,
3740 + if (user_service->dequeue_pending)
3741 + len += snprintf(buf + len, sizeof(buf) - len,
3742 + " (dequeue pending)");
3745 + vchiq_dump(dump_context, buf, len + 1);
3748 +/****************************************************************************
3752 +***************************************************************************/
3755 +dump_phys_mem(void *virt_addr, uint32_t num_bytes)
3758 + uint8_t *end_virt_addr = virt_addr + num_bytes;
3764 + struct page *page;
3765 + struct page **pages;
3766 + uint8_t *kmapped_virt_ptr;
3768 + /* Align virtAddr and endVirtAddr to 16 byte boundaries. */
3770 + virt_addr = (void *)((unsigned long)virt_addr & ~0x0fuL);
3771 + end_virt_addr = (void *)(((unsigned long)end_virt_addr + 15uL) &
3774 + offset = (int)(long)virt_addr & (PAGE_SIZE - 1);
3775 + end_offset = (int)(long)end_virt_addr & (PAGE_SIZE - 1);
3777 + num_pages = (offset + num_bytes + PAGE_SIZE - 1) / PAGE_SIZE;
3779 + pages = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
3780 + if (pages == NULL) {
3781 + vchiq_log_error(vchiq_arm_log_level,
3782 + "Unable to allocation memory for %d pages\n",
3787 + down_read(¤t->mm->mmap_sem);
3788 + rc = get_user_pages(current, /* task */
3789 + current->mm, /* mm */
3790 + (unsigned long)virt_addr, /* start */
3791 + num_pages, /* len */
3794 + pages, /* pages (array of page pointers) */
3796 + up_read(¤t->mm->mmap_sem);
3801 + while (offset < end_offset) {
3803 + int page_offset = offset % PAGE_SIZE;
3804 + page_idx = offset / PAGE_SIZE;
3806 + if (page_idx != prev_idx) {
3810 + page = pages[page_idx];
3811 + kmapped_virt_ptr = kmap(page);
3813 + prev_idx = page_idx;
3816 + if (vchiq_arm_log_level >= VCHIQ_LOG_TRACE)
3817 + vchiq_log_dump_mem("ph",
3818 + (uint32_t)(unsigned long)&kmapped_virt_ptr[
3820 + &kmapped_virt_ptr[page_offset], 16);
3827 + for (page_idx = 0; page_idx < num_pages; page_idx++)
3828 + page_cache_release(pages[page_idx]);
3833 +/****************************************************************************
3837 +***************************************************************************/
3840 +vchiq_read(struct file *file, char __user *buf,
3841 + size_t count, loff_t *ppos)
3843 + DUMP_CONTEXT_T context;
3844 + context.buf = buf;
3845 + context.actual = 0;
3846 + context.space = count;
3847 + context.offset = *ppos;
3849 + vchiq_dump_state(&context, &g_state);
3851 + *ppos += context.actual;
3853 + return context.actual;
3857 +vchiq_get_state(void)
3860 + if (g_state.remote == NULL)
3861 + printk(KERN_ERR "%s: g_state.remote == NULL\n", __func__);
3862 + else if (g_state.remote->initialised != 1)
3863 + printk(KERN_NOTICE "%s: g_state.remote->initialised != 1 (%d)\n",
3864 + __func__, g_state.remote->initialised);
3866 + return ((g_state.remote != NULL) &&
3867 + (g_state.remote->initialised == 1)) ? &g_state : NULL;
3870 +static const struct file_operations
3872 + .owner = THIS_MODULE,
3873 + .unlocked_ioctl = vchiq_ioctl,
3874 + .open = vchiq_open,
3875 + .release = vchiq_release,
3876 + .read = vchiq_read
3880 + * Autosuspend related functionality
3884 +vchiq_videocore_wanted(VCHIQ_STATE_T *state)
3886 + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
3888 + /* autosuspend not supported - always return wanted */
3890 + else if (arm_state->blocked_count)
3892 + else if (!arm_state->videocore_use_count)
3893 + /* usage count zero - check for override unless we're forcing */
3894 + if (arm_state->resume_blocked)
3897 + return vchiq_platform_videocore_wanted(state);
3899 + /* non-zero usage count - videocore still required */
3903 +static VCHIQ_STATUS_T
3904 +vchiq_keepalive_vchiq_callback(VCHIQ_REASON_T reason,
3905 + VCHIQ_HEADER_T *header,
3906 + VCHIQ_SERVICE_HANDLE_T service_user,
3909 + vchiq_log_error(vchiq_susp_log_level,
3910 + "%s callback reason %d", __func__, reason);
3915 +vchiq_keepalive_thread_func(void *v)
3917 + VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
3918 + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
3920 + VCHIQ_STATUS_T status;
3921 + VCHIQ_INSTANCE_T instance;
3922 + VCHIQ_SERVICE_HANDLE_T ka_handle;
3924 + VCHIQ_SERVICE_PARAMS_T params = {
3925 + .fourcc = VCHIQ_MAKE_FOURCC('K', 'E', 'E', 'P'),
3926 + .callback = vchiq_keepalive_vchiq_callback,
3927 + .version = KEEPALIVE_VER,
3928 + .version_min = KEEPALIVE_VER_MIN
3931 + status = vchiq_initialise(&instance);
3932 + if (status != VCHIQ_SUCCESS) {
3933 + vchiq_log_error(vchiq_susp_log_level,
3934 + "%s vchiq_initialise failed %d", __func__, status);
3938 + status = vchiq_connect(instance);
3939 + if (status != VCHIQ_SUCCESS) {
3940 + vchiq_log_error(vchiq_susp_log_level,
3941 + "%s vchiq_connect failed %d", __func__, status);
3945 + status = vchiq_add_service(instance, ¶ms, &ka_handle);
3946 + if (status != VCHIQ_SUCCESS) {
3947 + vchiq_log_error(vchiq_susp_log_level,
3948 + "%s vchiq_open_service failed %d", __func__, status);
3953 + long rc = 0, uc = 0;
3954 + if (wait_for_completion_interruptible(&arm_state->ka_evt)
3956 + vchiq_log_error(vchiq_susp_log_level,
3957 + "%s interrupted", __func__);
3958 + flush_signals(current);
3962 + /* read and clear counters. Do release_count then use_count to
3963 + * prevent getting more releases than uses */
3964 + rc = atomic_xchg(&arm_state->ka_release_count, 0);
3965 + uc = atomic_xchg(&arm_state->ka_use_count, 0);
3967 + /* Call use/release service the requisite number of times.
3968 + * Process use before release so use counts don't go negative */
3970 + atomic_inc(&arm_state->ka_use_ack_count);
3971 + status = vchiq_use_service(ka_handle);
3972 + if (status != VCHIQ_SUCCESS) {
3973 + vchiq_log_error(vchiq_susp_log_level,
3974 + "%s vchiq_use_service error %d",
3975 + __func__, status);
3979 + status = vchiq_release_service(ka_handle);
3980 + if (status != VCHIQ_SUCCESS) {
3981 + vchiq_log_error(vchiq_susp_log_level,
3982 + "%s vchiq_release_service error %d",
3983 + __func__, status);
3989 + vchiq_shutdown(instance);
3997 +vchiq_arm_init_state(VCHIQ_STATE_T *state, VCHIQ_ARM_STATE_T *arm_state)
3999 + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
4002 + rwlock_init(&arm_state->susp_res_lock);
4004 + init_completion(&arm_state->ka_evt);
4005 + atomic_set(&arm_state->ka_use_count, 0);
4006 + atomic_set(&arm_state->ka_use_ack_count, 0);
4007 + atomic_set(&arm_state->ka_release_count, 0);
4009 + init_completion(&arm_state->vc_suspend_complete);
4011 + init_completion(&arm_state->vc_resume_complete);
4012 + /* Initialise to 'done' state. We only want to block on resume
4013 + * completion while videocore is suspended. */
4014 + set_resume_state(arm_state, VC_RESUME_RESUMED);
4016 + init_completion(&arm_state->resume_blocker);
4017 + /* Initialise to 'done' state. We only want to block on this
4018 + * completion while resume is blocked */
4019 + complete_all(&arm_state->resume_blocker);
4021 + init_completion(&arm_state->blocked_blocker);
4022 + /* Initialise to 'done' state. We only want to block on this
4023 + * completion while things are waiting on the resume blocker */
4024 + complete_all(&arm_state->blocked_blocker);
4026 + arm_state->suspend_timer_timeout = SUSPEND_TIMER_TIMEOUT_MS;
4027 + arm_state->suspend_timer_running = 0;
4028 + init_timer(&arm_state->suspend_timer);
4029 + arm_state->suspend_timer.data = (unsigned long)(state);
4030 + arm_state->suspend_timer.function = suspend_timer_callback;
4032 + arm_state->first_connect = 0;
4039 +** Functions to modify the state variables;
4040 +** set_suspend_state
4041 +** set_resume_state
4043 +** There are more state variables than we might like, so ensure they remain in
4044 +** step. Suspend and resume state are maintained separately, since most of
4045 +** these state machines can operate independently. However, there are a few
4046 +** states where state transitions in one state machine cause a reset to the
4047 +** other state machine. In addition, there are some completion events which
4048 +** need to occur on state machine reset and end-state(s), so these are also
4049 +** dealt with in these functions.
4051 +** In all states we set the state variable according to the input, but in some
4052 +** cases we perform additional steps outlined below;
4054 +** VC_SUSPEND_IDLE - Initialise the suspend completion at the same time.
4055 +** The suspend completion is completed after any suspend
4056 +** attempt. When we reset the state machine we also reset
4057 +** the completion. This reset occurs when videocore is
4058 +** resumed, and also if we initiate suspend after a suspend
4061 +** VC_SUSPEND_IN_PROGRESS - This state is considered the point of no return for
4062 +** suspend - ie from this point on we must try to suspend
4063 +** before resuming can occur. We therefore also reset the
4064 +** resume state machine to VC_RESUME_IDLE in this state.
4066 +** VC_SUSPEND_SUSPENDED - Suspend has completed successfully. Also call
4067 +** complete_all on the suspend completion to notify
4068 +** anything waiting for suspend to happen.
4070 +** VC_SUSPEND_REJECTED - Videocore rejected suspend. Videocore will also
4071 +** initiate resume, so no need to alter resume state.
4072 +** We call complete_all on the suspend completion to notify
4073 +** of suspend rejection.
4075 +** VC_SUSPEND_FAILED - We failed to initiate videocore suspend. We notify the
4076 +** suspend completion and reset the resume state machine.
4078 +** VC_RESUME_IDLE - Initialise the resume completion at the same time. The
4079 +** resume completion is in it's 'done' state whenever
4080 +** videcore is running. Therfore, the VC_RESUME_IDLE state
4081 +** implies that videocore is suspended.
4082 +** Hence, any thread which needs to wait until videocore is
4083 +** running can wait on this completion - it will only block
4084 +** if videocore is suspended.
4086 +** VC_RESUME_RESUMED - Resume has completed successfully. Videocore is running.
4087 +** Call complete_all on the resume completion to unblock
4088 +** any threads waiting for resume. Also reset the suspend
4089 +** state machine to it's idle state.
4091 +** VC_RESUME_FAILED - Currently unused - no mechanism to fail resume exists.
4095 +set_suspend_state(VCHIQ_ARM_STATE_T *arm_state,
4096 + enum vc_suspend_status new_state)
4098 + /* set the state in all cases */
4099 + arm_state->vc_suspend_state = new_state;
4101 + /* state specific additional actions */
4102 + switch (new_state) {
4103 + case VC_SUSPEND_FORCE_CANCELED:
4104 + complete_all(&arm_state->vc_suspend_complete);
4106 + case VC_SUSPEND_REJECTED:
4107 + complete_all(&arm_state->vc_suspend_complete);
4109 + case VC_SUSPEND_FAILED:
4110 + complete_all(&arm_state->vc_suspend_complete);
4111 + arm_state->vc_resume_state = VC_RESUME_RESUMED;
4112 + complete_all(&arm_state->vc_resume_complete);
4114 + case VC_SUSPEND_IDLE:
4115 + reinit_completion(&arm_state->vc_suspend_complete);
4117 + case VC_SUSPEND_REQUESTED:
4119 + case VC_SUSPEND_IN_PROGRESS:
4120 + set_resume_state(arm_state, VC_RESUME_IDLE);
4122 + case VC_SUSPEND_SUSPENDED:
4123 + complete_all(&arm_state->vc_suspend_complete);
4132 +set_resume_state(VCHIQ_ARM_STATE_T *arm_state,
4133 + enum vc_resume_status new_state)
4135 + /* set the state in all cases */
4136 + arm_state->vc_resume_state = new_state;
4138 + /* state specific additional actions */
4139 + switch (new_state) {
4140 + case VC_RESUME_FAILED:
4142 + case VC_RESUME_IDLE:
4143 + reinit_completion(&arm_state->vc_resume_complete);
4145 + case VC_RESUME_REQUESTED:
4147 + case VC_RESUME_IN_PROGRESS:
4149 + case VC_RESUME_RESUMED:
4150 + complete_all(&arm_state->vc_resume_complete);
4151 + set_suspend_state(arm_state, VC_SUSPEND_IDLE);
4160 +/* should be called with the write lock held */
4162 +start_suspend_timer(VCHIQ_ARM_STATE_T *arm_state)
4164 + del_timer(&arm_state->suspend_timer);
4165 + arm_state->suspend_timer.expires = jiffies +
4166 + msecs_to_jiffies(arm_state->
4167 + suspend_timer_timeout);
4168 + add_timer(&arm_state->suspend_timer);
4169 + arm_state->suspend_timer_running = 1;
4172 +/* should be called with the write lock held */
4174 +stop_suspend_timer(VCHIQ_ARM_STATE_T *arm_state)
4176 + if (arm_state->suspend_timer_running) {
4177 + del_timer(&arm_state->suspend_timer);
4178 + arm_state->suspend_timer_running = 0;
4183 +need_resume(VCHIQ_STATE_T *state)
4185 + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
4186 + return (arm_state->vc_suspend_state > VC_SUSPEND_IDLE) &&
4187 + (arm_state->vc_resume_state < VC_RESUME_REQUESTED) &&
4188 + vchiq_videocore_wanted(state);
4192 +block_resume(VCHIQ_ARM_STATE_T *arm_state)
4194 + int status = VCHIQ_SUCCESS;
4195 + const unsigned long timeout_val =
4196 + msecs_to_jiffies(FORCE_SUSPEND_TIMEOUT_MS);
4197 + int resume_count = 0;
4199 + /* Allow any threads which were blocked by the last force suspend to
4200 + * complete if they haven't already. Only give this one shot; if
4201 + * blocked_count is incremented after blocked_blocker is completed
4202 + * (which only happens when blocked_count hits 0) then those threads
4203 + * will have to wait until next time around */
4204 + if (arm_state->blocked_count) {
4205 + reinit_completion(&arm_state->blocked_blocker);
4206 + write_unlock_bh(&arm_state->susp_res_lock);
4207 + vchiq_log_info(vchiq_susp_log_level, "%s wait for previously "
4208 + "blocked clients", __func__);
4209 + if (wait_for_completion_interruptible_timeout(
4210 + &arm_state->blocked_blocker, timeout_val)
4212 + vchiq_log_error(vchiq_susp_log_level, "%s wait for "
4213 + "previously blocked clients failed" , __func__);
4214 + status = VCHIQ_ERROR;
4215 + write_lock_bh(&arm_state->susp_res_lock);
4218 + vchiq_log_info(vchiq_susp_log_level, "%s previously blocked "
4219 + "clients resumed", __func__);
4220 + write_lock_bh(&arm_state->susp_res_lock);
4223 + /* We need to wait for resume to complete if it's in process */
4224 + while (arm_state->vc_resume_state != VC_RESUME_RESUMED &&
4225 + arm_state->vc_resume_state > VC_RESUME_IDLE) {
4226 + if (resume_count > 1) {
4227 + status = VCHIQ_ERROR;
4228 + vchiq_log_error(vchiq_susp_log_level, "%s waited too "
4229 + "many times for resume" , __func__);
4232 + write_unlock_bh(&arm_state->susp_res_lock);
4233 + vchiq_log_info(vchiq_susp_log_level, "%s wait for resume",
4235 + if (wait_for_completion_interruptible_timeout(
4236 + &arm_state->vc_resume_complete, timeout_val)
4238 + vchiq_log_error(vchiq_susp_log_level, "%s wait for "
4239 + "resume failed (%s)", __func__,
4240 + resume_state_names[arm_state->vc_resume_state +
4241 + VC_RESUME_NUM_OFFSET]);
4242 + status = VCHIQ_ERROR;
4243 + write_lock_bh(&arm_state->susp_res_lock);
4246 + vchiq_log_info(vchiq_susp_log_level, "%s resumed", __func__);
4247 + write_lock_bh(&arm_state->susp_res_lock);
4250 + reinit_completion(&arm_state->resume_blocker);
4251 + arm_state->resume_blocked = 1;
4258 +unblock_resume(VCHIQ_ARM_STATE_T *arm_state)
4260 + complete_all(&arm_state->resume_blocker);
4261 + arm_state->resume_blocked = 0;
4264 +/* Initiate suspend via slot handler. Should be called with the write lock
4267 +vchiq_arm_vcsuspend(VCHIQ_STATE_T *state)
4269 + VCHIQ_STATUS_T status = VCHIQ_ERROR;
4270 + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
4275 + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
4276 + status = VCHIQ_SUCCESS;
4279 + switch (arm_state->vc_suspend_state) {
4280 + case VC_SUSPEND_REQUESTED:
4281 + vchiq_log_info(vchiq_susp_log_level, "%s: suspend already "
4282 + "requested", __func__);
4284 + case VC_SUSPEND_IN_PROGRESS:
4285 + vchiq_log_info(vchiq_susp_log_level, "%s: suspend already in "
4286 + "progress", __func__);
4290 + /* We don't expect to be in other states, so log but continue
4292 + vchiq_log_error(vchiq_susp_log_level,
4293 + "%s unexpected suspend state %s", __func__,
4294 + suspend_state_names[arm_state->vc_suspend_state +
4295 + VC_SUSPEND_NUM_OFFSET]);
4296 + /* fall through */
4297 + case VC_SUSPEND_REJECTED:
4298 + case VC_SUSPEND_FAILED:
4299 + /* Ensure any idle state actions have been run */
4300 + set_suspend_state(arm_state, VC_SUSPEND_IDLE);
4301 + /* fall through */
4302 + case VC_SUSPEND_IDLE:
4303 + vchiq_log_info(vchiq_susp_log_level,
4304 + "%s: suspending", __func__);
4305 + set_suspend_state(arm_state, VC_SUSPEND_REQUESTED);
4306 + /* kick the slot handler thread to initiate suspend */
4307 + request_poll(state, NULL, 0);
4312 + vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, status);
4317 +vchiq_platform_check_suspend(VCHIQ_STATE_T *state)
4319 + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
4325 + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
4327 + write_lock_bh(&arm_state->susp_res_lock);
4328 + if (arm_state->vc_suspend_state == VC_SUSPEND_REQUESTED &&
4329 + arm_state->vc_resume_state == VC_RESUME_RESUMED) {
4330 + set_suspend_state(arm_state, VC_SUSPEND_IN_PROGRESS);
4333 + write_unlock_bh(&arm_state->susp_res_lock);
4336 + vchiq_platform_suspend(state);
4339 + vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
4345 +output_timeout_error(VCHIQ_STATE_T *state)
4347 + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
4348 + char service_err[50] = "";
4349 + int vc_use_count = arm_state->videocore_use_count;
4350 + int active_services = state->unused_service;
4353 + if (!arm_state->videocore_use_count) {
4354 + snprintf(service_err, 50, " Videocore usecount is 0");
4357 + for (i = 0; i < active_services; i++) {
4358 + VCHIQ_SERVICE_T *service_ptr = state->services[i];
4359 + if (service_ptr && service_ptr->service_use_count &&
4360 + (service_ptr->srvstate != VCHIQ_SRVSTATE_FREE)) {
4361 + snprintf(service_err, 50, " %c%c%c%c(%d) service has "
4362 + "use count %d%s", VCHIQ_FOURCC_AS_4CHARS(
4363 + service_ptr->base.fourcc),
4364 + service_ptr->client_id,
4365 + service_ptr->service_use_count,
4366 + service_ptr->service_use_count ==
4367 + vc_use_count ? "" : " (+ more)");
4373 + vchiq_log_error(vchiq_susp_log_level,
4374 + "timed out waiting for vc suspend (%d).%s",
4375 + arm_state->autosuspend_override, service_err);
4379 +/* Try to get videocore into suspended state, regardless of autosuspend state.
4380 +** We don't actually force suspend, since videocore may get into a bad state
4381 +** if we force suspend at a bad time. Instead, we wait for autosuspend to
4382 +** determine a good point to suspend. If this doesn't happen within 100ms we
4385 +** Returns VCHIQ_SUCCESS if videocore suspended successfully, VCHIQ_RETRY if
4386 +** videocore failed to suspend in time or VCHIQ_ERROR if interrupted.
4389 +vchiq_arm_force_suspend(VCHIQ_STATE_T *state)
4391 + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
4392 + VCHIQ_STATUS_T status = VCHIQ_ERROR;
4399 + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
4401 + write_lock_bh(&arm_state->susp_res_lock);
4403 + status = block_resume(arm_state);
4404 + if (status != VCHIQ_SUCCESS)
4406 + if (arm_state->vc_suspend_state == VC_SUSPEND_SUSPENDED) {
4407 + /* Already suspended - just block resume and exit */
4408 + vchiq_log_info(vchiq_susp_log_level, "%s already suspended",
4410 + status = VCHIQ_SUCCESS;
4412 + } else if (arm_state->vc_suspend_state <= VC_SUSPEND_IDLE) {
4413 + /* initiate suspend immediately in the case that we're waiting
4414 + * for the timeout */
4415 + stop_suspend_timer(arm_state);
4416 + if (!vchiq_videocore_wanted(state)) {
4417 + vchiq_log_info(vchiq_susp_log_level, "%s videocore "
4418 + "idle, initiating suspend", __func__);
4419 + status = vchiq_arm_vcsuspend(state);
4420 + } else if (arm_state->autosuspend_override <
4421 + FORCE_SUSPEND_FAIL_MAX) {
4422 + vchiq_log_info(vchiq_susp_log_level, "%s letting "
4423 + "videocore go idle", __func__);
4424 + status = VCHIQ_SUCCESS;
4426 + vchiq_log_warning(vchiq_susp_log_level, "%s failed too "
4427 + "many times - attempting suspend", __func__);
4428 + status = vchiq_arm_vcsuspend(state);
4431 + vchiq_log_info(vchiq_susp_log_level, "%s videocore suspend "
4432 + "in progress - wait for completion", __func__);
4433 + status = VCHIQ_SUCCESS;
4436 + /* Wait for suspend to happen due to system idle (not forced..) */
4437 + if (status != VCHIQ_SUCCESS)
4438 + goto unblock_resume;
4441 + write_unlock_bh(&arm_state->susp_res_lock);
4443 + rc = wait_for_completion_interruptible_timeout(
4444 + &arm_state->vc_suspend_complete,
4445 + msecs_to_jiffies(FORCE_SUSPEND_TIMEOUT_MS));
4447 + write_lock_bh(&arm_state->susp_res_lock);
4449 + vchiq_log_warning(vchiq_susp_log_level, "%s "
4450 + "interrupted waiting for suspend", __func__);
4451 + status = VCHIQ_ERROR;
4452 + goto unblock_resume;
4453 + } else if (rc == 0) {
4454 + if (arm_state->vc_suspend_state > VC_SUSPEND_IDLE) {
4455 + /* Repeat timeout once if in progress */
4461 + arm_state->autosuspend_override++;
4462 + output_timeout_error(state);
4464 + status = VCHIQ_RETRY;
4465 + goto unblock_resume;
4467 + } while (0 < (repeat--));
4469 + /* Check and report state in case we need to abort ARM suspend */
4470 + if (arm_state->vc_suspend_state != VC_SUSPEND_SUSPENDED) {
4471 + status = VCHIQ_RETRY;
4472 + vchiq_log_error(vchiq_susp_log_level,
4473 + "%s videocore suspend failed (state %s)", __func__,
4474 + suspend_state_names[arm_state->vc_suspend_state +
4475 + VC_SUSPEND_NUM_OFFSET]);
4476 + /* Reset the state only if it's still in an error state.
4477 + * Something could have already initiated another suspend. */
4478 + if (arm_state->vc_suspend_state < VC_SUSPEND_IDLE)
4479 + set_suspend_state(arm_state, VC_SUSPEND_IDLE);
4481 + goto unblock_resume;
4484 + /* successfully suspended - unlock and exit */
4488 + /* all error states need to unblock resume before exit */
4489 + unblock_resume(arm_state);
4492 + write_unlock_bh(&arm_state->susp_res_lock);
4495 + vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, status);
4500 +vchiq_check_suspend(VCHIQ_STATE_T *state)
4502 + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
4507 + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
4509 + write_lock_bh(&arm_state->susp_res_lock);
4510 + if (arm_state->vc_suspend_state != VC_SUSPEND_SUSPENDED &&
4511 + arm_state->first_connect &&
4512 + !vchiq_videocore_wanted(state)) {
4513 + vchiq_arm_vcsuspend(state);
4515 + write_unlock_bh(&arm_state->susp_res_lock);
4518 + vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
4524 +vchiq_arm_allow_resume(VCHIQ_STATE_T *state)
4526 + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
4533 + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
4535 + write_lock_bh(&arm_state->susp_res_lock);
4536 + unblock_resume(arm_state);
4537 + resume = vchiq_check_resume(state);
4538 + write_unlock_bh(&arm_state->susp_res_lock);
4541 + if (wait_for_completion_interruptible(
4542 + &arm_state->vc_resume_complete) < 0) {
4543 + vchiq_log_error(vchiq_susp_log_level,
4544 + "%s interrupted", __func__);
4545 + /* failed, cannot accurately derive suspend
4546 + * state, so exit early. */
4551 + read_lock_bh(&arm_state->susp_res_lock);
4552 + if (arm_state->vc_suspend_state == VC_SUSPEND_SUSPENDED) {
4553 + vchiq_log_info(vchiq_susp_log_level,
4554 + "%s: Videocore remains suspended", __func__);
4556 + vchiq_log_info(vchiq_susp_log_level,
4557 + "%s: Videocore resumed", __func__);
4560 + read_unlock_bh(&arm_state->susp_res_lock);
4562 + vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, ret);
4566 +/* This function should be called with the write lock held */
4568 +vchiq_check_resume(VCHIQ_STATE_T *state)
4570 + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
4576 + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
4578 + if (need_resume(state)) {
4579 + set_resume_state(arm_state, VC_RESUME_REQUESTED);
4580 + request_poll(state, NULL, 0);
4585 + vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
4590 +vchiq_platform_check_resume(VCHIQ_STATE_T *state)
4592 + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
4598 + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
4600 + write_lock_bh(&arm_state->susp_res_lock);
4601 + if (arm_state->wake_address == 0) {
4602 + vchiq_log_info(vchiq_susp_log_level,
4603 + "%s: already awake", __func__);
4606 + if (arm_state->vc_resume_state == VC_RESUME_IN_PROGRESS) {
4607 + vchiq_log_info(vchiq_susp_log_level,
4608 + "%s: already resuming", __func__);
4612 + if (arm_state->vc_resume_state == VC_RESUME_REQUESTED) {
4613 + set_resume_state(arm_state, VC_RESUME_IN_PROGRESS);
4616 + vchiq_log_trace(vchiq_susp_log_level,
4617 + "%s: not resuming (resume state %s)", __func__,
4618 + resume_state_names[arm_state->vc_resume_state +
4619 + VC_RESUME_NUM_OFFSET]);
4622 + write_unlock_bh(&arm_state->susp_res_lock);
4625 + vchiq_platform_resume(state);
4628 + vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
4636 +vchiq_use_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service,
4637 + enum USE_TYPE_E use_type)
4639 + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
4640 + VCHIQ_STATUS_T ret = VCHIQ_SUCCESS;
4643 + int local_uc, local_entity_uc;
4648 + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
4650 + if (use_type == USE_TYPE_VCHIQ) {
4651 + sprintf(entity, "VCHIQ: ");
4652 + entity_uc = &arm_state->peer_use_count;
4653 + } else if (service) {
4654 + sprintf(entity, "%c%c%c%c:%03d",
4655 + VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
4656 + service->client_id);
4657 + entity_uc = &service->service_use_count;
4659 + vchiq_log_error(vchiq_susp_log_level, "%s null service "
4661 + ret = VCHIQ_ERROR;
4665 + write_lock_bh(&arm_state->susp_res_lock);
4666 + while (arm_state->resume_blocked) {
4667 + /* If we call 'use' while force suspend is waiting for suspend,
4668 + * then we're about to block the thread which the force is
4669 + * waiting to complete, so we're bound to just time out. In this
4670 + * case, set the suspend state such that the wait will be
4671 + * canceled, so we can complete as quickly as possible. */
4672 + if (arm_state->resume_blocked && arm_state->vc_suspend_state ==
4673 + VC_SUSPEND_IDLE) {
4674 + set_suspend_state(arm_state, VC_SUSPEND_FORCE_CANCELED);
4677 + /* If suspend is already in progress then we need to block */
4678 + if (!try_wait_for_completion(&arm_state->resume_blocker)) {
4679 + /* Indicate that there are threads waiting on the resume
4680 + * blocker. These need to be allowed to complete before
4681 + * a _second_ call to force suspend can complete,
4682 + * otherwise low priority threads might never actually
4684 + arm_state->blocked_count++;
4685 + write_unlock_bh(&arm_state->susp_res_lock);
4686 + vchiq_log_info(vchiq_susp_log_level, "%s %s resume "
4687 + "blocked - waiting...", __func__, entity);
4688 + if (wait_for_completion_killable(
4689 + &arm_state->resume_blocker) != 0) {
4690 + vchiq_log_error(vchiq_susp_log_level, "%s %s "
4691 + "wait for resume blocker interrupted",
4692 + __func__, entity);
4693 + ret = VCHIQ_ERROR;
4694 + write_lock_bh(&arm_state->susp_res_lock);
4695 + arm_state->blocked_count--;
4696 + write_unlock_bh(&arm_state->susp_res_lock);
4699 + vchiq_log_info(vchiq_susp_log_level, "%s %s resume "
4700 + "unblocked", __func__, entity);
4701 + write_lock_bh(&arm_state->susp_res_lock);
4702 + if (--arm_state->blocked_count == 0)
4703 + complete_all(&arm_state->blocked_blocker);
4707 + stop_suspend_timer(arm_state);
4709 + local_uc = ++arm_state->videocore_use_count;
4710 + local_entity_uc = ++(*entity_uc);
4712 + /* If there's a pending request which hasn't yet been serviced then
4713 + * just clear it. If we're past VC_SUSPEND_REQUESTED state then
4714 + * vc_resume_complete will block until we either resume or fail to
4716 + if (arm_state->vc_suspend_state <= VC_SUSPEND_REQUESTED)
4717 + set_suspend_state(arm_state, VC_SUSPEND_IDLE);
4719 + if ((use_type != USE_TYPE_SERVICE_NO_RESUME) && need_resume(state)) {
4720 + set_resume_state(arm_state, VC_RESUME_REQUESTED);
4721 + vchiq_log_info(vchiq_susp_log_level,
4722 + "%s %s count %d, state count %d",
4723 + __func__, entity, local_entity_uc, local_uc);
4724 + request_poll(state, NULL, 0);
4726 + vchiq_log_trace(vchiq_susp_log_level,
4727 + "%s %s count %d, state count %d",
4728 + __func__, entity, *entity_uc, local_uc);
4731 + write_unlock_bh(&arm_state->susp_res_lock);
4733 + /* Completion is in a done state when we're not suspended, so this won't
4734 + * block for the non-suspended case. */
4735 + if (!try_wait_for_completion(&arm_state->vc_resume_complete)) {
4736 + vchiq_log_info(vchiq_susp_log_level, "%s %s wait for resume",
4737 + __func__, entity);
4738 + if (wait_for_completion_killable(
4739 + &arm_state->vc_resume_complete) != 0) {
4740 + vchiq_log_error(vchiq_susp_log_level, "%s %s wait for "
4741 + "resume interrupted", __func__, entity);
4742 + ret = VCHIQ_ERROR;
4745 + vchiq_log_info(vchiq_susp_log_level, "%s %s resumed", __func__,
4749 + if (ret == VCHIQ_SUCCESS) {
4750 + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
4751 + long ack_cnt = atomic_xchg(&arm_state->ka_use_ack_count, 0);
4752 + while (ack_cnt && (status == VCHIQ_SUCCESS)) {
4753 + /* Send the use notify to videocore */
4754 + status = vchiq_send_remote_use_active(state);
4755 + if (status == VCHIQ_SUCCESS)
4758 + atomic_add(ack_cnt,
4759 + &arm_state->ka_use_ack_count);
4764 + vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, ret);
4769 +vchiq_release_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service)
4771 + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
4772 + VCHIQ_STATUS_T ret = VCHIQ_SUCCESS;
4775 + int local_uc, local_entity_uc;
4780 + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
4783 + sprintf(entity, "%c%c%c%c:%03d",
4784 + VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
4785 + service->client_id);
4786 + entity_uc = &service->service_use_count;
4788 + sprintf(entity, "PEER: ");
4789 + entity_uc = &arm_state->peer_use_count;
4792 + write_lock_bh(&arm_state->susp_res_lock);
4793 + if (!arm_state->videocore_use_count || !(*entity_uc)) {
4794 + /* Don't use BUG_ON - don't allow user thread to crash kernel */
4795 + WARN_ON(!arm_state->videocore_use_count);
4796 + WARN_ON(!(*entity_uc));
4797 + ret = VCHIQ_ERROR;
4800 + local_uc = --arm_state->videocore_use_count;
4801 + local_entity_uc = --(*entity_uc);
4803 + if (!vchiq_videocore_wanted(state)) {
4804 + if (vchiq_platform_use_suspend_timer() &&
4805 + !arm_state->resume_blocked) {
4806 + /* Only use the timer if we're not trying to force
4807 + * suspend (=> resume_blocked) */
4808 + start_suspend_timer(arm_state);
4810 + vchiq_log_info(vchiq_susp_log_level,
4811 + "%s %s count %d, state count %d - suspending",
4812 + __func__, entity, *entity_uc,
4813 + arm_state->videocore_use_count);
4814 + vchiq_arm_vcsuspend(state);
4817 + vchiq_log_trace(vchiq_susp_log_level,
4818 + "%s %s count %d, state count %d",
4819 + __func__, entity, *entity_uc,
4820 + arm_state->videocore_use_count);
4823 + write_unlock_bh(&arm_state->susp_res_lock);
4826 + vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, ret);
4831 +vchiq_on_remote_use(VCHIQ_STATE_T *state)
4833 + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
4834 + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
4835 + atomic_inc(&arm_state->ka_use_count);
4836 + complete(&arm_state->ka_evt);
4840 +vchiq_on_remote_release(VCHIQ_STATE_T *state)
4842 + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
4843 + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
4844 + atomic_inc(&arm_state->ka_release_count);
4845 + complete(&arm_state->ka_evt);
4849 +vchiq_use_service_internal(VCHIQ_SERVICE_T *service)
4851 + return vchiq_use_internal(service->state, service, USE_TYPE_SERVICE);
4855 +vchiq_release_service_internal(VCHIQ_SERVICE_T *service)
4857 + return vchiq_release_internal(service->state, service);
4860 +VCHIQ_DEBUGFS_NODE_T *
4861 +vchiq_instance_get_debugfs_node(VCHIQ_INSTANCE_T instance)
4863 + return &instance->debugfs_node;
4867 +vchiq_instance_get_use_count(VCHIQ_INSTANCE_T instance)
4869 + VCHIQ_SERVICE_T *service;
4870 + int use_count = 0, i;
4872 + while ((service = next_service_by_instance(instance->state,
4873 + instance, &i)) != NULL) {
4874 + use_count += service->service_use_count;
4875 + unlock_service(service);
4881 +vchiq_instance_get_pid(VCHIQ_INSTANCE_T instance)
4883 + return instance->pid;
4887 +vchiq_instance_get_trace(VCHIQ_INSTANCE_T instance)
4889 + return instance->trace;
4893 +vchiq_instance_set_trace(VCHIQ_INSTANCE_T instance, int trace)
4895 + VCHIQ_SERVICE_T *service;
4898 + while ((service = next_service_by_instance(instance->state,
4899 + instance, &i)) != NULL) {
4900 + service->trace = trace;
4901 + unlock_service(service);
4903 + instance->trace = (trace != 0);
4906 +static void suspend_timer_callback(unsigned long context)
4908 + VCHIQ_STATE_T *state = (VCHIQ_STATE_T *)context;
4909 + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
4912 + vchiq_log_info(vchiq_susp_log_level,
4913 + "%s - suspend timer expired - check suspend", __func__);
4914 + vchiq_check_suspend(state);
4920 +vchiq_use_service_no_resume(VCHIQ_SERVICE_HANDLE_T handle)
4922 + VCHIQ_STATUS_T ret = VCHIQ_ERROR;
4923 + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
4925 + ret = vchiq_use_internal(service->state, service,
4926 + USE_TYPE_SERVICE_NO_RESUME);
4927 + unlock_service(service);
4933 +vchiq_use_service(VCHIQ_SERVICE_HANDLE_T handle)
4935 + VCHIQ_STATUS_T ret = VCHIQ_ERROR;
4936 + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
4938 + ret = vchiq_use_internal(service->state, service,
4939 + USE_TYPE_SERVICE);
4940 + unlock_service(service);
4946 +vchiq_release_service(VCHIQ_SERVICE_HANDLE_T handle)
4948 + VCHIQ_STATUS_T ret = VCHIQ_ERROR;
4949 + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
4951 + ret = vchiq_release_internal(service->state, service);
4952 + unlock_service(service);
4958 +vchiq_dump_service_use_state(VCHIQ_STATE_T *state)
4960 + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
4962 + /* Only dump 64 services */
4963 + static const int local_max_services = 64;
4964 + /* If there's more than 64 services, only dump ones with
4965 + * non-zero counts */
4966 + int only_nonzero = 0;
4967 + static const char *nz = "<-- preventing suspend";
4969 + enum vc_suspend_status vc_suspend_state;
4970 + enum vc_resume_status vc_resume_state;
4973 + int active_services;
4974 + struct service_data_struct {
4978 + } service_data[local_max_services];
4983 + read_lock_bh(&arm_state->susp_res_lock);
4984 + vc_suspend_state = arm_state->vc_suspend_state;
4985 + vc_resume_state = arm_state->vc_resume_state;
4986 + peer_count = arm_state->peer_use_count;
4987 + vc_use_count = arm_state->videocore_use_count;
4988 + active_services = state->unused_service;
4989 + if (active_services > local_max_services)
4992 + for (i = 0; (i < active_services) && (j < local_max_services); i++) {
4993 + VCHIQ_SERVICE_T *service_ptr = state->services[i];
4997 + if (only_nonzero && !service_ptr->service_use_count)
5000 + if (service_ptr->srvstate != VCHIQ_SRVSTATE_FREE) {
5001 + service_data[j].fourcc = service_ptr->base.fourcc;
5002 + service_data[j].clientid = service_ptr->client_id;
5003 + service_data[j++].use_count = service_ptr->
5004 + service_use_count;
5008 + read_unlock_bh(&arm_state->susp_res_lock);
5010 + vchiq_log_warning(vchiq_susp_log_level,
5011 + "-- Videcore suspend state: %s --",
5012 + suspend_state_names[vc_suspend_state + VC_SUSPEND_NUM_OFFSET]);
5013 + vchiq_log_warning(vchiq_susp_log_level,
5014 + "-- Videcore resume state: %s --",
5015 + resume_state_names[vc_resume_state + VC_RESUME_NUM_OFFSET]);
5018 + vchiq_log_warning(vchiq_susp_log_level, "Too many active "
5019 + "services (%d). Only dumping up to first %d services "
5020 + "with non-zero use-count", active_services,
5021 + local_max_services);
5023 + for (i = 0; i < j; i++) {
5024 + vchiq_log_warning(vchiq_susp_log_level,
5025 + "----- %c%c%c%c:%d service count %d %s",
5026 + VCHIQ_FOURCC_AS_4CHARS(service_data[i].fourcc),
5027 + service_data[i].clientid,
5028 + service_data[i].use_count,
5029 + service_data[i].use_count ? nz : "");
5031 + vchiq_log_warning(vchiq_susp_log_level,
5032 + "----- VCHIQ use count count %d", peer_count);
5033 + vchiq_log_warning(vchiq_susp_log_level,
5034 + "--- Overall vchiq instance use count %d", vc_use_count);
5036 + vchiq_dump_platform_use_state(state);
5040 +vchiq_check_service(VCHIQ_SERVICE_T *service)
5042 + VCHIQ_ARM_STATE_T *arm_state;
5043 + VCHIQ_STATUS_T ret = VCHIQ_ERROR;
5045 + if (!service || !service->state)
5048 + vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
5050 + arm_state = vchiq_platform_get_arm_state(service->state);
5052 + read_lock_bh(&arm_state->susp_res_lock);
5053 + if (service->service_use_count)
5054 + ret = VCHIQ_SUCCESS;
5055 + read_unlock_bh(&arm_state->susp_res_lock);
5057 + if (ret == VCHIQ_ERROR) {
5058 + vchiq_log_error(vchiq_susp_log_level,
5059 + "%s ERROR - %c%c%c%c:%d service count %d, "
5060 + "state count %d, videocore suspend state %s", __func__,
5061 + VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
5062 + service->client_id, service->service_use_count,
5063 + arm_state->videocore_use_count,
5064 + suspend_state_names[arm_state->vc_suspend_state +
5065 + VC_SUSPEND_NUM_OFFSET]);
5066 + vchiq_dump_service_use_state(service->state);
5072 +/* stub functions */
5073 +void vchiq_on_remote_use_active(VCHIQ_STATE_T *state)
5078 +void vchiq_platform_conn_state_changed(VCHIQ_STATE_T *state,
5079 + VCHIQ_CONNSTATE_T oldstate, VCHIQ_CONNSTATE_T newstate)
5081 + VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
5082 + vchiq_log_info(vchiq_susp_log_level, "%d: %s->%s", state->id,
5083 + get_conn_state_name(oldstate), get_conn_state_name(newstate));
5084 + if (state->conn_state == VCHIQ_CONNSTATE_CONNECTED) {
5085 + write_lock_bh(&arm_state->susp_res_lock);
5086 + if (!arm_state->first_connect) {
5087 + char threadname[10];
5088 + arm_state->first_connect = 1;
5089 + write_unlock_bh(&arm_state->susp_res_lock);
5090 + snprintf(threadname, sizeof(threadname), "VCHIQka-%d",
5092 + arm_state->ka_thread = kthread_create(
5093 + &vchiq_keepalive_thread_func,
5096 + if (arm_state->ka_thread == NULL) {
5097 + vchiq_log_error(vchiq_susp_log_level,
5098 + "vchiq: FATAL: couldn't create thread %s",
5101 + wake_up_process(arm_state->ka_thread);
5104 + write_unlock_bh(&arm_state->susp_res_lock);
5109 +/****************************************************************************
5111 +* vchiq_init - called when the module is loaded.
5113 +***************************************************************************/
5121 + /* create debugfs entries */
5122 + err = vchiq_debugfs_init();
5124 + goto failed_debugfs_init;
5126 + err = alloc_chrdev_region(&vchiq_devid, VCHIQ_MINOR, 1, DEVICE_NAME);
5128 + vchiq_log_error(vchiq_arm_log_level,
5129 + "Unable to allocate device number");
5130 + goto failed_alloc_chrdev;
5132 + cdev_init(&vchiq_cdev, &vchiq_fops);
5133 + vchiq_cdev.owner = THIS_MODULE;
5134 + err = cdev_add(&vchiq_cdev, vchiq_devid, 1);
5136 + vchiq_log_error(vchiq_arm_log_level,
5137 + "Unable to register device");
5138 + goto failed_cdev_add;
5141 + /* create sysfs entries */
5142 + vchiq_class = class_create(THIS_MODULE, DEVICE_NAME);
5143 + ptr_err = vchiq_class;
5144 + if (IS_ERR(ptr_err))
5145 + goto failed_class_create;
5147 + vchiq_dev = device_create(vchiq_class, NULL,
5148 + vchiq_devid, NULL, "vchiq");
5149 + ptr_err = vchiq_dev;
5150 + if (IS_ERR(ptr_err))
5151 + goto failed_device_create;
5153 + err = vchiq_platform_init(&g_state);
5155 + goto failed_platform_init;
5157 + vchiq_log_info(vchiq_arm_log_level,
5158 + "vchiq: initialised - version %d (min %d), device %d.%d",
5159 + VCHIQ_VERSION, VCHIQ_VERSION_MIN,
5160 + MAJOR(vchiq_devid), MINOR(vchiq_devid));
5164 +failed_platform_init:
5165 + device_destroy(vchiq_class, vchiq_devid);
5166 +failed_device_create:
5167 + class_destroy(vchiq_class);
5168 +failed_class_create:
5169 + cdev_del(&vchiq_cdev);
5170 + err = PTR_ERR(ptr_err);
5172 + unregister_chrdev_region(vchiq_devid, 1);
5173 +failed_alloc_chrdev:
5174 + vchiq_debugfs_deinit();
5175 +failed_debugfs_init:
5176 + vchiq_log_warning(vchiq_arm_log_level, "could not load vchiq");
5180 +/****************************************************************************
5182 +* vchiq_exit - called when the module is unloaded.
5184 +***************************************************************************/
5189 + vchiq_platform_exit(&g_state);
5190 + device_destroy(vchiq_class, vchiq_devid);
5191 + class_destroy(vchiq_class);
5192 + cdev_del(&vchiq_cdev);
5193 + unregister_chrdev_region(vchiq_devid, 1);
5196 +module_init(vchiq_init);
5197 +module_exit(vchiq_exit);
5198 +MODULE_LICENSE("GPL");
5199 +MODULE_AUTHOR("Broadcom Corporation");
5200 diff --git a/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.h b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.h
5201 new file mode 100644
5202 index 0000000..d1e2741
5204 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.h
5207 + * Copyright (c) 2014 Raspberry Pi (Trading) Ltd. All rights reserved.
5208 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
5210 + * Redistribution and use in source and binary forms, with or without
5211 + * modification, are permitted provided that the following conditions
5213 + * 1. Redistributions of source code must retain the above copyright
5214 + * notice, this list of conditions, and the following disclaimer,
5215 + * without modification.
5216 + * 2. Redistributions in binary form must reproduce the above copyright
5217 + * notice, this list of conditions and the following disclaimer in the
5218 + * documentation and/or other materials provided with the distribution.
5219 + * 3. The names of the above-listed copyright holders may not be used
5220 + * to endorse or promote products derived from this software without
5221 + * specific prior written permission.
5223 + * ALTERNATIVELY, this software may be distributed under the terms of the
5224 + * GNU General Public License ("GPL") version 2, as published by the Free
5225 + * Software Foundation.
5227 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
5228 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
5229 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
5230 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
5231 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
5232 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
5233 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
5234 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
5235 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
5236 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
5237 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5240 +#ifndef VCHIQ_ARM_H
5241 +#define VCHIQ_ARM_H
5243 +#include <linux/mutex.h>
5244 +#include <linux/semaphore.h>
5245 +#include <linux/atomic.h>
5246 +#include "vchiq_core.h"
5247 +#include "vchiq_debugfs.h"
5250 +enum vc_suspend_status {
5251 + VC_SUSPEND_FORCE_CANCELED = -3, /* Force suspend canceled, too busy */
5252 + VC_SUSPEND_REJECTED = -2, /* Videocore rejected suspend request */
5253 + VC_SUSPEND_FAILED = -1, /* Videocore suspend failed */
5254 + VC_SUSPEND_IDLE = 0, /* VC active, no suspend actions */
5255 + VC_SUSPEND_REQUESTED, /* User has requested suspend */
5256 + VC_SUSPEND_IN_PROGRESS, /* Slot handler has recvd suspend request */
5257 + VC_SUSPEND_SUSPENDED /* Videocore suspend succeeded */
5260 +enum vc_resume_status {
5261 + VC_RESUME_FAILED = -1, /* Videocore resume failed */
5262 + VC_RESUME_IDLE = 0, /* VC suspended, no resume actions */
5263 + VC_RESUME_REQUESTED, /* User has requested resume */
5264 + VC_RESUME_IN_PROGRESS, /* Slot handler has received resume request */
5265 + VC_RESUME_RESUMED /* Videocore resumed successfully (active) */
5271 + USE_TYPE_SERVICE_NO_RESUME,
5277 +typedef struct vchiq_arm_state_struct {
5278 + /* Keepalive-related data */
5279 + struct task_struct *ka_thread;
5280 + struct completion ka_evt;
5281 + atomic_t ka_use_count;
5282 + atomic_t ka_use_ack_count;
5283 + atomic_t ka_release_count;
5285 + struct completion vc_suspend_complete;
5286 + struct completion vc_resume_complete;
5288 + rwlock_t susp_res_lock;
5289 + enum vc_suspend_status vc_suspend_state;
5290 + enum vc_resume_status vc_resume_state;
5292 + unsigned int wake_address;
5294 + struct timer_list suspend_timer;
5295 + int suspend_timer_timeout;
5296 + int suspend_timer_running;
5298 + /* Global use count for videocore.
5299 + ** This is equal to the sum of the use counts for all services. When
5300 + ** this hits zero the videocore suspend procedure will be initiated.
5302 + int videocore_use_count;
5304 + /* Use count to track requests from videocore peer.
5305 + ** This use count is not associated with a service, so needs to be
5306 + ** tracked separately with the state.
5308 + int peer_use_count;
5310 + /* Flag to indicate whether resume is blocked. This happens when the
5311 + ** ARM is suspending
5313 + struct completion resume_blocker;
5314 + int resume_blocked;
5315 + struct completion blocked_blocker;
5316 + int blocked_count;
5318 + int autosuspend_override;
5320 + /* Flag to indicate that the first vchiq connect has made it through.
5321 + ** This means that both sides should be fully ready, and we should
5322 + ** be able to suspend after this point.
5324 + int first_connect;
5326 + unsigned long long suspend_start_time;
5327 + unsigned long long sleep_start_time;
5328 + unsigned long long resume_start_time;
5329 + unsigned long long last_wake_time;
5331 +} VCHIQ_ARM_STATE_T;
5333 +extern int vchiq_arm_log_level;
5334 +extern int vchiq_susp_log_level;
5337 +vchiq_platform_init(VCHIQ_STATE_T *state);
5340 +vchiq_platform_exit(VCHIQ_STATE_T *state);
5342 +extern VCHIQ_STATE_T *
5343 +vchiq_get_state(void);
5345 +extern VCHIQ_STATUS_T
5346 +vchiq_arm_vcsuspend(VCHIQ_STATE_T *state);
5348 +extern VCHIQ_STATUS_T
5349 +vchiq_arm_force_suspend(VCHIQ_STATE_T *state);
5352 +vchiq_arm_allow_resume(VCHIQ_STATE_T *state);
5354 +extern VCHIQ_STATUS_T
5355 +vchiq_arm_vcresume(VCHIQ_STATE_T *state);
5357 +extern VCHIQ_STATUS_T
5358 +vchiq_arm_init_state(VCHIQ_STATE_T *state, VCHIQ_ARM_STATE_T *arm_state);
5361 +vchiq_check_resume(VCHIQ_STATE_T *state);
5364 +vchiq_check_suspend(VCHIQ_STATE_T *state);
5366 +vchiq_use_service(VCHIQ_SERVICE_HANDLE_T handle);
5368 +extern VCHIQ_STATUS_T
5369 +vchiq_release_service(VCHIQ_SERVICE_HANDLE_T handle);
5371 +extern VCHIQ_STATUS_T
5372 +vchiq_check_service(VCHIQ_SERVICE_T *service);
5374 +extern VCHIQ_STATUS_T
5375 +vchiq_platform_suspend(VCHIQ_STATE_T *state);
5378 +vchiq_platform_videocore_wanted(VCHIQ_STATE_T *state);
5381 +vchiq_platform_use_suspend_timer(void);
5384 +vchiq_dump_platform_use_state(VCHIQ_STATE_T *state);
5387 +vchiq_dump_service_use_state(VCHIQ_STATE_T *state);
5389 +extern VCHIQ_ARM_STATE_T*
5390 +vchiq_platform_get_arm_state(VCHIQ_STATE_T *state);
5393 +vchiq_videocore_wanted(VCHIQ_STATE_T *state);
5395 +extern VCHIQ_STATUS_T
5396 +vchiq_use_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service,
5397 + enum USE_TYPE_E use_type);
5398 +extern VCHIQ_STATUS_T
5399 +vchiq_release_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service);
5401 +extern VCHIQ_DEBUGFS_NODE_T *
5402 +vchiq_instance_get_debugfs_node(VCHIQ_INSTANCE_T instance);
5405 +vchiq_instance_get_use_count(VCHIQ_INSTANCE_T instance);
5408 +vchiq_instance_get_pid(VCHIQ_INSTANCE_T instance);
5411 +vchiq_instance_get_trace(VCHIQ_INSTANCE_T instance);
5414 +vchiq_instance_set_trace(VCHIQ_INSTANCE_T instance, int trace);
5417 +set_suspend_state(VCHIQ_ARM_STATE_T *arm_state,
5418 + enum vc_suspend_status new_state);
5421 +set_resume_state(VCHIQ_ARM_STATE_T *arm_state,
5422 + enum vc_resume_status new_state);
5425 +start_suspend_timer(VCHIQ_ARM_STATE_T *arm_state);
5428 +#endif /* VCHIQ_ARM_H */
5429 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
5430 new file mode 100644
5431 index 0000000..df64581
5433 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_build_info.h
5436 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
5438 + * Redistribution and use in source and binary forms, with or without
5439 + * modification, are permitted provided that the following conditions
5441 + * 1. Redistributions of source code must retain the above copyright
5442 + * notice, this list of conditions, and the following disclaimer,
5443 + * without modification.
5444 + * 2. Redistributions in binary form must reproduce the above copyright
5445 + * notice, this list of conditions and the following disclaimer in the
5446 + * documentation and/or other materials provided with the distribution.
5447 + * 3. The names of the above-listed copyright holders may not be used
5448 + * to endorse or promote products derived from this software without
5449 + * specific prior written permission.
5451 + * ALTERNATIVELY, this software may be distributed under the terms of the
5452 + * GNU General Public License ("GPL") version 2, as published by the Free
5453 + * Software Foundation.
5455 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
5456 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
5457 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
5458 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
5459 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
5460 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
5461 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
5462 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
5463 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
5464 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
5465 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5468 +const char *vchiq_get_build_hostname(void);
5469 +const char *vchiq_get_build_version(void);
5470 +const char *vchiq_get_build_time(void);
5471 +const char *vchiq_get_build_date(void);
5472 diff --git a/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_cfg.h b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_cfg.h
5473 new file mode 100644
5474 index 0000000..c382740
5476 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_cfg.h
5479 + * Copyright (c) 2010-2014 Broadcom. All rights reserved.
5481 + * Redistribution and use in source and binary forms, with or without
5482 + * modification, are permitted provided that the following conditions
5484 + * 1. Redistributions of source code must retain the above copyright
5485 + * notice, this list of conditions, and the following disclaimer,
5486 + * without modification.
5487 + * 2. Redistributions in binary form must reproduce the above copyright
5488 + * notice, this list of conditions and the following disclaimer in the
5489 + * documentation and/or other materials provided with the distribution.
5490 + * 3. The names of the above-listed copyright holders may not be used
5491 + * to endorse or promote products derived from this software without
5492 + * specific prior written permission.
5494 + * ALTERNATIVELY, this software may be distributed under the terms of the
5495 + * GNU General Public License ("GPL") version 2, as published by the Free
5496 + * Software Foundation.
5498 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
5499 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
5500 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
5501 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
5502 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
5503 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
5504 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
5505 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
5506 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
5507 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
5508 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5511 +#ifndef VCHIQ_CFG_H
5512 +#define VCHIQ_CFG_H
5514 +#define VCHIQ_MAGIC VCHIQ_MAKE_FOURCC('V', 'C', 'H', 'I')
5515 +/* The version of VCHIQ - change with any non-trivial change */
5516 +#define VCHIQ_VERSION 7
5517 +/* The minimum compatible version - update to match VCHIQ_VERSION with any
5518 +** incompatible change */
5519 +#define VCHIQ_VERSION_MIN 3
5521 +/* The version that introduced the VCHIQ_IOC_LIB_VERSION ioctl */
5522 +#define VCHIQ_VERSION_LIB_VERSION 7
5524 +/* The version that introduced the VCHIQ_IOC_CLOSE_DELIVERED ioctl */
5525 +#define VCHIQ_VERSION_CLOSE_DELIVERED 7
5527 +#define VCHIQ_MAX_STATES 1
5528 +#define VCHIQ_MAX_SERVICES 4096
5529 +#define VCHIQ_MAX_SLOTS 128
5530 +#define VCHIQ_MAX_SLOTS_PER_SIDE 64
5532 +#define VCHIQ_NUM_CURRENT_BULKS 32
5533 +#define VCHIQ_NUM_SERVICE_BULKS 4
5535 +#ifndef VCHIQ_ENABLE_DEBUG
5536 +#define VCHIQ_ENABLE_DEBUG 1
5539 +#ifndef VCHIQ_ENABLE_STATS
5540 +#define VCHIQ_ENABLE_STATS 1
5543 +#endif /* VCHIQ_CFG_H */
5544 diff --git a/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.c b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.c
5545 new file mode 100644
5546 index 0000000..65f4b52
5548 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.c
5551 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
5553 + * Redistribution and use in source and binary forms, with or without
5554 + * modification, are permitted provided that the following conditions
5556 + * 1. Redistributions of source code must retain the above copyright
5557 + * notice, this list of conditions, and the following disclaimer,
5558 + * without modification.
5559 + * 2. Redistributions in binary form must reproduce the above copyright
5560 + * notice, this list of conditions and the following disclaimer in the
5561 + * documentation and/or other materials provided with the distribution.
5562 + * 3. The names of the above-listed copyright holders may not be used
5563 + * to endorse or promote products derived from this software without
5564 + * specific prior written permission.
5566 + * ALTERNATIVELY, this software may be distributed under the terms of the
5567 + * GNU General Public License ("GPL") version 2, as published by the Free
5568 + * Software Foundation.
5570 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
5571 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
5572 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
5573 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
5574 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
5575 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
5576 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
5577 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
5578 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
5579 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
5580 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5583 +#include "vchiq_connected.h"
5584 +#include "vchiq_core.h"
5585 +#include <linux/module.h>
5586 +#include <linux/mutex.h>
5588 +#define MAX_CALLBACKS 10
5590 +static int g_connected;
5591 +static int g_num_deferred_callbacks;
5592 +static VCHIQ_CONNECTED_CALLBACK_T g_deferred_callback[MAX_CALLBACKS];
5593 +static int g_once_init;
5594 +static struct mutex g_connected_mutex;
5596 +/****************************************************************************
5598 +* Function to initialize our lock.
5600 +***************************************************************************/
5602 +static void connected_init(void)
5604 + if (!g_once_init) {
5605 + mutex_init(&g_connected_mutex);
5610 +/****************************************************************************
5612 +* This function is used to defer initialization until the vchiq stack is
5613 +* initialized. If the stack is already initialized, then the callback will
5614 +* be made immediately, otherwise it will be deferred until
5615 +* vchiq_call_connected_callbacks is called.
5617 +***************************************************************************/
5619 +void vchiq_add_connected_callback(VCHIQ_CONNECTED_CALLBACK_T callback)
5623 + if (mutex_lock_interruptible(&g_connected_mutex) != 0)
5627 + /* We're already connected. Call the callback immediately. */
5631 + if (g_num_deferred_callbacks >= MAX_CALLBACKS)
5632 + vchiq_log_error(vchiq_core_log_level,
5633 + "There already %d callback registered - "
5634 + "please increase MAX_CALLBACKS",
5635 + g_num_deferred_callbacks);
5637 + g_deferred_callback[g_num_deferred_callbacks] =
5639 + g_num_deferred_callbacks++;
5642 + mutex_unlock(&g_connected_mutex);
5645 +/****************************************************************************
5647 +* This function is called by the vchiq stack once it has been connected to
5648 +* the videocore and clients can start to use the stack.
5650 +***************************************************************************/
5652 +void vchiq_call_connected_callbacks(void)
5658 + if (mutex_lock_interruptible(&g_connected_mutex) != 0)
5661 + for (i = 0; i < g_num_deferred_callbacks; i++)
5662 + g_deferred_callback[i]();
5664 + g_num_deferred_callbacks = 0;
5666 + mutex_unlock(&g_connected_mutex);
5668 +EXPORT_SYMBOL(vchiq_add_connected_callback);
5669 diff --git a/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.h b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.h
5670 new file mode 100644
5671 index 0000000..863b3e3
5673 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.h
5676 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
5678 + * Redistribution and use in source and binary forms, with or without
5679 + * modification, are permitted provided that the following conditions
5681 + * 1. Redistributions of source code must retain the above copyright
5682 + * notice, this list of conditions, and the following disclaimer,
5683 + * without modification.
5684 + * 2. Redistributions in binary form must reproduce the above copyright
5685 + * notice, this list of conditions and the following disclaimer in the
5686 + * documentation and/or other materials provided with the distribution.
5687 + * 3. The names of the above-listed copyright holders may not be used
5688 + * to endorse or promote products derived from this software without
5689 + * specific prior written permission.
5691 + * ALTERNATIVELY, this software may be distributed under the terms of the
5692 + * GNU General Public License ("GPL") version 2, as published by the Free
5693 + * Software Foundation.
5695 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
5696 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
5697 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
5698 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
5699 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
5700 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
5701 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
5702 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
5703 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
5704 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
5705 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5708 +#ifndef VCHIQ_CONNECTED_H
5709 +#define VCHIQ_CONNECTED_H
5711 +/* ---- Include Files ----------------------------------------------------- */
5713 +/* ---- Constants and Types ---------------------------------------------- */
5715 +typedef void (*VCHIQ_CONNECTED_CALLBACK_T)(void);
5717 +/* ---- Variable Externs ------------------------------------------------- */
5719 +/* ---- Function Prototypes ---------------------------------------------- */
5721 +void vchiq_add_connected_callback(VCHIQ_CONNECTED_CALLBACK_T callback);
5722 +void vchiq_call_connected_callbacks(void);
5724 +#endif /* VCHIQ_CONNECTED_H */
5725 diff --git a/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.c b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.c
5726 new file mode 100644
5727 index 0000000..f962027
5729 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.c
5732 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
5734 + * Redistribution and use in source and binary forms, with or without
5735 + * modification, are permitted provided that the following conditions
5737 + * 1. Redistributions of source code must retain the above copyright
5738 + * notice, this list of conditions, and the following disclaimer,
5739 + * without modification.
5740 + * 2. Redistributions in binary form must reproduce the above copyright
5741 + * notice, this list of conditions and the following disclaimer in the
5742 + * documentation and/or other materials provided with the distribution.
5743 + * 3. The names of the above-listed copyright holders may not be used
5744 + * to endorse or promote products derived from this software without
5745 + * specific prior written permission.
5747 + * ALTERNATIVELY, this software may be distributed under the terms of the
5748 + * GNU General Public License ("GPL") version 2, as published by the Free
5749 + * Software Foundation.
5751 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
5752 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
5753 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
5754 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
5755 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
5756 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
5757 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
5758 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
5759 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
5760 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
5761 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5764 +#include "vchiq_core.h"
5766 +#define VCHIQ_SLOT_HANDLER_STACK 8192
5768 +#define HANDLE_STATE_SHIFT 12
5770 +#define SLOT_INFO_FROM_INDEX(state, index) (state->slot_info + (index))
5771 +#define SLOT_DATA_FROM_INDEX(state, index) (state->slot_data + (index))
5772 +#define SLOT_INDEX_FROM_DATA(state, data) \
5773 + (((unsigned int)((char *)data - (char *)state->slot_data)) / \
5775 +#define SLOT_INDEX_FROM_INFO(state, info) \
5776 + ((unsigned int)(info - state->slot_info))
5777 +#define SLOT_QUEUE_INDEX_FROM_POS(pos) \
5778 + ((int)((unsigned int)(pos) / VCHIQ_SLOT_SIZE))
5780 +#define BULK_INDEX(x) (x & (VCHIQ_NUM_SERVICE_BULKS - 1))
5782 +#define SRVTRACE_LEVEL(srv) \
5783 + (((srv) && (srv)->trace) ? VCHIQ_LOG_TRACE : vchiq_core_msg_log_level)
5784 +#define SRVTRACE_ENABLED(srv, lev) \
5785 + (((srv) && (srv)->trace) || (vchiq_core_msg_log_level >= (lev)))
5787 +struct vchiq_open_payload {
5791 + short version_min;
5794 +struct vchiq_openack_payload {
5798 +/* we require this for consistency between endpoints */
5799 +vchiq_static_assert(sizeof(VCHIQ_HEADER_T) == 8);
5800 +vchiq_static_assert(IS_POW2(sizeof(VCHIQ_HEADER_T)));
5801 +vchiq_static_assert(IS_POW2(VCHIQ_NUM_CURRENT_BULKS));
5802 +vchiq_static_assert(IS_POW2(VCHIQ_NUM_SERVICE_BULKS));
5803 +vchiq_static_assert(IS_POW2(VCHIQ_MAX_SERVICES));
5804 +vchiq_static_assert(VCHIQ_VERSION >= VCHIQ_VERSION_MIN);
5806 +/* Run time control of log level, based on KERN_XXX level. */
5807 +int vchiq_core_log_level = VCHIQ_LOG_DEFAULT;
5808 +int vchiq_core_msg_log_level = VCHIQ_LOG_DEFAULT;
5809 +int vchiq_sync_log_level = VCHIQ_LOG_DEFAULT;
5811 +static atomic_t pause_bulks_count = ATOMIC_INIT(0);
5813 +static DEFINE_SPINLOCK(service_spinlock);
5814 +DEFINE_SPINLOCK(bulk_waiter_spinlock);
5815 +DEFINE_SPINLOCK(quota_spinlock);
5817 +VCHIQ_STATE_T *vchiq_states[VCHIQ_MAX_STATES];
5818 +static unsigned int handle_seq;
5820 +static const char *const srvstate_names[] = {
5833 +static const char *const reason_names[] = {
5836 + "MESSAGE_AVAILABLE",
5837 + "BULK_TRANSMIT_DONE",
5838 + "BULK_RECEIVE_DONE",
5839 + "BULK_TRANSMIT_ABORTED",
5840 + "BULK_RECEIVE_ABORTED"
5843 +static const char *const conn_state_names[] = {
5857 +release_message_sync(VCHIQ_STATE_T *state, VCHIQ_HEADER_T *header);
5859 +static const char *msg_type_str(unsigned int msg_type)
5861 + switch (msg_type) {
5862 + case VCHIQ_MSG_PADDING: return "PADDING";
5863 + case VCHIQ_MSG_CONNECT: return "CONNECT";
5864 + case VCHIQ_MSG_OPEN: return "OPEN";
5865 + case VCHIQ_MSG_OPENACK: return "OPENACK";
5866 + case VCHIQ_MSG_CLOSE: return "CLOSE";
5867 + case VCHIQ_MSG_DATA: return "DATA";
5868 + case VCHIQ_MSG_BULK_RX: return "BULK_RX";
5869 + case VCHIQ_MSG_BULK_TX: return "BULK_TX";
5870 + case VCHIQ_MSG_BULK_RX_DONE: return "BULK_RX_DONE";
5871 + case VCHIQ_MSG_BULK_TX_DONE: return "BULK_TX_DONE";
5872 + case VCHIQ_MSG_PAUSE: return "PAUSE";
5873 + case VCHIQ_MSG_RESUME: return "RESUME";
5874 + case VCHIQ_MSG_REMOTE_USE: return "REMOTE_USE";
5875 + case VCHIQ_MSG_REMOTE_RELEASE: return "REMOTE_RELEASE";
5876 + case VCHIQ_MSG_REMOTE_USE_ACTIVE: return "REMOTE_USE_ACTIVE";
5882 +vchiq_set_service_state(VCHIQ_SERVICE_T *service, int newstate)
5884 + vchiq_log_info(vchiq_core_log_level, "%d: srv:%d %s->%s",
5885 + service->state->id, service->localport,
5886 + srvstate_names[service->srvstate],
5887 + srvstate_names[newstate]);
5888 + service->srvstate = newstate;
5892 +find_service_by_handle(VCHIQ_SERVICE_HANDLE_T handle)
5894 + VCHIQ_SERVICE_T *service;
5896 + spin_lock(&service_spinlock);
5897 + service = handle_to_service(handle);
5898 + if (service && (service->srvstate != VCHIQ_SRVSTATE_FREE) &&
5899 + (service->handle == handle)) {
5900 + BUG_ON(service->ref_count == 0);
5901 + service->ref_count++;
5904 + spin_unlock(&service_spinlock);
5907 + vchiq_log_info(vchiq_core_log_level,
5908 + "Invalid service handle 0x%x", handle);
5914 +find_service_by_port(VCHIQ_STATE_T *state, int localport)
5916 + VCHIQ_SERVICE_T *service = NULL;
5917 + if ((unsigned int)localport <= VCHIQ_PORT_MAX) {
5918 + spin_lock(&service_spinlock);
5919 + service = state->services[localport];
5920 + if (service && (service->srvstate != VCHIQ_SRVSTATE_FREE)) {
5921 + BUG_ON(service->ref_count == 0);
5922 + service->ref_count++;
5925 + spin_unlock(&service_spinlock);
5929 + vchiq_log_info(vchiq_core_log_level,
5930 + "Invalid port %d", localport);
5936 +find_service_for_instance(VCHIQ_INSTANCE_T instance,
5937 + VCHIQ_SERVICE_HANDLE_T handle) {
5938 + VCHIQ_SERVICE_T *service;
5940 + spin_lock(&service_spinlock);
5941 + service = handle_to_service(handle);
5942 + if (service && (service->srvstate != VCHIQ_SRVSTATE_FREE) &&
5943 + (service->handle == handle) &&
5944 + (service->instance == instance)) {
5945 + BUG_ON(service->ref_count == 0);
5946 + service->ref_count++;
5949 + spin_unlock(&service_spinlock);
5952 + vchiq_log_info(vchiq_core_log_level,
5953 + "Invalid service handle 0x%x", handle);
5959 +find_closed_service_for_instance(VCHIQ_INSTANCE_T instance,
5960 + VCHIQ_SERVICE_HANDLE_T handle) {
5961 + VCHIQ_SERVICE_T *service;
5963 + spin_lock(&service_spinlock);
5964 + service = handle_to_service(handle);
5966 + ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
5967 + (service->srvstate == VCHIQ_SRVSTATE_CLOSED)) &&
5968 + (service->handle == handle) &&
5969 + (service->instance == instance)) {
5970 + BUG_ON(service->ref_count == 0);
5971 + service->ref_count++;
5974 + spin_unlock(&service_spinlock);
5977 + vchiq_log_info(vchiq_core_log_level,
5978 + "Invalid service handle 0x%x", handle);
5984 +next_service_by_instance(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance,
5987 + VCHIQ_SERVICE_T *service = NULL;
5990 + spin_lock(&service_spinlock);
5991 + while (idx < state->unused_service) {
5992 + VCHIQ_SERVICE_T *srv = state->services[idx++];
5993 + if (srv && (srv->srvstate != VCHIQ_SRVSTATE_FREE) &&
5994 + (srv->instance == instance)) {
5996 + BUG_ON(service->ref_count == 0);
5997 + service->ref_count++;
6001 + spin_unlock(&service_spinlock);
6009 +lock_service(VCHIQ_SERVICE_T *service)
6011 + spin_lock(&service_spinlock);
6012 + BUG_ON(!service || (service->ref_count == 0));
6014 + service->ref_count++;
6015 + spin_unlock(&service_spinlock);
6019 +unlock_service(VCHIQ_SERVICE_T *service)
6021 + VCHIQ_STATE_T *state = service->state;
6022 + spin_lock(&service_spinlock);
6023 + BUG_ON(!service || (service->ref_count == 0));
6024 + if (service && service->ref_count) {
6025 + service->ref_count--;
6026 + if (!service->ref_count) {
6027 + BUG_ON(service->srvstate != VCHIQ_SRVSTATE_FREE);
6028 + state->services[service->localport] = NULL;
6032 + spin_unlock(&service_spinlock);
6034 + if (service && service->userdata_term)
6035 + service->userdata_term(service->base.userdata);
6041 +vchiq_get_client_id(VCHIQ_SERVICE_HANDLE_T handle)
6043 + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
6046 + id = service ? service->client_id : 0;
6048 + unlock_service(service);
6054 +vchiq_get_service_userdata(VCHIQ_SERVICE_HANDLE_T handle)
6056 + VCHIQ_SERVICE_T *service = handle_to_service(handle);
6058 + return service ? service->base.userdata : NULL;
6062 +vchiq_get_service_fourcc(VCHIQ_SERVICE_HANDLE_T handle)
6064 + VCHIQ_SERVICE_T *service = handle_to_service(handle);
6066 + return service ? service->base.fourcc : 0;
6070 +mark_service_closing_internal(VCHIQ_SERVICE_T *service, int sh_thread)
6072 + VCHIQ_STATE_T *state = service->state;
6073 + VCHIQ_SERVICE_QUOTA_T *service_quota;
6075 + service->closing = 1;
6077 + /* Synchronise with other threads. */
6078 + mutex_lock(&state->recycle_mutex);
6079 + mutex_unlock(&state->recycle_mutex);
6080 + if (!sh_thread || (state->conn_state != VCHIQ_CONNSTATE_PAUSE_SENT)) {
6081 + /* If we're pausing then the slot_mutex is held until resume
6082 + * by the slot handler. Therefore don't try to acquire this
6083 + * mutex if we're the slot handler and in the pause sent state.
6084 + * We don't need to in this case anyway. */
6085 + mutex_lock(&state->slot_mutex);
6086 + mutex_unlock(&state->slot_mutex);
6089 + /* Unblock any sending thread. */
6090 + service_quota = &state->service_quotas[service->localport];
6091 + up(&service_quota->quota_event);
6095 +mark_service_closing(VCHIQ_SERVICE_T *service)
6097 + mark_service_closing_internal(service, 0);
6100 +static inline VCHIQ_STATUS_T
6101 +make_service_callback(VCHIQ_SERVICE_T *service, VCHIQ_REASON_T reason,
6102 + VCHIQ_HEADER_T *header, void *bulk_userdata)
6104 + VCHIQ_STATUS_T status;
6105 + vchiq_log_trace(vchiq_core_log_level, "%d: callback:%d (%s, %x, %x)",
6106 + service->state->id, service->localport, reason_names[reason],
6107 + (unsigned int)header, (unsigned int)bulk_userdata);
6108 + status = service->base.callback(reason, header, service->handle,
6110 + if (status == VCHIQ_ERROR) {
6111 + vchiq_log_warning(vchiq_core_log_level,
6112 + "%d: ignoring ERROR from callback to service %x",
6113 + service->state->id, service->handle);
6114 + status = VCHIQ_SUCCESS;
6120 +vchiq_set_conn_state(VCHIQ_STATE_T *state, VCHIQ_CONNSTATE_T newstate)
6122 + VCHIQ_CONNSTATE_T oldstate = state->conn_state;
6123 + vchiq_log_info(vchiq_core_log_level, "%d: %s->%s", state->id,
6124 + conn_state_names[oldstate],
6125 + conn_state_names[newstate]);
6126 + state->conn_state = newstate;
6127 + vchiq_platform_conn_state_changed(state, oldstate, newstate);
6131 +remote_event_create(REMOTE_EVENT_T *event)
6134 + /* Don't clear the 'fired' flag because it may already have been set
6135 + ** by the other side. */
6136 + sema_init(event->event, 0);
6140 +remote_event_destroy(REMOTE_EVENT_T *event)
6146 +remote_event_wait(REMOTE_EVENT_T *event)
6148 + if (!event->fired) {
6151 + if (!event->fired) {
6152 + if (down_interruptible(event->event) != 0) {
6166 +remote_event_signal_local(REMOTE_EVENT_T *event)
6173 +remote_event_poll(REMOTE_EVENT_T *event)
6175 + if (event->fired && event->armed)
6176 + remote_event_signal_local(event);
6180 +remote_event_pollall(VCHIQ_STATE_T *state)
6182 + remote_event_poll(&state->local->sync_trigger);
6183 + remote_event_poll(&state->local->sync_release);
6184 + remote_event_poll(&state->local->trigger);
6185 + remote_event_poll(&state->local->recycle);
6188 +/* Round up message sizes so that any space at the end of a slot is always big
6189 +** enough for a header. This relies on header size being a power of two, which
6190 +** has been verified earlier by a static assertion. */
6192 +static inline unsigned int
6193 +calc_stride(unsigned int size)
6195 + /* Allow room for the header */
6196 + size += sizeof(VCHIQ_HEADER_T);
6199 + return (size + sizeof(VCHIQ_HEADER_T) - 1) & ~(sizeof(VCHIQ_HEADER_T)
6203 +/* Called by the slot handler thread */
6204 +static VCHIQ_SERVICE_T *
6205 +get_listening_service(VCHIQ_STATE_T *state, int fourcc)
6209 + WARN_ON(fourcc == VCHIQ_FOURCC_INVALID);
6211 + for (i = 0; i < state->unused_service; i++) {
6212 + VCHIQ_SERVICE_T *service = state->services[i];
6214 + (service->public_fourcc == fourcc) &&
6215 + ((service->srvstate == VCHIQ_SRVSTATE_LISTENING) ||
6216 + ((service->srvstate == VCHIQ_SRVSTATE_OPEN) &&
6217 + (service->remoteport == VCHIQ_PORT_FREE)))) {
6218 + lock_service(service);
6226 +/* Called by the slot handler thread */
6227 +static VCHIQ_SERVICE_T *
6228 +get_connected_service(VCHIQ_STATE_T *state, unsigned int port)
6231 + for (i = 0; i < state->unused_service; i++) {
6232 + VCHIQ_SERVICE_T *service = state->services[i];
6233 + if (service && (service->srvstate == VCHIQ_SRVSTATE_OPEN)
6234 + && (service->remoteport == port)) {
6235 + lock_service(service);
6243 +request_poll(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service, int poll_type)
6249 + value = atomic_read(&service->poll_flags);
6250 + } while (atomic_cmpxchg(&service->poll_flags, value,
6251 + value | (1 << poll_type)) != value);
6254 + value = atomic_read(&state->poll_services[
6255 + service->localport>>5]);
6256 + } while (atomic_cmpxchg(
6257 + &state->poll_services[service->localport>>5],
6258 + value, value | (1 << (service->localport & 0x1f)))
6262 + state->poll_needed = 1;
6265 + /* ... and ensure the slot handler runs. */
6266 + remote_event_signal_local(&state->local->trigger);
6269 +/* Called from queue_message, by the slot handler and application threads,
6270 +** with slot_mutex held */
6271 +static VCHIQ_HEADER_T *
6272 +reserve_space(VCHIQ_STATE_T *state, int space, int is_blocking)
6274 + VCHIQ_SHARED_STATE_T *local = state->local;
6275 + int tx_pos = state->local_tx_pos;
6276 + int slot_space = VCHIQ_SLOT_SIZE - (tx_pos & VCHIQ_SLOT_MASK);
6278 + if (space > slot_space) {
6279 + VCHIQ_HEADER_T *header;
6280 + /* Fill the remaining space with padding */
6281 + WARN_ON(state->tx_data == NULL);
6282 + header = (VCHIQ_HEADER_T *)
6283 + (state->tx_data + (tx_pos & VCHIQ_SLOT_MASK));
6284 + header->msgid = VCHIQ_MSGID_PADDING;
6285 + header->size = slot_space - sizeof(VCHIQ_HEADER_T);
6287 + tx_pos += slot_space;
6290 + /* If necessary, get the next slot. */
6291 + if ((tx_pos & VCHIQ_SLOT_MASK) == 0) {
6294 + /* If there is no free slot... */
6296 + if (down_trylock(&state->slot_available_event) != 0) {
6297 + /* ...wait for one. */
6299 + VCHIQ_STATS_INC(state, slot_stalls);
6301 + /* But first, flush through the last slot. */
6302 + state->local_tx_pos = tx_pos;
6303 + local->tx_pos = tx_pos;
6304 + remote_event_signal(&state->remote->trigger);
6306 + if (!is_blocking ||
6307 + (down_interruptible(
6308 + &state->slot_available_event) != 0))
6309 + return NULL; /* No space available */
6313 + (state->slot_queue_available * VCHIQ_SLOT_SIZE));
6315 + slot_index = local->slot_queue[
6316 + SLOT_QUEUE_INDEX_FROM_POS(tx_pos) &
6317 + VCHIQ_SLOT_QUEUE_MASK];
6319 + (char *)SLOT_DATA_FROM_INDEX(state, slot_index);
6322 + state->local_tx_pos = tx_pos + space;
6324 + return (VCHIQ_HEADER_T *)(state->tx_data + (tx_pos & VCHIQ_SLOT_MASK));
6327 +/* Called by the recycle thread. */
6329 +process_free_queue(VCHIQ_STATE_T *state)
6331 + VCHIQ_SHARED_STATE_T *local = state->local;
6332 + BITSET_T service_found[BITSET_SIZE(VCHIQ_MAX_SERVICES)];
6333 + int slot_queue_available;
6335 + /* Use a read memory barrier to ensure that any state that may have
6336 + ** been modified by another thread is not masked by stale prefetched
6340 + /* Find slots which have been freed by the other side, and return them
6341 + ** to the available queue. */
6342 + slot_queue_available = state->slot_queue_available;
6344 + while (slot_queue_available != local->slot_queue_recycle) {
6346 + int slot_index = local->slot_queue[slot_queue_available++ &
6347 + VCHIQ_SLOT_QUEUE_MASK];
6348 + char *data = (char *)SLOT_DATA_FROM_INDEX(state, slot_index);
6349 + int data_found = 0;
6351 + vchiq_log_trace(vchiq_core_log_level, "%d: pfq %d=%x %x %x",
6352 + state->id, slot_index, (unsigned int)data,
6353 + local->slot_queue_recycle, slot_queue_available);
6355 + /* Initialise the bitmask for services which have used this
6357 + BITSET_ZERO(service_found);
6361 + while (pos < VCHIQ_SLOT_SIZE) {
6362 + VCHIQ_HEADER_T *header =
6363 + (VCHIQ_HEADER_T *)(data + pos);
6364 + int msgid = header->msgid;
6365 + if (VCHIQ_MSG_TYPE(msgid) == VCHIQ_MSG_DATA) {
6366 + int port = VCHIQ_MSG_SRCPORT(msgid);
6367 + VCHIQ_SERVICE_QUOTA_T *service_quota =
6368 + &state->service_quotas[port];
6370 + spin_lock("a_spinlock);
6371 + count = service_quota->message_use_count;
6373 + service_quota->message_use_count =
6375 + spin_unlock("a_spinlock);
6377 + if (count == service_quota->message_quota)
6378 + /* Signal the service that it
6379 + ** has dropped below its quota
6381 + up(&service_quota->quota_event);
6382 + else if (count == 0) {
6383 + vchiq_log_error(vchiq_core_log_level,
6385 + "message_use_count=%d "
6386 + "(header %x, msgid %x, "
6387 + "header->msgid %x, "
6388 + "header->size %x)",
6391 + message_use_count,
6392 + (unsigned int)header, msgid,
6395 + WARN(1, "invalid message use count\n");
6397 + if (!BITSET_IS_SET(service_found, port)) {
6398 + /* Set the found bit for this service */
6399 + BITSET_SET(service_found, port);
6401 + spin_lock("a_spinlock);
6402 + count = service_quota->slot_use_count;
6404 + service_quota->slot_use_count =
6406 + spin_unlock("a_spinlock);
6409 + /* Signal the service in case
6410 + ** it has dropped below its
6412 + up(&service_quota->quota_event);
6414 + vchiq_core_log_level,
6415 + "%d: pfq:%d %x@%x - "
6419 + (unsigned int)header,
6423 + vchiq_core_log_level,
6432 + (unsigned int)header,
6436 + WARN(1, "bad slot use count\n");
6443 + pos += calc_stride(header->size);
6444 + if (pos > VCHIQ_SLOT_SIZE) {
6445 + vchiq_log_error(vchiq_core_log_level,
6446 + "pfq - pos %x: header %x, msgid %x, "
6447 + "header->msgid %x, header->size %x",
6448 + pos, (unsigned int)header, msgid,
6449 + header->msgid, header->size);
6450 + WARN(1, "invalid slot position\n");
6456 + spin_lock("a_spinlock);
6457 + count = state->data_use_count;
6459 + state->data_use_count =
6461 + spin_unlock("a_spinlock);
6462 + if (count == state->data_quota)
6463 + up(&state->data_quota_event);
6466 + state->slot_queue_available = slot_queue_available;
6467 + up(&state->slot_available_event);
6471 +/* Called by the slot handler and application threads */
6472 +static VCHIQ_STATUS_T
6473 +queue_message(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service,
6474 + int msgid, const VCHIQ_ELEMENT_T *elements,
6475 + int count, int size, int is_blocking)
6477 + VCHIQ_SHARED_STATE_T *local;
6478 + VCHIQ_SERVICE_QUOTA_T *service_quota = NULL;
6479 + VCHIQ_HEADER_T *header;
6480 + int type = VCHIQ_MSG_TYPE(msgid);
6482 + unsigned int stride;
6484 + local = state->local;
6486 + stride = calc_stride(size);
6488 + WARN_ON(!(stride <= VCHIQ_SLOT_SIZE));
6490 + if ((type != VCHIQ_MSG_RESUME) &&
6491 + (mutex_lock_interruptible(&state->slot_mutex) != 0))
6492 + return VCHIQ_RETRY;
6494 + if (type == VCHIQ_MSG_DATA) {
6499 + if (service->closing) {
6500 + /* The service has been closed */
6501 + mutex_unlock(&state->slot_mutex);
6502 + return VCHIQ_ERROR;
6505 + service_quota = &state->service_quotas[service->localport];
6507 + spin_lock("a_spinlock);
6509 + /* Ensure this service doesn't use more than its quota of
6510 + ** messages or slots */
6511 + tx_end_index = SLOT_QUEUE_INDEX_FROM_POS(
6512 + state->local_tx_pos + stride - 1);
6514 + /* Ensure data messages don't use more than their quota of
6516 + while ((tx_end_index != state->previous_data_index) &&
6517 + (state->data_use_count == state->data_quota)) {
6518 + VCHIQ_STATS_INC(state, data_stalls);
6519 + spin_unlock("a_spinlock);
6520 + mutex_unlock(&state->slot_mutex);
6522 + if (down_interruptible(&state->data_quota_event)
6524 + return VCHIQ_RETRY;
6526 + mutex_lock(&state->slot_mutex);
6527 + spin_lock("a_spinlock);
6528 + tx_end_index = SLOT_QUEUE_INDEX_FROM_POS(
6529 + state->local_tx_pos + stride - 1);
6530 + if ((tx_end_index == state->previous_data_index) ||
6531 + (state->data_use_count < state->data_quota)) {
6532 + /* Pass the signal on to other waiters */
6533 + up(&state->data_quota_event);
6538 + while ((service_quota->message_use_count ==
6539 + service_quota->message_quota) ||
6540 + ((tx_end_index != service_quota->previous_tx_index) &&
6541 + (service_quota->slot_use_count ==
6542 + service_quota->slot_quota))) {
6543 + spin_unlock("a_spinlock);
6544 + vchiq_log_trace(vchiq_core_log_level,
6545 + "%d: qm:%d %s,%x - quota stall "
6546 + "(msg %d, slot %d)",
6547 + state->id, service->localport,
6548 + msg_type_str(type), size,
6549 + service_quota->message_use_count,
6550 + service_quota->slot_use_count);
6551 + VCHIQ_SERVICE_STATS_INC(service, quota_stalls);
6552 + mutex_unlock(&state->slot_mutex);
6553 + if (down_interruptible(&service_quota->quota_event)
6555 + return VCHIQ_RETRY;
6556 + if (service->closing)
6557 + return VCHIQ_ERROR;
6558 + if (mutex_lock_interruptible(&state->slot_mutex) != 0)
6559 + return VCHIQ_RETRY;
6560 + if (service->srvstate != VCHIQ_SRVSTATE_OPEN) {
6561 + /* The service has been closed */
6562 + mutex_unlock(&state->slot_mutex);
6563 + return VCHIQ_ERROR;
6565 + spin_lock("a_spinlock);
6566 + tx_end_index = SLOT_QUEUE_INDEX_FROM_POS(
6567 + state->local_tx_pos + stride - 1);
6570 + spin_unlock("a_spinlock);
6573 + header = reserve_space(state, stride, is_blocking);
6577 + VCHIQ_SERVICE_STATS_INC(service, slot_stalls);
6578 + mutex_unlock(&state->slot_mutex);
6579 + return VCHIQ_RETRY;
6582 + if (type == VCHIQ_MSG_DATA) {
6585 + int slot_use_count;
6587 + vchiq_log_info(vchiq_core_log_level,
6588 + "%d: qm %s@%x,%x (%d->%d)",
6590 + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
6591 + (unsigned int)header, size,
6592 + VCHIQ_MSG_SRCPORT(msgid),
6593 + VCHIQ_MSG_DSTPORT(msgid));
6597 + for (i = 0, pos = 0; i < (unsigned int)count;
6598 + pos += elements[i++].size)
6599 + if (elements[i].size) {
6600 + if (vchiq_copy_from_user
6601 + (header->data + pos, elements[i].data,
6602 + (size_t) elements[i].size) !=
6604 + mutex_unlock(&state->slot_mutex);
6605 + VCHIQ_SERVICE_STATS_INC(service,
6607 + return VCHIQ_ERROR;
6610 + if (SRVTRACE_ENABLED(service,
6612 + vchiq_log_dump_mem("Sent", 0,
6613 + header->data + pos,
6615 + elements[0].size));
6619 + spin_lock("a_spinlock);
6620 + service_quota->message_use_count++;
6623 + SLOT_QUEUE_INDEX_FROM_POS(state->local_tx_pos - 1);
6625 + /* If this transmission can't fit in the last slot used by any
6626 + ** service, the data_use_count must be increased. */
6627 + if (tx_end_index != state->previous_data_index) {
6628 + state->previous_data_index = tx_end_index;
6629 + state->data_use_count++;
6632 + /* If this isn't the same slot last used by this service,
6633 + ** the service's slot_use_count must be increased. */
6634 + if (tx_end_index != service_quota->previous_tx_index) {
6635 + service_quota->previous_tx_index = tx_end_index;
6636 + slot_use_count = ++service_quota->slot_use_count;
6638 + slot_use_count = 0;
6641 + spin_unlock("a_spinlock);
6643 + if (slot_use_count)
6644 + vchiq_log_trace(vchiq_core_log_level,
6645 + "%d: qm:%d %s,%x - slot_use->%d (hdr %p)",
6646 + state->id, service->localport,
6647 + msg_type_str(VCHIQ_MSG_TYPE(msgid)), size,
6648 + slot_use_count, header);
6650 + VCHIQ_SERVICE_STATS_INC(service, ctrl_tx_count);
6651 + VCHIQ_SERVICE_STATS_ADD(service, ctrl_tx_bytes, size);
6653 + vchiq_log_info(vchiq_core_log_level,
6654 + "%d: qm %s@%x,%x (%d->%d)", state->id,
6655 + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
6656 + (unsigned int)header, size,
6657 + VCHIQ_MSG_SRCPORT(msgid),
6658 + VCHIQ_MSG_DSTPORT(msgid));
6660 + WARN_ON(!((count == 1) && (size == elements[0].size)));
6661 + memcpy(header->data, elements[0].data,
6662 + elements[0].size);
6664 + VCHIQ_STATS_INC(state, ctrl_tx_count);
6667 + header->msgid = msgid;
6668 + header->size = size;
6673 + svc_fourcc = service
6674 + ? service->base.fourcc
6675 + : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
6677 + vchiq_log_info(SRVTRACE_LEVEL(service),
6678 + "Sent Msg %s(%u) to %c%c%c%c s:%u d:%d len:%d",
6679 + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
6680 + VCHIQ_MSG_TYPE(msgid),
6681 + VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
6682 + VCHIQ_MSG_SRCPORT(msgid),
6683 + VCHIQ_MSG_DSTPORT(msgid),
6687 + /* Make sure the new header is visible to the peer. */
6690 + /* Make the new tx_pos visible to the peer. */
6691 + local->tx_pos = state->local_tx_pos;
6694 + if (service && (type == VCHIQ_MSG_CLOSE))
6695 + vchiq_set_service_state(service, VCHIQ_SRVSTATE_CLOSESENT);
6697 + if (VCHIQ_MSG_TYPE(msgid) != VCHIQ_MSG_PAUSE)
6698 + mutex_unlock(&state->slot_mutex);
6700 + remote_event_signal(&state->remote->trigger);
6702 + return VCHIQ_SUCCESS;
6705 +/* Called by the slot handler and application threads */
6706 +static VCHIQ_STATUS_T
6707 +queue_message_sync(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service,
6708 + int msgid, const VCHIQ_ELEMENT_T *elements,
6709 + int count, int size, int is_blocking)
6711 + VCHIQ_SHARED_STATE_T *local;
6712 + VCHIQ_HEADER_T *header;
6714 + local = state->local;
6716 + if ((VCHIQ_MSG_TYPE(msgid) != VCHIQ_MSG_RESUME) &&
6717 + (mutex_lock_interruptible(&state->sync_mutex) != 0))
6718 + return VCHIQ_RETRY;
6720 + remote_event_wait(&local->sync_release);
6724 + header = (VCHIQ_HEADER_T *)SLOT_DATA_FROM_INDEX(state,
6725 + local->slot_sync);
6728 + int oldmsgid = header->msgid;
6729 + if (oldmsgid != VCHIQ_MSGID_PADDING)
6730 + vchiq_log_error(vchiq_core_log_level,
6731 + "%d: qms - msgid %x, not PADDING",
6732 + state->id, oldmsgid);
6738 + vchiq_log_info(vchiq_sync_log_level,
6739 + "%d: qms %s@%x,%x (%d->%d)", state->id,
6740 + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
6741 + (unsigned int)header, size,
6742 + VCHIQ_MSG_SRCPORT(msgid),
6743 + VCHIQ_MSG_DSTPORT(msgid));
6745 + for (i = 0, pos = 0; i < (unsigned int)count;
6746 + pos += elements[i++].size)
6747 + if (elements[i].size) {
6748 + if (vchiq_copy_from_user
6749 + (header->data + pos, elements[i].data,
6750 + (size_t) elements[i].size) !=
6752 + mutex_unlock(&state->sync_mutex);
6753 + VCHIQ_SERVICE_STATS_INC(service,
6755 + return VCHIQ_ERROR;
6758 + if (vchiq_sync_log_level >=
6760 + vchiq_log_dump_mem("Sent Sync",
6761 + 0, header->data + pos,
6763 + elements[0].size));
6767 + VCHIQ_SERVICE_STATS_INC(service, ctrl_tx_count);
6768 + VCHIQ_SERVICE_STATS_ADD(service, ctrl_tx_bytes, size);
6770 + vchiq_log_info(vchiq_sync_log_level,
6771 + "%d: qms %s@%x,%x (%d->%d)", state->id,
6772 + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
6773 + (unsigned int)header, size,
6774 + VCHIQ_MSG_SRCPORT(msgid),
6775 + VCHIQ_MSG_DSTPORT(msgid));
6777 + WARN_ON(!((count == 1) && (size == elements[0].size)));
6778 + memcpy(header->data, elements[0].data,
6779 + elements[0].size);
6781 + VCHIQ_STATS_INC(state, ctrl_tx_count);
6784 + header->size = size;
6785 + header->msgid = msgid;
6787 + if (vchiq_sync_log_level >= VCHIQ_LOG_TRACE) {
6790 + svc_fourcc = service
6791 + ? service->base.fourcc
6792 + : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
6794 + vchiq_log_trace(vchiq_sync_log_level,
6795 + "Sent Sync Msg %s(%u) to %c%c%c%c s:%u d:%d len:%d",
6796 + msg_type_str(VCHIQ_MSG_TYPE(msgid)),
6797 + VCHIQ_MSG_TYPE(msgid),
6798 + VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
6799 + VCHIQ_MSG_SRCPORT(msgid),
6800 + VCHIQ_MSG_DSTPORT(msgid),
6804 + /* Make sure the new header is visible to the peer. */
6807 + remote_event_signal(&state->remote->sync_trigger);
6809 + if (VCHIQ_MSG_TYPE(msgid) != VCHIQ_MSG_PAUSE)
6810 + mutex_unlock(&state->sync_mutex);
6812 + return VCHIQ_SUCCESS;
6816 +claim_slot(VCHIQ_SLOT_INFO_T *slot)
6818 + slot->use_count++;
6822 +release_slot(VCHIQ_STATE_T *state, VCHIQ_SLOT_INFO_T *slot_info,
6823 + VCHIQ_HEADER_T *header, VCHIQ_SERVICE_T *service)
6825 + int release_count;
6827 + mutex_lock(&state->recycle_mutex);
6830 + int msgid = header->msgid;
6831 + if (((msgid & VCHIQ_MSGID_CLAIMED) == 0) ||
6832 + (service && service->closing)) {
6833 + mutex_unlock(&state->recycle_mutex);
6837 + /* Rewrite the message header to prevent a double
6839 + header->msgid = msgid & ~VCHIQ_MSGID_CLAIMED;
6842 + release_count = slot_info->release_count;
6843 + slot_info->release_count = ++release_count;
6845 + if (release_count == slot_info->use_count) {
6846 + int slot_queue_recycle;
6847 + /* Add to the freed queue */
6849 + /* A read barrier is necessary here to prevent speculative
6850 + ** fetches of remote->slot_queue_recycle from overtaking the
6854 + slot_queue_recycle = state->remote->slot_queue_recycle;
6855 + state->remote->slot_queue[slot_queue_recycle &
6856 + VCHIQ_SLOT_QUEUE_MASK] =
6857 + SLOT_INDEX_FROM_INFO(state, slot_info);
6858 + state->remote->slot_queue_recycle = slot_queue_recycle + 1;
6859 + vchiq_log_info(vchiq_core_log_level,
6860 + "%d: release_slot %d - recycle->%x",
6861 + state->id, SLOT_INDEX_FROM_INFO(state, slot_info),
6862 + state->remote->slot_queue_recycle);
6864 + /* A write barrier is necessary, but remote_event_signal
6865 + ** contains one. */
6866 + remote_event_signal(&state->remote->recycle);
6869 + mutex_unlock(&state->recycle_mutex);
6872 +/* Called by the slot handler - don't hold the bulk mutex */
6873 +static VCHIQ_STATUS_T
6874 +notify_bulks(VCHIQ_SERVICE_T *service, VCHIQ_BULK_QUEUE_T *queue,
6877 + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
6879 + vchiq_log_trace(vchiq_core_log_level,
6880 + "%d: nb:%d %cx - p=%x rn=%x r=%x",
6881 + service->state->id, service->localport,
6882 + (queue == &service->bulk_tx) ? 't' : 'r',
6883 + queue->process, queue->remote_notify, queue->remove);
6885 + if (service->state->is_master) {
6886 + while (queue->remote_notify != queue->process) {
6887 + VCHIQ_BULK_T *bulk =
6888 + &queue->bulks[BULK_INDEX(queue->remote_notify)];
6889 + int msgtype = (bulk->dir == VCHIQ_BULK_TRANSMIT) ?
6890 + VCHIQ_MSG_BULK_RX_DONE : VCHIQ_MSG_BULK_TX_DONE;
6891 + int msgid = VCHIQ_MAKE_MSG(msgtype, service->localport,
6892 + service->remoteport);
6893 + VCHIQ_ELEMENT_T element = { &bulk->actual, 4 };
6894 + /* Only reply to non-dummy bulk requests */
6895 + if (bulk->remote_data) {
6896 + status = queue_message(service->state, NULL,
6897 + msgid, &element, 1, 4, 0);
6898 + if (status != VCHIQ_SUCCESS)
6901 + queue->remote_notify++;
6904 + queue->remote_notify = queue->process;
6907 + if (status == VCHIQ_SUCCESS) {
6908 + while (queue->remove != queue->remote_notify) {
6909 + VCHIQ_BULK_T *bulk =
6910 + &queue->bulks[BULK_INDEX(queue->remove)];
6912 + /* Only generate callbacks for non-dummy bulk
6913 + ** requests, and non-terminated services */
6914 + if (bulk->data && service->instance) {
6915 + if (bulk->actual != VCHIQ_BULK_ACTUAL_ABORTED) {
6916 + if (bulk->dir == VCHIQ_BULK_TRANSMIT) {
6917 + VCHIQ_SERVICE_STATS_INC(service,
6919 + VCHIQ_SERVICE_STATS_ADD(service,
6923 + VCHIQ_SERVICE_STATS_INC(service,
6925 + VCHIQ_SERVICE_STATS_ADD(service,
6930 + VCHIQ_SERVICE_STATS_INC(service,
6931 + bulk_aborted_count);
6933 + if (bulk->mode == VCHIQ_BULK_MODE_BLOCKING) {
6934 + struct bulk_waiter *waiter;
6935 + spin_lock(&bulk_waiter_spinlock);
6936 + waiter = bulk->userdata;
6938 + waiter->actual = bulk->actual;
6939 + up(&waiter->event);
6941 + spin_unlock(&bulk_waiter_spinlock);
6942 + } else if (bulk->mode ==
6943 + VCHIQ_BULK_MODE_CALLBACK) {
6944 + VCHIQ_REASON_T reason = (bulk->dir ==
6945 + VCHIQ_BULK_TRANSMIT) ?
6947 + VCHIQ_BULK_ACTUAL_ABORTED) ?
6948 + VCHIQ_BULK_TRANSMIT_ABORTED :
6949 + VCHIQ_BULK_TRANSMIT_DONE) :
6951 + VCHIQ_BULK_ACTUAL_ABORTED) ?
6952 + VCHIQ_BULK_RECEIVE_ABORTED :
6953 + VCHIQ_BULK_RECEIVE_DONE);
6954 + status = make_service_callback(service,
6955 + reason, NULL, bulk->userdata);
6956 + if (status == VCHIQ_RETRY)
6962 + up(&service->bulk_remove_event);
6965 + status = VCHIQ_SUCCESS;
6968 + if (status == VCHIQ_RETRY)
6969 + request_poll(service->state, service,
6970 + (queue == &service->bulk_tx) ?
6971 + VCHIQ_POLL_TXNOTIFY : VCHIQ_POLL_RXNOTIFY);
6976 +/* Called by the slot handler thread */
6978 +poll_services(VCHIQ_STATE_T *state)
6982 + for (group = 0; group < BITSET_SIZE(state->unused_service); group++) {
6984 + flags = atomic_xchg(&state->poll_services[group], 0);
6985 + for (i = 0; flags; i++) {
6986 + if (flags & (1 << i)) {
6987 + VCHIQ_SERVICE_T *service =
6988 + find_service_by_port(state,
6990 + uint32_t service_flags;
6991 + flags &= ~(1 << i);
6995 + atomic_xchg(&service->poll_flags, 0);
6996 + if (service_flags &
6997 + (1 << VCHIQ_POLL_REMOVE)) {
6998 + vchiq_log_info(vchiq_core_log_level,
6999 + "%d: ps - remove %d<->%d",
7000 + state->id, service->localport,
7001 + service->remoteport);
7003 + /* Make it look like a client, because
7004 + it must be removed and not left in
7005 + the LISTENING state. */
7006 + service->public_fourcc =
7007 + VCHIQ_FOURCC_INVALID;
7009 + if (vchiq_close_service_internal(
7010 + service, 0/*!close_recvd*/) !=
7012 + request_poll(state, service,
7013 + VCHIQ_POLL_REMOVE);
7014 + } else if (service_flags &
7015 + (1 << VCHIQ_POLL_TERMINATE)) {
7016 + vchiq_log_info(vchiq_core_log_level,
7017 + "%d: ps - terminate %d<->%d",
7018 + state->id, service->localport,
7019 + service->remoteport);
7020 + if (vchiq_close_service_internal(
7021 + service, 0/*!close_recvd*/) !=
7023 + request_poll(state, service,
7024 + VCHIQ_POLL_TERMINATE);
7026 + if (service_flags & (1 << VCHIQ_POLL_TXNOTIFY))
7027 + notify_bulks(service,
7028 + &service->bulk_tx,
7030 + if (service_flags & (1 << VCHIQ_POLL_RXNOTIFY))
7031 + notify_bulks(service,
7032 + &service->bulk_rx,
7034 + unlock_service(service);
7040 +/* Called by the slot handler or application threads, holding the bulk mutex. */
7042 +resolve_bulks(VCHIQ_SERVICE_T *service, VCHIQ_BULK_QUEUE_T *queue)
7044 + VCHIQ_STATE_T *state = service->state;
7048 + while ((queue->process != queue->local_insert) &&
7049 + (queue->process != queue->remote_insert)) {
7050 + VCHIQ_BULK_T *bulk = &queue->bulks[BULK_INDEX(queue->process)];
7052 + vchiq_log_trace(vchiq_core_log_level,
7053 + "%d: rb:%d %cx - li=%x ri=%x p=%x",
7054 + state->id, service->localport,
7055 + (queue == &service->bulk_tx) ? 't' : 'r',
7056 + queue->local_insert, queue->remote_insert,
7059 + WARN_ON(!((int)(queue->local_insert - queue->process) > 0));
7060 + WARN_ON(!((int)(queue->remote_insert - queue->process) > 0));
7062 + rc = mutex_lock_interruptible(&state->bulk_transfer_mutex);
7066 + vchiq_transfer_bulk(bulk);
7067 + mutex_unlock(&state->bulk_transfer_mutex);
7069 + if (SRVTRACE_ENABLED(service, VCHIQ_LOG_INFO)) {
7070 + const char *header = (queue == &service->bulk_tx) ?
7071 + "Send Bulk to" : "Recv Bulk from";
7072 + if (bulk->actual != VCHIQ_BULK_ACTUAL_ABORTED)
7073 + vchiq_log_info(SRVTRACE_LEVEL(service),
7074 + "%s %c%c%c%c d:%d len:%d %x<->%x",
7076 + VCHIQ_FOURCC_AS_4CHARS(
7077 + service->base.fourcc),
7078 + service->remoteport,
7080 + (unsigned int)bulk->data,
7081 + (unsigned int)bulk->remote_data);
7083 + vchiq_log_info(SRVTRACE_LEVEL(service),
7084 + "%s %c%c%c%c d:%d ABORTED - tx len:%d,"
7085 + " rx len:%d %x<->%x",
7087 + VCHIQ_FOURCC_AS_4CHARS(
7088 + service->base.fourcc),
7089 + service->remoteport,
7091 + bulk->remote_size,
7092 + (unsigned int)bulk->data,
7093 + (unsigned int)bulk->remote_data);
7096 + vchiq_complete_bulk(bulk);
7103 +/* Called with the bulk_mutex held */
7105 +abort_outstanding_bulks(VCHIQ_SERVICE_T *service, VCHIQ_BULK_QUEUE_T *queue)
7107 + int is_tx = (queue == &service->bulk_tx);
7108 + vchiq_log_trace(vchiq_core_log_level,
7109 + "%d: aob:%d %cx - li=%x ri=%x p=%x",
7110 + service->state->id, service->localport, is_tx ? 't' : 'r',
7111 + queue->local_insert, queue->remote_insert, queue->process);
7113 + WARN_ON(!((int)(queue->local_insert - queue->process) >= 0));
7114 + WARN_ON(!((int)(queue->remote_insert - queue->process) >= 0));
7116 + while ((queue->process != queue->local_insert) ||
7117 + (queue->process != queue->remote_insert)) {
7118 + VCHIQ_BULK_T *bulk = &queue->bulks[BULK_INDEX(queue->process)];
7120 + if (queue->process == queue->remote_insert) {
7121 + /* fabricate a matching dummy bulk */
7122 + bulk->remote_data = NULL;
7123 + bulk->remote_size = 0;
7124 + queue->remote_insert++;
7127 + if (queue->process != queue->local_insert) {
7128 + vchiq_complete_bulk(bulk);
7130 + vchiq_log_info(SRVTRACE_LEVEL(service),
7131 + "%s %c%c%c%c d:%d ABORTED - tx len:%d, "
7133 + is_tx ? "Send Bulk to" : "Recv Bulk from",
7134 + VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
7135 + service->remoteport,
7137 + bulk->remote_size);
7139 + /* fabricate a matching dummy bulk */
7140 + bulk->data = NULL;
7142 + bulk->actual = VCHIQ_BULK_ACTUAL_ABORTED;
7143 + bulk->dir = is_tx ? VCHIQ_BULK_TRANSMIT :
7144 + VCHIQ_BULK_RECEIVE;
7145 + queue->local_insert++;
7152 +/* Called from the slot handler thread */
7154 +pause_bulks(VCHIQ_STATE_T *state)
7156 + if (unlikely(atomic_inc_return(&pause_bulks_count) != 1)) {
7158 + atomic_set(&pause_bulks_count, 1);
7162 + /* Block bulk transfers from all services */
7163 + mutex_lock(&state->bulk_transfer_mutex);
7166 +/* Called from the slot handler thread */
7168 +resume_bulks(VCHIQ_STATE_T *state)
7171 + if (unlikely(atomic_dec_return(&pause_bulks_count) != 0)) {
7173 + atomic_set(&pause_bulks_count, 0);
7177 + /* Allow bulk transfers from all services */
7178 + mutex_unlock(&state->bulk_transfer_mutex);
7180 + if (state->deferred_bulks == 0)
7183 + /* Deal with any bulks which had to be deferred due to being in
7184 + * paused state. Don't try to match up to number of deferred bulks
7185 + * in case we've had something come and close the service in the
7186 + * interim - just process all bulk queues for all services */
7187 + vchiq_log_info(vchiq_core_log_level, "%s: processing %d deferred bulks",
7188 + __func__, state->deferred_bulks);
7190 + for (i = 0; i < state->unused_service; i++) {
7191 + VCHIQ_SERVICE_T *service = state->services[i];
7192 + int resolved_rx = 0;
7193 + int resolved_tx = 0;
7194 + if (!service || (service->srvstate != VCHIQ_SRVSTATE_OPEN))
7197 + mutex_lock(&service->bulk_mutex);
7198 + resolved_rx = resolve_bulks(service, &service->bulk_rx);
7199 + resolved_tx = resolve_bulks(service, &service->bulk_tx);
7200 + mutex_unlock(&service->bulk_mutex);
7202 + notify_bulks(service, &service->bulk_rx, 1);
7204 + notify_bulks(service, &service->bulk_tx, 1);
7206 + state->deferred_bulks = 0;
7210 +parse_open(VCHIQ_STATE_T *state, VCHIQ_HEADER_T *header)
7212 + VCHIQ_SERVICE_T *service = NULL;
7215 + unsigned int localport, remoteport;
7217 + msgid = header->msgid;
7218 + size = header->size;
7219 + type = VCHIQ_MSG_TYPE(msgid);
7220 + localport = VCHIQ_MSG_DSTPORT(msgid);
7221 + remoteport = VCHIQ_MSG_SRCPORT(msgid);
7222 + if (size >= sizeof(struct vchiq_open_payload)) {
7223 + const struct vchiq_open_payload *payload =
7224 + (struct vchiq_open_payload *)header->data;
7225 + unsigned int fourcc;
7227 + fourcc = payload->fourcc;
7228 + vchiq_log_info(vchiq_core_log_level,
7229 + "%d: prs OPEN@%x (%d->'%c%c%c%c')",
7230 + state->id, (unsigned int)header,
7232 + VCHIQ_FOURCC_AS_4CHARS(fourcc));
7234 + service = get_listening_service(state, fourcc);
7237 + /* A matching service exists */
7238 + short version = payload->version;
7239 + short version_min = payload->version_min;
7240 + if ((service->version < version_min) ||
7241 + (version < service->version_min)) {
7242 + /* Version mismatch */
7243 + vchiq_loud_error_header();
7244 + vchiq_loud_error("%d: service %d (%c%c%c%c) "
7245 + "version mismatch - local (%d, min %d)"
7246 + " vs. remote (%d, min %d)",
7247 + state->id, service->localport,
7248 + VCHIQ_FOURCC_AS_4CHARS(fourcc),
7249 + service->version, service->version_min,
7250 + version, version_min);
7251 + vchiq_loud_error_footer();
7252 + unlock_service(service);
7256 + service->peer_version = version;
7258 + if (service->srvstate == VCHIQ_SRVSTATE_LISTENING) {
7259 + struct vchiq_openack_payload ack_payload = {
7262 + VCHIQ_ELEMENT_T body = {
7264 + sizeof(ack_payload)
7267 + /* Acknowledge the OPEN */
7268 + if (service->sync) {
7269 + if (queue_message_sync(state, NULL,
7271 + VCHIQ_MSG_OPENACK,
7272 + service->localport,
7274 + &body, 1, sizeof(ack_payload),
7275 + 0) == VCHIQ_RETRY)
7276 + goto bail_not_ready;
7278 + if (queue_message(state, NULL,
7280 + VCHIQ_MSG_OPENACK,
7281 + service->localport,
7283 + &body, 1, sizeof(ack_payload),
7284 + 0) == VCHIQ_RETRY)
7285 + goto bail_not_ready;
7288 + /* The service is now open */
7289 + vchiq_set_service_state(service,
7290 + service->sync ? VCHIQ_SRVSTATE_OPENSYNC
7291 + : VCHIQ_SRVSTATE_OPEN);
7294 + service->remoteport = remoteport;
7295 + service->client_id = ((int *)header->data)[1];
7296 + if (make_service_callback(service, VCHIQ_SERVICE_OPENED,
7297 + NULL, NULL) == VCHIQ_RETRY) {
7298 + /* Bail out if not ready */
7299 + service->remoteport = VCHIQ_PORT_FREE;
7300 + goto bail_not_ready;
7303 + /* Success - the message has been dealt with */
7304 + unlock_service(service);
7310 + /* No available service, or an invalid request - send a CLOSE */
7311 + if (queue_message(state, NULL,
7312 + VCHIQ_MAKE_MSG(VCHIQ_MSG_CLOSE, 0, VCHIQ_MSG_SRCPORT(msgid)),
7313 + NULL, 0, 0, 0) == VCHIQ_RETRY)
7314 + goto bail_not_ready;
7320 + unlock_service(service);
7325 +/* Called by the slot handler thread */
7327 +parse_rx_slots(VCHIQ_STATE_T *state)
7329 + VCHIQ_SHARED_STATE_T *remote = state->remote;
7330 + VCHIQ_SERVICE_T *service = NULL;
7332 + DEBUG_INITIALISE(state->local)
7334 + tx_pos = remote->tx_pos;
7336 + while (state->rx_pos != tx_pos) {
7337 + VCHIQ_HEADER_T *header;
7340 + unsigned int localport, remoteport;
7342 + DEBUG_TRACE(PARSE_LINE);
7343 + if (!state->rx_data) {
7345 + WARN_ON(!((state->rx_pos & VCHIQ_SLOT_MASK) == 0));
7346 + rx_index = remote->slot_queue[
7347 + SLOT_QUEUE_INDEX_FROM_POS(state->rx_pos) &
7348 + VCHIQ_SLOT_QUEUE_MASK];
7349 + state->rx_data = (char *)SLOT_DATA_FROM_INDEX(state,
7351 + state->rx_info = SLOT_INFO_FROM_INDEX(state, rx_index);
7353 + /* Initialise use_count to one, and increment
7354 + ** release_count at the end of the slot to avoid
7355 + ** releasing the slot prematurely. */
7356 + state->rx_info->use_count = 1;
7357 + state->rx_info->release_count = 0;
7360 + header = (VCHIQ_HEADER_T *)(state->rx_data +
7361 + (state->rx_pos & VCHIQ_SLOT_MASK));
7362 + DEBUG_VALUE(PARSE_HEADER, (int)header);
7363 + msgid = header->msgid;
7364 + DEBUG_VALUE(PARSE_MSGID, msgid);
7365 + size = header->size;
7366 + type = VCHIQ_MSG_TYPE(msgid);
7367 + localport = VCHIQ_MSG_DSTPORT(msgid);
7368 + remoteport = VCHIQ_MSG_SRCPORT(msgid);
7370 + if (type != VCHIQ_MSG_DATA)
7371 + VCHIQ_STATS_INC(state, ctrl_rx_count);
7374 + case VCHIQ_MSG_OPENACK:
7375 + case VCHIQ_MSG_CLOSE:
7376 + case VCHIQ_MSG_DATA:
7377 + case VCHIQ_MSG_BULK_RX:
7378 + case VCHIQ_MSG_BULK_TX:
7379 + case VCHIQ_MSG_BULK_RX_DONE:
7380 + case VCHIQ_MSG_BULK_TX_DONE:
7381 + service = find_service_by_port(state, localport);
7383 + ((service->remoteport != remoteport) &&
7384 + (service->remoteport != VCHIQ_PORT_FREE))) &&
7385 + (localport == 0) &&
7386 + (type == VCHIQ_MSG_CLOSE)) {
7387 + /* This could be a CLOSE from a client which
7388 + hadn't yet received the OPENACK - look for
7389 + the connected service */
7391 + unlock_service(service);
7392 + service = get_connected_service(state,
7395 + vchiq_log_warning(vchiq_core_log_level,
7396 + "%d: prs %s@%x (%d->%d) - "
7397 + "found connected service %d",
7398 + state->id, msg_type_str(type),
7399 + (unsigned int)header,
7400 + remoteport, localport,
7401 + service->localport);
7405 + vchiq_log_error(vchiq_core_log_level,
7406 + "%d: prs %s@%x (%d->%d) - "
7407 + "invalid/closed service %d",
7408 + state->id, msg_type_str(type),
7409 + (unsigned int)header,
7410 + remoteport, localport, localport);
7411 + goto skip_message;
7418 + if (SRVTRACE_ENABLED(service, VCHIQ_LOG_INFO)) {
7421 + svc_fourcc = service
7422 + ? service->base.fourcc
7423 + : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
7424 + vchiq_log_info(SRVTRACE_LEVEL(service),
7425 + "Rcvd Msg %s(%u) from %c%c%c%c s:%d d:%d "
7427 + msg_type_str(type), type,
7428 + VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
7429 + remoteport, localport, size);
7431 + vchiq_log_dump_mem("Rcvd", 0, header->data,
7435 + if (((unsigned int)header & VCHIQ_SLOT_MASK) + calc_stride(size)
7436 + > VCHIQ_SLOT_SIZE) {
7437 + vchiq_log_error(vchiq_core_log_level,
7438 + "header %x (msgid %x) - size %x too big for "
7440 + (unsigned int)header, (unsigned int)msgid,
7441 + (unsigned int)size);
7442 + WARN(1, "oversized for slot\n");
7446 + case VCHIQ_MSG_OPEN:
7447 + WARN_ON(!(VCHIQ_MSG_DSTPORT(msgid) == 0));
7448 + if (!parse_open(state, header))
7449 + goto bail_not_ready;
7451 + case VCHIQ_MSG_OPENACK:
7452 + if (size >= sizeof(struct vchiq_openack_payload)) {
7453 + const struct vchiq_openack_payload *payload =
7454 + (struct vchiq_openack_payload *)
7456 + service->peer_version = payload->version;
7458 + vchiq_log_info(vchiq_core_log_level,
7459 + "%d: prs OPENACK@%x,%x (%d->%d) v:%d",
7460 + state->id, (unsigned int)header, size,
7461 + remoteport, localport, service->peer_version);
7462 + if (service->srvstate ==
7463 + VCHIQ_SRVSTATE_OPENING) {
7464 + service->remoteport = remoteport;
7465 + vchiq_set_service_state(service,
7466 + VCHIQ_SRVSTATE_OPEN);
7467 + up(&service->remove_event);
7469 + vchiq_log_error(vchiq_core_log_level,
7470 + "OPENACK received in state %s",
7471 + srvstate_names[service->srvstate]);
7473 + case VCHIQ_MSG_CLOSE:
7474 + WARN_ON(size != 0); /* There should be no data */
7476 + vchiq_log_info(vchiq_core_log_level,
7477 + "%d: prs CLOSE@%x (%d->%d)",
7478 + state->id, (unsigned int)header,
7479 + remoteport, localport);
7481 + mark_service_closing_internal(service, 1);
7483 + if (vchiq_close_service_internal(service,
7484 + 1/*close_recvd*/) == VCHIQ_RETRY)
7485 + goto bail_not_ready;
7487 + vchiq_log_info(vchiq_core_log_level,
7488 + "Close Service %c%c%c%c s:%u d:%d",
7489 + VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
7490 + service->localport,
7491 + service->remoteport);
7493 + case VCHIQ_MSG_DATA:
7494 + vchiq_log_trace(vchiq_core_log_level,
7495 + "%d: prs DATA@%x,%x (%d->%d)",
7496 + state->id, (unsigned int)header, size,
7497 + remoteport, localport);
7499 + if ((service->remoteport == remoteport)
7500 + && (service->srvstate ==
7501 + VCHIQ_SRVSTATE_OPEN)) {
7502 + header->msgid = msgid | VCHIQ_MSGID_CLAIMED;
7503 + claim_slot(state->rx_info);
7504 + DEBUG_TRACE(PARSE_LINE);
7505 + if (make_service_callback(service,
7506 + VCHIQ_MESSAGE_AVAILABLE, header,
7507 + NULL) == VCHIQ_RETRY) {
7508 + DEBUG_TRACE(PARSE_LINE);
7509 + goto bail_not_ready;
7511 + VCHIQ_SERVICE_STATS_INC(service, ctrl_rx_count);
7512 + VCHIQ_SERVICE_STATS_ADD(service, ctrl_rx_bytes,
7515 + VCHIQ_STATS_INC(state, error_count);
7518 + case VCHIQ_MSG_CONNECT:
7519 + vchiq_log_info(vchiq_core_log_level,
7520 + "%d: prs CONNECT@%x",
7521 + state->id, (unsigned int)header);
7522 + up(&state->connect);
7524 + case VCHIQ_MSG_BULK_RX:
7525 + case VCHIQ_MSG_BULK_TX: {
7526 + VCHIQ_BULK_QUEUE_T *queue;
7527 + WARN_ON(!state->is_master);
7528 + queue = (type == VCHIQ_MSG_BULK_RX) ?
7529 + &service->bulk_tx : &service->bulk_rx;
7530 + if ((service->remoteport == remoteport)
7531 + && (service->srvstate ==
7532 + VCHIQ_SRVSTATE_OPEN)) {
7533 + VCHIQ_BULK_T *bulk;
7536 + DEBUG_TRACE(PARSE_LINE);
7537 + if (mutex_lock_interruptible(
7538 + &service->bulk_mutex) != 0) {
7539 + DEBUG_TRACE(PARSE_LINE);
7540 + goto bail_not_ready;
7543 + WARN_ON(!(queue->remote_insert < queue->remove +
7544 + VCHIQ_NUM_SERVICE_BULKS));
7545 + bulk = &queue->bulks[
7546 + BULK_INDEX(queue->remote_insert)];
7547 + bulk->remote_data =
7548 + (void *)((int *)header->data)[0];
7549 + bulk->remote_size = ((int *)header->data)[1];
7552 + vchiq_log_info(vchiq_core_log_level,
7553 + "%d: prs %s@%x (%d->%d) %x@%x",
7554 + state->id, msg_type_str(type),
7555 + (unsigned int)header,
7556 + remoteport, localport,
7557 + bulk->remote_size,
7558 + (unsigned int)bulk->remote_data);
7560 + queue->remote_insert++;
7562 + if (atomic_read(&pause_bulks_count)) {
7563 + state->deferred_bulks++;
7564 + vchiq_log_info(vchiq_core_log_level,
7565 + "%s: deferring bulk (%d)",
7567 + state->deferred_bulks);
7568 + if (state->conn_state !=
7569 + VCHIQ_CONNSTATE_PAUSE_SENT)
7571 + vchiq_core_log_level,
7572 + "%s: bulks paused in "
7573 + "unexpected state %s",
7576 + state->conn_state]);
7577 + } else if (state->conn_state ==
7578 + VCHIQ_CONNSTATE_CONNECTED) {
7579 + DEBUG_TRACE(PARSE_LINE);
7580 + resolved = resolve_bulks(service,
7584 + mutex_unlock(&service->bulk_mutex);
7586 + notify_bulks(service, queue,
7590 + case VCHIQ_MSG_BULK_RX_DONE:
7591 + case VCHIQ_MSG_BULK_TX_DONE:
7592 + WARN_ON(state->is_master);
7593 + if ((service->remoteport == remoteport)
7594 + && (service->srvstate !=
7595 + VCHIQ_SRVSTATE_FREE)) {
7596 + VCHIQ_BULK_QUEUE_T *queue;
7597 + VCHIQ_BULK_T *bulk;
7599 + queue = (type == VCHIQ_MSG_BULK_RX_DONE) ?
7600 + &service->bulk_rx : &service->bulk_tx;
7602 + DEBUG_TRACE(PARSE_LINE);
7603 + if (mutex_lock_interruptible(
7604 + &service->bulk_mutex) != 0) {
7605 + DEBUG_TRACE(PARSE_LINE);
7606 + goto bail_not_ready;
7608 + if ((int)(queue->remote_insert -
7609 + queue->local_insert) >= 0) {
7610 + vchiq_log_error(vchiq_core_log_level,
7611 + "%d: prs %s@%x (%d->%d) "
7612 + "unexpected (ri=%d,li=%d)",
7613 + state->id, msg_type_str(type),
7614 + (unsigned int)header,
7615 + remoteport, localport,
7616 + queue->remote_insert,
7617 + queue->local_insert);
7618 + mutex_unlock(&service->bulk_mutex);
7622 + BUG_ON(queue->process == queue->local_insert);
7623 + BUG_ON(queue->process != queue->remote_insert);
7625 + bulk = &queue->bulks[
7626 + BULK_INDEX(queue->remote_insert)];
7627 + bulk->actual = *(int *)header->data;
7628 + queue->remote_insert++;
7630 + vchiq_log_info(vchiq_core_log_level,
7631 + "%d: prs %s@%x (%d->%d) %x@%x",
7632 + state->id, msg_type_str(type),
7633 + (unsigned int)header,
7634 + remoteport, localport,
7635 + bulk->actual, (unsigned int)bulk->data);
7637 + vchiq_log_trace(vchiq_core_log_level,
7638 + "%d: prs:%d %cx li=%x ri=%x p=%x",
7639 + state->id, localport,
7640 + (type == VCHIQ_MSG_BULK_RX_DONE) ?
7642 + queue->local_insert,
7643 + queue->remote_insert, queue->process);
7645 + DEBUG_TRACE(PARSE_LINE);
7646 + WARN_ON(queue->process == queue->local_insert);
7647 + vchiq_complete_bulk(bulk);
7649 + mutex_unlock(&service->bulk_mutex);
7650 + DEBUG_TRACE(PARSE_LINE);
7651 + notify_bulks(service, queue, 1/*retry_poll*/);
7652 + DEBUG_TRACE(PARSE_LINE);
7655 + case VCHIQ_MSG_PADDING:
7656 + vchiq_log_trace(vchiq_core_log_level,
7657 + "%d: prs PADDING@%x,%x",
7658 + state->id, (unsigned int)header, size);
7660 + case VCHIQ_MSG_PAUSE:
7661 + /* If initiated, signal the application thread */
7662 + vchiq_log_trace(vchiq_core_log_level,
7663 + "%d: prs PAUSE@%x,%x",
7664 + state->id, (unsigned int)header, size);
7665 + if (state->conn_state == VCHIQ_CONNSTATE_PAUSED) {
7666 + vchiq_log_error(vchiq_core_log_level,
7667 + "%d: PAUSE received in state PAUSED",
7671 + if (state->conn_state != VCHIQ_CONNSTATE_PAUSE_SENT) {
7672 + /* Send a PAUSE in response */
7673 + if (queue_message(state, NULL,
7674 + VCHIQ_MAKE_MSG(VCHIQ_MSG_PAUSE, 0, 0),
7675 + NULL, 0, 0, 0) == VCHIQ_RETRY)
7676 + goto bail_not_ready;
7677 + if (state->is_master)
7678 + pause_bulks(state);
7680 + /* At this point slot_mutex is held */
7681 + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_PAUSED);
7682 + vchiq_platform_paused(state);
7684 + case VCHIQ_MSG_RESUME:
7685 + vchiq_log_trace(vchiq_core_log_level,
7686 + "%d: prs RESUME@%x,%x",
7687 + state->id, (unsigned int)header, size);
7688 + /* Release the slot mutex */
7689 + mutex_unlock(&state->slot_mutex);
7690 + if (state->is_master)
7691 + resume_bulks(state);
7692 + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_CONNECTED);
7693 + vchiq_platform_resumed(state);
7696 + case VCHIQ_MSG_REMOTE_USE:
7697 + vchiq_on_remote_use(state);
7699 + case VCHIQ_MSG_REMOTE_RELEASE:
7700 + vchiq_on_remote_release(state);
7702 + case VCHIQ_MSG_REMOTE_USE_ACTIVE:
7703 + vchiq_on_remote_use_active(state);
7707 + vchiq_log_error(vchiq_core_log_level,
7708 + "%d: prs invalid msgid %x@%x,%x",
7709 + state->id, msgid, (unsigned int)header, size);
7710 + WARN(1, "invalid message\n");
7716 + unlock_service(service);
7720 + state->rx_pos += calc_stride(size);
7722 + DEBUG_TRACE(PARSE_LINE);
7723 + /* Perform some housekeeping when the end of the slot is
7725 + if ((state->rx_pos & VCHIQ_SLOT_MASK) == 0) {
7726 + /* Remove the extra reference count. */
7727 + release_slot(state, state->rx_info, NULL, NULL);
7728 + state->rx_data = NULL;
7734 + unlock_service(service);
7737 +/* Called by the slot handler thread */
7739 +slot_handler_func(void *v)
7741 + VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
7742 + VCHIQ_SHARED_STATE_T *local = state->local;
7743 + DEBUG_INITIALISE(local)
7746 + DEBUG_COUNT(SLOT_HANDLER_COUNT);
7747 + DEBUG_TRACE(SLOT_HANDLER_LINE);
7748 + remote_event_wait(&local->trigger);
7752 + DEBUG_TRACE(SLOT_HANDLER_LINE);
7753 + if (state->poll_needed) {
7754 + /* Check if we need to suspend - may change our
7756 + vchiq_platform_check_suspend(state);
7758 + state->poll_needed = 0;
7760 + /* Handle service polling and other rare conditions here
7761 + ** out of the mainline code */
7762 + switch (state->conn_state) {
7763 + case VCHIQ_CONNSTATE_CONNECTED:
7764 + /* Poll the services as requested */
7765 + poll_services(state);
7768 + case VCHIQ_CONNSTATE_PAUSING:
7769 + if (state->is_master)
7770 + pause_bulks(state);
7771 + if (queue_message(state, NULL,
7772 + VCHIQ_MAKE_MSG(VCHIQ_MSG_PAUSE, 0, 0),
7773 + NULL, 0, 0, 0) != VCHIQ_RETRY) {
7774 + vchiq_set_conn_state(state,
7775 + VCHIQ_CONNSTATE_PAUSE_SENT);
7777 + if (state->is_master)
7778 + resume_bulks(state);
7780 + state->poll_needed = 1;
7784 + case VCHIQ_CONNSTATE_PAUSED:
7785 + vchiq_platform_resume(state);
7788 + case VCHIQ_CONNSTATE_RESUMING:
7789 + if (queue_message(state, NULL,
7790 + VCHIQ_MAKE_MSG(VCHIQ_MSG_RESUME, 0, 0),
7791 + NULL, 0, 0, 0) != VCHIQ_RETRY) {
7792 + if (state->is_master)
7793 + resume_bulks(state);
7794 + vchiq_set_conn_state(state,
7795 + VCHIQ_CONNSTATE_CONNECTED);
7796 + vchiq_platform_resumed(state);
7798 + /* This should really be impossible,
7799 + ** since the PAUSE should have flushed
7800 + ** through outstanding messages. */
7801 + vchiq_log_error(vchiq_core_log_level,
7802 + "Failed to send RESUME "
7808 + case VCHIQ_CONNSTATE_PAUSE_TIMEOUT:
7809 + case VCHIQ_CONNSTATE_RESUME_TIMEOUT:
7810 + vchiq_platform_handle_timeout(state);
7819 + DEBUG_TRACE(SLOT_HANDLER_LINE);
7820 + parse_rx_slots(state);
7826 +/* Called by the recycle thread */
7828 +recycle_func(void *v)
7830 + VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
7831 + VCHIQ_SHARED_STATE_T *local = state->local;
7834 + remote_event_wait(&local->recycle);
7836 + process_free_queue(state);
7842 +/* Called by the sync thread */
7846 + VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
7847 + VCHIQ_SHARED_STATE_T *local = state->local;
7848 + VCHIQ_HEADER_T *header = (VCHIQ_HEADER_T *)SLOT_DATA_FROM_INDEX(state,
7849 + state->remote->slot_sync);
7852 + VCHIQ_SERVICE_T *service;
7855 + unsigned int localport, remoteport;
7857 + remote_event_wait(&local->sync_trigger);
7861 + msgid = header->msgid;
7862 + size = header->size;
7863 + type = VCHIQ_MSG_TYPE(msgid);
7864 + localport = VCHIQ_MSG_DSTPORT(msgid);
7865 + remoteport = VCHIQ_MSG_SRCPORT(msgid);
7867 + service = find_service_by_port(state, localport);
7870 + vchiq_log_error(vchiq_sync_log_level,
7871 + "%d: sf %s@%x (%d->%d) - "
7872 + "invalid/closed service %d",
7873 + state->id, msg_type_str(type),
7874 + (unsigned int)header,
7875 + remoteport, localport, localport);
7876 + release_message_sync(state, header);
7880 + if (vchiq_sync_log_level >= VCHIQ_LOG_TRACE) {
7883 + svc_fourcc = service
7884 + ? service->base.fourcc
7885 + : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
7886 + vchiq_log_trace(vchiq_sync_log_level,
7887 + "Rcvd Msg %s from %c%c%c%c s:%d d:%d len:%d",
7888 + msg_type_str(type),
7889 + VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
7890 + remoteport, localport, size);
7892 + vchiq_log_dump_mem("Rcvd", 0, header->data,
7897 + case VCHIQ_MSG_OPENACK:
7898 + if (size >= sizeof(struct vchiq_openack_payload)) {
7899 + const struct vchiq_openack_payload *payload =
7900 + (struct vchiq_openack_payload *)
7902 + service->peer_version = payload->version;
7904 + vchiq_log_info(vchiq_sync_log_level,
7905 + "%d: sf OPENACK@%x,%x (%d->%d) v:%d",
7906 + state->id, (unsigned int)header, size,
7907 + remoteport, localport, service->peer_version);
7908 + if (service->srvstate == VCHIQ_SRVSTATE_OPENING) {
7909 + service->remoteport = remoteport;
7910 + vchiq_set_service_state(service,
7911 + VCHIQ_SRVSTATE_OPENSYNC);
7912 + up(&service->remove_event);
7914 + release_message_sync(state, header);
7917 + case VCHIQ_MSG_DATA:
7918 + vchiq_log_trace(vchiq_sync_log_level,
7919 + "%d: sf DATA@%x,%x (%d->%d)",
7920 + state->id, (unsigned int)header, size,
7921 + remoteport, localport);
7923 + if ((service->remoteport == remoteport) &&
7924 + (service->srvstate ==
7925 + VCHIQ_SRVSTATE_OPENSYNC)) {
7926 + if (make_service_callback(service,
7927 + VCHIQ_MESSAGE_AVAILABLE, header,
7928 + NULL) == VCHIQ_RETRY)
7929 + vchiq_log_error(vchiq_sync_log_level,
7930 + "synchronous callback to "
7931 + "service %d returns "
7938 + vchiq_log_error(vchiq_sync_log_level,
7939 + "%d: sf unexpected msgid %x@%x,%x",
7940 + state->id, msgid, (unsigned int)header, size);
7941 + release_message_sync(state, header);
7945 + unlock_service(service);
7953 +init_bulk_queue(VCHIQ_BULK_QUEUE_T *queue)
7955 + queue->local_insert = 0;
7956 + queue->remote_insert = 0;
7957 + queue->process = 0;
7958 + queue->remote_notify = 0;
7959 + queue->remove = 0;
7963 +inline const char *
7964 +get_conn_state_name(VCHIQ_CONNSTATE_T conn_state)
7966 + return conn_state_names[conn_state];
7970 +VCHIQ_SLOT_ZERO_T *
7971 +vchiq_init_slots(void *mem_base, int mem_size)
7973 + int mem_align = (VCHIQ_SLOT_SIZE - (int)mem_base) & VCHIQ_SLOT_MASK;
7974 + VCHIQ_SLOT_ZERO_T *slot_zero =
7975 + (VCHIQ_SLOT_ZERO_T *)((char *)mem_base + mem_align);
7976 + int num_slots = (mem_size - mem_align)/VCHIQ_SLOT_SIZE;
7977 + int first_data_slot = VCHIQ_SLOT_ZERO_SLOTS;
7979 + /* Ensure there is enough memory to run an absolutely minimum system */
7980 + num_slots -= first_data_slot;
7982 + if (num_slots < 4) {
7983 + vchiq_log_error(vchiq_core_log_level,
7984 + "vchiq_init_slots - insufficient memory %x bytes",
7989 + memset(slot_zero, 0, sizeof(VCHIQ_SLOT_ZERO_T));
7991 + slot_zero->magic = VCHIQ_MAGIC;
7992 + slot_zero->version = VCHIQ_VERSION;
7993 + slot_zero->version_min = VCHIQ_VERSION_MIN;
7994 + slot_zero->slot_zero_size = sizeof(VCHIQ_SLOT_ZERO_T);
7995 + slot_zero->slot_size = VCHIQ_SLOT_SIZE;
7996 + slot_zero->max_slots = VCHIQ_MAX_SLOTS;
7997 + slot_zero->max_slots_per_side = VCHIQ_MAX_SLOTS_PER_SIDE;
7999 + slot_zero->master.slot_sync = first_data_slot;
8000 + slot_zero->master.slot_first = first_data_slot + 1;
8001 + slot_zero->master.slot_last = first_data_slot + (num_slots/2) - 1;
8002 + slot_zero->slave.slot_sync = first_data_slot + (num_slots/2);
8003 + slot_zero->slave.slot_first = first_data_slot + (num_slots/2) + 1;
8004 + slot_zero->slave.slot_last = first_data_slot + num_slots - 1;
8010 +vchiq_init_state(VCHIQ_STATE_T *state, VCHIQ_SLOT_ZERO_T *slot_zero,
8013 + VCHIQ_SHARED_STATE_T *local;
8014 + VCHIQ_SHARED_STATE_T *remote;
8015 + VCHIQ_STATUS_T status;
8016 + char threadname[10];
8020 + vchiq_log_warning(vchiq_core_log_level,
8021 + "%s: slot_zero = 0x%08lx, is_master = %d",
8022 + __func__, (unsigned long)slot_zero, is_master);
8024 + /* Check the input configuration */
8026 + if (slot_zero->magic != VCHIQ_MAGIC) {
8027 + vchiq_loud_error_header();
8028 + vchiq_loud_error("Invalid VCHIQ magic value found.");
8029 + vchiq_loud_error("slot_zero=%x: magic=%x (expected %x)",
8030 + (unsigned int)slot_zero, slot_zero->magic, VCHIQ_MAGIC);
8031 + vchiq_loud_error_footer();
8032 + return VCHIQ_ERROR;
8035 + if (slot_zero->version < VCHIQ_VERSION_MIN) {
8036 + vchiq_loud_error_header();
8037 + vchiq_loud_error("Incompatible VCHIQ versions found.");
8038 + vchiq_loud_error("slot_zero=%x: VideoCore version=%d "
8040 + (unsigned int)slot_zero, slot_zero->version,
8041 + VCHIQ_VERSION_MIN);
8042 + vchiq_loud_error("Restart with a newer VideoCore image.");
8043 + vchiq_loud_error_footer();
8044 + return VCHIQ_ERROR;
8047 + if (VCHIQ_VERSION < slot_zero->version_min) {
8048 + vchiq_loud_error_header();
8049 + vchiq_loud_error("Incompatible VCHIQ versions found.");
8050 + vchiq_loud_error("slot_zero=%x: version=%d (VideoCore "
8052 + (unsigned int)slot_zero, VCHIQ_VERSION,
8053 + slot_zero->version_min);
8054 + vchiq_loud_error("Restart with a newer kernel.");
8055 + vchiq_loud_error_footer();
8056 + return VCHIQ_ERROR;
8059 + if ((slot_zero->slot_zero_size != sizeof(VCHIQ_SLOT_ZERO_T)) ||
8060 + (slot_zero->slot_size != VCHIQ_SLOT_SIZE) ||
8061 + (slot_zero->max_slots != VCHIQ_MAX_SLOTS) ||
8062 + (slot_zero->max_slots_per_side != VCHIQ_MAX_SLOTS_PER_SIDE)) {
8063 + vchiq_loud_error_header();
8064 + if (slot_zero->slot_zero_size != sizeof(VCHIQ_SLOT_ZERO_T))
8065 + vchiq_loud_error("slot_zero=%x: slot_zero_size=%x "
8067 + (unsigned int)slot_zero,
8068 + slot_zero->slot_zero_size,
8069 + sizeof(VCHIQ_SLOT_ZERO_T));
8070 + if (slot_zero->slot_size != VCHIQ_SLOT_SIZE)
8071 + vchiq_loud_error("slot_zero=%x: slot_size=%d "
8073 + (unsigned int)slot_zero, slot_zero->slot_size,
8075 + if (slot_zero->max_slots != VCHIQ_MAX_SLOTS)
8076 + vchiq_loud_error("slot_zero=%x: max_slots=%d "
8078 + (unsigned int)slot_zero, slot_zero->max_slots,
8080 + if (slot_zero->max_slots_per_side != VCHIQ_MAX_SLOTS_PER_SIDE)
8081 + vchiq_loud_error("slot_zero=%x: max_slots_per_side=%d "
8083 + (unsigned int)slot_zero,
8084 + slot_zero->max_slots_per_side,
8085 + VCHIQ_MAX_SLOTS_PER_SIDE);
8086 + vchiq_loud_error_footer();
8087 + return VCHIQ_ERROR;
8091 + local = &slot_zero->master;
8092 + remote = &slot_zero->slave;
8094 + local = &slot_zero->slave;
8095 + remote = &slot_zero->master;
8098 + if (local->initialised) {
8099 + vchiq_loud_error_header();
8100 + if (remote->initialised)
8101 + vchiq_loud_error("local state has already been "
8104 + vchiq_loud_error("master/slave mismatch - two %ss",
8105 + is_master ? "master" : "slave");
8106 + vchiq_loud_error_footer();
8107 + return VCHIQ_ERROR;
8110 + memset(state, 0, sizeof(VCHIQ_STATE_T));
8113 + state->is_master = is_master;
8116 + initialize shared state pointers
8119 + state->local = local;
8120 + state->remote = remote;
8121 + state->slot_data = (VCHIQ_SLOT_T *)slot_zero;
8124 + initialize events and mutexes
8127 + sema_init(&state->connect, 0);
8128 + mutex_init(&state->mutex);
8129 + sema_init(&state->trigger_event, 0);
8130 + sema_init(&state->recycle_event, 0);
8131 + sema_init(&state->sync_trigger_event, 0);
8132 + sema_init(&state->sync_release_event, 0);
8134 + mutex_init(&state->slot_mutex);
8135 + mutex_init(&state->recycle_mutex);
8136 + mutex_init(&state->sync_mutex);
8137 + mutex_init(&state->bulk_transfer_mutex);
8139 + sema_init(&state->slot_available_event, 0);
8140 + sema_init(&state->slot_remove_event, 0);
8141 + sema_init(&state->data_quota_event, 0);
8143 + state->slot_queue_available = 0;
8145 + for (i = 0; i < VCHIQ_MAX_SERVICES; i++) {
8146 + VCHIQ_SERVICE_QUOTA_T *service_quota =
8147 + &state->service_quotas[i];
8148 + sema_init(&service_quota->quota_event, 0);
8151 + for (i = local->slot_first; i <= local->slot_last; i++) {
8152 + local->slot_queue[state->slot_queue_available++] = i;
8153 + up(&state->slot_available_event);
8156 + state->default_slot_quota = state->slot_queue_available/2;
8157 + state->default_message_quota =
8158 + min((unsigned short)(state->default_slot_quota * 256),
8159 + (unsigned short)~0);
8161 + state->previous_data_index = -1;
8162 + state->data_use_count = 0;
8163 + state->data_quota = state->slot_queue_available - 1;
8165 + local->trigger.event = &state->trigger_event;
8166 + remote_event_create(&local->trigger);
8167 + local->tx_pos = 0;
8169 + local->recycle.event = &state->recycle_event;
8170 + remote_event_create(&local->recycle);
8171 + local->slot_queue_recycle = state->slot_queue_available;
8173 + local->sync_trigger.event = &state->sync_trigger_event;
8174 + remote_event_create(&local->sync_trigger);
8176 + local->sync_release.event = &state->sync_release_event;
8177 + remote_event_create(&local->sync_release);
8179 + /* At start-of-day, the slot is empty and available */
8180 + ((VCHIQ_HEADER_T *)SLOT_DATA_FROM_INDEX(state, local->slot_sync))->msgid
8181 + = VCHIQ_MSGID_PADDING;
8182 + remote_event_signal_local(&local->sync_release);
8184 + local->debug[DEBUG_ENTRIES] = DEBUG_MAX;
8186 + status = vchiq_platform_init_state(state);
8189 + bring up slot handler thread
8191 + snprintf(threadname, sizeof(threadname), "VCHIQ-%d", state->id);
8192 + state->slot_handler_thread = kthread_create(&slot_handler_func,
8196 + if (state->slot_handler_thread == NULL) {
8197 + vchiq_loud_error_header();
8198 + vchiq_loud_error("couldn't create thread %s", threadname);
8199 + vchiq_loud_error_footer();
8200 + return VCHIQ_ERROR;
8202 + set_user_nice(state->slot_handler_thread, -19);
8203 + wake_up_process(state->slot_handler_thread);
8205 + snprintf(threadname, sizeof(threadname), "VCHIQr-%d", state->id);
8206 + state->recycle_thread = kthread_create(&recycle_func,
8209 + if (state->recycle_thread == NULL) {
8210 + vchiq_loud_error_header();
8211 + vchiq_loud_error("couldn't create thread %s", threadname);
8212 + vchiq_loud_error_footer();
8213 + return VCHIQ_ERROR;
8215 + set_user_nice(state->recycle_thread, -19);
8216 + wake_up_process(state->recycle_thread);
8218 + snprintf(threadname, sizeof(threadname), "VCHIQs-%d", state->id);
8219 + state->sync_thread = kthread_create(&sync_func,
8222 + if (state->sync_thread == NULL) {
8223 + vchiq_loud_error_header();
8224 + vchiq_loud_error("couldn't create thread %s", threadname);
8225 + vchiq_loud_error_footer();
8226 + return VCHIQ_ERROR;
8228 + set_user_nice(state->sync_thread, -20);
8229 + wake_up_process(state->sync_thread);
8231 + BUG_ON(state->id >= VCHIQ_MAX_STATES);
8232 + vchiq_states[state->id] = state;
8234 + /* Indicate readiness to the other side */
8235 + local->initialised = 1;
8240 +/* Called from application thread when a client or server service is created. */
8242 +vchiq_add_service_internal(VCHIQ_STATE_T *state,
8243 + const VCHIQ_SERVICE_PARAMS_T *params, int srvstate,
8244 + VCHIQ_INSTANCE_T instance, VCHIQ_USERDATA_TERM_T userdata_term)
8246 + VCHIQ_SERVICE_T *service;
8248 + service = kmalloc(sizeof(VCHIQ_SERVICE_T), GFP_KERNEL);
8250 + service->base.fourcc = params->fourcc;
8251 + service->base.callback = params->callback;
8252 + service->base.userdata = params->userdata;
8253 + service->handle = VCHIQ_SERVICE_HANDLE_INVALID;
8254 + service->ref_count = 1;
8255 + service->srvstate = VCHIQ_SRVSTATE_FREE;
8256 + service->userdata_term = userdata_term;
8257 + service->localport = VCHIQ_PORT_FREE;
8258 + service->remoteport = VCHIQ_PORT_FREE;
8260 + service->public_fourcc = (srvstate == VCHIQ_SRVSTATE_OPENING) ?
8261 + VCHIQ_FOURCC_INVALID : params->fourcc;
8262 + service->client_id = 0;
8263 + service->auto_close = 1;
8264 + service->sync = 0;
8265 + service->closing = 0;
8266 + service->trace = 0;
8267 + atomic_set(&service->poll_flags, 0);
8268 + service->version = params->version;
8269 + service->version_min = params->version_min;
8270 + service->state = state;
8271 + service->instance = instance;
8272 + service->service_use_count = 0;
8273 + init_bulk_queue(&service->bulk_tx);
8274 + init_bulk_queue(&service->bulk_rx);
8275 + sema_init(&service->remove_event, 0);
8276 + sema_init(&service->bulk_remove_event, 0);
8277 + mutex_init(&service->bulk_mutex);
8278 + memset(&service->stats, 0, sizeof(service->stats));
8280 + vchiq_log_error(vchiq_core_log_level,
8285 + VCHIQ_SERVICE_T **pservice = NULL;
8288 + /* Although it is perfectly possible to use service_spinlock
8289 + ** to protect the creation of services, it is overkill as it
8290 + ** disables interrupts while the array is searched.
8291 + ** The only danger is of another thread trying to create a
8292 + ** service - service deletion is safe.
8293 + ** Therefore it is preferable to use state->mutex which,
8294 + ** although slower to claim, doesn't block interrupts while
8298 + mutex_lock(&state->mutex);
8300 + /* Prepare to use a previously unused service */
8301 + if (state->unused_service < VCHIQ_MAX_SERVICES)
8302 + pservice = &state->services[state->unused_service];
8304 + if (srvstate == VCHIQ_SRVSTATE_OPENING) {
8305 + for (i = 0; i < state->unused_service; i++) {
8306 + VCHIQ_SERVICE_T *srv = state->services[i];
8308 + pservice = &state->services[i];
8313 + for (i = (state->unused_service - 1); i >= 0; i--) {
8314 + VCHIQ_SERVICE_T *srv = state->services[i];
8316 + pservice = &state->services[i];
8317 + else if ((srv->public_fourcc == params->fourcc)
8318 + && ((srv->instance != instance) ||
8319 + (srv->base.callback !=
8320 + params->callback))) {
8321 + /* There is another server using this
8322 + ** fourcc which doesn't match. */
8330 + service->localport = (pservice - state->services);
8332 + handle_seq = VCHIQ_MAX_STATES *
8333 + VCHIQ_MAX_SERVICES;
8334 + service->handle = handle_seq |
8335 + (state->id * VCHIQ_MAX_SERVICES) |
8336 + service->localport;
8337 + handle_seq += VCHIQ_MAX_STATES * VCHIQ_MAX_SERVICES;
8338 + *pservice = service;
8339 + if (pservice == &state->services[state->unused_service])
8340 + state->unused_service++;
8343 + mutex_unlock(&state->mutex);
8352 + VCHIQ_SERVICE_QUOTA_T *service_quota =
8353 + &state->service_quotas[service->localport];
8354 + service_quota->slot_quota = state->default_slot_quota;
8355 + service_quota->message_quota = state->default_message_quota;
8356 + if (service_quota->slot_use_count == 0)
8357 + service_quota->previous_tx_index =
8358 + SLOT_QUEUE_INDEX_FROM_POS(state->local_tx_pos)
8361 + /* Bring this service online */
8362 + vchiq_set_service_state(service, srvstate);
8364 + vchiq_log_info(vchiq_core_msg_log_level,
8365 + "%s Service %c%c%c%c SrcPort:%d",
8366 + (srvstate == VCHIQ_SRVSTATE_OPENING)
8368 + VCHIQ_FOURCC_AS_4CHARS(params->fourcc),
8369 + service->localport);
8372 + /* Don't unlock the service - leave it with a ref_count of 1. */
8378 +vchiq_open_service_internal(VCHIQ_SERVICE_T *service, int client_id)
8380 + struct vchiq_open_payload payload = {
8381 + service->base.fourcc,
8384 + service->version_min
8386 + VCHIQ_ELEMENT_T body = { &payload, sizeof(payload) };
8387 + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
8389 + service->client_id = client_id;
8390 + vchiq_use_service_internal(service);
8391 + status = queue_message(service->state, NULL,
8392 + VCHIQ_MAKE_MSG(VCHIQ_MSG_OPEN, service->localport, 0),
8393 + &body, 1, sizeof(payload), 1);
8394 + if (status == VCHIQ_SUCCESS) {
8395 + /* Wait for the ACK/NAK */
8396 + if (down_interruptible(&service->remove_event) != 0) {
8397 + status = VCHIQ_RETRY;
8398 + vchiq_release_service_internal(service);
8399 + } else if ((service->srvstate != VCHIQ_SRVSTATE_OPEN) &&
8400 + (service->srvstate != VCHIQ_SRVSTATE_OPENSYNC)) {
8401 + if (service->srvstate != VCHIQ_SRVSTATE_CLOSEWAIT)
8402 + vchiq_log_error(vchiq_core_log_level,
8403 + "%d: osi - srvstate = %s (ref %d)",
8404 + service->state->id,
8405 + srvstate_names[service->srvstate],
8406 + service->ref_count);
8407 + status = VCHIQ_ERROR;
8408 + VCHIQ_SERVICE_STATS_INC(service, error_count);
8409 + vchiq_release_service_internal(service);
8416 +release_service_messages(VCHIQ_SERVICE_T *service)
8418 + VCHIQ_STATE_T *state = service->state;
8419 + int slot_last = state->remote->slot_last;
8422 + /* Release any claimed messages */
8423 + for (i = state->remote->slot_first; i <= slot_last; i++) {
8424 + VCHIQ_SLOT_INFO_T *slot_info =
8425 + SLOT_INFO_FROM_INDEX(state, i);
8426 + if (slot_info->release_count != slot_info->use_count) {
8428 + (char *)SLOT_DATA_FROM_INDEX(state, i);
8429 + unsigned int pos, end;
8431 + end = VCHIQ_SLOT_SIZE;
8432 + if (data == state->rx_data)
8433 + /* This buffer is still being read from - stop
8434 + ** at the current read position */
8435 + end = state->rx_pos & VCHIQ_SLOT_MASK;
8439 + while (pos < end) {
8440 + VCHIQ_HEADER_T *header =
8441 + (VCHIQ_HEADER_T *)(data + pos);
8442 + int msgid = header->msgid;
8443 + int port = VCHIQ_MSG_DSTPORT(msgid);
8444 + if ((port == service->localport) &&
8445 + (msgid & VCHIQ_MSGID_CLAIMED)) {
8446 + vchiq_log_info(vchiq_core_log_level,
8448 + (unsigned int)header);
8449 + release_slot(state, slot_info, header,
8452 + pos += calc_stride(header->size);
8453 + if (pos > VCHIQ_SLOT_SIZE) {
8454 + vchiq_log_error(vchiq_core_log_level,
8455 + "fsi - pos %x: header %x, "
8456 + "msgid %x, header->msgid %x, "
8457 + "header->size %x",
8458 + pos, (unsigned int)header,
8459 + msgid, header->msgid,
8461 + WARN(1, "invalid slot position\n");
8469 +do_abort_bulks(VCHIQ_SERVICE_T *service)
8471 + VCHIQ_STATUS_T status;
8473 + /* Abort any outstanding bulk transfers */
8474 + if (mutex_lock_interruptible(&service->bulk_mutex) != 0)
8476 + abort_outstanding_bulks(service, &service->bulk_tx);
8477 + abort_outstanding_bulks(service, &service->bulk_rx);
8478 + mutex_unlock(&service->bulk_mutex);
8480 + status = notify_bulks(service, &service->bulk_tx, 0/*!retry_poll*/);
8481 + if (status == VCHIQ_SUCCESS)
8482 + status = notify_bulks(service, &service->bulk_rx,
8483 + 0/*!retry_poll*/);
8484 + return (status == VCHIQ_SUCCESS);
8487 +static VCHIQ_STATUS_T
8488 +close_service_complete(VCHIQ_SERVICE_T *service, int failstate)
8490 + VCHIQ_STATUS_T status;
8491 + int is_server = (service->public_fourcc != VCHIQ_FOURCC_INVALID);
8494 + switch (service->srvstate) {
8495 + case VCHIQ_SRVSTATE_OPEN:
8496 + case VCHIQ_SRVSTATE_CLOSESENT:
8497 + case VCHIQ_SRVSTATE_CLOSERECVD:
8499 + if (service->auto_close) {
8500 + service->client_id = 0;
8501 + service->remoteport = VCHIQ_PORT_FREE;
8502 + newstate = VCHIQ_SRVSTATE_LISTENING;
8504 + newstate = VCHIQ_SRVSTATE_CLOSEWAIT;
8506 + newstate = VCHIQ_SRVSTATE_CLOSED;
8507 + vchiq_set_service_state(service, newstate);
8509 + case VCHIQ_SRVSTATE_LISTENING:
8512 + vchiq_log_error(vchiq_core_log_level,
8513 + "close_service_complete(%x) called in state %s",
8514 + service->handle, srvstate_names[service->srvstate]);
8515 + WARN(1, "close_service_complete in unexpected state\n");
8516 + return VCHIQ_ERROR;
8519 + status = make_service_callback(service,
8520 + VCHIQ_SERVICE_CLOSED, NULL, NULL);
8522 + if (status != VCHIQ_RETRY) {
8523 + int uc = service->service_use_count;
8525 + /* Complete the close process */
8526 + for (i = 0; i < uc; i++)
8527 + /* cater for cases where close is forced and the
8528 + ** client may not close all it's handles */
8529 + vchiq_release_service_internal(service);
8531 + service->client_id = 0;
8532 + service->remoteport = VCHIQ_PORT_FREE;
8534 + if (service->srvstate == VCHIQ_SRVSTATE_CLOSED)
8535 + vchiq_free_service_internal(service);
8536 + else if (service->srvstate != VCHIQ_SRVSTATE_CLOSEWAIT) {
8538 + service->closing = 0;
8540 + up(&service->remove_event);
8543 + vchiq_set_service_state(service, failstate);
8548 +/* Called by the slot handler */
8550 +vchiq_close_service_internal(VCHIQ_SERVICE_T *service, int close_recvd)
8552 + VCHIQ_STATE_T *state = service->state;
8553 + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
8554 + int is_server = (service->public_fourcc != VCHIQ_FOURCC_INVALID);
8556 + vchiq_log_info(vchiq_core_log_level, "%d: csi:%d,%d (%s)",
8557 + service->state->id, service->localport, close_recvd,
8558 + srvstate_names[service->srvstate]);
8560 + switch (service->srvstate) {
8561 + case VCHIQ_SRVSTATE_CLOSED:
8562 + case VCHIQ_SRVSTATE_HIDDEN:
8563 + case VCHIQ_SRVSTATE_LISTENING:
8564 + case VCHIQ_SRVSTATE_CLOSEWAIT:
8566 + vchiq_log_error(vchiq_core_log_level,
8567 + "vchiq_close_service_internal(1) called "
8569 + srvstate_names[service->srvstate]);
8570 + else if (is_server) {
8571 + if (service->srvstate == VCHIQ_SRVSTATE_LISTENING) {
8572 + status = VCHIQ_ERROR;
8574 + service->client_id = 0;
8575 + service->remoteport = VCHIQ_PORT_FREE;
8576 + if (service->srvstate ==
8577 + VCHIQ_SRVSTATE_CLOSEWAIT)
8578 + vchiq_set_service_state(service,
8579 + VCHIQ_SRVSTATE_LISTENING);
8581 + up(&service->remove_event);
8583 + vchiq_free_service_internal(service);
8585 + case VCHIQ_SRVSTATE_OPENING:
8586 + if (close_recvd) {
8587 + /* The open was rejected - tell the user */
8588 + vchiq_set_service_state(service,
8589 + VCHIQ_SRVSTATE_CLOSEWAIT);
8590 + up(&service->remove_event);
8592 + /* Shutdown mid-open - let the other side know */
8593 + status = queue_message(state, service,
8596 + service->localport,
8597 + VCHIQ_MSG_DSTPORT(service->remoteport)),
8602 + case VCHIQ_SRVSTATE_OPENSYNC:
8603 + mutex_lock(&state->sync_mutex);
8604 + /* Drop through */
8606 + case VCHIQ_SRVSTATE_OPEN:
8607 + if (state->is_master || close_recvd) {
8608 + if (!do_abort_bulks(service))
8609 + status = VCHIQ_RETRY;
8612 + release_service_messages(service);
8614 + if (status == VCHIQ_SUCCESS)
8615 + status = queue_message(state, service,
8618 + service->localport,
8619 + VCHIQ_MSG_DSTPORT(service->remoteport)),
8622 + if (status == VCHIQ_SUCCESS) {
8625 + } else if (service->srvstate == VCHIQ_SRVSTATE_OPENSYNC) {
8626 + mutex_unlock(&state->sync_mutex);
8631 + status = close_service_complete(service,
8632 + VCHIQ_SRVSTATE_CLOSERECVD);
8635 + case VCHIQ_SRVSTATE_CLOSESENT:
8637 + /* This happens when a process is killed mid-close */
8640 + if (!state->is_master) {
8641 + if (!do_abort_bulks(service)) {
8642 + status = VCHIQ_RETRY;
8647 + if (status == VCHIQ_SUCCESS)
8648 + status = close_service_complete(service,
8649 + VCHIQ_SRVSTATE_CLOSERECVD);
8652 + case VCHIQ_SRVSTATE_CLOSERECVD:
8653 + if (!close_recvd && is_server)
8654 + /* Force into LISTENING mode */
8655 + vchiq_set_service_state(service,
8656 + VCHIQ_SRVSTATE_LISTENING);
8657 + status = close_service_complete(service,
8658 + VCHIQ_SRVSTATE_CLOSERECVD);
8662 + vchiq_log_error(vchiq_core_log_level,
8663 + "vchiq_close_service_internal(%d) called in state %s",
8664 + close_recvd, srvstate_names[service->srvstate]);
8671 +/* Called from the application process upon process death */
8673 +vchiq_terminate_service_internal(VCHIQ_SERVICE_T *service)
8675 + VCHIQ_STATE_T *state = service->state;
8677 + vchiq_log_info(vchiq_core_log_level, "%d: tsi - (%d<->%d)",
8678 + state->id, service->localport, service->remoteport);
8680 + mark_service_closing(service);
8682 + /* Mark the service for removal by the slot handler */
8683 + request_poll(state, service, VCHIQ_POLL_REMOVE);
8686 +/* Called from the slot handler */
8688 +vchiq_free_service_internal(VCHIQ_SERVICE_T *service)
8690 + VCHIQ_STATE_T *state = service->state;
8692 + vchiq_log_info(vchiq_core_log_level, "%d: fsi - (%d)",
8693 + state->id, service->localport);
8695 + switch (service->srvstate) {
8696 + case VCHIQ_SRVSTATE_OPENING:
8697 + case VCHIQ_SRVSTATE_CLOSED:
8698 + case VCHIQ_SRVSTATE_HIDDEN:
8699 + case VCHIQ_SRVSTATE_LISTENING:
8700 + case VCHIQ_SRVSTATE_CLOSEWAIT:
8703 + vchiq_log_error(vchiq_core_log_level,
8704 + "%d: fsi - (%d) in state %s",
8705 + state->id, service->localport,
8706 + srvstate_names[service->srvstate]);
8710 + vchiq_set_service_state(service, VCHIQ_SRVSTATE_FREE);
8712 + up(&service->remove_event);
8714 + /* Release the initial lock */
8715 + unlock_service(service);
8719 +vchiq_connect_internal(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance)
8721 + VCHIQ_SERVICE_T *service;
8724 + /* Find all services registered to this client and enable them. */
8726 + while ((service = next_service_by_instance(state, instance,
8728 + if (service->srvstate == VCHIQ_SRVSTATE_HIDDEN)
8729 + vchiq_set_service_state(service,
8730 + VCHIQ_SRVSTATE_LISTENING);
8731 + unlock_service(service);
8734 + if (state->conn_state == VCHIQ_CONNSTATE_DISCONNECTED) {
8735 + if (queue_message(state, NULL,
8736 + VCHIQ_MAKE_MSG(VCHIQ_MSG_CONNECT, 0, 0), NULL, 0,
8737 + 0, 1) == VCHIQ_RETRY)
8738 + return VCHIQ_RETRY;
8740 + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_CONNECTING);
8743 + if (state->conn_state == VCHIQ_CONNSTATE_CONNECTING) {
8744 + if (down_interruptible(&state->connect) != 0)
8745 + return VCHIQ_RETRY;
8747 + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_CONNECTED);
8748 + up(&state->connect);
8751 + return VCHIQ_SUCCESS;
8755 +vchiq_shutdown_internal(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance)
8757 + VCHIQ_SERVICE_T *service;
8760 + /* Find all services registered to this client and enable them. */
8762 + while ((service = next_service_by_instance(state, instance,
8764 + (void)vchiq_remove_service(service->handle);
8765 + unlock_service(service);
8768 + return VCHIQ_SUCCESS;
8772 +vchiq_pause_internal(VCHIQ_STATE_T *state)
8774 + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
8776 + switch (state->conn_state) {
8777 + case VCHIQ_CONNSTATE_CONNECTED:
8778 + /* Request a pause */
8779 + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_PAUSING);
8780 + request_poll(state, NULL, 0);
8783 + vchiq_log_error(vchiq_core_log_level,
8784 + "vchiq_pause_internal in state %s\n",
8785 + conn_state_names[state->conn_state]);
8786 + status = VCHIQ_ERROR;
8787 + VCHIQ_STATS_INC(state, error_count);
8795 +vchiq_resume_internal(VCHIQ_STATE_T *state)
8797 + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
8799 + if (state->conn_state == VCHIQ_CONNSTATE_PAUSED) {
8800 + vchiq_set_conn_state(state, VCHIQ_CONNSTATE_RESUMING);
8801 + request_poll(state, NULL, 0);
8803 + status = VCHIQ_ERROR;
8804 + VCHIQ_STATS_INC(state, error_count);
8811 +vchiq_close_service(VCHIQ_SERVICE_HANDLE_T handle)
8813 + /* Unregister the service */
8814 + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
8815 + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
8818 + return VCHIQ_ERROR;
8820 + vchiq_log_info(vchiq_core_log_level,
8821 + "%d: close_service:%d",
8822 + service->state->id, service->localport);
8824 + if ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
8825 + (service->srvstate == VCHIQ_SRVSTATE_LISTENING) ||
8826 + (service->srvstate == VCHIQ_SRVSTATE_HIDDEN)) {
8827 + unlock_service(service);
8828 + return VCHIQ_ERROR;
8831 + mark_service_closing(service);
8833 + if (current == service->state->slot_handler_thread) {
8834 + status = vchiq_close_service_internal(service,
8835 + 0/*!close_recvd*/);
8836 + BUG_ON(status == VCHIQ_RETRY);
8838 + /* Mark the service for termination by the slot handler */
8839 + request_poll(service->state, service, VCHIQ_POLL_TERMINATE);
8843 + if (down_interruptible(&service->remove_event) != 0) {
8844 + status = VCHIQ_RETRY;
8848 + if ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
8849 + (service->srvstate == VCHIQ_SRVSTATE_LISTENING) ||
8850 + (service->srvstate == VCHIQ_SRVSTATE_OPEN))
8853 + vchiq_log_warning(vchiq_core_log_level,
8854 + "%d: close_service:%d - waiting in state %s",
8855 + service->state->id, service->localport,
8856 + srvstate_names[service->srvstate]);
8859 + if ((status == VCHIQ_SUCCESS) &&
8860 + (service->srvstate != VCHIQ_SRVSTATE_FREE) &&
8861 + (service->srvstate != VCHIQ_SRVSTATE_LISTENING))
8862 + status = VCHIQ_ERROR;
8864 + unlock_service(service);
8870 +vchiq_remove_service(VCHIQ_SERVICE_HANDLE_T handle)
8872 + /* Unregister the service */
8873 + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
8874 + VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
8877 + return VCHIQ_ERROR;
8879 + vchiq_log_info(vchiq_core_log_level,
8880 + "%d: remove_service:%d",
8881 + service->state->id, service->localport);
8883 + if (service->srvstate == VCHIQ_SRVSTATE_FREE) {
8884 + unlock_service(service);
8885 + return VCHIQ_ERROR;
8888 + mark_service_closing(service);
8890 + if ((service->srvstate == VCHIQ_SRVSTATE_HIDDEN) ||
8891 + (current == service->state->slot_handler_thread)) {
8892 + /* Make it look like a client, because it must be removed and
8893 + not left in the LISTENING state. */
8894 + service->public_fourcc = VCHIQ_FOURCC_INVALID;
8896 + status = vchiq_close_service_internal(service,
8897 + 0/*!close_recvd*/);
8898 + BUG_ON(status == VCHIQ_RETRY);
8900 + /* Mark the service for removal by the slot handler */
8901 + request_poll(service->state, service, VCHIQ_POLL_REMOVE);
8904 + if (down_interruptible(&service->remove_event) != 0) {
8905 + status = VCHIQ_RETRY;
8909 + if ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
8910 + (service->srvstate == VCHIQ_SRVSTATE_OPEN))
8913 + vchiq_log_warning(vchiq_core_log_level,
8914 + "%d: remove_service:%d - waiting in state %s",
8915 + service->state->id, service->localport,
8916 + srvstate_names[service->srvstate]);
8919 + if ((status == VCHIQ_SUCCESS) &&
8920 + (service->srvstate != VCHIQ_SRVSTATE_FREE))
8921 + status = VCHIQ_ERROR;
8923 + unlock_service(service);
8929 +/* This function may be called by kernel threads or user threads.
8930 + * User threads may receive VCHIQ_RETRY to indicate that a signal has been
8931 + * received and the call should be retried after being returned to user
8933 + * When called in blocking mode, the userdata field points to a bulk_waiter
8937 +vchiq_bulk_transfer(VCHIQ_SERVICE_HANDLE_T handle,
8938 + VCHI_MEM_HANDLE_T memhandle, void *offset, int size, void *userdata,
8939 + VCHIQ_BULK_MODE_T mode, VCHIQ_BULK_DIR_T dir)
8941 + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
8942 + VCHIQ_BULK_QUEUE_T *queue;
8943 + VCHIQ_BULK_T *bulk;
8944 + VCHIQ_STATE_T *state;
8945 + struct bulk_waiter *bulk_waiter = NULL;
8946 + const char dir_char = (dir == VCHIQ_BULK_TRANSMIT) ? 't' : 'r';
8947 + const int dir_msgtype = (dir == VCHIQ_BULK_TRANSMIT) ?
8948 + VCHIQ_MSG_BULK_TX : VCHIQ_MSG_BULK_RX;
8949 + VCHIQ_STATUS_T status = VCHIQ_ERROR;
8952 + (service->srvstate != VCHIQ_SRVSTATE_OPEN) ||
8953 + ((memhandle == VCHI_MEM_HANDLE_INVALID) && (offset == NULL)) ||
8954 + (vchiq_check_service(service) != VCHIQ_SUCCESS))
8958 + case VCHIQ_BULK_MODE_NOCALLBACK:
8959 + case VCHIQ_BULK_MODE_CALLBACK:
8961 + case VCHIQ_BULK_MODE_BLOCKING:
8962 + bulk_waiter = (struct bulk_waiter *)userdata;
8963 + sema_init(&bulk_waiter->event, 0);
8964 + bulk_waiter->actual = 0;
8965 + bulk_waiter->bulk = NULL;
8967 + case VCHIQ_BULK_MODE_WAITING:
8968 + bulk_waiter = (struct bulk_waiter *)userdata;
8969 + bulk = bulk_waiter->bulk;
8975 + state = service->state;
8977 + queue = (dir == VCHIQ_BULK_TRANSMIT) ?
8978 + &service->bulk_tx : &service->bulk_rx;
8980 + if (mutex_lock_interruptible(&service->bulk_mutex) != 0) {
8981 + status = VCHIQ_RETRY;
8985 + if (queue->local_insert == queue->remove + VCHIQ_NUM_SERVICE_BULKS) {
8986 + VCHIQ_SERVICE_STATS_INC(service, bulk_stalls);
8988 + mutex_unlock(&service->bulk_mutex);
8989 + if (down_interruptible(&service->bulk_remove_event)
8991 + status = VCHIQ_RETRY;
8994 + if (mutex_lock_interruptible(&service->bulk_mutex)
8996 + status = VCHIQ_RETRY;
8999 + } while (queue->local_insert == queue->remove +
9000 + VCHIQ_NUM_SERVICE_BULKS);
9003 + bulk = &queue->bulks[BULK_INDEX(queue->local_insert)];
9005 + bulk->mode = mode;
9007 + bulk->userdata = userdata;
9008 + bulk->size = size;
9009 + bulk->actual = VCHIQ_BULK_ACTUAL_ABORTED;
9011 + if (vchiq_prepare_bulk_data(bulk, memhandle, offset, size, dir) !=
9013 + goto unlock_error_exit;
9017 + vchiq_log_info(vchiq_core_log_level,
9018 + "%d: bt (%d->%d) %cx %x@%x %x",
9020 + service->localport, service->remoteport, dir_char,
9021 + size, (unsigned int)bulk->data, (unsigned int)userdata);
9023 + if (state->is_master) {
9024 + queue->local_insert++;
9025 + if (resolve_bulks(service, queue))
9026 + request_poll(state, service,
9027 + (dir == VCHIQ_BULK_TRANSMIT) ?
9028 + VCHIQ_POLL_TXNOTIFY : VCHIQ_POLL_RXNOTIFY);
9030 + int payload[2] = { (int)bulk->data, bulk->size };
9031 + VCHIQ_ELEMENT_T element = { payload, sizeof(payload) };
9033 + status = queue_message(state, NULL,
9034 + VCHIQ_MAKE_MSG(dir_msgtype,
9035 + service->localport, service->remoteport),
9036 + &element, 1, sizeof(payload), 1);
9037 + if (status != VCHIQ_SUCCESS) {
9038 + vchiq_complete_bulk(bulk);
9039 + goto unlock_error_exit;
9041 + queue->local_insert++;
9044 + mutex_unlock(&service->bulk_mutex);
9046 + vchiq_log_trace(vchiq_core_log_level,
9047 + "%d: bt:%d %cx li=%x ri=%x p=%x",
9049 + service->localport, dir_char,
9050 + queue->local_insert, queue->remote_insert, queue->process);
9053 + unlock_service(service);
9055 + status = VCHIQ_SUCCESS;
9057 + if (bulk_waiter) {
9058 + bulk_waiter->bulk = bulk;
9059 + if (down_interruptible(&bulk_waiter->event) != 0)
9060 + status = VCHIQ_RETRY;
9061 + else if (bulk_waiter->actual == VCHIQ_BULK_ACTUAL_ABORTED)
9062 + status = VCHIQ_ERROR;
9068 + mutex_unlock(&service->bulk_mutex);
9072 + unlock_service(service);
9077 +vchiq_queue_message(VCHIQ_SERVICE_HANDLE_T handle,
9078 + const VCHIQ_ELEMENT_T *elements, unsigned int count)
9080 + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
9081 + VCHIQ_STATUS_T status = VCHIQ_ERROR;
9083 + unsigned int size = 0;
9087 + (vchiq_check_service(service) != VCHIQ_SUCCESS))
9090 + for (i = 0; i < (unsigned int)count; i++) {
9091 + if (elements[i].size) {
9092 + if (elements[i].data == NULL) {
9093 + VCHIQ_SERVICE_STATS_INC(service, error_count);
9096 + size += elements[i].size;
9100 + if (size > VCHIQ_MAX_MSG_SIZE) {
9101 + VCHIQ_SERVICE_STATS_INC(service, error_count);
9105 + switch (service->srvstate) {
9106 + case VCHIQ_SRVSTATE_OPEN:
9107 + status = queue_message(service->state, service,
9108 + VCHIQ_MAKE_MSG(VCHIQ_MSG_DATA,
9109 + service->localport,
9110 + service->remoteport),
9111 + elements, count, size, 1);
9113 + case VCHIQ_SRVSTATE_OPENSYNC:
9114 + status = queue_message_sync(service->state, service,
9115 + VCHIQ_MAKE_MSG(VCHIQ_MSG_DATA,
9116 + service->localport,
9117 + service->remoteport),
9118 + elements, count, size, 1);
9121 + status = VCHIQ_ERROR;
9127 + unlock_service(service);
9133 +vchiq_release_message(VCHIQ_SERVICE_HANDLE_T handle, VCHIQ_HEADER_T *header)
9135 + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
9136 + VCHIQ_SHARED_STATE_T *remote;
9137 + VCHIQ_STATE_T *state;
9143 + state = service->state;
9144 + remote = state->remote;
9146 + slot_index = SLOT_INDEX_FROM_DATA(state, (void *)header);
9148 + if ((slot_index >= remote->slot_first) &&
9149 + (slot_index <= remote->slot_last)) {
9150 + int msgid = header->msgid;
9151 + if (msgid & VCHIQ_MSGID_CLAIMED) {
9152 + VCHIQ_SLOT_INFO_T *slot_info =
9153 + SLOT_INFO_FROM_INDEX(state, slot_index);
9155 + release_slot(state, slot_info, header, service);
9157 + } else if (slot_index == remote->slot_sync)
9158 + release_message_sync(state, header);
9160 + unlock_service(service);
9164 +release_message_sync(VCHIQ_STATE_T *state, VCHIQ_HEADER_T *header)
9166 + header->msgid = VCHIQ_MSGID_PADDING;
9168 + remote_event_signal(&state->remote->sync_release);
9172 +vchiq_get_peer_version(VCHIQ_SERVICE_HANDLE_T handle, short *peer_version)
9174 + VCHIQ_STATUS_T status = VCHIQ_ERROR;
9175 + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
9178 + (vchiq_check_service(service) != VCHIQ_SUCCESS) ||
9181 + *peer_version = service->peer_version;
9182 + status = VCHIQ_SUCCESS;
9186 + unlock_service(service);
9191 +vchiq_get_config(VCHIQ_INSTANCE_T instance,
9192 + int config_size, VCHIQ_CONFIG_T *pconfig)
9194 + VCHIQ_CONFIG_T config;
9198 + config.max_msg_size = VCHIQ_MAX_MSG_SIZE;
9199 + config.bulk_threshold = VCHIQ_MAX_MSG_SIZE;
9200 + config.max_outstanding_bulks = VCHIQ_NUM_SERVICE_BULKS;
9201 + config.max_services = VCHIQ_MAX_SERVICES;
9202 + config.version = VCHIQ_VERSION;
9203 + config.version_min = VCHIQ_VERSION_MIN;
9205 + if (config_size > sizeof(VCHIQ_CONFIG_T))
9206 + return VCHIQ_ERROR;
9208 + memcpy(pconfig, &config,
9209 + min(config_size, (int)(sizeof(VCHIQ_CONFIG_T))));
9211 + return VCHIQ_SUCCESS;
9215 +vchiq_set_service_option(VCHIQ_SERVICE_HANDLE_T handle,
9216 + VCHIQ_SERVICE_OPTION_T option, int value)
9218 + VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
9219 + VCHIQ_STATUS_T status = VCHIQ_ERROR;
9223 + case VCHIQ_SERVICE_OPTION_AUTOCLOSE:
9224 + service->auto_close = value;
9225 + status = VCHIQ_SUCCESS;
9228 + case VCHIQ_SERVICE_OPTION_SLOT_QUOTA: {
9229 + VCHIQ_SERVICE_QUOTA_T *service_quota =
9230 + &service->state->service_quotas[
9231 + service->localport];
9233 + value = service->state->default_slot_quota;
9234 + if ((value >= service_quota->slot_use_count) &&
9235 + (value < (unsigned short)~0)) {
9236 + service_quota->slot_quota = value;
9237 + if ((value >= service_quota->slot_use_count) &&
9238 + (service_quota->message_quota >=
9239 + service_quota->message_use_count)) {
9240 + /* Signal the service that it may have
9241 + ** dropped below its quota */
9242 + up(&service_quota->quota_event);
9244 + status = VCHIQ_SUCCESS;
9248 + case VCHIQ_SERVICE_OPTION_MESSAGE_QUOTA: {
9249 + VCHIQ_SERVICE_QUOTA_T *service_quota =
9250 + &service->state->service_quotas[
9251 + service->localport];
9253 + value = service->state->default_message_quota;
9254 + if ((value >= service_quota->message_use_count) &&
9255 + (value < (unsigned short)~0)) {
9256 + service_quota->message_quota = value;
9258 + service_quota->message_use_count) &&
9259 + (service_quota->slot_quota >=
9260 + service_quota->slot_use_count))
9261 + /* Signal the service that it may have
9262 + ** dropped below its quota */
9263 + up(&service_quota->quota_event);
9264 + status = VCHIQ_SUCCESS;
9268 + case VCHIQ_SERVICE_OPTION_SYNCHRONOUS:
9269 + if ((service->srvstate == VCHIQ_SRVSTATE_HIDDEN) ||
9270 + (service->srvstate ==
9271 + VCHIQ_SRVSTATE_LISTENING)) {
9272 + service->sync = value;
9273 + status = VCHIQ_SUCCESS;
9277 + case VCHIQ_SERVICE_OPTION_TRACE:
9278 + service->trace = value;
9279 + status = VCHIQ_SUCCESS;
9285 + unlock_service(service);
9292 +vchiq_dump_shared_state(void *dump_context, VCHIQ_STATE_T *state,
9293 + VCHIQ_SHARED_STATE_T *shared, const char *label)
9295 + static const char *const debug_names[] = {
9297 + "SLOT_HANDLER_COUNT",
9298 + "SLOT_HANDLER_LINE",
9302 + "AWAIT_COMPLETION_LINE",
9303 + "DEQUEUE_MESSAGE_LINE",
9304 + "SERVICE_CALLBACK_LINE",
9305 + "MSG_QUEUE_FULL_COUNT",
9306 + "COMPLETION_QUEUE_FULL_COUNT"
9312 + len = snprintf(buf, sizeof(buf),
9313 + " %s: slots %d-%d tx_pos=%x recycle=%x",
9314 + label, shared->slot_first, shared->slot_last,
9315 + shared->tx_pos, shared->slot_queue_recycle);
9316 + vchiq_dump(dump_context, buf, len + 1);
9318 + len = snprintf(buf, sizeof(buf),
9319 + " Slots claimed:");
9320 + vchiq_dump(dump_context, buf, len + 1);
9322 + for (i = shared->slot_first; i <= shared->slot_last; i++) {
9323 + VCHIQ_SLOT_INFO_T slot_info = *SLOT_INFO_FROM_INDEX(state, i);
9324 + if (slot_info.use_count != slot_info.release_count) {
9325 + len = snprintf(buf, sizeof(buf),
9326 + " %d: %d/%d", i, slot_info.use_count,
9327 + slot_info.release_count);
9328 + vchiq_dump(dump_context, buf, len + 1);
9332 + for (i = 1; i < shared->debug[DEBUG_ENTRIES]; i++) {
9333 + len = snprintf(buf, sizeof(buf), " DEBUG: %s = %d(%x)",
9334 + debug_names[i], shared->debug[i], shared->debug[i]);
9335 + vchiq_dump(dump_context, buf, len + 1);
9340 +vchiq_dump_state(void *dump_context, VCHIQ_STATE_T *state)
9346 + len = snprintf(buf, sizeof(buf), "State %d: %s", state->id,
9347 + conn_state_names[state->conn_state]);
9348 + vchiq_dump(dump_context, buf, len + 1);
9350 + len = snprintf(buf, sizeof(buf),
9351 + " tx_pos=%x(@%x), rx_pos=%x(@%x)",
9352 + state->local->tx_pos,
9353 + (uint32_t)state->tx_data +
9354 + (state->local_tx_pos & VCHIQ_SLOT_MASK),
9356 + (uint32_t)state->rx_data +
9357 + (state->rx_pos & VCHIQ_SLOT_MASK));
9358 + vchiq_dump(dump_context, buf, len + 1);
9360 + len = snprintf(buf, sizeof(buf),
9361 + " Version: %d (min %d)",
9362 + VCHIQ_VERSION, VCHIQ_VERSION_MIN);
9363 + vchiq_dump(dump_context, buf, len + 1);
9365 + if (VCHIQ_ENABLE_STATS) {
9366 + len = snprintf(buf, sizeof(buf),
9367 + " Stats: ctrl_tx_count=%d, ctrl_rx_count=%d, "
9369 + state->stats.ctrl_tx_count, state->stats.ctrl_rx_count,
9370 + state->stats.error_count);
9371 + vchiq_dump(dump_context, buf, len + 1);
9374 + len = snprintf(buf, sizeof(buf),
9375 + " Slots: %d available (%d data), %d recyclable, %d stalls "
9377 + ((state->slot_queue_available * VCHIQ_SLOT_SIZE) -
9378 + state->local_tx_pos) / VCHIQ_SLOT_SIZE,
9379 + state->data_quota - state->data_use_count,
9380 + state->local->slot_queue_recycle - state->slot_queue_available,
9381 + state->stats.slot_stalls, state->stats.data_stalls);
9382 + vchiq_dump(dump_context, buf, len + 1);
9384 + vchiq_dump_platform_state(dump_context);
9386 + vchiq_dump_shared_state(dump_context, state, state->local, "Local");
9387 + vchiq_dump_shared_state(dump_context, state, state->remote, "Remote");
9389 + vchiq_dump_platform_instances(dump_context);
9391 + for (i = 0; i < state->unused_service; i++) {
9392 + VCHIQ_SERVICE_T *service = find_service_by_port(state, i);
9395 + vchiq_dump_service_state(dump_context, service);
9396 + unlock_service(service);
9402 +vchiq_dump_service_state(void *dump_context, VCHIQ_SERVICE_T *service)
9407 + len = snprintf(buf, sizeof(buf), "Service %d: %s (ref %u)",
9408 + service->localport, srvstate_names[service->srvstate],
9409 + service->ref_count - 1); /*Don't include the lock just taken*/
9411 + if (service->srvstate != VCHIQ_SRVSTATE_FREE) {
9412 + char remoteport[30];
9413 + VCHIQ_SERVICE_QUOTA_T *service_quota =
9414 + &service->state->service_quotas[service->localport];
9415 + int fourcc = service->base.fourcc;
9416 + int tx_pending, rx_pending;
9417 + if (service->remoteport != VCHIQ_PORT_FREE) {
9418 + int len2 = snprintf(remoteport, sizeof(remoteport),
9419 + "%d", service->remoteport);
9420 + if (service->public_fourcc != VCHIQ_FOURCC_INVALID)
9421 + snprintf(remoteport + len2,
9422 + sizeof(remoteport) - len2,
9423 + " (client %x)", service->client_id);
9425 + strcpy(remoteport, "n/a");
9427 + len += snprintf(buf + len, sizeof(buf) - len,
9428 + " '%c%c%c%c' remote %s (msg use %d/%d, slot use %d/%d)",
9429 + VCHIQ_FOURCC_AS_4CHARS(fourcc),
9431 + service_quota->message_use_count,
9432 + service_quota->message_quota,
9433 + service_quota->slot_use_count,
9434 + service_quota->slot_quota);
9436 + vchiq_dump(dump_context, buf, len + 1);
9438 + tx_pending = service->bulk_tx.local_insert -
9439 + service->bulk_tx.remote_insert;
9441 + rx_pending = service->bulk_rx.local_insert -
9442 + service->bulk_rx.remote_insert;
9444 + len = snprintf(buf, sizeof(buf),
9445 + " Bulk: tx_pending=%d (size %d),"
9446 + " rx_pending=%d (size %d)",
9448 + tx_pending ? service->bulk_tx.bulks[
9449 + BULK_INDEX(service->bulk_tx.remove)].size : 0,
9451 + rx_pending ? service->bulk_rx.bulks[
9452 + BULK_INDEX(service->bulk_rx.remove)].size : 0);
9454 + if (VCHIQ_ENABLE_STATS) {
9455 + vchiq_dump(dump_context, buf, len + 1);
9457 + len = snprintf(buf, sizeof(buf),
9458 + " Ctrl: tx_count=%d, tx_bytes=%llu, "
9459 + "rx_count=%d, rx_bytes=%llu",
9460 + service->stats.ctrl_tx_count,
9461 + service->stats.ctrl_tx_bytes,
9462 + service->stats.ctrl_rx_count,
9463 + service->stats.ctrl_rx_bytes);
9464 + vchiq_dump(dump_context, buf, len + 1);
9466 + len = snprintf(buf, sizeof(buf),
9467 + " Bulk: tx_count=%d, tx_bytes=%llu, "
9468 + "rx_count=%d, rx_bytes=%llu",
9469 + service->stats.bulk_tx_count,
9470 + service->stats.bulk_tx_bytes,
9471 + service->stats.bulk_rx_count,
9472 + service->stats.bulk_rx_bytes);
9473 + vchiq_dump(dump_context, buf, len + 1);
9475 + len = snprintf(buf, sizeof(buf),
9476 + " %d quota stalls, %d slot stalls, "
9477 + "%d bulk stalls, %d aborted, %d errors",
9478 + service->stats.quota_stalls,
9479 + service->stats.slot_stalls,
9480 + service->stats.bulk_stalls,
9481 + service->stats.bulk_aborted_count,
9482 + service->stats.error_count);
9486 + vchiq_dump(dump_context, buf, len + 1);
9488 + if (service->srvstate != VCHIQ_SRVSTATE_FREE)
9489 + vchiq_dump_platform_service_state(dump_context, service);
9494 +vchiq_loud_error_header(void)
9496 + vchiq_log_error(vchiq_core_log_level,
9497 + "============================================================"
9498 + "================");
9499 + vchiq_log_error(vchiq_core_log_level,
9500 + "============================================================"
9501 + "================");
9502 + vchiq_log_error(vchiq_core_log_level, "=====");
9506 +vchiq_loud_error_footer(void)
9508 + vchiq_log_error(vchiq_core_log_level, "=====");
9509 + vchiq_log_error(vchiq_core_log_level,
9510 + "============================================================"
9511 + "================");
9512 + vchiq_log_error(vchiq_core_log_level,
9513 + "============================================================"
9514 + "================");
9518 +VCHIQ_STATUS_T vchiq_send_remote_use(VCHIQ_STATE_T *state)
9520 + VCHIQ_STATUS_T status = VCHIQ_RETRY;
9521 + if (state->conn_state != VCHIQ_CONNSTATE_DISCONNECTED)
9522 + status = queue_message(state, NULL,
9523 + VCHIQ_MAKE_MSG(VCHIQ_MSG_REMOTE_USE, 0, 0),
9528 +VCHIQ_STATUS_T vchiq_send_remote_release(VCHIQ_STATE_T *state)
9530 + VCHIQ_STATUS_T status = VCHIQ_RETRY;
9531 + if (state->conn_state != VCHIQ_CONNSTATE_DISCONNECTED)
9532 + status = queue_message(state, NULL,
9533 + VCHIQ_MAKE_MSG(VCHIQ_MSG_REMOTE_RELEASE, 0, 0),
9538 +VCHIQ_STATUS_T vchiq_send_remote_use_active(VCHIQ_STATE_T *state)
9540 + VCHIQ_STATUS_T status = VCHIQ_RETRY;
9541 + if (state->conn_state != VCHIQ_CONNSTATE_DISCONNECTED)
9542 + status = queue_message(state, NULL,
9543 + VCHIQ_MAKE_MSG(VCHIQ_MSG_REMOTE_USE_ACTIVE, 0, 0),
9548 +void vchiq_log_dump_mem(const char *label, uint32_t addr, const void *voidMem,
9551 + const uint8_t *mem = (const uint8_t *)voidMem;
9553 + char lineBuf[100];
9556 + while (numBytes > 0) {
9559 + for (offset = 0; offset < 16; offset++) {
9560 + if (offset < numBytes)
9561 + s += snprintf(s, 4, "%02x ", mem[offset]);
9563 + s += snprintf(s, 4, " ");
9566 + for (offset = 0; offset < 16; offset++) {
9567 + if (offset < numBytes) {
9568 + uint8_t ch = mem[offset];
9570 + if ((ch < ' ') || (ch > '~'))
9577 + if ((label != NULL) && (*label != '\0'))
9578 + vchiq_log_trace(VCHIQ_LOG_TRACE,
9579 + "%s: %08x: %s", label, addr, lineBuf);
9581 + vchiq_log_trace(VCHIQ_LOG_TRACE,
9582 + "%08x: %s", addr, lineBuf);
9586 + if (numBytes > 16)
9592 diff --git a/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.h b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.h
9593 new file mode 100644
9594 index 0000000..1b27917
9596 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.h
9599 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
9601 + * Redistribution and use in source and binary forms, with or without
9602 + * modification, are permitted provided that the following conditions
9604 + * 1. Redistributions of source code must retain the above copyright
9605 + * notice, this list of conditions, and the following disclaimer,
9606 + * without modification.
9607 + * 2. Redistributions in binary form must reproduce the above copyright
9608 + * notice, this list of conditions and the following disclaimer in the
9609 + * documentation and/or other materials provided with the distribution.
9610 + * 3. The names of the above-listed copyright holders may not be used
9611 + * to endorse or promote products derived from this software without
9612 + * specific prior written permission.
9614 + * ALTERNATIVELY, this software may be distributed under the terms of the
9615 + * GNU General Public License ("GPL") version 2, as published by the Free
9616 + * Software Foundation.
9618 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
9619 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
9620 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
9621 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
9622 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
9623 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
9624 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
9625 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
9626 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
9627 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
9628 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
9631 +#ifndef VCHIQ_CORE_H
9632 +#define VCHIQ_CORE_H
9634 +#include <linux/mutex.h>
9635 +#include <linux/semaphore.h>
9636 +#include <linux/kthread.h>
9638 +#include "vchiq_cfg.h"
9642 +/* Run time control of log level, based on KERN_XXX level. */
9643 +#define VCHIQ_LOG_DEFAULT 4
9644 +#define VCHIQ_LOG_ERROR 3
9645 +#define VCHIQ_LOG_WARNING 4
9646 +#define VCHIQ_LOG_INFO 6
9647 +#define VCHIQ_LOG_TRACE 7
9649 +#define VCHIQ_LOG_PREFIX KERN_INFO "vchiq: "
9651 +#ifndef vchiq_log_error
9652 +#define vchiq_log_error(cat, fmt, ...) \
9653 + do { if (cat >= VCHIQ_LOG_ERROR) \
9654 + printk(VCHIQ_LOG_PREFIX fmt "\n", ##__VA_ARGS__); } while (0)
9656 +#ifndef vchiq_log_warning
9657 +#define vchiq_log_warning(cat, fmt, ...) \
9658 + do { if (cat >= VCHIQ_LOG_WARNING) \
9659 + printk(VCHIQ_LOG_PREFIX fmt "\n", ##__VA_ARGS__); } while (0)
9661 +#ifndef vchiq_log_info
9662 +#define vchiq_log_info(cat, fmt, ...) \
9663 + do { if (cat >= VCHIQ_LOG_INFO) \
9664 + printk(VCHIQ_LOG_PREFIX fmt "\n", ##__VA_ARGS__); } while (0)
9666 +#ifndef vchiq_log_trace
9667 +#define vchiq_log_trace(cat, fmt, ...) \
9668 + do { if (cat >= VCHIQ_LOG_TRACE) \
9669 + printk(VCHIQ_LOG_PREFIX fmt "\n", ##__VA_ARGS__); } while (0)
9672 +#define vchiq_loud_error(...) \
9673 + vchiq_log_error(vchiq_core_log_level, "===== " __VA_ARGS__)
9675 +#ifndef vchiq_static_assert
9676 +#define vchiq_static_assert(cond) __attribute__((unused)) \
9677 + extern int vchiq_static_assert[(cond) ? 1 : -1]
9680 +#define IS_POW2(x) (x && ((x & (x - 1)) == 0))
9682 +/* Ensure that the slot size and maximum number of slots are powers of 2 */
9683 +vchiq_static_assert(IS_POW2(VCHIQ_SLOT_SIZE));
9684 +vchiq_static_assert(IS_POW2(VCHIQ_MAX_SLOTS));
9685 +vchiq_static_assert(IS_POW2(VCHIQ_MAX_SLOTS_PER_SIDE));
9687 +#define VCHIQ_SLOT_MASK (VCHIQ_SLOT_SIZE - 1)
9688 +#define VCHIQ_SLOT_QUEUE_MASK (VCHIQ_MAX_SLOTS_PER_SIDE - 1)
9689 +#define VCHIQ_SLOT_ZERO_SLOTS ((sizeof(VCHIQ_SLOT_ZERO_T) + \
9690 + VCHIQ_SLOT_SIZE - 1) / VCHIQ_SLOT_SIZE)
9692 +#define VCHIQ_MSG_PADDING 0 /* - */
9693 +#define VCHIQ_MSG_CONNECT 1 /* - */
9694 +#define VCHIQ_MSG_OPEN 2 /* + (srcport, -), fourcc, client_id */
9695 +#define VCHIQ_MSG_OPENACK 3 /* + (srcport, dstport) */
9696 +#define VCHIQ_MSG_CLOSE 4 /* + (srcport, dstport) */
9697 +#define VCHIQ_MSG_DATA 5 /* + (srcport, dstport) */
9698 +#define VCHIQ_MSG_BULK_RX 6 /* + (srcport, dstport), data, size */
9699 +#define VCHIQ_MSG_BULK_TX 7 /* + (srcport, dstport), data, size */
9700 +#define VCHIQ_MSG_BULK_RX_DONE 8 /* + (srcport, dstport), actual */
9701 +#define VCHIQ_MSG_BULK_TX_DONE 9 /* + (srcport, dstport), actual */
9702 +#define VCHIQ_MSG_PAUSE 10 /* - */
9703 +#define VCHIQ_MSG_RESUME 11 /* - */
9704 +#define VCHIQ_MSG_REMOTE_USE 12 /* - */
9705 +#define VCHIQ_MSG_REMOTE_RELEASE 13 /* - */
9706 +#define VCHIQ_MSG_REMOTE_USE_ACTIVE 14 /* - */
9708 +#define VCHIQ_PORT_MAX (VCHIQ_MAX_SERVICES - 1)
9709 +#define VCHIQ_PORT_FREE 0x1000
9710 +#define VCHIQ_PORT_IS_VALID(port) (port < VCHIQ_PORT_FREE)
9711 +#define VCHIQ_MAKE_MSG(type, srcport, dstport) \
9712 + ((type<<24) | (srcport<<12) | (dstport<<0))
9713 +#define VCHIQ_MSG_TYPE(msgid) ((unsigned int)msgid >> 24)
9714 +#define VCHIQ_MSG_SRCPORT(msgid) \
9715 + (unsigned short)(((unsigned int)msgid >> 12) & 0xfff)
9716 +#define VCHIQ_MSG_DSTPORT(msgid) \
9717 + ((unsigned short)msgid & 0xfff)
9719 +#define VCHIQ_FOURCC_AS_4CHARS(fourcc) \
9720 + ((fourcc) >> 24) & 0xff, \
9721 + ((fourcc) >> 16) & 0xff, \
9722 + ((fourcc) >> 8) & 0xff, \
9725 +/* Ensure the fields are wide enough */
9726 +vchiq_static_assert(VCHIQ_MSG_SRCPORT(VCHIQ_MAKE_MSG(0, 0, VCHIQ_PORT_MAX))
9728 +vchiq_static_assert(VCHIQ_MSG_TYPE(VCHIQ_MAKE_MSG(0, VCHIQ_PORT_MAX, 0)) == 0);
9729 +vchiq_static_assert((unsigned int)VCHIQ_PORT_MAX <
9730 + (unsigned int)VCHIQ_PORT_FREE);
9732 +#define VCHIQ_MSGID_PADDING VCHIQ_MAKE_MSG(VCHIQ_MSG_PADDING, 0, 0)
9733 +#define VCHIQ_MSGID_CLAIMED 0x40000000
9735 +#define VCHIQ_FOURCC_INVALID 0x00000000
9736 +#define VCHIQ_FOURCC_IS_LEGAL(fourcc) (fourcc != VCHIQ_FOURCC_INVALID)
9738 +#define VCHIQ_BULK_ACTUAL_ABORTED -1
9740 +typedef uint32_t BITSET_T;
9742 +vchiq_static_assert((sizeof(BITSET_T) * 8) == 32);
9744 +#define BITSET_SIZE(b) ((b + 31) >> 5)
9745 +#define BITSET_WORD(b) (b >> 5)
9746 +#define BITSET_BIT(b) (1 << (b & 31))
9747 +#define BITSET_ZERO(bs) memset(bs, 0, sizeof(bs))
9748 +#define BITSET_IS_SET(bs, b) (bs[BITSET_WORD(b)] & BITSET_BIT(b))
9749 +#define BITSET_SET(bs, b) (bs[BITSET_WORD(b)] |= BITSET_BIT(b))
9750 +#define BITSET_CLR(bs, b) (bs[BITSET_WORD(b)] &= ~BITSET_BIT(b))
9752 +#if VCHIQ_ENABLE_STATS
9753 +#define VCHIQ_STATS_INC(state, stat) (state->stats. stat++)
9754 +#define VCHIQ_SERVICE_STATS_INC(service, stat) (service->stats. stat++)
9755 +#define VCHIQ_SERVICE_STATS_ADD(service, stat, addend) \
9756 + (service->stats. stat += addend)
9758 +#define VCHIQ_STATS_INC(state, stat) ((void)0)
9759 +#define VCHIQ_SERVICE_STATS_INC(service, stat) ((void)0)
9760 +#define VCHIQ_SERVICE_STATS_ADD(service, stat, addend) ((void)0)
9765 +#if VCHIQ_ENABLE_DEBUG
9766 + DEBUG_SLOT_HANDLER_COUNT,
9767 + DEBUG_SLOT_HANDLER_LINE,
9769 + DEBUG_PARSE_HEADER,
9770 + DEBUG_PARSE_MSGID,
9771 + DEBUG_AWAIT_COMPLETION_LINE,
9772 + DEBUG_DEQUEUE_MESSAGE_LINE,
9773 + DEBUG_SERVICE_CALLBACK_LINE,
9774 + DEBUG_MSG_QUEUE_FULL_COUNT,
9775 + DEBUG_COMPLETION_QUEUE_FULL_COUNT,
9780 +#if VCHIQ_ENABLE_DEBUG
9782 +#define DEBUG_INITIALISE(local) int *debug_ptr = (local)->debug;
9783 +#define DEBUG_TRACE(d) \
9784 + do { debug_ptr[DEBUG_ ## d] = __LINE__; dsb(); } while (0)
9785 +#define DEBUG_VALUE(d, v) \
9786 + do { debug_ptr[DEBUG_ ## d] = (v); dsb(); } while (0)
9787 +#define DEBUG_COUNT(d) \
9788 + do { debug_ptr[DEBUG_ ## d]++; dsb(); } while (0)
9790 +#else /* VCHIQ_ENABLE_DEBUG */
9792 +#define DEBUG_INITIALISE(local)
9793 +#define DEBUG_TRACE(d)
9794 +#define DEBUG_VALUE(d, v)
9795 +#define DEBUG_COUNT(d)
9797 +#endif /* VCHIQ_ENABLE_DEBUG */
9800 + VCHIQ_CONNSTATE_DISCONNECTED,
9801 + VCHIQ_CONNSTATE_CONNECTING,
9802 + VCHIQ_CONNSTATE_CONNECTED,
9803 + VCHIQ_CONNSTATE_PAUSING,
9804 + VCHIQ_CONNSTATE_PAUSE_SENT,
9805 + VCHIQ_CONNSTATE_PAUSED,
9806 + VCHIQ_CONNSTATE_RESUMING,
9807 + VCHIQ_CONNSTATE_PAUSE_TIMEOUT,
9808 + VCHIQ_CONNSTATE_RESUME_TIMEOUT
9809 +} VCHIQ_CONNSTATE_T;
9812 + VCHIQ_SRVSTATE_FREE,
9813 + VCHIQ_SRVSTATE_HIDDEN,
9814 + VCHIQ_SRVSTATE_LISTENING,
9815 + VCHIQ_SRVSTATE_OPENING,
9816 + VCHIQ_SRVSTATE_OPEN,
9817 + VCHIQ_SRVSTATE_OPENSYNC,
9818 + VCHIQ_SRVSTATE_CLOSESENT,
9819 + VCHIQ_SRVSTATE_CLOSERECVD,
9820 + VCHIQ_SRVSTATE_CLOSEWAIT,
9821 + VCHIQ_SRVSTATE_CLOSED
9825 + VCHIQ_POLL_TERMINATE,
9826 + VCHIQ_POLL_REMOVE,
9827 + VCHIQ_POLL_TXNOTIFY,
9828 + VCHIQ_POLL_RXNOTIFY,
9833 + VCHIQ_BULK_TRANSMIT,
9834 + VCHIQ_BULK_RECEIVE
9835 +} VCHIQ_BULK_DIR_T;
9837 +typedef void (*VCHIQ_USERDATA_TERM_T)(void *userdata);
9839 +typedef struct vchiq_bulk_struct {
9843 + VCHI_MEM_HANDLE_T handle;
9846 + void *remote_data;
9851 +typedef struct vchiq_bulk_queue_struct {
9852 + int local_insert; /* Where to insert the next local bulk */
9853 + int remote_insert; /* Where to insert the next remote bulk (master) */
9854 + int process; /* Bulk to transfer next */
9855 + int remote_notify; /* Bulk to notify the remote client of next (mstr) */
9856 + int remove; /* Bulk to notify the local client of, and remove,
9858 + VCHIQ_BULK_T bulks[VCHIQ_NUM_SERVICE_BULKS];
9859 +} VCHIQ_BULK_QUEUE_T;
9861 +typedef struct remote_event_struct {
9864 + struct semaphore *event;
9867 +typedef struct opaque_platform_state_t *VCHIQ_PLATFORM_STATE_T;
9869 +typedef struct vchiq_state_struct VCHIQ_STATE_T;
9871 +typedef struct vchiq_slot_struct {
9872 + char data[VCHIQ_SLOT_SIZE];
9875 +typedef struct vchiq_slot_info_struct {
9876 + /* Use two counters rather than one to avoid the need for a mutex. */
9878 + short release_count;
9879 +} VCHIQ_SLOT_INFO_T;
9881 +typedef struct vchiq_service_struct {
9882 + VCHIQ_SERVICE_BASE_T base;
9883 + VCHIQ_SERVICE_HANDLE_T handle;
9884 + unsigned int ref_count;
9886 + VCHIQ_USERDATA_TERM_T userdata_term;
9887 + unsigned int localport;
9888 + unsigned int remoteport;
9889 + int public_fourcc;
9895 + atomic_t poll_flags;
9897 + short version_min;
9898 + short peer_version;
9900 + VCHIQ_STATE_T *state;
9901 + VCHIQ_INSTANCE_T instance;
9903 + int service_use_count;
9905 + VCHIQ_BULK_QUEUE_T bulk_tx;
9906 + VCHIQ_BULK_QUEUE_T bulk_rx;
9908 + struct semaphore remove_event;
9909 + struct semaphore bulk_remove_event;
9910 + struct mutex bulk_mutex;
9912 + struct service_stats_struct {
9917 + int ctrl_tx_count;
9918 + int ctrl_rx_count;
9919 + int bulk_tx_count;
9920 + int bulk_rx_count;
9921 + int bulk_aborted_count;
9922 + uint64_t ctrl_tx_bytes;
9923 + uint64_t ctrl_rx_bytes;
9924 + uint64_t bulk_tx_bytes;
9925 + uint64_t bulk_rx_bytes;
9929 +/* The quota information is outside VCHIQ_SERVICE_T so that it can be
9930 + statically allocated, since for accounting reasons a service's slot
9931 + usage is carried over between users of the same port number.
9933 +typedef struct vchiq_service_quota_struct {
9934 + unsigned short slot_quota;
9935 + unsigned short slot_use_count;
9936 + unsigned short message_quota;
9937 + unsigned short message_use_count;
9938 + struct semaphore quota_event;
9939 + int previous_tx_index;
9940 +} VCHIQ_SERVICE_QUOTA_T;
9942 +typedef struct vchiq_shared_state_struct {
9944 + /* A non-zero value here indicates that the content is valid. */
9947 + /* The first and last (inclusive) slots allocated to the owner. */
9951 + /* The slot allocated to synchronous messages from the owner. */
9954 + /* Signalling this event indicates that owner's slot handler thread
9956 + REMOTE_EVENT_T trigger;
9958 + /* Indicates the byte position within the stream where the next message
9959 + ** will be written. The least significant bits are an index into the
9960 + ** slot. The next bits are the index of the slot in slot_queue. */
9963 + /* This event should be signalled when a slot is recycled. */
9964 + REMOTE_EVENT_T recycle;
9966 + /* The slot_queue index where the next recycled slot will be written. */
9967 + int slot_queue_recycle;
9969 + /* This event should be signalled when a synchronous message is sent. */
9970 + REMOTE_EVENT_T sync_trigger;
9972 + /* This event should be signalled when a synchronous message has been
9974 + REMOTE_EVENT_T sync_release;
9976 + /* A circular buffer of slot indexes. */
9977 + int slot_queue[VCHIQ_MAX_SLOTS_PER_SIDE];
9979 + /* Debugging state */
9980 + int debug[DEBUG_MAX];
9981 +} VCHIQ_SHARED_STATE_T;
9983 +typedef struct vchiq_slot_zero_struct {
9986 + short version_min;
9987 + int slot_zero_size;
9990 + int max_slots_per_side;
9991 + int platform_data[2];
9992 + VCHIQ_SHARED_STATE_T master;
9993 + VCHIQ_SHARED_STATE_T slave;
9994 + VCHIQ_SLOT_INFO_T slots[VCHIQ_MAX_SLOTS];
9995 +} VCHIQ_SLOT_ZERO_T;
9997 +struct vchiq_state_struct {
10000 + VCHIQ_CONNSTATE_T conn_state;
10003 + VCHIQ_SHARED_STATE_T *local;
10004 + VCHIQ_SHARED_STATE_T *remote;
10005 + VCHIQ_SLOT_T *slot_data;
10007 + unsigned short default_slot_quota;
10008 + unsigned short default_message_quota;
10010 + /* Event indicating connect message received */
10011 + struct semaphore connect;
10013 + /* Mutex protecting services */
10014 + struct mutex mutex;
10015 + VCHIQ_INSTANCE_T *instance;
10017 + /* Processes incoming messages */
10018 + struct task_struct *slot_handler_thread;
10020 + /* Processes recycled slots */
10021 + struct task_struct *recycle_thread;
10023 + /* Processes synchronous messages */
10024 + struct task_struct *sync_thread;
10026 + /* Local implementation of the trigger remote event */
10027 + struct semaphore trigger_event;
10029 + /* Local implementation of the recycle remote event */
10030 + struct semaphore recycle_event;
10032 + /* Local implementation of the sync trigger remote event */
10033 + struct semaphore sync_trigger_event;
10035 + /* Local implementation of the sync release remote event */
10036 + struct semaphore sync_release_event;
10040 + VCHIQ_SLOT_INFO_T *rx_info;
10042 + struct mutex slot_mutex;
10044 + struct mutex recycle_mutex;
10046 + struct mutex sync_mutex;
10048 + struct mutex bulk_transfer_mutex;
10050 + /* Indicates the byte position within the stream from where the next
10051 + ** message will be read. The least significant bits are an index into
10052 + ** the slot.The next bits are the index of the slot in
10053 + ** remote->slot_queue. */
10056 + /* A cached copy of local->tx_pos. Only write to local->tx_pos, and read
10057 + from remote->tx_pos. */
10058 + int local_tx_pos;
10060 + /* The slot_queue index of the slot to become available next. */
10061 + int slot_queue_available;
10063 + /* A flag to indicate if any poll has been requested */
10066 + /* Ths index of the previous slot used for data messages. */
10067 + int previous_data_index;
10069 + /* The number of slots occupied by data messages. */
10070 + unsigned short data_use_count;
10072 + /* The maximum number of slots to be occupied by data messages. */
10073 + unsigned short data_quota;
10075 + /* An array of bit sets indicating which services must be polled. */
10076 + atomic_t poll_services[BITSET_SIZE(VCHIQ_MAX_SERVICES)];
10078 + /* The number of the first unused service */
10079 + int unused_service;
10081 + /* Signalled when a free slot becomes available. */
10082 + struct semaphore slot_available_event;
10084 + struct semaphore slot_remove_event;
10086 + /* Signalled when a free data slot becomes available. */
10087 + struct semaphore data_quota_event;
10089 + /* Incremented when there are bulk transfers which cannot be processed
10090 + * whilst paused and must be processed on resume */
10091 + int deferred_bulks;
10093 + struct state_stats_struct {
10096 + int ctrl_tx_count;
10097 + int ctrl_rx_count;
10101 + VCHIQ_SERVICE_T * services[VCHIQ_MAX_SERVICES];
10102 + VCHIQ_SERVICE_QUOTA_T service_quotas[VCHIQ_MAX_SERVICES];
10103 + VCHIQ_SLOT_INFO_T slot_info[VCHIQ_MAX_SLOTS];
10105 + VCHIQ_PLATFORM_STATE_T platform_state;
10108 +struct bulk_waiter {
10109 + VCHIQ_BULK_T *bulk;
10110 + struct semaphore event;
10114 +extern spinlock_t bulk_waiter_spinlock;
10116 +extern int vchiq_core_log_level;
10117 +extern int vchiq_core_msg_log_level;
10118 +extern int vchiq_sync_log_level;
10120 +extern VCHIQ_STATE_T *vchiq_states[VCHIQ_MAX_STATES];
10122 +extern const char *
10123 +get_conn_state_name(VCHIQ_CONNSTATE_T conn_state);
10125 +extern VCHIQ_SLOT_ZERO_T *
10126 +vchiq_init_slots(void *mem_base, int mem_size);
10128 +extern VCHIQ_STATUS_T
10129 +vchiq_init_state(VCHIQ_STATE_T *state, VCHIQ_SLOT_ZERO_T *slot_zero,
10132 +extern VCHIQ_STATUS_T
10133 +vchiq_connect_internal(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance);
10135 +extern VCHIQ_SERVICE_T *
10136 +vchiq_add_service_internal(VCHIQ_STATE_T *state,
10137 + const VCHIQ_SERVICE_PARAMS_T *params, int srvstate,
10138 + VCHIQ_INSTANCE_T instance, VCHIQ_USERDATA_TERM_T userdata_term);
10140 +extern VCHIQ_STATUS_T
10141 +vchiq_open_service_internal(VCHIQ_SERVICE_T *service, int client_id);
10143 +extern VCHIQ_STATUS_T
10144 +vchiq_close_service_internal(VCHIQ_SERVICE_T *service, int close_recvd);
10147 +vchiq_terminate_service_internal(VCHIQ_SERVICE_T *service);
10150 +vchiq_free_service_internal(VCHIQ_SERVICE_T *service);
10152 +extern VCHIQ_STATUS_T
10153 +vchiq_shutdown_internal(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance);
10155 +extern VCHIQ_STATUS_T
10156 +vchiq_pause_internal(VCHIQ_STATE_T *state);
10158 +extern VCHIQ_STATUS_T
10159 +vchiq_resume_internal(VCHIQ_STATE_T *state);
10162 +remote_event_pollall(VCHIQ_STATE_T *state);
10164 +extern VCHIQ_STATUS_T
10165 +vchiq_bulk_transfer(VCHIQ_SERVICE_HANDLE_T handle,
10166 + VCHI_MEM_HANDLE_T memhandle, void *offset, int size, void *userdata,
10167 + VCHIQ_BULK_MODE_T mode, VCHIQ_BULK_DIR_T dir);
10170 +vchiq_dump_state(void *dump_context, VCHIQ_STATE_T *state);
10173 +vchiq_dump_service_state(void *dump_context, VCHIQ_SERVICE_T *service);
10176 +vchiq_loud_error_header(void);
10179 +vchiq_loud_error_footer(void);
10182 +request_poll(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service, int poll_type);
10184 +static inline VCHIQ_SERVICE_T *
10185 +handle_to_service(VCHIQ_SERVICE_HANDLE_T handle)
10187 + VCHIQ_STATE_T *state = vchiq_states[(handle / VCHIQ_MAX_SERVICES) &
10188 + (VCHIQ_MAX_STATES - 1)];
10192 + return state->services[handle & (VCHIQ_MAX_SERVICES - 1)];
10195 +extern VCHIQ_SERVICE_T *
10196 +find_service_by_handle(VCHIQ_SERVICE_HANDLE_T handle);
10198 +extern VCHIQ_SERVICE_T *
10199 +find_service_by_port(VCHIQ_STATE_T *state, int localport);
10201 +extern VCHIQ_SERVICE_T *
10202 +find_service_for_instance(VCHIQ_INSTANCE_T instance,
10203 + VCHIQ_SERVICE_HANDLE_T handle);
10205 +extern VCHIQ_SERVICE_T *
10206 +find_closed_service_for_instance(VCHIQ_INSTANCE_T instance,
10207 + VCHIQ_SERVICE_HANDLE_T handle);
10209 +extern VCHIQ_SERVICE_T *
10210 +next_service_by_instance(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance,
10214 +lock_service(VCHIQ_SERVICE_T *service);
10217 +unlock_service(VCHIQ_SERVICE_T *service);
10219 +/* The following functions are called from vchiq_core, and external
10220 +** implementations must be provided. */
10222 +extern VCHIQ_STATUS_T
10223 +vchiq_prepare_bulk_data(VCHIQ_BULK_T *bulk,
10224 + VCHI_MEM_HANDLE_T memhandle, void *offset, int size, int dir);
10227 +vchiq_transfer_bulk(VCHIQ_BULK_T *bulk);
10230 +vchiq_complete_bulk(VCHIQ_BULK_T *bulk);
10232 +extern VCHIQ_STATUS_T
10233 +vchiq_copy_from_user(void *dst, const void *src, int size);
10236 +remote_event_signal(REMOTE_EVENT_T *event);
10239 +vchiq_platform_check_suspend(VCHIQ_STATE_T *state);
10242 +vchiq_platform_paused(VCHIQ_STATE_T *state);
10244 +extern VCHIQ_STATUS_T
10245 +vchiq_platform_resume(VCHIQ_STATE_T *state);
10248 +vchiq_platform_resumed(VCHIQ_STATE_T *state);
10251 +vchiq_dump(void *dump_context, const char *str, int len);
10254 +vchiq_dump_platform_state(void *dump_context);
10257 +vchiq_dump_platform_instances(void *dump_context);
10260 +vchiq_dump_platform_service_state(void *dump_context,
10261 + VCHIQ_SERVICE_T *service);
10263 +extern VCHIQ_STATUS_T
10264 +vchiq_use_service_internal(VCHIQ_SERVICE_T *service);
10266 +extern VCHIQ_STATUS_T
10267 +vchiq_release_service_internal(VCHIQ_SERVICE_T *service);
10270 +vchiq_on_remote_use(VCHIQ_STATE_T *state);
10273 +vchiq_on_remote_release(VCHIQ_STATE_T *state);
10275 +extern VCHIQ_STATUS_T
10276 +vchiq_platform_init_state(VCHIQ_STATE_T *state);
10278 +extern VCHIQ_STATUS_T
10279 +vchiq_check_service(VCHIQ_SERVICE_T *service);
10282 +vchiq_on_remote_use_active(VCHIQ_STATE_T *state);
10284 +extern VCHIQ_STATUS_T
10285 +vchiq_send_remote_use(VCHIQ_STATE_T *state);
10287 +extern VCHIQ_STATUS_T
10288 +vchiq_send_remote_release(VCHIQ_STATE_T *state);
10290 +extern VCHIQ_STATUS_T
10291 +vchiq_send_remote_use_active(VCHIQ_STATE_T *state);
10294 +vchiq_platform_conn_state_changed(VCHIQ_STATE_T *state,
10295 + VCHIQ_CONNSTATE_T oldstate, VCHIQ_CONNSTATE_T newstate);
10298 +vchiq_platform_handle_timeout(VCHIQ_STATE_T *state);
10301 +vchiq_set_conn_state(VCHIQ_STATE_T *state, VCHIQ_CONNSTATE_T newstate);
10305 +vchiq_log_dump_mem(const char *label, uint32_t addr, const void *voidMem,
10306 + size_t numBytes);
10309 diff --git a/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_debugfs.c b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_debugfs.c
10310 new file mode 100644
10311 index 0000000..7e03213
10313 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_debugfs.c
10316 + * Copyright (c) 2014 Raspberry Pi (Trading) Ltd. All rights reserved.
10317 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
10319 + * Redistribution and use in source and binary forms, with or without
10320 + * modification, are permitted provided that the following conditions
10322 + * 1. Redistributions of source code must retain the above copyright
10323 + * notice, this list of conditions, and the following disclaimer,
10324 + * without modification.
10325 + * 2. Redistributions in binary form must reproduce the above copyright
10326 + * notice, this list of conditions and the following disclaimer in the
10327 + * documentation and/or other materials provided with the distribution.
10328 + * 3. The names of the above-listed copyright holders may not be used
10329 + * to endorse or promote products derived from this software without
10330 + * specific prior written permission.
10332 + * ALTERNATIVELY, this software may be distributed under the terms of the
10333 + * GNU General Public License ("GPL") version 2, as published by the Free
10334 + * Software Foundation.
10336 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
10337 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
10338 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
10339 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
10340 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
10341 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
10342 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
10343 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
10344 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
10345 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
10346 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
10350 +#include <linux/debugfs.h>
10351 +#include "vchiq_core.h"
10352 +#include "vchiq_arm.h"
10353 +#include "vchiq_debugfs.h"
10355 +#ifdef CONFIG_DEBUG_FS
10357 +/****************************************************************************
10359 +* log category entries
10361 +***************************************************************************/
10362 +#define DEBUGFS_WRITE_BUF_SIZE 256
10364 +#define VCHIQ_LOG_ERROR_STR "error"
10365 +#define VCHIQ_LOG_WARNING_STR "warning"
10366 +#define VCHIQ_LOG_INFO_STR "info"
10367 +#define VCHIQ_LOG_TRACE_STR "trace"
10370 +/* Top-level debug info */
10371 +struct vchiq_debugfs_info {
10372 + /* Global 'vchiq' debugfs entry used by all instances */
10373 + struct dentry *vchiq_cfg_dir;
10375 + /* one entry per client process */
10376 + struct dentry *clients;
10378 + /* log categories */
10379 + struct dentry *log_categories;
10382 +static struct vchiq_debugfs_info debugfs_info;
10384 +/* Log category debugfs entries */
10385 +struct vchiq_debugfs_log_entry {
10386 + const char *name;
10388 + struct dentry *dir;
10391 +static struct vchiq_debugfs_log_entry vchiq_debugfs_log_entries[] = {
10392 + { "core", &vchiq_core_log_level },
10393 + { "msg", &vchiq_core_msg_log_level },
10394 + { "sync", &vchiq_sync_log_level },
10395 + { "susp", &vchiq_susp_log_level },
10396 + { "arm", &vchiq_arm_log_level },
10398 +static int n_log_entries =
10399 + sizeof(vchiq_debugfs_log_entries)/sizeof(vchiq_debugfs_log_entries[0]);
10402 +static struct dentry *vchiq_clients_top(void);
10403 +static struct dentry *vchiq_debugfs_top(void);
10405 +static int debugfs_log_show(struct seq_file *f, void *offset)
10407 + int *levp = f->private;
10408 + char *log_value = NULL;
10411 + case VCHIQ_LOG_ERROR:
10412 + log_value = VCHIQ_LOG_ERROR_STR;
10414 + case VCHIQ_LOG_WARNING:
10415 + log_value = VCHIQ_LOG_WARNING_STR;
10417 + case VCHIQ_LOG_INFO:
10418 + log_value = VCHIQ_LOG_INFO_STR;
10420 + case VCHIQ_LOG_TRACE:
10421 + log_value = VCHIQ_LOG_TRACE_STR;
10427 + seq_printf(f, "%s\n", log_value ? log_value : "(null)");
10432 +static int debugfs_log_open(struct inode *inode, struct file *file)
10434 + return single_open(file, debugfs_log_show, inode->i_private);
10437 +static int debugfs_log_write(struct file *file,
10438 + const char __user *buffer,
10439 + size_t count, loff_t *ppos)
10441 + struct seq_file *f = (struct seq_file *)file->private_data;
10442 + int *levp = f->private;
10443 + char kbuf[DEBUGFS_WRITE_BUF_SIZE + 1];
10445 + memset(kbuf, 0, DEBUGFS_WRITE_BUF_SIZE + 1);
10446 + if (count >= DEBUGFS_WRITE_BUF_SIZE)
10447 + count = DEBUGFS_WRITE_BUF_SIZE;
10449 + if (copy_from_user(kbuf, buffer, count) != 0)
10451 + kbuf[count - 1] = 0;
10453 + if (strncmp("error", kbuf, strlen("error")) == 0)
10454 + *levp = VCHIQ_LOG_ERROR;
10455 + else if (strncmp("warning", kbuf, strlen("warning")) == 0)
10456 + *levp = VCHIQ_LOG_WARNING;
10457 + else if (strncmp("info", kbuf, strlen("info")) == 0)
10458 + *levp = VCHIQ_LOG_INFO;
10459 + else if (strncmp("trace", kbuf, strlen("trace")) == 0)
10460 + *levp = VCHIQ_LOG_TRACE;
10462 + *levp = VCHIQ_LOG_DEFAULT;
10469 +static const struct file_operations debugfs_log_fops = {
10470 + .owner = THIS_MODULE,
10471 + .open = debugfs_log_open,
10472 + .write = debugfs_log_write,
10473 + .read = seq_read,
10474 + .llseek = seq_lseek,
10475 + .release = single_release,
10478 +/* create an entry under <debugfs>/vchiq/log for each log category */
10479 +static int vchiq_debugfs_create_log_entries(struct dentry *top)
10481 + struct dentry *dir;
10484 + dir = debugfs_create_dir("log", vchiq_debugfs_top());
10487 + debugfs_info.log_categories = dir;
10489 + for (i = 0; i < n_log_entries; i++) {
10490 + void *levp = (void *)vchiq_debugfs_log_entries[i].plevel;
10491 + dir = debugfs_create_file(vchiq_debugfs_log_entries[i].name,
10493 + debugfs_info.log_categories,
10495 + &debugfs_log_fops);
10501 + vchiq_debugfs_log_entries[i].dir = dir;
10506 +static int debugfs_usecount_show(struct seq_file *f, void *offset)
10508 + VCHIQ_INSTANCE_T instance = f->private;
10511 + use_count = vchiq_instance_get_use_count(instance);
10512 + seq_printf(f, "%d\n", use_count);
10517 +static int debugfs_usecount_open(struct inode *inode, struct file *file)
10519 + return single_open(file, debugfs_usecount_show, inode->i_private);
10522 +static const struct file_operations debugfs_usecount_fops = {
10523 + .owner = THIS_MODULE,
10524 + .open = debugfs_usecount_open,
10525 + .read = seq_read,
10526 + .llseek = seq_lseek,
10527 + .release = single_release,
10530 +static int debugfs_trace_show(struct seq_file *f, void *offset)
10532 + VCHIQ_INSTANCE_T instance = f->private;
10535 + trace = vchiq_instance_get_trace(instance);
10536 + seq_printf(f, "%s\n", trace ? "Y" : "N");
10541 +static int debugfs_trace_open(struct inode *inode, struct file *file)
10543 + return single_open(file, debugfs_trace_show, inode->i_private);
10546 +static int debugfs_trace_write(struct file *file,
10547 + const char __user *buffer,
10548 + size_t count, loff_t *ppos)
10550 + struct seq_file *f = (struct seq_file *)file->private_data;
10551 + VCHIQ_INSTANCE_T instance = f->private;
10554 + if (copy_from_user(&firstchar, buffer, 1) != 0)
10557 + switch (firstchar) {
10561 + vchiq_instance_set_trace(instance, 1);
10566 + vchiq_instance_set_trace(instance, 0);
10577 +static const struct file_operations debugfs_trace_fops = {
10578 + .owner = THIS_MODULE,
10579 + .open = debugfs_trace_open,
10580 + .write = debugfs_trace_write,
10581 + .read = seq_read,
10582 + .llseek = seq_lseek,
10583 + .release = single_release,
10586 +/* add an instance (process) to the debugfs entries */
10587 +int vchiq_debugfs_add_instance(VCHIQ_INSTANCE_T instance)
10590 + struct dentry *top, *use_count, *trace;
10591 + struct dentry *clients = vchiq_clients_top();
10593 + snprintf(pidstr, sizeof(pidstr), "%d",
10594 + vchiq_instance_get_pid(instance));
10596 + top = debugfs_create_dir(pidstr, clients);
10600 + use_count = debugfs_create_file("use_count",
10603 + &debugfs_usecount_fops);
10605 + goto fail_use_count;
10607 + trace = debugfs_create_file("trace",
10610 + &debugfs_trace_fops);
10614 + vchiq_instance_get_debugfs_node(instance)->dentry = top;
10619 + debugfs_remove(use_count);
10621 + debugfs_remove(top);
10626 +void vchiq_debugfs_remove_instance(VCHIQ_INSTANCE_T instance)
10628 + VCHIQ_DEBUGFS_NODE_T *node = vchiq_instance_get_debugfs_node(instance);
10629 + debugfs_remove_recursive(node->dentry);
10633 +int vchiq_debugfs_init(void)
10635 + BUG_ON(debugfs_info.vchiq_cfg_dir != NULL);
10637 + debugfs_info.vchiq_cfg_dir = debugfs_create_dir("vchiq", NULL);
10638 + if (debugfs_info.vchiq_cfg_dir == NULL)
10641 + debugfs_info.clients = debugfs_create_dir("clients",
10642 + vchiq_debugfs_top());
10643 + if (!debugfs_info.clients)
10646 + if (vchiq_debugfs_create_log_entries(vchiq_debugfs_top()) != 0)
10652 + vchiq_debugfs_deinit();
10653 + vchiq_log_error(vchiq_arm_log_level,
10654 + "%s: failed to create debugfs directory",
10660 +/* remove all the debugfs entries */
10661 +void vchiq_debugfs_deinit(void)
10663 + debugfs_remove_recursive(vchiq_debugfs_top());
10666 +static struct dentry *vchiq_clients_top(void)
10668 + return debugfs_info.clients;
10671 +static struct dentry *vchiq_debugfs_top(void)
10673 + BUG_ON(debugfs_info.vchiq_cfg_dir == NULL);
10674 + return debugfs_info.vchiq_cfg_dir;
10677 +#else /* CONFIG_DEBUG_FS */
10679 +int vchiq_debugfs_init(void)
10684 +void vchiq_debugfs_deinit(void)
10688 +int vchiq_debugfs_add_instance(VCHIQ_INSTANCE_T instance)
10693 +void vchiq_debugfs_remove_instance(VCHIQ_INSTANCE_T instance)
10697 +#endif /* CONFIG_DEBUG_FS */
10698 diff --git a/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_debugfs.h b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_debugfs.h
10699 new file mode 100644
10700 index 0000000..4d6a378
10702 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_debugfs.h
10705 + * Copyright (c) 2014 Raspberry Pi (Trading) Ltd. All rights reserved.
10707 + * Redistribution and use in source and binary forms, with or without
10708 + * modification, are permitted provided that the following conditions
10710 + * 1. Redistributions of source code must retain the above copyright
10711 + * notice, this list of conditions, and the following disclaimer,
10712 + * without modification.
10713 + * 2. Redistributions in binary form must reproduce the above copyright
10714 + * notice, this list of conditions and the following disclaimer in the
10715 + * documentation and/or other materials provided with the distribution.
10716 + * 3. The names of the above-listed copyright holders may not be used
10717 + * to endorse or promote products derived from this software without
10718 + * specific prior written permission.
10720 + * ALTERNATIVELY, this software may be distributed under the terms of the
10721 + * GNU General Public License ("GPL") version 2, as published by the Free
10722 + * Software Foundation.
10724 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
10725 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
10726 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
10727 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
10728 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
10729 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
10730 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
10731 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
10732 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
10733 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
10734 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
10737 +#ifndef VCHIQ_DEBUGFS_H
10738 +#define VCHIQ_DEBUGFS_H
10740 +#include "vchiq_core.h"
10742 +typedef struct vchiq_debugfs_node_struct
10744 + struct dentry *dentry;
10745 +} VCHIQ_DEBUGFS_NODE_T;
10747 +int vchiq_debugfs_init(void);
10749 +void vchiq_debugfs_deinit(void);
10751 +int vchiq_debugfs_add_instance(VCHIQ_INSTANCE_T instance);
10753 +void vchiq_debugfs_remove_instance(VCHIQ_INSTANCE_T instance);
10755 +#endif /* VCHIQ_DEBUGFS_H */
10756 diff --git a/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_genversion b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_genversion
10757 new file mode 100644
10758 index 0000000..9f5b634
10760 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_genversion
10762 +#!/usr/bin/perl -w
10767 +# Generate a version from available information
10770 +my $prefix = shift @ARGV;
10771 +my $root = shift @ARGV;
10774 +if ( not defined $root ) {
10775 + die "usage: $0 prefix root-dir\n";
10778 +if ( ! -d $root ) {
10779 + die "root directory $root not found\n";
10782 +my $version = "unknown";
10785 +if ( -d "$root/.git" ) {
10786 + # attempt to work out git version. only do so
10787 + # on a linux build host, as cygwin builds are
10788 + # already slow enough
10790 + if ( -f "/usr/bin/git" || -f "/usr/local/bin/git" ) {
10791 + if (not open(F, "git --git-dir $root/.git rev-parse --verify HEAD|")) {
10792 + $version = "no git version";
10796 + $version =~ s/[ \r\n]*$//; # chomp may not be enough (cygwin).
10797 + $version =~ s/^[ \r\n]*//; # chomp may not be enough (cygwin).
10800 + if (open(G, "git --git-dir $root/.git status --porcelain|")) {
10802 + $tainted =~ s/[ \r\n]*$//; # chomp may not be enough (cygwin).
10803 + $tainted =~ s/^[ \r\n]*//; # chomp may not be enough (cygwin).
10804 + if (length $tainted) {
10805 + $version = join ' ', $version, "(tainted)";
10808 + $version = join ' ', $version, "(clean)";
10814 +my $hostname = `hostname`;
10815 +$hostname =~ s/[ \r\n]*$//; # chomp may not be enough (cygwin).
10816 +$hostname =~ s/^[ \r\n]*//; # chomp may not be enough (cygwin).
10819 +print STDERR "Version $version\n";
10821 +#include "${prefix}_build_info.h"
10822 +#include <linux/broadcom/vc_debug_sym.h>
10824 +VC_DEBUG_DECLARE_STRING_VAR( ${prefix}_build_hostname, "$hostname" );
10825 +VC_DEBUG_DECLARE_STRING_VAR( ${prefix}_build_version, "$version" );
10826 +VC_DEBUG_DECLARE_STRING_VAR( ${prefix}_build_time, __TIME__ );
10827 +VC_DEBUG_DECLARE_STRING_VAR( ${prefix}_build_date, __DATE__ );
10829 +const char *vchiq_get_build_hostname( void )
10831 + return vchiq_build_hostname;
10834 +const char *vchiq_get_build_version( void )
10836 + return vchiq_build_version;
10839 +const char *vchiq_get_build_date( void )
10841 + return vchiq_build_date;
10844 +const char *vchiq_get_build_time( void )
10846 + return vchiq_build_time;
10849 diff --git a/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_if.h b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_if.h
10850 new file mode 100644
10851 index 0000000..8067bbe
10853 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_if.h
10856 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
10858 + * Redistribution and use in source and binary forms, with or without
10859 + * modification, are permitted provided that the following conditions
10861 + * 1. Redistributions of source code must retain the above copyright
10862 + * notice, this list of conditions, and the following disclaimer,
10863 + * without modification.
10864 + * 2. Redistributions in binary form must reproduce the above copyright
10865 + * notice, this list of conditions and the following disclaimer in the
10866 + * documentation and/or other materials provided with the distribution.
10867 + * 3. The names of the above-listed copyright holders may not be used
10868 + * to endorse or promote products derived from this software without
10869 + * specific prior written permission.
10871 + * ALTERNATIVELY, this software may be distributed under the terms of the
10872 + * GNU General Public License ("GPL") version 2, as published by the Free
10873 + * Software Foundation.
10875 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
10876 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
10877 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
10878 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
10879 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
10880 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
10881 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
10882 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
10883 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
10884 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
10885 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
10888 +#ifndef VCHIQ_IF_H
10889 +#define VCHIQ_IF_H
10891 +#include "interface/vchi/vchi_mh.h"
10893 +#define VCHIQ_SERVICE_HANDLE_INVALID 0
10895 +#define VCHIQ_SLOT_SIZE 4096
10896 +#define VCHIQ_MAX_MSG_SIZE (VCHIQ_SLOT_SIZE - sizeof(VCHIQ_HEADER_T))
10897 +#define VCHIQ_CHANNEL_SIZE VCHIQ_MAX_MSG_SIZE /* For backwards compatibility */
10899 +#define VCHIQ_MAKE_FOURCC(x0, x1, x2, x3) \
10900 + (((x0) << 24) | ((x1) << 16) | ((x2) << 8) | (x3))
10901 +#define VCHIQ_GET_SERVICE_USERDATA(service) vchiq_get_service_userdata(service)
10902 +#define VCHIQ_GET_SERVICE_FOURCC(service) vchiq_get_service_fourcc(service)
10905 + VCHIQ_SERVICE_OPENED, /* service, -, - */
10906 + VCHIQ_SERVICE_CLOSED, /* service, -, - */
10907 + VCHIQ_MESSAGE_AVAILABLE, /* service, header, - */
10908 + VCHIQ_BULK_TRANSMIT_DONE, /* service, -, bulk_userdata */
10909 + VCHIQ_BULK_RECEIVE_DONE, /* service, -, bulk_userdata */
10910 + VCHIQ_BULK_TRANSMIT_ABORTED, /* service, -, bulk_userdata */
10911 + VCHIQ_BULK_RECEIVE_ABORTED /* service, -, bulk_userdata */
10915 + VCHIQ_ERROR = -1,
10916 + VCHIQ_SUCCESS = 0,
10921 + VCHIQ_BULK_MODE_CALLBACK,
10922 + VCHIQ_BULK_MODE_BLOCKING,
10923 + VCHIQ_BULK_MODE_NOCALLBACK,
10924 + VCHIQ_BULK_MODE_WAITING /* Reserved for internal use */
10925 +} VCHIQ_BULK_MODE_T;
10928 + VCHIQ_SERVICE_OPTION_AUTOCLOSE,
10929 + VCHIQ_SERVICE_OPTION_SLOT_QUOTA,
10930 + VCHIQ_SERVICE_OPTION_MESSAGE_QUOTA,
10931 + VCHIQ_SERVICE_OPTION_SYNCHRONOUS,
10932 + VCHIQ_SERVICE_OPTION_TRACE
10933 +} VCHIQ_SERVICE_OPTION_T;
10935 +typedef struct vchiq_header_struct {
10936 + /* The message identifier - opaque to applications. */
10939 + /* Size of message data. */
10940 + unsigned int size;
10942 + char data[0]; /* message */
10946 + const void *data;
10947 + unsigned int size;
10948 +} VCHIQ_ELEMENT_T;
10950 +typedef unsigned int VCHIQ_SERVICE_HANDLE_T;
10952 +typedef VCHIQ_STATUS_T (*VCHIQ_CALLBACK_T)(VCHIQ_REASON_T, VCHIQ_HEADER_T *,
10953 + VCHIQ_SERVICE_HANDLE_T, void *);
10955 +typedef struct vchiq_service_base_struct {
10957 + VCHIQ_CALLBACK_T callback;
10959 +} VCHIQ_SERVICE_BASE_T;
10961 +typedef struct vchiq_service_params_struct {
10963 + VCHIQ_CALLBACK_T callback;
10965 + short version; /* Increment for non-trivial changes */
10966 + short version_min; /* Update for incompatible changes */
10967 +} VCHIQ_SERVICE_PARAMS_T;
10969 +typedef struct vchiq_config_struct {
10970 + unsigned int max_msg_size;
10971 + unsigned int bulk_threshold; /* The message size above which it
10972 + is better to use a bulk transfer
10973 + (<= max_msg_size) */
10974 + unsigned int max_outstanding_bulks;
10975 + unsigned int max_services;
10976 + short version; /* The version of VCHIQ */
10977 + short version_min; /* The minimum compatible version of VCHIQ */
10980 +typedef struct vchiq_instance_struct *VCHIQ_INSTANCE_T;
10981 +typedef void (*VCHIQ_REMOTE_USE_CALLBACK_T)(void *cb_arg);
10983 +extern VCHIQ_STATUS_T vchiq_initialise(VCHIQ_INSTANCE_T *pinstance);
10984 +extern VCHIQ_STATUS_T vchiq_shutdown(VCHIQ_INSTANCE_T instance);
10985 +extern VCHIQ_STATUS_T vchiq_connect(VCHIQ_INSTANCE_T instance);
10986 +extern VCHIQ_STATUS_T vchiq_add_service(VCHIQ_INSTANCE_T instance,
10987 + const VCHIQ_SERVICE_PARAMS_T *params,
10988 + VCHIQ_SERVICE_HANDLE_T *pservice);
10989 +extern VCHIQ_STATUS_T vchiq_open_service(VCHIQ_INSTANCE_T instance,
10990 + const VCHIQ_SERVICE_PARAMS_T *params,
10991 + VCHIQ_SERVICE_HANDLE_T *pservice);
10992 +extern VCHIQ_STATUS_T vchiq_close_service(VCHIQ_SERVICE_HANDLE_T service);
10993 +extern VCHIQ_STATUS_T vchiq_remove_service(VCHIQ_SERVICE_HANDLE_T service);
10994 +extern VCHIQ_STATUS_T vchiq_use_service(VCHIQ_SERVICE_HANDLE_T service);
10995 +extern VCHIQ_STATUS_T vchiq_use_service_no_resume(
10996 + VCHIQ_SERVICE_HANDLE_T service);
10997 +extern VCHIQ_STATUS_T vchiq_release_service(VCHIQ_SERVICE_HANDLE_T service);
10999 +extern VCHIQ_STATUS_T vchiq_queue_message(VCHIQ_SERVICE_HANDLE_T service,
11000 + const VCHIQ_ELEMENT_T *elements, unsigned int count);
11001 +extern void vchiq_release_message(VCHIQ_SERVICE_HANDLE_T service,
11002 + VCHIQ_HEADER_T *header);
11003 +extern VCHIQ_STATUS_T vchiq_queue_bulk_transmit(VCHIQ_SERVICE_HANDLE_T service,
11004 + const void *data, unsigned int size, void *userdata);
11005 +extern VCHIQ_STATUS_T vchiq_queue_bulk_receive(VCHIQ_SERVICE_HANDLE_T service,
11006 + void *data, unsigned int size, void *userdata);
11007 +extern VCHIQ_STATUS_T vchiq_queue_bulk_transmit_handle(
11008 + VCHIQ_SERVICE_HANDLE_T service, VCHI_MEM_HANDLE_T handle,
11009 + const void *offset, unsigned int size, void *userdata);
11010 +extern VCHIQ_STATUS_T vchiq_queue_bulk_receive_handle(
11011 + VCHIQ_SERVICE_HANDLE_T service, VCHI_MEM_HANDLE_T handle,
11012 + void *offset, unsigned int size, void *userdata);
11013 +extern VCHIQ_STATUS_T vchiq_bulk_transmit(VCHIQ_SERVICE_HANDLE_T service,
11014 + const void *data, unsigned int size, void *userdata,
11015 + VCHIQ_BULK_MODE_T mode);
11016 +extern VCHIQ_STATUS_T vchiq_bulk_receive(VCHIQ_SERVICE_HANDLE_T service,
11017 + void *data, unsigned int size, void *userdata,
11018 + VCHIQ_BULK_MODE_T mode);
11019 +extern VCHIQ_STATUS_T vchiq_bulk_transmit_handle(VCHIQ_SERVICE_HANDLE_T service,
11020 + VCHI_MEM_HANDLE_T handle, const void *offset, unsigned int size,
11021 + void *userdata, VCHIQ_BULK_MODE_T mode);
11022 +extern VCHIQ_STATUS_T vchiq_bulk_receive_handle(VCHIQ_SERVICE_HANDLE_T service,
11023 + VCHI_MEM_HANDLE_T handle, void *offset, unsigned int size,
11024 + void *userdata, VCHIQ_BULK_MODE_T mode);
11025 +extern int vchiq_get_client_id(VCHIQ_SERVICE_HANDLE_T service);
11026 +extern void *vchiq_get_service_userdata(VCHIQ_SERVICE_HANDLE_T service);
11027 +extern int vchiq_get_service_fourcc(VCHIQ_SERVICE_HANDLE_T service);
11028 +extern VCHIQ_STATUS_T vchiq_get_config(VCHIQ_INSTANCE_T instance,
11029 + int config_size, VCHIQ_CONFIG_T *pconfig);
11030 +extern VCHIQ_STATUS_T vchiq_set_service_option(VCHIQ_SERVICE_HANDLE_T service,
11031 + VCHIQ_SERVICE_OPTION_T option, int value);
11033 +extern VCHIQ_STATUS_T vchiq_remote_use(VCHIQ_INSTANCE_T instance,
11034 + VCHIQ_REMOTE_USE_CALLBACK_T callback, void *cb_arg);
11035 +extern VCHIQ_STATUS_T vchiq_remote_release(VCHIQ_INSTANCE_T instance);
11037 +extern VCHIQ_STATUS_T vchiq_dump_phys_mem(VCHIQ_SERVICE_HANDLE_T service,
11038 + void *ptr, size_t num_bytes);
11040 +extern VCHIQ_STATUS_T vchiq_get_peer_version(VCHIQ_SERVICE_HANDLE_T handle,
11041 + short *peer_version);
11043 +#endif /* VCHIQ_IF_H */
11044 diff --git a/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_ioctl.h b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_ioctl.h
11045 new file mode 100644
11046 index 0000000..6137ae9
11048 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_ioctl.h
11051 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
11053 + * Redistribution and use in source and binary forms, with or without
11054 + * modification, are permitted provided that the following conditions
11056 + * 1. Redistributions of source code must retain the above copyright
11057 + * notice, this list of conditions, and the following disclaimer,
11058 + * without modification.
11059 + * 2. Redistributions in binary form must reproduce the above copyright
11060 + * notice, this list of conditions and the following disclaimer in the
11061 + * documentation and/or other materials provided with the distribution.
11062 + * 3. The names of the above-listed copyright holders may not be used
11063 + * to endorse or promote products derived from this software without
11064 + * specific prior written permission.
11066 + * ALTERNATIVELY, this software may be distributed under the terms of the
11067 + * GNU General Public License ("GPL") version 2, as published by the Free
11068 + * Software Foundation.
11070 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
11071 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
11072 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
11073 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
11074 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
11075 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
11076 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
11077 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
11078 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
11079 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
11080 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
11083 +#ifndef VCHIQ_IOCTLS_H
11084 +#define VCHIQ_IOCTLS_H
11086 +#include <linux/ioctl.h>
11087 +#include "vchiq_if.h"
11089 +#define VCHIQ_IOC_MAGIC 0xc4
11090 +#define VCHIQ_INVALID_HANDLE (~0)
11093 + VCHIQ_SERVICE_PARAMS_T params;
11096 + unsigned int handle; /* OUT */
11097 +} VCHIQ_CREATE_SERVICE_T;
11100 + unsigned int handle;
11101 + unsigned int count;
11102 + const VCHIQ_ELEMENT_T *elements;
11103 +} VCHIQ_QUEUE_MESSAGE_T;
11106 + unsigned int handle;
11108 + unsigned int size;
11110 + VCHIQ_BULK_MODE_T mode;
11111 +} VCHIQ_QUEUE_BULK_TRANSFER_T;
11114 + VCHIQ_REASON_T reason;
11115 + VCHIQ_HEADER_T *header;
11116 + void *service_userdata;
11117 + void *bulk_userdata;
11118 +} VCHIQ_COMPLETION_DATA_T;
11121 + unsigned int count;
11122 + VCHIQ_COMPLETION_DATA_T *buf;
11123 + unsigned int msgbufsize;
11124 + unsigned int msgbufcount; /* IN/OUT */
11126 +} VCHIQ_AWAIT_COMPLETION_T;
11129 + unsigned int handle;
11131 + unsigned int bufsize;
11133 +} VCHIQ_DEQUEUE_MESSAGE_T;
11136 + unsigned int config_size;
11137 + VCHIQ_CONFIG_T *pconfig;
11138 +} VCHIQ_GET_CONFIG_T;
11141 + unsigned int handle;
11142 + VCHIQ_SERVICE_OPTION_T option;
11144 +} VCHIQ_SET_SERVICE_OPTION_T;
11148 + size_t num_bytes;
11149 +} VCHIQ_DUMP_MEM_T;
11151 +#define VCHIQ_IOC_CONNECT _IO(VCHIQ_IOC_MAGIC, 0)
11152 +#define VCHIQ_IOC_SHUTDOWN _IO(VCHIQ_IOC_MAGIC, 1)
11153 +#define VCHIQ_IOC_CREATE_SERVICE \
11154 + _IOWR(VCHIQ_IOC_MAGIC, 2, VCHIQ_CREATE_SERVICE_T)
11155 +#define VCHIQ_IOC_REMOVE_SERVICE _IO(VCHIQ_IOC_MAGIC, 3)
11156 +#define VCHIQ_IOC_QUEUE_MESSAGE \
11157 + _IOW(VCHIQ_IOC_MAGIC, 4, VCHIQ_QUEUE_MESSAGE_T)
11158 +#define VCHIQ_IOC_QUEUE_BULK_TRANSMIT \
11159 + _IOWR(VCHIQ_IOC_MAGIC, 5, VCHIQ_QUEUE_BULK_TRANSFER_T)
11160 +#define VCHIQ_IOC_QUEUE_BULK_RECEIVE \
11161 + _IOWR(VCHIQ_IOC_MAGIC, 6, VCHIQ_QUEUE_BULK_TRANSFER_T)
11162 +#define VCHIQ_IOC_AWAIT_COMPLETION \
11163 + _IOWR(VCHIQ_IOC_MAGIC, 7, VCHIQ_AWAIT_COMPLETION_T)
11164 +#define VCHIQ_IOC_DEQUEUE_MESSAGE \
11165 + _IOWR(VCHIQ_IOC_MAGIC, 8, VCHIQ_DEQUEUE_MESSAGE_T)
11166 +#define VCHIQ_IOC_GET_CLIENT_ID _IO(VCHIQ_IOC_MAGIC, 9)
11167 +#define VCHIQ_IOC_GET_CONFIG \
11168 + _IOWR(VCHIQ_IOC_MAGIC, 10, VCHIQ_GET_CONFIG_T)
11169 +#define VCHIQ_IOC_CLOSE_SERVICE _IO(VCHIQ_IOC_MAGIC, 11)
11170 +#define VCHIQ_IOC_USE_SERVICE _IO(VCHIQ_IOC_MAGIC, 12)
11171 +#define VCHIQ_IOC_RELEASE_SERVICE _IO(VCHIQ_IOC_MAGIC, 13)
11172 +#define VCHIQ_IOC_SET_SERVICE_OPTION \
11173 + _IOW(VCHIQ_IOC_MAGIC, 14, VCHIQ_SET_SERVICE_OPTION_T)
11174 +#define VCHIQ_IOC_DUMP_PHYS_MEM \
11175 + _IOW(VCHIQ_IOC_MAGIC, 15, VCHIQ_DUMP_MEM_T)
11176 +#define VCHIQ_IOC_LIB_VERSION _IO(VCHIQ_IOC_MAGIC, 16)
11177 +#define VCHIQ_IOC_CLOSE_DELIVERED _IO(VCHIQ_IOC_MAGIC, 17)
11178 +#define VCHIQ_IOC_MAX 17
11181 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
11182 new file mode 100644
11183 index 0000000..be9735f
11185 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_kern_lib.c
11188 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
11190 + * Redistribution and use in source and binary forms, with or without
11191 + * modification, are permitted provided that the following conditions
11193 + * 1. Redistributions of source code must retain the above copyright
11194 + * notice, this list of conditions, and the following disclaimer,
11195 + * without modification.
11196 + * 2. Redistributions in binary form must reproduce the above copyright
11197 + * notice, this list of conditions and the following disclaimer in the
11198 + * documentation and/or other materials provided with the distribution.
11199 + * 3. The names of the above-listed copyright holders may not be used
11200 + * to endorse or promote products derived from this software without
11201 + * specific prior written permission.
11203 + * ALTERNATIVELY, this software may be distributed under the terms of the
11204 + * GNU General Public License ("GPL") version 2, as published by the Free
11205 + * Software Foundation.
11207 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
11208 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
11209 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
11210 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
11211 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
11212 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
11213 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
11214 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
11215 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
11216 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
11217 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
11220 +/* ---- Include Files ---------------------------------------------------- */
11222 +#include <linux/kernel.h>
11223 +#include <linux/module.h>
11224 +#include <linux/mutex.h>
11226 +#include "vchiq_core.h"
11227 +#include "vchiq_arm.h"
11229 +/* ---- Public Variables ------------------------------------------------- */
11231 +/* ---- Private Constants and Types -------------------------------------- */
11233 +struct bulk_waiter_node {
11234 + struct bulk_waiter bulk_waiter;
11236 + struct list_head list;
11239 +struct vchiq_instance_struct {
11240 + VCHIQ_STATE_T *state;
11244 + struct list_head bulk_waiter_list;
11245 + struct mutex bulk_waiter_list_mutex;
11248 +static VCHIQ_STATUS_T
11249 +vchiq_blocking_bulk_transfer(VCHIQ_SERVICE_HANDLE_T handle, void *data,
11250 + unsigned int size, VCHIQ_BULK_DIR_T dir);
11252 +/****************************************************************************
11254 +* vchiq_initialise
11256 +***************************************************************************/
11257 +#define VCHIQ_INIT_RETRIES 10
11258 +VCHIQ_STATUS_T vchiq_initialise(VCHIQ_INSTANCE_T *instanceOut)
11260 + VCHIQ_STATUS_T status = VCHIQ_ERROR;
11261 + VCHIQ_STATE_T *state;
11262 + VCHIQ_INSTANCE_T instance = NULL;
11265 + vchiq_log_trace(vchiq_core_log_level, "%s called", __func__);
11267 + /* VideoCore may not be ready due to boot up timing.
11268 + It may never be ready if kernel and firmware are mismatched, so don't block forever. */
11269 + for (i=0; i<VCHIQ_INIT_RETRIES; i++) {
11270 + state = vchiq_get_state();
11275 + if (i==VCHIQ_INIT_RETRIES) {
11276 + vchiq_log_error(vchiq_core_log_level,
11277 + "%s: videocore not initialized\n", __func__);
11279 + } else if (i>0) {
11280 + vchiq_log_warning(vchiq_core_log_level,
11281 + "%s: videocore initialized after %d retries\n", __func__, i);
11284 + instance = kzalloc(sizeof(*instance), GFP_KERNEL);
11286 + vchiq_log_error(vchiq_core_log_level,
11287 + "%s: error allocating vchiq instance\n", __func__);
11291 + instance->connected = 0;
11292 + instance->state = state;
11293 + mutex_init(&instance->bulk_waiter_list_mutex);
11294 + INIT_LIST_HEAD(&instance->bulk_waiter_list);
11296 + *instanceOut = instance;
11298 + status = VCHIQ_SUCCESS;
11301 + vchiq_log_trace(vchiq_core_log_level,
11302 + "%s(%p): returning %d", __func__, instance, status);
11306 +EXPORT_SYMBOL(vchiq_initialise);
11308 +/****************************************************************************
11312 +***************************************************************************/
11314 +VCHIQ_STATUS_T vchiq_shutdown(VCHIQ_INSTANCE_T instance)
11316 + VCHIQ_STATUS_T status;
11317 + VCHIQ_STATE_T *state = instance->state;
11319 + vchiq_log_trace(vchiq_core_log_level,
11320 + "%s(%p) called", __func__, instance);
11322 + if (mutex_lock_interruptible(&state->mutex) != 0)
11323 + return VCHIQ_RETRY;
11325 + /* Remove all services */
11326 + status = vchiq_shutdown_internal(state, instance);
11328 + mutex_unlock(&state->mutex);
11330 + vchiq_log_trace(vchiq_core_log_level,
11331 + "%s(%p): returning %d", __func__, instance, status);
11333 + if (status == VCHIQ_SUCCESS) {
11334 + struct list_head *pos, *next;
11335 + list_for_each_safe(pos, next,
11336 + &instance->bulk_waiter_list) {
11337 + struct bulk_waiter_node *waiter;
11338 + waiter = list_entry(pos,
11339 + struct bulk_waiter_node,
11342 + vchiq_log_info(vchiq_arm_log_level,
11343 + "bulk_waiter - cleaned up %x "
11345 + (unsigned int)waiter, waiter->pid);
11353 +EXPORT_SYMBOL(vchiq_shutdown);
11355 +/****************************************************************************
11357 +* vchiq_is_connected
11359 +***************************************************************************/
11361 +int vchiq_is_connected(VCHIQ_INSTANCE_T instance)
11363 + return instance->connected;
11366 +/****************************************************************************
11370 +***************************************************************************/
11372 +VCHIQ_STATUS_T vchiq_connect(VCHIQ_INSTANCE_T instance)
11374 + VCHIQ_STATUS_T status;
11375 + VCHIQ_STATE_T *state = instance->state;
11377 + vchiq_log_trace(vchiq_core_log_level,
11378 + "%s(%p) called", __func__, instance);
11380 + if (mutex_lock_interruptible(&state->mutex) != 0) {
11381 + vchiq_log_trace(vchiq_core_log_level,
11382 + "%s: call to mutex_lock failed", __func__);
11383 + status = VCHIQ_RETRY;
11386 + status = vchiq_connect_internal(state, instance);
11388 + if (status == VCHIQ_SUCCESS)
11389 + instance->connected = 1;
11391 + mutex_unlock(&state->mutex);
11394 + vchiq_log_trace(vchiq_core_log_level,
11395 + "%s(%p): returning %d", __func__, instance, status);
11399 +EXPORT_SYMBOL(vchiq_connect);
11401 +/****************************************************************************
11403 +* vchiq_add_service
11405 +***************************************************************************/
11407 +VCHIQ_STATUS_T vchiq_add_service(
11408 + VCHIQ_INSTANCE_T instance,
11409 + const VCHIQ_SERVICE_PARAMS_T *params,
11410 + VCHIQ_SERVICE_HANDLE_T *phandle)
11412 + VCHIQ_STATUS_T status;
11413 + VCHIQ_STATE_T *state = instance->state;
11414 + VCHIQ_SERVICE_T *service = NULL;
11417 + vchiq_log_trace(vchiq_core_log_level,
11418 + "%s(%p) called", __func__, instance);
11420 + *phandle = VCHIQ_SERVICE_HANDLE_INVALID;
11422 + srvstate = vchiq_is_connected(instance)
11423 + ? VCHIQ_SRVSTATE_LISTENING
11424 + : VCHIQ_SRVSTATE_HIDDEN;
11426 + service = vchiq_add_service_internal(
11434 + *phandle = service->handle;
11435 + status = VCHIQ_SUCCESS;
11437 + status = VCHIQ_ERROR;
11439 + vchiq_log_trace(vchiq_core_log_level,
11440 + "%s(%p): returning %d", __func__, instance, status);
11444 +EXPORT_SYMBOL(vchiq_add_service);
11446 +/****************************************************************************
11448 +* vchiq_open_service
11450 +***************************************************************************/
11452 +VCHIQ_STATUS_T vchiq_open_service(
11453 + VCHIQ_INSTANCE_T instance,
11454 + const VCHIQ_SERVICE_PARAMS_T *params,
11455 + VCHIQ_SERVICE_HANDLE_T *phandle)
11457 + VCHIQ_STATUS_T status = VCHIQ_ERROR;
11458 + VCHIQ_STATE_T *state = instance->state;
11459 + VCHIQ_SERVICE_T *service = NULL;
11461 + vchiq_log_trace(vchiq_core_log_level,
11462 + "%s(%p) called", __func__, instance);
11464 + *phandle = VCHIQ_SERVICE_HANDLE_INVALID;
11466 + if (!vchiq_is_connected(instance))
11469 + service = vchiq_add_service_internal(state,
11471 + VCHIQ_SRVSTATE_OPENING,
11476 + status = vchiq_open_service_internal(service, current->pid);
11477 + if (status == VCHIQ_SUCCESS)
11478 + *phandle = service->handle;
11480 + vchiq_remove_service(service->handle);
11484 + vchiq_log_trace(vchiq_core_log_level,
11485 + "%s(%p): returning %d", __func__, instance, status);
11489 +EXPORT_SYMBOL(vchiq_open_service);
11492 +vchiq_queue_bulk_transmit(VCHIQ_SERVICE_HANDLE_T handle,
11493 + const void *data, unsigned int size, void *userdata)
11495 + return vchiq_bulk_transfer(handle,
11496 + VCHI_MEM_HANDLE_INVALID, (void *)data, size, userdata,
11497 + VCHIQ_BULK_MODE_CALLBACK, VCHIQ_BULK_TRANSMIT);
11499 +EXPORT_SYMBOL(vchiq_queue_bulk_transmit);
11502 +vchiq_queue_bulk_receive(VCHIQ_SERVICE_HANDLE_T handle, void *data,
11503 + unsigned int size, void *userdata)
11505 + return vchiq_bulk_transfer(handle,
11506 + VCHI_MEM_HANDLE_INVALID, data, size, userdata,
11507 + VCHIQ_BULK_MODE_CALLBACK, VCHIQ_BULK_RECEIVE);
11509 +EXPORT_SYMBOL(vchiq_queue_bulk_receive);
11512 +vchiq_bulk_transmit(VCHIQ_SERVICE_HANDLE_T handle, const void *data,
11513 + unsigned int size, void *userdata, VCHIQ_BULK_MODE_T mode)
11515 + VCHIQ_STATUS_T status;
11518 + case VCHIQ_BULK_MODE_NOCALLBACK:
11519 + case VCHIQ_BULK_MODE_CALLBACK:
11520 + status = vchiq_bulk_transfer(handle,
11521 + VCHI_MEM_HANDLE_INVALID, (void *)data, size, userdata,
11522 + mode, VCHIQ_BULK_TRANSMIT);
11524 + case VCHIQ_BULK_MODE_BLOCKING:
11525 + status = vchiq_blocking_bulk_transfer(handle,
11526 + (void *)data, size, VCHIQ_BULK_TRANSMIT);
11529 + return VCHIQ_ERROR;
11534 +EXPORT_SYMBOL(vchiq_bulk_transmit);
11537 +vchiq_bulk_receive(VCHIQ_SERVICE_HANDLE_T handle, void *data,
11538 + unsigned int size, void *userdata, VCHIQ_BULK_MODE_T mode)
11540 + VCHIQ_STATUS_T status;
11543 + case VCHIQ_BULK_MODE_NOCALLBACK:
11544 + case VCHIQ_BULK_MODE_CALLBACK:
11545 + status = vchiq_bulk_transfer(handle,
11546 + VCHI_MEM_HANDLE_INVALID, data, size, userdata,
11547 + mode, VCHIQ_BULK_RECEIVE);
11549 + case VCHIQ_BULK_MODE_BLOCKING:
11550 + status = vchiq_blocking_bulk_transfer(handle,
11551 + (void *)data, size, VCHIQ_BULK_RECEIVE);
11554 + return VCHIQ_ERROR;
11559 +EXPORT_SYMBOL(vchiq_bulk_receive);
11561 +static VCHIQ_STATUS_T
11562 +vchiq_blocking_bulk_transfer(VCHIQ_SERVICE_HANDLE_T handle, void *data,
11563 + unsigned int size, VCHIQ_BULK_DIR_T dir)
11565 + VCHIQ_INSTANCE_T instance;
11566 + VCHIQ_SERVICE_T *service;
11567 + VCHIQ_STATUS_T status;
11568 + struct bulk_waiter_node *waiter = NULL;
11569 + struct list_head *pos;
11571 + service = find_service_by_handle(handle);
11573 + return VCHIQ_ERROR;
11575 + instance = service->instance;
11577 + unlock_service(service);
11579 + mutex_lock(&instance->bulk_waiter_list_mutex);
11580 + list_for_each(pos, &instance->bulk_waiter_list) {
11581 + if (list_entry(pos, struct bulk_waiter_node,
11582 + list)->pid == current->pid) {
11583 + waiter = list_entry(pos,
11584 + struct bulk_waiter_node,
11590 + mutex_unlock(&instance->bulk_waiter_list_mutex);
11593 + VCHIQ_BULK_T *bulk = waiter->bulk_waiter.bulk;
11595 + /* This thread has an outstanding bulk transfer. */
11596 + if ((bulk->data != data) ||
11597 + (bulk->size != size)) {
11598 + /* This is not a retry of the previous one.
11599 + ** Cancel the signal when the transfer
11601 + spin_lock(&bulk_waiter_spinlock);
11602 + bulk->userdata = NULL;
11603 + spin_unlock(&bulk_waiter_spinlock);
11609 + waiter = kzalloc(sizeof(struct bulk_waiter_node), GFP_KERNEL);
11611 + vchiq_log_error(vchiq_core_log_level,
11612 + "%s - out of memory", __func__);
11613 + return VCHIQ_ERROR;
11617 + status = vchiq_bulk_transfer(handle, VCHI_MEM_HANDLE_INVALID,
11618 + data, size, &waiter->bulk_waiter, VCHIQ_BULK_MODE_BLOCKING,
11620 + if ((status != VCHIQ_RETRY) || fatal_signal_pending(current) ||
11621 + !waiter->bulk_waiter.bulk) {
11622 + VCHIQ_BULK_T *bulk = waiter->bulk_waiter.bulk;
11624 + /* Cancel the signal when the transfer
11626 + spin_lock(&bulk_waiter_spinlock);
11627 + bulk->userdata = NULL;
11628 + spin_unlock(&bulk_waiter_spinlock);
11632 + waiter->pid = current->pid;
11633 + mutex_lock(&instance->bulk_waiter_list_mutex);
11634 + list_add(&waiter->list, &instance->bulk_waiter_list);
11635 + mutex_unlock(&instance->bulk_waiter_list_mutex);
11636 + vchiq_log_info(vchiq_arm_log_level,
11637 + "saved bulk_waiter %x for pid %d",
11638 + (unsigned int)waiter, current->pid);
11643 diff --git a/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_memdrv.h b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_memdrv.h
11644 new file mode 100644
11645 index 0000000..d02e776
11647 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_memdrv.h
11650 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
11652 + * Redistribution and use in source and binary forms, with or without
11653 + * modification, are permitted provided that the following conditions
11655 + * 1. Redistributions of source code must retain the above copyright
11656 + * notice, this list of conditions, and the following disclaimer,
11657 + * without modification.
11658 + * 2. Redistributions in binary form must reproduce the above copyright
11659 + * notice, this list of conditions and the following disclaimer in the
11660 + * documentation and/or other materials provided with the distribution.
11661 + * 3. The names of the above-listed copyright holders may not be used
11662 + * to endorse or promote products derived from this software without
11663 + * specific prior written permission.
11665 + * ALTERNATIVELY, this software may be distributed under the terms of the
11666 + * GNU General Public License ("GPL") version 2, as published by the Free
11667 + * Software Foundation.
11669 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
11670 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
11671 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
11672 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
11673 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
11674 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
11675 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
11676 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
11677 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
11678 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
11679 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
11682 +#ifndef VCHIQ_MEMDRV_H
11683 +#define VCHIQ_MEMDRV_H
11685 +/* ---- Include Files ----------------------------------------------------- */
11687 +#include <linux/kernel.h>
11688 +#include "vchiq_if.h"
11690 +/* ---- Constants and Types ---------------------------------------------- */
11693 + void *armSharedMemVirt;
11694 + dma_addr_t armSharedMemPhys;
11695 + size_t armSharedMemSize;
11697 + void *vcSharedMemVirt;
11698 + dma_addr_t vcSharedMemPhys;
11699 + size_t vcSharedMemSize;
11700 +} VCHIQ_SHARED_MEM_INFO_T;
11702 +/* ---- Variable Externs ------------------------------------------------- */
11704 +/* ---- Function Prototypes ---------------------------------------------- */
11706 +void vchiq_get_shared_mem_info(VCHIQ_SHARED_MEM_INFO_T *info);
11708 +VCHIQ_STATUS_T vchiq_memdrv_initialise(void);
11710 +VCHIQ_STATUS_T vchiq_userdrv_create_instance(
11711 + const VCHIQ_PLATFORM_DATA_T * platform_data);
11713 +VCHIQ_STATUS_T vchiq_userdrv_suspend(
11714 + const VCHIQ_PLATFORM_DATA_T * platform_data);
11716 +VCHIQ_STATUS_T vchiq_userdrv_resume(
11717 + const VCHIQ_PLATFORM_DATA_T * platform_data);
11720 diff --git a/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_pagelist.h b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_pagelist.h
11721 new file mode 100644
11722 index 0000000..54a3ece
11724 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_pagelist.h
11727 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
11729 + * Redistribution and use in source and binary forms, with or without
11730 + * modification, are permitted provided that the following conditions
11732 + * 1. Redistributions of source code must retain the above copyright
11733 + * notice, this list of conditions, and the following disclaimer,
11734 + * without modification.
11735 + * 2. Redistributions in binary form must reproduce the above copyright
11736 + * notice, this list of conditions and the following disclaimer in the
11737 + * documentation and/or other materials provided with the distribution.
11738 + * 3. The names of the above-listed copyright holders may not be used
11739 + * to endorse or promote products derived from this software without
11740 + * specific prior written permission.
11742 + * ALTERNATIVELY, this software may be distributed under the terms of the
11743 + * GNU General Public License ("GPL") version 2, as published by the Free
11744 + * Software Foundation.
11746 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
11747 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
11748 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
11749 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
11750 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
11751 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
11752 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
11753 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
11754 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
11755 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
11756 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
11759 +#ifndef VCHIQ_PAGELIST_H
11760 +#define VCHIQ_PAGELIST_H
11763 +#define PAGE_SIZE 4096
11765 +#define CACHE_LINE_SIZE 32
11766 +#define PAGELIST_WRITE 0
11767 +#define PAGELIST_READ 1
11768 +#define PAGELIST_READ_WITH_FRAGMENTS 2
11770 +typedef struct pagelist_struct {
11771 + unsigned long length;
11772 + unsigned short type;
11773 + unsigned short offset;
11774 + unsigned long addrs[1]; /* N.B. 12 LSBs hold the number of following
11775 + pages at consecutive addresses. */
11778 +typedef struct fragments_struct {
11779 + char headbuf[CACHE_LINE_SIZE];
11780 + char tailbuf[CACHE_LINE_SIZE];
11783 +#endif /* VCHIQ_PAGELIST_H */
11784 diff --git a/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_shim.c b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_shim.c
11785 new file mode 100644
11786 index 0000000..72eacdaf
11788 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_shim.c
11791 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
11793 + * Redistribution and use in source and binary forms, with or without
11794 + * modification, are permitted provided that the following conditions
11796 + * 1. Redistributions of source code must retain the above copyright
11797 + * notice, this list of conditions, and the following disclaimer,
11798 + * without modification.
11799 + * 2. Redistributions in binary form must reproduce the above copyright
11800 + * notice, this list of conditions and the following disclaimer in the
11801 + * documentation and/or other materials provided with the distribution.
11802 + * 3. The names of the above-listed copyright holders may not be used
11803 + * to endorse or promote products derived from this software without
11804 + * specific prior written permission.
11806 + * ALTERNATIVELY, this software may be distributed under the terms of the
11807 + * GNU General Public License ("GPL") version 2, as published by the Free
11808 + * Software Foundation.
11810 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
11811 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
11812 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
11813 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
11814 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
11815 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
11816 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
11817 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
11818 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
11819 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
11820 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
11822 +#include <linux/module.h>
11823 +#include <linux/types.h>
11825 +#include "interface/vchi/vchi.h"
11826 +#include "vchiq.h"
11827 +#include "vchiq_core.h"
11829 +#include "vchiq_util.h"
11831 +#include <stddef.h>
11833 +#define vchiq_status_to_vchi(status) ((int32_t)status)
11836 + VCHIQ_SERVICE_HANDLE_T handle;
11838 + VCHIU_QUEUE_T queue;
11840 + VCHI_CALLBACK_T callback;
11841 + void *callback_param;
11844 +/* ----------------------------------------------------------------------
11845 + * return pointer to the mphi message driver function table
11846 + * -------------------------------------------------------------------- */
11847 +const VCHI_MESSAGE_DRIVER_T *
11848 +vchi_mphi_message_driver_func_table(void)
11853 +/* ----------------------------------------------------------------------
11854 + * return a pointer to the 'single' connection driver fops
11855 + * -------------------------------------------------------------------- */
11856 +const VCHI_CONNECTION_API_T *
11857 +single_get_func_table(void)
11862 +VCHI_CONNECTION_T *vchi_create_connection(
11863 + const VCHI_CONNECTION_API_T *function_table,
11864 + const VCHI_MESSAGE_DRIVER_T *low_level)
11866 + (void)function_table;
11871 +/***********************************************************
11872 + * Name: vchi_msg_peek
11874 + * Arguments: const VCHI_SERVICE_HANDLE_T handle,
11876 + * uint32_t *msg_size,
11879 + * VCHI_FLAGS_T flags
11881 + * Description: Routine to return a pointer to the current message (to allow in
11882 + * place processing). The message can be removed using
11883 + * vchi_msg_remove when you're finished
11885 + * Returns: int32_t - success == 0
11887 + ***********************************************************/
11888 +int32_t vchi_msg_peek(VCHI_SERVICE_HANDLE_T handle,
11890 + uint32_t *msg_size,
11891 + VCHI_FLAGS_T flags)
11893 + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
11894 + VCHIQ_HEADER_T *header;
11896 + WARN_ON((flags != VCHI_FLAGS_NONE) &&
11897 + (flags != VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE));
11899 + if (flags == VCHI_FLAGS_NONE)
11900 + if (vchiu_queue_is_empty(&service->queue))
11903 + header = vchiu_queue_peek(&service->queue);
11905 + *data = header->data;
11906 + *msg_size = header->size;
11910 +EXPORT_SYMBOL(vchi_msg_peek);
11912 +/***********************************************************
11913 + * Name: vchi_msg_remove
11915 + * Arguments: const VCHI_SERVICE_HANDLE_T handle,
11917 + * Description: Routine to remove a message (after it has been read with
11920 + * Returns: int32_t - success == 0
11922 + ***********************************************************/
11923 +int32_t vchi_msg_remove(VCHI_SERVICE_HANDLE_T handle)
11925 + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
11926 + VCHIQ_HEADER_T *header;
11928 + header = vchiu_queue_pop(&service->queue);
11930 + vchiq_release_message(service->handle, header);
11934 +EXPORT_SYMBOL(vchi_msg_remove);
11936 +/***********************************************************
11937 + * Name: vchi_msg_queue
11939 + * Arguments: VCHI_SERVICE_HANDLE_T handle,
11940 + * const void *data,
11941 + * uint32_t data_size,
11942 + * VCHI_FLAGS_T flags,
11943 + * void *msg_handle,
11945 + * Description: Thin wrapper to queue a message onto a connection
11947 + * Returns: int32_t - success == 0
11949 + ***********************************************************/
11950 +int32_t vchi_msg_queue(VCHI_SERVICE_HANDLE_T handle,
11951 + const void *data,
11952 + uint32_t data_size,
11953 + VCHI_FLAGS_T flags,
11954 + void *msg_handle)
11956 + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
11957 + VCHIQ_ELEMENT_T element = {data, data_size};
11958 + VCHIQ_STATUS_T status;
11960 + (void)msg_handle;
11962 + WARN_ON(flags != VCHI_FLAGS_BLOCK_UNTIL_QUEUED);
11964 + status = vchiq_queue_message(service->handle, &element, 1);
11966 + /* vchiq_queue_message() may return VCHIQ_RETRY, so we need to
11967 + ** implement a retry mechanism since this function is supposed
11968 + ** to block until queued
11970 + while (status == VCHIQ_RETRY) {
11972 + status = vchiq_queue_message(service->handle, &element, 1);
11975 + return vchiq_status_to_vchi(status);
11977 +EXPORT_SYMBOL(vchi_msg_queue);
11979 +/***********************************************************
11980 + * Name: vchi_bulk_queue_receive
11982 + * Arguments: VCHI_BULK_HANDLE_T handle,
11983 + * void *data_dst,
11984 + * const uint32_t data_size,
11985 + * VCHI_FLAGS_T flags
11986 + * void *bulk_handle
11988 + * Description: Routine to setup a rcv buffer
11990 + * Returns: int32_t - success == 0
11992 + ***********************************************************/
11993 +int32_t vchi_bulk_queue_receive(VCHI_SERVICE_HANDLE_T handle,
11995 + uint32_t data_size,
11996 + VCHI_FLAGS_T flags,
11997 + void *bulk_handle)
11999 + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
12000 + VCHIQ_BULK_MODE_T mode;
12001 + VCHIQ_STATUS_T status;
12003 + switch ((int)flags) {
12004 + case VCHI_FLAGS_CALLBACK_WHEN_OP_COMPLETE
12005 + | VCHI_FLAGS_BLOCK_UNTIL_QUEUED:
12006 + WARN_ON(!service->callback);
12007 + mode = VCHIQ_BULK_MODE_CALLBACK;
12009 + case VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE:
12010 + mode = VCHIQ_BULK_MODE_BLOCKING;
12012 + case VCHI_FLAGS_BLOCK_UNTIL_QUEUED:
12013 + case VCHI_FLAGS_NONE:
12014 + mode = VCHIQ_BULK_MODE_NOCALLBACK;
12017 + WARN(1, "unsupported message\n");
12018 + return vchiq_status_to_vchi(VCHIQ_ERROR);
12021 + status = vchiq_bulk_receive(service->handle, data_dst, data_size,
12022 + bulk_handle, mode);
12024 + /* vchiq_bulk_receive() may return VCHIQ_RETRY, so we need to
12025 + ** implement a retry mechanism since this function is supposed
12026 + ** to block until queued
12028 + while (status == VCHIQ_RETRY) {
12030 + status = vchiq_bulk_receive(service->handle, data_dst,
12031 + data_size, bulk_handle, mode);
12034 + return vchiq_status_to_vchi(status);
12036 +EXPORT_SYMBOL(vchi_bulk_queue_receive);
12038 +/***********************************************************
12039 + * Name: vchi_bulk_queue_transmit
12041 + * Arguments: VCHI_BULK_HANDLE_T handle,
12042 + * const void *data_src,
12043 + * uint32_t data_size,
12044 + * VCHI_FLAGS_T flags,
12045 + * void *bulk_handle
12047 + * Description: Routine to transmit some data
12049 + * Returns: int32_t - success == 0
12051 + ***********************************************************/
12052 +int32_t vchi_bulk_queue_transmit(VCHI_SERVICE_HANDLE_T handle,
12053 + const void *data_src,
12054 + uint32_t data_size,
12055 + VCHI_FLAGS_T flags,
12056 + void *bulk_handle)
12058 + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
12059 + VCHIQ_BULK_MODE_T mode;
12060 + VCHIQ_STATUS_T status;
12062 + switch ((int)flags) {
12063 + case VCHI_FLAGS_CALLBACK_WHEN_OP_COMPLETE
12064 + | VCHI_FLAGS_BLOCK_UNTIL_QUEUED:
12065 + WARN_ON(!service->callback);
12066 + mode = VCHIQ_BULK_MODE_CALLBACK;
12068 + case VCHI_FLAGS_BLOCK_UNTIL_DATA_READ:
12069 + case VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE:
12070 + mode = VCHIQ_BULK_MODE_BLOCKING;
12072 + case VCHI_FLAGS_BLOCK_UNTIL_QUEUED:
12073 + case VCHI_FLAGS_NONE:
12074 + mode = VCHIQ_BULK_MODE_NOCALLBACK;
12077 + WARN(1, "unsupported message\n");
12078 + return vchiq_status_to_vchi(VCHIQ_ERROR);
12081 + status = vchiq_bulk_transmit(service->handle, data_src, data_size,
12082 + bulk_handle, mode);
12084 + /* vchiq_bulk_transmit() may return VCHIQ_RETRY, so we need to
12085 + ** implement a retry mechanism since this function is supposed
12086 + ** to block until queued
12088 + while (status == VCHIQ_RETRY) {
12090 + status = vchiq_bulk_transmit(service->handle, data_src,
12091 + data_size, bulk_handle, mode);
12094 + return vchiq_status_to_vchi(status);
12096 +EXPORT_SYMBOL(vchi_bulk_queue_transmit);
12098 +/***********************************************************
12099 + * Name: vchi_msg_dequeue
12101 + * Arguments: VCHI_SERVICE_HANDLE_T handle,
12103 + * uint32_t max_data_size_to_read,
12104 + * uint32_t *actual_msg_size
12105 + * VCHI_FLAGS_T flags
12107 + * Description: Routine to dequeue a message into the supplied buffer
12109 + * Returns: int32_t - success == 0
12111 + ***********************************************************/
12112 +int32_t vchi_msg_dequeue(VCHI_SERVICE_HANDLE_T handle,
12114 + uint32_t max_data_size_to_read,
12115 + uint32_t *actual_msg_size,
12116 + VCHI_FLAGS_T flags)
12118 + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
12119 + VCHIQ_HEADER_T *header;
12121 + WARN_ON((flags != VCHI_FLAGS_NONE) &&
12122 + (flags != VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE));
12124 + if (flags == VCHI_FLAGS_NONE)
12125 + if (vchiu_queue_is_empty(&service->queue))
12128 + header = vchiu_queue_pop(&service->queue);
12130 + memcpy(data, header->data, header->size < max_data_size_to_read ?
12131 + header->size : max_data_size_to_read);
12133 + *actual_msg_size = header->size;
12135 + vchiq_release_message(service->handle, header);
12139 +EXPORT_SYMBOL(vchi_msg_dequeue);
12141 +/***********************************************************
12142 + * Name: vchi_msg_queuev
12144 + * Arguments: VCHI_SERVICE_HANDLE_T handle,
12145 + * VCHI_MSG_VECTOR_T *vector,
12146 + * uint32_t count,
12147 + * VCHI_FLAGS_T flags,
12148 + * void *msg_handle
12150 + * Description: Thin wrapper to queue a message onto a connection
12152 + * Returns: int32_t - success == 0
12154 + ***********************************************************/
12156 +vchiq_static_assert(sizeof(VCHI_MSG_VECTOR_T) == sizeof(VCHIQ_ELEMENT_T));
12157 +vchiq_static_assert(offsetof(VCHI_MSG_VECTOR_T, vec_base) ==
12158 + offsetof(VCHIQ_ELEMENT_T, data));
12159 +vchiq_static_assert(offsetof(VCHI_MSG_VECTOR_T, vec_len) ==
12160 + offsetof(VCHIQ_ELEMENT_T, size));
12162 +int32_t vchi_msg_queuev(VCHI_SERVICE_HANDLE_T handle,
12163 + VCHI_MSG_VECTOR_T *vector,
12165 + VCHI_FLAGS_T flags,
12166 + void *msg_handle)
12168 + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
12170 + (void)msg_handle;
12172 + WARN_ON(flags != VCHI_FLAGS_BLOCK_UNTIL_QUEUED);
12174 + return vchiq_status_to_vchi(vchiq_queue_message(service->handle,
12175 + (const VCHIQ_ELEMENT_T *)vector, count));
12177 +EXPORT_SYMBOL(vchi_msg_queuev);
12179 +/***********************************************************
12180 + * Name: vchi_held_msg_release
12182 + * Arguments: VCHI_HELD_MSG_T *message
12184 + * Description: Routine to release a held message (after it has been read with
12187 + * Returns: int32_t - success == 0
12189 + ***********************************************************/
12190 +int32_t vchi_held_msg_release(VCHI_HELD_MSG_T *message)
12192 + vchiq_release_message((VCHIQ_SERVICE_HANDLE_T)message->service,
12193 + (VCHIQ_HEADER_T *)message->message);
12197 +EXPORT_SYMBOL(vchi_held_msg_release);
12199 +/***********************************************************
12200 + * Name: vchi_msg_hold
12202 + * Arguments: VCHI_SERVICE_HANDLE_T handle,
12204 + * uint32_t *msg_size,
12205 + * VCHI_FLAGS_T flags,
12206 + * VCHI_HELD_MSG_T *message_handle
12208 + * Description: Routine to return a pointer to the current message (to allow
12209 + * in place processing). The message is dequeued - don't forget
12210 + * to release the message using vchi_held_msg_release when you're
12213 + * Returns: int32_t - success == 0
12215 + ***********************************************************/
12216 +int32_t vchi_msg_hold(VCHI_SERVICE_HANDLE_T handle,
12218 + uint32_t *msg_size,
12219 + VCHI_FLAGS_T flags,
12220 + VCHI_HELD_MSG_T *message_handle)
12222 + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
12223 + VCHIQ_HEADER_T *header;
12225 + WARN_ON((flags != VCHI_FLAGS_NONE) &&
12226 + (flags != VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE));
12228 + if (flags == VCHI_FLAGS_NONE)
12229 + if (vchiu_queue_is_empty(&service->queue))
12232 + header = vchiu_queue_pop(&service->queue);
12234 + *data = header->data;
12235 + *msg_size = header->size;
12237 + message_handle->service =
12238 + (struct opaque_vchi_service_t *)service->handle;
12239 + message_handle->message = header;
12243 +EXPORT_SYMBOL(vchi_msg_hold);
12245 +/***********************************************************
12246 + * Name: vchi_initialise
12248 + * Arguments: VCHI_INSTANCE_T *instance_handle
12250 + * Description: Initialises the hardware but does not transmit anything
12251 + * When run as a Host App this will be called twice hence the need
12252 + * to malloc the state information
12254 + * Returns: 0 if successful, failure otherwise
12256 + ***********************************************************/
12258 +int32_t vchi_initialise(VCHI_INSTANCE_T *instance_handle)
12260 + VCHIQ_INSTANCE_T instance;
12261 + VCHIQ_STATUS_T status;
12263 + status = vchiq_initialise(&instance);
12265 + *instance_handle = (VCHI_INSTANCE_T)instance;
12267 + return vchiq_status_to_vchi(status);
12269 +EXPORT_SYMBOL(vchi_initialise);
12271 +/***********************************************************
12272 + * Name: vchi_connect
12274 + * Arguments: VCHI_CONNECTION_T **connections
12275 + * const uint32_t num_connections
12276 + * VCHI_INSTANCE_T instance_handle)
12278 + * Description: Starts the command service on each connection,
12279 + * causing INIT messages to be pinged back and forth
12281 + * Returns: 0 if successful, failure otherwise
12283 + ***********************************************************/
12284 +int32_t vchi_connect(VCHI_CONNECTION_T **connections,
12285 + const uint32_t num_connections,
12286 + VCHI_INSTANCE_T instance_handle)
12288 + VCHIQ_INSTANCE_T instance = (VCHIQ_INSTANCE_T)instance_handle;
12290 + (void)connections;
12291 + (void)num_connections;
12293 + return vchiq_connect(instance);
12295 +EXPORT_SYMBOL(vchi_connect);
12298 +/***********************************************************
12299 + * Name: vchi_disconnect
12301 + * Arguments: VCHI_INSTANCE_T instance_handle
12303 + * Description: Stops the command service on each connection,
12304 + * causing DE-INIT messages to be pinged back and forth
12306 + * Returns: 0 if successful, failure otherwise
12308 + ***********************************************************/
12309 +int32_t vchi_disconnect(VCHI_INSTANCE_T instance_handle)
12311 + VCHIQ_INSTANCE_T instance = (VCHIQ_INSTANCE_T)instance_handle;
12312 + return vchiq_status_to_vchi(vchiq_shutdown(instance));
12314 +EXPORT_SYMBOL(vchi_disconnect);
12317 +/***********************************************************
12318 + * Name: vchi_service_open
12319 + * Name: vchi_service_create
12321 + * Arguments: VCHI_INSTANCE_T *instance_handle
12322 + * SERVICE_CREATION_T *setup,
12323 + * VCHI_SERVICE_HANDLE_T *handle
12325 + * Description: Routine to open a service
12327 + * Returns: int32_t - success == 0
12329 + ***********************************************************/
12331 +static VCHIQ_STATUS_T shim_callback(VCHIQ_REASON_T reason,
12332 + VCHIQ_HEADER_T *header, VCHIQ_SERVICE_HANDLE_T handle, void *bulk_user)
12334 + SHIM_SERVICE_T *service =
12335 + (SHIM_SERVICE_T *)VCHIQ_GET_SERVICE_USERDATA(handle);
12337 + if (!service->callback)
12340 + switch (reason) {
12341 + case VCHIQ_MESSAGE_AVAILABLE:
12342 + vchiu_queue_push(&service->queue, header);
12344 + service->callback(service->callback_param,
12345 + VCHI_CALLBACK_MSG_AVAILABLE, NULL);
12350 + case VCHIQ_BULK_TRANSMIT_DONE:
12351 + service->callback(service->callback_param,
12352 + VCHI_CALLBACK_BULK_SENT, bulk_user);
12355 + case VCHIQ_BULK_RECEIVE_DONE:
12356 + service->callback(service->callback_param,
12357 + VCHI_CALLBACK_BULK_RECEIVED, bulk_user);
12360 + case VCHIQ_SERVICE_CLOSED:
12361 + service->callback(service->callback_param,
12362 + VCHI_CALLBACK_SERVICE_CLOSED, NULL);
12365 + case VCHIQ_SERVICE_OPENED:
12366 + /* No equivalent VCHI reason */
12369 + case VCHIQ_BULK_TRANSMIT_ABORTED:
12370 + service->callback(service->callback_param,
12371 + VCHI_CALLBACK_BULK_TRANSMIT_ABORTED,
12375 + case VCHIQ_BULK_RECEIVE_ABORTED:
12376 + service->callback(service->callback_param,
12377 + VCHI_CALLBACK_BULK_RECEIVE_ABORTED,
12382 + WARN(1, "not supported\n");
12387 + vchiq_release_message(service->handle, header);
12389 + return VCHIQ_SUCCESS;
12392 +static SHIM_SERVICE_T *service_alloc(VCHIQ_INSTANCE_T instance,
12393 + SERVICE_CREATION_T *setup)
12395 + SHIM_SERVICE_T *service = kzalloc(sizeof(SHIM_SERVICE_T), GFP_KERNEL);
12400 + if (vchiu_queue_init(&service->queue, 64)) {
12401 + service->callback = setup->callback;
12402 + service->callback_param = setup->callback_param;
12412 +static void service_free(SHIM_SERVICE_T *service)
12415 + vchiu_queue_delete(&service->queue);
12420 +int32_t vchi_service_open(VCHI_INSTANCE_T instance_handle,
12421 + SERVICE_CREATION_T *setup,
12422 + VCHI_SERVICE_HANDLE_T *handle)
12424 + VCHIQ_INSTANCE_T instance = (VCHIQ_INSTANCE_T)instance_handle;
12425 + SHIM_SERVICE_T *service = service_alloc(instance, setup);
12427 + VCHIQ_SERVICE_PARAMS_T params;
12428 + VCHIQ_STATUS_T status;
12430 + memset(¶ms, 0, sizeof(params));
12431 + params.fourcc = setup->service_id;
12432 + params.callback = shim_callback;
12433 + params.userdata = service;
12434 + params.version = setup->version.version;
12435 + params.version_min = setup->version.version_min;
12437 + status = vchiq_open_service(instance, ¶ms,
12438 + &service->handle);
12439 + if (status != VCHIQ_SUCCESS) {
12440 + service_free(service);
12445 + *handle = (VCHI_SERVICE_HANDLE_T)service;
12447 + return (service != NULL) ? 0 : -1;
12449 +EXPORT_SYMBOL(vchi_service_open);
12451 +int32_t vchi_service_create(VCHI_INSTANCE_T instance_handle,
12452 + SERVICE_CREATION_T *setup,
12453 + VCHI_SERVICE_HANDLE_T *handle)
12455 + VCHIQ_INSTANCE_T instance = (VCHIQ_INSTANCE_T)instance_handle;
12456 + SHIM_SERVICE_T *service = service_alloc(instance, setup);
12458 + VCHIQ_SERVICE_PARAMS_T params;
12459 + VCHIQ_STATUS_T status;
12461 + memset(¶ms, 0, sizeof(params));
12462 + params.fourcc = setup->service_id;
12463 + params.callback = shim_callback;
12464 + params.userdata = service;
12465 + params.version = setup->version.version;
12466 + params.version_min = setup->version.version_min;
12467 + status = vchiq_add_service(instance, ¶ms, &service->handle);
12469 + if (status != VCHIQ_SUCCESS) {
12470 + service_free(service);
12475 + *handle = (VCHI_SERVICE_HANDLE_T)service;
12477 + return (service != NULL) ? 0 : -1;
12479 +EXPORT_SYMBOL(vchi_service_create);
12481 +int32_t vchi_service_close(const VCHI_SERVICE_HANDLE_T handle)
12483 + int32_t ret = -1;
12484 + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
12486 + VCHIQ_STATUS_T status = vchiq_close_service(service->handle);
12487 + if (status == VCHIQ_SUCCESS) {
12488 + service_free(service);
12492 + ret = vchiq_status_to_vchi(status);
12496 +EXPORT_SYMBOL(vchi_service_close);
12498 +int32_t vchi_service_destroy(const VCHI_SERVICE_HANDLE_T handle)
12500 + int32_t ret = -1;
12501 + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
12503 + VCHIQ_STATUS_T status = vchiq_remove_service(service->handle);
12504 + if (status == VCHIQ_SUCCESS) {
12505 + service_free(service);
12509 + ret = vchiq_status_to_vchi(status);
12513 +EXPORT_SYMBOL(vchi_service_destroy);
12515 +int32_t vchi_service_set_option(const VCHI_SERVICE_HANDLE_T handle,
12516 + VCHI_SERVICE_OPTION_T option,
12519 + int32_t ret = -1;
12520 + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
12521 + VCHIQ_SERVICE_OPTION_T vchiq_option;
12522 + switch (option) {
12523 + case VCHI_SERVICE_OPTION_TRACE:
12524 + vchiq_option = VCHIQ_SERVICE_OPTION_TRACE;
12531 + VCHIQ_STATUS_T status =
12532 + vchiq_set_service_option(service->handle,
12536 + ret = vchiq_status_to_vchi(status);
12540 +EXPORT_SYMBOL(vchi_service_set_option);
12542 +int32_t vchi_get_peer_version( const VCHI_SERVICE_HANDLE_T handle, short *peer_version )
12544 + int32_t ret = -1;
12545 + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
12548 + VCHIQ_STATUS_T status = vchiq_get_peer_version(service->handle, peer_version);
12549 + ret = vchiq_status_to_vchi( status );
12553 +EXPORT_SYMBOL(vchi_get_peer_version);
12555 +/* ----------------------------------------------------------------------
12556 + * read a uint32_t from buffer.
12557 + * network format is defined to be little endian
12558 + * -------------------------------------------------------------------- */
12560 +vchi_readbuf_uint32(const void *_ptr)
12562 + const unsigned char *ptr = _ptr;
12563 + return ptr[0] | (ptr[1] << 8) | (ptr[2] << 16) | (ptr[3] << 24);
12566 +/* ----------------------------------------------------------------------
12567 + * write a uint32_t to buffer.
12568 + * network format is defined to be little endian
12569 + * -------------------------------------------------------------------- */
12571 +vchi_writebuf_uint32(void *_ptr, uint32_t value)
12573 + unsigned char *ptr = _ptr;
12574 + ptr[0] = (unsigned char)((value >> 0) & 0xFF);
12575 + ptr[1] = (unsigned char)((value >> 8) & 0xFF);
12576 + ptr[2] = (unsigned char)((value >> 16) & 0xFF);
12577 + ptr[3] = (unsigned char)((value >> 24) & 0xFF);
12580 +/* ----------------------------------------------------------------------
12581 + * read a uint16_t from buffer.
12582 + * network format is defined to be little endian
12583 + * -------------------------------------------------------------------- */
12585 +vchi_readbuf_uint16(const void *_ptr)
12587 + const unsigned char *ptr = _ptr;
12588 + return ptr[0] | (ptr[1] << 8);
12591 +/* ----------------------------------------------------------------------
12592 + * write a uint16_t into the buffer.
12593 + * network format is defined to be little endian
12594 + * -------------------------------------------------------------------- */
12596 +vchi_writebuf_uint16(void *_ptr, uint16_t value)
12598 + unsigned char *ptr = _ptr;
12599 + ptr[0] = (value >> 0) & 0xFF;
12600 + ptr[1] = (value >> 8) & 0xFF;
12603 +/***********************************************************
12604 + * Name: vchi_service_use
12606 + * Arguments: const VCHI_SERVICE_HANDLE_T handle
12608 + * Description: Routine to increment refcount on a service
12612 + ***********************************************************/
12613 +int32_t vchi_service_use(const VCHI_SERVICE_HANDLE_T handle)
12615 + int32_t ret = -1;
12616 + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
12618 + ret = vchiq_status_to_vchi(vchiq_use_service(service->handle));
12621 +EXPORT_SYMBOL(vchi_service_use);
12623 +/***********************************************************
12624 + * Name: vchi_service_release
12626 + * Arguments: const VCHI_SERVICE_HANDLE_T handle
12628 + * Description: Routine to decrement refcount on a service
12632 + ***********************************************************/
12633 +int32_t vchi_service_release(const VCHI_SERVICE_HANDLE_T handle)
12635 + int32_t ret = -1;
12636 + SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
12638 + ret = vchiq_status_to_vchi(
12639 + vchiq_release_service(service->handle));
12642 +EXPORT_SYMBOL(vchi_service_release);
12643 diff --git a/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.c b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.c
12644 new file mode 100644
12645 index 0000000..c2eefef
12647 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.c
12650 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
12652 + * Redistribution and use in source and binary forms, with or without
12653 + * modification, are permitted provided that the following conditions
12655 + * 1. Redistributions of source code must retain the above copyright
12656 + * notice, this list of conditions, and the following disclaimer,
12657 + * without modification.
12658 + * 2. Redistributions in binary form must reproduce the above copyright
12659 + * notice, this list of conditions and the following disclaimer in the
12660 + * documentation and/or other materials provided with the distribution.
12661 + * 3. The names of the above-listed copyright holders may not be used
12662 + * to endorse or promote products derived from this software without
12663 + * specific prior written permission.
12665 + * ALTERNATIVELY, this software may be distributed under the terms of the
12666 + * GNU General Public License ("GPL") version 2, as published by the Free
12667 + * Software Foundation.
12669 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
12670 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
12671 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
12672 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
12673 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
12674 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
12675 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
12676 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
12677 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
12678 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
12679 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
12682 +#include "vchiq_util.h"
12684 +static inline int is_pow2(int i)
12686 + return i && !(i & (i - 1));
12689 +int vchiu_queue_init(VCHIU_QUEUE_T *queue, int size)
12691 + WARN_ON(!is_pow2(size));
12693 + queue->size = size;
12695 + queue->write = 0;
12697 + sema_init(&queue->pop, 0);
12698 + sema_init(&queue->push, 0);
12700 + queue->storage = kzalloc(size * sizeof(VCHIQ_HEADER_T *), GFP_KERNEL);
12701 + if (queue->storage == NULL) {
12702 + vchiu_queue_delete(queue);
12708 +void vchiu_queue_delete(VCHIU_QUEUE_T *queue)
12710 + if (queue->storage != NULL)
12711 + kfree(queue->storage);
12714 +int vchiu_queue_is_empty(VCHIU_QUEUE_T *queue)
12716 + return queue->read == queue->write;
12719 +int vchiu_queue_is_full(VCHIU_QUEUE_T *queue)
12721 + return queue->write == queue->read + queue->size;
12724 +void vchiu_queue_push(VCHIU_QUEUE_T *queue, VCHIQ_HEADER_T *header)
12726 + while (queue->write == queue->read + queue->size) {
12727 + if (down_interruptible(&queue->pop) != 0) {
12728 + flush_signals(current);
12733 + * Write to queue->storage must be visible after read from
12738 + queue->storage[queue->write & (queue->size - 1)] = header;
12741 + * Write to queue->storage must be visible before write to
12748 + up(&queue->push);
12751 +VCHIQ_HEADER_T *vchiu_queue_peek(VCHIU_QUEUE_T *queue)
12753 + while (queue->write == queue->read) {
12754 + if (down_interruptible(&queue->push) != 0) {
12755 + flush_signals(current);
12759 + up(&queue->push); // We haven't removed anything from the queue.
12762 + * Read from queue->storage must be visible after read from
12767 + return queue->storage[queue->read & (queue->size - 1)];
12770 +VCHIQ_HEADER_T *vchiu_queue_pop(VCHIU_QUEUE_T *queue)
12772 + VCHIQ_HEADER_T *header;
12774 + while (queue->write == queue->read) {
12775 + if (down_interruptible(&queue->push) != 0) {
12776 + flush_signals(current);
12781 + * Read from queue->storage must be visible after read from
12786 + header = queue->storage[queue->read & (queue->size - 1)];
12789 + * Read from queue->storage must be visible before write to
12800 diff --git a/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.h b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.h
12801 new file mode 100644
12802 index 0000000..f4d0b66
12804 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.h
12807 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
12809 + * Redistribution and use in source and binary forms, with or without
12810 + * modification, are permitted provided that the following conditions
12812 + * 1. Redistributions of source code must retain the above copyright
12813 + * notice, this list of conditions, and the following disclaimer,
12814 + * without modification.
12815 + * 2. Redistributions in binary form must reproduce the above copyright
12816 + * notice, this list of conditions and the following disclaimer in the
12817 + * documentation and/or other materials provided with the distribution.
12818 + * 3. The names of the above-listed copyright holders may not be used
12819 + * to endorse or promote products derived from this software without
12820 + * specific prior written permission.
12822 + * ALTERNATIVELY, this software may be distributed under the terms of the
12823 + * GNU General Public License ("GPL") version 2, as published by the Free
12824 + * Software Foundation.
12826 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
12827 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
12828 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
12829 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
12830 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
12831 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
12832 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
12833 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
12834 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
12835 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
12836 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
12839 +#ifndef VCHIQ_UTIL_H
12840 +#define VCHIQ_UTIL_H
12842 +#include <linux/types.h>
12843 +#include <linux/semaphore.h>
12844 +#include <linux/mutex.h>
12845 +#include <linux/bitops.h>
12846 +#include <linux/kthread.h>
12847 +#include <linux/wait.h>
12848 +#include <linux/vmalloc.h>
12849 +#include <linux/jiffies.h>
12850 +#include <linux/delay.h>
12851 +#include <linux/string.h>
12852 +#include <linux/types.h>
12853 +#include <linux/interrupt.h>
12854 +#include <linux/random.h>
12855 +#include <linux/sched.h>
12856 +#include <linux/ctype.h>
12857 +#include <linux/uaccess.h>
12858 +#include <linux/time.h> /* for time_t */
12859 +#include <linux/slab.h>
12860 +#include <linux/vmalloc.h>
12862 +#include "vchiq_if.h"
12869 + struct semaphore pop;
12870 + struct semaphore push;
12872 + VCHIQ_HEADER_T **storage;
12875 +extern int vchiu_queue_init(VCHIU_QUEUE_T *queue, int size);
12876 +extern void vchiu_queue_delete(VCHIU_QUEUE_T *queue);
12878 +extern int vchiu_queue_is_empty(VCHIU_QUEUE_T *queue);
12879 +extern int vchiu_queue_is_full(VCHIU_QUEUE_T *queue);
12881 +extern void vchiu_queue_push(VCHIU_QUEUE_T *queue, VCHIQ_HEADER_T *header);
12883 +extern VCHIQ_HEADER_T *vchiu_queue_peek(VCHIU_QUEUE_T *queue);
12884 +extern VCHIQ_HEADER_T *vchiu_queue_pop(VCHIU_QUEUE_T *queue);
12887 diff --git a/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_version.c b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_version.c
12888 new file mode 100644
12889 index 0000000..b6bfa21
12891 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_version.c
12894 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
12896 + * Redistribution and use in source and binary forms, with or without
12897 + * modification, are permitted provided that the following conditions
12899 + * 1. Redistributions of source code must retain the above copyright
12900 + * notice, this list of conditions, and the following disclaimer,
12901 + * without modification.
12902 + * 2. Redistributions in binary form must reproduce the above copyright
12903 + * notice, this list of conditions and the following disclaimer in the
12904 + * documentation and/or other materials provided with the distribution.
12905 + * 3. The names of the above-listed copyright holders may not be used
12906 + * to endorse or promote products derived from this software without
12907 + * specific prior written permission.
12909 + * ALTERNATIVELY, this software may be distributed under the terms of the
12910 + * GNU General Public License ("GPL") version 2, as published by the Free
12911 + * Software Foundation.
12913 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
12914 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
12915 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
12916 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
12917 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
12918 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
12919 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
12920 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
12921 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
12922 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
12923 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
12925 +#include "vchiq_build_info.h"
12926 +#include <linux/broadcom/vc_debug_sym.h>
12928 +VC_DEBUG_DECLARE_STRING_VAR( vchiq_build_hostname, "dc4-arm-01" );
12929 +VC_DEBUG_DECLARE_STRING_VAR( vchiq_build_version, "9245b4c35b99b3870e1f7dc598c5692b3c66a6f0 (tainted)" );
12930 +VC_DEBUG_DECLARE_STRING_VAR( vchiq_build_time, __TIME__ );
12931 +VC_DEBUG_DECLARE_STRING_VAR( vchiq_build_date, __DATE__ );
12933 +const char *vchiq_get_build_hostname( void )
12935 + return vchiq_build_hostname;
12938 +const char *vchiq_get_build_version( void )
12940 + return vchiq_build_version;
12943 +const char *vchiq_get_build_date( void )
12945 + return vchiq_build_date;
12948 +const char *vchiq_get_build_time( void )
12950 + return vchiq_build_time;