1cc009e61SMukul Joshi /*
2cc009e61SMukul Joshi  * Copyright 2021 Advanced Micro Devices, Inc.
3cc009e61SMukul Joshi  *
4cc009e61SMukul Joshi  * Permission is hereby granted, free of charge, to any person obtaining a
5cc009e61SMukul Joshi  * copy of this software and associated documentation files (the "Software"),
6cc009e61SMukul Joshi  * to deal in the Software without restriction, including without limitation
7cc009e61SMukul Joshi  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8cc009e61SMukul Joshi  * and/or sell copies of the Software, and to permit persons to whom the
9cc009e61SMukul Joshi  * Software is furnished to do so, subject to the following conditions:
10cc009e61SMukul Joshi  *
11cc009e61SMukul Joshi  * The above copyright notice and this permission notice shall be included in
12cc009e61SMukul Joshi  * all copies or substantial portions of the Software.
13cc009e61SMukul Joshi  *
14cc009e61SMukul Joshi  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15cc009e61SMukul Joshi  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16cc009e61SMukul Joshi  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17cc009e61SMukul Joshi  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18cc009e61SMukul Joshi  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19cc009e61SMukul Joshi  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20cc009e61SMukul Joshi  * OTHER DEALINGS IN THE SOFTWARE.
21cc009e61SMukul Joshi  *
22cc009e61SMukul Joshi  */
23cc009e61SMukul Joshi 
24cc009e61SMukul Joshi #include <linux/printk.h>
25cc009e61SMukul Joshi #include <linux/slab.h>
26cc009e61SMukul Joshi #include <linux/uaccess.h>
27cc009e61SMukul Joshi #include "kfd_priv.h"
28cc009e61SMukul Joshi #include "kfd_mqd_manager.h"
29cc009e61SMukul Joshi #include "v11_structs.h"
30cc009e61SMukul Joshi #include "gc/gc_11_0_0_offset.h"
31cc009e61SMukul Joshi #include "gc/gc_11_0_0_sh_mask.h"
32cc009e61SMukul Joshi #include "amdgpu_amdkfd.h"
33cc009e61SMukul Joshi 
34cc009e61SMukul Joshi static inline struct v11_compute_mqd *get_mqd(void *mqd)
35cc009e61SMukul Joshi {
36cc009e61SMukul Joshi 	return (struct v11_compute_mqd *)mqd;
37cc009e61SMukul Joshi }
38cc009e61SMukul Joshi 
39cc009e61SMukul Joshi static inline struct v11_sdma_mqd *get_sdma_mqd(void *mqd)
40cc009e61SMukul Joshi {
41cc009e61SMukul Joshi 	return (struct v11_sdma_mqd *)mqd;
42cc009e61SMukul Joshi }
43cc009e61SMukul Joshi 
44cc009e61SMukul Joshi static void update_cu_mask(struct mqd_manager *mm, void *mqd,
45cc009e61SMukul Joshi 			   struct mqd_update_info *minfo)
46cc009e61SMukul Joshi {
47cc009e61SMukul Joshi 	struct v11_compute_mqd *m;
48cc009e61SMukul Joshi 	uint32_t se_mask[KFD_MAX_NUM_SE] = {0};
49cc009e61SMukul Joshi 
50cc009e61SMukul Joshi 	if (!minfo || (minfo->update_flag != UPDATE_FLAG_CU_MASK) ||
51cc009e61SMukul Joshi 	    !minfo->cu_mask.ptr)
52cc009e61SMukul Joshi 		return;
53cc009e61SMukul Joshi 
54cc009e61SMukul Joshi 	mqd_symmetrically_map_cu_mask(mm,
55cc009e61SMukul Joshi 		minfo->cu_mask.ptr, minfo->cu_mask.count, se_mask);
56cc009e61SMukul Joshi 
57cc009e61SMukul Joshi 	m = get_mqd(mqd);
58cc009e61SMukul Joshi 	m->compute_static_thread_mgmt_se0 = se_mask[0];
59cc009e61SMukul Joshi 	m->compute_static_thread_mgmt_se1 = se_mask[1];
60cc009e61SMukul Joshi 	m->compute_static_thread_mgmt_se2 = se_mask[2];
61cc009e61SMukul Joshi 	m->compute_static_thread_mgmt_se3 = se_mask[3];
62cc009e61SMukul Joshi 	m->compute_static_thread_mgmt_se4 = se_mask[4];
63cc009e61SMukul Joshi 	m->compute_static_thread_mgmt_se5 = se_mask[5];
64cc009e61SMukul Joshi 	m->compute_static_thread_mgmt_se6 = se_mask[6];
65cc009e61SMukul Joshi 	m->compute_static_thread_mgmt_se7 = se_mask[7];
66cc009e61SMukul Joshi 
67cc009e61SMukul Joshi 	pr_debug("update cu mask to %#x %#x %#x %#x %#x %#x %#x %#x\n",
68cc009e61SMukul Joshi 		m->compute_static_thread_mgmt_se0,
69cc009e61SMukul Joshi 		m->compute_static_thread_mgmt_se1,
70cc009e61SMukul Joshi 		m->compute_static_thread_mgmt_se2,
71cc009e61SMukul Joshi 		m->compute_static_thread_mgmt_se3,
72cc009e61SMukul Joshi 		m->compute_static_thread_mgmt_se4,
73cc009e61SMukul Joshi 		m->compute_static_thread_mgmt_se5,
74cc009e61SMukul Joshi 		m->compute_static_thread_mgmt_se6,
75cc009e61SMukul Joshi 		m->compute_static_thread_mgmt_se7);
76cc009e61SMukul Joshi }
77cc009e61SMukul Joshi 
78cc009e61SMukul Joshi static void set_priority(struct v11_compute_mqd *m, struct queue_properties *q)
79cc009e61SMukul Joshi {
80cc009e61SMukul Joshi 	m->cp_hqd_pipe_priority = pipe_priority_map[q->priority];
81cc009e61SMukul Joshi 	m->cp_hqd_queue_priority = q->priority;
82cc009e61SMukul Joshi }
83cc009e61SMukul Joshi 
84cc009e61SMukul Joshi static struct kfd_mem_obj *allocate_mqd(struct kfd_dev *kfd,
85cc009e61SMukul Joshi 		struct queue_properties *q)
86cc009e61SMukul Joshi {
87cc009e61SMukul Joshi 	struct kfd_mem_obj *mqd_mem_obj;
88cc009e61SMukul Joshi 	int size;
89cc009e61SMukul Joshi 
90cc009e61SMukul Joshi 	/*
91cc009e61SMukul Joshi 	 * MES write to areas beyond MQD size. So allocate
92cc009e61SMukul Joshi 	 * 1 PAGE_SIZE memory for MQD is MES is enabled.
93cc009e61SMukul Joshi 	 */
94cc009e61SMukul Joshi 	if (kfd->shared_resources.enable_mes)
95cc009e61SMukul Joshi 		size = PAGE_SIZE;
96cc009e61SMukul Joshi 	else
97cc009e61SMukul Joshi 		size = sizeof(struct v11_compute_mqd);
98cc009e61SMukul Joshi 
99cc009e61SMukul Joshi 	if (kfd_gtt_sa_allocate(kfd, size, &mqd_mem_obj))
100cc009e61SMukul Joshi 		return NULL;
101cc009e61SMukul Joshi 
102cc009e61SMukul Joshi 	return mqd_mem_obj;
103cc009e61SMukul Joshi }
104cc009e61SMukul Joshi 
105cc009e61SMukul Joshi static void init_mqd(struct mqd_manager *mm, void **mqd,
106cc009e61SMukul Joshi 			struct kfd_mem_obj *mqd_mem_obj, uint64_t *gart_addr,
107cc009e61SMukul Joshi 			struct queue_properties *q)
108cc009e61SMukul Joshi {
109cc009e61SMukul Joshi 	uint64_t addr;
110cc009e61SMukul Joshi 	struct v11_compute_mqd *m;
111cc009e61SMukul Joshi 	int size;
112cc009e61SMukul Joshi 
113cc009e61SMukul Joshi 	m = (struct v11_compute_mqd *) mqd_mem_obj->cpu_ptr;
114cc009e61SMukul Joshi 	addr = mqd_mem_obj->gpu_addr;
115cc009e61SMukul Joshi 
116cc009e61SMukul Joshi 	if (mm->dev->shared_resources.enable_mes)
117cc009e61SMukul Joshi 		size = PAGE_SIZE;
118cc009e61SMukul Joshi 	else
119cc009e61SMukul Joshi 		size = sizeof(struct v11_compute_mqd);
120cc009e61SMukul Joshi 
121cc009e61SMukul Joshi 	memset(m, 0, size);
122cc009e61SMukul Joshi 
123cc009e61SMukul Joshi 	m->header = 0xC0310800;
124cc009e61SMukul Joshi 	m->compute_pipelinestat_enable = 1;
125cc009e61SMukul Joshi 	m->compute_static_thread_mgmt_se0 = 0xFFFFFFFF;
126cc009e61SMukul Joshi 	m->compute_static_thread_mgmt_se1 = 0xFFFFFFFF;
127cc009e61SMukul Joshi 	m->compute_static_thread_mgmt_se2 = 0xFFFFFFFF;
128cc009e61SMukul Joshi 	m->compute_static_thread_mgmt_se3 = 0xFFFFFFFF;
1297971b5c2SGraham Sider 	m->compute_static_thread_mgmt_se4 = 0xFFFFFFFF;
1307971b5c2SGraham Sider 	m->compute_static_thread_mgmt_se5 = 0xFFFFFFFF;
1317971b5c2SGraham Sider 	m->compute_static_thread_mgmt_se6 = 0xFFFFFFFF;
1327971b5c2SGraham Sider 	m->compute_static_thread_mgmt_se7 = 0xFFFFFFFF;
133cc009e61SMukul Joshi 
134cc009e61SMukul Joshi 	m->cp_hqd_persistent_state = CP_HQD_PERSISTENT_STATE__PRELOAD_REQ_MASK |
135cc009e61SMukul Joshi 			0x55 << CP_HQD_PERSISTENT_STATE__PRELOAD_SIZE__SHIFT;
136cc009e61SMukul Joshi 
137cc009e61SMukul Joshi 	m->cp_mqd_control = 1 << CP_MQD_CONTROL__PRIV_STATE__SHIFT;
138cc009e61SMukul Joshi 
139cc009e61SMukul Joshi 	m->cp_mqd_base_addr_lo        = lower_32_bits(addr);
140cc009e61SMukul Joshi 	m->cp_mqd_base_addr_hi        = upper_32_bits(addr);
141cc009e61SMukul Joshi 
142cc009e61SMukul Joshi 	m->cp_hqd_quantum = 1 << CP_HQD_QUANTUM__QUANTUM_EN__SHIFT |
143cc009e61SMukul Joshi 			1 << CP_HQD_QUANTUM__QUANTUM_SCALE__SHIFT |
144cc009e61SMukul Joshi 			1 << CP_HQD_QUANTUM__QUANTUM_DURATION__SHIFT;
145cc009e61SMukul Joshi 
14600fa4035SSreekant Somasekharan 	/*
14700fa4035SSreekant Somasekharan 	 * GFX11 RS64 CPFW version >= 509 supports PCIe atomics support
14800fa4035SSreekant Somasekharan 	 * acknowledgment.
14900fa4035SSreekant Somasekharan 	 */
15000fa4035SSreekant Somasekharan 	if (amdgpu_amdkfd_have_atomics_support(mm->dev->adev))
15100fa4035SSreekant Somasekharan 		m->cp_hqd_hq_status0 |= 1 << 29;
15200fa4035SSreekant Somasekharan 
153cc009e61SMukul Joshi 	if (q->format == KFD_QUEUE_FORMAT_AQL) {
154cc009e61SMukul Joshi 		m->cp_hqd_aql_control =
155cc009e61SMukul Joshi 			1 << CP_HQD_AQL_CONTROL__CONTROL0__SHIFT;
156cc009e61SMukul Joshi 	}
157cc009e61SMukul Joshi 
158cc009e61SMukul Joshi 	if (mm->dev->cwsr_enabled) {
159cc009e61SMukul Joshi 		m->cp_hqd_persistent_state |=
160cc009e61SMukul Joshi 			(1 << CP_HQD_PERSISTENT_STATE__QSWITCH_MODE__SHIFT);
161cc009e61SMukul Joshi 		m->cp_hqd_ctx_save_base_addr_lo =
162cc009e61SMukul Joshi 			lower_32_bits(q->ctx_save_restore_area_address);
163cc009e61SMukul Joshi 		m->cp_hqd_ctx_save_base_addr_hi =
164cc009e61SMukul Joshi 			upper_32_bits(q->ctx_save_restore_area_address);
165cc009e61SMukul Joshi 		m->cp_hqd_ctx_save_size = q->ctx_save_restore_area_size;
166cc009e61SMukul Joshi 		m->cp_hqd_cntl_stack_size = q->ctl_stack_size;
167cc009e61SMukul Joshi 		m->cp_hqd_cntl_stack_offset = q->ctl_stack_size;
168cc009e61SMukul Joshi 		m->cp_hqd_wg_state_offset = q->ctl_stack_size;
169cc009e61SMukul Joshi 	}
170cc009e61SMukul Joshi 
171cc009e61SMukul Joshi 	*mqd = m;
172cc009e61SMukul Joshi 	if (gart_addr)
173cc009e61SMukul Joshi 		*gart_addr = addr;
174cc009e61SMukul Joshi 	mm->update_mqd(mm, m, q, NULL);
175cc009e61SMukul Joshi }
176cc009e61SMukul Joshi 
177cc009e61SMukul Joshi static int load_mqd(struct mqd_manager *mm, void *mqd,
178cc009e61SMukul Joshi 			uint32_t pipe_id, uint32_t queue_id,
179cc009e61SMukul Joshi 			struct queue_properties *p, struct mm_struct *mms)
180cc009e61SMukul Joshi {
181cc009e61SMukul Joshi 	int r = 0;
182cc009e61SMukul Joshi 	/* AQL write pointer counts in 64B packets, PM4/CP counts in dwords. */
183cc009e61SMukul Joshi 	uint32_t wptr_shift = (p->format == KFD_QUEUE_FORMAT_AQL ? 4 : 0);
184cc009e61SMukul Joshi 
185cc009e61SMukul Joshi 	r = mm->dev->kfd2kgd->hqd_load(mm->dev->adev, mqd, pipe_id, queue_id,
186cc009e61SMukul Joshi 					  (uint32_t __user *)p->write_ptr,
187cc009e61SMukul Joshi 					  wptr_shift, 0, mms);
188cc009e61SMukul Joshi 	return r;
189cc009e61SMukul Joshi }
190cc009e61SMukul Joshi 
191cc009e61SMukul Joshi static void update_mqd(struct mqd_manager *mm, void *mqd,
192cc009e61SMukul Joshi 		       struct queue_properties *q,
193cc009e61SMukul Joshi 		       struct mqd_update_info *minfo)
194cc009e61SMukul Joshi {
195cc009e61SMukul Joshi 	struct v11_compute_mqd *m;
196cc009e61SMukul Joshi 
197cc009e61SMukul Joshi 	m = get_mqd(mqd);
198cc009e61SMukul Joshi 
199cc009e61SMukul Joshi 	m->cp_hqd_pq_control = 5 << CP_HQD_PQ_CONTROL__RPTR_BLOCK_SIZE__SHIFT;
200cc009e61SMukul Joshi 	m->cp_hqd_pq_control |=
201cc009e61SMukul Joshi 			ffs(q->queue_size / sizeof(unsigned int)) - 1 - 1;
202cc009e61SMukul Joshi 	pr_debug("cp_hqd_pq_control 0x%x\n", m->cp_hqd_pq_control);
203cc009e61SMukul Joshi 
204cc009e61SMukul Joshi 	m->cp_hqd_pq_base_lo = lower_32_bits((uint64_t)q->queue_address >> 8);
205cc009e61SMukul Joshi 	m->cp_hqd_pq_base_hi = upper_32_bits((uint64_t)q->queue_address >> 8);
206cc009e61SMukul Joshi 
207cc009e61SMukul Joshi 	m->cp_hqd_pq_rptr_report_addr_lo = lower_32_bits((uint64_t)q->read_ptr);
208cc009e61SMukul Joshi 	m->cp_hqd_pq_rptr_report_addr_hi = upper_32_bits((uint64_t)q->read_ptr);
209cc009e61SMukul Joshi 	m->cp_hqd_pq_wptr_poll_addr_lo = lower_32_bits((uint64_t)q->write_ptr);
210cc009e61SMukul Joshi 	m->cp_hqd_pq_wptr_poll_addr_hi = upper_32_bits((uint64_t)q->write_ptr);
211cc009e61SMukul Joshi 
212cc009e61SMukul Joshi 	m->cp_hqd_pq_doorbell_control =
213cc009e61SMukul Joshi 		q->doorbell_off <<
214cc009e61SMukul Joshi 			CP_HQD_PQ_DOORBELL_CONTROL__DOORBELL_OFFSET__SHIFT;
215cc009e61SMukul Joshi 	pr_debug("cp_hqd_pq_doorbell_control 0x%x\n",
216cc009e61SMukul Joshi 			m->cp_hqd_pq_doorbell_control);
217cc009e61SMukul Joshi 
218cc009e61SMukul Joshi 	m->cp_hqd_ib_control = 3 << CP_HQD_IB_CONTROL__MIN_IB_AVAIL_SIZE__SHIFT;
219cc009e61SMukul Joshi 
220cc009e61SMukul Joshi 	/*
221cc009e61SMukul Joshi 	 * HW does not clamp this field correctly. Maximum EOP queue size
222cc009e61SMukul Joshi 	 * is constrained by per-SE EOP done signal count, which is 8-bit.
223cc009e61SMukul Joshi 	 * Limit is 0xFF EOP entries (= 0x7F8 dwords). CP will not submit
224cc009e61SMukul Joshi 	 * more than (EOP entry count - 1) so a queue size of 0x800 dwords
225cc009e61SMukul Joshi 	 * is safe, giving a maximum field value of 0xA.
226cc009e61SMukul Joshi 	 */
227cc009e61SMukul Joshi 	m->cp_hqd_eop_control = min(0xA,
228cc009e61SMukul Joshi 		ffs(q->eop_ring_buffer_size / sizeof(unsigned int)) - 1 - 1);
229cc009e61SMukul Joshi 	m->cp_hqd_eop_base_addr_lo =
230cc009e61SMukul Joshi 			lower_32_bits(q->eop_ring_buffer_address >> 8);
231cc009e61SMukul Joshi 	m->cp_hqd_eop_base_addr_hi =
232cc009e61SMukul Joshi 			upper_32_bits(q->eop_ring_buffer_address >> 8);
233cc009e61SMukul Joshi 
234cc009e61SMukul Joshi 	m->cp_hqd_iq_timer = 0;
235cc009e61SMukul Joshi 
236cc009e61SMukul Joshi 	m->cp_hqd_vmid = q->vmid;
237cc009e61SMukul Joshi 
238cc009e61SMukul Joshi 	if (q->format == KFD_QUEUE_FORMAT_AQL) {
239cc009e61SMukul Joshi 		/* GC 10 removed WPP_CLAMP from PQ Control */
240cc009e61SMukul Joshi 		m->cp_hqd_pq_control |= CP_HQD_PQ_CONTROL__NO_UPDATE_RPTR_MASK |
241cc009e61SMukul Joshi 				2 << CP_HQD_PQ_CONTROL__SLOT_BASED_WPTR__SHIFT |
242cc009e61SMukul Joshi 				1 << CP_HQD_PQ_CONTROL__QUEUE_FULL_EN__SHIFT ;
243cc009e61SMukul Joshi 		m->cp_hqd_pq_doorbell_control |=
244cc009e61SMukul Joshi 			1 << CP_HQD_PQ_DOORBELL_CONTROL__DOORBELL_BIF_DROP__SHIFT;
245cc009e61SMukul Joshi 	}
246cc009e61SMukul Joshi 	if (mm->dev->cwsr_enabled)
247cc009e61SMukul Joshi 		m->cp_hqd_ctx_save_control = 0;
248cc009e61SMukul Joshi 
249cc009e61SMukul Joshi 	update_cu_mask(mm, mqd, minfo);
250cc009e61SMukul Joshi 	set_priority(m, q);
251cc009e61SMukul Joshi 
252cc009e61SMukul Joshi 	q->is_active = QUEUE_IS_ACTIVE(*q);
253cc009e61SMukul Joshi }
254cc009e61SMukul Joshi 
255cc009e61SMukul Joshi static uint32_t read_doorbell_id(void *mqd)
256cc009e61SMukul Joshi {
257cc009e61SMukul Joshi 	struct v11_compute_mqd *m = (struct v11_compute_mqd *)mqd;
258cc009e61SMukul Joshi 
259cc009e61SMukul Joshi 	return m->queue_doorbell_id0;
260cc009e61SMukul Joshi }
261cc009e61SMukul Joshi 
262cc009e61SMukul Joshi static int get_wave_state(struct mqd_manager *mm, void *mqd,
263cc009e61SMukul Joshi 			  void __user *ctl_stack,
264cc009e61SMukul Joshi 			  u32 *ctl_stack_used_size,
265cc009e61SMukul Joshi 			  u32 *save_area_used_size)
266cc009e61SMukul Joshi {
267cc009e61SMukul Joshi 	struct v11_compute_mqd *m;
268cc009e61SMukul Joshi 	/*struct mqd_user_context_save_area_header header;*/
269cc009e61SMukul Joshi 
270cc009e61SMukul Joshi 	m = get_mqd(mqd);
271cc009e61SMukul Joshi 
272cc009e61SMukul Joshi 	/* Control stack is written backwards, while workgroup context data
273cc009e61SMukul Joshi 	 * is written forwards. Both starts from m->cp_hqd_cntl_stack_size.
274cc009e61SMukul Joshi 	 * Current position is at m->cp_hqd_cntl_stack_offset and
275cc009e61SMukul Joshi 	 * m->cp_hqd_wg_state_offset, respectively.
276cc009e61SMukul Joshi 	 */
277cc009e61SMukul Joshi 	*ctl_stack_used_size = m->cp_hqd_cntl_stack_size -
278cc009e61SMukul Joshi 		m->cp_hqd_cntl_stack_offset;
279cc009e61SMukul Joshi 	*save_area_used_size = m->cp_hqd_wg_state_offset -
280cc009e61SMukul Joshi 		m->cp_hqd_cntl_stack_size;
281cc009e61SMukul Joshi 
282cc009e61SMukul Joshi 	/* Control stack is not copied to user mode for GFXv11 because
283cc009e61SMukul Joshi 	 * it's part of the context save area that is already
284cc009e61SMukul Joshi 	 * accessible to user mode
285cc009e61SMukul Joshi 	 */
286cc009e61SMukul Joshi /*
287cc009e61SMukul Joshi 	header.control_stack_size = *ctl_stack_used_size;
288cc009e61SMukul Joshi 	header.wave_state_size = *save_area_used_size;
289cc009e61SMukul Joshi 
290cc009e61SMukul Joshi 	header.wave_state_offset = m->cp_hqd_wg_state_offset;
291cc009e61SMukul Joshi 	header.control_stack_offset = m->cp_hqd_cntl_stack_offset;
292cc009e61SMukul Joshi 
293cc009e61SMukul Joshi 	if (copy_to_user(ctl_stack, &header, sizeof(header)))
294cc009e61SMukul Joshi 		return -EFAULT;
295cc009e61SMukul Joshi */
296cc009e61SMukul Joshi 	return 0;
297cc009e61SMukul Joshi }
298cc009e61SMukul Joshi 
299cc009e61SMukul Joshi static void init_mqd_hiq(struct mqd_manager *mm, void **mqd,
300cc009e61SMukul Joshi 			struct kfd_mem_obj *mqd_mem_obj, uint64_t *gart_addr,
301cc009e61SMukul Joshi 			struct queue_properties *q)
302cc009e61SMukul Joshi {
303cc009e61SMukul Joshi 	struct v11_compute_mqd *m;
304cc009e61SMukul Joshi 
305cc009e61SMukul Joshi 	init_mqd(mm, mqd, mqd_mem_obj, gart_addr, q);
306cc009e61SMukul Joshi 
307cc009e61SMukul Joshi 	m = get_mqd(*mqd);
308cc009e61SMukul Joshi 
309cc009e61SMukul Joshi 	m->cp_hqd_pq_control |= 1 << CP_HQD_PQ_CONTROL__PRIV_STATE__SHIFT |
310cc009e61SMukul Joshi 			1 << CP_HQD_PQ_CONTROL__KMD_QUEUE__SHIFT;
311cc009e61SMukul Joshi }
312cc009e61SMukul Joshi 
313cc009e61SMukul Joshi static void init_mqd_sdma(struct mqd_manager *mm, void **mqd,
314cc009e61SMukul Joshi 		struct kfd_mem_obj *mqd_mem_obj, uint64_t *gart_addr,
315cc009e61SMukul Joshi 		struct queue_properties *q)
316cc009e61SMukul Joshi {
317cc009e61SMukul Joshi 	struct v11_sdma_mqd *m;
3182e2b9bafSRuili Ji 	int size;
319cc009e61SMukul Joshi 
320cc009e61SMukul Joshi 	m = (struct v11_sdma_mqd *) mqd_mem_obj->cpu_ptr;
321cc009e61SMukul Joshi 
3222e2b9bafSRuili Ji 	if (mm->dev->shared_resources.enable_mes)
3232e2b9bafSRuili Ji 		size = PAGE_SIZE;
3242e2b9bafSRuili Ji 	else
3252e2b9bafSRuili Ji 		size = sizeof(struct v11_sdma_mqd);
326cc009e61SMukul Joshi 
3272e2b9bafSRuili Ji 	memset(m, 0, size);
328cc009e61SMukul Joshi 	*mqd = m;
329cc009e61SMukul Joshi 	if (gart_addr)
330cc009e61SMukul Joshi 		*gart_addr = mqd_mem_obj->gpu_addr;
331cc009e61SMukul Joshi 
332cc009e61SMukul Joshi 	mm->update_mqd(mm, m, q, NULL);
333cc009e61SMukul Joshi }
334cc009e61SMukul Joshi 
335cc009e61SMukul Joshi #define SDMA_RLC_DUMMY_DEFAULT 0xf
336cc009e61SMukul Joshi 
337cc009e61SMukul Joshi static void update_mqd_sdma(struct mqd_manager *mm, void *mqd,
338cc009e61SMukul Joshi 		struct queue_properties *q,
339cc009e61SMukul Joshi 		struct mqd_update_info *minfo)
340cc009e61SMukul Joshi {
341cc009e61SMukul Joshi 	struct v11_sdma_mqd *m;
342cc009e61SMukul Joshi 
343cc009e61SMukul Joshi 	m = get_sdma_mqd(mqd);
344cc009e61SMukul Joshi 	m->sdmax_rlcx_rb_cntl = (ffs(q->queue_size / sizeof(unsigned int)) - 1)
345cc009e61SMukul Joshi 		<< SDMA0_QUEUE0_RB_CNTL__RB_SIZE__SHIFT |
346cc009e61SMukul Joshi 		q->vmid << SDMA0_QUEUE0_RB_CNTL__RB_VMID__SHIFT |
347cc009e61SMukul Joshi 		1 << SDMA0_QUEUE0_RB_CNTL__RPTR_WRITEBACK_ENABLE__SHIFT |
34821a550deSRuili Ji 		6 << SDMA0_QUEUE0_RB_CNTL__RPTR_WRITEBACK_TIMER__SHIFT |
34921a550deSRuili Ji 		1 << SDMA0_QUEUE0_RB_CNTL__F32_WPTR_POLL_ENABLE__SHIFT;
350cc009e61SMukul Joshi 
351cc009e61SMukul Joshi 	m->sdmax_rlcx_rb_base = lower_32_bits(q->queue_address >> 8);
352cc009e61SMukul Joshi 	m->sdmax_rlcx_rb_base_hi = upper_32_bits(q->queue_address >> 8);
353cc009e61SMukul Joshi 	m->sdmax_rlcx_rb_rptr_addr_lo = lower_32_bits((uint64_t)q->read_ptr);
354cc009e61SMukul Joshi 	m->sdmax_rlcx_rb_rptr_addr_hi = upper_32_bits((uint64_t)q->read_ptr);
355e77a541fSGraham Sider 	m->sdmax_rlcx_rb_wptr_poll_addr_lo = lower_32_bits((uint64_t)q->write_ptr);
356e77a541fSGraham Sider 	m->sdmax_rlcx_rb_wptr_poll_addr_hi = upper_32_bits((uint64_t)q->write_ptr);
357cc009e61SMukul Joshi 	m->sdmax_rlcx_doorbell_offset =
358cc009e61SMukul Joshi 		q->doorbell_off << SDMA0_QUEUE0_DOORBELL_OFFSET__OFFSET__SHIFT;
359cc009e61SMukul Joshi 
360*27488686SGraham Sider 	m->sdmax_rlcx_sched_cntl = (amdgpu_sdma_phase_quantum
361*27488686SGraham Sider 		<< SDMA0_QUEUE0_SCHEDULE_CNTL__CONTEXT_QUANTUM__SHIFT)
362*27488686SGraham Sider 		 & SDMA0_QUEUE0_SCHEDULE_CNTL__CONTEXT_QUANTUM_MASK;
363*27488686SGraham Sider 
364cc009e61SMukul Joshi 	m->sdma_engine_id = q->sdma_engine_id;
365cc009e61SMukul Joshi 	m->sdma_queue_id = q->sdma_queue_id;
366cc009e61SMukul Joshi 	m->sdmax_rlcx_dummy_reg = SDMA_RLC_DUMMY_DEFAULT;
367cc009e61SMukul Joshi 
368cc009e61SMukul Joshi 	q->is_active = QUEUE_IS_ACTIVE(*q);
369cc009e61SMukul Joshi }
370cc009e61SMukul Joshi 
371cc009e61SMukul Joshi #if defined(CONFIG_DEBUG_FS)
372cc009e61SMukul Joshi 
373cc009e61SMukul Joshi static int debugfs_show_mqd(struct seq_file *m, void *data)
374cc009e61SMukul Joshi {
375cc009e61SMukul Joshi 	seq_hex_dump(m, "    ", DUMP_PREFIX_OFFSET, 32, 4,
376cc009e61SMukul Joshi 		     data, sizeof(struct v11_compute_mqd), false);
377cc009e61SMukul Joshi 	return 0;
378cc009e61SMukul Joshi }
379cc009e61SMukul Joshi 
380cc009e61SMukul Joshi static int debugfs_show_mqd_sdma(struct seq_file *m, void *data)
381cc009e61SMukul Joshi {
382cc009e61SMukul Joshi 	seq_hex_dump(m, "    ", DUMP_PREFIX_OFFSET, 32, 4,
383cc009e61SMukul Joshi 		     data, sizeof(struct v11_sdma_mqd), false);
384cc009e61SMukul Joshi 	return 0;
385cc009e61SMukul Joshi }
386cc009e61SMukul Joshi 
387cc009e61SMukul Joshi #endif
388cc009e61SMukul Joshi 
389cc009e61SMukul Joshi struct mqd_manager *mqd_manager_init_v11(enum KFD_MQD_TYPE type,
390cc009e61SMukul Joshi 		struct kfd_dev *dev)
391cc009e61SMukul Joshi {
392cc009e61SMukul Joshi 	struct mqd_manager *mqd;
393cc009e61SMukul Joshi 
394cc009e61SMukul Joshi 	if (WARN_ON(type >= KFD_MQD_TYPE_MAX))
395cc009e61SMukul Joshi 		return NULL;
396cc009e61SMukul Joshi 
397cc009e61SMukul Joshi 	mqd = kzalloc(sizeof(*mqd), GFP_KERNEL);
398cc009e61SMukul Joshi 	if (!mqd)
399cc009e61SMukul Joshi 		return NULL;
400cc009e61SMukul Joshi 
401cc009e61SMukul Joshi 	mqd->dev = dev;
402cc009e61SMukul Joshi 
403cc009e61SMukul Joshi 	switch (type) {
404cc009e61SMukul Joshi 	case KFD_MQD_TYPE_CP:
405cc009e61SMukul Joshi 		pr_debug("%s@%i\n", __func__, __LINE__);
406cc009e61SMukul Joshi 		mqd->allocate_mqd = allocate_mqd;
407cc009e61SMukul Joshi 		mqd->init_mqd = init_mqd;
408b98451dcSShiwu Zhang 		mqd->free_mqd = kfd_free_mqd_cp;
409cc009e61SMukul Joshi 		mqd->load_mqd = load_mqd;
410cc009e61SMukul Joshi 		mqd->update_mqd = update_mqd;
411b98451dcSShiwu Zhang 		mqd->destroy_mqd = kfd_destroy_mqd_cp;
412b98451dcSShiwu Zhang 		mqd->is_occupied = kfd_is_occupied_cp;
413cc009e61SMukul Joshi 		mqd->mqd_size = sizeof(struct v11_compute_mqd);
414cc009e61SMukul Joshi 		mqd->get_wave_state = get_wave_state;
415cc009e61SMukul Joshi #if defined(CONFIG_DEBUG_FS)
416cc009e61SMukul Joshi 		mqd->debugfs_show_mqd = debugfs_show_mqd;
417cc009e61SMukul Joshi #endif
418cc009e61SMukul Joshi 		pr_debug("%s@%i\n", __func__, __LINE__);
419cc009e61SMukul Joshi 		break;
420cc009e61SMukul Joshi 	case KFD_MQD_TYPE_HIQ:
421cc009e61SMukul Joshi 		pr_debug("%s@%i\n", __func__, __LINE__);
422cc009e61SMukul Joshi 		mqd->allocate_mqd = allocate_hiq_mqd;
423cc009e61SMukul Joshi 		mqd->init_mqd = init_mqd_hiq;
424cc009e61SMukul Joshi 		mqd->free_mqd = free_mqd_hiq_sdma;
425b98451dcSShiwu Zhang 		mqd->load_mqd = kfd_hiq_load_mqd_kiq;
426cc009e61SMukul Joshi 		mqd->update_mqd = update_mqd;
427b98451dcSShiwu Zhang 		mqd->destroy_mqd = kfd_destroy_mqd_cp;
428b98451dcSShiwu Zhang 		mqd->is_occupied = kfd_is_occupied_cp;
429cc009e61SMukul Joshi 		mqd->mqd_size = sizeof(struct v11_compute_mqd);
430cc009e61SMukul Joshi #if defined(CONFIG_DEBUG_FS)
431cc009e61SMukul Joshi 		mqd->debugfs_show_mqd = debugfs_show_mqd;
432cc009e61SMukul Joshi #endif
433cc009e61SMukul Joshi 		mqd->read_doorbell_id = read_doorbell_id;
434cc009e61SMukul Joshi 		pr_debug("%s@%i\n", __func__, __LINE__);
435cc009e61SMukul Joshi 		break;
436cc009e61SMukul Joshi 	case KFD_MQD_TYPE_DIQ:
437cc009e61SMukul Joshi 		mqd->allocate_mqd = allocate_mqd;
438cc009e61SMukul Joshi 		mqd->init_mqd = init_mqd_hiq;
439b98451dcSShiwu Zhang 		mqd->free_mqd = kfd_free_mqd_cp;
440cc009e61SMukul Joshi 		mqd->load_mqd = load_mqd;
441cc009e61SMukul Joshi 		mqd->update_mqd = update_mqd;
442b98451dcSShiwu Zhang 		mqd->destroy_mqd = kfd_destroy_mqd_cp;
443b98451dcSShiwu Zhang 		mqd->is_occupied = kfd_is_occupied_cp;
444cc009e61SMukul Joshi 		mqd->mqd_size = sizeof(struct v11_compute_mqd);
445cc009e61SMukul Joshi #if defined(CONFIG_DEBUG_FS)
446cc009e61SMukul Joshi 		mqd->debugfs_show_mqd = debugfs_show_mqd;
447cc009e61SMukul Joshi #endif
448cc009e61SMukul Joshi 		break;
449cc009e61SMukul Joshi 	case KFD_MQD_TYPE_SDMA:
450cc009e61SMukul Joshi 		pr_debug("%s@%i\n", __func__, __LINE__);
451cc009e61SMukul Joshi 		mqd->allocate_mqd = allocate_sdma_mqd;
452cc009e61SMukul Joshi 		mqd->init_mqd = init_mqd_sdma;
453cc009e61SMukul Joshi 		mqd->free_mqd = free_mqd_hiq_sdma;
454b98451dcSShiwu Zhang 		mqd->load_mqd = kfd_load_mqd_sdma;
455cc009e61SMukul Joshi 		mqd->update_mqd = update_mqd_sdma;
456b98451dcSShiwu Zhang 		mqd->destroy_mqd = kfd_destroy_mqd_sdma;
457b98451dcSShiwu Zhang 		mqd->is_occupied = kfd_is_occupied_sdma;
458cc009e61SMukul Joshi 		mqd->mqd_size = sizeof(struct v11_sdma_mqd);
459cc009e61SMukul Joshi #if defined(CONFIG_DEBUG_FS)
460cc009e61SMukul Joshi 		mqd->debugfs_show_mqd = debugfs_show_mqd_sdma;
461cc009e61SMukul Joshi #endif
4622e2b9bafSRuili Ji 		/*
4632e2b9bafSRuili Ji 		 * To allocate SDMA MQDs by generic functions
4642e2b9bafSRuili Ji 		 * when MES is enabled.
4652e2b9bafSRuili Ji 		 */
4662e2b9bafSRuili Ji 		if (dev->shared_resources.enable_mes) {
4672e2b9bafSRuili Ji 			mqd->allocate_mqd = allocate_mqd;
4682e2b9bafSRuili Ji 			mqd->free_mqd = kfd_free_mqd_cp;
4692e2b9bafSRuili Ji 		}
470cc009e61SMukul Joshi 		pr_debug("%s@%i\n", __func__, __LINE__);
471cc009e61SMukul Joshi 		break;
472cc009e61SMukul Joshi 	default:
473cc009e61SMukul Joshi 		kfree(mqd);
474cc009e61SMukul Joshi 		return NULL;
475cc009e61SMukul Joshi 	}
476cc009e61SMukul Joshi 
477cc009e61SMukul Joshi 	return mqd;
478cc009e61SMukul Joshi }
479