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