amdgpu_amdkfd_gfx_v10.c 22.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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
/*
 * Copyright 2019 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.
 */
#include "amdgpu.h"
#include "amdgpu_amdkfd.h"
#include "gc/gc_10_1_0_offset.h"
#include "gc/gc_10_1_0_sh_mask.h"
#include "athub/athub_2_0_0_offset.h"
#include "athub/athub_2_0_0_sh_mask.h"
#include "oss/osssys_5_0_0_offset.h"
#include "oss/osssys_5_0_0_sh_mask.h"
#include "soc15_common.h"
#include "v10_structs.h"
#include "nv.h"
#include "nvd.h"

enum hqd_dequeue_request_type {
	NO_ACTION = 0,
	DRAIN_PIPE,
	RESET_WAVES,
	SAVE_WAVES
};

42
static void lock_srbm(struct amdgpu_device *adev, uint32_t mec, uint32_t pipe,
43 44 45 46 47 48
			uint32_t queue, uint32_t vmid)
{
	mutex_lock(&adev->srbm_mutex);
	nv_grbm_select(adev, mec, pipe, queue, vmid);
}

49
static void unlock_srbm(struct amdgpu_device *adev)
50 51 52 53 54
{
	nv_grbm_select(adev, 0, 0, 0, 0);
	mutex_unlock(&adev->srbm_mutex);
}

55
static void acquire_queue(struct amdgpu_device *adev, uint32_t pipe_id,
56 57 58 59 60
				uint32_t queue_id)
{
	uint32_t mec = (pipe_id / adev->gfx.mec.num_pipe_per_mec) + 1;
	uint32_t pipe = (pipe_id % adev->gfx.mec.num_pipe_per_mec);

61
	lock_srbm(adev, mec, pipe, queue_id, 0);
62 63
}

64
static uint64_t get_queue_mask(struct amdgpu_device *adev,
65 66
			       uint32_t pipe_id, uint32_t queue_id)
{
67 68
	unsigned int bit = pipe_id * adev->gfx.mec.num_queue_per_pipe +
			queue_id;
69

70
	return 1ull << bit;
71 72
}

73
static void release_queue(struct amdgpu_device *adev)
74
{
75
	unlock_srbm(adev);
76 77
}

78
static void kgd_program_sh_mem_settings(struct amdgpu_device *adev, uint32_t vmid,
79 80 81 82 83
					uint32_t sh_mem_config,
					uint32_t sh_mem_ape1_base,
					uint32_t sh_mem_ape1_limit,
					uint32_t sh_mem_bases)
{
84
	lock_srbm(adev, 0, 0, 0, vmid);
85

86 87
	WREG32_SOC15(GC, 0, mmSH_MEM_CONFIG, sh_mem_config);
	WREG32_SOC15(GC, 0, mmSH_MEM_BASES, sh_mem_bases);
88 89
	/* APE1 no longer exists on GFX9 */

90
	unlock_srbm(adev);
91 92
}

93
static int kgd_set_pasid_vmid_mapping(struct amdgpu_device *adev, u32 pasid,
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
					unsigned int vmid)
{
	/*
	 * We have to assume that there is no outstanding mapping.
	 * The ATC_VMID_PASID_MAPPING_UPDATE_STATUS bit could be 0 because
	 * a mapping is in progress or because a mapping finished
	 * and the SW cleared it.
	 * So the protocol is to always wait & clear.
	 */
	uint32_t pasid_mapping = (pasid == 0) ? 0 : (uint32_t)pasid |
			ATC_VMID0_PASID_MAPPING__VALID_MASK;

	pr_debug("pasid 0x%x vmid %d, reg value %x\n", pasid, vmid, pasid_mapping);

	pr_debug("ATHUB, reg %x\n", SOC15_REG_OFFSET(ATHUB, 0, mmATC_VMID0_PASID_MAPPING) + vmid);
	WREG32(SOC15_REG_OFFSET(ATHUB, 0, mmATC_VMID0_PASID_MAPPING) + vmid,
	       pasid_mapping);

#if 0
	/* TODO: uncomment this code when the hardware support is ready. */
	while (!(RREG32(SOC15_REG_OFFSET(
				ATHUB, 0,
				mmATC_VMID_PASID_MAPPING_UPDATE_STATUS)) &
		 (1U << vmid)))
		cpu_relax();

	pr_debug("ATHUB mapping update finished\n");
	WREG32(SOC15_REG_OFFSET(ATHUB, 0,
				mmATC_VMID_PASID_MAPPING_UPDATE_STATUS),
	       1U << vmid);
#endif

	/* Mapping vmid to pasid also for IH block */
	pr_debug("update mapping for IH block and mmhub");
	WREG32(SOC15_REG_OFFSET(OSSSYS, 0, mmIH_VMID_0_LUT) + vmid,
	       pasid_mapping);

	return 0;
}

/* TODO - RING0 form of field is obsolete, seems to date back to SI
 * but still works
 */

138
static int kgd_init_interrupts(struct amdgpu_device *adev, uint32_t pipe_id)
139 140 141 142 143 144 145
{
	uint32_t mec;
	uint32_t pipe;

	mec = (pipe_id / adev->gfx.mec.num_pipe_per_mec) + 1;
	pipe = (pipe_id % adev->gfx.mec.num_pipe_per_mec);

146
	lock_srbm(adev, mec, pipe, 0, 0);
147

148
	WREG32_SOC15(GC, 0, mmCPC_INT_CNTL,
149 150 151
		CP_INT_CNTL_RING0__TIME_STAMP_INT_ENABLE_MASK |
		CP_INT_CNTL_RING0__OPCODE_ERROR_INT_ENABLE_MASK);

152
	unlock_srbm(adev);
153 154 155 156

	return 0;
}

157
static uint32_t get_sdma_rlc_reg_offset(struct amdgpu_device *adev,
158 159 160
				unsigned int engine_id,
				unsigned int queue_id)
{
161
	uint32_t sdma_engine_reg_base[2] = {
162 163 164 165 166 167 168 169 170 171 172 173
		SOC15_REG_OFFSET(SDMA0, 0,
				 mmSDMA0_RLC0_RB_CNTL) - mmSDMA0_RLC0_RB_CNTL,
		/* On gfx10, mmSDMA1_xxx registers are defined NOT based
		 * on SDMA1 base address (dw 0x1860) but based on SDMA0
		 * base address (dw 0x1260). Therefore use mmSDMA0_RLC0_RB_CNTL
		 * instead of mmSDMA1_RLC0_RB_CNTL for the base address calc
		 * below
		 */
		SOC15_REG_OFFSET(SDMA1, 0,
				 mmSDMA1_RLC0_RB_CNTL) - mmSDMA0_RLC0_RB_CNTL
	};

174 175
	uint32_t retval = sdma_engine_reg_base[engine_id]
		+ queue_id * (mmSDMA0_RLC1_RB_CNTL - mmSDMA0_RLC0_RB_CNTL);
176

177 178
	pr_debug("RLC register offset for SDMA%d RLC%d: 0x%x\n", engine_id,
			queue_id, retval);
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204

	return retval;
}

#if 0
static uint32_t get_watch_base_addr(struct amdgpu_device *adev)
{
	uint32_t retval = SOC15_REG_OFFSET(GC, 0, mmTCP_WATCH0_ADDR_H) -
			mmTCP_WATCH0_ADDR_H;

	pr_debug("kfd: reg watch base address: 0x%x\n", retval);

	return retval;
}
#endif

static inline struct v10_compute_mqd *get_mqd(void *mqd)
{
	return (struct v10_compute_mqd *)mqd;
}

static inline struct v10_sdma_mqd *get_sdma_mqd(void *mqd)
{
	return (struct v10_sdma_mqd *)mqd;
}

205 206 207 208
static int kgd_hqd_load(struct amdgpu_device *adev, void *mqd,
			uint32_t pipe_id, uint32_t queue_id,
			uint32_t __user *wptr, uint32_t wptr_shift,
			uint32_t wptr_mask, struct mm_struct *mm)
209 210 211 212 213 214 215 216
{
	struct v10_compute_mqd *m;
	uint32_t *mqd_hqd;
	uint32_t reg, hqd_base, data;

	m = get_mqd(mqd);

	pr_debug("Load hqd of pipe %d queue %d\n", pipe_id, queue_id);
217
	acquire_queue(adev, pipe_id, queue_id);
218 219 220 221 222 223 224

	/* HQD registers extend from CP_MQD_BASE_ADDR to CP_HQD_EOP_WPTR_MEM. */
	mqd_hqd = &m->cp_mqd_base_addr_lo;
	hqd_base = SOC15_REG_OFFSET(GC, 0, mmCP_MQD_BASE_ADDR);

	for (reg = hqd_base;
	     reg <= SOC15_REG_OFFSET(GC, 0, mmCP_HQD_PQ_WPTR_HI); reg++)
225
		WREG32_SOC15_IP(GC, reg, mqd_hqd[reg - hqd_base]);
226 227 228 229 230


	/* Activate doorbell logic before triggering WPTR poll. */
	data = REG_SET_FIELD(m->cp_hqd_pq_doorbell_control,
			     CP_HQD_PQ_DOORBELL_CONTROL, DOORBELL_EN, 1);
231
	WREG32_SOC15(GC, 0, mmCP_HQD_PQ_DOORBELL_CONTROL, data);
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259

	if (wptr) {
		/* Don't read wptr with get_user because the user
		 * context may not be accessible (if this function
		 * runs in a work queue). Instead trigger a one-shot
		 * polling read from memory in the CP. This assumes
		 * that wptr is GPU-accessible in the queue's VMID via
		 * ATC or SVM. WPTR==RPTR before starting the poll so
		 * the CP starts fetching new commands from the right
		 * place.
		 *
		 * Guessing a 64-bit WPTR from a 32-bit RPTR is a bit
		 * tricky. Assume that the queue didn't overflow. The
		 * number of valid bits in the 32-bit RPTR depends on
		 * the queue size. The remaining bits are taken from
		 * the saved 64-bit WPTR. If the WPTR wrapped, add the
		 * queue size.
		 */
		uint32_t queue_size =
			2 << REG_GET_FIELD(m->cp_hqd_pq_control,
					   CP_HQD_PQ_CONTROL, QUEUE_SIZE);
		uint64_t guessed_wptr = m->cp_hqd_pq_rptr & (queue_size - 1);

		if ((m->cp_hqd_pq_wptr_lo & (queue_size - 1)) < guessed_wptr)
			guessed_wptr += queue_size;
		guessed_wptr += m->cp_hqd_pq_wptr_lo & ~(queue_size - 1);
		guessed_wptr += (uint64_t)m->cp_hqd_pq_wptr_hi << 32;

260
		WREG32_SOC15(GC, 0, mmCP_HQD_PQ_WPTR_LO,
261
		       lower_32_bits(guessed_wptr));
262
		WREG32_SOC15(GC, 0, mmCP_HQD_PQ_WPTR_HI,
263
		       upper_32_bits(guessed_wptr));
264
		WREG32_SOC15(GC, 0, mmCP_HQD_PQ_WPTR_POLL_ADDR,
265
		       lower_32_bits((uint64_t)wptr));
266
		WREG32_SOC15(GC, 0, mmCP_HQD_PQ_WPTR_POLL_ADDR_HI,
267
		       upper_32_bits((uint64_t)wptr));
268 269
		pr_debug("%s setting CP_PQ_WPTR_POLL_CNTL1 to %x\n", __func__,
			 (uint32_t)get_queue_mask(adev, pipe_id, queue_id));
270
		WREG32_SOC15(GC, 0, mmCP_PQ_WPTR_POLL_CNTL1,
271
		       (uint32_t)get_queue_mask(adev, pipe_id, queue_id));
272 273 274
	}

	/* Start the EOP fetcher */
275
	WREG32_SOC15(GC, 0, mmCP_HQD_EOP_RPTR,
276 277 278 279
	       REG_SET_FIELD(m->cp_hqd_eop_rptr,
			     CP_HQD_EOP_RPTR, INIT_FETCHER, 1));

	data = REG_SET_FIELD(m->cp_hqd_active, CP_HQD_ACTIVE, ACTIVE, 1);
280
	WREG32_SOC15(GC, 0, mmCP_HQD_ACTIVE, data);
281

282
	release_queue(adev);
283 284 285 286

	return 0;
}

287
static int kgd_hiq_mqd_load(struct amdgpu_device *adev, void *mqd,
288 289 290 291 292 293 294 295 296 297
			    uint32_t pipe_id, uint32_t queue_id,
			    uint32_t doorbell_off)
{
	struct amdgpu_ring *kiq_ring = &adev->gfx.kiq.ring;
	struct v10_compute_mqd *m;
	uint32_t mec, pipe;
	int r;

	m = get_mqd(mqd);

298
	acquire_queue(adev, pipe_id, queue_id);
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333

	mec = (pipe_id / adev->gfx.mec.num_pipe_per_mec) + 1;
	pipe = (pipe_id % adev->gfx.mec.num_pipe_per_mec);

	pr_debug("kfd: set HIQ, mec:%d, pipe:%d, queue:%d.\n",
		 mec, pipe, queue_id);

	spin_lock(&adev->gfx.kiq.ring_lock);
	r = amdgpu_ring_alloc(kiq_ring, 7);
	if (r) {
		pr_err("Failed to alloc KIQ (%d).\n", r);
		goto out_unlock;
	}

	amdgpu_ring_write(kiq_ring, PACKET3(PACKET3_MAP_QUEUES, 5));
	amdgpu_ring_write(kiq_ring,
			  PACKET3_MAP_QUEUES_QUEUE_SEL(0) | /* Queue_Sel */
			  PACKET3_MAP_QUEUES_VMID(m->cp_hqd_vmid) | /* VMID */
			  PACKET3_MAP_QUEUES_QUEUE(queue_id) |
			  PACKET3_MAP_QUEUES_PIPE(pipe) |
			  PACKET3_MAP_QUEUES_ME((mec - 1)) |
			  PACKET3_MAP_QUEUES_QUEUE_TYPE(0) | /*queue_type: normal compute queue */
			  PACKET3_MAP_QUEUES_ALLOC_FORMAT(0) | /* alloc format: all_on_one_pipe */
			  PACKET3_MAP_QUEUES_ENGINE_SEL(1) | /* engine_sel: hiq */
			  PACKET3_MAP_QUEUES_NUM_QUEUES(1)); /* num_queues: must be 1 */
	amdgpu_ring_write(kiq_ring,
			  PACKET3_MAP_QUEUES_DOORBELL_OFFSET(doorbell_off));
	amdgpu_ring_write(kiq_ring, m->cp_mqd_base_addr_lo);
	amdgpu_ring_write(kiq_ring, m->cp_mqd_base_addr_hi);
	amdgpu_ring_write(kiq_ring, m->cp_hqd_pq_wptr_poll_addr_lo);
	amdgpu_ring_write(kiq_ring, m->cp_hqd_pq_wptr_poll_addr_hi);
	amdgpu_ring_commit(kiq_ring);

out_unlock:
	spin_unlock(&adev->gfx.kiq.ring_lock);
334
	release_queue(adev);
335 336 337 338

	return r;
}

339
static int kgd_hqd_dump(struct amdgpu_device *adev,
340 341 342 343 344 345 346 347 348
			uint32_t pipe_id, uint32_t queue_id,
			uint32_t (**dump)[2], uint32_t *n_regs)
{
	uint32_t i = 0, reg;
#define HQD_N_REGS 56
#define DUMP_REG(addr) do {				\
		if (WARN_ON_ONCE(i >= HQD_N_REGS))	\
			break;				\
		(*dump)[i][0] = (addr) << 2;		\
349
		(*dump)[i++][1] = RREG32_SOC15_IP(GC, addr);		\
350 351 352 353 354 355
	} while (0)

	*dump = kmalloc(HQD_N_REGS*2*sizeof(uint32_t), GFP_KERNEL);
	if (*dump == NULL)
		return -ENOMEM;

356
	acquire_queue(adev, pipe_id, queue_id);
357 358 359 360 361

	for (reg = SOC15_REG_OFFSET(GC, 0, mmCP_MQD_BASE_ADDR);
	     reg <= SOC15_REG_OFFSET(GC, 0, mmCP_HQD_PQ_WPTR_HI); reg++)
		DUMP_REG(reg);

362
	release_queue(adev);
363 364 365 366 367 368 369

	WARN_ON_ONCE(i != HQD_N_REGS);
	*n_regs = i;

	return 0;
}

370
static int kgd_hqd_sdma_load(struct amdgpu_device *adev, void *mqd,
371 372 373
			     uint32_t __user *wptr, struct mm_struct *mm)
{
	struct v10_sdma_mqd *m;
374
	uint32_t sdma_rlc_reg_offset;
375 376 377 378 379 380
	unsigned long end_jiffies;
	uint32_t data;
	uint64_t data64;
	uint64_t __user *wptr64 = (uint64_t __user *)wptr;

	m = get_sdma_mqd(mqd);
381
	sdma_rlc_reg_offset = get_sdma_rlc_reg_offset(adev, m->sdma_engine_id,
382 383
					    m->sdma_queue_id);

384
	WREG32(sdma_rlc_reg_offset + mmSDMA0_RLC0_RB_CNTL,
385 386 387 388
		m->sdmax_rlcx_rb_cntl & (~SDMA0_RLC0_RB_CNTL__RB_ENABLE_MASK));

	end_jiffies = msecs_to_jiffies(2000) + jiffies;
	while (true) {
389
		data = RREG32(sdma_rlc_reg_offset + mmSDMA0_RLC0_CONTEXT_STATUS);
390 391
		if (data & SDMA0_RLC0_CONTEXT_STATUS__IDLE_MASK)
			break;
392 393
		if (time_after(jiffies, end_jiffies)) {
			pr_err("SDMA RLC not idle in %s\n", __func__);
394
			return -ETIME;
395
		}
396 397 398
		usleep_range(500, 1000);
	}

399
	WREG32(sdma_rlc_reg_offset + mmSDMA0_RLC0_DOORBELL_OFFSET,
400 401 402 403
	       m->sdmax_rlcx_doorbell_offset);

	data = REG_SET_FIELD(m->sdmax_rlcx_doorbell, SDMA0_RLC0_DOORBELL,
			     ENABLE, 1);
404 405 406 407
	WREG32(sdma_rlc_reg_offset + mmSDMA0_RLC0_DOORBELL, data);
	WREG32(sdma_rlc_reg_offset + mmSDMA0_RLC0_RB_RPTR,
				m->sdmax_rlcx_rb_rptr);
	WREG32(sdma_rlc_reg_offset + mmSDMA0_RLC0_RB_RPTR_HI,
408 409
				m->sdmax_rlcx_rb_rptr_hi);

410
	WREG32(sdma_rlc_reg_offset + mmSDMA0_RLC0_MINOR_PTR_UPDATE, 1);
411
	if (read_user_wptr(mm, wptr64, data64)) {
412
		WREG32(sdma_rlc_reg_offset + mmSDMA0_RLC0_RB_WPTR,
413
		       lower_32_bits(data64));
414
		WREG32(sdma_rlc_reg_offset + mmSDMA0_RLC0_RB_WPTR_HI,
415 416
		       upper_32_bits(data64));
	} else {
417
		WREG32(sdma_rlc_reg_offset + mmSDMA0_RLC0_RB_WPTR,
418
		       m->sdmax_rlcx_rb_rptr);
419
		WREG32(sdma_rlc_reg_offset + mmSDMA0_RLC0_RB_WPTR_HI,
420 421
		       m->sdmax_rlcx_rb_rptr_hi);
	}
422
	WREG32(sdma_rlc_reg_offset + mmSDMA0_RLC0_MINOR_PTR_UPDATE, 0);
423

424 425
	WREG32(sdma_rlc_reg_offset + mmSDMA0_RLC0_RB_BASE, m->sdmax_rlcx_rb_base);
	WREG32(sdma_rlc_reg_offset + mmSDMA0_RLC0_RB_BASE_HI,
426
			m->sdmax_rlcx_rb_base_hi);
427
	WREG32(sdma_rlc_reg_offset + mmSDMA0_RLC0_RB_RPTR_ADDR_LO,
428
			m->sdmax_rlcx_rb_rptr_addr_lo);
429
	WREG32(sdma_rlc_reg_offset + mmSDMA0_RLC0_RB_RPTR_ADDR_HI,
430 431 432 433
			m->sdmax_rlcx_rb_rptr_addr_hi);

	data = REG_SET_FIELD(m->sdmax_rlcx_rb_cntl, SDMA0_RLC0_RB_CNTL,
			     RB_ENABLE, 1);
434
	WREG32(sdma_rlc_reg_offset + mmSDMA0_RLC0_RB_CNTL, data);
435 436 437 438

	return 0;
}

439
static int kgd_hqd_sdma_dump(struct amdgpu_device *adev,
440 441 442
			     uint32_t engine_id, uint32_t queue_id,
			     uint32_t (**dump)[2], uint32_t *n_regs)
{
443 444
	uint32_t sdma_rlc_reg_offset = get_sdma_rlc_reg_offset(adev,
			engine_id, queue_id);
445 446 447 448 449 450 451 452 453
	uint32_t i = 0, reg;
#undef HQD_N_REGS
#define HQD_N_REGS (19+6+7+10)

	*dump = kmalloc(HQD_N_REGS*2*sizeof(uint32_t), GFP_KERNEL);
	if (*dump == NULL)
		return -ENOMEM;

	for (reg = mmSDMA0_RLC0_RB_CNTL; reg <= mmSDMA0_RLC0_DOORBELL; reg++)
454
		DUMP_REG(sdma_rlc_reg_offset + reg);
455
	for (reg = mmSDMA0_RLC0_STATUS; reg <= mmSDMA0_RLC0_CSA_ADDR_HI; reg++)
456
		DUMP_REG(sdma_rlc_reg_offset + reg);
457 458
	for (reg = mmSDMA0_RLC0_IB_SUB_REMAIN;
	     reg <= mmSDMA0_RLC0_MINOR_PTR_UPDATE; reg++)
459
		DUMP_REG(sdma_rlc_reg_offset + reg);
460 461
	for (reg = mmSDMA0_RLC0_MIDCMD_DATA0;
	     reg <= mmSDMA0_RLC0_MIDCMD_CNTL; reg++)
462
		DUMP_REG(sdma_rlc_reg_offset + reg);
463 464 465 466 467 468 469

	WARN_ON_ONCE(i != HQD_N_REGS);
	*n_regs = i;

	return 0;
}

470 471 472
static bool kgd_hqd_is_occupied(struct amdgpu_device *adev,
				uint64_t queue_address, uint32_t pipe_id,
				uint32_t queue_id)
473 474 475 476 477
{
	uint32_t act;
	bool retval = false;
	uint32_t low, high;

478
	acquire_queue(adev, pipe_id, queue_id);
479
	act = RREG32_SOC15(GC, 0, mmCP_HQD_ACTIVE);
480 481 482 483
	if (act) {
		low = lower_32_bits(queue_address >> 8);
		high = upper_32_bits(queue_address >> 8);

484 485
		if (low == RREG32_SOC15(GC, 0, mmCP_HQD_PQ_BASE) &&
		   high == RREG32_SOC15(GC, 0, mmCP_HQD_PQ_BASE_HI))
486 487
			retval = true;
	}
488
	release_queue(adev);
489 490 491
	return retval;
}

492
static bool kgd_hqd_sdma_is_occupied(struct amdgpu_device *adev, void *mqd)
493 494
{
	struct v10_sdma_mqd *m;
495
	uint32_t sdma_rlc_reg_offset;
496 497 498
	uint32_t sdma_rlc_rb_cntl;

	m = get_sdma_mqd(mqd);
499
	sdma_rlc_reg_offset = get_sdma_rlc_reg_offset(adev, m->sdma_engine_id,
500 501
					    m->sdma_queue_id);

502
	sdma_rlc_rb_cntl = RREG32(sdma_rlc_reg_offset + mmSDMA0_RLC0_RB_CNTL);
503 504 505 506 507 508 509

	if (sdma_rlc_rb_cntl & SDMA0_RLC0_RB_CNTL__RB_ENABLE_MASK)
		return true;

	return false;
}

510
static int kgd_hqd_destroy(struct amdgpu_device *adev, void *mqd,
511 512 513 514 515 516 517 518 519
				enum kfd_preempt_type reset_type,
				unsigned int utimeout, uint32_t pipe_id,
				uint32_t queue_id)
{
	enum hqd_dequeue_request_type type;
	unsigned long end_jiffies;
	uint32_t temp;
	struct v10_compute_mqd *m = get_mqd(mqd);

520
	if (amdgpu_in_reset(adev))
521
		return -EIO;
522

523 524 525 526 527
#if 0
	unsigned long flags;
	int retry;
#endif

528
	acquire_queue(adev, pipe_id, queue_id);
529 530 531 532 533 534 535 536 537 538 539

	if (m->cp_hqd_vmid == 0)
		WREG32_FIELD15(GC, 0, RLC_CP_SCHEDULERS, scheduler1, 0);

	switch (reset_type) {
	case KFD_PREEMPT_TYPE_WAVEFRONT_DRAIN:
		type = DRAIN_PIPE;
		break;
	case KFD_PREEMPT_TYPE_WAVEFRONT_RESET:
		type = RESET_WAVES;
		break;
540 541 542
	case KFD_PREEMPT_TYPE_WAVEFRONT_SAVE:
		type = SAVE_WAVES;
		break;
543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
	default:
		type = DRAIN_PIPE;
		break;
	}

#if 0 /* Is this still needed? */
	/* Workaround: If IQ timer is active and the wait time is close to or
	 * equal to 0, dequeueing is not safe. Wait until either the wait time
	 * is larger or timer is cleared. Also, ensure that IQ_REQ_PEND is
	 * cleared before continuing. Also, ensure wait times are set to at
	 * least 0x3.
	 */
	local_irq_save(flags);
	preempt_disable();
	retry = 5000; /* wait for 500 usecs at maximum */
	while (true) {
		temp = RREG32(mmCP_HQD_IQ_TIMER);
		if (REG_GET_FIELD(temp, CP_HQD_IQ_TIMER, PROCESSING_IQ)) {
			pr_debug("HW is processing IQ\n");
			goto loop;
		}
		if (REG_GET_FIELD(temp, CP_HQD_IQ_TIMER, ACTIVE)) {
			if (REG_GET_FIELD(temp, CP_HQD_IQ_TIMER, RETRY_TYPE)
					== 3) /* SEM-rearm is safe */
				break;
			/* Wait time 3 is safe for CP, but our MMIO read/write
			 * time is close to 1 microsecond, so check for 10 to
			 * leave more buffer room
			 */
			if (REG_GET_FIELD(temp, CP_HQD_IQ_TIMER, WAIT_TIME)
					>= 10)
				break;
			pr_debug("IQ timer is active\n");
		} else
			break;
loop:
		if (!retry) {
			pr_err("CP HQD IQ timer status time out\n");
			break;
		}
		ndelay(100);
		--retry;
	}
	retry = 1000;
	while (true) {
		temp = RREG32(mmCP_HQD_DEQUEUE_REQUEST);
		if (!(temp & CP_HQD_DEQUEUE_REQUEST__IQ_REQ_PEND_MASK))
			break;
		pr_debug("Dequeue request is pending\n");

		if (!retry) {
			pr_err("CP HQD dequeue request time out\n");
			break;
		}
		ndelay(100);
		--retry;
	}
	local_irq_restore(flags);
	preempt_enable();
#endif

604
	WREG32_SOC15(GC, 0, mmCP_HQD_DEQUEUE_REQUEST, type);
605 606 607

	end_jiffies = (utimeout * HZ / 1000) + jiffies;
	while (true) {
608
		temp = RREG32_SOC15(GC, 0, mmCP_HQD_ACTIVE);
609 610 611 612
		if (!(temp & CP_HQD_ACTIVE__ACTIVE_MASK))
			break;
		if (time_after(jiffies, end_jiffies)) {
			pr_err("cp queue preemption time out.\n");
613
			release_queue(adev);
614 615 616 617 618
			return -ETIME;
		}
		usleep_range(500, 1000);
	}

619
	release_queue(adev);
620 621 622
	return 0;
}

623
static int kgd_hqd_sdma_destroy(struct amdgpu_device *adev, void *mqd,
624 625 626
				unsigned int utimeout)
{
	struct v10_sdma_mqd *m;
627
	uint32_t sdma_rlc_reg_offset;
628 629 630 631
	uint32_t temp;
	unsigned long end_jiffies = (utimeout * HZ / 1000) + jiffies;

	m = get_sdma_mqd(mqd);
632
	sdma_rlc_reg_offset = get_sdma_rlc_reg_offset(adev, m->sdma_engine_id,
633 634
					    m->sdma_queue_id);

635
	temp = RREG32(sdma_rlc_reg_offset + mmSDMA0_RLC0_RB_CNTL);
636
	temp = temp & ~SDMA0_RLC0_RB_CNTL__RB_ENABLE_MASK;
637
	WREG32(sdma_rlc_reg_offset + mmSDMA0_RLC0_RB_CNTL, temp);
638 639

	while (true) {
640
		temp = RREG32(sdma_rlc_reg_offset + mmSDMA0_RLC0_CONTEXT_STATUS);
641 642
		if (temp & SDMA0_RLC0_CONTEXT_STATUS__IDLE_MASK)
			break;
643 644
		if (time_after(jiffies, end_jiffies)) {
			pr_err("SDMA RLC not idle in %s\n", __func__);
645
			return -ETIME;
646
		}
647 648 649
		usleep_range(500, 1000);
	}

650 651 652
	WREG32(sdma_rlc_reg_offset + mmSDMA0_RLC0_DOORBELL, 0);
	WREG32(sdma_rlc_reg_offset + mmSDMA0_RLC0_RB_CNTL,
		RREG32(sdma_rlc_reg_offset + mmSDMA0_RLC0_RB_CNTL) |
653 654
		SDMA0_RLC0_RB_CNTL__RB_ENABLE_MASK);

655
	m->sdmax_rlcx_rb_rptr = RREG32(sdma_rlc_reg_offset + mmSDMA0_RLC0_RB_RPTR);
656
	m->sdmax_rlcx_rb_rptr_hi =
657
		RREG32(sdma_rlc_reg_offset + mmSDMA0_RLC0_RB_RPTR_HI);
658 659 660 661

	return 0;
}

662
static bool get_atc_vmid_pasid_mapping_info(struct amdgpu_device *adev,
663
					uint8_t vmid, uint16_t *p_pasid)
664
{
665
	uint32_t value;
666

667
	value = RREG32(SOC15_REG_OFFSET(ATHUB, 0, mmATC_VMID0_PASID_MAPPING)
668
		     + vmid);
669
	*p_pasid = value & ATC_VMID0_PASID_MAPPING__PASID_MASK;
670

671
	return !!(value & ATC_VMID0_PASID_MAPPING__VALID_MASK);
672 673
}

674
static int kgd_address_watch_disable(struct amdgpu_device *adev)
675 676 677 678
{
	return 0;
}

679
static int kgd_address_watch_execute(struct amdgpu_device *adev,
680 681 682 683 684 685 686 687
					unsigned int watch_point_id,
					uint32_t cntl_val,
					uint32_t addr_hi,
					uint32_t addr_lo)
{
	return 0;
}

688
static int kgd_wave_control_execute(struct amdgpu_device *adev,
689 690 691 692 693 694 695
					uint32_t gfx_index_val,
					uint32_t sq_cmd)
{
	uint32_t data = 0;

	mutex_lock(&adev->grbm_idx_mutex);

696 697
	WREG32_SOC15(GC, 0, mmGRBM_GFX_INDEX, gfx_index_val);
	WREG32_SOC15(GC, 0, mmSQ_CMD, sq_cmd);
698 699 700 701 702 703 704 705

	data = REG_SET_FIELD(data, GRBM_GFX_INDEX,
		INSTANCE_BROADCAST_WRITES, 1);
	data = REG_SET_FIELD(data, GRBM_GFX_INDEX,
		SA_BROADCAST_WRITES, 1);
	data = REG_SET_FIELD(data, GRBM_GFX_INDEX,
		SE_BROADCAST_WRITES, 1);

706
	WREG32_SOC15(GC, 0, mmGRBM_GFX_INDEX, data);
707 708 709 710 711
	mutex_unlock(&adev->grbm_idx_mutex);

	return 0;
}

712
static uint32_t kgd_address_watch_get_offset(struct amdgpu_device *adev,
713 714 715 716 717 718
					unsigned int watch_point_id,
					unsigned int reg_offset)
{
	return 0;
}

719 720
static void set_vm_context_page_table_base(struct amdgpu_device *adev,
		uint32_t vmid, uint64_t page_table_base)
721 722 723 724 725 726 727
{
	if (!amdgpu_amdkfd_is_kfd_vmid(adev, vmid)) {
		pr_err("trying to set page table base for wrong VMID %u\n",
		       vmid);
		return;
	}

728
	/* SDMA is on gfxhub as well for Navi1* series */
729
	adev->gfxhub.funcs->setup_vm_pt_regs(adev, vmid, page_table_base);
730
}
731

732
static void program_trap_handler_settings(struct amdgpu_device *adev,
733 734
		uint32_t vmid, uint64_t tba_addr, uint64_t tma_addr)
{
735
	lock_srbm(adev, 0, 0, 0, vmid);
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753

	/*
	 * Program TBA registers
	 */
	WREG32(SOC15_REG_OFFSET(GC, 0, mmSQ_SHADER_TBA_LO),
			lower_32_bits(tba_addr >> 8));
	WREG32(SOC15_REG_OFFSET(GC, 0, mmSQ_SHADER_TBA_HI),
			upper_32_bits(tba_addr >> 8) |
			(1 << SQ_SHADER_TBA_HI__TRAP_EN__SHIFT));

	/*
	 * Program TMA registers
	 */
	WREG32(SOC15_REG_OFFSET(GC, 0, mmSQ_SHADER_TMA_LO),
			lower_32_bits(tma_addr >> 8));
	WREG32(SOC15_REG_OFFSET(GC, 0, mmSQ_SHADER_TMA_HI),
			upper_32_bits(tma_addr >> 8));

754
	unlock_srbm(adev);
755 756
}

757
const struct kfd2kgd_calls gfx_v10_kfd2kgd = {
758 759 760 761
	.program_sh_mem_settings = kgd_program_sh_mem_settings,
	.set_pasid_vmid_mapping = kgd_set_pasid_vmid_mapping,
	.init_interrupts = kgd_init_interrupts,
	.hqd_load = kgd_hqd_load,
762
	.hiq_mqd_load = kgd_hiq_mqd_load,
763 764 765 766 767 768 769 770 771 772 773 774 775 776
	.hqd_sdma_load = kgd_hqd_sdma_load,
	.hqd_dump = kgd_hqd_dump,
	.hqd_sdma_dump = kgd_hqd_sdma_dump,
	.hqd_is_occupied = kgd_hqd_is_occupied,
	.hqd_sdma_is_occupied = kgd_hqd_sdma_is_occupied,
	.hqd_destroy = kgd_hqd_destroy,
	.hqd_sdma_destroy = kgd_hqd_sdma_destroy,
	.address_watch_disable = kgd_address_watch_disable,
	.address_watch_execute = kgd_address_watch_execute,
	.wave_control_execute = kgd_wave_control_execute,
	.address_watch_get_offset = kgd_address_watch_get_offset,
	.get_atc_vmid_pasid_mapping_info =
			get_atc_vmid_pasid_mapping_info,
	.set_vm_context_page_table_base = set_vm_context_page_table_base,
777
	.program_trap_handler_settings = program_trap_handler_settings,
778
};