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