1 // SPDX-License-Identifier: GPL-2.0 OR MIT
3 * Copyright 2014-2022 Advanced Micro Devices, Inc.
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
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>
33 #include "kfd_device_queue_manager.h"
34 #include "kfd_mqd_manager.h"
36 #include "kfd_kernel_queue.h"
37 #include "amdgpu_amdkfd.h"
38 #include "mes_api_def.h"
39 #include "kfd_debug.h"
41 /* Size of the per-pipe EOP queue */
42 #define CIK_HPD_EOP_BYTES_LOG2 11
43 #define CIK_HPD_EOP_BYTES (1U << CIK_HPD_EOP_BYTES_LOG2)
45 static int set_pasid_vmid_mapping(struct device_queue_manager *dqm,
46 u32 pasid, unsigned int vmid);
48 static int execute_queues_cpsch(struct device_queue_manager *dqm,
49 enum kfd_unmap_queues_filter filter,
50 uint32_t filter_param,
51 uint32_t grace_period);
52 static int unmap_queues_cpsch(struct device_queue_manager *dqm,
53 enum kfd_unmap_queues_filter filter,
54 uint32_t filter_param,
55 uint32_t grace_period,
58 static int map_queues_cpsch(struct device_queue_manager *dqm);
60 static void deallocate_sdma_queue(struct device_queue_manager *dqm,
63 static inline void deallocate_hqd(struct device_queue_manager *dqm,
65 static int allocate_hqd(struct device_queue_manager *dqm, struct queue *q);
66 static int allocate_sdma_queue(struct device_queue_manager *dqm,
67 struct queue *q, const uint32_t *restore_sdma_id);
68 static void kfd_process_hw_exception(struct work_struct *work);
71 enum KFD_MQD_TYPE get_mqd_type_from_queue_type(enum kfd_queue_type type)
73 if (type == KFD_QUEUE_TYPE_SDMA || type == KFD_QUEUE_TYPE_SDMA_XGMI)
74 return KFD_MQD_TYPE_SDMA;
75 return KFD_MQD_TYPE_CP;
78 static bool is_pipe_enabled(struct device_queue_manager *dqm, int mec, int pipe)
81 int pipe_offset = (mec * dqm->dev->kfd->shared_resources.num_pipe_per_mec
82 + pipe) * dqm->dev->kfd->shared_resources.num_queue_per_pipe;
84 /* queue is available for KFD usage if bit is 1 */
85 for (i = 0; i < dqm->dev->kfd->shared_resources.num_queue_per_pipe; ++i)
86 if (test_bit(pipe_offset + i,
87 dqm->dev->kfd->shared_resources.cp_queue_bitmap))
92 unsigned int get_cp_queues_num(struct device_queue_manager *dqm)
94 return bitmap_weight(dqm->dev->kfd->shared_resources.cp_queue_bitmap,
98 unsigned int get_queues_per_pipe(struct device_queue_manager *dqm)
100 return dqm->dev->kfd->shared_resources.num_queue_per_pipe;
103 unsigned int get_pipes_per_mec(struct device_queue_manager *dqm)
105 return dqm->dev->kfd->shared_resources.num_pipe_per_mec;
108 static unsigned int get_num_all_sdma_engines(struct device_queue_manager *dqm)
110 return kfd_get_num_sdma_engines(dqm->dev) +
111 kfd_get_num_xgmi_sdma_engines(dqm->dev);
114 unsigned int get_num_sdma_queues(struct device_queue_manager *dqm)
116 return kfd_get_num_sdma_engines(dqm->dev) *
117 dqm->dev->kfd->device_info.num_sdma_queues_per_engine;
120 unsigned int get_num_xgmi_sdma_queues(struct device_queue_manager *dqm)
122 return kfd_get_num_xgmi_sdma_engines(dqm->dev) *
123 dqm->dev->kfd->device_info.num_sdma_queues_per_engine;
126 static void init_sdma_bitmaps(struct device_queue_manager *dqm)
128 bitmap_zero(dqm->sdma_bitmap, KFD_MAX_SDMA_QUEUES);
129 bitmap_set(dqm->sdma_bitmap, 0, get_num_sdma_queues(dqm));
131 bitmap_zero(dqm->xgmi_sdma_bitmap, KFD_MAX_SDMA_QUEUES);
132 bitmap_set(dqm->xgmi_sdma_bitmap, 0, get_num_xgmi_sdma_queues(dqm));
134 /* Mask out the reserved queues */
135 bitmap_andnot(dqm->sdma_bitmap, dqm->sdma_bitmap,
136 dqm->dev->kfd->device_info.reserved_sdma_queues_bitmap,
137 KFD_MAX_SDMA_QUEUES);
140 void program_sh_mem_settings(struct device_queue_manager *dqm,
141 struct qcm_process_device *qpd)
143 uint32_t xcc_mask = dqm->dev->xcc_mask;
146 for_each_inst(xcc_id, xcc_mask)
147 dqm->dev->kfd2kgd->program_sh_mem_settings(
148 dqm->dev->adev, qpd->vmid, qpd->sh_mem_config,
149 qpd->sh_mem_ape1_base, qpd->sh_mem_ape1_limit,
150 qpd->sh_mem_bases, xcc_id);
153 static void kfd_hws_hang(struct device_queue_manager *dqm)
156 * Issue a GPU reset if HWS is unresponsive
158 dqm->is_hws_hang = true;
160 /* It's possible we're detecting a HWS hang in the
161 * middle of a GPU reset. No need to schedule another
162 * reset in this case.
164 if (!dqm->is_resetting)
165 schedule_work(&dqm->hw_exception_work);
168 static int convert_to_mes_queue_type(int queue_type)
172 switch (queue_type) {
173 case KFD_QUEUE_TYPE_COMPUTE:
174 mes_queue_type = MES_QUEUE_TYPE_COMPUTE;
176 case KFD_QUEUE_TYPE_SDMA:
177 mes_queue_type = MES_QUEUE_TYPE_SDMA;
180 WARN(1, "Invalid queue type %d", queue_type);
181 mes_queue_type = -EINVAL;
185 return mes_queue_type;
188 static int add_queue_mes(struct device_queue_manager *dqm, struct queue *q,
189 struct qcm_process_device *qpd)
191 struct amdgpu_device *adev = (struct amdgpu_device *)dqm->dev->adev;
192 struct kfd_process_device *pdd = qpd_to_pdd(qpd);
193 struct mes_add_queue_input queue_input;
195 uint64_t wptr_addr_off;
197 if (dqm->is_hws_hang)
200 memset(&queue_input, 0x0, sizeof(struct mes_add_queue_input));
201 queue_input.process_id = qpd->pqm->process->pasid;
202 queue_input.page_table_base_addr = qpd->page_table_base;
203 queue_input.process_va_start = 0;
204 queue_input.process_va_end = adev->vm_manager.max_pfn - 1;
205 /* MES unit for quantum is 100ns */
206 queue_input.process_quantum = KFD_MES_PROCESS_QUANTUM; /* Equivalent to 10ms. */
207 queue_input.process_context_addr = pdd->proc_ctx_gpu_addr;
208 queue_input.gang_quantum = KFD_MES_GANG_QUANTUM; /* Equivalent to 1ms */
209 queue_input.gang_context_addr = q->gang_ctx_gpu_addr;
210 queue_input.inprocess_gang_priority = q->properties.priority;
211 queue_input.gang_global_priority_level =
212 AMDGPU_MES_PRIORITY_LEVEL_NORMAL;
213 queue_input.doorbell_offset = q->properties.doorbell_off;
214 queue_input.mqd_addr = q->gart_mqd_addr;
215 queue_input.wptr_addr = (uint64_t)q->properties.write_ptr;
218 wptr_addr_off = (uint64_t)q->properties.write_ptr & (PAGE_SIZE - 1);
219 queue_input.wptr_mc_addr = amdgpu_bo_gpu_offset(q->wptr_bo) + wptr_addr_off;
222 queue_input.is_kfd_process = 1;
223 queue_input.is_aql_queue = (q->properties.format == KFD_QUEUE_FORMAT_AQL);
224 queue_input.queue_size = q->properties.queue_size >> 2;
226 queue_input.paging = false;
227 queue_input.tba_addr = qpd->tba_addr;
228 queue_input.tma_addr = qpd->tma_addr;
229 queue_input.trap_en = !kfd_dbg_has_cwsr_workaround(q->device);
230 queue_input.skip_process_ctx_clear =
231 qpd->pqm->process->runtime_info.runtime_state == DEBUG_RUNTIME_STATE_ENABLED &&
232 (qpd->pqm->process->debug_trap_enabled ||
233 kfd_dbg_has_ttmps_always_setup(q->device));
235 queue_type = convert_to_mes_queue_type(q->properties.type);
236 if (queue_type < 0) {
237 dev_err(adev->dev, "Queue type not supported with MES, queue:%d\n",
241 queue_input.queue_type = (uint32_t)queue_type;
243 queue_input.exclusively_scheduled = q->properties.is_gws;
245 amdgpu_mes_lock(&adev->mes);
246 r = adev->mes.funcs->add_hw_queue(&adev->mes, &queue_input);
247 amdgpu_mes_unlock(&adev->mes);
249 dev_err(adev->dev, "failed to add hardware queue to MES, doorbell=0x%x\n",
250 q->properties.doorbell_off);
251 dev_err(adev->dev, "MES might be in unrecoverable state, issue a GPU reset\n");
258 static int remove_queue_mes(struct device_queue_manager *dqm, struct queue *q,
259 struct qcm_process_device *qpd)
261 struct amdgpu_device *adev = (struct amdgpu_device *)dqm->dev->adev;
263 struct mes_remove_queue_input queue_input;
265 if (dqm->is_hws_hang)
268 memset(&queue_input, 0x0, sizeof(struct mes_remove_queue_input));
269 queue_input.doorbell_offset = q->properties.doorbell_off;
270 queue_input.gang_context_addr = q->gang_ctx_gpu_addr;
272 amdgpu_mes_lock(&adev->mes);
273 r = adev->mes.funcs->remove_hw_queue(&adev->mes, &queue_input);
274 amdgpu_mes_unlock(&adev->mes);
277 dev_err(adev->dev, "failed to remove hardware queue from MES, doorbell=0x%x\n",
278 q->properties.doorbell_off);
279 dev_err(adev->dev, "MES might be in unrecoverable state, issue a GPU reset\n");
286 static int remove_all_queues_mes(struct device_queue_manager *dqm)
288 struct device_process_node *cur;
289 struct device *dev = dqm->dev->adev->dev;
290 struct qcm_process_device *qpd;
294 list_for_each_entry(cur, &dqm->queues, list) {
296 list_for_each_entry(q, &qpd->queues_list, list) {
297 if (q->properties.is_active) {
298 retval = remove_queue_mes(dqm, q, qpd);
300 dev_err(dev, "%s: Failed to remove queue %d for dev %d",
302 q->properties.queue_id,
313 static void increment_queue_count(struct device_queue_manager *dqm,
314 struct qcm_process_device *qpd,
317 dqm->active_queue_count++;
318 if (q->properties.type == KFD_QUEUE_TYPE_COMPUTE ||
319 q->properties.type == KFD_QUEUE_TYPE_DIQ)
320 dqm->active_cp_queue_count++;
322 if (q->properties.is_gws) {
323 dqm->gws_queue_count++;
324 qpd->mapped_gws_queue = true;
328 static void decrement_queue_count(struct device_queue_manager *dqm,
329 struct qcm_process_device *qpd,
332 dqm->active_queue_count--;
333 if (q->properties.type == KFD_QUEUE_TYPE_COMPUTE ||
334 q->properties.type == KFD_QUEUE_TYPE_DIQ)
335 dqm->active_cp_queue_count--;
337 if (q->properties.is_gws) {
338 dqm->gws_queue_count--;
339 qpd->mapped_gws_queue = false;
344 * Allocate a doorbell ID to this queue.
345 * If doorbell_id is passed in, make sure requested ID is valid then allocate it.
347 static int allocate_doorbell(struct qcm_process_device *qpd,
349 uint32_t const *restore_id)
351 struct kfd_node *dev = qpd->dqm->dev;
353 if (!KFD_IS_SOC15(dev)) {
354 /* On pre-SOC15 chips we need to use the queue ID to
355 * preserve the user mode ABI.
358 if (restore_id && *restore_id != q->properties.queue_id)
361 q->doorbell_id = q->properties.queue_id;
362 } else if (q->properties.type == KFD_QUEUE_TYPE_SDMA ||
363 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) {
364 /* For SDMA queues on SOC15 with 8-byte doorbell, use static
365 * doorbell assignments based on the engine and queue id.
366 * The doobell index distance between RLC (2*i) and (2*i+1)
367 * for a SDMA engine is 512.
370 uint32_t *idx_offset = dev->kfd->shared_resources.sdma_doorbell_idx;
373 * q->properties.sdma_engine_id corresponds to the virtual
374 * sdma engine number. However, for doorbell allocation,
375 * we need the physical sdma engine id in order to get the
376 * correct doorbell offset.
378 uint32_t valid_id = idx_offset[qpd->dqm->dev->node_id *
379 get_num_all_sdma_engines(qpd->dqm) +
380 q->properties.sdma_engine_id]
381 + (q->properties.sdma_queue_id & 1)
382 * KFD_QUEUE_DOORBELL_MIRROR_OFFSET
383 + (q->properties.sdma_queue_id >> 1);
385 if (restore_id && *restore_id != valid_id)
387 q->doorbell_id = valid_id;
389 /* For CP queues on SOC15 */
391 /* make sure that ID is free */
392 if (__test_and_set_bit(*restore_id, qpd->doorbell_bitmap))
395 q->doorbell_id = *restore_id;
397 /* or reserve a free doorbell ID */
400 found = find_first_zero_bit(qpd->doorbell_bitmap,
401 KFD_MAX_NUM_OF_QUEUES_PER_PROCESS);
402 if (found >= KFD_MAX_NUM_OF_QUEUES_PER_PROCESS) {
403 pr_debug("No doorbells available");
406 set_bit(found, qpd->doorbell_bitmap);
407 q->doorbell_id = found;
411 q->properties.doorbell_off = amdgpu_doorbell_index_on_bar(dev->adev,
414 dev->kfd->device_info.doorbell_size);
418 static void deallocate_doorbell(struct qcm_process_device *qpd,
422 struct kfd_node *dev = qpd->dqm->dev;
424 if (!KFD_IS_SOC15(dev) ||
425 q->properties.type == KFD_QUEUE_TYPE_SDMA ||
426 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)
429 old = test_and_clear_bit(q->doorbell_id, qpd->doorbell_bitmap);
433 static void program_trap_handler_settings(struct device_queue_manager *dqm,
434 struct qcm_process_device *qpd)
436 uint32_t xcc_mask = dqm->dev->xcc_mask;
439 if (dqm->dev->kfd2kgd->program_trap_handler_settings)
440 for_each_inst(xcc_id, xcc_mask)
441 dqm->dev->kfd2kgd->program_trap_handler_settings(
442 dqm->dev->adev, qpd->vmid, qpd->tba_addr,
443 qpd->tma_addr, xcc_id);
446 static int allocate_vmid(struct device_queue_manager *dqm,
447 struct qcm_process_device *qpd,
450 struct device *dev = dqm->dev->adev->dev;
451 int allocated_vmid = -1, i;
453 for (i = dqm->dev->vm_info.first_vmid_kfd;
454 i <= dqm->dev->vm_info.last_vmid_kfd; i++) {
455 if (!dqm->vmid_pasid[i]) {
461 if (allocated_vmid < 0) {
462 dev_err(dev, "no more vmid to allocate\n");
466 pr_debug("vmid allocated: %d\n", allocated_vmid);
468 dqm->vmid_pasid[allocated_vmid] = q->process->pasid;
470 set_pasid_vmid_mapping(dqm, q->process->pasid, allocated_vmid);
472 qpd->vmid = allocated_vmid;
473 q->properties.vmid = allocated_vmid;
475 program_sh_mem_settings(dqm, qpd);
477 if (KFD_IS_SOC15(dqm->dev) && dqm->dev->kfd->cwsr_enabled)
478 program_trap_handler_settings(dqm, qpd);
480 /* qpd->page_table_base is set earlier when register_process()
481 * is called, i.e. when the first queue is created.
483 dqm->dev->kfd2kgd->set_vm_context_page_table_base(dqm->dev->adev,
485 qpd->page_table_base);
486 /* invalidate the VM context after pasid and vmid mapping is set up */
487 kfd_flush_tlb(qpd_to_pdd(qpd), TLB_FLUSH_LEGACY);
489 if (dqm->dev->kfd2kgd->set_scratch_backing_va)
490 dqm->dev->kfd2kgd->set_scratch_backing_va(dqm->dev->adev,
491 qpd->sh_hidden_private_base, qpd->vmid);
496 static int flush_texture_cache_nocpsch(struct kfd_node *kdev,
497 struct qcm_process_device *qpd)
499 const struct packet_manager_funcs *pmf = qpd->dqm->packet_mgr.pmf;
505 ret = pmf->release_mem(qpd->ib_base, (uint32_t *)qpd->ib_kaddr);
509 return amdgpu_amdkfd_submit_ib(kdev->adev, KGD_ENGINE_MEC1, qpd->vmid,
510 qpd->ib_base, (uint32_t *)qpd->ib_kaddr,
511 pmf->release_mem_size / sizeof(uint32_t));
514 static void deallocate_vmid(struct device_queue_manager *dqm,
515 struct qcm_process_device *qpd,
518 struct device *dev = dqm->dev->adev->dev;
520 /* On GFX v7, CP doesn't flush TC at dequeue */
521 if (q->device->adev->asic_type == CHIP_HAWAII)
522 if (flush_texture_cache_nocpsch(q->device, qpd))
523 dev_err(dev, "Failed to flush TC\n");
525 kfd_flush_tlb(qpd_to_pdd(qpd), TLB_FLUSH_LEGACY);
527 /* Release the vmid mapping */
528 set_pasid_vmid_mapping(dqm, 0, qpd->vmid);
529 dqm->vmid_pasid[qpd->vmid] = 0;
532 q->properties.vmid = 0;
535 static int create_queue_nocpsch(struct device_queue_manager *dqm,
537 struct qcm_process_device *qpd,
538 const struct kfd_criu_queue_priv_data *qd,
539 const void *restore_mqd, const void *restore_ctl_stack)
541 struct mqd_manager *mqd_mgr;
546 if (dqm->total_queue_count >= max_num_of_queues_per_device) {
547 pr_warn("Can't create new usermode queue because %d queues were already created\n",
548 dqm->total_queue_count);
553 if (list_empty(&qpd->queues_list)) {
554 retval = allocate_vmid(dqm, qpd, q);
558 q->properties.vmid = qpd->vmid;
560 * Eviction state logic: mark all queues as evicted, even ones
561 * not currently active. Restoring inactive queues later only
562 * updates the is_evicted flag but is a no-op otherwise.
564 q->properties.is_evicted = !!qpd->evicted;
566 q->properties.tba_addr = qpd->tba_addr;
567 q->properties.tma_addr = qpd->tma_addr;
569 mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type(
570 q->properties.type)];
571 if (q->properties.type == KFD_QUEUE_TYPE_COMPUTE) {
572 retval = allocate_hqd(dqm, q);
574 goto deallocate_vmid;
575 pr_debug("Loading mqd to hqd on pipe %d, queue %d\n",
577 } else if (q->properties.type == KFD_QUEUE_TYPE_SDMA ||
578 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) {
579 retval = allocate_sdma_queue(dqm, q, qd ? &qd->sdma_id : NULL);
581 goto deallocate_vmid;
582 dqm->asic_ops.init_sdma_vm(dqm, q, qpd);
585 retval = allocate_doorbell(qpd, q, qd ? &qd->doorbell_id : NULL);
587 goto out_deallocate_hqd;
589 /* Temporarily release dqm lock to avoid a circular lock dependency */
591 q->mqd_mem_obj = mqd_mgr->allocate_mqd(mqd_mgr->dev, &q->properties);
594 if (!q->mqd_mem_obj) {
596 goto out_deallocate_doorbell;
600 mqd_mgr->restore_mqd(mqd_mgr, &q->mqd, q->mqd_mem_obj, &q->gart_mqd_addr,
601 &q->properties, restore_mqd, restore_ctl_stack,
604 mqd_mgr->init_mqd(mqd_mgr, &q->mqd, q->mqd_mem_obj,
605 &q->gart_mqd_addr, &q->properties);
607 if (q->properties.is_active) {
608 if (!dqm->sched_running) {
609 WARN_ONCE(1, "Load non-HWS mqd while stopped\n");
610 goto add_queue_to_list;
613 if (WARN(q->process->mm != current->mm,
614 "should only run in user thread"))
617 retval = mqd_mgr->load_mqd(mqd_mgr, q->mqd, q->pipe,
618 q->queue, &q->properties, current->mm);
624 list_add(&q->list, &qpd->queues_list);
626 if (q->properties.is_active)
627 increment_queue_count(dqm, qpd, q);
630 * Unconditionally increment this counter, regardless of the queue's
631 * type or whether the queue is active.
633 dqm->total_queue_count++;
634 pr_debug("Total of %d queues are accountable so far\n",
635 dqm->total_queue_count);
639 mqd_mgr->free_mqd(mqd_mgr, q->mqd, q->mqd_mem_obj);
640 out_deallocate_doorbell:
641 deallocate_doorbell(qpd, q);
643 if (q->properties.type == KFD_QUEUE_TYPE_COMPUTE)
644 deallocate_hqd(dqm, q);
645 else if (q->properties.type == KFD_QUEUE_TYPE_SDMA ||
646 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)
647 deallocate_sdma_queue(dqm, q);
649 if (list_empty(&qpd->queues_list))
650 deallocate_vmid(dqm, qpd, q);
656 static int allocate_hqd(struct device_queue_manager *dqm, struct queue *q)
663 for (pipe = dqm->next_pipe_to_allocate, i = 0;
664 i < get_pipes_per_mec(dqm);
665 pipe = ((pipe + 1) % get_pipes_per_mec(dqm)), ++i) {
667 if (!is_pipe_enabled(dqm, 0, pipe))
670 if (dqm->allocated_queues[pipe] != 0) {
671 bit = ffs(dqm->allocated_queues[pipe]) - 1;
672 dqm->allocated_queues[pipe] &= ~(1 << bit);
683 pr_debug("hqd slot - pipe %d, queue %d\n", q->pipe, q->queue);
684 /* horizontal hqd allocation */
685 dqm->next_pipe_to_allocate = (pipe + 1) % get_pipes_per_mec(dqm);
690 static inline void deallocate_hqd(struct device_queue_manager *dqm,
693 dqm->allocated_queues[q->pipe] |= (1 << q->queue);
696 #define SQ_IND_CMD_CMD_KILL 0x00000003
697 #define SQ_IND_CMD_MODE_BROADCAST 0x00000001
699 static int dbgdev_wave_reset_wavefronts(struct kfd_node *dev, struct kfd_process *p)
703 uint16_t queried_pasid;
704 union SQ_CMD_BITS reg_sq_cmd;
705 union GRBM_GFX_INDEX_BITS reg_gfx_index;
706 struct kfd_process_device *pdd;
707 int first_vmid_to_scan = dev->vm_info.first_vmid_kfd;
708 int last_vmid_to_scan = dev->vm_info.last_vmid_kfd;
709 uint32_t xcc_mask = dev->xcc_mask;
712 reg_sq_cmd.u32All = 0;
713 reg_gfx_index.u32All = 0;
715 pr_debug("Killing all process wavefronts\n");
717 if (!dev->kfd2kgd->get_atc_vmid_pasid_mapping_info) {
718 dev_err(dev->adev->dev, "no vmid pasid mapping supported\n");
722 /* Scan all registers in the range ATC_VMID8_PASID_MAPPING ..
723 * ATC_VMID15_PASID_MAPPING
724 * to check which VMID the current process is mapped to.
727 for (vmid = first_vmid_to_scan; vmid <= last_vmid_to_scan; vmid++) {
728 status = dev->kfd2kgd->get_atc_vmid_pasid_mapping_info
729 (dev->adev, vmid, &queried_pasid);
731 if (status && queried_pasid == p->pasid) {
732 pr_debug("Killing wave fronts of vmid %d and pasid 0x%x\n",
738 if (vmid > last_vmid_to_scan) {
739 dev_err(dev->adev->dev, "Didn't find vmid for pasid 0x%x\n", p->pasid);
743 /* taking the VMID for that process on the safe way using PDD */
744 pdd = kfd_get_process_device_data(dev, p);
748 reg_gfx_index.bits.sh_broadcast_writes = 1;
749 reg_gfx_index.bits.se_broadcast_writes = 1;
750 reg_gfx_index.bits.instance_broadcast_writes = 1;
751 reg_sq_cmd.bits.mode = SQ_IND_CMD_MODE_BROADCAST;
752 reg_sq_cmd.bits.cmd = SQ_IND_CMD_CMD_KILL;
753 reg_sq_cmd.bits.vm_id = vmid;
755 for_each_inst(xcc_id, xcc_mask)
756 dev->kfd2kgd->wave_control_execute(
757 dev->adev, reg_gfx_index.u32All,
758 reg_sq_cmd.u32All, xcc_id);
763 /* Access to DQM has to be locked before calling destroy_queue_nocpsch_locked
764 * to avoid asynchronized access
766 static int destroy_queue_nocpsch_locked(struct device_queue_manager *dqm,
767 struct qcm_process_device *qpd,
771 struct mqd_manager *mqd_mgr;
773 mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type(
774 q->properties.type)];
776 if (q->properties.type == KFD_QUEUE_TYPE_COMPUTE)
777 deallocate_hqd(dqm, q);
778 else if (q->properties.type == KFD_QUEUE_TYPE_SDMA)
779 deallocate_sdma_queue(dqm, q);
780 else if (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)
781 deallocate_sdma_queue(dqm, q);
783 pr_debug("q->properties.type %d is invalid\n",
787 dqm->total_queue_count--;
789 deallocate_doorbell(qpd, q);
791 if (!dqm->sched_running) {
792 WARN_ONCE(1, "Destroy non-HWS queue while stopped\n");
796 retval = mqd_mgr->destroy_mqd(mqd_mgr, q->mqd,
797 KFD_PREEMPT_TYPE_WAVEFRONT_RESET,
798 KFD_UNMAP_LATENCY_MS,
800 if (retval == -ETIME)
801 qpd->reset_wavefronts = true;
804 if (list_empty(&qpd->queues_list)) {
805 if (qpd->reset_wavefronts) {
806 pr_warn("Resetting wave fronts (nocpsch) on dev %p\n",
808 /* dbgdev_wave_reset_wavefronts has to be called before
809 * deallocate_vmid(), i.e. when vmid is still in use.
811 dbgdev_wave_reset_wavefronts(dqm->dev,
813 qpd->reset_wavefronts = false;
816 deallocate_vmid(dqm, qpd, q);
819 if (q->properties.is_active)
820 decrement_queue_count(dqm, qpd, q);
825 static int destroy_queue_nocpsch(struct device_queue_manager *dqm,
826 struct qcm_process_device *qpd,
830 uint64_t sdma_val = 0;
831 struct device *dev = dqm->dev->adev->dev;
832 struct kfd_process_device *pdd = qpd_to_pdd(qpd);
833 struct mqd_manager *mqd_mgr =
834 dqm->mqd_mgrs[get_mqd_type_from_queue_type(q->properties.type)];
836 /* Get the SDMA queue stats */
837 if ((q->properties.type == KFD_QUEUE_TYPE_SDMA) ||
838 (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)) {
839 retval = read_sdma_queue_counter((uint64_t __user *)q->properties.read_ptr,
842 dev_err(dev, "Failed to read SDMA queue counter for queue: %d\n",
843 q->properties.queue_id);
847 retval = destroy_queue_nocpsch_locked(dqm, qpd, q);
849 pdd->sdma_past_activity_counter += sdma_val;
852 mqd_mgr->free_mqd(mqd_mgr, q->mqd, q->mqd_mem_obj);
857 static int update_queue(struct device_queue_manager *dqm, struct queue *q,
858 struct mqd_update_info *minfo)
861 struct device *dev = dqm->dev->adev->dev;
862 struct mqd_manager *mqd_mgr;
863 struct kfd_process_device *pdd;
864 bool prev_active = false;
867 pdd = kfd_get_process_device_data(q->device, q->process);
872 mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type(
873 q->properties.type)];
875 /* Save previous activity state for counters */
876 prev_active = q->properties.is_active;
878 /* Make sure the queue is unmapped before updating the MQD */
879 if (dqm->sched_policy != KFD_SCHED_POLICY_NO_HWS) {
880 if (!dqm->dev->kfd->shared_resources.enable_mes)
881 retval = unmap_queues_cpsch(dqm,
882 KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 0, USE_DEFAULT_GRACE_PERIOD, false);
883 else if (prev_active)
884 retval = remove_queue_mes(dqm, q, &pdd->qpd);
887 dev_err(dev, "unmap queue failed\n");
890 } else if (prev_active &&
891 (q->properties.type == KFD_QUEUE_TYPE_COMPUTE ||
892 q->properties.type == KFD_QUEUE_TYPE_SDMA ||
893 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)) {
895 if (!dqm->sched_running) {
896 WARN_ONCE(1, "Update non-HWS queue while stopped\n");
900 retval = mqd_mgr->destroy_mqd(mqd_mgr, q->mqd,
901 (dqm->dev->kfd->cwsr_enabled ?
902 KFD_PREEMPT_TYPE_WAVEFRONT_SAVE :
903 KFD_PREEMPT_TYPE_WAVEFRONT_DRAIN),
904 KFD_UNMAP_LATENCY_MS, q->pipe, q->queue);
906 dev_err(dev, "destroy mqd failed\n");
911 mqd_mgr->update_mqd(mqd_mgr, q->mqd, &q->properties, minfo);
914 * check active state vs. the previous state and modify
915 * counter accordingly. map_queues_cpsch uses the
916 * dqm->active_queue_count to determine whether a new runlist must be
919 if (q->properties.is_active && !prev_active) {
920 increment_queue_count(dqm, &pdd->qpd, q);
921 } else if (!q->properties.is_active && prev_active) {
922 decrement_queue_count(dqm, &pdd->qpd, q);
923 } else if (q->gws && !q->properties.is_gws) {
924 if (q->properties.is_active) {
925 dqm->gws_queue_count++;
926 pdd->qpd.mapped_gws_queue = true;
928 q->properties.is_gws = true;
929 } else if (!q->gws && q->properties.is_gws) {
930 if (q->properties.is_active) {
931 dqm->gws_queue_count--;
932 pdd->qpd.mapped_gws_queue = false;
934 q->properties.is_gws = false;
937 if (dqm->sched_policy != KFD_SCHED_POLICY_NO_HWS) {
938 if (!dqm->dev->kfd->shared_resources.enable_mes)
939 retval = map_queues_cpsch(dqm);
940 else if (q->properties.is_active)
941 retval = add_queue_mes(dqm, q, &pdd->qpd);
942 } else if (q->properties.is_active &&
943 (q->properties.type == KFD_QUEUE_TYPE_COMPUTE ||
944 q->properties.type == KFD_QUEUE_TYPE_SDMA ||
945 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)) {
946 if (WARN(q->process->mm != current->mm,
947 "should only run in user thread"))
950 retval = mqd_mgr->load_mqd(mqd_mgr, q->mqd,
952 &q->properties, current->mm);
960 /* suspend_single_queue does not lock the dqm like the
961 * evict_process_queues_cpsch or evict_process_queues_nocpsch. You should
962 * lock the dqm before calling, and unlock after calling.
964 * The reason we don't lock the dqm is because this function may be
965 * called on multiple queues in a loop, so rather than locking/unlocking
966 * multiple times, we will just keep the dqm locked for all of the calls.
968 static int suspend_single_queue(struct device_queue_manager *dqm,
969 struct kfd_process_device *pdd,
974 if (q->properties.is_suspended)
977 pr_debug("Suspending PASID %u queue [%i]\n",
979 q->properties.queue_id);
981 is_new = q->properties.exception_status & KFD_EC_MASK(EC_QUEUE_NEW);
983 if (is_new || q->properties.is_being_destroyed) {
984 pr_debug("Suspend: skip %s queue id %i\n",
985 is_new ? "new" : "destroyed",
986 q->properties.queue_id);
990 q->properties.is_suspended = true;
991 if (q->properties.is_active) {
992 if (dqm->dev->kfd->shared_resources.enable_mes) {
993 int r = remove_queue_mes(dqm, q, &pdd->qpd);
999 decrement_queue_count(dqm, &pdd->qpd, q);
1000 q->properties.is_active = false;
1006 /* resume_single_queue does not lock the dqm like the functions
1007 * restore_process_queues_cpsch or restore_process_queues_nocpsch. You should
1008 * lock the dqm before calling, and unlock after calling.
1010 * The reason we don't lock the dqm is because this function may be
1011 * called on multiple queues in a loop, so rather than locking/unlocking
1012 * multiple times, we will just keep the dqm locked for all of the calls.
1014 static int resume_single_queue(struct device_queue_manager *dqm,
1015 struct qcm_process_device *qpd,
1018 struct kfd_process_device *pdd;
1020 if (!q->properties.is_suspended)
1023 pdd = qpd_to_pdd(qpd);
1025 pr_debug("Restoring from suspend PASID %u queue [%i]\n",
1026 pdd->process->pasid,
1027 q->properties.queue_id);
1029 q->properties.is_suspended = false;
1031 if (QUEUE_IS_ACTIVE(q->properties)) {
1032 if (dqm->dev->kfd->shared_resources.enable_mes) {
1033 int r = add_queue_mes(dqm, q, &pdd->qpd);
1039 q->properties.is_active = true;
1040 increment_queue_count(dqm, qpd, q);
1046 static int evict_process_queues_nocpsch(struct device_queue_manager *dqm,
1047 struct qcm_process_device *qpd)
1050 struct mqd_manager *mqd_mgr;
1051 struct kfd_process_device *pdd;
1052 int retval, ret = 0;
1055 if (qpd->evicted++ > 0) /* already evicted, do nothing */
1058 pdd = qpd_to_pdd(qpd);
1059 pr_debug_ratelimited("Evicting PASID 0x%x queues\n",
1060 pdd->process->pasid);
1062 pdd->last_evict_timestamp = get_jiffies_64();
1063 /* Mark all queues as evicted. Deactivate all active queues on
1066 list_for_each_entry(q, &qpd->queues_list, list) {
1067 q->properties.is_evicted = true;
1068 if (!q->properties.is_active)
1071 mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type(
1072 q->properties.type)];
1073 q->properties.is_active = false;
1074 decrement_queue_count(dqm, qpd, q);
1076 if (WARN_ONCE(!dqm->sched_running, "Evict when stopped\n"))
1079 retval = mqd_mgr->destroy_mqd(mqd_mgr, q->mqd,
1080 (dqm->dev->kfd->cwsr_enabled ?
1081 KFD_PREEMPT_TYPE_WAVEFRONT_SAVE :
1082 KFD_PREEMPT_TYPE_WAVEFRONT_DRAIN),
1083 KFD_UNMAP_LATENCY_MS, q->pipe, q->queue);
1085 /* Return the first error, but keep going to
1086 * maintain a consistent eviction state
1096 static int evict_process_queues_cpsch(struct device_queue_manager *dqm,
1097 struct qcm_process_device *qpd)
1100 struct device *dev = dqm->dev->adev->dev;
1101 struct kfd_process_device *pdd;
1105 if (qpd->evicted++ > 0) /* already evicted, do nothing */
1108 pdd = qpd_to_pdd(qpd);
1110 /* The debugger creates processes that temporarily have not acquired
1111 * all VMs for all devices and has no VMs itself.
1112 * Skip queue eviction on process eviction.
1117 pr_debug_ratelimited("Evicting PASID 0x%x queues\n",
1118 pdd->process->pasid);
1120 /* Mark all queues as evicted. Deactivate all active queues on
1123 list_for_each_entry(q, &qpd->queues_list, list) {
1124 q->properties.is_evicted = true;
1125 if (!q->properties.is_active)
1128 q->properties.is_active = false;
1129 decrement_queue_count(dqm, qpd, q);
1131 if (dqm->dev->kfd->shared_resources.enable_mes) {
1132 retval = remove_queue_mes(dqm, q, qpd);
1134 dev_err(dev, "Failed to evict queue %d\n",
1135 q->properties.queue_id);
1140 pdd->last_evict_timestamp = get_jiffies_64();
1141 if (!dqm->dev->kfd->shared_resources.enable_mes)
1142 retval = execute_queues_cpsch(dqm,
1144 KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES :
1145 KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 0,
1146 USE_DEFAULT_GRACE_PERIOD);
1153 static int restore_process_queues_nocpsch(struct device_queue_manager *dqm,
1154 struct qcm_process_device *qpd)
1156 struct mm_struct *mm = NULL;
1158 struct mqd_manager *mqd_mgr;
1159 struct kfd_process_device *pdd;
1161 uint64_t eviction_duration;
1162 int retval, ret = 0;
1164 pdd = qpd_to_pdd(qpd);
1165 /* Retrieve PD base */
1166 pd_base = amdgpu_amdkfd_gpuvm_get_process_page_dir(pdd->drm_priv);
1169 if (WARN_ON_ONCE(!qpd->evicted)) /* already restored, do nothing */
1171 if (qpd->evicted > 1) { /* ref count still > 0, decrement & quit */
1176 pr_debug_ratelimited("Restoring PASID 0x%x queues\n",
1177 pdd->process->pasid);
1179 /* Update PD Base in QPD */
1180 qpd->page_table_base = pd_base;
1181 pr_debug("Updated PD address to 0x%llx\n", pd_base);
1183 if (!list_empty(&qpd->queues_list)) {
1184 dqm->dev->kfd2kgd->set_vm_context_page_table_base(
1187 qpd->page_table_base);
1188 kfd_flush_tlb(pdd, TLB_FLUSH_LEGACY);
1191 /* Take a safe reference to the mm_struct, which may otherwise
1192 * disappear even while the kfd_process is still referenced.
1194 mm = get_task_mm(pdd->process->lead_thread);
1200 /* Remove the eviction flags. Activate queues that are not
1201 * inactive for other reasons.
1203 list_for_each_entry(q, &qpd->queues_list, list) {
1204 q->properties.is_evicted = false;
1205 if (!QUEUE_IS_ACTIVE(q->properties))
1208 mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type(
1209 q->properties.type)];
1210 q->properties.is_active = true;
1211 increment_queue_count(dqm, qpd, q);
1213 if (WARN_ONCE(!dqm->sched_running, "Restore when stopped\n"))
1216 retval = mqd_mgr->load_mqd(mqd_mgr, q->mqd, q->pipe,
1217 q->queue, &q->properties, mm);
1219 /* Return the first error, but keep going to
1220 * maintain a consistent eviction state
1225 eviction_duration = get_jiffies_64() - pdd->last_evict_timestamp;
1226 atomic64_add(eviction_duration, &pdd->evict_duration_counter);
1234 static int restore_process_queues_cpsch(struct device_queue_manager *dqm,
1235 struct qcm_process_device *qpd)
1238 struct device *dev = dqm->dev->adev->dev;
1239 struct kfd_process_device *pdd;
1240 uint64_t eviction_duration;
1243 pdd = qpd_to_pdd(qpd);
1246 if (WARN_ON_ONCE(!qpd->evicted)) /* already restored, do nothing */
1248 if (qpd->evicted > 1) { /* ref count still > 0, decrement & quit */
1253 /* The debugger creates processes that temporarily have not acquired
1254 * all VMs for all devices and has no VMs itself.
1255 * Skip queue restore on process restore.
1258 goto vm_not_acquired;
1260 pr_debug_ratelimited("Restoring PASID 0x%x queues\n",
1261 pdd->process->pasid);
1263 /* Update PD Base in QPD */
1264 qpd->page_table_base = amdgpu_amdkfd_gpuvm_get_process_page_dir(pdd->drm_priv);
1265 pr_debug("Updated PD address to 0x%llx\n", qpd->page_table_base);
1267 /* activate all active queues on the qpd */
1268 list_for_each_entry(q, &qpd->queues_list, list) {
1269 q->properties.is_evicted = false;
1270 if (!QUEUE_IS_ACTIVE(q->properties))
1273 q->properties.is_active = true;
1274 increment_queue_count(dqm, &pdd->qpd, q);
1276 if (dqm->dev->kfd->shared_resources.enable_mes) {
1277 retval = add_queue_mes(dqm, q, qpd);
1279 dev_err(dev, "Failed to restore queue %d\n",
1280 q->properties.queue_id);
1285 if (!dqm->dev->kfd->shared_resources.enable_mes)
1286 retval = execute_queues_cpsch(dqm,
1287 KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 0, USE_DEFAULT_GRACE_PERIOD);
1288 eviction_duration = get_jiffies_64() - pdd->last_evict_timestamp;
1289 atomic64_add(eviction_duration, &pdd->evict_duration_counter);
1297 static int register_process(struct device_queue_manager *dqm,
1298 struct qcm_process_device *qpd)
1300 struct device_process_node *n;
1301 struct kfd_process_device *pdd;
1305 n = kzalloc(sizeof(*n), GFP_KERNEL);
1311 pdd = qpd_to_pdd(qpd);
1312 /* Retrieve PD base */
1313 pd_base = amdgpu_amdkfd_gpuvm_get_process_page_dir(pdd->drm_priv);
1316 list_add(&n->list, &dqm->queues);
1318 /* Update PD Base in QPD */
1319 qpd->page_table_base = pd_base;
1320 pr_debug("Updated PD address to 0x%llx\n", pd_base);
1322 retval = dqm->asic_ops.update_qpd(dqm, qpd);
1324 dqm->processes_count++;
1328 /* Outside the DQM lock because under the DQM lock we can't do
1329 * reclaim or take other locks that others hold while reclaiming.
1331 kfd_inc_compute_active(dqm->dev);
1336 static int unregister_process(struct device_queue_manager *dqm,
1337 struct qcm_process_device *qpd)
1340 struct device_process_node *cur, *next;
1342 pr_debug("qpd->queues_list is %s\n",
1343 list_empty(&qpd->queues_list) ? "empty" : "not empty");
1348 list_for_each_entry_safe(cur, next, &dqm->queues, list) {
1349 if (qpd == cur->qpd) {
1350 list_del(&cur->list);
1352 dqm->processes_count--;
1356 /* qpd not found in dqm list */
1361 /* Outside the DQM lock because under the DQM lock we can't do
1362 * reclaim or take other locks that others hold while reclaiming.
1365 kfd_dec_compute_active(dqm->dev);
1371 set_pasid_vmid_mapping(struct device_queue_manager *dqm, u32 pasid,
1374 uint32_t xcc_mask = dqm->dev->xcc_mask;
1377 for_each_inst(xcc_id, xcc_mask) {
1378 ret = dqm->dev->kfd2kgd->set_pasid_vmid_mapping(
1379 dqm->dev->adev, pasid, vmid, xcc_id);
1387 static void init_interrupts(struct device_queue_manager *dqm)
1389 uint32_t xcc_mask = dqm->dev->xcc_mask;
1390 unsigned int i, xcc_id;
1392 for_each_inst(xcc_id, xcc_mask) {
1393 for (i = 0 ; i < get_pipes_per_mec(dqm) ; i++) {
1394 if (is_pipe_enabled(dqm, 0, i)) {
1395 dqm->dev->kfd2kgd->init_interrupts(
1396 dqm->dev->adev, i, xcc_id);
1402 static int initialize_nocpsch(struct device_queue_manager *dqm)
1406 pr_debug("num of pipes: %d\n", get_pipes_per_mec(dqm));
1408 dqm->allocated_queues = kcalloc(get_pipes_per_mec(dqm),
1409 sizeof(unsigned int), GFP_KERNEL);
1410 if (!dqm->allocated_queues)
1413 mutex_init(&dqm->lock_hidden);
1414 INIT_LIST_HEAD(&dqm->queues);
1415 dqm->active_queue_count = dqm->next_pipe_to_allocate = 0;
1416 dqm->active_cp_queue_count = 0;
1417 dqm->gws_queue_count = 0;
1419 for (pipe = 0; pipe < get_pipes_per_mec(dqm); pipe++) {
1420 int pipe_offset = pipe * get_queues_per_pipe(dqm);
1422 for (queue = 0; queue < get_queues_per_pipe(dqm); queue++)
1423 if (test_bit(pipe_offset + queue,
1424 dqm->dev->kfd->shared_resources.cp_queue_bitmap))
1425 dqm->allocated_queues[pipe] |= 1 << queue;
1428 memset(dqm->vmid_pasid, 0, sizeof(dqm->vmid_pasid));
1430 init_sdma_bitmaps(dqm);
1435 static void uninitialize(struct device_queue_manager *dqm)
1439 WARN_ON(dqm->active_queue_count > 0 || dqm->processes_count > 0);
1441 kfree(dqm->allocated_queues);
1442 for (i = 0 ; i < KFD_MQD_TYPE_MAX ; i++)
1443 kfree(dqm->mqd_mgrs[i]);
1444 mutex_destroy(&dqm->lock_hidden);
1447 static int start_nocpsch(struct device_queue_manager *dqm)
1451 pr_info("SW scheduler is used");
1452 init_interrupts(dqm);
1454 if (dqm->dev->adev->asic_type == CHIP_HAWAII)
1455 r = pm_init(&dqm->packet_mgr, dqm);
1457 dqm->sched_running = true;
1462 static int stop_nocpsch(struct device_queue_manager *dqm)
1465 if (!dqm->sched_running) {
1470 if (dqm->dev->adev->asic_type == CHIP_HAWAII)
1471 pm_uninit(&dqm->packet_mgr, false);
1472 dqm->sched_running = false;
1478 static void pre_reset(struct device_queue_manager *dqm)
1481 dqm->is_resetting = true;
1485 static int allocate_sdma_queue(struct device_queue_manager *dqm,
1486 struct queue *q, const uint32_t *restore_sdma_id)
1488 struct device *dev = dqm->dev->adev->dev;
1491 if (q->properties.type == KFD_QUEUE_TYPE_SDMA) {
1492 if (bitmap_empty(dqm->sdma_bitmap, KFD_MAX_SDMA_QUEUES)) {
1493 dev_err(dev, "No more SDMA queue to allocate\n");
1497 if (restore_sdma_id) {
1498 /* Re-use existing sdma_id */
1499 if (!test_bit(*restore_sdma_id, dqm->sdma_bitmap)) {
1500 dev_err(dev, "SDMA queue already in use\n");
1503 clear_bit(*restore_sdma_id, dqm->sdma_bitmap);
1504 q->sdma_id = *restore_sdma_id;
1506 /* Find first available sdma_id */
1507 bit = find_first_bit(dqm->sdma_bitmap,
1508 get_num_sdma_queues(dqm));
1509 clear_bit(bit, dqm->sdma_bitmap);
1513 q->properties.sdma_engine_id =
1514 q->sdma_id % kfd_get_num_sdma_engines(dqm->dev);
1515 q->properties.sdma_queue_id = q->sdma_id /
1516 kfd_get_num_sdma_engines(dqm->dev);
1517 } else if (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) {
1518 if (bitmap_empty(dqm->xgmi_sdma_bitmap, KFD_MAX_SDMA_QUEUES)) {
1519 dev_err(dev, "No more XGMI SDMA queue to allocate\n");
1522 if (restore_sdma_id) {
1523 /* Re-use existing sdma_id */
1524 if (!test_bit(*restore_sdma_id, dqm->xgmi_sdma_bitmap)) {
1525 dev_err(dev, "SDMA queue already in use\n");
1528 clear_bit(*restore_sdma_id, dqm->xgmi_sdma_bitmap);
1529 q->sdma_id = *restore_sdma_id;
1531 bit = find_first_bit(dqm->xgmi_sdma_bitmap,
1532 get_num_xgmi_sdma_queues(dqm));
1533 clear_bit(bit, dqm->xgmi_sdma_bitmap);
1536 /* sdma_engine_id is sdma id including
1537 * both PCIe-optimized SDMAs and XGMI-
1538 * optimized SDMAs. The calculation below
1539 * assumes the first N engines are always
1540 * PCIe-optimized ones
1542 q->properties.sdma_engine_id =
1543 kfd_get_num_sdma_engines(dqm->dev) +
1544 q->sdma_id % kfd_get_num_xgmi_sdma_engines(dqm->dev);
1545 q->properties.sdma_queue_id = q->sdma_id /
1546 kfd_get_num_xgmi_sdma_engines(dqm->dev);
1549 pr_debug("SDMA engine id: %d\n", q->properties.sdma_engine_id);
1550 pr_debug("SDMA queue id: %d\n", q->properties.sdma_queue_id);
1555 static void deallocate_sdma_queue(struct device_queue_manager *dqm,
1558 if (q->properties.type == KFD_QUEUE_TYPE_SDMA) {
1559 if (q->sdma_id >= get_num_sdma_queues(dqm))
1561 set_bit(q->sdma_id, dqm->sdma_bitmap);
1562 } else if (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) {
1563 if (q->sdma_id >= get_num_xgmi_sdma_queues(dqm))
1565 set_bit(q->sdma_id, dqm->xgmi_sdma_bitmap);
1570 * Device Queue Manager implementation for cp scheduler
1573 static int set_sched_resources(struct device_queue_manager *dqm)
1576 struct scheduling_resources res;
1577 struct device *dev = dqm->dev->adev->dev;
1579 res.vmid_mask = dqm->dev->compute_vmid_bitmap;
1582 for (i = 0; i < AMDGPU_MAX_QUEUES; ++i) {
1583 mec = (i / dqm->dev->kfd->shared_resources.num_queue_per_pipe)
1584 / dqm->dev->kfd->shared_resources.num_pipe_per_mec;
1586 if (!test_bit(i, dqm->dev->kfd->shared_resources.cp_queue_bitmap))
1589 /* only acquire queues from the first MEC */
1593 /* This situation may be hit in the future if a new HW
1594 * generation exposes more than 64 queues. If so, the
1595 * definition of res.queue_mask needs updating
1597 if (WARN_ON(i >= (sizeof(res.queue_mask)*8))) {
1598 dev_err(dev, "Invalid queue enabled by amdgpu: %d\n", i);
1602 res.queue_mask |= 1ull
1603 << amdgpu_queue_mask_bit_to_set_resource_bit(
1606 res.gws_mask = ~0ull;
1607 res.oac_mask = res.gds_heap_base = res.gds_heap_size = 0;
1609 pr_debug("Scheduling resources:\n"
1610 "vmid mask: 0x%8X\n"
1611 "queue mask: 0x%8llX\n",
1612 res.vmid_mask, res.queue_mask);
1614 return pm_send_set_resources(&dqm->packet_mgr, &res);
1617 static int initialize_cpsch(struct device_queue_manager *dqm)
1619 pr_debug("num of pipes: %d\n", get_pipes_per_mec(dqm));
1621 mutex_init(&dqm->lock_hidden);
1622 INIT_LIST_HEAD(&dqm->queues);
1623 dqm->active_queue_count = dqm->processes_count = 0;
1624 dqm->active_cp_queue_count = 0;
1625 dqm->gws_queue_count = 0;
1626 dqm->active_runlist = false;
1627 INIT_WORK(&dqm->hw_exception_work, kfd_process_hw_exception);
1628 dqm->trap_debug_vmid = 0;
1630 init_sdma_bitmaps(dqm);
1632 if (dqm->dev->kfd2kgd->get_iq_wait_times)
1633 dqm->dev->kfd2kgd->get_iq_wait_times(dqm->dev->adev,
1635 ffs(dqm->dev->xcc_mask) - 1);
1639 static int start_cpsch(struct device_queue_manager *dqm)
1641 struct device *dev = dqm->dev->adev->dev;
1648 if (!dqm->dev->kfd->shared_resources.enable_mes) {
1649 retval = pm_init(&dqm->packet_mgr, dqm);
1651 goto fail_packet_manager_init;
1653 retval = set_sched_resources(dqm);
1655 goto fail_set_sched_resources;
1657 pr_debug("Allocating fence memory\n");
1659 /* allocate fence memory on the gart */
1660 retval = kfd_gtt_sa_allocate(dqm->dev, sizeof(*dqm->fence_addr),
1664 goto fail_allocate_vidmem;
1666 dqm->fence_addr = (uint64_t *)dqm->fence_mem->cpu_ptr;
1667 dqm->fence_gpu_addr = dqm->fence_mem->gpu_addr;
1669 init_interrupts(dqm);
1671 /* clear hang status when driver try to start the hw scheduler */
1672 dqm->is_hws_hang = false;
1673 dqm->is_resetting = false;
1674 dqm->sched_running = true;
1676 if (!dqm->dev->kfd->shared_resources.enable_mes)
1677 execute_queues_cpsch(dqm, KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 0, USE_DEFAULT_GRACE_PERIOD);
1679 /* Set CWSR grace period to 1x1000 cycle for GFX9.4.3 APU */
1680 if (amdgpu_emu_mode == 0 && dqm->dev->adev->gmc.is_app_apu &&
1681 (KFD_GC_VERSION(dqm->dev) == IP_VERSION(9, 4, 3))) {
1682 uint32_t reg_offset = 0;
1683 uint32_t grace_period = 1;
1685 retval = pm_update_grace_period(&dqm->packet_mgr,
1688 dev_err(dev, "Setting grace timeout failed\n");
1689 else if (dqm->dev->kfd2kgd->build_grace_period_packet_info)
1690 /* Update dqm->wait_times maintained in software */
1691 dqm->dev->kfd2kgd->build_grace_period_packet_info(
1692 dqm->dev->adev, dqm->wait_times,
1693 grace_period, ®_offset,
1700 fail_allocate_vidmem:
1701 fail_set_sched_resources:
1702 if (!dqm->dev->kfd->shared_resources.enable_mes)
1703 pm_uninit(&dqm->packet_mgr, false);
1704 fail_packet_manager_init:
1709 static int stop_cpsch(struct device_queue_manager *dqm)
1714 if (!dqm->sched_running) {
1719 if (!dqm->is_hws_hang) {
1720 if (!dqm->dev->kfd->shared_resources.enable_mes)
1721 unmap_queues_cpsch(dqm, KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES, 0, USE_DEFAULT_GRACE_PERIOD, false);
1723 remove_all_queues_mes(dqm);
1726 hanging = dqm->is_hws_hang || dqm->is_resetting;
1727 dqm->sched_running = false;
1729 if (!dqm->dev->kfd->shared_resources.enable_mes)
1730 pm_release_ib(&dqm->packet_mgr);
1732 kfd_gtt_sa_free(dqm->dev, dqm->fence_mem);
1733 if (!dqm->dev->kfd->shared_resources.enable_mes)
1734 pm_uninit(&dqm->packet_mgr, hanging);
1740 static int create_kernel_queue_cpsch(struct device_queue_manager *dqm,
1741 struct kernel_queue *kq,
1742 struct qcm_process_device *qpd)
1745 if (dqm->total_queue_count >= max_num_of_queues_per_device) {
1746 pr_warn("Can't create new kernel queue because %d queues were already created\n",
1747 dqm->total_queue_count);
1753 * Unconditionally increment this counter, regardless of the queue's
1754 * type or whether the queue is active.
1756 dqm->total_queue_count++;
1757 pr_debug("Total of %d queues are accountable so far\n",
1758 dqm->total_queue_count);
1760 list_add(&kq->list, &qpd->priv_queue_list);
1761 increment_queue_count(dqm, qpd, kq->queue);
1762 qpd->is_debug = true;
1763 execute_queues_cpsch(dqm, KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 0,
1764 USE_DEFAULT_GRACE_PERIOD);
1770 static void destroy_kernel_queue_cpsch(struct device_queue_manager *dqm,
1771 struct kernel_queue *kq,
1772 struct qcm_process_device *qpd)
1775 list_del(&kq->list);
1776 decrement_queue_count(dqm, qpd, kq->queue);
1777 qpd->is_debug = false;
1778 execute_queues_cpsch(dqm, KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES, 0,
1779 USE_DEFAULT_GRACE_PERIOD);
1781 * Unconditionally decrement this counter, regardless of the queue's
1784 dqm->total_queue_count--;
1785 pr_debug("Total of %d queues are accountable so far\n",
1786 dqm->total_queue_count);
1790 static int create_queue_cpsch(struct device_queue_manager *dqm, struct queue *q,
1791 struct qcm_process_device *qpd,
1792 const struct kfd_criu_queue_priv_data *qd,
1793 const void *restore_mqd, const void *restore_ctl_stack)
1796 struct mqd_manager *mqd_mgr;
1798 if (dqm->total_queue_count >= max_num_of_queues_per_device) {
1799 pr_warn("Can't create new usermode queue because %d queues were already created\n",
1800 dqm->total_queue_count);
1805 if (q->properties.type == KFD_QUEUE_TYPE_SDMA ||
1806 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) {
1808 retval = allocate_sdma_queue(dqm, q, qd ? &qd->sdma_id : NULL);
1814 retval = allocate_doorbell(qpd, q, qd ? &qd->doorbell_id : NULL);
1816 goto out_deallocate_sdma_queue;
1818 mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type(
1819 q->properties.type)];
1821 if (q->properties.type == KFD_QUEUE_TYPE_SDMA ||
1822 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)
1823 dqm->asic_ops.init_sdma_vm(dqm, q, qpd);
1824 q->properties.tba_addr = qpd->tba_addr;
1825 q->properties.tma_addr = qpd->tma_addr;
1826 q->mqd_mem_obj = mqd_mgr->allocate_mqd(mqd_mgr->dev, &q->properties);
1827 if (!q->mqd_mem_obj) {
1829 goto out_deallocate_doorbell;
1834 * Eviction state logic: mark all queues as evicted, even ones
1835 * not currently active. Restoring inactive queues later only
1836 * updates the is_evicted flag but is a no-op otherwise.
1838 q->properties.is_evicted = !!qpd->evicted;
1839 q->properties.is_dbg_wa = qpd->pqm->process->debug_trap_enabled &&
1840 kfd_dbg_has_cwsr_workaround(q->device);
1843 mqd_mgr->restore_mqd(mqd_mgr, &q->mqd, q->mqd_mem_obj, &q->gart_mqd_addr,
1844 &q->properties, restore_mqd, restore_ctl_stack,
1845 qd->ctl_stack_size);
1847 mqd_mgr->init_mqd(mqd_mgr, &q->mqd, q->mqd_mem_obj,
1848 &q->gart_mqd_addr, &q->properties);
1850 list_add(&q->list, &qpd->queues_list);
1853 if (q->properties.is_active) {
1854 increment_queue_count(dqm, qpd, q);
1856 if (!dqm->dev->kfd->shared_resources.enable_mes)
1857 retval = execute_queues_cpsch(dqm,
1858 KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 0, USE_DEFAULT_GRACE_PERIOD);
1860 retval = add_queue_mes(dqm, q, qpd);
1866 * Unconditionally increment this counter, regardless of the queue's
1867 * type or whether the queue is active.
1869 dqm->total_queue_count++;
1871 pr_debug("Total of %d queues are accountable so far\n",
1872 dqm->total_queue_count);
1880 if (q->properties.is_active)
1881 decrement_queue_count(dqm, qpd, q);
1882 mqd_mgr->free_mqd(mqd_mgr, q->mqd, q->mqd_mem_obj);
1884 out_deallocate_doorbell:
1885 deallocate_doorbell(qpd, q);
1886 out_deallocate_sdma_queue:
1887 if (q->properties.type == KFD_QUEUE_TYPE_SDMA ||
1888 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) {
1890 deallocate_sdma_queue(dqm, q);
1897 int amdkfd_fence_wait_timeout(struct device_queue_manager *dqm,
1898 uint64_t fence_value,
1899 unsigned int timeout_ms)
1901 unsigned long end_jiffies = msecs_to_jiffies(timeout_ms) + jiffies;
1902 struct device *dev = dqm->dev->adev->dev;
1903 uint64_t *fence_addr = dqm->fence_addr;
1905 while (*fence_addr != fence_value) {
1906 if (time_after(jiffies, end_jiffies)) {
1907 dev_err(dev, "qcm fence wait loop timeout expired\n");
1908 /* In HWS case, this is used to halt the driver thread
1909 * in order not to mess up CP states before doing
1910 * scandumps for FW debugging.
1912 while (halt_if_hws_hang)
1923 /* dqm->lock mutex has to be locked before calling this function */
1924 static int map_queues_cpsch(struct device_queue_manager *dqm)
1926 struct device *dev = dqm->dev->adev->dev;
1929 if (!dqm->sched_running)
1931 if (dqm->active_queue_count <= 0 || dqm->processes_count <= 0)
1933 if (dqm->active_runlist)
1936 retval = pm_send_runlist(&dqm->packet_mgr, &dqm->queues);
1937 pr_debug("%s sent runlist\n", __func__);
1939 dev_err(dev, "failed to execute runlist\n");
1942 dqm->active_runlist = true;
1947 /* dqm->lock mutex has to be locked before calling this function */
1948 static int unmap_queues_cpsch(struct device_queue_manager *dqm,
1949 enum kfd_unmap_queues_filter filter,
1950 uint32_t filter_param,
1951 uint32_t grace_period,
1954 struct device *dev = dqm->dev->adev->dev;
1955 struct mqd_manager *mqd_mgr;
1958 if (!dqm->sched_running)
1960 if (dqm->is_hws_hang || dqm->is_resetting)
1962 if (!dqm->active_runlist)
1965 if (grace_period != USE_DEFAULT_GRACE_PERIOD) {
1966 retval = pm_update_grace_period(&dqm->packet_mgr, grace_period);
1971 retval = pm_send_unmap_queue(&dqm->packet_mgr, filter, filter_param, reset);
1975 *dqm->fence_addr = KFD_FENCE_INIT;
1976 pm_send_query_status(&dqm->packet_mgr, dqm->fence_gpu_addr,
1977 KFD_FENCE_COMPLETED);
1978 /* should be timed out */
1979 retval = amdkfd_fence_wait_timeout(dqm, KFD_FENCE_COMPLETED,
1980 queue_preemption_timeout_ms);
1982 dev_err(dev, "The cp might be in an unrecoverable state due to an unsuccessful queues preemption\n");
1987 /* In the current MEC firmware implementation, if compute queue
1988 * doesn't response to the preemption request in time, HIQ will
1989 * abandon the unmap request without returning any timeout error
1990 * to driver. Instead, MEC firmware will log the doorbell of the
1991 * unresponding compute queue to HIQ.MQD.queue_doorbell_id fields.
1992 * To make sure the queue unmap was successful, driver need to
1993 * check those fields
1995 mqd_mgr = dqm->mqd_mgrs[KFD_MQD_TYPE_HIQ];
1996 if (mqd_mgr->read_doorbell_id(dqm->packet_mgr.priv_queue->queue->mqd)) {
1997 dev_err(dev, "HIQ MQD's queue_doorbell_id0 is not 0, Queue preemption time out\n");
1998 while (halt_if_hws_hang)
2003 /* We need to reset the grace period value for this device */
2004 if (grace_period != USE_DEFAULT_GRACE_PERIOD) {
2005 if (pm_update_grace_period(&dqm->packet_mgr,
2006 USE_DEFAULT_GRACE_PERIOD))
2007 dev_err(dev, "Failed to reset grace period\n");
2010 pm_release_ib(&dqm->packet_mgr);
2011 dqm->active_runlist = false;
2016 /* only for compute queue */
2017 static int reset_queues_cpsch(struct device_queue_manager *dqm,
2024 retval = unmap_queues_cpsch(dqm, KFD_UNMAP_QUEUES_FILTER_BY_PASID,
2025 pasid, USE_DEFAULT_GRACE_PERIOD, true);
2031 /* dqm->lock mutex has to be locked before calling this function */
2032 static int execute_queues_cpsch(struct device_queue_manager *dqm,
2033 enum kfd_unmap_queues_filter filter,
2034 uint32_t filter_param,
2035 uint32_t grace_period)
2039 if (dqm->is_hws_hang)
2041 retval = unmap_queues_cpsch(dqm, filter, filter_param, grace_period, false);
2045 return map_queues_cpsch(dqm);
2048 static int wait_on_destroy_queue(struct device_queue_manager *dqm,
2051 struct kfd_process_device *pdd = kfd_get_process_device_data(q->device,
2055 if (pdd->qpd.is_debug)
2058 q->properties.is_being_destroyed = true;
2060 if (pdd->process->debug_trap_enabled && q->properties.is_suspended) {
2062 mutex_unlock(&q->process->mutex);
2063 ret = wait_event_interruptible(dqm->destroy_wait,
2064 !q->properties.is_suspended);
2066 mutex_lock(&q->process->mutex);
2073 static int destroy_queue_cpsch(struct device_queue_manager *dqm,
2074 struct qcm_process_device *qpd,
2078 struct mqd_manager *mqd_mgr;
2079 uint64_t sdma_val = 0;
2080 struct kfd_process_device *pdd = qpd_to_pdd(qpd);
2081 struct device *dev = dqm->dev->adev->dev;
2083 /* Get the SDMA queue stats */
2084 if ((q->properties.type == KFD_QUEUE_TYPE_SDMA) ||
2085 (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)) {
2086 retval = read_sdma_queue_counter((uint64_t __user *)q->properties.read_ptr,
2089 dev_err(dev, "Failed to read SDMA queue counter for queue: %d\n",
2090 q->properties.queue_id);
2093 /* remove queue from list to prevent rescheduling after preemption */
2096 retval = wait_on_destroy_queue(dqm, q);
2103 if (qpd->is_debug) {
2105 * error, currently we do not allow to destroy a queue
2106 * of a currently debugged process
2109 goto failed_try_destroy_debugged_queue;
2113 mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type(
2114 q->properties.type)];
2116 deallocate_doorbell(qpd, q);
2118 if ((q->properties.type == KFD_QUEUE_TYPE_SDMA) ||
2119 (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)) {
2120 deallocate_sdma_queue(dqm, q);
2121 pdd->sdma_past_activity_counter += sdma_val;
2126 if (q->properties.is_active) {
2127 decrement_queue_count(dqm, qpd, q);
2128 if (!dqm->dev->kfd->shared_resources.enable_mes) {
2129 retval = execute_queues_cpsch(dqm,
2130 KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 0,
2131 USE_DEFAULT_GRACE_PERIOD);
2132 if (retval == -ETIME)
2133 qpd->reset_wavefronts = true;
2135 retval = remove_queue_mes(dqm, q, qpd);
2140 * Unconditionally decrement this counter, regardless of the queue's
2143 dqm->total_queue_count--;
2144 pr_debug("Total of %d queues are accountable so far\n",
2145 dqm->total_queue_count);
2150 * Do free_mqd and raise delete event after dqm_unlock(dqm) to avoid
2153 kfd_dbg_ev_raise(KFD_EC_MASK(EC_DEVICE_QUEUE_DELETE),
2154 qpd->pqm->process, q->device,
2155 -1, false, NULL, 0);
2157 mqd_mgr->free_mqd(mqd_mgr, q->mqd, q->mqd_mem_obj);
2161 failed_try_destroy_debugged_queue:
2168 * Low bits must be 0000/FFFF as required by HW, high bits must be 0 to
2169 * stay in user mode.
2171 #define APE1_FIXED_BITS_MASK 0xFFFF80000000FFFFULL
2172 /* APE1 limit is inclusive and 64K aligned. */
2173 #define APE1_LIMIT_ALIGNMENT 0xFFFF
2175 static bool set_cache_memory_policy(struct device_queue_manager *dqm,
2176 struct qcm_process_device *qpd,
2177 enum cache_policy default_policy,
2178 enum cache_policy alternate_policy,
2179 void __user *alternate_aperture_base,
2180 uint64_t alternate_aperture_size)
2184 if (!dqm->asic_ops.set_cache_memory_policy)
2189 if (alternate_aperture_size == 0) {
2190 /* base > limit disables APE1 */
2191 qpd->sh_mem_ape1_base = 1;
2192 qpd->sh_mem_ape1_limit = 0;
2195 * In FSA64, APE1_Base[63:0] = { 16{SH_MEM_APE1_BASE[31]},
2196 * SH_MEM_APE1_BASE[31:0], 0x0000 }
2197 * APE1_Limit[63:0] = { 16{SH_MEM_APE1_LIMIT[31]},
2198 * SH_MEM_APE1_LIMIT[31:0], 0xFFFF }
2199 * Verify that the base and size parameters can be
2200 * represented in this format and convert them.
2201 * Additionally restrict APE1 to user-mode addresses.
2204 uint64_t base = (uintptr_t)alternate_aperture_base;
2205 uint64_t limit = base + alternate_aperture_size - 1;
2207 if (limit <= base || (base & APE1_FIXED_BITS_MASK) != 0 ||
2208 (limit & APE1_FIXED_BITS_MASK) != APE1_LIMIT_ALIGNMENT) {
2213 qpd->sh_mem_ape1_base = base >> 16;
2214 qpd->sh_mem_ape1_limit = limit >> 16;
2217 retval = dqm->asic_ops.set_cache_memory_policy(
2222 alternate_aperture_base,
2223 alternate_aperture_size);
2225 if ((dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS) && (qpd->vmid != 0))
2226 program_sh_mem_settings(dqm, qpd);
2228 pr_debug("sh_mem_config: 0x%x, ape1_base: 0x%x, ape1_limit: 0x%x\n",
2229 qpd->sh_mem_config, qpd->sh_mem_ape1_base,
2230 qpd->sh_mem_ape1_limit);
2237 static int process_termination_nocpsch(struct device_queue_manager *dqm,
2238 struct qcm_process_device *qpd)
2241 struct device_process_node *cur, *next_dpn;
2247 /* Clear all user mode queues */
2248 while (!list_empty(&qpd->queues_list)) {
2249 struct mqd_manager *mqd_mgr;
2252 q = list_first_entry(&qpd->queues_list, struct queue, list);
2253 mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type(
2254 q->properties.type)];
2255 ret = destroy_queue_nocpsch_locked(dqm, qpd, q);
2259 mqd_mgr->free_mqd(mqd_mgr, q->mqd, q->mqd_mem_obj);
2263 /* Unregister process */
2264 list_for_each_entry_safe(cur, next_dpn, &dqm->queues, list) {
2265 if (qpd == cur->qpd) {
2266 list_del(&cur->list);
2268 dqm->processes_count--;
2276 /* Outside the DQM lock because under the DQM lock we can't do
2277 * reclaim or take other locks that others hold while reclaiming.
2280 kfd_dec_compute_active(dqm->dev);
2285 static int get_wave_state(struct device_queue_manager *dqm,
2287 void __user *ctl_stack,
2288 u32 *ctl_stack_used_size,
2289 u32 *save_area_used_size)
2291 struct mqd_manager *mqd_mgr;
2295 mqd_mgr = dqm->mqd_mgrs[KFD_MQD_TYPE_CP];
2297 if (q->properties.type != KFD_QUEUE_TYPE_COMPUTE ||
2298 q->properties.is_active || !q->device->kfd->cwsr_enabled ||
2299 !mqd_mgr->get_wave_state) {
2307 * get_wave_state is outside the dqm lock to prevent circular locking
2308 * and the queue should be protected against destruction by the process
2311 return mqd_mgr->get_wave_state(mqd_mgr, q->mqd, &q->properties,
2312 ctl_stack, ctl_stack_used_size, save_area_used_size);
2315 static void get_queue_checkpoint_info(struct device_queue_manager *dqm,
2316 const struct queue *q,
2318 u32 *ctl_stack_size)
2320 struct mqd_manager *mqd_mgr;
2321 enum KFD_MQD_TYPE mqd_type =
2322 get_mqd_type_from_queue_type(q->properties.type);
2325 mqd_mgr = dqm->mqd_mgrs[mqd_type];
2326 *mqd_size = mqd_mgr->mqd_size;
2327 *ctl_stack_size = 0;
2329 if (q->properties.type == KFD_QUEUE_TYPE_COMPUTE && mqd_mgr->get_checkpoint_info)
2330 mqd_mgr->get_checkpoint_info(mqd_mgr, q->mqd, ctl_stack_size);
2335 static int checkpoint_mqd(struct device_queue_manager *dqm,
2336 const struct queue *q,
2340 struct mqd_manager *mqd_mgr;
2342 enum KFD_MQD_TYPE mqd_type =
2343 get_mqd_type_from_queue_type(q->properties.type);
2347 if (q->properties.is_active || !q->device->kfd->cwsr_enabled) {
2352 mqd_mgr = dqm->mqd_mgrs[mqd_type];
2353 if (!mqd_mgr->checkpoint_mqd) {
2358 mqd_mgr->checkpoint_mqd(mqd_mgr, q->mqd, mqd, ctl_stack);
2365 static int process_termination_cpsch(struct device_queue_manager *dqm,
2366 struct qcm_process_device *qpd)
2370 struct device *dev = dqm->dev->adev->dev;
2371 struct kernel_queue *kq, *kq_next;
2372 struct mqd_manager *mqd_mgr;
2373 struct device_process_node *cur, *next_dpn;
2374 enum kfd_unmap_queues_filter filter =
2375 KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES;
2382 /* Clean all kernel queues */
2383 list_for_each_entry_safe(kq, kq_next, &qpd->priv_queue_list, list) {
2384 list_del(&kq->list);
2385 decrement_queue_count(dqm, qpd, kq->queue);
2386 qpd->is_debug = false;
2387 dqm->total_queue_count--;
2388 filter = KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES;
2391 /* Clear all user mode queues */
2392 list_for_each_entry(q, &qpd->queues_list, list) {
2393 if (q->properties.type == KFD_QUEUE_TYPE_SDMA)
2394 deallocate_sdma_queue(dqm, q);
2395 else if (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)
2396 deallocate_sdma_queue(dqm, q);
2398 if (q->properties.is_active) {
2399 decrement_queue_count(dqm, qpd, q);
2401 if (dqm->dev->kfd->shared_resources.enable_mes) {
2402 retval = remove_queue_mes(dqm, q, qpd);
2404 dev_err(dev, "Failed to remove queue %d\n",
2405 q->properties.queue_id);
2409 dqm->total_queue_count--;
2412 /* Unregister process */
2413 list_for_each_entry_safe(cur, next_dpn, &dqm->queues, list) {
2414 if (qpd == cur->qpd) {
2415 list_del(&cur->list);
2417 dqm->processes_count--;
2423 if (!dqm->dev->kfd->shared_resources.enable_mes)
2424 retval = execute_queues_cpsch(dqm, filter, 0, USE_DEFAULT_GRACE_PERIOD);
2426 if ((!dqm->is_hws_hang) && (retval || qpd->reset_wavefronts)) {
2427 pr_warn("Resetting wave fronts (cpsch) on dev %p\n", dqm->dev);
2428 dbgdev_wave_reset_wavefronts(dqm->dev, qpd->pqm->process);
2429 qpd->reset_wavefronts = false;
2432 /* Lastly, free mqd resources.
2433 * Do free_mqd() after dqm_unlock to avoid circular locking.
2435 while (!list_empty(&qpd->queues_list)) {
2436 q = list_first_entry(&qpd->queues_list, struct queue, list);
2437 mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type(
2438 q->properties.type)];
2442 mqd_mgr->free_mqd(mqd_mgr, q->mqd, q->mqd_mem_obj);
2447 /* Outside the DQM lock because under the DQM lock we can't do
2448 * reclaim or take other locks that others hold while reclaiming.
2451 kfd_dec_compute_active(dqm->dev);
2456 static int init_mqd_managers(struct device_queue_manager *dqm)
2459 struct device *dev = dqm->dev->adev->dev;
2460 struct mqd_manager *mqd_mgr;
2462 for (i = 0; i < KFD_MQD_TYPE_MAX; i++) {
2463 mqd_mgr = dqm->asic_ops.mqd_manager_init(i, dqm->dev);
2465 dev_err(dev, "mqd manager [%d] initialization failed\n", i);
2468 dqm->mqd_mgrs[i] = mqd_mgr;
2474 for (j = 0; j < i; j++) {
2475 kfree(dqm->mqd_mgrs[j]);
2476 dqm->mqd_mgrs[j] = NULL;
2482 /* Allocate one hiq mqd (HWS) and all SDMA mqd in a continuous trunk*/
2483 static int allocate_hiq_sdma_mqd(struct device_queue_manager *dqm)
2486 struct kfd_node *dev = dqm->dev;
2487 struct kfd_mem_obj *mem_obj = &dqm->hiq_sdma_mqd;
2488 uint32_t size = dqm->mqd_mgrs[KFD_MQD_TYPE_SDMA]->mqd_size *
2489 get_num_all_sdma_engines(dqm) *
2490 dev->kfd->device_info.num_sdma_queues_per_engine +
2491 (dqm->mqd_mgrs[KFD_MQD_TYPE_HIQ]->mqd_size *
2492 NUM_XCC(dqm->dev->xcc_mask));
2494 retval = amdgpu_amdkfd_alloc_gtt_mem(dev->adev, size,
2495 &(mem_obj->gtt_mem), &(mem_obj->gpu_addr),
2496 (void *)&(mem_obj->cpu_ptr), false);
2501 struct device_queue_manager *device_queue_manager_init(struct kfd_node *dev)
2503 struct device_queue_manager *dqm;
2505 pr_debug("Loading device queue manager\n");
2507 dqm = kzalloc(sizeof(*dqm), GFP_KERNEL);
2511 switch (dev->adev->asic_type) {
2512 /* HWS is not available on Hawaii. */
2514 /* HWS depends on CWSR for timely dequeue. CWSR is not
2515 * available on Tonga.
2517 * FIXME: This argument also applies to Kaveri.
2520 dqm->sched_policy = KFD_SCHED_POLICY_NO_HWS;
2523 dqm->sched_policy = sched_policy;
2528 switch (dqm->sched_policy) {
2529 case KFD_SCHED_POLICY_HWS:
2530 case KFD_SCHED_POLICY_HWS_NO_OVERSUBSCRIPTION:
2531 /* initialize dqm for cp scheduling */
2532 dqm->ops.create_queue = create_queue_cpsch;
2533 dqm->ops.initialize = initialize_cpsch;
2534 dqm->ops.start = start_cpsch;
2535 dqm->ops.stop = stop_cpsch;
2536 dqm->ops.pre_reset = pre_reset;
2537 dqm->ops.destroy_queue = destroy_queue_cpsch;
2538 dqm->ops.update_queue = update_queue;
2539 dqm->ops.register_process = register_process;
2540 dqm->ops.unregister_process = unregister_process;
2541 dqm->ops.uninitialize = uninitialize;
2542 dqm->ops.create_kernel_queue = create_kernel_queue_cpsch;
2543 dqm->ops.destroy_kernel_queue = destroy_kernel_queue_cpsch;
2544 dqm->ops.set_cache_memory_policy = set_cache_memory_policy;
2545 dqm->ops.process_termination = process_termination_cpsch;
2546 dqm->ops.evict_process_queues = evict_process_queues_cpsch;
2547 dqm->ops.restore_process_queues = restore_process_queues_cpsch;
2548 dqm->ops.get_wave_state = get_wave_state;
2549 dqm->ops.reset_queues = reset_queues_cpsch;
2550 dqm->ops.get_queue_checkpoint_info = get_queue_checkpoint_info;
2551 dqm->ops.checkpoint_mqd = checkpoint_mqd;
2553 case KFD_SCHED_POLICY_NO_HWS:
2554 /* initialize dqm for no cp scheduling */
2555 dqm->ops.start = start_nocpsch;
2556 dqm->ops.stop = stop_nocpsch;
2557 dqm->ops.pre_reset = pre_reset;
2558 dqm->ops.create_queue = create_queue_nocpsch;
2559 dqm->ops.destroy_queue = destroy_queue_nocpsch;
2560 dqm->ops.update_queue = update_queue;
2561 dqm->ops.register_process = register_process;
2562 dqm->ops.unregister_process = unregister_process;
2563 dqm->ops.initialize = initialize_nocpsch;
2564 dqm->ops.uninitialize = uninitialize;
2565 dqm->ops.set_cache_memory_policy = set_cache_memory_policy;
2566 dqm->ops.process_termination = process_termination_nocpsch;
2567 dqm->ops.evict_process_queues = evict_process_queues_nocpsch;
2568 dqm->ops.restore_process_queues =
2569 restore_process_queues_nocpsch;
2570 dqm->ops.get_wave_state = get_wave_state;
2571 dqm->ops.get_queue_checkpoint_info = get_queue_checkpoint_info;
2572 dqm->ops.checkpoint_mqd = checkpoint_mqd;
2575 dev_err(dev->adev->dev, "Invalid scheduling policy %d\n", dqm->sched_policy);
2579 switch (dev->adev->asic_type) {
2582 device_queue_manager_init_cik(&dqm->asic_ops);
2588 case CHIP_POLARIS10:
2589 case CHIP_POLARIS11:
2590 case CHIP_POLARIS12:
2592 device_queue_manager_init_vi(&dqm->asic_ops);
2596 if (KFD_GC_VERSION(dev) >= IP_VERSION(11, 0, 0))
2597 device_queue_manager_init_v11(&dqm->asic_ops);
2598 else if (KFD_GC_VERSION(dev) >= IP_VERSION(10, 1, 1))
2599 device_queue_manager_init_v10(&dqm->asic_ops);
2600 else if (KFD_GC_VERSION(dev) >= IP_VERSION(9, 0, 1))
2601 device_queue_manager_init_v9(&dqm->asic_ops);
2603 WARN(1, "Unexpected ASIC family %u",
2604 dev->adev->asic_type);
2609 if (init_mqd_managers(dqm))
2612 if (!dev->kfd->shared_resources.enable_mes && allocate_hiq_sdma_mqd(dqm)) {
2613 dev_err(dev->adev->dev, "Failed to allocate hiq sdma mqd trunk buffer\n");
2617 if (!dqm->ops.initialize(dqm)) {
2618 init_waitqueue_head(&dqm->destroy_wait);
2627 static void deallocate_hiq_sdma_mqd(struct kfd_node *dev,
2628 struct kfd_mem_obj *mqd)
2630 WARN(!mqd, "No hiq sdma mqd trunk to free");
2632 amdgpu_amdkfd_free_gtt_mem(dev->adev, mqd->gtt_mem);
2635 void device_queue_manager_uninit(struct device_queue_manager *dqm)
2638 dqm->ops.uninitialize(dqm);
2639 if (!dqm->dev->kfd->shared_resources.enable_mes)
2640 deallocate_hiq_sdma_mqd(dqm->dev, &dqm->hiq_sdma_mqd);
2644 int kfd_dqm_evict_pasid(struct device_queue_manager *dqm, u32 pasid)
2646 struct kfd_process_device *pdd;
2647 struct kfd_process *p = kfd_lookup_process_by_pasid(pasid);
2652 WARN(debug_evictions, "Evicting pid %d", p->lead_thread->pid);
2653 pdd = kfd_get_process_device_data(dqm->dev, p);
2655 ret = dqm->ops.evict_process_queues(dqm, &pdd->qpd);
2656 kfd_unref_process(p);
2661 static void kfd_process_hw_exception(struct work_struct *work)
2663 struct device_queue_manager *dqm = container_of(work,
2664 struct device_queue_manager, hw_exception_work);
2665 amdgpu_amdkfd_gpu_reset(dqm->dev->adev);
2668 int reserve_debug_trap_vmid(struct device_queue_manager *dqm,
2669 struct qcm_process_device *qpd)
2672 struct device *dev = dqm->dev->adev->dev;
2673 int updated_vmid_mask;
2675 if (dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS) {
2676 dev_err(dev, "Unsupported on sched_policy: %i\n", dqm->sched_policy);
2682 if (dqm->trap_debug_vmid != 0) {
2683 dev_err(dev, "Trap debug id already reserved\n");
2688 r = unmap_queues_cpsch(dqm, KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES, 0,
2689 USE_DEFAULT_GRACE_PERIOD, false);
2693 updated_vmid_mask = dqm->dev->kfd->shared_resources.compute_vmid_bitmap;
2694 updated_vmid_mask &= ~(1 << dqm->dev->vm_info.last_vmid_kfd);
2696 dqm->dev->kfd->shared_resources.compute_vmid_bitmap = updated_vmid_mask;
2697 dqm->trap_debug_vmid = dqm->dev->vm_info.last_vmid_kfd;
2698 r = set_sched_resources(dqm);
2702 r = map_queues_cpsch(dqm);
2706 pr_debug("Reserved VMID for trap debug: %i\n", dqm->trap_debug_vmid);
2714 * Releases vmid for the trap debugger
2716 int release_debug_trap_vmid(struct device_queue_manager *dqm,
2717 struct qcm_process_device *qpd)
2719 struct device *dev = dqm->dev->adev->dev;
2721 int updated_vmid_mask;
2722 uint32_t trap_debug_vmid;
2724 if (dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS) {
2725 dev_err(dev, "Unsupported on sched_policy: %i\n", dqm->sched_policy);
2730 trap_debug_vmid = dqm->trap_debug_vmid;
2731 if (dqm->trap_debug_vmid == 0) {
2732 dev_err(dev, "Trap debug id is not reserved\n");
2737 r = unmap_queues_cpsch(dqm, KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES, 0,
2738 USE_DEFAULT_GRACE_PERIOD, false);
2742 updated_vmid_mask = dqm->dev->kfd->shared_resources.compute_vmid_bitmap;
2743 updated_vmid_mask |= (1 << dqm->dev->vm_info.last_vmid_kfd);
2745 dqm->dev->kfd->shared_resources.compute_vmid_bitmap = updated_vmid_mask;
2746 dqm->trap_debug_vmid = 0;
2747 r = set_sched_resources(dqm);
2751 r = map_queues_cpsch(dqm);
2755 pr_debug("Released VMID for trap debug: %i\n", trap_debug_vmid);
2762 #define QUEUE_NOT_FOUND -1
2763 /* invalidate queue operation in array */
2764 static void q_array_invalidate(uint32_t num_queues, uint32_t *queue_ids)
2768 for (i = 0; i < num_queues; i++)
2769 queue_ids[i] |= KFD_DBG_QUEUE_INVALID_MASK;
2772 /* find queue index in array */
2773 static int q_array_get_index(unsigned int queue_id,
2774 uint32_t num_queues,
2775 uint32_t *queue_ids)
2779 for (i = 0; i < num_queues; i++)
2780 if (queue_id == (queue_ids[i] & ~KFD_DBG_QUEUE_INVALID_MASK))
2783 return QUEUE_NOT_FOUND;
2786 struct copy_context_work_handler_workarea {
2787 struct work_struct copy_context_work;
2788 struct kfd_process *p;
2791 static void copy_context_work_handler (struct work_struct *work)
2793 struct copy_context_work_handler_workarea *workarea;
2794 struct mqd_manager *mqd_mgr;
2796 struct mm_struct *mm;
2797 struct kfd_process *p;
2798 uint32_t tmp_ctl_stack_used_size, tmp_save_area_used_size;
2801 workarea = container_of(work,
2802 struct copy_context_work_handler_workarea,
2806 mm = get_task_mm(p->lead_thread);
2812 for (i = 0; i < p->n_pdds; i++) {
2813 struct kfd_process_device *pdd = p->pdds[i];
2814 struct device_queue_manager *dqm = pdd->dev->dqm;
2815 struct qcm_process_device *qpd = &pdd->qpd;
2817 list_for_each_entry(q, &qpd->queues_list, list) {
2818 mqd_mgr = dqm->mqd_mgrs[KFD_MQD_TYPE_CP];
2820 /* We ignore the return value from get_wave_state
2822 * i) right now, it always returns 0, and
2823 * ii) if we hit an error, we would continue to the
2824 * next queue anyway.
2826 mqd_mgr->get_wave_state(mqd_mgr,
2829 (void __user *) q->properties.ctx_save_restore_area_address,
2830 &tmp_ctl_stack_used_size,
2831 &tmp_save_area_used_size);
2834 kthread_unuse_mm(mm);
2838 static uint32_t *get_queue_ids(uint32_t num_queues, uint32_t *usr_queue_id_array)
2840 size_t array_size = num_queues * sizeof(uint32_t);
2842 if (!usr_queue_id_array)
2845 return memdup_user(usr_queue_id_array, array_size);
2848 int resume_queues(struct kfd_process *p,
2849 uint32_t num_queues,
2850 uint32_t *usr_queue_id_array)
2852 uint32_t *queue_ids = NULL;
2853 int total_resumed = 0;
2856 if (usr_queue_id_array) {
2857 queue_ids = get_queue_ids(num_queues, usr_queue_id_array);
2859 if (IS_ERR(queue_ids))
2860 return PTR_ERR(queue_ids);
2862 /* mask all queues as invalid. unmask per successful request */
2863 q_array_invalidate(num_queues, queue_ids);
2866 for (i = 0; i < p->n_pdds; i++) {
2867 struct kfd_process_device *pdd = p->pdds[i];
2868 struct device_queue_manager *dqm = pdd->dev->dqm;
2869 struct device *dev = dqm->dev->adev->dev;
2870 struct qcm_process_device *qpd = &pdd->qpd;
2872 int r, per_device_resumed = 0;
2876 /* unmask queues that resume or already resumed as valid */
2877 list_for_each_entry(q, &qpd->queues_list, list) {
2878 int q_idx = QUEUE_NOT_FOUND;
2881 q_idx = q_array_get_index(
2882 q->properties.queue_id,
2886 if (!queue_ids || q_idx != QUEUE_NOT_FOUND) {
2887 int err = resume_single_queue(dqm, &pdd->qpd, q);
2892 ~KFD_DBG_QUEUE_INVALID_MASK;
2895 KFD_DBG_QUEUE_ERROR_MASK;
2900 if (dqm->dev->kfd->shared_resources.enable_mes) {
2901 wake_up_all(&dqm->destroy_wait);
2905 per_device_resumed++;
2910 if (!per_device_resumed) {
2915 r = execute_queues_cpsch(dqm,
2916 KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES,
2918 USE_DEFAULT_GRACE_PERIOD);
2920 dev_err(dev, "Failed to resume process queues\n");
2922 list_for_each_entry(q, &qpd->queues_list, list) {
2923 int q_idx = q_array_get_index(
2924 q->properties.queue_id,
2928 /* mask queue as error on resume fail */
2929 if (q_idx != QUEUE_NOT_FOUND)
2931 KFD_DBG_QUEUE_ERROR_MASK;
2935 wake_up_all(&dqm->destroy_wait);
2936 total_resumed += per_device_resumed;
2943 if (copy_to_user((void __user *)usr_queue_id_array, queue_ids,
2944 num_queues * sizeof(uint32_t)))
2945 pr_err("copy_to_user failed on queue resume\n");
2950 return total_resumed;
2953 int suspend_queues(struct kfd_process *p,
2954 uint32_t num_queues,
2955 uint32_t grace_period,
2956 uint64_t exception_clear_mask,
2957 uint32_t *usr_queue_id_array)
2959 uint32_t *queue_ids = get_queue_ids(num_queues, usr_queue_id_array);
2960 int total_suspended = 0;
2963 if (IS_ERR(queue_ids))
2964 return PTR_ERR(queue_ids);
2966 /* mask all queues as invalid. umask on successful request */
2967 q_array_invalidate(num_queues, queue_ids);
2969 for (i = 0; i < p->n_pdds; i++) {
2970 struct kfd_process_device *pdd = p->pdds[i];
2971 struct device_queue_manager *dqm = pdd->dev->dqm;
2972 struct device *dev = dqm->dev->adev->dev;
2973 struct qcm_process_device *qpd = &pdd->qpd;
2975 int r, per_device_suspended = 0;
2977 mutex_lock(&p->event_mutex);
2980 /* unmask queues that suspend or already suspended */
2981 list_for_each_entry(q, &qpd->queues_list, list) {
2982 int q_idx = q_array_get_index(q->properties.queue_id,
2986 if (q_idx != QUEUE_NOT_FOUND) {
2987 int err = suspend_single_queue(dqm, pdd, q);
2988 bool is_mes = dqm->dev->kfd->shared_resources.enable_mes;
2991 queue_ids[q_idx] &= ~KFD_DBG_QUEUE_INVALID_MASK;
2992 if (exception_clear_mask && is_mes)
2993 q->properties.exception_status &=
2994 ~exception_clear_mask;
2999 per_device_suspended++;
3000 } else if (err != -EBUSY) {
3002 queue_ids[q_idx] |= KFD_DBG_QUEUE_ERROR_MASK;
3008 if (!per_device_suspended) {
3010 mutex_unlock(&p->event_mutex);
3011 if (total_suspended)
3012 amdgpu_amdkfd_debug_mem_fence(dqm->dev->adev);
3016 r = execute_queues_cpsch(dqm,
3017 KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 0,
3021 dev_err(dev, "Failed to suspend process queues.\n");
3023 total_suspended += per_device_suspended;
3025 list_for_each_entry(q, &qpd->queues_list, list) {
3026 int q_idx = q_array_get_index(q->properties.queue_id,
3027 num_queues, queue_ids);
3029 if (q_idx == QUEUE_NOT_FOUND)
3032 /* mask queue as error on suspend fail */
3034 queue_ids[q_idx] |= KFD_DBG_QUEUE_ERROR_MASK;
3035 else if (exception_clear_mask)
3036 q->properties.exception_status &=
3037 ~exception_clear_mask;
3041 mutex_unlock(&p->event_mutex);
3042 amdgpu_device_flush_hdp(dqm->dev->adev, NULL);
3045 if (total_suspended) {
3046 struct copy_context_work_handler_workarea copy_context_worker;
3049 ©_context_worker.copy_context_work,
3050 copy_context_work_handler);
3052 copy_context_worker.p = p;
3054 schedule_work(©_context_worker.copy_context_work);
3057 flush_work(©_context_worker.copy_context_work);
3058 destroy_work_on_stack(©_context_worker.copy_context_work);
3061 if (copy_to_user((void __user *)usr_queue_id_array, queue_ids,
3062 num_queues * sizeof(uint32_t)))
3063 pr_err("copy_to_user failed on queue suspend\n");
3067 return total_suspended;
3070 static uint32_t set_queue_type_for_user(struct queue_properties *q_props)
3072 switch (q_props->type) {
3073 case KFD_QUEUE_TYPE_COMPUTE:
3074 return q_props->format == KFD_QUEUE_FORMAT_PM4
3075 ? KFD_IOC_QUEUE_TYPE_COMPUTE
3076 : KFD_IOC_QUEUE_TYPE_COMPUTE_AQL;
3077 case KFD_QUEUE_TYPE_SDMA:
3078 return KFD_IOC_QUEUE_TYPE_SDMA;
3079 case KFD_QUEUE_TYPE_SDMA_XGMI:
3080 return KFD_IOC_QUEUE_TYPE_SDMA_XGMI;
3082 WARN_ONCE(true, "queue type not recognized!");
3087 void set_queue_snapshot_entry(struct queue *q,
3088 uint64_t exception_clear_mask,
3089 struct kfd_queue_snapshot_entry *qss_entry)
3091 qss_entry->ring_base_address = q->properties.queue_address;
3092 qss_entry->write_pointer_address = (uint64_t)q->properties.write_ptr;
3093 qss_entry->read_pointer_address = (uint64_t)q->properties.read_ptr;
3094 qss_entry->ctx_save_restore_address =
3095 q->properties.ctx_save_restore_area_address;
3096 qss_entry->ctx_save_restore_area_size =
3097 q->properties.ctx_save_restore_area_size;
3098 qss_entry->exception_status = q->properties.exception_status;
3099 qss_entry->queue_id = q->properties.queue_id;
3100 qss_entry->gpu_id = q->device->id;
3101 qss_entry->ring_size = (uint32_t)q->properties.queue_size;
3102 qss_entry->queue_type = set_queue_type_for_user(&q->properties);
3103 q->properties.exception_status &= ~exception_clear_mask;
3106 int debug_lock_and_unmap(struct device_queue_manager *dqm)
3108 struct device *dev = dqm->dev->adev->dev;
3111 if (dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS) {
3112 dev_err(dev, "Unsupported on sched_policy: %i\n", dqm->sched_policy);
3116 if (!kfd_dbg_is_per_vmid_supported(dqm->dev))
3121 r = unmap_queues_cpsch(dqm, KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES, 0, 0, false);
3128 int debug_map_and_unlock(struct device_queue_manager *dqm)
3130 struct device *dev = dqm->dev->adev->dev;
3133 if (dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS) {
3134 dev_err(dev, "Unsupported on sched_policy: %i\n", dqm->sched_policy);
3138 if (!kfd_dbg_is_per_vmid_supported(dqm->dev))
3141 r = map_queues_cpsch(dqm);
3148 int debug_refresh_runlist(struct device_queue_manager *dqm)
3150 int r = debug_lock_and_unmap(dqm);
3155 return debug_map_and_unlock(dqm);
3158 #if defined(CONFIG_DEBUG_FS)
3160 static void seq_reg_dump(struct seq_file *m,
3161 uint32_t (*dump)[2], uint32_t n_regs)
3165 for (i = 0, count = 0; i < n_regs; i++) {
3167 dump[i-1][0] + sizeof(uint32_t) != dump[i][0]) {
3168 seq_printf(m, "%s %08x: %08x",
3170 dump[i][0], dump[i][1]);
3173 seq_printf(m, " %08x", dump[i][1]);
3181 int dqm_debugfs_hqds(struct seq_file *m, void *data)
3183 struct device_queue_manager *dqm = data;
3184 uint32_t xcc_mask = dqm->dev->xcc_mask;
3185 uint32_t (*dump)[2], n_regs;
3188 uint32_t sdma_engine_start;
3190 if (!dqm->sched_running) {
3191 seq_puts(m, " Device is stopped\n");
3195 for_each_inst(xcc_id, xcc_mask) {
3196 r = dqm->dev->kfd2kgd->hqd_dump(dqm->dev->adev,
3198 KFD_CIK_HIQ_QUEUE, &dump,
3203 " Inst %d, HIQ on MEC %d Pipe %d Queue %d\n",
3205 KFD_CIK_HIQ_PIPE / get_pipes_per_mec(dqm) + 1,
3206 KFD_CIK_HIQ_PIPE % get_pipes_per_mec(dqm),
3208 seq_reg_dump(m, dump, n_regs);
3213 for (pipe = 0; pipe < get_pipes_per_mec(dqm); pipe++) {
3214 int pipe_offset = pipe * get_queues_per_pipe(dqm);
3216 for (queue = 0; queue < get_queues_per_pipe(dqm); queue++) {
3217 if (!test_bit(pipe_offset + queue,
3218 dqm->dev->kfd->shared_resources.cp_queue_bitmap))
3221 r = dqm->dev->kfd2kgd->hqd_dump(dqm->dev->adev,
3229 " Inst %d, CP Pipe %d, Queue %d\n",
3230 xcc_id, pipe, queue);
3231 seq_reg_dump(m, dump, n_regs);
3238 sdma_engine_start = dqm->dev->node_id * get_num_all_sdma_engines(dqm);
3239 for (pipe = sdma_engine_start;
3240 pipe < (sdma_engine_start + get_num_all_sdma_engines(dqm));
3243 queue < dqm->dev->kfd->device_info.num_sdma_queues_per_engine;
3245 r = dqm->dev->kfd2kgd->hqd_sdma_dump(
3246 dqm->dev->adev, pipe, queue, &dump, &n_regs);
3250 seq_printf(m, " SDMA Engine %d, RLC %d\n",
3252 seq_reg_dump(m, dump, n_regs);
3261 int dqm_debugfs_hang_hws(struct device_queue_manager *dqm)
3266 r = pm_debugfs_hang_hws(&dqm->packet_mgr);
3271 dqm->active_runlist = true;
3272 r = execute_queues_cpsch(dqm, KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES,
3273 0, USE_DEFAULT_GRACE_PERIOD);