drm/amdgpu: move debugfs functions to their own file
[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 */
0875dc9e 28#include <linux/kthread.h>
d38ceaf9
AD
29#include <linux/console.h>
30#include <linux/slab.h>
d38ceaf9
AD
31#include <drm/drmP.h>
32#include <drm/drm_crtc_helper.h>
4562236b 33#include <drm/drm_atomic_helper.h>
d38ceaf9
AD
34#include <drm/amdgpu_drm.h>
35#include <linux/vgaarb.h>
36#include <linux/vga_switcheroo.h>
37#include <linux/efi.h>
38#include "amdgpu.h"
f4b373f4 39#include "amdgpu_trace.h"
d38ceaf9
AD
40#include "amdgpu_i2c.h"
41#include "atom.h"
42#include "amdgpu_atombios.h"
a5bde2f9 43#include "amdgpu_atomfirmware.h"
d0dd7f0c 44#include "amd_pcie.h"
33f34802
KW
45#ifdef CONFIG_DRM_AMDGPU_SI
46#include "si.h"
47#endif
a2e73f56
AD
48#ifdef CONFIG_DRM_AMDGPU_CIK
49#include "cik.h"
50#endif
aaa36a97 51#include "vi.h"
460826e6 52#include "soc15.h"
d38ceaf9 53#include "bif/bif_4_1_d.h"
9accf2fd 54#include <linux/pci.h>
bec86378 55#include <linux/firmware.h>
89041940 56#include "amdgpu_vf_error.h"
d38ceaf9 57
ba997709 58#include "amdgpu_amdkfd.h"
d2f52ac8 59#include "amdgpu_pm.h"
d38ceaf9 60
e2a75f88 61MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin");
2d2e5e7e 62MODULE_FIRMWARE("amdgpu/raven_gpu_info.bin");
e2a75f88 63
2dc80b00
S
64#define AMDGPU_RESUME_MS 2000
65
d38ceaf9 66static const char *amdgpu_asic_name[] = {
da69c161
KW
67 "TAHITI",
68 "PITCAIRN",
69 "VERDE",
70 "OLAND",
71 "HAINAN",
d38ceaf9
AD
72 "BONAIRE",
73 "KAVERI",
74 "KABINI",
75 "HAWAII",
76 "MULLINS",
77 "TOPAZ",
78 "TONGA",
48299f95 79 "FIJI",
d38ceaf9 80 "CARRIZO",
139f4917 81 "STONEY",
2cc0c0b5
FC
82 "POLARIS10",
83 "POLARIS11",
c4642a47 84 "POLARIS12",
d4196f01 85 "VEGA10",
2ca8a5d2 86 "RAVEN",
d38ceaf9
AD
87 "LAST",
88};
89
90bool amdgpu_device_is_px(struct drm_device *dev)
91{
92 struct amdgpu_device *adev = dev->dev_private;
93
2f7d10b3 94 if (adev->flags & AMD_IS_PX)
d38ceaf9
AD
95 return true;
96 return false;
97}
98
99/*
100 * MMIO register access helper functions.
101 */
102uint32_t amdgpu_mm_rreg(struct amdgpu_device *adev, uint32_t reg,
15d72fd7 103 uint32_t acc_flags)
d38ceaf9 104{
f4b373f4
TSD
105 uint32_t ret;
106
43ca8efa 107 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev))
bc992ba5 108 return amdgpu_virt_kiq_rreg(adev, reg);
bc992ba5 109
15d72fd7 110 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
f4b373f4 111 ret = readl(((void __iomem *)adev->rmmio) + (reg * 4));
d38ceaf9
AD
112 else {
113 unsigned long flags;
d38ceaf9
AD
114
115 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
116 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
117 ret = readl(((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
118 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
d38ceaf9 119 }
f4b373f4
TSD
120 trace_amdgpu_mm_rreg(adev->pdev->device, reg, ret);
121 return ret;
d38ceaf9
AD
122}
123
124void amdgpu_mm_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v,
15d72fd7 125 uint32_t acc_flags)
d38ceaf9 126{
f4b373f4 127 trace_amdgpu_mm_wreg(adev->pdev->device, reg, v);
4e99a44e 128
47ed4e1c
KW
129 if (adev->asic_type >= CHIP_VEGA10 && reg == 0) {
130 adev->last_mm_index = v;
131 }
132
43ca8efa 133 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev))
bc992ba5 134 return amdgpu_virt_kiq_wreg(adev, reg, v);
bc992ba5 135
15d72fd7 136 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
d38ceaf9
AD
137 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
138 else {
139 unsigned long flags;
140
141 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
142 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
143 writel(v, ((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
144 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
145 }
47ed4e1c
KW
146
147 if (adev->asic_type >= CHIP_VEGA10 && reg == 1 && adev->last_mm_index == 0x5702C) {
148 udelay(500);
149 }
d38ceaf9
AD
150}
151
152u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 reg)
153{
154 if ((reg * 4) < adev->rio_mem_size)
155 return ioread32(adev->rio_mem + (reg * 4));
156 else {
157 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
158 return ioread32(adev->rio_mem + (mmMM_DATA * 4));
159 }
160}
161
162void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
163{
47ed4e1c
KW
164 if (adev->asic_type >= CHIP_VEGA10 && reg == 0) {
165 adev->last_mm_index = v;
166 }
d38ceaf9
AD
167
168 if ((reg * 4) < adev->rio_mem_size)
169 iowrite32(v, adev->rio_mem + (reg * 4));
170 else {
171 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
172 iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
173 }
47ed4e1c
KW
174
175 if (adev->asic_type >= CHIP_VEGA10 && reg == 1 && adev->last_mm_index == 0x5702C) {
176 udelay(500);
177 }
d38ceaf9
AD
178}
179
180/**
181 * amdgpu_mm_rdoorbell - read a doorbell dword
182 *
183 * @adev: amdgpu_device pointer
184 * @index: doorbell index
185 *
186 * Returns the value in the doorbell aperture at the
187 * requested doorbell index (CIK).
188 */
189u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
190{
191 if (index < adev->doorbell.num_doorbells) {
192 return readl(adev->doorbell.ptr + index);
193 } else {
194 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
195 return 0;
196 }
197}
198
199/**
200 * amdgpu_mm_wdoorbell - write a doorbell dword
201 *
202 * @adev: amdgpu_device pointer
203 * @index: doorbell index
204 * @v: value to write
205 *
206 * Writes @v to the doorbell aperture at the
207 * requested doorbell index (CIK).
208 */
209void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
210{
211 if (index < adev->doorbell.num_doorbells) {
212 writel(v, adev->doorbell.ptr + index);
213 } else {
214 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
215 }
216}
217
832be404
KW
218/**
219 * amdgpu_mm_rdoorbell64 - read a doorbell Qword
220 *
221 * @adev: amdgpu_device pointer
222 * @index: doorbell index
223 *
224 * Returns the value in the doorbell aperture at the
225 * requested doorbell index (VEGA10+).
226 */
227u64 amdgpu_mm_rdoorbell64(struct amdgpu_device *adev, u32 index)
228{
229 if (index < adev->doorbell.num_doorbells) {
230 return atomic64_read((atomic64_t *)(adev->doorbell.ptr + index));
231 } else {
232 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
233 return 0;
234 }
235}
236
237/**
238 * amdgpu_mm_wdoorbell64 - write a doorbell Qword
239 *
240 * @adev: amdgpu_device pointer
241 * @index: doorbell index
242 * @v: value to write
243 *
244 * Writes @v to the doorbell aperture at the
245 * requested doorbell index (VEGA10+).
246 */
247void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v)
248{
249 if (index < adev->doorbell.num_doorbells) {
250 atomic64_set((atomic64_t *)(adev->doorbell.ptr + index), v);
251 } else {
252 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
253 }
254}
255
d38ceaf9
AD
256/**
257 * amdgpu_invalid_rreg - dummy reg read function
258 *
259 * @adev: amdgpu device pointer
260 * @reg: offset of register
261 *
262 * Dummy register read function. Used for register blocks
263 * that certain asics don't have (all asics).
264 * Returns the value in the register.
265 */
266static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
267{
268 DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
269 BUG();
270 return 0;
271}
272
273/**
274 * amdgpu_invalid_wreg - dummy reg write function
275 *
276 * @adev: amdgpu device pointer
277 * @reg: offset of register
278 * @v: value to write to the register
279 *
280 * Dummy register read function. Used for register blocks
281 * that certain asics don't have (all asics).
282 */
283static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
284{
285 DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
286 reg, v);
287 BUG();
288}
289
290/**
291 * amdgpu_block_invalid_rreg - dummy reg read function
292 *
293 * @adev: amdgpu device pointer
294 * @block: offset of instance
295 * @reg: offset of register
296 *
297 * Dummy register read function. Used for register blocks
298 * that certain asics don't have (all asics).
299 * Returns the value in the register.
300 */
301static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
302 uint32_t block, uint32_t reg)
303{
304 DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
305 reg, block);
306 BUG();
307 return 0;
308}
309
310/**
311 * amdgpu_block_invalid_wreg - dummy reg write function
312 *
313 * @adev: amdgpu device pointer
314 * @block: offset of instance
315 * @reg: offset of register
316 * @v: value to write to the register
317 *
318 * Dummy register read function. Used for register blocks
319 * that certain asics don't have (all asics).
320 */
321static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
322 uint32_t block,
323 uint32_t reg, uint32_t v)
324{
325 DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
326 reg, block, v);
327 BUG();
328}
329
06ec9070 330static int amdgpu_device_vram_scratch_init(struct amdgpu_device *adev)
d38ceaf9 331{
a4a02777
CK
332 return amdgpu_bo_create_kernel(adev, AMDGPU_GPU_PAGE_SIZE,
333 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
334 &adev->vram_scratch.robj,
335 &adev->vram_scratch.gpu_addr,
336 (void **)&adev->vram_scratch.ptr);
d38ceaf9
AD
337}
338
06ec9070 339static void amdgpu_device_vram_scratch_fini(struct amdgpu_device *adev)
d38ceaf9 340{
078af1a3 341 amdgpu_bo_free_kernel(&adev->vram_scratch.robj, NULL, NULL);
d38ceaf9
AD
342}
343
344/**
345 * amdgpu_program_register_sequence - program an array of registers.
346 *
347 * @adev: amdgpu_device pointer
348 * @registers: pointer to the register array
349 * @array_size: size of the register array
350 *
351 * Programs an array or registers with and and or masks.
352 * This is a helper for setting golden registers.
353 */
354void amdgpu_program_register_sequence(struct amdgpu_device *adev,
355 const u32 *registers,
356 const u32 array_size)
357{
358 u32 tmp, reg, and_mask, or_mask;
359 int i;
360
361 if (array_size % 3)
362 return;
363
364 for (i = 0; i < array_size; i +=3) {
365 reg = registers[i + 0];
366 and_mask = registers[i + 1];
367 or_mask = registers[i + 2];
368
369 if (and_mask == 0xffffffff) {
370 tmp = or_mask;
371 } else {
372 tmp = RREG32(reg);
373 tmp &= ~and_mask;
374 tmp |= or_mask;
375 }
376 WREG32(reg, tmp);
377 }
378}
379
380void amdgpu_pci_config_reset(struct amdgpu_device *adev)
381{
382 pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
383}
384
385/*
386 * GPU doorbell aperture helpers function.
387 */
388/**
06ec9070 389 * amdgpu_device_doorbell_init - Init doorbell driver information.
d38ceaf9
AD
390 *
391 * @adev: amdgpu_device pointer
392 *
393 * Init doorbell driver information (CIK)
394 * Returns 0 on success, error on failure.
395 */
06ec9070 396static int amdgpu_device_doorbell_init(struct amdgpu_device *adev)
d38ceaf9 397{
705e519e
CK
398 /* No doorbell on SI hardware generation */
399 if (adev->asic_type < CHIP_BONAIRE) {
400 adev->doorbell.base = 0;
401 adev->doorbell.size = 0;
402 adev->doorbell.num_doorbells = 0;
403 adev->doorbell.ptr = NULL;
404 return 0;
405 }
406
d6895ad3
CK
407 if (pci_resource_flags(adev->pdev, 2) & IORESOURCE_UNSET)
408 return -EINVAL;
409
d38ceaf9
AD
410 /* doorbell bar mapping */
411 adev->doorbell.base = pci_resource_start(adev->pdev, 2);
412 adev->doorbell.size = pci_resource_len(adev->pdev, 2);
413
edf600da 414 adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
d38ceaf9
AD
415 AMDGPU_DOORBELL_MAX_ASSIGNMENT+1);
416 if (adev->doorbell.num_doorbells == 0)
417 return -EINVAL;
418
8972e5d2
CK
419 adev->doorbell.ptr = ioremap(adev->doorbell.base,
420 adev->doorbell.num_doorbells *
421 sizeof(u32));
422 if (adev->doorbell.ptr == NULL)
d38ceaf9 423 return -ENOMEM;
d38ceaf9
AD
424
425 return 0;
426}
427
428/**
06ec9070 429 * amdgpu_device_doorbell_fini - Tear down doorbell driver information.
d38ceaf9
AD
430 *
431 * @adev: amdgpu_device pointer
432 *
433 * Tear down doorbell driver information (CIK)
434 */
06ec9070 435static void amdgpu_device_doorbell_fini(struct amdgpu_device *adev)
d38ceaf9
AD
436{
437 iounmap(adev->doorbell.ptr);
438 adev->doorbell.ptr = NULL;
439}
440
441/**
442 * amdgpu_doorbell_get_kfd_info - Report doorbell configuration required to
443 * setup amdkfd
444 *
445 * @adev: amdgpu_device pointer
446 * @aperture_base: output returning doorbell aperture base physical address
447 * @aperture_size: output returning doorbell aperture size in bytes
448 * @start_offset: output returning # of doorbell bytes reserved for amdgpu.
449 *
450 * amdgpu and amdkfd share the doorbell aperture. amdgpu sets it up,
451 * takes doorbells required for its own rings and reports the setup to amdkfd.
452 * amdgpu reserved doorbells are at the start of the doorbell aperture.
453 */
454void amdgpu_doorbell_get_kfd_info(struct amdgpu_device *adev,
455 phys_addr_t *aperture_base,
456 size_t *aperture_size,
457 size_t *start_offset)
458{
459 /*
460 * The first num_doorbells are used by amdgpu.
461 * amdkfd takes whatever's left in the aperture.
462 */
463 if (adev->doorbell.size > adev->doorbell.num_doorbells * sizeof(u32)) {
464 *aperture_base = adev->doorbell.base;
465 *aperture_size = adev->doorbell.size;
466 *start_offset = adev->doorbell.num_doorbells * sizeof(u32);
467 } else {
468 *aperture_base = 0;
469 *aperture_size = 0;
470 *start_offset = 0;
471 }
472}
473
474/*
06ec9070 475 * amdgpu_device_wb_*()
455a7bc2 476 * Writeback is the method by which the GPU updates special pages in memory
ea81a173 477 * with the status of certain GPU events (fences, ring pointers,etc.).
d38ceaf9
AD
478 */
479
480/**
06ec9070 481 * amdgpu_device_wb_fini - Disable Writeback and free memory
d38ceaf9
AD
482 *
483 * @adev: amdgpu_device pointer
484 *
485 * Disables Writeback and frees the Writeback memory (all asics).
486 * Used at driver shutdown.
487 */
06ec9070 488static void amdgpu_device_wb_fini(struct amdgpu_device *adev)
d38ceaf9
AD
489{
490 if (adev->wb.wb_obj) {
a76ed485
AD
491 amdgpu_bo_free_kernel(&adev->wb.wb_obj,
492 &adev->wb.gpu_addr,
493 (void **)&adev->wb.wb);
d38ceaf9
AD
494 adev->wb.wb_obj = NULL;
495 }
496}
497
498/**
06ec9070 499 * amdgpu_device_wb_init- Init Writeback driver info and allocate memory
d38ceaf9
AD
500 *
501 * @adev: amdgpu_device pointer
502 *
455a7bc2 503 * Initializes writeback and allocates writeback memory (all asics).
d38ceaf9
AD
504 * Used at driver startup.
505 * Returns 0 on success or an -error on failure.
506 */
06ec9070 507static int amdgpu_device_wb_init(struct amdgpu_device *adev)
d38ceaf9
AD
508{
509 int r;
510
511 if (adev->wb.wb_obj == NULL) {
97407b63
AD
512 /* AMDGPU_MAX_WB * sizeof(uint32_t) * 8 = AMDGPU_MAX_WB 256bit slots */
513 r = amdgpu_bo_create_kernel(adev, AMDGPU_MAX_WB * sizeof(uint32_t) * 8,
a76ed485
AD
514 PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
515 &adev->wb.wb_obj, &adev->wb.gpu_addr,
516 (void **)&adev->wb.wb);
d38ceaf9
AD
517 if (r) {
518 dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
519 return r;
520 }
d38ceaf9
AD
521
522 adev->wb.num_wb = AMDGPU_MAX_WB;
523 memset(&adev->wb.used, 0, sizeof(adev->wb.used));
524
525 /* clear wb memory */
60a970a6 526 memset((char *)adev->wb.wb, 0, AMDGPU_MAX_WB * sizeof(uint32_t));
d38ceaf9
AD
527 }
528
529 return 0;
530}
531
532/**
533 * amdgpu_wb_get - Allocate a wb entry
534 *
535 * @adev: amdgpu_device pointer
536 * @wb: wb index
537 *
538 * Allocate a wb slot for use by the driver (all asics).
539 * Returns 0 on success or -EINVAL on failure.
540 */
541int amdgpu_wb_get(struct amdgpu_device *adev, u32 *wb)
542{
543 unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
d38ceaf9 544
97407b63 545 if (offset < adev->wb.num_wb) {
7014285a 546 __set_bit(offset, adev->wb.used);
63ae07ca 547 *wb = offset << 3; /* convert to dw offset */
0915fdbc
ML
548 return 0;
549 } else {
550 return -EINVAL;
551 }
552}
553
d38ceaf9
AD
554/**
555 * amdgpu_wb_free - Free a wb entry
556 *
557 * @adev: amdgpu_device pointer
558 * @wb: wb index
559 *
560 * Free a wb slot allocated for use by the driver (all asics)
561 */
562void amdgpu_wb_free(struct amdgpu_device *adev, u32 wb)
563{
564 if (wb < adev->wb.num_wb)
63ae07ca 565 __clear_bit(wb >> 3, adev->wb.used);
d38ceaf9
AD
566}
567
568/**
569 * amdgpu_vram_location - try to find VRAM location
570 * @adev: amdgpu device structure holding all necessary informations
571 * @mc: memory controller structure holding memory informations
572 * @base: base address at which to put VRAM
573 *
455a7bc2 574 * Function will try to place VRAM at base address provided
3d647c8f 575 * as parameter.
d38ceaf9
AD
576 */
577void amdgpu_vram_location(struct amdgpu_device *adev, struct amdgpu_mc *mc, u64 base)
578{
579 uint64_t limit = (uint64_t)amdgpu_vram_limit << 20;
580
581 mc->vram_start = base;
d38ceaf9
AD
582 mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
583 if (limit && limit < mc->real_vram_size)
584 mc->real_vram_size = limit;
585 dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
586 mc->mc_vram_size >> 20, mc->vram_start,
587 mc->vram_end, mc->real_vram_size >> 20);
588}
589
590/**
6f02a696 591 * amdgpu_gart_location - try to find GTT location
d38ceaf9
AD
592 * @adev: amdgpu device structure holding all necessary informations
593 * @mc: memory controller structure holding memory informations
594 *
595 * Function will place try to place GTT before or after VRAM.
596 *
597 * If GTT size is bigger than space left then we ajust GTT size.
598 * Thus function will never fails.
599 *
600 * FIXME: when reducing GTT size align new size on power of 2.
601 */
6f02a696 602void amdgpu_gart_location(struct amdgpu_device *adev, struct amdgpu_mc *mc)
d38ceaf9
AD
603{
604 u64 size_af, size_bf;
605
ed21c047
CK
606 size_af = adev->mc.mc_mask - mc->vram_end;
607 size_bf = mc->vram_start;
d38ceaf9 608 if (size_bf > size_af) {
6f02a696 609 if (mc->gart_size > size_bf) {
d38ceaf9 610 dev_warn(adev->dev, "limiting GTT\n");
6f02a696 611 mc->gart_size = size_bf;
d38ceaf9 612 }
6f02a696 613 mc->gart_start = 0;
d38ceaf9 614 } else {
6f02a696 615 if (mc->gart_size > size_af) {
d38ceaf9 616 dev_warn(adev->dev, "limiting GTT\n");
6f02a696 617 mc->gart_size = size_af;
d38ceaf9 618 }
b98f1b9e
CK
619 /* VCE doesn't like it when BOs cross a 4GB segment, so align
620 * the GART base on a 4GB boundary as well.
621 */
622 mc->gart_start = ALIGN(mc->vram_end + 1, 0x100000000ULL);
d38ceaf9 623 }
6f02a696 624 mc->gart_end = mc->gart_start + mc->gart_size - 1;
d38ceaf9 625 dev_info(adev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
6f02a696 626 mc->gart_size >> 20, mc->gart_start, mc->gart_end);
d38ceaf9
AD
627}
628
a05502e5
HC
629/*
630 * Firmware Reservation functions
631 */
632/**
633 * amdgpu_fw_reserve_vram_fini - free fw reserved vram
634 *
635 * @adev: amdgpu_device pointer
636 *
637 * free fw reserved vram if it has been reserved.
638 */
639void amdgpu_fw_reserve_vram_fini(struct amdgpu_device *adev)
640{
641 amdgpu_bo_free_kernel(&adev->fw_vram_usage.reserved_bo,
642 NULL, &adev->fw_vram_usage.va);
643}
644
645/**
646 * amdgpu_fw_reserve_vram_init - create bo vram reservation from fw
647 *
648 * @adev: amdgpu_device pointer
649 *
650 * create bo vram reservation from fw.
651 */
652int amdgpu_fw_reserve_vram_init(struct amdgpu_device *adev)
653{
c13c55d6 654 struct ttm_operation_ctx ctx = { false, false };
a05502e5 655 int r = 0;
3c738893 656 int i;
a05502e5 657 u64 vram_size = adev->mc.visible_vram_size;
3c738893
HC
658 u64 offset = adev->fw_vram_usage.start_offset;
659 u64 size = adev->fw_vram_usage.size;
660 struct amdgpu_bo *bo;
a05502e5
HC
661
662 adev->fw_vram_usage.va = NULL;
663 adev->fw_vram_usage.reserved_bo = NULL;
664
665 if (adev->fw_vram_usage.size > 0 &&
666 adev->fw_vram_usage.size <= vram_size) {
667
668 r = amdgpu_bo_create(adev, adev->fw_vram_usage.size,
3c738893 669 PAGE_SIZE, true, AMDGPU_GEM_DOMAIN_VRAM,
a05502e5
HC
670 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
671 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS, NULL, NULL, 0,
672 &adev->fw_vram_usage.reserved_bo);
673 if (r)
674 goto error_create;
675
676 r = amdgpu_bo_reserve(adev->fw_vram_usage.reserved_bo, false);
677 if (r)
678 goto error_reserve;
3c738893
HC
679
680 /* remove the original mem node and create a new one at the
681 * request position
682 */
683 bo = adev->fw_vram_usage.reserved_bo;
684 offset = ALIGN(offset, PAGE_SIZE);
685 for (i = 0; i < bo->placement.num_placement; ++i) {
686 bo->placements[i].fpfn = offset >> PAGE_SHIFT;
687 bo->placements[i].lpfn = (offset + size) >> PAGE_SHIFT;
688 }
689
690 ttm_bo_mem_put(&bo->tbo, &bo->tbo.mem);
c13c55d6
CK
691 r = ttm_bo_mem_space(&bo->tbo, &bo->placement,
692 &bo->tbo.mem, &ctx);
3c738893
HC
693 if (r)
694 goto error_pin;
695
a05502e5
HC
696 r = amdgpu_bo_pin_restricted(adev->fw_vram_usage.reserved_bo,
697 AMDGPU_GEM_DOMAIN_VRAM,
698 adev->fw_vram_usage.start_offset,
699 (adev->fw_vram_usage.start_offset +
9921167d 700 adev->fw_vram_usage.size), NULL);
a05502e5
HC
701 if (r)
702 goto error_pin;
703 r = amdgpu_bo_kmap(adev->fw_vram_usage.reserved_bo,
704 &adev->fw_vram_usage.va);
705 if (r)
706 goto error_kmap;
707
708 amdgpu_bo_unreserve(adev->fw_vram_usage.reserved_bo);
709 }
710 return r;
711
712error_kmap:
713 amdgpu_bo_unpin(adev->fw_vram_usage.reserved_bo);
714error_pin:
715 amdgpu_bo_unreserve(adev->fw_vram_usage.reserved_bo);
716error_reserve:
717 amdgpu_bo_unref(&adev->fw_vram_usage.reserved_bo);
718error_create:
719 adev->fw_vram_usage.va = NULL;
720 adev->fw_vram_usage.reserved_bo = NULL;
721 return r;
722}
723
d6895ad3
CK
724/**
725 * amdgpu_device_resize_fb_bar - try to resize FB BAR
726 *
727 * @adev: amdgpu_device pointer
728 *
729 * Try to resize FB BAR to make all VRAM CPU accessible. We try very hard not
730 * to fail, but if any of the BARs is not accessible after the size we abort
731 * driver loading by returning -ENODEV.
732 */
733int amdgpu_device_resize_fb_bar(struct amdgpu_device *adev)
734{
735 u64 space_needed = roundup_pow_of_two(adev->mc.real_vram_size);
736 u32 rbar_size = order_base_2(((space_needed >> 20) | 1)) - 1;
31b8adab
CK
737 struct pci_bus *root;
738 struct resource *res;
739 unsigned i;
d6895ad3
CK
740 u16 cmd;
741 int r;
742
0c03b912 743 /* Bypass for VF */
744 if (amdgpu_sriov_vf(adev))
745 return 0;
746
31b8adab
CK
747 /* Check if the root BUS has 64bit memory resources */
748 root = adev->pdev->bus;
749 while (root->parent)
750 root = root->parent;
751
752 pci_bus_for_each_resource(root, res, i) {
753 if (res && res->flags & IORESOURCE_MEM_64 &&
754 res->start > 0x100000000ull)
755 break;
756 }
757
758 /* Trying to resize is pointless without a root hub window above 4GB */
759 if (!res)
760 return 0;
761
d6895ad3
CK
762 /* Disable memory decoding while we change the BAR addresses and size */
763 pci_read_config_word(adev->pdev, PCI_COMMAND, &cmd);
764 pci_write_config_word(adev->pdev, PCI_COMMAND,
765 cmd & ~PCI_COMMAND_MEMORY);
766
767 /* Free the VRAM and doorbell BAR, we most likely need to move both. */
06ec9070 768 amdgpu_device_doorbell_fini(adev);
d6895ad3
CK
769 if (adev->asic_type >= CHIP_BONAIRE)
770 pci_release_resource(adev->pdev, 2);
771
772 pci_release_resource(adev->pdev, 0);
773
774 r = pci_resize_resource(adev->pdev, 0, rbar_size);
775 if (r == -ENOSPC)
776 DRM_INFO("Not enough PCI address space for a large BAR.");
777 else if (r && r != -ENOTSUPP)
778 DRM_ERROR("Problem resizing BAR0 (%d).", r);
779
780 pci_assign_unassigned_bus_resources(adev->pdev->bus);
781
782 /* When the doorbell or fb BAR isn't available we have no chance of
783 * using the device.
784 */
06ec9070 785 r = amdgpu_device_doorbell_init(adev);
d6895ad3
CK
786 if (r || (pci_resource_flags(adev->pdev, 0) & IORESOURCE_UNSET))
787 return -ENODEV;
788
789 pci_write_config_word(adev->pdev, PCI_COMMAND, cmd);
790
791 return 0;
792}
a05502e5 793
d38ceaf9
AD
794/*
795 * GPU helpers function.
796 */
797/**
c836fec5 798 * amdgpu_need_post - check if the hw need post or not
d38ceaf9
AD
799 *
800 * @adev: amdgpu_device pointer
801 *
c836fec5
JQ
802 * Check if the asic has been initialized (all asics) at driver startup
803 * or post is needed if hw reset is performed.
804 * Returns true if need or false if not.
d38ceaf9 805 */
c836fec5 806bool amdgpu_need_post(struct amdgpu_device *adev)
d38ceaf9
AD
807{
808 uint32_t reg;
809
bec86378
ML
810 if (amdgpu_sriov_vf(adev))
811 return false;
812
813 if (amdgpu_passthrough(adev)) {
1da2c326
ML
814 /* for FIJI: In whole GPU pass-through virtualization case, after VM reboot
815 * some old smc fw still need driver do vPost otherwise gpu hang, while
816 * those smc fw version above 22.15 doesn't have this flaw, so we force
817 * vpost executed for smc version below 22.15
bec86378
ML
818 */
819 if (adev->asic_type == CHIP_FIJI) {
820 int err;
821 uint32_t fw_ver;
822 err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
823 /* force vPost if error occured */
824 if (err)
825 return true;
826
827 fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
1da2c326
ML
828 if (fw_ver < 0x00160e00)
829 return true;
bec86378 830 }
bec86378 831 }
91fe77eb 832
833 if (adev->has_hw_reset) {
834 adev->has_hw_reset = false;
835 return true;
836 }
837
838 /* bios scratch used on CIK+ */
839 if (adev->asic_type >= CHIP_BONAIRE)
840 return amdgpu_atombios_scratch_need_asic_init(adev);
841
842 /* check MEM_SIZE for older asics */
843 reg = amdgpu_asic_get_config_memsize(adev);
844
845 if ((reg != 0) && (reg != 0xffffffff))
846 return false;
847
848 return true;
bec86378
ML
849}
850
d38ceaf9
AD
851/**
852 * amdgpu_dummy_page_init - init dummy page used by the driver
853 *
854 * @adev: amdgpu_device pointer
855 *
856 * Allocate the dummy page used by the driver (all asics).
857 * This dummy page is used by the driver as a filler for gart entries
858 * when pages are taken out of the GART
859 * Returns 0 on sucess, -ENOMEM on failure.
860 */
861int amdgpu_dummy_page_init(struct amdgpu_device *adev)
862{
863 if (adev->dummy_page.page)
864 return 0;
865 adev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
866 if (adev->dummy_page.page == NULL)
867 return -ENOMEM;
868 adev->dummy_page.addr = pci_map_page(adev->pdev, adev->dummy_page.page,
869 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
870 if (pci_dma_mapping_error(adev->pdev, adev->dummy_page.addr)) {
871 dev_err(&adev->pdev->dev, "Failed to DMA MAP the dummy page\n");
872 __free_page(adev->dummy_page.page);
873 adev->dummy_page.page = NULL;
874 return -ENOMEM;
875 }
876 return 0;
877}
878
879/**
880 * amdgpu_dummy_page_fini - free dummy page used by the driver
881 *
882 * @adev: amdgpu_device pointer
883 *
884 * Frees the dummy page used by the driver (all asics).
885 */
886void amdgpu_dummy_page_fini(struct amdgpu_device *adev)
887{
888 if (adev->dummy_page.page == NULL)
889 return;
890 pci_unmap_page(adev->pdev, adev->dummy_page.addr,
891 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
892 __free_page(adev->dummy_page.page);
893 adev->dummy_page.page = NULL;
894}
895
d38ceaf9
AD
896/* if we get transitioned to only one device, take VGA back */
897/**
06ec9070 898 * amdgpu_device_vga_set_decode - enable/disable vga decode
d38ceaf9
AD
899 *
900 * @cookie: amdgpu_device pointer
901 * @state: enable/disable vga decode
902 *
903 * Enable/disable vga decode (all asics).
904 * Returns VGA resource flags.
905 */
06ec9070 906static unsigned int amdgpu_device_vga_set_decode(void *cookie, bool state)
d38ceaf9
AD
907{
908 struct amdgpu_device *adev = cookie;
909 amdgpu_asic_set_vga_state(adev, state);
910 if (state)
911 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
912 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
913 else
914 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
915}
916
06ec9070 917static void amdgpu_device_check_block_size(struct amdgpu_device *adev)
a1adf8be
CZ
918{
919 /* defines number of bits in page table versus page directory,
920 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
921 * page table and the remaining bits are in the page directory */
bab4fee7
JZ
922 if (amdgpu_vm_block_size == -1)
923 return;
a1adf8be 924
bab4fee7 925 if (amdgpu_vm_block_size < 9) {
a1adf8be
CZ
926 dev_warn(adev->dev, "VM page table size (%d) too small\n",
927 amdgpu_vm_block_size);
97489129 928 amdgpu_vm_block_size = -1;
a1adf8be 929 }
a1adf8be
CZ
930}
931
06ec9070 932static void amdgpu_device_check_vm_size(struct amdgpu_device *adev)
83ca145d 933{
64dab074
AD
934 /* no need to check the default value */
935 if (amdgpu_vm_size == -1)
936 return;
937
83ca145d
ZJ
938 if (amdgpu_vm_size < 1) {
939 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
940 amdgpu_vm_size);
f3368128 941 amdgpu_vm_size = -1;
83ca145d 942 }
83ca145d
ZJ
943}
944
d38ceaf9 945/**
06ec9070 946 * amdgpu_device_check_arguments - validate module params
d38ceaf9
AD
947 *
948 * @adev: amdgpu_device pointer
949 *
950 * Validates certain module parameters and updates
951 * the associated values used by the driver (all asics).
952 */
06ec9070 953static void amdgpu_device_check_arguments(struct amdgpu_device *adev)
d38ceaf9 954{
5b011235
CZ
955 if (amdgpu_sched_jobs < 4) {
956 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
957 amdgpu_sched_jobs);
958 amdgpu_sched_jobs = 4;
76117507 959 } else if (!is_power_of_2(amdgpu_sched_jobs)){
5b011235
CZ
960 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
961 amdgpu_sched_jobs);
962 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
963 }
d38ceaf9 964
83e74db6 965 if (amdgpu_gart_size != -1 && amdgpu_gart_size < 32) {
f9321cc4
CK
966 /* gart size must be greater or equal to 32M */
967 dev_warn(adev->dev, "gart size (%d) too small\n",
968 amdgpu_gart_size);
83e74db6 969 amdgpu_gart_size = -1;
d38ceaf9
AD
970 }
971
36d38372 972 if (amdgpu_gtt_size != -1 && amdgpu_gtt_size < 32) {
c4e1a13a 973 /* gtt size must be greater or equal to 32M */
36d38372
CK
974 dev_warn(adev->dev, "gtt size (%d) too small\n",
975 amdgpu_gtt_size);
976 amdgpu_gtt_size = -1;
d38ceaf9
AD
977 }
978
d07f14be
RH
979 /* valid range is between 4 and 9 inclusive */
980 if (amdgpu_vm_fragment_size != -1 &&
981 (amdgpu_vm_fragment_size > 9 || amdgpu_vm_fragment_size < 4)) {
982 dev_warn(adev->dev, "valid range is between 4 and 9\n");
983 amdgpu_vm_fragment_size = -1;
984 }
985
06ec9070 986 amdgpu_device_check_vm_size(adev);
d38ceaf9 987
06ec9070 988 amdgpu_device_check_block_size(adev);
6a7f76e7 989
526bae37 990 if (amdgpu_vram_page_split != -1 && (amdgpu_vram_page_split < 16 ||
76117507 991 !is_power_of_2(amdgpu_vram_page_split))) {
6a7f76e7
CK
992 dev_warn(adev->dev, "invalid VRAM page split (%d)\n",
993 amdgpu_vram_page_split);
994 amdgpu_vram_page_split = 1024;
995 }
8854695a
AG
996
997 if (amdgpu_lockup_timeout == 0) {
998 dev_warn(adev->dev, "lockup_timeout msut be > 0, adjusting to 10000\n");
999 amdgpu_lockup_timeout = 10000;
1000 }
d38ceaf9
AD
1001}
1002
1003/**
1004 * amdgpu_switcheroo_set_state - set switcheroo state
1005 *
1006 * @pdev: pci dev pointer
1694467b 1007 * @state: vga_switcheroo state
d38ceaf9
AD
1008 *
1009 * Callback for the switcheroo driver. Suspends or resumes the
1010 * the asics before or after it is powered up using ACPI methods.
1011 */
1012static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1013{
1014 struct drm_device *dev = pci_get_drvdata(pdev);
1015
1016 if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
1017 return;
1018
1019 if (state == VGA_SWITCHEROO_ON) {
7ca85295 1020 pr_info("amdgpu: switched on\n");
d38ceaf9
AD
1021 /* don't suspend or resume card normally */
1022 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1023
810ddc3a 1024 amdgpu_device_resume(dev, true, true);
d38ceaf9 1025
d38ceaf9
AD
1026 dev->switch_power_state = DRM_SWITCH_POWER_ON;
1027 drm_kms_helper_poll_enable(dev);
1028 } else {
7ca85295 1029 pr_info("amdgpu: switched off\n");
d38ceaf9
AD
1030 drm_kms_helper_poll_disable(dev);
1031 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
810ddc3a 1032 amdgpu_device_suspend(dev, true, true);
d38ceaf9
AD
1033 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1034 }
1035}
1036
1037/**
1038 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1039 *
1040 * @pdev: pci dev pointer
1041 *
1042 * Callback for the switcheroo driver. Check of the switcheroo
1043 * state can be changed.
1044 * Returns true if the state can be changed, false if not.
1045 */
1046static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1047{
1048 struct drm_device *dev = pci_get_drvdata(pdev);
1049
1050 /*
1051 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1052 * locking inversion with the driver load path. And the access here is
1053 * completely racy anyway. So don't bother with locking for now.
1054 */
1055 return dev->open_count == 0;
1056}
1057
1058static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1059 .set_gpu_state = amdgpu_switcheroo_set_state,
1060 .reprobe = NULL,
1061 .can_switch = amdgpu_switcheroo_can_switch,
1062};
1063
1064int amdgpu_set_clockgating_state(struct amdgpu_device *adev,
5fc3aeeb 1065 enum amd_ip_block_type block_type,
1066 enum amd_clockgating_state state)
d38ceaf9
AD
1067{
1068 int i, r = 0;
1069
1070 for (i = 0; i < adev->num_ip_blocks; i++) {
a1255107 1071 if (!adev->ip_blocks[i].status.valid)
9ecbe7f5 1072 continue;
c722865a
RZ
1073 if (adev->ip_blocks[i].version->type != block_type)
1074 continue;
1075 if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
1076 continue;
1077 r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
1078 (void *)adev, state);
1079 if (r)
1080 DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
1081 adev->ip_blocks[i].version->funcs->name, r);
d38ceaf9
AD
1082 }
1083 return r;
1084}
1085
1086int amdgpu_set_powergating_state(struct amdgpu_device *adev,
5fc3aeeb 1087 enum amd_ip_block_type block_type,
1088 enum amd_powergating_state state)
d38ceaf9
AD
1089{
1090 int i, r = 0;
1091
1092 for (i = 0; i < adev->num_ip_blocks; i++) {
a1255107 1093 if (!adev->ip_blocks[i].status.valid)
9ecbe7f5 1094 continue;
c722865a
RZ
1095 if (adev->ip_blocks[i].version->type != block_type)
1096 continue;
1097 if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
1098 continue;
1099 r = adev->ip_blocks[i].version->funcs->set_powergating_state(
1100 (void *)adev, state);
1101 if (r)
1102 DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
1103 adev->ip_blocks[i].version->funcs->name, r);
d38ceaf9
AD
1104 }
1105 return r;
1106}
1107
6cb2d4e4
HR
1108void amdgpu_get_clockgating_state(struct amdgpu_device *adev, u32 *flags)
1109{
1110 int i;
1111
1112 for (i = 0; i < adev->num_ip_blocks; i++) {
1113 if (!adev->ip_blocks[i].status.valid)
1114 continue;
1115 if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
1116 adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
1117 }
1118}
1119
5dbbb60b
AD
1120int amdgpu_wait_for_idle(struct amdgpu_device *adev,
1121 enum amd_ip_block_type block_type)
1122{
1123 int i, r;
1124
1125 for (i = 0; i < adev->num_ip_blocks; i++) {
a1255107 1126 if (!adev->ip_blocks[i].status.valid)
9ecbe7f5 1127 continue;
a1255107
AD
1128 if (adev->ip_blocks[i].version->type == block_type) {
1129 r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
5dbbb60b
AD
1130 if (r)
1131 return r;
1132 break;
1133 }
1134 }
1135 return 0;
1136
1137}
1138
1139bool amdgpu_is_idle(struct amdgpu_device *adev,
1140 enum amd_ip_block_type block_type)
1141{
1142 int i;
1143
1144 for (i = 0; i < adev->num_ip_blocks; i++) {
a1255107 1145 if (!adev->ip_blocks[i].status.valid)
9ecbe7f5 1146 continue;
a1255107
AD
1147 if (adev->ip_blocks[i].version->type == block_type)
1148 return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
5dbbb60b
AD
1149 }
1150 return true;
1151
1152}
1153
a1255107
AD
1154struct amdgpu_ip_block * amdgpu_get_ip_block(struct amdgpu_device *adev,
1155 enum amd_ip_block_type type)
d38ceaf9
AD
1156{
1157 int i;
1158
1159 for (i = 0; i < adev->num_ip_blocks; i++)
a1255107 1160 if (adev->ip_blocks[i].version->type == type)
d38ceaf9
AD
1161 return &adev->ip_blocks[i];
1162
1163 return NULL;
1164}
1165
1166/**
1167 * amdgpu_ip_block_version_cmp
1168 *
1169 * @adev: amdgpu_device pointer
5fc3aeeb 1170 * @type: enum amd_ip_block_type
d38ceaf9
AD
1171 * @major: major version
1172 * @minor: minor version
1173 *
1174 * return 0 if equal or greater
1175 * return 1 if smaller or the ip_block doesn't exist
1176 */
1177int amdgpu_ip_block_version_cmp(struct amdgpu_device *adev,
5fc3aeeb 1178 enum amd_ip_block_type type,
d38ceaf9
AD
1179 u32 major, u32 minor)
1180{
a1255107 1181 struct amdgpu_ip_block *ip_block = amdgpu_get_ip_block(adev, type);
d38ceaf9 1182
a1255107
AD
1183 if (ip_block && ((ip_block->version->major > major) ||
1184 ((ip_block->version->major == major) &&
1185 (ip_block->version->minor >= minor))))
d38ceaf9
AD
1186 return 0;
1187
1188 return 1;
1189}
1190
a1255107
AD
1191/**
1192 * amdgpu_ip_block_add
1193 *
1194 * @adev: amdgpu_device pointer
1195 * @ip_block_version: pointer to the IP to add
1196 *
1197 * Adds the IP block driver information to the collection of IPs
1198 * on the asic.
1199 */
1200int amdgpu_ip_block_add(struct amdgpu_device *adev,
1201 const struct amdgpu_ip_block_version *ip_block_version)
1202{
1203 if (!ip_block_version)
1204 return -EINVAL;
1205
a0bae357
HR
1206 DRM_DEBUG("add ip block number %d <%s>\n", adev->num_ip_blocks,
1207 ip_block_version->funcs->name);
1208
a1255107
AD
1209 adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
1210
1211 return 0;
1212}
1213
483ef985 1214static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
9accf2fd
ED
1215{
1216 adev->enable_virtual_display = false;
1217
1218 if (amdgpu_virtual_display) {
1219 struct drm_device *ddev = adev->ddev;
1220 const char *pci_address_name = pci_name(ddev->pdev);
0f66356d 1221 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
9accf2fd
ED
1222
1223 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1224 pciaddstr_tmp = pciaddstr;
0f66356d
ED
1225 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1226 pciaddname = strsep(&pciaddname_tmp, ",");
967de2a9
YT
1227 if (!strcmp("all", pciaddname)
1228 || !strcmp(pci_address_name, pciaddname)) {
0f66356d
ED
1229 long num_crtc;
1230 int res = -1;
1231
9accf2fd 1232 adev->enable_virtual_display = true;
0f66356d
ED
1233
1234 if (pciaddname_tmp)
1235 res = kstrtol(pciaddname_tmp, 10,
1236 &num_crtc);
1237
1238 if (!res) {
1239 if (num_crtc < 1)
1240 num_crtc = 1;
1241 if (num_crtc > 6)
1242 num_crtc = 6;
1243 adev->mode_info.num_crtc = num_crtc;
1244 } else {
1245 adev->mode_info.num_crtc = 1;
1246 }
9accf2fd
ED
1247 break;
1248 }
1249 }
1250
0f66356d
ED
1251 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
1252 amdgpu_virtual_display, pci_address_name,
1253 adev->enable_virtual_display, adev->mode_info.num_crtc);
9accf2fd
ED
1254
1255 kfree(pciaddstr);
1256 }
1257}
1258
e2a75f88
AD
1259static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev)
1260{
e2a75f88
AD
1261 const char *chip_name;
1262 char fw_name[30];
1263 int err;
1264 const struct gpu_info_firmware_header_v1_0 *hdr;
1265
ab4fe3e1
HR
1266 adev->firmware.gpu_info_fw = NULL;
1267
e2a75f88
AD
1268 switch (adev->asic_type) {
1269 case CHIP_TOPAZ:
1270 case CHIP_TONGA:
1271 case CHIP_FIJI:
1272 case CHIP_POLARIS11:
1273 case CHIP_POLARIS10:
1274 case CHIP_POLARIS12:
1275 case CHIP_CARRIZO:
1276 case CHIP_STONEY:
1277#ifdef CONFIG_DRM_AMDGPU_SI
1278 case CHIP_VERDE:
1279 case CHIP_TAHITI:
1280 case CHIP_PITCAIRN:
1281 case CHIP_OLAND:
1282 case CHIP_HAINAN:
1283#endif
1284#ifdef CONFIG_DRM_AMDGPU_CIK
1285 case CHIP_BONAIRE:
1286 case CHIP_HAWAII:
1287 case CHIP_KAVERI:
1288 case CHIP_KABINI:
1289 case CHIP_MULLINS:
1290#endif
1291 default:
1292 return 0;
1293 case CHIP_VEGA10:
1294 chip_name = "vega10";
1295 break;
2d2e5e7e
AD
1296 case CHIP_RAVEN:
1297 chip_name = "raven";
1298 break;
e2a75f88
AD
1299 }
1300
1301 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name);
ab4fe3e1 1302 err = request_firmware(&adev->firmware.gpu_info_fw, fw_name, adev->dev);
e2a75f88
AD
1303 if (err) {
1304 dev_err(adev->dev,
1305 "Failed to load gpu_info firmware \"%s\"\n",
1306 fw_name);
1307 goto out;
1308 }
ab4fe3e1 1309 err = amdgpu_ucode_validate(adev->firmware.gpu_info_fw);
e2a75f88
AD
1310 if (err) {
1311 dev_err(adev->dev,
1312 "Failed to validate gpu_info firmware \"%s\"\n",
1313 fw_name);
1314 goto out;
1315 }
1316
ab4fe3e1 1317 hdr = (const struct gpu_info_firmware_header_v1_0 *)adev->firmware.gpu_info_fw->data;
e2a75f88
AD
1318 amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
1319
1320 switch (hdr->version_major) {
1321 case 1:
1322 {
1323 const struct gpu_info_firmware_v1_0 *gpu_info_fw =
ab4fe3e1 1324 (const struct gpu_info_firmware_v1_0 *)(adev->firmware.gpu_info_fw->data +
e2a75f88
AD
1325 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1326
b5ab16bf
AD
1327 adev->gfx.config.max_shader_engines = le32_to_cpu(gpu_info_fw->gc_num_se);
1328 adev->gfx.config.max_cu_per_sh = le32_to_cpu(gpu_info_fw->gc_num_cu_per_sh);
1329 adev->gfx.config.max_sh_per_se = le32_to_cpu(gpu_info_fw->gc_num_sh_per_se);
1330 adev->gfx.config.max_backends_per_se = le32_to_cpu(gpu_info_fw->gc_num_rb_per_se);
e2a75f88 1331 adev->gfx.config.max_texture_channel_caches =
b5ab16bf
AD
1332 le32_to_cpu(gpu_info_fw->gc_num_tccs);
1333 adev->gfx.config.max_gprs = le32_to_cpu(gpu_info_fw->gc_num_gprs);
1334 adev->gfx.config.max_gs_threads = le32_to_cpu(gpu_info_fw->gc_num_max_gs_thds);
1335 adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gpu_info_fw->gc_gs_table_depth);
1336 adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gpu_info_fw->gc_gsprim_buff_depth);
e2a75f88 1337 adev->gfx.config.double_offchip_lds_buf =
b5ab16bf
AD
1338 le32_to_cpu(gpu_info_fw->gc_double_offchip_lds_buffer);
1339 adev->gfx.cu_info.wave_front_size = le32_to_cpu(gpu_info_fw->gc_wave_size);
51fd0370
HZ
1340 adev->gfx.cu_info.max_waves_per_simd =
1341 le32_to_cpu(gpu_info_fw->gc_max_waves_per_simd);
1342 adev->gfx.cu_info.max_scratch_slots_per_cu =
1343 le32_to_cpu(gpu_info_fw->gc_max_scratch_slots_per_cu);
1344 adev->gfx.cu_info.lds_size = le32_to_cpu(gpu_info_fw->gc_lds_size);
e2a75f88
AD
1345 break;
1346 }
1347 default:
1348 dev_err(adev->dev,
1349 "Unsupported gpu_info table %d\n", hdr->header.ucode_version);
1350 err = -EINVAL;
1351 goto out;
1352 }
1353out:
e2a75f88
AD
1354 return err;
1355}
1356
06ec9070 1357static int amdgpu_device_ip_early_init(struct amdgpu_device *adev)
d38ceaf9 1358{
aaa36a97 1359 int i, r;
d38ceaf9 1360
483ef985 1361 amdgpu_device_enable_virtual_display(adev);
a6be7570 1362
d38ceaf9 1363 switch (adev->asic_type) {
aaa36a97
AD
1364 case CHIP_TOPAZ:
1365 case CHIP_TONGA:
48299f95 1366 case CHIP_FIJI:
2cc0c0b5
FC
1367 case CHIP_POLARIS11:
1368 case CHIP_POLARIS10:
c4642a47 1369 case CHIP_POLARIS12:
aaa36a97 1370 case CHIP_CARRIZO:
39bb0c92
SL
1371 case CHIP_STONEY:
1372 if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
aaa36a97
AD
1373 adev->family = AMDGPU_FAMILY_CZ;
1374 else
1375 adev->family = AMDGPU_FAMILY_VI;
1376
1377 r = vi_set_ip_blocks(adev);
1378 if (r)
1379 return r;
1380 break;
33f34802
KW
1381#ifdef CONFIG_DRM_AMDGPU_SI
1382 case CHIP_VERDE:
1383 case CHIP_TAHITI:
1384 case CHIP_PITCAIRN:
1385 case CHIP_OLAND:
1386 case CHIP_HAINAN:
295d0daf 1387 adev->family = AMDGPU_FAMILY_SI;
33f34802
KW
1388 r = si_set_ip_blocks(adev);
1389 if (r)
1390 return r;
1391 break;
1392#endif
a2e73f56
AD
1393#ifdef CONFIG_DRM_AMDGPU_CIK
1394 case CHIP_BONAIRE:
1395 case CHIP_HAWAII:
1396 case CHIP_KAVERI:
1397 case CHIP_KABINI:
1398 case CHIP_MULLINS:
1399 if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
1400 adev->family = AMDGPU_FAMILY_CI;
1401 else
1402 adev->family = AMDGPU_FAMILY_KV;
1403
1404 r = cik_set_ip_blocks(adev);
1405 if (r)
1406 return r;
1407 break;
1408#endif
2ca8a5d2
CZ
1409 case CHIP_VEGA10:
1410 case CHIP_RAVEN:
1411 if (adev->asic_type == CHIP_RAVEN)
1412 adev->family = AMDGPU_FAMILY_RV;
1413 else
1414 adev->family = AMDGPU_FAMILY_AI;
460826e6
KW
1415
1416 r = soc15_set_ip_blocks(adev);
1417 if (r)
1418 return r;
1419 break;
d38ceaf9
AD
1420 default:
1421 /* FIXME: not supported yet */
1422 return -EINVAL;
1423 }
1424
e2a75f88
AD
1425 r = amdgpu_device_parse_gpu_info_fw(adev);
1426 if (r)
1427 return r;
1428
1884734a 1429 amdgpu_amdkfd_device_probe(adev);
1430
3149d9da
XY
1431 if (amdgpu_sriov_vf(adev)) {
1432 r = amdgpu_virt_request_full_gpu(adev, true);
1433 if (r)
5ffa61c1 1434 return -EAGAIN;
3149d9da
XY
1435 }
1436
d38ceaf9
AD
1437 for (i = 0; i < adev->num_ip_blocks; i++) {
1438 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
ed8cf00c
HR
1439 DRM_ERROR("disabled ip block: %d <%s>\n",
1440 i, adev->ip_blocks[i].version->funcs->name);
a1255107 1441 adev->ip_blocks[i].status.valid = false;
d38ceaf9 1442 } else {
a1255107
AD
1443 if (adev->ip_blocks[i].version->funcs->early_init) {
1444 r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
2c1a2784 1445 if (r == -ENOENT) {
a1255107 1446 adev->ip_blocks[i].status.valid = false;
2c1a2784 1447 } else if (r) {
a1255107
AD
1448 DRM_ERROR("early_init of IP block <%s> failed %d\n",
1449 adev->ip_blocks[i].version->funcs->name, r);
d38ceaf9 1450 return r;
2c1a2784 1451 } else {
a1255107 1452 adev->ip_blocks[i].status.valid = true;
2c1a2784 1453 }
974e6b64 1454 } else {
a1255107 1455 adev->ip_blocks[i].status.valid = true;
d38ceaf9 1456 }
d38ceaf9
AD
1457 }
1458 }
1459
395d1fb9
NH
1460 adev->cg_flags &= amdgpu_cg_mask;
1461 adev->pg_flags &= amdgpu_pg_mask;
1462
d38ceaf9
AD
1463 return 0;
1464}
1465
06ec9070 1466static int amdgpu_device_ip_init(struct amdgpu_device *adev)
d38ceaf9
AD
1467{
1468 int i, r;
1469
1470 for (i = 0; i < adev->num_ip_blocks; i++) {
a1255107 1471 if (!adev->ip_blocks[i].status.valid)
d38ceaf9 1472 continue;
a1255107 1473 r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
2c1a2784 1474 if (r) {
a1255107
AD
1475 DRM_ERROR("sw_init of IP block <%s> failed %d\n",
1476 adev->ip_blocks[i].version->funcs->name, r);
d38ceaf9 1477 return r;
2c1a2784 1478 }
a1255107 1479 adev->ip_blocks[i].status.sw = true;
d38ceaf9 1480 /* need to do gmc hw init early so we can allocate gpu mem */
a1255107 1481 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
06ec9070 1482 r = amdgpu_device_vram_scratch_init(adev);
2c1a2784
AD
1483 if (r) {
1484 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
d38ceaf9 1485 return r;
2c1a2784 1486 }
a1255107 1487 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
2c1a2784
AD
1488 if (r) {
1489 DRM_ERROR("hw_init %d failed %d\n", i, r);
d38ceaf9 1490 return r;
2c1a2784 1491 }
06ec9070 1492 r = amdgpu_device_wb_init(adev);
2c1a2784 1493 if (r) {
06ec9070 1494 DRM_ERROR("amdgpu_device_wb_init failed %d\n", r);
d38ceaf9 1495 return r;
2c1a2784 1496 }
a1255107 1497 adev->ip_blocks[i].status.hw = true;
2493664f
ML
1498
1499 /* right after GMC hw init, we create CSA */
1500 if (amdgpu_sriov_vf(adev)) {
1501 r = amdgpu_allocate_static_csa(adev);
1502 if (r) {
1503 DRM_ERROR("allocate CSA failed %d\n", r);
1504 return r;
1505 }
1506 }
d38ceaf9
AD
1507 }
1508 }
1509
1510 for (i = 0; i < adev->num_ip_blocks; i++) {
a1255107 1511 if (!adev->ip_blocks[i].status.sw)
d38ceaf9
AD
1512 continue;
1513 /* gmc hw init is done early */
a1255107 1514 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC)
d38ceaf9 1515 continue;
a1255107 1516 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
2c1a2784 1517 if (r) {
a1255107
AD
1518 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
1519 adev->ip_blocks[i].version->funcs->name, r);
d38ceaf9 1520 return r;
2c1a2784 1521 }
a1255107 1522 adev->ip_blocks[i].status.hw = true;
d38ceaf9
AD
1523 }
1524
1884734a 1525 amdgpu_amdkfd_device_init(adev);
c6332b97 1526
1527 if (amdgpu_sriov_vf(adev))
1528 amdgpu_virt_release_full_gpu(adev, true);
1529
d38ceaf9
AD
1530 return 0;
1531}
1532
06ec9070 1533static void amdgpu_device_fill_reset_magic(struct amdgpu_device *adev)
0c49e0b8
CZ
1534{
1535 memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM);
1536}
1537
06ec9070 1538static bool amdgpu_device_check_vram_lost(struct amdgpu_device *adev)
0c49e0b8
CZ
1539{
1540 return !!memcmp(adev->gart.ptr, adev->reset_magic,
1541 AMDGPU_RESET_MAGIC_NUM);
1542}
1543
06ec9070 1544static int amdgpu_device_ip_late_set_cg_state(struct amdgpu_device *adev)
d38ceaf9
AD
1545{
1546 int i = 0, r;
1547
1548 for (i = 0; i < adev->num_ip_blocks; i++) {
a1255107 1549 if (!adev->ip_blocks[i].status.valid)
d38ceaf9 1550 continue;
4a446d55 1551 /* skip CG for VCE/UVD, it's handled specially */
a1255107
AD
1552 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1553 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
4a446d55 1554 /* enable clockgating to save power */
a1255107
AD
1555 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1556 AMD_CG_STATE_GATE);
4a446d55
AD
1557 if (r) {
1558 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
a1255107 1559 adev->ip_blocks[i].version->funcs->name, r);
4a446d55
AD
1560 return r;
1561 }
b0b00ff1 1562 }
d38ceaf9 1563 }
2dc80b00
S
1564 return 0;
1565}
1566
06ec9070 1567static int amdgpu_device_ip_late_init(struct amdgpu_device *adev)
2dc80b00
S
1568{
1569 int i = 0, r;
1570
1571 for (i = 0; i < adev->num_ip_blocks; i++) {
1572 if (!adev->ip_blocks[i].status.valid)
1573 continue;
1574 if (adev->ip_blocks[i].version->funcs->late_init) {
1575 r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
1576 if (r) {
1577 DRM_ERROR("late_init of IP block <%s> failed %d\n",
1578 adev->ip_blocks[i].version->funcs->name, r);
1579 return r;
1580 }
1581 adev->ip_blocks[i].status.late_initialized = true;
1582 }
1583 }
1584
1585 mod_delayed_work(system_wq, &adev->late_init_work,
1586 msecs_to_jiffies(AMDGPU_RESUME_MS));
d38ceaf9 1587
06ec9070 1588 amdgpu_device_fill_reset_magic(adev);
d38ceaf9
AD
1589
1590 return 0;
1591}
1592
06ec9070 1593static int amdgpu_device_ip_fini(struct amdgpu_device *adev)
d38ceaf9
AD
1594{
1595 int i, r;
1596
1884734a 1597 amdgpu_amdkfd_device_fini(adev);
3e96dbfd
AD
1598 /* need to disable SMC first */
1599 for (i = 0; i < adev->num_ip_blocks; i++) {
a1255107 1600 if (!adev->ip_blocks[i].status.hw)
3e96dbfd 1601 continue;
a1255107 1602 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
3e96dbfd 1603 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
a1255107
AD
1604 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1605 AMD_CG_STATE_UNGATE);
3e96dbfd
AD
1606 if (r) {
1607 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
a1255107 1608 adev->ip_blocks[i].version->funcs->name, r);
3e96dbfd
AD
1609 return r;
1610 }
a1255107 1611 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
3e96dbfd
AD
1612 /* XXX handle errors */
1613 if (r) {
1614 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
a1255107 1615 adev->ip_blocks[i].version->funcs->name, r);
3e96dbfd 1616 }
a1255107 1617 adev->ip_blocks[i].status.hw = false;
3e96dbfd
AD
1618 break;
1619 }
1620 }
1621
d38ceaf9 1622 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
a1255107 1623 if (!adev->ip_blocks[i].status.hw)
d38ceaf9 1624 continue;
a1255107 1625 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
84e5b516 1626 amdgpu_free_static_csa(adev);
06ec9070
AD
1627 amdgpu_device_wb_fini(adev);
1628 amdgpu_device_vram_scratch_fini(adev);
d38ceaf9 1629 }
8201a67a
RZ
1630
1631 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1632 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1633 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
1634 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1635 AMD_CG_STATE_UNGATE);
1636 if (r) {
1637 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1638 adev->ip_blocks[i].version->funcs->name, r);
1639 return r;
1640 }
2c1a2784 1641 }
8201a67a 1642
a1255107 1643 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
d38ceaf9 1644 /* XXX handle errors */
2c1a2784 1645 if (r) {
a1255107
AD
1646 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
1647 adev->ip_blocks[i].version->funcs->name, r);
2c1a2784 1648 }
8201a67a 1649
a1255107 1650 adev->ip_blocks[i].status.hw = false;
d38ceaf9
AD
1651 }
1652
1653 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
a1255107 1654 if (!adev->ip_blocks[i].status.sw)
d38ceaf9 1655 continue;
a1255107 1656 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
d38ceaf9 1657 /* XXX handle errors */
2c1a2784 1658 if (r) {
a1255107
AD
1659 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
1660 adev->ip_blocks[i].version->funcs->name, r);
2c1a2784 1661 }
a1255107
AD
1662 adev->ip_blocks[i].status.sw = false;
1663 adev->ip_blocks[i].status.valid = false;
d38ceaf9
AD
1664 }
1665
a6dcfd9c 1666 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
a1255107 1667 if (!adev->ip_blocks[i].status.late_initialized)
8a2eef1d 1668 continue;
a1255107
AD
1669 if (adev->ip_blocks[i].version->funcs->late_fini)
1670 adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
1671 adev->ip_blocks[i].status.late_initialized = false;
a6dcfd9c
ML
1672 }
1673
030308fc 1674 if (amdgpu_sriov_vf(adev))
24136135
ML
1675 if (amdgpu_virt_release_full_gpu(adev, false))
1676 DRM_ERROR("failed to release exclusive mode on fini\n");
2493664f 1677
d38ceaf9
AD
1678 return 0;
1679}
1680
06ec9070 1681static void amdgpu_device_ip_late_init_func_handler(struct work_struct *work)
2dc80b00
S
1682{
1683 struct amdgpu_device *adev =
1684 container_of(work, struct amdgpu_device, late_init_work.work);
06ec9070 1685 amdgpu_device_ip_late_set_cg_state(adev);
2dc80b00
S
1686}
1687
cdd61df6 1688int amdgpu_device_ip_suspend(struct amdgpu_device *adev)
d38ceaf9
AD
1689{
1690 int i, r;
1691
e941ea99
XY
1692 if (amdgpu_sriov_vf(adev))
1693 amdgpu_virt_request_full_gpu(adev, false);
1694
c5a93a28
FC
1695 /* ungate SMC block first */
1696 r = amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC,
1697 AMD_CG_STATE_UNGATE);
1698 if (r) {
1699 DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n",r);
1700 }
1701
d38ceaf9 1702 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
a1255107 1703 if (!adev->ip_blocks[i].status.valid)
d38ceaf9
AD
1704 continue;
1705 /* ungate blocks so that suspend can properly shut them down */
c5a93a28 1706 if (i != AMD_IP_BLOCK_TYPE_SMC) {
a1255107
AD
1707 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1708 AMD_CG_STATE_UNGATE);
c5a93a28 1709 if (r) {
a1255107
AD
1710 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1711 adev->ip_blocks[i].version->funcs->name, r);
c5a93a28 1712 }
2c1a2784 1713 }
d38ceaf9 1714 /* XXX handle errors */
a1255107 1715 r = adev->ip_blocks[i].version->funcs->suspend(adev);
d38ceaf9 1716 /* XXX handle errors */
2c1a2784 1717 if (r) {
a1255107
AD
1718 DRM_ERROR("suspend of IP block <%s> failed %d\n",
1719 adev->ip_blocks[i].version->funcs->name, r);
2c1a2784 1720 }
d38ceaf9
AD
1721 }
1722
e941ea99
XY
1723 if (amdgpu_sriov_vf(adev))
1724 amdgpu_virt_release_full_gpu(adev, false);
1725
d38ceaf9
AD
1726 return 0;
1727}
1728
06ec9070 1729static int amdgpu_device_ip_reinit_early_sriov(struct amdgpu_device *adev)
a90ad3c2
ML
1730{
1731 int i, r;
1732
2cb681b6
ML
1733 static enum amd_ip_block_type ip_order[] = {
1734 AMD_IP_BLOCK_TYPE_GMC,
1735 AMD_IP_BLOCK_TYPE_COMMON,
2cb681b6
ML
1736 AMD_IP_BLOCK_TYPE_IH,
1737 };
a90ad3c2 1738
2cb681b6
ML
1739 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1740 int j;
1741 struct amdgpu_ip_block *block;
a90ad3c2 1742
2cb681b6
ML
1743 for (j = 0; j < adev->num_ip_blocks; j++) {
1744 block = &adev->ip_blocks[j];
1745
1746 if (block->version->type != ip_order[i] ||
1747 !block->status.valid)
1748 continue;
1749
1750 r = block->version->funcs->hw_init(adev);
1751 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
a90ad3c2
ML
1752 }
1753 }
1754
1755 return 0;
1756}
1757
06ec9070 1758static int amdgpu_device_ip_reinit_late_sriov(struct amdgpu_device *adev)
a90ad3c2
ML
1759{
1760 int i, r;
1761
2cb681b6
ML
1762 static enum amd_ip_block_type ip_order[] = {
1763 AMD_IP_BLOCK_TYPE_SMC,
ef4c166d 1764 AMD_IP_BLOCK_TYPE_PSP,
2cb681b6
ML
1765 AMD_IP_BLOCK_TYPE_DCE,
1766 AMD_IP_BLOCK_TYPE_GFX,
1767 AMD_IP_BLOCK_TYPE_SDMA,
257deb8c
FM
1768 AMD_IP_BLOCK_TYPE_UVD,
1769 AMD_IP_BLOCK_TYPE_VCE
2cb681b6 1770 };
a90ad3c2 1771
2cb681b6
ML
1772 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1773 int j;
1774 struct amdgpu_ip_block *block;
a90ad3c2 1775
2cb681b6
ML
1776 for (j = 0; j < adev->num_ip_blocks; j++) {
1777 block = &adev->ip_blocks[j];
1778
1779 if (block->version->type != ip_order[i] ||
1780 !block->status.valid)
1781 continue;
1782
1783 r = block->version->funcs->hw_init(adev);
1784 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
a90ad3c2
ML
1785 }
1786 }
1787
1788 return 0;
1789}
1790
06ec9070 1791static int amdgpu_device_ip_resume_phase1(struct amdgpu_device *adev)
d38ceaf9
AD
1792{
1793 int i, r;
1794
a90ad3c2
ML
1795 for (i = 0; i < adev->num_ip_blocks; i++) {
1796 if (!adev->ip_blocks[i].status.valid)
1797 continue;
a90ad3c2
ML
1798 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
1799 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
fcf0649f
CZ
1800 adev->ip_blocks[i].version->type ==
1801 AMD_IP_BLOCK_TYPE_IH) {
1802 r = adev->ip_blocks[i].version->funcs->resume(adev);
1803 if (r) {
1804 DRM_ERROR("resume of IP block <%s> failed %d\n",
1805 adev->ip_blocks[i].version->funcs->name, r);
1806 return r;
1807 }
a90ad3c2
ML
1808 }
1809 }
1810
1811 return 0;
1812}
1813
06ec9070 1814static int amdgpu_device_ip_resume_phase2(struct amdgpu_device *adev)
d38ceaf9
AD
1815{
1816 int i, r;
1817
1818 for (i = 0; i < adev->num_ip_blocks; i++) {
a1255107 1819 if (!adev->ip_blocks[i].status.valid)
d38ceaf9 1820 continue;
fcf0649f
CZ
1821 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
1822 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
1823 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH )
1824 continue;
a1255107 1825 r = adev->ip_blocks[i].version->funcs->resume(adev);
2c1a2784 1826 if (r) {
a1255107
AD
1827 DRM_ERROR("resume of IP block <%s> failed %d\n",
1828 adev->ip_blocks[i].version->funcs->name, r);
d38ceaf9 1829 return r;
2c1a2784 1830 }
d38ceaf9
AD
1831 }
1832
1833 return 0;
1834}
1835
06ec9070 1836static int amdgpu_device_ip_resume(struct amdgpu_device *adev)
fcf0649f
CZ
1837{
1838 int r;
1839
06ec9070 1840 r = amdgpu_device_ip_resume_phase1(adev);
fcf0649f
CZ
1841 if (r)
1842 return r;
06ec9070 1843 r = amdgpu_device_ip_resume_phase2(adev);
fcf0649f
CZ
1844
1845 return r;
1846}
1847
4e99a44e 1848static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
048765ad 1849{
6867e1b5
ML
1850 if (amdgpu_sriov_vf(adev)) {
1851 if (adev->is_atom_fw) {
1852 if (amdgpu_atomfirmware_gpu_supports_virtualization(adev))
1853 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
1854 } else {
1855 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
1856 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
1857 }
1858
1859 if (!(adev->virt.caps & AMDGPU_SRIOV_CAPS_SRIOV_VBIOS))
1860 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_NO_VBIOS, 0, 0);
a5bde2f9 1861 }
048765ad
AR
1862}
1863
4562236b
HW
1864bool amdgpu_device_asic_has_dc_support(enum amd_asic_type asic_type)
1865{
1866 switch (asic_type) {
1867#if defined(CONFIG_DRM_AMD_DC)
1868 case CHIP_BONAIRE:
1869 case CHIP_HAWAII:
0d6fbccb 1870 case CHIP_KAVERI:
4562236b
HW
1871 case CHIP_CARRIZO:
1872 case CHIP_STONEY:
1873 case CHIP_POLARIS11:
1874 case CHIP_POLARIS10:
2c8ad2d5 1875 case CHIP_POLARIS12:
4562236b
HW
1876 case CHIP_TONGA:
1877 case CHIP_FIJI:
1878#if defined(CONFIG_DRM_AMD_DC_PRE_VEGA)
1879 return amdgpu_dc != 0;
4562236b 1880#endif
17b7cf8c
AD
1881 case CHIP_KABINI:
1882 case CHIP_MULLINS:
1883 return amdgpu_dc > 0;
42f8ffa1
HW
1884 case CHIP_VEGA10:
1885#if defined(CONFIG_DRM_AMD_DC_DCN1_0)
fd187853 1886 case CHIP_RAVEN:
42f8ffa1 1887#endif
fd187853 1888 return amdgpu_dc != 0;
4562236b
HW
1889#endif
1890 default:
1891 return false;
1892 }
1893}
1894
1895/**
1896 * amdgpu_device_has_dc_support - check if dc is supported
1897 *
1898 * @adev: amdgpu_device_pointer
1899 *
1900 * Returns true for supported, false for not supported
1901 */
1902bool amdgpu_device_has_dc_support(struct amdgpu_device *adev)
1903{
2555039d
XY
1904 if (amdgpu_sriov_vf(adev))
1905 return false;
1906
4562236b
HW
1907 return amdgpu_device_asic_has_dc_support(adev->asic_type);
1908}
1909
d38ceaf9
AD
1910/**
1911 * amdgpu_device_init - initialize the driver
1912 *
1913 * @adev: amdgpu_device pointer
1914 * @pdev: drm dev pointer
1915 * @pdev: pci dev pointer
1916 * @flags: driver flags
1917 *
1918 * Initializes the driver info and hw (all asics).
1919 * Returns 0 for success or an error on failure.
1920 * Called at driver startup.
1921 */
1922int amdgpu_device_init(struct amdgpu_device *adev,
1923 struct drm_device *ddev,
1924 struct pci_dev *pdev,
1925 uint32_t flags)
1926{
1927 int r, i;
1928 bool runtime = false;
95844d20 1929 u32 max_MBps;
d38ceaf9
AD
1930
1931 adev->shutdown = false;
1932 adev->dev = &pdev->dev;
1933 adev->ddev = ddev;
1934 adev->pdev = pdev;
1935 adev->flags = flags;
2f7d10b3 1936 adev->asic_type = flags & AMD_ASIC_MASK;
d38ceaf9 1937 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
6f02a696 1938 adev->mc.gart_size = 512 * 1024 * 1024;
d38ceaf9
AD
1939 adev->accel_working = false;
1940 adev->num_rings = 0;
1941 adev->mman.buffer_funcs = NULL;
1942 adev->mman.buffer_funcs_ring = NULL;
1943 adev->vm_manager.vm_pte_funcs = NULL;
2d55e45a 1944 adev->vm_manager.vm_pte_num_rings = 0;
d38ceaf9 1945 adev->gart.gart_funcs = NULL;
f54d1867 1946 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
b8866c26 1947 bitmap_zero(adev->gfx.pipe_reserve_bitmap, AMDGPU_MAX_COMPUTE_QUEUES);
d38ceaf9
AD
1948
1949 adev->smc_rreg = &amdgpu_invalid_rreg;
1950 adev->smc_wreg = &amdgpu_invalid_wreg;
1951 adev->pcie_rreg = &amdgpu_invalid_rreg;
1952 adev->pcie_wreg = &amdgpu_invalid_wreg;
36b9a952
HR
1953 adev->pciep_rreg = &amdgpu_invalid_rreg;
1954 adev->pciep_wreg = &amdgpu_invalid_wreg;
d38ceaf9
AD
1955 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
1956 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
1957 adev->didt_rreg = &amdgpu_invalid_rreg;
1958 adev->didt_wreg = &amdgpu_invalid_wreg;
ccdbb20a
RZ
1959 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
1960 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
d38ceaf9
AD
1961 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
1962 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
1963
3e39ab90
AD
1964 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
1965 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
1966 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
d38ceaf9
AD
1967
1968 /* mutex initialization are all done here so we
1969 * can recall function without having locking issues */
d38ceaf9 1970 atomic_set(&adev->irq.ih.lock, 0);
0e5ca0d1 1971 mutex_init(&adev->firmware.mutex);
d38ceaf9
AD
1972 mutex_init(&adev->pm.mutex);
1973 mutex_init(&adev->gfx.gpu_clock_mutex);
1974 mutex_init(&adev->srbm_mutex);
b8866c26 1975 mutex_init(&adev->gfx.pipe_reserve_mutex);
d38ceaf9 1976 mutex_init(&adev->grbm_idx_mutex);
d38ceaf9 1977 mutex_init(&adev->mn_lock);
e23b74aa 1978 mutex_init(&adev->virt.vf_errors.lock);
d38ceaf9 1979 hash_init(adev->mn_hash);
13a752e3 1980 mutex_init(&adev->lock_reset);
d38ceaf9 1981
06ec9070 1982 amdgpu_device_check_arguments(adev);
d38ceaf9 1983
d38ceaf9
AD
1984 spin_lock_init(&adev->mmio_idx_lock);
1985 spin_lock_init(&adev->smc_idx_lock);
1986 spin_lock_init(&adev->pcie_idx_lock);
1987 spin_lock_init(&adev->uvd_ctx_idx_lock);
1988 spin_lock_init(&adev->didt_idx_lock);
ccdbb20a 1989 spin_lock_init(&adev->gc_cac_idx_lock);
16abb5d2 1990 spin_lock_init(&adev->se_cac_idx_lock);
d38ceaf9 1991 spin_lock_init(&adev->audio_endpt_idx_lock);
95844d20 1992 spin_lock_init(&adev->mm_stats.lock);
d38ceaf9 1993
0c4e7fa5
CZ
1994 INIT_LIST_HEAD(&adev->shadow_list);
1995 mutex_init(&adev->shadow_list_lock);
1996
795f2813
AR
1997 INIT_LIST_HEAD(&adev->ring_lru_list);
1998 spin_lock_init(&adev->ring_lru_list_lock);
1999
06ec9070
AD
2000 INIT_DELAYED_WORK(&adev->late_init_work,
2001 amdgpu_device_ip_late_init_func_handler);
2dc80b00 2002
0fa49558
AX
2003 /* Registers mapping */
2004 /* TODO: block userspace mapping of io register */
da69c161
KW
2005 if (adev->asic_type >= CHIP_BONAIRE) {
2006 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
2007 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
2008 } else {
2009 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
2010 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
2011 }
d38ceaf9 2012
d38ceaf9
AD
2013 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
2014 if (adev->rmmio == NULL) {
2015 return -ENOMEM;
2016 }
2017 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
2018 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
2019
705e519e 2020 /* doorbell bar mapping */
06ec9070 2021 amdgpu_device_doorbell_init(adev);
d38ceaf9
AD
2022
2023 /* io port mapping */
2024 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
2025 if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
2026 adev->rio_mem_size = pci_resource_len(adev->pdev, i);
2027 adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
2028 break;
2029 }
2030 }
2031 if (adev->rio_mem == NULL)
b64a18c5 2032 DRM_INFO("PCI I/O BAR is not found.\n");
d38ceaf9
AD
2033
2034 /* early init functions */
06ec9070 2035 r = amdgpu_device_ip_early_init(adev);
d38ceaf9
AD
2036 if (r)
2037 return r;
2038
2039 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
2040 /* this will fail for cards that aren't VGA class devices, just
2041 * ignore it */
06ec9070 2042 vga_client_register(adev->pdev, adev, NULL, amdgpu_device_vga_set_decode);
d38ceaf9
AD
2043
2044 if (amdgpu_runtime_pm == 1)
2045 runtime = true;
e9bef455 2046 if (amdgpu_device_is_px(ddev))
d38ceaf9 2047 runtime = true;
84c8b22e
LW
2048 if (!pci_is_thunderbolt_attached(adev->pdev))
2049 vga_switcheroo_register_client(adev->pdev,
2050 &amdgpu_switcheroo_ops, runtime);
d38ceaf9
AD
2051 if (runtime)
2052 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
2053
2054 /* Read BIOS */
83ba126a
AD
2055 if (!amdgpu_get_bios(adev)) {
2056 r = -EINVAL;
2057 goto failed;
2058 }
f7e9e9fe 2059
d38ceaf9 2060 r = amdgpu_atombios_init(adev);
2c1a2784
AD
2061 if (r) {
2062 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
e23b74aa 2063 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_INIT_FAIL, 0, 0);
83ba126a 2064 goto failed;
2c1a2784 2065 }
d38ceaf9 2066
4e99a44e
ML
2067 /* detect if we are with an SRIOV vbios */
2068 amdgpu_device_detect_sriov_bios(adev);
048765ad 2069
d38ceaf9 2070 /* Post card if necessary */
91fe77eb 2071 if (amdgpu_need_post(adev)) {
d38ceaf9 2072 if (!adev->bios) {
bec86378 2073 dev_err(adev->dev, "no vBIOS found\n");
83ba126a
AD
2074 r = -EINVAL;
2075 goto failed;
d38ceaf9 2076 }
bec86378 2077 DRM_INFO("GPU posting now...\n");
4e99a44e
ML
2078 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2079 if (r) {
2080 dev_err(adev->dev, "gpu post error!\n");
2081 goto failed;
2082 }
d38ceaf9
AD
2083 }
2084
88b64e95
AD
2085 if (adev->is_atom_fw) {
2086 /* Initialize clocks */
2087 r = amdgpu_atomfirmware_get_clock_info(adev);
2088 if (r) {
2089 dev_err(adev->dev, "amdgpu_atomfirmware_get_clock_info failed\n");
e23b74aa 2090 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
88b64e95
AD
2091 goto failed;
2092 }
2093 } else {
a5bde2f9
AD
2094 /* Initialize clocks */
2095 r = amdgpu_atombios_get_clock_info(adev);
2096 if (r) {
2097 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
e23b74aa 2098 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
89041940 2099 goto failed;
a5bde2f9
AD
2100 }
2101 /* init i2c buses */
4562236b
HW
2102 if (!amdgpu_device_has_dc_support(adev))
2103 amdgpu_atombios_i2c_init(adev);
2c1a2784 2104 }
d38ceaf9
AD
2105
2106 /* Fence driver */
2107 r = amdgpu_fence_driver_init(adev);
2c1a2784
AD
2108 if (r) {
2109 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
e23b74aa 2110 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0);
83ba126a 2111 goto failed;
2c1a2784 2112 }
d38ceaf9
AD
2113
2114 /* init the mode config */
2115 drm_mode_config_init(adev->ddev);
2116
06ec9070 2117 r = amdgpu_device_ip_init(adev);
d38ceaf9 2118 if (r) {
8840a387 2119 /* failed in exclusive mode due to timeout */
2120 if (amdgpu_sriov_vf(adev) &&
2121 !amdgpu_sriov_runtime(adev) &&
2122 amdgpu_virt_mmio_blocked(adev) &&
2123 !amdgpu_virt_wait_reset(adev)) {
2124 dev_err(adev->dev, "VF exclusive mode timeout\n");
1daee8b4
PD
2125 /* Don't send request since VF is inactive. */
2126 adev->virt.caps &= ~AMDGPU_SRIOV_CAPS_RUNTIME;
2127 adev->virt.ops = NULL;
8840a387 2128 r = -EAGAIN;
2129 goto failed;
2130 }
06ec9070 2131 dev_err(adev->dev, "amdgpu_device_ip_init failed\n");
e23b74aa 2132 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0);
06ec9070 2133 amdgpu_device_ip_fini(adev);
83ba126a 2134 goto failed;
d38ceaf9
AD
2135 }
2136
2137 adev->accel_working = true;
2138
e59c0205
AX
2139 amdgpu_vm_check_compute_bug(adev);
2140
95844d20
MO
2141 /* Initialize the buffer migration limit. */
2142 if (amdgpu_moverate >= 0)
2143 max_MBps = amdgpu_moverate;
2144 else
2145 max_MBps = 8; /* Allow 8 MB/s. */
2146 /* Get a log2 for easy divisions. */
2147 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
2148
d38ceaf9
AD
2149 r = amdgpu_ib_pool_init(adev);
2150 if (r) {
2151 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
e23b74aa 2152 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r);
83ba126a 2153 goto failed;
d38ceaf9
AD
2154 }
2155
2156 r = amdgpu_ib_ring_tests(adev);
2157 if (r)
2158 DRM_ERROR("ib ring test failed (%d).\n", r);
2159
2dc8f81e
HC
2160 if (amdgpu_sriov_vf(adev))
2161 amdgpu_virt_init_data_exchange(adev);
2162
9bc92b9c
ML
2163 amdgpu_fbdev_init(adev);
2164
d2f52ac8
RZ
2165 r = amdgpu_pm_sysfs_init(adev);
2166 if (r)
2167 DRM_ERROR("registering pm debugfs failed (%d).\n", r);
2168
75758255 2169 r = amdgpu_debugfs_gem_init(adev);
3f14e623 2170 if (r)
d38ceaf9 2171 DRM_ERROR("registering gem debugfs failed (%d).\n", r);
d38ceaf9
AD
2172
2173 r = amdgpu_debugfs_regs_init(adev);
3f14e623 2174 if (r)
d38ceaf9 2175 DRM_ERROR("registering register debugfs failed (%d).\n", r);
d38ceaf9 2176
50ab2533 2177 r = amdgpu_debugfs_firmware_init(adev);
3f14e623 2178 if (r)
50ab2533 2179 DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
50ab2533 2180
763efb6c 2181 r = amdgpu_debugfs_init(adev);
db95e218 2182 if (r)
763efb6c 2183 DRM_ERROR("Creating debugfs files failed (%d).\n", r);
db95e218 2184
d38ceaf9
AD
2185 if ((amdgpu_testing & 1)) {
2186 if (adev->accel_working)
2187 amdgpu_test_moves(adev);
2188 else
2189 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
2190 }
d38ceaf9
AD
2191 if (amdgpu_benchmarking) {
2192 if (adev->accel_working)
2193 amdgpu_benchmark(adev, amdgpu_benchmarking);
2194 else
2195 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
2196 }
2197
2198 /* enable clockgating, etc. after ib tests, etc. since some blocks require
2199 * explicit gating rather than handling it automatically.
2200 */
06ec9070 2201 r = amdgpu_device_ip_late_init(adev);
2c1a2784 2202 if (r) {
06ec9070 2203 dev_err(adev->dev, "amdgpu_device_ip_late_init failed\n");
e23b74aa 2204 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r);
83ba126a 2205 goto failed;
2c1a2784 2206 }
d38ceaf9
AD
2207
2208 return 0;
83ba126a
AD
2209
2210failed:
89041940 2211 amdgpu_vf_error_trans_all(adev);
83ba126a
AD
2212 if (runtime)
2213 vga_switcheroo_fini_domain_pm_ops(adev->dev);
8840a387 2214
83ba126a 2215 return r;
d38ceaf9
AD
2216}
2217
d38ceaf9
AD
2218/**
2219 * amdgpu_device_fini - tear down the driver
2220 *
2221 * @adev: amdgpu_device pointer
2222 *
2223 * Tear down the driver info (all asics).
2224 * Called at driver shutdown.
2225 */
2226void amdgpu_device_fini(struct amdgpu_device *adev)
2227{
2228 int r;
2229
2230 DRM_INFO("amdgpu: finishing device.\n");
2231 adev->shutdown = true;
db2c2a97
PD
2232 if (adev->mode_info.mode_config_initialized)
2233 drm_crtc_force_disable_all(adev->ddev);
b9141cd3 2234
d38ceaf9
AD
2235 amdgpu_ib_pool_fini(adev);
2236 amdgpu_fence_driver_fini(adev);
2237 amdgpu_fbdev_fini(adev);
06ec9070 2238 r = amdgpu_device_ip_fini(adev);
ab4fe3e1
HR
2239 if (adev->firmware.gpu_info_fw) {
2240 release_firmware(adev->firmware.gpu_info_fw);
2241 adev->firmware.gpu_info_fw = NULL;
2242 }
d38ceaf9 2243 adev->accel_working = false;
2dc80b00 2244 cancel_delayed_work_sync(&adev->late_init_work);
d38ceaf9 2245 /* free i2c buses */
4562236b
HW
2246 if (!amdgpu_device_has_dc_support(adev))
2247 amdgpu_i2c_fini(adev);
d38ceaf9
AD
2248 amdgpu_atombios_fini(adev);
2249 kfree(adev->bios);
2250 adev->bios = NULL;
84c8b22e
LW
2251 if (!pci_is_thunderbolt_attached(adev->pdev))
2252 vga_switcheroo_unregister_client(adev->pdev);
83ba126a
AD
2253 if (adev->flags & AMD_IS_PX)
2254 vga_switcheroo_fini_domain_pm_ops(adev->dev);
d38ceaf9
AD
2255 vga_client_register(adev->pdev, NULL, NULL, NULL);
2256 if (adev->rio_mem)
2257 pci_iounmap(adev->pdev, adev->rio_mem);
2258 adev->rio_mem = NULL;
2259 iounmap(adev->rmmio);
2260 adev->rmmio = NULL;
06ec9070 2261 amdgpu_device_doorbell_fini(adev);
d2f52ac8 2262 amdgpu_pm_sysfs_fini(adev);
d38ceaf9 2263 amdgpu_debugfs_regs_cleanup(adev);
d38ceaf9
AD
2264}
2265
2266
2267/*
2268 * Suspend & resume.
2269 */
2270/**
810ddc3a 2271 * amdgpu_device_suspend - initiate device suspend
d38ceaf9
AD
2272 *
2273 * @pdev: drm dev pointer
2274 * @state: suspend state
2275 *
2276 * Puts the hw in the suspend state (all asics).
2277 * Returns 0 for success or an error on failure.
2278 * Called at driver suspend.
2279 */
810ddc3a 2280int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
d38ceaf9
AD
2281{
2282 struct amdgpu_device *adev;
2283 struct drm_crtc *crtc;
2284 struct drm_connector *connector;
5ceb54c6 2285 int r;
d38ceaf9
AD
2286
2287 if (dev == NULL || dev->dev_private == NULL) {
2288 return -ENODEV;
2289 }
2290
2291 adev = dev->dev_private;
2292
2293 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2294 return 0;
2295
2296 drm_kms_helper_poll_disable(dev);
2297
4562236b
HW
2298 if (!amdgpu_device_has_dc_support(adev)) {
2299 /* turn off display hw */
2300 drm_modeset_lock_all(dev);
2301 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2302 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
2303 }
2304 drm_modeset_unlock_all(dev);
d38ceaf9
AD
2305 }
2306
ba997709
YZ
2307 amdgpu_amdkfd_suspend(adev);
2308
756e6880 2309 /* unpin the front buffers and cursors */
d38ceaf9 2310 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
756e6880 2311 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
d38ceaf9
AD
2312 struct amdgpu_framebuffer *rfb = to_amdgpu_framebuffer(crtc->primary->fb);
2313 struct amdgpu_bo *robj;
2314
756e6880
AD
2315 if (amdgpu_crtc->cursor_bo) {
2316 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
7a6901d7 2317 r = amdgpu_bo_reserve(aobj, true);
756e6880
AD
2318 if (r == 0) {
2319 amdgpu_bo_unpin(aobj);
2320 amdgpu_bo_unreserve(aobj);
2321 }
2322 }
2323
d38ceaf9
AD
2324 if (rfb == NULL || rfb->obj == NULL) {
2325 continue;
2326 }
2327 robj = gem_to_amdgpu_bo(rfb->obj);
2328 /* don't unpin kernel fb objects */
2329 if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
7a6901d7 2330 r = amdgpu_bo_reserve(robj, true);
d38ceaf9
AD
2331 if (r == 0) {
2332 amdgpu_bo_unpin(robj);
2333 amdgpu_bo_unreserve(robj);
2334 }
2335 }
2336 }
2337 /* evict vram memory */
2338 amdgpu_bo_evict_vram(adev);
2339
5ceb54c6 2340 amdgpu_fence_driver_suspend(adev);
d38ceaf9 2341
cdd61df6 2342 r = amdgpu_device_ip_suspend(adev);
d38ceaf9 2343
a0a71e49
AD
2344 /* evict remaining vram memory
2345 * This second call to evict vram is to evict the gart page table
2346 * using the CPU.
2347 */
d38ceaf9
AD
2348 amdgpu_bo_evict_vram(adev);
2349
2350 pci_save_state(dev->pdev);
2351 if (suspend) {
2352 /* Shut down the device */
2353 pci_disable_device(dev->pdev);
2354 pci_set_power_state(dev->pdev, PCI_D3hot);
74b0b157 2355 } else {
2356 r = amdgpu_asic_reset(adev);
2357 if (r)
2358 DRM_ERROR("amdgpu asic reset failed\n");
d38ceaf9
AD
2359 }
2360
2361 if (fbcon) {
2362 console_lock();
2363 amdgpu_fbdev_set_suspend(adev, 1);
2364 console_unlock();
2365 }
2366 return 0;
2367}
2368
2369/**
810ddc3a 2370 * amdgpu_device_resume - initiate device resume
d38ceaf9
AD
2371 *
2372 * @pdev: drm dev pointer
2373 *
2374 * Bring the hw back to operating state (all asics).
2375 * Returns 0 for success or an error on failure.
2376 * Called at driver resume.
2377 */
810ddc3a 2378int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
d38ceaf9
AD
2379{
2380 struct drm_connector *connector;
2381 struct amdgpu_device *adev = dev->dev_private;
756e6880 2382 struct drm_crtc *crtc;
03161a6e 2383 int r = 0;
d38ceaf9
AD
2384
2385 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2386 return 0;
2387
74b0b157 2388 if (fbcon)
d38ceaf9 2389 console_lock();
74b0b157 2390
d38ceaf9
AD
2391 if (resume) {
2392 pci_set_power_state(dev->pdev, PCI_D0);
2393 pci_restore_state(dev->pdev);
74b0b157 2394 r = pci_enable_device(dev->pdev);
03161a6e
HR
2395 if (r)
2396 goto unlock;
d38ceaf9
AD
2397 }
2398
2399 /* post card */
c836fec5 2400 if (amdgpu_need_post(adev)) {
74b0b157 2401 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2402 if (r)
2403 DRM_ERROR("amdgpu asic init failed\n");
2404 }
d38ceaf9 2405
06ec9070 2406 r = amdgpu_device_ip_resume(adev);
e6707218 2407 if (r) {
06ec9070 2408 DRM_ERROR("amdgpu_device_ip_resume failed (%d).\n", r);
03161a6e 2409 goto unlock;
e6707218 2410 }
5ceb54c6
AD
2411 amdgpu_fence_driver_resume(adev);
2412
ca198528
FC
2413 if (resume) {
2414 r = amdgpu_ib_ring_tests(adev);
2415 if (r)
2416 DRM_ERROR("ib ring test failed (%d).\n", r);
2417 }
d38ceaf9 2418
06ec9070 2419 r = amdgpu_device_ip_late_init(adev);
03161a6e
HR
2420 if (r)
2421 goto unlock;
d38ceaf9 2422
756e6880
AD
2423 /* pin cursors */
2424 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2425 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2426
2427 if (amdgpu_crtc->cursor_bo) {
2428 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
7a6901d7 2429 r = amdgpu_bo_reserve(aobj, true);
756e6880
AD
2430 if (r == 0) {
2431 r = amdgpu_bo_pin(aobj,
2432 AMDGPU_GEM_DOMAIN_VRAM,
2433 &amdgpu_crtc->cursor_addr);
2434 if (r != 0)
2435 DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
2436 amdgpu_bo_unreserve(aobj);
2437 }
2438 }
2439 }
ba997709
YZ
2440 r = amdgpu_amdkfd_resume(adev);
2441 if (r)
2442 return r;
756e6880 2443
d38ceaf9
AD
2444 /* blat the mode back in */
2445 if (fbcon) {
4562236b
HW
2446 if (!amdgpu_device_has_dc_support(adev)) {
2447 /* pre DCE11 */
2448 drm_helper_resume_force_mode(dev);
2449
2450 /* turn on display hw */
2451 drm_modeset_lock_all(dev);
2452 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2453 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
2454 }
2455 drm_modeset_unlock_all(dev);
2456 } else {
2457 /*
2458 * There is no equivalent atomic helper to turn on
2459 * display, so we defined our own function for this,
2460 * once suspend resume is supported by the atomic
2461 * framework this will be reworked
2462 */
2463 amdgpu_dm_display_resume(adev);
d38ceaf9
AD
2464 }
2465 }
2466
2467 drm_kms_helper_poll_enable(dev);
23a1a9e5
L
2468
2469 /*
2470 * Most of the connector probing functions try to acquire runtime pm
2471 * refs to ensure that the GPU is powered on when connector polling is
2472 * performed. Since we're calling this from a runtime PM callback,
2473 * trying to acquire rpm refs will cause us to deadlock.
2474 *
2475 * Since we're guaranteed to be holding the rpm lock, it's safe to
2476 * temporarily disable the rpm helpers so this doesn't deadlock us.
2477 */
2478#ifdef CONFIG_PM
2479 dev->dev->power.disable_depth++;
2480#endif
4562236b
HW
2481 if (!amdgpu_device_has_dc_support(adev))
2482 drm_helper_hpd_irq_event(dev);
2483 else
2484 drm_kms_helper_hotplug_event(dev);
23a1a9e5
L
2485#ifdef CONFIG_PM
2486 dev->dev->power.disable_depth--;
2487#endif
d38ceaf9 2488
03161a6e 2489 if (fbcon)
d38ceaf9 2490 amdgpu_fbdev_set_suspend(adev, 0);
03161a6e
HR
2491
2492unlock:
2493 if (fbcon)
d38ceaf9 2494 console_unlock();
d38ceaf9 2495
03161a6e 2496 return r;
d38ceaf9
AD
2497}
2498
06ec9070 2499static bool amdgpu_device_ip_check_soft_reset(struct amdgpu_device *adev)
63fbf42f
CZ
2500{
2501 int i;
2502 bool asic_hang = false;
2503
f993d628
ML
2504 if (amdgpu_sriov_vf(adev))
2505 return true;
2506
63fbf42f 2507 for (i = 0; i < adev->num_ip_blocks; i++) {
a1255107 2508 if (!adev->ip_blocks[i].status.valid)
63fbf42f 2509 continue;
a1255107
AD
2510 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
2511 adev->ip_blocks[i].status.hang =
2512 adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
2513 if (adev->ip_blocks[i].status.hang) {
2514 DRM_INFO("IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
63fbf42f
CZ
2515 asic_hang = true;
2516 }
2517 }
2518 return asic_hang;
2519}
2520
06ec9070 2521static int amdgpu_device_ip_pre_soft_reset(struct amdgpu_device *adev)
d31a501e
CZ
2522{
2523 int i, r = 0;
2524
2525 for (i = 0; i < adev->num_ip_blocks; i++) {
a1255107 2526 if (!adev->ip_blocks[i].status.valid)
d31a501e 2527 continue;
a1255107
AD
2528 if (adev->ip_blocks[i].status.hang &&
2529 adev->ip_blocks[i].version->funcs->pre_soft_reset) {
2530 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
d31a501e
CZ
2531 if (r)
2532 return r;
2533 }
2534 }
2535
2536 return 0;
2537}
2538
06ec9070 2539static bool amdgpu_device_ip_need_full_reset(struct amdgpu_device *adev)
35d782fe 2540{
da146d3b
AD
2541 int i;
2542
2543 for (i = 0; i < adev->num_ip_blocks; i++) {
a1255107 2544 if (!adev->ip_blocks[i].status.valid)
da146d3b 2545 continue;
a1255107
AD
2546 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
2547 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
2548 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
98512bb8
KW
2549 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) ||
2550 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
a1255107 2551 if (adev->ip_blocks[i].status.hang) {
da146d3b
AD
2552 DRM_INFO("Some block need full reset!\n");
2553 return true;
2554 }
2555 }
35d782fe
CZ
2556 }
2557 return false;
2558}
2559
06ec9070 2560static int amdgpu_device_ip_soft_reset(struct amdgpu_device *adev)
35d782fe
CZ
2561{
2562 int i, r = 0;
2563
2564 for (i = 0; i < adev->num_ip_blocks; i++) {
a1255107 2565 if (!adev->ip_blocks[i].status.valid)
35d782fe 2566 continue;
a1255107
AD
2567 if (adev->ip_blocks[i].status.hang &&
2568 adev->ip_blocks[i].version->funcs->soft_reset) {
2569 r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
35d782fe
CZ
2570 if (r)
2571 return r;
2572 }
2573 }
2574
2575 return 0;
2576}
2577
06ec9070 2578static int amdgpu_device_ip_post_soft_reset(struct amdgpu_device *adev)
35d782fe
CZ
2579{
2580 int i, r = 0;
2581
2582 for (i = 0; i < adev->num_ip_blocks; i++) {
a1255107 2583 if (!adev->ip_blocks[i].status.valid)
35d782fe 2584 continue;
a1255107
AD
2585 if (adev->ip_blocks[i].status.hang &&
2586 adev->ip_blocks[i].version->funcs->post_soft_reset)
2587 r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
35d782fe
CZ
2588 if (r)
2589 return r;
2590 }
2591
2592 return 0;
2593}
2594
3ad81f16
CZ
2595bool amdgpu_need_backup(struct amdgpu_device *adev)
2596{
2597 if (adev->flags & AMD_IS_APU)
2598 return false;
2599
8854695a 2600 return amdgpu_gpu_recovery;
3ad81f16
CZ
2601}
2602
06ec9070
AD
2603static int amdgpu_device_recover_vram_from_shadow(struct amdgpu_device *adev,
2604 struct amdgpu_ring *ring,
2605 struct amdgpu_bo *bo,
2606 struct dma_fence **fence)
53cdccd5
CZ
2607{
2608 uint32_t domain;
2609 int r;
2610
23d2e504
RH
2611 if (!bo->shadow)
2612 return 0;
2613
1d284797 2614 r = amdgpu_bo_reserve(bo, true);
23d2e504
RH
2615 if (r)
2616 return r;
2617 domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
2618 /* if bo has been evicted, then no need to recover */
2619 if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
82521316
RH
2620 r = amdgpu_bo_validate(bo->shadow);
2621 if (r) {
2622 DRM_ERROR("bo validate failed!\n");
2623 goto err;
2624 }
2625
23d2e504 2626 r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
53cdccd5 2627 NULL, fence, true);
23d2e504
RH
2628 if (r) {
2629 DRM_ERROR("recover page table failed!\n");
2630 goto err;
2631 }
2632 }
53cdccd5 2633err:
23d2e504
RH
2634 amdgpu_bo_unreserve(bo);
2635 return r;
53cdccd5
CZ
2636}
2637
5740682e 2638/*
06ec9070 2639 * amdgpu_device_reset - reset ASIC/GPU for bare-metal or passthrough
a90ad3c2
ML
2640 *
2641 * @adev: amdgpu device pointer
5740682e 2642 * @reset_flags: output param tells caller the reset result
a90ad3c2 2643 *
5740682e
ML
2644 * attempt to do soft-reset or full-reset and reinitialize Asic
2645 * return 0 means successed otherwise failed
2646*/
06ec9070
AD
2647static int amdgpu_device_reset(struct amdgpu_device *adev,
2648 uint64_t* reset_flags)
a90ad3c2 2649{
5740682e
ML
2650 bool need_full_reset, vram_lost = 0;
2651 int r;
a90ad3c2 2652
06ec9070 2653 need_full_reset = amdgpu_device_ip_need_full_reset(adev);
a90ad3c2 2654
5740682e 2655 if (!need_full_reset) {
06ec9070
AD
2656 amdgpu_device_ip_pre_soft_reset(adev);
2657 r = amdgpu_device_ip_soft_reset(adev);
2658 amdgpu_device_ip_post_soft_reset(adev);
2659 if (r || amdgpu_device_ip_check_soft_reset(adev)) {
5740682e
ML
2660 DRM_INFO("soft reset failed, will fallback to full reset!\n");
2661 need_full_reset = true;
2662 }
a90ad3c2 2663
5740682e 2664 }
a90ad3c2 2665
5740682e 2666 if (need_full_reset) {
cdd61df6 2667 r = amdgpu_device_ip_suspend(adev);
a90ad3c2 2668
5740682e 2669retry:
5740682e 2670 r = amdgpu_asic_reset(adev);
5740682e
ML
2671 /* post card */
2672 amdgpu_atom_asic_init(adev->mode_info.atom_context);
65781c78 2673
5740682e
ML
2674 if (!r) {
2675 dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
06ec9070 2676 r = amdgpu_device_ip_resume_phase1(adev);
5740682e
ML
2677 if (r)
2678 goto out;
65781c78 2679
06ec9070 2680 vram_lost = amdgpu_device_check_vram_lost(adev);
5740682e
ML
2681 if (vram_lost) {
2682 DRM_ERROR("VRAM is lost!\n");
2683 atomic_inc(&adev->vram_lost_counter);
2684 }
2685
c1c7ce8f
CK
2686 r = amdgpu_gtt_mgr_recover(
2687 &adev->mman.bdev.man[TTM_PL_TT]);
5740682e
ML
2688 if (r)
2689 goto out;
2690
06ec9070 2691 r = amdgpu_device_ip_resume_phase2(adev);
5740682e
ML
2692 if (r)
2693 goto out;
2694
2695 if (vram_lost)
06ec9070 2696 amdgpu_device_fill_reset_magic(adev);
65781c78 2697 }
5740682e 2698 }
65781c78 2699
5740682e
ML
2700out:
2701 if (!r) {
2702 amdgpu_irq_gpu_reset_resume_helper(adev);
2703 r = amdgpu_ib_ring_tests(adev);
2704 if (r) {
2705 dev_err(adev->dev, "ib ring test failed (%d).\n", r);
cdd61df6 2706 r = amdgpu_device_ip_suspend(adev);
5740682e
ML
2707 need_full_reset = true;
2708 goto retry;
2709 }
2710 }
65781c78 2711
5740682e
ML
2712 if (reset_flags) {
2713 if (vram_lost)
2714 (*reset_flags) |= AMDGPU_RESET_INFO_VRAM_LOST;
a90ad3c2 2715
5740682e
ML
2716 if (need_full_reset)
2717 (*reset_flags) |= AMDGPU_RESET_INFO_FULLRESET;
65781c78 2718 }
a90ad3c2 2719
5740682e
ML
2720 return r;
2721}
a90ad3c2 2722
5740682e 2723/*
06ec9070 2724 * amdgpu_device_reset_sriov - reset ASIC for SR-IOV vf
5740682e
ML
2725 *
2726 * @adev: amdgpu device pointer
2727 * @reset_flags: output param tells caller the reset result
2728 *
2729 * do VF FLR and reinitialize Asic
2730 * return 0 means successed otherwise failed
2731*/
06ec9070
AD
2732static int amdgpu_device_reset_sriov(struct amdgpu_device *adev,
2733 uint64_t *reset_flags,
2734 bool from_hypervisor)
5740682e
ML
2735{
2736 int r;
2737
2738 if (from_hypervisor)
2739 r = amdgpu_virt_request_full_gpu(adev, true);
2740 else
2741 r = amdgpu_virt_reset_gpu(adev);
2742 if (r)
2743 return r;
a90ad3c2
ML
2744
2745 /* Resume IP prior to SMC */
06ec9070 2746 r = amdgpu_device_ip_reinit_early_sriov(adev);
5740682e
ML
2747 if (r)
2748 goto error;
a90ad3c2
ML
2749
2750 /* we need recover gart prior to run SMC/CP/SDMA resume */
c1c7ce8f 2751 amdgpu_gtt_mgr_recover(&adev->mman.bdev.man[TTM_PL_TT]);
a90ad3c2
ML
2752
2753 /* now we are okay to resume SMC/CP/SDMA */
06ec9070 2754 r = amdgpu_device_ip_reinit_late_sriov(adev);
5740682e
ML
2755 if (r)
2756 goto error;
a90ad3c2
ML
2757
2758 amdgpu_irq_gpu_reset_resume_helper(adev);
5740682e
ML
2759 r = amdgpu_ib_ring_tests(adev);
2760 if (r)
a90ad3c2
ML
2761 dev_err(adev->dev, "[GPU_RESET] ib ring test failed (%d).\n", r);
2762
5740682e 2763error:
a90ad3c2
ML
2764 /* release full control of GPU after ib test */
2765 amdgpu_virt_release_full_gpu(adev, true);
2766
5740682e 2767 if (reset_flags) {
75bc6099
ML
2768 if (adev->virt.gim_feature & AMDGIM_FEATURE_GIM_FLR_VRAMLOST) {
2769 (*reset_flags) |= AMDGPU_RESET_INFO_VRAM_LOST;
2770 atomic_inc(&adev->vram_lost_counter);
2771 }
a90ad3c2 2772
5740682e
ML
2773 /* VF FLR or hotlink reset is always full-reset */
2774 (*reset_flags) |= AMDGPU_RESET_INFO_FULLRESET;
a90ad3c2
ML
2775 }
2776
2777 return r;
2778}
2779
d38ceaf9 2780/**
5740682e 2781 * amdgpu_gpu_recover - reset the asic and recover scheduler
d38ceaf9
AD
2782 *
2783 * @adev: amdgpu device pointer
5740682e 2784 * @job: which job trigger hang
dcebf026 2785 * @force forces reset regardless of amdgpu_gpu_recovery
d38ceaf9 2786 *
5740682e 2787 * Attempt to reset the GPU if it has hung (all asics).
d38ceaf9
AD
2788 * Returns 0 for success or an error on failure.
2789 */
dcebf026 2790int amdgpu_gpu_recover(struct amdgpu_device *adev, struct amdgpu_job *job, bool force)
d38ceaf9 2791{
4562236b 2792 struct drm_atomic_state *state = NULL;
5740682e
ML
2793 uint64_t reset_flags = 0;
2794 int i, r, resched;
fb140b29 2795
06ec9070 2796 if (!amdgpu_device_ip_check_soft_reset(adev)) {
63fbf42f
CZ
2797 DRM_INFO("No hardware hang detected. Did some blocks stall?\n");
2798 return 0;
2799 }
d38ceaf9 2800
dcebf026
AG
2801 if (!force && (amdgpu_gpu_recovery == 0 ||
2802 (amdgpu_gpu_recovery == -1 && !amdgpu_sriov_vf(adev)))) {
2803 DRM_INFO("GPU recovery disabled.\n");
2804 return 0;
2805 }
2806
5740682e
ML
2807 dev_info(adev->dev, "GPU reset begin!\n");
2808
13a752e3 2809 mutex_lock(&adev->lock_reset);
d94aed5a 2810 atomic_inc(&adev->gpu_reset_counter);
13a752e3 2811 adev->in_gpu_reset = 1;
d38ceaf9 2812
a3c47d6b
CZ
2813 /* block TTM */
2814 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
4562236b
HW
2815 /* store modesetting */
2816 if (amdgpu_device_has_dc_support(adev))
2817 state = drm_atomic_helper_suspend(adev->ddev);
a3c47d6b 2818
0875dc9e
CZ
2819 /* block scheduler */
2820 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2821 struct amdgpu_ring *ring = adev->rings[i];
2822
51687759 2823 if (!ring || !ring->sched.thread)
0875dc9e 2824 continue;
5740682e
ML
2825
2826 /* only focus on the ring hit timeout if &job not NULL */
2827 if (job && job->ring->idx != i)
2828 continue;
2829
0875dc9e 2830 kthread_park(ring->sched.thread);
1b1f42d8 2831 drm_sched_hw_job_reset(&ring->sched, &job->base);
5740682e 2832
2f9d4084
ML
2833 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
2834 amdgpu_fence_driver_force_completion(ring);
0875dc9e 2835 }
d38ceaf9 2836
5740682e 2837 if (amdgpu_sriov_vf(adev))
06ec9070 2838 r = amdgpu_device_reset_sriov(adev, &reset_flags, job ? false : true);
5740682e 2839 else
06ec9070 2840 r = amdgpu_device_reset(adev, &reset_flags);
35d782fe 2841
d38ceaf9 2842 if (!r) {
5740682e
ML
2843 if (((reset_flags & AMDGPU_RESET_INFO_FULLRESET) && !(adev->flags & AMD_IS_APU)) ||
2844 (reset_flags & AMDGPU_RESET_INFO_VRAM_LOST)) {
53cdccd5
CZ
2845 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
2846 struct amdgpu_bo *bo, *tmp;
f54d1867 2847 struct dma_fence *fence = NULL, *next = NULL;
53cdccd5
CZ
2848
2849 DRM_INFO("recover vram bo from shadow\n");
2850 mutex_lock(&adev->shadow_list_lock);
2851 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
236763d3 2852 next = NULL;
06ec9070 2853 amdgpu_device_recover_vram_from_shadow(adev, ring, bo, &next);
53cdccd5 2854 if (fence) {
f54d1867 2855 r = dma_fence_wait(fence, false);
53cdccd5 2856 if (r) {
1d7b17b0 2857 WARN(r, "recovery from shadow isn't completed\n");
53cdccd5
CZ
2858 break;
2859 }
2860 }
1f465087 2861
f54d1867 2862 dma_fence_put(fence);
53cdccd5
CZ
2863 fence = next;
2864 }
2865 mutex_unlock(&adev->shadow_list_lock);
2866 if (fence) {
f54d1867 2867 r = dma_fence_wait(fence, false);
53cdccd5 2868 if (r)
1d7b17b0 2869 WARN(r, "recovery from shadow isn't completed\n");
53cdccd5 2870 }
f54d1867 2871 dma_fence_put(fence);
53cdccd5 2872 }
5740682e 2873
d38ceaf9
AD
2874 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2875 struct amdgpu_ring *ring = adev->rings[i];
51687759
CZ
2876
2877 if (!ring || !ring->sched.thread)
d38ceaf9 2878 continue;
53cdccd5 2879
5740682e
ML
2880 /* only focus on the ring hit timeout if &job not NULL */
2881 if (job && job->ring->idx != i)
2882 continue;
2883
1b1f42d8 2884 drm_sched_job_recovery(&ring->sched);
0875dc9e 2885 kthread_unpark(ring->sched.thread);
d38ceaf9 2886 }
d38ceaf9 2887 } else {
d38ceaf9 2888 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
5740682e
ML
2889 struct amdgpu_ring *ring = adev->rings[i];
2890
2891 if (!ring || !ring->sched.thread)
2892 continue;
2893
2894 /* only focus on the ring hit timeout if &job not NULL */
2895 if (job && job->ring->idx != i)
2896 continue;
2897
2898 kthread_unpark(adev->rings[i]->sched.thread);
d38ceaf9
AD
2899 }
2900 }
2901
4562236b 2902 if (amdgpu_device_has_dc_support(adev)) {
5740682e
ML
2903 if (drm_atomic_helper_resume(adev->ddev, state))
2904 dev_info(adev->dev, "drm resume failed:%d\n", r);
4562236b 2905 amdgpu_dm_display_resume(adev);
5740682e 2906 } else {
4562236b 2907 drm_helper_resume_force_mode(adev->ddev);
5740682e 2908 }
d38ceaf9
AD
2909
2910 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
5740682e 2911
89041940 2912 if (r) {
d38ceaf9 2913 /* bad news, how to tell it to userspace ? */
5740682e
ML
2914 dev_info(adev->dev, "GPU reset(%d) failed\n", atomic_read(&adev->gpu_reset_counter));
2915 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, r);
2916 } else {
2917 dev_info(adev->dev, "GPU reset(%d) successed!\n",atomic_read(&adev->gpu_reset_counter));
89041940 2918 }
d38ceaf9 2919
89041940 2920 amdgpu_vf_error_trans_all(adev);
13a752e3
ML
2921 adev->in_gpu_reset = 0;
2922 mutex_unlock(&adev->lock_reset);
d38ceaf9
AD
2923 return r;
2924}
2925
d0dd7f0c
AD
2926void amdgpu_get_pcie_info(struct amdgpu_device *adev)
2927{
2928 u32 mask;
2929 int ret;
2930
cd474ba0
AD
2931 if (amdgpu_pcie_gen_cap)
2932 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
d0dd7f0c 2933
cd474ba0
AD
2934 if (amdgpu_pcie_lane_cap)
2935 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
d0dd7f0c 2936
cd474ba0
AD
2937 /* covers APUs as well */
2938 if (pci_is_root_bus(adev->pdev->bus)) {
2939 if (adev->pm.pcie_gen_mask == 0)
2940 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
2941 if (adev->pm.pcie_mlw_mask == 0)
2942 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
d0dd7f0c 2943 return;
cd474ba0 2944 }
d0dd7f0c 2945
cd474ba0
AD
2946 if (adev->pm.pcie_gen_mask == 0) {
2947 ret = drm_pcie_get_speed_cap_mask(adev->ddev, &mask);
2948 if (!ret) {
2949 adev->pm.pcie_gen_mask = (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
2950 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
2951 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
2952
2953 if (mask & DRM_PCIE_SPEED_25)
2954 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
2955 if (mask & DRM_PCIE_SPEED_50)
2956 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2;
2957 if (mask & DRM_PCIE_SPEED_80)
2958 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3;
2959 } else {
2960 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
2961 }
2962 }
2963 if (adev->pm.pcie_mlw_mask == 0) {
2964 ret = drm_pcie_get_max_link_width(adev->ddev, &mask);
2965 if (!ret) {
2966 switch (mask) {
2967 case 32:
2968 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
2969 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
2970 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2971 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2972 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2973 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2974 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2975 break;
2976 case 16:
2977 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
2978 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2979 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2980 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2981 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2982 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2983 break;
2984 case 12:
2985 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2986 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2987 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2988 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2989 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2990 break;
2991 case 8:
2992 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2993 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2994 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2995 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2996 break;
2997 case 4:
2998 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2999 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3000 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3001 break;
3002 case 2:
3003 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3004 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3005 break;
3006 case 1:
3007 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
3008 break;
3009 default:
3010 break;
3011 }
3012 } else {
3013 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
d0dd7f0c
AD
3014 }
3015 }
3016}
d38ceaf9 3017