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