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