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