1 /* SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause */
2 /* Copyright (c) 2010-2012 Broadcom. All rights reserved. */
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>
14 #include "vchiq_cfg.h"
18 /* Do this so that we can test-build the code on non-rpi systems */
19 #if IS_ENABLED(CONFIG_RASPBERRYPI_FIRMWARE)
27 #endif /* IS_ENABLED(CONFIG_RASPBERRYPI_FIRMWARE) */
29 #define VCHIQ_SERVICE_HANDLE_INVALID 0
31 #define VCHIQ_SLOT_SIZE 4096
32 #define VCHIQ_MAX_MSG_SIZE (VCHIQ_SLOT_SIZE - sizeof(struct vchiq_header))
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
41 #define VCHIQ_LOG_PREFIX KERN_INFO "vchiq: "
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)
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)
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)
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)
64 #define vchiq_loud_error(...) \
65 vchiq_log_error(vchiq_core_log_level, "===== " __VA_ARGS__)
67 #ifndef vchiq_static_assert
68 #define vchiq_static_assert(cond) __attribute__((unused)) \
69 extern int vchiq_static_assert[(cond) ? 1 : -1]
72 #define IS_POW2(x) (x && ((x & (x - 1)) == 0))
74 /* Ensure that the slot size and maximum number of slots are powers of 2 */
75 vchiq_static_assert(IS_POW2(VCHIQ_SLOT_SIZE));
76 vchiq_static_assert(IS_POW2(VCHIQ_MAX_SLOTS));
77 vchiq_static_assert(IS_POW2(VCHIQ_MAX_SLOTS_PER_SIDE));
79 #define VCHIQ_SLOT_MASK (VCHIQ_SLOT_SIZE - 1)
80 #define VCHIQ_SLOT_QUEUE_MASK (VCHIQ_MAX_SLOTS_PER_SIDE - 1)
81 #define VCHIQ_SLOT_ZERO_SLOTS DIV_ROUND_UP(sizeof(struct vchiq_slot_zero), \
84 #define VCHIQ_MSG_PADDING 0 /* - */
85 #define VCHIQ_MSG_CONNECT 1 /* - */
86 #define VCHIQ_MSG_OPEN 2 /* + (srcport, -), fourcc, client_id */
87 #define VCHIQ_MSG_OPENACK 3 /* + (srcport, dstport) */
88 #define VCHIQ_MSG_CLOSE 4 /* + (srcport, dstport) */
89 #define VCHIQ_MSG_DATA 5 /* + (srcport, dstport) */
90 #define VCHIQ_MSG_BULK_RX 6 /* + (srcport, dstport), data, size */
91 #define VCHIQ_MSG_BULK_TX 7 /* + (srcport, dstport), data, size */
92 #define VCHIQ_MSG_BULK_RX_DONE 8 /* + (srcport, dstport), actual */
93 #define VCHIQ_MSG_BULK_TX_DONE 9 /* + (srcport, dstport), actual */
94 #define VCHIQ_MSG_PAUSE 10 /* - */
95 #define VCHIQ_MSG_RESUME 11 /* - */
96 #define VCHIQ_MSG_REMOTE_USE 12 /* - */
97 #define VCHIQ_MSG_REMOTE_RELEASE 13 /* - */
98 #define VCHIQ_MSG_REMOTE_USE_ACTIVE 14 /* - */
100 #define VCHIQ_PORT_MAX (VCHIQ_MAX_SERVICES - 1)
101 #define VCHIQ_PORT_FREE 0x1000
102 #define VCHIQ_PORT_IS_VALID(port) (port < VCHIQ_PORT_FREE)
103 #define VCHIQ_MAKE_MSG(type, srcport, dstport) \
104 ((type<<24) | (srcport<<12) | (dstport<<0))
105 #define VCHIQ_MSG_TYPE(msgid) ((unsigned int)msgid >> 24)
106 #define VCHIQ_MSG_SRCPORT(msgid) \
107 (unsigned short)(((unsigned int)msgid >> 12) & 0xfff)
108 #define VCHIQ_MSG_DSTPORT(msgid) \
109 ((unsigned short)msgid & 0xfff)
111 #define VCHIQ_FOURCC_AS_4CHARS(fourcc) \
112 ((fourcc) >> 24) & 0xff, \
113 ((fourcc) >> 16) & 0xff, \
114 ((fourcc) >> 8) & 0xff, \
117 /* Ensure the fields are wide enough */
118 vchiq_static_assert(VCHIQ_MSG_SRCPORT(VCHIQ_MAKE_MSG(0, 0, VCHIQ_PORT_MAX))
120 vchiq_static_assert(VCHIQ_MSG_TYPE(VCHIQ_MAKE_MSG(0, VCHIQ_PORT_MAX, 0)) == 0);
121 vchiq_static_assert((unsigned int)VCHIQ_PORT_MAX <
122 (unsigned int)VCHIQ_PORT_FREE);
124 #define VCHIQ_MSGID_PADDING VCHIQ_MAKE_MSG(VCHIQ_MSG_PADDING, 0, 0)
125 #define VCHIQ_MSGID_CLAIMED 0x40000000
127 #define VCHIQ_FOURCC_INVALID 0x00000000
128 #define VCHIQ_FOURCC_IS_LEGAL(fourcc) (fourcc != VCHIQ_FOURCC_INVALID)
130 #define VCHIQ_BULK_ACTUAL_ABORTED -1
132 typedef uint32_t BITSET_T;
134 vchiq_static_assert((sizeof(BITSET_T) * 8) == 32);
136 #define BITSET_SIZE(b) ((b + 31) >> 5)
137 #define BITSET_WORD(b) (b >> 5)
138 #define BITSET_BIT(b) (1 << (b & 31))
139 #define BITSET_IS_SET(bs, b) (bs[BITSET_WORD(b)] & BITSET_BIT(b))
140 #define BITSET_SET(bs, b) (bs[BITSET_WORD(b)] |= BITSET_BIT(b))
141 #define BITSET_CLR(bs, b) (bs[BITSET_WORD(b)] &= ~BITSET_BIT(b))
143 #if VCHIQ_ENABLE_STATS
144 #define VCHIQ_STATS_INC(state, stat) (state->stats. stat++)
145 #define VCHIQ_SERVICE_STATS_INC(service, stat) (service->stats. stat++)
146 #define VCHIQ_SERVICE_STATS_ADD(service, stat, addend) \
147 (service->stats. stat += addend)
149 #define VCHIQ_STATS_INC(state, stat) ((void)0)
150 #define VCHIQ_SERVICE_STATS_INC(service, stat) ((void)0)
151 #define VCHIQ_SERVICE_STATS_ADD(service, stat, addend) ((void)0)
156 #if VCHIQ_ENABLE_DEBUG
157 DEBUG_SLOT_HANDLER_COUNT,
158 DEBUG_SLOT_HANDLER_LINE,
162 DEBUG_AWAIT_COMPLETION_LINE,
163 DEBUG_DEQUEUE_MESSAGE_LINE,
164 DEBUG_SERVICE_CALLBACK_LINE,
165 DEBUG_MSG_QUEUE_FULL_COUNT,
166 DEBUG_COMPLETION_QUEUE_FULL_COUNT,
171 #if VCHIQ_ENABLE_DEBUG
173 #define DEBUG_INITIALISE(local) int *debug_ptr = (local)->debug;
174 #define DEBUG_TRACE(d) \
175 do { debug_ptr[DEBUG_ ## d] = __LINE__; dsb(sy); } while (0)
176 #define DEBUG_VALUE(d, v) \
177 do { debug_ptr[DEBUG_ ## d] = (v); dsb(sy); } while (0)
178 #define DEBUG_COUNT(d) \
179 do { debug_ptr[DEBUG_ ## d]++; dsb(sy); } while (0)
181 #else /* VCHIQ_ENABLE_DEBUG */
183 #define DEBUG_INITIALISE(local)
184 #define DEBUG_TRACE(d)
185 #define DEBUG_VALUE(d, v)
186 #define DEBUG_COUNT(d)
188 #endif /* VCHIQ_ENABLE_DEBUG */
190 enum vchiq_connstate {
191 VCHIQ_CONNSTATE_DISCONNECTED,
192 VCHIQ_CONNSTATE_CONNECTING,
193 VCHIQ_CONNSTATE_CONNECTED,
194 VCHIQ_CONNSTATE_PAUSING,
195 VCHIQ_CONNSTATE_PAUSE_SENT,
196 VCHIQ_CONNSTATE_PAUSED,
197 VCHIQ_CONNSTATE_RESUMING,
198 VCHIQ_CONNSTATE_PAUSE_TIMEOUT,
199 VCHIQ_CONNSTATE_RESUME_TIMEOUT
204 VCHIQ_SRVSTATE_HIDDEN,
205 VCHIQ_SRVSTATE_LISTENING,
206 VCHIQ_SRVSTATE_OPENING,
208 VCHIQ_SRVSTATE_OPENSYNC,
209 VCHIQ_SRVSTATE_CLOSESENT,
210 VCHIQ_SRVSTATE_CLOSERECVD,
211 VCHIQ_SRVSTATE_CLOSEWAIT,
212 VCHIQ_SRVSTATE_CLOSED
216 VCHIQ_POLL_TERMINATE,
223 enum vchiq_bulk_dir {
228 typedef void (*vchiq_userdata_term)(void *userdata);
241 struct vchiq_bulk_queue {
242 int local_insert; /* Where to insert the next local bulk */
243 int remote_insert; /* Where to insert the next remote bulk (master) */
244 int process; /* Bulk to transfer next */
245 int remote_notify; /* Bulk to notify the remote client of next (mstr) */
246 int remove; /* Bulk to notify the local client of, and remove,
248 struct vchiq_bulk bulks[VCHIQ_NUM_SERVICE_BULKS];
251 struct remote_event {
257 struct opaque_platform_state;
260 char data[VCHIQ_SLOT_SIZE];
263 struct vchiq_slot_info {
264 /* Use two counters rather than one to avoid the need for a mutex. */
269 struct vchiq_service {
270 struct vchiq_service_base base;
272 struct kref ref_count;
275 vchiq_userdata_term userdata_term;
276 unsigned int localport;
277 unsigned int remoteport;
289 struct vchiq_state *state;
290 struct vchiq_instance *instance;
292 int service_use_count;
294 struct vchiq_bulk_queue bulk_tx;
295 struct vchiq_bulk_queue bulk_rx;
297 struct completion remove_event;
298 struct completion bulk_remove_event;
299 struct mutex bulk_mutex;
301 struct service_stats_struct {
310 int bulk_aborted_count;
311 uint64_t ctrl_tx_bytes;
312 uint64_t ctrl_rx_bytes;
313 uint64_t bulk_tx_bytes;
314 uint64_t bulk_rx_bytes;
319 struct completion msg_queue_pop;
320 struct completion msg_queue_push;
321 struct vchiq_header *msg_queue[VCHIQ_MAX_SLOTS];
324 /* The quota information is outside struct vchiq_service so that it can
325 * be statically allocated, since for accounting reasons a service's slot
326 * usage is carried over between users of the same port number.
328 struct vchiq_service_quota {
329 unsigned short slot_quota;
330 unsigned short slot_use_count;
331 unsigned short message_quota;
332 unsigned short message_use_count;
333 struct completion quota_event;
334 int previous_tx_index;
337 struct vchiq_shared_state {
339 /* A non-zero value here indicates that the content is valid. */
342 /* The first and last (inclusive) slots allocated to the owner. */
346 /* The slot allocated to synchronous messages from the owner. */
349 /* Signalling this event indicates that owner's slot handler thread
351 struct remote_event trigger;
353 /* Indicates the byte position within the stream where the next message
354 ** will be written. The least significant bits are an index into the
355 ** slot. The next bits are the index of the slot in slot_queue. */
358 /* This event should be signalled when a slot is recycled. */
359 struct remote_event recycle;
361 /* The slot_queue index where the next recycled slot will be written. */
362 int slot_queue_recycle;
364 /* This event should be signalled when a synchronous message is sent. */
365 struct remote_event sync_trigger;
367 /* This event should be signalled when a synchronous message has been
369 struct remote_event sync_release;
371 /* A circular buffer of slot indexes. */
372 int slot_queue[VCHIQ_MAX_SLOTS_PER_SIDE];
374 /* Debugging state */
375 int debug[DEBUG_MAX];
378 struct vchiq_slot_zero {
385 int max_slots_per_side;
386 int platform_data[2];
387 struct vchiq_shared_state master;
388 struct vchiq_shared_state slave;
389 struct vchiq_slot_info slots[VCHIQ_MAX_SLOTS];
395 enum vchiq_connstate conn_state;
396 short version_common;
398 struct vchiq_shared_state *local;
399 struct vchiq_shared_state *remote;
400 struct vchiq_slot *slot_data;
402 unsigned short default_slot_quota;
403 unsigned short default_message_quota;
405 /* Event indicating connect message received */
406 struct completion connect;
408 /* Mutex protecting services */
410 struct vchiq_instance **instance;
412 /* Processes incoming messages */
413 struct task_struct *slot_handler_thread;
415 /* Processes recycled slots */
416 struct task_struct *recycle_thread;
418 /* Processes synchronous messages */
419 struct task_struct *sync_thread;
421 /* Local implementation of the trigger remote event */
422 wait_queue_head_t trigger_event;
424 /* Local implementation of the recycle remote event */
425 wait_queue_head_t recycle_event;
427 /* Local implementation of the sync trigger remote event */
428 wait_queue_head_t sync_trigger_event;
430 /* Local implementation of the sync release remote event */
431 wait_queue_head_t sync_release_event;
435 struct vchiq_slot_info *rx_info;
437 struct mutex slot_mutex;
439 struct mutex recycle_mutex;
441 struct mutex sync_mutex;
443 struct mutex bulk_transfer_mutex;
445 /* Indicates the byte position within the stream from where the next
446 ** message will be read. The least significant bits are an index into
447 ** the slot.The next bits are the index of the slot in
448 ** remote->slot_queue. */
451 /* A cached copy of local->tx_pos. Only write to local->tx_pos, and read
452 from remote->tx_pos. */
455 /* The slot_queue index of the slot to become available next. */
456 int slot_queue_available;
458 /* A flag to indicate if any poll has been requested */
461 /* Ths index of the previous slot used for data messages. */
462 int previous_data_index;
464 /* The number of slots occupied by data messages. */
465 unsigned short data_use_count;
467 /* The maximum number of slots to be occupied by data messages. */
468 unsigned short data_quota;
470 /* An array of bit sets indicating which services must be polled. */
471 atomic_t poll_services[BITSET_SIZE(VCHIQ_MAX_SERVICES)];
473 /* The number of the first unused service */
476 /* Signalled when a free slot becomes available. */
477 struct completion slot_available_event;
479 struct completion slot_remove_event;
481 /* Signalled when a free data slot becomes available. */
482 struct completion data_quota_event;
484 struct state_stats_struct {
492 struct vchiq_service __rcu *services[VCHIQ_MAX_SERVICES];
493 struct vchiq_service_quota service_quotas[VCHIQ_MAX_SERVICES];
494 struct vchiq_slot_info slot_info[VCHIQ_MAX_SLOTS];
496 struct opaque_platform_state *platform_state;
500 struct vchiq_bulk *bulk;
501 struct completion event;
505 struct vchiq_config {
506 unsigned int max_msg_size;
507 unsigned int bulk_threshold; /* The message size above which it
508 is better to use a bulk transfer
510 unsigned int max_outstanding_bulks;
511 unsigned int max_services;
512 short version; /* The version of VCHIQ */
513 short version_min; /* The minimum compatible version of VCHIQ */
517 extern spinlock_t bulk_waiter_spinlock;
519 extern int vchiq_core_log_level;
520 extern int vchiq_core_msg_log_level;
521 extern int vchiq_sync_log_level;
523 extern struct vchiq_state *vchiq_states[VCHIQ_MAX_STATES];
526 get_conn_state_name(enum vchiq_connstate conn_state);
528 extern struct vchiq_slot_zero *
529 vchiq_init_slots(void *mem_base, int mem_size);
531 extern enum vchiq_status
532 vchiq_init_state(struct vchiq_state *state, struct vchiq_slot_zero *slot_zero);
534 extern enum vchiq_status
535 vchiq_connect_internal(struct vchiq_state *state, struct vchiq_instance *instance);
537 extern struct vchiq_service *
538 vchiq_add_service_internal(struct vchiq_state *state,
539 const struct vchiq_service_params *params,
540 int srvstate, struct vchiq_instance *instance,
541 vchiq_userdata_term userdata_term);
543 extern enum vchiq_status
544 vchiq_open_service_internal(struct vchiq_service *service, int client_id);
546 extern enum vchiq_status
547 vchiq_close_service_internal(struct vchiq_service *service, int close_recvd);
550 vchiq_terminate_service_internal(struct vchiq_service *service);
553 vchiq_free_service_internal(struct vchiq_service *service);
555 extern enum vchiq_status
556 vchiq_shutdown_internal(struct vchiq_state *state, struct vchiq_instance *instance);
559 remote_event_pollall(struct vchiq_state *state);
561 extern enum vchiq_status
562 vchiq_bulk_transfer(unsigned int handle, void *offset, int size,
563 void *userdata, enum vchiq_bulk_mode mode,
564 enum vchiq_bulk_dir dir);
567 vchiq_dump_state(void *dump_context, struct vchiq_state *state);
570 vchiq_dump_service_state(void *dump_context, struct vchiq_service *service);
573 vchiq_loud_error_header(void);
576 vchiq_loud_error_footer(void);
579 request_poll(struct vchiq_state *state, struct vchiq_service *service,
582 static inline struct vchiq_service *
583 handle_to_service(unsigned int handle)
585 int idx = handle & (VCHIQ_MAX_SERVICES - 1);
586 struct vchiq_state *state = vchiq_states[(handle / VCHIQ_MAX_SERVICES) &
587 (VCHIQ_MAX_STATES - 1)];
591 return rcu_dereference(state->services[idx]);
594 extern struct vchiq_service *
595 find_service_by_handle(unsigned int handle);
597 extern struct vchiq_service *
598 find_service_by_port(struct vchiq_state *state, int localport);
600 extern struct vchiq_service *
601 find_service_for_instance(struct vchiq_instance *instance,
602 unsigned int handle);
604 extern struct vchiq_service *
605 find_closed_service_for_instance(struct vchiq_instance *instance,
606 unsigned int handle);
608 extern struct vchiq_service *
609 __next_service_by_instance(struct vchiq_state *state,
610 struct vchiq_instance *instance,
613 extern struct vchiq_service *
614 next_service_by_instance(struct vchiq_state *state,
615 struct vchiq_instance *instance,
619 lock_service(struct vchiq_service *service);
622 unlock_service(struct vchiq_service *service);
624 extern enum vchiq_status
625 vchiq_queue_message(unsigned int handle,
626 ssize_t (*copy_callback)(void *context, void *dest,
627 size_t offset, size_t maxsize),
631 /* The following functions are called from vchiq_core, and external
632 ** implementations must be provided. */
634 extern enum vchiq_status
635 vchiq_prepare_bulk_data(struct vchiq_bulk *bulk, void *offset, int size,
639 vchiq_complete_bulk(struct vchiq_bulk *bulk);
642 remote_event_signal(struct remote_event *event);
645 vchiq_dump(void *dump_context, const char *str, int len);
648 vchiq_dump_platform_state(void *dump_context);
651 vchiq_dump_platform_instances(void *dump_context);
654 vchiq_dump_platform_service_state(void *dump_context,
655 struct vchiq_service *service);
657 extern enum vchiq_status
658 vchiq_use_service_internal(struct vchiq_service *service);
660 extern enum vchiq_status
661 vchiq_release_service_internal(struct vchiq_service *service);
664 vchiq_on_remote_use(struct vchiq_state *state);
667 vchiq_on_remote_release(struct vchiq_state *state);
669 extern enum vchiq_status
670 vchiq_platform_init_state(struct vchiq_state *state);
672 extern enum vchiq_status
673 vchiq_check_service(struct vchiq_service *service);
676 vchiq_on_remote_use_active(struct vchiq_state *state);
678 extern enum vchiq_status
679 vchiq_send_remote_use(struct vchiq_state *state);
681 extern enum vchiq_status
682 vchiq_send_remote_use_active(struct vchiq_state *state);
685 vchiq_platform_conn_state_changed(struct vchiq_state *state,
686 enum vchiq_connstate oldstate,
687 enum vchiq_connstate newstate);
690 vchiq_set_conn_state(struct vchiq_state *state, enum vchiq_connstate newstate);
693 vchiq_log_dump_mem(const char *label, uint32_t addr, const void *voidMem,
696 extern enum vchiq_status vchiq_remove_service(unsigned int service);
698 extern int vchiq_get_client_id(unsigned int service);
700 extern void vchiq_get_config(struct vchiq_config *config);
702 extern enum vchiq_status
703 vchiq_set_service_option(unsigned int service, enum vchiq_service_option option,