ramips: add support for PandoraBox PBR-M1
[15.05/openwrt.git] / target / linux / brcm2708 / patches-3.18 / 0010-bcm2708-vchiq-driver.patch
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
5
6 Signed-off-by: popcornmix <popcornmix@gmail.com>
7
8 vchiq: create_pagelist copes with vmalloc memory
9
10 Signed-off-by: Daniel Stone <daniels@collabora.com>
11
12 vchiq: fix the shim message release
13
14 Signed-off-by: Daniel Stone <daniels@collabora.com>
15
16 vchiq: export additional symbols
17
18 Signed-off-by: Daniel Stone <daniels@collabora.com>
19
20 VCHIQ: Make service closure fully synchronous (drv)
21
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.
26
27 VCHIQ: Add per-service tracing
28
29 The new service option VCHIQ_SERVICE_OPTION_TRACE is a boolean that
30 toggles tracing for the specified service.
31
32 This commit also introduces vchi_service_set_option and the associated
33 option VCHI_SERVICE_OPTION_TRACE.
34
35 vchiq: Make the synchronous-CLOSE logic more tolerant
36
37 vchiq: Move logging control into debugfs
38
39 vchiq: Take care of a corner case tickled by VCSM
40
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.
46
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
50 grab port 0.)
51
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
55 described above.
56 ---
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
124
125 --- a/drivers/misc/Kconfig
126 +++ b/drivers/misc/Kconfig
127 @@ -524,6 +524,7 @@ source "drivers/misc/carma/Kconfig"
128  source "drivers/misc/altera-stapl/Kconfig"
129  source "drivers/misc/mei/Kconfig"
130  source "drivers/misc/vmw_vmci/Kconfig"
131 +source "drivers/misc/vc04_services/Kconfig"
132  source "drivers/misc/mic/Kconfig"
133  source "drivers/misc/genwqe/Kconfig"
134  source "drivers/misc/echo/Kconfig"
135 --- a/drivers/misc/Makefile
136 +++ b/drivers/misc/Makefile
137 @@ -51,6 +51,7 @@ obj-$(CONFIG_INTEL_MEI)               += mei/
138  obj-$(CONFIG_VMWARE_VMCI)      += vmw_vmci/
139  obj-$(CONFIG_LATTICE_ECP3_CONFIG)      += lattice-ecp3-config.o
140  obj-$(CONFIG_SRAM)             += sram.o
141 +obj-y                          += vc04_services/
142  obj-y                          += mic/
143  obj-$(CONFIG_GENWQE)           += genwqe/
144  obj-$(CONFIG_ECHO)             += echo/
145 --- /dev/null
146 +++ b/drivers/misc/vc04_services/Kconfig
147 @@ -0,0 +1,9 @@
148 +config BCM2708_VCHIQ
149 +       tristate "Videocore VCHIQ"
150 +       depends on MACH_BCM2708
151 +       default y
152 +       help
153 +               Kernel to VideoCore communication interface for the
154 +               BCM2708 family of products.
155 +               Defaults to Y when the Broadcom Videocore services
156 +               are included in the build, N otherwise.
157 --- /dev/null
158 +++ b/drivers/misc/vc04_services/Makefile
159 @@ -0,0 +1,17 @@
160 +ifeq ($(CONFIG_MACH_BCM2708),y)
161 +
162 +obj-$(CONFIG_BCM2708_VCHIQ)    += vchiq.o
163 +
164 +vchiq-objs := \
165 +   interface/vchiq_arm/vchiq_core.o  \
166 +   interface/vchiq_arm/vchiq_arm.o \
167 +   interface/vchiq_arm/vchiq_kern_lib.o \
168 +   interface/vchiq_arm/vchiq_2835_arm.o \
169 +   interface/vchiq_arm/vchiq_debugfs.o \
170 +   interface/vchiq_arm/vchiq_shim.o \
171 +   interface/vchiq_arm/vchiq_util.o \
172 +   interface/vchiq_arm/vchiq_connected.o \
173 +
174 +ccflags-y += -DVCOS_VERIFY_BKPTS=1 -Idrivers/misc/vc04_services -DUSE_VCHIQ_ARM -D__VCCOREVER__=0x04000000
175 +
176 +endif
177 --- /dev/null
178 +++ b/drivers/misc/vc04_services/interface/vchi/connections/connection.h
179 @@ -0,0 +1,328 @@
180 +/**
181 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
182 + *
183 + * Redistribution and use in source and binary forms, with or without
184 + * modification, are permitted provided that the following conditions
185 + * are met:
186 + * 1. Redistributions of source code must retain the above copyright
187 + *    notice, this list of conditions, and the following disclaimer,
188 + *    without modification.
189 + * 2. Redistributions in binary form must reproduce the above copyright
190 + *    notice, this list of conditions and the following disclaimer in the
191 + *    documentation and/or other materials provided with the distribution.
192 + * 3. The names of the above-listed copyright holders may not be used
193 + *    to endorse or promote products derived from this software without
194 + *    specific prior written permission.
195 + *
196 + * ALTERNATIVELY, this software may be distributed under the terms of the
197 + * GNU General Public License ("GPL") version 2, as published by the Free
198 + * Software Foundation.
199 + *
200 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
201 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
202 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
203 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
204 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
205 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
206 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
207 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
208 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
209 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
210 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
211 + */
212 +
213 +#ifndef CONNECTION_H_
214 +#define CONNECTION_H_
215 +
216 +#include <linux/kernel.h>
217 +#include <linux/types.h>
218 +#include <linux/semaphore.h>
219 +
220 +#include "interface/vchi/vchi_cfg_internal.h"
221 +#include "interface/vchi/vchi_common.h"
222 +#include "interface/vchi/message_drivers/message.h"
223 +
224 +/******************************************************************************
225 + Global defs
226 + *****************************************************************************/
227 +
228 +// Opaque handle for a connection / service pair
229 +typedef struct opaque_vchi_connection_connected_service_handle_t *VCHI_CONNECTION_SERVICE_HANDLE_T;
230 +
231 +// opaque handle to the connection state information
232 +typedef struct opaque_vchi_connection_info_t VCHI_CONNECTION_STATE_T;
233 +
234 +typedef struct vchi_connection_t VCHI_CONNECTION_T;
235 +
236 +
237 +/******************************************************************************
238 + API
239 + *****************************************************************************/
240 +
241 +// Routine to init a connection with a particular low level driver
242 +typedef VCHI_CONNECTION_STATE_T * (*VCHI_CONNECTION_INIT_T)( struct vchi_connection_t * connection,
243 +                                                             const VCHI_MESSAGE_DRIVER_T * driver );
244 +
245 +// Routine to control CRC enabling at a connection level
246 +typedef int32_t (*VCHI_CONNECTION_CRC_CONTROL_T)( VCHI_CONNECTION_STATE_T *state_handle,
247 +                                                  VCHI_CRC_CONTROL_T control );
248 +
249 +// Routine to create a service
250 +typedef int32_t (*VCHI_CONNECTION_SERVICE_CONNECT_T)( VCHI_CONNECTION_STATE_T *state_handle,
251 +                                                      int32_t service_id,
252 +                                                      uint32_t rx_fifo_size,
253 +                                                      uint32_t tx_fifo_size,
254 +                                                      int server,
255 +                                                      VCHI_CALLBACK_T callback,
256 +                                                      void *callback_param,
257 +                                                      int32_t want_crc,
258 +                                                      int32_t want_unaligned_bulk_rx,
259 +                                                      int32_t want_unaligned_bulk_tx,
260 +                                                      VCHI_CONNECTION_SERVICE_HANDLE_T *service_handle );
261 +
262 +// Routine to close a service
263 +typedef int32_t (*VCHI_CONNECTION_SERVICE_DISCONNECT_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle );
264 +
265 +// Routine to queue a message
266 +typedef int32_t (*VCHI_CONNECTION_SERVICE_QUEUE_MESSAGE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
267 +                                                            const void *data,
268 +                                                            uint32_t data_size,
269 +                                                            VCHI_FLAGS_T flags,
270 +                                                            void *msg_handle );
271 +
272 +// scatter-gather (vector) message queueing
273 +typedef int32_t (*VCHI_CONNECTION_SERVICE_QUEUE_MESSAGEV_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
274 +                                                             VCHI_MSG_VECTOR_T *vector,
275 +                                                             uint32_t count,
276 +                                                             VCHI_FLAGS_T flags,
277 +                                                             void *msg_handle );
278 +
279 +// Routine to dequeue a message
280 +typedef int32_t (*VCHI_CONNECTION_SERVICE_DEQUEUE_MESSAGE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
281 +                                                              void *data,
282 +                                                              uint32_t max_data_size_to_read,
283 +                                                              uint32_t *actual_msg_size,
284 +                                                              VCHI_FLAGS_T flags );
285 +
286 +// Routine to peek at a message
287 +typedef int32_t (*VCHI_CONNECTION_SERVICE_PEEK_MESSAGE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
288 +                                                           void **data,
289 +                                                           uint32_t *msg_size,
290 +                                                           VCHI_FLAGS_T flags );
291 +
292 +// Routine to hold a message
293 +typedef int32_t (*VCHI_CONNECTION_SERVICE_HOLD_MESSAGE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
294 +                                                           void **data,
295 +                                                           uint32_t *msg_size,
296 +                                                           VCHI_FLAGS_T flags,
297 +                                                           void **message_handle );
298 +
299 +// Routine to initialise a received message iterator
300 +typedef int32_t (*VCHI_CONNECTION_SERVICE_LOOKAHEAD_MESSAGE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
301 +                                                                VCHI_MSG_ITER_T *iter,
302 +                                                                VCHI_FLAGS_T flags );
303 +
304 +// Routine to release a held message
305 +typedef int32_t (*VCHI_CONNECTION_HELD_MSG_RELEASE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
306 +                                                       void *message_handle );
307 +
308 +// Routine to get info on a held message
309 +typedef int32_t (*VCHI_CONNECTION_HELD_MSG_INFO_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
310 +                                                    void *message_handle,
311 +                                                    void **data,
312 +                                                    int32_t *msg_size,
313 +                                                    uint32_t *tx_timestamp,
314 +                                                    uint32_t *rx_timestamp );
315 +
316 +// Routine to check whether the iterator has a next message
317 +typedef int32_t (*VCHI_CONNECTION_MSG_ITER_HAS_NEXT_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service,
318 +                                                       const VCHI_MSG_ITER_T *iter );
319 +
320 +// Routine to advance the iterator
321 +typedef int32_t (*VCHI_CONNECTION_MSG_ITER_NEXT_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service,
322 +                                                    VCHI_MSG_ITER_T *iter,
323 +                                                    void **data,
324 +                                                    uint32_t *msg_size );
325 +
326 +// Routine to remove the last message returned by the iterator
327 +typedef int32_t (*VCHI_CONNECTION_MSG_ITER_REMOVE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service,
328 +                                                      VCHI_MSG_ITER_T *iter );
329 +
330 +// Routine to hold the last message returned by the iterator
331 +typedef int32_t (*VCHI_CONNECTION_MSG_ITER_HOLD_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service,
332 +                                                    VCHI_MSG_ITER_T *iter,
333 +                                                    void **msg_handle );
334 +
335 +// Routine to transmit bulk data
336 +typedef int32_t (*VCHI_CONNECTION_BULK_QUEUE_TRANSMIT_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
337 +                                                          const void *data_src,
338 +                                                          uint32_t data_size,
339 +                                                          VCHI_FLAGS_T flags,
340 +                                                          void *bulk_handle );
341 +
342 +// Routine to receive data
343 +typedef int32_t (*VCHI_CONNECTION_BULK_QUEUE_RECEIVE_T)( VCHI_CONNECTION_SERVICE_HANDLE_T service_handle,
344 +                                                         void *data_dst,
345 +                                                         uint32_t data_size,
346 +                                                         VCHI_FLAGS_T flags,
347 +                                                         void *bulk_handle );
348 +
349 +// Routine to report if a server is available
350 +typedef int32_t (*VCHI_CONNECTION_SERVER_PRESENT)( VCHI_CONNECTION_STATE_T *state, int32_t service_id, int32_t peer_flags );
351 +
352 +// Routine to report the number of RX slots available
353 +typedef int (*VCHI_CONNECTION_RX_SLOTS_AVAILABLE)( const VCHI_CONNECTION_STATE_T *state );
354 +
355 +// Routine to report the RX slot size
356 +typedef uint32_t (*VCHI_CONNECTION_RX_SLOT_SIZE)( const VCHI_CONNECTION_STATE_T *state );
357 +
358 +// Callback to indicate that the other side has added a buffer to the rx bulk DMA FIFO
359 +typedef void (*VCHI_CONNECTION_RX_BULK_BUFFER_ADDED)(VCHI_CONNECTION_STATE_T *state,
360 +                                                     int32_t service,
361 +                                                     uint32_t length,
362 +                                                     MESSAGE_TX_CHANNEL_T channel,
363 +                                                     uint32_t channel_params,
364 +                                                     uint32_t data_length,
365 +                                                     uint32_t data_offset);
366 +
367 +// Callback to inform a service that a Xon or Xoff message has been received
368 +typedef void (*VCHI_CONNECTION_FLOW_CONTROL)(VCHI_CONNECTION_STATE_T *state, int32_t service_id, int32_t xoff);
369 +
370 +// Callback to inform a service that a server available reply message has been received
371 +typedef void (*VCHI_CONNECTION_SERVER_AVAILABLE_REPLY)(VCHI_CONNECTION_STATE_T *state, int32_t service_id, uint32_t flags);
372 +
373 +// Callback to indicate that bulk auxiliary messages have arrived
374 +typedef void (*VCHI_CONNECTION_BULK_AUX_RECEIVED)(VCHI_CONNECTION_STATE_T *state);
375 +
376 +// Callback to indicate that bulk auxiliary messages have arrived
377 +typedef void (*VCHI_CONNECTION_BULK_AUX_TRANSMITTED)(VCHI_CONNECTION_STATE_T *state, void *handle);
378 +
379 +// Callback with all the connection info you require
380 +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);
381 +
382 +// Callback to inform of a disconnect
383 +typedef void (*VCHI_CONNECTION_DISCONNECT)(VCHI_CONNECTION_STATE_T *state, uint32_t flags);
384 +
385 +// Callback to inform of a power control request
386 +typedef void (*VCHI_CONNECTION_POWER_CONTROL)(VCHI_CONNECTION_STATE_T *state, MESSAGE_TX_CHANNEL_T channel, int32_t enable);
387 +
388 +// allocate memory suitably aligned for this connection
389 +typedef void * (*VCHI_BUFFER_ALLOCATE)(VCHI_CONNECTION_SERVICE_HANDLE_T service_handle, uint32_t * length);
390 +
391 +// free memory allocated by buffer_allocate
392 +typedef void   (*VCHI_BUFFER_FREE)(VCHI_CONNECTION_SERVICE_HANDLE_T service_handle, void * address);
393 +
394 +
395 +/******************************************************************************
396 + System driver struct
397 + *****************************************************************************/
398 +
399 +struct opaque_vchi_connection_api_t
400 +{
401 +   // Routine to init the connection
402 +   VCHI_CONNECTION_INIT_T                      init;
403 +
404 +   // Connection-level CRC control
405 +   VCHI_CONNECTION_CRC_CONTROL_T               crc_control;
406 +
407 +   // Routine to connect to or create service
408 +   VCHI_CONNECTION_SERVICE_CONNECT_T           service_connect;
409 +
410 +   // Routine to disconnect from a service
411 +   VCHI_CONNECTION_SERVICE_DISCONNECT_T        service_disconnect;
412 +
413 +   // Routine to queue a message
414 +   VCHI_CONNECTION_SERVICE_QUEUE_MESSAGE_T     service_queue_msg;
415 +
416 +   // scatter-gather (vector) message queue
417 +   VCHI_CONNECTION_SERVICE_QUEUE_MESSAGEV_T    service_queue_msgv;
418 +
419 +   // Routine to dequeue a message
420 +   VCHI_CONNECTION_SERVICE_DEQUEUE_MESSAGE_T   service_dequeue_msg;
421 +
422 +   // Routine to peek at a message
423 +   VCHI_CONNECTION_SERVICE_PEEK_MESSAGE_T      service_peek_msg;
424 +
425 +   // Routine to hold a message
426 +   VCHI_CONNECTION_SERVICE_HOLD_MESSAGE_T      service_hold_msg;
427 +
428 +   // Routine to initialise a received message iterator
429 +   VCHI_CONNECTION_SERVICE_LOOKAHEAD_MESSAGE_T service_look_ahead_msg;
430 +
431 +   // Routine to release a message
432 +   VCHI_CONNECTION_HELD_MSG_RELEASE_T          held_msg_release;
433 +
434 +   // Routine to get information on a held message
435 +   VCHI_CONNECTION_HELD_MSG_INFO_T             held_msg_info;
436 +
437 +   // Routine to check for next message on iterator
438 +   VCHI_CONNECTION_MSG_ITER_HAS_NEXT_T         msg_iter_has_next;
439 +
440 +   // Routine to get next message on iterator
441 +   VCHI_CONNECTION_MSG_ITER_NEXT_T             msg_iter_next;
442 +
443 +   // Routine to remove the last message returned by iterator
444 +   VCHI_CONNECTION_MSG_ITER_REMOVE_T           msg_iter_remove;
445 +
446 +   // Routine to hold the last message returned by iterator
447 +   VCHI_CONNECTION_MSG_ITER_HOLD_T             msg_iter_hold;
448 +
449 +   // Routine to transmit bulk data
450 +   VCHI_CONNECTION_BULK_QUEUE_TRANSMIT_T       bulk_queue_transmit;
451 +
452 +   // Routine to receive data
453 +   VCHI_CONNECTION_BULK_QUEUE_RECEIVE_T        bulk_queue_receive;
454 +
455 +   // Routine to report the available servers
456 +   VCHI_CONNECTION_SERVER_PRESENT              server_present;
457 +
458 +   // Routine to report the number of RX slots available
459 +   VCHI_CONNECTION_RX_SLOTS_AVAILABLE          connection_rx_slots_available;
460 +
461 +   // Routine to report the RX slot size
462 +   VCHI_CONNECTION_RX_SLOT_SIZE                connection_rx_slot_size;
463 +
464 +   // Callback to indicate that the other side has added a buffer to the rx bulk DMA FIFO
465 +   VCHI_CONNECTION_RX_BULK_BUFFER_ADDED        rx_bulk_buffer_added;
466 +
467 +   // Callback to inform a service that a Xon or Xoff message has been received
468 +   VCHI_CONNECTION_FLOW_CONTROL                flow_control;
469 +
470 +   // Callback to inform a service that a server available reply message has been received
471 +   VCHI_CONNECTION_SERVER_AVAILABLE_REPLY      server_available_reply;
472 +
473 +   // Callback to indicate that bulk auxiliary messages have arrived
474 +   VCHI_CONNECTION_BULK_AUX_RECEIVED           bulk_aux_received;
475 +
476 +   // Callback to indicate that a bulk auxiliary message has been transmitted
477 +   VCHI_CONNECTION_BULK_AUX_TRANSMITTED        bulk_aux_transmitted;
478 +
479 +   // Callback to provide information about the connection
480 +   VCHI_CONNECTION_INFO                        connection_info;
481 +
482 +   // Callback to notify that peer has requested disconnect
483 +   VCHI_CONNECTION_DISCONNECT                  disconnect;
484 +
485 +   // Callback to notify that peer has requested power change
486 +   VCHI_CONNECTION_POWER_CONTROL               power_control;
487 +
488 +   // allocate memory suitably aligned for this connection
489 +   VCHI_BUFFER_ALLOCATE                        buffer_allocate;
490 +
491 +   // free memory allocated by buffer_allocate
492 +   VCHI_BUFFER_FREE                            buffer_free;
493 +
494 +};
495 +
496 +struct vchi_connection_t {
497 +   const VCHI_CONNECTION_API_T *api;
498 +   VCHI_CONNECTION_STATE_T     *state;
499 +#ifdef VCHI_COARSE_LOCKING
500 +   struct semaphore             sem;
501 +#endif
502 +};
503 +
504 +
505 +#endif /* CONNECTION_H_ */
506 +
507 +/****************************** End of file **********************************/
508 --- /dev/null
509 +++ b/drivers/misc/vc04_services/interface/vchi/message_drivers/message.h
510 @@ -0,0 +1,204 @@
511 +/**
512 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
513 + *
514 + * Redistribution and use in source and binary forms, with or without
515 + * modification, are permitted provided that the following conditions
516 + * are met:
517 + * 1. Redistributions of source code must retain the above copyright
518 + *    notice, this list of conditions, and the following disclaimer,
519 + *    without modification.
520 + * 2. Redistributions in binary form must reproduce the above copyright
521 + *    notice, this list of conditions and the following disclaimer in the
522 + *    documentation and/or other materials provided with the distribution.
523 + * 3. The names of the above-listed copyright holders may not be used
524 + *    to endorse or promote products derived from this software without
525 + *    specific prior written permission.
526 + *
527 + * ALTERNATIVELY, this software may be distributed under the terms of the
528 + * GNU General Public License ("GPL") version 2, as published by the Free
529 + * Software Foundation.
530 + *
531 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
532 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
533 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
534 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
535 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
536 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
537 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
538 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
539 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
540 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
541 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
542 + */
543 +
544 +#ifndef _VCHI_MESSAGE_H_
545 +#define _VCHI_MESSAGE_H_
546 +
547 +#include <linux/kernel.h>
548 +#include <linux/types.h>
549 +#include <linux/semaphore.h>
550 +
551 +#include "interface/vchi/vchi_cfg_internal.h"
552 +#include "interface/vchi/vchi_common.h"
553 +
554 +
555 +typedef enum message_event_type {
556 +   MESSAGE_EVENT_NONE,
557 +   MESSAGE_EVENT_NOP,
558 +   MESSAGE_EVENT_MESSAGE,
559 +   MESSAGE_EVENT_SLOT_COMPLETE,
560 +   MESSAGE_EVENT_RX_BULK_PAUSED,
561 +   MESSAGE_EVENT_RX_BULK_COMPLETE,
562 +   MESSAGE_EVENT_TX_COMPLETE,
563 +   MESSAGE_EVENT_MSG_DISCARDED
564 +} MESSAGE_EVENT_TYPE_T;
565 +
566 +typedef enum vchi_msg_flags
567 +{
568 +   VCHI_MSG_FLAGS_NONE                  = 0x0,
569 +   VCHI_MSG_FLAGS_TERMINATE_DMA         = 0x1
570 +} VCHI_MSG_FLAGS_T;
571 +
572 +typedef enum message_tx_channel
573 +{
574 +   MESSAGE_TX_CHANNEL_MESSAGE           = 0,
575 +   MESSAGE_TX_CHANNEL_BULK              = 1 // drivers may provide multiple bulk channels, from 1 upwards
576 +} MESSAGE_TX_CHANNEL_T;
577 +
578 +// Macros used for cycling through bulk channels
579 +#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)
580 +#define MESSAGE_TX_CHANNEL_BULK_NEXT(c) (MESSAGE_TX_CHANNEL_BULK+((c)-MESSAGE_TX_CHANNEL_BULK+1)%VCHI_MAX_BULK_TX_CHANNELS_PER_CONNECTION)
581 +
582 +typedef enum message_rx_channel
583 +{
584 +   MESSAGE_RX_CHANNEL_MESSAGE           = 0,
585 +   MESSAGE_RX_CHANNEL_BULK              = 1 // drivers may provide multiple bulk channels, from 1 upwards
586 +} MESSAGE_RX_CHANNEL_T;
587 +
588 +// Message receive slot information
589 +typedef struct rx_msg_slot_info {
590 +
591 +   struct rx_msg_slot_info *next;
592 +   //struct slot_info *prev;
593 +#if !defined VCHI_COARSE_LOCKING
594 +   struct semaphore   sem;
595 +#endif
596 +
597 +   uint8_t           *addr;               // base address of slot
598 +   uint32_t           len;                // length of slot in bytes
599 +
600 +   uint32_t           write_ptr;          // hardware causes this to advance
601 +   uint32_t           read_ptr;           // this module does the reading
602 +   int                active;             // is this slot in the hardware dma fifo?
603 +   uint32_t           msgs_parsed;        // count how many messages are in this slot
604 +   uint32_t           msgs_released;      // how many messages have been released
605 +   void              *state;              // connection state information
606 +   uint8_t            ref_count[VCHI_MAX_SERVICES_PER_CONNECTION];          // reference count for slots held by services
607 +} RX_MSG_SLOTINFO_T;
608 +
609 +// The message driver no longer needs to know about the fields of RX_BULK_SLOTINFO_T - sort this out.
610 +// In particular, it mustn't use addr and len - they're the client buffer, but the message
611 +// driver will be tasked with sending the aligned core section.
612 +typedef struct rx_bulk_slotinfo_t {
613 +   struct rx_bulk_slotinfo_t *next;
614 +
615 +   struct semaphore *blocking;
616 +
617 +   // needed by DMA
618 +   void        *addr;
619 +   uint32_t     len;
620 +
621 +   // needed for the callback
622 +   void        *service;
623 +   void        *handle;
624 +   VCHI_FLAGS_T flags;
625 +} RX_BULK_SLOTINFO_T;
626 +
627 +
628 +/* ----------------------------------------------------------------------
629 + * each connection driver will have a pool of the following struct.
630 + *
631 + * the pool will be managed by vchi_qman_*
632 + * this means there will be multiple queues (single linked lists)
633 + * a given struct message_info will be on exactly one of these queues
634 + * at any one time
635 + * -------------------------------------------------------------------- */
636 +typedef struct rx_message_info {
637 +
638 +   struct message_info *next;
639 +   //struct message_info *prev;
640 +
641 +   uint8_t    *addr;
642 +   uint32_t   len;
643 +   RX_MSG_SLOTINFO_T *slot; // points to whichever slot contains this message
644 +   uint32_t   tx_timestamp;
645 +   uint32_t   rx_timestamp;
646 +
647 +} RX_MESSAGE_INFO_T;
648 +
649 +typedef struct {
650 +   MESSAGE_EVENT_TYPE_T type;
651 +
652 +   struct {
653 +      // for messages
654 +      void    *addr;           // address of message
655 +      uint16_t slot_delta;     // whether this message indicated slot delta
656 +      uint32_t len;            // length of message
657 +      RX_MSG_SLOTINFO_T *slot; // slot this message is in
658 +      int32_t  service;   // service id this message is destined for
659 +      uint32_t tx_timestamp;   // timestamp from the header
660 +      uint32_t rx_timestamp;   // timestamp when we parsed it
661 +   } message;
662 +
663 +   // FIXME: cleanup slot reporting...
664 +   RX_MSG_SLOTINFO_T *rx_msg;
665 +   RX_BULK_SLOTINFO_T *rx_bulk;
666 +   void *tx_handle;
667 +   MESSAGE_TX_CHANNEL_T tx_channel;
668 +
669 +} MESSAGE_EVENT_T;
670 +
671 +
672 +// callbacks
673 +typedef void VCHI_MESSAGE_DRIVER_EVENT_CALLBACK_T( void *state );
674 +
675 +typedef struct {
676 +   VCHI_MESSAGE_DRIVER_EVENT_CALLBACK_T *event_callback;
677 +} VCHI_MESSAGE_DRIVER_OPEN_T;
678 +
679 +
680 +// handle to this instance of message driver (as returned by ->open)
681 +typedef struct opaque_mhandle_t *VCHI_MDRIVER_HANDLE_T;
682 +
683 +struct opaque_vchi_message_driver_t {
684 +   VCHI_MDRIVER_HANDLE_T *(*open)( VCHI_MESSAGE_DRIVER_OPEN_T *params, void *state );
685 +   int32_t (*suspending)( VCHI_MDRIVER_HANDLE_T *handle );
686 +   int32_t (*resumed)( VCHI_MDRIVER_HANDLE_T *handle );
687 +   int32_t (*power_control)( VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_TX_CHANNEL_T, int32_t enable );
688 +   int32_t (*add_msg_rx_slot)( VCHI_MDRIVER_HANDLE_T *handle, RX_MSG_SLOTINFO_T *slot );      // rx message
689 +   int32_t (*add_bulk_rx)( VCHI_MDRIVER_HANDLE_T *handle, void *data, uint32_t len, RX_BULK_SLOTINFO_T *slot );  // rx data (bulk)
690 +   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)
691 +   void    (*next_event)( VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_EVENT_T *event );     // get the next event from message_driver
692 +   int32_t (*enable)( VCHI_MDRIVER_HANDLE_T *handle );
693 +   int32_t (*form_message)( VCHI_MDRIVER_HANDLE_T *handle, int32_t service_id, VCHI_MSG_VECTOR_T *vector, uint32_t count, void
694 +                            *address, uint32_t length_avail, uint32_t max_total_length, int32_t pad_to_fill, int32_t allow_partial );
695 +
696 +   int32_t (*update_message)( VCHI_MDRIVER_HANDLE_T *handle, void *dest, int16_t *slot_count );
697 +   int32_t (*buffer_aligned)( VCHI_MDRIVER_HANDLE_T *handle, int tx, int uncached, const void *address, const uint32_t length );
698 +   void *  (*allocate_buffer)( VCHI_MDRIVER_HANDLE_T *handle, uint32_t *length );
699 +   void    (*free_buffer)( VCHI_MDRIVER_HANDLE_T *handle, void *address );
700 +   int     (*rx_slot_size)( VCHI_MDRIVER_HANDLE_T *handle, int msg_size );
701 +   int     (*tx_slot_size)( VCHI_MDRIVER_HANDLE_T *handle, int msg_size );
702 +
703 +   int32_t  (*tx_supports_terminate)( const VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_TX_CHANNEL_T channel );
704 +   uint32_t (*tx_bulk_chunk_size)( const VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_TX_CHANNEL_T channel );
705 +   int     (*tx_alignment)( const VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_TX_CHANNEL_T channel );
706 +   int     (*rx_alignment)( const VCHI_MDRIVER_HANDLE_T *handle, MESSAGE_RX_CHANNEL_T channel );
707 +   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 );
708 +   void    (*debug)( VCHI_MDRIVER_HANDLE_T *handle );
709 +};
710 +
711 +
712 +#endif // _VCHI_MESSAGE_H_
713 +
714 +/****************************** End of file ***********************************/
715 --- /dev/null
716 +++ b/drivers/misc/vc04_services/interface/vchi/vchi.h
717 @@ -0,0 +1,378 @@
718 +/**
719 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
720 + *
721 + * Redistribution and use in source and binary forms, with or without
722 + * modification, are permitted provided that the following conditions
723 + * are met:
724 + * 1. Redistributions of source code must retain the above copyright
725 + *    notice, this list of conditions, and the following disclaimer,
726 + *    without modification.
727 + * 2. Redistributions in binary form must reproduce the above copyright
728 + *    notice, this list of conditions and the following disclaimer in the
729 + *    documentation and/or other materials provided with the distribution.
730 + * 3. The names of the above-listed copyright holders may not be used
731 + *    to endorse or promote products derived from this software without
732 + *    specific prior written permission.
733 + *
734 + * ALTERNATIVELY, this software may be distributed under the terms of the
735 + * GNU General Public License ("GPL") version 2, as published by the Free
736 + * Software Foundation.
737 + *
738 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
739 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
740 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
741 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
742 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
743 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
744 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
745 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
746 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
747 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
748 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
749 + */
750 +
751 +#ifndef VCHI_H_
752 +#define VCHI_H_
753 +
754 +#include "interface/vchi/vchi_cfg.h"
755 +#include "interface/vchi/vchi_common.h"
756 +#include "interface/vchi/connections/connection.h"
757 +#include "vchi_mh.h"
758 +
759 +
760 +/******************************************************************************
761 + Global defs
762 + *****************************************************************************/
763 +
764 +#define VCHI_BULK_ROUND_UP(x)     ((((unsigned long)(x))+VCHI_BULK_ALIGN-1) & ~(VCHI_BULK_ALIGN-1))
765 +#define VCHI_BULK_ROUND_DOWN(x)   (((unsigned long)(x)) & ~(VCHI_BULK_ALIGN-1))
766 +#define VCHI_BULK_ALIGN_NBYTES(x) (VCHI_BULK_ALIGNED(x) ? 0 : (VCHI_BULK_ALIGN - ((unsigned long)(x) & (VCHI_BULK_ALIGN-1))))
767 +
768 +#ifdef USE_VCHIQ_ARM
769 +#define VCHI_BULK_ALIGNED(x)      1
770 +#else
771 +#define VCHI_BULK_ALIGNED(x)      (((unsigned long)(x) & (VCHI_BULK_ALIGN-1)) == 0)
772 +#endif
773 +
774 +struct vchi_version {
775 +       uint32_t version;
776 +       uint32_t version_min;
777 +};
778 +#define VCHI_VERSION(v_) { v_, v_ }
779 +#define VCHI_VERSION_EX(v_, m_) { v_, m_ }
780 +
781 +typedef enum
782 +{
783 +   VCHI_VEC_POINTER,
784 +   VCHI_VEC_HANDLE,
785 +   VCHI_VEC_LIST
786 +} VCHI_MSG_VECTOR_TYPE_T;
787 +
788 +typedef struct vchi_msg_vector_ex {
789 +
790 +   VCHI_MSG_VECTOR_TYPE_T type;
791 +   union
792 +   {
793 +      // a memory handle
794 +      struct
795 +      {
796 +         VCHI_MEM_HANDLE_T handle;
797 +         uint32_t offset;
798 +         int32_t vec_len;
799 +      } handle;
800 +
801 +      // an ordinary data pointer
802 +      struct
803 +      {
804 +         const void *vec_base;
805 +         int32_t vec_len;
806 +      } ptr;
807 +
808 +      // a nested vector list
809 +      struct
810 +      {
811 +         struct vchi_msg_vector_ex *vec;
812 +         uint32_t vec_len;
813 +      } list;
814 +   } u;
815 +} VCHI_MSG_VECTOR_EX_T;
816 +
817 +
818 +// Construct an entry in a msg vector for a pointer (p) of length (l)
819 +#define VCHI_VEC_POINTER(p,l)  VCHI_VEC_POINTER, { { (VCHI_MEM_HANDLE_T)(p), (l) } }
820 +
821 +// Construct an entry in a msg vector for a message handle (h), starting at offset (o) of length (l)
822 +#define VCHI_VEC_HANDLE(h,o,l) VCHI_VEC_HANDLE,  { { (h), (o), (l) } }
823 +
824 +// Macros to manipulate 'FOURCC' values
825 +#define MAKE_FOURCC(x) ((int32_t)( (x[0] << 24) | (x[1] << 16) | (x[2] << 8) | x[3] ))
826 +#define FOURCC_TO_CHAR(x) (x >> 24) & 0xFF,(x >> 16) & 0xFF,(x >> 8) & 0xFF, x & 0xFF
827 +
828 +
829 +// Opaque service information
830 +struct opaque_vchi_service_t;
831 +
832 +// Descriptor for a held message. Allocated by client, initialised by vchi_msg_hold,
833 +// vchi_msg_iter_hold or vchi_msg_iter_hold_next. Fields are for internal VCHI use only.
834 +typedef struct
835 +{
836 +   struct opaque_vchi_service_t *service;
837 +   void *message;
838 +} VCHI_HELD_MSG_T;
839 +
840 +
841 +
842 +// structure used to provide the information needed to open a server or a client
843 +typedef struct {
844 +       struct vchi_version version;
845 +       int32_t service_id;
846 +       VCHI_CONNECTION_T *connection;
847 +       uint32_t rx_fifo_size;
848 +       uint32_t tx_fifo_size;
849 +       VCHI_CALLBACK_T callback;
850 +       void *callback_param;
851 +       /* client intends to receive bulk transfers of
852 +               odd lengths or into unaligned buffers */
853 +       int32_t want_unaligned_bulk_rx;
854 +       /* client intends to transmit bulk transfers of
855 +               odd lengths or out of unaligned buffers */
856 +       int32_t want_unaligned_bulk_tx;
857 +       /* client wants to check CRCs on (bulk) xfers.
858 +               Only needs to be set at 1 end - will do both directions. */
859 +       int32_t want_crc;
860 +} SERVICE_CREATION_T;
861 +
862 +// Opaque handle for a VCHI instance
863 +typedef struct opaque_vchi_instance_handle_t *VCHI_INSTANCE_T;
864 +
865 +// Opaque handle for a server or client
866 +typedef struct opaque_vchi_service_handle_t *VCHI_SERVICE_HANDLE_T;
867 +
868 +// Service registration & startup
869 +typedef void (*VCHI_SERVICE_INIT)(VCHI_INSTANCE_T initialise_instance, VCHI_CONNECTION_T **connections, uint32_t num_connections);
870 +
871 +typedef struct service_info_tag {
872 +   const char * const vll_filename; /* VLL to load to start this service. This is an empty string if VLL is "static" */
873 +   VCHI_SERVICE_INIT init;          /* Service initialisation function */
874 +   void *vll_handle;                /* VLL handle; NULL when unloaded or a "static VLL" in build */
875 +} SERVICE_INFO_T;
876 +
877 +/******************************************************************************
878 + Global funcs - implementation is specific to which side you are on (local / remote)
879 + *****************************************************************************/
880 +
881 +#ifdef __cplusplus
882 +extern "C" {
883 +#endif
884 +
885 +extern /*@observer@*/ VCHI_CONNECTION_T * vchi_create_connection( const VCHI_CONNECTION_API_T * function_table,
886 +                                                   const VCHI_MESSAGE_DRIVER_T * low_level);
887 +
888 +
889 +// Routine used to initialise the vchi on both local + remote connections
890 +extern int32_t vchi_initialise( VCHI_INSTANCE_T *instance_handle );
891 +
892 +extern int32_t vchi_exit( void );
893 +
894 +extern int32_t vchi_connect( VCHI_CONNECTION_T **connections,
895 +                             const uint32_t num_connections,
896 +                             VCHI_INSTANCE_T instance_handle );
897 +
898 +//When this is called, ensure that all services have no data pending.
899 +//Bulk transfers can remain 'queued'
900 +extern int32_t vchi_disconnect( VCHI_INSTANCE_T instance_handle );
901 +
902 +// Global control over bulk CRC checking
903 +extern int32_t vchi_crc_control( VCHI_CONNECTION_T *connection,
904 +                                 VCHI_CRC_CONTROL_T control );
905 +
906 +// helper functions
907 +extern void * vchi_allocate_buffer(VCHI_SERVICE_HANDLE_T handle, uint32_t *length);
908 +extern void vchi_free_buffer(VCHI_SERVICE_HANDLE_T handle, void *address);
909 +extern uint32_t vchi_current_time(VCHI_INSTANCE_T instance_handle);
910 +
911 +
912 +/******************************************************************************
913 + Global service API
914 + *****************************************************************************/
915 +// Routine to create a named service
916 +extern int32_t vchi_service_create( VCHI_INSTANCE_T instance_handle,
917 +                                    SERVICE_CREATION_T *setup,
918 +                                    VCHI_SERVICE_HANDLE_T *handle );
919 +
920 +// Routine to destory a service
921 +extern int32_t vchi_service_destroy( const VCHI_SERVICE_HANDLE_T handle );
922 +
923 +// Routine to open a named service
924 +extern int32_t vchi_service_open( VCHI_INSTANCE_T instance_handle,
925 +                                  SERVICE_CREATION_T *setup,
926 +                                  VCHI_SERVICE_HANDLE_T *handle);
927 +
928 +extern int32_t vchi_get_peer_version( const VCHI_SERVICE_HANDLE_T handle,
929 +                                      short *peer_version );
930 +
931 +// Routine to close a named service
932 +extern int32_t vchi_service_close( const VCHI_SERVICE_HANDLE_T handle );
933 +
934 +// Routine to increment ref count on a named service
935 +extern int32_t vchi_service_use( const VCHI_SERVICE_HANDLE_T handle );
936 +
937 +// Routine to decrement ref count on a named service
938 +extern int32_t vchi_service_release( const VCHI_SERVICE_HANDLE_T handle );
939 +
940 +// Routine to set a control option for a named service
941 +extern int32_t vchi_service_set_option( const VCHI_SERVICE_HANDLE_T handle,
942 +                                       VCHI_SERVICE_OPTION_T option,
943 +                                       int value);
944 +
945 +// Routine to send a message across a service
946 +extern int32_t vchi_msg_queue( VCHI_SERVICE_HANDLE_T handle,
947 +                               const void *data,
948 +                               uint32_t data_size,
949 +                               VCHI_FLAGS_T flags,
950 +                               void *msg_handle );
951 +
952 +// scatter-gather (vector) and send message
953 +int32_t vchi_msg_queuev_ex( VCHI_SERVICE_HANDLE_T handle,
954 +                            VCHI_MSG_VECTOR_EX_T *vector,
955 +                            uint32_t count,
956 +                            VCHI_FLAGS_T flags,
957 +                            void *msg_handle );
958 +
959 +// legacy scatter-gather (vector) and send message, only handles pointers
960 +int32_t vchi_msg_queuev( VCHI_SERVICE_HANDLE_T handle,
961 +                         VCHI_MSG_VECTOR_T *vector,
962 +                         uint32_t count,
963 +                         VCHI_FLAGS_T flags,
964 +                         void *msg_handle );
965 +
966 +// Routine to receive a msg from a service
967 +// Dequeue is equivalent to hold, copy into client buffer, release
968 +extern int32_t vchi_msg_dequeue( VCHI_SERVICE_HANDLE_T handle,
969 +                                 void *data,
970 +                                 uint32_t max_data_size_to_read,
971 +                                 uint32_t *actual_msg_size,
972 +                                 VCHI_FLAGS_T flags );
973 +
974 +// Routine to look at a message in place.
975 +// The message is not dequeued, so a subsequent call to peek or dequeue
976 +// will return the same message.
977 +extern int32_t vchi_msg_peek( VCHI_SERVICE_HANDLE_T handle,
978 +                              void **data,
979 +                              uint32_t *msg_size,
980 +                              VCHI_FLAGS_T flags );
981 +
982 +// Routine to remove a message after it has been read in place with peek
983 +// The first message on the queue is dequeued.
984 +extern int32_t vchi_msg_remove( VCHI_SERVICE_HANDLE_T handle );
985 +
986 +// Routine to look at a message in place.
987 +// The message is dequeued, so the caller is left holding it; the descriptor is
988 +// filled in and must be released when the user has finished with the message.
989 +extern int32_t vchi_msg_hold( VCHI_SERVICE_HANDLE_T handle,
990 +                              void **data,        // } may be NULL, as info can be
991 +                              uint32_t *msg_size, // } obtained from HELD_MSG_T
992 +                              VCHI_FLAGS_T flags,
993 +                              VCHI_HELD_MSG_T *message_descriptor );
994 +
995 +// Initialise an iterator to look through messages in place
996 +extern int32_t vchi_msg_look_ahead( VCHI_SERVICE_HANDLE_T handle,
997 +                                    VCHI_MSG_ITER_T *iter,
998 +                                    VCHI_FLAGS_T flags );
999 +
1000 +/******************************************************************************
1001 + Global service support API - operations on held messages and message iterators
1002 + *****************************************************************************/
1003 +
1004 +// Routine to get the address of a held message
1005 +extern void *vchi_held_msg_ptr( const VCHI_HELD_MSG_T *message );
1006 +
1007 +// Routine to get the size of a held message
1008 +extern int32_t vchi_held_msg_size( const VCHI_HELD_MSG_T *message );
1009 +
1010 +// Routine to get the transmit timestamp as written into the header by the peer
1011 +extern uint32_t vchi_held_msg_tx_timestamp( const VCHI_HELD_MSG_T *message );
1012 +
1013 +// Routine to get the reception timestamp, written as we parsed the header
1014 +extern uint32_t vchi_held_msg_rx_timestamp( const VCHI_HELD_MSG_T *message );
1015 +
1016 +// Routine to release a held message after it has been processed
1017 +extern int32_t vchi_held_msg_release( VCHI_HELD_MSG_T *message );
1018 +
1019 +// Indicates whether the iterator has a next message.
1020 +extern int32_t vchi_msg_iter_has_next( const VCHI_MSG_ITER_T *iter );
1021 +
1022 +// Return the pointer and length for the next message and advance the iterator.
1023 +extern int32_t vchi_msg_iter_next( VCHI_MSG_ITER_T *iter,
1024 +                                   void **data,
1025 +                                   uint32_t *msg_size );
1026 +
1027 +// Remove the last message returned by vchi_msg_iter_next.
1028 +// Can only be called once after each call to vchi_msg_iter_next.
1029 +extern int32_t vchi_msg_iter_remove( VCHI_MSG_ITER_T *iter );
1030 +
1031 +// Hold the last message returned by vchi_msg_iter_next.
1032 +// Can only be called once after each call to vchi_msg_iter_next.
1033 +extern int32_t vchi_msg_iter_hold( VCHI_MSG_ITER_T *iter,
1034 +                                   VCHI_HELD_MSG_T *message );
1035 +
1036 +// Return information for the next message, and hold it, advancing the iterator.
1037 +extern int32_t vchi_msg_iter_hold_next( VCHI_MSG_ITER_T *iter,
1038 +                                        void **data,        // } may be NULL
1039 +                                        uint32_t *msg_size, // }
1040 +                                        VCHI_HELD_MSG_T *message );
1041 +
1042 +
1043 +/******************************************************************************
1044 + Global bulk API
1045 + *****************************************************************************/
1046 +
1047 +// Routine to prepare interface for a transfer from the other side
1048 +extern int32_t vchi_bulk_queue_receive( VCHI_SERVICE_HANDLE_T handle,
1049 +                                        void *data_dst,
1050 +                                        uint32_t data_size,
1051 +                                        VCHI_FLAGS_T flags,
1052 +                                        void *transfer_handle );
1053 +
1054 +
1055 +// Prepare interface for a transfer from the other side into relocatable memory.
1056 +int32_t vchi_bulk_queue_receive_reloc( const VCHI_SERVICE_HANDLE_T handle,
1057 +                                       VCHI_MEM_HANDLE_T h_dst,
1058 +                                       uint32_t offset,
1059 +                                       uint32_t data_size,
1060 +                                       const VCHI_FLAGS_T flags,
1061 +                                       void * const bulk_handle );
1062 +
1063 +// Routine to queue up data ready for transfer to the other (once they have signalled they are ready)
1064 +extern int32_t vchi_bulk_queue_transmit( VCHI_SERVICE_HANDLE_T handle,
1065 +                                         const void *data_src,
1066 +                                         uint32_t data_size,
1067 +                                         VCHI_FLAGS_T flags,
1068 +                                         void *transfer_handle );
1069 +
1070 +
1071 +/******************************************************************************
1072 + Configuration plumbing
1073 + *****************************************************************************/
1074 +
1075 +// function prototypes for the different mid layers (the state info gives the different physical connections)
1076 +extern const VCHI_CONNECTION_API_T *single_get_func_table( void );
1077 +//extern const VCHI_CONNECTION_API_T *local_server_get_func_table( void );
1078 +//extern const VCHI_CONNECTION_API_T *local_client_get_func_table( void );
1079 +
1080 +// declare all message drivers here
1081 +const VCHI_MESSAGE_DRIVER_T *vchi_mphi_message_driver_func_table( void );
1082 +
1083 +#ifdef __cplusplus
1084 +}
1085 +#endif
1086 +
1087 +extern int32_t vchi_bulk_queue_transmit_reloc( VCHI_SERVICE_HANDLE_T handle,
1088 +                                               VCHI_MEM_HANDLE_T h_src,
1089 +                                               uint32_t offset,
1090 +                                               uint32_t data_size,
1091 +                                               VCHI_FLAGS_T flags,
1092 +                                               void *transfer_handle );
1093 +#endif /* VCHI_H_ */
1094 +
1095 +/****************************** End of file **********************************/
1096 --- /dev/null
1097 +++ b/drivers/misc/vc04_services/interface/vchi/vchi_cfg.h
1098 @@ -0,0 +1,224 @@
1099 +/**
1100 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
1101 + *
1102 + * Redistribution and use in source and binary forms, with or without
1103 + * modification, are permitted provided that the following conditions
1104 + * are met:
1105 + * 1. Redistributions of source code must retain the above copyright
1106 + *    notice, this list of conditions, and the following disclaimer,
1107 + *    without modification.
1108 + * 2. Redistributions in binary form must reproduce the above copyright
1109 + *    notice, this list of conditions and the following disclaimer in the
1110 + *    documentation and/or other materials provided with the distribution.
1111 + * 3. The names of the above-listed copyright holders may not be used
1112 + *    to endorse or promote products derived from this software without
1113 + *    specific prior written permission.
1114 + *
1115 + * ALTERNATIVELY, this software may be distributed under the terms of the
1116 + * GNU General Public License ("GPL") version 2, as published by the Free
1117 + * Software Foundation.
1118 + *
1119 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
1120 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
1121 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1122 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
1123 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
1124 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
1125 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
1126 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
1127 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
1128 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
1129 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1130 + */
1131 +
1132 +#ifndef VCHI_CFG_H_
1133 +#define VCHI_CFG_H_
1134 +
1135 +/****************************************************************************************
1136 + * Defines in this first section are part of the VCHI API and may be examined by VCHI
1137 + * services.
1138 + ***************************************************************************************/
1139 +
1140 +/* Required alignment of base addresses for bulk transfer, if unaligned transfers are not enabled */
1141 +/* Really determined by the message driver, and should be available from a run-time call. */
1142 +#ifndef VCHI_BULK_ALIGN
1143 +#   if __VCCOREVER__ >= 0x04000000
1144 +#       define VCHI_BULK_ALIGN 32 // Allows for the need to do cache cleans
1145 +#   else
1146 +#       define VCHI_BULK_ALIGN 16
1147 +#   endif
1148 +#endif
1149 +
1150 +/* Required length multiple for bulk transfers, if unaligned transfers are not enabled */
1151 +/* May be less than or greater than VCHI_BULK_ALIGN */
1152 +/* Really determined by the message driver, and should be available from a run-time call. */
1153 +#ifndef VCHI_BULK_GRANULARITY
1154 +#   if __VCCOREVER__ >= 0x04000000
1155 +#       define VCHI_BULK_GRANULARITY 32 // Allows for the need to do cache cleans
1156 +#   else
1157 +#       define VCHI_BULK_GRANULARITY 16
1158 +#   endif
1159 +#endif
1160 +
1161 +/* The largest possible message to be queued with vchi_msg_queue. */
1162 +#ifndef VCHI_MAX_MSG_SIZE
1163 +#   if defined VCHI_LOCAL_HOST_PORT
1164 +#       define VCHI_MAX_MSG_SIZE     16384         // makes file transfers fast, but should they be using bulk?
1165 +#   else
1166 +#       define VCHI_MAX_MSG_SIZE      4096 // NOTE: THIS MUST BE LARGER THAN OR EQUAL TO THE SIZE OF THE KHRONOS MERGE BUFFER!!
1167 +#   endif
1168 +#endif
1169 +
1170 +/******************************************************************************************
1171 + * Defines below are system configuration options, and should not be used by VCHI services.
1172 + *****************************************************************************************/
1173 +
1174 +/* How many connections can we support? A localhost implementation uses 2 connections,
1175 + * 1 for host-app, 1 for VMCS, and these are hooked together by a loopback MPHI VCFW
1176 + * driver. */
1177 +#ifndef VCHI_MAX_NUM_CONNECTIONS
1178 +#   define VCHI_MAX_NUM_CONNECTIONS 3
1179 +#endif
1180 +
1181 +/* How many services can we open per connection? Extending this doesn't cost processing time, just a small
1182 + * amount of static memory. */
1183 +#ifndef VCHI_MAX_SERVICES_PER_CONNECTION
1184 +#  define VCHI_MAX_SERVICES_PER_CONNECTION 36
1185 +#endif
1186 +
1187 +/* Adjust if using a message driver that supports more logical TX channels */
1188 +#ifndef VCHI_MAX_BULK_TX_CHANNELS_PER_CONNECTION
1189 +#   define VCHI_MAX_BULK_TX_CHANNELS_PER_CONNECTION 9 // 1 MPHI + 8 CCP2 logical channels
1190 +#endif
1191 +
1192 +/* Adjust if using a message driver that supports more logical RX channels */
1193 +#ifndef VCHI_MAX_BULK_RX_CHANNELS_PER_CONNECTION
1194 +#   define VCHI_MAX_BULK_RX_CHANNELS_PER_CONNECTION 1 // 1 MPHI
1195 +#endif
1196 +
1197 +/* How many receive slots do we use. This times VCHI_MAX_MSG_SIZE gives the effective
1198 + * receive queue space, less message headers. */
1199 +#ifndef VCHI_NUM_READ_SLOTS
1200 +#  if defined(VCHI_LOCAL_HOST_PORT)
1201 +#     define VCHI_NUM_READ_SLOTS 4
1202 +#  else
1203 +#     define VCHI_NUM_READ_SLOTS 48
1204 +#  endif
1205 +#endif
1206 +
1207 +/* Do we utilise overrun facility for receive message slots? Can aid peer transmit
1208 + * performance. Only define on VideoCore end, talking to host.
1209 + */
1210 +//#define VCHI_MSG_RX_OVERRUN
1211 +
1212 +/* How many transmit slots do we use. Generally don't need many, as the hardware driver
1213 + * underneath VCHI will usually have its own buffering. */
1214 +#ifndef VCHI_NUM_WRITE_SLOTS
1215 +#  define VCHI_NUM_WRITE_SLOTS 4
1216 +#endif
1217 +
1218 +/* If a service has held or queued received messages in VCHI_XOFF_THRESHOLD or more slots,
1219 + * then it's taking up too much buffer space, and the peer service will be told to stop
1220 + * transmitting with an XOFF message. For this to be effective, the VCHI_NUM_READ_SLOTS
1221 + * needs to be considerably bigger than VCHI_NUM_WRITE_SLOTS, or the transmit latency
1222 + * is too high. */
1223 +#ifndef VCHI_XOFF_THRESHOLD
1224 +#  define VCHI_XOFF_THRESHOLD (VCHI_NUM_READ_SLOTS / 2)
1225 +#endif
1226 +
1227 +/* After we've sent an XOFF, the peer will be told to resume transmission once the local
1228 + * service has dequeued/released enough messages that it's now occupying
1229 + * VCHI_XON_THRESHOLD slots or fewer. */
1230 +#ifndef VCHI_XON_THRESHOLD
1231 +#  define VCHI_XON_THRESHOLD (VCHI_NUM_READ_SLOTS / 4)
1232 +#endif
1233 +
1234 +/* A size below which a bulk transfer omits the handshake completely and always goes
1235 + * via the message channel, if bulk auxiliary is being sent on that service. (The user
1236 + * can guarantee this by enabling unaligned transmits).
1237 + * Not API. */
1238 +#ifndef VCHI_MIN_BULK_SIZE
1239 +#  define VCHI_MIN_BULK_SIZE    ( VCHI_MAX_MSG_SIZE / 2 < 4096 ? VCHI_MAX_MSG_SIZE / 2 : 4096 )
1240 +#endif
1241 +
1242 +/* Maximum size of bulk transmission chunks, for each interface type. A trade-off between
1243 + * speed and latency; the smaller the chunk size the better change of messages and other
1244 + * bulk transmissions getting in when big bulk transfers are happening. Set to 0 to not
1245 + * break transmissions into chunks.
1246 + */
1247 +#ifndef VCHI_MAX_BULK_CHUNK_SIZE_MPHI
1248 +#  define VCHI_MAX_BULK_CHUNK_SIZE_MPHI (16 * 1024)
1249 +#endif
1250 +
1251 +/* NB Chunked CCP2 transmissions violate the letter of the CCP2 spec by using "JPEG8" mode
1252 + * with multiple-line frames. Only use if the receiver can cope. */
1253 +#ifndef VCHI_MAX_BULK_CHUNK_SIZE_CCP2
1254 +#  define VCHI_MAX_BULK_CHUNK_SIZE_CCP2 0
1255 +#endif
1256 +
1257 +/* How many TX messages can we have pending in our transmit slots. Once exhausted,
1258 + * vchi_msg_queue will be blocked. */
1259 +#ifndef VCHI_TX_MSG_QUEUE_SIZE
1260 +#  define VCHI_TX_MSG_QUEUE_SIZE           256
1261 +#endif
1262 +
1263 +/* How many RX messages can we have parsed in the receive slots. Once exhausted, parsing
1264 + * will be suspended until older messages are dequeued/released. */
1265 +#ifndef VCHI_RX_MSG_QUEUE_SIZE
1266 +#  define VCHI_RX_MSG_QUEUE_SIZE           256
1267 +#endif
1268 +
1269 +/* Really should be able to cope if we run out of received message descriptors, by
1270 + * suspending parsing as the comment above says, but we don't. This sweeps the issue
1271 + * under the carpet. */
1272 +#if VCHI_RX_MSG_QUEUE_SIZE < (VCHI_MAX_MSG_SIZE/16 + 1) * VCHI_NUM_READ_SLOTS
1273 +#  undef VCHI_RX_MSG_QUEUE_SIZE
1274 +#  define VCHI_RX_MSG_QUEUE_SIZE (VCHI_MAX_MSG_SIZE/16 + 1) * VCHI_NUM_READ_SLOTS
1275 +#endif
1276 +
1277 +/* How many bulk transmits can we have pending. Once exhausted, vchi_bulk_queue_transmit
1278 + * will be blocked. */
1279 +#ifndef VCHI_TX_BULK_QUEUE_SIZE
1280 +#  define VCHI_TX_BULK_QUEUE_SIZE           64
1281 +#endif
1282 +
1283 +/* How many bulk receives can we have pending. Once exhausted, vchi_bulk_queue_receive
1284 + * will be blocked. */
1285 +#ifndef VCHI_RX_BULK_QUEUE_SIZE
1286 +#  define VCHI_RX_BULK_QUEUE_SIZE           64
1287 +#endif
1288 +
1289 +/* A limit on how many outstanding bulk requests we expect the peer to give us. If
1290 + * the peer asks for more than this, VCHI will fail and assert. The number is determined
1291 + * by the peer's hardware - it's the number of outstanding requests that can be queued
1292 + * on all bulk channels. VC3's MPHI peripheral allows 16. */
1293 +#ifndef VCHI_MAX_PEER_BULK_REQUESTS
1294 +#  define VCHI_MAX_PEER_BULK_REQUESTS       32
1295 +#endif
1296 +
1297 +/* Define VCHI_CCP2TX_MANUAL_POWER if the host tells us when to turn the CCP2
1298 + * transmitter on and off.
1299 + */
1300 +/*#define VCHI_CCP2TX_MANUAL_POWER*/
1301 +
1302 +#ifndef VCHI_CCP2TX_MANUAL_POWER
1303 +
1304 +/* Timeout (in milliseconds) for putting the CCP2TX interface into IDLE state. Set
1305 + * negative for no IDLE.
1306 + */
1307 +#  ifndef VCHI_CCP2TX_IDLE_TIMEOUT
1308 +#    define VCHI_CCP2TX_IDLE_TIMEOUT        5
1309 +#  endif
1310 +
1311 +/* Timeout (in milliseconds) for putting the CCP2TX interface into OFF state. Set
1312 + * negative for no OFF.
1313 + */
1314 +#  ifndef VCHI_CCP2TX_OFF_TIMEOUT
1315 +#    define VCHI_CCP2TX_OFF_TIMEOUT         1000
1316 +#  endif
1317 +
1318 +#endif /* VCHI_CCP2TX_MANUAL_POWER */
1319 +
1320 +#endif /* VCHI_CFG_H_ */
1321 +
1322 +/****************************** End of file **********************************/
1323 --- /dev/null
1324 +++ b/drivers/misc/vc04_services/interface/vchi/vchi_cfg_internal.h
1325 @@ -0,0 +1,71 @@
1326 +/**
1327 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
1328 + *
1329 + * Redistribution and use in source and binary forms, with or without
1330 + * modification, are permitted provided that the following conditions
1331 + * are met:
1332 + * 1. Redistributions of source code must retain the above copyright
1333 + *    notice, this list of conditions, and the following disclaimer,
1334 + *    without modification.
1335 + * 2. Redistributions in binary form must reproduce the above copyright
1336 + *    notice, this list of conditions and the following disclaimer in the
1337 + *    documentation and/or other materials provided with the distribution.
1338 + * 3. The names of the above-listed copyright holders may not be used
1339 + *    to endorse or promote products derived from this software without
1340 + *    specific prior written permission.
1341 + *
1342 + * ALTERNATIVELY, this software may be distributed under the terms of the
1343 + * GNU General Public License ("GPL") version 2, as published by the Free
1344 + * Software Foundation.
1345 + *
1346 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
1347 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
1348 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1349 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
1350 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
1351 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
1352 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
1353 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
1354 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
1355 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
1356 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1357 + */
1358 +
1359 +#ifndef VCHI_CFG_INTERNAL_H_
1360 +#define VCHI_CFG_INTERNAL_H_
1361 +
1362 +/****************************************************************************************
1363 + * Control optimisation attempts.
1364 + ***************************************************************************************/
1365 +
1366 +// Don't use lots of short-term locks - use great long ones, reducing the overall locks-per-second
1367 +#define VCHI_COARSE_LOCKING
1368 +
1369 +// Avoid lock then unlock on exit from blocking queue operations (msg tx, bulk rx/tx)
1370 +// (only relevant if VCHI_COARSE_LOCKING)
1371 +#define VCHI_ELIDE_BLOCK_EXIT_LOCK
1372 +
1373 +// Avoid lock on non-blocking peek
1374 +// (only relevant if VCHI_COARSE_LOCKING)
1375 +#define VCHI_AVOID_PEEK_LOCK
1376 +
1377 +// Use one slot-handler thread per connection, rather than 1 thread dealing with all connections in rotation.
1378 +#define VCHI_MULTIPLE_HANDLER_THREADS
1379 +
1380 +// Put free descriptors onto the head of the free queue, rather than the tail, so that we don't thrash
1381 +// our way through the pool of descriptors.
1382 +#define VCHI_PUSH_FREE_DESCRIPTORS_ONTO_HEAD
1383 +
1384 +// Don't issue a MSG_AVAILABLE callback for every single message. Possibly only safe if VCHI_COARSE_LOCKING.
1385 +#define VCHI_FEWER_MSG_AVAILABLE_CALLBACKS
1386 +
1387 +// Don't use message descriptors for TX messages that don't need them
1388 +#define VCHI_MINIMISE_TX_MSG_DESCRIPTORS
1389 +
1390 +// Nano-locks for multiqueue
1391 +//#define VCHI_MQUEUE_NANOLOCKS
1392 +
1393 +// Lock-free(er) dequeuing
1394 +//#define VCHI_RX_NANOLOCKS
1395 +
1396 +#endif /*VCHI_CFG_INTERNAL_H_*/
1397 --- /dev/null
1398 +++ b/drivers/misc/vc04_services/interface/vchi/vchi_common.h
1399 @@ -0,0 +1,174 @@
1400 +/**
1401 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
1402 + *
1403 + * Redistribution and use in source and binary forms, with or without
1404 + * modification, are permitted provided that the following conditions
1405 + * are met:
1406 + * 1. Redistributions of source code must retain the above copyright
1407 + *    notice, this list of conditions, and the following disclaimer,
1408 + *    without modification.
1409 + * 2. Redistributions in binary form must reproduce the above copyright
1410 + *    notice, this list of conditions and the following disclaimer in the
1411 + *    documentation and/or other materials provided with the distribution.
1412 + * 3. The names of the above-listed copyright holders may not be used
1413 + *    to endorse or promote products derived from this software without
1414 + *    specific prior written permission.
1415 + *
1416 + * ALTERNATIVELY, this software may be distributed under the terms of the
1417 + * GNU General Public License ("GPL") version 2, as published by the Free
1418 + * Software Foundation.
1419 + *
1420 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
1421 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
1422 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1423 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
1424 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
1425 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
1426 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
1427 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
1428 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
1429 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
1430 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1431 + */
1432 +
1433 +#ifndef VCHI_COMMON_H_
1434 +#define VCHI_COMMON_H_
1435 +
1436 +
1437 +//flags used when sending messages (must be bitmapped)
1438 +typedef enum
1439 +{
1440 +   VCHI_FLAGS_NONE                      = 0x0,
1441 +   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)
1442 +   VCHI_FLAGS_CALLBACK_WHEN_OP_COMPLETE = 0x2,   // run a callback when message sent
1443 +   VCHI_FLAGS_BLOCK_UNTIL_QUEUED        = 0x4,   // return once the transfer is in a queue ready to go
1444 +   VCHI_FLAGS_ALLOW_PARTIAL             = 0x8,
1445 +   VCHI_FLAGS_BLOCK_UNTIL_DATA_READ     = 0x10,
1446 +   VCHI_FLAGS_CALLBACK_WHEN_DATA_READ   = 0x20,
1447 +
1448 +   VCHI_FLAGS_ALIGN_SLOT            = 0x000080,  // internal use only
1449 +   VCHI_FLAGS_BULK_AUX_QUEUED       = 0x010000,  // internal use only
1450 +   VCHI_FLAGS_BULK_AUX_COMPLETE     = 0x020000,  // internal use only
1451 +   VCHI_FLAGS_BULK_DATA_QUEUED      = 0x040000,  // internal use only
1452 +   VCHI_FLAGS_BULK_DATA_COMPLETE    = 0x080000,  // internal use only
1453 +   VCHI_FLAGS_INTERNAL              = 0xFF0000
1454 +} VCHI_FLAGS_T;
1455 +
1456 +// constants for vchi_crc_control()
1457 +typedef enum {
1458 +   VCHI_CRC_NOTHING = -1,
1459 +   VCHI_CRC_PER_SERVICE = 0,
1460 +   VCHI_CRC_EVERYTHING = 1,
1461 +} VCHI_CRC_CONTROL_T;
1462 +
1463 +//callback reasons when an event occurs on a service
1464 +typedef enum
1465 +{
1466 +   VCHI_CALLBACK_REASON_MIN,
1467 +
1468 +   //This indicates that there is data available
1469 +   //handle is the msg id that was transmitted with the data
1470 +   //    When a message is received and there was no FULL message available previously, send callback
1471 +   //    Tasks get kicked by the callback, reset their event and try and read from the fifo until it fails
1472 +   VCHI_CALLBACK_MSG_AVAILABLE,
1473 +   VCHI_CALLBACK_MSG_SENT,
1474 +   VCHI_CALLBACK_MSG_SPACE_AVAILABLE, // XXX not yet implemented
1475 +
1476 +   // This indicates that a transfer from the other side has completed
1477 +   VCHI_CALLBACK_BULK_RECEIVED,
1478 +   //This indicates that data queued up to be sent has now gone
1479 +   //handle is the msg id that was used when sending the data
1480 +   VCHI_CALLBACK_BULK_SENT,
1481 +   VCHI_CALLBACK_BULK_RX_SPACE_AVAILABLE, // XXX not yet implemented
1482 +   VCHI_CALLBACK_BULK_TX_SPACE_AVAILABLE, // XXX not yet implemented
1483 +
1484 +   VCHI_CALLBACK_SERVICE_CLOSED,
1485 +
1486 +   // this side has sent XOFF to peer due to lack of data consumption by service
1487 +   // (suggests the service may need to take some recovery action if it has
1488 +   // been deliberately holding off consuming data)
1489 +   VCHI_CALLBACK_SENT_XOFF,
1490 +   VCHI_CALLBACK_SENT_XON,
1491 +
1492 +   // indicates that a bulk transfer has finished reading the source buffer
1493 +   VCHI_CALLBACK_BULK_DATA_READ,
1494 +
1495 +   // power notification events (currently host side only)
1496 +   VCHI_CALLBACK_PEER_OFF,
1497 +   VCHI_CALLBACK_PEER_SUSPENDED,
1498 +   VCHI_CALLBACK_PEER_ON,
1499 +   VCHI_CALLBACK_PEER_RESUMED,
1500 +   VCHI_CALLBACK_FORCED_POWER_OFF,
1501 +
1502 +#ifdef USE_VCHIQ_ARM
1503 +   // some extra notifications provided by vchiq_arm
1504 +   VCHI_CALLBACK_SERVICE_OPENED,
1505 +   VCHI_CALLBACK_BULK_RECEIVE_ABORTED,
1506 +   VCHI_CALLBACK_BULK_TRANSMIT_ABORTED,
1507 +#endif
1508 +
1509 +   VCHI_CALLBACK_REASON_MAX
1510 +} VCHI_CALLBACK_REASON_T;
1511 +
1512 +// service control options
1513 +typedef enum
1514 +{
1515 +   VCHI_SERVICE_OPTION_MIN,
1516 +
1517 +   VCHI_SERVICE_OPTION_TRACE,
1518 +
1519 +   VCHI_SERVICE_OPTION_MAX
1520 +} VCHI_SERVICE_OPTION_T;
1521 +
1522 +
1523 +//Callback used by all services / bulk transfers
1524 +typedef void (*VCHI_CALLBACK_T)( void *callback_param, //my service local param
1525 +                                 VCHI_CALLBACK_REASON_T reason,
1526 +                                 void *handle ); //for transmitting msg's only
1527 +
1528 +
1529 +
1530 +/*
1531 + * Define vector struct for scatter-gather (vector) operations
1532 + * Vectors can be nested - if a vector element has negative length, then
1533 + * the data pointer is treated as pointing to another vector array, with
1534 + * '-vec_len' elements. Thus to append a header onto an existing vector,
1535 + * you can do this:
1536 + *
1537 + * void foo(const VCHI_MSG_VECTOR_T *v, int n)
1538 + * {
1539 + *    VCHI_MSG_VECTOR_T nv[2];
1540 + *    nv[0].vec_base = my_header;
1541 + *    nv[0].vec_len = sizeof my_header;
1542 + *    nv[1].vec_base = v;
1543 + *    nv[1].vec_len = -n;
1544 + *    ...
1545 + *
1546 + */
1547 +typedef struct vchi_msg_vector {
1548 +   const void *vec_base;
1549 +   int32_t vec_len;
1550 +} VCHI_MSG_VECTOR_T;
1551 +
1552 +// Opaque type for a connection API
1553 +typedef struct opaque_vchi_connection_api_t VCHI_CONNECTION_API_T;
1554 +
1555 +// Opaque type for a message driver
1556 +typedef struct opaque_vchi_message_driver_t VCHI_MESSAGE_DRIVER_T;
1557 +
1558 +
1559 +// Iterator structure for reading ahead through received message queue. Allocated by client,
1560 +// initialised by vchi_msg_look_ahead. Fields are for internal VCHI use only.
1561 +// Iterates over messages in queue at the instant of the call to vchi_msg_lookahead -
1562 +// will not proceed to messages received since. Behaviour is undefined if an iterator
1563 +// is used again after messages for that service are removed/dequeued by any
1564 +// means other than vchi_msg_iter_... calls on the iterator itself.
1565 +typedef struct {
1566 +   struct opaque_vchi_service_t *service;
1567 +   void *last;
1568 +   void *next;
1569 +   void *remove;
1570 +} VCHI_MSG_ITER_T;
1571 +
1572 +
1573 +#endif // VCHI_COMMON_H_
1574 --- /dev/null
1575 +++ b/drivers/misc/vc04_services/interface/vchi/vchi_mh.h
1576 @@ -0,0 +1,42 @@
1577 +/**
1578 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
1579 + *
1580 + * Redistribution and use in source and binary forms, with or without
1581 + * modification, are permitted provided that the following conditions
1582 + * are met:
1583 + * 1. Redistributions of source code must retain the above copyright
1584 + *    notice, this list of conditions, and the following disclaimer,
1585 + *    without modification.
1586 + * 2. Redistributions in binary form must reproduce the above copyright
1587 + *    notice, this list of conditions and the following disclaimer in the
1588 + *    documentation and/or other materials provided with the distribution.
1589 + * 3. The names of the above-listed copyright holders may not be used
1590 + *    to endorse or promote products derived from this software without
1591 + *    specific prior written permission.
1592 + *
1593 + * ALTERNATIVELY, this software may be distributed under the terms of the
1594 + * GNU General Public License ("GPL") version 2, as published by the Free
1595 + * Software Foundation.
1596 + *
1597 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
1598 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
1599 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1600 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
1601 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
1602 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
1603 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
1604 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
1605 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
1606 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
1607 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1608 + */
1609 +
1610 +#ifndef VCHI_MH_H_
1611 +#define VCHI_MH_H_
1612 +
1613 +#include <linux/types.h>
1614 +
1615 +typedef int32_t VCHI_MEM_HANDLE_T;
1616 +#define VCHI_MEM_HANDLE_INVALID 0
1617 +
1618 +#endif
1619 --- /dev/null
1620 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq.h
1621 @@ -0,0 +1,40 @@
1622 +/**
1623 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
1624 + *
1625 + * Redistribution and use in source and binary forms, with or without
1626 + * modification, are permitted provided that the following conditions
1627 + * are met:
1628 + * 1. Redistributions of source code must retain the above copyright
1629 + *    notice, this list of conditions, and the following disclaimer,
1630 + *    without modification.
1631 + * 2. Redistributions in binary form must reproduce the above copyright
1632 + *    notice, this list of conditions and the following disclaimer in the
1633 + *    documentation and/or other materials provided with the distribution.
1634 + * 3. The names of the above-listed copyright holders may not be used
1635 + *    to endorse or promote products derived from this software without
1636 + *    specific prior written permission.
1637 + *
1638 + * ALTERNATIVELY, this software may be distributed under the terms of the
1639 + * GNU General Public License ("GPL") version 2, as published by the Free
1640 + * Software Foundation.
1641 + *
1642 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
1643 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
1644 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1645 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
1646 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
1647 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
1648 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
1649 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
1650 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
1651 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
1652 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1653 + */
1654 +
1655 +#ifndef VCHIQ_VCHIQ_H
1656 +#define VCHIQ_VCHIQ_H
1657 +
1658 +#include "vchiq_if.h"
1659 +#include "vchiq_util.h"
1660 +
1661 +#endif
1662 --- /dev/null
1663 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835.h
1664 @@ -0,0 +1,42 @@
1665 +/**
1666 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
1667 + *
1668 + * Redistribution and use in source and binary forms, with or without
1669 + * modification, are permitted provided that the following conditions
1670 + * are met:
1671 + * 1. Redistributions of source code must retain the above copyright
1672 + *    notice, this list of conditions, and the following disclaimer,
1673 + *    without modification.
1674 + * 2. Redistributions in binary form must reproduce the above copyright
1675 + *    notice, this list of conditions and the following disclaimer in the
1676 + *    documentation and/or other materials provided with the distribution.
1677 + * 3. The names of the above-listed copyright holders may not be used
1678 + *    to endorse or promote products derived from this software without
1679 + *    specific prior written permission.
1680 + *
1681 + * ALTERNATIVELY, this software may be distributed under the terms of the
1682 + * GNU General Public License ("GPL") version 2, as published by the Free
1683 + * Software Foundation.
1684 + *
1685 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
1686 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
1687 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1688 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
1689 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
1690 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
1691 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
1692 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
1693 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
1694 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
1695 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1696 + */
1697 +
1698 +#ifndef VCHIQ_2835_H
1699 +#define VCHIQ_2835_H
1700 +
1701 +#include "vchiq_pagelist.h"
1702 +
1703 +#define VCHIQ_PLATFORM_FRAGMENTS_OFFSET_IDX 0
1704 +#define VCHIQ_PLATFORM_FRAGMENTS_COUNT_IDX  1
1705 +
1706 +#endif /* VCHIQ_2835_H */
1707 --- /dev/null
1708 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_2835_arm.c
1709 @@ -0,0 +1,561 @@
1710 +/**
1711 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
1712 + *
1713 + * Redistribution and use in source and binary forms, with or without
1714 + * modification, are permitted provided that the following conditions
1715 + * are met:
1716 + * 1. Redistributions of source code must retain the above copyright
1717 + *    notice, this list of conditions, and the following disclaimer,
1718 + *    without modification.
1719 + * 2. Redistributions in binary form must reproduce the above copyright
1720 + *    notice, this list of conditions and the following disclaimer in the
1721 + *    documentation and/or other materials provided with the distribution.
1722 + * 3. The names of the above-listed copyright holders may not be used
1723 + *    to endorse or promote products derived from this software without
1724 + *    specific prior written permission.
1725 + *
1726 + * ALTERNATIVELY, this software may be distributed under the terms of the
1727 + * GNU General Public License ("GPL") version 2, as published by the Free
1728 + * Software Foundation.
1729 + *
1730 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
1731 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
1732 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1733 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
1734 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
1735 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
1736 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
1737 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
1738 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
1739 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
1740 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
1741 + */
1742 +
1743 +#include <linux/kernel.h>
1744 +#include <linux/types.h>
1745 +#include <linux/errno.h>
1746 +#include <linux/interrupt.h>
1747 +#include <linux/irq.h>
1748 +#include <linux/pagemap.h>
1749 +#include <linux/dma-mapping.h>
1750 +#include <linux/version.h>
1751 +#include <linux/io.h>
1752 +#include <linux/uaccess.h>
1753 +#include <asm/pgtable.h>
1754 +
1755 +#include <mach/irqs.h>
1756 +
1757 +#include <mach/platform.h>
1758 +#include <mach/vcio.h>
1759 +
1760 +#define TOTAL_SLOTS (VCHIQ_SLOT_ZERO_SLOTS + 2 * 32)
1761 +
1762 +#define VCHIQ_DOORBELL_IRQ IRQ_ARM_DOORBELL_0
1763 +#define VCHIQ_ARM_ADDRESS(x) ((void *)__virt_to_bus((unsigned)x))
1764 +
1765 +#include "vchiq_arm.h"
1766 +#include "vchiq_2835.h"
1767 +#include "vchiq_connected.h"
1768 +
1769 +#define MAX_FRAGMENTS (VCHIQ_NUM_CURRENT_BULKS * 2)
1770 +
1771 +typedef struct vchiq_2835_state_struct {
1772 +   int inited;
1773 +   VCHIQ_ARM_STATE_T arm_state;
1774 +} VCHIQ_2835_ARM_STATE_T;
1775 +
1776 +static char *g_slot_mem;
1777 +static int g_slot_mem_size;
1778 +dma_addr_t g_slot_phys;
1779 +static FRAGMENTS_T *g_fragments_base;
1780 +static FRAGMENTS_T *g_free_fragments;
1781 +struct semaphore g_free_fragments_sema;
1782 +
1783 +extern int vchiq_arm_log_level;
1784 +
1785 +static DEFINE_SEMAPHORE(g_free_fragments_mutex);
1786 +
1787 +static irqreturn_t
1788 +vchiq_doorbell_irq(int irq, void *dev_id);
1789 +
1790 +static int
1791 +create_pagelist(char __user *buf, size_t count, unsigned short type,
1792 +                struct task_struct *task, PAGELIST_T ** ppagelist);
1793 +
1794 +static void
1795 +free_pagelist(PAGELIST_T *pagelist, int actual);
1796 +
1797 +int __init
1798 +vchiq_platform_init(VCHIQ_STATE_T *state)
1799 +{
1800 +       VCHIQ_SLOT_ZERO_T *vchiq_slot_zero;
1801 +       int frag_mem_size;
1802 +       int err;
1803 +       int i;
1804 +
1805 +       /* Allocate space for the channels in coherent memory */
1806 +       g_slot_mem_size = PAGE_ALIGN(TOTAL_SLOTS * VCHIQ_SLOT_SIZE);
1807 +       frag_mem_size = PAGE_ALIGN(sizeof(FRAGMENTS_T) * MAX_FRAGMENTS);
1808 +
1809 +       g_slot_mem = dma_alloc_coherent(NULL, g_slot_mem_size + frag_mem_size,
1810 +               &g_slot_phys, GFP_ATOMIC);
1811 +
1812 +       if (!g_slot_mem) {
1813 +               vchiq_log_error(vchiq_arm_log_level,
1814 +                       "Unable to allocate channel memory");
1815 +               err = -ENOMEM;
1816 +               goto failed_alloc;
1817 +       }
1818 +
1819 +       WARN_ON(((int)g_slot_mem & (PAGE_SIZE - 1)) != 0);
1820 +
1821 +       vchiq_slot_zero = vchiq_init_slots(g_slot_mem, g_slot_mem_size);
1822 +       if (!vchiq_slot_zero) {
1823 +               err = -EINVAL;
1824 +               goto failed_init_slots;
1825 +       }
1826 +
1827 +       vchiq_slot_zero->platform_data[VCHIQ_PLATFORM_FRAGMENTS_OFFSET_IDX] =
1828 +               (int)g_slot_phys + g_slot_mem_size;
1829 +       vchiq_slot_zero->platform_data[VCHIQ_PLATFORM_FRAGMENTS_COUNT_IDX] =
1830 +               MAX_FRAGMENTS;
1831 +
1832 +       g_fragments_base = (FRAGMENTS_T *)(g_slot_mem + g_slot_mem_size);
1833 +       g_slot_mem_size += frag_mem_size;
1834 +
1835 +       g_free_fragments = g_fragments_base;
1836 +       for (i = 0; i < (MAX_FRAGMENTS - 1); i++) {
1837 +               *(FRAGMENTS_T **)&g_fragments_base[i] =
1838 +                       &g_fragments_base[i + 1];
1839 +       }
1840 +       *(FRAGMENTS_T **)&g_fragments_base[i] = NULL;
1841 +       sema_init(&g_free_fragments_sema, MAX_FRAGMENTS);
1842 +
1843 +       if (vchiq_init_state(state, vchiq_slot_zero, 0/*slave*/) !=
1844 +               VCHIQ_SUCCESS) {
1845 +               err = -EINVAL;
1846 +               goto failed_vchiq_init;
1847 +       }
1848 +
1849 +       err = request_irq(VCHIQ_DOORBELL_IRQ, vchiq_doorbell_irq,
1850 +               IRQF_IRQPOLL, "VCHIQ doorbell",
1851 +               state);
1852 +       if (err < 0) {
1853 +               vchiq_log_error(vchiq_arm_log_level, "%s: failed to register "
1854 +                       "irq=%d err=%d", __func__,
1855 +                       VCHIQ_DOORBELL_IRQ, err);
1856 +               goto failed_request_irq;
1857 +       }
1858 +
1859 +       /* Send the base address of the slots to VideoCore */
1860 +
1861 +       dsb(); /* Ensure all writes have completed */
1862 +
1863 +       bcm_mailbox_write(MBOX_CHAN_VCHIQ, (unsigned int)g_slot_phys);
1864 +
1865 +       vchiq_log_info(vchiq_arm_log_level,
1866 +               "vchiq_init - done (slots %x, phys %x)",
1867 +               (unsigned int)vchiq_slot_zero, g_slot_phys);
1868 +
1869 +   vchiq_call_connected_callbacks();
1870 +
1871 +   return 0;
1872 +
1873 +failed_request_irq:
1874 +failed_vchiq_init:
1875 +failed_init_slots:
1876 +   dma_free_coherent(NULL, g_slot_mem_size, g_slot_mem, g_slot_phys);
1877 +
1878 +failed_alloc:
1879 +   return err;
1880 +}
1881 +
1882 +void __exit
1883 +vchiq_platform_exit(VCHIQ_STATE_T *state)
1884 +{
1885 +   free_irq(VCHIQ_DOORBELL_IRQ, state);
1886 +   dma_free_coherent(NULL, g_slot_mem_size,
1887 +                     g_slot_mem, g_slot_phys);
1888 +}
1889 +
1890 +
1891 +VCHIQ_STATUS_T
1892 +vchiq_platform_init_state(VCHIQ_STATE_T *state)
1893 +{
1894 +   VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
1895 +   state->platform_state = kzalloc(sizeof(VCHIQ_2835_ARM_STATE_T), GFP_KERNEL);
1896 +   ((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->inited = 1;
1897 +   status = vchiq_arm_init_state(state, &((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->arm_state);
1898 +   if(status != VCHIQ_SUCCESS)
1899 +   {
1900 +      ((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->inited = 0;
1901 +   }
1902 +   return status;
1903 +}
1904 +
1905 +VCHIQ_ARM_STATE_T*
1906 +vchiq_platform_get_arm_state(VCHIQ_STATE_T *state)
1907 +{
1908 +   if(!((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->inited)
1909 +   {
1910 +      BUG();
1911 +   }
1912 +   return &((VCHIQ_2835_ARM_STATE_T*)state->platform_state)->arm_state;
1913 +}
1914 +
1915 +void
1916 +remote_event_signal(REMOTE_EVENT_T *event)
1917 +{
1918 +       wmb();
1919 +
1920 +       event->fired = 1;
1921 +
1922 +       dsb();         /* data barrier operation */
1923 +
1924 +       if (event->armed) {
1925 +               /* trigger vc interrupt */
1926 +
1927 +               writel(0, __io_address(ARM_0_BELL2));
1928 +       }
1929 +}
1930 +
1931 +int
1932 +vchiq_copy_from_user(void *dst, const void *src, int size)
1933 +{
1934 +       if ((uint32_t)src < TASK_SIZE) {
1935 +               return copy_from_user(dst, src, size);
1936 +       } else {
1937 +               memcpy(dst, src, size);
1938 +               return 0;
1939 +       }
1940 +}
1941 +
1942 +VCHIQ_STATUS_T
1943 +vchiq_prepare_bulk_data(VCHIQ_BULK_T *bulk, VCHI_MEM_HANDLE_T memhandle,
1944 +       void *offset, int size, int dir)
1945 +{
1946 +       PAGELIST_T *pagelist;
1947 +       int ret;
1948 +
1949 +       WARN_ON(memhandle != VCHI_MEM_HANDLE_INVALID);
1950 +
1951 +       ret = create_pagelist((char __user *)offset, size,
1952 +                       (dir == VCHIQ_BULK_RECEIVE)
1953 +                       ? PAGELIST_READ
1954 +                       : PAGELIST_WRITE,
1955 +                       current,
1956 +                       &pagelist);
1957 +       if (ret != 0)
1958 +               return VCHIQ_ERROR;
1959 +
1960 +       bulk->handle = memhandle;
1961 +       bulk->data = VCHIQ_ARM_ADDRESS(pagelist);
1962 +
1963 +       /* Store the pagelist address in remote_data, which isn't used by the
1964 +          slave. */
1965 +       bulk->remote_data = pagelist;
1966 +
1967 +       return VCHIQ_SUCCESS;
1968 +}
1969 +
1970 +void
1971 +vchiq_complete_bulk(VCHIQ_BULK_T *bulk)
1972 +{
1973 +       if (bulk && bulk->remote_data && bulk->actual)
1974 +               free_pagelist((PAGELIST_T *)bulk->remote_data, bulk->actual);
1975 +}
1976 +
1977 +void
1978 +vchiq_transfer_bulk(VCHIQ_BULK_T *bulk)
1979 +{
1980 +       /*
1981 +        * This should only be called on the master (VideoCore) side, but
1982 +        * provide an implementation to avoid the need for ifdefery.
1983 +        */
1984 +       BUG();
1985 +}
1986 +
1987 +void
1988 +vchiq_dump_platform_state(void *dump_context)
1989 +{
1990 +       char buf[80];
1991 +       int len;
1992 +       len = snprintf(buf, sizeof(buf),
1993 +               "  Platform: 2835 (VC master)");
1994 +       vchiq_dump(dump_context, buf, len + 1);
1995 +}
1996 +
1997 +VCHIQ_STATUS_T
1998 +vchiq_platform_suspend(VCHIQ_STATE_T *state)
1999 +{
2000 +   return VCHIQ_ERROR;
2001 +}
2002 +
2003 +VCHIQ_STATUS_T
2004 +vchiq_platform_resume(VCHIQ_STATE_T *state)
2005 +{
2006 +   return VCHIQ_SUCCESS;
2007 +}
2008 +
2009 +void
2010 +vchiq_platform_paused(VCHIQ_STATE_T *state)
2011 +{
2012 +}
2013 +
2014 +void
2015 +vchiq_platform_resumed(VCHIQ_STATE_T *state)
2016 +{
2017 +}
2018 +
2019 +int
2020 +vchiq_platform_videocore_wanted(VCHIQ_STATE_T* state)
2021 +{
2022 +   return 1; // autosuspend not supported - videocore always wanted
2023 +}
2024 +
2025 +int
2026 +vchiq_platform_use_suspend_timer(void)
2027 +{
2028 +   return 0;
2029 +}
2030 +void
2031 +vchiq_dump_platform_use_state(VCHIQ_STATE_T *state)
2032 +{
2033 +       vchiq_log_info((vchiq_arm_log_level>=VCHIQ_LOG_INFO),"Suspend timer not in use");
2034 +}
2035 +void
2036 +vchiq_platform_handle_timeout(VCHIQ_STATE_T *state)
2037 +{
2038 +       (void)state;
2039 +}
2040 +/*
2041 + * Local functions
2042 + */
2043 +
2044 +static irqreturn_t
2045 +vchiq_doorbell_irq(int irq, void *dev_id)
2046 +{
2047 +       VCHIQ_STATE_T *state = dev_id;
2048 +       irqreturn_t ret = IRQ_NONE;
2049 +       unsigned int status;
2050 +
2051 +       /* Read (and clear) the doorbell */
2052 +       status = readl(__io_address(ARM_0_BELL0));
2053 +
2054 +       if (status & 0x4) {  /* Was the doorbell rung? */
2055 +               remote_event_pollall(state);
2056 +               ret = IRQ_HANDLED;
2057 +       }
2058 +
2059 +       return ret;
2060 +}
2061 +
2062 +/* There is a potential problem with partial cache lines (pages?)
2063 +** at the ends of the block when reading. If the CPU accessed anything in
2064 +** the same line (page?) then it may have pulled old data into the cache,
2065 +** obscuring the new data underneath. We can solve this by transferring the
2066 +** partial cache lines separately, and allowing the ARM to copy into the
2067 +** cached area.
2068 +
2069 +** N.B. This implementation plays slightly fast and loose with the Linux
2070 +** driver programming rules, e.g. its use of __virt_to_bus instead of
2071 +** dma_map_single, but it isn't a multi-platform driver and it benefits
2072 +** from increased speed as a result.
2073 +*/
2074 +
2075 +static int
2076 +create_pagelist(char __user *buf, size_t count, unsigned short type,
2077 +       struct task_struct *task, PAGELIST_T ** ppagelist)
2078 +{
2079 +       PAGELIST_T *pagelist;
2080 +       struct page **pages;
2081 +       struct page *page;
2082 +       unsigned long *addrs;
2083 +       unsigned int num_pages, offset, i;
2084 +       char *addr, *base_addr, *next_addr;
2085 +       int run, addridx, actual_pages;
2086 +        unsigned long *need_release;
2087 +
2088 +       offset = (unsigned int)buf & (PAGE_SIZE - 1);
2089 +       num_pages = (count + offset + PAGE_SIZE - 1) / PAGE_SIZE;
2090 +
2091 +       *ppagelist = NULL;
2092 +
2093 +       /* Allocate enough storage to hold the page pointers and the page
2094 +       ** list
2095 +       */
2096 +       pagelist = kmalloc(sizeof(PAGELIST_T) +
2097 +                           (num_pages * sizeof(unsigned long)) +
2098 +                           sizeof(unsigned long) +
2099 +                           (num_pages * sizeof(pages[0])),
2100 +                           GFP_KERNEL);
2101 +
2102 +       vchiq_log_trace(vchiq_arm_log_level,
2103 +               "create_pagelist - %x", (unsigned int)pagelist);
2104 +       if (!pagelist)
2105 +               return -ENOMEM;
2106 +
2107 +       addrs = pagelist->addrs;
2108 +        need_release = (unsigned long *)(addrs + num_pages);
2109 +       pages = (struct page **)(addrs + num_pages + 1);
2110 +
2111 +       if (is_vmalloc_addr(buf)) {
2112 +               for (actual_pages = 0; actual_pages < num_pages; actual_pages++) {
2113 +                       pages[actual_pages] = vmalloc_to_page(buf + (actual_pages * PAGE_SIZE));
2114 +               }
2115 +                *need_release = 0; /* do not try and release vmalloc pages */
2116 +       } else {
2117 +               down_read(&task->mm->mmap_sem);
2118 +               actual_pages = get_user_pages(task, task->mm,
2119 +                                         (unsigned long)buf & ~(PAGE_SIZE - 1),
2120 +                                         num_pages,
2121 +                                         (type == PAGELIST_READ) /*Write */ ,
2122 +                                         0 /*Force */ ,
2123 +                                         pages,
2124 +                                         NULL /*vmas */);
2125 +               up_read(&task->mm->mmap_sem);
2126 +
2127 +               if (actual_pages != num_pages) {
2128 +                       vchiq_log_info(vchiq_arm_log_level,
2129 +                                      "create_pagelist - only %d/%d pages locked",
2130 +                                      actual_pages,
2131 +                                      num_pages);
2132 +
2133 +                       /* This is probably due to the process being killed */
2134 +                       while (actual_pages > 0)
2135 +                       {
2136 +                               actual_pages--;
2137 +                               page_cache_release(pages[actual_pages]);
2138 +                       }
2139 +                       kfree(pagelist);
2140 +                       if (actual_pages == 0)
2141 +                               actual_pages = -ENOMEM;
2142 +                       return actual_pages;
2143 +               }
2144 +                *need_release = 1; /* release user pages */
2145 +       }
2146 +
2147 +       pagelist->length = count;
2148 +       pagelist->type = type;
2149 +       pagelist->offset = offset;
2150 +
2151 +       /* Group the pages into runs of contiguous pages */
2152 +
2153 +       base_addr = VCHIQ_ARM_ADDRESS(page_address(pages[0]));
2154 +       next_addr = base_addr + PAGE_SIZE;
2155 +       addridx = 0;
2156 +       run = 0;
2157 +
2158 +       for (i = 1; i < num_pages; i++) {
2159 +               addr = VCHIQ_ARM_ADDRESS(page_address(pages[i]));
2160 +               if ((addr == next_addr) && (run < (PAGE_SIZE - 1))) {
2161 +                       next_addr += PAGE_SIZE;
2162 +                       run++;
2163 +               } else {
2164 +                       addrs[addridx] = (unsigned long)base_addr + run;
2165 +                       addridx++;
2166 +                       base_addr = addr;
2167 +                       next_addr = addr + PAGE_SIZE;
2168 +                       run = 0;
2169 +               }
2170 +       }
2171 +
2172 +       addrs[addridx] = (unsigned long)base_addr + run;
2173 +       addridx++;
2174 +
2175 +       /* Partial cache lines (fragments) require special measures */
2176 +       if ((type == PAGELIST_READ) &&
2177 +               ((pagelist->offset & (CACHE_LINE_SIZE - 1)) ||
2178 +               ((pagelist->offset + pagelist->length) &
2179 +               (CACHE_LINE_SIZE - 1)))) {
2180 +               FRAGMENTS_T *fragments;
2181 +
2182 +               if (down_interruptible(&g_free_fragments_sema) != 0) {
2183 +                       kfree(pagelist);
2184 +                       return -EINTR;
2185 +               }
2186 +
2187 +               WARN_ON(g_free_fragments == NULL);
2188 +
2189 +               down(&g_free_fragments_mutex);
2190 +               fragments = (FRAGMENTS_T *) g_free_fragments;
2191 +               WARN_ON(fragments == NULL);
2192 +               g_free_fragments = *(FRAGMENTS_T **) g_free_fragments;
2193 +               up(&g_free_fragments_mutex);
2194 +               pagelist->type =
2195 +                        PAGELIST_READ_WITH_FRAGMENTS + (fragments -
2196 +                                                        g_fragments_base);
2197 +       }
2198 +
2199 +       for (page = virt_to_page(pagelist);
2200 +               page <= virt_to_page(addrs + num_pages - 1); page++) {
2201 +               flush_dcache_page(page);
2202 +       }
2203 +
2204 +       *ppagelist = pagelist;
2205 +
2206 +       return 0;
2207 +}
2208 +
2209 +static void
2210 +free_pagelist(PAGELIST_T *pagelist, int actual)
2211 +{
2212 +        unsigned long *need_release;
2213 +       struct page **pages;
2214 +       unsigned int num_pages, i;
2215 +
2216 +       vchiq_log_trace(vchiq_arm_log_level,
2217 +               "free_pagelist - %x, %d", (unsigned int)pagelist, actual);
2218 +
2219 +       num_pages =
2220 +               (pagelist->length + pagelist->offset + PAGE_SIZE - 1) /
2221 +               PAGE_SIZE;
2222 +
2223 +        need_release = (unsigned long *)(pagelist->addrs + num_pages);
2224 +       pages = (struct page **)(pagelist->addrs + num_pages + 1);
2225 +
2226 +       /* Deal with any partial cache lines (fragments) */
2227 +       if (pagelist->type >= PAGELIST_READ_WITH_FRAGMENTS) {
2228 +               FRAGMENTS_T *fragments = g_fragments_base +
2229 +                       (pagelist->type - PAGELIST_READ_WITH_FRAGMENTS);
2230 +               int head_bytes, tail_bytes;
2231 +               head_bytes = (CACHE_LINE_SIZE - pagelist->offset) &
2232 +                       (CACHE_LINE_SIZE - 1);
2233 +               tail_bytes = (pagelist->offset + actual) &
2234 +                       (CACHE_LINE_SIZE - 1);
2235 +
2236 +               if ((actual >= 0) && (head_bytes != 0)) {
2237 +                       if (head_bytes > actual)
2238 +                               head_bytes = actual;
2239 +
2240 +                       memcpy((char *)page_address(pages[0]) +
2241 +                               pagelist->offset,
2242 +                               fragments->headbuf,
2243 +                               head_bytes);
2244 +               }
2245 +               if ((actual >= 0) && (head_bytes < actual) &&
2246 +                       (tail_bytes != 0)) {
2247 +                       memcpy((char *)page_address(pages[num_pages - 1]) +
2248 +                               ((pagelist->offset + actual) &
2249 +                               (PAGE_SIZE - 1) & ~(CACHE_LINE_SIZE - 1)),
2250 +                               fragments->tailbuf, tail_bytes);
2251 +               }
2252 +
2253 +               down(&g_free_fragments_mutex);
2254 +               *(FRAGMENTS_T **) fragments = g_free_fragments;
2255 +               g_free_fragments = fragments;
2256 +               up(&g_free_fragments_mutex);
2257 +               up(&g_free_fragments_sema);
2258 +       }
2259 +
2260 +        if (*need_release) {
2261 +               for (i = 0; i < num_pages; i++) {
2262 +                       if (pagelist->type != PAGELIST_WRITE)
2263 +                               set_page_dirty(pages[i]);
2264 +
2265 +                       page_cache_release(pages[i]);
2266 +               }
2267 +        }
2268 +
2269 +       kfree(pagelist);
2270 +}
2271 --- /dev/null
2272 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.c
2273 @@ -0,0 +1,2883 @@
2274 +/**
2275 + * Copyright (c) 2014 Raspberry Pi (Trading) Ltd. All rights reserved.
2276 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
2277 + *
2278 + * Redistribution and use in source and binary forms, with or without
2279 + * modification, are permitted provided that the following conditions
2280 + * are met:
2281 + * 1. Redistributions of source code must retain the above copyright
2282 + *    notice, this list of conditions, and the following disclaimer,
2283 + *    without modification.
2284 + * 2. Redistributions in binary form must reproduce the above copyright
2285 + *    notice, this list of conditions and the following disclaimer in the
2286 + *    documentation and/or other materials provided with the distribution.
2287 + * 3. The names of the above-listed copyright holders may not be used
2288 + *    to endorse or promote products derived from this software without
2289 + *    specific prior written permission.
2290 + *
2291 + * ALTERNATIVELY, this software may be distributed under the terms of the
2292 + * GNU General Public License ("GPL") version 2, as published by the Free
2293 + * Software Foundation.
2294 + *
2295 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
2296 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
2297 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2298 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
2299 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
2300 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
2301 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
2302 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
2303 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
2304 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
2305 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2306 + */
2307 +
2308 +#include <linux/kernel.h>
2309 +#include <linux/module.h>
2310 +#include <linux/types.h>
2311 +#include <linux/errno.h>
2312 +#include <linux/cdev.h>
2313 +#include <linux/fs.h>
2314 +#include <linux/device.h>
2315 +#include <linux/mm.h>
2316 +#include <linux/highmem.h>
2317 +#include <linux/pagemap.h>
2318 +#include <linux/bug.h>
2319 +#include <linux/semaphore.h>
2320 +#include <linux/list.h>
2321 +
2322 +#include "vchiq_core.h"
2323 +#include "vchiq_ioctl.h"
2324 +#include "vchiq_arm.h"
2325 +#include "vchiq_debugfs.h"
2326 +
2327 +#define DEVICE_NAME "vchiq"
2328 +
2329 +/* Override the default prefix, which would be vchiq_arm (from the filename) */
2330 +#undef MODULE_PARAM_PREFIX
2331 +#define MODULE_PARAM_PREFIX DEVICE_NAME "."
2332 +
2333 +#define VCHIQ_MINOR 0
2334 +
2335 +/* Some per-instance constants */
2336 +#define MAX_COMPLETIONS 16
2337 +#define MAX_SERVICES 64
2338 +#define MAX_ELEMENTS 8
2339 +#define MSG_QUEUE_SIZE 64
2340 +
2341 +#define KEEPALIVE_VER 1
2342 +#define KEEPALIVE_VER_MIN KEEPALIVE_VER
2343 +
2344 +/* Run time control of log level, based on KERN_XXX level. */
2345 +int vchiq_arm_log_level = VCHIQ_LOG_DEFAULT;
2346 +int vchiq_susp_log_level = VCHIQ_LOG_ERROR;
2347 +
2348 +#define SUSPEND_TIMER_TIMEOUT_MS 100
2349 +#define SUSPEND_RETRY_TIMER_TIMEOUT_MS 1000
2350 +
2351 +#define VC_SUSPEND_NUM_OFFSET 3 /* number of values before idle which are -ve */
2352 +static const char *const suspend_state_names[] = {
2353 +       "VC_SUSPEND_FORCE_CANCELED",
2354 +       "VC_SUSPEND_REJECTED",
2355 +       "VC_SUSPEND_FAILED",
2356 +       "VC_SUSPEND_IDLE",
2357 +       "VC_SUSPEND_REQUESTED",
2358 +       "VC_SUSPEND_IN_PROGRESS",
2359 +       "VC_SUSPEND_SUSPENDED"
2360 +};
2361 +#define VC_RESUME_NUM_OFFSET 1 /* number of values before idle which are -ve */
2362 +static const char *const resume_state_names[] = {
2363 +       "VC_RESUME_FAILED",
2364 +       "VC_RESUME_IDLE",
2365 +       "VC_RESUME_REQUESTED",
2366 +       "VC_RESUME_IN_PROGRESS",
2367 +       "VC_RESUME_RESUMED"
2368 +};
2369 +/* The number of times we allow force suspend to timeout before actually
2370 +** _forcing_ suspend.  This is to cater for SW which fails to release vchiq
2371 +** correctly - we don't want to prevent ARM suspend indefinitely in this case.
2372 +*/
2373 +#define FORCE_SUSPEND_FAIL_MAX 8
2374 +
2375 +/* The time in ms allowed for videocore to go idle when force suspend has been
2376 + * requested */
2377 +#define FORCE_SUSPEND_TIMEOUT_MS 200
2378 +
2379 +
2380 +static void suspend_timer_callback(unsigned long context);
2381 +
2382 +
2383 +typedef struct user_service_struct {
2384 +       VCHIQ_SERVICE_T *service;
2385 +       void *userdata;
2386 +       VCHIQ_INSTANCE_T instance;
2387 +       char is_vchi;
2388 +       char dequeue_pending;
2389 +       char close_pending;
2390 +       int message_available_pos;
2391 +       int msg_insert;
2392 +       int msg_remove;
2393 +       struct semaphore insert_event;
2394 +       struct semaphore remove_event;
2395 +       struct semaphore close_event;
2396 +       VCHIQ_HEADER_T * msg_queue[MSG_QUEUE_SIZE];
2397 +} USER_SERVICE_T;
2398 +
2399 +struct bulk_waiter_node {
2400 +       struct bulk_waiter bulk_waiter;
2401 +       int pid;
2402 +       struct list_head list;
2403 +};
2404 +
2405 +struct vchiq_instance_struct {
2406 +       VCHIQ_STATE_T *state;
2407 +       VCHIQ_COMPLETION_DATA_T completions[MAX_COMPLETIONS];
2408 +       int completion_insert;
2409 +       int completion_remove;
2410 +       struct semaphore insert_event;
2411 +       struct semaphore remove_event;
2412 +       struct mutex completion_mutex;
2413 +
2414 +       int connected;
2415 +       int closing;
2416 +       int pid;
2417 +       int mark;
2418 +       int use_close_delivered;
2419 +       int trace;
2420 +
2421 +       struct list_head bulk_waiter_list;
2422 +       struct mutex bulk_waiter_list_mutex;
2423 +
2424 +       VCHIQ_DEBUGFS_NODE_T debugfs_node;
2425 +};
2426 +
2427 +typedef struct dump_context_struct {
2428 +       char __user *buf;
2429 +       size_t actual;
2430 +       size_t space;
2431 +       loff_t offset;
2432 +} DUMP_CONTEXT_T;
2433 +
2434 +static struct cdev    vchiq_cdev;
2435 +static dev_t          vchiq_devid;
2436 +static VCHIQ_STATE_T g_state;
2437 +static struct class  *vchiq_class;
2438 +static struct device *vchiq_dev;
2439 +static DEFINE_SPINLOCK(msg_queue_spinlock);
2440 +
2441 +static const char *const ioctl_names[] = {
2442 +       "CONNECT",
2443 +       "SHUTDOWN",
2444 +       "CREATE_SERVICE",
2445 +       "REMOVE_SERVICE",
2446 +       "QUEUE_MESSAGE",
2447 +       "QUEUE_BULK_TRANSMIT",
2448 +       "QUEUE_BULK_RECEIVE",
2449 +       "AWAIT_COMPLETION",
2450 +       "DEQUEUE_MESSAGE",
2451 +       "GET_CLIENT_ID",
2452 +       "GET_CONFIG",
2453 +       "CLOSE_SERVICE",
2454 +       "USE_SERVICE",
2455 +       "RELEASE_SERVICE",
2456 +       "SET_SERVICE_OPTION",
2457 +       "DUMP_PHYS_MEM",
2458 +       "LIB_VERSION",
2459 +       "CLOSE_DELIVERED"
2460 +};
2461 +
2462 +vchiq_static_assert((sizeof(ioctl_names)/sizeof(ioctl_names[0])) ==
2463 +       (VCHIQ_IOC_MAX + 1));
2464 +
2465 +static void
2466 +dump_phys_mem(void *virt_addr, uint32_t num_bytes);
2467 +
2468 +/****************************************************************************
2469 +*
2470 +*   add_completion
2471 +*
2472 +***************************************************************************/
2473 +
2474 +static VCHIQ_STATUS_T
2475 +add_completion(VCHIQ_INSTANCE_T instance, VCHIQ_REASON_T reason,
2476 +       VCHIQ_HEADER_T *header, USER_SERVICE_T *user_service,
2477 +       void *bulk_userdata)
2478 +{
2479 +       VCHIQ_COMPLETION_DATA_T *completion;
2480 +       DEBUG_INITIALISE(g_state.local)
2481 +
2482 +       while (instance->completion_insert ==
2483 +               (instance->completion_remove + MAX_COMPLETIONS)) {
2484 +               /* Out of space - wait for the client */
2485 +               DEBUG_TRACE(SERVICE_CALLBACK_LINE);
2486 +               vchiq_log_trace(vchiq_arm_log_level,
2487 +                       "add_completion - completion queue full");
2488 +               DEBUG_COUNT(COMPLETION_QUEUE_FULL_COUNT);
2489 +               if (down_interruptible(&instance->remove_event) != 0) {
2490 +                       vchiq_log_info(vchiq_arm_log_level,
2491 +                               "service_callback interrupted");
2492 +                       return VCHIQ_RETRY;
2493 +               } else if (instance->closing) {
2494 +                       vchiq_log_info(vchiq_arm_log_level,
2495 +                               "service_callback closing");
2496 +                       return VCHIQ_ERROR;
2497 +               }
2498 +               DEBUG_TRACE(SERVICE_CALLBACK_LINE);
2499 +       }
2500 +
2501 +       completion =
2502 +                &instance->completions[instance->completion_insert &
2503 +                (MAX_COMPLETIONS - 1)];
2504 +
2505 +       completion->header = header;
2506 +       completion->reason = reason;
2507 +       /* N.B. service_userdata is updated while processing AWAIT_COMPLETION */
2508 +       completion->service_userdata = user_service->service;
2509 +       completion->bulk_userdata = bulk_userdata;
2510 +
2511 +       if (reason == VCHIQ_SERVICE_CLOSED) {
2512 +               /* Take an extra reference, to be held until
2513 +                  this CLOSED notification is delivered. */
2514 +               lock_service(user_service->service);
2515 +               if (instance->use_close_delivered)
2516 +                       user_service->close_pending = 1;
2517 +       }
2518 +
2519 +       /* A write barrier is needed here to ensure that the entire completion
2520 +               record is written out before the insert point. */
2521 +       wmb();
2522 +
2523 +       if (reason == VCHIQ_MESSAGE_AVAILABLE)
2524 +               user_service->message_available_pos =
2525 +                       instance->completion_insert;
2526 +       instance->completion_insert++;
2527 +
2528 +       up(&instance->insert_event);
2529 +
2530 +       return VCHIQ_SUCCESS;
2531 +}
2532 +
2533 +/****************************************************************************
2534 +*
2535 +*   service_callback
2536 +*
2537 +***************************************************************************/
2538 +
2539 +static VCHIQ_STATUS_T
2540 +service_callback(VCHIQ_REASON_T reason, VCHIQ_HEADER_T *header,
2541 +       VCHIQ_SERVICE_HANDLE_T handle, void *bulk_userdata)
2542 +{
2543 +       /* How do we ensure the callback goes to the right client?
2544 +       ** The service_user data points to a USER_SERVICE_T record containing
2545 +       ** the original callback and the user state structure, which contains a
2546 +       ** circular buffer for completion records.
2547 +       */
2548 +       USER_SERVICE_T *user_service;
2549 +       VCHIQ_SERVICE_T *service;
2550 +       VCHIQ_INSTANCE_T instance;
2551 +       DEBUG_INITIALISE(g_state.local)
2552 +
2553 +       DEBUG_TRACE(SERVICE_CALLBACK_LINE);
2554 +
2555 +       service = handle_to_service(handle);
2556 +       BUG_ON(!service);
2557 +       user_service = (USER_SERVICE_T *)service->base.userdata;
2558 +       instance = user_service->instance;
2559 +
2560 +       if (!instance || instance->closing)
2561 +               return VCHIQ_SUCCESS;
2562 +
2563 +       vchiq_log_trace(vchiq_arm_log_level,
2564 +               "service_callback - service %lx(%d,%p), reason %d, header %lx, "
2565 +               "instance %lx, bulk_userdata %lx",
2566 +               (unsigned long)user_service,
2567 +               service->localport, user_service->userdata,
2568 +               reason, (unsigned long)header,
2569 +               (unsigned long)instance, (unsigned long)bulk_userdata);
2570 +
2571 +       if (header && user_service->is_vchi) {
2572 +               spin_lock(&msg_queue_spinlock);
2573 +               while (user_service->msg_insert ==
2574 +                       (user_service->msg_remove + MSG_QUEUE_SIZE)) {
2575 +                       spin_unlock(&msg_queue_spinlock);
2576 +                       DEBUG_TRACE(SERVICE_CALLBACK_LINE);
2577 +                       DEBUG_COUNT(MSG_QUEUE_FULL_COUNT);
2578 +                       vchiq_log_trace(vchiq_arm_log_level,
2579 +                               "service_callback - msg queue full");
2580 +                       /* If there is no MESSAGE_AVAILABLE in the completion
2581 +                       ** queue, add one
2582 +                       */
2583 +                       if ((user_service->message_available_pos -
2584 +                               instance->completion_remove) < 0) {
2585 +                               VCHIQ_STATUS_T status;
2586 +                               vchiq_log_info(vchiq_arm_log_level,
2587 +                                       "Inserting extra MESSAGE_AVAILABLE");
2588 +                               DEBUG_TRACE(SERVICE_CALLBACK_LINE);
2589 +                               status = add_completion(instance, reason,
2590 +                                       NULL, user_service, bulk_userdata);
2591 +                               if (status != VCHIQ_SUCCESS) {
2592 +                                       DEBUG_TRACE(SERVICE_CALLBACK_LINE);
2593 +                                       return status;
2594 +                               }
2595 +                       }
2596 +
2597 +                       DEBUG_TRACE(SERVICE_CALLBACK_LINE);
2598 +                       if (down_interruptible(&user_service->remove_event)
2599 +                               != 0) {
2600 +                               vchiq_log_info(vchiq_arm_log_level,
2601 +                                       "service_callback interrupted");
2602 +                               DEBUG_TRACE(SERVICE_CALLBACK_LINE);
2603 +                               return VCHIQ_RETRY;
2604 +                       } else if (instance->closing) {
2605 +                               vchiq_log_info(vchiq_arm_log_level,
2606 +                                       "service_callback closing");
2607 +                               DEBUG_TRACE(SERVICE_CALLBACK_LINE);
2608 +                               return VCHIQ_ERROR;
2609 +                       }
2610 +                       DEBUG_TRACE(SERVICE_CALLBACK_LINE);
2611 +                       spin_lock(&msg_queue_spinlock);
2612 +               }
2613 +
2614 +               user_service->msg_queue[user_service->msg_insert &
2615 +                       (MSG_QUEUE_SIZE - 1)] = header;
2616 +               user_service->msg_insert++;
2617 +               spin_unlock(&msg_queue_spinlock);
2618 +
2619 +               up(&user_service->insert_event);
2620 +
2621 +               /* If there is a thread waiting in DEQUEUE_MESSAGE, or if
2622 +               ** there is a MESSAGE_AVAILABLE in the completion queue then
2623 +               ** bypass the completion queue.
2624 +               */
2625 +               if (((user_service->message_available_pos -
2626 +                       instance->completion_remove) >= 0) ||
2627 +                       user_service->dequeue_pending) {
2628 +                       DEBUG_TRACE(SERVICE_CALLBACK_LINE);
2629 +                       user_service->dequeue_pending = 0;
2630 +                       return VCHIQ_SUCCESS;
2631 +               }
2632 +
2633 +               header = NULL;
2634 +       }
2635 +       DEBUG_TRACE(SERVICE_CALLBACK_LINE);
2636 +
2637 +       return add_completion(instance, reason, header, user_service,
2638 +               bulk_userdata);
2639 +}
2640 +
2641 +/****************************************************************************
2642 +*
2643 +*   user_service_free
2644 +*
2645 +***************************************************************************/
2646 +static void
2647 +user_service_free(void *userdata)
2648 +{
2649 +       kfree(userdata);
2650 +}
2651 +
2652 +/****************************************************************************
2653 +*
2654 +*   close_delivered
2655 +*
2656 +***************************************************************************/
2657 +static void close_delivered(USER_SERVICE_T *user_service)
2658 +{
2659 +       vchiq_log_info(vchiq_arm_log_level,
2660 +               "close_delivered(handle=%x)",
2661 +               user_service->service->handle);
2662 +
2663 +       if (user_service->close_pending) {
2664 +               /* Allow the underlying service to be culled */
2665 +               unlock_service(user_service->service);
2666 +
2667 +               /* Wake the user-thread blocked in close_ or remove_service */
2668 +               up(&user_service->close_event);
2669 +
2670 +               user_service->close_pending = 0;
2671 +       }
2672 +}
2673 +
2674 +/****************************************************************************
2675 +*
2676 +*   vchiq_ioctl
2677 +*
2678 +***************************************************************************/
2679 +static long
2680 +vchiq_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2681 +{
2682 +       VCHIQ_INSTANCE_T instance = file->private_data;
2683 +       VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
2684 +       VCHIQ_SERVICE_T *service = NULL;
2685 +       long ret = 0;
2686 +       int i, rc;
2687 +       DEBUG_INITIALISE(g_state.local)
2688 +
2689 +       vchiq_log_trace(vchiq_arm_log_level,
2690 +                "vchiq_ioctl - instance %x, cmd %s, arg %lx",
2691 +               (unsigned int)instance,
2692 +               ((_IOC_TYPE(cmd) == VCHIQ_IOC_MAGIC) &&
2693 +               (_IOC_NR(cmd) <= VCHIQ_IOC_MAX)) ?
2694 +               ioctl_names[_IOC_NR(cmd)] : "<invalid>", arg);
2695 +
2696 +       switch (cmd) {
2697 +       case VCHIQ_IOC_SHUTDOWN:
2698 +               if (!instance->connected)
2699 +                       break;
2700 +
2701 +               /* Remove all services */
2702 +               i = 0;
2703 +               while ((service = next_service_by_instance(instance->state,
2704 +                       instance, &i)) != NULL) {
2705 +                       status = vchiq_remove_service(service->handle);
2706 +                       unlock_service(service);
2707 +                       if (status != VCHIQ_SUCCESS)
2708 +                               break;
2709 +               }
2710 +               service = NULL;
2711 +
2712 +               if (status == VCHIQ_SUCCESS) {
2713 +                       /* Wake the completion thread and ask it to exit */
2714 +                       instance->closing = 1;
2715 +                       up(&instance->insert_event);
2716 +               }
2717 +
2718 +               break;
2719 +
2720 +       case VCHIQ_IOC_CONNECT:
2721 +               if (instance->connected) {
2722 +                       ret = -EINVAL;
2723 +                       break;
2724 +               }
2725 +               rc = mutex_lock_interruptible(&instance->state->mutex);
2726 +               if (rc != 0) {
2727 +                       vchiq_log_error(vchiq_arm_log_level,
2728 +                               "vchiq: connect: could not lock mutex for "
2729 +                               "state %d: %d",
2730 +                               instance->state->id, rc);
2731 +                       ret = -EINTR;
2732 +                       break;
2733 +               }
2734 +               status = vchiq_connect_internal(instance->state, instance);
2735 +               mutex_unlock(&instance->state->mutex);
2736 +
2737 +               if (status == VCHIQ_SUCCESS)
2738 +                       instance->connected = 1;
2739 +               else
2740 +                       vchiq_log_error(vchiq_arm_log_level,
2741 +                               "vchiq: could not connect: %d", status);
2742 +               break;
2743 +
2744 +       case VCHIQ_IOC_CREATE_SERVICE: {
2745 +               VCHIQ_CREATE_SERVICE_T args;
2746 +               USER_SERVICE_T *user_service = NULL;
2747 +               void *userdata;
2748 +               int srvstate;
2749 +
2750 +               if (copy_from_user
2751 +                        (&args, (const void __user *)arg,
2752 +                         sizeof(args)) != 0) {
2753 +                       ret = -EFAULT;
2754 +                       break;
2755 +               }
2756 +
2757 +               user_service = kmalloc(sizeof(USER_SERVICE_T), GFP_KERNEL);
2758 +               if (!user_service) {
2759 +                       ret = -ENOMEM;
2760 +                       break;
2761 +               }
2762 +
2763 +               if (args.is_open) {
2764 +                       if (!instance->connected) {
2765 +                               ret = -ENOTCONN;
2766 +                               kfree(user_service);
2767 +                               break;
2768 +                       }
2769 +                       srvstate = VCHIQ_SRVSTATE_OPENING;
2770 +               } else {
2771 +                       srvstate =
2772 +                                instance->connected ?
2773 +                                VCHIQ_SRVSTATE_LISTENING :
2774 +                                VCHIQ_SRVSTATE_HIDDEN;
2775 +               }
2776 +
2777 +               userdata = args.params.userdata;
2778 +               args.params.callback = service_callback;
2779 +               args.params.userdata = user_service;
2780 +               service = vchiq_add_service_internal(
2781 +                               instance->state,
2782 +                               &args.params, srvstate,
2783 +                               instance, user_service_free);
2784 +
2785 +               if (service != NULL) {
2786 +                       user_service->service = service;
2787 +                       user_service->userdata = userdata;
2788 +                       user_service->instance = instance;
2789 +                       user_service->is_vchi = (args.is_vchi != 0);
2790 +                       user_service->dequeue_pending = 0;
2791 +                       user_service->close_pending = 0;
2792 +                       user_service->message_available_pos =
2793 +                               instance->completion_remove - 1;
2794 +                       user_service->msg_insert = 0;
2795 +                       user_service->msg_remove = 0;
2796 +                       sema_init(&user_service->insert_event, 0);
2797 +                       sema_init(&user_service->remove_event, 0);
2798 +                       sema_init(&user_service->close_event, 0);
2799 +
2800 +                       if (args.is_open) {
2801 +                               status = vchiq_open_service_internal
2802 +                                       (service, instance->pid);
2803 +                               if (status != VCHIQ_SUCCESS) {
2804 +                                       vchiq_remove_service(service->handle);
2805 +                                       service = NULL;
2806 +                                       ret = (status == VCHIQ_RETRY) ?
2807 +                                               -EINTR : -EIO;
2808 +                                       break;
2809 +                               }
2810 +                       }
2811 +
2812 +                       if (copy_to_user((void __user *)
2813 +                               &(((VCHIQ_CREATE_SERVICE_T __user *)
2814 +                                       arg)->handle),
2815 +                               (const void *)&service->handle,
2816 +                               sizeof(service->handle)) != 0) {
2817 +                               ret = -EFAULT;
2818 +                               vchiq_remove_service(service->handle);
2819 +                       }
2820 +
2821 +                       service = NULL;
2822 +               } else {
2823 +                       ret = -EEXIST;
2824 +                       kfree(user_service);
2825 +               }
2826 +       } break;
2827 +
2828 +       case VCHIQ_IOC_CLOSE_SERVICE: {
2829 +               VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
2830 +
2831 +               service = find_service_for_instance(instance, handle);
2832 +               if (service != NULL) {
2833 +                       USER_SERVICE_T *user_service =
2834 +                               (USER_SERVICE_T *)service->base.userdata;
2835 +                       /* close_pending is false on first entry, and when the
2836 +                           wait in vchiq_close_service has been interrupted. */
2837 +                       if (!user_service->close_pending) {
2838 +                               status = vchiq_close_service(service->handle);
2839 +                               if (status != VCHIQ_SUCCESS)
2840 +                                       break;
2841 +                       }
2842 +
2843 +                       /* close_pending is true once the underlying service
2844 +                          has been closed until the client library calls the
2845 +                          CLOSE_DELIVERED ioctl, signalling close_event. */
2846 +                       if (user_service->close_pending &&
2847 +                               down_interruptible(&user_service->close_event))
2848 +                               status = VCHIQ_RETRY;
2849 +               }
2850 +               else
2851 +                       ret = -EINVAL;
2852 +       } break;
2853 +
2854 +       case VCHIQ_IOC_REMOVE_SERVICE: {
2855 +               VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
2856 +
2857 +               service = find_service_for_instance(instance, handle);
2858 +               if (service != NULL) {
2859 +                       USER_SERVICE_T *user_service =
2860 +                               (USER_SERVICE_T *)service->base.userdata;
2861 +                       /* close_pending is false on first entry, and when the
2862 +                           wait in vchiq_close_service has been interrupted. */
2863 +                       if (!user_service->close_pending) {
2864 +                               status = vchiq_remove_service(service->handle);
2865 +                               if (status != VCHIQ_SUCCESS)
2866 +                                       break;
2867 +                       }
2868 +
2869 +                       /* close_pending is true once the underlying service
2870 +                          has been closed until the client library calls the
2871 +                          CLOSE_DELIVERED ioctl, signalling close_event. */
2872 +                       if (user_service->close_pending &&
2873 +                               down_interruptible(&user_service->close_event))
2874 +                               status = VCHIQ_RETRY;
2875 +               }
2876 +               else
2877 +                       ret = -EINVAL;
2878 +       } break;
2879 +
2880 +       case VCHIQ_IOC_USE_SERVICE:
2881 +       case VCHIQ_IOC_RELEASE_SERVICE: {
2882 +               VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
2883 +
2884 +               service = find_service_for_instance(instance, handle);
2885 +               if (service != NULL) {
2886 +                       status = (cmd == VCHIQ_IOC_USE_SERVICE) ?
2887 +                               vchiq_use_service_internal(service) :
2888 +                               vchiq_release_service_internal(service);
2889 +                       if (status != VCHIQ_SUCCESS) {
2890 +                               vchiq_log_error(vchiq_susp_log_level,
2891 +                                       "%s: cmd %s returned error %d for "
2892 +                                       "service %c%c%c%c:%03d",
2893 +                                       __func__,
2894 +                                       (cmd == VCHIQ_IOC_USE_SERVICE) ?
2895 +                                               "VCHIQ_IOC_USE_SERVICE" :
2896 +                                               "VCHIQ_IOC_RELEASE_SERVICE",
2897 +                                       status,
2898 +                                       VCHIQ_FOURCC_AS_4CHARS(
2899 +                                               service->base.fourcc),
2900 +                                       service->client_id);
2901 +                               ret = -EINVAL;
2902 +                       }
2903 +               } else
2904 +                       ret = -EINVAL;
2905 +       } break;
2906 +
2907 +       case VCHIQ_IOC_QUEUE_MESSAGE: {
2908 +               VCHIQ_QUEUE_MESSAGE_T args;
2909 +               if (copy_from_user
2910 +                        (&args, (const void __user *)arg,
2911 +                         sizeof(args)) != 0) {
2912 +                       ret = -EFAULT;
2913 +                       break;
2914 +               }
2915 +
2916 +               service = find_service_for_instance(instance, args.handle);
2917 +
2918 +               if ((service != NULL) && (args.count <= MAX_ELEMENTS)) {
2919 +                       /* Copy elements into kernel space */
2920 +                       VCHIQ_ELEMENT_T elements[MAX_ELEMENTS];
2921 +                       if (copy_from_user(elements, args.elements,
2922 +                               args.count * sizeof(VCHIQ_ELEMENT_T)) == 0)
2923 +                               status = vchiq_queue_message
2924 +                                       (args.handle,
2925 +                                       elements, args.count);
2926 +                       else
2927 +                               ret = -EFAULT;
2928 +               } else {
2929 +                       ret = -EINVAL;
2930 +               }
2931 +       } break;
2932 +
2933 +       case VCHIQ_IOC_QUEUE_BULK_TRANSMIT:
2934 +       case VCHIQ_IOC_QUEUE_BULK_RECEIVE: {
2935 +               VCHIQ_QUEUE_BULK_TRANSFER_T args;
2936 +               struct bulk_waiter_node *waiter = NULL;
2937 +               VCHIQ_BULK_DIR_T dir =
2938 +                       (cmd == VCHIQ_IOC_QUEUE_BULK_TRANSMIT) ?
2939 +                       VCHIQ_BULK_TRANSMIT : VCHIQ_BULK_RECEIVE;
2940 +
2941 +               if (copy_from_user
2942 +                       (&args, (const void __user *)arg,
2943 +                       sizeof(args)) != 0) {
2944 +                       ret = -EFAULT;
2945 +                       break;
2946 +               }
2947 +
2948 +               service = find_service_for_instance(instance, args.handle);
2949 +               if (!service) {
2950 +                       ret = -EINVAL;
2951 +                       break;
2952 +               }
2953 +
2954 +               if (args.mode == VCHIQ_BULK_MODE_BLOCKING) {
2955 +                       waiter = kzalloc(sizeof(struct bulk_waiter_node),
2956 +                               GFP_KERNEL);
2957 +                       if (!waiter) {
2958 +                               ret = -ENOMEM;
2959 +                               break;
2960 +                       }
2961 +                       args.userdata = &waiter->bulk_waiter;
2962 +               } else if (args.mode == VCHIQ_BULK_MODE_WAITING) {
2963 +                       struct list_head *pos;
2964 +                       mutex_lock(&instance->bulk_waiter_list_mutex);
2965 +                       list_for_each(pos, &instance->bulk_waiter_list) {
2966 +                               if (list_entry(pos, struct bulk_waiter_node,
2967 +                                       list)->pid == current->pid) {
2968 +                                       waiter = list_entry(pos,
2969 +                                               struct bulk_waiter_node,
2970 +                                               list);
2971 +                                       list_del(pos);
2972 +                                       break;
2973 +                               }
2974 +
2975 +                       }
2976 +                       mutex_unlock(&instance->bulk_waiter_list_mutex);
2977 +                       if (!waiter) {
2978 +                               vchiq_log_error(vchiq_arm_log_level,
2979 +                                       "no bulk_waiter found for pid %d",
2980 +                                       current->pid);
2981 +                               ret = -ESRCH;
2982 +                               break;
2983 +                       }
2984 +                       vchiq_log_info(vchiq_arm_log_level,
2985 +                               "found bulk_waiter %x for pid %d",
2986 +                               (unsigned int)waiter, current->pid);
2987 +                       args.userdata = &waiter->bulk_waiter;
2988 +               }
2989 +               status = vchiq_bulk_transfer
2990 +                       (args.handle,
2991 +                        VCHI_MEM_HANDLE_INVALID,
2992 +                        args.data, args.size,
2993 +                        args.userdata, args.mode,
2994 +                        dir);
2995 +               if (!waiter)
2996 +                       break;
2997 +               if ((status != VCHIQ_RETRY) || fatal_signal_pending(current) ||
2998 +                       !waiter->bulk_waiter.bulk) {
2999 +                       if (waiter->bulk_waiter.bulk) {
3000 +                               /* Cancel the signal when the transfer
3001 +                               ** completes. */
3002 +                               spin_lock(&bulk_waiter_spinlock);
3003 +                               waiter->bulk_waiter.bulk->userdata = NULL;
3004 +                               spin_unlock(&bulk_waiter_spinlock);
3005 +                       }
3006 +                       kfree(waiter);
3007 +               } else {
3008 +                       const VCHIQ_BULK_MODE_T mode_waiting =
3009 +                               VCHIQ_BULK_MODE_WAITING;
3010 +                       waiter->pid = current->pid;
3011 +                       mutex_lock(&instance->bulk_waiter_list_mutex);
3012 +                       list_add(&waiter->list, &instance->bulk_waiter_list);
3013 +                       mutex_unlock(&instance->bulk_waiter_list_mutex);
3014 +                       vchiq_log_info(vchiq_arm_log_level,
3015 +                               "saved bulk_waiter %x for pid %d",
3016 +                               (unsigned int)waiter, current->pid);
3017 +
3018 +                       if (copy_to_user((void __user *)
3019 +                               &(((VCHIQ_QUEUE_BULK_TRANSFER_T __user *)
3020 +                                       arg)->mode),
3021 +                               (const void *)&mode_waiting,
3022 +                               sizeof(mode_waiting)) != 0)
3023 +                               ret = -EFAULT;
3024 +               }
3025 +       } break;
3026 +
3027 +       case VCHIQ_IOC_AWAIT_COMPLETION: {
3028 +               VCHIQ_AWAIT_COMPLETION_T args;
3029 +
3030 +               DEBUG_TRACE(AWAIT_COMPLETION_LINE);
3031 +               if (!instance->connected) {
3032 +                       ret = -ENOTCONN;
3033 +                       break;
3034 +               }
3035 +
3036 +               if (copy_from_user(&args, (const void __user *)arg,
3037 +                       sizeof(args)) != 0) {
3038 +                       ret = -EFAULT;
3039 +                       break;
3040 +               }
3041 +
3042 +               mutex_lock(&instance->completion_mutex);
3043 +
3044 +               DEBUG_TRACE(AWAIT_COMPLETION_LINE);
3045 +               while ((instance->completion_remove ==
3046 +                       instance->completion_insert)
3047 +                       && !instance->closing) {
3048 +                       int rc;
3049 +                       DEBUG_TRACE(AWAIT_COMPLETION_LINE);
3050 +                       mutex_unlock(&instance->completion_mutex);
3051 +                       rc = down_interruptible(&instance->insert_event);
3052 +                       mutex_lock(&instance->completion_mutex);
3053 +                       if (rc != 0) {
3054 +                               DEBUG_TRACE(AWAIT_COMPLETION_LINE);
3055 +                               vchiq_log_info(vchiq_arm_log_level,
3056 +                                       "AWAIT_COMPLETION interrupted");
3057 +                               ret = -EINTR;
3058 +                               break;
3059 +                       }
3060 +               }
3061 +               DEBUG_TRACE(AWAIT_COMPLETION_LINE);
3062 +
3063 +               /* A read memory barrier is needed to stop prefetch of a stale
3064 +               ** completion record
3065 +               */
3066 +               rmb();
3067 +
3068 +               if (ret == 0) {
3069 +                       int msgbufcount = args.msgbufcount;
3070 +                       for (ret = 0; ret < args.count; ret++) {
3071 +                               VCHIQ_COMPLETION_DATA_T *completion;
3072 +                               VCHIQ_SERVICE_T *service;
3073 +                               USER_SERVICE_T *user_service;
3074 +                               VCHIQ_HEADER_T *header;
3075 +                               if (instance->completion_remove ==
3076 +                                       instance->completion_insert)
3077 +                                       break;
3078 +                               completion = &instance->completions[
3079 +                                       instance->completion_remove &
3080 +                                       (MAX_COMPLETIONS - 1)];
3081 +
3082 +                               service = completion->service_userdata;
3083 +                               user_service = service->base.userdata;
3084 +                               completion->service_userdata =
3085 +                                       user_service->userdata;
3086 +
3087 +                               header = completion->header;
3088 +                               if (header) {
3089 +                                       void __user *msgbuf;
3090 +                                       int msglen;
3091 +
3092 +                                       msglen = header->size +
3093 +                                               sizeof(VCHIQ_HEADER_T);
3094 +                                       /* This must be a VCHIQ-style service */
3095 +                                       if (args.msgbufsize < msglen) {
3096 +                                               vchiq_log_error(
3097 +                                                       vchiq_arm_log_level,
3098 +                                                       "header %x: msgbufsize"
3099 +                                                       " %x < msglen %x",
3100 +                                                       (unsigned int)header,
3101 +                                                       args.msgbufsize,
3102 +                                                       msglen);
3103 +                                               WARN(1, "invalid message "
3104 +                                                       "size\n");
3105 +                                               if (ret == 0)
3106 +                                                       ret = -EMSGSIZE;
3107 +                                               break;
3108 +                                       }
3109 +                                       if (msgbufcount <= 0)
3110 +                                               /* Stall here for lack of a
3111 +                                               ** buffer for the message. */
3112 +                                               break;
3113 +                                       /* Get the pointer from user space */
3114 +                                       msgbufcount--;
3115 +                                       if (copy_from_user(&msgbuf,
3116 +                                               (const void __user *)
3117 +                                               &args.msgbufs[msgbufcount],
3118 +                                               sizeof(msgbuf)) != 0) {
3119 +                                               if (ret == 0)
3120 +                                                       ret = -EFAULT;
3121 +                                               break;
3122 +                                       }
3123 +
3124 +                                       /* Copy the message to user space */
3125 +                                       if (copy_to_user(msgbuf, header,
3126 +                                               msglen) != 0) {
3127 +                                               if (ret == 0)
3128 +                                                       ret = -EFAULT;
3129 +                                               break;
3130 +                                       }
3131 +
3132 +                                       /* Now it has been copied, the message
3133 +                                       ** can be released. */
3134 +                                       vchiq_release_message(service->handle,
3135 +                                               header);
3136 +
3137 +                                       /* The completion must point to the
3138 +                                       ** msgbuf. */
3139 +                                       completion->header = msgbuf;
3140 +                               }
3141 +
3142 +                               if ((completion->reason ==
3143 +                                       VCHIQ_SERVICE_CLOSED) &&
3144 +                                       !instance->use_close_delivered)
3145 +                                       unlock_service(service);
3146 +
3147 +                               if (copy_to_user((void __user *)(
3148 +                                       (size_t)args.buf +
3149 +                                       ret * sizeof(VCHIQ_COMPLETION_DATA_T)),
3150 +                                       completion,
3151 +                                       sizeof(VCHIQ_COMPLETION_DATA_T)) != 0) {
3152 +                                               if (ret == 0)
3153 +                                                       ret = -EFAULT;
3154 +                                       break;
3155 +                               }
3156 +
3157 +                               instance->completion_remove++;
3158 +                       }
3159 +
3160 +                       if (msgbufcount != args.msgbufcount) {
3161 +                               if (copy_to_user((void __user *)
3162 +                                       &((VCHIQ_AWAIT_COMPLETION_T *)arg)->
3163 +                                               msgbufcount,
3164 +                                       &msgbufcount,
3165 +                                       sizeof(msgbufcount)) != 0) {
3166 +                                       ret = -EFAULT;
3167 +                               }
3168 +                       }
3169 +               }
3170 +
3171 +               if (ret != 0)
3172 +                       up(&instance->remove_event);
3173 +               mutex_unlock(&instance->completion_mutex);
3174 +               DEBUG_TRACE(AWAIT_COMPLETION_LINE);
3175 +       } break;
3176 +
3177 +       case VCHIQ_IOC_DEQUEUE_MESSAGE: {
3178 +               VCHIQ_DEQUEUE_MESSAGE_T args;
3179 +               USER_SERVICE_T *user_service;
3180 +               VCHIQ_HEADER_T *header;
3181 +
3182 +               DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
3183 +               if (copy_from_user
3184 +                        (&args, (const void __user *)arg,
3185 +                         sizeof(args)) != 0) {
3186 +                       ret = -EFAULT;
3187 +                       break;
3188 +               }
3189 +               service = find_service_for_instance(instance, args.handle);
3190 +               if (!service) {
3191 +                       ret = -EINVAL;
3192 +                       break;
3193 +               }
3194 +               user_service = (USER_SERVICE_T *)service->base.userdata;
3195 +               if (user_service->is_vchi == 0) {
3196 +                       ret = -EINVAL;
3197 +                       break;
3198 +               }
3199 +
3200 +               spin_lock(&msg_queue_spinlock);
3201 +               if (user_service->msg_remove == user_service->msg_insert) {
3202 +                       if (!args.blocking) {
3203 +                               spin_unlock(&msg_queue_spinlock);
3204 +                               DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
3205 +                               ret = -EWOULDBLOCK;
3206 +                               break;
3207 +                       }
3208 +                       user_service->dequeue_pending = 1;
3209 +                       do {
3210 +                               spin_unlock(&msg_queue_spinlock);
3211 +                               DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
3212 +                               if (down_interruptible(
3213 +                                       &user_service->insert_event) != 0) {
3214 +                                       vchiq_log_info(vchiq_arm_log_level,
3215 +                                               "DEQUEUE_MESSAGE interrupted");
3216 +                                       ret = -EINTR;
3217 +                                       break;
3218 +                               }
3219 +                               spin_lock(&msg_queue_spinlock);
3220 +                       } while (user_service->msg_remove ==
3221 +                               user_service->msg_insert);
3222 +
3223 +                       if (ret)
3224 +                               break;
3225 +               }
3226 +
3227 +               BUG_ON((int)(user_service->msg_insert -
3228 +                       user_service->msg_remove) < 0);
3229 +
3230 +               header = user_service->msg_queue[user_service->msg_remove &
3231 +                       (MSG_QUEUE_SIZE - 1)];
3232 +               user_service->msg_remove++;
3233 +               spin_unlock(&msg_queue_spinlock);
3234 +
3235 +               up(&user_service->remove_event);
3236 +               if (header == NULL)
3237 +                       ret = -ENOTCONN;
3238 +               else if (header->size <= args.bufsize) {
3239 +                       /* Copy to user space if msgbuf is not NULL */
3240 +                       if ((args.buf == NULL) ||
3241 +                               (copy_to_user((void __user *)args.buf,
3242 +                               header->data,
3243 +                               header->size) == 0)) {
3244 +                               ret = header->size;
3245 +                               vchiq_release_message(
3246 +                                       service->handle,
3247 +                                       header);
3248 +                       } else
3249 +                               ret = -EFAULT;
3250 +               } else {
3251 +                       vchiq_log_error(vchiq_arm_log_level,
3252 +                               "header %x: bufsize %x < size %x",
3253 +                               (unsigned int)header, args.bufsize,
3254 +                               header->size);
3255 +                       WARN(1, "invalid size\n");
3256 +                       ret = -EMSGSIZE;
3257 +               }
3258 +               DEBUG_TRACE(DEQUEUE_MESSAGE_LINE);
3259 +       } break;
3260 +
3261 +       case VCHIQ_IOC_GET_CLIENT_ID: {
3262 +               VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
3263 +
3264 +               ret = vchiq_get_client_id(handle);
3265 +       } break;
3266 +
3267 +       case VCHIQ_IOC_GET_CONFIG: {
3268 +               VCHIQ_GET_CONFIG_T args;
3269 +               VCHIQ_CONFIG_T config;
3270 +
3271 +               if (copy_from_user(&args, (const void __user *)arg,
3272 +                       sizeof(args)) != 0) {
3273 +                       ret = -EFAULT;
3274 +                       break;
3275 +               }
3276 +               if (args.config_size > sizeof(config)) {
3277 +                       ret = -EINVAL;
3278 +                       break;
3279 +               }
3280 +               status = vchiq_get_config(instance, args.config_size, &config);
3281 +               if (status == VCHIQ_SUCCESS) {
3282 +                       if (copy_to_user((void __user *)args.pconfig,
3283 +                                   &config, args.config_size) != 0) {
3284 +                               ret = -EFAULT;
3285 +                               break;
3286 +                       }
3287 +               }
3288 +       } break;
3289 +
3290 +       case VCHIQ_IOC_SET_SERVICE_OPTION: {
3291 +               VCHIQ_SET_SERVICE_OPTION_T args;
3292 +
3293 +               if (copy_from_user(
3294 +                       &args, (const void __user *)arg,
3295 +                       sizeof(args)) != 0) {
3296 +                       ret = -EFAULT;
3297 +                       break;
3298 +               }
3299 +
3300 +               service = find_service_for_instance(instance, args.handle);
3301 +               if (!service) {
3302 +                       ret = -EINVAL;
3303 +                       break;
3304 +               }
3305 +
3306 +               status = vchiq_set_service_option(
3307 +                               args.handle, args.option, args.value);
3308 +       } break;
3309 +
3310 +       case VCHIQ_IOC_DUMP_PHYS_MEM: {
3311 +               VCHIQ_DUMP_MEM_T  args;
3312 +
3313 +               if (copy_from_user
3314 +                        (&args, (const void __user *)arg,
3315 +                         sizeof(args)) != 0) {
3316 +                       ret = -EFAULT;
3317 +                       break;
3318 +               }
3319 +               dump_phys_mem(args.virt_addr, args.num_bytes);
3320 +       } break;
3321 +
3322 +       case VCHIQ_IOC_LIB_VERSION: {
3323 +               unsigned int lib_version = (unsigned int)arg;
3324 +
3325 +               if (lib_version < VCHIQ_VERSION_MIN)
3326 +                       ret = -EINVAL;
3327 +               else if (lib_version >= VCHIQ_VERSION_CLOSE_DELIVERED)
3328 +                       instance->use_close_delivered = 1;
3329 +       } break;
3330 +
3331 +       case VCHIQ_IOC_CLOSE_DELIVERED: {
3332 +               VCHIQ_SERVICE_HANDLE_T handle = (VCHIQ_SERVICE_HANDLE_T)arg;
3333 +
3334 +               service = find_closed_service_for_instance(instance, handle);
3335 +               if (service != NULL) {
3336 +                       USER_SERVICE_T *user_service =
3337 +                               (USER_SERVICE_T *)service->base.userdata;
3338 +                       close_delivered(user_service);
3339 +               }
3340 +               else
3341 +                       ret = -EINVAL;
3342 +       } break;
3343 +
3344 +       default:
3345 +               ret = -ENOTTY;
3346 +               break;
3347 +       }
3348 +
3349 +       if (service)
3350 +               unlock_service(service);
3351 +
3352 +       if (ret == 0) {
3353 +               if (status == VCHIQ_ERROR)
3354 +                       ret = -EIO;
3355 +               else if (status == VCHIQ_RETRY)
3356 +                       ret = -EINTR;
3357 +       }
3358 +
3359 +       if ((status == VCHIQ_SUCCESS) && (ret < 0) && (ret != -EINTR) &&
3360 +               (ret != -EWOULDBLOCK))
3361 +               vchiq_log_info(vchiq_arm_log_level,
3362 +                       "  ioctl instance %lx, cmd %s -> status %d, %ld",
3363 +                       (unsigned long)instance,
3364 +                       (_IOC_NR(cmd) <= VCHIQ_IOC_MAX) ?
3365 +                               ioctl_names[_IOC_NR(cmd)] :
3366 +                               "<invalid>",
3367 +                       status, ret);
3368 +       else
3369 +               vchiq_log_trace(vchiq_arm_log_level,
3370 +                       "  ioctl instance %lx, cmd %s -> status %d, %ld",
3371 +                       (unsigned long)instance,
3372 +                       (_IOC_NR(cmd) <= VCHIQ_IOC_MAX) ?
3373 +                               ioctl_names[_IOC_NR(cmd)] :
3374 +                               "<invalid>",
3375 +                       status, ret);
3376 +
3377 +       return ret;
3378 +}
3379 +
3380 +/****************************************************************************
3381 +*
3382 +*   vchiq_open
3383 +*
3384 +***************************************************************************/
3385 +
3386 +static int
3387 +vchiq_open(struct inode *inode, struct file *file)
3388 +{
3389 +       int dev = iminor(inode) & 0x0f;
3390 +       vchiq_log_info(vchiq_arm_log_level, "vchiq_open");
3391 +       switch (dev) {
3392 +       case VCHIQ_MINOR: {
3393 +               int ret;
3394 +               VCHIQ_STATE_T *state = vchiq_get_state();
3395 +               VCHIQ_INSTANCE_T instance;
3396 +
3397 +               if (!state) {
3398 +                       vchiq_log_error(vchiq_arm_log_level,
3399 +                               "vchiq has no connection to VideoCore");
3400 +                       return -ENOTCONN;
3401 +               }
3402 +
3403 +               instance = kzalloc(sizeof(*instance), GFP_KERNEL);
3404 +               if (!instance)
3405 +                       return -ENOMEM;
3406 +
3407 +               instance->state = state;
3408 +               instance->pid = current->tgid;
3409 +
3410 +               ret = vchiq_debugfs_add_instance(instance);
3411 +               if (ret != 0) {
3412 +                       kfree(instance);
3413 +                       return ret;
3414 +               }
3415 +
3416 +               sema_init(&instance->insert_event, 0);
3417 +               sema_init(&instance->remove_event, 0);
3418 +               mutex_init(&instance->completion_mutex);
3419 +               mutex_init(&instance->bulk_waiter_list_mutex);
3420 +               INIT_LIST_HEAD(&instance->bulk_waiter_list);
3421 +
3422 +               file->private_data = instance;
3423 +       } break;
3424 +
3425 +       default:
3426 +               vchiq_log_error(vchiq_arm_log_level,
3427 +                       "Unknown minor device: %d", dev);
3428 +               return -ENXIO;
3429 +       }
3430 +
3431 +       return 0;
3432 +}
3433 +
3434 +/****************************************************************************
3435 +*
3436 +*   vchiq_release
3437 +*
3438 +***************************************************************************/
3439 +
3440 +static int
3441 +vchiq_release(struct inode *inode, struct file *file)
3442 +{
3443 +       int dev = iminor(inode) & 0x0f;
3444 +       int ret = 0;
3445 +       switch (dev) {
3446 +       case VCHIQ_MINOR: {
3447 +               VCHIQ_INSTANCE_T instance = file->private_data;
3448 +               VCHIQ_STATE_T *state = vchiq_get_state();
3449 +               VCHIQ_SERVICE_T *service;
3450 +               int i;
3451 +
3452 +               vchiq_log_info(vchiq_arm_log_level,
3453 +                       "vchiq_release: instance=%lx",
3454 +                       (unsigned long)instance);
3455 +
3456 +               if (!state) {
3457 +                       ret = -EPERM;
3458 +                       goto out;
3459 +               }
3460 +
3461 +               /* Ensure videocore is awake to allow termination. */
3462 +               vchiq_use_internal(instance->state, NULL,
3463 +                               USE_TYPE_VCHIQ);
3464 +
3465 +               mutex_lock(&instance->completion_mutex);
3466 +
3467 +               /* Wake the completion thread and ask it to exit */
3468 +               instance->closing = 1;
3469 +               up(&instance->insert_event);
3470 +
3471 +               mutex_unlock(&instance->completion_mutex);
3472 +
3473 +               /* Wake the slot handler if the completion queue is full. */
3474 +               up(&instance->remove_event);
3475 +
3476 +               /* Mark all services for termination... */
3477 +               i = 0;
3478 +               while ((service = next_service_by_instance(state, instance,
3479 +                       &i)) != NULL) {
3480 +                       USER_SERVICE_T *user_service = service->base.userdata;
3481 +
3482 +                       /* Wake the slot handler if the msg queue is full. */
3483 +                       up(&user_service->remove_event);
3484 +
3485 +                       vchiq_terminate_service_internal(service);
3486 +                       unlock_service(service);
3487 +               }
3488 +
3489 +               /* ...and wait for them to die */
3490 +               i = 0;
3491 +               while ((service = next_service_by_instance(state, instance, &i))
3492 +                       != NULL) {
3493 +                       USER_SERVICE_T *user_service = service->base.userdata;
3494 +
3495 +                       down(&service->remove_event);
3496 +
3497 +                       BUG_ON(service->srvstate != VCHIQ_SRVSTATE_FREE);
3498 +
3499 +                       spin_lock(&msg_queue_spinlock);
3500 +
3501 +                       while (user_service->msg_remove !=
3502 +                               user_service->msg_insert) {
3503 +                               VCHIQ_HEADER_T *header = user_service->
3504 +                                       msg_queue[user_service->msg_remove &
3505 +                                               (MSG_QUEUE_SIZE - 1)];
3506 +                               user_service->msg_remove++;
3507 +                               spin_unlock(&msg_queue_spinlock);
3508 +
3509 +                               if (header)
3510 +                                       vchiq_release_message(
3511 +                                               service->handle,
3512 +                                               header);
3513 +                               spin_lock(&msg_queue_spinlock);
3514 +                       }
3515 +
3516 +                       spin_unlock(&msg_queue_spinlock);
3517 +
3518 +                       unlock_service(service);
3519 +               }
3520 +
3521 +               /* Release any closed services */
3522 +               while (instance->completion_remove !=
3523 +                       instance->completion_insert) {
3524 +                       VCHIQ_COMPLETION_DATA_T *completion;
3525 +                       VCHIQ_SERVICE_T *service;
3526 +                       completion = &instance->completions[
3527 +                               instance->completion_remove &
3528 +                               (MAX_COMPLETIONS - 1)];
3529 +                       service = completion->service_userdata;
3530 +                       if (completion->reason == VCHIQ_SERVICE_CLOSED)
3531 +                       {
3532 +                               USER_SERVICE_T *user_service =
3533 +                                       service->base.userdata;
3534 +
3535 +                               /* Wake any blocked user-thread */
3536 +                               if (instance->use_close_delivered)
3537 +                                       up(&user_service->close_event);
3538 +                               unlock_service(service);
3539 +                       }
3540 +                       instance->completion_remove++;
3541 +               }
3542 +
3543 +               /* Release the PEER service count. */
3544 +               vchiq_release_internal(instance->state, NULL);
3545 +
3546 +               {
3547 +                       struct list_head *pos, *next;
3548 +                       list_for_each_safe(pos, next,
3549 +                               &instance->bulk_waiter_list) {
3550 +                               struct bulk_waiter_node *waiter;
3551 +                               waiter = list_entry(pos,
3552 +                                       struct bulk_waiter_node,
3553 +                                       list);
3554 +                               list_del(pos);
3555 +                               vchiq_log_info(vchiq_arm_log_level,
3556 +                                       "bulk_waiter - cleaned up %x "
3557 +                                       "for pid %d",
3558 +                                       (unsigned int)waiter, waiter->pid);
3559 +                               kfree(waiter);
3560 +                       }
3561 +               }
3562 +
3563 +               vchiq_debugfs_remove_instance(instance);
3564 +
3565 +               kfree(instance);
3566 +               file->private_data = NULL;
3567 +       } break;
3568 +
3569 +       default:
3570 +               vchiq_log_error(vchiq_arm_log_level,
3571 +                       "Unknown minor device: %d", dev);
3572 +               ret = -ENXIO;
3573 +       }
3574 +
3575 +out:
3576 +       return ret;
3577 +}
3578 +
3579 +/****************************************************************************
3580 +*
3581 +*   vchiq_dump
3582 +*
3583 +***************************************************************************/
3584 +
3585 +void
3586 +vchiq_dump(void *dump_context, const char *str, int len)
3587 +{
3588 +       DUMP_CONTEXT_T *context = (DUMP_CONTEXT_T *)dump_context;
3589 +
3590 +       if (context->actual < context->space) {
3591 +               int copy_bytes;
3592 +               if (context->offset > 0) {
3593 +                       int skip_bytes = min(len, (int)context->offset);
3594 +                       str += skip_bytes;
3595 +                       len -= skip_bytes;
3596 +                       context->offset -= skip_bytes;
3597 +                       if (context->offset > 0)
3598 +                               return;
3599 +               }
3600 +               copy_bytes = min(len, (int)(context->space - context->actual));
3601 +               if (copy_bytes == 0)
3602 +                       return;
3603 +               if (copy_to_user(context->buf + context->actual, str,
3604 +                       copy_bytes))
3605 +                       context->actual = -EFAULT;
3606 +               context->actual += copy_bytes;
3607 +               len -= copy_bytes;
3608 +
3609 +               /* If tne terminating NUL is included in the length, then it
3610 +               ** marks the end of a line and should be replaced with a
3611 +               ** carriage return. */
3612 +               if ((len == 0) && (str[copy_bytes - 1] == '\0')) {
3613 +                       char cr = '\n';
3614 +                       if (copy_to_user(context->buf + context->actual - 1,
3615 +                               &cr, 1))
3616 +                               context->actual = -EFAULT;
3617 +               }
3618 +       }
3619 +}
3620 +
3621 +/****************************************************************************
3622 +*
3623 +*   vchiq_dump_platform_instance_state
3624 +*
3625 +***************************************************************************/
3626 +
3627 +void
3628 +vchiq_dump_platform_instances(void *dump_context)
3629 +{
3630 +       VCHIQ_STATE_T *state = vchiq_get_state();
3631 +       char buf[80];
3632 +       int len;
3633 +       int i;
3634 +
3635 +       /* There is no list of instances, so instead scan all services,
3636 +               marking those that have been dumped. */
3637 +
3638 +       for (i = 0; i < state->unused_service; i++) {
3639 +               VCHIQ_SERVICE_T *service = state->services[i];
3640 +               VCHIQ_INSTANCE_T instance;
3641 +
3642 +               if (service && (service->base.callback == service_callback)) {
3643 +                       instance = service->instance;
3644 +                       if (instance)
3645 +                               instance->mark = 0;
3646 +               }
3647 +       }
3648 +
3649 +       for (i = 0; i < state->unused_service; i++) {
3650 +               VCHIQ_SERVICE_T *service = state->services[i];
3651 +               VCHIQ_INSTANCE_T instance;
3652 +
3653 +               if (service && (service->base.callback == service_callback)) {
3654 +                       instance = service->instance;
3655 +                       if (instance && !instance->mark) {
3656 +                               len = snprintf(buf, sizeof(buf),
3657 +                                       "Instance %x: pid %d,%s completions "
3658 +                                               "%d/%d",
3659 +                                       (unsigned int)instance, instance->pid,
3660 +                                       instance->connected ? " connected, " :
3661 +                                               "",
3662 +                                       instance->completion_insert -
3663 +                                               instance->completion_remove,
3664 +                                       MAX_COMPLETIONS);
3665 +
3666 +                               vchiq_dump(dump_context, buf, len + 1);
3667 +
3668 +                               instance->mark = 1;
3669 +                       }
3670 +               }
3671 +       }
3672 +}
3673 +
3674 +/****************************************************************************
3675 +*
3676 +*   vchiq_dump_platform_service_state
3677 +*
3678 +***************************************************************************/
3679 +
3680 +void
3681 +vchiq_dump_platform_service_state(void *dump_context, VCHIQ_SERVICE_T *service)
3682 +{
3683 +       USER_SERVICE_T *user_service = (USER_SERVICE_T *)service->base.userdata;
3684 +       char buf[80];
3685 +       int len;
3686 +
3687 +       len = snprintf(buf, sizeof(buf), "  instance %x",
3688 +               (unsigned int)service->instance);
3689 +
3690 +       if ((service->base.callback == service_callback) &&
3691 +               user_service->is_vchi) {
3692 +               len += snprintf(buf + len, sizeof(buf) - len,
3693 +                       ", %d/%d messages",
3694 +                       user_service->msg_insert - user_service->msg_remove,
3695 +                       MSG_QUEUE_SIZE);
3696 +
3697 +               if (user_service->dequeue_pending)
3698 +                       len += snprintf(buf + len, sizeof(buf) - len,
3699 +                               " (dequeue pending)");
3700 +       }
3701 +
3702 +       vchiq_dump(dump_context, buf, len + 1);
3703 +}
3704 +
3705 +/****************************************************************************
3706 +*
3707 +*   dump_user_mem
3708 +*
3709 +***************************************************************************/
3710 +
3711 +static void
3712 +dump_phys_mem(void *virt_addr, uint32_t num_bytes)
3713 +{
3714 +       int            rc;
3715 +       uint8_t       *end_virt_addr = virt_addr + num_bytes;
3716 +       int            num_pages;
3717 +       int            offset;
3718 +       int            end_offset;
3719 +       int            page_idx;
3720 +       int            prev_idx;
3721 +       struct page   *page;
3722 +       struct page  **pages;
3723 +       uint8_t       *kmapped_virt_ptr;
3724 +
3725 +       /* Align virtAddr and endVirtAddr to 16 byte boundaries. */
3726 +
3727 +       virt_addr = (void *)((unsigned long)virt_addr & ~0x0fuL);
3728 +       end_virt_addr = (void *)(((unsigned long)end_virt_addr + 15uL) &
3729 +               ~0x0fuL);
3730 +
3731 +       offset = (int)(long)virt_addr & (PAGE_SIZE - 1);
3732 +       end_offset = (int)(long)end_virt_addr & (PAGE_SIZE - 1);
3733 +
3734 +       num_pages = (offset + num_bytes + PAGE_SIZE - 1) / PAGE_SIZE;
3735 +
3736 +       pages = kmalloc(sizeof(struct page *) * num_pages, GFP_KERNEL);
3737 +       if (pages == NULL) {
3738 +               vchiq_log_error(vchiq_arm_log_level,
3739 +                       "Unable to allocation memory for %d pages\n",
3740 +                       num_pages);
3741 +               return;
3742 +       }
3743 +
3744 +       down_read(&current->mm->mmap_sem);
3745 +       rc = get_user_pages(current,      /* task */
3746 +               current->mm,              /* mm */
3747 +               (unsigned long)virt_addr, /* start */
3748 +               num_pages,                /* len */
3749 +               0,                        /* write */
3750 +               0,                        /* force */
3751 +               pages,                    /* pages (array of page pointers) */
3752 +               NULL);                    /* vmas */
3753 +       up_read(&current->mm->mmap_sem);
3754 +
3755 +       prev_idx = -1;
3756 +       page = NULL;
3757 +
3758 +       while (offset < end_offset) {
3759 +
3760 +               int page_offset = offset % PAGE_SIZE;
3761 +               page_idx = offset / PAGE_SIZE;
3762 +
3763 +               if (page_idx != prev_idx) {
3764 +
3765 +                       if (page != NULL)
3766 +                               kunmap(page);
3767 +                       page = pages[page_idx];
3768 +                       kmapped_virt_ptr = kmap(page);
3769 +
3770 +                       prev_idx = page_idx;
3771 +               }
3772 +
3773 +               if (vchiq_arm_log_level >= VCHIQ_LOG_TRACE)
3774 +                       vchiq_log_dump_mem("ph",
3775 +                               (uint32_t)(unsigned long)&kmapped_virt_ptr[
3776 +                                       page_offset],
3777 +                               &kmapped_virt_ptr[page_offset], 16);
3778 +
3779 +               offset += 16;
3780 +       }
3781 +       if (page != NULL)
3782 +               kunmap(page);
3783 +
3784 +       for (page_idx = 0; page_idx < num_pages; page_idx++)
3785 +               page_cache_release(pages[page_idx]);
3786 +
3787 +       kfree(pages);
3788 +}
3789 +
3790 +/****************************************************************************
3791 +*
3792 +*   vchiq_read
3793 +*
3794 +***************************************************************************/
3795 +
3796 +static ssize_t
3797 +vchiq_read(struct file *file, char __user *buf,
3798 +       size_t count, loff_t *ppos)
3799 +{
3800 +       DUMP_CONTEXT_T context;
3801 +       context.buf = buf;
3802 +       context.actual = 0;
3803 +       context.space = count;
3804 +       context.offset = *ppos;
3805 +
3806 +       vchiq_dump_state(&context, &g_state);
3807 +
3808 +       *ppos += context.actual;
3809 +
3810 +       return context.actual;
3811 +}
3812 +
3813 +VCHIQ_STATE_T *
3814 +vchiq_get_state(void)
3815 +{
3816 +
3817 +       if (g_state.remote == NULL)
3818 +               printk(KERN_ERR "%s: g_state.remote == NULL\n", __func__);
3819 +       else if (g_state.remote->initialised != 1)
3820 +               printk(KERN_NOTICE "%s: g_state.remote->initialised != 1 (%d)\n",
3821 +                       __func__, g_state.remote->initialised);
3822 +
3823 +       return ((g_state.remote != NULL) &&
3824 +               (g_state.remote->initialised == 1)) ? &g_state : NULL;
3825 +}
3826 +
3827 +static const struct file_operations
3828 +vchiq_fops = {
3829 +       .owner = THIS_MODULE,
3830 +       .unlocked_ioctl = vchiq_ioctl,
3831 +       .open = vchiq_open,
3832 +       .release = vchiq_release,
3833 +       .read = vchiq_read
3834 +};
3835 +
3836 +/*
3837 + * Autosuspend related functionality
3838 + */
3839 +
3840 +int
3841 +vchiq_videocore_wanted(VCHIQ_STATE_T *state)
3842 +{
3843 +       VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
3844 +       if (!arm_state)
3845 +               /* autosuspend not supported - always return wanted */
3846 +               return 1;
3847 +       else if (arm_state->blocked_count)
3848 +               return 1;
3849 +       else if (!arm_state->videocore_use_count)
3850 +               /* usage count zero - check for override unless we're forcing */
3851 +               if (arm_state->resume_blocked)
3852 +                       return 0;
3853 +               else
3854 +                       return vchiq_platform_videocore_wanted(state);
3855 +       else
3856 +               /* non-zero usage count - videocore still required */
3857 +               return 1;
3858 +}
3859 +
3860 +static VCHIQ_STATUS_T
3861 +vchiq_keepalive_vchiq_callback(VCHIQ_REASON_T reason,
3862 +       VCHIQ_HEADER_T *header,
3863 +       VCHIQ_SERVICE_HANDLE_T service_user,
3864 +       void *bulk_user)
3865 +{
3866 +       vchiq_log_error(vchiq_susp_log_level,
3867 +               "%s callback reason %d", __func__, reason);
3868 +       return 0;
3869 +}
3870 +
3871 +static int
3872 +vchiq_keepalive_thread_func(void *v)
3873 +{
3874 +       VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
3875 +       VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
3876 +
3877 +       VCHIQ_STATUS_T status;
3878 +       VCHIQ_INSTANCE_T instance;
3879 +       VCHIQ_SERVICE_HANDLE_T ka_handle;
3880 +
3881 +       VCHIQ_SERVICE_PARAMS_T params = {
3882 +               .fourcc      = VCHIQ_MAKE_FOURCC('K', 'E', 'E', 'P'),
3883 +               .callback    = vchiq_keepalive_vchiq_callback,
3884 +               .version     = KEEPALIVE_VER,
3885 +               .version_min = KEEPALIVE_VER_MIN
3886 +       };
3887 +
3888 +       status = vchiq_initialise(&instance);
3889 +       if (status != VCHIQ_SUCCESS) {
3890 +               vchiq_log_error(vchiq_susp_log_level,
3891 +                       "%s vchiq_initialise failed %d", __func__, status);
3892 +               goto exit;
3893 +       }
3894 +
3895 +       status = vchiq_connect(instance);
3896 +       if (status != VCHIQ_SUCCESS) {
3897 +               vchiq_log_error(vchiq_susp_log_level,
3898 +                       "%s vchiq_connect failed %d", __func__, status);
3899 +               goto shutdown;
3900 +       }
3901 +
3902 +       status = vchiq_add_service(instance, &params, &ka_handle);
3903 +       if (status != VCHIQ_SUCCESS) {
3904 +               vchiq_log_error(vchiq_susp_log_level,
3905 +                       "%s vchiq_open_service failed %d", __func__, status);
3906 +               goto shutdown;
3907 +       }
3908 +
3909 +       while (1) {
3910 +               long rc = 0, uc = 0;
3911 +               if (wait_for_completion_interruptible(&arm_state->ka_evt)
3912 +                               != 0) {
3913 +                       vchiq_log_error(vchiq_susp_log_level,
3914 +                               "%s interrupted", __func__);
3915 +                       flush_signals(current);
3916 +                       continue;
3917 +               }
3918 +
3919 +               /* read and clear counters.  Do release_count then use_count to
3920 +                * prevent getting more releases than uses */
3921 +               rc = atomic_xchg(&arm_state->ka_release_count, 0);
3922 +               uc = atomic_xchg(&arm_state->ka_use_count, 0);
3923 +
3924 +               /* Call use/release service the requisite number of times.
3925 +                * Process use before release so use counts don't go negative */
3926 +               while (uc--) {
3927 +                       atomic_inc(&arm_state->ka_use_ack_count);
3928 +                       status = vchiq_use_service(ka_handle);
3929 +                       if (status != VCHIQ_SUCCESS) {
3930 +                               vchiq_log_error(vchiq_susp_log_level,
3931 +                                       "%s vchiq_use_service error %d",
3932 +                                       __func__, status);
3933 +                       }
3934 +               }
3935 +               while (rc--) {
3936 +                       status = vchiq_release_service(ka_handle);
3937 +                       if (status != VCHIQ_SUCCESS) {
3938 +                               vchiq_log_error(vchiq_susp_log_level,
3939 +                                       "%s vchiq_release_service error %d",
3940 +                                       __func__, status);
3941 +                       }
3942 +               }
3943 +       }
3944 +
3945 +shutdown:
3946 +       vchiq_shutdown(instance);
3947 +exit:
3948 +       return 0;
3949 +}
3950 +
3951 +
3952 +
3953 +VCHIQ_STATUS_T
3954 +vchiq_arm_init_state(VCHIQ_STATE_T *state, VCHIQ_ARM_STATE_T *arm_state)
3955 +{
3956 +       VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
3957 +
3958 +       if (arm_state) {
3959 +               rwlock_init(&arm_state->susp_res_lock);
3960 +
3961 +               init_completion(&arm_state->ka_evt);
3962 +               atomic_set(&arm_state->ka_use_count, 0);
3963 +               atomic_set(&arm_state->ka_use_ack_count, 0);
3964 +               atomic_set(&arm_state->ka_release_count, 0);
3965 +
3966 +               init_completion(&arm_state->vc_suspend_complete);
3967 +
3968 +               init_completion(&arm_state->vc_resume_complete);
3969 +               /* Initialise to 'done' state.  We only want to block on resume
3970 +                * completion while videocore is suspended. */
3971 +               set_resume_state(arm_state, VC_RESUME_RESUMED);
3972 +
3973 +               init_completion(&arm_state->resume_blocker);
3974 +               /* Initialise to 'done' state.  We only want to block on this
3975 +                * completion while resume is blocked */
3976 +               complete_all(&arm_state->resume_blocker);
3977 +
3978 +               init_completion(&arm_state->blocked_blocker);
3979 +               /* Initialise to 'done' state.  We only want to block on this
3980 +                * completion while things are waiting on the resume blocker */
3981 +               complete_all(&arm_state->blocked_blocker);
3982 +
3983 +               arm_state->suspend_timer_timeout = SUSPEND_TIMER_TIMEOUT_MS;
3984 +               arm_state->suspend_timer_running = 0;
3985 +               init_timer(&arm_state->suspend_timer);
3986 +               arm_state->suspend_timer.data = (unsigned long)(state);
3987 +               arm_state->suspend_timer.function = suspend_timer_callback;
3988 +
3989 +               arm_state->first_connect = 0;
3990 +
3991 +       }
3992 +       return status;
3993 +}
3994 +
3995 +/*
3996 +** Functions to modify the state variables;
3997 +**     set_suspend_state
3998 +**     set_resume_state
3999 +**
4000 +** There are more state variables than we might like, so ensure they remain in
4001 +** step.  Suspend and resume state are maintained separately, since most of
4002 +** these state machines can operate independently.  However, there are a few
4003 +** states where state transitions in one state machine cause a reset to the
4004 +** other state machine.  In addition, there are some completion events which
4005 +** need to occur on state machine reset and end-state(s), so these are also
4006 +** dealt with in these functions.
4007 +**
4008 +** In all states we set the state variable according to the input, but in some
4009 +** cases we perform additional steps outlined below;
4010 +**
4011 +** VC_SUSPEND_IDLE - Initialise the suspend completion at the same time.
4012 +**                     The suspend completion is completed after any suspend
4013 +**                     attempt.  When we reset the state machine we also reset
4014 +**                     the completion.  This reset occurs when videocore is
4015 +**                     resumed, and also if we initiate suspend after a suspend
4016 +**                     failure.
4017 +**
4018 +** VC_SUSPEND_IN_PROGRESS - This state is considered the point of no return for
4019 +**                     suspend - ie from this point on we must try to suspend
4020 +**                     before resuming can occur.  We therefore also reset the
4021 +**                     resume state machine to VC_RESUME_IDLE in this state.
4022 +**
4023 +** VC_SUSPEND_SUSPENDED - Suspend has completed successfully. Also call
4024 +**                     complete_all on the suspend completion to notify
4025 +**                     anything waiting for suspend to happen.
4026 +**
4027 +** VC_SUSPEND_REJECTED - Videocore rejected suspend. Videocore will also
4028 +**                     initiate resume, so no need to alter resume state.
4029 +**                     We call complete_all on the suspend completion to notify
4030 +**                     of suspend rejection.
4031 +**
4032 +** VC_SUSPEND_FAILED - We failed to initiate videocore suspend.  We notify the
4033 +**                     suspend completion and reset the resume state machine.
4034 +**
4035 +** VC_RESUME_IDLE - Initialise the resume completion at the same time.  The
4036 +**                     resume completion is in it's 'done' state whenever
4037 +**                     videcore is running.  Therfore, the VC_RESUME_IDLE state
4038 +**                     implies that videocore is suspended.
4039 +**                     Hence, any thread which needs to wait until videocore is
4040 +**                     running can wait on this completion - it will only block
4041 +**                     if videocore is suspended.
4042 +**
4043 +** VC_RESUME_RESUMED - Resume has completed successfully.  Videocore is running.
4044 +**                     Call complete_all on the resume completion to unblock
4045 +**                     any threads waiting for resume.  Also reset the suspend
4046 +**                     state machine to it's idle state.
4047 +**
4048 +** VC_RESUME_FAILED - Currently unused - no mechanism to fail resume exists.
4049 +*/
4050 +
4051 +inline void
4052 +set_suspend_state(VCHIQ_ARM_STATE_T *arm_state,
4053 +       enum vc_suspend_status new_state)
4054 +{
4055 +       /* set the state in all cases */
4056 +       arm_state->vc_suspend_state = new_state;
4057 +
4058 +       /* state specific additional actions */
4059 +       switch (new_state) {
4060 +       case VC_SUSPEND_FORCE_CANCELED:
4061 +               complete_all(&arm_state->vc_suspend_complete);
4062 +               break;
4063 +       case VC_SUSPEND_REJECTED:
4064 +               complete_all(&arm_state->vc_suspend_complete);
4065 +               break;
4066 +       case VC_SUSPEND_FAILED:
4067 +               complete_all(&arm_state->vc_suspend_complete);
4068 +               arm_state->vc_resume_state = VC_RESUME_RESUMED;
4069 +               complete_all(&arm_state->vc_resume_complete);
4070 +               break;
4071 +       case VC_SUSPEND_IDLE:
4072 +               reinit_completion(&arm_state->vc_suspend_complete);
4073 +               break;
4074 +       case VC_SUSPEND_REQUESTED:
4075 +               break;
4076 +       case VC_SUSPEND_IN_PROGRESS:
4077 +               set_resume_state(arm_state, VC_RESUME_IDLE);
4078 +               break;
4079 +       case VC_SUSPEND_SUSPENDED:
4080 +               complete_all(&arm_state->vc_suspend_complete);
4081 +               break;
4082 +       default:
4083 +               BUG();
4084 +               break;
4085 +       }
4086 +}
4087 +
4088 +inline void
4089 +set_resume_state(VCHIQ_ARM_STATE_T *arm_state,
4090 +       enum vc_resume_status new_state)
4091 +{
4092 +       /* set the state in all cases */
4093 +       arm_state->vc_resume_state = new_state;
4094 +
4095 +       /* state specific additional actions */
4096 +       switch (new_state) {
4097 +       case VC_RESUME_FAILED:
4098 +               break;
4099 +       case VC_RESUME_IDLE:
4100 +               reinit_completion(&arm_state->vc_resume_complete);
4101 +               break;
4102 +       case VC_RESUME_REQUESTED:
4103 +               break;
4104 +       case VC_RESUME_IN_PROGRESS:
4105 +               break;
4106 +       case VC_RESUME_RESUMED:
4107 +               complete_all(&arm_state->vc_resume_complete);
4108 +               set_suspend_state(arm_state, VC_SUSPEND_IDLE);
4109 +               break;
4110 +       default:
4111 +               BUG();
4112 +               break;
4113 +       }
4114 +}
4115 +
4116 +
4117 +/* should be called with the write lock held */
4118 +inline void
4119 +start_suspend_timer(VCHIQ_ARM_STATE_T *arm_state)
4120 +{
4121 +       del_timer(&arm_state->suspend_timer);
4122 +       arm_state->suspend_timer.expires = jiffies +
4123 +               msecs_to_jiffies(arm_state->
4124 +                       suspend_timer_timeout);
4125 +       add_timer(&arm_state->suspend_timer);
4126 +       arm_state->suspend_timer_running = 1;
4127 +}
4128 +
4129 +/* should be called with the write lock held */
4130 +static inline void
4131 +stop_suspend_timer(VCHIQ_ARM_STATE_T *arm_state)
4132 +{
4133 +       if (arm_state->suspend_timer_running) {
4134 +               del_timer(&arm_state->suspend_timer);
4135 +               arm_state->suspend_timer_running = 0;
4136 +       }
4137 +}
4138 +
4139 +static inline int
4140 +need_resume(VCHIQ_STATE_T *state)
4141 +{
4142 +       VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
4143 +       return (arm_state->vc_suspend_state > VC_SUSPEND_IDLE) &&
4144 +                       (arm_state->vc_resume_state < VC_RESUME_REQUESTED) &&
4145 +                       vchiq_videocore_wanted(state);
4146 +}
4147 +
4148 +static int
4149 +block_resume(VCHIQ_ARM_STATE_T *arm_state)
4150 +{
4151 +       int status = VCHIQ_SUCCESS;
4152 +       const unsigned long timeout_val =
4153 +                               msecs_to_jiffies(FORCE_SUSPEND_TIMEOUT_MS);
4154 +       int resume_count = 0;
4155 +
4156 +       /* Allow any threads which were blocked by the last force suspend to
4157 +        * complete if they haven't already.  Only give this one shot; if
4158 +        * blocked_count is incremented after blocked_blocker is completed
4159 +        * (which only happens when blocked_count hits 0) then those threads
4160 +        * will have to wait until next time around */
4161 +       if (arm_state->blocked_count) {
4162 +               reinit_completion(&arm_state->blocked_blocker);
4163 +               write_unlock_bh(&arm_state->susp_res_lock);
4164 +               vchiq_log_info(vchiq_susp_log_level, "%s wait for previously "
4165 +                       "blocked clients", __func__);
4166 +               if (wait_for_completion_interruptible_timeout(
4167 +                               &arm_state->blocked_blocker, timeout_val)
4168 +                                       <= 0) {
4169 +                       vchiq_log_error(vchiq_susp_log_level, "%s wait for "
4170 +                               "previously blocked clients failed" , __func__);
4171 +                       status = VCHIQ_ERROR;
4172 +                       write_lock_bh(&arm_state->susp_res_lock);
4173 +                       goto out;
4174 +               }
4175 +               vchiq_log_info(vchiq_susp_log_level, "%s previously blocked "
4176 +                       "clients resumed", __func__);
4177 +               write_lock_bh(&arm_state->susp_res_lock);
4178 +       }
4179 +
4180 +       /* We need to wait for resume to complete if it's in process */
4181 +       while (arm_state->vc_resume_state != VC_RESUME_RESUMED &&
4182 +                       arm_state->vc_resume_state > VC_RESUME_IDLE) {
4183 +               if (resume_count > 1) {
4184 +                       status = VCHIQ_ERROR;
4185 +                       vchiq_log_error(vchiq_susp_log_level, "%s waited too "
4186 +                               "many times for resume" , __func__);
4187 +                       goto out;
4188 +               }
4189 +               write_unlock_bh(&arm_state->susp_res_lock);
4190 +               vchiq_log_info(vchiq_susp_log_level, "%s wait for resume",
4191 +                       __func__);
4192 +               if (wait_for_completion_interruptible_timeout(
4193 +                               &arm_state->vc_resume_complete, timeout_val)
4194 +                                       <= 0) {
4195 +                       vchiq_log_error(vchiq_susp_log_level, "%s wait for "
4196 +                               "resume failed (%s)", __func__,
4197 +                               resume_state_names[arm_state->vc_resume_state +
4198 +                                                       VC_RESUME_NUM_OFFSET]);
4199 +                       status = VCHIQ_ERROR;
4200 +                       write_lock_bh(&arm_state->susp_res_lock);
4201 +                       goto out;
4202 +               }
4203 +               vchiq_log_info(vchiq_susp_log_level, "%s resumed", __func__);
4204 +               write_lock_bh(&arm_state->susp_res_lock);
4205 +               resume_count++;
4206 +       }
4207 +       reinit_completion(&arm_state->resume_blocker);
4208 +       arm_state->resume_blocked = 1;
4209 +
4210 +out:
4211 +       return status;
4212 +}
4213 +
4214 +static inline void
4215 +unblock_resume(VCHIQ_ARM_STATE_T *arm_state)
4216 +{
4217 +       complete_all(&arm_state->resume_blocker);
4218 +       arm_state->resume_blocked = 0;
4219 +}
4220 +
4221 +/* Initiate suspend via slot handler. Should be called with the write lock
4222 + * held */
4223 +VCHIQ_STATUS_T
4224 +vchiq_arm_vcsuspend(VCHIQ_STATE_T *state)
4225 +{
4226 +       VCHIQ_STATUS_T status = VCHIQ_ERROR;
4227 +       VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
4228 +
4229 +       if (!arm_state)
4230 +               goto out;
4231 +
4232 +       vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
4233 +       status = VCHIQ_SUCCESS;
4234 +
4235 +
4236 +       switch (arm_state->vc_suspend_state) {
4237 +       case VC_SUSPEND_REQUESTED:
4238 +               vchiq_log_info(vchiq_susp_log_level, "%s: suspend already "
4239 +                       "requested", __func__);
4240 +               break;
4241 +       case VC_SUSPEND_IN_PROGRESS:
4242 +               vchiq_log_info(vchiq_susp_log_level, "%s: suspend already in "
4243 +                       "progress", __func__);
4244 +               break;
4245 +
4246 +       default:
4247 +               /* We don't expect to be in other states, so log but continue
4248 +                * anyway */
4249 +               vchiq_log_error(vchiq_susp_log_level,
4250 +                       "%s unexpected suspend state %s", __func__,
4251 +                       suspend_state_names[arm_state->vc_suspend_state +
4252 +                                               VC_SUSPEND_NUM_OFFSET]);
4253 +               /* fall through */
4254 +       case VC_SUSPEND_REJECTED:
4255 +       case VC_SUSPEND_FAILED:
4256 +               /* Ensure any idle state actions have been run */
4257 +               set_suspend_state(arm_state, VC_SUSPEND_IDLE);
4258 +               /* fall through */
4259 +       case VC_SUSPEND_IDLE:
4260 +               vchiq_log_info(vchiq_susp_log_level,
4261 +                       "%s: suspending", __func__);
4262 +               set_suspend_state(arm_state, VC_SUSPEND_REQUESTED);
4263 +               /* kick the slot handler thread to initiate suspend */
4264 +               request_poll(state, NULL, 0);
4265 +               break;
4266 +       }
4267 +
4268 +out:
4269 +       vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, status);
4270 +       return status;
4271 +}
4272 +
4273 +void
4274 +vchiq_platform_check_suspend(VCHIQ_STATE_T *state)
4275 +{
4276 +       VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
4277 +       int susp = 0;
4278 +
4279 +       if (!arm_state)
4280 +               goto out;
4281 +
4282 +       vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
4283 +
4284 +       write_lock_bh(&arm_state->susp_res_lock);
4285 +       if (arm_state->vc_suspend_state == VC_SUSPEND_REQUESTED &&
4286 +                       arm_state->vc_resume_state == VC_RESUME_RESUMED) {
4287 +               set_suspend_state(arm_state, VC_SUSPEND_IN_PROGRESS);
4288 +               susp = 1;
4289 +       }
4290 +       write_unlock_bh(&arm_state->susp_res_lock);
4291 +
4292 +       if (susp)
4293 +               vchiq_platform_suspend(state);
4294 +
4295 +out:
4296 +       vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
4297 +       return;
4298 +}
4299 +
4300 +
4301 +static void
4302 +output_timeout_error(VCHIQ_STATE_T *state)
4303 +{
4304 +       VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
4305 +       char service_err[50] = "";
4306 +       int vc_use_count = arm_state->videocore_use_count;
4307 +       int active_services = state->unused_service;
4308 +       int i;
4309 +
4310 +       if (!arm_state->videocore_use_count) {
4311 +               snprintf(service_err, 50, " Videocore usecount is 0");
4312 +               goto output_msg;
4313 +       }
4314 +       for (i = 0; i < active_services; i++) {
4315 +               VCHIQ_SERVICE_T *service_ptr = state->services[i];
4316 +               if (service_ptr && service_ptr->service_use_count &&
4317 +                       (service_ptr->srvstate != VCHIQ_SRVSTATE_FREE)) {
4318 +                       snprintf(service_err, 50, " %c%c%c%c(%d) service has "
4319 +                               "use count %d%s", VCHIQ_FOURCC_AS_4CHARS(
4320 +                                       service_ptr->base.fourcc),
4321 +                                service_ptr->client_id,
4322 +                                service_ptr->service_use_count,
4323 +                                service_ptr->service_use_count ==
4324 +                                        vc_use_count ? "" : " (+ more)");
4325 +                       break;
4326 +               }
4327 +       }
4328 +
4329 +output_msg:
4330 +       vchiq_log_error(vchiq_susp_log_level,
4331 +               "timed out waiting for vc suspend (%d).%s",
4332 +                arm_state->autosuspend_override, service_err);
4333 +
4334 +}
4335 +
4336 +/* Try to get videocore into suspended state, regardless of autosuspend state.
4337 +** We don't actually force suspend, since videocore may get into a bad state
4338 +** if we force suspend at a bad time.  Instead, we wait for autosuspend to
4339 +** determine a good point to suspend.  If this doesn't happen within 100ms we
4340 +** report failure.
4341 +**
4342 +** Returns VCHIQ_SUCCESS if videocore suspended successfully, VCHIQ_RETRY if
4343 +** videocore failed to suspend in time or VCHIQ_ERROR if interrupted.
4344 +*/
4345 +VCHIQ_STATUS_T
4346 +vchiq_arm_force_suspend(VCHIQ_STATE_T *state)
4347 +{
4348 +       VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
4349 +       VCHIQ_STATUS_T status = VCHIQ_ERROR;
4350 +       long rc = 0;
4351 +       int repeat = -1;
4352 +
4353 +       if (!arm_state)
4354 +               goto out;
4355 +
4356 +       vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
4357 +
4358 +       write_lock_bh(&arm_state->susp_res_lock);
4359 +
4360 +       status = block_resume(arm_state);
4361 +       if (status != VCHIQ_SUCCESS)
4362 +               goto unlock;
4363 +       if (arm_state->vc_suspend_state == VC_SUSPEND_SUSPENDED) {
4364 +               /* Already suspended - just block resume and exit */
4365 +               vchiq_log_info(vchiq_susp_log_level, "%s already suspended",
4366 +                       __func__);
4367 +               status = VCHIQ_SUCCESS;
4368 +               goto unlock;
4369 +       } else if (arm_state->vc_suspend_state <= VC_SUSPEND_IDLE) {
4370 +               /* initiate suspend immediately in the case that we're waiting
4371 +                * for the timeout */
4372 +               stop_suspend_timer(arm_state);
4373 +               if (!vchiq_videocore_wanted(state)) {
4374 +                       vchiq_log_info(vchiq_susp_log_level, "%s videocore "
4375 +                               "idle, initiating suspend", __func__);
4376 +                       status = vchiq_arm_vcsuspend(state);
4377 +               } else if (arm_state->autosuspend_override <
4378 +                                               FORCE_SUSPEND_FAIL_MAX) {
4379 +                       vchiq_log_info(vchiq_susp_log_level, "%s letting "
4380 +                               "videocore go idle", __func__);
4381 +                       status = VCHIQ_SUCCESS;
4382 +               } else {
4383 +                       vchiq_log_warning(vchiq_susp_log_level, "%s failed too "
4384 +                               "many times - attempting suspend", __func__);
4385 +                       status = vchiq_arm_vcsuspend(state);
4386 +               }
4387 +       } else {
4388 +               vchiq_log_info(vchiq_susp_log_level, "%s videocore suspend "
4389 +                       "in progress - wait for completion", __func__);
4390 +               status = VCHIQ_SUCCESS;
4391 +       }
4392 +
4393 +       /* Wait for suspend to happen due to system idle (not forced..) */
4394 +       if (status != VCHIQ_SUCCESS)
4395 +               goto unblock_resume;
4396 +
4397 +       do {
4398 +               write_unlock_bh(&arm_state->susp_res_lock);
4399 +
4400 +               rc = wait_for_completion_interruptible_timeout(
4401 +                               &arm_state->vc_suspend_complete,
4402 +                               msecs_to_jiffies(FORCE_SUSPEND_TIMEOUT_MS));
4403 +
4404 +               write_lock_bh(&arm_state->susp_res_lock);
4405 +               if (rc < 0) {
4406 +                       vchiq_log_warning(vchiq_susp_log_level, "%s "
4407 +                               "interrupted waiting for suspend", __func__);
4408 +                       status = VCHIQ_ERROR;
4409 +                       goto unblock_resume;
4410 +               } else if (rc == 0) {
4411 +                       if (arm_state->vc_suspend_state > VC_SUSPEND_IDLE) {
4412 +                               /* Repeat timeout once if in progress */
4413 +                               if (repeat < 0) {
4414 +                                       repeat = 1;
4415 +                                       continue;
4416 +                               }
4417 +                       }
4418 +                       arm_state->autosuspend_override++;
4419 +                       output_timeout_error(state);
4420 +
4421 +                       status = VCHIQ_RETRY;
4422 +                       goto unblock_resume;
4423 +               }
4424 +       } while (0 < (repeat--));
4425 +
4426 +       /* Check and report state in case we need to abort ARM suspend */
4427 +       if (arm_state->vc_suspend_state != VC_SUSPEND_SUSPENDED) {
4428 +               status = VCHIQ_RETRY;
4429 +               vchiq_log_error(vchiq_susp_log_level,
4430 +                       "%s videocore suspend failed (state %s)", __func__,
4431 +                       suspend_state_names[arm_state->vc_suspend_state +
4432 +                                               VC_SUSPEND_NUM_OFFSET]);
4433 +               /* Reset the state only if it's still in an error state.
4434 +                * Something could have already initiated another suspend. */
4435 +               if (arm_state->vc_suspend_state < VC_SUSPEND_IDLE)
4436 +                       set_suspend_state(arm_state, VC_SUSPEND_IDLE);
4437 +
4438 +               goto unblock_resume;
4439 +       }
4440 +
4441 +       /* successfully suspended - unlock and exit */
4442 +       goto unlock;
4443 +
4444 +unblock_resume:
4445 +       /* all error states need to unblock resume before exit */
4446 +       unblock_resume(arm_state);
4447 +
4448 +unlock:
4449 +       write_unlock_bh(&arm_state->susp_res_lock);
4450 +
4451 +out:
4452 +       vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, status);
4453 +       return status;
4454 +}
4455 +
4456 +void
4457 +vchiq_check_suspend(VCHIQ_STATE_T *state)
4458 +{
4459 +       VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
4460 +
4461 +       if (!arm_state)
4462 +               goto out;
4463 +
4464 +       vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
4465 +
4466 +       write_lock_bh(&arm_state->susp_res_lock);
4467 +       if (arm_state->vc_suspend_state != VC_SUSPEND_SUSPENDED &&
4468 +                       arm_state->first_connect &&
4469 +                       !vchiq_videocore_wanted(state)) {
4470 +               vchiq_arm_vcsuspend(state);
4471 +       }
4472 +       write_unlock_bh(&arm_state->susp_res_lock);
4473 +
4474 +out:
4475 +       vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
4476 +       return;
4477 +}
4478 +
4479 +
4480 +int
4481 +vchiq_arm_allow_resume(VCHIQ_STATE_T *state)
4482 +{
4483 +       VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
4484 +       int resume = 0;
4485 +       int ret = -1;
4486 +
4487 +       if (!arm_state)
4488 +               goto out;
4489 +
4490 +       vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
4491 +
4492 +       write_lock_bh(&arm_state->susp_res_lock);
4493 +       unblock_resume(arm_state);
4494 +       resume = vchiq_check_resume(state);
4495 +       write_unlock_bh(&arm_state->susp_res_lock);
4496 +
4497 +       if (resume) {
4498 +               if (wait_for_completion_interruptible(
4499 +                       &arm_state->vc_resume_complete) < 0) {
4500 +                       vchiq_log_error(vchiq_susp_log_level,
4501 +                               "%s interrupted", __func__);
4502 +                       /* failed, cannot accurately derive suspend
4503 +                        * state, so exit early. */
4504 +                       goto out;
4505 +               }
4506 +       }
4507 +
4508 +       read_lock_bh(&arm_state->susp_res_lock);
4509 +       if (arm_state->vc_suspend_state == VC_SUSPEND_SUSPENDED) {
4510 +               vchiq_log_info(vchiq_susp_log_level,
4511 +                               "%s: Videocore remains suspended", __func__);
4512 +       } else {
4513 +               vchiq_log_info(vchiq_susp_log_level,
4514 +                               "%s: Videocore resumed", __func__);
4515 +               ret = 0;
4516 +       }
4517 +       read_unlock_bh(&arm_state->susp_res_lock);
4518 +out:
4519 +       vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, ret);
4520 +       return ret;
4521 +}
4522 +
4523 +/* This function should be called with the write lock held */
4524 +int
4525 +vchiq_check_resume(VCHIQ_STATE_T *state)
4526 +{
4527 +       VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
4528 +       int resume = 0;
4529 +
4530 +       if (!arm_state)
4531 +               goto out;
4532 +
4533 +       vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
4534 +
4535 +       if (need_resume(state)) {
4536 +               set_resume_state(arm_state, VC_RESUME_REQUESTED);
4537 +               request_poll(state, NULL, 0);
4538 +               resume = 1;
4539 +       }
4540 +
4541 +out:
4542 +       vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
4543 +       return resume;
4544 +}
4545 +
4546 +void
4547 +vchiq_platform_check_resume(VCHIQ_STATE_T *state)
4548 +{
4549 +       VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
4550 +       int res = 0;
4551 +
4552 +       if (!arm_state)
4553 +               goto out;
4554 +
4555 +       vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
4556 +
4557 +       write_lock_bh(&arm_state->susp_res_lock);
4558 +       if (arm_state->wake_address == 0) {
4559 +               vchiq_log_info(vchiq_susp_log_level,
4560 +                                       "%s: already awake", __func__);
4561 +               goto unlock;
4562 +       }
4563 +       if (arm_state->vc_resume_state == VC_RESUME_IN_PROGRESS) {
4564 +               vchiq_log_info(vchiq_susp_log_level,
4565 +                                       "%s: already resuming", __func__);
4566 +               goto unlock;
4567 +       }
4568 +
4569 +       if (arm_state->vc_resume_state == VC_RESUME_REQUESTED) {
4570 +               set_resume_state(arm_state, VC_RESUME_IN_PROGRESS);
4571 +               res = 1;
4572 +       } else
4573 +               vchiq_log_trace(vchiq_susp_log_level,
4574 +                               "%s: not resuming (resume state %s)", __func__,
4575 +                               resume_state_names[arm_state->vc_resume_state +
4576 +                                                       VC_RESUME_NUM_OFFSET]);
4577 +
4578 +unlock:
4579 +       write_unlock_bh(&arm_state->susp_res_lock);
4580 +
4581 +       if (res)
4582 +               vchiq_platform_resume(state);
4583 +
4584 +out:
4585 +       vchiq_log_trace(vchiq_susp_log_level, "%s exit", __func__);
4586 +       return;
4587 +
4588 +}
4589 +
4590 +
4591 +
4592 +VCHIQ_STATUS_T
4593 +vchiq_use_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service,
4594 +               enum USE_TYPE_E use_type)
4595 +{
4596 +       VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
4597 +       VCHIQ_STATUS_T ret = VCHIQ_SUCCESS;
4598 +       char entity[16];
4599 +       int *entity_uc;
4600 +       int local_uc, local_entity_uc;
4601 +
4602 +       if (!arm_state)
4603 +               goto out;
4604 +
4605 +       vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
4606 +
4607 +       if (use_type == USE_TYPE_VCHIQ) {
4608 +               sprintf(entity, "VCHIQ:   ");
4609 +               entity_uc = &arm_state->peer_use_count;
4610 +       } else if (service) {
4611 +               sprintf(entity, "%c%c%c%c:%03d",
4612 +                       VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
4613 +                       service->client_id);
4614 +               entity_uc = &service->service_use_count;
4615 +       } else {
4616 +               vchiq_log_error(vchiq_susp_log_level, "%s null service "
4617 +                               "ptr", __func__);
4618 +               ret = VCHIQ_ERROR;
4619 +               goto out;
4620 +       }
4621 +
4622 +       write_lock_bh(&arm_state->susp_res_lock);
4623 +       while (arm_state->resume_blocked) {
4624 +               /* If we call 'use' while force suspend is waiting for suspend,
4625 +                * then we're about to block the thread which the force is
4626 +                * waiting to complete, so we're bound to just time out. In this
4627 +                * case, set the suspend state such that the wait will be
4628 +                * canceled, so we can complete as quickly as possible. */
4629 +               if (arm_state->resume_blocked && arm_state->vc_suspend_state ==
4630 +                               VC_SUSPEND_IDLE) {
4631 +                       set_suspend_state(arm_state, VC_SUSPEND_FORCE_CANCELED);
4632 +                       break;
4633 +               }
4634 +               /* If suspend is already in progress then we need to block */
4635 +               if (!try_wait_for_completion(&arm_state->resume_blocker)) {
4636 +                       /* Indicate that there are threads waiting on the resume
4637 +                        * blocker.  These need to be allowed to complete before
4638 +                        * a _second_ call to force suspend can complete,
4639 +                        * otherwise low priority threads might never actually
4640 +                        * continue */
4641 +                       arm_state->blocked_count++;
4642 +                       write_unlock_bh(&arm_state->susp_res_lock);
4643 +                       vchiq_log_info(vchiq_susp_log_level, "%s %s resume "
4644 +                               "blocked - waiting...", __func__, entity);
4645 +                       if (wait_for_completion_killable(
4646 +                                       &arm_state->resume_blocker) != 0) {
4647 +                               vchiq_log_error(vchiq_susp_log_level, "%s %s "
4648 +                                       "wait for resume blocker interrupted",
4649 +                                       __func__, entity);
4650 +                               ret = VCHIQ_ERROR;
4651 +                               write_lock_bh(&arm_state->susp_res_lock);
4652 +                               arm_state->blocked_count--;
4653 +                               write_unlock_bh(&arm_state->susp_res_lock);
4654 +                               goto out;
4655 +                       }
4656 +                       vchiq_log_info(vchiq_susp_log_level, "%s %s resume "
4657 +                               "unblocked", __func__, entity);
4658 +                       write_lock_bh(&arm_state->susp_res_lock);
4659 +                       if (--arm_state->blocked_count == 0)
4660 +                               complete_all(&arm_state->blocked_blocker);
4661 +               }
4662 +       }
4663 +
4664 +       stop_suspend_timer(arm_state);
4665 +
4666 +       local_uc = ++arm_state->videocore_use_count;
4667 +       local_entity_uc = ++(*entity_uc);
4668 +
4669 +       /* If there's a pending request which hasn't yet been serviced then
4670 +        * just clear it.  If we're past VC_SUSPEND_REQUESTED state then
4671 +        * vc_resume_complete will block until we either resume or fail to
4672 +        * suspend */
4673 +       if (arm_state->vc_suspend_state <= VC_SUSPEND_REQUESTED)
4674 +               set_suspend_state(arm_state, VC_SUSPEND_IDLE);
4675 +
4676 +       if ((use_type != USE_TYPE_SERVICE_NO_RESUME) && need_resume(state)) {
4677 +               set_resume_state(arm_state, VC_RESUME_REQUESTED);
4678 +               vchiq_log_info(vchiq_susp_log_level,
4679 +                       "%s %s count %d, state count %d",
4680 +                       __func__, entity, local_entity_uc, local_uc);
4681 +               request_poll(state, NULL, 0);
4682 +       } else
4683 +               vchiq_log_trace(vchiq_susp_log_level,
4684 +                       "%s %s count %d, state count %d",
4685 +                       __func__, entity, *entity_uc, local_uc);
4686 +
4687 +
4688 +       write_unlock_bh(&arm_state->susp_res_lock);
4689 +
4690 +       /* Completion is in a done state when we're not suspended, so this won't
4691 +        * block for the non-suspended case. */
4692 +       if (!try_wait_for_completion(&arm_state->vc_resume_complete)) {
4693 +               vchiq_log_info(vchiq_susp_log_level, "%s %s wait for resume",
4694 +                       __func__, entity);
4695 +               if (wait_for_completion_killable(
4696 +                               &arm_state->vc_resume_complete) != 0) {
4697 +                       vchiq_log_error(vchiq_susp_log_level, "%s %s wait for "
4698 +                               "resume interrupted", __func__, entity);
4699 +                       ret = VCHIQ_ERROR;
4700 +                       goto out;
4701 +               }
4702 +               vchiq_log_info(vchiq_susp_log_level, "%s %s resumed", __func__,
4703 +                       entity);
4704 +       }
4705 +
4706 +       if (ret == VCHIQ_SUCCESS) {
4707 +               VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
4708 +               long ack_cnt = atomic_xchg(&arm_state->ka_use_ack_count, 0);
4709 +               while (ack_cnt && (status == VCHIQ_SUCCESS)) {
4710 +                       /* Send the use notify to videocore */
4711 +                       status = vchiq_send_remote_use_active(state);
4712 +                       if (status == VCHIQ_SUCCESS)
4713 +                               ack_cnt--;
4714 +                       else
4715 +                               atomic_add(ack_cnt,
4716 +                                       &arm_state->ka_use_ack_count);
4717 +               }
4718 +       }
4719 +
4720 +out:
4721 +       vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, ret);
4722 +       return ret;
4723 +}
4724 +
4725 +VCHIQ_STATUS_T
4726 +vchiq_release_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service)
4727 +{
4728 +       VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
4729 +       VCHIQ_STATUS_T ret = VCHIQ_SUCCESS;
4730 +       char entity[16];
4731 +       int *entity_uc;
4732 +       int local_uc, local_entity_uc;
4733 +
4734 +       if (!arm_state)
4735 +               goto out;
4736 +
4737 +       vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
4738 +
4739 +       if (service) {
4740 +               sprintf(entity, "%c%c%c%c:%03d",
4741 +                       VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
4742 +                       service->client_id);
4743 +               entity_uc = &service->service_use_count;
4744 +       } else {
4745 +               sprintf(entity, "PEER:   ");
4746 +               entity_uc = &arm_state->peer_use_count;
4747 +       }
4748 +
4749 +       write_lock_bh(&arm_state->susp_res_lock);
4750 +       if (!arm_state->videocore_use_count || !(*entity_uc)) {
4751 +               /* Don't use BUG_ON - don't allow user thread to crash kernel */
4752 +               WARN_ON(!arm_state->videocore_use_count);
4753 +               WARN_ON(!(*entity_uc));
4754 +               ret = VCHIQ_ERROR;
4755 +               goto unlock;
4756 +       }
4757 +       local_uc = --arm_state->videocore_use_count;
4758 +       local_entity_uc = --(*entity_uc);
4759 +
4760 +       if (!vchiq_videocore_wanted(state)) {
4761 +               if (vchiq_platform_use_suspend_timer() &&
4762 +                               !arm_state->resume_blocked) {
4763 +                       /* Only use the timer if we're not trying to force
4764 +                        * suspend (=> resume_blocked) */
4765 +                       start_suspend_timer(arm_state);
4766 +               } else {
4767 +                       vchiq_log_info(vchiq_susp_log_level,
4768 +                               "%s %s count %d, state count %d - suspending",
4769 +                               __func__, entity, *entity_uc,
4770 +                               arm_state->videocore_use_count);
4771 +                       vchiq_arm_vcsuspend(state);
4772 +               }
4773 +       } else
4774 +               vchiq_log_trace(vchiq_susp_log_level,
4775 +                       "%s %s count %d, state count %d",
4776 +                       __func__, entity, *entity_uc,
4777 +                       arm_state->videocore_use_count);
4778 +
4779 +unlock:
4780 +       write_unlock_bh(&arm_state->susp_res_lock);
4781 +
4782 +out:
4783 +       vchiq_log_trace(vchiq_susp_log_level, "%s exit %d", __func__, ret);
4784 +       return ret;
4785 +}
4786 +
4787 +void
4788 +vchiq_on_remote_use(VCHIQ_STATE_T *state)
4789 +{
4790 +       VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
4791 +       vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
4792 +       atomic_inc(&arm_state->ka_use_count);
4793 +       complete(&arm_state->ka_evt);
4794 +}
4795 +
4796 +void
4797 +vchiq_on_remote_release(VCHIQ_STATE_T *state)
4798 +{
4799 +       VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
4800 +       vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
4801 +       atomic_inc(&arm_state->ka_release_count);
4802 +       complete(&arm_state->ka_evt);
4803 +}
4804 +
4805 +VCHIQ_STATUS_T
4806 +vchiq_use_service_internal(VCHIQ_SERVICE_T *service)
4807 +{
4808 +       return vchiq_use_internal(service->state, service, USE_TYPE_SERVICE);
4809 +}
4810 +
4811 +VCHIQ_STATUS_T
4812 +vchiq_release_service_internal(VCHIQ_SERVICE_T *service)
4813 +{
4814 +       return vchiq_release_internal(service->state, service);
4815 +}
4816 +
4817 +VCHIQ_DEBUGFS_NODE_T *
4818 +vchiq_instance_get_debugfs_node(VCHIQ_INSTANCE_T instance)
4819 +{
4820 +       return &instance->debugfs_node;
4821 +}
4822 +
4823 +int
4824 +vchiq_instance_get_use_count(VCHIQ_INSTANCE_T instance)
4825 +{
4826 +       VCHIQ_SERVICE_T *service;
4827 +       int use_count = 0, i;
4828 +       i = 0;
4829 +       while ((service = next_service_by_instance(instance->state,
4830 +               instance, &i)) != NULL) {
4831 +               use_count += service->service_use_count;
4832 +               unlock_service(service);
4833 +       }
4834 +       return use_count;
4835 +}
4836 +
4837 +int
4838 +vchiq_instance_get_pid(VCHIQ_INSTANCE_T instance)
4839 +{
4840 +       return instance->pid;
4841 +}
4842 +
4843 +int
4844 +vchiq_instance_get_trace(VCHIQ_INSTANCE_T instance)
4845 +{
4846 +       return instance->trace;
4847 +}
4848 +
4849 +void
4850 +vchiq_instance_set_trace(VCHIQ_INSTANCE_T instance, int trace)
4851 +{
4852 +       VCHIQ_SERVICE_T *service;
4853 +       int i;
4854 +       i = 0;
4855 +       while ((service = next_service_by_instance(instance->state,
4856 +               instance, &i)) != NULL) {
4857 +               service->trace = trace;
4858 +               unlock_service(service);
4859 +       }
4860 +       instance->trace = (trace != 0);
4861 +}
4862 +
4863 +static void suspend_timer_callback(unsigned long context)
4864 +{
4865 +       VCHIQ_STATE_T *state = (VCHIQ_STATE_T *)context;
4866 +       VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
4867 +       if (!arm_state)
4868 +               goto out;
4869 +       vchiq_log_info(vchiq_susp_log_level,
4870 +               "%s - suspend timer expired - check suspend", __func__);
4871 +       vchiq_check_suspend(state);
4872 +out:
4873 +       return;
4874 +}
4875 +
4876 +VCHIQ_STATUS_T
4877 +vchiq_use_service_no_resume(VCHIQ_SERVICE_HANDLE_T handle)
4878 +{
4879 +       VCHIQ_STATUS_T ret = VCHIQ_ERROR;
4880 +       VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
4881 +       if (service) {
4882 +               ret = vchiq_use_internal(service->state, service,
4883 +                               USE_TYPE_SERVICE_NO_RESUME);
4884 +               unlock_service(service);
4885 +       }
4886 +       return ret;
4887 +}
4888 +
4889 +VCHIQ_STATUS_T
4890 +vchiq_use_service(VCHIQ_SERVICE_HANDLE_T handle)
4891 +{
4892 +       VCHIQ_STATUS_T ret = VCHIQ_ERROR;
4893 +       VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
4894 +       if (service) {
4895 +               ret = vchiq_use_internal(service->state, service,
4896 +                               USE_TYPE_SERVICE);
4897 +               unlock_service(service);
4898 +       }
4899 +       return ret;
4900 +}
4901 +
4902 +VCHIQ_STATUS_T
4903 +vchiq_release_service(VCHIQ_SERVICE_HANDLE_T handle)
4904 +{
4905 +       VCHIQ_STATUS_T ret = VCHIQ_ERROR;
4906 +       VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
4907 +       if (service) {
4908 +               ret = vchiq_release_internal(service->state, service);
4909 +               unlock_service(service);
4910 +       }
4911 +       return ret;
4912 +}
4913 +
4914 +void
4915 +vchiq_dump_service_use_state(VCHIQ_STATE_T *state)
4916 +{
4917 +       VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
4918 +       int i, j = 0;
4919 +       /* Only dump 64 services */
4920 +       static const int local_max_services = 64;
4921 +       /* If there's more than 64 services, only dump ones with
4922 +        * non-zero counts */
4923 +       int only_nonzero = 0;
4924 +       static const char *nz = "<-- preventing suspend";
4925 +
4926 +       enum vc_suspend_status vc_suspend_state;
4927 +       enum vc_resume_status  vc_resume_state;
4928 +       int peer_count;
4929 +       int vc_use_count;
4930 +       int active_services;
4931 +       struct service_data_struct {
4932 +               int fourcc;
4933 +               int clientid;
4934 +               int use_count;
4935 +       } service_data[local_max_services];
4936 +
4937 +       if (!arm_state)
4938 +               return;
4939 +
4940 +       read_lock_bh(&arm_state->susp_res_lock);
4941 +       vc_suspend_state = arm_state->vc_suspend_state;
4942 +       vc_resume_state  = arm_state->vc_resume_state;
4943 +       peer_count = arm_state->peer_use_count;
4944 +       vc_use_count = arm_state->videocore_use_count;
4945 +       active_services = state->unused_service;
4946 +       if (active_services > local_max_services)
4947 +               only_nonzero = 1;
4948 +
4949 +       for (i = 0; (i < active_services) && (j < local_max_services); i++) {
4950 +               VCHIQ_SERVICE_T *service_ptr = state->services[i];
4951 +               if (!service_ptr)
4952 +                       continue;
4953 +
4954 +               if (only_nonzero && !service_ptr->service_use_count)
4955 +                       continue;
4956 +
4957 +               if (service_ptr->srvstate != VCHIQ_SRVSTATE_FREE) {
4958 +                       service_data[j].fourcc = service_ptr->base.fourcc;
4959 +                       service_data[j].clientid = service_ptr->client_id;
4960 +                       service_data[j++].use_count = service_ptr->
4961 +                                                       service_use_count;
4962 +               }
4963 +       }
4964 +
4965 +       read_unlock_bh(&arm_state->susp_res_lock);
4966 +
4967 +       vchiq_log_warning(vchiq_susp_log_level,
4968 +               "-- Videcore suspend state: %s --",
4969 +               suspend_state_names[vc_suspend_state + VC_SUSPEND_NUM_OFFSET]);
4970 +       vchiq_log_warning(vchiq_susp_log_level,
4971 +               "-- Videcore resume state: %s --",
4972 +               resume_state_names[vc_resume_state + VC_RESUME_NUM_OFFSET]);
4973 +
4974 +       if (only_nonzero)
4975 +               vchiq_log_warning(vchiq_susp_log_level, "Too many active "
4976 +                       "services (%d).  Only dumping up to first %d services "
4977 +                       "with non-zero use-count", active_services,
4978 +                       local_max_services);
4979 +
4980 +       for (i = 0; i < j; i++) {
4981 +               vchiq_log_warning(vchiq_susp_log_level,
4982 +                       "----- %c%c%c%c:%d service count %d %s",
4983 +                       VCHIQ_FOURCC_AS_4CHARS(service_data[i].fourcc),
4984 +                       service_data[i].clientid,
4985 +                       service_data[i].use_count,
4986 +                       service_data[i].use_count ? nz : "");
4987 +       }
4988 +       vchiq_log_warning(vchiq_susp_log_level,
4989 +               "----- VCHIQ use count count %d", peer_count);
4990 +       vchiq_log_warning(vchiq_susp_log_level,
4991 +               "--- Overall vchiq instance use count %d", vc_use_count);
4992 +
4993 +       vchiq_dump_platform_use_state(state);
4994 +}
4995 +
4996 +VCHIQ_STATUS_T
4997 +vchiq_check_service(VCHIQ_SERVICE_T *service)
4998 +{
4999 +       VCHIQ_ARM_STATE_T *arm_state;
5000 +       VCHIQ_STATUS_T ret = VCHIQ_ERROR;
5001 +
5002 +       if (!service || !service->state)
5003 +               goto out;
5004 +
5005 +       vchiq_log_trace(vchiq_susp_log_level, "%s", __func__);
5006 +
5007 +       arm_state = vchiq_platform_get_arm_state(service->state);
5008 +
5009 +       read_lock_bh(&arm_state->susp_res_lock);
5010 +       if (service->service_use_count)
5011 +               ret = VCHIQ_SUCCESS;
5012 +       read_unlock_bh(&arm_state->susp_res_lock);
5013 +
5014 +       if (ret == VCHIQ_ERROR) {
5015 +               vchiq_log_error(vchiq_susp_log_level,
5016 +                       "%s ERROR - %c%c%c%c:%d service count %d, "
5017 +                       "state count %d, videocore suspend state %s", __func__,
5018 +                       VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
5019 +                       service->client_id, service->service_use_count,
5020 +                       arm_state->videocore_use_count,
5021 +                       suspend_state_names[arm_state->vc_suspend_state +
5022 +                                               VC_SUSPEND_NUM_OFFSET]);
5023 +               vchiq_dump_service_use_state(service->state);
5024 +       }
5025 +out:
5026 +       return ret;
5027 +}
5028 +
5029 +/* stub functions */
5030 +void vchiq_on_remote_use_active(VCHIQ_STATE_T *state)
5031 +{
5032 +       (void)state;
5033 +}
5034 +
5035 +void vchiq_platform_conn_state_changed(VCHIQ_STATE_T *state,
5036 +       VCHIQ_CONNSTATE_T oldstate, VCHIQ_CONNSTATE_T newstate)
5037 +{
5038 +       VCHIQ_ARM_STATE_T *arm_state = vchiq_platform_get_arm_state(state);
5039 +       vchiq_log_info(vchiq_susp_log_level, "%d: %s->%s", state->id,
5040 +               get_conn_state_name(oldstate), get_conn_state_name(newstate));
5041 +       if (state->conn_state == VCHIQ_CONNSTATE_CONNECTED) {
5042 +               write_lock_bh(&arm_state->susp_res_lock);
5043 +               if (!arm_state->first_connect) {
5044 +                       char threadname[10];
5045 +                       arm_state->first_connect = 1;
5046 +                       write_unlock_bh(&arm_state->susp_res_lock);
5047 +                       snprintf(threadname, sizeof(threadname), "VCHIQka-%d",
5048 +                               state->id);
5049 +                       arm_state->ka_thread = kthread_create(
5050 +                               &vchiq_keepalive_thread_func,
5051 +                               (void *)state,
5052 +                               threadname);
5053 +                       if (arm_state->ka_thread == NULL) {
5054 +                               vchiq_log_error(vchiq_susp_log_level,
5055 +                                       "vchiq: FATAL: couldn't create thread %s",
5056 +                                       threadname);
5057 +                       } else {
5058 +                               wake_up_process(arm_state->ka_thread);
5059 +                       }
5060 +               } else
5061 +                       write_unlock_bh(&arm_state->susp_res_lock);
5062 +       }
5063 +}
5064 +
5065 +
5066 +/****************************************************************************
5067 +*
5068 +*   vchiq_init - called when the module is loaded.
5069 +*
5070 +***************************************************************************/
5071 +
5072 +static int __init
5073 +vchiq_init(void)
5074 +{
5075 +       int err;
5076 +       void *ptr_err;
5077 +
5078 +       /* create debugfs entries */
5079 +       err = vchiq_debugfs_init();
5080 +       if (err != 0)
5081 +               goto failed_debugfs_init;
5082 +
5083 +       err = alloc_chrdev_region(&vchiq_devid, VCHIQ_MINOR, 1, DEVICE_NAME);
5084 +       if (err != 0) {
5085 +               vchiq_log_error(vchiq_arm_log_level,
5086 +                       "Unable to allocate device number");
5087 +               goto failed_alloc_chrdev;
5088 +       }
5089 +       cdev_init(&vchiq_cdev, &vchiq_fops);
5090 +       vchiq_cdev.owner = THIS_MODULE;
5091 +       err = cdev_add(&vchiq_cdev, vchiq_devid, 1);
5092 +       if (err != 0) {
5093 +               vchiq_log_error(vchiq_arm_log_level,
5094 +                       "Unable to register device");
5095 +               goto failed_cdev_add;
5096 +       }
5097 +
5098 +       /* create sysfs entries */
5099 +       vchiq_class = class_create(THIS_MODULE, DEVICE_NAME);
5100 +       ptr_err = vchiq_class;
5101 +       if (IS_ERR(ptr_err))
5102 +               goto failed_class_create;
5103 +
5104 +       vchiq_dev = device_create(vchiq_class, NULL,
5105 +               vchiq_devid, NULL, "vchiq");
5106 +       ptr_err = vchiq_dev;
5107 +       if (IS_ERR(ptr_err))
5108 +               goto failed_device_create;
5109 +
5110 +       err = vchiq_platform_init(&g_state);
5111 +       if (err != 0)
5112 +               goto failed_platform_init;
5113 +
5114 +       vchiq_log_info(vchiq_arm_log_level,
5115 +               "vchiq: initialised - version %d (min %d), device %d.%d",
5116 +               VCHIQ_VERSION, VCHIQ_VERSION_MIN,
5117 +               MAJOR(vchiq_devid), MINOR(vchiq_devid));
5118 +
5119 +       return 0;
5120 +
5121 +failed_platform_init:
5122 +       device_destroy(vchiq_class, vchiq_devid);
5123 +failed_device_create:
5124 +       class_destroy(vchiq_class);
5125 +failed_class_create:
5126 +       cdev_del(&vchiq_cdev);
5127 +       err = PTR_ERR(ptr_err);
5128 +failed_cdev_add:
5129 +       unregister_chrdev_region(vchiq_devid, 1);
5130 +failed_alloc_chrdev:
5131 +       vchiq_debugfs_deinit();
5132 +failed_debugfs_init:
5133 +       vchiq_log_warning(vchiq_arm_log_level, "could not load vchiq");
5134 +       return err;
5135 +}
5136 +
5137 +/****************************************************************************
5138 +*
5139 +*   vchiq_exit - called when the module is unloaded.
5140 +*
5141 +***************************************************************************/
5142 +
5143 +static void __exit
5144 +vchiq_exit(void)
5145 +{
5146 +       vchiq_platform_exit(&g_state);
5147 +       device_destroy(vchiq_class, vchiq_devid);
5148 +       class_destroy(vchiq_class);
5149 +       cdev_del(&vchiq_cdev);
5150 +       unregister_chrdev_region(vchiq_devid, 1);
5151 +}
5152 +
5153 +module_init(vchiq_init);
5154 +module_exit(vchiq_exit);
5155 +MODULE_LICENSE("GPL");
5156 +MODULE_AUTHOR("Broadcom Corporation");
5157 --- /dev/null
5158 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_arm.h
5159 @@ -0,0 +1,223 @@
5160 +/**
5161 + * Copyright (c) 2014 Raspberry Pi (Trading) Ltd. All rights reserved.
5162 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
5163 + *
5164 + * Redistribution and use in source and binary forms, with or without
5165 + * modification, are permitted provided that the following conditions
5166 + * are met:
5167 + * 1. Redistributions of source code must retain the above copyright
5168 + *    notice, this list of conditions, and the following disclaimer,
5169 + *    without modification.
5170 + * 2. Redistributions in binary form must reproduce the above copyright
5171 + *    notice, this list of conditions and the following disclaimer in the
5172 + *    documentation and/or other materials provided with the distribution.
5173 + * 3. The names of the above-listed copyright holders may not be used
5174 + *    to endorse or promote products derived from this software without
5175 + *    specific prior written permission.
5176 + *
5177 + * ALTERNATIVELY, this software may be distributed under the terms of the
5178 + * GNU General Public License ("GPL") version 2, as published by the Free
5179 + * Software Foundation.
5180 + *
5181 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
5182 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
5183 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
5184 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
5185 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
5186 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
5187 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
5188 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
5189 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
5190 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
5191 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5192 + */
5193 +
5194 +#ifndef VCHIQ_ARM_H
5195 +#define VCHIQ_ARM_H
5196 +
5197 +#include <linux/mutex.h>
5198 +#include <linux/semaphore.h>
5199 +#include <linux/atomic.h>
5200 +#include "vchiq_core.h"
5201 +#include "vchiq_debugfs.h"
5202 +
5203 +
5204 +enum vc_suspend_status {
5205 +       VC_SUSPEND_FORCE_CANCELED = -3, /* Force suspend canceled, too busy */
5206 +       VC_SUSPEND_REJECTED = -2,  /* Videocore rejected suspend request */
5207 +       VC_SUSPEND_FAILED = -1,    /* Videocore suspend failed */
5208 +       VC_SUSPEND_IDLE = 0,       /* VC active, no suspend actions */
5209 +       VC_SUSPEND_REQUESTED,      /* User has requested suspend */
5210 +       VC_SUSPEND_IN_PROGRESS,    /* Slot handler has recvd suspend request */
5211 +       VC_SUSPEND_SUSPENDED       /* Videocore suspend succeeded */
5212 +};
5213 +
5214 +enum vc_resume_status {
5215 +       VC_RESUME_FAILED = -1, /* Videocore resume failed */
5216 +       VC_RESUME_IDLE = 0,    /* VC suspended, no resume actions */
5217 +       VC_RESUME_REQUESTED,   /* User has requested resume */
5218 +       VC_RESUME_IN_PROGRESS, /* Slot handler has received resume request */
5219 +       VC_RESUME_RESUMED      /* Videocore resumed successfully (active) */
5220 +};
5221 +
5222 +
5223 +enum USE_TYPE_E {
5224 +       USE_TYPE_SERVICE,
5225 +       USE_TYPE_SERVICE_NO_RESUME,
5226 +       USE_TYPE_VCHIQ
5227 +};
5228 +
5229 +
5230 +
5231 +typedef struct vchiq_arm_state_struct {
5232 +       /* Keepalive-related data */
5233 +       struct task_struct *ka_thread;
5234 +       struct completion ka_evt;
5235 +       atomic_t ka_use_count;
5236 +       atomic_t ka_use_ack_count;
5237 +       atomic_t ka_release_count;
5238 +
5239 +       struct completion vc_suspend_complete;
5240 +       struct completion vc_resume_complete;
5241 +
5242 +       rwlock_t susp_res_lock;
5243 +       enum vc_suspend_status vc_suspend_state;
5244 +       enum vc_resume_status vc_resume_state;
5245 +
5246 +       unsigned int wake_address;
5247 +
5248 +       struct timer_list suspend_timer;
5249 +       int suspend_timer_timeout;
5250 +       int suspend_timer_running;
5251 +
5252 +       /* Global use count for videocore.
5253 +       ** This is equal to the sum of the use counts for all services.  When
5254 +       ** this hits zero the videocore suspend procedure will be initiated.
5255 +       */
5256 +       int videocore_use_count;
5257 +
5258 +       /* Use count to track requests from videocore peer.
5259 +       ** This use count is not associated with a service, so needs to be
5260 +       ** tracked separately with the state.
5261 +       */
5262 +       int peer_use_count;
5263 +
5264 +       /* Flag to indicate whether resume is blocked.  This happens when the
5265 +       ** ARM is suspending
5266 +       */
5267 +       struct completion resume_blocker;
5268 +       int resume_blocked;
5269 +       struct completion blocked_blocker;
5270 +       int blocked_count;
5271 +
5272 +       int autosuspend_override;
5273 +
5274 +       /* Flag to indicate that the first vchiq connect has made it through.
5275 +       ** This means that both sides should be fully ready, and we should
5276 +       ** be able to suspend after this point.
5277 +       */
5278 +       int first_connect;
5279 +
5280 +       unsigned long long suspend_start_time;
5281 +       unsigned long long sleep_start_time;
5282 +       unsigned long long resume_start_time;
5283 +       unsigned long long last_wake_time;
5284 +
5285 +} VCHIQ_ARM_STATE_T;
5286 +
5287 +extern int vchiq_arm_log_level;
5288 +extern int vchiq_susp_log_level;
5289 +
5290 +extern int __init
5291 +vchiq_platform_init(VCHIQ_STATE_T *state);
5292 +
5293 +extern void __exit
5294 +vchiq_platform_exit(VCHIQ_STATE_T *state);
5295 +
5296 +extern VCHIQ_STATE_T *
5297 +vchiq_get_state(void);
5298 +
5299 +extern VCHIQ_STATUS_T
5300 +vchiq_arm_vcsuspend(VCHIQ_STATE_T *state);
5301 +
5302 +extern VCHIQ_STATUS_T
5303 +vchiq_arm_force_suspend(VCHIQ_STATE_T *state);
5304 +
5305 +extern int
5306 +vchiq_arm_allow_resume(VCHIQ_STATE_T *state);
5307 +
5308 +extern VCHIQ_STATUS_T
5309 +vchiq_arm_vcresume(VCHIQ_STATE_T *state);
5310 +
5311 +extern VCHIQ_STATUS_T
5312 +vchiq_arm_init_state(VCHIQ_STATE_T *state, VCHIQ_ARM_STATE_T *arm_state);
5313 +
5314 +extern int
5315 +vchiq_check_resume(VCHIQ_STATE_T *state);
5316 +
5317 +extern void
5318 +vchiq_check_suspend(VCHIQ_STATE_T *state);
5319 + VCHIQ_STATUS_T
5320 +vchiq_use_service(VCHIQ_SERVICE_HANDLE_T handle);
5321 +
5322 +extern VCHIQ_STATUS_T
5323 +vchiq_release_service(VCHIQ_SERVICE_HANDLE_T handle);
5324 +
5325 +extern VCHIQ_STATUS_T
5326 +vchiq_check_service(VCHIQ_SERVICE_T *service);
5327 +
5328 +extern VCHIQ_STATUS_T
5329 +vchiq_platform_suspend(VCHIQ_STATE_T *state);
5330 +
5331 +extern int
5332 +vchiq_platform_videocore_wanted(VCHIQ_STATE_T *state);
5333 +
5334 +extern int
5335 +vchiq_platform_use_suspend_timer(void);
5336 +
5337 +extern void
5338 +vchiq_dump_platform_use_state(VCHIQ_STATE_T *state);
5339 +
5340 +extern void
5341 +vchiq_dump_service_use_state(VCHIQ_STATE_T *state);
5342 +
5343 +extern VCHIQ_ARM_STATE_T*
5344 +vchiq_platform_get_arm_state(VCHIQ_STATE_T *state);
5345 +
5346 +extern int
5347 +vchiq_videocore_wanted(VCHIQ_STATE_T *state);
5348 +
5349 +extern VCHIQ_STATUS_T
5350 +vchiq_use_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service,
5351 +               enum USE_TYPE_E use_type);
5352 +extern VCHIQ_STATUS_T
5353 +vchiq_release_internal(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service);
5354 +
5355 +extern VCHIQ_DEBUGFS_NODE_T *
5356 +vchiq_instance_get_debugfs_node(VCHIQ_INSTANCE_T instance);
5357 +
5358 +extern int
5359 +vchiq_instance_get_use_count(VCHIQ_INSTANCE_T instance);
5360 +
5361 +extern int
5362 +vchiq_instance_get_pid(VCHIQ_INSTANCE_T instance);
5363 +
5364 +extern int
5365 +vchiq_instance_get_trace(VCHIQ_INSTANCE_T instance);
5366 +
5367 +extern void
5368 +vchiq_instance_set_trace(VCHIQ_INSTANCE_T instance, int trace);
5369 +
5370 +extern void
5371 +set_suspend_state(VCHIQ_ARM_STATE_T *arm_state,
5372 +       enum vc_suspend_status new_state);
5373 +
5374 +extern void
5375 +set_resume_state(VCHIQ_ARM_STATE_T *arm_state,
5376 +       enum vc_resume_status new_state);
5377 +
5378 +extern void
5379 +start_suspend_timer(VCHIQ_ARM_STATE_T *arm_state);
5380 +
5381 +
5382 +#endif /* VCHIQ_ARM_H */
5383 --- /dev/null
5384 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_build_info.h
5385 @@ -0,0 +1,37 @@
5386 +/**
5387 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
5388 + *
5389 + * Redistribution and use in source and binary forms, with or without
5390 + * modification, are permitted provided that the following conditions
5391 + * are met:
5392 + * 1. Redistributions of source code must retain the above copyright
5393 + *    notice, this list of conditions, and the following disclaimer,
5394 + *    without modification.
5395 + * 2. Redistributions in binary form must reproduce the above copyright
5396 + *    notice, this list of conditions and the following disclaimer in the
5397 + *    documentation and/or other materials provided with the distribution.
5398 + * 3. The names of the above-listed copyright holders may not be used
5399 + *    to endorse or promote products derived from this software without
5400 + *    specific prior written permission.
5401 + *
5402 + * ALTERNATIVELY, this software may be distributed under the terms of the
5403 + * GNU General Public License ("GPL") version 2, as published by the Free
5404 + * Software Foundation.
5405 + *
5406 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
5407 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
5408 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
5409 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
5410 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
5411 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
5412 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
5413 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
5414 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
5415 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
5416 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5417 + */
5418 +
5419 +const char *vchiq_get_build_hostname(void);
5420 +const char *vchiq_get_build_version(void);
5421 +const char *vchiq_get_build_time(void);
5422 +const char *vchiq_get_build_date(void);
5423 --- /dev/null
5424 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_cfg.h
5425 @@ -0,0 +1,66 @@
5426 +/**
5427 + * Copyright (c) 2010-2014 Broadcom. All rights reserved.
5428 + *
5429 + * Redistribution and use in source and binary forms, with or without
5430 + * modification, are permitted provided that the following conditions
5431 + * are met:
5432 + * 1. Redistributions of source code must retain the above copyright
5433 + *    notice, this list of conditions, and the following disclaimer,
5434 + *    without modification.
5435 + * 2. Redistributions in binary form must reproduce the above copyright
5436 + *    notice, this list of conditions and the following disclaimer in the
5437 + *    documentation and/or other materials provided with the distribution.
5438 + * 3. The names of the above-listed copyright holders may not be used
5439 + *    to endorse or promote products derived from this software without
5440 + *    specific prior written permission.
5441 + *
5442 + * ALTERNATIVELY, this software may be distributed under the terms of the
5443 + * GNU General Public License ("GPL") version 2, as published by the Free
5444 + * Software Foundation.
5445 + *
5446 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
5447 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
5448 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
5449 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
5450 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
5451 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
5452 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
5453 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
5454 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
5455 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
5456 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5457 + */
5458 +
5459 +#ifndef VCHIQ_CFG_H
5460 +#define VCHIQ_CFG_H
5461 +
5462 +#define VCHIQ_MAGIC              VCHIQ_MAKE_FOURCC('V', 'C', 'H', 'I')
5463 +/* The version of VCHIQ - change with any non-trivial change */
5464 +#define VCHIQ_VERSION            7
5465 +/* The minimum compatible version - update to match VCHIQ_VERSION with any
5466 +** incompatible change */
5467 +#define VCHIQ_VERSION_MIN        3
5468 +
5469 +/* The version that introduced the VCHIQ_IOC_LIB_VERSION ioctl */
5470 +#define VCHIQ_VERSION_LIB_VERSION 7
5471 +
5472 +/* The version that introduced the VCHIQ_IOC_CLOSE_DELIVERED ioctl */
5473 +#define VCHIQ_VERSION_CLOSE_DELIVERED 7
5474 +
5475 +#define VCHIQ_MAX_STATES         1
5476 +#define VCHIQ_MAX_SERVICES       4096
5477 +#define VCHIQ_MAX_SLOTS          128
5478 +#define VCHIQ_MAX_SLOTS_PER_SIDE 64
5479 +
5480 +#define VCHIQ_NUM_CURRENT_BULKS        32
5481 +#define VCHIQ_NUM_SERVICE_BULKS        4
5482 +
5483 +#ifndef VCHIQ_ENABLE_DEBUG
5484 +#define VCHIQ_ENABLE_DEBUG             1
5485 +#endif
5486 +
5487 +#ifndef VCHIQ_ENABLE_STATS
5488 +#define VCHIQ_ENABLE_STATS             1
5489 +#endif
5490 +
5491 +#endif /* VCHIQ_CFG_H */
5492 --- /dev/null
5493 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.c
5494 @@ -0,0 +1,119 @@
5495 +/**
5496 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
5497 + *
5498 + * Redistribution and use in source and binary forms, with or without
5499 + * modification, are permitted provided that the following conditions
5500 + * are met:
5501 + * 1. Redistributions of source code must retain the above copyright
5502 + *    notice, this list of conditions, and the following disclaimer,
5503 + *    without modification.
5504 + * 2. Redistributions in binary form must reproduce the above copyright
5505 + *    notice, this list of conditions and the following disclaimer in the
5506 + *    documentation and/or other materials provided with the distribution.
5507 + * 3. The names of the above-listed copyright holders may not be used
5508 + *    to endorse or promote products derived from this software without
5509 + *    specific prior written permission.
5510 + *
5511 + * ALTERNATIVELY, this software may be distributed under the terms of the
5512 + * GNU General Public License ("GPL") version 2, as published by the Free
5513 + * Software Foundation.
5514 + *
5515 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
5516 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
5517 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
5518 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
5519 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
5520 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
5521 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
5522 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
5523 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
5524 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
5525 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5526 + */
5527 +
5528 +#include "vchiq_connected.h"
5529 +#include "vchiq_core.h"
5530 +#include <linux/module.h>
5531 +#include <linux/mutex.h>
5532 +
5533 +#define  MAX_CALLBACKS  10
5534 +
5535 +static   int                        g_connected;
5536 +static   int                        g_num_deferred_callbacks;
5537 +static   VCHIQ_CONNECTED_CALLBACK_T g_deferred_callback[MAX_CALLBACKS];
5538 +static   int                        g_once_init;
5539 +static   struct mutex               g_connected_mutex;
5540 +
5541 +/****************************************************************************
5542 +*
5543 +* Function to initialize our lock.
5544 +*
5545 +***************************************************************************/
5546 +
5547 +static void connected_init(void)
5548 +{
5549 +       if (!g_once_init) {
5550 +               mutex_init(&g_connected_mutex);
5551 +               g_once_init = 1;
5552 +       }
5553 +}
5554 +
5555 +/****************************************************************************
5556 +*
5557 +* This function is used to defer initialization until the vchiq stack is
5558 +* initialized. If the stack is already initialized, then the callback will
5559 +* be made immediately, otherwise it will be deferred until
5560 +* vchiq_call_connected_callbacks is called.
5561 +*
5562 +***************************************************************************/
5563 +
5564 +void vchiq_add_connected_callback(VCHIQ_CONNECTED_CALLBACK_T callback)
5565 +{
5566 +       connected_init();
5567 +
5568 +       if (mutex_lock_interruptible(&g_connected_mutex) != 0)
5569 +               return;
5570 +
5571 +       if (g_connected)
5572 +               /* We're already connected. Call the callback immediately. */
5573 +
5574 +               callback();
5575 +       else {
5576 +               if (g_num_deferred_callbacks >= MAX_CALLBACKS)
5577 +                       vchiq_log_error(vchiq_core_log_level,
5578 +                               "There already %d callback registered - "
5579 +                               "please increase MAX_CALLBACKS",
5580 +                               g_num_deferred_callbacks);
5581 +               else {
5582 +                       g_deferred_callback[g_num_deferred_callbacks] =
5583 +                               callback;
5584 +                       g_num_deferred_callbacks++;
5585 +               }
5586 +       }
5587 +       mutex_unlock(&g_connected_mutex);
5588 +}
5589 +
5590 +/****************************************************************************
5591 +*
5592 +* This function is called by the vchiq stack once it has been connected to
5593 +* the videocore and clients can start to use the stack.
5594 +*
5595 +***************************************************************************/
5596 +
5597 +void vchiq_call_connected_callbacks(void)
5598 +{
5599 +       int i;
5600 +
5601 +       connected_init();
5602 +
5603 +       if (mutex_lock_interruptible(&g_connected_mutex) != 0)
5604 +               return;
5605 +
5606 +       for (i = 0; i <  g_num_deferred_callbacks; i++)
5607 +               g_deferred_callback[i]();
5608 +
5609 +       g_num_deferred_callbacks = 0;
5610 +       g_connected = 1;
5611 +       mutex_unlock(&g_connected_mutex);
5612 +}
5613 +EXPORT_SYMBOL(vchiq_add_connected_callback);
5614 --- /dev/null
5615 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_connected.h
5616 @@ -0,0 +1,50 @@
5617 +/**
5618 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
5619 + *
5620 + * Redistribution and use in source and binary forms, with or without
5621 + * modification, are permitted provided that the following conditions
5622 + * are met:
5623 + * 1. Redistributions of source code must retain the above copyright
5624 + *    notice, this list of conditions, and the following disclaimer,
5625 + *    without modification.
5626 + * 2. Redistributions in binary form must reproduce the above copyright
5627 + *    notice, this list of conditions and the following disclaimer in the
5628 + *    documentation and/or other materials provided with the distribution.
5629 + * 3. The names of the above-listed copyright holders may not be used
5630 + *    to endorse or promote products derived from this software without
5631 + *    specific prior written permission.
5632 + *
5633 + * ALTERNATIVELY, this software may be distributed under the terms of the
5634 + * GNU General Public License ("GPL") version 2, as published by the Free
5635 + * Software Foundation.
5636 + *
5637 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
5638 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
5639 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
5640 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
5641 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
5642 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
5643 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
5644 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
5645 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
5646 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
5647 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5648 + */
5649 +
5650 +#ifndef VCHIQ_CONNECTED_H
5651 +#define VCHIQ_CONNECTED_H
5652 +
5653 +/* ---- Include Files ----------------------------------------------------- */
5654 +
5655 +/* ---- Constants and Types ---------------------------------------------- */
5656 +
5657 +typedef void (*VCHIQ_CONNECTED_CALLBACK_T)(void);
5658 +
5659 +/* ---- Variable Externs ------------------------------------------------- */
5660 +
5661 +/* ---- Function Prototypes ---------------------------------------------- */
5662 +
5663 +void vchiq_add_connected_callback(VCHIQ_CONNECTED_CALLBACK_T callback);
5664 +void vchiq_call_connected_callbacks(void);
5665 +
5666 +#endif /* VCHIQ_CONNECTED_H */
5667 --- /dev/null
5668 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.c
5669 @@ -0,0 +1,3861 @@
5670 +/**
5671 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
5672 + *
5673 + * Redistribution and use in source and binary forms, with or without
5674 + * modification, are permitted provided that the following conditions
5675 + * are met:
5676 + * 1. Redistributions of source code must retain the above copyright
5677 + *    notice, this list of conditions, and the following disclaimer,
5678 + *    without modification.
5679 + * 2. Redistributions in binary form must reproduce the above copyright
5680 + *    notice, this list of conditions and the following disclaimer in the
5681 + *    documentation and/or other materials provided with the distribution.
5682 + * 3. The names of the above-listed copyright holders may not be used
5683 + *    to endorse or promote products derived from this software without
5684 + *    specific prior written permission.
5685 + *
5686 + * ALTERNATIVELY, this software may be distributed under the terms of the
5687 + * GNU General Public License ("GPL") version 2, as published by the Free
5688 + * Software Foundation.
5689 + *
5690 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
5691 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
5692 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
5693 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
5694 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
5695 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
5696 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
5697 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
5698 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
5699 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
5700 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
5701 + */
5702 +
5703 +#include "vchiq_core.h"
5704 +
5705 +#define VCHIQ_SLOT_HANDLER_STACK 8192
5706 +
5707 +#define HANDLE_STATE_SHIFT 12
5708 +
5709 +#define SLOT_INFO_FROM_INDEX(state, index) (state->slot_info + (index))
5710 +#define SLOT_DATA_FROM_INDEX(state, index) (state->slot_data + (index))
5711 +#define SLOT_INDEX_FROM_DATA(state, data) \
5712 +       (((unsigned int)((char *)data - (char *)state->slot_data)) / \
5713 +       VCHIQ_SLOT_SIZE)
5714 +#define SLOT_INDEX_FROM_INFO(state, info) \
5715 +       ((unsigned int)(info - state->slot_info))
5716 +#define SLOT_QUEUE_INDEX_FROM_POS(pos) \
5717 +       ((int)((unsigned int)(pos) / VCHIQ_SLOT_SIZE))
5718 +
5719 +#define BULK_INDEX(x) (x & (VCHIQ_NUM_SERVICE_BULKS - 1))
5720 +
5721 +#define SRVTRACE_LEVEL(srv) \
5722 +       (((srv) && (srv)->trace) ? VCHIQ_LOG_TRACE : vchiq_core_msg_log_level)
5723 +#define SRVTRACE_ENABLED(srv, lev) \
5724 +       (((srv) && (srv)->trace) || (vchiq_core_msg_log_level >= (lev)))
5725 +
5726 +struct vchiq_open_payload {
5727 +       int fourcc;
5728 +       int client_id;
5729 +       short version;
5730 +       short version_min;
5731 +};
5732 +
5733 +struct vchiq_openack_payload {
5734 +       short version;
5735 +};
5736 +
5737 +/* we require this for consistency between endpoints */
5738 +vchiq_static_assert(sizeof(VCHIQ_HEADER_T) == 8);
5739 +vchiq_static_assert(IS_POW2(sizeof(VCHIQ_HEADER_T)));
5740 +vchiq_static_assert(IS_POW2(VCHIQ_NUM_CURRENT_BULKS));
5741 +vchiq_static_assert(IS_POW2(VCHIQ_NUM_SERVICE_BULKS));
5742 +vchiq_static_assert(IS_POW2(VCHIQ_MAX_SERVICES));
5743 +vchiq_static_assert(VCHIQ_VERSION >= VCHIQ_VERSION_MIN);
5744 +
5745 +/* Run time control of log level, based on KERN_XXX level. */
5746 +int vchiq_core_log_level = VCHIQ_LOG_DEFAULT;
5747 +int vchiq_core_msg_log_level = VCHIQ_LOG_DEFAULT;
5748 +int vchiq_sync_log_level = VCHIQ_LOG_DEFAULT;
5749 +
5750 +static atomic_t pause_bulks_count = ATOMIC_INIT(0);
5751 +
5752 +static DEFINE_SPINLOCK(service_spinlock);
5753 +DEFINE_SPINLOCK(bulk_waiter_spinlock);
5754 +DEFINE_SPINLOCK(quota_spinlock);
5755 +
5756 +VCHIQ_STATE_T *vchiq_states[VCHIQ_MAX_STATES];
5757 +static unsigned int handle_seq;
5758 +
5759 +static const char *const srvstate_names[] = {
5760 +       "FREE",
5761 +       "HIDDEN",
5762 +       "LISTENING",
5763 +       "OPENING",
5764 +       "OPEN",
5765 +       "OPENSYNC",
5766 +       "CLOSESENT",
5767 +       "CLOSERECVD",
5768 +       "CLOSEWAIT",
5769 +       "CLOSED"
5770 +};
5771 +
5772 +static const char *const reason_names[] = {
5773 +       "SERVICE_OPENED",
5774 +       "SERVICE_CLOSED",
5775 +       "MESSAGE_AVAILABLE",
5776 +       "BULK_TRANSMIT_DONE",
5777 +       "BULK_RECEIVE_DONE",
5778 +       "BULK_TRANSMIT_ABORTED",
5779 +       "BULK_RECEIVE_ABORTED"
5780 +};
5781 +
5782 +static const char *const conn_state_names[] = {
5783 +       "DISCONNECTED",
5784 +       "CONNECTING",
5785 +       "CONNECTED",
5786 +       "PAUSING",
5787 +       "PAUSE_SENT",
5788 +       "PAUSED",
5789 +       "RESUMING",
5790 +       "PAUSE_TIMEOUT",
5791 +       "RESUME_TIMEOUT"
5792 +};
5793 +
5794 +
5795 +static void
5796 +release_message_sync(VCHIQ_STATE_T *state, VCHIQ_HEADER_T *header);
5797 +
5798 +static const char *msg_type_str(unsigned int msg_type)
5799 +{
5800 +       switch (msg_type) {
5801 +       case VCHIQ_MSG_PADDING:       return "PADDING";
5802 +       case VCHIQ_MSG_CONNECT:       return "CONNECT";
5803 +       case VCHIQ_MSG_OPEN:          return "OPEN";
5804 +       case VCHIQ_MSG_OPENACK:       return "OPENACK";
5805 +       case VCHIQ_MSG_CLOSE:         return "CLOSE";
5806 +       case VCHIQ_MSG_DATA:          return "DATA";
5807 +       case VCHIQ_MSG_BULK_RX:       return "BULK_RX";
5808 +       case VCHIQ_MSG_BULK_TX:       return "BULK_TX";
5809 +       case VCHIQ_MSG_BULK_RX_DONE:  return "BULK_RX_DONE";
5810 +       case VCHIQ_MSG_BULK_TX_DONE:  return "BULK_TX_DONE";
5811 +       case VCHIQ_MSG_PAUSE:         return "PAUSE";
5812 +       case VCHIQ_MSG_RESUME:        return "RESUME";
5813 +       case VCHIQ_MSG_REMOTE_USE:    return "REMOTE_USE";
5814 +       case VCHIQ_MSG_REMOTE_RELEASE:      return "REMOTE_RELEASE";
5815 +       case VCHIQ_MSG_REMOTE_USE_ACTIVE:   return "REMOTE_USE_ACTIVE";
5816 +       }
5817 +       return "???";
5818 +}
5819 +
5820 +static inline void
5821 +vchiq_set_service_state(VCHIQ_SERVICE_T *service, int newstate)
5822 +{
5823 +       vchiq_log_info(vchiq_core_log_level, "%d: srv:%d %s->%s",
5824 +               service->state->id, service->localport,
5825 +               srvstate_names[service->srvstate],
5826 +               srvstate_names[newstate]);
5827 +       service->srvstate = newstate;
5828 +}
5829 +
5830 +VCHIQ_SERVICE_T *
5831 +find_service_by_handle(VCHIQ_SERVICE_HANDLE_T handle)
5832 +{
5833 +       VCHIQ_SERVICE_T *service;
5834 +
5835 +       spin_lock(&service_spinlock);
5836 +       service = handle_to_service(handle);
5837 +       if (service && (service->srvstate != VCHIQ_SRVSTATE_FREE) &&
5838 +               (service->handle == handle)) {
5839 +               BUG_ON(service->ref_count == 0);
5840 +               service->ref_count++;
5841 +       } else
5842 +               service = NULL;
5843 +       spin_unlock(&service_spinlock);
5844 +
5845 +       if (!service)
5846 +               vchiq_log_info(vchiq_core_log_level,
5847 +                       "Invalid service handle 0x%x", handle);
5848 +
5849 +       return service;
5850 +}
5851 +
5852 +VCHIQ_SERVICE_T *
5853 +find_service_by_port(VCHIQ_STATE_T *state, int localport)
5854 +{
5855 +       VCHIQ_SERVICE_T *service = NULL;
5856 +       if ((unsigned int)localport <= VCHIQ_PORT_MAX) {
5857 +               spin_lock(&service_spinlock);
5858 +               service = state->services[localport];
5859 +               if (service && (service->srvstate != VCHIQ_SRVSTATE_FREE)) {
5860 +                       BUG_ON(service->ref_count == 0);
5861 +                       service->ref_count++;
5862 +               } else
5863 +                       service = NULL;
5864 +               spin_unlock(&service_spinlock);
5865 +       }
5866 +
5867 +       if (!service)
5868 +               vchiq_log_info(vchiq_core_log_level,
5869 +                       "Invalid port %d", localport);
5870 +
5871 +       return service;
5872 +}
5873 +
5874 +VCHIQ_SERVICE_T *
5875 +find_service_for_instance(VCHIQ_INSTANCE_T instance,
5876 +       VCHIQ_SERVICE_HANDLE_T handle) {
5877 +       VCHIQ_SERVICE_T *service;
5878 +
5879 +       spin_lock(&service_spinlock);
5880 +       service = handle_to_service(handle);
5881 +       if (service && (service->srvstate != VCHIQ_SRVSTATE_FREE) &&
5882 +               (service->handle == handle) &&
5883 +               (service->instance == instance)) {
5884 +               BUG_ON(service->ref_count == 0);
5885 +               service->ref_count++;
5886 +       } else
5887 +               service = NULL;
5888 +       spin_unlock(&service_spinlock);
5889 +
5890 +       if (!service)
5891 +               vchiq_log_info(vchiq_core_log_level,
5892 +                       "Invalid service handle 0x%x", handle);
5893 +
5894 +       return service;
5895 +}
5896 +
5897 +VCHIQ_SERVICE_T *
5898 +find_closed_service_for_instance(VCHIQ_INSTANCE_T instance,
5899 +       VCHIQ_SERVICE_HANDLE_T handle) {
5900 +       VCHIQ_SERVICE_T *service;
5901 +
5902 +       spin_lock(&service_spinlock);
5903 +       service = handle_to_service(handle);
5904 +       if (service &&
5905 +               ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
5906 +                (service->srvstate == VCHIQ_SRVSTATE_CLOSED)) &&
5907 +               (service->handle == handle) &&
5908 +               (service->instance == instance)) {
5909 +               BUG_ON(service->ref_count == 0);
5910 +               service->ref_count++;
5911 +       } else
5912 +               service = NULL;
5913 +       spin_unlock(&service_spinlock);
5914 +
5915 +       if (!service)
5916 +               vchiq_log_info(vchiq_core_log_level,
5917 +                       "Invalid service handle 0x%x", handle);
5918 +
5919 +       return service;
5920 +}
5921 +
5922 +VCHIQ_SERVICE_T *
5923 +next_service_by_instance(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance,
5924 +       int *pidx)
5925 +{
5926 +       VCHIQ_SERVICE_T *service = NULL;
5927 +       int idx = *pidx;
5928 +
5929 +       spin_lock(&service_spinlock);
5930 +       while (idx < state->unused_service) {
5931 +               VCHIQ_SERVICE_T *srv = state->services[idx++];
5932 +               if (srv && (srv->srvstate != VCHIQ_SRVSTATE_FREE) &&
5933 +                       (srv->instance == instance)) {
5934 +                       service = srv;
5935 +                       BUG_ON(service->ref_count == 0);
5936 +                       service->ref_count++;
5937 +                       break;
5938 +               }
5939 +       }
5940 +       spin_unlock(&service_spinlock);
5941 +
5942 +       *pidx = idx;
5943 +
5944 +       return service;
5945 +}
5946 +
5947 +void
5948 +lock_service(VCHIQ_SERVICE_T *service)
5949 +{
5950 +       spin_lock(&service_spinlock);
5951 +       BUG_ON(!service || (service->ref_count == 0));
5952 +       if (service)
5953 +               service->ref_count++;
5954 +       spin_unlock(&service_spinlock);
5955 +}
5956 +
5957 +void
5958 +unlock_service(VCHIQ_SERVICE_T *service)
5959 +{
5960 +       VCHIQ_STATE_T *state = service->state;
5961 +       spin_lock(&service_spinlock);
5962 +       BUG_ON(!service || (service->ref_count == 0));
5963 +       if (service && service->ref_count) {
5964 +               service->ref_count--;
5965 +               if (!service->ref_count) {
5966 +                       BUG_ON(service->srvstate != VCHIQ_SRVSTATE_FREE);
5967 +                       state->services[service->localport] = NULL;
5968 +               } else
5969 +                       service = NULL;
5970 +       }
5971 +       spin_unlock(&service_spinlock);
5972 +
5973 +       if (service && service->userdata_term)
5974 +               service->userdata_term(service->base.userdata);
5975 +
5976 +       kfree(service);
5977 +}
5978 +
5979 +int
5980 +vchiq_get_client_id(VCHIQ_SERVICE_HANDLE_T handle)
5981 +{
5982 +       VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
5983 +       int id;
5984 +
5985 +       id = service ? service->client_id : 0;
5986 +       if (service)
5987 +               unlock_service(service);
5988 +
5989 +       return id;
5990 +}
5991 +
5992 +void *
5993 +vchiq_get_service_userdata(VCHIQ_SERVICE_HANDLE_T handle)
5994 +{
5995 +       VCHIQ_SERVICE_T *service = handle_to_service(handle);
5996 +
5997 +       return service ? service->base.userdata : NULL;
5998 +}
5999 +
6000 +int
6001 +vchiq_get_service_fourcc(VCHIQ_SERVICE_HANDLE_T handle)
6002 +{
6003 +       VCHIQ_SERVICE_T *service = handle_to_service(handle);
6004 +
6005 +       return service ? service->base.fourcc : 0;
6006 +}
6007 +
6008 +static void
6009 +mark_service_closing_internal(VCHIQ_SERVICE_T *service, int sh_thread)
6010 +{
6011 +       VCHIQ_STATE_T *state = service->state;
6012 +       VCHIQ_SERVICE_QUOTA_T *service_quota;
6013 +
6014 +       service->closing = 1;
6015 +
6016 +       /* Synchronise with other threads. */
6017 +       mutex_lock(&state->recycle_mutex);
6018 +       mutex_unlock(&state->recycle_mutex);
6019 +       if (!sh_thread || (state->conn_state != VCHIQ_CONNSTATE_PAUSE_SENT)) {
6020 +               /* If we're pausing then the slot_mutex is held until resume
6021 +                * by the slot handler.  Therefore don't try to acquire this
6022 +                * mutex if we're the slot handler and in the pause sent state.
6023 +                * We don't need to in this case anyway. */
6024 +               mutex_lock(&state->slot_mutex);
6025 +               mutex_unlock(&state->slot_mutex);
6026 +       }
6027 +
6028 +       /* Unblock any sending thread. */
6029 +       service_quota = &state->service_quotas[service->localport];
6030 +       up(&service_quota->quota_event);
6031 +}
6032 +
6033 +static void
6034 +mark_service_closing(VCHIQ_SERVICE_T *service)
6035 +{
6036 +       mark_service_closing_internal(service, 0);
6037 +}
6038 +
6039 +static inline VCHIQ_STATUS_T
6040 +make_service_callback(VCHIQ_SERVICE_T *service, VCHIQ_REASON_T reason,
6041 +       VCHIQ_HEADER_T *header, void *bulk_userdata)
6042 +{
6043 +       VCHIQ_STATUS_T status;
6044 +       vchiq_log_trace(vchiq_core_log_level, "%d: callback:%d (%s, %x, %x)",
6045 +               service->state->id, service->localport, reason_names[reason],
6046 +               (unsigned int)header, (unsigned int)bulk_userdata);
6047 +       status = service->base.callback(reason, header, service->handle,
6048 +               bulk_userdata);
6049 +       if (status == VCHIQ_ERROR) {
6050 +               vchiq_log_warning(vchiq_core_log_level,
6051 +                       "%d: ignoring ERROR from callback to service %x",
6052 +                       service->state->id, service->handle);
6053 +               status = VCHIQ_SUCCESS;
6054 +       }
6055 +       return status;
6056 +}
6057 +
6058 +inline void
6059 +vchiq_set_conn_state(VCHIQ_STATE_T *state, VCHIQ_CONNSTATE_T newstate)
6060 +{
6061 +       VCHIQ_CONNSTATE_T oldstate = state->conn_state;
6062 +       vchiq_log_info(vchiq_core_log_level, "%d: %s->%s", state->id,
6063 +               conn_state_names[oldstate],
6064 +               conn_state_names[newstate]);
6065 +       state->conn_state = newstate;
6066 +       vchiq_platform_conn_state_changed(state, oldstate, newstate);
6067 +}
6068 +
6069 +static inline void
6070 +remote_event_create(REMOTE_EVENT_T *event)
6071 +{
6072 +       event->armed = 0;
6073 +       /* Don't clear the 'fired' flag because it may already have been set
6074 +       ** by the other side. */
6075 +       sema_init(event->event, 0);
6076 +}
6077 +
6078 +static inline void
6079 +remote_event_destroy(REMOTE_EVENT_T *event)
6080 +{
6081 +       (void)event;
6082 +}
6083 +
6084 +static inline int
6085 +remote_event_wait(REMOTE_EVENT_T *event)
6086 +{
6087 +       if (!event->fired) {
6088 +               event->armed = 1;
6089 +               dsb();
6090 +               if (!event->fired) {
6091 +                       if (down_interruptible(event->event) != 0) {
6092 +                               event->armed = 0;
6093 +                               return 0;
6094 +                       }
6095 +               }
6096 +               event->armed = 0;
6097 +               wmb();
6098 +       }
6099 +
6100 +       event->fired = 0;
6101 +       return 1;
6102 +}
6103 +
6104 +static inline void
6105 +remote_event_signal_local(REMOTE_EVENT_T *event)
6106 +{
6107 +       event->armed = 0;
6108 +       up(event->event);
6109 +}
6110 +
6111 +static inline void
6112 +remote_event_poll(REMOTE_EVENT_T *event)
6113 +{
6114 +       if (event->fired && event->armed)
6115 +               remote_event_signal_local(event);
6116 +}
6117 +
6118 +void
6119 +remote_event_pollall(VCHIQ_STATE_T *state)
6120 +{
6121 +       remote_event_poll(&state->local->sync_trigger);
6122 +       remote_event_poll(&state->local->sync_release);
6123 +       remote_event_poll(&state->local->trigger);
6124 +       remote_event_poll(&state->local->recycle);
6125 +}
6126 +
6127 +/* Round up message sizes so that any space at the end of a slot is always big
6128 +** enough for a header. This relies on header size being a power of two, which
6129 +** has been verified earlier by a static assertion. */
6130 +
6131 +static inline unsigned int
6132 +calc_stride(unsigned int size)
6133 +{
6134 +       /* Allow room for the header */
6135 +       size += sizeof(VCHIQ_HEADER_T);
6136 +
6137 +       /* Round up */
6138 +       return (size + sizeof(VCHIQ_HEADER_T) - 1) & ~(sizeof(VCHIQ_HEADER_T)
6139 +               - 1);
6140 +}
6141 +
6142 +/* Called by the slot handler thread */
6143 +static VCHIQ_SERVICE_T *
6144 +get_listening_service(VCHIQ_STATE_T *state, int fourcc)
6145 +{
6146 +       int i;
6147 +
6148 +       WARN_ON(fourcc == VCHIQ_FOURCC_INVALID);
6149 +
6150 +       for (i = 0; i < state->unused_service; i++) {
6151 +               VCHIQ_SERVICE_T *service = state->services[i];
6152 +               if (service &&
6153 +                       (service->public_fourcc == fourcc) &&
6154 +                       ((service->srvstate == VCHIQ_SRVSTATE_LISTENING) ||
6155 +                       ((service->srvstate == VCHIQ_SRVSTATE_OPEN) &&
6156 +                       (service->remoteport == VCHIQ_PORT_FREE)))) {
6157 +                       lock_service(service);
6158 +                       return service;
6159 +               }
6160 +       }
6161 +
6162 +       return NULL;
6163 +}
6164 +
6165 +/* Called by the slot handler thread */
6166 +static VCHIQ_SERVICE_T *
6167 +get_connected_service(VCHIQ_STATE_T *state, unsigned int port)
6168 +{
6169 +       int i;
6170 +       for (i = 0; i < state->unused_service; i++) {
6171 +               VCHIQ_SERVICE_T *service = state->services[i];
6172 +               if (service && (service->srvstate == VCHIQ_SRVSTATE_OPEN)
6173 +                       && (service->remoteport == port)) {
6174 +                       lock_service(service);
6175 +                       return service;
6176 +               }
6177 +       }
6178 +       return NULL;
6179 +}
6180 +
6181 +inline void
6182 +request_poll(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service, int poll_type)
6183 +{
6184 +       uint32_t value;
6185 +
6186 +       if (service) {
6187 +               do {
6188 +                       value = atomic_read(&service->poll_flags);
6189 +               } while (atomic_cmpxchg(&service->poll_flags, value,
6190 +                       value | (1 << poll_type)) != value);
6191 +
6192 +               do {
6193 +                       value = atomic_read(&state->poll_services[
6194 +                               service->localport>>5]);
6195 +               } while (atomic_cmpxchg(
6196 +                       &state->poll_services[service->localport>>5],
6197 +                       value, value | (1 << (service->localport & 0x1f)))
6198 +                       != value);
6199 +       }
6200 +
6201 +       state->poll_needed = 1;
6202 +       wmb();
6203 +
6204 +       /* ... and ensure the slot handler runs. */
6205 +       remote_event_signal_local(&state->local->trigger);
6206 +}
6207 +
6208 +/* Called from queue_message, by the slot handler and application threads,
6209 +** with slot_mutex held */
6210 +static VCHIQ_HEADER_T *
6211 +reserve_space(VCHIQ_STATE_T *state, int space, int is_blocking)
6212 +{
6213 +       VCHIQ_SHARED_STATE_T *local = state->local;
6214 +       int tx_pos = state->local_tx_pos;
6215 +       int slot_space = VCHIQ_SLOT_SIZE - (tx_pos & VCHIQ_SLOT_MASK);
6216 +
6217 +       if (space > slot_space) {
6218 +               VCHIQ_HEADER_T *header;
6219 +               /* Fill the remaining space with padding */
6220 +               WARN_ON(state->tx_data == NULL);
6221 +               header = (VCHIQ_HEADER_T *)
6222 +                       (state->tx_data + (tx_pos & VCHIQ_SLOT_MASK));
6223 +               header->msgid = VCHIQ_MSGID_PADDING;
6224 +               header->size = slot_space - sizeof(VCHIQ_HEADER_T);
6225 +
6226 +               tx_pos += slot_space;
6227 +       }
6228 +
6229 +       /* If necessary, get the next slot. */
6230 +       if ((tx_pos & VCHIQ_SLOT_MASK) == 0) {
6231 +               int slot_index;
6232 +
6233 +               /* If there is no free slot... */
6234 +
6235 +               if (down_trylock(&state->slot_available_event) != 0) {
6236 +                       /* ...wait for one. */
6237 +
6238 +                       VCHIQ_STATS_INC(state, slot_stalls);
6239 +
6240 +                       /* But first, flush through the last slot. */
6241 +                       state->local_tx_pos = tx_pos;
6242 +                       local->tx_pos = tx_pos;
6243 +                       remote_event_signal(&state->remote->trigger);
6244 +
6245 +                       if (!is_blocking ||
6246 +                               (down_interruptible(
6247 +                               &state->slot_available_event) != 0))
6248 +                               return NULL; /* No space available */
6249 +               }
6250 +
6251 +               BUG_ON(tx_pos ==
6252 +                       (state->slot_queue_available * VCHIQ_SLOT_SIZE));
6253 +
6254 +               slot_index = local->slot_queue[
6255 +                       SLOT_QUEUE_INDEX_FROM_POS(tx_pos) &
6256 +                       VCHIQ_SLOT_QUEUE_MASK];
6257 +               state->tx_data =
6258 +                       (char *)SLOT_DATA_FROM_INDEX(state, slot_index);
6259 +       }
6260 +
6261 +       state->local_tx_pos = tx_pos + space;
6262 +
6263 +       return (VCHIQ_HEADER_T *)(state->tx_data + (tx_pos & VCHIQ_SLOT_MASK));
6264 +}
6265 +
6266 +/* Called by the recycle thread. */
6267 +static void
6268 +process_free_queue(VCHIQ_STATE_T *state)
6269 +{
6270 +       VCHIQ_SHARED_STATE_T *local = state->local;
6271 +       BITSET_T service_found[BITSET_SIZE(VCHIQ_MAX_SERVICES)];
6272 +       int slot_queue_available;
6273 +
6274 +       /* Use a read memory barrier to ensure that any state that may have
6275 +       ** been modified by another thread is not masked by stale prefetched
6276 +       ** values. */
6277 +       rmb();
6278 +
6279 +       /* Find slots which have been freed by the other side, and return them
6280 +       ** to the available queue. */
6281 +       slot_queue_available = state->slot_queue_available;
6282 +
6283 +       while (slot_queue_available != local->slot_queue_recycle) {
6284 +               unsigned int pos;
6285 +               int slot_index = local->slot_queue[slot_queue_available++ &
6286 +                       VCHIQ_SLOT_QUEUE_MASK];
6287 +               char *data = (char *)SLOT_DATA_FROM_INDEX(state, slot_index);
6288 +               int data_found = 0;
6289 +
6290 +               vchiq_log_trace(vchiq_core_log_level, "%d: pfq %d=%x %x %x",
6291 +                       state->id, slot_index, (unsigned int)data,
6292 +                       local->slot_queue_recycle, slot_queue_available);
6293 +
6294 +               /* Initialise the bitmask for services which have used this
6295 +               ** slot */
6296 +               BITSET_ZERO(service_found);
6297 +
6298 +               pos = 0;
6299 +
6300 +               while (pos < VCHIQ_SLOT_SIZE) {
6301 +                       VCHIQ_HEADER_T *header =
6302 +                               (VCHIQ_HEADER_T *)(data + pos);
6303 +                       int msgid = header->msgid;
6304 +                       if (VCHIQ_MSG_TYPE(msgid) == VCHIQ_MSG_DATA) {
6305 +                               int port = VCHIQ_MSG_SRCPORT(msgid);
6306 +                               VCHIQ_SERVICE_QUOTA_T *service_quota =
6307 +                                       &state->service_quotas[port];
6308 +                               int count;
6309 +                               spin_lock(&quota_spinlock);
6310 +                               count = service_quota->message_use_count;
6311 +                               if (count > 0)
6312 +                                       service_quota->message_use_count =
6313 +                                               count - 1;
6314 +                               spin_unlock(&quota_spinlock);
6315 +
6316 +                               if (count == service_quota->message_quota)
6317 +                                       /* Signal the service that it
6318 +                                       ** has dropped below its quota
6319 +                                       */
6320 +                                       up(&service_quota->quota_event);
6321 +                               else if (count == 0) {
6322 +                                       vchiq_log_error(vchiq_core_log_level,
6323 +                                               "service %d "
6324 +                                               "message_use_count=%d "
6325 +                                               "(header %x, msgid %x, "
6326 +                                               "header->msgid %x, "
6327 +                                               "header->size %x)",
6328 +                                               port,
6329 +                                               service_quota->
6330 +                                                       message_use_count,
6331 +                                               (unsigned int)header, msgid,
6332 +                                               header->msgid,
6333 +                                               header->size);
6334 +                                       WARN(1, "invalid message use count\n");
6335 +                               }
6336 +                               if (!BITSET_IS_SET(service_found, port)) {
6337 +                                       /* Set the found bit for this service */
6338 +                                       BITSET_SET(service_found, port);
6339 +
6340 +                                       spin_lock(&quota_spinlock);
6341 +                                       count = service_quota->slot_use_count;
6342 +                                       if (count > 0)
6343 +                                               service_quota->slot_use_count =
6344 +                                                       count - 1;
6345 +                                       spin_unlock(&quota_spinlock);
6346 +
6347 +                                       if (count > 0) {
6348 +                                               /* Signal the service in case
6349 +                                               ** it has dropped below its
6350 +                                               ** quota */
6351 +                                               up(&service_quota->quota_event);
6352 +                                               vchiq_log_trace(
6353 +                                                       vchiq_core_log_level,
6354 +                                                       "%d: pfq:%d %x@%x - "
6355 +                                                       "slot_use->%d",
6356 +                                                       state->id, port,
6357 +                                                       header->size,
6358 +                                                       (unsigned int)header,
6359 +                                                       count - 1);
6360 +                                       } else {
6361 +                                               vchiq_log_error(
6362 +                                                       vchiq_core_log_level,
6363 +                                                               "service %d "
6364 +                                                               "slot_use_count"
6365 +                                                               "=%d (header %x"
6366 +                                                               ", msgid %x, "
6367 +                                                               "header->msgid"
6368 +                                                               " %x, header->"
6369 +                                                               "size %x)",
6370 +                                                       port, count,
6371 +                                                       (unsigned int)header,
6372 +                                                       msgid,
6373 +                                                       header->msgid,
6374 +                                                       header->size);
6375 +                                               WARN(1, "bad slot use count\n");
6376 +                                       }
6377 +                               }
6378 +
6379 +                               data_found = 1;
6380 +                       }
6381 +
6382 +                       pos += calc_stride(header->size);
6383 +                       if (pos > VCHIQ_SLOT_SIZE) {
6384 +                               vchiq_log_error(vchiq_core_log_level,
6385 +                                       "pfq - pos %x: header %x, msgid %x, "
6386 +                                       "header->msgid %x, header->size %x",
6387 +                                       pos, (unsigned int)header, msgid,
6388 +                                       header->msgid, header->size);
6389 +                               WARN(1, "invalid slot position\n");
6390 +                       }
6391 +               }
6392 +
6393 +               if (data_found) {
6394 +                       int count;
6395 +                       spin_lock(&quota_spinlock);
6396 +                       count = state->data_use_count;
6397 +                       if (count > 0)
6398 +                               state->data_use_count =
6399 +                                       count - 1;
6400 +                       spin_unlock(&quota_spinlock);
6401 +                       if (count == state->data_quota)
6402 +                               up(&state->data_quota_event);
6403 +               }
6404 +
6405 +               state->slot_queue_available = slot_queue_available;
6406 +               up(&state->slot_available_event);
6407 +       }
6408 +}
6409 +
6410 +/* Called by the slot handler and application threads */
6411 +static VCHIQ_STATUS_T
6412 +queue_message(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service,
6413 +       int msgid, const VCHIQ_ELEMENT_T *elements,
6414 +       int count, int size, int is_blocking)
6415 +{
6416 +       VCHIQ_SHARED_STATE_T *local;
6417 +       VCHIQ_SERVICE_QUOTA_T *service_quota = NULL;
6418 +       VCHIQ_HEADER_T *header;
6419 +       int type = VCHIQ_MSG_TYPE(msgid);
6420 +
6421 +       unsigned int stride;
6422 +
6423 +       local = state->local;
6424 +
6425 +       stride = calc_stride(size);
6426 +
6427 +       WARN_ON(!(stride <= VCHIQ_SLOT_SIZE));
6428 +
6429 +       if ((type != VCHIQ_MSG_RESUME) &&
6430 +               (mutex_lock_interruptible(&state->slot_mutex) != 0))
6431 +               return VCHIQ_RETRY;
6432 +
6433 +       if (type == VCHIQ_MSG_DATA) {
6434 +               int tx_end_index;
6435 +
6436 +               BUG_ON(!service);
6437 +
6438 +               if (service->closing) {
6439 +                       /* The service has been closed */
6440 +                       mutex_unlock(&state->slot_mutex);
6441 +                       return VCHIQ_ERROR;
6442 +               }
6443 +
6444 +               service_quota = &state->service_quotas[service->localport];
6445 +
6446 +               spin_lock(&quota_spinlock);
6447 +
6448 +               /* Ensure this service doesn't use more than its quota of
6449 +               ** messages or slots */
6450 +               tx_end_index = SLOT_QUEUE_INDEX_FROM_POS(
6451 +                       state->local_tx_pos + stride - 1);
6452 +
6453 +               /* Ensure data messages don't use more than their quota of
6454 +               ** slots */
6455 +               while ((tx_end_index != state->previous_data_index) &&
6456 +                       (state->data_use_count == state->data_quota)) {
6457 +                       VCHIQ_STATS_INC(state, data_stalls);
6458 +                       spin_unlock(&quota_spinlock);
6459 +                       mutex_unlock(&state->slot_mutex);
6460 +
6461 +                       if (down_interruptible(&state->data_quota_event)
6462 +                               != 0)
6463 +                               return VCHIQ_RETRY;
6464 +
6465 +                       mutex_lock(&state->slot_mutex);
6466 +                       spin_lock(&quota_spinlock);
6467 +                       tx_end_index = SLOT_QUEUE_INDEX_FROM_POS(
6468 +                               state->local_tx_pos + stride - 1);
6469 +                       if ((tx_end_index == state->previous_data_index) ||
6470 +                               (state->data_use_count < state->data_quota)) {
6471 +                               /* Pass the signal on to other waiters */
6472 +                               up(&state->data_quota_event);
6473 +                               break;
6474 +                       }
6475 +               }
6476 +
6477 +               while ((service_quota->message_use_count ==
6478 +                               service_quota->message_quota) ||
6479 +                       ((tx_end_index != service_quota->previous_tx_index) &&
6480 +                       (service_quota->slot_use_count ==
6481 +                               service_quota->slot_quota))) {
6482 +                       spin_unlock(&quota_spinlock);
6483 +                       vchiq_log_trace(vchiq_core_log_level,
6484 +                               "%d: qm:%d %s,%x - quota stall "
6485 +                               "(msg %d, slot %d)",
6486 +                               state->id, service->localport,
6487 +                               msg_type_str(type), size,
6488 +                               service_quota->message_use_count,
6489 +                               service_quota->slot_use_count);
6490 +                       VCHIQ_SERVICE_STATS_INC(service, quota_stalls);
6491 +                       mutex_unlock(&state->slot_mutex);
6492 +                       if (down_interruptible(&service_quota->quota_event)
6493 +                               != 0)
6494 +                               return VCHIQ_RETRY;
6495 +                       if (service->closing)
6496 +                               return VCHIQ_ERROR;
6497 +                       if (mutex_lock_interruptible(&state->slot_mutex) != 0)
6498 +                               return VCHIQ_RETRY;
6499 +                       if (service->srvstate != VCHIQ_SRVSTATE_OPEN) {
6500 +                               /* The service has been closed */
6501 +                               mutex_unlock(&state->slot_mutex);
6502 +                               return VCHIQ_ERROR;
6503 +                       }
6504 +                       spin_lock(&quota_spinlock);
6505 +                       tx_end_index = SLOT_QUEUE_INDEX_FROM_POS(
6506 +                               state->local_tx_pos + stride - 1);
6507 +               }
6508 +
6509 +               spin_unlock(&quota_spinlock);
6510 +       }
6511 +
6512 +       header = reserve_space(state, stride, is_blocking);
6513 +
6514 +       if (!header) {
6515 +               if (service)
6516 +                       VCHIQ_SERVICE_STATS_INC(service, slot_stalls);
6517 +               mutex_unlock(&state->slot_mutex);
6518 +               return VCHIQ_RETRY;
6519 +       }
6520 +
6521 +       if (type == VCHIQ_MSG_DATA) {
6522 +               int i, pos;
6523 +               int tx_end_index;
6524 +               int slot_use_count;
6525 +
6526 +               vchiq_log_info(vchiq_core_log_level,
6527 +                       "%d: qm %s@%x,%x (%d->%d)",
6528 +                       state->id,
6529 +                       msg_type_str(VCHIQ_MSG_TYPE(msgid)),
6530 +                       (unsigned int)header, size,
6531 +                       VCHIQ_MSG_SRCPORT(msgid),
6532 +                       VCHIQ_MSG_DSTPORT(msgid));
6533 +
6534 +               BUG_ON(!service);
6535 +
6536 +               for (i = 0, pos = 0; i < (unsigned int)count;
6537 +                       pos += elements[i++].size)
6538 +                       if (elements[i].size) {
6539 +                               if (vchiq_copy_from_user
6540 +                                       (header->data + pos, elements[i].data,
6541 +                                       (size_t) elements[i].size) !=
6542 +                                       VCHIQ_SUCCESS) {
6543 +                                       mutex_unlock(&state->slot_mutex);
6544 +                                       VCHIQ_SERVICE_STATS_INC(service,
6545 +                                               error_count);
6546 +                                       return VCHIQ_ERROR;
6547 +                               }
6548 +                               if (i == 0) {
6549 +                                       if (SRVTRACE_ENABLED(service,
6550 +                                                       VCHIQ_LOG_INFO))
6551 +                                               vchiq_log_dump_mem("Sent", 0,
6552 +                                                       header->data + pos,
6553 +                                                       min(64u,
6554 +                                                       elements[0].size));
6555 +                               }
6556 +                       }
6557 +
6558 +               spin_lock(&quota_spinlock);
6559 +               service_quota->message_use_count++;
6560 +
6561 +               tx_end_index =
6562 +                       SLOT_QUEUE_INDEX_FROM_POS(state->local_tx_pos - 1);
6563 +
6564 +               /* If this transmission can't fit in the last slot used by any
6565 +               ** service, the data_use_count must be increased. */
6566 +               if (tx_end_index != state->previous_data_index) {
6567 +                       state->previous_data_index = tx_end_index;
6568 +                       state->data_use_count++;
6569 +               }
6570 +
6571 +               /* If this isn't the same slot last used by this service,
6572 +               ** the service's slot_use_count must be increased. */
6573 +               if (tx_end_index != service_quota->previous_tx_index) {
6574 +                       service_quota->previous_tx_index = tx_end_index;
6575 +                       slot_use_count = ++service_quota->slot_use_count;
6576 +               } else {
6577 +                       slot_use_count = 0;
6578 +               }
6579 +
6580 +               spin_unlock(&quota_spinlock);
6581 +
6582 +               if (slot_use_count)
6583 +                       vchiq_log_trace(vchiq_core_log_level,
6584 +                               "%d: qm:%d %s,%x - slot_use->%d (hdr %p)",
6585 +                               state->id, service->localport,
6586 +                               msg_type_str(VCHIQ_MSG_TYPE(msgid)), size,
6587 +                               slot_use_count, header);
6588 +
6589 +               VCHIQ_SERVICE_STATS_INC(service, ctrl_tx_count);
6590 +               VCHIQ_SERVICE_STATS_ADD(service, ctrl_tx_bytes, size);
6591 +       } else {
6592 +               vchiq_log_info(vchiq_core_log_level,
6593 +                       "%d: qm %s@%x,%x (%d->%d)", state->id,
6594 +                       msg_type_str(VCHIQ_MSG_TYPE(msgid)),
6595 +                       (unsigned int)header, size,
6596 +                       VCHIQ_MSG_SRCPORT(msgid),
6597 +                       VCHIQ_MSG_DSTPORT(msgid));
6598 +               if (size != 0) {
6599 +                       WARN_ON(!((count == 1) && (size == elements[0].size)));
6600 +                       memcpy(header->data, elements[0].data,
6601 +                               elements[0].size);
6602 +               }
6603 +               VCHIQ_STATS_INC(state, ctrl_tx_count);
6604 +       }
6605 +
6606 +       header->msgid = msgid;
6607 +       header->size = size;
6608 +
6609 +       {
6610 +               int svc_fourcc;
6611 +
6612 +               svc_fourcc = service
6613 +                       ? service->base.fourcc
6614 +                       : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
6615 +
6616 +               vchiq_log_info(SRVTRACE_LEVEL(service),
6617 +                       "Sent Msg %s(%u) to %c%c%c%c s:%u d:%d len:%d",
6618 +                       msg_type_str(VCHIQ_MSG_TYPE(msgid)),
6619 +                       VCHIQ_MSG_TYPE(msgid),
6620 +                       VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
6621 +                       VCHIQ_MSG_SRCPORT(msgid),
6622 +                       VCHIQ_MSG_DSTPORT(msgid),
6623 +                       size);
6624 +       }
6625 +
6626 +       /* Make sure the new header is visible to the peer. */
6627 +       wmb();
6628 +
6629 +       /* Make the new tx_pos visible to the peer. */
6630 +       local->tx_pos = state->local_tx_pos;
6631 +       wmb();
6632 +
6633 +       if (service && (type == VCHIQ_MSG_CLOSE))
6634 +               vchiq_set_service_state(service, VCHIQ_SRVSTATE_CLOSESENT);
6635 +
6636 +       if (VCHIQ_MSG_TYPE(msgid) != VCHIQ_MSG_PAUSE)
6637 +               mutex_unlock(&state->slot_mutex);
6638 +
6639 +       remote_event_signal(&state->remote->trigger);
6640 +
6641 +       return VCHIQ_SUCCESS;
6642 +}
6643 +
6644 +/* Called by the slot handler and application threads */
6645 +static VCHIQ_STATUS_T
6646 +queue_message_sync(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service,
6647 +       int msgid, const VCHIQ_ELEMENT_T *elements,
6648 +       int count, int size, int is_blocking)
6649 +{
6650 +       VCHIQ_SHARED_STATE_T *local;
6651 +       VCHIQ_HEADER_T *header;
6652 +
6653 +       local = state->local;
6654 +
6655 +       if ((VCHIQ_MSG_TYPE(msgid) != VCHIQ_MSG_RESUME) &&
6656 +               (mutex_lock_interruptible(&state->sync_mutex) != 0))
6657 +               return VCHIQ_RETRY;
6658 +
6659 +       remote_event_wait(&local->sync_release);
6660 +
6661 +       rmb();
6662 +
6663 +       header = (VCHIQ_HEADER_T *)SLOT_DATA_FROM_INDEX(state,
6664 +               local->slot_sync);
6665 +
6666 +       {
6667 +               int oldmsgid = header->msgid;
6668 +               if (oldmsgid != VCHIQ_MSGID_PADDING)
6669 +                       vchiq_log_error(vchiq_core_log_level,
6670 +                               "%d: qms - msgid %x, not PADDING",
6671 +                               state->id, oldmsgid);
6672 +       }
6673 +
6674 +       if (service) {
6675 +               int i, pos;
6676 +
6677 +               vchiq_log_info(vchiq_sync_log_level,
6678 +                       "%d: qms %s@%x,%x (%d->%d)", state->id,
6679 +                       msg_type_str(VCHIQ_MSG_TYPE(msgid)),
6680 +                       (unsigned int)header, size,
6681 +                       VCHIQ_MSG_SRCPORT(msgid),
6682 +                       VCHIQ_MSG_DSTPORT(msgid));
6683 +
6684 +               for (i = 0, pos = 0; i < (unsigned int)count;
6685 +                       pos += elements[i++].size)
6686 +                       if (elements[i].size) {
6687 +                               if (vchiq_copy_from_user
6688 +                                       (header->data + pos, elements[i].data,
6689 +                                       (size_t) elements[i].size) !=
6690 +                                       VCHIQ_SUCCESS) {
6691 +                                       mutex_unlock(&state->sync_mutex);
6692 +                                       VCHIQ_SERVICE_STATS_INC(service,
6693 +                                               error_count);
6694 +                                       return VCHIQ_ERROR;
6695 +                               }
6696 +                               if (i == 0) {
6697 +                                       if (vchiq_sync_log_level >=
6698 +                                               VCHIQ_LOG_TRACE)
6699 +                                               vchiq_log_dump_mem("Sent Sync",
6700 +                                                       0, header->data + pos,
6701 +                                                       min(64u,
6702 +                                                       elements[0].size));
6703 +                               }
6704 +                       }
6705 +
6706 +               VCHIQ_SERVICE_STATS_INC(service, ctrl_tx_count);
6707 +               VCHIQ_SERVICE_STATS_ADD(service, ctrl_tx_bytes, size);
6708 +       } else {
6709 +               vchiq_log_info(vchiq_sync_log_level,
6710 +                       "%d: qms %s@%x,%x (%d->%d)", state->id,
6711 +                       msg_type_str(VCHIQ_MSG_TYPE(msgid)),
6712 +                       (unsigned int)header, size,
6713 +                       VCHIQ_MSG_SRCPORT(msgid),
6714 +                       VCHIQ_MSG_DSTPORT(msgid));
6715 +               if (size != 0) {
6716 +                       WARN_ON(!((count == 1) && (size == elements[0].size)));
6717 +                       memcpy(header->data, elements[0].data,
6718 +                               elements[0].size);
6719 +               }
6720 +               VCHIQ_STATS_INC(state, ctrl_tx_count);
6721 +       }
6722 +
6723 +       header->size = size;
6724 +       header->msgid = msgid;
6725 +
6726 +       if (vchiq_sync_log_level >= VCHIQ_LOG_TRACE) {
6727 +               int svc_fourcc;
6728 +
6729 +               svc_fourcc = service
6730 +                       ? service->base.fourcc
6731 +                       : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
6732 +
6733 +               vchiq_log_trace(vchiq_sync_log_level,
6734 +                       "Sent Sync Msg %s(%u) to %c%c%c%c s:%u d:%d len:%d",
6735 +                       msg_type_str(VCHIQ_MSG_TYPE(msgid)),
6736 +                       VCHIQ_MSG_TYPE(msgid),
6737 +                       VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
6738 +                       VCHIQ_MSG_SRCPORT(msgid),
6739 +                       VCHIQ_MSG_DSTPORT(msgid),
6740 +                       size);
6741 +       }
6742 +
6743 +       /* Make sure the new header is visible to the peer. */
6744 +       wmb();
6745 +
6746 +       remote_event_signal(&state->remote->sync_trigger);
6747 +
6748 +       if (VCHIQ_MSG_TYPE(msgid) != VCHIQ_MSG_PAUSE)
6749 +               mutex_unlock(&state->sync_mutex);
6750 +
6751 +       return VCHIQ_SUCCESS;
6752 +}
6753 +
6754 +static inline void
6755 +claim_slot(VCHIQ_SLOT_INFO_T *slot)
6756 +{
6757 +       slot->use_count++;
6758 +}
6759 +
6760 +static void
6761 +release_slot(VCHIQ_STATE_T *state, VCHIQ_SLOT_INFO_T *slot_info,
6762 +       VCHIQ_HEADER_T *header, VCHIQ_SERVICE_T *service)
6763 +{
6764 +       int release_count;
6765 +
6766 +       mutex_lock(&state->recycle_mutex);
6767 +
6768 +       if (header) {
6769 +               int msgid = header->msgid;
6770 +               if (((msgid & VCHIQ_MSGID_CLAIMED) == 0) ||
6771 +                       (service && service->closing)) {
6772 +                       mutex_unlock(&state->recycle_mutex);
6773 +                       return;
6774 +               }
6775 +
6776 +               /* Rewrite the message header to prevent a double
6777 +               ** release */
6778 +               header->msgid = msgid & ~VCHIQ_MSGID_CLAIMED;
6779 +       }
6780 +
6781 +       release_count = slot_info->release_count;
6782 +       slot_info->release_count = ++release_count;
6783 +
6784 +       if (release_count == slot_info->use_count) {
6785 +               int slot_queue_recycle;
6786 +               /* Add to the freed queue */
6787 +
6788 +               /* A read barrier is necessary here to prevent speculative
6789 +               ** fetches of remote->slot_queue_recycle from overtaking the
6790 +               ** mutex. */
6791 +               rmb();
6792 +
6793 +               slot_queue_recycle = state->remote->slot_queue_recycle;
6794 +               state->remote->slot_queue[slot_queue_recycle &
6795 +                       VCHIQ_SLOT_QUEUE_MASK] =
6796 +                       SLOT_INDEX_FROM_INFO(state, slot_info);
6797 +               state->remote->slot_queue_recycle = slot_queue_recycle + 1;
6798 +               vchiq_log_info(vchiq_core_log_level,
6799 +                       "%d: release_slot %d - recycle->%x",
6800 +                       state->id, SLOT_INDEX_FROM_INFO(state, slot_info),
6801 +                       state->remote->slot_queue_recycle);
6802 +
6803 +               /* A write barrier is necessary, but remote_event_signal
6804 +               ** contains one. */
6805 +               remote_event_signal(&state->remote->recycle);
6806 +       }
6807 +
6808 +       mutex_unlock(&state->recycle_mutex);
6809 +}
6810 +
6811 +/* Called by the slot handler - don't hold the bulk mutex */
6812 +static VCHIQ_STATUS_T
6813 +notify_bulks(VCHIQ_SERVICE_T *service, VCHIQ_BULK_QUEUE_T *queue,
6814 +       int retry_poll)
6815 +{
6816 +       VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
6817 +
6818 +       vchiq_log_trace(vchiq_core_log_level,
6819 +               "%d: nb:%d %cx - p=%x rn=%x r=%x",
6820 +               service->state->id, service->localport,
6821 +               (queue == &service->bulk_tx) ? 't' : 'r',
6822 +               queue->process, queue->remote_notify, queue->remove);
6823 +
6824 +       if (service->state->is_master) {
6825 +               while (queue->remote_notify != queue->process) {
6826 +                       VCHIQ_BULK_T *bulk =
6827 +                               &queue->bulks[BULK_INDEX(queue->remote_notify)];
6828 +                       int msgtype = (bulk->dir == VCHIQ_BULK_TRANSMIT) ?
6829 +                               VCHIQ_MSG_BULK_RX_DONE : VCHIQ_MSG_BULK_TX_DONE;
6830 +                       int msgid = VCHIQ_MAKE_MSG(msgtype, service->localport,
6831 +                               service->remoteport);
6832 +                       VCHIQ_ELEMENT_T element = { &bulk->actual, 4 };
6833 +                       /* Only reply to non-dummy bulk requests */
6834 +                       if (bulk->remote_data) {
6835 +                               status = queue_message(service->state, NULL,
6836 +                                       msgid, &element, 1, 4, 0);
6837 +                               if (status != VCHIQ_SUCCESS)
6838 +                                       break;
6839 +                       }
6840 +                       queue->remote_notify++;
6841 +               }
6842 +       } else {
6843 +               queue->remote_notify = queue->process;
6844 +       }
6845 +
6846 +       if (status == VCHIQ_SUCCESS) {
6847 +               while (queue->remove != queue->remote_notify) {
6848 +                       VCHIQ_BULK_T *bulk =
6849 +                               &queue->bulks[BULK_INDEX(queue->remove)];
6850 +
6851 +                       /* Only generate callbacks for non-dummy bulk
6852 +                       ** requests, and non-terminated services */
6853 +                       if (bulk->data && service->instance) {
6854 +                               if (bulk->actual != VCHIQ_BULK_ACTUAL_ABORTED) {
6855 +                                       if (bulk->dir == VCHIQ_BULK_TRANSMIT) {
6856 +                                               VCHIQ_SERVICE_STATS_INC(service,
6857 +                                                       bulk_tx_count);
6858 +                                               VCHIQ_SERVICE_STATS_ADD(service,
6859 +                                                       bulk_tx_bytes,
6860 +                                                       bulk->actual);
6861 +                                       } else {
6862 +                                               VCHIQ_SERVICE_STATS_INC(service,
6863 +                                                       bulk_rx_count);
6864 +                                               VCHIQ_SERVICE_STATS_ADD(service,
6865 +                                                       bulk_rx_bytes,
6866 +                                                       bulk->actual);
6867 +                                       }
6868 +                               } else {
6869 +                                       VCHIQ_SERVICE_STATS_INC(service,
6870 +                                               bulk_aborted_count);
6871 +                               }
6872 +                               if (bulk->mode == VCHIQ_BULK_MODE_BLOCKING) {
6873 +                                       struct bulk_waiter *waiter;
6874 +                                       spin_lock(&bulk_waiter_spinlock);
6875 +                                       waiter = bulk->userdata;
6876 +                                       if (waiter) {
6877 +                                               waiter->actual = bulk->actual;
6878 +                                               up(&waiter->event);
6879 +                                       }
6880 +                                       spin_unlock(&bulk_waiter_spinlock);
6881 +                               } else if (bulk->mode ==
6882 +                                       VCHIQ_BULK_MODE_CALLBACK) {
6883 +                                       VCHIQ_REASON_T reason = (bulk->dir ==
6884 +                                               VCHIQ_BULK_TRANSMIT) ?
6885 +                                               ((bulk->actual ==
6886 +                                               VCHIQ_BULK_ACTUAL_ABORTED) ?
6887 +                                               VCHIQ_BULK_TRANSMIT_ABORTED :
6888 +                                               VCHIQ_BULK_TRANSMIT_DONE) :
6889 +                                               ((bulk->actual ==
6890 +                                               VCHIQ_BULK_ACTUAL_ABORTED) ?
6891 +                                               VCHIQ_BULK_RECEIVE_ABORTED :
6892 +                                               VCHIQ_BULK_RECEIVE_DONE);
6893 +                                       status = make_service_callback(service,
6894 +                                               reason, NULL, bulk->userdata);
6895 +                                       if (status == VCHIQ_RETRY)
6896 +                                               break;
6897 +                               }
6898 +                       }
6899 +
6900 +                       queue->remove++;
6901 +                       up(&service->bulk_remove_event);
6902 +               }
6903 +               if (!retry_poll)
6904 +                       status = VCHIQ_SUCCESS;
6905 +       }
6906 +
6907 +       if (status == VCHIQ_RETRY)
6908 +               request_poll(service->state, service,
6909 +                       (queue == &service->bulk_tx) ?
6910 +                       VCHIQ_POLL_TXNOTIFY : VCHIQ_POLL_RXNOTIFY);
6911 +
6912 +       return status;
6913 +}
6914 +
6915 +/* Called by the slot handler thread */
6916 +static void
6917 +poll_services(VCHIQ_STATE_T *state)
6918 +{
6919 +       int group, i;
6920 +
6921 +       for (group = 0; group < BITSET_SIZE(state->unused_service); group++) {
6922 +               uint32_t flags;
6923 +               flags = atomic_xchg(&state->poll_services[group], 0);
6924 +               for (i = 0; flags; i++) {
6925 +                       if (flags & (1 << i)) {
6926 +                               VCHIQ_SERVICE_T *service =
6927 +                                       find_service_by_port(state,
6928 +                                               (group<<5) + i);
6929 +                               uint32_t service_flags;
6930 +                               flags &= ~(1 << i);
6931 +                               if (!service)
6932 +                                       continue;
6933 +                               service_flags =
6934 +                                       atomic_xchg(&service->poll_flags, 0);
6935 +                               if (service_flags &
6936 +                                       (1 << VCHIQ_POLL_REMOVE)) {
6937 +                                       vchiq_log_info(vchiq_core_log_level,
6938 +                                               "%d: ps - remove %d<->%d",
6939 +                                               state->id, service->localport,
6940 +                                               service->remoteport);
6941 +
6942 +                                       /* Make it look like a client, because
6943 +                                          it must be removed and not left in
6944 +                                          the LISTENING state. */
6945 +                                       service->public_fourcc =
6946 +                                               VCHIQ_FOURCC_INVALID;
6947 +
6948 +                                       if (vchiq_close_service_internal(
6949 +                                               service, 0/*!close_recvd*/) !=
6950 +                                               VCHIQ_SUCCESS)
6951 +                                               request_poll(state, service,
6952 +                                                       VCHIQ_POLL_REMOVE);
6953 +                               } else if (service_flags &
6954 +                                       (1 << VCHIQ_POLL_TERMINATE)) {
6955 +                                       vchiq_log_info(vchiq_core_log_level,
6956 +                                               "%d: ps - terminate %d<->%d",
6957 +                                               state->id, service->localport,
6958 +                                               service->remoteport);
6959 +                                       if (vchiq_close_service_internal(
6960 +                                               service, 0/*!close_recvd*/) !=
6961 +                                               VCHIQ_SUCCESS)
6962 +                                               request_poll(state, service,
6963 +                                                       VCHIQ_POLL_TERMINATE);
6964 +                               }
6965 +                               if (service_flags & (1 << VCHIQ_POLL_TXNOTIFY))
6966 +                                       notify_bulks(service,
6967 +                                               &service->bulk_tx,
6968 +                                               1/*retry_poll*/);
6969 +                               if (service_flags & (1 << VCHIQ_POLL_RXNOTIFY))
6970 +                                       notify_bulks(service,
6971 +                                               &service->bulk_rx,
6972 +                                               1/*retry_poll*/);
6973 +                               unlock_service(service);
6974 +                       }
6975 +               }
6976 +       }
6977 +}
6978 +
6979 +/* Called by the slot handler or application threads, holding the bulk mutex. */
6980 +static int
6981 +resolve_bulks(VCHIQ_SERVICE_T *service, VCHIQ_BULK_QUEUE_T *queue)
6982 +{
6983 +       VCHIQ_STATE_T *state = service->state;
6984 +       int resolved = 0;
6985 +       int rc;
6986 +
6987 +       while ((queue->process != queue->local_insert) &&
6988 +               (queue->process != queue->remote_insert)) {
6989 +               VCHIQ_BULK_T *bulk = &queue->bulks[BULK_INDEX(queue->process)];
6990 +
6991 +               vchiq_log_trace(vchiq_core_log_level,
6992 +                       "%d: rb:%d %cx - li=%x ri=%x p=%x",
6993 +                       state->id, service->localport,
6994 +                       (queue == &service->bulk_tx) ? 't' : 'r',
6995 +                       queue->local_insert, queue->remote_insert,
6996 +                       queue->process);
6997 +
6998 +               WARN_ON(!((int)(queue->local_insert - queue->process) > 0));
6999 +               WARN_ON(!((int)(queue->remote_insert - queue->process) > 0));
7000 +
7001 +               rc = mutex_lock_interruptible(&state->bulk_transfer_mutex);
7002 +               if (rc != 0)
7003 +                       break;
7004 +
7005 +               vchiq_transfer_bulk(bulk);
7006 +               mutex_unlock(&state->bulk_transfer_mutex);
7007 +
7008 +               if (SRVTRACE_ENABLED(service, VCHIQ_LOG_INFO)) {
7009 +                       const char *header = (queue == &service->bulk_tx) ?
7010 +                               "Send Bulk to" : "Recv Bulk from";
7011 +                       if (bulk->actual != VCHIQ_BULK_ACTUAL_ABORTED)
7012 +                               vchiq_log_info(SRVTRACE_LEVEL(service),
7013 +                                       "%s %c%c%c%c d:%d len:%d %x<->%x",
7014 +                                       header,
7015 +                                       VCHIQ_FOURCC_AS_4CHARS(
7016 +                                               service->base.fourcc),
7017 +                                       service->remoteport,
7018 +                                       bulk->size,
7019 +                                       (unsigned int)bulk->data,
7020 +                                       (unsigned int)bulk->remote_data);
7021 +                       else
7022 +                               vchiq_log_info(SRVTRACE_LEVEL(service),
7023 +                                       "%s %c%c%c%c d:%d ABORTED - tx len:%d,"
7024 +                                       " rx len:%d %x<->%x",
7025 +                                       header,
7026 +                                       VCHIQ_FOURCC_AS_4CHARS(
7027 +                                               service->base.fourcc),
7028 +                                       service->remoteport,
7029 +                                       bulk->size,
7030 +                                       bulk->remote_size,
7031 +                                       (unsigned int)bulk->data,
7032 +                                       (unsigned int)bulk->remote_data);
7033 +               }
7034 +
7035 +               vchiq_complete_bulk(bulk);
7036 +               queue->process++;
7037 +               resolved++;
7038 +       }
7039 +       return resolved;
7040 +}
7041 +
7042 +/* Called with the bulk_mutex held */
7043 +static void
7044 +abort_outstanding_bulks(VCHIQ_SERVICE_T *service, VCHIQ_BULK_QUEUE_T *queue)
7045 +{
7046 +       int is_tx = (queue == &service->bulk_tx);
7047 +       vchiq_log_trace(vchiq_core_log_level,
7048 +               "%d: aob:%d %cx - li=%x ri=%x p=%x",
7049 +               service->state->id, service->localport, is_tx ? 't' : 'r',
7050 +               queue->local_insert, queue->remote_insert, queue->process);
7051 +
7052 +       WARN_ON(!((int)(queue->local_insert - queue->process) >= 0));
7053 +       WARN_ON(!((int)(queue->remote_insert - queue->process) >= 0));
7054 +
7055 +       while ((queue->process != queue->local_insert) ||
7056 +               (queue->process != queue->remote_insert)) {
7057 +               VCHIQ_BULK_T *bulk = &queue->bulks[BULK_INDEX(queue->process)];
7058 +
7059 +               if (queue->process == queue->remote_insert) {
7060 +                       /* fabricate a matching dummy bulk */
7061 +                       bulk->remote_data = NULL;
7062 +                       bulk->remote_size = 0;
7063 +                       queue->remote_insert++;
7064 +               }
7065 +
7066 +               if (queue->process != queue->local_insert) {
7067 +                       vchiq_complete_bulk(bulk);
7068 +
7069 +                       vchiq_log_info(SRVTRACE_LEVEL(service),
7070 +                               "%s %c%c%c%c d:%d ABORTED - tx len:%d, "
7071 +                               "rx len:%d",
7072 +                               is_tx ? "Send Bulk to" : "Recv Bulk from",
7073 +                               VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
7074 +                               service->remoteport,
7075 +                               bulk->size,
7076 +                               bulk->remote_size);
7077 +               } else {
7078 +                       /* fabricate a matching dummy bulk */
7079 +                       bulk->data = NULL;
7080 +                       bulk->size = 0;
7081 +                       bulk->actual = VCHIQ_BULK_ACTUAL_ABORTED;
7082 +                       bulk->dir = is_tx ? VCHIQ_BULK_TRANSMIT :
7083 +                               VCHIQ_BULK_RECEIVE;
7084 +                       queue->local_insert++;
7085 +               }
7086 +
7087 +               queue->process++;
7088 +       }
7089 +}
7090 +
7091 +/* Called from the slot handler thread */
7092 +static void
7093 +pause_bulks(VCHIQ_STATE_T *state)
7094 +{
7095 +       if (unlikely(atomic_inc_return(&pause_bulks_count) != 1)) {
7096 +               WARN_ON_ONCE(1);
7097 +               atomic_set(&pause_bulks_count, 1);
7098 +               return;
7099 +       }
7100 +
7101 +       /* Block bulk transfers from all services */
7102 +       mutex_lock(&state->bulk_transfer_mutex);
7103 +}
7104 +
7105 +/* Called from the slot handler thread */
7106 +static void
7107 +resume_bulks(VCHIQ_STATE_T *state)
7108 +{
7109 +       int i;
7110 +       if (unlikely(atomic_dec_return(&pause_bulks_count) != 0)) {
7111 +               WARN_ON_ONCE(1);
7112 +               atomic_set(&pause_bulks_count, 0);
7113 +               return;
7114 +       }
7115 +
7116 +       /* Allow bulk transfers from all services */
7117 +       mutex_unlock(&state->bulk_transfer_mutex);
7118 +
7119 +       if (state->deferred_bulks == 0)
7120 +               return;
7121 +
7122 +       /* Deal with any bulks which had to be deferred due to being in
7123 +        * paused state.  Don't try to match up to number of deferred bulks
7124 +        * in case we've had something come and close the service in the
7125 +        * interim - just process all bulk queues for all services */
7126 +       vchiq_log_info(vchiq_core_log_level, "%s: processing %d deferred bulks",
7127 +               __func__, state->deferred_bulks);
7128 +
7129 +       for (i = 0; i < state->unused_service; i++) {
7130 +               VCHIQ_SERVICE_T *service = state->services[i];
7131 +               int resolved_rx = 0;
7132 +               int resolved_tx = 0;
7133 +               if (!service || (service->srvstate != VCHIQ_SRVSTATE_OPEN))
7134 +                       continue;
7135 +
7136 +               mutex_lock(&service->bulk_mutex);
7137 +               resolved_rx = resolve_bulks(service, &service->bulk_rx);
7138 +               resolved_tx = resolve_bulks(service, &service->bulk_tx);
7139 +               mutex_unlock(&service->bulk_mutex);
7140 +               if (resolved_rx)
7141 +                       notify_bulks(service, &service->bulk_rx, 1);
7142 +               if (resolved_tx)
7143 +                       notify_bulks(service, &service->bulk_tx, 1);
7144 +       }
7145 +       state->deferred_bulks = 0;
7146 +}
7147 +
7148 +static int
7149 +parse_open(VCHIQ_STATE_T *state, VCHIQ_HEADER_T *header)
7150 +{
7151 +       VCHIQ_SERVICE_T *service = NULL;
7152 +       int msgid, size;
7153 +       int type;
7154 +       unsigned int localport, remoteport;
7155 +
7156 +       msgid = header->msgid;
7157 +       size = header->size;
7158 +       type = VCHIQ_MSG_TYPE(msgid);
7159 +       localport = VCHIQ_MSG_DSTPORT(msgid);
7160 +       remoteport = VCHIQ_MSG_SRCPORT(msgid);
7161 +       if (size >= sizeof(struct vchiq_open_payload)) {
7162 +               const struct vchiq_open_payload *payload =
7163 +                       (struct vchiq_open_payload *)header->data;
7164 +               unsigned int fourcc;
7165 +
7166 +               fourcc = payload->fourcc;
7167 +               vchiq_log_info(vchiq_core_log_level,
7168 +                       "%d: prs OPEN@%x (%d->'%c%c%c%c')",
7169 +                       state->id, (unsigned int)header,
7170 +                       localport,
7171 +                       VCHIQ_FOURCC_AS_4CHARS(fourcc));
7172 +
7173 +               service = get_listening_service(state, fourcc);
7174 +
7175 +               if (service) {
7176 +                       /* A matching service exists */
7177 +                       short version = payload->version;
7178 +                       short version_min = payload->version_min;
7179 +                       if ((service->version < version_min) ||
7180 +                               (version < service->version_min)) {
7181 +                               /* Version mismatch */
7182 +                               vchiq_loud_error_header();
7183 +                               vchiq_loud_error("%d: service %d (%c%c%c%c) "
7184 +                                       "version mismatch - local (%d, min %d)"
7185 +                                       " vs. remote (%d, min %d)",
7186 +                                       state->id, service->localport,
7187 +                                       VCHIQ_FOURCC_AS_4CHARS(fourcc),
7188 +                                       service->version, service->version_min,
7189 +                                       version, version_min);
7190 +                               vchiq_loud_error_footer();
7191 +                               unlock_service(service);
7192 +                               service = NULL;
7193 +                               goto fail_open;
7194 +                       }
7195 +                       service->peer_version = version;
7196 +
7197 +                       if (service->srvstate == VCHIQ_SRVSTATE_LISTENING) {
7198 +                               struct vchiq_openack_payload ack_payload = {
7199 +                                       service->version
7200 +                               };
7201 +                               VCHIQ_ELEMENT_T body = {
7202 +                                       &ack_payload,
7203 +                                       sizeof(ack_payload)
7204 +                               };
7205 +
7206 +                               /* Acknowledge the OPEN */
7207 +                               if (service->sync) {
7208 +                                       if (queue_message_sync(state, NULL,
7209 +                                               VCHIQ_MAKE_MSG(
7210 +                                                       VCHIQ_MSG_OPENACK,
7211 +                                                       service->localport,
7212 +                                                       remoteport),
7213 +                                               &body, 1, sizeof(ack_payload),
7214 +                                               0) == VCHIQ_RETRY)
7215 +                                               goto bail_not_ready;
7216 +                               } else {
7217 +                                       if (queue_message(state, NULL,
7218 +                                               VCHIQ_MAKE_MSG(
7219 +                                                       VCHIQ_MSG_OPENACK,
7220 +                                                       service->localport,
7221 +                                                       remoteport),
7222 +                                               &body, 1, sizeof(ack_payload),
7223 +                                               0) == VCHIQ_RETRY)
7224 +                                               goto bail_not_ready;
7225 +                               }
7226 +
7227 +                               /* The service is now open */
7228 +                               vchiq_set_service_state(service,
7229 +                                       service->sync ? VCHIQ_SRVSTATE_OPENSYNC
7230 +                                       : VCHIQ_SRVSTATE_OPEN);
7231 +                       }
7232 +
7233 +                       service->remoteport = remoteport;
7234 +                       service->client_id = ((int *)header->data)[1];
7235 +                       if (make_service_callback(service, VCHIQ_SERVICE_OPENED,
7236 +                               NULL, NULL) == VCHIQ_RETRY) {
7237 +                               /* Bail out if not ready */
7238 +                               service->remoteport = VCHIQ_PORT_FREE;
7239 +                               goto bail_not_ready;
7240 +                       }
7241 +
7242 +                       /* Success - the message has been dealt with */
7243 +                       unlock_service(service);
7244 +                       return 1;
7245 +               }
7246 +       }
7247 +
7248 +fail_open:
7249 +       /* No available service, or an invalid request - send a CLOSE */
7250 +       if (queue_message(state, NULL,
7251 +               VCHIQ_MAKE_MSG(VCHIQ_MSG_CLOSE, 0, VCHIQ_MSG_SRCPORT(msgid)),
7252 +               NULL, 0, 0, 0) == VCHIQ_RETRY)
7253 +               goto bail_not_ready;
7254 +
7255 +       return 1;
7256 +
7257 +bail_not_ready:
7258 +       if (service)
7259 +               unlock_service(service);
7260 +
7261 +       return 0;
7262 +}
7263 +
7264 +/* Called by the slot handler thread */
7265 +static void
7266 +parse_rx_slots(VCHIQ_STATE_T *state)
7267 +{
7268 +       VCHIQ_SHARED_STATE_T *remote = state->remote;
7269 +       VCHIQ_SERVICE_T *service = NULL;
7270 +       int tx_pos;
7271 +       DEBUG_INITIALISE(state->local)
7272 +
7273 +       tx_pos = remote->tx_pos;
7274 +
7275 +       while (state->rx_pos != tx_pos) {
7276 +               VCHIQ_HEADER_T *header;
7277 +               int msgid, size;
7278 +               int type;
7279 +               unsigned int localport, remoteport;
7280 +
7281 +               DEBUG_TRACE(PARSE_LINE);
7282 +               if (!state->rx_data) {
7283 +                       int rx_index;
7284 +                       WARN_ON(!((state->rx_pos & VCHIQ_SLOT_MASK) == 0));
7285 +                       rx_index = remote->slot_queue[
7286 +                               SLOT_QUEUE_INDEX_FROM_POS(state->rx_pos) &
7287 +                               VCHIQ_SLOT_QUEUE_MASK];
7288 +                       state->rx_data = (char *)SLOT_DATA_FROM_INDEX(state,
7289 +                               rx_index);
7290 +                       state->rx_info = SLOT_INFO_FROM_INDEX(state, rx_index);
7291 +
7292 +                       /* Initialise use_count to one, and increment
7293 +                       ** release_count at the end of the slot to avoid
7294 +                       ** releasing the slot prematurely. */
7295 +                       state->rx_info->use_count = 1;
7296 +                       state->rx_info->release_count = 0;
7297 +               }
7298 +
7299 +               header = (VCHIQ_HEADER_T *)(state->rx_data +
7300 +                       (state->rx_pos & VCHIQ_SLOT_MASK));
7301 +               DEBUG_VALUE(PARSE_HEADER, (int)header);
7302 +               msgid = header->msgid;
7303 +               DEBUG_VALUE(PARSE_MSGID, msgid);
7304 +               size = header->size;
7305 +               type = VCHIQ_MSG_TYPE(msgid);
7306 +               localport = VCHIQ_MSG_DSTPORT(msgid);
7307 +               remoteport = VCHIQ_MSG_SRCPORT(msgid);
7308 +
7309 +               if (type != VCHIQ_MSG_DATA)
7310 +                       VCHIQ_STATS_INC(state, ctrl_rx_count);
7311 +
7312 +               switch (type) {
7313 +               case VCHIQ_MSG_OPENACK:
7314 +               case VCHIQ_MSG_CLOSE:
7315 +               case VCHIQ_MSG_DATA:
7316 +               case VCHIQ_MSG_BULK_RX:
7317 +               case VCHIQ_MSG_BULK_TX:
7318 +               case VCHIQ_MSG_BULK_RX_DONE:
7319 +               case VCHIQ_MSG_BULK_TX_DONE:
7320 +                       service = find_service_by_port(state, localport);
7321 +                       if ((!service ||
7322 +                            ((service->remoteport != remoteport) &&
7323 +                             (service->remoteport != VCHIQ_PORT_FREE))) &&
7324 +                           (localport == 0) &&
7325 +                           (type == VCHIQ_MSG_CLOSE)) {
7326 +                               /* This could be a CLOSE from a client which
7327 +                                  hadn't yet received the OPENACK - look for
7328 +                                  the connected service */
7329 +                               if (service)
7330 +                                       unlock_service(service);
7331 +                               service = get_connected_service(state,
7332 +                                       remoteport);
7333 +                               if (service)
7334 +                                       vchiq_log_warning(vchiq_core_log_level,
7335 +                                               "%d: prs %s@%x (%d->%d) - "
7336 +                                               "found connected service %d",
7337 +                                               state->id, msg_type_str(type),
7338 +                                               (unsigned int)header,
7339 +                                               remoteport, localport,
7340 +                                               service->localport);
7341 +                       }
7342 +
7343 +                       if (!service) {
7344 +                               vchiq_log_error(vchiq_core_log_level,
7345 +                                       "%d: prs %s@%x (%d->%d) - "
7346 +                                       "invalid/closed service %d",
7347 +                                       state->id, msg_type_str(type),
7348 +                                       (unsigned int)header,
7349 +                                       remoteport, localport, localport);
7350 +                               goto skip_message;
7351 +                       }
7352 +                       break;
7353 +               default:
7354 +                       break;
7355 +               }
7356 +
7357 +               if (SRVTRACE_ENABLED(service, VCHIQ_LOG_INFO)) {
7358 +                       int svc_fourcc;
7359 +
7360 +                       svc_fourcc = service
7361 +                               ? service->base.fourcc
7362 +                               : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
7363 +                       vchiq_log_info(SRVTRACE_LEVEL(service),
7364 +                               "Rcvd Msg %s(%u) from %c%c%c%c s:%d d:%d "
7365 +                               "len:%d",
7366 +                               msg_type_str(type), type,
7367 +                               VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
7368 +                               remoteport, localport, size);
7369 +                       if (size > 0)
7370 +                               vchiq_log_dump_mem("Rcvd", 0, header->data,
7371 +                                       min(64, size));
7372 +               }
7373 +
7374 +               if (((unsigned int)header & VCHIQ_SLOT_MASK) + calc_stride(size)
7375 +                       > VCHIQ_SLOT_SIZE) {
7376 +                       vchiq_log_error(vchiq_core_log_level,
7377 +                               "header %x (msgid %x) - size %x too big for "
7378 +                               "slot",
7379 +                               (unsigned int)header, (unsigned int)msgid,
7380 +                               (unsigned int)size);
7381 +                       WARN(1, "oversized for slot\n");
7382 +               }
7383 +
7384 +               switch (type) {
7385 +               case VCHIQ_MSG_OPEN:
7386 +                       WARN_ON(!(VCHIQ_MSG_DSTPORT(msgid) == 0));
7387 +                       if (!parse_open(state, header))
7388 +                               goto bail_not_ready;
7389 +                       break;
7390 +               case VCHIQ_MSG_OPENACK:
7391 +                       if (size >= sizeof(struct vchiq_openack_payload)) {
7392 +                               const struct vchiq_openack_payload *payload =
7393 +                                       (struct vchiq_openack_payload *)
7394 +                                       header->data;
7395 +                               service->peer_version = payload->version;
7396 +                       }
7397 +                       vchiq_log_info(vchiq_core_log_level,
7398 +                               "%d: prs OPENACK@%x,%x (%d->%d) v:%d",
7399 +                               state->id, (unsigned int)header, size,
7400 +                               remoteport, localport, service->peer_version);
7401 +                       if (service->srvstate ==
7402 +                               VCHIQ_SRVSTATE_OPENING) {
7403 +                               service->remoteport = remoteport;
7404 +                               vchiq_set_service_state(service,
7405 +                                       VCHIQ_SRVSTATE_OPEN);
7406 +                               up(&service->remove_event);
7407 +                       } else
7408 +                               vchiq_log_error(vchiq_core_log_level,
7409 +                                       "OPENACK received in state %s",
7410 +                                       srvstate_names[service->srvstate]);
7411 +                       break;
7412 +               case VCHIQ_MSG_CLOSE:
7413 +                       WARN_ON(size != 0); /* There should be no data */
7414 +
7415 +                       vchiq_log_info(vchiq_core_log_level,
7416 +                               "%d: prs CLOSE@%x (%d->%d)",
7417 +                               state->id, (unsigned int)header,
7418 +                               remoteport, localport);
7419 +
7420 +                       mark_service_closing_internal(service, 1);
7421 +
7422 +                       if (vchiq_close_service_internal(service,
7423 +                               1/*close_recvd*/) == VCHIQ_RETRY)
7424 +                               goto bail_not_ready;
7425 +
7426 +                       vchiq_log_info(vchiq_core_log_level,
7427 +                               "Close Service %c%c%c%c s:%u d:%d",
7428 +                               VCHIQ_FOURCC_AS_4CHARS(service->base.fourcc),
7429 +                               service->localport,
7430 +                               service->remoteport);
7431 +                       break;
7432 +               case VCHIQ_MSG_DATA:
7433 +                       vchiq_log_trace(vchiq_core_log_level,
7434 +                               "%d: prs DATA@%x,%x (%d->%d)",
7435 +                               state->id, (unsigned int)header, size,
7436 +                               remoteport, localport);
7437 +
7438 +                       if ((service->remoteport == remoteport)
7439 +                               && (service->srvstate ==
7440 +                               VCHIQ_SRVSTATE_OPEN)) {
7441 +                               header->msgid = msgid | VCHIQ_MSGID_CLAIMED;
7442 +                               claim_slot(state->rx_info);
7443 +                               DEBUG_TRACE(PARSE_LINE);
7444 +                               if (make_service_callback(service,
7445 +                                       VCHIQ_MESSAGE_AVAILABLE, header,
7446 +                                       NULL) == VCHIQ_RETRY) {
7447 +                                       DEBUG_TRACE(PARSE_LINE);
7448 +                                       goto bail_not_ready;
7449 +                               }
7450 +                               VCHIQ_SERVICE_STATS_INC(service, ctrl_rx_count);
7451 +                               VCHIQ_SERVICE_STATS_ADD(service, ctrl_rx_bytes,
7452 +                                       size);
7453 +                       } else {
7454 +                               VCHIQ_STATS_INC(state, error_count);
7455 +                       }
7456 +                       break;
7457 +               case VCHIQ_MSG_CONNECT:
7458 +                       vchiq_log_info(vchiq_core_log_level,
7459 +                               "%d: prs CONNECT@%x",
7460 +                               state->id, (unsigned int)header);
7461 +                       up(&state->connect);
7462 +                       break;
7463 +               case VCHIQ_MSG_BULK_RX:
7464 +               case VCHIQ_MSG_BULK_TX: {
7465 +                       VCHIQ_BULK_QUEUE_T *queue;
7466 +                       WARN_ON(!state->is_master);
7467 +                       queue = (type == VCHIQ_MSG_BULK_RX) ?
7468 +                               &service->bulk_tx : &service->bulk_rx;
7469 +                       if ((service->remoteport == remoteport)
7470 +                               && (service->srvstate ==
7471 +                               VCHIQ_SRVSTATE_OPEN)) {
7472 +                               VCHIQ_BULK_T *bulk;
7473 +                               int resolved = 0;
7474 +
7475 +                               DEBUG_TRACE(PARSE_LINE);
7476 +                               if (mutex_lock_interruptible(
7477 +                                       &service->bulk_mutex) != 0) {
7478 +                                       DEBUG_TRACE(PARSE_LINE);
7479 +                                       goto bail_not_ready;
7480 +                               }
7481 +
7482 +                               WARN_ON(!(queue->remote_insert < queue->remove +
7483 +                                       VCHIQ_NUM_SERVICE_BULKS));
7484 +                               bulk = &queue->bulks[
7485 +                                       BULK_INDEX(queue->remote_insert)];
7486 +                               bulk->remote_data =
7487 +                                       (void *)((int *)header->data)[0];
7488 +                               bulk->remote_size = ((int *)header->data)[1];
7489 +                               wmb();
7490 +
7491 +                               vchiq_log_info(vchiq_core_log_level,
7492 +                                       "%d: prs %s@%x (%d->%d) %x@%x",
7493 +                                       state->id, msg_type_str(type),
7494 +                                       (unsigned int)header,
7495 +                                       remoteport, localport,
7496 +                                       bulk->remote_size,
7497 +                                       (unsigned int)bulk->remote_data);
7498 +
7499 +                               queue->remote_insert++;
7500 +
7501 +                               if (atomic_read(&pause_bulks_count)) {
7502 +                                       state->deferred_bulks++;
7503 +                                       vchiq_log_info(vchiq_core_log_level,
7504 +                                               "%s: deferring bulk (%d)",
7505 +                                               __func__,
7506 +                                               state->deferred_bulks);
7507 +                                       if (state->conn_state !=
7508 +                                               VCHIQ_CONNSTATE_PAUSE_SENT)
7509 +                                               vchiq_log_error(
7510 +                                                       vchiq_core_log_level,
7511 +                                                       "%s: bulks paused in "
7512 +                                                       "unexpected state %s",
7513 +                                                       __func__,
7514 +                                                       conn_state_names[
7515 +                                                       state->conn_state]);
7516 +                               } else if (state->conn_state ==
7517 +                                       VCHIQ_CONNSTATE_CONNECTED) {
7518 +                                       DEBUG_TRACE(PARSE_LINE);
7519 +                                       resolved = resolve_bulks(service,
7520 +                                               queue);
7521 +                               }
7522 +
7523 +                               mutex_unlock(&service->bulk_mutex);
7524 +                               if (resolved)
7525 +                                       notify_bulks(service, queue,
7526 +                                               1/*retry_poll*/);
7527 +                       }
7528 +               } break;
7529 +               case VCHIQ_MSG_BULK_RX_DONE:
7530 +               case VCHIQ_MSG_BULK_TX_DONE:
7531 +                       WARN_ON(state->is_master);
7532 +                       if ((service->remoteport == remoteport)
7533 +                               && (service->srvstate !=
7534 +                               VCHIQ_SRVSTATE_FREE)) {
7535 +                               VCHIQ_BULK_QUEUE_T *queue;
7536 +                               VCHIQ_BULK_T *bulk;
7537 +
7538 +                               queue = (type == VCHIQ_MSG_BULK_RX_DONE) ?
7539 +                                       &service->bulk_rx : &service->bulk_tx;
7540 +
7541 +                               DEBUG_TRACE(PARSE_LINE);
7542 +                               if (mutex_lock_interruptible(
7543 +                                       &service->bulk_mutex) != 0) {
7544 +                                       DEBUG_TRACE(PARSE_LINE);
7545 +                                       goto bail_not_ready;
7546 +                               }
7547 +                               if ((int)(queue->remote_insert -
7548 +                                       queue->local_insert) >= 0) {
7549 +                                       vchiq_log_error(vchiq_core_log_level,
7550 +                                               "%d: prs %s@%x (%d->%d) "
7551 +                                               "unexpected (ri=%d,li=%d)",
7552 +                                               state->id, msg_type_str(type),
7553 +                                               (unsigned int)header,
7554 +                                               remoteport, localport,
7555 +                                               queue->remote_insert,
7556 +                                               queue->local_insert);
7557 +                                       mutex_unlock(&service->bulk_mutex);
7558 +                                       break;
7559 +                               }
7560 +
7561 +                               BUG_ON(queue->process == queue->local_insert);
7562 +                               BUG_ON(queue->process != queue->remote_insert);
7563 +
7564 +                               bulk = &queue->bulks[
7565 +                                       BULK_INDEX(queue->remote_insert)];
7566 +                               bulk->actual = *(int *)header->data;
7567 +                               queue->remote_insert++;
7568 +
7569 +                               vchiq_log_info(vchiq_core_log_level,
7570 +                                       "%d: prs %s@%x (%d->%d) %x@%x",
7571 +                                       state->id, msg_type_str(type),
7572 +                                       (unsigned int)header,
7573 +                                       remoteport, localport,
7574 +                                       bulk->actual, (unsigned int)bulk->data);
7575 +
7576 +                               vchiq_log_trace(vchiq_core_log_level,
7577 +                                       "%d: prs:%d %cx li=%x ri=%x p=%x",
7578 +                                       state->id, localport,
7579 +                                       (type == VCHIQ_MSG_BULK_RX_DONE) ?
7580 +                                               'r' : 't',
7581 +                                       queue->local_insert,
7582 +                                       queue->remote_insert, queue->process);
7583 +
7584 +                               DEBUG_TRACE(PARSE_LINE);
7585 +                               WARN_ON(queue->process == queue->local_insert);
7586 +                               vchiq_complete_bulk(bulk);
7587 +                               queue->process++;
7588 +                               mutex_unlock(&service->bulk_mutex);
7589 +                               DEBUG_TRACE(PARSE_LINE);
7590 +                               notify_bulks(service, queue, 1/*retry_poll*/);
7591 +                               DEBUG_TRACE(PARSE_LINE);
7592 +                       }
7593 +                       break;
7594 +               case VCHIQ_MSG_PADDING:
7595 +                       vchiq_log_trace(vchiq_core_log_level,
7596 +                               "%d: prs PADDING@%x,%x",
7597 +                               state->id, (unsigned int)header, size);
7598 +                       break;
7599 +               case VCHIQ_MSG_PAUSE:
7600 +                       /* If initiated, signal the application thread */
7601 +                       vchiq_log_trace(vchiq_core_log_level,
7602 +                               "%d: prs PAUSE@%x,%x",
7603 +                               state->id, (unsigned int)header, size);
7604 +                       if (state->conn_state == VCHIQ_CONNSTATE_PAUSED) {
7605 +                               vchiq_log_error(vchiq_core_log_level,
7606 +                                       "%d: PAUSE received in state PAUSED",
7607 +                                       state->id);
7608 +                               break;
7609 +                       }
7610 +                       if (state->conn_state != VCHIQ_CONNSTATE_PAUSE_SENT) {
7611 +                               /* Send a PAUSE in response */
7612 +                               if (queue_message(state, NULL,
7613 +                                       VCHIQ_MAKE_MSG(VCHIQ_MSG_PAUSE, 0, 0),
7614 +                                       NULL, 0, 0, 0) == VCHIQ_RETRY)
7615 +                                       goto bail_not_ready;
7616 +                               if (state->is_master)
7617 +                                       pause_bulks(state);
7618 +                       }
7619 +                       /* At this point slot_mutex is held */
7620 +                       vchiq_set_conn_state(state, VCHIQ_CONNSTATE_PAUSED);
7621 +                       vchiq_platform_paused(state);
7622 +                       break;
7623 +               case VCHIQ_MSG_RESUME:
7624 +                       vchiq_log_trace(vchiq_core_log_level,
7625 +                               "%d: prs RESUME@%x,%x",
7626 +                               state->id, (unsigned int)header, size);
7627 +                       /* Release the slot mutex */
7628 +                       mutex_unlock(&state->slot_mutex);
7629 +                       if (state->is_master)
7630 +                               resume_bulks(state);
7631 +                       vchiq_set_conn_state(state, VCHIQ_CONNSTATE_CONNECTED);
7632 +                       vchiq_platform_resumed(state);
7633 +                       break;
7634 +
7635 +               case VCHIQ_MSG_REMOTE_USE:
7636 +                       vchiq_on_remote_use(state);
7637 +                       break;
7638 +               case VCHIQ_MSG_REMOTE_RELEASE:
7639 +                       vchiq_on_remote_release(state);
7640 +                       break;
7641 +               case VCHIQ_MSG_REMOTE_USE_ACTIVE:
7642 +                       vchiq_on_remote_use_active(state);
7643 +                       break;
7644 +
7645 +               default:
7646 +                       vchiq_log_error(vchiq_core_log_level,
7647 +                               "%d: prs invalid msgid %x@%x,%x",
7648 +                               state->id, msgid, (unsigned int)header, size);
7649 +                       WARN(1, "invalid message\n");
7650 +                       break;
7651 +               }
7652 +
7653 +skip_message:
7654 +               if (service) {
7655 +                       unlock_service(service);
7656 +                       service = NULL;
7657 +               }
7658 +
7659 +               state->rx_pos += calc_stride(size);
7660 +
7661 +               DEBUG_TRACE(PARSE_LINE);
7662 +               /* Perform some housekeeping when the end of the slot is
7663 +               ** reached. */
7664 +               if ((state->rx_pos & VCHIQ_SLOT_MASK) == 0) {
7665 +                       /* Remove the extra reference count. */
7666 +                       release_slot(state, state->rx_info, NULL, NULL);
7667 +                       state->rx_data = NULL;
7668 +               }
7669 +       }
7670 +
7671 +bail_not_ready:
7672 +       if (service)
7673 +               unlock_service(service);
7674 +}
7675 +
7676 +/* Called by the slot handler thread */
7677 +static int
7678 +slot_handler_func(void *v)
7679 +{
7680 +       VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
7681 +       VCHIQ_SHARED_STATE_T *local = state->local;
7682 +       DEBUG_INITIALISE(local)
7683 +
7684 +       while (1) {
7685 +               DEBUG_COUNT(SLOT_HANDLER_COUNT);
7686 +               DEBUG_TRACE(SLOT_HANDLER_LINE);
7687 +               remote_event_wait(&local->trigger);
7688 +
7689 +               rmb();
7690 +
7691 +               DEBUG_TRACE(SLOT_HANDLER_LINE);
7692 +               if (state->poll_needed) {
7693 +                       /* Check if we need to suspend - may change our
7694 +                        * conn_state */
7695 +                       vchiq_platform_check_suspend(state);
7696 +
7697 +                       state->poll_needed = 0;
7698 +
7699 +                       /* Handle service polling and other rare conditions here
7700 +                       ** out of the mainline code */
7701 +                       switch (state->conn_state) {
7702 +                       case VCHIQ_CONNSTATE_CONNECTED:
7703 +                               /* Poll the services as requested */
7704 +                               poll_services(state);
7705 +                               break;
7706 +
7707 +                       case VCHIQ_CONNSTATE_PAUSING:
7708 +                               if (state->is_master)
7709 +                                       pause_bulks(state);
7710 +                               if (queue_message(state, NULL,
7711 +                                       VCHIQ_MAKE_MSG(VCHIQ_MSG_PAUSE, 0, 0),
7712 +                                       NULL, 0, 0, 0) != VCHIQ_RETRY) {
7713 +                                       vchiq_set_conn_state(state,
7714 +                                               VCHIQ_CONNSTATE_PAUSE_SENT);
7715 +                               } else {
7716 +                                       if (state->is_master)
7717 +                                               resume_bulks(state);
7718 +                                       /* Retry later */
7719 +                                       state->poll_needed = 1;
7720 +                               }
7721 +                               break;
7722 +
7723 +                       case VCHIQ_CONNSTATE_PAUSED:
7724 +                               vchiq_platform_resume(state);
7725 +                               break;
7726 +
7727 +                       case VCHIQ_CONNSTATE_RESUMING:
7728 +                               if (queue_message(state, NULL,
7729 +                                       VCHIQ_MAKE_MSG(VCHIQ_MSG_RESUME, 0, 0),
7730 +                                       NULL, 0, 0, 0) != VCHIQ_RETRY) {
7731 +                                       if (state->is_master)
7732 +                                               resume_bulks(state);
7733 +                                       vchiq_set_conn_state(state,
7734 +                                               VCHIQ_CONNSTATE_CONNECTED);
7735 +                                       vchiq_platform_resumed(state);
7736 +                               } else {
7737 +                                       /* This should really be impossible,
7738 +                                       ** since the PAUSE should have flushed
7739 +                                       ** through outstanding messages. */
7740 +                                       vchiq_log_error(vchiq_core_log_level,
7741 +                                               "Failed to send RESUME "
7742 +                                               "message");
7743 +                                       BUG();
7744 +                               }
7745 +                               break;
7746 +
7747 +                       case VCHIQ_CONNSTATE_PAUSE_TIMEOUT:
7748 +                       case VCHIQ_CONNSTATE_RESUME_TIMEOUT:
7749 +                               vchiq_platform_handle_timeout(state);
7750 +                               break;
7751 +                       default:
7752 +                               break;
7753 +                       }
7754 +
7755 +
7756 +               }
7757 +
7758 +               DEBUG_TRACE(SLOT_HANDLER_LINE);
7759 +               parse_rx_slots(state);
7760 +       }
7761 +       return 0;
7762 +}
7763 +
7764 +
7765 +/* Called by the recycle thread */
7766 +static int
7767 +recycle_func(void *v)
7768 +{
7769 +       VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
7770 +       VCHIQ_SHARED_STATE_T *local = state->local;
7771 +
7772 +       while (1) {
7773 +               remote_event_wait(&local->recycle);
7774 +
7775 +               process_free_queue(state);
7776 +       }
7777 +       return 0;
7778 +}
7779 +
7780 +
7781 +/* Called by the sync thread */
7782 +static int
7783 +sync_func(void *v)
7784 +{
7785 +       VCHIQ_STATE_T *state = (VCHIQ_STATE_T *) v;
7786 +       VCHIQ_SHARED_STATE_T *local = state->local;
7787 +       VCHIQ_HEADER_T *header = (VCHIQ_HEADER_T *)SLOT_DATA_FROM_INDEX(state,
7788 +               state->remote->slot_sync);
7789 +
7790 +       while (1) {
7791 +               VCHIQ_SERVICE_T *service;
7792 +               int msgid, size;
7793 +               int type;
7794 +               unsigned int localport, remoteport;
7795 +
7796 +               remote_event_wait(&local->sync_trigger);
7797 +
7798 +               rmb();
7799 +
7800 +               msgid = header->msgid;
7801 +               size = header->size;
7802 +               type = VCHIQ_MSG_TYPE(msgid);
7803 +               localport = VCHIQ_MSG_DSTPORT(msgid);
7804 +               remoteport = VCHIQ_MSG_SRCPORT(msgid);
7805 +
7806 +               service = find_service_by_port(state, localport);
7807 +
7808 +               if (!service) {
7809 +                       vchiq_log_error(vchiq_sync_log_level,
7810 +                               "%d: sf %s@%x (%d->%d) - "
7811 +                               "invalid/closed service %d",
7812 +                               state->id, msg_type_str(type),
7813 +                               (unsigned int)header,
7814 +                               remoteport, localport, localport);
7815 +                       release_message_sync(state, header);
7816 +                       continue;
7817 +               }
7818 +
7819 +               if (vchiq_sync_log_level >= VCHIQ_LOG_TRACE) {
7820 +                       int svc_fourcc;
7821 +
7822 +                       svc_fourcc = service
7823 +                               ? service->base.fourcc
7824 +                               : VCHIQ_MAKE_FOURCC('?', '?', '?', '?');
7825 +                       vchiq_log_trace(vchiq_sync_log_level,
7826 +                               "Rcvd Msg %s from %c%c%c%c s:%d d:%d len:%d",
7827 +                               msg_type_str(type),
7828 +                               VCHIQ_FOURCC_AS_4CHARS(svc_fourcc),
7829 +                               remoteport, localport, size);
7830 +                       if (size > 0)
7831 +                               vchiq_log_dump_mem("Rcvd", 0, header->data,
7832 +                                       min(64, size));
7833 +               }
7834 +
7835 +               switch (type) {
7836 +               case VCHIQ_MSG_OPENACK:
7837 +                       if (size >= sizeof(struct vchiq_openack_payload)) {
7838 +                               const struct vchiq_openack_payload *payload =
7839 +                                       (struct vchiq_openack_payload *)
7840 +                                       header->data;
7841 +                               service->peer_version = payload->version;
7842 +                       }
7843 +                       vchiq_log_info(vchiq_sync_log_level,
7844 +                               "%d: sf OPENACK@%x,%x (%d->%d) v:%d",
7845 +                               state->id, (unsigned int)header, size,
7846 +                               remoteport, localport, service->peer_version);
7847 +                       if (service->srvstate == VCHIQ_SRVSTATE_OPENING) {
7848 +                               service->remoteport = remoteport;
7849 +                               vchiq_set_service_state(service,
7850 +                                       VCHIQ_SRVSTATE_OPENSYNC);
7851 +                               up(&service->remove_event);
7852 +                       }
7853 +                       release_message_sync(state, header);
7854 +                       break;
7855 +
7856 +               case VCHIQ_MSG_DATA:
7857 +                       vchiq_log_trace(vchiq_sync_log_level,
7858 +                               "%d: sf DATA@%x,%x (%d->%d)",
7859 +                               state->id, (unsigned int)header, size,
7860 +                               remoteport, localport);
7861 +
7862 +                       if ((service->remoteport == remoteport) &&
7863 +                               (service->srvstate ==
7864 +                               VCHIQ_SRVSTATE_OPENSYNC)) {
7865 +                               if (make_service_callback(service,
7866 +                                       VCHIQ_MESSAGE_AVAILABLE, header,
7867 +                                       NULL) == VCHIQ_RETRY)
7868 +                                       vchiq_log_error(vchiq_sync_log_level,
7869 +                                               "synchronous callback to "
7870 +                                               "service %d returns "
7871 +                                               "VCHIQ_RETRY",
7872 +                                               localport);
7873 +                       }
7874 +                       break;
7875 +
7876 +               default:
7877 +                       vchiq_log_error(vchiq_sync_log_level,
7878 +                               "%d: sf unexpected msgid %x@%x,%x",
7879 +                               state->id, msgid, (unsigned int)header, size);
7880 +                       release_message_sync(state, header);
7881 +                       break;
7882 +               }
7883 +
7884 +               unlock_service(service);
7885 +       }
7886 +
7887 +       return 0;
7888 +}
7889 +
7890 +
7891 +static void
7892 +init_bulk_queue(VCHIQ_BULK_QUEUE_T *queue)
7893 +{
7894 +       queue->local_insert = 0;
7895 +       queue->remote_insert = 0;
7896 +       queue->process = 0;
7897 +       queue->remote_notify = 0;
7898 +       queue->remove = 0;
7899 +}
7900 +
7901 +
7902 +inline const char *
7903 +get_conn_state_name(VCHIQ_CONNSTATE_T conn_state)
7904 +{
7905 +       return conn_state_names[conn_state];
7906 +}
7907 +
7908 +
7909 +VCHIQ_SLOT_ZERO_T *
7910 +vchiq_init_slots(void *mem_base, int mem_size)
7911 +{
7912 +       int mem_align = (VCHIQ_SLOT_SIZE - (int)mem_base) & VCHIQ_SLOT_MASK;
7913 +       VCHIQ_SLOT_ZERO_T *slot_zero =
7914 +               (VCHIQ_SLOT_ZERO_T *)((char *)mem_base + mem_align);
7915 +       int num_slots = (mem_size - mem_align)/VCHIQ_SLOT_SIZE;
7916 +       int first_data_slot = VCHIQ_SLOT_ZERO_SLOTS;
7917 +
7918 +       /* Ensure there is enough memory to run an absolutely minimum system */
7919 +       num_slots -= first_data_slot;
7920 +
7921 +       if (num_slots < 4) {
7922 +               vchiq_log_error(vchiq_core_log_level,
7923 +                       "vchiq_init_slots - insufficient memory %x bytes",
7924 +                       mem_size);
7925 +               return NULL;
7926 +       }
7927 +
7928 +       memset(slot_zero, 0, sizeof(VCHIQ_SLOT_ZERO_T));
7929 +
7930 +       slot_zero->magic = VCHIQ_MAGIC;
7931 +       slot_zero->version = VCHIQ_VERSION;
7932 +       slot_zero->version_min = VCHIQ_VERSION_MIN;
7933 +       slot_zero->slot_zero_size = sizeof(VCHIQ_SLOT_ZERO_T);
7934 +       slot_zero->slot_size = VCHIQ_SLOT_SIZE;
7935 +       slot_zero->max_slots = VCHIQ_MAX_SLOTS;
7936 +       slot_zero->max_slots_per_side = VCHIQ_MAX_SLOTS_PER_SIDE;
7937 +
7938 +       slot_zero->master.slot_sync = first_data_slot;
7939 +       slot_zero->master.slot_first = first_data_slot + 1;
7940 +       slot_zero->master.slot_last = first_data_slot + (num_slots/2) - 1;
7941 +       slot_zero->slave.slot_sync = first_data_slot + (num_slots/2);
7942 +       slot_zero->slave.slot_first = first_data_slot + (num_slots/2) + 1;
7943 +       slot_zero->slave.slot_last = first_data_slot + num_slots - 1;
7944 +
7945 +       return slot_zero;
7946 +}
7947 +
7948 +VCHIQ_STATUS_T
7949 +vchiq_init_state(VCHIQ_STATE_T *state, VCHIQ_SLOT_ZERO_T *slot_zero,
7950 +                int is_master)
7951 +{
7952 +       VCHIQ_SHARED_STATE_T *local;
7953 +       VCHIQ_SHARED_STATE_T *remote;
7954 +       VCHIQ_STATUS_T status;
7955 +       char threadname[10];
7956 +       static int id;
7957 +       int i;
7958 +
7959 +       vchiq_log_warning(vchiq_core_log_level,
7960 +               "%s: slot_zero = 0x%08lx, is_master = %d",
7961 +               __func__, (unsigned long)slot_zero, is_master);
7962 +
7963 +       /* Check the input configuration */
7964 +
7965 +       if (slot_zero->magic != VCHIQ_MAGIC) {
7966 +               vchiq_loud_error_header();
7967 +               vchiq_loud_error("Invalid VCHIQ magic value found.");
7968 +               vchiq_loud_error("slot_zero=%x: magic=%x (expected %x)",
7969 +                       (unsigned int)slot_zero, slot_zero->magic, VCHIQ_MAGIC);
7970 +               vchiq_loud_error_footer();
7971 +               return VCHIQ_ERROR;
7972 +       }
7973 +
7974 +       if (slot_zero->version < VCHIQ_VERSION_MIN) {
7975 +               vchiq_loud_error_header();
7976 +               vchiq_loud_error("Incompatible VCHIQ versions found.");
7977 +               vchiq_loud_error("slot_zero=%x: VideoCore version=%d "
7978 +                       "(minimum %d)",
7979 +                       (unsigned int)slot_zero, slot_zero->version,
7980 +                       VCHIQ_VERSION_MIN);
7981 +               vchiq_loud_error("Restart with a newer VideoCore image.");
7982 +               vchiq_loud_error_footer();
7983 +               return VCHIQ_ERROR;
7984 +       }
7985 +
7986 +       if (VCHIQ_VERSION < slot_zero->version_min) {
7987 +               vchiq_loud_error_header();
7988 +               vchiq_loud_error("Incompatible VCHIQ versions found.");
7989 +               vchiq_loud_error("slot_zero=%x: version=%d (VideoCore "
7990 +                       "minimum %d)",
7991 +                       (unsigned int)slot_zero, VCHIQ_VERSION,
7992 +                       slot_zero->version_min);
7993 +               vchiq_loud_error("Restart with a newer kernel.");
7994 +               vchiq_loud_error_footer();
7995 +               return VCHIQ_ERROR;
7996 +       }
7997 +
7998 +       if ((slot_zero->slot_zero_size != sizeof(VCHIQ_SLOT_ZERO_T)) ||
7999 +                (slot_zero->slot_size != VCHIQ_SLOT_SIZE) ||
8000 +                (slot_zero->max_slots != VCHIQ_MAX_SLOTS) ||
8001 +                (slot_zero->max_slots_per_side != VCHIQ_MAX_SLOTS_PER_SIDE)) {
8002 +               vchiq_loud_error_header();
8003 +               if (slot_zero->slot_zero_size != sizeof(VCHIQ_SLOT_ZERO_T))
8004 +                       vchiq_loud_error("slot_zero=%x: slot_zero_size=%x "
8005 +                               "(expected %x)",
8006 +                               (unsigned int)slot_zero,
8007 +                               slot_zero->slot_zero_size,
8008 +                               sizeof(VCHIQ_SLOT_ZERO_T));
8009 +               if (slot_zero->slot_size != VCHIQ_SLOT_SIZE)
8010 +                       vchiq_loud_error("slot_zero=%x: slot_size=%d "
8011 +                               "(expected %d",
8012 +                               (unsigned int)slot_zero, slot_zero->slot_size,
8013 +                               VCHIQ_SLOT_SIZE);
8014 +               if (slot_zero->max_slots != VCHIQ_MAX_SLOTS)
8015 +                       vchiq_loud_error("slot_zero=%x: max_slots=%d "
8016 +                               "(expected %d)",
8017 +                               (unsigned int)slot_zero, slot_zero->max_slots,
8018 +                               VCHIQ_MAX_SLOTS);
8019 +               if (slot_zero->max_slots_per_side != VCHIQ_MAX_SLOTS_PER_SIDE)
8020 +                       vchiq_loud_error("slot_zero=%x: max_slots_per_side=%d "
8021 +                               "(expected %d)",
8022 +                               (unsigned int)slot_zero,
8023 +                               slot_zero->max_slots_per_side,
8024 +                               VCHIQ_MAX_SLOTS_PER_SIDE);
8025 +               vchiq_loud_error_footer();
8026 +               return VCHIQ_ERROR;
8027 +       }
8028 +
8029 +       if (is_master) {
8030 +               local = &slot_zero->master;
8031 +               remote = &slot_zero->slave;
8032 +       } else {
8033 +               local = &slot_zero->slave;
8034 +               remote = &slot_zero->master;
8035 +       }
8036 +
8037 +       if (local->initialised) {
8038 +               vchiq_loud_error_header();
8039 +               if (remote->initialised)
8040 +                       vchiq_loud_error("local state has already been "
8041 +                               "initialised");
8042 +               else
8043 +                       vchiq_loud_error("master/slave mismatch - two %ss",
8044 +                               is_master ? "master" : "slave");
8045 +               vchiq_loud_error_footer();
8046 +               return VCHIQ_ERROR;
8047 +       }
8048 +
8049 +       memset(state, 0, sizeof(VCHIQ_STATE_T));
8050 +
8051 +       state->id = id++;
8052 +       state->is_master = is_master;
8053 +
8054 +       /*
8055 +               initialize shared state pointers
8056 +        */
8057 +
8058 +       state->local = local;
8059 +       state->remote = remote;
8060 +       state->slot_data = (VCHIQ_SLOT_T *)slot_zero;
8061 +
8062 +       /*
8063 +               initialize events and mutexes
8064 +        */
8065 +
8066 +       sema_init(&state->connect, 0);
8067 +       mutex_init(&state->mutex);
8068 +       sema_init(&state->trigger_event, 0);
8069 +       sema_init(&state->recycle_event, 0);
8070 +       sema_init(&state->sync_trigger_event, 0);
8071 +       sema_init(&state->sync_release_event, 0);
8072 +
8073 +       mutex_init(&state->slot_mutex);
8074 +       mutex_init(&state->recycle_mutex);
8075 +       mutex_init(&state->sync_mutex);
8076 +       mutex_init(&state->bulk_transfer_mutex);
8077 +
8078 +       sema_init(&state->slot_available_event, 0);
8079 +       sema_init(&state->slot_remove_event, 0);
8080 +       sema_init(&state->data_quota_event, 0);
8081 +
8082 +       state->slot_queue_available = 0;
8083 +
8084 +       for (i = 0; i < VCHIQ_MAX_SERVICES; i++) {
8085 +               VCHIQ_SERVICE_QUOTA_T *service_quota =
8086 +                       &state->service_quotas[i];
8087 +               sema_init(&service_quota->quota_event, 0);
8088 +       }
8089 +
8090 +       for (i = local->slot_first; i <= local->slot_last; i++) {
8091 +               local->slot_queue[state->slot_queue_available++] = i;
8092 +               up(&state->slot_available_event);
8093 +       }
8094 +
8095 +       state->default_slot_quota = state->slot_queue_available/2;
8096 +       state->default_message_quota =
8097 +               min((unsigned short)(state->default_slot_quota * 256),
8098 +               (unsigned short)~0);
8099 +
8100 +       state->previous_data_index = -1;
8101 +       state->data_use_count = 0;
8102 +       state->data_quota = state->slot_queue_available - 1;
8103 +
8104 +       local->trigger.event = &state->trigger_event;
8105 +       remote_event_create(&local->trigger);
8106 +       local->tx_pos = 0;
8107 +
8108 +       local->recycle.event = &state->recycle_event;
8109 +       remote_event_create(&local->recycle);
8110 +       local->slot_queue_recycle = state->slot_queue_available;
8111 +
8112 +       local->sync_trigger.event = &state->sync_trigger_event;
8113 +       remote_event_create(&local->sync_trigger);
8114 +
8115 +       local->sync_release.event = &state->sync_release_event;
8116 +       remote_event_create(&local->sync_release);
8117 +
8118 +       /* At start-of-day, the slot is empty and available */
8119 +       ((VCHIQ_HEADER_T *)SLOT_DATA_FROM_INDEX(state, local->slot_sync))->msgid
8120 +               = VCHIQ_MSGID_PADDING;
8121 +       remote_event_signal_local(&local->sync_release);
8122 +
8123 +       local->debug[DEBUG_ENTRIES] = DEBUG_MAX;
8124 +
8125 +       status = vchiq_platform_init_state(state);
8126 +
8127 +       /*
8128 +               bring up slot handler thread
8129 +        */
8130 +       snprintf(threadname, sizeof(threadname), "VCHIQ-%d", state->id);
8131 +       state->slot_handler_thread = kthread_create(&slot_handler_func,
8132 +               (void *)state,
8133 +               threadname);
8134 +
8135 +       if (state->slot_handler_thread == NULL) {
8136 +               vchiq_loud_error_header();
8137 +               vchiq_loud_error("couldn't create thread %s", threadname);
8138 +               vchiq_loud_error_footer();
8139 +               return VCHIQ_ERROR;
8140 +       }
8141 +       set_user_nice(state->slot_handler_thread, -19);
8142 +       wake_up_process(state->slot_handler_thread);
8143 +
8144 +       snprintf(threadname, sizeof(threadname), "VCHIQr-%d", state->id);
8145 +       state->recycle_thread = kthread_create(&recycle_func,
8146 +               (void *)state,
8147 +               threadname);
8148 +       if (state->recycle_thread == NULL) {
8149 +               vchiq_loud_error_header();
8150 +               vchiq_loud_error("couldn't create thread %s", threadname);
8151 +               vchiq_loud_error_footer();
8152 +               return VCHIQ_ERROR;
8153 +       }
8154 +       set_user_nice(state->recycle_thread, -19);
8155 +       wake_up_process(state->recycle_thread);
8156 +
8157 +       snprintf(threadname, sizeof(threadname), "VCHIQs-%d", state->id);
8158 +       state->sync_thread = kthread_create(&sync_func,
8159 +               (void *)state,
8160 +               threadname);
8161 +       if (state->sync_thread == NULL) {
8162 +               vchiq_loud_error_header();
8163 +               vchiq_loud_error("couldn't create thread %s", threadname);
8164 +               vchiq_loud_error_footer();
8165 +               return VCHIQ_ERROR;
8166 +       }
8167 +       set_user_nice(state->sync_thread, -20);
8168 +       wake_up_process(state->sync_thread);
8169 +
8170 +       BUG_ON(state->id >= VCHIQ_MAX_STATES);
8171 +       vchiq_states[state->id] = state;
8172 +
8173 +       /* Indicate readiness to the other side */
8174 +       local->initialised = 1;
8175 +
8176 +       return status;
8177 +}
8178 +
8179 +/* Called from application thread when a client or server service is created. */
8180 +VCHIQ_SERVICE_T *
8181 +vchiq_add_service_internal(VCHIQ_STATE_T *state,
8182 +       const VCHIQ_SERVICE_PARAMS_T *params, int srvstate,
8183 +       VCHIQ_INSTANCE_T instance, VCHIQ_USERDATA_TERM_T userdata_term)
8184 +{
8185 +       VCHIQ_SERVICE_T *service;
8186 +
8187 +       service = kmalloc(sizeof(VCHIQ_SERVICE_T), GFP_KERNEL);
8188 +       if (service) {
8189 +               service->base.fourcc   = params->fourcc;
8190 +               service->base.callback = params->callback;
8191 +               service->base.userdata = params->userdata;
8192 +               service->handle        = VCHIQ_SERVICE_HANDLE_INVALID;
8193 +               service->ref_count     = 1;
8194 +               service->srvstate      = VCHIQ_SRVSTATE_FREE;
8195 +               service->userdata_term = userdata_term;
8196 +               service->localport     = VCHIQ_PORT_FREE;
8197 +               service->remoteport    = VCHIQ_PORT_FREE;
8198 +
8199 +               service->public_fourcc = (srvstate == VCHIQ_SRVSTATE_OPENING) ?
8200 +                       VCHIQ_FOURCC_INVALID : params->fourcc;
8201 +               service->client_id     = 0;
8202 +               service->auto_close    = 1;
8203 +               service->sync          = 0;
8204 +               service->closing       = 0;
8205 +               service->trace         = 0;
8206 +               atomic_set(&service->poll_flags, 0);
8207 +               service->version       = params->version;
8208 +               service->version_min   = params->version_min;
8209 +               service->state         = state;
8210 +               service->instance      = instance;
8211 +               service->service_use_count = 0;
8212 +               init_bulk_queue(&service->bulk_tx);
8213 +               init_bulk_queue(&service->bulk_rx);
8214 +               sema_init(&service->remove_event, 0);
8215 +               sema_init(&service->bulk_remove_event, 0);
8216 +               mutex_init(&service->bulk_mutex);
8217 +               memset(&service->stats, 0, sizeof(service->stats));
8218 +       } else {
8219 +               vchiq_log_error(vchiq_core_log_level,
8220 +                       "Out of memory");
8221 +       }
8222 +
8223 +       if (service) {
8224 +               VCHIQ_SERVICE_T **pservice = NULL;
8225 +               int i;
8226 +
8227 +               /* Although it is perfectly possible to use service_spinlock
8228 +               ** to protect the creation of services, it is overkill as it
8229 +               ** disables interrupts while the array is searched.
8230 +               ** The only danger is of another thread trying to create a
8231 +               ** service - service deletion is safe.
8232 +               ** Therefore it is preferable to use state->mutex which,
8233 +               ** although slower to claim, doesn't block interrupts while
8234 +               ** it is held.
8235 +               */
8236 +
8237 +               mutex_lock(&state->mutex);
8238 +
8239 +               /* Prepare to use a previously unused service */
8240 +               if (state->unused_service < VCHIQ_MAX_SERVICES)
8241 +                       pservice = &state->services[state->unused_service];
8242 +
8243 +               if (srvstate == VCHIQ_SRVSTATE_OPENING) {
8244 +                       for (i = 0; i < state->unused_service; i++) {
8245 +                               VCHIQ_SERVICE_T *srv = state->services[i];
8246 +                               if (!srv) {
8247 +                                       pservice = &state->services[i];
8248 +                                       break;
8249 +                               }
8250 +                       }
8251 +               } else {
8252 +                       for (i = (state->unused_service - 1); i >= 0; i--) {
8253 +                               VCHIQ_SERVICE_T *srv = state->services[i];
8254 +                               if (!srv)
8255 +                                       pservice = &state->services[i];
8256 +                               else if ((srv->public_fourcc == params->fourcc)
8257 +                                       && ((srv->instance != instance) ||
8258 +                                       (srv->base.callback !=
8259 +                                       params->callback))) {
8260 +                                       /* There is another server using this
8261 +                                       ** fourcc which doesn't match. */
8262 +                                       pservice = NULL;
8263 +                                       break;
8264 +                               }
8265 +                       }
8266 +               }
8267 +
8268 +               if (pservice) {
8269 +                       service->localport = (pservice - state->services);
8270 +                       if (!handle_seq)
8271 +                               handle_seq = VCHIQ_MAX_STATES *
8272 +                                        VCHIQ_MAX_SERVICES;
8273 +                       service->handle = handle_seq |
8274 +                               (state->id * VCHIQ_MAX_SERVICES) |
8275 +                               service->localport;
8276 +                       handle_seq += VCHIQ_MAX_STATES * VCHIQ_MAX_SERVICES;
8277 +                       *pservice = service;
8278 +                       if (pservice == &state->services[state->unused_service])
8279 +                               state->unused_service++;
8280 +               }
8281 +
8282 +               mutex_unlock(&state->mutex);
8283 +
8284 +               if (!pservice) {
8285 +                       kfree(service);
8286 +                       service = NULL;
8287 +               }
8288 +       }
8289 +
8290 +       if (service) {
8291 +               VCHIQ_SERVICE_QUOTA_T *service_quota =
8292 +                       &state->service_quotas[service->localport];
8293 +               service_quota->slot_quota = state->default_slot_quota;
8294 +               service_quota->message_quota = state->default_message_quota;
8295 +               if (service_quota->slot_use_count == 0)
8296 +                       service_quota->previous_tx_index =
8297 +                               SLOT_QUEUE_INDEX_FROM_POS(state->local_tx_pos)
8298 +                               - 1;
8299 +
8300 +               /* Bring this service online */
8301 +               vchiq_set_service_state(service, srvstate);
8302 +
8303 +               vchiq_log_info(vchiq_core_msg_log_level,
8304 +                       "%s Service %c%c%c%c SrcPort:%d",
8305 +                       (srvstate == VCHIQ_SRVSTATE_OPENING)
8306 +                       ? "Open" : "Add",
8307 +                       VCHIQ_FOURCC_AS_4CHARS(params->fourcc),
8308 +                       service->localport);
8309 +       }
8310 +
8311 +       /* Don't unlock the service - leave it with a ref_count of 1. */
8312 +
8313 +       return service;
8314 +}
8315 +
8316 +VCHIQ_STATUS_T
8317 +vchiq_open_service_internal(VCHIQ_SERVICE_T *service, int client_id)
8318 +{
8319 +       struct vchiq_open_payload payload = {
8320 +               service->base.fourcc,
8321 +               client_id,
8322 +               service->version,
8323 +               service->version_min
8324 +       };
8325 +       VCHIQ_ELEMENT_T body = { &payload, sizeof(payload) };
8326 +       VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
8327 +
8328 +       service->client_id = client_id;
8329 +       vchiq_use_service_internal(service);
8330 +       status = queue_message(service->state, NULL,
8331 +               VCHIQ_MAKE_MSG(VCHIQ_MSG_OPEN, service->localport, 0),
8332 +               &body, 1, sizeof(payload), 1);
8333 +       if (status == VCHIQ_SUCCESS) {
8334 +               /* Wait for the ACK/NAK */
8335 +               if (down_interruptible(&service->remove_event) != 0) {
8336 +                       status = VCHIQ_RETRY;
8337 +                       vchiq_release_service_internal(service);
8338 +               } else if ((service->srvstate != VCHIQ_SRVSTATE_OPEN) &&
8339 +                       (service->srvstate != VCHIQ_SRVSTATE_OPENSYNC)) {
8340 +                       if (service->srvstate != VCHIQ_SRVSTATE_CLOSEWAIT)
8341 +                               vchiq_log_error(vchiq_core_log_level,
8342 +                                       "%d: osi - srvstate = %s (ref %d)",
8343 +                                       service->state->id,
8344 +                                       srvstate_names[service->srvstate],
8345 +                                       service->ref_count);
8346 +                       status = VCHIQ_ERROR;
8347 +                       VCHIQ_SERVICE_STATS_INC(service, error_count);
8348 +                       vchiq_release_service_internal(service);
8349 +               }
8350 +       }
8351 +       return status;
8352 +}
8353 +
8354 +static void
8355 +release_service_messages(VCHIQ_SERVICE_T *service)
8356 +{
8357 +       VCHIQ_STATE_T *state = service->state;
8358 +       int slot_last = state->remote->slot_last;
8359 +       int i;
8360 +
8361 +       /* Release any claimed messages */
8362 +       for (i = state->remote->slot_first; i <= slot_last; i++) {
8363 +               VCHIQ_SLOT_INFO_T *slot_info =
8364 +                       SLOT_INFO_FROM_INDEX(state, i);
8365 +               if (slot_info->release_count != slot_info->use_count) {
8366 +                       char *data =
8367 +                               (char *)SLOT_DATA_FROM_INDEX(state, i);
8368 +                       unsigned int pos, end;
8369 +
8370 +                       end = VCHIQ_SLOT_SIZE;
8371 +                       if (data == state->rx_data)
8372 +                               /* This buffer is still being read from - stop
8373 +                               ** at the current read position */
8374 +                               end = state->rx_pos & VCHIQ_SLOT_MASK;
8375 +
8376 +                       pos = 0;
8377 +
8378 +                       while (pos < end) {
8379 +                               VCHIQ_HEADER_T *header =
8380 +                                       (VCHIQ_HEADER_T *)(data + pos);
8381 +                               int msgid = header->msgid;
8382 +                               int port = VCHIQ_MSG_DSTPORT(msgid);
8383 +                               if ((port == service->localport) &&
8384 +                                       (msgid & VCHIQ_MSGID_CLAIMED)) {
8385 +                                       vchiq_log_info(vchiq_core_log_level,
8386 +                                               "  fsi - hdr %x",
8387 +                                               (unsigned int)header);
8388 +                                       release_slot(state, slot_info, header,
8389 +                                               NULL);
8390 +                               }
8391 +                               pos += calc_stride(header->size);
8392 +                               if (pos > VCHIQ_SLOT_SIZE) {
8393 +                                       vchiq_log_error(vchiq_core_log_level,
8394 +                                               "fsi - pos %x: header %x, "
8395 +                                               "msgid %x, header->msgid %x, "
8396 +                                               "header->size %x",
8397 +                                               pos, (unsigned int)header,
8398 +                                               msgid, header->msgid,
8399 +                                               header->size);
8400 +                                       WARN(1, "invalid slot position\n");
8401 +                               }
8402 +                       }
8403 +               }
8404 +       }
8405 +}
8406 +
8407 +static int
8408 +do_abort_bulks(VCHIQ_SERVICE_T *service)
8409 +{
8410 +       VCHIQ_STATUS_T status;
8411 +
8412 +       /* Abort any outstanding bulk transfers */
8413 +       if (mutex_lock_interruptible(&service->bulk_mutex) != 0)
8414 +               return 0;
8415 +       abort_outstanding_bulks(service, &service->bulk_tx);
8416 +       abort_outstanding_bulks(service, &service->bulk_rx);
8417 +       mutex_unlock(&service->bulk_mutex);
8418 +
8419 +       status = notify_bulks(service, &service->bulk_tx, 0/*!retry_poll*/);
8420 +       if (status == VCHIQ_SUCCESS)
8421 +               status = notify_bulks(service, &service->bulk_rx,
8422 +                       0/*!retry_poll*/);
8423 +       return (status == VCHIQ_SUCCESS);
8424 +}
8425 +
8426 +static VCHIQ_STATUS_T
8427 +close_service_complete(VCHIQ_SERVICE_T *service, int failstate)
8428 +{
8429 +       VCHIQ_STATUS_T status;
8430 +       int is_server = (service->public_fourcc != VCHIQ_FOURCC_INVALID);
8431 +       int newstate;
8432 +
8433 +       switch (service->srvstate) {
8434 +       case VCHIQ_SRVSTATE_OPEN:
8435 +       case VCHIQ_SRVSTATE_CLOSESENT:
8436 +       case VCHIQ_SRVSTATE_CLOSERECVD:
8437 +               if (is_server) {
8438 +                       if (service->auto_close) {
8439 +                               service->client_id = 0;
8440 +                               service->remoteport = VCHIQ_PORT_FREE;
8441 +                               newstate = VCHIQ_SRVSTATE_LISTENING;
8442 +                       } else
8443 +                               newstate = VCHIQ_SRVSTATE_CLOSEWAIT;
8444 +               } else
8445 +                       newstate = VCHIQ_SRVSTATE_CLOSED;
8446 +               vchiq_set_service_state(service, newstate);
8447 +               break;
8448 +       case VCHIQ_SRVSTATE_LISTENING:
8449 +               break;
8450 +       default:
8451 +               vchiq_log_error(vchiq_core_log_level,
8452 +                       "close_service_complete(%x) called in state %s",
8453 +                       service->handle, srvstate_names[service->srvstate]);
8454 +               WARN(1, "close_service_complete in unexpected state\n");
8455 +               return VCHIQ_ERROR;
8456 +       }
8457 +
8458 +       status = make_service_callback(service,
8459 +               VCHIQ_SERVICE_CLOSED, NULL, NULL);
8460 +
8461 +       if (status != VCHIQ_RETRY) {
8462 +               int uc = service->service_use_count;
8463 +               int i;
8464 +               /* Complete the close process */
8465 +               for (i = 0; i < uc; i++)
8466 +                       /* cater for cases where close is forced and the
8467 +                       ** client may not close all it's handles */
8468 +                       vchiq_release_service_internal(service);
8469 +
8470 +               service->client_id = 0;
8471 +               service->remoteport = VCHIQ_PORT_FREE;
8472 +
8473 +               if (service->srvstate == VCHIQ_SRVSTATE_CLOSED)
8474 +                       vchiq_free_service_internal(service);
8475 +               else if (service->srvstate != VCHIQ_SRVSTATE_CLOSEWAIT) {
8476 +                       if (is_server)
8477 +                               service->closing = 0;
8478 +
8479 +                       up(&service->remove_event);
8480 +               }
8481 +       } else
8482 +               vchiq_set_service_state(service, failstate);
8483 +
8484 +       return status;
8485 +}
8486 +
8487 +/* Called by the slot handler */
8488 +VCHIQ_STATUS_T
8489 +vchiq_close_service_internal(VCHIQ_SERVICE_T *service, int close_recvd)
8490 +{
8491 +       VCHIQ_STATE_T *state = service->state;
8492 +       VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
8493 +       int is_server = (service->public_fourcc != VCHIQ_FOURCC_INVALID);
8494 +
8495 +       vchiq_log_info(vchiq_core_log_level, "%d: csi:%d,%d (%s)",
8496 +               service->state->id, service->localport, close_recvd,
8497 +               srvstate_names[service->srvstate]);
8498 +
8499 +       switch (service->srvstate) {
8500 +       case VCHIQ_SRVSTATE_CLOSED:
8501 +       case VCHIQ_SRVSTATE_HIDDEN:
8502 +       case VCHIQ_SRVSTATE_LISTENING:
8503 +       case VCHIQ_SRVSTATE_CLOSEWAIT:
8504 +               if (close_recvd)
8505 +                       vchiq_log_error(vchiq_core_log_level,
8506 +                               "vchiq_close_service_internal(1) called "
8507 +                               "in state %s",
8508 +                               srvstate_names[service->srvstate]);
8509 +               else if (is_server) {
8510 +                       if (service->srvstate == VCHIQ_SRVSTATE_LISTENING) {
8511 +                               status = VCHIQ_ERROR;
8512 +                       } else {
8513 +                               service->client_id = 0;
8514 +                               service->remoteport = VCHIQ_PORT_FREE;
8515 +                               if (service->srvstate ==
8516 +                                       VCHIQ_SRVSTATE_CLOSEWAIT)
8517 +                                       vchiq_set_service_state(service,
8518 +                                               VCHIQ_SRVSTATE_LISTENING);
8519 +                       }
8520 +                       up(&service->remove_event);
8521 +               } else
8522 +                       vchiq_free_service_internal(service);
8523 +               break;
8524 +       case VCHIQ_SRVSTATE_OPENING:
8525 +               if (close_recvd) {
8526 +                       /* The open was rejected - tell the user */
8527 +                       vchiq_set_service_state(service,
8528 +                               VCHIQ_SRVSTATE_CLOSEWAIT);
8529 +                       up(&service->remove_event);
8530 +               } else {
8531 +                       /* Shutdown mid-open - let the other side know */
8532 +                       status = queue_message(state, service,
8533 +                               VCHIQ_MAKE_MSG
8534 +                               (VCHIQ_MSG_CLOSE,
8535 +                               service->localport,
8536 +                               VCHIQ_MSG_DSTPORT(service->remoteport)),
8537 +                               NULL, 0, 0, 0);
8538 +               }
8539 +               break;
8540 +
8541 +       case VCHIQ_SRVSTATE_OPENSYNC:
8542 +               mutex_lock(&state->sync_mutex);
8543 +               /* Drop through */
8544 +
8545 +       case VCHIQ_SRVSTATE_OPEN:
8546 +               if (state->is_master || close_recvd) {
8547 +                       if (!do_abort_bulks(service))
8548 +                               status = VCHIQ_RETRY;
8549 +               }
8550 +
8551 +               release_service_messages(service);
8552 +
8553 +               if (status == VCHIQ_SUCCESS)
8554 +                       status = queue_message(state, service,
8555 +                               VCHIQ_MAKE_MSG
8556 +                               (VCHIQ_MSG_CLOSE,
8557 +                               service->localport,
8558 +                               VCHIQ_MSG_DSTPORT(service->remoteport)),
8559 +                               NULL, 0, 0, 0);
8560 +
8561 +               if (status == VCHIQ_SUCCESS) {
8562 +                       if (!close_recvd)
8563 +                               break;
8564 +               } else if (service->srvstate == VCHIQ_SRVSTATE_OPENSYNC) {
8565 +                       mutex_unlock(&state->sync_mutex);
8566 +                       break;
8567 +               } else
8568 +                       break;
8569 +
8570 +               status = close_service_complete(service,
8571 +                               VCHIQ_SRVSTATE_CLOSERECVD);
8572 +               break;
8573 +
8574 +       case VCHIQ_SRVSTATE_CLOSESENT:
8575 +               if (!close_recvd)
8576 +                       /* This happens when a process is killed mid-close */
8577 +                       break;
8578 +
8579 +               if (!state->is_master) {
8580 +                       if (!do_abort_bulks(service)) {
8581 +                               status = VCHIQ_RETRY;
8582 +                               break;
8583 +                       }
8584 +               }
8585 +
8586 +               if (status == VCHIQ_SUCCESS)
8587 +                       status = close_service_complete(service,
8588 +                               VCHIQ_SRVSTATE_CLOSERECVD);
8589 +               break;
8590 +
8591 +       case VCHIQ_SRVSTATE_CLOSERECVD:
8592 +               if (!close_recvd && is_server)
8593 +                       /* Force into LISTENING mode */
8594 +                       vchiq_set_service_state(service,
8595 +                               VCHIQ_SRVSTATE_LISTENING);
8596 +               status = close_service_complete(service,
8597 +                       VCHIQ_SRVSTATE_CLOSERECVD);
8598 +               break;
8599 +
8600 +       default:
8601 +               vchiq_log_error(vchiq_core_log_level,
8602 +                       "vchiq_close_service_internal(%d) called in state %s",
8603 +                       close_recvd, srvstate_names[service->srvstate]);
8604 +               break;
8605 +       }
8606 +
8607 +       return status;
8608 +}
8609 +
8610 +/* Called from the application process upon process death */
8611 +void
8612 +vchiq_terminate_service_internal(VCHIQ_SERVICE_T *service)
8613 +{
8614 +       VCHIQ_STATE_T *state = service->state;
8615 +
8616 +       vchiq_log_info(vchiq_core_log_level, "%d: tsi - (%d<->%d)",
8617 +               state->id, service->localport, service->remoteport);
8618 +
8619 +       mark_service_closing(service);
8620 +
8621 +       /* Mark the service for removal by the slot handler */
8622 +       request_poll(state, service, VCHIQ_POLL_REMOVE);
8623 +}
8624 +
8625 +/* Called from the slot handler */
8626 +void
8627 +vchiq_free_service_internal(VCHIQ_SERVICE_T *service)
8628 +{
8629 +       VCHIQ_STATE_T *state = service->state;
8630 +
8631 +       vchiq_log_info(vchiq_core_log_level, "%d: fsi - (%d)",
8632 +               state->id, service->localport);
8633 +
8634 +       switch (service->srvstate) {
8635 +       case VCHIQ_SRVSTATE_OPENING:
8636 +       case VCHIQ_SRVSTATE_CLOSED:
8637 +       case VCHIQ_SRVSTATE_HIDDEN:
8638 +       case VCHIQ_SRVSTATE_LISTENING:
8639 +       case VCHIQ_SRVSTATE_CLOSEWAIT:
8640 +               break;
8641 +       default:
8642 +               vchiq_log_error(vchiq_core_log_level,
8643 +                       "%d: fsi - (%d) in state %s",
8644 +                       state->id, service->localport,
8645 +                       srvstate_names[service->srvstate]);
8646 +               return;
8647 +       }
8648 +
8649 +       vchiq_set_service_state(service, VCHIQ_SRVSTATE_FREE);
8650 +
8651 +       up(&service->remove_event);
8652 +
8653 +       /* Release the initial lock */
8654 +       unlock_service(service);
8655 +}
8656 +
8657 +VCHIQ_STATUS_T
8658 +vchiq_connect_internal(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance)
8659 +{
8660 +       VCHIQ_SERVICE_T *service;
8661 +       int i;
8662 +
8663 +       /* Find all services registered to this client and enable them. */
8664 +       i = 0;
8665 +       while ((service = next_service_by_instance(state, instance,
8666 +               &i)) != NULL) {
8667 +               if (service->srvstate == VCHIQ_SRVSTATE_HIDDEN)
8668 +                       vchiq_set_service_state(service,
8669 +                               VCHIQ_SRVSTATE_LISTENING);
8670 +               unlock_service(service);
8671 +       }
8672 +
8673 +       if (state->conn_state == VCHIQ_CONNSTATE_DISCONNECTED) {
8674 +               if (queue_message(state, NULL,
8675 +                       VCHIQ_MAKE_MSG(VCHIQ_MSG_CONNECT, 0, 0), NULL, 0,
8676 +                       0, 1) == VCHIQ_RETRY)
8677 +                       return VCHIQ_RETRY;
8678 +
8679 +               vchiq_set_conn_state(state, VCHIQ_CONNSTATE_CONNECTING);
8680 +       }
8681 +
8682 +       if (state->conn_state == VCHIQ_CONNSTATE_CONNECTING) {
8683 +               if (down_interruptible(&state->connect) != 0)
8684 +                       return VCHIQ_RETRY;
8685 +
8686 +               vchiq_set_conn_state(state, VCHIQ_CONNSTATE_CONNECTED);
8687 +               up(&state->connect);
8688 +       }
8689 +
8690 +       return VCHIQ_SUCCESS;
8691 +}
8692 +
8693 +VCHIQ_STATUS_T
8694 +vchiq_shutdown_internal(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance)
8695 +{
8696 +       VCHIQ_SERVICE_T *service;
8697 +       int i;
8698 +
8699 +       /* Find all services registered to this client and enable them. */
8700 +       i = 0;
8701 +       while ((service = next_service_by_instance(state, instance,
8702 +               &i)) != NULL) {
8703 +               (void)vchiq_remove_service(service->handle);
8704 +               unlock_service(service);
8705 +       }
8706 +
8707 +       return VCHIQ_SUCCESS;
8708 +}
8709 +
8710 +VCHIQ_STATUS_T
8711 +vchiq_pause_internal(VCHIQ_STATE_T *state)
8712 +{
8713 +       VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
8714 +
8715 +       switch (state->conn_state) {
8716 +       case VCHIQ_CONNSTATE_CONNECTED:
8717 +               /* Request a pause */
8718 +               vchiq_set_conn_state(state, VCHIQ_CONNSTATE_PAUSING);
8719 +               request_poll(state, NULL, 0);
8720 +               break;
8721 +       default:
8722 +               vchiq_log_error(vchiq_core_log_level,
8723 +                       "vchiq_pause_internal in state %s\n",
8724 +                       conn_state_names[state->conn_state]);
8725 +               status = VCHIQ_ERROR;
8726 +               VCHIQ_STATS_INC(state, error_count);
8727 +               break;
8728 +       }
8729 +
8730 +       return status;
8731 +}
8732 +
8733 +VCHIQ_STATUS_T
8734 +vchiq_resume_internal(VCHIQ_STATE_T *state)
8735 +{
8736 +       VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
8737 +
8738 +       if (state->conn_state == VCHIQ_CONNSTATE_PAUSED) {
8739 +               vchiq_set_conn_state(state, VCHIQ_CONNSTATE_RESUMING);
8740 +               request_poll(state, NULL, 0);
8741 +       } else {
8742 +               status = VCHIQ_ERROR;
8743 +               VCHIQ_STATS_INC(state, error_count);
8744 +       }
8745 +
8746 +       return status;
8747 +}
8748 +
8749 +VCHIQ_STATUS_T
8750 +vchiq_close_service(VCHIQ_SERVICE_HANDLE_T handle)
8751 +{
8752 +       /* Unregister the service */
8753 +       VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
8754 +       VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
8755 +
8756 +       if (!service)
8757 +               return VCHIQ_ERROR;
8758 +
8759 +       vchiq_log_info(vchiq_core_log_level,
8760 +               "%d: close_service:%d",
8761 +               service->state->id, service->localport);
8762 +
8763 +       if ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
8764 +               (service->srvstate == VCHIQ_SRVSTATE_LISTENING) ||
8765 +               (service->srvstate == VCHIQ_SRVSTATE_HIDDEN)) {
8766 +               unlock_service(service);
8767 +               return VCHIQ_ERROR;
8768 +       }
8769 +
8770 +       mark_service_closing(service);
8771 +
8772 +       if (current == service->state->slot_handler_thread) {
8773 +               status = vchiq_close_service_internal(service,
8774 +                       0/*!close_recvd*/);
8775 +               BUG_ON(status == VCHIQ_RETRY);
8776 +       } else {
8777 +       /* Mark the service for termination by the slot handler */
8778 +               request_poll(service->state, service, VCHIQ_POLL_TERMINATE);
8779 +       }
8780 +
8781 +       while (1) {
8782 +               if (down_interruptible(&service->remove_event) != 0) {
8783 +                       status = VCHIQ_RETRY;
8784 +                       break;
8785 +               }
8786 +
8787 +               if ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
8788 +                       (service->srvstate == VCHIQ_SRVSTATE_LISTENING) ||
8789 +                       (service->srvstate == VCHIQ_SRVSTATE_OPEN))
8790 +                       break;
8791 +
8792 +               vchiq_log_warning(vchiq_core_log_level,
8793 +                       "%d: close_service:%d - waiting in state %s",
8794 +                       service->state->id, service->localport,
8795 +                       srvstate_names[service->srvstate]);
8796 +       }
8797 +
8798 +       if ((status == VCHIQ_SUCCESS) &&
8799 +               (service->srvstate != VCHIQ_SRVSTATE_FREE) &&
8800 +               (service->srvstate != VCHIQ_SRVSTATE_LISTENING))
8801 +               status = VCHIQ_ERROR;
8802 +
8803 +       unlock_service(service);
8804 +
8805 +       return status;
8806 +}
8807 +
8808 +VCHIQ_STATUS_T
8809 +vchiq_remove_service(VCHIQ_SERVICE_HANDLE_T handle)
8810 +{
8811 +       /* Unregister the service */
8812 +       VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
8813 +       VCHIQ_STATUS_T status = VCHIQ_SUCCESS;
8814 +
8815 +       if (!service)
8816 +               return VCHIQ_ERROR;
8817 +
8818 +       vchiq_log_info(vchiq_core_log_level,
8819 +               "%d: remove_service:%d",
8820 +               service->state->id, service->localport);
8821 +
8822 +       if (service->srvstate == VCHIQ_SRVSTATE_FREE) {
8823 +               unlock_service(service);
8824 +               return VCHIQ_ERROR;
8825 +       }
8826 +
8827 +       mark_service_closing(service);
8828 +
8829 +       if ((service->srvstate == VCHIQ_SRVSTATE_HIDDEN) ||
8830 +               (current == service->state->slot_handler_thread)) {
8831 +               /* Make it look like a client, because it must be removed and
8832 +                  not left in the LISTENING state. */
8833 +               service->public_fourcc = VCHIQ_FOURCC_INVALID;
8834 +
8835 +               status = vchiq_close_service_internal(service,
8836 +                       0/*!close_recvd*/);
8837 +               BUG_ON(status == VCHIQ_RETRY);
8838 +       } else {
8839 +               /* Mark the service for removal by the slot handler */
8840 +               request_poll(service->state, service, VCHIQ_POLL_REMOVE);
8841 +       }
8842 +       while (1) {
8843 +               if (down_interruptible(&service->remove_event) != 0) {
8844 +                       status = VCHIQ_RETRY;
8845 +                       break;
8846 +               }
8847 +
8848 +               if ((service->srvstate == VCHIQ_SRVSTATE_FREE) ||
8849 +                       (service->srvstate == VCHIQ_SRVSTATE_OPEN))
8850 +                       break;
8851 +
8852 +               vchiq_log_warning(vchiq_core_log_level,
8853 +                       "%d: remove_service:%d - waiting in state %s",
8854 +                       service->state->id, service->localport,
8855 +                       srvstate_names[service->srvstate]);
8856 +       }
8857 +
8858 +       if ((status == VCHIQ_SUCCESS) &&
8859 +               (service->srvstate != VCHIQ_SRVSTATE_FREE))
8860 +               status = VCHIQ_ERROR;
8861 +
8862 +       unlock_service(service);
8863 +
8864 +       return status;
8865 +}
8866 +
8867 +
8868 +/* This function may be called by kernel threads or user threads.
8869 + * User threads may receive VCHIQ_RETRY to indicate that a signal has been
8870 + * received and the call should be retried after being returned to user
8871 + * context.
8872 + * When called in blocking mode, the userdata field points to a bulk_waiter
8873 + * structure.
8874 + */
8875 +VCHIQ_STATUS_T
8876 +vchiq_bulk_transfer(VCHIQ_SERVICE_HANDLE_T handle,
8877 +       VCHI_MEM_HANDLE_T memhandle, void *offset, int size, void *userdata,
8878 +       VCHIQ_BULK_MODE_T mode, VCHIQ_BULK_DIR_T dir)
8879 +{
8880 +       VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
8881 +       VCHIQ_BULK_QUEUE_T *queue;
8882 +       VCHIQ_BULK_T *bulk;
8883 +       VCHIQ_STATE_T *state;
8884 +       struct bulk_waiter *bulk_waiter = NULL;
8885 +       const char dir_char = (dir == VCHIQ_BULK_TRANSMIT) ? 't' : 'r';
8886 +       const int dir_msgtype = (dir == VCHIQ_BULK_TRANSMIT) ?
8887 +               VCHIQ_MSG_BULK_TX : VCHIQ_MSG_BULK_RX;
8888 +       VCHIQ_STATUS_T status = VCHIQ_ERROR;
8889 +
8890 +       if (!service ||
8891 +                (service->srvstate != VCHIQ_SRVSTATE_OPEN) ||
8892 +                ((memhandle == VCHI_MEM_HANDLE_INVALID) && (offset == NULL)) ||
8893 +                (vchiq_check_service(service) != VCHIQ_SUCCESS))
8894 +               goto error_exit;
8895 +
8896 +       switch (mode) {
8897 +       case VCHIQ_BULK_MODE_NOCALLBACK:
8898 +       case VCHIQ_BULK_MODE_CALLBACK:
8899 +               break;
8900 +       case VCHIQ_BULK_MODE_BLOCKING:
8901 +               bulk_waiter = (struct bulk_waiter *)userdata;
8902 +               sema_init(&bulk_waiter->event, 0);
8903 +               bulk_waiter->actual = 0;
8904 +               bulk_waiter->bulk = NULL;
8905 +               break;
8906 +       case VCHIQ_BULK_MODE_WAITING:
8907 +               bulk_waiter = (struct bulk_waiter *)userdata;
8908 +               bulk = bulk_waiter->bulk;
8909 +               goto waiting;
8910 +       default:
8911 +               goto error_exit;
8912 +       }
8913 +
8914 +       state = service->state;
8915 +
8916 +       queue = (dir == VCHIQ_BULK_TRANSMIT) ?
8917 +               &service->bulk_tx : &service->bulk_rx;
8918 +
8919 +       if (mutex_lock_interruptible(&service->bulk_mutex) != 0) {
8920 +               status = VCHIQ_RETRY;
8921 +               goto error_exit;
8922 +       }
8923 +
8924 +       if (queue->local_insert == queue->remove + VCHIQ_NUM_SERVICE_BULKS) {
8925 +               VCHIQ_SERVICE_STATS_INC(service, bulk_stalls);
8926 +               do {
8927 +                       mutex_unlock(&service->bulk_mutex);
8928 +                       if (down_interruptible(&service->bulk_remove_event)
8929 +                               != 0) {
8930 +                               status = VCHIQ_RETRY;
8931 +                               goto error_exit;
8932 +                       }
8933 +                       if (mutex_lock_interruptible(&service->bulk_mutex)
8934 +                               != 0) {
8935 +                               status = VCHIQ_RETRY;
8936 +                               goto error_exit;
8937 +                       }
8938 +               } while (queue->local_insert == queue->remove +
8939 +                               VCHIQ_NUM_SERVICE_BULKS);
8940 +       }
8941 +
8942 +       bulk = &queue->bulks[BULK_INDEX(queue->local_insert)];
8943 +
8944 +       bulk->mode = mode;
8945 +       bulk->dir = dir;
8946 +       bulk->userdata = userdata;
8947 +       bulk->size = size;
8948 +       bulk->actual = VCHIQ_BULK_ACTUAL_ABORTED;
8949 +
8950 +       if (vchiq_prepare_bulk_data(bulk, memhandle, offset, size, dir) !=
8951 +               VCHIQ_SUCCESS)
8952 +               goto unlock_error_exit;
8953 +
8954 +       wmb();
8955 +
8956 +       vchiq_log_info(vchiq_core_log_level,
8957 +               "%d: bt (%d->%d) %cx %x@%x %x",
8958 +               state->id,
8959 +               service->localport, service->remoteport, dir_char,
8960 +               size, (unsigned int)bulk->data, (unsigned int)userdata);
8961 +
8962 +       if (state->is_master) {
8963 +               queue->local_insert++;
8964 +               if (resolve_bulks(service, queue))
8965 +                       request_poll(state, service,
8966 +                               (dir == VCHIQ_BULK_TRANSMIT) ?
8967 +                               VCHIQ_POLL_TXNOTIFY : VCHIQ_POLL_RXNOTIFY);
8968 +       } else {
8969 +               int payload[2] = { (int)bulk->data, bulk->size };
8970 +               VCHIQ_ELEMENT_T element = { payload, sizeof(payload) };
8971 +
8972 +               status = queue_message(state, NULL,
8973 +                       VCHIQ_MAKE_MSG(dir_msgtype,
8974 +                               service->localport, service->remoteport),
8975 +                       &element, 1, sizeof(payload), 1);
8976 +               if (status != VCHIQ_SUCCESS) {
8977 +                       vchiq_complete_bulk(bulk);
8978 +                       goto unlock_error_exit;
8979 +               }
8980 +               queue->local_insert++;
8981 +       }
8982 +
8983 +       mutex_unlock(&service->bulk_mutex);
8984 +
8985 +       vchiq_log_trace(vchiq_core_log_level,
8986 +               "%d: bt:%d %cx li=%x ri=%x p=%x",
8987 +               state->id,
8988 +               service->localport, dir_char,
8989 +               queue->local_insert, queue->remote_insert, queue->process);
8990 +
8991 +waiting:
8992 +       unlock_service(service);
8993 +
8994 +       status = VCHIQ_SUCCESS;
8995 +
8996 +       if (bulk_waiter) {
8997 +               bulk_waiter->bulk = bulk;
8998 +               if (down_interruptible(&bulk_waiter->event) != 0)
8999 +                       status = VCHIQ_RETRY;
9000 +               else if (bulk_waiter->actual == VCHIQ_BULK_ACTUAL_ABORTED)
9001 +                       status = VCHIQ_ERROR;
9002 +       }
9003 +
9004 +       return status;
9005 +
9006 +unlock_error_exit:
9007 +       mutex_unlock(&service->bulk_mutex);
9008 +
9009 +error_exit:
9010 +       if (service)
9011 +               unlock_service(service);
9012 +       return status;
9013 +}
9014 +
9015 +VCHIQ_STATUS_T
9016 +vchiq_queue_message(VCHIQ_SERVICE_HANDLE_T handle,
9017 +       const VCHIQ_ELEMENT_T *elements, unsigned int count)
9018 +{
9019 +       VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
9020 +       VCHIQ_STATUS_T status = VCHIQ_ERROR;
9021 +
9022 +       unsigned int size = 0;
9023 +       unsigned int i;
9024 +
9025 +       if (!service ||
9026 +               (vchiq_check_service(service) != VCHIQ_SUCCESS))
9027 +               goto error_exit;
9028 +
9029 +       for (i = 0; i < (unsigned int)count; i++) {
9030 +               if (elements[i].size) {
9031 +                       if (elements[i].data == NULL) {
9032 +                               VCHIQ_SERVICE_STATS_INC(service, error_count);
9033 +                               goto error_exit;
9034 +                       }
9035 +                       size += elements[i].size;
9036 +               }
9037 +       }
9038 +
9039 +       if (size > VCHIQ_MAX_MSG_SIZE) {
9040 +               VCHIQ_SERVICE_STATS_INC(service, error_count);
9041 +               goto error_exit;
9042 +       }
9043 +
9044 +       switch (service->srvstate) {
9045 +       case VCHIQ_SRVSTATE_OPEN:
9046 +               status = queue_message(service->state, service,
9047 +                               VCHIQ_MAKE_MSG(VCHIQ_MSG_DATA,
9048 +                                       service->localport,
9049 +                                       service->remoteport),
9050 +                               elements, count, size, 1);
9051 +               break;
9052 +       case VCHIQ_SRVSTATE_OPENSYNC:
9053 +               status = queue_message_sync(service->state, service,
9054 +                               VCHIQ_MAKE_MSG(VCHIQ_MSG_DATA,
9055 +                                       service->localport,
9056 +                                       service->remoteport),
9057 +                               elements, count, size, 1);
9058 +               break;
9059 +       default:
9060 +               status = VCHIQ_ERROR;
9061 +               break;
9062 +       }
9063 +
9064 +error_exit:
9065 +       if (service)
9066 +               unlock_service(service);
9067 +
9068 +       return status;
9069 +}
9070 +
9071 +void
9072 +vchiq_release_message(VCHIQ_SERVICE_HANDLE_T handle, VCHIQ_HEADER_T *header)
9073 +{
9074 +       VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
9075 +       VCHIQ_SHARED_STATE_T *remote;
9076 +       VCHIQ_STATE_T *state;
9077 +       int slot_index;
9078 +
9079 +       if (!service)
9080 +               return;
9081 +
9082 +       state = service->state;
9083 +       remote = state->remote;
9084 +
9085 +       slot_index = SLOT_INDEX_FROM_DATA(state, (void *)header);
9086 +
9087 +       if ((slot_index >= remote->slot_first) &&
9088 +               (slot_index <= remote->slot_last)) {
9089 +               int msgid = header->msgid;
9090 +               if (msgid & VCHIQ_MSGID_CLAIMED) {
9091 +                       VCHIQ_SLOT_INFO_T *slot_info =
9092 +                               SLOT_INFO_FROM_INDEX(state, slot_index);
9093 +
9094 +                       release_slot(state, slot_info, header, service);
9095 +               }
9096 +       } else if (slot_index == remote->slot_sync)
9097 +               release_message_sync(state, header);
9098 +
9099 +       unlock_service(service);
9100 +}
9101 +
9102 +static void
9103 +release_message_sync(VCHIQ_STATE_T *state, VCHIQ_HEADER_T *header)
9104 +{
9105 +       header->msgid = VCHIQ_MSGID_PADDING;
9106 +       wmb();
9107 +       remote_event_signal(&state->remote->sync_release);
9108 +}
9109 +
9110 +VCHIQ_STATUS_T
9111 +vchiq_get_peer_version(VCHIQ_SERVICE_HANDLE_T handle, short *peer_version)
9112 +{
9113 +   VCHIQ_STATUS_T status = VCHIQ_ERROR;
9114 +   VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
9115 +
9116 +   if (!service ||
9117 +      (vchiq_check_service(service) != VCHIQ_SUCCESS) ||
9118 +      !peer_version)
9119 +      goto exit;
9120 +   *peer_version = service->peer_version;
9121 +   status = VCHIQ_SUCCESS;
9122 +
9123 +exit:
9124 +   if (service)
9125 +      unlock_service(service);
9126 +   return status;
9127 +}
9128 +
9129 +VCHIQ_STATUS_T
9130 +vchiq_get_config(VCHIQ_INSTANCE_T instance,
9131 +       int config_size, VCHIQ_CONFIG_T *pconfig)
9132 +{
9133 +       VCHIQ_CONFIG_T config;
9134 +
9135 +       (void)instance;
9136 +
9137 +       config.max_msg_size           = VCHIQ_MAX_MSG_SIZE;
9138 +       config.bulk_threshold         = VCHIQ_MAX_MSG_SIZE;
9139 +       config.max_outstanding_bulks  = VCHIQ_NUM_SERVICE_BULKS;
9140 +       config.max_services           = VCHIQ_MAX_SERVICES;
9141 +       config.version                = VCHIQ_VERSION;
9142 +       config.version_min            = VCHIQ_VERSION_MIN;
9143 +
9144 +       if (config_size > sizeof(VCHIQ_CONFIG_T))
9145 +               return VCHIQ_ERROR;
9146 +
9147 +       memcpy(pconfig, &config,
9148 +               min(config_size, (int)(sizeof(VCHIQ_CONFIG_T))));
9149 +
9150 +       return VCHIQ_SUCCESS;
9151 +}
9152 +
9153 +VCHIQ_STATUS_T
9154 +vchiq_set_service_option(VCHIQ_SERVICE_HANDLE_T handle,
9155 +       VCHIQ_SERVICE_OPTION_T option, int value)
9156 +{
9157 +       VCHIQ_SERVICE_T *service = find_service_by_handle(handle);
9158 +       VCHIQ_STATUS_T status = VCHIQ_ERROR;
9159 +
9160 +       if (service) {
9161 +               switch (option) {
9162 +               case VCHIQ_SERVICE_OPTION_AUTOCLOSE:
9163 +                       service->auto_close = value;
9164 +                       status = VCHIQ_SUCCESS;
9165 +                       break;
9166 +
9167 +               case VCHIQ_SERVICE_OPTION_SLOT_QUOTA: {
9168 +                       VCHIQ_SERVICE_QUOTA_T *service_quota =
9169 +                               &service->state->service_quotas[
9170 +                                       service->localport];
9171 +                       if (value == 0)
9172 +                               value = service->state->default_slot_quota;
9173 +                       if ((value >= service_quota->slot_use_count) &&
9174 +                                (value < (unsigned short)~0)) {
9175 +                               service_quota->slot_quota = value;
9176 +                               if ((value >= service_quota->slot_use_count) &&
9177 +                                       (service_quota->message_quota >=
9178 +                                        service_quota->message_use_count)) {
9179 +                                       /* Signal the service that it may have
9180 +                                       ** dropped below its quota */
9181 +                                       up(&service_quota->quota_event);
9182 +                               }
9183 +                               status = VCHIQ_SUCCESS;
9184 +                       }
9185 +               } break;
9186 +
9187 +               case VCHIQ_SERVICE_OPTION_MESSAGE_QUOTA: {
9188 +                       VCHIQ_SERVICE_QUOTA_T *service_quota =
9189 +                               &service->state->service_quotas[
9190 +                                       service->localport];
9191 +                       if (value == 0)
9192 +                               value = service->state->default_message_quota;
9193 +                       if ((value >= service_quota->message_use_count) &&
9194 +                                (value < (unsigned short)~0)) {
9195 +                               service_quota->message_quota = value;
9196 +                               if ((value >=
9197 +                                       service_quota->message_use_count) &&
9198 +                                       (service_quota->slot_quota >=
9199 +                                       service_quota->slot_use_count))
9200 +                                       /* Signal the service that it may have
9201 +                                       ** dropped below its quota */
9202 +                                       up(&service_quota->quota_event);
9203 +                               status = VCHIQ_SUCCESS;
9204 +                       }
9205 +               } break;
9206 +
9207 +               case VCHIQ_SERVICE_OPTION_SYNCHRONOUS:
9208 +                       if ((service->srvstate == VCHIQ_SRVSTATE_HIDDEN) ||
9209 +                               (service->srvstate ==
9210 +                               VCHIQ_SRVSTATE_LISTENING)) {
9211 +                               service->sync = value;
9212 +                               status = VCHIQ_SUCCESS;
9213 +                       }
9214 +                       break;
9215 +
9216 +               case VCHIQ_SERVICE_OPTION_TRACE:
9217 +                       service->trace = value;
9218 +                       status = VCHIQ_SUCCESS;
9219 +                       break;
9220 +
9221 +               default:
9222 +                       break;
9223 +               }
9224 +               unlock_service(service);
9225 +       }
9226 +
9227 +       return status;
9228 +}
9229 +
9230 +void
9231 +vchiq_dump_shared_state(void *dump_context, VCHIQ_STATE_T *state,
9232 +       VCHIQ_SHARED_STATE_T *shared, const char *label)
9233 +{
9234 +       static const char *const debug_names[] = {
9235 +               "<entries>",
9236 +               "SLOT_HANDLER_COUNT",
9237 +               "SLOT_HANDLER_LINE",
9238 +               "PARSE_LINE",
9239 +               "PARSE_HEADER",
9240 +               "PARSE_MSGID",
9241 +               "AWAIT_COMPLETION_LINE",
9242 +               "DEQUEUE_MESSAGE_LINE",
9243 +               "SERVICE_CALLBACK_LINE",
9244 +               "MSG_QUEUE_FULL_COUNT",
9245 +               "COMPLETION_QUEUE_FULL_COUNT"
9246 +       };
9247 +       int i;
9248 +
9249 +       char buf[80];
9250 +       int len;
9251 +       len = snprintf(buf, sizeof(buf),
9252 +               "  %s: slots %d-%d tx_pos=%x recycle=%x",
9253 +               label, shared->slot_first, shared->slot_last,
9254 +               shared->tx_pos, shared->slot_queue_recycle);
9255 +       vchiq_dump(dump_context, buf, len + 1);
9256 +
9257 +       len = snprintf(buf, sizeof(buf),
9258 +               "    Slots claimed:");
9259 +       vchiq_dump(dump_context, buf, len + 1);
9260 +
9261 +       for (i = shared->slot_first; i <= shared->slot_last; i++) {
9262 +               VCHIQ_SLOT_INFO_T slot_info = *SLOT_INFO_FROM_INDEX(state, i);
9263 +               if (slot_info.use_count != slot_info.release_count) {
9264 +                       len = snprintf(buf, sizeof(buf),
9265 +                               "      %d: %d/%d", i, slot_info.use_count,
9266 +                               slot_info.release_count);
9267 +                       vchiq_dump(dump_context, buf, len + 1);
9268 +               }
9269 +       }
9270 +
9271 +       for (i = 1; i < shared->debug[DEBUG_ENTRIES]; i++) {
9272 +               len = snprintf(buf, sizeof(buf), "    DEBUG: %s = %d(%x)",
9273 +                       debug_names[i], shared->debug[i], shared->debug[i]);
9274 +               vchiq_dump(dump_context, buf, len + 1);
9275 +       }
9276 +}
9277 +
9278 +void
9279 +vchiq_dump_state(void *dump_context, VCHIQ_STATE_T *state)
9280 +{
9281 +       char buf[80];
9282 +       int len;
9283 +       int i;
9284 +
9285 +       len = snprintf(buf, sizeof(buf), "State %d: %s", state->id,
9286 +               conn_state_names[state->conn_state]);
9287 +       vchiq_dump(dump_context, buf, len + 1);
9288 +
9289 +       len = snprintf(buf, sizeof(buf),
9290 +               "  tx_pos=%x(@%x), rx_pos=%x(@%x)",
9291 +               state->local->tx_pos,
9292 +               (uint32_t)state->tx_data +
9293 +                       (state->local_tx_pos & VCHIQ_SLOT_MASK),
9294 +               state->rx_pos,
9295 +               (uint32_t)state->rx_data +
9296 +                       (state->rx_pos & VCHIQ_SLOT_MASK));
9297 +       vchiq_dump(dump_context, buf, len + 1);
9298 +
9299 +       len = snprintf(buf, sizeof(buf),
9300 +               "  Version: %d (min %d)",
9301 +               VCHIQ_VERSION, VCHIQ_VERSION_MIN);
9302 +       vchiq_dump(dump_context, buf, len + 1);
9303 +
9304 +       if (VCHIQ_ENABLE_STATS) {
9305 +               len = snprintf(buf, sizeof(buf),
9306 +                       "  Stats: ctrl_tx_count=%d, ctrl_rx_count=%d, "
9307 +                       "error_count=%d",
9308 +                       state->stats.ctrl_tx_count, state->stats.ctrl_rx_count,
9309 +                       state->stats.error_count);
9310 +               vchiq_dump(dump_context, buf, len + 1);
9311 +       }
9312 +
9313 +       len = snprintf(buf, sizeof(buf),
9314 +               "  Slots: %d available (%d data), %d recyclable, %d stalls "
9315 +               "(%d data)",
9316 +               ((state->slot_queue_available * VCHIQ_SLOT_SIZE) -
9317 +                       state->local_tx_pos) / VCHIQ_SLOT_SIZE,
9318 +               state->data_quota - state->data_use_count,
9319 +               state->local->slot_queue_recycle - state->slot_queue_available,
9320 +               state->stats.slot_stalls, state->stats.data_stalls);
9321 +       vchiq_dump(dump_context, buf, len + 1);
9322 +
9323 +       vchiq_dump_platform_state(dump_context);
9324 +
9325 +       vchiq_dump_shared_state(dump_context, state, state->local, "Local");
9326 +       vchiq_dump_shared_state(dump_context, state, state->remote, "Remote");
9327 +
9328 +       vchiq_dump_platform_instances(dump_context);
9329 +
9330 +       for (i = 0; i < state->unused_service; i++) {
9331 +               VCHIQ_SERVICE_T *service = find_service_by_port(state, i);
9332 +
9333 +               if (service) {
9334 +                       vchiq_dump_service_state(dump_context, service);
9335 +                       unlock_service(service);
9336 +               }
9337 +       }
9338 +}
9339 +
9340 +void
9341 +vchiq_dump_service_state(void *dump_context, VCHIQ_SERVICE_T *service)
9342 +{
9343 +       char buf[80];
9344 +       int len;
9345 +
9346 +       len = snprintf(buf, sizeof(buf), "Service %d: %s (ref %u)",
9347 +               service->localport, srvstate_names[service->srvstate],
9348 +               service->ref_count - 1); /*Don't include the lock just taken*/
9349 +
9350 +       if (service->srvstate != VCHIQ_SRVSTATE_FREE) {
9351 +               char remoteport[30];
9352 +               VCHIQ_SERVICE_QUOTA_T *service_quota =
9353 +                       &service->state->service_quotas[service->localport];
9354 +               int fourcc = service->base.fourcc;
9355 +               int tx_pending, rx_pending;
9356 +               if (service->remoteport != VCHIQ_PORT_FREE) {
9357 +                       int len2 = snprintf(remoteport, sizeof(remoteport),
9358 +                               "%d", service->remoteport);
9359 +                       if (service->public_fourcc != VCHIQ_FOURCC_INVALID)
9360 +                               snprintf(remoteport + len2,
9361 +                                       sizeof(remoteport) - len2,
9362 +                                       " (client %x)", service->client_id);
9363 +               } else
9364 +                       strcpy(remoteport, "n/a");
9365 +
9366 +               len += snprintf(buf + len, sizeof(buf) - len,
9367 +                       " '%c%c%c%c' remote %s (msg use %d/%d, slot use %d/%d)",
9368 +                       VCHIQ_FOURCC_AS_4CHARS(fourcc),
9369 +                       remoteport,
9370 +                       service_quota->message_use_count,
9371 +                       service_quota->message_quota,
9372 +                       service_quota->slot_use_count,
9373 +                       service_quota->slot_quota);
9374 +
9375 +               vchiq_dump(dump_context, buf, len + 1);
9376 +
9377 +               tx_pending = service->bulk_tx.local_insert -
9378 +                       service->bulk_tx.remote_insert;
9379 +
9380 +               rx_pending = service->bulk_rx.local_insert -
9381 +                       service->bulk_rx.remote_insert;
9382 +
9383 +               len = snprintf(buf, sizeof(buf),
9384 +                       "  Bulk: tx_pending=%d (size %d),"
9385 +                       " rx_pending=%d (size %d)",
9386 +                       tx_pending,
9387 +                       tx_pending ? service->bulk_tx.bulks[
9388 +                       BULK_INDEX(service->bulk_tx.remove)].size : 0,
9389 +                       rx_pending,
9390 +                       rx_pending ? service->bulk_rx.bulks[
9391 +                       BULK_INDEX(service->bulk_rx.remove)].size : 0);
9392 +
9393 +               if (VCHIQ_ENABLE_STATS) {
9394 +                       vchiq_dump(dump_context, buf, len + 1);
9395 +
9396 +                       len = snprintf(buf, sizeof(buf),
9397 +                               "  Ctrl: tx_count=%d, tx_bytes=%llu, "
9398 +                               "rx_count=%d, rx_bytes=%llu",
9399 +                               service->stats.ctrl_tx_count,
9400 +                               service->stats.ctrl_tx_bytes,
9401 +                               service->stats.ctrl_rx_count,
9402 +                               service->stats.ctrl_rx_bytes);
9403 +                       vchiq_dump(dump_context, buf, len + 1);
9404 +
9405 +                       len = snprintf(buf, sizeof(buf),
9406 +                               "  Bulk: tx_count=%d, tx_bytes=%llu, "
9407 +                               "rx_count=%d, rx_bytes=%llu",
9408 +                               service->stats.bulk_tx_count,
9409 +                               service->stats.bulk_tx_bytes,
9410 +                               service->stats.bulk_rx_count,
9411 +                               service->stats.bulk_rx_bytes);
9412 +                       vchiq_dump(dump_context, buf, len + 1);
9413 +
9414 +                       len = snprintf(buf, sizeof(buf),
9415 +                               "  %d quota stalls, %d slot stalls, "
9416 +                               "%d bulk stalls, %d aborted, %d errors",
9417 +                               service->stats.quota_stalls,
9418 +                               service->stats.slot_stalls,
9419 +                               service->stats.bulk_stalls,
9420 +                               service->stats.bulk_aborted_count,
9421 +                               service->stats.error_count);
9422 +                }
9423 +       }
9424 +
9425 +       vchiq_dump(dump_context, buf, len + 1);
9426 +
9427 +       if (service->srvstate != VCHIQ_SRVSTATE_FREE)
9428 +               vchiq_dump_platform_service_state(dump_context, service);
9429 +}
9430 +
9431 +
9432 +void
9433 +vchiq_loud_error_header(void)
9434 +{
9435 +       vchiq_log_error(vchiq_core_log_level,
9436 +               "============================================================"
9437 +               "================");
9438 +       vchiq_log_error(vchiq_core_log_level,
9439 +               "============================================================"
9440 +               "================");
9441 +       vchiq_log_error(vchiq_core_log_level, "=====");
9442 +}
9443 +
9444 +void
9445 +vchiq_loud_error_footer(void)
9446 +{
9447 +       vchiq_log_error(vchiq_core_log_level, "=====");
9448 +       vchiq_log_error(vchiq_core_log_level,
9449 +               "============================================================"
9450 +               "================");
9451 +       vchiq_log_error(vchiq_core_log_level,
9452 +               "============================================================"
9453 +               "================");
9454 +}
9455 +
9456 +
9457 +VCHIQ_STATUS_T vchiq_send_remote_use(VCHIQ_STATE_T *state)
9458 +{
9459 +       VCHIQ_STATUS_T status = VCHIQ_RETRY;
9460 +       if (state->conn_state != VCHIQ_CONNSTATE_DISCONNECTED)
9461 +               status = queue_message(state, NULL,
9462 +                       VCHIQ_MAKE_MSG(VCHIQ_MSG_REMOTE_USE, 0, 0),
9463 +                       NULL, 0, 0, 0);
9464 +       return status;
9465 +}
9466 +
9467 +VCHIQ_STATUS_T vchiq_send_remote_release(VCHIQ_STATE_T *state)
9468 +{
9469 +       VCHIQ_STATUS_T status = VCHIQ_RETRY;
9470 +       if (state->conn_state != VCHIQ_CONNSTATE_DISCONNECTED)
9471 +               status = queue_message(state, NULL,
9472 +                       VCHIQ_MAKE_MSG(VCHIQ_MSG_REMOTE_RELEASE, 0, 0),
9473 +                       NULL, 0, 0, 0);
9474 +       return status;
9475 +}
9476 +
9477 +VCHIQ_STATUS_T vchiq_send_remote_use_active(VCHIQ_STATE_T *state)
9478 +{
9479 +       VCHIQ_STATUS_T status = VCHIQ_RETRY;
9480 +       if (state->conn_state != VCHIQ_CONNSTATE_DISCONNECTED)
9481 +               status = queue_message(state, NULL,
9482 +                       VCHIQ_MAKE_MSG(VCHIQ_MSG_REMOTE_USE_ACTIVE, 0, 0),
9483 +                       NULL, 0, 0, 0);
9484 +       return status;
9485 +}
9486 +
9487 +void vchiq_log_dump_mem(const char *label, uint32_t addr, const void *voidMem,
9488 +       size_t numBytes)
9489 +{
9490 +       const uint8_t  *mem = (const uint8_t *)voidMem;
9491 +       size_t          offset;
9492 +       char            lineBuf[100];
9493 +       char           *s;
9494 +
9495 +       while (numBytes > 0) {
9496 +               s = lineBuf;
9497 +
9498 +               for (offset = 0; offset < 16; offset++) {
9499 +                       if (offset < numBytes)
9500 +                               s += snprintf(s, 4, "%02x ", mem[offset]);
9501 +                       else
9502 +                               s += snprintf(s, 4, "   ");
9503 +               }
9504 +
9505 +               for (offset = 0; offset < 16; offset++) {
9506 +                       if (offset < numBytes) {
9507 +                               uint8_t ch = mem[offset];
9508 +
9509 +                               if ((ch < ' ') || (ch > '~'))
9510 +                                       ch = '.';
9511 +                               *s++ = (char)ch;
9512 +                       }
9513 +               }
9514 +               *s++ = '\0';
9515 +
9516 +               if ((label != NULL) && (*label != '\0'))
9517 +                       vchiq_log_trace(VCHIQ_LOG_TRACE,
9518 +                               "%s: %08x: %s", label, addr, lineBuf);
9519 +               else
9520 +                       vchiq_log_trace(VCHIQ_LOG_TRACE,
9521 +                               "%08x: %s", addr, lineBuf);
9522 +
9523 +               addr += 16;
9524 +               mem += 16;
9525 +               if (numBytes > 16)
9526 +                       numBytes -= 16;
9527 +               else
9528 +                       numBytes = 0;
9529 +       }
9530 +}
9531 --- /dev/null
9532 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_core.h
9533 @@ -0,0 +1,711 @@
9534 +/**
9535 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
9536 + *
9537 + * Redistribution and use in source and binary forms, with or without
9538 + * modification, are permitted provided that the following conditions
9539 + * are met:
9540 + * 1. Redistributions of source code must retain the above copyright
9541 + *    notice, this list of conditions, and the following disclaimer,
9542 + *    without modification.
9543 + * 2. Redistributions in binary form must reproduce the above copyright
9544 + *    notice, this list of conditions and the following disclaimer in the
9545 + *    documentation and/or other materials provided with the distribution.
9546 + * 3. The names of the above-listed copyright holders may not be used
9547 + *    to endorse or promote products derived from this software without
9548 + *    specific prior written permission.
9549 + *
9550 + * ALTERNATIVELY, this software may be distributed under the terms of the
9551 + * GNU General Public License ("GPL") version 2, as published by the Free
9552 + * Software Foundation.
9553 + *
9554 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
9555 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
9556 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
9557 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
9558 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
9559 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
9560 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
9561 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
9562 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
9563 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
9564 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
9565 + */
9566 +
9567 +#ifndef VCHIQ_CORE_H
9568 +#define VCHIQ_CORE_H
9569 +
9570 +#include <linux/mutex.h>
9571 +#include <linux/semaphore.h>
9572 +#include <linux/kthread.h>
9573 +
9574 +#include "vchiq_cfg.h"
9575 +
9576 +#include "vchiq.h"
9577 +
9578 +/* Run time control of log level, based on KERN_XXX level. */
9579 +#define VCHIQ_LOG_DEFAULT  4
9580 +#define VCHIQ_LOG_ERROR    3
9581 +#define VCHIQ_LOG_WARNING  4
9582 +#define VCHIQ_LOG_INFO     6
9583 +#define VCHIQ_LOG_TRACE    7
9584 +
9585 +#define VCHIQ_LOG_PREFIX   KERN_INFO "vchiq: "
9586 +
9587 +#ifndef vchiq_log_error
9588 +#define vchiq_log_error(cat, fmt, ...) \
9589 +       do { if (cat >= VCHIQ_LOG_ERROR) \
9590 +               printk(VCHIQ_LOG_PREFIX fmt "\n", ##__VA_ARGS__); } while (0)
9591 +#endif
9592 +#ifndef vchiq_log_warning
9593 +#define vchiq_log_warning(cat, fmt, ...) \
9594 +       do { if (cat >= VCHIQ_LOG_WARNING) \
9595 +                printk(VCHIQ_LOG_PREFIX fmt "\n", ##__VA_ARGS__); } while (0)
9596 +#endif
9597 +#ifndef vchiq_log_info
9598 +#define vchiq_log_info(cat, fmt, ...) \
9599 +       do { if (cat >= VCHIQ_LOG_INFO) \
9600 +               printk(VCHIQ_LOG_PREFIX fmt "\n", ##__VA_ARGS__); } while (0)
9601 +#endif
9602 +#ifndef vchiq_log_trace
9603 +#define vchiq_log_trace(cat, fmt, ...) \
9604 +       do { if (cat >= VCHIQ_LOG_TRACE) \
9605 +               printk(VCHIQ_LOG_PREFIX fmt "\n", ##__VA_ARGS__); } while (0)
9606 +#endif
9607 +
9608 +#define vchiq_loud_error(...) \
9609 +       vchiq_log_error(vchiq_core_log_level, "===== " __VA_ARGS__)
9610 +
9611 +#ifndef vchiq_static_assert
9612 +#define vchiq_static_assert(cond) __attribute__((unused)) \
9613 +       extern int vchiq_static_assert[(cond) ? 1 : -1]
9614 +#endif
9615 +
9616 +#define IS_POW2(x) (x && ((x & (x - 1)) == 0))
9617 +
9618 +/* Ensure that the slot size and maximum number of slots are powers of 2 */
9619 +vchiq_static_assert(IS_POW2(VCHIQ_SLOT_SIZE));
9620 +vchiq_static_assert(IS_POW2(VCHIQ_MAX_SLOTS));
9621 +vchiq_static_assert(IS_POW2(VCHIQ_MAX_SLOTS_PER_SIDE));
9622 +
9623 +#define VCHIQ_SLOT_MASK        (VCHIQ_SLOT_SIZE - 1)
9624 +#define VCHIQ_SLOT_QUEUE_MASK  (VCHIQ_MAX_SLOTS_PER_SIDE - 1)
9625 +#define VCHIQ_SLOT_ZERO_SLOTS  ((sizeof(VCHIQ_SLOT_ZERO_T) + \
9626 +       VCHIQ_SLOT_SIZE - 1) / VCHIQ_SLOT_SIZE)
9627 +
9628 +#define VCHIQ_MSG_PADDING            0  /* -                                 */
9629 +#define VCHIQ_MSG_CONNECT            1  /* -                                 */
9630 +#define VCHIQ_MSG_OPEN               2  /* + (srcport, -), fourcc, client_id */
9631 +#define VCHIQ_MSG_OPENACK            3  /* + (srcport, dstport)              */
9632 +#define VCHIQ_MSG_CLOSE              4  /* + (srcport, dstport)              */
9633 +#define VCHIQ_MSG_DATA               5  /* + (srcport, dstport)              */
9634 +#define VCHIQ_MSG_BULK_RX            6  /* + (srcport, dstport), data, size  */
9635 +#define VCHIQ_MSG_BULK_TX            7  /* + (srcport, dstport), data, size  */
9636 +#define VCHIQ_MSG_BULK_RX_DONE       8  /* + (srcport, dstport), actual      */
9637 +#define VCHIQ_MSG_BULK_TX_DONE       9  /* + (srcport, dstport), actual      */
9638 +#define VCHIQ_MSG_PAUSE             10  /* -                                 */
9639 +#define VCHIQ_MSG_RESUME            11  /* -                                 */
9640 +#define VCHIQ_MSG_REMOTE_USE        12  /* -                                 */
9641 +#define VCHIQ_MSG_REMOTE_RELEASE    13  /* -                                 */
9642 +#define VCHIQ_MSG_REMOTE_USE_ACTIVE 14  /* -                                 */
9643 +
9644 +#define VCHIQ_PORT_MAX                 (VCHIQ_MAX_SERVICES - 1)
9645 +#define VCHIQ_PORT_FREE                0x1000
9646 +#define VCHIQ_PORT_IS_VALID(port)      (port < VCHIQ_PORT_FREE)
9647 +#define VCHIQ_MAKE_MSG(type, srcport, dstport) \
9648 +       ((type<<24) | (srcport<<12) | (dstport<<0))
9649 +#define VCHIQ_MSG_TYPE(msgid)          ((unsigned int)msgid >> 24)
9650 +#define VCHIQ_MSG_SRCPORT(msgid) \
9651 +       (unsigned short)(((unsigned int)msgid >> 12) & 0xfff)
9652 +#define VCHIQ_MSG_DSTPORT(msgid) \
9653 +       ((unsigned short)msgid & 0xfff)
9654 +
9655 +#define VCHIQ_FOURCC_AS_4CHARS(fourcc) \
9656 +       ((fourcc) >> 24) & 0xff, \
9657 +       ((fourcc) >> 16) & 0xff, \
9658 +       ((fourcc) >>  8) & 0xff, \
9659 +       (fourcc) & 0xff
9660 +
9661 +/* Ensure the fields are wide enough */
9662 +vchiq_static_assert(VCHIQ_MSG_SRCPORT(VCHIQ_MAKE_MSG(0, 0, VCHIQ_PORT_MAX))
9663 +       == 0);
9664 +vchiq_static_assert(VCHIQ_MSG_TYPE(VCHIQ_MAKE_MSG(0, VCHIQ_PORT_MAX, 0)) == 0);
9665 +vchiq_static_assert((unsigned int)VCHIQ_PORT_MAX <
9666 +       (unsigned int)VCHIQ_PORT_FREE);
9667 +
9668 +#define VCHIQ_MSGID_PADDING            VCHIQ_MAKE_MSG(VCHIQ_MSG_PADDING, 0, 0)
9669 +#define VCHIQ_MSGID_CLAIMED            0x40000000
9670 +
9671 +#define VCHIQ_FOURCC_INVALID           0x00000000
9672 +#define VCHIQ_FOURCC_IS_LEGAL(fourcc)  (fourcc != VCHIQ_FOURCC_INVALID)
9673 +
9674 +#define VCHIQ_BULK_ACTUAL_ABORTED -1
9675 +
9676 +typedef uint32_t BITSET_T;
9677 +
9678 +vchiq_static_assert((sizeof(BITSET_T) * 8) == 32);
9679 +
9680 +#define BITSET_SIZE(b)        ((b + 31) >> 5)
9681 +#define BITSET_WORD(b)        (b >> 5)
9682 +#define BITSET_BIT(b)         (1 << (b & 31))
9683 +#define BITSET_ZERO(bs)       memset(bs, 0, sizeof(bs))
9684 +#define BITSET_IS_SET(bs, b)  (bs[BITSET_WORD(b)] & BITSET_BIT(b))
9685 +#define BITSET_SET(bs, b)     (bs[BITSET_WORD(b)] |= BITSET_BIT(b))
9686 +#define BITSET_CLR(bs, b)     (bs[BITSET_WORD(b)] &= ~BITSET_BIT(b))
9687 +
9688 +#if VCHIQ_ENABLE_STATS
9689 +#define VCHIQ_STATS_INC(state, stat) (state->stats. stat++)
9690 +#define VCHIQ_SERVICE_STATS_INC(service, stat) (service->stats. stat++)
9691 +#define VCHIQ_SERVICE_STATS_ADD(service, stat, addend) \
9692 +       (service->stats. stat += addend)
9693 +#else
9694 +#define VCHIQ_STATS_INC(state, stat) ((void)0)
9695 +#define VCHIQ_SERVICE_STATS_INC(service, stat) ((void)0)
9696 +#define VCHIQ_SERVICE_STATS_ADD(service, stat, addend) ((void)0)
9697 +#endif
9698 +
9699 +enum {
9700 +       DEBUG_ENTRIES,
9701 +#if VCHIQ_ENABLE_DEBUG
9702 +       DEBUG_SLOT_HANDLER_COUNT,
9703 +       DEBUG_SLOT_HANDLER_LINE,
9704 +       DEBUG_PARSE_LINE,
9705 +       DEBUG_PARSE_HEADER,
9706 +       DEBUG_PARSE_MSGID,
9707 +       DEBUG_AWAIT_COMPLETION_LINE,
9708 +       DEBUG_DEQUEUE_MESSAGE_LINE,
9709 +       DEBUG_SERVICE_CALLBACK_LINE,
9710 +       DEBUG_MSG_QUEUE_FULL_COUNT,
9711 +       DEBUG_COMPLETION_QUEUE_FULL_COUNT,
9712 +#endif
9713 +       DEBUG_MAX
9714 +};
9715 +
9716 +#if VCHIQ_ENABLE_DEBUG
9717 +
9718 +#define DEBUG_INITIALISE(local) int *debug_ptr = (local)->debug;
9719 +#define DEBUG_TRACE(d) \
9720 +       do { debug_ptr[DEBUG_ ## d] = __LINE__; dsb(); } while (0)
9721 +#define DEBUG_VALUE(d, v) \
9722 +       do { debug_ptr[DEBUG_ ## d] = (v); dsb(); } while (0)
9723 +#define DEBUG_COUNT(d) \
9724 +       do { debug_ptr[DEBUG_ ## d]++; dsb(); } while (0)
9725 +
9726 +#else /* VCHIQ_ENABLE_DEBUG */
9727 +
9728 +#define DEBUG_INITIALISE(local)
9729 +#define DEBUG_TRACE(d)
9730 +#define DEBUG_VALUE(d, v)
9731 +#define DEBUG_COUNT(d)
9732 +
9733 +#endif /* VCHIQ_ENABLE_DEBUG */
9734 +
9735 +typedef enum {
9736 +       VCHIQ_CONNSTATE_DISCONNECTED,
9737 +       VCHIQ_CONNSTATE_CONNECTING,
9738 +       VCHIQ_CONNSTATE_CONNECTED,
9739 +       VCHIQ_CONNSTATE_PAUSING,
9740 +       VCHIQ_CONNSTATE_PAUSE_SENT,
9741 +       VCHIQ_CONNSTATE_PAUSED,
9742 +       VCHIQ_CONNSTATE_RESUMING,
9743 +       VCHIQ_CONNSTATE_PAUSE_TIMEOUT,
9744 +       VCHIQ_CONNSTATE_RESUME_TIMEOUT
9745 +} VCHIQ_CONNSTATE_T;
9746 +
9747 +enum {
9748 +       VCHIQ_SRVSTATE_FREE,
9749 +       VCHIQ_SRVSTATE_HIDDEN,
9750 +       VCHIQ_SRVSTATE_LISTENING,
9751 +       VCHIQ_SRVSTATE_OPENING,
9752 +       VCHIQ_SRVSTATE_OPEN,
9753 +       VCHIQ_SRVSTATE_OPENSYNC,
9754 +       VCHIQ_SRVSTATE_CLOSESENT,
9755 +       VCHIQ_SRVSTATE_CLOSERECVD,
9756 +       VCHIQ_SRVSTATE_CLOSEWAIT,
9757 +       VCHIQ_SRVSTATE_CLOSED
9758 +};
9759 +
9760 +enum {
9761 +       VCHIQ_POLL_TERMINATE,
9762 +       VCHIQ_POLL_REMOVE,
9763 +       VCHIQ_POLL_TXNOTIFY,
9764 +       VCHIQ_POLL_RXNOTIFY,
9765 +       VCHIQ_POLL_COUNT
9766 +};
9767 +
9768 +typedef enum {
9769 +       VCHIQ_BULK_TRANSMIT,
9770 +       VCHIQ_BULK_RECEIVE
9771 +} VCHIQ_BULK_DIR_T;
9772 +
9773 +typedef void (*VCHIQ_USERDATA_TERM_T)(void *userdata);
9774 +
9775 +typedef struct vchiq_bulk_struct {
9776 +       short mode;
9777 +       short dir;
9778 +       void *userdata;
9779 +       VCHI_MEM_HANDLE_T handle;
9780 +       void *data;
9781 +       int size;
9782 +       void *remote_data;
9783 +       int remote_size;
9784 +       int actual;
9785 +} VCHIQ_BULK_T;
9786 +
9787 +typedef struct vchiq_bulk_queue_struct {
9788 +       int local_insert;  /* Where to insert the next local bulk */
9789 +       int remote_insert; /* Where to insert the next remote bulk (master) */
9790 +       int process;       /* Bulk to transfer next */
9791 +       int remote_notify; /* Bulk to notify the remote client of next (mstr) */
9792 +       int remove;        /* Bulk to notify the local client of, and remove,
9793 +                          ** next */
9794 +       VCHIQ_BULK_T bulks[VCHIQ_NUM_SERVICE_BULKS];
9795 +} VCHIQ_BULK_QUEUE_T;
9796 +
9797 +typedef struct remote_event_struct {
9798 +       int armed;
9799 +       int fired;
9800 +       struct semaphore *event;
9801 +} REMOTE_EVENT_T;
9802 +
9803 +typedef struct opaque_platform_state_t *VCHIQ_PLATFORM_STATE_T;
9804 +
9805 +typedef struct vchiq_state_struct VCHIQ_STATE_T;
9806 +
9807 +typedef struct vchiq_slot_struct {
9808 +       char data[VCHIQ_SLOT_SIZE];
9809 +} VCHIQ_SLOT_T;
9810 +
9811 +typedef struct vchiq_slot_info_struct {
9812 +       /* Use two counters rather than one to avoid the need for a mutex. */
9813 +       short use_count;
9814 +       short release_count;
9815 +} VCHIQ_SLOT_INFO_T;
9816 +
9817 +typedef struct vchiq_service_struct {
9818 +       VCHIQ_SERVICE_BASE_T base;
9819 +       VCHIQ_SERVICE_HANDLE_T handle;
9820 +       unsigned int ref_count;
9821 +       int srvstate;
9822 +       VCHIQ_USERDATA_TERM_T userdata_term;
9823 +       unsigned int localport;
9824 +       unsigned int remoteport;
9825 +       int public_fourcc;
9826 +       int client_id;
9827 +       char auto_close;
9828 +       char sync;
9829 +       char closing;
9830 +       char trace;
9831 +       atomic_t poll_flags;
9832 +       short version;
9833 +       short version_min;
9834 +       short peer_version;
9835 +
9836 +       VCHIQ_STATE_T *state;
9837 +       VCHIQ_INSTANCE_T instance;
9838 +
9839 +       int service_use_count;
9840 +
9841 +       VCHIQ_BULK_QUEUE_T bulk_tx;
9842 +       VCHIQ_BULK_QUEUE_T bulk_rx;
9843 +
9844 +       struct semaphore remove_event;
9845 +       struct semaphore bulk_remove_event;
9846 +       struct mutex bulk_mutex;
9847 +
9848 +       struct service_stats_struct {
9849 +               int quota_stalls;
9850 +               int slot_stalls;
9851 +               int bulk_stalls;
9852 +               int error_count;
9853 +               int ctrl_tx_count;
9854 +               int ctrl_rx_count;
9855 +               int bulk_tx_count;
9856 +               int bulk_rx_count;
9857 +               int bulk_aborted_count;
9858 +               uint64_t ctrl_tx_bytes;
9859 +               uint64_t ctrl_rx_bytes;
9860 +               uint64_t bulk_tx_bytes;
9861 +               uint64_t bulk_rx_bytes;
9862 +       } stats;
9863 +} VCHIQ_SERVICE_T;
9864 +
9865 +/* The quota information is outside VCHIQ_SERVICE_T so that it can be
9866 +       statically allocated, since for accounting reasons a service's slot
9867 +       usage is carried over between users of the same port number.
9868 + */
9869 +typedef struct vchiq_service_quota_struct {
9870 +       unsigned short slot_quota;
9871 +       unsigned short slot_use_count;
9872 +       unsigned short message_quota;
9873 +       unsigned short message_use_count;
9874 +       struct semaphore quota_event;
9875 +       int previous_tx_index;
9876 +} VCHIQ_SERVICE_QUOTA_T;
9877 +
9878 +typedef struct vchiq_shared_state_struct {
9879 +
9880 +       /* A non-zero value here indicates that the content is valid. */
9881 +       int initialised;
9882 +
9883 +       /* The first and last (inclusive) slots allocated to the owner. */
9884 +       int slot_first;
9885 +       int slot_last;
9886 +
9887 +       /* The slot allocated to synchronous messages from the owner. */
9888 +       int slot_sync;
9889 +
9890 +       /* Signalling this event indicates that owner's slot handler thread
9891 +       ** should run. */
9892 +       REMOTE_EVENT_T trigger;
9893 +
9894 +       /* Indicates the byte position within the stream where the next message
9895 +       ** will be written. The least significant bits are an index into the
9896 +       ** slot. The next bits are the index of the slot in slot_queue. */
9897 +       int tx_pos;
9898 +
9899 +       /* This event should be signalled when a slot is recycled. */
9900 +       REMOTE_EVENT_T recycle;
9901 +
9902 +       /* The slot_queue index where the next recycled slot will be written. */
9903 +       int slot_queue_recycle;
9904 +
9905 +       /* This event should be signalled when a synchronous message is sent. */
9906 +       REMOTE_EVENT_T sync_trigger;
9907 +
9908 +       /* This event should be signalled when a synchronous message has been
9909 +       ** released. */
9910 +       REMOTE_EVENT_T sync_release;
9911 +
9912 +       /* A circular buffer of slot indexes. */
9913 +       int slot_queue[VCHIQ_MAX_SLOTS_PER_SIDE];
9914 +
9915 +       /* Debugging state */
9916 +       int debug[DEBUG_MAX];
9917 +} VCHIQ_SHARED_STATE_T;
9918 +
9919 +typedef struct vchiq_slot_zero_struct {
9920 +       int magic;
9921 +       short version;
9922 +       short version_min;
9923 +       int slot_zero_size;
9924 +       int slot_size;
9925 +       int max_slots;
9926 +       int max_slots_per_side;
9927 +       int platform_data[2];
9928 +       VCHIQ_SHARED_STATE_T master;
9929 +       VCHIQ_SHARED_STATE_T slave;
9930 +       VCHIQ_SLOT_INFO_T slots[VCHIQ_MAX_SLOTS];
9931 +} VCHIQ_SLOT_ZERO_T;
9932 +
9933 +struct vchiq_state_struct {
9934 +       int id;
9935 +       int initialised;
9936 +       VCHIQ_CONNSTATE_T conn_state;
9937 +       int is_master;
9938 +
9939 +       VCHIQ_SHARED_STATE_T *local;
9940 +       VCHIQ_SHARED_STATE_T *remote;
9941 +       VCHIQ_SLOT_T *slot_data;
9942 +
9943 +       unsigned short default_slot_quota;
9944 +       unsigned short default_message_quota;
9945 +
9946 +       /* Event indicating connect message received */
9947 +       struct semaphore connect;
9948 +
9949 +       /* Mutex protecting services */
9950 +       struct mutex mutex;
9951 +       VCHIQ_INSTANCE_T *instance;
9952 +
9953 +       /* Processes incoming messages */
9954 +       struct task_struct *slot_handler_thread;
9955 +
9956 +       /* Processes recycled slots */
9957 +       struct task_struct *recycle_thread;
9958 +
9959 +       /* Processes synchronous messages */
9960 +       struct task_struct *sync_thread;
9961 +
9962 +       /* Local implementation of the trigger remote event */
9963 +       struct semaphore trigger_event;
9964 +
9965 +       /* Local implementation of the recycle remote event */
9966 +       struct semaphore recycle_event;
9967 +
9968 +       /* Local implementation of the sync trigger remote event */
9969 +       struct semaphore sync_trigger_event;
9970 +
9971 +       /* Local implementation of the sync release remote event */
9972 +       struct semaphore sync_release_event;
9973 +
9974 +       char *tx_data;
9975 +       char *rx_data;
9976 +       VCHIQ_SLOT_INFO_T *rx_info;
9977 +
9978 +       struct mutex slot_mutex;
9979 +
9980 +       struct mutex recycle_mutex;
9981 +
9982 +       struct mutex sync_mutex;
9983 +
9984 +       struct mutex bulk_transfer_mutex;
9985 +
9986 +       /* Indicates the byte position within the stream from where the next
9987 +       ** message will be read. The least significant bits are an index into
9988 +       ** the slot.The next bits are the index of the slot in
9989 +       ** remote->slot_queue. */
9990 +       int rx_pos;
9991 +
9992 +       /* A cached copy of local->tx_pos. Only write to local->tx_pos, and read
9993 +               from remote->tx_pos. */
9994 +       int local_tx_pos;
9995 +
9996 +       /* The slot_queue index of the slot to become available next. */
9997 +       int slot_queue_available;
9998 +
9999 +       /* A flag to indicate if any poll has been requested */
10000 +       int poll_needed;
10001 +
10002 +       /* Ths index of the previous slot used for data messages. */
10003 +       int previous_data_index;
10004 +
10005 +       /* The number of slots occupied by data messages. */
10006 +       unsigned short data_use_count;
10007 +
10008 +       /* The maximum number of slots to be occupied by data messages. */
10009 +       unsigned short data_quota;
10010 +
10011 +       /* An array of bit sets indicating which services must be polled. */
10012 +       atomic_t poll_services[BITSET_SIZE(VCHIQ_MAX_SERVICES)];
10013 +
10014 +       /* The number of the first unused service */
10015 +       int unused_service;
10016 +
10017 +       /* Signalled when a free slot becomes available. */
10018 +       struct semaphore slot_available_event;
10019 +
10020 +       struct semaphore slot_remove_event;
10021 +
10022 +       /* Signalled when a free data slot becomes available. */
10023 +       struct semaphore data_quota_event;
10024 +
10025 +       /* Incremented when there are bulk transfers which cannot be processed
10026 +        * whilst paused and must be processed on resume */
10027 +       int deferred_bulks;
10028 +
10029 +       struct state_stats_struct {
10030 +               int slot_stalls;
10031 +               int data_stalls;
10032 +               int ctrl_tx_count;
10033 +               int ctrl_rx_count;
10034 +               int error_count;
10035 +       } stats;
10036 +
10037 +       VCHIQ_SERVICE_T * services[VCHIQ_MAX_SERVICES];
10038 +       VCHIQ_SERVICE_QUOTA_T service_quotas[VCHIQ_MAX_SERVICES];
10039 +       VCHIQ_SLOT_INFO_T slot_info[VCHIQ_MAX_SLOTS];
10040 +
10041 +       VCHIQ_PLATFORM_STATE_T platform_state;
10042 +};
10043 +
10044 +struct bulk_waiter {
10045 +       VCHIQ_BULK_T *bulk;
10046 +       struct semaphore event;
10047 +       int actual;
10048 +};
10049 +
10050 +extern spinlock_t bulk_waiter_spinlock;
10051 +
10052 +extern int vchiq_core_log_level;
10053 +extern int vchiq_core_msg_log_level;
10054 +extern int vchiq_sync_log_level;
10055 +
10056 +extern VCHIQ_STATE_T *vchiq_states[VCHIQ_MAX_STATES];
10057 +
10058 +extern const char *
10059 +get_conn_state_name(VCHIQ_CONNSTATE_T conn_state);
10060 +
10061 +extern VCHIQ_SLOT_ZERO_T *
10062 +vchiq_init_slots(void *mem_base, int mem_size);
10063 +
10064 +extern VCHIQ_STATUS_T
10065 +vchiq_init_state(VCHIQ_STATE_T *state, VCHIQ_SLOT_ZERO_T *slot_zero,
10066 +       int is_master);
10067 +
10068 +extern VCHIQ_STATUS_T
10069 +vchiq_connect_internal(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance);
10070 +
10071 +extern VCHIQ_SERVICE_T *
10072 +vchiq_add_service_internal(VCHIQ_STATE_T *state,
10073 +       const VCHIQ_SERVICE_PARAMS_T *params, int srvstate,
10074 +       VCHIQ_INSTANCE_T instance, VCHIQ_USERDATA_TERM_T userdata_term);
10075 +
10076 +extern VCHIQ_STATUS_T
10077 +vchiq_open_service_internal(VCHIQ_SERVICE_T *service, int client_id);
10078 +
10079 +extern VCHIQ_STATUS_T
10080 +vchiq_close_service_internal(VCHIQ_SERVICE_T *service, int close_recvd);
10081 +
10082 +extern void
10083 +vchiq_terminate_service_internal(VCHIQ_SERVICE_T *service);
10084 +
10085 +extern void
10086 +vchiq_free_service_internal(VCHIQ_SERVICE_T *service);
10087 +
10088 +extern VCHIQ_STATUS_T
10089 +vchiq_shutdown_internal(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance);
10090 +
10091 +extern VCHIQ_STATUS_T
10092 +vchiq_pause_internal(VCHIQ_STATE_T *state);
10093 +
10094 +extern VCHIQ_STATUS_T
10095 +vchiq_resume_internal(VCHIQ_STATE_T *state);
10096 +
10097 +extern void
10098 +remote_event_pollall(VCHIQ_STATE_T *state);
10099 +
10100 +extern VCHIQ_STATUS_T
10101 +vchiq_bulk_transfer(VCHIQ_SERVICE_HANDLE_T handle,
10102 +       VCHI_MEM_HANDLE_T memhandle, void *offset, int size, void *userdata,
10103 +       VCHIQ_BULK_MODE_T mode, VCHIQ_BULK_DIR_T dir);
10104 +
10105 +extern void
10106 +vchiq_dump_state(void *dump_context, VCHIQ_STATE_T *state);
10107 +
10108 +extern void
10109 +vchiq_dump_service_state(void *dump_context, VCHIQ_SERVICE_T *service);
10110 +
10111 +extern void
10112 +vchiq_loud_error_header(void);
10113 +
10114 +extern void
10115 +vchiq_loud_error_footer(void);
10116 +
10117 +extern void
10118 +request_poll(VCHIQ_STATE_T *state, VCHIQ_SERVICE_T *service, int poll_type);
10119 +
10120 +static inline VCHIQ_SERVICE_T *
10121 +handle_to_service(VCHIQ_SERVICE_HANDLE_T handle)
10122 +{
10123 +       VCHIQ_STATE_T *state = vchiq_states[(handle / VCHIQ_MAX_SERVICES) &
10124 +               (VCHIQ_MAX_STATES - 1)];
10125 +       if (!state)
10126 +               return NULL;
10127 +
10128 +       return state->services[handle & (VCHIQ_MAX_SERVICES - 1)];
10129 +}
10130 +
10131 +extern VCHIQ_SERVICE_T *
10132 +find_service_by_handle(VCHIQ_SERVICE_HANDLE_T handle);
10133 +
10134 +extern VCHIQ_SERVICE_T *
10135 +find_service_by_port(VCHIQ_STATE_T *state, int localport);
10136 +
10137 +extern VCHIQ_SERVICE_T *
10138 +find_service_for_instance(VCHIQ_INSTANCE_T instance,
10139 +       VCHIQ_SERVICE_HANDLE_T handle);
10140 +
10141 +extern VCHIQ_SERVICE_T *
10142 +find_closed_service_for_instance(VCHIQ_INSTANCE_T instance,
10143 +       VCHIQ_SERVICE_HANDLE_T handle);
10144 +
10145 +extern VCHIQ_SERVICE_T *
10146 +next_service_by_instance(VCHIQ_STATE_T *state, VCHIQ_INSTANCE_T instance,
10147 +       int *pidx);
10148 +
10149 +extern void
10150 +lock_service(VCHIQ_SERVICE_T *service);
10151 +
10152 +extern void
10153 +unlock_service(VCHIQ_SERVICE_T *service);
10154 +
10155 +/* The following functions are called from vchiq_core, and external
10156 +** implementations must be provided. */
10157 +
10158 +extern VCHIQ_STATUS_T
10159 +vchiq_prepare_bulk_data(VCHIQ_BULK_T *bulk,
10160 +       VCHI_MEM_HANDLE_T memhandle, void *offset, int size, int dir);
10161 +
10162 +extern void
10163 +vchiq_transfer_bulk(VCHIQ_BULK_T *bulk);
10164 +
10165 +extern void
10166 +vchiq_complete_bulk(VCHIQ_BULK_T *bulk);
10167 +
10168 +extern VCHIQ_STATUS_T
10169 +vchiq_copy_from_user(void *dst, const void *src, int size);
10170 +
10171 +extern void
10172 +remote_event_signal(REMOTE_EVENT_T *event);
10173 +
10174 +void
10175 +vchiq_platform_check_suspend(VCHIQ_STATE_T *state);
10176 +
10177 +extern void
10178 +vchiq_platform_paused(VCHIQ_STATE_T *state);
10179 +
10180 +extern VCHIQ_STATUS_T
10181 +vchiq_platform_resume(VCHIQ_STATE_T *state);
10182 +
10183 +extern void
10184 +vchiq_platform_resumed(VCHIQ_STATE_T *state);
10185 +
10186 +extern void
10187 +vchiq_dump(void *dump_context, const char *str, int len);
10188 +
10189 +extern void
10190 +vchiq_dump_platform_state(void *dump_context);
10191 +
10192 +extern void
10193 +vchiq_dump_platform_instances(void *dump_context);
10194 +
10195 +extern void
10196 +vchiq_dump_platform_service_state(void *dump_context,
10197 +       VCHIQ_SERVICE_T *service);
10198 +
10199 +extern VCHIQ_STATUS_T
10200 +vchiq_use_service_internal(VCHIQ_SERVICE_T *service);
10201 +
10202 +extern VCHIQ_STATUS_T
10203 +vchiq_release_service_internal(VCHIQ_SERVICE_T *service);
10204 +
10205 +extern void
10206 +vchiq_on_remote_use(VCHIQ_STATE_T *state);
10207 +
10208 +extern void
10209 +vchiq_on_remote_release(VCHIQ_STATE_T *state);
10210 +
10211 +extern VCHIQ_STATUS_T
10212 +vchiq_platform_init_state(VCHIQ_STATE_T *state);
10213 +
10214 +extern VCHIQ_STATUS_T
10215 +vchiq_check_service(VCHIQ_SERVICE_T *service);
10216 +
10217 +extern void
10218 +vchiq_on_remote_use_active(VCHIQ_STATE_T *state);
10219 +
10220 +extern VCHIQ_STATUS_T
10221 +vchiq_send_remote_use(VCHIQ_STATE_T *state);
10222 +
10223 +extern VCHIQ_STATUS_T
10224 +vchiq_send_remote_release(VCHIQ_STATE_T *state);
10225 +
10226 +extern VCHIQ_STATUS_T
10227 +vchiq_send_remote_use_active(VCHIQ_STATE_T *state);
10228 +
10229 +extern void
10230 +vchiq_platform_conn_state_changed(VCHIQ_STATE_T *state,
10231 +       VCHIQ_CONNSTATE_T oldstate, VCHIQ_CONNSTATE_T newstate);
10232 +
10233 +extern void
10234 +vchiq_platform_handle_timeout(VCHIQ_STATE_T *state);
10235 +
10236 +extern void
10237 +vchiq_set_conn_state(VCHIQ_STATE_T *state, VCHIQ_CONNSTATE_T newstate);
10238 +
10239 +
10240 +extern void
10241 +vchiq_log_dump_mem(const char *label, uint32_t addr, const void *voidMem,
10242 +       size_t numBytes);
10243 +
10244 +#endif
10245 --- /dev/null
10246 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_debugfs.c
10247 @@ -0,0 +1,383 @@
10248 +/**
10249 + * Copyright (c) 2014 Raspberry Pi (Trading) Ltd. All rights reserved.
10250 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
10251 + *
10252 + * Redistribution and use in source and binary forms, with or without
10253 + * modification, are permitted provided that the following conditions
10254 + * are met:
10255 + * 1. Redistributions of source code must retain the above copyright
10256 + *    notice, this list of conditions, and the following disclaimer,
10257 + *    without modification.
10258 + * 2. Redistributions in binary form must reproduce the above copyright
10259 + *    notice, this list of conditions and the following disclaimer in the
10260 + *    documentation and/or other materials provided with the distribution.
10261 + * 3. The names of the above-listed copyright holders may not be used
10262 + *    to endorse or promote products derived from this software without
10263 + *    specific prior written permission.
10264 + *
10265 + * ALTERNATIVELY, this software may be distributed under the terms of the
10266 + * GNU General Public License ("GPL") version 2, as published by the Free
10267 + * Software Foundation.
10268 + *
10269 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
10270 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
10271 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
10272 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
10273 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
10274 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
10275 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
10276 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
10277 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
10278 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
10279 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
10280 + */
10281 +
10282 +
10283 +#include <linux/debugfs.h>
10284 +#include "vchiq_core.h"
10285 +#include "vchiq_arm.h"
10286 +#include "vchiq_debugfs.h"
10287 +
10288 +#ifdef CONFIG_DEBUG_FS
10289 +
10290 +/****************************************************************************
10291 +*
10292 +*   log category entries
10293 +*
10294 +***************************************************************************/
10295 +#define DEBUGFS_WRITE_BUF_SIZE 256
10296 +
10297 +#define VCHIQ_LOG_ERROR_STR   "error"
10298 +#define VCHIQ_LOG_WARNING_STR "warning"
10299 +#define VCHIQ_LOG_INFO_STR    "info"
10300 +#define VCHIQ_LOG_TRACE_STR   "trace"
10301 +
10302 +
10303 +/* Top-level debug info */
10304 +struct vchiq_debugfs_info {
10305 +       /* Global 'vchiq' debugfs entry used by all instances */
10306 +       struct dentry *vchiq_cfg_dir;
10307 +
10308 +       /* one entry per client process */
10309 +       struct dentry *clients;
10310 +
10311 +       /* log categories */
10312 +       struct dentry *log_categories;
10313 +};
10314 +
10315 +static struct vchiq_debugfs_info debugfs_info;
10316 +
10317 +/* Log category debugfs entries */
10318 +struct vchiq_debugfs_log_entry {
10319 +       const char *name;
10320 +       int *plevel;
10321 +       struct dentry *dir;
10322 +};
10323 +
10324 +static struct vchiq_debugfs_log_entry vchiq_debugfs_log_entries[] = {
10325 +       { "core", &vchiq_core_log_level },
10326 +       { "msg",  &vchiq_core_msg_log_level },
10327 +       { "sync", &vchiq_sync_log_level },
10328 +       { "susp", &vchiq_susp_log_level },
10329 +       { "arm",  &vchiq_arm_log_level },
10330 +};
10331 +static int n_log_entries =
10332 +       sizeof(vchiq_debugfs_log_entries)/sizeof(vchiq_debugfs_log_entries[0]);
10333 +
10334 +
10335 +static struct dentry *vchiq_clients_top(void);
10336 +static struct dentry *vchiq_debugfs_top(void);
10337 +
10338 +static int debugfs_log_show(struct seq_file *f, void *offset)
10339 +{
10340 +       int *levp = f->private;
10341 +       char *log_value = NULL;
10342 +
10343 +       switch (*levp) {
10344 +       case VCHIQ_LOG_ERROR:
10345 +               log_value = VCHIQ_LOG_ERROR_STR;
10346 +               break;
10347 +       case VCHIQ_LOG_WARNING:
10348 +               log_value = VCHIQ_LOG_WARNING_STR;
10349 +               break;
10350 +       case VCHIQ_LOG_INFO:
10351 +               log_value = VCHIQ_LOG_INFO_STR;
10352 +               break;
10353 +       case VCHIQ_LOG_TRACE:
10354 +               log_value = VCHIQ_LOG_TRACE_STR;
10355 +               break;
10356 +       default:
10357 +               break;
10358 +       }
10359 +
10360 +       seq_printf(f, "%s\n", log_value ? log_value : "(null)");
10361 +
10362 +       return 0;
10363 +}
10364 +
10365 +static int debugfs_log_open(struct inode *inode, struct file *file)
10366 +{
10367 +       return single_open(file, debugfs_log_show, inode->i_private);
10368 +}
10369 +
10370 +static int debugfs_log_write(struct file *file,
10371 +       const char __user *buffer,
10372 +       size_t count, loff_t *ppos)
10373 +{
10374 +       struct seq_file *f = (struct seq_file *)file->private_data;
10375 +       int *levp = f->private;
10376 +       char kbuf[DEBUGFS_WRITE_BUF_SIZE + 1];
10377 +
10378 +       memset(kbuf, 0, DEBUGFS_WRITE_BUF_SIZE + 1);
10379 +       if (count >= DEBUGFS_WRITE_BUF_SIZE)
10380 +               count = DEBUGFS_WRITE_BUF_SIZE;
10381 +
10382 +       if (copy_from_user(kbuf, buffer, count) != 0)
10383 +               return -EFAULT;
10384 +       kbuf[count - 1] = 0;
10385 +
10386 +       if (strncmp("error", kbuf, strlen("error")) == 0)
10387 +               *levp = VCHIQ_LOG_ERROR;
10388 +       else if (strncmp("warning", kbuf, strlen("warning")) == 0)
10389 +               *levp = VCHIQ_LOG_WARNING;
10390 +       else if (strncmp("info", kbuf, strlen("info")) == 0)
10391 +               *levp = VCHIQ_LOG_INFO;
10392 +       else if (strncmp("trace", kbuf, strlen("trace")) == 0)
10393 +               *levp = VCHIQ_LOG_TRACE;
10394 +       else
10395 +               *levp = VCHIQ_LOG_DEFAULT;
10396 +
10397 +       *ppos += count;
10398 +
10399 +       return count;
10400 +}
10401 +
10402 +static const struct file_operations debugfs_log_fops = {
10403 +       .owner          = THIS_MODULE,
10404 +       .open           = debugfs_log_open,
10405 +       .write          = debugfs_log_write,
10406 +       .read           = seq_read,
10407 +       .llseek         = seq_lseek,
10408 +       .release        = single_release,
10409 +};
10410 +
10411 +/* create an entry under <debugfs>/vchiq/log for each log category */
10412 +static int vchiq_debugfs_create_log_entries(struct dentry *top)
10413 +{
10414 +       struct dentry *dir;
10415 +       size_t i;
10416 +       int ret = 0;
10417 +       dir = debugfs_create_dir("log", vchiq_debugfs_top());
10418 +       if (!dir)
10419 +               return -ENOMEM;
10420 +       debugfs_info.log_categories = dir;
10421 +
10422 +       for (i = 0; i < n_log_entries; i++) {
10423 +               void *levp = (void *)vchiq_debugfs_log_entries[i].plevel;
10424 +               dir = debugfs_create_file(vchiq_debugfs_log_entries[i].name,
10425 +                                         0644,
10426 +                                         debugfs_info.log_categories,
10427 +                                         levp,
10428 +                                         &debugfs_log_fops);
10429 +               if (!dir) {
10430 +                       ret = -ENOMEM;
10431 +                       break;
10432 +               }
10433 +
10434 +               vchiq_debugfs_log_entries[i].dir = dir;
10435 +       }
10436 +       return ret;
10437 +}
10438 +
10439 +static int debugfs_usecount_show(struct seq_file *f, void *offset)
10440 +{
10441 +       VCHIQ_INSTANCE_T instance = f->private;
10442 +       int use_count;
10443 +
10444 +       use_count = vchiq_instance_get_use_count(instance);
10445 +       seq_printf(f, "%d\n", use_count);
10446 +
10447 +       return 0;
10448 +}
10449 +
10450 +static int debugfs_usecount_open(struct inode *inode, struct file *file)
10451 +{
10452 +       return single_open(file, debugfs_usecount_show, inode->i_private);
10453 +}
10454 +
10455 +static const struct file_operations debugfs_usecount_fops = {
10456 +       .owner          = THIS_MODULE,
10457 +       .open           = debugfs_usecount_open,
10458 +       .read           = seq_read,
10459 +       .llseek         = seq_lseek,
10460 +       .release        = single_release,
10461 +};
10462 +
10463 +static int debugfs_trace_show(struct seq_file *f, void *offset)
10464 +{
10465 +       VCHIQ_INSTANCE_T instance = f->private;
10466 +       int trace;
10467 +
10468 +       trace = vchiq_instance_get_trace(instance);
10469 +       seq_printf(f, "%s\n", trace ? "Y" : "N");
10470 +
10471 +       return 0;
10472 +}
10473 +
10474 +static int debugfs_trace_open(struct inode *inode, struct file *file)
10475 +{
10476 +       return single_open(file, debugfs_trace_show, inode->i_private);
10477 +}
10478 +
10479 +static int debugfs_trace_write(struct file *file,
10480 +       const char __user *buffer,
10481 +       size_t count, loff_t *ppos)
10482 +{
10483 +       struct seq_file *f = (struct seq_file *)file->private_data;
10484 +       VCHIQ_INSTANCE_T instance = f->private;
10485 +       char firstchar;
10486 +
10487 +       if (copy_from_user(&firstchar, buffer, 1) != 0)
10488 +               return -EFAULT;
10489 +
10490 +       switch (firstchar) {
10491 +       case 'Y':
10492 +       case 'y':
10493 +       case '1':
10494 +               vchiq_instance_set_trace(instance, 1);
10495 +               break;
10496 +       case 'N':
10497 +       case 'n':
10498 +       case '0':
10499 +               vchiq_instance_set_trace(instance, 0);
10500 +               break;
10501 +       default:
10502 +               break;
10503 +       }
10504 +
10505 +       *ppos += count;
10506 +
10507 +       return count;
10508 +}
10509 +
10510 +static const struct file_operations debugfs_trace_fops = {
10511 +       .owner          = THIS_MODULE,
10512 +       .open           = debugfs_trace_open,
10513 +       .write          = debugfs_trace_write,
10514 +       .read           = seq_read,
10515 +       .llseek         = seq_lseek,
10516 +       .release        = single_release,
10517 +};
10518 +
10519 +/* add an instance (process) to the debugfs entries */
10520 +int vchiq_debugfs_add_instance(VCHIQ_INSTANCE_T instance)
10521 +{
10522 +       char pidstr[16];
10523 +       struct dentry *top, *use_count, *trace;
10524 +       struct dentry *clients = vchiq_clients_top();
10525 +
10526 +       snprintf(pidstr, sizeof(pidstr), "%d",
10527 +                vchiq_instance_get_pid(instance));
10528 +
10529 +       top = debugfs_create_dir(pidstr, clients);
10530 +       if (!top)
10531 +               goto fail_top;
10532 +
10533 +       use_count = debugfs_create_file("use_count",
10534 +                                       0444, top,
10535 +                                       instance,
10536 +                                       &debugfs_usecount_fops);
10537 +       if (!use_count)
10538 +               goto fail_use_count;
10539 +
10540 +       trace = debugfs_create_file("trace",
10541 +                                   0644, top,
10542 +                                   instance,
10543 +                                   &debugfs_trace_fops);
10544 +       if (!trace)
10545 +               goto fail_trace;
10546 +
10547 +       vchiq_instance_get_debugfs_node(instance)->dentry = top;
10548 +
10549 +       return 0;
10550 +
10551 +fail_trace:
10552 +       debugfs_remove(use_count);
10553 +fail_use_count:
10554 +       debugfs_remove(top);
10555 +fail_top:
10556 +       return -ENOMEM;
10557 +}
10558 +
10559 +void vchiq_debugfs_remove_instance(VCHIQ_INSTANCE_T instance)
10560 +{
10561 +       VCHIQ_DEBUGFS_NODE_T *node = vchiq_instance_get_debugfs_node(instance);
10562 +       debugfs_remove_recursive(node->dentry);
10563 +}
10564 +
10565 +
10566 +int vchiq_debugfs_init(void)
10567 +{
10568 +       BUG_ON(debugfs_info.vchiq_cfg_dir != NULL);
10569 +
10570 +       debugfs_info.vchiq_cfg_dir = debugfs_create_dir("vchiq", NULL);
10571 +       if (debugfs_info.vchiq_cfg_dir == NULL)
10572 +               goto fail;
10573 +
10574 +       debugfs_info.clients = debugfs_create_dir("clients",
10575 +                               vchiq_debugfs_top());
10576 +       if (!debugfs_info.clients)
10577 +               goto fail;
10578 +
10579 +       if (vchiq_debugfs_create_log_entries(vchiq_debugfs_top()) != 0)
10580 +               goto fail;
10581 +
10582 +       return 0;
10583 +
10584 +fail:
10585 +       vchiq_debugfs_deinit();
10586 +       vchiq_log_error(vchiq_arm_log_level,
10587 +               "%s: failed to create debugfs directory",
10588 +               __func__);
10589 +
10590 +       return -ENOMEM;
10591 +}
10592 +
10593 +/* remove all the debugfs entries */
10594 +void vchiq_debugfs_deinit(void)
10595 +{
10596 +       debugfs_remove_recursive(vchiq_debugfs_top());
10597 +}
10598 +
10599 +static struct dentry *vchiq_clients_top(void)
10600 +{
10601 +       return debugfs_info.clients;
10602 +}
10603 +
10604 +static struct dentry *vchiq_debugfs_top(void)
10605 +{
10606 +       BUG_ON(debugfs_info.vchiq_cfg_dir == NULL);
10607 +       return debugfs_info.vchiq_cfg_dir;
10608 +}
10609 +
10610 +#else /* CONFIG_DEBUG_FS */
10611 +
10612 +int vchiq_debugfs_init(void)
10613 +{
10614 +       return 0;
10615 +}
10616 +
10617 +void vchiq_debugfs_deinit(void)
10618 +{
10619 +}
10620 +
10621 +int vchiq_debugfs_add_instance(VCHIQ_INSTANCE_T instance)
10622 +{
10623 +       return 0;
10624 +}
10625 +
10626 +void vchiq_debugfs_remove_instance(VCHIQ_INSTANCE_T instance)
10627 +{
10628 +}
10629 +
10630 +#endif /* CONFIG_DEBUG_FS */
10631 --- /dev/null
10632 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_debugfs.h
10633 @@ -0,0 +1,52 @@
10634 +/**
10635 + * Copyright (c) 2014 Raspberry Pi (Trading) Ltd. All rights reserved.
10636 + *
10637 + * Redistribution and use in source and binary forms, with or without
10638 + * modification, are permitted provided that the following conditions
10639 + * are met:
10640 + * 1. Redistributions of source code must retain the above copyright
10641 + *    notice, this list of conditions, and the following disclaimer,
10642 + *    without modification.
10643 + * 2. Redistributions in binary form must reproduce the above copyright
10644 + *    notice, this list of conditions and the following disclaimer in the
10645 + *    documentation and/or other materials provided with the distribution.
10646 + * 3. The names of the above-listed copyright holders may not be used
10647 + *    to endorse or promote products derived from this software without
10648 + *    specific prior written permission.
10649 + *
10650 + * ALTERNATIVELY, this software may be distributed under the terms of the
10651 + * GNU General Public License ("GPL") version 2, as published by the Free
10652 + * Software Foundation.
10653 + *
10654 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
10655 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
10656 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
10657 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
10658 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
10659 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
10660 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
10661 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
10662 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
10663 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
10664 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
10665 + */
10666 +
10667 +#ifndef VCHIQ_DEBUGFS_H
10668 +#define VCHIQ_DEBUGFS_H
10669 +
10670 +#include "vchiq_core.h"
10671 +
10672 +typedef struct vchiq_debugfs_node_struct
10673 +{
10674 +    struct dentry *dentry;
10675 +} VCHIQ_DEBUGFS_NODE_T;
10676 +
10677 +int vchiq_debugfs_init(void);
10678 +
10679 +void vchiq_debugfs_deinit(void);
10680 +
10681 +int vchiq_debugfs_add_instance(VCHIQ_INSTANCE_T instance);
10682 +
10683 +void vchiq_debugfs_remove_instance(VCHIQ_INSTANCE_T instance);
10684 +
10685 +#endif /* VCHIQ_DEBUGFS_H */
10686 --- /dev/null
10687 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_genversion
10688 @@ -0,0 +1,87 @@
10689 +#!/usr/bin/perl -w
10690 +
10691 +use strict;
10692 +
10693 +#
10694 +# Generate a version from available information
10695 +#
10696 +
10697 +my $prefix = shift @ARGV;
10698 +my $root = shift @ARGV;
10699 +
10700 +
10701 +if ( not defined $root ) {
10702 +       die "usage: $0 prefix root-dir\n";
10703 +}
10704 +
10705 +if ( ! -d $root ) {
10706 +       die "root directory $root not found\n";
10707 +}
10708 +
10709 +my $version = "unknown";
10710 +my $tainted = "";
10711 +
10712 +if ( -d "$root/.git" ) {
10713 +       # attempt to work out git version. only do so
10714 +       # on a linux build host, as cygwin builds are
10715 +       # already slow enough
10716 +
10717 +       if ( -f "/usr/bin/git" || -f "/usr/local/bin/git" ) {
10718 +               if (not open(F, "git --git-dir $root/.git rev-parse --verify HEAD|")) {
10719 +                       $version = "no git version";
10720 +               }
10721 +               else {
10722 +                       $version = <F>;
10723 +                       $version =~ s/[ \r\n]*$//;     # chomp may not be enough (cygwin).
10724 +                       $version =~ s/^[ \r\n]*//;     # chomp may not be enough (cygwin).
10725 +               }
10726 +
10727 +               if (open(G, "git --git-dir $root/.git status --porcelain|")) {
10728 +                       $tainted = <G>;
10729 +                       $tainted =~ s/[ \r\n]*$//;     # chomp may not be enough (cygwin).
10730 +                       $tainted =~ s/^[ \r\n]*//;     # chomp may not be enough (cygwin).
10731 +                       if (length $tainted) {
10732 +                       $version = join ' ', $version, "(tainted)";
10733 +               }
10734 +               else {
10735 +                       $version = join ' ', $version, "(clean)";
10736 +         }
10737 +               }
10738 +       }
10739 +}
10740 +
10741 +my $hostname = `hostname`;
10742 +$hostname =~ s/[ \r\n]*$//;     # chomp may not be enough (cygwin).
10743 +$hostname =~ s/^[ \r\n]*//;     # chomp may not be enough (cygwin).
10744 +
10745 +
10746 +print STDERR "Version $version\n";
10747 +print <<EOF;
10748 +#include "${prefix}_build_info.h"
10749 +#include <linux/broadcom/vc_debug_sym.h>
10750 +
10751 +VC_DEBUG_DECLARE_STRING_VAR( ${prefix}_build_hostname, "$hostname" );
10752 +VC_DEBUG_DECLARE_STRING_VAR( ${prefix}_build_version, "$version" );
10753 +VC_DEBUG_DECLARE_STRING_VAR( ${prefix}_build_time,    __TIME__ );
10754 +VC_DEBUG_DECLARE_STRING_VAR( ${prefix}_build_date,    __DATE__ );
10755 +
10756 +const char *vchiq_get_build_hostname( void )
10757 +{
10758 +   return vchiq_build_hostname;
10759 +}
10760 +
10761 +const char *vchiq_get_build_version( void )
10762 +{
10763 +   return vchiq_build_version;
10764 +}
10765 +
10766 +const char *vchiq_get_build_date( void )
10767 +{
10768 +   return vchiq_build_date;
10769 +}
10770 +
10771 +const char *vchiq_get_build_time( void )
10772 +{
10773 +   return vchiq_build_time;
10774 +}
10775 +EOF
10776 --- /dev/null
10777 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_if.h
10778 @@ -0,0 +1,189 @@
10779 +/**
10780 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
10781 + *
10782 + * Redistribution and use in source and binary forms, with or without
10783 + * modification, are permitted provided that the following conditions
10784 + * are met:
10785 + * 1. Redistributions of source code must retain the above copyright
10786 + *    notice, this list of conditions, and the following disclaimer,
10787 + *    without modification.
10788 + * 2. Redistributions in binary form must reproduce the above copyright
10789 + *    notice, this list of conditions and the following disclaimer in the
10790 + *    documentation and/or other materials provided with the distribution.
10791 + * 3. The names of the above-listed copyright holders may not be used
10792 + *    to endorse or promote products derived from this software without
10793 + *    specific prior written permission.
10794 + *
10795 + * ALTERNATIVELY, this software may be distributed under the terms of the
10796 + * GNU General Public License ("GPL") version 2, as published by the Free
10797 + * Software Foundation.
10798 + *
10799 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
10800 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
10801 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
10802 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
10803 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
10804 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
10805 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
10806 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
10807 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
10808 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
10809 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
10810 + */
10811 +
10812 +#ifndef VCHIQ_IF_H
10813 +#define VCHIQ_IF_H
10814 +
10815 +#include "interface/vchi/vchi_mh.h"
10816 +
10817 +#define VCHIQ_SERVICE_HANDLE_INVALID 0
10818 +
10819 +#define VCHIQ_SLOT_SIZE     4096
10820 +#define VCHIQ_MAX_MSG_SIZE  (VCHIQ_SLOT_SIZE - sizeof(VCHIQ_HEADER_T))
10821 +#define VCHIQ_CHANNEL_SIZE  VCHIQ_MAX_MSG_SIZE /* For backwards compatibility */
10822 +
10823 +#define VCHIQ_MAKE_FOURCC(x0, x1, x2, x3) \
10824 +                       (((x0) << 24) | ((x1) << 16) | ((x2) << 8) | (x3))
10825 +#define VCHIQ_GET_SERVICE_USERDATA(service) vchiq_get_service_userdata(service)
10826 +#define VCHIQ_GET_SERVICE_FOURCC(service)   vchiq_get_service_fourcc(service)
10827 +
10828 +typedef enum {
10829 +       VCHIQ_SERVICE_OPENED,         /* service, -, -             */
10830 +       VCHIQ_SERVICE_CLOSED,         /* service, -, -             */
10831 +       VCHIQ_MESSAGE_AVAILABLE,      /* service, header, -        */
10832 +       VCHIQ_BULK_TRANSMIT_DONE,     /* service, -, bulk_userdata */
10833 +       VCHIQ_BULK_RECEIVE_DONE,      /* service, -, bulk_userdata */
10834 +       VCHIQ_BULK_TRANSMIT_ABORTED,  /* service, -, bulk_userdata */
10835 +       VCHIQ_BULK_RECEIVE_ABORTED    /* service, -, bulk_userdata */
10836 +} VCHIQ_REASON_T;
10837 +
10838 +typedef enum {
10839 +       VCHIQ_ERROR   = -1,
10840 +       VCHIQ_SUCCESS = 0,
10841 +       VCHIQ_RETRY   = 1
10842 +} VCHIQ_STATUS_T;
10843 +
10844 +typedef enum {
10845 +       VCHIQ_BULK_MODE_CALLBACK,
10846 +       VCHIQ_BULK_MODE_BLOCKING,
10847 +       VCHIQ_BULK_MODE_NOCALLBACK,
10848 +       VCHIQ_BULK_MODE_WAITING         /* Reserved for internal use */
10849 +} VCHIQ_BULK_MODE_T;
10850 +
10851 +typedef enum {
10852 +       VCHIQ_SERVICE_OPTION_AUTOCLOSE,
10853 +       VCHIQ_SERVICE_OPTION_SLOT_QUOTA,
10854 +       VCHIQ_SERVICE_OPTION_MESSAGE_QUOTA,
10855 +       VCHIQ_SERVICE_OPTION_SYNCHRONOUS,
10856 +       VCHIQ_SERVICE_OPTION_TRACE
10857 +} VCHIQ_SERVICE_OPTION_T;
10858 +
10859 +typedef struct vchiq_header_struct {
10860 +       /* The message identifier - opaque to applications. */
10861 +       int msgid;
10862 +
10863 +       /* Size of message data. */
10864 +       unsigned int size;
10865 +
10866 +       char data[0];           /* message */
10867 +} VCHIQ_HEADER_T;
10868 +
10869 +typedef struct {
10870 +       const void *data;
10871 +       unsigned int size;
10872 +} VCHIQ_ELEMENT_T;
10873 +
10874 +typedef unsigned int VCHIQ_SERVICE_HANDLE_T;
10875 +
10876 +typedef VCHIQ_STATUS_T (*VCHIQ_CALLBACK_T)(VCHIQ_REASON_T, VCHIQ_HEADER_T *,
10877 +       VCHIQ_SERVICE_HANDLE_T, void *);
10878 +
10879 +typedef struct vchiq_service_base_struct {
10880 +       int fourcc;
10881 +       VCHIQ_CALLBACK_T callback;
10882 +       void *userdata;
10883 +} VCHIQ_SERVICE_BASE_T;
10884 +
10885 +typedef struct vchiq_service_params_struct {
10886 +       int fourcc;
10887 +       VCHIQ_CALLBACK_T callback;
10888 +       void *userdata;
10889 +       short version;       /* Increment for non-trivial changes */
10890 +       short version_min;   /* Update for incompatible changes */
10891 +} VCHIQ_SERVICE_PARAMS_T;
10892 +
10893 +typedef struct vchiq_config_struct {
10894 +       unsigned int max_msg_size;
10895 +       unsigned int bulk_threshold; /* The message size above which it
10896 +                                       is better to use a bulk transfer
10897 +                                       (<= max_msg_size) */
10898 +       unsigned int max_outstanding_bulks;
10899 +       unsigned int max_services;
10900 +       short version;      /* The version of VCHIQ */
10901 +       short version_min;  /* The minimum compatible version of VCHIQ */
10902 +} VCHIQ_CONFIG_T;
10903 +
10904 +typedef struct vchiq_instance_struct *VCHIQ_INSTANCE_T;
10905 +typedef void (*VCHIQ_REMOTE_USE_CALLBACK_T)(void *cb_arg);
10906 +
10907 +extern VCHIQ_STATUS_T vchiq_initialise(VCHIQ_INSTANCE_T *pinstance);
10908 +extern VCHIQ_STATUS_T vchiq_shutdown(VCHIQ_INSTANCE_T instance);
10909 +extern VCHIQ_STATUS_T vchiq_connect(VCHIQ_INSTANCE_T instance);
10910 +extern VCHIQ_STATUS_T vchiq_add_service(VCHIQ_INSTANCE_T instance,
10911 +       const VCHIQ_SERVICE_PARAMS_T *params,
10912 +       VCHIQ_SERVICE_HANDLE_T *pservice);
10913 +extern VCHIQ_STATUS_T vchiq_open_service(VCHIQ_INSTANCE_T instance,
10914 +       const VCHIQ_SERVICE_PARAMS_T *params,
10915 +       VCHIQ_SERVICE_HANDLE_T *pservice);
10916 +extern VCHIQ_STATUS_T vchiq_close_service(VCHIQ_SERVICE_HANDLE_T service);
10917 +extern VCHIQ_STATUS_T vchiq_remove_service(VCHIQ_SERVICE_HANDLE_T service);
10918 +extern VCHIQ_STATUS_T vchiq_use_service(VCHIQ_SERVICE_HANDLE_T service);
10919 +extern VCHIQ_STATUS_T vchiq_use_service_no_resume(
10920 +       VCHIQ_SERVICE_HANDLE_T service);
10921 +extern VCHIQ_STATUS_T vchiq_release_service(VCHIQ_SERVICE_HANDLE_T service);
10922 +
10923 +extern VCHIQ_STATUS_T vchiq_queue_message(VCHIQ_SERVICE_HANDLE_T service,
10924 +       const VCHIQ_ELEMENT_T *elements, unsigned int count);
10925 +extern void           vchiq_release_message(VCHIQ_SERVICE_HANDLE_T service,
10926 +       VCHIQ_HEADER_T *header);
10927 +extern VCHIQ_STATUS_T vchiq_queue_bulk_transmit(VCHIQ_SERVICE_HANDLE_T service,
10928 +       const void *data, unsigned int size, void *userdata);
10929 +extern VCHIQ_STATUS_T vchiq_queue_bulk_receive(VCHIQ_SERVICE_HANDLE_T service,
10930 +       void *data, unsigned int size, void *userdata);
10931 +extern VCHIQ_STATUS_T vchiq_queue_bulk_transmit_handle(
10932 +       VCHIQ_SERVICE_HANDLE_T service, VCHI_MEM_HANDLE_T handle,
10933 +       const void *offset, unsigned int size, void *userdata);
10934 +extern VCHIQ_STATUS_T vchiq_queue_bulk_receive_handle(
10935 +       VCHIQ_SERVICE_HANDLE_T service, VCHI_MEM_HANDLE_T handle,
10936 +       void *offset, unsigned int size, void *userdata);
10937 +extern VCHIQ_STATUS_T vchiq_bulk_transmit(VCHIQ_SERVICE_HANDLE_T service,
10938 +       const void *data, unsigned int size, void *userdata,
10939 +       VCHIQ_BULK_MODE_T mode);
10940 +extern VCHIQ_STATUS_T vchiq_bulk_receive(VCHIQ_SERVICE_HANDLE_T service,
10941 +       void *data, unsigned int size, void *userdata,
10942 +       VCHIQ_BULK_MODE_T mode);
10943 +extern VCHIQ_STATUS_T vchiq_bulk_transmit_handle(VCHIQ_SERVICE_HANDLE_T service,
10944 +       VCHI_MEM_HANDLE_T handle, const void *offset, unsigned int size,
10945 +       void *userdata, VCHIQ_BULK_MODE_T mode);
10946 +extern VCHIQ_STATUS_T vchiq_bulk_receive_handle(VCHIQ_SERVICE_HANDLE_T service,
10947 +       VCHI_MEM_HANDLE_T handle, void *offset, unsigned int size,
10948 +       void *userdata, VCHIQ_BULK_MODE_T mode);
10949 +extern int   vchiq_get_client_id(VCHIQ_SERVICE_HANDLE_T service);
10950 +extern void *vchiq_get_service_userdata(VCHIQ_SERVICE_HANDLE_T service);
10951 +extern int   vchiq_get_service_fourcc(VCHIQ_SERVICE_HANDLE_T service);
10952 +extern VCHIQ_STATUS_T vchiq_get_config(VCHIQ_INSTANCE_T instance,
10953 +       int config_size, VCHIQ_CONFIG_T *pconfig);
10954 +extern VCHIQ_STATUS_T vchiq_set_service_option(VCHIQ_SERVICE_HANDLE_T service,
10955 +       VCHIQ_SERVICE_OPTION_T option, int value);
10956 +
10957 +extern VCHIQ_STATUS_T vchiq_remote_use(VCHIQ_INSTANCE_T instance,
10958 +       VCHIQ_REMOTE_USE_CALLBACK_T callback, void *cb_arg);
10959 +extern VCHIQ_STATUS_T vchiq_remote_release(VCHIQ_INSTANCE_T instance);
10960 +
10961 +extern VCHIQ_STATUS_T vchiq_dump_phys_mem(VCHIQ_SERVICE_HANDLE_T service,
10962 +       void *ptr, size_t num_bytes);
10963 +
10964 +extern VCHIQ_STATUS_T vchiq_get_peer_version(VCHIQ_SERVICE_HANDLE_T handle,
10965 +      short *peer_version);
10966 +
10967 +#endif /* VCHIQ_IF_H */
10968 --- /dev/null
10969 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_ioctl.h
10970 @@ -0,0 +1,131 @@
10971 +/**
10972 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
10973 + *
10974 + * Redistribution and use in source and binary forms, with or without
10975 + * modification, are permitted provided that the following conditions
10976 + * are met:
10977 + * 1. Redistributions of source code must retain the above copyright
10978 + *    notice, this list of conditions, and the following disclaimer,
10979 + *    without modification.
10980 + * 2. Redistributions in binary form must reproduce the above copyright
10981 + *    notice, this list of conditions and the following disclaimer in the
10982 + *    documentation and/or other materials provided with the distribution.
10983 + * 3. The names of the above-listed copyright holders may not be used
10984 + *    to endorse or promote products derived from this software without
10985 + *    specific prior written permission.
10986 + *
10987 + * ALTERNATIVELY, this software may be distributed under the terms of the
10988 + * GNU General Public License ("GPL") version 2, as published by the Free
10989 + * Software Foundation.
10990 + *
10991 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
10992 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
10993 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
10994 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
10995 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
10996 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
10997 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
10998 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
10999 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
11000 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
11001 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
11002 + */
11003 +
11004 +#ifndef VCHIQ_IOCTLS_H
11005 +#define VCHIQ_IOCTLS_H
11006 +
11007 +#include <linux/ioctl.h>
11008 +#include "vchiq_if.h"
11009 +
11010 +#define VCHIQ_IOC_MAGIC 0xc4
11011 +#define VCHIQ_INVALID_HANDLE (~0)
11012 +
11013 +typedef struct {
11014 +       VCHIQ_SERVICE_PARAMS_T params;
11015 +       int is_open;
11016 +       int is_vchi;
11017 +       unsigned int handle;       /* OUT */
11018 +} VCHIQ_CREATE_SERVICE_T;
11019 +
11020 +typedef struct {
11021 +       unsigned int handle;
11022 +       unsigned int count;
11023 +       const VCHIQ_ELEMENT_T *elements;
11024 +} VCHIQ_QUEUE_MESSAGE_T;
11025 +
11026 +typedef struct {
11027 +       unsigned int handle;
11028 +       void *data;
11029 +       unsigned int size;
11030 +       void *userdata;
11031 +       VCHIQ_BULK_MODE_T mode;
11032 +} VCHIQ_QUEUE_BULK_TRANSFER_T;
11033 +
11034 +typedef struct {
11035 +       VCHIQ_REASON_T reason;
11036 +       VCHIQ_HEADER_T *header;
11037 +       void *service_userdata;
11038 +       void *bulk_userdata;
11039 +} VCHIQ_COMPLETION_DATA_T;
11040 +
11041 +typedef struct {
11042 +       unsigned int count;
11043 +       VCHIQ_COMPLETION_DATA_T *buf;
11044 +       unsigned int msgbufsize;
11045 +       unsigned int msgbufcount; /* IN/OUT */
11046 +       void **msgbufs;
11047 +} VCHIQ_AWAIT_COMPLETION_T;
11048 +
11049 +typedef struct {
11050 +       unsigned int handle;
11051 +       int blocking;
11052 +       unsigned int bufsize;
11053 +       void *buf;
11054 +} VCHIQ_DEQUEUE_MESSAGE_T;
11055 +
11056 +typedef struct {
11057 +       unsigned int config_size;
11058 +       VCHIQ_CONFIG_T *pconfig;
11059 +} VCHIQ_GET_CONFIG_T;
11060 +
11061 +typedef struct {
11062 +       unsigned int handle;
11063 +       VCHIQ_SERVICE_OPTION_T option;
11064 +       int value;
11065 +} VCHIQ_SET_SERVICE_OPTION_T;
11066 +
11067 +typedef struct {
11068 +       void     *virt_addr;
11069 +       size_t    num_bytes;
11070 +} VCHIQ_DUMP_MEM_T;
11071 +
11072 +#define VCHIQ_IOC_CONNECT              _IO(VCHIQ_IOC_MAGIC,   0)
11073 +#define VCHIQ_IOC_SHUTDOWN             _IO(VCHIQ_IOC_MAGIC,   1)
11074 +#define VCHIQ_IOC_CREATE_SERVICE \
11075 +       _IOWR(VCHIQ_IOC_MAGIC, 2, VCHIQ_CREATE_SERVICE_T)
11076 +#define VCHIQ_IOC_REMOVE_SERVICE       _IO(VCHIQ_IOC_MAGIC,   3)
11077 +#define VCHIQ_IOC_QUEUE_MESSAGE \
11078 +       _IOW(VCHIQ_IOC_MAGIC,  4, VCHIQ_QUEUE_MESSAGE_T)
11079 +#define VCHIQ_IOC_QUEUE_BULK_TRANSMIT \
11080 +       _IOWR(VCHIQ_IOC_MAGIC, 5, VCHIQ_QUEUE_BULK_TRANSFER_T)
11081 +#define VCHIQ_IOC_QUEUE_BULK_RECEIVE \
11082 +       _IOWR(VCHIQ_IOC_MAGIC, 6, VCHIQ_QUEUE_BULK_TRANSFER_T)
11083 +#define VCHIQ_IOC_AWAIT_COMPLETION \
11084 +       _IOWR(VCHIQ_IOC_MAGIC, 7, VCHIQ_AWAIT_COMPLETION_T)
11085 +#define VCHIQ_IOC_DEQUEUE_MESSAGE \
11086 +       _IOWR(VCHIQ_IOC_MAGIC, 8, VCHIQ_DEQUEUE_MESSAGE_T)
11087 +#define VCHIQ_IOC_GET_CLIENT_ID        _IO(VCHIQ_IOC_MAGIC,   9)
11088 +#define VCHIQ_IOC_GET_CONFIG \
11089 +       _IOWR(VCHIQ_IOC_MAGIC, 10, VCHIQ_GET_CONFIG_T)
11090 +#define VCHIQ_IOC_CLOSE_SERVICE        _IO(VCHIQ_IOC_MAGIC,   11)
11091 +#define VCHIQ_IOC_USE_SERVICE          _IO(VCHIQ_IOC_MAGIC,   12)
11092 +#define VCHIQ_IOC_RELEASE_SERVICE      _IO(VCHIQ_IOC_MAGIC,   13)
11093 +#define VCHIQ_IOC_SET_SERVICE_OPTION \
11094 +       _IOW(VCHIQ_IOC_MAGIC,  14, VCHIQ_SET_SERVICE_OPTION_T)
11095 +#define VCHIQ_IOC_DUMP_PHYS_MEM \
11096 +       _IOW(VCHIQ_IOC_MAGIC,  15, VCHIQ_DUMP_MEM_T)
11097 +#define VCHIQ_IOC_LIB_VERSION          _IO(VCHIQ_IOC_MAGIC,   16)
11098 +#define VCHIQ_IOC_CLOSE_DELIVERED      _IO(VCHIQ_IOC_MAGIC,   17)
11099 +#define VCHIQ_IOC_MAX                  17
11100 +
11101 +#endif
11102 --- /dev/null
11103 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_kern_lib.c
11104 @@ -0,0 +1,456 @@
11105 +/**
11106 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
11107 + *
11108 + * Redistribution and use in source and binary forms, with or without
11109 + * modification, are permitted provided that the following conditions
11110 + * are met:
11111 + * 1. Redistributions of source code must retain the above copyright
11112 + *    notice, this list of conditions, and the following disclaimer,
11113 + *    without modification.
11114 + * 2. Redistributions in binary form must reproduce the above copyright
11115 + *    notice, this list of conditions and the following disclaimer in the
11116 + *    documentation and/or other materials provided with the distribution.
11117 + * 3. The names of the above-listed copyright holders may not be used
11118 + *    to endorse or promote products derived from this software without
11119 + *    specific prior written permission.
11120 + *
11121 + * ALTERNATIVELY, this software may be distributed under the terms of the
11122 + * GNU General Public License ("GPL") version 2, as published by the Free
11123 + * Software Foundation.
11124 + *
11125 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
11126 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
11127 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
11128 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
11129 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
11130 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
11131 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
11132 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
11133 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
11134 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
11135 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
11136 + */
11137 +
11138 +/* ---- Include Files ---------------------------------------------------- */
11139 +
11140 +#include <linux/kernel.h>
11141 +#include <linux/module.h>
11142 +#include <linux/mutex.h>
11143 +
11144 +#include "vchiq_core.h"
11145 +#include "vchiq_arm.h"
11146 +
11147 +/* ---- Public Variables ------------------------------------------------- */
11148 +
11149 +/* ---- Private Constants and Types -------------------------------------- */
11150 +
11151 +struct bulk_waiter_node {
11152 +       struct bulk_waiter bulk_waiter;
11153 +       int pid;
11154 +       struct list_head list;
11155 +};
11156 +
11157 +struct vchiq_instance_struct {
11158 +       VCHIQ_STATE_T *state;
11159 +
11160 +       int connected;
11161 +
11162 +       struct list_head bulk_waiter_list;
11163 +       struct mutex bulk_waiter_list_mutex;
11164 +};
11165 +
11166 +static VCHIQ_STATUS_T
11167 +vchiq_blocking_bulk_transfer(VCHIQ_SERVICE_HANDLE_T handle, void *data,
11168 +       unsigned int size, VCHIQ_BULK_DIR_T dir);
11169 +
11170 +/****************************************************************************
11171 +*
11172 +*   vchiq_initialise
11173 +*
11174 +***************************************************************************/
11175 +#define VCHIQ_INIT_RETRIES 10
11176 +VCHIQ_STATUS_T vchiq_initialise(VCHIQ_INSTANCE_T *instanceOut)
11177 +{
11178 +       VCHIQ_STATUS_T status = VCHIQ_ERROR;
11179 +       VCHIQ_STATE_T *state;
11180 +       VCHIQ_INSTANCE_T instance = NULL;
11181 +        int i;
11182 +
11183 +       vchiq_log_trace(vchiq_core_log_level, "%s called", __func__);
11184 +
11185 +        /* VideoCore may not be ready due to boot up timing.
11186 +           It may never be ready if kernel and firmware are mismatched, so don't block forever. */
11187 +        for (i=0; i<VCHIQ_INIT_RETRIES; i++) {
11188 +               state = vchiq_get_state();
11189 +               if (state)
11190 +                       break;
11191 +               udelay(500);
11192 +       }
11193 +       if (i==VCHIQ_INIT_RETRIES) {
11194 +               vchiq_log_error(vchiq_core_log_level,
11195 +                       "%s: videocore not initialized\n", __func__);
11196 +               goto failed;
11197 +       } else if (i>0) {
11198 +               vchiq_log_warning(vchiq_core_log_level,
11199 +                       "%s: videocore initialized after %d retries\n", __func__, i);
11200 +       }
11201 +
11202 +       instance = kzalloc(sizeof(*instance), GFP_KERNEL);
11203 +       if (!instance) {
11204 +               vchiq_log_error(vchiq_core_log_level,
11205 +                       "%s: error allocating vchiq instance\n", __func__);
11206 +               goto failed;
11207 +       }
11208 +
11209 +       instance->connected = 0;
11210 +       instance->state = state;
11211 +       mutex_init(&instance->bulk_waiter_list_mutex);
11212 +       INIT_LIST_HEAD(&instance->bulk_waiter_list);
11213 +
11214 +       *instanceOut = instance;
11215 +
11216 +       status = VCHIQ_SUCCESS;
11217 +
11218 +failed:
11219 +       vchiq_log_trace(vchiq_core_log_level,
11220 +               "%s(%p): returning %d", __func__, instance, status);
11221 +
11222 +       return status;
11223 +}
11224 +EXPORT_SYMBOL(vchiq_initialise);
11225 +
11226 +/****************************************************************************
11227 +*
11228 +*   vchiq_shutdown
11229 +*
11230 +***************************************************************************/
11231 +
11232 +VCHIQ_STATUS_T vchiq_shutdown(VCHIQ_INSTANCE_T instance)
11233 +{
11234 +       VCHIQ_STATUS_T status;
11235 +       VCHIQ_STATE_T *state = instance->state;
11236 +
11237 +       vchiq_log_trace(vchiq_core_log_level,
11238 +               "%s(%p) called", __func__, instance);
11239 +
11240 +       if (mutex_lock_interruptible(&state->mutex) != 0)
11241 +               return VCHIQ_RETRY;
11242 +
11243 +       /* Remove all services */
11244 +       status = vchiq_shutdown_internal(state, instance);
11245 +
11246 +       mutex_unlock(&state->mutex);
11247 +
11248 +       vchiq_log_trace(vchiq_core_log_level,
11249 +               "%s(%p): returning %d", __func__, instance, status);
11250 +
11251 +       if (status == VCHIQ_SUCCESS) {
11252 +               struct list_head *pos, *next;
11253 +               list_for_each_safe(pos, next,
11254 +                               &instance->bulk_waiter_list) {
11255 +                       struct bulk_waiter_node *waiter;
11256 +                       waiter = list_entry(pos,
11257 +                                       struct bulk_waiter_node,
11258 +                                       list);
11259 +                       list_del(pos);
11260 +                       vchiq_log_info(vchiq_arm_log_level,
11261 +                                       "bulk_waiter - cleaned up %x "
11262 +                                       "for pid %d",
11263 +                                       (unsigned int)waiter, waiter->pid);
11264 +                       kfree(waiter);
11265 +               }
11266 +               kfree(instance);
11267 +       }
11268 +
11269 +       return status;
11270 +}
11271 +EXPORT_SYMBOL(vchiq_shutdown);
11272 +
11273 +/****************************************************************************
11274 +*
11275 +*   vchiq_is_connected
11276 +*
11277 +***************************************************************************/
11278 +
11279 +int vchiq_is_connected(VCHIQ_INSTANCE_T instance)
11280 +{
11281 +       return instance->connected;
11282 +}
11283 +
11284 +/****************************************************************************
11285 +*
11286 +*   vchiq_connect
11287 +*
11288 +***************************************************************************/
11289 +
11290 +VCHIQ_STATUS_T vchiq_connect(VCHIQ_INSTANCE_T instance)
11291 +{
11292 +       VCHIQ_STATUS_T status;
11293 +       VCHIQ_STATE_T *state = instance->state;
11294 +
11295 +       vchiq_log_trace(vchiq_core_log_level,
11296 +               "%s(%p) called", __func__, instance);
11297 +
11298 +       if (mutex_lock_interruptible(&state->mutex) != 0) {
11299 +               vchiq_log_trace(vchiq_core_log_level,
11300 +                       "%s: call to mutex_lock failed", __func__);
11301 +               status = VCHIQ_RETRY;
11302 +               goto failed;
11303 +       }
11304 +       status = vchiq_connect_internal(state, instance);
11305 +
11306 +       if (status == VCHIQ_SUCCESS)
11307 +               instance->connected = 1;
11308 +
11309 +       mutex_unlock(&state->mutex);
11310 +
11311 +failed:
11312 +       vchiq_log_trace(vchiq_core_log_level,
11313 +               "%s(%p): returning %d", __func__, instance, status);
11314 +
11315 +       return status;
11316 +}
11317 +EXPORT_SYMBOL(vchiq_connect);
11318 +
11319 +/****************************************************************************
11320 +*
11321 +*   vchiq_add_service
11322 +*
11323 +***************************************************************************/
11324 +
11325 +VCHIQ_STATUS_T vchiq_add_service(
11326 +       VCHIQ_INSTANCE_T              instance,
11327 +       const VCHIQ_SERVICE_PARAMS_T *params,
11328 +       VCHIQ_SERVICE_HANDLE_T       *phandle)
11329 +{
11330 +       VCHIQ_STATUS_T status;
11331 +       VCHIQ_STATE_T *state = instance->state;
11332 +       VCHIQ_SERVICE_T *service = NULL;
11333 +       int srvstate;
11334 +
11335 +       vchiq_log_trace(vchiq_core_log_level,
11336 +               "%s(%p) called", __func__, instance);
11337 +
11338 +       *phandle = VCHIQ_SERVICE_HANDLE_INVALID;
11339 +
11340 +       srvstate = vchiq_is_connected(instance)
11341 +               ? VCHIQ_SRVSTATE_LISTENING
11342 +               : VCHIQ_SRVSTATE_HIDDEN;
11343 +
11344 +       service = vchiq_add_service_internal(
11345 +               state,
11346 +               params,
11347 +               srvstate,
11348 +               instance,
11349 +               NULL);
11350 +
11351 +       if (service) {
11352 +               *phandle = service->handle;
11353 +               status = VCHIQ_SUCCESS;
11354 +       } else
11355 +               status = VCHIQ_ERROR;
11356 +
11357 +       vchiq_log_trace(vchiq_core_log_level,
11358 +               "%s(%p): returning %d", __func__, instance, status);
11359 +
11360 +       return status;
11361 +}
11362 +EXPORT_SYMBOL(vchiq_add_service);
11363 +
11364 +/****************************************************************************
11365 +*
11366 +*   vchiq_open_service
11367 +*
11368 +***************************************************************************/
11369 +
11370 +VCHIQ_STATUS_T vchiq_open_service(
11371 +       VCHIQ_INSTANCE_T              instance,
11372 +       const VCHIQ_SERVICE_PARAMS_T *params,
11373 +       VCHIQ_SERVICE_HANDLE_T       *phandle)
11374 +{
11375 +       VCHIQ_STATUS_T   status = VCHIQ_ERROR;
11376 +       VCHIQ_STATE_T   *state = instance->state;
11377 +       VCHIQ_SERVICE_T *service = NULL;
11378 +
11379 +       vchiq_log_trace(vchiq_core_log_level,
11380 +               "%s(%p) called", __func__, instance);
11381 +
11382 +       *phandle = VCHIQ_SERVICE_HANDLE_INVALID;
11383 +
11384 +       if (!vchiq_is_connected(instance))
11385 +               goto failed;
11386 +
11387 +       service = vchiq_add_service_internal(state,
11388 +               params,
11389 +               VCHIQ_SRVSTATE_OPENING,
11390 +               instance,
11391 +               NULL);
11392 +
11393 +       if (service) {
11394 +               status = vchiq_open_service_internal(service, current->pid);
11395 +               if (status == VCHIQ_SUCCESS)
11396 +                       *phandle = service->handle;
11397 +               else
11398 +                       vchiq_remove_service(service->handle);
11399 +       }
11400 +
11401 +failed:
11402 +       vchiq_log_trace(vchiq_core_log_level,
11403 +               "%s(%p): returning %d", __func__, instance, status);
11404 +
11405 +       return status;
11406 +}
11407 +EXPORT_SYMBOL(vchiq_open_service);
11408 +
11409 +VCHIQ_STATUS_T
11410 +vchiq_queue_bulk_transmit(VCHIQ_SERVICE_HANDLE_T handle,
11411 +       const void *data, unsigned int size, void *userdata)
11412 +{
11413 +       return vchiq_bulk_transfer(handle,
11414 +               VCHI_MEM_HANDLE_INVALID, (void *)data, size, userdata,
11415 +               VCHIQ_BULK_MODE_CALLBACK, VCHIQ_BULK_TRANSMIT);
11416 +}
11417 +EXPORT_SYMBOL(vchiq_queue_bulk_transmit);
11418 +
11419 +VCHIQ_STATUS_T
11420 +vchiq_queue_bulk_receive(VCHIQ_SERVICE_HANDLE_T handle, void *data,
11421 +       unsigned int size, void *userdata)
11422 +{
11423 +       return vchiq_bulk_transfer(handle,
11424 +               VCHI_MEM_HANDLE_INVALID, data, size, userdata,
11425 +               VCHIQ_BULK_MODE_CALLBACK, VCHIQ_BULK_RECEIVE);
11426 +}
11427 +EXPORT_SYMBOL(vchiq_queue_bulk_receive);
11428 +
11429 +VCHIQ_STATUS_T
11430 +vchiq_bulk_transmit(VCHIQ_SERVICE_HANDLE_T handle, const void *data,
11431 +       unsigned int size, void *userdata, VCHIQ_BULK_MODE_T mode)
11432 +{
11433 +       VCHIQ_STATUS_T status;
11434 +
11435 +       switch (mode) {
11436 +       case VCHIQ_BULK_MODE_NOCALLBACK:
11437 +       case VCHIQ_BULK_MODE_CALLBACK:
11438 +               status = vchiq_bulk_transfer(handle,
11439 +                       VCHI_MEM_HANDLE_INVALID, (void *)data, size, userdata,
11440 +                       mode, VCHIQ_BULK_TRANSMIT);
11441 +               break;
11442 +       case VCHIQ_BULK_MODE_BLOCKING:
11443 +               status = vchiq_blocking_bulk_transfer(handle,
11444 +                       (void *)data, size, VCHIQ_BULK_TRANSMIT);
11445 +               break;
11446 +       default:
11447 +               return VCHIQ_ERROR;
11448 +       }
11449 +
11450 +       return status;
11451 +}
11452 +EXPORT_SYMBOL(vchiq_bulk_transmit);
11453 +
11454 +VCHIQ_STATUS_T
11455 +vchiq_bulk_receive(VCHIQ_SERVICE_HANDLE_T handle, void *data,
11456 +       unsigned int size, void *userdata, VCHIQ_BULK_MODE_T mode)
11457 +{
11458 +       VCHIQ_STATUS_T status;
11459 +
11460 +       switch (mode) {
11461 +       case VCHIQ_BULK_MODE_NOCALLBACK:
11462 +       case VCHIQ_BULK_MODE_CALLBACK:
11463 +               status = vchiq_bulk_transfer(handle,
11464 +                       VCHI_MEM_HANDLE_INVALID, data, size, userdata,
11465 +                       mode, VCHIQ_BULK_RECEIVE);
11466 +               break;
11467 +       case VCHIQ_BULK_MODE_BLOCKING:
11468 +               status = vchiq_blocking_bulk_transfer(handle,
11469 +                       (void *)data, size, VCHIQ_BULK_RECEIVE);
11470 +               break;
11471 +       default:
11472 +               return VCHIQ_ERROR;
11473 +       }
11474 +
11475 +       return status;
11476 +}
11477 +EXPORT_SYMBOL(vchiq_bulk_receive);
11478 +
11479 +static VCHIQ_STATUS_T
11480 +vchiq_blocking_bulk_transfer(VCHIQ_SERVICE_HANDLE_T handle, void *data,
11481 +       unsigned int size, VCHIQ_BULK_DIR_T dir)
11482 +{
11483 +       VCHIQ_INSTANCE_T instance;
11484 +       VCHIQ_SERVICE_T *service;
11485 +       VCHIQ_STATUS_T status;
11486 +       struct bulk_waiter_node *waiter = NULL;
11487 +       struct list_head *pos;
11488 +
11489 +       service = find_service_by_handle(handle);
11490 +       if (!service)
11491 +               return VCHIQ_ERROR;
11492 +
11493 +       instance = service->instance;
11494 +
11495 +       unlock_service(service);
11496 +
11497 +       mutex_lock(&instance->bulk_waiter_list_mutex);
11498 +       list_for_each(pos, &instance->bulk_waiter_list) {
11499 +               if (list_entry(pos, struct bulk_waiter_node,
11500 +                               list)->pid == current->pid) {
11501 +                       waiter = list_entry(pos,
11502 +                               struct bulk_waiter_node,
11503 +                               list);
11504 +                       list_del(pos);
11505 +                       break;
11506 +               }
11507 +       }
11508 +       mutex_unlock(&instance->bulk_waiter_list_mutex);
11509 +
11510 +       if (waiter) {
11511 +               VCHIQ_BULK_T *bulk = waiter->bulk_waiter.bulk;
11512 +               if (bulk) {
11513 +                       /* This thread has an outstanding bulk transfer. */
11514 +                       if ((bulk->data != data) ||
11515 +                               (bulk->size != size)) {
11516 +                               /* This is not a retry of the previous one.
11517 +                               ** Cancel the signal when the transfer
11518 +                               ** completes. */
11519 +                               spin_lock(&bulk_waiter_spinlock);
11520 +                               bulk->userdata = NULL;
11521 +                               spin_unlock(&bulk_waiter_spinlock);
11522 +                       }
11523 +               }
11524 +       }
11525 +
11526 +       if (!waiter) {
11527 +               waiter = kzalloc(sizeof(struct bulk_waiter_node), GFP_KERNEL);
11528 +               if (!waiter) {
11529 +                       vchiq_log_error(vchiq_core_log_level,
11530 +                               "%s - out of memory", __func__);
11531 +                       return VCHIQ_ERROR;
11532 +               }
11533 +       }
11534 +
11535 +       status = vchiq_bulk_transfer(handle, VCHI_MEM_HANDLE_INVALID,
11536 +               data, size, &waiter->bulk_waiter, VCHIQ_BULK_MODE_BLOCKING,
11537 +               dir);
11538 +       if ((status != VCHIQ_RETRY) || fatal_signal_pending(current) ||
11539 +               !waiter->bulk_waiter.bulk) {
11540 +               VCHIQ_BULK_T *bulk = waiter->bulk_waiter.bulk;
11541 +               if (bulk) {
11542 +                       /* Cancel the signal when the transfer
11543 +                        ** completes. */
11544 +                       spin_lock(&bulk_waiter_spinlock);
11545 +                       bulk->userdata = NULL;
11546 +                       spin_unlock(&bulk_waiter_spinlock);
11547 +               }
11548 +               kfree(waiter);
11549 +       } else {
11550 +               waiter->pid = current->pid;
11551 +               mutex_lock(&instance->bulk_waiter_list_mutex);
11552 +               list_add(&waiter->list, &instance->bulk_waiter_list);
11553 +               mutex_unlock(&instance->bulk_waiter_list_mutex);
11554 +               vchiq_log_info(vchiq_arm_log_level,
11555 +                               "saved bulk_waiter %x for pid %d",
11556 +                               (unsigned int)waiter, current->pid);
11557 +       }
11558 +
11559 +       return status;
11560 +}
11561 --- /dev/null
11562 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_memdrv.h
11563 @@ -0,0 +1,71 @@
11564 +/**
11565 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
11566 + *
11567 + * Redistribution and use in source and binary forms, with or without
11568 + * modification, are permitted provided that the following conditions
11569 + * are met:
11570 + * 1. Redistributions of source code must retain the above copyright
11571 + *    notice, this list of conditions, and the following disclaimer,
11572 + *    without modification.
11573 + * 2. Redistributions in binary form must reproduce the above copyright
11574 + *    notice, this list of conditions and the following disclaimer in the
11575 + *    documentation and/or other materials provided with the distribution.
11576 + * 3. The names of the above-listed copyright holders may not be used
11577 + *    to endorse or promote products derived from this software without
11578 + *    specific prior written permission.
11579 + *
11580 + * ALTERNATIVELY, this software may be distributed under the terms of the
11581 + * GNU General Public License ("GPL") version 2, as published by the Free
11582 + * Software Foundation.
11583 + *
11584 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
11585 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
11586 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
11587 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
11588 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
11589 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
11590 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
11591 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
11592 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
11593 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
11594 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
11595 + */
11596 +
11597 +#ifndef VCHIQ_MEMDRV_H
11598 +#define VCHIQ_MEMDRV_H
11599 +
11600 +/* ---- Include Files ----------------------------------------------------- */
11601 +
11602 +#include <linux/kernel.h>
11603 +#include "vchiq_if.h"
11604 +
11605 +/* ---- Constants and Types ---------------------------------------------- */
11606 +
11607 +typedef struct {
11608 +        void                   *armSharedMemVirt;
11609 +        dma_addr_t              armSharedMemPhys;
11610 +        size_t                  armSharedMemSize;
11611 +
11612 +        void                   *vcSharedMemVirt;
11613 +        dma_addr_t              vcSharedMemPhys;
11614 +        size_t                  vcSharedMemSize;
11615 +} VCHIQ_SHARED_MEM_INFO_T;
11616 +
11617 +/* ---- Variable Externs ------------------------------------------------- */
11618 +
11619 +/* ---- Function Prototypes ---------------------------------------------- */
11620 +
11621 +void vchiq_get_shared_mem_info(VCHIQ_SHARED_MEM_INFO_T *info);
11622 +
11623 +VCHIQ_STATUS_T vchiq_memdrv_initialise(void);
11624 +
11625 +VCHIQ_STATUS_T vchiq_userdrv_create_instance(
11626 +       const VCHIQ_PLATFORM_DATA_T * platform_data);
11627 +
11628 +VCHIQ_STATUS_T vchiq_userdrv_suspend(
11629 +       const VCHIQ_PLATFORM_DATA_T * platform_data);
11630 +
11631 +VCHIQ_STATUS_T vchiq_userdrv_resume(
11632 +       const VCHIQ_PLATFORM_DATA_T * platform_data);
11633 +
11634 +#endif
11635 --- /dev/null
11636 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_pagelist.h
11637 @@ -0,0 +1,58 @@
11638 +/**
11639 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
11640 + *
11641 + * Redistribution and use in source and binary forms, with or without
11642 + * modification, are permitted provided that the following conditions
11643 + * are met:
11644 + * 1. Redistributions of source code must retain the above copyright
11645 + *    notice, this list of conditions, and the following disclaimer,
11646 + *    without modification.
11647 + * 2. Redistributions in binary form must reproduce the above copyright
11648 + *    notice, this list of conditions and the following disclaimer in the
11649 + *    documentation and/or other materials provided with the distribution.
11650 + * 3. The names of the above-listed copyright holders may not be used
11651 + *    to endorse or promote products derived from this software without
11652 + *    specific prior written permission.
11653 + *
11654 + * ALTERNATIVELY, this software may be distributed under the terms of the
11655 + * GNU General Public License ("GPL") version 2, as published by the Free
11656 + * Software Foundation.
11657 + *
11658 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
11659 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
11660 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
11661 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
11662 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
11663 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
11664 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
11665 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
11666 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
11667 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
11668 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
11669 + */
11670 +
11671 +#ifndef VCHIQ_PAGELIST_H
11672 +#define VCHIQ_PAGELIST_H
11673 +
11674 +#ifndef PAGE_SIZE
11675 +#define PAGE_SIZE 4096
11676 +#endif
11677 +#define CACHE_LINE_SIZE 32
11678 +#define PAGELIST_WRITE 0
11679 +#define PAGELIST_READ 1
11680 +#define PAGELIST_READ_WITH_FRAGMENTS 2
11681 +
11682 +typedef struct pagelist_struct {
11683 +       unsigned long length;
11684 +       unsigned short type;
11685 +       unsigned short offset;
11686 +       unsigned long addrs[1]; /* N.B. 12 LSBs hold the number of following
11687 +                                  pages at consecutive addresses. */
11688 +} PAGELIST_T;
11689 +
11690 +typedef struct fragments_struct {
11691 +       char headbuf[CACHE_LINE_SIZE];
11692 +       char tailbuf[CACHE_LINE_SIZE];
11693 +} FRAGMENTS_T;
11694 +
11695 +#endif /* VCHIQ_PAGELIST_H */
11696 --- /dev/null
11697 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_shim.c
11698 @@ -0,0 +1,853 @@
11699 +/**
11700 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
11701 + *
11702 + * Redistribution and use in source and binary forms, with or without
11703 + * modification, are permitted provided that the following conditions
11704 + * are met:
11705 + * 1. Redistributions of source code must retain the above copyright
11706 + *    notice, this list of conditions, and the following disclaimer,
11707 + *    without modification.
11708 + * 2. Redistributions in binary form must reproduce the above copyright
11709 + *    notice, this list of conditions and the following disclaimer in the
11710 + *    documentation and/or other materials provided with the distribution.
11711 + * 3. The names of the above-listed copyright holders may not be used
11712 + *    to endorse or promote products derived from this software without
11713 + *    specific prior written permission.
11714 + *
11715 + * ALTERNATIVELY, this software may be distributed under the terms of the
11716 + * GNU General Public License ("GPL") version 2, as published by the Free
11717 + * Software Foundation.
11718 + *
11719 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
11720 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
11721 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
11722 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
11723 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
11724 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
11725 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
11726 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
11727 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
11728 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
11729 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
11730 + */
11731 +#include <linux/module.h>
11732 +#include <linux/types.h>
11733 +
11734 +#include "interface/vchi/vchi.h"
11735 +#include "vchiq.h"
11736 +#include "vchiq_core.h"
11737 +
11738 +#include "vchiq_util.h"
11739 +
11740 +#include <stddef.h>
11741 +
11742 +#define vchiq_status_to_vchi(status) ((int32_t)status)
11743 +
11744 +typedef struct {
11745 +       VCHIQ_SERVICE_HANDLE_T handle;
11746 +
11747 +       VCHIU_QUEUE_T queue;
11748 +
11749 +       VCHI_CALLBACK_T callback;
11750 +       void *callback_param;
11751 +} SHIM_SERVICE_T;
11752 +
11753 +/* ----------------------------------------------------------------------
11754 + * return pointer to the mphi message driver function table
11755 + * -------------------------------------------------------------------- */
11756 +const VCHI_MESSAGE_DRIVER_T *
11757 +vchi_mphi_message_driver_func_table(void)
11758 +{
11759 +       return NULL;
11760 +}
11761 +
11762 +/* ----------------------------------------------------------------------
11763 + * return a pointer to the 'single' connection driver fops
11764 + * -------------------------------------------------------------------- */
11765 +const VCHI_CONNECTION_API_T *
11766 +single_get_func_table(void)
11767 +{
11768 +       return NULL;
11769 +}
11770 +
11771 +VCHI_CONNECTION_T *vchi_create_connection(
11772 +       const VCHI_CONNECTION_API_T *function_table,
11773 +       const VCHI_MESSAGE_DRIVER_T *low_level)
11774 +{
11775 +       (void)function_table;
11776 +       (void)low_level;
11777 +       return NULL;
11778 +}
11779 +
11780 +/***********************************************************
11781 + * Name: vchi_msg_peek
11782 + *
11783 + * Arguments:  const VCHI_SERVICE_HANDLE_T handle,
11784 + *             void **data,
11785 + *             uint32_t *msg_size,
11786 +
11787 +
11788 + *             VCHI_FLAGS_T flags
11789 + *
11790 + * Description: Routine to return a pointer to the current message (to allow in
11791 + *              place processing). The message can be removed using
11792 + *              vchi_msg_remove when you're finished
11793 + *
11794 + * Returns: int32_t - success == 0
11795 + *
11796 + ***********************************************************/
11797 +int32_t vchi_msg_peek(VCHI_SERVICE_HANDLE_T handle,
11798 +       void **data,
11799 +       uint32_t *msg_size,
11800 +       VCHI_FLAGS_T flags)
11801 +{
11802 +       SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
11803 +       VCHIQ_HEADER_T *header;
11804 +
11805 +       WARN_ON((flags != VCHI_FLAGS_NONE) &&
11806 +               (flags != VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE));
11807 +
11808 +       if (flags == VCHI_FLAGS_NONE)
11809 +               if (vchiu_queue_is_empty(&service->queue))
11810 +                       return -1;
11811 +
11812 +       header = vchiu_queue_peek(&service->queue);
11813 +
11814 +       *data = header->data;
11815 +       *msg_size = header->size;
11816 +
11817 +       return 0;
11818 +}
11819 +EXPORT_SYMBOL(vchi_msg_peek);
11820 +
11821 +/***********************************************************
11822 + * Name: vchi_msg_remove
11823 + *
11824 + * Arguments:  const VCHI_SERVICE_HANDLE_T handle,
11825 + *
11826 + * Description: Routine to remove a message (after it has been read with
11827 + *              vchi_msg_peek)
11828 + *
11829 + * Returns: int32_t - success == 0
11830 + *
11831 + ***********************************************************/
11832 +int32_t vchi_msg_remove(VCHI_SERVICE_HANDLE_T handle)
11833 +{
11834 +       SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
11835 +       VCHIQ_HEADER_T *header;
11836 +
11837 +       header = vchiu_queue_pop(&service->queue);
11838 +
11839 +       vchiq_release_message(service->handle, header);
11840 +
11841 +       return 0;
11842 +}
11843 +EXPORT_SYMBOL(vchi_msg_remove);
11844 +
11845 +/***********************************************************
11846 + * Name: vchi_msg_queue
11847 + *
11848 + * Arguments:  VCHI_SERVICE_HANDLE_T handle,
11849 + *             const void *data,
11850 + *             uint32_t data_size,
11851 + *             VCHI_FLAGS_T flags,
11852 + *             void *msg_handle,
11853 + *
11854 + * Description: Thin wrapper to queue a message onto a connection
11855 + *
11856 + * Returns: int32_t - success == 0
11857 + *
11858 + ***********************************************************/
11859 +int32_t vchi_msg_queue(VCHI_SERVICE_HANDLE_T handle,
11860 +       const void *data,
11861 +       uint32_t data_size,
11862 +       VCHI_FLAGS_T flags,
11863 +       void *msg_handle)
11864 +{
11865 +       SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
11866 +       VCHIQ_ELEMENT_T element = {data, data_size};
11867 +       VCHIQ_STATUS_T status;
11868 +
11869 +       (void)msg_handle;
11870 +
11871 +       WARN_ON(flags != VCHI_FLAGS_BLOCK_UNTIL_QUEUED);
11872 +
11873 +       status = vchiq_queue_message(service->handle, &element, 1);
11874 +
11875 +       /* vchiq_queue_message() may return VCHIQ_RETRY, so we need to
11876 +       ** implement a retry mechanism since this function is supposed
11877 +       ** to block until queued
11878 +       */
11879 +       while (status == VCHIQ_RETRY) {
11880 +               msleep(1);
11881 +               status = vchiq_queue_message(service->handle, &element, 1);
11882 +       }
11883 +
11884 +       return vchiq_status_to_vchi(status);
11885 +}
11886 +EXPORT_SYMBOL(vchi_msg_queue);
11887 +
11888 +/***********************************************************
11889 + * Name: vchi_bulk_queue_receive
11890 + *
11891 + * Arguments:  VCHI_BULK_HANDLE_T handle,
11892 + *             void *data_dst,
11893 + *             const uint32_t data_size,
11894 + *             VCHI_FLAGS_T flags
11895 + *             void *bulk_handle
11896 + *
11897 + * Description: Routine to setup a rcv buffer
11898 + *
11899 + * Returns: int32_t - success == 0
11900 + *
11901 + ***********************************************************/
11902 +int32_t vchi_bulk_queue_receive(VCHI_SERVICE_HANDLE_T handle,
11903 +       void *data_dst,
11904 +       uint32_t data_size,
11905 +       VCHI_FLAGS_T flags,
11906 +       void *bulk_handle)
11907 +{
11908 +       SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
11909 +       VCHIQ_BULK_MODE_T mode;
11910 +       VCHIQ_STATUS_T status;
11911 +
11912 +       switch ((int)flags) {
11913 +       case VCHI_FLAGS_CALLBACK_WHEN_OP_COMPLETE
11914 +               | VCHI_FLAGS_BLOCK_UNTIL_QUEUED:
11915 +               WARN_ON(!service->callback);
11916 +               mode = VCHIQ_BULK_MODE_CALLBACK;
11917 +               break;
11918 +       case VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE:
11919 +               mode = VCHIQ_BULK_MODE_BLOCKING;
11920 +               break;
11921 +       case VCHI_FLAGS_BLOCK_UNTIL_QUEUED:
11922 +       case VCHI_FLAGS_NONE:
11923 +               mode = VCHIQ_BULK_MODE_NOCALLBACK;
11924 +               break;
11925 +       default:
11926 +               WARN(1, "unsupported message\n");
11927 +               return vchiq_status_to_vchi(VCHIQ_ERROR);
11928 +       }
11929 +
11930 +       status = vchiq_bulk_receive(service->handle, data_dst, data_size,
11931 +               bulk_handle, mode);
11932 +
11933 +       /* vchiq_bulk_receive() may return VCHIQ_RETRY, so we need to
11934 +       ** implement a retry mechanism since this function is supposed
11935 +       ** to block until queued
11936 +       */
11937 +       while (status == VCHIQ_RETRY) {
11938 +               msleep(1);
11939 +               status = vchiq_bulk_receive(service->handle, data_dst,
11940 +                       data_size, bulk_handle, mode);
11941 +       }
11942 +
11943 +       return vchiq_status_to_vchi(status);
11944 +}
11945 +EXPORT_SYMBOL(vchi_bulk_queue_receive);
11946 +
11947 +/***********************************************************
11948 + * Name: vchi_bulk_queue_transmit
11949 + *
11950 + * Arguments:  VCHI_BULK_HANDLE_T handle,
11951 + *             const void *data_src,
11952 + *             uint32_t data_size,
11953 + *             VCHI_FLAGS_T flags,
11954 + *             void *bulk_handle
11955 + *
11956 + * Description: Routine to transmit some data
11957 + *
11958 + * Returns: int32_t - success == 0
11959 + *
11960 + ***********************************************************/
11961 +int32_t vchi_bulk_queue_transmit(VCHI_SERVICE_HANDLE_T handle,
11962 +       const void *data_src,
11963 +       uint32_t data_size,
11964 +       VCHI_FLAGS_T flags,
11965 +       void *bulk_handle)
11966 +{
11967 +       SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
11968 +       VCHIQ_BULK_MODE_T mode;
11969 +       VCHIQ_STATUS_T status;
11970 +
11971 +       switch ((int)flags) {
11972 +       case VCHI_FLAGS_CALLBACK_WHEN_OP_COMPLETE
11973 +               | VCHI_FLAGS_BLOCK_UNTIL_QUEUED:
11974 +               WARN_ON(!service->callback);
11975 +               mode = VCHIQ_BULK_MODE_CALLBACK;
11976 +               break;
11977 +       case VCHI_FLAGS_BLOCK_UNTIL_DATA_READ:
11978 +       case VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE:
11979 +               mode = VCHIQ_BULK_MODE_BLOCKING;
11980 +               break;
11981 +       case VCHI_FLAGS_BLOCK_UNTIL_QUEUED:
11982 +       case VCHI_FLAGS_NONE:
11983 +               mode = VCHIQ_BULK_MODE_NOCALLBACK;
11984 +               break;
11985 +       default:
11986 +               WARN(1, "unsupported message\n");
11987 +               return vchiq_status_to_vchi(VCHIQ_ERROR);
11988 +       }
11989 +
11990 +       status = vchiq_bulk_transmit(service->handle, data_src, data_size,
11991 +               bulk_handle, mode);
11992 +
11993 +       /* vchiq_bulk_transmit() may return VCHIQ_RETRY, so we need to
11994 +       ** implement a retry mechanism since this function is supposed
11995 +       ** to block until queued
11996 +       */
11997 +       while (status == VCHIQ_RETRY) {
11998 +               msleep(1);
11999 +               status = vchiq_bulk_transmit(service->handle, data_src,
12000 +                       data_size, bulk_handle, mode);
12001 +       }
12002 +
12003 +       return vchiq_status_to_vchi(status);
12004 +}
12005 +EXPORT_SYMBOL(vchi_bulk_queue_transmit);
12006 +
12007 +/***********************************************************
12008 + * Name: vchi_msg_dequeue
12009 + *
12010 + * Arguments:  VCHI_SERVICE_HANDLE_T handle,
12011 + *             void *data,
12012 + *             uint32_t max_data_size_to_read,
12013 + *             uint32_t *actual_msg_size
12014 + *             VCHI_FLAGS_T flags
12015 + *
12016 + * Description: Routine to dequeue a message into the supplied buffer
12017 + *
12018 + * Returns: int32_t - success == 0
12019 + *
12020 + ***********************************************************/
12021 +int32_t vchi_msg_dequeue(VCHI_SERVICE_HANDLE_T handle,
12022 +       void *data,
12023 +       uint32_t max_data_size_to_read,
12024 +       uint32_t *actual_msg_size,
12025 +       VCHI_FLAGS_T flags)
12026 +{
12027 +       SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
12028 +       VCHIQ_HEADER_T *header;
12029 +
12030 +       WARN_ON((flags != VCHI_FLAGS_NONE) &&
12031 +               (flags != VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE));
12032 +
12033 +       if (flags == VCHI_FLAGS_NONE)
12034 +               if (vchiu_queue_is_empty(&service->queue))
12035 +                       return -1;
12036 +
12037 +       header = vchiu_queue_pop(&service->queue);
12038 +
12039 +       memcpy(data, header->data, header->size < max_data_size_to_read ?
12040 +               header->size : max_data_size_to_read);
12041 +
12042 +       *actual_msg_size = header->size;
12043 +
12044 +       vchiq_release_message(service->handle, header);
12045 +
12046 +       return 0;
12047 +}
12048 +EXPORT_SYMBOL(vchi_msg_dequeue);
12049 +
12050 +/***********************************************************
12051 + * Name: vchi_msg_queuev
12052 + *
12053 + * Arguments:  VCHI_SERVICE_HANDLE_T handle,
12054 + *             VCHI_MSG_VECTOR_T *vector,
12055 + *             uint32_t count,
12056 + *             VCHI_FLAGS_T flags,
12057 + *             void *msg_handle
12058 + *
12059 + * Description: Thin wrapper to queue a message onto a connection
12060 + *
12061 + * Returns: int32_t - success == 0
12062 + *
12063 + ***********************************************************/
12064 +
12065 +vchiq_static_assert(sizeof(VCHI_MSG_VECTOR_T) == sizeof(VCHIQ_ELEMENT_T));
12066 +vchiq_static_assert(offsetof(VCHI_MSG_VECTOR_T, vec_base) ==
12067 +       offsetof(VCHIQ_ELEMENT_T, data));
12068 +vchiq_static_assert(offsetof(VCHI_MSG_VECTOR_T, vec_len) ==
12069 +       offsetof(VCHIQ_ELEMENT_T, size));
12070 +
12071 +int32_t vchi_msg_queuev(VCHI_SERVICE_HANDLE_T handle,
12072 +       VCHI_MSG_VECTOR_T *vector,
12073 +       uint32_t count,
12074 +       VCHI_FLAGS_T flags,
12075 +       void *msg_handle)
12076 +{
12077 +       SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
12078 +
12079 +       (void)msg_handle;
12080 +
12081 +       WARN_ON(flags != VCHI_FLAGS_BLOCK_UNTIL_QUEUED);
12082 +
12083 +       return vchiq_status_to_vchi(vchiq_queue_message(service->handle,
12084 +               (const VCHIQ_ELEMENT_T *)vector, count));
12085 +}
12086 +EXPORT_SYMBOL(vchi_msg_queuev);
12087 +
12088 +/***********************************************************
12089 + * Name: vchi_held_msg_release
12090 + *
12091 + * Arguments:  VCHI_HELD_MSG_T *message
12092 + *
12093 + * Description: Routine to release a held message (after it has been read with
12094 + *              vchi_msg_hold)
12095 + *
12096 + * Returns: int32_t - success == 0
12097 + *
12098 + ***********************************************************/
12099 +int32_t vchi_held_msg_release(VCHI_HELD_MSG_T *message)
12100 +{
12101 +       vchiq_release_message((VCHIQ_SERVICE_HANDLE_T)message->service,
12102 +               (VCHIQ_HEADER_T *)message->message);
12103 +
12104 +       return 0;
12105 +}
12106 +EXPORT_SYMBOL(vchi_held_msg_release);
12107 +
12108 +/***********************************************************
12109 + * Name: vchi_msg_hold
12110 + *
12111 + * Arguments:  VCHI_SERVICE_HANDLE_T handle,
12112 + *             void **data,
12113 + *             uint32_t *msg_size,
12114 + *             VCHI_FLAGS_T flags,
12115 + *             VCHI_HELD_MSG_T *message_handle
12116 + *
12117 + * Description: Routine to return a pointer to the current message (to allow
12118 + *              in place processing). The message is dequeued - don't forget
12119 + *              to release the message using vchi_held_msg_release when you're
12120 + *              finished.
12121 + *
12122 + * Returns: int32_t - success == 0
12123 + *
12124 + ***********************************************************/
12125 +int32_t vchi_msg_hold(VCHI_SERVICE_HANDLE_T handle,
12126 +       void **data,
12127 +       uint32_t *msg_size,
12128 +       VCHI_FLAGS_T flags,
12129 +       VCHI_HELD_MSG_T *message_handle)
12130 +{
12131 +       SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
12132 +       VCHIQ_HEADER_T *header;
12133 +
12134 +       WARN_ON((flags != VCHI_FLAGS_NONE) &&
12135 +               (flags != VCHI_FLAGS_BLOCK_UNTIL_OP_COMPLETE));
12136 +
12137 +       if (flags == VCHI_FLAGS_NONE)
12138 +               if (vchiu_queue_is_empty(&service->queue))
12139 +                       return -1;
12140 +
12141 +       header = vchiu_queue_pop(&service->queue);
12142 +
12143 +       *data = header->data;
12144 +       *msg_size = header->size;
12145 +
12146 +       message_handle->service =
12147 +               (struct opaque_vchi_service_t *)service->handle;
12148 +       message_handle->message = header;
12149 +
12150 +       return 0;
12151 +}
12152 +EXPORT_SYMBOL(vchi_msg_hold);
12153 +
12154 +/***********************************************************
12155 + * Name: vchi_initialise
12156 + *
12157 + * Arguments: VCHI_INSTANCE_T *instance_handle
12158 + *
12159 + * Description: Initialises the hardware but does not transmit anything
12160 + *              When run as a Host App this will be called twice hence the need
12161 + *              to malloc the state information
12162 + *
12163 + * Returns: 0 if successful, failure otherwise
12164 + *
12165 + ***********************************************************/
12166 +
12167 +int32_t vchi_initialise(VCHI_INSTANCE_T *instance_handle)
12168 +{
12169 +       VCHIQ_INSTANCE_T instance;
12170 +       VCHIQ_STATUS_T status;
12171 +
12172 +       status = vchiq_initialise(&instance);
12173 +
12174 +       *instance_handle = (VCHI_INSTANCE_T)instance;
12175 +
12176 +       return vchiq_status_to_vchi(status);
12177 +}
12178 +EXPORT_SYMBOL(vchi_initialise);
12179 +
12180 +/***********************************************************
12181 + * Name: vchi_connect
12182 + *
12183 + * Arguments: VCHI_CONNECTION_T **connections
12184 + *            const uint32_t num_connections
12185 + *            VCHI_INSTANCE_T instance_handle)
12186 + *
12187 + * Description: Starts the command service on each connection,
12188 + *              causing INIT messages to be pinged back and forth
12189 + *
12190 + * Returns: 0 if successful, failure otherwise
12191 + *
12192 + ***********************************************************/
12193 +int32_t vchi_connect(VCHI_CONNECTION_T **connections,
12194 +       const uint32_t num_connections,
12195 +       VCHI_INSTANCE_T instance_handle)
12196 +{
12197 +       VCHIQ_INSTANCE_T instance = (VCHIQ_INSTANCE_T)instance_handle;
12198 +
12199 +       (void)connections;
12200 +       (void)num_connections;
12201 +
12202 +       return vchiq_connect(instance);
12203 +}
12204 +EXPORT_SYMBOL(vchi_connect);
12205 +
12206 +
12207 +/***********************************************************
12208 + * Name: vchi_disconnect
12209 + *
12210 + * Arguments: VCHI_INSTANCE_T instance_handle
12211 + *
12212 + * Description: Stops the command service on each connection,
12213 + *              causing DE-INIT messages to be pinged back and forth
12214 + *
12215 + * Returns: 0 if successful, failure otherwise
12216 + *
12217 + ***********************************************************/
12218 +int32_t vchi_disconnect(VCHI_INSTANCE_T instance_handle)
12219 +{
12220 +       VCHIQ_INSTANCE_T instance = (VCHIQ_INSTANCE_T)instance_handle;
12221 +       return vchiq_status_to_vchi(vchiq_shutdown(instance));
12222 +}
12223 +EXPORT_SYMBOL(vchi_disconnect);
12224 +
12225 +
12226 +/***********************************************************
12227 + * Name: vchi_service_open
12228 + * Name: vchi_service_create
12229 + *
12230 + * Arguments: VCHI_INSTANCE_T *instance_handle
12231 + *            SERVICE_CREATION_T *setup,
12232 + *            VCHI_SERVICE_HANDLE_T *handle
12233 + *
12234 + * Description: Routine to open a service
12235 + *
12236 + * Returns: int32_t - success == 0
12237 + *
12238 + ***********************************************************/
12239 +
12240 +static VCHIQ_STATUS_T shim_callback(VCHIQ_REASON_T reason,
12241 +       VCHIQ_HEADER_T *header, VCHIQ_SERVICE_HANDLE_T handle, void *bulk_user)
12242 +{
12243 +       SHIM_SERVICE_T *service =
12244 +               (SHIM_SERVICE_T *)VCHIQ_GET_SERVICE_USERDATA(handle);
12245 +
12246 +        if (!service->callback)
12247 +               goto release;
12248 +
12249 +       switch (reason) {
12250 +       case VCHIQ_MESSAGE_AVAILABLE:
12251 +               vchiu_queue_push(&service->queue, header);
12252 +
12253 +               service->callback(service->callback_param,
12254 +                                 VCHI_CALLBACK_MSG_AVAILABLE, NULL);
12255 +
12256 +               goto done;
12257 +               break;
12258 +
12259 +       case VCHIQ_BULK_TRANSMIT_DONE:
12260 +               service->callback(service->callback_param,
12261 +                                 VCHI_CALLBACK_BULK_SENT, bulk_user);
12262 +               break;
12263 +
12264 +       case VCHIQ_BULK_RECEIVE_DONE:
12265 +               service->callback(service->callback_param,
12266 +                                 VCHI_CALLBACK_BULK_RECEIVED, bulk_user);
12267 +               break;
12268 +
12269 +       case VCHIQ_SERVICE_CLOSED:
12270 +               service->callback(service->callback_param,
12271 +                                 VCHI_CALLBACK_SERVICE_CLOSED, NULL);
12272 +               break;
12273 +
12274 +       case VCHIQ_SERVICE_OPENED:
12275 +               /* No equivalent VCHI reason */
12276 +               break;
12277 +
12278 +       case VCHIQ_BULK_TRANSMIT_ABORTED:
12279 +               service->callback(service->callback_param,
12280 +                                 VCHI_CALLBACK_BULK_TRANSMIT_ABORTED,
12281 +                                 bulk_user);
12282 +               break;
12283 +
12284 +       case VCHIQ_BULK_RECEIVE_ABORTED:
12285 +               service->callback(service->callback_param,
12286 +                                 VCHI_CALLBACK_BULK_RECEIVE_ABORTED,
12287 +                                 bulk_user);
12288 +               break;
12289 +
12290 +       default:
12291 +               WARN(1, "not supported\n");
12292 +               break;
12293 +       }
12294 +
12295 +release:
12296 +        vchiq_release_message(service->handle, header);
12297 +done:
12298 +       return VCHIQ_SUCCESS;
12299 +}
12300 +
12301 +static SHIM_SERVICE_T *service_alloc(VCHIQ_INSTANCE_T instance,
12302 +       SERVICE_CREATION_T *setup)
12303 +{
12304 +       SHIM_SERVICE_T *service = kzalloc(sizeof(SHIM_SERVICE_T), GFP_KERNEL);
12305 +
12306 +       (void)instance;
12307 +
12308 +       if (service) {
12309 +               if (vchiu_queue_init(&service->queue, 64)) {
12310 +                       service->callback = setup->callback;
12311 +                       service->callback_param = setup->callback_param;
12312 +               } else {
12313 +                       kfree(service);
12314 +                       service = NULL;
12315 +               }
12316 +       }
12317 +
12318 +       return service;
12319 +}
12320 +
12321 +static void service_free(SHIM_SERVICE_T *service)
12322 +{
12323 +       if (service) {
12324 +               vchiu_queue_delete(&service->queue);
12325 +               kfree(service);
12326 +       }
12327 +}
12328 +
12329 +int32_t vchi_service_open(VCHI_INSTANCE_T instance_handle,
12330 +       SERVICE_CREATION_T *setup,
12331 +       VCHI_SERVICE_HANDLE_T *handle)
12332 +{
12333 +       VCHIQ_INSTANCE_T instance = (VCHIQ_INSTANCE_T)instance_handle;
12334 +       SHIM_SERVICE_T *service = service_alloc(instance, setup);
12335 +       if (service) {
12336 +               VCHIQ_SERVICE_PARAMS_T params;
12337 +               VCHIQ_STATUS_T status;
12338 +
12339 +               memset(&params, 0, sizeof(params));
12340 +               params.fourcc = setup->service_id;
12341 +               params.callback = shim_callback;
12342 +               params.userdata = service;
12343 +               params.version = setup->version.version;
12344 +               params.version_min = setup->version.version_min;
12345 +
12346 +               status = vchiq_open_service(instance, &params,
12347 +                       &service->handle);
12348 +               if (status != VCHIQ_SUCCESS) {
12349 +                       service_free(service);
12350 +                       service = NULL;
12351 +               }
12352 +       }
12353 +
12354 +       *handle = (VCHI_SERVICE_HANDLE_T)service;
12355 +
12356 +       return (service != NULL) ? 0 : -1;
12357 +}
12358 +EXPORT_SYMBOL(vchi_service_open);
12359 +
12360 +int32_t vchi_service_create(VCHI_INSTANCE_T instance_handle,
12361 +       SERVICE_CREATION_T *setup,
12362 +       VCHI_SERVICE_HANDLE_T *handle)
12363 +{
12364 +       VCHIQ_INSTANCE_T instance = (VCHIQ_INSTANCE_T)instance_handle;
12365 +       SHIM_SERVICE_T *service = service_alloc(instance, setup);
12366 +       if (service) {
12367 +               VCHIQ_SERVICE_PARAMS_T params;
12368 +               VCHIQ_STATUS_T status;
12369 +
12370 +               memset(&params, 0, sizeof(params));
12371 +               params.fourcc = setup->service_id;
12372 +               params.callback = shim_callback;
12373 +               params.userdata = service;
12374 +               params.version = setup->version.version;
12375 +               params.version_min = setup->version.version_min;
12376 +               status = vchiq_add_service(instance, &params, &service->handle);
12377 +
12378 +               if (status != VCHIQ_SUCCESS) {
12379 +                       service_free(service);
12380 +                       service = NULL;
12381 +               }
12382 +       }
12383 +
12384 +       *handle = (VCHI_SERVICE_HANDLE_T)service;
12385 +
12386 +       return (service != NULL) ? 0 : -1;
12387 +}
12388 +EXPORT_SYMBOL(vchi_service_create);
12389 +
12390 +int32_t vchi_service_close(const VCHI_SERVICE_HANDLE_T handle)
12391 +{
12392 +       int32_t ret = -1;
12393 +       SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
12394 +       if (service) {
12395 +               VCHIQ_STATUS_T status = vchiq_close_service(service->handle);
12396 +               if (status == VCHIQ_SUCCESS) {
12397 +                       service_free(service);
12398 +                       service = NULL;
12399 +               }
12400 +
12401 +               ret = vchiq_status_to_vchi(status);
12402 +       }
12403 +       return ret;
12404 +}
12405 +EXPORT_SYMBOL(vchi_service_close);
12406 +
12407 +int32_t vchi_service_destroy(const VCHI_SERVICE_HANDLE_T handle)
12408 +{
12409 +       int32_t ret = -1;
12410 +       SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
12411 +       if (service) {
12412 +               VCHIQ_STATUS_T status = vchiq_remove_service(service->handle);
12413 +               if (status == VCHIQ_SUCCESS) {
12414 +                       service_free(service);
12415 +                       service = NULL;
12416 +               }
12417 +
12418 +               ret = vchiq_status_to_vchi(status);
12419 +       }
12420 +       return ret;
12421 +}
12422 +EXPORT_SYMBOL(vchi_service_destroy);
12423 +
12424 +int32_t vchi_service_set_option(const VCHI_SERVICE_HANDLE_T handle,
12425 +                               VCHI_SERVICE_OPTION_T option,
12426 +                               int value)
12427 +{
12428 +       int32_t ret = -1;
12429 +       SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
12430 +       VCHIQ_SERVICE_OPTION_T vchiq_option;
12431 +       switch (option) {
12432 +       case VCHI_SERVICE_OPTION_TRACE:
12433 +               vchiq_option = VCHIQ_SERVICE_OPTION_TRACE;
12434 +               break;
12435 +       default:
12436 +               service = NULL;
12437 +               break;
12438 +       }
12439 +       if (service) {
12440 +               VCHIQ_STATUS_T status =
12441 +                       vchiq_set_service_option(service->handle,
12442 +                                               vchiq_option,
12443 +                                               value);
12444 +
12445 +               ret = vchiq_status_to_vchi(status);
12446 +       }
12447 +       return ret;
12448 +}
12449 +EXPORT_SYMBOL(vchi_service_set_option);
12450 +
12451 +int32_t vchi_get_peer_version( const VCHI_SERVICE_HANDLE_T handle, short *peer_version )
12452 +{
12453 +   int32_t ret = -1;
12454 +   SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
12455 +   if(service)
12456 +   {
12457 +      VCHIQ_STATUS_T status = vchiq_get_peer_version(service->handle, peer_version);
12458 +      ret = vchiq_status_to_vchi( status );
12459 +   }
12460 +   return ret;
12461 +}
12462 +EXPORT_SYMBOL(vchi_get_peer_version);
12463 +
12464 +/* ----------------------------------------------------------------------
12465 + * read a uint32_t from buffer.
12466 + * network format is defined to be little endian
12467 + * -------------------------------------------------------------------- */
12468 +uint32_t
12469 +vchi_readbuf_uint32(const void *_ptr)
12470 +{
12471 +       const unsigned char *ptr = _ptr;
12472 +       return ptr[0] | (ptr[1] << 8) | (ptr[2] << 16) | (ptr[3] << 24);
12473 +}
12474 +
12475 +/* ----------------------------------------------------------------------
12476 + * write a uint32_t to buffer.
12477 + * network format is defined to be little endian
12478 + * -------------------------------------------------------------------- */
12479 +void
12480 +vchi_writebuf_uint32(void *_ptr, uint32_t value)
12481 +{
12482 +       unsigned char *ptr = _ptr;
12483 +       ptr[0] = (unsigned char)((value >> 0)  & 0xFF);
12484 +       ptr[1] = (unsigned char)((value >> 8)  & 0xFF);
12485 +       ptr[2] = (unsigned char)((value >> 16) & 0xFF);
12486 +       ptr[3] = (unsigned char)((value >> 24) & 0xFF);
12487 +}
12488 +
12489 +/* ----------------------------------------------------------------------
12490 + * read a uint16_t from buffer.
12491 + * network format is defined to be little endian
12492 + * -------------------------------------------------------------------- */
12493 +uint16_t
12494 +vchi_readbuf_uint16(const void *_ptr)
12495 +{
12496 +       const unsigned char *ptr = _ptr;
12497 +       return ptr[0] | (ptr[1] << 8);
12498 +}
12499 +
12500 +/* ----------------------------------------------------------------------
12501 + * write a uint16_t into the buffer.
12502 + * network format is defined to be little endian
12503 + * -------------------------------------------------------------------- */
12504 +void
12505 +vchi_writebuf_uint16(void *_ptr, uint16_t value)
12506 +{
12507 +       unsigned char *ptr = _ptr;
12508 +       ptr[0] = (value >> 0)  & 0xFF;
12509 +       ptr[1] = (value >> 8)  & 0xFF;
12510 +}
12511 +
12512 +/***********************************************************
12513 + * Name: vchi_service_use
12514 + *
12515 + * Arguments: const VCHI_SERVICE_HANDLE_T handle
12516 + *
12517 + * Description: Routine to increment refcount on a service
12518 + *
12519 + * Returns: void
12520 + *
12521 + ***********************************************************/
12522 +int32_t vchi_service_use(const VCHI_SERVICE_HANDLE_T handle)
12523 +{
12524 +       int32_t ret = -1;
12525 +       SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
12526 +       if (service)
12527 +               ret = vchiq_status_to_vchi(vchiq_use_service(service->handle));
12528 +       return ret;
12529 +}
12530 +EXPORT_SYMBOL(vchi_service_use);
12531 +
12532 +/***********************************************************
12533 + * Name: vchi_service_release
12534 + *
12535 + * Arguments: const VCHI_SERVICE_HANDLE_T handle
12536 + *
12537 + * Description: Routine to decrement refcount on a service
12538 + *
12539 + * Returns: void
12540 + *
12541 + ***********************************************************/
12542 +int32_t vchi_service_release(const VCHI_SERVICE_HANDLE_T handle)
12543 +{
12544 +       int32_t ret = -1;
12545 +       SHIM_SERVICE_T *service = (SHIM_SERVICE_T *)handle;
12546 +       if (service)
12547 +               ret = vchiq_status_to_vchi(
12548 +                       vchiq_release_service(service->handle));
12549 +       return ret;
12550 +}
12551 +EXPORT_SYMBOL(vchi_service_release);
12552 --- /dev/null
12553 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.c
12554 @@ -0,0 +1,151 @@
12555 +/**
12556 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
12557 + *
12558 + * Redistribution and use in source and binary forms, with or without
12559 + * modification, are permitted provided that the following conditions
12560 + * are met:
12561 + * 1. Redistributions of source code must retain the above copyright
12562 + *    notice, this list of conditions, and the following disclaimer,
12563 + *    without modification.
12564 + * 2. Redistributions in binary form must reproduce the above copyright
12565 + *    notice, this list of conditions and the following disclaimer in the
12566 + *    documentation and/or other materials provided with the distribution.
12567 + * 3. The names of the above-listed copyright holders may not be used
12568 + *    to endorse or promote products derived from this software without
12569 + *    specific prior written permission.
12570 + *
12571 + * ALTERNATIVELY, this software may be distributed under the terms of the
12572 + * GNU General Public License ("GPL") version 2, as published by the Free
12573 + * Software Foundation.
12574 + *
12575 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
12576 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
12577 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
12578 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
12579 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
12580 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
12581 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
12582 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
12583 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
12584 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
12585 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
12586 + */
12587 +
12588 +#include "vchiq_util.h"
12589 +
12590 +static inline int is_pow2(int i)
12591 +{
12592 +       return i && !(i & (i - 1));
12593 +}
12594 +
12595 +int vchiu_queue_init(VCHIU_QUEUE_T *queue, int size)
12596 +{
12597 +       WARN_ON(!is_pow2(size));
12598 +
12599 +       queue->size = size;
12600 +       queue->read = 0;
12601 +       queue->write = 0;
12602 +
12603 +       sema_init(&queue->pop, 0);
12604 +       sema_init(&queue->push, 0);
12605 +
12606 +       queue->storage = kzalloc(size * sizeof(VCHIQ_HEADER_T *), GFP_KERNEL);
12607 +       if (queue->storage == NULL) {
12608 +               vchiu_queue_delete(queue);
12609 +               return 0;
12610 +       }
12611 +       return 1;
12612 +}
12613 +
12614 +void vchiu_queue_delete(VCHIU_QUEUE_T *queue)
12615 +{
12616 +       if (queue->storage != NULL)
12617 +               kfree(queue->storage);
12618 +}
12619 +
12620 +int vchiu_queue_is_empty(VCHIU_QUEUE_T *queue)
12621 +{
12622 +       return queue->read == queue->write;
12623 +}
12624 +
12625 +int vchiu_queue_is_full(VCHIU_QUEUE_T *queue)
12626 +{
12627 +       return queue->write == queue->read + queue->size;
12628 +}
12629 +
12630 +void vchiu_queue_push(VCHIU_QUEUE_T *queue, VCHIQ_HEADER_T *header)
12631 +{
12632 +       while (queue->write == queue->read + queue->size) {
12633 +               if (down_interruptible(&queue->pop) != 0) {
12634 +                       flush_signals(current);
12635 +               }
12636 +       }
12637 +
12638 +       /*
12639 +        * Write to queue->storage must be visible after read from
12640 +        * queue->read
12641 +        */
12642 +       smp_mb();
12643 +
12644 +       queue->storage[queue->write & (queue->size - 1)] = header;
12645 +
12646 +       /*
12647 +        * Write to queue->storage must be visible before write to
12648 +        * queue->write
12649 +        */
12650 +       smp_wmb();
12651 +
12652 +       queue->write++;
12653 +
12654 +       up(&queue->push);
12655 +}
12656 +
12657 +VCHIQ_HEADER_T *vchiu_queue_peek(VCHIU_QUEUE_T *queue)
12658 +{
12659 +       while (queue->write == queue->read) {
12660 +               if (down_interruptible(&queue->push) != 0) {
12661 +                       flush_signals(current);
12662 +               }
12663 +       }
12664 +
12665 +       up(&queue->push); // We haven't removed anything from the queue.
12666 +
12667 +       /*
12668 +        * Read from queue->storage must be visible after read from
12669 +        * queue->write
12670 +        */
12671 +       smp_rmb();
12672 +
12673 +       return queue->storage[queue->read & (queue->size - 1)];
12674 +}
12675 +
12676 +VCHIQ_HEADER_T *vchiu_queue_pop(VCHIU_QUEUE_T *queue)
12677 +{
12678 +       VCHIQ_HEADER_T *header;
12679 +
12680 +       while (queue->write == queue->read) {
12681 +               if (down_interruptible(&queue->push) != 0) {
12682 +                       flush_signals(current);
12683 +               }
12684 +       }
12685 +
12686 +       /*
12687 +        * Read from queue->storage must be visible after read from
12688 +        * queue->write
12689 +        */
12690 +       smp_rmb();
12691 +
12692 +       header = queue->storage[queue->read & (queue->size - 1)];
12693 +
12694 +       /*
12695 +        * Read from queue->storage must be visible before write to
12696 +        * queue->read
12697 +        */
12698 +       smp_mb();
12699 +
12700 +       queue->read++;
12701 +
12702 +       up(&queue->pop);
12703 +
12704 +       return header;
12705 +}
12706 --- /dev/null
12707 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_util.h
12708 @@ -0,0 +1,81 @@
12709 +/**
12710 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
12711 + *
12712 + * Redistribution and use in source and binary forms, with or without
12713 + * modification, are permitted provided that the following conditions
12714 + * are met:
12715 + * 1. Redistributions of source code must retain the above copyright
12716 + *    notice, this list of conditions, and the following disclaimer,
12717 + *    without modification.
12718 + * 2. Redistributions in binary form must reproduce the above copyright
12719 + *    notice, this list of conditions and the following disclaimer in the
12720 + *    documentation and/or other materials provided with the distribution.
12721 + * 3. The names of the above-listed copyright holders may not be used
12722 + *    to endorse or promote products derived from this software without
12723 + *    specific prior written permission.
12724 + *
12725 + * ALTERNATIVELY, this software may be distributed under the terms of the
12726 + * GNU General Public License ("GPL") version 2, as published by the Free
12727 + * Software Foundation.
12728 + *
12729 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
12730 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
12731 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
12732 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
12733 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
12734 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
12735 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
12736 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
12737 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
12738 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
12739 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
12740 + */
12741 +
12742 +#ifndef VCHIQ_UTIL_H
12743 +#define VCHIQ_UTIL_H
12744 +
12745 +#include <linux/types.h>
12746 +#include <linux/semaphore.h>
12747 +#include <linux/mutex.h>
12748 +#include <linux/bitops.h>
12749 +#include <linux/kthread.h>
12750 +#include <linux/wait.h>
12751 +#include <linux/vmalloc.h>
12752 +#include <linux/jiffies.h>
12753 +#include <linux/delay.h>
12754 +#include <linux/string.h>
12755 +#include <linux/types.h>
12756 +#include <linux/interrupt.h>
12757 +#include <linux/random.h>
12758 +#include <linux/sched.h>
12759 +#include <linux/ctype.h>
12760 +#include <linux/uaccess.h>
12761 +#include <linux/time.h>  /* for time_t */
12762 +#include <linux/slab.h>
12763 +#include <linux/vmalloc.h>
12764 +
12765 +#include "vchiq_if.h"
12766 +
12767 +typedef struct {
12768 +       int size;
12769 +       int read;
12770 +       int write;
12771 +
12772 +       struct semaphore pop;
12773 +       struct semaphore push;
12774 +
12775 +       VCHIQ_HEADER_T **storage;
12776 +} VCHIU_QUEUE_T;
12777 +
12778 +extern int  vchiu_queue_init(VCHIU_QUEUE_T *queue, int size);
12779 +extern void vchiu_queue_delete(VCHIU_QUEUE_T *queue);
12780 +
12781 +extern int vchiu_queue_is_empty(VCHIU_QUEUE_T *queue);
12782 +extern int vchiu_queue_is_full(VCHIU_QUEUE_T *queue);
12783 +
12784 +extern void vchiu_queue_push(VCHIU_QUEUE_T *queue, VCHIQ_HEADER_T *header);
12785 +
12786 +extern VCHIQ_HEADER_T *vchiu_queue_peek(VCHIU_QUEUE_T *queue);
12787 +extern VCHIQ_HEADER_T *vchiu_queue_pop(VCHIU_QUEUE_T *queue);
12788 +
12789 +#endif
12790 --- /dev/null
12791 +++ b/drivers/misc/vc04_services/interface/vchiq_arm/vchiq_version.c
12792 @@ -0,0 +1,59 @@
12793 +/**
12794 + * Copyright (c) 2010-2012 Broadcom. All rights reserved.
12795 + *
12796 + * Redistribution and use in source and binary forms, with or without
12797 + * modification, are permitted provided that the following conditions
12798 + * are met:
12799 + * 1. Redistributions of source code must retain the above copyright
12800 + *    notice, this list of conditions, and the following disclaimer,
12801 + *    without modification.
12802 + * 2. Redistributions in binary form must reproduce the above copyright
12803 + *    notice, this list of conditions and the following disclaimer in the
12804 + *    documentation and/or other materials provided with the distribution.
12805 + * 3. The names of the above-listed copyright holders may not be used
12806 + *    to endorse or promote products derived from this software without
12807 + *    specific prior written permission.
12808 + *
12809 + * ALTERNATIVELY, this software may be distributed under the terms of the
12810 + * GNU General Public License ("GPL") version 2, as published by the Free
12811 + * Software Foundation.
12812 + *
12813 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
12814 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
12815 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
12816 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
12817 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
12818 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
12819 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
12820 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
12821 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
12822 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
12823 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
12824 + */
12825 +#include "vchiq_build_info.h"
12826 +#include <linux/broadcom/vc_debug_sym.h>
12827 +
12828 +VC_DEBUG_DECLARE_STRING_VAR( vchiq_build_hostname, "dc4-arm-01" );
12829 +VC_DEBUG_DECLARE_STRING_VAR( vchiq_build_version, "9245b4c35b99b3870e1f7dc598c5692b3c66a6f0 (tainted)" );
12830 +VC_DEBUG_DECLARE_STRING_VAR( vchiq_build_time,    __TIME__ );
12831 +VC_DEBUG_DECLARE_STRING_VAR( vchiq_build_date,    __DATE__ );
12832 +
12833 +const char *vchiq_get_build_hostname( void )
12834 +{
12835 +   return vchiq_build_hostname;
12836 +}
12837 +
12838 +const char *vchiq_get_build_version( void )
12839 +{
12840 +   return vchiq_build_version;
12841 +}
12842 +
12843 +const char *vchiq_get_build_date( void )
12844 +{
12845 +   return vchiq_build_date;
12846 +}
12847 +
12848 +const char *vchiq_get_build_time( void )
12849 +{
12850 +   return vchiq_build_time;
12851 +}