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