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.
24 #include <linux/bsearch.h>
25 #include <linux/pci.h>
26 #include <linux/slab.h>
28 #include "kfd_device_queue_manager.h"
29 #include "kfd_pm4_headers_vi.h"
30 #include "kfd_pm4_headers_aldebaran.h"
31 #include "cwsr_trap_handler.h"
32 #include "kfd_iommu.h"
33 #include "amdgpu_amdkfd.h"
34 #include "kfd_smi_events.h"
35 #include "kfd_migrate.h"
38 #define MQD_SIZE_ALIGNED 768
41 * kfd_locked is used to lock the kfd driver during suspend or reset
42 * once locked, kfd driver will stop any further GPU execution.
43 * create process (open) will return -EAGAIN.
45 static atomic_t kfd_locked = ATOMIC_INIT(0);
47 #ifdef CONFIG_DRM_AMDGPU_CIK
48 extern const struct kfd2kgd_calls gfx_v7_kfd2kgd;
50 extern const struct kfd2kgd_calls gfx_v8_kfd2kgd;
51 extern const struct kfd2kgd_calls gfx_v9_kfd2kgd;
52 extern const struct kfd2kgd_calls arcturus_kfd2kgd;
53 extern const struct kfd2kgd_calls aldebaran_kfd2kgd;
54 extern const struct kfd2kgd_calls gfx_v10_kfd2kgd;
55 extern const struct kfd2kgd_calls gfx_v10_3_kfd2kgd;
57 static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
58 unsigned int chunk_size);
59 static void kfd_gtt_sa_fini(struct kfd_dev *kfd);
61 static int kfd_resume(struct kfd_dev *kfd);
63 static void kfd_device_info_set_sdma_queue_num(struct kfd_dev *kfd)
65 uint32_t sdma_version = kfd->adev->ip_versions[SDMA0_HWIP][0];
67 switch (sdma_version) {
68 case IP_VERSION(4, 0, 0):/* VEGA10 */
69 case IP_VERSION(4, 0, 1):/* VEGA12 */
70 case IP_VERSION(4, 1, 0):/* RAVEN */
71 case IP_VERSION(4, 1, 1):/* RAVEN */
72 case IP_VERSION(4, 1, 2):/* RENOIR */
73 case IP_VERSION(5, 2, 1):/* VANGOGH */
74 case IP_VERSION(5, 2, 3):/* YELLOW_CARP */
75 kfd->device_info.num_sdma_queues_per_engine = 2;
77 case IP_VERSION(4, 2, 0):/* VEGA20 */
78 case IP_VERSION(4, 2, 2):/* ARCTURUS */
79 case IP_VERSION(4, 4, 0):/* ALDEBARAN */
80 case IP_VERSION(5, 0, 0):/* NAVI10 */
81 case IP_VERSION(5, 0, 1):/* CYAN_SKILLFISH */
82 case IP_VERSION(5, 0, 2):/* NAVI14 */
83 case IP_VERSION(5, 0, 5):/* NAVI12 */
84 case IP_VERSION(5, 2, 0):/* SIENNA_CICHLID */
85 case IP_VERSION(5, 2, 2):/* NAVY_FLOUNDER */
86 case IP_VERSION(5, 2, 4):/* DIMGREY_CAVEFISH */
87 case IP_VERSION(5, 2, 5):/* BEIGE_GOBY */
88 kfd->device_info.num_sdma_queues_per_engine = 8;
92 "Default sdma queue per engine(8) is set due to mismatch of sdma ip block(SDMA_HWIP:0x%x).\n",
94 kfd->device_info.num_sdma_queues_per_engine = 8;
98 static void kfd_device_info_set_event_interrupt_class(struct kfd_dev *kfd)
100 uint32_t gc_version = KFD_GC_VERSION(kfd);
102 switch (gc_version) {
103 case IP_VERSION(9, 0, 1): /* VEGA10 */
104 case IP_VERSION(9, 1, 0): /* RAVEN */
105 case IP_VERSION(9, 2, 1): /* VEGA12 */
106 case IP_VERSION(9, 2, 2): /* RAVEN */
107 case IP_VERSION(9, 3, 0): /* RENOIR */
108 case IP_VERSION(9, 4, 0): /* VEGA20 */
109 case IP_VERSION(9, 4, 1): /* ARCTURUS */
110 case IP_VERSION(9, 4, 2): /* ALDEBARAN */
111 case IP_VERSION(10, 3, 1): /* VANGOGH */
112 case IP_VERSION(10, 3, 3): /* YELLOW_CARP */
113 case IP_VERSION(10, 1, 3): /* CYAN_SKILLFISH */
114 case IP_VERSION(10, 1, 4):
115 case IP_VERSION(10, 1, 10): /* NAVI10 */
116 case IP_VERSION(10, 1, 2): /* NAVI12 */
117 case IP_VERSION(10, 1, 1): /* NAVI14 */
118 case IP_VERSION(10, 3, 0): /* SIENNA_CICHLID */
119 case IP_VERSION(10, 3, 2): /* NAVY_FLOUNDER */
120 case IP_VERSION(10, 3, 4): /* DIMGREY_CAVEFISH */
121 case IP_VERSION(10, 3, 5): /* BEIGE_GOBY */
122 kfd->device_info.event_interrupt_class = &event_interrupt_class_v9;
125 dev_warn(kfd_device, "v9 event interrupt handler is set due to "
126 "mismatch of gc ip block(GC_HWIP:0x%x).\n", gc_version);
127 kfd->device_info.event_interrupt_class = &event_interrupt_class_v9;
131 static void kfd_device_info_init(struct kfd_dev *kfd,
132 bool vf, uint32_t gfx_target_version)
134 uint32_t gc_version = KFD_GC_VERSION(kfd);
135 uint32_t asic_type = kfd->adev->asic_type;
137 kfd->device_info.max_pasid_bits = 16;
138 kfd->device_info.max_no_of_hqd = 24;
139 kfd->device_info.num_of_watch_points = 4;
140 kfd->device_info.mqd_size_aligned = MQD_SIZE_ALIGNED;
141 kfd->device_info.gfx_target_version = gfx_target_version;
143 if (KFD_IS_SOC15(kfd)) {
144 kfd->device_info.doorbell_size = 8;
145 kfd->device_info.ih_ring_entry_size = 8 * sizeof(uint32_t);
146 kfd->device_info.supports_cwsr = true;
148 kfd_device_info_set_sdma_queue_num(kfd);
150 kfd_device_info_set_event_interrupt_class(kfd);
153 if (gc_version == IP_VERSION(9, 1, 0) ||
154 gc_version == IP_VERSION(9, 2, 2))
155 kfd->device_info.needs_iommu_device = true;
157 if (gc_version < IP_VERSION(11, 0, 0)) {
158 /* Navi2x+, Navi1x+ */
159 if (gc_version >= IP_VERSION(10, 3, 0))
160 kfd->device_info.no_atomic_fw_version = 92;
161 else if (gc_version >= IP_VERSION(10, 1, 1))
162 kfd->device_info.no_atomic_fw_version = 145;
165 if (gc_version >= IP_VERSION(10, 1, 1))
166 kfd->device_info.needs_pci_atomics = true;
169 kfd->device_info.doorbell_size = 4;
170 kfd->device_info.ih_ring_entry_size = 4 * sizeof(uint32_t);
171 kfd->device_info.event_interrupt_class = &event_interrupt_class_cik;
172 kfd->device_info.num_sdma_queues_per_engine = 2;
174 if (asic_type != CHIP_KAVERI &&
175 asic_type != CHIP_HAWAII &&
176 asic_type != CHIP_TONGA)
177 kfd->device_info.supports_cwsr = true;
179 if (asic_type == CHIP_KAVERI ||
180 asic_type == CHIP_CARRIZO)
181 kfd->device_info.needs_iommu_device = true;
183 if (asic_type != CHIP_HAWAII && !vf)
184 kfd->device_info.needs_pci_atomics = true;
188 struct kfd_dev *kgd2kfd_probe(struct amdgpu_device *adev, bool vf)
190 struct kfd_dev *kfd = NULL;
191 const struct kfd2kgd_calls *f2g = NULL;
192 struct pci_dev *pdev = adev->pdev;
193 uint32_t gfx_target_version = 0;
195 switch (adev->asic_type) {
196 #ifdef KFD_SUPPORT_IOMMU_V2
197 #ifdef CONFIG_DRM_AMDGPU_CIK
199 gfx_target_version = 70000;
201 f2g = &gfx_v7_kfd2kgd;
205 gfx_target_version = 80001;
207 f2g = &gfx_v8_kfd2kgd;
210 #ifdef CONFIG_DRM_AMDGPU_CIK
212 gfx_target_version = 70001;
213 if (!amdgpu_exp_hw_support)
215 "KFD support on Hawaii is experimental. See modparam exp_hw_support\n"
218 f2g = &gfx_v7_kfd2kgd;
222 gfx_target_version = 80002;
224 f2g = &gfx_v8_kfd2kgd;
227 gfx_target_version = 80003;
228 f2g = &gfx_v8_kfd2kgd;
231 gfx_target_version = 80003;
232 f2g = &gfx_v8_kfd2kgd;
235 gfx_target_version = 80003;
237 f2g = &gfx_v8_kfd2kgd;
240 gfx_target_version = 80003;
242 f2g = &gfx_v8_kfd2kgd;
245 gfx_target_version = 80003;
247 f2g = &gfx_v8_kfd2kgd;
250 switch (adev->ip_versions[GC_HWIP][0]) {
252 case IP_VERSION(9, 0, 1):
253 gfx_target_version = 90000;
254 f2g = &gfx_v9_kfd2kgd;
256 #ifdef KFD_SUPPORT_IOMMU_V2
258 case IP_VERSION(9, 1, 0):
259 case IP_VERSION(9, 2, 2):
260 gfx_target_version = 90002;
262 f2g = &gfx_v9_kfd2kgd;
266 case IP_VERSION(9, 2, 1):
267 gfx_target_version = 90004;
269 f2g = &gfx_v9_kfd2kgd;
272 case IP_VERSION(9, 3, 0):
273 gfx_target_version = 90012;
275 f2g = &gfx_v9_kfd2kgd;
278 case IP_VERSION(9, 4, 0):
279 gfx_target_version = 90006;
281 f2g = &gfx_v9_kfd2kgd;
284 case IP_VERSION(9, 4, 1):
285 gfx_target_version = 90008;
286 f2g = &arcturus_kfd2kgd;
289 case IP_VERSION(9, 4, 2):
290 gfx_target_version = 90010;
291 f2g = &aldebaran_kfd2kgd;
294 case IP_VERSION(10, 1, 10):
295 gfx_target_version = 100100;
297 f2g = &gfx_v10_kfd2kgd;
300 case IP_VERSION(10, 1, 2):
301 gfx_target_version = 100101;
302 f2g = &gfx_v10_kfd2kgd;
305 case IP_VERSION(10, 1, 1):
306 gfx_target_version = 100102;
308 f2g = &gfx_v10_kfd2kgd;
311 case IP_VERSION(10, 1, 3):
312 case IP_VERSION(10, 1, 4):
313 gfx_target_version = 100103;
315 f2g = &gfx_v10_kfd2kgd;
318 case IP_VERSION(10, 3, 0):
319 gfx_target_version = 100300;
320 f2g = &gfx_v10_3_kfd2kgd;
323 case IP_VERSION(10, 3, 2):
324 gfx_target_version = 100301;
325 f2g = &gfx_v10_3_kfd2kgd;
328 case IP_VERSION(10, 3, 1):
329 gfx_target_version = 100303;
331 f2g = &gfx_v10_3_kfd2kgd;
333 /* Dimgrey Cavefish */
334 case IP_VERSION(10, 3, 4):
335 gfx_target_version = 100302;
336 f2g = &gfx_v10_3_kfd2kgd;
339 case IP_VERSION(10, 3, 5):
340 gfx_target_version = 100304;
341 f2g = &gfx_v10_3_kfd2kgd;
344 case IP_VERSION(10, 3, 3):
345 gfx_target_version = 100305;
347 f2g = &gfx_v10_3_kfd2kgd;
356 if (adev->ip_versions[GC_HWIP][0])
357 dev_err(kfd_device, "GC IP %06x %s not supported in kfd\n",
358 adev->ip_versions[GC_HWIP][0], vf ? "VF" : "");
360 dev_err(kfd_device, "%s %s not supported in kfd\n",
361 amdgpu_asic_name[adev->asic_type], vf ? "VF" : "");
365 kfd = kzalloc(sizeof(*kfd), GFP_KERNEL);
370 kfd_device_info_init(kfd, vf, gfx_target_version);
372 kfd->init_complete = false;
374 atomic_set(&kfd->compute_profile, 0);
376 mutex_init(&kfd->doorbell_mutex);
377 memset(&kfd->doorbell_available_index, 0,
378 sizeof(kfd->doorbell_available_index));
380 atomic_set(&kfd->sram_ecc_flag, 0);
382 ida_init(&kfd->doorbell_ida);
387 static void kfd_cwsr_init(struct kfd_dev *kfd)
389 if (cwsr_enable && kfd->device_info.supports_cwsr) {
390 if (KFD_GC_VERSION(kfd) < IP_VERSION(9, 0, 1)) {
391 BUILD_BUG_ON(sizeof(cwsr_trap_gfx8_hex) > PAGE_SIZE);
392 kfd->cwsr_isa = cwsr_trap_gfx8_hex;
393 kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx8_hex);
394 } else if (KFD_GC_VERSION(kfd) == IP_VERSION(9, 4, 1)) {
395 BUILD_BUG_ON(sizeof(cwsr_trap_arcturus_hex) > PAGE_SIZE);
396 kfd->cwsr_isa = cwsr_trap_arcturus_hex;
397 kfd->cwsr_isa_size = sizeof(cwsr_trap_arcturus_hex);
398 } else if (KFD_GC_VERSION(kfd) == IP_VERSION(9, 4, 2)) {
399 BUILD_BUG_ON(sizeof(cwsr_trap_aldebaran_hex) > PAGE_SIZE);
400 kfd->cwsr_isa = cwsr_trap_aldebaran_hex;
401 kfd->cwsr_isa_size = sizeof(cwsr_trap_aldebaran_hex);
402 } else if (KFD_GC_VERSION(kfd) < IP_VERSION(10, 1, 1)) {
403 BUILD_BUG_ON(sizeof(cwsr_trap_gfx9_hex) > PAGE_SIZE);
404 kfd->cwsr_isa = cwsr_trap_gfx9_hex;
405 kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx9_hex);
406 } else if (KFD_GC_VERSION(kfd) < IP_VERSION(10, 3, 0)) {
407 BUILD_BUG_ON(sizeof(cwsr_trap_nv1x_hex) > PAGE_SIZE);
408 kfd->cwsr_isa = cwsr_trap_nv1x_hex;
409 kfd->cwsr_isa_size = sizeof(cwsr_trap_nv1x_hex);
411 BUILD_BUG_ON(sizeof(cwsr_trap_gfx10_hex) > PAGE_SIZE);
412 kfd->cwsr_isa = cwsr_trap_gfx10_hex;
413 kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx10_hex);
416 kfd->cwsr_enabled = true;
420 static int kfd_gws_init(struct kfd_dev *kfd)
424 if (kfd->dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS)
427 if (hws_gws_support || (KFD_IS_SOC15(kfd) &&
428 ((KFD_GC_VERSION(kfd) == IP_VERSION(9, 0, 1)
429 && kfd->mec2_fw_version >= 0x81b3) ||
430 (KFD_GC_VERSION(kfd) <= IP_VERSION(9, 4, 0)
431 && kfd->mec2_fw_version >= 0x1b3) ||
432 (KFD_GC_VERSION(kfd) == IP_VERSION(9, 4, 1)
433 && kfd->mec2_fw_version >= 0x30) ||
434 (KFD_GC_VERSION(kfd) == IP_VERSION(9, 4, 2)
435 && kfd->mec2_fw_version >= 0x28))))
436 ret = amdgpu_amdkfd_alloc_gws(kfd->adev,
437 kfd->adev->gds.gws_size, &kfd->gws);
442 static void kfd_smi_init(struct kfd_dev *dev)
444 INIT_LIST_HEAD(&dev->smi_clients);
445 spin_lock_init(&dev->smi_lock);
448 bool kgd2kfd_device_init(struct kfd_dev *kfd,
449 struct drm_device *ddev,
450 const struct kgd2kfd_shared_resources *gpu_resources)
452 unsigned int size, map_process_packet_size;
455 kfd->mec_fw_version = amdgpu_amdkfd_get_fw_version(kfd->adev,
457 kfd->mec2_fw_version = amdgpu_amdkfd_get_fw_version(kfd->adev,
459 kfd->sdma_fw_version = amdgpu_amdkfd_get_fw_version(kfd->adev,
461 kfd->shared_resources = *gpu_resources;
463 kfd->vm_info.first_vmid_kfd = ffs(gpu_resources->compute_vmid_bitmap)-1;
464 kfd->vm_info.last_vmid_kfd = fls(gpu_resources->compute_vmid_bitmap)-1;
465 kfd->vm_info.vmid_num_kfd = kfd->vm_info.last_vmid_kfd
466 - kfd->vm_info.first_vmid_kfd + 1;
468 /* Allow BIF to recode atomics to PCIe 3.0 AtomicOps.
469 * 32 and 64-bit requests are possible and must be
472 kfd->pci_atomic_requested = amdgpu_amdkfd_have_atomics_support(kfd->adev);
473 if (!kfd->pci_atomic_requested &&
474 kfd->device_info.needs_pci_atomics &&
475 (!kfd->device_info.no_atomic_fw_version ||
476 kfd->mec_fw_version < kfd->device_info.no_atomic_fw_version)) {
478 "skipped device %x:%x, PCI rejects atomics %d<%d\n",
479 kfd->pdev->vendor, kfd->pdev->device,
481 kfd->device_info.no_atomic_fw_version);
485 /* Verify module parameters regarding mapped process number*/
486 if ((hws_max_conc_proc < 0)
487 || (hws_max_conc_proc > kfd->vm_info.vmid_num_kfd)) {
489 "hws_max_conc_proc %d must be between 0 and %d, use %d instead\n",
490 hws_max_conc_proc, kfd->vm_info.vmid_num_kfd,
491 kfd->vm_info.vmid_num_kfd);
492 kfd->max_proc_per_quantum = kfd->vm_info.vmid_num_kfd;
494 kfd->max_proc_per_quantum = hws_max_conc_proc;
496 /* calculate max size of mqds needed for queues */
497 size = max_num_of_queues_per_device *
498 kfd->device_info.mqd_size_aligned;
501 * calculate max size of runlist packet.
502 * There can be only 2 packets at once
504 map_process_packet_size = KFD_GC_VERSION(kfd) == IP_VERSION(9, 4, 2) ?
505 sizeof(struct pm4_mes_map_process_aldebaran) :
506 sizeof(struct pm4_mes_map_process);
507 size += (KFD_MAX_NUM_OF_PROCESSES * map_process_packet_size +
508 max_num_of_queues_per_device * sizeof(struct pm4_mes_map_queues)
509 + sizeof(struct pm4_mes_runlist)) * 2;
511 /* Add size of HIQ & DIQ */
512 size += KFD_KERNEL_QUEUE_SIZE * 2;
514 /* add another 512KB for all other allocations on gart (HPD, fences) */
517 if (amdgpu_amdkfd_alloc_gtt_mem(
518 kfd->adev, size, &kfd->gtt_mem,
519 &kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr,
521 dev_err(kfd_device, "Could not allocate %d bytes\n", size);
522 goto alloc_gtt_mem_failure;
525 dev_info(kfd_device, "Allocated %d bytes on gart\n", size);
527 /* Initialize GTT sa with 512 byte chunk size */
528 if (kfd_gtt_sa_init(kfd, size, 512) != 0) {
529 dev_err(kfd_device, "Error initializing gtt sub-allocator\n");
530 goto kfd_gtt_sa_init_error;
533 if (kfd_doorbell_init(kfd)) {
535 "Error initializing doorbell aperture\n");
536 goto kfd_doorbell_error;
539 kfd->hive_id = kfd->adev->gmc.xgmi.hive_id;
541 kfd->noretry = kfd->adev->gmc.noretry;
543 if (kfd_interrupt_init(kfd)) {
544 dev_err(kfd_device, "Error initializing interrupts\n");
545 goto kfd_interrupt_error;
548 kfd->dqm = device_queue_manager_init(kfd);
550 dev_err(kfd_device, "Error initializing queue manager\n");
551 goto device_queue_manager_error;
554 /* If supported on this device, allocate global GWS that is shared
555 * by all KFD processes
557 if (kfd_gws_init(kfd)) {
558 dev_err(kfd_device, "Could not allocate %d gws\n",
559 kfd->adev->gds.gws_size);
563 /* If CRAT is broken, won't set iommu enabled */
564 kfd_double_confirm_iommu_support(kfd);
566 if (kfd_iommu_device_init(kfd)) {
567 kfd->use_iommu_v2 = false;
568 dev_err(kfd_device, "Error initializing iommuv2\n");
569 goto device_iommu_error;
574 svm_migrate_init(kfd->adev);
576 if (kgd2kfd_resume_iommu(kfd))
577 goto device_iommu_error;
580 goto kfd_resume_error;
582 if (kfd_topology_add_device(kfd)) {
583 dev_err(kfd_device, "Error adding device to topology\n");
584 goto kfd_topology_add_device_error;
589 kfd->init_complete = true;
590 dev_info(kfd_device, "added device %x:%x\n", kfd->pdev->vendor,
593 pr_debug("Starting kfd with the following scheduling policy %d\n",
594 kfd->dqm->sched_policy);
598 kfd_topology_add_device_error:
602 device_queue_manager_uninit(kfd->dqm);
603 device_queue_manager_error:
604 kfd_interrupt_exit(kfd);
606 kfd_doorbell_fini(kfd);
608 kfd_gtt_sa_fini(kfd);
609 kfd_gtt_sa_init_error:
610 amdgpu_amdkfd_free_gtt_mem(kfd->adev, kfd->gtt_mem);
611 alloc_gtt_mem_failure:
613 amdgpu_amdkfd_free_gws(kfd->adev, kfd->gws);
615 "device %x:%x NOT added due to errors\n",
616 kfd->pdev->vendor, kfd->pdev->device);
618 return kfd->init_complete;
621 void kgd2kfd_device_exit(struct kfd_dev *kfd)
623 if (kfd->init_complete) {
624 device_queue_manager_uninit(kfd->dqm);
625 kfd_interrupt_exit(kfd);
626 kfd_topology_remove_device(kfd);
627 kfd_doorbell_fini(kfd);
628 ida_destroy(&kfd->doorbell_ida);
629 kfd_gtt_sa_fini(kfd);
630 amdgpu_amdkfd_free_gtt_mem(kfd->adev, kfd->gtt_mem);
632 amdgpu_amdkfd_free_gws(kfd->adev, kfd->gws);
638 int kgd2kfd_pre_reset(struct kfd_dev *kfd)
640 if (!kfd->init_complete)
643 kfd_smi_event_update_gpu_reset(kfd, false);
645 kfd->dqm->ops.pre_reset(kfd->dqm);
647 kgd2kfd_suspend(kfd, false);
649 kfd_signal_reset_event(kfd);
654 * Fix me. KFD won't be able to resume existing process for now.
655 * We will keep all existing process in a evicted state and
656 * wait the process to be terminated.
659 int kgd2kfd_post_reset(struct kfd_dev *kfd)
663 if (!kfd->init_complete)
666 ret = kfd_resume(kfd);
669 atomic_dec(&kfd_locked);
671 atomic_set(&kfd->sram_ecc_flag, 0);
673 kfd_smi_event_update_gpu_reset(kfd, true);
678 bool kfd_is_locked(void)
680 return (atomic_read(&kfd_locked) > 0);
683 void kgd2kfd_suspend(struct kfd_dev *kfd, bool run_pm)
685 if (!kfd->init_complete)
688 /* for runtime suspend, skip locking kfd */
690 /* For first KFD device suspend all the KFD processes */
691 if (atomic_inc_return(&kfd_locked) == 1)
692 kfd_suspend_all_processes();
695 kfd->dqm->ops.stop(kfd->dqm);
696 kfd_iommu_suspend(kfd);
699 int kgd2kfd_resume(struct kfd_dev *kfd, bool run_pm)
703 if (!kfd->init_complete)
706 ret = kfd_resume(kfd);
710 /* for runtime resume, skip unlocking kfd */
712 count = atomic_dec_return(&kfd_locked);
713 WARN_ONCE(count < 0, "KFD suspend / resume ref. error");
715 ret = kfd_resume_all_processes();
721 int kgd2kfd_resume_iommu(struct kfd_dev *kfd)
725 err = kfd_iommu_resume(kfd);
728 "Failed to resume IOMMU for device %x:%x\n",
729 kfd->pdev->vendor, kfd->pdev->device);
733 static int kfd_resume(struct kfd_dev *kfd)
737 err = kfd->dqm->ops.start(kfd->dqm);
740 "Error starting queue manager for device %x:%x\n",
741 kfd->pdev->vendor, kfd->pdev->device);
746 static inline void kfd_queue_work(struct workqueue_struct *wq,
747 struct work_struct *work)
751 cpu = new_cpu = smp_processor_id();
753 new_cpu = cpumask_next(new_cpu, cpu_online_mask) % nr_cpu_ids;
754 if (cpu_to_node(new_cpu) == numa_node_id())
756 } while (cpu != new_cpu);
758 queue_work_on(new_cpu, wq, work);
761 /* This is called directly from KGD at ISR. */
762 void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry)
764 uint32_t patched_ihre[KFD_MAX_RING_ENTRY_SIZE];
765 bool is_patched = false;
768 if (!kfd->init_complete)
771 if (kfd->device_info.ih_ring_entry_size > sizeof(patched_ihre)) {
772 dev_err_once(kfd_device, "Ring entry too small\n");
776 spin_lock_irqsave(&kfd->interrupt_lock, flags);
778 if (kfd->interrupts_active
779 && interrupt_is_wanted(kfd, ih_ring_entry,
780 patched_ihre, &is_patched)
781 && enqueue_ih_ring_entry(kfd,
782 is_patched ? patched_ihre : ih_ring_entry))
783 kfd_queue_work(kfd->ih_wq, &kfd->interrupt_work);
785 spin_unlock_irqrestore(&kfd->interrupt_lock, flags);
788 int kgd2kfd_quiesce_mm(struct mm_struct *mm)
790 struct kfd_process *p;
793 /* Because we are called from arbitrary context (workqueue) as opposed
794 * to process context, kfd_process could attempt to exit while we are
795 * running so the lookup function increments the process ref count.
797 p = kfd_lookup_process_by_mm(mm);
801 WARN(debug_evictions, "Evicting pid %d", p->lead_thread->pid);
802 r = kfd_process_evict_queues(p);
804 kfd_unref_process(p);
808 int kgd2kfd_resume_mm(struct mm_struct *mm)
810 struct kfd_process *p;
813 /* Because we are called from arbitrary context (workqueue) as opposed
814 * to process context, kfd_process could attempt to exit while we are
815 * running so the lookup function increments the process ref count.
817 p = kfd_lookup_process_by_mm(mm);
821 r = kfd_process_restore_queues(p);
823 kfd_unref_process(p);
827 /** kgd2kfd_schedule_evict_and_restore_process - Schedules work queue that will
828 * prepare for safe eviction of KFD BOs that belong to the specified
831 * @mm: mm_struct that identifies the specified KFD process
832 * @fence: eviction fence attached to KFD process BOs
835 int kgd2kfd_schedule_evict_and_restore_process(struct mm_struct *mm,
836 struct dma_fence *fence)
838 struct kfd_process *p;
839 unsigned long active_time;
840 unsigned long delay_jiffies = msecs_to_jiffies(PROCESS_ACTIVE_TIME_MS);
845 if (dma_fence_is_signaled(fence))
848 p = kfd_lookup_process_by_mm(mm);
852 if (fence->seqno == p->last_eviction_seqno)
855 p->last_eviction_seqno = fence->seqno;
857 /* Avoid KFD process starvation. Wait for at least
858 * PROCESS_ACTIVE_TIME_MS before evicting the process again
860 active_time = get_jiffies_64() - p->last_restore_timestamp;
861 if (delay_jiffies > active_time)
862 delay_jiffies -= active_time;
866 /* During process initialization eviction_work.dwork is initialized
867 * to kfd_evict_bo_worker
869 WARN(debug_evictions, "Scheduling eviction of pid %d in %ld jiffies",
870 p->lead_thread->pid, delay_jiffies);
871 schedule_delayed_work(&p->eviction_work, delay_jiffies);
873 kfd_unref_process(p);
877 static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
878 unsigned int chunk_size)
880 unsigned int num_of_longs;
882 if (WARN_ON(buf_size < chunk_size))
884 if (WARN_ON(buf_size == 0))
886 if (WARN_ON(chunk_size == 0))
889 kfd->gtt_sa_chunk_size = chunk_size;
890 kfd->gtt_sa_num_of_chunks = buf_size / chunk_size;
892 num_of_longs = (kfd->gtt_sa_num_of_chunks + BITS_PER_LONG - 1) /
895 kfd->gtt_sa_bitmap = kcalloc(num_of_longs, sizeof(long), GFP_KERNEL);
897 if (!kfd->gtt_sa_bitmap)
900 pr_debug("gtt_sa_num_of_chunks = %d, gtt_sa_bitmap = %p\n",
901 kfd->gtt_sa_num_of_chunks, kfd->gtt_sa_bitmap);
903 mutex_init(&kfd->gtt_sa_lock);
909 static void kfd_gtt_sa_fini(struct kfd_dev *kfd)
911 mutex_destroy(&kfd->gtt_sa_lock);
912 kfree(kfd->gtt_sa_bitmap);
915 static inline uint64_t kfd_gtt_sa_calc_gpu_addr(uint64_t start_addr,
916 unsigned int bit_num,
917 unsigned int chunk_size)
919 return start_addr + bit_num * chunk_size;
922 static inline uint32_t *kfd_gtt_sa_calc_cpu_addr(void *start_addr,
923 unsigned int bit_num,
924 unsigned int chunk_size)
926 return (uint32_t *) ((uint64_t) start_addr + bit_num * chunk_size);
929 int kfd_gtt_sa_allocate(struct kfd_dev *kfd, unsigned int size,
930 struct kfd_mem_obj **mem_obj)
932 unsigned int found, start_search, cur_size;
937 if (size > kfd->gtt_sa_num_of_chunks * kfd->gtt_sa_chunk_size)
940 *mem_obj = kzalloc(sizeof(struct kfd_mem_obj), GFP_KERNEL);
944 pr_debug("Allocated mem_obj = %p for size = %d\n", *mem_obj, size);
948 mutex_lock(&kfd->gtt_sa_lock);
950 kfd_gtt_restart_search:
951 /* Find the first chunk that is free */
952 found = find_next_zero_bit(kfd->gtt_sa_bitmap,
953 kfd->gtt_sa_num_of_chunks,
956 pr_debug("Found = %d\n", found);
958 /* If there wasn't any free chunk, bail out */
959 if (found == kfd->gtt_sa_num_of_chunks)
960 goto kfd_gtt_no_free_chunk;
962 /* Update fields of mem_obj */
963 (*mem_obj)->range_start = found;
964 (*mem_obj)->range_end = found;
965 (*mem_obj)->gpu_addr = kfd_gtt_sa_calc_gpu_addr(
966 kfd->gtt_start_gpu_addr,
968 kfd->gtt_sa_chunk_size);
969 (*mem_obj)->cpu_ptr = kfd_gtt_sa_calc_cpu_addr(
970 kfd->gtt_start_cpu_ptr,
972 kfd->gtt_sa_chunk_size);
974 pr_debug("gpu_addr = %p, cpu_addr = %p\n",
975 (uint64_t *) (*mem_obj)->gpu_addr, (*mem_obj)->cpu_ptr);
977 /* If we need only one chunk, mark it as allocated and get out */
978 if (size <= kfd->gtt_sa_chunk_size) {
979 pr_debug("Single bit\n");
980 set_bit(found, kfd->gtt_sa_bitmap);
984 /* Otherwise, try to see if we have enough contiguous chunks */
985 cur_size = size - kfd->gtt_sa_chunk_size;
987 (*mem_obj)->range_end =
988 find_next_zero_bit(kfd->gtt_sa_bitmap,
989 kfd->gtt_sa_num_of_chunks, ++found);
991 * If next free chunk is not contiguous than we need to
992 * restart our search from the last free chunk we found (which
993 * wasn't contiguous to the previous ones
995 if ((*mem_obj)->range_end != found) {
996 start_search = found;
997 goto kfd_gtt_restart_search;
1001 * If we reached end of buffer, bail out with error
1003 if (found == kfd->gtt_sa_num_of_chunks)
1004 goto kfd_gtt_no_free_chunk;
1006 /* Check if we don't need another chunk */
1007 if (cur_size <= kfd->gtt_sa_chunk_size)
1010 cur_size -= kfd->gtt_sa_chunk_size;
1012 } while (cur_size > 0);
1014 pr_debug("range_start = %d, range_end = %d\n",
1015 (*mem_obj)->range_start, (*mem_obj)->range_end);
1017 /* Mark the chunks as allocated */
1018 for (found = (*mem_obj)->range_start;
1019 found <= (*mem_obj)->range_end;
1021 set_bit(found, kfd->gtt_sa_bitmap);
1024 mutex_unlock(&kfd->gtt_sa_lock);
1027 kfd_gtt_no_free_chunk:
1028 pr_debug("Allocation failed with mem_obj = %p\n", *mem_obj);
1029 mutex_unlock(&kfd->gtt_sa_lock);
1034 int kfd_gtt_sa_free(struct kfd_dev *kfd, struct kfd_mem_obj *mem_obj)
1038 /* Act like kfree when trying to free a NULL object */
1042 pr_debug("Free mem_obj = %p, range_start = %d, range_end = %d\n",
1043 mem_obj, mem_obj->range_start, mem_obj->range_end);
1045 mutex_lock(&kfd->gtt_sa_lock);
1047 /* Mark the chunks as free */
1048 for (bit = mem_obj->range_start;
1049 bit <= mem_obj->range_end;
1051 clear_bit(bit, kfd->gtt_sa_bitmap);
1053 mutex_unlock(&kfd->gtt_sa_lock);
1059 void kgd2kfd_set_sram_ecc_flag(struct kfd_dev *kfd)
1062 atomic_inc(&kfd->sram_ecc_flag);
1065 void kfd_inc_compute_active(struct kfd_dev *kfd)
1067 if (atomic_inc_return(&kfd->compute_profile) == 1)
1068 amdgpu_amdkfd_set_compute_idle(kfd->adev, false);
1071 void kfd_dec_compute_active(struct kfd_dev *kfd)
1073 int count = atomic_dec_return(&kfd->compute_profile);
1076 amdgpu_amdkfd_set_compute_idle(kfd->adev, true);
1077 WARN_ONCE(count < 0, "Compute profile ref. count error");
1080 void kgd2kfd_smi_event_throttle(struct kfd_dev *kfd, uint64_t throttle_bitmask)
1082 if (kfd && kfd->init_complete)
1083 kfd_smi_event_update_thermal_throttling(kfd, throttle_bitmask);
1086 /* kfd_get_num_sdma_engines returns the number of PCIe optimized SDMA and
1087 * kfd_get_num_xgmi_sdma_engines returns the number of XGMI SDMA.
1088 * When the device has more than two engines, we reserve two for PCIe to enable
1089 * full-duplex and the rest are used as XGMI.
1091 unsigned int kfd_get_num_sdma_engines(struct kfd_dev *kdev)
1093 /* If XGMI is not supported, all SDMA engines are PCIe */
1094 if (!kdev->adev->gmc.xgmi.supported)
1095 return kdev->adev->sdma.num_instances;
1097 return min(kdev->adev->sdma.num_instances, 2);
1100 unsigned int kfd_get_num_xgmi_sdma_engines(struct kfd_dev *kdev)
1102 /* After reserved for PCIe, the rest of engines are XGMI */
1103 return kdev->adev->sdma.num_instances - kfd_get_num_sdma_engines(kdev);
1106 #if defined(CONFIG_DEBUG_FS)
1108 /* This function will send a package to HIQ to hang the HWS
1109 * which will trigger a GPU reset and bring the HWS back to normal state
1111 int kfd_debugfs_hang_hws(struct kfd_dev *dev)
1113 if (dev->dqm->sched_policy != KFD_SCHED_POLICY_HWS) {
1114 pr_err("HWS is not enabled");
1118 return dqm_debugfs_hang_hws(dev->dqm);