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