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