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>
13 #include <linux/raspberrypi/vchiq.h>
15 #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 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), \
77 #define VCHIQ_FOURCC_AS_4CHARS(fourcc) \
78 ((fourcc) >> 24) & 0xff, \
79 ((fourcc) >> 16) & 0xff, \
80 ((fourcc) >> 8) & 0xff, \
83 typedef uint32_t BITSET_T;
85 vchiq_static_assert((sizeof(BITSET_T) * 8) == 32);
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))
96 #if VCHIQ_ENABLE_DEBUG
97 DEBUG_SLOT_HANDLER_COUNT,
98 DEBUG_SLOT_HANDLER_LINE,
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,
111 #if VCHIQ_ENABLE_DEBUG
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)
121 #else /* VCHIQ_ENABLE_DEBUG */
123 #define DEBUG_INITIALISE(local)
124 #define DEBUG_TRACE(d)
125 #define DEBUG_VALUE(d, v)
126 #define DEBUG_COUNT(d)
128 #endif /* VCHIQ_ENABLE_DEBUG */
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
144 VCHIQ_SRVSTATE_HIDDEN,
145 VCHIQ_SRVSTATE_LISTENING,
146 VCHIQ_SRVSTATE_OPENING,
148 VCHIQ_SRVSTATE_OPENSYNC,
149 VCHIQ_SRVSTATE_CLOSESENT,
150 VCHIQ_SRVSTATE_CLOSERECVD,
151 VCHIQ_SRVSTATE_CLOSEWAIT,
152 VCHIQ_SRVSTATE_CLOSED
155 enum vchiq_bulk_dir {
160 typedef void (*vchiq_userdata_term)(void *userdata);
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];
182 struct remote_event {
188 struct opaque_platform_state;
191 char data[VCHIQ_SLOT_SIZE];
194 struct vchiq_slot_info {
195 /* Use two counters rather than one to avoid the need for a mutex. */
200 struct vchiq_service {
201 struct vchiq_service_base base;
203 struct kref ref_count;
206 vchiq_userdata_term userdata_term;
207 unsigned int localport;
208 unsigned int remoteport;
220 struct vchiq_state *state;
221 struct vchiq_instance *instance;
223 int service_use_count;
225 struct vchiq_bulk_queue bulk_tx;
226 struct vchiq_bulk_queue bulk_rx;
228 struct completion remove_event;
229 struct completion bulk_remove_event;
230 struct mutex bulk_mutex;
232 struct service_stats_struct {
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;
250 struct completion msg_queue_pop;
251 struct completion msg_queue_push;
252 struct vchiq_header *msg_queue[VCHIQ_MAX_SLOTS];
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.
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;
269 struct vchiq_shared_state {
271 /* A non-zero value here indicates that the content is valid. */
274 /* The first and last (inclusive) slots allocated to the owner. */
278 /* The slot allocated to synchronous messages from the owner. */
282 * Signalling this event indicates that owner's slot handler thread
285 struct remote_event trigger;
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.
294 /* This event should be signalled when a slot is recycled. */
295 struct remote_event recycle;
297 /* The slot_queue index where the next recycled slot will be written. */
298 int slot_queue_recycle;
300 /* This event should be signalled when a synchronous message is sent. */
301 struct remote_event sync_trigger;
304 * This event should be signalled when a synchronous message has been
307 struct remote_event sync_release;
309 /* A circular buffer of slot indexes. */
310 int slot_queue[VCHIQ_MAX_SLOTS_PER_SIDE];
312 /* Debugging state */
313 int debug[DEBUG_MAX];
316 struct vchiq_slot_zero {
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];
333 enum vchiq_connstate conn_state;
334 short version_common;
336 struct vchiq_shared_state *local;
337 struct vchiq_shared_state *remote;
338 struct vchiq_slot *slot_data;
340 unsigned short default_slot_quota;
341 unsigned short default_message_quota;
343 /* Event indicating connect message received */
344 struct completion connect;
346 /* Mutex protecting services */
348 struct vchiq_instance **instance;
350 /* Processes incoming messages */
351 struct task_struct *slot_handler_thread;
353 /* Processes recycled slots */
354 struct task_struct *recycle_thread;
356 /* Processes synchronous messages */
357 struct task_struct *sync_thread;
359 /* Local implementation of the trigger remote event */
360 wait_queue_head_t trigger_event;
362 /* Local implementation of the recycle remote event */
363 wait_queue_head_t recycle_event;
365 /* Local implementation of the sync trigger remote event */
366 wait_queue_head_t sync_trigger_event;
368 /* Local implementation of the sync release remote event */
369 wait_queue_head_t sync_release_event;
373 struct vchiq_slot_info *rx_info;
375 struct mutex slot_mutex;
377 struct mutex recycle_mutex;
379 struct mutex sync_mutex;
381 struct mutex bulk_transfer_mutex;
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.
392 * A cached copy of local->tx_pos. Only write to local->tx_pos, and read
393 * from remote->tx_pos.
397 /* The slot_queue index of the slot to become available next. */
398 int slot_queue_available;
400 /* A flag to indicate if any poll has been requested */
403 /* Ths index of the previous slot used for data messages. */
404 int previous_data_index;
406 /* The number of slots occupied by data messages. */
407 unsigned short data_use_count;
409 /* The maximum number of slots to be occupied by data messages. */
410 unsigned short data_quota;
412 /* An array of bit sets indicating which services must be polled. */
413 atomic_t poll_services[BITSET_SIZE(VCHIQ_MAX_SERVICES)];
415 /* The number of the first unused service */
418 /* Signalled when a free slot becomes available. */
419 struct completion slot_available_event;
421 struct completion slot_remove_event;
423 /* Signalled when a free data slot becomes available. */
424 struct completion data_quota_event;
426 struct state_stats_struct {
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];
438 struct opaque_platform_state *platform_state;
442 struct vchiq_bulk *bulk;
443 struct completion event;
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
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 */
460 extern spinlock_t bulk_waiter_spinlock;
462 extern int vchiq_core_log_level;
463 extern int vchiq_core_msg_log_level;
464 extern int vchiq_sync_log_level;
466 extern struct vchiq_state *vchiq_states[VCHIQ_MAX_STATES];
469 get_conn_state_name(enum vchiq_connstate conn_state);
471 extern struct vchiq_slot_zero *
472 vchiq_init_slots(void *mem_base, int mem_size);
475 vchiq_init_state(struct vchiq_state *state, struct vchiq_slot_zero *slot_zero);
477 extern enum vchiq_status
478 vchiq_connect_internal(struct vchiq_state *state, struct vchiq_instance *instance);
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);
486 extern enum vchiq_status
487 vchiq_open_service_internal(struct vchiq_service *service, int client_id);
489 extern enum vchiq_status
490 vchiq_close_service_internal(struct vchiq_service *service, int close_recvd);
493 vchiq_terminate_service_internal(struct vchiq_service *service);
496 vchiq_free_service_internal(struct vchiq_service *service);
499 vchiq_shutdown_internal(struct vchiq_state *state, struct vchiq_instance *instance);
502 remote_event_pollall(struct vchiq_state *state);
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);
510 vchiq_dump_state(void *dump_context, struct vchiq_state *state);
513 vchiq_dump_service_state(void *dump_context, struct vchiq_service *service);
516 vchiq_loud_error_header(void);
519 vchiq_loud_error_footer(void);
522 request_poll(struct vchiq_state *state, struct vchiq_service *service,
525 static inline struct vchiq_service *
526 handle_to_service(unsigned int handle)
528 int idx = handle & (VCHIQ_MAX_SERVICES - 1);
529 struct vchiq_state *state = vchiq_states[(handle / VCHIQ_MAX_SERVICES) &
530 (VCHIQ_MAX_STATES - 1)];
534 return rcu_dereference(state->services[idx]);
537 extern struct vchiq_service *
538 find_service_by_handle(unsigned int handle);
540 extern struct vchiq_service *
541 find_service_by_port(struct vchiq_state *state, int localport);
543 extern struct vchiq_service *
544 find_service_for_instance(struct vchiq_instance *instance,
545 unsigned int handle);
547 extern struct vchiq_service *
548 find_closed_service_for_instance(struct vchiq_instance *instance,
549 unsigned int handle);
551 extern struct vchiq_service *
552 __next_service_by_instance(struct vchiq_state *state,
553 struct vchiq_instance *instance,
556 extern struct vchiq_service *
557 next_service_by_instance(struct vchiq_state *state,
558 struct vchiq_instance *instance,
562 lock_service(struct vchiq_service *service);
565 unlock_service(struct vchiq_service *service);
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),
575 * The following functions are called from vchiq_core, and external
576 * implementations must be provided.
580 vchiq_prepare_bulk_data(struct vchiq_bulk *bulk, void *offset,
581 void __user *uoffset, int size, int dir);
584 vchiq_complete_bulk(struct vchiq_bulk *bulk);
587 remote_event_signal(struct remote_event *event);
590 vchiq_dump(void *dump_context, const char *str, int len);
593 vchiq_dump_platform_state(void *dump_context);
596 vchiq_dump_platform_instances(void *dump_context);
599 vchiq_dump_platform_service_state(void *dump_context,
600 struct vchiq_service *service);
603 vchiq_use_service_internal(struct vchiq_service *service);
606 vchiq_release_service_internal(struct vchiq_service *service);
609 vchiq_on_remote_use(struct vchiq_state *state);
612 vchiq_on_remote_release(struct vchiq_state *state);
615 vchiq_platform_init_state(struct vchiq_state *state);
617 extern enum vchiq_status
618 vchiq_check_service(struct vchiq_service *service);
621 vchiq_on_remote_use_active(struct vchiq_state *state);
623 extern enum vchiq_status
624 vchiq_send_remote_use(struct vchiq_state *state);
626 extern enum vchiq_status
627 vchiq_send_remote_use_active(struct vchiq_state *state);
630 vchiq_platform_conn_state_changed(struct vchiq_state *state,
631 enum vchiq_connstate oldstate,
632 enum vchiq_connstate newstate);
635 vchiq_set_conn_state(struct vchiq_state *state, enum vchiq_connstate newstate);
638 vchiq_log_dump_mem(const char *label, uint32_t addr, const void *voidMem,
641 extern enum vchiq_status vchiq_remove_service(unsigned int service);
643 extern int vchiq_get_client_id(unsigned int service);
645 extern void vchiq_get_config(struct vchiq_config *config);
648 vchiq_set_service_option(unsigned int service, enum vchiq_service_option option,