1 // SPDX-License-Identifier: GPL-2.0+
2 /* Copyright (C) 2015-2018 Broadcom */
3
4 #include <linux/delay.h>
5 #include <linux/mutex.h>
6 #include <linux/spinlock_types.h>
7 #include <linux/workqueue.h>
8
9 #include <drm/drm_encoder.h>
10 #include <drm/drm_gem.h>
11 #include <drm/drm_gem_shmem_helper.h>
12 #include <drm/gpu_scheduler.h>
13
14 #include "v3d_performance_counters.h"
15
16 #include "uapi/drm/v3d_drm.h"
17
18 struct clk;
19 struct platform_device;
20 struct reset_control;
21
22 #define GMP_GRANULARITY (128 * 1024)
23
24 #define V3D_MMU_PAGE_SHIFT 12
25
26 #define V3D_MAX_QUEUES (V3D_CPU + 1)
27
v3d_queue_to_string(enum v3d_queue queue)28 static inline char *v3d_queue_to_string(enum v3d_queue queue)
29 {
30 switch (queue) {
31 case V3D_BIN: return "bin";
32 case V3D_RENDER: return "render";
33 case V3D_TFU: return "tfu";
34 case V3D_CSD: return "csd";
35 case V3D_CACHE_CLEAN: return "cache_clean";
36 case V3D_CPU: return "cpu";
37 }
38 return "UNKNOWN";
39 }
40
41 struct v3d_stats {
42 u64 start_ns;
43 u64 enabled_ns;
44 u64 jobs_completed;
45
46 /*
47 * This seqcount is used to protect the access to the GPU stats
48 * variables. It must be used as, while we are reading the stats,
49 * IRQs can happen and the stats can be updated.
50 */
51 seqcount_t lock;
52 };
53
54 struct v3d_queue_state {
55 struct drm_gpu_scheduler sched;
56
57 u64 fence_context;
58 u64 emit_seqno;
59
60 /* Stores the GPU stats for this queue in the global context. */
61 struct v3d_stats stats;
62 };
63
64 /* Performance monitor object. The perform lifetime is controlled by userspace
65 * using perfmon related ioctls. A perfmon can be attached to a submit_cl
66 * request, and when this is the case, HW perf counters will be activated just
67 * before the submit_cl is submitted to the GPU and disabled when the job is
68 * done. This way, only events related to a specific job will be counted.
69 */
70 struct v3d_perfmon {
71 /* Tracks the number of users of the perfmon, when this counter reaches
72 * zero the perfmon is destroyed.
73 */
74 refcount_t refcnt;
75
76 /* Protects perfmon stop, as it can be invoked from multiple places. */
77 struct mutex lock;
78
79 /* Number of counters activated in this perfmon instance
80 * (should be less than DRM_V3D_MAX_PERF_COUNTERS).
81 */
82 u8 ncounters;
83
84 /* Events counted by the HW perf counters. */
85 u8 counters[DRM_V3D_MAX_PERF_COUNTERS];
86
87 /* Storage for counter values. Counters are incremented by the
88 * HW perf counter values every time the perfmon is attached
89 * to a GPU job. This way, perfmon users don't have to
90 * retrieve the results after each job if they want to track
91 * events covering several submissions. Note that counter
92 * values can't be reset, but you can fake a reset by
93 * destroying the perfmon and creating a new one.
94 */
95 u64 values[] __counted_by(ncounters);
96 };
97
98 enum v3d_irq {
99 V3D_CORE_IRQ,
100 V3D_HUB_IRQ,
101 V3D_MAX_IRQS,
102 };
103
104 struct v3d_dev {
105 struct drm_device drm;
106
107 /* Short representation (e.g. 33, 41) of the V3D tech version */
108 int ver;
109
110 /* Short representation (e.g. 5, 6) of the V3D tech revision */
111 int rev;
112
113 bool single_irq_line;
114
115 int irq[V3D_MAX_IRQS];
116
117 struct v3d_perfmon_info perfmon_info;
118
119 void __iomem *hub_regs;
120 void __iomem *core_regs[3];
121 void __iomem *bridge_regs;
122 void __iomem *gca_regs;
123 struct clk *clk;
124 struct reset_control *reset;
125
126 /* Virtual and DMA addresses of the single shared page table. */
127 volatile u32 *pt;
128 dma_addr_t pt_paddr;
129
130 /* Virtual and DMA addresses of the MMU's scratch page. When
131 * a read or write is invalid in the MMU, it will be
132 * redirected here.
133 */
134 void *mmu_scratch;
135 dma_addr_t mmu_scratch_paddr;
136 /* virtual address bits from V3D to the MMU. */
137 int va_width;
138
139 /* Number of V3D cores. */
140 u32 cores;
141
142 /* Allocator managing the address space. All units are in
143 * number of pages.
144 */
145 struct drm_mm mm;
146 spinlock_t mm_lock;
147
148 struct work_struct overflow_mem_work;
149
150 struct v3d_bin_job *bin_job;
151 struct v3d_render_job *render_job;
152 struct v3d_tfu_job *tfu_job;
153 struct v3d_csd_job *csd_job;
154 struct v3d_cpu_job *cpu_job;
155
156 struct v3d_queue_state queue[V3D_MAX_QUEUES];
157
158 /* Spinlock used to synchronize the overflow memory
159 * management against bin job submission.
160 */
161 spinlock_t job_lock;
162
163 /* Used to track the active perfmon if any. */
164 struct v3d_perfmon *active_perfmon;
165
166 /* Protects bo_stats */
167 struct mutex bo_lock;
168
169 /* Lock taken when resetting the GPU, to keep multiple
170 * processes from trying to park the scheduler threads and
171 * reset at once.
172 */
173 struct mutex reset_lock;
174
175 /* Lock taken when creating and pushing the GPU scheduler
176 * jobs, to keep the sched-fence seqnos in order.
177 */
178 struct mutex sched_lock;
179
180 /* Lock taken during a cache clean and when initiating an L2
181 * flush, to keep L2 flushes from interfering with the
182 * synchronous L2 cleans.
183 */
184 struct mutex cache_clean_lock;
185
186 struct {
187 u32 num_allocated;
188 u32 pages_allocated;
189 } bo_stats;
190 };
191
192 static inline struct v3d_dev *
to_v3d_dev(struct drm_device * dev)193 to_v3d_dev(struct drm_device *dev)
194 {
195 return container_of(dev, struct v3d_dev, drm);
196 }
197
198 static inline bool
v3d_has_csd(struct v3d_dev * v3d)199 v3d_has_csd(struct v3d_dev *v3d)
200 {
201 return v3d->ver >= 41;
202 }
203
204 #define v3d_to_pdev(v3d) to_platform_device((v3d)->drm.dev)
205
206 /* The per-fd struct, which tracks the MMU mappings. */
207 struct v3d_file_priv {
208 struct v3d_dev *v3d;
209
210 struct {
211 struct idr idr;
212 struct mutex lock;
213 } perfmon;
214
215 struct drm_sched_entity sched_entity[V3D_MAX_QUEUES];
216
217 /* Stores the GPU stats for a specific queue for this fd. */
218 struct v3d_stats stats[V3D_MAX_QUEUES];
219 };
220
221 struct v3d_bo {
222 struct drm_gem_shmem_object base;
223
224 struct drm_mm_node node;
225
226 /* List entry for the BO's position in
227 * v3d_render_job->unref_list
228 */
229 struct list_head unref_head;
230
231 void *vaddr;
232 };
233
234 static inline struct v3d_bo *
to_v3d_bo(struct drm_gem_object * bo)235 to_v3d_bo(struct drm_gem_object *bo)
236 {
237 return (struct v3d_bo *)bo;
238 }
239
240 struct v3d_fence {
241 struct dma_fence base;
242 struct drm_device *dev;
243 /* v3d seqno for signaled() test */
244 u64 seqno;
245 enum v3d_queue queue;
246 };
247
248 static inline struct v3d_fence *
to_v3d_fence(struct dma_fence * fence)249 to_v3d_fence(struct dma_fence *fence)
250 {
251 return (struct v3d_fence *)fence;
252 }
253
254 #define V3D_READ(offset) readl(v3d->hub_regs + offset)
255 #define V3D_WRITE(offset, val) writel(val, v3d->hub_regs + offset)
256
257 #define V3D_BRIDGE_READ(offset) readl(v3d->bridge_regs + offset)
258 #define V3D_BRIDGE_WRITE(offset, val) writel(val, v3d->bridge_regs + offset)
259
260 #define V3D_GCA_READ(offset) readl(v3d->gca_regs + offset)
261 #define V3D_GCA_WRITE(offset, val) writel(val, v3d->gca_regs + offset)
262
263 #define V3D_CORE_READ(core, offset) readl(v3d->core_regs[core] + offset)
264 #define V3D_CORE_WRITE(core, offset, val) writel(val, v3d->core_regs[core] + offset)
265
266 struct v3d_job {
267 struct drm_sched_job base;
268
269 struct kref refcount;
270
271 struct v3d_dev *v3d;
272
273 /* This is the array of BOs that were looked up at the start
274 * of submission.
275 */
276 struct drm_gem_object **bo;
277 u32 bo_count;
278
279 /* v3d fence to be signaled by IRQ handler when the job is complete. */
280 struct dma_fence *irq_fence;
281
282 /* scheduler fence for when the job is considered complete and
283 * the BO reservations can be released.
284 */
285 struct dma_fence *done_fence;
286
287 /* Pointer to a performance monitor object if the user requested it,
288 * NULL otherwise.
289 */
290 struct v3d_perfmon *perfmon;
291
292 /* File descriptor of the process that submitted the job that could be used
293 * for collecting stats by process of GPU usage.
294 */
295 struct drm_file *file;
296
297 /* Callback for the freeing of the job on refcount going to 0. */
298 void (*free)(struct kref *ref);
299 };
300
301 struct v3d_bin_job {
302 struct v3d_job base;
303
304 /* GPU virtual addresses of the start/end of the CL job. */
305 u32 start, end;
306
307 u32 timedout_ctca, timedout_ctra;
308
309 /* Corresponding render job, for attaching our overflow memory. */
310 struct v3d_render_job *render;
311
312 /* Submitted tile memory allocation start/size, tile state. */
313 u32 qma, qms, qts;
314 };
315
316 struct v3d_render_job {
317 struct v3d_job base;
318
319 /* GPU virtual addresses of the start/end of the CL job. */
320 u32 start, end;
321
322 u32 timedout_ctca, timedout_ctra;
323
324 /* List of overflow BOs used in the job that need to be
325 * released once the job is complete.
326 */
327 struct list_head unref_list;
328 };
329
330 struct v3d_tfu_job {
331 struct v3d_job base;
332
333 struct drm_v3d_submit_tfu args;
334 };
335
336 struct v3d_csd_job {
337 struct v3d_job base;
338
339 u32 timedout_batches;
340
341 struct drm_v3d_submit_csd args;
342 };
343
344 enum v3d_cpu_job_type {
345 V3D_CPU_JOB_TYPE_INDIRECT_CSD = 1,
346 V3D_CPU_JOB_TYPE_TIMESTAMP_QUERY,
347 V3D_CPU_JOB_TYPE_RESET_TIMESTAMP_QUERY,
348 V3D_CPU_JOB_TYPE_COPY_TIMESTAMP_QUERY,
349 V3D_CPU_JOB_TYPE_RESET_PERFORMANCE_QUERY,
350 V3D_CPU_JOB_TYPE_COPY_PERFORMANCE_QUERY,
351 };
352
353 struct v3d_timestamp_query {
354 /* Offset of this query in the timestamp BO for its value. */
355 u32 offset;
356
357 /* Syncobj that indicates the timestamp availability */
358 struct drm_syncobj *syncobj;
359 };
360
361 struct v3d_performance_query {
362 /* Performance monitor IDs for this query */
363 u32 *kperfmon_ids;
364
365 /* Syncobj that indicates the query availability */
366 struct drm_syncobj *syncobj;
367 };
368
369 struct v3d_indirect_csd_info {
370 /* Indirect CSD */
371 struct v3d_csd_job *job;
372
373 /* Clean cache job associated to the Indirect CSD job */
374 struct v3d_job *clean_job;
375
376 /* Offset within the BO where the workgroup counts are stored */
377 u32 offset;
378
379 /* Workgroups size */
380 u32 wg_size;
381
382 /* Indices of the uniforms with the workgroup dispatch counts
383 * in the uniform stream.
384 */
385 u32 wg_uniform_offsets[3];
386
387 /* Indirect BO */
388 struct drm_gem_object *indirect;
389
390 /* Context of the Indirect CSD job */
391 struct ww_acquire_ctx acquire_ctx;
392 };
393
394 struct v3d_timestamp_query_info {
395 struct v3d_timestamp_query *queries;
396
397 u32 count;
398 };
399
400 struct v3d_performance_query_info {
401 struct v3d_performance_query *queries;
402
403 /* Number of performance queries */
404 u32 count;
405
406 /* Number of performance monitors related to that query pool */
407 u32 nperfmons;
408
409 /* Number of performance counters related to that query pool */
410 u32 ncounters;
411 };
412
413 struct v3d_copy_query_results_info {
414 /* Define if should write to buffer using 64 or 32 bits */
415 bool do_64bit;
416
417 /* Define if it can write to buffer even if the query is not available */
418 bool do_partial;
419
420 /* Define if it should write availability bit to buffer */
421 bool availability_bit;
422
423 /* Offset of the copy buffer in the BO */
424 u32 offset;
425
426 /* Stride of the copy buffer in the BO */
427 u32 stride;
428 };
429
430 struct v3d_cpu_job {
431 struct v3d_job base;
432
433 enum v3d_cpu_job_type job_type;
434
435 struct v3d_indirect_csd_info indirect_csd;
436
437 struct v3d_timestamp_query_info timestamp_query;
438
439 struct v3d_copy_query_results_info copy;
440
441 struct v3d_performance_query_info performance_query;
442 };
443
444 typedef void (*v3d_cpu_job_fn)(struct v3d_cpu_job *);
445
446 struct v3d_submit_outsync {
447 struct drm_syncobj *syncobj;
448 };
449
450 struct v3d_submit_ext {
451 u32 flags;
452 u32 wait_stage;
453
454 u32 in_sync_count;
455 u64 in_syncs;
456
457 u32 out_sync_count;
458 struct v3d_submit_outsync *out_syncs;
459 };
460
461 /**
462 * __wait_for - magic wait macro
463 *
464 * Macro to help avoid open coding check/wait/timeout patterns. Note that it's
465 * important that we check the condition again after having timed out, since the
466 * timeout could be due to preemption or similar and we've never had a chance to
467 * check the condition before the timeout.
468 */
469 #define __wait_for(OP, COND, US, Wmin, Wmax) ({ \
470 const ktime_t end__ = ktime_add_ns(ktime_get_raw(), 1000ll * (US)); \
471 long wait__ = (Wmin); /* recommended min for usleep is 10 us */ \
472 int ret__; \
473 might_sleep(); \
474 for (;;) { \
475 const bool expired__ = ktime_after(ktime_get_raw(), end__); \
476 OP; \
477 /* Guarantee COND check prior to timeout */ \
478 barrier(); \
479 if (COND) { \
480 ret__ = 0; \
481 break; \
482 } \
483 if (expired__) { \
484 ret__ = -ETIMEDOUT; \
485 break; \
486 } \
487 usleep_range(wait__, wait__ * 2); \
488 if (wait__ < (Wmax)) \
489 wait__ <<= 1; \
490 } \
491 ret__; \
492 })
493
494 #define _wait_for(COND, US, Wmin, Wmax) __wait_for(, (COND), (US), (Wmin), \
495 (Wmax))
496 #define wait_for(COND, MS) _wait_for((COND), (MS) * 1000, 10, 1000)
497
nsecs_to_jiffies_timeout(const u64 n)498 static inline unsigned long nsecs_to_jiffies_timeout(const u64 n)
499 {
500 /* nsecs_to_jiffies64() does not guard against overflow */
501 if ((NSEC_PER_SEC % HZ) != 0 &&
502 div_u64(n, NSEC_PER_SEC) >= MAX_JIFFY_OFFSET / HZ)
503 return MAX_JIFFY_OFFSET;
504
505 return min_t(u64, MAX_JIFFY_OFFSET, nsecs_to_jiffies64(n) + 1);
506 }
507
508 /* v3d_bo.c */
509 struct drm_gem_object *v3d_create_object(struct drm_device *dev, size_t size);
510 void v3d_free_object(struct drm_gem_object *gem_obj);
511 struct v3d_bo *v3d_bo_create(struct drm_device *dev, struct drm_file *file_priv,
512 size_t size);
513 void v3d_get_bo_vaddr(struct v3d_bo *bo);
514 void v3d_put_bo_vaddr(struct v3d_bo *bo);
515 int v3d_create_bo_ioctl(struct drm_device *dev, void *data,
516 struct drm_file *file_priv);
517 int v3d_mmap_bo_ioctl(struct drm_device *dev, void *data,
518 struct drm_file *file_priv);
519 int v3d_get_bo_offset_ioctl(struct drm_device *dev, void *data,
520 struct drm_file *file_priv);
521 int v3d_wait_bo_ioctl(struct drm_device *dev, void *data,
522 struct drm_file *file_priv);
523 struct drm_gem_object *v3d_prime_import_sg_table(struct drm_device *dev,
524 struct dma_buf_attachment *attach,
525 struct sg_table *sgt);
526
527 /* v3d_debugfs.c */
528 void v3d_debugfs_init(struct drm_minor *minor);
529
530 /* v3d_drv.c */
531 void v3d_get_stats(const struct v3d_stats *stats, u64 timestamp,
532 u64 *active_runtime, u64 *jobs_completed);
533
534 /* v3d_fence.c */
535 extern const struct dma_fence_ops v3d_fence_ops;
536 struct dma_fence *v3d_fence_create(struct v3d_dev *v3d, enum v3d_queue queue);
537
538 /* v3d_gem.c */
539 int v3d_gem_init(struct drm_device *dev);
540 void v3d_gem_destroy(struct drm_device *dev);
541 void v3d_reset(struct v3d_dev *v3d);
542 void v3d_invalidate_caches(struct v3d_dev *v3d);
543 void v3d_clean_caches(struct v3d_dev *v3d);
544
545 /* v3d_submit.c */
546 void v3d_job_cleanup(struct v3d_job *job);
547 void v3d_job_put(struct v3d_job *job);
548 int v3d_submit_cl_ioctl(struct drm_device *dev, void *data,
549 struct drm_file *file_priv);
550 int v3d_submit_tfu_ioctl(struct drm_device *dev, void *data,
551 struct drm_file *file_priv);
552 int v3d_submit_csd_ioctl(struct drm_device *dev, void *data,
553 struct drm_file *file_priv);
554 int v3d_submit_cpu_ioctl(struct drm_device *dev, void *data,
555 struct drm_file *file_priv);
556
557 /* v3d_irq.c */
558 int v3d_irq_init(struct v3d_dev *v3d);
559 void v3d_irq_enable(struct v3d_dev *v3d);
560 void v3d_irq_disable(struct v3d_dev *v3d);
561 void v3d_irq_reset(struct v3d_dev *v3d);
562
563 /* v3d_mmu.c */
564 int v3d_mmu_flush_all(struct v3d_dev *v3d);
565 int v3d_mmu_set_page_table(struct v3d_dev *v3d);
566 void v3d_mmu_insert_ptes(struct v3d_bo *bo);
567 void v3d_mmu_remove_ptes(struct v3d_bo *bo);
568
569 /* v3d_sched.c */
570 void v3d_timestamp_query_info_free(struct v3d_timestamp_query_info *query_info,
571 unsigned int count);
572 void v3d_performance_query_info_free(struct v3d_performance_query_info *query_info,
573 unsigned int count);
574 void v3d_job_update_stats(struct v3d_job *job, enum v3d_queue queue);
575 int v3d_sched_init(struct v3d_dev *v3d);
576 void v3d_sched_fini(struct v3d_dev *v3d);
577
578 /* v3d_perfmon.c */
579 void v3d_perfmon_init(struct v3d_dev *v3d);
580 void v3d_perfmon_get(struct v3d_perfmon *perfmon);
581 void v3d_perfmon_put(struct v3d_perfmon *perfmon);
582 void v3d_perfmon_start(struct v3d_dev *v3d, struct v3d_perfmon *perfmon);
583 void v3d_perfmon_stop(struct v3d_dev *v3d, struct v3d_perfmon *perfmon,
584 bool capture);
585 struct v3d_perfmon *v3d_perfmon_find(struct v3d_file_priv *v3d_priv, int id);
586 void v3d_perfmon_open_file(struct v3d_file_priv *v3d_priv);
587 void v3d_perfmon_close_file(struct v3d_file_priv *v3d_priv);
588 int v3d_perfmon_create_ioctl(struct drm_device *dev, void *data,
589 struct drm_file *file_priv);
590 int v3d_perfmon_destroy_ioctl(struct drm_device *dev, void *data,
591 struct drm_file *file_priv);
592 int v3d_perfmon_get_values_ioctl(struct drm_device *dev, void *data,
593 struct drm_file *file_priv);
594 int v3d_perfmon_get_counter_ioctl(struct drm_device *dev, void *data,
595 struct drm_file *file_priv);
596
597 /* v3d_sysfs.c */
598 int v3d_sysfs_init(struct device *dev);
599 void v3d_sysfs_destroy(struct device *dev);
600