Linux Audio

Check our new training course

Open-source upstreaming

Need help get the support for your hardware in upstream Linux?
Loading...
v6.2
  1/*
  2 * Copyright 2014 Advanced Micro Devices, Inc.
  3 *
  4 * Permission is hereby granted, free of charge, to any person obtaining a
  5 * copy of this software and associated documentation files (the "Software"),
  6 * to deal in the Software without restriction, including without limitation
  7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8 * and/or sell copies of the Software, and to permit persons to whom the
  9 * Software is furnished to do so, subject to the following conditions:
 10 *
 11 * The above copyright notice and this permission notice shall be included in
 12 * all copies or substantial portions of the Software.
 13 *
 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 20 * OTHER DEALINGS IN THE SOFTWARE.
 21 */
 22
 
 
 
 
 
 23#include "amdgpu.h"
 24#include "amdgpu_amdkfd.h"
 25#include "gfx_v8_0.h"
 26#include "gca/gfx_8_0_sh_mask.h"
 27#include "gca/gfx_8_0_d.h"
 28#include "gca/gfx_8_0_enum.h"
 29#include "oss/oss_3_0_sh_mask.h"
 30#include "oss/oss_3_0_d.h"
 31#include "gmc/gmc_8_1_sh_mask.h"
 32#include "gmc/gmc_8_1_d.h"
 33#include "vi_structs.h"
 34#include "vid.h"
 35
 36enum hqd_dequeue_request_type {
 37	NO_ACTION = 0,
 38	DRAIN_PIPE,
 39	RESET_WAVES
 40};
 41
 42static void lock_srbm(struct amdgpu_device *adev, uint32_t mec, uint32_t pipe,
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 43			uint32_t queue, uint32_t vmid)
 44{
 
 45	uint32_t value = PIPEID(pipe) | MEID(mec) | VMID(vmid) | QUEUEID(queue);
 46
 47	mutex_lock(&adev->srbm_mutex);
 48	WREG32(mmSRBM_GFX_CNTL, value);
 49}
 50
 51static void unlock_srbm(struct amdgpu_device *adev)
 52{
 
 
 53	WREG32(mmSRBM_GFX_CNTL, 0);
 54	mutex_unlock(&adev->srbm_mutex);
 55}
 56
 57static void acquire_queue(struct amdgpu_device *adev, uint32_t pipe_id,
 58				uint32_t queue_id)
 59{
 
 
 60	uint32_t mec = (pipe_id / adev->gfx.mec.num_pipe_per_mec) + 1;
 61	uint32_t pipe = (pipe_id % adev->gfx.mec.num_pipe_per_mec);
 62
 63	lock_srbm(adev, mec, pipe, queue_id, 0);
 64}
 65
 66static void release_queue(struct amdgpu_device *adev)
 67{
 68	unlock_srbm(adev);
 69}
 70
 71static void kgd_program_sh_mem_settings(struct amdgpu_device *adev, uint32_t vmid,
 72					uint32_t sh_mem_config,
 73					uint32_t sh_mem_ape1_base,
 74					uint32_t sh_mem_ape1_limit,
 75					uint32_t sh_mem_bases)
 76{
 77	lock_srbm(adev, 0, 0, 0, vmid);
 
 
 78
 79	WREG32(mmSH_MEM_CONFIG, sh_mem_config);
 80	WREG32(mmSH_MEM_APE1_BASE, sh_mem_ape1_base);
 81	WREG32(mmSH_MEM_APE1_LIMIT, sh_mem_ape1_limit);
 82	WREG32(mmSH_MEM_BASES, sh_mem_bases);
 83
 84	unlock_srbm(adev);
 85}
 86
 87static int kgd_set_pasid_vmid_mapping(struct amdgpu_device *adev, u32 pasid,
 88					unsigned int vmid)
 89{
 
 
 90	/*
 91	 * We have to assume that there is no outstanding mapping.
 92	 * The ATC_VMID_PASID_MAPPING_UPDATE_STATUS bit could be 0 because
 93	 * a mapping is in progress or because a mapping finished
 94	 * and the SW cleared it.
 95	 * So the protocol is to always wait & clear.
 96	 */
 97	uint32_t pasid_mapping = (pasid == 0) ? 0 : (uint32_t)pasid |
 98			ATC_VMID0_PASID_MAPPING__VALID_MASK;
 99
100	WREG32(mmATC_VMID0_PASID_MAPPING + vmid, pasid_mapping);
101
102	while (!(RREG32(mmATC_VMID_PASID_MAPPING_UPDATE_STATUS) & (1U << vmid)))
103		cpu_relax();
104	WREG32(mmATC_VMID_PASID_MAPPING_UPDATE_STATUS, 1U << vmid);
105
106	/* Mapping vmid to pasid also for IH block */
107	WREG32(mmIH_VMID_0_LUT + vmid, pasid_mapping);
108
109	return 0;
110}
111
112static int kgd_init_interrupts(struct amdgpu_device *adev, uint32_t pipe_id)
113{
 
114	uint32_t mec;
115	uint32_t pipe;
116
117	mec = (pipe_id / adev->gfx.mec.num_pipe_per_mec) + 1;
118	pipe = (pipe_id % adev->gfx.mec.num_pipe_per_mec);
119
120	lock_srbm(adev, mec, pipe, 0, 0);
121
122	WREG32(mmCPC_INT_CNTL, CP_INT_CNTL_RING0__TIME_STAMP_INT_ENABLE_MASK |
123			CP_INT_CNTL_RING0__OPCODE_ERROR_INT_ENABLE_MASK);
124
125	unlock_srbm(adev);
126
127	return 0;
128}
129
130static inline uint32_t get_sdma_rlc_reg_offset(struct vi_sdma_mqd *m)
131{
132	uint32_t retval;
133
134	retval = m->sdma_engine_id * SDMA1_REGISTER_OFFSET +
135		m->sdma_queue_id * KFD_VI_SDMA_QUEUE_OFFSET;
136
137	pr_debug("RLC register offset for SDMA%d RLC%d: 0x%x\n",
138			m->sdma_engine_id, m->sdma_queue_id, retval);
139
140	return retval;
141}
142
143static inline struct vi_mqd *get_mqd(void *mqd)
144{
145	return (struct vi_mqd *)mqd;
146}
147
148static inline struct vi_sdma_mqd *get_sdma_mqd(void *mqd)
149{
150	return (struct vi_sdma_mqd *)mqd;
151}
152
153static int kgd_hqd_load(struct amdgpu_device *adev, void *mqd,
154			uint32_t pipe_id, uint32_t queue_id,
155			uint32_t __user *wptr, uint32_t wptr_shift,
156			uint32_t wptr_mask, struct mm_struct *mm)
157{
 
158	struct vi_mqd *m;
159	uint32_t *mqd_hqd;
160	uint32_t reg, wptr_val, data;
161	bool valid_wptr = false;
162
163	m = get_mqd(mqd);
164
165	acquire_queue(adev, pipe_id, queue_id);
166
167	/* HIQ is set during driver init period with vmid set to 0*/
168	if (m->cp_hqd_vmid == 0) {
169		uint32_t value, mec, pipe;
170
171		mec = (pipe_id / adev->gfx.mec.num_pipe_per_mec) + 1;
172		pipe = (pipe_id % adev->gfx.mec.num_pipe_per_mec);
173
174		pr_debug("kfd: set HIQ, mec:%d, pipe:%d, queue:%d.\n",
175			mec, pipe, queue_id);
176		value = RREG32(mmRLC_CP_SCHEDULERS);
177		value = REG_SET_FIELD(value, RLC_CP_SCHEDULERS, scheduler1,
178			((mec << 5) | (pipe << 3) | queue_id | 0x80));
179		WREG32(mmRLC_CP_SCHEDULERS, value);
180	}
181
182	/* HQD registers extend from CP_MQD_BASE_ADDR to CP_HQD_EOP_WPTR_MEM. */
183	mqd_hqd = &m->cp_mqd_base_addr_lo;
184
185	for (reg = mmCP_MQD_BASE_ADDR; reg <= mmCP_HQD_EOP_CONTROL; reg++)
186		WREG32(reg, mqd_hqd[reg - mmCP_MQD_BASE_ADDR]);
187
188	/* Tonga errata: EOP RPTR/WPTR should be left unmodified.
189	 * This is safe since EOP RPTR==WPTR for any inactive HQD
190	 * on ASICs that do not support context-save.
191	 * EOP writes/reads can start anywhere in the ring.
192	 */
193	if (adev->asic_type != CHIP_TONGA) {
194		WREG32(mmCP_HQD_EOP_RPTR, m->cp_hqd_eop_rptr);
195		WREG32(mmCP_HQD_EOP_WPTR, m->cp_hqd_eop_wptr);
196		WREG32(mmCP_HQD_EOP_WPTR_MEM, m->cp_hqd_eop_wptr_mem);
197	}
198
199	for (reg = mmCP_HQD_EOP_EVENTS; reg <= mmCP_HQD_ERROR; reg++)
200		WREG32(reg, mqd_hqd[reg - mmCP_MQD_BASE_ADDR]);
201
202	/* Copy userspace write pointer value to register.
203	 * Activate doorbell logic to monitor subsequent changes.
204	 */
205	data = REG_SET_FIELD(m->cp_hqd_pq_doorbell_control,
206			     CP_HQD_PQ_DOORBELL_CONTROL, DOORBELL_EN, 1);
207	WREG32(mmCP_HQD_PQ_DOORBELL_CONTROL, data);
208
209	/* read_user_ptr may take the mm->mmap_lock.
210	 * release srbm_mutex to avoid circular dependency between
211	 * srbm_mutex->mmap_lock->reservation_ww_class_mutex->srbm_mutex.
212	 */
213	release_queue(adev);
214	valid_wptr = read_user_wptr(mm, wptr, wptr_val);
215	acquire_queue(adev, pipe_id, queue_id);
216	if (valid_wptr)
217		WREG32(mmCP_HQD_PQ_WPTR, (wptr_val << wptr_shift) & wptr_mask);
218
219	data = REG_SET_FIELD(m->cp_hqd_active, CP_HQD_ACTIVE, ACTIVE, 1);
220	WREG32(mmCP_HQD_ACTIVE, data);
221
222	release_queue(adev);
223
224	return 0;
225}
226
227static int kgd_hqd_dump(struct amdgpu_device *adev,
228			uint32_t pipe_id, uint32_t queue_id,
229			uint32_t (**dump)[2], uint32_t *n_regs)
230{
 
231	uint32_t i = 0, reg;
232#define HQD_N_REGS (54+4)
233#define DUMP_REG(addr) do {				\
234		if (WARN_ON_ONCE(i >= HQD_N_REGS))	\
235			break;				\
236		(*dump)[i][0] = (addr) << 2;		\
237		(*dump)[i++][1] = RREG32(addr);		\
238	} while (0)
239
240	*dump = kmalloc_array(HQD_N_REGS * 2, sizeof(uint32_t), GFP_KERNEL);
241	if (*dump == NULL)
242		return -ENOMEM;
243
244	acquire_queue(adev, pipe_id, queue_id);
245
246	DUMP_REG(mmCOMPUTE_STATIC_THREAD_MGMT_SE0);
247	DUMP_REG(mmCOMPUTE_STATIC_THREAD_MGMT_SE1);
248	DUMP_REG(mmCOMPUTE_STATIC_THREAD_MGMT_SE2);
249	DUMP_REG(mmCOMPUTE_STATIC_THREAD_MGMT_SE3);
250
251	for (reg = mmCP_MQD_BASE_ADDR; reg <= mmCP_HQD_EOP_DONES; reg++)
252		DUMP_REG(reg);
253
254	release_queue(adev);
255
256	WARN_ON_ONCE(i != HQD_N_REGS);
257	*n_regs = i;
258
259	return 0;
260}
261
262static int kgd_hqd_sdma_load(struct amdgpu_device *adev, void *mqd,
263			     uint32_t __user *wptr, struct mm_struct *mm)
264{
 
265	struct vi_sdma_mqd *m;
266	unsigned long end_jiffies;
267	uint32_t sdma_rlc_reg_offset;
268	uint32_t data;
269
270	m = get_sdma_mqd(mqd);
271	sdma_rlc_reg_offset = get_sdma_rlc_reg_offset(m);
272	WREG32(sdma_rlc_reg_offset + mmSDMA0_RLC0_RB_CNTL,
273		m->sdmax_rlcx_rb_cntl & (~SDMA0_RLC0_RB_CNTL__RB_ENABLE_MASK));
274
275	end_jiffies = msecs_to_jiffies(2000) + jiffies;
276	while (true) {
277		data = RREG32(sdma_rlc_reg_offset + mmSDMA0_RLC0_CONTEXT_STATUS);
278		if (data & SDMA0_RLC0_CONTEXT_STATUS__IDLE_MASK)
279			break;
280		if (time_after(jiffies, end_jiffies)) {
281			pr_err("SDMA RLC not idle in %s\n", __func__);
282			return -ETIME;
283		}
284		usleep_range(500, 1000);
285	}
 
 
 
 
 
 
 
 
 
 
 
286
287	data = REG_SET_FIELD(m->sdmax_rlcx_doorbell, SDMA0_RLC0_DOORBELL,
288			     ENABLE, 1);
289	WREG32(sdma_rlc_reg_offset + mmSDMA0_RLC0_DOORBELL, data);
290	WREG32(sdma_rlc_reg_offset + mmSDMA0_RLC0_RB_RPTR,
291				m->sdmax_rlcx_rb_rptr);
292
293	if (read_user_wptr(mm, wptr, data))
294		WREG32(sdma_rlc_reg_offset + mmSDMA0_RLC0_RB_WPTR, data);
295	else
296		WREG32(sdma_rlc_reg_offset + mmSDMA0_RLC0_RB_WPTR,
297		       m->sdmax_rlcx_rb_rptr);
298
299	WREG32(sdma_rlc_reg_offset + mmSDMA0_RLC0_VIRTUAL_ADDR,
300				m->sdmax_rlcx_virtual_addr);
301	WREG32(sdma_rlc_reg_offset + mmSDMA0_RLC0_RB_BASE, m->sdmax_rlcx_rb_base);
302	WREG32(sdma_rlc_reg_offset + mmSDMA0_RLC0_RB_BASE_HI,
303			m->sdmax_rlcx_rb_base_hi);
304	WREG32(sdma_rlc_reg_offset + mmSDMA0_RLC0_RB_RPTR_ADDR_LO,
305			m->sdmax_rlcx_rb_rptr_addr_lo);
306	WREG32(sdma_rlc_reg_offset + mmSDMA0_RLC0_RB_RPTR_ADDR_HI,
307			m->sdmax_rlcx_rb_rptr_addr_hi);
308
309	data = REG_SET_FIELD(m->sdmax_rlcx_rb_cntl, SDMA0_RLC0_RB_CNTL,
310			     RB_ENABLE, 1);
311	WREG32(sdma_rlc_reg_offset + mmSDMA0_RLC0_RB_CNTL, data);
312
313	return 0;
314}
315
316static int kgd_hqd_sdma_dump(struct amdgpu_device *adev,
317			     uint32_t engine_id, uint32_t queue_id,
318			     uint32_t (**dump)[2], uint32_t *n_regs)
319{
 
320	uint32_t sdma_offset = engine_id * SDMA1_REGISTER_OFFSET +
321		queue_id * KFD_VI_SDMA_QUEUE_OFFSET;
322	uint32_t i = 0, reg;
323#undef HQD_N_REGS
324#define HQD_N_REGS (19+4+2+3+7)
325
326	*dump = kmalloc_array(HQD_N_REGS * 2, sizeof(uint32_t), GFP_KERNEL);
327	if (*dump == NULL)
328		return -ENOMEM;
329
330	for (reg = mmSDMA0_RLC0_RB_CNTL; reg <= mmSDMA0_RLC0_DOORBELL; reg++)
331		DUMP_REG(sdma_offset + reg);
332	for (reg = mmSDMA0_RLC0_VIRTUAL_ADDR; reg <= mmSDMA0_RLC0_WATERMARK;
333	     reg++)
334		DUMP_REG(sdma_offset + reg);
335	for (reg = mmSDMA0_RLC0_CSA_ADDR_LO; reg <= mmSDMA0_RLC0_CSA_ADDR_HI;
336	     reg++)
337		DUMP_REG(sdma_offset + reg);
338	for (reg = mmSDMA0_RLC0_IB_SUB_REMAIN; reg <= mmSDMA0_RLC0_DUMMY_REG;
339	     reg++)
340		DUMP_REG(sdma_offset + reg);
341	for (reg = mmSDMA0_RLC0_MIDCMD_DATA0; reg <= mmSDMA0_RLC0_MIDCMD_CNTL;
342	     reg++)
343		DUMP_REG(sdma_offset + reg);
344
345	WARN_ON_ONCE(i != HQD_N_REGS);
346	*n_regs = i;
347
348	return 0;
349}
350
351static bool kgd_hqd_is_occupied(struct amdgpu_device *adev,
352				uint64_t queue_address, uint32_t pipe_id,
353				uint32_t queue_id)
354{
 
355	uint32_t act;
356	bool retval = false;
357	uint32_t low, high;
358
359	acquire_queue(adev, pipe_id, queue_id);
360	act = RREG32(mmCP_HQD_ACTIVE);
361	if (act) {
362		low = lower_32_bits(queue_address >> 8);
363		high = upper_32_bits(queue_address >> 8);
364
365		if (low == RREG32(mmCP_HQD_PQ_BASE) &&
366				high == RREG32(mmCP_HQD_PQ_BASE_HI))
367			retval = true;
368	}
369	release_queue(adev);
370	return retval;
371}
372
373static bool kgd_hqd_sdma_is_occupied(struct amdgpu_device *adev, void *mqd)
374{
 
375	struct vi_sdma_mqd *m;
376	uint32_t sdma_rlc_reg_offset;
377	uint32_t sdma_rlc_rb_cntl;
378
379	m = get_sdma_mqd(mqd);
380	sdma_rlc_reg_offset = get_sdma_rlc_reg_offset(m);
381
382	sdma_rlc_rb_cntl = RREG32(sdma_rlc_reg_offset + mmSDMA0_RLC0_RB_CNTL);
383
384	if (sdma_rlc_rb_cntl & SDMA0_RLC0_RB_CNTL__RB_ENABLE_MASK)
385		return true;
386
387	return false;
388}
389
390static int kgd_hqd_destroy(struct amdgpu_device *adev, void *mqd,
391				enum kfd_preempt_type reset_type,
392				unsigned int utimeout, uint32_t pipe_id,
393				uint32_t queue_id)
394{
 
395	uint32_t temp;
396	enum hqd_dequeue_request_type type;
397	unsigned long flags, end_jiffies;
398	int retry;
399	struct vi_mqd *m = get_mqd(mqd);
400
401	if (amdgpu_in_reset(adev))
402		return -EIO;
403
404	acquire_queue(adev, pipe_id, queue_id);
405
406	if (m->cp_hqd_vmid == 0)
407		WREG32_FIELD(RLC_CP_SCHEDULERS, scheduler1, 0);
408
409	switch (reset_type) {
410	case KFD_PREEMPT_TYPE_WAVEFRONT_DRAIN:
411		type = DRAIN_PIPE;
412		break;
413	case KFD_PREEMPT_TYPE_WAVEFRONT_RESET:
414		type = RESET_WAVES;
415		break;
416	default:
417		type = DRAIN_PIPE;
418		break;
419	}
420
421	/* Workaround: If IQ timer is active and the wait time is close to or
422	 * equal to 0, dequeueing is not safe. Wait until either the wait time
423	 * is larger or timer is cleared. Also, ensure that IQ_REQ_PEND is
424	 * cleared before continuing. Also, ensure wait times are set to at
425	 * least 0x3.
426	 */
427	local_irq_save(flags);
428	preempt_disable();
429	retry = 5000; /* wait for 500 usecs at maximum */
430	while (true) {
431		temp = RREG32(mmCP_HQD_IQ_TIMER);
432		if (REG_GET_FIELD(temp, CP_HQD_IQ_TIMER, PROCESSING_IQ)) {
433			pr_debug("HW is processing IQ\n");
434			goto loop;
435		}
436		if (REG_GET_FIELD(temp, CP_HQD_IQ_TIMER, ACTIVE)) {
437			if (REG_GET_FIELD(temp, CP_HQD_IQ_TIMER, RETRY_TYPE)
438					== 3) /* SEM-rearm is safe */
439				break;
440			/* Wait time 3 is safe for CP, but our MMIO read/write
441			 * time is close to 1 microsecond, so check for 10 to
442			 * leave more buffer room
443			 */
444			if (REG_GET_FIELD(temp, CP_HQD_IQ_TIMER, WAIT_TIME)
445					>= 10)
446				break;
447			pr_debug("IQ timer is active\n");
448		} else
449			break;
450loop:
451		if (!retry) {
452			pr_err("CP HQD IQ timer status time out\n");
453			break;
454		}
455		ndelay(100);
456		--retry;
457	}
458	retry = 1000;
459	while (true) {
460		temp = RREG32(mmCP_HQD_DEQUEUE_REQUEST);
461		if (!(temp & CP_HQD_DEQUEUE_REQUEST__IQ_REQ_PEND_MASK))
462			break;
463		pr_debug("Dequeue request is pending\n");
464
465		if (!retry) {
466			pr_err("CP HQD dequeue request time out\n");
467			break;
468		}
469		ndelay(100);
470		--retry;
471	}
472	local_irq_restore(flags);
473	preempt_enable();
474
475	WREG32(mmCP_HQD_DEQUEUE_REQUEST, type);
476
477	end_jiffies = (utimeout * HZ / 1000) + jiffies;
478	while (true) {
479		temp = RREG32(mmCP_HQD_ACTIVE);
480		if (!(temp & CP_HQD_ACTIVE__ACTIVE_MASK))
481			break;
482		if (time_after(jiffies, end_jiffies)) {
483			pr_err("cp queue preemption time out.\n");
484			release_queue(adev);
485			return -ETIME;
486		}
487		usleep_range(500, 1000);
488	}
489
490	release_queue(adev);
491	return 0;
492}
493
494static int kgd_hqd_sdma_destroy(struct amdgpu_device *adev, void *mqd,
495				unsigned int utimeout)
496{
 
497	struct vi_sdma_mqd *m;
498	uint32_t sdma_rlc_reg_offset;
499	uint32_t temp;
500	unsigned long end_jiffies = (utimeout * HZ / 1000) + jiffies;
501
502	m = get_sdma_mqd(mqd);
503	sdma_rlc_reg_offset = get_sdma_rlc_reg_offset(m);
504
505	temp = RREG32(sdma_rlc_reg_offset + mmSDMA0_RLC0_RB_CNTL);
506	temp = temp & ~SDMA0_RLC0_RB_CNTL__RB_ENABLE_MASK;
507	WREG32(sdma_rlc_reg_offset + mmSDMA0_RLC0_RB_CNTL, temp);
508
509	while (true) {
510		temp = RREG32(sdma_rlc_reg_offset + mmSDMA0_RLC0_CONTEXT_STATUS);
511		if (temp & SDMA0_RLC0_CONTEXT_STATUS__IDLE_MASK)
512			break;
513		if (time_after(jiffies, end_jiffies)) {
514			pr_err("SDMA RLC not idle in %s\n", __func__);
515			return -ETIME;
516		}
517		usleep_range(500, 1000);
518	}
519
520	WREG32(sdma_rlc_reg_offset + mmSDMA0_RLC0_DOORBELL, 0);
521	WREG32(sdma_rlc_reg_offset + mmSDMA0_RLC0_RB_CNTL,
522		RREG32(sdma_rlc_reg_offset + mmSDMA0_RLC0_RB_CNTL) |
523		SDMA0_RLC0_RB_CNTL__RB_ENABLE_MASK);
524
525	m->sdmax_rlcx_rb_rptr = RREG32(sdma_rlc_reg_offset + mmSDMA0_RLC0_RB_RPTR);
526
527	return 0;
528}
529
530static bool get_atc_vmid_pasid_mapping_info(struct amdgpu_device *adev,
531					uint8_t vmid, uint16_t *p_pasid)
 
 
 
 
 
 
 
 
 
 
532{
533	uint32_t value;
 
 
 
 
 
534
535	value = RREG32(mmATC_VMID0_PASID_MAPPING + vmid);
536	*p_pasid = value & ATC_VMID0_PASID_MAPPING__PASID_MASK;
 
 
537
538	return !!(value & ATC_VMID0_PASID_MAPPING__VALID_MASK);
 
 
 
 
 
 
539}
540
541static int kgd_wave_control_execute(struct amdgpu_device *adev,
542					uint32_t gfx_index_val,
543					uint32_t sq_cmd)
544{
 
545	uint32_t data = 0;
546
547	mutex_lock(&adev->grbm_idx_mutex);
548
549	WREG32(mmGRBM_GFX_INDEX, gfx_index_val);
550	WREG32(mmSQ_CMD, sq_cmd);
551
552	data = REG_SET_FIELD(data, GRBM_GFX_INDEX,
553		INSTANCE_BROADCAST_WRITES, 1);
554	data = REG_SET_FIELD(data, GRBM_GFX_INDEX,
555		SH_BROADCAST_WRITES, 1);
556	data = REG_SET_FIELD(data, GRBM_GFX_INDEX,
557		SE_BROADCAST_WRITES, 1);
558
559	WREG32(mmGRBM_GFX_INDEX, data);
560	mutex_unlock(&adev->grbm_idx_mutex);
561
562	return 0;
563}
564
565static void set_scratch_backing_va(struct amdgpu_device *adev,
 
 
 
 
 
 
 
566					uint64_t va, uint32_t vmid)
567{
568	lock_srbm(adev, 0, 0, 0, vmid);
 
 
569	WREG32(mmSH_HIDDEN_PRIVATE_BASE_VMID, va);
570	unlock_srbm(adev);
571}
572
573static void set_vm_context_page_table_base(struct amdgpu_device *adev,
574		uint32_t vmid, uint64_t page_table_base)
575{
 
 
576	if (!amdgpu_amdkfd_is_kfd_vmid(adev, vmid)) {
577		pr_err("trying to set page table base for wrong VMID\n");
578		return;
579	}
580	WREG32(mmVM_CONTEXT8_PAGE_TABLE_BASE_ADDR + vmid - 8,
581			lower_32_bits(page_table_base));
582}
583
584const struct kfd2kgd_calls gfx_v8_kfd2kgd = {
585	.program_sh_mem_settings = kgd_program_sh_mem_settings,
586	.set_pasid_vmid_mapping = kgd_set_pasid_vmid_mapping,
587	.init_interrupts = kgd_init_interrupts,
588	.hqd_load = kgd_hqd_load,
589	.hqd_sdma_load = kgd_hqd_sdma_load,
590	.hqd_dump = kgd_hqd_dump,
591	.hqd_sdma_dump = kgd_hqd_sdma_dump,
592	.hqd_is_occupied = kgd_hqd_is_occupied,
593	.hqd_sdma_is_occupied = kgd_hqd_sdma_is_occupied,
594	.hqd_destroy = kgd_hqd_destroy,
595	.hqd_sdma_destroy = kgd_hqd_sdma_destroy,
596	.wave_control_execute = kgd_wave_control_execute,
597	.get_atc_vmid_pasid_mapping_info =
598			get_atc_vmid_pasid_mapping_info,
599	.set_scratch_backing_va = set_scratch_backing_va,
600	.set_vm_context_page_table_base = set_vm_context_page_table_base,
601};
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
v5.4
  1/*
  2 * Copyright 2014 Advanced Micro Devices, Inc.
  3 *
  4 * Permission is hereby granted, free of charge, to any person obtaining a
  5 * copy of this software and associated documentation files (the "Software"),
  6 * to deal in the Software without restriction, including without limitation
  7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8 * and/or sell copies of the Software, and to permit persons to whom the
  9 * Software is furnished to do so, subject to the following conditions:
 10 *
 11 * The above copyright notice and this permission notice shall be included in
 12 * all copies or substantial portions of the Software.
 13 *
 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 17 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
 18 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 20 * OTHER DEALINGS IN THE SOFTWARE.
 21 */
 22
 23#include <linux/module.h>
 24#include <linux/fdtable.h>
 25#include <linux/uaccess.h>
 26#include <linux/mmu_context.h>
 27
 28#include "amdgpu.h"
 29#include "amdgpu_amdkfd.h"
 30#include "gfx_v8_0.h"
 31#include "gca/gfx_8_0_sh_mask.h"
 32#include "gca/gfx_8_0_d.h"
 33#include "gca/gfx_8_0_enum.h"
 34#include "oss/oss_3_0_sh_mask.h"
 35#include "oss/oss_3_0_d.h"
 36#include "gmc/gmc_8_1_sh_mask.h"
 37#include "gmc/gmc_8_1_d.h"
 38#include "vi_structs.h"
 39#include "vid.h"
 40
 41enum hqd_dequeue_request_type {
 42	NO_ACTION = 0,
 43	DRAIN_PIPE,
 44	RESET_WAVES
 45};
 46
 47/*
 48 * Register access functions
 49 */
 50
 51static void kgd_program_sh_mem_settings(struct kgd_dev *kgd, uint32_t vmid,
 52		uint32_t sh_mem_config,
 53		uint32_t sh_mem_ape1_base, uint32_t sh_mem_ape1_limit,
 54		uint32_t sh_mem_bases);
 55static int kgd_set_pasid_vmid_mapping(struct kgd_dev *kgd, unsigned int pasid,
 56		unsigned int vmid);
 57static int kgd_init_interrupts(struct kgd_dev *kgd, uint32_t pipe_id);
 58static int kgd_hqd_load(struct kgd_dev *kgd, void *mqd, uint32_t pipe_id,
 59			uint32_t queue_id, uint32_t __user *wptr,
 60			uint32_t wptr_shift, uint32_t wptr_mask,
 61			struct mm_struct *mm);
 62static int kgd_hqd_dump(struct kgd_dev *kgd,
 63			uint32_t pipe_id, uint32_t queue_id,
 64			uint32_t (**dump)[2], uint32_t *n_regs);
 65static int kgd_hqd_sdma_load(struct kgd_dev *kgd, void *mqd,
 66			     uint32_t __user *wptr, struct mm_struct *mm);
 67static int kgd_hqd_sdma_dump(struct kgd_dev *kgd,
 68			     uint32_t engine_id, uint32_t queue_id,
 69			     uint32_t (**dump)[2], uint32_t *n_regs);
 70static bool kgd_hqd_is_occupied(struct kgd_dev *kgd, uint64_t queue_address,
 71		uint32_t pipe_id, uint32_t queue_id);
 72static bool kgd_hqd_sdma_is_occupied(struct kgd_dev *kgd, void *mqd);
 73static int kgd_hqd_destroy(struct kgd_dev *kgd, void *mqd,
 74				enum kfd_preempt_type reset_type,
 75				unsigned int utimeout, uint32_t pipe_id,
 76				uint32_t queue_id);
 77static int kgd_hqd_sdma_destroy(struct kgd_dev *kgd, void *mqd,
 78				unsigned int utimeout);
 79static int kgd_address_watch_disable(struct kgd_dev *kgd);
 80static int kgd_address_watch_execute(struct kgd_dev *kgd,
 81					unsigned int watch_point_id,
 82					uint32_t cntl_val,
 83					uint32_t addr_hi,
 84					uint32_t addr_lo);
 85static int kgd_wave_control_execute(struct kgd_dev *kgd,
 86					uint32_t gfx_index_val,
 87					uint32_t sq_cmd);
 88static uint32_t kgd_address_watch_get_offset(struct kgd_dev *kgd,
 89					unsigned int watch_point_id,
 90					unsigned int reg_offset);
 91
 92static bool get_atc_vmid_pasid_mapping_valid(struct kgd_dev *kgd,
 93		uint8_t vmid);
 94static uint16_t get_atc_vmid_pasid_mapping_pasid(struct kgd_dev *kgd,
 95		uint8_t vmid);
 96static void set_scratch_backing_va(struct kgd_dev *kgd,
 97					uint64_t va, uint32_t vmid);
 98static void set_vm_context_page_table_base(struct kgd_dev *kgd, uint32_t vmid,
 99		uint64_t page_table_base);
100static int invalidate_tlbs(struct kgd_dev *kgd, uint16_t pasid);
101static int invalidate_tlbs_vmid(struct kgd_dev *kgd, uint16_t vmid);
102
103/* Because of REG_GET_FIELD() being used, we put this function in the
104 * asic specific file.
105 */
106static int get_tile_config(struct kgd_dev *kgd,
107		struct tile_config *config)
108{
109	struct amdgpu_device *adev = (struct amdgpu_device *)kgd;
110
111	config->gb_addr_config = adev->gfx.config.gb_addr_config;
112	config->num_banks = REG_GET_FIELD(adev->gfx.config.mc_arb_ramcfg,
113				MC_ARB_RAMCFG, NOOFBANK);
114	config->num_ranks = REG_GET_FIELD(adev->gfx.config.mc_arb_ramcfg,
115				MC_ARB_RAMCFG, NOOFRANKS);
116
117	config->tile_config_ptr = adev->gfx.config.tile_mode_array;
118	config->num_tile_configs =
119			ARRAY_SIZE(adev->gfx.config.tile_mode_array);
120	config->macro_tile_config_ptr =
121			adev->gfx.config.macrotile_mode_array;
122	config->num_macro_tile_configs =
123			ARRAY_SIZE(adev->gfx.config.macrotile_mode_array);
124
125	return 0;
126}
127
128static const struct kfd2kgd_calls kfd2kgd = {
129	.program_sh_mem_settings = kgd_program_sh_mem_settings,
130	.set_pasid_vmid_mapping = kgd_set_pasid_vmid_mapping,
131	.init_interrupts = kgd_init_interrupts,
132	.hqd_load = kgd_hqd_load,
133	.hqd_sdma_load = kgd_hqd_sdma_load,
134	.hqd_dump = kgd_hqd_dump,
135	.hqd_sdma_dump = kgd_hqd_sdma_dump,
136	.hqd_is_occupied = kgd_hqd_is_occupied,
137	.hqd_sdma_is_occupied = kgd_hqd_sdma_is_occupied,
138	.hqd_destroy = kgd_hqd_destroy,
139	.hqd_sdma_destroy = kgd_hqd_sdma_destroy,
140	.address_watch_disable = kgd_address_watch_disable,
141	.address_watch_execute = kgd_address_watch_execute,
142	.wave_control_execute = kgd_wave_control_execute,
143	.address_watch_get_offset = kgd_address_watch_get_offset,
144	.get_atc_vmid_pasid_mapping_pasid =
145			get_atc_vmid_pasid_mapping_pasid,
146	.get_atc_vmid_pasid_mapping_valid =
147			get_atc_vmid_pasid_mapping_valid,
148	.set_scratch_backing_va = set_scratch_backing_va,
149	.get_tile_config = get_tile_config,
150	.set_vm_context_page_table_base = set_vm_context_page_table_base,
151	.invalidate_tlbs = invalidate_tlbs,
152	.invalidate_tlbs_vmid = invalidate_tlbs_vmid,
153};
154
155struct kfd2kgd_calls *amdgpu_amdkfd_gfx_8_0_get_functions(void)
156{
157	return (struct kfd2kgd_calls *)&kfd2kgd;
158}
159
160static inline struct amdgpu_device *get_amdgpu_device(struct kgd_dev *kgd)
161{
162	return (struct amdgpu_device *)kgd;
163}
164
165static void lock_srbm(struct kgd_dev *kgd, uint32_t mec, uint32_t pipe,
166			uint32_t queue, uint32_t vmid)
167{
168	struct amdgpu_device *adev = get_amdgpu_device(kgd);
169	uint32_t value = PIPEID(pipe) | MEID(mec) | VMID(vmid) | QUEUEID(queue);
170
171	mutex_lock(&adev->srbm_mutex);
172	WREG32(mmSRBM_GFX_CNTL, value);
173}
174
175static void unlock_srbm(struct kgd_dev *kgd)
176{
177	struct amdgpu_device *adev = get_amdgpu_device(kgd);
178
179	WREG32(mmSRBM_GFX_CNTL, 0);
180	mutex_unlock(&adev->srbm_mutex);
181}
182
183static void acquire_queue(struct kgd_dev *kgd, uint32_t pipe_id,
184				uint32_t queue_id)
185{
186	struct amdgpu_device *adev = get_amdgpu_device(kgd);
187
188	uint32_t mec = (pipe_id / adev->gfx.mec.num_pipe_per_mec) + 1;
189	uint32_t pipe = (pipe_id % adev->gfx.mec.num_pipe_per_mec);
190
191	lock_srbm(kgd, mec, pipe, queue_id, 0);
192}
193
194static void release_queue(struct kgd_dev *kgd)
195{
196	unlock_srbm(kgd);
197}
198
199static void kgd_program_sh_mem_settings(struct kgd_dev *kgd, uint32_t vmid,
200					uint32_t sh_mem_config,
201					uint32_t sh_mem_ape1_base,
202					uint32_t sh_mem_ape1_limit,
203					uint32_t sh_mem_bases)
204{
205	struct amdgpu_device *adev = get_amdgpu_device(kgd);
206
207	lock_srbm(kgd, 0, 0, 0, vmid);
208
209	WREG32(mmSH_MEM_CONFIG, sh_mem_config);
210	WREG32(mmSH_MEM_APE1_BASE, sh_mem_ape1_base);
211	WREG32(mmSH_MEM_APE1_LIMIT, sh_mem_ape1_limit);
212	WREG32(mmSH_MEM_BASES, sh_mem_bases);
213
214	unlock_srbm(kgd);
215}
216
217static int kgd_set_pasid_vmid_mapping(struct kgd_dev *kgd, unsigned int pasid,
218					unsigned int vmid)
219{
220	struct amdgpu_device *adev = get_amdgpu_device(kgd);
221
222	/*
223	 * We have to assume that there is no outstanding mapping.
224	 * The ATC_VMID_PASID_MAPPING_UPDATE_STATUS bit could be 0 because
225	 * a mapping is in progress or because a mapping finished
226	 * and the SW cleared it.
227	 * So the protocol is to always wait & clear.
228	 */
229	uint32_t pasid_mapping = (pasid == 0) ? 0 : (uint32_t)pasid |
230			ATC_VMID0_PASID_MAPPING__VALID_MASK;
231
232	WREG32(mmATC_VMID0_PASID_MAPPING + vmid, pasid_mapping);
233
234	while (!(RREG32(mmATC_VMID_PASID_MAPPING_UPDATE_STATUS) & (1U << vmid)))
235		cpu_relax();
236	WREG32(mmATC_VMID_PASID_MAPPING_UPDATE_STATUS, 1U << vmid);
237
238	/* Mapping vmid to pasid also for IH block */
239	WREG32(mmIH_VMID_0_LUT + vmid, pasid_mapping);
240
241	return 0;
242}
243
244static int kgd_init_interrupts(struct kgd_dev *kgd, uint32_t pipe_id)
245{
246	struct amdgpu_device *adev = get_amdgpu_device(kgd);
247	uint32_t mec;
248	uint32_t pipe;
249
250	mec = (pipe_id / adev->gfx.mec.num_pipe_per_mec) + 1;
251	pipe = (pipe_id % adev->gfx.mec.num_pipe_per_mec);
252
253	lock_srbm(kgd, mec, pipe, 0, 0);
254
255	WREG32(mmCPC_INT_CNTL, CP_INT_CNTL_RING0__TIME_STAMP_INT_ENABLE_MASK |
256			CP_INT_CNTL_RING0__OPCODE_ERROR_INT_ENABLE_MASK);
257
258	unlock_srbm(kgd);
259
260	return 0;
261}
262
263static inline uint32_t get_sdma_base_addr(struct vi_sdma_mqd *m)
264{
265	uint32_t retval;
266
267	retval = m->sdma_engine_id * SDMA1_REGISTER_OFFSET +
268		m->sdma_queue_id * KFD_VI_SDMA_QUEUE_OFFSET;
269	pr_debug("sdma base address: 0x%x\n", retval);
 
 
270
271	return retval;
272}
273
274static inline struct vi_mqd *get_mqd(void *mqd)
275{
276	return (struct vi_mqd *)mqd;
277}
278
279static inline struct vi_sdma_mqd *get_sdma_mqd(void *mqd)
280{
281	return (struct vi_sdma_mqd *)mqd;
282}
283
284static int kgd_hqd_load(struct kgd_dev *kgd, void *mqd, uint32_t pipe_id,
285			uint32_t queue_id, uint32_t __user *wptr,
286			uint32_t wptr_shift, uint32_t wptr_mask,
287			struct mm_struct *mm)
288{
289	struct amdgpu_device *adev = get_amdgpu_device(kgd);
290	struct vi_mqd *m;
291	uint32_t *mqd_hqd;
292	uint32_t reg, wptr_val, data;
293	bool valid_wptr = false;
294
295	m = get_mqd(mqd);
296
297	acquire_queue(kgd, pipe_id, queue_id);
298
299	/* HIQ is set during driver init period with vmid set to 0*/
300	if (m->cp_hqd_vmid == 0) {
301		uint32_t value, mec, pipe;
302
303		mec = (pipe_id / adev->gfx.mec.num_pipe_per_mec) + 1;
304		pipe = (pipe_id % adev->gfx.mec.num_pipe_per_mec);
305
306		pr_debug("kfd: set HIQ, mec:%d, pipe:%d, queue:%d.\n",
307			mec, pipe, queue_id);
308		value = RREG32(mmRLC_CP_SCHEDULERS);
309		value = REG_SET_FIELD(value, RLC_CP_SCHEDULERS, scheduler1,
310			((mec << 5) | (pipe << 3) | queue_id | 0x80));
311		WREG32(mmRLC_CP_SCHEDULERS, value);
312	}
313
314	/* HQD registers extend from CP_MQD_BASE_ADDR to CP_HQD_EOP_WPTR_MEM. */
315	mqd_hqd = &m->cp_mqd_base_addr_lo;
316
317	for (reg = mmCP_MQD_BASE_ADDR; reg <= mmCP_HQD_EOP_CONTROL; reg++)
318		WREG32(reg, mqd_hqd[reg - mmCP_MQD_BASE_ADDR]);
319
320	/* Tonga errata: EOP RPTR/WPTR should be left unmodified.
321	 * This is safe since EOP RPTR==WPTR for any inactive HQD
322	 * on ASICs that do not support context-save.
323	 * EOP writes/reads can start anywhere in the ring.
324	 */
325	if (get_amdgpu_device(kgd)->asic_type != CHIP_TONGA) {
326		WREG32(mmCP_HQD_EOP_RPTR, m->cp_hqd_eop_rptr);
327		WREG32(mmCP_HQD_EOP_WPTR, m->cp_hqd_eop_wptr);
328		WREG32(mmCP_HQD_EOP_WPTR_MEM, m->cp_hqd_eop_wptr_mem);
329	}
330
331	for (reg = mmCP_HQD_EOP_EVENTS; reg <= mmCP_HQD_ERROR; reg++)
332		WREG32(reg, mqd_hqd[reg - mmCP_MQD_BASE_ADDR]);
333
334	/* Copy userspace write pointer value to register.
335	 * Activate doorbell logic to monitor subsequent changes.
336	 */
337	data = REG_SET_FIELD(m->cp_hqd_pq_doorbell_control,
338			     CP_HQD_PQ_DOORBELL_CONTROL, DOORBELL_EN, 1);
339	WREG32(mmCP_HQD_PQ_DOORBELL_CONTROL, data);
340
341	/* read_user_ptr may take the mm->mmap_sem.
342	 * release srbm_mutex to avoid circular dependency between
343	 * srbm_mutex->mm_sem->reservation_ww_class_mutex->srbm_mutex.
344	 */
345	release_queue(kgd);
346	valid_wptr = read_user_wptr(mm, wptr, wptr_val);
347	acquire_queue(kgd, pipe_id, queue_id);
348	if (valid_wptr)
349		WREG32(mmCP_HQD_PQ_WPTR, (wptr_val << wptr_shift) & wptr_mask);
350
351	data = REG_SET_FIELD(m->cp_hqd_active, CP_HQD_ACTIVE, ACTIVE, 1);
352	WREG32(mmCP_HQD_ACTIVE, data);
353
354	release_queue(kgd);
355
356	return 0;
357}
358
359static int kgd_hqd_dump(struct kgd_dev *kgd,
360			uint32_t pipe_id, uint32_t queue_id,
361			uint32_t (**dump)[2], uint32_t *n_regs)
362{
363	struct amdgpu_device *adev = get_amdgpu_device(kgd);
364	uint32_t i = 0, reg;
365#define HQD_N_REGS (54+4)
366#define DUMP_REG(addr) do {				\
367		if (WARN_ON_ONCE(i >= HQD_N_REGS))	\
368			break;				\
369		(*dump)[i][0] = (addr) << 2;		\
370		(*dump)[i++][1] = RREG32(addr);		\
371	} while (0)
372
373	*dump = kmalloc_array(HQD_N_REGS * 2, sizeof(uint32_t), GFP_KERNEL);
374	if (*dump == NULL)
375		return -ENOMEM;
376
377	acquire_queue(kgd, pipe_id, queue_id);
378
379	DUMP_REG(mmCOMPUTE_STATIC_THREAD_MGMT_SE0);
380	DUMP_REG(mmCOMPUTE_STATIC_THREAD_MGMT_SE1);
381	DUMP_REG(mmCOMPUTE_STATIC_THREAD_MGMT_SE2);
382	DUMP_REG(mmCOMPUTE_STATIC_THREAD_MGMT_SE3);
383
384	for (reg = mmCP_MQD_BASE_ADDR; reg <= mmCP_HQD_EOP_DONES; reg++)
385		DUMP_REG(reg);
386
387	release_queue(kgd);
388
389	WARN_ON_ONCE(i != HQD_N_REGS);
390	*n_regs = i;
391
392	return 0;
393}
394
395static int kgd_hqd_sdma_load(struct kgd_dev *kgd, void *mqd,
396			     uint32_t __user *wptr, struct mm_struct *mm)
397{
398	struct amdgpu_device *adev = get_amdgpu_device(kgd);
399	struct vi_sdma_mqd *m;
400	unsigned long end_jiffies;
401	uint32_t sdma_base_addr;
402	uint32_t data;
403
404	m = get_sdma_mqd(mqd);
405	sdma_base_addr = get_sdma_base_addr(m);
406	WREG32(sdma_base_addr + mmSDMA0_RLC0_RB_CNTL,
407		m->sdmax_rlcx_rb_cntl & (~SDMA0_RLC0_RB_CNTL__RB_ENABLE_MASK));
408
409	end_jiffies = msecs_to_jiffies(2000) + jiffies;
410	while (true) {
411		data = RREG32(sdma_base_addr + mmSDMA0_RLC0_CONTEXT_STATUS);
412		if (data & SDMA0_RLC0_CONTEXT_STATUS__IDLE_MASK)
413			break;
414		if (time_after(jiffies, end_jiffies))
 
415			return -ETIME;
 
416		usleep_range(500, 1000);
417	}
418	if (m->sdma_engine_id) {
419		data = RREG32(mmSDMA1_GFX_CONTEXT_CNTL);
420		data = REG_SET_FIELD(data, SDMA1_GFX_CONTEXT_CNTL,
421				RESUME_CTX, 0);
422		WREG32(mmSDMA1_GFX_CONTEXT_CNTL, data);
423	} else {
424		data = RREG32(mmSDMA0_GFX_CONTEXT_CNTL);
425		data = REG_SET_FIELD(data, SDMA0_GFX_CONTEXT_CNTL,
426				RESUME_CTX, 0);
427		WREG32(mmSDMA0_GFX_CONTEXT_CNTL, data);
428	}
429
430	data = REG_SET_FIELD(m->sdmax_rlcx_doorbell, SDMA0_RLC0_DOORBELL,
431			     ENABLE, 1);
432	WREG32(sdma_base_addr + mmSDMA0_RLC0_DOORBELL, data);
433	WREG32(sdma_base_addr + mmSDMA0_RLC0_RB_RPTR, m->sdmax_rlcx_rb_rptr);
 
434
435	if (read_user_wptr(mm, wptr, data))
436		WREG32(sdma_base_addr + mmSDMA0_RLC0_RB_WPTR, data);
437	else
438		WREG32(sdma_base_addr + mmSDMA0_RLC0_RB_WPTR,
439		       m->sdmax_rlcx_rb_rptr);
440
441	WREG32(sdma_base_addr + mmSDMA0_RLC0_VIRTUAL_ADDR,
442				m->sdmax_rlcx_virtual_addr);
443	WREG32(sdma_base_addr + mmSDMA0_RLC0_RB_BASE, m->sdmax_rlcx_rb_base);
444	WREG32(sdma_base_addr + mmSDMA0_RLC0_RB_BASE_HI,
445			m->sdmax_rlcx_rb_base_hi);
446	WREG32(sdma_base_addr + mmSDMA0_RLC0_RB_RPTR_ADDR_LO,
447			m->sdmax_rlcx_rb_rptr_addr_lo);
448	WREG32(sdma_base_addr + mmSDMA0_RLC0_RB_RPTR_ADDR_HI,
449			m->sdmax_rlcx_rb_rptr_addr_hi);
450
451	data = REG_SET_FIELD(m->sdmax_rlcx_rb_cntl, SDMA0_RLC0_RB_CNTL,
452			     RB_ENABLE, 1);
453	WREG32(sdma_base_addr + mmSDMA0_RLC0_RB_CNTL, data);
454
455	return 0;
456}
457
458static int kgd_hqd_sdma_dump(struct kgd_dev *kgd,
459			     uint32_t engine_id, uint32_t queue_id,
460			     uint32_t (**dump)[2], uint32_t *n_regs)
461{
462	struct amdgpu_device *adev = get_amdgpu_device(kgd);
463	uint32_t sdma_offset = engine_id * SDMA1_REGISTER_OFFSET +
464		queue_id * KFD_VI_SDMA_QUEUE_OFFSET;
465	uint32_t i = 0, reg;
466#undef HQD_N_REGS
467#define HQD_N_REGS (19+4+2+3+7)
468
469	*dump = kmalloc_array(HQD_N_REGS * 2, sizeof(uint32_t), GFP_KERNEL);
470	if (*dump == NULL)
471		return -ENOMEM;
472
473	for (reg = mmSDMA0_RLC0_RB_CNTL; reg <= mmSDMA0_RLC0_DOORBELL; reg++)
474		DUMP_REG(sdma_offset + reg);
475	for (reg = mmSDMA0_RLC0_VIRTUAL_ADDR; reg <= mmSDMA0_RLC0_WATERMARK;
476	     reg++)
477		DUMP_REG(sdma_offset + reg);
478	for (reg = mmSDMA0_RLC0_CSA_ADDR_LO; reg <= mmSDMA0_RLC0_CSA_ADDR_HI;
479	     reg++)
480		DUMP_REG(sdma_offset + reg);
481	for (reg = mmSDMA0_RLC0_IB_SUB_REMAIN; reg <= mmSDMA0_RLC0_DUMMY_REG;
482	     reg++)
483		DUMP_REG(sdma_offset + reg);
484	for (reg = mmSDMA0_RLC0_MIDCMD_DATA0; reg <= mmSDMA0_RLC0_MIDCMD_CNTL;
485	     reg++)
486		DUMP_REG(sdma_offset + reg);
487
488	WARN_ON_ONCE(i != HQD_N_REGS);
489	*n_regs = i;
490
491	return 0;
492}
493
494static bool kgd_hqd_is_occupied(struct kgd_dev *kgd, uint64_t queue_address,
495				uint32_t pipe_id, uint32_t queue_id)
 
496{
497	struct amdgpu_device *adev = get_amdgpu_device(kgd);
498	uint32_t act;
499	bool retval = false;
500	uint32_t low, high;
501
502	acquire_queue(kgd, pipe_id, queue_id);
503	act = RREG32(mmCP_HQD_ACTIVE);
504	if (act) {
505		low = lower_32_bits(queue_address >> 8);
506		high = upper_32_bits(queue_address >> 8);
507
508		if (low == RREG32(mmCP_HQD_PQ_BASE) &&
509				high == RREG32(mmCP_HQD_PQ_BASE_HI))
510			retval = true;
511	}
512	release_queue(kgd);
513	return retval;
514}
515
516static bool kgd_hqd_sdma_is_occupied(struct kgd_dev *kgd, void *mqd)
517{
518	struct amdgpu_device *adev = get_amdgpu_device(kgd);
519	struct vi_sdma_mqd *m;
520	uint32_t sdma_base_addr;
521	uint32_t sdma_rlc_rb_cntl;
522
523	m = get_sdma_mqd(mqd);
524	sdma_base_addr = get_sdma_base_addr(m);
525
526	sdma_rlc_rb_cntl = RREG32(sdma_base_addr + mmSDMA0_RLC0_RB_CNTL);
527
528	if (sdma_rlc_rb_cntl & SDMA0_RLC0_RB_CNTL__RB_ENABLE_MASK)
529		return true;
530
531	return false;
532}
533
534static int kgd_hqd_destroy(struct kgd_dev *kgd, void *mqd,
535				enum kfd_preempt_type reset_type,
536				unsigned int utimeout, uint32_t pipe_id,
537				uint32_t queue_id)
538{
539	struct amdgpu_device *adev = get_amdgpu_device(kgd);
540	uint32_t temp;
541	enum hqd_dequeue_request_type type;
542	unsigned long flags, end_jiffies;
543	int retry;
544	struct vi_mqd *m = get_mqd(mqd);
545
546	if (adev->in_gpu_reset)
547		return -EIO;
548
549	acquire_queue(kgd, pipe_id, queue_id);
550
551	if (m->cp_hqd_vmid == 0)
552		WREG32_FIELD(RLC_CP_SCHEDULERS, scheduler1, 0);
553
554	switch (reset_type) {
555	case KFD_PREEMPT_TYPE_WAVEFRONT_DRAIN:
556		type = DRAIN_PIPE;
557		break;
558	case KFD_PREEMPT_TYPE_WAVEFRONT_RESET:
559		type = RESET_WAVES;
560		break;
561	default:
562		type = DRAIN_PIPE;
563		break;
564	}
565
566	/* Workaround: If IQ timer is active and the wait time is close to or
567	 * equal to 0, dequeueing is not safe. Wait until either the wait time
568	 * is larger or timer is cleared. Also, ensure that IQ_REQ_PEND is
569	 * cleared before continuing. Also, ensure wait times are set to at
570	 * least 0x3.
571	 */
572	local_irq_save(flags);
573	preempt_disable();
574	retry = 5000; /* wait for 500 usecs at maximum */
575	while (true) {
576		temp = RREG32(mmCP_HQD_IQ_TIMER);
577		if (REG_GET_FIELD(temp, CP_HQD_IQ_TIMER, PROCESSING_IQ)) {
578			pr_debug("HW is processing IQ\n");
579			goto loop;
580		}
581		if (REG_GET_FIELD(temp, CP_HQD_IQ_TIMER, ACTIVE)) {
582			if (REG_GET_FIELD(temp, CP_HQD_IQ_TIMER, RETRY_TYPE)
583					== 3) /* SEM-rearm is safe */
584				break;
585			/* Wait time 3 is safe for CP, but our MMIO read/write
586			 * time is close to 1 microsecond, so check for 10 to
587			 * leave more buffer room
588			 */
589			if (REG_GET_FIELD(temp, CP_HQD_IQ_TIMER, WAIT_TIME)
590					>= 10)
591				break;
592			pr_debug("IQ timer is active\n");
593		} else
594			break;
595loop:
596		if (!retry) {
597			pr_err("CP HQD IQ timer status time out\n");
598			break;
599		}
600		ndelay(100);
601		--retry;
602	}
603	retry = 1000;
604	while (true) {
605		temp = RREG32(mmCP_HQD_DEQUEUE_REQUEST);
606		if (!(temp & CP_HQD_DEQUEUE_REQUEST__IQ_REQ_PEND_MASK))
607			break;
608		pr_debug("Dequeue request is pending\n");
609
610		if (!retry) {
611			pr_err("CP HQD dequeue request time out\n");
612			break;
613		}
614		ndelay(100);
615		--retry;
616	}
617	local_irq_restore(flags);
618	preempt_enable();
619
620	WREG32(mmCP_HQD_DEQUEUE_REQUEST, type);
621
622	end_jiffies = (utimeout * HZ / 1000) + jiffies;
623	while (true) {
624		temp = RREG32(mmCP_HQD_ACTIVE);
625		if (!(temp & CP_HQD_ACTIVE__ACTIVE_MASK))
626			break;
627		if (time_after(jiffies, end_jiffies)) {
628			pr_err("cp queue preemption time out.\n");
629			release_queue(kgd);
630			return -ETIME;
631		}
632		usleep_range(500, 1000);
633	}
634
635	release_queue(kgd);
636	return 0;
637}
638
639static int kgd_hqd_sdma_destroy(struct kgd_dev *kgd, void *mqd,
640				unsigned int utimeout)
641{
642	struct amdgpu_device *adev = get_amdgpu_device(kgd);
643	struct vi_sdma_mqd *m;
644	uint32_t sdma_base_addr;
645	uint32_t temp;
646	unsigned long end_jiffies = (utimeout * HZ / 1000) + jiffies;
647
648	m = get_sdma_mqd(mqd);
649	sdma_base_addr = get_sdma_base_addr(m);
650
651	temp = RREG32(sdma_base_addr + mmSDMA0_RLC0_RB_CNTL);
652	temp = temp & ~SDMA0_RLC0_RB_CNTL__RB_ENABLE_MASK;
653	WREG32(sdma_base_addr + mmSDMA0_RLC0_RB_CNTL, temp);
654
655	while (true) {
656		temp = RREG32(sdma_base_addr + mmSDMA0_RLC0_CONTEXT_STATUS);
657		if (temp & SDMA0_RLC0_CONTEXT_STATUS__IDLE_MASK)
658			break;
659		if (time_after(jiffies, end_jiffies))
 
660			return -ETIME;
 
661		usleep_range(500, 1000);
662	}
663
664	WREG32(sdma_base_addr + mmSDMA0_RLC0_DOORBELL, 0);
665	WREG32(sdma_base_addr + mmSDMA0_RLC0_RB_CNTL,
666		RREG32(sdma_base_addr + mmSDMA0_RLC0_RB_CNTL) |
667		SDMA0_RLC0_RB_CNTL__RB_ENABLE_MASK);
668
669	m->sdmax_rlcx_rb_rptr = RREG32(sdma_base_addr + mmSDMA0_RLC0_RB_RPTR);
670
671	return 0;
672}
673
674static bool get_atc_vmid_pasid_mapping_valid(struct kgd_dev *kgd,
675							uint8_t vmid)
676{
677	uint32_t reg;
678	struct amdgpu_device *adev = (struct amdgpu_device *) kgd;
679
680	reg = RREG32(mmATC_VMID0_PASID_MAPPING + vmid);
681	return reg & ATC_VMID0_PASID_MAPPING__VALID_MASK;
682}
683
684static uint16_t get_atc_vmid_pasid_mapping_pasid(struct kgd_dev *kgd,
685								uint8_t vmid)
686{
687	uint32_t reg;
688	struct amdgpu_device *adev = (struct amdgpu_device *) kgd;
689
690	reg = RREG32(mmATC_VMID0_PASID_MAPPING + vmid);
691	return reg & ATC_VMID0_PASID_MAPPING__PASID_MASK;
692}
693
694static int kgd_address_watch_disable(struct kgd_dev *kgd)
695{
696	return 0;
697}
698
699static int kgd_address_watch_execute(struct kgd_dev *kgd,
700					unsigned int watch_point_id,
701					uint32_t cntl_val,
702					uint32_t addr_hi,
703					uint32_t addr_lo)
704{
705	return 0;
706}
707
708static int kgd_wave_control_execute(struct kgd_dev *kgd,
709					uint32_t gfx_index_val,
710					uint32_t sq_cmd)
711{
712	struct amdgpu_device *adev = get_amdgpu_device(kgd);
713	uint32_t data = 0;
714
715	mutex_lock(&adev->grbm_idx_mutex);
716
717	WREG32(mmGRBM_GFX_INDEX, gfx_index_val);
718	WREG32(mmSQ_CMD, sq_cmd);
719
720	data = REG_SET_FIELD(data, GRBM_GFX_INDEX,
721		INSTANCE_BROADCAST_WRITES, 1);
722	data = REG_SET_FIELD(data, GRBM_GFX_INDEX,
723		SH_BROADCAST_WRITES, 1);
724	data = REG_SET_FIELD(data, GRBM_GFX_INDEX,
725		SE_BROADCAST_WRITES, 1);
726
727	WREG32(mmGRBM_GFX_INDEX, data);
728	mutex_unlock(&adev->grbm_idx_mutex);
729
730	return 0;
731}
732
733static uint32_t kgd_address_watch_get_offset(struct kgd_dev *kgd,
734					unsigned int watch_point_id,
735					unsigned int reg_offset)
736{
737	return 0;
738}
739
740static void set_scratch_backing_va(struct kgd_dev *kgd,
741					uint64_t va, uint32_t vmid)
742{
743	struct amdgpu_device *adev = (struct amdgpu_device *) kgd;
744
745	lock_srbm(kgd, 0, 0, 0, vmid);
746	WREG32(mmSH_HIDDEN_PRIVATE_BASE_VMID, va);
747	unlock_srbm(kgd);
748}
749
750static void set_vm_context_page_table_base(struct kgd_dev *kgd, uint32_t vmid,
751		uint64_t page_table_base)
752{
753	struct amdgpu_device *adev = get_amdgpu_device(kgd);
754
755	if (!amdgpu_amdkfd_is_kfd_vmid(adev, vmid)) {
756		pr_err("trying to set page table base for wrong VMID\n");
757		return;
758	}
759	WREG32(mmVM_CONTEXT8_PAGE_TABLE_BASE_ADDR + vmid - 8,
760			lower_32_bits(page_table_base));
761}
762
763static int invalidate_tlbs(struct kgd_dev *kgd, uint16_t pasid)
764{
765	struct amdgpu_device *adev = (struct amdgpu_device *) kgd;
766	int vmid;
767	unsigned int tmp;
768
769	if (adev->in_gpu_reset)
770		return -EIO;
771
772	for (vmid = 0; vmid < 16; vmid++) {
773		if (!amdgpu_amdkfd_is_kfd_vmid(adev, vmid))
774			continue;
775
776		tmp = RREG32(mmATC_VMID0_PASID_MAPPING + vmid);
777		if ((tmp & ATC_VMID0_PASID_MAPPING__VALID_MASK) &&
778			(tmp & ATC_VMID0_PASID_MAPPING__PASID_MASK) == pasid) {
779			WREG32(mmVM_INVALIDATE_REQUEST, 1 << vmid);
780			RREG32(mmVM_INVALIDATE_RESPONSE);
781			break;
782		}
783	}
784
785	return 0;
786}
787
788static int invalidate_tlbs_vmid(struct kgd_dev *kgd, uint16_t vmid)
789{
790	struct amdgpu_device *adev = (struct amdgpu_device *) kgd;
791
792	if (!amdgpu_amdkfd_is_kfd_vmid(adev, vmid)) {
793		pr_err("non kfd vmid %d\n", vmid);
794		return -EINVAL;
795	}
796
797	WREG32(mmVM_INVALIDATE_REQUEST, 1 << vmid);
798	RREG32(mmVM_INVALIDATE_RESPONSE);
799	return 0;
800}