staging: vchiq_core: get the rid of IS_POW2
[linux-2.6-microblaze.git] / drivers / staging / vc04_services / interface / vchiq_arm / vchiq_core.h
1 /* SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause */
2 /* Copyright (c) 2010-2012 Broadcom. All rights reserved. */
3
4 #ifndef VCHIQ_CORE_H
5 #define VCHIQ_CORE_H
6
7 #include <linux/mutex.h>
8 #include <linux/completion.h>
9 #include <linux/kthread.h>
10 #include <linux/kref.h>
11 #include <linux/rcupdate.h>
12 #include <linux/wait.h>
13 #include <linux/raspberrypi/vchiq.h>
14
15 #include "vchiq_cfg.h"
16
17
18 /* Do this so that we can test-build the code on non-rpi systems */
19 #if IS_ENABLED(CONFIG_RASPBERRYPI_FIRMWARE)
20
21 #else
22
23 #ifndef dsb
24 #define dsb(a)
25 #endif
26
27 #endif  /* IS_ENABLED(CONFIG_RASPBERRYPI_FIRMWARE) */
28
29 #define VCHIQ_SERVICE_HANDLE_INVALID 0
30
31 #define VCHIQ_SLOT_SIZE     4096
32 #define VCHIQ_MAX_MSG_SIZE  (VCHIQ_SLOT_SIZE - sizeof(struct vchiq_header))
33
34 /* Run time control of log level, based on KERN_XXX level. */
35 #define VCHIQ_LOG_DEFAULT  4
36 #define VCHIQ_LOG_ERROR    3
37 #define VCHIQ_LOG_WARNING  4
38 #define VCHIQ_LOG_INFO     6
39 #define VCHIQ_LOG_TRACE    7
40
41 #define VCHIQ_LOG_PREFIX   KERN_INFO "vchiq: "
42
43 #ifndef vchiq_log_error
44 #define vchiq_log_error(cat, fmt, ...) \
45         do { if (cat >= VCHIQ_LOG_ERROR) \
46                 printk(VCHIQ_LOG_PREFIX fmt "\n", ##__VA_ARGS__); } while (0)
47 #endif
48 #ifndef vchiq_log_warning
49 #define vchiq_log_warning(cat, fmt, ...) \
50         do { if (cat >= VCHIQ_LOG_WARNING) \
51                  printk(VCHIQ_LOG_PREFIX fmt "\n", ##__VA_ARGS__); } while (0)
52 #endif
53 #ifndef vchiq_log_info
54 #define vchiq_log_info(cat, fmt, ...) \
55         do { if (cat >= VCHIQ_LOG_INFO) \
56                 printk(VCHIQ_LOG_PREFIX fmt "\n", ##__VA_ARGS__); } while (0)
57 #endif
58 #ifndef vchiq_log_trace
59 #define vchiq_log_trace(cat, fmt, ...) \
60         do { if (cat >= VCHIQ_LOG_TRACE) \
61                 printk(VCHIQ_LOG_PREFIX fmt "\n", ##__VA_ARGS__); } while (0)
62 #endif
63
64 #define vchiq_loud_error(...) \
65         vchiq_log_error(vchiq_core_log_level, "===== " __VA_ARGS__)
66
67 #ifndef vchiq_static_assert
68 #define vchiq_static_assert(cond) __attribute__((unused)) \
69         extern int vchiq_static_assert[(cond) ? 1 : -1]
70 #endif
71
72 #define VCHIQ_SLOT_MASK        (VCHIQ_SLOT_SIZE - 1)
73 #define VCHIQ_SLOT_QUEUE_MASK  (VCHIQ_MAX_SLOTS_PER_SIDE - 1)
74 #define VCHIQ_SLOT_ZERO_SLOTS  DIV_ROUND_UP(sizeof(struct vchiq_slot_zero), \
75                                             VCHIQ_SLOT_SIZE)
76
77 #define VCHIQ_FOURCC_AS_4CHARS(fourcc)  \
78         ((fourcc) >> 24) & 0xff, \
79         ((fourcc) >> 16) & 0xff, \
80         ((fourcc) >>  8) & 0xff, \
81         (fourcc) & 0xff
82
83 typedef uint32_t BITSET_T;
84
85 vchiq_static_assert((sizeof(BITSET_T) * 8) == 32);
86
87 #define BITSET_SIZE(b)        ((b + 31) >> 5)
88 #define BITSET_WORD(b)        (b >> 5)
89 #define BITSET_BIT(b)         (1 << (b & 31))
90 #define BITSET_IS_SET(bs, b)  (bs[BITSET_WORD(b)] & BITSET_BIT(b))
91 #define BITSET_SET(bs, b)     (bs[BITSET_WORD(b)] |= BITSET_BIT(b))
92 #define BITSET_CLR(bs, b)     (bs[BITSET_WORD(b)] &= ~BITSET_BIT(b))
93
94 enum {
95         DEBUG_ENTRIES,
96 #if VCHIQ_ENABLE_DEBUG
97         DEBUG_SLOT_HANDLER_COUNT,
98         DEBUG_SLOT_HANDLER_LINE,
99         DEBUG_PARSE_LINE,
100         DEBUG_PARSE_HEADER,
101         DEBUG_PARSE_MSGID,
102         DEBUG_AWAIT_COMPLETION_LINE,
103         DEBUG_DEQUEUE_MESSAGE_LINE,
104         DEBUG_SERVICE_CALLBACK_LINE,
105         DEBUG_MSG_QUEUE_FULL_COUNT,
106         DEBUG_COMPLETION_QUEUE_FULL_COUNT,
107 #endif
108         DEBUG_MAX
109 };
110
111 #if VCHIQ_ENABLE_DEBUG
112
113 #define DEBUG_INITIALISE(local) int *debug_ptr = (local)->debug;
114 #define DEBUG_TRACE(d) \
115         do { debug_ptr[DEBUG_ ## d] = __LINE__; dsb(sy); } while (0)
116 #define DEBUG_VALUE(d, v) \
117         do { debug_ptr[DEBUG_ ## d] = (v); dsb(sy); } while (0)
118 #define DEBUG_COUNT(d) \
119         do { debug_ptr[DEBUG_ ## d]++; dsb(sy); } while (0)
120
121 #else /* VCHIQ_ENABLE_DEBUG */
122
123 #define DEBUG_INITIALISE(local)
124 #define DEBUG_TRACE(d)
125 #define DEBUG_VALUE(d, v)
126 #define DEBUG_COUNT(d)
127
128 #endif /* VCHIQ_ENABLE_DEBUG */
129
130 enum vchiq_connstate {
131         VCHIQ_CONNSTATE_DISCONNECTED,
132         VCHIQ_CONNSTATE_CONNECTING,
133         VCHIQ_CONNSTATE_CONNECTED,
134         VCHIQ_CONNSTATE_PAUSING,
135         VCHIQ_CONNSTATE_PAUSE_SENT,
136         VCHIQ_CONNSTATE_PAUSED,
137         VCHIQ_CONNSTATE_RESUMING,
138         VCHIQ_CONNSTATE_PAUSE_TIMEOUT,
139         VCHIQ_CONNSTATE_RESUME_TIMEOUT
140 };
141
142 enum {
143         VCHIQ_SRVSTATE_FREE,
144         VCHIQ_SRVSTATE_HIDDEN,
145         VCHIQ_SRVSTATE_LISTENING,
146         VCHIQ_SRVSTATE_OPENING,
147         VCHIQ_SRVSTATE_OPEN,
148         VCHIQ_SRVSTATE_OPENSYNC,
149         VCHIQ_SRVSTATE_CLOSESENT,
150         VCHIQ_SRVSTATE_CLOSERECVD,
151         VCHIQ_SRVSTATE_CLOSEWAIT,
152         VCHIQ_SRVSTATE_CLOSED
153 };
154
155 enum vchiq_bulk_dir {
156         VCHIQ_BULK_TRANSMIT,
157         VCHIQ_BULK_RECEIVE
158 };
159
160 typedef void (*vchiq_userdata_term)(void *userdata);
161
162 struct vchiq_bulk {
163         short mode;
164         short dir;
165         void *userdata;
166         dma_addr_t data;
167         int size;
168         void *remote_data;
169         int remote_size;
170         int actual;
171 };
172
173 struct vchiq_bulk_queue {
174         int local_insert;  /* Where to insert the next local bulk */
175         int remote_insert; /* Where to insert the next remote bulk (master) */
176         int process;       /* Bulk to transfer next */
177         int remote_notify; /* Bulk to notify the remote client of next (mstr) */
178         int remove;        /* Bulk to notify the local client of, and remove, next */
179         struct vchiq_bulk bulks[VCHIQ_NUM_SERVICE_BULKS];
180 };
181
182 struct remote_event {
183         int armed;
184         int fired;
185         u32 __unused;
186 };
187
188 struct opaque_platform_state;
189
190 struct vchiq_slot {
191         char data[VCHIQ_SLOT_SIZE];
192 };
193
194 struct vchiq_slot_info {
195         /* Use two counters rather than one to avoid the need for a mutex. */
196         short use_count;
197         short release_count;
198 };
199
200 struct vchiq_service {
201         struct vchiq_service_base base;
202         unsigned int handle;
203         struct kref ref_count;
204         struct rcu_head rcu;
205         int srvstate;
206         vchiq_userdata_term userdata_term;
207         unsigned int localport;
208         unsigned int remoteport;
209         int public_fourcc;
210         int client_id;
211         char auto_close;
212         char sync;
213         char closing;
214         char trace;
215         atomic_t poll_flags;
216         short version;
217         short version_min;
218         short peer_version;
219
220         struct vchiq_state *state;
221         struct vchiq_instance *instance;
222
223         int service_use_count;
224
225         struct vchiq_bulk_queue bulk_tx;
226         struct vchiq_bulk_queue bulk_rx;
227
228         struct completion remove_event;
229         struct completion bulk_remove_event;
230         struct mutex bulk_mutex;
231
232         struct service_stats_struct {
233                 int quota_stalls;
234                 int slot_stalls;
235                 int bulk_stalls;
236                 int error_count;
237                 int ctrl_tx_count;
238                 int ctrl_rx_count;
239                 int bulk_tx_count;
240                 int bulk_rx_count;
241                 int bulk_aborted_count;
242                 uint64_t ctrl_tx_bytes;
243                 uint64_t ctrl_rx_bytes;
244                 uint64_t bulk_tx_bytes;
245                 uint64_t bulk_rx_bytes;
246         } stats;
247
248         int msg_queue_read;
249         int msg_queue_write;
250         struct completion msg_queue_pop;
251         struct completion msg_queue_push;
252         struct vchiq_header *msg_queue[VCHIQ_MAX_SLOTS];
253 };
254
255 /*
256  * The quota information is outside struct vchiq_service so that it can
257  * be statically allocated, since for accounting reasons a service's slot
258  * usage is carried over between users of the same port number.
259  */
260 struct vchiq_service_quota {
261         unsigned short slot_quota;
262         unsigned short slot_use_count;
263         unsigned short message_quota;
264         unsigned short message_use_count;
265         struct completion quota_event;
266         int previous_tx_index;
267 };
268
269 struct vchiq_shared_state {
270
271         /* A non-zero value here indicates that the content is valid. */
272         int initialised;
273
274         /* The first and last (inclusive) slots allocated to the owner. */
275         int slot_first;
276         int slot_last;
277
278         /* The slot allocated to synchronous messages from the owner. */
279         int slot_sync;
280
281         /*
282          * Signalling this event indicates that owner's slot handler thread
283          * should run.
284          */
285         struct remote_event trigger;
286
287         /*
288          * Indicates the byte position within the stream where the next message
289          * will be written. The least significant bits are an index into the
290          * slot. The next bits are the index of the slot in slot_queue.
291          */
292         int tx_pos;
293
294         /* This event should be signalled when a slot is recycled. */
295         struct remote_event recycle;
296
297         /* The slot_queue index where the next recycled slot will be written. */
298         int slot_queue_recycle;
299
300         /* This event should be signalled when a synchronous message is sent. */
301         struct remote_event sync_trigger;
302
303         /*
304          * This event should be signalled when a synchronous message has been
305          * released.
306          */
307         struct remote_event sync_release;
308
309         /* A circular buffer of slot indexes. */
310         int slot_queue[VCHIQ_MAX_SLOTS_PER_SIDE];
311
312         /* Debugging state */
313         int debug[DEBUG_MAX];
314 };
315
316 struct vchiq_slot_zero {
317         int magic;
318         short version;
319         short version_min;
320         int slot_zero_size;
321         int slot_size;
322         int max_slots;
323         int max_slots_per_side;
324         int platform_data[2];
325         struct vchiq_shared_state master;
326         struct vchiq_shared_state slave;
327         struct vchiq_slot_info slots[VCHIQ_MAX_SLOTS];
328 };
329
330 struct vchiq_state {
331         int id;
332         int initialised;
333         enum vchiq_connstate conn_state;
334         short version_common;
335
336         struct vchiq_shared_state *local;
337         struct vchiq_shared_state *remote;
338         struct vchiq_slot *slot_data;
339
340         unsigned short default_slot_quota;
341         unsigned short default_message_quota;
342
343         /* Event indicating connect message received */
344         struct completion connect;
345
346         /* Mutex protecting services */
347         struct mutex mutex;
348         struct vchiq_instance **instance;
349
350         /* Processes incoming messages */
351         struct task_struct *slot_handler_thread;
352
353         /* Processes recycled slots */
354         struct task_struct *recycle_thread;
355
356         /* Processes synchronous messages */
357         struct task_struct *sync_thread;
358
359         /* Local implementation of the trigger remote event */
360         wait_queue_head_t trigger_event;
361
362         /* Local implementation of the recycle remote event */
363         wait_queue_head_t recycle_event;
364
365         /* Local implementation of the sync trigger remote event */
366         wait_queue_head_t sync_trigger_event;
367
368         /* Local implementation of the sync release remote event */
369         wait_queue_head_t sync_release_event;
370
371         char *tx_data;
372         char *rx_data;
373         struct vchiq_slot_info *rx_info;
374
375         struct mutex slot_mutex;
376
377         struct mutex recycle_mutex;
378
379         struct mutex sync_mutex;
380
381         struct mutex bulk_transfer_mutex;
382
383         /*
384          * Indicates the byte position within the stream from where the next
385          * message will be read. The least significant bits are an index into
386          * the slot.The next bits are the index of the slot in
387          * remote->slot_queue.
388          */
389         int rx_pos;
390
391         /*
392          * A cached copy of local->tx_pos. Only write to local->tx_pos, and read
393          * from remote->tx_pos.
394          */
395         int local_tx_pos;
396
397         /* The slot_queue index of the slot to become available next. */
398         int slot_queue_available;
399
400         /* A flag to indicate if any poll has been requested */
401         int poll_needed;
402
403         /* Ths index of the previous slot used for data messages. */
404         int previous_data_index;
405
406         /* The number of slots occupied by data messages. */
407         unsigned short data_use_count;
408
409         /* The maximum number of slots to be occupied by data messages. */
410         unsigned short data_quota;
411
412         /* An array of bit sets indicating which services must be polled. */
413         atomic_t poll_services[BITSET_SIZE(VCHIQ_MAX_SERVICES)];
414
415         /* The number of the first unused service */
416         int unused_service;
417
418         /* Signalled when a free slot becomes available. */
419         struct completion slot_available_event;
420
421         struct completion slot_remove_event;
422
423         /* Signalled when a free data slot becomes available. */
424         struct completion data_quota_event;
425
426         struct state_stats_struct {
427                 int slot_stalls;
428                 int data_stalls;
429                 int ctrl_tx_count;
430                 int ctrl_rx_count;
431                 int error_count;
432         } stats;
433
434         struct vchiq_service __rcu *services[VCHIQ_MAX_SERVICES];
435         struct vchiq_service_quota service_quotas[VCHIQ_MAX_SERVICES];
436         struct vchiq_slot_info slot_info[VCHIQ_MAX_SLOTS];
437
438         struct opaque_platform_state *platform_state;
439 };
440
441 struct bulk_waiter {
442         struct vchiq_bulk *bulk;
443         struct completion event;
444         int actual;
445 };
446
447 struct vchiq_config {
448         unsigned int max_msg_size;
449         unsigned int bulk_threshold;    /* The message size above which it
450                                          * is better to use a bulk transfer
451                                          * (<= max_msg_size)
452                                          */
453         unsigned int max_outstanding_bulks;
454         unsigned int max_services;
455         short version;      /* The version of VCHIQ */
456         short version_min;  /* The minimum compatible version of VCHIQ */
457 };
458
459
460 extern spinlock_t bulk_waiter_spinlock;
461
462 extern int vchiq_core_log_level;
463 extern int vchiq_core_msg_log_level;
464 extern int vchiq_sync_log_level;
465
466 extern struct vchiq_state *vchiq_states[VCHIQ_MAX_STATES];
467
468 extern const char *
469 get_conn_state_name(enum vchiq_connstate conn_state);
470
471 extern struct vchiq_slot_zero *
472 vchiq_init_slots(void *mem_base, int mem_size);
473
474 extern int
475 vchiq_init_state(struct vchiq_state *state, struct vchiq_slot_zero *slot_zero);
476
477 extern enum vchiq_status
478 vchiq_connect_internal(struct vchiq_state *state, struct vchiq_instance *instance);
479
480 struct vchiq_service *
481 vchiq_add_service_internal(struct vchiq_state *state,
482                            const struct vchiq_service_params_kernel *params,
483                            int srvstate, struct vchiq_instance *instance,
484                            vchiq_userdata_term userdata_term);
485
486 extern enum vchiq_status
487 vchiq_open_service_internal(struct vchiq_service *service, int client_id);
488
489 extern enum vchiq_status
490 vchiq_close_service_internal(struct vchiq_service *service, int close_recvd);
491
492 extern void
493 vchiq_terminate_service_internal(struct vchiq_service *service);
494
495 extern void
496 vchiq_free_service_internal(struct vchiq_service *service);
497
498 extern void
499 vchiq_shutdown_internal(struct vchiq_state *state, struct vchiq_instance *instance);
500
501 extern void
502 remote_event_pollall(struct vchiq_state *state);
503
504 extern enum vchiq_status
505 vchiq_bulk_transfer(unsigned int handle, void *offset, void __user *uoffset,
506                     int size, void *userdata, enum vchiq_bulk_mode mode,
507                     enum vchiq_bulk_dir dir);
508
509 extern int
510 vchiq_dump_state(void *dump_context, struct vchiq_state *state);
511
512 extern int
513 vchiq_dump_service_state(void *dump_context, struct vchiq_service *service);
514
515 extern void
516 vchiq_loud_error_header(void);
517
518 extern void
519 vchiq_loud_error_footer(void);
520
521 extern void
522 request_poll(struct vchiq_state *state, struct vchiq_service *service,
523              int poll_type);
524
525 static inline struct vchiq_service *
526 handle_to_service(unsigned int handle)
527 {
528         int idx = handle & (VCHIQ_MAX_SERVICES - 1);
529         struct vchiq_state *state = vchiq_states[(handle / VCHIQ_MAX_SERVICES) &
530                 (VCHIQ_MAX_STATES - 1)];
531
532         if (!state)
533                 return NULL;
534         return rcu_dereference(state->services[idx]);
535 }
536
537 extern struct vchiq_service *
538 find_service_by_handle(unsigned int handle);
539
540 extern struct vchiq_service *
541 find_service_by_port(struct vchiq_state *state, int localport);
542
543 extern struct vchiq_service *
544 find_service_for_instance(struct vchiq_instance *instance,
545         unsigned int handle);
546
547 extern struct vchiq_service *
548 find_closed_service_for_instance(struct vchiq_instance *instance,
549         unsigned int handle);
550
551 extern struct vchiq_service *
552 __next_service_by_instance(struct vchiq_state *state,
553                            struct vchiq_instance *instance,
554                            int *pidx);
555
556 extern struct vchiq_service *
557 next_service_by_instance(struct vchiq_state *state,
558                          struct vchiq_instance *instance,
559                          int *pidx);
560
561 extern void
562 lock_service(struct vchiq_service *service);
563
564 extern void
565 unlock_service(struct vchiq_service *service);
566
567 extern enum vchiq_status
568 vchiq_queue_message(unsigned int handle,
569                     ssize_t (*copy_callback)(void *context, void *dest,
570                                              size_t offset, size_t maxsize),
571                     void *context,
572                     size_t size);
573
574 /*
575  * The following functions are called from vchiq_core, and external
576  * implementations must be provided.
577  */
578
579 extern int
580 vchiq_prepare_bulk_data(struct vchiq_bulk *bulk, void *offset,
581                         void __user *uoffset, int size, int dir);
582
583 extern void
584 vchiq_complete_bulk(struct vchiq_bulk *bulk);
585
586 extern void
587 remote_event_signal(struct remote_event *event);
588
589 extern int
590 vchiq_dump(void *dump_context, const char *str, int len);
591
592 extern int
593 vchiq_dump_platform_state(void *dump_context);
594
595 extern int
596 vchiq_dump_platform_instances(void *dump_context);
597
598 extern int
599 vchiq_dump_platform_service_state(void *dump_context,
600         struct vchiq_service *service);
601
602 extern int
603 vchiq_use_service_internal(struct vchiq_service *service);
604
605 extern int
606 vchiq_release_service_internal(struct vchiq_service *service);
607
608 extern void
609 vchiq_on_remote_use(struct vchiq_state *state);
610
611 extern void
612 vchiq_on_remote_release(struct vchiq_state *state);
613
614 extern int
615 vchiq_platform_init_state(struct vchiq_state *state);
616
617 extern enum vchiq_status
618 vchiq_check_service(struct vchiq_service *service);
619
620 extern void
621 vchiq_on_remote_use_active(struct vchiq_state *state);
622
623 extern enum vchiq_status
624 vchiq_send_remote_use(struct vchiq_state *state);
625
626 extern enum vchiq_status
627 vchiq_send_remote_use_active(struct vchiq_state *state);
628
629 extern void
630 vchiq_platform_conn_state_changed(struct vchiq_state *state,
631                                   enum vchiq_connstate oldstate,
632                                   enum vchiq_connstate newstate);
633
634 extern void
635 vchiq_set_conn_state(struct vchiq_state *state, enum vchiq_connstate newstate);
636
637 extern void
638 vchiq_log_dump_mem(const char *label, uint32_t addr, const void *voidMem,
639         size_t numBytes);
640
641 extern enum vchiq_status vchiq_remove_service(unsigned int service);
642
643 extern int vchiq_get_client_id(unsigned int service);
644
645 extern void vchiq_get_config(struct vchiq_config *config);
646
647 extern int
648 vchiq_set_service_option(unsigned int service, enum vchiq_service_option option,
649                          int value);
650
651 #endif