deeeeac05f786b3d10bf72fb73a2b08966711b81
[platform/kernel/linux-rpi.git] / drivers / gpu / drm / vc4 / vc4_drv.h
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (C) 2015 Broadcom
4  */
5 #ifndef _VC4_DRV_H_
6 #define _VC4_DRV_H_
7
8 #include <linux/delay.h>
9 #include <linux/of.h>
10 #include <linux/refcount.h>
11 #include <linux/uaccess.h>
12
13 #include <drm/drm_atomic.h>
14 #include <drm/drm_debugfs.h>
15 #include <drm/drm_device.h>
16 #include <drm/drm_encoder.h>
17 #include <drm/drm_fourcc.h>
18 #include <drm/drm_gem_dma_helper.h>
19 #include <drm/drm_managed.h>
20 #include <drm/drm_mm.h>
21 #include <drm/drm_modeset_lock.h>
22
23 #include <kunit/test-bug.h>
24
25 #include "uapi/drm/vc4_drm.h"
26 #include "vc4_regs.h"
27
28 struct drm_device;
29 struct drm_gem_object;
30
31 extern const struct drm_driver vc4_drm_driver;
32 extern const struct drm_driver vc5_drm_driver;
33
34 /* Don't forget to update vc4_bo.c: bo_type_names[] when adding to
35  * this.
36  */
37 enum vc4_kernel_bo_type {
38         /* Any kernel allocation (gem_create_object hook) before it
39          * gets another type set.
40          */
41         VC4_BO_TYPE_KERNEL,
42         VC4_BO_TYPE_V3D,
43         VC4_BO_TYPE_V3D_SHADER,
44         VC4_BO_TYPE_DUMB,
45         VC4_BO_TYPE_BIN,
46         VC4_BO_TYPE_RCL,
47         VC4_BO_TYPE_BCL,
48         VC4_BO_TYPE_KERNEL_CACHE,
49         VC4_BO_TYPE_COUNT
50 };
51
52 /* Performance monitor object. The perform lifetime is controlled by userspace
53  * using perfmon related ioctls. A perfmon can be attached to a submit_cl
54  * request, and when this is the case, HW perf counters will be activated just
55  * before the submit_cl is submitted to the GPU and disabled when the job is
56  * done. This way, only events related to a specific job will be counted.
57  */
58 struct vc4_perfmon {
59         struct vc4_dev *dev;
60
61         /* Tracks the number of users of the perfmon, when this counter reaches
62          * zero the perfmon is destroyed.
63          */
64         refcount_t refcnt;
65
66         /* Number of counters activated in this perfmon instance
67          * (should be less than DRM_VC4_MAX_PERF_COUNTERS).
68          */
69         u8 ncounters;
70
71         /* Events counted by the HW perf counters. */
72         u8 events[DRM_VC4_MAX_PERF_COUNTERS];
73
74         /* Storage for counter values. Counters are incremented by the HW
75          * perf counter values every time the perfmon is attached to a GPU job.
76          * This way, perfmon users don't have to retrieve the results after
77          * each job if they want to track events covering several submissions.
78          * Note that counter values can't be reset, but you can fake a reset by
79          * destroying the perfmon and creating a new one.
80          */
81         u64 counters[];
82 };
83
84 enum vc4_gen {
85         VC4_GEN_4,
86         VC4_GEN_5,
87         VC4_GEN_6,
88 };
89
90 struct vc4_dev {
91         struct drm_device base;
92         struct device *dev;
93
94         enum vc4_gen gen;
95
96         unsigned int irq;
97
98         bool firmware_kms;
99         struct rpi_firmware *firmware;
100
101         struct vc4_hvs *hvs;
102         struct vc4_v3d *v3d;
103         struct vc4_fkms *fkms;
104
105         struct vc4_hang_state *hang_state;
106
107         /* The kernel-space BO cache.  Tracks buffers that have been
108          * unreferenced by all other users (refcounts of 0!) but not
109          * yet freed, so we can do cheap allocations.
110          */
111         struct vc4_bo_cache {
112                 /* Array of list heads for entries in the BO cache,
113                  * based on number of pages, so we can do O(1) lookups
114                  * in the cache when allocating.
115                  */
116                 struct list_head *size_list;
117                 uint32_t size_list_size;
118
119                 /* List of all BOs in the cache, ordered by age, so we
120                  * can do O(1) lookups when trying to free old
121                  * buffers.
122                  */
123                 struct list_head time_list;
124                 struct work_struct time_work;
125                 struct timer_list time_timer;
126         } bo_cache;
127
128         u32 num_labels;
129         struct vc4_label {
130                 const char *name;
131                 u32 num_allocated;
132                 u32 size_allocated;
133         } *bo_labels;
134
135         /* Protects bo_cache and bo_labels. */
136         struct mutex bo_lock;
137
138         /* Purgeable BO pool. All BOs in this pool can have their memory
139          * reclaimed if the driver is unable to allocate new BOs. We also
140          * keep stats related to the purge mechanism here.
141          */
142         struct {
143                 struct list_head list;
144                 unsigned int num;
145                 size_t size;
146                 unsigned int purged_num;
147                 size_t purged_size;
148                 struct mutex lock;
149         } purgeable;
150
151         uint64_t dma_fence_context;
152
153         /* Sequence number for the last job queued in bin_job_list.
154          * Starts at 0 (no jobs emitted).
155          */
156         uint64_t emit_seqno;
157
158         /* Sequence number for the last completed job on the GPU.
159          * Starts at 0 (no jobs completed).
160          */
161         uint64_t finished_seqno;
162
163         /* List of all struct vc4_exec_info for jobs to be executed in
164          * the binner.  The first job in the list is the one currently
165          * programmed into ct0ca for execution.
166          */
167         struct list_head bin_job_list;
168
169         /* List of all struct vc4_exec_info for jobs that have
170          * completed binning and are ready for rendering.  The first
171          * job in the list is the one currently programmed into ct1ca
172          * for execution.
173          */
174         struct list_head render_job_list;
175
176         /* List of the finished vc4_exec_infos waiting to be freed by
177          * job_done_work.
178          */
179         struct list_head job_done_list;
180         /* Spinlock used to synchronize the job_list and seqno
181          * accesses between the IRQ handler and GEM ioctls.
182          */
183         spinlock_t job_lock;
184         wait_queue_head_t job_wait_queue;
185         struct work_struct job_done_work;
186
187         /* Used to track the active perfmon if any. Access to this field is
188          * protected by job_lock.
189          */
190         struct vc4_perfmon *active_perfmon;
191
192         /* List of struct vc4_seqno_cb for callbacks to be made from a
193          * workqueue when the given seqno is passed.
194          */
195         struct list_head seqno_cb_list;
196
197         /* The memory used for storing binner tile alloc, tile state,
198          * and overflow memory allocations.  This is freed when V3D
199          * powers down.
200          */
201         struct vc4_bo *bin_bo;
202
203         /* Size of blocks allocated within bin_bo. */
204         uint32_t bin_alloc_size;
205
206         /* Bitmask of the bin_alloc_size chunks in bin_bo that are
207          * used.
208          */
209         uint32_t bin_alloc_used;
210
211         /* Bitmask of the current bin_alloc used for overflow memory. */
212         uint32_t bin_alloc_overflow;
213
214         /* Incremented when an underrun error happened after an atomic commit.
215          * This is particularly useful to detect when a specific modeset is too
216          * demanding in term of memory or HVS bandwidth which is hard to guess
217          * at atomic check time.
218          */
219         atomic_t underrun;
220
221         struct work_struct overflow_mem_work;
222
223         int power_refcount;
224
225         /* Set to true when the load tracker is active. */
226         bool load_tracker_enabled;
227
228         /* Mutex controlling the power refcount. */
229         struct mutex power_lock;
230
231         struct {
232                 struct timer_list timer;
233                 struct work_struct reset_work;
234         } hangcheck;
235
236         struct drm_modeset_lock ctm_state_lock;
237         struct drm_private_obj ctm_manager;
238         struct drm_private_obj hvs_channels;
239         struct drm_private_obj load_tracker;
240
241         /* Mutex for binner bo allocation. */
242         struct mutex bin_bo_lock;
243         /* Reference count for our binner bo. */
244         struct kref bin_bo_kref;
245 };
246
247 #define to_vc4_dev(_dev)                        \
248         container_of_const(_dev, struct vc4_dev, base)
249
250 struct vc4_bo {
251         struct drm_gem_dma_object base;
252
253         /* seqno of the last job to render using this BO. */
254         uint64_t seqno;
255
256         /* seqno of the last job to use the RCL to write to this BO.
257          *
258          * Note that this doesn't include binner overflow memory
259          * writes.
260          */
261         uint64_t write_seqno;
262
263         bool t_format;
264
265         /* List entry for the BO's position in either
266          * vc4_exec_info->unref_list or vc4_dev->bo_cache.time_list
267          */
268         struct list_head unref_head;
269
270         /* Time in jiffies when the BO was put in vc4->bo_cache. */
271         unsigned long free_time;
272
273         /* List entry for the BO's position in vc4_dev->bo_cache.size_list */
274         struct list_head size_head;
275
276         /* Struct for shader validation state, if created by
277          * DRM_IOCTL_VC4_CREATE_SHADER_BO.
278          */
279         struct vc4_validated_shader_info *validated_shader;
280
281         /* One of enum vc4_kernel_bo_type, or VC4_BO_TYPE_COUNT + i
282          * for user-allocated labels.
283          */
284         int label;
285
286         /* Count the number of active users. This is needed to determine
287          * whether we can move the BO to the purgeable list or not (when the BO
288          * is used by the GPU or the display engine we can't purge it).
289          */
290         refcount_t usecnt;
291
292         /* Store purgeable/purged state here */
293         u32 madv;
294         struct mutex madv_lock;
295 };
296
297 #define to_vc4_bo(_bo)                                                  \
298         container_of_const(to_drm_gem_dma_obj(_bo), struct vc4_bo, base)
299
300 struct vc4_fence {
301         struct dma_fence base;
302         struct drm_device *dev;
303         /* vc4 seqno for signaled() test */
304         uint64_t seqno;
305 };
306
307 #define to_vc4_fence(_fence)                                    \
308         container_of_const(_fence, struct vc4_fence, base)
309
310 struct vc4_seqno_cb {
311         struct work_struct work;
312         uint64_t seqno;
313         void (*func)(struct vc4_seqno_cb *cb);
314 };
315
316 struct vc4_v3d {
317         struct vc4_dev *vc4;
318         struct platform_device *pdev;
319         void __iomem *regs;
320         struct clk *clk;
321         struct debugfs_regset32 regset;
322 };
323
324 #define HVS_NUM_CHANNELS 3
325
326 struct vc4_hvs {
327         struct vc4_dev *vc4;
328         struct platform_device *pdev;
329         void __iomem *regs;
330         u32 __iomem *dlist;
331         unsigned int dlist_mem_size;
332
333         struct clk *core_clk;
334         struct clk *disp_clk;
335
336         struct {
337                 unsigned int desc;
338                 unsigned int enabled: 1;
339         } eof_irq[HVS_NUM_CHANNELS];
340
341         unsigned long max_core_rate;
342
343         /* Memory manager for CRTCs to allocate space in the display
344          * list.  Units are dwords.
345          */
346         struct drm_mm dlist_mm;
347         /* Memory manager for the LBM memory used by HVS scaling. */
348         struct drm_mm lbm_mm;
349
350         /* Memory manager for the UPM memory used for prefetching. */
351         struct drm_mm upm_mm;
352         struct ida upm_handles;
353
354         spinlock_t mm_lock;
355
356         struct list_head stale_dlist_entries;
357         struct work_struct free_dlist_work;
358
359         struct drm_mm_node mitchell_netravali_filter;
360
361         struct debugfs_regset32 regset;
362
363         /*
364          * Even if HDMI0 on the RPi4 can output modes requiring a pixel
365          * rate higher than 297MHz, it needs some adjustments in the
366          * config.txt file to be able to do so and thus won't always be
367          * available.
368          */
369         bool vc5_hdmi_enable_hdmi_20;
370
371         /*
372          * 4096x2160@60 requires a core overclock to work, so register
373          * whether that is sufficient.
374          */
375         bool vc5_hdmi_enable_4096by2160;
376 };
377
378 #define HVS_UBM_WORD_SIZE 256
379
380 struct vc4_hvs_state {
381         struct drm_private_state base;
382         unsigned long core_clock_rate;
383
384         struct {
385                 unsigned in_use: 1;
386                 unsigned long fifo_load;
387                 struct drm_crtc_commit *pending_commit;
388         } fifo_state[HVS_NUM_CHANNELS];
389 };
390
391 #define to_vc4_hvs_state(_state)                                \
392         container_of_const(_state, struct vc4_hvs_state, base)
393
394 struct vc4_hvs_state *vc4_hvs_get_global_state(struct drm_atomic_state *state);
395 struct vc4_hvs_state *vc4_hvs_get_old_global_state(const struct drm_atomic_state *state);
396 struct vc4_hvs_state *vc4_hvs_get_new_global_state(const struct drm_atomic_state *state);
397
398 struct vc4_plane {
399         struct drm_plane base;
400 };
401
402 #define to_vc4_plane(_plane)                                    \
403         container_of_const(_plane, struct vc4_plane, base)
404
405 enum vc4_scaling_mode {
406         VC4_SCALING_NONE,
407         VC4_SCALING_TPZ,
408         VC4_SCALING_PPF,
409 };
410
411 struct vc4_plane_state {
412         struct drm_plane_state base;
413         /* System memory copy of the display list for this element, computed
414          * at atomic_check time.
415          */
416         u32 *dlist;
417         u32 dlist_size; /* Number of dwords allocated for the display list */
418         u32 dlist_count; /* Number of used dwords in the display list. */
419
420         u32 lbm_size; /* LBM requirements for this plane */
421
422         /* Offset in the dlist to various words, for pageflip or
423          * cursor updates.
424          */
425         u32 pos0_offset;
426         u32 pos2_offset;
427         u32 ptr0_offset[DRM_FORMAT_MAX_PLANES];
428         u32 lbm_offset;
429
430         /* Offset where the plane's dlist was last stored in the
431          * hardware at vc4_crtc_atomic_flush() time.
432          */
433         u32 __iomem *hw_dlist;
434
435         /* Clipped coordinates of the plane on the display. */
436         int crtc_x, crtc_y, crtc_w, crtc_h;
437         /* Clipped area being scanned from in the FB in u16.16 format */
438         u32 src_x, src_y;
439
440         u32 src_w[2], src_h[2];
441
442         /* Scaling selection for the RGB/Y plane and the Cb/Cr planes. */
443         enum vc4_scaling_mode x_scaling[2], y_scaling[2];
444         bool is_unity;
445         bool is_yuv;
446
447         /* Our allocation in UPM for prefetching. */
448         struct drm_mm_node upm[DRM_FORMAT_MAX_PLANES];
449
450         /* The Unified Pre-Fetcher Handle */
451         unsigned int upm_handle[DRM_FORMAT_MAX_PLANES];
452
453         /* Number of lines to pre-fetch */
454         unsigned int upm_buffer_lines;
455
456         /* Set when the plane has per-pixel alpha content or does not cover
457          * the entire screen. This is a hint to the CRTC that it might need
458          * to enable background color fill.
459          */
460         bool needs_bg_fill;
461
462         /* Mark the dlist as initialized. Useful to avoid initializing it twice
463          * when async update is not possible.
464          */
465         bool dlist_initialized;
466
467         /* Load of this plane on the HVS block. The load is expressed in HVS
468          * cycles/sec.
469          */
470         u64 hvs_load;
471
472         /* Memory bandwidth needed for this plane. This is expressed in
473          * bytes/sec.
474          */
475         u64 membus_load;
476 };
477
478 #define to_vc4_plane_state(_state)                              \
479         container_of_const(_state, struct vc4_plane_state, base)
480
481 enum vc4_encoder_type {
482         VC4_ENCODER_TYPE_NONE,
483         VC4_ENCODER_TYPE_HDMI0,
484         VC4_ENCODER_TYPE_HDMI1,
485         VC4_ENCODER_TYPE_VEC,
486         VC4_ENCODER_TYPE_DSI0,
487         VC4_ENCODER_TYPE_DSI1,
488         VC4_ENCODER_TYPE_SMI,
489         VC4_ENCODER_TYPE_DPI,
490         VC4_ENCODER_TYPE_TXP0,
491         VC4_ENCODER_TYPE_TXP1,
492 };
493
494 struct vc4_encoder {
495         struct drm_encoder base;
496         enum vc4_encoder_type type;
497         u32 clock_select;
498
499         void (*pre_crtc_configure)(struct drm_encoder *encoder, struct drm_atomic_state *state);
500         void (*pre_crtc_enable)(struct drm_encoder *encoder, struct drm_atomic_state *state);
501         void (*post_crtc_enable)(struct drm_encoder *encoder, struct drm_atomic_state *state);
502
503         void (*post_crtc_disable)(struct drm_encoder *encoder, struct drm_atomic_state *state);
504         void (*post_crtc_powerdown)(struct drm_encoder *encoder, struct drm_atomic_state *state);
505 };
506
507 #define to_vc4_encoder(_encoder)                                \
508         container_of_const(_encoder, struct vc4_encoder, base)
509
510 static inline
511 struct drm_encoder *vc4_find_encoder_by_type(struct drm_device *drm,
512                                              enum vc4_encoder_type type)
513 {
514         struct drm_encoder *encoder;
515
516         drm_for_each_encoder(encoder, drm) {
517                 struct vc4_encoder *vc4_encoder = to_vc4_encoder(encoder);
518
519                 if (vc4_encoder->type == type)
520                         return encoder;
521         }
522
523         return NULL;
524 }
525
526 struct vc5_gamma_entry {
527         u32 x_c_terms;
528         u32 grad_term;
529 };
530
531 #define VC5_HVS_SET_GAMMA_ENTRY(x, c, g) (struct vc5_gamma_entry){      \
532         .x_c_terms = VC4_SET_FIELD((x), SCALER5_DSPGAMMA_OFF_X) |       \
533                      VC4_SET_FIELD((c), SCALER5_DSPGAMMA_OFF_C),        \
534         .grad_term = (g)                                                \
535 }
536
537 struct vc4_crtc_data {
538         const char *name;
539
540         const char *debugfs_name;
541
542         /* Bitmask of channels (FIFOs) of the HVS that the output can source from */
543         unsigned int hvs_available_channels;
544
545         /* Which output of the HVS this pixelvalve sources from. */
546         int hvs_output;
547 };
548
549 struct vc4_txp_data {
550         struct vc4_crtc_data    base;
551         enum vc4_encoder_type encoder_type;
552         unsigned int has_byte_enable:1;
553         unsigned int size_minus_one:1;
554         unsigned int supports_40bit_addresses:1;
555 };
556
557 extern const struct vc4_txp_data bcm2712_mop_data;
558 extern const struct vc4_txp_data bcm2712_moplet_data;
559 extern const struct vc4_txp_data bcm2835_txp_data;
560
561 struct vc4_pv_data {
562         struct vc4_crtc_data    base;
563
564         /* Depth of the PixelValve FIFO in bytes */
565         unsigned int fifo_depth;
566
567         /* Number of pixels output per clock period */
568         u8 pixels_per_clock;
569
570         enum vc4_encoder_type encoder_types[4];
571 };
572
573 extern const struct vc4_pv_data bcm2835_pv0_data;
574 extern const struct vc4_pv_data bcm2835_pv1_data;
575 extern const struct vc4_pv_data bcm2835_pv2_data;
576 extern const struct vc4_pv_data bcm2711_pv0_data;
577 extern const struct vc4_pv_data bcm2711_pv1_data;
578 extern const struct vc4_pv_data bcm2711_pv2_data;
579 extern const struct vc4_pv_data bcm2711_pv3_data;
580 extern const struct vc4_pv_data bcm2711_pv4_data;
581 extern const struct vc4_pv_data bcm2712_pv0_data;
582 extern const struct vc4_pv_data bcm2712_pv1_data;
583
584 struct vc4_crtc {
585         struct drm_crtc base;
586         struct platform_device *pdev;
587         const struct vc4_crtc_data *data;
588         void __iomem *regs;
589
590         /* Timestamp at start of vblank irq - unaffected by lock delays. */
591         ktime_t t_vblank;
592
593         union {
594                 struct {  /* VC4 gamma LUT */
595                         u8 lut_r[256];
596                         u8 lut_g[256];
597                         u8 lut_b[256];
598                 };
599                 struct {  /* VC5 gamma PWL entries */
600                         struct vc5_gamma_entry pwl_r[SCALER5_DSPGAMMA_NUM_POINTS];
601                         struct vc5_gamma_entry pwl_g[SCALER5_DSPGAMMA_NUM_POINTS];
602                         struct vc5_gamma_entry pwl_b[SCALER5_DSPGAMMA_NUM_POINTS];
603                         struct vc5_gamma_entry pwl_a[SCALER5_DSPGAMMA_NUM_POINTS];
604                 };
605         };
606
607         struct drm_pending_vblank_event *event;
608
609         struct debugfs_regset32 regset;
610
611         /**
612          * @feeds_txp: True if the CRTC feeds our writeback controller.
613          */
614         bool feeds_txp;
615
616         /**
617          * @irq_lock: Spinlock protecting the resources shared between
618          * the atomic code and our vblank handler.
619          */
620         spinlock_t irq_lock;
621
622         /**
623          * @current_dlist: Start offset of the display list currently
624          * set in the HVS for that CRTC. Protected by @irq_lock, and
625          * copied in vc4_hvs_update_dlist() for the CRTC interrupt
626          * handler to have access to that value.
627          */
628         unsigned int current_dlist;
629
630         /**
631          * @current_hvs_channel: HVS channel currently assigned to the
632          * CRTC. Protected by @irq_lock, and copied in
633          * vc4_hvs_atomic_begin() for the CRTC interrupt handler to have
634          * access to that value.
635          */
636         unsigned int current_hvs_channel;
637
638         /* @lbm: Our allocation in LBM for temporary storage during scaling. */
639         struct drm_mm_node lbm;
640 };
641
642 #define to_vc4_crtc(_crtc)                                      \
643         container_of_const(_crtc, struct vc4_crtc, base)
644
645 static inline const struct vc4_crtc_data *
646 vc4_crtc_to_vc4_crtc_data(const struct vc4_crtc *crtc)
647 {
648         return crtc->data;
649 }
650
651 static inline const struct vc4_pv_data *
652 vc4_crtc_to_vc4_pv_data(const struct vc4_crtc *crtc)
653 {
654         const struct vc4_crtc_data *data = vc4_crtc_to_vc4_crtc_data(crtc);
655
656         return container_of_const(data, struct vc4_pv_data, base);
657 }
658
659 struct drm_connector *vc4_get_crtc_connector(struct drm_crtc *crtc,
660                                              struct drm_crtc_state *state);
661
662 struct drm_encoder *vc4_get_crtc_encoder(struct drm_crtc *crtc,
663                                          struct drm_crtc_state *state);
664
665 struct vc4_hvs_dlist_allocation {
666         struct list_head node;
667         struct drm_mm_node mm_node;
668         unsigned int channel;
669         u8 target_frame_count;
670 };
671
672 struct vc4_crtc_state {
673         struct drm_crtc_state base;
674         struct vc4_hvs_dlist_allocation *mm;
675         bool txp_armed;
676         unsigned int assigned_channel;
677
678         struct drm_connector_tv_margins margins;
679
680         unsigned long hvs_load;
681
682         /* Transitional state below, only valid during atomic commits */
683         bool update_muxing;
684 };
685
686 #define VC4_HVS_CHANNEL_DISABLED ((unsigned int)-1)
687
688 #define to_vc4_crtc_state(_state)                               \
689         container_of_const(_state, struct vc4_crtc_state, base)
690
691 #define V3D_READ(offset)                                                                \
692         ({                                                                              \
693                 kunit_fail_current_test("Accessing a register in a unit test!\n");      \
694                 readl(vc4->v3d->regs + (offset));                                               \
695         })
696
697 #define V3D_WRITE(offset, val)                                                          \
698         do {                                                                            \
699                 kunit_fail_current_test("Accessing a register in a unit test!\n");      \
700                 writel(val, vc4->v3d->regs + (offset));                                 \
701         } while (0)
702
703 #define HVS_READ(offset)                                                                \
704         ({                                                                              \
705                 kunit_fail_current_test("Accessing a register in a unit test!\n");      \
706                 readl(hvs->regs + (offset));                                            \
707         })
708
709 #define HVS_WRITE(offset, val)                                                          \
710         do {                                                                            \
711                 kunit_fail_current_test("Accessing a register in a unit test!\n");      \
712                 writel(val, hvs->regs + (offset));                                      \
713         } while (0)
714
715 #define VC4_REG32(reg) { .name = #reg, .offset = reg }
716
717 struct vc4_exec_info {
718         struct vc4_dev *dev;
719
720         /* Sequence number for this bin/render job. */
721         uint64_t seqno;
722
723         /* Latest write_seqno of any BO that binning depends on. */
724         uint64_t bin_dep_seqno;
725
726         struct dma_fence *fence;
727
728         /* Last current addresses the hardware was processing when the
729          * hangcheck timer checked on us.
730          */
731         uint32_t last_ct0ca, last_ct1ca;
732
733         /* Kernel-space copy of the ioctl arguments */
734         struct drm_vc4_submit_cl *args;
735
736         /* This is the array of BOs that were looked up at the start of exec.
737          * Command validation will use indices into this array.
738          */
739         struct drm_gem_object **bo;
740         uint32_t bo_count;
741
742         /* List of BOs that are being written by the RCL.  Other than
743          * the binner temporary storage, this is all the BOs written
744          * by the job.
745          */
746         struct drm_gem_dma_object *rcl_write_bo[4];
747         uint32_t rcl_write_bo_count;
748
749         /* Pointers for our position in vc4->job_list */
750         struct list_head head;
751
752         /* List of other BOs used in the job that need to be released
753          * once the job is complete.
754          */
755         struct list_head unref_list;
756
757         /* Current unvalidated indices into @bo loaded by the non-hardware
758          * VC4_PACKET_GEM_HANDLES.
759          */
760         uint32_t bo_index[2];
761
762         /* This is the BO where we store the validated command lists, shader
763          * records, and uniforms.
764          */
765         struct drm_gem_dma_object *exec_bo;
766
767         /**
768          * This tracks the per-shader-record state (packet 64) that
769          * determines the length of the shader record and the offset
770          * it's expected to be found at.  It gets read in from the
771          * command lists.
772          */
773         struct vc4_shader_state {
774                 uint32_t addr;
775                 /* Maximum vertex index referenced by any primitive using this
776                  * shader state.
777                  */
778                 uint32_t max_index;
779         } *shader_state;
780
781         /** How many shader states the user declared they were using. */
782         uint32_t shader_state_size;
783         /** How many shader state records the validator has seen. */
784         uint32_t shader_state_count;
785
786         bool found_tile_binning_mode_config_packet;
787         bool found_start_tile_binning_packet;
788         bool found_increment_semaphore_packet;
789         bool found_flush;
790         uint8_t bin_tiles_x, bin_tiles_y;
791         /* Physical address of the start of the tile alloc array
792          * (where each tile's binned CL will start)
793          */
794         uint32_t tile_alloc_offset;
795         /* Bitmask of which binner slots are freed when this job completes. */
796         uint32_t bin_slots;
797
798         /**
799          * Computed addresses pointing into exec_bo where we start the
800          * bin thread (ct0) and render thread (ct1).
801          */
802         uint32_t ct0ca, ct0ea;
803         uint32_t ct1ca, ct1ea;
804
805         /* Pointer to the unvalidated bin CL (if present). */
806         void *bin_u;
807
808         /* Pointers to the shader recs.  These paddr gets incremented as CL
809          * packets are relocated in validate_gl_shader_state, and the vaddrs
810          * (u and v) get incremented and size decremented as the shader recs
811          * themselves are validated.
812          */
813         void *shader_rec_u;
814         void *shader_rec_v;
815         uint32_t shader_rec_p;
816         uint32_t shader_rec_size;
817
818         /* Pointers to the uniform data.  These pointers are incremented, and
819          * size decremented, as each batch of uniforms is uploaded.
820          */
821         void *uniforms_u;
822         void *uniforms_v;
823         uint32_t uniforms_p;
824         uint32_t uniforms_size;
825
826         /* Pointer to a performance monitor object if the user requested it,
827          * NULL otherwise.
828          */
829         struct vc4_perfmon *perfmon;
830
831         /* Whether the exec has taken a reference to the binner BO, which should
832          * happen with a VC4_PACKET_TILE_BINNING_MODE_CONFIG packet.
833          */
834         bool bin_bo_used;
835 };
836
837 /* Per-open file private data. Any driver-specific resource that has to be
838  * released when the DRM file is closed should be placed here.
839  */
840 struct vc4_file {
841         struct vc4_dev *dev;
842
843         struct {
844                 struct idr idr;
845                 struct mutex lock;
846         } perfmon;
847
848         bool bin_bo_used;
849 };
850
851 static inline struct vc4_exec_info *
852 vc4_first_bin_job(struct vc4_dev *vc4)
853 {
854         return list_first_entry_or_null(&vc4->bin_job_list,
855                                         struct vc4_exec_info, head);
856 }
857
858 static inline struct vc4_exec_info *
859 vc4_first_render_job(struct vc4_dev *vc4)
860 {
861         return list_first_entry_or_null(&vc4->render_job_list,
862                                         struct vc4_exec_info, head);
863 }
864
865 static inline struct vc4_exec_info *
866 vc4_last_render_job(struct vc4_dev *vc4)
867 {
868         if (list_empty(&vc4->render_job_list))
869                 return NULL;
870         return list_last_entry(&vc4->render_job_list,
871                                struct vc4_exec_info, head);
872 }
873
874 /**
875  * struct vc4_texture_sample_info - saves the offsets into the UBO for texture
876  * setup parameters.
877  *
878  * This will be used at draw time to relocate the reference to the texture
879  * contents in p0, and validate that the offset combined with
880  * width/height/stride/etc. from p1 and p2/p3 doesn't sample outside the BO.
881  * Note that the hardware treats unprovided config parameters as 0, so not all
882  * of them need to be set up for every texure sample, and we'll store ~0 as
883  * the offset to mark the unused ones.
884  *
885  * See the VC4 3D architecture guide page 41 ("Texture and Memory Lookup Unit
886  * Setup") for definitions of the texture parameters.
887  */
888 struct vc4_texture_sample_info {
889         bool is_direct;
890         uint32_t p_offset[4];
891 };
892
893 /**
894  * struct vc4_validated_shader_info - information about validated shaders that
895  * needs to be used from command list validation.
896  *
897  * For a given shader, each time a shader state record references it, we need
898  * to verify that the shader doesn't read more uniforms than the shader state
899  * record's uniform BO pointer can provide, and we need to apply relocations
900  * and validate the shader state record's uniforms that define the texture
901  * samples.
902  */
903 struct vc4_validated_shader_info {
904         uint32_t uniforms_size;
905         uint32_t uniforms_src_size;
906         uint32_t num_texture_samples;
907         struct vc4_texture_sample_info *texture_samples;
908
909         uint32_t num_uniform_addr_offsets;
910         uint32_t *uniform_addr_offsets;
911
912         bool is_threaded;
913 };
914
915 /**
916  * __wait_for - magic wait macro
917  *
918  * Macro to help avoid open coding check/wait/timeout patterns. Note that it's
919  * important that we check the condition again after having timed out, since the
920  * timeout could be due to preemption or similar and we've never had a chance to
921  * check the condition before the timeout.
922  */
923 #define __wait_for(OP, COND, US, Wmin, Wmax) ({ \
924         const ktime_t end__ = ktime_add_ns(ktime_get_raw(), 1000ll * (US)); \
925         long wait__ = (Wmin); /* recommended min for usleep is 10 us */ \
926         int ret__;                                                      \
927         might_sleep();                                                  \
928         for (;;) {                                                      \
929                 const bool expired__ = ktime_after(ktime_get_raw(), end__); \
930                 OP;                                                     \
931                 /* Guarantee COND check prior to timeout */             \
932                 barrier();                                              \
933                 if (COND) {                                             \
934                         ret__ = 0;                                      \
935                         break;                                          \
936                 }                                                       \
937                 if (expired__) {                                        \
938                         ret__ = -ETIMEDOUT;                             \
939                         break;                                          \
940                 }                                                       \
941                 usleep_range(wait__, wait__ * 2);                       \
942                 if (wait__ < (Wmax))                                    \
943                         wait__ <<= 1;                                   \
944         }                                                               \
945         ret__;                                                          \
946 })
947
948 #define _wait_for(COND, US, Wmin, Wmax) __wait_for(, (COND), (US), (Wmin), \
949                                                    (Wmax))
950 #define wait_for(COND, MS)              _wait_for((COND), (MS) * 1000, 10, 1000)
951
952 /* vc4_bo.c */
953 struct drm_gem_object *vc4_create_object(struct drm_device *dev, size_t size);
954 struct vc4_bo *vc4_bo_create(struct drm_device *dev, size_t size,
955                              bool from_cache, enum vc4_kernel_bo_type type);
956 int vc4_bo_dumb_create(struct drm_file *file_priv,
957                        struct drm_device *dev,
958                        struct drm_mode_create_dumb *args);
959 int vc4_create_bo_ioctl(struct drm_device *dev, void *data,
960                         struct drm_file *file_priv);
961 int vc4_create_shader_bo_ioctl(struct drm_device *dev, void *data,
962                                struct drm_file *file_priv);
963 int vc4_mmap_bo_ioctl(struct drm_device *dev, void *data,
964                       struct drm_file *file_priv);
965 int vc4_set_tiling_ioctl(struct drm_device *dev, void *data,
966                          struct drm_file *file_priv);
967 int vc4_get_tiling_ioctl(struct drm_device *dev, void *data,
968                          struct drm_file *file_priv);
969 int vc4_get_hang_state_ioctl(struct drm_device *dev, void *data,
970                              struct drm_file *file_priv);
971 int vc4_label_bo_ioctl(struct drm_device *dev, void *data,
972                        struct drm_file *file_priv);
973 int vc4_bo_cache_init(struct drm_device *dev);
974 int vc4_bo_inc_usecnt(struct vc4_bo *bo);
975 void vc4_bo_dec_usecnt(struct vc4_bo *bo);
976 void vc4_bo_add_to_purgeable_pool(struct vc4_bo *bo);
977 void vc4_bo_remove_from_purgeable_pool(struct vc4_bo *bo);
978 int vc4_bo_debugfs_init(struct drm_minor *minor);
979
980 /* vc4_crtc.c */
981 extern struct platform_driver vc4_crtc_driver;
982 int vc4_crtc_disable_at_boot(struct drm_crtc *crtc);
983 int __vc4_crtc_init(struct drm_device *drm, struct platform_device *pdev,
984                     struct vc4_crtc *vc4_crtc, const struct vc4_crtc_data *data,
985                     struct drm_plane *primary_plane,
986                     const struct drm_crtc_funcs *crtc_funcs,
987                     const struct drm_crtc_helper_funcs *crtc_helper_funcs,
988                     bool feeds_txp);
989 int vc4_crtc_init(struct drm_device *drm, struct platform_device *pdev,
990                   struct vc4_crtc *vc4_crtc, const struct vc4_crtc_data *data,
991                   const struct drm_crtc_funcs *crtc_funcs,
992                   const struct drm_crtc_helper_funcs *crtc_helper_funcs,
993                   bool feeds_txp);
994 int vc4_page_flip(struct drm_crtc *crtc,
995                   struct drm_framebuffer *fb,
996                   struct drm_pending_vblank_event *event,
997                   uint32_t flags,
998                   struct drm_modeset_acquire_ctx *ctx);
999 int vc4_crtc_atomic_check(struct drm_crtc *crtc,
1000                           struct drm_atomic_state *state);
1001 struct drm_crtc_state *vc4_crtc_duplicate_state(struct drm_crtc *crtc);
1002 void vc4_crtc_destroy_state(struct drm_crtc *crtc,
1003                             struct drm_crtc_state *state);
1004 void vc4_crtc_reset(struct drm_crtc *crtc);
1005 void vc4_crtc_handle_vblank(struct vc4_crtc *crtc);
1006 void vc4_crtc_send_vblank(struct drm_crtc *crtc);
1007 int vc4_crtc_late_register(struct drm_crtc *crtc);
1008 void vc4_crtc_get_margins(struct drm_crtc_state *state,
1009                           unsigned int *left, unsigned int *right,
1010                           unsigned int *top, unsigned int *bottom);
1011
1012 /* vc4_debugfs.c */
1013 void vc4_debugfs_init(struct drm_minor *minor);
1014 #ifdef CONFIG_DEBUG_FS
1015 void vc4_debugfs_add_regset32(struct drm_device *drm,
1016                               const char *filename,
1017                               struct debugfs_regset32 *regset);
1018 #else
1019
1020 static inline void vc4_debugfs_add_regset32(struct drm_device *drm,
1021                                             const char *filename,
1022                                             struct debugfs_regset32 *regset)
1023 {}
1024 #endif
1025
1026 /* vc4_drv.c */
1027 void __iomem *vc4_ioremap_regs(struct platform_device *dev, int index);
1028 int vc4_dumb_fixup_args(struct drm_mode_create_dumb *args);
1029
1030 /* vc4_dpi.c */
1031 extern struct platform_driver vc4_dpi_driver;
1032
1033 /* vc4_dsi.c */
1034 extern struct platform_driver vc4_dsi_driver;
1035
1036 /* vc4_fence.c */
1037 extern const struct dma_fence_ops vc4_fence_ops;
1038
1039 /* vc4_firmware_kms.c */
1040 extern struct platform_driver vc4_firmware_kms_driver;
1041
1042 /* vc4_gem.c */
1043 int vc4_gem_init(struct drm_device *dev);
1044 int vc4_submit_cl_ioctl(struct drm_device *dev, void *data,
1045                         struct drm_file *file_priv);
1046 int vc4_wait_seqno_ioctl(struct drm_device *dev, void *data,
1047                          struct drm_file *file_priv);
1048 int vc4_wait_bo_ioctl(struct drm_device *dev, void *data,
1049                       struct drm_file *file_priv);
1050 void vc4_submit_next_bin_job(struct drm_device *dev);
1051 void vc4_submit_next_render_job(struct drm_device *dev);
1052 void vc4_move_job_to_render(struct drm_device *dev, struct vc4_exec_info *exec);
1053 int vc4_wait_for_seqno(struct drm_device *dev, uint64_t seqno,
1054                        uint64_t timeout_ns, bool interruptible);
1055 void vc4_job_handle_completed(struct vc4_dev *vc4);
1056 int vc4_queue_seqno_cb(struct drm_device *dev,
1057                        struct vc4_seqno_cb *cb, uint64_t seqno,
1058                        void (*func)(struct vc4_seqno_cb *cb));
1059 int vc4_gem_madvise_ioctl(struct drm_device *dev, void *data,
1060                           struct drm_file *file_priv);
1061
1062 /* vc4_hdmi.c */
1063 extern struct platform_driver vc4_hdmi_driver;
1064
1065 /* vc4_vec.c */
1066 extern struct platform_driver vc4_vec_driver;
1067
1068 /* vc4_txp.c */
1069 extern struct platform_driver vc4_txp_driver;
1070
1071 /* vc4_irq.c */
1072 void vc4_irq_enable(struct drm_device *dev);
1073 void vc4_irq_disable(struct drm_device *dev);
1074 int vc4_irq_install(struct drm_device *dev, int irq);
1075 void vc4_irq_uninstall(struct drm_device *dev);
1076 void vc4_irq_reset(struct drm_device *dev);
1077
1078 /* vc4_hvs.c */
1079 extern struct platform_driver vc4_hvs_driver;
1080 struct vc4_hvs *__vc4_hvs_alloc(struct vc4_dev *vc4,
1081                                 void __iomem *regs,
1082                                 struct platform_device *pdev);
1083 void vc4_hvs_stop_channel(struct vc4_hvs *hvs, unsigned int output);
1084 int vc4_hvs_get_fifo_from_output(struct vc4_hvs *hvs, unsigned int output);
1085 u8 vc4_hvs_get_fifo_frame_count(struct vc4_hvs *hvs, unsigned int fifo);
1086 void vc4_hvs_mark_dlist_entry_stale(struct vc4_hvs *hvs,
1087                                     struct vc4_hvs_dlist_allocation *alloc);
1088 int vc4_hvs_atomic_check(struct drm_crtc *crtc, struct drm_atomic_state *state);
1089 void vc4_hvs_atomic_begin(struct drm_crtc *crtc, struct drm_atomic_state *state);
1090 void vc4_hvs_atomic_enable(struct drm_crtc *crtc, struct drm_atomic_state *state);
1091 void vc4_hvs_atomic_disable(struct drm_crtc *crtc, struct drm_atomic_state *state);
1092 void vc4_hvs_atomic_flush(struct drm_crtc *crtc, struct drm_atomic_state *state);
1093 void vc4_hvs_dump_state(struct vc4_hvs *hvs);
1094 void vc4_hvs_unmask_underrun(struct vc4_hvs *hvs, int channel);
1095 void vc4_hvs_mask_underrun(struct vc4_hvs *hvs, int channel);
1096 int vc4_hvs_debugfs_init(struct drm_minor *minor);
1097
1098 /* vc4_kms.c */
1099 int vc4_kms_load(struct drm_device *dev);
1100
1101 /* vc4_plane.c */
1102 struct drm_plane *vc4_plane_init(struct drm_device *dev,
1103                                  enum drm_plane_type type,
1104                                  uint32_t possible_crtcs);
1105 void vc4_plane_reset(struct drm_plane *plane);
1106 void vc4_plane_destroy_state(struct drm_plane *plane,
1107                              struct drm_plane_state *state);
1108 struct drm_plane_state *vc4_plane_duplicate_state(struct drm_plane *plane);
1109 int vc4_plane_atomic_check(struct drm_plane *plane,
1110                            struct drm_atomic_state *state);
1111 int vc4_plane_create_additional_planes(struct drm_device *dev);
1112 u32 vc4_plane_write_dlist(struct drm_plane *plane, u32 __iomem *dlist);
1113 u32 vc4_plane_dlist_size(const struct drm_plane_state *state);
1114 void vc4_plane_async_set_fb(struct drm_plane *plane,
1115                             struct drm_framebuffer *fb);
1116
1117 /* vc4_v3d.c */
1118 extern struct platform_driver vc4_v3d_driver;
1119 extern const struct of_device_id vc4_v3d_dt_match[];
1120 int vc4_v3d_get_bin_slot(struct vc4_dev *vc4);
1121 int vc4_v3d_bin_bo_get(struct vc4_dev *vc4, bool *used);
1122 void vc4_v3d_bin_bo_put(struct vc4_dev *vc4);
1123 int vc4_v3d_pm_get(struct vc4_dev *vc4);
1124 void vc4_v3d_pm_put(struct vc4_dev *vc4);
1125 int vc4_v3d_debugfs_init(struct drm_minor *minor);
1126
1127 /* vc4_validate.c */
1128 int
1129 vc4_validate_bin_cl(struct drm_device *dev,
1130                     void *validated,
1131                     void *unvalidated,
1132                     struct vc4_exec_info *exec);
1133
1134 int
1135 vc4_validate_shader_recs(struct drm_device *dev, struct vc4_exec_info *exec);
1136
1137 struct drm_gem_dma_object *vc4_use_bo(struct vc4_exec_info *exec,
1138                                       uint32_t hindex);
1139
1140 int vc4_get_rcl(struct drm_device *dev, struct vc4_exec_info *exec);
1141
1142 bool vc4_check_tex_size(struct vc4_exec_info *exec,
1143                         struct drm_gem_dma_object *fbo,
1144                         uint32_t offset, uint8_t tiling_format,
1145                         uint32_t width, uint32_t height, uint8_t cpp);
1146
1147 /* vc4_validate_shader.c */
1148 struct vc4_validated_shader_info *
1149 vc4_validate_shader(struct drm_gem_dma_object *shader_obj);
1150
1151 /* vc4_perfmon.c */
1152 void vc4_perfmon_get(struct vc4_perfmon *perfmon);
1153 void vc4_perfmon_put(struct vc4_perfmon *perfmon);
1154 void vc4_perfmon_start(struct vc4_dev *vc4, struct vc4_perfmon *perfmon);
1155 void vc4_perfmon_stop(struct vc4_dev *vc4, struct vc4_perfmon *perfmon,
1156                       bool capture);
1157 struct vc4_perfmon *vc4_perfmon_find(struct vc4_file *vc4file, int id);
1158 void vc4_perfmon_open_file(struct vc4_file *vc4file);
1159 void vc4_perfmon_close_file(struct vc4_file *vc4file);
1160 int vc4_perfmon_create_ioctl(struct drm_device *dev, void *data,
1161                              struct drm_file *file_priv);
1162 int vc4_perfmon_destroy_ioctl(struct drm_device *dev, void *data,
1163                               struct drm_file *file_priv);
1164 int vc4_perfmon_get_values_ioctl(struct drm_device *dev, void *data,
1165                                  struct drm_file *file_priv);
1166
1167 #endif /* _VC4_DRV_H_ */