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