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"
39 /* Size of the per-pipe EOP queue */
40 #define CIK_HPD_EOP_BYTES_LOG2 11
41 #define CIK_HPD_EOP_BYTES (1U << CIK_HPD_EOP_BYTES_LOG2)
43 static int set_pasid_vmid_mapping(struct device_queue_manager *dqm,
44 u32 pasid, unsigned int vmid);
46 static int execute_queues_cpsch(struct device_queue_manager *dqm,
47 enum kfd_unmap_queues_filter filter,
48 uint32_t filter_param);
49 static int unmap_queues_cpsch(struct device_queue_manager *dqm,
50 enum kfd_unmap_queues_filter filter,
51 uint32_t filter_param, bool reset);
53 static int map_queues_cpsch(struct device_queue_manager *dqm);
55 static void deallocate_sdma_queue(struct device_queue_manager *dqm,
58 static inline void deallocate_hqd(struct device_queue_manager *dqm,
60 static int allocate_hqd(struct device_queue_manager *dqm, struct queue *q);
61 static int allocate_sdma_queue(struct device_queue_manager *dqm,
62 struct queue *q, const uint32_t *restore_sdma_id);
63 static void kfd_process_hw_exception(struct work_struct *work);
66 enum KFD_MQD_TYPE get_mqd_type_from_queue_type(enum kfd_queue_type type)
68 if (type == KFD_QUEUE_TYPE_SDMA || type == KFD_QUEUE_TYPE_SDMA_XGMI)
69 return KFD_MQD_TYPE_SDMA;
70 return KFD_MQD_TYPE_CP;
73 static bool is_pipe_enabled(struct device_queue_manager *dqm, int mec, int pipe)
76 int pipe_offset = (mec * dqm->dev->shared_resources.num_pipe_per_mec
77 + pipe) * dqm->dev->shared_resources.num_queue_per_pipe;
79 /* queue is available for KFD usage if bit is 1 */
80 for (i = 0; i < dqm->dev->shared_resources.num_queue_per_pipe; ++i)
81 if (test_bit(pipe_offset + i,
82 dqm->dev->shared_resources.cp_queue_bitmap))
87 unsigned int get_cp_queues_num(struct device_queue_manager *dqm)
89 return bitmap_weight(dqm->dev->shared_resources.cp_queue_bitmap,
93 unsigned int get_queues_per_pipe(struct device_queue_manager *dqm)
95 return dqm->dev->shared_resources.num_queue_per_pipe;
98 unsigned int get_pipes_per_mec(struct device_queue_manager *dqm)
100 return dqm->dev->shared_resources.num_pipe_per_mec;
103 static unsigned int get_num_all_sdma_engines(struct device_queue_manager *dqm)
105 return kfd_get_num_sdma_engines(dqm->dev) +
106 kfd_get_num_xgmi_sdma_engines(dqm->dev);
109 unsigned int get_num_sdma_queues(struct device_queue_manager *dqm)
111 return kfd_get_num_sdma_engines(dqm->dev) *
112 dqm->dev->device_info.num_sdma_queues_per_engine;
115 unsigned int get_num_xgmi_sdma_queues(struct device_queue_manager *dqm)
117 return kfd_get_num_xgmi_sdma_engines(dqm->dev) *
118 dqm->dev->device_info.num_sdma_queues_per_engine;
121 void program_sh_mem_settings(struct device_queue_manager *dqm,
122 struct qcm_process_device *qpd)
124 return dqm->dev->kfd2kgd->program_sh_mem_settings(
125 dqm->dev->adev, qpd->vmid,
127 qpd->sh_mem_ape1_base,
128 qpd->sh_mem_ape1_limit,
132 static void increment_queue_count(struct device_queue_manager *dqm,
133 struct qcm_process_device *qpd,
136 dqm->active_queue_count++;
137 if (q->properties.type == KFD_QUEUE_TYPE_COMPUTE ||
138 q->properties.type == KFD_QUEUE_TYPE_DIQ)
139 dqm->active_cp_queue_count++;
141 if (q->properties.is_gws) {
142 dqm->gws_queue_count++;
143 qpd->mapped_gws_queue = true;
147 static void decrement_queue_count(struct device_queue_manager *dqm,
148 struct qcm_process_device *qpd,
151 dqm->active_queue_count--;
152 if (q->properties.type == KFD_QUEUE_TYPE_COMPUTE ||
153 q->properties.type == KFD_QUEUE_TYPE_DIQ)
154 dqm->active_cp_queue_count--;
156 if (q->properties.is_gws) {
157 dqm->gws_queue_count--;
158 qpd->mapped_gws_queue = false;
163 * Allocate a doorbell ID to this queue.
164 * If doorbell_id is passed in, make sure requested ID is valid then allocate it.
166 static int allocate_doorbell(struct qcm_process_device *qpd,
168 uint32_t const *restore_id)
170 struct kfd_dev *dev = qpd->dqm->dev;
172 if (!KFD_IS_SOC15(dev)) {
173 /* On pre-SOC15 chips we need to use the queue ID to
174 * preserve the user mode ABI.
177 if (restore_id && *restore_id != q->properties.queue_id)
180 q->doorbell_id = q->properties.queue_id;
181 } else if (q->properties.type == KFD_QUEUE_TYPE_SDMA ||
182 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) {
183 /* For SDMA queues on SOC15 with 8-byte doorbell, use static
184 * doorbell assignments based on the engine and queue id.
185 * The doobell index distance between RLC (2*i) and (2*i+1)
186 * for a SDMA engine is 512.
189 uint32_t *idx_offset = dev->shared_resources.sdma_doorbell_idx;
190 uint32_t valid_id = idx_offset[q->properties.sdma_engine_id]
191 + (q->properties.sdma_queue_id & 1)
192 * KFD_QUEUE_DOORBELL_MIRROR_OFFSET
193 + (q->properties.sdma_queue_id >> 1);
195 if (restore_id && *restore_id != valid_id)
197 q->doorbell_id = valid_id;
199 /* For CP queues on SOC15 */
201 /* make sure that ID is free */
202 if (__test_and_set_bit(*restore_id, qpd->doorbell_bitmap))
205 q->doorbell_id = *restore_id;
207 /* or reserve a free doorbell ID */
210 found = find_first_zero_bit(qpd->doorbell_bitmap,
211 KFD_MAX_NUM_OF_QUEUES_PER_PROCESS);
212 if (found >= KFD_MAX_NUM_OF_QUEUES_PER_PROCESS) {
213 pr_debug("No doorbells available");
216 set_bit(found, qpd->doorbell_bitmap);
217 q->doorbell_id = found;
221 q->properties.doorbell_off =
222 kfd_get_doorbell_dw_offset_in_bar(dev, qpd_to_pdd(qpd),
227 static void deallocate_doorbell(struct qcm_process_device *qpd,
231 struct kfd_dev *dev = qpd->dqm->dev;
233 if (!KFD_IS_SOC15(dev) ||
234 q->properties.type == KFD_QUEUE_TYPE_SDMA ||
235 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)
238 old = test_and_clear_bit(q->doorbell_id, qpd->doorbell_bitmap);
242 static void program_trap_handler_settings(struct device_queue_manager *dqm,
243 struct qcm_process_device *qpd)
245 if (dqm->dev->kfd2kgd->program_trap_handler_settings)
246 dqm->dev->kfd2kgd->program_trap_handler_settings(
247 dqm->dev->adev, qpd->vmid,
248 qpd->tba_addr, qpd->tma_addr);
251 static int allocate_vmid(struct device_queue_manager *dqm,
252 struct qcm_process_device *qpd,
255 int allocated_vmid = -1, i;
257 for (i = dqm->dev->vm_info.first_vmid_kfd;
258 i <= dqm->dev->vm_info.last_vmid_kfd; i++) {
259 if (!dqm->vmid_pasid[i]) {
265 if (allocated_vmid < 0) {
266 pr_err("no more vmid to allocate\n");
270 pr_debug("vmid allocated: %d\n", allocated_vmid);
272 dqm->vmid_pasid[allocated_vmid] = q->process->pasid;
274 set_pasid_vmid_mapping(dqm, q->process->pasid, allocated_vmid);
276 qpd->vmid = allocated_vmid;
277 q->properties.vmid = allocated_vmid;
279 program_sh_mem_settings(dqm, qpd);
281 if (KFD_IS_SOC15(dqm->dev) && dqm->dev->cwsr_enabled)
282 program_trap_handler_settings(dqm, qpd);
284 /* qpd->page_table_base is set earlier when register_process()
285 * is called, i.e. when the first queue is created.
287 dqm->dev->kfd2kgd->set_vm_context_page_table_base(dqm->dev->adev,
289 qpd->page_table_base);
290 /* invalidate the VM context after pasid and vmid mapping is set up */
291 kfd_flush_tlb(qpd_to_pdd(qpd), TLB_FLUSH_LEGACY);
293 if (dqm->dev->kfd2kgd->set_scratch_backing_va)
294 dqm->dev->kfd2kgd->set_scratch_backing_va(dqm->dev->adev,
295 qpd->sh_hidden_private_base, qpd->vmid);
300 static int flush_texture_cache_nocpsch(struct kfd_dev *kdev,
301 struct qcm_process_device *qpd)
303 const struct packet_manager_funcs *pmf = qpd->dqm->packet_mgr.pmf;
309 ret = pmf->release_mem(qpd->ib_base, (uint32_t *)qpd->ib_kaddr);
313 return amdgpu_amdkfd_submit_ib(kdev->adev, KGD_ENGINE_MEC1, qpd->vmid,
314 qpd->ib_base, (uint32_t *)qpd->ib_kaddr,
315 pmf->release_mem_size / sizeof(uint32_t));
318 static void deallocate_vmid(struct device_queue_manager *dqm,
319 struct qcm_process_device *qpd,
322 /* On GFX v7, CP doesn't flush TC at dequeue */
323 if (q->device->adev->asic_type == CHIP_HAWAII)
324 if (flush_texture_cache_nocpsch(q->device, qpd))
325 pr_err("Failed to flush TC\n");
327 kfd_flush_tlb(qpd_to_pdd(qpd), TLB_FLUSH_LEGACY);
329 /* Release the vmid mapping */
330 set_pasid_vmid_mapping(dqm, 0, qpd->vmid);
331 dqm->vmid_pasid[qpd->vmid] = 0;
334 q->properties.vmid = 0;
337 static int create_queue_nocpsch(struct device_queue_manager *dqm,
339 struct qcm_process_device *qpd,
340 const struct kfd_criu_queue_priv_data *qd,
341 const void *restore_mqd, const void *restore_ctl_stack)
343 struct mqd_manager *mqd_mgr;
348 if (dqm->total_queue_count >= max_num_of_queues_per_device) {
349 pr_warn("Can't create new usermode queue because %d queues were already created\n",
350 dqm->total_queue_count);
355 if (list_empty(&qpd->queues_list)) {
356 retval = allocate_vmid(dqm, qpd, q);
360 q->properties.vmid = qpd->vmid;
362 * Eviction state logic: mark all queues as evicted, even ones
363 * not currently active. Restoring inactive queues later only
364 * updates the is_evicted flag but is a no-op otherwise.
366 q->properties.is_evicted = !!qpd->evicted;
368 q->properties.tba_addr = qpd->tba_addr;
369 q->properties.tma_addr = qpd->tma_addr;
371 mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type(
372 q->properties.type)];
373 if (q->properties.type == KFD_QUEUE_TYPE_COMPUTE) {
374 retval = allocate_hqd(dqm, q);
376 goto deallocate_vmid;
377 pr_debug("Loading mqd to hqd on pipe %d, queue %d\n",
379 } else if (q->properties.type == KFD_QUEUE_TYPE_SDMA ||
380 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) {
381 retval = allocate_sdma_queue(dqm, q, qd ? &qd->sdma_id : NULL);
383 goto deallocate_vmid;
384 dqm->asic_ops.init_sdma_vm(dqm, q, qpd);
387 retval = allocate_doorbell(qpd, q, qd ? &qd->doorbell_id : NULL);
389 goto out_deallocate_hqd;
391 /* Temporarily release dqm lock to avoid a circular lock dependency */
393 q->mqd_mem_obj = mqd_mgr->allocate_mqd(mqd_mgr->dev, &q->properties);
396 if (!q->mqd_mem_obj) {
398 goto out_deallocate_doorbell;
402 mqd_mgr->restore_mqd(mqd_mgr, &q->mqd, q->mqd_mem_obj, &q->gart_mqd_addr,
403 &q->properties, restore_mqd, restore_ctl_stack,
406 mqd_mgr->init_mqd(mqd_mgr, &q->mqd, q->mqd_mem_obj,
407 &q->gart_mqd_addr, &q->properties);
409 if (q->properties.is_active) {
410 if (!dqm->sched_running) {
411 WARN_ONCE(1, "Load non-HWS mqd while stopped\n");
412 goto add_queue_to_list;
415 if (WARN(q->process->mm != current->mm,
416 "should only run in user thread"))
419 retval = mqd_mgr->load_mqd(mqd_mgr, q->mqd, q->pipe,
420 q->queue, &q->properties, current->mm);
426 list_add(&q->list, &qpd->queues_list);
428 if (q->properties.is_active)
429 increment_queue_count(dqm, qpd, q);
432 * Unconditionally increment this counter, regardless of the queue's
433 * type or whether the queue is active.
435 dqm->total_queue_count++;
436 pr_debug("Total of %d queues are accountable so far\n",
437 dqm->total_queue_count);
441 mqd_mgr->free_mqd(mqd_mgr, q->mqd, q->mqd_mem_obj);
442 out_deallocate_doorbell:
443 deallocate_doorbell(qpd, q);
445 if (q->properties.type == KFD_QUEUE_TYPE_COMPUTE)
446 deallocate_hqd(dqm, q);
447 else if (q->properties.type == KFD_QUEUE_TYPE_SDMA ||
448 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)
449 deallocate_sdma_queue(dqm, q);
451 if (list_empty(&qpd->queues_list))
452 deallocate_vmid(dqm, qpd, q);
458 static int allocate_hqd(struct device_queue_manager *dqm, struct queue *q)
465 for (pipe = dqm->next_pipe_to_allocate, i = 0;
466 i < get_pipes_per_mec(dqm);
467 pipe = ((pipe + 1) % get_pipes_per_mec(dqm)), ++i) {
469 if (!is_pipe_enabled(dqm, 0, pipe))
472 if (dqm->allocated_queues[pipe] != 0) {
473 bit = ffs(dqm->allocated_queues[pipe]) - 1;
474 dqm->allocated_queues[pipe] &= ~(1 << bit);
485 pr_debug("hqd slot - pipe %d, queue %d\n", q->pipe, q->queue);
486 /* horizontal hqd allocation */
487 dqm->next_pipe_to_allocate = (pipe + 1) % get_pipes_per_mec(dqm);
492 static inline void deallocate_hqd(struct device_queue_manager *dqm,
495 dqm->allocated_queues[q->pipe] |= (1 << q->queue);
498 #define SQ_IND_CMD_CMD_KILL 0x00000003
499 #define SQ_IND_CMD_MODE_BROADCAST 0x00000001
501 static int dbgdev_wave_reset_wavefronts(struct kfd_dev *dev, struct kfd_process *p)
505 uint16_t queried_pasid;
506 union SQ_CMD_BITS reg_sq_cmd;
507 union GRBM_GFX_INDEX_BITS reg_gfx_index;
508 struct kfd_process_device *pdd;
509 int first_vmid_to_scan = dev->vm_info.first_vmid_kfd;
510 int last_vmid_to_scan = dev->vm_info.last_vmid_kfd;
512 reg_sq_cmd.u32All = 0;
513 reg_gfx_index.u32All = 0;
515 pr_debug("Killing all process wavefronts\n");
517 if (!dev->kfd2kgd->get_atc_vmid_pasid_mapping_info) {
518 pr_err("no vmid pasid mapping supported \n");
522 /* Scan all registers in the range ATC_VMID8_PASID_MAPPING ..
523 * ATC_VMID15_PASID_MAPPING
524 * to check which VMID the current process is mapped to.
527 for (vmid = first_vmid_to_scan; vmid <= last_vmid_to_scan; vmid++) {
528 status = dev->kfd2kgd->get_atc_vmid_pasid_mapping_info
529 (dev->adev, vmid, &queried_pasid);
531 if (status && queried_pasid == p->pasid) {
532 pr_debug("Killing wave fronts of vmid %d and pasid 0x%x\n",
538 if (vmid > last_vmid_to_scan) {
539 pr_err("Didn't find vmid for pasid 0x%x\n", p->pasid);
543 /* taking the VMID for that process on the safe way using PDD */
544 pdd = kfd_get_process_device_data(dev, p);
548 reg_gfx_index.bits.sh_broadcast_writes = 1;
549 reg_gfx_index.bits.se_broadcast_writes = 1;
550 reg_gfx_index.bits.instance_broadcast_writes = 1;
551 reg_sq_cmd.bits.mode = SQ_IND_CMD_MODE_BROADCAST;
552 reg_sq_cmd.bits.cmd = SQ_IND_CMD_CMD_KILL;
553 reg_sq_cmd.bits.vm_id = vmid;
555 dev->kfd2kgd->wave_control_execute(dev->adev,
556 reg_gfx_index.u32All,
562 /* Access to DQM has to be locked before calling destroy_queue_nocpsch_locked
563 * to avoid asynchronized access
565 static int destroy_queue_nocpsch_locked(struct device_queue_manager *dqm,
566 struct qcm_process_device *qpd,
570 struct mqd_manager *mqd_mgr;
572 mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type(
573 q->properties.type)];
575 if (q->properties.type == KFD_QUEUE_TYPE_COMPUTE)
576 deallocate_hqd(dqm, q);
577 else if (q->properties.type == KFD_QUEUE_TYPE_SDMA)
578 deallocate_sdma_queue(dqm, q);
579 else if (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)
580 deallocate_sdma_queue(dqm, q);
582 pr_debug("q->properties.type %d is invalid\n",
586 dqm->total_queue_count--;
588 deallocate_doorbell(qpd, q);
590 if (!dqm->sched_running) {
591 WARN_ONCE(1, "Destroy non-HWS queue while stopped\n");
595 retval = mqd_mgr->destroy_mqd(mqd_mgr, q->mqd,
596 KFD_PREEMPT_TYPE_WAVEFRONT_RESET,
597 KFD_UNMAP_LATENCY_MS,
599 if (retval == -ETIME)
600 qpd->reset_wavefronts = true;
603 if (list_empty(&qpd->queues_list)) {
604 if (qpd->reset_wavefronts) {
605 pr_warn("Resetting wave fronts (nocpsch) on dev %p\n",
607 /* dbgdev_wave_reset_wavefronts has to be called before
608 * deallocate_vmid(), i.e. when vmid is still in use.
610 dbgdev_wave_reset_wavefronts(dqm->dev,
612 qpd->reset_wavefronts = false;
615 deallocate_vmid(dqm, qpd, q);
618 if (q->properties.is_active)
619 decrement_queue_count(dqm, qpd, q);
624 static int destroy_queue_nocpsch(struct device_queue_manager *dqm,
625 struct qcm_process_device *qpd,
629 uint64_t sdma_val = 0;
630 struct kfd_process_device *pdd = qpd_to_pdd(qpd);
631 struct mqd_manager *mqd_mgr =
632 dqm->mqd_mgrs[get_mqd_type_from_queue_type(q->properties.type)];
634 /* Get the SDMA queue stats */
635 if ((q->properties.type == KFD_QUEUE_TYPE_SDMA) ||
636 (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)) {
637 retval = read_sdma_queue_counter((uint64_t __user *)q->properties.read_ptr,
640 pr_err("Failed to read SDMA queue counter for queue: %d\n",
641 q->properties.queue_id);
645 retval = destroy_queue_nocpsch_locked(dqm, qpd, q);
647 pdd->sdma_past_activity_counter += sdma_val;
650 mqd_mgr->free_mqd(mqd_mgr, q->mqd, q->mqd_mem_obj);
655 static int update_queue(struct device_queue_manager *dqm, struct queue *q,
656 struct mqd_update_info *minfo)
659 struct mqd_manager *mqd_mgr;
660 struct kfd_process_device *pdd;
661 bool prev_active = false;
664 pdd = kfd_get_process_device_data(q->device, q->process);
669 mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type(
670 q->properties.type)];
672 /* Save previous activity state for counters */
673 prev_active = q->properties.is_active;
675 /* Make sure the queue is unmapped before updating the MQD */
676 if (dqm->sched_policy != KFD_SCHED_POLICY_NO_HWS) {
677 retval = unmap_queues_cpsch(dqm,
678 KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 0, false);
680 pr_err("unmap queue failed\n");
683 } else if (prev_active &&
684 (q->properties.type == KFD_QUEUE_TYPE_COMPUTE ||
685 q->properties.type == KFD_QUEUE_TYPE_SDMA ||
686 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)) {
688 if (!dqm->sched_running) {
689 WARN_ONCE(1, "Update non-HWS queue while stopped\n");
693 retval = mqd_mgr->destroy_mqd(mqd_mgr, q->mqd,
694 (dqm->dev->cwsr_enabled ?
695 KFD_PREEMPT_TYPE_WAVEFRONT_SAVE :
696 KFD_PREEMPT_TYPE_WAVEFRONT_DRAIN),
697 KFD_UNMAP_LATENCY_MS, q->pipe, q->queue);
699 pr_err("destroy mqd failed\n");
704 mqd_mgr->update_mqd(mqd_mgr, q->mqd, &q->properties, minfo);
707 * check active state vs. the previous state and modify
708 * counter accordingly. map_queues_cpsch uses the
709 * dqm->active_queue_count to determine whether a new runlist must be
712 if (q->properties.is_active && !prev_active) {
713 increment_queue_count(dqm, &pdd->qpd, q);
714 } else if (!q->properties.is_active && prev_active) {
715 decrement_queue_count(dqm, &pdd->qpd, q);
716 } else if (q->gws && !q->properties.is_gws) {
717 if (q->properties.is_active) {
718 dqm->gws_queue_count++;
719 pdd->qpd.mapped_gws_queue = true;
721 q->properties.is_gws = true;
722 } else if (!q->gws && q->properties.is_gws) {
723 if (q->properties.is_active) {
724 dqm->gws_queue_count--;
725 pdd->qpd.mapped_gws_queue = false;
727 q->properties.is_gws = false;
730 if (dqm->sched_policy != KFD_SCHED_POLICY_NO_HWS)
731 retval = map_queues_cpsch(dqm);
732 else if (q->properties.is_active &&
733 (q->properties.type == KFD_QUEUE_TYPE_COMPUTE ||
734 q->properties.type == KFD_QUEUE_TYPE_SDMA ||
735 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)) {
736 if (WARN(q->process->mm != current->mm,
737 "should only run in user thread"))
740 retval = mqd_mgr->load_mqd(mqd_mgr, q->mqd,
742 &q->properties, current->mm);
750 static int evict_process_queues_nocpsch(struct device_queue_manager *dqm,
751 struct qcm_process_device *qpd)
754 struct mqd_manager *mqd_mgr;
755 struct kfd_process_device *pdd;
759 if (qpd->evicted++ > 0) /* already evicted, do nothing */
762 pdd = qpd_to_pdd(qpd);
763 pr_debug_ratelimited("Evicting PASID 0x%x queues\n",
764 pdd->process->pasid);
766 pdd->last_evict_timestamp = get_jiffies_64();
767 /* Mark all queues as evicted. Deactivate all active queues on
770 list_for_each_entry(q, &qpd->queues_list, list) {
771 q->properties.is_evicted = true;
772 if (!q->properties.is_active)
775 mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type(
776 q->properties.type)];
777 q->properties.is_active = false;
778 decrement_queue_count(dqm, qpd, q);
780 if (WARN_ONCE(!dqm->sched_running, "Evict when stopped\n"))
783 retval = mqd_mgr->destroy_mqd(mqd_mgr, q->mqd,
784 (dqm->dev->cwsr_enabled ?
785 KFD_PREEMPT_TYPE_WAVEFRONT_SAVE :
786 KFD_PREEMPT_TYPE_WAVEFRONT_DRAIN),
787 KFD_UNMAP_LATENCY_MS, q->pipe, q->queue);
789 /* Return the first error, but keep going to
790 * maintain a consistent eviction state
800 static int evict_process_queues_cpsch(struct device_queue_manager *dqm,
801 struct qcm_process_device *qpd)
804 struct kfd_process_device *pdd;
808 if (qpd->evicted++ > 0) /* already evicted, do nothing */
811 pdd = qpd_to_pdd(qpd);
812 pr_debug_ratelimited("Evicting PASID 0x%x queues\n",
813 pdd->process->pasid);
815 /* Mark all queues as evicted. Deactivate all active queues on
818 list_for_each_entry(q, &qpd->queues_list, list) {
819 q->properties.is_evicted = true;
820 if (!q->properties.is_active)
823 q->properties.is_active = false;
824 decrement_queue_count(dqm, qpd, q);
826 pdd->last_evict_timestamp = get_jiffies_64();
827 retval = execute_queues_cpsch(dqm,
829 KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES :
830 KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 0);
837 static int restore_process_queues_nocpsch(struct device_queue_manager *dqm,
838 struct qcm_process_device *qpd)
840 struct mm_struct *mm = NULL;
842 struct mqd_manager *mqd_mgr;
843 struct kfd_process_device *pdd;
845 uint64_t eviction_duration;
848 pdd = qpd_to_pdd(qpd);
849 /* Retrieve PD base */
850 pd_base = amdgpu_amdkfd_gpuvm_get_process_page_dir(pdd->drm_priv);
853 if (WARN_ON_ONCE(!qpd->evicted)) /* already restored, do nothing */
855 if (qpd->evicted > 1) { /* ref count still > 0, decrement & quit */
860 pr_debug_ratelimited("Restoring PASID 0x%x queues\n",
861 pdd->process->pasid);
863 /* Update PD Base in QPD */
864 qpd->page_table_base = pd_base;
865 pr_debug("Updated PD address to 0x%llx\n", pd_base);
867 if (!list_empty(&qpd->queues_list)) {
868 dqm->dev->kfd2kgd->set_vm_context_page_table_base(
871 qpd->page_table_base);
872 kfd_flush_tlb(pdd, TLB_FLUSH_LEGACY);
875 /* Take a safe reference to the mm_struct, which may otherwise
876 * disappear even while the kfd_process is still referenced.
878 mm = get_task_mm(pdd->process->lead_thread);
884 /* Remove the eviction flags. Activate queues that are not
885 * inactive for other reasons.
887 list_for_each_entry(q, &qpd->queues_list, list) {
888 q->properties.is_evicted = false;
889 if (!QUEUE_IS_ACTIVE(q->properties))
892 mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type(
893 q->properties.type)];
894 q->properties.is_active = true;
895 increment_queue_count(dqm, qpd, q);
897 if (WARN_ONCE(!dqm->sched_running, "Restore when stopped\n"))
900 retval = mqd_mgr->load_mqd(mqd_mgr, q->mqd, q->pipe,
901 q->queue, &q->properties, mm);
903 /* Return the first error, but keep going to
904 * maintain a consistent eviction state
909 eviction_duration = get_jiffies_64() - pdd->last_evict_timestamp;
910 atomic64_add(eviction_duration, &pdd->evict_duration_counter);
918 static int restore_process_queues_cpsch(struct device_queue_manager *dqm,
919 struct qcm_process_device *qpd)
922 struct kfd_process_device *pdd;
924 uint64_t eviction_duration;
927 pdd = qpd_to_pdd(qpd);
928 /* Retrieve PD base */
929 pd_base = amdgpu_amdkfd_gpuvm_get_process_page_dir(pdd->drm_priv);
932 if (WARN_ON_ONCE(!qpd->evicted)) /* already restored, do nothing */
934 if (qpd->evicted > 1) { /* ref count still > 0, decrement & quit */
939 pr_debug_ratelimited("Restoring PASID 0x%x queues\n",
940 pdd->process->pasid);
942 /* Update PD Base in QPD */
943 qpd->page_table_base = pd_base;
944 pr_debug("Updated PD address to 0x%llx\n", pd_base);
946 /* activate all active queues on the qpd */
947 list_for_each_entry(q, &qpd->queues_list, list) {
948 q->properties.is_evicted = false;
949 if (!QUEUE_IS_ACTIVE(q->properties))
952 q->properties.is_active = true;
953 increment_queue_count(dqm, &pdd->qpd, q);
955 retval = execute_queues_cpsch(dqm,
956 KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 0);
958 eviction_duration = get_jiffies_64() - pdd->last_evict_timestamp;
959 atomic64_add(eviction_duration, &pdd->evict_duration_counter);
965 static int register_process(struct device_queue_manager *dqm,
966 struct qcm_process_device *qpd)
968 struct device_process_node *n;
969 struct kfd_process_device *pdd;
973 n = kzalloc(sizeof(*n), GFP_KERNEL);
979 pdd = qpd_to_pdd(qpd);
980 /* Retrieve PD base */
981 pd_base = amdgpu_amdkfd_gpuvm_get_process_page_dir(pdd->drm_priv);
984 list_add(&n->list, &dqm->queues);
986 /* Update PD Base in QPD */
987 qpd->page_table_base = pd_base;
988 pr_debug("Updated PD address to 0x%llx\n", pd_base);
990 retval = dqm->asic_ops.update_qpd(dqm, qpd);
992 dqm->processes_count++;
996 /* Outside the DQM lock because under the DQM lock we can't do
997 * reclaim or take other locks that others hold while reclaiming.
999 kfd_inc_compute_active(dqm->dev);
1004 static int unregister_process(struct device_queue_manager *dqm,
1005 struct qcm_process_device *qpd)
1008 struct device_process_node *cur, *next;
1010 pr_debug("qpd->queues_list is %s\n",
1011 list_empty(&qpd->queues_list) ? "empty" : "not empty");
1016 list_for_each_entry_safe(cur, next, &dqm->queues, list) {
1017 if (qpd == cur->qpd) {
1018 list_del(&cur->list);
1020 dqm->processes_count--;
1024 /* qpd not found in dqm list */
1029 /* Outside the DQM lock because under the DQM lock we can't do
1030 * reclaim or take other locks that others hold while reclaiming.
1033 kfd_dec_compute_active(dqm->dev);
1039 set_pasid_vmid_mapping(struct device_queue_manager *dqm, u32 pasid,
1042 return dqm->dev->kfd2kgd->set_pasid_vmid_mapping(
1043 dqm->dev->adev, pasid, vmid);
1046 static void init_interrupts(struct device_queue_manager *dqm)
1050 for (i = 0 ; i < get_pipes_per_mec(dqm) ; i++)
1051 if (is_pipe_enabled(dqm, 0, i))
1052 dqm->dev->kfd2kgd->init_interrupts(dqm->dev->adev, i);
1055 static int initialize_nocpsch(struct device_queue_manager *dqm)
1059 pr_debug("num of pipes: %d\n", get_pipes_per_mec(dqm));
1061 dqm->allocated_queues = kcalloc(get_pipes_per_mec(dqm),
1062 sizeof(unsigned int), GFP_KERNEL);
1063 if (!dqm->allocated_queues)
1066 mutex_init(&dqm->lock_hidden);
1067 INIT_LIST_HEAD(&dqm->queues);
1068 dqm->active_queue_count = dqm->next_pipe_to_allocate = 0;
1069 dqm->active_cp_queue_count = 0;
1070 dqm->gws_queue_count = 0;
1072 for (pipe = 0; pipe < get_pipes_per_mec(dqm); pipe++) {
1073 int pipe_offset = pipe * get_queues_per_pipe(dqm);
1075 for (queue = 0; queue < get_queues_per_pipe(dqm); queue++)
1076 if (test_bit(pipe_offset + queue,
1077 dqm->dev->shared_resources.cp_queue_bitmap))
1078 dqm->allocated_queues[pipe] |= 1 << queue;
1081 memset(dqm->vmid_pasid, 0, sizeof(dqm->vmid_pasid));
1083 dqm->sdma_bitmap = ~0ULL >> (64 - get_num_sdma_queues(dqm));
1084 dqm->xgmi_sdma_bitmap = ~0ULL >> (64 - get_num_xgmi_sdma_queues(dqm));
1089 static void uninitialize(struct device_queue_manager *dqm)
1093 WARN_ON(dqm->active_queue_count > 0 || dqm->processes_count > 0);
1095 kfree(dqm->allocated_queues);
1096 for (i = 0 ; i < KFD_MQD_TYPE_MAX ; i++)
1097 kfree(dqm->mqd_mgrs[i]);
1098 mutex_destroy(&dqm->lock_hidden);
1101 static int start_nocpsch(struct device_queue_manager *dqm)
1105 pr_info("SW scheduler is used");
1106 init_interrupts(dqm);
1108 if (dqm->dev->adev->asic_type == CHIP_HAWAII)
1109 r = pm_init(&dqm->packet_mgr, dqm);
1111 dqm->sched_running = true;
1116 static int stop_nocpsch(struct device_queue_manager *dqm)
1118 if (dqm->dev->adev->asic_type == CHIP_HAWAII)
1119 pm_uninit(&dqm->packet_mgr, false);
1120 dqm->sched_running = false;
1125 static void pre_reset(struct device_queue_manager *dqm)
1128 dqm->is_resetting = true;
1132 static int allocate_sdma_queue(struct device_queue_manager *dqm,
1133 struct queue *q, const uint32_t *restore_sdma_id)
1137 if (q->properties.type == KFD_QUEUE_TYPE_SDMA) {
1138 if (dqm->sdma_bitmap == 0) {
1139 pr_err("No more SDMA queue to allocate\n");
1143 if (restore_sdma_id) {
1144 /* Re-use existing sdma_id */
1145 if (!(dqm->sdma_bitmap & (1ULL << *restore_sdma_id))) {
1146 pr_err("SDMA queue already in use\n");
1149 dqm->sdma_bitmap &= ~(1ULL << *restore_sdma_id);
1150 q->sdma_id = *restore_sdma_id;
1152 /* Find first available sdma_id */
1153 bit = __ffs64(dqm->sdma_bitmap);
1154 dqm->sdma_bitmap &= ~(1ULL << bit);
1158 q->properties.sdma_engine_id = q->sdma_id %
1159 kfd_get_num_sdma_engines(dqm->dev);
1160 q->properties.sdma_queue_id = q->sdma_id /
1161 kfd_get_num_sdma_engines(dqm->dev);
1162 } else if (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) {
1163 if (dqm->xgmi_sdma_bitmap == 0) {
1164 pr_err("No more XGMI SDMA queue to allocate\n");
1167 if (restore_sdma_id) {
1168 /* Re-use existing sdma_id */
1169 if (!(dqm->xgmi_sdma_bitmap & (1ULL << *restore_sdma_id))) {
1170 pr_err("SDMA queue already in use\n");
1173 dqm->xgmi_sdma_bitmap &= ~(1ULL << *restore_sdma_id);
1174 q->sdma_id = *restore_sdma_id;
1176 bit = __ffs64(dqm->xgmi_sdma_bitmap);
1177 dqm->xgmi_sdma_bitmap &= ~(1ULL << bit);
1180 /* sdma_engine_id is sdma id including
1181 * both PCIe-optimized SDMAs and XGMI-
1182 * optimized SDMAs. The calculation below
1183 * assumes the first N engines are always
1184 * PCIe-optimized ones
1186 q->properties.sdma_engine_id =
1187 kfd_get_num_sdma_engines(dqm->dev) +
1188 q->sdma_id % kfd_get_num_xgmi_sdma_engines(dqm->dev);
1189 q->properties.sdma_queue_id = q->sdma_id /
1190 kfd_get_num_xgmi_sdma_engines(dqm->dev);
1193 pr_debug("SDMA engine id: %d\n", q->properties.sdma_engine_id);
1194 pr_debug("SDMA queue id: %d\n", q->properties.sdma_queue_id);
1199 static void deallocate_sdma_queue(struct device_queue_manager *dqm,
1202 if (q->properties.type == KFD_QUEUE_TYPE_SDMA) {
1203 if (q->sdma_id >= get_num_sdma_queues(dqm))
1205 dqm->sdma_bitmap |= (1ULL << q->sdma_id);
1206 } else if (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) {
1207 if (q->sdma_id >= get_num_xgmi_sdma_queues(dqm))
1209 dqm->xgmi_sdma_bitmap |= (1ULL << q->sdma_id);
1214 * Device Queue Manager implementation for cp scheduler
1217 static int set_sched_resources(struct device_queue_manager *dqm)
1220 struct scheduling_resources res;
1222 res.vmid_mask = dqm->dev->shared_resources.compute_vmid_bitmap;
1225 for (i = 0; i < KGD_MAX_QUEUES; ++i) {
1226 mec = (i / dqm->dev->shared_resources.num_queue_per_pipe)
1227 / dqm->dev->shared_resources.num_pipe_per_mec;
1229 if (!test_bit(i, dqm->dev->shared_resources.cp_queue_bitmap))
1232 /* only acquire queues from the first MEC */
1236 /* This situation may be hit in the future if a new HW
1237 * generation exposes more than 64 queues. If so, the
1238 * definition of res.queue_mask needs updating
1240 if (WARN_ON(i >= (sizeof(res.queue_mask)*8))) {
1241 pr_err("Invalid queue enabled by amdgpu: %d\n", i);
1245 res.queue_mask |= 1ull
1246 << amdgpu_queue_mask_bit_to_set_resource_bit(
1249 res.gws_mask = ~0ull;
1250 res.oac_mask = res.gds_heap_base = res.gds_heap_size = 0;
1252 pr_debug("Scheduling resources:\n"
1253 "vmid mask: 0x%8X\n"
1254 "queue mask: 0x%8llX\n",
1255 res.vmid_mask, res.queue_mask);
1257 return pm_send_set_resources(&dqm->packet_mgr, &res);
1260 static int initialize_cpsch(struct device_queue_manager *dqm)
1262 uint64_t num_sdma_queues;
1263 uint64_t num_xgmi_sdma_queues;
1265 pr_debug("num of pipes: %d\n", get_pipes_per_mec(dqm));
1267 mutex_init(&dqm->lock_hidden);
1268 INIT_LIST_HEAD(&dqm->queues);
1269 dqm->active_queue_count = dqm->processes_count = 0;
1270 dqm->active_cp_queue_count = 0;
1271 dqm->gws_queue_count = 0;
1272 dqm->active_runlist = false;
1274 num_sdma_queues = get_num_sdma_queues(dqm);
1275 if (num_sdma_queues >= BITS_PER_TYPE(dqm->sdma_bitmap))
1276 dqm->sdma_bitmap = ULLONG_MAX;
1278 dqm->sdma_bitmap = (BIT_ULL(num_sdma_queues) - 1);
1280 num_xgmi_sdma_queues = get_num_xgmi_sdma_queues(dqm);
1281 if (num_xgmi_sdma_queues >= BITS_PER_TYPE(dqm->xgmi_sdma_bitmap))
1282 dqm->xgmi_sdma_bitmap = ULLONG_MAX;
1284 dqm->xgmi_sdma_bitmap = (BIT_ULL(num_xgmi_sdma_queues) - 1);
1286 INIT_WORK(&dqm->hw_exception_work, kfd_process_hw_exception);
1291 static int start_cpsch(struct device_queue_manager *dqm)
1298 retval = pm_init(&dqm->packet_mgr, dqm);
1300 goto fail_packet_manager_init;
1302 retval = set_sched_resources(dqm);
1304 goto fail_set_sched_resources;
1306 pr_debug("Allocating fence memory\n");
1308 /* allocate fence memory on the gart */
1309 retval = kfd_gtt_sa_allocate(dqm->dev, sizeof(*dqm->fence_addr),
1313 goto fail_allocate_vidmem;
1315 dqm->fence_addr = (uint64_t *)dqm->fence_mem->cpu_ptr;
1316 dqm->fence_gpu_addr = dqm->fence_mem->gpu_addr;
1318 init_interrupts(dqm);
1320 /* clear hang status when driver try to start the hw scheduler */
1321 dqm->is_hws_hang = false;
1322 dqm->is_resetting = false;
1323 dqm->sched_running = true;
1324 execute_queues_cpsch(dqm, KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 0);
1328 fail_allocate_vidmem:
1329 fail_set_sched_resources:
1330 pm_uninit(&dqm->packet_mgr, false);
1331 fail_packet_manager_init:
1336 static int stop_cpsch(struct device_queue_manager *dqm)
1341 if (!dqm->sched_running) {
1346 if (!dqm->is_hws_hang)
1347 unmap_queues_cpsch(dqm, KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES, 0, false);
1348 hanging = dqm->is_hws_hang || dqm->is_resetting;
1349 dqm->sched_running = false;
1351 pm_release_ib(&dqm->packet_mgr);
1353 kfd_gtt_sa_free(dqm->dev, dqm->fence_mem);
1354 pm_uninit(&dqm->packet_mgr, hanging);
1360 static int create_kernel_queue_cpsch(struct device_queue_manager *dqm,
1361 struct kernel_queue *kq,
1362 struct qcm_process_device *qpd)
1365 if (dqm->total_queue_count >= max_num_of_queues_per_device) {
1366 pr_warn("Can't create new kernel queue because %d queues were already created\n",
1367 dqm->total_queue_count);
1373 * Unconditionally increment this counter, regardless of the queue's
1374 * type or whether the queue is active.
1376 dqm->total_queue_count++;
1377 pr_debug("Total of %d queues are accountable so far\n",
1378 dqm->total_queue_count);
1380 list_add(&kq->list, &qpd->priv_queue_list);
1381 increment_queue_count(dqm, qpd, kq->queue);
1382 qpd->is_debug = true;
1383 execute_queues_cpsch(dqm, KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 0);
1389 static void destroy_kernel_queue_cpsch(struct device_queue_manager *dqm,
1390 struct kernel_queue *kq,
1391 struct qcm_process_device *qpd)
1394 list_del(&kq->list);
1395 decrement_queue_count(dqm, qpd, kq->queue);
1396 qpd->is_debug = false;
1397 execute_queues_cpsch(dqm, KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES, 0);
1399 * Unconditionally decrement this counter, regardless of the queue's
1402 dqm->total_queue_count--;
1403 pr_debug("Total of %d queues are accountable so far\n",
1404 dqm->total_queue_count);
1408 static int create_queue_cpsch(struct device_queue_manager *dqm, struct queue *q,
1409 struct qcm_process_device *qpd,
1410 const struct kfd_criu_queue_priv_data *qd,
1411 const void *restore_mqd, const void *restore_ctl_stack)
1414 struct mqd_manager *mqd_mgr;
1416 if (dqm->total_queue_count >= max_num_of_queues_per_device) {
1417 pr_warn("Can't create new usermode queue because %d queues were already created\n",
1418 dqm->total_queue_count);
1423 if (q->properties.type == KFD_QUEUE_TYPE_SDMA ||
1424 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) {
1426 retval = allocate_sdma_queue(dqm, q, qd ? &qd->sdma_id : NULL);
1432 retval = allocate_doorbell(qpd, q, qd ? &qd->doorbell_id : NULL);
1434 goto out_deallocate_sdma_queue;
1436 mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type(
1437 q->properties.type)];
1439 if (q->properties.type == KFD_QUEUE_TYPE_SDMA ||
1440 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)
1441 dqm->asic_ops.init_sdma_vm(dqm, q, qpd);
1442 q->properties.tba_addr = qpd->tba_addr;
1443 q->properties.tma_addr = qpd->tma_addr;
1444 q->mqd_mem_obj = mqd_mgr->allocate_mqd(mqd_mgr->dev, &q->properties);
1445 if (!q->mqd_mem_obj) {
1447 goto out_deallocate_doorbell;
1452 * Eviction state logic: mark all queues as evicted, even ones
1453 * not currently active. Restoring inactive queues later only
1454 * updates the is_evicted flag but is a no-op otherwise.
1456 q->properties.is_evicted = !!qpd->evicted;
1459 mqd_mgr->restore_mqd(mqd_mgr, &q->mqd, q->mqd_mem_obj, &q->gart_mqd_addr,
1460 &q->properties, restore_mqd, restore_ctl_stack,
1461 qd->ctl_stack_size);
1463 mqd_mgr->init_mqd(mqd_mgr, &q->mqd, q->mqd_mem_obj,
1464 &q->gart_mqd_addr, &q->properties);
1466 list_add(&q->list, &qpd->queues_list);
1469 if (q->properties.is_active) {
1470 increment_queue_count(dqm, qpd, q);
1472 execute_queues_cpsch(dqm,
1473 KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 0);
1477 * Unconditionally increment this counter, regardless of the queue's
1478 * type or whether the queue is active.
1480 dqm->total_queue_count++;
1482 pr_debug("Total of %d queues are accountable so far\n",
1483 dqm->total_queue_count);
1488 out_deallocate_doorbell:
1489 deallocate_doorbell(qpd, q);
1490 out_deallocate_sdma_queue:
1491 if (q->properties.type == KFD_QUEUE_TYPE_SDMA ||
1492 q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI) {
1494 deallocate_sdma_queue(dqm, q);
1501 int amdkfd_fence_wait_timeout(uint64_t *fence_addr,
1502 uint64_t fence_value,
1503 unsigned int timeout_ms)
1505 unsigned long end_jiffies = msecs_to_jiffies(timeout_ms) + jiffies;
1507 while (*fence_addr != fence_value) {
1508 if (time_after(jiffies, end_jiffies)) {
1509 pr_err("qcm fence wait loop timeout expired\n");
1510 /* In HWS case, this is used to halt the driver thread
1511 * in order not to mess up CP states before doing
1512 * scandumps for FW debugging.
1514 while (halt_if_hws_hang)
1525 /* dqm->lock mutex has to be locked before calling this function */
1526 static int map_queues_cpsch(struct device_queue_manager *dqm)
1530 if (!dqm->sched_running)
1532 if (dqm->active_queue_count <= 0 || dqm->processes_count <= 0)
1534 if (dqm->active_runlist)
1537 retval = pm_send_runlist(&dqm->packet_mgr, &dqm->queues);
1538 pr_debug("%s sent runlist\n", __func__);
1540 pr_err("failed to execute runlist\n");
1543 dqm->active_runlist = true;
1548 /* dqm->lock mutex has to be locked before calling this function */
1549 static int unmap_queues_cpsch(struct device_queue_manager *dqm,
1550 enum kfd_unmap_queues_filter filter,
1551 uint32_t filter_param, bool reset)
1554 struct mqd_manager *mqd_mgr;
1556 if (!dqm->sched_running)
1558 if (dqm->is_hws_hang || dqm->is_resetting)
1560 if (!dqm->active_runlist)
1563 retval = pm_send_unmap_queue(&dqm->packet_mgr, filter, filter_param, reset);
1567 *dqm->fence_addr = KFD_FENCE_INIT;
1568 pm_send_query_status(&dqm->packet_mgr, dqm->fence_gpu_addr,
1569 KFD_FENCE_COMPLETED);
1570 /* should be timed out */
1571 retval = amdkfd_fence_wait_timeout(dqm->fence_addr, KFD_FENCE_COMPLETED,
1572 queue_preemption_timeout_ms);
1574 pr_err("The cp might be in an unrecoverable state due to an unsuccessful queues preemption\n");
1575 dqm->is_hws_hang = true;
1576 /* It's possible we're detecting a HWS hang in the
1577 * middle of a GPU reset. No need to schedule another
1578 * reset in this case.
1580 if (!dqm->is_resetting)
1581 schedule_work(&dqm->hw_exception_work);
1585 /* In the current MEC firmware implementation, if compute queue
1586 * doesn't response to the preemption request in time, HIQ will
1587 * abandon the unmap request without returning any timeout error
1588 * to driver. Instead, MEC firmware will log the doorbell of the
1589 * unresponding compute queue to HIQ.MQD.queue_doorbell_id fields.
1590 * To make sure the queue unmap was successful, driver need to
1591 * check those fields
1593 mqd_mgr = dqm->mqd_mgrs[KFD_MQD_TYPE_HIQ];
1594 if (mqd_mgr->read_doorbell_id(dqm->packet_mgr.priv_queue->queue->mqd)) {
1595 pr_err("HIQ MQD's queue_doorbell_id0 is not 0, Queue preemption time out\n");
1596 while (halt_if_hws_hang)
1601 pm_release_ib(&dqm->packet_mgr);
1602 dqm->active_runlist = false;
1607 /* only for compute queue */
1608 static int reset_queues_cpsch(struct device_queue_manager *dqm,
1615 retval = unmap_queues_cpsch(dqm, KFD_UNMAP_QUEUES_FILTER_BY_PASID,
1622 /* dqm->lock mutex has to be locked before calling this function */
1623 static int execute_queues_cpsch(struct device_queue_manager *dqm,
1624 enum kfd_unmap_queues_filter filter,
1625 uint32_t filter_param)
1629 if (dqm->is_hws_hang)
1631 retval = unmap_queues_cpsch(dqm, filter, filter_param, false);
1635 return map_queues_cpsch(dqm);
1638 static int destroy_queue_cpsch(struct device_queue_manager *dqm,
1639 struct qcm_process_device *qpd,
1643 struct mqd_manager *mqd_mgr;
1644 uint64_t sdma_val = 0;
1645 struct kfd_process_device *pdd = qpd_to_pdd(qpd);
1647 /* Get the SDMA queue stats */
1648 if ((q->properties.type == KFD_QUEUE_TYPE_SDMA) ||
1649 (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)) {
1650 retval = read_sdma_queue_counter((uint64_t __user *)q->properties.read_ptr,
1653 pr_err("Failed to read SDMA queue counter for queue: %d\n",
1654 q->properties.queue_id);
1659 /* remove queue from list to prevent rescheduling after preemption */
1662 if (qpd->is_debug) {
1664 * error, currently we do not allow to destroy a queue
1665 * of a currently debugged process
1668 goto failed_try_destroy_debugged_queue;
1672 mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type(
1673 q->properties.type)];
1675 deallocate_doorbell(qpd, q);
1677 if ((q->properties.type == KFD_QUEUE_TYPE_SDMA) ||
1678 (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)) {
1679 deallocate_sdma_queue(dqm, q);
1680 pdd->sdma_past_activity_counter += sdma_val;
1685 if (q->properties.is_active) {
1686 decrement_queue_count(dqm, qpd, q);
1687 retval = execute_queues_cpsch(dqm,
1688 KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES, 0);
1689 if (retval == -ETIME)
1690 qpd->reset_wavefronts = true;
1694 * Unconditionally decrement this counter, regardless of the queue's
1697 dqm->total_queue_count--;
1698 pr_debug("Total of %d queues are accountable so far\n",
1699 dqm->total_queue_count);
1703 /* Do free_mqd after dqm_unlock(dqm) to avoid circular locking */
1704 mqd_mgr->free_mqd(mqd_mgr, q->mqd, q->mqd_mem_obj);
1708 failed_try_destroy_debugged_queue:
1715 * Low bits must be 0000/FFFF as required by HW, high bits must be 0 to
1716 * stay in user mode.
1718 #define APE1_FIXED_BITS_MASK 0xFFFF80000000FFFFULL
1719 /* APE1 limit is inclusive and 64K aligned. */
1720 #define APE1_LIMIT_ALIGNMENT 0xFFFF
1722 static bool set_cache_memory_policy(struct device_queue_manager *dqm,
1723 struct qcm_process_device *qpd,
1724 enum cache_policy default_policy,
1725 enum cache_policy alternate_policy,
1726 void __user *alternate_aperture_base,
1727 uint64_t alternate_aperture_size)
1731 if (!dqm->asic_ops.set_cache_memory_policy)
1736 if (alternate_aperture_size == 0) {
1737 /* base > limit disables APE1 */
1738 qpd->sh_mem_ape1_base = 1;
1739 qpd->sh_mem_ape1_limit = 0;
1742 * In FSA64, APE1_Base[63:0] = { 16{SH_MEM_APE1_BASE[31]},
1743 * SH_MEM_APE1_BASE[31:0], 0x0000 }
1744 * APE1_Limit[63:0] = { 16{SH_MEM_APE1_LIMIT[31]},
1745 * SH_MEM_APE1_LIMIT[31:0], 0xFFFF }
1746 * Verify that the base and size parameters can be
1747 * represented in this format and convert them.
1748 * Additionally restrict APE1 to user-mode addresses.
1751 uint64_t base = (uintptr_t)alternate_aperture_base;
1752 uint64_t limit = base + alternate_aperture_size - 1;
1754 if (limit <= base || (base & APE1_FIXED_BITS_MASK) != 0 ||
1755 (limit & APE1_FIXED_BITS_MASK) != APE1_LIMIT_ALIGNMENT) {
1760 qpd->sh_mem_ape1_base = base >> 16;
1761 qpd->sh_mem_ape1_limit = limit >> 16;
1764 retval = dqm->asic_ops.set_cache_memory_policy(
1769 alternate_aperture_base,
1770 alternate_aperture_size);
1772 if ((dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS) && (qpd->vmid != 0))
1773 program_sh_mem_settings(dqm, qpd);
1775 pr_debug("sh_mem_config: 0x%x, ape1_base: 0x%x, ape1_limit: 0x%x\n",
1776 qpd->sh_mem_config, qpd->sh_mem_ape1_base,
1777 qpd->sh_mem_ape1_limit);
1784 static int process_termination_nocpsch(struct device_queue_manager *dqm,
1785 struct qcm_process_device *qpd)
1788 struct device_process_node *cur, *next_dpn;
1794 /* Clear all user mode queues */
1795 while (!list_empty(&qpd->queues_list)) {
1796 struct mqd_manager *mqd_mgr;
1799 q = list_first_entry(&qpd->queues_list, struct queue, list);
1800 mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type(
1801 q->properties.type)];
1802 ret = destroy_queue_nocpsch_locked(dqm, qpd, q);
1806 mqd_mgr->free_mqd(mqd_mgr, q->mqd, q->mqd_mem_obj);
1810 /* Unregister process */
1811 list_for_each_entry_safe(cur, next_dpn, &dqm->queues, list) {
1812 if (qpd == cur->qpd) {
1813 list_del(&cur->list);
1815 dqm->processes_count--;
1823 /* Outside the DQM lock because under the DQM lock we can't do
1824 * reclaim or take other locks that others hold while reclaiming.
1827 kfd_dec_compute_active(dqm->dev);
1832 static int get_wave_state(struct device_queue_manager *dqm,
1834 void __user *ctl_stack,
1835 u32 *ctl_stack_used_size,
1836 u32 *save_area_used_size)
1838 struct mqd_manager *mqd_mgr;
1842 mqd_mgr = dqm->mqd_mgrs[KFD_MQD_TYPE_CP];
1844 if (q->properties.type != KFD_QUEUE_TYPE_COMPUTE ||
1845 q->properties.is_active || !q->device->cwsr_enabled ||
1846 !mqd_mgr->get_wave_state) {
1854 * get_wave_state is outside the dqm lock to prevent circular locking
1855 * and the queue should be protected against destruction by the process
1858 return mqd_mgr->get_wave_state(mqd_mgr, q->mqd, ctl_stack,
1859 ctl_stack_used_size, save_area_used_size);
1862 static void get_queue_checkpoint_info(struct device_queue_manager *dqm,
1863 const struct queue *q,
1865 u32 *ctl_stack_size)
1867 struct mqd_manager *mqd_mgr;
1868 enum KFD_MQD_TYPE mqd_type =
1869 get_mqd_type_from_queue_type(q->properties.type);
1872 mqd_mgr = dqm->mqd_mgrs[mqd_type];
1873 *mqd_size = mqd_mgr->mqd_size;
1874 *ctl_stack_size = 0;
1876 if (q->properties.type == KFD_QUEUE_TYPE_COMPUTE && mqd_mgr->get_checkpoint_info)
1877 mqd_mgr->get_checkpoint_info(mqd_mgr, q->mqd, ctl_stack_size);
1882 static int checkpoint_mqd(struct device_queue_manager *dqm,
1883 const struct queue *q,
1887 struct mqd_manager *mqd_mgr;
1889 enum KFD_MQD_TYPE mqd_type =
1890 get_mqd_type_from_queue_type(q->properties.type);
1894 if (q->properties.is_active || !q->device->cwsr_enabled) {
1899 mqd_mgr = dqm->mqd_mgrs[mqd_type];
1900 if (!mqd_mgr->checkpoint_mqd) {
1905 mqd_mgr->checkpoint_mqd(mqd_mgr, q->mqd, mqd, ctl_stack);
1912 static int process_termination_cpsch(struct device_queue_manager *dqm,
1913 struct qcm_process_device *qpd)
1917 struct kernel_queue *kq, *kq_next;
1918 struct mqd_manager *mqd_mgr;
1919 struct device_process_node *cur, *next_dpn;
1920 enum kfd_unmap_queues_filter filter =
1921 KFD_UNMAP_QUEUES_FILTER_DYNAMIC_QUEUES;
1928 /* Clean all kernel queues */
1929 list_for_each_entry_safe(kq, kq_next, &qpd->priv_queue_list, list) {
1930 list_del(&kq->list);
1931 decrement_queue_count(dqm, qpd, kq->queue);
1932 qpd->is_debug = false;
1933 dqm->total_queue_count--;
1934 filter = KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES;
1937 /* Clear all user mode queues */
1938 list_for_each_entry(q, &qpd->queues_list, list) {
1939 if (q->properties.type == KFD_QUEUE_TYPE_SDMA)
1940 deallocate_sdma_queue(dqm, q);
1941 else if (q->properties.type == KFD_QUEUE_TYPE_SDMA_XGMI)
1942 deallocate_sdma_queue(dqm, q);
1944 if (q->properties.is_active)
1945 decrement_queue_count(dqm, qpd, q);
1947 dqm->total_queue_count--;
1950 /* Unregister process */
1951 list_for_each_entry_safe(cur, next_dpn, &dqm->queues, list) {
1952 if (qpd == cur->qpd) {
1953 list_del(&cur->list);
1955 dqm->processes_count--;
1961 retval = execute_queues_cpsch(dqm, filter, 0);
1962 if ((!dqm->is_hws_hang) && (retval || qpd->reset_wavefronts)) {
1963 pr_warn("Resetting wave fronts (cpsch) on dev %p\n", dqm->dev);
1964 dbgdev_wave_reset_wavefronts(dqm->dev, qpd->pqm->process);
1965 qpd->reset_wavefronts = false;
1968 /* Lastly, free mqd resources.
1969 * Do free_mqd() after dqm_unlock to avoid circular locking.
1971 while (!list_empty(&qpd->queues_list)) {
1972 q = list_first_entry(&qpd->queues_list, struct queue, list);
1973 mqd_mgr = dqm->mqd_mgrs[get_mqd_type_from_queue_type(
1974 q->properties.type)];
1978 mqd_mgr->free_mqd(mqd_mgr, q->mqd, q->mqd_mem_obj);
1983 /* Outside the DQM lock because under the DQM lock we can't do
1984 * reclaim or take other locks that others hold while reclaiming.
1987 kfd_dec_compute_active(dqm->dev);
1992 static int init_mqd_managers(struct device_queue_manager *dqm)
1995 struct mqd_manager *mqd_mgr;
1997 for (i = 0; i < KFD_MQD_TYPE_MAX; i++) {
1998 mqd_mgr = dqm->asic_ops.mqd_manager_init(i, dqm->dev);
2000 pr_err("mqd manager [%d] initialization failed\n", i);
2003 dqm->mqd_mgrs[i] = mqd_mgr;
2009 for (j = 0; j < i; j++) {
2010 kfree(dqm->mqd_mgrs[j]);
2011 dqm->mqd_mgrs[j] = NULL;
2017 /* Allocate one hiq mqd (HWS) and all SDMA mqd in a continuous trunk*/
2018 static int allocate_hiq_sdma_mqd(struct device_queue_manager *dqm)
2021 struct kfd_dev *dev = dqm->dev;
2022 struct kfd_mem_obj *mem_obj = &dqm->hiq_sdma_mqd;
2023 uint32_t size = dqm->mqd_mgrs[KFD_MQD_TYPE_SDMA]->mqd_size *
2024 get_num_all_sdma_engines(dqm) *
2025 dev->device_info.num_sdma_queues_per_engine +
2026 dqm->mqd_mgrs[KFD_MQD_TYPE_HIQ]->mqd_size;
2028 retval = amdgpu_amdkfd_alloc_gtt_mem(dev->adev, size,
2029 &(mem_obj->gtt_mem), &(mem_obj->gpu_addr),
2030 (void *)&(mem_obj->cpu_ptr), false);
2035 struct device_queue_manager *device_queue_manager_init(struct kfd_dev *dev)
2037 struct device_queue_manager *dqm;
2039 pr_debug("Loading device queue manager\n");
2041 dqm = kzalloc(sizeof(*dqm), GFP_KERNEL);
2045 switch (dev->adev->asic_type) {
2046 /* HWS is not available on Hawaii. */
2048 /* HWS depends on CWSR for timely dequeue. CWSR is not
2049 * available on Tonga.
2051 * FIXME: This argument also applies to Kaveri.
2054 dqm->sched_policy = KFD_SCHED_POLICY_NO_HWS;
2057 dqm->sched_policy = sched_policy;
2062 switch (dqm->sched_policy) {
2063 case KFD_SCHED_POLICY_HWS:
2064 case KFD_SCHED_POLICY_HWS_NO_OVERSUBSCRIPTION:
2065 /* initialize dqm for cp scheduling */
2066 dqm->ops.create_queue = create_queue_cpsch;
2067 dqm->ops.initialize = initialize_cpsch;
2068 dqm->ops.start = start_cpsch;
2069 dqm->ops.stop = stop_cpsch;
2070 dqm->ops.pre_reset = pre_reset;
2071 dqm->ops.destroy_queue = destroy_queue_cpsch;
2072 dqm->ops.update_queue = update_queue;
2073 dqm->ops.register_process = register_process;
2074 dqm->ops.unregister_process = unregister_process;
2075 dqm->ops.uninitialize = uninitialize;
2076 dqm->ops.create_kernel_queue = create_kernel_queue_cpsch;
2077 dqm->ops.destroy_kernel_queue = destroy_kernel_queue_cpsch;
2078 dqm->ops.set_cache_memory_policy = set_cache_memory_policy;
2079 dqm->ops.process_termination = process_termination_cpsch;
2080 dqm->ops.evict_process_queues = evict_process_queues_cpsch;
2081 dqm->ops.restore_process_queues = restore_process_queues_cpsch;
2082 dqm->ops.get_wave_state = get_wave_state;
2083 dqm->ops.reset_queues = reset_queues_cpsch;
2084 dqm->ops.get_queue_checkpoint_info = get_queue_checkpoint_info;
2085 dqm->ops.checkpoint_mqd = checkpoint_mqd;
2087 case KFD_SCHED_POLICY_NO_HWS:
2088 /* initialize dqm for no cp scheduling */
2089 dqm->ops.start = start_nocpsch;
2090 dqm->ops.stop = stop_nocpsch;
2091 dqm->ops.pre_reset = pre_reset;
2092 dqm->ops.create_queue = create_queue_nocpsch;
2093 dqm->ops.destroy_queue = destroy_queue_nocpsch;
2094 dqm->ops.update_queue = update_queue;
2095 dqm->ops.register_process = register_process;
2096 dqm->ops.unregister_process = unregister_process;
2097 dqm->ops.initialize = initialize_nocpsch;
2098 dqm->ops.uninitialize = uninitialize;
2099 dqm->ops.set_cache_memory_policy = set_cache_memory_policy;
2100 dqm->ops.process_termination = process_termination_nocpsch;
2101 dqm->ops.evict_process_queues = evict_process_queues_nocpsch;
2102 dqm->ops.restore_process_queues =
2103 restore_process_queues_nocpsch;
2104 dqm->ops.get_wave_state = get_wave_state;
2105 dqm->ops.get_queue_checkpoint_info = get_queue_checkpoint_info;
2106 dqm->ops.checkpoint_mqd = checkpoint_mqd;
2109 pr_err("Invalid scheduling policy %d\n", dqm->sched_policy);
2113 switch (dev->adev->asic_type) {
2115 device_queue_manager_init_vi(&dqm->asic_ops);
2119 device_queue_manager_init_cik(&dqm->asic_ops);
2123 device_queue_manager_init_cik_hawaii(&dqm->asic_ops);
2128 case CHIP_POLARIS10:
2129 case CHIP_POLARIS11:
2130 case CHIP_POLARIS12:
2132 device_queue_manager_init_vi_tonga(&dqm->asic_ops);
2136 if (KFD_GC_VERSION(dev) >= IP_VERSION(10, 1, 1))
2137 device_queue_manager_init_v10_navi10(&dqm->asic_ops);
2138 else if (KFD_GC_VERSION(dev) >= IP_VERSION(9, 0, 1))
2139 device_queue_manager_init_v9(&dqm->asic_ops);
2141 WARN(1, "Unexpected ASIC family %u",
2142 dev->adev->asic_type);
2147 if (init_mqd_managers(dqm))
2150 if (allocate_hiq_sdma_mqd(dqm)) {
2151 pr_err("Failed to allocate hiq sdma mqd trunk buffer\n");
2155 if (!dqm->ops.initialize(dqm))
2163 static void deallocate_hiq_sdma_mqd(struct kfd_dev *dev,
2164 struct kfd_mem_obj *mqd)
2166 WARN(!mqd, "No hiq sdma mqd trunk to free");
2168 amdgpu_amdkfd_free_gtt_mem(dev->adev, mqd->gtt_mem);
2171 void device_queue_manager_uninit(struct device_queue_manager *dqm)
2173 dqm->ops.uninitialize(dqm);
2174 deallocate_hiq_sdma_mqd(dqm->dev, &dqm->hiq_sdma_mqd);
2178 int kfd_dqm_evict_pasid(struct device_queue_manager *dqm, u32 pasid)
2180 struct kfd_process_device *pdd;
2181 struct kfd_process *p = kfd_lookup_process_by_pasid(pasid);
2186 WARN(debug_evictions, "Evicting pid %d", p->lead_thread->pid);
2187 pdd = kfd_get_process_device_data(dqm->dev, p);
2189 ret = dqm->ops.evict_process_queues(dqm, &pdd->qpd);
2190 kfd_unref_process(p);
2195 static void kfd_process_hw_exception(struct work_struct *work)
2197 struct device_queue_manager *dqm = container_of(work,
2198 struct device_queue_manager, hw_exception_work);
2199 amdgpu_amdkfd_gpu_reset(dqm->dev->adev);
2202 #if defined(CONFIG_DEBUG_FS)
2204 static void seq_reg_dump(struct seq_file *m,
2205 uint32_t (*dump)[2], uint32_t n_regs)
2209 for (i = 0, count = 0; i < n_regs; i++) {
2211 dump[i-1][0] + sizeof(uint32_t) != dump[i][0]) {
2212 seq_printf(m, "%s %08x: %08x",
2214 dump[i][0], dump[i][1]);
2217 seq_printf(m, " %08x", dump[i][1]);
2225 int dqm_debugfs_hqds(struct seq_file *m, void *data)
2227 struct device_queue_manager *dqm = data;
2228 uint32_t (*dump)[2], n_regs;
2232 if (!dqm->sched_running) {
2233 seq_puts(m, " Device is stopped\n");
2237 r = dqm->dev->kfd2kgd->hqd_dump(dqm->dev->adev,
2238 KFD_CIK_HIQ_PIPE, KFD_CIK_HIQ_QUEUE,
2241 seq_printf(m, " HIQ on MEC %d Pipe %d Queue %d\n",
2242 KFD_CIK_HIQ_PIPE/get_pipes_per_mec(dqm)+1,
2243 KFD_CIK_HIQ_PIPE%get_pipes_per_mec(dqm),
2245 seq_reg_dump(m, dump, n_regs);
2250 for (pipe = 0; pipe < get_pipes_per_mec(dqm); pipe++) {
2251 int pipe_offset = pipe * get_queues_per_pipe(dqm);
2253 for (queue = 0; queue < get_queues_per_pipe(dqm); queue++) {
2254 if (!test_bit(pipe_offset + queue,
2255 dqm->dev->shared_resources.cp_queue_bitmap))
2258 r = dqm->dev->kfd2kgd->hqd_dump(
2259 dqm->dev->adev, pipe, queue, &dump, &n_regs);
2263 seq_printf(m, " CP Pipe %d, Queue %d\n",
2265 seq_reg_dump(m, dump, n_regs);
2271 for (pipe = 0; pipe < get_num_all_sdma_engines(dqm); pipe++) {
2273 queue < dqm->dev->device_info.num_sdma_queues_per_engine;
2275 r = dqm->dev->kfd2kgd->hqd_sdma_dump(
2276 dqm->dev->adev, pipe, queue, &dump, &n_regs);
2280 seq_printf(m, " SDMA Engine %d, RLC %d\n",
2282 seq_reg_dump(m, dump, n_regs);
2291 int dqm_debugfs_hang_hws(struct device_queue_manager *dqm)
2296 r = pm_debugfs_hang_hws(&dqm->packet_mgr);
2301 dqm->active_runlist = true;
2302 r = execute_queues_cpsch(dqm, KFD_UNMAP_QUEUES_FILTER_ALL_QUEUES, 0);