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