drm/amd/display: For vblank_disable_immediate, check PSR is really used
[linux-2.6-block.git] / drivers / gpu / drm / amd / amdgpu / amdgpu_device.c
CommitLineData
d38ceaf9
AD
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 */
b1ddf548 28#include <linux/power_supply.h>
0875dc9e 29#include <linux/kthread.h>
fdf2f6c5 30#include <linux/module.h>
d38ceaf9
AD
31#include <linux/console.h>
32#include <linux/slab.h>
4a74c38c 33#include <linux/iommu.h>
901e2be2 34#include <linux/pci.h>
fdf2f6c5 35
4562236b 36#include <drm/drm_atomic_helper.h>
fcd70cd3 37#include <drm/drm_probe_helper.h>
d38ceaf9
AD
38#include <drm/amdgpu_drm.h>
39#include <linux/vgaarb.h>
40#include <linux/vga_switcheroo.h>
41#include <linux/efi.h>
42#include "amdgpu.h"
f4b373f4 43#include "amdgpu_trace.h"
d38ceaf9
AD
44#include "amdgpu_i2c.h"
45#include "atom.h"
46#include "amdgpu_atombios.h"
a5bde2f9 47#include "amdgpu_atomfirmware.h"
d0dd7f0c 48#include "amd_pcie.h"
33f34802
KW
49#ifdef CONFIG_DRM_AMDGPU_SI
50#include "si.h"
51#endif
a2e73f56
AD
52#ifdef CONFIG_DRM_AMDGPU_CIK
53#include "cik.h"
54#endif
aaa36a97 55#include "vi.h"
460826e6 56#include "soc15.h"
0a5b8c7b 57#include "nv.h"
d38ceaf9 58#include "bif/bif_4_1_d.h"
bec86378 59#include <linux/firmware.h>
89041940 60#include "amdgpu_vf_error.h"
d38ceaf9 61
ba997709 62#include "amdgpu_amdkfd.h"
d2f52ac8 63#include "amdgpu_pm.h"
d38ceaf9 64
5183411b 65#include "amdgpu_xgmi.h"
c030f2e4 66#include "amdgpu_ras.h"
9c7c85f7 67#include "amdgpu_pmu.h"
bd607166 68#include "amdgpu_fru_eeprom.h"
04442bf7 69#include "amdgpu_reset.h"
5183411b 70
d5ea093e 71#include <linux/suspend.h>
c6a6e2db 72#include <drm/task_barrier.h>
3f12acc8 73#include <linux/pm_runtime.h>
d5ea093e 74
f89f8c6b
AG
75#include <drm/drm_drv.h>
76
e2a75f88 77MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin");
3f76dced 78MODULE_FIRMWARE("amdgpu/vega12_gpu_info.bin");
2d2e5e7e 79MODULE_FIRMWARE("amdgpu/raven_gpu_info.bin");
ad5a67a7 80MODULE_FIRMWARE("amdgpu/picasso_gpu_info.bin");
54c4d17e 81MODULE_FIRMWARE("amdgpu/raven2_gpu_info.bin");
65e60f6e 82MODULE_FIRMWARE("amdgpu/arcturus_gpu_info.bin");
b51a26a0 83MODULE_FIRMWARE("amdgpu/renoir_gpu_info.bin");
23c6268e 84MODULE_FIRMWARE("amdgpu/navi10_gpu_info.bin");
ed42cfe1 85MODULE_FIRMWARE("amdgpu/navi14_gpu_info.bin");
42b325e5 86MODULE_FIRMWARE("amdgpu/navi12_gpu_info.bin");
4e52a9f8 87MODULE_FIRMWARE("amdgpu/vangogh_gpu_info.bin");
8bf84f60 88MODULE_FIRMWARE("amdgpu/yellow_carp_gpu_info.bin");
e2a75f88 89
2dc80b00 90#define AMDGPU_RESUME_MS 2000
7258fa31
SK
91#define AMDGPU_MAX_RETRY_LIMIT 2
92#define AMDGPU_RETRY_SRIOV_RESET(r) ((r) == -EBUSY || (r) == -ETIMEDOUT || (r) == -EINVAL)
2dc80b00 93
050091ab 94const char *amdgpu_asic_name[] = {
da69c161
KW
95 "TAHITI",
96 "PITCAIRN",
97 "VERDE",
98 "OLAND",
99 "HAINAN",
d38ceaf9
AD
100 "BONAIRE",
101 "KAVERI",
102 "KABINI",
103 "HAWAII",
104 "MULLINS",
105 "TOPAZ",
106 "TONGA",
48299f95 107 "FIJI",
d38ceaf9 108 "CARRIZO",
139f4917 109 "STONEY",
2cc0c0b5
FC
110 "POLARIS10",
111 "POLARIS11",
c4642a47 112 "POLARIS12",
48ff108d 113 "VEGAM",
d4196f01 114 "VEGA10",
8fab806a 115 "VEGA12",
956fcddc 116 "VEGA20",
2ca8a5d2 117 "RAVEN",
d6c3b24e 118 "ARCTURUS",
1eee4228 119 "RENOIR",
d46b417a 120 "ALDEBARAN",
852a6626 121 "NAVI10",
d0f56dc2 122 "CYAN_SKILLFISH",
87dbad02 123 "NAVI14",
9802f5d7 124 "NAVI12",
ccaf72d3 125 "SIENNA_CICHLID",
ddd8fbe7 126 "NAVY_FLOUNDER",
4f1e9a76 127 "VANGOGH",
a2468e04 128 "DIMGREY_CAVEFISH",
6f169591 129 "BEIGE_GOBY",
ee9236b7 130 "YELLOW_CARP",
3ae695d6 131 "IP DISCOVERY",
d38ceaf9
AD
132 "LAST",
133};
134
dcea6e65
KR
135/**
136 * DOC: pcie_replay_count
137 *
138 * The amdgpu driver provides a sysfs API for reporting the total number
139 * of PCIe replays (NAKs)
140 * The file pcie_replay_count is used for this and returns the total
141 * number of replays as a sum of the NAKs generated and NAKs received
142 */
143
144static ssize_t amdgpu_device_get_pcie_replay_count(struct device *dev,
145 struct device_attribute *attr, char *buf)
146{
147 struct drm_device *ddev = dev_get_drvdata(dev);
1348969a 148 struct amdgpu_device *adev = drm_to_adev(ddev);
dcea6e65
KR
149 uint64_t cnt = amdgpu_asic_get_pcie_replay_count(adev);
150
36000c7a 151 return sysfs_emit(buf, "%llu\n", cnt);
dcea6e65
KR
152}
153
154static DEVICE_ATTR(pcie_replay_count, S_IRUGO,
155 amdgpu_device_get_pcie_replay_count, NULL);
156
5494d864
AD
157static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev);
158
bd607166
KR
159/**
160 * DOC: product_name
161 *
162 * The amdgpu driver provides a sysfs API for reporting the product name
163 * for the device
164 * The file serial_number is used for this and returns the product name
165 * as returned from the FRU.
166 * NOTE: This is only available for certain server cards
167 */
168
169static ssize_t amdgpu_device_get_product_name(struct device *dev,
170 struct device_attribute *attr, char *buf)
171{
172 struct drm_device *ddev = dev_get_drvdata(dev);
1348969a 173 struct amdgpu_device *adev = drm_to_adev(ddev);
bd607166 174
36000c7a 175 return sysfs_emit(buf, "%s\n", adev->product_name);
bd607166
KR
176}
177
178static DEVICE_ATTR(product_name, S_IRUGO,
179 amdgpu_device_get_product_name, NULL);
180
181/**
182 * DOC: product_number
183 *
184 * The amdgpu driver provides a sysfs API for reporting the part number
185 * for the device
186 * The file serial_number is used for this and returns the part number
187 * as returned from the FRU.
188 * NOTE: This is only available for certain server cards
189 */
190
191static ssize_t amdgpu_device_get_product_number(struct device *dev,
192 struct device_attribute *attr, char *buf)
193{
194 struct drm_device *ddev = dev_get_drvdata(dev);
1348969a 195 struct amdgpu_device *adev = drm_to_adev(ddev);
bd607166 196
36000c7a 197 return sysfs_emit(buf, "%s\n", adev->product_number);
bd607166
KR
198}
199
200static DEVICE_ATTR(product_number, S_IRUGO,
201 amdgpu_device_get_product_number, NULL);
202
203/**
204 * DOC: serial_number
205 *
206 * The amdgpu driver provides a sysfs API for reporting the serial number
207 * for the device
208 * The file serial_number is used for this and returns the serial number
209 * as returned from the FRU.
210 * NOTE: This is only available for certain server cards
211 */
212
213static ssize_t amdgpu_device_get_serial_number(struct device *dev,
214 struct device_attribute *attr, char *buf)
215{
216 struct drm_device *ddev = dev_get_drvdata(dev);
1348969a 217 struct amdgpu_device *adev = drm_to_adev(ddev);
bd607166 218
36000c7a 219 return sysfs_emit(buf, "%s\n", adev->serial);
bd607166
KR
220}
221
222static DEVICE_ATTR(serial_number, S_IRUGO,
223 amdgpu_device_get_serial_number, NULL);
224
fd496ca8 225/**
b98c6299 226 * amdgpu_device_supports_px - Is the device a dGPU with ATPX power control
fd496ca8
AD
227 *
228 * @dev: drm_device pointer
229 *
b98c6299 230 * Returns true if the device is a dGPU with ATPX power control,
fd496ca8
AD
231 * otherwise return false.
232 */
b98c6299 233bool amdgpu_device_supports_px(struct drm_device *dev)
fd496ca8
AD
234{
235 struct amdgpu_device *adev = drm_to_adev(dev);
236
b98c6299 237 if ((adev->flags & AMD_IS_PX) && !amdgpu_is_atpx_hybrid())
fd496ca8
AD
238 return true;
239 return false;
240}
241
e3ecdffa 242/**
0330b848 243 * amdgpu_device_supports_boco - Is the device a dGPU with ACPI power resources
e3ecdffa
AD
244 *
245 * @dev: drm_device pointer
246 *
b98c6299 247 * Returns true if the device is a dGPU with ACPI power control,
e3ecdffa
AD
248 * otherwise return false.
249 */
31af062a 250bool amdgpu_device_supports_boco(struct drm_device *dev)
d38ceaf9 251{
1348969a 252 struct amdgpu_device *adev = drm_to_adev(dev);
d38ceaf9 253
b98c6299
AD
254 if (adev->has_pr3 ||
255 ((adev->flags & AMD_IS_PX) && amdgpu_is_atpx_hybrid()))
d38ceaf9
AD
256 return true;
257 return false;
258}
259
a69cba42
AD
260/**
261 * amdgpu_device_supports_baco - Does the device support BACO
262 *
263 * @dev: drm_device pointer
264 *
265 * Returns true if the device supporte BACO,
266 * otherwise return false.
267 */
268bool amdgpu_device_supports_baco(struct drm_device *dev)
269{
1348969a 270 struct amdgpu_device *adev = drm_to_adev(dev);
a69cba42
AD
271
272 return amdgpu_asic_supports_baco(adev);
273}
274
3fa8f89d
S
275/**
276 * amdgpu_device_supports_smart_shift - Is the device dGPU with
277 * smart shift support
278 *
279 * @dev: drm_device pointer
280 *
281 * Returns true if the device is a dGPU with Smart Shift support,
282 * otherwise returns false.
283 */
284bool amdgpu_device_supports_smart_shift(struct drm_device *dev)
285{
286 return (amdgpu_device_supports_boco(dev) &&
287 amdgpu_acpi_is_power_shift_control_supported());
288}
289
6e3cd2a9
MCC
290/*
291 * VRAM access helper functions
292 */
293
e35e2b11 294/**
048af66b 295 * amdgpu_device_mm_access - access vram by MM_INDEX/MM_DATA
e35e2b11
TY
296 *
297 * @adev: amdgpu_device pointer
298 * @pos: offset of the buffer in vram
299 * @buf: virtual address of the buffer in system memory
300 * @size: read/write size, sizeof(@buf) must > @size
301 * @write: true - write to vram, otherwise - read from vram
302 */
048af66b
KW
303void amdgpu_device_mm_access(struct amdgpu_device *adev, loff_t pos,
304 void *buf, size_t size, bool write)
e35e2b11 305{
e35e2b11 306 unsigned long flags;
048af66b
KW
307 uint32_t hi = ~0, tmp = 0;
308 uint32_t *data = buf;
ce05ac56 309 uint64_t last;
f89f8c6b 310 int idx;
ce05ac56 311
c58a863b 312 if (!drm_dev_enter(adev_to_drm(adev), &idx))
f89f8c6b 313 return;
9d11eb0d 314
048af66b
KW
315 BUG_ON(!IS_ALIGNED(pos, 4) || !IS_ALIGNED(size, 4));
316
317 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
318 for (last = pos + size; pos < last; pos += 4) {
319 tmp = pos >> 31;
320
321 WREG32_NO_KIQ(mmMM_INDEX, ((uint32_t)pos) | 0x80000000);
322 if (tmp != hi) {
323 WREG32_NO_KIQ(mmMM_INDEX_HI, tmp);
324 hi = tmp;
325 }
326 if (write)
327 WREG32_NO_KIQ(mmMM_DATA, *data++);
328 else
329 *data++ = RREG32_NO_KIQ(mmMM_DATA);
330 }
331
332 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
333 drm_dev_exit(idx);
334}
335
336/**
bbe04dec 337 * amdgpu_device_aper_access - access vram by vram aperature
048af66b
KW
338 *
339 * @adev: amdgpu_device pointer
340 * @pos: offset of the buffer in vram
341 * @buf: virtual address of the buffer in system memory
342 * @size: read/write size, sizeof(@buf) must > @size
343 * @write: true - write to vram, otherwise - read from vram
344 *
345 * The return value means how many bytes have been transferred.
346 */
347size_t amdgpu_device_aper_access(struct amdgpu_device *adev, loff_t pos,
348 void *buf, size_t size, bool write)
349{
9d11eb0d 350#ifdef CONFIG_64BIT
048af66b
KW
351 void __iomem *addr;
352 size_t count = 0;
353 uint64_t last;
354
355 if (!adev->mman.aper_base_kaddr)
356 return 0;
357
9d11eb0d
CK
358 last = min(pos + size, adev->gmc.visible_vram_size);
359 if (last > pos) {
048af66b
KW
360 addr = adev->mman.aper_base_kaddr + pos;
361 count = last - pos;
9d11eb0d
CK
362
363 if (write) {
364 memcpy_toio(addr, buf, count);
365 mb();
810085dd 366 amdgpu_device_flush_hdp(adev, NULL);
9d11eb0d 367 } else {
810085dd 368 amdgpu_device_invalidate_hdp(adev, NULL);
9d11eb0d
CK
369 mb();
370 memcpy_fromio(buf, addr, count);
371 }
372
9d11eb0d 373 }
048af66b
KW
374
375 return count;
376#else
377 return 0;
9d11eb0d 378#endif
048af66b 379}
9d11eb0d 380
048af66b
KW
381/**
382 * amdgpu_device_vram_access - read/write a buffer in vram
383 *
384 * @adev: amdgpu_device pointer
385 * @pos: offset of the buffer in vram
386 * @buf: virtual address of the buffer in system memory
387 * @size: read/write size, sizeof(@buf) must > @size
388 * @write: true - write to vram, otherwise - read from vram
389 */
390void amdgpu_device_vram_access(struct amdgpu_device *adev, loff_t pos,
391 void *buf, size_t size, bool write)
392{
393 size_t count;
e35e2b11 394
048af66b
KW
395 /* try to using vram apreature to access vram first */
396 count = amdgpu_device_aper_access(adev, pos, buf, size, write);
397 size -= count;
398 if (size) {
399 /* using MM to access rest vram */
400 pos += count;
401 buf += count;
402 amdgpu_device_mm_access(adev, pos, buf, size, write);
e35e2b11
TY
403 }
404}
405
d38ceaf9 406/*
f7ee1874 407 * register access helper functions.
d38ceaf9 408 */
56b53c0b
DL
409
410/* Check if hw access should be skipped because of hotplug or device error */
411bool amdgpu_device_skip_hw_access(struct amdgpu_device *adev)
412{
7afefb81 413 if (adev->no_hw_access)
56b53c0b
DL
414 return true;
415
416#ifdef CONFIG_LOCKDEP
417 /*
418 * This is a bit complicated to understand, so worth a comment. What we assert
419 * here is that the GPU reset is not running on another thread in parallel.
420 *
421 * For this we trylock the read side of the reset semaphore, if that succeeds
422 * we know that the reset is not running in paralell.
423 *
424 * If the trylock fails we assert that we are either already holding the read
425 * side of the lock or are the reset thread itself and hold the write side of
426 * the lock.
427 */
428 if (in_task()) {
429 if (down_read_trylock(&adev->reset_sem))
430 up_read(&adev->reset_sem);
431 else
432 lockdep_assert_held(&adev->reset_sem);
433 }
434#endif
435 return false;
436}
437
e3ecdffa 438/**
f7ee1874 439 * amdgpu_device_rreg - read a memory mapped IO or indirect register
e3ecdffa
AD
440 *
441 * @adev: amdgpu_device pointer
442 * @reg: dword aligned register offset
443 * @acc_flags: access flags which require special behavior
444 *
445 * Returns the 32 bit value from the offset specified.
446 */
f7ee1874
HZ
447uint32_t amdgpu_device_rreg(struct amdgpu_device *adev,
448 uint32_t reg, uint32_t acc_flags)
d38ceaf9 449{
f4b373f4
TSD
450 uint32_t ret;
451
56b53c0b 452 if (amdgpu_device_skip_hw_access(adev))
bf36b52e
AG
453 return 0;
454
f7ee1874
HZ
455 if ((reg * 4) < adev->rmmio_size) {
456 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) &&
457 amdgpu_sriov_runtime(adev) &&
458 down_read_trylock(&adev->reset_sem)) {
459 ret = amdgpu_kiq_rreg(adev, reg);
460 up_read(&adev->reset_sem);
461 } else {
462 ret = readl(((void __iomem *)adev->rmmio) + (reg * 4));
463 }
464 } else {
465 ret = adev->pcie_rreg(adev, reg * 4);
81202807 466 }
bc992ba5 467
f7ee1874 468 trace_amdgpu_device_rreg(adev->pdev->device, reg, ret);
e78b579d 469
f4b373f4 470 return ret;
d38ceaf9
AD
471}
472
421a2a30
ML
473/*
474 * MMIO register read with bytes helper functions
475 * @offset:bytes offset from MMIO start
476 *
477*/
478
e3ecdffa
AD
479/**
480 * amdgpu_mm_rreg8 - read a memory mapped IO register
481 *
482 * @adev: amdgpu_device pointer
483 * @offset: byte aligned register offset
484 *
485 * Returns the 8 bit value from the offset specified.
486 */
7cbbc745
AG
487uint8_t amdgpu_mm_rreg8(struct amdgpu_device *adev, uint32_t offset)
488{
56b53c0b 489 if (amdgpu_device_skip_hw_access(adev))
bf36b52e
AG
490 return 0;
491
421a2a30
ML
492 if (offset < adev->rmmio_size)
493 return (readb(adev->rmmio + offset));
494 BUG();
495}
496
497/*
498 * MMIO register write with bytes helper functions
499 * @offset:bytes offset from MMIO start
500 * @value: the value want to be written to the register
501 *
502*/
e3ecdffa
AD
503/**
504 * amdgpu_mm_wreg8 - read a memory mapped IO register
505 *
506 * @adev: amdgpu_device pointer
507 * @offset: byte aligned register offset
508 * @value: 8 bit value to write
509 *
510 * Writes the value specified to the offset specified.
511 */
7cbbc745
AG
512void amdgpu_mm_wreg8(struct amdgpu_device *adev, uint32_t offset, uint8_t value)
513{
56b53c0b 514 if (amdgpu_device_skip_hw_access(adev))
bf36b52e
AG
515 return;
516
421a2a30
ML
517 if (offset < adev->rmmio_size)
518 writeb(value, adev->rmmio + offset);
519 else
520 BUG();
521}
522
e3ecdffa 523/**
f7ee1874 524 * amdgpu_device_wreg - write to a memory mapped IO or indirect register
e3ecdffa
AD
525 *
526 * @adev: amdgpu_device pointer
527 * @reg: dword aligned register offset
528 * @v: 32 bit value to write to the register
529 * @acc_flags: access flags which require special behavior
530 *
531 * Writes the value specified to the offset specified.
532 */
f7ee1874
HZ
533void amdgpu_device_wreg(struct amdgpu_device *adev,
534 uint32_t reg, uint32_t v,
535 uint32_t acc_flags)
d38ceaf9 536{
56b53c0b 537 if (amdgpu_device_skip_hw_access(adev))
bf36b52e
AG
538 return;
539
f7ee1874
HZ
540 if ((reg * 4) < adev->rmmio_size) {
541 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) &&
542 amdgpu_sriov_runtime(adev) &&
543 down_read_trylock(&adev->reset_sem)) {
544 amdgpu_kiq_wreg(adev, reg, v);
545 up_read(&adev->reset_sem);
546 } else {
547 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
548 }
549 } else {
550 adev->pcie_wreg(adev, reg * 4, v);
81202807 551 }
bc992ba5 552
f7ee1874 553 trace_amdgpu_device_wreg(adev->pdev->device, reg, v);
2e0cc4d4 554}
d38ceaf9 555
03f2abb0 556/**
4cc9f86f 557 * amdgpu_mm_wreg_mmio_rlc - write register either with direct/indirect mmio or with RLC path if in range
2e0cc4d4 558 *
71579346
RB
559 * @adev: amdgpu_device pointer
560 * @reg: mmio/rlc register
561 * @v: value to write
562 *
563 * this function is invoked only for the debugfs register access
03f2abb0 564 */
f7ee1874
HZ
565void amdgpu_mm_wreg_mmio_rlc(struct amdgpu_device *adev,
566 uint32_t reg, uint32_t v)
2e0cc4d4 567{
56b53c0b 568 if (amdgpu_device_skip_hw_access(adev))
bf36b52e
AG
569 return;
570
2e0cc4d4 571 if (amdgpu_sriov_fullaccess(adev) &&
f7ee1874
HZ
572 adev->gfx.rlc.funcs &&
573 adev->gfx.rlc.funcs->is_rlcg_access_range) {
2e0cc4d4 574 if (adev->gfx.rlc.funcs->is_rlcg_access_range(adev, reg))
1b2dc99e 575 return amdgpu_sriov_wreg(adev, reg, v, 0, 0);
4cc9f86f
TSD
576 } else if ((reg * 4) >= adev->rmmio_size) {
577 adev->pcie_wreg(adev, reg * 4, v);
f7ee1874
HZ
578 } else {
579 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
47ed4e1c 580 }
d38ceaf9
AD
581}
582
d38ceaf9
AD
583/**
584 * amdgpu_mm_rdoorbell - read a doorbell dword
585 *
586 * @adev: amdgpu_device pointer
587 * @index: doorbell index
588 *
589 * Returns the value in the doorbell aperture at the
590 * requested doorbell index (CIK).
591 */
592u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
593{
56b53c0b 594 if (amdgpu_device_skip_hw_access(adev))
bf36b52e
AG
595 return 0;
596
d38ceaf9
AD
597 if (index < adev->doorbell.num_doorbells) {
598 return readl(adev->doorbell.ptr + index);
599 } else {
600 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
601 return 0;
602 }
603}
604
605/**
606 * amdgpu_mm_wdoorbell - write a doorbell dword
607 *
608 * @adev: amdgpu_device pointer
609 * @index: doorbell index
610 * @v: value to write
611 *
612 * Writes @v to the doorbell aperture at the
613 * requested doorbell index (CIK).
614 */
615void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
616{
56b53c0b 617 if (amdgpu_device_skip_hw_access(adev))
bf36b52e
AG
618 return;
619
d38ceaf9
AD
620 if (index < adev->doorbell.num_doorbells) {
621 writel(v, adev->doorbell.ptr + index);
622 } else {
623 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
624 }
625}
626
832be404
KW
627/**
628 * amdgpu_mm_rdoorbell64 - read a doorbell Qword
629 *
630 * @adev: amdgpu_device pointer
631 * @index: doorbell index
632 *
633 * Returns the value in the doorbell aperture at the
634 * requested doorbell index (VEGA10+).
635 */
636u64 amdgpu_mm_rdoorbell64(struct amdgpu_device *adev, u32 index)
637{
56b53c0b 638 if (amdgpu_device_skip_hw_access(adev))
bf36b52e
AG
639 return 0;
640
832be404
KW
641 if (index < adev->doorbell.num_doorbells) {
642 return atomic64_read((atomic64_t *)(adev->doorbell.ptr + index));
643 } else {
644 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
645 return 0;
646 }
647}
648
649/**
650 * amdgpu_mm_wdoorbell64 - write a doorbell Qword
651 *
652 * @adev: amdgpu_device pointer
653 * @index: doorbell index
654 * @v: value to write
655 *
656 * Writes @v to the doorbell aperture at the
657 * requested doorbell index (VEGA10+).
658 */
659void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v)
660{
56b53c0b 661 if (amdgpu_device_skip_hw_access(adev))
bf36b52e
AG
662 return;
663
832be404
KW
664 if (index < adev->doorbell.num_doorbells) {
665 atomic64_set((atomic64_t *)(adev->doorbell.ptr + index), v);
666 } else {
667 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
668 }
669}
670
1bba3683
HZ
671/**
672 * amdgpu_device_indirect_rreg - read an indirect register
673 *
674 * @adev: amdgpu_device pointer
675 * @pcie_index: mmio register offset
676 * @pcie_data: mmio register offset
22f453fb 677 * @reg_addr: indirect register address to read from
1bba3683
HZ
678 *
679 * Returns the value of indirect register @reg_addr
680 */
681u32 amdgpu_device_indirect_rreg(struct amdgpu_device *adev,
682 u32 pcie_index, u32 pcie_data,
683 u32 reg_addr)
684{
685 unsigned long flags;
686 u32 r;
687 void __iomem *pcie_index_offset;
688 void __iomem *pcie_data_offset;
689
690 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
691 pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
692 pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4;
693
694 writel(reg_addr, pcie_index_offset);
695 readl(pcie_index_offset);
696 r = readl(pcie_data_offset);
697 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
698
699 return r;
700}
701
702/**
703 * amdgpu_device_indirect_rreg64 - read a 64bits indirect register
704 *
705 * @adev: amdgpu_device pointer
706 * @pcie_index: mmio register offset
707 * @pcie_data: mmio register offset
22f453fb 708 * @reg_addr: indirect register address to read from
1bba3683
HZ
709 *
710 * Returns the value of indirect register @reg_addr
711 */
712u64 amdgpu_device_indirect_rreg64(struct amdgpu_device *adev,
713 u32 pcie_index, u32 pcie_data,
714 u32 reg_addr)
715{
716 unsigned long flags;
717 u64 r;
718 void __iomem *pcie_index_offset;
719 void __iomem *pcie_data_offset;
720
721 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
722 pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
723 pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4;
724
725 /* read low 32 bits */
726 writel(reg_addr, pcie_index_offset);
727 readl(pcie_index_offset);
728 r = readl(pcie_data_offset);
729 /* read high 32 bits */
730 writel(reg_addr + 4, pcie_index_offset);
731 readl(pcie_index_offset);
732 r |= ((u64)readl(pcie_data_offset) << 32);
733 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
734
735 return r;
736}
737
738/**
739 * amdgpu_device_indirect_wreg - write an indirect register address
740 *
741 * @adev: amdgpu_device pointer
742 * @pcie_index: mmio register offset
743 * @pcie_data: mmio register offset
744 * @reg_addr: indirect register offset
745 * @reg_data: indirect register data
746 *
747 */
748void amdgpu_device_indirect_wreg(struct amdgpu_device *adev,
749 u32 pcie_index, u32 pcie_data,
750 u32 reg_addr, u32 reg_data)
751{
752 unsigned long flags;
753 void __iomem *pcie_index_offset;
754 void __iomem *pcie_data_offset;
755
756 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
757 pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
758 pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4;
759
760 writel(reg_addr, pcie_index_offset);
761 readl(pcie_index_offset);
762 writel(reg_data, pcie_data_offset);
763 readl(pcie_data_offset);
764 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
765}
766
767/**
768 * amdgpu_device_indirect_wreg64 - write a 64bits indirect register address
769 *
770 * @adev: amdgpu_device pointer
771 * @pcie_index: mmio register offset
772 * @pcie_data: mmio register offset
773 * @reg_addr: indirect register offset
774 * @reg_data: indirect register data
775 *
776 */
777void amdgpu_device_indirect_wreg64(struct amdgpu_device *adev,
778 u32 pcie_index, u32 pcie_data,
779 u32 reg_addr, u64 reg_data)
780{
781 unsigned long flags;
782 void __iomem *pcie_index_offset;
783 void __iomem *pcie_data_offset;
784
785 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
786 pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
787 pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4;
788
789 /* write low 32 bits */
790 writel(reg_addr, pcie_index_offset);
791 readl(pcie_index_offset);
792 writel((u32)(reg_data & 0xffffffffULL), pcie_data_offset);
793 readl(pcie_data_offset);
794 /* write high 32 bits */
795 writel(reg_addr + 4, pcie_index_offset);
796 readl(pcie_index_offset);
797 writel((u32)(reg_data >> 32), pcie_data_offset);
798 readl(pcie_data_offset);
799 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
800}
801
d38ceaf9
AD
802/**
803 * amdgpu_invalid_rreg - dummy reg read function
804 *
982a820b 805 * @adev: amdgpu_device pointer
d38ceaf9
AD
806 * @reg: offset of register
807 *
808 * Dummy register read function. Used for register blocks
809 * that certain asics don't have (all asics).
810 * Returns the value in the register.
811 */
812static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
813{
814 DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
815 BUG();
816 return 0;
817}
818
819/**
820 * amdgpu_invalid_wreg - dummy reg write function
821 *
982a820b 822 * @adev: amdgpu_device pointer
d38ceaf9
AD
823 * @reg: offset of register
824 * @v: value to write to the register
825 *
826 * Dummy register read function. Used for register blocks
827 * that certain asics don't have (all asics).
828 */
829static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
830{
831 DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
832 reg, v);
833 BUG();
834}
835
4fa1c6a6
TZ
836/**
837 * amdgpu_invalid_rreg64 - dummy 64 bit reg read function
838 *
982a820b 839 * @adev: amdgpu_device pointer
4fa1c6a6
TZ
840 * @reg: offset of register
841 *
842 * Dummy register read function. Used for register blocks
843 * that certain asics don't have (all asics).
844 * Returns the value in the register.
845 */
846static uint64_t amdgpu_invalid_rreg64(struct amdgpu_device *adev, uint32_t reg)
847{
848 DRM_ERROR("Invalid callback to read 64 bit register 0x%04X\n", reg);
849 BUG();
850 return 0;
851}
852
853/**
854 * amdgpu_invalid_wreg64 - dummy reg write function
855 *
982a820b 856 * @adev: amdgpu_device pointer
4fa1c6a6
TZ
857 * @reg: offset of register
858 * @v: value to write to the register
859 *
860 * Dummy register read function. Used for register blocks
861 * that certain asics don't have (all asics).
862 */
863static void amdgpu_invalid_wreg64(struct amdgpu_device *adev, uint32_t reg, uint64_t v)
864{
865 DRM_ERROR("Invalid callback to write 64 bit register 0x%04X with 0x%08llX\n",
866 reg, v);
867 BUG();
868}
869
d38ceaf9
AD
870/**
871 * amdgpu_block_invalid_rreg - dummy reg read function
872 *
982a820b 873 * @adev: amdgpu_device pointer
d38ceaf9
AD
874 * @block: offset of instance
875 * @reg: offset of register
876 *
877 * Dummy register read function. Used for register blocks
878 * that certain asics don't have (all asics).
879 * Returns the value in the register.
880 */
881static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
882 uint32_t block, uint32_t reg)
883{
884 DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
885 reg, block);
886 BUG();
887 return 0;
888}
889
890/**
891 * amdgpu_block_invalid_wreg - dummy reg write function
892 *
982a820b 893 * @adev: amdgpu_device pointer
d38ceaf9
AD
894 * @block: offset of instance
895 * @reg: offset of register
896 * @v: value to write to the register
897 *
898 * Dummy register read function. Used for register blocks
899 * that certain asics don't have (all asics).
900 */
901static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
902 uint32_t block,
903 uint32_t reg, uint32_t v)
904{
905 DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
906 reg, block, v);
907 BUG();
908}
909
4d2997ab
AD
910/**
911 * amdgpu_device_asic_init - Wrapper for atom asic_init
912 *
982a820b 913 * @adev: amdgpu_device pointer
4d2997ab
AD
914 *
915 * Does any asic specific work and then calls atom asic init.
916 */
917static int amdgpu_device_asic_init(struct amdgpu_device *adev)
918{
919 amdgpu_asic_pre_asic_init(adev);
920
921 return amdgpu_atom_asic_init(adev->mode_info.atom_context);
922}
923
e3ecdffa
AD
924/**
925 * amdgpu_device_vram_scratch_init - allocate the VRAM scratch page
926 *
982a820b 927 * @adev: amdgpu_device pointer
e3ecdffa
AD
928 *
929 * Allocates a scratch page of VRAM for use by various things in the
930 * driver.
931 */
06ec9070 932static int amdgpu_device_vram_scratch_init(struct amdgpu_device *adev)
d38ceaf9 933{
a4a02777
CK
934 return amdgpu_bo_create_kernel(adev, AMDGPU_GPU_PAGE_SIZE,
935 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
936 &adev->vram_scratch.robj,
937 &adev->vram_scratch.gpu_addr,
938 (void **)&adev->vram_scratch.ptr);
d38ceaf9
AD
939}
940
e3ecdffa
AD
941/**
942 * amdgpu_device_vram_scratch_fini - Free the VRAM scratch page
943 *
982a820b 944 * @adev: amdgpu_device pointer
e3ecdffa
AD
945 *
946 * Frees the VRAM scratch page.
947 */
06ec9070 948static void amdgpu_device_vram_scratch_fini(struct amdgpu_device *adev)
d38ceaf9 949{
078af1a3 950 amdgpu_bo_free_kernel(&adev->vram_scratch.robj, NULL, NULL);
d38ceaf9
AD
951}
952
953/**
9c3f2b54 954 * amdgpu_device_program_register_sequence - program an array of registers.
d38ceaf9
AD
955 *
956 * @adev: amdgpu_device pointer
957 * @registers: pointer to the register array
958 * @array_size: size of the register array
959 *
960 * Programs an array or registers with and and or masks.
961 * This is a helper for setting golden registers.
962 */
9c3f2b54
AD
963void amdgpu_device_program_register_sequence(struct amdgpu_device *adev,
964 const u32 *registers,
965 const u32 array_size)
d38ceaf9
AD
966{
967 u32 tmp, reg, and_mask, or_mask;
968 int i;
969
970 if (array_size % 3)
971 return;
972
973 for (i = 0; i < array_size; i +=3) {
974 reg = registers[i + 0];
975 and_mask = registers[i + 1];
976 or_mask = registers[i + 2];
977
978 if (and_mask == 0xffffffff) {
979 tmp = or_mask;
980 } else {
981 tmp = RREG32(reg);
982 tmp &= ~and_mask;
e0d07657
HZ
983 if (adev->family >= AMDGPU_FAMILY_AI)
984 tmp |= (or_mask & and_mask);
985 else
986 tmp |= or_mask;
d38ceaf9
AD
987 }
988 WREG32(reg, tmp);
989 }
990}
991
e3ecdffa
AD
992/**
993 * amdgpu_device_pci_config_reset - reset the GPU
994 *
995 * @adev: amdgpu_device pointer
996 *
997 * Resets the GPU using the pci config reset sequence.
998 * Only applicable to asics prior to vega10.
999 */
8111c387 1000void amdgpu_device_pci_config_reset(struct amdgpu_device *adev)
d38ceaf9
AD
1001{
1002 pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
1003}
1004
af484df8
AD
1005/**
1006 * amdgpu_device_pci_reset - reset the GPU using generic PCI means
1007 *
1008 * @adev: amdgpu_device pointer
1009 *
1010 * Resets the GPU using generic pci reset interfaces (FLR, SBR, etc.).
1011 */
1012int amdgpu_device_pci_reset(struct amdgpu_device *adev)
1013{
1014 return pci_reset_function(adev->pdev);
1015}
1016
d38ceaf9
AD
1017/*
1018 * GPU doorbell aperture helpers function.
1019 */
1020/**
06ec9070 1021 * amdgpu_device_doorbell_init - Init doorbell driver information.
d38ceaf9
AD
1022 *
1023 * @adev: amdgpu_device pointer
1024 *
1025 * Init doorbell driver information (CIK)
1026 * Returns 0 on success, error on failure.
1027 */
06ec9070 1028static int amdgpu_device_doorbell_init(struct amdgpu_device *adev)
d38ceaf9 1029{
6585661d 1030
705e519e
CK
1031 /* No doorbell on SI hardware generation */
1032 if (adev->asic_type < CHIP_BONAIRE) {
1033 adev->doorbell.base = 0;
1034 adev->doorbell.size = 0;
1035 adev->doorbell.num_doorbells = 0;
1036 adev->doorbell.ptr = NULL;
1037 return 0;
1038 }
1039
d6895ad3
CK
1040 if (pci_resource_flags(adev->pdev, 2) & IORESOURCE_UNSET)
1041 return -EINVAL;
1042
22357775
AD
1043 amdgpu_asic_init_doorbell_index(adev);
1044
d38ceaf9
AD
1045 /* doorbell bar mapping */
1046 adev->doorbell.base = pci_resource_start(adev->pdev, 2);
1047 adev->doorbell.size = pci_resource_len(adev->pdev, 2);
1048
edf600da 1049 adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
9564f192 1050 adev->doorbell_index.max_assignment+1);
d38ceaf9
AD
1051 if (adev->doorbell.num_doorbells == 0)
1052 return -EINVAL;
1053
ec3db8a6 1054 /* For Vega, reserve and map two pages on doorbell BAR since SDMA
88dc26e4
OZ
1055 * paging queue doorbell use the second page. The
1056 * AMDGPU_DOORBELL64_MAX_ASSIGNMENT definition assumes all the
1057 * doorbells are in the first page. So with paging queue enabled,
1058 * the max num_doorbells should + 1 page (0x400 in dword)
ec3db8a6
PY
1059 */
1060 if (adev->asic_type >= CHIP_VEGA10)
88dc26e4 1061 adev->doorbell.num_doorbells += 0x400;
ec3db8a6 1062
8972e5d2
CK
1063 adev->doorbell.ptr = ioremap(adev->doorbell.base,
1064 adev->doorbell.num_doorbells *
1065 sizeof(u32));
1066 if (adev->doorbell.ptr == NULL)
d38ceaf9 1067 return -ENOMEM;
d38ceaf9
AD
1068
1069 return 0;
1070}
1071
1072/**
06ec9070 1073 * amdgpu_device_doorbell_fini - Tear down doorbell driver information.
d38ceaf9
AD
1074 *
1075 * @adev: amdgpu_device pointer
1076 *
1077 * Tear down doorbell driver information (CIK)
1078 */
06ec9070 1079static void amdgpu_device_doorbell_fini(struct amdgpu_device *adev)
d38ceaf9
AD
1080{
1081 iounmap(adev->doorbell.ptr);
1082 adev->doorbell.ptr = NULL;
1083}
1084
22cb0164 1085
d38ceaf9
AD
1086
1087/*
06ec9070 1088 * amdgpu_device_wb_*()
455a7bc2 1089 * Writeback is the method by which the GPU updates special pages in memory
ea81a173 1090 * with the status of certain GPU events (fences, ring pointers,etc.).
d38ceaf9
AD
1091 */
1092
1093/**
06ec9070 1094 * amdgpu_device_wb_fini - Disable Writeback and free memory
d38ceaf9
AD
1095 *
1096 * @adev: amdgpu_device pointer
1097 *
1098 * Disables Writeback and frees the Writeback memory (all asics).
1099 * Used at driver shutdown.
1100 */
06ec9070 1101static void amdgpu_device_wb_fini(struct amdgpu_device *adev)
d38ceaf9
AD
1102{
1103 if (adev->wb.wb_obj) {
a76ed485
AD
1104 amdgpu_bo_free_kernel(&adev->wb.wb_obj,
1105 &adev->wb.gpu_addr,
1106 (void **)&adev->wb.wb);
d38ceaf9
AD
1107 adev->wb.wb_obj = NULL;
1108 }
1109}
1110
1111/**
03f2abb0 1112 * amdgpu_device_wb_init - Init Writeback driver info and allocate memory
d38ceaf9
AD
1113 *
1114 * @adev: amdgpu_device pointer
1115 *
455a7bc2 1116 * Initializes writeback and allocates writeback memory (all asics).
d38ceaf9
AD
1117 * Used at driver startup.
1118 * Returns 0 on success or an -error on failure.
1119 */
06ec9070 1120static int amdgpu_device_wb_init(struct amdgpu_device *adev)
d38ceaf9
AD
1121{
1122 int r;
1123
1124 if (adev->wb.wb_obj == NULL) {
97407b63
AD
1125 /* AMDGPU_MAX_WB * sizeof(uint32_t) * 8 = AMDGPU_MAX_WB 256bit slots */
1126 r = amdgpu_bo_create_kernel(adev, AMDGPU_MAX_WB * sizeof(uint32_t) * 8,
a76ed485
AD
1127 PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
1128 &adev->wb.wb_obj, &adev->wb.gpu_addr,
1129 (void **)&adev->wb.wb);
d38ceaf9
AD
1130 if (r) {
1131 dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
1132 return r;
1133 }
d38ceaf9
AD
1134
1135 adev->wb.num_wb = AMDGPU_MAX_WB;
1136 memset(&adev->wb.used, 0, sizeof(adev->wb.used));
1137
1138 /* clear wb memory */
73469585 1139 memset((char *)adev->wb.wb, 0, AMDGPU_MAX_WB * sizeof(uint32_t) * 8);
d38ceaf9
AD
1140 }
1141
1142 return 0;
1143}
1144
1145/**
131b4b36 1146 * amdgpu_device_wb_get - Allocate a wb entry
d38ceaf9
AD
1147 *
1148 * @adev: amdgpu_device pointer
1149 * @wb: wb index
1150 *
1151 * Allocate a wb slot for use by the driver (all asics).
1152 * Returns 0 on success or -EINVAL on failure.
1153 */
131b4b36 1154int amdgpu_device_wb_get(struct amdgpu_device *adev, u32 *wb)
d38ceaf9
AD
1155{
1156 unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
d38ceaf9 1157
97407b63 1158 if (offset < adev->wb.num_wb) {
7014285a 1159 __set_bit(offset, adev->wb.used);
63ae07ca 1160 *wb = offset << 3; /* convert to dw offset */
0915fdbc
ML
1161 return 0;
1162 } else {
1163 return -EINVAL;
1164 }
1165}
1166
d38ceaf9 1167/**
131b4b36 1168 * amdgpu_device_wb_free - Free a wb entry
d38ceaf9
AD
1169 *
1170 * @adev: amdgpu_device pointer
1171 * @wb: wb index
1172 *
1173 * Free a wb slot allocated for use by the driver (all asics)
1174 */
131b4b36 1175void amdgpu_device_wb_free(struct amdgpu_device *adev, u32 wb)
d38ceaf9 1176{
73469585 1177 wb >>= 3;
d38ceaf9 1178 if (wb < adev->wb.num_wb)
73469585 1179 __clear_bit(wb, adev->wb.used);
d38ceaf9
AD
1180}
1181
d6895ad3
CK
1182/**
1183 * amdgpu_device_resize_fb_bar - try to resize FB BAR
1184 *
1185 * @adev: amdgpu_device pointer
1186 *
1187 * Try to resize FB BAR to make all VRAM CPU accessible. We try very hard not
1188 * to fail, but if any of the BARs is not accessible after the size we abort
1189 * driver loading by returning -ENODEV.
1190 */
1191int amdgpu_device_resize_fb_bar(struct amdgpu_device *adev)
1192{
453f617a 1193 int rbar_size = pci_rebar_bytes_to_size(adev->gmc.real_vram_size);
31b8adab
CK
1194 struct pci_bus *root;
1195 struct resource *res;
1196 unsigned i;
d6895ad3
CK
1197 u16 cmd;
1198 int r;
1199
0c03b912 1200 /* Bypass for VF */
1201 if (amdgpu_sriov_vf(adev))
1202 return 0;
1203
b7221f2b
AD
1204 /* skip if the bios has already enabled large BAR */
1205 if (adev->gmc.real_vram_size &&
1206 (pci_resource_len(adev->pdev, 0) >= adev->gmc.real_vram_size))
1207 return 0;
1208
31b8adab
CK
1209 /* Check if the root BUS has 64bit memory resources */
1210 root = adev->pdev->bus;
1211 while (root->parent)
1212 root = root->parent;
1213
1214 pci_bus_for_each_resource(root, res, i) {
0ebb7c54 1215 if (res && res->flags & (IORESOURCE_MEM | IORESOURCE_MEM_64) &&
31b8adab
CK
1216 res->start > 0x100000000ull)
1217 break;
1218 }
1219
1220 /* Trying to resize is pointless without a root hub window above 4GB */
1221 if (!res)
1222 return 0;
1223
453f617a
ND
1224 /* Limit the BAR size to what is available */
1225 rbar_size = min(fls(pci_rebar_get_possible_sizes(adev->pdev, 0)) - 1,
1226 rbar_size);
1227
d6895ad3
CK
1228 /* Disable memory decoding while we change the BAR addresses and size */
1229 pci_read_config_word(adev->pdev, PCI_COMMAND, &cmd);
1230 pci_write_config_word(adev->pdev, PCI_COMMAND,
1231 cmd & ~PCI_COMMAND_MEMORY);
1232
1233 /* Free the VRAM and doorbell BAR, we most likely need to move both. */
06ec9070 1234 amdgpu_device_doorbell_fini(adev);
d6895ad3
CK
1235 if (adev->asic_type >= CHIP_BONAIRE)
1236 pci_release_resource(adev->pdev, 2);
1237
1238 pci_release_resource(adev->pdev, 0);
1239
1240 r = pci_resize_resource(adev->pdev, 0, rbar_size);
1241 if (r == -ENOSPC)
1242 DRM_INFO("Not enough PCI address space for a large BAR.");
1243 else if (r && r != -ENOTSUPP)
1244 DRM_ERROR("Problem resizing BAR0 (%d).", r);
1245
1246 pci_assign_unassigned_bus_resources(adev->pdev->bus);
1247
1248 /* When the doorbell or fb BAR isn't available we have no chance of
1249 * using the device.
1250 */
06ec9070 1251 r = amdgpu_device_doorbell_init(adev);
d6895ad3
CK
1252 if (r || (pci_resource_flags(adev->pdev, 0) & IORESOURCE_UNSET))
1253 return -ENODEV;
1254
1255 pci_write_config_word(adev->pdev, PCI_COMMAND, cmd);
1256
1257 return 0;
1258}
a05502e5 1259
d38ceaf9
AD
1260/*
1261 * GPU helpers function.
1262 */
1263/**
39c640c0 1264 * amdgpu_device_need_post - check if the hw need post or not
d38ceaf9
AD
1265 *
1266 * @adev: amdgpu_device pointer
1267 *
c836fec5
JQ
1268 * Check if the asic has been initialized (all asics) at driver startup
1269 * or post is needed if hw reset is performed.
1270 * Returns true if need or false if not.
d38ceaf9 1271 */
39c640c0 1272bool amdgpu_device_need_post(struct amdgpu_device *adev)
d38ceaf9
AD
1273{
1274 uint32_t reg;
1275
bec86378
ML
1276 if (amdgpu_sriov_vf(adev))
1277 return false;
1278
1279 if (amdgpu_passthrough(adev)) {
1da2c326
ML
1280 /* for FIJI: In whole GPU pass-through virtualization case, after VM reboot
1281 * some old smc fw still need driver do vPost otherwise gpu hang, while
1282 * those smc fw version above 22.15 doesn't have this flaw, so we force
1283 * vpost executed for smc version below 22.15
bec86378
ML
1284 */
1285 if (adev->asic_type == CHIP_FIJI) {
1286 int err;
1287 uint32_t fw_ver;
1288 err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
1289 /* force vPost if error occured */
1290 if (err)
1291 return true;
1292
1293 fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
1da2c326
ML
1294 if (fw_ver < 0x00160e00)
1295 return true;
bec86378 1296 }
bec86378 1297 }
91fe77eb 1298
e3c1b071 1299 /* Don't post if we need to reset whole hive on init */
1300 if (adev->gmc.xgmi.pending_reset)
1301 return false;
1302
91fe77eb 1303 if (adev->has_hw_reset) {
1304 adev->has_hw_reset = false;
1305 return true;
1306 }
1307
1308 /* bios scratch used on CIK+ */
1309 if (adev->asic_type >= CHIP_BONAIRE)
1310 return amdgpu_atombios_scratch_need_asic_init(adev);
1311
1312 /* check MEM_SIZE for older asics */
1313 reg = amdgpu_asic_get_config_memsize(adev);
1314
1315 if ((reg != 0) && (reg != 0xffffffff))
1316 return false;
1317
1318 return true;
bec86378
ML
1319}
1320
d38ceaf9
AD
1321/* if we get transitioned to only one device, take VGA back */
1322/**
06ec9070 1323 * amdgpu_device_vga_set_decode - enable/disable vga decode
d38ceaf9 1324 *
bf44e8ce 1325 * @pdev: PCI device pointer
d38ceaf9
AD
1326 * @state: enable/disable vga decode
1327 *
1328 * Enable/disable vga decode (all asics).
1329 * Returns VGA resource flags.
1330 */
bf44e8ce
CH
1331static unsigned int amdgpu_device_vga_set_decode(struct pci_dev *pdev,
1332 bool state)
d38ceaf9 1333{
bf44e8ce 1334 struct amdgpu_device *adev = drm_to_adev(pci_get_drvdata(pdev));
d38ceaf9
AD
1335 amdgpu_asic_set_vga_state(adev, state);
1336 if (state)
1337 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
1338 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1339 else
1340 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1341}
1342
e3ecdffa
AD
1343/**
1344 * amdgpu_device_check_block_size - validate the vm block size
1345 *
1346 * @adev: amdgpu_device pointer
1347 *
1348 * Validates the vm block size specified via module parameter.
1349 * The vm block size defines number of bits in page table versus page directory,
1350 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
1351 * page table and the remaining bits are in the page directory.
1352 */
06ec9070 1353static void amdgpu_device_check_block_size(struct amdgpu_device *adev)
a1adf8be
CZ
1354{
1355 /* defines number of bits in page table versus page directory,
1356 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
1357 * page table and the remaining bits are in the page directory */
bab4fee7
JZ
1358 if (amdgpu_vm_block_size == -1)
1359 return;
a1adf8be 1360
bab4fee7 1361 if (amdgpu_vm_block_size < 9) {
a1adf8be
CZ
1362 dev_warn(adev->dev, "VM page table size (%d) too small\n",
1363 amdgpu_vm_block_size);
97489129 1364 amdgpu_vm_block_size = -1;
a1adf8be 1365 }
a1adf8be
CZ
1366}
1367
e3ecdffa
AD
1368/**
1369 * amdgpu_device_check_vm_size - validate the vm size
1370 *
1371 * @adev: amdgpu_device pointer
1372 *
1373 * Validates the vm size in GB specified via module parameter.
1374 * The VM size is the size of the GPU virtual memory space in GB.
1375 */
06ec9070 1376static void amdgpu_device_check_vm_size(struct amdgpu_device *adev)
83ca145d 1377{
64dab074
AD
1378 /* no need to check the default value */
1379 if (amdgpu_vm_size == -1)
1380 return;
1381
83ca145d
ZJ
1382 if (amdgpu_vm_size < 1) {
1383 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
1384 amdgpu_vm_size);
f3368128 1385 amdgpu_vm_size = -1;
83ca145d 1386 }
83ca145d
ZJ
1387}
1388
7951e376
RZ
1389static void amdgpu_device_check_smu_prv_buffer_size(struct amdgpu_device *adev)
1390{
1391 struct sysinfo si;
a9d4fe2f 1392 bool is_os_64 = (sizeof(void *) == 8);
7951e376
RZ
1393 uint64_t total_memory;
1394 uint64_t dram_size_seven_GB = 0x1B8000000;
1395 uint64_t dram_size_three_GB = 0xB8000000;
1396
1397 if (amdgpu_smu_memory_pool_size == 0)
1398 return;
1399
1400 if (!is_os_64) {
1401 DRM_WARN("Not 64-bit OS, feature not supported\n");
1402 goto def_value;
1403 }
1404 si_meminfo(&si);
1405 total_memory = (uint64_t)si.totalram * si.mem_unit;
1406
1407 if ((amdgpu_smu_memory_pool_size == 1) ||
1408 (amdgpu_smu_memory_pool_size == 2)) {
1409 if (total_memory < dram_size_three_GB)
1410 goto def_value1;
1411 } else if ((amdgpu_smu_memory_pool_size == 4) ||
1412 (amdgpu_smu_memory_pool_size == 8)) {
1413 if (total_memory < dram_size_seven_GB)
1414 goto def_value1;
1415 } else {
1416 DRM_WARN("Smu memory pool size not supported\n");
1417 goto def_value;
1418 }
1419 adev->pm.smu_prv_buffer_size = amdgpu_smu_memory_pool_size << 28;
1420
1421 return;
1422
1423def_value1:
1424 DRM_WARN("No enough system memory\n");
1425def_value:
1426 adev->pm.smu_prv_buffer_size = 0;
1427}
1428
9f6a7857
HR
1429static int amdgpu_device_init_apu_flags(struct amdgpu_device *adev)
1430{
1431 if (!(adev->flags & AMD_IS_APU) ||
1432 adev->asic_type < CHIP_RAVEN)
1433 return 0;
1434
1435 switch (adev->asic_type) {
1436 case CHIP_RAVEN:
1437 if (adev->pdev->device == 0x15dd)
1438 adev->apu_flags |= AMD_APU_IS_RAVEN;
1439 if (adev->pdev->device == 0x15d8)
1440 adev->apu_flags |= AMD_APU_IS_PICASSO;
1441 break;
1442 case CHIP_RENOIR:
1443 if ((adev->pdev->device == 0x1636) ||
1444 (adev->pdev->device == 0x164c))
1445 adev->apu_flags |= AMD_APU_IS_RENOIR;
1446 else
1447 adev->apu_flags |= AMD_APU_IS_GREEN_SARDINE;
1448 break;
1449 case CHIP_VANGOGH:
1450 adev->apu_flags |= AMD_APU_IS_VANGOGH;
1451 break;
1452 case CHIP_YELLOW_CARP:
1453 break;
d0f56dc2
TZ
1454 case CHIP_CYAN_SKILLFISH:
1455 if (adev->pdev->device == 0x13FE)
1456 adev->apu_flags |= AMD_APU_IS_CYAN_SKILLFISH2;
1457 break;
9f6a7857 1458 default:
4eaf21b7 1459 break;
9f6a7857
HR
1460 }
1461
1462 return 0;
1463}
1464
d38ceaf9 1465/**
06ec9070 1466 * amdgpu_device_check_arguments - validate module params
d38ceaf9
AD
1467 *
1468 * @adev: amdgpu_device pointer
1469 *
1470 * Validates certain module parameters and updates
1471 * the associated values used by the driver (all asics).
1472 */
912dfc84 1473static int amdgpu_device_check_arguments(struct amdgpu_device *adev)
d38ceaf9 1474{
5b011235
CZ
1475 if (amdgpu_sched_jobs < 4) {
1476 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
1477 amdgpu_sched_jobs);
1478 amdgpu_sched_jobs = 4;
76117507 1479 } else if (!is_power_of_2(amdgpu_sched_jobs)){
5b011235
CZ
1480 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
1481 amdgpu_sched_jobs);
1482 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
1483 }
d38ceaf9 1484
83e74db6 1485 if (amdgpu_gart_size != -1 && amdgpu_gart_size < 32) {
f9321cc4
CK
1486 /* gart size must be greater or equal to 32M */
1487 dev_warn(adev->dev, "gart size (%d) too small\n",
1488 amdgpu_gart_size);
83e74db6 1489 amdgpu_gart_size = -1;
d38ceaf9
AD
1490 }
1491
36d38372 1492 if (amdgpu_gtt_size != -1 && amdgpu_gtt_size < 32) {
c4e1a13a 1493 /* gtt size must be greater or equal to 32M */
36d38372
CK
1494 dev_warn(adev->dev, "gtt size (%d) too small\n",
1495 amdgpu_gtt_size);
1496 amdgpu_gtt_size = -1;
d38ceaf9
AD
1497 }
1498
d07f14be
RH
1499 /* valid range is between 4 and 9 inclusive */
1500 if (amdgpu_vm_fragment_size != -1 &&
1501 (amdgpu_vm_fragment_size > 9 || amdgpu_vm_fragment_size < 4)) {
1502 dev_warn(adev->dev, "valid range is between 4 and 9\n");
1503 amdgpu_vm_fragment_size = -1;
1504 }
1505
5d5bd5e3
KW
1506 if (amdgpu_sched_hw_submission < 2) {
1507 dev_warn(adev->dev, "sched hw submission jobs (%d) must be at least 2\n",
1508 amdgpu_sched_hw_submission);
1509 amdgpu_sched_hw_submission = 2;
1510 } else if (!is_power_of_2(amdgpu_sched_hw_submission)) {
1511 dev_warn(adev->dev, "sched hw submission jobs (%d) must be a power of 2\n",
1512 amdgpu_sched_hw_submission);
1513 amdgpu_sched_hw_submission = roundup_pow_of_two(amdgpu_sched_hw_submission);
1514 }
1515
7951e376
RZ
1516 amdgpu_device_check_smu_prv_buffer_size(adev);
1517
06ec9070 1518 amdgpu_device_check_vm_size(adev);
d38ceaf9 1519
06ec9070 1520 amdgpu_device_check_block_size(adev);
6a7f76e7 1521
19aede77 1522 adev->firmware.load_type = amdgpu_ucode_get_load_type(adev, amdgpu_fw_load_type);
912dfc84 1523
c6252390 1524 amdgpu_gmc_tmz_set(adev);
01a8dcec 1525
9b498efa
AD
1526 amdgpu_gmc_noretry_set(adev);
1527
e3c00faa 1528 return 0;
d38ceaf9
AD
1529}
1530
1531/**
1532 * amdgpu_switcheroo_set_state - set switcheroo state
1533 *
1534 * @pdev: pci dev pointer
1694467b 1535 * @state: vga_switcheroo state
d38ceaf9
AD
1536 *
1537 * Callback for the switcheroo driver. Suspends or resumes the
1538 * the asics before or after it is powered up using ACPI methods.
1539 */
8aba21b7
LT
1540static void amdgpu_switcheroo_set_state(struct pci_dev *pdev,
1541 enum vga_switcheroo_state state)
d38ceaf9
AD
1542{
1543 struct drm_device *dev = pci_get_drvdata(pdev);
de185019 1544 int r;
d38ceaf9 1545
b98c6299 1546 if (amdgpu_device_supports_px(dev) && state == VGA_SWITCHEROO_OFF)
d38ceaf9
AD
1547 return;
1548
1549 if (state == VGA_SWITCHEROO_ON) {
dd4fa6c1 1550 pr_info("switched on\n");
d38ceaf9
AD
1551 /* don't suspend or resume card normally */
1552 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1553
8f66090b
TZ
1554 pci_set_power_state(pdev, PCI_D0);
1555 amdgpu_device_load_pci_state(pdev);
1556 r = pci_enable_device(pdev);
de185019
AD
1557 if (r)
1558 DRM_WARN("pci_enable_device failed (%d)\n", r);
1559 amdgpu_device_resume(dev, true);
d38ceaf9 1560
d38ceaf9 1561 dev->switch_power_state = DRM_SWITCH_POWER_ON;
d38ceaf9 1562 } else {
dd4fa6c1 1563 pr_info("switched off\n");
d38ceaf9 1564 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
de185019 1565 amdgpu_device_suspend(dev, true);
8f66090b 1566 amdgpu_device_cache_pci_state(pdev);
de185019 1567 /* Shut down the device */
8f66090b
TZ
1568 pci_disable_device(pdev);
1569 pci_set_power_state(pdev, PCI_D3cold);
d38ceaf9
AD
1570 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1571 }
1572}
1573
1574/**
1575 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1576 *
1577 * @pdev: pci dev pointer
1578 *
1579 * Callback for the switcheroo driver. Check of the switcheroo
1580 * state can be changed.
1581 * Returns true if the state can be changed, false if not.
1582 */
1583static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1584{
1585 struct drm_device *dev = pci_get_drvdata(pdev);
1586
1587 /*
1588 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1589 * locking inversion with the driver load path. And the access here is
1590 * completely racy anyway. So don't bother with locking for now.
1591 */
7e13ad89 1592 return atomic_read(&dev->open_count) == 0;
d38ceaf9
AD
1593}
1594
1595static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1596 .set_gpu_state = amdgpu_switcheroo_set_state,
1597 .reprobe = NULL,
1598 .can_switch = amdgpu_switcheroo_can_switch,
1599};
1600
e3ecdffa
AD
1601/**
1602 * amdgpu_device_ip_set_clockgating_state - set the CG state
1603 *
87e3f136 1604 * @dev: amdgpu_device pointer
e3ecdffa
AD
1605 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1606 * @state: clockgating state (gate or ungate)
1607 *
1608 * Sets the requested clockgating state for all instances of
1609 * the hardware IP specified.
1610 * Returns the error code from the last instance.
1611 */
43fa561f 1612int amdgpu_device_ip_set_clockgating_state(void *dev,
2990a1fc
AD
1613 enum amd_ip_block_type block_type,
1614 enum amd_clockgating_state state)
d38ceaf9 1615{
43fa561f 1616 struct amdgpu_device *adev = dev;
d38ceaf9
AD
1617 int i, r = 0;
1618
1619 for (i = 0; i < adev->num_ip_blocks; i++) {
a1255107 1620 if (!adev->ip_blocks[i].status.valid)
9ecbe7f5 1621 continue;
c722865a
RZ
1622 if (adev->ip_blocks[i].version->type != block_type)
1623 continue;
1624 if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
1625 continue;
1626 r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
1627 (void *)adev, state);
1628 if (r)
1629 DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
1630 adev->ip_blocks[i].version->funcs->name, r);
d38ceaf9
AD
1631 }
1632 return r;
1633}
1634
e3ecdffa
AD
1635/**
1636 * amdgpu_device_ip_set_powergating_state - set the PG state
1637 *
87e3f136 1638 * @dev: amdgpu_device pointer
e3ecdffa
AD
1639 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1640 * @state: powergating state (gate or ungate)
1641 *
1642 * Sets the requested powergating state for all instances of
1643 * the hardware IP specified.
1644 * Returns the error code from the last instance.
1645 */
43fa561f 1646int amdgpu_device_ip_set_powergating_state(void *dev,
2990a1fc
AD
1647 enum amd_ip_block_type block_type,
1648 enum amd_powergating_state state)
d38ceaf9 1649{
43fa561f 1650 struct amdgpu_device *adev = dev;
d38ceaf9
AD
1651 int i, r = 0;
1652
1653 for (i = 0; i < adev->num_ip_blocks; i++) {
a1255107 1654 if (!adev->ip_blocks[i].status.valid)
9ecbe7f5 1655 continue;
c722865a
RZ
1656 if (adev->ip_blocks[i].version->type != block_type)
1657 continue;
1658 if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
1659 continue;
1660 r = adev->ip_blocks[i].version->funcs->set_powergating_state(
1661 (void *)adev, state);
1662 if (r)
1663 DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
1664 adev->ip_blocks[i].version->funcs->name, r);
d38ceaf9
AD
1665 }
1666 return r;
1667}
1668
e3ecdffa
AD
1669/**
1670 * amdgpu_device_ip_get_clockgating_state - get the CG state
1671 *
1672 * @adev: amdgpu_device pointer
1673 * @flags: clockgating feature flags
1674 *
1675 * Walks the list of IPs on the device and updates the clockgating
1676 * flags for each IP.
1677 * Updates @flags with the feature flags for each hardware IP where
1678 * clockgating is enabled.
1679 */
2990a1fc
AD
1680void amdgpu_device_ip_get_clockgating_state(struct amdgpu_device *adev,
1681 u32 *flags)
6cb2d4e4
HR
1682{
1683 int i;
1684
1685 for (i = 0; i < adev->num_ip_blocks; i++) {
1686 if (!adev->ip_blocks[i].status.valid)
1687 continue;
1688 if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
1689 adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
1690 }
1691}
1692
e3ecdffa
AD
1693/**
1694 * amdgpu_device_ip_wait_for_idle - wait for idle
1695 *
1696 * @adev: amdgpu_device pointer
1697 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1698 *
1699 * Waits for the request hardware IP to be idle.
1700 * Returns 0 for success or a negative error code on failure.
1701 */
2990a1fc
AD
1702int amdgpu_device_ip_wait_for_idle(struct amdgpu_device *adev,
1703 enum amd_ip_block_type block_type)
5dbbb60b
AD
1704{
1705 int i, r;
1706
1707 for (i = 0; i < adev->num_ip_blocks; i++) {
a1255107 1708 if (!adev->ip_blocks[i].status.valid)
9ecbe7f5 1709 continue;
a1255107
AD
1710 if (adev->ip_blocks[i].version->type == block_type) {
1711 r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
5dbbb60b
AD
1712 if (r)
1713 return r;
1714 break;
1715 }
1716 }
1717 return 0;
1718
1719}
1720
e3ecdffa
AD
1721/**
1722 * amdgpu_device_ip_is_idle - is the hardware IP idle
1723 *
1724 * @adev: amdgpu_device pointer
1725 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1726 *
1727 * Check if the hardware IP is idle or not.
1728 * Returns true if it the IP is idle, false if not.
1729 */
2990a1fc
AD
1730bool amdgpu_device_ip_is_idle(struct amdgpu_device *adev,
1731 enum amd_ip_block_type block_type)
5dbbb60b
AD
1732{
1733 int i;
1734
1735 for (i = 0; i < adev->num_ip_blocks; i++) {
a1255107 1736 if (!adev->ip_blocks[i].status.valid)
9ecbe7f5 1737 continue;
a1255107
AD
1738 if (adev->ip_blocks[i].version->type == block_type)
1739 return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
5dbbb60b
AD
1740 }
1741 return true;
1742
1743}
1744
e3ecdffa
AD
1745/**
1746 * amdgpu_device_ip_get_ip_block - get a hw IP pointer
1747 *
1748 * @adev: amdgpu_device pointer
87e3f136 1749 * @type: Type of hardware IP (SMU, GFX, UVD, etc.)
e3ecdffa
AD
1750 *
1751 * Returns a pointer to the hardware IP block structure
1752 * if it exists for the asic, otherwise NULL.
1753 */
2990a1fc
AD
1754struct amdgpu_ip_block *
1755amdgpu_device_ip_get_ip_block(struct amdgpu_device *adev,
1756 enum amd_ip_block_type type)
d38ceaf9
AD
1757{
1758 int i;
1759
1760 for (i = 0; i < adev->num_ip_blocks; i++)
a1255107 1761 if (adev->ip_blocks[i].version->type == type)
d38ceaf9
AD
1762 return &adev->ip_blocks[i];
1763
1764 return NULL;
1765}
1766
1767/**
2990a1fc 1768 * amdgpu_device_ip_block_version_cmp
d38ceaf9
AD
1769 *
1770 * @adev: amdgpu_device pointer
5fc3aeeb 1771 * @type: enum amd_ip_block_type
d38ceaf9
AD
1772 * @major: major version
1773 * @minor: minor version
1774 *
1775 * return 0 if equal or greater
1776 * return 1 if smaller or the ip_block doesn't exist
1777 */
2990a1fc
AD
1778int amdgpu_device_ip_block_version_cmp(struct amdgpu_device *adev,
1779 enum amd_ip_block_type type,
1780 u32 major, u32 minor)
d38ceaf9 1781{
2990a1fc 1782 struct amdgpu_ip_block *ip_block = amdgpu_device_ip_get_ip_block(adev, type);
d38ceaf9 1783
a1255107
AD
1784 if (ip_block && ((ip_block->version->major > major) ||
1785 ((ip_block->version->major == major) &&
1786 (ip_block->version->minor >= minor))))
d38ceaf9
AD
1787 return 0;
1788
1789 return 1;
1790}
1791
a1255107 1792/**
2990a1fc 1793 * amdgpu_device_ip_block_add
a1255107
AD
1794 *
1795 * @adev: amdgpu_device pointer
1796 * @ip_block_version: pointer to the IP to add
1797 *
1798 * Adds the IP block driver information to the collection of IPs
1799 * on the asic.
1800 */
2990a1fc
AD
1801int amdgpu_device_ip_block_add(struct amdgpu_device *adev,
1802 const struct amdgpu_ip_block_version *ip_block_version)
a1255107
AD
1803{
1804 if (!ip_block_version)
1805 return -EINVAL;
1806
7bd939d0
LG
1807 switch (ip_block_version->type) {
1808 case AMD_IP_BLOCK_TYPE_VCN:
1809 if (adev->harvest_ip_mask & AMD_HARVEST_IP_VCN_MASK)
1810 return 0;
1811 break;
1812 case AMD_IP_BLOCK_TYPE_JPEG:
1813 if (adev->harvest_ip_mask & AMD_HARVEST_IP_JPEG_MASK)
1814 return 0;
1815 break;
1816 default:
1817 break;
1818 }
1819
e966a725 1820 DRM_INFO("add ip block number %d <%s>\n", adev->num_ip_blocks,
a0bae357
HR
1821 ip_block_version->funcs->name);
1822
a1255107
AD
1823 adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
1824
1825 return 0;
1826}
1827
e3ecdffa
AD
1828/**
1829 * amdgpu_device_enable_virtual_display - enable virtual display feature
1830 *
1831 * @adev: amdgpu_device pointer
1832 *
1833 * Enabled the virtual display feature if the user has enabled it via
1834 * the module parameter virtual_display. This feature provides a virtual
1835 * display hardware on headless boards or in virtualized environments.
1836 * This function parses and validates the configuration string specified by
1837 * the user and configues the virtual display configuration (number of
1838 * virtual connectors, crtcs, etc.) specified.
1839 */
483ef985 1840static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
9accf2fd
ED
1841{
1842 adev->enable_virtual_display = false;
1843
1844 if (amdgpu_virtual_display) {
8f66090b 1845 const char *pci_address_name = pci_name(adev->pdev);
0f66356d 1846 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
9accf2fd
ED
1847
1848 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1849 pciaddstr_tmp = pciaddstr;
0f66356d
ED
1850 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1851 pciaddname = strsep(&pciaddname_tmp, ",");
967de2a9
YT
1852 if (!strcmp("all", pciaddname)
1853 || !strcmp(pci_address_name, pciaddname)) {
0f66356d
ED
1854 long num_crtc;
1855 int res = -1;
1856
9accf2fd 1857 adev->enable_virtual_display = true;
0f66356d
ED
1858
1859 if (pciaddname_tmp)
1860 res = kstrtol(pciaddname_tmp, 10,
1861 &num_crtc);
1862
1863 if (!res) {
1864 if (num_crtc < 1)
1865 num_crtc = 1;
1866 if (num_crtc > 6)
1867 num_crtc = 6;
1868 adev->mode_info.num_crtc = num_crtc;
1869 } else {
1870 adev->mode_info.num_crtc = 1;
1871 }
9accf2fd
ED
1872 break;
1873 }
1874 }
1875
0f66356d
ED
1876 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
1877 amdgpu_virtual_display, pci_address_name,
1878 adev->enable_virtual_display, adev->mode_info.num_crtc);
9accf2fd
ED
1879
1880 kfree(pciaddstr);
1881 }
1882}
1883
e3ecdffa
AD
1884/**
1885 * amdgpu_device_parse_gpu_info_fw - parse gpu info firmware
1886 *
1887 * @adev: amdgpu_device pointer
1888 *
1889 * Parses the asic configuration parameters specified in the gpu info
1890 * firmware and makes them availale to the driver for use in configuring
1891 * the asic.
1892 * Returns 0 on success, -EINVAL on failure.
1893 */
e2a75f88
AD
1894static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev)
1895{
e2a75f88 1896 const char *chip_name;
c0a43457 1897 char fw_name[40];
e2a75f88
AD
1898 int err;
1899 const struct gpu_info_firmware_header_v1_0 *hdr;
1900
ab4fe3e1
HR
1901 adev->firmware.gpu_info_fw = NULL;
1902
72de33f8 1903 if (adev->mman.discovery_bin) {
258620d0 1904 amdgpu_discovery_get_gfx_info(adev);
cc375d8c
TY
1905
1906 /*
1907 * FIXME: The bounding box is still needed by Navi12, so
1908 * temporarily read it from gpu_info firmware. Should be droped
1909 * when DAL no longer needs it.
1910 */
1911 if (adev->asic_type != CHIP_NAVI12)
1912 return 0;
258620d0
AD
1913 }
1914
e2a75f88 1915 switch (adev->asic_type) {
e2a75f88
AD
1916#ifdef CONFIG_DRM_AMDGPU_SI
1917 case CHIP_VERDE:
1918 case CHIP_TAHITI:
1919 case CHIP_PITCAIRN:
1920 case CHIP_OLAND:
1921 case CHIP_HAINAN:
1922#endif
1923#ifdef CONFIG_DRM_AMDGPU_CIK
1924 case CHIP_BONAIRE:
1925 case CHIP_HAWAII:
1926 case CHIP_KAVERI:
1927 case CHIP_KABINI:
1928 case CHIP_MULLINS:
1929#endif
da87c30b
AD
1930 case CHIP_TOPAZ:
1931 case CHIP_TONGA:
1932 case CHIP_FIJI:
1933 case CHIP_POLARIS10:
1934 case CHIP_POLARIS11:
1935 case CHIP_POLARIS12:
1936 case CHIP_VEGAM:
1937 case CHIP_CARRIZO:
1938 case CHIP_STONEY:
27c0bc71 1939 case CHIP_VEGA20:
44b3253a 1940 case CHIP_ALDEBARAN:
84d244a3
JC
1941 case CHIP_SIENNA_CICHLID:
1942 case CHIP_NAVY_FLOUNDER:
eac88a5f 1943 case CHIP_DIMGREY_CAVEFISH:
0e5f4b09 1944 case CHIP_BEIGE_GOBY:
e2a75f88
AD
1945 default:
1946 return 0;
1947 case CHIP_VEGA10:
1948 chip_name = "vega10";
1949 break;
3f76dced
AD
1950 case CHIP_VEGA12:
1951 chip_name = "vega12";
1952 break;
2d2e5e7e 1953 case CHIP_RAVEN:
54f78a76 1954 if (adev->apu_flags & AMD_APU_IS_RAVEN2)
54c4d17e 1955 chip_name = "raven2";
54f78a76 1956 else if (adev->apu_flags & AMD_APU_IS_PICASSO)
741deade 1957 chip_name = "picasso";
54c4d17e
FX
1958 else
1959 chip_name = "raven";
2d2e5e7e 1960 break;
65e60f6e
LM
1961 case CHIP_ARCTURUS:
1962 chip_name = "arcturus";
1963 break;
b51a26a0 1964 case CHIP_RENOIR:
2e62f0b5
PL
1965 if (adev->apu_flags & AMD_APU_IS_RENOIR)
1966 chip_name = "renoir";
1967 else
1968 chip_name = "green_sardine";
b51a26a0 1969 break;
23c6268e
HR
1970 case CHIP_NAVI10:
1971 chip_name = "navi10";
1972 break;
ed42cfe1
XY
1973 case CHIP_NAVI14:
1974 chip_name = "navi14";
1975 break;
42b325e5
XY
1976 case CHIP_NAVI12:
1977 chip_name = "navi12";
1978 break;
4e52a9f8
HR
1979 case CHIP_VANGOGH:
1980 chip_name = "vangogh";
1981 break;
8bf84f60
AL
1982 case CHIP_YELLOW_CARP:
1983 chip_name = "yellow_carp";
1984 break;
e2a75f88
AD
1985 }
1986
1987 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name);
ab4fe3e1 1988 err = request_firmware(&adev->firmware.gpu_info_fw, fw_name, adev->dev);
e2a75f88
AD
1989 if (err) {
1990 dev_err(adev->dev,
1991 "Failed to load gpu_info firmware \"%s\"\n",
1992 fw_name);
1993 goto out;
1994 }
ab4fe3e1 1995 err = amdgpu_ucode_validate(adev->firmware.gpu_info_fw);
e2a75f88
AD
1996 if (err) {
1997 dev_err(adev->dev,
1998 "Failed to validate gpu_info firmware \"%s\"\n",
1999 fw_name);
2000 goto out;
2001 }
2002
ab4fe3e1 2003 hdr = (const struct gpu_info_firmware_header_v1_0 *)adev->firmware.gpu_info_fw->data;
e2a75f88
AD
2004 amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
2005
2006 switch (hdr->version_major) {
2007 case 1:
2008 {
2009 const struct gpu_info_firmware_v1_0 *gpu_info_fw =
ab4fe3e1 2010 (const struct gpu_info_firmware_v1_0 *)(adev->firmware.gpu_info_fw->data +
e2a75f88
AD
2011 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
2012
cc375d8c
TY
2013 /*
2014 * Should be droped when DAL no longer needs it.
2015 */
2016 if (adev->asic_type == CHIP_NAVI12)
ec51d3fa
XY
2017 goto parse_soc_bounding_box;
2018
b5ab16bf
AD
2019 adev->gfx.config.max_shader_engines = le32_to_cpu(gpu_info_fw->gc_num_se);
2020 adev->gfx.config.max_cu_per_sh = le32_to_cpu(gpu_info_fw->gc_num_cu_per_sh);
2021 adev->gfx.config.max_sh_per_se = le32_to_cpu(gpu_info_fw->gc_num_sh_per_se);
2022 adev->gfx.config.max_backends_per_se = le32_to_cpu(gpu_info_fw->gc_num_rb_per_se);
e2a75f88 2023 adev->gfx.config.max_texture_channel_caches =
b5ab16bf
AD
2024 le32_to_cpu(gpu_info_fw->gc_num_tccs);
2025 adev->gfx.config.max_gprs = le32_to_cpu(gpu_info_fw->gc_num_gprs);
2026 adev->gfx.config.max_gs_threads = le32_to_cpu(gpu_info_fw->gc_num_max_gs_thds);
2027 adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gpu_info_fw->gc_gs_table_depth);
2028 adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gpu_info_fw->gc_gsprim_buff_depth);
e2a75f88 2029 adev->gfx.config.double_offchip_lds_buf =
b5ab16bf
AD
2030 le32_to_cpu(gpu_info_fw->gc_double_offchip_lds_buffer);
2031 adev->gfx.cu_info.wave_front_size = le32_to_cpu(gpu_info_fw->gc_wave_size);
51fd0370
HZ
2032 adev->gfx.cu_info.max_waves_per_simd =
2033 le32_to_cpu(gpu_info_fw->gc_max_waves_per_simd);
2034 adev->gfx.cu_info.max_scratch_slots_per_cu =
2035 le32_to_cpu(gpu_info_fw->gc_max_scratch_slots_per_cu);
2036 adev->gfx.cu_info.lds_size = le32_to_cpu(gpu_info_fw->gc_lds_size);
48321c3d 2037 if (hdr->version_minor >= 1) {
35c2e910
HZ
2038 const struct gpu_info_firmware_v1_1 *gpu_info_fw =
2039 (const struct gpu_info_firmware_v1_1 *)(adev->firmware.gpu_info_fw->data +
2040 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
2041 adev->gfx.config.num_sc_per_sh =
2042 le32_to_cpu(gpu_info_fw->num_sc_per_sh);
2043 adev->gfx.config.num_packer_per_sc =
2044 le32_to_cpu(gpu_info_fw->num_packer_per_sc);
2045 }
ec51d3fa
XY
2046
2047parse_soc_bounding_box:
ec51d3fa
XY
2048 /*
2049 * soc bounding box info is not integrated in disocovery table,
258620d0 2050 * we always need to parse it from gpu info firmware if needed.
ec51d3fa 2051 */
48321c3d
HW
2052 if (hdr->version_minor == 2) {
2053 const struct gpu_info_firmware_v1_2 *gpu_info_fw =
2054 (const struct gpu_info_firmware_v1_2 *)(adev->firmware.gpu_info_fw->data +
2055 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
2056 adev->dm.soc_bounding_box = &gpu_info_fw->soc_bounding_box;
2057 }
e2a75f88
AD
2058 break;
2059 }
2060 default:
2061 dev_err(adev->dev,
2062 "Unsupported gpu_info table %d\n", hdr->header.ucode_version);
2063 err = -EINVAL;
2064 goto out;
2065 }
2066out:
e2a75f88
AD
2067 return err;
2068}
2069
e3ecdffa
AD
2070/**
2071 * amdgpu_device_ip_early_init - run early init for hardware IPs
2072 *
2073 * @adev: amdgpu_device pointer
2074 *
2075 * Early initialization pass for hardware IPs. The hardware IPs that make
2076 * up each asic are discovered each IP's early_init callback is run. This
2077 * is the first stage in initializing the asic.
2078 * Returns 0 on success, negative error code on failure.
2079 */
06ec9070 2080static int amdgpu_device_ip_early_init(struct amdgpu_device *adev)
d38ceaf9 2081{
901e2be2
AD
2082 struct drm_device *dev = adev_to_drm(adev);
2083 struct pci_dev *parent;
aaa36a97 2084 int i, r;
d38ceaf9 2085
483ef985 2086 amdgpu_device_enable_virtual_display(adev);
a6be7570 2087
00a979f3 2088 if (amdgpu_sriov_vf(adev)) {
00a979f3 2089 r = amdgpu_virt_request_full_gpu(adev, true);
aaa36a97
AD
2090 if (r)
2091 return r;
00a979f3
WS
2092 }
2093
d38ceaf9 2094 switch (adev->asic_type) {
33f34802
KW
2095#ifdef CONFIG_DRM_AMDGPU_SI
2096 case CHIP_VERDE:
2097 case CHIP_TAHITI:
2098 case CHIP_PITCAIRN:
2099 case CHIP_OLAND:
2100 case CHIP_HAINAN:
295d0daf 2101 adev->family = AMDGPU_FAMILY_SI;
33f34802
KW
2102 r = si_set_ip_blocks(adev);
2103 if (r)
2104 return r;
2105 break;
2106#endif
a2e73f56
AD
2107#ifdef CONFIG_DRM_AMDGPU_CIK
2108 case CHIP_BONAIRE:
2109 case CHIP_HAWAII:
2110 case CHIP_KAVERI:
2111 case CHIP_KABINI:
2112 case CHIP_MULLINS:
e1ad2d53 2113 if (adev->flags & AMD_IS_APU)
a2e73f56 2114 adev->family = AMDGPU_FAMILY_KV;
e1ad2d53
AD
2115 else
2116 adev->family = AMDGPU_FAMILY_CI;
a2e73f56
AD
2117
2118 r = cik_set_ip_blocks(adev);
2119 if (r)
2120 return r;
2121 break;
2122#endif
da87c30b
AD
2123 case CHIP_TOPAZ:
2124 case CHIP_TONGA:
2125 case CHIP_FIJI:
2126 case CHIP_POLARIS10:
2127 case CHIP_POLARIS11:
2128 case CHIP_POLARIS12:
2129 case CHIP_VEGAM:
2130 case CHIP_CARRIZO:
2131 case CHIP_STONEY:
2132 if (adev->flags & AMD_IS_APU)
2133 adev->family = AMDGPU_FAMILY_CZ;
2134 else
2135 adev->family = AMDGPU_FAMILY_VI;
2136
2137 r = vi_set_ip_blocks(adev);
2138 if (r)
2139 return r;
2140 break;
d38ceaf9 2141 default:
63352b7f
AD
2142 r = amdgpu_discovery_set_ip_blocks(adev);
2143 if (r)
2144 return r;
2145 break;
d38ceaf9
AD
2146 }
2147
901e2be2
AD
2148 if (amdgpu_has_atpx() &&
2149 (amdgpu_is_atpx_hybrid() ||
2150 amdgpu_has_atpx_dgpu_power_cntl()) &&
2151 ((adev->flags & AMD_IS_APU) == 0) &&
2152 !pci_is_thunderbolt_attached(to_pci_dev(dev->dev)))
2153 adev->flags |= AMD_IS_PX;
2154
2155 parent = pci_upstream_bridge(adev->pdev);
2156 adev->has_pr3 = parent ? pci_pr3_present(parent) : false;
2157
1884734a 2158 amdgpu_amdkfd_device_probe(adev);
2159
3b94fb10 2160 adev->pm.pp_feature = amdgpu_pp_feature_mask;
a35ad98b 2161 if (amdgpu_sriov_vf(adev) || sched_policy == KFD_SCHED_POLICY_NO_HWS)
00544006 2162 adev->pm.pp_feature &= ~PP_GFXOFF_MASK;
4215a119
HC
2163 if (amdgpu_sriov_vf(adev) && adev->asic_type == CHIP_SIENNA_CICHLID)
2164 adev->pm.pp_feature &= ~PP_OVERDRIVE_MASK;
00f54b97 2165
d38ceaf9
AD
2166 for (i = 0; i < adev->num_ip_blocks; i++) {
2167 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
ed8cf00c
HR
2168 DRM_ERROR("disabled ip block: %d <%s>\n",
2169 i, adev->ip_blocks[i].version->funcs->name);
a1255107 2170 adev->ip_blocks[i].status.valid = false;
d38ceaf9 2171 } else {
a1255107
AD
2172 if (adev->ip_blocks[i].version->funcs->early_init) {
2173 r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
2c1a2784 2174 if (r == -ENOENT) {
a1255107 2175 adev->ip_blocks[i].status.valid = false;
2c1a2784 2176 } else if (r) {
a1255107
AD
2177 DRM_ERROR("early_init of IP block <%s> failed %d\n",
2178 adev->ip_blocks[i].version->funcs->name, r);
d38ceaf9 2179 return r;
2c1a2784 2180 } else {
a1255107 2181 adev->ip_blocks[i].status.valid = true;
2c1a2784 2182 }
974e6b64 2183 } else {
a1255107 2184 adev->ip_blocks[i].status.valid = true;
d38ceaf9 2185 }
d38ceaf9 2186 }
21a249ca
AD
2187 /* get the vbios after the asic_funcs are set up */
2188 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON) {
6e29c227
AD
2189 r = amdgpu_device_parse_gpu_info_fw(adev);
2190 if (r)
2191 return r;
2192
21a249ca
AD
2193 /* Read BIOS */
2194 if (!amdgpu_get_bios(adev))
2195 return -EINVAL;
2196
2197 r = amdgpu_atombios_init(adev);
2198 if (r) {
2199 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
2200 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_INIT_FAIL, 0, 0);
2201 return r;
2202 }
77eabc6f
PJZ
2203
2204 /*get pf2vf msg info at it's earliest time*/
2205 if (amdgpu_sriov_vf(adev))
2206 amdgpu_virt_init_data_exchange(adev);
2207
21a249ca 2208 }
d38ceaf9
AD
2209 }
2210
395d1fb9
NH
2211 adev->cg_flags &= amdgpu_cg_mask;
2212 adev->pg_flags &= amdgpu_pg_mask;
2213
d38ceaf9
AD
2214 return 0;
2215}
2216
0a4f2520
RZ
2217static int amdgpu_device_ip_hw_init_phase1(struct amdgpu_device *adev)
2218{
2219 int i, r;
2220
2221 for (i = 0; i < adev->num_ip_blocks; i++) {
2222 if (!adev->ip_blocks[i].status.sw)
2223 continue;
2224 if (adev->ip_blocks[i].status.hw)
2225 continue;
2226 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
2d11fd3f 2227 (amdgpu_sriov_vf(adev) && (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP)) ||
0a4f2520
RZ
2228 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH) {
2229 r = adev->ip_blocks[i].version->funcs->hw_init(adev);
2230 if (r) {
2231 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
2232 adev->ip_blocks[i].version->funcs->name, r);
2233 return r;
2234 }
2235 adev->ip_blocks[i].status.hw = true;
2236 }
2237 }
2238
2239 return 0;
2240}
2241
2242static int amdgpu_device_ip_hw_init_phase2(struct amdgpu_device *adev)
2243{
2244 int i, r;
2245
2246 for (i = 0; i < adev->num_ip_blocks; i++) {
2247 if (!adev->ip_blocks[i].status.sw)
2248 continue;
2249 if (adev->ip_blocks[i].status.hw)
2250 continue;
2251 r = adev->ip_blocks[i].version->funcs->hw_init(adev);
2252 if (r) {
2253 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
2254 adev->ip_blocks[i].version->funcs->name, r);
2255 return r;
2256 }
2257 adev->ip_blocks[i].status.hw = true;
2258 }
2259
2260 return 0;
2261}
2262
7a3e0bb2
RZ
2263static int amdgpu_device_fw_loading(struct amdgpu_device *adev)
2264{
2265 int r = 0;
2266 int i;
80f41f84 2267 uint32_t smu_version;
7a3e0bb2
RZ
2268
2269 if (adev->asic_type >= CHIP_VEGA10) {
2270 for (i = 0; i < adev->num_ip_blocks; i++) {
482f0e53
ML
2271 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_PSP)
2272 continue;
2273
e3c1b071 2274 if (!adev->ip_blocks[i].status.sw)
2275 continue;
2276
482f0e53
ML
2277 /* no need to do the fw loading again if already done*/
2278 if (adev->ip_blocks[i].status.hw == true)
2279 break;
2280
53b3f8f4 2281 if (amdgpu_in_reset(adev) || adev->in_suspend) {
482f0e53
ML
2282 r = adev->ip_blocks[i].version->funcs->resume(adev);
2283 if (r) {
2284 DRM_ERROR("resume of IP block <%s> failed %d\n",
7a3e0bb2 2285 adev->ip_blocks[i].version->funcs->name, r);
482f0e53
ML
2286 return r;
2287 }
2288 } else {
2289 r = adev->ip_blocks[i].version->funcs->hw_init(adev);
2290 if (r) {
2291 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
2292 adev->ip_blocks[i].version->funcs->name, r);
2293 return r;
7a3e0bb2 2294 }
7a3e0bb2 2295 }
482f0e53
ML
2296
2297 adev->ip_blocks[i].status.hw = true;
2298 break;
7a3e0bb2
RZ
2299 }
2300 }
482f0e53 2301
8973d9ec
ED
2302 if (!amdgpu_sriov_vf(adev) || adev->asic_type == CHIP_TONGA)
2303 r = amdgpu_pm_load_smu_firmware(adev, &smu_version);
7a3e0bb2 2304
80f41f84 2305 return r;
7a3e0bb2
RZ
2306}
2307
e3ecdffa
AD
2308/**
2309 * amdgpu_device_ip_init - run init for hardware IPs
2310 *
2311 * @adev: amdgpu_device pointer
2312 *
2313 * Main initialization pass for hardware IPs. The list of all the hardware
2314 * IPs that make up the asic is walked and the sw_init and hw_init callbacks
2315 * are run. sw_init initializes the software state associated with each IP
2316 * and hw_init initializes the hardware associated with each IP.
2317 * Returns 0 on success, negative error code on failure.
2318 */
06ec9070 2319static int amdgpu_device_ip_init(struct amdgpu_device *adev)
d38ceaf9
AD
2320{
2321 int i, r;
2322
c030f2e4 2323 r = amdgpu_ras_init(adev);
2324 if (r)
2325 return r;
2326
d38ceaf9 2327 for (i = 0; i < adev->num_ip_blocks; i++) {
a1255107 2328 if (!adev->ip_blocks[i].status.valid)
d38ceaf9 2329 continue;
a1255107 2330 r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
2c1a2784 2331 if (r) {
a1255107
AD
2332 DRM_ERROR("sw_init of IP block <%s> failed %d\n",
2333 adev->ip_blocks[i].version->funcs->name, r);
72d3f592 2334 goto init_failed;
2c1a2784 2335 }
a1255107 2336 adev->ip_blocks[i].status.sw = true;
bfca0289 2337
d38ceaf9 2338 /* need to do gmc hw init early so we can allocate gpu mem */
a1255107 2339 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
892deb48
VS
2340 /* Try to reserve bad pages early */
2341 if (amdgpu_sriov_vf(adev))
2342 amdgpu_virt_exchange_data(adev);
2343
06ec9070 2344 r = amdgpu_device_vram_scratch_init(adev);
2c1a2784
AD
2345 if (r) {
2346 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
72d3f592 2347 goto init_failed;
2c1a2784 2348 }
a1255107 2349 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
2c1a2784
AD
2350 if (r) {
2351 DRM_ERROR("hw_init %d failed %d\n", i, r);
72d3f592 2352 goto init_failed;
2c1a2784 2353 }
06ec9070 2354 r = amdgpu_device_wb_init(adev);
2c1a2784 2355 if (r) {
06ec9070 2356 DRM_ERROR("amdgpu_device_wb_init failed %d\n", r);
72d3f592 2357 goto init_failed;
2c1a2784 2358 }
a1255107 2359 adev->ip_blocks[i].status.hw = true;
2493664f
ML
2360
2361 /* right after GMC hw init, we create CSA */
f92d5c61 2362 if (amdgpu_mcbp || amdgpu_sriov_vf(adev)) {
1e256e27
RZ
2363 r = amdgpu_allocate_static_csa(adev, &adev->virt.csa_obj,
2364 AMDGPU_GEM_DOMAIN_VRAM,
2365 AMDGPU_CSA_SIZE);
2493664f
ML
2366 if (r) {
2367 DRM_ERROR("allocate CSA failed %d\n", r);
72d3f592 2368 goto init_failed;
2493664f
ML
2369 }
2370 }
d38ceaf9
AD
2371 }
2372 }
2373
c9ffa427 2374 if (amdgpu_sriov_vf(adev))
22c16d25 2375 amdgpu_virt_init_data_exchange(adev);
c9ffa427 2376
533aed27
AG
2377 r = amdgpu_ib_pool_init(adev);
2378 if (r) {
2379 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
2380 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r);
2381 goto init_failed;
2382 }
2383
c8963ea4
RZ
2384 r = amdgpu_ucode_create_bo(adev); /* create ucode bo when sw_init complete*/
2385 if (r)
72d3f592 2386 goto init_failed;
0a4f2520
RZ
2387
2388 r = amdgpu_device_ip_hw_init_phase1(adev);
2389 if (r)
72d3f592 2390 goto init_failed;
0a4f2520 2391
7a3e0bb2
RZ
2392 r = amdgpu_device_fw_loading(adev);
2393 if (r)
72d3f592 2394 goto init_failed;
7a3e0bb2 2395
0a4f2520
RZ
2396 r = amdgpu_device_ip_hw_init_phase2(adev);
2397 if (r)
72d3f592 2398 goto init_failed;
d38ceaf9 2399
121a2bc6
AG
2400 /*
2401 * retired pages will be loaded from eeprom and reserved here,
2402 * it should be called after amdgpu_device_ip_hw_init_phase2 since
2403 * for some ASICs the RAS EEPROM code relies on SMU fully functioning
2404 * for I2C communication which only true at this point.
b82e65a9
GC
2405 *
2406 * amdgpu_ras_recovery_init may fail, but the upper only cares the
2407 * failure from bad gpu situation and stop amdgpu init process
2408 * accordingly. For other failed cases, it will still release all
2409 * the resource and print error message, rather than returning one
2410 * negative value to upper level.
121a2bc6
AG
2411 *
2412 * Note: theoretically, this should be called before all vram allocations
2413 * to protect retired page from abusing
2414 */
b82e65a9
GC
2415 r = amdgpu_ras_recovery_init(adev);
2416 if (r)
2417 goto init_failed;
121a2bc6 2418
3e2e2ab5
HZ
2419 if (adev->gmc.xgmi.num_physical_nodes > 1)
2420 amdgpu_xgmi_add_device(adev);
e3c1b071 2421
2422 /* Don't init kfd if whole hive need to be reset during init */
2423 if (!adev->gmc.xgmi.pending_reset)
2424 amdgpu_amdkfd_device_init(adev);
c6332b97 2425
bd607166
KR
2426 amdgpu_fru_get_product_info(adev);
2427
72d3f592 2428init_failed:
c9ffa427 2429 if (amdgpu_sriov_vf(adev))
c6332b97 2430 amdgpu_virt_release_full_gpu(adev, true);
2431
72d3f592 2432 return r;
d38ceaf9
AD
2433}
2434
e3ecdffa
AD
2435/**
2436 * amdgpu_device_fill_reset_magic - writes reset magic to gart pointer
2437 *
2438 * @adev: amdgpu_device pointer
2439 *
2440 * Writes a reset magic value to the gart pointer in VRAM. The driver calls
2441 * this function before a GPU reset. If the value is retained after a
2442 * GPU reset, VRAM has not been lost. Some GPU resets may destry VRAM contents.
2443 */
06ec9070 2444static void amdgpu_device_fill_reset_magic(struct amdgpu_device *adev)
0c49e0b8
CZ
2445{
2446 memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM);
2447}
2448
e3ecdffa
AD
2449/**
2450 * amdgpu_device_check_vram_lost - check if vram is valid
2451 *
2452 * @adev: amdgpu_device pointer
2453 *
2454 * Checks the reset magic value written to the gart pointer in VRAM.
2455 * The driver calls this after a GPU reset to see if the contents of
2456 * VRAM is lost or now.
2457 * returns true if vram is lost, false if not.
2458 */
06ec9070 2459static bool amdgpu_device_check_vram_lost(struct amdgpu_device *adev)
0c49e0b8 2460{
dadce777
EQ
2461 if (memcmp(adev->gart.ptr, adev->reset_magic,
2462 AMDGPU_RESET_MAGIC_NUM))
2463 return true;
2464
53b3f8f4 2465 if (!amdgpu_in_reset(adev))
dadce777
EQ
2466 return false;
2467
2468 /*
2469 * For all ASICs with baco/mode1 reset, the VRAM is
2470 * always assumed to be lost.
2471 */
2472 switch (amdgpu_asic_reset_method(adev)) {
2473 case AMD_RESET_METHOD_BACO:
2474 case AMD_RESET_METHOD_MODE1:
2475 return true;
2476 default:
2477 return false;
2478 }
0c49e0b8
CZ
2479}
2480
e3ecdffa 2481/**
1112a46b 2482 * amdgpu_device_set_cg_state - set clockgating for amdgpu device
e3ecdffa
AD
2483 *
2484 * @adev: amdgpu_device pointer
b8b72130 2485 * @state: clockgating state (gate or ungate)
e3ecdffa 2486 *
e3ecdffa 2487 * The list of all the hardware IPs that make up the asic is walked and the
1112a46b
RZ
2488 * set_clockgating_state callbacks are run.
2489 * Late initialization pass enabling clockgating for hardware IPs.
2490 * Fini or suspend, pass disabling clockgating for hardware IPs.
e3ecdffa
AD
2491 * Returns 0 on success, negative error code on failure.
2492 */
fdd34271 2493
5d89bb2d
LL
2494int amdgpu_device_set_cg_state(struct amdgpu_device *adev,
2495 enum amd_clockgating_state state)
d38ceaf9 2496{
1112a46b 2497 int i, j, r;
d38ceaf9 2498
4a2ba394
SL
2499 if (amdgpu_emu_mode == 1)
2500 return 0;
2501
1112a46b
RZ
2502 for (j = 0; j < adev->num_ip_blocks; j++) {
2503 i = state == AMD_CG_STATE_GATE ? j : adev->num_ip_blocks - j - 1;
a2d31dc3 2504 if (!adev->ip_blocks[i].status.late_initialized)
d38ceaf9 2505 continue;
5d70a549
PV
2506 /* skip CG for GFX on S0ix */
2507 if (adev->in_s0ix &&
2508 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GFX)
2509 continue;
4a446d55 2510 /* skip CG for VCE/UVD, it's handled specially */
a1255107 2511 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
57716327 2512 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
34319b32 2513 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCN &&
52f2e779 2514 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_JPEG &&
57716327 2515 adev->ip_blocks[i].version->funcs->set_clockgating_state) {
4a446d55 2516 /* enable clockgating to save power */
a1255107 2517 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1112a46b 2518 state);
4a446d55
AD
2519 if (r) {
2520 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
a1255107 2521 adev->ip_blocks[i].version->funcs->name, r);
4a446d55
AD
2522 return r;
2523 }
b0b00ff1 2524 }
d38ceaf9 2525 }
06b18f61 2526
c9f96fd5
RZ
2527 return 0;
2528}
2529
5d89bb2d
LL
2530int amdgpu_device_set_pg_state(struct amdgpu_device *adev,
2531 enum amd_powergating_state state)
c9f96fd5 2532{
1112a46b 2533 int i, j, r;
06b18f61 2534
c9f96fd5
RZ
2535 if (amdgpu_emu_mode == 1)
2536 return 0;
2537
1112a46b
RZ
2538 for (j = 0; j < adev->num_ip_blocks; j++) {
2539 i = state == AMD_PG_STATE_GATE ? j : adev->num_ip_blocks - j - 1;
a2d31dc3 2540 if (!adev->ip_blocks[i].status.late_initialized)
c9f96fd5 2541 continue;
5d70a549
PV
2542 /* skip PG for GFX on S0ix */
2543 if (adev->in_s0ix &&
2544 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GFX)
2545 continue;
c9f96fd5
RZ
2546 /* skip CG for VCE/UVD, it's handled specially */
2547 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
2548 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
2549 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCN &&
52f2e779 2550 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_JPEG &&
c9f96fd5
RZ
2551 adev->ip_blocks[i].version->funcs->set_powergating_state) {
2552 /* enable powergating to save power */
2553 r = adev->ip_blocks[i].version->funcs->set_powergating_state((void *)adev,
1112a46b 2554 state);
c9f96fd5
RZ
2555 if (r) {
2556 DRM_ERROR("set_powergating_state(gate) of IP block <%s> failed %d\n",
2557 adev->ip_blocks[i].version->funcs->name, r);
2558 return r;
2559 }
2560 }
2561 }
2dc80b00
S
2562 return 0;
2563}
2564
beff74bc
AD
2565static int amdgpu_device_enable_mgpu_fan_boost(void)
2566{
2567 struct amdgpu_gpu_instance *gpu_ins;
2568 struct amdgpu_device *adev;
2569 int i, ret = 0;
2570
2571 mutex_lock(&mgpu_info.mutex);
2572
2573 /*
2574 * MGPU fan boost feature should be enabled
2575 * only when there are two or more dGPUs in
2576 * the system
2577 */
2578 if (mgpu_info.num_dgpu < 2)
2579 goto out;
2580
2581 for (i = 0; i < mgpu_info.num_dgpu; i++) {
2582 gpu_ins = &(mgpu_info.gpu_ins[i]);
2583 adev = gpu_ins->adev;
2584 if (!(adev->flags & AMD_IS_APU) &&
f10bb940 2585 !gpu_ins->mgpu_fan_enabled) {
beff74bc
AD
2586 ret = amdgpu_dpm_enable_mgpu_fan_boost(adev);
2587 if (ret)
2588 break;
2589
2590 gpu_ins->mgpu_fan_enabled = 1;
2591 }
2592 }
2593
2594out:
2595 mutex_unlock(&mgpu_info.mutex);
2596
2597 return ret;
2598}
2599
e3ecdffa
AD
2600/**
2601 * amdgpu_device_ip_late_init - run late init for hardware IPs
2602 *
2603 * @adev: amdgpu_device pointer
2604 *
2605 * Late initialization pass for hardware IPs. The list of all the hardware
2606 * IPs that make up the asic is walked and the late_init callbacks are run.
2607 * late_init covers any special initialization that an IP requires
2608 * after all of the have been initialized or something that needs to happen
2609 * late in the init process.
2610 * Returns 0 on success, negative error code on failure.
2611 */
06ec9070 2612static int amdgpu_device_ip_late_init(struct amdgpu_device *adev)
2dc80b00 2613{
60599a03 2614 struct amdgpu_gpu_instance *gpu_instance;
2dc80b00
S
2615 int i = 0, r;
2616
2617 for (i = 0; i < adev->num_ip_blocks; i++) {
73f847db 2618 if (!adev->ip_blocks[i].status.hw)
2dc80b00
S
2619 continue;
2620 if (adev->ip_blocks[i].version->funcs->late_init) {
2621 r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
2622 if (r) {
2623 DRM_ERROR("late_init of IP block <%s> failed %d\n",
2624 adev->ip_blocks[i].version->funcs->name, r);
2625 return r;
2626 }
2dc80b00 2627 }
73f847db 2628 adev->ip_blocks[i].status.late_initialized = true;
2dc80b00
S
2629 }
2630
a891d239
DL
2631 amdgpu_ras_set_error_query_ready(adev, true);
2632
1112a46b
RZ
2633 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_GATE);
2634 amdgpu_device_set_pg_state(adev, AMD_PG_STATE_GATE);
916ac57f 2635
06ec9070 2636 amdgpu_device_fill_reset_magic(adev);
d38ceaf9 2637
beff74bc
AD
2638 r = amdgpu_device_enable_mgpu_fan_boost();
2639 if (r)
2640 DRM_ERROR("enable mgpu fan boost failed (%d).\n", r);
2641
4da8b639 2642 /* For passthrough configuration on arcturus and aldebaran, enable special handling SBR */
2643 if (amdgpu_passthrough(adev) && ((adev->asic_type == CHIP_ARCTURUS && adev->gmc.xgmi.num_physical_nodes > 1)||
2644 adev->asic_type == CHIP_ALDEBARAN ))
bc143d8b 2645 amdgpu_dpm_handle_passthrough_sbr(adev, true);
60599a03
EQ
2646
2647 if (adev->gmc.xgmi.num_physical_nodes > 1) {
2648 mutex_lock(&mgpu_info.mutex);
2649
2650 /*
2651 * Reset device p-state to low as this was booted with high.
2652 *
2653 * This should be performed only after all devices from the same
2654 * hive get initialized.
2655 *
2656 * However, it's unknown how many device in the hive in advance.
2657 * As this is counted one by one during devices initializations.
2658 *
2659 * So, we wait for all XGMI interlinked devices initialized.
2660 * This may bring some delays as those devices may come from
2661 * different hives. But that should be OK.
2662 */
2663 if (mgpu_info.num_dgpu == adev->gmc.xgmi.num_physical_nodes) {
2664 for (i = 0; i < mgpu_info.num_gpu; i++) {
2665 gpu_instance = &(mgpu_info.gpu_ins[i]);
2666 if (gpu_instance->adev->flags & AMD_IS_APU)
2667 continue;
2668
d84a430d
JK
2669 r = amdgpu_xgmi_set_pstate(gpu_instance->adev,
2670 AMDGPU_XGMI_PSTATE_MIN);
60599a03
EQ
2671 if (r) {
2672 DRM_ERROR("pstate setting failed (%d).\n", r);
2673 break;
2674 }
2675 }
2676 }
2677
2678 mutex_unlock(&mgpu_info.mutex);
2679 }
2680
d38ceaf9
AD
2681 return 0;
2682}
2683
613aa3ea
LY
2684/**
2685 * amdgpu_device_smu_fini_early - smu hw_fini wrapper
2686 *
2687 * @adev: amdgpu_device pointer
2688 *
2689 * For ASICs need to disable SMC first
2690 */
2691static void amdgpu_device_smu_fini_early(struct amdgpu_device *adev)
2692{
2693 int i, r;
2694
2695 if (adev->ip_versions[GC_HWIP][0] > IP_VERSION(9, 0, 0))
2696 return;
2697
2698 for (i = 0; i < adev->num_ip_blocks; i++) {
2699 if (!adev->ip_blocks[i].status.hw)
2700 continue;
2701 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
2702 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
2703 /* XXX handle errors */
2704 if (r) {
2705 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
2706 adev->ip_blocks[i].version->funcs->name, r);
2707 }
2708 adev->ip_blocks[i].status.hw = false;
2709 break;
2710 }
2711 }
2712}
2713
e9669fb7 2714static int amdgpu_device_ip_fini_early(struct amdgpu_device *adev)
d38ceaf9
AD
2715{
2716 int i, r;
2717
e9669fb7
AG
2718 for (i = 0; i < adev->num_ip_blocks; i++) {
2719 if (!adev->ip_blocks[i].version->funcs->early_fini)
2720 continue;
5278a159 2721
e9669fb7
AG
2722 r = adev->ip_blocks[i].version->funcs->early_fini((void *)adev);
2723 if (r) {
2724 DRM_DEBUG("early_fini of IP block <%s> failed %d\n",
2725 adev->ip_blocks[i].version->funcs->name, r);
2726 }
2727 }
c030f2e4 2728
05df1f01 2729 amdgpu_device_set_pg_state(adev, AMD_PG_STATE_UNGATE);
fdd34271
RZ
2730 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_UNGATE);
2731
7270e895
TY
2732 amdgpu_amdkfd_suspend(adev, false);
2733
613aa3ea
LY
2734 /* Workaroud for ASICs need to disable SMC first */
2735 amdgpu_device_smu_fini_early(adev);
3e96dbfd 2736
d38ceaf9 2737 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
a1255107 2738 if (!adev->ip_blocks[i].status.hw)
d38ceaf9 2739 continue;
8201a67a 2740
a1255107 2741 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
d38ceaf9 2742 /* XXX handle errors */
2c1a2784 2743 if (r) {
a1255107
AD
2744 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
2745 adev->ip_blocks[i].version->funcs->name, r);
2c1a2784 2746 }
8201a67a 2747
a1255107 2748 adev->ip_blocks[i].status.hw = false;
d38ceaf9
AD
2749 }
2750
6effad8a
GC
2751 if (amdgpu_sriov_vf(adev)) {
2752 if (amdgpu_virt_release_full_gpu(adev, false))
2753 DRM_ERROR("failed to release exclusive mode on fini\n");
2754 }
2755
e9669fb7
AG
2756 return 0;
2757}
2758
2759/**
2760 * amdgpu_device_ip_fini - run fini for hardware IPs
2761 *
2762 * @adev: amdgpu_device pointer
2763 *
2764 * Main teardown pass for hardware IPs. The list of all the hardware
2765 * IPs that make up the asic is walked and the hw_fini and sw_fini callbacks
2766 * are run. hw_fini tears down the hardware associated with each IP
2767 * and sw_fini tears down any software state associated with each IP.
2768 * Returns 0 on success, negative error code on failure.
2769 */
2770static int amdgpu_device_ip_fini(struct amdgpu_device *adev)
2771{
2772 int i, r;
2773
2774 if (amdgpu_sriov_vf(adev) && adev->virt.ras_init_done)
2775 amdgpu_virt_release_ras_err_handler_data(adev);
2776
e9669fb7
AG
2777 if (adev->gmc.xgmi.num_physical_nodes > 1)
2778 amdgpu_xgmi_remove_device(adev);
2779
2780 amdgpu_amdkfd_device_fini_sw(adev);
9950cda2 2781
d38ceaf9 2782 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
a1255107 2783 if (!adev->ip_blocks[i].status.sw)
d38ceaf9 2784 continue;
c12aba3a
ML
2785
2786 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
c8963ea4 2787 amdgpu_ucode_free_bo(adev);
1e256e27 2788 amdgpu_free_static_csa(&adev->virt.csa_obj);
c12aba3a
ML
2789 amdgpu_device_wb_fini(adev);
2790 amdgpu_device_vram_scratch_fini(adev);
533aed27 2791 amdgpu_ib_pool_fini(adev);
c12aba3a
ML
2792 }
2793
a1255107 2794 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
d38ceaf9 2795 /* XXX handle errors */
2c1a2784 2796 if (r) {
a1255107
AD
2797 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
2798 adev->ip_blocks[i].version->funcs->name, r);
2c1a2784 2799 }
a1255107
AD
2800 adev->ip_blocks[i].status.sw = false;
2801 adev->ip_blocks[i].status.valid = false;
d38ceaf9
AD
2802 }
2803
a6dcfd9c 2804 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
a1255107 2805 if (!adev->ip_blocks[i].status.late_initialized)
8a2eef1d 2806 continue;
a1255107
AD
2807 if (adev->ip_blocks[i].version->funcs->late_fini)
2808 adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
2809 adev->ip_blocks[i].status.late_initialized = false;
a6dcfd9c
ML
2810 }
2811
c030f2e4 2812 amdgpu_ras_fini(adev);
2813
d38ceaf9
AD
2814 return 0;
2815}
2816
e3ecdffa 2817/**
beff74bc 2818 * amdgpu_device_delayed_init_work_handler - work handler for IB tests
e3ecdffa 2819 *
1112a46b 2820 * @work: work_struct.
e3ecdffa 2821 */
beff74bc 2822static void amdgpu_device_delayed_init_work_handler(struct work_struct *work)
2dc80b00
S
2823{
2824 struct amdgpu_device *adev =
beff74bc 2825 container_of(work, struct amdgpu_device, delayed_init_work.work);
916ac57f
RZ
2826 int r;
2827
2828 r = amdgpu_ib_ring_tests(adev);
2829 if (r)
2830 DRM_ERROR("ib ring test failed (%d).\n", r);
2dc80b00
S
2831}
2832
1e317b99
RZ
2833static void amdgpu_device_delay_enable_gfx_off(struct work_struct *work)
2834{
2835 struct amdgpu_device *adev =
2836 container_of(work, struct amdgpu_device, gfx.gfx_off_delay_work.work);
2837
90a92662
MD
2838 WARN_ON_ONCE(adev->gfx.gfx_off_state);
2839 WARN_ON_ONCE(adev->gfx.gfx_off_req_count);
2840
2841 if (!amdgpu_dpm_set_powergating_by_smu(adev, AMD_IP_BLOCK_TYPE_GFX, true))
2842 adev->gfx.gfx_off_state = true;
1e317b99
RZ
2843}
2844
e3ecdffa 2845/**
e7854a03 2846 * amdgpu_device_ip_suspend_phase1 - run suspend for hardware IPs (phase 1)
e3ecdffa
AD
2847 *
2848 * @adev: amdgpu_device pointer
2849 *
2850 * Main suspend function for hardware IPs. The list of all the hardware
2851 * IPs that make up the asic is walked, clockgating is disabled and the
2852 * suspend callbacks are run. suspend puts the hardware and software state
2853 * in each IP into a state suitable for suspend.
2854 * Returns 0 on success, negative error code on failure.
2855 */
e7854a03
AD
2856static int amdgpu_device_ip_suspend_phase1(struct amdgpu_device *adev)
2857{
2858 int i, r;
2859
50ec83f0
AD
2860 amdgpu_device_set_pg_state(adev, AMD_PG_STATE_UNGATE);
2861 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_UNGATE);
05df1f01 2862
e7854a03
AD
2863 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2864 if (!adev->ip_blocks[i].status.valid)
2865 continue;
2b9f7848 2866
e7854a03 2867 /* displays are handled separately */
2b9f7848
ND
2868 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_DCE)
2869 continue;
2870
2871 /* XXX handle errors */
2872 r = adev->ip_blocks[i].version->funcs->suspend(adev);
2873 /* XXX handle errors */
2874 if (r) {
2875 DRM_ERROR("suspend of IP block <%s> failed %d\n",
2876 adev->ip_blocks[i].version->funcs->name, r);
2877 return r;
e7854a03 2878 }
2b9f7848
ND
2879
2880 adev->ip_blocks[i].status.hw = false;
e7854a03
AD
2881 }
2882
e7854a03
AD
2883 return 0;
2884}
2885
2886/**
2887 * amdgpu_device_ip_suspend_phase2 - run suspend for hardware IPs (phase 2)
2888 *
2889 * @adev: amdgpu_device pointer
2890 *
2891 * Main suspend function for hardware IPs. The list of all the hardware
2892 * IPs that make up the asic is walked, clockgating is disabled and the
2893 * suspend callbacks are run. suspend puts the hardware and software state
2894 * in each IP into a state suitable for suspend.
2895 * Returns 0 on success, negative error code on failure.
2896 */
2897static int amdgpu_device_ip_suspend_phase2(struct amdgpu_device *adev)
d38ceaf9
AD
2898{
2899 int i, r;
2900
557f42a2 2901 if (adev->in_s0ix)
bc143d8b 2902 amdgpu_dpm_gfx_state_change(adev, sGpuChangeState_D3Entry);
34416931 2903
d38ceaf9 2904 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
a1255107 2905 if (!adev->ip_blocks[i].status.valid)
d38ceaf9 2906 continue;
e7854a03
AD
2907 /* displays are handled in phase1 */
2908 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE)
2909 continue;
bff77e86
LM
2910 /* PSP lost connection when err_event_athub occurs */
2911 if (amdgpu_ras_intr_triggered() &&
2912 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
2913 adev->ip_blocks[i].status.hw = false;
2914 continue;
2915 }
e3c1b071 2916
2917 /* skip unnecessary suspend if we do not initialize them yet */
2918 if (adev->gmc.xgmi.pending_reset &&
2919 !(adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
2920 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC ||
2921 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
2922 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH)) {
2923 adev->ip_blocks[i].status.hw = false;
2924 continue;
2925 }
557f42a2 2926
32ff160d
AD
2927 /* skip suspend of gfx and psp for S0ix
2928 * gfx is in gfxoff state, so on resume it will exit gfxoff just
2929 * like at runtime. PSP is also part of the always on hardware
2930 * so no need to suspend it.
2931 */
557f42a2 2932 if (adev->in_s0ix &&
32ff160d
AD
2933 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP ||
2934 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GFX))
557f42a2
AD
2935 continue;
2936
d38ceaf9 2937 /* XXX handle errors */
a1255107 2938 r = adev->ip_blocks[i].version->funcs->suspend(adev);
d38ceaf9 2939 /* XXX handle errors */
2c1a2784 2940 if (r) {
a1255107
AD
2941 DRM_ERROR("suspend of IP block <%s> failed %d\n",
2942 adev->ip_blocks[i].version->funcs->name, r);
2c1a2784 2943 }
876923fb 2944 adev->ip_blocks[i].status.hw = false;
a3a09142 2945 /* handle putting the SMC in the appropriate state */
86b93fd6
JZ
2946 if(!amdgpu_sriov_vf(adev)){
2947 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
2948 r = amdgpu_dpm_set_mp1_state(adev, adev->mp1_state);
2949 if (r) {
2950 DRM_ERROR("SMC failed to set mp1 state %d, %d\n",
2951 adev->mp1_state, r);
2952 return r;
2953 }
a3a09142
AD
2954 }
2955 }
d38ceaf9
AD
2956 }
2957
2958 return 0;
2959}
2960
e7854a03
AD
2961/**
2962 * amdgpu_device_ip_suspend - run suspend for hardware IPs
2963 *
2964 * @adev: amdgpu_device pointer
2965 *
2966 * Main suspend function for hardware IPs. The list of all the hardware
2967 * IPs that make up the asic is walked, clockgating is disabled and the
2968 * suspend callbacks are run. suspend puts the hardware and software state
2969 * in each IP into a state suitable for suspend.
2970 * Returns 0 on success, negative error code on failure.
2971 */
2972int amdgpu_device_ip_suspend(struct amdgpu_device *adev)
2973{
2974 int r;
2975
3c73683c
JC
2976 if (amdgpu_sriov_vf(adev)) {
2977 amdgpu_virt_fini_data_exchange(adev);
e7819644 2978 amdgpu_virt_request_full_gpu(adev, false);
3c73683c 2979 }
e7819644 2980
e7854a03
AD
2981 r = amdgpu_device_ip_suspend_phase1(adev);
2982 if (r)
2983 return r;
2984 r = amdgpu_device_ip_suspend_phase2(adev);
2985
e7819644
YT
2986 if (amdgpu_sriov_vf(adev))
2987 amdgpu_virt_release_full_gpu(adev, false);
2988
e7854a03
AD
2989 return r;
2990}
2991
06ec9070 2992static int amdgpu_device_ip_reinit_early_sriov(struct amdgpu_device *adev)
a90ad3c2
ML
2993{
2994 int i, r;
2995
2cb681b6
ML
2996 static enum amd_ip_block_type ip_order[] = {
2997 AMD_IP_BLOCK_TYPE_GMC,
2998 AMD_IP_BLOCK_TYPE_COMMON,
39186aef 2999 AMD_IP_BLOCK_TYPE_PSP,
2cb681b6
ML
3000 AMD_IP_BLOCK_TYPE_IH,
3001 };
a90ad3c2 3002
95ea3dbc 3003 for (i = 0; i < adev->num_ip_blocks; i++) {
2cb681b6
ML
3004 int j;
3005 struct amdgpu_ip_block *block;
a90ad3c2 3006
4cd2a96d
J
3007 block = &adev->ip_blocks[i];
3008 block->status.hw = false;
2cb681b6 3009
4cd2a96d 3010 for (j = 0; j < ARRAY_SIZE(ip_order); j++) {
2cb681b6 3011
4cd2a96d 3012 if (block->version->type != ip_order[j] ||
2cb681b6
ML
3013 !block->status.valid)
3014 continue;
3015
3016 r = block->version->funcs->hw_init(adev);
0aaeefcc 3017 DRM_INFO("RE-INIT-early: %s %s\n", block->version->funcs->name, r?"failed":"succeeded");
c41d1cf6
ML
3018 if (r)
3019 return r;
482f0e53 3020 block->status.hw = true;
a90ad3c2
ML
3021 }
3022 }
3023
3024 return 0;
3025}
3026
06ec9070 3027static int amdgpu_device_ip_reinit_late_sriov(struct amdgpu_device *adev)
a90ad3c2
ML
3028{
3029 int i, r;
3030
2cb681b6
ML
3031 static enum amd_ip_block_type ip_order[] = {
3032 AMD_IP_BLOCK_TYPE_SMC,
3033 AMD_IP_BLOCK_TYPE_DCE,
3034 AMD_IP_BLOCK_TYPE_GFX,
3035 AMD_IP_BLOCK_TYPE_SDMA,
257deb8c 3036 AMD_IP_BLOCK_TYPE_UVD,
d83c7a07
JJ
3037 AMD_IP_BLOCK_TYPE_VCE,
3038 AMD_IP_BLOCK_TYPE_VCN
2cb681b6 3039 };
a90ad3c2 3040
2cb681b6
ML
3041 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
3042 int j;
3043 struct amdgpu_ip_block *block;
a90ad3c2 3044
2cb681b6
ML
3045 for (j = 0; j < adev->num_ip_blocks; j++) {
3046 block = &adev->ip_blocks[j];
3047
3048 if (block->version->type != ip_order[i] ||
482f0e53
ML
3049 !block->status.valid ||
3050 block->status.hw)
2cb681b6
ML
3051 continue;
3052
895bd048
JZ
3053 if (block->version->type == AMD_IP_BLOCK_TYPE_SMC)
3054 r = block->version->funcs->resume(adev);
3055 else
3056 r = block->version->funcs->hw_init(adev);
3057
0aaeefcc 3058 DRM_INFO("RE-INIT-late: %s %s\n", block->version->funcs->name, r?"failed":"succeeded");
c41d1cf6
ML
3059 if (r)
3060 return r;
482f0e53 3061 block->status.hw = true;
a90ad3c2
ML
3062 }
3063 }
3064
3065 return 0;
3066}
3067
e3ecdffa
AD
3068/**
3069 * amdgpu_device_ip_resume_phase1 - run resume for hardware IPs
3070 *
3071 * @adev: amdgpu_device pointer
3072 *
3073 * First resume function for hardware IPs. The list of all the hardware
3074 * IPs that make up the asic is walked and the resume callbacks are run for
3075 * COMMON, GMC, and IH. resume puts the hardware into a functional state
3076 * after a suspend and updates the software state as necessary. This
3077 * function is also used for restoring the GPU after a GPU reset.
3078 * Returns 0 on success, negative error code on failure.
3079 */
06ec9070 3080static int amdgpu_device_ip_resume_phase1(struct amdgpu_device *adev)
d38ceaf9
AD
3081{
3082 int i, r;
3083
a90ad3c2 3084 for (i = 0; i < adev->num_ip_blocks; i++) {
482f0e53 3085 if (!adev->ip_blocks[i].status.valid || adev->ip_blocks[i].status.hw)
a90ad3c2 3086 continue;
a90ad3c2 3087 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
e3ecdffa
AD
3088 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
3089 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH) {
482f0e53 3090
fcf0649f
CZ
3091 r = adev->ip_blocks[i].version->funcs->resume(adev);
3092 if (r) {
3093 DRM_ERROR("resume of IP block <%s> failed %d\n",
3094 adev->ip_blocks[i].version->funcs->name, r);
3095 return r;
3096 }
482f0e53 3097 adev->ip_blocks[i].status.hw = true;
a90ad3c2
ML
3098 }
3099 }
3100
3101 return 0;
3102}
3103
e3ecdffa
AD
3104/**
3105 * amdgpu_device_ip_resume_phase2 - run resume for hardware IPs
3106 *
3107 * @adev: amdgpu_device pointer
3108 *
3109 * First resume function for hardware IPs. The list of all the hardware
3110 * IPs that make up the asic is walked and the resume callbacks are run for
3111 * all blocks except COMMON, GMC, and IH. resume puts the hardware into a
3112 * functional state after a suspend and updates the software state as
3113 * necessary. This function is also used for restoring the GPU after a GPU
3114 * reset.
3115 * Returns 0 on success, negative error code on failure.
3116 */
06ec9070 3117static int amdgpu_device_ip_resume_phase2(struct amdgpu_device *adev)
d38ceaf9
AD
3118{
3119 int i, r;
3120
3121 for (i = 0; i < adev->num_ip_blocks; i++) {
482f0e53 3122 if (!adev->ip_blocks[i].status.valid || adev->ip_blocks[i].status.hw)
d38ceaf9 3123 continue;
fcf0649f 3124 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
e3ecdffa 3125 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
7a3e0bb2
RZ
3126 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH ||
3127 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP)
fcf0649f 3128 continue;
a1255107 3129 r = adev->ip_blocks[i].version->funcs->resume(adev);
2c1a2784 3130 if (r) {
a1255107
AD
3131 DRM_ERROR("resume of IP block <%s> failed %d\n",
3132 adev->ip_blocks[i].version->funcs->name, r);
d38ceaf9 3133 return r;
2c1a2784 3134 }
482f0e53 3135 adev->ip_blocks[i].status.hw = true;
d38ceaf9
AD
3136 }
3137
3138 return 0;
3139}
3140
e3ecdffa
AD
3141/**
3142 * amdgpu_device_ip_resume - run resume for hardware IPs
3143 *
3144 * @adev: amdgpu_device pointer
3145 *
3146 * Main resume function for hardware IPs. The hardware IPs
3147 * are split into two resume functions because they are
3148 * are also used in in recovering from a GPU reset and some additional
3149 * steps need to be take between them. In this case (S3/S4) they are
3150 * run sequentially.
3151 * Returns 0 on success, negative error code on failure.
3152 */
06ec9070 3153static int amdgpu_device_ip_resume(struct amdgpu_device *adev)
fcf0649f
CZ
3154{
3155 int r;
3156
9cec53c1
JZ
3157 r = amdgpu_amdkfd_resume_iommu(adev);
3158 if (r)
3159 return r;
3160
06ec9070 3161 r = amdgpu_device_ip_resume_phase1(adev);
fcf0649f
CZ
3162 if (r)
3163 return r;
7a3e0bb2
RZ
3164
3165 r = amdgpu_device_fw_loading(adev);
3166 if (r)
3167 return r;
3168
06ec9070 3169 r = amdgpu_device_ip_resume_phase2(adev);
fcf0649f
CZ
3170
3171 return r;
3172}
3173
e3ecdffa
AD
3174/**
3175 * amdgpu_device_detect_sriov_bios - determine if the board supports SR-IOV
3176 *
3177 * @adev: amdgpu_device pointer
3178 *
3179 * Query the VBIOS data tables to determine if the board supports SR-IOV.
3180 */
4e99a44e 3181static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
048765ad 3182{
6867e1b5
ML
3183 if (amdgpu_sriov_vf(adev)) {
3184 if (adev->is_atom_fw) {
58ff791a 3185 if (amdgpu_atomfirmware_gpu_virtualization_supported(adev))
6867e1b5
ML
3186 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
3187 } else {
3188 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
3189 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
3190 }
3191
3192 if (!(adev->virt.caps & AMDGPU_SRIOV_CAPS_SRIOV_VBIOS))
3193 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_NO_VBIOS, 0, 0);
a5bde2f9 3194 }
048765ad
AR
3195}
3196
e3ecdffa
AD
3197/**
3198 * amdgpu_device_asic_has_dc_support - determine if DC supports the asic
3199 *
3200 * @asic_type: AMD asic type
3201 *
3202 * Check if there is DC (new modesetting infrastructre) support for an asic.
3203 * returns true if DC has support, false if not.
3204 */
4562236b
HW
3205bool amdgpu_device_asic_has_dc_support(enum amd_asic_type asic_type)
3206{
3207 switch (asic_type) {
0637d417
AD
3208#ifdef CONFIG_DRM_AMDGPU_SI
3209 case CHIP_HAINAN:
3210#endif
3211 case CHIP_TOPAZ:
3212 /* chips with no display hardware */
3213 return false;
4562236b 3214#if defined(CONFIG_DRM_AMD_DC)
64200c46
MR
3215 case CHIP_TAHITI:
3216 case CHIP_PITCAIRN:
3217 case CHIP_VERDE:
3218 case CHIP_OLAND:
2d32ffd6
AD
3219 /*
3220 * We have systems in the wild with these ASICs that require
3221 * LVDS and VGA support which is not supported with DC.
3222 *
3223 * Fallback to the non-DC driver here by default so as not to
3224 * cause regressions.
3225 */
3226#if defined(CONFIG_DRM_AMD_DC_SI)
3227 return amdgpu_dc > 0;
3228#else
3229 return false;
64200c46 3230#endif
4562236b 3231 case CHIP_BONAIRE:
0d6fbccb 3232 case CHIP_KAVERI:
367e6687
AD
3233 case CHIP_KABINI:
3234 case CHIP_MULLINS:
d9fda248
HW
3235 /*
3236 * We have systems in the wild with these ASICs that require
3237 * LVDS and VGA support which is not supported with DC.
3238 *
3239 * Fallback to the non-DC driver here by default so as not to
3240 * cause regressions.
3241 */
3242 return amdgpu_dc > 0;
3243 case CHIP_HAWAII:
4562236b
HW
3244 case CHIP_CARRIZO:
3245 case CHIP_STONEY:
4562236b 3246 case CHIP_POLARIS10:
675fd32b 3247 case CHIP_POLARIS11:
2c8ad2d5 3248 case CHIP_POLARIS12:
675fd32b 3249 case CHIP_VEGAM:
4562236b
HW
3250 case CHIP_TONGA:
3251 case CHIP_FIJI:
42f8ffa1 3252 case CHIP_VEGA10:
dca7b401 3253 case CHIP_VEGA12:
c6034aa2 3254 case CHIP_VEGA20:
b86a1aa3 3255#if defined(CONFIG_DRM_AMD_DC_DCN)
fd187853 3256 case CHIP_RAVEN:
b4f199c7 3257 case CHIP_NAVI10:
8fceceb6 3258 case CHIP_NAVI14:
078655d9 3259 case CHIP_NAVI12:
e1c14c43 3260 case CHIP_RENOIR:
3f68c01b 3261 case CHIP_CYAN_SKILLFISH:
81d9bfb8 3262 case CHIP_SIENNA_CICHLID:
a6c5308f 3263 case CHIP_NAVY_FLOUNDER:
7cc656e2 3264 case CHIP_DIMGREY_CAVEFISH:
ddaed58b 3265 case CHIP_BEIGE_GOBY:
84b934bc 3266 case CHIP_VANGOGH:
c8b73f7f 3267 case CHIP_YELLOW_CARP:
42f8ffa1 3268#endif
f7f12b25 3269 default:
fd187853 3270 return amdgpu_dc != 0;
f7f12b25 3271#else
4562236b 3272 default:
93b09a9a 3273 if (amdgpu_dc > 0)
044a48f4 3274 DRM_INFO_ONCE("Display Core has been requested via kernel parameter "
93b09a9a 3275 "but isn't supported by ASIC, ignoring\n");
4562236b 3276 return false;
f7f12b25 3277#endif
4562236b
HW
3278 }
3279}
3280
3281/**
3282 * amdgpu_device_has_dc_support - check if dc is supported
3283 *
982a820b 3284 * @adev: amdgpu_device pointer
4562236b
HW
3285 *
3286 * Returns true for supported, false for not supported
3287 */
3288bool amdgpu_device_has_dc_support(struct amdgpu_device *adev)
3289{
abaf210c
AS
3290 if (amdgpu_sriov_vf(adev) ||
3291 adev->enable_virtual_display ||
3292 (adev->harvest_ip_mask & AMD_HARVEST_IP_DMU_MASK))
2555039d
XY
3293 return false;
3294
4562236b
HW
3295 return amdgpu_device_asic_has_dc_support(adev->asic_type);
3296}
3297
d4535e2c
AG
3298static void amdgpu_device_xgmi_reset_func(struct work_struct *__work)
3299{
3300 struct amdgpu_device *adev =
3301 container_of(__work, struct amdgpu_device, xgmi_reset_work);
d95e8e97 3302 struct amdgpu_hive_info *hive = amdgpu_get_xgmi_hive(adev);
d4535e2c 3303
c6a6e2db
AG
3304 /* It's a bug to not have a hive within this function */
3305 if (WARN_ON(!hive))
3306 return;
3307
3308 /*
3309 * Use task barrier to synchronize all xgmi reset works across the
3310 * hive. task_barrier_enter and task_barrier_exit will block
3311 * until all the threads running the xgmi reset works reach
3312 * those points. task_barrier_full will do both blocks.
3313 */
3314 if (amdgpu_asic_reset_method(adev) == AMD_RESET_METHOD_BACO) {
3315
3316 task_barrier_enter(&hive->tb);
4a580877 3317 adev->asic_reset_res = amdgpu_device_baco_enter(adev_to_drm(adev));
c6a6e2db
AG
3318
3319 if (adev->asic_reset_res)
3320 goto fail;
3321
3322 task_barrier_exit(&hive->tb);
4a580877 3323 adev->asic_reset_res = amdgpu_device_baco_exit(adev_to_drm(adev));
c6a6e2db
AG
3324
3325 if (adev->asic_reset_res)
3326 goto fail;
43c4d576 3327
5e67bba3 3328 if (adev->mmhub.ras && adev->mmhub.ras->ras_block.hw_ops &&
3329 adev->mmhub.ras->ras_block.hw_ops->reset_ras_error_count)
3330 adev->mmhub.ras->ras_block.hw_ops->reset_ras_error_count(adev);
c6a6e2db
AG
3331 } else {
3332
3333 task_barrier_full(&hive->tb);
3334 adev->asic_reset_res = amdgpu_asic_reset(adev);
3335 }
ce316fa5 3336
c6a6e2db 3337fail:
d4535e2c 3338 if (adev->asic_reset_res)
fed184e9 3339 DRM_WARN("ASIC reset failed with error, %d for drm dev, %s",
4a580877 3340 adev->asic_reset_res, adev_to_drm(adev)->unique);
d95e8e97 3341 amdgpu_put_xgmi_hive(hive);
d4535e2c
AG
3342}
3343
71f98027
AD
3344static int amdgpu_device_get_job_timeout_settings(struct amdgpu_device *adev)
3345{
3346 char *input = amdgpu_lockup_timeout;
3347 char *timeout_setting = NULL;
3348 int index = 0;
3349 long timeout;
3350 int ret = 0;
3351
3352 /*
67387dfe
AD
3353 * By default timeout for non compute jobs is 10000
3354 * and 60000 for compute jobs.
71f98027 3355 * In SR-IOV or passthrough mode, timeout for compute
b7b2a316 3356 * jobs are 60000 by default.
71f98027
AD
3357 */
3358 adev->gfx_timeout = msecs_to_jiffies(10000);
3359 adev->sdma_timeout = adev->video_timeout = adev->gfx_timeout;
9882e278
ED
3360 if (amdgpu_sriov_vf(adev))
3361 adev->compute_timeout = amdgpu_sriov_is_pp_one_vf(adev) ?
3362 msecs_to_jiffies(60000) : msecs_to_jiffies(10000);
71f98027 3363 else
67387dfe 3364 adev->compute_timeout = msecs_to_jiffies(60000);
71f98027 3365
f440ff44 3366 if (strnlen(input, AMDGPU_MAX_TIMEOUT_PARAM_LENGTH)) {
71f98027 3367 while ((timeout_setting = strsep(&input, ",")) &&
f440ff44 3368 strnlen(timeout_setting, AMDGPU_MAX_TIMEOUT_PARAM_LENGTH)) {
71f98027
AD
3369 ret = kstrtol(timeout_setting, 0, &timeout);
3370 if (ret)
3371 return ret;
3372
3373 if (timeout == 0) {
3374 index++;
3375 continue;
3376 } else if (timeout < 0) {
3377 timeout = MAX_SCHEDULE_TIMEOUT;
127aedf9
CK
3378 dev_warn(adev->dev, "lockup timeout disabled");
3379 add_taint(TAINT_SOFTLOCKUP, LOCKDEP_STILL_OK);
71f98027
AD
3380 } else {
3381 timeout = msecs_to_jiffies(timeout);
3382 }
3383
3384 switch (index++) {
3385 case 0:
3386 adev->gfx_timeout = timeout;
3387 break;
3388 case 1:
3389 adev->compute_timeout = timeout;
3390 break;
3391 case 2:
3392 adev->sdma_timeout = timeout;
3393 break;
3394 case 3:
3395 adev->video_timeout = timeout;
3396 break;
3397 default:
3398 break;
3399 }
3400 }
3401 /*
3402 * There is only one value specified and
3403 * it should apply to all non-compute jobs.
3404 */
bcccee89 3405 if (index == 1) {
71f98027 3406 adev->sdma_timeout = adev->video_timeout = adev->gfx_timeout;
bcccee89
ED
3407 if (amdgpu_sriov_vf(adev) || amdgpu_passthrough(adev))
3408 adev->compute_timeout = adev->gfx_timeout;
3409 }
71f98027
AD
3410 }
3411
3412 return ret;
3413}
d4535e2c 3414
4a74c38c
PY
3415/**
3416 * amdgpu_device_check_iommu_direct_map - check if RAM direct mapped to GPU
3417 *
3418 * @adev: amdgpu_device pointer
3419 *
3420 * RAM direct mapped to GPU if IOMMU is not enabled or is pass through mode
3421 */
3422static void amdgpu_device_check_iommu_direct_map(struct amdgpu_device *adev)
3423{
3424 struct iommu_domain *domain;
3425
3426 domain = iommu_get_domain_for_dev(adev->dev);
3427 if (!domain || domain->type == IOMMU_DOMAIN_IDENTITY)
3428 adev->ram_is_direct_mapped = true;
3429}
3430
77f3a5cd
ND
3431static const struct attribute *amdgpu_dev_attributes[] = {
3432 &dev_attr_product_name.attr,
3433 &dev_attr_product_number.attr,
3434 &dev_attr_serial_number.attr,
3435 &dev_attr_pcie_replay_count.attr,
3436 NULL
3437};
3438
d38ceaf9
AD
3439/**
3440 * amdgpu_device_init - initialize the driver
3441 *
3442 * @adev: amdgpu_device pointer
d38ceaf9
AD
3443 * @flags: driver flags
3444 *
3445 * Initializes the driver info and hw (all asics).
3446 * Returns 0 for success or an error on failure.
3447 * Called at driver startup.
3448 */
3449int amdgpu_device_init(struct amdgpu_device *adev,
d38ceaf9
AD
3450 uint32_t flags)
3451{
8aba21b7
LT
3452 struct drm_device *ddev = adev_to_drm(adev);
3453 struct pci_dev *pdev = adev->pdev;
d38ceaf9 3454 int r, i;
b98c6299 3455 bool px = false;
95844d20 3456 u32 max_MBps;
d38ceaf9
AD
3457
3458 adev->shutdown = false;
d38ceaf9 3459 adev->flags = flags;
4e66d7d2
YZ
3460
3461 if (amdgpu_force_asic_type >= 0 && amdgpu_force_asic_type < CHIP_LAST)
3462 adev->asic_type = amdgpu_force_asic_type;
3463 else
3464 adev->asic_type = flags & AMD_ASIC_MASK;
3465
d38ceaf9 3466 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
593aa2d2 3467 if (amdgpu_emu_mode == 1)
8bdab6bb 3468 adev->usec_timeout *= 10;
770d13b1 3469 adev->gmc.gart_size = 512 * 1024 * 1024;
d38ceaf9
AD
3470 adev->accel_working = false;
3471 adev->num_rings = 0;
3472 adev->mman.buffer_funcs = NULL;
3473 adev->mman.buffer_funcs_ring = NULL;
3474 adev->vm_manager.vm_pte_funcs = NULL;
0c88b430 3475 adev->vm_manager.vm_pte_num_scheds = 0;
132f34e4 3476 adev->gmc.gmc_funcs = NULL;
7bd939d0 3477 adev->harvest_ip_mask = 0x0;
f54d1867 3478 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
b8866c26 3479 bitmap_zero(adev->gfx.pipe_reserve_bitmap, AMDGPU_MAX_COMPUTE_QUEUES);
d38ceaf9
AD
3480
3481 adev->smc_rreg = &amdgpu_invalid_rreg;
3482 adev->smc_wreg = &amdgpu_invalid_wreg;
3483 adev->pcie_rreg = &amdgpu_invalid_rreg;
3484 adev->pcie_wreg = &amdgpu_invalid_wreg;
36b9a952
HR
3485 adev->pciep_rreg = &amdgpu_invalid_rreg;
3486 adev->pciep_wreg = &amdgpu_invalid_wreg;
4fa1c6a6
TZ
3487 adev->pcie_rreg64 = &amdgpu_invalid_rreg64;
3488 adev->pcie_wreg64 = &amdgpu_invalid_wreg64;
d38ceaf9
AD
3489 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
3490 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
3491 adev->didt_rreg = &amdgpu_invalid_rreg;
3492 adev->didt_wreg = &amdgpu_invalid_wreg;
ccdbb20a
RZ
3493 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
3494 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
d38ceaf9
AD
3495 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
3496 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
3497
3e39ab90
AD
3498 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
3499 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
3500 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
d38ceaf9
AD
3501
3502 /* mutex initialization are all done here so we
3503 * can recall function without having locking issues */
0e5ca0d1 3504 mutex_init(&adev->firmware.mutex);
d38ceaf9
AD
3505 mutex_init(&adev->pm.mutex);
3506 mutex_init(&adev->gfx.gpu_clock_mutex);
3507 mutex_init(&adev->srbm_mutex);
b8866c26 3508 mutex_init(&adev->gfx.pipe_reserve_mutex);
d23ee13f 3509 mutex_init(&adev->gfx.gfx_off_mutex);
d38ceaf9 3510 mutex_init(&adev->grbm_idx_mutex);
d38ceaf9 3511 mutex_init(&adev->mn_lock);
e23b74aa 3512 mutex_init(&adev->virt.vf_errors.lock);
d38ceaf9 3513 hash_init(adev->mn_hash);
53b3f8f4 3514 atomic_set(&adev->in_gpu_reset, 0);
6049db43 3515 init_rwsem(&adev->reset_sem);
32eaeae0 3516 mutex_init(&adev->psp.mutex);
bd052211 3517 mutex_init(&adev->notifier_lock);
8cda7a4f 3518 mutex_init(&adev->pm.stable_pstate_ctx_lock);
d38ceaf9 3519
ab3b9de6 3520 amdgpu_device_init_apu_flags(adev);
9f6a7857 3521
912dfc84
EQ
3522 r = amdgpu_device_check_arguments(adev);
3523 if (r)
3524 return r;
d38ceaf9 3525
d38ceaf9
AD
3526 spin_lock_init(&adev->mmio_idx_lock);
3527 spin_lock_init(&adev->smc_idx_lock);
3528 spin_lock_init(&adev->pcie_idx_lock);
3529 spin_lock_init(&adev->uvd_ctx_idx_lock);
3530 spin_lock_init(&adev->didt_idx_lock);
ccdbb20a 3531 spin_lock_init(&adev->gc_cac_idx_lock);
16abb5d2 3532 spin_lock_init(&adev->se_cac_idx_lock);
d38ceaf9 3533 spin_lock_init(&adev->audio_endpt_idx_lock);
95844d20 3534 spin_lock_init(&adev->mm_stats.lock);
d38ceaf9 3535
0c4e7fa5
CZ
3536 INIT_LIST_HEAD(&adev->shadow_list);
3537 mutex_init(&adev->shadow_list_lock);
3538
655ce9cb 3539 INIT_LIST_HEAD(&adev->reset_list);
3540
6492e1b0 3541 INIT_LIST_HEAD(&adev->ras_list);
3542
beff74bc
AD
3543 INIT_DELAYED_WORK(&adev->delayed_init_work,
3544 amdgpu_device_delayed_init_work_handler);
1e317b99
RZ
3545 INIT_DELAYED_WORK(&adev->gfx.gfx_off_delay_work,
3546 amdgpu_device_delay_enable_gfx_off);
2dc80b00 3547
d4535e2c
AG
3548 INIT_WORK(&adev->xgmi_reset_work, amdgpu_device_xgmi_reset_func);
3549
d23ee13f 3550 adev->gfx.gfx_off_req_count = 1;
b6e79d9a 3551 adev->pm.ac_power = power_supply_is_system_supplied() > 0;
b1ddf548 3552
b265bdbd
EQ
3553 atomic_set(&adev->throttling_logging_enabled, 1);
3554 /*
3555 * If throttling continues, logging will be performed every minute
3556 * to avoid log flooding. "-1" is subtracted since the thermal
3557 * throttling interrupt comes every second. Thus, the total logging
3558 * interval is 59 seconds(retelimited printk interval) + 1(waiting
3559 * for throttling interrupt) = 60 seconds.
3560 */
3561 ratelimit_state_init(&adev->throttling_logging_rs, (60 - 1) * HZ, 1);
3562 ratelimit_set_flags(&adev->throttling_logging_rs, RATELIMIT_MSG_ON_RELEASE);
3563
0fa49558
AX
3564 /* Registers mapping */
3565 /* TODO: block userspace mapping of io register */
da69c161
KW
3566 if (adev->asic_type >= CHIP_BONAIRE) {
3567 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
3568 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
3569 } else {
3570 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
3571 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
3572 }
d38ceaf9 3573
6c08e0ef
EQ
3574 for (i = 0; i < AMD_IP_BLOCK_TYPE_NUM; i++)
3575 atomic_set(&adev->pm.pwr_state[i], POWER_STATE_UNKNOWN);
3576
d38ceaf9
AD
3577 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
3578 if (adev->rmmio == NULL) {
3579 return -ENOMEM;
3580 }
3581 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
3582 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
3583
5494d864
AD
3584 amdgpu_device_get_pcie_info(adev);
3585
b239c017
JX
3586 if (amdgpu_mcbp)
3587 DRM_INFO("MCBP is enabled\n");
3588
5f84cc63
JX
3589 if (amdgpu_mes && adev->asic_type >= CHIP_NAVI10)
3590 adev->enable_mes = true;
3591
3aa0115d
ML
3592 /* detect hw virtualization here */
3593 amdgpu_detect_virtualization(adev);
3594
dffa11b4
ML
3595 r = amdgpu_device_get_job_timeout_settings(adev);
3596 if (r) {
3597 dev_err(adev->dev, "invalid lockup_timeout parameter syntax\n");
4ef87d8f 3598 return r;
a190d1c7
XY
3599 }
3600
d38ceaf9 3601 /* early init functions */
06ec9070 3602 r = amdgpu_device_ip_early_init(adev);
d38ceaf9 3603 if (r)
4ef87d8f 3604 return r;
d38ceaf9 3605
4a0165f0
VS
3606 /* Need to get xgmi info early to decide the reset behavior*/
3607 if (adev->gmc.xgmi.supported) {
3608 r = adev->gfxhub.funcs->get_xgmi_info(adev);
3609 if (r)
3610 return r;
3611 }
3612
8e6d0b69 3613 /* enable PCIE atomic ops */
3614 if (amdgpu_sriov_vf(adev))
3615 adev->have_atomics_support = ((struct amd_sriov_msg_pf2vf_info *)
3616 adev->virt.fw_reserve.p_pf2vf)->pcie_atomic_ops_enabled_flags ==
3617 (PCI_EXP_DEVCAP2_ATOMIC_COMP32 | PCI_EXP_DEVCAP2_ATOMIC_COMP64);
3618 else
3619 adev->have_atomics_support =
3620 !pci_enable_atomic_ops_to_root(adev->pdev,
3621 PCI_EXP_DEVCAP2_ATOMIC_COMP32 |
3622 PCI_EXP_DEVCAP2_ATOMIC_COMP64);
3623 if (!adev->have_atomics_support)
3624 dev_info(adev->dev, "PCIE atomic ops is not supported\n");
3625
6585661d
OZ
3626 /* doorbell bar mapping and doorbell index init*/
3627 amdgpu_device_doorbell_init(adev);
3628
9475a943
SL
3629 if (amdgpu_emu_mode == 1) {
3630 /* post the asic on emulation mode */
3631 emu_soc_asic_init(adev);
bfca0289 3632 goto fence_driver_init;
9475a943 3633 }
bfca0289 3634
04442bf7
LL
3635 amdgpu_reset_init(adev);
3636
4e99a44e
ML
3637 /* detect if we are with an SRIOV vbios */
3638 amdgpu_device_detect_sriov_bios(adev);
048765ad 3639
95e8e59e
AD
3640 /* check if we need to reset the asic
3641 * E.g., driver was not cleanly unloaded previously, etc.
3642 */
f14899fd 3643 if (!amdgpu_sriov_vf(adev) && amdgpu_asic_need_reset_on_init(adev)) {
e3c1b071 3644 if (adev->gmc.xgmi.num_physical_nodes) {
3645 dev_info(adev->dev, "Pending hive reset.\n");
3646 adev->gmc.xgmi.pending_reset = true;
3647 /* Only need to init necessary block for SMU to handle the reset */
3648 for (i = 0; i < adev->num_ip_blocks; i++) {
3649 if (!adev->ip_blocks[i].status.valid)
3650 continue;
3651 if (!(adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
3652 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
3653 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH ||
3654 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC)) {
751f43e7 3655 DRM_DEBUG("IP %s disabled for hw_init.\n",
e3c1b071 3656 adev->ip_blocks[i].version->funcs->name);
3657 adev->ip_blocks[i].status.hw = true;
3658 }
3659 }
3660 } else {
3661 r = amdgpu_asic_reset(adev);
3662 if (r) {
3663 dev_err(adev->dev, "asic reset on init failed\n");
3664 goto failed;
3665 }
95e8e59e
AD
3666 }
3667 }
3668
8f66090b 3669 pci_enable_pcie_error_reporting(adev->pdev);
c9a6b82f 3670
d38ceaf9 3671 /* Post card if necessary */
39c640c0 3672 if (amdgpu_device_need_post(adev)) {
d38ceaf9 3673 if (!adev->bios) {
bec86378 3674 dev_err(adev->dev, "no vBIOS found\n");
83ba126a
AD
3675 r = -EINVAL;
3676 goto failed;
d38ceaf9 3677 }
bec86378 3678 DRM_INFO("GPU posting now...\n");
4d2997ab 3679 r = amdgpu_device_asic_init(adev);
4e99a44e
ML
3680 if (r) {
3681 dev_err(adev->dev, "gpu post error!\n");
3682 goto failed;
3683 }
d38ceaf9
AD
3684 }
3685
88b64e95
AD
3686 if (adev->is_atom_fw) {
3687 /* Initialize clocks */
3688 r = amdgpu_atomfirmware_get_clock_info(adev);
3689 if (r) {
3690 dev_err(adev->dev, "amdgpu_atomfirmware_get_clock_info failed\n");
e23b74aa 3691 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
88b64e95
AD
3692 goto failed;
3693 }
3694 } else {
a5bde2f9
AD
3695 /* Initialize clocks */
3696 r = amdgpu_atombios_get_clock_info(adev);
3697 if (r) {
3698 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
e23b74aa 3699 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
89041940 3700 goto failed;
a5bde2f9
AD
3701 }
3702 /* init i2c buses */
4562236b
HW
3703 if (!amdgpu_device_has_dc_support(adev))
3704 amdgpu_atombios_i2c_init(adev);
2c1a2784 3705 }
d38ceaf9 3706
bfca0289 3707fence_driver_init:
d38ceaf9 3708 /* Fence driver */
067f44c8 3709 r = amdgpu_fence_driver_sw_init(adev);
2c1a2784 3710 if (r) {
067f44c8 3711 dev_err(adev->dev, "amdgpu_fence_driver_sw_init failed\n");
e23b74aa 3712 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0);
83ba126a 3713 goto failed;
2c1a2784 3714 }
d38ceaf9
AD
3715
3716 /* init the mode config */
4a580877 3717 drm_mode_config_init(adev_to_drm(adev));
d38ceaf9 3718
06ec9070 3719 r = amdgpu_device_ip_init(adev);
d38ceaf9 3720 if (r) {
8840a387 3721 /* failed in exclusive mode due to timeout */
3722 if (amdgpu_sriov_vf(adev) &&
3723 !amdgpu_sriov_runtime(adev) &&
3724 amdgpu_virt_mmio_blocked(adev) &&
3725 !amdgpu_virt_wait_reset(adev)) {
3726 dev_err(adev->dev, "VF exclusive mode timeout\n");
1daee8b4
PD
3727 /* Don't send request since VF is inactive. */
3728 adev->virt.caps &= ~AMDGPU_SRIOV_CAPS_RUNTIME;
3729 adev->virt.ops = NULL;
8840a387 3730 r = -EAGAIN;
970fd197 3731 goto release_ras_con;
8840a387 3732 }
06ec9070 3733 dev_err(adev->dev, "amdgpu_device_ip_init failed\n");
e23b74aa 3734 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0);
970fd197 3735 goto release_ras_con;
d38ceaf9
AD
3736 }
3737
8d35a259
LG
3738 amdgpu_fence_driver_hw_init(adev);
3739
d69b8971
YZ
3740 dev_info(adev->dev,
3741 "SE %d, SH per SE %d, CU per SH %d, active_cu_number %d\n",
d7f72fe4
YZ
3742 adev->gfx.config.max_shader_engines,
3743 adev->gfx.config.max_sh_per_se,
3744 adev->gfx.config.max_cu_per_sh,
3745 adev->gfx.cu_info.number);
3746
d38ceaf9
AD
3747 adev->accel_working = true;
3748
e59c0205
AX
3749 amdgpu_vm_check_compute_bug(adev);
3750
95844d20
MO
3751 /* Initialize the buffer migration limit. */
3752 if (amdgpu_moverate >= 0)
3753 max_MBps = amdgpu_moverate;
3754 else
3755 max_MBps = 8; /* Allow 8 MB/s. */
3756 /* Get a log2 for easy divisions. */
3757 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
3758
d2f52ac8 3759 r = amdgpu_pm_sysfs_init(adev);
7c868b59
YT
3760 if (r) {
3761 adev->pm_sysfs_en = false;
d2f52ac8 3762 DRM_ERROR("registering pm debugfs failed (%d).\n", r);
7c868b59
YT
3763 } else
3764 adev->pm_sysfs_en = true;
d2f52ac8 3765
5bb23532 3766 r = amdgpu_ucode_sysfs_init(adev);
7c868b59
YT
3767 if (r) {
3768 adev->ucode_sysfs_en = false;
5bb23532 3769 DRM_ERROR("Creating firmware sysfs failed (%d).\n", r);
7c868b59
YT
3770 } else
3771 adev->ucode_sysfs_en = true;
5bb23532 3772
d38ceaf9
AD
3773 if ((amdgpu_testing & 1)) {
3774 if (adev->accel_working)
3775 amdgpu_test_moves(adev);
3776 else
3777 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
3778 }
d38ceaf9
AD
3779 if (amdgpu_benchmarking) {
3780 if (adev->accel_working)
3781 amdgpu_benchmark(adev, amdgpu_benchmarking);
3782 else
3783 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
3784 }
3785
b0adca4d
EQ
3786 /*
3787 * Register gpu instance before amdgpu_device_enable_mgpu_fan_boost.
3788 * Otherwise the mgpu fan boost feature will be skipped due to the
3789 * gpu instance is counted less.
3790 */
3791 amdgpu_register_gpu_instance(adev);
3792
d38ceaf9
AD
3793 /* enable clockgating, etc. after ib tests, etc. since some blocks require
3794 * explicit gating rather than handling it automatically.
3795 */
e3c1b071 3796 if (!adev->gmc.xgmi.pending_reset) {
3797 r = amdgpu_device_ip_late_init(adev);
3798 if (r) {
3799 dev_err(adev->dev, "amdgpu_device_ip_late_init failed\n");
3800 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r);
970fd197 3801 goto release_ras_con;
e3c1b071 3802 }
3803 /* must succeed. */
3804 amdgpu_ras_resume(adev);
3805 queue_delayed_work(system_wq, &adev->delayed_init_work,
3806 msecs_to_jiffies(AMDGPU_RESUME_MS));
2c1a2784 3807 }
d38ceaf9 3808
2c738637
ML
3809 if (amdgpu_sriov_vf(adev))
3810 flush_delayed_work(&adev->delayed_init_work);
3811
77f3a5cd 3812 r = sysfs_create_files(&adev->dev->kobj, amdgpu_dev_attributes);
5aea5327 3813 if (r)
77f3a5cd 3814 dev_err(adev->dev, "Could not create amdgpu device attr\n");
bd607166 3815
d155bef0
AB
3816 if (IS_ENABLED(CONFIG_PERF_EVENTS))
3817 r = amdgpu_pmu_init(adev);
9c7c85f7
JK
3818 if (r)
3819 dev_err(adev->dev, "amdgpu_pmu_init failed\n");
3820
c1dd4aa6
AG
3821 /* Have stored pci confspace at hand for restore in sudden PCI error */
3822 if (amdgpu_device_cache_pci_state(adev->pdev))
3823 pci_restore_state(pdev);
3824
8c3dd61c
KHF
3825 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
3826 /* this will fail for cards that aren't VGA class devices, just
3827 * ignore it */
3828 if ((adev->pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
bf44e8ce 3829 vga_client_register(adev->pdev, amdgpu_device_vga_set_decode);
8c3dd61c
KHF
3830
3831 if (amdgpu_device_supports_px(ddev)) {
3832 px = true;
3833 vga_switcheroo_register_client(adev->pdev,
3834 &amdgpu_switcheroo_ops, px);
3835 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
3836 }
3837
e3c1b071 3838 if (adev->gmc.xgmi.pending_reset)
3839 queue_delayed_work(system_wq, &mgpu_info.delayed_reset_work,
3840 msecs_to_jiffies(AMDGPU_RESUME_MS));
3841
4a74c38c
PY
3842 amdgpu_device_check_iommu_direct_map(adev);
3843
d38ceaf9 3844 return 0;
83ba126a 3845
970fd197
SY
3846release_ras_con:
3847 amdgpu_release_ras_context(adev);
3848
83ba126a 3849failed:
89041940 3850 amdgpu_vf_error_trans_all(adev);
8840a387 3851
83ba126a 3852 return r;
d38ceaf9
AD
3853}
3854
07775fc1
AG
3855static void amdgpu_device_unmap_mmio(struct amdgpu_device *adev)
3856{
62d5f9f7 3857
07775fc1
AG
3858 /* Clear all CPU mappings pointing to this device */
3859 unmap_mapping_range(adev->ddev.anon_inode->i_mapping, 0, 0, 1);
3860
3861 /* Unmap all mapped bars - Doorbell, registers and VRAM */
3862 amdgpu_device_doorbell_fini(adev);
3863
3864 iounmap(adev->rmmio);
3865 adev->rmmio = NULL;
3866 if (adev->mman.aper_base_kaddr)
3867 iounmap(adev->mman.aper_base_kaddr);
3868 adev->mman.aper_base_kaddr = NULL;
3869
3870 /* Memory manager related */
3871 if (!adev->gmc.xgmi.connected_to_cpu) {
3872 arch_phys_wc_del(adev->gmc.vram_mtrr);
3873 arch_io_free_memtype_wc(adev->gmc.aper_base, adev->gmc.aper_size);
3874 }
3875}
3876
d38ceaf9 3877/**
bbe04dec 3878 * amdgpu_device_fini_hw - tear down the driver
d38ceaf9
AD
3879 *
3880 * @adev: amdgpu_device pointer
3881 *
3882 * Tear down the driver info (all asics).
3883 * Called at driver shutdown.
3884 */
72c8c97b 3885void amdgpu_device_fini_hw(struct amdgpu_device *adev)
d38ceaf9 3886{
aac89168 3887 dev_info(adev->dev, "amdgpu: finishing device.\n");
9f875167 3888 flush_delayed_work(&adev->delayed_init_work);
691191a2
YW
3889 if (adev->mman.initialized) {
3890 flush_delayed_work(&adev->mman.bdev.wq);
e78b3197 3891 ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
691191a2 3892 }
d0d13fe8 3893 adev->shutdown = true;
9f875167 3894
752c683d
ML
3895 /* make sure IB test finished before entering exclusive mode
3896 * to avoid preemption on IB test
3897 * */
519b8b76 3898 if (amdgpu_sriov_vf(adev)) {
752c683d 3899 amdgpu_virt_request_full_gpu(adev, false);
519b8b76
BZ
3900 amdgpu_virt_fini_data_exchange(adev);
3901 }
752c683d 3902
e5b03032
ML
3903 /* disable all interrupts */
3904 amdgpu_irq_disable_all(adev);
ff97cba8 3905 if (adev->mode_info.mode_config_initialized){
1053b9c9 3906 if (!drm_drv_uses_atomic_modeset(adev_to_drm(adev)))
4a580877 3907 drm_helper_force_disable_all(adev_to_drm(adev));
ff97cba8 3908 else
4a580877 3909 drm_atomic_helper_shutdown(adev_to_drm(adev));
ff97cba8 3910 }
8d35a259 3911 amdgpu_fence_driver_hw_fini(adev);
72c8c97b 3912
7c868b59
YT
3913 if (adev->pm_sysfs_en)
3914 amdgpu_pm_sysfs_fini(adev);
72c8c97b
AG
3915 if (adev->ucode_sysfs_en)
3916 amdgpu_ucode_sysfs_fini(adev);
3917 sysfs_remove_files(&adev->dev->kobj, amdgpu_dev_attributes);
3918
232d1d43
SY
3919 /* disable ras feature must before hw fini */
3920 amdgpu_ras_pre_fini(adev);
3921
e9669fb7 3922 amdgpu_device_ip_fini_early(adev);
d10d0daa 3923
a3848df6
YW
3924 amdgpu_irq_fini_hw(adev);
3925
b6fd6e0f
SK
3926 if (adev->mman.initialized)
3927 ttm_device_clear_dma_mappings(&adev->mman.bdev);
894c6890 3928
d10d0daa 3929 amdgpu_gart_dummy_page_fini(adev);
07775fc1 3930
87172e89
LS
3931 if (drm_dev_is_unplugged(adev_to_drm(adev)))
3932 amdgpu_device_unmap_mmio(adev);
3933
72c8c97b
AG
3934}
3935
3936void amdgpu_device_fini_sw(struct amdgpu_device *adev)
3937{
62d5f9f7
LS
3938 int idx;
3939
8d35a259 3940 amdgpu_fence_driver_sw_fini(adev);
a5c5d8d5 3941 amdgpu_device_ip_fini(adev);
75e1658e
ND
3942 release_firmware(adev->firmware.gpu_info_fw);
3943 adev->firmware.gpu_info_fw = NULL;
d38ceaf9 3944 adev->accel_working = false;
04442bf7
LL
3945
3946 amdgpu_reset_fini(adev);
3947
d38ceaf9 3948 /* free i2c buses */
4562236b
HW
3949 if (!amdgpu_device_has_dc_support(adev))
3950 amdgpu_i2c_fini(adev);
bfca0289
SL
3951
3952 if (amdgpu_emu_mode != 1)
3953 amdgpu_atombios_fini(adev);
3954
d38ceaf9
AD
3955 kfree(adev->bios);
3956 adev->bios = NULL;
b98c6299 3957 if (amdgpu_device_supports_px(adev_to_drm(adev))) {
84c8b22e 3958 vga_switcheroo_unregister_client(adev->pdev);
83ba126a 3959 vga_switcheroo_fini_domain_pm_ops(adev->dev);
b98c6299 3960 }
38d6be81 3961 if ((adev->pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
b8779475 3962 vga_client_unregister(adev->pdev);
e9bc1bf7 3963
62d5f9f7
LS
3964 if (drm_dev_enter(adev_to_drm(adev), &idx)) {
3965
3966 iounmap(adev->rmmio);
3967 adev->rmmio = NULL;
3968 amdgpu_device_doorbell_fini(adev);
3969 drm_dev_exit(idx);
3970 }
3971
d155bef0
AB
3972 if (IS_ENABLED(CONFIG_PERF_EVENTS))
3973 amdgpu_pmu_fini(adev);
72de33f8 3974 if (adev->mman.discovery_bin)
a190d1c7 3975 amdgpu_discovery_fini(adev);
72c8c97b
AG
3976
3977 kfree(adev->pci_state);
3978
d38ceaf9
AD
3979}
3980
58144d28
ND
3981/**
3982 * amdgpu_device_evict_resources - evict device resources
3983 * @adev: amdgpu device object
3984 *
3985 * Evicts all ttm device resources(vram BOs, gart table) from the lru list
3986 * of the vram memory type. Mainly used for evicting device resources
3987 * at suspend time.
3988 *
3989 */
3990static void amdgpu_device_evict_resources(struct amdgpu_device *adev)
3991{
e53d9665
ML
3992 /* No need to evict vram on APUs for suspend to ram or s2idle */
3993 if ((adev->in_s3 || adev->in_s0ix) && (adev->flags & AMD_IS_APU))
58144d28
ND
3994 return;
3995
3996 if (amdgpu_ttm_evict_resources(adev, TTM_PL_VRAM))
3997 DRM_WARN("evicting device resources failed\n");
3998
3999}
d38ceaf9
AD
4000
4001/*
4002 * Suspend & resume.
4003 */
4004/**
810ddc3a 4005 * amdgpu_device_suspend - initiate device suspend
d38ceaf9 4006 *
87e3f136 4007 * @dev: drm dev pointer
87e3f136 4008 * @fbcon : notify the fbdev of suspend
d38ceaf9
AD
4009 *
4010 * Puts the hw in the suspend state (all asics).
4011 * Returns 0 for success or an error on failure.
4012 * Called at driver suspend.
4013 */
de185019 4014int amdgpu_device_suspend(struct drm_device *dev, bool fbcon)
d38ceaf9 4015{
a2e15b0e 4016 struct amdgpu_device *adev = drm_to_adev(dev);
d38ceaf9 4017
d38ceaf9
AD
4018 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
4019 return 0;
4020
44779b43 4021 adev->in_suspend = true;
3fa8f89d
S
4022
4023 if (amdgpu_acpi_smart_shift_update(dev, AMDGPU_SS_DEV_D3))
4024 DRM_WARN("smart shift update failed\n");
4025
d38ceaf9
AD
4026 drm_kms_helper_poll_disable(dev);
4027
5f818173 4028 if (fbcon)
087451f3 4029 drm_fb_helper_set_suspend_unlocked(adev_to_drm(adev)->fb_helper, true);
5f818173 4030
beff74bc 4031 cancel_delayed_work_sync(&adev->delayed_init_work);
a5459475 4032
5e6932fe 4033 amdgpu_ras_suspend(adev);
4034
2196927b 4035 amdgpu_device_ip_suspend_phase1(adev);
fe1053b7 4036
5d3a2d95
AD
4037 if (!adev->in_s0ix)
4038 amdgpu_amdkfd_suspend(adev, adev->in_runpm);
94fa5660 4039
58144d28 4040 amdgpu_device_evict_resources(adev);
d38ceaf9 4041
8d35a259 4042 amdgpu_fence_driver_hw_fini(adev);
d38ceaf9 4043
2196927b 4044 amdgpu_device_ip_suspend_phase2(adev);
d38ceaf9 4045
d38ceaf9
AD
4046 return 0;
4047}
4048
4049/**
810ddc3a 4050 * amdgpu_device_resume - initiate device resume
d38ceaf9 4051 *
87e3f136 4052 * @dev: drm dev pointer
87e3f136 4053 * @fbcon : notify the fbdev of resume
d38ceaf9
AD
4054 *
4055 * Bring the hw back to operating state (all asics).
4056 * Returns 0 for success or an error on failure.
4057 * Called at driver resume.
4058 */
de185019 4059int amdgpu_device_resume(struct drm_device *dev, bool fbcon)
d38ceaf9 4060{
1348969a 4061 struct amdgpu_device *adev = drm_to_adev(dev);
03161a6e 4062 int r = 0;
d38ceaf9
AD
4063
4064 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
4065 return 0;
4066
62498733 4067 if (adev->in_s0ix)
bc143d8b 4068 amdgpu_dpm_gfx_state_change(adev, sGpuChangeState_D0Entry);
628c36d7 4069
d38ceaf9 4070 /* post card */
39c640c0 4071 if (amdgpu_device_need_post(adev)) {
4d2997ab 4072 r = amdgpu_device_asic_init(adev);
74b0b157 4073 if (r)
aac89168 4074 dev_err(adev->dev, "amdgpu asic init failed\n");
74b0b157 4075 }
d38ceaf9 4076
06ec9070 4077 r = amdgpu_device_ip_resume(adev);
e6707218 4078 if (r) {
aac89168 4079 dev_err(adev->dev, "amdgpu_device_ip_resume failed (%d).\n", r);
4d3b9ae5 4080 return r;
e6707218 4081 }
8d35a259 4082 amdgpu_fence_driver_hw_init(adev);
5ceb54c6 4083
06ec9070 4084 r = amdgpu_device_ip_late_init(adev);
03161a6e 4085 if (r)
4d3b9ae5 4086 return r;
d38ceaf9 4087
beff74bc
AD
4088 queue_delayed_work(system_wq, &adev->delayed_init_work,
4089 msecs_to_jiffies(AMDGPU_RESUME_MS));
4090
5d3a2d95
AD
4091 if (!adev->in_s0ix) {
4092 r = amdgpu_amdkfd_resume(adev, adev->in_runpm);
4093 if (r)
4094 return r;
4095 }
756e6880 4096
96a5d8d4 4097 /* Make sure IB tests flushed */
beff74bc 4098 flush_delayed_work(&adev->delayed_init_work);
96a5d8d4 4099
a2e15b0e 4100 if (fbcon)
087451f3 4101 drm_fb_helper_set_suspend_unlocked(adev_to_drm(adev)->fb_helper, false);
d38ceaf9
AD
4102
4103 drm_kms_helper_poll_enable(dev);
23a1a9e5 4104
5e6932fe 4105 amdgpu_ras_resume(adev);
4106
23a1a9e5
L
4107 /*
4108 * Most of the connector probing functions try to acquire runtime pm
4109 * refs to ensure that the GPU is powered on when connector polling is
4110 * performed. Since we're calling this from a runtime PM callback,
4111 * trying to acquire rpm refs will cause us to deadlock.
4112 *
4113 * Since we're guaranteed to be holding the rpm lock, it's safe to
4114 * temporarily disable the rpm helpers so this doesn't deadlock us.
4115 */
4116#ifdef CONFIG_PM
4117 dev->dev->power.disable_depth++;
4118#endif
4562236b
HW
4119 if (!amdgpu_device_has_dc_support(adev))
4120 drm_helper_hpd_irq_event(dev);
4121 else
4122 drm_kms_helper_hotplug_event(dev);
23a1a9e5
L
4123#ifdef CONFIG_PM
4124 dev->dev->power.disable_depth--;
4125#endif
44779b43
RZ
4126 adev->in_suspend = false;
4127
3fa8f89d
S
4128 if (amdgpu_acpi_smart_shift_update(dev, AMDGPU_SS_DEV_D0))
4129 DRM_WARN("smart shift update failed\n");
4130
4d3b9ae5 4131 return 0;
d38ceaf9
AD
4132}
4133
e3ecdffa
AD
4134/**
4135 * amdgpu_device_ip_check_soft_reset - did soft reset succeed
4136 *
4137 * @adev: amdgpu_device pointer
4138 *
4139 * The list of all the hardware IPs that make up the asic is walked and
4140 * the check_soft_reset callbacks are run. check_soft_reset determines
4141 * if the asic is still hung or not.
4142 * Returns true if any of the IPs are still in a hung state, false if not.
4143 */
06ec9070 4144static bool amdgpu_device_ip_check_soft_reset(struct amdgpu_device *adev)
63fbf42f
CZ
4145{
4146 int i;
4147 bool asic_hang = false;
4148
f993d628
ML
4149 if (amdgpu_sriov_vf(adev))
4150 return true;
4151
8bc04c29
AD
4152 if (amdgpu_asic_need_full_reset(adev))
4153 return true;
4154
63fbf42f 4155 for (i = 0; i < adev->num_ip_blocks; i++) {
a1255107 4156 if (!adev->ip_blocks[i].status.valid)
63fbf42f 4157 continue;
a1255107
AD
4158 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
4159 adev->ip_blocks[i].status.hang =
4160 adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
4161 if (adev->ip_blocks[i].status.hang) {
aac89168 4162 dev_info(adev->dev, "IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
63fbf42f
CZ
4163 asic_hang = true;
4164 }
4165 }
4166 return asic_hang;
4167}
4168
e3ecdffa
AD
4169/**
4170 * amdgpu_device_ip_pre_soft_reset - prepare for soft reset
4171 *
4172 * @adev: amdgpu_device pointer
4173 *
4174 * The list of all the hardware IPs that make up the asic is walked and the
4175 * pre_soft_reset callbacks are run if the block is hung. pre_soft_reset
4176 * handles any IP specific hardware or software state changes that are
4177 * necessary for a soft reset to succeed.
4178 * Returns 0 on success, negative error code on failure.
4179 */
06ec9070 4180static int amdgpu_device_ip_pre_soft_reset(struct amdgpu_device *adev)
d31a501e
CZ
4181{
4182 int i, r = 0;
4183
4184 for (i = 0; i < adev->num_ip_blocks; i++) {
a1255107 4185 if (!adev->ip_blocks[i].status.valid)
d31a501e 4186 continue;
a1255107
AD
4187 if (adev->ip_blocks[i].status.hang &&
4188 adev->ip_blocks[i].version->funcs->pre_soft_reset) {
4189 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
d31a501e
CZ
4190 if (r)
4191 return r;
4192 }
4193 }
4194
4195 return 0;
4196}
4197
e3ecdffa
AD
4198/**
4199 * amdgpu_device_ip_need_full_reset - check if a full asic reset is needed
4200 *
4201 * @adev: amdgpu_device pointer
4202 *
4203 * Some hardware IPs cannot be soft reset. If they are hung, a full gpu
4204 * reset is necessary to recover.
4205 * Returns true if a full asic reset is required, false if not.
4206 */
06ec9070 4207static bool amdgpu_device_ip_need_full_reset(struct amdgpu_device *adev)
35d782fe 4208{
da146d3b
AD
4209 int i;
4210
8bc04c29
AD
4211 if (amdgpu_asic_need_full_reset(adev))
4212 return true;
4213
da146d3b 4214 for (i = 0; i < adev->num_ip_blocks; i++) {
a1255107 4215 if (!adev->ip_blocks[i].status.valid)
da146d3b 4216 continue;
a1255107
AD
4217 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
4218 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
4219 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
98512bb8
KW
4220 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) ||
4221 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
a1255107 4222 if (adev->ip_blocks[i].status.hang) {
aac89168 4223 dev_info(adev->dev, "Some block need full reset!\n");
da146d3b
AD
4224 return true;
4225 }
4226 }
35d782fe
CZ
4227 }
4228 return false;
4229}
4230
e3ecdffa
AD
4231/**
4232 * amdgpu_device_ip_soft_reset - do a soft reset
4233 *
4234 * @adev: amdgpu_device pointer
4235 *
4236 * The list of all the hardware IPs that make up the asic is walked and the
4237 * soft_reset callbacks are run if the block is hung. soft_reset handles any
4238 * IP specific hardware or software state changes that are necessary to soft
4239 * reset the IP.
4240 * Returns 0 on success, negative error code on failure.
4241 */
06ec9070 4242static int amdgpu_device_ip_soft_reset(struct amdgpu_device *adev)
35d782fe
CZ
4243{
4244 int i, r = 0;
4245
4246 for (i = 0; i < adev->num_ip_blocks; i++) {
a1255107 4247 if (!adev->ip_blocks[i].status.valid)
35d782fe 4248 continue;
a1255107
AD
4249 if (adev->ip_blocks[i].status.hang &&
4250 adev->ip_blocks[i].version->funcs->soft_reset) {
4251 r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
35d782fe
CZ
4252 if (r)
4253 return r;
4254 }
4255 }
4256
4257 return 0;
4258}
4259
e3ecdffa
AD
4260/**
4261 * amdgpu_device_ip_post_soft_reset - clean up from soft reset
4262 *
4263 * @adev: amdgpu_device pointer
4264 *
4265 * The list of all the hardware IPs that make up the asic is walked and the
4266 * post_soft_reset callbacks are run if the asic was hung. post_soft_reset
4267 * handles any IP specific hardware or software state changes that are
4268 * necessary after the IP has been soft reset.
4269 * Returns 0 on success, negative error code on failure.
4270 */
06ec9070 4271static int amdgpu_device_ip_post_soft_reset(struct amdgpu_device *adev)
35d782fe
CZ
4272{
4273 int i, r = 0;
4274
4275 for (i = 0; i < adev->num_ip_blocks; i++) {
a1255107 4276 if (!adev->ip_blocks[i].status.valid)
35d782fe 4277 continue;
a1255107
AD
4278 if (adev->ip_blocks[i].status.hang &&
4279 adev->ip_blocks[i].version->funcs->post_soft_reset)
4280 r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
35d782fe
CZ
4281 if (r)
4282 return r;
4283 }
4284
4285 return 0;
4286}
4287
e3ecdffa 4288/**
c33adbc7 4289 * amdgpu_device_recover_vram - Recover some VRAM contents
e3ecdffa
AD
4290 *
4291 * @adev: amdgpu_device pointer
4292 *
4293 * Restores the contents of VRAM buffers from the shadows in GTT. Used to
4294 * restore things like GPUVM page tables after a GPU reset where
4295 * the contents of VRAM might be lost.
403009bf
CK
4296 *
4297 * Returns:
4298 * 0 on success, negative error code on failure.
e3ecdffa 4299 */
c33adbc7 4300static int amdgpu_device_recover_vram(struct amdgpu_device *adev)
c41d1cf6 4301{
c41d1cf6 4302 struct dma_fence *fence = NULL, *next = NULL;
403009bf 4303 struct amdgpu_bo *shadow;
e18aaea7 4304 struct amdgpu_bo_vm *vmbo;
403009bf 4305 long r = 1, tmo;
c41d1cf6
ML
4306
4307 if (amdgpu_sriov_runtime(adev))
b045d3af 4308 tmo = msecs_to_jiffies(8000);
c41d1cf6
ML
4309 else
4310 tmo = msecs_to_jiffies(100);
4311
aac89168 4312 dev_info(adev->dev, "recover vram bo from shadow start\n");
c41d1cf6 4313 mutex_lock(&adev->shadow_list_lock);
e18aaea7
ND
4314 list_for_each_entry(vmbo, &adev->shadow_list, shadow_list) {
4315 shadow = &vmbo->bo;
403009bf 4316 /* No need to recover an evicted BO */
d3116756
CK
4317 if (shadow->tbo.resource->mem_type != TTM_PL_TT ||
4318 shadow->tbo.resource->start == AMDGPU_BO_INVALID_OFFSET ||
4319 shadow->parent->tbo.resource->mem_type != TTM_PL_VRAM)
403009bf
CK
4320 continue;
4321
4322 r = amdgpu_bo_restore_shadow(shadow, &next);
4323 if (r)
4324 break;
4325
c41d1cf6 4326 if (fence) {
1712fb1a 4327 tmo = dma_fence_wait_timeout(fence, false, tmo);
403009bf
CK
4328 dma_fence_put(fence);
4329 fence = next;
1712fb1a 4330 if (tmo == 0) {
4331 r = -ETIMEDOUT;
c41d1cf6 4332 break;
1712fb1a 4333 } else if (tmo < 0) {
4334 r = tmo;
4335 break;
4336 }
403009bf
CK
4337 } else {
4338 fence = next;
c41d1cf6 4339 }
c41d1cf6
ML
4340 }
4341 mutex_unlock(&adev->shadow_list_lock);
4342
403009bf
CK
4343 if (fence)
4344 tmo = dma_fence_wait_timeout(fence, false, tmo);
c41d1cf6
ML
4345 dma_fence_put(fence);
4346
1712fb1a 4347 if (r < 0 || tmo <= 0) {
aac89168 4348 dev_err(adev->dev, "recover vram bo from shadow failed, r is %ld, tmo is %ld\n", r, tmo);
403009bf
CK
4349 return -EIO;
4350 }
c41d1cf6 4351
aac89168 4352 dev_info(adev->dev, "recover vram bo from shadow done\n");
403009bf 4353 return 0;
c41d1cf6
ML
4354}
4355
a90ad3c2 4356
e3ecdffa 4357/**
06ec9070 4358 * amdgpu_device_reset_sriov - reset ASIC for SR-IOV vf
5740682e 4359 *
982a820b 4360 * @adev: amdgpu_device pointer
87e3f136 4361 * @from_hypervisor: request from hypervisor
5740682e
ML
4362 *
4363 * do VF FLR and reinitialize Asic
3f48c681 4364 * return 0 means succeeded otherwise failed
e3ecdffa
AD
4365 */
4366static int amdgpu_device_reset_sriov(struct amdgpu_device *adev,
4367 bool from_hypervisor)
5740682e
ML
4368{
4369 int r;
a5f67c93 4370 struct amdgpu_hive_info *hive = NULL;
7258fa31 4371 int retry_limit = 0;
5740682e 4372
7258fa31 4373retry:
992110d7 4374 amdgpu_amdkfd_pre_reset(adev);
5740682e 4375
428890a3 4376 amdgpu_amdkfd_pre_reset(adev);
4377
5740682e
ML
4378 if (from_hypervisor)
4379 r = amdgpu_virt_request_full_gpu(adev, true);
4380 else
4381 r = amdgpu_virt_reset_gpu(adev);
4382 if (r)
4383 return r;
a90ad3c2
ML
4384
4385 /* Resume IP prior to SMC */
06ec9070 4386 r = amdgpu_device_ip_reinit_early_sriov(adev);
5740682e
ML
4387 if (r)
4388 goto error;
a90ad3c2 4389
c9ffa427 4390 amdgpu_virt_init_data_exchange(adev);
a90ad3c2 4391
7a3e0bb2
RZ
4392 r = amdgpu_device_fw_loading(adev);
4393 if (r)
4394 return r;
4395
a90ad3c2 4396 /* now we are okay to resume SMC/CP/SDMA */
06ec9070 4397 r = amdgpu_device_ip_reinit_late_sriov(adev);
5740682e
ML
4398 if (r)
4399 goto error;
a90ad3c2 4400
a5f67c93
ZL
4401 hive = amdgpu_get_xgmi_hive(adev);
4402 /* Update PSP FW topology after reset */
4403 if (hive && adev->gmc.xgmi.num_physical_nodes > 1)
4404 r = amdgpu_xgmi_update_topology(hive, adev);
4405
4406 if (hive)
4407 amdgpu_put_xgmi_hive(hive);
4408
4409 if (!r) {
4410 amdgpu_irq_gpu_reset_resume_helper(adev);
4411 r = amdgpu_ib_ring_tests(adev);
4412 amdgpu_amdkfd_post_reset(adev);
4413 }
a90ad3c2 4414
abc34253 4415error:
c41d1cf6 4416 if (!r && adev->virt.gim_feature & AMDGIM_FEATURE_GIM_FLR_VRAMLOST) {
e3526257 4417 amdgpu_inc_vram_lost(adev);
c33adbc7 4418 r = amdgpu_device_recover_vram(adev);
a90ad3c2 4419 }
437f3e0b 4420 amdgpu_virt_release_full_gpu(adev, true);
a90ad3c2 4421
7258fa31
SK
4422 if (AMDGPU_RETRY_SRIOV_RESET(r)) {
4423 if (retry_limit < AMDGPU_MAX_RETRY_LIMIT) {
4424 retry_limit++;
4425 goto retry;
4426 } else
4427 DRM_ERROR("GPU reset retry is beyond the retry limit\n");
4428 }
4429
a90ad3c2
ML
4430 return r;
4431}
4432
9a1cddd6 4433/**
4434 * amdgpu_device_has_job_running - check if there is any job in mirror list
4435 *
982a820b 4436 * @adev: amdgpu_device pointer
9a1cddd6 4437 *
4438 * check if there is any job in mirror list
4439 */
4440bool amdgpu_device_has_job_running(struct amdgpu_device *adev)
4441{
4442 int i;
4443 struct drm_sched_job *job;
4444
4445 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
4446 struct amdgpu_ring *ring = adev->rings[i];
4447
4448 if (!ring || !ring->sched.thread)
4449 continue;
4450
4451 spin_lock(&ring->sched.job_list_lock);
6efa4b46
LT
4452 job = list_first_entry_or_null(&ring->sched.pending_list,
4453 struct drm_sched_job, list);
9a1cddd6 4454 spin_unlock(&ring->sched.job_list_lock);
4455 if (job)
4456 return true;
4457 }
4458 return false;
4459}
4460
12938fad
CK
4461/**
4462 * amdgpu_device_should_recover_gpu - check if we should try GPU recovery
4463 *
982a820b 4464 * @adev: amdgpu_device pointer
12938fad
CK
4465 *
4466 * Check amdgpu_gpu_recovery and SRIOV status to see if we should try to recover
4467 * a hung GPU.
4468 */
4469bool amdgpu_device_should_recover_gpu(struct amdgpu_device *adev)
4470{
4471 if (!amdgpu_device_ip_check_soft_reset(adev)) {
aac89168 4472 dev_info(adev->dev, "Timeout, but no hardware hang detected.\n");
12938fad
CK
4473 return false;
4474 }
4475
3ba7b418
AG
4476 if (amdgpu_gpu_recovery == 0)
4477 goto disabled;
4478
4479 if (amdgpu_sriov_vf(adev))
4480 return true;
4481
4482 if (amdgpu_gpu_recovery == -1) {
4483 switch (adev->asic_type) {
b3523c45
AD
4484#ifdef CONFIG_DRM_AMDGPU_SI
4485 case CHIP_VERDE:
4486 case CHIP_TAHITI:
4487 case CHIP_PITCAIRN:
4488 case CHIP_OLAND:
4489 case CHIP_HAINAN:
4490#endif
4491#ifdef CONFIG_DRM_AMDGPU_CIK
4492 case CHIP_KAVERI:
4493 case CHIP_KABINI:
4494 case CHIP_MULLINS:
4495#endif
4496 case CHIP_CARRIZO:
4497 case CHIP_STONEY:
4498 case CHIP_CYAN_SKILLFISH:
3ba7b418 4499 goto disabled;
b3523c45
AD
4500 default:
4501 break;
3ba7b418 4502 }
12938fad
CK
4503 }
4504
4505 return true;
3ba7b418
AG
4506
4507disabled:
aac89168 4508 dev_info(adev->dev, "GPU recovery disabled.\n");
3ba7b418 4509 return false;
12938fad
CK
4510}
4511
5c03e584
FX
4512int amdgpu_device_mode1_reset(struct amdgpu_device *adev)
4513{
4514 u32 i;
4515 int ret = 0;
4516
4517 amdgpu_atombios_scratch_regs_engine_hung(adev, true);
4518
4519 dev_info(adev->dev, "GPU mode1 reset\n");
4520
4521 /* disable BM */
4522 pci_clear_master(adev->pdev);
4523
4524 amdgpu_device_cache_pci_state(adev->pdev);
4525
4526 if (amdgpu_dpm_is_mode1_reset_supported(adev)) {
4527 dev_info(adev->dev, "GPU smu mode1 reset\n");
4528 ret = amdgpu_dpm_mode1_reset(adev);
4529 } else {
4530 dev_info(adev->dev, "GPU psp mode1 reset\n");
4531 ret = psp_gpu_reset(adev);
4532 }
4533
4534 if (ret)
4535 dev_err(adev->dev, "GPU mode1 reset failed\n");
4536
4537 amdgpu_device_load_pci_state(adev->pdev);
4538
4539 /* wait for asic to come out of reset */
4540 for (i = 0; i < adev->usec_timeout; i++) {
4541 u32 memsize = adev->nbio.funcs->get_memsize(adev);
4542
4543 if (memsize != 0xffffffff)
4544 break;
4545 udelay(1);
4546 }
4547
4548 amdgpu_atombios_scratch_regs_engine_hung(adev, false);
4549 return ret;
4550}
5c6dd71e 4551
e3c1b071 4552int amdgpu_device_pre_asic_reset(struct amdgpu_device *adev,
04442bf7 4553 struct amdgpu_reset_context *reset_context)
26bc5340 4554{
5c1e6fa4 4555 int i, r = 0;
04442bf7
LL
4556 struct amdgpu_job *job = NULL;
4557 bool need_full_reset =
4558 test_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
4559
4560 if (reset_context->reset_req_dev == adev)
4561 job = reset_context->job;
71182665 4562
b602ca5f
TZ
4563 if (amdgpu_sriov_vf(adev)) {
4564 /* stop the data exchange thread */
4565 amdgpu_virt_fini_data_exchange(adev);
4566 }
4567
71182665 4568 /* block all schedulers and reset given job's ring */
0875dc9e
CZ
4569 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
4570 struct amdgpu_ring *ring = adev->rings[i];
4571
51687759 4572 if (!ring || !ring->sched.thread)
0875dc9e 4573 continue;
5740682e 4574
c530b02f
JZ
4575 /*clear job fence from fence drv to avoid force_completion
4576 *leave NULL and vm flush fence in fence drv */
5c1e6fa4 4577 amdgpu_fence_driver_clear_job_fences(ring);
c530b02f 4578
2f9d4084
ML
4579 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
4580 amdgpu_fence_driver_force_completion(ring);
0875dc9e 4581 }
d38ceaf9 4582
ff99849b 4583 if (job && job->vm)
222b5f04
AG
4584 drm_sched_increase_karma(&job->base);
4585
04442bf7 4586 r = amdgpu_reset_prepare_hwcontext(adev, reset_context);
404b277b
LL
4587 /* If reset handler not implemented, continue; otherwise return */
4588 if (r == -ENOSYS)
4589 r = 0;
4590 else
04442bf7
LL
4591 return r;
4592
1d721ed6 4593 /* Don't suspend on bare metal if we are not going to HW reset the ASIC */
26bc5340
AG
4594 if (!amdgpu_sriov_vf(adev)) {
4595
4596 if (!need_full_reset)
4597 need_full_reset = amdgpu_device_ip_need_full_reset(adev);
4598
4599 if (!need_full_reset) {
4600 amdgpu_device_ip_pre_soft_reset(adev);
4601 r = amdgpu_device_ip_soft_reset(adev);
4602 amdgpu_device_ip_post_soft_reset(adev);
4603 if (r || amdgpu_device_ip_check_soft_reset(adev)) {
aac89168 4604 dev_info(adev->dev, "soft reset failed, will fallback to full reset!\n");
26bc5340
AG
4605 need_full_reset = true;
4606 }
4607 }
4608
4609 if (need_full_reset)
4610 r = amdgpu_device_ip_suspend(adev);
04442bf7
LL
4611 if (need_full_reset)
4612 set_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
4613 else
4614 clear_bit(AMDGPU_NEED_FULL_RESET,
4615 &reset_context->flags);
26bc5340
AG
4616 }
4617
4618 return r;
4619}
4620
04442bf7
LL
4621int amdgpu_do_asic_reset(struct list_head *device_list_handle,
4622 struct amdgpu_reset_context *reset_context)
26bc5340
AG
4623{
4624 struct amdgpu_device *tmp_adev = NULL;
04442bf7 4625 bool need_full_reset, skip_hw_reset, vram_lost = false;
26bc5340
AG
4626 int r = 0;
4627
04442bf7
LL
4628 /* Try reset handler method first */
4629 tmp_adev = list_first_entry(device_list_handle, struct amdgpu_device,
4630 reset_list);
4631 r = amdgpu_reset_perform_reset(tmp_adev, reset_context);
404b277b
LL
4632 /* If reset handler not implemented, continue; otherwise return */
4633 if (r == -ENOSYS)
4634 r = 0;
4635 else
04442bf7
LL
4636 return r;
4637
4638 /* Reset handler not implemented, use the default method */
4639 need_full_reset =
4640 test_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
4641 skip_hw_reset = test_bit(AMDGPU_SKIP_HW_RESET, &reset_context->flags);
4642
26bc5340 4643 /*
655ce9cb 4644 * ASIC reset has to be done on all XGMI hive nodes ASAP
26bc5340
AG
4645 * to allow proper links negotiation in FW (within 1 sec)
4646 */
7ac71382 4647 if (!skip_hw_reset && need_full_reset) {
655ce9cb 4648 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
041a62bc 4649 /* For XGMI run all resets in parallel to speed up the process */
d4535e2c 4650 if (tmp_adev->gmc.xgmi.num_physical_nodes > 1) {
e3c1b071 4651 tmp_adev->gmc.xgmi.pending_reset = false;
c96cf282 4652 if (!queue_work(system_unbound_wq, &tmp_adev->xgmi_reset_work))
d4535e2c
AG
4653 r = -EALREADY;
4654 } else
4655 r = amdgpu_asic_reset(tmp_adev);
d4535e2c 4656
041a62bc 4657 if (r) {
aac89168 4658 dev_err(tmp_adev->dev, "ASIC reset failed with error, %d for drm dev, %s",
4a580877 4659 r, adev_to_drm(tmp_adev)->unique);
041a62bc 4660 break;
ce316fa5
LM
4661 }
4662 }
4663
041a62bc
AG
4664 /* For XGMI wait for all resets to complete before proceed */
4665 if (!r) {
655ce9cb 4666 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
ce316fa5
LM
4667 if (tmp_adev->gmc.xgmi.num_physical_nodes > 1) {
4668 flush_work(&tmp_adev->xgmi_reset_work);
4669 r = tmp_adev->asic_reset_res;
4670 if (r)
4671 break;
ce316fa5
LM
4672 }
4673 }
4674 }
ce316fa5 4675 }
26bc5340 4676
43c4d576 4677 if (!r && amdgpu_ras_intr_triggered()) {
655ce9cb 4678 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
5e67bba3 4679 if (tmp_adev->mmhub.ras && tmp_adev->mmhub.ras->ras_block.hw_ops &&
4680 tmp_adev->mmhub.ras->ras_block.hw_ops->reset_ras_error_count)
4681 tmp_adev->mmhub.ras->ras_block.hw_ops->reset_ras_error_count(tmp_adev);
43c4d576
JC
4682 }
4683
00eaa571 4684 amdgpu_ras_intr_cleared();
43c4d576 4685 }
00eaa571 4686
655ce9cb 4687 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
26bc5340
AG
4688 if (need_full_reset) {
4689 /* post card */
e3c1b071 4690 r = amdgpu_device_asic_init(tmp_adev);
4691 if (r) {
aac89168 4692 dev_warn(tmp_adev->dev, "asic atom init failed!");
e3c1b071 4693 } else {
26bc5340 4694 dev_info(tmp_adev->dev, "GPU reset succeeded, trying to resume\n");
9cec53c1
JZ
4695 r = amdgpu_amdkfd_resume_iommu(tmp_adev);
4696 if (r)
4697 goto out;
4698
26bc5340
AG
4699 r = amdgpu_device_ip_resume_phase1(tmp_adev);
4700 if (r)
4701 goto out;
4702
4703 vram_lost = amdgpu_device_check_vram_lost(tmp_adev);
4704 if (vram_lost) {
77e7f829 4705 DRM_INFO("VRAM is lost due to GPU reset!\n");
e3526257 4706 amdgpu_inc_vram_lost(tmp_adev);
26bc5340
AG
4707 }
4708
26bc5340
AG
4709 r = amdgpu_device_fw_loading(tmp_adev);
4710 if (r)
4711 return r;
4712
4713 r = amdgpu_device_ip_resume_phase2(tmp_adev);
4714 if (r)
4715 goto out;
4716
4717 if (vram_lost)
4718 amdgpu_device_fill_reset_magic(tmp_adev);
4719
fdafb359
EQ
4720 /*
4721 * Add this ASIC as tracked as reset was already
4722 * complete successfully.
4723 */
4724 amdgpu_register_gpu_instance(tmp_adev);
4725
04442bf7
LL
4726 if (!reset_context->hive &&
4727 tmp_adev->gmc.xgmi.num_physical_nodes > 1)
e3c1b071 4728 amdgpu_xgmi_add_device(tmp_adev);
4729
7c04ca50 4730 r = amdgpu_device_ip_late_init(tmp_adev);
4731 if (r)
4732 goto out;
4733
087451f3 4734 drm_fb_helper_set_suspend_unlocked(adev_to_drm(tmp_adev)->fb_helper, false);
565d1941 4735
e8fbaf03
GC
4736 /*
4737 * The GPU enters bad state once faulty pages
4738 * by ECC has reached the threshold, and ras
4739 * recovery is scheduled next. So add one check
4740 * here to break recovery if it indeed exceeds
4741 * bad page threshold, and remind user to
4742 * retire this GPU or setting one bigger
4743 * bad_page_threshold value to fix this once
4744 * probing driver again.
4745 */
11003c68 4746 if (!amdgpu_ras_eeprom_check_err_threshold(tmp_adev)) {
e8fbaf03
GC
4747 /* must succeed. */
4748 amdgpu_ras_resume(tmp_adev);
4749 } else {
4750 r = -EINVAL;
4751 goto out;
4752 }
e79a04d5 4753
26bc5340 4754 /* Update PSP FW topology after reset */
04442bf7
LL
4755 if (reset_context->hive &&
4756 tmp_adev->gmc.xgmi.num_physical_nodes > 1)
4757 r = amdgpu_xgmi_update_topology(
4758 reset_context->hive, tmp_adev);
26bc5340
AG
4759 }
4760 }
4761
26bc5340
AG
4762out:
4763 if (!r) {
4764 amdgpu_irq_gpu_reset_resume_helper(tmp_adev);
4765 r = amdgpu_ib_ring_tests(tmp_adev);
4766 if (r) {
4767 dev_err(tmp_adev->dev, "ib ring test failed (%d).\n", r);
26bc5340
AG
4768 need_full_reset = true;
4769 r = -EAGAIN;
4770 goto end;
4771 }
4772 }
4773
4774 if (!r)
4775 r = amdgpu_device_recover_vram(tmp_adev);
4776 else
4777 tmp_adev->asic_reset_res = r;
4778 }
4779
4780end:
04442bf7
LL
4781 if (need_full_reset)
4782 set_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
4783 else
4784 clear_bit(AMDGPU_NEED_FULL_RESET, &reset_context->flags);
26bc5340
AG
4785 return r;
4786}
4787
08ebb485
DL
4788static bool amdgpu_device_lock_adev(struct amdgpu_device *adev,
4789 struct amdgpu_hive_info *hive)
26bc5340 4790{
53b3f8f4
DL
4791 if (atomic_cmpxchg(&adev->in_gpu_reset, 0, 1) != 0)
4792 return false;
4793
08ebb485
DL
4794 if (hive) {
4795 down_write_nest_lock(&adev->reset_sem, &hive->hive_lock);
4796 } else {
4797 down_write(&adev->reset_sem);
4798 }
5740682e 4799
a3a09142
AD
4800 switch (amdgpu_asic_reset_method(adev)) {
4801 case AMD_RESET_METHOD_MODE1:
4802 adev->mp1_state = PP_MP1_STATE_SHUTDOWN;
4803 break;
4804 case AMD_RESET_METHOD_MODE2:
4805 adev->mp1_state = PP_MP1_STATE_RESET;
4806 break;
4807 default:
4808 adev->mp1_state = PP_MP1_STATE_NONE;
4809 break;
4810 }
1d721ed6
AG
4811
4812 return true;
26bc5340 4813}
d38ceaf9 4814
26bc5340
AG
4815static void amdgpu_device_unlock_adev(struct amdgpu_device *adev)
4816{
89041940 4817 amdgpu_vf_error_trans_all(adev);
a3a09142 4818 adev->mp1_state = PP_MP1_STATE_NONE;
53b3f8f4 4819 atomic_set(&adev->in_gpu_reset, 0);
6049db43 4820 up_write(&adev->reset_sem);
26bc5340
AG
4821}
4822
91fb309d
HC
4823/*
4824 * to lockup a list of amdgpu devices in a hive safely, if not a hive
4825 * with multiple nodes, it will be similar as amdgpu_device_lock_adev.
4826 *
4827 * unlock won't require roll back.
4828 */
4829static int amdgpu_device_lock_hive_adev(struct amdgpu_device *adev, struct amdgpu_hive_info *hive)
4830{
4831 struct amdgpu_device *tmp_adev = NULL;
4832
175ac6ec 4833 if (!amdgpu_sriov_vf(adev) && (adev->gmc.xgmi.num_physical_nodes > 1)) {
91fb309d
HC
4834 if (!hive) {
4835 dev_err(adev->dev, "Hive is NULL while device has multiple xgmi nodes");
4836 return -ENODEV;
4837 }
4838 list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head) {
4839 if (!amdgpu_device_lock_adev(tmp_adev, hive))
4840 goto roll_back;
4841 }
4842 } else if (!amdgpu_device_lock_adev(adev, hive))
4843 return -EAGAIN;
4844
4845 return 0;
4846roll_back:
4847 if (!list_is_first(&tmp_adev->gmc.xgmi.head, &hive->device_list)) {
4848 /*
4849 * if the lockup iteration break in the middle of a hive,
4850 * it may means there may has a race issue,
4851 * or a hive device locked up independently.
4852 * we may be in trouble and may not, so will try to roll back
4853 * the lock and give out a warnning.
4854 */
4855 dev_warn(tmp_adev->dev, "Hive lock iteration broke in the middle. Rolling back to unlock");
4856 list_for_each_entry_continue_reverse(tmp_adev, &hive->device_list, gmc.xgmi.head) {
4857 amdgpu_device_unlock_adev(tmp_adev);
4858 }
4859 }
4860 return -EAGAIN;
4861}
4862
3f12acc8
EQ
4863static void amdgpu_device_resume_display_audio(struct amdgpu_device *adev)
4864{
4865 struct pci_dev *p = NULL;
4866
4867 p = pci_get_domain_bus_and_slot(pci_domain_nr(adev->pdev->bus),
4868 adev->pdev->bus->number, 1);
4869 if (p) {
4870 pm_runtime_enable(&(p->dev));
4871 pm_runtime_resume(&(p->dev));
4872 }
4873}
4874
4875static int amdgpu_device_suspend_display_audio(struct amdgpu_device *adev)
4876{
4877 enum amd_reset_method reset_method;
4878 struct pci_dev *p = NULL;
4879 u64 expires;
4880
4881 /*
4882 * For now, only BACO and mode1 reset are confirmed
4883 * to suffer the audio issue without proper suspended.
4884 */
4885 reset_method = amdgpu_asic_reset_method(adev);
4886 if ((reset_method != AMD_RESET_METHOD_BACO) &&
4887 (reset_method != AMD_RESET_METHOD_MODE1))
4888 return -EINVAL;
4889
4890 p = pci_get_domain_bus_and_slot(pci_domain_nr(adev->pdev->bus),
4891 adev->pdev->bus->number, 1);
4892 if (!p)
4893 return -ENODEV;
4894
4895 expires = pm_runtime_autosuspend_expiration(&(p->dev));
4896 if (!expires)
4897 /*
4898 * If we cannot get the audio device autosuspend delay,
4899 * a fixed 4S interval will be used. Considering 3S is
4900 * the audio controller default autosuspend delay setting.
4901 * 4S used here is guaranteed to cover that.
4902 */
54b7feb9 4903 expires = ktime_get_mono_fast_ns() + NSEC_PER_SEC * 4ULL;
3f12acc8
EQ
4904
4905 while (!pm_runtime_status_suspended(&(p->dev))) {
4906 if (!pm_runtime_suspend(&(p->dev)))
4907 break;
4908
4909 if (expires < ktime_get_mono_fast_ns()) {
4910 dev_warn(adev->dev, "failed to suspend display audio\n");
4911 /* TODO: abort the succeeding gpu reset? */
4912 return -ETIMEDOUT;
4913 }
4914 }
4915
4916 pm_runtime_disable(&(p->dev));
4917
4918 return 0;
4919}
4920
9d8d96be 4921static void amdgpu_device_recheck_guilty_jobs(
04442bf7
LL
4922 struct amdgpu_device *adev, struct list_head *device_list_handle,
4923 struct amdgpu_reset_context *reset_context)
e6c6338f
JZ
4924{
4925 int i, r = 0;
4926
4927 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
4928 struct amdgpu_ring *ring = adev->rings[i];
4929 int ret = 0;
4930 struct drm_sched_job *s_job;
4931
4932 if (!ring || !ring->sched.thread)
4933 continue;
4934
4935 s_job = list_first_entry_or_null(&ring->sched.pending_list,
4936 struct drm_sched_job, list);
4937 if (s_job == NULL)
4938 continue;
4939
4940 /* clear job's guilty and depend the folowing step to decide the real one */
4941 drm_sched_reset_karma(s_job);
38d4e463
JC
4942 /* for the real bad job, it will be resubmitted twice, adding a dma_fence_get
4943 * to make sure fence is balanced */
4944 dma_fence_get(s_job->s_fence->parent);
e6c6338f
JZ
4945 drm_sched_resubmit_jobs_ext(&ring->sched, 1);
4946
4947 ret = dma_fence_wait_timeout(s_job->s_fence->parent, false, ring->sched.timeout);
4948 if (ret == 0) { /* timeout */
4949 DRM_ERROR("Found the real bad job! ring:%s, job_id:%llx\n",
4950 ring->sched.name, s_job->id);
4951
4952 /* set guilty */
4953 drm_sched_increase_karma(s_job);
4954retry:
4955 /* do hw reset */
4956 if (amdgpu_sriov_vf(adev)) {
4957 amdgpu_virt_fini_data_exchange(adev);
4958 r = amdgpu_device_reset_sriov(adev, false);
4959 if (r)
4960 adev->asic_reset_res = r;
4961 } else {
04442bf7
LL
4962 clear_bit(AMDGPU_SKIP_HW_RESET,
4963 &reset_context->flags);
4964 r = amdgpu_do_asic_reset(device_list_handle,
4965 reset_context);
e6c6338f
JZ
4966 if (r && r == -EAGAIN)
4967 goto retry;
4968 }
4969
4970 /*
4971 * add reset counter so that the following
4972 * resubmitted job could flush vmid
4973 */
4974 atomic_inc(&adev->gpu_reset_counter);
4975 continue;
4976 }
4977
4978 /* got the hw fence, signal finished fence */
4979 atomic_dec(ring->sched.score);
38d4e463 4980 dma_fence_put(s_job->s_fence->parent);
e6c6338f
JZ
4981 dma_fence_get(&s_job->s_fence->finished);
4982 dma_fence_signal(&s_job->s_fence->finished);
4983 dma_fence_put(&s_job->s_fence->finished);
4984
4985 /* remove node from list and free the job */
4986 spin_lock(&ring->sched.job_list_lock);
4987 list_del_init(&s_job->list);
4988 spin_unlock(&ring->sched.job_list_lock);
4989 ring->sched.ops->free_job(s_job);
4990 }
4991}
4992
26bc5340
AG
4993/**
4994 * amdgpu_device_gpu_recover - reset the asic and recover scheduler
4995 *
982a820b 4996 * @adev: amdgpu_device pointer
26bc5340
AG
4997 * @job: which job trigger hang
4998 *
4999 * Attempt to reset the GPU if it has hung (all asics).
5000 * Attempt to do soft-reset or full-reset and reinitialize Asic
5001 * Returns 0 for success or an error on failure.
5002 */
5003
5004int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
5005 struct amdgpu_job *job)
5006{
1d721ed6 5007 struct list_head device_list, *device_list_handle = NULL;
7dd8c205 5008 bool job_signaled = false;
26bc5340 5009 struct amdgpu_hive_info *hive = NULL;
26bc5340 5010 struct amdgpu_device *tmp_adev = NULL;
1d721ed6 5011 int i, r = 0;
bb5c7235 5012 bool need_emergency_restart = false;
3f12acc8 5013 bool audio_suspended = false;
e6c6338f 5014 int tmp_vram_lost_counter;
04442bf7
LL
5015 struct amdgpu_reset_context reset_context;
5016
5017 memset(&reset_context, 0, sizeof(reset_context));
26bc5340 5018
6e3cd2a9 5019 /*
bb5c7235
WS
5020 * Special case: RAS triggered and full reset isn't supported
5021 */
5022 need_emergency_restart = amdgpu_ras_need_emergency_restart(adev);
5023
d5ea093e
AG
5024 /*
5025 * Flush RAM to disk so that after reboot
5026 * the user can read log and see why the system rebooted.
5027 */
bb5c7235 5028 if (need_emergency_restart && amdgpu_ras_get_context(adev)->reboot) {
d5ea093e
AG
5029 DRM_WARN("Emergency reboot.");
5030
5031 ksys_sync_helper();
5032 emergency_restart();
5033 }
5034
b823821f 5035 dev_info(adev->dev, "GPU %s begin!\n",
bb5c7235 5036 need_emergency_restart ? "jobs stop":"reset");
26bc5340
AG
5037
5038 /*
1d721ed6
AG
5039 * Here we trylock to avoid chain of resets executing from
5040 * either trigger by jobs on different adevs in XGMI hive or jobs on
5041 * different schedulers for same device while this TO handler is running.
5042 * We always reset all schedulers for device and all devices for XGMI
5043 * hive so that should take care of them too.
26bc5340 5044 */
175ac6ec
ZL
5045 if (!amdgpu_sriov_vf(adev))
5046 hive = amdgpu_get_xgmi_hive(adev);
53b3f8f4
DL
5047 if (hive) {
5048 if (atomic_cmpxchg(&hive->in_reset, 0, 1) != 0) {
5049 DRM_INFO("Bailing on TDR for s_job:%llx, hive: %llx as another already in progress",
5050 job ? job->base.id : -1, hive->hive_id);
d95e8e97 5051 amdgpu_put_xgmi_hive(hive);
ff99849b 5052 if (job && job->vm)
91fb309d 5053 drm_sched_increase_karma(&job->base);
53b3f8f4
DL
5054 return 0;
5055 }
5056 mutex_lock(&hive->hive_lock);
1d721ed6 5057 }
26bc5340 5058
04442bf7
LL
5059 reset_context.method = AMD_RESET_METHOD_NONE;
5060 reset_context.reset_req_dev = adev;
5061 reset_context.job = job;
5062 reset_context.hive = hive;
5063 clear_bit(AMDGPU_NEED_FULL_RESET, &reset_context.flags);
5064
91fb309d
HC
5065 /*
5066 * lock the device before we try to operate the linked list
5067 * if didn't get the device lock, don't touch the linked list since
5068 * others may iterating it.
5069 */
5070 r = amdgpu_device_lock_hive_adev(adev, hive);
5071 if (r) {
5072 dev_info(adev->dev, "Bailing on TDR for s_job:%llx, as another already in progress",
5073 job ? job->base.id : -1);
5074
5075 /* even we skipped this reset, still need to set the job to guilty */
ff99849b 5076 if (job && job->vm)
91fb309d
HC
5077 drm_sched_increase_karma(&job->base);
5078 goto skip_recovery;
5079 }
5080
9e94d22c
EQ
5081 /*
5082 * Build list of devices to reset.
5083 * In case we are in XGMI hive mode, resort the device list
5084 * to put adev in the 1st position.
5085 */
5086 INIT_LIST_HEAD(&device_list);
175ac6ec 5087 if (!amdgpu_sriov_vf(adev) && (adev->gmc.xgmi.num_physical_nodes > 1)) {
655ce9cb 5088 list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head)
5089 list_add_tail(&tmp_adev->reset_list, &device_list);
5090 if (!list_is_first(&adev->reset_list, &device_list))
5091 list_rotate_to_front(&adev->reset_list, &device_list);
5092 device_list_handle = &device_list;
26bc5340 5093 } else {
655ce9cb 5094 list_add_tail(&adev->reset_list, &device_list);
26bc5340
AG
5095 device_list_handle = &device_list;
5096 }
5097
1d721ed6 5098 /* block all schedulers and reset given job's ring */
655ce9cb 5099 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
3f12acc8
EQ
5100 /*
5101 * Try to put the audio codec into suspend state
5102 * before gpu reset started.
5103 *
5104 * Due to the power domain of the graphics device
5105 * is shared with AZ power domain. Without this,
5106 * we may change the audio hardware from behind
5107 * the audio driver's back. That will trigger
5108 * some audio codec errors.
5109 */
5110 if (!amdgpu_device_suspend_display_audio(tmp_adev))
5111 audio_suspended = true;
5112
9e94d22c
EQ
5113 amdgpu_ras_set_error_query_ready(tmp_adev, false);
5114
52fb44cf
EQ
5115 cancel_delayed_work_sync(&tmp_adev->delayed_init_work);
5116
428890a3 5117 if (!amdgpu_sriov_vf(tmp_adev))
5118 amdgpu_amdkfd_pre_reset(tmp_adev);
9e94d22c 5119
12ffa55d
AG
5120 /*
5121 * Mark these ASICs to be reseted as untracked first
5122 * And add them back after reset completed
5123 */
5124 amdgpu_unregister_gpu_instance(tmp_adev);
5125
087451f3 5126 drm_fb_helper_set_suspend_unlocked(adev_to_drm(adev)->fb_helper, true);
565d1941 5127
f1c1314b 5128 /* disable ras on ALL IPs */
bb5c7235 5129 if (!need_emergency_restart &&
b823821f 5130 amdgpu_device_ip_need_full_reset(tmp_adev))
f1c1314b 5131 amdgpu_ras_suspend(tmp_adev);
5132
1d721ed6
AG
5133 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
5134 struct amdgpu_ring *ring = tmp_adev->rings[i];
5135
5136 if (!ring || !ring->sched.thread)
5137 continue;
5138
0b2d2c2e 5139 drm_sched_stop(&ring->sched, job ? &job->base : NULL);
7c6e68c7 5140
bb5c7235 5141 if (need_emergency_restart)
7c6e68c7 5142 amdgpu_job_stop_all_jobs_on_sched(&ring->sched);
1d721ed6 5143 }
8f8c80f4 5144 atomic_inc(&tmp_adev->gpu_reset_counter);
1d721ed6
AG
5145 }
5146
bb5c7235 5147 if (need_emergency_restart)
7c6e68c7
AG
5148 goto skip_sched_resume;
5149
1d721ed6
AG
5150 /*
5151 * Must check guilty signal here since after this point all old
5152 * HW fences are force signaled.
5153 *
5154 * job->base holds a reference to parent fence
5155 */
5156 if (job && job->base.s_fence->parent &&
7dd8c205 5157 dma_fence_is_signaled(job->base.s_fence->parent)) {
1d721ed6 5158 job_signaled = true;
1d721ed6
AG
5159 dev_info(adev->dev, "Guilty job already signaled, skipping HW reset");
5160 goto skip_hw_reset;
5161 }
5162
26bc5340 5163retry: /* Rest of adevs pre asic reset from XGMI hive. */
655ce9cb 5164 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
04442bf7 5165 r = amdgpu_device_pre_asic_reset(tmp_adev, &reset_context);
26bc5340
AG
5166 /*TODO Should we stop ?*/
5167 if (r) {
aac89168 5168 dev_err(tmp_adev->dev, "GPU pre asic reset failed with err, %d for drm dev, %s ",
4a580877 5169 r, adev_to_drm(tmp_adev)->unique);
26bc5340
AG
5170 tmp_adev->asic_reset_res = r;
5171 }
5172 }
5173
e6c6338f 5174 tmp_vram_lost_counter = atomic_read(&((adev)->vram_lost_counter));
26bc5340 5175 /* Actual ASIC resets if needed.*/
4f30d920 5176 /* Host driver will handle XGMI hive reset for SRIOV */
26bc5340
AG
5177 if (amdgpu_sriov_vf(adev)) {
5178 r = amdgpu_device_reset_sriov(adev, job ? false : true);
5179 if (r)
5180 adev->asic_reset_res = r;
5181 } else {
04442bf7 5182 r = amdgpu_do_asic_reset(device_list_handle, &reset_context);
26bc5340
AG
5183 if (r && r == -EAGAIN)
5184 goto retry;
5185 }
5186
1d721ed6
AG
5187skip_hw_reset:
5188
26bc5340 5189 /* Post ASIC reset for all devs .*/
655ce9cb 5190 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
7c6e68c7 5191
e6c6338f
JZ
5192 /*
5193 * Sometimes a later bad compute job can block a good gfx job as gfx
5194 * and compute ring share internal GC HW mutually. We add an additional
5195 * guilty jobs recheck step to find the real guilty job, it synchronously
5196 * submits and pends for the first job being signaled. If it gets timeout,
5197 * we identify it as a real guilty job.
5198 */
5199 if (amdgpu_gpu_recovery == 2 &&
5200 !(tmp_vram_lost_counter < atomic_read(&adev->vram_lost_counter)))
04442bf7
LL
5201 amdgpu_device_recheck_guilty_jobs(
5202 tmp_adev, device_list_handle, &reset_context);
e6c6338f 5203
1d721ed6
AG
5204 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
5205 struct amdgpu_ring *ring = tmp_adev->rings[i];
5206
5207 if (!ring || !ring->sched.thread)
5208 continue;
5209
5210 /* No point to resubmit jobs if we didn't HW reset*/
5211 if (!tmp_adev->asic_reset_res && !job_signaled)
5212 drm_sched_resubmit_jobs(&ring->sched);
5213
5214 drm_sched_start(&ring->sched, !tmp_adev->asic_reset_res);
5215 }
5216
1053b9c9 5217 if (!drm_drv_uses_atomic_modeset(adev_to_drm(tmp_adev)) && !job_signaled) {
4a580877 5218 drm_helper_resume_force_mode(adev_to_drm(tmp_adev));
1d721ed6
AG
5219 }
5220
7258fa31
SK
5221 if (tmp_adev->asic_reset_res)
5222 r = tmp_adev->asic_reset_res;
5223
1d721ed6 5224 tmp_adev->asic_reset_res = 0;
26bc5340
AG
5225
5226 if (r) {
5227 /* bad news, how to tell it to userspace ? */
12ffa55d 5228 dev_info(tmp_adev->dev, "GPU reset(%d) failed\n", atomic_read(&tmp_adev->gpu_reset_counter));
26bc5340
AG
5229 amdgpu_vf_error_put(tmp_adev, AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, r);
5230 } else {
12ffa55d 5231 dev_info(tmp_adev->dev, "GPU reset(%d) succeeded!\n", atomic_read(&tmp_adev->gpu_reset_counter));
3fa8f89d
S
5232 if (amdgpu_acpi_smart_shift_update(adev_to_drm(tmp_adev), AMDGPU_SS_DEV_D0))
5233 DRM_WARN("smart shift update failed\n");
26bc5340 5234 }
7c6e68c7 5235 }
26bc5340 5236
7c6e68c7 5237skip_sched_resume:
655ce9cb 5238 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
428890a3 5239 /* unlock kfd: SRIOV would do it separately */
5240 if (!need_emergency_restart && !amdgpu_sriov_vf(tmp_adev))
5241 amdgpu_amdkfd_post_reset(tmp_adev);
8e2712e7 5242
5243 /* kfd_post_reset will do nothing if kfd device is not initialized,
5244 * need to bring up kfd here if it's not be initialized before
5245 */
5246 if (!adev->kfd.init_complete)
5247 amdgpu_amdkfd_device_init(adev);
5248
3f12acc8
EQ
5249 if (audio_suspended)
5250 amdgpu_device_resume_display_audio(tmp_adev);
26bc5340
AG
5251 amdgpu_device_unlock_adev(tmp_adev);
5252 }
5253
cbfd17f7 5254skip_recovery:
9e94d22c 5255 if (hive) {
53b3f8f4 5256 atomic_set(&hive->in_reset, 0);
9e94d22c 5257 mutex_unlock(&hive->hive_lock);
d95e8e97 5258 amdgpu_put_xgmi_hive(hive);
9e94d22c 5259 }
26bc5340 5260
91fb309d 5261 if (r && r != -EAGAIN)
26bc5340 5262 dev_info(adev->dev, "GPU reset end with ret = %d\n", r);
d38ceaf9
AD
5263 return r;
5264}
5265
e3ecdffa
AD
5266/**
5267 * amdgpu_device_get_pcie_info - fence pcie info about the PCIE slot
5268 *
5269 * @adev: amdgpu_device pointer
5270 *
5271 * Fetchs and stores in the driver the PCIE capabilities (gen speed
5272 * and lanes) of the slot the device is in. Handles APUs and
5273 * virtualized environments where PCIE config space may not be available.
5274 */
5494d864 5275static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev)
d0dd7f0c 5276{
5d9a6330 5277 struct pci_dev *pdev;
c5313457
HK
5278 enum pci_bus_speed speed_cap, platform_speed_cap;
5279 enum pcie_link_width platform_link_width;
d0dd7f0c 5280
cd474ba0
AD
5281 if (amdgpu_pcie_gen_cap)
5282 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
d0dd7f0c 5283
cd474ba0
AD
5284 if (amdgpu_pcie_lane_cap)
5285 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
d0dd7f0c 5286
cd474ba0
AD
5287 /* covers APUs as well */
5288 if (pci_is_root_bus(adev->pdev->bus)) {
5289 if (adev->pm.pcie_gen_mask == 0)
5290 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
5291 if (adev->pm.pcie_mlw_mask == 0)
5292 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
d0dd7f0c 5293 return;
cd474ba0 5294 }
d0dd7f0c 5295
c5313457
HK
5296 if (adev->pm.pcie_gen_mask && adev->pm.pcie_mlw_mask)
5297 return;
5298
dbaa922b
AD
5299 pcie_bandwidth_available(adev->pdev, NULL,
5300 &platform_speed_cap, &platform_link_width);
c5313457 5301
cd474ba0 5302 if (adev->pm.pcie_gen_mask == 0) {
5d9a6330
AD
5303 /* asic caps */
5304 pdev = adev->pdev;
5305 speed_cap = pcie_get_speed_cap(pdev);
5306 if (speed_cap == PCI_SPEED_UNKNOWN) {
5307 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
cd474ba0
AD
5308 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5309 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
cd474ba0 5310 } else {
2b3a1f51
FX
5311 if (speed_cap == PCIE_SPEED_32_0GT)
5312 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5313 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5314 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3 |
5315 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN4 |
5316 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN5);
5317 else if (speed_cap == PCIE_SPEED_16_0GT)
5d9a6330
AD
5318 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5319 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5320 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3 |
5321 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN4);
5322 else if (speed_cap == PCIE_SPEED_8_0GT)
5323 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5324 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5325 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
5326 else if (speed_cap == PCIE_SPEED_5_0GT)
5327 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5328 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2);
5329 else
5330 adev->pm.pcie_gen_mask |= CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1;
5331 }
5332 /* platform caps */
c5313457 5333 if (platform_speed_cap == PCI_SPEED_UNKNOWN) {
5d9a6330
AD
5334 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5335 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2);
5336 } else {
2b3a1f51
FX
5337 if (platform_speed_cap == PCIE_SPEED_32_0GT)
5338 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5339 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5340 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3 |
5341 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4 |
5342 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN5);
5343 else if (platform_speed_cap == PCIE_SPEED_16_0GT)
5d9a6330
AD
5344 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5345 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5346 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3 |
5347 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4);
c5313457 5348 else if (platform_speed_cap == PCIE_SPEED_8_0GT)
5d9a6330
AD
5349 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5350 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5351 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3);
c5313457 5352 else if (platform_speed_cap == PCIE_SPEED_5_0GT)
5d9a6330
AD
5353 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5354 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2);
5355 else
5356 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
5357
cd474ba0
AD
5358 }
5359 }
5360 if (adev->pm.pcie_mlw_mask == 0) {
c5313457 5361 if (platform_link_width == PCIE_LNK_WIDTH_UNKNOWN) {
5d9a6330
AD
5362 adev->pm.pcie_mlw_mask |= AMDGPU_DEFAULT_PCIE_MLW_MASK;
5363 } else {
c5313457 5364 switch (platform_link_width) {
5d9a6330 5365 case PCIE_LNK_X32:
cd474ba0
AD
5366 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
5367 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
5368 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
5369 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
5370 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5371 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5372 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5373 break;
5d9a6330 5374 case PCIE_LNK_X16:
cd474ba0
AD
5375 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
5376 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
5377 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
5378 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5379 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5380 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5381 break;
5d9a6330 5382 case PCIE_LNK_X12:
cd474ba0
AD
5383 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
5384 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
5385 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5386 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5387 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5388 break;
5d9a6330 5389 case PCIE_LNK_X8:
cd474ba0
AD
5390 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
5391 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5392 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5393 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5394 break;
5d9a6330 5395 case PCIE_LNK_X4:
cd474ba0
AD
5396 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5397 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5398 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5399 break;
5d9a6330 5400 case PCIE_LNK_X2:
cd474ba0
AD
5401 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5402 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5403 break;
5d9a6330 5404 case PCIE_LNK_X1:
cd474ba0
AD
5405 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
5406 break;
5407 default:
5408 break;
5409 }
d0dd7f0c
AD
5410 }
5411 }
5412}
d38ceaf9 5413
361dbd01
AD
5414int amdgpu_device_baco_enter(struct drm_device *dev)
5415{
1348969a 5416 struct amdgpu_device *adev = drm_to_adev(dev);
7a22677b 5417 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
361dbd01 5418
4a580877 5419 if (!amdgpu_device_supports_baco(adev_to_drm(adev)))
361dbd01
AD
5420 return -ENOTSUPP;
5421
8ab0d6f0 5422 if (ras && adev->ras_enabled &&
acdae216 5423 adev->nbio.funcs->enable_doorbell_interrupt)
7a22677b
LM
5424 adev->nbio.funcs->enable_doorbell_interrupt(adev, false);
5425
9530273e 5426 return amdgpu_dpm_baco_enter(adev);
361dbd01
AD
5427}
5428
5429int amdgpu_device_baco_exit(struct drm_device *dev)
5430{
1348969a 5431 struct amdgpu_device *adev = drm_to_adev(dev);
7a22677b 5432 struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
9530273e 5433 int ret = 0;
361dbd01 5434
4a580877 5435 if (!amdgpu_device_supports_baco(adev_to_drm(adev)))
361dbd01
AD
5436 return -ENOTSUPP;
5437
9530273e
EQ
5438 ret = amdgpu_dpm_baco_exit(adev);
5439 if (ret)
5440 return ret;
7a22677b 5441
8ab0d6f0 5442 if (ras && adev->ras_enabled &&
acdae216 5443 adev->nbio.funcs->enable_doorbell_interrupt)
7a22677b
LM
5444 adev->nbio.funcs->enable_doorbell_interrupt(adev, true);
5445
1bece222
CL
5446 if (amdgpu_passthrough(adev) &&
5447 adev->nbio.funcs->clear_doorbell_interrupt)
5448 adev->nbio.funcs->clear_doorbell_interrupt(adev);
5449
7a22677b 5450 return 0;
361dbd01 5451}
c9a6b82f 5452
acd89fca
AG
5453static void amdgpu_cancel_all_tdr(struct amdgpu_device *adev)
5454{
5455 int i;
5456
5457 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
5458 struct amdgpu_ring *ring = adev->rings[i];
5459
5460 if (!ring || !ring->sched.thread)
5461 continue;
5462
5463 cancel_delayed_work_sync(&ring->sched.work_tdr);
5464 }
5465}
5466
c9a6b82f
AG
5467/**
5468 * amdgpu_pci_error_detected - Called when a PCI error is detected.
5469 * @pdev: PCI device struct
5470 * @state: PCI channel state
5471 *
5472 * Description: Called when a PCI error is detected.
5473 *
5474 * Return: PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT.
5475 */
5476pci_ers_result_t amdgpu_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
5477{
5478 struct drm_device *dev = pci_get_drvdata(pdev);
5479 struct amdgpu_device *adev = drm_to_adev(dev);
acd89fca 5480 int i;
c9a6b82f
AG
5481
5482 DRM_INFO("PCI error: detected callback, state(%d)!!\n", state);
5483
6894305c
AG
5484 if (adev->gmc.xgmi.num_physical_nodes > 1) {
5485 DRM_WARN("No support for XGMI hive yet...");
5486 return PCI_ERS_RESULT_DISCONNECT;
5487 }
5488
e17e27f9
GC
5489 adev->pci_channel_state = state;
5490
c9a6b82f
AG
5491 switch (state) {
5492 case pci_channel_io_normal:
5493 return PCI_ERS_RESULT_CAN_RECOVER;
acd89fca 5494 /* Fatal error, prepare for slot reset */
8a11d283
TZ
5495 case pci_channel_io_frozen:
5496 /*
acd89fca
AG
5497 * Cancel and wait for all TDRs in progress if failing to
5498 * set adev->in_gpu_reset in amdgpu_device_lock_adev
5499 *
5500 * Locking adev->reset_sem will prevent any external access
5501 * to GPU during PCI error recovery
5502 */
5503 while (!amdgpu_device_lock_adev(adev, NULL))
5504 amdgpu_cancel_all_tdr(adev);
5505
5506 /*
5507 * Block any work scheduling as we do for regular GPU reset
5508 * for the duration of the recovery
5509 */
5510 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
5511 struct amdgpu_ring *ring = adev->rings[i];
5512
5513 if (!ring || !ring->sched.thread)
5514 continue;
5515
5516 drm_sched_stop(&ring->sched, NULL);
5517 }
8f8c80f4 5518 atomic_inc(&adev->gpu_reset_counter);
c9a6b82f
AG
5519 return PCI_ERS_RESULT_NEED_RESET;
5520 case pci_channel_io_perm_failure:
5521 /* Permanent error, prepare for device removal */
5522 return PCI_ERS_RESULT_DISCONNECT;
5523 }
5524
5525 return PCI_ERS_RESULT_NEED_RESET;
5526}
5527
5528/**
5529 * amdgpu_pci_mmio_enabled - Enable MMIO and dump debug registers
5530 * @pdev: pointer to PCI device
5531 */
5532pci_ers_result_t amdgpu_pci_mmio_enabled(struct pci_dev *pdev)
5533{
5534
5535 DRM_INFO("PCI error: mmio enabled callback!!\n");
5536
5537 /* TODO - dump whatever for debugging purposes */
5538
5539 /* This called only if amdgpu_pci_error_detected returns
5540 * PCI_ERS_RESULT_CAN_RECOVER. Read/write to the device still
5541 * works, no need to reset slot.
5542 */
5543
5544 return PCI_ERS_RESULT_RECOVERED;
5545}
5546
5547/**
5548 * amdgpu_pci_slot_reset - Called when PCI slot has been reset.
5549 * @pdev: PCI device struct
5550 *
5551 * Description: This routine is called by the pci error recovery
5552 * code after the PCI slot has been reset, just before we
5553 * should resume normal operations.
5554 */
5555pci_ers_result_t amdgpu_pci_slot_reset(struct pci_dev *pdev)
5556{
5557 struct drm_device *dev = pci_get_drvdata(pdev);
5558 struct amdgpu_device *adev = drm_to_adev(dev);
362c7b91 5559 int r, i;
04442bf7 5560 struct amdgpu_reset_context reset_context;
362c7b91 5561 u32 memsize;
7ac71382 5562 struct list_head device_list;
c9a6b82f
AG
5563
5564 DRM_INFO("PCI error: slot reset callback!!\n");
5565
04442bf7
LL
5566 memset(&reset_context, 0, sizeof(reset_context));
5567
7ac71382 5568 INIT_LIST_HEAD(&device_list);
655ce9cb 5569 list_add_tail(&adev->reset_list, &device_list);
7ac71382 5570
362c7b91
AG
5571 /* wait for asic to come out of reset */
5572 msleep(500);
5573
7ac71382 5574 /* Restore PCI confspace */
c1dd4aa6 5575 amdgpu_device_load_pci_state(pdev);
c9a6b82f 5576
362c7b91
AG
5577 /* confirm ASIC came out of reset */
5578 for (i = 0; i < adev->usec_timeout; i++) {
5579 memsize = amdgpu_asic_get_config_memsize(adev);
5580
5581 if (memsize != 0xffffffff)
5582 break;
5583 udelay(1);
5584 }
5585 if (memsize == 0xffffffff) {
5586 r = -ETIME;
5587 goto out;
5588 }
5589
04442bf7
LL
5590 reset_context.method = AMD_RESET_METHOD_NONE;
5591 reset_context.reset_req_dev = adev;
5592 set_bit(AMDGPU_NEED_FULL_RESET, &reset_context.flags);
5593 set_bit(AMDGPU_SKIP_HW_RESET, &reset_context.flags);
5594
7afefb81 5595 adev->no_hw_access = true;
04442bf7 5596 r = amdgpu_device_pre_asic_reset(adev, &reset_context);
7afefb81 5597 adev->no_hw_access = false;
c9a6b82f
AG
5598 if (r)
5599 goto out;
5600
04442bf7 5601 r = amdgpu_do_asic_reset(&device_list, &reset_context);
c9a6b82f
AG
5602
5603out:
c9a6b82f 5604 if (!r) {
c1dd4aa6
AG
5605 if (amdgpu_device_cache_pci_state(adev->pdev))
5606 pci_restore_state(adev->pdev);
5607
c9a6b82f
AG
5608 DRM_INFO("PCIe error recovery succeeded\n");
5609 } else {
5610 DRM_ERROR("PCIe error recovery failed, err:%d", r);
5611 amdgpu_device_unlock_adev(adev);
5612 }
5613
5614 return r ? PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_RECOVERED;
5615}
5616
5617/**
5618 * amdgpu_pci_resume() - resume normal ops after PCI reset
5619 * @pdev: pointer to PCI device
5620 *
5621 * Called when the error recovery driver tells us that its
505199a3 5622 * OK to resume normal operation.
c9a6b82f
AG
5623 */
5624void amdgpu_pci_resume(struct pci_dev *pdev)
5625{
5626 struct drm_device *dev = pci_get_drvdata(pdev);
5627 struct amdgpu_device *adev = drm_to_adev(dev);
acd89fca 5628 int i;
c9a6b82f 5629
c9a6b82f
AG
5630
5631 DRM_INFO("PCI error: resume callback!!\n");
acd89fca 5632
e17e27f9
GC
5633 /* Only continue execution for the case of pci_channel_io_frozen */
5634 if (adev->pci_channel_state != pci_channel_io_frozen)
5635 return;
5636
acd89fca
AG
5637 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
5638 struct amdgpu_ring *ring = adev->rings[i];
5639
5640 if (!ring || !ring->sched.thread)
5641 continue;
5642
5643
5644 drm_sched_resubmit_jobs(&ring->sched);
5645 drm_sched_start(&ring->sched, true);
5646 }
5647
5648 amdgpu_device_unlock_adev(adev);
c9a6b82f 5649}
c1dd4aa6
AG
5650
5651bool amdgpu_device_cache_pci_state(struct pci_dev *pdev)
5652{
5653 struct drm_device *dev = pci_get_drvdata(pdev);
5654 struct amdgpu_device *adev = drm_to_adev(dev);
5655 int r;
5656
5657 r = pci_save_state(pdev);
5658 if (!r) {
5659 kfree(adev->pci_state);
5660
5661 adev->pci_state = pci_store_saved_state(pdev);
5662
5663 if (!adev->pci_state) {
5664 DRM_ERROR("Failed to store PCI saved state");
5665 return false;
5666 }
5667 } else {
5668 DRM_WARN("Failed to save PCI state, err:%d\n", r);
5669 return false;
5670 }
5671
5672 return true;
5673}
5674
5675bool amdgpu_device_load_pci_state(struct pci_dev *pdev)
5676{
5677 struct drm_device *dev = pci_get_drvdata(pdev);
5678 struct amdgpu_device *adev = drm_to_adev(dev);
5679 int r;
5680
5681 if (!adev->pci_state)
5682 return false;
5683
5684 r = pci_load_saved_state(pdev, adev->pci_state);
5685
5686 if (!r) {
5687 pci_restore_state(pdev);
5688 } else {
5689 DRM_WARN("Failed to load PCI state, err:%d\n", r);
5690 return false;
5691 }
5692
5693 return true;
5694}
5695
810085dd
EH
5696void amdgpu_device_flush_hdp(struct amdgpu_device *adev,
5697 struct amdgpu_ring *ring)
5698{
5699#ifdef CONFIG_X86_64
5700 if (adev->flags & AMD_IS_APU)
5701 return;
5702#endif
5703 if (adev->gmc.xgmi.connected_to_cpu)
5704 return;
5705
5706 if (ring && ring->funcs->emit_hdp_flush)
5707 amdgpu_ring_emit_hdp_flush(ring);
5708 else
5709 amdgpu_asic_flush_hdp(adev, ring);
5710}
c1dd4aa6 5711
810085dd
EH
5712void amdgpu_device_invalidate_hdp(struct amdgpu_device *adev,
5713 struct amdgpu_ring *ring)
5714{
5715#ifdef CONFIG_X86_64
5716 if (adev->flags & AMD_IS_APU)
5717 return;
5718#endif
5719 if (adev->gmc.xgmi.connected_to_cpu)
5720 return;
c1dd4aa6 5721
810085dd
EH
5722 amdgpu_asic_invalidate_hdp(adev, ring);
5723}
34f3a4a9
LY
5724
5725/**
5726 * amdgpu_device_halt() - bring hardware to some kind of halt state
5727 *
5728 * @adev: amdgpu_device pointer
5729 *
5730 * Bring hardware to some kind of halt state so that no one can touch it
5731 * any more. It will help to maintain error context when error occurred.
5732 * Compare to a simple hang, the system will keep stable at least for SSH
5733 * access. Then it should be trivial to inspect the hardware state and
5734 * see what's going on. Implemented as following:
5735 *
5736 * 1. drm_dev_unplug() makes device inaccessible to user space(IOCTLs, etc),
5737 * clears all CPU mappings to device, disallows remappings through page faults
5738 * 2. amdgpu_irq_disable_all() disables all interrupts
5739 * 3. amdgpu_fence_driver_hw_fini() signals all HW fences
5740 * 4. set adev->no_hw_access to avoid potential crashes after setp 5
5741 * 5. amdgpu_device_unmap_mmio() clears all MMIO mappings
5742 * 6. pci_disable_device() and pci_wait_for_pending_transaction()
5743 * flush any in flight DMA operations
5744 */
5745void amdgpu_device_halt(struct amdgpu_device *adev)
5746{
5747 struct pci_dev *pdev = adev->pdev;
e0f943b4 5748 struct drm_device *ddev = adev_to_drm(adev);
34f3a4a9
LY
5749
5750 drm_dev_unplug(ddev);
5751
5752 amdgpu_irq_disable_all(adev);
5753
5754 amdgpu_fence_driver_hw_fini(adev);
5755
5756 adev->no_hw_access = true;
5757
5758 amdgpu_device_unmap_mmio(adev);
5759
5760 pci_disable_device(pdev);
5761 pci_wait_for_pending_transaction(pdev);
5762}
86700a40
XD
5763
5764u32 amdgpu_device_pcie_port_rreg(struct amdgpu_device *adev,
5765 u32 reg)
5766{
5767 unsigned long flags, address, data;
5768 u32 r;
5769
5770 address = adev->nbio.funcs->get_pcie_port_index_offset(adev);
5771 data = adev->nbio.funcs->get_pcie_port_data_offset(adev);
5772
5773 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
5774 WREG32(address, reg * 4);
5775 (void)RREG32(address);
5776 r = RREG32(data);
5777 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
5778 return r;
5779}
5780
5781void amdgpu_device_pcie_port_wreg(struct amdgpu_device *adev,
5782 u32 reg, u32 v)
5783{
5784 unsigned long flags, address, data;
5785
5786 address = adev->nbio.funcs->get_pcie_port_index_offset(adev);
5787 data = adev->nbio.funcs->get_pcie_port_data_offset(adev);
5788
5789 spin_lock_irqsave(&adev->pcie_idx_lock, flags);
5790 WREG32(address, reg * 4);
5791 (void)RREG32(address);
5792 WREG32(data, v);
5793 (void)RREG32(data);
5794 spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
5795}