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