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