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