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