1 // SPDX-License-Identifier: GPL-2.0 OR MIT 2 /* 3 * Copyright 2014-2022 Advanced Micro Devices, Inc. 4 * 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: 11 * 12 * The above copyright notice and this permission notice shall be included in 13 * all copies or substantial portions of the Software. 14 * 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. 22 */ 23 24 #include <linux/bsearch.h> 25 #include <linux/pci.h> 26 #include <linux/slab.h> 27 #include "kfd_priv.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" 36 #include "amdgpu.h" 37 38 #define MQD_SIZE_ALIGNED 768 39 40 /* 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. 44 */ 45 static atomic_t kfd_locked = ATOMIC_INIT(0); 46 47 #ifdef CONFIG_DRM_AMDGPU_CIK 48 extern const struct kfd2kgd_calls gfx_v7_kfd2kgd; 49 #endif 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 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); 60 61 static int kfd_resume(struct kfd_dev *kfd); 62 63 static void kfd_device_info_set_sdma_queue_num(struct kfd_dev *kfd) 64 { 65 uint32_t sdma_version = kfd->adev->ip_versions[SDMA0_HWIP][0]; 66 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; 76 break; 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; 89 break; 90 default: 91 dev_warn(kfd_device, 92 "Default sdma queue per engine(8) is set due to mismatch of sdma ip block(SDMA_HWIP:0x%x).\n", 93 sdma_version); 94 kfd->device_info.num_sdma_queues_per_engine = 8; 95 } 96 } 97 98 static void kfd_device_info_set_event_interrupt_class(struct kfd_dev *kfd) 99 { 100 uint32_t gc_version = KFD_GC_VERSION(kfd); 101 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; 123 break; 124 default: 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; 128 } 129 } 130 131 static void kfd_device_info_init(struct kfd_dev *kfd, 132 bool vf, uint32_t gfx_target_version) 133 { 134 uint32_t gc_version = KFD_GC_VERSION(kfd); 135 uint32_t asic_type = kfd->adev->asic_type; 136 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; 142 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; 147 148 kfd_device_info_set_sdma_queue_num(kfd); 149 150 kfd_device_info_set_event_interrupt_class(kfd); 151 152 /* Raven */ 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; 156 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; 163 164 /* Navi1x+ */ 165 if (gc_version >= IP_VERSION(10, 1, 1)) 166 kfd->device_info.needs_pci_atomics = true; 167 } 168 } else { 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; 173 174 if (asic_type != CHIP_KAVERI && 175 asic_type != CHIP_HAWAII && 176 asic_type != CHIP_TONGA) 177 kfd->device_info.supports_cwsr = true; 178 179 if (asic_type == CHIP_KAVERI || 180 asic_type == CHIP_CARRIZO) 181 kfd->device_info.needs_iommu_device = true; 182 183 if (asic_type != CHIP_HAWAII && !vf) 184 kfd->device_info.needs_pci_atomics = true; 185 } 186 } 187 188 struct kfd_dev *kgd2kfd_probe(struct amdgpu_device *adev, bool vf) 189 { 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; 194 195 switch (adev->asic_type) { 196 #ifdef KFD_SUPPORT_IOMMU_V2 197 #ifdef CONFIG_DRM_AMDGPU_CIK 198 case CHIP_KAVERI: 199 gfx_target_version = 70000; 200 if (!vf) 201 f2g = &gfx_v7_kfd2kgd; 202 break; 203 #endif 204 case CHIP_CARRIZO: 205 gfx_target_version = 80001; 206 if (!vf) 207 f2g = &gfx_v8_kfd2kgd; 208 break; 209 #endif 210 #ifdef CONFIG_DRM_AMDGPU_CIK 211 case CHIP_HAWAII: 212 gfx_target_version = 70001; 213 if (!amdgpu_exp_hw_support) 214 pr_info( 215 "KFD support on Hawaii is experimental. See modparam exp_hw_support\n" 216 ); 217 else if (!vf) 218 f2g = &gfx_v7_kfd2kgd; 219 break; 220 #endif 221 case CHIP_TONGA: 222 gfx_target_version = 80002; 223 if (!vf) 224 f2g = &gfx_v8_kfd2kgd; 225 break; 226 case CHIP_FIJI: 227 gfx_target_version = 80003; 228 f2g = &gfx_v8_kfd2kgd; 229 break; 230 case CHIP_POLARIS10: 231 gfx_target_version = 80003; 232 f2g = &gfx_v8_kfd2kgd; 233 break; 234 case CHIP_POLARIS11: 235 gfx_target_version = 80003; 236 if (!vf) 237 f2g = &gfx_v8_kfd2kgd; 238 break; 239 case CHIP_POLARIS12: 240 gfx_target_version = 80003; 241 if (!vf) 242 f2g = &gfx_v8_kfd2kgd; 243 break; 244 case CHIP_VEGAM: 245 gfx_target_version = 80003; 246 if (!vf) 247 f2g = &gfx_v8_kfd2kgd; 248 break; 249 default: 250 switch (adev->ip_versions[GC_HWIP][0]) { 251 /* Vega 10 */ 252 case IP_VERSION(9, 0, 1): 253 gfx_target_version = 90000; 254 f2g = &gfx_v9_kfd2kgd; 255 break; 256 #ifdef KFD_SUPPORT_IOMMU_V2 257 /* Raven */ 258 case IP_VERSION(9, 1, 0): 259 case IP_VERSION(9, 2, 2): 260 gfx_target_version = 90002; 261 if (!vf) 262 f2g = &gfx_v9_kfd2kgd; 263 break; 264 #endif 265 /* Vega12 */ 266 case IP_VERSION(9, 2, 1): 267 gfx_target_version = 90004; 268 if (!vf) 269 f2g = &gfx_v9_kfd2kgd; 270 break; 271 /* Renoir */ 272 case IP_VERSION(9, 3, 0): 273 gfx_target_version = 90012; 274 if (!vf) 275 f2g = &gfx_v9_kfd2kgd; 276 break; 277 /* Vega20 */ 278 case IP_VERSION(9, 4, 0): 279 gfx_target_version = 90006; 280 if (!vf) 281 f2g = &gfx_v9_kfd2kgd; 282 break; 283 /* Arcturus */ 284 case IP_VERSION(9, 4, 1): 285 gfx_target_version = 90008; 286 f2g = &arcturus_kfd2kgd; 287 break; 288 /* Aldebaran */ 289 case IP_VERSION(9, 4, 2): 290 gfx_target_version = 90010; 291 f2g = &aldebaran_kfd2kgd; 292 break; 293 /* Navi10 */ 294 case IP_VERSION(10, 1, 10): 295 gfx_target_version = 100100; 296 if (!vf) 297 f2g = &gfx_v10_kfd2kgd; 298 break; 299 /* Navi12 */ 300 case IP_VERSION(10, 1, 2): 301 gfx_target_version = 100101; 302 f2g = &gfx_v10_kfd2kgd; 303 break; 304 /* Navi14 */ 305 case IP_VERSION(10, 1, 1): 306 gfx_target_version = 100102; 307 if (!vf) 308 f2g = &gfx_v10_kfd2kgd; 309 break; 310 /* Cyan Skillfish */ 311 case IP_VERSION(10, 1, 3): 312 case IP_VERSION(10, 1, 4): 313 gfx_target_version = 100103; 314 if (!vf) 315 f2g = &gfx_v10_kfd2kgd; 316 break; 317 /* Sienna Cichlid */ 318 case IP_VERSION(10, 3, 0): 319 gfx_target_version = 100300; 320 f2g = &gfx_v10_3_kfd2kgd; 321 break; 322 /* Navy Flounder */ 323 case IP_VERSION(10, 3, 2): 324 gfx_target_version = 100301; 325 f2g = &gfx_v10_3_kfd2kgd; 326 break; 327 /* Van Gogh */ 328 case IP_VERSION(10, 3, 1): 329 gfx_target_version = 100303; 330 if (!vf) 331 f2g = &gfx_v10_3_kfd2kgd; 332 break; 333 /* Dimgrey Cavefish */ 334 case IP_VERSION(10, 3, 4): 335 gfx_target_version = 100302; 336 f2g = &gfx_v10_3_kfd2kgd; 337 break; 338 /* Beige Goby */ 339 case IP_VERSION(10, 3, 5): 340 gfx_target_version = 100304; 341 f2g = &gfx_v10_3_kfd2kgd; 342 break; 343 /* Yellow Carp */ 344 case IP_VERSION(10, 3, 3): 345 gfx_target_version = 100305; 346 if (!vf) 347 f2g = &gfx_v10_3_kfd2kgd; 348 break; 349 default: 350 break; 351 } 352 break; 353 } 354 355 if (!f2g) { 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" : ""); 359 else 360 dev_err(kfd_device, "%s %s not supported in kfd\n", 361 amdgpu_asic_name[adev->asic_type], vf ? "VF" : ""); 362 return NULL; 363 } 364 365 kfd = kzalloc(sizeof(*kfd), GFP_KERNEL); 366 if (!kfd) 367 return NULL; 368 369 kfd->adev = adev; 370 kfd_device_info_init(kfd, vf, gfx_target_version); 371 kfd->pdev = pdev; 372 kfd->init_complete = false; 373 kfd->kfd2kgd = f2g; 374 atomic_set(&kfd->compute_profile, 0); 375 376 mutex_init(&kfd->doorbell_mutex); 377 memset(&kfd->doorbell_available_index, 0, 378 sizeof(kfd->doorbell_available_index)); 379 380 atomic_set(&kfd->sram_ecc_flag, 0); 381 382 ida_init(&kfd->doorbell_ida); 383 384 return kfd; 385 } 386 387 static void kfd_cwsr_init(struct kfd_dev *kfd) 388 { 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); 410 } else { 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); 414 } 415 416 kfd->cwsr_enabled = true; 417 } 418 } 419 420 static int kfd_gws_init(struct kfd_dev *kfd) 421 { 422 int ret = 0; 423 424 if (kfd->dqm->sched_policy == KFD_SCHED_POLICY_NO_HWS) 425 return 0; 426 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); 438 439 return ret; 440 } 441 442 static void kfd_smi_init(struct kfd_dev *dev) 443 { 444 INIT_LIST_HEAD(&dev->smi_clients); 445 spin_lock_init(&dev->smi_lock); 446 } 447 448 bool kgd2kfd_device_init(struct kfd_dev *kfd, 449 struct drm_device *ddev, 450 const struct kgd2kfd_shared_resources *gpu_resources) 451 { 452 unsigned int size, map_process_packet_size; 453 454 kfd->ddev = ddev; 455 kfd->mec_fw_version = amdgpu_amdkfd_get_fw_version(kfd->adev, 456 KGD_ENGINE_MEC1); 457 kfd->mec2_fw_version = amdgpu_amdkfd_get_fw_version(kfd->adev, 458 KGD_ENGINE_MEC2); 459 kfd->sdma_fw_version = amdgpu_amdkfd_get_fw_version(kfd->adev, 460 KGD_ENGINE_SDMA1); 461 kfd->shared_resources = *gpu_resources; 462 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; 467 468 /* Allow BIF to recode atomics to PCIe 3.0 AtomicOps. 469 * 32 and 64-bit requests are possible and must be 470 * supported. 471 */ 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)) { 477 dev_info(kfd_device, 478 "skipped device %x:%x, PCI rejects atomics %d<%d\n", 479 kfd->pdev->vendor, kfd->pdev->device, 480 kfd->mec_fw_version, 481 kfd->device_info.no_atomic_fw_version); 482 return false; 483 } 484 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)) { 488 dev_err(kfd_device, 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; 493 } else 494 kfd->max_proc_per_quantum = hws_max_conc_proc; 495 496 /* calculate max size of mqds needed for queues */ 497 size = max_num_of_queues_per_device * 498 kfd->device_info.mqd_size_aligned; 499 500 /* 501 * calculate max size of runlist packet. 502 * There can be only 2 packets at once 503 */ 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; 510 511 /* Add size of HIQ & DIQ */ 512 size += KFD_KERNEL_QUEUE_SIZE * 2; 513 514 /* add another 512KB for all other allocations on gart (HPD, fences) */ 515 size += 512 * 1024; 516 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, 520 false)) { 521 dev_err(kfd_device, "Could not allocate %d bytes\n", size); 522 goto alloc_gtt_mem_failure; 523 } 524 525 dev_info(kfd_device, "Allocated %d bytes on gart\n", size); 526 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; 531 } 532 533 if (kfd_doorbell_init(kfd)) { 534 dev_err(kfd_device, 535 "Error initializing doorbell aperture\n"); 536 goto kfd_doorbell_error; 537 } 538 539 kfd->hive_id = kfd->adev->gmc.xgmi.hive_id; 540 541 kfd->noretry = kfd->adev->gmc.noretry; 542 543 if (kfd_interrupt_init(kfd)) { 544 dev_err(kfd_device, "Error initializing interrupts\n"); 545 goto kfd_interrupt_error; 546 } 547 548 kfd->dqm = device_queue_manager_init(kfd); 549 if (!kfd->dqm) { 550 dev_err(kfd_device, "Error initializing queue manager\n"); 551 goto device_queue_manager_error; 552 } 553 554 /* If supported on this device, allocate global GWS that is shared 555 * by all KFD processes 556 */ 557 if (kfd_gws_init(kfd)) { 558 dev_err(kfd_device, "Could not allocate %d gws\n", 559 kfd->adev->gds.gws_size); 560 goto gws_error; 561 } 562 563 /* If CRAT is broken, won't set iommu enabled */ 564 kfd_double_confirm_iommu_support(kfd); 565 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; 570 } 571 572 kfd_cwsr_init(kfd); 573 574 svm_migrate_init(kfd->adev); 575 576 if (kgd2kfd_resume_iommu(kfd)) 577 goto device_iommu_error; 578 579 if (kfd_resume(kfd)) 580 goto kfd_resume_error; 581 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; 585 } 586 587 kfd_smi_init(kfd); 588 589 kfd->init_complete = true; 590 dev_info(kfd_device, "added device %x:%x\n", kfd->pdev->vendor, 591 kfd->pdev->device); 592 593 pr_debug("Starting kfd with the following scheduling policy %d\n", 594 kfd->dqm->sched_policy); 595 596 goto out; 597 598 kfd_topology_add_device_error: 599 kfd_resume_error: 600 device_iommu_error: 601 gws_error: 602 device_queue_manager_uninit(kfd->dqm); 603 device_queue_manager_error: 604 kfd_interrupt_exit(kfd); 605 kfd_interrupt_error: 606 kfd_doorbell_fini(kfd); 607 kfd_doorbell_error: 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: 612 if (kfd->gws) 613 amdgpu_amdkfd_free_gws(kfd->adev, kfd->gws); 614 dev_err(kfd_device, 615 "device %x:%x NOT added due to errors\n", 616 kfd->pdev->vendor, kfd->pdev->device); 617 out: 618 return kfd->init_complete; 619 } 620 621 void kgd2kfd_device_exit(struct kfd_dev *kfd) 622 { 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); 631 if (kfd->gws) 632 amdgpu_amdkfd_free_gws(kfd->adev, kfd->gws); 633 } 634 635 kfree(kfd); 636 } 637 638 int kgd2kfd_pre_reset(struct kfd_dev *kfd) 639 { 640 if (!kfd->init_complete) 641 return 0; 642 643 kfd_smi_event_update_gpu_reset(kfd, false); 644 645 kfd->dqm->ops.pre_reset(kfd->dqm); 646 647 kgd2kfd_suspend(kfd, false); 648 649 kfd_signal_reset_event(kfd); 650 return 0; 651 } 652 653 /* 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. 657 */ 658 659 int kgd2kfd_post_reset(struct kfd_dev *kfd) 660 { 661 int ret; 662 663 if (!kfd->init_complete) 664 return 0; 665 666 ret = kfd_resume(kfd); 667 if (ret) 668 return ret; 669 atomic_dec(&kfd_locked); 670 671 atomic_set(&kfd->sram_ecc_flag, 0); 672 673 kfd_smi_event_update_gpu_reset(kfd, true); 674 675 return 0; 676 } 677 678 bool kfd_is_locked(void) 679 { 680 return (atomic_read(&kfd_locked) > 0); 681 } 682 683 void kgd2kfd_suspend(struct kfd_dev *kfd, bool run_pm) 684 { 685 if (!kfd->init_complete) 686 return; 687 688 /* for runtime suspend, skip locking kfd */ 689 if (!run_pm) { 690 /* For first KFD device suspend all the KFD processes */ 691 if (atomic_inc_return(&kfd_locked) == 1) 692 kfd_suspend_all_processes(); 693 } 694 695 kfd->dqm->ops.stop(kfd->dqm); 696 kfd_iommu_suspend(kfd); 697 } 698 699 int kgd2kfd_resume(struct kfd_dev *kfd, bool run_pm) 700 { 701 int ret, count; 702 703 if (!kfd->init_complete) 704 return 0; 705 706 ret = kfd_resume(kfd); 707 if (ret) 708 return ret; 709 710 /* for runtime resume, skip unlocking kfd */ 711 if (!run_pm) { 712 count = atomic_dec_return(&kfd_locked); 713 WARN_ONCE(count < 0, "KFD suspend / resume ref. error"); 714 if (count == 0) 715 ret = kfd_resume_all_processes(); 716 } 717 718 return ret; 719 } 720 721 int kgd2kfd_resume_iommu(struct kfd_dev *kfd) 722 { 723 int err = 0; 724 725 err = kfd_iommu_resume(kfd); 726 if (err) 727 dev_err(kfd_device, 728 "Failed to resume IOMMU for device %x:%x\n", 729 kfd->pdev->vendor, kfd->pdev->device); 730 return err; 731 } 732 733 static int kfd_resume(struct kfd_dev *kfd) 734 { 735 int err = 0; 736 737 err = kfd->dqm->ops.start(kfd->dqm); 738 if (err) 739 dev_err(kfd_device, 740 "Error starting queue manager for device %x:%x\n", 741 kfd->pdev->vendor, kfd->pdev->device); 742 743 return err; 744 } 745 746 static inline void kfd_queue_work(struct workqueue_struct *wq, 747 struct work_struct *work) 748 { 749 int cpu, new_cpu; 750 751 cpu = new_cpu = smp_processor_id(); 752 do { 753 new_cpu = cpumask_next(new_cpu, cpu_online_mask) % nr_cpu_ids; 754 if (cpu_to_node(new_cpu) == numa_node_id()) 755 break; 756 } while (cpu != new_cpu); 757 758 queue_work_on(new_cpu, wq, work); 759 } 760 761 /* This is called directly from KGD at ISR. */ 762 void kgd2kfd_interrupt(struct kfd_dev *kfd, const void *ih_ring_entry) 763 { 764 uint32_t patched_ihre[KFD_MAX_RING_ENTRY_SIZE]; 765 bool is_patched = false; 766 unsigned long flags; 767 768 if (!kfd->init_complete) 769 return; 770 771 if (kfd->device_info.ih_ring_entry_size > sizeof(patched_ihre)) { 772 dev_err_once(kfd_device, "Ring entry too small\n"); 773 return; 774 } 775 776 spin_lock_irqsave(&kfd->interrupt_lock, flags); 777 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); 784 785 spin_unlock_irqrestore(&kfd->interrupt_lock, flags); 786 } 787 788 int kgd2kfd_quiesce_mm(struct mm_struct *mm) 789 { 790 struct kfd_process *p; 791 int r; 792 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. 796 */ 797 p = kfd_lookup_process_by_mm(mm); 798 if (!p) 799 return -ESRCH; 800 801 WARN(debug_evictions, "Evicting pid %d", p->lead_thread->pid); 802 r = kfd_process_evict_queues(p); 803 804 kfd_unref_process(p); 805 return r; 806 } 807 808 int kgd2kfd_resume_mm(struct mm_struct *mm) 809 { 810 struct kfd_process *p; 811 int r; 812 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. 816 */ 817 p = kfd_lookup_process_by_mm(mm); 818 if (!p) 819 return -ESRCH; 820 821 r = kfd_process_restore_queues(p); 822 823 kfd_unref_process(p); 824 return r; 825 } 826 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 829 * process. 830 * 831 * @mm: mm_struct that identifies the specified KFD process 832 * @fence: eviction fence attached to KFD process BOs 833 * 834 */ 835 int kgd2kfd_schedule_evict_and_restore_process(struct mm_struct *mm, 836 struct dma_fence *fence) 837 { 838 struct kfd_process *p; 839 unsigned long active_time; 840 unsigned long delay_jiffies = msecs_to_jiffies(PROCESS_ACTIVE_TIME_MS); 841 842 if (!fence) 843 return -EINVAL; 844 845 if (dma_fence_is_signaled(fence)) 846 return 0; 847 848 p = kfd_lookup_process_by_mm(mm); 849 if (!p) 850 return -ENODEV; 851 852 if (fence->seqno == p->last_eviction_seqno) 853 goto out; 854 855 p->last_eviction_seqno = fence->seqno; 856 857 /* Avoid KFD process starvation. Wait for at least 858 * PROCESS_ACTIVE_TIME_MS before evicting the process again 859 */ 860 active_time = get_jiffies_64() - p->last_restore_timestamp; 861 if (delay_jiffies > active_time) 862 delay_jiffies -= active_time; 863 else 864 delay_jiffies = 0; 865 866 /* During process initialization eviction_work.dwork is initialized 867 * to kfd_evict_bo_worker 868 */ 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); 872 out: 873 kfd_unref_process(p); 874 return 0; 875 } 876 877 static int kfd_gtt_sa_init(struct kfd_dev *kfd, unsigned int buf_size, 878 unsigned int chunk_size) 879 { 880 unsigned int num_of_longs; 881 882 if (WARN_ON(buf_size < chunk_size)) 883 return -EINVAL; 884 if (WARN_ON(buf_size == 0)) 885 return -EINVAL; 886 if (WARN_ON(chunk_size == 0)) 887 return -EINVAL; 888 889 kfd->gtt_sa_chunk_size = chunk_size; 890 kfd->gtt_sa_num_of_chunks = buf_size / chunk_size; 891 892 num_of_longs = (kfd->gtt_sa_num_of_chunks + BITS_PER_LONG - 1) / 893 BITS_PER_LONG; 894 895 kfd->gtt_sa_bitmap = kcalloc(num_of_longs, sizeof(long), GFP_KERNEL); 896 897 if (!kfd->gtt_sa_bitmap) 898 return -ENOMEM; 899 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); 902 903 mutex_init(&kfd->gtt_sa_lock); 904 905 return 0; 906 907 } 908 909 static void kfd_gtt_sa_fini(struct kfd_dev *kfd) 910 { 911 mutex_destroy(&kfd->gtt_sa_lock); 912 kfree(kfd->gtt_sa_bitmap); 913 } 914 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) 918 { 919 return start_addr + bit_num * chunk_size; 920 } 921 922 static inline uint32_t *kfd_gtt_sa_calc_cpu_addr(void *start_addr, 923 unsigned int bit_num, 924 unsigned int chunk_size) 925 { 926 return (uint32_t *) ((uint64_t) start_addr + bit_num * chunk_size); 927 } 928 929 int kfd_gtt_sa_allocate(struct kfd_dev *kfd, unsigned int size, 930 struct kfd_mem_obj **mem_obj) 931 { 932 unsigned int found, start_search, cur_size; 933 934 if (size == 0) 935 return -EINVAL; 936 937 if (size > kfd->gtt_sa_num_of_chunks * kfd->gtt_sa_chunk_size) 938 return -ENOMEM; 939 940 *mem_obj = kzalloc(sizeof(struct kfd_mem_obj), GFP_KERNEL); 941 if (!(*mem_obj)) 942 return -ENOMEM; 943 944 pr_debug("Allocated mem_obj = %p for size = %d\n", *mem_obj, size); 945 946 start_search = 0; 947 948 mutex_lock(&kfd->gtt_sa_lock); 949 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, 954 start_search); 955 956 pr_debug("Found = %d\n", found); 957 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; 961 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, 967 found, 968 kfd->gtt_sa_chunk_size); 969 (*mem_obj)->cpu_ptr = kfd_gtt_sa_calc_cpu_addr( 970 kfd->gtt_start_cpu_ptr, 971 found, 972 kfd->gtt_sa_chunk_size); 973 974 pr_debug("gpu_addr = %p, cpu_addr = %p\n", 975 (uint64_t *) (*mem_obj)->gpu_addr, (*mem_obj)->cpu_ptr); 976 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); 981 goto kfd_gtt_out; 982 } 983 984 /* Otherwise, try to see if we have enough contiguous chunks */ 985 cur_size = size - kfd->gtt_sa_chunk_size; 986 do { 987 (*mem_obj)->range_end = 988 find_next_zero_bit(kfd->gtt_sa_bitmap, 989 kfd->gtt_sa_num_of_chunks, ++found); 990 /* 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 994 */ 995 if ((*mem_obj)->range_end != found) { 996 start_search = found; 997 goto kfd_gtt_restart_search; 998 } 999 1000 /* 1001 * If we reached end of buffer, bail out with error 1002 */ 1003 if (found == kfd->gtt_sa_num_of_chunks) 1004 goto kfd_gtt_no_free_chunk; 1005 1006 /* Check if we don't need another chunk */ 1007 if (cur_size <= kfd->gtt_sa_chunk_size) 1008 cur_size = 0; 1009 else 1010 cur_size -= kfd->gtt_sa_chunk_size; 1011 1012 } while (cur_size > 0); 1013 1014 pr_debug("range_start = %d, range_end = %d\n", 1015 (*mem_obj)->range_start, (*mem_obj)->range_end); 1016 1017 /* Mark the chunks as allocated */ 1018 for (found = (*mem_obj)->range_start; 1019 found <= (*mem_obj)->range_end; 1020 found++) 1021 set_bit(found, kfd->gtt_sa_bitmap); 1022 1023 kfd_gtt_out: 1024 mutex_unlock(&kfd->gtt_sa_lock); 1025 return 0; 1026 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); 1030 kfree(*mem_obj); 1031 return -ENOMEM; 1032 } 1033 1034 int kfd_gtt_sa_free(struct kfd_dev *kfd, struct kfd_mem_obj *mem_obj) 1035 { 1036 unsigned int bit; 1037 1038 /* Act like kfree when trying to free a NULL object */ 1039 if (!mem_obj) 1040 return 0; 1041 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); 1044 1045 mutex_lock(&kfd->gtt_sa_lock); 1046 1047 /* Mark the chunks as free */ 1048 for (bit = mem_obj->range_start; 1049 bit <= mem_obj->range_end; 1050 bit++) 1051 clear_bit(bit, kfd->gtt_sa_bitmap); 1052 1053 mutex_unlock(&kfd->gtt_sa_lock); 1054 1055 kfree(mem_obj); 1056 return 0; 1057 } 1058 1059 void kgd2kfd_set_sram_ecc_flag(struct kfd_dev *kfd) 1060 { 1061 if (kfd) 1062 atomic_inc(&kfd->sram_ecc_flag); 1063 } 1064 1065 void kfd_inc_compute_active(struct kfd_dev *kfd) 1066 { 1067 if (atomic_inc_return(&kfd->compute_profile) == 1) 1068 amdgpu_amdkfd_set_compute_idle(kfd->adev, false); 1069 } 1070 1071 void kfd_dec_compute_active(struct kfd_dev *kfd) 1072 { 1073 int count = atomic_dec_return(&kfd->compute_profile); 1074 1075 if (count == 0) 1076 amdgpu_amdkfd_set_compute_idle(kfd->adev, true); 1077 WARN_ONCE(count < 0, "Compute profile ref. count error"); 1078 } 1079 1080 void kgd2kfd_smi_event_throttle(struct kfd_dev *kfd, uint64_t throttle_bitmask) 1081 { 1082 if (kfd && kfd->init_complete) 1083 kfd_smi_event_update_thermal_throttling(kfd, throttle_bitmask); 1084 } 1085 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. 1090 */ 1091 unsigned int kfd_get_num_sdma_engines(struct kfd_dev *kdev) 1092 { 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; 1096 1097 return min(kdev->adev->sdma.num_instances, 2); 1098 } 1099 1100 unsigned int kfd_get_num_xgmi_sdma_engines(struct kfd_dev *kdev) 1101 { 1102 /* After reserved for PCIe, the rest of engines are XGMI */ 1103 return kdev->adev->sdma.num_instances - kfd_get_num_sdma_engines(kdev); 1104 } 1105 1106 #if defined(CONFIG_DEBUG_FS) 1107 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 1110 */ 1111 int kfd_debugfs_hang_hws(struct kfd_dev *dev) 1112 { 1113 if (dev->dqm->sched_policy != KFD_SCHED_POLICY_HWS) { 1114 pr_err("HWS is not enabled"); 1115 return -EINVAL; 1116 } 1117 1118 return dqm_debugfs_hang_hws(dev->dqm); 1119 } 1120 1121 #endif 1122