drm/amd/powerplay: Delete a redundant memory setting in vega20_set_default_od8_settti...
[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
912dfc84
EQ
976 ret = amdgpu_device_get_job_timeout_settings(adev);
977 if (ret) {
978 dev_err(adev->dev, "invalid lockup_timeout parameter syntax\n");
979 return ret;
8854695a 980 }
19aede77
AD
981
982 adev->firmware.load_type = amdgpu_ucode_get_load_type(adev, amdgpu_fw_load_type);
912dfc84
EQ
983
984 return ret;
d38ceaf9
AD
985}
986
987/**
988 * amdgpu_switcheroo_set_state - set switcheroo state
989 *
990 * @pdev: pci dev pointer
1694467b 991 * @state: vga_switcheroo state
d38ceaf9
AD
992 *
993 * Callback for the switcheroo driver. Suspends or resumes the
994 * the asics before or after it is powered up using ACPI methods.
995 */
996static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
997{
998 struct drm_device *dev = pci_get_drvdata(pdev);
999
1000 if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
1001 return;
1002
1003 if (state == VGA_SWITCHEROO_ON) {
7ca85295 1004 pr_info("amdgpu: switched on\n");
d38ceaf9
AD
1005 /* don't suspend or resume card normally */
1006 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1007
810ddc3a 1008 amdgpu_device_resume(dev, true, true);
d38ceaf9 1009
d38ceaf9
AD
1010 dev->switch_power_state = DRM_SWITCH_POWER_ON;
1011 drm_kms_helper_poll_enable(dev);
1012 } else {
7ca85295 1013 pr_info("amdgpu: switched off\n");
d38ceaf9
AD
1014 drm_kms_helper_poll_disable(dev);
1015 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
810ddc3a 1016 amdgpu_device_suspend(dev, true, true);
d38ceaf9
AD
1017 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1018 }
1019}
1020
1021/**
1022 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1023 *
1024 * @pdev: pci dev pointer
1025 *
1026 * Callback for the switcheroo driver. Check of the switcheroo
1027 * state can be changed.
1028 * Returns true if the state can be changed, false if not.
1029 */
1030static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1031{
1032 struct drm_device *dev = pci_get_drvdata(pdev);
1033
1034 /*
1035 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1036 * locking inversion with the driver load path. And the access here is
1037 * completely racy anyway. So don't bother with locking for now.
1038 */
1039 return dev->open_count == 0;
1040}
1041
1042static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1043 .set_gpu_state = amdgpu_switcheroo_set_state,
1044 .reprobe = NULL,
1045 .can_switch = amdgpu_switcheroo_can_switch,
1046};
1047
e3ecdffa
AD
1048/**
1049 * amdgpu_device_ip_set_clockgating_state - set the CG state
1050 *
87e3f136 1051 * @dev: amdgpu_device pointer
e3ecdffa
AD
1052 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1053 * @state: clockgating state (gate or ungate)
1054 *
1055 * Sets the requested clockgating state for all instances of
1056 * the hardware IP specified.
1057 * Returns the error code from the last instance.
1058 */
43fa561f 1059int amdgpu_device_ip_set_clockgating_state(void *dev,
2990a1fc
AD
1060 enum amd_ip_block_type block_type,
1061 enum amd_clockgating_state state)
d38ceaf9 1062{
43fa561f 1063 struct amdgpu_device *adev = dev;
d38ceaf9
AD
1064 int i, r = 0;
1065
1066 for (i = 0; i < adev->num_ip_blocks; i++) {
a1255107 1067 if (!adev->ip_blocks[i].status.valid)
9ecbe7f5 1068 continue;
c722865a
RZ
1069 if (adev->ip_blocks[i].version->type != block_type)
1070 continue;
1071 if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
1072 continue;
1073 r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
1074 (void *)adev, state);
1075 if (r)
1076 DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
1077 adev->ip_blocks[i].version->funcs->name, r);
d38ceaf9
AD
1078 }
1079 return r;
1080}
1081
e3ecdffa
AD
1082/**
1083 * amdgpu_device_ip_set_powergating_state - set the PG state
1084 *
87e3f136 1085 * @dev: amdgpu_device pointer
e3ecdffa
AD
1086 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1087 * @state: powergating state (gate or ungate)
1088 *
1089 * Sets the requested powergating state for all instances of
1090 * the hardware IP specified.
1091 * Returns the error code from the last instance.
1092 */
43fa561f 1093int amdgpu_device_ip_set_powergating_state(void *dev,
2990a1fc
AD
1094 enum amd_ip_block_type block_type,
1095 enum amd_powergating_state state)
d38ceaf9 1096{
43fa561f 1097 struct amdgpu_device *adev = dev;
d38ceaf9
AD
1098 int i, r = 0;
1099
1100 for (i = 0; i < adev->num_ip_blocks; i++) {
a1255107 1101 if (!adev->ip_blocks[i].status.valid)
9ecbe7f5 1102 continue;
c722865a
RZ
1103 if (adev->ip_blocks[i].version->type != block_type)
1104 continue;
1105 if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
1106 continue;
1107 r = adev->ip_blocks[i].version->funcs->set_powergating_state(
1108 (void *)adev, state);
1109 if (r)
1110 DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
1111 adev->ip_blocks[i].version->funcs->name, r);
d38ceaf9
AD
1112 }
1113 return r;
1114}
1115
e3ecdffa
AD
1116/**
1117 * amdgpu_device_ip_get_clockgating_state - get the CG state
1118 *
1119 * @adev: amdgpu_device pointer
1120 * @flags: clockgating feature flags
1121 *
1122 * Walks the list of IPs on the device and updates the clockgating
1123 * flags for each IP.
1124 * Updates @flags with the feature flags for each hardware IP where
1125 * clockgating is enabled.
1126 */
2990a1fc
AD
1127void amdgpu_device_ip_get_clockgating_state(struct amdgpu_device *adev,
1128 u32 *flags)
6cb2d4e4
HR
1129{
1130 int i;
1131
1132 for (i = 0; i < adev->num_ip_blocks; i++) {
1133 if (!adev->ip_blocks[i].status.valid)
1134 continue;
1135 if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
1136 adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
1137 }
1138}
1139
e3ecdffa
AD
1140/**
1141 * amdgpu_device_ip_wait_for_idle - wait for idle
1142 *
1143 * @adev: amdgpu_device pointer
1144 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1145 *
1146 * Waits for the request hardware IP to be idle.
1147 * Returns 0 for success or a negative error code on failure.
1148 */
2990a1fc
AD
1149int amdgpu_device_ip_wait_for_idle(struct amdgpu_device *adev,
1150 enum amd_ip_block_type block_type)
5dbbb60b
AD
1151{
1152 int i, r;
1153
1154 for (i = 0; i < adev->num_ip_blocks; i++) {
a1255107 1155 if (!adev->ip_blocks[i].status.valid)
9ecbe7f5 1156 continue;
a1255107
AD
1157 if (adev->ip_blocks[i].version->type == block_type) {
1158 r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
5dbbb60b
AD
1159 if (r)
1160 return r;
1161 break;
1162 }
1163 }
1164 return 0;
1165
1166}
1167
e3ecdffa
AD
1168/**
1169 * amdgpu_device_ip_is_idle - is the hardware IP idle
1170 *
1171 * @adev: amdgpu_device pointer
1172 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1173 *
1174 * Check if the hardware IP is idle or not.
1175 * Returns true if it the IP is idle, false if not.
1176 */
2990a1fc
AD
1177bool amdgpu_device_ip_is_idle(struct amdgpu_device *adev,
1178 enum amd_ip_block_type block_type)
5dbbb60b
AD
1179{
1180 int i;
1181
1182 for (i = 0; i < adev->num_ip_blocks; i++) {
a1255107 1183 if (!adev->ip_blocks[i].status.valid)
9ecbe7f5 1184 continue;
a1255107
AD
1185 if (adev->ip_blocks[i].version->type == block_type)
1186 return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
5dbbb60b
AD
1187 }
1188 return true;
1189
1190}
1191
e3ecdffa
AD
1192/**
1193 * amdgpu_device_ip_get_ip_block - get a hw IP pointer
1194 *
1195 * @adev: amdgpu_device pointer
87e3f136 1196 * @type: Type of hardware IP (SMU, GFX, UVD, etc.)
e3ecdffa
AD
1197 *
1198 * Returns a pointer to the hardware IP block structure
1199 * if it exists for the asic, otherwise NULL.
1200 */
2990a1fc
AD
1201struct amdgpu_ip_block *
1202amdgpu_device_ip_get_ip_block(struct amdgpu_device *adev,
1203 enum amd_ip_block_type type)
d38ceaf9
AD
1204{
1205 int i;
1206
1207 for (i = 0; i < adev->num_ip_blocks; i++)
a1255107 1208 if (adev->ip_blocks[i].version->type == type)
d38ceaf9
AD
1209 return &adev->ip_blocks[i];
1210
1211 return NULL;
1212}
1213
1214/**
2990a1fc 1215 * amdgpu_device_ip_block_version_cmp
d38ceaf9
AD
1216 *
1217 * @adev: amdgpu_device pointer
5fc3aeeb 1218 * @type: enum amd_ip_block_type
d38ceaf9
AD
1219 * @major: major version
1220 * @minor: minor version
1221 *
1222 * return 0 if equal or greater
1223 * return 1 if smaller or the ip_block doesn't exist
1224 */
2990a1fc
AD
1225int amdgpu_device_ip_block_version_cmp(struct amdgpu_device *adev,
1226 enum amd_ip_block_type type,
1227 u32 major, u32 minor)
d38ceaf9 1228{
2990a1fc 1229 struct amdgpu_ip_block *ip_block = amdgpu_device_ip_get_ip_block(adev, type);
d38ceaf9 1230
a1255107
AD
1231 if (ip_block && ((ip_block->version->major > major) ||
1232 ((ip_block->version->major == major) &&
1233 (ip_block->version->minor >= minor))))
d38ceaf9
AD
1234 return 0;
1235
1236 return 1;
1237}
1238
a1255107 1239/**
2990a1fc 1240 * amdgpu_device_ip_block_add
a1255107
AD
1241 *
1242 * @adev: amdgpu_device pointer
1243 * @ip_block_version: pointer to the IP to add
1244 *
1245 * Adds the IP block driver information to the collection of IPs
1246 * on the asic.
1247 */
2990a1fc
AD
1248int amdgpu_device_ip_block_add(struct amdgpu_device *adev,
1249 const struct amdgpu_ip_block_version *ip_block_version)
a1255107
AD
1250{
1251 if (!ip_block_version)
1252 return -EINVAL;
1253
e966a725 1254 DRM_INFO("add ip block number %d <%s>\n", adev->num_ip_blocks,
a0bae357
HR
1255 ip_block_version->funcs->name);
1256
a1255107
AD
1257 adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
1258
1259 return 0;
1260}
1261
e3ecdffa
AD
1262/**
1263 * amdgpu_device_enable_virtual_display - enable virtual display feature
1264 *
1265 * @adev: amdgpu_device pointer
1266 *
1267 * Enabled the virtual display feature if the user has enabled it via
1268 * the module parameter virtual_display. This feature provides a virtual
1269 * display hardware on headless boards or in virtualized environments.
1270 * This function parses and validates the configuration string specified by
1271 * the user and configues the virtual display configuration (number of
1272 * virtual connectors, crtcs, etc.) specified.
1273 */
483ef985 1274static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
9accf2fd
ED
1275{
1276 adev->enable_virtual_display = false;
1277
1278 if (amdgpu_virtual_display) {
1279 struct drm_device *ddev = adev->ddev;
1280 const char *pci_address_name = pci_name(ddev->pdev);
0f66356d 1281 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
9accf2fd
ED
1282
1283 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1284 pciaddstr_tmp = pciaddstr;
0f66356d
ED
1285 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1286 pciaddname = strsep(&pciaddname_tmp, ",");
967de2a9
YT
1287 if (!strcmp("all", pciaddname)
1288 || !strcmp(pci_address_name, pciaddname)) {
0f66356d
ED
1289 long num_crtc;
1290 int res = -1;
1291
9accf2fd 1292 adev->enable_virtual_display = true;
0f66356d
ED
1293
1294 if (pciaddname_tmp)
1295 res = kstrtol(pciaddname_tmp, 10,
1296 &num_crtc);
1297
1298 if (!res) {
1299 if (num_crtc < 1)
1300 num_crtc = 1;
1301 if (num_crtc > 6)
1302 num_crtc = 6;
1303 adev->mode_info.num_crtc = num_crtc;
1304 } else {
1305 adev->mode_info.num_crtc = 1;
1306 }
9accf2fd
ED
1307 break;
1308 }
1309 }
1310
0f66356d
ED
1311 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
1312 amdgpu_virtual_display, pci_address_name,
1313 adev->enable_virtual_display, adev->mode_info.num_crtc);
9accf2fd
ED
1314
1315 kfree(pciaddstr);
1316 }
1317}
1318
e3ecdffa
AD
1319/**
1320 * amdgpu_device_parse_gpu_info_fw - parse gpu info firmware
1321 *
1322 * @adev: amdgpu_device pointer
1323 *
1324 * Parses the asic configuration parameters specified in the gpu info
1325 * firmware and makes them availale to the driver for use in configuring
1326 * the asic.
1327 * Returns 0 on success, -EINVAL on failure.
1328 */
e2a75f88
AD
1329static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev)
1330{
e2a75f88
AD
1331 const char *chip_name;
1332 char fw_name[30];
1333 int err;
1334 const struct gpu_info_firmware_header_v1_0 *hdr;
1335
ab4fe3e1
HR
1336 adev->firmware.gpu_info_fw = NULL;
1337
e2a75f88
AD
1338 switch (adev->asic_type) {
1339 case CHIP_TOPAZ:
1340 case CHIP_TONGA:
1341 case CHIP_FIJI:
e2a75f88 1342 case CHIP_POLARIS10:
cc07f18d 1343 case CHIP_POLARIS11:
e2a75f88 1344 case CHIP_POLARIS12:
cc07f18d 1345 case CHIP_VEGAM:
e2a75f88
AD
1346 case CHIP_CARRIZO:
1347 case CHIP_STONEY:
1348#ifdef CONFIG_DRM_AMDGPU_SI
1349 case CHIP_VERDE:
1350 case CHIP_TAHITI:
1351 case CHIP_PITCAIRN:
1352 case CHIP_OLAND:
1353 case CHIP_HAINAN:
1354#endif
1355#ifdef CONFIG_DRM_AMDGPU_CIK
1356 case CHIP_BONAIRE:
1357 case CHIP_HAWAII:
1358 case CHIP_KAVERI:
1359 case CHIP_KABINI:
1360 case CHIP_MULLINS:
1361#endif
27c0bc71 1362 case CHIP_VEGA20:
e2a75f88
AD
1363 default:
1364 return 0;
1365 case CHIP_VEGA10:
1366 chip_name = "vega10";
1367 break;
3f76dced
AD
1368 case CHIP_VEGA12:
1369 chip_name = "vega12";
1370 break;
2d2e5e7e 1371 case CHIP_RAVEN:
54c4d17e
FX
1372 if (adev->rev_id >= 8)
1373 chip_name = "raven2";
741deade
AD
1374 else if (adev->pdev->device == 0x15d8)
1375 chip_name = "picasso";
54c4d17e
FX
1376 else
1377 chip_name = "raven";
2d2e5e7e 1378 break;
e2a75f88
AD
1379 }
1380
1381 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name);
ab4fe3e1 1382 err = request_firmware(&adev->firmware.gpu_info_fw, fw_name, adev->dev);
e2a75f88
AD
1383 if (err) {
1384 dev_err(adev->dev,
1385 "Failed to load gpu_info firmware \"%s\"\n",
1386 fw_name);
1387 goto out;
1388 }
ab4fe3e1 1389 err = amdgpu_ucode_validate(adev->firmware.gpu_info_fw);
e2a75f88
AD
1390 if (err) {
1391 dev_err(adev->dev,
1392 "Failed to validate gpu_info firmware \"%s\"\n",
1393 fw_name);
1394 goto out;
1395 }
1396
ab4fe3e1 1397 hdr = (const struct gpu_info_firmware_header_v1_0 *)adev->firmware.gpu_info_fw->data;
e2a75f88
AD
1398 amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
1399
1400 switch (hdr->version_major) {
1401 case 1:
1402 {
1403 const struct gpu_info_firmware_v1_0 *gpu_info_fw =
ab4fe3e1 1404 (const struct gpu_info_firmware_v1_0 *)(adev->firmware.gpu_info_fw->data +
e2a75f88
AD
1405 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1406
b5ab16bf
AD
1407 adev->gfx.config.max_shader_engines = le32_to_cpu(gpu_info_fw->gc_num_se);
1408 adev->gfx.config.max_cu_per_sh = le32_to_cpu(gpu_info_fw->gc_num_cu_per_sh);
1409 adev->gfx.config.max_sh_per_se = le32_to_cpu(gpu_info_fw->gc_num_sh_per_se);
1410 adev->gfx.config.max_backends_per_se = le32_to_cpu(gpu_info_fw->gc_num_rb_per_se);
e2a75f88 1411 adev->gfx.config.max_texture_channel_caches =
b5ab16bf
AD
1412 le32_to_cpu(gpu_info_fw->gc_num_tccs);
1413 adev->gfx.config.max_gprs = le32_to_cpu(gpu_info_fw->gc_num_gprs);
1414 adev->gfx.config.max_gs_threads = le32_to_cpu(gpu_info_fw->gc_num_max_gs_thds);
1415 adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gpu_info_fw->gc_gs_table_depth);
1416 adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gpu_info_fw->gc_gsprim_buff_depth);
e2a75f88 1417 adev->gfx.config.double_offchip_lds_buf =
b5ab16bf
AD
1418 le32_to_cpu(gpu_info_fw->gc_double_offchip_lds_buffer);
1419 adev->gfx.cu_info.wave_front_size = le32_to_cpu(gpu_info_fw->gc_wave_size);
51fd0370
HZ
1420 adev->gfx.cu_info.max_waves_per_simd =
1421 le32_to_cpu(gpu_info_fw->gc_max_waves_per_simd);
1422 adev->gfx.cu_info.max_scratch_slots_per_cu =
1423 le32_to_cpu(gpu_info_fw->gc_max_scratch_slots_per_cu);
1424 adev->gfx.cu_info.lds_size = le32_to_cpu(gpu_info_fw->gc_lds_size);
e2a75f88
AD
1425 break;
1426 }
1427 default:
1428 dev_err(adev->dev,
1429 "Unsupported gpu_info table %d\n", hdr->header.ucode_version);
1430 err = -EINVAL;
1431 goto out;
1432 }
1433out:
e2a75f88
AD
1434 return err;
1435}
1436
e3ecdffa
AD
1437/**
1438 * amdgpu_device_ip_early_init - run early init for hardware IPs
1439 *
1440 * @adev: amdgpu_device pointer
1441 *
1442 * Early initialization pass for hardware IPs. The hardware IPs that make
1443 * up each asic are discovered each IP's early_init callback is run. This
1444 * is the first stage in initializing the asic.
1445 * Returns 0 on success, negative error code on failure.
1446 */
06ec9070 1447static int amdgpu_device_ip_early_init(struct amdgpu_device *adev)
d38ceaf9 1448{
aaa36a97 1449 int i, r;
d38ceaf9 1450
483ef985 1451 amdgpu_device_enable_virtual_display(adev);
a6be7570 1452
d38ceaf9 1453 switch (adev->asic_type) {
aaa36a97
AD
1454 case CHIP_TOPAZ:
1455 case CHIP_TONGA:
48299f95 1456 case CHIP_FIJI:
2cc0c0b5 1457 case CHIP_POLARIS10:
32cc7e53 1458 case CHIP_POLARIS11:
c4642a47 1459 case CHIP_POLARIS12:
32cc7e53 1460 case CHIP_VEGAM:
aaa36a97 1461 case CHIP_CARRIZO:
39bb0c92
SL
1462 case CHIP_STONEY:
1463 if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
aaa36a97
AD
1464 adev->family = AMDGPU_FAMILY_CZ;
1465 else
1466 adev->family = AMDGPU_FAMILY_VI;
1467
1468 r = vi_set_ip_blocks(adev);
1469 if (r)
1470 return r;
1471 break;
33f34802
KW
1472#ifdef CONFIG_DRM_AMDGPU_SI
1473 case CHIP_VERDE:
1474 case CHIP_TAHITI:
1475 case CHIP_PITCAIRN:
1476 case CHIP_OLAND:
1477 case CHIP_HAINAN:
295d0daf 1478 adev->family = AMDGPU_FAMILY_SI;
33f34802
KW
1479 r = si_set_ip_blocks(adev);
1480 if (r)
1481 return r;
1482 break;
1483#endif
a2e73f56
AD
1484#ifdef CONFIG_DRM_AMDGPU_CIK
1485 case CHIP_BONAIRE:
1486 case CHIP_HAWAII:
1487 case CHIP_KAVERI:
1488 case CHIP_KABINI:
1489 case CHIP_MULLINS:
1490 if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
1491 adev->family = AMDGPU_FAMILY_CI;
1492 else
1493 adev->family = AMDGPU_FAMILY_KV;
1494
1495 r = cik_set_ip_blocks(adev);
1496 if (r)
1497 return r;
1498 break;
1499#endif
e48a3cd9
AD
1500 case CHIP_VEGA10:
1501 case CHIP_VEGA12:
e4bd8170 1502 case CHIP_VEGA20:
e48a3cd9 1503 case CHIP_RAVEN:
741deade 1504 if (adev->asic_type == CHIP_RAVEN)
2ca8a5d2
CZ
1505 adev->family = AMDGPU_FAMILY_RV;
1506 else
1507 adev->family = AMDGPU_FAMILY_AI;
460826e6
KW
1508
1509 r = soc15_set_ip_blocks(adev);
1510 if (r)
1511 return r;
1512 break;
d38ceaf9
AD
1513 default:
1514 /* FIXME: not supported yet */
1515 return -EINVAL;
1516 }
1517
e2a75f88
AD
1518 r = amdgpu_device_parse_gpu_info_fw(adev);
1519 if (r)
1520 return r;
1521
1884734a 1522 amdgpu_amdkfd_device_probe(adev);
1523
3149d9da
XY
1524 if (amdgpu_sriov_vf(adev)) {
1525 r = amdgpu_virt_request_full_gpu(adev, true);
1526 if (r)
5ffa61c1 1527 return -EAGAIN;
78d48112
TH
1528
1529 /* query the reg access mode at the very beginning */
1530 amdgpu_virt_init_reg_access_mode(adev);
3149d9da
XY
1531 }
1532
3b94fb10 1533 adev->pm.pp_feature = amdgpu_pp_feature_mask;
00544006
HR
1534 if (amdgpu_sriov_vf(adev))
1535 adev->pm.pp_feature &= ~PP_GFXOFF_MASK;
00f54b97 1536
394e9a14
ED
1537 /* Read BIOS */
1538 if (!amdgpu_get_bios(adev))
1539 return -EINVAL;
1540
1541 r = amdgpu_atombios_init(adev);
1542 if (r) {
1543 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
1544 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_INIT_FAIL, 0, 0);
1545 return r;
1546 }
1547
d38ceaf9
AD
1548 for (i = 0; i < adev->num_ip_blocks; i++) {
1549 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
ed8cf00c
HR
1550 DRM_ERROR("disabled ip block: %d <%s>\n",
1551 i, adev->ip_blocks[i].version->funcs->name);
a1255107 1552 adev->ip_blocks[i].status.valid = false;
d38ceaf9 1553 } else {
a1255107
AD
1554 if (adev->ip_blocks[i].version->funcs->early_init) {
1555 r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
2c1a2784 1556 if (r == -ENOENT) {
a1255107 1557 adev->ip_blocks[i].status.valid = false;
2c1a2784 1558 } else if (r) {
a1255107
AD
1559 DRM_ERROR("early_init of IP block <%s> failed %d\n",
1560 adev->ip_blocks[i].version->funcs->name, r);
d38ceaf9 1561 return r;
2c1a2784 1562 } else {
a1255107 1563 adev->ip_blocks[i].status.valid = true;
2c1a2784 1564 }
974e6b64 1565 } else {
a1255107 1566 adev->ip_blocks[i].status.valid = true;
d38ceaf9 1567 }
d38ceaf9
AD
1568 }
1569 }
1570
395d1fb9
NH
1571 adev->cg_flags &= amdgpu_cg_mask;
1572 adev->pg_flags &= amdgpu_pg_mask;
1573
d38ceaf9
AD
1574 return 0;
1575}
1576
0a4f2520
RZ
1577static int amdgpu_device_ip_hw_init_phase1(struct amdgpu_device *adev)
1578{
1579 int i, r;
1580
1581 for (i = 0; i < adev->num_ip_blocks; i++) {
1582 if (!adev->ip_blocks[i].status.sw)
1583 continue;
1584 if (adev->ip_blocks[i].status.hw)
1585 continue;
1586 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
2d11fd3f 1587 (amdgpu_sriov_vf(adev) && (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP)) ||
0a4f2520
RZ
1588 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH) {
1589 r = adev->ip_blocks[i].version->funcs->hw_init(adev);
1590 if (r) {
1591 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
1592 adev->ip_blocks[i].version->funcs->name, r);
1593 return r;
1594 }
1595 adev->ip_blocks[i].status.hw = true;
1596 }
1597 }
1598
1599 return 0;
1600}
1601
1602static int amdgpu_device_ip_hw_init_phase2(struct amdgpu_device *adev)
1603{
1604 int i, r;
1605
1606 for (i = 0; i < adev->num_ip_blocks; i++) {
1607 if (!adev->ip_blocks[i].status.sw)
1608 continue;
1609 if (adev->ip_blocks[i].status.hw)
1610 continue;
1611 r = adev->ip_blocks[i].version->funcs->hw_init(adev);
1612 if (r) {
1613 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
1614 adev->ip_blocks[i].version->funcs->name, r);
1615 return r;
1616 }
1617 adev->ip_blocks[i].status.hw = true;
1618 }
1619
1620 return 0;
1621}
1622
7a3e0bb2
RZ
1623static int amdgpu_device_fw_loading(struct amdgpu_device *adev)
1624{
1625 int r = 0;
1626 int i;
80f41f84 1627 uint32_t smu_version;
7a3e0bb2
RZ
1628
1629 if (adev->asic_type >= CHIP_VEGA10) {
1630 for (i = 0; i < adev->num_ip_blocks; i++) {
1631 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
1632 if (adev->in_gpu_reset || adev->in_suspend) {
1633 if (amdgpu_sriov_vf(adev) && adev->in_gpu_reset)
1634 break; /* sriov gpu reset, psp need to do hw_init before IH because of hw limit */
1635 r = adev->ip_blocks[i].version->funcs->resume(adev);
1636 if (r) {
1637 DRM_ERROR("resume of IP block <%s> failed %d\n",
1638 adev->ip_blocks[i].version->funcs->name, r);
1639 return r;
1640 }
1641 } else {
1642 r = adev->ip_blocks[i].version->funcs->hw_init(adev);
1643 if (r) {
1644 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
1645 adev->ip_blocks[i].version->funcs->name, r);
1646 return r;
1647 }
1648 }
1649 adev->ip_blocks[i].status.hw = true;
1650 }
1651 }
1652 }
80f41f84 1653 r = amdgpu_pm_load_smu_firmware(adev, &smu_version);
7a3e0bb2 1654
80f41f84 1655 return r;
7a3e0bb2
RZ
1656}
1657
e3ecdffa
AD
1658/**
1659 * amdgpu_device_ip_init - run init for hardware IPs
1660 *
1661 * @adev: amdgpu_device pointer
1662 *
1663 * Main initialization pass for hardware IPs. The list of all the hardware
1664 * IPs that make up the asic is walked and the sw_init and hw_init callbacks
1665 * are run. sw_init initializes the software state associated with each IP
1666 * and hw_init initializes the hardware associated with each IP.
1667 * Returns 0 on success, negative error code on failure.
1668 */
06ec9070 1669static int amdgpu_device_ip_init(struct amdgpu_device *adev)
d38ceaf9
AD
1670{
1671 int i, r;
1672
c030f2e4 1673 r = amdgpu_ras_init(adev);
1674 if (r)
1675 return r;
1676
d38ceaf9 1677 for (i = 0; i < adev->num_ip_blocks; i++) {
a1255107 1678 if (!adev->ip_blocks[i].status.valid)
d38ceaf9 1679 continue;
a1255107 1680 r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
2c1a2784 1681 if (r) {
a1255107
AD
1682 DRM_ERROR("sw_init of IP block <%s> failed %d\n",
1683 adev->ip_blocks[i].version->funcs->name, r);
72d3f592 1684 goto init_failed;
2c1a2784 1685 }
a1255107 1686 adev->ip_blocks[i].status.sw = true;
bfca0289 1687
d38ceaf9 1688 /* need to do gmc hw init early so we can allocate gpu mem */
a1255107 1689 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
06ec9070 1690 r = amdgpu_device_vram_scratch_init(adev);
2c1a2784
AD
1691 if (r) {
1692 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
72d3f592 1693 goto init_failed;
2c1a2784 1694 }
a1255107 1695 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
2c1a2784
AD
1696 if (r) {
1697 DRM_ERROR("hw_init %d failed %d\n", i, r);
72d3f592 1698 goto init_failed;
2c1a2784 1699 }
06ec9070 1700 r = amdgpu_device_wb_init(adev);
2c1a2784 1701 if (r) {
06ec9070 1702 DRM_ERROR("amdgpu_device_wb_init failed %d\n", r);
72d3f592 1703 goto init_failed;
2c1a2784 1704 }
a1255107 1705 adev->ip_blocks[i].status.hw = true;
2493664f
ML
1706
1707 /* right after GMC hw init, we create CSA */
1708 if (amdgpu_sriov_vf(adev)) {
1e256e27
RZ
1709 r = amdgpu_allocate_static_csa(adev, &adev->virt.csa_obj,
1710 AMDGPU_GEM_DOMAIN_VRAM,
1711 AMDGPU_CSA_SIZE);
2493664f
ML
1712 if (r) {
1713 DRM_ERROR("allocate CSA failed %d\n", r);
72d3f592 1714 goto init_failed;
2493664f
ML
1715 }
1716 }
d38ceaf9
AD
1717 }
1718 }
1719
533aed27
AG
1720 r = amdgpu_ib_pool_init(adev);
1721 if (r) {
1722 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
1723 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r);
1724 goto init_failed;
1725 }
1726
c8963ea4
RZ
1727 r = amdgpu_ucode_create_bo(adev); /* create ucode bo when sw_init complete*/
1728 if (r)
72d3f592 1729 goto init_failed;
0a4f2520
RZ
1730
1731 r = amdgpu_device_ip_hw_init_phase1(adev);
1732 if (r)
72d3f592 1733 goto init_failed;
0a4f2520 1734
7a3e0bb2
RZ
1735 r = amdgpu_device_fw_loading(adev);
1736 if (r)
72d3f592 1737 goto init_failed;
7a3e0bb2 1738
0a4f2520
RZ
1739 r = amdgpu_device_ip_hw_init_phase2(adev);
1740 if (r)
72d3f592 1741 goto init_failed;
d38ceaf9 1742
3e2e2ab5
HZ
1743 if (adev->gmc.xgmi.num_physical_nodes > 1)
1744 amdgpu_xgmi_add_device(adev);
1884734a 1745 amdgpu_amdkfd_device_init(adev);
c6332b97 1746
72d3f592 1747init_failed:
d3c117e5 1748 if (amdgpu_sriov_vf(adev)) {
72d3f592
ED
1749 if (!r)
1750 amdgpu_virt_init_data_exchange(adev);
c6332b97 1751 amdgpu_virt_release_full_gpu(adev, true);
d3c117e5 1752 }
c6332b97 1753
72d3f592 1754 return r;
d38ceaf9
AD
1755}
1756
e3ecdffa
AD
1757/**
1758 * amdgpu_device_fill_reset_magic - writes reset magic to gart pointer
1759 *
1760 * @adev: amdgpu_device pointer
1761 *
1762 * Writes a reset magic value to the gart pointer in VRAM. The driver calls
1763 * this function before a GPU reset. If the value is retained after a
1764 * GPU reset, VRAM has not been lost. Some GPU resets may destry VRAM contents.
1765 */
06ec9070 1766static void amdgpu_device_fill_reset_magic(struct amdgpu_device *adev)
0c49e0b8
CZ
1767{
1768 memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM);
1769}
1770
e3ecdffa
AD
1771/**
1772 * amdgpu_device_check_vram_lost - check if vram is valid
1773 *
1774 * @adev: amdgpu_device pointer
1775 *
1776 * Checks the reset magic value written to the gart pointer in VRAM.
1777 * The driver calls this after a GPU reset to see if the contents of
1778 * VRAM is lost or now.
1779 * returns true if vram is lost, false if not.
1780 */
06ec9070 1781static bool amdgpu_device_check_vram_lost(struct amdgpu_device *adev)
0c49e0b8
CZ
1782{
1783 return !!memcmp(adev->gart.ptr, adev->reset_magic,
1784 AMDGPU_RESET_MAGIC_NUM);
1785}
1786
e3ecdffa 1787/**
1112a46b 1788 * amdgpu_device_set_cg_state - set clockgating for amdgpu device
e3ecdffa
AD
1789 *
1790 * @adev: amdgpu_device pointer
1791 *
e3ecdffa 1792 * The list of all the hardware IPs that make up the asic is walked and the
1112a46b
RZ
1793 * set_clockgating_state callbacks are run.
1794 * Late initialization pass enabling clockgating for hardware IPs.
1795 * Fini or suspend, pass disabling clockgating for hardware IPs.
e3ecdffa
AD
1796 * Returns 0 on success, negative error code on failure.
1797 */
fdd34271 1798
1112a46b
RZ
1799static int amdgpu_device_set_cg_state(struct amdgpu_device *adev,
1800 enum amd_clockgating_state state)
d38ceaf9 1801{
1112a46b 1802 int i, j, r;
d38ceaf9 1803
4a2ba394
SL
1804 if (amdgpu_emu_mode == 1)
1805 return 0;
1806
1112a46b
RZ
1807 for (j = 0; j < adev->num_ip_blocks; j++) {
1808 i = state == AMD_CG_STATE_GATE ? j : adev->num_ip_blocks - j - 1;
a2d31dc3 1809 if (!adev->ip_blocks[i].status.late_initialized)
d38ceaf9 1810 continue;
4a446d55 1811 /* skip CG for VCE/UVD, it's handled specially */
a1255107 1812 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
57716327 1813 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
34319b32 1814 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCN &&
57716327 1815 adev->ip_blocks[i].version->funcs->set_clockgating_state) {
4a446d55 1816 /* enable clockgating to save power */
a1255107 1817 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1112a46b 1818 state);
4a446d55
AD
1819 if (r) {
1820 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
a1255107 1821 adev->ip_blocks[i].version->funcs->name, r);
4a446d55
AD
1822 return r;
1823 }
b0b00ff1 1824 }
d38ceaf9 1825 }
06b18f61 1826
c9f96fd5
RZ
1827 return 0;
1828}
1829
1112a46b 1830static int amdgpu_device_set_pg_state(struct amdgpu_device *adev, enum amd_powergating_state state)
c9f96fd5 1831{
1112a46b 1832 int i, j, r;
06b18f61 1833
c9f96fd5
RZ
1834 if (amdgpu_emu_mode == 1)
1835 return 0;
1836
1112a46b
RZ
1837 for (j = 0; j < adev->num_ip_blocks; j++) {
1838 i = state == AMD_PG_STATE_GATE ? j : adev->num_ip_blocks - j - 1;
a2d31dc3 1839 if (!adev->ip_blocks[i].status.late_initialized)
c9f96fd5
RZ
1840 continue;
1841 /* skip CG for VCE/UVD, it's handled specially */
1842 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1843 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
1844 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCN &&
1845 adev->ip_blocks[i].version->funcs->set_powergating_state) {
1846 /* enable powergating to save power */
1847 r = adev->ip_blocks[i].version->funcs->set_powergating_state((void *)adev,
1112a46b 1848 state);
c9f96fd5
RZ
1849 if (r) {
1850 DRM_ERROR("set_powergating_state(gate) of IP block <%s> failed %d\n",
1851 adev->ip_blocks[i].version->funcs->name, r);
1852 return r;
1853 }
1854 }
1855 }
2dc80b00
S
1856 return 0;
1857}
1858
beff74bc
AD
1859static int amdgpu_device_enable_mgpu_fan_boost(void)
1860{
1861 struct amdgpu_gpu_instance *gpu_ins;
1862 struct amdgpu_device *adev;
1863 int i, ret = 0;
1864
1865 mutex_lock(&mgpu_info.mutex);
1866
1867 /*
1868 * MGPU fan boost feature should be enabled
1869 * only when there are two or more dGPUs in
1870 * the system
1871 */
1872 if (mgpu_info.num_dgpu < 2)
1873 goto out;
1874
1875 for (i = 0; i < mgpu_info.num_dgpu; i++) {
1876 gpu_ins = &(mgpu_info.gpu_ins[i]);
1877 adev = gpu_ins->adev;
1878 if (!(adev->flags & AMD_IS_APU) &&
1879 !gpu_ins->mgpu_fan_enabled &&
1880 adev->powerplay.pp_funcs &&
1881 adev->powerplay.pp_funcs->enable_mgpu_fan_boost) {
1882 ret = amdgpu_dpm_enable_mgpu_fan_boost(adev);
1883 if (ret)
1884 break;
1885
1886 gpu_ins->mgpu_fan_enabled = 1;
1887 }
1888 }
1889
1890out:
1891 mutex_unlock(&mgpu_info.mutex);
1892
1893 return ret;
1894}
1895
e3ecdffa
AD
1896/**
1897 * amdgpu_device_ip_late_init - run late init for hardware IPs
1898 *
1899 * @adev: amdgpu_device pointer
1900 *
1901 * Late initialization pass for hardware IPs. The list of all the hardware
1902 * IPs that make up the asic is walked and the late_init callbacks are run.
1903 * late_init covers any special initialization that an IP requires
1904 * after all of the have been initialized or something that needs to happen
1905 * late in the init process.
1906 * Returns 0 on success, negative error code on failure.
1907 */
06ec9070 1908static int amdgpu_device_ip_late_init(struct amdgpu_device *adev)
2dc80b00
S
1909{
1910 int i = 0, r;
1911
1912 for (i = 0; i < adev->num_ip_blocks; i++) {
73f847db 1913 if (!adev->ip_blocks[i].status.hw)
2dc80b00
S
1914 continue;
1915 if (adev->ip_blocks[i].version->funcs->late_init) {
1916 r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
1917 if (r) {
1918 DRM_ERROR("late_init of IP block <%s> failed %d\n",
1919 adev->ip_blocks[i].version->funcs->name, r);
1920 return r;
1921 }
2dc80b00 1922 }
73f847db 1923 adev->ip_blocks[i].status.late_initialized = true;
2dc80b00
S
1924 }
1925
1112a46b
RZ
1926 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_GATE);
1927 amdgpu_device_set_pg_state(adev, AMD_PG_STATE_GATE);
916ac57f 1928
06ec9070 1929 amdgpu_device_fill_reset_magic(adev);
d38ceaf9 1930
beff74bc
AD
1931 r = amdgpu_device_enable_mgpu_fan_boost();
1932 if (r)
1933 DRM_ERROR("enable mgpu fan boost failed (%d).\n", r);
1934
1935 /* set to low pstate by default */
1936 amdgpu_xgmi_set_pstate(adev, 0);
1937
d38ceaf9
AD
1938 return 0;
1939}
1940
e3ecdffa
AD
1941/**
1942 * amdgpu_device_ip_fini - run fini for hardware IPs
1943 *
1944 * @adev: amdgpu_device pointer
1945 *
1946 * Main teardown pass for hardware IPs. The list of all the hardware
1947 * IPs that make up the asic is walked and the hw_fini and sw_fini callbacks
1948 * are run. hw_fini tears down the hardware associated with each IP
1949 * and sw_fini tears down any software state associated with each IP.
1950 * Returns 0 on success, negative error code on failure.
1951 */
06ec9070 1952static int amdgpu_device_ip_fini(struct amdgpu_device *adev)
d38ceaf9
AD
1953{
1954 int i, r;
1955
c030f2e4 1956 amdgpu_ras_pre_fini(adev);
1957
a82400b5
AG
1958 if (adev->gmc.xgmi.num_physical_nodes > 1)
1959 amdgpu_xgmi_remove_device(adev);
1960
1884734a 1961 amdgpu_amdkfd_device_fini(adev);
05df1f01
RZ
1962
1963 amdgpu_device_set_pg_state(adev, AMD_PG_STATE_UNGATE);
fdd34271
RZ
1964 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_UNGATE);
1965
3e96dbfd
AD
1966 /* need to disable SMC first */
1967 for (i = 0; i < adev->num_ip_blocks; i++) {
a1255107 1968 if (!adev->ip_blocks[i].status.hw)
3e96dbfd 1969 continue;
fdd34271 1970 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
a1255107 1971 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
3e96dbfd
AD
1972 /* XXX handle errors */
1973 if (r) {
1974 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
a1255107 1975 adev->ip_blocks[i].version->funcs->name, r);
3e96dbfd 1976 }
a1255107 1977 adev->ip_blocks[i].status.hw = false;
3e96dbfd
AD
1978 break;
1979 }
1980 }
1981
d38ceaf9 1982 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
a1255107 1983 if (!adev->ip_blocks[i].status.hw)
d38ceaf9 1984 continue;
8201a67a 1985
a1255107 1986 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
d38ceaf9 1987 /* XXX handle errors */
2c1a2784 1988 if (r) {
a1255107
AD
1989 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
1990 adev->ip_blocks[i].version->funcs->name, r);
2c1a2784 1991 }
8201a67a 1992
a1255107 1993 adev->ip_blocks[i].status.hw = false;
d38ceaf9
AD
1994 }
1995
9950cda2 1996
d38ceaf9 1997 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
a1255107 1998 if (!adev->ip_blocks[i].status.sw)
d38ceaf9 1999 continue;
c12aba3a
ML
2000
2001 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
c8963ea4 2002 amdgpu_ucode_free_bo(adev);
1e256e27 2003 amdgpu_free_static_csa(&adev->virt.csa_obj);
c12aba3a
ML
2004 amdgpu_device_wb_fini(adev);
2005 amdgpu_device_vram_scratch_fini(adev);
533aed27 2006 amdgpu_ib_pool_fini(adev);
c12aba3a
ML
2007 }
2008
a1255107 2009 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
d38ceaf9 2010 /* XXX handle errors */
2c1a2784 2011 if (r) {
a1255107
AD
2012 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
2013 adev->ip_blocks[i].version->funcs->name, r);
2c1a2784 2014 }
a1255107
AD
2015 adev->ip_blocks[i].status.sw = false;
2016 adev->ip_blocks[i].status.valid = false;
d38ceaf9
AD
2017 }
2018
a6dcfd9c 2019 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
a1255107 2020 if (!adev->ip_blocks[i].status.late_initialized)
8a2eef1d 2021 continue;
a1255107
AD
2022 if (adev->ip_blocks[i].version->funcs->late_fini)
2023 adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
2024 adev->ip_blocks[i].status.late_initialized = false;
a6dcfd9c
ML
2025 }
2026
c030f2e4 2027 amdgpu_ras_fini(adev);
2028
030308fc 2029 if (amdgpu_sriov_vf(adev))
24136135
ML
2030 if (amdgpu_virt_release_full_gpu(adev, false))
2031 DRM_ERROR("failed to release exclusive mode on fini\n");
2493664f 2032
d38ceaf9
AD
2033 return 0;
2034}
2035
e3ecdffa 2036/**
beff74bc 2037 * amdgpu_device_delayed_init_work_handler - work handler for IB tests
e3ecdffa 2038 *
1112a46b 2039 * @work: work_struct.
e3ecdffa 2040 */
beff74bc 2041static void amdgpu_device_delayed_init_work_handler(struct work_struct *work)
2dc80b00
S
2042{
2043 struct amdgpu_device *adev =
beff74bc 2044 container_of(work, struct amdgpu_device, delayed_init_work.work);
916ac57f
RZ
2045 int r;
2046
2047 r = amdgpu_ib_ring_tests(adev);
2048 if (r)
2049 DRM_ERROR("ib ring test failed (%d).\n", r);
2dc80b00
S
2050}
2051
1e317b99
RZ
2052static void amdgpu_device_delay_enable_gfx_off(struct work_struct *work)
2053{
2054 struct amdgpu_device *adev =
2055 container_of(work, struct amdgpu_device, gfx.gfx_off_delay_work.work);
2056
2057 mutex_lock(&adev->gfx.gfx_off_mutex);
2058 if (!adev->gfx.gfx_off_state && !adev->gfx.gfx_off_req_count) {
2059 if (!amdgpu_dpm_set_powergating_by_smu(adev, AMD_IP_BLOCK_TYPE_GFX, true))
2060 adev->gfx.gfx_off_state = true;
2061 }
2062 mutex_unlock(&adev->gfx.gfx_off_mutex);
2063}
2064
e3ecdffa 2065/**
e7854a03 2066 * amdgpu_device_ip_suspend_phase1 - run suspend for hardware IPs (phase 1)
e3ecdffa
AD
2067 *
2068 * @adev: amdgpu_device pointer
2069 *
2070 * Main suspend function for hardware IPs. The list of all the hardware
2071 * IPs that make up the asic is walked, clockgating is disabled and the
2072 * suspend callbacks are run. suspend puts the hardware and software state
2073 * in each IP into a state suitable for suspend.
2074 * Returns 0 on success, negative error code on failure.
2075 */
e7854a03
AD
2076static int amdgpu_device_ip_suspend_phase1(struct amdgpu_device *adev)
2077{
2078 int i, r;
2079
05df1f01 2080 amdgpu_device_set_pg_state(adev, AMD_PG_STATE_UNGATE);
fdd34271 2081 amdgpu_device_set_cg_state(adev, AMD_CG_STATE_UNGATE);
05df1f01 2082
e7854a03
AD
2083 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2084 if (!adev->ip_blocks[i].status.valid)
2085 continue;
2086 /* displays are handled separately */
2087 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) {
e7854a03
AD
2088 /* XXX handle errors */
2089 r = adev->ip_blocks[i].version->funcs->suspend(adev);
2090 /* XXX handle errors */
2091 if (r) {
2092 DRM_ERROR("suspend of IP block <%s> failed %d\n",
2093 adev->ip_blocks[i].version->funcs->name, r);
2094 }
2095 }
2096 }
2097
e7854a03
AD
2098 return 0;
2099}
2100
2101/**
2102 * amdgpu_device_ip_suspend_phase2 - run suspend for hardware IPs (phase 2)
2103 *
2104 * @adev: amdgpu_device pointer
2105 *
2106 * Main suspend function for hardware IPs. The list of all the hardware
2107 * IPs that make up the asic is walked, clockgating is disabled and the
2108 * suspend callbacks are run. suspend puts the hardware and software state
2109 * in each IP into a state suitable for suspend.
2110 * Returns 0 on success, negative error code on failure.
2111 */
2112static int amdgpu_device_ip_suspend_phase2(struct amdgpu_device *adev)
d38ceaf9
AD
2113{
2114 int i, r;
2115
2116 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
a1255107 2117 if (!adev->ip_blocks[i].status.valid)
d38ceaf9 2118 continue;
e7854a03
AD
2119 /* displays are handled in phase1 */
2120 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE)
2121 continue;
d38ceaf9 2122 /* XXX handle errors */
a1255107 2123 r = adev->ip_blocks[i].version->funcs->suspend(adev);
d38ceaf9 2124 /* XXX handle errors */
2c1a2784 2125 if (r) {
a1255107
AD
2126 DRM_ERROR("suspend of IP block <%s> failed %d\n",
2127 adev->ip_blocks[i].version->funcs->name, r);
2c1a2784 2128 }
d38ceaf9
AD
2129 }
2130
2131 return 0;
2132}
2133
e7854a03
AD
2134/**
2135 * amdgpu_device_ip_suspend - run suspend for hardware IPs
2136 *
2137 * @adev: amdgpu_device pointer
2138 *
2139 * Main suspend function for hardware IPs. The list of all the hardware
2140 * IPs that make up the asic is walked, clockgating is disabled and the
2141 * suspend callbacks are run. suspend puts the hardware and software state
2142 * in each IP into a state suitable for suspend.
2143 * Returns 0 on success, negative error code on failure.
2144 */
2145int amdgpu_device_ip_suspend(struct amdgpu_device *adev)
2146{
2147 int r;
2148
e7819644
YT
2149 if (amdgpu_sriov_vf(adev))
2150 amdgpu_virt_request_full_gpu(adev, false);
2151
e7854a03
AD
2152 r = amdgpu_device_ip_suspend_phase1(adev);
2153 if (r)
2154 return r;
2155 r = amdgpu_device_ip_suspend_phase2(adev);
2156
e7819644
YT
2157 if (amdgpu_sriov_vf(adev))
2158 amdgpu_virt_release_full_gpu(adev, false);
2159
e7854a03
AD
2160 return r;
2161}
2162
06ec9070 2163static int amdgpu_device_ip_reinit_early_sriov(struct amdgpu_device *adev)
a90ad3c2
ML
2164{
2165 int i, r;
2166
2cb681b6
ML
2167 static enum amd_ip_block_type ip_order[] = {
2168 AMD_IP_BLOCK_TYPE_GMC,
2169 AMD_IP_BLOCK_TYPE_COMMON,
39186aef 2170 AMD_IP_BLOCK_TYPE_PSP,
2cb681b6
ML
2171 AMD_IP_BLOCK_TYPE_IH,
2172 };
a90ad3c2 2173
2cb681b6
ML
2174 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
2175 int j;
2176 struct amdgpu_ip_block *block;
a90ad3c2 2177
2cb681b6
ML
2178 for (j = 0; j < adev->num_ip_blocks; j++) {
2179 block = &adev->ip_blocks[j];
2180
2181 if (block->version->type != ip_order[i] ||
2182 !block->status.valid)
2183 continue;
2184
2185 r = block->version->funcs->hw_init(adev);
0aaeefcc 2186 DRM_INFO("RE-INIT-early: %s %s\n", block->version->funcs->name, r?"failed":"succeeded");
c41d1cf6
ML
2187 if (r)
2188 return r;
a90ad3c2
ML
2189 }
2190 }
2191
2192 return 0;
2193}
2194
06ec9070 2195static int amdgpu_device_ip_reinit_late_sriov(struct amdgpu_device *adev)
a90ad3c2
ML
2196{
2197 int i, r;
2198
2cb681b6
ML
2199 static enum amd_ip_block_type ip_order[] = {
2200 AMD_IP_BLOCK_TYPE_SMC,
2201 AMD_IP_BLOCK_TYPE_DCE,
2202 AMD_IP_BLOCK_TYPE_GFX,
2203 AMD_IP_BLOCK_TYPE_SDMA,
257deb8c
FM
2204 AMD_IP_BLOCK_TYPE_UVD,
2205 AMD_IP_BLOCK_TYPE_VCE
2cb681b6 2206 };
a90ad3c2 2207
2cb681b6
ML
2208 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
2209 int j;
2210 struct amdgpu_ip_block *block;
a90ad3c2 2211
2cb681b6
ML
2212 for (j = 0; j < adev->num_ip_blocks; j++) {
2213 block = &adev->ip_blocks[j];
2214
2215 if (block->version->type != ip_order[i] ||
2216 !block->status.valid)
2217 continue;
2218
2219 r = block->version->funcs->hw_init(adev);
0aaeefcc 2220 DRM_INFO("RE-INIT-late: %s %s\n", block->version->funcs->name, r?"failed":"succeeded");
c41d1cf6
ML
2221 if (r)
2222 return r;
a90ad3c2
ML
2223 }
2224 }
2225
2226 return 0;
2227}
2228
e3ecdffa
AD
2229/**
2230 * amdgpu_device_ip_resume_phase1 - run resume for hardware IPs
2231 *
2232 * @adev: amdgpu_device pointer
2233 *
2234 * First resume function for hardware IPs. The list of all the hardware
2235 * IPs that make up the asic is walked and the resume callbacks are run for
2236 * COMMON, GMC, and IH. resume puts the hardware into a functional state
2237 * after a suspend and updates the software state as necessary. This
2238 * function is also used for restoring the GPU after a GPU reset.
2239 * Returns 0 on success, negative error code on failure.
2240 */
06ec9070 2241static int amdgpu_device_ip_resume_phase1(struct amdgpu_device *adev)
d38ceaf9
AD
2242{
2243 int i, r;
2244
a90ad3c2
ML
2245 for (i = 0; i < adev->num_ip_blocks; i++) {
2246 if (!adev->ip_blocks[i].status.valid)
2247 continue;
a90ad3c2 2248 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
e3ecdffa
AD
2249 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
2250 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH) {
fcf0649f
CZ
2251 r = adev->ip_blocks[i].version->funcs->resume(adev);
2252 if (r) {
2253 DRM_ERROR("resume of IP block <%s> failed %d\n",
2254 adev->ip_blocks[i].version->funcs->name, r);
2255 return r;
2256 }
a90ad3c2
ML
2257 }
2258 }
2259
2260 return 0;
2261}
2262
e3ecdffa
AD
2263/**
2264 * amdgpu_device_ip_resume_phase2 - run resume for hardware IPs
2265 *
2266 * @adev: amdgpu_device pointer
2267 *
2268 * First resume function for hardware IPs. The list of all the hardware
2269 * IPs that make up the asic is walked and the resume callbacks are run for
2270 * all blocks except COMMON, GMC, and IH. resume puts the hardware into a
2271 * functional state after a suspend and updates the software state as
2272 * necessary. This function is also used for restoring the GPU after a GPU
2273 * reset.
2274 * Returns 0 on success, negative error code on failure.
2275 */
06ec9070 2276static int amdgpu_device_ip_resume_phase2(struct amdgpu_device *adev)
d38ceaf9
AD
2277{
2278 int i, r;
2279
2280 for (i = 0; i < adev->num_ip_blocks; i++) {
a1255107 2281 if (!adev->ip_blocks[i].status.valid)
d38ceaf9 2282 continue;
fcf0649f 2283 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
e3ecdffa 2284 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
7a3e0bb2
RZ
2285 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH ||
2286 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP)
fcf0649f 2287 continue;
a1255107 2288 r = adev->ip_blocks[i].version->funcs->resume(adev);
2c1a2784 2289 if (r) {
a1255107
AD
2290 DRM_ERROR("resume of IP block <%s> failed %d\n",
2291 adev->ip_blocks[i].version->funcs->name, r);
d38ceaf9 2292 return r;
2c1a2784 2293 }
d38ceaf9
AD
2294 }
2295
2296 return 0;
2297}
2298
e3ecdffa
AD
2299/**
2300 * amdgpu_device_ip_resume - run resume for hardware IPs
2301 *
2302 * @adev: amdgpu_device pointer
2303 *
2304 * Main resume function for hardware IPs. The hardware IPs
2305 * are split into two resume functions because they are
2306 * are also used in in recovering from a GPU reset and some additional
2307 * steps need to be take between them. In this case (S3/S4) they are
2308 * run sequentially.
2309 * Returns 0 on success, negative error code on failure.
2310 */
06ec9070 2311static int amdgpu_device_ip_resume(struct amdgpu_device *adev)
fcf0649f
CZ
2312{
2313 int r;
2314
06ec9070 2315 r = amdgpu_device_ip_resume_phase1(adev);
fcf0649f
CZ
2316 if (r)
2317 return r;
7a3e0bb2
RZ
2318
2319 r = amdgpu_device_fw_loading(adev);
2320 if (r)
2321 return r;
2322
06ec9070 2323 r = amdgpu_device_ip_resume_phase2(adev);
fcf0649f
CZ
2324
2325 return r;
2326}
2327
e3ecdffa
AD
2328/**
2329 * amdgpu_device_detect_sriov_bios - determine if the board supports SR-IOV
2330 *
2331 * @adev: amdgpu_device pointer
2332 *
2333 * Query the VBIOS data tables to determine if the board supports SR-IOV.
2334 */
4e99a44e 2335static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
048765ad 2336{
6867e1b5
ML
2337 if (amdgpu_sriov_vf(adev)) {
2338 if (adev->is_atom_fw) {
2339 if (amdgpu_atomfirmware_gpu_supports_virtualization(adev))
2340 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
2341 } else {
2342 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
2343 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
2344 }
2345
2346 if (!(adev->virt.caps & AMDGPU_SRIOV_CAPS_SRIOV_VBIOS))
2347 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_NO_VBIOS, 0, 0);
a5bde2f9 2348 }
048765ad
AR
2349}
2350
e3ecdffa
AD
2351/**
2352 * amdgpu_device_asic_has_dc_support - determine if DC supports the asic
2353 *
2354 * @asic_type: AMD asic type
2355 *
2356 * Check if there is DC (new modesetting infrastructre) support for an asic.
2357 * returns true if DC has support, false if not.
2358 */
4562236b
HW
2359bool amdgpu_device_asic_has_dc_support(enum amd_asic_type asic_type)
2360{
2361 switch (asic_type) {
2362#if defined(CONFIG_DRM_AMD_DC)
2363 case CHIP_BONAIRE:
0d6fbccb 2364 case CHIP_KAVERI:
367e6687
AD
2365 case CHIP_KABINI:
2366 case CHIP_MULLINS:
d9fda248
HW
2367 /*
2368 * We have systems in the wild with these ASICs that require
2369 * LVDS and VGA support which is not supported with DC.
2370 *
2371 * Fallback to the non-DC driver here by default so as not to
2372 * cause regressions.
2373 */
2374 return amdgpu_dc > 0;
2375 case CHIP_HAWAII:
4562236b
HW
2376 case CHIP_CARRIZO:
2377 case CHIP_STONEY:
4562236b 2378 case CHIP_POLARIS10:
675fd32b 2379 case CHIP_POLARIS11:
2c8ad2d5 2380 case CHIP_POLARIS12:
675fd32b 2381 case CHIP_VEGAM:
4562236b
HW
2382 case CHIP_TONGA:
2383 case CHIP_FIJI:
42f8ffa1 2384 case CHIP_VEGA10:
dca7b401 2385 case CHIP_VEGA12:
c6034aa2 2386 case CHIP_VEGA20:
dc37a9a0 2387#if defined(CONFIG_DRM_AMD_DC_DCN1_0)
fd187853 2388 case CHIP_RAVEN:
42f8ffa1 2389#endif
fd187853 2390 return amdgpu_dc != 0;
4562236b
HW
2391#endif
2392 default:
2393 return false;
2394 }
2395}
2396
2397/**
2398 * amdgpu_device_has_dc_support - check if dc is supported
2399 *
2400 * @adev: amdgpu_device_pointer
2401 *
2402 * Returns true for supported, false for not supported
2403 */
2404bool amdgpu_device_has_dc_support(struct amdgpu_device *adev)
2405{
2555039d
XY
2406 if (amdgpu_sriov_vf(adev))
2407 return false;
2408
4562236b
HW
2409 return amdgpu_device_asic_has_dc_support(adev->asic_type);
2410}
2411
d4535e2c
AG
2412
2413static void amdgpu_device_xgmi_reset_func(struct work_struct *__work)
2414{
2415 struct amdgpu_device *adev =
2416 container_of(__work, struct amdgpu_device, xgmi_reset_work);
2417
2418 adev->asic_reset_res = amdgpu_asic_reset(adev);
2419 if (adev->asic_reset_res)
fed184e9 2420 DRM_WARN("ASIC reset failed with error, %d for drm dev, %s",
d4535e2c
AG
2421 adev->asic_reset_res, adev->ddev->unique);
2422}
2423
2424
d38ceaf9
AD
2425/**
2426 * amdgpu_device_init - initialize the driver
2427 *
2428 * @adev: amdgpu_device pointer
87e3f136 2429 * @ddev: drm dev pointer
d38ceaf9
AD
2430 * @pdev: pci dev pointer
2431 * @flags: driver flags
2432 *
2433 * Initializes the driver info and hw (all asics).
2434 * Returns 0 for success or an error on failure.
2435 * Called at driver startup.
2436 */
2437int amdgpu_device_init(struct amdgpu_device *adev,
2438 struct drm_device *ddev,
2439 struct pci_dev *pdev,
2440 uint32_t flags)
2441{
2442 int r, i;
2443 bool runtime = false;
95844d20 2444 u32 max_MBps;
d38ceaf9
AD
2445
2446 adev->shutdown = false;
2447 adev->dev = &pdev->dev;
2448 adev->ddev = ddev;
2449 adev->pdev = pdev;
2450 adev->flags = flags;
2f7d10b3 2451 adev->asic_type = flags & AMD_ASIC_MASK;
d38ceaf9 2452 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
593aa2d2
SL
2453 if (amdgpu_emu_mode == 1)
2454 adev->usec_timeout *= 2;
770d13b1 2455 adev->gmc.gart_size = 512 * 1024 * 1024;
d38ceaf9
AD
2456 adev->accel_working = false;
2457 adev->num_rings = 0;
2458 adev->mman.buffer_funcs = NULL;
2459 adev->mman.buffer_funcs_ring = NULL;
2460 adev->vm_manager.vm_pte_funcs = NULL;
3798e9a6 2461 adev->vm_manager.vm_pte_num_rqs = 0;
132f34e4 2462 adev->gmc.gmc_funcs = NULL;
f54d1867 2463 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
b8866c26 2464 bitmap_zero(adev->gfx.pipe_reserve_bitmap, AMDGPU_MAX_COMPUTE_QUEUES);
d38ceaf9
AD
2465
2466 adev->smc_rreg = &amdgpu_invalid_rreg;
2467 adev->smc_wreg = &amdgpu_invalid_wreg;
2468 adev->pcie_rreg = &amdgpu_invalid_rreg;
2469 adev->pcie_wreg = &amdgpu_invalid_wreg;
36b9a952
HR
2470 adev->pciep_rreg = &amdgpu_invalid_rreg;
2471 adev->pciep_wreg = &amdgpu_invalid_wreg;
d38ceaf9
AD
2472 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
2473 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
2474 adev->didt_rreg = &amdgpu_invalid_rreg;
2475 adev->didt_wreg = &amdgpu_invalid_wreg;
ccdbb20a
RZ
2476 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
2477 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
d38ceaf9
AD
2478 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
2479 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
2480
3e39ab90
AD
2481 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
2482 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
2483 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
d38ceaf9
AD
2484
2485 /* mutex initialization are all done here so we
2486 * can recall function without having locking issues */
d38ceaf9 2487 atomic_set(&adev->irq.ih.lock, 0);
0e5ca0d1 2488 mutex_init(&adev->firmware.mutex);
d38ceaf9
AD
2489 mutex_init(&adev->pm.mutex);
2490 mutex_init(&adev->gfx.gpu_clock_mutex);
2491 mutex_init(&adev->srbm_mutex);
b8866c26 2492 mutex_init(&adev->gfx.pipe_reserve_mutex);
d23ee13f 2493 mutex_init(&adev->gfx.gfx_off_mutex);
d38ceaf9 2494 mutex_init(&adev->grbm_idx_mutex);
d38ceaf9 2495 mutex_init(&adev->mn_lock);
e23b74aa 2496 mutex_init(&adev->virt.vf_errors.lock);
d38ceaf9 2497 hash_init(adev->mn_hash);
13a752e3 2498 mutex_init(&adev->lock_reset);
bb5a2bdf 2499 mutex_init(&adev->virt.dpm_mutex);
d38ceaf9 2500
912dfc84
EQ
2501 r = amdgpu_device_check_arguments(adev);
2502 if (r)
2503 return r;
d38ceaf9 2504
d38ceaf9
AD
2505 spin_lock_init(&adev->mmio_idx_lock);
2506 spin_lock_init(&adev->smc_idx_lock);
2507 spin_lock_init(&adev->pcie_idx_lock);
2508 spin_lock_init(&adev->uvd_ctx_idx_lock);
2509 spin_lock_init(&adev->didt_idx_lock);
ccdbb20a 2510 spin_lock_init(&adev->gc_cac_idx_lock);
16abb5d2 2511 spin_lock_init(&adev->se_cac_idx_lock);
d38ceaf9 2512 spin_lock_init(&adev->audio_endpt_idx_lock);
95844d20 2513 spin_lock_init(&adev->mm_stats.lock);
d38ceaf9 2514
0c4e7fa5
CZ
2515 INIT_LIST_HEAD(&adev->shadow_list);
2516 mutex_init(&adev->shadow_list_lock);
2517
795f2813
AR
2518 INIT_LIST_HEAD(&adev->ring_lru_list);
2519 spin_lock_init(&adev->ring_lru_list_lock);
2520
beff74bc
AD
2521 INIT_DELAYED_WORK(&adev->delayed_init_work,
2522 amdgpu_device_delayed_init_work_handler);
1e317b99
RZ
2523 INIT_DELAYED_WORK(&adev->gfx.gfx_off_delay_work,
2524 amdgpu_device_delay_enable_gfx_off);
2dc80b00 2525
d4535e2c
AG
2526 INIT_WORK(&adev->xgmi_reset_work, amdgpu_device_xgmi_reset_func);
2527
d23ee13f 2528 adev->gfx.gfx_off_req_count = 1;
b1ddf548
RZ
2529 adev->pm.ac_power = power_supply_is_system_supplied() > 0 ? true : false;
2530
0fa49558
AX
2531 /* Registers mapping */
2532 /* TODO: block userspace mapping of io register */
da69c161
KW
2533 if (adev->asic_type >= CHIP_BONAIRE) {
2534 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
2535 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
2536 } else {
2537 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
2538 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
2539 }
d38ceaf9 2540
d38ceaf9
AD
2541 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
2542 if (adev->rmmio == NULL) {
2543 return -ENOMEM;
2544 }
2545 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
2546 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
2547
d38ceaf9
AD
2548 /* io port mapping */
2549 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
2550 if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
2551 adev->rio_mem_size = pci_resource_len(adev->pdev, i);
2552 adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
2553 break;
2554 }
2555 }
2556 if (adev->rio_mem == NULL)
b64a18c5 2557 DRM_INFO("PCI I/O BAR is not found.\n");
d38ceaf9 2558
5494d864
AD
2559 amdgpu_device_get_pcie_info(adev);
2560
d38ceaf9 2561 /* early init functions */
06ec9070 2562 r = amdgpu_device_ip_early_init(adev);
d38ceaf9
AD
2563 if (r)
2564 return r;
2565
6585661d
OZ
2566 /* doorbell bar mapping and doorbell index init*/
2567 amdgpu_device_doorbell_init(adev);
2568
d38ceaf9
AD
2569 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
2570 /* this will fail for cards that aren't VGA class devices, just
2571 * ignore it */
06ec9070 2572 vga_client_register(adev->pdev, adev, NULL, amdgpu_device_vga_set_decode);
d38ceaf9 2573
e9bef455 2574 if (amdgpu_device_is_px(ddev))
d38ceaf9 2575 runtime = true;
84c8b22e
LW
2576 if (!pci_is_thunderbolt_attached(adev->pdev))
2577 vga_switcheroo_register_client(adev->pdev,
2578 &amdgpu_switcheroo_ops, runtime);
d38ceaf9
AD
2579 if (runtime)
2580 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
2581
9475a943
SL
2582 if (amdgpu_emu_mode == 1) {
2583 /* post the asic on emulation mode */
2584 emu_soc_asic_init(adev);
bfca0289 2585 goto fence_driver_init;
9475a943 2586 }
bfca0289 2587
4e99a44e
ML
2588 /* detect if we are with an SRIOV vbios */
2589 amdgpu_device_detect_sriov_bios(adev);
048765ad 2590
95e8e59e
AD
2591 /* check if we need to reset the asic
2592 * E.g., driver was not cleanly unloaded previously, etc.
2593 */
f14899fd 2594 if (!amdgpu_sriov_vf(adev) && amdgpu_asic_need_reset_on_init(adev)) {
95e8e59e
AD
2595 r = amdgpu_asic_reset(adev);
2596 if (r) {
2597 dev_err(adev->dev, "asic reset on init failed\n");
2598 goto failed;
2599 }
2600 }
2601
d38ceaf9 2602 /* Post card if necessary */
39c640c0 2603 if (amdgpu_device_need_post(adev)) {
d38ceaf9 2604 if (!adev->bios) {
bec86378 2605 dev_err(adev->dev, "no vBIOS found\n");
83ba126a
AD
2606 r = -EINVAL;
2607 goto failed;
d38ceaf9 2608 }
bec86378 2609 DRM_INFO("GPU posting now...\n");
4e99a44e
ML
2610 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2611 if (r) {
2612 dev_err(adev->dev, "gpu post error!\n");
2613 goto failed;
2614 }
d38ceaf9
AD
2615 }
2616
88b64e95
AD
2617 if (adev->is_atom_fw) {
2618 /* Initialize clocks */
2619 r = amdgpu_atomfirmware_get_clock_info(adev);
2620 if (r) {
2621 dev_err(adev->dev, "amdgpu_atomfirmware_get_clock_info failed\n");
e23b74aa 2622 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
88b64e95
AD
2623 goto failed;
2624 }
2625 } else {
a5bde2f9
AD
2626 /* Initialize clocks */
2627 r = amdgpu_atombios_get_clock_info(adev);
2628 if (r) {
2629 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
e23b74aa 2630 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
89041940 2631 goto failed;
a5bde2f9
AD
2632 }
2633 /* init i2c buses */
4562236b
HW
2634 if (!amdgpu_device_has_dc_support(adev))
2635 amdgpu_atombios_i2c_init(adev);
2c1a2784 2636 }
d38ceaf9 2637
bfca0289 2638fence_driver_init:
d38ceaf9
AD
2639 /* Fence driver */
2640 r = amdgpu_fence_driver_init(adev);
2c1a2784
AD
2641 if (r) {
2642 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
e23b74aa 2643 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0);
83ba126a 2644 goto failed;
2c1a2784 2645 }
d38ceaf9
AD
2646
2647 /* init the mode config */
2648 drm_mode_config_init(adev->ddev);
2649
06ec9070 2650 r = amdgpu_device_ip_init(adev);
d38ceaf9 2651 if (r) {
8840a387 2652 /* failed in exclusive mode due to timeout */
2653 if (amdgpu_sriov_vf(adev) &&
2654 !amdgpu_sriov_runtime(adev) &&
2655 amdgpu_virt_mmio_blocked(adev) &&
2656 !amdgpu_virt_wait_reset(adev)) {
2657 dev_err(adev->dev, "VF exclusive mode timeout\n");
1daee8b4
PD
2658 /* Don't send request since VF is inactive. */
2659 adev->virt.caps &= ~AMDGPU_SRIOV_CAPS_RUNTIME;
2660 adev->virt.ops = NULL;
8840a387 2661 r = -EAGAIN;
2662 goto failed;
2663 }
06ec9070 2664 dev_err(adev->dev, "amdgpu_device_ip_init failed\n");
e23b74aa 2665 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0);
72d3f592
ED
2666 if (amdgpu_virt_request_full_gpu(adev, false))
2667 amdgpu_virt_release_full_gpu(adev, false);
83ba126a 2668 goto failed;
d38ceaf9
AD
2669 }
2670
2671 adev->accel_working = true;
2672
e59c0205
AX
2673 amdgpu_vm_check_compute_bug(adev);
2674
95844d20
MO
2675 /* Initialize the buffer migration limit. */
2676 if (amdgpu_moverate >= 0)
2677 max_MBps = amdgpu_moverate;
2678 else
2679 max_MBps = 8; /* Allow 8 MB/s. */
2680 /* Get a log2 for easy divisions. */
2681 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
2682
9bc92b9c
ML
2683 amdgpu_fbdev_init(adev);
2684
e9bc1bf7
YT
2685 if (amdgpu_sriov_vf(adev) && amdgim_is_hwperf(adev))
2686 amdgpu_pm_virt_sysfs_init(adev);
2687
d2f52ac8
RZ
2688 r = amdgpu_pm_sysfs_init(adev);
2689 if (r)
2690 DRM_ERROR("registering pm debugfs failed (%d).\n", r);
2691
5bb23532
OM
2692 r = amdgpu_ucode_sysfs_init(adev);
2693 if (r)
2694 DRM_ERROR("Creating firmware sysfs failed (%d).\n", r);
2695
75758255 2696 r = amdgpu_debugfs_gem_init(adev);
3f14e623 2697 if (r)
d38ceaf9 2698 DRM_ERROR("registering gem debugfs failed (%d).\n", r);
d38ceaf9
AD
2699
2700 r = amdgpu_debugfs_regs_init(adev);
3f14e623 2701 if (r)
d38ceaf9 2702 DRM_ERROR("registering register debugfs failed (%d).\n", r);
d38ceaf9 2703
50ab2533 2704 r = amdgpu_debugfs_firmware_init(adev);
3f14e623 2705 if (r)
50ab2533 2706 DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
50ab2533 2707
763efb6c 2708 r = amdgpu_debugfs_init(adev);
db95e218 2709 if (r)
763efb6c 2710 DRM_ERROR("Creating debugfs files failed (%d).\n", r);
db95e218 2711
d38ceaf9
AD
2712 if ((amdgpu_testing & 1)) {
2713 if (adev->accel_working)
2714 amdgpu_test_moves(adev);
2715 else
2716 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
2717 }
d38ceaf9
AD
2718 if (amdgpu_benchmarking) {
2719 if (adev->accel_working)
2720 amdgpu_benchmark(adev, amdgpu_benchmarking);
2721 else
2722 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
2723 }
2724
2725 /* enable clockgating, etc. after ib tests, etc. since some blocks require
2726 * explicit gating rather than handling it automatically.
2727 */
06ec9070 2728 r = amdgpu_device_ip_late_init(adev);
2c1a2784 2729 if (r) {
06ec9070 2730 dev_err(adev->dev, "amdgpu_device_ip_late_init failed\n");
e23b74aa 2731 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r);
83ba126a 2732 goto failed;
2c1a2784 2733 }
d38ceaf9 2734
108c6a63 2735 /* must succeed. */
511fdbc3 2736 amdgpu_ras_resume(adev);
108c6a63 2737
beff74bc
AD
2738 queue_delayed_work(system_wq, &adev->delayed_init_work,
2739 msecs_to_jiffies(AMDGPU_RESUME_MS));
2740
dcea6e65
KR
2741 r = device_create_file(adev->dev, &dev_attr_pcie_replay_count);
2742 if (r) {
2743 dev_err(adev->dev, "Could not create pcie_replay_count");
2744 return r;
2745 }
108c6a63 2746
d38ceaf9 2747 return 0;
83ba126a
AD
2748
2749failed:
89041940 2750 amdgpu_vf_error_trans_all(adev);
83ba126a
AD
2751 if (runtime)
2752 vga_switcheroo_fini_domain_pm_ops(adev->dev);
8840a387 2753
83ba126a 2754 return r;
d38ceaf9
AD
2755}
2756
d38ceaf9
AD
2757/**
2758 * amdgpu_device_fini - tear down the driver
2759 *
2760 * @adev: amdgpu_device pointer
2761 *
2762 * Tear down the driver info (all asics).
2763 * Called at driver shutdown.
2764 */
2765void amdgpu_device_fini(struct amdgpu_device *adev)
2766{
2767 int r;
2768
2769 DRM_INFO("amdgpu: finishing device.\n");
2770 adev->shutdown = true;
e5b03032
ML
2771 /* disable all interrupts */
2772 amdgpu_irq_disable_all(adev);
ff97cba8
ML
2773 if (adev->mode_info.mode_config_initialized){
2774 if (!amdgpu_device_has_dc_support(adev))
c2d88e06 2775 drm_helper_force_disable_all(adev->ddev);
ff97cba8
ML
2776 else
2777 drm_atomic_helper_shutdown(adev->ddev);
2778 }
d38ceaf9 2779 amdgpu_fence_driver_fini(adev);
58e955d9 2780 amdgpu_pm_sysfs_fini(adev);
d38ceaf9 2781 amdgpu_fbdev_fini(adev);
06ec9070 2782 r = amdgpu_device_ip_fini(adev);
ab4fe3e1
HR
2783 if (adev->firmware.gpu_info_fw) {
2784 release_firmware(adev->firmware.gpu_info_fw);
2785 adev->firmware.gpu_info_fw = NULL;
2786 }
d38ceaf9 2787 adev->accel_working = false;
beff74bc 2788 cancel_delayed_work_sync(&adev->delayed_init_work);
d38ceaf9 2789 /* free i2c buses */
4562236b
HW
2790 if (!amdgpu_device_has_dc_support(adev))
2791 amdgpu_i2c_fini(adev);
bfca0289
SL
2792
2793 if (amdgpu_emu_mode != 1)
2794 amdgpu_atombios_fini(adev);
2795
d38ceaf9
AD
2796 kfree(adev->bios);
2797 adev->bios = NULL;
84c8b22e
LW
2798 if (!pci_is_thunderbolt_attached(adev->pdev))
2799 vga_switcheroo_unregister_client(adev->pdev);
83ba126a
AD
2800 if (adev->flags & AMD_IS_PX)
2801 vga_switcheroo_fini_domain_pm_ops(adev->dev);
d38ceaf9
AD
2802 vga_client_register(adev->pdev, NULL, NULL, NULL);
2803 if (adev->rio_mem)
2804 pci_iounmap(adev->pdev, adev->rio_mem);
2805 adev->rio_mem = NULL;
2806 iounmap(adev->rmmio);
2807 adev->rmmio = NULL;
06ec9070 2808 amdgpu_device_doorbell_fini(adev);
e9bc1bf7
YT
2809 if (amdgpu_sriov_vf(adev) && amdgim_is_hwperf(adev))
2810 amdgpu_pm_virt_sysfs_fini(adev);
2811
d38ceaf9 2812 amdgpu_debugfs_regs_cleanup(adev);
dcea6e65 2813 device_remove_file(adev->dev, &dev_attr_pcie_replay_count);
5bb23532 2814 amdgpu_ucode_sysfs_fini(adev);
d38ceaf9
AD
2815}
2816
2817
2818/*
2819 * Suspend & resume.
2820 */
2821/**
810ddc3a 2822 * amdgpu_device_suspend - initiate device suspend
d38ceaf9 2823 *
87e3f136
DP
2824 * @dev: drm dev pointer
2825 * @suspend: suspend state
2826 * @fbcon : notify the fbdev of suspend
d38ceaf9
AD
2827 *
2828 * Puts the hw in the suspend state (all asics).
2829 * Returns 0 for success or an error on failure.
2830 * Called at driver suspend.
2831 */
810ddc3a 2832int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
d38ceaf9
AD
2833{
2834 struct amdgpu_device *adev;
2835 struct drm_crtc *crtc;
2836 struct drm_connector *connector;
5ceb54c6 2837 int r;
d38ceaf9
AD
2838
2839 if (dev == NULL || dev->dev_private == NULL) {
2840 return -ENODEV;
2841 }
2842
2843 adev = dev->dev_private;
2844
2845 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2846 return 0;
2847
44779b43 2848 adev->in_suspend = true;
d38ceaf9
AD
2849 drm_kms_helper_poll_disable(dev);
2850
5f818173
S
2851 if (fbcon)
2852 amdgpu_fbdev_set_suspend(adev, 1);
2853
beff74bc 2854 cancel_delayed_work_sync(&adev->delayed_init_work);
a5459475 2855
4562236b
HW
2856 if (!amdgpu_device_has_dc_support(adev)) {
2857 /* turn off display hw */
2858 drm_modeset_lock_all(dev);
2859 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2860 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
2861 }
2862 drm_modeset_unlock_all(dev);
fe1053b7
AD
2863 /* unpin the front buffers and cursors */
2864 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2865 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2866 struct drm_framebuffer *fb = crtc->primary->fb;
2867 struct amdgpu_bo *robj;
2868
91334223 2869 if (amdgpu_crtc->cursor_bo && !adev->enable_virtual_display) {
fe1053b7
AD
2870 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
2871 r = amdgpu_bo_reserve(aobj, true);
2872 if (r == 0) {
2873 amdgpu_bo_unpin(aobj);
2874 amdgpu_bo_unreserve(aobj);
2875 }
756e6880 2876 }
756e6880 2877
fe1053b7
AD
2878 if (fb == NULL || fb->obj[0] == NULL) {
2879 continue;
2880 }
2881 robj = gem_to_amdgpu_bo(fb->obj[0]);
2882 /* don't unpin kernel fb objects */
2883 if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
2884 r = amdgpu_bo_reserve(robj, true);
2885 if (r == 0) {
2886 amdgpu_bo_unpin(robj);
2887 amdgpu_bo_unreserve(robj);
2888 }
d38ceaf9
AD
2889 }
2890 }
2891 }
fe1053b7
AD
2892
2893 amdgpu_amdkfd_suspend(adev);
2894
5e6932fe 2895 amdgpu_ras_suspend(adev);
2896
fe1053b7
AD
2897 r = amdgpu_device_ip_suspend_phase1(adev);
2898
d38ceaf9
AD
2899 /* evict vram memory */
2900 amdgpu_bo_evict_vram(adev);
2901
5ceb54c6 2902 amdgpu_fence_driver_suspend(adev);
d38ceaf9 2903
fe1053b7 2904 r = amdgpu_device_ip_suspend_phase2(adev);
d38ceaf9 2905
a0a71e49
AD
2906 /* evict remaining vram memory
2907 * This second call to evict vram is to evict the gart page table
2908 * using the CPU.
2909 */
d38ceaf9
AD
2910 amdgpu_bo_evict_vram(adev);
2911
2912 pci_save_state(dev->pdev);
2913 if (suspend) {
2914 /* Shut down the device */
2915 pci_disable_device(dev->pdev);
2916 pci_set_power_state(dev->pdev, PCI_D3hot);
74b0b157 2917 } else {
2918 r = amdgpu_asic_reset(adev);
2919 if (r)
2920 DRM_ERROR("amdgpu asic reset failed\n");
d38ceaf9
AD
2921 }
2922
d38ceaf9
AD
2923 return 0;
2924}
2925
2926/**
810ddc3a 2927 * amdgpu_device_resume - initiate device resume
d38ceaf9 2928 *
87e3f136
DP
2929 * @dev: drm dev pointer
2930 * @resume: resume state
2931 * @fbcon : notify the fbdev of resume
d38ceaf9
AD
2932 *
2933 * Bring the hw back to operating state (all asics).
2934 * Returns 0 for success or an error on failure.
2935 * Called at driver resume.
2936 */
810ddc3a 2937int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
d38ceaf9
AD
2938{
2939 struct drm_connector *connector;
2940 struct amdgpu_device *adev = dev->dev_private;
756e6880 2941 struct drm_crtc *crtc;
03161a6e 2942 int r = 0;
d38ceaf9
AD
2943
2944 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2945 return 0;
2946
d38ceaf9
AD
2947 if (resume) {
2948 pci_set_power_state(dev->pdev, PCI_D0);
2949 pci_restore_state(dev->pdev);
74b0b157 2950 r = pci_enable_device(dev->pdev);
03161a6e 2951 if (r)
4d3b9ae5 2952 return r;
d38ceaf9
AD
2953 }
2954
2955 /* post card */
39c640c0 2956 if (amdgpu_device_need_post(adev)) {
74b0b157 2957 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2958 if (r)
2959 DRM_ERROR("amdgpu asic init failed\n");
2960 }
d38ceaf9 2961
06ec9070 2962 r = amdgpu_device_ip_resume(adev);
e6707218 2963 if (r) {
06ec9070 2964 DRM_ERROR("amdgpu_device_ip_resume failed (%d).\n", r);
4d3b9ae5 2965 return r;
e6707218 2966 }
5ceb54c6
AD
2967 amdgpu_fence_driver_resume(adev);
2968
d38ceaf9 2969
06ec9070 2970 r = amdgpu_device_ip_late_init(adev);
03161a6e 2971 if (r)
4d3b9ae5 2972 return r;
d38ceaf9 2973
beff74bc
AD
2974 queue_delayed_work(system_wq, &adev->delayed_init_work,
2975 msecs_to_jiffies(AMDGPU_RESUME_MS));
2976
fe1053b7
AD
2977 if (!amdgpu_device_has_dc_support(adev)) {
2978 /* pin cursors */
2979 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2980 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2981
91334223 2982 if (amdgpu_crtc->cursor_bo && !adev->enable_virtual_display) {
fe1053b7
AD
2983 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
2984 r = amdgpu_bo_reserve(aobj, true);
2985 if (r == 0) {
2986 r = amdgpu_bo_pin(aobj, AMDGPU_GEM_DOMAIN_VRAM);
2987 if (r != 0)
2988 DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
2989 amdgpu_crtc->cursor_addr = amdgpu_bo_gpu_offset(aobj);
2990 amdgpu_bo_unreserve(aobj);
2991 }
756e6880
AD
2992 }
2993 }
2994 }
ba997709
YZ
2995 r = amdgpu_amdkfd_resume(adev);
2996 if (r)
2997 return r;
756e6880 2998
96a5d8d4 2999 /* Make sure IB tests flushed */
beff74bc 3000 flush_delayed_work(&adev->delayed_init_work);
96a5d8d4 3001
d38ceaf9
AD
3002 /* blat the mode back in */
3003 if (fbcon) {
4562236b
HW
3004 if (!amdgpu_device_has_dc_support(adev)) {
3005 /* pre DCE11 */
3006 drm_helper_resume_force_mode(dev);
3007
3008 /* turn on display hw */
3009 drm_modeset_lock_all(dev);
3010 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
3011 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
3012 }
3013 drm_modeset_unlock_all(dev);
d38ceaf9 3014 }
4d3b9ae5 3015 amdgpu_fbdev_set_suspend(adev, 0);
d38ceaf9
AD
3016 }
3017
3018 drm_kms_helper_poll_enable(dev);
23a1a9e5 3019
5e6932fe 3020 amdgpu_ras_resume(adev);
3021
23a1a9e5
L
3022 /*
3023 * Most of the connector probing functions try to acquire runtime pm
3024 * refs to ensure that the GPU is powered on when connector polling is
3025 * performed. Since we're calling this from a runtime PM callback,
3026 * trying to acquire rpm refs will cause us to deadlock.
3027 *
3028 * Since we're guaranteed to be holding the rpm lock, it's safe to
3029 * temporarily disable the rpm helpers so this doesn't deadlock us.
3030 */
3031#ifdef CONFIG_PM
3032 dev->dev->power.disable_depth++;
3033#endif
4562236b
HW
3034 if (!amdgpu_device_has_dc_support(adev))
3035 drm_helper_hpd_irq_event(dev);
3036 else
3037 drm_kms_helper_hotplug_event(dev);
23a1a9e5
L
3038#ifdef CONFIG_PM
3039 dev->dev->power.disable_depth--;
3040#endif
44779b43
RZ
3041 adev->in_suspend = false;
3042
4d3b9ae5 3043 return 0;
d38ceaf9
AD
3044}
3045
e3ecdffa
AD
3046/**
3047 * amdgpu_device_ip_check_soft_reset - did soft reset succeed
3048 *
3049 * @adev: amdgpu_device pointer
3050 *
3051 * The list of all the hardware IPs that make up the asic is walked and
3052 * the check_soft_reset callbacks are run. check_soft_reset determines
3053 * if the asic is still hung or not.
3054 * Returns true if any of the IPs are still in a hung state, false if not.
3055 */
06ec9070 3056static bool amdgpu_device_ip_check_soft_reset(struct amdgpu_device *adev)
63fbf42f
CZ
3057{
3058 int i;
3059 bool asic_hang = false;
3060
f993d628
ML
3061 if (amdgpu_sriov_vf(adev))
3062 return true;
3063
8bc04c29
AD
3064 if (amdgpu_asic_need_full_reset(adev))
3065 return true;
3066
63fbf42f 3067 for (i = 0; i < adev->num_ip_blocks; i++) {
a1255107 3068 if (!adev->ip_blocks[i].status.valid)
63fbf42f 3069 continue;
a1255107
AD
3070 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
3071 adev->ip_blocks[i].status.hang =
3072 adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
3073 if (adev->ip_blocks[i].status.hang) {
3074 DRM_INFO("IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
63fbf42f
CZ
3075 asic_hang = true;
3076 }
3077 }
3078 return asic_hang;
3079}
3080
e3ecdffa
AD
3081/**
3082 * amdgpu_device_ip_pre_soft_reset - prepare for soft reset
3083 *
3084 * @adev: amdgpu_device pointer
3085 *
3086 * The list of all the hardware IPs that make up the asic is walked and the
3087 * pre_soft_reset callbacks are run if the block is hung. pre_soft_reset
3088 * handles any IP specific hardware or software state changes that are
3089 * necessary for a soft reset to succeed.
3090 * Returns 0 on success, negative error code on failure.
3091 */
06ec9070 3092static int amdgpu_device_ip_pre_soft_reset(struct amdgpu_device *adev)
d31a501e
CZ
3093{
3094 int i, r = 0;
3095
3096 for (i = 0; i < adev->num_ip_blocks; i++) {
a1255107 3097 if (!adev->ip_blocks[i].status.valid)
d31a501e 3098 continue;
a1255107
AD
3099 if (adev->ip_blocks[i].status.hang &&
3100 adev->ip_blocks[i].version->funcs->pre_soft_reset) {
3101 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
d31a501e
CZ
3102 if (r)
3103 return r;
3104 }
3105 }
3106
3107 return 0;
3108}
3109
e3ecdffa
AD
3110/**
3111 * amdgpu_device_ip_need_full_reset - check if a full asic reset is needed
3112 *
3113 * @adev: amdgpu_device pointer
3114 *
3115 * Some hardware IPs cannot be soft reset. If they are hung, a full gpu
3116 * reset is necessary to recover.
3117 * Returns true if a full asic reset is required, false if not.
3118 */
06ec9070 3119static bool amdgpu_device_ip_need_full_reset(struct amdgpu_device *adev)
35d782fe 3120{
da146d3b
AD
3121 int i;
3122
8bc04c29
AD
3123 if (amdgpu_asic_need_full_reset(adev))
3124 return true;
3125
da146d3b 3126 for (i = 0; i < adev->num_ip_blocks; i++) {
a1255107 3127 if (!adev->ip_blocks[i].status.valid)
da146d3b 3128 continue;
a1255107
AD
3129 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
3130 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
3131 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
98512bb8
KW
3132 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) ||
3133 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
a1255107 3134 if (adev->ip_blocks[i].status.hang) {
da146d3b
AD
3135 DRM_INFO("Some block need full reset!\n");
3136 return true;
3137 }
3138 }
35d782fe
CZ
3139 }
3140 return false;
3141}
3142
e3ecdffa
AD
3143/**
3144 * amdgpu_device_ip_soft_reset - do a soft reset
3145 *
3146 * @adev: amdgpu_device pointer
3147 *
3148 * The list of all the hardware IPs that make up the asic is walked and the
3149 * soft_reset callbacks are run if the block is hung. soft_reset handles any
3150 * IP specific hardware or software state changes that are necessary to soft
3151 * reset the IP.
3152 * Returns 0 on success, negative error code on failure.
3153 */
06ec9070 3154static int amdgpu_device_ip_soft_reset(struct amdgpu_device *adev)
35d782fe
CZ
3155{
3156 int i, r = 0;
3157
3158 for (i = 0; i < adev->num_ip_blocks; i++) {
a1255107 3159 if (!adev->ip_blocks[i].status.valid)
35d782fe 3160 continue;
a1255107
AD
3161 if (adev->ip_blocks[i].status.hang &&
3162 adev->ip_blocks[i].version->funcs->soft_reset) {
3163 r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
35d782fe
CZ
3164 if (r)
3165 return r;
3166 }
3167 }
3168
3169 return 0;
3170}
3171
e3ecdffa
AD
3172/**
3173 * amdgpu_device_ip_post_soft_reset - clean up from soft reset
3174 *
3175 * @adev: amdgpu_device pointer
3176 *
3177 * The list of all the hardware IPs that make up the asic is walked and the
3178 * post_soft_reset callbacks are run if the asic was hung. post_soft_reset
3179 * handles any IP specific hardware or software state changes that are
3180 * necessary after the IP has been soft reset.
3181 * Returns 0 on success, negative error code on failure.
3182 */
06ec9070 3183static int amdgpu_device_ip_post_soft_reset(struct amdgpu_device *adev)
35d782fe
CZ
3184{
3185 int i, r = 0;
3186
3187 for (i = 0; i < adev->num_ip_blocks; i++) {
a1255107 3188 if (!adev->ip_blocks[i].status.valid)
35d782fe 3189 continue;
a1255107
AD
3190 if (adev->ip_blocks[i].status.hang &&
3191 adev->ip_blocks[i].version->funcs->post_soft_reset)
3192 r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
35d782fe
CZ
3193 if (r)
3194 return r;
3195 }
3196
3197 return 0;
3198}
3199
e3ecdffa 3200/**
c33adbc7 3201 * amdgpu_device_recover_vram - Recover some VRAM contents
e3ecdffa
AD
3202 *
3203 * @adev: amdgpu_device pointer
3204 *
3205 * Restores the contents of VRAM buffers from the shadows in GTT. Used to
3206 * restore things like GPUVM page tables after a GPU reset where
3207 * the contents of VRAM might be lost.
403009bf
CK
3208 *
3209 * Returns:
3210 * 0 on success, negative error code on failure.
e3ecdffa 3211 */
c33adbc7 3212static int amdgpu_device_recover_vram(struct amdgpu_device *adev)
c41d1cf6 3213{
c41d1cf6 3214 struct dma_fence *fence = NULL, *next = NULL;
403009bf
CK
3215 struct amdgpu_bo *shadow;
3216 long r = 1, tmo;
c41d1cf6
ML
3217
3218 if (amdgpu_sriov_runtime(adev))
b045d3af 3219 tmo = msecs_to_jiffies(8000);
c41d1cf6
ML
3220 else
3221 tmo = msecs_to_jiffies(100);
3222
3223 DRM_INFO("recover vram bo from shadow start\n");
3224 mutex_lock(&adev->shadow_list_lock);
403009bf
CK
3225 list_for_each_entry(shadow, &adev->shadow_list, shadow_list) {
3226
3227 /* No need to recover an evicted BO */
3228 if (shadow->tbo.mem.mem_type != TTM_PL_TT ||
b575f10d 3229 shadow->tbo.mem.start == AMDGPU_BO_INVALID_OFFSET ||
403009bf
CK
3230 shadow->parent->tbo.mem.mem_type != TTM_PL_VRAM)
3231 continue;
3232
3233 r = amdgpu_bo_restore_shadow(shadow, &next);
3234 if (r)
3235 break;
3236
c41d1cf6 3237 if (fence) {
1712fb1a 3238 tmo = dma_fence_wait_timeout(fence, false, tmo);
403009bf
CK
3239 dma_fence_put(fence);
3240 fence = next;
1712fb1a 3241 if (tmo == 0) {
3242 r = -ETIMEDOUT;
c41d1cf6 3243 break;
1712fb1a 3244 } else if (tmo < 0) {
3245 r = tmo;
3246 break;
3247 }
403009bf
CK
3248 } else {
3249 fence = next;
c41d1cf6 3250 }
c41d1cf6
ML
3251 }
3252 mutex_unlock(&adev->shadow_list_lock);
3253
403009bf
CK
3254 if (fence)
3255 tmo = dma_fence_wait_timeout(fence, false, tmo);
c41d1cf6
ML
3256 dma_fence_put(fence);
3257
1712fb1a 3258 if (r < 0 || tmo <= 0) {
3259 DRM_ERROR("recover vram bo from shadow failed, r is %ld, tmo is %ld\n", r, tmo);
403009bf
CK
3260 return -EIO;
3261 }
c41d1cf6 3262
403009bf
CK
3263 DRM_INFO("recover vram bo from shadow done\n");
3264 return 0;
c41d1cf6
ML
3265}
3266
a90ad3c2 3267
e3ecdffa 3268/**
06ec9070 3269 * amdgpu_device_reset_sriov - reset ASIC for SR-IOV vf
5740682e
ML
3270 *
3271 * @adev: amdgpu device pointer
87e3f136 3272 * @from_hypervisor: request from hypervisor
5740682e
ML
3273 *
3274 * do VF FLR and reinitialize Asic
3f48c681 3275 * return 0 means succeeded otherwise failed
e3ecdffa
AD
3276 */
3277static int amdgpu_device_reset_sriov(struct amdgpu_device *adev,
3278 bool from_hypervisor)
5740682e
ML
3279{
3280 int r;
3281
3282 if (from_hypervisor)
3283 r = amdgpu_virt_request_full_gpu(adev, true);
3284 else
3285 r = amdgpu_virt_reset_gpu(adev);
3286 if (r)
3287 return r;
a90ad3c2 3288
f81e8d53
WL
3289 amdgpu_amdkfd_pre_reset(adev);
3290
a90ad3c2 3291 /* Resume IP prior to SMC */
06ec9070 3292 r = amdgpu_device_ip_reinit_early_sriov(adev);
5740682e
ML
3293 if (r)
3294 goto error;
a90ad3c2
ML
3295
3296 /* we need recover gart prior to run SMC/CP/SDMA resume */
c1c7ce8f 3297 amdgpu_gtt_mgr_recover(&adev->mman.bdev.man[TTM_PL_TT]);
a90ad3c2 3298
7a3e0bb2
RZ
3299 r = amdgpu_device_fw_loading(adev);
3300 if (r)
3301 return r;
3302
a90ad3c2 3303 /* now we are okay to resume SMC/CP/SDMA */
06ec9070 3304 r = amdgpu_device_ip_reinit_late_sriov(adev);
5740682e
ML
3305 if (r)
3306 goto error;
a90ad3c2
ML
3307
3308 amdgpu_irq_gpu_reset_resume_helper(adev);
5740682e 3309 r = amdgpu_ib_ring_tests(adev);
f81e8d53 3310 amdgpu_amdkfd_post_reset(adev);
a90ad3c2 3311
abc34253 3312error:
d3c117e5 3313 amdgpu_virt_init_data_exchange(adev);
abc34253 3314 amdgpu_virt_release_full_gpu(adev, true);
c41d1cf6
ML
3315 if (!r && adev->virt.gim_feature & AMDGIM_FEATURE_GIM_FLR_VRAMLOST) {
3316 atomic_inc(&adev->vram_lost_counter);
c33adbc7 3317 r = amdgpu_device_recover_vram(adev);
a90ad3c2
ML
3318 }
3319
3320 return r;
3321}
3322
12938fad
CK
3323/**
3324 * amdgpu_device_should_recover_gpu - check if we should try GPU recovery
3325 *
3326 * @adev: amdgpu device pointer
3327 *
3328 * Check amdgpu_gpu_recovery and SRIOV status to see if we should try to recover
3329 * a hung GPU.
3330 */
3331bool amdgpu_device_should_recover_gpu(struct amdgpu_device *adev)
3332{
3333 if (!amdgpu_device_ip_check_soft_reset(adev)) {
3334 DRM_INFO("Timeout, but no hardware hang detected.\n");
3335 return false;
3336 }
3337
3ba7b418
AG
3338 if (amdgpu_gpu_recovery == 0)
3339 goto disabled;
3340
3341 if (amdgpu_sriov_vf(adev))
3342 return true;
3343
3344 if (amdgpu_gpu_recovery == -1) {
3345 switch (adev->asic_type) {
fc42d47c
AG
3346 case CHIP_BONAIRE:
3347 case CHIP_HAWAII:
3ba7b418
AG
3348 case CHIP_TOPAZ:
3349 case CHIP_TONGA:
3350 case CHIP_FIJI:
3351 case CHIP_POLARIS10:
3352 case CHIP_POLARIS11:
3353 case CHIP_POLARIS12:
3354 case CHIP_VEGAM:
3355 case CHIP_VEGA20:
3356 case CHIP_VEGA10:
3357 case CHIP_VEGA12:
3358 break;
3359 default:
3360 goto disabled;
3361 }
12938fad
CK
3362 }
3363
3364 return true;
3ba7b418
AG
3365
3366disabled:
3367 DRM_INFO("GPU recovery disabled.\n");
3368 return false;
12938fad
CK
3369}
3370
5c6dd71e 3371
26bc5340
AG
3372static int amdgpu_device_pre_asic_reset(struct amdgpu_device *adev,
3373 struct amdgpu_job *job,
3374 bool *need_full_reset_arg)
3375{
3376 int i, r = 0;
3377 bool need_full_reset = *need_full_reset_arg;
71182665 3378
71182665 3379 /* block all schedulers and reset given job's ring */
0875dc9e
CZ
3380 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
3381 struct amdgpu_ring *ring = adev->rings[i];
3382
51687759 3383 if (!ring || !ring->sched.thread)
0875dc9e 3384 continue;
5740682e 3385
2f9d4084
ML
3386 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
3387 amdgpu_fence_driver_force_completion(ring);
0875dc9e 3388 }
d38ceaf9 3389
222b5f04
AG
3390 if(job)
3391 drm_sched_increase_karma(&job->base);
3392
1d721ed6 3393 /* Don't suspend on bare metal if we are not going to HW reset the ASIC */
26bc5340
AG
3394 if (!amdgpu_sriov_vf(adev)) {
3395
3396 if (!need_full_reset)
3397 need_full_reset = amdgpu_device_ip_need_full_reset(adev);
3398
3399 if (!need_full_reset) {
3400 amdgpu_device_ip_pre_soft_reset(adev);
3401 r = amdgpu_device_ip_soft_reset(adev);
3402 amdgpu_device_ip_post_soft_reset(adev);
3403 if (r || amdgpu_device_ip_check_soft_reset(adev)) {
3404 DRM_INFO("soft reset failed, will fallback to full reset!\n");
3405 need_full_reset = true;
3406 }
3407 }
3408
3409 if (need_full_reset)
3410 r = amdgpu_device_ip_suspend(adev);
3411
3412 *need_full_reset_arg = need_full_reset;
3413 }
3414
3415 return r;
3416}
3417
3418static int amdgpu_do_asic_reset(struct amdgpu_hive_info *hive,
3419 struct list_head *device_list_handle,
3420 bool *need_full_reset_arg)
3421{
3422 struct amdgpu_device *tmp_adev = NULL;
3423 bool need_full_reset = *need_full_reset_arg, vram_lost = false;
3424 int r = 0;
3425
3426 /*
3427 * ASIC reset has to be done on all HGMI hive nodes ASAP
3428 * to allow proper links negotiation in FW (within 1 sec)
3429 */
3430 if (need_full_reset) {
3431 list_for_each_entry(tmp_adev, device_list_handle, gmc.xgmi.head) {
d4535e2c
AG
3432 /* For XGMI run all resets in parallel to speed up the process */
3433 if (tmp_adev->gmc.xgmi.num_physical_nodes > 1) {
3434 if (!queue_work(system_highpri_wq, &tmp_adev->xgmi_reset_work))
3435 r = -EALREADY;
3436 } else
3437 r = amdgpu_asic_reset(tmp_adev);
3438
3439 if (r) {
fed184e9 3440 DRM_ERROR("ASIC reset failed with error, %d for drm dev, %s",
26bc5340 3441 r, tmp_adev->ddev->unique);
d4535e2c
AG
3442 break;
3443 }
3444 }
3445
3446 /* For XGMI wait for all PSP resets to complete before proceed */
3447 if (!r) {
3448 list_for_each_entry(tmp_adev, device_list_handle,
3449 gmc.xgmi.head) {
3450 if (tmp_adev->gmc.xgmi.num_physical_nodes > 1) {
3451 flush_work(&tmp_adev->xgmi_reset_work);
3452 r = tmp_adev->asic_reset_res;
3453 if (r)
3454 break;
3455 }
3456 }
2be4c4a9 3457
3458 list_for_each_entry(tmp_adev, device_list_handle,
3459 gmc.xgmi.head) {
3460 amdgpu_ras_reserve_bad_pages(tmp_adev);
3461 }
26bc5340
AG
3462 }
3463 }
3464
3465
3466 list_for_each_entry(tmp_adev, device_list_handle, gmc.xgmi.head) {
3467 if (need_full_reset) {
3468 /* post card */
3469 if (amdgpu_atom_asic_init(tmp_adev->mode_info.atom_context))
3470 DRM_WARN("asic atom init failed!");
3471
3472 if (!r) {
3473 dev_info(tmp_adev->dev, "GPU reset succeeded, trying to resume\n");
3474 r = amdgpu_device_ip_resume_phase1(tmp_adev);
3475 if (r)
3476 goto out;
3477
3478 vram_lost = amdgpu_device_check_vram_lost(tmp_adev);
3479 if (vram_lost) {
77e7f829 3480 DRM_INFO("VRAM is lost due to GPU reset!\n");
26bc5340
AG
3481 atomic_inc(&tmp_adev->vram_lost_counter);
3482 }
3483
3484 r = amdgpu_gtt_mgr_recover(
3485 &tmp_adev->mman.bdev.man[TTM_PL_TT]);
3486 if (r)
3487 goto out;
3488
3489 r = amdgpu_device_fw_loading(tmp_adev);
3490 if (r)
3491 return r;
3492
3493 r = amdgpu_device_ip_resume_phase2(tmp_adev);
3494 if (r)
3495 goto out;
3496
3497 if (vram_lost)
3498 amdgpu_device_fill_reset_magic(tmp_adev);
3499
7c04ca50 3500 r = amdgpu_device_ip_late_init(tmp_adev);
3501 if (r)
3502 goto out;
3503
e79a04d5 3504 /* must succeed. */
511fdbc3 3505 amdgpu_ras_resume(tmp_adev);
e79a04d5 3506
26bc5340
AG
3507 /* Update PSP FW topology after reset */
3508 if (hive && tmp_adev->gmc.xgmi.num_physical_nodes > 1)
3509 r = amdgpu_xgmi_update_topology(hive, tmp_adev);
3510 }
3511 }
3512
3513
3514out:
3515 if (!r) {
3516 amdgpu_irq_gpu_reset_resume_helper(tmp_adev);
3517 r = amdgpu_ib_ring_tests(tmp_adev);
3518 if (r) {
3519 dev_err(tmp_adev->dev, "ib ring test failed (%d).\n", r);
3520 r = amdgpu_device_ip_suspend(tmp_adev);
3521 need_full_reset = true;
3522 r = -EAGAIN;
3523 goto end;
3524 }
3525 }
3526
3527 if (!r)
3528 r = amdgpu_device_recover_vram(tmp_adev);
3529 else
3530 tmp_adev->asic_reset_res = r;
3531 }
3532
3533end:
3534 *need_full_reset_arg = need_full_reset;
3535 return r;
3536}
3537
1d721ed6 3538static bool amdgpu_device_lock_adev(struct amdgpu_device *adev, bool trylock)
26bc5340 3539{
1d721ed6
AG
3540 if (trylock) {
3541 if (!mutex_trylock(&adev->lock_reset))
3542 return false;
3543 } else
3544 mutex_lock(&adev->lock_reset);
5740682e 3545
26bc5340
AG
3546 atomic_inc(&adev->gpu_reset_counter);
3547 adev->in_gpu_reset = 1;
7b184b00 3548 /* Block kfd: SRIOV would do it separately */
3549 if (!amdgpu_sriov_vf(adev))
3550 amdgpu_amdkfd_pre_reset(adev);
1d721ed6
AG
3551
3552 return true;
26bc5340 3553}
d38ceaf9 3554
26bc5340
AG
3555static void amdgpu_device_unlock_adev(struct amdgpu_device *adev)
3556{
7b184b00 3557 /*unlock kfd: SRIOV would do it separately */
3558 if (!amdgpu_sriov_vf(adev))
3559 amdgpu_amdkfd_post_reset(adev);
89041940 3560 amdgpu_vf_error_trans_all(adev);
13a752e3
ML
3561 adev->in_gpu_reset = 0;
3562 mutex_unlock(&adev->lock_reset);
26bc5340
AG
3563}
3564
3565
3566/**
3567 * amdgpu_device_gpu_recover - reset the asic and recover scheduler
3568 *
3569 * @adev: amdgpu device pointer
3570 * @job: which job trigger hang
3571 *
3572 * Attempt to reset the GPU if it has hung (all asics).
3573 * Attempt to do soft-reset or full-reset and reinitialize Asic
3574 * Returns 0 for success or an error on failure.
3575 */
3576
3577int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
3578 struct amdgpu_job *job)
3579{
1d721ed6
AG
3580 struct list_head device_list, *device_list_handle = NULL;
3581 bool need_full_reset, job_signaled;
26bc5340 3582 struct amdgpu_hive_info *hive = NULL;
26bc5340 3583 struct amdgpu_device *tmp_adev = NULL;
1d721ed6 3584 int i, r = 0;
26bc5340 3585
1d721ed6 3586 need_full_reset = job_signaled = false;
26bc5340
AG
3587 INIT_LIST_HEAD(&device_list);
3588
3589 dev_info(adev->dev, "GPU reset begin!\n");
3590
beff74bc 3591 cancel_delayed_work_sync(&adev->delayed_init_work);
c53e4db7 3592
1d721ed6
AG
3593 hive = amdgpu_get_xgmi_hive(adev, false);
3594
26bc5340 3595 /*
1d721ed6
AG
3596 * Here we trylock to avoid chain of resets executing from
3597 * either trigger by jobs on different adevs in XGMI hive or jobs on
3598 * different schedulers for same device while this TO handler is running.
3599 * We always reset all schedulers for device and all devices for XGMI
3600 * hive so that should take care of them too.
26bc5340 3601 */
1d721ed6
AG
3602
3603 if (hive && !mutex_trylock(&hive->reset_lock)) {
3604 DRM_INFO("Bailing on TDR for s_job:%llx, hive: %llx as another already in progress",
3605 job->base.id, hive->hive_id);
26bc5340 3606 return 0;
1d721ed6 3607 }
26bc5340
AG
3608
3609 /* Start with adev pre asic reset first for soft reset check.*/
1d721ed6
AG
3610 if (!amdgpu_device_lock_adev(adev, !hive)) {
3611 DRM_INFO("Bailing on TDR for s_job:%llx, as another already in progress",
3612 job->base.id);
3613 return 0;
26bc5340
AG
3614 }
3615
3616 /* Build list of devices to reset */
1d721ed6 3617 if (adev->gmc.xgmi.num_physical_nodes > 1) {
26bc5340
AG
3618 if (!hive) {
3619 amdgpu_device_unlock_adev(adev);
3620 return -ENODEV;
3621 }
3622
3623 /*
3624 * In case we are in XGMI hive mode device reset is done for all the
3625 * nodes in the hive to retrain all XGMI links and hence the reset
3626 * sequence is executed in loop on all nodes.
3627 */
3628 device_list_handle = &hive->device_list;
3629 } else {
3630 list_add_tail(&adev->gmc.xgmi.head, &device_list);
3631 device_list_handle = &device_list;
3632 }
3633
1d721ed6
AG
3634 /* block all schedulers and reset given job's ring */
3635 list_for_each_entry(tmp_adev, device_list_handle, gmc.xgmi.head) {
3636 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
3637 struct amdgpu_ring *ring = tmp_adev->rings[i];
3638
3639 if (!ring || !ring->sched.thread)
3640 continue;
3641
3642 drm_sched_stop(&ring->sched, &job->base);
3643 }
3644 }
3645
3646
3647 /*
3648 * Must check guilty signal here since after this point all old
3649 * HW fences are force signaled.
3650 *
3651 * job->base holds a reference to parent fence
3652 */
3653 if (job && job->base.s_fence->parent &&
3654 dma_fence_is_signaled(job->base.s_fence->parent))
3655 job_signaled = true;
3656
3657 if (!amdgpu_device_ip_need_full_reset(adev))
3658 device_list_handle = &device_list;
3659
3660 if (job_signaled) {
3661 dev_info(adev->dev, "Guilty job already signaled, skipping HW reset");
3662 goto skip_hw_reset;
3663 }
3664
3665
3666 /* Guilty job will be freed after this*/
3667 r = amdgpu_device_pre_asic_reset(adev,
3668 job,
3669 &need_full_reset);
3670 if (r) {
3671 /*TODO Should we stop ?*/
3672 DRM_ERROR("GPU pre asic reset failed with err, %d for drm dev, %s ",
3673 r, adev->ddev->unique);
3674 adev->asic_reset_res = r;
3675 }
3676
26bc5340
AG
3677retry: /* Rest of adevs pre asic reset from XGMI hive. */
3678 list_for_each_entry(tmp_adev, device_list_handle, gmc.xgmi.head) {
3679
3680 if (tmp_adev == adev)
3681 continue;
3682
1d721ed6 3683 amdgpu_device_lock_adev(tmp_adev, false);
26bc5340
AG
3684 r = amdgpu_device_pre_asic_reset(tmp_adev,
3685 NULL,
3686 &need_full_reset);
3687 /*TODO Should we stop ?*/
3688 if (r) {
3689 DRM_ERROR("GPU pre asic reset failed with err, %d for drm dev, %s ",
3690 r, tmp_adev->ddev->unique);
3691 tmp_adev->asic_reset_res = r;
3692 }
3693 }
3694
3695 /* Actual ASIC resets if needed.*/
3696 /* TODO Implement XGMI hive reset logic for SRIOV */
3697 if (amdgpu_sriov_vf(adev)) {
3698 r = amdgpu_device_reset_sriov(adev, job ? false : true);
3699 if (r)
3700 adev->asic_reset_res = r;
3701 } else {
3702 r = amdgpu_do_asic_reset(hive, device_list_handle, &need_full_reset);
3703 if (r && r == -EAGAIN)
3704 goto retry;
3705 }
3706
1d721ed6
AG
3707skip_hw_reset:
3708
26bc5340
AG
3709 /* Post ASIC reset for all devs .*/
3710 list_for_each_entry(tmp_adev, device_list_handle, gmc.xgmi.head) {
1d721ed6
AG
3711 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
3712 struct amdgpu_ring *ring = tmp_adev->rings[i];
3713
3714 if (!ring || !ring->sched.thread)
3715 continue;
3716
3717 /* No point to resubmit jobs if we didn't HW reset*/
3718 if (!tmp_adev->asic_reset_res && !job_signaled)
3719 drm_sched_resubmit_jobs(&ring->sched);
3720
3721 drm_sched_start(&ring->sched, !tmp_adev->asic_reset_res);
3722 }
3723
3724 if (!amdgpu_device_has_dc_support(tmp_adev) && !job_signaled) {
3725 drm_helper_resume_force_mode(tmp_adev->ddev);
3726 }
3727
3728 tmp_adev->asic_reset_res = 0;
26bc5340
AG
3729
3730 if (r) {
3731 /* bad news, how to tell it to userspace ? */
3732 dev_info(tmp_adev->dev, "GPU reset(%d) failed\n", atomic_read(&adev->gpu_reset_counter));
3733 amdgpu_vf_error_put(tmp_adev, AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, r);
3734 } else {
3735 dev_info(tmp_adev->dev, "GPU reset(%d) succeeded!\n", atomic_read(&adev->gpu_reset_counter));
3736 }
3737
3738 amdgpu_device_unlock_adev(tmp_adev);
3739 }
3740
1d721ed6 3741 if (hive)
22d6575b 3742 mutex_unlock(&hive->reset_lock);
26bc5340
AG
3743
3744 if (r)
3745 dev_info(adev->dev, "GPU reset end with ret = %d\n", r);
d38ceaf9
AD
3746 return r;
3747}
3748
e3ecdffa
AD
3749/**
3750 * amdgpu_device_get_pcie_info - fence pcie info about the PCIE slot
3751 *
3752 * @adev: amdgpu_device pointer
3753 *
3754 * Fetchs and stores in the driver the PCIE capabilities (gen speed
3755 * and lanes) of the slot the device is in. Handles APUs and
3756 * virtualized environments where PCIE config space may not be available.
3757 */
5494d864 3758static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev)
d0dd7f0c 3759{
5d9a6330 3760 struct pci_dev *pdev;
c5313457
HK
3761 enum pci_bus_speed speed_cap, platform_speed_cap;
3762 enum pcie_link_width platform_link_width;
d0dd7f0c 3763
cd474ba0
AD
3764 if (amdgpu_pcie_gen_cap)
3765 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
d0dd7f0c 3766
cd474ba0
AD
3767 if (amdgpu_pcie_lane_cap)
3768 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
d0dd7f0c 3769
cd474ba0
AD
3770 /* covers APUs as well */
3771 if (pci_is_root_bus(adev->pdev->bus)) {
3772 if (adev->pm.pcie_gen_mask == 0)
3773 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
3774 if (adev->pm.pcie_mlw_mask == 0)
3775 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
d0dd7f0c 3776 return;
cd474ba0 3777 }
d0dd7f0c 3778
c5313457
HK
3779 if (adev->pm.pcie_gen_mask && adev->pm.pcie_mlw_mask)
3780 return;
3781
dbaa922b
AD
3782 pcie_bandwidth_available(adev->pdev, NULL,
3783 &platform_speed_cap, &platform_link_width);
c5313457 3784
cd474ba0 3785 if (adev->pm.pcie_gen_mask == 0) {
5d9a6330
AD
3786 /* asic caps */
3787 pdev = adev->pdev;
3788 speed_cap = pcie_get_speed_cap(pdev);
3789 if (speed_cap == PCI_SPEED_UNKNOWN) {
3790 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
cd474ba0
AD
3791 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
3792 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
cd474ba0 3793 } else {
5d9a6330
AD
3794 if (speed_cap == PCIE_SPEED_16_0GT)
3795 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
3796 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
3797 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3 |
3798 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN4);
3799 else if (speed_cap == PCIE_SPEED_8_0GT)
3800 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
3801 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
3802 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
3803 else if (speed_cap == PCIE_SPEED_5_0GT)
3804 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
3805 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2);
3806 else
3807 adev->pm.pcie_gen_mask |= CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1;
3808 }
3809 /* platform caps */
c5313457 3810 if (platform_speed_cap == PCI_SPEED_UNKNOWN) {
5d9a6330
AD
3811 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
3812 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2);
3813 } else {
c5313457 3814 if (platform_speed_cap == PCIE_SPEED_16_0GT)
5d9a6330
AD
3815 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
3816 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
3817 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3 |
3818 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4);
c5313457 3819 else if (platform_speed_cap == PCIE_SPEED_8_0GT)
5d9a6330
AD
3820 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
3821 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
3822 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3);
c5313457 3823 else if (platform_speed_cap == PCIE_SPEED_5_0GT)
5d9a6330
AD
3824 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
3825 CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2);
3826 else
3827 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
3828
cd474ba0
AD
3829 }
3830 }
3831 if (adev->pm.pcie_mlw_mask == 0) {
c5313457 3832 if (platform_link_width == PCIE_LNK_WIDTH_UNKNOWN) {
5d9a6330
AD
3833 adev->pm.pcie_mlw_mask |= AMDGPU_DEFAULT_PCIE_MLW_MASK;
3834 } else {
c5313457 3835 switch (platform_link_width) {
5d9a6330 3836 case PCIE_LNK_X32:
cd474ba0
AD
3837 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
3838 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
3839 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3840 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3841 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3842 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3843 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3844 break;
5d9a6330 3845 case PCIE_LNK_X16:
cd474ba0
AD
3846 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
3847 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3848 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3849 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3850 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3851 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3852 break;
5d9a6330 3853 case PCIE_LNK_X12:
cd474ba0
AD
3854 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3855 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3856 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3857 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3858 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3859 break;
5d9a6330 3860 case PCIE_LNK_X8:
cd474ba0
AD
3861 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3862 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3863 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3864 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3865 break;
5d9a6330 3866 case PCIE_LNK_X4:
cd474ba0
AD
3867 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3868 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3869 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3870 break;
5d9a6330 3871 case PCIE_LNK_X2:
cd474ba0
AD
3872 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3873 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3874 break;
5d9a6330 3875 case PCIE_LNK_X1:
cd474ba0
AD
3876 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
3877 break;
3878 default:
3879 break;
3880 }
d0dd7f0c
AD
3881 }
3882 }
3883}
d38ceaf9 3884