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"
40 /* Size of the per-pipe EOP queue */
41 #define CIK_HPD_EOP_BYTES_LOG2 11
42 #define CIK_HPD_EOP_BYTES (1U << CIK_HPD_EOP_BYTES_LOG2)
44 static int set_pasid_vmid_mapping(struct device_queue_manager *dqm,
45 u32 pasid, unsigned int vmid);
47 static int execute_queues_cpsch(struct device_queue_manager *dqm,
48 enum kfd_unmap_queues_filter filter,
49 uint32_t filter_param);
50 static int unmap_queues_cpsch(struct device_queue_manager *dqm,
51 enum kfd_unmap_queues_filter filter,
52 uint32_t filter_param, bool reset);
54 static int map_queues_cpsch(struct device_queue_manager *dqm);
56 static void deallocate_sdma_queue(struct device_queue_manager *dqm,
59 static inline void deallocate_hqd(struct device_queue_manager *dqm,
61 static int allocate_hqd(struct device_queue_manager *dqm, struct queue *q);
62 static int allocate_sdma_queue(struct device_queue_manager *dqm,
63 struct queue *q, const uint32_t *restore_sdma_id);
64 static void kfd_process_hw_exception(struct work_struct *work);
67 enum KFD_MQD_TYPE get_mqd_type_from_queue_type(enum kfd_queue_type type)
69 if (type == KFD_QUEUE_TYPE_SDMA || type == KFD_QUEUE_TYPE_SDMA_XGMI)
70 return KFD_MQD_TYPE_SDMA;
71 return KFD_MQD_TYPE_CP;
74 static bool is_pipe_enabled(struct device_queue_manager *dqm, int mec, int pipe)
77 int pipe_offset = (mec * dqm->dev->shared_resources.num_pipe_per_mec
78 + pipe) * dqm->dev->shared_resources.num_queue_per_pipe;
80 /* queue is available for KFD usage if bit is 1 */
81 for (i = 0; i < dqm->dev->shared_resources.num_queue_per_pipe; ++i)
82 if (test_bit(pipe_offset + i,
83 dqm->dev->shared_resources.cp_queue_bitmap))
88 unsigned int get_cp_queues_num(struct device_queue_manager *dqm)
90 return bitmap_weight(dqm->dev->shared_resources.cp_queue_bitmap,
94 unsigned int get_queues_per_pipe(struct device_queue_manager *dqm)
96 return dqm->dev->shared_resources.num_queue_per_pipe;
99 unsigned int get_pipes_per_mec(struct device_queue_manager *dqm)
101 return dqm->dev->shared_resources.num_pipe_per_mec;
104 static unsigned int get_num_all_sdma_engines(struct device_queue_manager *dqm)
106 return kfd_get_num_sdma_engines(dqm->dev) +
107 kfd_get_num_xgmi_sdma_engines(dqm->dev);
110 unsigned int get_num_sdma_queues(struct device_queue_manager *dqm)
112 return kfd_get_num_sdma_engines(dqm->dev) *
113 dqm->dev->device_info.num_sdma_queues_per_engine;
116 unsigned int get_num_xgmi_sdma_queues(struct device_queue_manager *dqm)
118 return kfd_get_num_xgmi_sdma_engines(dqm->dev) *
119 dqm->dev->device_info.num_sdma_queues_per_engine;
122 static inline uint64_t get_reserved_sdma_queues_bitmap(struct device_queue_manager *dqm)
124 return dqm->dev->device_info.reserved_sdma_queues_bitmap;
127 void program_sh_mem_settings(struct device_queue_manager *dqm,
128 struct qcm_process_device *qpd)
130 return dqm->dev->kfd2kgd->program_sh_mem_settings(
131 dqm->dev->adev, qpd->vmid,
133 qpd->sh_mem_ape1_base,
134 qpd->sh_mem_ape1_limit,
138 static void kfd_hws_hang(struct device_queue_manager *dqm)
141 * Issue a GPU reset if HWS is unresponsive
143 dqm->is_hws_hang = true;
145 /* It's possible we're detecting a HWS hang in the
146 * middle of a GPU reset. No need to schedule another
147 * reset in this case.
149 if (!dqm->is_resetting)
150 schedule_work(&dqm->hw_exception_work);
153 static int convert_to_mes_queue_type(int queue_type)
157 switch (queue_type) {
158 case KFD_QUEUE_TYPE_COMPUTE:
159 mes_queue_type = MES_QUEUE_TYPE_COMPUTE;
161 case KFD_QUEUE_TYPE_SDMA:
162 mes_queue_type = MES_QUEUE_TYPE_SDMA;
165 WARN(1, "Invalid queue type %d", queue_type);
166 mes_queue_type = -EINVAL;
170 return mes_queue_type;
173 static int add_queue_mes(struct device_queue_manager *dqm, struct queue *q,
174 struct qcm_process_device *qpd)
176 struct amdgpu_device *adev = (struct amdgpu_device *)dqm->dev->adev;
177 struct kfd_process_device *pdd = qpd_to_pdd(qpd);
178 struct mes_add_queue_input queue_input;
181 if (dqm->is_hws_hang)
184 memset(&queue_input, 0x0, sizeof(struct mes_add_queue_input));
185 queue_input.process_id = qpd->pqm->process->pasid;
186 queue_input.page_table_base_addr = qpd->page_table_base;
187 queue_input.process_va_start = 0;
188 queue_input.process_va_end = adev->vm_manager.max_pfn - 1;
189 /* MES unit for quantum is 100ns */
190 queue_input.process_quantum = KFD_MES_PROCESS_QUANTUM; /* Equivalent to 10ms. */
191 queue_input.process_context_addr = pdd->proc_ctx_gpu_addr;
192 queue_input.gang_quantum = KFD_MES_GANG_QUANTUM; /* Equivalent to 1ms */
193 queue_input.gang_context_addr = q->gang_ctx_gpu_addr;
194 queue_input.inprocess_gang_priority = q->properties.priority;
195 queue_input.gang_global_priority_level =
196 AMDGPU_MES_PRIORITY_LEVEL_NORMAL;
197 queue_input.doorbell_offset = q->properties.doorbell_off;
198 queue_input.mqd_addr = q->gart_mqd_addr;
199 queue_input.wptr_addr = (uint64_t)q->properties.write_ptr;
200 queue_input.paging = false;
201 queue_input.tba_addr = qpd->tba_addr;
202 queue_input.tma_addr = qpd->tma_addr;
204 queue_type = convert_to_mes_queue_type(q->properties.type);
205 if (queue_type < 0) {
206 pr_err("Queue type not supported with MES, queue:%d\n",
210 queue_input.queue_type = (uint32_t)queue_type;
213 queue_input.gws_base = 0;
214 queue_input.gws_size = qpd->num_gws;
217 amdgpu_mes_lock(&adev->mes);
218 r = adev->mes.funcs->add_hw_queue(&adev->mes, &queue_input);
219 amdgpu_mes_unlock(&adev->mes);
221 pr_err("failed to add hardware queue to MES, doorbell=0x%x\n",
222 q->properties.doorbell_off);
223 pr_err("MES might be in unrecoverable state, issue a GPU reset\n");
230 static int remove_queue_mes(struct device_queue_manager *dqm, struct queue *q,
231 struct qcm_process_device *qpd)
233 struct amdgpu_device *adev = (struct amdgpu_device *)dqm->dev->adev;
235 struct mes_remove_queue_input queue_input;
237 if (dqm->is_hws_hang)
240 memset(&queue_input, 0x0, sizeof(struct mes_remove_queue_input));
241 queue_input.doorbell_offset = q->properties.doorbell_off;
242 queue_input.gang_context_addr = q->gang_ctx_gpu_addr;
244 amdgpu_mes_lock(&adev->mes);
245 r = adev->mes.funcs->remove_hw_queue(&adev->mes, &queue_input);
246 amdgpu_mes_unlock(&adev->mes);
249 pr_err("failed to remove hardware queue from MES, doorbell=0x%x\n",
250 q->properties.doorbell_off);
251 pr_err("MES might be in unrecoverable state, issue a GPU reset\n");
258 static int remove_all_queues_mes(struct device_queue_manager *dqm)
260 struct device_process_node *cur;
261 struct qcm_process_device *qpd;
265 list_for_each_entry(cur, &dqm->queues, list) {
267 list_for_each_entry(q, &qpd->queues_list, list) {
268 if (q->properties.is_active) {
269 retval = remove_queue_mes(dqm, q, qpd);
271 pr_err("%s: Failed to remove queue %d for dev %d",
273 q->properties.queue_id,
284 static void increment_queue_count(struct device_queue_manager *dqm,
285 struct qcm_process_device *qpd,
288 dqm->active_queue_count++;
289 if (q->properties.type == KFD_QUEUE_TYPE_COMPUTE ||
290 q->properties.type == KFD_QUEUE_TYPE_DIQ)
291 dqm->active_cp_queue_count++;
293 if (q->properties.is_gws) {
294 dqm->gws_queue_count++;
295 qpd->mapped_gws_queue = true;
299 static void decrement_queue_count(struct device_queue_manager *dqm,
300 struct qcm_process_device *qpd,
303 dqm->active_queue_count--;
304 if (q->properties.type == KFD_QUEUE_TYPE_COMPUTE ||
305 q->properties.type == KFD_QUEUE_TYPE_DIQ)
306 dqm->active_cp_queue_count--;
308 if (q->properties.is_gws) {
309 dqm->gws_queue_count--;
310 qpd->mapped_gws_queue = false;
315 * Allocate a doorbell ID to this queue.
316 * If doorbell_id is passed in, make sure requested ID is valid then allocate it.
318 static int allocate_doorbell(struct qcm_process_device *qpd,
320 uint32_t const *restore_id)
322 struct kfd_dev *dev = qpd->dqm->dev;
324 if (!KFD_IS_SOC15(dev)) {
325 /* On pre-SOC15 chips we need to use the queue ID to
326 * preserve the user mode ABI.
329 if (restore_id && *restore_id != q->properties.queue_id)
332 q->doorbell_id = q->properties.queue_id;
333 } else if (q->properties.type == KFD_QUEUE_TYPE_SDMA ||
334 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) {
335 /* For SDMA queues on SOC15 with 8-byte doorbell, use static
336 * doorbell assignments based on the engine and queue id.
337 * The doobell index distance between RLC (2*i) and (2*i+1)
338 * for a SDMA engine is 512.
341 uint32_t *idx_offset = dev->shared_resources.sdma_doorbell_idx;
342 uint32_t valid_id = idx_offset[q->properties.sdma_engine_id]
343 + (q->properties.sdma_queue_id & 1)
344 * KFD_QUEUE_DOORBELL_MIRROR_OFFSET
345 + (q->properties.sdma_queue_id >> 1);
347 if (restore_id && *restore_id != valid_id)
349 q->doorbell_id = valid_id;
351 /* For CP queues on SOC15 */
353 /* make sure that ID is free */
354 if (__test_and_set_bit(*restore_id, qpd->doorbell_bitmap))
357 q->doorbell_id = *restore_id;
359 /* or reserve a free doorbell ID */
362 found = find_first_zero_bit(qpd->doorbell_bitmap,
363 KFD_MAX_NUM_OF_QUEUES_PER_PROCESS);
364 if (found >= KFD_MAX_NUM_OF_QUEUES_PER_PROCESS) {
365 pr_debug("No doorbells available");
368 set_bit(found, qpd->doorbell_bitmap);
369 q->doorbell_id = found;
373 q->properties.doorbell_off =
374 kfd_get_doorbell_dw_offset_in_bar(dev, qpd_to_pdd(qpd),
379 static void deallocate_doorbell(struct qcm_process_device *qpd,
383 struct kfd_dev *dev = qpd->dqm->dev;
385 if (!KFD_IS_SOC15(dev) ||
386 q->properties.type == KFD_QUEUE_TYPE_SDMA ||
387 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)
390 old = test_and_clear_bit(q->doorbell_id, qpd->doorbell_bitmap);
394 static void program_trap_handler_settings(struct device_queue_manager *dqm,
395 struct qcm_process_device *qpd)
397 if (dqm->dev->kfd2kgd->program_trap_handler_settings)
398 dqm->dev->kfd2kgd->program_trap_handler_settings(
399 dqm->dev->adev, qpd->vmid,
400 qpd->tba_addr, qpd->tma_addr);
403 static int allocate_vmid(struct device_queue_manager *dqm,
404 struct qcm_process_device *qpd,
407 int allocated_vmid = -1, i;
409 for (i = dqm->dev->vm_info.first_vmid_kfd;
410 i <= dqm->dev->vm_info.last_vmid_kfd; i++) {
411 if (!dqm->vmid_pasid[i]) {
417 if (allocated_vmid < 0) {
418 pr_err("no more vmid to allocate\n");
422 pr_debug("vmid allocated: %d\n", allocated_vmid);
424 dqm->vmid_pasid[allocated_vmid] = q->process->pasid;
426 set_pasid_vmid_mapping(dqm, q->process->pasid, allocated_vmid);
428 qpd->vmid = allocated_vmid;
429 q->properties.vmid = allocated_vmid;
431 program_sh_mem_settings(dqm, qpd);
433 if (KFD_IS_SOC15(dqm->dev) && dqm->dev->cwsr_enabled)
434 program_trap_handler_settings(dqm, qpd);
436 /* qpd->page_table_base is set earlier when register_process()
437 * is called, i.e. when the first queue is created.
439 dqm->dev->kfd2kgd->set_vm_context_page_table_base(dqm->dev->adev,
441 qpd->page_table_base);
442 /* invalidate the VM context after pasid and vmid mapping is set up */
443 kfd_flush_tlb(qpd_to_pdd(qpd), TLB_FLUSH_LEGACY);
445 if (dqm->dev->kfd2kgd->set_scratch_backing_va)
446 dqm->dev->kfd2kgd->set_scratch_backing_va(dqm->dev->adev,
447 qpd->sh_hidden_private_base, qpd->vmid);
452 static int flush_texture_cache_nocpsch(struct kfd_dev *kdev,
453 struct qcm_process_device *qpd)
455 const struct packet_manager_funcs *pmf = qpd->dqm->packet_mgr.pmf;
461 ret = pmf->release_mem(qpd->ib_base, (uint32_t *)qpd->ib_kaddr);
465 return amdgpu_amdkfd_submit_ib(kdev->adev, KGD_ENGINE_MEC1, qpd->vmid,
466 qpd->ib_base, (uint32_t *)qpd->ib_kaddr,
467 pmf->release_mem_size / sizeof(uint32_t));
470 static void deallocate_vmid(struct device_queue_manager *dqm,
471 struct qcm_process_device *qpd,
474 /* On GFX v7, CP doesn't flush TC at dequeue */
475 if (q->device->adev->asic_type == CHIP_HAWAII)
476 if (flush_texture_cache_nocpsch(q->device, qpd))
477 pr_err("Failed to flush TC\n");
479 kfd_flush_tlb(qpd_to_pdd(qpd), TLB_FLUSH_LEGACY);
481 /* Release the vmid mapping */
482 set_pasid_vmid_mapping(dqm, 0, qpd->vmid);
483 dqm->vmid_pasid[qpd->vmid] = 0;
486 q->properties.vmid = 0;
489 static int create_queue_nocpsch(struct device_queue_manager *dqm,
491 struct qcm_process_device *qpd,
492 const struct kfd_criu_queue_priv_data *qd,
493 const void *restore_mqd, const void *restore_ctl_stack)
495 struct mqd_manager *mqd_mgr;
500 if (dqm->total_queue_count >= max_num_of_queues_per_device) {
501 pr_warn("Can't create new usermode queue because %d queues were already created\n",
502 dqm->total_queue_count);
507 if (list_empty(&qpd->queues_list)) {
508 retval = allocate_vmid(dqm, qpd, q);
512 q->properties.vmid = qpd->vmid;
514 * Eviction state logic: mark all queues as evicted, even ones
515 * not currently active. Restoring inactive queues later only
516 * updates the is_evicted flag but is a no-op otherwise.
518 q->properties.is_evicted = !!qpd->evicted;
520 q->properties.tba_addr = qpd->tba_addr;
521 q->properties.tma_addr = qpd->tma_addr;
523 mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type(
524 q->properties.type)];
525 if (q->properties.type == KFD_QUEUE_TYPE_COMPUTE) {
526 retval = allocate_hqd(dqm, q);
528 goto deallocate_vmid;
529 pr_debug("Loading mqd to hqd on pipe %d, queue %d\n",
531 } else if (q->properties.type == KFD_QUEUE_TYPE_SDMA ||
532 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) {
533 retval = allocate_sdma_queue(dqm, q, qd ? &qd->sdma_id : NULL);
535 goto deallocate_vmid;
536 dqm->asic_ops.init_sdma_vm(dqm, q, qpd);
539 retval = allocate_doorbell(qpd, q, qd ? &qd->doorbell_id : NULL);
541 goto out_deallocate_hqd;
543 /* Temporarily release dqm lock to avoid a circular lock dependency */
545 q->mqd_mem_obj = mqd_mgr->allocate_mqd(mqd_mgr->dev, &q->properties);
548 if (!q->mqd_mem_obj) {
550 goto out_deallocate_doorbell;
554 mqd_mgr->restore_mqd(mqd_mgr, &q->mqd, q->mqd_mem_obj, &q->gart_mqd_addr,
555 &q->properties, restore_mqd, restore_ctl_stack,
558 mqd_mgr->init_mqd(mqd_mgr, &q->mqd, q->mqd_mem_obj,
559 &q->gart_mqd_addr, &q->properties);
561 if (q->properties.is_active) {
562 if (!dqm->sched_running) {
563 WARN_ONCE(1, "Load non-HWS mqd while stopped\n");
564 goto add_queue_to_list;
567 if (WARN(q->process->mm != current->mm,
568 "should only run in user thread"))
571 retval = mqd_mgr->load_mqd(mqd_mgr, q->mqd, q->pipe,
572 q->queue, &q->properties, current->mm);
578 list_add(&q->list, &qpd->queues_list);
580 if (q->properties.is_active)
581 increment_queue_count(dqm, qpd, q);
584 * Unconditionally increment this counter, regardless of the queue's
585 * type or whether the queue is active.
587 dqm->total_queue_count++;
588 pr_debug("Total of %d queues are accountable so far\n",
589 dqm->total_queue_count);
593 mqd_mgr->free_mqd(mqd_mgr, q->mqd, q->mqd_mem_obj);
594 out_deallocate_doorbell:
595 deallocate_doorbell(qpd, q);
597 if (q->properties.type == KFD_QUEUE_TYPE_COMPUTE)
598 deallocate_hqd(dqm, q);
599 else if (q->properties.type == KFD_QUEUE_TYPE_SDMA ||
600 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)
601 deallocate_sdma_queue(dqm, q);
603 if (list_empty(&qpd->queues_list))
604 deallocate_vmid(dqm, qpd, q);
610 static int allocate_hqd(struct device_queue_manager *dqm, struct queue *q)
617 for (pipe = dqm->next_pipe_to_allocate, i = 0;
618 i < get_pipes_per_mec(dqm);
619 pipe = ((pipe + 1) % get_pipes_per_mec(dqm)), ++i) {
621 if (!is_pipe_enabled(dqm, 0, pipe))
624 if (dqm->allocated_queues[pipe] != 0) {
625 bit = ffs(dqm->allocated_queues[pipe]) - 1;
626 dqm->allocated_queues[pipe] &= ~(1 << bit);
637 pr_debug("hqd slot - pipe %d, queue %d\n", q->pipe, q->queue);
638 /* horizontal hqd allocation */
639 dqm->next_pipe_to_allocate = (pipe + 1) % get_pipes_per_mec(dqm);
644 static inline void deallocate_hqd(struct device_queue_manager *dqm,
647 dqm->allocated_queues[q->pipe] |= (1 << q->queue);
650 #define SQ_IND_CMD_CMD_KILL 0x00000003
651 #define SQ_IND_CMD_MODE_BROADCAST 0x00000001
653 static int dbgdev_wave_reset_wavefronts(struct kfd_dev *dev, struct kfd_process *p)
657 uint16_t queried_pasid;
658 union SQ_CMD_BITS reg_sq_cmd;
659 union GRBM_GFX_INDEX_BITS reg_gfx_index;
660 struct kfd_process_device *pdd;
661 int first_vmid_to_scan = dev->vm_info.first_vmid_kfd;
662 int last_vmid_to_scan = dev->vm_info.last_vmid_kfd;
664 reg_sq_cmd.u32All = 0;
665 reg_gfx_index.u32All = 0;
667 pr_debug("Killing all process wavefronts\n");
669 if (!dev->kfd2kgd->get_atc_vmid_pasid_mapping_info) {
670 pr_err("no vmid pasid mapping supported \n");
674 /* Scan all registers in the range ATC_VMID8_PASID_MAPPING ..
675 * ATC_VMID15_PASID_MAPPING
676 * to check which VMID the current process is mapped to.
679 for (vmid = first_vmid_to_scan; vmid <= last_vmid_to_scan; vmid++) {
680 status = dev->kfd2kgd->get_atc_vmid_pasid_mapping_info
681 (dev->adev, vmid, &queried_pasid);
683 if (status && queried_pasid == p->pasid) {
684 pr_debug("Killing wave fronts of vmid %d and pasid 0x%x\n",
690 if (vmid > last_vmid_to_scan) {
691 pr_err("Didn't find vmid for pasid 0x%x\n", p->pasid);
695 /* taking the VMID for that process on the safe way using PDD */
696 pdd = kfd_get_process_device_data(dev, p);
700 reg_gfx_index.bits.sh_broadcast_writes = 1;
701 reg_gfx_index.bits.se_broadcast_writes = 1;
702 reg_gfx_index.bits.instance_broadcast_writes = 1;
703 reg_sq_cmd.bits.mode = SQ_IND_CMD_MODE_BROADCAST;
704 reg_sq_cmd.bits.cmd = SQ_IND_CMD_CMD_KILL;
705 reg_sq_cmd.bits.vm_id = vmid;
707 dev->kfd2kgd->wave_control_execute(dev->adev,
708 reg_gfx_index.u32All,
714 /* Access to DQM has to be locked before calling destroy_queue_nocpsch_locked
715 * to avoid asynchronized access
717 static int destroy_queue_nocpsch_locked(struct device_queue_manager *dqm,
718 struct qcm_process_device *qpd,
722 struct mqd_manager *mqd_mgr;
724 mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type(
725 q->properties.type)];
727 if (q->properties.type == KFD_QUEUE_TYPE_COMPUTE)
728 deallocate_hqd(dqm, q);
729 else if (q->properties.type == KFD_QUEUE_TYPE_SDMA)
730 deallocate_sdma_queue(dqm, q);
731 else if (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)
732 deallocate_sdma_queue(dqm, q);
734 pr_debug("q->properties.type %d is invalid\n",
738 dqm->total_queue_count--;
740 deallocate_doorbell(qpd, q);
742 if (!dqm->sched_running) {
743 WARN_ONCE(1, "Destroy non-HWS queue while stopped\n");
747 retval = mqd_mgr->destroy_mqd(mqd_mgr, q->mqd,
748 KFD_PREEMPT_TYPE_WAVEFRONT_RESET,
749 KFD_UNMAP_LATENCY_MS,
751 if (retval == -ETIME)
752 qpd->reset_wavefronts = true;
755 if (list_empty(&qpd->queues_list)) {
756 if (qpd->reset_wavefronts) {
757 pr_warn("Resetting wave fronts (nocpsch) on dev %p\n",
759 /* dbgdev_wave_reset_wavefronts has to be called before
760 * deallocate_vmid(), i.e. when vmid is still in use.
762 dbgdev_wave_reset_wavefronts(dqm->dev,
764 qpd->reset_wavefronts = false;
767 deallocate_vmid(dqm, qpd, q);
770 if (q->properties.is_active)
771 decrement_queue_count(dqm, qpd, q);
776 static int destroy_queue_nocpsch(struct device_queue_manager *dqm,
777 struct qcm_process_device *qpd,
781 uint64_t sdma_val = 0;
782 struct kfd_process_device *pdd = qpd_to_pdd(qpd);
783 struct mqd_manager *mqd_mgr =
784 dqm->mqd_mgrs[get_mqd_type_from_queue_type(q->properties.type)];
786 /* Get the SDMA queue stats */
787 if ((q->properties.type == KFD_QUEUE_TYPE_SDMA) ||
788 (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)) {
789 retval = read_sdma_queue_counter((uint64_t __user *)q->properties.read_ptr,
792 pr_err("Failed to read SDMA queue counter for queue: %d\n",
793 q->properties.queue_id);
797 retval = destroy_queue_nocpsch_locked(dqm, qpd, q);
799 pdd->sdma_past_activity_counter += sdma_val;
802 mqd_mgr->free_mqd(mqd_mgr, q->mqd, q->mqd_mem_obj);
807 static int update_queue(struct device_queue_manager *dqm, struct queue *q,
808 struct mqd_update_info *minfo)
811 struct mqd_manager *mqd_mgr;
812 struct kfd_process_device *pdd;
813 bool prev_active = false;
814 bool add_queue = false;
817 pdd = kfd_get_process_device_data(q->device, q->process);
822 mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type(
823 q->properties.type)];
825 /* Save previous activity state for counters */
826 prev_active = q->properties.is_active;
828 /* Make sure the queue is unmapped before updating the MQD */
829 if (dqm->sched_policy != KFD_SCHED_POLICY_NO_HWS) {
830 if (!dqm->dev->shared_resources.enable_mes)
831 retval = unmap_queues_cpsch(dqm,
832 KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 0, false);
833 else if (prev_active)
834 retval = remove_queue_mes(dqm, q, &pdd->qpd);
837 pr_err("unmap queue failed\n");
840 } else if (prev_active &&
841 (q->properties.type == KFD_QUEUE_TYPE_COMPUTE ||
842 q->properties.type == KFD_QUEUE_TYPE_SDMA ||
843 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)) {
845 if (!dqm->sched_running) {
846 WARN_ONCE(1, "Update non-HWS queue while stopped\n");
850 retval = mqd_mgr->destroy_mqd(mqd_mgr, q->mqd,
851 (dqm->dev->cwsr_enabled ?
852 KFD_PREEMPT_TYPE_WAVEFRONT_SAVE :
853 KFD_PREEMPT_TYPE_WAVEFRONT_DRAIN),
854 KFD_UNMAP_LATENCY_MS, q->pipe, q->queue);
856 pr_err("destroy mqd failed\n");
861 mqd_mgr->update_mqd(mqd_mgr, q->mqd, &q->properties, minfo);
864 * check active state vs. the previous state and modify
865 * counter accordingly. map_queues_cpsch uses the
866 * dqm->active_queue_count to determine whether a new runlist must be
869 if (q->properties.is_active && !prev_active) {
870 increment_queue_count(dqm, &pdd->qpd, q);
871 } else if (!q->properties.is_active && prev_active) {
872 decrement_queue_count(dqm, &pdd->qpd, q);
873 } else if (q->gws && !q->properties.is_gws) {
874 if (q->properties.is_active) {
875 dqm->gws_queue_count++;
876 pdd->qpd.mapped_gws_queue = true;
878 q->properties.is_gws = true;
879 } else if (!q->gws && q->properties.is_gws) {
880 if (q->properties.is_active) {
881 dqm->gws_queue_count--;
882 pdd->qpd.mapped_gws_queue = false;
884 q->properties.is_gws = false;
887 if (dqm->sched_policy != KFD_SCHED_POLICY_NO_HWS) {
888 if (!dqm->dev->shared_resources.enable_mes)
889 retval = map_queues_cpsch(dqm);
891 retval = add_queue_mes(dqm, q, &pdd->qpd);
892 } else if (q->properties.is_active &&
893 (q->properties.type == KFD_QUEUE_TYPE_COMPUTE ||
894 q->properties.type == KFD_QUEUE_TYPE_SDMA ||
895 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)) {
896 if (WARN(q->process->mm != current->mm,
897 "should only run in user thread"))
900 retval = mqd_mgr->load_mqd(mqd_mgr, q->mqd,
902 &q->properties, current->mm);
910 static int evict_process_queues_nocpsch(struct device_queue_manager *dqm,
911 struct qcm_process_device *qpd)
914 struct mqd_manager *mqd_mgr;
915 struct kfd_process_device *pdd;
919 if (qpd->evicted++ > 0) /* already evicted, do nothing */
922 pdd = qpd_to_pdd(qpd);
923 pr_debug_ratelimited("Evicting PASID 0x%x queues\n",
924 pdd->process->pasid);
926 pdd->last_evict_timestamp = get_jiffies_64();
927 /* Mark all queues as evicted. Deactivate all active queues on
930 list_for_each_entry(q, &qpd->queues_list, list) {
931 q->properties.is_evicted = true;
932 if (!q->properties.is_active)
935 mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type(
936 q->properties.type)];
937 q->properties.is_active = false;
938 decrement_queue_count(dqm, qpd, q);
940 if (WARN_ONCE(!dqm->sched_running, "Evict when stopped\n"))
943 retval = mqd_mgr->destroy_mqd(mqd_mgr, q->mqd,
944 (dqm->dev->cwsr_enabled ?
945 KFD_PREEMPT_TYPE_WAVEFRONT_SAVE :
946 KFD_PREEMPT_TYPE_WAVEFRONT_DRAIN),
947 KFD_UNMAP_LATENCY_MS, q->pipe, q->queue);
949 /* Return the first error, but keep going to
950 * maintain a consistent eviction state
960 static int evict_process_queues_cpsch(struct device_queue_manager *dqm,
961 struct qcm_process_device *qpd)
964 struct kfd_process_device *pdd;
968 if (qpd->evicted++ > 0) /* already evicted, do nothing */
971 pdd = qpd_to_pdd(qpd);
972 pr_debug_ratelimited("Evicting PASID 0x%x queues\n",
973 pdd->process->pasid);
975 /* Mark all queues as evicted. Deactivate all active queues on
978 list_for_each_entry(q, &qpd->queues_list, list) {
979 q->properties.is_evicted = true;
980 if (!q->properties.is_active)
983 q->properties.is_active = false;
984 decrement_queue_count(dqm, qpd, q);
986 if (dqm->dev->shared_resources.enable_mes) {
987 retval = remove_queue_mes(dqm, q, qpd);
989 pr_err("Failed to evict queue %d\n",
990 q->properties.queue_id);
995 pdd->last_evict_timestamp = get_jiffies_64();
996 if (!dqm->dev->shared_resources.enable_mes)
997 retval = execute_queues_cpsch(dqm,
999 KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES :
1000 KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 0);
1007 static int restore_process_queues_nocpsch(struct device_queue_manager *dqm,
1008 struct qcm_process_device *qpd)
1010 struct mm_struct *mm = NULL;
1012 struct mqd_manager *mqd_mgr;
1013 struct kfd_process_device *pdd;
1015 uint64_t eviction_duration;
1016 int retval, ret = 0;
1018 pdd = qpd_to_pdd(qpd);
1019 /* Retrieve PD base */
1020 pd_base = amdgpu_amdkfd_gpuvm_get_process_page_dir(pdd->drm_priv);
1023 if (WARN_ON_ONCE(!qpd->evicted)) /* already restored, do nothing */
1025 if (qpd->evicted > 1) { /* ref count still > 0, decrement & quit */
1030 pr_debug_ratelimited("Restoring PASID 0x%x queues\n",
1031 pdd->process->pasid);
1033 /* Update PD Base in QPD */
1034 qpd->page_table_base = pd_base;
1035 pr_debug("Updated PD address to 0x%llx\n", pd_base);
1037 if (!list_empty(&qpd->queues_list)) {
1038 dqm->dev->kfd2kgd->set_vm_context_page_table_base(
1041 qpd->page_table_base);
1042 kfd_flush_tlb(pdd, TLB_FLUSH_LEGACY);
1045 /* Take a safe reference to the mm_struct, which may otherwise
1046 * disappear even while the kfd_process is still referenced.
1048 mm = get_task_mm(pdd->process->lead_thread);
1054 /* Remove the eviction flags. Activate queues that are not
1055 * inactive for other reasons.
1057 list_for_each_entry(q, &qpd->queues_list, list) {
1058 q->properties.is_evicted = false;
1059 if (!QUEUE_IS_ACTIVE(q->properties))
1062 mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type(
1063 q->properties.type)];
1064 q->properties.is_active = true;
1065 increment_queue_count(dqm, qpd, q);
1067 if (WARN_ONCE(!dqm->sched_running, "Restore when stopped\n"))
1070 retval = mqd_mgr->load_mqd(mqd_mgr, q->mqd, q->pipe,
1071 q->queue, &q->properties, mm);
1073 /* Return the first error, but keep going to
1074 * maintain a consistent eviction state
1079 eviction_duration = get_jiffies_64() - pdd->last_evict_timestamp;
1080 atomic64_add(eviction_duration, &pdd->evict_duration_counter);
1088 static int restore_process_queues_cpsch(struct device_queue_manager *dqm,
1089 struct qcm_process_device *qpd)
1092 struct kfd_process_device *pdd;
1094 uint64_t eviction_duration;
1097 pdd = qpd_to_pdd(qpd);
1098 /* Retrieve PD base */
1099 pd_base = amdgpu_amdkfd_gpuvm_get_process_page_dir(pdd->drm_priv);
1102 if (WARN_ON_ONCE(!qpd->evicted)) /* already restored, do nothing */
1104 if (qpd->evicted > 1) { /* ref count still > 0, decrement & quit */
1109 pr_debug_ratelimited("Restoring PASID 0x%x queues\n",
1110 pdd->process->pasid);
1112 /* Update PD Base in QPD */
1113 qpd->page_table_base = pd_base;
1114 pr_debug("Updated PD address to 0x%llx\n", pd_base);
1116 /* activate all active queues on the qpd */
1117 list_for_each_entry(q, &qpd->queues_list, list) {
1118 q->properties.is_evicted = false;
1119 if (!QUEUE_IS_ACTIVE(q->properties))
1122 q->properties.is_active = true;
1123 increment_queue_count(dqm, &pdd->qpd, q);
1125 if (dqm->dev->shared_resources.enable_mes) {
1126 retval = add_queue_mes(dqm, q, qpd);
1128 pr_err("Failed to restore queue %d\n",
1129 q->properties.queue_id);
1134 if (!dqm->dev->shared_resources.enable_mes)
1135 retval = execute_queues_cpsch(dqm,
1136 KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 0);
1138 eviction_duration = get_jiffies_64() - pdd->last_evict_timestamp;
1139 atomic64_add(eviction_duration, &pdd->evict_duration_counter);
1145 static int register_process(struct device_queue_manager *dqm,
1146 struct qcm_process_device *qpd)
1148 struct device_process_node *n;
1149 struct kfd_process_device *pdd;
1153 n = kzalloc(sizeof(*n), GFP_KERNEL);
1159 pdd = qpd_to_pdd(qpd);
1160 /* Retrieve PD base */
1161 pd_base = amdgpu_amdkfd_gpuvm_get_process_page_dir(pdd->drm_priv);
1164 list_add(&n->list, &dqm->queues);
1166 /* Update PD Base in QPD */
1167 qpd->page_table_base = pd_base;
1168 pr_debug("Updated PD address to 0x%llx\n", pd_base);
1170 retval = dqm->asic_ops.update_qpd(dqm, qpd);
1172 dqm->processes_count++;
1176 /* Outside the DQM lock because under the DQM lock we can't do
1177 * reclaim or take other locks that others hold while reclaiming.
1179 kfd_inc_compute_active(dqm->dev);
1184 static int unregister_process(struct device_queue_manager *dqm,
1185 struct qcm_process_device *qpd)
1188 struct device_process_node *cur, *next;
1190 pr_debug("qpd->queues_list is %s\n",
1191 list_empty(&qpd->queues_list) ? "empty" : "not empty");
1196 list_for_each_entry_safe(cur, next, &dqm->queues, list) {
1197 if (qpd == cur->qpd) {
1198 list_del(&cur->list);
1200 dqm->processes_count--;
1204 /* qpd not found in dqm list */
1209 /* Outside the DQM lock because under the DQM lock we can't do
1210 * reclaim or take other locks that others hold while reclaiming.
1213 kfd_dec_compute_active(dqm->dev);
1219 set_pasid_vmid_mapping(struct device_queue_manager *dqm, u32 pasid,
1222 return dqm->dev->kfd2kgd->set_pasid_vmid_mapping(
1223 dqm->dev->adev, pasid, vmid);
1226 static void init_interrupts(struct device_queue_manager *dqm)
1230 for (i = 0 ; i < get_pipes_per_mec(dqm) ; i++)
1231 if (is_pipe_enabled(dqm, 0, i))
1232 dqm->dev->kfd2kgd->init_interrupts(dqm->dev->adev, i);
1235 static int initialize_nocpsch(struct device_queue_manager *dqm)
1239 pr_debug("num of pipes: %d\n", get_pipes_per_mec(dqm));
1241 dqm->allocated_queues = kcalloc(get_pipes_per_mec(dqm),
1242 sizeof(unsigned int), GFP_KERNEL);
1243 if (!dqm->allocated_queues)
1246 mutex_init(&dqm->lock_hidden);
1247 INIT_LIST_HEAD(&dqm->queues);
1248 dqm->active_queue_count = dqm->next_pipe_to_allocate = 0;
1249 dqm->active_cp_queue_count = 0;
1250 dqm->gws_queue_count = 0;
1252 for (pipe = 0; pipe < get_pipes_per_mec(dqm); pipe++) {
1253 int pipe_offset = pipe * get_queues_per_pipe(dqm);
1255 for (queue = 0; queue < get_queues_per_pipe(dqm); queue++)
1256 if (test_bit(pipe_offset + queue,
1257 dqm->dev->shared_resources.cp_queue_bitmap))
1258 dqm->allocated_queues[pipe] |= 1 << queue;
1261 memset(dqm->vmid_pasid, 0, sizeof(dqm->vmid_pasid));
1263 dqm->sdma_bitmap = ~0ULL >> (64 - get_num_sdma_queues(dqm));
1264 dqm->sdma_bitmap &= ~(get_reserved_sdma_queues_bitmap(dqm));
1265 pr_info("sdma_bitmap: %llx\n", dqm->sdma_bitmap);
1267 dqm->xgmi_sdma_bitmap = ~0ULL >> (64 - get_num_xgmi_sdma_queues(dqm));
1272 static void uninitialize(struct device_queue_manager *dqm)
1276 WARN_ON(dqm->active_queue_count > 0 || dqm->processes_count > 0);
1278 kfree(dqm->allocated_queues);
1279 for (i = 0 ; i < KFD_MQD_TYPE_MAX ; i++)
1280 kfree(dqm->mqd_mgrs[i]);
1281 mutex_destroy(&dqm->lock_hidden);
1284 static int start_nocpsch(struct device_queue_manager *dqm)
1288 pr_info("SW scheduler is used");
1289 init_interrupts(dqm);
1291 if (dqm->dev->adev->asic_type == CHIP_HAWAII)
1292 r = pm_init(&dqm->packet_mgr, dqm);
1294 dqm->sched_running = true;
1299 static int stop_nocpsch(struct device_queue_manager *dqm)
1301 if (dqm->dev->adev->asic_type == CHIP_HAWAII)
1302 pm_uninit(&dqm->packet_mgr, false);
1303 dqm->sched_running = false;
1308 static void pre_reset(struct device_queue_manager *dqm)
1311 dqm->is_resetting = true;
1315 static int allocate_sdma_queue(struct device_queue_manager *dqm,
1316 struct queue *q, const uint32_t *restore_sdma_id)
1320 if (q->properties.type == KFD_QUEUE_TYPE_SDMA) {
1321 if (dqm->sdma_bitmap == 0) {
1322 pr_err("No more SDMA queue to allocate\n");
1326 if (restore_sdma_id) {
1327 /* Re-use existing sdma_id */
1328 if (!(dqm->sdma_bitmap & (1ULL << *restore_sdma_id))) {
1329 pr_err("SDMA queue already in use\n");
1332 dqm->sdma_bitmap &= ~(1ULL << *restore_sdma_id);
1333 q->sdma_id = *restore_sdma_id;
1335 /* Find first available sdma_id */
1336 bit = __ffs64(dqm->sdma_bitmap);
1337 dqm->sdma_bitmap &= ~(1ULL << bit);
1341 q->properties.sdma_engine_id = q->sdma_id %
1342 kfd_get_num_sdma_engines(dqm->dev);
1343 q->properties.sdma_queue_id = q->sdma_id /
1344 kfd_get_num_sdma_engines(dqm->dev);
1345 } else if (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) {
1346 if (dqm->xgmi_sdma_bitmap == 0) {
1347 pr_err("No more XGMI SDMA queue to allocate\n");
1350 if (restore_sdma_id) {
1351 /* Re-use existing sdma_id */
1352 if (!(dqm->xgmi_sdma_bitmap & (1ULL << *restore_sdma_id))) {
1353 pr_err("SDMA queue already in use\n");
1356 dqm->xgmi_sdma_bitmap &= ~(1ULL << *restore_sdma_id);
1357 q->sdma_id = *restore_sdma_id;
1359 bit = __ffs64(dqm->xgmi_sdma_bitmap);
1360 dqm->xgmi_sdma_bitmap &= ~(1ULL << bit);
1363 /* sdma_engine_id is sdma id including
1364 * both PCIe-optimized SDMAs and XGMI-
1365 * optimized SDMAs. The calculation below
1366 * assumes the first N engines are always
1367 * PCIe-optimized ones
1369 q->properties.sdma_engine_id =
1370 kfd_get_num_sdma_engines(dqm->dev) +
1371 q->sdma_id % kfd_get_num_xgmi_sdma_engines(dqm->dev);
1372 q->properties.sdma_queue_id = q->sdma_id /
1373 kfd_get_num_xgmi_sdma_engines(dqm->dev);
1376 pr_debug("SDMA engine id: %d\n", q->properties.sdma_engine_id);
1377 pr_debug("SDMA queue id: %d\n", q->properties.sdma_queue_id);
1382 static void deallocate_sdma_queue(struct device_queue_manager *dqm,
1385 if (q->properties.type == KFD_QUEUE_TYPE_SDMA) {
1386 if (q->sdma_id >= get_num_sdma_queues(dqm))
1388 dqm->sdma_bitmap |= (1ULL << q->sdma_id);
1389 } else if (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) {
1390 if (q->sdma_id >= get_num_xgmi_sdma_queues(dqm))
1392 dqm->xgmi_sdma_bitmap |= (1ULL << q->sdma_id);
1397 * Device Queue Manager implementation for cp scheduler
1400 static int set_sched_resources(struct device_queue_manager *dqm)
1403 struct scheduling_resources res;
1405 res.vmid_mask = dqm->dev->shared_resources.compute_vmid_bitmap;
1408 for (i = 0; i < KGD_MAX_QUEUES; ++i) {
1409 mec = (i / dqm->dev->shared_resources.num_queue_per_pipe)
1410 / dqm->dev->shared_resources.num_pipe_per_mec;
1412 if (!test_bit(i, dqm->dev->shared_resources.cp_queue_bitmap))
1415 /* only acquire queues from the first MEC */
1419 /* This situation may be hit in the future if a new HW
1420 * generation exposes more than 64 queues. If so, the
1421 * definition of res.queue_mask needs updating
1423 if (WARN_ON(i >= (sizeof(res.queue_mask)*8))) {
1424 pr_err("Invalid queue enabled by amdgpu: %d\n", i);
1428 res.queue_mask |= 1ull
1429 << amdgpu_queue_mask_bit_to_set_resource_bit(
1432 res.gws_mask = ~0ull;
1433 res.oac_mask = res.gds_heap_base = res.gds_heap_size = 0;
1435 pr_debug("Scheduling resources:\n"
1436 "vmid mask: 0x%8X\n"
1437 "queue mask: 0x%8llX\n",
1438 res.vmid_mask, res.queue_mask);
1440 return pm_send_set_resources(&dqm->packet_mgr, &res);
1443 static int initialize_cpsch(struct device_queue_manager *dqm)
1445 uint64_t num_sdma_queues;
1446 uint64_t num_xgmi_sdma_queues;
1448 pr_debug("num of pipes: %d\n", get_pipes_per_mec(dqm));
1450 mutex_init(&dqm->lock_hidden);
1451 INIT_LIST_HEAD(&dqm->queues);
1452 dqm->active_queue_count = dqm->processes_count = 0;
1453 dqm->active_cp_queue_count = 0;
1454 dqm->gws_queue_count = 0;
1455 dqm->active_runlist = false;
1457 num_sdma_queues = get_num_sdma_queues(dqm);
1458 if (num_sdma_queues >= BITS_PER_TYPE(dqm->sdma_bitmap))
1459 dqm->sdma_bitmap = ULLONG_MAX;
1461 dqm->sdma_bitmap = (BIT_ULL(num_sdma_queues) - 1);
1463 dqm->sdma_bitmap &= ~(get_reserved_sdma_queues_bitmap(dqm));
1464 pr_info("sdma_bitmap: %llx\n", dqm->sdma_bitmap);
1466 num_xgmi_sdma_queues = get_num_xgmi_sdma_queues(dqm);
1467 if (num_xgmi_sdma_queues >= BITS_PER_TYPE(dqm->xgmi_sdma_bitmap))
1468 dqm->xgmi_sdma_bitmap = ULLONG_MAX;
1470 dqm->xgmi_sdma_bitmap = (BIT_ULL(num_xgmi_sdma_queues) - 1);
1472 INIT_WORK(&dqm->hw_exception_work, kfd_process_hw_exception);
1477 static int start_cpsch(struct device_queue_manager *dqm)
1485 if (!dqm->dev->shared_resources.enable_mes) {
1486 retval = pm_init(&dqm->packet_mgr, dqm);
1488 goto fail_packet_manager_init;
1490 retval = set_sched_resources(dqm);
1492 goto fail_set_sched_resources;
1494 pr_debug("Allocating fence memory\n");
1496 /* allocate fence memory on the gart */
1497 retval = kfd_gtt_sa_allocate(dqm->dev, sizeof(*dqm->fence_addr),
1501 goto fail_allocate_vidmem;
1503 dqm->fence_addr = (uint64_t *)dqm->fence_mem->cpu_ptr;
1504 dqm->fence_gpu_addr = dqm->fence_mem->gpu_addr;
1506 init_interrupts(dqm);
1508 /* clear hang status when driver try to start the hw scheduler */
1509 dqm->is_hws_hang = false;
1510 dqm->is_resetting = false;
1511 dqm->sched_running = true;
1512 if (!dqm->dev->shared_resources.enable_mes)
1513 execute_queues_cpsch(dqm, KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 0);
1517 fail_allocate_vidmem:
1518 fail_set_sched_resources:
1519 if (!dqm->dev->shared_resources.enable_mes)
1520 pm_uninit(&dqm->packet_mgr, false);
1521 fail_packet_manager_init:
1526 static int stop_cpsch(struct device_queue_manager *dqm)
1531 if (!dqm->sched_running) {
1536 if (!dqm->is_hws_hang) {
1537 if (!dqm->dev->shared_resources.enable_mes)
1538 unmap_queues_cpsch(dqm, KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES, 0, false);
1540 remove_all_queues_mes(dqm);
1543 hanging = dqm->is_hws_hang || dqm->is_resetting;
1544 dqm->sched_running = false;
1546 if (!dqm->dev->shared_resources.enable_mes)
1547 pm_release_ib(&dqm->packet_mgr);
1549 kfd_gtt_sa_free(dqm->dev, dqm->fence_mem);
1550 if (!dqm->dev->shared_resources.enable_mes)
1551 pm_uninit(&dqm->packet_mgr, hanging);
1557 static int create_kernel_queue_cpsch(struct device_queue_manager *dqm,
1558 struct kernel_queue *kq,
1559 struct qcm_process_device *qpd)
1562 if (dqm->total_queue_count >= max_num_of_queues_per_device) {
1563 pr_warn("Can't create new kernel queue because %d queues were already created\n",
1564 dqm->total_queue_count);
1570 * Unconditionally increment this counter, regardless of the queue's
1571 * type or whether the queue is active.
1573 dqm->total_queue_count++;
1574 pr_debug("Total of %d queues are accountable so far\n",
1575 dqm->total_queue_count);
1577 list_add(&kq->list, &qpd->priv_queue_list);
1578 increment_queue_count(dqm, qpd, kq->queue);
1579 qpd->is_debug = true;
1580 execute_queues_cpsch(dqm, KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 0);
1586 static void destroy_kernel_queue_cpsch(struct device_queue_manager *dqm,
1587 struct kernel_queue *kq,
1588 struct qcm_process_device *qpd)
1591 list_del(&kq->list);
1592 decrement_queue_count(dqm, qpd, kq->queue);
1593 qpd->is_debug = false;
1594 execute_queues_cpsch(dqm, KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES, 0);
1596 * Unconditionally decrement this counter, regardless of the queue's
1599 dqm->total_queue_count--;
1600 pr_debug("Total of %d queues are accountable so far\n",
1601 dqm->total_queue_count);
1605 static int create_queue_cpsch(struct device_queue_manager *dqm, struct queue *q,
1606 struct qcm_process_device *qpd,
1607 const struct kfd_criu_queue_priv_data *qd,
1608 const void *restore_mqd, const void *restore_ctl_stack)
1611 struct mqd_manager *mqd_mgr;
1613 if (dqm->total_queue_count >= max_num_of_queues_per_device) {
1614 pr_warn("Can't create new usermode queue because %d queues were already created\n",
1615 dqm->total_queue_count);
1620 if (q->properties.type == KFD_QUEUE_TYPE_SDMA ||
1621 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) {
1623 retval = allocate_sdma_queue(dqm, q, qd ? &qd->sdma_id : NULL);
1629 retval = allocate_doorbell(qpd, q, qd ? &qd->doorbell_id : NULL);
1631 goto out_deallocate_sdma_queue;
1633 mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type(
1634 q->properties.type)];
1636 if (q->properties.type == KFD_QUEUE_TYPE_SDMA ||
1637 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)
1638 dqm->asic_ops.init_sdma_vm(dqm, q, qpd);
1639 q->properties.tba_addr = qpd->tba_addr;
1640 q->properties.tma_addr = qpd->tma_addr;
1641 q->mqd_mem_obj = mqd_mgr->allocate_mqd(mqd_mgr->dev, &q->properties);
1642 if (!q->mqd_mem_obj) {
1644 goto out_deallocate_doorbell;
1649 * Eviction state logic: mark all queues as evicted, even ones
1650 * not currently active. Restoring inactive queues later only
1651 * updates the is_evicted flag but is a no-op otherwise.
1653 q->properties.is_evicted = !!qpd->evicted;
1656 mqd_mgr->restore_mqd(mqd_mgr, &q->mqd, q->mqd_mem_obj, &q->gart_mqd_addr,
1657 &q->properties, restore_mqd, restore_ctl_stack,
1658 qd->ctl_stack_size);
1660 mqd_mgr->init_mqd(mqd_mgr, &q->mqd, q->mqd_mem_obj,
1661 &q->gart_mqd_addr, &q->properties);
1663 list_add(&q->list, &qpd->queues_list);
1666 if (q->properties.is_active) {
1667 increment_queue_count(dqm, qpd, q);
1669 if (!dqm->dev->shared_resources.enable_mes) {
1670 retval = execute_queues_cpsch(dqm,
1671 KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 0);
1673 retval = add_queue_mes(dqm, q, qpd);
1680 * Unconditionally increment this counter, regardless of the queue's
1681 * type or whether the queue is active.
1683 dqm->total_queue_count++;
1685 pr_debug("Total of %d queues are accountable so far\n",
1686 dqm->total_queue_count);
1694 if (q->properties.is_active)
1695 decrement_queue_count(dqm, qpd, q);
1696 mqd_mgr->free_mqd(mqd_mgr, q->mqd, q->mqd_mem_obj);
1698 out_deallocate_doorbell:
1699 deallocate_doorbell(qpd, q);
1700 out_deallocate_sdma_queue:
1701 if (q->properties.type == KFD_QUEUE_TYPE_SDMA ||
1702 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) {
1704 deallocate_sdma_queue(dqm, q);
1711 int amdkfd_fence_wait_timeout(uint64_t *fence_addr,
1712 uint64_t fence_value,
1713 unsigned int timeout_ms)
1715 unsigned long end_jiffies = msecs_to_jiffies(timeout_ms) + jiffies;
1717 while (*fence_addr != fence_value) {
1718 if (time_after(jiffies, end_jiffies)) {
1719 pr_err("qcm fence wait loop timeout expired\n");
1720 /* In HWS case, this is used to halt the driver thread
1721 * in order not to mess up CP states before doing
1722 * scandumps for FW debugging.
1724 while (halt_if_hws_hang)
1735 /* dqm->lock mutex has to be locked before calling this function */
1736 static int map_queues_cpsch(struct device_queue_manager *dqm)
1740 if (!dqm->sched_running)
1742 if (dqm->active_queue_count <= 0 || dqm->processes_count <= 0)
1744 if (dqm->active_runlist)
1747 retval = pm_send_runlist(&dqm->packet_mgr, &dqm->queues);
1748 pr_debug("%s sent runlist\n", __func__);
1750 pr_err("failed to execute runlist\n");
1753 dqm->active_runlist = true;
1758 /* dqm->lock mutex has to be locked before calling this function */
1759 static int unmap_queues_cpsch(struct device_queue_manager *dqm,
1760 enum kfd_unmap_queues_filter filter,
1761 uint32_t filter_param, bool reset)
1764 struct mqd_manager *mqd_mgr;
1766 if (!dqm->sched_running)
1768 if (dqm->is_hws_hang || dqm->is_resetting)
1770 if (!dqm->active_runlist)
1773 retval = pm_send_unmap_queue(&dqm->packet_mgr, filter, filter_param, reset);
1777 *dqm->fence_addr = KFD_FENCE_INIT;
1778 pm_send_query_status(&dqm->packet_mgr, dqm->fence_gpu_addr,
1779 KFD_FENCE_COMPLETED);
1780 /* should be timed out */
1781 retval = amdkfd_fence_wait_timeout(dqm->fence_addr, KFD_FENCE_COMPLETED,
1782 queue_preemption_timeout_ms);
1784 pr_err("The cp might be in an unrecoverable state due to an unsuccessful queues preemption\n");
1789 /* In the current MEC firmware implementation, if compute queue
1790 * doesn't response to the preemption request in time, HIQ will
1791 * abandon the unmap request without returning any timeout error
1792 * to driver. Instead, MEC firmware will log the doorbell of the
1793 * unresponding compute queue to HIQ.MQD.queue_doorbell_id fields.
1794 * To make sure the queue unmap was successful, driver need to
1795 * check those fields
1797 mqd_mgr = dqm->mqd_mgrs[KFD_MQD_TYPE_HIQ];
1798 if (mqd_mgr->read_doorbell_id(dqm->packet_mgr.priv_queue->queue->mqd)) {
1799 pr_err("HIQ MQD's queue_doorbell_id0 is not 0, Queue preemption time out\n");
1800 while (halt_if_hws_hang)
1805 pm_release_ib(&dqm->packet_mgr);
1806 dqm->active_runlist = false;
1811 /* only for compute queue */
1812 static int reset_queues_cpsch(struct device_queue_manager *dqm,
1819 retval = unmap_queues_cpsch(dqm, KFD_UNMAP_QUEUES_FILTER_BY_PASID,
1826 /* dqm->lock mutex has to be locked before calling this function */
1827 static int execute_queues_cpsch(struct device_queue_manager *dqm,
1828 enum kfd_unmap_queues_filter filter,
1829 uint32_t filter_param)
1833 if (dqm->is_hws_hang)
1835 retval = unmap_queues_cpsch(dqm, filter, filter_param, false);
1839 return map_queues_cpsch(dqm);
1842 static int destroy_queue_cpsch(struct device_queue_manager *dqm,
1843 struct qcm_process_device *qpd,
1847 struct mqd_manager *mqd_mgr;
1848 uint64_t sdma_val = 0;
1849 struct kfd_process_device *pdd = qpd_to_pdd(qpd);
1851 /* Get the SDMA queue stats */
1852 if ((q->properties.type == KFD_QUEUE_TYPE_SDMA) ||
1853 (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)) {
1854 retval = read_sdma_queue_counter((uint64_t __user *)q->properties.read_ptr,
1857 pr_err("Failed to read SDMA queue counter for queue: %d\n",
1858 q->properties.queue_id);
1863 /* remove queue from list to prevent rescheduling after preemption */
1866 if (qpd->is_debug) {
1868 * error, currently we do not allow to destroy a queue
1869 * of a currently debugged process
1872 goto failed_try_destroy_debugged_queue;
1876 mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type(
1877 q->properties.type)];
1879 deallocate_doorbell(qpd, q);
1881 if ((q->properties.type == KFD_QUEUE_TYPE_SDMA) ||
1882 (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)) {
1883 deallocate_sdma_queue(dqm, q);
1884 pdd->sdma_past_activity_counter += sdma_val;
1889 if (q->properties.is_active) {
1890 if (!dqm->dev->shared_resources.enable_mes) {
1891 decrement_queue_count(dqm, qpd, q);
1892 retval = execute_queues_cpsch(dqm,
1893 KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 0);
1894 if (retval == -ETIME)
1895 qpd->reset_wavefronts = true;
1897 retval = remove_queue_mes(dqm, q, qpd);
1902 * Unconditionally decrement this counter, regardless of the queue's
1905 dqm->total_queue_count--;
1906 pr_debug("Total of %d queues are accountable so far\n",
1907 dqm->total_queue_count);
1911 /* Do free_mqd after dqm_unlock(dqm) to avoid circular locking */
1912 mqd_mgr->free_mqd(mqd_mgr, q->mqd, q->mqd_mem_obj);
1916 failed_try_destroy_debugged_queue:
1923 * Low bits must be 0000/FFFF as required by HW, high bits must be 0 to
1924 * stay in user mode.
1926 #define APE1_FIXED_BITS_MASK 0xFFFF80000000FFFFULL
1927 /* APE1 limit is inclusive and 64K aligned. */
1928 #define APE1_LIMIT_ALIGNMENT 0xFFFF
1930 static bool set_cache_memory_policy(struct device_queue_manager *dqm,
1931 struct qcm_process_device *qpd,
1932 enum cache_policy default_policy,
1933 enum cache_policy alternate_policy,
1934 void __user *alternate_aperture_base,
1935 uint64_t alternate_aperture_size)
1939 if (!dqm->asic_ops.set_cache_memory_policy)
1944 if (alternate_aperture_size == 0) {
1945 /* base > limit disables APE1 */
1946 qpd->sh_mem_ape1_base = 1;
1947 qpd->sh_mem_ape1_limit = 0;
1950 * In FSA64, APE1_Base[63:0] = { 16{SH_MEM_APE1_BASE[31]},
1951 * SH_MEM_APE1_BASE[31:0], 0x0000 }
1952 * APE1_Limit[63:0] = { 16{SH_MEM_APE1_LIMIT[31]},
1953 * SH_MEM_APE1_LIMIT[31:0], 0xFFFF }
1954 * Verify that the base and size parameters can be
1955 * represented in this format and convert them.
1956 * Additionally restrict APE1 to user-mode addresses.
1959 uint64_t base = (uintptr_t)alternate_aperture_base;
1960 uint64_t limit = base + alternate_aperture_size - 1;
1962 if (limit <= base || (base & APE1_FIXED_BITS_MASK) != 0 ||
1963 (limit & APE1_FIXED_BITS_MASK) != APE1_LIMIT_ALIGNMENT) {
1968 qpd->sh_mem_ape1_base = base >> 16;
1969 qpd->sh_mem_ape1_limit = limit >> 16;
1972 retval = dqm->asic_ops.set_cache_memory_policy(
1977 alternate_aperture_base,
1978 alternate_aperture_size);
1980 if ((dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS) && (qpd->vmid != 0))
1981 program_sh_mem_settings(dqm, qpd);
1983 pr_debug("sh_mem_config: 0x%x, ape1_base: 0x%x, ape1_limit: 0x%x\n",
1984 qpd->sh_mem_config, qpd->sh_mem_ape1_base,
1985 qpd->sh_mem_ape1_limit);
1992 static int process_termination_nocpsch(struct device_queue_manager *dqm,
1993 struct qcm_process_device *qpd)
1996 struct device_process_node *cur, *next_dpn;
2002 /* Clear all user mode queues */
2003 while (!list_empty(&qpd->queues_list)) {
2004 struct mqd_manager *mqd_mgr;
2007 q = list_first_entry(&qpd->queues_list, struct queue, list);
2008 mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type(
2009 q->properties.type)];
2010 ret = destroy_queue_nocpsch_locked(dqm, qpd, q);
2014 mqd_mgr->free_mqd(mqd_mgr, q->mqd, q->mqd_mem_obj);
2018 /* Unregister process */
2019 list_for_each_entry_safe(cur, next_dpn, &dqm->queues, list) {
2020 if (qpd == cur->qpd) {
2021 list_del(&cur->list);
2023 dqm->processes_count--;
2031 /* Outside the DQM lock because under the DQM lock we can't do
2032 * reclaim or take other locks that others hold while reclaiming.
2035 kfd_dec_compute_active(dqm->dev);
2040 static int get_wave_state(struct device_queue_manager *dqm,
2042 void __user *ctl_stack,
2043 u32 *ctl_stack_used_size,
2044 u32 *save_area_used_size)
2046 struct mqd_manager *mqd_mgr;
2050 mqd_mgr = dqm->mqd_mgrs[KFD_MQD_TYPE_CP];
2052 if (q->properties.type != KFD_QUEUE_TYPE_COMPUTE ||
2053 q->properties.is_active || !q->device->cwsr_enabled ||
2054 !mqd_mgr->get_wave_state) {
2062 * get_wave_state is outside the dqm lock to prevent circular locking
2063 * and the queue should be protected against destruction by the process
2066 return mqd_mgr->get_wave_state(mqd_mgr, q->mqd, ctl_stack,
2067 ctl_stack_used_size, save_area_used_size);
2070 static void get_queue_checkpoint_info(struct device_queue_manager *dqm,
2071 const struct queue *q,
2073 u32 *ctl_stack_size)
2075 struct mqd_manager *mqd_mgr;
2076 enum KFD_MQD_TYPE mqd_type =
2077 get_mqd_type_from_queue_type(q->properties.type);
2080 mqd_mgr = dqm->mqd_mgrs[mqd_type];
2081 *mqd_size = mqd_mgr->mqd_size;
2082 *ctl_stack_size = 0;
2084 if (q->properties.type == KFD_QUEUE_TYPE_COMPUTE && mqd_mgr->get_checkpoint_info)
2085 mqd_mgr->get_checkpoint_info(mqd_mgr, q->mqd, ctl_stack_size);
2090 static int checkpoint_mqd(struct device_queue_manager *dqm,
2091 const struct queue *q,
2095 struct mqd_manager *mqd_mgr;
2097 enum KFD_MQD_TYPE mqd_type =
2098 get_mqd_type_from_queue_type(q->properties.type);
2102 if (q->properties.is_active || !q->device->cwsr_enabled) {
2107 mqd_mgr = dqm->mqd_mgrs[mqd_type];
2108 if (!mqd_mgr->checkpoint_mqd) {
2113 mqd_mgr->checkpoint_mqd(mqd_mgr, q->mqd, mqd, ctl_stack);
2120 static int process_termination_cpsch(struct device_queue_manager *dqm,
2121 struct qcm_process_device *qpd)
2125 struct kernel_queue *kq, *kq_next;
2126 struct mqd_manager *mqd_mgr;
2127 struct device_process_node *cur, *next_dpn;
2128 enum kfd_unmap_queues_filter filter =
2129 KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES;
2136 /* Clean all kernel queues */
2137 list_for_each_entry_safe(kq, kq_next, &qpd->priv_queue_list, list) {
2138 list_del(&kq->list);
2139 decrement_queue_count(dqm, qpd, kq->queue);
2140 qpd->is_debug = false;
2141 dqm->total_queue_count--;
2142 filter = KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES;
2145 /* Clear all user mode queues */
2146 list_for_each_entry(q, &qpd->queues_list, list) {
2147 if (q->properties.type == KFD_QUEUE_TYPE_SDMA)
2148 deallocate_sdma_queue(dqm, q);
2149 else if (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)
2150 deallocate_sdma_queue(dqm, q);
2152 if (q->properties.is_active) {
2153 decrement_queue_count(dqm, qpd, q);
2155 if (dqm->dev->shared_resources.enable_mes) {
2156 retval = remove_queue_mes(dqm, q, qpd);
2158 pr_err("Failed to remove queue %d\n",
2159 q->properties.queue_id);
2163 dqm->total_queue_count--;
2166 /* Unregister process */
2167 list_for_each_entry_safe(cur, next_dpn, &dqm->queues, list) {
2168 if (qpd == cur->qpd) {
2169 list_del(&cur->list);
2171 dqm->processes_count--;
2177 if (!dqm->dev->shared_resources.enable_mes)
2178 retval = execute_queues_cpsch(dqm, filter, 0);
2180 if ((!dqm->is_hws_hang) && (retval || qpd->reset_wavefronts)) {
2181 pr_warn("Resetting wave fronts (cpsch) on dev %p\n", dqm->dev);
2182 dbgdev_wave_reset_wavefronts(dqm->dev, qpd->pqm->process);
2183 qpd->reset_wavefronts = false;
2186 /* Lastly, free mqd resources.
2187 * Do free_mqd() after dqm_unlock to avoid circular locking.
2189 while (!list_empty(&qpd->queues_list)) {
2190 q = list_first_entry(&qpd->queues_list, struct queue, list);
2191 mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type(
2192 q->properties.type)];
2196 mqd_mgr->free_mqd(mqd_mgr, q->mqd, q->mqd_mem_obj);
2201 /* Outside the DQM lock because under the DQM lock we can't do
2202 * reclaim or take other locks that others hold while reclaiming.
2205 kfd_dec_compute_active(dqm->dev);
2210 static int init_mqd_managers(struct device_queue_manager *dqm)
2213 struct mqd_manager *mqd_mgr;
2215 for (i = 0; i < KFD_MQD_TYPE_MAX; i++) {
2216 mqd_mgr = dqm->asic_ops.mqd_manager_init(i, dqm->dev);
2218 pr_err("mqd manager [%d] initialization failed\n", i);
2221 dqm->mqd_mgrs[i] = mqd_mgr;
2227 for (j = 0; j < i; j++) {
2228 kfree(dqm->mqd_mgrs[j]);
2229 dqm->mqd_mgrs[j] = NULL;
2235 /* Allocate one hiq mqd (HWS) and all SDMA mqd in a continuous trunk*/
2236 static int allocate_hiq_sdma_mqd(struct device_queue_manager *dqm)
2239 struct kfd_dev *dev = dqm->dev;
2240 struct kfd_mem_obj *mem_obj = &dqm->hiq_sdma_mqd;
2241 uint32_t size = dqm->mqd_mgrs[KFD_MQD_TYPE_SDMA]->mqd_size *
2242 get_num_all_sdma_engines(dqm) *
2243 dev->device_info.num_sdma_queues_per_engine +
2244 dqm->mqd_mgrs[KFD_MQD_TYPE_HIQ]->mqd_size;
2246 retval = amdgpu_amdkfd_alloc_gtt_mem(dev->adev, size,
2247 &(mem_obj->gtt_mem), &(mem_obj->gpu_addr),
2248 (void *)&(mem_obj->cpu_ptr), false);
2253 struct device_queue_manager *device_queue_manager_init(struct kfd_dev *dev)
2255 struct device_queue_manager *dqm;
2257 pr_debug("Loading device queue manager\n");
2259 dqm = kzalloc(sizeof(*dqm), GFP_KERNEL);
2263 switch (dev->adev->asic_type) {
2264 /* HWS is not available on Hawaii. */
2266 /* HWS depends on CWSR for timely dequeue. CWSR is not
2267 * available on Tonga.
2269 * FIXME: This argument also applies to Kaveri.
2272 dqm->sched_policy = KFD_SCHED_POLICY_NO_HWS;
2275 dqm->sched_policy = sched_policy;
2280 switch (dqm->sched_policy) {
2281 case KFD_SCHED_POLICY_HWS:
2282 case KFD_SCHED_POLICY_HWS_NO_OVERSUBSCRIPTION:
2283 /* initialize dqm for cp scheduling */
2284 dqm->ops.create_queue = create_queue_cpsch;
2285 dqm->ops.initialize = initialize_cpsch;
2286 dqm->ops.start = start_cpsch;
2287 dqm->ops.stop = stop_cpsch;
2288 dqm->ops.pre_reset = pre_reset;
2289 dqm->ops.destroy_queue = destroy_queue_cpsch;
2290 dqm->ops.update_queue = update_queue;
2291 dqm->ops.register_process = register_process;
2292 dqm->ops.unregister_process = unregister_process;
2293 dqm->ops.uninitialize = uninitialize;
2294 dqm->ops.create_kernel_queue = create_kernel_queue_cpsch;
2295 dqm->ops.destroy_kernel_queue = destroy_kernel_queue_cpsch;
2296 dqm->ops.set_cache_memory_policy = set_cache_memory_policy;
2297 dqm->ops.process_termination = process_termination_cpsch;
2298 dqm->ops.evict_process_queues = evict_process_queues_cpsch;
2299 dqm->ops.restore_process_queues = restore_process_queues_cpsch;
2300 dqm->ops.get_wave_state = get_wave_state;
2301 dqm->ops.reset_queues = reset_queues_cpsch;
2302 dqm->ops.get_queue_checkpoint_info = get_queue_checkpoint_info;
2303 dqm->ops.checkpoint_mqd = checkpoint_mqd;
2305 case KFD_SCHED_POLICY_NO_HWS:
2306 /* initialize dqm for no cp scheduling */
2307 dqm->ops.start = start_nocpsch;
2308 dqm->ops.stop = stop_nocpsch;
2309 dqm->ops.pre_reset = pre_reset;
2310 dqm->ops.create_queue = create_queue_nocpsch;
2311 dqm->ops.destroy_queue = destroy_queue_nocpsch;
2312 dqm->ops.update_queue = update_queue;
2313 dqm->ops.register_process = register_process;
2314 dqm->ops.unregister_process = unregister_process;
2315 dqm->ops.initialize = initialize_nocpsch;
2316 dqm->ops.uninitialize = uninitialize;
2317 dqm->ops.set_cache_memory_policy = set_cache_memory_policy;
2318 dqm->ops.process_termination = process_termination_nocpsch;
2319 dqm->ops.evict_process_queues = evict_process_queues_nocpsch;
2320 dqm->ops.restore_process_queues =
2321 restore_process_queues_nocpsch;
2322 dqm->ops.get_wave_state = get_wave_state;
2323 dqm->ops.get_queue_checkpoint_info = get_queue_checkpoint_info;
2324 dqm->ops.checkpoint_mqd = checkpoint_mqd;
2327 pr_err("Invalid scheduling policy %d\n", dqm->sched_policy);
2331 switch (dev->adev->asic_type) {
2333 device_queue_manager_init_vi(&dqm->asic_ops);
2337 device_queue_manager_init_cik(&dqm->asic_ops);
2341 device_queue_manager_init_cik_hawaii(&dqm->asic_ops);
2346 case CHIP_POLARIS10:
2347 case CHIP_POLARIS11:
2348 case CHIP_POLARIS12:
2350 device_queue_manager_init_vi_tonga(&dqm->asic_ops);
2354 if (KFD_GC_VERSION(dev) >= IP_VERSION(11, 0, 0))
2355 device_queue_manager_init_v11(&dqm->asic_ops);
2356 else if (KFD_GC_VERSION(dev) >= IP_VERSION(10, 1, 1))
2357 device_queue_manager_init_v10_navi10(&dqm->asic_ops);
2358 else if (KFD_GC_VERSION(dev) >= IP_VERSION(9, 0, 1))
2359 device_queue_manager_init_v9(&dqm->asic_ops);
2361 WARN(1, "Unexpected ASIC family %u",
2362 dev->adev->asic_type);
2367 if (init_mqd_managers(dqm))
2370 if (allocate_hiq_sdma_mqd(dqm)) {
2371 pr_err("Failed to allocate hiq sdma mqd trunk buffer\n");
2375 if (!dqm->ops.initialize(dqm))
2383 static void deallocate_hiq_sdma_mqd(struct kfd_dev *dev,
2384 struct kfd_mem_obj *mqd)
2386 WARN(!mqd, "No hiq sdma mqd trunk to free");
2388 amdgpu_amdkfd_free_gtt_mem(dev->adev, mqd->gtt_mem);
2391 void device_queue_manager_uninit(struct device_queue_manager *dqm)
2393 dqm->ops.uninitialize(dqm);
2394 deallocate_hiq_sdma_mqd(dqm->dev, &dqm->hiq_sdma_mqd);
2398 int kfd_dqm_evict_pasid(struct device_queue_manager *dqm, u32 pasid)
2400 struct kfd_process_device *pdd;
2401 struct kfd_process *p = kfd_lookup_process_by_pasid(pasid);
2406 WARN(debug_evictions, "Evicting pid %d", p->lead_thread->pid);
2407 pdd = kfd_get_process_device_data(dqm->dev, p);
2409 ret = dqm->ops.evict_process_queues(dqm, &pdd->qpd);
2410 kfd_unref_process(p);
2415 static void kfd_process_hw_exception(struct work_struct *work)
2417 struct device_queue_manager *dqm = container_of(work,
2418 struct device_queue_manager, hw_exception_work);
2419 amdgpu_amdkfd_gpu_reset(dqm->dev->adev);
2422 #if defined(CONFIG_DEBUG_FS)
2424 static void seq_reg_dump(struct seq_file *m,
2425 uint32_t (*dump)[2], uint32_t n_regs)
2429 for (i = 0, count = 0; i < n_regs; i++) {
2431 dump[i-1][0] + sizeof(uint32_t) != dump[i][0]) {
2432 seq_printf(m, "%s %08x: %08x",
2434 dump[i][0], dump[i][1]);
2437 seq_printf(m, " %08x", dump[i][1]);
2445 int dqm_debugfs_hqds(struct seq_file *m, void *data)
2447 struct device_queue_manager *dqm = data;
2448 uint32_t (*dump)[2], n_regs;
2452 if (!dqm->sched_running) {
2453 seq_puts(m, " Device is stopped\n");
2457 r = dqm->dev->kfd2kgd->hqd_dump(dqm->dev->adev,
2458 KFD_CIK_HIQ_PIPE, KFD_CIK_HIQ_QUEUE,
2461 seq_printf(m, " HIQ on MEC %d Pipe %d Queue %d\n",
2462 KFD_CIK_HIQ_PIPE/get_pipes_per_mec(dqm)+1,
2463 KFD_CIK_HIQ_PIPE%get_pipes_per_mec(dqm),
2465 seq_reg_dump(m, dump, n_regs);
2470 for (pipe = 0; pipe < get_pipes_per_mec(dqm); pipe++) {
2471 int pipe_offset = pipe * get_queues_per_pipe(dqm);
2473 for (queue = 0; queue < get_queues_per_pipe(dqm); queue++) {
2474 if (!test_bit(pipe_offset + queue,
2475 dqm->dev->shared_resources.cp_queue_bitmap))
2478 r = dqm->dev->kfd2kgd->hqd_dump(
2479 dqm->dev->adev, pipe, queue, &dump, &n_regs);
2483 seq_printf(m, " CP Pipe %d, Queue %d\n",
2485 seq_reg_dump(m, dump, n_regs);
2491 for (pipe = 0; pipe < get_num_all_sdma_engines(dqm); pipe++) {
2493 queue < dqm->dev->device_info.num_sdma_queues_per_engine;
2495 r = dqm->dev->kfd2kgd->hqd_sdma_dump(
2496 dqm->dev->adev, pipe, queue, &dump, &n_regs);
2500 seq_printf(m, " SDMA Engine %d, RLC %d\n",
2502 seq_reg_dump(m, dump, n_regs);
2511 int dqm_debugfs_hang_hws(struct device_queue_manager *dqm)
2516 r = pm_debugfs_hang_hws(&dqm->packet_mgr);
2521 dqm->active_runlist = true;
2522 r = execute_queues_cpsch(dqm, KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES, 0);