1 /* 2 * Copyright 2008 Advanced Micro Devices, Inc. 3 * Copyright 2008 Red Hat Inc. 4 * Copyright 2009 Jerome Glisse. 5 * 6 * Permission is hereby granted, free of charge, to any person obtaining a 7 * copy of this software and associated documentation files (the "Software"), 8 * to deal in the Software without restriction, including without limitation 9 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 * and/or sell copies of the Software, and to permit persons to whom the 11 * Software is furnished to do so, subject to the following conditions: 12 * 13 * The above copyright notice and this permission notice shall be included in 14 * all copies or substantial portions of the Software. 15 * 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 * OTHER DEALINGS IN THE SOFTWARE. 23 * 24 * Authors: Dave Airlie 25 * Alex Deucher 26 * Jerome Glisse 27 */ 28 #include <linux/power_supply.h> 29 #include <linux/kthread.h> 30 #include <linux/module.h> 31 #include <linux/console.h> 32 #include <linux/slab.h> 33 34 #include <drm/drm_atomic_helper.h> 35 #include <drm/drm_probe_helper.h> 36 #include <drm/amdgpu_drm.h> 37 #include <linux/vgaarb.h> 38 #include <linux/vga_switcheroo.h> 39 #include <linux/efi.h> 40 #include "amdgpu.h" 41 #include "amdgpu_trace.h" 42 #include "amdgpu_i2c.h" 43 #include "atom.h" 44 #include "amdgpu_atombios.h" 45 #include "amdgpu_atomfirmware.h" 46 #include "amd_pcie.h" 47 #ifdef CONFIG_DRM_AMDGPU_SI 48 #include "si.h" 49 #endif 50 #ifdef CONFIG_DRM_AMDGPU_CIK 51 #include "cik.h" 52 #endif 53 #include "vi.h" 54 #include "soc15.h" 55 #include "nv.h" 56 #include "bif/bif_4_1_d.h" 57 #include <linux/pci.h> 58 #include <linux/firmware.h> 59 #include "amdgpu_vf_error.h" 60 61 #include "amdgpu_amdkfd.h" 62 #include "amdgpu_pm.h" 63 64 #include "amdgpu_xgmi.h" 65 #include "amdgpu_ras.h" 66 #include "amdgpu_pmu.h" 67 #include "amdgpu_fru_eeprom.h" 68 #include "amdgpu_reset.h" 69 70 #include <linux/suspend.h> 71 #include <drm/task_barrier.h> 72 #include <linux/pm_runtime.h> 73 74 #include <drm/drm_drv.h> 75 76 MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin"); 77 MODULE_FIRMWARE("amdgpu/vega12_gpu_info.bin"); 78 MODULE_FIRMWARE("amdgpu/raven_gpu_info.bin"); 79 MODULE_FIRMWARE("amdgpu/picasso_gpu_info.bin"); 80 MODULE_FIRMWARE("amdgpu/raven2_gpu_info.bin"); 81 MODULE_FIRMWARE("amdgpu/arcturus_gpu_info.bin"); 82 MODULE_FIRMWARE("amdgpu/renoir_gpu_info.bin"); 83 MODULE_FIRMWARE("amdgpu/navi10_gpu_info.bin"); 84 MODULE_FIRMWARE("amdgpu/navi14_gpu_info.bin"); 85 MODULE_FIRMWARE("amdgpu/navi12_gpu_info.bin"); 86 MODULE_FIRMWARE("amdgpu/vangogh_gpu_info.bin"); 87 MODULE_FIRMWARE("amdgpu/yellow_carp_gpu_info.bin"); 88 89 #define AMDGPU_RESUME_MS 2000 90 91 const char *amdgpu_asic_name[] = { 92 "TAHITI", 93 "PITCAIRN", 94 "VERDE", 95 "OLAND", 96 "HAINAN", 97 "BONAIRE", 98 "KAVERI", 99 "KABINI", 100 "HAWAII", 101 "MULLINS", 102 "TOPAZ", 103 "TONGA", 104 "FIJI", 105 "CARRIZO", 106 "STONEY", 107 "POLARIS10", 108 "POLARIS11", 109 "POLARIS12", 110 "VEGAM", 111 "VEGA10", 112 "VEGA12", 113 "VEGA20", 114 "RAVEN", 115 "ARCTURUS", 116 "RENOIR", 117 "ALDEBARAN", 118 "NAVI10", 119 "NAVI14", 120 "NAVI12", 121 "SIENNA_CICHLID", 122 "NAVY_FLOUNDER", 123 "VANGOGH", 124 "DIMGREY_CAVEFISH", 125 "BEIGE_GOBY", 126 "YELLOW_CARP", 127 "LAST", 128 }; 129 130 /** 131 * DOC: pcie_replay_count 132 * 133 * The amdgpu driver provides a sysfs API for reporting the total number 134 * of PCIe replays (NAKs) 135 * The file pcie_replay_count is used for this and returns the total 136 * number of replays as a sum of the NAKs generated and NAKs received 137 */ 138 139 static ssize_t amdgpu_device_get_pcie_replay_count(struct device *dev, 140 struct device_attribute *attr, char *buf) 141 { 142 struct drm_device *ddev = dev_get_drvdata(dev); 143 struct amdgpu_device *adev = drm_to_adev(ddev); 144 uint64_t cnt = amdgpu_asic_get_pcie_replay_count(adev); 145 146 return sysfs_emit(buf, "%llu\n", cnt); 147 } 148 149 static DEVICE_ATTR(pcie_replay_count, S_IRUGO, 150 amdgpu_device_get_pcie_replay_count, NULL); 151 152 static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev); 153 154 /** 155 * DOC: product_name 156 * 157 * The amdgpu driver provides a sysfs API for reporting the product name 158 * for the device 159 * The file serial_number is used for this and returns the product name 160 * as returned from the FRU. 161 * NOTE: This is only available for certain server cards 162 */ 163 164 static ssize_t amdgpu_device_get_product_name(struct device *dev, 165 struct device_attribute *attr, char *buf) 166 { 167 struct drm_device *ddev = dev_get_drvdata(dev); 168 struct amdgpu_device *adev = drm_to_adev(ddev); 169 170 return sysfs_emit(buf, "%s\n", adev->product_name); 171 } 172 173 static DEVICE_ATTR(product_name, S_IRUGO, 174 amdgpu_device_get_product_name, NULL); 175 176 /** 177 * DOC: product_number 178 * 179 * The amdgpu driver provides a sysfs API for reporting the part number 180 * for the device 181 * The file serial_number is used for this and returns the part number 182 * as returned from the FRU. 183 * NOTE: This is only available for certain server cards 184 */ 185 186 static ssize_t amdgpu_device_get_product_number(struct device *dev, 187 struct device_attribute *attr, char *buf) 188 { 189 struct drm_device *ddev = dev_get_drvdata(dev); 190 struct amdgpu_device *adev = drm_to_adev(ddev); 191 192 return sysfs_emit(buf, "%s\n", adev->product_number); 193 } 194 195 static DEVICE_ATTR(product_number, S_IRUGO, 196 amdgpu_device_get_product_number, NULL); 197 198 /** 199 * DOC: serial_number 200 * 201 * The amdgpu driver provides a sysfs API for reporting the serial number 202 * for the device 203 * The file serial_number is used for this and returns the serial number 204 * as returned from the FRU. 205 * NOTE: This is only available for certain server cards 206 */ 207 208 static ssize_t amdgpu_device_get_serial_number(struct device *dev, 209 struct device_attribute *attr, char *buf) 210 { 211 struct drm_device *ddev = dev_get_drvdata(dev); 212 struct amdgpu_device *adev = drm_to_adev(ddev); 213 214 return sysfs_emit(buf, "%s\n", adev->serial); 215 } 216 217 static DEVICE_ATTR(serial_number, S_IRUGO, 218 amdgpu_device_get_serial_number, NULL); 219 220 /** 221 * amdgpu_device_supports_px - Is the device a dGPU with ATPX power control 222 * 223 * @dev: drm_device pointer 224 * 225 * Returns true if the device is a dGPU with ATPX power control, 226 * otherwise return false. 227 */ 228 bool amdgpu_device_supports_px(struct drm_device *dev) 229 { 230 struct amdgpu_device *adev = drm_to_adev(dev); 231 232 if ((adev->flags & AMD_IS_PX) && !amdgpu_is_atpx_hybrid()) 233 return true; 234 return false; 235 } 236 237 /** 238 * amdgpu_device_supports_boco - Is the device a dGPU with ACPI power resources 239 * 240 * @dev: drm_device pointer 241 * 242 * Returns true if the device is a dGPU with ACPI power control, 243 * otherwise return false. 244 */ 245 bool amdgpu_device_supports_boco(struct drm_device *dev) 246 { 247 struct amdgpu_device *adev = drm_to_adev(dev); 248 249 if (adev->has_pr3 || 250 ((adev->flags & AMD_IS_PX) && amdgpu_is_atpx_hybrid())) 251 return true; 252 return false; 253 } 254 255 /** 256 * amdgpu_device_supports_baco - Does the device support BACO 257 * 258 * @dev: drm_device pointer 259 * 260 * Returns true if the device supporte BACO, 261 * otherwise return false. 262 */ 263 bool amdgpu_device_supports_baco(struct drm_device *dev) 264 { 265 struct amdgpu_device *adev = drm_to_adev(dev); 266 267 return amdgpu_asic_supports_baco(adev); 268 } 269 270 /** 271 * amdgpu_device_supports_smart_shift - Is the device dGPU with 272 * smart shift support 273 * 274 * @dev: drm_device pointer 275 * 276 * Returns true if the device is a dGPU with Smart Shift support, 277 * otherwise returns false. 278 */ 279 bool amdgpu_device_supports_smart_shift(struct drm_device *dev) 280 { 281 return (amdgpu_device_supports_boco(dev) && 282 amdgpu_acpi_is_power_shift_control_supported()); 283 } 284 285 /* 286 * VRAM access helper functions 287 */ 288 289 /** 290 * amdgpu_device_vram_access - read/write a buffer in vram 291 * 292 * @adev: amdgpu_device pointer 293 * @pos: offset of the buffer in vram 294 * @buf: virtual address of the buffer in system memory 295 * @size: read/write size, sizeof(@buf) must > @size 296 * @write: true - write to vram, otherwise - read from vram 297 */ 298 void amdgpu_device_vram_access(struct amdgpu_device *adev, loff_t pos, 299 uint32_t *buf, size_t size, bool write) 300 { 301 unsigned long flags; 302 uint32_t hi = ~0; 303 uint64_t last; 304 int idx; 305 306 if (!drm_dev_enter(&adev->ddev, &idx)) 307 return; 308 309 #ifdef CONFIG_64BIT 310 last = min(pos + size, adev->gmc.visible_vram_size); 311 if (last > pos) { 312 void __iomem *addr = adev->mman.aper_base_kaddr + pos; 313 size_t count = last - pos; 314 315 if (write) { 316 memcpy_toio(addr, buf, count); 317 mb(); 318 amdgpu_device_flush_hdp(adev, NULL); 319 } else { 320 amdgpu_device_invalidate_hdp(adev, NULL); 321 mb(); 322 memcpy_fromio(buf, addr, count); 323 } 324 325 if (count == size) 326 goto exit; 327 328 pos += count; 329 buf += count / 4; 330 size -= count; 331 } 332 #endif 333 334 spin_lock_irqsave(&adev->mmio_idx_lock, flags); 335 for (last = pos + size; pos < last; pos += 4) { 336 uint32_t tmp = pos >> 31; 337 338 WREG32_NO_KIQ(mmMM_INDEX, ((uint32_t)pos) | 0x80000000); 339 if (tmp != hi) { 340 WREG32_NO_KIQ(mmMM_INDEX_HI, tmp); 341 hi = tmp; 342 } 343 if (write) 344 WREG32_NO_KIQ(mmMM_DATA, *buf++); 345 else 346 *buf++ = RREG32_NO_KIQ(mmMM_DATA); 347 } 348 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags); 349 350 #ifdef CONFIG_64BIT 351 exit: 352 #endif 353 drm_dev_exit(idx); 354 } 355 356 /* 357 * register access helper functions. 358 */ 359 360 /* Check if hw access should be skipped because of hotplug or device error */ 361 bool amdgpu_device_skip_hw_access(struct amdgpu_device *adev) 362 { 363 if (adev->no_hw_access) 364 return true; 365 366 #ifdef CONFIG_LOCKDEP 367 /* 368 * This is a bit complicated to understand, so worth a comment. What we assert 369 * here is that the GPU reset is not running on another thread in parallel. 370 * 371 * For this we trylock the read side of the reset semaphore, if that succeeds 372 * we know that the reset is not running in paralell. 373 * 374 * If the trylock fails we assert that we are either already holding the read 375 * side of the lock or are the reset thread itself and hold the write side of 376 * the lock. 377 */ 378 if (in_task()) { 379 if (down_read_trylock(&adev->reset_sem)) 380 up_read(&adev->reset_sem); 381 else 382 lockdep_assert_held(&adev->reset_sem); 383 } 384 #endif 385 return false; 386 } 387 388 /** 389 * amdgpu_device_rreg - read a memory mapped IO or indirect register 390 * 391 * @adev: amdgpu_device pointer 392 * @reg: dword aligned register offset 393 * @acc_flags: access flags which require special behavior 394 * 395 * Returns the 32 bit value from the offset specified. 396 */ 397 uint32_t amdgpu_device_rreg(struct amdgpu_device *adev, 398 uint32_t reg, uint32_t acc_flags) 399 { 400 uint32_t ret; 401 402 if (amdgpu_device_skip_hw_access(adev)) 403 return 0; 404 405 if ((reg * 4) < adev->rmmio_size) { 406 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && 407 amdgpu_sriov_runtime(adev) && 408 down_read_trylock(&adev->reset_sem)) { 409 ret = amdgpu_kiq_rreg(adev, reg); 410 up_read(&adev->reset_sem); 411 } else { 412 ret = readl(((void __iomem *)adev->rmmio) + (reg * 4)); 413 } 414 } else { 415 ret = adev->pcie_rreg(adev, reg * 4); 416 } 417 418 trace_amdgpu_device_rreg(adev->pdev->device, reg, ret); 419 420 return ret; 421 } 422 423 /* 424 * MMIO register read with bytes helper functions 425 * @offset:bytes offset from MMIO start 426 * 427 */ 428 429 /** 430 * amdgpu_mm_rreg8 - read a memory mapped IO register 431 * 432 * @adev: amdgpu_device pointer 433 * @offset: byte aligned register offset 434 * 435 * Returns the 8 bit value from the offset specified. 436 */ 437 uint8_t amdgpu_mm_rreg8(struct amdgpu_device *adev, uint32_t offset) 438 { 439 if (amdgpu_device_skip_hw_access(adev)) 440 return 0; 441 442 if (offset < adev->rmmio_size) 443 return (readb(adev->rmmio + offset)); 444 BUG(); 445 } 446 447 /* 448 * MMIO register write with bytes helper functions 449 * @offset:bytes offset from MMIO start 450 * @value: the value want to be written to the register 451 * 452 */ 453 /** 454 * amdgpu_mm_wreg8 - read a memory mapped IO register 455 * 456 * @adev: amdgpu_device pointer 457 * @offset: byte aligned register offset 458 * @value: 8 bit value to write 459 * 460 * Writes the value specified to the offset specified. 461 */ 462 void amdgpu_mm_wreg8(struct amdgpu_device *adev, uint32_t offset, uint8_t value) 463 { 464 if (amdgpu_device_skip_hw_access(adev)) 465 return; 466 467 if (offset < adev->rmmio_size) 468 writeb(value, adev->rmmio + offset); 469 else 470 BUG(); 471 } 472 473 /** 474 * amdgpu_device_wreg - write to a memory mapped IO or indirect register 475 * 476 * @adev: amdgpu_device pointer 477 * @reg: dword aligned register offset 478 * @v: 32 bit value to write to the register 479 * @acc_flags: access flags which require special behavior 480 * 481 * Writes the value specified to the offset specified. 482 */ 483 void amdgpu_device_wreg(struct amdgpu_device *adev, 484 uint32_t reg, uint32_t v, 485 uint32_t acc_flags) 486 { 487 if (amdgpu_device_skip_hw_access(adev)) 488 return; 489 490 if ((reg * 4) < adev->rmmio_size) { 491 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && 492 amdgpu_sriov_runtime(adev) && 493 down_read_trylock(&adev->reset_sem)) { 494 amdgpu_kiq_wreg(adev, reg, v); 495 up_read(&adev->reset_sem); 496 } else { 497 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4)); 498 } 499 } else { 500 adev->pcie_wreg(adev, reg * 4, v); 501 } 502 503 trace_amdgpu_device_wreg(adev->pdev->device, reg, v); 504 } 505 506 /* 507 * amdgpu_mm_wreg_mmio_rlc - write register either with mmio or with RLC path if in range 508 * 509 * this function is invoked only the debugfs register access 510 * */ 511 void amdgpu_mm_wreg_mmio_rlc(struct amdgpu_device *adev, 512 uint32_t reg, uint32_t v) 513 { 514 if (amdgpu_device_skip_hw_access(adev)) 515 return; 516 517 if (amdgpu_sriov_fullaccess(adev) && 518 adev->gfx.rlc.funcs && 519 adev->gfx.rlc.funcs->is_rlcg_access_range) { 520 if (adev->gfx.rlc.funcs->is_rlcg_access_range(adev, reg)) 521 return adev->gfx.rlc.funcs->rlcg_wreg(adev, reg, v, 0, 0); 522 } else { 523 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4)); 524 } 525 } 526 527 /** 528 * amdgpu_mm_rdoorbell - read a doorbell dword 529 * 530 * @adev: amdgpu_device pointer 531 * @index: doorbell index 532 * 533 * Returns the value in the doorbell aperture at the 534 * requested doorbell index (CIK). 535 */ 536 u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index) 537 { 538 if (amdgpu_device_skip_hw_access(adev)) 539 return 0; 540 541 if (index < adev->doorbell.num_doorbells) { 542 return readl(adev->doorbell.ptr + index); 543 } else { 544 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index); 545 return 0; 546 } 547 } 548 549 /** 550 * amdgpu_mm_wdoorbell - write a doorbell dword 551 * 552 * @adev: amdgpu_device pointer 553 * @index: doorbell index 554 * @v: value to write 555 * 556 * Writes @v to the doorbell aperture at the 557 * requested doorbell index (CIK). 558 */ 559 void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v) 560 { 561 if (amdgpu_device_skip_hw_access(adev)) 562 return; 563 564 if (index < adev->doorbell.num_doorbells) { 565 writel(v, adev->doorbell.ptr + index); 566 } else { 567 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index); 568 } 569 } 570 571 /** 572 * amdgpu_mm_rdoorbell64 - read a doorbell Qword 573 * 574 * @adev: amdgpu_device pointer 575 * @index: doorbell index 576 * 577 * Returns the value in the doorbell aperture at the 578 * requested doorbell index (VEGA10+). 579 */ 580 u64 amdgpu_mm_rdoorbell64(struct amdgpu_device *adev, u32 index) 581 { 582 if (amdgpu_device_skip_hw_access(adev)) 583 return 0; 584 585 if (index < adev->doorbell.num_doorbells) { 586 return atomic64_read((atomic64_t *)(adev->doorbell.ptr + index)); 587 } else { 588 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index); 589 return 0; 590 } 591 } 592 593 /** 594 * amdgpu_mm_wdoorbell64 - write a doorbell Qword 595 * 596 * @adev: amdgpu_device pointer 597 * @index: doorbell index 598 * @v: value to write 599 * 600 * Writes @v to the doorbell aperture at the 601 * requested doorbell index (VEGA10+). 602 */ 603 void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v) 604 { 605 if (amdgpu_device_skip_hw_access(adev)) 606 return; 607 608 if (index < adev->doorbell.num_doorbells) { 609 atomic64_set((atomic64_t *)(adev->doorbell.ptr + index), v); 610 } else { 611 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index); 612 } 613 } 614 615 /** 616 * amdgpu_device_indirect_rreg - read an indirect register 617 * 618 * @adev: amdgpu_device pointer 619 * @pcie_index: mmio register offset 620 * @pcie_data: mmio register offset 621 * @reg_addr: indirect register address to read from 622 * 623 * Returns the value of indirect register @reg_addr 624 */ 625 u32 amdgpu_device_indirect_rreg(struct amdgpu_device *adev, 626 u32 pcie_index, u32 pcie_data, 627 u32 reg_addr) 628 { 629 unsigned long flags; 630 u32 r; 631 void __iomem *pcie_index_offset; 632 void __iomem *pcie_data_offset; 633 634 spin_lock_irqsave(&adev->pcie_idx_lock, flags); 635 pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4; 636 pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4; 637 638 writel(reg_addr, pcie_index_offset); 639 readl(pcie_index_offset); 640 r = readl(pcie_data_offset); 641 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags); 642 643 return r; 644 } 645 646 /** 647 * amdgpu_device_indirect_rreg64 - read a 64bits indirect register 648 * 649 * @adev: amdgpu_device pointer 650 * @pcie_index: mmio register offset 651 * @pcie_data: mmio register offset 652 * @reg_addr: indirect register address to read from 653 * 654 * Returns the value of indirect register @reg_addr 655 */ 656 u64 amdgpu_device_indirect_rreg64(struct amdgpu_device *adev, 657 u32 pcie_index, u32 pcie_data, 658 u32 reg_addr) 659 { 660 unsigned long flags; 661 u64 r; 662 void __iomem *pcie_index_offset; 663 void __iomem *pcie_data_offset; 664 665 spin_lock_irqsave(&adev->pcie_idx_lock, flags); 666 pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4; 667 pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4; 668 669 /* read low 32 bits */ 670 writel(reg_addr, pcie_index_offset); 671 readl(pcie_index_offset); 672 r = readl(pcie_data_offset); 673 /* read high 32 bits */ 674 writel(reg_addr + 4, pcie_index_offset); 675 readl(pcie_index_offset); 676 r |= ((u64)readl(pcie_data_offset) << 32); 677 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags); 678 679 return r; 680 } 681 682 /** 683 * amdgpu_device_indirect_wreg - write an indirect register address 684 * 685 * @adev: amdgpu_device pointer 686 * @pcie_index: mmio register offset 687 * @pcie_data: mmio register offset 688 * @reg_addr: indirect register offset 689 * @reg_data: indirect register data 690 * 691 */ 692 void amdgpu_device_indirect_wreg(struct amdgpu_device *adev, 693 u32 pcie_index, u32 pcie_data, 694 u32 reg_addr, u32 reg_data) 695 { 696 unsigned long flags; 697 void __iomem *pcie_index_offset; 698 void __iomem *pcie_data_offset; 699 700 spin_lock_irqsave(&adev->pcie_idx_lock, flags); 701 pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4; 702 pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4; 703 704 writel(reg_addr, pcie_index_offset); 705 readl(pcie_index_offset); 706 writel(reg_data, pcie_data_offset); 707 readl(pcie_data_offset); 708 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags); 709 } 710 711 /** 712 * amdgpu_device_indirect_wreg64 - write a 64bits indirect register address 713 * 714 * @adev: amdgpu_device pointer 715 * @pcie_index: mmio register offset 716 * @pcie_data: mmio register offset 717 * @reg_addr: indirect register offset 718 * @reg_data: indirect register data 719 * 720 */ 721 void amdgpu_device_indirect_wreg64(struct amdgpu_device *adev, 722 u32 pcie_index, u32 pcie_data, 723 u32 reg_addr, u64 reg_data) 724 { 725 unsigned long flags; 726 void __iomem *pcie_index_offset; 727 void __iomem *pcie_data_offset; 728 729 spin_lock_irqsave(&adev->pcie_idx_lock, flags); 730 pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4; 731 pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4; 732 733 /* write low 32 bits */ 734 writel(reg_addr, pcie_index_offset); 735 readl(pcie_index_offset); 736 writel((u32)(reg_data & 0xffffffffULL), pcie_data_offset); 737 readl(pcie_data_offset); 738 /* write high 32 bits */ 739 writel(reg_addr + 4, pcie_index_offset); 740 readl(pcie_index_offset); 741 writel((u32)(reg_data >> 32), pcie_data_offset); 742 readl(pcie_data_offset); 743 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags); 744 } 745 746 /** 747 * amdgpu_invalid_rreg - dummy reg read function 748 * 749 * @adev: amdgpu_device pointer 750 * @reg: offset of register 751 * 752 * Dummy register read function. Used for register blocks 753 * that certain asics don't have (all asics). 754 * Returns the value in the register. 755 */ 756 static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg) 757 { 758 DRM_ERROR("Invalid callback to read register 0x%04X\n", reg); 759 BUG(); 760 return 0; 761 } 762 763 /** 764 * amdgpu_invalid_wreg - dummy reg write function 765 * 766 * @adev: amdgpu_device pointer 767 * @reg: offset of register 768 * @v: value to write to the register 769 * 770 * Dummy register read function. Used for register blocks 771 * that certain asics don't have (all asics). 772 */ 773 static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v) 774 { 775 DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n", 776 reg, v); 777 BUG(); 778 } 779 780 /** 781 * amdgpu_invalid_rreg64 - dummy 64 bit reg read function 782 * 783 * @adev: amdgpu_device pointer 784 * @reg: offset of register 785 * 786 * Dummy register read function. Used for register blocks 787 * that certain asics don't have (all asics). 788 * Returns the value in the register. 789 */ 790 static uint64_t amdgpu_invalid_rreg64(struct amdgpu_device *adev, uint32_t reg) 791 { 792 DRM_ERROR("Invalid callback to read 64 bit register 0x%04X\n", reg); 793 BUG(); 794 return 0; 795 } 796 797 /** 798 * amdgpu_invalid_wreg64 - dummy reg write function 799 * 800 * @adev: amdgpu_device pointer 801 * @reg: offset of register 802 * @v: value to write to the register 803 * 804 * Dummy register read function. Used for register blocks 805 * that certain asics don't have (all asics). 806 */ 807 static void amdgpu_invalid_wreg64(struct amdgpu_device *adev, uint32_t reg, uint64_t v) 808 { 809 DRM_ERROR("Invalid callback to write 64 bit register 0x%04X with 0x%08llX\n", 810 reg, v); 811 BUG(); 812 } 813 814 /** 815 * amdgpu_block_invalid_rreg - dummy reg read function 816 * 817 * @adev: amdgpu_device pointer 818 * @block: offset of instance 819 * @reg: offset of register 820 * 821 * Dummy register read function. Used for register blocks 822 * that certain asics don't have (all asics). 823 * Returns the value in the register. 824 */ 825 static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev, 826 uint32_t block, uint32_t reg) 827 { 828 DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n", 829 reg, block); 830 BUG(); 831 return 0; 832 } 833 834 /** 835 * amdgpu_block_invalid_wreg - dummy reg write function 836 * 837 * @adev: amdgpu_device pointer 838 * @block: offset of instance 839 * @reg: offset of register 840 * @v: value to write to the register 841 * 842 * Dummy register read function. Used for register blocks 843 * that certain asics don't have (all asics). 844 */ 845 static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev, 846 uint32_t block, 847 uint32_t reg, uint32_t v) 848 { 849 DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n", 850 reg, block, v); 851 BUG(); 852 } 853 854 /** 855 * amdgpu_device_asic_init - Wrapper for atom asic_init 856 * 857 * @adev: amdgpu_device pointer 858 * 859 * Does any asic specific work and then calls atom asic init. 860 */ 861 static int amdgpu_device_asic_init(struct amdgpu_device *adev) 862 { 863 amdgpu_asic_pre_asic_init(adev); 864 865 return amdgpu_atom_asic_init(adev->mode_info.atom_context); 866 } 867 868 /** 869 * amdgpu_device_vram_scratch_init - allocate the VRAM scratch page 870 * 871 * @adev: amdgpu_device pointer 872 * 873 * Allocates a scratch page of VRAM for use by various things in the 874 * driver. 875 */ 876 static int amdgpu_device_vram_scratch_init(struct amdgpu_device *adev) 877 { 878 return amdgpu_bo_create_kernel(adev, AMDGPU_GPU_PAGE_SIZE, 879 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM, 880 &adev->vram_scratch.robj, 881 &adev->vram_scratch.gpu_addr, 882 (void **)&adev->vram_scratch.ptr); 883 } 884 885 /** 886 * amdgpu_device_vram_scratch_fini - Free the VRAM scratch page 887 * 888 * @adev: amdgpu_device pointer 889 * 890 * Frees the VRAM scratch page. 891 */ 892 static void amdgpu_device_vram_scratch_fini(struct amdgpu_device *adev) 893 { 894 amdgpu_bo_free_kernel(&adev->vram_scratch.robj, NULL, NULL); 895 } 896 897 /** 898 * amdgpu_device_program_register_sequence - program an array of registers. 899 * 900 * @adev: amdgpu_device pointer 901 * @registers: pointer to the register array 902 * @array_size: size of the register array 903 * 904 * Programs an array or registers with and and or masks. 905 * This is a helper for setting golden registers. 906 */ 907 void amdgpu_device_program_register_sequence(struct amdgpu_device *adev, 908 const u32 *registers, 909 const u32 array_size) 910 { 911 u32 tmp, reg, and_mask, or_mask; 912 int i; 913 914 if (array_size % 3) 915 return; 916 917 for (i = 0; i < array_size; i +=3) { 918 reg = registers[i + 0]; 919 and_mask = registers[i + 1]; 920 or_mask = registers[i + 2]; 921 922 if (and_mask == 0xffffffff) { 923 tmp = or_mask; 924 } else { 925 tmp = RREG32(reg); 926 tmp &= ~and_mask; 927 if (adev->family >= AMDGPU_FAMILY_AI) 928 tmp |= (or_mask & and_mask); 929 else 930 tmp |= or_mask; 931 } 932 WREG32(reg, tmp); 933 } 934 } 935 936 /** 937 * amdgpu_device_pci_config_reset - reset the GPU 938 * 939 * @adev: amdgpu_device pointer 940 * 941 * Resets the GPU using the pci config reset sequence. 942 * Only applicable to asics prior to vega10. 943 */ 944 void amdgpu_device_pci_config_reset(struct amdgpu_device *adev) 945 { 946 pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA); 947 } 948 949 /** 950 * amdgpu_device_pci_reset - reset the GPU using generic PCI means 951 * 952 * @adev: amdgpu_device pointer 953 * 954 * Resets the GPU using generic pci reset interfaces (FLR, SBR, etc.). 955 */ 956 int amdgpu_device_pci_reset(struct amdgpu_device *adev) 957 { 958 return pci_reset_function(adev->pdev); 959 } 960 961 /* 962 * GPU doorbell aperture helpers function. 963 */ 964 /** 965 * amdgpu_device_doorbell_init - Init doorbell driver information. 966 * 967 * @adev: amdgpu_device pointer 968 * 969 * Init doorbell driver information (CIK) 970 * Returns 0 on success, error on failure. 971 */ 972 static int amdgpu_device_doorbell_init(struct amdgpu_device *adev) 973 { 974 975 /* No doorbell on SI hardware generation */ 976 if (adev->asic_type < CHIP_BONAIRE) { 977 adev->doorbell.base = 0; 978 adev->doorbell.size = 0; 979 adev->doorbell.num_doorbells = 0; 980 adev->doorbell.ptr = NULL; 981 return 0; 982 } 983 984 if (pci_resource_flags(adev->pdev, 2) & IORESOURCE_UNSET) 985 return -EINVAL; 986 987 amdgpu_asic_init_doorbell_index(adev); 988 989 /* doorbell bar mapping */ 990 adev->doorbell.base = pci_resource_start(adev->pdev, 2); 991 adev->doorbell.size = pci_resource_len(adev->pdev, 2); 992 993 adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32), 994 adev->doorbell_index.max_assignment+1); 995 if (adev->doorbell.num_doorbells == 0) 996 return -EINVAL; 997 998 /* For Vega, reserve and map two pages on doorbell BAR since SDMA 999 * paging queue doorbell use the second page. The 1000 * AMDGPU_DOORBELL64_MAX_ASSIGNMENT definition assumes all the 1001 * doorbells are in the first page. So with paging queue enabled, 1002 * the max num_doorbells should + 1 page (0x400 in dword) 1003 */ 1004 if (adev->asic_type >= CHIP_VEGA10) 1005 adev->doorbell.num_doorbells += 0x400; 1006 1007 adev->doorbell.ptr = ioremap(adev->doorbell.base, 1008 adev->doorbell.num_doorbells * 1009 sizeof(u32)); 1010 if (adev->doorbell.ptr == NULL) 1011 return -ENOMEM; 1012 1013 return 0; 1014 } 1015 1016 /** 1017 * amdgpu_device_doorbell_fini - Tear down doorbell driver information. 1018 * 1019 * @adev: amdgpu_device pointer 1020 * 1021 * Tear down doorbell driver information (CIK) 1022 */ 1023 static void amdgpu_device_doorbell_fini(struct amdgpu_device *adev) 1024 { 1025 iounmap(adev->doorbell.ptr); 1026 adev->doorbell.ptr = NULL; 1027 } 1028 1029 1030 1031 /* 1032 * amdgpu_device_wb_*() 1033 * Writeback is the method by which the GPU updates special pages in memory 1034 * with the status of certain GPU events (fences, ring pointers,etc.). 1035 */ 1036 1037 /** 1038 * amdgpu_device_wb_fini - Disable Writeback and free memory 1039 * 1040 * @adev: amdgpu_device pointer 1041 * 1042 * Disables Writeback and frees the Writeback memory (all asics). 1043 * Used at driver shutdown. 1044 */ 1045 static void amdgpu_device_wb_fini(struct amdgpu_device *adev) 1046 { 1047 if (adev->wb.wb_obj) { 1048 amdgpu_bo_free_kernel(&adev->wb.wb_obj, 1049 &adev->wb.gpu_addr, 1050 (void **)&adev->wb.wb); 1051 adev->wb.wb_obj = NULL; 1052 } 1053 } 1054 1055 /** 1056 * amdgpu_device_wb_init- Init Writeback driver info and allocate memory 1057 * 1058 * @adev: amdgpu_device pointer 1059 * 1060 * Initializes writeback and allocates writeback memory (all asics). 1061 * Used at driver startup. 1062 * Returns 0 on success or an -error on failure. 1063 */ 1064 static int amdgpu_device_wb_init(struct amdgpu_device *adev) 1065 { 1066 int r; 1067 1068 if (adev->wb.wb_obj == NULL) { 1069 /* AMDGPU_MAX_WB * sizeof(uint32_t) * 8 = AMDGPU_MAX_WB 256bit slots */ 1070 r = amdgpu_bo_create_kernel(adev, AMDGPU_MAX_WB * sizeof(uint32_t) * 8, 1071 PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT, 1072 &adev->wb.wb_obj, &adev->wb.gpu_addr, 1073 (void **)&adev->wb.wb); 1074 if (r) { 1075 dev_warn(adev->dev, "(%d) create WB bo failed\n", r); 1076 return r; 1077 } 1078 1079 adev->wb.num_wb = AMDGPU_MAX_WB; 1080 memset(&adev->wb.used, 0, sizeof(adev->wb.used)); 1081 1082 /* clear wb memory */ 1083 memset((char *)adev->wb.wb, 0, AMDGPU_MAX_WB * sizeof(uint32_t) * 8); 1084 } 1085 1086 return 0; 1087 } 1088 1089 /** 1090 * amdgpu_device_wb_get - Allocate a wb entry 1091 * 1092 * @adev: amdgpu_device pointer 1093 * @wb: wb index 1094 * 1095 * Allocate a wb slot for use by the driver (all asics). 1096 * Returns 0 on success or -EINVAL on failure. 1097 */ 1098 int amdgpu_device_wb_get(struct amdgpu_device *adev, u32 *wb) 1099 { 1100 unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb); 1101 1102 if (offset < adev->wb.num_wb) { 1103 __set_bit(offset, adev->wb.used); 1104 *wb = offset << 3; /* convert to dw offset */ 1105 return 0; 1106 } else { 1107 return -EINVAL; 1108 } 1109 } 1110 1111 /** 1112 * amdgpu_device_wb_free - Free a wb entry 1113 * 1114 * @adev: amdgpu_device pointer 1115 * @wb: wb index 1116 * 1117 * Free a wb slot allocated for use by the driver (all asics) 1118 */ 1119 void amdgpu_device_wb_free(struct amdgpu_device *adev, u32 wb) 1120 { 1121 wb >>= 3; 1122 if (wb < adev->wb.num_wb) 1123 __clear_bit(wb, adev->wb.used); 1124 } 1125 1126 /** 1127 * amdgpu_device_resize_fb_bar - try to resize FB BAR 1128 * 1129 * @adev: amdgpu_device pointer 1130 * 1131 * Try to resize FB BAR to make all VRAM CPU accessible. We try very hard not 1132 * to fail, but if any of the BARs is not accessible after the size we abort 1133 * driver loading by returning -ENODEV. 1134 */ 1135 int amdgpu_device_resize_fb_bar(struct amdgpu_device *adev) 1136 { 1137 int rbar_size = pci_rebar_bytes_to_size(adev->gmc.real_vram_size); 1138 struct pci_bus *root; 1139 struct resource *res; 1140 unsigned i; 1141 u16 cmd; 1142 int r; 1143 1144 /* Bypass for VF */ 1145 if (amdgpu_sriov_vf(adev)) 1146 return 0; 1147 1148 /* skip if the bios has already enabled large BAR */ 1149 if (adev->gmc.real_vram_size && 1150 (pci_resource_len(adev->pdev, 0) >= adev->gmc.real_vram_size)) 1151 return 0; 1152 1153 /* Check if the root BUS has 64bit memory resources */ 1154 root = adev->pdev->bus; 1155 while (root->parent) 1156 root = root->parent; 1157 1158 pci_bus_for_each_resource(root, res, i) { 1159 if (res && res->flags & (IORESOURCE_MEM | IORESOURCE_MEM_64) && 1160 res->start > 0x100000000ull) 1161 break; 1162 } 1163 1164 /* Trying to resize is pointless without a root hub window above 4GB */ 1165 if (!res) 1166 return 0; 1167 1168 /* Limit the BAR size to what is available */ 1169 rbar_size = min(fls(pci_rebar_get_possible_sizes(adev->pdev, 0)) - 1, 1170 rbar_size); 1171 1172 /* Disable memory decoding while we change the BAR addresses and size */ 1173 pci_read_config_word(adev->pdev, PCI_COMMAND, &cmd); 1174 pci_write_config_word(adev->pdev, PCI_COMMAND, 1175 cmd & ~PCI_COMMAND_MEMORY); 1176 1177 /* Free the VRAM and doorbell BAR, we most likely need to move both. */ 1178 amdgpu_device_doorbell_fini(adev); 1179 if (adev->asic_type >= CHIP_BONAIRE) 1180 pci_release_resource(adev->pdev, 2); 1181 1182 pci_release_resource(adev->pdev, 0); 1183 1184 r = pci_resize_resource(adev->pdev, 0, rbar_size); 1185 if (r == -ENOSPC) 1186 DRM_INFO("Not enough PCI address space for a large BAR."); 1187 else if (r && r != -ENOTSUPP) 1188 DRM_ERROR("Problem resizing BAR0 (%d).", r); 1189 1190 pci_assign_unassigned_bus_resources(adev->pdev->bus); 1191 1192 /* When the doorbell or fb BAR isn't available we have no chance of 1193 * using the device. 1194 */ 1195 r = amdgpu_device_doorbell_init(adev); 1196 if (r || (pci_resource_flags(adev->pdev, 0) & IORESOURCE_UNSET)) 1197 return -ENODEV; 1198 1199 pci_write_config_word(adev->pdev, PCI_COMMAND, cmd); 1200 1201 return 0; 1202 } 1203 1204 /* 1205 * GPU helpers function. 1206 */ 1207 /** 1208 * amdgpu_device_need_post - check if the hw need post or not 1209 * 1210 * @adev: amdgpu_device pointer 1211 * 1212 * Check if the asic has been initialized (all asics) at driver startup 1213 * or post is needed if hw reset is performed. 1214 * Returns true if need or false if not. 1215 */ 1216 bool amdgpu_device_need_post(struct amdgpu_device *adev) 1217 { 1218 uint32_t reg; 1219 1220 if (amdgpu_sriov_vf(adev)) 1221 return false; 1222 1223 if (amdgpu_passthrough(adev)) { 1224 /* for FIJI: In whole GPU pass-through virtualization case, after VM reboot 1225 * some old smc fw still need driver do vPost otherwise gpu hang, while 1226 * those smc fw version above 22.15 doesn't have this flaw, so we force 1227 * vpost executed for smc version below 22.15 1228 */ 1229 if (adev->asic_type == CHIP_FIJI) { 1230 int err; 1231 uint32_t fw_ver; 1232 err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev); 1233 /* force vPost if error occured */ 1234 if (err) 1235 return true; 1236 1237 fw_ver = *((uint32_t *)adev->pm.fw->data + 69); 1238 if (fw_ver < 0x00160e00) 1239 return true; 1240 } 1241 } 1242 1243 /* Don't post if we need to reset whole hive on init */ 1244 if (adev->gmc.xgmi.pending_reset) 1245 return false; 1246 1247 if (adev->has_hw_reset) { 1248 adev->has_hw_reset = false; 1249 return true; 1250 } 1251 1252 /* bios scratch used on CIK+ */ 1253 if (adev->asic_type >= CHIP_BONAIRE) 1254 return amdgpu_atombios_scratch_need_asic_init(adev); 1255 1256 /* check MEM_SIZE for older asics */ 1257 reg = amdgpu_asic_get_config_memsize(adev); 1258 1259 if ((reg != 0) && (reg != 0xffffffff)) 1260 return false; 1261 1262 return true; 1263 } 1264 1265 /* if we get transitioned to only one device, take VGA back */ 1266 /** 1267 * amdgpu_device_vga_set_decode - enable/disable vga decode 1268 * 1269 * @cookie: amdgpu_device pointer 1270 * @state: enable/disable vga decode 1271 * 1272 * Enable/disable vga decode (all asics). 1273 * Returns VGA resource flags. 1274 */ 1275 static unsigned int amdgpu_device_vga_set_decode(void *cookie, bool state) 1276 { 1277 struct amdgpu_device *adev = cookie; 1278 amdgpu_asic_set_vga_state(adev, state); 1279 if (state) 1280 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM | 1281 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM; 1282 else 1283 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM; 1284 } 1285 1286 /** 1287 * amdgpu_device_check_block_size - validate the vm block size 1288 * 1289 * @adev: amdgpu_device pointer 1290 * 1291 * Validates the vm block size specified via module parameter. 1292 * The vm block size defines number of bits in page table versus page directory, 1293 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the 1294 * page table and the remaining bits are in the page directory. 1295 */ 1296 static void amdgpu_device_check_block_size(struct amdgpu_device *adev) 1297 { 1298 /* defines number of bits in page table versus page directory, 1299 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the 1300 * page table and the remaining bits are in the page directory */ 1301 if (amdgpu_vm_block_size == -1) 1302 return; 1303 1304 if (amdgpu_vm_block_size < 9) { 1305 dev_warn(adev->dev, "VM page table size (%d) too small\n", 1306 amdgpu_vm_block_size); 1307 amdgpu_vm_block_size = -1; 1308 } 1309 } 1310 1311 /** 1312 * amdgpu_device_check_vm_size - validate the vm size 1313 * 1314 * @adev: amdgpu_device pointer 1315 * 1316 * Validates the vm size in GB specified via module parameter. 1317 * The VM size is the size of the GPU virtual memory space in GB. 1318 */ 1319 static void amdgpu_device_check_vm_size(struct amdgpu_device *adev) 1320 { 1321 /* no need to check the default value */ 1322 if (amdgpu_vm_size == -1) 1323 return; 1324 1325 if (amdgpu_vm_size < 1) { 1326 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n", 1327 amdgpu_vm_size); 1328 amdgpu_vm_size = -1; 1329 } 1330 } 1331 1332 static void amdgpu_device_check_smu_prv_buffer_size(struct amdgpu_device *adev) 1333 { 1334 struct sysinfo si; 1335 bool is_os_64 = (sizeof(void *) == 8); 1336 uint64_t total_memory; 1337 uint64_t dram_size_seven_GB = 0x1B8000000; 1338 uint64_t dram_size_three_GB = 0xB8000000; 1339 1340 if (amdgpu_smu_memory_pool_size == 0) 1341 return; 1342 1343 if (!is_os_64) { 1344 DRM_WARN("Not 64-bit OS, feature not supported\n"); 1345 goto def_value; 1346 } 1347 si_meminfo(&si); 1348 total_memory = (uint64_t)si.totalram * si.mem_unit; 1349 1350 if ((amdgpu_smu_memory_pool_size == 1) || 1351 (amdgpu_smu_memory_pool_size == 2)) { 1352 if (total_memory < dram_size_three_GB) 1353 goto def_value1; 1354 } else if ((amdgpu_smu_memory_pool_size == 4) || 1355 (amdgpu_smu_memory_pool_size == 8)) { 1356 if (total_memory < dram_size_seven_GB) 1357 goto def_value1; 1358 } else { 1359 DRM_WARN("Smu memory pool size not supported\n"); 1360 goto def_value; 1361 } 1362 adev->pm.smu_prv_buffer_size = amdgpu_smu_memory_pool_size << 28; 1363 1364 return; 1365 1366 def_value1: 1367 DRM_WARN("No enough system memory\n"); 1368 def_value: 1369 adev->pm.smu_prv_buffer_size = 0; 1370 } 1371 1372 /** 1373 * amdgpu_device_check_arguments - validate module params 1374 * 1375 * @adev: amdgpu_device pointer 1376 * 1377 * Validates certain module parameters and updates 1378 * the associated values used by the driver (all asics). 1379 */ 1380 static int amdgpu_device_check_arguments(struct amdgpu_device *adev) 1381 { 1382 if (amdgpu_sched_jobs < 4) { 1383 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n", 1384 amdgpu_sched_jobs); 1385 amdgpu_sched_jobs = 4; 1386 } else if (!is_power_of_2(amdgpu_sched_jobs)){ 1387 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n", 1388 amdgpu_sched_jobs); 1389 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs); 1390 } 1391 1392 if (amdgpu_gart_size != -1 && amdgpu_gart_size < 32) { 1393 /* gart size must be greater or equal to 32M */ 1394 dev_warn(adev->dev, "gart size (%d) too small\n", 1395 amdgpu_gart_size); 1396 amdgpu_gart_size = -1; 1397 } 1398 1399 if (amdgpu_gtt_size != -1 && amdgpu_gtt_size < 32) { 1400 /* gtt size must be greater or equal to 32M */ 1401 dev_warn(adev->dev, "gtt size (%d) too small\n", 1402 amdgpu_gtt_size); 1403 amdgpu_gtt_size = -1; 1404 } 1405 1406 /* valid range is between 4 and 9 inclusive */ 1407 if (amdgpu_vm_fragment_size != -1 && 1408 (amdgpu_vm_fragment_size > 9 || amdgpu_vm_fragment_size < 4)) { 1409 dev_warn(adev->dev, "valid range is between 4 and 9\n"); 1410 amdgpu_vm_fragment_size = -1; 1411 } 1412 1413 if (amdgpu_sched_hw_submission < 2) { 1414 dev_warn(adev->dev, "sched hw submission jobs (%d) must be at least 2\n", 1415 amdgpu_sched_hw_submission); 1416 amdgpu_sched_hw_submission = 2; 1417 } else if (!is_power_of_2(amdgpu_sched_hw_submission)) { 1418 dev_warn(adev->dev, "sched hw submission jobs (%d) must be a power of 2\n", 1419 amdgpu_sched_hw_submission); 1420 amdgpu_sched_hw_submission = roundup_pow_of_two(amdgpu_sched_hw_submission); 1421 } 1422 1423 amdgpu_device_check_smu_prv_buffer_size(adev); 1424 1425 amdgpu_device_check_vm_size(adev); 1426 1427 amdgpu_device_check_block_size(adev); 1428 1429 adev->firmware.load_type = amdgpu_ucode_get_load_type(adev, amdgpu_fw_load_type); 1430 1431 amdgpu_gmc_tmz_set(adev); 1432 1433 amdgpu_gmc_noretry_set(adev); 1434 1435 return 0; 1436 } 1437 1438 /** 1439 * amdgpu_switcheroo_set_state - set switcheroo state 1440 * 1441 * @pdev: pci dev pointer 1442 * @state: vga_switcheroo state 1443 * 1444 * Callback for the switcheroo driver. Suspends or resumes the 1445 * the asics before or after it is powered up using ACPI methods. 1446 */ 1447 static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, 1448 enum vga_switcheroo_state state) 1449 { 1450 struct drm_device *dev = pci_get_drvdata(pdev); 1451 int r; 1452 1453 if (amdgpu_device_supports_px(dev) && state == VGA_SWITCHEROO_OFF) 1454 return; 1455 1456 if (state == VGA_SWITCHEROO_ON) { 1457 pr_info("switched on\n"); 1458 /* don't suspend or resume card normally */ 1459 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING; 1460 1461 pci_set_power_state(pdev, PCI_D0); 1462 amdgpu_device_load_pci_state(pdev); 1463 r = pci_enable_device(pdev); 1464 if (r) 1465 DRM_WARN("pci_enable_device failed (%d)\n", r); 1466 amdgpu_device_resume(dev, true); 1467 1468 dev->switch_power_state = DRM_SWITCH_POWER_ON; 1469 } else { 1470 pr_info("switched off\n"); 1471 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING; 1472 amdgpu_device_suspend(dev, true); 1473 amdgpu_device_cache_pci_state(pdev); 1474 /* Shut down the device */ 1475 pci_disable_device(pdev); 1476 pci_set_power_state(pdev, PCI_D3cold); 1477 dev->switch_power_state = DRM_SWITCH_POWER_OFF; 1478 } 1479 } 1480 1481 /** 1482 * amdgpu_switcheroo_can_switch - see if switcheroo state can change 1483 * 1484 * @pdev: pci dev pointer 1485 * 1486 * Callback for the switcheroo driver. Check of the switcheroo 1487 * state can be changed. 1488 * Returns true if the state can be changed, false if not. 1489 */ 1490 static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev) 1491 { 1492 struct drm_device *dev = pci_get_drvdata(pdev); 1493 1494 /* 1495 * FIXME: open_count is protected by drm_global_mutex but that would lead to 1496 * locking inversion with the driver load path. And the access here is 1497 * completely racy anyway. So don't bother with locking for now. 1498 */ 1499 return atomic_read(&dev->open_count) == 0; 1500 } 1501 1502 static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = { 1503 .set_gpu_state = amdgpu_switcheroo_set_state, 1504 .reprobe = NULL, 1505 .can_switch = amdgpu_switcheroo_can_switch, 1506 }; 1507 1508 /** 1509 * amdgpu_device_ip_set_clockgating_state - set the CG state 1510 * 1511 * @dev: amdgpu_device pointer 1512 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.) 1513 * @state: clockgating state (gate or ungate) 1514 * 1515 * Sets the requested clockgating state for all instances of 1516 * the hardware IP specified. 1517 * Returns the error code from the last instance. 1518 */ 1519 int amdgpu_device_ip_set_clockgating_state(void *dev, 1520 enum amd_ip_block_type block_type, 1521 enum amd_clockgating_state state) 1522 { 1523 struct amdgpu_device *adev = dev; 1524 int i, r = 0; 1525 1526 for (i = 0; i < adev->num_ip_blocks; i++) { 1527 if (!adev->ip_blocks[i].status.valid) 1528 continue; 1529 if (adev->ip_blocks[i].version->type != block_type) 1530 continue; 1531 if (!adev->ip_blocks[i].version->funcs->set_clockgating_state) 1532 continue; 1533 r = adev->ip_blocks[i].version->funcs->set_clockgating_state( 1534 (void *)adev, state); 1535 if (r) 1536 DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n", 1537 adev->ip_blocks[i].version->funcs->name, r); 1538 } 1539 return r; 1540 } 1541 1542 /** 1543 * amdgpu_device_ip_set_powergating_state - set the PG state 1544 * 1545 * @dev: amdgpu_device pointer 1546 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.) 1547 * @state: powergating state (gate or ungate) 1548 * 1549 * Sets the requested powergating state for all instances of 1550 * the hardware IP specified. 1551 * Returns the error code from the last instance. 1552 */ 1553 int amdgpu_device_ip_set_powergating_state(void *dev, 1554 enum amd_ip_block_type block_type, 1555 enum amd_powergating_state state) 1556 { 1557 struct amdgpu_device *adev = dev; 1558 int i, r = 0; 1559 1560 for (i = 0; i < adev->num_ip_blocks; i++) { 1561 if (!adev->ip_blocks[i].status.valid) 1562 continue; 1563 if (adev->ip_blocks[i].version->type != block_type) 1564 continue; 1565 if (!adev->ip_blocks[i].version->funcs->set_powergating_state) 1566 continue; 1567 r = adev->ip_blocks[i].version->funcs->set_powergating_state( 1568 (void *)adev, state); 1569 if (r) 1570 DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n", 1571 adev->ip_blocks[i].version->funcs->name, r); 1572 } 1573 return r; 1574 } 1575 1576 /** 1577 * amdgpu_device_ip_get_clockgating_state - get the CG state 1578 * 1579 * @adev: amdgpu_device pointer 1580 * @flags: clockgating feature flags 1581 * 1582 * Walks the list of IPs on the device and updates the clockgating 1583 * flags for each IP. 1584 * Updates @flags with the feature flags for each hardware IP where 1585 * clockgating is enabled. 1586 */ 1587 void amdgpu_device_ip_get_clockgating_state(struct amdgpu_device *adev, 1588 u32 *flags) 1589 { 1590 int i; 1591 1592 for (i = 0; i < adev->num_ip_blocks; i++) { 1593 if (!adev->ip_blocks[i].status.valid) 1594 continue; 1595 if (adev->ip_blocks[i].version->funcs->get_clockgating_state) 1596 adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags); 1597 } 1598 } 1599 1600 /** 1601 * amdgpu_device_ip_wait_for_idle - wait for idle 1602 * 1603 * @adev: amdgpu_device pointer 1604 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.) 1605 * 1606 * Waits for the request hardware IP to be idle. 1607 * Returns 0 for success or a negative error code on failure. 1608 */ 1609 int amdgpu_device_ip_wait_for_idle(struct amdgpu_device *adev, 1610 enum amd_ip_block_type block_type) 1611 { 1612 int i, r; 1613 1614 for (i = 0; i < adev->num_ip_blocks; i++) { 1615 if (!adev->ip_blocks[i].status.valid) 1616 continue; 1617 if (adev->ip_blocks[i].version->type == block_type) { 1618 r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev); 1619 if (r) 1620 return r; 1621 break; 1622 } 1623 } 1624 return 0; 1625 1626 } 1627 1628 /** 1629 * amdgpu_device_ip_is_idle - is the hardware IP idle 1630 * 1631 * @adev: amdgpu_device pointer 1632 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.) 1633 * 1634 * Check if the hardware IP is idle or not. 1635 * Returns true if it the IP is idle, false if not. 1636 */ 1637 bool amdgpu_device_ip_is_idle(struct amdgpu_device *adev, 1638 enum amd_ip_block_type block_type) 1639 { 1640 int i; 1641 1642 for (i = 0; i < adev->num_ip_blocks; i++) { 1643 if (!adev->ip_blocks[i].status.valid) 1644 continue; 1645 if (adev->ip_blocks[i].version->type == block_type) 1646 return adev->ip_blocks[i].version->funcs->is_idle((void *)adev); 1647 } 1648 return true; 1649 1650 } 1651 1652 /** 1653 * amdgpu_device_ip_get_ip_block - get a hw IP pointer 1654 * 1655 * @adev: amdgpu_device pointer 1656 * @type: Type of hardware IP (SMU, GFX, UVD, etc.) 1657 * 1658 * Returns a pointer to the hardware IP block structure 1659 * if it exists for the asic, otherwise NULL. 1660 */ 1661 struct amdgpu_ip_block * 1662 amdgpu_device_ip_get_ip_block(struct amdgpu_device *adev, 1663 enum amd_ip_block_type type) 1664 { 1665 int i; 1666 1667 for (i = 0; i < adev->num_ip_blocks; i++) 1668 if (adev->ip_blocks[i].version->type == type) 1669 return &adev->ip_blocks[i]; 1670 1671 return NULL; 1672 } 1673 1674 /** 1675 * amdgpu_device_ip_block_version_cmp 1676 * 1677 * @adev: amdgpu_device pointer 1678 * @type: enum amd_ip_block_type 1679 * @major: major version 1680 * @minor: minor version 1681 * 1682 * return 0 if equal or greater 1683 * return 1 if smaller or the ip_block doesn't exist 1684 */ 1685 int amdgpu_device_ip_block_version_cmp(struct amdgpu_device *adev, 1686 enum amd_ip_block_type type, 1687 u32 major, u32 minor) 1688 { 1689 struct amdgpu_ip_block *ip_block = amdgpu_device_ip_get_ip_block(adev, type); 1690 1691 if (ip_block && ((ip_block->version->major > major) || 1692 ((ip_block->version->major == major) && 1693 (ip_block->version->minor >= minor)))) 1694 return 0; 1695 1696 return 1; 1697 } 1698 1699 /** 1700 * amdgpu_device_ip_block_add 1701 * 1702 * @adev: amdgpu_device pointer 1703 * @ip_block_version: pointer to the IP to add 1704 * 1705 * Adds the IP block driver information to the collection of IPs 1706 * on the asic. 1707 */ 1708 int amdgpu_device_ip_block_add(struct amdgpu_device *adev, 1709 const struct amdgpu_ip_block_version *ip_block_version) 1710 { 1711 if (!ip_block_version) 1712 return -EINVAL; 1713 1714 switch (ip_block_version->type) { 1715 case AMD_IP_BLOCK_TYPE_VCN: 1716 if (adev->harvest_ip_mask & AMD_HARVEST_IP_VCN_MASK) 1717 return 0; 1718 break; 1719 case AMD_IP_BLOCK_TYPE_JPEG: 1720 if (adev->harvest_ip_mask & AMD_HARVEST_IP_JPEG_MASK) 1721 return 0; 1722 break; 1723 default: 1724 break; 1725 } 1726 1727 DRM_INFO("add ip block number %d <%s>\n", adev->num_ip_blocks, 1728 ip_block_version->funcs->name); 1729 1730 adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version; 1731 1732 return 0; 1733 } 1734 1735 /** 1736 * amdgpu_device_enable_virtual_display - enable virtual display feature 1737 * 1738 * @adev: amdgpu_device pointer 1739 * 1740 * Enabled the virtual display feature if the user has enabled it via 1741 * the module parameter virtual_display. This feature provides a virtual 1742 * display hardware on headless boards or in virtualized environments. 1743 * This function parses and validates the configuration string specified by 1744 * the user and configues the virtual display configuration (number of 1745 * virtual connectors, crtcs, etc.) specified. 1746 */ 1747 static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev) 1748 { 1749 adev->enable_virtual_display = false; 1750 1751 if (amdgpu_virtual_display) { 1752 const char *pci_address_name = pci_name(adev->pdev); 1753 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname; 1754 1755 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL); 1756 pciaddstr_tmp = pciaddstr; 1757 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) { 1758 pciaddname = strsep(&pciaddname_tmp, ","); 1759 if (!strcmp("all", pciaddname) 1760 || !strcmp(pci_address_name, pciaddname)) { 1761 long num_crtc; 1762 int res = -1; 1763 1764 adev->enable_virtual_display = true; 1765 1766 if (pciaddname_tmp) 1767 res = kstrtol(pciaddname_tmp, 10, 1768 &num_crtc); 1769 1770 if (!res) { 1771 if (num_crtc < 1) 1772 num_crtc = 1; 1773 if (num_crtc > 6) 1774 num_crtc = 6; 1775 adev->mode_info.num_crtc = num_crtc; 1776 } else { 1777 adev->mode_info.num_crtc = 1; 1778 } 1779 break; 1780 } 1781 } 1782 1783 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n", 1784 amdgpu_virtual_display, pci_address_name, 1785 adev->enable_virtual_display, adev->mode_info.num_crtc); 1786 1787 kfree(pciaddstr); 1788 } 1789 } 1790 1791 /** 1792 * amdgpu_device_parse_gpu_info_fw - parse gpu info firmware 1793 * 1794 * @adev: amdgpu_device pointer 1795 * 1796 * Parses the asic configuration parameters specified in the gpu info 1797 * firmware and makes them availale to the driver for use in configuring 1798 * the asic. 1799 * Returns 0 on success, -EINVAL on failure. 1800 */ 1801 static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev) 1802 { 1803 const char *chip_name; 1804 char fw_name[40]; 1805 int err; 1806 const struct gpu_info_firmware_header_v1_0 *hdr; 1807 1808 adev->firmware.gpu_info_fw = NULL; 1809 1810 if (adev->mman.discovery_bin) { 1811 amdgpu_discovery_get_gfx_info(adev); 1812 1813 /* 1814 * FIXME: The bounding box is still needed by Navi12, so 1815 * temporarily read it from gpu_info firmware. Should be droped 1816 * when DAL no longer needs it. 1817 */ 1818 if (adev->asic_type != CHIP_NAVI12) 1819 return 0; 1820 } 1821 1822 switch (adev->asic_type) { 1823 #ifdef CONFIG_DRM_AMDGPU_SI 1824 case CHIP_VERDE: 1825 case CHIP_TAHITI: 1826 case CHIP_PITCAIRN: 1827 case CHIP_OLAND: 1828 case CHIP_HAINAN: 1829 #endif 1830 #ifdef CONFIG_DRM_AMDGPU_CIK 1831 case CHIP_BONAIRE: 1832 case CHIP_HAWAII: 1833 case CHIP_KAVERI: 1834 case CHIP_KABINI: 1835 case CHIP_MULLINS: 1836 #endif 1837 case CHIP_TOPAZ: 1838 case CHIP_TONGA: 1839 case CHIP_FIJI: 1840 case CHIP_POLARIS10: 1841 case CHIP_POLARIS11: 1842 case CHIP_POLARIS12: 1843 case CHIP_VEGAM: 1844 case CHIP_CARRIZO: 1845 case CHIP_STONEY: 1846 case CHIP_VEGA20: 1847 case CHIP_ALDEBARAN: 1848 case CHIP_SIENNA_CICHLID: 1849 case CHIP_NAVY_FLOUNDER: 1850 case CHIP_DIMGREY_CAVEFISH: 1851 case CHIP_BEIGE_GOBY: 1852 default: 1853 return 0; 1854 case CHIP_VEGA10: 1855 chip_name = "vega10"; 1856 break; 1857 case CHIP_VEGA12: 1858 chip_name = "vega12"; 1859 break; 1860 case CHIP_RAVEN: 1861 if (adev->apu_flags & AMD_APU_IS_RAVEN2) 1862 chip_name = "raven2"; 1863 else if (adev->apu_flags & AMD_APU_IS_PICASSO) 1864 chip_name = "picasso"; 1865 else 1866 chip_name = "raven"; 1867 break; 1868 case CHIP_ARCTURUS: 1869 chip_name = "arcturus"; 1870 break; 1871 case CHIP_RENOIR: 1872 if (adev->apu_flags & AMD_APU_IS_RENOIR) 1873 chip_name = "renoir"; 1874 else 1875 chip_name = "green_sardine"; 1876 break; 1877 case CHIP_NAVI10: 1878 chip_name = "navi10"; 1879 break; 1880 case CHIP_NAVI14: 1881 chip_name = "navi14"; 1882 break; 1883 case CHIP_NAVI12: 1884 chip_name = "navi12"; 1885 break; 1886 case CHIP_VANGOGH: 1887 chip_name = "vangogh"; 1888 break; 1889 case CHIP_YELLOW_CARP: 1890 chip_name = "yellow_carp"; 1891 break; 1892 } 1893 1894 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name); 1895 err = request_firmware(&adev->firmware.gpu_info_fw, fw_name, adev->dev); 1896 if (err) { 1897 dev_err(adev->dev, 1898 "Failed to load gpu_info firmware \"%s\"\n", 1899 fw_name); 1900 goto out; 1901 } 1902 err = amdgpu_ucode_validate(adev->firmware.gpu_info_fw); 1903 if (err) { 1904 dev_err(adev->dev, 1905 "Failed to validate gpu_info firmware \"%s\"\n", 1906 fw_name); 1907 goto out; 1908 } 1909 1910 hdr = (const struct gpu_info_firmware_header_v1_0 *)adev->firmware.gpu_info_fw->data; 1911 amdgpu_ucode_print_gpu_info_hdr(&hdr->header); 1912 1913 switch (hdr->version_major) { 1914 case 1: 1915 { 1916 const struct gpu_info_firmware_v1_0 *gpu_info_fw = 1917 (const struct gpu_info_firmware_v1_0 *)(adev->firmware.gpu_info_fw->data + 1918 le32_to_cpu(hdr->header.ucode_array_offset_bytes)); 1919 1920 /* 1921 * Should be droped when DAL no longer needs it. 1922 */ 1923 if (adev->asic_type == CHIP_NAVI12) 1924 goto parse_soc_bounding_box; 1925 1926 adev->gfx.config.max_shader_engines = le32_to_cpu(gpu_info_fw->gc_num_se); 1927 adev->gfx.config.max_cu_per_sh = le32_to_cpu(gpu_info_fw->gc_num_cu_per_sh); 1928 adev->gfx.config.max_sh_per_se = le32_to_cpu(gpu_info_fw->gc_num_sh_per_se); 1929 adev->gfx.config.max_backends_per_se = le32_to_cpu(gpu_info_fw->gc_num_rb_per_se); 1930 adev->gfx.config.max_texture_channel_caches = 1931 le32_to_cpu(gpu_info_fw->gc_num_tccs); 1932 adev->gfx.config.max_gprs = le32_to_cpu(gpu_info_fw->gc_num_gprs); 1933 adev->gfx.config.max_gs_threads = le32_to_cpu(gpu_info_fw->gc_num_max_gs_thds); 1934 adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gpu_info_fw->gc_gs_table_depth); 1935 adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gpu_info_fw->gc_gsprim_buff_depth); 1936 adev->gfx.config.double_offchip_lds_buf = 1937 le32_to_cpu(gpu_info_fw->gc_double_offchip_lds_buffer); 1938 adev->gfx.cu_info.wave_front_size = le32_to_cpu(gpu_info_fw->gc_wave_size); 1939 adev->gfx.cu_info.max_waves_per_simd = 1940 le32_to_cpu(gpu_info_fw->gc_max_waves_per_simd); 1941 adev->gfx.cu_info.max_scratch_slots_per_cu = 1942 le32_to_cpu(gpu_info_fw->gc_max_scratch_slots_per_cu); 1943 adev->gfx.cu_info.lds_size = le32_to_cpu(gpu_info_fw->gc_lds_size); 1944 if (hdr->version_minor >= 1) { 1945 const struct gpu_info_firmware_v1_1 *gpu_info_fw = 1946 (const struct gpu_info_firmware_v1_1 *)(adev->firmware.gpu_info_fw->data + 1947 le32_to_cpu(hdr->header.ucode_array_offset_bytes)); 1948 adev->gfx.config.num_sc_per_sh = 1949 le32_to_cpu(gpu_info_fw->num_sc_per_sh); 1950 adev->gfx.config.num_packer_per_sc = 1951 le32_to_cpu(gpu_info_fw->num_packer_per_sc); 1952 } 1953 1954 parse_soc_bounding_box: 1955 /* 1956 * soc bounding box info is not integrated in disocovery table, 1957 * we always need to parse it from gpu info firmware if needed. 1958 */ 1959 if (hdr->version_minor == 2) { 1960 const struct gpu_info_firmware_v1_2 *gpu_info_fw = 1961 (const struct gpu_info_firmware_v1_2 *)(adev->firmware.gpu_info_fw->data + 1962 le32_to_cpu(hdr->header.ucode_array_offset_bytes)); 1963 adev->dm.soc_bounding_box = &gpu_info_fw->soc_bounding_box; 1964 } 1965 break; 1966 } 1967 default: 1968 dev_err(adev->dev, 1969 "Unsupported gpu_info table %d\n", hdr->header.ucode_version); 1970 err = -EINVAL; 1971 goto out; 1972 } 1973 out: 1974 return err; 1975 } 1976 1977 /** 1978 * amdgpu_device_ip_early_init - run early init for hardware IPs 1979 * 1980 * @adev: amdgpu_device pointer 1981 * 1982 * Early initialization pass for hardware IPs. The hardware IPs that make 1983 * up each asic are discovered each IP's early_init callback is run. This 1984 * is the first stage in initializing the asic. 1985 * Returns 0 on success, negative error code on failure. 1986 */ 1987 static int amdgpu_device_ip_early_init(struct amdgpu_device *adev) 1988 { 1989 int i, r; 1990 1991 amdgpu_device_enable_virtual_display(adev); 1992 1993 if (amdgpu_sriov_vf(adev)) { 1994 r = amdgpu_virt_request_full_gpu(adev, true); 1995 if (r) 1996 return r; 1997 } 1998 1999 switch (adev->asic_type) { 2000 #ifdef CONFIG_DRM_AMDGPU_SI 2001 case CHIP_VERDE: 2002 case CHIP_TAHITI: 2003 case CHIP_PITCAIRN: 2004 case CHIP_OLAND: 2005 case CHIP_HAINAN: 2006 adev->family = AMDGPU_FAMILY_SI; 2007 r = si_set_ip_blocks(adev); 2008 if (r) 2009 return r; 2010 break; 2011 #endif 2012 #ifdef CONFIG_DRM_AMDGPU_CIK 2013 case CHIP_BONAIRE: 2014 case CHIP_HAWAII: 2015 case CHIP_KAVERI: 2016 case CHIP_KABINI: 2017 case CHIP_MULLINS: 2018 if (adev->flags & AMD_IS_APU) 2019 adev->family = AMDGPU_FAMILY_KV; 2020 else 2021 adev->family = AMDGPU_FAMILY_CI; 2022 2023 r = cik_set_ip_blocks(adev); 2024 if (r) 2025 return r; 2026 break; 2027 #endif 2028 case CHIP_TOPAZ: 2029 case CHIP_TONGA: 2030 case CHIP_FIJI: 2031 case CHIP_POLARIS10: 2032 case CHIP_POLARIS11: 2033 case CHIP_POLARIS12: 2034 case CHIP_VEGAM: 2035 case CHIP_CARRIZO: 2036 case CHIP_STONEY: 2037 if (adev->flags & AMD_IS_APU) 2038 adev->family = AMDGPU_FAMILY_CZ; 2039 else 2040 adev->family = AMDGPU_FAMILY_VI; 2041 2042 r = vi_set_ip_blocks(adev); 2043 if (r) 2044 return r; 2045 break; 2046 case CHIP_VEGA10: 2047 case CHIP_VEGA12: 2048 case CHIP_VEGA20: 2049 case CHIP_RAVEN: 2050 case CHIP_ARCTURUS: 2051 case CHIP_RENOIR: 2052 case CHIP_ALDEBARAN: 2053 if (adev->flags & AMD_IS_APU) 2054 adev->family = AMDGPU_FAMILY_RV; 2055 else 2056 adev->family = AMDGPU_FAMILY_AI; 2057 2058 r = soc15_set_ip_blocks(adev); 2059 if (r) 2060 return r; 2061 break; 2062 case CHIP_NAVI10: 2063 case CHIP_NAVI14: 2064 case CHIP_NAVI12: 2065 case CHIP_SIENNA_CICHLID: 2066 case CHIP_NAVY_FLOUNDER: 2067 case CHIP_DIMGREY_CAVEFISH: 2068 case CHIP_BEIGE_GOBY: 2069 case CHIP_VANGOGH: 2070 case CHIP_YELLOW_CARP: 2071 if (adev->asic_type == CHIP_VANGOGH) 2072 adev->family = AMDGPU_FAMILY_VGH; 2073 else if (adev->asic_type == CHIP_YELLOW_CARP) 2074 adev->family = AMDGPU_FAMILY_YC; 2075 else 2076 adev->family = AMDGPU_FAMILY_NV; 2077 2078 r = nv_set_ip_blocks(adev); 2079 if (r) 2080 return r; 2081 break; 2082 default: 2083 /* FIXME: not supported yet */ 2084 return -EINVAL; 2085 } 2086 2087 amdgpu_amdkfd_device_probe(adev); 2088 2089 adev->pm.pp_feature = amdgpu_pp_feature_mask; 2090 if (amdgpu_sriov_vf(adev) || sched_policy == KFD_SCHED_POLICY_NO_HWS) 2091 adev->pm.pp_feature &= ~PP_GFXOFF_MASK; 2092 if (amdgpu_sriov_vf(adev) && adev->asic_type == CHIP_SIENNA_CICHLID) 2093 adev->pm.pp_feature &= ~PP_OVERDRIVE_MASK; 2094 2095 for (i = 0; i < adev->num_ip_blocks; i++) { 2096 if ((amdgpu_ip_block_mask & (1 << i)) == 0) { 2097 DRM_ERROR("disabled ip block: %d <%s>\n", 2098 i, adev->ip_blocks[i].version->funcs->name); 2099 adev->ip_blocks[i].status.valid = false; 2100 } else { 2101 if (adev->ip_blocks[i].version->funcs->early_init) { 2102 r = adev->ip_blocks[i].version->funcs->early_init((void *)adev); 2103 if (r == -ENOENT) { 2104 adev->ip_blocks[i].status.valid = false; 2105 } else if (r) { 2106 DRM_ERROR("early_init of IP block <%s> failed %d\n", 2107 adev->ip_blocks[i].version->funcs->name, r); 2108 return r; 2109 } else { 2110 adev->ip_blocks[i].status.valid = true; 2111 } 2112 } else { 2113 adev->ip_blocks[i].status.valid = true; 2114 } 2115 } 2116 /* get the vbios after the asic_funcs are set up */ 2117 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON) { 2118 r = amdgpu_device_parse_gpu_info_fw(adev); 2119 if (r) 2120 return r; 2121 2122 /* Read BIOS */ 2123 if (!amdgpu_get_bios(adev)) 2124 return -EINVAL; 2125 2126 r = amdgpu_atombios_init(adev); 2127 if (r) { 2128 dev_err(adev->dev, "amdgpu_atombios_init failed\n"); 2129 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_INIT_FAIL, 0, 0); 2130 return r; 2131 } 2132 2133 /*get pf2vf msg info at it's earliest time*/ 2134 if (amdgpu_sriov_vf(adev)) 2135 amdgpu_virt_init_data_exchange(adev); 2136 2137 } 2138 } 2139 2140 adev->cg_flags &= amdgpu_cg_mask; 2141 adev->pg_flags &= amdgpu_pg_mask; 2142 2143 return 0; 2144 } 2145 2146 static int amdgpu_device_ip_hw_init_phase1(struct amdgpu_device *adev) 2147 { 2148 int i, r; 2149 2150 for (i = 0; i < adev->num_ip_blocks; i++) { 2151 if (!adev->ip_blocks[i].status.sw) 2152 continue; 2153 if (adev->ip_blocks[i].status.hw) 2154 continue; 2155 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON || 2156 (amdgpu_sriov_vf(adev) && (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP)) || 2157 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH) { 2158 r = adev->ip_blocks[i].version->funcs->hw_init(adev); 2159 if (r) { 2160 DRM_ERROR("hw_init of IP block <%s> failed %d\n", 2161 adev->ip_blocks[i].version->funcs->name, r); 2162 return r; 2163 } 2164 adev->ip_blocks[i].status.hw = true; 2165 } 2166 } 2167 2168 return 0; 2169 } 2170 2171 static int amdgpu_device_ip_hw_init_phase2(struct amdgpu_device *adev) 2172 { 2173 int i, r; 2174 2175 for (i = 0; i < adev->num_ip_blocks; i++) { 2176 if (!adev->ip_blocks[i].status.sw) 2177 continue; 2178 if (adev->ip_blocks[i].status.hw) 2179 continue; 2180 r = adev->ip_blocks[i].version->funcs->hw_init(adev); 2181 if (r) { 2182 DRM_ERROR("hw_init of IP block <%s> failed %d\n", 2183 adev->ip_blocks[i].version->funcs->name, r); 2184 return r; 2185 } 2186 adev->ip_blocks[i].status.hw = true; 2187 } 2188 2189 return 0; 2190 } 2191 2192 static int amdgpu_device_fw_loading(struct amdgpu_device *adev) 2193 { 2194 int r = 0; 2195 int i; 2196 uint32_t smu_version; 2197 2198 if (adev->asic_type >= CHIP_VEGA10) { 2199 for (i = 0; i < adev->num_ip_blocks; i++) { 2200 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_PSP) 2201 continue; 2202 2203 if (!adev->ip_blocks[i].status.sw) 2204 continue; 2205 2206 /* no need to do the fw loading again if already done*/ 2207 if (adev->ip_blocks[i].status.hw == true) 2208 break; 2209 2210 if (amdgpu_in_reset(adev) || adev->in_suspend) { 2211 r = adev->ip_blocks[i].version->funcs->resume(adev); 2212 if (r) { 2213 DRM_ERROR("resume of IP block <%s> failed %d\n", 2214 adev->ip_blocks[i].version->funcs->name, r); 2215 return r; 2216 } 2217 } else { 2218 r = adev->ip_blocks[i].version->funcs->hw_init(adev); 2219 if (r) { 2220 DRM_ERROR("hw_init of IP block <%s> failed %d\n", 2221 adev->ip_blocks[i].version->funcs->name, r); 2222 return r; 2223 } 2224 } 2225 2226 adev->ip_blocks[i].status.hw = true; 2227 break; 2228 } 2229 } 2230 2231 if (!amdgpu_sriov_vf(adev) || adev->asic_type == CHIP_TONGA) 2232 r = amdgpu_pm_load_smu_firmware(adev, &smu_version); 2233 2234 return r; 2235 } 2236 2237 /** 2238 * amdgpu_device_ip_init - run init for hardware IPs 2239 * 2240 * @adev: amdgpu_device pointer 2241 * 2242 * Main initialization pass for hardware IPs. The list of all the hardware 2243 * IPs that make up the asic is walked and the sw_init and hw_init callbacks 2244 * are run. sw_init initializes the software state associated with each IP 2245 * and hw_init initializes the hardware associated with each IP. 2246 * Returns 0 on success, negative error code on failure. 2247 */ 2248 static int amdgpu_device_ip_init(struct amdgpu_device *adev) 2249 { 2250 int i, r; 2251 2252 r = amdgpu_ras_init(adev); 2253 if (r) 2254 return r; 2255 2256 for (i = 0; i < adev->num_ip_blocks; i++) { 2257 if (!adev->ip_blocks[i].status.valid) 2258 continue; 2259 r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev); 2260 if (r) { 2261 DRM_ERROR("sw_init of IP block <%s> failed %d\n", 2262 adev->ip_blocks[i].version->funcs->name, r); 2263 goto init_failed; 2264 } 2265 adev->ip_blocks[i].status.sw = true; 2266 2267 /* need to do gmc hw init early so we can allocate gpu mem */ 2268 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) { 2269 r = amdgpu_device_vram_scratch_init(adev); 2270 if (r) { 2271 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r); 2272 goto init_failed; 2273 } 2274 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev); 2275 if (r) { 2276 DRM_ERROR("hw_init %d failed %d\n", i, r); 2277 goto init_failed; 2278 } 2279 r = amdgpu_device_wb_init(adev); 2280 if (r) { 2281 DRM_ERROR("amdgpu_device_wb_init failed %d\n", r); 2282 goto init_failed; 2283 } 2284 adev->ip_blocks[i].status.hw = true; 2285 2286 /* right after GMC hw init, we create CSA */ 2287 if (amdgpu_mcbp || amdgpu_sriov_vf(adev)) { 2288 r = amdgpu_allocate_static_csa(adev, &adev->virt.csa_obj, 2289 AMDGPU_GEM_DOMAIN_VRAM, 2290 AMDGPU_CSA_SIZE); 2291 if (r) { 2292 DRM_ERROR("allocate CSA failed %d\n", r); 2293 goto init_failed; 2294 } 2295 } 2296 } 2297 } 2298 2299 if (amdgpu_sriov_vf(adev)) 2300 amdgpu_virt_init_data_exchange(adev); 2301 2302 r = amdgpu_ib_pool_init(adev); 2303 if (r) { 2304 dev_err(adev->dev, "IB initialization failed (%d).\n", r); 2305 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r); 2306 goto init_failed; 2307 } 2308 2309 r = amdgpu_ucode_create_bo(adev); /* create ucode bo when sw_init complete*/ 2310 if (r) 2311 goto init_failed; 2312 2313 r = amdgpu_device_ip_hw_init_phase1(adev); 2314 if (r) 2315 goto init_failed; 2316 2317 r = amdgpu_device_fw_loading(adev); 2318 if (r) 2319 goto init_failed; 2320 2321 r = amdgpu_device_ip_hw_init_phase2(adev); 2322 if (r) 2323 goto init_failed; 2324 2325 /* 2326 * retired pages will be loaded from eeprom and reserved here, 2327 * it should be called after amdgpu_device_ip_hw_init_phase2 since 2328 * for some ASICs the RAS EEPROM code relies on SMU fully functioning 2329 * for I2C communication which only true at this point. 2330 * 2331 * amdgpu_ras_recovery_init may fail, but the upper only cares the 2332 * failure from bad gpu situation and stop amdgpu init process 2333 * accordingly. For other failed cases, it will still release all 2334 * the resource and print error message, rather than returning one 2335 * negative value to upper level. 2336 * 2337 * Note: theoretically, this should be called before all vram allocations 2338 * to protect retired page from abusing 2339 */ 2340 r = amdgpu_ras_recovery_init(adev); 2341 if (r) 2342 goto init_failed; 2343 2344 if (adev->gmc.xgmi.num_physical_nodes > 1) 2345 amdgpu_xgmi_add_device(adev); 2346 2347 /* Don't init kfd if whole hive need to be reset during init */ 2348 if (!adev->gmc.xgmi.pending_reset) 2349 amdgpu_amdkfd_device_init(adev); 2350 2351 amdgpu_fru_get_product_info(adev); 2352 2353 init_failed: 2354 if (amdgpu_sriov_vf(adev)) 2355 amdgpu_virt_release_full_gpu(adev, true); 2356 2357 return r; 2358 } 2359 2360 /** 2361 * amdgpu_device_fill_reset_magic - writes reset magic to gart pointer 2362 * 2363 * @adev: amdgpu_device pointer 2364 * 2365 * Writes a reset magic value to the gart pointer in VRAM. The driver calls 2366 * this function before a GPU reset. If the value is retained after a 2367 * GPU reset, VRAM has not been lost. Some GPU resets may destry VRAM contents. 2368 */ 2369 static void amdgpu_device_fill_reset_magic(struct amdgpu_device *adev) 2370 { 2371 memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM); 2372 } 2373 2374 /** 2375 * amdgpu_device_check_vram_lost - check if vram is valid 2376 * 2377 * @adev: amdgpu_device pointer 2378 * 2379 * Checks the reset magic value written to the gart pointer in VRAM. 2380 * The driver calls this after a GPU reset to see if the contents of 2381 * VRAM is lost or now. 2382 * returns true if vram is lost, false if not. 2383 */ 2384 static bool amdgpu_device_check_vram_lost(struct amdgpu_device *adev) 2385 { 2386 if (memcmp(adev->gart.ptr, adev->reset_magic, 2387 AMDGPU_RESET_MAGIC_NUM)) 2388 return true; 2389 2390 if (!amdgpu_in_reset(adev)) 2391 return false; 2392 2393 /* 2394 * For all ASICs with baco/mode1 reset, the VRAM is 2395 * always assumed to be lost. 2396 */ 2397 switch (amdgpu_asic_reset_method(adev)) { 2398 case AMD_RESET_METHOD_BACO: 2399 case AMD_RESET_METHOD_MODE1: 2400 return true; 2401 default: 2402 return false; 2403 } 2404 } 2405 2406 /** 2407 * amdgpu_device_set_cg_state - set clockgating for amdgpu device 2408 * 2409 * @adev: amdgpu_device pointer 2410 * @state: clockgating state (gate or ungate) 2411 * 2412 * The list of all the hardware IPs that make up the asic is walked and the 2413 * set_clockgating_state callbacks are run. 2414 * Late initialization pass enabling clockgating for hardware IPs. 2415 * Fini or suspend, pass disabling clockgating for hardware IPs. 2416 * Returns 0 on success, negative error code on failure. 2417 */ 2418 2419 int amdgpu_device_set_cg_state(struct amdgpu_device *adev, 2420 enum amd_clockgating_state state) 2421 { 2422 int i, j, r; 2423 2424 if (amdgpu_emu_mode == 1) 2425 return 0; 2426 2427 for (j = 0; j < adev->num_ip_blocks; j++) { 2428 i = state == AMD_CG_STATE_GATE ? j : adev->num_ip_blocks - j - 1; 2429 if (!adev->ip_blocks[i].status.late_initialized) 2430 continue; 2431 /* skip CG for GFX on S0ix */ 2432 if (adev->in_s0ix && 2433 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GFX) 2434 continue; 2435 /* skip CG for VCE/UVD, it's handled specially */ 2436 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD && 2437 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE && 2438 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCN && 2439 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_JPEG && 2440 adev->ip_blocks[i].version->funcs->set_clockgating_state) { 2441 /* enable clockgating to save power */ 2442 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev, 2443 state); 2444 if (r) { 2445 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n", 2446 adev->ip_blocks[i].version->funcs->name, r); 2447 return r; 2448 } 2449 } 2450 } 2451 2452 return 0; 2453 } 2454 2455 int amdgpu_device_set_pg_state(struct amdgpu_device *adev, 2456 enum amd_powergating_state state) 2457 { 2458 int i, j, r; 2459 2460 if (amdgpu_emu_mode == 1) 2461 return 0; 2462 2463 for (j = 0; j < adev->num_ip_blocks; j++) { 2464 i = state == AMD_PG_STATE_GATE ? j : adev->num_ip_blocks - j - 1; 2465 if (!adev->ip_blocks[i].status.late_initialized) 2466 continue; 2467 /* skip PG for GFX on S0ix */ 2468 if (adev->in_s0ix && 2469 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GFX) 2470 continue; 2471 /* skip CG for VCE/UVD, it's handled specially */ 2472 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD && 2473 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE && 2474 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCN && 2475 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_JPEG && 2476 adev->ip_blocks[i].version->funcs->set_powergating_state) { 2477 /* enable powergating to save power */ 2478 r = adev->ip_blocks[i].version->funcs->set_powergating_state((void *)adev, 2479 state); 2480 if (r) { 2481 DRM_ERROR("set_powergating_state(gate) of IP block <%s> failed %d\n", 2482 adev->ip_blocks[i].version->funcs->name, r); 2483 return r; 2484 } 2485 } 2486 } 2487 return 0; 2488 } 2489 2490 static int amdgpu_device_enable_mgpu_fan_boost(void) 2491 { 2492 struct amdgpu_gpu_instance *gpu_ins; 2493 struct amdgpu_device *adev; 2494 int i, ret = 0; 2495 2496 mutex_lock(&mgpu_info.mutex); 2497 2498 /* 2499 * MGPU fan boost feature should be enabled 2500 * only when there are two or more dGPUs in 2501 * the system 2502 */ 2503 if (mgpu_info.num_dgpu < 2) 2504 goto out; 2505 2506 for (i = 0; i < mgpu_info.num_dgpu; i++) { 2507 gpu_ins = &(mgpu_info.gpu_ins[i]); 2508 adev = gpu_ins->adev; 2509 if (!(adev->flags & AMD_IS_APU) && 2510 !gpu_ins->mgpu_fan_enabled) { 2511 ret = amdgpu_dpm_enable_mgpu_fan_boost(adev); 2512 if (ret) 2513 break; 2514 2515 gpu_ins->mgpu_fan_enabled = 1; 2516 } 2517 } 2518 2519 out: 2520 mutex_unlock(&mgpu_info.mutex); 2521 2522 return ret; 2523 } 2524 2525 /** 2526 * amdgpu_device_ip_late_init - run late init for hardware IPs 2527 * 2528 * @adev: amdgpu_device pointer 2529 * 2530 * Late initialization pass for hardware IPs. The list of all the hardware 2531 * IPs that make up the asic is walked and the late_init callbacks are run. 2532 * late_init covers any special initialization that an IP requires 2533 * after all of the have been initialized or something that needs to happen 2534 * late in the init process. 2535 * Returns 0 on success, negative error code on failure. 2536 */ 2537 static int amdgpu_device_ip_late_init(struct amdgpu_device *adev) 2538 { 2539 struct amdgpu_gpu_instance *gpu_instance; 2540 int i = 0, r; 2541 2542 for (i = 0; i < adev->num_ip_blocks; i++) { 2543 if (!adev->ip_blocks[i].status.hw) 2544 continue; 2545 if (adev->ip_blocks[i].version->funcs->late_init) { 2546 r = adev->ip_blocks[i].version->funcs->late_init((void *)adev); 2547 if (r) { 2548 DRM_ERROR("late_init of IP block <%s> failed %d\n", 2549 adev->ip_blocks[i].version->funcs->name, r); 2550 return r; 2551 } 2552 } 2553 adev->ip_blocks[i].status.late_initialized = true; 2554 } 2555 2556 amdgpu_ras_set_error_query_ready(adev, true); 2557 2558 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_GATE); 2559 amdgpu_device_set_pg_state(adev, AMD_PG_STATE_GATE); 2560 2561 amdgpu_device_fill_reset_magic(adev); 2562 2563 r = amdgpu_device_enable_mgpu_fan_boost(); 2564 if (r) 2565 DRM_ERROR("enable mgpu fan boost failed (%d).\n", r); 2566 2567 /* For XGMI + passthrough configuration on arcturus, enable light SBR */ 2568 if (adev->asic_type == CHIP_ARCTURUS && 2569 amdgpu_passthrough(adev) && 2570 adev->gmc.xgmi.num_physical_nodes > 1) 2571 smu_set_light_sbr(&adev->smu, true); 2572 2573 if (adev->gmc.xgmi.num_physical_nodes > 1) { 2574 mutex_lock(&mgpu_info.mutex); 2575 2576 /* 2577 * Reset device p-state to low as this was booted with high. 2578 * 2579 * This should be performed only after all devices from the same 2580 * hive get initialized. 2581 * 2582 * However, it's unknown how many device in the hive in advance. 2583 * As this is counted one by one during devices initializations. 2584 * 2585 * So, we wait for all XGMI interlinked devices initialized. 2586 * This may bring some delays as those devices may come from 2587 * different hives. But that should be OK. 2588 */ 2589 if (mgpu_info.num_dgpu == adev->gmc.xgmi.num_physical_nodes) { 2590 for (i = 0; i < mgpu_info.num_gpu; i++) { 2591 gpu_instance = &(mgpu_info.gpu_ins[i]); 2592 if (gpu_instance->adev->flags & AMD_IS_APU) 2593 continue; 2594 2595 r = amdgpu_xgmi_set_pstate(gpu_instance->adev, 2596 AMDGPU_XGMI_PSTATE_MIN); 2597 if (r) { 2598 DRM_ERROR("pstate setting failed (%d).\n", r); 2599 break; 2600 } 2601 } 2602 } 2603 2604 mutex_unlock(&mgpu_info.mutex); 2605 } 2606 2607 return 0; 2608 } 2609 2610 static int amdgpu_device_ip_fini_early(struct amdgpu_device *adev) 2611 { 2612 int i, r; 2613 2614 for (i = 0; i < adev->num_ip_blocks; i++) { 2615 if (!adev->ip_blocks[i].version->funcs->early_fini) 2616 continue; 2617 2618 r = adev->ip_blocks[i].version->funcs->early_fini((void *)adev); 2619 if (r) { 2620 DRM_DEBUG("early_fini of IP block <%s> failed %d\n", 2621 adev->ip_blocks[i].version->funcs->name, r); 2622 } 2623 } 2624 2625 amdgpu_amdkfd_suspend(adev, false); 2626 2627 amdgpu_device_set_pg_state(adev, AMD_PG_STATE_UNGATE); 2628 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_UNGATE); 2629 2630 /* need to disable SMC first */ 2631 for (i = 0; i < adev->num_ip_blocks; i++) { 2632 if (!adev->ip_blocks[i].status.hw) 2633 continue; 2634 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) { 2635 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev); 2636 /* XXX handle errors */ 2637 if (r) { 2638 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n", 2639 adev->ip_blocks[i].version->funcs->name, r); 2640 } 2641 adev->ip_blocks[i].status.hw = false; 2642 break; 2643 } 2644 } 2645 2646 for (i = adev->num_ip_blocks - 1; i >= 0; i--) { 2647 if (!adev->ip_blocks[i].status.hw) 2648 continue; 2649 2650 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev); 2651 /* XXX handle errors */ 2652 if (r) { 2653 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n", 2654 adev->ip_blocks[i].version->funcs->name, r); 2655 } 2656 2657 adev->ip_blocks[i].status.hw = false; 2658 } 2659 2660 return 0; 2661 } 2662 2663 /** 2664 * amdgpu_device_ip_fini - run fini for hardware IPs 2665 * 2666 * @adev: amdgpu_device pointer 2667 * 2668 * Main teardown pass for hardware IPs. The list of all the hardware 2669 * IPs that make up the asic is walked and the hw_fini and sw_fini callbacks 2670 * are run. hw_fini tears down the hardware associated with each IP 2671 * and sw_fini tears down any software state associated with each IP. 2672 * Returns 0 on success, negative error code on failure. 2673 */ 2674 static int amdgpu_device_ip_fini(struct amdgpu_device *adev) 2675 { 2676 int i, r; 2677 2678 if (amdgpu_sriov_vf(adev) && adev->virt.ras_init_done) 2679 amdgpu_virt_release_ras_err_handler_data(adev); 2680 2681 amdgpu_ras_pre_fini(adev); 2682 2683 if (adev->gmc.xgmi.num_physical_nodes > 1) 2684 amdgpu_xgmi_remove_device(adev); 2685 2686 amdgpu_amdkfd_device_fini_sw(adev); 2687 2688 for (i = adev->num_ip_blocks - 1; i >= 0; i--) { 2689 if (!adev->ip_blocks[i].status.sw) 2690 continue; 2691 2692 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) { 2693 amdgpu_ucode_free_bo(adev); 2694 amdgpu_free_static_csa(&adev->virt.csa_obj); 2695 amdgpu_device_wb_fini(adev); 2696 amdgpu_device_vram_scratch_fini(adev); 2697 amdgpu_ib_pool_fini(adev); 2698 } 2699 2700 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev); 2701 /* XXX handle errors */ 2702 if (r) { 2703 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n", 2704 adev->ip_blocks[i].version->funcs->name, r); 2705 } 2706 adev->ip_blocks[i].status.sw = false; 2707 adev->ip_blocks[i].status.valid = false; 2708 } 2709 2710 for (i = adev->num_ip_blocks - 1; i >= 0; i--) { 2711 if (!adev->ip_blocks[i].status.late_initialized) 2712 continue; 2713 if (adev->ip_blocks[i].version->funcs->late_fini) 2714 adev->ip_blocks[i].version->funcs->late_fini((void *)adev); 2715 adev->ip_blocks[i].status.late_initialized = false; 2716 } 2717 2718 amdgpu_ras_fini(adev); 2719 2720 if (amdgpu_sriov_vf(adev)) 2721 if (amdgpu_virt_release_full_gpu(adev, false)) 2722 DRM_ERROR("failed to release exclusive mode on fini\n"); 2723 2724 return 0; 2725 } 2726 2727 /** 2728 * amdgpu_device_delayed_init_work_handler - work handler for IB tests 2729 * 2730 * @work: work_struct. 2731 */ 2732 static void amdgpu_device_delayed_init_work_handler(struct work_struct *work) 2733 { 2734 struct amdgpu_device *adev = 2735 container_of(work, struct amdgpu_device, delayed_init_work.work); 2736 int r; 2737 2738 r = amdgpu_ib_ring_tests(adev); 2739 if (r) 2740 DRM_ERROR("ib ring test failed (%d).\n", r); 2741 } 2742 2743 static void amdgpu_device_delay_enable_gfx_off(struct work_struct *work) 2744 { 2745 struct amdgpu_device *adev = 2746 container_of(work, struct amdgpu_device, gfx.gfx_off_delay_work.work); 2747 2748 mutex_lock(&adev->gfx.gfx_off_mutex); 2749 if (!adev->gfx.gfx_off_state && !adev->gfx.gfx_off_req_count) { 2750 if (!amdgpu_dpm_set_powergating_by_smu(adev, AMD_IP_BLOCK_TYPE_GFX, true)) 2751 adev->gfx.gfx_off_state = true; 2752 } 2753 mutex_unlock(&adev->gfx.gfx_off_mutex); 2754 } 2755 2756 /** 2757 * amdgpu_device_ip_suspend_phase1 - run suspend for hardware IPs (phase 1) 2758 * 2759 * @adev: amdgpu_device pointer 2760 * 2761 * Main suspend function for hardware IPs. The list of all the hardware 2762 * IPs that make up the asic is walked, clockgating is disabled and the 2763 * suspend callbacks are run. suspend puts the hardware and software state 2764 * in each IP into a state suitable for suspend. 2765 * Returns 0 on success, negative error code on failure. 2766 */ 2767 static int amdgpu_device_ip_suspend_phase1(struct amdgpu_device *adev) 2768 { 2769 int i, r; 2770 2771 amdgpu_device_set_pg_state(adev, AMD_PG_STATE_UNGATE); 2772 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_UNGATE); 2773 2774 for (i = adev->num_ip_blocks - 1; i >= 0; i--) { 2775 if (!adev->ip_blocks[i].status.valid) 2776 continue; 2777 2778 /* displays are handled separately */ 2779 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_DCE) 2780 continue; 2781 2782 /* XXX handle errors */ 2783 r = adev->ip_blocks[i].version->funcs->suspend(adev); 2784 /* XXX handle errors */ 2785 if (r) { 2786 DRM_ERROR("suspend of IP block <%s> failed %d\n", 2787 adev->ip_blocks[i].version->funcs->name, r); 2788 return r; 2789 } 2790 2791 adev->ip_blocks[i].status.hw = false; 2792 } 2793 2794 return 0; 2795 } 2796 2797 /** 2798 * amdgpu_device_ip_suspend_phase2 - run suspend for hardware IPs (phase 2) 2799 * 2800 * @adev: amdgpu_device pointer 2801 * 2802 * Main suspend function for hardware IPs. The list of all the hardware 2803 * IPs that make up the asic is walked, clockgating is disabled and the 2804 * suspend callbacks are run. suspend puts the hardware and software state 2805 * in each IP into a state suitable for suspend. 2806 * Returns 0 on success, negative error code on failure. 2807 */ 2808 static int amdgpu_device_ip_suspend_phase2(struct amdgpu_device *adev) 2809 { 2810 int i, r; 2811 2812 if (adev->in_s0ix) 2813 amdgpu_gfx_state_change_set(adev, sGpuChangeState_D3Entry); 2814 2815 for (i = adev->num_ip_blocks - 1; i >= 0; i--) { 2816 if (!adev->ip_blocks[i].status.valid) 2817 continue; 2818 /* displays are handled in phase1 */ 2819 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) 2820 continue; 2821 /* PSP lost connection when err_event_athub occurs */ 2822 if (amdgpu_ras_intr_triggered() && 2823 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) { 2824 adev->ip_blocks[i].status.hw = false; 2825 continue; 2826 } 2827 2828 /* skip unnecessary suspend if we do not initialize them yet */ 2829 if (adev->gmc.xgmi.pending_reset && 2830 !(adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC || 2831 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC || 2832 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON || 2833 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH)) { 2834 adev->ip_blocks[i].status.hw = false; 2835 continue; 2836 } 2837 2838 /* skip suspend of gfx and psp for S0ix 2839 * gfx is in gfxoff state, so on resume it will exit gfxoff just 2840 * like at runtime. PSP is also part of the always on hardware 2841 * so no need to suspend it. 2842 */ 2843 if (adev->in_s0ix && 2844 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP || 2845 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GFX)) 2846 continue; 2847 2848 /* XXX handle errors */ 2849 r = adev->ip_blocks[i].version->funcs->suspend(adev); 2850 /* XXX handle errors */ 2851 if (r) { 2852 DRM_ERROR("suspend of IP block <%s> failed %d\n", 2853 adev->ip_blocks[i].version->funcs->name, r); 2854 } 2855 adev->ip_blocks[i].status.hw = false; 2856 /* handle putting the SMC in the appropriate state */ 2857 if(!amdgpu_sriov_vf(adev)){ 2858 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) { 2859 r = amdgpu_dpm_set_mp1_state(adev, adev->mp1_state); 2860 if (r) { 2861 DRM_ERROR("SMC failed to set mp1 state %d, %d\n", 2862 adev->mp1_state, r); 2863 return r; 2864 } 2865 } 2866 } 2867 } 2868 2869 return 0; 2870 } 2871 2872 /** 2873 * amdgpu_device_ip_suspend - run suspend for hardware IPs 2874 * 2875 * @adev: amdgpu_device pointer 2876 * 2877 * Main suspend function for hardware IPs. The list of all the hardware 2878 * IPs that make up the asic is walked, clockgating is disabled and the 2879 * suspend callbacks are run. suspend puts the hardware and software state 2880 * in each IP into a state suitable for suspend. 2881 * Returns 0 on success, negative error code on failure. 2882 */ 2883 int amdgpu_device_ip_suspend(struct amdgpu_device *adev) 2884 { 2885 int r; 2886 2887 if (amdgpu_sriov_vf(adev)) { 2888 amdgpu_virt_fini_data_exchange(adev); 2889 amdgpu_virt_request_full_gpu(adev, false); 2890 } 2891 2892 r = amdgpu_device_ip_suspend_phase1(adev); 2893 if (r) 2894 return r; 2895 r = amdgpu_device_ip_suspend_phase2(adev); 2896 2897 if (amdgpu_sriov_vf(adev)) 2898 amdgpu_virt_release_full_gpu(adev, false); 2899 2900 return r; 2901 } 2902 2903 static int amdgpu_device_ip_reinit_early_sriov(struct amdgpu_device *adev) 2904 { 2905 int i, r; 2906 2907 static enum amd_ip_block_type ip_order[] = { 2908 AMD_IP_BLOCK_TYPE_GMC, 2909 AMD_IP_BLOCK_TYPE_COMMON, 2910 AMD_IP_BLOCK_TYPE_PSP, 2911 AMD_IP_BLOCK_TYPE_IH, 2912 }; 2913 2914 for (i = 0; i < adev->num_ip_blocks; i++) { 2915 int j; 2916 struct amdgpu_ip_block *block; 2917 2918 block = &adev->ip_blocks[i]; 2919 block->status.hw = false; 2920 2921 for (j = 0; j < ARRAY_SIZE(ip_order); j++) { 2922 2923 if (block->version->type != ip_order[j] || 2924 !block->status.valid) 2925 continue; 2926 2927 r = block->version->funcs->hw_init(adev); 2928 DRM_INFO("RE-INIT-early: %s %s\n", block->version->funcs->name, r?"failed":"succeeded"); 2929 if (r) 2930 return r; 2931 block->status.hw = true; 2932 } 2933 } 2934 2935 return 0; 2936 } 2937 2938 static int amdgpu_device_ip_reinit_late_sriov(struct amdgpu_device *adev) 2939 { 2940 int i, r; 2941 2942 static enum amd_ip_block_type ip_order[] = { 2943 AMD_IP_BLOCK_TYPE_SMC, 2944 AMD_IP_BLOCK_TYPE_DCE, 2945 AMD_IP_BLOCK_TYPE_GFX, 2946 AMD_IP_BLOCK_TYPE_SDMA, 2947 AMD_IP_BLOCK_TYPE_UVD, 2948 AMD_IP_BLOCK_TYPE_VCE, 2949 AMD_IP_BLOCK_TYPE_VCN 2950 }; 2951 2952 for (i = 0; i < ARRAY_SIZE(ip_order); i++) { 2953 int j; 2954 struct amdgpu_ip_block *block; 2955 2956 for (j = 0; j < adev->num_ip_blocks; j++) { 2957 block = &adev->ip_blocks[j]; 2958 2959 if (block->version->type != ip_order[i] || 2960 !block->status.valid || 2961 block->status.hw) 2962 continue; 2963 2964 if (block->version->type == AMD_IP_BLOCK_TYPE_SMC) 2965 r = block->version->funcs->resume(adev); 2966 else 2967 r = block->version->funcs->hw_init(adev); 2968 2969 DRM_INFO("RE-INIT-late: %s %s\n", block->version->funcs->name, r?"failed":"succeeded"); 2970 if (r) 2971 return r; 2972 block->status.hw = true; 2973 } 2974 } 2975 2976 return 0; 2977 } 2978 2979 /** 2980 * amdgpu_device_ip_resume_phase1 - run resume for hardware IPs 2981 * 2982 * @adev: amdgpu_device pointer 2983 * 2984 * First resume function for hardware IPs. The list of all the hardware 2985 * IPs that make up the asic is walked and the resume callbacks are run for 2986 * COMMON, GMC, and IH. resume puts the hardware into a functional state 2987 * after a suspend and updates the software state as necessary. This 2988 * function is also used for restoring the GPU after a GPU reset. 2989 * Returns 0 on success, negative error code on failure. 2990 */ 2991 static int amdgpu_device_ip_resume_phase1(struct amdgpu_device *adev) 2992 { 2993 int i, r; 2994 2995 for (i = 0; i < adev->num_ip_blocks; i++) { 2996 if (!adev->ip_blocks[i].status.valid || adev->ip_blocks[i].status.hw) 2997 continue; 2998 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON || 2999 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC || 3000 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH) { 3001 3002 r = adev->ip_blocks[i].version->funcs->resume(adev); 3003 if (r) { 3004 DRM_ERROR("resume of IP block <%s> failed %d\n", 3005 adev->ip_blocks[i].version->funcs->name, r); 3006 return r; 3007 } 3008 adev->ip_blocks[i].status.hw = true; 3009 } 3010 } 3011 3012 return 0; 3013 } 3014 3015 /** 3016 * amdgpu_device_ip_resume_phase2 - run resume for hardware IPs 3017 * 3018 * @adev: amdgpu_device pointer 3019 * 3020 * First resume function for hardware IPs. The list of all the hardware 3021 * IPs that make up the asic is walked and the resume callbacks are run for 3022 * all blocks except COMMON, GMC, and IH. resume puts the hardware into a 3023 * functional state after a suspend and updates the software state as 3024 * necessary. This function is also used for restoring the GPU after a GPU 3025 * reset. 3026 * Returns 0 on success, negative error code on failure. 3027 */ 3028 static int amdgpu_device_ip_resume_phase2(struct amdgpu_device *adev) 3029 { 3030 int i, r; 3031 3032 for (i = 0; i < adev->num_ip_blocks; i++) { 3033 if (!adev->ip_blocks[i].status.valid || adev->ip_blocks[i].status.hw) 3034 continue; 3035 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON || 3036 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC || 3037 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH || 3038 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) 3039 continue; 3040 r = adev->ip_blocks[i].version->funcs->resume(adev); 3041 if (r) { 3042 DRM_ERROR("resume of IP block <%s> failed %d\n", 3043 adev->ip_blocks[i].version->funcs->name, r); 3044 return r; 3045 } 3046 adev->ip_blocks[i].status.hw = true; 3047 } 3048 3049 return 0; 3050 } 3051 3052 /** 3053 * amdgpu_device_ip_resume - run resume for hardware IPs 3054 * 3055 * @adev: amdgpu_device pointer 3056 * 3057 * Main resume function for hardware IPs. The hardware IPs 3058 * are split into two resume functions because they are 3059 * are also used in in recovering from a GPU reset and some additional 3060 * steps need to be take between them. In this case (S3/S4) they are 3061 * run sequentially. 3062 * Returns 0 on success, negative error code on failure. 3063 */ 3064 static int amdgpu_device_ip_resume(struct amdgpu_device *adev) 3065 { 3066 int r; 3067 3068 r = amdgpu_device_ip_resume_phase1(adev); 3069 if (r) 3070 return r; 3071 3072 r = amdgpu_device_fw_loading(adev); 3073 if (r) 3074 return r; 3075 3076 r = amdgpu_device_ip_resume_phase2(adev); 3077 3078 return r; 3079 } 3080 3081 /** 3082 * amdgpu_device_detect_sriov_bios - determine if the board supports SR-IOV 3083 * 3084 * @adev: amdgpu_device pointer 3085 * 3086 * Query the VBIOS data tables to determine if the board supports SR-IOV. 3087 */ 3088 static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev) 3089 { 3090 if (amdgpu_sriov_vf(adev)) { 3091 if (adev->is_atom_fw) { 3092 if (amdgpu_atomfirmware_gpu_virtualization_supported(adev)) 3093 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS; 3094 } else { 3095 if (amdgpu_atombios_has_gpu_virtualization_table(adev)) 3096 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS; 3097 } 3098 3099 if (!(adev->virt.caps & AMDGPU_SRIOV_CAPS_SRIOV_VBIOS)) 3100 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_NO_VBIOS, 0, 0); 3101 } 3102 } 3103 3104 /** 3105 * amdgpu_device_asic_has_dc_support - determine if DC supports the asic 3106 * 3107 * @asic_type: AMD asic type 3108 * 3109 * Check if there is DC (new modesetting infrastructre) support for an asic. 3110 * returns true if DC has support, false if not. 3111 */ 3112 bool amdgpu_device_asic_has_dc_support(enum amd_asic_type asic_type) 3113 { 3114 switch (asic_type) { 3115 #if defined(CONFIG_DRM_AMD_DC) 3116 #if defined(CONFIG_DRM_AMD_DC_SI) 3117 case CHIP_TAHITI: 3118 case CHIP_PITCAIRN: 3119 case CHIP_VERDE: 3120 case CHIP_OLAND: 3121 #endif 3122 case CHIP_BONAIRE: 3123 case CHIP_KAVERI: 3124 case CHIP_KABINI: 3125 case CHIP_MULLINS: 3126 /* 3127 * We have systems in the wild with these ASICs that require 3128 * LVDS and VGA support which is not supported with DC. 3129 * 3130 * Fallback to the non-DC driver here by default so as not to 3131 * cause regressions. 3132 */ 3133 return amdgpu_dc > 0; 3134 case CHIP_HAWAII: 3135 case CHIP_CARRIZO: 3136 case CHIP_STONEY: 3137 case CHIP_POLARIS10: 3138 case CHIP_POLARIS11: 3139 case CHIP_POLARIS12: 3140 case CHIP_VEGAM: 3141 case CHIP_TONGA: 3142 case CHIP_FIJI: 3143 case CHIP_VEGA10: 3144 case CHIP_VEGA12: 3145 case CHIP_VEGA20: 3146 #if defined(CONFIG_DRM_AMD_DC_DCN) 3147 case CHIP_RAVEN: 3148 case CHIP_NAVI10: 3149 case CHIP_NAVI14: 3150 case CHIP_NAVI12: 3151 case CHIP_RENOIR: 3152 case CHIP_SIENNA_CICHLID: 3153 case CHIP_NAVY_FLOUNDER: 3154 case CHIP_DIMGREY_CAVEFISH: 3155 case CHIP_BEIGE_GOBY: 3156 case CHIP_VANGOGH: 3157 #endif 3158 return amdgpu_dc != 0; 3159 #endif 3160 default: 3161 if (amdgpu_dc > 0) 3162 DRM_INFO_ONCE("Display Core has been requested via kernel parameter " 3163 "but isn't supported by ASIC, ignoring\n"); 3164 return false; 3165 } 3166 } 3167 3168 /** 3169 * amdgpu_device_has_dc_support - check if dc is supported 3170 * 3171 * @adev: amdgpu_device pointer 3172 * 3173 * Returns true for supported, false for not supported 3174 */ 3175 bool amdgpu_device_has_dc_support(struct amdgpu_device *adev) 3176 { 3177 if (amdgpu_sriov_vf(adev) || 3178 adev->enable_virtual_display || 3179 (adev->harvest_ip_mask & AMD_HARVEST_IP_DMU_MASK)) 3180 return false; 3181 3182 return amdgpu_device_asic_has_dc_support(adev->asic_type); 3183 } 3184 3185 static void amdgpu_device_xgmi_reset_func(struct work_struct *__work) 3186 { 3187 struct amdgpu_device *adev = 3188 container_of(__work, struct amdgpu_device, xgmi_reset_work); 3189 struct amdgpu_hive_info *hive = amdgpu_get_xgmi_hive(adev); 3190 3191 /* It's a bug to not have a hive within this function */ 3192 if (WARN_ON(!hive)) 3193 return; 3194 3195 /* 3196 * Use task barrier to synchronize all xgmi reset works across the 3197 * hive. task_barrier_enter and task_barrier_exit will block 3198 * until all the threads running the xgmi reset works reach 3199 * those points. task_barrier_full will do both blocks. 3200 */ 3201 if (amdgpu_asic_reset_method(adev) == AMD_RESET_METHOD_BACO) { 3202 3203 task_barrier_enter(&hive->tb); 3204 adev->asic_reset_res = amdgpu_device_baco_enter(adev_to_drm(adev)); 3205 3206 if (adev->asic_reset_res) 3207 goto fail; 3208 3209 task_barrier_exit(&hive->tb); 3210 adev->asic_reset_res = amdgpu_device_baco_exit(adev_to_drm(adev)); 3211 3212 if (adev->asic_reset_res) 3213 goto fail; 3214 3215 if (adev->mmhub.ras_funcs && 3216 adev->mmhub.ras_funcs->reset_ras_error_count) 3217 adev->mmhub.ras_funcs->reset_ras_error_count(adev); 3218 } else { 3219 3220 task_barrier_full(&hive->tb); 3221 adev->asic_reset_res = amdgpu_asic_reset(adev); 3222 } 3223 3224 fail: 3225 if (adev->asic_reset_res) 3226 DRM_WARN("ASIC reset failed with error, %d for drm dev, %s", 3227 adev->asic_reset_res, adev_to_drm(adev)->unique); 3228 amdgpu_put_xgmi_hive(hive); 3229 } 3230 3231 static int amdgpu_device_get_job_timeout_settings(struct amdgpu_device *adev) 3232 { 3233 char *input = amdgpu_lockup_timeout; 3234 char *timeout_setting = NULL; 3235 int index = 0; 3236 long timeout; 3237 int ret = 0; 3238 3239 /* 3240 * By default timeout for non compute jobs is 10000 3241 * and 60000 for compute jobs. 3242 * In SR-IOV or passthrough mode, timeout for compute 3243 * jobs are 60000 by default. 3244 */ 3245 adev->gfx_timeout = msecs_to_jiffies(10000); 3246 adev->sdma_timeout = adev->video_timeout = adev->gfx_timeout; 3247 if (amdgpu_sriov_vf(adev)) 3248 adev->compute_timeout = amdgpu_sriov_is_pp_one_vf(adev) ? 3249 msecs_to_jiffies(60000) : msecs_to_jiffies(10000); 3250 else 3251 adev->compute_timeout = msecs_to_jiffies(60000); 3252 3253 if (strnlen(input, AMDGPU_MAX_TIMEOUT_PARAM_LENGTH)) { 3254 while ((timeout_setting = strsep(&input, ",")) && 3255 strnlen(timeout_setting, AMDGPU_MAX_TIMEOUT_PARAM_LENGTH)) { 3256 ret = kstrtol(timeout_setting, 0, &timeout); 3257 if (ret) 3258 return ret; 3259 3260 if (timeout == 0) { 3261 index++; 3262 continue; 3263 } else if (timeout < 0) { 3264 timeout = MAX_SCHEDULE_TIMEOUT; 3265 } else { 3266 timeout = msecs_to_jiffies(timeout); 3267 } 3268 3269 switch (index++) { 3270 case 0: 3271 adev->gfx_timeout = timeout; 3272 break; 3273 case 1: 3274 adev->compute_timeout = timeout; 3275 break; 3276 case 2: 3277 adev->sdma_timeout = timeout; 3278 break; 3279 case 3: 3280 adev->video_timeout = timeout; 3281 break; 3282 default: 3283 break; 3284 } 3285 } 3286 /* 3287 * There is only one value specified and 3288 * it should apply to all non-compute jobs. 3289 */ 3290 if (index == 1) { 3291 adev->sdma_timeout = adev->video_timeout = adev->gfx_timeout; 3292 if (amdgpu_sriov_vf(adev) || amdgpu_passthrough(adev)) 3293 adev->compute_timeout = adev->gfx_timeout; 3294 } 3295 } 3296 3297 return ret; 3298 } 3299 3300 static const struct attribute *amdgpu_dev_attributes[] = { 3301 &dev_attr_product_name.attr, 3302 &dev_attr_product_number.attr, 3303 &dev_attr_serial_number.attr, 3304 &dev_attr_pcie_replay_count.attr, 3305 NULL 3306 }; 3307 3308 /** 3309 * amdgpu_device_init - initialize the driver 3310 * 3311 * @adev: amdgpu_device pointer 3312 * @flags: driver flags 3313 * 3314 * Initializes the driver info and hw (all asics). 3315 * Returns 0 for success or an error on failure. 3316 * Called at driver startup. 3317 */ 3318 int amdgpu_device_init(struct amdgpu_device *adev, 3319 uint32_t flags) 3320 { 3321 struct drm_device *ddev = adev_to_drm(adev); 3322 struct pci_dev *pdev = adev->pdev; 3323 int r, i; 3324 bool px = false; 3325 u32 max_MBps; 3326 3327 adev->shutdown = false; 3328 adev->flags = flags; 3329 3330 if (amdgpu_force_asic_type >= 0 && amdgpu_force_asic_type < CHIP_LAST) 3331 adev->asic_type = amdgpu_force_asic_type; 3332 else 3333 adev->asic_type = flags & AMD_ASIC_MASK; 3334 3335 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT; 3336 if (amdgpu_emu_mode == 1) 3337 adev->usec_timeout *= 10; 3338 adev->gmc.gart_size = 512 * 1024 * 1024; 3339 adev->accel_working = false; 3340 adev->num_rings = 0; 3341 adev->mman.buffer_funcs = NULL; 3342 adev->mman.buffer_funcs_ring = NULL; 3343 adev->vm_manager.vm_pte_funcs = NULL; 3344 adev->vm_manager.vm_pte_num_scheds = 0; 3345 adev->gmc.gmc_funcs = NULL; 3346 adev->harvest_ip_mask = 0x0; 3347 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS); 3348 bitmap_zero(adev->gfx.pipe_reserve_bitmap, AMDGPU_MAX_COMPUTE_QUEUES); 3349 3350 adev->smc_rreg = &amdgpu_invalid_rreg; 3351 adev->smc_wreg = &amdgpu_invalid_wreg; 3352 adev->pcie_rreg = &amdgpu_invalid_rreg; 3353 adev->pcie_wreg = &amdgpu_invalid_wreg; 3354 adev->pciep_rreg = &amdgpu_invalid_rreg; 3355 adev->pciep_wreg = &amdgpu_invalid_wreg; 3356 adev->pcie_rreg64 = &amdgpu_invalid_rreg64; 3357 adev->pcie_wreg64 = &amdgpu_invalid_wreg64; 3358 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg; 3359 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg; 3360 adev->didt_rreg = &amdgpu_invalid_rreg; 3361 adev->didt_wreg = &amdgpu_invalid_wreg; 3362 adev->gc_cac_rreg = &amdgpu_invalid_rreg; 3363 adev->gc_cac_wreg = &amdgpu_invalid_wreg; 3364 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg; 3365 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg; 3366 3367 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n", 3368 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device, 3369 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision); 3370 3371 /* mutex initialization are all done here so we 3372 * can recall function without having locking issues */ 3373 mutex_init(&adev->firmware.mutex); 3374 mutex_init(&adev->pm.mutex); 3375 mutex_init(&adev->gfx.gpu_clock_mutex); 3376 mutex_init(&adev->srbm_mutex); 3377 mutex_init(&adev->gfx.pipe_reserve_mutex); 3378 mutex_init(&adev->gfx.gfx_off_mutex); 3379 mutex_init(&adev->grbm_idx_mutex); 3380 mutex_init(&adev->mn_lock); 3381 mutex_init(&adev->virt.vf_errors.lock); 3382 hash_init(adev->mn_hash); 3383 atomic_set(&adev->in_gpu_reset, 0); 3384 init_rwsem(&adev->reset_sem); 3385 mutex_init(&adev->psp.mutex); 3386 mutex_init(&adev->notifier_lock); 3387 3388 r = amdgpu_device_check_arguments(adev); 3389 if (r) 3390 return r; 3391 3392 spin_lock_init(&adev->mmio_idx_lock); 3393 spin_lock_init(&adev->smc_idx_lock); 3394 spin_lock_init(&adev->pcie_idx_lock); 3395 spin_lock_init(&adev->uvd_ctx_idx_lock); 3396 spin_lock_init(&adev->didt_idx_lock); 3397 spin_lock_init(&adev->gc_cac_idx_lock); 3398 spin_lock_init(&adev->se_cac_idx_lock); 3399 spin_lock_init(&adev->audio_endpt_idx_lock); 3400 spin_lock_init(&adev->mm_stats.lock); 3401 3402 INIT_LIST_HEAD(&adev->shadow_list); 3403 mutex_init(&adev->shadow_list_lock); 3404 3405 INIT_LIST_HEAD(&adev->reset_list); 3406 3407 INIT_DELAYED_WORK(&adev->delayed_init_work, 3408 amdgpu_device_delayed_init_work_handler); 3409 INIT_DELAYED_WORK(&adev->gfx.gfx_off_delay_work, 3410 amdgpu_device_delay_enable_gfx_off); 3411 3412 INIT_WORK(&adev->xgmi_reset_work, amdgpu_device_xgmi_reset_func); 3413 3414 adev->gfx.gfx_off_req_count = 1; 3415 adev->pm.ac_power = power_supply_is_system_supplied() > 0; 3416 3417 atomic_set(&adev->throttling_logging_enabled, 1); 3418 /* 3419 * If throttling continues, logging will be performed every minute 3420 * to avoid log flooding. "-1" is subtracted since the thermal 3421 * throttling interrupt comes every second. Thus, the total logging 3422 * interval is 59 seconds(retelimited printk interval) + 1(waiting 3423 * for throttling interrupt) = 60 seconds. 3424 */ 3425 ratelimit_state_init(&adev->throttling_logging_rs, (60 - 1) * HZ, 1); 3426 ratelimit_set_flags(&adev->throttling_logging_rs, RATELIMIT_MSG_ON_RELEASE); 3427 3428 /* Registers mapping */ 3429 /* TODO: block userspace mapping of io register */ 3430 if (adev->asic_type >= CHIP_BONAIRE) { 3431 adev->rmmio_base = pci_resource_start(adev->pdev, 5); 3432 adev->rmmio_size = pci_resource_len(adev->pdev, 5); 3433 } else { 3434 adev->rmmio_base = pci_resource_start(adev->pdev, 2); 3435 adev->rmmio_size = pci_resource_len(adev->pdev, 2); 3436 } 3437 3438 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size); 3439 if (adev->rmmio == NULL) { 3440 return -ENOMEM; 3441 } 3442 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base); 3443 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size); 3444 3445 /* enable PCIE atomic ops */ 3446 r = pci_enable_atomic_ops_to_root(adev->pdev, 3447 PCI_EXP_DEVCAP2_ATOMIC_COMP32 | 3448 PCI_EXP_DEVCAP2_ATOMIC_COMP64); 3449 if (r) { 3450 adev->have_atomics_support = false; 3451 DRM_INFO("PCIE atomic ops is not supported\n"); 3452 } else { 3453 adev->have_atomics_support = true; 3454 } 3455 3456 amdgpu_device_get_pcie_info(adev); 3457 3458 if (amdgpu_mcbp) 3459 DRM_INFO("MCBP is enabled\n"); 3460 3461 if (amdgpu_mes && adev->asic_type >= CHIP_NAVI10) 3462 adev->enable_mes = true; 3463 3464 /* detect hw virtualization here */ 3465 amdgpu_detect_virtualization(adev); 3466 3467 r = amdgpu_device_get_job_timeout_settings(adev); 3468 if (r) { 3469 dev_err(adev->dev, "invalid lockup_timeout parameter syntax\n"); 3470 goto failed_unmap; 3471 } 3472 3473 /* early init functions */ 3474 r = amdgpu_device_ip_early_init(adev); 3475 if (r) 3476 goto failed_unmap; 3477 3478 /* doorbell bar mapping and doorbell index init*/ 3479 amdgpu_device_doorbell_init(adev); 3480 3481 if (amdgpu_emu_mode == 1) { 3482 /* post the asic on emulation mode */ 3483 emu_soc_asic_init(adev); 3484 goto fence_driver_init; 3485 } 3486 3487 amdgpu_reset_init(adev); 3488 3489 /* detect if we are with an SRIOV vbios */ 3490 amdgpu_device_detect_sriov_bios(adev); 3491 3492 /* check if we need to reset the asic 3493 * E.g., driver was not cleanly unloaded previously, etc. 3494 */ 3495 if (!amdgpu_sriov_vf(adev) && amdgpu_asic_need_reset_on_init(adev)) { 3496 if (adev->gmc.xgmi.num_physical_nodes) { 3497 dev_info(adev->dev, "Pending hive reset.\n"); 3498 adev->gmc.xgmi.pending_reset = true; 3499 /* Only need to init necessary block for SMU to handle the reset */ 3500 for (i = 0; i < adev->num_ip_blocks; i++) { 3501 if (!adev->ip_blocks[i].status.valid) 3502 continue; 3503 if (!(adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC || 3504 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON || 3505 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH || 3506 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC)) { 3507 DRM_DEBUG("IP %s disabled for hw_init.\n", 3508 adev->ip_blocks[i].version->funcs->name); 3509 adev->ip_blocks[i].status.hw = true; 3510 } 3511 } 3512 } else { 3513 r = amdgpu_asic_reset(adev); 3514 if (r) { 3515 dev_err(adev->dev, "asic reset on init failed\n"); 3516 goto failed; 3517 } 3518 } 3519 } 3520 3521 pci_enable_pcie_error_reporting(adev->pdev); 3522 3523 /* Post card if necessary */ 3524 if (amdgpu_device_need_post(adev)) { 3525 if (!adev->bios) { 3526 dev_err(adev->dev, "no vBIOS found\n"); 3527 r = -EINVAL; 3528 goto failed; 3529 } 3530 DRM_INFO("GPU posting now...\n"); 3531 r = amdgpu_device_asic_init(adev); 3532 if (r) { 3533 dev_err(adev->dev, "gpu post error!\n"); 3534 goto failed; 3535 } 3536 } 3537 3538 if (adev->is_atom_fw) { 3539 /* Initialize clocks */ 3540 r = amdgpu_atomfirmware_get_clock_info(adev); 3541 if (r) { 3542 dev_err(adev->dev, "amdgpu_atomfirmware_get_clock_info failed\n"); 3543 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0); 3544 goto failed; 3545 } 3546 } else { 3547 /* Initialize clocks */ 3548 r = amdgpu_atombios_get_clock_info(adev); 3549 if (r) { 3550 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n"); 3551 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0); 3552 goto failed; 3553 } 3554 /* init i2c buses */ 3555 if (!amdgpu_device_has_dc_support(adev)) 3556 amdgpu_atombios_i2c_init(adev); 3557 } 3558 3559 fence_driver_init: 3560 /* Fence driver */ 3561 r = amdgpu_fence_driver_init(adev); 3562 if (r) { 3563 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n"); 3564 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0); 3565 goto failed; 3566 } 3567 3568 /* init the mode config */ 3569 drm_mode_config_init(adev_to_drm(adev)); 3570 3571 r = amdgpu_device_ip_init(adev); 3572 if (r) { 3573 /* failed in exclusive mode due to timeout */ 3574 if (amdgpu_sriov_vf(adev) && 3575 !amdgpu_sriov_runtime(adev) && 3576 amdgpu_virt_mmio_blocked(adev) && 3577 !amdgpu_virt_wait_reset(adev)) { 3578 dev_err(adev->dev, "VF exclusive mode timeout\n"); 3579 /* Don't send request since VF is inactive. */ 3580 adev->virt.caps &= ~AMDGPU_SRIOV_CAPS_RUNTIME; 3581 adev->virt.ops = NULL; 3582 r = -EAGAIN; 3583 goto release_ras_con; 3584 } 3585 dev_err(adev->dev, "amdgpu_device_ip_init failed\n"); 3586 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0); 3587 goto release_ras_con; 3588 } 3589 3590 dev_info(adev->dev, 3591 "SE %d, SH per SE %d, CU per SH %d, active_cu_number %d\n", 3592 adev->gfx.config.max_shader_engines, 3593 adev->gfx.config.max_sh_per_se, 3594 adev->gfx.config.max_cu_per_sh, 3595 adev->gfx.cu_info.number); 3596 3597 adev->accel_working = true; 3598 3599 amdgpu_vm_check_compute_bug(adev); 3600 3601 /* Initialize the buffer migration limit. */ 3602 if (amdgpu_moverate >= 0) 3603 max_MBps = amdgpu_moverate; 3604 else 3605 max_MBps = 8; /* Allow 8 MB/s. */ 3606 /* Get a log2 for easy divisions. */ 3607 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps)); 3608 3609 amdgpu_fbdev_init(adev); 3610 3611 r = amdgpu_pm_sysfs_init(adev); 3612 if (r) { 3613 adev->pm_sysfs_en = false; 3614 DRM_ERROR("registering pm debugfs failed (%d).\n", r); 3615 } else 3616 adev->pm_sysfs_en = true; 3617 3618 r = amdgpu_ucode_sysfs_init(adev); 3619 if (r) { 3620 adev->ucode_sysfs_en = false; 3621 DRM_ERROR("Creating firmware sysfs failed (%d).\n", r); 3622 } else 3623 adev->ucode_sysfs_en = true; 3624 3625 if ((amdgpu_testing & 1)) { 3626 if (adev->accel_working) 3627 amdgpu_test_moves(adev); 3628 else 3629 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n"); 3630 } 3631 if (amdgpu_benchmarking) { 3632 if (adev->accel_working) 3633 amdgpu_benchmark(adev, amdgpu_benchmarking); 3634 else 3635 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n"); 3636 } 3637 3638 /* 3639 * Register gpu instance before amdgpu_device_enable_mgpu_fan_boost. 3640 * Otherwise the mgpu fan boost feature will be skipped due to the 3641 * gpu instance is counted less. 3642 */ 3643 amdgpu_register_gpu_instance(adev); 3644 3645 /* enable clockgating, etc. after ib tests, etc. since some blocks require 3646 * explicit gating rather than handling it automatically. 3647 */ 3648 if (!adev->gmc.xgmi.pending_reset) { 3649 r = amdgpu_device_ip_late_init(adev); 3650 if (r) { 3651 dev_err(adev->dev, "amdgpu_device_ip_late_init failed\n"); 3652 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r); 3653 goto release_ras_con; 3654 } 3655 /* must succeed. */ 3656 amdgpu_ras_resume(adev); 3657 queue_delayed_work(system_wq, &adev->delayed_init_work, 3658 msecs_to_jiffies(AMDGPU_RESUME_MS)); 3659 } 3660 3661 if (amdgpu_sriov_vf(adev)) 3662 flush_delayed_work(&adev->delayed_init_work); 3663 3664 r = sysfs_create_files(&adev->dev->kobj, amdgpu_dev_attributes); 3665 if (r) 3666 dev_err(adev->dev, "Could not create amdgpu device attr\n"); 3667 3668 if (IS_ENABLED(CONFIG_PERF_EVENTS)) 3669 r = amdgpu_pmu_init(adev); 3670 if (r) 3671 dev_err(adev->dev, "amdgpu_pmu_init failed\n"); 3672 3673 /* Have stored pci confspace at hand for restore in sudden PCI error */ 3674 if (amdgpu_device_cache_pci_state(adev->pdev)) 3675 pci_restore_state(pdev); 3676 3677 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */ 3678 /* this will fail for cards that aren't VGA class devices, just 3679 * ignore it */ 3680 if ((adev->pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA) 3681 vga_client_register(adev->pdev, adev, NULL, amdgpu_device_vga_set_decode); 3682 3683 if (amdgpu_device_supports_px(ddev)) { 3684 px = true; 3685 vga_switcheroo_register_client(adev->pdev, 3686 &amdgpu_switcheroo_ops, px); 3687 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain); 3688 } 3689 3690 if (adev->gmc.xgmi.pending_reset) 3691 queue_delayed_work(system_wq, &mgpu_info.delayed_reset_work, 3692 msecs_to_jiffies(AMDGPU_RESUME_MS)); 3693 3694 return 0; 3695 3696 release_ras_con: 3697 amdgpu_release_ras_context(adev); 3698 3699 failed: 3700 amdgpu_vf_error_trans_all(adev); 3701 3702 failed_unmap: 3703 iounmap(adev->rmmio); 3704 adev->rmmio = NULL; 3705 3706 return r; 3707 } 3708 3709 static void amdgpu_device_unmap_mmio(struct amdgpu_device *adev) 3710 { 3711 /* Clear all CPU mappings pointing to this device */ 3712 unmap_mapping_range(adev->ddev.anon_inode->i_mapping, 0, 0, 1); 3713 3714 /* Unmap all mapped bars - Doorbell, registers and VRAM */ 3715 amdgpu_device_doorbell_fini(adev); 3716 3717 iounmap(adev->rmmio); 3718 adev->rmmio = NULL; 3719 if (adev->mman.aper_base_kaddr) 3720 iounmap(adev->mman.aper_base_kaddr); 3721 adev->mman.aper_base_kaddr = NULL; 3722 3723 /* Memory manager related */ 3724 if (!adev->gmc.xgmi.connected_to_cpu) { 3725 arch_phys_wc_del(adev->gmc.vram_mtrr); 3726 arch_io_free_memtype_wc(adev->gmc.aper_base, adev->gmc.aper_size); 3727 } 3728 } 3729 3730 /** 3731 * amdgpu_device_fini - tear down the driver 3732 * 3733 * @adev: amdgpu_device pointer 3734 * 3735 * Tear down the driver info (all asics). 3736 * Called at driver shutdown. 3737 */ 3738 void amdgpu_device_fini_hw(struct amdgpu_device *adev) 3739 { 3740 dev_info(adev->dev, "amdgpu: finishing device.\n"); 3741 flush_delayed_work(&adev->delayed_init_work); 3742 ttm_bo_lock_delayed_workqueue(&adev->mman.bdev); 3743 adev->shutdown = true; 3744 3745 /* make sure IB test finished before entering exclusive mode 3746 * to avoid preemption on IB test 3747 * */ 3748 if (amdgpu_sriov_vf(adev)) { 3749 amdgpu_virt_request_full_gpu(adev, false); 3750 amdgpu_virt_fini_data_exchange(adev); 3751 } 3752 3753 /* disable all interrupts */ 3754 amdgpu_irq_disable_all(adev); 3755 if (adev->mode_info.mode_config_initialized){ 3756 if (!amdgpu_device_has_dc_support(adev)) 3757 drm_helper_force_disable_all(adev_to_drm(adev)); 3758 else 3759 drm_atomic_helper_shutdown(adev_to_drm(adev)); 3760 } 3761 amdgpu_fence_driver_fini_hw(adev); 3762 3763 if (adev->pm_sysfs_en) 3764 amdgpu_pm_sysfs_fini(adev); 3765 if (adev->ucode_sysfs_en) 3766 amdgpu_ucode_sysfs_fini(adev); 3767 sysfs_remove_files(&adev->dev->kobj, amdgpu_dev_attributes); 3768 3769 amdgpu_fbdev_fini(adev); 3770 3771 amdgpu_irq_fini_hw(adev); 3772 3773 amdgpu_device_ip_fini_early(adev); 3774 3775 amdgpu_gart_dummy_page_fini(adev); 3776 3777 amdgpu_device_unmap_mmio(adev); 3778 } 3779 3780 void amdgpu_device_fini_sw(struct amdgpu_device *adev) 3781 { 3782 amdgpu_device_ip_fini(adev); 3783 amdgpu_fence_driver_fini_sw(adev); 3784 release_firmware(adev->firmware.gpu_info_fw); 3785 adev->firmware.gpu_info_fw = NULL; 3786 adev->accel_working = false; 3787 3788 amdgpu_reset_fini(adev); 3789 3790 /* free i2c buses */ 3791 if (!amdgpu_device_has_dc_support(adev)) 3792 amdgpu_i2c_fini(adev); 3793 3794 if (amdgpu_emu_mode != 1) 3795 amdgpu_atombios_fini(adev); 3796 3797 kfree(adev->bios); 3798 adev->bios = NULL; 3799 if (amdgpu_device_supports_px(adev_to_drm(adev))) { 3800 vga_switcheroo_unregister_client(adev->pdev); 3801 vga_switcheroo_fini_domain_pm_ops(adev->dev); 3802 } 3803 if ((adev->pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA) 3804 vga_client_register(adev->pdev, NULL, NULL, NULL); 3805 3806 if (IS_ENABLED(CONFIG_PERF_EVENTS)) 3807 amdgpu_pmu_fini(adev); 3808 if (adev->mman.discovery_bin) 3809 amdgpu_discovery_fini(adev); 3810 3811 kfree(adev->pci_state); 3812 3813 } 3814 3815 3816 /* 3817 * Suspend & resume. 3818 */ 3819 /** 3820 * amdgpu_device_suspend - initiate device suspend 3821 * 3822 * @dev: drm dev pointer 3823 * @fbcon : notify the fbdev of suspend 3824 * 3825 * Puts the hw in the suspend state (all asics). 3826 * Returns 0 for success or an error on failure. 3827 * Called at driver suspend. 3828 */ 3829 int amdgpu_device_suspend(struct drm_device *dev, bool fbcon) 3830 { 3831 struct amdgpu_device *adev = drm_to_adev(dev); 3832 3833 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF) 3834 return 0; 3835 3836 adev->in_suspend = true; 3837 3838 if (amdgpu_acpi_smart_shift_update(dev, AMDGPU_SS_DEV_D3)) 3839 DRM_WARN("smart shift update failed\n"); 3840 3841 drm_kms_helper_poll_disable(dev); 3842 3843 if (fbcon) 3844 amdgpu_fbdev_set_suspend(adev, 1); 3845 3846 cancel_delayed_work_sync(&adev->delayed_init_work); 3847 3848 amdgpu_ras_suspend(adev); 3849 3850 amdgpu_device_ip_suspend_phase1(adev); 3851 3852 if (!adev->in_s0ix) 3853 amdgpu_amdkfd_suspend(adev, adev->in_runpm); 3854 3855 /* evict vram memory */ 3856 amdgpu_bo_evict_vram(adev); 3857 3858 amdgpu_fence_driver_suspend(adev); 3859 3860 amdgpu_device_ip_suspend_phase2(adev); 3861 /* evict remaining vram memory 3862 * This second call to evict vram is to evict the gart page table 3863 * using the CPU. 3864 */ 3865 amdgpu_bo_evict_vram(adev); 3866 3867 return 0; 3868 } 3869 3870 /** 3871 * amdgpu_device_resume - initiate device resume 3872 * 3873 * @dev: drm dev pointer 3874 * @fbcon : notify the fbdev of resume 3875 * 3876 * Bring the hw back to operating state (all asics). 3877 * Returns 0 for success or an error on failure. 3878 * Called at driver resume. 3879 */ 3880 int amdgpu_device_resume(struct drm_device *dev, bool fbcon) 3881 { 3882 struct amdgpu_device *adev = drm_to_adev(dev); 3883 int r = 0; 3884 3885 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF) 3886 return 0; 3887 3888 if (adev->in_s0ix) 3889 amdgpu_gfx_state_change_set(adev, sGpuChangeState_D0Entry); 3890 3891 /* post card */ 3892 if (amdgpu_device_need_post(adev)) { 3893 r = amdgpu_device_asic_init(adev); 3894 if (r) 3895 dev_err(adev->dev, "amdgpu asic init failed\n"); 3896 } 3897 3898 r = amdgpu_device_ip_resume(adev); 3899 if (r) { 3900 dev_err(adev->dev, "amdgpu_device_ip_resume failed (%d).\n", r); 3901 return r; 3902 } 3903 amdgpu_fence_driver_resume(adev); 3904 3905 3906 r = amdgpu_device_ip_late_init(adev); 3907 if (r) 3908 return r; 3909 3910 queue_delayed_work(system_wq, &adev->delayed_init_work, 3911 msecs_to_jiffies(AMDGPU_RESUME_MS)); 3912 3913 if (!adev->in_s0ix) { 3914 r = amdgpu_amdkfd_resume(adev, adev->in_runpm); 3915 if (r) 3916 return r; 3917 } 3918 3919 /* Make sure IB tests flushed */ 3920 flush_delayed_work(&adev->delayed_init_work); 3921 3922 if (fbcon) 3923 amdgpu_fbdev_set_suspend(adev, 0); 3924 3925 drm_kms_helper_poll_enable(dev); 3926 3927 amdgpu_ras_resume(adev); 3928 3929 /* 3930 * Most of the connector probing functions try to acquire runtime pm 3931 * refs to ensure that the GPU is powered on when connector polling is 3932 * performed. Since we're calling this from a runtime PM callback, 3933 * trying to acquire rpm refs will cause us to deadlock. 3934 * 3935 * Since we're guaranteed to be holding the rpm lock, it's safe to 3936 * temporarily disable the rpm helpers so this doesn't deadlock us. 3937 */ 3938 #ifdef CONFIG_PM 3939 dev->dev->power.disable_depth++; 3940 #endif 3941 if (!amdgpu_device_has_dc_support(adev)) 3942 drm_helper_hpd_irq_event(dev); 3943 else 3944 drm_kms_helper_hotplug_event(dev); 3945 #ifdef CONFIG_PM 3946 dev->dev->power.disable_depth--; 3947 #endif 3948 adev->in_suspend = false; 3949 3950 if (amdgpu_acpi_smart_shift_update(dev, AMDGPU_SS_DEV_D0)) 3951 DRM_WARN("smart shift update failed\n"); 3952 3953 return 0; 3954 } 3955 3956 /** 3957 * amdgpu_device_ip_check_soft_reset - did soft reset succeed 3958 * 3959 * @adev: amdgpu_device pointer 3960 * 3961 * The list of all the hardware IPs that make up the asic is walked and 3962 * the check_soft_reset callbacks are run. check_soft_reset determines 3963 * if the asic is still hung or not. 3964 * Returns true if any of the IPs are still in a hung state, false if not. 3965 */ 3966 static bool amdgpu_device_ip_check_soft_reset(struct amdgpu_device *adev) 3967 { 3968 int i; 3969 bool asic_hang = false; 3970 3971 if (amdgpu_sriov_vf(adev)) 3972 return true; 3973 3974 if (amdgpu_asic_need_full_reset(adev)) 3975 return true; 3976 3977 for (i = 0; i < adev->num_ip_blocks; i++) { 3978 if (!adev->ip_blocks[i].status.valid) 3979 continue; 3980 if (adev->ip_blocks[i].version->funcs->check_soft_reset) 3981 adev->ip_blocks[i].status.hang = 3982 adev->ip_blocks[i].version->funcs->check_soft_reset(adev); 3983 if (adev->ip_blocks[i].status.hang) { 3984 dev_info(adev->dev, "IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name); 3985 asic_hang = true; 3986 } 3987 } 3988 return asic_hang; 3989 } 3990 3991 /** 3992 * amdgpu_device_ip_pre_soft_reset - prepare for soft reset 3993 * 3994 * @adev: amdgpu_device pointer 3995 * 3996 * The list of all the hardware IPs that make up the asic is walked and the 3997 * pre_soft_reset callbacks are run if the block is hung. pre_soft_reset 3998 * handles any IP specific hardware or software state changes that are 3999 * necessary for a soft reset to succeed. 4000 * Returns 0 on success, negative error code on failure. 4001 */ 4002 static int amdgpu_device_ip_pre_soft_reset(struct amdgpu_device *adev) 4003 { 4004 int i, r = 0; 4005 4006 for (i = 0; i < adev->num_ip_blocks; i++) { 4007 if (!adev->ip_blocks[i].status.valid) 4008 continue; 4009 if (adev->ip_blocks[i].status.hang && 4010 adev->ip_blocks[i].version->funcs->pre_soft_reset) { 4011 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev); 4012 if (r) 4013 return r; 4014 } 4015 } 4016 4017 return 0; 4018 } 4019 4020 /** 4021 * amdgpu_device_ip_need_full_reset - check if a full asic reset is needed 4022 * 4023 * @adev: amdgpu_device pointer 4024 * 4025 * Some hardware IPs cannot be soft reset. If they are hung, a full gpu 4026 * reset is necessary to recover. 4027 * Returns true if a full asic reset is required, false if not. 4028 */ 4029 static bool amdgpu_device_ip_need_full_reset(struct amdgpu_device *adev) 4030 { 4031 int i; 4032 4033 if (amdgpu_asic_need_full_reset(adev)) 4034 return true; 4035 4036 for (i = 0; i < adev->num_ip_blocks; i++) { 4037 if (!adev->ip_blocks[i].status.valid) 4038 continue; 4039 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) || 4040 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) || 4041 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) || 4042 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) || 4043 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) { 4044 if (adev->ip_blocks[i].status.hang) { 4045 dev_info(adev->dev, "Some block need full reset!\n"); 4046 return true; 4047 } 4048 } 4049 } 4050 return false; 4051 } 4052 4053 /** 4054 * amdgpu_device_ip_soft_reset - do a soft reset 4055 * 4056 * @adev: amdgpu_device pointer 4057 * 4058 * The list of all the hardware IPs that make up the asic is walked and the 4059 * soft_reset callbacks are run if the block is hung. soft_reset handles any 4060 * IP specific hardware or software state changes that are necessary to soft 4061 * reset the IP. 4062 * Returns 0 on success, negative error code on failure. 4063 */ 4064 static int amdgpu_device_ip_soft_reset(struct amdgpu_device *adev) 4065 { 4066 int i, r = 0; 4067 4068 for (i = 0; i < adev->num_ip_blocks; i++) { 4069 if (!adev->ip_blocks[i].status.valid) 4070 continue; 4071 if (adev->ip_blocks[i].status.hang && 4072 adev->ip_blocks[i].version->funcs->soft_reset) { 4073 r = adev->ip_blocks[i].version->funcs->soft_reset(adev); 4074 if (r) 4075 return r; 4076 } 4077 } 4078 4079 return 0; 4080 } 4081 4082 /** 4083 * amdgpu_device_ip_post_soft_reset - clean up from soft reset 4084 * 4085 * @adev: amdgpu_device pointer 4086 * 4087 * The list of all the hardware IPs that make up the asic is walked and the 4088 * post_soft_reset callbacks are run if the asic was hung. post_soft_reset 4089 * handles any IP specific hardware or software state changes that are 4090 * necessary after the IP has been soft reset. 4091 * Returns 0 on success, negative error code on failure. 4092 */ 4093 static int amdgpu_device_ip_post_soft_reset(struct amdgpu_device *adev) 4094 { 4095 int i, r = 0; 4096 4097 for (i = 0; i < adev->num_ip_blocks; i++) { 4098 if (!adev->ip_blocks[i].status.valid) 4099 continue; 4100 if (adev->ip_blocks[i].status.hang && 4101 adev->ip_blocks[i].version->funcs->post_soft_reset) 4102 r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev); 4103 if (r) 4104 return r; 4105 } 4106 4107 return 0; 4108 } 4109 4110 /** 4111 * amdgpu_device_recover_vram - Recover some VRAM contents 4112 * 4113 * @adev: amdgpu_device pointer 4114 * 4115 * Restores the contents of VRAM buffers from the shadows in GTT. Used to 4116 * restore things like GPUVM page tables after a GPU reset where 4117 * the contents of VRAM might be lost. 4118 * 4119 * Returns: 4120 * 0 on success, negative error code on failure. 4121 */ 4122 static int amdgpu_device_recover_vram(struct amdgpu_device *adev) 4123 { 4124 struct dma_fence *fence = NULL, *next = NULL; 4125 struct amdgpu_bo *shadow; 4126 long r = 1, tmo; 4127 4128 if (amdgpu_sriov_runtime(adev)) 4129 tmo = msecs_to_jiffies(8000); 4130 else 4131 tmo = msecs_to_jiffies(100); 4132 4133 dev_info(adev->dev, "recover vram bo from shadow start\n"); 4134 mutex_lock(&adev->shadow_list_lock); 4135 list_for_each_entry(shadow, &adev->shadow_list, shadow_list) { 4136 4137 /* No need to recover an evicted BO */ 4138 if (shadow->tbo.mem.mem_type != TTM_PL_TT || 4139 shadow->tbo.mem.start == AMDGPU_BO_INVALID_OFFSET || 4140 shadow->parent->tbo.mem.mem_type != TTM_PL_VRAM) 4141 continue; 4142 4143 r = amdgpu_bo_restore_shadow(shadow, &next); 4144 if (r) 4145 break; 4146 4147 if (fence) { 4148 tmo = dma_fence_wait_timeout(fence, false, tmo); 4149 dma_fence_put(fence); 4150 fence = next; 4151 if (tmo == 0) { 4152 r = -ETIMEDOUT; 4153 break; 4154 } else if (tmo < 0) { 4155 r = tmo; 4156 break; 4157 } 4158 } else { 4159 fence = next; 4160 } 4161 } 4162 mutex_unlock(&adev->shadow_list_lock); 4163 4164 if (fence) 4165 tmo = dma_fence_wait_timeout(fence, false, tmo); 4166 dma_fence_put(fence); 4167 4168 if (r < 0 || tmo <= 0) { 4169 dev_err(adev->dev, "recover vram bo from shadow failed, r is %ld, tmo is %ld\n", r, tmo); 4170 return -EIO; 4171 } 4172 4173 dev_info(adev->dev, "recover vram bo from shadow done\n"); 4174 return 0; 4175 } 4176 4177 4178 /** 4179 * amdgpu_device_reset_sriov - reset ASIC for SR-IOV vf 4180 * 4181 * @adev: amdgpu_device pointer 4182 * @from_hypervisor: request from hypervisor 4183 * 4184 * do VF FLR and reinitialize Asic 4185 * return 0 means succeeded otherwise failed 4186 */ 4187 static int amdgpu_device_reset_sriov(struct amdgpu_device *adev, 4188 bool from_hypervisor) 4189 { 4190 int r; 4191 4192 if (from_hypervisor) 4193 r = amdgpu_virt_request_full_gpu(adev, true); 4194 else 4195 r = amdgpu_virt_reset_gpu(adev); 4196 if (r) 4197 return r; 4198 4199 amdgpu_amdkfd_pre_reset(adev); 4200 4201 /* Resume IP prior to SMC */ 4202 r = amdgpu_device_ip_reinit_early_sriov(adev); 4203 if (r) 4204 goto error; 4205 4206 amdgpu_virt_init_data_exchange(adev); 4207 /* we need recover gart prior to run SMC/CP/SDMA resume */ 4208 amdgpu_gtt_mgr_recover(ttm_manager_type(&adev->mman.bdev, TTM_PL_TT)); 4209 4210 r = amdgpu_device_fw_loading(adev); 4211 if (r) 4212 return r; 4213 4214 /* now we are okay to resume SMC/CP/SDMA */ 4215 r = amdgpu_device_ip_reinit_late_sriov(adev); 4216 if (r) 4217 goto error; 4218 4219 amdgpu_irq_gpu_reset_resume_helper(adev); 4220 r = amdgpu_ib_ring_tests(adev); 4221 amdgpu_amdkfd_post_reset(adev); 4222 4223 error: 4224 if (!r && adev->virt.gim_feature & AMDGIM_FEATURE_GIM_FLR_VRAMLOST) { 4225 amdgpu_inc_vram_lost(adev); 4226 r = amdgpu_device_recover_vram(adev); 4227 } 4228 amdgpu_virt_release_full_gpu(adev, true); 4229 4230 return r; 4231 } 4232 4233 /** 4234 * amdgpu_device_has_job_running - check if there is any job in mirror list 4235 * 4236 * @adev: amdgpu_device pointer 4237 * 4238 * check if there is any job in mirror list 4239 */ 4240 bool amdgpu_device_has_job_running(struct amdgpu_device *adev) 4241 { 4242 int i; 4243 struct drm_sched_job *job; 4244 4245 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) { 4246 struct amdgpu_ring *ring = adev->rings[i]; 4247 4248 if (!ring || !ring->sched.thread) 4249 continue; 4250 4251 spin_lock(&ring->sched.job_list_lock); 4252 job = list_first_entry_or_null(&ring->sched.pending_list, 4253 struct drm_sched_job, list); 4254 spin_unlock(&ring->sched.job_list_lock); 4255 if (job) 4256 return true; 4257 } 4258 return false; 4259 } 4260 4261 /** 4262 * amdgpu_device_should_recover_gpu - check if we should try GPU recovery 4263 * 4264 * @adev: amdgpu_device pointer 4265 * 4266 * Check amdgpu_gpu_recovery and SRIOV status to see if we should try to recover 4267 * a hung GPU. 4268 */ 4269 bool amdgpu_device_should_recover_gpu(struct amdgpu_device *adev) 4270 { 4271 if (!amdgpu_device_ip_check_soft_reset(adev)) { 4272 dev_info(adev->dev, "Timeout, but no hardware hang detected.\n"); 4273 return false; 4274 } 4275 4276 if (amdgpu_gpu_recovery == 0) 4277 goto disabled; 4278 4279 if (amdgpu_sriov_vf(adev)) 4280 return true; 4281 4282 if (amdgpu_gpu_recovery == -1) { 4283 switch (adev->asic_type) { 4284 case CHIP_BONAIRE: 4285 case CHIP_HAWAII: 4286 case CHIP_TOPAZ: 4287 case CHIP_TONGA: 4288 case CHIP_FIJI: 4289 case CHIP_POLARIS10: 4290 case CHIP_POLARIS11: 4291 case CHIP_POLARIS12: 4292 case CHIP_VEGAM: 4293 case CHIP_VEGA20: 4294 case CHIP_VEGA10: 4295 case CHIP_VEGA12: 4296 case CHIP_RAVEN: 4297 case CHIP_ARCTURUS: 4298 case CHIP_RENOIR: 4299 case CHIP_NAVI10: 4300 case CHIP_NAVI14: 4301 case CHIP_NAVI12: 4302 case CHIP_SIENNA_CICHLID: 4303 case CHIP_NAVY_FLOUNDER: 4304 case CHIP_DIMGREY_CAVEFISH: 4305 case CHIP_VANGOGH: 4306 case CHIP_ALDEBARAN: 4307 break; 4308 default: 4309 goto disabled; 4310 } 4311 } 4312 4313 return true; 4314 4315 disabled: 4316 dev_info(adev->dev, "GPU recovery disabled.\n"); 4317 return false; 4318 } 4319 4320 int amdgpu_device_mode1_reset(struct amdgpu_device *adev) 4321 { 4322 u32 i; 4323 int ret = 0; 4324 4325 amdgpu_atombios_scratch_regs_engine_hung(adev, true); 4326 4327 dev_info(adev->dev, "GPU mode1 reset\n"); 4328 4329 /* disable BM */ 4330 pci_clear_master(adev->pdev); 4331 4332 amdgpu_device_cache_pci_state(adev->pdev); 4333 4334 if (amdgpu_dpm_is_mode1_reset_supported(adev)) { 4335 dev_info(adev->dev, "GPU smu mode1 reset\n"); 4336 ret = amdgpu_dpm_mode1_reset(adev); 4337 } else { 4338 dev_info(adev->dev, "GPU psp mode1 reset\n"); 4339 ret = psp_gpu_reset(adev); 4340 } 4341 4342 if (ret) 4343 dev_err(adev->dev, "GPU mode1 reset failed\n"); 4344 4345 amdgpu_device_load_pci_state(adev->pdev); 4346 4347 /* wait for asic to come out of reset */ 4348 for (i = 0; i < adev->usec_timeout; i++) { 4349 u32 memsize = adev->nbio.funcs->get_memsize(adev); 4350 4351 if (memsize != 0xffffffff) 4352 break; 4353 udelay(1); 4354 } 4355 4356 amdgpu_atombios_scratch_regs_engine_hung(adev, false); 4357 return ret; 4358 } 4359 4360 int amdgpu_device_pre_asic_reset(struct amdgpu_device *adev, 4361 struct amdgpu_reset_context *reset_context) 4362 { 4363 int i, r = 0; 4364 struct amdgpu_job *job = NULL; 4365 bool need_full_reset = 4366 test_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags); 4367 4368 if (reset_context->reset_req_dev == adev) 4369 job = reset_context->job; 4370 4371 /* no need to dump if device is not in good state during probe period */ 4372 if (!adev->gmc.xgmi.pending_reset) 4373 amdgpu_debugfs_wait_dump(adev); 4374 4375 if (amdgpu_sriov_vf(adev)) { 4376 /* stop the data exchange thread */ 4377 amdgpu_virt_fini_data_exchange(adev); 4378 } 4379 4380 /* block all schedulers and reset given job's ring */ 4381 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) { 4382 struct amdgpu_ring *ring = adev->rings[i]; 4383 4384 if (!ring || !ring->sched.thread) 4385 continue; 4386 4387 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */ 4388 amdgpu_fence_driver_force_completion(ring); 4389 } 4390 4391 if(job) 4392 drm_sched_increase_karma(&job->base); 4393 4394 r = amdgpu_reset_prepare_hwcontext(adev, reset_context); 4395 /* If reset handler not implemented, continue; otherwise return */ 4396 if (r == -ENOSYS) 4397 r = 0; 4398 else 4399 return r; 4400 4401 /* Don't suspend on bare metal if we are not going to HW reset the ASIC */ 4402 if (!amdgpu_sriov_vf(adev)) { 4403 4404 if (!need_full_reset) 4405 need_full_reset = amdgpu_device_ip_need_full_reset(adev); 4406 4407 if (!need_full_reset) { 4408 amdgpu_device_ip_pre_soft_reset(adev); 4409 r = amdgpu_device_ip_soft_reset(adev); 4410 amdgpu_device_ip_post_soft_reset(adev); 4411 if (r || amdgpu_device_ip_check_soft_reset(adev)) { 4412 dev_info(adev->dev, "soft reset failed, will fallback to full reset!\n"); 4413 need_full_reset = true; 4414 } 4415 } 4416 4417 if (need_full_reset) 4418 r = amdgpu_device_ip_suspend(adev); 4419 if (need_full_reset) 4420 set_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags); 4421 else 4422 clear_bit(AMDGPU_NEED_FULL_RESET, 4423 &reset_context->flags); 4424 } 4425 4426 return r; 4427 } 4428 4429 int amdgpu_do_asic_reset(struct list_head *device_list_handle, 4430 struct amdgpu_reset_context *reset_context) 4431 { 4432 struct amdgpu_device *tmp_adev = NULL; 4433 bool need_full_reset, skip_hw_reset, vram_lost = false; 4434 int r = 0; 4435 4436 /* Try reset handler method first */ 4437 tmp_adev = list_first_entry(device_list_handle, struct amdgpu_device, 4438 reset_list); 4439 r = amdgpu_reset_perform_reset(tmp_adev, reset_context); 4440 /* If reset handler not implemented, continue; otherwise return */ 4441 if (r == -ENOSYS) 4442 r = 0; 4443 else 4444 return r; 4445 4446 /* Reset handler not implemented, use the default method */ 4447 need_full_reset = 4448 test_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags); 4449 skip_hw_reset = test_bit(AMDGPU_SKIP_HW_RESET, &reset_context->flags); 4450 4451 /* 4452 * ASIC reset has to be done on all XGMI hive nodes ASAP 4453 * to allow proper links negotiation in FW (within 1 sec) 4454 */ 4455 if (!skip_hw_reset && need_full_reset) { 4456 list_for_each_entry(tmp_adev, device_list_handle, reset_list) { 4457 /* For XGMI run all resets in parallel to speed up the process */ 4458 if (tmp_adev->gmc.xgmi.num_physical_nodes > 1) { 4459 tmp_adev->gmc.xgmi.pending_reset = false; 4460 if (!queue_work(system_unbound_wq, &tmp_adev->xgmi_reset_work)) 4461 r = -EALREADY; 4462 } else 4463 r = amdgpu_asic_reset(tmp_adev); 4464 4465 if (r) { 4466 dev_err(tmp_adev->dev, "ASIC reset failed with error, %d for drm dev, %s", 4467 r, adev_to_drm(tmp_adev)->unique); 4468 break; 4469 } 4470 } 4471 4472 /* For XGMI wait for all resets to complete before proceed */ 4473 if (!r) { 4474 list_for_each_entry(tmp_adev, device_list_handle, reset_list) { 4475 if (tmp_adev->gmc.xgmi.num_physical_nodes > 1) { 4476 flush_work(&tmp_adev->xgmi_reset_work); 4477 r = tmp_adev->asic_reset_res; 4478 if (r) 4479 break; 4480 } 4481 } 4482 } 4483 } 4484 4485 if (!r && amdgpu_ras_intr_triggered()) { 4486 list_for_each_entry(tmp_adev, device_list_handle, reset_list) { 4487 if (tmp_adev->mmhub.ras_funcs && 4488 tmp_adev->mmhub.ras_funcs->reset_ras_error_count) 4489 tmp_adev->mmhub.ras_funcs->reset_ras_error_count(tmp_adev); 4490 } 4491 4492 amdgpu_ras_intr_cleared(); 4493 } 4494 4495 list_for_each_entry(tmp_adev, device_list_handle, reset_list) { 4496 if (need_full_reset) { 4497 /* post card */ 4498 r = amdgpu_device_asic_init(tmp_adev); 4499 if (r) { 4500 dev_warn(tmp_adev->dev, "asic atom init failed!"); 4501 } else { 4502 dev_info(tmp_adev->dev, "GPU reset succeeded, trying to resume\n"); 4503 r = amdgpu_device_ip_resume_phase1(tmp_adev); 4504 if (r) 4505 goto out; 4506 4507 vram_lost = amdgpu_device_check_vram_lost(tmp_adev); 4508 if (vram_lost) { 4509 DRM_INFO("VRAM is lost due to GPU reset!\n"); 4510 amdgpu_inc_vram_lost(tmp_adev); 4511 } 4512 4513 r = amdgpu_gtt_mgr_recover(ttm_manager_type(&tmp_adev->mman.bdev, TTM_PL_TT)); 4514 if (r) 4515 goto out; 4516 4517 r = amdgpu_device_fw_loading(tmp_adev); 4518 if (r) 4519 return r; 4520 4521 r = amdgpu_device_ip_resume_phase2(tmp_adev); 4522 if (r) 4523 goto out; 4524 4525 if (vram_lost) 4526 amdgpu_device_fill_reset_magic(tmp_adev); 4527 4528 /* 4529 * Add this ASIC as tracked as reset was already 4530 * complete successfully. 4531 */ 4532 amdgpu_register_gpu_instance(tmp_adev); 4533 4534 if (!reset_context->hive && 4535 tmp_adev->gmc.xgmi.num_physical_nodes > 1) 4536 amdgpu_xgmi_add_device(tmp_adev); 4537 4538 r = amdgpu_device_ip_late_init(tmp_adev); 4539 if (r) 4540 goto out; 4541 4542 amdgpu_fbdev_set_suspend(tmp_adev, 0); 4543 4544 /* 4545 * The GPU enters bad state once faulty pages 4546 * by ECC has reached the threshold, and ras 4547 * recovery is scheduled next. So add one check 4548 * here to break recovery if it indeed exceeds 4549 * bad page threshold, and remind user to 4550 * retire this GPU or setting one bigger 4551 * bad_page_threshold value to fix this once 4552 * probing driver again. 4553 */ 4554 if (!amdgpu_ras_eeprom_check_err_threshold(tmp_adev)) { 4555 /* must succeed. */ 4556 amdgpu_ras_resume(tmp_adev); 4557 } else { 4558 r = -EINVAL; 4559 goto out; 4560 } 4561 4562 /* Update PSP FW topology after reset */ 4563 if (reset_context->hive && 4564 tmp_adev->gmc.xgmi.num_physical_nodes > 1) 4565 r = amdgpu_xgmi_update_topology( 4566 reset_context->hive, tmp_adev); 4567 } 4568 } 4569 4570 out: 4571 if (!r) { 4572 amdgpu_irq_gpu_reset_resume_helper(tmp_adev); 4573 r = amdgpu_ib_ring_tests(tmp_adev); 4574 if (r) { 4575 dev_err(tmp_adev->dev, "ib ring test failed (%d).\n", r); 4576 need_full_reset = true; 4577 r = -EAGAIN; 4578 goto end; 4579 } 4580 } 4581 4582 if (!r) 4583 r = amdgpu_device_recover_vram(tmp_adev); 4584 else 4585 tmp_adev->asic_reset_res = r; 4586 } 4587 4588 end: 4589 if (need_full_reset) 4590 set_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags); 4591 else 4592 clear_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags); 4593 return r; 4594 } 4595 4596 static bool amdgpu_device_lock_adev(struct amdgpu_device *adev, 4597 struct amdgpu_hive_info *hive) 4598 { 4599 if (atomic_cmpxchg(&adev->in_gpu_reset, 0, 1) != 0) 4600 return false; 4601 4602 if (hive) { 4603 down_write_nest_lock(&adev->reset_sem, &hive->hive_lock); 4604 } else { 4605 down_write(&adev->reset_sem); 4606 } 4607 4608 switch (amdgpu_asic_reset_method(adev)) { 4609 case AMD_RESET_METHOD_MODE1: 4610 adev->mp1_state = PP_MP1_STATE_SHUTDOWN; 4611 break; 4612 case AMD_RESET_METHOD_MODE2: 4613 adev->mp1_state = PP_MP1_STATE_RESET; 4614 break; 4615 default: 4616 adev->mp1_state = PP_MP1_STATE_NONE; 4617 break; 4618 } 4619 4620 return true; 4621 } 4622 4623 static void amdgpu_device_unlock_adev(struct amdgpu_device *adev) 4624 { 4625 amdgpu_vf_error_trans_all(adev); 4626 adev->mp1_state = PP_MP1_STATE_NONE; 4627 atomic_set(&adev->in_gpu_reset, 0); 4628 up_write(&adev->reset_sem); 4629 } 4630 4631 /* 4632 * to lockup a list of amdgpu devices in a hive safely, if not a hive 4633 * with multiple nodes, it will be similar as amdgpu_device_lock_adev. 4634 * 4635 * unlock won't require roll back. 4636 */ 4637 static int amdgpu_device_lock_hive_adev(struct amdgpu_device *adev, struct amdgpu_hive_info *hive) 4638 { 4639 struct amdgpu_device *tmp_adev = NULL; 4640 4641 if (adev->gmc.xgmi.num_physical_nodes > 1) { 4642 if (!hive) { 4643 dev_err(adev->dev, "Hive is NULL while device has multiple xgmi nodes"); 4644 return -ENODEV; 4645 } 4646 list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head) { 4647 if (!amdgpu_device_lock_adev(tmp_adev, hive)) 4648 goto roll_back; 4649 } 4650 } else if (!amdgpu_device_lock_adev(adev, hive)) 4651 return -EAGAIN; 4652 4653 return 0; 4654 roll_back: 4655 if (!list_is_first(&tmp_adev->gmc.xgmi.head, &hive->device_list)) { 4656 /* 4657 * if the lockup iteration break in the middle of a hive, 4658 * it may means there may has a race issue, 4659 * or a hive device locked up independently. 4660 * we may be in trouble and may not, so will try to roll back 4661 * the lock and give out a warnning. 4662 */ 4663 dev_warn(tmp_adev->dev, "Hive lock iteration broke in the middle. Rolling back to unlock"); 4664 list_for_each_entry_continue_reverse(tmp_adev, &hive->device_list, gmc.xgmi.head) { 4665 amdgpu_device_unlock_adev(tmp_adev); 4666 } 4667 } 4668 return -EAGAIN; 4669 } 4670 4671 static void amdgpu_device_resume_display_audio(struct amdgpu_device *adev) 4672 { 4673 struct pci_dev *p = NULL; 4674 4675 p = pci_get_domain_bus_and_slot(pci_domain_nr(adev->pdev->bus), 4676 adev->pdev->bus->number, 1); 4677 if (p) { 4678 pm_runtime_enable(&(p->dev)); 4679 pm_runtime_resume(&(p->dev)); 4680 } 4681 } 4682 4683 static int amdgpu_device_suspend_display_audio(struct amdgpu_device *adev) 4684 { 4685 enum amd_reset_method reset_method; 4686 struct pci_dev *p = NULL; 4687 u64 expires; 4688 4689 /* 4690 * For now, only BACO and mode1 reset are confirmed 4691 * to suffer the audio issue without proper suspended. 4692 */ 4693 reset_method = amdgpu_asic_reset_method(adev); 4694 if ((reset_method != AMD_RESET_METHOD_BACO) && 4695 (reset_method != AMD_RESET_METHOD_MODE1)) 4696 return -EINVAL; 4697 4698 p = pci_get_domain_bus_and_slot(pci_domain_nr(adev->pdev->bus), 4699 adev->pdev->bus->number, 1); 4700 if (!p) 4701 return -ENODEV; 4702 4703 expires = pm_runtime_autosuspend_expiration(&(p->dev)); 4704 if (!expires) 4705 /* 4706 * If we cannot get the audio device autosuspend delay, 4707 * a fixed 4S interval will be used. Considering 3S is 4708 * the audio controller default autosuspend delay setting. 4709 * 4S used here is guaranteed to cover that. 4710 */ 4711 expires = ktime_get_mono_fast_ns() + NSEC_PER_SEC * 4ULL; 4712 4713 while (!pm_runtime_status_suspended(&(p->dev))) { 4714 if (!pm_runtime_suspend(&(p->dev))) 4715 break; 4716 4717 if (expires < ktime_get_mono_fast_ns()) { 4718 dev_warn(adev->dev, "failed to suspend display audio\n"); 4719 /* TODO: abort the succeeding gpu reset? */ 4720 return -ETIMEDOUT; 4721 } 4722 } 4723 4724 pm_runtime_disable(&(p->dev)); 4725 4726 return 0; 4727 } 4728 4729 static void amdgpu_device_recheck_guilty_jobs( 4730 struct amdgpu_device *adev, struct list_head *device_list_handle, 4731 struct amdgpu_reset_context *reset_context) 4732 { 4733 int i, r = 0; 4734 4735 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) { 4736 struct amdgpu_ring *ring = adev->rings[i]; 4737 int ret = 0; 4738 struct drm_sched_job *s_job; 4739 4740 if (!ring || !ring->sched.thread) 4741 continue; 4742 4743 s_job = list_first_entry_or_null(&ring->sched.pending_list, 4744 struct drm_sched_job, list); 4745 if (s_job == NULL) 4746 continue; 4747 4748 /* clear job's guilty and depend the folowing step to decide the real one */ 4749 drm_sched_reset_karma(s_job); 4750 drm_sched_resubmit_jobs_ext(&ring->sched, 1); 4751 4752 ret = dma_fence_wait_timeout(s_job->s_fence->parent, false, ring->sched.timeout); 4753 if (ret == 0) { /* timeout */ 4754 DRM_ERROR("Found the real bad job! ring:%s, job_id:%llx\n", 4755 ring->sched.name, s_job->id); 4756 4757 /* set guilty */ 4758 drm_sched_increase_karma(s_job); 4759 retry: 4760 /* do hw reset */ 4761 if (amdgpu_sriov_vf(adev)) { 4762 amdgpu_virt_fini_data_exchange(adev); 4763 r = amdgpu_device_reset_sriov(adev, false); 4764 if (r) 4765 adev->asic_reset_res = r; 4766 } else { 4767 clear_bit(AMDGPU_SKIP_HW_RESET, 4768 &reset_context->flags); 4769 r = amdgpu_do_asic_reset(device_list_handle, 4770 reset_context); 4771 if (r && r == -EAGAIN) 4772 goto retry; 4773 } 4774 4775 /* 4776 * add reset counter so that the following 4777 * resubmitted job could flush vmid 4778 */ 4779 atomic_inc(&adev->gpu_reset_counter); 4780 continue; 4781 } 4782 4783 /* got the hw fence, signal finished fence */ 4784 atomic_dec(ring->sched.score); 4785 dma_fence_get(&s_job->s_fence->finished); 4786 dma_fence_signal(&s_job->s_fence->finished); 4787 dma_fence_put(&s_job->s_fence->finished); 4788 4789 /* remove node from list and free the job */ 4790 spin_lock(&ring->sched.job_list_lock); 4791 list_del_init(&s_job->list); 4792 spin_unlock(&ring->sched.job_list_lock); 4793 ring->sched.ops->free_job(s_job); 4794 } 4795 } 4796 4797 /** 4798 * amdgpu_device_gpu_recover - reset the asic and recover scheduler 4799 * 4800 * @adev: amdgpu_device pointer 4801 * @job: which job trigger hang 4802 * 4803 * Attempt to reset the GPU if it has hung (all asics). 4804 * Attempt to do soft-reset or full-reset and reinitialize Asic 4805 * Returns 0 for success or an error on failure. 4806 */ 4807 4808 int amdgpu_device_gpu_recover(struct amdgpu_device *adev, 4809 struct amdgpu_job *job) 4810 { 4811 struct list_head device_list, *device_list_handle = NULL; 4812 bool job_signaled = false; 4813 struct amdgpu_hive_info *hive = NULL; 4814 struct amdgpu_device *tmp_adev = NULL; 4815 int i, r = 0; 4816 bool need_emergency_restart = false; 4817 bool audio_suspended = false; 4818 int tmp_vram_lost_counter; 4819 struct amdgpu_reset_context reset_context; 4820 4821 memset(&reset_context, 0, sizeof(reset_context)); 4822 4823 /* 4824 * Special case: RAS triggered and full reset isn't supported 4825 */ 4826 need_emergency_restart = amdgpu_ras_need_emergency_restart(adev); 4827 4828 /* 4829 * Flush RAM to disk so that after reboot 4830 * the user can read log and see why the system rebooted. 4831 */ 4832 if (need_emergency_restart && amdgpu_ras_get_context(adev)->reboot) { 4833 DRM_WARN("Emergency reboot."); 4834 4835 ksys_sync_helper(); 4836 emergency_restart(); 4837 } 4838 4839 dev_info(adev->dev, "GPU %s begin!\n", 4840 need_emergency_restart ? "jobs stop":"reset"); 4841 4842 /* 4843 * Here we trylock to avoid chain of resets executing from 4844 * either trigger by jobs on different adevs in XGMI hive or jobs on 4845 * different schedulers for same device while this TO handler is running. 4846 * We always reset all schedulers for device and all devices for XGMI 4847 * hive so that should take care of them too. 4848 */ 4849 hive = amdgpu_get_xgmi_hive(adev); 4850 if (hive) { 4851 if (atomic_cmpxchg(&hive->in_reset, 0, 1) != 0) { 4852 DRM_INFO("Bailing on TDR for s_job:%llx, hive: %llx as another already in progress", 4853 job ? job->base.id : -1, hive->hive_id); 4854 amdgpu_put_xgmi_hive(hive); 4855 if (job) 4856 drm_sched_increase_karma(&job->base); 4857 return 0; 4858 } 4859 mutex_lock(&hive->hive_lock); 4860 } 4861 4862 reset_context.method = AMD_RESET_METHOD_NONE; 4863 reset_context.reset_req_dev = adev; 4864 reset_context.job = job; 4865 reset_context.hive = hive; 4866 clear_bit(AMDGPU_NEED_FULL_RESET, &reset_context.flags); 4867 4868 /* 4869 * lock the device before we try to operate the linked list 4870 * if didn't get the device lock, don't touch the linked list since 4871 * others may iterating it. 4872 */ 4873 r = amdgpu_device_lock_hive_adev(adev, hive); 4874 if (r) { 4875 dev_info(adev->dev, "Bailing on TDR for s_job:%llx, as another already in progress", 4876 job ? job->base.id : -1); 4877 4878 /* even we skipped this reset, still need to set the job to guilty */ 4879 if (job) 4880 drm_sched_increase_karma(&job->base); 4881 goto skip_recovery; 4882 } 4883 4884 /* 4885 * Build list of devices to reset. 4886 * In case we are in XGMI hive mode, resort the device list 4887 * to put adev in the 1st position. 4888 */ 4889 INIT_LIST_HEAD(&device_list); 4890 if (adev->gmc.xgmi.num_physical_nodes > 1) { 4891 list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head) 4892 list_add_tail(&tmp_adev->reset_list, &device_list); 4893 if (!list_is_first(&adev->reset_list, &device_list)) 4894 list_rotate_to_front(&adev->reset_list, &device_list); 4895 device_list_handle = &device_list; 4896 } else { 4897 list_add_tail(&adev->reset_list, &device_list); 4898 device_list_handle = &device_list; 4899 } 4900 4901 /* block all schedulers and reset given job's ring */ 4902 list_for_each_entry(tmp_adev, device_list_handle, reset_list) { 4903 /* 4904 * Try to put the audio codec into suspend state 4905 * before gpu reset started. 4906 * 4907 * Due to the power domain of the graphics device 4908 * is shared with AZ power domain. Without this, 4909 * we may change the audio hardware from behind 4910 * the audio driver's back. That will trigger 4911 * some audio codec errors. 4912 */ 4913 if (!amdgpu_device_suspend_display_audio(tmp_adev)) 4914 audio_suspended = true; 4915 4916 amdgpu_ras_set_error_query_ready(tmp_adev, false); 4917 4918 cancel_delayed_work_sync(&tmp_adev->delayed_init_work); 4919 4920 if (!amdgpu_sriov_vf(tmp_adev)) 4921 amdgpu_amdkfd_pre_reset(tmp_adev); 4922 4923 /* 4924 * Mark these ASICs to be reseted as untracked first 4925 * And add them back after reset completed 4926 */ 4927 amdgpu_unregister_gpu_instance(tmp_adev); 4928 4929 amdgpu_fbdev_set_suspend(tmp_adev, 1); 4930 4931 /* disable ras on ALL IPs */ 4932 if (!need_emergency_restart && 4933 amdgpu_device_ip_need_full_reset(tmp_adev)) 4934 amdgpu_ras_suspend(tmp_adev); 4935 4936 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) { 4937 struct amdgpu_ring *ring = tmp_adev->rings[i]; 4938 4939 if (!ring || !ring->sched.thread) 4940 continue; 4941 4942 drm_sched_stop(&ring->sched, job ? &job->base : NULL); 4943 4944 if (need_emergency_restart) 4945 amdgpu_job_stop_all_jobs_on_sched(&ring->sched); 4946 } 4947 atomic_inc(&tmp_adev->gpu_reset_counter); 4948 } 4949 4950 if (need_emergency_restart) 4951 goto skip_sched_resume; 4952 4953 /* 4954 * Must check guilty signal here since after this point all old 4955 * HW fences are force signaled. 4956 * 4957 * job->base holds a reference to parent fence 4958 */ 4959 if (job && job->base.s_fence->parent && 4960 dma_fence_is_signaled(job->base.s_fence->parent)) { 4961 job_signaled = true; 4962 dev_info(adev->dev, "Guilty job already signaled, skipping HW reset"); 4963 goto skip_hw_reset; 4964 } 4965 4966 retry: /* Rest of adevs pre asic reset from XGMI hive. */ 4967 list_for_each_entry(tmp_adev, device_list_handle, reset_list) { 4968 r = amdgpu_device_pre_asic_reset(tmp_adev, &reset_context); 4969 /*TODO Should we stop ?*/ 4970 if (r) { 4971 dev_err(tmp_adev->dev, "GPU pre asic reset failed with err, %d for drm dev, %s ", 4972 r, adev_to_drm(tmp_adev)->unique); 4973 tmp_adev->asic_reset_res = r; 4974 } 4975 } 4976 4977 tmp_vram_lost_counter = atomic_read(&((adev)->vram_lost_counter)); 4978 /* Actual ASIC resets if needed.*/ 4979 /* TODO Implement XGMI hive reset logic for SRIOV */ 4980 if (amdgpu_sriov_vf(adev)) { 4981 r = amdgpu_device_reset_sriov(adev, job ? false : true); 4982 if (r) 4983 adev->asic_reset_res = r; 4984 } else { 4985 r = amdgpu_do_asic_reset(device_list_handle, &reset_context); 4986 if (r && r == -EAGAIN) 4987 goto retry; 4988 } 4989 4990 skip_hw_reset: 4991 4992 /* Post ASIC reset for all devs .*/ 4993 list_for_each_entry(tmp_adev, device_list_handle, reset_list) { 4994 4995 /* 4996 * Sometimes a later bad compute job can block a good gfx job as gfx 4997 * and compute ring share internal GC HW mutually. We add an additional 4998 * guilty jobs recheck step to find the real guilty job, it synchronously 4999 * submits and pends for the first job being signaled. If it gets timeout, 5000 * we identify it as a real guilty job. 5001 */ 5002 if (amdgpu_gpu_recovery == 2 && 5003 !(tmp_vram_lost_counter < atomic_read(&adev->vram_lost_counter))) 5004 amdgpu_device_recheck_guilty_jobs( 5005 tmp_adev, device_list_handle, &reset_context); 5006 5007 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) { 5008 struct amdgpu_ring *ring = tmp_adev->rings[i]; 5009 5010 if (!ring || !ring->sched.thread) 5011 continue; 5012 5013 /* No point to resubmit jobs if we didn't HW reset*/ 5014 if (!tmp_adev->asic_reset_res && !job_signaled) 5015 drm_sched_resubmit_jobs(&ring->sched); 5016 5017 drm_sched_start(&ring->sched, !tmp_adev->asic_reset_res); 5018 } 5019 5020 if (!amdgpu_device_has_dc_support(tmp_adev) && !job_signaled) { 5021 drm_helper_resume_force_mode(adev_to_drm(tmp_adev)); 5022 } 5023 5024 tmp_adev->asic_reset_res = 0; 5025 5026 if (r) { 5027 /* bad news, how to tell it to userspace ? */ 5028 dev_info(tmp_adev->dev, "GPU reset(%d) failed\n", atomic_read(&tmp_adev->gpu_reset_counter)); 5029 amdgpu_vf_error_put(tmp_adev, AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, r); 5030 } else { 5031 dev_info(tmp_adev->dev, "GPU reset(%d) succeeded!\n", atomic_read(&tmp_adev->gpu_reset_counter)); 5032 if (amdgpu_acpi_smart_shift_update(adev_to_drm(tmp_adev), AMDGPU_SS_DEV_D0)) 5033 DRM_WARN("smart shift update failed\n"); 5034 } 5035 } 5036 5037 skip_sched_resume: 5038 list_for_each_entry(tmp_adev, device_list_handle, reset_list) { 5039 /* unlock kfd: SRIOV would do it separately */ 5040 if (!need_emergency_restart && !amdgpu_sriov_vf(tmp_adev)) 5041 amdgpu_amdkfd_post_reset(tmp_adev); 5042 5043 /* kfd_post_reset will do nothing if kfd device is not initialized, 5044 * need to bring up kfd here if it's not be initialized before 5045 */ 5046 if (!adev->kfd.init_complete) 5047 amdgpu_amdkfd_device_init(adev); 5048 5049 if (audio_suspended) 5050 amdgpu_device_resume_display_audio(tmp_adev); 5051 amdgpu_device_unlock_adev(tmp_adev); 5052 } 5053 5054 skip_recovery: 5055 if (hive) { 5056 atomic_set(&hive->in_reset, 0); 5057 mutex_unlock(&hive->hive_lock); 5058 amdgpu_put_xgmi_hive(hive); 5059 } 5060 5061 if (r && r != -EAGAIN) 5062 dev_info(adev->dev, "GPU reset end with ret = %d\n", r); 5063 return r; 5064 } 5065 5066 /** 5067 * amdgpu_device_get_pcie_info - fence pcie info about the PCIE slot 5068 * 5069 * @adev: amdgpu_device pointer 5070 * 5071 * Fetchs and stores in the driver the PCIE capabilities (gen speed 5072 * and lanes) of the slot the device is in. Handles APUs and 5073 * virtualized environments where PCIE config space may not be available. 5074 */ 5075 static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev) 5076 { 5077 struct pci_dev *pdev; 5078 enum pci_bus_speed speed_cap, platform_speed_cap; 5079 enum pcie_link_width platform_link_width; 5080 5081 if (amdgpu_pcie_gen_cap) 5082 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap; 5083 5084 if (amdgpu_pcie_lane_cap) 5085 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap; 5086 5087 /* covers APUs as well */ 5088 if (pci_is_root_bus(adev->pdev->bus)) { 5089 if (adev->pm.pcie_gen_mask == 0) 5090 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK; 5091 if (adev->pm.pcie_mlw_mask == 0) 5092 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK; 5093 return; 5094 } 5095 5096 if (adev->pm.pcie_gen_mask && adev->pm.pcie_mlw_mask) 5097 return; 5098 5099 pcie_bandwidth_available(adev->pdev, NULL, 5100 &platform_speed_cap, &platform_link_width); 5101 5102 if (adev->pm.pcie_gen_mask == 0) { 5103 /* asic caps */ 5104 pdev = adev->pdev; 5105 speed_cap = pcie_get_speed_cap(pdev); 5106 if (speed_cap == PCI_SPEED_UNKNOWN) { 5107 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 | 5108 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 | 5109 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3); 5110 } else { 5111 if (speed_cap == PCIE_SPEED_32_0GT) 5112 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 | 5113 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 | 5114 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3 | 5115 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN4 | 5116 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN5); 5117 else if (speed_cap == PCIE_SPEED_16_0GT) 5118 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 | 5119 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 | 5120 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3 | 5121 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN4); 5122 else if (speed_cap == PCIE_SPEED_8_0GT) 5123 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 | 5124 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 | 5125 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3); 5126 else if (speed_cap == PCIE_SPEED_5_0GT) 5127 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 | 5128 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2); 5129 else 5130 adev->pm.pcie_gen_mask |= CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1; 5131 } 5132 /* platform caps */ 5133 if (platform_speed_cap == PCI_SPEED_UNKNOWN) { 5134 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 | 5135 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2); 5136 } else { 5137 if (platform_speed_cap == PCIE_SPEED_32_0GT) 5138 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 | 5139 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 | 5140 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3 | 5141 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4 | 5142 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN5); 5143 else if (platform_speed_cap == PCIE_SPEED_16_0GT) 5144 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 | 5145 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 | 5146 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3 | 5147 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4); 5148 else if (platform_speed_cap == PCIE_SPEED_8_0GT) 5149 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 | 5150 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 | 5151 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3); 5152 else if (platform_speed_cap == PCIE_SPEED_5_0GT) 5153 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 | 5154 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2); 5155 else 5156 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1; 5157 5158 } 5159 } 5160 if (adev->pm.pcie_mlw_mask == 0) { 5161 if (platform_link_width == PCIE_LNK_WIDTH_UNKNOWN) { 5162 adev->pm.pcie_mlw_mask |= AMDGPU_DEFAULT_PCIE_MLW_MASK; 5163 } else { 5164 switch (platform_link_width) { 5165 case PCIE_LNK_X32: 5166 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 | 5167 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 | 5168 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 | 5169 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 | 5170 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 | 5171 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 | 5172 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1); 5173 break; 5174 case PCIE_LNK_X16: 5175 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 | 5176 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 | 5177 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 | 5178 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 | 5179 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 | 5180 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1); 5181 break; 5182 case PCIE_LNK_X12: 5183 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 | 5184 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 | 5185 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 | 5186 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 | 5187 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1); 5188 break; 5189 case PCIE_LNK_X8: 5190 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 | 5191 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 | 5192 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 | 5193 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1); 5194 break; 5195 case PCIE_LNK_X4: 5196 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 | 5197 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 | 5198 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1); 5199 break; 5200 case PCIE_LNK_X2: 5201 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 | 5202 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1); 5203 break; 5204 case PCIE_LNK_X1: 5205 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1; 5206 break; 5207 default: 5208 break; 5209 } 5210 } 5211 } 5212 } 5213 5214 int amdgpu_device_baco_enter(struct drm_device *dev) 5215 { 5216 struct amdgpu_device *adev = drm_to_adev(dev); 5217 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev); 5218 5219 if (!amdgpu_device_supports_baco(adev_to_drm(adev))) 5220 return -ENOTSUPP; 5221 5222 if (ras && adev->ras_enabled && 5223 adev->nbio.funcs->enable_doorbell_interrupt) 5224 adev->nbio.funcs->enable_doorbell_interrupt(adev, false); 5225 5226 return amdgpu_dpm_baco_enter(adev); 5227 } 5228 5229 int amdgpu_device_baco_exit(struct drm_device *dev) 5230 { 5231 struct amdgpu_device *adev = drm_to_adev(dev); 5232 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev); 5233 int ret = 0; 5234 5235 if (!amdgpu_device_supports_baco(adev_to_drm(adev))) 5236 return -ENOTSUPP; 5237 5238 ret = amdgpu_dpm_baco_exit(adev); 5239 if (ret) 5240 return ret; 5241 5242 if (ras && adev->ras_enabled && 5243 adev->nbio.funcs->enable_doorbell_interrupt) 5244 adev->nbio.funcs->enable_doorbell_interrupt(adev, true); 5245 5246 return 0; 5247 } 5248 5249 static void amdgpu_cancel_all_tdr(struct amdgpu_device *adev) 5250 { 5251 int i; 5252 5253 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) { 5254 struct amdgpu_ring *ring = adev->rings[i]; 5255 5256 if (!ring || !ring->sched.thread) 5257 continue; 5258 5259 cancel_delayed_work_sync(&ring->sched.work_tdr); 5260 } 5261 } 5262 5263 /** 5264 * amdgpu_pci_error_detected - Called when a PCI error is detected. 5265 * @pdev: PCI device struct 5266 * @state: PCI channel state 5267 * 5268 * Description: Called when a PCI error is detected. 5269 * 5270 * Return: PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT. 5271 */ 5272 pci_ers_result_t amdgpu_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state) 5273 { 5274 struct drm_device *dev = pci_get_drvdata(pdev); 5275 struct amdgpu_device *adev = drm_to_adev(dev); 5276 int i; 5277 5278 DRM_INFO("PCI error: detected callback, state(%d)!!\n", state); 5279 5280 if (adev->gmc.xgmi.num_physical_nodes > 1) { 5281 DRM_WARN("No support for XGMI hive yet..."); 5282 return PCI_ERS_RESULT_DISCONNECT; 5283 } 5284 5285 switch (state) { 5286 case pci_channel_io_normal: 5287 return PCI_ERS_RESULT_CAN_RECOVER; 5288 /* Fatal error, prepare for slot reset */ 5289 case pci_channel_io_frozen: 5290 /* 5291 * Cancel and wait for all TDRs in progress if failing to 5292 * set adev->in_gpu_reset in amdgpu_device_lock_adev 5293 * 5294 * Locking adev->reset_sem will prevent any external access 5295 * to GPU during PCI error recovery 5296 */ 5297 while (!amdgpu_device_lock_adev(adev, NULL)) 5298 amdgpu_cancel_all_tdr(adev); 5299 5300 /* 5301 * Block any work scheduling as we do for regular GPU reset 5302 * for the duration of the recovery 5303 */ 5304 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) { 5305 struct amdgpu_ring *ring = adev->rings[i]; 5306 5307 if (!ring || !ring->sched.thread) 5308 continue; 5309 5310 drm_sched_stop(&ring->sched, NULL); 5311 } 5312 atomic_inc(&adev->gpu_reset_counter); 5313 return PCI_ERS_RESULT_NEED_RESET; 5314 case pci_channel_io_perm_failure: 5315 /* Permanent error, prepare for device removal */ 5316 return PCI_ERS_RESULT_DISCONNECT; 5317 } 5318 5319 return PCI_ERS_RESULT_NEED_RESET; 5320 } 5321 5322 /** 5323 * amdgpu_pci_mmio_enabled - Enable MMIO and dump debug registers 5324 * @pdev: pointer to PCI device 5325 */ 5326 pci_ers_result_t amdgpu_pci_mmio_enabled(struct pci_dev *pdev) 5327 { 5328 5329 DRM_INFO("PCI error: mmio enabled callback!!\n"); 5330 5331 /* TODO - dump whatever for debugging purposes */ 5332 5333 /* This called only if amdgpu_pci_error_detected returns 5334 * PCI_ERS_RESULT_CAN_RECOVER. Read/write to the device still 5335 * works, no need to reset slot. 5336 */ 5337 5338 return PCI_ERS_RESULT_RECOVERED; 5339 } 5340 5341 /** 5342 * amdgpu_pci_slot_reset - Called when PCI slot has been reset. 5343 * @pdev: PCI device struct 5344 * 5345 * Description: This routine is called by the pci error recovery 5346 * code after the PCI slot has been reset, just before we 5347 * should resume normal operations. 5348 */ 5349 pci_ers_result_t amdgpu_pci_slot_reset(struct pci_dev *pdev) 5350 { 5351 struct drm_device *dev = pci_get_drvdata(pdev); 5352 struct amdgpu_device *adev = drm_to_adev(dev); 5353 int r, i; 5354 struct amdgpu_reset_context reset_context; 5355 u32 memsize; 5356 struct list_head device_list; 5357 5358 DRM_INFO("PCI error: slot reset callback!!\n"); 5359 5360 memset(&reset_context, 0, sizeof(reset_context)); 5361 5362 INIT_LIST_HEAD(&device_list); 5363 list_add_tail(&adev->reset_list, &device_list); 5364 5365 /* wait for asic to come out of reset */ 5366 msleep(500); 5367 5368 /* Restore PCI confspace */ 5369 amdgpu_device_load_pci_state(pdev); 5370 5371 /* confirm ASIC came out of reset */ 5372 for (i = 0; i < adev->usec_timeout; i++) { 5373 memsize = amdgpu_asic_get_config_memsize(adev); 5374 5375 if (memsize != 0xffffffff) 5376 break; 5377 udelay(1); 5378 } 5379 if (memsize == 0xffffffff) { 5380 r = -ETIME; 5381 goto out; 5382 } 5383 5384 reset_context.method = AMD_RESET_METHOD_NONE; 5385 reset_context.reset_req_dev = adev; 5386 set_bit(AMDGPU_NEED_FULL_RESET, &reset_context.flags); 5387 set_bit(AMDGPU_SKIP_HW_RESET, &reset_context.flags); 5388 5389 adev->no_hw_access = true; 5390 r = amdgpu_device_pre_asic_reset(adev, &reset_context); 5391 adev->no_hw_access = false; 5392 if (r) 5393 goto out; 5394 5395 r = amdgpu_do_asic_reset(&device_list, &reset_context); 5396 5397 out: 5398 if (!r) { 5399 if (amdgpu_device_cache_pci_state(adev->pdev)) 5400 pci_restore_state(adev->pdev); 5401 5402 DRM_INFO("PCIe error recovery succeeded\n"); 5403 } else { 5404 DRM_ERROR("PCIe error recovery failed, err:%d", r); 5405 amdgpu_device_unlock_adev(adev); 5406 } 5407 5408 return r ? PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_RECOVERED; 5409 } 5410 5411 /** 5412 * amdgpu_pci_resume() - resume normal ops after PCI reset 5413 * @pdev: pointer to PCI device 5414 * 5415 * Called when the error recovery driver tells us that its 5416 * OK to resume normal operation. 5417 */ 5418 void amdgpu_pci_resume(struct pci_dev *pdev) 5419 { 5420 struct drm_device *dev = pci_get_drvdata(pdev); 5421 struct amdgpu_device *adev = drm_to_adev(dev); 5422 int i; 5423 5424 5425 DRM_INFO("PCI error: resume callback!!\n"); 5426 5427 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) { 5428 struct amdgpu_ring *ring = adev->rings[i]; 5429 5430 if (!ring || !ring->sched.thread) 5431 continue; 5432 5433 5434 drm_sched_resubmit_jobs(&ring->sched); 5435 drm_sched_start(&ring->sched, true); 5436 } 5437 5438 amdgpu_device_unlock_adev(adev); 5439 } 5440 5441 bool amdgpu_device_cache_pci_state(struct pci_dev *pdev) 5442 { 5443 struct drm_device *dev = pci_get_drvdata(pdev); 5444 struct amdgpu_device *adev = drm_to_adev(dev); 5445 int r; 5446 5447 r = pci_save_state(pdev); 5448 if (!r) { 5449 kfree(adev->pci_state); 5450 5451 adev->pci_state = pci_store_saved_state(pdev); 5452 5453 if (!adev->pci_state) { 5454 DRM_ERROR("Failed to store PCI saved state"); 5455 return false; 5456 } 5457 } else { 5458 DRM_WARN("Failed to save PCI state, err:%d\n", r); 5459 return false; 5460 } 5461 5462 return true; 5463 } 5464 5465 bool amdgpu_device_load_pci_state(struct pci_dev *pdev) 5466 { 5467 struct drm_device *dev = pci_get_drvdata(pdev); 5468 struct amdgpu_device *adev = drm_to_adev(dev); 5469 int r; 5470 5471 if (!adev->pci_state) 5472 return false; 5473 5474 r = pci_load_saved_state(pdev, adev->pci_state); 5475 5476 if (!r) { 5477 pci_restore_state(pdev); 5478 } else { 5479 DRM_WARN("Failed to load PCI state, err:%d\n", r); 5480 return false; 5481 } 5482 5483 return true; 5484 } 5485 5486 void amdgpu_device_flush_hdp(struct amdgpu_device *adev, 5487 struct amdgpu_ring *ring) 5488 { 5489 #ifdef CONFIG_X86_64 5490 if (adev->flags & AMD_IS_APU) 5491 return; 5492 #endif 5493 if (adev->gmc.xgmi.connected_to_cpu) 5494 return; 5495 5496 if (ring && ring->funcs->emit_hdp_flush) 5497 amdgpu_ring_emit_hdp_flush(ring); 5498 else 5499 amdgpu_asic_flush_hdp(adev, ring); 5500 } 5501 5502 void amdgpu_device_invalidate_hdp(struct amdgpu_device *adev, 5503 struct amdgpu_ring *ring) 5504 { 5505 #ifdef CONFIG_X86_64 5506 if (adev->flags & AMD_IS_APU) 5507 return; 5508 #endif 5509 if (adev->gmc.xgmi.connected_to_cpu) 5510 return; 5511 5512 amdgpu_asic_invalidate_hdp(adev, ring); 5513 } 5514