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