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