1 // SPDX-License-Identifier: GPL-2.0 OR MIT
2 /*
3  * Copyright 2014-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/mm_types.h>
28 
29 #include "kfd_priv.h"
30 #include "kfd_mqd_manager.h"
31 #include "cik_regs.h"
32 #include "cik_structs.h"
33 #include "oss/oss_2_4_sh_mask.h"
34 
35 static inline struct cik_mqd *get_mqd(void *mqd)
36 {
37 	return (struct cik_mqd *)mqd;
38 }
39 
40 static inline struct cik_sdma_rlc_registers *get_sdma_mqd(void *mqd)
41 {
42 	return (struct cik_sdma_rlc_registers *)mqd;
43 }
44 
45 static void update_cu_mask(struct mqd_manager *mm, void *mqd,
46 			struct mqd_update_info *minfo)
47 {
48 	struct cik_mqd *m;
49 	uint32_t se_mask[4] = {0}; /* 4 is the max # of SEs */
50 
51 	if (!minfo || !minfo->cu_mask.ptr)
52 		return;
53 
54 	mqd_symmetrically_map_cu_mask(mm,
55 		minfo->cu_mask.ptr, minfo->cu_mask.count, se_mask);
56 
57 	m = get_mqd(mqd);
58 	m->compute_static_thread_mgmt_se0 = se_mask[0];
59 	m->compute_static_thread_mgmt_se1 = se_mask[1];
60 	m->compute_static_thread_mgmt_se2 = se_mask[2];
61 	m->compute_static_thread_mgmt_se3 = se_mask[3];
62 
63 	pr_debug("Update cu mask to %#x %#x %#x %#x\n",
64 		m->compute_static_thread_mgmt_se0,
65 		m->compute_static_thread_mgmt_se1,
66 		m->compute_static_thread_mgmt_se2,
67 		m->compute_static_thread_mgmt_se3);
68 }
69 
70 static void set_priority(struct cik_mqd *m, struct queue_properties *q)
71 {
72 	m->cp_hqd_pipe_priority = pipe_priority_map[q->priority];
73 	m->cp_hqd_queue_priority = q->priority;
74 }
75 
76 static struct kfd_mem_obj *allocate_mqd(struct kfd_node *kfd,
77 					struct queue_properties *q)
78 {
79 	struct kfd_mem_obj *mqd_mem_obj;
80 
81 	if (kfd_gtt_sa_allocate(kfd, sizeof(struct cik_mqd),
82 			&mqd_mem_obj))
83 		return NULL;
84 
85 	return mqd_mem_obj;
86 }
87 
88 static void init_mqd(struct mqd_manager *mm, void **mqd,
89 		struct kfd_mem_obj *mqd_mem_obj, uint64_t *gart_addr,
90 		struct queue_properties *q)
91 {
92 	uint64_t addr;
93 	struct cik_mqd *m;
94 
95 	m = (struct cik_mqd *) mqd_mem_obj->cpu_ptr;
96 	addr = mqd_mem_obj->gpu_addr;
97 
98 	memset(m, 0, ALIGN(sizeof(struct cik_mqd), 256));
99 
100 	m->header = 0xC0310800;
101 	m->compute_pipelinestat_enable = 1;
102 	m->compute_static_thread_mgmt_se0 = 0xFFFFFFFF;
103 	m->compute_static_thread_mgmt_se1 = 0xFFFFFFFF;
104 	m->compute_static_thread_mgmt_se2 = 0xFFFFFFFF;
105 	m->compute_static_thread_mgmt_se3 = 0xFFFFFFFF;
106 
107 	/*
108 	 * Make sure to use the last queue state saved on mqd when the cp
109 	 * reassigns the queue, so when queue is switched on/off (e.g over
110 	 * subscription or quantum timeout) the context will be consistent
111 	 */
112 	m->cp_hqd_persistent_state =
113 				DEFAULT_CP_HQD_PERSISTENT_STATE | PRELOAD_REQ;
114 
115 	m->cp_mqd_control             = MQD_CONTROL_PRIV_STATE_EN;
116 	m->cp_mqd_base_addr_lo        = lower_32_bits(addr);
117 	m->cp_mqd_base_addr_hi        = upper_32_bits(addr);
118 
119 	m->cp_hqd_quantum = QUANTUM_EN | QUANTUM_SCALE_1MS |
120 				QUANTUM_DURATION(10);
121 
122 	/*
123 	 * Pipe Priority
124 	 * Identifies the pipe relative priority when this queue is connected
125 	 * to the pipeline. The pipe priority is against the GFX pipe and HP3D.
126 	 * In KFD we are using a fixed pipe priority set to CS_MEDIUM.
127 	 * 0 = CS_LOW (typically below GFX)
128 	 * 1 = CS_MEDIUM (typically between HP3D and GFX
129 	 * 2 = CS_HIGH (typically above HP3D)
130 	 */
131 	set_priority(m, q);
132 
133 	if (q->format == KFD_QUEUE_FORMAT_AQL)
134 		m->cp_hqd_iq_rptr = AQL_ENABLE;
135 
136 	*mqd = m;
137 	if (gart_addr)
138 		*gart_addr = addr;
139 	mm->update_mqd(mm, m, q, NULL);
140 }
141 
142 static void init_mqd_sdma(struct mqd_manager *mm, void **mqd,
143 			struct kfd_mem_obj *mqd_mem_obj, uint64_t *gart_addr,
144 			struct queue_properties *q)
145 {
146 	struct cik_sdma_rlc_registers *m;
147 
148 	m = (struct cik_sdma_rlc_registers *) mqd_mem_obj->cpu_ptr;
149 
150 	memset(m, 0, sizeof(struct cik_sdma_rlc_registers));
151 
152 	*mqd = m;
153 	if (gart_addr)
154 		*gart_addr = mqd_mem_obj->gpu_addr;
155 
156 	mm->update_mqd(mm, m, q, NULL);
157 }
158 
159 static int load_mqd(struct mqd_manager *mm, void *mqd, uint32_t pipe_id,
160 		    uint32_t queue_id, struct queue_properties *p,
161 		    struct mm_struct *mms)
162 {
163 	/* AQL write pointer counts in 64B packets, PM4/CP counts in dwords. */
164 	uint32_t wptr_shift = (p->format == KFD_QUEUE_FORMAT_AQL ? 4 : 0);
165 	uint32_t wptr_mask = (uint32_t)((p->queue_size / 4) - 1);
166 
167 	return mm->dev->kfd2kgd->hqd_load(mm->dev->adev, mqd, pipe_id, queue_id,
168 					  (uint32_t __user *)p->write_ptr,
169 					  wptr_shift, wptr_mask, mms, 0);
170 }
171 
172 static void __update_mqd(struct mqd_manager *mm, void *mqd,
173 			struct queue_properties *q, struct mqd_update_info *minfo,
174 			unsigned int atc_bit)
175 {
176 	struct cik_mqd *m;
177 
178 	m = get_mqd(mqd);
179 	m->cp_hqd_pq_control = DEFAULT_RPTR_BLOCK_SIZE |
180 				DEFAULT_MIN_AVAIL_SIZE;
181 	m->cp_hqd_ib_control = DEFAULT_MIN_IB_AVAIL_SIZE;
182 	if (atc_bit) {
183 		m->cp_hqd_pq_control |= PQ_ATC_EN;
184 		m->cp_hqd_ib_control |= IB_ATC_EN;
185 	}
186 
187 	/*
188 	 * Calculating queue size which is log base 2 of actual queue size -1
189 	 * dwords and another -1 for ffs
190 	 */
191 	m->cp_hqd_pq_control |= order_base_2(q->queue_size / 4) - 1;
192 	m->cp_hqd_pq_base_lo = lower_32_bits((uint64_t)q->queue_address >> 8);
193 	m->cp_hqd_pq_base_hi = upper_32_bits((uint64_t)q->queue_address >> 8);
194 	m->cp_hqd_pq_rptr_report_addr_lo = lower_32_bits((uint64_t)q->read_ptr);
195 	m->cp_hqd_pq_rptr_report_addr_hi = upper_32_bits((uint64_t)q->read_ptr);
196 	m->cp_hqd_pq_doorbell_control = DOORBELL_OFFSET(q->doorbell_off);
197 
198 	m->cp_hqd_vmid = q->vmid;
199 
200 	if (q->format == KFD_QUEUE_FORMAT_AQL)
201 		m->cp_hqd_pq_control |= NO_UPDATE_RPTR;
202 
203 	update_cu_mask(mm, mqd, minfo);
204 	set_priority(m, q);
205 
206 	q->is_active = QUEUE_IS_ACTIVE(*q);
207 }
208 
209 static void update_mqd(struct mqd_manager *mm, void *mqd,
210 			struct queue_properties *q,
211 			struct mqd_update_info *minfo)
212 {
213 	__update_mqd(mm, mqd, q, minfo, 1);
214 }
215 
216 static uint32_t read_doorbell_id(void *mqd)
217 {
218 	struct cik_mqd *m = (struct cik_mqd *)mqd;
219 
220 	return m->queue_doorbell_id0;
221 }
222 
223 static void update_mqd_hawaii(struct mqd_manager *mm, void *mqd,
224 			struct queue_properties *q,
225 			struct mqd_update_info *minfo)
226 {
227 	__update_mqd(mm, mqd, q, minfo, 0);
228 }
229 
230 static void update_mqd_sdma(struct mqd_manager *mm, void *mqd,
231 			struct queue_properties *q,
232 			struct mqd_update_info *minfo)
233 {
234 	struct cik_sdma_rlc_registers *m;
235 
236 	m = get_sdma_mqd(mqd);
237 	m->sdma_rlc_rb_cntl = order_base_2(q->queue_size / 4)
238 			<< SDMA0_RLC0_RB_CNTL__RB_SIZE__SHIFT |
239 			q->vmid << SDMA0_RLC0_RB_CNTL__RB_VMID__SHIFT |
240 			1 << SDMA0_RLC0_RB_CNTL__RPTR_WRITEBACK_ENABLE__SHIFT |
241 			6 << SDMA0_RLC0_RB_CNTL__RPTR_WRITEBACK_TIMER__SHIFT;
242 
243 	m->sdma_rlc_rb_base = lower_32_bits(q->queue_address >> 8);
244 	m->sdma_rlc_rb_base_hi = upper_32_bits(q->queue_address >> 8);
245 	m->sdma_rlc_rb_rptr_addr_lo = lower_32_bits((uint64_t)q->read_ptr);
246 	m->sdma_rlc_rb_rptr_addr_hi = upper_32_bits((uint64_t)q->read_ptr);
247 	m->sdma_rlc_doorbell =
248 		q->doorbell_off << SDMA0_RLC0_DOORBELL__OFFSET__SHIFT;
249 
250 	m->sdma_rlc_virtual_addr = q->sdma_vm_addr;
251 
252 	m->sdma_engine_id = q->sdma_engine_id;
253 	m->sdma_queue_id = q->sdma_queue_id;
254 
255 	q->is_active = QUEUE_IS_ACTIVE(*q);
256 }
257 
258 static void checkpoint_mqd(struct mqd_manager *mm, void *mqd, void *mqd_dst, void *ctl_stack_dst)
259 {
260 	struct cik_mqd *m;
261 
262 	m = get_mqd(mqd);
263 
264 	memcpy(mqd_dst, m, sizeof(struct cik_mqd));
265 }
266 
267 static void restore_mqd(struct mqd_manager *mm, void **mqd,
268 			struct kfd_mem_obj *mqd_mem_obj, uint64_t *gart_addr,
269 			struct queue_properties *qp,
270 			const void *mqd_src,
271 			const void *ctl_stack_src, const u32 ctl_stack_size)
272 {
273 	uint64_t addr;
274 	struct cik_mqd *m;
275 
276 	m = (struct cik_mqd *) mqd_mem_obj->cpu_ptr;
277 	addr = mqd_mem_obj->gpu_addr;
278 
279 	memcpy(m, mqd_src, sizeof(*m));
280 
281 	*mqd = m;
282 	if (gart_addr)
283 		*gart_addr = addr;
284 
285 	m->cp_hqd_pq_doorbell_control = DOORBELL_OFFSET(qp->doorbell_off);
286 
287 	pr_debug("cp_hqd_pq_doorbell_control 0x%x\n",
288 			m->cp_hqd_pq_doorbell_control);
289 
290 	qp->is_active = 0;
291 }
292 
293 static void checkpoint_mqd_sdma(struct mqd_manager *mm,
294 				void *mqd,
295 				void *mqd_dst,
296 				void *ctl_stack_dst)
297 {
298 	struct cik_sdma_rlc_registers *m;
299 
300 	m = get_sdma_mqd(mqd);
301 
302 	memcpy(mqd_dst, m, sizeof(struct cik_sdma_rlc_registers));
303 }
304 
305 static void restore_mqd_sdma(struct mqd_manager *mm, void **mqd,
306 				struct kfd_mem_obj *mqd_mem_obj, uint64_t *gart_addr,
307 				struct queue_properties *qp,
308 				const void *mqd_src,
309 				const void *ctl_stack_src, const u32 ctl_stack_size)
310 {
311 	uint64_t addr;
312 	struct cik_sdma_rlc_registers *m;
313 
314 	m = (struct cik_sdma_rlc_registers *) mqd_mem_obj->cpu_ptr;
315 	addr = mqd_mem_obj->gpu_addr;
316 
317 	memcpy(m, mqd_src, sizeof(*m));
318 
319 	m->sdma_rlc_doorbell =
320 		qp->doorbell_off << SDMA0_RLC0_DOORBELL__OFFSET__SHIFT;
321 
322 	*mqd = m;
323 	if (gart_addr)
324 		*gart_addr = addr;
325 
326 	qp->is_active = 0;
327 }
328 
329 /*
330  * HIQ MQD Implementation, concrete implementation for HIQ MQD implementation.
331  * The HIQ queue in Kaveri is using the same MQD structure as all the user mode
332  * queues but with different initial values.
333  */
334 
335 static void init_mqd_hiq(struct mqd_manager *mm, void **mqd,
336 		struct kfd_mem_obj *mqd_mem_obj, uint64_t *gart_addr,
337 		struct queue_properties *q)
338 {
339 	init_mqd(mm, mqd, mqd_mem_obj, gart_addr, q);
340 }
341 
342 static void update_mqd_hiq(struct mqd_manager *mm, void *mqd,
343 			struct queue_properties *q,
344 			struct mqd_update_info *minfo)
345 {
346 	struct cik_mqd *m;
347 
348 	m = get_mqd(mqd);
349 	m->cp_hqd_pq_control = DEFAULT_RPTR_BLOCK_SIZE |
350 				DEFAULT_MIN_AVAIL_SIZE |
351 				PRIV_STATE |
352 				KMD_QUEUE;
353 
354 	/*
355 	 * Calculating queue size which is log base 2 of actual queue
356 	 * size -1 dwords
357 	 */
358 	m->cp_hqd_pq_control |= order_base_2(q->queue_size / 4) - 1;
359 	m->cp_hqd_pq_base_lo = lower_32_bits((uint64_t)q->queue_address >> 8);
360 	m->cp_hqd_pq_base_hi = upper_32_bits((uint64_t)q->queue_address >> 8);
361 	m->cp_hqd_pq_rptr_report_addr_lo = lower_32_bits((uint64_t)q->read_ptr);
362 	m->cp_hqd_pq_rptr_report_addr_hi = upper_32_bits((uint64_t)q->read_ptr);
363 	m->cp_hqd_pq_doorbell_control = DOORBELL_OFFSET(q->doorbell_off);
364 
365 	m->cp_hqd_vmid = q->vmid;
366 
367 	q->is_active = QUEUE_IS_ACTIVE(*q);
368 
369 	set_priority(m, q);
370 }
371 
372 #if defined(CONFIG_DEBUG_FS)
373 
374 static int debugfs_show_mqd(struct seq_file *m, void *data)
375 {
376 	seq_hex_dump(m, "    ", DUMP_PREFIX_OFFSET, 32, 4,
377 		     data, sizeof(struct cik_mqd), false);
378 	return 0;
379 }
380 
381 static int debugfs_show_mqd_sdma(struct seq_file *m, void *data)
382 {
383 	seq_hex_dump(m, "    ", DUMP_PREFIX_OFFSET, 32, 4,
384 		     data, sizeof(struct cik_sdma_rlc_registers), false);
385 	return 0;
386 }
387 
388 #endif
389 
390 
391 struct mqd_manager *mqd_manager_init_cik(enum KFD_MQD_TYPE type,
392 		struct kfd_node *dev)
393 {
394 	struct mqd_manager *mqd;
395 
396 	if (WARN_ON(type >= KFD_MQD_TYPE_MAX))
397 		return NULL;
398 
399 	mqd = kzalloc(sizeof(*mqd), GFP_KERNEL);
400 	if (!mqd)
401 		return NULL;
402 
403 	mqd->dev = dev;
404 
405 	switch (type) {
406 	case KFD_MQD_TYPE_CP:
407 		mqd->allocate_mqd = allocate_mqd;
408 		mqd->init_mqd = init_mqd;
409 		mqd->free_mqd = kfd_free_mqd_cp;
410 		mqd->load_mqd = load_mqd;
411 		mqd->update_mqd = update_mqd;
412 		mqd->destroy_mqd = kfd_destroy_mqd_cp;
413 		mqd->is_occupied = kfd_is_occupied_cp;
414 		mqd->checkpoint_mqd = checkpoint_mqd;
415 		mqd->restore_mqd = restore_mqd;
416 		mqd->mqd_size = sizeof(struct cik_mqd);
417 #if defined(CONFIG_DEBUG_FS)
418 		mqd->debugfs_show_mqd = debugfs_show_mqd;
419 #endif
420 		break;
421 	case KFD_MQD_TYPE_HIQ:
422 		mqd->allocate_mqd = allocate_hiq_mqd;
423 		mqd->init_mqd = init_mqd_hiq;
424 		mqd->free_mqd = free_mqd_hiq_sdma;
425 		mqd->load_mqd = load_mqd;
426 		mqd->update_mqd = update_mqd_hiq;
427 		mqd->destroy_mqd = kfd_destroy_mqd_cp;
428 		mqd->is_occupied = kfd_is_occupied_cp;
429 		mqd->mqd_size = sizeof(struct cik_mqd);
430 		mqd->mqd_stride = kfd_mqd_stride;
431 #if defined(CONFIG_DEBUG_FS)
432 		mqd->debugfs_show_mqd = debugfs_show_mqd;
433 #endif
434 		mqd->read_doorbell_id = read_doorbell_id;
435 		break;
436 	case KFD_MQD_TYPE_DIQ:
437 		mqd->allocate_mqd = allocate_mqd;
438 		mqd->init_mqd = init_mqd_hiq;
439 		mqd->free_mqd = kfd_free_mqd_cp;
440 		mqd->load_mqd = load_mqd;
441 		mqd->update_mqd = update_mqd_hiq;
442 		mqd->destroy_mqd = kfd_destroy_mqd_cp;
443 		mqd->is_occupied = kfd_is_occupied_cp;
444 		mqd->mqd_size = sizeof(struct cik_mqd);
445 		mqd->mqd_stride = kfd_mqd_stride;
446 #if defined(CONFIG_DEBUG_FS)
447 		mqd->debugfs_show_mqd = debugfs_show_mqd;
448 #endif
449 		break;
450 	case KFD_MQD_TYPE_SDMA:
451 		mqd->allocate_mqd = allocate_sdma_mqd;
452 		mqd->init_mqd = init_mqd_sdma;
453 		mqd->free_mqd = free_mqd_hiq_sdma;
454 		mqd->load_mqd = kfd_load_mqd_sdma;
455 		mqd->update_mqd = update_mqd_sdma;
456 		mqd->destroy_mqd = kfd_destroy_mqd_sdma;
457 		mqd->is_occupied = kfd_is_occupied_sdma;
458 		mqd->checkpoint_mqd = checkpoint_mqd_sdma;
459 		mqd->restore_mqd = restore_mqd_sdma;
460 		mqd->mqd_size = sizeof(struct cik_sdma_rlc_registers);
461 		mqd->mqd_stride = kfd_mqd_stride;
462 #if defined(CONFIG_DEBUG_FS)
463 		mqd->debugfs_show_mqd = debugfs_show_mqd_sdma;
464 #endif
465 		break;
466 	default:
467 		kfree(mqd);
468 		return NULL;
469 	}
470 
471 	return mqd;
472 }
473 
474 struct mqd_manager *mqd_manager_init_cik_hawaii(enum KFD_MQD_TYPE type,
475 			struct kfd_node *dev)
476 {
477 	struct mqd_manager *mqd;
478 
479 	mqd = mqd_manager_init_cik(type, dev);
480 	if (!mqd)
481 		return NULL;
482 	if (type == KFD_MQD_TYPE_CP)
483 		mqd->update_mqd = update_mqd_hawaii;
484 	return mqd;
485 }
486