1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3 * Copyright (C) Rockchip Electronics Co., Ltd.
4 *
5 * Author: Huang Lee <Putin.li@rock-chips.com>
6 */
7
8 #ifndef __LINUX_RVE_DRV_H_
9 #define __LINUX_RVE_DRV_H_
10
11 #include <linux/clk.h>
12 #include <linux/completion.h>
13 #include <linux/debugfs.h>
14 #include <linux/delay.h>
15 #include <linux/device.h>
16 #include <linux/dma-buf-cache.h>
17 #include <linux/dma-mapping.h>
18 #include <linux/err.h>
19 #include <linux/fb.h>
20 #include <linux/fs.h>
21 #include <linux/init.h>
22 #include <linux/interrupt.h>
23 #include <linux/io.h>
24 #include <linux/irq.h>
25 #include <linux/kernel.h>
26 #include <linux/kref.h>
27 #include <linux/miscdevice.h>
28 #include <linux/module.h>
29 #include <linux/mutex.h>
30 #include <linux/of_device.h>
31 #include <linux/platform_device.h>
32 #include <linux/poll.h>
33 #include <linux/regulator/consumer.h>
34 #include <linux/scatterlist.h>
35 #include <linux/sched.h>
36 #include <linux/slab.h>
37 #include <linux/spinlock.h>
38 #include <linux/syscalls.h>
39 #include <linux/time.h>
40 #include <linux/timer.h>
41 #include <linux/uaccess.h>
42 #include <linux/version.h>
43 #include <linux/wait.h>
44 #include <linux/wakelock.h>
45 #include <linux/pm_runtime.h>
46 #include <linux/sched/mm.h>
47
48 #include <asm/cacheflush.h>
49
50 #include <linux/iommu.h>
51 #include <linux/iova.h>
52 #include <linux/dma-iommu.h>
53 #include <linux/dma-map-ops.h>
54 #include <linux/hrtimer.h>
55
56 #include "rve_debugger.h"
57 #include "rve.h"
58
59 /* sample interval: 1000ms */
60 #define RVE_LOAD_INTERVAL 1000000000
61
62 /* Driver information */
63 #define DRIVER_DESC "RVE Device Driver"
64 #define DRIVER_NAME "rve"
65
66 #define STR_HELPER(x) #x
67 #define STR(x) STR_HELPER(x)
68
69 #define RVE_MAJOR_VERSION_MASK (0x0000FF00)
70 #define RVE_MINOR_VERSION_MASK (0x000000FF)
71 #define RVE_PROD_NUM_MASK (0xFFFF0000)
72
73 #define DRIVER_MAJOR_VERSION 1
74 #define DRIVER_MINOR_VERSION 0
75 #define DRIVER_REVISION_VERSION 4
76
77 #define DRIVER_VERSION (STR(DRIVER_MAJOR_VERSION) "." STR(DRIVER_MINOR_VERSION) \
78 "." STR(DRIVER_REVISION_VERSION))
79
80 /* time limit */
81 #define RVE_ASYNC_TIMEOUT_DELAY 500
82 #define RVE_SYNC_TIMEOUT_DELAY HZ
83 #define RVE_RESET_TIMEOUT 10000
84
85 #define RVE_BUFFER_POOL_MAX_SIZE 64
86 #define RVE_MAX_SCHEDULER 1
87
88 #define RVE_MAX_BUS_CLK 10
89 #define RVE_MAX_PID_INFO 10
90
91 extern struct rve_drvdata_t *rve_drvdata;
92
93 enum {
94 RVE_SCHEDULER_CORE0 = 1,
95 RVE_NONE_CORE = 0,
96 };
97
98 enum {
99 RVE_CMD_SLAVE = 1,
100 RVE_CMD_MASTER = 2,
101 };
102
103 struct rve_fence_context {
104 unsigned int context;
105 unsigned int seqno;
106 spinlock_t spinlock;
107 };
108
109 struct rve_fence_waiter {
110 /* Base sync driver waiter structure */
111 struct dma_fence_cb waiter;
112
113 struct rve_job *job;
114 };
115
116 struct rve_scheduler_t;
117 struct rve_internal_ctx_t;
118
119 struct rve_session {
120 int id;
121
122 pid_t tgid;
123 };
124
125 struct rve_job {
126 struct list_head head;
127 struct rve_scheduler_t *scheduler;
128 struct rve_session *session;
129
130 struct rve_cmd_reg_array_t *regcmd_data;
131
132 struct rve_internal_ctx_t *ctx;
133
134 /* for rve virtual_address */
135 struct mm_struct *mm;
136
137 struct dma_fence *out_fence;
138 struct dma_fence *in_fence;
139 spinlock_t fence_lock;
140 ktime_t timestamp;
141 ktime_t hw_running_time;
142 ktime_t hw_recoder_time;
143 unsigned int flags;
144
145 int priority;
146 int core;
147 int ret;
148 pid_t pid;
149 };
150
151 struct rve_backend_ops {
152 int (*get_version)(struct rve_scheduler_t *scheduler);
153 int (*set_reg)(struct rve_job *job, struct rve_scheduler_t *scheduler);
154 int (*init_reg)(struct rve_job *job);
155 void (*soft_reset)(struct rve_scheduler_t *scheduler);
156 };
157
158 struct rve_timer {
159 u32 busy_time;
160 u32 busy_time_record;
161 };
162
163 struct rve_sche_pid_info_t {
164 pid_t pid;
165 /* hw total use time, per hrtimer */
166 u32 hw_time_total;
167
168 uint32_t last_job_rd_bandwidth;
169 uint32_t last_job_wr_bandwidth;
170 uint32_t last_job_cycle_cnt;
171 };
172
173 struct rve_sche_session_info_t {
174 struct rve_sche_pid_info_t pid_info[RVE_MAX_PID_INFO];
175
176 int pd_refcount;
177
178 /* the bandwidth of total read bytes, per hrtimer */
179 uint32_t rd_bandwidth;
180 /* the bandwidth of total write bytes, per hrtimer */
181 uint32_t wr_bandwidth;
182 /* the total running cycle of current frame, per hrtimer */
183 uint32_t cycle_cnt;
184 /* total interrupt count */
185 uint64_t total_int_cnt;
186 };
187
188 struct rve_scheduler_t {
189 struct device *dev;
190 void __iomem *rve_base;
191
192 struct clk *clks[RVE_MAX_BUS_CLK];
193 int num_clks;
194
195 struct rve_job *running_job;
196 struct list_head todo_list;
197 spinlock_t irq_lock;
198 wait_queue_head_t job_done_wq;
199 const struct rve_backend_ops *ops;
200 const struct rve_hw_data *data;
201 int job_count;
202 int irq;
203 struct rve_version_t version;
204 int core;
205
206 struct rve_timer timer;
207
208 struct rve_sche_session_info_t session;
209 };
210
211 struct rve_cmd_reg_array_t {
212 uint32_t cmd_reg[58];
213 };
214
215 struct rve_ctx_debug_info_t {
216 pid_t pid;
217 u32 timestamp;
218 /* hw total use time, per hrtimer */
219 u32 hw_time_total;
220 /* last job use time, per hrtimer*/
221 u32 last_job_use_time;
222 /* last job hardware use time, per hrtimer*/
223 u32 last_job_hw_use_time;
224 /* the most time-consuming job, per hrtimer */
225 u32 max_cost_time_per_sec;
226 };
227
228 struct rve_internal_ctx_t {
229 struct rve_scheduler_t *scheduler;
230 struct rve_session *session;
231
232 struct rve_cmd_reg_array_t *regcmd_data;
233 uint32_t cmd_num;
234
235 uint32_t sync_mode;
236 int flags;
237 int id;
238
239 uint32_t running_job_count;
240 uint32_t finished_job_count;
241 bool is_running;
242
243 uint32_t disable_auto_cancel;
244
245 int priority;
246 int32_t out_fence_fd;
247 int32_t in_fence_fd;
248
249 struct dma_fence *out_fence;
250
251 spinlock_t lock;
252 struct kref refcount;
253
254 /* debug info */
255 struct rve_ctx_debug_info_t debug_info;
256
257 /* TODO: add some common work */
258 };
259
260 struct rve_pending_ctx_manager {
261 spinlock_t lock;
262
263 /*
264 * @ctx_id_idr:
265 *
266 * Mapping of ctx id to object pointers. Used by the GEM
267 * subsystem. Protected by @lock.
268 */
269 struct idr ctx_id_idr;
270
271 int ctx_count;
272 };
273
274 struct rve_session_manager {
275 struct mutex lock;
276
277 struct idr ctx_id_idr;
278
279 int session_cnt;
280 };
281
282 struct rve_drvdata_t {
283 struct rve_fence_context *fence_ctx;
284
285 /* used by rve2's mmu lock */
286 struct mutex lock;
287
288 struct rve_scheduler_t *scheduler[RVE_MAX_SCHEDULER];
289 int num_of_scheduler;
290
291 struct delayed_work power_off_work;
292 struct wake_lock wake_lock;
293
294 struct rve_mm *mm;
295
296 /* rve_job pending manager, import by RVE_IOC_START_CONFIG */
297 struct rve_pending_ctx_manager *pend_ctx_manager;
298
299 struct rve_session_manager *session_manager;
300
301 #ifdef CONFIG_ROCKCHIP_RVE_DEBUGGER
302 struct rve_debugger *debugger;
303 #endif
304 };
305
306 struct rve_irqs_data_t {
307 const char *name;
308 irqreturn_t (*irq_hdl)(int irq, void *ctx);
309 irqreturn_t (*irq_thread)(int irq, void *ctx);
310 };
311
312 struct rve_match_data_t {
313 const char * const *clks;
314 int num_clks;
315 const struct rve_irqs_data_t *irqs;
316 int num_irqs;
317 };
318
rve_read(int offset,struct rve_scheduler_t * scheduler)319 static inline int rve_read(int offset, struct rve_scheduler_t *scheduler)
320 {
321 return readl(scheduler->rve_base + offset);
322 }
323
rve_write(int value,int offset,struct rve_scheduler_t * scheduler)324 static inline void rve_write(int value, int offset, struct rve_scheduler_t *scheduler)
325 {
326 writel(value, scheduler->rve_base + offset);
327 }
328
329 int rve_power_enable(struct rve_scheduler_t *scheduler);
330 int rve_power_disable(struct rve_scheduler_t *scheduler);
331
332 #endif /* __LINUX_RVE_FENCE_H_ */
333