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