amdgpu_ring.h 15.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
/*
 * Copyright 2016 Advanced Micro Devices, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Authors: Christian König
 */
#ifndef __AMDGPU_RING_H__
#define __AMDGPU_RING_H__

27
#include <drm/amdgpu_drm.h>
28
#include <drm/gpu_scheduler.h>
29
#include <drm/drm_print.h>
30
#include <drm/drm_suballoc.h>
31

32 33 34 35 36 37 38
struct amdgpu_device;
struct amdgpu_ring;
struct amdgpu_ib;
struct amdgpu_cs_parser;
struct amdgpu_job;
struct amdgpu_vm;

39
/* max number of rings */
40
#define AMDGPU_MAX_RINGS		124
41
#define AMDGPU_MAX_HWIP_RINGS		64
42
#define AMDGPU_MAX_GFX_RINGS		2
43
#define AMDGPU_MAX_SW_GFX_RINGS         2
44 45
#define AMDGPU_MAX_COMPUTE_RINGS	8
#define AMDGPU_MAX_VCE_RINGS		3
Leo Liu's avatar
Leo Liu committed
46
#define AMDGPU_MAX_UVD_ENC_RINGS	2
47

48 49 50 51 52 53 54
enum amdgpu_ring_priority_level {
	AMDGPU_RING_PRIO_0,
	AMDGPU_RING_PRIO_1,
	AMDGPU_RING_PRIO_DEFAULT = 1,
	AMDGPU_RING_PRIO_2,
	AMDGPU_RING_PRIO_MAX
};
55

56
/* some special values for the owner field */
57 58 59
#define AMDGPU_FENCE_OWNER_UNDEFINED	((void *)0ul)
#define AMDGPU_FENCE_OWNER_VM		((void *)1ul)
#define AMDGPU_FENCE_OWNER_KFD		((void *)2ul)
60 61 62

#define AMDGPU_FENCE_FLAG_64BIT         (1 << 0)
#define AMDGPU_FENCE_FLAG_INT           (1 << 1)
63
#define AMDGPU_FENCE_FLAG_TC_WB_ONLY    (1 << 2)
64
#define AMDGPU_FENCE_FLAG_EXEC          (1 << 3)
65

66 67
#define to_amdgpu_ring(s) container_of((s), struct amdgpu_ring, sched)

68 69
#define AMDGPU_IB_POOL_SIZE	(1024 * 1024)

70
enum amdgpu_ring_type {
71 72 73 74 75 76 77 78 79
	AMDGPU_RING_TYPE_GFX		= AMDGPU_HW_IP_GFX,
	AMDGPU_RING_TYPE_COMPUTE	= AMDGPU_HW_IP_COMPUTE,
	AMDGPU_RING_TYPE_SDMA		= AMDGPU_HW_IP_DMA,
	AMDGPU_RING_TYPE_UVD		= AMDGPU_HW_IP_UVD,
	AMDGPU_RING_TYPE_VCE		= AMDGPU_HW_IP_VCE,
	AMDGPU_RING_TYPE_UVD_ENC	= AMDGPU_HW_IP_UVD_ENC,
	AMDGPU_RING_TYPE_VCN_DEC	= AMDGPU_HW_IP_VCN_DEC,
	AMDGPU_RING_TYPE_VCN_ENC	= AMDGPU_HW_IP_VCN_ENC,
	AMDGPU_RING_TYPE_VCN_JPEG	= AMDGPU_HW_IP_VCN_JPEG,
80 81
	AMDGPU_RING_TYPE_KIQ,
	AMDGPU_RING_TYPE_MES
82 83
};

84 85 86 87 88 89 90 91 92 93 94
enum amdgpu_ib_pool_type {
	/* Normal submissions to the top of the pipeline. */
	AMDGPU_IB_POOL_DELAYED,
	/* Immediate submissions to the bottom of the pipeline. */
	AMDGPU_IB_POOL_IMMEDIATE,
	/* Direct submission to the ring buffer during init and reset. */
	AMDGPU_IB_POOL_DIRECT,

	AMDGPU_IB_POOL_MAX
};

95
struct amdgpu_ib {
96
	struct drm_suballoc		*sa_bo;
97 98 99 100 101
	uint32_t			length_dw;
	uint64_t			gpu_addr;
	uint32_t			*ptr;
	uint32_t			flags;
};
102

103 104 105 106 107
struct amdgpu_sched {
	u32				num_scheds;
	struct drm_gpu_scheduler	*sched[AMDGPU_MAX_HWIP_RINGS];
};

108 109 110 111 112 113 114 115 116 117 118 119
/*
 * Fences.
 */
struct amdgpu_fence_driver {
	uint64_t			gpu_addr;
	volatile uint32_t		*cpu_addr;
	/* sync_seq is protected by ring emission lock */
	uint32_t			sync_seq;
	atomic_t			last_seq;
	bool				initialized;
	struct amdgpu_irq_src		*irq_src;
	unsigned			irq_type;
120
	struct timer_list		fallback_timer;
121 122
	unsigned			num_fences_mask;
	spinlock_t			lock;
123
	struct dma_fence		**fences;
124 125
};

126 127
extern const struct drm_sched_backend_ops amdgpu_sched_ops;

128
void amdgpu_fence_driver_clear_job_fences(struct amdgpu_ring *ring);
129
void amdgpu_fence_driver_force_completion(struct amdgpu_ring *ring);
130

131
int amdgpu_fence_driver_init_ring(struct amdgpu_ring *ring);
132 133 134
int amdgpu_fence_driver_start_ring(struct amdgpu_ring *ring,
				   struct amdgpu_irq_src *irq_src,
				   unsigned irq_type);
135
void amdgpu_fence_driver_hw_init(struct amdgpu_device *adev);
136
void amdgpu_fence_driver_hw_fini(struct amdgpu_device *adev);
137
int amdgpu_fence_driver_sw_init(struct amdgpu_device *adev);
138
void amdgpu_fence_driver_sw_fini(struct amdgpu_device *adev);
139
int amdgpu_fence_emit(struct amdgpu_ring *ring, struct dma_fence **fence, struct amdgpu_job *job,
140
		      unsigned flags);
141 142
int amdgpu_fence_emit_polling(struct amdgpu_ring *ring, uint32_t *s,
			      uint32_t timeout);
143
bool amdgpu_fence_process(struct amdgpu_ring *ring);
144
int amdgpu_fence_wait_empty(struct amdgpu_ring *ring);
145 146 147
signed long amdgpu_fence_wait_polling(struct amdgpu_ring *ring,
				      uint32_t wait_seq,
				      signed long timeout);
148
unsigned amdgpu_fence_count_emitted(struct amdgpu_ring *ring);
149

150
void amdgpu_fence_driver_isr_toggle(struct amdgpu_device *adev, bool stop);
151

152 153 154 155
u64 amdgpu_fence_last_unsignaled_time_us(struct amdgpu_ring *ring);
void amdgpu_fence_update_start_timestamp(struct amdgpu_ring *ring, uint32_t seq,
					 ktime_t timestamp);

156 157 158 159 160 161
/*
 * Rings.
 */

/* provided by hw blocks that expose a ring buffer for commands */
struct amdgpu_ring_funcs {
162
	enum amdgpu_ring_type	type;
163 164
	uint32_t		align_mask;
	u32			nop;
165
	bool			support_64bit_ptrs;
166
	bool			no_user_fence;
167
	bool			secure_submission_supported;
168
	unsigned		extra_dw;
169

170
	/* ring read/write ptr handling */
171 172
	u64 (*get_rptr)(struct amdgpu_ring *ring);
	u64 (*get_wptr)(struct amdgpu_ring *ring);
173 174
	void (*set_wptr)(struct amdgpu_ring *ring);
	/* validating and patching of IBs */
175 176 177 178 179 180
	int (*parse_cs)(struct amdgpu_cs_parser *p,
			struct amdgpu_job *job,
			struct amdgpu_ib *ib);
	int (*patch_cs_in_place)(struct amdgpu_cs_parser *p,
				 struct amdgpu_job *job,
				 struct amdgpu_ib *ib);
181 182 183
	/* constants to calculate how many DW are needed for an emit */
	unsigned emit_frame_size;
	unsigned emit_ib_size;
184 185
	/* command emit functions */
	void (*emit_ib)(struct amdgpu_ring *ring,
186
			struct amdgpu_job *job,
187
			struct amdgpu_ib *ib,
188
			uint32_t flags);
189 190 191
	void (*emit_fence)(struct amdgpu_ring *ring, uint64_t addr,
			   uint64_t seq, unsigned flags);
	void (*emit_pipeline_sync)(struct amdgpu_ring *ring);
192
	void (*emit_vm_flush)(struct amdgpu_ring *ring, unsigned vmid,
193
			      uint64_t pd_addr);
194 195 196 197 198 199 200 201 202 203
	void (*emit_hdp_flush)(struct amdgpu_ring *ring);
	void (*emit_gds_switch)(struct amdgpu_ring *ring, uint32_t vmid,
				uint32_t gds_base, uint32_t gds_size,
				uint32_t gws_base, uint32_t gws_size,
				uint32_t oa_base, uint32_t oa_size);
	/* testing functions */
	int (*test_ring)(struct amdgpu_ring *ring);
	int (*test_ib)(struct amdgpu_ring *ring, long timeout);
	/* insert NOP packets */
	void (*insert_nop)(struct amdgpu_ring *ring, uint32_t count);
204
	void (*insert_start)(struct amdgpu_ring *ring);
205
	void (*insert_end)(struct amdgpu_ring *ring);
206 207 208 209 210 211 212 213
	/* pad the indirect buffer to the necessary number of dw */
	void (*pad_ib)(struct amdgpu_ring *ring, struct amdgpu_ib *ib);
	unsigned (*init_cond_exec)(struct amdgpu_ring *ring);
	void (*patch_cond_exec)(struct amdgpu_ring *ring, unsigned offset);
	/* note usage for clock and power gating */
	void (*begin_use)(struct amdgpu_ring *ring);
	void (*end_use)(struct amdgpu_ring *ring);
	void (*emit_switch_buffer) (struct amdgpu_ring *ring);
214
	void (*emit_cntxcntl) (struct amdgpu_ring *ring, uint32_t flags);
215 216
	void (*emit_gfx_shadow)(struct amdgpu_ring *ring, u64 shadow_va, u64 csa_va,
				u64 gds_va, bool init_shadow, int vmid);
217 218
	void (*emit_rreg)(struct amdgpu_ring *ring, uint32_t reg,
			  uint32_t reg_val_offs);
219
	void (*emit_wreg)(struct amdgpu_ring *ring, uint32_t reg, uint32_t val);
220 221
	void (*emit_reg_wait)(struct amdgpu_ring *ring, uint32_t reg,
			      uint32_t val, uint32_t mask);
222 223 224
	void (*emit_reg_write_reg_wait)(struct amdgpu_ring *ring,
					uint32_t reg0, uint32_t reg1,
					uint32_t ref, uint32_t mask);
225 226
	void (*emit_frame_cntl)(struct amdgpu_ring *ring, bool start,
				bool secure);
227 228
	/* Try to soft recover the ring to make the fence signal */
	void (*soft_recovery)(struct amdgpu_ring *ring, unsigned vmid);
229
	int (*preempt_ib)(struct amdgpu_ring *ring);
230
	void (*emit_mem_sync)(struct amdgpu_ring *ring);
231
	void (*emit_wave_limit)(struct amdgpu_ring *ring, bool enable);
232 233 234 235 236 237
};

struct amdgpu_ring {
	struct amdgpu_device		*adev;
	const struct amdgpu_ring_funcs	*funcs;
	struct amdgpu_fence_driver	fence_drv;
238
	struct drm_gpu_scheduler	sched;
239 240 241 242

	struct amdgpu_bo	*ring_obj;
	volatile uint32_t	*ring;
	unsigned		rptr_offs;
243 244
	u64			rptr_gpu_addr;
	volatile u32		*rptr_cpu_addr;
245 246
	u64			wptr;
	u64			wptr_old;
247 248 249 250
	unsigned		ring_size;
	unsigned		max_dw;
	int			count_dw;
	uint64_t		gpu_addr;
251 252
	uint64_t		ptr_mask;
	uint32_t		buf_mask;
253
	u32			idx;
254
	u32			xcc_id;
255
	u32			xcp_id;
256 257 258 259
	u32			me;
	u32			pipe;
	u32			queue;
	struct amdgpu_bo	*mqd_obj;
260
	uint64_t                mqd_gpu_addr;
261
	void                    *mqd_ptr;
262
	unsigned                mqd_size;
263
	uint64_t                eop_gpu_addr;
264 265
	u32			doorbell_index;
	bool			use_doorbell;
266
	bool			use_pollmem;
267
	unsigned		wptr_offs;
268 269
	u64			wptr_gpu_addr;
	volatile u32		*wptr_cpu_addr;
270
	unsigned		fence_offs;
271 272
	u64			fence_gpu_addr;
	volatile u32		*fence_cpu_addr;
273 274
	uint64_t		current_ctx;
	char			name[16];
275 276 277 278
	u32                     trail_seq;
	unsigned		trail_fence_offs;
	u64			trail_fence_gpu_addr;
	volatile u32		*trail_fence_cpu_addr;
279 280 281
	unsigned		cond_exe_offs;
	u64			cond_exe_gpu_addr;
	volatile u32		*cond_exe_cpu_addr;
282
	unsigned		vm_hub;
283
	unsigned		vm_inv_eng;
284
	struct dma_fence	*vmid_wait;
285
	bool			has_compute_vm_bug;
286
	bool			no_scheduler;
287
	int			hw_prio;
288 289
	unsigned 		num_hw_submission;
	atomic_t		*sched_score;
290 291 292 293 294

	/* used for mes */
	bool			is_mes_queue;
	uint32_t		hw_queue_id;
	struct amdgpu_mes_ctx_data *mes_ctx;
295 296 297 298

	bool            is_sw_ring;
	unsigned int    entry_index;

299 300
};

301 302
#define amdgpu_ring_parse_cs(r, p, job, ib) ((r)->funcs->parse_cs((p), (job), (ib)))
#define amdgpu_ring_patch_cs_in_place(r, p, job, ib) ((r)->funcs->patch_cs_in_place((p), (job), (ib)))
303
#define amdgpu_ring_test_ring(r) (r)->funcs->test_ring((r))
304
#define amdgpu_ring_test_ib(r, t) ((r)->funcs->test_ib ? (r)->funcs->test_ib((r), (t)) : 0)
305 306 307
#define amdgpu_ring_get_rptr(r) (r)->funcs->get_rptr((r))
#define amdgpu_ring_get_wptr(r) (r)->funcs->get_wptr((r))
#define amdgpu_ring_set_wptr(r) (r)->funcs->set_wptr((r))
308
#define amdgpu_ring_emit_ib(r, job, ib, flags) ((r)->funcs->emit_ib((r), (job), (ib), (flags)))
309 310 311 312 313 314
#define amdgpu_ring_emit_pipeline_sync(r) (r)->funcs->emit_pipeline_sync((r))
#define amdgpu_ring_emit_vm_flush(r, vmid, addr) (r)->funcs->emit_vm_flush((r), (vmid), (addr))
#define amdgpu_ring_emit_fence(r, addr, seq, flags) (r)->funcs->emit_fence((r), (addr), (seq), (flags))
#define amdgpu_ring_emit_gds_switch(r, v, db, ds, wb, ws, ab, as) (r)->funcs->emit_gds_switch((r), (v), (db), (ds), (wb), (ws), (ab), (as))
#define amdgpu_ring_emit_hdp_flush(r) (r)->funcs->emit_hdp_flush((r))
#define amdgpu_ring_emit_switch_buffer(r) (r)->funcs->emit_switch_buffer((r))
315
#define amdgpu_ring_emit_cntxcntl(r, d) (r)->funcs->emit_cntxcntl((r), (d))
316
#define amdgpu_ring_emit_gfx_shadow(r, s, c, g, i, v) ((r)->funcs->emit_gfx_shadow((r), (s), (c), (g), (i), (v)))
317
#define amdgpu_ring_emit_rreg(r, d, o) (r)->funcs->emit_rreg((r), (d), (o))
318 319 320
#define amdgpu_ring_emit_wreg(r, d, v) (r)->funcs->emit_wreg((r), (d), (v))
#define amdgpu_ring_emit_reg_wait(r, d, v, m) (r)->funcs->emit_reg_wait((r), (d), (v), (m))
#define amdgpu_ring_emit_reg_write_reg_wait(r, d0, d1, v, m) (r)->funcs->emit_reg_write_reg_wait((r), (d0), (d1), (v), (m))
321
#define amdgpu_ring_emit_frame_cntl(r, b, s) (r)->funcs->emit_frame_cntl((r), (b), (s))
322 323 324
#define amdgpu_ring_pad_ib(r, ib) ((r)->funcs->pad_ib((r), (ib)))
#define amdgpu_ring_init_cond_exec(r) (r)->funcs->init_cond_exec((r))
#define amdgpu_ring_patch_cond_exec(r,o) (r)->funcs->patch_cond_exec((r),(o))
325
#define amdgpu_ring_preempt_ib(r) (r)->funcs->preempt_ib(r)
326

327
unsigned int amdgpu_ring_max_ibs(enum amdgpu_ring_type type);
328
int amdgpu_ring_alloc(struct amdgpu_ring *ring, unsigned ndw);
329 330 331
void amdgpu_ring_ib_begin(struct amdgpu_ring *ring);
void amdgpu_ring_ib_end(struct amdgpu_ring *ring);

332 333 334 335 336
void amdgpu_ring_insert_nop(struct amdgpu_ring *ring, uint32_t count);
void amdgpu_ring_generic_pad_ib(struct amdgpu_ring *ring, struct amdgpu_ib *ib);
void amdgpu_ring_commit(struct amdgpu_ring *ring);
void amdgpu_ring_undo(struct amdgpu_ring *ring);
int amdgpu_ring_init(struct amdgpu_device *adev, struct amdgpu_ring *ring,
337 338
		     unsigned int max_dw, struct amdgpu_irq_src *irq_src,
		     unsigned int irq_type, unsigned int hw_prio,
339
		     atomic_t *sched_score);
340
void amdgpu_ring_fini(struct amdgpu_ring *ring);
341 342 343
void amdgpu_ring_emit_reg_write_reg_wait_helper(struct amdgpu_ring *ring,
						uint32_t reg0, uint32_t val0,
						uint32_t reg1, uint32_t val1);
344 345
bool amdgpu_ring_soft_recovery(struct amdgpu_ring *ring, unsigned int vmid,
			       struct dma_fence *fence);
346

347 348 349 350 351 352
static inline void amdgpu_ring_set_preempt_cond_exec(struct amdgpu_ring *ring,
							bool cond_exec)
{
	*ring->cond_exe_cpu_addr = cond_exec;
}

Monk Liu's avatar
Monk Liu committed
353 354 355
static inline void amdgpu_ring_clear_ring(struct amdgpu_ring *ring)
{
	int i = 0;
356
	while (i <= ring->buf_mask)
Monk Liu's avatar
Monk Liu committed
357 358 359
		ring->ring[i++] = ring->funcs->nop;

}
360

361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
static inline void amdgpu_ring_write(struct amdgpu_ring *ring, uint32_t v)
{
	if (ring->count_dw <= 0)
		DRM_ERROR("amdgpu: writing more dwords to the ring than expected!\n");
	ring->ring[ring->wptr++ & ring->buf_mask] = v;
	ring->wptr &= ring->ptr_mask;
	ring->count_dw--;
}

static inline void amdgpu_ring_write_multiple(struct amdgpu_ring *ring,
					      void *src, int count_dw)
{
	unsigned occupied, chunk1, chunk2;
	void *dst;

376
	if (unlikely(ring->count_dw < count_dw))
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400
		DRM_ERROR("amdgpu: writing more dwords to the ring than expected!\n");

	occupied = ring->wptr & ring->buf_mask;
	dst = (void *)&ring->ring[occupied];
	chunk1 = ring->buf_mask + 1 - occupied;
	chunk1 = (chunk1 >= count_dw) ? count_dw: chunk1;
	chunk2 = count_dw - chunk1;
	chunk1 <<= 2;
	chunk2 <<= 2;

	if (chunk1)
		memcpy(dst, src, chunk1);

	if (chunk2) {
		src += chunk1;
		dst = (void *)ring->ring;
		memcpy(dst, src, chunk2);
	}

	ring->wptr += count_dw;
	ring->wptr &= ring->ptr_mask;
	ring->count_dw -= count_dw;
}

401 402 403 404 405 406 407 408 409
#define amdgpu_mes_ctx_get_offs_gpu_addr(ring, offset)			\
	(ring->is_mes_queue && ring->mes_ctx ?				\
	 (ring->mes_ctx->meta_data_gpu_addr + offset) : 0)

#define amdgpu_mes_ctx_get_offs_cpu_addr(ring, offset)			\
	(ring->is_mes_queue && ring->mes_ctx ?				\
	 (void *)((uint8_t *)(ring->mes_ctx->meta_data_ptr) + offset) : \
	 NULL)

410 411
int amdgpu_ring_test_helper(struct amdgpu_ring *ring);

412 413
void amdgpu_debugfs_ring_init(struct amdgpu_device *adev,
			      struct amdgpu_ring *ring);
414

415 416
int amdgpu_ring_init_mqd(struct amdgpu_ring *ring);

417 418 419 420 421 422 423 424 425 426 427
static inline u32 amdgpu_ib_get_value(struct amdgpu_ib *ib, int idx)
{
	return ib->ptr[idx];
}

static inline void amdgpu_ib_set_value(struct amdgpu_ib *ib, int idx,
				       uint32_t value)
{
	ib->ptr[idx] = value;
}

428 429 430 431 432 433 434 435 436 437 438 439 440
int amdgpu_ib_get(struct amdgpu_device *adev, struct amdgpu_vm *vm,
		  unsigned size,
		  enum amdgpu_ib_pool_type pool,
		  struct amdgpu_ib *ib);
void amdgpu_ib_free(struct amdgpu_device *adev, struct amdgpu_ib *ib,
		    struct dma_fence *f);
int amdgpu_ib_schedule(struct amdgpu_ring *ring, unsigned num_ibs,
		       struct amdgpu_ib *ibs, struct amdgpu_job *job,
		       struct dma_fence **f);
int amdgpu_ib_pool_init(struct amdgpu_device *adev);
void amdgpu_ib_pool_fini(struct amdgpu_device *adev);
int amdgpu_ib_ring_tests(struct amdgpu_device *adev);

441
#endif