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