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/ratelimit.h>
26 #include <linux/printk.h>
27 #include <linux/slab.h>
28 #include <linux/list.h>
29 #include <linux/types.h>
30 #include <linux/bitops.h>
31 #include <linux/sched.h>
32 #include "kfd_priv.h"
33 #include "kfd_device_queue_manager.h"
34 #include "kfd_mqd_manager.h"
35 #include "cik_regs.h"
36 #include "kfd_kernel_queue.h"
37 #include "amdgpu_amdkfd.h"
38 #include "amdgpu_reset.h"
39 #include "mes_v11_api_def.h"
40 #include "kfd_debug.h"
41 
42 /* Size of the per-pipe EOP queue */
43 #define CIK_HPD_EOP_BYTES_LOG2 11
44 #define CIK_HPD_EOP_BYTES (1U << CIK_HPD_EOP_BYTES_LOG2)
45 
46 static int set_pasid_vmid_mapping(struct device_queue_manager *dqm,
47 				  u32 pasid, unsigned int vmid);
48 
49 static int execute_queues_cpsch(struct device_queue_manager *dqm,
50 				enum kfd_unmap_queues_filter filter,
51 				uint32_t filter_param,
52 				uint32_t grace_period);
53 static int unmap_queues_cpsch(struct device_queue_manager *dqm,
54 				enum kfd_unmap_queues_filter filter,
55 				uint32_t filter_param,
56 				uint32_t grace_period,
57 				bool reset);
58 
59 static int map_queues_cpsch(struct device_queue_manager *dqm);
60 
61 static void deallocate_sdma_queue(struct device_queue_manager *dqm,
62 				struct queue *q);
63 
64 static inline void deallocate_hqd(struct device_queue_manager *dqm,
65 				struct queue *q);
66 static int allocate_hqd(struct device_queue_manager *dqm, struct queue *q);
67 static int allocate_sdma_queue(struct device_queue_manager *dqm,
68 				struct queue *q, const uint32_t *restore_sdma_id);
69 static void kfd_process_hw_exception(struct work_struct *work);
70 
71 static inline
get_mqd_type_from_queue_type(enum kfd_queue_type type)72 enum KFD_MQD_TYPE get_mqd_type_from_queue_type(enum kfd_queue_type type)
73 {
74 	if (type == KFD_QUEUE_TYPE_SDMA || type == KFD_QUEUE_TYPE_SDMA_XGMI)
75 		return KFD_MQD_TYPE_SDMA;
76 	return KFD_MQD_TYPE_CP;
77 }
78 
is_pipe_enabled(struct device_queue_manager * dqm,int mec,int pipe)79 static bool is_pipe_enabled(struct device_queue_manager *dqm, int mec, int pipe)
80 {
81 	int i;
82 	int pipe_offset = (mec * dqm->dev->kfd->shared_resources.num_pipe_per_mec
83 		+ pipe) * dqm->dev->kfd->shared_resources.num_queue_per_pipe;
84 
85 	/* queue is available for KFD usage if bit is 1 */
86 	for (i = 0; i <  dqm->dev->kfd->shared_resources.num_queue_per_pipe; ++i)
87 		if (test_bit(pipe_offset + i,
88 			      dqm->dev->kfd->shared_resources.cp_queue_bitmap))
89 			return true;
90 	return false;
91 }
92 
get_cp_queues_num(struct device_queue_manager * dqm)93 unsigned int get_cp_queues_num(struct device_queue_manager *dqm)
94 {
95 	return bitmap_weight(dqm->dev->kfd->shared_resources.cp_queue_bitmap,
96 				AMDGPU_MAX_QUEUES);
97 }
98 
get_queues_per_pipe(struct device_queue_manager * dqm)99 unsigned int get_queues_per_pipe(struct device_queue_manager *dqm)
100 {
101 	return dqm->dev->kfd->shared_resources.num_queue_per_pipe;
102 }
103 
get_pipes_per_mec(struct device_queue_manager * dqm)104 unsigned int get_pipes_per_mec(struct device_queue_manager *dqm)
105 {
106 	return dqm->dev->kfd->shared_resources.num_pipe_per_mec;
107 }
108 
get_num_all_sdma_engines(struct device_queue_manager * dqm)109 static unsigned int get_num_all_sdma_engines(struct device_queue_manager *dqm)
110 {
111 	return kfd_get_num_sdma_engines(dqm->dev) +
112 		kfd_get_num_xgmi_sdma_engines(dqm->dev);
113 }
114 
get_num_sdma_queues(struct device_queue_manager * dqm)115 unsigned int get_num_sdma_queues(struct device_queue_manager *dqm)
116 {
117 	return kfd_get_num_sdma_engines(dqm->dev) *
118 		dqm->dev->kfd->device_info.num_sdma_queues_per_engine;
119 }
120 
get_num_xgmi_sdma_queues(struct device_queue_manager * dqm)121 unsigned int get_num_xgmi_sdma_queues(struct device_queue_manager *dqm)
122 {
123 	return kfd_get_num_xgmi_sdma_engines(dqm->dev) *
124 		dqm->dev->kfd->device_info.num_sdma_queues_per_engine;
125 }
126 
init_sdma_bitmaps(struct device_queue_manager * dqm)127 static void init_sdma_bitmaps(struct device_queue_manager *dqm)
128 {
129 	bitmap_zero(dqm->sdma_bitmap, KFD_MAX_SDMA_QUEUES);
130 	bitmap_set(dqm->sdma_bitmap, 0, get_num_sdma_queues(dqm));
131 
132 	bitmap_zero(dqm->xgmi_sdma_bitmap, KFD_MAX_SDMA_QUEUES);
133 	bitmap_set(dqm->xgmi_sdma_bitmap, 0, get_num_xgmi_sdma_queues(dqm));
134 
135 	/* Mask out the reserved queues */
136 	bitmap_andnot(dqm->sdma_bitmap, dqm->sdma_bitmap,
137 		      dqm->dev->kfd->device_info.reserved_sdma_queues_bitmap,
138 		      KFD_MAX_SDMA_QUEUES);
139 }
140 
program_sh_mem_settings(struct device_queue_manager * dqm,struct qcm_process_device * qpd)141 void program_sh_mem_settings(struct device_queue_manager *dqm,
142 					struct qcm_process_device *qpd)
143 {
144 	uint32_t xcc_mask = dqm->dev->xcc_mask;
145 	int xcc_id;
146 
147 	for_each_inst(xcc_id, xcc_mask)
148 		dqm->dev->kfd2kgd->program_sh_mem_settings(
149 			dqm->dev->adev, qpd->vmid, qpd->sh_mem_config,
150 			qpd->sh_mem_ape1_base, qpd->sh_mem_ape1_limit,
151 			qpd->sh_mem_bases, xcc_id);
152 }
153 
kfd_hws_hang(struct device_queue_manager * dqm)154 static void kfd_hws_hang(struct device_queue_manager *dqm)
155 {
156 	struct device_process_node *cur;
157 	struct qcm_process_device *qpd;
158 	struct queue *q;
159 
160 	/* Mark all device queues as reset. */
161 	list_for_each_entry(cur, &dqm->queues, list) {
162 		qpd = cur->qpd;
163 		list_for_each_entry(q, &qpd->queues_list, list) {
164 			struct kfd_process_device *pdd = qpd_to_pdd(qpd);
165 
166 			pdd->has_reset_queue = true;
167 		}
168 	}
169 
170 	/*
171 	 * Issue a GPU reset if HWS is unresponsive
172 	 */
173 	schedule_work(&dqm->hw_exception_work);
174 }
175 
convert_to_mes_queue_type(int queue_type)176 static int convert_to_mes_queue_type(int queue_type)
177 {
178 	int mes_queue_type;
179 
180 	switch (queue_type) {
181 	case KFD_QUEUE_TYPE_COMPUTE:
182 		mes_queue_type = MES_QUEUE_TYPE_COMPUTE;
183 		break;
184 	case KFD_QUEUE_TYPE_SDMA:
185 		mes_queue_type = MES_QUEUE_TYPE_SDMA;
186 		break;
187 	default:
188 		WARN(1, "Invalid queue type %d", queue_type);
189 		mes_queue_type = -EINVAL;
190 		break;
191 	}
192 
193 	return mes_queue_type;
194 }
195 
add_queue_mes(struct device_queue_manager * dqm,struct queue * q,struct qcm_process_device * qpd)196 static int add_queue_mes(struct device_queue_manager *dqm, struct queue *q,
197 			 struct qcm_process_device *qpd)
198 {
199 	struct amdgpu_device *adev = (struct amdgpu_device *)dqm->dev->adev;
200 	struct kfd_process_device *pdd = qpd_to_pdd(qpd);
201 	struct mes_add_queue_input queue_input;
202 	int r, queue_type;
203 	uint64_t wptr_addr_off;
204 
205 	if (!down_read_trylock(&adev->reset_domain->sem))
206 		return -EIO;
207 
208 	if (!pdd->proc_ctx_cpu_ptr) {
209 		r = amdgpu_amdkfd_alloc_gtt_mem(adev,
210 				AMDGPU_MES_PROC_CTX_SIZE,
211 				&pdd->proc_ctx_bo,
212 				&pdd->proc_ctx_gpu_addr,
213 				&pdd->proc_ctx_cpu_ptr,
214 				false);
215 		if (r) {
216 			dev_err(adev->dev,
217 				"failed to allocate process context bo\n");
218 			return r;
219 		}
220 		memset(pdd->proc_ctx_cpu_ptr, 0, AMDGPU_MES_PROC_CTX_SIZE);
221 	}
222 
223 	memset(&queue_input, 0x0, sizeof(struct mes_add_queue_input));
224 	queue_input.process_id = qpd->pqm->process->pasid;
225 	queue_input.page_table_base_addr =  qpd->page_table_base;
226 	queue_input.process_va_start = 0;
227 	queue_input.process_va_end = adev->vm_manager.max_pfn - 1;
228 	/* MES unit for quantum is 100ns */
229 	queue_input.process_quantum = KFD_MES_PROCESS_QUANTUM;  /* Equivalent to 10ms. */
230 	queue_input.process_context_addr = pdd->proc_ctx_gpu_addr;
231 	queue_input.gang_quantum = KFD_MES_GANG_QUANTUM; /* Equivalent to 1ms */
232 	queue_input.gang_context_addr = q->gang_ctx_gpu_addr;
233 	queue_input.inprocess_gang_priority = q->properties.priority;
234 	queue_input.gang_global_priority_level =
235 					AMDGPU_MES_PRIORITY_LEVEL_NORMAL;
236 	queue_input.doorbell_offset = q->properties.doorbell_off;
237 	queue_input.mqd_addr = q->gart_mqd_addr;
238 	queue_input.wptr_addr = (uint64_t)q->properties.write_ptr;
239 
240 	wptr_addr_off = (uint64_t)q->properties.write_ptr & (PAGE_SIZE - 1);
241 	queue_input.wptr_mc_addr = amdgpu_bo_gpu_offset(q->properties.wptr_bo) + wptr_addr_off;
242 
243 	queue_input.is_kfd_process = 1;
244 	queue_input.is_aql_queue = (q->properties.format == KFD_QUEUE_FORMAT_AQL);
245 	queue_input.queue_size = q->properties.queue_size >> 2;
246 
247 	queue_input.paging = false;
248 	queue_input.tba_addr = qpd->tba_addr;
249 	queue_input.tma_addr = qpd->tma_addr;
250 	queue_input.trap_en = !kfd_dbg_has_cwsr_workaround(q->device);
251 	queue_input.skip_process_ctx_clear =
252 		qpd->pqm->process->runtime_info.runtime_state == DEBUG_RUNTIME_STATE_ENABLED &&
253 						(qpd->pqm->process->debug_trap_enabled ||
254 						 kfd_dbg_has_ttmps_always_setup(q->device));
255 
256 	queue_type = convert_to_mes_queue_type(q->properties.type);
257 	if (queue_type < 0) {
258 		dev_err(adev->dev, "Queue type not supported with MES, queue:%d\n",
259 			q->properties.type);
260 		up_read(&adev->reset_domain->sem);
261 		return -EINVAL;
262 	}
263 	queue_input.queue_type = (uint32_t)queue_type;
264 
265 	queue_input.exclusively_scheduled = q->properties.is_gws;
266 
267 	amdgpu_mes_lock(&adev->mes);
268 	r = adev->mes.funcs->add_hw_queue(&adev->mes, &queue_input);
269 	amdgpu_mes_unlock(&adev->mes);
270 	up_read(&adev->reset_domain->sem);
271 	if (r) {
272 		dev_err(adev->dev, "failed to add hardware queue to MES, doorbell=0x%x\n",
273 			q->properties.doorbell_off);
274 		dev_err(adev->dev, "MES might be in unrecoverable state, issue a GPU reset\n");
275 		kfd_hws_hang(dqm);
276 	}
277 
278 	return r;
279 }
280 
remove_queue_mes(struct device_queue_manager * dqm,struct queue * q,struct qcm_process_device * qpd)281 static int remove_queue_mes(struct device_queue_manager *dqm, struct queue *q,
282 			struct qcm_process_device *qpd)
283 {
284 	struct amdgpu_device *adev = (struct amdgpu_device *)dqm->dev->adev;
285 	int r;
286 	struct mes_remove_queue_input queue_input;
287 
288 	if (!down_read_trylock(&adev->reset_domain->sem))
289 		return -EIO;
290 
291 	memset(&queue_input, 0x0, sizeof(struct mes_remove_queue_input));
292 	queue_input.doorbell_offset = q->properties.doorbell_off;
293 	queue_input.gang_context_addr = q->gang_ctx_gpu_addr;
294 
295 	amdgpu_mes_lock(&adev->mes);
296 	r = adev->mes.funcs->remove_hw_queue(&adev->mes, &queue_input);
297 	amdgpu_mes_unlock(&adev->mes);
298 	up_read(&adev->reset_domain->sem);
299 
300 	if (r) {
301 		dev_err(adev->dev, "failed to remove hardware queue from MES, doorbell=0x%x\n",
302 			q->properties.doorbell_off);
303 		dev_err(adev->dev, "MES might be in unrecoverable state, issue a GPU reset\n");
304 		kfd_hws_hang(dqm);
305 	}
306 
307 	return r;
308 }
309 
remove_all_queues_mes(struct device_queue_manager * dqm)310 static int remove_all_queues_mes(struct device_queue_manager *dqm)
311 {
312 	struct device_process_node *cur;
313 	struct device *dev = dqm->dev->adev->dev;
314 	struct qcm_process_device *qpd;
315 	struct queue *q;
316 	int retval = 0;
317 
318 	list_for_each_entry(cur, &dqm->queues, list) {
319 		qpd = cur->qpd;
320 		list_for_each_entry(q, &qpd->queues_list, list) {
321 			if (q->properties.is_active) {
322 				retval = remove_queue_mes(dqm, q, qpd);
323 				if (retval) {
324 					dev_err(dev, "%s: Failed to remove queue %d for dev %d",
325 						__func__,
326 						q->properties.queue_id,
327 						dqm->dev->id);
328 					return retval;
329 				}
330 			}
331 		}
332 	}
333 
334 	return retval;
335 }
336 
suspend_all_queues_mes(struct device_queue_manager * dqm)337 static int suspend_all_queues_mes(struct device_queue_manager *dqm)
338 {
339 	struct amdgpu_device *adev = (struct amdgpu_device *)dqm->dev->adev;
340 	int r = 0;
341 
342 	if (!down_read_trylock(&adev->reset_domain->sem))
343 		return -EIO;
344 
345 	r = amdgpu_mes_suspend(adev);
346 	up_read(&adev->reset_domain->sem);
347 
348 	if (r) {
349 		dev_err(adev->dev, "failed to suspend gangs from MES\n");
350 		dev_err(adev->dev, "MES might be in unrecoverable state, issue a GPU reset\n");
351 		kfd_hws_hang(dqm);
352 	}
353 
354 	return r;
355 }
356 
resume_all_queues_mes(struct device_queue_manager * dqm)357 static int resume_all_queues_mes(struct device_queue_manager *dqm)
358 {
359 	struct amdgpu_device *adev = (struct amdgpu_device *)dqm->dev->adev;
360 	int r = 0;
361 
362 	if (!down_read_trylock(&adev->reset_domain->sem))
363 		return -EIO;
364 
365 	r = amdgpu_mes_resume(adev);
366 	up_read(&adev->reset_domain->sem);
367 
368 	if (r) {
369 		dev_err(adev->dev, "failed to resume gangs from MES\n");
370 		dev_err(adev->dev, "MES might be in unrecoverable state, issue a GPU reset\n");
371 		kfd_hws_hang(dqm);
372 	}
373 
374 	return r;
375 }
376 
increment_queue_count(struct device_queue_manager * dqm,struct qcm_process_device * qpd,struct queue * q)377 static void increment_queue_count(struct device_queue_manager *dqm,
378 				  struct qcm_process_device *qpd,
379 				  struct queue *q)
380 {
381 	dqm->active_queue_count++;
382 	if (q->properties.type == KFD_QUEUE_TYPE_COMPUTE ||
383 	    q->properties.type == KFD_QUEUE_TYPE_DIQ)
384 		dqm->active_cp_queue_count++;
385 
386 	if (q->properties.is_gws) {
387 		dqm->gws_queue_count++;
388 		qpd->mapped_gws_queue = true;
389 	}
390 }
391 
decrement_queue_count(struct device_queue_manager * dqm,struct qcm_process_device * qpd,struct queue * q)392 static void decrement_queue_count(struct device_queue_manager *dqm,
393 				  struct qcm_process_device *qpd,
394 				  struct queue *q)
395 {
396 	dqm->active_queue_count--;
397 	if (q->properties.type == KFD_QUEUE_TYPE_COMPUTE ||
398 	    q->properties.type == KFD_QUEUE_TYPE_DIQ)
399 		dqm->active_cp_queue_count--;
400 
401 	if (q->properties.is_gws) {
402 		dqm->gws_queue_count--;
403 		qpd->mapped_gws_queue = false;
404 	}
405 }
406 
407 /*
408  * Allocate a doorbell ID to this queue.
409  * If doorbell_id is passed in, make sure requested ID is valid then allocate it.
410  */
allocate_doorbell(struct qcm_process_device * qpd,struct queue * q,uint32_t const * restore_id)411 static int allocate_doorbell(struct qcm_process_device *qpd,
412 			     struct queue *q,
413 			     uint32_t const *restore_id)
414 {
415 	struct kfd_node *dev = qpd->dqm->dev;
416 
417 	if (!KFD_IS_SOC15(dev)) {
418 		/* On pre-SOC15 chips we need to use the queue ID to
419 		 * preserve the user mode ABI.
420 		 */
421 
422 		if (restore_id && *restore_id != q->properties.queue_id)
423 			return -EINVAL;
424 
425 		q->doorbell_id = q->properties.queue_id;
426 	} else if (q->properties.type == KFD_QUEUE_TYPE_SDMA ||
427 			q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) {
428 		/* For SDMA queues on SOC15 with 8-byte doorbell, use static
429 		 * doorbell assignments based on the engine and queue id.
430 		 * The doobell index distance between RLC (2*i) and (2*i+1)
431 		 * for a SDMA engine is 512.
432 		 */
433 
434 		uint32_t *idx_offset = dev->kfd->shared_resources.sdma_doorbell_idx;
435 
436 		/*
437 		 * q->properties.sdma_engine_id corresponds to the virtual
438 		 * sdma engine number. However, for doorbell allocation,
439 		 * we need the physical sdma engine id in order to get the
440 		 * correct doorbell offset.
441 		 */
442 		uint32_t valid_id = idx_offset[qpd->dqm->dev->node_id *
443 					       get_num_all_sdma_engines(qpd->dqm) +
444 					       q->properties.sdma_engine_id]
445 						+ (q->properties.sdma_queue_id & 1)
446 						* KFD_QUEUE_DOORBELL_MIRROR_OFFSET
447 						+ (q->properties.sdma_queue_id >> 1);
448 
449 		if (restore_id && *restore_id != valid_id)
450 			return -EINVAL;
451 		q->doorbell_id = valid_id;
452 	} else {
453 		/* For CP queues on SOC15 */
454 		if (restore_id) {
455 			/* make sure that ID is free  */
456 			if (__test_and_set_bit(*restore_id, qpd->doorbell_bitmap))
457 				return -EINVAL;
458 
459 			q->doorbell_id = *restore_id;
460 		} else {
461 			/* or reserve a free doorbell ID */
462 			unsigned int found;
463 
464 			found = find_first_zero_bit(qpd->doorbell_bitmap,
465 						    KFD_MAX_NUM_OF_QUEUES_PER_PROCESS);
466 			if (found >= KFD_MAX_NUM_OF_QUEUES_PER_PROCESS) {
467 				pr_debug("No doorbells available");
468 				return -EBUSY;
469 			}
470 			set_bit(found, qpd->doorbell_bitmap);
471 			q->doorbell_id = found;
472 		}
473 	}
474 
475 	q->properties.doorbell_off = amdgpu_doorbell_index_on_bar(dev->adev,
476 								  qpd->proc_doorbells,
477 								  q->doorbell_id,
478 								  dev->kfd->device_info.doorbell_size);
479 	return 0;
480 }
481 
deallocate_doorbell(struct qcm_process_device * qpd,struct queue * q)482 static void deallocate_doorbell(struct qcm_process_device *qpd,
483 				struct queue *q)
484 {
485 	unsigned int old;
486 	struct kfd_node *dev = qpd->dqm->dev;
487 
488 	if (!KFD_IS_SOC15(dev) ||
489 	    q->properties.type == KFD_QUEUE_TYPE_SDMA ||
490 	    q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)
491 		return;
492 
493 	old = test_and_clear_bit(q->doorbell_id, qpd->doorbell_bitmap);
494 	WARN_ON(!old);
495 }
496 
program_trap_handler_settings(struct device_queue_manager * dqm,struct qcm_process_device * qpd)497 static void program_trap_handler_settings(struct device_queue_manager *dqm,
498 				struct qcm_process_device *qpd)
499 {
500 	uint32_t xcc_mask = dqm->dev->xcc_mask;
501 	int xcc_id;
502 
503 	if (dqm->dev->kfd2kgd->program_trap_handler_settings)
504 		for_each_inst(xcc_id, xcc_mask)
505 			dqm->dev->kfd2kgd->program_trap_handler_settings(
506 				dqm->dev->adev, qpd->vmid, qpd->tba_addr,
507 				qpd->tma_addr, xcc_id);
508 }
509 
allocate_vmid(struct device_queue_manager * dqm,struct qcm_process_device * qpd,struct queue * q)510 static int allocate_vmid(struct device_queue_manager *dqm,
511 			struct qcm_process_device *qpd,
512 			struct queue *q)
513 {
514 	struct device *dev = dqm->dev->adev->dev;
515 	int allocated_vmid = -1, i;
516 
517 	for (i = dqm->dev->vm_info.first_vmid_kfd;
518 			i <= dqm->dev->vm_info.last_vmid_kfd; i++) {
519 		if (!dqm->vmid_pasid[i]) {
520 			allocated_vmid = i;
521 			break;
522 		}
523 	}
524 
525 	if (allocated_vmid < 0) {
526 		dev_err(dev, "no more vmid to allocate\n");
527 		return -ENOSPC;
528 	}
529 
530 	pr_debug("vmid allocated: %d\n", allocated_vmid);
531 
532 	dqm->vmid_pasid[allocated_vmid] = q->process->pasid;
533 
534 	set_pasid_vmid_mapping(dqm, q->process->pasid, allocated_vmid);
535 
536 	qpd->vmid = allocated_vmid;
537 	q->properties.vmid = allocated_vmid;
538 
539 	program_sh_mem_settings(dqm, qpd);
540 
541 	if (KFD_IS_SOC15(dqm->dev) && dqm->dev->kfd->cwsr_enabled)
542 		program_trap_handler_settings(dqm, qpd);
543 
544 	/* qpd->page_table_base is set earlier when register_process()
545 	 * is called, i.e. when the first queue is created.
546 	 */
547 	dqm->dev->kfd2kgd->set_vm_context_page_table_base(dqm->dev->adev,
548 			qpd->vmid,
549 			qpd->page_table_base);
550 	/* invalidate the VM context after pasid and vmid mapping is set up */
551 	kfd_flush_tlb(qpd_to_pdd(qpd), TLB_FLUSH_LEGACY);
552 
553 	if (dqm->dev->kfd2kgd->set_scratch_backing_va)
554 		dqm->dev->kfd2kgd->set_scratch_backing_va(dqm->dev->adev,
555 				qpd->sh_hidden_private_base, qpd->vmid);
556 
557 	return 0;
558 }
559 
flush_texture_cache_nocpsch(struct kfd_node * kdev,struct qcm_process_device * qpd)560 static int flush_texture_cache_nocpsch(struct kfd_node *kdev,
561 				struct qcm_process_device *qpd)
562 {
563 	const struct packet_manager_funcs *pmf = qpd->dqm->packet_mgr.pmf;
564 	int ret;
565 
566 	if (!qpd->ib_kaddr)
567 		return -ENOMEM;
568 
569 	ret = pmf->release_mem(qpd->ib_base, (uint32_t *)qpd->ib_kaddr);
570 	if (ret)
571 		return ret;
572 
573 	return amdgpu_amdkfd_submit_ib(kdev->adev, KGD_ENGINE_MEC1, qpd->vmid,
574 				qpd->ib_base, (uint32_t *)qpd->ib_kaddr,
575 				pmf->release_mem_size / sizeof(uint32_t));
576 }
577 
deallocate_vmid(struct device_queue_manager * dqm,struct qcm_process_device * qpd,struct queue * q)578 static void deallocate_vmid(struct device_queue_manager *dqm,
579 				struct qcm_process_device *qpd,
580 				struct queue *q)
581 {
582 	struct device *dev = dqm->dev->adev->dev;
583 
584 	/* On GFX v7, CP doesn't flush TC at dequeue */
585 	if (q->device->adev->asic_type == CHIP_HAWAII)
586 		if (flush_texture_cache_nocpsch(q->device, qpd))
587 			dev_err(dev, "Failed to flush TC\n");
588 
589 	kfd_flush_tlb(qpd_to_pdd(qpd), TLB_FLUSH_LEGACY);
590 
591 	/* Release the vmid mapping */
592 	set_pasid_vmid_mapping(dqm, 0, qpd->vmid);
593 	dqm->vmid_pasid[qpd->vmid] = 0;
594 
595 	qpd->vmid = 0;
596 	q->properties.vmid = 0;
597 }
598 
create_queue_nocpsch(struct device_queue_manager * dqm,struct queue * q,struct qcm_process_device * qpd,const struct kfd_criu_queue_priv_data * qd,const void * restore_mqd,const void * restore_ctl_stack)599 static int create_queue_nocpsch(struct device_queue_manager *dqm,
600 				struct queue *q,
601 				struct qcm_process_device *qpd,
602 				const struct kfd_criu_queue_priv_data *qd,
603 				const void *restore_mqd, const void *restore_ctl_stack)
604 {
605 	struct mqd_manager *mqd_mgr;
606 	int retval;
607 
608 	dqm_lock(dqm);
609 
610 	if (dqm->total_queue_count >= max_num_of_queues_per_device) {
611 		pr_warn("Can't create new usermode queue because %d queues were already created\n",
612 				dqm->total_queue_count);
613 		retval = -EPERM;
614 		goto out_unlock;
615 	}
616 
617 	if (list_empty(&qpd->queues_list)) {
618 		retval = allocate_vmid(dqm, qpd, q);
619 		if (retval)
620 			goto out_unlock;
621 	}
622 	q->properties.vmid = qpd->vmid;
623 	/*
624 	 * Eviction state logic: mark all queues as evicted, even ones
625 	 * not currently active. Restoring inactive queues later only
626 	 * updates the is_evicted flag but is a no-op otherwise.
627 	 */
628 	q->properties.is_evicted = !!qpd->evicted;
629 
630 	q->properties.tba_addr = qpd->tba_addr;
631 	q->properties.tma_addr = qpd->tma_addr;
632 
633 	mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type(
634 			q->properties.type)];
635 	if (q->properties.type == KFD_QUEUE_TYPE_COMPUTE) {
636 		retval = allocate_hqd(dqm, q);
637 		if (retval)
638 			goto deallocate_vmid;
639 		pr_debug("Loading mqd to hqd on pipe %d, queue %d\n",
640 			q->pipe, q->queue);
641 	} else if (q->properties.type == KFD_QUEUE_TYPE_SDMA ||
642 		q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) {
643 		retval = allocate_sdma_queue(dqm, q, qd ? &qd->sdma_id : NULL);
644 		if (retval)
645 			goto deallocate_vmid;
646 		dqm->asic_ops.init_sdma_vm(dqm, q, qpd);
647 	}
648 
649 	retval = allocate_doorbell(qpd, q, qd ? &qd->doorbell_id : NULL);
650 	if (retval)
651 		goto out_deallocate_hqd;
652 
653 	/* Temporarily release dqm lock to avoid a circular lock dependency */
654 	dqm_unlock(dqm);
655 	q->mqd_mem_obj = mqd_mgr->allocate_mqd(mqd_mgr->dev, &q->properties);
656 	dqm_lock(dqm);
657 
658 	if (!q->mqd_mem_obj) {
659 		retval = -ENOMEM;
660 		goto out_deallocate_doorbell;
661 	}
662 
663 	if (qd)
664 		mqd_mgr->restore_mqd(mqd_mgr, &q->mqd, q->mqd_mem_obj, &q->gart_mqd_addr,
665 				     &q->properties, restore_mqd, restore_ctl_stack,
666 				     qd->ctl_stack_size);
667 	else
668 		mqd_mgr->init_mqd(mqd_mgr, &q->mqd, q->mqd_mem_obj,
669 					&q->gart_mqd_addr, &q->properties);
670 
671 	if (q->properties.is_active) {
672 		if (!dqm->sched_running) {
673 			WARN_ONCE(1, "Load non-HWS mqd while stopped\n");
674 			goto add_queue_to_list;
675 		}
676 
677 		if (WARN(q->process->mm != current->mm,
678 					"should only run in user thread"))
679 			retval = -EFAULT;
680 		else
681 			retval = mqd_mgr->load_mqd(mqd_mgr, q->mqd, q->pipe,
682 					q->queue, &q->properties, current->mm);
683 		if (retval)
684 			goto out_free_mqd;
685 	}
686 
687 add_queue_to_list:
688 	list_add(&q->list, &qpd->queues_list);
689 	qpd->queue_count++;
690 	if (q->properties.is_active)
691 		increment_queue_count(dqm, qpd, q);
692 
693 	/*
694 	 * Unconditionally increment this counter, regardless of the queue's
695 	 * type or whether the queue is active.
696 	 */
697 	dqm->total_queue_count++;
698 	pr_debug("Total of %d queues are accountable so far\n",
699 			dqm->total_queue_count);
700 	goto out_unlock;
701 
702 out_free_mqd:
703 	mqd_mgr->free_mqd(mqd_mgr, q->mqd, q->mqd_mem_obj);
704 out_deallocate_doorbell:
705 	deallocate_doorbell(qpd, q);
706 out_deallocate_hqd:
707 	if (q->properties.type == KFD_QUEUE_TYPE_COMPUTE)
708 		deallocate_hqd(dqm, q);
709 	else if (q->properties.type == KFD_QUEUE_TYPE_SDMA ||
710 		q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)
711 		deallocate_sdma_queue(dqm, q);
712 deallocate_vmid:
713 	if (list_empty(&qpd->queues_list))
714 		deallocate_vmid(dqm, qpd, q);
715 out_unlock:
716 	dqm_unlock(dqm);
717 	return retval;
718 }
719 
allocate_hqd(struct device_queue_manager * dqm,struct queue * q)720 static int allocate_hqd(struct device_queue_manager *dqm, struct queue *q)
721 {
722 	bool set;
723 	int pipe, bit, i;
724 
725 	set = false;
726 
727 	for (pipe = dqm->next_pipe_to_allocate, i = 0;
728 			i < get_pipes_per_mec(dqm);
729 			pipe = ((pipe + 1) % get_pipes_per_mec(dqm)), ++i) {
730 
731 		if (!is_pipe_enabled(dqm, 0, pipe))
732 			continue;
733 
734 		if (dqm->allocated_queues[pipe] != 0) {
735 			bit = ffs(dqm->allocated_queues[pipe]) - 1;
736 			dqm->allocated_queues[pipe] &= ~(1 << bit);
737 			q->pipe = pipe;
738 			q->queue = bit;
739 			set = true;
740 			break;
741 		}
742 	}
743 
744 	if (!set)
745 		return -EBUSY;
746 
747 	pr_debug("hqd slot - pipe %d, queue %d\n", q->pipe, q->queue);
748 	/* horizontal hqd allocation */
749 	dqm->next_pipe_to_allocate = (pipe + 1) % get_pipes_per_mec(dqm);
750 
751 	return 0;
752 }
753 
deallocate_hqd(struct device_queue_manager * dqm,struct queue * q)754 static inline void deallocate_hqd(struct device_queue_manager *dqm,
755 				struct queue *q)
756 {
757 	dqm->allocated_queues[q->pipe] |= (1 << q->queue);
758 }
759 
760 #define SQ_IND_CMD_CMD_KILL		0x00000003
761 #define SQ_IND_CMD_MODE_BROADCAST	0x00000001
762 
dbgdev_wave_reset_wavefronts(struct kfd_node * dev,struct kfd_process * p)763 static int dbgdev_wave_reset_wavefronts(struct kfd_node *dev, struct kfd_process *p)
764 {
765 	int status = 0;
766 	unsigned int vmid;
767 	uint16_t queried_pasid;
768 	union SQ_CMD_BITS reg_sq_cmd;
769 	union GRBM_GFX_INDEX_BITS reg_gfx_index;
770 	struct kfd_process_device *pdd;
771 	int first_vmid_to_scan = dev->vm_info.first_vmid_kfd;
772 	int last_vmid_to_scan = dev->vm_info.last_vmid_kfd;
773 	uint32_t xcc_mask = dev->xcc_mask;
774 	int xcc_id;
775 
776 	reg_sq_cmd.u32All = 0;
777 	reg_gfx_index.u32All = 0;
778 
779 	pr_debug("Killing all process wavefronts\n");
780 
781 	if (!dev->kfd2kgd->get_atc_vmid_pasid_mapping_info) {
782 		dev_err(dev->adev->dev, "no vmid pasid mapping supported\n");
783 		return -EOPNOTSUPP;
784 	}
785 
786 	/* Scan all registers in the range ATC_VMID8_PASID_MAPPING ..
787 	 * ATC_VMID15_PASID_MAPPING
788 	 * to check which VMID the current process is mapped to.
789 	 */
790 
791 	for (vmid = first_vmid_to_scan; vmid <= last_vmid_to_scan; vmid++) {
792 		status = dev->kfd2kgd->get_atc_vmid_pasid_mapping_info
793 				(dev->adev, vmid, &queried_pasid);
794 
795 		if (status && queried_pasid == p->pasid) {
796 			pr_debug("Killing wave fronts of vmid %d and pasid 0x%x\n",
797 					vmid, p->pasid);
798 			break;
799 		}
800 	}
801 
802 	if (vmid > last_vmid_to_scan) {
803 		dev_err(dev->adev->dev, "Didn't find vmid for pasid 0x%x\n", p->pasid);
804 		return -EFAULT;
805 	}
806 
807 	/* taking the VMID for that process on the safe way using PDD */
808 	pdd = kfd_get_process_device_data(dev, p);
809 	if (!pdd)
810 		return -EFAULT;
811 
812 	reg_gfx_index.bits.sh_broadcast_writes = 1;
813 	reg_gfx_index.bits.se_broadcast_writes = 1;
814 	reg_gfx_index.bits.instance_broadcast_writes = 1;
815 	reg_sq_cmd.bits.mode = SQ_IND_CMD_MODE_BROADCAST;
816 	reg_sq_cmd.bits.cmd = SQ_IND_CMD_CMD_KILL;
817 	reg_sq_cmd.bits.vm_id = vmid;
818 
819 	for_each_inst(xcc_id, xcc_mask)
820 		dev->kfd2kgd->wave_control_execute(
821 			dev->adev, reg_gfx_index.u32All,
822 			reg_sq_cmd.u32All, xcc_id);
823 
824 	return 0;
825 }
826 
827 /* Access to DQM has to be locked before calling destroy_queue_nocpsch_locked
828  * to avoid asynchronized access
829  */
destroy_queue_nocpsch_locked(struct device_queue_manager * dqm,struct qcm_process_device * qpd,struct queue * q)830 static int destroy_queue_nocpsch_locked(struct device_queue_manager *dqm,
831 				struct qcm_process_device *qpd,
832 				struct queue *q)
833 {
834 	int retval;
835 	struct mqd_manager *mqd_mgr;
836 
837 	mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type(
838 			q->properties.type)];
839 
840 	if (q->properties.type == KFD_QUEUE_TYPE_COMPUTE)
841 		deallocate_hqd(dqm, q);
842 	else if (q->properties.type == KFD_QUEUE_TYPE_SDMA)
843 		deallocate_sdma_queue(dqm, q);
844 	else if (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)
845 		deallocate_sdma_queue(dqm, q);
846 	else {
847 		pr_debug("q->properties.type %d is invalid\n",
848 				q->properties.type);
849 		return -EINVAL;
850 	}
851 	dqm->total_queue_count--;
852 
853 	deallocate_doorbell(qpd, q);
854 
855 	if (!dqm->sched_running) {
856 		WARN_ONCE(1, "Destroy non-HWS queue while stopped\n");
857 		return 0;
858 	}
859 
860 	retval = mqd_mgr->destroy_mqd(mqd_mgr, q->mqd,
861 				KFD_PREEMPT_TYPE_WAVEFRONT_RESET,
862 				KFD_UNMAP_LATENCY_MS,
863 				q->pipe, q->queue);
864 	if (retval == -ETIME)
865 		qpd->reset_wavefronts = true;
866 
867 	list_del(&q->list);
868 	if (list_empty(&qpd->queues_list)) {
869 		if (qpd->reset_wavefronts) {
870 			pr_warn("Resetting wave fronts (nocpsch) on dev %p\n",
871 					dqm->dev);
872 			/* dbgdev_wave_reset_wavefronts has to be called before
873 			 * deallocate_vmid(), i.e. when vmid is still in use.
874 			 */
875 			dbgdev_wave_reset_wavefronts(dqm->dev,
876 					qpd->pqm->process);
877 			qpd->reset_wavefronts = false;
878 		}
879 
880 		deallocate_vmid(dqm, qpd, q);
881 	}
882 	qpd->queue_count--;
883 	if (q->properties.is_active)
884 		decrement_queue_count(dqm, qpd, q);
885 
886 	return retval;
887 }
888 
destroy_queue_nocpsch(struct device_queue_manager * dqm,struct qcm_process_device * qpd,struct queue * q)889 static int destroy_queue_nocpsch(struct device_queue_manager *dqm,
890 				struct qcm_process_device *qpd,
891 				struct queue *q)
892 {
893 	int retval;
894 	uint64_t sdma_val = 0;
895 	struct device *dev = dqm->dev->adev->dev;
896 	struct kfd_process_device *pdd = qpd_to_pdd(qpd);
897 	struct mqd_manager *mqd_mgr =
898 		dqm->mqd_mgrs[get_mqd_type_from_queue_type(q->properties.type)];
899 
900 	/* Get the SDMA queue stats */
901 	if ((q->properties.type == KFD_QUEUE_TYPE_SDMA) ||
902 	    (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)) {
903 		retval = read_sdma_queue_counter((uint64_t __user *)q->properties.read_ptr,
904 							&sdma_val);
905 		if (retval)
906 			dev_err(dev, "Failed to read SDMA queue counter for queue: %d\n",
907 				q->properties.queue_id);
908 	}
909 
910 	dqm_lock(dqm);
911 	retval = destroy_queue_nocpsch_locked(dqm, qpd, q);
912 	if (!retval)
913 		pdd->sdma_past_activity_counter += sdma_val;
914 	dqm_unlock(dqm);
915 
916 	mqd_mgr->free_mqd(mqd_mgr, q->mqd, q->mqd_mem_obj);
917 
918 	return retval;
919 }
920 
update_queue(struct device_queue_manager * dqm,struct queue * q,struct mqd_update_info * minfo)921 static int update_queue(struct device_queue_manager *dqm, struct queue *q,
922 			struct mqd_update_info *minfo)
923 {
924 	int retval = 0;
925 	struct device *dev = dqm->dev->adev->dev;
926 	struct mqd_manager *mqd_mgr;
927 	struct kfd_process_device *pdd;
928 	bool prev_active = false;
929 
930 	dqm_lock(dqm);
931 	pdd = kfd_get_process_device_data(q->device, q->process);
932 	if (!pdd) {
933 		retval = -ENODEV;
934 		goto out_unlock;
935 	}
936 	mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type(
937 			q->properties.type)];
938 
939 	/* Save previous activity state for counters */
940 	prev_active = q->properties.is_active;
941 
942 	/* Make sure the queue is unmapped before updating the MQD */
943 	if (dqm->sched_policy != KFD_SCHED_POLICY_NO_HWS) {
944 		if (!dqm->dev->kfd->shared_resources.enable_mes)
945 			retval = unmap_queues_cpsch(dqm,
946 						    KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 0, USE_DEFAULT_GRACE_PERIOD, false);
947 		else if (prev_active)
948 			retval = remove_queue_mes(dqm, q, &pdd->qpd);
949 
950 		/* queue is reset so inaccessable  */
951 		if (pdd->has_reset_queue) {
952 			retval = -EACCES;
953 			goto out_unlock;
954 		}
955 
956 		if (retval) {
957 			dev_err(dev, "unmap queue failed\n");
958 			goto out_unlock;
959 		}
960 	} else if (prev_active &&
961 		   (q->properties.type == KFD_QUEUE_TYPE_COMPUTE ||
962 		    q->properties.type == KFD_QUEUE_TYPE_SDMA ||
963 		    q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)) {
964 
965 		if (!dqm->sched_running) {
966 			WARN_ONCE(1, "Update non-HWS queue while stopped\n");
967 			goto out_unlock;
968 		}
969 
970 		retval = mqd_mgr->destroy_mqd(mqd_mgr, q->mqd,
971 				(dqm->dev->kfd->cwsr_enabled ?
972 				 KFD_PREEMPT_TYPE_WAVEFRONT_SAVE :
973 				 KFD_PREEMPT_TYPE_WAVEFRONT_DRAIN),
974 				KFD_UNMAP_LATENCY_MS, q->pipe, q->queue);
975 		if (retval) {
976 			dev_err(dev, "destroy mqd failed\n");
977 			goto out_unlock;
978 		}
979 	}
980 
981 	mqd_mgr->update_mqd(mqd_mgr, q->mqd, &q->properties, minfo);
982 
983 	/*
984 	 * check active state vs. the previous state and modify
985 	 * counter accordingly. map_queues_cpsch uses the
986 	 * dqm->active_queue_count to determine whether a new runlist must be
987 	 * uploaded.
988 	 */
989 	if (q->properties.is_active && !prev_active) {
990 		increment_queue_count(dqm, &pdd->qpd, q);
991 	} else if (!q->properties.is_active && prev_active) {
992 		decrement_queue_count(dqm, &pdd->qpd, q);
993 	} else if (q->gws && !q->properties.is_gws) {
994 		if (q->properties.is_active) {
995 			dqm->gws_queue_count++;
996 			pdd->qpd.mapped_gws_queue = true;
997 		}
998 		q->properties.is_gws = true;
999 	} else if (!q->gws && q->properties.is_gws) {
1000 		if (q->properties.is_active) {
1001 			dqm->gws_queue_count--;
1002 			pdd->qpd.mapped_gws_queue = false;
1003 		}
1004 		q->properties.is_gws = false;
1005 	}
1006 
1007 	if (dqm->sched_policy != KFD_SCHED_POLICY_NO_HWS) {
1008 		if (!dqm->dev->kfd->shared_resources.enable_mes)
1009 			retval = map_queues_cpsch(dqm);
1010 		else if (q->properties.is_active)
1011 			retval = add_queue_mes(dqm, q, &pdd->qpd);
1012 	} else if (q->properties.is_active &&
1013 		 (q->properties.type == KFD_QUEUE_TYPE_COMPUTE ||
1014 		  q->properties.type == KFD_QUEUE_TYPE_SDMA ||
1015 		  q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)) {
1016 		if (WARN(q->process->mm != current->mm,
1017 			 "should only run in user thread"))
1018 			retval = -EFAULT;
1019 		else
1020 			retval = mqd_mgr->load_mqd(mqd_mgr, q->mqd,
1021 						   q->pipe, q->queue,
1022 						   &q->properties, current->mm);
1023 	}
1024 
1025 out_unlock:
1026 	dqm_unlock(dqm);
1027 	return retval;
1028 }
1029 
1030 /* suspend_single_queue does not lock the dqm like the
1031  * evict_process_queues_cpsch or evict_process_queues_nocpsch. You should
1032  * lock the dqm before calling, and unlock after calling.
1033  *
1034  * The reason we don't lock the dqm is because this function may be
1035  * called on multiple queues in a loop, so rather than locking/unlocking
1036  * multiple times, we will just keep the dqm locked for all of the calls.
1037  */
suspend_single_queue(struct device_queue_manager * dqm,struct kfd_process_device * pdd,struct queue * q)1038 static int suspend_single_queue(struct device_queue_manager *dqm,
1039 				      struct kfd_process_device *pdd,
1040 				      struct queue *q)
1041 {
1042 	bool is_new;
1043 
1044 	if (q->properties.is_suspended)
1045 		return 0;
1046 
1047 	pr_debug("Suspending PASID %u queue [%i]\n",
1048 			pdd->process->pasid,
1049 			q->properties.queue_id);
1050 
1051 	is_new = q->properties.exception_status & KFD_EC_MASK(EC_QUEUE_NEW);
1052 
1053 	if (is_new || q->properties.is_being_destroyed) {
1054 		pr_debug("Suspend: skip %s queue id %i\n",
1055 				is_new ? "new" : "destroyed",
1056 				q->properties.queue_id);
1057 		return -EBUSY;
1058 	}
1059 
1060 	q->properties.is_suspended = true;
1061 	if (q->properties.is_active) {
1062 		if (dqm->dev->kfd->shared_resources.enable_mes) {
1063 			int r = remove_queue_mes(dqm, q, &pdd->qpd);
1064 
1065 			if (r)
1066 				return r;
1067 		}
1068 
1069 		decrement_queue_count(dqm, &pdd->qpd, q);
1070 		q->properties.is_active = false;
1071 	}
1072 
1073 	return 0;
1074 }
1075 
1076 /* resume_single_queue does not lock the dqm like the functions
1077  * restore_process_queues_cpsch or restore_process_queues_nocpsch. You should
1078  * lock the dqm before calling, and unlock after calling.
1079  *
1080  * The reason we don't lock the dqm is because this function may be
1081  * called on multiple queues in a loop, so rather than locking/unlocking
1082  * multiple times, we will just keep the dqm locked for all of the calls.
1083  */
resume_single_queue(struct device_queue_manager * dqm,struct qcm_process_device * qpd,struct queue * q)1084 static int resume_single_queue(struct device_queue_manager *dqm,
1085 				      struct qcm_process_device *qpd,
1086 				      struct queue *q)
1087 {
1088 	struct kfd_process_device *pdd;
1089 
1090 	if (!q->properties.is_suspended)
1091 		return 0;
1092 
1093 	pdd = qpd_to_pdd(qpd);
1094 
1095 	pr_debug("Restoring from suspend PASID %u queue [%i]\n",
1096 			    pdd->process->pasid,
1097 			    q->properties.queue_id);
1098 
1099 	q->properties.is_suspended = false;
1100 
1101 	if (QUEUE_IS_ACTIVE(q->properties)) {
1102 		if (dqm->dev->kfd->shared_resources.enable_mes) {
1103 			int r = add_queue_mes(dqm, q, &pdd->qpd);
1104 
1105 			if (r)
1106 				return r;
1107 		}
1108 
1109 		q->properties.is_active = true;
1110 		increment_queue_count(dqm, qpd, q);
1111 	}
1112 
1113 	return 0;
1114 }
1115 
evict_process_queues_nocpsch(struct device_queue_manager * dqm,struct qcm_process_device * qpd)1116 static int evict_process_queues_nocpsch(struct device_queue_manager *dqm,
1117 					struct qcm_process_device *qpd)
1118 {
1119 	struct queue *q;
1120 	struct mqd_manager *mqd_mgr;
1121 	struct kfd_process_device *pdd;
1122 	int retval, ret = 0;
1123 
1124 	dqm_lock(dqm);
1125 	if (qpd->evicted++ > 0) /* already evicted, do nothing */
1126 		goto out;
1127 
1128 	pdd = qpd_to_pdd(qpd);
1129 	pr_debug_ratelimited("Evicting PASID 0x%x queues\n",
1130 			    pdd->process->pasid);
1131 
1132 	pdd->last_evict_timestamp = get_jiffies_64();
1133 	/* Mark all queues as evicted. Deactivate all active queues on
1134 	 * the qpd.
1135 	 */
1136 	list_for_each_entry(q, &qpd->queues_list, list) {
1137 		q->properties.is_evicted = true;
1138 		if (!q->properties.is_active)
1139 			continue;
1140 
1141 		mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type(
1142 				q->properties.type)];
1143 		q->properties.is_active = false;
1144 		decrement_queue_count(dqm, qpd, q);
1145 
1146 		if (WARN_ONCE(!dqm->sched_running, "Evict when stopped\n"))
1147 			continue;
1148 
1149 		retval = mqd_mgr->destroy_mqd(mqd_mgr, q->mqd,
1150 				(dqm->dev->kfd->cwsr_enabled ?
1151 				 KFD_PREEMPT_TYPE_WAVEFRONT_SAVE :
1152 				 KFD_PREEMPT_TYPE_WAVEFRONT_DRAIN),
1153 				KFD_UNMAP_LATENCY_MS, q->pipe, q->queue);
1154 		if (retval && !ret)
1155 			/* Return the first error, but keep going to
1156 			 * maintain a consistent eviction state
1157 			 */
1158 			ret = retval;
1159 	}
1160 
1161 out:
1162 	dqm_unlock(dqm);
1163 	return ret;
1164 }
1165 
evict_process_queues_cpsch(struct device_queue_manager * dqm,struct qcm_process_device * qpd)1166 static int evict_process_queues_cpsch(struct device_queue_manager *dqm,
1167 				      struct qcm_process_device *qpd)
1168 {
1169 	struct queue *q;
1170 	struct device *dev = dqm->dev->adev->dev;
1171 	struct kfd_process_device *pdd;
1172 	int retval = 0;
1173 
1174 	dqm_lock(dqm);
1175 	if (qpd->evicted++ > 0) /* already evicted, do nothing */
1176 		goto out;
1177 
1178 	pdd = qpd_to_pdd(qpd);
1179 
1180 	/* The debugger creates processes that temporarily have not acquired
1181 	 * all VMs for all devices and has no VMs itself.
1182 	 * Skip queue eviction on process eviction.
1183 	 */
1184 	if (!pdd->drm_priv)
1185 		goto out;
1186 
1187 	pr_debug_ratelimited("Evicting PASID 0x%x queues\n",
1188 			    pdd->process->pasid);
1189 
1190 	/* Mark all queues as evicted. Deactivate all active queues on
1191 	 * the qpd.
1192 	 */
1193 	list_for_each_entry(q, &qpd->queues_list, list) {
1194 		q->properties.is_evicted = true;
1195 		if (!q->properties.is_active)
1196 			continue;
1197 
1198 		q->properties.is_active = false;
1199 		decrement_queue_count(dqm, qpd, q);
1200 
1201 		if (dqm->dev->kfd->shared_resources.enable_mes) {
1202 			retval = remove_queue_mes(dqm, q, qpd);
1203 			if (retval) {
1204 				dev_err(dev, "Failed to evict queue %d\n",
1205 					q->properties.queue_id);
1206 				goto out;
1207 			}
1208 		}
1209 	}
1210 	pdd->last_evict_timestamp = get_jiffies_64();
1211 	if (!dqm->dev->kfd->shared_resources.enable_mes)
1212 		retval = execute_queues_cpsch(dqm,
1213 					      qpd->is_debug ?
1214 					      KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES :
1215 					      KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 0,
1216 					      USE_DEFAULT_GRACE_PERIOD);
1217 
1218 out:
1219 	dqm_unlock(dqm);
1220 	return retval;
1221 }
1222 
restore_process_queues_nocpsch(struct device_queue_manager * dqm,struct qcm_process_device * qpd)1223 static int restore_process_queues_nocpsch(struct device_queue_manager *dqm,
1224 					  struct qcm_process_device *qpd)
1225 {
1226 	struct mm_struct *mm = NULL;
1227 	struct queue *q;
1228 	struct mqd_manager *mqd_mgr;
1229 	struct kfd_process_device *pdd;
1230 	uint64_t pd_base;
1231 	uint64_t eviction_duration;
1232 	int retval, ret = 0;
1233 
1234 	pdd = qpd_to_pdd(qpd);
1235 	/* Retrieve PD base */
1236 	pd_base = amdgpu_amdkfd_gpuvm_get_process_page_dir(pdd->drm_priv);
1237 
1238 	dqm_lock(dqm);
1239 	if (WARN_ON_ONCE(!qpd->evicted)) /* already restored, do nothing */
1240 		goto out;
1241 	if (qpd->evicted > 1) { /* ref count still > 0, decrement & quit */
1242 		qpd->evicted--;
1243 		goto out;
1244 	}
1245 
1246 	pr_debug_ratelimited("Restoring PASID 0x%x queues\n",
1247 			    pdd->process->pasid);
1248 
1249 	/* Update PD Base in QPD */
1250 	qpd->page_table_base = pd_base;
1251 	pr_debug("Updated PD address to 0x%llx\n", pd_base);
1252 
1253 	if (!list_empty(&qpd->queues_list)) {
1254 		dqm->dev->kfd2kgd->set_vm_context_page_table_base(
1255 				dqm->dev->adev,
1256 				qpd->vmid,
1257 				qpd->page_table_base);
1258 		kfd_flush_tlb(pdd, TLB_FLUSH_LEGACY);
1259 	}
1260 
1261 	/* Take a safe reference to the mm_struct, which may otherwise
1262 	 * disappear even while the kfd_process is still referenced.
1263 	 */
1264 	mm = get_task_mm(pdd->process->lead_thread);
1265 	if (!mm) {
1266 		ret = -EFAULT;
1267 		goto out;
1268 	}
1269 
1270 	/* Remove the eviction flags. Activate queues that are not
1271 	 * inactive for other reasons.
1272 	 */
1273 	list_for_each_entry(q, &qpd->queues_list, list) {
1274 		q->properties.is_evicted = false;
1275 		if (!QUEUE_IS_ACTIVE(q->properties))
1276 			continue;
1277 
1278 		mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type(
1279 				q->properties.type)];
1280 		q->properties.is_active = true;
1281 		increment_queue_count(dqm, qpd, q);
1282 
1283 		if (WARN_ONCE(!dqm->sched_running, "Restore when stopped\n"))
1284 			continue;
1285 
1286 		retval = mqd_mgr->load_mqd(mqd_mgr, q->mqd, q->pipe,
1287 				       q->queue, &q->properties, mm);
1288 		if (retval && !ret)
1289 			/* Return the first error, but keep going to
1290 			 * maintain a consistent eviction state
1291 			 */
1292 			ret = retval;
1293 	}
1294 	qpd->evicted = 0;
1295 	eviction_duration = get_jiffies_64() - pdd->last_evict_timestamp;
1296 	atomic64_add(eviction_duration, &pdd->evict_duration_counter);
1297 out:
1298 	if (mm)
1299 		mmput(mm);
1300 	dqm_unlock(dqm);
1301 	return ret;
1302 }
1303 
restore_process_queues_cpsch(struct device_queue_manager * dqm,struct qcm_process_device * qpd)1304 static int restore_process_queues_cpsch(struct device_queue_manager *dqm,
1305 					struct qcm_process_device *qpd)
1306 {
1307 	struct queue *q;
1308 	struct device *dev = dqm->dev->adev->dev;
1309 	struct kfd_process_device *pdd;
1310 	uint64_t eviction_duration;
1311 	int retval = 0;
1312 
1313 	pdd = qpd_to_pdd(qpd);
1314 
1315 	dqm_lock(dqm);
1316 	if (WARN_ON_ONCE(!qpd->evicted)) /* already restored, do nothing */
1317 		goto out;
1318 	if (qpd->evicted > 1) { /* ref count still > 0, decrement & quit */
1319 		qpd->evicted--;
1320 		goto out;
1321 	}
1322 
1323 	/* The debugger creates processes that temporarily have not acquired
1324 	 * all VMs for all devices and has no VMs itself.
1325 	 * Skip queue restore on process restore.
1326 	 */
1327 	if (!pdd->drm_priv)
1328 		goto vm_not_acquired;
1329 
1330 	pr_debug_ratelimited("Restoring PASID 0x%x queues\n",
1331 			    pdd->process->pasid);
1332 
1333 	/* Update PD Base in QPD */
1334 	qpd->page_table_base = amdgpu_amdkfd_gpuvm_get_process_page_dir(pdd->drm_priv);
1335 	pr_debug("Updated PD address to 0x%llx\n", qpd->page_table_base);
1336 
1337 	/* activate all active queues on the qpd */
1338 	list_for_each_entry(q, &qpd->queues_list, list) {
1339 		q->properties.is_evicted = false;
1340 		if (!QUEUE_IS_ACTIVE(q->properties))
1341 			continue;
1342 
1343 		q->properties.is_active = true;
1344 		increment_queue_count(dqm, &pdd->qpd, q);
1345 
1346 		if (dqm->dev->kfd->shared_resources.enable_mes) {
1347 			retval = add_queue_mes(dqm, q, qpd);
1348 			if (retval) {
1349 				dev_err(dev, "Failed to restore queue %d\n",
1350 					q->properties.queue_id);
1351 				goto out;
1352 			}
1353 		}
1354 	}
1355 	if (!dqm->dev->kfd->shared_resources.enable_mes)
1356 		retval = execute_queues_cpsch(dqm,
1357 					      KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 0, USE_DEFAULT_GRACE_PERIOD);
1358 	eviction_duration = get_jiffies_64() - pdd->last_evict_timestamp;
1359 	atomic64_add(eviction_duration, &pdd->evict_duration_counter);
1360 vm_not_acquired:
1361 	qpd->evicted = 0;
1362 out:
1363 	dqm_unlock(dqm);
1364 	return retval;
1365 }
1366 
register_process(struct device_queue_manager * dqm,struct qcm_process_device * qpd)1367 static int register_process(struct device_queue_manager *dqm,
1368 					struct qcm_process_device *qpd)
1369 {
1370 	struct device_process_node *n;
1371 	struct kfd_process_device *pdd;
1372 	uint64_t pd_base;
1373 	int retval;
1374 
1375 	n = kzalloc(sizeof(*n), GFP_KERNEL);
1376 	if (!n)
1377 		return -ENOMEM;
1378 
1379 	n->qpd = qpd;
1380 
1381 	pdd = qpd_to_pdd(qpd);
1382 	/* Retrieve PD base */
1383 	pd_base = amdgpu_amdkfd_gpuvm_get_process_page_dir(pdd->drm_priv);
1384 
1385 	dqm_lock(dqm);
1386 	list_add(&n->list, &dqm->queues);
1387 
1388 	/* Update PD Base in QPD */
1389 	qpd->page_table_base = pd_base;
1390 	pr_debug("Updated PD address to 0x%llx\n", pd_base);
1391 
1392 	retval = dqm->asic_ops.update_qpd(dqm, qpd);
1393 
1394 	dqm->processes_count++;
1395 
1396 	dqm_unlock(dqm);
1397 
1398 	/* Outside the DQM lock because under the DQM lock we can't do
1399 	 * reclaim or take other locks that others hold while reclaiming.
1400 	 */
1401 	kfd_inc_compute_active(dqm->dev);
1402 
1403 	return retval;
1404 }
1405 
unregister_process(struct device_queue_manager * dqm,struct qcm_process_device * qpd)1406 static int unregister_process(struct device_queue_manager *dqm,
1407 					struct qcm_process_device *qpd)
1408 {
1409 	int retval;
1410 	struct device_process_node *cur, *next;
1411 
1412 	pr_debug("qpd->queues_list is %s\n",
1413 			list_empty(&qpd->queues_list) ? "empty" : "not empty");
1414 
1415 	retval = 0;
1416 	dqm_lock(dqm);
1417 
1418 	list_for_each_entry_safe(cur, next, &dqm->queues, list) {
1419 		if (qpd == cur->qpd) {
1420 			list_del(&cur->list);
1421 			kfree(cur);
1422 			dqm->processes_count--;
1423 			goto out;
1424 		}
1425 	}
1426 	/* qpd not found in dqm list */
1427 	retval = 1;
1428 out:
1429 	dqm_unlock(dqm);
1430 
1431 	/* Outside the DQM lock because under the DQM lock we can't do
1432 	 * reclaim or take other locks that others hold while reclaiming.
1433 	 */
1434 	if (!retval)
1435 		kfd_dec_compute_active(dqm->dev);
1436 
1437 	return retval;
1438 }
1439 
1440 static int
set_pasid_vmid_mapping(struct device_queue_manager * dqm,u32 pasid,unsigned int vmid)1441 set_pasid_vmid_mapping(struct device_queue_manager *dqm, u32 pasid,
1442 			unsigned int vmid)
1443 {
1444 	uint32_t xcc_mask = dqm->dev->xcc_mask;
1445 	int xcc_id, ret;
1446 
1447 	for_each_inst(xcc_id, xcc_mask) {
1448 		ret = dqm->dev->kfd2kgd->set_pasid_vmid_mapping(
1449 			dqm->dev->adev, pasid, vmid, xcc_id);
1450 		if (ret)
1451 			break;
1452 	}
1453 
1454 	return ret;
1455 }
1456 
init_interrupts(struct device_queue_manager * dqm)1457 static void init_interrupts(struct device_queue_manager *dqm)
1458 {
1459 	uint32_t xcc_mask = dqm->dev->xcc_mask;
1460 	unsigned int i, xcc_id;
1461 
1462 	for_each_inst(xcc_id, xcc_mask) {
1463 		for (i = 0 ; i < get_pipes_per_mec(dqm) ; i++) {
1464 			if (is_pipe_enabled(dqm, 0, i)) {
1465 				dqm->dev->kfd2kgd->init_interrupts(
1466 					dqm->dev->adev, i, xcc_id);
1467 			}
1468 		}
1469 	}
1470 }
1471 
initialize_nocpsch(struct device_queue_manager * dqm)1472 static int initialize_nocpsch(struct device_queue_manager *dqm)
1473 {
1474 	int pipe, queue;
1475 
1476 	pr_debug("num of pipes: %d\n", get_pipes_per_mec(dqm));
1477 
1478 	dqm->allocated_queues = kcalloc(get_pipes_per_mec(dqm),
1479 					sizeof(unsigned int), GFP_KERNEL);
1480 	if (!dqm->allocated_queues)
1481 		return -ENOMEM;
1482 
1483 	mutex_init(&dqm->lock_hidden);
1484 	INIT_LIST_HEAD(&dqm->queues);
1485 	dqm->active_queue_count = dqm->next_pipe_to_allocate = 0;
1486 	dqm->active_cp_queue_count = 0;
1487 	dqm->gws_queue_count = 0;
1488 
1489 	for (pipe = 0; pipe < get_pipes_per_mec(dqm); pipe++) {
1490 		int pipe_offset = pipe * get_queues_per_pipe(dqm);
1491 
1492 		for (queue = 0; queue < get_queues_per_pipe(dqm); queue++)
1493 			if (test_bit(pipe_offset + queue,
1494 				     dqm->dev->kfd->shared_resources.cp_queue_bitmap))
1495 				dqm->allocated_queues[pipe] |= 1 << queue;
1496 	}
1497 
1498 	memset(dqm->vmid_pasid, 0, sizeof(dqm->vmid_pasid));
1499 
1500 	init_sdma_bitmaps(dqm);
1501 
1502 	return 0;
1503 }
1504 
uninitialize(struct device_queue_manager * dqm)1505 static void uninitialize(struct device_queue_manager *dqm)
1506 {
1507 	int i;
1508 
1509 	WARN_ON(dqm->active_queue_count > 0 || dqm->processes_count > 0);
1510 
1511 	kfree(dqm->allocated_queues);
1512 	for (i = 0 ; i < KFD_MQD_TYPE_MAX ; i++)
1513 		kfree(dqm->mqd_mgrs[i]);
1514 	mutex_destroy(&dqm->lock_hidden);
1515 }
1516 
start_nocpsch(struct device_queue_manager * dqm)1517 static int start_nocpsch(struct device_queue_manager *dqm)
1518 {
1519 	int r = 0;
1520 
1521 	pr_info("SW scheduler is used");
1522 	init_interrupts(dqm);
1523 
1524 	if (dqm->dev->adev->asic_type == CHIP_HAWAII)
1525 		r = pm_init(&dqm->packet_mgr, dqm);
1526 	if (!r)
1527 		dqm->sched_running = true;
1528 
1529 	return r;
1530 }
1531 
stop_nocpsch(struct device_queue_manager * dqm)1532 static int stop_nocpsch(struct device_queue_manager *dqm)
1533 {
1534 	dqm_lock(dqm);
1535 	if (!dqm->sched_running) {
1536 		dqm_unlock(dqm);
1537 		return 0;
1538 	}
1539 
1540 	if (dqm->dev->adev->asic_type == CHIP_HAWAII)
1541 		pm_uninit(&dqm->packet_mgr);
1542 	dqm->sched_running = false;
1543 	dqm_unlock(dqm);
1544 
1545 	return 0;
1546 }
1547 
allocate_sdma_queue(struct device_queue_manager * dqm,struct queue * q,const uint32_t * restore_sdma_id)1548 static int allocate_sdma_queue(struct device_queue_manager *dqm,
1549 				struct queue *q, const uint32_t *restore_sdma_id)
1550 {
1551 	struct device *dev = dqm->dev->adev->dev;
1552 	int bit;
1553 
1554 	if (q->properties.type == KFD_QUEUE_TYPE_SDMA) {
1555 		if (bitmap_empty(dqm->sdma_bitmap, KFD_MAX_SDMA_QUEUES)) {
1556 			dev_err(dev, "No more SDMA queue to allocate\n");
1557 			return -ENOMEM;
1558 		}
1559 
1560 		if (restore_sdma_id) {
1561 			/* Re-use existing sdma_id */
1562 			if (!test_bit(*restore_sdma_id, dqm->sdma_bitmap)) {
1563 				dev_err(dev, "SDMA queue already in use\n");
1564 				return -EBUSY;
1565 			}
1566 			clear_bit(*restore_sdma_id, dqm->sdma_bitmap);
1567 			q->sdma_id = *restore_sdma_id;
1568 		} else {
1569 			/* Find first available sdma_id */
1570 			bit = find_first_bit(dqm->sdma_bitmap,
1571 					     get_num_sdma_queues(dqm));
1572 			clear_bit(bit, dqm->sdma_bitmap);
1573 			q->sdma_id = bit;
1574 		}
1575 
1576 		q->properties.sdma_engine_id =
1577 			q->sdma_id % kfd_get_num_sdma_engines(dqm->dev);
1578 		q->properties.sdma_queue_id = q->sdma_id /
1579 				kfd_get_num_sdma_engines(dqm->dev);
1580 	} else if (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) {
1581 		if (bitmap_empty(dqm->xgmi_sdma_bitmap, KFD_MAX_SDMA_QUEUES)) {
1582 			dev_err(dev, "No more XGMI SDMA queue to allocate\n");
1583 			return -ENOMEM;
1584 		}
1585 		if (restore_sdma_id) {
1586 			/* Re-use existing sdma_id */
1587 			if (!test_bit(*restore_sdma_id, dqm->xgmi_sdma_bitmap)) {
1588 				dev_err(dev, "SDMA queue already in use\n");
1589 				return -EBUSY;
1590 			}
1591 			clear_bit(*restore_sdma_id, dqm->xgmi_sdma_bitmap);
1592 			q->sdma_id = *restore_sdma_id;
1593 		} else {
1594 			bit = find_first_bit(dqm->xgmi_sdma_bitmap,
1595 					     get_num_xgmi_sdma_queues(dqm));
1596 			clear_bit(bit, dqm->xgmi_sdma_bitmap);
1597 			q->sdma_id = bit;
1598 		}
1599 		/* sdma_engine_id is sdma id including
1600 		 * both PCIe-optimized SDMAs and XGMI-
1601 		 * optimized SDMAs. The calculation below
1602 		 * assumes the first N engines are always
1603 		 * PCIe-optimized ones
1604 		 */
1605 		q->properties.sdma_engine_id =
1606 			kfd_get_num_sdma_engines(dqm->dev) +
1607 			q->sdma_id % kfd_get_num_xgmi_sdma_engines(dqm->dev);
1608 		q->properties.sdma_queue_id = q->sdma_id /
1609 			kfd_get_num_xgmi_sdma_engines(dqm->dev);
1610 	} else if (q->properties.type == KFD_QUEUE_TYPE_SDMA_BY_ENG_ID) {
1611 		int i, num_queues, num_engines, eng_offset = 0, start_engine;
1612 		bool free_bit_found = false, is_xgmi = false;
1613 
1614 		if (q->properties.sdma_engine_id < kfd_get_num_sdma_engines(dqm->dev)) {
1615 			num_queues = get_num_sdma_queues(dqm);
1616 			num_engines = kfd_get_num_sdma_engines(dqm->dev);
1617 			q->properties.type = KFD_QUEUE_TYPE_SDMA;
1618 		} else {
1619 			num_queues = get_num_xgmi_sdma_queues(dqm);
1620 			num_engines = kfd_get_num_xgmi_sdma_engines(dqm->dev);
1621 			eng_offset = kfd_get_num_sdma_engines(dqm->dev);
1622 			q->properties.type = KFD_QUEUE_TYPE_SDMA_XGMI;
1623 			is_xgmi = true;
1624 		}
1625 
1626 		/* Scan available bit based on target engine ID. */
1627 		start_engine = q->properties.sdma_engine_id - eng_offset;
1628 		for (i = start_engine; i < num_queues; i += num_engines) {
1629 
1630 			if (!test_bit(i, is_xgmi ? dqm->xgmi_sdma_bitmap : dqm->sdma_bitmap))
1631 				continue;
1632 
1633 			clear_bit(i, is_xgmi ? dqm->xgmi_sdma_bitmap : dqm->sdma_bitmap);
1634 			q->sdma_id = i;
1635 			q->properties.sdma_queue_id = q->sdma_id / num_engines;
1636 			free_bit_found = true;
1637 			break;
1638 		}
1639 
1640 		if (!free_bit_found) {
1641 			dev_err(dev, "No more SDMA queue to allocate for target ID %i\n",
1642 				q->properties.sdma_engine_id);
1643 			return -ENOMEM;
1644 		}
1645 	}
1646 
1647 	pr_debug("SDMA engine id: %d\n", q->properties.sdma_engine_id);
1648 	pr_debug("SDMA queue id: %d\n", q->properties.sdma_queue_id);
1649 
1650 	return 0;
1651 }
1652 
deallocate_sdma_queue(struct device_queue_manager * dqm,struct queue * q)1653 static void deallocate_sdma_queue(struct device_queue_manager *dqm,
1654 				struct queue *q)
1655 {
1656 	if (q->properties.type == KFD_QUEUE_TYPE_SDMA) {
1657 		if (q->sdma_id >= get_num_sdma_queues(dqm))
1658 			return;
1659 		set_bit(q->sdma_id, dqm->sdma_bitmap);
1660 	} else if (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) {
1661 		if (q->sdma_id >= get_num_xgmi_sdma_queues(dqm))
1662 			return;
1663 		set_bit(q->sdma_id, dqm->xgmi_sdma_bitmap);
1664 	}
1665 }
1666 
1667 /*
1668  * Device Queue Manager implementation for cp scheduler
1669  */
1670 
set_sched_resources(struct device_queue_manager * dqm)1671 static int set_sched_resources(struct device_queue_manager *dqm)
1672 {
1673 	int i, mec;
1674 	struct scheduling_resources res;
1675 	struct device *dev = dqm->dev->adev->dev;
1676 
1677 	res.vmid_mask = dqm->dev->compute_vmid_bitmap;
1678 
1679 	res.queue_mask = 0;
1680 	for (i = 0; i < AMDGPU_MAX_QUEUES; ++i) {
1681 		mec = (i / dqm->dev->kfd->shared_resources.num_queue_per_pipe)
1682 			/ dqm->dev->kfd->shared_resources.num_pipe_per_mec;
1683 
1684 		if (!test_bit(i, dqm->dev->kfd->shared_resources.cp_queue_bitmap))
1685 			continue;
1686 
1687 		/* only acquire queues from the first MEC */
1688 		if (mec > 0)
1689 			continue;
1690 
1691 		/* This situation may be hit in the future if a new HW
1692 		 * generation exposes more than 64 queues. If so, the
1693 		 * definition of res.queue_mask needs updating
1694 		 */
1695 		if (WARN_ON(i >= (sizeof(res.queue_mask)*8))) {
1696 			dev_err(dev, "Invalid queue enabled by amdgpu: %d\n", i);
1697 			break;
1698 		}
1699 
1700 		res.queue_mask |= 1ull
1701 			<< amdgpu_queue_mask_bit_to_set_resource_bit(
1702 				dqm->dev->adev, i);
1703 	}
1704 	res.gws_mask = ~0ull;
1705 	res.oac_mask = res.gds_heap_base = res.gds_heap_size = 0;
1706 
1707 	pr_debug("Scheduling resources:\n"
1708 			"vmid mask: 0x%8X\n"
1709 			"queue mask: 0x%8llX\n",
1710 			res.vmid_mask, res.queue_mask);
1711 
1712 	return pm_send_set_resources(&dqm->packet_mgr, &res);
1713 }
1714 
initialize_cpsch(struct device_queue_manager * dqm)1715 static int initialize_cpsch(struct device_queue_manager *dqm)
1716 {
1717 	pr_debug("num of pipes: %d\n", get_pipes_per_mec(dqm));
1718 
1719 	mutex_init(&dqm->lock_hidden);
1720 	INIT_LIST_HEAD(&dqm->queues);
1721 	dqm->active_queue_count = dqm->processes_count = 0;
1722 	dqm->active_cp_queue_count = 0;
1723 	dqm->gws_queue_count = 0;
1724 	dqm->active_runlist = false;
1725 	INIT_WORK(&dqm->hw_exception_work, kfd_process_hw_exception);
1726 	dqm->trap_debug_vmid = 0;
1727 
1728 	init_sdma_bitmaps(dqm);
1729 
1730 	if (dqm->dev->kfd2kgd->get_iq_wait_times)
1731 		dqm->dev->kfd2kgd->get_iq_wait_times(dqm->dev->adev,
1732 					&dqm->wait_times,
1733 					ffs(dqm->dev->xcc_mask) - 1);
1734 	return 0;
1735 }
1736 
1737 /* halt_cpsch:
1738  * Unmap queues so the schedule doesn't continue remaining jobs in the queue.
1739  * Then set dqm->sched_halt so queues don't map to runlist until unhalt_cpsch
1740  * is called.
1741  */
halt_cpsch(struct device_queue_manager * dqm)1742 static int halt_cpsch(struct device_queue_manager *dqm)
1743 {
1744 	int ret = 0;
1745 
1746 	dqm_lock(dqm);
1747 	if (!dqm->sched_running) {
1748 		dqm_unlock(dqm);
1749 		return 0;
1750 	}
1751 
1752 	WARN_ONCE(dqm->sched_halt, "Scheduling is already on halt\n");
1753 
1754 	if (!dqm->is_hws_hang) {
1755 		if (!dqm->dev->kfd->shared_resources.enable_mes)
1756 			ret = unmap_queues_cpsch(dqm,
1757 						 KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES, 0,
1758 				USE_DEFAULT_GRACE_PERIOD, false);
1759 		else
1760 			ret = remove_all_queues_mes(dqm);
1761 	}
1762 	dqm->sched_halt = true;
1763 	dqm_unlock(dqm);
1764 
1765 	return ret;
1766 }
1767 
1768 /* unhalt_cpsch
1769  * Unset dqm->sched_halt and map queues back to runlist
1770  */
unhalt_cpsch(struct device_queue_manager * dqm)1771 static int unhalt_cpsch(struct device_queue_manager *dqm)
1772 {
1773 	int ret = 0;
1774 
1775 	dqm_lock(dqm);
1776 	if (!dqm->sched_running || !dqm->sched_halt) {
1777 		WARN_ONCE(!dqm->sched_halt, "Scheduling is not on halt.\n");
1778 		dqm_unlock(dqm);
1779 		return 0;
1780 	}
1781 	dqm->sched_halt = false;
1782 	if (!dqm->dev->kfd->shared_resources.enable_mes)
1783 		ret = execute_queues_cpsch(dqm,
1784 					   KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES,
1785 			0, USE_DEFAULT_GRACE_PERIOD);
1786 	dqm_unlock(dqm);
1787 
1788 	return ret;
1789 }
1790 
start_cpsch(struct device_queue_manager * dqm)1791 static int start_cpsch(struct device_queue_manager *dqm)
1792 {
1793 	struct device *dev = dqm->dev->adev->dev;
1794 	int retval, num_hw_queue_slots;
1795 
1796 	retval = 0;
1797 
1798 	dqm_lock(dqm);
1799 
1800 	if (!dqm->dev->kfd->shared_resources.enable_mes) {
1801 		retval = pm_init(&dqm->packet_mgr, dqm);
1802 		if (retval)
1803 			goto fail_packet_manager_init;
1804 
1805 		retval = set_sched_resources(dqm);
1806 		if (retval)
1807 			goto fail_set_sched_resources;
1808 	}
1809 	pr_debug("Allocating fence memory\n");
1810 
1811 	/* allocate fence memory on the gart */
1812 	retval = kfd_gtt_sa_allocate(dqm->dev, sizeof(*dqm->fence_addr),
1813 					&dqm->fence_mem);
1814 
1815 	if (retval)
1816 		goto fail_allocate_vidmem;
1817 
1818 	dqm->fence_addr = (uint64_t *)dqm->fence_mem->cpu_ptr;
1819 	dqm->fence_gpu_addr = dqm->fence_mem->gpu_addr;
1820 
1821 	init_interrupts(dqm);
1822 
1823 	/* clear hang status when driver try to start the hw scheduler */
1824 	dqm->sched_running = true;
1825 
1826 	if (!dqm->dev->kfd->shared_resources.enable_mes)
1827 		execute_queues_cpsch(dqm, KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 0, USE_DEFAULT_GRACE_PERIOD);
1828 
1829 	/* Set CWSR grace period to 1x1000 cycle for GFX9.4.3 APU */
1830 	if (amdgpu_emu_mode == 0 && dqm->dev->adev->gmc.is_app_apu &&
1831 	    (KFD_GC_VERSION(dqm->dev) == IP_VERSION(9, 4, 3))) {
1832 		uint32_t reg_offset = 0;
1833 		uint32_t grace_period = 1;
1834 
1835 		retval = pm_update_grace_period(&dqm->packet_mgr,
1836 						grace_period);
1837 		if (retval)
1838 			dev_err(dev, "Setting grace timeout failed\n");
1839 		else if (dqm->dev->kfd2kgd->build_grace_period_packet_info)
1840 			/* Update dqm->wait_times maintained in software */
1841 			dqm->dev->kfd2kgd->build_grace_period_packet_info(
1842 					dqm->dev->adev,	dqm->wait_times,
1843 					grace_period, &reg_offset,
1844 					&dqm->wait_times);
1845 	}
1846 
1847 	/* setup per-queue reset detection buffer  */
1848 	num_hw_queue_slots =  dqm->dev->kfd->shared_resources.num_queue_per_pipe *
1849 			      dqm->dev->kfd->shared_resources.num_pipe_per_mec *
1850 			      NUM_XCC(dqm->dev->xcc_mask);
1851 
1852 	dqm->detect_hang_info_size = num_hw_queue_slots * sizeof(struct dqm_detect_hang_info);
1853 	dqm->detect_hang_info = kzalloc(dqm->detect_hang_info_size, GFP_KERNEL);
1854 
1855 	if (!dqm->detect_hang_info) {
1856 		retval = -ENOMEM;
1857 		goto fail_detect_hang_buffer;
1858 	}
1859 
1860 	dqm_unlock(dqm);
1861 
1862 	return 0;
1863 fail_detect_hang_buffer:
1864 	kfd_gtt_sa_free(dqm->dev, dqm->fence_mem);
1865 fail_allocate_vidmem:
1866 fail_set_sched_resources:
1867 	if (!dqm->dev->kfd->shared_resources.enable_mes)
1868 		pm_uninit(&dqm->packet_mgr);
1869 fail_packet_manager_init:
1870 	dqm_unlock(dqm);
1871 	return retval;
1872 }
1873 
stop_cpsch(struct device_queue_manager * dqm)1874 static int stop_cpsch(struct device_queue_manager *dqm)
1875 {
1876 	dqm_lock(dqm);
1877 	if (!dqm->sched_running) {
1878 		dqm_unlock(dqm);
1879 		return 0;
1880 	}
1881 
1882 	if (!dqm->dev->kfd->shared_resources.enable_mes)
1883 		unmap_queues_cpsch(dqm, KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES, 0, USE_DEFAULT_GRACE_PERIOD, false);
1884 	else
1885 		remove_all_queues_mes(dqm);
1886 
1887 	dqm->sched_running = false;
1888 
1889 	if (!dqm->dev->kfd->shared_resources.enable_mes)
1890 		pm_release_ib(&dqm->packet_mgr);
1891 
1892 	kfd_gtt_sa_free(dqm->dev, dqm->fence_mem);
1893 	if (!dqm->dev->kfd->shared_resources.enable_mes)
1894 		pm_uninit(&dqm->packet_mgr);
1895 	kfree(dqm->detect_hang_info);
1896 	dqm->detect_hang_info = NULL;
1897 	dqm_unlock(dqm);
1898 
1899 	return 0;
1900 }
1901 
create_kernel_queue_cpsch(struct device_queue_manager * dqm,struct kernel_queue * kq,struct qcm_process_device * qpd)1902 static int create_kernel_queue_cpsch(struct device_queue_manager *dqm,
1903 					struct kernel_queue *kq,
1904 					struct qcm_process_device *qpd)
1905 {
1906 	dqm_lock(dqm);
1907 	if (dqm->total_queue_count >= max_num_of_queues_per_device) {
1908 		pr_warn("Can't create new kernel queue because %d queues were already created\n",
1909 				dqm->total_queue_count);
1910 		dqm_unlock(dqm);
1911 		return -EPERM;
1912 	}
1913 
1914 	/*
1915 	 * Unconditionally increment this counter, regardless of the queue's
1916 	 * type or whether the queue is active.
1917 	 */
1918 	dqm->total_queue_count++;
1919 	pr_debug("Total of %d queues are accountable so far\n",
1920 			dqm->total_queue_count);
1921 
1922 	list_add(&kq->list, &qpd->priv_queue_list);
1923 	increment_queue_count(dqm, qpd, kq->queue);
1924 	qpd->is_debug = true;
1925 	execute_queues_cpsch(dqm, KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 0,
1926 			USE_DEFAULT_GRACE_PERIOD);
1927 	dqm_unlock(dqm);
1928 
1929 	return 0;
1930 }
1931 
destroy_kernel_queue_cpsch(struct device_queue_manager * dqm,struct kernel_queue * kq,struct qcm_process_device * qpd)1932 static void destroy_kernel_queue_cpsch(struct device_queue_manager *dqm,
1933 					struct kernel_queue *kq,
1934 					struct qcm_process_device *qpd)
1935 {
1936 	dqm_lock(dqm);
1937 	list_del(&kq->list);
1938 	decrement_queue_count(dqm, qpd, kq->queue);
1939 	qpd->is_debug = false;
1940 	execute_queues_cpsch(dqm, KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES, 0,
1941 			USE_DEFAULT_GRACE_PERIOD);
1942 	/*
1943 	 * Unconditionally decrement this counter, regardless of the queue's
1944 	 * type.
1945 	 */
1946 	dqm->total_queue_count--;
1947 	pr_debug("Total of %d queues are accountable so far\n",
1948 			dqm->total_queue_count);
1949 	dqm_unlock(dqm);
1950 }
1951 
create_queue_cpsch(struct device_queue_manager * dqm,struct queue * q,struct qcm_process_device * qpd,const struct kfd_criu_queue_priv_data * qd,const void * restore_mqd,const void * restore_ctl_stack)1952 static int create_queue_cpsch(struct device_queue_manager *dqm, struct queue *q,
1953 			struct qcm_process_device *qpd,
1954 			const struct kfd_criu_queue_priv_data *qd,
1955 			const void *restore_mqd, const void *restore_ctl_stack)
1956 {
1957 	int retval;
1958 	struct mqd_manager *mqd_mgr;
1959 
1960 	if (dqm->total_queue_count >= max_num_of_queues_per_device) {
1961 		pr_warn("Can't create new usermode queue because %d queues were already created\n",
1962 				dqm->total_queue_count);
1963 		retval = -EPERM;
1964 		goto out;
1965 	}
1966 
1967 	if (q->properties.type == KFD_QUEUE_TYPE_SDMA ||
1968 		q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI ||
1969 		q->properties.type == KFD_QUEUE_TYPE_SDMA_BY_ENG_ID) {
1970 		dqm_lock(dqm);
1971 		retval = allocate_sdma_queue(dqm, q, qd ? &qd->sdma_id : NULL);
1972 		dqm_unlock(dqm);
1973 		if (retval)
1974 			goto out;
1975 	}
1976 
1977 	retval = allocate_doorbell(qpd, q, qd ? &qd->doorbell_id : NULL);
1978 	if (retval)
1979 		goto out_deallocate_sdma_queue;
1980 
1981 	mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type(
1982 			q->properties.type)];
1983 
1984 	if (q->properties.type == KFD_QUEUE_TYPE_SDMA ||
1985 		q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)
1986 		dqm->asic_ops.init_sdma_vm(dqm, q, qpd);
1987 	q->properties.tba_addr = qpd->tba_addr;
1988 	q->properties.tma_addr = qpd->tma_addr;
1989 	q->mqd_mem_obj = mqd_mgr->allocate_mqd(mqd_mgr->dev, &q->properties);
1990 	if (!q->mqd_mem_obj) {
1991 		retval = -ENOMEM;
1992 		goto out_deallocate_doorbell;
1993 	}
1994 
1995 	dqm_lock(dqm);
1996 	/*
1997 	 * Eviction state logic: mark all queues as evicted, even ones
1998 	 * not currently active. Restoring inactive queues later only
1999 	 * updates the is_evicted flag but is a no-op otherwise.
2000 	 */
2001 	q->properties.is_evicted = !!qpd->evicted;
2002 	q->properties.is_dbg_wa = qpd->pqm->process->debug_trap_enabled &&
2003 				  kfd_dbg_has_cwsr_workaround(q->device);
2004 
2005 	if (qd)
2006 		mqd_mgr->restore_mqd(mqd_mgr, &q->mqd, q->mqd_mem_obj, &q->gart_mqd_addr,
2007 				     &q->properties, restore_mqd, restore_ctl_stack,
2008 				     qd->ctl_stack_size);
2009 	else
2010 		mqd_mgr->init_mqd(mqd_mgr, &q->mqd, q->mqd_mem_obj,
2011 					&q->gart_mqd_addr, &q->properties);
2012 
2013 	list_add(&q->list, &qpd->queues_list);
2014 	qpd->queue_count++;
2015 
2016 	if (q->properties.is_active) {
2017 		increment_queue_count(dqm, qpd, q);
2018 
2019 		if (!dqm->dev->kfd->shared_resources.enable_mes)
2020 			retval = execute_queues_cpsch(dqm,
2021 					KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 0, USE_DEFAULT_GRACE_PERIOD);
2022 		else
2023 			retval = add_queue_mes(dqm, q, qpd);
2024 		if (retval)
2025 			goto cleanup_queue;
2026 	}
2027 
2028 	/*
2029 	 * Unconditionally increment this counter, regardless of the queue's
2030 	 * type or whether the queue is active.
2031 	 */
2032 	dqm->total_queue_count++;
2033 
2034 	pr_debug("Total of %d queues are accountable so far\n",
2035 			dqm->total_queue_count);
2036 
2037 	dqm_unlock(dqm);
2038 	return retval;
2039 
2040 cleanup_queue:
2041 	qpd->queue_count--;
2042 	list_del(&q->list);
2043 	if (q->properties.is_active)
2044 		decrement_queue_count(dqm, qpd, q);
2045 	mqd_mgr->free_mqd(mqd_mgr, q->mqd, q->mqd_mem_obj);
2046 	dqm_unlock(dqm);
2047 out_deallocate_doorbell:
2048 	deallocate_doorbell(qpd, q);
2049 out_deallocate_sdma_queue:
2050 	if (q->properties.type == KFD_QUEUE_TYPE_SDMA ||
2051 		q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) {
2052 		dqm_lock(dqm);
2053 		deallocate_sdma_queue(dqm, q);
2054 		dqm_unlock(dqm);
2055 	}
2056 out:
2057 	return retval;
2058 }
2059 
amdkfd_fence_wait_timeout(struct device_queue_manager * dqm,uint64_t fence_value,unsigned int timeout_ms)2060 int amdkfd_fence_wait_timeout(struct device_queue_manager *dqm,
2061 			      uint64_t fence_value,
2062 			      unsigned int timeout_ms)
2063 {
2064 	unsigned long end_jiffies = msecs_to_jiffies(timeout_ms) + jiffies;
2065 	struct device *dev = dqm->dev->adev->dev;
2066 	uint64_t *fence_addr =  dqm->fence_addr;
2067 
2068 	while (*fence_addr != fence_value) {
2069 		/* Fatal err detected, this response won't come */
2070 		if (amdgpu_amdkfd_is_fed(dqm->dev->adev))
2071 			return -EIO;
2072 
2073 		if (time_after(jiffies, end_jiffies)) {
2074 			dev_err(dev, "qcm fence wait loop timeout expired\n");
2075 			/* In HWS case, this is used to halt the driver thread
2076 			 * in order not to mess up CP states before doing
2077 			 * scandumps for FW debugging.
2078 			 */
2079 			while (halt_if_hws_hang)
2080 				schedule();
2081 
2082 			return -ETIME;
2083 		}
2084 		schedule();
2085 	}
2086 
2087 	return 0;
2088 }
2089 
2090 /* dqm->lock mutex has to be locked before calling this function */
map_queues_cpsch(struct device_queue_manager * dqm)2091 static int map_queues_cpsch(struct device_queue_manager *dqm)
2092 {
2093 	struct device *dev = dqm->dev->adev->dev;
2094 	int retval;
2095 
2096 	if (!dqm->sched_running || dqm->sched_halt)
2097 		return 0;
2098 	if (dqm->active_queue_count <= 0 || dqm->processes_count <= 0)
2099 		return 0;
2100 	if (dqm->active_runlist)
2101 		return 0;
2102 
2103 	retval = pm_send_runlist(&dqm->packet_mgr, &dqm->queues);
2104 	pr_debug("%s sent runlist\n", __func__);
2105 	if (retval) {
2106 		dev_err(dev, "failed to execute runlist\n");
2107 		return retval;
2108 	}
2109 	dqm->active_runlist = true;
2110 
2111 	return retval;
2112 }
2113 
set_queue_as_reset(struct device_queue_manager * dqm,struct queue * q,struct qcm_process_device * qpd)2114 static void set_queue_as_reset(struct device_queue_manager *dqm, struct queue *q,
2115 			       struct qcm_process_device *qpd)
2116 {
2117 	struct kfd_process_device *pdd = qpd_to_pdd(qpd);
2118 
2119 	dev_err(dqm->dev->adev->dev, "queue id 0x%0x at pasid 0x%0x is reset\n",
2120 		q->properties.queue_id, q->process->pasid);
2121 
2122 	pdd->has_reset_queue = true;
2123 	if (q->properties.is_active) {
2124 		q->properties.is_active = false;
2125 		decrement_queue_count(dqm, qpd, q);
2126 	}
2127 }
2128 
detect_queue_hang(struct device_queue_manager * dqm)2129 static int detect_queue_hang(struct device_queue_manager *dqm)
2130 {
2131 	int i;
2132 
2133 	/* detect should be used only in dqm locked queue reset */
2134 	if (WARN_ON(dqm->detect_hang_count > 0))
2135 		return 0;
2136 
2137 	memset(dqm->detect_hang_info, 0, dqm->detect_hang_info_size);
2138 
2139 	for (i = 0; i < AMDGPU_MAX_QUEUES; ++i) {
2140 		uint32_t mec, pipe, queue;
2141 		int xcc_id;
2142 
2143 		mec = (i / dqm->dev->kfd->shared_resources.num_queue_per_pipe)
2144 			/ dqm->dev->kfd->shared_resources.num_pipe_per_mec;
2145 
2146 		if (mec || !test_bit(i, dqm->dev->kfd->shared_resources.cp_queue_bitmap))
2147 			continue;
2148 
2149 		amdgpu_queue_mask_bit_to_mec_queue(dqm->dev->adev, i, &mec, &pipe, &queue);
2150 
2151 		for_each_inst(xcc_id, dqm->dev->xcc_mask) {
2152 			uint64_t queue_addr = dqm->dev->kfd2kgd->hqd_get_pq_addr(
2153 						dqm->dev->adev, pipe, queue, xcc_id);
2154 			struct dqm_detect_hang_info hang_info;
2155 
2156 			if (!queue_addr)
2157 				continue;
2158 
2159 			hang_info.pipe_id = pipe;
2160 			hang_info.queue_id = queue;
2161 			hang_info.xcc_id = xcc_id;
2162 			hang_info.queue_address = queue_addr;
2163 
2164 			dqm->detect_hang_info[dqm->detect_hang_count] = hang_info;
2165 			dqm->detect_hang_count++;
2166 		}
2167 	}
2168 
2169 	return dqm->detect_hang_count;
2170 }
2171 
find_queue_by_address(struct device_queue_manager * dqm,uint64_t queue_address)2172 static struct queue *find_queue_by_address(struct device_queue_manager *dqm, uint64_t queue_address)
2173 {
2174 	struct device_process_node *cur;
2175 	struct qcm_process_device *qpd;
2176 	struct queue *q;
2177 
2178 	list_for_each_entry(cur, &dqm->queues, list) {
2179 		qpd = cur->qpd;
2180 		list_for_each_entry(q, &qpd->queues_list, list) {
2181 			if (queue_address == q->properties.queue_address)
2182 				return q;
2183 		}
2184 	}
2185 
2186 	return NULL;
2187 }
2188 
2189 /* only for compute queue */
reset_queues_on_hws_hang(struct device_queue_manager * dqm)2190 static int reset_queues_on_hws_hang(struct device_queue_manager *dqm)
2191 {
2192 	int r = 0, reset_count = 0, i;
2193 
2194 	if (!dqm->detect_hang_info || dqm->is_hws_hang)
2195 		return -EIO;
2196 
2197 	/* assume dqm locked. */
2198 	if (!detect_queue_hang(dqm))
2199 		return -ENOTRECOVERABLE;
2200 
2201 	for (i = 0; i < dqm->detect_hang_count; i++) {
2202 		struct dqm_detect_hang_info hang_info = dqm->detect_hang_info[i];
2203 		struct queue *q = find_queue_by_address(dqm, hang_info.queue_address);
2204 		struct kfd_process_device *pdd;
2205 		uint64_t queue_addr = 0;
2206 
2207 		if (!q) {
2208 			r = -ENOTRECOVERABLE;
2209 			goto reset_fail;
2210 		}
2211 
2212 		pdd = kfd_get_process_device_data(dqm->dev, q->process);
2213 		if (!pdd) {
2214 			r = -ENOTRECOVERABLE;
2215 			goto reset_fail;
2216 		}
2217 
2218 		queue_addr = dqm->dev->kfd2kgd->hqd_reset(dqm->dev->adev,
2219 				hang_info.pipe_id, hang_info.queue_id, hang_info.xcc_id,
2220 				KFD_UNMAP_LATENCY_MS);
2221 
2222 		/* either reset failed or we reset an unexpected queue. */
2223 		if (queue_addr != q->properties.queue_address) {
2224 			r = -ENOTRECOVERABLE;
2225 			goto reset_fail;
2226 		}
2227 
2228 		set_queue_as_reset(dqm, q, &pdd->qpd);
2229 		reset_count++;
2230 	}
2231 
2232 	if (reset_count == dqm->detect_hang_count)
2233 		kfd_signal_reset_event(dqm->dev);
2234 	else
2235 		r = -ENOTRECOVERABLE;
2236 
2237 reset_fail:
2238 	dqm->detect_hang_count = 0;
2239 
2240 	return r;
2241 }
2242 
2243 /* dqm->lock mutex has to be locked before calling this function */
unmap_queues_cpsch(struct device_queue_manager * dqm,enum kfd_unmap_queues_filter filter,uint32_t filter_param,uint32_t grace_period,bool reset)2244 static int unmap_queues_cpsch(struct device_queue_manager *dqm,
2245 				enum kfd_unmap_queues_filter filter,
2246 				uint32_t filter_param,
2247 				uint32_t grace_period,
2248 				bool reset)
2249 {
2250 	struct device *dev = dqm->dev->adev->dev;
2251 	struct mqd_manager *mqd_mgr;
2252 	int retval;
2253 
2254 	if (!dqm->sched_running)
2255 		return 0;
2256 	if (!dqm->active_runlist)
2257 		return 0;
2258 	if (!down_read_trylock(&dqm->dev->adev->reset_domain->sem))
2259 		return -EIO;
2260 
2261 	if (grace_period != USE_DEFAULT_GRACE_PERIOD) {
2262 		retval = pm_update_grace_period(&dqm->packet_mgr, grace_period);
2263 		if (retval)
2264 			goto out;
2265 	}
2266 
2267 	retval = pm_send_unmap_queue(&dqm->packet_mgr, filter, filter_param, reset);
2268 	if (retval)
2269 		goto out;
2270 
2271 	*dqm->fence_addr = KFD_FENCE_INIT;
2272 	pm_send_query_status(&dqm->packet_mgr, dqm->fence_gpu_addr,
2273 				KFD_FENCE_COMPLETED);
2274 	/* should be timed out */
2275 	retval = amdkfd_fence_wait_timeout(dqm, KFD_FENCE_COMPLETED,
2276 					   queue_preemption_timeout_ms);
2277 	if (retval) {
2278 		dev_err(dev, "The cp might be in an unrecoverable state due to an unsuccessful queues preemption\n");
2279 		kfd_hws_hang(dqm);
2280 		goto out;
2281 	}
2282 
2283 	/* In the current MEC firmware implementation, if compute queue
2284 	 * doesn't response to the preemption request in time, HIQ will
2285 	 * abandon the unmap request without returning any timeout error
2286 	 * to driver. Instead, MEC firmware will log the doorbell of the
2287 	 * unresponding compute queue to HIQ.MQD.queue_doorbell_id fields.
2288 	 * To make sure the queue unmap was successful, driver need to
2289 	 * check those fields
2290 	 */
2291 	mqd_mgr = dqm->mqd_mgrs[KFD_MQD_TYPE_HIQ];
2292 	if (mqd_mgr->check_preemption_failed(mqd_mgr, dqm->packet_mgr.priv_queue->queue->mqd)) {
2293 		if (reset_queues_on_hws_hang(dqm)) {
2294 			while (halt_if_hws_hang)
2295 				schedule();
2296 			dqm->is_hws_hang = true;
2297 			kfd_hws_hang(dqm);
2298 			retval = -ETIME;
2299 			goto out;
2300 		}
2301 	}
2302 
2303 	/* We need to reset the grace period value for this device */
2304 	if (grace_period != USE_DEFAULT_GRACE_PERIOD) {
2305 		if (pm_update_grace_period(&dqm->packet_mgr,
2306 					USE_DEFAULT_GRACE_PERIOD))
2307 			dev_err(dev, "Failed to reset grace period\n");
2308 	}
2309 
2310 	pm_release_ib(&dqm->packet_mgr);
2311 	dqm->active_runlist = false;
2312 
2313 out:
2314 	up_read(&dqm->dev->adev->reset_domain->sem);
2315 	return retval;
2316 }
2317 
2318 /* only for compute queue */
reset_queues_cpsch(struct device_queue_manager * dqm,uint16_t pasid)2319 static int reset_queues_cpsch(struct device_queue_manager *dqm, uint16_t pasid)
2320 {
2321 	int retval;
2322 
2323 	dqm_lock(dqm);
2324 
2325 	retval = unmap_queues_cpsch(dqm, KFD_UNMAP_QUEUES_FILTER_BY_PASID,
2326 			pasid, USE_DEFAULT_GRACE_PERIOD, true);
2327 
2328 	dqm_unlock(dqm);
2329 	return retval;
2330 }
2331 
2332 /* dqm->lock mutex has to be locked before calling this function */
execute_queues_cpsch(struct device_queue_manager * dqm,enum kfd_unmap_queues_filter filter,uint32_t filter_param,uint32_t grace_period)2333 static int execute_queues_cpsch(struct device_queue_manager *dqm,
2334 				enum kfd_unmap_queues_filter filter,
2335 				uint32_t filter_param,
2336 				uint32_t grace_period)
2337 {
2338 	int retval;
2339 
2340 	if (!down_read_trylock(&dqm->dev->adev->reset_domain->sem))
2341 		return -EIO;
2342 	retval = unmap_queues_cpsch(dqm, filter, filter_param, grace_period, false);
2343 	if (!retval)
2344 		retval = map_queues_cpsch(dqm);
2345 	up_read(&dqm->dev->adev->reset_domain->sem);
2346 	return retval;
2347 }
2348 
wait_on_destroy_queue(struct device_queue_manager * dqm,struct queue * q)2349 static int wait_on_destroy_queue(struct device_queue_manager *dqm,
2350 				 struct queue *q)
2351 {
2352 	struct kfd_process_device *pdd = kfd_get_process_device_data(q->device,
2353 								q->process);
2354 	int ret = 0;
2355 
2356 	if (pdd->qpd.is_debug)
2357 		return ret;
2358 
2359 	q->properties.is_being_destroyed = true;
2360 
2361 	if (pdd->process->debug_trap_enabled && q->properties.is_suspended) {
2362 		dqm_unlock(dqm);
2363 		mutex_unlock(&q->process->mutex);
2364 		ret = wait_event_interruptible(dqm->destroy_wait,
2365 						!q->properties.is_suspended);
2366 
2367 		mutex_lock(&q->process->mutex);
2368 		dqm_lock(dqm);
2369 	}
2370 
2371 	return ret;
2372 }
2373 
destroy_queue_cpsch(struct device_queue_manager * dqm,struct qcm_process_device * qpd,struct queue * q)2374 static int destroy_queue_cpsch(struct device_queue_manager *dqm,
2375 				struct qcm_process_device *qpd,
2376 				struct queue *q)
2377 {
2378 	int retval;
2379 	struct mqd_manager *mqd_mgr;
2380 	uint64_t sdma_val = 0;
2381 	struct kfd_process_device *pdd = qpd_to_pdd(qpd);
2382 	struct device *dev = dqm->dev->adev->dev;
2383 
2384 	/* Get the SDMA queue stats */
2385 	if ((q->properties.type == KFD_QUEUE_TYPE_SDMA) ||
2386 	    (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)) {
2387 		retval = read_sdma_queue_counter((uint64_t __user *)q->properties.read_ptr,
2388 							&sdma_val);
2389 		if (retval)
2390 			dev_err(dev, "Failed to read SDMA queue counter for queue: %d\n",
2391 				q->properties.queue_id);
2392 	}
2393 
2394 	/* remove queue from list to prevent rescheduling after preemption */
2395 	dqm_lock(dqm);
2396 
2397 	retval = wait_on_destroy_queue(dqm, q);
2398 
2399 	if (retval) {
2400 		dqm_unlock(dqm);
2401 		return retval;
2402 	}
2403 
2404 	if (qpd->is_debug) {
2405 		/*
2406 		 * error, currently we do not allow to destroy a queue
2407 		 * of a currently debugged process
2408 		 */
2409 		retval = -EBUSY;
2410 		goto failed_try_destroy_debugged_queue;
2411 
2412 	}
2413 
2414 	mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type(
2415 			q->properties.type)];
2416 
2417 	deallocate_doorbell(qpd, q);
2418 
2419 	if ((q->properties.type == KFD_QUEUE_TYPE_SDMA) ||
2420 	    (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)) {
2421 		deallocate_sdma_queue(dqm, q);
2422 		pdd->sdma_past_activity_counter += sdma_val;
2423 	}
2424 
2425 	if (q->properties.is_active) {
2426 		decrement_queue_count(dqm, qpd, q);
2427 		q->properties.is_active = false;
2428 		if (!dqm->dev->kfd->shared_resources.enable_mes) {
2429 			retval = execute_queues_cpsch(dqm,
2430 						      KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 0,
2431 						      USE_DEFAULT_GRACE_PERIOD);
2432 			if (retval == -ETIME)
2433 				qpd->reset_wavefronts = true;
2434 		} else {
2435 			retval = remove_queue_mes(dqm, q, qpd);
2436 		}
2437 	}
2438 	list_del(&q->list);
2439 	qpd->queue_count--;
2440 
2441 	/*
2442 	 * Unconditionally decrement this counter, regardless of the queue's
2443 	 * type
2444 	 */
2445 	dqm->total_queue_count--;
2446 	pr_debug("Total of %d queues are accountable so far\n",
2447 			dqm->total_queue_count);
2448 
2449 	dqm_unlock(dqm);
2450 
2451 	/*
2452 	 * Do free_mqd and raise delete event after dqm_unlock(dqm) to avoid
2453 	 * circular locking
2454 	 */
2455 	kfd_dbg_ev_raise(KFD_EC_MASK(EC_DEVICE_QUEUE_DELETE),
2456 				qpd->pqm->process, q->device,
2457 				-1, false, NULL, 0);
2458 
2459 	mqd_mgr->free_mqd(mqd_mgr, q->mqd, q->mqd_mem_obj);
2460 
2461 	return retval;
2462 
2463 failed_try_destroy_debugged_queue:
2464 
2465 	dqm_unlock(dqm);
2466 	return retval;
2467 }
2468 
2469 /*
2470  * Low bits must be 0000/FFFF as required by HW, high bits must be 0 to
2471  * stay in user mode.
2472  */
2473 #define APE1_FIXED_BITS_MASK 0xFFFF80000000FFFFULL
2474 /* APE1 limit is inclusive and 64K aligned. */
2475 #define APE1_LIMIT_ALIGNMENT 0xFFFF
2476 
set_cache_memory_policy(struct device_queue_manager * dqm,struct qcm_process_device * qpd,enum cache_policy default_policy,enum cache_policy alternate_policy,void __user * alternate_aperture_base,uint64_t alternate_aperture_size)2477 static bool set_cache_memory_policy(struct device_queue_manager *dqm,
2478 				   struct qcm_process_device *qpd,
2479 				   enum cache_policy default_policy,
2480 				   enum cache_policy alternate_policy,
2481 				   void __user *alternate_aperture_base,
2482 				   uint64_t alternate_aperture_size)
2483 {
2484 	bool retval = true;
2485 
2486 	if (!dqm->asic_ops.set_cache_memory_policy)
2487 		return retval;
2488 
2489 	dqm_lock(dqm);
2490 
2491 	if (alternate_aperture_size == 0) {
2492 		/* base > limit disables APE1 */
2493 		qpd->sh_mem_ape1_base = 1;
2494 		qpd->sh_mem_ape1_limit = 0;
2495 	} else {
2496 		/*
2497 		 * In FSA64, APE1_Base[63:0] = { 16{SH_MEM_APE1_BASE[31]},
2498 		 *			SH_MEM_APE1_BASE[31:0], 0x0000 }
2499 		 * APE1_Limit[63:0] = { 16{SH_MEM_APE1_LIMIT[31]},
2500 		 *			SH_MEM_APE1_LIMIT[31:0], 0xFFFF }
2501 		 * Verify that the base and size parameters can be
2502 		 * represented in this format and convert them.
2503 		 * Additionally restrict APE1 to user-mode addresses.
2504 		 */
2505 
2506 		uint64_t base = (uintptr_t)alternate_aperture_base;
2507 		uint64_t limit = base + alternate_aperture_size - 1;
2508 
2509 		if (limit <= base || (base & APE1_FIXED_BITS_MASK) != 0 ||
2510 		   (limit & APE1_FIXED_BITS_MASK) != APE1_LIMIT_ALIGNMENT) {
2511 			retval = false;
2512 			goto out;
2513 		}
2514 
2515 		qpd->sh_mem_ape1_base = base >> 16;
2516 		qpd->sh_mem_ape1_limit = limit >> 16;
2517 	}
2518 
2519 	retval = dqm->asic_ops.set_cache_memory_policy(
2520 			dqm,
2521 			qpd,
2522 			default_policy,
2523 			alternate_policy,
2524 			alternate_aperture_base,
2525 			alternate_aperture_size);
2526 
2527 	if ((dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS) && (qpd->vmid != 0))
2528 		program_sh_mem_settings(dqm, qpd);
2529 
2530 	pr_debug("sh_mem_config: 0x%x, ape1_base: 0x%x, ape1_limit: 0x%x\n",
2531 		qpd->sh_mem_config, qpd->sh_mem_ape1_base,
2532 		qpd->sh_mem_ape1_limit);
2533 
2534 out:
2535 	dqm_unlock(dqm);
2536 	return retval;
2537 }
2538 
process_termination_nocpsch(struct device_queue_manager * dqm,struct qcm_process_device * qpd)2539 static int process_termination_nocpsch(struct device_queue_manager *dqm,
2540 		struct qcm_process_device *qpd)
2541 {
2542 	struct queue *q;
2543 	struct device_process_node *cur, *next_dpn;
2544 	int retval = 0;
2545 	bool found = false;
2546 
2547 	dqm_lock(dqm);
2548 
2549 	/* Clear all user mode queues */
2550 	while (!list_empty(&qpd->queues_list)) {
2551 		struct mqd_manager *mqd_mgr;
2552 		int ret;
2553 
2554 		q = list_first_entry(&qpd->queues_list, struct queue, list);
2555 		mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type(
2556 				q->properties.type)];
2557 		ret = destroy_queue_nocpsch_locked(dqm, qpd, q);
2558 		if (ret)
2559 			retval = ret;
2560 		dqm_unlock(dqm);
2561 		mqd_mgr->free_mqd(mqd_mgr, q->mqd, q->mqd_mem_obj);
2562 		dqm_lock(dqm);
2563 	}
2564 
2565 	/* Unregister process */
2566 	list_for_each_entry_safe(cur, next_dpn, &dqm->queues, list) {
2567 		if (qpd == cur->qpd) {
2568 			list_del(&cur->list);
2569 			kfree(cur);
2570 			dqm->processes_count--;
2571 			found = true;
2572 			break;
2573 		}
2574 	}
2575 
2576 	dqm_unlock(dqm);
2577 
2578 	/* Outside the DQM lock because under the DQM lock we can't do
2579 	 * reclaim or take other locks that others hold while reclaiming.
2580 	 */
2581 	if (found)
2582 		kfd_dec_compute_active(dqm->dev);
2583 
2584 	return retval;
2585 }
2586 
get_wave_state(struct device_queue_manager * dqm,struct queue * q,void __user * ctl_stack,u32 * ctl_stack_used_size,u32 * save_area_used_size)2587 static int get_wave_state(struct device_queue_manager *dqm,
2588 			  struct queue *q,
2589 			  void __user *ctl_stack,
2590 			  u32 *ctl_stack_used_size,
2591 			  u32 *save_area_used_size)
2592 {
2593 	struct mqd_manager *mqd_mgr;
2594 
2595 	dqm_lock(dqm);
2596 
2597 	mqd_mgr = dqm->mqd_mgrs[KFD_MQD_TYPE_CP];
2598 
2599 	if (q->properties.type != KFD_QUEUE_TYPE_COMPUTE ||
2600 	    q->properties.is_active || !q->device->kfd->cwsr_enabled ||
2601 	    !mqd_mgr->get_wave_state) {
2602 		dqm_unlock(dqm);
2603 		return -EINVAL;
2604 	}
2605 
2606 	dqm_unlock(dqm);
2607 
2608 	/*
2609 	 * get_wave_state is outside the dqm lock to prevent circular locking
2610 	 * and the queue should be protected against destruction by the process
2611 	 * lock.
2612 	 */
2613 	return mqd_mgr->get_wave_state(mqd_mgr, q->mqd, &q->properties,
2614 			ctl_stack, ctl_stack_used_size, save_area_used_size);
2615 }
2616 
get_queue_checkpoint_info(struct device_queue_manager * dqm,const struct queue * q,u32 * mqd_size,u32 * ctl_stack_size)2617 static void get_queue_checkpoint_info(struct device_queue_manager *dqm,
2618 			const struct queue *q,
2619 			u32 *mqd_size,
2620 			u32 *ctl_stack_size)
2621 {
2622 	struct mqd_manager *mqd_mgr;
2623 	enum KFD_MQD_TYPE mqd_type =
2624 			get_mqd_type_from_queue_type(q->properties.type);
2625 
2626 	dqm_lock(dqm);
2627 	mqd_mgr = dqm->mqd_mgrs[mqd_type];
2628 	*mqd_size = mqd_mgr->mqd_size;
2629 	*ctl_stack_size = 0;
2630 
2631 	if (q->properties.type == KFD_QUEUE_TYPE_COMPUTE && mqd_mgr->get_checkpoint_info)
2632 		mqd_mgr->get_checkpoint_info(mqd_mgr, q->mqd, ctl_stack_size);
2633 
2634 	dqm_unlock(dqm);
2635 }
2636 
checkpoint_mqd(struct device_queue_manager * dqm,const struct queue * q,void * mqd,void * ctl_stack)2637 static int checkpoint_mqd(struct device_queue_manager *dqm,
2638 			  const struct queue *q,
2639 			  void *mqd,
2640 			  void *ctl_stack)
2641 {
2642 	struct mqd_manager *mqd_mgr;
2643 	int r = 0;
2644 	enum KFD_MQD_TYPE mqd_type =
2645 			get_mqd_type_from_queue_type(q->properties.type);
2646 
2647 	dqm_lock(dqm);
2648 
2649 	if (q->properties.is_active || !q->device->kfd->cwsr_enabled) {
2650 		r = -EINVAL;
2651 		goto dqm_unlock;
2652 	}
2653 
2654 	mqd_mgr = dqm->mqd_mgrs[mqd_type];
2655 	if (!mqd_mgr->checkpoint_mqd) {
2656 		r = -EOPNOTSUPP;
2657 		goto dqm_unlock;
2658 	}
2659 
2660 	mqd_mgr->checkpoint_mqd(mqd_mgr, q->mqd, mqd, ctl_stack);
2661 
2662 dqm_unlock:
2663 	dqm_unlock(dqm);
2664 	return r;
2665 }
2666 
process_termination_cpsch(struct device_queue_manager * dqm,struct qcm_process_device * qpd)2667 static int process_termination_cpsch(struct device_queue_manager *dqm,
2668 		struct qcm_process_device *qpd)
2669 {
2670 	int retval;
2671 	struct queue *q;
2672 	struct device *dev = dqm->dev->adev->dev;
2673 	struct kernel_queue *kq, *kq_next;
2674 	struct mqd_manager *mqd_mgr;
2675 	struct device_process_node *cur, *next_dpn;
2676 	enum kfd_unmap_queues_filter filter =
2677 		KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES;
2678 	bool found = false;
2679 
2680 	retval = 0;
2681 
2682 	dqm_lock(dqm);
2683 
2684 	/* Clean all kernel queues */
2685 	list_for_each_entry_safe(kq, kq_next, &qpd->priv_queue_list, list) {
2686 		list_del(&kq->list);
2687 		decrement_queue_count(dqm, qpd, kq->queue);
2688 		qpd->is_debug = false;
2689 		dqm->total_queue_count--;
2690 		filter = KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES;
2691 	}
2692 
2693 	/* Clear all user mode queues */
2694 	list_for_each_entry(q, &qpd->queues_list, list) {
2695 		if (q->properties.type == KFD_QUEUE_TYPE_SDMA)
2696 			deallocate_sdma_queue(dqm, q);
2697 		else if (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)
2698 			deallocate_sdma_queue(dqm, q);
2699 
2700 		if (q->properties.is_active) {
2701 			decrement_queue_count(dqm, qpd, q);
2702 
2703 			if (dqm->dev->kfd->shared_resources.enable_mes) {
2704 				retval = remove_queue_mes(dqm, q, qpd);
2705 				if (retval)
2706 					dev_err(dev, "Failed to remove queue %d\n",
2707 						q->properties.queue_id);
2708 			}
2709 		}
2710 
2711 		dqm->total_queue_count--;
2712 	}
2713 
2714 	/* Unregister process */
2715 	list_for_each_entry_safe(cur, next_dpn, &dqm->queues, list) {
2716 		if (qpd == cur->qpd) {
2717 			list_del(&cur->list);
2718 			kfree(cur);
2719 			dqm->processes_count--;
2720 			found = true;
2721 			break;
2722 		}
2723 	}
2724 
2725 	if (!dqm->dev->kfd->shared_resources.enable_mes)
2726 		retval = execute_queues_cpsch(dqm, filter, 0, USE_DEFAULT_GRACE_PERIOD);
2727 
2728 	if ((retval || qpd->reset_wavefronts) &&
2729 	    down_read_trylock(&dqm->dev->adev->reset_domain->sem)) {
2730 		pr_warn("Resetting wave fronts (cpsch) on dev %p\n", dqm->dev);
2731 		dbgdev_wave_reset_wavefronts(dqm->dev, qpd->pqm->process);
2732 		qpd->reset_wavefronts = false;
2733 		up_read(&dqm->dev->adev->reset_domain->sem);
2734 	}
2735 
2736 	/* Lastly, free mqd resources.
2737 	 * Do free_mqd() after dqm_unlock to avoid circular locking.
2738 	 */
2739 	while (!list_empty(&qpd->queues_list)) {
2740 		q = list_first_entry(&qpd->queues_list, struct queue, list);
2741 		mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type(
2742 				q->properties.type)];
2743 		list_del(&q->list);
2744 		qpd->queue_count--;
2745 		dqm_unlock(dqm);
2746 		mqd_mgr->free_mqd(mqd_mgr, q->mqd, q->mqd_mem_obj);
2747 		dqm_lock(dqm);
2748 	}
2749 	dqm_unlock(dqm);
2750 
2751 	/* Outside the DQM lock because under the DQM lock we can't do
2752 	 * reclaim or take other locks that others hold while reclaiming.
2753 	 */
2754 	if (found)
2755 		kfd_dec_compute_active(dqm->dev);
2756 
2757 	return retval;
2758 }
2759 
init_mqd_managers(struct device_queue_manager * dqm)2760 static int init_mqd_managers(struct device_queue_manager *dqm)
2761 {
2762 	int i, j;
2763 	struct device *dev = dqm->dev->adev->dev;
2764 	struct mqd_manager *mqd_mgr;
2765 
2766 	for (i = 0; i < KFD_MQD_TYPE_MAX; i++) {
2767 		mqd_mgr = dqm->asic_ops.mqd_manager_init(i, dqm->dev);
2768 		if (!mqd_mgr) {
2769 			dev_err(dev, "mqd manager [%d] initialization failed\n", i);
2770 			goto out_free;
2771 		}
2772 		dqm->mqd_mgrs[i] = mqd_mgr;
2773 	}
2774 
2775 	return 0;
2776 
2777 out_free:
2778 	for (j = 0; j < i; j++) {
2779 		kfree(dqm->mqd_mgrs[j]);
2780 		dqm->mqd_mgrs[j] = NULL;
2781 	}
2782 
2783 	return -ENOMEM;
2784 }
2785 
2786 /* Allocate one hiq mqd (HWS) and all SDMA mqd in a continuous trunk*/
allocate_hiq_sdma_mqd(struct device_queue_manager * dqm)2787 static int allocate_hiq_sdma_mqd(struct device_queue_manager *dqm)
2788 {
2789 	int retval;
2790 	struct kfd_node *dev = dqm->dev;
2791 	struct kfd_mem_obj *mem_obj = &dqm->hiq_sdma_mqd;
2792 	uint32_t size = dqm->mqd_mgrs[KFD_MQD_TYPE_SDMA]->mqd_size *
2793 		get_num_all_sdma_engines(dqm) *
2794 		dev->kfd->device_info.num_sdma_queues_per_engine +
2795 		(dqm->mqd_mgrs[KFD_MQD_TYPE_HIQ]->mqd_size *
2796 		NUM_XCC(dqm->dev->xcc_mask));
2797 
2798 	retval = amdgpu_amdkfd_alloc_gtt_mem(dev->adev, size,
2799 		&(mem_obj->gtt_mem), &(mem_obj->gpu_addr),
2800 		(void *)&(mem_obj->cpu_ptr), false);
2801 
2802 	return retval;
2803 }
2804 
device_queue_manager_init(struct kfd_node * dev)2805 struct device_queue_manager *device_queue_manager_init(struct kfd_node *dev)
2806 {
2807 	struct device_queue_manager *dqm;
2808 
2809 	pr_debug("Loading device queue manager\n");
2810 
2811 	dqm = kzalloc(sizeof(*dqm), GFP_KERNEL);
2812 	if (!dqm)
2813 		return NULL;
2814 
2815 	switch (dev->adev->asic_type) {
2816 	/* HWS is not available on Hawaii. */
2817 	case CHIP_HAWAII:
2818 	/* HWS depends on CWSR for timely dequeue. CWSR is not
2819 	 * available on Tonga.
2820 	 *
2821 	 * FIXME: This argument also applies to Kaveri.
2822 	 */
2823 	case CHIP_TONGA:
2824 		dqm->sched_policy = KFD_SCHED_POLICY_NO_HWS;
2825 		break;
2826 	default:
2827 		dqm->sched_policy = sched_policy;
2828 		break;
2829 	}
2830 
2831 	dqm->dev = dev;
2832 	switch (dqm->sched_policy) {
2833 	case KFD_SCHED_POLICY_HWS:
2834 	case KFD_SCHED_POLICY_HWS_NO_OVERSUBSCRIPTION:
2835 		/* initialize dqm for cp scheduling */
2836 		dqm->ops.create_queue = create_queue_cpsch;
2837 		dqm->ops.initialize = initialize_cpsch;
2838 		dqm->ops.start = start_cpsch;
2839 		dqm->ops.stop = stop_cpsch;
2840 		dqm->ops.halt = halt_cpsch;
2841 		dqm->ops.unhalt = unhalt_cpsch;
2842 		dqm->ops.destroy_queue = destroy_queue_cpsch;
2843 		dqm->ops.update_queue = update_queue;
2844 		dqm->ops.register_process = register_process;
2845 		dqm->ops.unregister_process = unregister_process;
2846 		dqm->ops.uninitialize = uninitialize;
2847 		dqm->ops.create_kernel_queue = create_kernel_queue_cpsch;
2848 		dqm->ops.destroy_kernel_queue = destroy_kernel_queue_cpsch;
2849 		dqm->ops.set_cache_memory_policy = set_cache_memory_policy;
2850 		dqm->ops.process_termination = process_termination_cpsch;
2851 		dqm->ops.evict_process_queues = evict_process_queues_cpsch;
2852 		dqm->ops.restore_process_queues = restore_process_queues_cpsch;
2853 		dqm->ops.get_wave_state = get_wave_state;
2854 		dqm->ops.reset_queues = reset_queues_cpsch;
2855 		dqm->ops.get_queue_checkpoint_info = get_queue_checkpoint_info;
2856 		dqm->ops.checkpoint_mqd = checkpoint_mqd;
2857 		break;
2858 	case KFD_SCHED_POLICY_NO_HWS:
2859 		/* initialize dqm for no cp scheduling */
2860 		dqm->ops.start = start_nocpsch;
2861 		dqm->ops.stop = stop_nocpsch;
2862 		dqm->ops.create_queue = create_queue_nocpsch;
2863 		dqm->ops.destroy_queue = destroy_queue_nocpsch;
2864 		dqm->ops.update_queue = update_queue;
2865 		dqm->ops.register_process = register_process;
2866 		dqm->ops.unregister_process = unregister_process;
2867 		dqm->ops.initialize = initialize_nocpsch;
2868 		dqm->ops.uninitialize = uninitialize;
2869 		dqm->ops.set_cache_memory_policy = set_cache_memory_policy;
2870 		dqm->ops.process_termination = process_termination_nocpsch;
2871 		dqm->ops.evict_process_queues = evict_process_queues_nocpsch;
2872 		dqm->ops.restore_process_queues =
2873 			restore_process_queues_nocpsch;
2874 		dqm->ops.get_wave_state = get_wave_state;
2875 		dqm->ops.get_queue_checkpoint_info = get_queue_checkpoint_info;
2876 		dqm->ops.checkpoint_mqd = checkpoint_mqd;
2877 		break;
2878 	default:
2879 		dev_err(dev->adev->dev, "Invalid scheduling policy %d\n", dqm->sched_policy);
2880 		goto out_free;
2881 	}
2882 
2883 	switch (dev->adev->asic_type) {
2884 	case CHIP_KAVERI:
2885 	case CHIP_HAWAII:
2886 		device_queue_manager_init_cik(&dqm->asic_ops);
2887 		break;
2888 
2889 	case CHIP_CARRIZO:
2890 	case CHIP_TONGA:
2891 	case CHIP_FIJI:
2892 	case CHIP_POLARIS10:
2893 	case CHIP_POLARIS11:
2894 	case CHIP_POLARIS12:
2895 	case CHIP_VEGAM:
2896 		device_queue_manager_init_vi(&dqm->asic_ops);
2897 		break;
2898 
2899 	default:
2900 		if (KFD_GC_VERSION(dev) >= IP_VERSION(12, 0, 0))
2901 			device_queue_manager_init_v12(&dqm->asic_ops);
2902 		else if (KFD_GC_VERSION(dev) >= IP_VERSION(11, 0, 0))
2903 			device_queue_manager_init_v11(&dqm->asic_ops);
2904 		else if (KFD_GC_VERSION(dev) >= IP_VERSION(10, 1, 1))
2905 			device_queue_manager_init_v10(&dqm->asic_ops);
2906 		else if (KFD_GC_VERSION(dev) >= IP_VERSION(9, 0, 1))
2907 			device_queue_manager_init_v9(&dqm->asic_ops);
2908 		else {
2909 			WARN(1, "Unexpected ASIC family %u",
2910 			     dev->adev->asic_type);
2911 			goto out_free;
2912 		}
2913 	}
2914 
2915 	if (init_mqd_managers(dqm))
2916 		goto out_free;
2917 
2918 	if (!dev->kfd->shared_resources.enable_mes && allocate_hiq_sdma_mqd(dqm)) {
2919 		dev_err(dev->adev->dev, "Failed to allocate hiq sdma mqd trunk buffer\n");
2920 		goto out_free;
2921 	}
2922 
2923 	if (!dqm->ops.initialize(dqm)) {
2924 		init_waitqueue_head(&dqm->destroy_wait);
2925 		return dqm;
2926 	}
2927 
2928 out_free:
2929 	kfree(dqm);
2930 	return NULL;
2931 }
2932 
deallocate_hiq_sdma_mqd(struct kfd_node * dev,struct kfd_mem_obj * mqd)2933 static void deallocate_hiq_sdma_mqd(struct kfd_node *dev,
2934 				    struct kfd_mem_obj *mqd)
2935 {
2936 	WARN(!mqd, "No hiq sdma mqd trunk to free");
2937 
2938 	amdgpu_amdkfd_free_gtt_mem(dev->adev, &mqd->gtt_mem);
2939 }
2940 
device_queue_manager_uninit(struct device_queue_manager * dqm)2941 void device_queue_manager_uninit(struct device_queue_manager *dqm)
2942 {
2943 	dqm->ops.stop(dqm);
2944 	dqm->ops.uninitialize(dqm);
2945 	if (!dqm->dev->kfd->shared_resources.enable_mes)
2946 		deallocate_hiq_sdma_mqd(dqm->dev, &dqm->hiq_sdma_mqd);
2947 	kfree(dqm);
2948 }
2949 
kfd_dqm_suspend_bad_queue_mes(struct kfd_node * knode,u32 pasid,u32 doorbell_id)2950 int kfd_dqm_suspend_bad_queue_mes(struct kfd_node *knode, u32 pasid, u32 doorbell_id)
2951 {
2952 	struct kfd_process_device *pdd;
2953 	struct kfd_process *p = kfd_lookup_process_by_pasid(pasid);
2954 	struct device_queue_manager *dqm = knode->dqm;
2955 	struct device *dev = dqm->dev->adev->dev;
2956 	struct qcm_process_device *qpd;
2957 	struct queue *q = NULL;
2958 	int ret = 0;
2959 
2960 	if (!p)
2961 		return -EINVAL;
2962 
2963 	dqm_lock(dqm);
2964 
2965 	pdd = kfd_get_process_device_data(dqm->dev, p);
2966 	if (pdd) {
2967 		qpd = &pdd->qpd;
2968 
2969 		list_for_each_entry(q, &qpd->queues_list, list) {
2970 			if (q->doorbell_id == doorbell_id && q->properties.is_active) {
2971 				ret = suspend_all_queues_mes(dqm);
2972 				if (ret) {
2973 					dev_err(dev, "Suspending all queues failed");
2974 					goto out;
2975 				}
2976 
2977 				q->properties.is_evicted = true;
2978 				q->properties.is_active = false;
2979 				decrement_queue_count(dqm, qpd, q);
2980 
2981 				ret = remove_queue_mes(dqm, q, qpd);
2982 				if (ret) {
2983 					dev_err(dev, "Removing bad queue failed");
2984 					goto out;
2985 				}
2986 
2987 				ret = resume_all_queues_mes(dqm);
2988 				if (ret)
2989 					dev_err(dev, "Resuming all queues failed");
2990 
2991 				break;
2992 			}
2993 		}
2994 	}
2995 
2996 out:
2997 	dqm_unlock(dqm);
2998 	return ret;
2999 }
3000 
kfd_dqm_evict_pasid_mes(struct device_queue_manager * dqm,struct qcm_process_device * qpd)3001 static int kfd_dqm_evict_pasid_mes(struct device_queue_manager *dqm,
3002 				   struct qcm_process_device *qpd)
3003 {
3004 	struct device *dev = dqm->dev->adev->dev;
3005 	int ret = 0;
3006 
3007 	/* Check if process is already evicted */
3008 	dqm_lock(dqm);
3009 	if (qpd->evicted) {
3010 		/* Increment the evicted count to make sure the
3011 		 * process stays evicted before its terminated.
3012 		 */
3013 		qpd->evicted++;
3014 		dqm_unlock(dqm);
3015 		goto out;
3016 	}
3017 	dqm_unlock(dqm);
3018 
3019 	ret = suspend_all_queues_mes(dqm);
3020 	if (ret) {
3021 		dev_err(dev, "Suspending all queues failed");
3022 		goto out;
3023 	}
3024 
3025 	ret = dqm->ops.evict_process_queues(dqm, qpd);
3026 	if (ret) {
3027 		dev_err(dev, "Evicting process queues failed");
3028 		goto out;
3029 	}
3030 
3031 	ret = resume_all_queues_mes(dqm);
3032 	if (ret)
3033 		dev_err(dev, "Resuming all queues failed");
3034 
3035 out:
3036 	return ret;
3037 }
3038 
kfd_dqm_evict_pasid(struct device_queue_manager * dqm,u32 pasid)3039 int kfd_dqm_evict_pasid(struct device_queue_manager *dqm, u32 pasid)
3040 {
3041 	struct kfd_process_device *pdd;
3042 	struct kfd_process *p = kfd_lookup_process_by_pasid(pasid);
3043 	int ret = 0;
3044 
3045 	if (!p)
3046 		return -EINVAL;
3047 	WARN(debug_evictions, "Evicting pid %d", p->lead_thread->pid);
3048 	pdd = kfd_get_process_device_data(dqm->dev, p);
3049 	if (pdd) {
3050 		if (dqm->dev->kfd->shared_resources.enable_mes)
3051 			ret = kfd_dqm_evict_pasid_mes(dqm, &pdd->qpd);
3052 		else
3053 			ret = dqm->ops.evict_process_queues(dqm, &pdd->qpd);
3054 	}
3055 
3056 	kfd_unref_process(p);
3057 
3058 	return ret;
3059 }
3060 
kfd_process_hw_exception(struct work_struct * work)3061 static void kfd_process_hw_exception(struct work_struct *work)
3062 {
3063 	struct device_queue_manager *dqm = container_of(work,
3064 			struct device_queue_manager, hw_exception_work);
3065 	amdgpu_amdkfd_gpu_reset(dqm->dev->adev);
3066 }
3067 
reserve_debug_trap_vmid(struct device_queue_manager * dqm,struct qcm_process_device * qpd)3068 int reserve_debug_trap_vmid(struct device_queue_manager *dqm,
3069 				struct qcm_process_device *qpd)
3070 {
3071 	int r;
3072 	struct device *dev = dqm->dev->adev->dev;
3073 	int updated_vmid_mask;
3074 
3075 	if (dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS) {
3076 		dev_err(dev, "Unsupported on sched_policy: %i\n", dqm->sched_policy);
3077 		return -EINVAL;
3078 	}
3079 
3080 	dqm_lock(dqm);
3081 
3082 	if (dqm->trap_debug_vmid != 0) {
3083 		dev_err(dev, "Trap debug id already reserved\n");
3084 		r = -EBUSY;
3085 		goto out_unlock;
3086 	}
3087 
3088 	r = unmap_queues_cpsch(dqm, KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES, 0,
3089 			USE_DEFAULT_GRACE_PERIOD, false);
3090 	if (r)
3091 		goto out_unlock;
3092 
3093 	updated_vmid_mask = dqm->dev->kfd->shared_resources.compute_vmid_bitmap;
3094 	updated_vmid_mask &= ~(1 << dqm->dev->vm_info.last_vmid_kfd);
3095 
3096 	dqm->dev->kfd->shared_resources.compute_vmid_bitmap = updated_vmid_mask;
3097 	dqm->trap_debug_vmid = dqm->dev->vm_info.last_vmid_kfd;
3098 	r = set_sched_resources(dqm);
3099 	if (r)
3100 		goto out_unlock;
3101 
3102 	r = map_queues_cpsch(dqm);
3103 	if (r)
3104 		goto out_unlock;
3105 
3106 	pr_debug("Reserved VMID for trap debug: %i\n", dqm->trap_debug_vmid);
3107 
3108 out_unlock:
3109 	dqm_unlock(dqm);
3110 	return r;
3111 }
3112 
3113 /*
3114  * Releases vmid for the trap debugger
3115  */
release_debug_trap_vmid(struct device_queue_manager * dqm,struct qcm_process_device * qpd)3116 int release_debug_trap_vmid(struct device_queue_manager *dqm,
3117 			struct qcm_process_device *qpd)
3118 {
3119 	struct device *dev = dqm->dev->adev->dev;
3120 	int r;
3121 	int updated_vmid_mask;
3122 	uint32_t trap_debug_vmid;
3123 
3124 	if (dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS) {
3125 		dev_err(dev, "Unsupported on sched_policy: %i\n", dqm->sched_policy);
3126 		return -EINVAL;
3127 	}
3128 
3129 	dqm_lock(dqm);
3130 	trap_debug_vmid = dqm->trap_debug_vmid;
3131 	if (dqm->trap_debug_vmid == 0) {
3132 		dev_err(dev, "Trap debug id is not reserved\n");
3133 		r = -EINVAL;
3134 		goto out_unlock;
3135 	}
3136 
3137 	r = unmap_queues_cpsch(dqm, KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES, 0,
3138 			USE_DEFAULT_GRACE_PERIOD, false);
3139 	if (r)
3140 		goto out_unlock;
3141 
3142 	updated_vmid_mask = dqm->dev->kfd->shared_resources.compute_vmid_bitmap;
3143 	updated_vmid_mask |= (1 << dqm->dev->vm_info.last_vmid_kfd);
3144 
3145 	dqm->dev->kfd->shared_resources.compute_vmid_bitmap = updated_vmid_mask;
3146 	dqm->trap_debug_vmid = 0;
3147 	r = set_sched_resources(dqm);
3148 	if (r)
3149 		goto out_unlock;
3150 
3151 	r = map_queues_cpsch(dqm);
3152 	if (r)
3153 		goto out_unlock;
3154 
3155 	pr_debug("Released VMID for trap debug: %i\n", trap_debug_vmid);
3156 
3157 out_unlock:
3158 	dqm_unlock(dqm);
3159 	return r;
3160 }
3161 
3162 #define QUEUE_NOT_FOUND		-1
3163 /* invalidate queue operation in array */
q_array_invalidate(uint32_t num_queues,uint32_t * queue_ids)3164 static void q_array_invalidate(uint32_t num_queues, uint32_t *queue_ids)
3165 {
3166 	int i;
3167 
3168 	for (i = 0; i < num_queues; i++)
3169 		queue_ids[i] |= KFD_DBG_QUEUE_INVALID_MASK;
3170 }
3171 
3172 /* find queue index in array */
q_array_get_index(unsigned int queue_id,uint32_t num_queues,uint32_t * queue_ids)3173 static int q_array_get_index(unsigned int queue_id,
3174 		uint32_t num_queues,
3175 		uint32_t *queue_ids)
3176 {
3177 	int i;
3178 
3179 	for (i = 0; i < num_queues; i++)
3180 		if (queue_id == (queue_ids[i] & ~KFD_DBG_QUEUE_INVALID_MASK))
3181 			return i;
3182 
3183 	return QUEUE_NOT_FOUND;
3184 }
3185 
3186 struct copy_context_work_handler_workarea {
3187 	struct work_struct copy_context_work;
3188 	struct kfd_process *p;
3189 };
3190 
copy_context_work_handler(struct work_struct * work)3191 static void copy_context_work_handler (struct work_struct *work)
3192 {
3193 	struct copy_context_work_handler_workarea *workarea;
3194 	struct mqd_manager *mqd_mgr;
3195 	struct queue *q;
3196 	struct mm_struct *mm;
3197 	struct kfd_process *p;
3198 	uint32_t tmp_ctl_stack_used_size, tmp_save_area_used_size;
3199 	int i;
3200 
3201 	workarea = container_of(work,
3202 			struct copy_context_work_handler_workarea,
3203 			copy_context_work);
3204 
3205 	p = workarea->p;
3206 	mm = get_task_mm(p->lead_thread);
3207 
3208 	if (!mm)
3209 		return;
3210 
3211 	kthread_use_mm(mm);
3212 	for (i = 0; i < p->n_pdds; i++) {
3213 		struct kfd_process_device *pdd = p->pdds[i];
3214 		struct device_queue_manager *dqm = pdd->dev->dqm;
3215 		struct qcm_process_device *qpd = &pdd->qpd;
3216 
3217 		list_for_each_entry(q, &qpd->queues_list, list) {
3218 			mqd_mgr = dqm->mqd_mgrs[KFD_MQD_TYPE_CP];
3219 
3220 			/* We ignore the return value from get_wave_state
3221 			 * because
3222 			 * i) right now, it always returns 0, and
3223 			 * ii) if we hit an error, we would continue to the
3224 			 *      next queue anyway.
3225 			 */
3226 			mqd_mgr->get_wave_state(mqd_mgr,
3227 					q->mqd,
3228 					&q->properties,
3229 					(void __user *)	q->properties.ctx_save_restore_area_address,
3230 					&tmp_ctl_stack_used_size,
3231 					&tmp_save_area_used_size);
3232 		}
3233 	}
3234 	kthread_unuse_mm(mm);
3235 	mmput(mm);
3236 }
3237 
get_queue_ids(uint32_t num_queues,uint32_t * usr_queue_id_array)3238 static uint32_t *get_queue_ids(uint32_t num_queues, uint32_t *usr_queue_id_array)
3239 {
3240 	size_t array_size = num_queues * sizeof(uint32_t);
3241 
3242 	if (!usr_queue_id_array)
3243 		return NULL;
3244 
3245 	return memdup_user(usr_queue_id_array, array_size);
3246 }
3247 
resume_queues(struct kfd_process * p,uint32_t num_queues,uint32_t * usr_queue_id_array)3248 int resume_queues(struct kfd_process *p,
3249 		uint32_t num_queues,
3250 		uint32_t *usr_queue_id_array)
3251 {
3252 	uint32_t *queue_ids = NULL;
3253 	int total_resumed = 0;
3254 	int i;
3255 
3256 	if (usr_queue_id_array) {
3257 		queue_ids = get_queue_ids(num_queues, usr_queue_id_array);
3258 
3259 		if (IS_ERR(queue_ids))
3260 			return PTR_ERR(queue_ids);
3261 
3262 		/* mask all queues as invalid.  unmask per successful request */
3263 		q_array_invalidate(num_queues, queue_ids);
3264 	}
3265 
3266 	for (i = 0; i < p->n_pdds; i++) {
3267 		struct kfd_process_device *pdd = p->pdds[i];
3268 		struct device_queue_manager *dqm = pdd->dev->dqm;
3269 		struct device *dev = dqm->dev->adev->dev;
3270 		struct qcm_process_device *qpd = &pdd->qpd;
3271 		struct queue *q;
3272 		int r, per_device_resumed = 0;
3273 
3274 		dqm_lock(dqm);
3275 
3276 		/* unmask queues that resume or already resumed as valid */
3277 		list_for_each_entry(q, &qpd->queues_list, list) {
3278 			int q_idx = QUEUE_NOT_FOUND;
3279 
3280 			if (queue_ids)
3281 				q_idx = q_array_get_index(
3282 						q->properties.queue_id,
3283 						num_queues,
3284 						queue_ids);
3285 
3286 			if (!queue_ids || q_idx != QUEUE_NOT_FOUND) {
3287 				int err = resume_single_queue(dqm, &pdd->qpd, q);
3288 
3289 				if (queue_ids) {
3290 					if (!err) {
3291 						queue_ids[q_idx] &=
3292 							~KFD_DBG_QUEUE_INVALID_MASK;
3293 					} else {
3294 						queue_ids[q_idx] |=
3295 							KFD_DBG_QUEUE_ERROR_MASK;
3296 						break;
3297 					}
3298 				}
3299 
3300 				if (dqm->dev->kfd->shared_resources.enable_mes) {
3301 					wake_up_all(&dqm->destroy_wait);
3302 					if (!err)
3303 						total_resumed++;
3304 				} else {
3305 					per_device_resumed++;
3306 				}
3307 			}
3308 		}
3309 
3310 		if (!per_device_resumed) {
3311 			dqm_unlock(dqm);
3312 			continue;
3313 		}
3314 
3315 		r = execute_queues_cpsch(dqm,
3316 					KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES,
3317 					0,
3318 					USE_DEFAULT_GRACE_PERIOD);
3319 		if (r) {
3320 			dev_err(dev, "Failed to resume process queues\n");
3321 			if (queue_ids) {
3322 				list_for_each_entry(q, &qpd->queues_list, list) {
3323 					int q_idx = q_array_get_index(
3324 							q->properties.queue_id,
3325 							num_queues,
3326 							queue_ids);
3327 
3328 					/* mask queue as error on resume fail */
3329 					if (q_idx != QUEUE_NOT_FOUND)
3330 						queue_ids[q_idx] |=
3331 							KFD_DBG_QUEUE_ERROR_MASK;
3332 				}
3333 			}
3334 		} else {
3335 			wake_up_all(&dqm->destroy_wait);
3336 			total_resumed += per_device_resumed;
3337 		}
3338 
3339 		dqm_unlock(dqm);
3340 	}
3341 
3342 	if (queue_ids) {
3343 		if (copy_to_user((void __user *)usr_queue_id_array, queue_ids,
3344 				num_queues * sizeof(uint32_t)))
3345 			pr_err("copy_to_user failed on queue resume\n");
3346 
3347 		kfree(queue_ids);
3348 	}
3349 
3350 	return total_resumed;
3351 }
3352 
suspend_queues(struct kfd_process * p,uint32_t num_queues,uint32_t grace_period,uint64_t exception_clear_mask,uint32_t * usr_queue_id_array)3353 int suspend_queues(struct kfd_process *p,
3354 			uint32_t num_queues,
3355 			uint32_t grace_period,
3356 			uint64_t exception_clear_mask,
3357 			uint32_t *usr_queue_id_array)
3358 {
3359 	uint32_t *queue_ids = get_queue_ids(num_queues, usr_queue_id_array);
3360 	int total_suspended = 0;
3361 	int i;
3362 
3363 	if (IS_ERR(queue_ids))
3364 		return PTR_ERR(queue_ids);
3365 
3366 	/* mask all queues as invalid.  umask on successful request */
3367 	q_array_invalidate(num_queues, queue_ids);
3368 
3369 	for (i = 0; i < p->n_pdds; i++) {
3370 		struct kfd_process_device *pdd = p->pdds[i];
3371 		struct device_queue_manager *dqm = pdd->dev->dqm;
3372 		struct device *dev = dqm->dev->adev->dev;
3373 		struct qcm_process_device *qpd = &pdd->qpd;
3374 		struct queue *q;
3375 		int r, per_device_suspended = 0;
3376 
3377 		mutex_lock(&p->event_mutex);
3378 		dqm_lock(dqm);
3379 
3380 		/* unmask queues that suspend or already suspended */
3381 		list_for_each_entry(q, &qpd->queues_list, list) {
3382 			int q_idx = q_array_get_index(q->properties.queue_id,
3383 							num_queues,
3384 							queue_ids);
3385 
3386 			if (q_idx != QUEUE_NOT_FOUND) {
3387 				int err = suspend_single_queue(dqm, pdd, q);
3388 				bool is_mes = dqm->dev->kfd->shared_resources.enable_mes;
3389 
3390 				if (!err) {
3391 					queue_ids[q_idx] &= ~KFD_DBG_QUEUE_INVALID_MASK;
3392 					if (exception_clear_mask && is_mes)
3393 						q->properties.exception_status &=
3394 							~exception_clear_mask;
3395 
3396 					if (is_mes)
3397 						total_suspended++;
3398 					else
3399 						per_device_suspended++;
3400 				} else if (err != -EBUSY) {
3401 					r = err;
3402 					queue_ids[q_idx] |= KFD_DBG_QUEUE_ERROR_MASK;
3403 					break;
3404 				}
3405 			}
3406 		}
3407 
3408 		if (!per_device_suspended) {
3409 			dqm_unlock(dqm);
3410 			mutex_unlock(&p->event_mutex);
3411 			if (total_suspended)
3412 				amdgpu_amdkfd_debug_mem_fence(dqm->dev->adev);
3413 			continue;
3414 		}
3415 
3416 		r = execute_queues_cpsch(dqm,
3417 			KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 0,
3418 			grace_period);
3419 
3420 		if (r)
3421 			dev_err(dev, "Failed to suspend process queues.\n");
3422 		else
3423 			total_suspended += per_device_suspended;
3424 
3425 		list_for_each_entry(q, &qpd->queues_list, list) {
3426 			int q_idx = q_array_get_index(q->properties.queue_id,
3427 						num_queues, queue_ids);
3428 
3429 			if (q_idx == QUEUE_NOT_FOUND)
3430 				continue;
3431 
3432 			/* mask queue as error on suspend fail */
3433 			if (r)
3434 				queue_ids[q_idx] |= KFD_DBG_QUEUE_ERROR_MASK;
3435 			else if (exception_clear_mask)
3436 				q->properties.exception_status &=
3437 							~exception_clear_mask;
3438 		}
3439 
3440 		dqm_unlock(dqm);
3441 		mutex_unlock(&p->event_mutex);
3442 		amdgpu_device_flush_hdp(dqm->dev->adev, NULL);
3443 	}
3444 
3445 	if (total_suspended) {
3446 		struct copy_context_work_handler_workarea copy_context_worker;
3447 
3448 		INIT_WORK_ONSTACK(
3449 				&copy_context_worker.copy_context_work,
3450 				copy_context_work_handler);
3451 
3452 		copy_context_worker.p = p;
3453 
3454 		schedule_work(&copy_context_worker.copy_context_work);
3455 
3456 
3457 		flush_work(&copy_context_worker.copy_context_work);
3458 		destroy_work_on_stack(&copy_context_worker.copy_context_work);
3459 	}
3460 
3461 	if (copy_to_user((void __user *)usr_queue_id_array, queue_ids,
3462 			num_queues * sizeof(uint32_t)))
3463 		pr_err("copy_to_user failed on queue suspend\n");
3464 
3465 	kfree(queue_ids);
3466 
3467 	return total_suspended;
3468 }
3469 
set_queue_type_for_user(struct queue_properties * q_props)3470 static uint32_t set_queue_type_for_user(struct queue_properties *q_props)
3471 {
3472 	switch (q_props->type) {
3473 	case KFD_QUEUE_TYPE_COMPUTE:
3474 		return q_props->format == KFD_QUEUE_FORMAT_PM4
3475 					? KFD_IOC_QUEUE_TYPE_COMPUTE
3476 					: KFD_IOC_QUEUE_TYPE_COMPUTE_AQL;
3477 	case KFD_QUEUE_TYPE_SDMA:
3478 		return KFD_IOC_QUEUE_TYPE_SDMA;
3479 	case KFD_QUEUE_TYPE_SDMA_XGMI:
3480 		return KFD_IOC_QUEUE_TYPE_SDMA_XGMI;
3481 	default:
3482 		WARN_ONCE(true, "queue type not recognized!");
3483 		return 0xffffffff;
3484 	};
3485 }
3486 
set_queue_snapshot_entry(struct queue * q,uint64_t exception_clear_mask,struct kfd_queue_snapshot_entry * qss_entry)3487 void set_queue_snapshot_entry(struct queue *q,
3488 			      uint64_t exception_clear_mask,
3489 			      struct kfd_queue_snapshot_entry *qss_entry)
3490 {
3491 	qss_entry->ring_base_address = q->properties.queue_address;
3492 	qss_entry->write_pointer_address = (uint64_t)q->properties.write_ptr;
3493 	qss_entry->read_pointer_address = (uint64_t)q->properties.read_ptr;
3494 	qss_entry->ctx_save_restore_address =
3495 				q->properties.ctx_save_restore_area_address;
3496 	qss_entry->ctx_save_restore_area_size =
3497 				q->properties.ctx_save_restore_area_size;
3498 	qss_entry->exception_status = q->properties.exception_status;
3499 	qss_entry->queue_id = q->properties.queue_id;
3500 	qss_entry->gpu_id = q->device->id;
3501 	qss_entry->ring_size = (uint32_t)q->properties.queue_size;
3502 	qss_entry->queue_type = set_queue_type_for_user(&q->properties);
3503 	q->properties.exception_status &= ~exception_clear_mask;
3504 }
3505 
debug_lock_and_unmap(struct device_queue_manager * dqm)3506 int debug_lock_and_unmap(struct device_queue_manager *dqm)
3507 {
3508 	struct device *dev = dqm->dev->adev->dev;
3509 	int r;
3510 
3511 	if (dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS) {
3512 		dev_err(dev, "Unsupported on sched_policy: %i\n", dqm->sched_policy);
3513 		return -EINVAL;
3514 	}
3515 
3516 	if (!kfd_dbg_is_per_vmid_supported(dqm->dev))
3517 		return 0;
3518 
3519 	dqm_lock(dqm);
3520 
3521 	r = unmap_queues_cpsch(dqm, KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES, 0, 0, false);
3522 	if (r)
3523 		dqm_unlock(dqm);
3524 
3525 	return r;
3526 }
3527 
debug_map_and_unlock(struct device_queue_manager * dqm)3528 int debug_map_and_unlock(struct device_queue_manager *dqm)
3529 {
3530 	struct device *dev = dqm->dev->adev->dev;
3531 	int r;
3532 
3533 	if (dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS) {
3534 		dev_err(dev, "Unsupported on sched_policy: %i\n", dqm->sched_policy);
3535 		return -EINVAL;
3536 	}
3537 
3538 	if (!kfd_dbg_is_per_vmid_supported(dqm->dev))
3539 		return 0;
3540 
3541 	r = map_queues_cpsch(dqm);
3542 
3543 	dqm_unlock(dqm);
3544 
3545 	return r;
3546 }
3547 
debug_refresh_runlist(struct device_queue_manager * dqm)3548 int debug_refresh_runlist(struct device_queue_manager *dqm)
3549 {
3550 	int r = debug_lock_and_unmap(dqm);
3551 
3552 	if (r)
3553 		return r;
3554 
3555 	return debug_map_and_unlock(dqm);
3556 }
3557 
kfd_dqm_is_queue_in_process(struct device_queue_manager * dqm,struct qcm_process_device * qpd,int doorbell_off,u32 * queue_format)3558 bool kfd_dqm_is_queue_in_process(struct device_queue_manager *dqm,
3559 				 struct qcm_process_device *qpd,
3560 				 int doorbell_off, u32 *queue_format)
3561 {
3562 	struct queue *q;
3563 	bool r = false;
3564 
3565 	if (!queue_format)
3566 		return r;
3567 
3568 	dqm_lock(dqm);
3569 
3570 	list_for_each_entry(q, &qpd->queues_list, list) {
3571 		if (q->properties.doorbell_off == doorbell_off) {
3572 			*queue_format = q->properties.format;
3573 			r = true;
3574 			goto out;
3575 		}
3576 	}
3577 
3578 out:
3579 	dqm_unlock(dqm);
3580 	return r;
3581 }
3582 #if defined(CONFIG_DEBUG_FS)
3583 
seq_reg_dump(struct seq_file * m,uint32_t (* dump)[2],uint32_t n_regs)3584 static void seq_reg_dump(struct seq_file *m,
3585 			 uint32_t (*dump)[2], uint32_t n_regs)
3586 {
3587 	uint32_t i, count;
3588 
3589 	for (i = 0, count = 0; i < n_regs; i++) {
3590 		if (count == 0 ||
3591 		    dump[i-1][0] + sizeof(uint32_t) != dump[i][0]) {
3592 			seq_printf(m, "%s    %08x: %08x",
3593 				   i ? "\n" : "",
3594 				   dump[i][0], dump[i][1]);
3595 			count = 7;
3596 		} else {
3597 			seq_printf(m, " %08x", dump[i][1]);
3598 			count--;
3599 		}
3600 	}
3601 
3602 	seq_puts(m, "\n");
3603 }
3604 
dqm_debugfs_hqds(struct seq_file * m,void * data)3605 int dqm_debugfs_hqds(struct seq_file *m, void *data)
3606 {
3607 	struct device_queue_manager *dqm = data;
3608 	uint32_t xcc_mask = dqm->dev->xcc_mask;
3609 	uint32_t (*dump)[2], n_regs;
3610 	int pipe, queue;
3611 	int r = 0, xcc_id;
3612 	uint32_t sdma_engine_start;
3613 
3614 	if (!dqm->sched_running) {
3615 		seq_puts(m, " Device is stopped\n");
3616 		return 0;
3617 	}
3618 
3619 	for_each_inst(xcc_id, xcc_mask) {
3620 		r = dqm->dev->kfd2kgd->hqd_dump(dqm->dev->adev,
3621 						KFD_CIK_HIQ_PIPE,
3622 						KFD_CIK_HIQ_QUEUE, &dump,
3623 						&n_regs, xcc_id);
3624 		if (!r) {
3625 			seq_printf(
3626 				m,
3627 				"   Inst %d, HIQ on MEC %d Pipe %d Queue %d\n",
3628 				xcc_id,
3629 				KFD_CIK_HIQ_PIPE / get_pipes_per_mec(dqm) + 1,
3630 				KFD_CIK_HIQ_PIPE % get_pipes_per_mec(dqm),
3631 				KFD_CIK_HIQ_QUEUE);
3632 			seq_reg_dump(m, dump, n_regs);
3633 
3634 			kfree(dump);
3635 		}
3636 
3637 		for (pipe = 0; pipe < get_pipes_per_mec(dqm); pipe++) {
3638 			int pipe_offset = pipe * get_queues_per_pipe(dqm);
3639 
3640 			for (queue = 0; queue < get_queues_per_pipe(dqm); queue++) {
3641 				if (!test_bit(pipe_offset + queue,
3642 				      dqm->dev->kfd->shared_resources.cp_queue_bitmap))
3643 					continue;
3644 
3645 				r = dqm->dev->kfd2kgd->hqd_dump(dqm->dev->adev,
3646 								pipe, queue,
3647 								&dump, &n_regs,
3648 								xcc_id);
3649 				if (r)
3650 					break;
3651 
3652 				seq_printf(m,
3653 					   " Inst %d,  CP Pipe %d, Queue %d\n",
3654 					   xcc_id, pipe, queue);
3655 				seq_reg_dump(m, dump, n_regs);
3656 
3657 				kfree(dump);
3658 			}
3659 		}
3660 	}
3661 
3662 	sdma_engine_start = dqm->dev->node_id * get_num_all_sdma_engines(dqm);
3663 	for (pipe = sdma_engine_start;
3664 	     pipe < (sdma_engine_start + get_num_all_sdma_engines(dqm));
3665 	     pipe++) {
3666 		for (queue = 0;
3667 		     queue < dqm->dev->kfd->device_info.num_sdma_queues_per_engine;
3668 		     queue++) {
3669 			r = dqm->dev->kfd2kgd->hqd_sdma_dump(
3670 				dqm->dev->adev, pipe, queue, &dump, &n_regs);
3671 			if (r)
3672 				break;
3673 
3674 			seq_printf(m, "  SDMA Engine %d, RLC %d\n",
3675 				  pipe, queue);
3676 			seq_reg_dump(m, dump, n_regs);
3677 
3678 			kfree(dump);
3679 		}
3680 	}
3681 
3682 	return r;
3683 }
3684 
dqm_debugfs_hang_hws(struct device_queue_manager * dqm)3685 int dqm_debugfs_hang_hws(struct device_queue_manager *dqm)
3686 {
3687 	int r = 0;
3688 
3689 	dqm_lock(dqm);
3690 	r = pm_debugfs_hang_hws(&dqm->packet_mgr);
3691 	if (r) {
3692 		dqm_unlock(dqm);
3693 		return r;
3694 	}
3695 	dqm->active_runlist = true;
3696 	r = execute_queues_cpsch(dqm, KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES,
3697 				0, USE_DEFAULT_GRACE_PERIOD);
3698 	dqm_unlock(dqm);
3699 
3700 	return r;
3701 }
3702 
3703 #endif
3704