xref: /dflybsd-src/sys/dev/drm/amd/amdgpu/amdgpu_ring.h (revision 789731325bde747251c28a37e0a00ed4efb88c46)
1b843c749SSergey Zigachev /*
2b843c749SSergey Zigachev  * Copyright 2016 Advanced Micro Devices, Inc.
3b843c749SSergey Zigachev  *
4b843c749SSergey Zigachev  * Permission is hereby granted, free of charge, to any person obtaining a
5b843c749SSergey Zigachev  * copy of this software and associated documentation files (the "Software"),
6b843c749SSergey Zigachev  * to deal in the Software without restriction, including without limitation
7b843c749SSergey Zigachev  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8b843c749SSergey Zigachev  * and/or sell copies of the Software, and to permit persons to whom the
9b843c749SSergey Zigachev  * Software is furnished to do so, subject to the following conditions:
10b843c749SSergey Zigachev  *
11b843c749SSergey Zigachev  * The above copyright notice and this permission notice shall be included in
12b843c749SSergey Zigachev  * all copies or substantial portions of the Software.
13b843c749SSergey Zigachev  *
14b843c749SSergey Zigachev  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15b843c749SSergey Zigachev  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16b843c749SSergey Zigachev  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17b843c749SSergey Zigachev  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18b843c749SSergey Zigachev  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19b843c749SSergey Zigachev  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20b843c749SSergey Zigachev  * OTHER DEALINGS IN THE SOFTWARE.
21b843c749SSergey Zigachev  *
22b843c749SSergey Zigachev  * Authors: Christian König
23b843c749SSergey Zigachev  */
24b843c749SSergey Zigachev #ifndef __AMDGPU_RING_H__
25b843c749SSergey Zigachev #define __AMDGPU_RING_H__
26b843c749SSergey Zigachev 
27b843c749SSergey Zigachev #include <drm/amdgpu_drm.h>
28b843c749SSergey Zigachev #include <drm/gpu_scheduler.h>
29b843c749SSergey Zigachev #include <drm/drm_print.h>
30b843c749SSergey Zigachev 
31b843c749SSergey Zigachev /* max number of rings */
32b843c749SSergey Zigachev #define AMDGPU_MAX_RINGS		21
33b843c749SSergey Zigachev #define AMDGPU_MAX_GFX_RINGS		1
34b843c749SSergey Zigachev #define AMDGPU_MAX_COMPUTE_RINGS	8
35b843c749SSergey Zigachev #define AMDGPU_MAX_VCE_RINGS		3
36b843c749SSergey Zigachev #define AMDGPU_MAX_UVD_ENC_RINGS	2
37b843c749SSergey Zigachev 
38b843c749SSergey Zigachev /* some special values for the owner field */
39b843c749SSergey Zigachev #define AMDGPU_FENCE_OWNER_UNDEFINED	((void *)0ul)
40b843c749SSergey Zigachev #define AMDGPU_FENCE_OWNER_VM		((void *)1ul)
41b843c749SSergey Zigachev #define AMDGPU_FENCE_OWNER_KFD		((void *)2ul)
42b843c749SSergey Zigachev 
43b843c749SSergey Zigachev #define AMDGPU_FENCE_FLAG_64BIT         (1 << 0)
44b843c749SSergey Zigachev #define AMDGPU_FENCE_FLAG_INT           (1 << 1)
45b843c749SSergey Zigachev #define AMDGPU_FENCE_FLAG_TC_WB_ONLY    (1 << 2)
46b843c749SSergey Zigachev 
47b843c749SSergey Zigachev #define to_amdgpu_ring(s) container_of((s), struct amdgpu_ring, sched)
48b843c749SSergey Zigachev 
49b843c749SSergey Zigachev enum amdgpu_ring_type {
50b843c749SSergey Zigachev 	AMDGPU_RING_TYPE_GFX,
51b843c749SSergey Zigachev 	AMDGPU_RING_TYPE_COMPUTE,
52b843c749SSergey Zigachev 	AMDGPU_RING_TYPE_SDMA,
53b843c749SSergey Zigachev 	AMDGPU_RING_TYPE_UVD,
54b843c749SSergey Zigachev 	AMDGPU_RING_TYPE_VCE,
55b843c749SSergey Zigachev 	AMDGPU_RING_TYPE_KIQ,
56b843c749SSergey Zigachev 	AMDGPU_RING_TYPE_UVD_ENC,
57b843c749SSergey Zigachev 	AMDGPU_RING_TYPE_VCN_DEC,
58b843c749SSergey Zigachev 	AMDGPU_RING_TYPE_VCN_ENC,
59b843c749SSergey Zigachev 	AMDGPU_RING_TYPE_VCN_JPEG
60b843c749SSergey Zigachev };
61b843c749SSergey Zigachev 
62b843c749SSergey Zigachev struct amdgpu_device;
63b843c749SSergey Zigachev struct amdgpu_ring;
64b843c749SSergey Zigachev struct amdgpu_ib;
65b843c749SSergey Zigachev struct amdgpu_cs_parser;
66b843c749SSergey Zigachev struct amdgpu_job;
67b843c749SSergey Zigachev 
68b843c749SSergey Zigachev /*
69b843c749SSergey Zigachev  * Fences.
70b843c749SSergey Zigachev  */
71b843c749SSergey Zigachev struct amdgpu_fence_driver {
72b843c749SSergey Zigachev 	uint64_t			gpu_addr;
73b843c749SSergey Zigachev 	volatile uint32_t		*cpu_addr;
74b843c749SSergey Zigachev 	/* sync_seq is protected by ring emission lock */
75b843c749SSergey Zigachev 	uint32_t			sync_seq;
76b843c749SSergey Zigachev 	atomic_t			last_seq;
77b843c749SSergey Zigachev 	bool				initialized;
78b843c749SSergey Zigachev 	struct amdgpu_irq_src		*irq_src;
79b843c749SSergey Zigachev 	unsigned			irq_type;
80b843c749SSergey Zigachev 	struct timer_list		fallback_timer;
81b843c749SSergey Zigachev 	unsigned			num_fences_mask;
82b843c749SSergey Zigachev 	spinlock_t			lock;
83b843c749SSergey Zigachev 	struct dma_fence		**fences;
84b843c749SSergey Zigachev };
85b843c749SSergey Zigachev 
86b843c749SSergey Zigachev int amdgpu_fence_driver_init(struct amdgpu_device *adev);
87b843c749SSergey Zigachev void amdgpu_fence_driver_fini(struct amdgpu_device *adev);
88b843c749SSergey Zigachev void amdgpu_fence_driver_force_completion(struct amdgpu_ring *ring);
89b843c749SSergey Zigachev 
90b843c749SSergey Zigachev int amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring,
91b843c749SSergey Zigachev 				  unsigned num_hw_submission);
92b843c749SSergey Zigachev int amdgpu_fence_driver_start_ring(struct amdgpu_ring *ring,
93b843c749SSergey Zigachev 				   struct amdgpu_irq_src *irq_src,
94b843c749SSergey Zigachev 				   unsigned irq_type);
95b843c749SSergey Zigachev void amdgpu_fence_driver_suspend(struct amdgpu_device *adev);
96b843c749SSergey Zigachev void amdgpu_fence_driver_resume(struct amdgpu_device *adev);
97b843c749SSergey Zigachev int amdgpu_fence_emit(struct amdgpu_ring *ring, struct dma_fence **fence,
98b843c749SSergey Zigachev 		      unsigned flags);
99b843c749SSergey Zigachev int amdgpu_fence_emit_polling(struct amdgpu_ring *ring, uint32_t *s);
100b843c749SSergey Zigachev void amdgpu_fence_process(struct amdgpu_ring *ring);
101b843c749SSergey Zigachev int amdgpu_fence_wait_empty(struct amdgpu_ring *ring);
102b843c749SSergey Zigachev signed long amdgpu_fence_wait_polling(struct amdgpu_ring *ring,
103b843c749SSergey Zigachev 				      uint32_t wait_seq,
104b843c749SSergey Zigachev 				      signed long timeout);
105b843c749SSergey Zigachev unsigned amdgpu_fence_count_emitted(struct amdgpu_ring *ring);
106b843c749SSergey Zigachev 
107b843c749SSergey Zigachev /*
108b843c749SSergey Zigachev  * Rings.
109b843c749SSergey Zigachev  */
110b843c749SSergey Zigachev 
111b843c749SSergey Zigachev /* provided by hw blocks that expose a ring buffer for commands */
112b843c749SSergey Zigachev struct amdgpu_ring_funcs {
113b843c749SSergey Zigachev 	enum amdgpu_ring_type	type;
114b843c749SSergey Zigachev 	uint32_t		align_mask;
115b843c749SSergey Zigachev 	u32			nop;
116b843c749SSergey Zigachev 	bool			support_64bit_ptrs;
117b843c749SSergey Zigachev 	unsigned		vmhub;
118b843c749SSergey Zigachev 	unsigned		extra_dw;
119b843c749SSergey Zigachev 
120b843c749SSergey Zigachev 	/* ring read/write ptr handling */
121*78973132SSergey Zigachev 	uint64_t (*get_rptr)(struct amdgpu_ring *ring);
122*78973132SSergey Zigachev 	uint64_t (*get_wptr)(struct amdgpu_ring *ring);
123b843c749SSergey Zigachev 	void (*set_wptr)(struct amdgpu_ring *ring);
124b843c749SSergey Zigachev 	/* validating and patching of IBs */
125b843c749SSergey Zigachev 	int (*parse_cs)(struct amdgpu_cs_parser *p, uint32_t ib_idx);
126b843c749SSergey Zigachev 	int (*patch_cs_in_place)(struct amdgpu_cs_parser *p, uint32_t ib_idx);
127b843c749SSergey Zigachev 	/* constants to calculate how many DW are needed for an emit */
128b843c749SSergey Zigachev 	unsigned emit_frame_size;
129b843c749SSergey Zigachev 	unsigned emit_ib_size;
130b843c749SSergey Zigachev 	/* command emit functions */
131b843c749SSergey Zigachev 	void (*emit_ib)(struct amdgpu_ring *ring,
132b843c749SSergey Zigachev 			struct amdgpu_ib *ib,
133b843c749SSergey Zigachev 			unsigned vmid, bool ctx_switch);
134b843c749SSergey Zigachev 	void (*emit_fence)(struct amdgpu_ring *ring, uint64_t addr,
135b843c749SSergey Zigachev 			   uint64_t seq, unsigned flags);
136b843c749SSergey Zigachev 	void (*emit_pipeline_sync)(struct amdgpu_ring *ring);
137b843c749SSergey Zigachev 	void (*emit_vm_flush)(struct amdgpu_ring *ring, unsigned vmid,
138b843c749SSergey Zigachev 			      uint64_t pd_addr);
139b843c749SSergey Zigachev 	void (*emit_hdp_flush)(struct amdgpu_ring *ring);
140b843c749SSergey Zigachev 	void (*emit_gds_switch)(struct amdgpu_ring *ring, uint32_t vmid,
141b843c749SSergey Zigachev 				uint32_t gds_base, uint32_t gds_size,
142b843c749SSergey Zigachev 				uint32_t gws_base, uint32_t gws_size,
143b843c749SSergey Zigachev 				uint32_t oa_base, uint32_t oa_size);
144b843c749SSergey Zigachev 	/* testing functions */
145b843c749SSergey Zigachev 	int (*test_ring)(struct amdgpu_ring *ring);
146b843c749SSergey Zigachev 	int (*test_ib)(struct amdgpu_ring *ring, long timeout);
147b843c749SSergey Zigachev 	/* insert NOP packets */
148b843c749SSergey Zigachev 	void (*insert_nop)(struct amdgpu_ring *ring, uint32_t count);
149b843c749SSergey Zigachev 	void (*insert_start)(struct amdgpu_ring *ring);
150b843c749SSergey Zigachev 	void (*insert_end)(struct amdgpu_ring *ring);
151b843c749SSergey Zigachev 	/* pad the indirect buffer to the necessary number of dw */
152b843c749SSergey Zigachev 	void (*pad_ib)(struct amdgpu_ring *ring, struct amdgpu_ib *ib);
153b843c749SSergey Zigachev 	unsigned (*init_cond_exec)(struct amdgpu_ring *ring);
154b843c749SSergey Zigachev 	void (*patch_cond_exec)(struct amdgpu_ring *ring, unsigned offset);
155b843c749SSergey Zigachev 	/* note usage for clock and power gating */
156b843c749SSergey Zigachev 	void (*begin_use)(struct amdgpu_ring *ring);
157b843c749SSergey Zigachev 	void (*end_use)(struct amdgpu_ring *ring);
158b843c749SSergey Zigachev 	void (*emit_switch_buffer) (struct amdgpu_ring *ring);
159b843c749SSergey Zigachev 	void (*emit_cntxcntl) (struct amdgpu_ring *ring, uint32_t flags);
160b843c749SSergey Zigachev 	void (*emit_rreg)(struct amdgpu_ring *ring, uint32_t reg);
161b843c749SSergey Zigachev 	void (*emit_wreg)(struct amdgpu_ring *ring, uint32_t reg, uint32_t val);
162b843c749SSergey Zigachev 	void (*emit_reg_wait)(struct amdgpu_ring *ring, uint32_t reg,
163b843c749SSergey Zigachev 			      uint32_t val, uint32_t mask);
164b843c749SSergey Zigachev 	void (*emit_reg_write_reg_wait)(struct amdgpu_ring *ring,
165b843c749SSergey Zigachev 					uint32_t reg0, uint32_t reg1,
166b843c749SSergey Zigachev 					uint32_t ref, uint32_t mask);
167b843c749SSergey Zigachev 	void (*emit_tmz)(struct amdgpu_ring *ring, bool start);
168b843c749SSergey Zigachev 	/* priority functions */
169b843c749SSergey Zigachev 	void (*set_priority) (struct amdgpu_ring *ring,
170b843c749SSergey Zigachev 			      enum drm_sched_priority priority);
171b843c749SSergey Zigachev };
172b843c749SSergey Zigachev 
173b843c749SSergey Zigachev struct amdgpu_ring {
174b843c749SSergey Zigachev 	struct amdgpu_device		*adev;
175b843c749SSergey Zigachev 	const struct amdgpu_ring_funcs	*funcs;
176b843c749SSergey Zigachev 	struct amdgpu_fence_driver	fence_drv;
177b843c749SSergey Zigachev 	struct drm_gpu_scheduler	sched;
178b843c749SSergey Zigachev 	struct list_head		lru_list;
179b843c749SSergey Zigachev 
180b843c749SSergey Zigachev 	struct amdgpu_bo	*ring_obj;
181b843c749SSergey Zigachev 	volatile uint32_t	*ring;
182b843c749SSergey Zigachev 	unsigned		rptr_offs;
183b843c749SSergey Zigachev 	u64			wptr;
184b843c749SSergey Zigachev 	u64			wptr_old;
185b843c749SSergey Zigachev 	unsigned		ring_size;
186b843c749SSergey Zigachev 	unsigned		max_dw;
187b843c749SSergey Zigachev 	int			count_dw;
188b843c749SSergey Zigachev 	uint64_t		gpu_addr;
189b843c749SSergey Zigachev 	uint64_t		ptr_mask;
190b843c749SSergey Zigachev 	uint32_t		buf_mask;
191b843c749SSergey Zigachev 	bool			ready;
192b843c749SSergey Zigachev 	u32			idx;
193b843c749SSergey Zigachev 	u32			me;
194b843c749SSergey Zigachev 	u32			pipe;
195b843c749SSergey Zigachev 	u32			queue;
196b843c749SSergey Zigachev 	struct amdgpu_bo	*mqd_obj;
197b843c749SSergey Zigachev 	uint64_t                mqd_gpu_addr;
198b843c749SSergey Zigachev 	void                    *mqd_ptr;
199b843c749SSergey Zigachev 	uint64_t                eop_gpu_addr;
200b843c749SSergey Zigachev 	u32			doorbell_index;
201b843c749SSergey Zigachev 	bool			use_doorbell;
202b843c749SSergey Zigachev 	bool			use_pollmem;
203b843c749SSergey Zigachev 	unsigned		wptr_offs;
204b843c749SSergey Zigachev 	unsigned		fence_offs;
205b843c749SSergey Zigachev 	uint64_t		current_ctx;
206b843c749SSergey Zigachev 	char			name[16];
207b843c749SSergey Zigachev 	unsigned		cond_exe_offs;
208b843c749SSergey Zigachev 	u64			cond_exe_gpu_addr;
209b843c749SSergey Zigachev 	volatile u32		*cond_exe_cpu_addr;
210b843c749SSergey Zigachev 	unsigned		vm_inv_eng;
211b843c749SSergey Zigachev 	struct dma_fence	*vmid_wait;
212b843c749SSergey Zigachev 	bool			has_compute_vm_bug;
213b843c749SSergey Zigachev 
214b843c749SSergey Zigachev 	atomic_t		num_jobs[DRM_SCHED_PRIORITY_MAX];
215*78973132SSergey Zigachev 	struct lock		priority_mutex;
216b843c749SSergey Zigachev 	/* protected by priority_mutex */
217b843c749SSergey Zigachev 	int			priority;
218b843c749SSergey Zigachev 
219b843c749SSergey Zigachev #if defined(CONFIG_DEBUG_FS)
220b843c749SSergey Zigachev 	struct dentry *ent;
221b843c749SSergey Zigachev #endif
222b843c749SSergey Zigachev };
223b843c749SSergey Zigachev 
224b843c749SSergey Zigachev int amdgpu_ring_alloc(struct amdgpu_ring *ring, unsigned ndw);
225b843c749SSergey Zigachev void amdgpu_ring_insert_nop(struct amdgpu_ring *ring, uint32_t count);
226b843c749SSergey Zigachev void amdgpu_ring_generic_pad_ib(struct amdgpu_ring *ring, struct amdgpu_ib *ib);
227b843c749SSergey Zigachev void amdgpu_ring_commit(struct amdgpu_ring *ring);
228b843c749SSergey Zigachev void amdgpu_ring_undo(struct amdgpu_ring *ring);
229b843c749SSergey Zigachev void amdgpu_ring_priority_get(struct amdgpu_ring *ring,
230b843c749SSergey Zigachev 			      enum drm_sched_priority priority);
231b843c749SSergey Zigachev void amdgpu_ring_priority_put(struct amdgpu_ring *ring,
232b843c749SSergey Zigachev 			      enum drm_sched_priority priority);
233b843c749SSergey Zigachev int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
234b843c749SSergey Zigachev 		     unsigned ring_size, struct amdgpu_irq_src *irq_src,
235b843c749SSergey Zigachev 		     unsigned irq_type);
236b843c749SSergey Zigachev void amdgpu_ring_fini(struct amdgpu_ring *ring);
237b843c749SSergey Zigachev int amdgpu_ring_lru_get(struct amdgpu_device *adev, int type,
238b843c749SSergey Zigachev 			int *blacklist, int num_blacklist,
239b843c749SSergey Zigachev 			bool lru_pipe_order, struct amdgpu_ring **ring);
240b843c749SSergey Zigachev void amdgpu_ring_lru_touch(struct amdgpu_device *adev, struct amdgpu_ring *ring);
241b843c749SSergey Zigachev void amdgpu_ring_emit_reg_write_reg_wait_helper(struct amdgpu_ring *ring,
242b843c749SSergey Zigachev 						uint32_t reg0, uint32_t val0,
243b843c749SSergey Zigachev 						uint32_t reg1, uint32_t val1);
244b843c749SSergey Zigachev 
amdgpu_ring_clear_ring(struct amdgpu_ring * ring)245b843c749SSergey Zigachev static inline void amdgpu_ring_clear_ring(struct amdgpu_ring *ring)
246b843c749SSergey Zigachev {
247b843c749SSergey Zigachev 	int i = 0;
248b843c749SSergey Zigachev 	while (i <= ring->buf_mask)
249b843c749SSergey Zigachev 		ring->ring[i++] = ring->funcs->nop;
250b843c749SSergey Zigachev 
251b843c749SSergey Zigachev }
252b843c749SSergey Zigachev 
amdgpu_ring_write(struct amdgpu_ring * ring,uint32_t v)253b843c749SSergey Zigachev static inline void amdgpu_ring_write(struct amdgpu_ring *ring, uint32_t v)
254b843c749SSergey Zigachev {
255b843c749SSergey Zigachev 	if (ring->count_dw <= 0)
256b843c749SSergey Zigachev 		DRM_ERROR("amdgpu: writing more dwords to the ring than expected!\n");
257b843c749SSergey Zigachev 	ring->ring[ring->wptr++ & ring->buf_mask] = v;
258b843c749SSergey Zigachev 	ring->wptr &= ring->ptr_mask;
259b843c749SSergey Zigachev 	ring->count_dw--;
260b843c749SSergey Zigachev }
261b843c749SSergey Zigachev 
amdgpu_ring_write_multiple(struct amdgpu_ring * ring,void * src,int count_dw)262b843c749SSergey Zigachev static inline void amdgpu_ring_write_multiple(struct amdgpu_ring *ring,
263b843c749SSergey Zigachev 					      void *src, int count_dw)
264b843c749SSergey Zigachev {
265b843c749SSergey Zigachev 	unsigned occupied, chunk1, chunk2;
266b843c749SSergey Zigachev 	void *dst;
267b843c749SSergey Zigachev 
268b843c749SSergey Zigachev 	if (unlikely(ring->count_dw < count_dw))
269b843c749SSergey Zigachev 		DRM_ERROR("amdgpu: writing more dwords to the ring than expected!\n");
270b843c749SSergey Zigachev 
271b843c749SSergey Zigachev 	occupied = ring->wptr & ring->buf_mask;
272b843c749SSergey Zigachev 	dst = (void *)&ring->ring[occupied];
273b843c749SSergey Zigachev 	chunk1 = ring->buf_mask + 1 - occupied;
274b843c749SSergey Zigachev 	chunk1 = (chunk1 >= count_dw) ? count_dw: chunk1;
275b843c749SSergey Zigachev 	chunk2 = count_dw - chunk1;
276b843c749SSergey Zigachev 	chunk1 <<= 2;
277b843c749SSergey Zigachev 	chunk2 <<= 2;
278b843c749SSergey Zigachev 
279b843c749SSergey Zigachev 	if (chunk1)
280b843c749SSergey Zigachev 		memcpy(dst, src, chunk1);
281b843c749SSergey Zigachev 
282b843c749SSergey Zigachev 	if (chunk2) {
283b843c749SSergey Zigachev 		src += chunk1;
284b843c749SSergey Zigachev 		dst = (void *)ring->ring;
285b843c749SSergey Zigachev 		memcpy(dst, src, chunk2);
286b843c749SSergey Zigachev 	}
287b843c749SSergey Zigachev 
288b843c749SSergey Zigachev 	ring->wptr += count_dw;
289b843c749SSergey Zigachev 	ring->wptr &= ring->ptr_mask;
290b843c749SSergey Zigachev 	ring->count_dw -= count_dw;
291b843c749SSergey Zigachev }
292b843c749SSergey Zigachev 
293b843c749SSergey Zigachev #endif
294