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;
56 extern const struct kfd2kgd_calls gfx_v11_kfd2kgd;
58 static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
59 unsigned int chunk_size);
60 static void kfd_gtt_sa_fini(struct kfd_dev *kfd);
62 static int kfd_resume(struct kfd_dev *kfd);
64 static void kfd_device_info_set_sdma_info(struct kfd_dev *kfd)
66 uint32_t sdma_version = kfd->adev->ip_versions[SDMA0_HWIP][0];
68 switch (sdma_version) {
69 case IP_VERSION(4, 0, 0):/* VEGA10 */
70 case IP_VERSION(4, 0, 1):/* VEGA12 */
71 case IP_VERSION(4, 1, 0):/* RAVEN */
72 case IP_VERSION(4, 1, 1):/* RAVEN */
73 case IP_VERSION(4, 1, 2):/* RENOIR */
74 case IP_VERSION(5, 2, 1):/* VANGOGH */
75 case IP_VERSION(5, 2, 3):/* YELLOW_CARP */
76 case IP_VERSION(5, 2, 6):/* GC 10.3.6 */
77 case IP_VERSION(5, 2, 7):/* GC 10.3.7 */
78 kfd->device_info.num_sdma_queues_per_engine = 2;
80 case IP_VERSION(4, 2, 0):/* VEGA20 */
81 case IP_VERSION(4, 2, 2):/* ARCTURUS */
82 case IP_VERSION(4, 4, 0):/* ALDEBARAN */
83 case IP_VERSION(5, 0, 0):/* NAVI10 */
84 case IP_VERSION(5, 0, 1):/* CYAN_SKILLFISH */
85 case IP_VERSION(5, 0, 2):/* NAVI14 */
86 case IP_VERSION(5, 0, 5):/* NAVI12 */
87 case IP_VERSION(5, 2, 0):/* SIENNA_CICHLID */
88 case IP_VERSION(5, 2, 2):/* NAVY_FLOUNDER */
89 case IP_VERSION(5, 2, 4):/* DIMGREY_CAVEFISH */
90 case IP_VERSION(5, 2, 5):/* BEIGE_GOBY */
91 case IP_VERSION(6, 0, 0):
92 case IP_VERSION(6, 0, 1):
93 case IP_VERSION(6, 0, 2):
94 kfd->device_info.num_sdma_queues_per_engine = 8;
98 "Default sdma queue per engine(8) is set due to mismatch of sdma ip block(SDMA_HWIP:0x%x).\n",
100 kfd->device_info.num_sdma_queues_per_engine = 8;
103 switch (sdma_version) {
104 case IP_VERSION(6, 0, 0):
105 case IP_VERSION(6, 0, 2):
106 /* Reserve 1 for paging and 1 for gfx */
107 kfd->device_info.num_reserved_sdma_queues_per_engine = 2;
108 /* BIT(0)=engine-0 queue-0; BIT(1)=engine-1 queue-0; BIT(2)=engine-0 queue-1; ... */
109 kfd->device_info.reserved_sdma_queues_bitmap = 0xFULL;
111 case IP_VERSION(6, 0, 1):
112 /* Reserve 1 for paging and 1 for gfx */
113 kfd->device_info.num_reserved_sdma_queues_per_engine = 2;
114 /* BIT(0)=engine-0 queue-0; BIT(1)=engine-0 queue-1; ... */
115 kfd->device_info.reserved_sdma_queues_bitmap = 0x3ULL;
122 static void kfd_device_info_set_event_interrupt_class(struct kfd_dev *kfd)
124 uint32_t gc_version = KFD_GC_VERSION(kfd);
126 switch (gc_version) {
127 case IP_VERSION(9, 0, 1): /* VEGA10 */
128 case IP_VERSION(9, 1, 0): /* RAVEN */
129 case IP_VERSION(9, 2, 1): /* VEGA12 */
130 case IP_VERSION(9, 2, 2): /* RAVEN */
131 case IP_VERSION(9, 3, 0): /* RENOIR */
132 case IP_VERSION(9, 4, 0): /* VEGA20 */
133 case IP_VERSION(9, 4, 1): /* ARCTURUS */
134 case IP_VERSION(9, 4, 2): /* ALDEBARAN */
135 case IP_VERSION(10, 3, 1): /* VANGOGH */
136 case IP_VERSION(10, 3, 3): /* YELLOW_CARP */
137 case IP_VERSION(10, 3, 6): /* GC 10.3.6 */
138 case IP_VERSION(10, 3, 7): /* GC 10.3.7 */
139 case IP_VERSION(10, 1, 3): /* CYAN_SKILLFISH */
140 case IP_VERSION(10, 1, 4):
141 case IP_VERSION(10, 1, 10): /* NAVI10 */
142 case IP_VERSION(10, 1, 2): /* NAVI12 */
143 case IP_VERSION(10, 1, 1): /* NAVI14 */
144 case IP_VERSION(10, 3, 0): /* SIENNA_CICHLID */
145 case IP_VERSION(10, 3, 2): /* NAVY_FLOUNDER */
146 case IP_VERSION(10, 3, 4): /* DIMGREY_CAVEFISH */
147 case IP_VERSION(10, 3, 5): /* BEIGE_GOBY */
148 kfd->device_info.event_interrupt_class = &event_interrupt_class_v9;
150 case IP_VERSION(11, 0, 0):
151 case IP_VERSION(11, 0, 1):
152 case IP_VERSION(11, 0, 2):
153 kfd->device_info.event_interrupt_class = &event_interrupt_class_v11;
156 dev_warn(kfd_device, "v9 event interrupt handler is set due to "
157 "mismatch of gc ip block(GC_HWIP:0x%x).\n", gc_version);
158 kfd->device_info.event_interrupt_class = &event_interrupt_class_v9;
162 static void kfd_device_info_init(struct kfd_dev *kfd,
163 bool vf, uint32_t gfx_target_version)
165 uint32_t gc_version = KFD_GC_VERSION(kfd);
166 uint32_t asic_type = kfd->adev->asic_type;
168 kfd->device_info.max_pasid_bits = 16;
169 kfd->device_info.max_no_of_hqd = 24;
170 kfd->device_info.num_of_watch_points = 4;
171 kfd->device_info.mqd_size_aligned = MQD_SIZE_ALIGNED;
172 kfd->device_info.gfx_target_version = gfx_target_version;
174 if (KFD_IS_SOC15(kfd)) {
175 kfd->device_info.doorbell_size = 8;
176 kfd->device_info.ih_ring_entry_size = 8 * sizeof(uint32_t);
177 kfd->device_info.supports_cwsr = true;
179 kfd_device_info_set_sdma_info(kfd);
181 kfd_device_info_set_event_interrupt_class(kfd);
184 if (gc_version == IP_VERSION(9, 1, 0) ||
185 gc_version == IP_VERSION(9, 2, 2))
186 kfd->device_info.needs_iommu_device = true;
188 if (gc_version < IP_VERSION(11, 0, 0)) {
189 /* Navi2x+, Navi1x+ */
190 if (gc_version == IP_VERSION(10, 3, 6))
191 kfd->device_info.no_atomic_fw_version = 14;
192 else if (gc_version == IP_VERSION(10, 3, 7))
193 kfd->device_info.no_atomic_fw_version = 3;
194 else if (gc_version >= IP_VERSION(10, 3, 0))
195 kfd->device_info.no_atomic_fw_version = 92;
196 else if (gc_version >= IP_VERSION(10, 1, 1))
197 kfd->device_info.no_atomic_fw_version = 145;
200 if (gc_version >= IP_VERSION(10, 1, 1))
201 kfd->device_info.needs_pci_atomics = true;
204 kfd->device_info.doorbell_size = 4;
205 kfd->device_info.ih_ring_entry_size = 4 * sizeof(uint32_t);
206 kfd->device_info.event_interrupt_class = &event_interrupt_class_cik;
207 kfd->device_info.num_sdma_queues_per_engine = 2;
209 if (asic_type != CHIP_KAVERI &&
210 asic_type != CHIP_HAWAII &&
211 asic_type != CHIP_TONGA)
212 kfd->device_info.supports_cwsr = true;
214 if (asic_type == CHIP_KAVERI ||
215 asic_type == CHIP_CARRIZO)
216 kfd->device_info.needs_iommu_device = true;
218 if (asic_type != CHIP_HAWAII && !vf)
219 kfd->device_info.needs_pci_atomics = true;
223 struct kfd_dev *kgd2kfd_probe(struct amdgpu_device *adev, bool vf)
225 struct kfd_dev *kfd = NULL;
226 const struct kfd2kgd_calls *f2g = NULL;
227 struct pci_dev *pdev = adev->pdev;
228 uint32_t gfx_target_version = 0;
230 switch (adev->asic_type) {
231 #ifdef KFD_SUPPORT_IOMMU_V2
232 #ifdef CONFIG_DRM_AMDGPU_CIK
234 gfx_target_version = 70000;
236 f2g = &gfx_v7_kfd2kgd;
240 gfx_target_version = 80001;
242 f2g = &gfx_v8_kfd2kgd;
245 #ifdef CONFIG_DRM_AMDGPU_CIK
247 gfx_target_version = 70001;
248 if (!amdgpu_exp_hw_support)
250 "KFD support on Hawaii is experimental. See modparam exp_hw_support\n"
253 f2g = &gfx_v7_kfd2kgd;
257 gfx_target_version = 80002;
259 f2g = &gfx_v8_kfd2kgd;
262 gfx_target_version = 80003;
263 f2g = &gfx_v8_kfd2kgd;
266 gfx_target_version = 80003;
267 f2g = &gfx_v8_kfd2kgd;
270 gfx_target_version = 80003;
272 f2g = &gfx_v8_kfd2kgd;
275 gfx_target_version = 80003;
277 f2g = &gfx_v8_kfd2kgd;
280 gfx_target_version = 80003;
282 f2g = &gfx_v8_kfd2kgd;
285 switch (adev->ip_versions[GC_HWIP][0]) {
287 case IP_VERSION(9, 0, 1):
288 gfx_target_version = 90000;
289 f2g = &gfx_v9_kfd2kgd;
291 #ifdef KFD_SUPPORT_IOMMU_V2
293 case IP_VERSION(9, 1, 0):
294 case IP_VERSION(9, 2, 2):
295 gfx_target_version = 90002;
297 f2g = &gfx_v9_kfd2kgd;
301 case IP_VERSION(9, 2, 1):
302 gfx_target_version = 90004;
304 f2g = &gfx_v9_kfd2kgd;
307 case IP_VERSION(9, 3, 0):
308 gfx_target_version = 90012;
310 f2g = &gfx_v9_kfd2kgd;
313 case IP_VERSION(9, 4, 0):
314 gfx_target_version = 90006;
316 f2g = &gfx_v9_kfd2kgd;
319 case IP_VERSION(9, 4, 1):
320 gfx_target_version = 90008;
321 f2g = &arcturus_kfd2kgd;
324 case IP_VERSION(9, 4, 2):
325 gfx_target_version = 90010;
326 f2g = &aldebaran_kfd2kgd;
329 case IP_VERSION(10, 1, 10):
330 gfx_target_version = 100100;
332 f2g = &gfx_v10_kfd2kgd;
335 case IP_VERSION(10, 1, 2):
336 gfx_target_version = 100101;
337 f2g = &gfx_v10_kfd2kgd;
340 case IP_VERSION(10, 1, 1):
341 gfx_target_version = 100102;
343 f2g = &gfx_v10_kfd2kgd;
346 case IP_VERSION(10, 1, 3):
347 case IP_VERSION(10, 1, 4):
348 gfx_target_version = 100103;
350 f2g = &gfx_v10_kfd2kgd;
353 case IP_VERSION(10, 3, 0):
354 gfx_target_version = 100300;
355 f2g = &gfx_v10_3_kfd2kgd;
358 case IP_VERSION(10, 3, 2):
359 gfx_target_version = 100301;
360 f2g = &gfx_v10_3_kfd2kgd;
363 case IP_VERSION(10, 3, 1):
364 gfx_target_version = 100303;
366 f2g = &gfx_v10_3_kfd2kgd;
368 /* Dimgrey Cavefish */
369 case IP_VERSION(10, 3, 4):
370 gfx_target_version = 100302;
371 f2g = &gfx_v10_3_kfd2kgd;
374 case IP_VERSION(10, 3, 5):
375 gfx_target_version = 100304;
376 f2g = &gfx_v10_3_kfd2kgd;
379 case IP_VERSION(10, 3, 3):
380 gfx_target_version = 100305;
382 f2g = &gfx_v10_3_kfd2kgd;
384 case IP_VERSION(10, 3, 6):
385 gfx_target_version = 100306;
387 f2g = &gfx_v10_3_kfd2kgd;
389 case IP_VERSION(10, 3, 7):
390 gfx_target_version = 100307;
392 f2g = &gfx_v10_3_kfd2kgd;
394 case IP_VERSION(11, 0, 0):
395 gfx_target_version = 110000;
396 f2g = &gfx_v11_kfd2kgd;
398 case IP_VERSION(11, 0, 1):
399 gfx_target_version = 110003;
400 f2g = &gfx_v11_kfd2kgd;
402 case IP_VERSION(11, 0, 2):
403 gfx_target_version = 110002;
404 f2g = &gfx_v11_kfd2kgd;
413 if (adev->ip_versions[GC_HWIP][0])
414 dev_err(kfd_device, "GC IP %06x %s not supported in kfd\n",
415 adev->ip_versions[GC_HWIP][0], vf ? "VF" : "");
417 dev_err(kfd_device, "%s %s not supported in kfd\n",
418 amdgpu_asic_name[adev->asic_type], vf ? "VF" : "");
422 kfd = kzalloc(sizeof(*kfd), GFP_KERNEL);
427 kfd_device_info_init(kfd, vf, gfx_target_version);
429 kfd->init_complete = false;
431 atomic_set(&kfd->compute_profile, 0);
433 mutex_init(&kfd->doorbell_mutex);
434 memset(&kfd->doorbell_available_index, 0,
435 sizeof(kfd->doorbell_available_index));
437 atomic_set(&kfd->sram_ecc_flag, 0);
439 ida_init(&kfd->doorbell_ida);
444 static void kfd_cwsr_init(struct kfd_dev *kfd)
446 if (cwsr_enable && kfd->device_info.supports_cwsr) {
447 if (KFD_GC_VERSION(kfd) < IP_VERSION(9, 0, 1)) {
448 BUILD_BUG_ON(sizeof(cwsr_trap_gfx8_hex) > PAGE_SIZE);
449 kfd->cwsr_isa = cwsr_trap_gfx8_hex;
450 kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx8_hex);
451 } else if (KFD_GC_VERSION(kfd) == IP_VERSION(9, 4, 1)) {
452 BUILD_BUG_ON(sizeof(cwsr_trap_arcturus_hex) > PAGE_SIZE);
453 kfd->cwsr_isa = cwsr_trap_arcturus_hex;
454 kfd->cwsr_isa_size = sizeof(cwsr_trap_arcturus_hex);
455 } else if (KFD_GC_VERSION(kfd) == IP_VERSION(9, 4, 2)) {
456 BUILD_BUG_ON(sizeof(cwsr_trap_aldebaran_hex) > PAGE_SIZE);
457 kfd->cwsr_isa = cwsr_trap_aldebaran_hex;
458 kfd->cwsr_isa_size = sizeof(cwsr_trap_aldebaran_hex);
459 } else if (KFD_GC_VERSION(kfd) < IP_VERSION(10, 1, 1)) {
460 BUILD_BUG_ON(sizeof(cwsr_trap_gfx9_hex) > PAGE_SIZE);
461 kfd->cwsr_isa = cwsr_trap_gfx9_hex;
462 kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx9_hex);
463 } else if (KFD_GC_VERSION(kfd) < IP_VERSION(10, 3, 0)) {
464 BUILD_BUG_ON(sizeof(cwsr_trap_nv1x_hex) > PAGE_SIZE);
465 kfd->cwsr_isa = cwsr_trap_nv1x_hex;
466 kfd->cwsr_isa_size = sizeof(cwsr_trap_nv1x_hex);
467 } else if (KFD_GC_VERSION(kfd) < IP_VERSION(11, 0, 0)) {
468 BUILD_BUG_ON(sizeof(cwsr_trap_gfx10_hex) > PAGE_SIZE);
469 kfd->cwsr_isa = cwsr_trap_gfx10_hex;
470 kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx10_hex);
472 BUILD_BUG_ON(sizeof(cwsr_trap_gfx11_hex) > PAGE_SIZE);
473 kfd->cwsr_isa = cwsr_trap_gfx11_hex;
474 kfd->cwsr_isa_size = sizeof(cwsr_trap_gfx11_hex);
477 kfd->cwsr_enabled = true;
481 static int kfd_gws_init(struct kfd_dev *kfd)
485 if (kfd->dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS)
488 if (hws_gws_support || (KFD_IS_SOC15(kfd) &&
489 ((KFD_GC_VERSION(kfd) == IP_VERSION(9, 0, 1)
490 && kfd->mec2_fw_version >= 0x81b3) ||
491 (KFD_GC_VERSION(kfd) <= IP_VERSION(9, 4, 0)
492 && kfd->mec2_fw_version >= 0x1b3) ||
493 (KFD_GC_VERSION(kfd) == IP_VERSION(9, 4, 1)
494 && kfd->mec2_fw_version >= 0x30) ||
495 (KFD_GC_VERSION(kfd) == IP_VERSION(9, 4, 2)
496 && kfd->mec2_fw_version >= 0x28))))
497 ret = amdgpu_amdkfd_alloc_gws(kfd->adev,
498 kfd->adev->gds.gws_size, &kfd->gws);
503 static void kfd_smi_init(struct kfd_dev *dev)
505 INIT_LIST_HEAD(&dev->smi_clients);
506 spin_lock_init(&dev->smi_lock);
509 bool kgd2kfd_device_init(struct kfd_dev *kfd,
510 struct drm_device *ddev,
511 const struct kgd2kfd_shared_resources *gpu_resources)
513 unsigned int size, map_process_packet_size;
516 kfd->mec_fw_version = amdgpu_amdkfd_get_fw_version(kfd->adev,
518 kfd->mec2_fw_version = amdgpu_amdkfd_get_fw_version(kfd->adev,
520 kfd->sdma_fw_version = amdgpu_amdkfd_get_fw_version(kfd->adev,
522 kfd->shared_resources = *gpu_resources;
524 kfd->vm_info.first_vmid_kfd = ffs(gpu_resources->compute_vmid_bitmap)-1;
525 kfd->vm_info.last_vmid_kfd = fls(gpu_resources->compute_vmid_bitmap)-1;
526 kfd->vm_info.vmid_num_kfd = kfd->vm_info.last_vmid_kfd
527 - kfd->vm_info.first_vmid_kfd + 1;
529 /* Allow BIF to recode atomics to PCIe 3.0 AtomicOps.
530 * 32 and 64-bit requests are possible and must be
533 kfd->pci_atomic_requested = amdgpu_amdkfd_have_atomics_support(kfd->adev);
534 if (!kfd->pci_atomic_requested &&
535 kfd->device_info.needs_pci_atomics &&
536 (!kfd->device_info.no_atomic_fw_version ||
537 kfd->mec_fw_version < kfd->device_info.no_atomic_fw_version)) {
539 "skipped device %x:%x, PCI rejects atomics %d<%d\n",
540 kfd->pdev->vendor, kfd->pdev->device,
542 kfd->device_info.no_atomic_fw_version);
546 /* Verify module parameters regarding mapped process number*/
547 if (hws_max_conc_proc >= 0)
548 kfd->max_proc_per_quantum = min((u32)hws_max_conc_proc, kfd->vm_info.vmid_num_kfd);
550 kfd->max_proc_per_quantum = kfd->vm_info.vmid_num_kfd;
552 /* calculate max size of mqds needed for queues */
553 size = max_num_of_queues_per_device *
554 kfd->device_info.mqd_size_aligned;
557 * calculate max size of runlist packet.
558 * There can be only 2 packets at once
560 map_process_packet_size = KFD_GC_VERSION(kfd) == IP_VERSION(9, 4, 2) ?
561 sizeof(struct pm4_mes_map_process_aldebaran) :
562 sizeof(struct pm4_mes_map_process);
563 size += (KFD_MAX_NUM_OF_PROCESSES * map_process_packet_size +
564 max_num_of_queues_per_device * sizeof(struct pm4_mes_map_queues)
565 + sizeof(struct pm4_mes_runlist)) * 2;
567 /* Add size of HIQ & DIQ */
568 size += KFD_KERNEL_QUEUE_SIZE * 2;
570 /* add another 512KB for all other allocations on gart (HPD, fences) */
573 if (amdgpu_amdkfd_alloc_gtt_mem(
574 kfd->adev, size, &kfd->gtt_mem,
575 &kfd->gtt_start_gpu_addr, &kfd->gtt_start_cpu_ptr,
577 dev_err(kfd_device, "Could not allocate %d bytes\n", size);
578 goto alloc_gtt_mem_failure;
581 dev_info(kfd_device, "Allocated %d bytes on gart\n", size);
583 /* Initialize GTT sa with 512 byte chunk size */
584 if (kfd_gtt_sa_init(kfd, size, 512) != 0) {
585 dev_err(kfd_device, "Error initializing gtt sub-allocator\n");
586 goto kfd_gtt_sa_init_error;
589 if (kfd_doorbell_init(kfd)) {
591 "Error initializing doorbell aperture\n");
592 goto kfd_doorbell_error;
595 if (amdgpu_use_xgmi_p2p)
596 kfd->hive_id = kfd->adev->gmc.xgmi.hive_id;
598 kfd->noretry = kfd->adev->gmc.noretry;
600 if (kfd_interrupt_init(kfd)) {
601 dev_err(kfd_device, "Error initializing interrupts\n");
602 goto kfd_interrupt_error;
605 kfd->dqm = device_queue_manager_init(kfd);
607 dev_err(kfd_device, "Error initializing queue manager\n");
608 goto device_queue_manager_error;
611 /* If supported on this device, allocate global GWS that is shared
612 * by all KFD processes
614 if (kfd_gws_init(kfd)) {
615 dev_err(kfd_device, "Could not allocate %d gws\n",
616 kfd->adev->gds.gws_size);
620 /* If CRAT is broken, won't set iommu enabled */
621 kfd_double_confirm_iommu_support(kfd);
623 if (kfd_iommu_device_init(kfd)) {
624 kfd->use_iommu_v2 = false;
625 dev_err(kfd_device, "Error initializing iommuv2\n");
626 goto device_iommu_error;
631 svm_migrate_init(kfd->adev);
633 if (kgd2kfd_resume_iommu(kfd))
634 goto device_iommu_error;
637 goto kfd_resume_error;
639 amdgpu_amdkfd_get_local_mem_info(kfd->adev, &kfd->local_mem_info);
641 if (kfd_topology_add_device(kfd)) {
642 dev_err(kfd_device, "Error adding device to topology\n");
643 goto kfd_topology_add_device_error;
648 kfd->init_complete = true;
649 dev_info(kfd_device, "added device %x:%x\n", kfd->pdev->vendor,
652 pr_debug("Starting kfd with the following scheduling policy %d\n",
653 kfd->dqm->sched_policy);
657 kfd_topology_add_device_error:
661 device_queue_manager_uninit(kfd->dqm);
662 device_queue_manager_error:
663 kfd_interrupt_exit(kfd);
665 kfd_doorbell_fini(kfd);
667 kfd_gtt_sa_fini(kfd);
668 kfd_gtt_sa_init_error:
669 amdgpu_amdkfd_free_gtt_mem(kfd->adev, kfd->gtt_mem);
670 alloc_gtt_mem_failure:
672 amdgpu_amdkfd_free_gws(kfd->adev, kfd->gws);
674 "device %x:%x NOT added due to errors\n",
675 kfd->pdev->vendor, kfd->pdev->device);
677 return kfd->init_complete;
680 void kgd2kfd_device_exit(struct kfd_dev *kfd)
682 if (kfd->init_complete) {
683 device_queue_manager_uninit(kfd->dqm);
684 kfd_interrupt_exit(kfd);
685 kfd_topology_remove_device(kfd);
686 kfd_doorbell_fini(kfd);
687 ida_destroy(&kfd->doorbell_ida);
688 kfd_gtt_sa_fini(kfd);
689 amdgpu_amdkfd_free_gtt_mem(kfd->adev, kfd->gtt_mem);
691 amdgpu_amdkfd_free_gws(kfd->adev, kfd->gws);
697 int kgd2kfd_pre_reset(struct kfd_dev *kfd)
699 if (!kfd->init_complete)
702 kfd_smi_event_update_gpu_reset(kfd, false);
704 kfd->dqm->ops.pre_reset(kfd->dqm);
706 kgd2kfd_suspend(kfd, false);
708 kfd_signal_reset_event(kfd);
713 * Fix me. KFD won't be able to resume existing process for now.
714 * We will keep all existing process in a evicted state and
715 * wait the process to be terminated.
718 int kgd2kfd_post_reset(struct kfd_dev *kfd)
722 if (!kfd->init_complete)
725 ret = kfd_resume(kfd);
728 atomic_dec(&kfd_locked);
730 atomic_set(&kfd->sram_ecc_flag, 0);
732 kfd_smi_event_update_gpu_reset(kfd, true);
737 bool kfd_is_locked(void)
739 return (atomic_read(&kfd_locked) > 0);
742 void kgd2kfd_suspend(struct kfd_dev *kfd, bool run_pm)
744 if (!kfd->init_complete)
747 /* for runtime suspend, skip locking kfd */
749 /* For first KFD device suspend all the KFD processes */
750 if (atomic_inc_return(&kfd_locked) == 1)
751 kfd_suspend_all_processes();
754 kfd->dqm->ops.stop(kfd->dqm);
755 kfd_iommu_suspend(kfd);
758 int kgd2kfd_resume(struct kfd_dev *kfd, bool run_pm)
762 if (!kfd->init_complete)
765 ret = kfd_resume(kfd);
769 /* for runtime resume, skip unlocking kfd */
771 count = atomic_dec_return(&kfd_locked);
772 WARN_ONCE(count < 0, "KFD suspend / resume ref. error");
774 ret = kfd_resume_all_processes();
780 int kgd2kfd_resume_iommu(struct kfd_dev *kfd)
784 err = kfd_iommu_resume(kfd);
787 "Failed to resume IOMMU for device %x:%x\n",
788 kfd->pdev->vendor, kfd->pdev->device);
792 static int kfd_resume(struct kfd_dev *kfd)
796 err = kfd->dqm->ops.start(kfd->dqm);
799 "Error starting queue manager for device %x:%x\n",
800 kfd->pdev->vendor, kfd->pdev->device);
805 static inline void kfd_queue_work(struct workqueue_struct *wq,
806 struct work_struct *work)
810 cpu = new_cpu = smp_processor_id();
812 new_cpu = cpumask_next(new_cpu, cpu_online_mask) % nr_cpu_ids;
813 if (cpu_to_node(new_cpu) == numa_node_id())
815 } while (cpu != new_cpu);
817 queue_work_on(new_cpu, wq, work);
820 /* This is called directly from KGD at ISR. */
821 void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry)
823 uint32_t patched_ihre[KFD_MAX_RING_ENTRY_SIZE];
824 bool is_patched = false;
827 if (!kfd->init_complete)
830 if (kfd->device_info.ih_ring_entry_size > sizeof(patched_ihre)) {
831 dev_err_once(kfd_device, "Ring entry too small\n");
835 spin_lock_irqsave(&kfd->interrupt_lock, flags);
837 if (kfd->interrupts_active
838 && interrupt_is_wanted(kfd, ih_ring_entry,
839 patched_ihre, &is_patched)
840 && enqueue_ih_ring_entry(kfd,
841 is_patched ? patched_ihre : ih_ring_entry))
842 kfd_queue_work(kfd->ih_wq, &kfd->interrupt_work);
844 spin_unlock_irqrestore(&kfd->interrupt_lock, flags);
847 int kgd2kfd_quiesce_mm(struct mm_struct *mm, uint32_t trigger)
849 struct kfd_process *p;
852 /* Because we are called from arbitrary context (workqueue) as opposed
853 * to process context, kfd_process could attempt to exit while we are
854 * running so the lookup function increments the process ref count.
856 p = kfd_lookup_process_by_mm(mm);
860 WARN(debug_evictions, "Evicting pid %d", p->lead_thread->pid);
861 r = kfd_process_evict_queues(p, trigger);
863 kfd_unref_process(p);
867 int kgd2kfd_resume_mm(struct mm_struct *mm)
869 struct kfd_process *p;
872 /* Because we are called from arbitrary context (workqueue) as opposed
873 * to process context, kfd_process could attempt to exit while we are
874 * running so the lookup function increments the process ref count.
876 p = kfd_lookup_process_by_mm(mm);
880 r = kfd_process_restore_queues(p);
882 kfd_unref_process(p);
886 /** kgd2kfd_schedule_evict_and_restore_process - Schedules work queue that will
887 * prepare for safe eviction of KFD BOs that belong to the specified
890 * @mm: mm_struct that identifies the specified KFD process
891 * @fence: eviction fence attached to KFD process BOs
894 int kgd2kfd_schedule_evict_and_restore_process(struct mm_struct *mm,
895 struct dma_fence *fence)
897 struct kfd_process *p;
898 unsigned long active_time;
899 unsigned long delay_jiffies = msecs_to_jiffies(PROCESS_ACTIVE_TIME_MS);
904 if (dma_fence_is_signaled(fence))
907 p = kfd_lookup_process_by_mm(mm);
911 if (fence->seqno == p->last_eviction_seqno)
914 p->last_eviction_seqno = fence->seqno;
916 /* Avoid KFD process starvation. Wait for at least
917 * PROCESS_ACTIVE_TIME_MS before evicting the process again
919 active_time = get_jiffies_64() - p->last_restore_timestamp;
920 if (delay_jiffies > active_time)
921 delay_jiffies -= active_time;
925 /* During process initialization eviction_work.dwork is initialized
926 * to kfd_evict_bo_worker
928 WARN(debug_evictions, "Scheduling eviction of pid %d in %ld jiffies",
929 p->lead_thread->pid, delay_jiffies);
930 schedule_delayed_work(&p->eviction_work, delay_jiffies);
932 kfd_unref_process(p);
936 static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size,
937 unsigned int chunk_size)
939 if (WARN_ON(buf_size < chunk_size))
941 if (WARN_ON(buf_size == 0))
943 if (WARN_ON(chunk_size == 0))
946 kfd->gtt_sa_chunk_size = chunk_size;
947 kfd->gtt_sa_num_of_chunks = buf_size / chunk_size;
949 kfd->gtt_sa_bitmap = bitmap_zalloc(kfd->gtt_sa_num_of_chunks,
951 if (!kfd->gtt_sa_bitmap)
954 pr_debug("gtt_sa_num_of_chunks = %d, gtt_sa_bitmap = %p\n",
955 kfd->gtt_sa_num_of_chunks, kfd->gtt_sa_bitmap);
957 mutex_init(&kfd->gtt_sa_lock);
962 static void kfd_gtt_sa_fini(struct kfd_dev *kfd)
964 mutex_destroy(&kfd->gtt_sa_lock);
965 bitmap_free(kfd->gtt_sa_bitmap);
968 static inline uint64_t kfd_gtt_sa_calc_gpu_addr(uint64_t start_addr,
969 unsigned int bit_num,
970 unsigned int chunk_size)
972 return start_addr + bit_num * chunk_size;
975 static inline uint32_t *kfd_gtt_sa_calc_cpu_addr(void *start_addr,
976 unsigned int bit_num,
977 unsigned int chunk_size)
979 return (uint32_t *) ((uint64_t) start_addr + bit_num * chunk_size);
982 int kfd_gtt_sa_allocate(struct kfd_dev *kfd, unsigned int size,
983 struct kfd_mem_obj **mem_obj)
985 unsigned int found, start_search, cur_size;
990 if (size > kfd->gtt_sa_num_of_chunks * kfd->gtt_sa_chunk_size)
993 *mem_obj = kzalloc(sizeof(struct kfd_mem_obj), GFP_KERNEL);
997 pr_debug("Allocated mem_obj = %p for size = %d\n", *mem_obj, size);
1001 mutex_lock(&kfd->gtt_sa_lock);
1003 kfd_gtt_restart_search:
1004 /* Find the first chunk that is free */
1005 found = find_next_zero_bit(kfd->gtt_sa_bitmap,
1006 kfd->gtt_sa_num_of_chunks,
1009 pr_debug("Found = %d\n", found);
1011 /* If there wasn't any free chunk, bail out */
1012 if (found == kfd->gtt_sa_num_of_chunks)
1013 goto kfd_gtt_no_free_chunk;
1015 /* Update fields of mem_obj */
1016 (*mem_obj)->range_start = found;
1017 (*mem_obj)->range_end = found;
1018 (*mem_obj)->gpu_addr = kfd_gtt_sa_calc_gpu_addr(
1019 kfd->gtt_start_gpu_addr,
1021 kfd->gtt_sa_chunk_size);
1022 (*mem_obj)->cpu_ptr = kfd_gtt_sa_calc_cpu_addr(
1023 kfd->gtt_start_cpu_ptr,
1025 kfd->gtt_sa_chunk_size);
1027 pr_debug("gpu_addr = %p, cpu_addr = %p\n",
1028 (uint64_t *) (*mem_obj)->gpu_addr, (*mem_obj)->cpu_ptr);
1030 /* If we need only one chunk, mark it as allocated and get out */
1031 if (size <= kfd->gtt_sa_chunk_size) {
1032 pr_debug("Single bit\n");
1033 __set_bit(found, kfd->gtt_sa_bitmap);
1037 /* Otherwise, try to see if we have enough contiguous chunks */
1038 cur_size = size - kfd->gtt_sa_chunk_size;
1040 (*mem_obj)->range_end =
1041 find_next_zero_bit(kfd->gtt_sa_bitmap,
1042 kfd->gtt_sa_num_of_chunks, ++found);
1044 * If next free chunk is not contiguous than we need to
1045 * restart our search from the last free chunk we found (which
1046 * wasn't contiguous to the previous ones
1048 if ((*mem_obj)->range_end != found) {
1049 start_search = found;
1050 goto kfd_gtt_restart_search;
1054 * If we reached end of buffer, bail out with error
1056 if (found == kfd->gtt_sa_num_of_chunks)
1057 goto kfd_gtt_no_free_chunk;
1059 /* Check if we don't need another chunk */
1060 if (cur_size <= kfd->gtt_sa_chunk_size)
1063 cur_size -= kfd->gtt_sa_chunk_size;
1065 } while (cur_size > 0);
1067 pr_debug("range_start = %d, range_end = %d\n",
1068 (*mem_obj)->range_start, (*mem_obj)->range_end);
1070 /* Mark the chunks as allocated */
1071 bitmap_set(kfd->gtt_sa_bitmap, (*mem_obj)->range_start,
1072 (*mem_obj)->range_end - (*mem_obj)->range_start + 1);
1075 mutex_unlock(&kfd->gtt_sa_lock);
1078 kfd_gtt_no_free_chunk:
1079 pr_debug("Allocation failed with mem_obj = %p\n", *mem_obj);
1080 mutex_unlock(&kfd->gtt_sa_lock);
1085 int kfd_gtt_sa_free(struct kfd_dev *kfd, struct kfd_mem_obj *mem_obj)
1087 /* Act like kfree when trying to free a NULL object */
1091 pr_debug("Free mem_obj = %p, range_start = %d, range_end = %d\n",
1092 mem_obj, mem_obj->range_start, mem_obj->range_end);
1094 mutex_lock(&kfd->gtt_sa_lock);
1096 /* Mark the chunks as free */
1097 bitmap_clear(kfd->gtt_sa_bitmap, mem_obj->range_start,
1098 mem_obj->range_end - mem_obj->range_start + 1);
1100 mutex_unlock(&kfd->gtt_sa_lock);
1106 void kgd2kfd_set_sram_ecc_flag(struct kfd_dev *kfd)
1109 atomic_inc(&kfd->sram_ecc_flag);
1112 void kfd_inc_compute_active(struct kfd_dev *kfd)
1114 if (atomic_inc_return(&kfd->compute_profile) == 1)
1115 amdgpu_amdkfd_set_compute_idle(kfd->adev, false);
1118 void kfd_dec_compute_active(struct kfd_dev *kfd)
1120 int count = atomic_dec_return(&kfd->compute_profile);
1123 amdgpu_amdkfd_set_compute_idle(kfd->adev, true);
1124 WARN_ONCE(count < 0, "Compute profile ref. count error");
1127 void kgd2kfd_smi_event_throttle(struct kfd_dev *kfd, uint64_t throttle_bitmask)
1129 if (kfd && kfd->init_complete)
1130 kfd_smi_event_update_thermal_throttling(kfd, throttle_bitmask);
1133 /* kfd_get_num_sdma_engines returns the number of PCIe optimized SDMA and
1134 * kfd_get_num_xgmi_sdma_engines returns the number of XGMI SDMA.
1135 * When the device has more than two engines, we reserve two for PCIe to enable
1136 * full-duplex and the rest are used as XGMI.
1138 unsigned int kfd_get_num_sdma_engines(struct kfd_dev *kdev)
1140 /* If XGMI is not supported, all SDMA engines are PCIe */
1141 if (!kdev->adev->gmc.xgmi.supported)
1142 return kdev->adev->sdma.num_instances;
1144 return min(kdev->adev->sdma.num_instances, 2);
1147 unsigned int kfd_get_num_xgmi_sdma_engines(struct kfd_dev *kdev)
1149 /* After reserved for PCIe, the rest of engines are XGMI */
1150 return kdev->adev->sdma.num_instances - kfd_get_num_sdma_engines(kdev);
1153 #if defined(CONFIG_DEBUG_FS)
1155 /* This function will send a package to HIQ to hang the HWS
1156 * which will trigger a GPU reset and bring the HWS back to normal state
1158 int kfd_debugfs_hang_hws(struct kfd_dev *dev)
1160 if (dev->dqm->sched_policy != KFD_SCHED_POLICY_HWS) {
1161 pr_err("HWS is not enabled");
1165 return dqm_debugfs_hang_hws(dev->dqm);