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