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