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 68 MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin"); 69 MODULE_FIRMWARE("amdgpu/vega12_gpu_info.bin"); 70 MODULE_FIRMWARE("amdgpu/raven_gpu_info.bin"); 71 MODULE_FIRMWARE("amdgpu/picasso_gpu_info.bin"); 72 MODULE_FIRMWARE("amdgpu/raven2_gpu_info.bin"); 73 MODULE_FIRMWARE("amdgpu/arcturus_gpu_info.bin"); 74 MODULE_FIRMWARE("amdgpu/navi10_gpu_info.bin"); 75 MODULE_FIRMWARE("amdgpu/navi14_gpu_info.bin"); 76 77 #define AMDGPU_RESUME_MS 2000 78 79 static const char *amdgpu_asic_name[] = { 80 "TAHITI", 81 "PITCAIRN", 82 "VERDE", 83 "OLAND", 84 "HAINAN", 85 "BONAIRE", 86 "KAVERI", 87 "KABINI", 88 "HAWAII", 89 "MULLINS", 90 "TOPAZ", 91 "TONGA", 92 "FIJI", 93 "CARRIZO", 94 "STONEY", 95 "POLARIS10", 96 "POLARIS11", 97 "POLARIS12", 98 "VEGAM", 99 "VEGA10", 100 "VEGA12", 101 "VEGA20", 102 "RAVEN", 103 "ARCTURUS", 104 "NAVI10", 105 "NAVI14", 106 "LAST", 107 }; 108 109 /** 110 * DOC: pcie_replay_count 111 * 112 * The amdgpu driver provides a sysfs API for reporting the total number 113 * of PCIe replays (NAKs) 114 * The file pcie_replay_count is used for this and returns the total 115 * number of replays as a sum of the NAKs generated and NAKs received 116 */ 117 118 static ssize_t amdgpu_device_get_pcie_replay_count(struct device *dev, 119 struct device_attribute *attr, char *buf) 120 { 121 struct drm_device *ddev = dev_get_drvdata(dev); 122 struct amdgpu_device *adev = ddev->dev_private; 123 uint64_t cnt = amdgpu_asic_get_pcie_replay_count(adev); 124 125 return snprintf(buf, PAGE_SIZE, "%llu\n", cnt); 126 } 127 128 static DEVICE_ATTR(pcie_replay_count, S_IRUGO, 129 amdgpu_device_get_pcie_replay_count, NULL); 130 131 static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev); 132 133 /** 134 * amdgpu_device_is_px - Is the device is a dGPU with HG/PX power control 135 * 136 * @dev: drm_device pointer 137 * 138 * Returns true if the device is a dGPU with HG/PX power control, 139 * otherwise return false. 140 */ 141 bool amdgpu_device_is_px(struct drm_device *dev) 142 { 143 struct amdgpu_device *adev = dev->dev_private; 144 145 if (adev->flags & AMD_IS_PX) 146 return true; 147 return false; 148 } 149 150 /* 151 * MMIO register access helper functions. 152 */ 153 /** 154 * amdgpu_mm_rreg - read a memory mapped IO register 155 * 156 * @adev: amdgpu_device pointer 157 * @reg: dword aligned register offset 158 * @acc_flags: access flags which require special behavior 159 * 160 * Returns the 32 bit value from the offset specified. 161 */ 162 uint32_t amdgpu_mm_rreg(struct amdgpu_device *adev, uint32_t reg, 163 uint32_t acc_flags) 164 { 165 uint32_t ret; 166 167 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev)) 168 return amdgpu_virt_kiq_rreg(adev, reg); 169 170 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX)) 171 ret = readl(((void __iomem *)adev->rmmio) + (reg * 4)); 172 else { 173 unsigned long flags; 174 175 spin_lock_irqsave(&adev->mmio_idx_lock, flags); 176 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4)); 177 ret = readl(((void __iomem *)adev->rmmio) + (mmMM_DATA * 4)); 178 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags); 179 } 180 trace_amdgpu_mm_rreg(adev->pdev->device, reg, ret); 181 return ret; 182 } 183 184 /* 185 * MMIO register read with bytes helper functions 186 * @offset:bytes offset from MMIO start 187 * 188 */ 189 190 /** 191 * amdgpu_mm_rreg8 - read a memory mapped IO register 192 * 193 * @adev: amdgpu_device pointer 194 * @offset: byte aligned register offset 195 * 196 * Returns the 8 bit value from the offset specified. 197 */ 198 uint8_t amdgpu_mm_rreg8(struct amdgpu_device *adev, uint32_t offset) { 199 if (offset < adev->rmmio_size) 200 return (readb(adev->rmmio + offset)); 201 BUG(); 202 } 203 204 /* 205 * MMIO register write with bytes helper functions 206 * @offset:bytes offset from MMIO start 207 * @value: the value want to be written to the register 208 * 209 */ 210 /** 211 * amdgpu_mm_wreg8 - read a memory mapped IO register 212 * 213 * @adev: amdgpu_device pointer 214 * @offset: byte aligned register offset 215 * @value: 8 bit value to write 216 * 217 * Writes the value specified to the offset specified. 218 */ 219 void amdgpu_mm_wreg8(struct amdgpu_device *adev, uint32_t offset, uint8_t value) { 220 if (offset < adev->rmmio_size) 221 writeb(value, adev->rmmio + offset); 222 else 223 BUG(); 224 } 225 226 /** 227 * amdgpu_mm_wreg - write to a memory mapped IO register 228 * 229 * @adev: amdgpu_device pointer 230 * @reg: dword aligned register offset 231 * @v: 32 bit value to write to the register 232 * @acc_flags: access flags which require special behavior 233 * 234 * Writes the value specified to the offset specified. 235 */ 236 void amdgpu_mm_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v, 237 uint32_t acc_flags) 238 { 239 trace_amdgpu_mm_wreg(adev->pdev->device, reg, v); 240 241 if (adev->asic_type >= CHIP_VEGA10 && reg == 0) { 242 adev->last_mm_index = v; 243 } 244 245 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev)) 246 return amdgpu_virt_kiq_wreg(adev, reg, v); 247 248 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX)) 249 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4)); 250 else { 251 unsigned long flags; 252 253 spin_lock_irqsave(&adev->mmio_idx_lock, flags); 254 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4)); 255 writel(v, ((void __iomem *)adev->rmmio) + (mmMM_DATA * 4)); 256 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags); 257 } 258 259 if (adev->asic_type >= CHIP_VEGA10 && reg == 1 && adev->last_mm_index == 0x5702C) { 260 udelay(500); 261 } 262 } 263 264 /** 265 * amdgpu_io_rreg - read an IO register 266 * 267 * @adev: amdgpu_device pointer 268 * @reg: dword aligned register offset 269 * 270 * Returns the 32 bit value from the offset specified. 271 */ 272 u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 reg) 273 { 274 if ((reg * 4) < adev->rio_mem_size) 275 return ioread32(adev->rio_mem + (reg * 4)); 276 else { 277 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4)); 278 return ioread32(adev->rio_mem + (mmMM_DATA * 4)); 279 } 280 } 281 282 /** 283 * amdgpu_io_wreg - write to an IO register 284 * 285 * @adev: amdgpu_device pointer 286 * @reg: dword aligned register offset 287 * @v: 32 bit value to write to the register 288 * 289 * Writes the value specified to the offset specified. 290 */ 291 void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v) 292 { 293 if (adev->asic_type >= CHIP_VEGA10 && reg == 0) { 294 adev->last_mm_index = v; 295 } 296 297 if ((reg * 4) < adev->rio_mem_size) 298 iowrite32(v, adev->rio_mem + (reg * 4)); 299 else { 300 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4)); 301 iowrite32(v, adev->rio_mem + (mmMM_DATA * 4)); 302 } 303 304 if (adev->asic_type >= CHIP_VEGA10 && reg == 1 && adev->last_mm_index == 0x5702C) { 305 udelay(500); 306 } 307 } 308 309 /** 310 * amdgpu_mm_rdoorbell - read a doorbell dword 311 * 312 * @adev: amdgpu_device pointer 313 * @index: doorbell index 314 * 315 * Returns the value in the doorbell aperture at the 316 * requested doorbell index (CIK). 317 */ 318 u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index) 319 { 320 if (index < adev->doorbell.num_doorbells) { 321 return readl(adev->doorbell.ptr + index); 322 } else { 323 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index); 324 return 0; 325 } 326 } 327 328 /** 329 * amdgpu_mm_wdoorbell - write a doorbell dword 330 * 331 * @adev: amdgpu_device pointer 332 * @index: doorbell index 333 * @v: value to write 334 * 335 * Writes @v to the doorbell aperture at the 336 * requested doorbell index (CIK). 337 */ 338 void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v) 339 { 340 if (index < adev->doorbell.num_doorbells) { 341 writel(v, adev->doorbell.ptr + index); 342 } else { 343 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index); 344 } 345 } 346 347 /** 348 * amdgpu_mm_rdoorbell64 - read a doorbell Qword 349 * 350 * @adev: amdgpu_device pointer 351 * @index: doorbell index 352 * 353 * Returns the value in the doorbell aperture at the 354 * requested doorbell index (VEGA10+). 355 */ 356 u64 amdgpu_mm_rdoorbell64(struct amdgpu_device *adev, u32 index) 357 { 358 if (index < adev->doorbell.num_doorbells) { 359 return atomic64_read((atomic64_t *)(adev->doorbell.ptr + index)); 360 } else { 361 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index); 362 return 0; 363 } 364 } 365 366 /** 367 * amdgpu_mm_wdoorbell64 - write a doorbell Qword 368 * 369 * @adev: amdgpu_device pointer 370 * @index: doorbell index 371 * @v: value to write 372 * 373 * Writes @v to the doorbell aperture at the 374 * requested doorbell index (VEGA10+). 375 */ 376 void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v) 377 { 378 if (index < adev->doorbell.num_doorbells) { 379 atomic64_set((atomic64_t *)(adev->doorbell.ptr + index), v); 380 } else { 381 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index); 382 } 383 } 384 385 /** 386 * amdgpu_invalid_rreg - dummy reg read function 387 * 388 * @adev: amdgpu device pointer 389 * @reg: offset of register 390 * 391 * Dummy register read function. Used for register blocks 392 * that certain asics don't have (all asics). 393 * Returns the value in the register. 394 */ 395 static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg) 396 { 397 DRM_ERROR("Invalid callback to read register 0x%04X\n", reg); 398 BUG(); 399 return 0; 400 } 401 402 /** 403 * amdgpu_invalid_wreg - dummy reg write function 404 * 405 * @adev: amdgpu device pointer 406 * @reg: offset of register 407 * @v: value to write to the register 408 * 409 * Dummy register read function. Used for register blocks 410 * that certain asics don't have (all asics). 411 */ 412 static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v) 413 { 414 DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n", 415 reg, v); 416 BUG(); 417 } 418 419 /** 420 * amdgpu_block_invalid_rreg - dummy reg read function 421 * 422 * @adev: amdgpu device pointer 423 * @block: offset of instance 424 * @reg: offset of register 425 * 426 * Dummy register read function. Used for register blocks 427 * that certain asics don't have (all asics). 428 * Returns the value in the register. 429 */ 430 static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev, 431 uint32_t block, uint32_t reg) 432 { 433 DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n", 434 reg, block); 435 BUG(); 436 return 0; 437 } 438 439 /** 440 * amdgpu_block_invalid_wreg - dummy reg write function 441 * 442 * @adev: amdgpu device pointer 443 * @block: offset of instance 444 * @reg: offset of register 445 * @v: value to write to the register 446 * 447 * Dummy register read function. Used for register blocks 448 * that certain asics don't have (all asics). 449 */ 450 static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev, 451 uint32_t block, 452 uint32_t reg, uint32_t v) 453 { 454 DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n", 455 reg, block, v); 456 BUG(); 457 } 458 459 /** 460 * amdgpu_device_vram_scratch_init - allocate the VRAM scratch page 461 * 462 * @adev: amdgpu device pointer 463 * 464 * Allocates a scratch page of VRAM for use by various things in the 465 * driver. 466 */ 467 static int amdgpu_device_vram_scratch_init(struct amdgpu_device *adev) 468 { 469 return amdgpu_bo_create_kernel(adev, AMDGPU_GPU_PAGE_SIZE, 470 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM, 471 &adev->vram_scratch.robj, 472 &adev->vram_scratch.gpu_addr, 473 (void **)&adev->vram_scratch.ptr); 474 } 475 476 /** 477 * amdgpu_device_vram_scratch_fini - Free the VRAM scratch page 478 * 479 * @adev: amdgpu device pointer 480 * 481 * Frees the VRAM scratch page. 482 */ 483 static void amdgpu_device_vram_scratch_fini(struct amdgpu_device *adev) 484 { 485 amdgpu_bo_free_kernel(&adev->vram_scratch.robj, NULL, NULL); 486 } 487 488 /** 489 * amdgpu_device_program_register_sequence - program an array of registers. 490 * 491 * @adev: amdgpu_device pointer 492 * @registers: pointer to the register array 493 * @array_size: size of the register array 494 * 495 * Programs an array or registers with and and or masks. 496 * This is a helper for setting golden registers. 497 */ 498 void amdgpu_device_program_register_sequence(struct amdgpu_device *adev, 499 const u32 *registers, 500 const u32 array_size) 501 { 502 u32 tmp, reg, and_mask, or_mask; 503 int i; 504 505 if (array_size % 3) 506 return; 507 508 for (i = 0; i < array_size; i +=3) { 509 reg = registers[i + 0]; 510 and_mask = registers[i + 1]; 511 or_mask = registers[i + 2]; 512 513 if (and_mask == 0xffffffff) { 514 tmp = or_mask; 515 } else { 516 tmp = RREG32(reg); 517 tmp &= ~and_mask; 518 if (adev->family >= AMDGPU_FAMILY_AI) 519 tmp |= (or_mask & and_mask); 520 else 521 tmp |= or_mask; 522 } 523 WREG32(reg, tmp); 524 } 525 } 526 527 /** 528 * amdgpu_device_pci_config_reset - reset the GPU 529 * 530 * @adev: amdgpu_device pointer 531 * 532 * Resets the GPU using the pci config reset sequence. 533 * Only applicable to asics prior to vega10. 534 */ 535 void amdgpu_device_pci_config_reset(struct amdgpu_device *adev) 536 { 537 pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA); 538 } 539 540 /* 541 * GPU doorbell aperture helpers function. 542 */ 543 /** 544 * amdgpu_device_doorbell_init - Init doorbell driver information. 545 * 546 * @adev: amdgpu_device pointer 547 * 548 * Init doorbell driver information (CIK) 549 * Returns 0 on success, error on failure. 550 */ 551 static int amdgpu_device_doorbell_init(struct amdgpu_device *adev) 552 { 553 554 /* No doorbell on SI hardware generation */ 555 if (adev->asic_type < CHIP_BONAIRE) { 556 adev->doorbell.base = 0; 557 adev->doorbell.size = 0; 558 adev->doorbell.num_doorbells = 0; 559 adev->doorbell.ptr = NULL; 560 return 0; 561 } 562 563 if (pci_resource_flags(adev->pdev, 2) & IORESOURCE_UNSET) 564 return -EINVAL; 565 566 amdgpu_asic_init_doorbell_index(adev); 567 568 /* doorbell bar mapping */ 569 adev->doorbell.base = pci_resource_start(adev->pdev, 2); 570 adev->doorbell.size = pci_resource_len(adev->pdev, 2); 571 572 adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32), 573 adev->doorbell_index.max_assignment+1); 574 if (adev->doorbell.num_doorbells == 0) 575 return -EINVAL; 576 577 /* For Vega, reserve and map two pages on doorbell BAR since SDMA 578 * paging queue doorbell use the second page. The 579 * AMDGPU_DOORBELL64_MAX_ASSIGNMENT definition assumes all the 580 * doorbells are in the first page. So with paging queue enabled, 581 * the max num_doorbells should + 1 page (0x400 in dword) 582 */ 583 if (adev->asic_type >= CHIP_VEGA10) 584 adev->doorbell.num_doorbells += 0x400; 585 586 adev->doorbell.ptr = ioremap(adev->doorbell.base, 587 adev->doorbell.num_doorbells * 588 sizeof(u32)); 589 if (adev->doorbell.ptr == NULL) 590 return -ENOMEM; 591 592 return 0; 593 } 594 595 /** 596 * amdgpu_device_doorbell_fini - Tear down doorbell driver information. 597 * 598 * @adev: amdgpu_device pointer 599 * 600 * Tear down doorbell driver information (CIK) 601 */ 602 static void amdgpu_device_doorbell_fini(struct amdgpu_device *adev) 603 { 604 iounmap(adev->doorbell.ptr); 605 adev->doorbell.ptr = NULL; 606 } 607 608 609 610 /* 611 * amdgpu_device_wb_*() 612 * Writeback is the method by which the GPU updates special pages in memory 613 * with the status of certain GPU events (fences, ring pointers,etc.). 614 */ 615 616 /** 617 * amdgpu_device_wb_fini - Disable Writeback and free memory 618 * 619 * @adev: amdgpu_device pointer 620 * 621 * Disables Writeback and frees the Writeback memory (all asics). 622 * Used at driver shutdown. 623 */ 624 static void amdgpu_device_wb_fini(struct amdgpu_device *adev) 625 { 626 if (adev->wb.wb_obj) { 627 amdgpu_bo_free_kernel(&adev->wb.wb_obj, 628 &adev->wb.gpu_addr, 629 (void **)&adev->wb.wb); 630 adev->wb.wb_obj = NULL; 631 } 632 } 633 634 /** 635 * amdgpu_device_wb_init- Init Writeback driver info and allocate memory 636 * 637 * @adev: amdgpu_device pointer 638 * 639 * Initializes writeback and allocates writeback memory (all asics). 640 * Used at driver startup. 641 * Returns 0 on success or an -error on failure. 642 */ 643 static int amdgpu_device_wb_init(struct amdgpu_device *adev) 644 { 645 int r; 646 647 if (adev->wb.wb_obj == NULL) { 648 /* AMDGPU_MAX_WB * sizeof(uint32_t) * 8 = AMDGPU_MAX_WB 256bit slots */ 649 r = amdgpu_bo_create_kernel(adev, AMDGPU_MAX_WB * sizeof(uint32_t) * 8, 650 PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT, 651 &adev->wb.wb_obj, &adev->wb.gpu_addr, 652 (void **)&adev->wb.wb); 653 if (r) { 654 dev_warn(adev->dev, "(%d) create WB bo failed\n", r); 655 return r; 656 } 657 658 adev->wb.num_wb = AMDGPU_MAX_WB; 659 memset(&adev->wb.used, 0, sizeof(adev->wb.used)); 660 661 /* clear wb memory */ 662 memset((char *)adev->wb.wb, 0, AMDGPU_MAX_WB * sizeof(uint32_t) * 8); 663 } 664 665 return 0; 666 } 667 668 /** 669 * amdgpu_device_wb_get - Allocate a wb entry 670 * 671 * @adev: amdgpu_device pointer 672 * @wb: wb index 673 * 674 * Allocate a wb slot for use by the driver (all asics). 675 * Returns 0 on success or -EINVAL on failure. 676 */ 677 int amdgpu_device_wb_get(struct amdgpu_device *adev, u32 *wb) 678 { 679 unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb); 680 681 if (offset < adev->wb.num_wb) { 682 __set_bit(offset, adev->wb.used); 683 *wb = offset << 3; /* convert to dw offset */ 684 return 0; 685 } else { 686 return -EINVAL; 687 } 688 } 689 690 /** 691 * amdgpu_device_wb_free - Free a wb entry 692 * 693 * @adev: amdgpu_device pointer 694 * @wb: wb index 695 * 696 * Free a wb slot allocated for use by the driver (all asics) 697 */ 698 void amdgpu_device_wb_free(struct amdgpu_device *adev, u32 wb) 699 { 700 wb >>= 3; 701 if (wb < adev->wb.num_wb) 702 __clear_bit(wb, adev->wb.used); 703 } 704 705 /** 706 * amdgpu_device_resize_fb_bar - try to resize FB BAR 707 * 708 * @adev: amdgpu_device pointer 709 * 710 * Try to resize FB BAR to make all VRAM CPU accessible. We try very hard not 711 * to fail, but if any of the BARs is not accessible after the size we abort 712 * driver loading by returning -ENODEV. 713 */ 714 int amdgpu_device_resize_fb_bar(struct amdgpu_device *adev) 715 { 716 u64 space_needed = roundup_pow_of_two(adev->gmc.real_vram_size); 717 u32 rbar_size = order_base_2(((space_needed >> 20) | 1)) - 1; 718 struct pci_bus *root; 719 struct resource *res; 720 unsigned i; 721 u16 cmd; 722 int r; 723 724 /* Bypass for VF */ 725 if (amdgpu_sriov_vf(adev)) 726 return 0; 727 728 /* Check if the root BUS has 64bit memory resources */ 729 root = adev->pdev->bus; 730 while (root->parent) 731 root = root->parent; 732 733 pci_bus_for_each_resource(root, res, i) { 734 if (res && res->flags & (IORESOURCE_MEM | IORESOURCE_MEM_64) && 735 res->start > 0x100000000ull) 736 break; 737 } 738 739 /* Trying to resize is pointless without a root hub window above 4GB */ 740 if (!res) 741 return 0; 742 743 /* Disable memory decoding while we change the BAR addresses and size */ 744 pci_read_config_word(adev->pdev, PCI_COMMAND, &cmd); 745 pci_write_config_word(adev->pdev, PCI_COMMAND, 746 cmd & ~PCI_COMMAND_MEMORY); 747 748 /* Free the VRAM and doorbell BAR, we most likely need to move both. */ 749 amdgpu_device_doorbell_fini(adev); 750 if (adev->asic_type >= CHIP_BONAIRE) 751 pci_release_resource(adev->pdev, 2); 752 753 pci_release_resource(adev->pdev, 0); 754 755 r = pci_resize_resource(adev->pdev, 0, rbar_size); 756 if (r == -ENOSPC) 757 DRM_INFO("Not enough PCI address space for a large BAR."); 758 else if (r && r != -ENOTSUPP) 759 DRM_ERROR("Problem resizing BAR0 (%d).", r); 760 761 pci_assign_unassigned_bus_resources(adev->pdev->bus); 762 763 /* When the doorbell or fb BAR isn't available we have no chance of 764 * using the device. 765 */ 766 r = amdgpu_device_doorbell_init(adev); 767 if (r || (pci_resource_flags(adev->pdev, 0) & IORESOURCE_UNSET)) 768 return -ENODEV; 769 770 pci_write_config_word(adev->pdev, PCI_COMMAND, cmd); 771 772 return 0; 773 } 774 775 /* 776 * GPU helpers function. 777 */ 778 /** 779 * amdgpu_device_need_post - check if the hw need post or not 780 * 781 * @adev: amdgpu_device pointer 782 * 783 * Check if the asic has been initialized (all asics) at driver startup 784 * or post is needed if hw reset is performed. 785 * Returns true if need or false if not. 786 */ 787 bool amdgpu_device_need_post(struct amdgpu_device *adev) 788 { 789 uint32_t reg; 790 791 if (amdgpu_sriov_vf(adev)) 792 return false; 793 794 if (amdgpu_passthrough(adev)) { 795 /* for FIJI: In whole GPU pass-through virtualization case, after VM reboot 796 * some old smc fw still need driver do vPost otherwise gpu hang, while 797 * those smc fw version above 22.15 doesn't have this flaw, so we force 798 * vpost executed for smc version below 22.15 799 */ 800 if (adev->asic_type == CHIP_FIJI) { 801 int err; 802 uint32_t fw_ver; 803 err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev); 804 /* force vPost if error occured */ 805 if (err) 806 return true; 807 808 fw_ver = *((uint32_t *)adev->pm.fw->data + 69); 809 if (fw_ver < 0x00160e00) 810 return true; 811 } 812 } 813 814 if (adev->has_hw_reset) { 815 adev->has_hw_reset = false; 816 return true; 817 } 818 819 /* bios scratch used on CIK+ */ 820 if (adev->asic_type >= CHIP_BONAIRE) 821 return amdgpu_atombios_scratch_need_asic_init(adev); 822 823 /* check MEM_SIZE for older asics */ 824 reg = amdgpu_asic_get_config_memsize(adev); 825 826 if ((reg != 0) && (reg != 0xffffffff)) 827 return false; 828 829 return true; 830 } 831 832 /* if we get transitioned to only one device, take VGA back */ 833 /** 834 * amdgpu_device_vga_set_decode - enable/disable vga decode 835 * 836 * @cookie: amdgpu_device pointer 837 * @state: enable/disable vga decode 838 * 839 * Enable/disable vga decode (all asics). 840 * Returns VGA resource flags. 841 */ 842 static unsigned int amdgpu_device_vga_set_decode(void *cookie, bool state) 843 { 844 struct amdgpu_device *adev = cookie; 845 amdgpu_asic_set_vga_state(adev, state); 846 if (state) 847 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM | 848 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM; 849 else 850 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM; 851 } 852 853 /** 854 * amdgpu_device_check_block_size - validate the vm block size 855 * 856 * @adev: amdgpu_device pointer 857 * 858 * Validates the vm block size specified via module parameter. 859 * The vm block size defines number of bits in page table versus page directory, 860 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the 861 * page table and the remaining bits are in the page directory. 862 */ 863 static void amdgpu_device_check_block_size(struct amdgpu_device *adev) 864 { 865 /* defines number of bits in page table versus page directory, 866 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the 867 * page table and the remaining bits are in the page directory */ 868 if (amdgpu_vm_block_size == -1) 869 return; 870 871 if (amdgpu_vm_block_size < 9) { 872 dev_warn(adev->dev, "VM page table size (%d) too small\n", 873 amdgpu_vm_block_size); 874 amdgpu_vm_block_size = -1; 875 } 876 } 877 878 /** 879 * amdgpu_device_check_vm_size - validate the vm size 880 * 881 * @adev: amdgpu_device pointer 882 * 883 * Validates the vm size in GB specified via module parameter. 884 * The VM size is the size of the GPU virtual memory space in GB. 885 */ 886 static void amdgpu_device_check_vm_size(struct amdgpu_device *adev) 887 { 888 /* no need to check the default value */ 889 if (amdgpu_vm_size == -1) 890 return; 891 892 if (amdgpu_vm_size < 1) { 893 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n", 894 amdgpu_vm_size); 895 amdgpu_vm_size = -1; 896 } 897 } 898 899 static void amdgpu_device_check_smu_prv_buffer_size(struct amdgpu_device *adev) 900 { 901 struct sysinfo si; 902 bool is_os_64 = (sizeof(void *) == 8) ? true : false; 903 uint64_t total_memory; 904 uint64_t dram_size_seven_GB = 0x1B8000000; 905 uint64_t dram_size_three_GB = 0xB8000000; 906 907 if (amdgpu_smu_memory_pool_size == 0) 908 return; 909 910 if (!is_os_64) { 911 DRM_WARN("Not 64-bit OS, feature not supported\n"); 912 goto def_value; 913 } 914 si_meminfo(&si); 915 total_memory = (uint64_t)si.totalram * si.mem_unit; 916 917 if ((amdgpu_smu_memory_pool_size == 1) || 918 (amdgpu_smu_memory_pool_size == 2)) { 919 if (total_memory < dram_size_three_GB) 920 goto def_value1; 921 } else if ((amdgpu_smu_memory_pool_size == 4) || 922 (amdgpu_smu_memory_pool_size == 8)) { 923 if (total_memory < dram_size_seven_GB) 924 goto def_value1; 925 } else { 926 DRM_WARN("Smu memory pool size not supported\n"); 927 goto def_value; 928 } 929 adev->pm.smu_prv_buffer_size = amdgpu_smu_memory_pool_size << 28; 930 931 return; 932 933 def_value1: 934 DRM_WARN("No enough system memory\n"); 935 def_value: 936 adev->pm.smu_prv_buffer_size = 0; 937 } 938 939 /** 940 * amdgpu_device_check_arguments - validate module params 941 * 942 * @adev: amdgpu_device pointer 943 * 944 * Validates certain module parameters and updates 945 * the associated values used by the driver (all asics). 946 */ 947 static int amdgpu_device_check_arguments(struct amdgpu_device *adev) 948 { 949 int ret = 0; 950 951 if (amdgpu_sched_jobs < 4) { 952 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n", 953 amdgpu_sched_jobs); 954 amdgpu_sched_jobs = 4; 955 } else if (!is_power_of_2(amdgpu_sched_jobs)){ 956 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n", 957 amdgpu_sched_jobs); 958 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs); 959 } 960 961 if (amdgpu_gart_size != -1 && amdgpu_gart_size < 32) { 962 /* gart size must be greater or equal to 32M */ 963 dev_warn(adev->dev, "gart size (%d) too small\n", 964 amdgpu_gart_size); 965 amdgpu_gart_size = -1; 966 } 967 968 if (amdgpu_gtt_size != -1 && amdgpu_gtt_size < 32) { 969 /* gtt size must be greater or equal to 32M */ 970 dev_warn(adev->dev, "gtt size (%d) too small\n", 971 amdgpu_gtt_size); 972 amdgpu_gtt_size = -1; 973 } 974 975 /* valid range is between 4 and 9 inclusive */ 976 if (amdgpu_vm_fragment_size != -1 && 977 (amdgpu_vm_fragment_size > 9 || amdgpu_vm_fragment_size < 4)) { 978 dev_warn(adev->dev, "valid range is between 4 and 9\n"); 979 amdgpu_vm_fragment_size = -1; 980 } 981 982 amdgpu_device_check_smu_prv_buffer_size(adev); 983 984 amdgpu_device_check_vm_size(adev); 985 986 amdgpu_device_check_block_size(adev); 987 988 ret = amdgpu_device_get_job_timeout_settings(adev); 989 if (ret) { 990 dev_err(adev->dev, "invalid lockup_timeout parameter syntax\n"); 991 return ret; 992 } 993 994 adev->firmware.load_type = amdgpu_ucode_get_load_type(adev, amdgpu_fw_load_type); 995 996 return ret; 997 } 998 999 /** 1000 * amdgpu_switcheroo_set_state - set switcheroo state 1001 * 1002 * @pdev: pci dev pointer 1003 * @state: vga_switcheroo state 1004 * 1005 * Callback for the switcheroo driver. Suspends or resumes the 1006 * the asics before or after it is powered up using ACPI methods. 1007 */ 1008 static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state) 1009 { 1010 struct drm_device *dev = pci_get_drvdata(pdev); 1011 1012 if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF) 1013 return; 1014 1015 if (state == VGA_SWITCHEROO_ON) { 1016 pr_info("amdgpu: switched on\n"); 1017 /* don't suspend or resume card normally */ 1018 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING; 1019 1020 amdgpu_device_resume(dev, true, true); 1021 1022 dev->switch_power_state = DRM_SWITCH_POWER_ON; 1023 drm_kms_helper_poll_enable(dev); 1024 } else { 1025 pr_info("amdgpu: switched off\n"); 1026 drm_kms_helper_poll_disable(dev); 1027 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING; 1028 amdgpu_device_suspend(dev, true, true); 1029 dev->switch_power_state = DRM_SWITCH_POWER_OFF; 1030 } 1031 } 1032 1033 /** 1034 * amdgpu_switcheroo_can_switch - see if switcheroo state can change 1035 * 1036 * @pdev: pci dev pointer 1037 * 1038 * Callback for the switcheroo driver. Check of the switcheroo 1039 * state can be changed. 1040 * Returns true if the state can be changed, false if not. 1041 */ 1042 static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev) 1043 { 1044 struct drm_device *dev = pci_get_drvdata(pdev); 1045 1046 /* 1047 * FIXME: open_count is protected by drm_global_mutex but that would lead to 1048 * locking inversion with the driver load path. And the access here is 1049 * completely racy anyway. So don't bother with locking for now. 1050 */ 1051 return dev->open_count == 0; 1052 } 1053 1054 static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = { 1055 .set_gpu_state = amdgpu_switcheroo_set_state, 1056 .reprobe = NULL, 1057 .can_switch = amdgpu_switcheroo_can_switch, 1058 }; 1059 1060 /** 1061 * amdgpu_device_ip_set_clockgating_state - set the CG state 1062 * 1063 * @dev: amdgpu_device pointer 1064 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.) 1065 * @state: clockgating state (gate or ungate) 1066 * 1067 * Sets the requested clockgating state for all instances of 1068 * the hardware IP specified. 1069 * Returns the error code from the last instance. 1070 */ 1071 int amdgpu_device_ip_set_clockgating_state(void *dev, 1072 enum amd_ip_block_type block_type, 1073 enum amd_clockgating_state state) 1074 { 1075 struct amdgpu_device *adev = dev; 1076 int i, r = 0; 1077 1078 for (i = 0; i < adev->num_ip_blocks; i++) { 1079 if (!adev->ip_blocks[i].status.valid) 1080 continue; 1081 if (adev->ip_blocks[i].version->type != block_type) 1082 continue; 1083 if (!adev->ip_blocks[i].version->funcs->set_clockgating_state) 1084 continue; 1085 r = adev->ip_blocks[i].version->funcs->set_clockgating_state( 1086 (void *)adev, state); 1087 if (r) 1088 DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n", 1089 adev->ip_blocks[i].version->funcs->name, r); 1090 } 1091 return r; 1092 } 1093 1094 /** 1095 * amdgpu_device_ip_set_powergating_state - set the PG state 1096 * 1097 * @dev: amdgpu_device pointer 1098 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.) 1099 * @state: powergating state (gate or ungate) 1100 * 1101 * Sets the requested powergating state for all instances of 1102 * the hardware IP specified. 1103 * Returns the error code from the last instance. 1104 */ 1105 int amdgpu_device_ip_set_powergating_state(void *dev, 1106 enum amd_ip_block_type block_type, 1107 enum amd_powergating_state state) 1108 { 1109 struct amdgpu_device *adev = dev; 1110 int i, r = 0; 1111 1112 for (i = 0; i < adev->num_ip_blocks; i++) { 1113 if (!adev->ip_blocks[i].status.valid) 1114 continue; 1115 if (adev->ip_blocks[i].version->type != block_type) 1116 continue; 1117 if (!adev->ip_blocks[i].version->funcs->set_powergating_state) 1118 continue; 1119 r = adev->ip_blocks[i].version->funcs->set_powergating_state( 1120 (void *)adev, state); 1121 if (r) 1122 DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n", 1123 adev->ip_blocks[i].version->funcs->name, r); 1124 } 1125 return r; 1126 } 1127 1128 /** 1129 * amdgpu_device_ip_get_clockgating_state - get the CG state 1130 * 1131 * @adev: amdgpu_device pointer 1132 * @flags: clockgating feature flags 1133 * 1134 * Walks the list of IPs on the device and updates the clockgating 1135 * flags for each IP. 1136 * Updates @flags with the feature flags for each hardware IP where 1137 * clockgating is enabled. 1138 */ 1139 void amdgpu_device_ip_get_clockgating_state(struct amdgpu_device *adev, 1140 u32 *flags) 1141 { 1142 int i; 1143 1144 for (i = 0; i < adev->num_ip_blocks; i++) { 1145 if (!adev->ip_blocks[i].status.valid) 1146 continue; 1147 if (adev->ip_blocks[i].version->funcs->get_clockgating_state) 1148 adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags); 1149 } 1150 } 1151 1152 /** 1153 * amdgpu_device_ip_wait_for_idle - wait for idle 1154 * 1155 * @adev: amdgpu_device pointer 1156 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.) 1157 * 1158 * Waits for the request hardware IP to be idle. 1159 * Returns 0 for success or a negative error code on failure. 1160 */ 1161 int amdgpu_device_ip_wait_for_idle(struct amdgpu_device *adev, 1162 enum amd_ip_block_type block_type) 1163 { 1164 int i, r; 1165 1166 for (i = 0; i < adev->num_ip_blocks; i++) { 1167 if (!adev->ip_blocks[i].status.valid) 1168 continue; 1169 if (adev->ip_blocks[i].version->type == block_type) { 1170 r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev); 1171 if (r) 1172 return r; 1173 break; 1174 } 1175 } 1176 return 0; 1177 1178 } 1179 1180 /** 1181 * amdgpu_device_ip_is_idle - is the hardware IP idle 1182 * 1183 * @adev: amdgpu_device pointer 1184 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.) 1185 * 1186 * Check if the hardware IP is idle or not. 1187 * Returns true if it the IP is idle, false if not. 1188 */ 1189 bool amdgpu_device_ip_is_idle(struct amdgpu_device *adev, 1190 enum amd_ip_block_type block_type) 1191 { 1192 int i; 1193 1194 for (i = 0; i < adev->num_ip_blocks; i++) { 1195 if (!adev->ip_blocks[i].status.valid) 1196 continue; 1197 if (adev->ip_blocks[i].version->type == block_type) 1198 return adev->ip_blocks[i].version->funcs->is_idle((void *)adev); 1199 } 1200 return true; 1201 1202 } 1203 1204 /** 1205 * amdgpu_device_ip_get_ip_block - get a hw IP pointer 1206 * 1207 * @adev: amdgpu_device pointer 1208 * @type: Type of hardware IP (SMU, GFX, UVD, etc.) 1209 * 1210 * Returns a pointer to the hardware IP block structure 1211 * if it exists for the asic, otherwise NULL. 1212 */ 1213 struct amdgpu_ip_block * 1214 amdgpu_device_ip_get_ip_block(struct amdgpu_device *adev, 1215 enum amd_ip_block_type type) 1216 { 1217 int i; 1218 1219 for (i = 0; i < adev->num_ip_blocks; i++) 1220 if (adev->ip_blocks[i].version->type == type) 1221 return &adev->ip_blocks[i]; 1222 1223 return NULL; 1224 } 1225 1226 /** 1227 * amdgpu_device_ip_block_version_cmp 1228 * 1229 * @adev: amdgpu_device pointer 1230 * @type: enum amd_ip_block_type 1231 * @major: major version 1232 * @minor: minor version 1233 * 1234 * return 0 if equal or greater 1235 * return 1 if smaller or the ip_block doesn't exist 1236 */ 1237 int amdgpu_device_ip_block_version_cmp(struct amdgpu_device *adev, 1238 enum amd_ip_block_type type, 1239 u32 major, u32 minor) 1240 { 1241 struct amdgpu_ip_block *ip_block = amdgpu_device_ip_get_ip_block(adev, type); 1242 1243 if (ip_block && ((ip_block->version->major > major) || 1244 ((ip_block->version->major == major) && 1245 (ip_block->version->minor >= minor)))) 1246 return 0; 1247 1248 return 1; 1249 } 1250 1251 /** 1252 * amdgpu_device_ip_block_add 1253 * 1254 * @adev: amdgpu_device pointer 1255 * @ip_block_version: pointer to the IP to add 1256 * 1257 * Adds the IP block driver information to the collection of IPs 1258 * on the asic. 1259 */ 1260 int amdgpu_device_ip_block_add(struct amdgpu_device *adev, 1261 const struct amdgpu_ip_block_version *ip_block_version) 1262 { 1263 if (!ip_block_version) 1264 return -EINVAL; 1265 1266 DRM_INFO("add ip block number %d <%s>\n", adev->num_ip_blocks, 1267 ip_block_version->funcs->name); 1268 1269 adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version; 1270 1271 return 0; 1272 } 1273 1274 /** 1275 * amdgpu_device_enable_virtual_display - enable virtual display feature 1276 * 1277 * @adev: amdgpu_device pointer 1278 * 1279 * Enabled the virtual display feature if the user has enabled it via 1280 * the module parameter virtual_display. This feature provides a virtual 1281 * display hardware on headless boards or in virtualized environments. 1282 * This function parses and validates the configuration string specified by 1283 * the user and configues the virtual display configuration (number of 1284 * virtual connectors, crtcs, etc.) specified. 1285 */ 1286 static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev) 1287 { 1288 adev->enable_virtual_display = false; 1289 1290 if (amdgpu_virtual_display) { 1291 struct drm_device *ddev = adev->ddev; 1292 const char *pci_address_name = pci_name(ddev->pdev); 1293 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname; 1294 1295 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL); 1296 pciaddstr_tmp = pciaddstr; 1297 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) { 1298 pciaddname = strsep(&pciaddname_tmp, ","); 1299 if (!strcmp("all", pciaddname) 1300 || !strcmp(pci_address_name, pciaddname)) { 1301 long num_crtc; 1302 int res = -1; 1303 1304 adev->enable_virtual_display = true; 1305 1306 if (pciaddname_tmp) 1307 res = kstrtol(pciaddname_tmp, 10, 1308 &num_crtc); 1309 1310 if (!res) { 1311 if (num_crtc < 1) 1312 num_crtc = 1; 1313 if (num_crtc > 6) 1314 num_crtc = 6; 1315 adev->mode_info.num_crtc = num_crtc; 1316 } else { 1317 adev->mode_info.num_crtc = 1; 1318 } 1319 break; 1320 } 1321 } 1322 1323 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n", 1324 amdgpu_virtual_display, pci_address_name, 1325 adev->enable_virtual_display, adev->mode_info.num_crtc); 1326 1327 kfree(pciaddstr); 1328 } 1329 } 1330 1331 /** 1332 * amdgpu_device_parse_gpu_info_fw - parse gpu info firmware 1333 * 1334 * @adev: amdgpu_device pointer 1335 * 1336 * Parses the asic configuration parameters specified in the gpu info 1337 * firmware and makes them availale to the driver for use in configuring 1338 * the asic. 1339 * Returns 0 on success, -EINVAL on failure. 1340 */ 1341 static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev) 1342 { 1343 const char *chip_name; 1344 char fw_name[30]; 1345 int err; 1346 const struct gpu_info_firmware_header_v1_0 *hdr; 1347 1348 adev->firmware.gpu_info_fw = NULL; 1349 1350 switch (adev->asic_type) { 1351 case CHIP_TOPAZ: 1352 case CHIP_TONGA: 1353 case CHIP_FIJI: 1354 case CHIP_POLARIS10: 1355 case CHIP_POLARIS11: 1356 case CHIP_POLARIS12: 1357 case CHIP_VEGAM: 1358 case CHIP_CARRIZO: 1359 case CHIP_STONEY: 1360 #ifdef CONFIG_DRM_AMDGPU_SI 1361 case CHIP_VERDE: 1362 case CHIP_TAHITI: 1363 case CHIP_PITCAIRN: 1364 case CHIP_OLAND: 1365 case CHIP_HAINAN: 1366 #endif 1367 #ifdef CONFIG_DRM_AMDGPU_CIK 1368 case CHIP_BONAIRE: 1369 case CHIP_HAWAII: 1370 case CHIP_KAVERI: 1371 case CHIP_KABINI: 1372 case CHIP_MULLINS: 1373 #endif 1374 case CHIP_VEGA20: 1375 default: 1376 return 0; 1377 case CHIP_VEGA10: 1378 chip_name = "vega10"; 1379 break; 1380 case CHIP_VEGA12: 1381 chip_name = "vega12"; 1382 break; 1383 case CHIP_RAVEN: 1384 if (adev->rev_id >= 8) 1385 chip_name = "raven2"; 1386 else if (adev->pdev->device == 0x15d8) 1387 chip_name = "picasso"; 1388 else 1389 chip_name = "raven"; 1390 break; 1391 case CHIP_ARCTURUS: 1392 chip_name = "arcturus"; 1393 break; 1394 case CHIP_NAVI10: 1395 chip_name = "navi10"; 1396 break; 1397 case CHIP_NAVI14: 1398 chip_name = "navi14"; 1399 break; 1400 } 1401 1402 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name); 1403 err = request_firmware(&adev->firmware.gpu_info_fw, fw_name, adev->dev); 1404 if (err) { 1405 dev_err(adev->dev, 1406 "Failed to load gpu_info firmware \"%s\"\n", 1407 fw_name); 1408 goto out; 1409 } 1410 err = amdgpu_ucode_validate(adev->firmware.gpu_info_fw); 1411 if (err) { 1412 dev_err(adev->dev, 1413 "Failed to validate gpu_info firmware \"%s\"\n", 1414 fw_name); 1415 goto out; 1416 } 1417 1418 hdr = (const struct gpu_info_firmware_header_v1_0 *)adev->firmware.gpu_info_fw->data; 1419 amdgpu_ucode_print_gpu_info_hdr(&hdr->header); 1420 1421 switch (hdr->version_major) { 1422 case 1: 1423 { 1424 const struct gpu_info_firmware_v1_0 *gpu_info_fw = 1425 (const struct gpu_info_firmware_v1_0 *)(adev->firmware.gpu_info_fw->data + 1426 le32_to_cpu(hdr->header.ucode_array_offset_bytes)); 1427 1428 adev->gfx.config.max_shader_engines = le32_to_cpu(gpu_info_fw->gc_num_se); 1429 adev->gfx.config.max_cu_per_sh = le32_to_cpu(gpu_info_fw->gc_num_cu_per_sh); 1430 adev->gfx.config.max_sh_per_se = le32_to_cpu(gpu_info_fw->gc_num_sh_per_se); 1431 adev->gfx.config.max_backends_per_se = le32_to_cpu(gpu_info_fw->gc_num_rb_per_se); 1432 adev->gfx.config.max_texture_channel_caches = 1433 le32_to_cpu(gpu_info_fw->gc_num_tccs); 1434 adev->gfx.config.max_gprs = le32_to_cpu(gpu_info_fw->gc_num_gprs); 1435 adev->gfx.config.max_gs_threads = le32_to_cpu(gpu_info_fw->gc_num_max_gs_thds); 1436 adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gpu_info_fw->gc_gs_table_depth); 1437 adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gpu_info_fw->gc_gsprim_buff_depth); 1438 adev->gfx.config.double_offchip_lds_buf = 1439 le32_to_cpu(gpu_info_fw->gc_double_offchip_lds_buffer); 1440 adev->gfx.cu_info.wave_front_size = le32_to_cpu(gpu_info_fw->gc_wave_size); 1441 adev->gfx.cu_info.max_waves_per_simd = 1442 le32_to_cpu(gpu_info_fw->gc_max_waves_per_simd); 1443 adev->gfx.cu_info.max_scratch_slots_per_cu = 1444 le32_to_cpu(gpu_info_fw->gc_max_scratch_slots_per_cu); 1445 adev->gfx.cu_info.lds_size = le32_to_cpu(gpu_info_fw->gc_lds_size); 1446 if (hdr->version_minor >= 1) { 1447 const struct gpu_info_firmware_v1_1 *gpu_info_fw = 1448 (const struct gpu_info_firmware_v1_1 *)(adev->firmware.gpu_info_fw->data + 1449 le32_to_cpu(hdr->header.ucode_array_offset_bytes)); 1450 adev->gfx.config.num_sc_per_sh = 1451 le32_to_cpu(gpu_info_fw->num_sc_per_sh); 1452 adev->gfx.config.num_packer_per_sc = 1453 le32_to_cpu(gpu_info_fw->num_packer_per_sc); 1454 } 1455 #ifdef CONFIG_DRM_AMD_DC_DCN2_0 1456 if (hdr->version_minor == 2) { 1457 const struct gpu_info_firmware_v1_2 *gpu_info_fw = 1458 (const struct gpu_info_firmware_v1_2 *)(adev->firmware.gpu_info_fw->data + 1459 le32_to_cpu(hdr->header.ucode_array_offset_bytes)); 1460 adev->dm.soc_bounding_box = &gpu_info_fw->soc_bounding_box; 1461 } 1462 #endif 1463 break; 1464 } 1465 default: 1466 dev_err(adev->dev, 1467 "Unsupported gpu_info table %d\n", hdr->header.ucode_version); 1468 err = -EINVAL; 1469 goto out; 1470 } 1471 out: 1472 return err; 1473 } 1474 1475 /** 1476 * amdgpu_device_ip_early_init - run early init for hardware IPs 1477 * 1478 * @adev: amdgpu_device pointer 1479 * 1480 * Early initialization pass for hardware IPs. The hardware IPs that make 1481 * up each asic are discovered each IP's early_init callback is run. This 1482 * is the first stage in initializing the asic. 1483 * Returns 0 on success, negative error code on failure. 1484 */ 1485 static int amdgpu_device_ip_early_init(struct amdgpu_device *adev) 1486 { 1487 int i, r; 1488 1489 amdgpu_device_enable_virtual_display(adev); 1490 1491 switch (adev->asic_type) { 1492 case CHIP_TOPAZ: 1493 case CHIP_TONGA: 1494 case CHIP_FIJI: 1495 case CHIP_POLARIS10: 1496 case CHIP_POLARIS11: 1497 case CHIP_POLARIS12: 1498 case CHIP_VEGAM: 1499 case CHIP_CARRIZO: 1500 case CHIP_STONEY: 1501 if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY) 1502 adev->family = AMDGPU_FAMILY_CZ; 1503 else 1504 adev->family = AMDGPU_FAMILY_VI; 1505 1506 r = vi_set_ip_blocks(adev); 1507 if (r) 1508 return r; 1509 break; 1510 #ifdef CONFIG_DRM_AMDGPU_SI 1511 case CHIP_VERDE: 1512 case CHIP_TAHITI: 1513 case CHIP_PITCAIRN: 1514 case CHIP_OLAND: 1515 case CHIP_HAINAN: 1516 adev->family = AMDGPU_FAMILY_SI; 1517 r = si_set_ip_blocks(adev); 1518 if (r) 1519 return r; 1520 break; 1521 #endif 1522 #ifdef CONFIG_DRM_AMDGPU_CIK 1523 case CHIP_BONAIRE: 1524 case CHIP_HAWAII: 1525 case CHIP_KAVERI: 1526 case CHIP_KABINI: 1527 case CHIP_MULLINS: 1528 if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII)) 1529 adev->family = AMDGPU_FAMILY_CI; 1530 else 1531 adev->family = AMDGPU_FAMILY_KV; 1532 1533 r = cik_set_ip_blocks(adev); 1534 if (r) 1535 return r; 1536 break; 1537 #endif 1538 case CHIP_VEGA10: 1539 case CHIP_VEGA12: 1540 case CHIP_VEGA20: 1541 case CHIP_RAVEN: 1542 case CHIP_ARCTURUS: 1543 if (adev->asic_type == CHIP_RAVEN) 1544 adev->family = AMDGPU_FAMILY_RV; 1545 else 1546 adev->family = AMDGPU_FAMILY_AI; 1547 1548 r = soc15_set_ip_blocks(adev); 1549 if (r) 1550 return r; 1551 break; 1552 case CHIP_NAVI10: 1553 case CHIP_NAVI14: 1554 adev->family = AMDGPU_FAMILY_NV; 1555 1556 r = nv_set_ip_blocks(adev); 1557 if (r) 1558 return r; 1559 break; 1560 default: 1561 /* FIXME: not supported yet */ 1562 return -EINVAL; 1563 } 1564 1565 r = amdgpu_device_parse_gpu_info_fw(adev); 1566 if (r) 1567 return r; 1568 1569 amdgpu_amdkfd_device_probe(adev); 1570 1571 if (amdgpu_sriov_vf(adev)) { 1572 r = amdgpu_virt_request_full_gpu(adev, true); 1573 if (r) 1574 return -EAGAIN; 1575 1576 /* query the reg access mode at the very beginning */ 1577 amdgpu_virt_init_reg_access_mode(adev); 1578 } 1579 1580 adev->pm.pp_feature = amdgpu_pp_feature_mask; 1581 if (amdgpu_sriov_vf(adev)) 1582 adev->pm.pp_feature &= ~PP_GFXOFF_MASK; 1583 1584 for (i = 0; i < adev->num_ip_blocks; i++) { 1585 if ((amdgpu_ip_block_mask & (1 << i)) == 0) { 1586 DRM_ERROR("disabled ip block: %d <%s>\n", 1587 i, adev->ip_blocks[i].version->funcs->name); 1588 adev->ip_blocks[i].status.valid = false; 1589 } else { 1590 if (adev->ip_blocks[i].version->funcs->early_init) { 1591 r = adev->ip_blocks[i].version->funcs->early_init((void *)adev); 1592 if (r == -ENOENT) { 1593 adev->ip_blocks[i].status.valid = false; 1594 } else if (r) { 1595 DRM_ERROR("early_init of IP block <%s> failed %d\n", 1596 adev->ip_blocks[i].version->funcs->name, r); 1597 return r; 1598 } else { 1599 adev->ip_blocks[i].status.valid = true; 1600 } 1601 } else { 1602 adev->ip_blocks[i].status.valid = true; 1603 } 1604 } 1605 /* get the vbios after the asic_funcs are set up */ 1606 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON) { 1607 /* Read BIOS */ 1608 if (!amdgpu_get_bios(adev)) 1609 return -EINVAL; 1610 1611 r = amdgpu_atombios_init(adev); 1612 if (r) { 1613 dev_err(adev->dev, "amdgpu_atombios_init failed\n"); 1614 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_INIT_FAIL, 0, 0); 1615 return r; 1616 } 1617 } 1618 } 1619 1620 adev->cg_flags &= amdgpu_cg_mask; 1621 adev->pg_flags &= amdgpu_pg_mask; 1622 1623 return 0; 1624 } 1625 1626 static int amdgpu_device_ip_hw_init_phase1(struct amdgpu_device *adev) 1627 { 1628 int i, r; 1629 1630 for (i = 0; i < adev->num_ip_blocks; i++) { 1631 if (!adev->ip_blocks[i].status.sw) 1632 continue; 1633 if (adev->ip_blocks[i].status.hw) 1634 continue; 1635 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON || 1636 (amdgpu_sriov_vf(adev) && (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP)) || 1637 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH) { 1638 r = adev->ip_blocks[i].version->funcs->hw_init(adev); 1639 if (r) { 1640 DRM_ERROR("hw_init of IP block <%s> failed %d\n", 1641 adev->ip_blocks[i].version->funcs->name, r); 1642 return r; 1643 } 1644 adev->ip_blocks[i].status.hw = true; 1645 } 1646 } 1647 1648 return 0; 1649 } 1650 1651 static int amdgpu_device_ip_hw_init_phase2(struct amdgpu_device *adev) 1652 { 1653 int i, r; 1654 1655 for (i = 0; i < adev->num_ip_blocks; i++) { 1656 if (!adev->ip_blocks[i].status.sw) 1657 continue; 1658 if (adev->ip_blocks[i].status.hw) 1659 continue; 1660 r = adev->ip_blocks[i].version->funcs->hw_init(adev); 1661 if (r) { 1662 DRM_ERROR("hw_init of IP block <%s> failed %d\n", 1663 adev->ip_blocks[i].version->funcs->name, r); 1664 return r; 1665 } 1666 adev->ip_blocks[i].status.hw = true; 1667 } 1668 1669 return 0; 1670 } 1671 1672 static int amdgpu_device_fw_loading(struct amdgpu_device *adev) 1673 { 1674 int r = 0; 1675 int i; 1676 uint32_t smu_version; 1677 1678 if (adev->asic_type >= CHIP_VEGA10) { 1679 for (i = 0; i < adev->num_ip_blocks; i++) { 1680 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) { 1681 if (adev->in_gpu_reset || adev->in_suspend) { 1682 if (amdgpu_sriov_vf(adev) && adev->in_gpu_reset) 1683 break; /* sriov gpu reset, psp need to do hw_init before IH because of hw limit */ 1684 r = adev->ip_blocks[i].version->funcs->resume(adev); 1685 if (r) { 1686 DRM_ERROR("resume of IP block <%s> failed %d\n", 1687 adev->ip_blocks[i].version->funcs->name, r); 1688 return r; 1689 } 1690 } else { 1691 r = adev->ip_blocks[i].version->funcs->hw_init(adev); 1692 if (r) { 1693 DRM_ERROR("hw_init of IP block <%s> failed %d\n", 1694 adev->ip_blocks[i].version->funcs->name, r); 1695 return r; 1696 } 1697 } 1698 adev->ip_blocks[i].status.hw = true; 1699 } 1700 } 1701 } 1702 r = amdgpu_pm_load_smu_firmware(adev, &smu_version); 1703 1704 return r; 1705 } 1706 1707 /** 1708 * amdgpu_device_ip_init - run init for hardware IPs 1709 * 1710 * @adev: amdgpu_device pointer 1711 * 1712 * Main initialization pass for hardware IPs. The list of all the hardware 1713 * IPs that make up the asic is walked and the sw_init and hw_init callbacks 1714 * are run. sw_init initializes the software state associated with each IP 1715 * and hw_init initializes the hardware associated with each IP. 1716 * Returns 0 on success, negative error code on failure. 1717 */ 1718 static int amdgpu_device_ip_init(struct amdgpu_device *adev) 1719 { 1720 int i, r; 1721 1722 r = amdgpu_ras_init(adev); 1723 if (r) 1724 return r; 1725 1726 for (i = 0; i < adev->num_ip_blocks; i++) { 1727 if (!adev->ip_blocks[i].status.valid) 1728 continue; 1729 r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev); 1730 if (r) { 1731 DRM_ERROR("sw_init of IP block <%s> failed %d\n", 1732 adev->ip_blocks[i].version->funcs->name, r); 1733 goto init_failed; 1734 } 1735 adev->ip_blocks[i].status.sw = true; 1736 1737 /* need to do gmc hw init early so we can allocate gpu mem */ 1738 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) { 1739 r = amdgpu_device_vram_scratch_init(adev); 1740 if (r) { 1741 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r); 1742 goto init_failed; 1743 } 1744 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev); 1745 if (r) { 1746 DRM_ERROR("hw_init %d failed %d\n", i, r); 1747 goto init_failed; 1748 } 1749 r = amdgpu_device_wb_init(adev); 1750 if (r) { 1751 DRM_ERROR("amdgpu_device_wb_init failed %d\n", r); 1752 goto init_failed; 1753 } 1754 adev->ip_blocks[i].status.hw = true; 1755 1756 /* right after GMC hw init, we create CSA */ 1757 if (amdgpu_mcbp || amdgpu_sriov_vf(adev)) { 1758 r = amdgpu_allocate_static_csa(adev, &adev->virt.csa_obj, 1759 AMDGPU_GEM_DOMAIN_VRAM, 1760 AMDGPU_CSA_SIZE); 1761 if (r) { 1762 DRM_ERROR("allocate CSA failed %d\n", r); 1763 goto init_failed; 1764 } 1765 } 1766 } 1767 } 1768 1769 r = amdgpu_ib_pool_init(adev); 1770 if (r) { 1771 dev_err(adev->dev, "IB initialization failed (%d).\n", r); 1772 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r); 1773 goto init_failed; 1774 } 1775 1776 r = amdgpu_ucode_create_bo(adev); /* create ucode bo when sw_init complete*/ 1777 if (r) 1778 goto init_failed; 1779 1780 r = amdgpu_device_ip_hw_init_phase1(adev); 1781 if (r) 1782 goto init_failed; 1783 1784 r = amdgpu_device_fw_loading(adev); 1785 if (r) 1786 goto init_failed; 1787 1788 r = amdgpu_device_ip_hw_init_phase2(adev); 1789 if (r) 1790 goto init_failed; 1791 1792 if (adev->gmc.xgmi.num_physical_nodes > 1) 1793 amdgpu_xgmi_add_device(adev); 1794 amdgpu_amdkfd_device_init(adev); 1795 1796 init_failed: 1797 if (amdgpu_sriov_vf(adev)) { 1798 if (!r) 1799 amdgpu_virt_init_data_exchange(adev); 1800 amdgpu_virt_release_full_gpu(adev, true); 1801 } 1802 1803 return r; 1804 } 1805 1806 /** 1807 * amdgpu_device_fill_reset_magic - writes reset magic to gart pointer 1808 * 1809 * @adev: amdgpu_device pointer 1810 * 1811 * Writes a reset magic value to the gart pointer in VRAM. The driver calls 1812 * this function before a GPU reset. If the value is retained after a 1813 * GPU reset, VRAM has not been lost. Some GPU resets may destry VRAM contents. 1814 */ 1815 static void amdgpu_device_fill_reset_magic(struct amdgpu_device *adev) 1816 { 1817 memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM); 1818 } 1819 1820 /** 1821 * amdgpu_device_check_vram_lost - check if vram is valid 1822 * 1823 * @adev: amdgpu_device pointer 1824 * 1825 * Checks the reset magic value written to the gart pointer in VRAM. 1826 * The driver calls this after a GPU reset to see if the contents of 1827 * VRAM is lost or now. 1828 * returns true if vram is lost, false if not. 1829 */ 1830 static bool amdgpu_device_check_vram_lost(struct amdgpu_device *adev) 1831 { 1832 return !!memcmp(adev->gart.ptr, adev->reset_magic, 1833 AMDGPU_RESET_MAGIC_NUM); 1834 } 1835 1836 /** 1837 * amdgpu_device_set_cg_state - set clockgating for amdgpu device 1838 * 1839 * @adev: amdgpu_device pointer 1840 * 1841 * The list of all the hardware IPs that make up the asic is walked and the 1842 * set_clockgating_state callbacks are run. 1843 * Late initialization pass enabling clockgating for hardware IPs. 1844 * Fini or suspend, pass disabling clockgating for hardware IPs. 1845 * Returns 0 on success, negative error code on failure. 1846 */ 1847 1848 static int amdgpu_device_set_cg_state(struct amdgpu_device *adev, 1849 enum amd_clockgating_state state) 1850 { 1851 int i, j, r; 1852 1853 if (amdgpu_emu_mode == 1) 1854 return 0; 1855 1856 for (j = 0; j < adev->num_ip_blocks; j++) { 1857 i = state == AMD_CG_STATE_GATE ? j : adev->num_ip_blocks - j - 1; 1858 if (!adev->ip_blocks[i].status.late_initialized) 1859 continue; 1860 /* skip CG for VCE/UVD, it's handled specially */ 1861 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD && 1862 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE && 1863 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCN && 1864 adev->ip_blocks[i].version->funcs->set_clockgating_state) { 1865 /* enable clockgating to save power */ 1866 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev, 1867 state); 1868 if (r) { 1869 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n", 1870 adev->ip_blocks[i].version->funcs->name, r); 1871 return r; 1872 } 1873 } 1874 } 1875 1876 return 0; 1877 } 1878 1879 static int amdgpu_device_set_pg_state(struct amdgpu_device *adev, enum amd_powergating_state state) 1880 { 1881 int i, j, r; 1882 1883 if (amdgpu_emu_mode == 1) 1884 return 0; 1885 1886 for (j = 0; j < adev->num_ip_blocks; j++) { 1887 i = state == AMD_PG_STATE_GATE ? j : adev->num_ip_blocks - j - 1; 1888 if (!adev->ip_blocks[i].status.late_initialized) 1889 continue; 1890 /* skip CG for VCE/UVD, it's handled specially */ 1891 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD && 1892 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE && 1893 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCN && 1894 adev->ip_blocks[i].version->funcs->set_powergating_state) { 1895 /* enable powergating to save power */ 1896 r = adev->ip_blocks[i].version->funcs->set_powergating_state((void *)adev, 1897 state); 1898 if (r) { 1899 DRM_ERROR("set_powergating_state(gate) of IP block <%s> failed %d\n", 1900 adev->ip_blocks[i].version->funcs->name, r); 1901 return r; 1902 } 1903 } 1904 } 1905 return 0; 1906 } 1907 1908 static int amdgpu_device_enable_mgpu_fan_boost(void) 1909 { 1910 struct amdgpu_gpu_instance *gpu_ins; 1911 struct amdgpu_device *adev; 1912 int i, ret = 0; 1913 1914 mutex_lock(&mgpu_info.mutex); 1915 1916 /* 1917 * MGPU fan boost feature should be enabled 1918 * only when there are two or more dGPUs in 1919 * the system 1920 */ 1921 if (mgpu_info.num_dgpu < 2) 1922 goto out; 1923 1924 for (i = 0; i < mgpu_info.num_dgpu; i++) { 1925 gpu_ins = &(mgpu_info.gpu_ins[i]); 1926 adev = gpu_ins->adev; 1927 if (!(adev->flags & AMD_IS_APU) && 1928 !gpu_ins->mgpu_fan_enabled && 1929 adev->powerplay.pp_funcs && 1930 adev->powerplay.pp_funcs->enable_mgpu_fan_boost) { 1931 ret = amdgpu_dpm_enable_mgpu_fan_boost(adev); 1932 if (ret) 1933 break; 1934 1935 gpu_ins->mgpu_fan_enabled = 1; 1936 } 1937 } 1938 1939 out: 1940 mutex_unlock(&mgpu_info.mutex); 1941 1942 return ret; 1943 } 1944 1945 /** 1946 * amdgpu_device_ip_late_init - run late init for hardware IPs 1947 * 1948 * @adev: amdgpu_device pointer 1949 * 1950 * Late initialization pass for hardware IPs. The list of all the hardware 1951 * IPs that make up the asic is walked and the late_init callbacks are run. 1952 * late_init covers any special initialization that an IP requires 1953 * after all of the have been initialized or something that needs to happen 1954 * late in the init process. 1955 * Returns 0 on success, negative error code on failure. 1956 */ 1957 static int amdgpu_device_ip_late_init(struct amdgpu_device *adev) 1958 { 1959 int i = 0, r; 1960 1961 for (i = 0; i < adev->num_ip_blocks; i++) { 1962 if (!adev->ip_blocks[i].status.hw) 1963 continue; 1964 if (adev->ip_blocks[i].version->funcs->late_init) { 1965 r = adev->ip_blocks[i].version->funcs->late_init((void *)adev); 1966 if (r) { 1967 DRM_ERROR("late_init of IP block <%s> failed %d\n", 1968 adev->ip_blocks[i].version->funcs->name, r); 1969 return r; 1970 } 1971 } 1972 adev->ip_blocks[i].status.late_initialized = true; 1973 } 1974 1975 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_GATE); 1976 amdgpu_device_set_pg_state(adev, AMD_PG_STATE_GATE); 1977 1978 amdgpu_device_fill_reset_magic(adev); 1979 1980 r = amdgpu_device_enable_mgpu_fan_boost(); 1981 if (r) 1982 DRM_ERROR("enable mgpu fan boost failed (%d).\n", r); 1983 1984 /* set to low pstate by default */ 1985 amdgpu_xgmi_set_pstate(adev, 0); 1986 1987 return 0; 1988 } 1989 1990 /** 1991 * amdgpu_device_ip_fini - run fini for hardware IPs 1992 * 1993 * @adev: amdgpu_device pointer 1994 * 1995 * Main teardown pass for hardware IPs. The list of all the hardware 1996 * IPs that make up the asic is walked and the hw_fini and sw_fini callbacks 1997 * are run. hw_fini tears down the hardware associated with each IP 1998 * and sw_fini tears down any software state associated with each IP. 1999 * Returns 0 on success, negative error code on failure. 2000 */ 2001 static int amdgpu_device_ip_fini(struct amdgpu_device *adev) 2002 { 2003 int i, r; 2004 2005 amdgpu_ras_pre_fini(adev); 2006 2007 if (adev->gmc.xgmi.num_physical_nodes > 1) 2008 amdgpu_xgmi_remove_device(adev); 2009 2010 amdgpu_amdkfd_device_fini(adev); 2011 2012 amdgpu_device_set_pg_state(adev, AMD_PG_STATE_UNGATE); 2013 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_UNGATE); 2014 2015 /* need to disable SMC first */ 2016 for (i = 0; i < adev->num_ip_blocks; i++) { 2017 if (!adev->ip_blocks[i].status.hw) 2018 continue; 2019 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) { 2020 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev); 2021 /* XXX handle errors */ 2022 if (r) { 2023 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n", 2024 adev->ip_blocks[i].version->funcs->name, r); 2025 } 2026 adev->ip_blocks[i].status.hw = false; 2027 break; 2028 } 2029 } 2030 2031 for (i = adev->num_ip_blocks - 1; i >= 0; i--) { 2032 if (!adev->ip_blocks[i].status.hw) 2033 continue; 2034 2035 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev); 2036 /* XXX handle errors */ 2037 if (r) { 2038 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n", 2039 adev->ip_blocks[i].version->funcs->name, r); 2040 } 2041 2042 adev->ip_blocks[i].status.hw = false; 2043 } 2044 2045 2046 for (i = adev->num_ip_blocks - 1; i >= 0; i--) { 2047 if (!adev->ip_blocks[i].status.sw) 2048 continue; 2049 2050 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) { 2051 amdgpu_ucode_free_bo(adev); 2052 amdgpu_free_static_csa(&adev->virt.csa_obj); 2053 amdgpu_device_wb_fini(adev); 2054 amdgpu_device_vram_scratch_fini(adev); 2055 amdgpu_ib_pool_fini(adev); 2056 } 2057 2058 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev); 2059 /* XXX handle errors */ 2060 if (r) { 2061 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n", 2062 adev->ip_blocks[i].version->funcs->name, r); 2063 } 2064 adev->ip_blocks[i].status.sw = false; 2065 adev->ip_blocks[i].status.valid = false; 2066 } 2067 2068 for (i = adev->num_ip_blocks - 1; i >= 0; i--) { 2069 if (!adev->ip_blocks[i].status.late_initialized) 2070 continue; 2071 if (adev->ip_blocks[i].version->funcs->late_fini) 2072 adev->ip_blocks[i].version->funcs->late_fini((void *)adev); 2073 adev->ip_blocks[i].status.late_initialized = false; 2074 } 2075 2076 amdgpu_ras_fini(adev); 2077 2078 if (amdgpu_sriov_vf(adev)) 2079 if (amdgpu_virt_release_full_gpu(adev, false)) 2080 DRM_ERROR("failed to release exclusive mode on fini\n"); 2081 2082 return 0; 2083 } 2084 2085 /** 2086 * amdgpu_device_delayed_init_work_handler - work handler for IB tests 2087 * 2088 * @work: work_struct. 2089 */ 2090 static void amdgpu_device_delayed_init_work_handler(struct work_struct *work) 2091 { 2092 struct amdgpu_device *adev = 2093 container_of(work, struct amdgpu_device, delayed_init_work.work); 2094 int r; 2095 2096 r = amdgpu_ib_ring_tests(adev); 2097 if (r) 2098 DRM_ERROR("ib ring test failed (%d).\n", r); 2099 } 2100 2101 static void amdgpu_device_delay_enable_gfx_off(struct work_struct *work) 2102 { 2103 struct amdgpu_device *adev = 2104 container_of(work, struct amdgpu_device, gfx.gfx_off_delay_work.work); 2105 2106 mutex_lock(&adev->gfx.gfx_off_mutex); 2107 if (!adev->gfx.gfx_off_state && !adev->gfx.gfx_off_req_count) { 2108 if (!amdgpu_dpm_set_powergating_by_smu(adev, AMD_IP_BLOCK_TYPE_GFX, true)) 2109 adev->gfx.gfx_off_state = true; 2110 } 2111 mutex_unlock(&adev->gfx.gfx_off_mutex); 2112 } 2113 2114 /** 2115 * amdgpu_device_ip_suspend_phase1 - run suspend for hardware IPs (phase 1) 2116 * 2117 * @adev: amdgpu_device pointer 2118 * 2119 * Main suspend function for hardware IPs. The list of all the hardware 2120 * IPs that make up the asic is walked, clockgating is disabled and the 2121 * suspend callbacks are run. suspend puts the hardware and software state 2122 * in each IP into a state suitable for suspend. 2123 * Returns 0 on success, negative error code on failure. 2124 */ 2125 static int amdgpu_device_ip_suspend_phase1(struct amdgpu_device *adev) 2126 { 2127 int i, r; 2128 2129 amdgpu_device_set_pg_state(adev, AMD_PG_STATE_UNGATE); 2130 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_UNGATE); 2131 2132 for (i = adev->num_ip_blocks - 1; i >= 0; i--) { 2133 if (!adev->ip_blocks[i].status.valid) 2134 continue; 2135 /* displays are handled separately */ 2136 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) { 2137 /* XXX handle errors */ 2138 r = adev->ip_blocks[i].version->funcs->suspend(adev); 2139 /* XXX handle errors */ 2140 if (r) { 2141 DRM_ERROR("suspend of IP block <%s> failed %d\n", 2142 adev->ip_blocks[i].version->funcs->name, r); 2143 } 2144 } 2145 } 2146 2147 return 0; 2148 } 2149 2150 /** 2151 * amdgpu_device_ip_suspend_phase2 - run suspend for hardware IPs (phase 2) 2152 * 2153 * @adev: amdgpu_device pointer 2154 * 2155 * Main suspend function for hardware IPs. The list of all the hardware 2156 * IPs that make up the asic is walked, clockgating is disabled and the 2157 * suspend callbacks are run. suspend puts the hardware and software state 2158 * in each IP into a state suitable for suspend. 2159 * Returns 0 on success, negative error code on failure. 2160 */ 2161 static int amdgpu_device_ip_suspend_phase2(struct amdgpu_device *adev) 2162 { 2163 int i, r; 2164 2165 for (i = adev->num_ip_blocks - 1; i >= 0; i--) { 2166 if (!adev->ip_blocks[i].status.valid) 2167 continue; 2168 /* displays are handled in phase1 */ 2169 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) 2170 continue; 2171 /* XXX handle errors */ 2172 r = adev->ip_blocks[i].version->funcs->suspend(adev); 2173 /* XXX handle errors */ 2174 if (r) { 2175 DRM_ERROR("suspend of IP block <%s> failed %d\n", 2176 adev->ip_blocks[i].version->funcs->name, r); 2177 } 2178 /* handle putting the SMC in the appropriate state */ 2179 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) { 2180 if (is_support_sw_smu(adev)) { 2181 /* todo */ 2182 } else if (adev->powerplay.pp_funcs && 2183 adev->powerplay.pp_funcs->set_mp1_state) { 2184 r = adev->powerplay.pp_funcs->set_mp1_state( 2185 adev->powerplay.pp_handle, 2186 adev->mp1_state); 2187 if (r) { 2188 DRM_ERROR("SMC failed to set mp1 state %d, %d\n", 2189 adev->mp1_state, r); 2190 } 2191 } 2192 } 2193 } 2194 2195 return 0; 2196 } 2197 2198 /** 2199 * amdgpu_device_ip_suspend - run suspend for hardware IPs 2200 * 2201 * @adev: amdgpu_device pointer 2202 * 2203 * Main suspend function for hardware IPs. The list of all the hardware 2204 * IPs that make up the asic is walked, clockgating is disabled and the 2205 * suspend callbacks are run. suspend puts the hardware and software state 2206 * in each IP into a state suitable for suspend. 2207 * Returns 0 on success, negative error code on failure. 2208 */ 2209 int amdgpu_device_ip_suspend(struct amdgpu_device *adev) 2210 { 2211 int r; 2212 2213 if (amdgpu_sriov_vf(adev)) 2214 amdgpu_virt_request_full_gpu(adev, false); 2215 2216 r = amdgpu_device_ip_suspend_phase1(adev); 2217 if (r) 2218 return r; 2219 r = amdgpu_device_ip_suspend_phase2(adev); 2220 2221 if (amdgpu_sriov_vf(adev)) 2222 amdgpu_virt_release_full_gpu(adev, false); 2223 2224 return r; 2225 } 2226 2227 static int amdgpu_device_ip_reinit_early_sriov(struct amdgpu_device *adev) 2228 { 2229 int i, r; 2230 2231 static enum amd_ip_block_type ip_order[] = { 2232 AMD_IP_BLOCK_TYPE_GMC, 2233 AMD_IP_BLOCK_TYPE_COMMON, 2234 AMD_IP_BLOCK_TYPE_PSP, 2235 AMD_IP_BLOCK_TYPE_IH, 2236 }; 2237 2238 for (i = 0; i < ARRAY_SIZE(ip_order); i++) { 2239 int j; 2240 struct amdgpu_ip_block *block; 2241 2242 for (j = 0; j < adev->num_ip_blocks; j++) { 2243 block = &adev->ip_blocks[j]; 2244 2245 if (block->version->type != ip_order[i] || 2246 !block->status.valid) 2247 continue; 2248 2249 r = block->version->funcs->hw_init(adev); 2250 DRM_INFO("RE-INIT-early: %s %s\n", block->version->funcs->name, r?"failed":"succeeded"); 2251 if (r) 2252 return r; 2253 } 2254 } 2255 2256 return 0; 2257 } 2258 2259 static int amdgpu_device_ip_reinit_late_sriov(struct amdgpu_device *adev) 2260 { 2261 int i, r; 2262 2263 static enum amd_ip_block_type ip_order[] = { 2264 AMD_IP_BLOCK_TYPE_SMC, 2265 AMD_IP_BLOCK_TYPE_DCE, 2266 AMD_IP_BLOCK_TYPE_GFX, 2267 AMD_IP_BLOCK_TYPE_SDMA, 2268 AMD_IP_BLOCK_TYPE_UVD, 2269 AMD_IP_BLOCK_TYPE_VCE 2270 }; 2271 2272 for (i = 0; i < ARRAY_SIZE(ip_order); i++) { 2273 int j; 2274 struct amdgpu_ip_block *block; 2275 2276 for (j = 0; j < adev->num_ip_blocks; j++) { 2277 block = &adev->ip_blocks[j]; 2278 2279 if (block->version->type != ip_order[i] || 2280 !block->status.valid) 2281 continue; 2282 2283 r = block->version->funcs->hw_init(adev); 2284 DRM_INFO("RE-INIT-late: %s %s\n", block->version->funcs->name, r?"failed":"succeeded"); 2285 if (r) 2286 return r; 2287 } 2288 } 2289 2290 return 0; 2291 } 2292 2293 /** 2294 * amdgpu_device_ip_resume_phase1 - run resume for hardware IPs 2295 * 2296 * @adev: amdgpu_device pointer 2297 * 2298 * First resume function for hardware IPs. The list of all the hardware 2299 * IPs that make up the asic is walked and the resume callbacks are run for 2300 * COMMON, GMC, and IH. resume puts the hardware into a functional state 2301 * after a suspend and updates the software state as necessary. This 2302 * function is also used for restoring the GPU after a GPU reset. 2303 * Returns 0 on success, negative error code on failure. 2304 */ 2305 static int amdgpu_device_ip_resume_phase1(struct amdgpu_device *adev) 2306 { 2307 int i, r; 2308 2309 for (i = 0; i < adev->num_ip_blocks; i++) { 2310 if (!adev->ip_blocks[i].status.valid) 2311 continue; 2312 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON || 2313 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC || 2314 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH) { 2315 r = adev->ip_blocks[i].version->funcs->resume(adev); 2316 if (r) { 2317 DRM_ERROR("resume of IP block <%s> failed %d\n", 2318 adev->ip_blocks[i].version->funcs->name, r); 2319 return r; 2320 } 2321 } 2322 } 2323 2324 return 0; 2325 } 2326 2327 /** 2328 * amdgpu_device_ip_resume_phase2 - run resume for hardware IPs 2329 * 2330 * @adev: amdgpu_device pointer 2331 * 2332 * First resume function for hardware IPs. The list of all the hardware 2333 * IPs that make up the asic is walked and the resume callbacks are run for 2334 * all blocks except COMMON, GMC, and IH. resume puts the hardware into a 2335 * functional state after a suspend and updates the software state as 2336 * necessary. This function is also used for restoring the GPU after a GPU 2337 * reset. 2338 * Returns 0 on success, negative error code on failure. 2339 */ 2340 static int amdgpu_device_ip_resume_phase2(struct amdgpu_device *adev) 2341 { 2342 int i, r; 2343 2344 for (i = 0; i < adev->num_ip_blocks; i++) { 2345 if (!adev->ip_blocks[i].status.valid) 2346 continue; 2347 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON || 2348 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC || 2349 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH || 2350 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) 2351 continue; 2352 r = adev->ip_blocks[i].version->funcs->resume(adev); 2353 if (r) { 2354 DRM_ERROR("resume of IP block <%s> failed %d\n", 2355 adev->ip_blocks[i].version->funcs->name, r); 2356 return r; 2357 } 2358 } 2359 2360 return 0; 2361 } 2362 2363 /** 2364 * amdgpu_device_ip_resume - run resume for hardware IPs 2365 * 2366 * @adev: amdgpu_device pointer 2367 * 2368 * Main resume function for hardware IPs. The hardware IPs 2369 * are split into two resume functions because they are 2370 * are also used in in recovering from a GPU reset and some additional 2371 * steps need to be take between them. In this case (S3/S4) they are 2372 * run sequentially. 2373 * Returns 0 on success, negative error code on failure. 2374 */ 2375 static int amdgpu_device_ip_resume(struct amdgpu_device *adev) 2376 { 2377 int r; 2378 2379 r = amdgpu_device_ip_resume_phase1(adev); 2380 if (r) 2381 return r; 2382 2383 r = amdgpu_device_fw_loading(adev); 2384 if (r) 2385 return r; 2386 2387 r = amdgpu_device_ip_resume_phase2(adev); 2388 2389 return r; 2390 } 2391 2392 /** 2393 * amdgpu_device_detect_sriov_bios - determine if the board supports SR-IOV 2394 * 2395 * @adev: amdgpu_device pointer 2396 * 2397 * Query the VBIOS data tables to determine if the board supports SR-IOV. 2398 */ 2399 static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev) 2400 { 2401 if (amdgpu_sriov_vf(adev)) { 2402 if (adev->is_atom_fw) { 2403 if (amdgpu_atomfirmware_gpu_supports_virtualization(adev)) 2404 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS; 2405 } else { 2406 if (amdgpu_atombios_has_gpu_virtualization_table(adev)) 2407 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS; 2408 } 2409 2410 if (!(adev->virt.caps & AMDGPU_SRIOV_CAPS_SRIOV_VBIOS)) 2411 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_NO_VBIOS, 0, 0); 2412 } 2413 } 2414 2415 /** 2416 * amdgpu_device_asic_has_dc_support - determine if DC supports the asic 2417 * 2418 * @asic_type: AMD asic type 2419 * 2420 * Check if there is DC (new modesetting infrastructre) support for an asic. 2421 * returns true if DC has support, false if not. 2422 */ 2423 bool amdgpu_device_asic_has_dc_support(enum amd_asic_type asic_type) 2424 { 2425 switch (asic_type) { 2426 #if defined(CONFIG_DRM_AMD_DC) 2427 case CHIP_BONAIRE: 2428 case CHIP_KAVERI: 2429 case CHIP_KABINI: 2430 case CHIP_MULLINS: 2431 /* 2432 * We have systems in the wild with these ASICs that require 2433 * LVDS and VGA support which is not supported with DC. 2434 * 2435 * Fallback to the non-DC driver here by default so as not to 2436 * cause regressions. 2437 */ 2438 return amdgpu_dc > 0; 2439 case CHIP_HAWAII: 2440 case CHIP_CARRIZO: 2441 case CHIP_STONEY: 2442 case CHIP_POLARIS10: 2443 case CHIP_POLARIS11: 2444 case CHIP_POLARIS12: 2445 case CHIP_VEGAM: 2446 case CHIP_TONGA: 2447 case CHIP_FIJI: 2448 case CHIP_VEGA10: 2449 case CHIP_VEGA12: 2450 case CHIP_VEGA20: 2451 #if defined(CONFIG_DRM_AMD_DC_DCN1_0) 2452 case CHIP_RAVEN: 2453 #endif 2454 #if defined(CONFIG_DRM_AMD_DC_DCN2_0) 2455 case CHIP_NAVI10: 2456 case CHIP_NAVI14: 2457 #endif 2458 return amdgpu_dc != 0; 2459 #endif 2460 default: 2461 return false; 2462 } 2463 } 2464 2465 /** 2466 * amdgpu_device_has_dc_support - check if dc is supported 2467 * 2468 * @adev: amdgpu_device_pointer 2469 * 2470 * Returns true for supported, false for not supported 2471 */ 2472 bool amdgpu_device_has_dc_support(struct amdgpu_device *adev) 2473 { 2474 if (amdgpu_sriov_vf(adev)) 2475 return false; 2476 2477 return amdgpu_device_asic_has_dc_support(adev->asic_type); 2478 } 2479 2480 2481 static void amdgpu_device_xgmi_reset_func(struct work_struct *__work) 2482 { 2483 struct amdgpu_device *adev = 2484 container_of(__work, struct amdgpu_device, xgmi_reset_work); 2485 2486 adev->asic_reset_res = amdgpu_asic_reset(adev); 2487 if (adev->asic_reset_res) 2488 DRM_WARN("ASIC reset failed with error, %d for drm dev, %s", 2489 adev->asic_reset_res, adev->ddev->unique); 2490 } 2491 2492 2493 /** 2494 * amdgpu_device_init - initialize the driver 2495 * 2496 * @adev: amdgpu_device pointer 2497 * @ddev: drm dev pointer 2498 * @pdev: pci dev pointer 2499 * @flags: driver flags 2500 * 2501 * Initializes the driver info and hw (all asics). 2502 * Returns 0 for success or an error on failure. 2503 * Called at driver startup. 2504 */ 2505 int amdgpu_device_init(struct amdgpu_device *adev, 2506 struct drm_device *ddev, 2507 struct pci_dev *pdev, 2508 uint32_t flags) 2509 { 2510 int r, i; 2511 bool runtime = false; 2512 u32 max_MBps; 2513 2514 adev->shutdown = false; 2515 adev->dev = &pdev->dev; 2516 adev->ddev = ddev; 2517 adev->pdev = pdev; 2518 adev->flags = flags; 2519 adev->asic_type = flags & AMD_ASIC_MASK; 2520 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT; 2521 if (amdgpu_emu_mode == 1) 2522 adev->usec_timeout *= 2; 2523 adev->gmc.gart_size = 512 * 1024 * 1024; 2524 adev->accel_working = false; 2525 adev->num_rings = 0; 2526 adev->mman.buffer_funcs = NULL; 2527 adev->mman.buffer_funcs_ring = NULL; 2528 adev->vm_manager.vm_pte_funcs = NULL; 2529 adev->vm_manager.vm_pte_num_rqs = 0; 2530 adev->gmc.gmc_funcs = NULL; 2531 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS); 2532 bitmap_zero(adev->gfx.pipe_reserve_bitmap, AMDGPU_MAX_COMPUTE_QUEUES); 2533 2534 adev->smc_rreg = &amdgpu_invalid_rreg; 2535 adev->smc_wreg = &amdgpu_invalid_wreg; 2536 adev->pcie_rreg = &amdgpu_invalid_rreg; 2537 adev->pcie_wreg = &amdgpu_invalid_wreg; 2538 adev->pciep_rreg = &amdgpu_invalid_rreg; 2539 adev->pciep_wreg = &amdgpu_invalid_wreg; 2540 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg; 2541 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg; 2542 adev->didt_rreg = &amdgpu_invalid_rreg; 2543 adev->didt_wreg = &amdgpu_invalid_wreg; 2544 adev->gc_cac_rreg = &amdgpu_invalid_rreg; 2545 adev->gc_cac_wreg = &amdgpu_invalid_wreg; 2546 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg; 2547 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg; 2548 2549 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n", 2550 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device, 2551 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision); 2552 2553 /* mutex initialization are all done here so we 2554 * can recall function without having locking issues */ 2555 atomic_set(&adev->irq.ih.lock, 0); 2556 mutex_init(&adev->firmware.mutex); 2557 mutex_init(&adev->pm.mutex); 2558 mutex_init(&adev->gfx.gpu_clock_mutex); 2559 mutex_init(&adev->srbm_mutex); 2560 mutex_init(&adev->gfx.pipe_reserve_mutex); 2561 mutex_init(&adev->gfx.gfx_off_mutex); 2562 mutex_init(&adev->grbm_idx_mutex); 2563 mutex_init(&adev->mn_lock); 2564 mutex_init(&adev->virt.vf_errors.lock); 2565 hash_init(adev->mn_hash); 2566 mutex_init(&adev->lock_reset); 2567 mutex_init(&adev->virt.dpm_mutex); 2568 mutex_init(&adev->psp.mutex); 2569 2570 r = amdgpu_device_check_arguments(adev); 2571 if (r) 2572 return r; 2573 2574 spin_lock_init(&adev->mmio_idx_lock); 2575 spin_lock_init(&adev->smc_idx_lock); 2576 spin_lock_init(&adev->pcie_idx_lock); 2577 spin_lock_init(&adev->uvd_ctx_idx_lock); 2578 spin_lock_init(&adev->didt_idx_lock); 2579 spin_lock_init(&adev->gc_cac_idx_lock); 2580 spin_lock_init(&adev->se_cac_idx_lock); 2581 spin_lock_init(&adev->audio_endpt_idx_lock); 2582 spin_lock_init(&adev->mm_stats.lock); 2583 2584 INIT_LIST_HEAD(&adev->shadow_list); 2585 mutex_init(&adev->shadow_list_lock); 2586 2587 INIT_LIST_HEAD(&adev->ring_lru_list); 2588 spin_lock_init(&adev->ring_lru_list_lock); 2589 2590 INIT_DELAYED_WORK(&adev->delayed_init_work, 2591 amdgpu_device_delayed_init_work_handler); 2592 INIT_DELAYED_WORK(&adev->gfx.gfx_off_delay_work, 2593 amdgpu_device_delay_enable_gfx_off); 2594 2595 INIT_WORK(&adev->xgmi_reset_work, amdgpu_device_xgmi_reset_func); 2596 2597 adev->gfx.gfx_off_req_count = 1; 2598 adev->pm.ac_power = power_supply_is_system_supplied() > 0 ? true : false; 2599 2600 /* Registers mapping */ 2601 /* TODO: block userspace mapping of io register */ 2602 if (adev->asic_type >= CHIP_BONAIRE) { 2603 adev->rmmio_base = pci_resource_start(adev->pdev, 5); 2604 adev->rmmio_size = pci_resource_len(adev->pdev, 5); 2605 } else { 2606 adev->rmmio_base = pci_resource_start(adev->pdev, 2); 2607 adev->rmmio_size = pci_resource_len(adev->pdev, 2); 2608 } 2609 2610 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size); 2611 if (adev->rmmio == NULL) { 2612 return -ENOMEM; 2613 } 2614 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base); 2615 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size); 2616 2617 /* io port mapping */ 2618 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) { 2619 if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) { 2620 adev->rio_mem_size = pci_resource_len(adev->pdev, i); 2621 adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size); 2622 break; 2623 } 2624 } 2625 if (adev->rio_mem == NULL) 2626 DRM_INFO("PCI I/O BAR is not found.\n"); 2627 2628 /* enable PCIE atomic ops */ 2629 r = pci_enable_atomic_ops_to_root(adev->pdev, 2630 PCI_EXP_DEVCAP2_ATOMIC_COMP32 | 2631 PCI_EXP_DEVCAP2_ATOMIC_COMP64); 2632 if (r) { 2633 adev->have_atomics_support = false; 2634 DRM_INFO("PCIE atomic ops is not supported\n"); 2635 } else { 2636 adev->have_atomics_support = true; 2637 } 2638 2639 amdgpu_device_get_pcie_info(adev); 2640 2641 if (amdgpu_mcbp) 2642 DRM_INFO("MCBP is enabled\n"); 2643 2644 if (amdgpu_mes && adev->asic_type >= CHIP_NAVI10) 2645 adev->enable_mes = true; 2646 2647 if (amdgpu_discovery && adev->asic_type >= CHIP_NAVI10) { 2648 r = amdgpu_discovery_init(adev); 2649 if (r) { 2650 dev_err(adev->dev, "amdgpu_discovery_init failed\n"); 2651 return r; 2652 } 2653 } 2654 2655 /* early init functions */ 2656 r = amdgpu_device_ip_early_init(adev); 2657 if (r) 2658 return r; 2659 2660 /* doorbell bar mapping and doorbell index init*/ 2661 amdgpu_device_doorbell_init(adev); 2662 2663 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */ 2664 /* this will fail for cards that aren't VGA class devices, just 2665 * ignore it */ 2666 vga_client_register(adev->pdev, adev, NULL, amdgpu_device_vga_set_decode); 2667 2668 if (amdgpu_device_is_px(ddev)) 2669 runtime = true; 2670 if (!pci_is_thunderbolt_attached(adev->pdev)) 2671 vga_switcheroo_register_client(adev->pdev, 2672 &amdgpu_switcheroo_ops, runtime); 2673 if (runtime) 2674 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain); 2675 2676 if (amdgpu_emu_mode == 1) { 2677 /* post the asic on emulation mode */ 2678 emu_soc_asic_init(adev); 2679 goto fence_driver_init; 2680 } 2681 2682 /* detect if we are with an SRIOV vbios */ 2683 amdgpu_device_detect_sriov_bios(adev); 2684 2685 /* check if we need to reset the asic 2686 * E.g., driver was not cleanly unloaded previously, etc. 2687 */ 2688 if (!amdgpu_sriov_vf(adev) && amdgpu_asic_need_reset_on_init(adev)) { 2689 r = amdgpu_asic_reset(adev); 2690 if (r) { 2691 dev_err(adev->dev, "asic reset on init failed\n"); 2692 goto failed; 2693 } 2694 } 2695 2696 /* Post card if necessary */ 2697 if (amdgpu_device_need_post(adev)) { 2698 if (!adev->bios) { 2699 dev_err(adev->dev, "no vBIOS found\n"); 2700 r = -EINVAL; 2701 goto failed; 2702 } 2703 DRM_INFO("GPU posting now...\n"); 2704 r = amdgpu_atom_asic_init(adev->mode_info.atom_context); 2705 if (r) { 2706 dev_err(adev->dev, "gpu post error!\n"); 2707 goto failed; 2708 } 2709 } 2710 2711 if (adev->is_atom_fw) { 2712 /* Initialize clocks */ 2713 r = amdgpu_atomfirmware_get_clock_info(adev); 2714 if (r) { 2715 dev_err(adev->dev, "amdgpu_atomfirmware_get_clock_info failed\n"); 2716 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0); 2717 goto failed; 2718 } 2719 } else { 2720 /* Initialize clocks */ 2721 r = amdgpu_atombios_get_clock_info(adev); 2722 if (r) { 2723 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n"); 2724 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0); 2725 goto failed; 2726 } 2727 /* init i2c buses */ 2728 if (!amdgpu_device_has_dc_support(adev)) 2729 amdgpu_atombios_i2c_init(adev); 2730 } 2731 2732 fence_driver_init: 2733 /* Fence driver */ 2734 r = amdgpu_fence_driver_init(adev); 2735 if (r) { 2736 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n"); 2737 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0); 2738 goto failed; 2739 } 2740 2741 /* init the mode config */ 2742 drm_mode_config_init(adev->ddev); 2743 2744 r = amdgpu_device_ip_init(adev); 2745 if (r) { 2746 /* failed in exclusive mode due to timeout */ 2747 if (amdgpu_sriov_vf(adev) && 2748 !amdgpu_sriov_runtime(adev) && 2749 amdgpu_virt_mmio_blocked(adev) && 2750 !amdgpu_virt_wait_reset(adev)) { 2751 dev_err(adev->dev, "VF exclusive mode timeout\n"); 2752 /* Don't send request since VF is inactive. */ 2753 adev->virt.caps &= ~AMDGPU_SRIOV_CAPS_RUNTIME; 2754 adev->virt.ops = NULL; 2755 r = -EAGAIN; 2756 goto failed; 2757 } 2758 dev_err(adev->dev, "amdgpu_device_ip_init failed\n"); 2759 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0); 2760 if (amdgpu_virt_request_full_gpu(adev, false)) 2761 amdgpu_virt_release_full_gpu(adev, false); 2762 goto failed; 2763 } 2764 2765 adev->accel_working = true; 2766 2767 amdgpu_vm_check_compute_bug(adev); 2768 2769 /* Initialize the buffer migration limit. */ 2770 if (amdgpu_moverate >= 0) 2771 max_MBps = amdgpu_moverate; 2772 else 2773 max_MBps = 8; /* Allow 8 MB/s. */ 2774 /* Get a log2 for easy divisions. */ 2775 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps)); 2776 2777 amdgpu_fbdev_init(adev); 2778 2779 if (amdgpu_sriov_vf(adev) && amdgim_is_hwperf(adev)) 2780 amdgpu_pm_virt_sysfs_init(adev); 2781 2782 r = amdgpu_pm_sysfs_init(adev); 2783 if (r) 2784 DRM_ERROR("registering pm debugfs failed (%d).\n", r); 2785 2786 r = amdgpu_ucode_sysfs_init(adev); 2787 if (r) 2788 DRM_ERROR("Creating firmware sysfs failed (%d).\n", r); 2789 2790 r = amdgpu_debugfs_gem_init(adev); 2791 if (r) 2792 DRM_ERROR("registering gem debugfs failed (%d).\n", r); 2793 2794 r = amdgpu_debugfs_regs_init(adev); 2795 if (r) 2796 DRM_ERROR("registering register debugfs failed (%d).\n", r); 2797 2798 r = amdgpu_debugfs_firmware_init(adev); 2799 if (r) 2800 DRM_ERROR("registering firmware debugfs failed (%d).\n", r); 2801 2802 r = amdgpu_debugfs_init(adev); 2803 if (r) 2804 DRM_ERROR("Creating debugfs files failed (%d).\n", r); 2805 2806 if ((amdgpu_testing & 1)) { 2807 if (adev->accel_working) 2808 amdgpu_test_moves(adev); 2809 else 2810 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n"); 2811 } 2812 if (amdgpu_benchmarking) { 2813 if (adev->accel_working) 2814 amdgpu_benchmark(adev, amdgpu_benchmarking); 2815 else 2816 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n"); 2817 } 2818 2819 /* enable clockgating, etc. after ib tests, etc. since some blocks require 2820 * explicit gating rather than handling it automatically. 2821 */ 2822 r = amdgpu_device_ip_late_init(adev); 2823 if (r) { 2824 dev_err(adev->dev, "amdgpu_device_ip_late_init failed\n"); 2825 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r); 2826 goto failed; 2827 } 2828 2829 /* must succeed. */ 2830 amdgpu_ras_resume(adev); 2831 2832 queue_delayed_work(system_wq, &adev->delayed_init_work, 2833 msecs_to_jiffies(AMDGPU_RESUME_MS)); 2834 2835 r = device_create_file(adev->dev, &dev_attr_pcie_replay_count); 2836 if (r) { 2837 dev_err(adev->dev, "Could not create pcie_replay_count"); 2838 return r; 2839 } 2840 2841 if (IS_ENABLED(CONFIG_PERF_EVENTS)) 2842 r = amdgpu_pmu_init(adev); 2843 if (r) 2844 dev_err(adev->dev, "amdgpu_pmu_init failed\n"); 2845 2846 return 0; 2847 2848 failed: 2849 amdgpu_vf_error_trans_all(adev); 2850 if (runtime) 2851 vga_switcheroo_fini_domain_pm_ops(adev->dev); 2852 2853 return r; 2854 } 2855 2856 /** 2857 * amdgpu_device_fini - tear down the driver 2858 * 2859 * @adev: amdgpu_device pointer 2860 * 2861 * Tear down the driver info (all asics). 2862 * Called at driver shutdown. 2863 */ 2864 void amdgpu_device_fini(struct amdgpu_device *adev) 2865 { 2866 int r; 2867 2868 DRM_INFO("amdgpu: finishing device.\n"); 2869 adev->shutdown = true; 2870 /* disable all interrupts */ 2871 amdgpu_irq_disable_all(adev); 2872 if (adev->mode_info.mode_config_initialized){ 2873 if (!amdgpu_device_has_dc_support(adev)) 2874 drm_helper_force_disable_all(adev->ddev); 2875 else 2876 drm_atomic_helper_shutdown(adev->ddev); 2877 } 2878 amdgpu_fence_driver_fini(adev); 2879 amdgpu_pm_sysfs_fini(adev); 2880 amdgpu_fbdev_fini(adev); 2881 r = amdgpu_device_ip_fini(adev); 2882 if (adev->firmware.gpu_info_fw) { 2883 release_firmware(adev->firmware.gpu_info_fw); 2884 adev->firmware.gpu_info_fw = NULL; 2885 } 2886 adev->accel_working = false; 2887 cancel_delayed_work_sync(&adev->delayed_init_work); 2888 /* free i2c buses */ 2889 if (!amdgpu_device_has_dc_support(adev)) 2890 amdgpu_i2c_fini(adev); 2891 2892 if (amdgpu_emu_mode != 1) 2893 amdgpu_atombios_fini(adev); 2894 2895 kfree(adev->bios); 2896 adev->bios = NULL; 2897 if (!pci_is_thunderbolt_attached(adev->pdev)) 2898 vga_switcheroo_unregister_client(adev->pdev); 2899 if (adev->flags & AMD_IS_PX) 2900 vga_switcheroo_fini_domain_pm_ops(adev->dev); 2901 vga_client_register(adev->pdev, NULL, NULL, NULL); 2902 if (adev->rio_mem) 2903 pci_iounmap(adev->pdev, adev->rio_mem); 2904 adev->rio_mem = NULL; 2905 iounmap(adev->rmmio); 2906 adev->rmmio = NULL; 2907 amdgpu_device_doorbell_fini(adev); 2908 if (amdgpu_sriov_vf(adev) && amdgim_is_hwperf(adev)) 2909 amdgpu_pm_virt_sysfs_fini(adev); 2910 2911 amdgpu_debugfs_regs_cleanup(adev); 2912 device_remove_file(adev->dev, &dev_attr_pcie_replay_count); 2913 amdgpu_ucode_sysfs_fini(adev); 2914 if (IS_ENABLED(CONFIG_PERF_EVENTS)) 2915 amdgpu_pmu_fini(adev); 2916 amdgpu_debugfs_preempt_cleanup(adev); 2917 if (amdgpu_discovery && adev->asic_type >= CHIP_NAVI10) 2918 amdgpu_discovery_fini(adev); 2919 } 2920 2921 2922 /* 2923 * Suspend & resume. 2924 */ 2925 /** 2926 * amdgpu_device_suspend - initiate device suspend 2927 * 2928 * @dev: drm dev pointer 2929 * @suspend: suspend state 2930 * @fbcon : notify the fbdev of suspend 2931 * 2932 * Puts the hw in the suspend state (all asics). 2933 * Returns 0 for success or an error on failure. 2934 * Called at driver suspend. 2935 */ 2936 int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon) 2937 { 2938 struct amdgpu_device *adev; 2939 struct drm_crtc *crtc; 2940 struct drm_connector *connector; 2941 int r; 2942 2943 if (dev == NULL || dev->dev_private == NULL) { 2944 return -ENODEV; 2945 } 2946 2947 adev = dev->dev_private; 2948 2949 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF) 2950 return 0; 2951 2952 adev->in_suspend = true; 2953 drm_kms_helper_poll_disable(dev); 2954 2955 if (fbcon) 2956 amdgpu_fbdev_set_suspend(adev, 1); 2957 2958 cancel_delayed_work_sync(&adev->delayed_init_work); 2959 2960 if (!amdgpu_device_has_dc_support(adev)) { 2961 /* turn off display hw */ 2962 drm_modeset_lock_all(dev); 2963 list_for_each_entry(connector, &dev->mode_config.connector_list, head) { 2964 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF); 2965 } 2966 drm_modeset_unlock_all(dev); 2967 /* unpin the front buffers and cursors */ 2968 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { 2969 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc); 2970 struct drm_framebuffer *fb = crtc->primary->fb; 2971 struct amdgpu_bo *robj; 2972 2973 if (amdgpu_crtc->cursor_bo && !adev->enable_virtual_display) { 2974 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo); 2975 r = amdgpu_bo_reserve(aobj, true); 2976 if (r == 0) { 2977 amdgpu_bo_unpin(aobj); 2978 amdgpu_bo_unreserve(aobj); 2979 } 2980 } 2981 2982 if (fb == NULL || fb->obj[0] == NULL) { 2983 continue; 2984 } 2985 robj = gem_to_amdgpu_bo(fb->obj[0]); 2986 /* don't unpin kernel fb objects */ 2987 if (!amdgpu_fbdev_robj_is_fb(adev, robj)) { 2988 r = amdgpu_bo_reserve(robj, true); 2989 if (r == 0) { 2990 amdgpu_bo_unpin(robj); 2991 amdgpu_bo_unreserve(robj); 2992 } 2993 } 2994 } 2995 } 2996 2997 amdgpu_amdkfd_suspend(adev); 2998 2999 amdgpu_ras_suspend(adev); 3000 3001 r = amdgpu_device_ip_suspend_phase1(adev); 3002 3003 /* evict vram memory */ 3004 amdgpu_bo_evict_vram(adev); 3005 3006 amdgpu_fence_driver_suspend(adev); 3007 3008 r = amdgpu_device_ip_suspend_phase2(adev); 3009 3010 /* evict remaining vram memory 3011 * This second call to evict vram is to evict the gart page table 3012 * using the CPU. 3013 */ 3014 amdgpu_bo_evict_vram(adev); 3015 3016 pci_save_state(dev->pdev); 3017 if (suspend) { 3018 /* Shut down the device */ 3019 pci_disable_device(dev->pdev); 3020 pci_set_power_state(dev->pdev, PCI_D3hot); 3021 } else { 3022 r = amdgpu_asic_reset(adev); 3023 if (r) 3024 DRM_ERROR("amdgpu asic reset failed\n"); 3025 } 3026 3027 return 0; 3028 } 3029 3030 /** 3031 * amdgpu_device_resume - initiate device resume 3032 * 3033 * @dev: drm dev pointer 3034 * @resume: resume state 3035 * @fbcon : notify the fbdev of resume 3036 * 3037 * Bring the hw back to operating state (all asics). 3038 * Returns 0 for success or an error on failure. 3039 * Called at driver resume. 3040 */ 3041 int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon) 3042 { 3043 struct drm_connector *connector; 3044 struct amdgpu_device *adev = dev->dev_private; 3045 struct drm_crtc *crtc; 3046 int r = 0; 3047 3048 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF) 3049 return 0; 3050 3051 if (resume) { 3052 pci_set_power_state(dev->pdev, PCI_D0); 3053 pci_restore_state(dev->pdev); 3054 r = pci_enable_device(dev->pdev); 3055 if (r) 3056 return r; 3057 } 3058 3059 /* post card */ 3060 if (amdgpu_device_need_post(adev)) { 3061 r = amdgpu_atom_asic_init(adev->mode_info.atom_context); 3062 if (r) 3063 DRM_ERROR("amdgpu asic init failed\n"); 3064 } 3065 3066 r = amdgpu_device_ip_resume(adev); 3067 if (r) { 3068 DRM_ERROR("amdgpu_device_ip_resume failed (%d).\n", r); 3069 return r; 3070 } 3071 amdgpu_fence_driver_resume(adev); 3072 3073 3074 r = amdgpu_device_ip_late_init(adev); 3075 if (r) 3076 return r; 3077 3078 queue_delayed_work(system_wq, &adev->delayed_init_work, 3079 msecs_to_jiffies(AMDGPU_RESUME_MS)); 3080 3081 if (!amdgpu_device_has_dc_support(adev)) { 3082 /* pin cursors */ 3083 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) { 3084 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc); 3085 3086 if (amdgpu_crtc->cursor_bo && !adev->enable_virtual_display) { 3087 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo); 3088 r = amdgpu_bo_reserve(aobj, true); 3089 if (r == 0) { 3090 r = amdgpu_bo_pin(aobj, AMDGPU_GEM_DOMAIN_VRAM); 3091 if (r != 0) 3092 DRM_ERROR("Failed to pin cursor BO (%d)\n", r); 3093 amdgpu_crtc->cursor_addr = amdgpu_bo_gpu_offset(aobj); 3094 amdgpu_bo_unreserve(aobj); 3095 } 3096 } 3097 } 3098 } 3099 r = amdgpu_amdkfd_resume(adev); 3100 if (r) 3101 return r; 3102 3103 /* Make sure IB tests flushed */ 3104 flush_delayed_work(&adev->delayed_init_work); 3105 3106 /* blat the mode back in */ 3107 if (fbcon) { 3108 if (!amdgpu_device_has_dc_support(adev)) { 3109 /* pre DCE11 */ 3110 drm_helper_resume_force_mode(dev); 3111 3112 /* turn on display hw */ 3113 drm_modeset_lock_all(dev); 3114 list_for_each_entry(connector, &dev->mode_config.connector_list, head) { 3115 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON); 3116 } 3117 drm_modeset_unlock_all(dev); 3118 } 3119 amdgpu_fbdev_set_suspend(adev, 0); 3120 } 3121 3122 drm_kms_helper_poll_enable(dev); 3123 3124 amdgpu_ras_resume(adev); 3125 3126 /* 3127 * Most of the connector probing functions try to acquire runtime pm 3128 * refs to ensure that the GPU is powered on when connector polling is 3129 * performed. Since we're calling this from a runtime PM callback, 3130 * trying to acquire rpm refs will cause us to deadlock. 3131 * 3132 * Since we're guaranteed to be holding the rpm lock, it's safe to 3133 * temporarily disable the rpm helpers so this doesn't deadlock us. 3134 */ 3135 #ifdef CONFIG_PM 3136 dev->dev->power.disable_depth++; 3137 #endif 3138 if (!amdgpu_device_has_dc_support(adev)) 3139 drm_helper_hpd_irq_event(dev); 3140 else 3141 drm_kms_helper_hotplug_event(dev); 3142 #ifdef CONFIG_PM 3143 dev->dev->power.disable_depth--; 3144 #endif 3145 adev->in_suspend = false; 3146 3147 return 0; 3148 } 3149 3150 /** 3151 * amdgpu_device_ip_check_soft_reset - did soft reset succeed 3152 * 3153 * @adev: amdgpu_device pointer 3154 * 3155 * The list of all the hardware IPs that make up the asic is walked and 3156 * the check_soft_reset callbacks are run. check_soft_reset determines 3157 * if the asic is still hung or not. 3158 * Returns true if any of the IPs are still in a hung state, false if not. 3159 */ 3160 static bool amdgpu_device_ip_check_soft_reset(struct amdgpu_device *adev) 3161 { 3162 int i; 3163 bool asic_hang = false; 3164 3165 if (amdgpu_sriov_vf(adev)) 3166 return true; 3167 3168 if (amdgpu_asic_need_full_reset(adev)) 3169 return true; 3170 3171 for (i = 0; i < adev->num_ip_blocks; i++) { 3172 if (!adev->ip_blocks[i].status.valid) 3173 continue; 3174 if (adev->ip_blocks[i].version->funcs->check_soft_reset) 3175 adev->ip_blocks[i].status.hang = 3176 adev->ip_blocks[i].version->funcs->check_soft_reset(adev); 3177 if (adev->ip_blocks[i].status.hang) { 3178 DRM_INFO("IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name); 3179 asic_hang = true; 3180 } 3181 } 3182 return asic_hang; 3183 } 3184 3185 /** 3186 * amdgpu_device_ip_pre_soft_reset - prepare for soft reset 3187 * 3188 * @adev: amdgpu_device pointer 3189 * 3190 * The list of all the hardware IPs that make up the asic is walked and the 3191 * pre_soft_reset callbacks are run if the block is hung. pre_soft_reset 3192 * handles any IP specific hardware or software state changes that are 3193 * necessary for a soft reset to succeed. 3194 * Returns 0 on success, negative error code on failure. 3195 */ 3196 static int amdgpu_device_ip_pre_soft_reset(struct amdgpu_device *adev) 3197 { 3198 int i, r = 0; 3199 3200 for (i = 0; i < adev->num_ip_blocks; i++) { 3201 if (!adev->ip_blocks[i].status.valid) 3202 continue; 3203 if (adev->ip_blocks[i].status.hang && 3204 adev->ip_blocks[i].version->funcs->pre_soft_reset) { 3205 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev); 3206 if (r) 3207 return r; 3208 } 3209 } 3210 3211 return 0; 3212 } 3213 3214 /** 3215 * amdgpu_device_ip_need_full_reset - check if a full asic reset is needed 3216 * 3217 * @adev: amdgpu_device pointer 3218 * 3219 * Some hardware IPs cannot be soft reset. If they are hung, a full gpu 3220 * reset is necessary to recover. 3221 * Returns true if a full asic reset is required, false if not. 3222 */ 3223 static bool amdgpu_device_ip_need_full_reset(struct amdgpu_device *adev) 3224 { 3225 int i; 3226 3227 if (amdgpu_asic_need_full_reset(adev)) 3228 return true; 3229 3230 for (i = 0; i < adev->num_ip_blocks; i++) { 3231 if (!adev->ip_blocks[i].status.valid) 3232 continue; 3233 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) || 3234 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) || 3235 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) || 3236 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) || 3237 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) { 3238 if (adev->ip_blocks[i].status.hang) { 3239 DRM_INFO("Some block need full reset!\n"); 3240 return true; 3241 } 3242 } 3243 } 3244 return false; 3245 } 3246 3247 /** 3248 * amdgpu_device_ip_soft_reset - do a soft reset 3249 * 3250 * @adev: amdgpu_device pointer 3251 * 3252 * The list of all the hardware IPs that make up the asic is walked and the 3253 * soft_reset callbacks are run if the block is hung. soft_reset handles any 3254 * IP specific hardware or software state changes that are necessary to soft 3255 * reset the IP. 3256 * Returns 0 on success, negative error code on failure. 3257 */ 3258 static int amdgpu_device_ip_soft_reset(struct amdgpu_device *adev) 3259 { 3260 int i, r = 0; 3261 3262 for (i = 0; i < adev->num_ip_blocks; i++) { 3263 if (!adev->ip_blocks[i].status.valid) 3264 continue; 3265 if (adev->ip_blocks[i].status.hang && 3266 adev->ip_blocks[i].version->funcs->soft_reset) { 3267 r = adev->ip_blocks[i].version->funcs->soft_reset(adev); 3268 if (r) 3269 return r; 3270 } 3271 } 3272 3273 return 0; 3274 } 3275 3276 /** 3277 * amdgpu_device_ip_post_soft_reset - clean up from soft reset 3278 * 3279 * @adev: amdgpu_device pointer 3280 * 3281 * The list of all the hardware IPs that make up the asic is walked and the 3282 * post_soft_reset callbacks are run if the asic was hung. post_soft_reset 3283 * handles any IP specific hardware or software state changes that are 3284 * necessary after the IP has been soft reset. 3285 * Returns 0 on success, negative error code on failure. 3286 */ 3287 static int amdgpu_device_ip_post_soft_reset(struct amdgpu_device *adev) 3288 { 3289 int i, r = 0; 3290 3291 for (i = 0; i < adev->num_ip_blocks; i++) { 3292 if (!adev->ip_blocks[i].status.valid) 3293 continue; 3294 if (adev->ip_blocks[i].status.hang && 3295 adev->ip_blocks[i].version->funcs->post_soft_reset) 3296 r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev); 3297 if (r) 3298 return r; 3299 } 3300 3301 return 0; 3302 } 3303 3304 /** 3305 * amdgpu_device_recover_vram - Recover some VRAM contents 3306 * 3307 * @adev: amdgpu_device pointer 3308 * 3309 * Restores the contents of VRAM buffers from the shadows in GTT. Used to 3310 * restore things like GPUVM page tables after a GPU reset where 3311 * the contents of VRAM might be lost. 3312 * 3313 * Returns: 3314 * 0 on success, negative error code on failure. 3315 */ 3316 static int amdgpu_device_recover_vram(struct amdgpu_device *adev) 3317 { 3318 struct dma_fence *fence = NULL, *next = NULL; 3319 struct amdgpu_bo *shadow; 3320 long r = 1, tmo; 3321 3322 if (amdgpu_sriov_runtime(adev)) 3323 tmo = msecs_to_jiffies(8000); 3324 else 3325 tmo = msecs_to_jiffies(100); 3326 3327 DRM_INFO("recover vram bo from shadow start\n"); 3328 mutex_lock(&adev->shadow_list_lock); 3329 list_for_each_entry(shadow, &adev->shadow_list, shadow_list) { 3330 3331 /* No need to recover an evicted BO */ 3332 if (shadow->tbo.mem.mem_type != TTM_PL_TT || 3333 shadow->tbo.mem.start == AMDGPU_BO_INVALID_OFFSET || 3334 shadow->parent->tbo.mem.mem_type != TTM_PL_VRAM) 3335 continue; 3336 3337 r = amdgpu_bo_restore_shadow(shadow, &next); 3338 if (r) 3339 break; 3340 3341 if (fence) { 3342 tmo = dma_fence_wait_timeout(fence, false, tmo); 3343 dma_fence_put(fence); 3344 fence = next; 3345 if (tmo == 0) { 3346 r = -ETIMEDOUT; 3347 break; 3348 } else if (tmo < 0) { 3349 r = tmo; 3350 break; 3351 } 3352 } else { 3353 fence = next; 3354 } 3355 } 3356 mutex_unlock(&adev->shadow_list_lock); 3357 3358 if (fence) 3359 tmo = dma_fence_wait_timeout(fence, false, tmo); 3360 dma_fence_put(fence); 3361 3362 if (r < 0 || tmo <= 0) { 3363 DRM_ERROR("recover vram bo from shadow failed, r is %ld, tmo is %ld\n", r, tmo); 3364 return -EIO; 3365 } 3366 3367 DRM_INFO("recover vram bo from shadow done\n"); 3368 return 0; 3369 } 3370 3371 3372 /** 3373 * amdgpu_device_reset_sriov - reset ASIC for SR-IOV vf 3374 * 3375 * @adev: amdgpu device pointer 3376 * @from_hypervisor: request from hypervisor 3377 * 3378 * do VF FLR and reinitialize Asic 3379 * return 0 means succeeded otherwise failed 3380 */ 3381 static int amdgpu_device_reset_sriov(struct amdgpu_device *adev, 3382 bool from_hypervisor) 3383 { 3384 int r; 3385 3386 if (from_hypervisor) 3387 r = amdgpu_virt_request_full_gpu(adev, true); 3388 else 3389 r = amdgpu_virt_reset_gpu(adev); 3390 if (r) 3391 return r; 3392 3393 amdgpu_amdkfd_pre_reset(adev); 3394 3395 /* Resume IP prior to SMC */ 3396 r = amdgpu_device_ip_reinit_early_sriov(adev); 3397 if (r) 3398 goto error; 3399 3400 /* we need recover gart prior to run SMC/CP/SDMA resume */ 3401 amdgpu_gtt_mgr_recover(&adev->mman.bdev.man[TTM_PL_TT]); 3402 3403 r = amdgpu_device_fw_loading(adev); 3404 if (r) 3405 return r; 3406 3407 /* now we are okay to resume SMC/CP/SDMA */ 3408 r = amdgpu_device_ip_reinit_late_sriov(adev); 3409 if (r) 3410 goto error; 3411 3412 amdgpu_irq_gpu_reset_resume_helper(adev); 3413 r = amdgpu_ib_ring_tests(adev); 3414 amdgpu_amdkfd_post_reset(adev); 3415 3416 error: 3417 amdgpu_virt_init_data_exchange(adev); 3418 amdgpu_virt_release_full_gpu(adev, true); 3419 if (!r && adev->virt.gim_feature & AMDGIM_FEATURE_GIM_FLR_VRAMLOST) { 3420 atomic_inc(&adev->vram_lost_counter); 3421 r = amdgpu_device_recover_vram(adev); 3422 } 3423 3424 return r; 3425 } 3426 3427 /** 3428 * amdgpu_device_should_recover_gpu - check if we should try GPU recovery 3429 * 3430 * @adev: amdgpu device pointer 3431 * 3432 * Check amdgpu_gpu_recovery and SRIOV status to see if we should try to recover 3433 * a hung GPU. 3434 */ 3435 bool amdgpu_device_should_recover_gpu(struct amdgpu_device *adev) 3436 { 3437 if (!amdgpu_device_ip_check_soft_reset(adev)) { 3438 DRM_INFO("Timeout, but no hardware hang detected.\n"); 3439 return false; 3440 } 3441 3442 if (amdgpu_gpu_recovery == 0) 3443 goto disabled; 3444 3445 if (amdgpu_sriov_vf(adev)) 3446 return true; 3447 3448 if (amdgpu_gpu_recovery == -1) { 3449 switch (adev->asic_type) { 3450 case CHIP_BONAIRE: 3451 case CHIP_HAWAII: 3452 case CHIP_TOPAZ: 3453 case CHIP_TONGA: 3454 case CHIP_FIJI: 3455 case CHIP_POLARIS10: 3456 case CHIP_POLARIS11: 3457 case CHIP_POLARIS12: 3458 case CHIP_VEGAM: 3459 case CHIP_VEGA20: 3460 case CHIP_VEGA10: 3461 case CHIP_VEGA12: 3462 break; 3463 default: 3464 goto disabled; 3465 } 3466 } 3467 3468 return true; 3469 3470 disabled: 3471 DRM_INFO("GPU recovery disabled.\n"); 3472 return false; 3473 } 3474 3475 3476 static int amdgpu_device_pre_asic_reset(struct amdgpu_device *adev, 3477 struct amdgpu_job *job, 3478 bool *need_full_reset_arg) 3479 { 3480 int i, r = 0; 3481 bool need_full_reset = *need_full_reset_arg; 3482 3483 /* block all schedulers and reset given job's ring */ 3484 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) { 3485 struct amdgpu_ring *ring = adev->rings[i]; 3486 3487 if (!ring || !ring->sched.thread) 3488 continue; 3489 3490 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */ 3491 amdgpu_fence_driver_force_completion(ring); 3492 } 3493 3494 if(job) 3495 drm_sched_increase_karma(&job->base); 3496 3497 /* Don't suspend on bare metal if we are not going to HW reset the ASIC */ 3498 if (!amdgpu_sriov_vf(adev)) { 3499 3500 if (!need_full_reset) 3501 need_full_reset = amdgpu_device_ip_need_full_reset(adev); 3502 3503 if (!need_full_reset) { 3504 amdgpu_device_ip_pre_soft_reset(adev); 3505 r = amdgpu_device_ip_soft_reset(adev); 3506 amdgpu_device_ip_post_soft_reset(adev); 3507 if (r || amdgpu_device_ip_check_soft_reset(adev)) { 3508 DRM_INFO("soft reset failed, will fallback to full reset!\n"); 3509 need_full_reset = true; 3510 } 3511 } 3512 3513 if (need_full_reset) 3514 r = amdgpu_device_ip_suspend(adev); 3515 3516 *need_full_reset_arg = need_full_reset; 3517 } 3518 3519 return r; 3520 } 3521 3522 static int amdgpu_do_asic_reset(struct amdgpu_hive_info *hive, 3523 struct list_head *device_list_handle, 3524 bool *need_full_reset_arg) 3525 { 3526 struct amdgpu_device *tmp_adev = NULL; 3527 bool need_full_reset = *need_full_reset_arg, vram_lost = false; 3528 int r = 0; 3529 3530 /* 3531 * ASIC reset has to be done on all HGMI hive nodes ASAP 3532 * to allow proper links negotiation in FW (within 1 sec) 3533 */ 3534 if (need_full_reset) { 3535 list_for_each_entry(tmp_adev, device_list_handle, gmc.xgmi.head) { 3536 /* For XGMI run all resets in parallel to speed up the process */ 3537 if (tmp_adev->gmc.xgmi.num_physical_nodes > 1) { 3538 if (!queue_work(system_highpri_wq, &tmp_adev->xgmi_reset_work)) 3539 r = -EALREADY; 3540 } else 3541 r = amdgpu_asic_reset(tmp_adev); 3542 3543 if (r) { 3544 DRM_ERROR("ASIC reset failed with error, %d for drm dev, %s", 3545 r, tmp_adev->ddev->unique); 3546 break; 3547 } 3548 } 3549 3550 /* For XGMI wait for all PSP resets to complete before proceed */ 3551 if (!r) { 3552 list_for_each_entry(tmp_adev, device_list_handle, 3553 gmc.xgmi.head) { 3554 if (tmp_adev->gmc.xgmi.num_physical_nodes > 1) { 3555 flush_work(&tmp_adev->xgmi_reset_work); 3556 r = tmp_adev->asic_reset_res; 3557 if (r) 3558 break; 3559 } 3560 } 3561 3562 list_for_each_entry(tmp_adev, device_list_handle, 3563 gmc.xgmi.head) { 3564 amdgpu_ras_reserve_bad_pages(tmp_adev); 3565 } 3566 } 3567 } 3568 3569 3570 list_for_each_entry(tmp_adev, device_list_handle, gmc.xgmi.head) { 3571 if (need_full_reset) { 3572 /* post card */ 3573 if (amdgpu_atom_asic_init(tmp_adev->mode_info.atom_context)) 3574 DRM_WARN("asic atom init failed!"); 3575 3576 if (!r) { 3577 dev_info(tmp_adev->dev, "GPU reset succeeded, trying to resume\n"); 3578 r = amdgpu_device_ip_resume_phase1(tmp_adev); 3579 if (r) 3580 goto out; 3581 3582 vram_lost = amdgpu_device_check_vram_lost(tmp_adev); 3583 if (vram_lost) { 3584 DRM_INFO("VRAM is lost due to GPU reset!\n"); 3585 atomic_inc(&tmp_adev->vram_lost_counter); 3586 } 3587 3588 r = amdgpu_gtt_mgr_recover( 3589 &tmp_adev->mman.bdev.man[TTM_PL_TT]); 3590 if (r) 3591 goto out; 3592 3593 r = amdgpu_device_fw_loading(tmp_adev); 3594 if (r) 3595 return r; 3596 3597 r = amdgpu_device_ip_resume_phase2(tmp_adev); 3598 if (r) 3599 goto out; 3600 3601 if (vram_lost) 3602 amdgpu_device_fill_reset_magic(tmp_adev); 3603 3604 /* 3605 * Add this ASIC as tracked as reset was already 3606 * complete successfully. 3607 */ 3608 amdgpu_register_gpu_instance(tmp_adev); 3609 3610 r = amdgpu_device_ip_late_init(tmp_adev); 3611 if (r) 3612 goto out; 3613 3614 /* must succeed. */ 3615 amdgpu_ras_resume(tmp_adev); 3616 3617 /* Update PSP FW topology after reset */ 3618 if (hive && tmp_adev->gmc.xgmi.num_physical_nodes > 1) 3619 r = amdgpu_xgmi_update_topology(hive, tmp_adev); 3620 } 3621 } 3622 3623 3624 out: 3625 if (!r) { 3626 amdgpu_irq_gpu_reset_resume_helper(tmp_adev); 3627 r = amdgpu_ib_ring_tests(tmp_adev); 3628 if (r) { 3629 dev_err(tmp_adev->dev, "ib ring test failed (%d).\n", r); 3630 r = amdgpu_device_ip_suspend(tmp_adev); 3631 need_full_reset = true; 3632 r = -EAGAIN; 3633 goto end; 3634 } 3635 } 3636 3637 if (!r) 3638 r = amdgpu_device_recover_vram(tmp_adev); 3639 else 3640 tmp_adev->asic_reset_res = r; 3641 } 3642 3643 end: 3644 *need_full_reset_arg = need_full_reset; 3645 return r; 3646 } 3647 3648 static bool amdgpu_device_lock_adev(struct amdgpu_device *adev, bool trylock) 3649 { 3650 if (trylock) { 3651 if (!mutex_trylock(&adev->lock_reset)) 3652 return false; 3653 } else 3654 mutex_lock(&adev->lock_reset); 3655 3656 atomic_inc(&adev->gpu_reset_counter); 3657 adev->in_gpu_reset = 1; 3658 switch (amdgpu_asic_reset_method(adev)) { 3659 case AMD_RESET_METHOD_MODE1: 3660 adev->mp1_state = PP_MP1_STATE_SHUTDOWN; 3661 break; 3662 case AMD_RESET_METHOD_MODE2: 3663 adev->mp1_state = PP_MP1_STATE_RESET; 3664 break; 3665 default: 3666 adev->mp1_state = PP_MP1_STATE_NONE; 3667 break; 3668 } 3669 /* Block kfd: SRIOV would do it separately */ 3670 if (!amdgpu_sriov_vf(adev)) 3671 amdgpu_amdkfd_pre_reset(adev); 3672 3673 return true; 3674 } 3675 3676 static void amdgpu_device_unlock_adev(struct amdgpu_device *adev) 3677 { 3678 /*unlock kfd: SRIOV would do it separately */ 3679 if (!amdgpu_sriov_vf(adev)) 3680 amdgpu_amdkfd_post_reset(adev); 3681 amdgpu_vf_error_trans_all(adev); 3682 adev->mp1_state = PP_MP1_STATE_NONE; 3683 adev->in_gpu_reset = 0; 3684 mutex_unlock(&adev->lock_reset); 3685 } 3686 3687 3688 /** 3689 * amdgpu_device_gpu_recover - reset the asic and recover scheduler 3690 * 3691 * @adev: amdgpu device pointer 3692 * @job: which job trigger hang 3693 * 3694 * Attempt to reset the GPU if it has hung (all asics). 3695 * Attempt to do soft-reset or full-reset and reinitialize Asic 3696 * Returns 0 for success or an error on failure. 3697 */ 3698 3699 int amdgpu_device_gpu_recover(struct amdgpu_device *adev, 3700 struct amdgpu_job *job) 3701 { 3702 struct list_head device_list, *device_list_handle = NULL; 3703 bool need_full_reset, job_signaled; 3704 struct amdgpu_hive_info *hive = NULL; 3705 struct amdgpu_device *tmp_adev = NULL; 3706 int i, r = 0; 3707 3708 need_full_reset = job_signaled = false; 3709 INIT_LIST_HEAD(&device_list); 3710 3711 dev_info(adev->dev, "GPU reset begin!\n"); 3712 3713 cancel_delayed_work_sync(&adev->delayed_init_work); 3714 3715 hive = amdgpu_get_xgmi_hive(adev, false); 3716 3717 /* 3718 * Here we trylock to avoid chain of resets executing from 3719 * either trigger by jobs on different adevs in XGMI hive or jobs on 3720 * different schedulers for same device while this TO handler is running. 3721 * We always reset all schedulers for device and all devices for XGMI 3722 * hive so that should take care of them too. 3723 */ 3724 3725 if (hive && !mutex_trylock(&hive->reset_lock)) { 3726 DRM_INFO("Bailing on TDR for s_job:%llx, hive: %llx as another already in progress", 3727 job->base.id, hive->hive_id); 3728 return 0; 3729 } 3730 3731 /* Start with adev pre asic reset first for soft reset check.*/ 3732 if (!amdgpu_device_lock_adev(adev, !hive)) { 3733 DRM_INFO("Bailing on TDR for s_job:%llx, as another already in progress", 3734 job->base.id); 3735 return 0; 3736 } 3737 3738 /* Build list of devices to reset */ 3739 if (adev->gmc.xgmi.num_physical_nodes > 1) { 3740 if (!hive) { 3741 amdgpu_device_unlock_adev(adev); 3742 return -ENODEV; 3743 } 3744 3745 /* 3746 * In case we are in XGMI hive mode device reset is done for all the 3747 * nodes in the hive to retrain all XGMI links and hence the reset 3748 * sequence is executed in loop on all nodes. 3749 */ 3750 device_list_handle = &hive->device_list; 3751 } else { 3752 list_add_tail(&adev->gmc.xgmi.head, &device_list); 3753 device_list_handle = &device_list; 3754 } 3755 3756 /* 3757 * Mark these ASICs to be reseted as untracked first 3758 * And add them back after reset completed 3759 */ 3760 list_for_each_entry(tmp_adev, device_list_handle, gmc.xgmi.head) 3761 amdgpu_unregister_gpu_instance(tmp_adev); 3762 3763 /* block all schedulers and reset given job's ring */ 3764 list_for_each_entry(tmp_adev, device_list_handle, gmc.xgmi.head) { 3765 /* disable ras on ALL IPs */ 3766 if (amdgpu_device_ip_need_full_reset(tmp_adev)) 3767 amdgpu_ras_suspend(tmp_adev); 3768 3769 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) { 3770 struct amdgpu_ring *ring = tmp_adev->rings[i]; 3771 3772 if (!ring || !ring->sched.thread) 3773 continue; 3774 3775 drm_sched_stop(&ring->sched, &job->base); 3776 } 3777 } 3778 3779 3780 /* 3781 * Must check guilty signal here since after this point all old 3782 * HW fences are force signaled. 3783 * 3784 * job->base holds a reference to parent fence 3785 */ 3786 if (job && job->base.s_fence->parent && 3787 dma_fence_is_signaled(job->base.s_fence->parent)) 3788 job_signaled = true; 3789 3790 if (!amdgpu_device_ip_need_full_reset(adev)) 3791 device_list_handle = &device_list; 3792 3793 if (job_signaled) { 3794 dev_info(adev->dev, "Guilty job already signaled, skipping HW reset"); 3795 goto skip_hw_reset; 3796 } 3797 3798 3799 /* Guilty job will be freed after this*/ 3800 r = amdgpu_device_pre_asic_reset(adev, 3801 job, 3802 &need_full_reset); 3803 if (r) { 3804 /*TODO Should we stop ?*/ 3805 DRM_ERROR("GPU pre asic reset failed with err, %d for drm dev, %s ", 3806 r, adev->ddev->unique); 3807 adev->asic_reset_res = r; 3808 } 3809 3810 retry: /* Rest of adevs pre asic reset from XGMI hive. */ 3811 list_for_each_entry(tmp_adev, device_list_handle, gmc.xgmi.head) { 3812 3813 if (tmp_adev == adev) 3814 continue; 3815 3816 amdgpu_device_lock_adev(tmp_adev, false); 3817 r = amdgpu_device_pre_asic_reset(tmp_adev, 3818 NULL, 3819 &need_full_reset); 3820 /*TODO Should we stop ?*/ 3821 if (r) { 3822 DRM_ERROR("GPU pre asic reset failed with err, %d for drm dev, %s ", 3823 r, tmp_adev->ddev->unique); 3824 tmp_adev->asic_reset_res = r; 3825 } 3826 } 3827 3828 /* Actual ASIC resets if needed.*/ 3829 /* TODO Implement XGMI hive reset logic for SRIOV */ 3830 if (amdgpu_sriov_vf(adev)) { 3831 r = amdgpu_device_reset_sriov(adev, job ? false : true); 3832 if (r) 3833 adev->asic_reset_res = r; 3834 } else { 3835 r = amdgpu_do_asic_reset(hive, device_list_handle, &need_full_reset); 3836 if (r && r == -EAGAIN) 3837 goto retry; 3838 } 3839 3840 skip_hw_reset: 3841 3842 /* Post ASIC reset for all devs .*/ 3843 list_for_each_entry(tmp_adev, device_list_handle, gmc.xgmi.head) { 3844 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) { 3845 struct amdgpu_ring *ring = tmp_adev->rings[i]; 3846 3847 if (!ring || !ring->sched.thread) 3848 continue; 3849 3850 /* No point to resubmit jobs if we didn't HW reset*/ 3851 if (!tmp_adev->asic_reset_res && !job_signaled) 3852 drm_sched_resubmit_jobs(&ring->sched); 3853 3854 drm_sched_start(&ring->sched, !tmp_adev->asic_reset_res); 3855 } 3856 3857 if (!amdgpu_device_has_dc_support(tmp_adev) && !job_signaled) { 3858 drm_helper_resume_force_mode(tmp_adev->ddev); 3859 } 3860 3861 tmp_adev->asic_reset_res = 0; 3862 3863 if (r) { 3864 /* bad news, how to tell it to userspace ? */ 3865 dev_info(tmp_adev->dev, "GPU reset(%d) failed\n", atomic_read(&adev->gpu_reset_counter)); 3866 amdgpu_vf_error_put(tmp_adev, AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, r); 3867 } else { 3868 dev_info(tmp_adev->dev, "GPU reset(%d) succeeded!\n", atomic_read(&adev->gpu_reset_counter)); 3869 } 3870 3871 amdgpu_device_unlock_adev(tmp_adev); 3872 } 3873 3874 if (hive) 3875 mutex_unlock(&hive->reset_lock); 3876 3877 if (r) 3878 dev_info(adev->dev, "GPU reset end with ret = %d\n", r); 3879 return r; 3880 } 3881 3882 /** 3883 * amdgpu_device_get_pcie_info - fence pcie info about the PCIE slot 3884 * 3885 * @adev: amdgpu_device pointer 3886 * 3887 * Fetchs and stores in the driver the PCIE capabilities (gen speed 3888 * and lanes) of the slot the device is in. Handles APUs and 3889 * virtualized environments where PCIE config space may not be available. 3890 */ 3891 static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev) 3892 { 3893 struct pci_dev *pdev; 3894 enum pci_bus_speed speed_cap, platform_speed_cap; 3895 enum pcie_link_width platform_link_width; 3896 3897 if (amdgpu_pcie_gen_cap) 3898 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap; 3899 3900 if (amdgpu_pcie_lane_cap) 3901 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap; 3902 3903 /* covers APUs as well */ 3904 if (pci_is_root_bus(adev->pdev->bus)) { 3905 if (adev->pm.pcie_gen_mask == 0) 3906 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK; 3907 if (adev->pm.pcie_mlw_mask == 0) 3908 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK; 3909 return; 3910 } 3911 3912 if (adev->pm.pcie_gen_mask && adev->pm.pcie_mlw_mask) 3913 return; 3914 3915 pcie_bandwidth_available(adev->pdev, NULL, 3916 &platform_speed_cap, &platform_link_width); 3917 3918 if (adev->pm.pcie_gen_mask == 0) { 3919 /* asic caps */ 3920 pdev = adev->pdev; 3921 speed_cap = pcie_get_speed_cap(pdev); 3922 if (speed_cap == PCI_SPEED_UNKNOWN) { 3923 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 | 3924 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 | 3925 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3); 3926 } else { 3927 if (speed_cap == PCIE_SPEED_16_0GT) 3928 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 | 3929 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 | 3930 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3 | 3931 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN4); 3932 else if (speed_cap == PCIE_SPEED_8_0GT) 3933 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 | 3934 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 | 3935 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3); 3936 else if (speed_cap == PCIE_SPEED_5_0GT) 3937 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 | 3938 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2); 3939 else 3940 adev->pm.pcie_gen_mask |= CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1; 3941 } 3942 /* platform caps */ 3943 if (platform_speed_cap == PCI_SPEED_UNKNOWN) { 3944 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 | 3945 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2); 3946 } else { 3947 if (platform_speed_cap == PCIE_SPEED_16_0GT) 3948 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 | 3949 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 | 3950 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3 | 3951 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4); 3952 else if (platform_speed_cap == PCIE_SPEED_8_0GT) 3953 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 | 3954 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 | 3955 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3); 3956 else if (platform_speed_cap == PCIE_SPEED_5_0GT) 3957 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 | 3958 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2); 3959 else 3960 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1; 3961 3962 } 3963 } 3964 if (adev->pm.pcie_mlw_mask == 0) { 3965 if (platform_link_width == PCIE_LNK_WIDTH_UNKNOWN) { 3966 adev->pm.pcie_mlw_mask |= AMDGPU_DEFAULT_PCIE_MLW_MASK; 3967 } else { 3968 switch (platform_link_width) { 3969 case PCIE_LNK_X32: 3970 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 | 3971 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 | 3972 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 | 3973 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 | 3974 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 | 3975 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 | 3976 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1); 3977 break; 3978 case PCIE_LNK_X16: 3979 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 | 3980 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 | 3981 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 | 3982 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 | 3983 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 | 3984 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1); 3985 break; 3986 case PCIE_LNK_X12: 3987 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 | 3988 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 | 3989 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 | 3990 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 | 3991 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1); 3992 break; 3993 case PCIE_LNK_X8: 3994 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 | 3995 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 | 3996 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 | 3997 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1); 3998 break; 3999 case PCIE_LNK_X4: 4000 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 | 4001 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 | 4002 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1); 4003 break; 4004 case PCIE_LNK_X2: 4005 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 | 4006 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1); 4007 break; 4008 case PCIE_LNK_X1: 4009 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1; 4010 break; 4011 default: 4012 break; 4013 } 4014 } 4015 } 4016 } 4017 4018