GNU Linux-libre 4.19.245-gnu1
[releases.git] / drivers / staging / vc04_services / interface / vchi / vchi.h
1 /**
2  * Copyright (c) 2010-2012 Broadcom. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions, and the following disclaimer,
9  *    without modification.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. The names of the above-listed copyright holders may not be used
14  *    to endorse or promote products derived from this software without
15  *    specific prior written permission.
16  *
17  * ALTERNATIVELY, this software may be distributed under the terms of the
18  * GNU General Public License ("GPL") version 2, as published by the Free
19  * Software Foundation.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
22  * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
25  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
26  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
27  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
28  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
29  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
30  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
31  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #ifndef VCHI_H_
35 #define VCHI_H_
36
37 #include "interface/vchi/vchi_cfg.h"
38 #include "interface/vchi/vchi_common.h"
39 #include "interface/vchi/connections/connection.h"
40 #include "vchi_mh.h"
41
42 /******************************************************************************
43  Global defs
44  *****************************************************************************/
45
46 #define VCHI_BULK_ROUND_UP(x)     ((((unsigned long)(x))+VCHI_BULK_ALIGN-1) & ~(VCHI_BULK_ALIGN-1))
47 #define VCHI_BULK_ROUND_DOWN(x)   (((unsigned long)(x)) & ~(VCHI_BULK_ALIGN-1))
48 #define VCHI_BULK_ALIGN_NBYTES(x) (VCHI_BULK_ALIGNED(x) ? 0 : (VCHI_BULK_ALIGN - ((unsigned long)(x) & (VCHI_BULK_ALIGN-1))))
49
50 #ifdef USE_VCHIQ_ARM
51 #define VCHI_BULK_ALIGNED(x)      1
52 #else
53 #define VCHI_BULK_ALIGNED(x)      (((unsigned long)(x) & (VCHI_BULK_ALIGN-1)) == 0)
54 #endif
55
56 struct vchi_version {
57         uint32_t version;
58         uint32_t version_min;
59 };
60 #define VCHI_VERSION(v_) { v_, v_ }
61 #define VCHI_VERSION_EX(v_, m_) { v_, m_ }
62
63 typedef enum {
64    VCHI_VEC_POINTER,
65    VCHI_VEC_HANDLE,
66    VCHI_VEC_LIST
67 } VCHI_MSG_VECTOR_TYPE_T;
68
69 typedef struct vchi_msg_vector_ex {
70
71    VCHI_MSG_VECTOR_TYPE_T type;
72    union {
73       // a memory handle
74       struct {
75          VCHI_MEM_HANDLE_T handle;
76          uint32_t offset;
77          int32_t vec_len;
78       } handle;
79
80       // an ordinary data pointer
81       struct {
82          const void *vec_base;
83          int32_t vec_len;
84       } ptr;
85
86       // a nested vector list
87       struct {
88          struct vchi_msg_vector_ex *vec;
89          uint32_t vec_len;
90       } list;
91    } u;
92 } VCHI_MSG_VECTOR_EX_T;
93
94 // Construct an entry in a msg vector for a pointer (p) of length (l)
95 #define VCHI_VEC_POINTER(p,l)  VCHI_VEC_POINTER, { { (VCHI_MEM_HANDLE_T)(p), (l) } }
96
97 // Construct an entry in a msg vector for a message handle (h), starting at offset (o) of length (l)
98 #define VCHI_VEC_HANDLE(h,o,l) VCHI_VEC_HANDLE,  { { (h), (o), (l) } }
99
100 // Macros to manipulate 'FOURCC' values
101 #define MAKE_FOURCC(x) ((int32_t)( (x[0] << 24) | (x[1] << 16) | (x[2] << 8) | x[3] ))
102 #define FOURCC_TO_CHAR(x) (x >> 24) & 0xFF,(x >> 16) & 0xFF,(x >> 8) & 0xFF, x & 0xFF
103
104 // Opaque service information
105 struct opaque_vchi_service_t;
106
107 // Descriptor for a held message. Allocated by client, initialised by vchi_msg_hold,
108 // vchi_msg_iter_hold or vchi_msg_iter_hold_next. Fields are for internal VCHI use only.
109 typedef struct {
110    struct opaque_vchi_service_t *service;
111    void *message;
112 } VCHI_HELD_MSG_T;
113
114 // structure used to provide the information needed to open a server or a client
115 typedef struct {
116         struct vchi_version version;
117         int32_t service_id;
118         VCHI_CONNECTION_T *connection;
119         uint32_t rx_fifo_size;
120         uint32_t tx_fifo_size;
121         VCHI_CALLBACK_T callback;
122         void *callback_param;
123         /* client intends to receive bulk transfers of
124                 odd lengths or into unaligned buffers */
125         int32_t want_unaligned_bulk_rx;
126         /* client intends to transmit bulk transfers of
127                 odd lengths or out of unaligned buffers */
128         int32_t want_unaligned_bulk_tx;
129         /* client wants to check CRCs on (bulk) xfers.
130                 Only needs to be set at 1 end - will do both directions. */
131         int32_t want_crc;
132 } SERVICE_CREATION_T;
133
134 // Opaque handle for a VCHI instance
135 typedef struct opaque_vchi_instance_handle_t *VCHI_INSTANCE_T;
136
137 // Opaque handle for a server or client
138 typedef struct opaque_vchi_service_handle_t *VCHI_SERVICE_HANDLE_T;
139
140 // Service registration & startup
141 typedef void (*VCHI_SERVICE_INIT)(VCHI_INSTANCE_T initialise_instance, VCHI_CONNECTION_T **connections, uint32_t num_connections);
142
143 typedef struct service_info_tag {
144    const char * const vll_filename; /* VLL to load to start this service. This is an empty string if VLL is "static" */
145    VCHI_SERVICE_INIT init;          /* Service initialisation function */
146    void *vll_handle;                /* VLL handle; NULL when unloaded or a "static VLL" in build */
147 } SERVICE_INFO_T;
148
149 /******************************************************************************
150  Global funcs - implementation is specific to which side you are on (local / remote)
151  *****************************************************************************/
152
153 #ifdef __cplusplus
154 extern "C" {
155 #endif
156
157 extern /*@observer@*/ VCHI_CONNECTION_T * vchi_create_connection( const VCHI_CONNECTION_API_T * function_table,
158                                                    const VCHI_MESSAGE_DRIVER_T * low_level);
159
160 // Routine used to initialise the vchi on both local + remote connections
161 extern int32_t vchi_initialise( VCHI_INSTANCE_T *instance_handle );
162
163 extern int32_t vchi_exit( void );
164
165 extern int32_t vchi_connect( VCHI_CONNECTION_T **connections,
166                              const uint32_t num_connections,
167                              VCHI_INSTANCE_T instance_handle );
168
169 //When this is called, ensure that all services have no data pending.
170 //Bulk transfers can remain 'queued'
171 extern int32_t vchi_disconnect( VCHI_INSTANCE_T instance_handle );
172
173 // Global control over bulk CRC checking
174 extern int32_t vchi_crc_control( VCHI_CONNECTION_T *connection,
175                                  VCHI_CRC_CONTROL_T control );
176
177 // helper functions
178 extern void * vchi_allocate_buffer(VCHI_SERVICE_HANDLE_T handle, uint32_t *length);
179 extern void vchi_free_buffer(VCHI_SERVICE_HANDLE_T handle, void *address);
180 extern uint32_t vchi_current_time(VCHI_INSTANCE_T instance_handle);
181
182 /******************************************************************************
183  Global service API
184  *****************************************************************************/
185 // Routine to create a named service
186 extern int32_t vchi_service_create( VCHI_INSTANCE_T instance_handle,
187                                     SERVICE_CREATION_T *setup,
188                                     VCHI_SERVICE_HANDLE_T *handle );
189
190 // Routine to destroy a service
191 extern int32_t vchi_service_destroy( const VCHI_SERVICE_HANDLE_T handle );
192
193 // Routine to open a named service
194 extern int32_t vchi_service_open( VCHI_INSTANCE_T instance_handle,
195                                   SERVICE_CREATION_T *setup,
196                                   VCHI_SERVICE_HANDLE_T *handle);
197
198 extern int32_t vchi_get_peer_version( const VCHI_SERVICE_HANDLE_T handle,
199                                       short *peer_version );
200
201 // Routine to close a named service
202 extern int32_t vchi_service_close( const VCHI_SERVICE_HANDLE_T handle );
203
204 // Routine to increment ref count on a named service
205 extern int32_t vchi_service_use( const VCHI_SERVICE_HANDLE_T handle );
206
207 // Routine to decrement ref count on a named service
208 extern int32_t vchi_service_release( const VCHI_SERVICE_HANDLE_T handle );
209
210 // Routine to set a control option for a named service
211 extern int32_t vchi_service_set_option( const VCHI_SERVICE_HANDLE_T handle,
212                                         VCHI_SERVICE_OPTION_T option,
213                                         int value);
214
215 /* Routine to send a message from kernel memory across a service */
216 extern int
217 vchi_queue_kernel_message(VCHI_SERVICE_HANDLE_T handle,
218                           void *data,
219                           unsigned int size);
220
221 /* Routine to send a message from user memory across a service */
222 extern int
223 vchi_queue_user_message(VCHI_SERVICE_HANDLE_T handle,
224                         void __user *data,
225                         unsigned int size);
226
227 // Routine to receive a msg from a service
228 // Dequeue is equivalent to hold, copy into client buffer, release
229 extern int32_t vchi_msg_dequeue( VCHI_SERVICE_HANDLE_T handle,
230                                  void *data,
231                                  uint32_t max_data_size_to_read,
232                                  uint32_t *actual_msg_size,
233                                  VCHI_FLAGS_T flags );
234
235 // Routine to look at a message in place.
236 // The message is not dequeued, so a subsequent call to peek or dequeue
237 // will return the same message.
238 extern int32_t vchi_msg_peek( VCHI_SERVICE_HANDLE_T handle,
239                               void **data,
240                               uint32_t *msg_size,
241                               VCHI_FLAGS_T flags );
242
243 // Routine to remove a message after it has been read in place with peek
244 // The first message on the queue is dequeued.
245 extern int32_t vchi_msg_remove( VCHI_SERVICE_HANDLE_T handle );
246
247 // Routine to look at a message in place.
248 // The message is dequeued, so the caller is left holding it; the descriptor is
249 // filled in and must be released when the user has finished with the message.
250 extern int32_t vchi_msg_hold( VCHI_SERVICE_HANDLE_T handle,
251                               void **data,        // } may be NULL, as info can be
252                               uint32_t *msg_size, // } obtained from HELD_MSG_T
253                               VCHI_FLAGS_T flags,
254                               VCHI_HELD_MSG_T *message_descriptor );
255
256 // Initialise an iterator to look through messages in place
257 extern int32_t vchi_msg_look_ahead( VCHI_SERVICE_HANDLE_T handle,
258                                     VCHI_MSG_ITER_T *iter,
259                                     VCHI_FLAGS_T flags );
260
261 /******************************************************************************
262  Global service support API - operations on held messages and message iterators
263  *****************************************************************************/
264
265 // Routine to get the address of a held message
266 extern void *vchi_held_msg_ptr( const VCHI_HELD_MSG_T *message );
267
268 // Routine to get the size of a held message
269 extern int32_t vchi_held_msg_size( const VCHI_HELD_MSG_T *message );
270
271 // Routine to get the transmit timestamp as written into the header by the peer
272 extern uint32_t vchi_held_msg_tx_timestamp( const VCHI_HELD_MSG_T *message );
273
274 // Routine to get the reception timestamp, written as we parsed the header
275 extern uint32_t vchi_held_msg_rx_timestamp( const VCHI_HELD_MSG_T *message );
276
277 // Routine to release a held message after it has been processed
278 extern int32_t vchi_held_msg_release( VCHI_HELD_MSG_T *message );
279
280 // Indicates whether the iterator has a next message.
281 extern int32_t vchi_msg_iter_has_next( const VCHI_MSG_ITER_T *iter );
282
283 // Return the pointer and length for the next message and advance the iterator.
284 extern int32_t vchi_msg_iter_next( VCHI_MSG_ITER_T *iter,
285                                    void **data,
286                                    uint32_t *msg_size );
287
288 // Remove the last message returned by vchi_msg_iter_next.
289 // Can only be called once after each call to vchi_msg_iter_next.
290 extern int32_t vchi_msg_iter_remove( VCHI_MSG_ITER_T *iter );
291
292 // Hold the last message returned by vchi_msg_iter_next.
293 // Can only be called once after each call to vchi_msg_iter_next.
294 extern int32_t vchi_msg_iter_hold( VCHI_MSG_ITER_T *iter,
295                                    VCHI_HELD_MSG_T *message );
296
297 // Return information for the next message, and hold it, advancing the iterator.
298 extern int32_t vchi_msg_iter_hold_next( VCHI_MSG_ITER_T *iter,
299                                         void **data,        // } may be NULL
300                                         uint32_t *msg_size, // }
301                                         VCHI_HELD_MSG_T *message );
302
303 /******************************************************************************
304  Global bulk API
305  *****************************************************************************/
306
307 // Routine to prepare interface for a transfer from the other side
308 extern int32_t vchi_bulk_queue_receive( VCHI_SERVICE_HANDLE_T handle,
309                                         void *data_dst,
310                                         uint32_t data_size,
311                                         VCHI_FLAGS_T flags,
312                                         void *transfer_handle );
313
314 // Prepare interface for a transfer from the other side into relocatable memory.
315 int32_t vchi_bulk_queue_receive_reloc( const VCHI_SERVICE_HANDLE_T handle,
316                                        VCHI_MEM_HANDLE_T h_dst,
317                                        uint32_t offset,
318                                        uint32_t data_size,
319                                        const VCHI_FLAGS_T flags,
320                                        void * const bulk_handle );
321
322 // Routine to queue up data ready for transfer to the other (once they have signalled they are ready)
323 extern int32_t vchi_bulk_queue_transmit( VCHI_SERVICE_HANDLE_T handle,
324                                          const void *data_src,
325                                          uint32_t data_size,
326                                          VCHI_FLAGS_T flags,
327                                          void *transfer_handle );
328
329 /******************************************************************************
330  Configuration plumbing
331  *****************************************************************************/
332
333 // function prototypes for the different mid layers (the state info gives the different physical connections)
334 extern const VCHI_CONNECTION_API_T *single_get_func_table( void );
335 //extern const VCHI_CONNECTION_API_T *local_server_get_func_table( void );
336 //extern const VCHI_CONNECTION_API_T *local_client_get_func_table( void );
337
338 // declare all message drivers here
339 const VCHI_MESSAGE_DRIVER_T *vchi_mphi_message_driver_func_table( void );
340
341 #ifdef __cplusplus
342 }
343 #endif
344
345 extern int32_t vchi_bulk_queue_transmit_reloc( VCHI_SERVICE_HANDLE_T handle,
346                                                VCHI_MEM_HANDLE_T h_src,
347                                                uint32_t offset,
348                                                uint32_t data_size,
349                                                VCHI_FLAGS_T flags,
350                                                void *transfer_handle );
351 #endif /* VCHI_H_ */
352
353 /****************************** End of file **********************************/