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