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