1 /* SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause */
2 /*
3  * Copyright (c) 2014 Raspberry Pi (Trading) Ltd. All rights reserved.
4  * Copyright (c) 2010-2012 Broadcom. All rights reserved.
5  */
6 
7 #ifndef VCHIQ_ARM_H
8 #define VCHIQ_ARM_H
9 
10 #include <linux/mutex.h>
11 #include <linux/platform_device.h>
12 #include <linux/semaphore.h>
13 #include <linux/atomic.h>
14 #include "vchiq_core.h"
15 #include "vchiq_debugfs.h"
16 
17 /* Some per-instance constants */
18 #define MAX_COMPLETIONS 128
19 #define MAX_SERVICES 64
20 #define MAX_ELEMENTS 8
21 #define MSG_QUEUE_SIZE 128
22 
23 enum USE_TYPE_E {
24 	USE_TYPE_SERVICE,
25 	USE_TYPE_VCHIQ
26 };
27 
28 struct user_service {
29 	struct vchiq_service *service;
30 	void __user *userdata;
31 	struct vchiq_instance *instance;
32 	char is_vchi;
33 	char dequeue_pending;
34 	char close_pending;
35 	int message_available_pos;
36 	int msg_insert;
37 	int msg_remove;
38 	struct completion insert_event;
39 	struct completion remove_event;
40 	struct completion close_event;
41 	struct vchiq_header *msg_queue[MSG_QUEUE_SIZE];
42 };
43 
44 struct bulk_waiter_node {
45 	struct bulk_waiter bulk_waiter;
46 	int pid;
47 	struct list_head list;
48 };
49 
50 struct vchiq_instance {
51 	struct vchiq_state *state;
52 	struct vchiq_completion_data_kernel completions[MAX_COMPLETIONS];
53 	int completion_insert;
54 	int completion_remove;
55 	struct completion insert_event;
56 	struct completion remove_event;
57 	struct mutex completion_mutex;
58 
59 	int connected;
60 	int closing;
61 	int pid;
62 	int mark;
63 	int use_close_delivered;
64 	int trace;
65 
66 	struct list_head bulk_waiter_list;
67 	struct mutex bulk_waiter_list_mutex;
68 
69 	struct vchiq_debugfs_node debugfs_node;
70 };
71 
72 struct dump_context {
73 	char __user *buf;
74 	size_t actual;
75 	size_t space;
76 	loff_t offset;
77 };
78 
79 extern int vchiq_arm_log_level;
80 extern int vchiq_susp_log_level;
81 
82 extern spinlock_t msg_queue_spinlock;
83 extern struct vchiq_state g_state;
84 
85 extern struct vchiq_state *
86 vchiq_get_state(void);
87 
88 enum vchiq_status
89 vchiq_use_service(struct vchiq_instance *instance, unsigned int handle);
90 
91 extern enum vchiq_status
92 vchiq_release_service(struct vchiq_instance *instance, unsigned int handle);
93 
94 extern enum vchiq_status
95 vchiq_check_service(struct vchiq_service *service);
96 
97 extern void
98 vchiq_dump_platform_use_state(struct vchiq_state *state);
99 
100 extern void
101 vchiq_dump_service_use_state(struct vchiq_state *state);
102 
103 extern int
104 vchiq_use_internal(struct vchiq_state *state, struct vchiq_service *service,
105 		   enum USE_TYPE_E use_type);
106 extern int
107 vchiq_release_internal(struct vchiq_state *state,
108 		       struct vchiq_service *service);
109 
110 extern struct vchiq_debugfs_node *
111 vchiq_instance_get_debugfs_node(struct vchiq_instance *instance);
112 
113 extern int
114 vchiq_instance_get_use_count(struct vchiq_instance *instance);
115 
116 extern int
117 vchiq_instance_get_pid(struct vchiq_instance *instance);
118 
119 extern int
120 vchiq_instance_get_trace(struct vchiq_instance *instance);
121 
122 extern void
123 vchiq_instance_set_trace(struct vchiq_instance *instance, int trace);
124 
125 #if IS_ENABLED(CONFIG_VCHIQ_CDEV)
126 
127 extern void
128 vchiq_deregister_chrdev(void);
129 
130 extern int
131 vchiq_register_chrdev(struct device *parent);
132 
133 #else
134 
vchiq_deregister_chrdev(void)135 static inline void vchiq_deregister_chrdev(void) { }
vchiq_register_chrdev(struct device * parent)136 static inline int vchiq_register_chrdev(struct device *parent) { return 0; }
137 
138 #endif /* IS_ENABLED(CONFIG_VCHIQ_CDEV) */
139 
140 extern enum vchiq_status
141 service_callback(struct vchiq_instance *vchiq_instance, enum vchiq_reason reason,
142 		 struct vchiq_header *header, unsigned int handle, void *bulk_userdata);
143 
144 extern void
145 free_bulk_waiter(struct vchiq_instance *instance);
146 
147 #endif /* VCHIQ_ARM_H */
148