xref: /linux/drivers/gpu/drm/amd/amdkfd/kfd_mqd_manager_v9.c (revision bfb921b2a9d5d1123d1d10b196a39db629ddef87)
1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /*
3  * Copyright 2016-2022 Advanced Micro Devices, Inc.
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
19  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21  * OTHER DEALINGS IN THE SOFTWARE.
22  *
23  */
24 
25 #include <linux/printk.h>
26 #include <linux/slab.h>
27 #include <linux/uaccess.h>
28 #include "kfd_priv.h"
29 #include "kfd_mqd_manager.h"
30 #include "v9_structs.h"
31 #include "gc/gc_9_0_offset.h"
32 #include "gc/gc_9_0_sh_mask.h"
33 #include "sdma0/sdma0_4_0_sh_mask.h"
34 #include "amdgpu_amdkfd.h"
35 #include "kfd_device_queue_manager.h"
36 
37 static void update_mqd(struct mqd_manager *mm, void *mqd,
38 		       struct queue_properties *q,
39 		       struct mqd_update_info *minfo);
40 
41 static uint64_t mqd_stride_v9(struct mqd_manager *mm,
42 				struct queue_properties *q)
43 {
44 	if (mm->dev->kfd->cwsr_enabled &&
45 	    q->type == KFD_QUEUE_TYPE_COMPUTE)
46 		return ALIGN(q->ctl_stack_size, PAGE_SIZE) +
47 			ALIGN(sizeof(struct v9_mqd), PAGE_SIZE);
48 
49 	return mm->mqd_size;
50 }
51 
52 static inline struct v9_mqd *get_mqd(void *mqd)
53 {
54 	return (struct v9_mqd *)mqd;
55 }
56 
57 static inline struct v9_sdma_mqd *get_sdma_mqd(void *mqd)
58 {
59 	return (struct v9_sdma_mqd *)mqd;
60 }
61 
62 static void update_cu_mask(struct mqd_manager *mm, void *mqd,
63 			struct mqd_update_info *minfo, uint32_t inst)
64 {
65 	struct v9_mqd *m;
66 	uint32_t se_mask[KFD_MAX_NUM_SE] = {0};
67 
68 	if (!minfo || !minfo->cu_mask.ptr)
69 		return;
70 
71 	mqd_symmetrically_map_cu_mask(mm,
72 		minfo->cu_mask.ptr, minfo->cu_mask.count, se_mask, inst);
73 
74 	m = get_mqd(mqd);
75 
76 	m->compute_static_thread_mgmt_se0 = se_mask[0];
77 	m->compute_static_thread_mgmt_se1 = se_mask[1];
78 	m->compute_static_thread_mgmt_se2 = se_mask[2];
79 	m->compute_static_thread_mgmt_se3 = se_mask[3];
80 	if (KFD_GC_VERSION(mm->dev) != IP_VERSION(9, 4, 3)) {
81 		m->compute_static_thread_mgmt_se4 = se_mask[4];
82 		m->compute_static_thread_mgmt_se5 = se_mask[5];
83 		m->compute_static_thread_mgmt_se6 = se_mask[6];
84 		m->compute_static_thread_mgmt_se7 = se_mask[7];
85 
86 		pr_debug("update cu mask to %#x %#x %#x %#x %#x %#x %#x %#x\n",
87 			m->compute_static_thread_mgmt_se0,
88 			m->compute_static_thread_mgmt_se1,
89 			m->compute_static_thread_mgmt_se2,
90 			m->compute_static_thread_mgmt_se3,
91 			m->compute_static_thread_mgmt_se4,
92 			m->compute_static_thread_mgmt_se5,
93 			m->compute_static_thread_mgmt_se6,
94 			m->compute_static_thread_mgmt_se7);
95 	} else {
96 		pr_debug("inst: %u, update cu mask to %#x %#x %#x %#x\n",
97 			inst, m->compute_static_thread_mgmt_se0,
98 			m->compute_static_thread_mgmt_se1,
99 			m->compute_static_thread_mgmt_se2,
100 			m->compute_static_thread_mgmt_se3);
101 	}
102 }
103 
104 static void set_priority(struct v9_mqd *m, struct queue_properties *q)
105 {
106 	m->cp_hqd_pipe_priority = pipe_priority_map[q->priority];
107 	m->cp_hqd_queue_priority = q->priority;
108 }
109 
110 static struct kfd_mem_obj *allocate_mqd(struct kfd_node *node,
111 		struct queue_properties *q)
112 {
113 	int retval;
114 	struct kfd_mem_obj *mqd_mem_obj = NULL;
115 
116 	/* For V9 only, due to a HW bug, the control stack of a user mode
117 	 * compute queue needs to be allocated just behind the page boundary
118 	 * of its regular MQD buffer. So we allocate an enlarged MQD buffer:
119 	 * the first page of the buffer serves as the regular MQD buffer
120 	 * purpose and the remaining is for control stack. Although the two
121 	 * parts are in the same buffer object, they need different memory
122 	 * types: MQD part needs UC (uncached) as usual, while control stack
123 	 * needs NC (non coherent), which is different from the UC type which
124 	 * is used when control stack is allocated in user space.
125 	 *
126 	 * Because of all those, we use the gtt allocation function instead
127 	 * of sub-allocation function for this enlarged MQD buffer. Moreover,
128 	 * in order to achieve two memory types in a single buffer object, we
129 	 * pass a special bo flag AMDGPU_GEM_CREATE_CP_MQD_GFX9 to instruct
130 	 * amdgpu memory functions to do so.
131 	 */
132 	if (node->kfd->cwsr_enabled && (q->type == KFD_QUEUE_TYPE_COMPUTE)) {
133 		mqd_mem_obj = kzalloc(sizeof(struct kfd_mem_obj), GFP_KERNEL);
134 		if (!mqd_mem_obj)
135 			return NULL;
136 		retval = amdgpu_amdkfd_alloc_gtt_mem(node->adev,
137 			(ALIGN(q->ctl_stack_size, PAGE_SIZE) +
138 			ALIGN(sizeof(struct v9_mqd), PAGE_SIZE)) *
139 			NUM_XCC(node->xcc_mask),
140 			&(mqd_mem_obj->gtt_mem),
141 			&(mqd_mem_obj->gpu_addr),
142 			(void *)&(mqd_mem_obj->cpu_ptr), true);
143 
144 		if (retval) {
145 			kfree(mqd_mem_obj);
146 			return NULL;
147 		}
148 	} else {
149 		retval = kfd_gtt_sa_allocate(node, sizeof(struct v9_mqd),
150 				&mqd_mem_obj);
151 		if (retval)
152 			return NULL;
153 	}
154 
155 	return mqd_mem_obj;
156 }
157 
158 static void init_mqd(struct mqd_manager *mm, void **mqd,
159 			struct kfd_mem_obj *mqd_mem_obj, uint64_t *gart_addr,
160 			struct queue_properties *q)
161 {
162 	uint64_t addr;
163 	struct v9_mqd *m;
164 
165 	m = (struct v9_mqd *) mqd_mem_obj->cpu_ptr;
166 	addr = mqd_mem_obj->gpu_addr;
167 
168 	memset(m, 0, sizeof(struct v9_mqd));
169 
170 	m->header = 0xC0310800;
171 	m->compute_pipelinestat_enable = 1;
172 	m->compute_static_thread_mgmt_se0 = 0xFFFFFFFF;
173 	m->compute_static_thread_mgmt_se1 = 0xFFFFFFFF;
174 	m->compute_static_thread_mgmt_se2 = 0xFFFFFFFF;
175 	m->compute_static_thread_mgmt_se3 = 0xFFFFFFFF;
176 	m->compute_static_thread_mgmt_se4 = 0xFFFFFFFF;
177 	m->compute_static_thread_mgmt_se5 = 0xFFFFFFFF;
178 	m->compute_static_thread_mgmt_se6 = 0xFFFFFFFF;
179 	m->compute_static_thread_mgmt_se7 = 0xFFFFFFFF;
180 
181 	m->cp_hqd_persistent_state = CP_HQD_PERSISTENT_STATE__PRELOAD_REQ_MASK |
182 			0x53 << CP_HQD_PERSISTENT_STATE__PRELOAD_SIZE__SHIFT;
183 
184 	m->cp_mqd_control = 1 << CP_MQD_CONTROL__PRIV_STATE__SHIFT;
185 
186 	m->cp_mqd_base_addr_lo        = lower_32_bits(addr);
187 	m->cp_mqd_base_addr_hi        = upper_32_bits(addr);
188 
189 	m->cp_hqd_quantum = 1 << CP_HQD_QUANTUM__QUANTUM_EN__SHIFT |
190 			1 << CP_HQD_QUANTUM__QUANTUM_SCALE__SHIFT |
191 			1 << CP_HQD_QUANTUM__QUANTUM_DURATION__SHIFT;
192 
193 	/* Set cp_hqd_hq_scheduler0 bit 14 to 1 to have the CP set up the
194 	 * DISPATCH_PTR.  This is required for the kfd debugger
195 	 */
196 	m->cp_hqd_hq_status0 = 1 << 14;
197 
198 	if (q->format == KFD_QUEUE_FORMAT_AQL)
199 		m->cp_hqd_aql_control =
200 			1 << CP_HQD_AQL_CONTROL__CONTROL0__SHIFT;
201 
202 	if (q->tba_addr) {
203 		m->compute_pgm_rsrc2 |=
204 			(1 << COMPUTE_PGM_RSRC2__TRAP_PRESENT__SHIFT);
205 	}
206 
207 	if (mm->dev->kfd->cwsr_enabled && q->ctx_save_restore_area_address) {
208 		m->cp_hqd_persistent_state |=
209 			(1 << CP_HQD_PERSISTENT_STATE__QSWITCH_MODE__SHIFT);
210 		m->cp_hqd_ctx_save_base_addr_lo =
211 			lower_32_bits(q->ctx_save_restore_area_address);
212 		m->cp_hqd_ctx_save_base_addr_hi =
213 			upper_32_bits(q->ctx_save_restore_area_address);
214 		m->cp_hqd_ctx_save_size = q->ctx_save_restore_area_size;
215 		m->cp_hqd_cntl_stack_size = q->ctl_stack_size;
216 		m->cp_hqd_cntl_stack_offset = q->ctl_stack_size;
217 		m->cp_hqd_wg_state_offset = q->ctl_stack_size;
218 	}
219 
220 	*mqd = m;
221 	if (gart_addr)
222 		*gart_addr = addr;
223 	update_mqd(mm, m, q, NULL);
224 }
225 
226 static int load_mqd(struct mqd_manager *mm, void *mqd,
227 			uint32_t pipe_id, uint32_t queue_id,
228 			struct queue_properties *p, struct mm_struct *mms)
229 {
230 	/* AQL write pointer counts in 64B packets, PM4/CP counts in dwords. */
231 	uint32_t wptr_shift = (p->format == KFD_QUEUE_FORMAT_AQL ? 4 : 0);
232 
233 	return mm->dev->kfd2kgd->hqd_load(mm->dev->adev, mqd, pipe_id, queue_id,
234 					  (uint32_t __user *)p->write_ptr,
235 					  wptr_shift, 0, mms, 0);
236 }
237 
238 static void update_mqd(struct mqd_manager *mm, void *mqd,
239 			struct queue_properties *q,
240 			struct mqd_update_info *minfo)
241 {
242 	struct v9_mqd *m;
243 
244 	m = get_mqd(mqd);
245 
246 	m->cp_hqd_pq_control = 5 << CP_HQD_PQ_CONTROL__RPTR_BLOCK_SIZE__SHIFT;
247 	m->cp_hqd_pq_control |= order_base_2(q->queue_size / 4) - 1;
248 	pr_debug("cp_hqd_pq_control 0x%x\n", m->cp_hqd_pq_control);
249 
250 	m->cp_hqd_pq_base_lo = lower_32_bits((uint64_t)q->queue_address >> 8);
251 	m->cp_hqd_pq_base_hi = upper_32_bits((uint64_t)q->queue_address >> 8);
252 
253 	m->cp_hqd_pq_rptr_report_addr_lo = lower_32_bits((uint64_t)q->read_ptr);
254 	m->cp_hqd_pq_rptr_report_addr_hi = upper_32_bits((uint64_t)q->read_ptr);
255 	m->cp_hqd_pq_wptr_poll_addr_lo = lower_32_bits((uint64_t)q->write_ptr);
256 	m->cp_hqd_pq_wptr_poll_addr_hi = upper_32_bits((uint64_t)q->write_ptr);
257 
258 	m->cp_hqd_pq_doorbell_control =
259 		q->doorbell_off <<
260 			CP_HQD_PQ_DOORBELL_CONTROL__DOORBELL_OFFSET__SHIFT;
261 	pr_debug("cp_hqd_pq_doorbell_control 0x%x\n",
262 			m->cp_hqd_pq_doorbell_control);
263 
264 	m->cp_hqd_ib_control =
265 		3 << CP_HQD_IB_CONTROL__MIN_IB_AVAIL_SIZE__SHIFT |
266 		1 << CP_HQD_IB_CONTROL__IB_EXE_DISABLE__SHIFT;
267 
268 	/*
269 	 * HW does not clamp this field correctly. Maximum EOP queue size
270 	 * is constrained by per-SE EOP done signal count, which is 8-bit.
271 	 * Limit is 0xFF EOP entries (= 0x7F8 dwords). CP will not submit
272 	 * more than (EOP entry count - 1) so a queue size of 0x800 dwords
273 	 * is safe, giving a maximum field value of 0xA.
274 	 *
275 	 * Also, do calculation only if EOP is used (size > 0), otherwise
276 	 * the order_base_2 calculation provides incorrect result.
277 	 *
278 	 */
279 	m->cp_hqd_eop_control = q->eop_ring_buffer_size ?
280 		min(0xA, order_base_2(q->eop_ring_buffer_size / 4) - 1) : 0;
281 
282 	m->cp_hqd_eop_base_addr_lo =
283 			lower_32_bits(q->eop_ring_buffer_address >> 8);
284 	m->cp_hqd_eop_base_addr_hi =
285 			upper_32_bits(q->eop_ring_buffer_address >> 8);
286 
287 	m->cp_hqd_iq_timer = 0;
288 
289 	m->cp_hqd_vmid = q->vmid;
290 
291 	if (q->format == KFD_QUEUE_FORMAT_AQL) {
292 		m->cp_hqd_pq_control |= CP_HQD_PQ_CONTROL__NO_UPDATE_RPTR_MASK |
293 				2 << CP_HQD_PQ_CONTROL__SLOT_BASED_WPTR__SHIFT |
294 				1 << CP_HQD_PQ_CONTROL__QUEUE_FULL_EN__SHIFT |
295 				1 << CP_HQD_PQ_CONTROL__WPP_CLAMP_EN__SHIFT;
296 		m->cp_hqd_pq_doorbell_control |= 1 <<
297 			CP_HQD_PQ_DOORBELL_CONTROL__DOORBELL_BIF_DROP__SHIFT;
298 	}
299 	if (mm->dev->kfd->cwsr_enabled && q->ctx_save_restore_area_address)
300 		m->cp_hqd_ctx_save_control = 0;
301 
302 	if (KFD_GC_VERSION(mm->dev) != IP_VERSION(9, 4, 3))
303 		update_cu_mask(mm, mqd, minfo, 0);
304 	set_priority(m, q);
305 
306 	if (minfo && KFD_GC_VERSION(mm->dev) >= IP_VERSION(9, 4, 2)) {
307 		if (minfo->update_flag & UPDATE_FLAG_IS_GWS)
308 			m->compute_resource_limits |=
309 				COMPUTE_RESOURCE_LIMITS__FORCE_SIMD_DIST_MASK;
310 		else
311 			m->compute_resource_limits &=
312 				~COMPUTE_RESOURCE_LIMITS__FORCE_SIMD_DIST_MASK;
313 	}
314 
315 	q->is_active = QUEUE_IS_ACTIVE(*q);
316 }
317 
318 
319 static bool check_preemption_failed(struct mqd_manager *mm, void *mqd)
320 {
321 	struct v9_mqd *m = (struct v9_mqd *)mqd;
322 
323 	return kfd_check_hiq_mqd_doorbell_id(mm->dev, m->queue_doorbell_id0, 0);
324 }
325 
326 static int get_wave_state(struct mqd_manager *mm, void *mqd,
327 			  struct queue_properties *q,
328 			  void __user *ctl_stack,
329 			  u32 *ctl_stack_used_size,
330 			  u32 *save_area_used_size)
331 {
332 	struct v9_mqd *m;
333 	struct kfd_context_save_area_header header;
334 
335 	/* Control stack is located one page after MQD. */
336 	void *mqd_ctl_stack = (void *)((uintptr_t)mqd + PAGE_SIZE);
337 
338 	m = get_mqd(mqd);
339 
340 	*ctl_stack_used_size = m->cp_hqd_cntl_stack_size -
341 		m->cp_hqd_cntl_stack_offset;
342 	*save_area_used_size = m->cp_hqd_wg_state_offset -
343 		m->cp_hqd_cntl_stack_size;
344 
345 	header.wave_state.control_stack_size = *ctl_stack_used_size;
346 	header.wave_state.wave_state_size = *save_area_used_size;
347 
348 	header.wave_state.wave_state_offset = m->cp_hqd_wg_state_offset;
349 	header.wave_state.control_stack_offset = m->cp_hqd_cntl_stack_offset;
350 
351 	if (copy_to_user(ctl_stack, &header, sizeof(header.wave_state)))
352 		return -EFAULT;
353 
354 	if (copy_to_user(ctl_stack + m->cp_hqd_cntl_stack_offset,
355 				mqd_ctl_stack + m->cp_hqd_cntl_stack_offset,
356 				*ctl_stack_used_size))
357 		return -EFAULT;
358 
359 	return 0;
360 }
361 
362 static void get_checkpoint_info(struct mqd_manager *mm, void *mqd, u32 *ctl_stack_size)
363 {
364 	struct v9_mqd *m = get_mqd(mqd);
365 
366 	*ctl_stack_size = m->cp_hqd_cntl_stack_size;
367 }
368 
369 static void checkpoint_mqd(struct mqd_manager *mm, void *mqd, void *mqd_dst, void *ctl_stack_dst)
370 {
371 	struct v9_mqd *m;
372 	/* Control stack is located one page after MQD. */
373 	void *ctl_stack = (void *)((uintptr_t)mqd + PAGE_SIZE);
374 
375 	m = get_mqd(mqd);
376 
377 	memcpy(mqd_dst, m, sizeof(struct v9_mqd));
378 	memcpy(ctl_stack_dst, ctl_stack, m->cp_hqd_cntl_stack_size);
379 }
380 
381 static void restore_mqd(struct mqd_manager *mm, void **mqd,
382 			struct kfd_mem_obj *mqd_mem_obj, uint64_t *gart_addr,
383 			struct queue_properties *qp,
384 			const void *mqd_src,
385 			const void *ctl_stack_src, u32 ctl_stack_size)
386 {
387 	uint64_t addr;
388 	struct v9_mqd *m;
389 	void *ctl_stack;
390 
391 	m = (struct v9_mqd *) mqd_mem_obj->cpu_ptr;
392 	addr = mqd_mem_obj->gpu_addr;
393 
394 	memcpy(m, mqd_src, sizeof(*m));
395 
396 	*mqd = m;
397 	if (gart_addr)
398 		*gart_addr = addr;
399 
400 	/* Control stack is located one page after MQD. */
401 	ctl_stack = (void *)((uintptr_t)*mqd + PAGE_SIZE);
402 	memcpy(ctl_stack, ctl_stack_src, ctl_stack_size);
403 
404 	m->cp_hqd_pq_doorbell_control =
405 		qp->doorbell_off <<
406 			CP_HQD_PQ_DOORBELL_CONTROL__DOORBELL_OFFSET__SHIFT;
407 	pr_debug("cp_hqd_pq_doorbell_control 0x%x\n",
408 				m->cp_hqd_pq_doorbell_control);
409 
410 	qp->is_active = 0;
411 }
412 
413 static void init_mqd_hiq(struct mqd_manager *mm, void **mqd,
414 			struct kfd_mem_obj *mqd_mem_obj, uint64_t *gart_addr,
415 			struct queue_properties *q)
416 {
417 	struct v9_mqd *m;
418 
419 	init_mqd(mm, mqd, mqd_mem_obj, gart_addr, q);
420 
421 	m = get_mqd(*mqd);
422 
423 	m->cp_hqd_pq_control |= 1 << CP_HQD_PQ_CONTROL__PRIV_STATE__SHIFT |
424 			1 << CP_HQD_PQ_CONTROL__KMD_QUEUE__SHIFT;
425 }
426 
427 static int destroy_hiq_mqd(struct mqd_manager *mm, void *mqd,
428 			enum kfd_preempt_type type, unsigned int timeout,
429 			uint32_t pipe_id, uint32_t queue_id)
430 {
431 	int err;
432 	struct v9_mqd *m;
433 	u32 doorbell_off;
434 
435 	m = get_mqd(mqd);
436 
437 	doorbell_off = m->cp_hqd_pq_doorbell_control >>
438 			CP_HQD_PQ_DOORBELL_CONTROL__DOORBELL_OFFSET__SHIFT;
439 	err = amdgpu_amdkfd_unmap_hiq(mm->dev->adev, doorbell_off, 0);
440 	if (err)
441 		pr_debug("Destroy HIQ MQD failed: %d\n", err);
442 
443 	return err;
444 }
445 
446 static void init_mqd_sdma(struct mqd_manager *mm, void **mqd,
447 		struct kfd_mem_obj *mqd_mem_obj, uint64_t *gart_addr,
448 		struct queue_properties *q)
449 {
450 	struct v9_sdma_mqd *m;
451 
452 	m = (struct v9_sdma_mqd *) mqd_mem_obj->cpu_ptr;
453 
454 	memset(m, 0, sizeof(struct v9_sdma_mqd));
455 
456 	*mqd = m;
457 	if (gart_addr)
458 		*gart_addr = mqd_mem_obj->gpu_addr;
459 
460 	mm->update_mqd(mm, m, q, NULL);
461 }
462 
463 #define SDMA_RLC_DUMMY_DEFAULT 0xf
464 
465 static void update_mqd_sdma(struct mqd_manager *mm, void *mqd,
466 			struct queue_properties *q,
467 			struct mqd_update_info *minfo)
468 {
469 	struct v9_sdma_mqd *m;
470 
471 	m = get_sdma_mqd(mqd);
472 	m->sdmax_rlcx_rb_cntl = order_base_2(q->queue_size / 4)
473 		<< SDMA0_RLC0_RB_CNTL__RB_SIZE__SHIFT |
474 		q->vmid << SDMA0_RLC0_RB_CNTL__RB_VMID__SHIFT |
475 		1 << SDMA0_RLC0_RB_CNTL__RPTR_WRITEBACK_ENABLE__SHIFT |
476 		6 << SDMA0_RLC0_RB_CNTL__RPTR_WRITEBACK_TIMER__SHIFT;
477 
478 	m->sdmax_rlcx_rb_base = lower_32_bits(q->queue_address >> 8);
479 	m->sdmax_rlcx_rb_base_hi = upper_32_bits(q->queue_address >> 8);
480 	m->sdmax_rlcx_rb_rptr_addr_lo = lower_32_bits((uint64_t)q->read_ptr);
481 	m->sdmax_rlcx_rb_rptr_addr_hi = upper_32_bits((uint64_t)q->read_ptr);
482 	m->sdmax_rlcx_doorbell_offset =
483 		q->doorbell_off << SDMA0_RLC0_DOORBELL_OFFSET__OFFSET__SHIFT;
484 
485 	m->sdma_engine_id = q->sdma_engine_id;
486 	m->sdma_queue_id = q->sdma_queue_id;
487 	m->sdmax_rlcx_dummy_reg = SDMA_RLC_DUMMY_DEFAULT;
488 
489 	q->is_active = QUEUE_IS_ACTIVE(*q);
490 }
491 
492 static void checkpoint_mqd_sdma(struct mqd_manager *mm,
493 				void *mqd,
494 				void *mqd_dst,
495 				void *ctl_stack_dst)
496 {
497 	struct v9_sdma_mqd *m;
498 
499 	m = get_sdma_mqd(mqd);
500 
501 	memcpy(mqd_dst, m, sizeof(struct v9_sdma_mqd));
502 }
503 
504 static void restore_mqd_sdma(struct mqd_manager *mm, void **mqd,
505 			     struct kfd_mem_obj *mqd_mem_obj, uint64_t *gart_addr,
506 			     struct queue_properties *qp,
507 			     const void *mqd_src,
508 			     const void *ctl_stack_src, const u32 ctl_stack_size)
509 {
510 	uint64_t addr;
511 	struct v9_sdma_mqd *m;
512 
513 	m = (struct v9_sdma_mqd *) mqd_mem_obj->cpu_ptr;
514 	addr = mqd_mem_obj->gpu_addr;
515 
516 	memcpy(m, mqd_src, sizeof(*m));
517 
518 	m->sdmax_rlcx_doorbell_offset =
519 		qp->doorbell_off << SDMA0_RLC0_DOORBELL_OFFSET__OFFSET__SHIFT;
520 
521 	*mqd = m;
522 	if (gart_addr)
523 		*gart_addr = addr;
524 
525 	qp->is_active = 0;
526 }
527 
528 static void init_mqd_hiq_v9_4_3(struct mqd_manager *mm, void **mqd,
529 			struct kfd_mem_obj *mqd_mem_obj, uint64_t *gart_addr,
530 			struct queue_properties *q)
531 {
532 	struct v9_mqd *m;
533 	int xcc = 0;
534 	struct kfd_mem_obj xcc_mqd_mem_obj;
535 	uint64_t xcc_gart_addr = 0;
536 
537 	memset(&xcc_mqd_mem_obj, 0x0, sizeof(struct kfd_mem_obj));
538 
539 	for (xcc = 0; xcc < NUM_XCC(mm->dev->xcc_mask); xcc++) {
540 		kfd_get_hiq_xcc_mqd(mm->dev, &xcc_mqd_mem_obj, xcc);
541 
542 		init_mqd(mm, (void **)&m, &xcc_mqd_mem_obj, &xcc_gart_addr, q);
543 
544 		m->cp_hqd_pq_control |= CP_HQD_PQ_CONTROL__NO_UPDATE_RPTR_MASK |
545 					1 << CP_HQD_PQ_CONTROL__PRIV_STATE__SHIFT |
546 					1 << CP_HQD_PQ_CONTROL__KMD_QUEUE__SHIFT;
547 		m->cp_mqd_stride_size = kfd_hiq_mqd_stride(mm->dev);
548 		if (xcc == 0) {
549 			/* Set no_update_rptr = 0 in Master XCC */
550 			m->cp_hqd_pq_control &= ~CP_HQD_PQ_CONTROL__NO_UPDATE_RPTR_MASK;
551 
552 			/* Set the MQD pointer and gart address to XCC0 MQD */
553 			*mqd = m;
554 			*gart_addr = xcc_gart_addr;
555 		}
556 	}
557 }
558 
559 static int hiq_load_mqd_kiq_v9_4_3(struct mqd_manager *mm, void *mqd,
560 			uint32_t pipe_id, uint32_t queue_id,
561 			struct queue_properties *p, struct mm_struct *mms)
562 {
563 	uint32_t xcc_mask = mm->dev->xcc_mask;
564 	int xcc_id, err, inst = 0;
565 	void *xcc_mqd;
566 	uint64_t hiq_mqd_size = kfd_hiq_mqd_stride(mm->dev);
567 
568 	for_each_inst(xcc_id, xcc_mask) {
569 		xcc_mqd = mqd + hiq_mqd_size * inst;
570 		err = mm->dev->kfd2kgd->hiq_mqd_load(mm->dev->adev, xcc_mqd,
571 						     pipe_id, queue_id,
572 						     p->doorbell_off, xcc_id);
573 		if (err) {
574 			pr_debug("Failed to load HIQ MQD for XCC: %d\n", inst);
575 			break;
576 		}
577 		++inst;
578 	}
579 
580 	return err;
581 }
582 
583 static int destroy_hiq_mqd_v9_4_3(struct mqd_manager *mm, void *mqd,
584 			enum kfd_preempt_type type, unsigned int timeout,
585 			uint32_t pipe_id, uint32_t queue_id)
586 {
587 	uint32_t xcc_mask = mm->dev->xcc_mask;
588 	int xcc_id, err, inst = 0;
589 	uint64_t hiq_mqd_size = kfd_hiq_mqd_stride(mm->dev);
590 	struct v9_mqd *m;
591 	u32 doorbell_off;
592 
593 	for_each_inst(xcc_id, xcc_mask) {
594 		m = get_mqd(mqd + hiq_mqd_size * inst);
595 
596 		doorbell_off = m->cp_hqd_pq_doorbell_control >>
597 				CP_HQD_PQ_DOORBELL_CONTROL__DOORBELL_OFFSET__SHIFT;
598 
599 		err = amdgpu_amdkfd_unmap_hiq(mm->dev->adev, doorbell_off, xcc_id);
600 		if (err) {
601 			pr_debug("Destroy HIQ MQD failed for xcc: %d\n", inst);
602 			break;
603 		}
604 		++inst;
605 	}
606 
607 	return err;
608 }
609 
610 static bool check_preemption_failed_v9_4_3(struct mqd_manager *mm, void *mqd)
611 {
612 	uint64_t hiq_mqd_size = kfd_hiq_mqd_stride(mm->dev);
613 	uint32_t xcc_mask = mm->dev->xcc_mask;
614 	int inst = 0, xcc_id;
615 	struct v9_mqd *m;
616 	bool ret = false;
617 
618 	for_each_inst(xcc_id, xcc_mask) {
619 		m = get_mqd(mqd + hiq_mqd_size * inst);
620 		ret |= kfd_check_hiq_mqd_doorbell_id(mm->dev,
621 					m->queue_doorbell_id0, inst);
622 		++inst;
623 	}
624 
625 	return ret;
626 }
627 
628 static void get_xcc_mqd(struct kfd_mem_obj *mqd_mem_obj,
629 			       struct kfd_mem_obj *xcc_mqd_mem_obj,
630 			       uint64_t offset)
631 {
632 	xcc_mqd_mem_obj->gtt_mem = (offset == 0) ?
633 					mqd_mem_obj->gtt_mem : NULL;
634 	xcc_mqd_mem_obj->gpu_addr = mqd_mem_obj->gpu_addr + offset;
635 	xcc_mqd_mem_obj->cpu_ptr = (uint32_t *)((uintptr_t)mqd_mem_obj->cpu_ptr
636 						+ offset);
637 }
638 
639 static void init_mqd_v9_4_3(struct mqd_manager *mm, void **mqd,
640 			struct kfd_mem_obj *mqd_mem_obj, uint64_t *gart_addr,
641 			struct queue_properties *q)
642 {
643 	struct v9_mqd *m;
644 	int xcc = 0;
645 	struct kfd_mem_obj xcc_mqd_mem_obj;
646 	uint64_t xcc_gart_addr = 0;
647 	uint64_t xcc_ctx_save_restore_area_address;
648 	uint64_t offset = mm->mqd_stride(mm, q);
649 	uint32_t local_xcc_start = mm->dev->dqm->current_logical_xcc_start++;
650 
651 	memset(&xcc_mqd_mem_obj, 0x0, sizeof(struct kfd_mem_obj));
652 	for (xcc = 0; xcc < NUM_XCC(mm->dev->xcc_mask); xcc++) {
653 		get_xcc_mqd(mqd_mem_obj, &xcc_mqd_mem_obj, offset*xcc);
654 
655 		init_mqd(mm, (void **)&m, &xcc_mqd_mem_obj, &xcc_gart_addr, q);
656 
657 		m->cp_mqd_stride_size = offset;
658 
659 		/*
660 		 * Update the CWSR address for each XCC if CWSR is enabled
661 		 * and CWSR area is allocated in thunk
662 		 */
663 		if (mm->dev->kfd->cwsr_enabled &&
664 		    q->ctx_save_restore_area_address) {
665 			xcc_ctx_save_restore_area_address =
666 				q->ctx_save_restore_area_address +
667 				(xcc * q->ctx_save_restore_area_size);
668 
669 			m->cp_hqd_ctx_save_base_addr_lo =
670 				lower_32_bits(xcc_ctx_save_restore_area_address);
671 			m->cp_hqd_ctx_save_base_addr_hi =
672 				upper_32_bits(xcc_ctx_save_restore_area_address);
673 		}
674 
675 		if (q->format == KFD_QUEUE_FORMAT_AQL) {
676 			m->compute_tg_chunk_size = 1;
677 			m->compute_current_logic_xcc_id =
678 					(local_xcc_start + xcc) %
679 					NUM_XCC(mm->dev->xcc_mask);
680 
681 			switch (xcc) {
682 			case 0:
683 				/* Master XCC */
684 				m->cp_hqd_pq_control &=
685 					~CP_HQD_PQ_CONTROL__NO_UPDATE_RPTR_MASK;
686 				break;
687 			default:
688 				break;
689 			}
690 		} else {
691 			/* PM4 Queue */
692 			m->compute_current_logic_xcc_id = 0;
693 			m->compute_tg_chunk_size = 0;
694 			m->pm4_target_xcc_in_xcp = q->pm4_target_xcc;
695 		}
696 
697 		if (xcc == 0) {
698 			/* Set the MQD pointer and gart address to XCC0 MQD */
699 			*mqd = m;
700 			*gart_addr = xcc_gart_addr;
701 		}
702 	}
703 }
704 
705 static void update_mqd_v9_4_3(struct mqd_manager *mm, void *mqd,
706 		      struct queue_properties *q, struct mqd_update_info *minfo)
707 {
708 	struct v9_mqd *m;
709 	int xcc = 0;
710 	uint64_t size = mm->mqd_stride(mm, q);
711 
712 	for (xcc = 0; xcc < NUM_XCC(mm->dev->xcc_mask); xcc++) {
713 		m = get_mqd(mqd + size * xcc);
714 		update_mqd(mm, m, q, minfo);
715 
716 		update_cu_mask(mm, mqd, minfo, xcc);
717 
718 		if (q->format == KFD_QUEUE_FORMAT_AQL) {
719 			switch (xcc) {
720 			case 0:
721 				/* Master XCC */
722 				m->cp_hqd_pq_control &=
723 					~CP_HQD_PQ_CONTROL__NO_UPDATE_RPTR_MASK;
724 				break;
725 			default:
726 				break;
727 			}
728 			m->compute_tg_chunk_size = 1;
729 		} else {
730 			/* PM4 Queue */
731 			m->compute_current_logic_xcc_id = 0;
732 			m->compute_tg_chunk_size = 0;
733 			m->pm4_target_xcc_in_xcp = q->pm4_target_xcc;
734 		}
735 	}
736 }
737 
738 static int destroy_mqd_v9_4_3(struct mqd_manager *mm, void *mqd,
739 		   enum kfd_preempt_type type, unsigned int timeout,
740 		   uint32_t pipe_id, uint32_t queue_id)
741 {
742 	uint32_t xcc_mask = mm->dev->xcc_mask;
743 	int xcc_id, err, inst = 0;
744 	void *xcc_mqd;
745 	struct v9_mqd *m;
746 	uint64_t mqd_offset;
747 
748 	m = get_mqd(mqd);
749 	mqd_offset = m->cp_mqd_stride_size;
750 
751 	for_each_inst(xcc_id, xcc_mask) {
752 		xcc_mqd = mqd + mqd_offset * inst;
753 		err = mm->dev->kfd2kgd->hqd_destroy(mm->dev->adev, xcc_mqd,
754 						    type, timeout, pipe_id,
755 						    queue_id, xcc_id);
756 		if (err) {
757 			pr_debug("Destroy MQD failed for xcc: %d\n", inst);
758 			break;
759 		}
760 		++inst;
761 	}
762 
763 	return err;
764 }
765 
766 static int load_mqd_v9_4_3(struct mqd_manager *mm, void *mqd,
767 			uint32_t pipe_id, uint32_t queue_id,
768 			struct queue_properties *p, struct mm_struct *mms)
769 {
770 	/* AQL write pointer counts in 64B packets, PM4/CP counts in dwords. */
771 	uint32_t wptr_shift = (p->format == KFD_QUEUE_FORMAT_AQL ? 4 : 0);
772 	uint32_t xcc_mask = mm->dev->xcc_mask;
773 	int xcc_id, err, inst = 0;
774 	void *xcc_mqd;
775 	uint64_t mqd_stride_size = mm->mqd_stride(mm, p);
776 
777 	for_each_inst(xcc_id, xcc_mask) {
778 		xcc_mqd = mqd + mqd_stride_size * inst;
779 		err = mm->dev->kfd2kgd->hqd_load(
780 			mm->dev->adev, xcc_mqd, pipe_id, queue_id,
781 			(uint32_t __user *)p->write_ptr, wptr_shift, 0, mms,
782 			xcc_id);
783 		if (err) {
784 			pr_debug("Load MQD failed for xcc: %d\n", inst);
785 			break;
786 		}
787 		++inst;
788 	}
789 
790 	return err;
791 }
792 
793 static int get_wave_state_v9_4_3(struct mqd_manager *mm, void *mqd,
794 				 struct queue_properties *q,
795 				 void __user *ctl_stack,
796 				 u32 *ctl_stack_used_size,
797 				 u32 *save_area_used_size)
798 {
799 	int xcc, err = 0;
800 	void *xcc_mqd;
801 	void __user *xcc_ctl_stack;
802 	uint64_t mqd_stride_size = mm->mqd_stride(mm, q);
803 	u32 tmp_ctl_stack_used_size = 0, tmp_save_area_used_size = 0;
804 
805 	for (xcc = 0; xcc < NUM_XCC(mm->dev->xcc_mask); xcc++) {
806 		xcc_mqd = mqd + mqd_stride_size * xcc;
807 		xcc_ctl_stack = (void __user *)((uintptr_t)ctl_stack +
808 					q->ctx_save_restore_area_size * xcc);
809 
810 		err = get_wave_state(mm, xcc_mqd, q, xcc_ctl_stack,
811 				     &tmp_ctl_stack_used_size,
812 				     &tmp_save_area_used_size);
813 		if (err)
814 			break;
815 
816 		/*
817 		 * Set the ctl_stack_used_size and save_area_used_size to
818 		 * ctl_stack_used_size and save_area_used_size of XCC 0 when
819 		 * passing the info the user-space.
820 		 * For multi XCC, user-space would have to look at the header
821 		 * info of each Control stack area to determine the control
822 		 * stack size and save area used.
823 		 */
824 		if (xcc == 0) {
825 			*ctl_stack_used_size = tmp_ctl_stack_used_size;
826 			*save_area_used_size = tmp_save_area_used_size;
827 		}
828 	}
829 
830 	return err;
831 }
832 
833 #if defined(CONFIG_DEBUG_FS)
834 
835 static int debugfs_show_mqd(struct seq_file *m, void *data)
836 {
837 	seq_hex_dump(m, "    ", DUMP_PREFIX_OFFSET, 32, 4,
838 		     data, sizeof(struct v9_mqd), false);
839 	return 0;
840 }
841 
842 static int debugfs_show_mqd_sdma(struct seq_file *m, void *data)
843 {
844 	seq_hex_dump(m, "    ", DUMP_PREFIX_OFFSET, 32, 4,
845 		     data, sizeof(struct v9_sdma_mqd), false);
846 	return 0;
847 }
848 
849 #endif
850 
851 struct mqd_manager *mqd_manager_init_v9(enum KFD_MQD_TYPE type,
852 		struct kfd_node *dev)
853 {
854 	struct mqd_manager *mqd;
855 
856 	if (WARN_ON(type >= KFD_MQD_TYPE_MAX))
857 		return NULL;
858 
859 	mqd = kzalloc(sizeof(*mqd), GFP_KERNEL);
860 	if (!mqd)
861 		return NULL;
862 
863 	mqd->dev = dev;
864 
865 	switch (type) {
866 	case KFD_MQD_TYPE_CP:
867 		mqd->allocate_mqd = allocate_mqd;
868 		mqd->free_mqd = kfd_free_mqd_cp;
869 		mqd->is_occupied = kfd_is_occupied_cp;
870 		mqd->get_checkpoint_info = get_checkpoint_info;
871 		mqd->checkpoint_mqd = checkpoint_mqd;
872 		mqd->restore_mqd = restore_mqd;
873 		mqd->mqd_size = sizeof(struct v9_mqd);
874 		mqd->mqd_stride = mqd_stride_v9;
875 #if defined(CONFIG_DEBUG_FS)
876 		mqd->debugfs_show_mqd = debugfs_show_mqd;
877 #endif
878 		if (KFD_GC_VERSION(dev) == IP_VERSION(9, 4, 3)) {
879 			mqd->init_mqd = init_mqd_v9_4_3;
880 			mqd->load_mqd = load_mqd_v9_4_3;
881 			mqd->update_mqd = update_mqd_v9_4_3;
882 			mqd->destroy_mqd = destroy_mqd_v9_4_3;
883 			mqd->get_wave_state = get_wave_state_v9_4_3;
884 		} else {
885 			mqd->init_mqd = init_mqd;
886 			mqd->load_mqd = load_mqd;
887 			mqd->update_mqd = update_mqd;
888 			mqd->destroy_mqd = kfd_destroy_mqd_cp;
889 			mqd->get_wave_state = get_wave_state;
890 		}
891 		break;
892 	case KFD_MQD_TYPE_HIQ:
893 		mqd->allocate_mqd = allocate_hiq_mqd;
894 		mqd->free_mqd = free_mqd_hiq_sdma;
895 		mqd->update_mqd = update_mqd;
896 		mqd->is_occupied = kfd_is_occupied_cp;
897 		mqd->mqd_size = sizeof(struct v9_mqd);
898 		mqd->mqd_stride = kfd_mqd_stride;
899 #if defined(CONFIG_DEBUG_FS)
900 		mqd->debugfs_show_mqd = debugfs_show_mqd;
901 #endif
902 		if (KFD_GC_VERSION(dev) == IP_VERSION(9, 4, 3)) {
903 			mqd->init_mqd = init_mqd_hiq_v9_4_3;
904 			mqd->load_mqd = hiq_load_mqd_kiq_v9_4_3;
905 			mqd->destroy_mqd = destroy_hiq_mqd_v9_4_3;
906 			mqd->check_preemption_failed = check_preemption_failed_v9_4_3;
907 		} else {
908 			mqd->init_mqd = init_mqd_hiq;
909 			mqd->load_mqd = kfd_hiq_load_mqd_kiq;
910 			mqd->destroy_mqd = destroy_hiq_mqd;
911 			mqd->check_preemption_failed = check_preemption_failed;
912 		}
913 		break;
914 	case KFD_MQD_TYPE_DIQ:
915 		mqd->allocate_mqd = allocate_mqd;
916 		mqd->init_mqd = init_mqd_hiq;
917 		mqd->free_mqd = kfd_free_mqd_cp;
918 		mqd->load_mqd = load_mqd;
919 		mqd->update_mqd = update_mqd;
920 		mqd->destroy_mqd = kfd_destroy_mqd_cp;
921 		mqd->is_occupied = kfd_is_occupied_cp;
922 		mqd->mqd_size = sizeof(struct v9_mqd);
923 #if defined(CONFIG_DEBUG_FS)
924 		mqd->debugfs_show_mqd = debugfs_show_mqd;
925 #endif
926 		break;
927 	case KFD_MQD_TYPE_SDMA:
928 		mqd->allocate_mqd = allocate_sdma_mqd;
929 		mqd->init_mqd = init_mqd_sdma;
930 		mqd->free_mqd = free_mqd_hiq_sdma;
931 		mqd->load_mqd = kfd_load_mqd_sdma;
932 		mqd->update_mqd = update_mqd_sdma;
933 		mqd->destroy_mqd = kfd_destroy_mqd_sdma;
934 		mqd->is_occupied = kfd_is_occupied_sdma;
935 		mqd->checkpoint_mqd = checkpoint_mqd_sdma;
936 		mqd->restore_mqd = restore_mqd_sdma;
937 		mqd->mqd_size = sizeof(struct v9_sdma_mqd);
938 		mqd->mqd_stride = kfd_mqd_stride;
939 #if defined(CONFIG_DEBUG_FS)
940 		mqd->debugfs_show_mqd = debugfs_show_mqd_sdma;
941 #endif
942 		break;
943 	default:
944 		kfree(mqd);
945 		return NULL;
946 	}
947 
948 	return mqd;
949 }
950