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