drm/v3d: Add gpu_gem_info node via debugfs
[platform/kernel/linux-rpi.git] / drivers / gpu / drm / v3d / v3d_drv.h
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
16 struct clk;
17 struct platform_device;
18 struct reset_control;
19
20 #define GMP_GRANULARITY (128 * 1024)
21
22 #define V3D_MAX_QUEUES (V3D_CACHE_CLEAN + 1)
23
24 static inline char *
25 v3d_queue_to_string(enum v3d_queue queue)
26 {
27         switch (queue) {
28         case V3D_BIN: return "v3d_bin";
29         case V3D_RENDER: return "v3d_render";
30         case V3D_TFU: return "v3d_tfu";
31         case V3D_CSD: return "v3d_csd";
32         case V3D_CACHE_CLEAN: return "v3d_cache_clean";
33         }
34         return "UNKNOWN";
35 }
36
37 struct v3d_queue_state {
38         struct drm_gpu_scheduler sched;
39
40         u64 fence_context;
41         u64 emit_seqno;
42 };
43
44 struct v3d_queue_pid_stats {
45         struct  list_head list;
46         u64     runtime;
47         /* Time in jiffes.to purge the stats of this process. Every time a
48          * process sends a new job to the queue, this timeout is delayed by
49          * V3D_QUEUE_STATS_TIMEOUT while the gpu_pid_stats_timeout of the
50          * queue is not reached.
51          */
52         unsigned long timeout_purge;
53         u32     jobs_sent;
54         pid_t   pid;
55 };
56
57 struct v3d_queue_stats {
58         struct mutex lock;
59         u64     last_exec_start;
60         u64     last_exec_end;
61         u64     runtime;
62         u32     jobs_sent;
63         /* Time in jiffes to stop collecting gpu stats by process. This is
64          * increased by every access to*the debugfs interface gpu_pid_usage.
65          * If the debugfs is not used stats are not collected.
66          */
67         unsigned long gpu_pid_stats_timeout;
68         pid_t   last_pid;
69         struct list_head pid_stats_list;
70 };
71
72 /* pid_stats by process (v3d_queue_pid_stats) are recorded if there is an
73  * access to the gpu_pid_usageare debugfs interface for the last
74  * V3D_QUEUE_STATS_TIMEOUT (70s).
75  *
76  * The same timeout is used to purge the stats by process for those process
77  * that have not sent jobs this period.
78  */
79 #define V3D_QUEUE_STATS_TIMEOUT (70 * HZ)
80
81
82 /* Performance monitor object. The perform lifetime is controlled by userspace
83  * using perfmon related ioctls. A perfmon can be attached to a submit_cl
84  * request, and when this is the case, HW perf counters will be activated just
85  * before the submit_cl is submitted to the GPU and disabled when the job is
86  * done. This way, only events related to a specific job will be counted.
87  */
88 struct v3d_perfmon {
89         /* Tracks the number of users of the perfmon, when this counter reaches
90          * zero the perfmon is destroyed.
91          */
92         refcount_t refcnt;
93
94         /* Protects perfmon stop, as it can be invoked from multiple places. */
95         struct mutex lock;
96
97         /* Number of counters activated in this perfmon instance
98          * (should be less than DRM_V3D_MAX_PERF_COUNTERS).
99          */
100         u8 ncounters;
101
102         /* Events counted by the HW perf counters. */
103         u8 counters[DRM_V3D_MAX_PERF_COUNTERS];
104
105         /* Storage for counter values. Counters are incremented by the
106          * HW perf counter values every time the perfmon is attached
107          * to a GPU job.  This way, perfmon users don't have to
108          * retrieve the results after each job if they want to track
109          * events covering several submissions.  Note that counter
110          * values can't be reset, but you can fake a reset by
111          * destroying the perfmon and creating a new one.
112          */
113         u64 values[];
114 };
115
116 struct v3d_dev {
117         struct drm_device drm;
118
119         /* Short representation (e.g. 33, 41) of the V3D tech version
120          * and revision.
121          */
122         int ver;
123         bool single_irq_line;
124
125         void __iomem *hub_regs;
126         void __iomem *core_regs[3];
127         void __iomem *bridge_regs;
128         void __iomem *gca_regs;
129         struct clk *clk;
130         struct delayed_work clk_down_work;
131         unsigned long clk_up_rate, clk_down_rate;
132         struct mutex clk_lock;
133         u32 clk_refcount;
134         bool clk_up;
135
136         struct reset_control *reset;
137
138         /* Virtual and DMA addresses of the single shared page table. */
139         volatile u32 *pt;
140         dma_addr_t pt_paddr;
141
142         /* Virtual and DMA addresses of the MMU's scratch page.  When
143          * a read or write is invalid in the MMU, it will be
144          * redirected here.
145          */
146         void *mmu_scratch;
147         dma_addr_t mmu_scratch_paddr;
148         /* virtual address bits from V3D to the MMU. */
149         int va_width;
150
151         /* Number of V3D cores. */
152         u32 cores;
153
154         /* Allocator managing the address space.  All units are in
155          * number of pages.
156          */
157         struct drm_mm mm;
158         spinlock_t mm_lock;
159
160         struct work_struct overflow_mem_work;
161
162         struct v3d_bin_job *bin_job;
163         struct v3d_render_job *render_job;
164         struct v3d_tfu_job *tfu_job;
165         struct v3d_csd_job *csd_job;
166
167         struct v3d_queue_state queue[V3D_MAX_QUEUES];
168
169         /* Spinlock used to synchronize the overflow memory
170          * management against bin job submission.
171          */
172         spinlock_t job_lock;
173
174         /* Used to track the active perfmon if any. */
175         struct v3d_perfmon *active_perfmon;
176
177         /* Protects bo_stats */
178         struct mutex bo_lock;
179
180         /* Lock taken when resetting the GPU, to keep multiple
181          * processes from trying to park the scheduler threads and
182          * reset at once.
183          */
184         struct mutex reset_lock;
185
186         /* Lock taken when creating and pushing the GPU scheduler
187          * jobs, to keep the sched-fence seqnos in order.
188          */
189         struct mutex sched_lock;
190
191         /* Lock taken during a cache clean and when initiating an L2
192          * flush, to keep L2 flushes from interfering with the
193          * synchronous L2 cleans.
194          */
195         struct mutex cache_clean_lock;
196
197         struct {
198                 u32 num_allocated;
199                 u32 pages_allocated;
200         } bo_stats;
201
202         struct v3d_queue_stats gpu_queue_stats[V3D_MAX_QUEUES];
203 };
204
205 static inline struct v3d_dev *
206 to_v3d_dev(struct drm_device *dev)
207 {
208         return container_of(dev, struct v3d_dev, drm);
209 }
210
211 static inline bool
212 v3d_has_csd(struct v3d_dev *v3d)
213 {
214         return v3d->ver >= 41;
215 }
216
217 #define v3d_to_pdev(v3d) to_platform_device((v3d)->drm.dev)
218
219 struct drm_v3d_file_private {
220         pid_t pid;
221         pid_t tgid;
222 };
223
224 /* The per-fd struct, which tracks the MMU mappings. */
225 struct v3d_file_priv {
226         struct v3d_dev *v3d;
227
228         struct {
229                 struct idr idr;
230                 struct mutex lock;
231         } perfmon;
232
233         struct drm_sched_entity sched_entity[V3D_MAX_QUEUES];
234         struct drm_v3d_file_private priv;
235 };
236
237 struct v3d_bo {
238         struct drm_gem_shmem_object base;
239
240         struct drm_mm_node node;
241
242         /* List entry for the BO's position in
243          * v3d_render_job->unref_list
244          */
245         struct list_head unref_head;
246 };
247
248 static inline struct v3d_bo *
249 to_v3d_bo(struct drm_gem_object *bo)
250 {
251         return (struct v3d_bo *)bo;
252 }
253
254 struct v3d_fence {
255         struct dma_fence base;
256         struct drm_device *dev;
257         /* v3d seqno for signaled() test */
258         u64 seqno;
259         enum v3d_queue queue;
260 };
261
262 static inline struct v3d_fence *
263 to_v3d_fence(struct dma_fence *fence)
264 {
265         return (struct v3d_fence *)fence;
266 }
267
268 #define V3D_READ(offset) readl(v3d->hub_regs + offset)
269 #define V3D_WRITE(offset, val) writel(val, v3d->hub_regs + offset)
270
271 #define V3D_BRIDGE_READ(offset) readl(v3d->bridge_regs + offset)
272 #define V3D_BRIDGE_WRITE(offset, val) writel(val, v3d->bridge_regs + offset)
273
274 #define V3D_GCA_READ(offset) readl(v3d->gca_regs + offset)
275 #define V3D_GCA_WRITE(offset, val) writel(val, v3d->gca_regs + offset)
276
277 #define V3D_CORE_READ(core, offset) readl(v3d->core_regs[core] + offset)
278 #define V3D_CORE_WRITE(core, offset, val) writel(val, v3d->core_regs[core] + offset)
279
280 struct v3d_job {
281         struct drm_sched_job base;
282
283         struct kref refcount;
284
285         struct v3d_dev *v3d;
286
287         /* This is the array of BOs that were looked up at the start
288          * of submission.
289          */
290         struct drm_gem_object **bo;
291         u32 bo_count;
292
293         /* v3d fence to be signaled by IRQ handler when the job is complete. */
294         struct dma_fence *irq_fence;
295
296         /* scheduler fence for when the job is considered complete and
297          * the BO reservations can be released.
298          */
299         struct dma_fence *done_fence;
300
301         /* Pointer to a performance monitor object if the user requested it,
302          * NULL otherwise.
303          */
304         struct v3d_perfmon *perfmon;
305
306         /* PID of the process that submitted the job that could be used to
307          * for collecting stats by process of gpu usage.
308          */
309         pid_t client_pid;
310
311         /* Callback for the freeing of the job on refcount going to 0. */
312         void (*free)(struct kref *ref);
313 };
314
315 struct v3d_bin_job {
316         struct v3d_job base;
317
318         /* GPU virtual addresses of the start/end of the CL job. */
319         u32 start, end;
320
321         u32 timedout_ctca, timedout_ctra;
322
323         /* Corresponding render job, for attaching our overflow memory. */
324         struct v3d_render_job *render;
325
326         /* Submitted tile memory allocation start/size, tile state. */
327         u32 qma, qms, qts;
328 };
329
330 struct v3d_render_job {
331         struct v3d_job base;
332
333         /* GPU virtual addresses of the start/end of the CL job. */
334         u32 start, end;
335
336         u32 timedout_ctca, timedout_ctra;
337
338         /* List of overflow BOs used in the job that need to be
339          * released once the job is complete.
340          */
341         struct list_head unref_list;
342 };
343
344 struct v3d_tfu_job {
345         struct v3d_job base;
346
347         struct drm_v3d_submit_tfu args;
348 };
349
350 struct v3d_csd_job {
351         struct v3d_job base;
352
353         u32 timedout_batches;
354
355         struct drm_v3d_submit_csd args;
356 };
357
358 struct v3d_submit_outsync {
359         struct drm_syncobj *syncobj;
360 };
361
362 struct v3d_submit_ext {
363         u32 flags;
364         u32 wait_stage;
365
366         u32 in_sync_count;
367         u64 in_syncs;
368
369         u32 out_sync_count;
370         struct v3d_submit_outsync *out_syncs;
371 };
372
373 /**
374  * __wait_for - magic wait macro
375  *
376  * Macro to help avoid open coding check/wait/timeout patterns. Note that it's
377  * important that we check the condition again after having timed out, since the
378  * timeout could be due to preemption or similar and we've never had a chance to
379  * check the condition before the timeout.
380  */
381 #define __wait_for(OP, COND, US, Wmin, Wmax) ({ \
382         const ktime_t end__ = ktime_add_ns(ktime_get_raw(), 1000ll * (US)); \
383         long wait__ = (Wmin); /* recommended min for usleep is 10 us */ \
384         int ret__;                                                      \
385         might_sleep();                                                  \
386         for (;;) {                                                      \
387                 const bool expired__ = ktime_after(ktime_get_raw(), end__); \
388                 OP;                                                     \
389                 /* Guarantee COND check prior to timeout */             \
390                 barrier();                                              \
391                 if (COND) {                                             \
392                         ret__ = 0;                                      \
393                         break;                                          \
394                 }                                                       \
395                 if (expired__) {                                        \
396                         ret__ = -ETIMEDOUT;                             \
397                         break;                                          \
398                 }                                                       \
399                 usleep_range(wait__, wait__ * 2);                       \
400                 if (wait__ < (Wmax))                                    \
401                         wait__ <<= 1;                                   \
402         }                                                               \
403         ret__;                                                          \
404 })
405
406 #define _wait_for(COND, US, Wmin, Wmax) __wait_for(, (COND), (US), (Wmin), \
407                                                    (Wmax))
408 #define wait_for(COND, MS)              _wait_for((COND), (MS) * 1000, 10, 1000)
409
410 static inline unsigned long nsecs_to_jiffies_timeout(const u64 n)
411 {
412         /* nsecs_to_jiffies64() does not guard against overflow */
413         if ((NSEC_PER_SEC % HZ) != 0 &&
414             div_u64(n, NSEC_PER_SEC) >= MAX_JIFFY_OFFSET / HZ)
415                 return MAX_JIFFY_OFFSET;
416
417         return min_t(u64, MAX_JIFFY_OFFSET, nsecs_to_jiffies64(n) + 1);
418 }
419
420 /* v3d_bo.c */
421 struct drm_gem_object *v3d_create_object(struct drm_device *dev, size_t size);
422 void v3d_free_object(struct drm_gem_object *gem_obj);
423 struct v3d_bo *v3d_bo_create(struct drm_device *dev, struct drm_file *file_priv,
424                              size_t size);
425 int v3d_create_bo_ioctl(struct drm_device *dev, void *data,
426                         struct drm_file *file_priv);
427 int v3d_mmap_bo_ioctl(struct drm_device *dev, void *data,
428                       struct drm_file *file_priv);
429 int v3d_get_bo_offset_ioctl(struct drm_device *dev, void *data,
430                             struct drm_file *file_priv);
431 struct drm_gem_object *v3d_prime_import_sg_table(struct drm_device *dev,
432                                                  struct dma_buf_attachment *attach,
433                                                  struct sg_table *sgt);
434 int v3d_drm_gem_prime_fd_to_handle(struct drm_device *dev,
435                                    struct drm_file *file_priv, int prime_fd,
436                                    uint32_t *handle);
437
438 /* v3d_debugfs.c */
439 void v3d_debugfs_init(struct drm_minor *minor);
440
441 /* v3d_fence.c */
442 extern const struct dma_fence_ops v3d_fence_ops;
443 struct dma_fence *v3d_fence_create(struct v3d_dev *v3d, enum v3d_queue queue);
444
445 /* v3d_gem.c */
446 int v3d_gem_init(struct drm_device *dev);
447 void v3d_gem_destroy(struct drm_device *dev);
448 int v3d_submit_cl_ioctl(struct drm_device *dev, void *data,
449                         struct drm_file *file_priv);
450 int v3d_submit_tfu_ioctl(struct drm_device *dev, void *data,
451                          struct drm_file *file_priv);
452 int v3d_submit_csd_ioctl(struct drm_device *dev, void *data,
453                          struct drm_file *file_priv);
454 int v3d_wait_bo_ioctl(struct drm_device *dev, void *data,
455                       struct drm_file *file_priv);
456 void v3d_job_cleanup(struct v3d_job *job);
457 void v3d_job_put(struct v3d_job *job);
458 void v3d_reset(struct v3d_dev *v3d);
459 void v3d_invalidate_caches(struct v3d_dev *v3d);
460 void v3d_clean_caches(struct v3d_dev *v3d);
461
462 /* v3d_irq.c */
463 int v3d_irq_init(struct v3d_dev *v3d);
464 void v3d_irq_enable(struct v3d_dev *v3d);
465 void v3d_irq_disable(struct v3d_dev *v3d);
466 void v3d_irq_reset(struct v3d_dev *v3d);
467
468 /* v3d_mmu.c */
469 int v3d_mmu_get_offset(struct drm_file *file_priv, struct v3d_bo *bo,
470                        u32 *offset);
471 int v3d_mmu_set_page_table(struct v3d_dev *v3d);
472 void v3d_mmu_insert_ptes(struct v3d_bo *bo);
473 void v3d_mmu_remove_ptes(struct v3d_bo *bo);
474
475 /* v3d_sched.c */
476 int v3d_sched_init(struct v3d_dev *v3d);
477 void v3d_sched_fini(struct v3d_dev *v3d);
478 void v3d_sched_stats_update(struct v3d_queue_stats *queue_stats);
479
480 /* v3d_perfmon.c */
481 void v3d_perfmon_get(struct v3d_perfmon *perfmon);
482 void v3d_perfmon_put(struct v3d_perfmon *perfmon);
483 void v3d_perfmon_start(struct v3d_dev *v3d, struct v3d_perfmon *perfmon);
484 void v3d_perfmon_stop(struct v3d_dev *v3d, struct v3d_perfmon *perfmon,
485                       bool capture);
486 struct v3d_perfmon *v3d_perfmon_find(struct v3d_file_priv *v3d_priv, int id);
487 void v3d_perfmon_open_file(struct v3d_file_priv *v3d_priv);
488 void v3d_perfmon_close_file(struct v3d_file_priv *v3d_priv);
489 int v3d_perfmon_create_ioctl(struct drm_device *dev, void *data,
490                              struct drm_file *file_priv);
491 int v3d_perfmon_destroy_ioctl(struct drm_device *dev, void *data,
492                               struct drm_file *file_priv);
493 int v3d_perfmon_get_values_ioctl(struct drm_device *dev, void *data,
494                                  struct drm_file *file_priv);