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