xref: /openbmc/linux/drivers/gpu/drm/vc4/vc4_drv.h (revision 2a001ca0)
1d2912cb1SThomas Gleixner /* SPDX-License-Identifier: GPL-2.0-only */
2c8b75bcaSEric Anholt /*
3c8b75bcaSEric Anholt  * Copyright (C) 2015 Broadcom
4c8b75bcaSEric Anholt  */
56a88752cSMaxime Ripard #ifndef _VC4_DRV_H_
66a88752cSMaxime Ripard #define _VC4_DRV_H_
7c8b75bcaSEric Anholt 
8fd6d6d80SSam Ravnborg #include <linux/delay.h>
973289afeSVille Syrjälä #include <linux/of.h>
10fd6d6d80SSam Ravnborg #include <linux/refcount.h>
11fd6d6d80SSam Ravnborg #include <linux/uaccess.h>
12fd6d6d80SSam Ravnborg 
13fd6d6d80SSam Ravnborg #include <drm/drm_atomic.h>
14fd6d6d80SSam Ravnborg #include <drm/drm_debugfs.h>
15fd6d6d80SSam Ravnborg #include <drm/drm_device.h>
169338203cSLaurent Pinchart #include <drm/drm_encoder.h>
174a83c26aSDanilo Krummrich #include <drm/drm_gem_dma_helper.h>
181c80be48SMaxime Ripard #include <drm/drm_managed.h>
19fd6d6d80SSam Ravnborg #include <drm/drm_mm.h>
20fd6d6d80SSam Ravnborg #include <drm/drm_modeset_lock.h>
219338203cSLaurent Pinchart 
2265101d8cSBoris Brezillon #include "uapi/drm/vc4_drm.h"
2365101d8cSBoris Brezillon 
24fd6d6d80SSam Ravnborg struct drm_device;
25fd6d6d80SSam Ravnborg struct drm_gem_object;
26fd6d6d80SSam Ravnborg 
27f3099462SEric Anholt /* Don't forget to update vc4_bo.c: bo_type_names[] when adding to
28f3099462SEric Anholt  * this.
29f3099462SEric Anholt  */
30f3099462SEric Anholt enum vc4_kernel_bo_type {
31f3099462SEric Anholt 	/* Any kernel allocation (gem_create_object hook) before it
32f3099462SEric Anholt 	 * gets another type set.
33f3099462SEric Anholt 	 */
34f3099462SEric Anholt 	VC4_BO_TYPE_KERNEL,
35f3099462SEric Anholt 	VC4_BO_TYPE_V3D,
36f3099462SEric Anholt 	VC4_BO_TYPE_V3D_SHADER,
37f3099462SEric Anholt 	VC4_BO_TYPE_DUMB,
38f3099462SEric Anholt 	VC4_BO_TYPE_BIN,
39f3099462SEric Anholt 	VC4_BO_TYPE_RCL,
40f3099462SEric Anholt 	VC4_BO_TYPE_BCL,
41f3099462SEric Anholt 	VC4_BO_TYPE_KERNEL_CACHE,
42f3099462SEric Anholt 	VC4_BO_TYPE_COUNT
43f3099462SEric Anholt };
44f3099462SEric Anholt 
4565101d8cSBoris Brezillon /* Performance monitor object. The perform lifetime is controlled by userspace
4665101d8cSBoris Brezillon  * using perfmon related ioctls. A perfmon can be attached to a submit_cl
4765101d8cSBoris Brezillon  * request, and when this is the case, HW perf counters will be activated just
4865101d8cSBoris Brezillon  * before the submit_cl is submitted to the GPU and disabled when the job is
4965101d8cSBoris Brezillon  * done. This way, only events related to a specific job will be counted.
5065101d8cSBoris Brezillon  */
5165101d8cSBoris Brezillon struct vc4_perfmon {
5230f8c74cSMaxime Ripard 	struct vc4_dev *dev;
5330f8c74cSMaxime Ripard 
5465101d8cSBoris Brezillon 	/* Tracks the number of users of the perfmon, when this counter reaches
5565101d8cSBoris Brezillon 	 * zero the perfmon is destroyed.
5665101d8cSBoris Brezillon 	 */
5765101d8cSBoris Brezillon 	refcount_t refcnt;
5865101d8cSBoris Brezillon 
5965101d8cSBoris Brezillon 	/* Number of counters activated in this perfmon instance
6065101d8cSBoris Brezillon 	 * (should be less than DRM_VC4_MAX_PERF_COUNTERS).
6165101d8cSBoris Brezillon 	 */
6265101d8cSBoris Brezillon 	u8 ncounters;
6365101d8cSBoris Brezillon 
6465101d8cSBoris Brezillon 	/* Events counted by the HW perf counters. */
6565101d8cSBoris Brezillon 	u8 events[DRM_VC4_MAX_PERF_COUNTERS];
6665101d8cSBoris Brezillon 
6765101d8cSBoris Brezillon 	/* Storage for counter values. Counters are incremented by the HW
6865101d8cSBoris Brezillon 	 * perf counter values every time the perfmon is attached to a GPU job.
6965101d8cSBoris Brezillon 	 * This way, perfmon users don't have to retrieve the results after
7065101d8cSBoris Brezillon 	 * each job if they want to track events covering several submissions.
7165101d8cSBoris Brezillon 	 * Note that counter values can't be reset, but you can fake a reset by
7265101d8cSBoris Brezillon 	 * destroying the perfmon and creating a new one.
7365101d8cSBoris Brezillon 	 */
745b2adbddSGustavo A. R. Silva 	u64 counters[];
7565101d8cSBoris Brezillon };
7665101d8cSBoris Brezillon 
77c8b75bcaSEric Anholt struct vc4_dev {
7884d7d472SMaxime Ripard 	struct drm_device base;
796cf61bf4SMaxime Ripard 	struct device *dev;
80c8b75bcaSEric Anholt 
811cbc91ebSMaxime Ripard 	bool is_vc5;
821cbc91ebSMaxime Ripard 
835226711eSThomas Zimmermann 	unsigned int irq;
845226711eSThomas Zimmermann 
85c8b75bcaSEric Anholt 	struct vc4_hvs *hvs;
86d3f5168aSEric Anholt 	struct vc4_v3d *v3d;
8748666d56SDerek Foreman 
8821461365SEric Anholt 	struct vc4_hang_state *hang_state;
8921461365SEric Anholt 
90c826a6e1SEric Anholt 	/* The kernel-space BO cache.  Tracks buffers that have been
91c826a6e1SEric Anholt 	 * unreferenced by all other users (refcounts of 0!) but not
92c826a6e1SEric Anholt 	 * yet freed, so we can do cheap allocations.
93c826a6e1SEric Anholt 	 */
94c826a6e1SEric Anholt 	struct vc4_bo_cache {
95c826a6e1SEric Anholt 		/* Array of list heads for entries in the BO cache,
96c826a6e1SEric Anholt 		 * based on number of pages, so we can do O(1) lookups
97c826a6e1SEric Anholt 		 * in the cache when allocating.
98c826a6e1SEric Anholt 		 */
99c826a6e1SEric Anholt 		struct list_head *size_list;
100c826a6e1SEric Anholt 		uint32_t size_list_size;
101c826a6e1SEric Anholt 
102c826a6e1SEric Anholt 		/* List of all BOs in the cache, ordered by age, so we
103c826a6e1SEric Anholt 		 * can do O(1) lookups when trying to free old
104c826a6e1SEric Anholt 		 * buffers.
105c826a6e1SEric Anholt 		 */
106c826a6e1SEric Anholt 		struct list_head time_list;
107c826a6e1SEric Anholt 		struct work_struct time_work;
108c826a6e1SEric Anholt 		struct timer_list time_timer;
109c826a6e1SEric Anholt 	} bo_cache;
110c826a6e1SEric Anholt 
111f3099462SEric Anholt 	u32 num_labels;
112f3099462SEric Anholt 	struct vc4_label {
113f3099462SEric Anholt 		const char *name;
114c826a6e1SEric Anholt 		u32 num_allocated;
115c826a6e1SEric Anholt 		u32 size_allocated;
116f3099462SEric Anholt 	} *bo_labels;
117c826a6e1SEric Anholt 
118f3099462SEric Anholt 	/* Protects bo_cache and bo_labels. */
119c826a6e1SEric Anholt 	struct mutex bo_lock;
120d5b1a78aSEric Anholt 
121b9f19259SBoris Brezillon 	/* Purgeable BO pool. All BOs in this pool can have their memory
122b9f19259SBoris Brezillon 	 * reclaimed if the driver is unable to allocate new BOs. We also
123b9f19259SBoris Brezillon 	 * keep stats related to the purge mechanism here.
124b9f19259SBoris Brezillon 	 */
125b9f19259SBoris Brezillon 	struct {
126b9f19259SBoris Brezillon 		struct list_head list;
127b9f19259SBoris Brezillon 		unsigned int num;
128b9f19259SBoris Brezillon 		size_t size;
129b9f19259SBoris Brezillon 		unsigned int purged_num;
130b9f19259SBoris Brezillon 		size_t purged_size;
131b9f19259SBoris Brezillon 		struct mutex lock;
132b9f19259SBoris Brezillon 	} purgeable;
133b9f19259SBoris Brezillon 
134cdec4d36SEric Anholt 	uint64_t dma_fence_context;
135cdec4d36SEric Anholt 
136ca26d28bSVarad Gautam 	/* Sequence number for the last job queued in bin_job_list.
137d5b1a78aSEric Anholt 	 * Starts at 0 (no jobs emitted).
138d5b1a78aSEric Anholt 	 */
139d5b1a78aSEric Anholt 	uint64_t emit_seqno;
140d5b1a78aSEric Anholt 
141d5b1a78aSEric Anholt 	/* Sequence number for the last completed job on the GPU.
142d5b1a78aSEric Anholt 	 * Starts at 0 (no jobs completed).
143d5b1a78aSEric Anholt 	 */
144d5b1a78aSEric Anholt 	uint64_t finished_seqno;
145d5b1a78aSEric Anholt 
146ca26d28bSVarad Gautam 	/* List of all struct vc4_exec_info for jobs to be executed in
147ca26d28bSVarad Gautam 	 * the binner.  The first job in the list is the one currently
148ca26d28bSVarad Gautam 	 * programmed into ct0ca for execution.
149d5b1a78aSEric Anholt 	 */
150ca26d28bSVarad Gautam 	struct list_head bin_job_list;
151ca26d28bSVarad Gautam 
152ca26d28bSVarad Gautam 	/* List of all struct vc4_exec_info for jobs that have
153ca26d28bSVarad Gautam 	 * completed binning and are ready for rendering.  The first
154ca26d28bSVarad Gautam 	 * job in the list is the one currently programmed into ct1ca
155ca26d28bSVarad Gautam 	 * for execution.
156ca26d28bSVarad Gautam 	 */
157ca26d28bSVarad Gautam 	struct list_head render_job_list;
158ca26d28bSVarad Gautam 
159d5b1a78aSEric Anholt 	/* List of the finished vc4_exec_infos waiting to be freed by
160d5b1a78aSEric Anholt 	 * job_done_work.
161d5b1a78aSEric Anholt 	 */
162d5b1a78aSEric Anholt 	struct list_head job_done_list;
163d5b1a78aSEric Anholt 	/* Spinlock used to synchronize the job_list and seqno
164d5b1a78aSEric Anholt 	 * accesses between the IRQ handler and GEM ioctls.
165d5b1a78aSEric Anholt 	 */
166d5b1a78aSEric Anholt 	spinlock_t job_lock;
167d5b1a78aSEric Anholt 	wait_queue_head_t job_wait_queue;
168d5b1a78aSEric Anholt 	struct work_struct job_done_work;
169d5b1a78aSEric Anholt 
17065101d8cSBoris Brezillon 	/* Used to track the active perfmon if any. Access to this field is
17165101d8cSBoris Brezillon 	 * protected by job_lock.
17265101d8cSBoris Brezillon 	 */
17365101d8cSBoris Brezillon 	struct vc4_perfmon *active_perfmon;
17465101d8cSBoris Brezillon 
175b501baccSEric Anholt 	/* List of struct vc4_seqno_cb for callbacks to be made from a
176b501baccSEric Anholt 	 * workqueue when the given seqno is passed.
177b501baccSEric Anholt 	 */
178b501baccSEric Anholt 	struct list_head seqno_cb_list;
179b501baccSEric Anholt 
180553c942fSEric Anholt 	/* The memory used for storing binner tile alloc, tile state,
181553c942fSEric Anholt 	 * and overflow memory allocations.  This is freed when V3D
182553c942fSEric Anholt 	 * powers down.
183d5b1a78aSEric Anholt 	 */
184553c942fSEric Anholt 	struct vc4_bo *bin_bo;
185553c942fSEric Anholt 
186553c942fSEric Anholt 	/* Size of blocks allocated within bin_bo. */
187553c942fSEric Anholt 	uint32_t bin_alloc_size;
188553c942fSEric Anholt 
189553c942fSEric Anholt 	/* Bitmask of the bin_alloc_size chunks in bin_bo that are
190553c942fSEric Anholt 	 * used.
191553c942fSEric Anholt 	 */
192553c942fSEric Anholt 	uint32_t bin_alloc_used;
193553c942fSEric Anholt 
194553c942fSEric Anholt 	/* Bitmask of the current bin_alloc used for overflow memory. */
195553c942fSEric Anholt 	uint32_t bin_alloc_overflow;
196553c942fSEric Anholt 
197531a1b62SBoris Brezillon 	/* Incremented when an underrun error happened after an atomic commit.
198531a1b62SBoris Brezillon 	 * This is particularly useful to detect when a specific modeset is too
199531a1b62SBoris Brezillon 	 * demanding in term of memory or HVS bandwidth which is hard to guess
200531a1b62SBoris Brezillon 	 * at atomic check time.
201531a1b62SBoris Brezillon 	 */
202531a1b62SBoris Brezillon 	atomic_t underrun;
203531a1b62SBoris Brezillon 
204d5b1a78aSEric Anholt 	struct work_struct overflow_mem_work;
205d5b1a78aSEric Anholt 
20636cb6253SEric Anholt 	int power_refcount;
20736cb6253SEric Anholt 
2086b5c029dSPaul Kocialkowski 	/* Set to true when the load tracker is active. */
2096b5c029dSPaul Kocialkowski 	bool load_tracker_enabled;
2106b5c029dSPaul Kocialkowski 
21136cb6253SEric Anholt 	/* Mutex controlling the power refcount. */
21236cb6253SEric Anholt 	struct mutex power_lock;
21336cb6253SEric Anholt 
214d5b1a78aSEric Anholt 	struct {
215d5b1a78aSEric Anholt 		struct timer_list timer;
216d5b1a78aSEric Anholt 		struct work_struct reset_work;
217d5b1a78aSEric Anholt 	} hangcheck;
218d5b1a78aSEric Anholt 
219766cc6b1SStefan Schake 	struct drm_modeset_lock ctm_state_lock;
220766cc6b1SStefan Schake 	struct drm_private_obj ctm_manager;
221f2df84e0SMaxime Ripard 	struct drm_private_obj hvs_channels;
2224686da83SBoris Brezillon 	struct drm_private_obj load_tracker;
223c9be804cSEric Anholt 
224c9be804cSEric Anholt 	/* List of vc4_debugfs_info_entry for adding to debugfs once
225c9be804cSEric Anholt 	 * the minor is available (after drm_dev_register()).
226c9be804cSEric Anholt 	 */
227c9be804cSEric Anholt 	struct list_head debugfs_list;
22835c8b4b2SPaul Kocialkowski 
22935c8b4b2SPaul Kocialkowski 	/* Mutex for binner bo allocation. */
23035c8b4b2SPaul Kocialkowski 	struct mutex bin_bo_lock;
23135c8b4b2SPaul Kocialkowski 	/* Reference count for our binner bo. */
23235c8b4b2SPaul Kocialkowski 	struct kref bin_bo_kref;
233c8b75bcaSEric Anholt };
234c8b75bcaSEric Anholt 
235c8b75bcaSEric Anholt static inline struct vc4_dev *
236c8b75bcaSEric Anholt to_vc4_dev(struct drm_device *dev)
237c8b75bcaSEric Anholt {
23884d7d472SMaxime Ripard 	return container_of(dev, struct vc4_dev, base);
239c8b75bcaSEric Anholt }
240c8b75bcaSEric Anholt 
241c8b75bcaSEric Anholt struct vc4_bo {
2424a83c26aSDanilo Krummrich 	struct drm_gem_dma_object base;
243c826a6e1SEric Anholt 
2447edabee0SEric Anholt 	/* seqno of the last job to render using this BO. */
245d5b1a78aSEric Anholt 	uint64_t seqno;
246d5b1a78aSEric Anholt 
2477edabee0SEric Anholt 	/* seqno of the last job to use the RCL to write to this BO.
2487edabee0SEric Anholt 	 *
2497edabee0SEric Anholt 	 * Note that this doesn't include binner overflow memory
2507edabee0SEric Anholt 	 * writes.
2517edabee0SEric Anholt 	 */
2527edabee0SEric Anholt 	uint64_t write_seqno;
2537edabee0SEric Anholt 
25483753117SEric Anholt 	bool t_format;
25583753117SEric Anholt 
256c826a6e1SEric Anholt 	/* List entry for the BO's position in either
257c826a6e1SEric Anholt 	 * vc4_exec_info->unref_list or vc4_dev->bo_cache.time_list
258c826a6e1SEric Anholt 	 */
259c826a6e1SEric Anholt 	struct list_head unref_head;
260c826a6e1SEric Anholt 
261c826a6e1SEric Anholt 	/* Time in jiffies when the BO was put in vc4->bo_cache. */
262c826a6e1SEric Anholt 	unsigned long free_time;
263c826a6e1SEric Anholt 
264c826a6e1SEric Anholt 	/* List entry for the BO's position in vc4_dev->bo_cache.size_list */
265c826a6e1SEric Anholt 	struct list_head size_head;
266463873d5SEric Anholt 
267463873d5SEric Anholt 	/* Struct for shader validation state, if created by
268463873d5SEric Anholt 	 * DRM_IOCTL_VC4_CREATE_SHADER_BO.
269463873d5SEric Anholt 	 */
270463873d5SEric Anholt 	struct vc4_validated_shader_info *validated_shader;
271cdec4d36SEric Anholt 
272f3099462SEric Anholt 	/* One of enum vc4_kernel_bo_type, or VC4_BO_TYPE_COUNT + i
273f3099462SEric Anholt 	 * for user-allocated labels.
274f3099462SEric Anholt 	 */
275f3099462SEric Anholt 	int label;
276b9f19259SBoris Brezillon 
277b9f19259SBoris Brezillon 	/* Count the number of active users. This is needed to determine
278b9f19259SBoris Brezillon 	 * whether we can move the BO to the purgeable list or not (when the BO
279b9f19259SBoris Brezillon 	 * is used by the GPU or the display engine we can't purge it).
280b9f19259SBoris Brezillon 	 */
281b9f19259SBoris Brezillon 	refcount_t usecnt;
282b9f19259SBoris Brezillon 
283b9f19259SBoris Brezillon 	/* Store purgeable/purged state here */
284b9f19259SBoris Brezillon 	u32 madv;
285b9f19259SBoris Brezillon 	struct mutex madv_lock;
286c8b75bcaSEric Anholt };
287c8b75bcaSEric Anholt 
288c8b75bcaSEric Anholt static inline struct vc4_bo *
289c8b75bcaSEric Anholt to_vc4_bo(struct drm_gem_object *bo)
290c8b75bcaSEric Anholt {
2914a83c26aSDanilo Krummrich 	return container_of(to_drm_gem_dma_obj(bo), struct vc4_bo, base);
292c8b75bcaSEric Anholt }
293c8b75bcaSEric Anholt 
294cdec4d36SEric Anholt struct vc4_fence {
295cdec4d36SEric Anholt 	struct dma_fence base;
296cdec4d36SEric Anholt 	struct drm_device *dev;
297cdec4d36SEric Anholt 	/* vc4 seqno for signaled() test */
298cdec4d36SEric Anholt 	uint64_t seqno;
299cdec4d36SEric Anholt };
300cdec4d36SEric Anholt 
301cdec4d36SEric Anholt static inline struct vc4_fence *
302cdec4d36SEric Anholt to_vc4_fence(struct dma_fence *fence)
303cdec4d36SEric Anholt {
3045066f42cSMaxime Ripard 	return container_of(fence, struct vc4_fence, base);
305cdec4d36SEric Anholt }
306cdec4d36SEric Anholt 
307b501baccSEric Anholt struct vc4_seqno_cb {
308b501baccSEric Anholt 	struct work_struct work;
309b501baccSEric Anholt 	uint64_t seqno;
310b501baccSEric Anholt 	void (*func)(struct vc4_seqno_cb *cb);
311b501baccSEric Anholt };
312b501baccSEric Anholt 
313d3f5168aSEric Anholt struct vc4_v3d {
314001bdb55SEric Anholt 	struct vc4_dev *vc4;
315d3f5168aSEric Anholt 	struct platform_device *pdev;
316d3f5168aSEric Anholt 	void __iomem *regs;
317b72a2816SEric Anholt 	struct clk *clk;
3183051719aSEric Anholt 	struct debugfs_regset32 regset;
319d3f5168aSEric Anholt };
320d3f5168aSEric Anholt 
321c8b75bcaSEric Anholt struct vc4_hvs {
3221cbc91ebSMaxime Ripard 	struct vc4_dev *vc4;
323c8b75bcaSEric Anholt 	struct platform_device *pdev;
324c8b75bcaSEric Anholt 	void __iomem *regs;
325d8dbf44fSEric Anholt 	u32 __iomem *dlist;
326d8dbf44fSEric Anholt 
327d7d96c00SMaxime Ripard 	struct clk *core_clk;
328d7d96c00SMaxime Ripard 
329*2a001ca0SMaxime Ripard 	unsigned long max_core_rate;
330*2a001ca0SMaxime Ripard 
331d8dbf44fSEric Anholt 	/* Memory manager for CRTCs to allocate space in the display
332d8dbf44fSEric Anholt 	 * list.  Units are dwords.
333d8dbf44fSEric Anholt 	 */
334d8dbf44fSEric Anholt 	struct drm_mm dlist_mm;
33521af94cfSEric Anholt 	/* Memory manager for the LBM memory used by HVS scaling. */
33621af94cfSEric Anholt 	struct drm_mm lbm_mm;
337d8dbf44fSEric Anholt 	spinlock_t mm_lock;
33821af94cfSEric Anholt 
33921af94cfSEric Anholt 	struct drm_mm_node mitchell_netravali_filter;
340c54619b0SDave Stevenson 
3413051719aSEric Anholt 	struct debugfs_regset32 regset;
342*2a001ca0SMaxime Ripard 
343*2a001ca0SMaxime Ripard 	/*
344*2a001ca0SMaxime Ripard 	 * Even if HDMI0 on the RPi4 can output modes requiring a pixel
345*2a001ca0SMaxime Ripard 	 * rate higher than 297MHz, it needs some adjustments in the
346*2a001ca0SMaxime Ripard 	 * config.txt file to be able to do so and thus won't always be
347*2a001ca0SMaxime Ripard 	 * available.
348*2a001ca0SMaxime Ripard 	 */
349*2a001ca0SMaxime Ripard 	bool vc5_hdmi_enable_hdmi_20;
350c8b75bcaSEric Anholt };
351c8b75bcaSEric Anholt 
352c8b75bcaSEric Anholt struct vc4_plane {
353c8b75bcaSEric Anholt 	struct drm_plane base;
354c8b75bcaSEric Anholt };
355c8b75bcaSEric Anholt 
356c8b75bcaSEric Anholt static inline struct vc4_plane *
357c8b75bcaSEric Anholt to_vc4_plane(struct drm_plane *plane)
358c8b75bcaSEric Anholt {
3595066f42cSMaxime Ripard 	return container_of(plane, struct vc4_plane, base);
360c8b75bcaSEric Anholt }
361c8b75bcaSEric Anholt 
36282364698SStefan Schake enum vc4_scaling_mode {
36382364698SStefan Schake 	VC4_SCALING_NONE,
36482364698SStefan Schake 	VC4_SCALING_TPZ,
36582364698SStefan Schake 	VC4_SCALING_PPF,
36682364698SStefan Schake };
36782364698SStefan Schake 
36882364698SStefan Schake struct vc4_plane_state {
36982364698SStefan Schake 	struct drm_plane_state base;
37082364698SStefan Schake 	/* System memory copy of the display list for this element, computed
37182364698SStefan Schake 	 * at atomic_check time.
37282364698SStefan Schake 	 */
37382364698SStefan Schake 	u32 *dlist;
37482364698SStefan Schake 	u32 dlist_size; /* Number of dwords allocated for the display list */
37582364698SStefan Schake 	u32 dlist_count; /* Number of used dwords in the display list. */
37682364698SStefan Schake 
37782364698SStefan Schake 	/* Offset in the dlist to various words, for pageflip or
37882364698SStefan Schake 	 * cursor updates.
37982364698SStefan Schake 	 */
38082364698SStefan Schake 	u32 pos0_offset;
38182364698SStefan Schake 	u32 pos2_offset;
38282364698SStefan Schake 	u32 ptr0_offset;
3830a038c1cSBoris Brezillon 	u32 lbm_offset;
38482364698SStefan Schake 
38582364698SStefan Schake 	/* Offset where the plane's dlist was last stored in the
38682364698SStefan Schake 	 * hardware at vc4_crtc_atomic_flush() time.
38782364698SStefan Schake 	 */
38882364698SStefan Schake 	u32 __iomem *hw_dlist;
38982364698SStefan Schake 
39082364698SStefan Schake 	/* Clipped coordinates of the plane on the display. */
39182364698SStefan Schake 	int crtc_x, crtc_y, crtc_w, crtc_h;
39282364698SStefan Schake 	/* Clipped area being scanned from in the FB. */
39382364698SStefan Schake 	u32 src_x, src_y;
39482364698SStefan Schake 
39582364698SStefan Schake 	u32 src_w[2], src_h[2];
39682364698SStefan Schake 
39782364698SStefan Schake 	/* Scaling selection for the RGB/Y plane and the Cb/Cr planes. */
39882364698SStefan Schake 	enum vc4_scaling_mode x_scaling[2], y_scaling[2];
39982364698SStefan Schake 	bool is_unity;
40082364698SStefan Schake 	bool is_yuv;
40182364698SStefan Schake 
40282364698SStefan Schake 	/* Offset to start scanning out from the start of the plane's
40382364698SStefan Schake 	 * BO.
40482364698SStefan Schake 	 */
40582364698SStefan Schake 	u32 offsets[3];
40682364698SStefan Schake 
40782364698SStefan Schake 	/* Our allocation in LBM for temporary storage during scaling. */
40882364698SStefan Schake 	struct drm_mm_node lbm;
40982364698SStefan Schake 
41082364698SStefan Schake 	/* Set when the plane has per-pixel alpha content or does not cover
41182364698SStefan Schake 	 * the entire screen. This is a hint to the CRTC that it might need
41282364698SStefan Schake 	 * to enable background color fill.
41382364698SStefan Schake 	 */
41482364698SStefan Schake 	bool needs_bg_fill;
4158d938449SBoris Brezillon 
4168d938449SBoris Brezillon 	/* Mark the dlist as initialized. Useful to avoid initializing it twice
4178d938449SBoris Brezillon 	 * when async update is not possible.
4188d938449SBoris Brezillon 	 */
4198d938449SBoris Brezillon 	bool dlist_initialized;
4204686da83SBoris Brezillon 
4214686da83SBoris Brezillon 	/* Load of this plane on the HVS block. The load is expressed in HVS
4224686da83SBoris Brezillon 	 * cycles/sec.
4234686da83SBoris Brezillon 	 */
4244686da83SBoris Brezillon 	u64 hvs_load;
4254686da83SBoris Brezillon 
4264686da83SBoris Brezillon 	/* Memory bandwidth needed for this plane. This is expressed in
4274686da83SBoris Brezillon 	 * bytes/sec.
4284686da83SBoris Brezillon 	 */
4294686da83SBoris Brezillon 	u64 membus_load;
43082364698SStefan Schake };
43182364698SStefan Schake 
43282364698SStefan Schake static inline struct vc4_plane_state *
43382364698SStefan Schake to_vc4_plane_state(struct drm_plane_state *state)
43482364698SStefan Schake {
4355066f42cSMaxime Ripard 	return container_of(state, struct vc4_plane_state, base);
43682364698SStefan Schake }
43782364698SStefan Schake 
438c8b75bcaSEric Anholt enum vc4_encoder_type {
439ab8df60eSBoris Brezillon 	VC4_ENCODER_TYPE_NONE,
440ed024b22SMaxime Ripard 	VC4_ENCODER_TYPE_HDMI0,
441aa2fd1caSMaxime Ripard 	VC4_ENCODER_TYPE_HDMI1,
442c8b75bcaSEric Anholt 	VC4_ENCODER_TYPE_VEC,
443c8b75bcaSEric Anholt 	VC4_ENCODER_TYPE_DSI0,
444c8b75bcaSEric Anholt 	VC4_ENCODER_TYPE_DSI1,
445c8b75bcaSEric Anholt 	VC4_ENCODER_TYPE_SMI,
446c8b75bcaSEric Anholt 	VC4_ENCODER_TYPE_DPI,
447c8b75bcaSEric Anholt };
448c8b75bcaSEric Anholt 
449c8b75bcaSEric Anholt struct vc4_encoder {
450c8b75bcaSEric Anholt 	struct drm_encoder base;
451c8b75bcaSEric Anholt 	enum vc4_encoder_type type;
452c8b75bcaSEric Anholt 	u32 clock_select;
453792c3132SMaxime Ripard 
4548d914746SMaxime Ripard 	void (*pre_crtc_configure)(struct drm_encoder *encoder, struct drm_atomic_state *state);
4558d914746SMaxime Ripard 	void (*pre_crtc_enable)(struct drm_encoder *encoder, struct drm_atomic_state *state);
4568d914746SMaxime Ripard 	void (*post_crtc_enable)(struct drm_encoder *encoder, struct drm_atomic_state *state);
457792c3132SMaxime Ripard 
4588d914746SMaxime Ripard 	void (*post_crtc_disable)(struct drm_encoder *encoder, struct drm_atomic_state *state);
4598d914746SMaxime Ripard 	void (*post_crtc_powerdown)(struct drm_encoder *encoder, struct drm_atomic_state *state);
460c8b75bcaSEric Anholt };
461c8b75bcaSEric Anholt 
462c8b75bcaSEric Anholt static inline struct vc4_encoder *
463c8b75bcaSEric Anholt to_vc4_encoder(struct drm_encoder *encoder)
464c8b75bcaSEric Anholt {
465c8b75bcaSEric Anholt 	return container_of(encoder, struct vc4_encoder, base);
466c8b75bcaSEric Anholt }
467c8b75bcaSEric Anholt 
46879271807SStefan Schake struct vc4_crtc_data {
4696bad4774SMaxime Ripard 	const char *debugfs_name;
4706bad4774SMaxime Ripard 
47187ebcd42SMaxime Ripard 	/* Bitmask of channels (FIFOs) of the HVS that the output can source from */
47287ebcd42SMaxime Ripard 	unsigned int hvs_available_channels;
47387ebcd42SMaxime Ripard 
4748ebb2cf0SMaxime Ripard 	/* Which output of the HVS this pixelvalve sources from. */
4758ebb2cf0SMaxime Ripard 	int hvs_output;
4765a20ff8bSMaxime Ripard };
4775a20ff8bSMaxime Ripard 
4785a20ff8bSMaxime Ripard struct vc4_pv_data {
4795a20ff8bSMaxime Ripard 	struct vc4_crtc_data	base;
48079271807SStefan Schake 
481649abf2fSMaxime Ripard 	/* Depth of the PixelValve FIFO in bytes */
482649abf2fSMaxime Ripard 	unsigned int fifo_depth;
483649abf2fSMaxime Ripard 
484644df22fSMaxime Ripard 	/* Number of pixels output per clock period */
485644df22fSMaxime Ripard 	u8 pixels_per_clock;
486644df22fSMaxime Ripard 
48779271807SStefan Schake 	enum vc4_encoder_type encoder_types[4];
48879271807SStefan Schake };
48979271807SStefan Schake 
49079271807SStefan Schake struct vc4_crtc {
49179271807SStefan Schake 	struct drm_crtc base;
4923051719aSEric Anholt 	struct platform_device *pdev;
49379271807SStefan Schake 	const struct vc4_crtc_data *data;
49479271807SStefan Schake 	void __iomem *regs;
49579271807SStefan Schake 
49679271807SStefan Schake 	/* Timestamp at start of vblank irq - unaffected by lock delays. */
49779271807SStefan Schake 	ktime_t t_vblank;
49879271807SStefan Schake 
49979271807SStefan Schake 	u8 lut_r[256];
50079271807SStefan Schake 	u8 lut_g[256];
50179271807SStefan Schake 	u8 lut_b[256];
50279271807SStefan Schake 
50379271807SStefan Schake 	struct drm_pending_vblank_event *event;
5043051719aSEric Anholt 
5053051719aSEric Anholt 	struct debugfs_regset32 regset;
506a16c6640SMaxime Ripard 
507a16c6640SMaxime Ripard 	/**
508a16c6640SMaxime Ripard 	 * @feeds_txp: True if the CRTC feeds our writeback controller.
509a16c6640SMaxime Ripard 	 */
510a16c6640SMaxime Ripard 	bool feeds_txp;
5110c250c15SMaxime Ripard 
5120c250c15SMaxime Ripard 	/**
5130c250c15SMaxime Ripard 	 * @irq_lock: Spinlock protecting the resources shared between
5140c250c15SMaxime Ripard 	 * the atomic code and our vblank handler.
5150c250c15SMaxime Ripard 	 */
5160c250c15SMaxime Ripard 	spinlock_t irq_lock;
5170c250c15SMaxime Ripard 
5180c250c15SMaxime Ripard 	/**
5190c250c15SMaxime Ripard 	 * @current_dlist: Start offset of the display list currently
5200c250c15SMaxime Ripard 	 * set in the HVS for that CRTC. Protected by @irq_lock, and
5210c250c15SMaxime Ripard 	 * copied in vc4_hvs_update_dlist() for the CRTC interrupt
5220c250c15SMaxime Ripard 	 * handler to have access to that value.
5230c250c15SMaxime Ripard 	 */
5240c250c15SMaxime Ripard 	unsigned int current_dlist;
525eeb6ab46SMaxime Ripard 
526eeb6ab46SMaxime Ripard 	/**
527eeb6ab46SMaxime Ripard 	 * @current_hvs_channel: HVS channel currently assigned to the
528eeb6ab46SMaxime Ripard 	 * CRTC. Protected by @irq_lock, and copied in
529eeb6ab46SMaxime Ripard 	 * vc4_hvs_atomic_begin() for the CRTC interrupt handler to have
530eeb6ab46SMaxime Ripard 	 * access to that value.
531eeb6ab46SMaxime Ripard 	 */
532eeb6ab46SMaxime Ripard 	unsigned int current_hvs_channel;
53379271807SStefan Schake };
53479271807SStefan Schake 
53579271807SStefan Schake static inline struct vc4_crtc *
53679271807SStefan Schake to_vc4_crtc(struct drm_crtc *crtc)
53779271807SStefan Schake {
5385066f42cSMaxime Ripard 	return container_of(crtc, struct vc4_crtc, base);
53979271807SStefan Schake }
54079271807SStefan Schake 
5415a20ff8bSMaxime Ripard static inline const struct vc4_crtc_data *
5425a20ff8bSMaxime Ripard vc4_crtc_to_vc4_crtc_data(const struct vc4_crtc *crtc)
5435a20ff8bSMaxime Ripard {
5445a20ff8bSMaxime Ripard 	return crtc->data;
5455a20ff8bSMaxime Ripard }
5465a20ff8bSMaxime Ripard 
5475a20ff8bSMaxime Ripard static inline const struct vc4_pv_data *
5485a20ff8bSMaxime Ripard vc4_crtc_to_vc4_pv_data(const struct vc4_crtc *crtc)
5495a20ff8bSMaxime Ripard {
5505a20ff8bSMaxime Ripard 	const struct vc4_crtc_data *data = vc4_crtc_to_vc4_crtc_data(crtc);
5515a20ff8bSMaxime Ripard 
5525a20ff8bSMaxime Ripard 	return container_of(data, struct vc4_pv_data, base);
5535a20ff8bSMaxime Ripard }
5545a20ff8bSMaxime Ripard 
555d0229c36SMaxime Ripard struct drm_encoder *vc4_get_crtc_encoder(struct drm_crtc *crtc,
55694c1adc4SMaxime Ripard 					 struct drm_crtc_state *state);
557d0229c36SMaxime Ripard 
558ae44a527SMaxime Ripard struct vc4_crtc_state {
559ae44a527SMaxime Ripard 	struct drm_crtc_state base;
560ae44a527SMaxime Ripard 	/* Dlist area for this CRTC configuration. */
561ae44a527SMaxime Ripard 	struct drm_mm_node mm;
562ae44a527SMaxime Ripard 	bool txp_armed;
56387ebcd42SMaxime Ripard 	unsigned int assigned_channel;
564ae44a527SMaxime Ripard 
565ae44a527SMaxime Ripard 	struct {
566ae44a527SMaxime Ripard 		unsigned int left;
567ae44a527SMaxime Ripard 		unsigned int right;
568ae44a527SMaxime Ripard 		unsigned int top;
569ae44a527SMaxime Ripard 		unsigned int bottom;
570ae44a527SMaxime Ripard 	} margins;
5712820526dSMaxime Ripard 
57216e10105SMaxime Ripard 	unsigned long hvs_load;
57316e10105SMaxime Ripard 
5742820526dSMaxime Ripard 	/* Transitional state below, only valid during atomic commits */
5752820526dSMaxime Ripard 	bool update_muxing;
576ae44a527SMaxime Ripard };
577ae44a527SMaxime Ripard 
5788ba0b6d1SMaxime Ripard #define VC4_HVS_CHANNEL_DISABLED ((unsigned int)-1)
5798ba0b6d1SMaxime Ripard 
580ae44a527SMaxime Ripard static inline struct vc4_crtc_state *
581ae44a527SMaxime Ripard to_vc4_crtc_state(struct drm_crtc_state *crtc_state)
582ae44a527SMaxime Ripard {
5835066f42cSMaxime Ripard 	return container_of(crtc_state, struct vc4_crtc_state, base);
584ae44a527SMaxime Ripard }
585ae44a527SMaxime Ripard 
586d3f5168aSEric Anholt #define V3D_READ(offset) readl(vc4->v3d->regs + offset)
587d3f5168aSEric Anholt #define V3D_WRITE(offset, val) writel(val, vc4->v3d->regs + offset)
5883454f01aSMaxime Ripard #define HVS_READ(offset) readl(hvs->regs + offset)
5893454f01aSMaxime Ripard #define HVS_WRITE(offset, val) writel(val, hvs->regs + offset)
590c8b75bcaSEric Anholt 
5913051719aSEric Anholt #define VC4_REG32(reg) { .name = #reg, .offset = reg }
5923051719aSEric Anholt 
593d5b1a78aSEric Anholt struct vc4_exec_info {
59430f8c74cSMaxime Ripard 	struct vc4_dev *dev;
59530f8c74cSMaxime Ripard 
596d5b1a78aSEric Anholt 	/* Sequence number for this bin/render job. */
597d5b1a78aSEric Anholt 	uint64_t seqno;
598d5b1a78aSEric Anholt 
5997edabee0SEric Anholt 	/* Latest write_seqno of any BO that binning depends on. */
6007edabee0SEric Anholt 	uint64_t bin_dep_seqno;
6017edabee0SEric Anholt 
602cdec4d36SEric Anholt 	struct dma_fence *fence;
603cdec4d36SEric Anholt 
604c4ce60dcSEric Anholt 	/* Last current addresses the hardware was processing when the
605c4ce60dcSEric Anholt 	 * hangcheck timer checked on us.
606c4ce60dcSEric Anholt 	 */
607c4ce60dcSEric Anholt 	uint32_t last_ct0ca, last_ct1ca;
608c4ce60dcSEric Anholt 
609d5b1a78aSEric Anholt 	/* Kernel-space copy of the ioctl arguments */
610d5b1a78aSEric Anholt 	struct drm_vc4_submit_cl *args;
611d5b1a78aSEric Anholt 
612d5b1a78aSEric Anholt 	/* This is the array of BOs that were looked up at the start of exec.
613d5b1a78aSEric Anholt 	 * Command validation will use indices into this array.
614d5b1a78aSEric Anholt 	 */
6154a83c26aSDanilo Krummrich 	struct drm_gem_dma_object **bo;
616d5b1a78aSEric Anholt 	uint32_t bo_count;
617d5b1a78aSEric Anholt 
6187edabee0SEric Anholt 	/* List of BOs that are being written by the RCL.  Other than
6197edabee0SEric Anholt 	 * the binner temporary storage, this is all the BOs written
6207edabee0SEric Anholt 	 * by the job.
6217edabee0SEric Anholt 	 */
6224a83c26aSDanilo Krummrich 	struct drm_gem_dma_object *rcl_write_bo[4];
6237edabee0SEric Anholt 	uint32_t rcl_write_bo_count;
6247edabee0SEric Anholt 
625d5b1a78aSEric Anholt 	/* Pointers for our position in vc4->job_list */
626d5b1a78aSEric Anholt 	struct list_head head;
627d5b1a78aSEric Anholt 
628d5b1a78aSEric Anholt 	/* List of other BOs used in the job that need to be released
629d5b1a78aSEric Anholt 	 * once the job is complete.
630d5b1a78aSEric Anholt 	 */
631d5b1a78aSEric Anholt 	struct list_head unref_list;
632d5b1a78aSEric Anholt 
633d5b1a78aSEric Anholt 	/* Current unvalidated indices into @bo loaded by the non-hardware
634d5b1a78aSEric Anholt 	 * VC4_PACKET_GEM_HANDLES.
635d5b1a78aSEric Anholt 	 */
636d5b1a78aSEric Anholt 	uint32_t bo_index[2];
637d5b1a78aSEric Anholt 
638d5b1a78aSEric Anholt 	/* This is the BO where we store the validated command lists, shader
639d5b1a78aSEric Anholt 	 * records, and uniforms.
640d5b1a78aSEric Anholt 	 */
6414a83c26aSDanilo Krummrich 	struct drm_gem_dma_object *exec_bo;
642d5b1a78aSEric Anholt 
643d5b1a78aSEric Anholt 	/**
644d5b1a78aSEric Anholt 	 * This tracks the per-shader-record state (packet 64) that
645d5b1a78aSEric Anholt 	 * determines the length of the shader record and the offset
646d5b1a78aSEric Anholt 	 * it's expected to be found at.  It gets read in from the
647d5b1a78aSEric Anholt 	 * command lists.
648d5b1a78aSEric Anholt 	 */
649d5b1a78aSEric Anholt 	struct vc4_shader_state {
650d5b1a78aSEric Anholt 		uint32_t addr;
651d5b1a78aSEric Anholt 		/* Maximum vertex index referenced by any primitive using this
652d5b1a78aSEric Anholt 		 * shader state.
653d5b1a78aSEric Anholt 		 */
654d5b1a78aSEric Anholt 		uint32_t max_index;
655d5b1a78aSEric Anholt 	} *shader_state;
656d5b1a78aSEric Anholt 
657d5b1a78aSEric Anholt 	/** How many shader states the user declared they were using. */
658d5b1a78aSEric Anholt 	uint32_t shader_state_size;
659d5b1a78aSEric Anholt 	/** How many shader state records the validator has seen. */
660d5b1a78aSEric Anholt 	uint32_t shader_state_count;
661d5b1a78aSEric Anholt 
662d5b1a78aSEric Anholt 	bool found_tile_binning_mode_config_packet;
663d5b1a78aSEric Anholt 	bool found_start_tile_binning_packet;
664d5b1a78aSEric Anholt 	bool found_increment_semaphore_packet;
665d5b1a78aSEric Anholt 	bool found_flush;
666d5b1a78aSEric Anholt 	uint8_t bin_tiles_x, bin_tiles_y;
667553c942fSEric Anholt 	/* Physical address of the start of the tile alloc array
668553c942fSEric Anholt 	 * (where each tile's binned CL will start)
669553c942fSEric Anholt 	 */
670d5b1a78aSEric Anholt 	uint32_t tile_alloc_offset;
671553c942fSEric Anholt 	/* Bitmask of which binner slots are freed when this job completes. */
672553c942fSEric Anholt 	uint32_t bin_slots;
673d5b1a78aSEric Anholt 
674d5b1a78aSEric Anholt 	/**
675d5b1a78aSEric Anholt 	 * Computed addresses pointing into exec_bo where we start the
676d5b1a78aSEric Anholt 	 * bin thread (ct0) and render thread (ct1).
677d5b1a78aSEric Anholt 	 */
678d5b1a78aSEric Anholt 	uint32_t ct0ca, ct0ea;
679d5b1a78aSEric Anholt 	uint32_t ct1ca, ct1ea;
680d5b1a78aSEric Anholt 
681d5b1a78aSEric Anholt 	/* Pointer to the unvalidated bin CL (if present). */
682d5b1a78aSEric Anholt 	void *bin_u;
683d5b1a78aSEric Anholt 
684d5b1a78aSEric Anholt 	/* Pointers to the shader recs.  These paddr gets incremented as CL
685d5b1a78aSEric Anholt 	 * packets are relocated in validate_gl_shader_state, and the vaddrs
686d5b1a78aSEric Anholt 	 * (u and v) get incremented and size decremented as the shader recs
687d5b1a78aSEric Anholt 	 * themselves are validated.
688d5b1a78aSEric Anholt 	 */
689d5b1a78aSEric Anholt 	void *shader_rec_u;
690d5b1a78aSEric Anholt 	void *shader_rec_v;
691d5b1a78aSEric Anholt 	uint32_t shader_rec_p;
692d5b1a78aSEric Anholt 	uint32_t shader_rec_size;
693d5b1a78aSEric Anholt 
694d5b1a78aSEric Anholt 	/* Pointers to the uniform data.  These pointers are incremented, and
695d5b1a78aSEric Anholt 	 * size decremented, as each batch of uniforms is uploaded.
696d5b1a78aSEric Anholt 	 */
697d5b1a78aSEric Anholt 	void *uniforms_u;
698d5b1a78aSEric Anholt 	void *uniforms_v;
699d5b1a78aSEric Anholt 	uint32_t uniforms_p;
700d5b1a78aSEric Anholt 	uint32_t uniforms_size;
70165101d8cSBoris Brezillon 
70265101d8cSBoris Brezillon 	/* Pointer to a performance monitor object if the user requested it,
70365101d8cSBoris Brezillon 	 * NULL otherwise.
70465101d8cSBoris Brezillon 	 */
70565101d8cSBoris Brezillon 	struct vc4_perfmon *perfmon;
70635c8b4b2SPaul Kocialkowski 
70735c8b4b2SPaul Kocialkowski 	/* Whether the exec has taken a reference to the binner BO, which should
70835c8b4b2SPaul Kocialkowski 	 * happen with a VC4_PACKET_TILE_BINNING_MODE_CONFIG packet.
70935c8b4b2SPaul Kocialkowski 	 */
71035c8b4b2SPaul Kocialkowski 	bool bin_bo_used;
71165101d8cSBoris Brezillon };
71265101d8cSBoris Brezillon 
71365101d8cSBoris Brezillon /* Per-open file private data. Any driver-specific resource that has to be
71465101d8cSBoris Brezillon  * released when the DRM file is closed should be placed here.
71565101d8cSBoris Brezillon  */
71665101d8cSBoris Brezillon struct vc4_file {
71730f8c74cSMaxime Ripard 	struct vc4_dev *dev;
71830f8c74cSMaxime Ripard 
71965101d8cSBoris Brezillon 	struct {
72065101d8cSBoris Brezillon 		struct idr idr;
72165101d8cSBoris Brezillon 		struct mutex lock;
72265101d8cSBoris Brezillon 	} perfmon;
72335c8b4b2SPaul Kocialkowski 
72435c8b4b2SPaul Kocialkowski 	bool bin_bo_used;
725d5b1a78aSEric Anholt };
726d5b1a78aSEric Anholt 
727d5b1a78aSEric Anholt static inline struct vc4_exec_info *
728ca26d28bSVarad Gautam vc4_first_bin_job(struct vc4_dev *vc4)
729d5b1a78aSEric Anholt {
73057b9f569SMasahiro Yamada 	return list_first_entry_or_null(&vc4->bin_job_list,
73157b9f569SMasahiro Yamada 					struct vc4_exec_info, head);
732ca26d28bSVarad Gautam }
733ca26d28bSVarad Gautam 
734ca26d28bSVarad Gautam static inline struct vc4_exec_info *
735ca26d28bSVarad Gautam vc4_first_render_job(struct vc4_dev *vc4)
736ca26d28bSVarad Gautam {
73757b9f569SMasahiro Yamada 	return list_first_entry_or_null(&vc4->render_job_list,
738ca26d28bSVarad Gautam 					struct vc4_exec_info, head);
739d5b1a78aSEric Anholt }
740d5b1a78aSEric Anholt 
7419326e6f2SEric Anholt static inline struct vc4_exec_info *
7429326e6f2SEric Anholt vc4_last_render_job(struct vc4_dev *vc4)
7439326e6f2SEric Anholt {
7449326e6f2SEric Anholt 	if (list_empty(&vc4->render_job_list))
7459326e6f2SEric Anholt 		return NULL;
7469326e6f2SEric Anholt 	return list_last_entry(&vc4->render_job_list,
7479326e6f2SEric Anholt 			       struct vc4_exec_info, head);
7489326e6f2SEric Anholt }
7499326e6f2SEric Anholt 
750c8b75bcaSEric Anholt /**
751463873d5SEric Anholt  * struct vc4_texture_sample_info - saves the offsets into the UBO for texture
752463873d5SEric Anholt  * setup parameters.
753463873d5SEric Anholt  *
754463873d5SEric Anholt  * This will be used at draw time to relocate the reference to the texture
755463873d5SEric Anholt  * contents in p0, and validate that the offset combined with
756463873d5SEric Anholt  * width/height/stride/etc. from p1 and p2/p3 doesn't sample outside the BO.
757463873d5SEric Anholt  * Note that the hardware treats unprovided config parameters as 0, so not all
758463873d5SEric Anholt  * of them need to be set up for every texure sample, and we'll store ~0 as
759463873d5SEric Anholt  * the offset to mark the unused ones.
760463873d5SEric Anholt  *
761463873d5SEric Anholt  * See the VC4 3D architecture guide page 41 ("Texture and Memory Lookup Unit
762463873d5SEric Anholt  * Setup") for definitions of the texture parameters.
763463873d5SEric Anholt  */
764463873d5SEric Anholt struct vc4_texture_sample_info {
765463873d5SEric Anholt 	bool is_direct;
766463873d5SEric Anholt 	uint32_t p_offset[4];
767463873d5SEric Anholt };
768463873d5SEric Anholt 
769463873d5SEric Anholt /**
770463873d5SEric Anholt  * struct vc4_validated_shader_info - information about validated shaders that
771463873d5SEric Anholt  * needs to be used from command list validation.
772463873d5SEric Anholt  *
773463873d5SEric Anholt  * For a given shader, each time a shader state record references it, we need
774463873d5SEric Anholt  * to verify that the shader doesn't read more uniforms than the shader state
775463873d5SEric Anholt  * record's uniform BO pointer can provide, and we need to apply relocations
776463873d5SEric Anholt  * and validate the shader state record's uniforms that define the texture
777463873d5SEric Anholt  * samples.
778463873d5SEric Anholt  */
779463873d5SEric Anholt struct vc4_validated_shader_info {
780463873d5SEric Anholt 	uint32_t uniforms_size;
781463873d5SEric Anholt 	uint32_t uniforms_src_size;
782463873d5SEric Anholt 	uint32_t num_texture_samples;
783463873d5SEric Anholt 	struct vc4_texture_sample_info *texture_samples;
7846d45c81dSEric Anholt 
7856d45c81dSEric Anholt 	uint32_t num_uniform_addr_offsets;
7866d45c81dSEric Anholt 	uint32_t *uniform_addr_offsets;
787c778cc5dSJonas Pfeil 
788c778cc5dSJonas Pfeil 	bool is_threaded;
789463873d5SEric Anholt };
790463873d5SEric Anholt 
791463873d5SEric Anholt /**
7927f2a09ecSJames Hughes  * __wait_for - magic wait macro
793c8b75bcaSEric Anholt  *
7947f2a09ecSJames Hughes  * Macro to help avoid open coding check/wait/timeout patterns. Note that it's
7957f2a09ecSJames Hughes  * important that we check the condition again after having timed out, since the
7967f2a09ecSJames Hughes  * timeout could be due to preemption or similar and we've never had a chance to
7977f2a09ecSJames Hughes  * check the condition before the timeout.
798c8b75bcaSEric Anholt  */
7997f2a09ecSJames Hughes #define __wait_for(OP, COND, US, Wmin, Wmax) ({ \
8007f2a09ecSJames Hughes 	const ktime_t end__ = ktime_add_ns(ktime_get_raw(), 1000ll * (US)); \
8017f2a09ecSJames Hughes 	long wait__ = (Wmin); /* recommended min for usleep is 10 us */	\
8027f2a09ecSJames Hughes 	int ret__;							\
8037f2a09ecSJames Hughes 	might_sleep();							\
8047f2a09ecSJames Hughes 	for (;;) {							\
8057f2a09ecSJames Hughes 		const bool expired__ = ktime_after(ktime_get_raw(), end__); \
8067f2a09ecSJames Hughes 		OP;							\
8077f2a09ecSJames Hughes 		/* Guarantee COND check prior to timeout */		\
8087f2a09ecSJames Hughes 		barrier();						\
8097f2a09ecSJames Hughes 		if (COND) {						\
8107f2a09ecSJames Hughes 			ret__ = 0;					\
8117f2a09ecSJames Hughes 			break;						\
8127f2a09ecSJames Hughes 		}							\
8137f2a09ecSJames Hughes 		if (expired__) {					\
814c8b75bcaSEric Anholt 			ret__ = -ETIMEDOUT;				\
815c8b75bcaSEric Anholt 			break;						\
816c8b75bcaSEric Anholt 		}							\
8177f2a09ecSJames Hughes 		usleep_range(wait__, wait__ * 2);			\
8187f2a09ecSJames Hughes 		if (wait__ < (Wmax))					\
8197f2a09ecSJames Hughes 			wait__ <<= 1;					\
820c8b75bcaSEric Anholt 	}								\
821c8b75bcaSEric Anholt 	ret__;								\
822c8b75bcaSEric Anholt })
823c8b75bcaSEric Anholt 
8247f2a09ecSJames Hughes #define _wait_for(COND, US, Wmin, Wmax)	__wait_for(, (COND), (US), (Wmin), \
8257f2a09ecSJames Hughes 						   (Wmax))
8267f2a09ecSJames Hughes #define wait_for(COND, MS)		_wait_for((COND), (MS) * 1000, 10, 1000)
827c8b75bcaSEric Anholt 
828c8b75bcaSEric Anholt /* vc4_bo.c */
829c826a6e1SEric Anholt struct drm_gem_object *vc4_create_object(struct drm_device *dev, size_t size);
830c826a6e1SEric Anholt struct vc4_bo *vc4_bo_create(struct drm_device *dev, size_t size,
831f3099462SEric Anholt 			     bool from_cache, enum vc4_kernel_bo_type type);
832dd2dfd44SMaxime Ripard int vc4_bo_dumb_create(struct drm_file *file_priv,
833c8b75bcaSEric Anholt 		       struct drm_device *dev,
834c8b75bcaSEric Anholt 		       struct drm_mode_create_dumb *args);
835d5bc60f6SEric Anholt int vc4_create_bo_ioctl(struct drm_device *dev, void *data,
836d5bc60f6SEric Anholt 			struct drm_file *file_priv);
837463873d5SEric Anholt int vc4_create_shader_bo_ioctl(struct drm_device *dev, void *data,
838463873d5SEric Anholt 			       struct drm_file *file_priv);
839d5bc60f6SEric Anholt int vc4_mmap_bo_ioctl(struct drm_device *dev, void *data,
840d5bc60f6SEric Anholt 		      struct drm_file *file_priv);
84183753117SEric Anholt int vc4_set_tiling_ioctl(struct drm_device *dev, void *data,
84283753117SEric Anholt 			 struct drm_file *file_priv);
84383753117SEric Anholt int vc4_get_tiling_ioctl(struct drm_device *dev, void *data,
84483753117SEric Anholt 			 struct drm_file *file_priv);
84521461365SEric Anholt int vc4_get_hang_state_ioctl(struct drm_device *dev, void *data,
84621461365SEric Anholt 			     struct drm_file *file_priv);
847f3099462SEric Anholt int vc4_label_bo_ioctl(struct drm_device *dev, void *data,
848f3099462SEric Anholt 		       struct drm_file *file_priv);
849f3099462SEric Anholt int vc4_bo_cache_init(struct drm_device *dev);
850b9f19259SBoris Brezillon int vc4_bo_inc_usecnt(struct vc4_bo *bo);
851b9f19259SBoris Brezillon void vc4_bo_dec_usecnt(struct vc4_bo *bo);
852b9f19259SBoris Brezillon void vc4_bo_add_to_purgeable_pool(struct vc4_bo *bo);
853b9f19259SBoris Brezillon void vc4_bo_remove_from_purgeable_pool(struct vc4_bo *bo);
854445b287eSMaxime Ripard int vc4_bo_debugfs_init(struct drm_minor *minor);
855c8b75bcaSEric Anholt 
856c8b75bcaSEric Anholt /* vc4_crtc.c */
857c8b75bcaSEric Anholt extern struct platform_driver vc4_crtc_driver;
858875a4d53SMaxime Ripard int vc4_crtc_disable_at_boot(struct drm_crtc *crtc);
8595fefc601SMaxime Ripard int vc4_crtc_init(struct drm_device *drm, struct vc4_crtc *vc4_crtc,
8605fefc601SMaxime Ripard 		  const struct drm_crtc_funcs *crtc_funcs,
8615fefc601SMaxime Ripard 		  const struct drm_crtc_helper_funcs *crtc_helper_funcs);
862bdd96472SMaxime Ripard int vc4_page_flip(struct drm_crtc *crtc,
863bdd96472SMaxime Ripard 		  struct drm_framebuffer *fb,
864bdd96472SMaxime Ripard 		  struct drm_pending_vblank_event *event,
865bdd96472SMaxime Ripard 		  uint32_t flags,
866bdd96472SMaxime Ripard 		  struct drm_modeset_acquire_ctx *ctx);
867bdd96472SMaxime Ripard struct drm_crtc_state *vc4_crtc_duplicate_state(struct drm_crtc *crtc);
868bdd96472SMaxime Ripard void vc4_crtc_destroy_state(struct drm_crtc *crtc,
869bdd96472SMaxime Ripard 			    struct drm_crtc_state *state);
870bdd96472SMaxime Ripard void vc4_crtc_reset(struct drm_crtc *crtc);
871008095e0SBoris Brezillon void vc4_crtc_handle_vblank(struct vc4_crtc *crtc);
87268e4a69aSMaxime Ripard void vc4_crtc_send_vblank(struct drm_crtc *crtc);
873445b287eSMaxime Ripard int vc4_crtc_late_register(struct drm_crtc *crtc);
874666e7358SBoris Brezillon void vc4_crtc_get_margins(struct drm_crtc_state *state,
875e590c2b0SDan Carpenter 			  unsigned int *left, unsigned int *right,
876666e7358SBoris Brezillon 			  unsigned int *top, unsigned int *bottom);
877c8b75bcaSEric Anholt 
878c8b75bcaSEric Anholt /* vc4_debugfs.c */
8797ce84471SWambui Karuga void vc4_debugfs_init(struct drm_minor *minor);
880c9be804cSEric Anholt #ifdef CONFIG_DEBUG_FS
881445b287eSMaxime Ripard int vc4_debugfs_add_file(struct drm_minor *minor,
882c9be804cSEric Anholt 			 const char *filename,
883c9be804cSEric Anholt 			 int (*show)(struct seq_file*, void*),
884c9be804cSEric Anholt 			 void *data);
885445b287eSMaxime Ripard int vc4_debugfs_add_regset32(struct drm_minor *minor,
886c9be804cSEric Anholt 			     const char *filename,
887c9be804cSEric Anholt 			     struct debugfs_regset32 *regset);
888c9be804cSEric Anholt #else
889445b287eSMaxime Ripard static inline int vc4_debugfs_add_file(struct drm_minor *minor,
890c9be804cSEric Anholt 				       const char *filename,
891c9be804cSEric Anholt 				       int (*show)(struct seq_file*, void*),
892c9be804cSEric Anholt 				       void *data)
893c9be804cSEric Anholt {
894fe3b0f78SMaxime Ripard 	return 0;
895c9be804cSEric Anholt }
896c9be804cSEric Anholt 
897445b287eSMaxime Ripard static inline int vc4_debugfs_add_regset32(struct drm_minor *minor,
898c9be804cSEric Anholt 					   const char *filename,
899c9be804cSEric Anholt 					   struct debugfs_regset32 *regset)
900c9be804cSEric Anholt {
901fe3b0f78SMaxime Ripard 	return 0;
902c9be804cSEric Anholt }
903c9be804cSEric Anholt #endif
904c8b75bcaSEric Anholt 
905c8b75bcaSEric Anholt /* vc4_drv.c */
906c8b75bcaSEric Anholt void __iomem *vc4_ioremap_regs(struct platform_device *dev, int index);
9073d763742SMaxime Ripard int vc4_dumb_fixup_args(struct drm_mode_create_dumb *args);
908c8b75bcaSEric Anholt 
90908302c35SEric Anholt /* vc4_dpi.c */
91008302c35SEric Anholt extern struct platform_driver vc4_dpi_driver;
91108302c35SEric Anholt 
9124078f575SEric Anholt /* vc4_dsi.c */
9134078f575SEric Anholt extern struct platform_driver vc4_dsi_driver;
9144078f575SEric Anholt 
915cdec4d36SEric Anholt /* vc4_fence.c */
916cdec4d36SEric Anholt extern const struct dma_fence_ops vc4_fence_ops;
917cdec4d36SEric Anholt 
918d5b1a78aSEric Anholt /* vc4_gem.c */
919171a072bSMaxime Ripard int vc4_gem_init(struct drm_device *dev);
920d5b1a78aSEric Anholt int vc4_submit_cl_ioctl(struct drm_device *dev, void *data,
921d5b1a78aSEric Anholt 			struct drm_file *file_priv);
922d5b1a78aSEric Anholt int vc4_wait_seqno_ioctl(struct drm_device *dev, void *data,
923d5b1a78aSEric Anholt 			 struct drm_file *file_priv);
924d5b1a78aSEric Anholt int vc4_wait_bo_ioctl(struct drm_device *dev, void *data,
925d5b1a78aSEric Anholt 		      struct drm_file *file_priv);
926ca26d28bSVarad Gautam void vc4_submit_next_bin_job(struct drm_device *dev);
927ca26d28bSVarad Gautam void vc4_submit_next_render_job(struct drm_device *dev);
928ca26d28bSVarad Gautam void vc4_move_job_to_render(struct drm_device *dev, struct vc4_exec_info *exec);
929d5b1a78aSEric Anholt int vc4_wait_for_seqno(struct drm_device *dev, uint64_t seqno,
930d5b1a78aSEric Anholt 		       uint64_t timeout_ns, bool interruptible);
931d5b1a78aSEric Anholt void vc4_job_handle_completed(struct vc4_dev *vc4);
932b501baccSEric Anholt int vc4_queue_seqno_cb(struct drm_device *dev,
933b501baccSEric Anholt 		       struct vc4_seqno_cb *cb, uint64_t seqno,
934b501baccSEric Anholt 		       void (*func)(struct vc4_seqno_cb *cb));
935b9f19259SBoris Brezillon int vc4_gem_madvise_ioctl(struct drm_device *dev, void *data,
936b9f19259SBoris Brezillon 			  struct drm_file *file_priv);
937d5b1a78aSEric Anholt 
938c8b75bcaSEric Anholt /* vc4_hdmi.c */
939c8b75bcaSEric Anholt extern struct platform_driver vc4_hdmi_driver;
940c8b75bcaSEric Anholt 
9419a8d5e4aSBoris Brezillon /* vc4_vec.c */
942e4b81f8cSBoris Brezillon extern struct platform_driver vc4_vec_driver;
943e4b81f8cSBoris Brezillon 
944008095e0SBoris Brezillon /* vc4_txp.c */
945008095e0SBoris Brezillon extern struct platform_driver vc4_txp_driver;
946008095e0SBoris Brezillon 
947d5b1a78aSEric Anholt /* vc4_irq.c */
9485226711eSThomas Zimmermann void vc4_irq_enable(struct drm_device *dev);
9495226711eSThomas Zimmermann void vc4_irq_disable(struct drm_device *dev);
9505226711eSThomas Zimmermann int vc4_irq_install(struct drm_device *dev, int irq);
951d5b1a78aSEric Anholt void vc4_irq_uninstall(struct drm_device *dev);
952d5b1a78aSEric Anholt void vc4_irq_reset(struct drm_device *dev);
953d5b1a78aSEric Anholt 
954c8b75bcaSEric Anholt /* vc4_hvs.c */
955c8b75bcaSEric Anholt extern struct platform_driver vc4_hvs_driver;
9563454f01aSMaxime Ripard void vc4_hvs_stop_channel(struct vc4_hvs *hvs, unsigned int output);
9573454f01aSMaxime Ripard int vc4_hvs_get_fifo_from_output(struct vc4_hvs *hvs, unsigned int output);
9583454f01aSMaxime Ripard u8 vc4_hvs_get_fifo_frame_count(struct vc4_hvs *hvs, unsigned int fifo);
959ee6965c8SMaxime Ripard int vc4_hvs_atomic_check(struct drm_crtc *crtc, struct drm_atomic_state *state);
960eeb6ab46SMaxime Ripard void vc4_hvs_atomic_begin(struct drm_crtc *crtc, struct drm_atomic_state *state);
961ee6965c8SMaxime Ripard void vc4_hvs_atomic_enable(struct drm_crtc *crtc, struct drm_atomic_state *state);
962ee6965c8SMaxime Ripard void vc4_hvs_atomic_disable(struct drm_crtc *crtc, struct drm_atomic_state *state);
963ee6965c8SMaxime Ripard void vc4_hvs_atomic_flush(struct drm_crtc *crtc, struct drm_atomic_state *state);
9643454f01aSMaxime Ripard void vc4_hvs_dump_state(struct vc4_hvs *hvs);
9653454f01aSMaxime Ripard void vc4_hvs_unmask_underrun(struct vc4_hvs *hvs, int channel);
9663454f01aSMaxime Ripard void vc4_hvs_mask_underrun(struct vc4_hvs *hvs, int channel);
967445b287eSMaxime Ripard int vc4_hvs_debugfs_init(struct drm_minor *minor);
968c8b75bcaSEric Anholt 
969c8b75bcaSEric Anholt /* vc4_kms.c */
970c8b75bcaSEric Anholt int vc4_kms_load(struct drm_device *dev);
971c8b75bcaSEric Anholt 
972c8b75bcaSEric Anholt /* vc4_plane.c */
973c8b75bcaSEric Anholt struct drm_plane *vc4_plane_init(struct drm_device *dev,
97477c5fb12SMaxime Ripard 				 enum drm_plane_type type,
97577c5fb12SMaxime Ripard 				 uint32_t possible_crtcs);
9760c2a50f1SMaxime Ripard int vc4_plane_create_additional_planes(struct drm_device *dev);
977c8b75bcaSEric Anholt u32 vc4_plane_write_dlist(struct drm_plane *plane, u32 __iomem *dlist);
9782f196b7cSDaniel Vetter u32 vc4_plane_dlist_size(const struct drm_plane_state *state);
979b501baccSEric Anholt void vc4_plane_async_set_fb(struct drm_plane *plane,
980b501baccSEric Anholt 			    struct drm_framebuffer *fb);
981463873d5SEric Anholt 
982d3f5168aSEric Anholt /* vc4_v3d.c */
983d3f5168aSEric Anholt extern struct platform_driver vc4_v3d_driver;
984ffc26740SEric Anholt extern const struct of_device_id vc4_v3d_dt_match[];
985553c942fSEric Anholt int vc4_v3d_get_bin_slot(struct vc4_dev *vc4);
98635c8b4b2SPaul Kocialkowski int vc4_v3d_bin_bo_get(struct vc4_dev *vc4, bool *used);
98735c8b4b2SPaul Kocialkowski void vc4_v3d_bin_bo_put(struct vc4_dev *vc4);
988cb74f6eeSEric Anholt int vc4_v3d_pm_get(struct vc4_dev *vc4);
989cb74f6eeSEric Anholt void vc4_v3d_pm_put(struct vc4_dev *vc4);
990445b287eSMaxime Ripard int vc4_v3d_debugfs_init(struct drm_minor *minor);
991d5b1a78aSEric Anholt 
992d5b1a78aSEric Anholt /* vc4_validate.c */
993d5b1a78aSEric Anholt int
994d5b1a78aSEric Anholt vc4_validate_bin_cl(struct drm_device *dev,
995d5b1a78aSEric Anholt 		    void *validated,
996d5b1a78aSEric Anholt 		    void *unvalidated,
997d5b1a78aSEric Anholt 		    struct vc4_exec_info *exec);
998d5b1a78aSEric Anholt 
999d5b1a78aSEric Anholt int
1000d5b1a78aSEric Anholt vc4_validate_shader_recs(struct drm_device *dev, struct vc4_exec_info *exec);
1001d5b1a78aSEric Anholt 
10024a83c26aSDanilo Krummrich struct drm_gem_dma_object *vc4_use_bo(struct vc4_exec_info *exec,
1003d5b1a78aSEric Anholt 				      uint32_t hindex);
1004d5b1a78aSEric Anholt 
1005d5b1a78aSEric Anholt int vc4_get_rcl(struct drm_device *dev, struct vc4_exec_info *exec);
1006d5b1a78aSEric Anholt 
1007d5b1a78aSEric Anholt bool vc4_check_tex_size(struct vc4_exec_info *exec,
10084a83c26aSDanilo Krummrich 			struct drm_gem_dma_object *fbo,
1009d5b1a78aSEric Anholt 			uint32_t offset, uint8_t tiling_format,
1010d5b1a78aSEric Anholt 			uint32_t width, uint32_t height, uint8_t cpp);
1011d3f5168aSEric Anholt 
1012463873d5SEric Anholt /* vc4_validate_shader.c */
1013463873d5SEric Anholt struct vc4_validated_shader_info *
10144a83c26aSDanilo Krummrich vc4_validate_shader(struct drm_gem_dma_object *shader_obj);
101565101d8cSBoris Brezillon 
101665101d8cSBoris Brezillon /* vc4_perfmon.c */
101765101d8cSBoris Brezillon void vc4_perfmon_get(struct vc4_perfmon *perfmon);
101865101d8cSBoris Brezillon void vc4_perfmon_put(struct vc4_perfmon *perfmon);
101965101d8cSBoris Brezillon void vc4_perfmon_start(struct vc4_dev *vc4, struct vc4_perfmon *perfmon);
102065101d8cSBoris Brezillon void vc4_perfmon_stop(struct vc4_dev *vc4, struct vc4_perfmon *perfmon,
102165101d8cSBoris Brezillon 		      bool capture);
102265101d8cSBoris Brezillon struct vc4_perfmon *vc4_perfmon_find(struct vc4_file *vc4file, int id);
102365101d8cSBoris Brezillon void vc4_perfmon_open_file(struct vc4_file *vc4file);
102465101d8cSBoris Brezillon void vc4_perfmon_close_file(struct vc4_file *vc4file);
102565101d8cSBoris Brezillon int vc4_perfmon_create_ioctl(struct drm_device *dev, void *data,
102665101d8cSBoris Brezillon 			     struct drm_file *file_priv);
102765101d8cSBoris Brezillon int vc4_perfmon_destroy_ioctl(struct drm_device *dev, void *data,
102865101d8cSBoris Brezillon 			      struct drm_file *file_priv);
102965101d8cSBoris Brezillon int vc4_perfmon_get_values_ioctl(struct drm_device *dev, void *data,
103065101d8cSBoris Brezillon 				 struct drm_file *file_priv);
10316a88752cSMaxime Ripard 
10326a88752cSMaxime Ripard #endif /* _VC4_DRV_H_ */
1033