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