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 #include "amdgpu_fru_eeprom.h"
68 
69 #include <linux/suspend.h>
70 #include <drm/task_barrier.h>
71 #include <linux/pm_runtime.h>
72 
73 MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin");
74 MODULE_FIRMWARE("amdgpu/vega12_gpu_info.bin");
75 MODULE_FIRMWARE("amdgpu/raven_gpu_info.bin");
76 MODULE_FIRMWARE("amdgpu/picasso_gpu_info.bin");
77 MODULE_FIRMWARE("amdgpu/raven2_gpu_info.bin");
78 MODULE_FIRMWARE("amdgpu/arcturus_gpu_info.bin");
79 MODULE_FIRMWARE("amdgpu/renoir_gpu_info.bin");
80 MODULE_FIRMWARE("amdgpu/navi10_gpu_info.bin");
81 MODULE_FIRMWARE("amdgpu/navi14_gpu_info.bin");
82 MODULE_FIRMWARE("amdgpu/navi12_gpu_info.bin");
83 MODULE_FIRMWARE("amdgpu/vangogh_gpu_info.bin");
84 MODULE_FIRMWARE("amdgpu/green_sardine_gpu_info.bin");
85 
86 #define AMDGPU_RESUME_MS		2000
87 
88 const char *amdgpu_asic_name[] = {
89 	"TAHITI",
90 	"PITCAIRN",
91 	"VERDE",
92 	"OLAND",
93 	"HAINAN",
94 	"BONAIRE",
95 	"KAVERI",
96 	"KABINI",
97 	"HAWAII",
98 	"MULLINS",
99 	"TOPAZ",
100 	"TONGA",
101 	"FIJI",
102 	"CARRIZO",
103 	"STONEY",
104 	"POLARIS10",
105 	"POLARIS11",
106 	"POLARIS12",
107 	"VEGAM",
108 	"VEGA10",
109 	"VEGA12",
110 	"VEGA20",
111 	"RAVEN",
112 	"ARCTURUS",
113 	"RENOIR",
114 	"NAVI10",
115 	"NAVI14",
116 	"NAVI12",
117 	"SIENNA_CICHLID",
118 	"NAVY_FLOUNDER",
119 	"VANGOGH",
120 	"DIMGREY_CAVEFISH",
121 	"LAST",
122 };
123 
124 /**
125  * DOC: pcie_replay_count
126  *
127  * The amdgpu driver provides a sysfs API for reporting the total number
128  * of PCIe replays (NAKs)
129  * The file pcie_replay_count is used for this and returns the total
130  * number of replays as a sum of the NAKs generated and NAKs received
131  */
132 
133 static ssize_t amdgpu_device_get_pcie_replay_count(struct device *dev,
134 		struct device_attribute *attr, char *buf)
135 {
136 	struct drm_device *ddev = dev_get_drvdata(dev);
137 	struct amdgpu_device *adev = drm_to_adev(ddev);
138 	uint64_t cnt = amdgpu_asic_get_pcie_replay_count(adev);
139 
140 	return snprintf(buf, PAGE_SIZE, "%llu\n", cnt);
141 }
142 
143 static DEVICE_ATTR(pcie_replay_count, S_IRUGO,
144 		amdgpu_device_get_pcie_replay_count, NULL);
145 
146 static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev);
147 
148 /**
149  * DOC: product_name
150  *
151  * The amdgpu driver provides a sysfs API for reporting the product name
152  * for the device
153  * The file serial_number is used for this and returns the product name
154  * as returned from the FRU.
155  * NOTE: This is only available for certain server cards
156  */
157 
158 static ssize_t amdgpu_device_get_product_name(struct device *dev,
159 		struct device_attribute *attr, char *buf)
160 {
161 	struct drm_device *ddev = dev_get_drvdata(dev);
162 	struct amdgpu_device *adev = drm_to_adev(ddev);
163 
164 	return snprintf(buf, PAGE_SIZE, "%s\n", adev->product_name);
165 }
166 
167 static DEVICE_ATTR(product_name, S_IRUGO,
168 		amdgpu_device_get_product_name, NULL);
169 
170 /**
171  * DOC: product_number
172  *
173  * The amdgpu driver provides a sysfs API for reporting the part number
174  * for the device
175  * The file serial_number is used for this and returns the part number
176  * as returned from the FRU.
177  * NOTE: This is only available for certain server cards
178  */
179 
180 static ssize_t amdgpu_device_get_product_number(struct device *dev,
181 		struct device_attribute *attr, char *buf)
182 {
183 	struct drm_device *ddev = dev_get_drvdata(dev);
184 	struct amdgpu_device *adev = drm_to_adev(ddev);
185 
186 	return snprintf(buf, PAGE_SIZE, "%s\n", adev->product_number);
187 }
188 
189 static DEVICE_ATTR(product_number, S_IRUGO,
190 		amdgpu_device_get_product_number, NULL);
191 
192 /**
193  * DOC: serial_number
194  *
195  * The amdgpu driver provides a sysfs API for reporting the serial number
196  * for the device
197  * The file serial_number is used for this and returns the serial number
198  * as returned from the FRU.
199  * NOTE: This is only available for certain server cards
200  */
201 
202 static ssize_t amdgpu_device_get_serial_number(struct device *dev,
203 		struct device_attribute *attr, char *buf)
204 {
205 	struct drm_device *ddev = dev_get_drvdata(dev);
206 	struct amdgpu_device *adev = drm_to_adev(ddev);
207 
208 	return snprintf(buf, PAGE_SIZE, "%s\n", adev->serial);
209 }
210 
211 static DEVICE_ATTR(serial_number, S_IRUGO,
212 		amdgpu_device_get_serial_number, NULL);
213 
214 /**
215  * amdgpu_device_supports_atpx - Is the device a dGPU with HG/PX power control
216  *
217  * @dev: drm_device pointer
218  *
219  * Returns true if the device is a dGPU with HG/PX power control,
220  * otherwise return false.
221  */
222 bool amdgpu_device_supports_atpx(struct drm_device *dev)
223 {
224 	struct amdgpu_device *adev = drm_to_adev(dev);
225 
226 	if (adev->flags & AMD_IS_PX)
227 		return true;
228 	return false;
229 }
230 
231 /**
232  * amdgpu_device_supports_boco - Is the device a dGPU with ACPI power resources
233  *
234  * @dev: drm_device pointer
235  *
236  * Returns true if the device is a dGPU with HG/PX power control,
237  * otherwise return false.
238  */
239 bool amdgpu_device_supports_boco(struct drm_device *dev)
240 {
241 	struct amdgpu_device *adev = drm_to_adev(dev);
242 
243 	if (adev->has_pr3)
244 		return true;
245 	return false;
246 }
247 
248 /**
249  * amdgpu_device_supports_baco - Does the device support BACO
250  *
251  * @dev: drm_device pointer
252  *
253  * Returns true if the device supporte BACO,
254  * otherwise return false.
255  */
256 bool amdgpu_device_supports_baco(struct drm_device *dev)
257 {
258 	struct amdgpu_device *adev = drm_to_adev(dev);
259 
260 	return amdgpu_asic_supports_baco(adev);
261 }
262 
263 /*
264  * VRAM access helper functions
265  */
266 
267 /**
268  * amdgpu_device_vram_access - read/write a buffer in vram
269  *
270  * @adev: amdgpu_device pointer
271  * @pos: offset of the buffer in vram
272  * @buf: virtual address of the buffer in system memory
273  * @size: read/write size, sizeof(@buf) must > @size
274  * @write: true - write to vram, otherwise - read from vram
275  */
276 void amdgpu_device_vram_access(struct amdgpu_device *adev, loff_t pos,
277 			       uint32_t *buf, size_t size, bool write)
278 {
279 	unsigned long flags;
280 	uint32_t hi = ~0;
281 	uint64_t last;
282 
283 
284 #ifdef CONFIG_64BIT
285 	last = min(pos + size, adev->gmc.visible_vram_size);
286 	if (last > pos) {
287 		void __iomem *addr = adev->mman.aper_base_kaddr + pos;
288 		size_t count = last - pos;
289 
290 		if (write) {
291 			memcpy_toio(addr, buf, count);
292 			mb();
293 			amdgpu_asic_flush_hdp(adev, NULL);
294 		} else {
295 			amdgpu_asic_invalidate_hdp(adev, NULL);
296 			mb();
297 			memcpy_fromio(buf, addr, count);
298 		}
299 
300 		if (count == size)
301 			return;
302 
303 		pos += count;
304 		buf += count / 4;
305 		size -= count;
306 	}
307 #endif
308 
309 	spin_lock_irqsave(&adev->mmio_idx_lock, flags);
310 	for (last = pos + size; pos < last; pos += 4) {
311 		uint32_t tmp = pos >> 31;
312 
313 		WREG32_NO_KIQ(mmMM_INDEX, ((uint32_t)pos) | 0x80000000);
314 		if (tmp != hi) {
315 			WREG32_NO_KIQ(mmMM_INDEX_HI, tmp);
316 			hi = tmp;
317 		}
318 		if (write)
319 			WREG32_NO_KIQ(mmMM_DATA, *buf++);
320 		else
321 			*buf++ = RREG32_NO_KIQ(mmMM_DATA);
322 	}
323 	spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
324 }
325 
326 /*
327  * register access helper functions.
328  */
329 /**
330  * amdgpu_device_rreg - read a memory mapped IO or indirect register
331  *
332  * @adev: amdgpu_device pointer
333  * @reg: dword aligned register offset
334  * @acc_flags: access flags which require special behavior
335  *
336  * Returns the 32 bit value from the offset specified.
337  */
338 uint32_t amdgpu_device_rreg(struct amdgpu_device *adev,
339 			    uint32_t reg, uint32_t acc_flags)
340 {
341 	uint32_t ret;
342 
343 	if (adev->in_pci_err_recovery)
344 		return 0;
345 
346 	if ((reg * 4) < adev->rmmio_size) {
347 		if (!(acc_flags & AMDGPU_REGS_NO_KIQ) &&
348 		    amdgpu_sriov_runtime(adev) &&
349 		    down_read_trylock(&adev->reset_sem)) {
350 			ret = amdgpu_kiq_rreg(adev, reg);
351 			up_read(&adev->reset_sem);
352 		} else {
353 			ret = readl(((void __iomem *)adev->rmmio) + (reg * 4));
354 		}
355 	} else {
356 		ret = adev->pcie_rreg(adev, reg * 4);
357 	}
358 
359 	trace_amdgpu_device_rreg(adev->pdev->device, reg, ret);
360 
361 	return ret;
362 }
363 
364 /*
365  * MMIO register read with bytes helper functions
366  * @offset:bytes offset from MMIO start
367  *
368 */
369 
370 /**
371  * amdgpu_mm_rreg8 - read a memory mapped IO register
372  *
373  * @adev: amdgpu_device pointer
374  * @offset: byte aligned register offset
375  *
376  * Returns the 8 bit value from the offset specified.
377  */
378 uint8_t amdgpu_mm_rreg8(struct amdgpu_device *adev, uint32_t offset)
379 {
380 	if (adev->in_pci_err_recovery)
381 		return 0;
382 
383 	if (offset < adev->rmmio_size)
384 		return (readb(adev->rmmio + offset));
385 	BUG();
386 }
387 
388 /*
389  * MMIO register write with bytes helper functions
390  * @offset:bytes offset from MMIO start
391  * @value: the value want to be written to the register
392  *
393 */
394 /**
395  * amdgpu_mm_wreg8 - read a memory mapped IO register
396  *
397  * @adev: amdgpu_device pointer
398  * @offset: byte aligned register offset
399  * @value: 8 bit value to write
400  *
401  * Writes the value specified to the offset specified.
402  */
403 void amdgpu_mm_wreg8(struct amdgpu_device *adev, uint32_t offset, uint8_t value)
404 {
405 	if (adev->in_pci_err_recovery)
406 		return;
407 
408 	if (offset < adev->rmmio_size)
409 		writeb(value, adev->rmmio + offset);
410 	else
411 		BUG();
412 }
413 
414 /**
415  * amdgpu_device_wreg - write to a memory mapped IO or indirect register
416  *
417  * @adev: amdgpu_device pointer
418  * @reg: dword aligned register offset
419  * @v: 32 bit value to write to the register
420  * @acc_flags: access flags which require special behavior
421  *
422  * Writes the value specified to the offset specified.
423  */
424 void amdgpu_device_wreg(struct amdgpu_device *adev,
425 			uint32_t reg, uint32_t v,
426 			uint32_t acc_flags)
427 {
428 	if (adev->in_pci_err_recovery)
429 		return;
430 
431 	if ((reg * 4) < adev->rmmio_size) {
432 		if (!(acc_flags & AMDGPU_REGS_NO_KIQ) &&
433 		    amdgpu_sriov_runtime(adev) &&
434 		    down_read_trylock(&adev->reset_sem)) {
435 			amdgpu_kiq_wreg(adev, reg, v);
436 			up_read(&adev->reset_sem);
437 		} else {
438 			writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
439 		}
440 	} else {
441 		adev->pcie_wreg(adev, reg * 4, v);
442 	}
443 
444 	trace_amdgpu_device_wreg(adev->pdev->device, reg, v);
445 }
446 
447 /*
448  * amdgpu_mm_wreg_mmio_rlc -  write register either with mmio or with RLC path if in range
449  *
450  * this function is invoked only the debugfs register access
451  * */
452 void amdgpu_mm_wreg_mmio_rlc(struct amdgpu_device *adev,
453 			     uint32_t reg, uint32_t v)
454 {
455 	if (adev->in_pci_err_recovery)
456 		return;
457 
458 	if (amdgpu_sriov_fullaccess(adev) &&
459 	    adev->gfx.rlc.funcs &&
460 	    adev->gfx.rlc.funcs->is_rlcg_access_range) {
461 		if (adev->gfx.rlc.funcs->is_rlcg_access_range(adev, reg))
462 			return adev->gfx.rlc.funcs->rlcg_wreg(adev, reg, v);
463 	} else {
464 		writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
465 	}
466 }
467 
468 /**
469  * amdgpu_io_rreg - read an IO register
470  *
471  * @adev: amdgpu_device pointer
472  * @reg: dword aligned register offset
473  *
474  * Returns the 32 bit value from the offset specified.
475  */
476 u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 reg)
477 {
478 	if (adev->in_pci_err_recovery)
479 		return 0;
480 
481 	if ((reg * 4) < adev->rio_mem_size)
482 		return ioread32(adev->rio_mem + (reg * 4));
483 	else {
484 		iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
485 		return ioread32(adev->rio_mem + (mmMM_DATA * 4));
486 	}
487 }
488 
489 /**
490  * amdgpu_io_wreg - write to an IO register
491  *
492  * @adev: amdgpu_device pointer
493  * @reg: dword aligned register offset
494  * @v: 32 bit value to write to the register
495  *
496  * Writes the value specified to the offset specified.
497  */
498 void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
499 {
500 	if (adev->in_pci_err_recovery)
501 		return;
502 
503 	if ((reg * 4) < adev->rio_mem_size)
504 		iowrite32(v, adev->rio_mem + (reg * 4));
505 	else {
506 		iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
507 		iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
508 	}
509 }
510 
511 /**
512  * amdgpu_mm_rdoorbell - read a doorbell dword
513  *
514  * @adev: amdgpu_device pointer
515  * @index: doorbell index
516  *
517  * Returns the value in the doorbell aperture at the
518  * requested doorbell index (CIK).
519  */
520 u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
521 {
522 	if (adev->in_pci_err_recovery)
523 		return 0;
524 
525 	if (index < adev->doorbell.num_doorbells) {
526 		return readl(adev->doorbell.ptr + index);
527 	} else {
528 		DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
529 		return 0;
530 	}
531 }
532 
533 /**
534  * amdgpu_mm_wdoorbell - write a doorbell dword
535  *
536  * @adev: amdgpu_device pointer
537  * @index: doorbell index
538  * @v: value to write
539  *
540  * Writes @v to the doorbell aperture at the
541  * requested doorbell index (CIK).
542  */
543 void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
544 {
545 	if (adev->in_pci_err_recovery)
546 		return;
547 
548 	if (index < adev->doorbell.num_doorbells) {
549 		writel(v, adev->doorbell.ptr + index);
550 	} else {
551 		DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
552 	}
553 }
554 
555 /**
556  * amdgpu_mm_rdoorbell64 - read a doorbell Qword
557  *
558  * @adev: amdgpu_device pointer
559  * @index: doorbell index
560  *
561  * Returns the value in the doorbell aperture at the
562  * requested doorbell index (VEGA10+).
563  */
564 u64 amdgpu_mm_rdoorbell64(struct amdgpu_device *adev, u32 index)
565 {
566 	if (adev->in_pci_err_recovery)
567 		return 0;
568 
569 	if (index < adev->doorbell.num_doorbells) {
570 		return atomic64_read((atomic64_t *)(adev->doorbell.ptr + index));
571 	} else {
572 		DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
573 		return 0;
574 	}
575 }
576 
577 /**
578  * amdgpu_mm_wdoorbell64 - write a doorbell Qword
579  *
580  * @adev: amdgpu_device pointer
581  * @index: doorbell index
582  * @v: value to write
583  *
584  * Writes @v to the doorbell aperture at the
585  * requested doorbell index (VEGA10+).
586  */
587 void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v)
588 {
589 	if (adev->in_pci_err_recovery)
590 		return;
591 
592 	if (index < adev->doorbell.num_doorbells) {
593 		atomic64_set((atomic64_t *)(adev->doorbell.ptr + index), v);
594 	} else {
595 		DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
596 	}
597 }
598 
599 /**
600  * amdgpu_device_indirect_rreg - read an indirect register
601  *
602  * @adev: amdgpu_device pointer
603  * @pcie_index: mmio register offset
604  * @pcie_data: mmio register offset
605  * @reg_addr: indirect register address to read from
606  *
607  * Returns the value of indirect register @reg_addr
608  */
609 u32 amdgpu_device_indirect_rreg(struct amdgpu_device *adev,
610 				u32 pcie_index, u32 pcie_data,
611 				u32 reg_addr)
612 {
613 	unsigned long flags;
614 	u32 r;
615 	void __iomem *pcie_index_offset;
616 	void __iomem *pcie_data_offset;
617 
618 	spin_lock_irqsave(&adev->pcie_idx_lock, flags);
619 	pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
620 	pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4;
621 
622 	writel(reg_addr, pcie_index_offset);
623 	readl(pcie_index_offset);
624 	r = readl(pcie_data_offset);
625 	spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
626 
627 	return r;
628 }
629 
630 /**
631  * amdgpu_device_indirect_rreg64 - read a 64bits indirect register
632  *
633  * @adev: amdgpu_device pointer
634  * @pcie_index: mmio register offset
635  * @pcie_data: mmio register offset
636  * @reg_addr: indirect register address to read from
637  *
638  * Returns the value of indirect register @reg_addr
639  */
640 u64 amdgpu_device_indirect_rreg64(struct amdgpu_device *adev,
641 				  u32 pcie_index, u32 pcie_data,
642 				  u32 reg_addr)
643 {
644 	unsigned long flags;
645 	u64 r;
646 	void __iomem *pcie_index_offset;
647 	void __iomem *pcie_data_offset;
648 
649 	spin_lock_irqsave(&adev->pcie_idx_lock, flags);
650 	pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
651 	pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4;
652 
653 	/* read low 32 bits */
654 	writel(reg_addr, pcie_index_offset);
655 	readl(pcie_index_offset);
656 	r = readl(pcie_data_offset);
657 	/* read high 32 bits */
658 	writel(reg_addr + 4, pcie_index_offset);
659 	readl(pcie_index_offset);
660 	r |= ((u64)readl(pcie_data_offset) << 32);
661 	spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
662 
663 	return r;
664 }
665 
666 /**
667  * amdgpu_device_indirect_wreg - write an indirect register address
668  *
669  * @adev: amdgpu_device pointer
670  * @pcie_index: mmio register offset
671  * @pcie_data: mmio register offset
672  * @reg_addr: indirect register offset
673  * @reg_data: indirect register data
674  *
675  */
676 void amdgpu_device_indirect_wreg(struct amdgpu_device *adev,
677 				 u32 pcie_index, u32 pcie_data,
678 				 u32 reg_addr, u32 reg_data)
679 {
680 	unsigned long flags;
681 	void __iomem *pcie_index_offset;
682 	void __iomem *pcie_data_offset;
683 
684 	spin_lock_irqsave(&adev->pcie_idx_lock, flags);
685 	pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
686 	pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4;
687 
688 	writel(reg_addr, pcie_index_offset);
689 	readl(pcie_index_offset);
690 	writel(reg_data, pcie_data_offset);
691 	readl(pcie_data_offset);
692 	spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
693 }
694 
695 /**
696  * amdgpu_device_indirect_wreg64 - write a 64bits indirect register address
697  *
698  * @adev: amdgpu_device pointer
699  * @pcie_index: mmio register offset
700  * @pcie_data: mmio register offset
701  * @reg_addr: indirect register offset
702  * @reg_data: indirect register data
703  *
704  */
705 void amdgpu_device_indirect_wreg64(struct amdgpu_device *adev,
706 				   u32 pcie_index, u32 pcie_data,
707 				   u32 reg_addr, u64 reg_data)
708 {
709 	unsigned long flags;
710 	void __iomem *pcie_index_offset;
711 	void __iomem *pcie_data_offset;
712 
713 	spin_lock_irqsave(&adev->pcie_idx_lock, flags);
714 	pcie_index_offset = (void __iomem *)adev->rmmio + pcie_index * 4;
715 	pcie_data_offset = (void __iomem *)adev->rmmio + pcie_data * 4;
716 
717 	/* write low 32 bits */
718 	writel(reg_addr, pcie_index_offset);
719 	readl(pcie_index_offset);
720 	writel((u32)(reg_data & 0xffffffffULL), pcie_data_offset);
721 	readl(pcie_data_offset);
722 	/* write high 32 bits */
723 	writel(reg_addr + 4, pcie_index_offset);
724 	readl(pcie_index_offset);
725 	writel((u32)(reg_data >> 32), pcie_data_offset);
726 	readl(pcie_data_offset);
727 	spin_unlock_irqrestore(&adev->pcie_idx_lock, flags);
728 }
729 
730 /**
731  * amdgpu_invalid_rreg - dummy reg read function
732  *
733  * @adev: amdgpu_device pointer
734  * @reg: offset of register
735  *
736  * Dummy register read function.  Used for register blocks
737  * that certain asics don't have (all asics).
738  * Returns the value in the register.
739  */
740 static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
741 {
742 	DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
743 	BUG();
744 	return 0;
745 }
746 
747 /**
748  * amdgpu_invalid_wreg - dummy reg write function
749  *
750  * @adev: amdgpu_device pointer
751  * @reg: offset of register
752  * @v: value to write to the register
753  *
754  * Dummy register read function.  Used for register blocks
755  * that certain asics don't have (all asics).
756  */
757 static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
758 {
759 	DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
760 		  reg, v);
761 	BUG();
762 }
763 
764 /**
765  * amdgpu_invalid_rreg64 - dummy 64 bit reg read function
766  *
767  * @adev: amdgpu_device pointer
768  * @reg: offset of register
769  *
770  * Dummy register read function.  Used for register blocks
771  * that certain asics don't have (all asics).
772  * Returns the value in the register.
773  */
774 static uint64_t amdgpu_invalid_rreg64(struct amdgpu_device *adev, uint32_t reg)
775 {
776 	DRM_ERROR("Invalid callback to read 64 bit register 0x%04X\n", reg);
777 	BUG();
778 	return 0;
779 }
780 
781 /**
782  * amdgpu_invalid_wreg64 - dummy reg write function
783  *
784  * @adev: amdgpu_device pointer
785  * @reg: offset of register
786  * @v: value to write to the register
787  *
788  * Dummy register read function.  Used for register blocks
789  * that certain asics don't have (all asics).
790  */
791 static void amdgpu_invalid_wreg64(struct amdgpu_device *adev, uint32_t reg, uint64_t v)
792 {
793 	DRM_ERROR("Invalid callback to write 64 bit register 0x%04X with 0x%08llX\n",
794 		  reg, v);
795 	BUG();
796 }
797 
798 /**
799  * amdgpu_block_invalid_rreg - dummy reg read function
800  *
801  * @adev: amdgpu_device pointer
802  * @block: offset of instance
803  * @reg: offset of register
804  *
805  * Dummy register read function.  Used for register blocks
806  * that certain asics don't have (all asics).
807  * Returns the value in the register.
808  */
809 static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
810 					  uint32_t block, uint32_t reg)
811 {
812 	DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
813 		  reg, block);
814 	BUG();
815 	return 0;
816 }
817 
818 /**
819  * amdgpu_block_invalid_wreg - dummy reg write function
820  *
821  * @adev: amdgpu_device pointer
822  * @block: offset of instance
823  * @reg: offset of register
824  * @v: value to write to the register
825  *
826  * Dummy register read function.  Used for register blocks
827  * that certain asics don't have (all asics).
828  */
829 static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
830 				      uint32_t block,
831 				      uint32_t reg, uint32_t v)
832 {
833 	DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
834 		  reg, block, v);
835 	BUG();
836 }
837 
838 /**
839  * amdgpu_device_asic_init - Wrapper for atom asic_init
840  *
841  * @adev: amdgpu_device pointer
842  *
843  * Does any asic specific work and then calls atom asic init.
844  */
845 static int amdgpu_device_asic_init(struct amdgpu_device *adev)
846 {
847 	amdgpu_asic_pre_asic_init(adev);
848 
849 	return amdgpu_atom_asic_init(adev->mode_info.atom_context);
850 }
851 
852 /**
853  * amdgpu_device_vram_scratch_init - allocate the VRAM scratch page
854  *
855  * @adev: amdgpu_device pointer
856  *
857  * Allocates a scratch page of VRAM for use by various things in the
858  * driver.
859  */
860 static int amdgpu_device_vram_scratch_init(struct amdgpu_device *adev)
861 {
862 	return amdgpu_bo_create_kernel(adev, AMDGPU_GPU_PAGE_SIZE,
863 				       PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
864 				       &adev->vram_scratch.robj,
865 				       &adev->vram_scratch.gpu_addr,
866 				       (void **)&adev->vram_scratch.ptr);
867 }
868 
869 /**
870  * amdgpu_device_vram_scratch_fini - Free the VRAM scratch page
871  *
872  * @adev: amdgpu_device pointer
873  *
874  * Frees the VRAM scratch page.
875  */
876 static void amdgpu_device_vram_scratch_fini(struct amdgpu_device *adev)
877 {
878 	amdgpu_bo_free_kernel(&adev->vram_scratch.robj, NULL, NULL);
879 }
880 
881 /**
882  * amdgpu_device_program_register_sequence - program an array of registers.
883  *
884  * @adev: amdgpu_device pointer
885  * @registers: pointer to the register array
886  * @array_size: size of the register array
887  *
888  * Programs an array or registers with and and or masks.
889  * This is a helper for setting golden registers.
890  */
891 void amdgpu_device_program_register_sequence(struct amdgpu_device *adev,
892 					     const u32 *registers,
893 					     const u32 array_size)
894 {
895 	u32 tmp, reg, and_mask, or_mask;
896 	int i;
897 
898 	if (array_size % 3)
899 		return;
900 
901 	for (i = 0; i < array_size; i +=3) {
902 		reg = registers[i + 0];
903 		and_mask = registers[i + 1];
904 		or_mask = registers[i + 2];
905 
906 		if (and_mask == 0xffffffff) {
907 			tmp = or_mask;
908 		} else {
909 			tmp = RREG32(reg);
910 			tmp &= ~and_mask;
911 			if (adev->family >= AMDGPU_FAMILY_AI)
912 				tmp |= (or_mask & and_mask);
913 			else
914 				tmp |= or_mask;
915 		}
916 		WREG32(reg, tmp);
917 	}
918 }
919 
920 /**
921  * amdgpu_device_pci_config_reset - reset the GPU
922  *
923  * @adev: amdgpu_device pointer
924  *
925  * Resets the GPU using the pci config reset sequence.
926  * Only applicable to asics prior to vega10.
927  */
928 void amdgpu_device_pci_config_reset(struct amdgpu_device *adev)
929 {
930 	pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
931 }
932 
933 /*
934  * GPU doorbell aperture helpers function.
935  */
936 /**
937  * amdgpu_device_doorbell_init - Init doorbell driver information.
938  *
939  * @adev: amdgpu_device pointer
940  *
941  * Init doorbell driver information (CIK)
942  * Returns 0 on success, error on failure.
943  */
944 static int amdgpu_device_doorbell_init(struct amdgpu_device *adev)
945 {
946 
947 	/* No doorbell on SI hardware generation */
948 	if (adev->asic_type < CHIP_BONAIRE) {
949 		adev->doorbell.base = 0;
950 		adev->doorbell.size = 0;
951 		adev->doorbell.num_doorbells = 0;
952 		adev->doorbell.ptr = NULL;
953 		return 0;
954 	}
955 
956 	if (pci_resource_flags(adev->pdev, 2) & IORESOURCE_UNSET)
957 		return -EINVAL;
958 
959 	amdgpu_asic_init_doorbell_index(adev);
960 
961 	/* doorbell bar mapping */
962 	adev->doorbell.base = pci_resource_start(adev->pdev, 2);
963 	adev->doorbell.size = pci_resource_len(adev->pdev, 2);
964 
965 	adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
966 					     adev->doorbell_index.max_assignment+1);
967 	if (adev->doorbell.num_doorbells == 0)
968 		return -EINVAL;
969 
970 	/* For Vega, reserve and map two pages on doorbell BAR since SDMA
971 	 * paging queue doorbell use the second page. The
972 	 * AMDGPU_DOORBELL64_MAX_ASSIGNMENT definition assumes all the
973 	 * doorbells are in the first page. So with paging queue enabled,
974 	 * the max num_doorbells should + 1 page (0x400 in dword)
975 	 */
976 	if (adev->asic_type >= CHIP_VEGA10)
977 		adev->doorbell.num_doorbells += 0x400;
978 
979 	adev->doorbell.ptr = ioremap(adev->doorbell.base,
980 				     adev->doorbell.num_doorbells *
981 				     sizeof(u32));
982 	if (adev->doorbell.ptr == NULL)
983 		return -ENOMEM;
984 
985 	return 0;
986 }
987 
988 /**
989  * amdgpu_device_doorbell_fini - Tear down doorbell driver information.
990  *
991  * @adev: amdgpu_device pointer
992  *
993  * Tear down doorbell driver information (CIK)
994  */
995 static void amdgpu_device_doorbell_fini(struct amdgpu_device *adev)
996 {
997 	iounmap(adev->doorbell.ptr);
998 	adev->doorbell.ptr = NULL;
999 }
1000 
1001 
1002 
1003 /*
1004  * amdgpu_device_wb_*()
1005  * Writeback is the method by which the GPU updates special pages in memory
1006  * with the status of certain GPU events (fences, ring pointers,etc.).
1007  */
1008 
1009 /**
1010  * amdgpu_device_wb_fini - Disable Writeback and free memory
1011  *
1012  * @adev: amdgpu_device pointer
1013  *
1014  * Disables Writeback and frees the Writeback memory (all asics).
1015  * Used at driver shutdown.
1016  */
1017 static void amdgpu_device_wb_fini(struct amdgpu_device *adev)
1018 {
1019 	if (adev->wb.wb_obj) {
1020 		amdgpu_bo_free_kernel(&adev->wb.wb_obj,
1021 				      &adev->wb.gpu_addr,
1022 				      (void **)&adev->wb.wb);
1023 		adev->wb.wb_obj = NULL;
1024 	}
1025 }
1026 
1027 /**
1028  * amdgpu_device_wb_init- Init Writeback driver info and allocate memory
1029  *
1030  * @adev: amdgpu_device pointer
1031  *
1032  * Initializes writeback and allocates writeback memory (all asics).
1033  * Used at driver startup.
1034  * Returns 0 on success or an -error on failure.
1035  */
1036 static int amdgpu_device_wb_init(struct amdgpu_device *adev)
1037 {
1038 	int r;
1039 
1040 	if (adev->wb.wb_obj == NULL) {
1041 		/* AMDGPU_MAX_WB * sizeof(uint32_t) * 8 = AMDGPU_MAX_WB 256bit slots */
1042 		r = amdgpu_bo_create_kernel(adev, AMDGPU_MAX_WB * sizeof(uint32_t) * 8,
1043 					    PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
1044 					    &adev->wb.wb_obj, &adev->wb.gpu_addr,
1045 					    (void **)&adev->wb.wb);
1046 		if (r) {
1047 			dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
1048 			return r;
1049 		}
1050 
1051 		adev->wb.num_wb = AMDGPU_MAX_WB;
1052 		memset(&adev->wb.used, 0, sizeof(adev->wb.used));
1053 
1054 		/* clear wb memory */
1055 		memset((char *)adev->wb.wb, 0, AMDGPU_MAX_WB * sizeof(uint32_t) * 8);
1056 	}
1057 
1058 	return 0;
1059 }
1060 
1061 /**
1062  * amdgpu_device_wb_get - Allocate a wb entry
1063  *
1064  * @adev: amdgpu_device pointer
1065  * @wb: wb index
1066  *
1067  * Allocate a wb slot for use by the driver (all asics).
1068  * Returns 0 on success or -EINVAL on failure.
1069  */
1070 int amdgpu_device_wb_get(struct amdgpu_device *adev, u32 *wb)
1071 {
1072 	unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
1073 
1074 	if (offset < adev->wb.num_wb) {
1075 		__set_bit(offset, adev->wb.used);
1076 		*wb = offset << 3; /* convert to dw offset */
1077 		return 0;
1078 	} else {
1079 		return -EINVAL;
1080 	}
1081 }
1082 
1083 /**
1084  * amdgpu_device_wb_free - Free a wb entry
1085  *
1086  * @adev: amdgpu_device pointer
1087  * @wb: wb index
1088  *
1089  * Free a wb slot allocated for use by the driver (all asics)
1090  */
1091 void amdgpu_device_wb_free(struct amdgpu_device *adev, u32 wb)
1092 {
1093 	wb >>= 3;
1094 	if (wb < adev->wb.num_wb)
1095 		__clear_bit(wb, adev->wb.used);
1096 }
1097 
1098 /**
1099  * amdgpu_device_resize_fb_bar - try to resize FB BAR
1100  *
1101  * @adev: amdgpu_device pointer
1102  *
1103  * Try to resize FB BAR to make all VRAM CPU accessible. We try very hard not
1104  * to fail, but if any of the BARs is not accessible after the size we abort
1105  * driver loading by returning -ENODEV.
1106  */
1107 int amdgpu_device_resize_fb_bar(struct amdgpu_device *adev)
1108 {
1109 	u64 space_needed = roundup_pow_of_two(adev->gmc.real_vram_size);
1110 	int rbar_size = pci_rebar_bytes_to_size(adev->gmc.real_vram_size);
1111 	struct pci_bus *root;
1112 	struct resource *res;
1113 	unsigned i;
1114 	u16 cmd;
1115 	int r;
1116 
1117 	/* Bypass for VF */
1118 	if (amdgpu_sriov_vf(adev))
1119 		return 0;
1120 
1121 	/* skip if the bios has already enabled large BAR */
1122 	if (adev->gmc.real_vram_size &&
1123 	    (pci_resource_len(adev->pdev, 0) >= adev->gmc.real_vram_size))
1124 		return 0;
1125 
1126 	/* Check if the root BUS has 64bit memory resources */
1127 	root = adev->pdev->bus;
1128 	while (root->parent)
1129 		root = root->parent;
1130 
1131 	pci_bus_for_each_resource(root, res, i) {
1132 		if (res && res->flags & (IORESOURCE_MEM | IORESOURCE_MEM_64) &&
1133 		    res->start > 0x100000000ull)
1134 			break;
1135 	}
1136 
1137 	/* Trying to resize is pointless without a root hub window above 4GB */
1138 	if (!res)
1139 		return 0;
1140 
1141 	/* Limit the BAR size to what is available */
1142 	rbar_size = min(fls(pci_rebar_get_possible_sizes(adev->pdev, 0)) - 1,
1143 			rbar_size);
1144 
1145 	/* Disable memory decoding while we change the BAR addresses and size */
1146 	pci_read_config_word(adev->pdev, PCI_COMMAND, &cmd);
1147 	pci_write_config_word(adev->pdev, PCI_COMMAND,
1148 			      cmd & ~PCI_COMMAND_MEMORY);
1149 
1150 	/* Free the VRAM and doorbell BAR, we most likely need to move both. */
1151 	amdgpu_device_doorbell_fini(adev);
1152 	if (adev->asic_type >= CHIP_BONAIRE)
1153 		pci_release_resource(adev->pdev, 2);
1154 
1155 	pci_release_resource(adev->pdev, 0);
1156 
1157 	r = pci_resize_resource(adev->pdev, 0, rbar_size);
1158 	if (r == -ENOSPC)
1159 		DRM_INFO("Not enough PCI address space for a large BAR.");
1160 	else if (r && r != -ENOTSUPP)
1161 		DRM_ERROR("Problem resizing BAR0 (%d).", r);
1162 
1163 	pci_assign_unassigned_bus_resources(adev->pdev->bus);
1164 
1165 	/* When the doorbell or fb BAR isn't available we have no chance of
1166 	 * using the device.
1167 	 */
1168 	r = amdgpu_device_doorbell_init(adev);
1169 	if (r || (pci_resource_flags(adev->pdev, 0) & IORESOURCE_UNSET))
1170 		return -ENODEV;
1171 
1172 	pci_write_config_word(adev->pdev, PCI_COMMAND, cmd);
1173 
1174 	return 0;
1175 }
1176 
1177 /*
1178  * GPU helpers function.
1179  */
1180 /**
1181  * amdgpu_device_need_post - check if the hw need post or not
1182  *
1183  * @adev: amdgpu_device pointer
1184  *
1185  * Check if the asic has been initialized (all asics) at driver startup
1186  * or post is needed if  hw reset is performed.
1187  * Returns true if need or false if not.
1188  */
1189 bool amdgpu_device_need_post(struct amdgpu_device *adev)
1190 {
1191 	uint32_t reg;
1192 
1193 	if (amdgpu_sriov_vf(adev))
1194 		return false;
1195 
1196 	if (amdgpu_passthrough(adev)) {
1197 		/* for FIJI: In whole GPU pass-through virtualization case, after VM reboot
1198 		 * some old smc fw still need driver do vPost otherwise gpu hang, while
1199 		 * those smc fw version above 22.15 doesn't have this flaw, so we force
1200 		 * vpost executed for smc version below 22.15
1201 		 */
1202 		if (adev->asic_type == CHIP_FIJI) {
1203 			int err;
1204 			uint32_t fw_ver;
1205 			err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
1206 			/* force vPost if error occured */
1207 			if (err)
1208 				return true;
1209 
1210 			fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
1211 			if (fw_ver < 0x00160e00)
1212 				return true;
1213 		}
1214 	}
1215 
1216 	if (adev->has_hw_reset) {
1217 		adev->has_hw_reset = false;
1218 		return true;
1219 	}
1220 
1221 	/* bios scratch used on CIK+ */
1222 	if (adev->asic_type >= CHIP_BONAIRE)
1223 		return amdgpu_atombios_scratch_need_asic_init(adev);
1224 
1225 	/* check MEM_SIZE for older asics */
1226 	reg = amdgpu_asic_get_config_memsize(adev);
1227 
1228 	if ((reg != 0) && (reg != 0xffffffff))
1229 		return false;
1230 
1231 	return true;
1232 }
1233 
1234 /* if we get transitioned to only one device, take VGA back */
1235 /**
1236  * amdgpu_device_vga_set_decode - enable/disable vga decode
1237  *
1238  * @cookie: amdgpu_device pointer
1239  * @state: enable/disable vga decode
1240  *
1241  * Enable/disable vga decode (all asics).
1242  * Returns VGA resource flags.
1243  */
1244 static unsigned int amdgpu_device_vga_set_decode(void *cookie, bool state)
1245 {
1246 	struct amdgpu_device *adev = cookie;
1247 	amdgpu_asic_set_vga_state(adev, state);
1248 	if (state)
1249 		return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
1250 		       VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1251 	else
1252 		return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1253 }
1254 
1255 /**
1256  * amdgpu_device_check_block_size - validate the vm block size
1257  *
1258  * @adev: amdgpu_device pointer
1259  *
1260  * Validates the vm block size specified via module parameter.
1261  * The vm block size defines number of bits in page table versus page directory,
1262  * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
1263  * page table and the remaining bits are in the page directory.
1264  */
1265 static void amdgpu_device_check_block_size(struct amdgpu_device *adev)
1266 {
1267 	/* defines number of bits in page table versus page directory,
1268 	 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
1269 	 * page table and the remaining bits are in the page directory */
1270 	if (amdgpu_vm_block_size == -1)
1271 		return;
1272 
1273 	if (amdgpu_vm_block_size < 9) {
1274 		dev_warn(adev->dev, "VM page table size (%d) too small\n",
1275 			 amdgpu_vm_block_size);
1276 		amdgpu_vm_block_size = -1;
1277 	}
1278 }
1279 
1280 /**
1281  * amdgpu_device_check_vm_size - validate the vm size
1282  *
1283  * @adev: amdgpu_device pointer
1284  *
1285  * Validates the vm size in GB specified via module parameter.
1286  * The VM size is the size of the GPU virtual memory space in GB.
1287  */
1288 static void amdgpu_device_check_vm_size(struct amdgpu_device *adev)
1289 {
1290 	/* no need to check the default value */
1291 	if (amdgpu_vm_size == -1)
1292 		return;
1293 
1294 	if (amdgpu_vm_size < 1) {
1295 		dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
1296 			 amdgpu_vm_size);
1297 		amdgpu_vm_size = -1;
1298 	}
1299 }
1300 
1301 static void amdgpu_device_check_smu_prv_buffer_size(struct amdgpu_device *adev)
1302 {
1303 	struct sysinfo si;
1304 	bool is_os_64 = (sizeof(void *) == 8);
1305 	uint64_t total_memory;
1306 	uint64_t dram_size_seven_GB = 0x1B8000000;
1307 	uint64_t dram_size_three_GB = 0xB8000000;
1308 
1309 	if (amdgpu_smu_memory_pool_size == 0)
1310 		return;
1311 
1312 	if (!is_os_64) {
1313 		DRM_WARN("Not 64-bit OS, feature not supported\n");
1314 		goto def_value;
1315 	}
1316 	si_meminfo(&si);
1317 	total_memory = (uint64_t)si.totalram * si.mem_unit;
1318 
1319 	if ((amdgpu_smu_memory_pool_size == 1) ||
1320 		(amdgpu_smu_memory_pool_size == 2)) {
1321 		if (total_memory < dram_size_three_GB)
1322 			goto def_value1;
1323 	} else if ((amdgpu_smu_memory_pool_size == 4) ||
1324 		(amdgpu_smu_memory_pool_size == 8)) {
1325 		if (total_memory < dram_size_seven_GB)
1326 			goto def_value1;
1327 	} else {
1328 		DRM_WARN("Smu memory pool size not supported\n");
1329 		goto def_value;
1330 	}
1331 	adev->pm.smu_prv_buffer_size = amdgpu_smu_memory_pool_size << 28;
1332 
1333 	return;
1334 
1335 def_value1:
1336 	DRM_WARN("No enough system memory\n");
1337 def_value:
1338 	adev->pm.smu_prv_buffer_size = 0;
1339 }
1340 
1341 /**
1342  * amdgpu_device_check_arguments - validate module params
1343  *
1344  * @adev: amdgpu_device pointer
1345  *
1346  * Validates certain module parameters and updates
1347  * the associated values used by the driver (all asics).
1348  */
1349 static int amdgpu_device_check_arguments(struct amdgpu_device *adev)
1350 {
1351 	if (amdgpu_sched_jobs < 4) {
1352 		dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
1353 			 amdgpu_sched_jobs);
1354 		amdgpu_sched_jobs = 4;
1355 	} else if (!is_power_of_2(amdgpu_sched_jobs)){
1356 		dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
1357 			 amdgpu_sched_jobs);
1358 		amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
1359 	}
1360 
1361 	if (amdgpu_gart_size != -1 && amdgpu_gart_size < 32) {
1362 		/* gart size must be greater or equal to 32M */
1363 		dev_warn(adev->dev, "gart size (%d) too small\n",
1364 			 amdgpu_gart_size);
1365 		amdgpu_gart_size = -1;
1366 	}
1367 
1368 	if (amdgpu_gtt_size != -1 && amdgpu_gtt_size < 32) {
1369 		/* gtt size must be greater or equal to 32M */
1370 		dev_warn(adev->dev, "gtt size (%d) too small\n",
1371 				 amdgpu_gtt_size);
1372 		amdgpu_gtt_size = -1;
1373 	}
1374 
1375 	/* valid range is between 4 and 9 inclusive */
1376 	if (amdgpu_vm_fragment_size != -1 &&
1377 	    (amdgpu_vm_fragment_size > 9 || amdgpu_vm_fragment_size < 4)) {
1378 		dev_warn(adev->dev, "valid range is between 4 and 9\n");
1379 		amdgpu_vm_fragment_size = -1;
1380 	}
1381 
1382 	if (amdgpu_sched_hw_submission < 2) {
1383 		dev_warn(adev->dev, "sched hw submission jobs (%d) must be at least 2\n",
1384 			 amdgpu_sched_hw_submission);
1385 		amdgpu_sched_hw_submission = 2;
1386 	} else if (!is_power_of_2(amdgpu_sched_hw_submission)) {
1387 		dev_warn(adev->dev, "sched hw submission jobs (%d) must be a power of 2\n",
1388 			 amdgpu_sched_hw_submission);
1389 		amdgpu_sched_hw_submission = roundup_pow_of_two(amdgpu_sched_hw_submission);
1390 	}
1391 
1392 	amdgpu_device_check_smu_prv_buffer_size(adev);
1393 
1394 	amdgpu_device_check_vm_size(adev);
1395 
1396 	amdgpu_device_check_block_size(adev);
1397 
1398 	adev->firmware.load_type = amdgpu_ucode_get_load_type(adev, amdgpu_fw_load_type);
1399 
1400 	amdgpu_gmc_tmz_set(adev);
1401 
1402 	amdgpu_gmc_noretry_set(adev);
1403 
1404 	return 0;
1405 }
1406 
1407 /**
1408  * amdgpu_switcheroo_set_state - set switcheroo state
1409  *
1410  * @pdev: pci dev pointer
1411  * @state: vga_switcheroo state
1412  *
1413  * Callback for the switcheroo driver.  Suspends or resumes the
1414  * the asics before or after it is powered up using ACPI methods.
1415  */
1416 static void amdgpu_switcheroo_set_state(struct pci_dev *pdev,
1417 					enum vga_switcheroo_state state)
1418 {
1419 	struct drm_device *dev = pci_get_drvdata(pdev);
1420 	int r;
1421 
1422 	if (amdgpu_device_supports_atpx(dev) && state == VGA_SWITCHEROO_OFF)
1423 		return;
1424 
1425 	if (state == VGA_SWITCHEROO_ON) {
1426 		pr_info("switched on\n");
1427 		/* don't suspend or resume card normally */
1428 		dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1429 
1430 		pci_set_power_state(dev->pdev, PCI_D0);
1431 		amdgpu_device_load_pci_state(dev->pdev);
1432 		r = pci_enable_device(dev->pdev);
1433 		if (r)
1434 			DRM_WARN("pci_enable_device failed (%d)\n", r);
1435 		amdgpu_device_resume(dev, true);
1436 
1437 		dev->switch_power_state = DRM_SWITCH_POWER_ON;
1438 		drm_kms_helper_poll_enable(dev);
1439 	} else {
1440 		pr_info("switched off\n");
1441 		drm_kms_helper_poll_disable(dev);
1442 		dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1443 		amdgpu_device_suspend(dev, true);
1444 		amdgpu_device_cache_pci_state(dev->pdev);
1445 		/* Shut down the device */
1446 		pci_disable_device(dev->pdev);
1447 		pci_set_power_state(dev->pdev, PCI_D3cold);
1448 		dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1449 	}
1450 }
1451 
1452 /**
1453  * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1454  *
1455  * @pdev: pci dev pointer
1456  *
1457  * Callback for the switcheroo driver.  Check of the switcheroo
1458  * state can be changed.
1459  * Returns true if the state can be changed, false if not.
1460  */
1461 static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1462 {
1463 	struct drm_device *dev = pci_get_drvdata(pdev);
1464 
1465 	/*
1466 	* FIXME: open_count is protected by drm_global_mutex but that would lead to
1467 	* locking inversion with the driver load path. And the access here is
1468 	* completely racy anyway. So don't bother with locking for now.
1469 	*/
1470 	return atomic_read(&dev->open_count) == 0;
1471 }
1472 
1473 static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1474 	.set_gpu_state = amdgpu_switcheroo_set_state,
1475 	.reprobe = NULL,
1476 	.can_switch = amdgpu_switcheroo_can_switch,
1477 };
1478 
1479 /**
1480  * amdgpu_device_ip_set_clockgating_state - set the CG state
1481  *
1482  * @dev: amdgpu_device pointer
1483  * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1484  * @state: clockgating state (gate or ungate)
1485  *
1486  * Sets the requested clockgating state for all instances of
1487  * the hardware IP specified.
1488  * Returns the error code from the last instance.
1489  */
1490 int amdgpu_device_ip_set_clockgating_state(void *dev,
1491 					   enum amd_ip_block_type block_type,
1492 					   enum amd_clockgating_state state)
1493 {
1494 	struct amdgpu_device *adev = dev;
1495 	int i, r = 0;
1496 
1497 	for (i = 0; i < adev->num_ip_blocks; i++) {
1498 		if (!adev->ip_blocks[i].status.valid)
1499 			continue;
1500 		if (adev->ip_blocks[i].version->type != block_type)
1501 			continue;
1502 		if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
1503 			continue;
1504 		r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
1505 			(void *)adev, state);
1506 		if (r)
1507 			DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
1508 				  adev->ip_blocks[i].version->funcs->name, r);
1509 	}
1510 	return r;
1511 }
1512 
1513 /**
1514  * amdgpu_device_ip_set_powergating_state - set the PG state
1515  *
1516  * @dev: amdgpu_device pointer
1517  * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1518  * @state: powergating state (gate or ungate)
1519  *
1520  * Sets the requested powergating state for all instances of
1521  * the hardware IP specified.
1522  * Returns the error code from the last instance.
1523  */
1524 int amdgpu_device_ip_set_powergating_state(void *dev,
1525 					   enum amd_ip_block_type block_type,
1526 					   enum amd_powergating_state state)
1527 {
1528 	struct amdgpu_device *adev = dev;
1529 	int i, r = 0;
1530 
1531 	for (i = 0; i < adev->num_ip_blocks; i++) {
1532 		if (!adev->ip_blocks[i].status.valid)
1533 			continue;
1534 		if (adev->ip_blocks[i].version->type != block_type)
1535 			continue;
1536 		if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
1537 			continue;
1538 		r = adev->ip_blocks[i].version->funcs->set_powergating_state(
1539 			(void *)adev, state);
1540 		if (r)
1541 			DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
1542 				  adev->ip_blocks[i].version->funcs->name, r);
1543 	}
1544 	return r;
1545 }
1546 
1547 /**
1548  * amdgpu_device_ip_get_clockgating_state - get the CG state
1549  *
1550  * @adev: amdgpu_device pointer
1551  * @flags: clockgating feature flags
1552  *
1553  * Walks the list of IPs on the device and updates the clockgating
1554  * flags for each IP.
1555  * Updates @flags with the feature flags for each hardware IP where
1556  * clockgating is enabled.
1557  */
1558 void amdgpu_device_ip_get_clockgating_state(struct amdgpu_device *adev,
1559 					    u32 *flags)
1560 {
1561 	int i;
1562 
1563 	for (i = 0; i < adev->num_ip_blocks; i++) {
1564 		if (!adev->ip_blocks[i].status.valid)
1565 			continue;
1566 		if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
1567 			adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
1568 	}
1569 }
1570 
1571 /**
1572  * amdgpu_device_ip_wait_for_idle - wait for idle
1573  *
1574  * @adev: amdgpu_device pointer
1575  * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1576  *
1577  * Waits for the request hardware IP to be idle.
1578  * Returns 0 for success or a negative error code on failure.
1579  */
1580 int amdgpu_device_ip_wait_for_idle(struct amdgpu_device *adev,
1581 				   enum amd_ip_block_type block_type)
1582 {
1583 	int i, r;
1584 
1585 	for (i = 0; i < adev->num_ip_blocks; i++) {
1586 		if (!adev->ip_blocks[i].status.valid)
1587 			continue;
1588 		if (adev->ip_blocks[i].version->type == block_type) {
1589 			r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
1590 			if (r)
1591 				return r;
1592 			break;
1593 		}
1594 	}
1595 	return 0;
1596 
1597 }
1598 
1599 /**
1600  * amdgpu_device_ip_is_idle - is the hardware IP idle
1601  *
1602  * @adev: amdgpu_device pointer
1603  * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1604  *
1605  * Check if the hardware IP is idle or not.
1606  * Returns true if it the IP is idle, false if not.
1607  */
1608 bool amdgpu_device_ip_is_idle(struct amdgpu_device *adev,
1609 			      enum amd_ip_block_type block_type)
1610 {
1611 	int i;
1612 
1613 	for (i = 0; i < adev->num_ip_blocks; i++) {
1614 		if (!adev->ip_blocks[i].status.valid)
1615 			continue;
1616 		if (adev->ip_blocks[i].version->type == block_type)
1617 			return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
1618 	}
1619 	return true;
1620 
1621 }
1622 
1623 /**
1624  * amdgpu_device_ip_get_ip_block - get a hw IP pointer
1625  *
1626  * @adev: amdgpu_device pointer
1627  * @type: Type of hardware IP (SMU, GFX, UVD, etc.)
1628  *
1629  * Returns a pointer to the hardware IP block structure
1630  * if it exists for the asic, otherwise NULL.
1631  */
1632 struct amdgpu_ip_block *
1633 amdgpu_device_ip_get_ip_block(struct amdgpu_device *adev,
1634 			      enum amd_ip_block_type type)
1635 {
1636 	int i;
1637 
1638 	for (i = 0; i < adev->num_ip_blocks; i++)
1639 		if (adev->ip_blocks[i].version->type == type)
1640 			return &adev->ip_blocks[i];
1641 
1642 	return NULL;
1643 }
1644 
1645 /**
1646  * amdgpu_device_ip_block_version_cmp
1647  *
1648  * @adev: amdgpu_device pointer
1649  * @type: enum amd_ip_block_type
1650  * @major: major version
1651  * @minor: minor version
1652  *
1653  * return 0 if equal or greater
1654  * return 1 if smaller or the ip_block doesn't exist
1655  */
1656 int amdgpu_device_ip_block_version_cmp(struct amdgpu_device *adev,
1657 				       enum amd_ip_block_type type,
1658 				       u32 major, u32 minor)
1659 {
1660 	struct amdgpu_ip_block *ip_block = amdgpu_device_ip_get_ip_block(adev, type);
1661 
1662 	if (ip_block && ((ip_block->version->major > major) ||
1663 			((ip_block->version->major == major) &&
1664 			(ip_block->version->minor >= minor))))
1665 		return 0;
1666 
1667 	return 1;
1668 }
1669 
1670 /**
1671  * amdgpu_device_ip_block_add
1672  *
1673  * @adev: amdgpu_device pointer
1674  * @ip_block_version: pointer to the IP to add
1675  *
1676  * Adds the IP block driver information to the collection of IPs
1677  * on the asic.
1678  */
1679 int amdgpu_device_ip_block_add(struct amdgpu_device *adev,
1680 			       const struct amdgpu_ip_block_version *ip_block_version)
1681 {
1682 	if (!ip_block_version)
1683 		return -EINVAL;
1684 
1685 	DRM_INFO("add ip block number %d <%s>\n", adev->num_ip_blocks,
1686 		  ip_block_version->funcs->name);
1687 
1688 	adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
1689 
1690 	return 0;
1691 }
1692 
1693 /**
1694  * amdgpu_device_enable_virtual_display - enable virtual display feature
1695  *
1696  * @adev: amdgpu_device pointer
1697  *
1698  * Enabled the virtual display feature if the user has enabled it via
1699  * the module parameter virtual_display.  This feature provides a virtual
1700  * display hardware on headless boards or in virtualized environments.
1701  * This function parses and validates the configuration string specified by
1702  * the user and configues the virtual display configuration (number of
1703  * virtual connectors, crtcs, etc.) specified.
1704  */
1705 static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
1706 {
1707 	adev->enable_virtual_display = false;
1708 
1709 	if (amdgpu_virtual_display) {
1710 		struct drm_device *ddev = adev_to_drm(adev);
1711 		const char *pci_address_name = pci_name(ddev->pdev);
1712 		char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
1713 
1714 		pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1715 		pciaddstr_tmp = pciaddstr;
1716 		while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1717 			pciaddname = strsep(&pciaddname_tmp, ",");
1718 			if (!strcmp("all", pciaddname)
1719 			    || !strcmp(pci_address_name, pciaddname)) {
1720 				long num_crtc;
1721 				int res = -1;
1722 
1723 				adev->enable_virtual_display = true;
1724 
1725 				if (pciaddname_tmp)
1726 					res = kstrtol(pciaddname_tmp, 10,
1727 						      &num_crtc);
1728 
1729 				if (!res) {
1730 					if (num_crtc < 1)
1731 						num_crtc = 1;
1732 					if (num_crtc > 6)
1733 						num_crtc = 6;
1734 					adev->mode_info.num_crtc = num_crtc;
1735 				} else {
1736 					adev->mode_info.num_crtc = 1;
1737 				}
1738 				break;
1739 			}
1740 		}
1741 
1742 		DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
1743 			 amdgpu_virtual_display, pci_address_name,
1744 			 adev->enable_virtual_display, adev->mode_info.num_crtc);
1745 
1746 		kfree(pciaddstr);
1747 	}
1748 }
1749 
1750 /**
1751  * amdgpu_device_parse_gpu_info_fw - parse gpu info firmware
1752  *
1753  * @adev: amdgpu_device pointer
1754  *
1755  * Parses the asic configuration parameters specified in the gpu info
1756  * firmware and makes them availale to the driver for use in configuring
1757  * the asic.
1758  * Returns 0 on success, -EINVAL on failure.
1759  */
1760 static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev)
1761 {
1762 	const char *chip_name;
1763 	char fw_name[40];
1764 	int err;
1765 	const struct gpu_info_firmware_header_v1_0 *hdr;
1766 
1767 	adev->firmware.gpu_info_fw = NULL;
1768 
1769 	if (adev->mman.discovery_bin) {
1770 		amdgpu_discovery_get_gfx_info(adev);
1771 
1772 		/*
1773 		 * FIXME: The bounding box is still needed by Navi12, so
1774 		 * temporarily read it from gpu_info firmware. Should be droped
1775 		 * when DAL no longer needs it.
1776 		 */
1777 		if (adev->asic_type != CHIP_NAVI12)
1778 			return 0;
1779 	}
1780 
1781 	switch (adev->asic_type) {
1782 #ifdef CONFIG_DRM_AMDGPU_SI
1783 	case CHIP_VERDE:
1784 	case CHIP_TAHITI:
1785 	case CHIP_PITCAIRN:
1786 	case CHIP_OLAND:
1787 	case CHIP_HAINAN:
1788 #endif
1789 #ifdef CONFIG_DRM_AMDGPU_CIK
1790 	case CHIP_BONAIRE:
1791 	case CHIP_HAWAII:
1792 	case CHIP_KAVERI:
1793 	case CHIP_KABINI:
1794 	case CHIP_MULLINS:
1795 #endif
1796 	case CHIP_TOPAZ:
1797 	case CHIP_TONGA:
1798 	case CHIP_FIJI:
1799 	case CHIP_POLARIS10:
1800 	case CHIP_POLARIS11:
1801 	case CHIP_POLARIS12:
1802 	case CHIP_VEGAM:
1803 	case CHIP_CARRIZO:
1804 	case CHIP_STONEY:
1805 	case CHIP_VEGA20:
1806 	case CHIP_SIENNA_CICHLID:
1807 	case CHIP_NAVY_FLOUNDER:
1808 	case CHIP_DIMGREY_CAVEFISH:
1809 	default:
1810 		return 0;
1811 	case CHIP_VEGA10:
1812 		chip_name = "vega10";
1813 		break;
1814 	case CHIP_VEGA12:
1815 		chip_name = "vega12";
1816 		break;
1817 	case CHIP_RAVEN:
1818 		if (adev->apu_flags & AMD_APU_IS_RAVEN2)
1819 			chip_name = "raven2";
1820 		else if (adev->apu_flags & AMD_APU_IS_PICASSO)
1821 			chip_name = "picasso";
1822 		else
1823 			chip_name = "raven";
1824 		break;
1825 	case CHIP_ARCTURUS:
1826 		chip_name = "arcturus";
1827 		break;
1828 	case CHIP_RENOIR:
1829 		if (adev->apu_flags & AMD_APU_IS_RENOIR)
1830 			chip_name = "renoir";
1831 		else
1832 			chip_name = "green_sardine";
1833 		break;
1834 	case CHIP_NAVI10:
1835 		chip_name = "navi10";
1836 		break;
1837 	case CHIP_NAVI14:
1838 		chip_name = "navi14";
1839 		break;
1840 	case CHIP_NAVI12:
1841 		chip_name = "navi12";
1842 		break;
1843 	case CHIP_VANGOGH:
1844 		chip_name = "vangogh";
1845 		break;
1846 	}
1847 
1848 	snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name);
1849 	err = request_firmware(&adev->firmware.gpu_info_fw, fw_name, adev->dev);
1850 	if (err) {
1851 		dev_err(adev->dev,
1852 			"Failed to load gpu_info firmware \"%s\"\n",
1853 			fw_name);
1854 		goto out;
1855 	}
1856 	err = amdgpu_ucode_validate(adev->firmware.gpu_info_fw);
1857 	if (err) {
1858 		dev_err(adev->dev,
1859 			"Failed to validate gpu_info firmware \"%s\"\n",
1860 			fw_name);
1861 		goto out;
1862 	}
1863 
1864 	hdr = (const struct gpu_info_firmware_header_v1_0 *)adev->firmware.gpu_info_fw->data;
1865 	amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
1866 
1867 	switch (hdr->version_major) {
1868 	case 1:
1869 	{
1870 		const struct gpu_info_firmware_v1_0 *gpu_info_fw =
1871 			(const struct gpu_info_firmware_v1_0 *)(adev->firmware.gpu_info_fw->data +
1872 								le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1873 
1874 		/*
1875 		 * Should be droped when DAL no longer needs it.
1876 		 */
1877 		if (adev->asic_type == CHIP_NAVI12)
1878 			goto parse_soc_bounding_box;
1879 
1880 		adev->gfx.config.max_shader_engines = le32_to_cpu(gpu_info_fw->gc_num_se);
1881 		adev->gfx.config.max_cu_per_sh = le32_to_cpu(gpu_info_fw->gc_num_cu_per_sh);
1882 		adev->gfx.config.max_sh_per_se = le32_to_cpu(gpu_info_fw->gc_num_sh_per_se);
1883 		adev->gfx.config.max_backends_per_se = le32_to_cpu(gpu_info_fw->gc_num_rb_per_se);
1884 		adev->gfx.config.max_texture_channel_caches =
1885 			le32_to_cpu(gpu_info_fw->gc_num_tccs);
1886 		adev->gfx.config.max_gprs = le32_to_cpu(gpu_info_fw->gc_num_gprs);
1887 		adev->gfx.config.max_gs_threads = le32_to_cpu(gpu_info_fw->gc_num_max_gs_thds);
1888 		adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gpu_info_fw->gc_gs_table_depth);
1889 		adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gpu_info_fw->gc_gsprim_buff_depth);
1890 		adev->gfx.config.double_offchip_lds_buf =
1891 			le32_to_cpu(gpu_info_fw->gc_double_offchip_lds_buffer);
1892 		adev->gfx.cu_info.wave_front_size = le32_to_cpu(gpu_info_fw->gc_wave_size);
1893 		adev->gfx.cu_info.max_waves_per_simd =
1894 			le32_to_cpu(gpu_info_fw->gc_max_waves_per_simd);
1895 		adev->gfx.cu_info.max_scratch_slots_per_cu =
1896 			le32_to_cpu(gpu_info_fw->gc_max_scratch_slots_per_cu);
1897 		adev->gfx.cu_info.lds_size = le32_to_cpu(gpu_info_fw->gc_lds_size);
1898 		if (hdr->version_minor >= 1) {
1899 			const struct gpu_info_firmware_v1_1 *gpu_info_fw =
1900 				(const struct gpu_info_firmware_v1_1 *)(adev->firmware.gpu_info_fw->data +
1901 									le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1902 			adev->gfx.config.num_sc_per_sh =
1903 				le32_to_cpu(gpu_info_fw->num_sc_per_sh);
1904 			adev->gfx.config.num_packer_per_sc =
1905 				le32_to_cpu(gpu_info_fw->num_packer_per_sc);
1906 		}
1907 
1908 parse_soc_bounding_box:
1909 		/*
1910 		 * soc bounding box info is not integrated in disocovery table,
1911 		 * we always need to parse it from gpu info firmware if needed.
1912 		 */
1913 		if (hdr->version_minor == 2) {
1914 			const struct gpu_info_firmware_v1_2 *gpu_info_fw =
1915 				(const struct gpu_info_firmware_v1_2 *)(adev->firmware.gpu_info_fw->data +
1916 									le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1917 			adev->dm.soc_bounding_box = &gpu_info_fw->soc_bounding_box;
1918 		}
1919 		break;
1920 	}
1921 	default:
1922 		dev_err(adev->dev,
1923 			"Unsupported gpu_info table %d\n", hdr->header.ucode_version);
1924 		err = -EINVAL;
1925 		goto out;
1926 	}
1927 out:
1928 	return err;
1929 }
1930 
1931 /**
1932  * amdgpu_device_ip_early_init - run early init for hardware IPs
1933  *
1934  * @adev: amdgpu_device pointer
1935  *
1936  * Early initialization pass for hardware IPs.  The hardware IPs that make
1937  * up each asic are discovered each IP's early_init callback is run.  This
1938  * is the first stage in initializing the asic.
1939  * Returns 0 on success, negative error code on failure.
1940  */
1941 static int amdgpu_device_ip_early_init(struct amdgpu_device *adev)
1942 {
1943 	int i, r;
1944 
1945 	amdgpu_device_enable_virtual_display(adev);
1946 
1947 	if (amdgpu_sriov_vf(adev)) {
1948 		r = amdgpu_virt_request_full_gpu(adev, true);
1949 		if (r)
1950 			return r;
1951 	}
1952 
1953 	switch (adev->asic_type) {
1954 #ifdef CONFIG_DRM_AMDGPU_SI
1955 	case CHIP_VERDE:
1956 	case CHIP_TAHITI:
1957 	case CHIP_PITCAIRN:
1958 	case CHIP_OLAND:
1959 	case CHIP_HAINAN:
1960 		adev->family = AMDGPU_FAMILY_SI;
1961 		r = si_set_ip_blocks(adev);
1962 		if (r)
1963 			return r;
1964 		break;
1965 #endif
1966 #ifdef CONFIG_DRM_AMDGPU_CIK
1967 	case CHIP_BONAIRE:
1968 	case CHIP_HAWAII:
1969 	case CHIP_KAVERI:
1970 	case CHIP_KABINI:
1971 	case CHIP_MULLINS:
1972 		if (adev->flags & AMD_IS_APU)
1973 			adev->family = AMDGPU_FAMILY_KV;
1974 		else
1975 			adev->family = AMDGPU_FAMILY_CI;
1976 
1977 		r = cik_set_ip_blocks(adev);
1978 		if (r)
1979 			return r;
1980 		break;
1981 #endif
1982 	case CHIP_TOPAZ:
1983 	case CHIP_TONGA:
1984 	case CHIP_FIJI:
1985 	case CHIP_POLARIS10:
1986 	case CHIP_POLARIS11:
1987 	case CHIP_POLARIS12:
1988 	case CHIP_VEGAM:
1989 	case CHIP_CARRIZO:
1990 	case CHIP_STONEY:
1991 		if (adev->flags & AMD_IS_APU)
1992 			adev->family = AMDGPU_FAMILY_CZ;
1993 		else
1994 			adev->family = AMDGPU_FAMILY_VI;
1995 
1996 		r = vi_set_ip_blocks(adev);
1997 		if (r)
1998 			return r;
1999 		break;
2000 	case CHIP_VEGA10:
2001 	case CHIP_VEGA12:
2002 	case CHIP_VEGA20:
2003 	case CHIP_RAVEN:
2004 	case CHIP_ARCTURUS:
2005 	case CHIP_RENOIR:
2006 		if (adev->flags & AMD_IS_APU)
2007 			adev->family = AMDGPU_FAMILY_RV;
2008 		else
2009 			adev->family = AMDGPU_FAMILY_AI;
2010 
2011 		r = soc15_set_ip_blocks(adev);
2012 		if (r)
2013 			return r;
2014 		break;
2015 	case  CHIP_NAVI10:
2016 	case  CHIP_NAVI14:
2017 	case  CHIP_NAVI12:
2018 	case  CHIP_SIENNA_CICHLID:
2019 	case  CHIP_NAVY_FLOUNDER:
2020 	case  CHIP_DIMGREY_CAVEFISH:
2021 	case CHIP_VANGOGH:
2022 		if (adev->asic_type == CHIP_VANGOGH)
2023 			adev->family = AMDGPU_FAMILY_VGH;
2024 		else
2025 			adev->family = AMDGPU_FAMILY_NV;
2026 
2027 		r = nv_set_ip_blocks(adev);
2028 		if (r)
2029 			return r;
2030 		break;
2031 	default:
2032 		/* FIXME: not supported yet */
2033 		return -EINVAL;
2034 	}
2035 
2036 	amdgpu_amdkfd_device_probe(adev);
2037 
2038 	adev->pm.pp_feature = amdgpu_pp_feature_mask;
2039 	if (amdgpu_sriov_vf(adev) || sched_policy == KFD_SCHED_POLICY_NO_HWS)
2040 		adev->pm.pp_feature &= ~PP_GFXOFF_MASK;
2041 
2042 	for (i = 0; i < adev->num_ip_blocks; i++) {
2043 		if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
2044 			DRM_ERROR("disabled ip block: %d <%s>\n",
2045 				  i, adev->ip_blocks[i].version->funcs->name);
2046 			adev->ip_blocks[i].status.valid = false;
2047 		} else {
2048 			if (adev->ip_blocks[i].version->funcs->early_init) {
2049 				r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
2050 				if (r == -ENOENT) {
2051 					adev->ip_blocks[i].status.valid = false;
2052 				} else if (r) {
2053 					DRM_ERROR("early_init of IP block <%s> failed %d\n",
2054 						  adev->ip_blocks[i].version->funcs->name, r);
2055 					return r;
2056 				} else {
2057 					adev->ip_blocks[i].status.valid = true;
2058 				}
2059 			} else {
2060 				adev->ip_blocks[i].status.valid = true;
2061 			}
2062 		}
2063 		/* get the vbios after the asic_funcs are set up */
2064 		if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON) {
2065 			r = amdgpu_device_parse_gpu_info_fw(adev);
2066 			if (r)
2067 				return r;
2068 
2069 			/* Read BIOS */
2070 			if (!amdgpu_get_bios(adev))
2071 				return -EINVAL;
2072 
2073 			r = amdgpu_atombios_init(adev);
2074 			if (r) {
2075 				dev_err(adev->dev, "amdgpu_atombios_init failed\n");
2076 				amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_INIT_FAIL, 0, 0);
2077 				return r;
2078 			}
2079 		}
2080 	}
2081 
2082 	adev->cg_flags &= amdgpu_cg_mask;
2083 	adev->pg_flags &= amdgpu_pg_mask;
2084 
2085 	return 0;
2086 }
2087 
2088 static int amdgpu_device_ip_hw_init_phase1(struct amdgpu_device *adev)
2089 {
2090 	int i, r;
2091 
2092 	for (i = 0; i < adev->num_ip_blocks; i++) {
2093 		if (!adev->ip_blocks[i].status.sw)
2094 			continue;
2095 		if (adev->ip_blocks[i].status.hw)
2096 			continue;
2097 		if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
2098 		    (amdgpu_sriov_vf(adev) && (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP)) ||
2099 		    adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH) {
2100 			r = adev->ip_blocks[i].version->funcs->hw_init(adev);
2101 			if (r) {
2102 				DRM_ERROR("hw_init of IP block <%s> failed %d\n",
2103 					  adev->ip_blocks[i].version->funcs->name, r);
2104 				return r;
2105 			}
2106 			adev->ip_blocks[i].status.hw = true;
2107 		}
2108 	}
2109 
2110 	return 0;
2111 }
2112 
2113 static int amdgpu_device_ip_hw_init_phase2(struct amdgpu_device *adev)
2114 {
2115 	int i, r;
2116 
2117 	for (i = 0; i < adev->num_ip_blocks; i++) {
2118 		if (!adev->ip_blocks[i].status.sw)
2119 			continue;
2120 		if (adev->ip_blocks[i].status.hw)
2121 			continue;
2122 		r = adev->ip_blocks[i].version->funcs->hw_init(adev);
2123 		if (r) {
2124 			DRM_ERROR("hw_init of IP block <%s> failed %d\n",
2125 				  adev->ip_blocks[i].version->funcs->name, r);
2126 			return r;
2127 		}
2128 		adev->ip_blocks[i].status.hw = true;
2129 	}
2130 
2131 	return 0;
2132 }
2133 
2134 static int amdgpu_device_fw_loading(struct amdgpu_device *adev)
2135 {
2136 	int r = 0;
2137 	int i;
2138 	uint32_t smu_version;
2139 
2140 	if (adev->asic_type >= CHIP_VEGA10) {
2141 		for (i = 0; i < adev->num_ip_blocks; i++) {
2142 			if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_PSP)
2143 				continue;
2144 
2145 			/* no need to do the fw loading again if already done*/
2146 			if (adev->ip_blocks[i].status.hw == true)
2147 				break;
2148 
2149 			if (amdgpu_in_reset(adev) || adev->in_suspend) {
2150 				r = adev->ip_blocks[i].version->funcs->resume(adev);
2151 				if (r) {
2152 					DRM_ERROR("resume of IP block <%s> failed %d\n",
2153 							  adev->ip_blocks[i].version->funcs->name, r);
2154 					return r;
2155 				}
2156 			} else {
2157 				r = adev->ip_blocks[i].version->funcs->hw_init(adev);
2158 				if (r) {
2159 					DRM_ERROR("hw_init of IP block <%s> failed %d\n",
2160 							  adev->ip_blocks[i].version->funcs->name, r);
2161 					return r;
2162 				}
2163 			}
2164 
2165 			adev->ip_blocks[i].status.hw = true;
2166 			break;
2167 		}
2168 	}
2169 
2170 	if (!amdgpu_sriov_vf(adev) || adev->asic_type == CHIP_TONGA)
2171 		r = amdgpu_pm_load_smu_firmware(adev, &smu_version);
2172 
2173 	return r;
2174 }
2175 
2176 /**
2177  * amdgpu_device_ip_init - run init for hardware IPs
2178  *
2179  * @adev: amdgpu_device pointer
2180  *
2181  * Main initialization pass for hardware IPs.  The list of all the hardware
2182  * IPs that make up the asic is walked and the sw_init and hw_init callbacks
2183  * are run.  sw_init initializes the software state associated with each IP
2184  * and hw_init initializes the hardware associated with each IP.
2185  * Returns 0 on success, negative error code on failure.
2186  */
2187 static int amdgpu_device_ip_init(struct amdgpu_device *adev)
2188 {
2189 	int i, r;
2190 
2191 	r = amdgpu_ras_init(adev);
2192 	if (r)
2193 		return r;
2194 
2195 	for (i = 0; i < adev->num_ip_blocks; i++) {
2196 		if (!adev->ip_blocks[i].status.valid)
2197 			continue;
2198 		r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
2199 		if (r) {
2200 			DRM_ERROR("sw_init of IP block <%s> failed %d\n",
2201 				  adev->ip_blocks[i].version->funcs->name, r);
2202 			goto init_failed;
2203 		}
2204 		adev->ip_blocks[i].status.sw = true;
2205 
2206 		/* need to do gmc hw init early so we can allocate gpu mem */
2207 		if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
2208 			r = amdgpu_device_vram_scratch_init(adev);
2209 			if (r) {
2210 				DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
2211 				goto init_failed;
2212 			}
2213 			r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
2214 			if (r) {
2215 				DRM_ERROR("hw_init %d failed %d\n", i, r);
2216 				goto init_failed;
2217 			}
2218 			r = amdgpu_device_wb_init(adev);
2219 			if (r) {
2220 				DRM_ERROR("amdgpu_device_wb_init failed %d\n", r);
2221 				goto init_failed;
2222 			}
2223 			adev->ip_blocks[i].status.hw = true;
2224 
2225 			/* right after GMC hw init, we create CSA */
2226 			if (amdgpu_mcbp || amdgpu_sriov_vf(adev)) {
2227 				r = amdgpu_allocate_static_csa(adev, &adev->virt.csa_obj,
2228 								AMDGPU_GEM_DOMAIN_VRAM,
2229 								AMDGPU_CSA_SIZE);
2230 				if (r) {
2231 					DRM_ERROR("allocate CSA failed %d\n", r);
2232 					goto init_failed;
2233 				}
2234 			}
2235 		}
2236 	}
2237 
2238 	if (amdgpu_sriov_vf(adev))
2239 		amdgpu_virt_init_data_exchange(adev);
2240 
2241 	r = amdgpu_ib_pool_init(adev);
2242 	if (r) {
2243 		dev_err(adev->dev, "IB initialization failed (%d).\n", r);
2244 		amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r);
2245 		goto init_failed;
2246 	}
2247 
2248 	r = amdgpu_ucode_create_bo(adev); /* create ucode bo when sw_init complete*/
2249 	if (r)
2250 		goto init_failed;
2251 
2252 	r = amdgpu_device_ip_hw_init_phase1(adev);
2253 	if (r)
2254 		goto init_failed;
2255 
2256 	r = amdgpu_device_fw_loading(adev);
2257 	if (r)
2258 		goto init_failed;
2259 
2260 	r = amdgpu_device_ip_hw_init_phase2(adev);
2261 	if (r)
2262 		goto init_failed;
2263 
2264 	/*
2265 	 * retired pages will be loaded from eeprom and reserved here,
2266 	 * it should be called after amdgpu_device_ip_hw_init_phase2  since
2267 	 * for some ASICs the RAS EEPROM code relies on SMU fully functioning
2268 	 * for I2C communication which only true at this point.
2269 	 *
2270 	 * amdgpu_ras_recovery_init may fail, but the upper only cares the
2271 	 * failure from bad gpu situation and stop amdgpu init process
2272 	 * accordingly. For other failed cases, it will still release all
2273 	 * the resource and print error message, rather than returning one
2274 	 * negative value to upper level.
2275 	 *
2276 	 * Note: theoretically, this should be called before all vram allocations
2277 	 * to protect retired page from abusing
2278 	 */
2279 	r = amdgpu_ras_recovery_init(adev);
2280 	if (r)
2281 		goto init_failed;
2282 
2283 	if (adev->gmc.xgmi.num_physical_nodes > 1)
2284 		amdgpu_xgmi_add_device(adev);
2285 	amdgpu_amdkfd_device_init(adev);
2286 
2287 	amdgpu_fru_get_product_info(adev);
2288 
2289 init_failed:
2290 	if (amdgpu_sriov_vf(adev))
2291 		amdgpu_virt_release_full_gpu(adev, true);
2292 
2293 	return r;
2294 }
2295 
2296 /**
2297  * amdgpu_device_fill_reset_magic - writes reset magic to gart pointer
2298  *
2299  * @adev: amdgpu_device pointer
2300  *
2301  * Writes a reset magic value to the gart pointer in VRAM.  The driver calls
2302  * this function before a GPU reset.  If the value is retained after a
2303  * GPU reset, VRAM has not been lost.  Some GPU resets may destry VRAM contents.
2304  */
2305 static void amdgpu_device_fill_reset_magic(struct amdgpu_device *adev)
2306 {
2307 	memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM);
2308 }
2309 
2310 /**
2311  * amdgpu_device_check_vram_lost - check if vram is valid
2312  *
2313  * @adev: amdgpu_device pointer
2314  *
2315  * Checks the reset magic value written to the gart pointer in VRAM.
2316  * The driver calls this after a GPU reset to see if the contents of
2317  * VRAM is lost or now.
2318  * returns true if vram is lost, false if not.
2319  */
2320 static bool amdgpu_device_check_vram_lost(struct amdgpu_device *adev)
2321 {
2322 	if (memcmp(adev->gart.ptr, adev->reset_magic,
2323 			AMDGPU_RESET_MAGIC_NUM))
2324 		return true;
2325 
2326 	if (!amdgpu_in_reset(adev))
2327 		return false;
2328 
2329 	/*
2330 	 * For all ASICs with baco/mode1 reset, the VRAM is
2331 	 * always assumed to be lost.
2332 	 */
2333 	switch (amdgpu_asic_reset_method(adev)) {
2334 	case AMD_RESET_METHOD_BACO:
2335 	case AMD_RESET_METHOD_MODE1:
2336 		return true;
2337 	default:
2338 		return false;
2339 	}
2340 }
2341 
2342 /**
2343  * amdgpu_device_set_cg_state - set clockgating for amdgpu device
2344  *
2345  * @adev: amdgpu_device pointer
2346  * @state: clockgating state (gate or ungate)
2347  *
2348  * The list of all the hardware IPs that make up the asic is walked and the
2349  * set_clockgating_state callbacks are run.
2350  * Late initialization pass enabling clockgating for hardware IPs.
2351  * Fini or suspend, pass disabling clockgating for hardware IPs.
2352  * Returns 0 on success, negative error code on failure.
2353  */
2354 
2355 static int amdgpu_device_set_cg_state(struct amdgpu_device *adev,
2356 						enum amd_clockgating_state state)
2357 {
2358 	int i, j, r;
2359 
2360 	if (amdgpu_emu_mode == 1)
2361 		return 0;
2362 
2363 	for (j = 0; j < adev->num_ip_blocks; j++) {
2364 		i = state == AMD_CG_STATE_GATE ? j : adev->num_ip_blocks - j - 1;
2365 		if (!adev->ip_blocks[i].status.late_initialized)
2366 			continue;
2367 		/* skip CG for VCE/UVD, it's handled specially */
2368 		if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
2369 		    adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
2370 		    adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCN &&
2371 		    adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_JPEG &&
2372 		    adev->ip_blocks[i].version->funcs->set_clockgating_state) {
2373 			/* enable clockgating to save power */
2374 			r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
2375 										     state);
2376 			if (r) {
2377 				DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
2378 					  adev->ip_blocks[i].version->funcs->name, r);
2379 				return r;
2380 			}
2381 		}
2382 	}
2383 
2384 	return 0;
2385 }
2386 
2387 static int amdgpu_device_set_pg_state(struct amdgpu_device *adev, enum amd_powergating_state state)
2388 {
2389 	int i, j, r;
2390 
2391 	if (amdgpu_emu_mode == 1)
2392 		return 0;
2393 
2394 	for (j = 0; j < adev->num_ip_blocks; j++) {
2395 		i = state == AMD_PG_STATE_GATE ? j : adev->num_ip_blocks - j - 1;
2396 		if (!adev->ip_blocks[i].status.late_initialized)
2397 			continue;
2398 		/* skip CG for VCE/UVD, it's handled specially */
2399 		if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
2400 		    adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
2401 		    adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCN &&
2402 		    adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_JPEG &&
2403 		    adev->ip_blocks[i].version->funcs->set_powergating_state) {
2404 			/* enable powergating to save power */
2405 			r = adev->ip_blocks[i].version->funcs->set_powergating_state((void *)adev,
2406 											state);
2407 			if (r) {
2408 				DRM_ERROR("set_powergating_state(gate) of IP block <%s> failed %d\n",
2409 					  adev->ip_blocks[i].version->funcs->name, r);
2410 				return r;
2411 			}
2412 		}
2413 	}
2414 	return 0;
2415 }
2416 
2417 static int amdgpu_device_enable_mgpu_fan_boost(void)
2418 {
2419 	struct amdgpu_gpu_instance *gpu_ins;
2420 	struct amdgpu_device *adev;
2421 	int i, ret = 0;
2422 
2423 	mutex_lock(&mgpu_info.mutex);
2424 
2425 	/*
2426 	 * MGPU fan boost feature should be enabled
2427 	 * only when there are two or more dGPUs in
2428 	 * the system
2429 	 */
2430 	if (mgpu_info.num_dgpu < 2)
2431 		goto out;
2432 
2433 	for (i = 0; i < mgpu_info.num_dgpu; i++) {
2434 		gpu_ins = &(mgpu_info.gpu_ins[i]);
2435 		adev = gpu_ins->adev;
2436 		if (!(adev->flags & AMD_IS_APU) &&
2437 		    !gpu_ins->mgpu_fan_enabled) {
2438 			ret = amdgpu_dpm_enable_mgpu_fan_boost(adev);
2439 			if (ret)
2440 				break;
2441 
2442 			gpu_ins->mgpu_fan_enabled = 1;
2443 		}
2444 	}
2445 
2446 out:
2447 	mutex_unlock(&mgpu_info.mutex);
2448 
2449 	return ret;
2450 }
2451 
2452 /**
2453  * amdgpu_device_ip_late_init - run late init for hardware IPs
2454  *
2455  * @adev: amdgpu_device pointer
2456  *
2457  * Late initialization pass for hardware IPs.  The list of all the hardware
2458  * IPs that make up the asic is walked and the late_init callbacks are run.
2459  * late_init covers any special initialization that an IP requires
2460  * after all of the have been initialized or something that needs to happen
2461  * late in the init process.
2462  * Returns 0 on success, negative error code on failure.
2463  */
2464 static int amdgpu_device_ip_late_init(struct amdgpu_device *adev)
2465 {
2466 	struct amdgpu_gpu_instance *gpu_instance;
2467 	int i = 0, r;
2468 
2469 	for (i = 0; i < adev->num_ip_blocks; i++) {
2470 		if (!adev->ip_blocks[i].status.hw)
2471 			continue;
2472 		if (adev->ip_blocks[i].version->funcs->late_init) {
2473 			r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
2474 			if (r) {
2475 				DRM_ERROR("late_init of IP block <%s> failed %d\n",
2476 					  adev->ip_blocks[i].version->funcs->name, r);
2477 				return r;
2478 			}
2479 		}
2480 		adev->ip_blocks[i].status.late_initialized = true;
2481 	}
2482 
2483 	amdgpu_ras_set_error_query_ready(adev, true);
2484 
2485 	amdgpu_device_set_cg_state(adev, AMD_CG_STATE_GATE);
2486 	amdgpu_device_set_pg_state(adev, AMD_PG_STATE_GATE);
2487 
2488 	amdgpu_device_fill_reset_magic(adev);
2489 
2490 	r = amdgpu_device_enable_mgpu_fan_boost();
2491 	if (r)
2492 		DRM_ERROR("enable mgpu fan boost failed (%d).\n", r);
2493 
2494 
2495 	if (adev->gmc.xgmi.num_physical_nodes > 1) {
2496 		mutex_lock(&mgpu_info.mutex);
2497 
2498 		/*
2499 		 * Reset device p-state to low as this was booted with high.
2500 		 *
2501 		 * This should be performed only after all devices from the same
2502 		 * hive get initialized.
2503 		 *
2504 		 * However, it's unknown how many device in the hive in advance.
2505 		 * As this is counted one by one during devices initializations.
2506 		 *
2507 		 * So, we wait for all XGMI interlinked devices initialized.
2508 		 * This may bring some delays as those devices may come from
2509 		 * different hives. But that should be OK.
2510 		 */
2511 		if (mgpu_info.num_dgpu == adev->gmc.xgmi.num_physical_nodes) {
2512 			for (i = 0; i < mgpu_info.num_gpu; i++) {
2513 				gpu_instance = &(mgpu_info.gpu_ins[i]);
2514 				if (gpu_instance->adev->flags & AMD_IS_APU)
2515 					continue;
2516 
2517 				r = amdgpu_xgmi_set_pstate(gpu_instance->adev,
2518 						AMDGPU_XGMI_PSTATE_MIN);
2519 				if (r) {
2520 					DRM_ERROR("pstate setting failed (%d).\n", r);
2521 					break;
2522 				}
2523 			}
2524 		}
2525 
2526 		mutex_unlock(&mgpu_info.mutex);
2527 	}
2528 
2529 	return 0;
2530 }
2531 
2532 /**
2533  * amdgpu_device_ip_fini - run fini for hardware IPs
2534  *
2535  * @adev: amdgpu_device pointer
2536  *
2537  * Main teardown pass for hardware IPs.  The list of all the hardware
2538  * IPs that make up the asic is walked and the hw_fini and sw_fini callbacks
2539  * are run.  hw_fini tears down the hardware associated with each IP
2540  * and sw_fini tears down any software state associated with each IP.
2541  * Returns 0 on success, negative error code on failure.
2542  */
2543 static int amdgpu_device_ip_fini(struct amdgpu_device *adev)
2544 {
2545 	int i, r;
2546 
2547 	if (amdgpu_sriov_vf(adev) && adev->virt.ras_init_done)
2548 		amdgpu_virt_release_ras_err_handler_data(adev);
2549 
2550 	amdgpu_ras_pre_fini(adev);
2551 
2552 	if (adev->gmc.xgmi.num_physical_nodes > 1)
2553 		amdgpu_xgmi_remove_device(adev);
2554 
2555 	amdgpu_amdkfd_device_fini(adev);
2556 
2557 	amdgpu_device_set_pg_state(adev, AMD_PG_STATE_UNGATE);
2558 	amdgpu_device_set_cg_state(adev, AMD_CG_STATE_UNGATE);
2559 
2560 	/* need to disable SMC first */
2561 	for (i = 0; i < adev->num_ip_blocks; i++) {
2562 		if (!adev->ip_blocks[i].status.hw)
2563 			continue;
2564 		if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
2565 			r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
2566 			/* XXX handle errors */
2567 			if (r) {
2568 				DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
2569 					  adev->ip_blocks[i].version->funcs->name, r);
2570 			}
2571 			adev->ip_blocks[i].status.hw = false;
2572 			break;
2573 		}
2574 	}
2575 
2576 	for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2577 		if (!adev->ip_blocks[i].status.hw)
2578 			continue;
2579 
2580 		r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
2581 		/* XXX handle errors */
2582 		if (r) {
2583 			DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
2584 				  adev->ip_blocks[i].version->funcs->name, r);
2585 		}
2586 
2587 		adev->ip_blocks[i].status.hw = false;
2588 	}
2589 
2590 
2591 	for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2592 		if (!adev->ip_blocks[i].status.sw)
2593 			continue;
2594 
2595 		if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
2596 			amdgpu_ucode_free_bo(adev);
2597 			amdgpu_free_static_csa(&adev->virt.csa_obj);
2598 			amdgpu_device_wb_fini(adev);
2599 			amdgpu_device_vram_scratch_fini(adev);
2600 			amdgpu_ib_pool_fini(adev);
2601 		}
2602 
2603 		r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
2604 		/* XXX handle errors */
2605 		if (r) {
2606 			DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
2607 				  adev->ip_blocks[i].version->funcs->name, r);
2608 		}
2609 		adev->ip_blocks[i].status.sw = false;
2610 		adev->ip_blocks[i].status.valid = false;
2611 	}
2612 
2613 	for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2614 		if (!adev->ip_blocks[i].status.late_initialized)
2615 			continue;
2616 		if (adev->ip_blocks[i].version->funcs->late_fini)
2617 			adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
2618 		adev->ip_blocks[i].status.late_initialized = false;
2619 	}
2620 
2621 	amdgpu_ras_fini(adev);
2622 
2623 	if (amdgpu_sriov_vf(adev))
2624 		if (amdgpu_virt_release_full_gpu(adev, false))
2625 			DRM_ERROR("failed to release exclusive mode on fini\n");
2626 
2627 	return 0;
2628 }
2629 
2630 /**
2631  * amdgpu_device_delayed_init_work_handler - work handler for IB tests
2632  *
2633  * @work: work_struct.
2634  */
2635 static void amdgpu_device_delayed_init_work_handler(struct work_struct *work)
2636 {
2637 	struct amdgpu_device *adev =
2638 		container_of(work, struct amdgpu_device, delayed_init_work.work);
2639 	int r;
2640 
2641 	r = amdgpu_ib_ring_tests(adev);
2642 	if (r)
2643 		DRM_ERROR("ib ring test failed (%d).\n", r);
2644 }
2645 
2646 static void amdgpu_device_delay_enable_gfx_off(struct work_struct *work)
2647 {
2648 	struct amdgpu_device *adev =
2649 		container_of(work, struct amdgpu_device, gfx.gfx_off_delay_work.work);
2650 
2651 	mutex_lock(&adev->gfx.gfx_off_mutex);
2652 	if (!adev->gfx.gfx_off_state && !adev->gfx.gfx_off_req_count) {
2653 		if (!amdgpu_dpm_set_powergating_by_smu(adev, AMD_IP_BLOCK_TYPE_GFX, true))
2654 			adev->gfx.gfx_off_state = true;
2655 	}
2656 	mutex_unlock(&adev->gfx.gfx_off_mutex);
2657 }
2658 
2659 /**
2660  * amdgpu_device_ip_suspend_phase1 - run suspend for hardware IPs (phase 1)
2661  *
2662  * @adev: amdgpu_device pointer
2663  *
2664  * Main suspend function for hardware IPs.  The list of all the hardware
2665  * IPs that make up the asic is walked, clockgating is disabled and the
2666  * suspend callbacks are run.  suspend puts the hardware and software state
2667  * in each IP into a state suitable for suspend.
2668  * Returns 0 on success, negative error code on failure.
2669  */
2670 static int amdgpu_device_ip_suspend_phase1(struct amdgpu_device *adev)
2671 {
2672 	int i, r;
2673 
2674 	if (!amdgpu_acpi_is_s0ix_supported(adev) || amdgpu_in_reset(adev)) {
2675 		amdgpu_device_set_pg_state(adev, AMD_PG_STATE_UNGATE);
2676 		amdgpu_device_set_cg_state(adev, AMD_CG_STATE_UNGATE);
2677 	}
2678 
2679 	for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2680 		if (!adev->ip_blocks[i].status.valid)
2681 			continue;
2682 
2683 		/* displays are handled separately */
2684 		if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_DCE)
2685 			continue;
2686 
2687 		/* XXX handle errors */
2688 		r = adev->ip_blocks[i].version->funcs->suspend(adev);
2689 		/* XXX handle errors */
2690 		if (r) {
2691 			DRM_ERROR("suspend of IP block <%s> failed %d\n",
2692 				  adev->ip_blocks[i].version->funcs->name, r);
2693 			return r;
2694 		}
2695 
2696 		adev->ip_blocks[i].status.hw = false;
2697 	}
2698 
2699 	return 0;
2700 }
2701 
2702 /**
2703  * amdgpu_device_ip_suspend_phase2 - run suspend for hardware IPs (phase 2)
2704  *
2705  * @adev: amdgpu_device pointer
2706  *
2707  * Main suspend function for hardware IPs.  The list of all the hardware
2708  * IPs that make up the asic is walked, clockgating is disabled and the
2709  * suspend callbacks are run.  suspend puts the hardware and software state
2710  * in each IP into a state suitable for suspend.
2711  * Returns 0 on success, negative error code on failure.
2712  */
2713 static int amdgpu_device_ip_suspend_phase2(struct amdgpu_device *adev)
2714 {
2715 	int i, r;
2716 
2717 	for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2718 		if (!adev->ip_blocks[i].status.valid)
2719 			continue;
2720 		/* displays are handled in phase1 */
2721 		if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE)
2722 			continue;
2723 		/* PSP lost connection when err_event_athub occurs */
2724 		if (amdgpu_ras_intr_triggered() &&
2725 		    adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
2726 			adev->ip_blocks[i].status.hw = false;
2727 			continue;
2728 		}
2729 		/* XXX handle errors */
2730 		r = adev->ip_blocks[i].version->funcs->suspend(adev);
2731 		/* XXX handle errors */
2732 		if (r) {
2733 			DRM_ERROR("suspend of IP block <%s> failed %d\n",
2734 				  adev->ip_blocks[i].version->funcs->name, r);
2735 		}
2736 		adev->ip_blocks[i].status.hw = false;
2737 		/* handle putting the SMC in the appropriate state */
2738 		if(!amdgpu_sriov_vf(adev)){
2739 			if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
2740 				r = amdgpu_dpm_set_mp1_state(adev, adev->mp1_state);
2741 				if (r) {
2742 					DRM_ERROR("SMC failed to set mp1 state %d, %d\n",
2743 							adev->mp1_state, r);
2744 					return r;
2745 				}
2746 			}
2747 		}
2748 		adev->ip_blocks[i].status.hw = false;
2749 	}
2750 
2751 	return 0;
2752 }
2753 
2754 /**
2755  * amdgpu_device_ip_suspend - run suspend for hardware IPs
2756  *
2757  * @adev: amdgpu_device pointer
2758  *
2759  * Main suspend function for hardware IPs.  The list of all the hardware
2760  * IPs that make up the asic is walked, clockgating is disabled and the
2761  * suspend callbacks are run.  suspend puts the hardware and software state
2762  * in each IP into a state suitable for suspend.
2763  * Returns 0 on success, negative error code on failure.
2764  */
2765 int amdgpu_device_ip_suspend(struct amdgpu_device *adev)
2766 {
2767 	int r;
2768 
2769 	if (amdgpu_sriov_vf(adev))
2770 		amdgpu_virt_request_full_gpu(adev, false);
2771 
2772 	r = amdgpu_device_ip_suspend_phase1(adev);
2773 	if (r)
2774 		return r;
2775 	r = amdgpu_device_ip_suspend_phase2(adev);
2776 
2777 	if (amdgpu_sriov_vf(adev))
2778 		amdgpu_virt_release_full_gpu(adev, false);
2779 
2780 	return r;
2781 }
2782 
2783 static int amdgpu_device_ip_reinit_early_sriov(struct amdgpu_device *adev)
2784 {
2785 	int i, r;
2786 
2787 	static enum amd_ip_block_type ip_order[] = {
2788 		AMD_IP_BLOCK_TYPE_GMC,
2789 		AMD_IP_BLOCK_TYPE_COMMON,
2790 		AMD_IP_BLOCK_TYPE_PSP,
2791 		AMD_IP_BLOCK_TYPE_IH,
2792 	};
2793 
2794 	for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
2795 		int j;
2796 		struct amdgpu_ip_block *block;
2797 
2798 		block = &adev->ip_blocks[i];
2799 		block->status.hw = false;
2800 
2801 		for (j = 0; j < ARRAY_SIZE(ip_order); j++) {
2802 
2803 			if (block->version->type != ip_order[j] ||
2804 				!block->status.valid)
2805 				continue;
2806 
2807 			r = block->version->funcs->hw_init(adev);
2808 			DRM_INFO("RE-INIT-early: %s %s\n", block->version->funcs->name, r?"failed":"succeeded");
2809 			if (r)
2810 				return r;
2811 			block->status.hw = true;
2812 		}
2813 	}
2814 
2815 	return 0;
2816 }
2817 
2818 static int amdgpu_device_ip_reinit_late_sriov(struct amdgpu_device *adev)
2819 {
2820 	int i, r;
2821 
2822 	static enum amd_ip_block_type ip_order[] = {
2823 		AMD_IP_BLOCK_TYPE_SMC,
2824 		AMD_IP_BLOCK_TYPE_DCE,
2825 		AMD_IP_BLOCK_TYPE_GFX,
2826 		AMD_IP_BLOCK_TYPE_SDMA,
2827 		AMD_IP_BLOCK_TYPE_UVD,
2828 		AMD_IP_BLOCK_TYPE_VCE,
2829 		AMD_IP_BLOCK_TYPE_VCN
2830 	};
2831 
2832 	for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
2833 		int j;
2834 		struct amdgpu_ip_block *block;
2835 
2836 		for (j = 0; j < adev->num_ip_blocks; j++) {
2837 			block = &adev->ip_blocks[j];
2838 
2839 			if (block->version->type != ip_order[i] ||
2840 				!block->status.valid ||
2841 				block->status.hw)
2842 				continue;
2843 
2844 			if (block->version->type == AMD_IP_BLOCK_TYPE_SMC)
2845 				r = block->version->funcs->resume(adev);
2846 			else
2847 				r = block->version->funcs->hw_init(adev);
2848 
2849 			DRM_INFO("RE-INIT-late: %s %s\n", block->version->funcs->name, r?"failed":"succeeded");
2850 			if (r)
2851 				return r;
2852 			block->status.hw = true;
2853 		}
2854 	}
2855 
2856 	return 0;
2857 }
2858 
2859 /**
2860  * amdgpu_device_ip_resume_phase1 - run resume for hardware IPs
2861  *
2862  * @adev: amdgpu_device pointer
2863  *
2864  * First resume function for hardware IPs.  The list of all the hardware
2865  * IPs that make up the asic is walked and the resume callbacks are run for
2866  * COMMON, GMC, and IH.  resume puts the hardware into a functional state
2867  * after a suspend and updates the software state as necessary.  This
2868  * function is also used for restoring the GPU after a GPU reset.
2869  * Returns 0 on success, negative error code on failure.
2870  */
2871 static int amdgpu_device_ip_resume_phase1(struct amdgpu_device *adev)
2872 {
2873 	int i, r;
2874 
2875 	for (i = 0; i < adev->num_ip_blocks; i++) {
2876 		if (!adev->ip_blocks[i].status.valid || adev->ip_blocks[i].status.hw)
2877 			continue;
2878 		if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
2879 		    adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
2880 		    adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH) {
2881 
2882 			r = adev->ip_blocks[i].version->funcs->resume(adev);
2883 			if (r) {
2884 				DRM_ERROR("resume of IP block <%s> failed %d\n",
2885 					  adev->ip_blocks[i].version->funcs->name, r);
2886 				return r;
2887 			}
2888 			adev->ip_blocks[i].status.hw = true;
2889 		}
2890 	}
2891 
2892 	return 0;
2893 }
2894 
2895 /**
2896  * amdgpu_device_ip_resume_phase2 - run resume for hardware IPs
2897  *
2898  * @adev: amdgpu_device pointer
2899  *
2900  * First resume function for hardware IPs.  The list of all the hardware
2901  * IPs that make up the asic is walked and the resume callbacks are run for
2902  * all blocks except COMMON, GMC, and IH.  resume puts the hardware into a
2903  * functional state after a suspend and updates the software state as
2904  * necessary.  This function is also used for restoring the GPU after a GPU
2905  * reset.
2906  * Returns 0 on success, negative error code on failure.
2907  */
2908 static int amdgpu_device_ip_resume_phase2(struct amdgpu_device *adev)
2909 {
2910 	int i, r;
2911 
2912 	for (i = 0; i < adev->num_ip_blocks; i++) {
2913 		if (!adev->ip_blocks[i].status.valid || adev->ip_blocks[i].status.hw)
2914 			continue;
2915 		if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
2916 		    adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
2917 		    adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH ||
2918 		    adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP)
2919 			continue;
2920 		r = adev->ip_blocks[i].version->funcs->resume(adev);
2921 		if (r) {
2922 			DRM_ERROR("resume of IP block <%s> failed %d\n",
2923 				  adev->ip_blocks[i].version->funcs->name, r);
2924 			return r;
2925 		}
2926 		adev->ip_blocks[i].status.hw = true;
2927 	}
2928 
2929 	return 0;
2930 }
2931 
2932 /**
2933  * amdgpu_device_ip_resume - run resume for hardware IPs
2934  *
2935  * @adev: amdgpu_device pointer
2936  *
2937  * Main resume function for hardware IPs.  The hardware IPs
2938  * are split into two resume functions because they are
2939  * are also used in in recovering from a GPU reset and some additional
2940  * steps need to be take between them.  In this case (S3/S4) they are
2941  * run sequentially.
2942  * Returns 0 on success, negative error code on failure.
2943  */
2944 static int amdgpu_device_ip_resume(struct amdgpu_device *adev)
2945 {
2946 	int r;
2947 
2948 	r = amdgpu_device_ip_resume_phase1(adev);
2949 	if (r)
2950 		return r;
2951 
2952 	r = amdgpu_device_fw_loading(adev);
2953 	if (r)
2954 		return r;
2955 
2956 	r = amdgpu_device_ip_resume_phase2(adev);
2957 
2958 	return r;
2959 }
2960 
2961 /**
2962  * amdgpu_device_detect_sriov_bios - determine if the board supports SR-IOV
2963  *
2964  * @adev: amdgpu_device pointer
2965  *
2966  * Query the VBIOS data tables to determine if the board supports SR-IOV.
2967  */
2968 static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
2969 {
2970 	if (amdgpu_sriov_vf(adev)) {
2971 		if (adev->is_atom_fw) {
2972 			if (amdgpu_atomfirmware_gpu_supports_virtualization(adev))
2973 				adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
2974 		} else {
2975 			if (amdgpu_atombios_has_gpu_virtualization_table(adev))
2976 				adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
2977 		}
2978 
2979 		if (!(adev->virt.caps & AMDGPU_SRIOV_CAPS_SRIOV_VBIOS))
2980 			amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_NO_VBIOS, 0, 0);
2981 	}
2982 }
2983 
2984 /**
2985  * amdgpu_device_asic_has_dc_support - determine if DC supports the asic
2986  *
2987  * @asic_type: AMD asic type
2988  *
2989  * Check if there is DC (new modesetting infrastructre) support for an asic.
2990  * returns true if DC has support, false if not.
2991  */
2992 bool amdgpu_device_asic_has_dc_support(enum amd_asic_type asic_type)
2993 {
2994 	switch (asic_type) {
2995 #if defined(CONFIG_DRM_AMD_DC)
2996 #if defined(CONFIG_DRM_AMD_DC_SI)
2997 	case CHIP_TAHITI:
2998 	case CHIP_PITCAIRN:
2999 	case CHIP_VERDE:
3000 	case CHIP_OLAND:
3001 #endif
3002 	case CHIP_BONAIRE:
3003 	case CHIP_KAVERI:
3004 	case CHIP_KABINI:
3005 	case CHIP_MULLINS:
3006 		/*
3007 		 * We have systems in the wild with these ASICs that require
3008 		 * LVDS and VGA support which is not supported with DC.
3009 		 *
3010 		 * Fallback to the non-DC driver here by default so as not to
3011 		 * cause regressions.
3012 		 */
3013 		return amdgpu_dc > 0;
3014 	case CHIP_HAWAII:
3015 	case CHIP_CARRIZO:
3016 	case CHIP_STONEY:
3017 	case CHIP_POLARIS10:
3018 	case CHIP_POLARIS11:
3019 	case CHIP_POLARIS12:
3020 	case CHIP_VEGAM:
3021 	case CHIP_TONGA:
3022 	case CHIP_FIJI:
3023 	case CHIP_VEGA10:
3024 	case CHIP_VEGA12:
3025 	case CHIP_VEGA20:
3026 #if defined(CONFIG_DRM_AMD_DC_DCN)
3027 	case CHIP_RAVEN:
3028 	case CHIP_NAVI10:
3029 	case CHIP_NAVI14:
3030 	case CHIP_NAVI12:
3031 	case CHIP_RENOIR:
3032 	case CHIP_SIENNA_CICHLID:
3033 	case CHIP_NAVY_FLOUNDER:
3034 	case CHIP_DIMGREY_CAVEFISH:
3035 	case CHIP_VANGOGH:
3036 #endif
3037 		return amdgpu_dc != 0;
3038 #endif
3039 	default:
3040 		if (amdgpu_dc > 0)
3041 			DRM_INFO("Display Core has been requested via kernel parameter "
3042 					 "but isn't supported by ASIC, ignoring\n");
3043 		return false;
3044 	}
3045 }
3046 
3047 /**
3048  * amdgpu_device_has_dc_support - check if dc is supported
3049  *
3050  * @adev: amdgpu_device pointer
3051  *
3052  * Returns true for supported, false for not supported
3053  */
3054 bool amdgpu_device_has_dc_support(struct amdgpu_device *adev)
3055 {
3056 	if (amdgpu_sriov_vf(adev) || adev->enable_virtual_display)
3057 		return false;
3058 
3059 	return amdgpu_device_asic_has_dc_support(adev->asic_type);
3060 }
3061 
3062 
3063 static void amdgpu_device_xgmi_reset_func(struct work_struct *__work)
3064 {
3065 	struct amdgpu_device *adev =
3066 		container_of(__work, struct amdgpu_device, xgmi_reset_work);
3067 	struct amdgpu_hive_info *hive = amdgpu_get_xgmi_hive(adev);
3068 
3069 	/* It's a bug to not have a hive within this function */
3070 	if (WARN_ON(!hive))
3071 		return;
3072 
3073 	/*
3074 	 * Use task barrier to synchronize all xgmi reset works across the
3075 	 * hive. task_barrier_enter and task_barrier_exit will block
3076 	 * until all the threads running the xgmi reset works reach
3077 	 * those points. task_barrier_full will do both blocks.
3078 	 */
3079 	if (amdgpu_asic_reset_method(adev) == AMD_RESET_METHOD_BACO) {
3080 
3081 		task_barrier_enter(&hive->tb);
3082 		adev->asic_reset_res = amdgpu_device_baco_enter(adev_to_drm(adev));
3083 
3084 		if (adev->asic_reset_res)
3085 			goto fail;
3086 
3087 		task_barrier_exit(&hive->tb);
3088 		adev->asic_reset_res = amdgpu_device_baco_exit(adev_to_drm(adev));
3089 
3090 		if (adev->asic_reset_res)
3091 			goto fail;
3092 
3093 		if (adev->mmhub.funcs && adev->mmhub.funcs->reset_ras_error_count)
3094 			adev->mmhub.funcs->reset_ras_error_count(adev);
3095 	} else {
3096 
3097 		task_barrier_full(&hive->tb);
3098 		adev->asic_reset_res =  amdgpu_asic_reset(adev);
3099 	}
3100 
3101 fail:
3102 	if (adev->asic_reset_res)
3103 		DRM_WARN("ASIC reset failed with error, %d for drm dev, %s",
3104 			 adev->asic_reset_res, adev_to_drm(adev)->unique);
3105 	amdgpu_put_xgmi_hive(hive);
3106 }
3107 
3108 static int amdgpu_device_get_job_timeout_settings(struct amdgpu_device *adev)
3109 {
3110 	char *input = amdgpu_lockup_timeout;
3111 	char *timeout_setting = NULL;
3112 	int index = 0;
3113 	long timeout;
3114 	int ret = 0;
3115 
3116 	/*
3117 	 * By default timeout for non compute jobs is 10000.
3118 	 * And there is no timeout enforced on compute jobs.
3119 	 * In SR-IOV or passthrough mode, timeout for compute
3120 	 * jobs are 60000 by default.
3121 	 */
3122 	adev->gfx_timeout = msecs_to_jiffies(10000);
3123 	adev->sdma_timeout = adev->video_timeout = adev->gfx_timeout;
3124 	if (amdgpu_sriov_vf(adev) || amdgpu_passthrough(adev))
3125 		adev->compute_timeout =  msecs_to_jiffies(60000);
3126 	else
3127 		adev->compute_timeout = MAX_SCHEDULE_TIMEOUT;
3128 
3129 	if (strnlen(input, AMDGPU_MAX_TIMEOUT_PARAM_LENGTH)) {
3130 		while ((timeout_setting = strsep(&input, ",")) &&
3131 				strnlen(timeout_setting, AMDGPU_MAX_TIMEOUT_PARAM_LENGTH)) {
3132 			ret = kstrtol(timeout_setting, 0, &timeout);
3133 			if (ret)
3134 				return ret;
3135 
3136 			if (timeout == 0) {
3137 				index++;
3138 				continue;
3139 			} else if (timeout < 0) {
3140 				timeout = MAX_SCHEDULE_TIMEOUT;
3141 			} else {
3142 				timeout = msecs_to_jiffies(timeout);
3143 			}
3144 
3145 			switch (index++) {
3146 			case 0:
3147 				adev->gfx_timeout = timeout;
3148 				break;
3149 			case 1:
3150 				adev->compute_timeout = timeout;
3151 				break;
3152 			case 2:
3153 				adev->sdma_timeout = timeout;
3154 				break;
3155 			case 3:
3156 				adev->video_timeout = timeout;
3157 				break;
3158 			default:
3159 				break;
3160 			}
3161 		}
3162 		/*
3163 		 * There is only one value specified and
3164 		 * it should apply to all non-compute jobs.
3165 		 */
3166 		if (index == 1) {
3167 			adev->sdma_timeout = adev->video_timeout = adev->gfx_timeout;
3168 			if (amdgpu_sriov_vf(adev) || amdgpu_passthrough(adev))
3169 				adev->compute_timeout = adev->gfx_timeout;
3170 		}
3171 	}
3172 
3173 	return ret;
3174 }
3175 
3176 static const struct attribute *amdgpu_dev_attributes[] = {
3177 	&dev_attr_product_name.attr,
3178 	&dev_attr_product_number.attr,
3179 	&dev_attr_serial_number.attr,
3180 	&dev_attr_pcie_replay_count.attr,
3181 	NULL
3182 };
3183 
3184 
3185 /**
3186  * amdgpu_device_init - initialize the driver
3187  *
3188  * @adev: amdgpu_device pointer
3189  * @flags: driver flags
3190  *
3191  * Initializes the driver info and hw (all asics).
3192  * Returns 0 for success or an error on failure.
3193  * Called at driver startup.
3194  */
3195 int amdgpu_device_init(struct amdgpu_device *adev,
3196 		       uint32_t flags)
3197 {
3198 	struct drm_device *ddev = adev_to_drm(adev);
3199 	struct pci_dev *pdev = adev->pdev;
3200 	int r, i;
3201 	bool atpx = false;
3202 	u32 max_MBps;
3203 
3204 	adev->shutdown = false;
3205 	adev->flags = flags;
3206 
3207 	if (amdgpu_force_asic_type >= 0 && amdgpu_force_asic_type < CHIP_LAST)
3208 		adev->asic_type = amdgpu_force_asic_type;
3209 	else
3210 		adev->asic_type = flags & AMD_ASIC_MASK;
3211 
3212 	adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
3213 	if (amdgpu_emu_mode == 1)
3214 		adev->usec_timeout *= 10;
3215 	adev->gmc.gart_size = 512 * 1024 * 1024;
3216 	adev->accel_working = false;
3217 	adev->num_rings = 0;
3218 	adev->mman.buffer_funcs = NULL;
3219 	adev->mman.buffer_funcs_ring = NULL;
3220 	adev->vm_manager.vm_pte_funcs = NULL;
3221 	adev->vm_manager.vm_pte_num_scheds = 0;
3222 	adev->gmc.gmc_funcs = NULL;
3223 	adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
3224 	bitmap_zero(adev->gfx.pipe_reserve_bitmap, AMDGPU_MAX_COMPUTE_QUEUES);
3225 
3226 	adev->smc_rreg = &amdgpu_invalid_rreg;
3227 	adev->smc_wreg = &amdgpu_invalid_wreg;
3228 	adev->pcie_rreg = &amdgpu_invalid_rreg;
3229 	adev->pcie_wreg = &amdgpu_invalid_wreg;
3230 	adev->pciep_rreg = &amdgpu_invalid_rreg;
3231 	adev->pciep_wreg = &amdgpu_invalid_wreg;
3232 	adev->pcie_rreg64 = &amdgpu_invalid_rreg64;
3233 	adev->pcie_wreg64 = &amdgpu_invalid_wreg64;
3234 	adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
3235 	adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
3236 	adev->didt_rreg = &amdgpu_invalid_rreg;
3237 	adev->didt_wreg = &amdgpu_invalid_wreg;
3238 	adev->gc_cac_rreg = &amdgpu_invalid_rreg;
3239 	adev->gc_cac_wreg = &amdgpu_invalid_wreg;
3240 	adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
3241 	adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
3242 
3243 	DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
3244 		 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
3245 		 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
3246 
3247 	/* mutex initialization are all done here so we
3248 	 * can recall function without having locking issues */
3249 	atomic_set(&adev->irq.ih.lock, 0);
3250 	mutex_init(&adev->firmware.mutex);
3251 	mutex_init(&adev->pm.mutex);
3252 	mutex_init(&adev->gfx.gpu_clock_mutex);
3253 	mutex_init(&adev->srbm_mutex);
3254 	mutex_init(&adev->gfx.pipe_reserve_mutex);
3255 	mutex_init(&adev->gfx.gfx_off_mutex);
3256 	mutex_init(&adev->grbm_idx_mutex);
3257 	mutex_init(&adev->mn_lock);
3258 	mutex_init(&adev->virt.vf_errors.lock);
3259 	hash_init(adev->mn_hash);
3260 	atomic_set(&adev->in_gpu_reset, 0);
3261 	init_rwsem(&adev->reset_sem);
3262 	mutex_init(&adev->psp.mutex);
3263 	mutex_init(&adev->notifier_lock);
3264 
3265 	r = amdgpu_device_check_arguments(adev);
3266 	if (r)
3267 		return r;
3268 
3269 	spin_lock_init(&adev->mmio_idx_lock);
3270 	spin_lock_init(&adev->smc_idx_lock);
3271 	spin_lock_init(&adev->pcie_idx_lock);
3272 	spin_lock_init(&adev->uvd_ctx_idx_lock);
3273 	spin_lock_init(&adev->didt_idx_lock);
3274 	spin_lock_init(&adev->gc_cac_idx_lock);
3275 	spin_lock_init(&adev->se_cac_idx_lock);
3276 	spin_lock_init(&adev->audio_endpt_idx_lock);
3277 	spin_lock_init(&adev->mm_stats.lock);
3278 
3279 	INIT_LIST_HEAD(&adev->shadow_list);
3280 	mutex_init(&adev->shadow_list_lock);
3281 
3282 	INIT_DELAYED_WORK(&adev->delayed_init_work,
3283 			  amdgpu_device_delayed_init_work_handler);
3284 	INIT_DELAYED_WORK(&adev->gfx.gfx_off_delay_work,
3285 			  amdgpu_device_delay_enable_gfx_off);
3286 
3287 	INIT_WORK(&adev->xgmi_reset_work, amdgpu_device_xgmi_reset_func);
3288 
3289 	adev->gfx.gfx_off_req_count = 1;
3290 	adev->pm.ac_power = power_supply_is_system_supplied() > 0;
3291 
3292 	atomic_set(&adev->throttling_logging_enabled, 1);
3293 	/*
3294 	 * If throttling continues, logging will be performed every minute
3295 	 * to avoid log flooding. "-1" is subtracted since the thermal
3296 	 * throttling interrupt comes every second. Thus, the total logging
3297 	 * interval is 59 seconds(retelimited printk interval) + 1(waiting
3298 	 * for throttling interrupt) = 60 seconds.
3299 	 */
3300 	ratelimit_state_init(&adev->throttling_logging_rs, (60 - 1) * HZ, 1);
3301 	ratelimit_set_flags(&adev->throttling_logging_rs, RATELIMIT_MSG_ON_RELEASE);
3302 
3303 	/* Registers mapping */
3304 	/* TODO: block userspace mapping of io register */
3305 	if (adev->asic_type >= CHIP_BONAIRE) {
3306 		adev->rmmio_base = pci_resource_start(adev->pdev, 5);
3307 		adev->rmmio_size = pci_resource_len(adev->pdev, 5);
3308 	} else {
3309 		adev->rmmio_base = pci_resource_start(adev->pdev, 2);
3310 		adev->rmmio_size = pci_resource_len(adev->pdev, 2);
3311 	}
3312 
3313 	adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
3314 	if (adev->rmmio == NULL) {
3315 		return -ENOMEM;
3316 	}
3317 	DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
3318 	DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
3319 
3320 	/* io port mapping */
3321 	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
3322 		if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
3323 			adev->rio_mem_size = pci_resource_len(adev->pdev, i);
3324 			adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
3325 			break;
3326 		}
3327 	}
3328 	if (adev->rio_mem == NULL)
3329 		DRM_INFO("PCI I/O BAR is not found.\n");
3330 
3331 	/* enable PCIE atomic ops */
3332 	r = pci_enable_atomic_ops_to_root(adev->pdev,
3333 					  PCI_EXP_DEVCAP2_ATOMIC_COMP32 |
3334 					  PCI_EXP_DEVCAP2_ATOMIC_COMP64);
3335 	if (r) {
3336 		adev->have_atomics_support = false;
3337 		DRM_INFO("PCIE atomic ops is not supported\n");
3338 	} else {
3339 		adev->have_atomics_support = true;
3340 	}
3341 
3342 	amdgpu_device_get_pcie_info(adev);
3343 
3344 	if (amdgpu_mcbp)
3345 		DRM_INFO("MCBP is enabled\n");
3346 
3347 	if (amdgpu_mes && adev->asic_type >= CHIP_NAVI10)
3348 		adev->enable_mes = true;
3349 
3350 	/* detect hw virtualization here */
3351 	amdgpu_detect_virtualization(adev);
3352 
3353 	r = amdgpu_device_get_job_timeout_settings(adev);
3354 	if (r) {
3355 		dev_err(adev->dev, "invalid lockup_timeout parameter syntax\n");
3356 		goto failed_unmap;
3357 	}
3358 
3359 	/* early init functions */
3360 	r = amdgpu_device_ip_early_init(adev);
3361 	if (r)
3362 		goto failed_unmap;
3363 
3364 	/* doorbell bar mapping and doorbell index init*/
3365 	amdgpu_device_doorbell_init(adev);
3366 
3367 	/* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
3368 	/* this will fail for cards that aren't VGA class devices, just
3369 	 * ignore it */
3370 	if ((adev->pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
3371 		vga_client_register(adev->pdev, adev, NULL, amdgpu_device_vga_set_decode);
3372 
3373 	if (amdgpu_device_supports_atpx(ddev))
3374 		atpx = true;
3375 	if (amdgpu_has_atpx() &&
3376 	    (amdgpu_is_atpx_hybrid() ||
3377 	     amdgpu_has_atpx_dgpu_power_cntl()) &&
3378 	    !pci_is_thunderbolt_attached(adev->pdev))
3379 		vga_switcheroo_register_client(adev->pdev,
3380 					       &amdgpu_switcheroo_ops, atpx);
3381 	if (atpx)
3382 		vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
3383 
3384 	if (amdgpu_emu_mode == 1) {
3385 		/* post the asic on emulation mode */
3386 		emu_soc_asic_init(adev);
3387 		goto fence_driver_init;
3388 	}
3389 
3390 	/* detect if we are with an SRIOV vbios */
3391 	amdgpu_device_detect_sriov_bios(adev);
3392 
3393 	/* check if we need to reset the asic
3394 	 *  E.g., driver was not cleanly unloaded previously, etc.
3395 	 */
3396 	if (!amdgpu_sriov_vf(adev) && amdgpu_asic_need_reset_on_init(adev)) {
3397 		r = amdgpu_asic_reset(adev);
3398 		if (r) {
3399 			dev_err(adev->dev, "asic reset on init failed\n");
3400 			goto failed;
3401 		}
3402 	}
3403 
3404 	pci_enable_pcie_error_reporting(adev->ddev.pdev);
3405 
3406 	/* Post card if necessary */
3407 	if (amdgpu_device_need_post(adev)) {
3408 		if (!adev->bios) {
3409 			dev_err(adev->dev, "no vBIOS found\n");
3410 			r = -EINVAL;
3411 			goto failed;
3412 		}
3413 		DRM_INFO("GPU posting now...\n");
3414 		r = amdgpu_device_asic_init(adev);
3415 		if (r) {
3416 			dev_err(adev->dev, "gpu post error!\n");
3417 			goto failed;
3418 		}
3419 	}
3420 
3421 	if (adev->is_atom_fw) {
3422 		/* Initialize clocks */
3423 		r = amdgpu_atomfirmware_get_clock_info(adev);
3424 		if (r) {
3425 			dev_err(adev->dev, "amdgpu_atomfirmware_get_clock_info failed\n");
3426 			amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
3427 			goto failed;
3428 		}
3429 	} else {
3430 		/* Initialize clocks */
3431 		r = amdgpu_atombios_get_clock_info(adev);
3432 		if (r) {
3433 			dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
3434 			amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
3435 			goto failed;
3436 		}
3437 		/* init i2c buses */
3438 		if (!amdgpu_device_has_dc_support(adev))
3439 			amdgpu_atombios_i2c_init(adev);
3440 	}
3441 
3442 fence_driver_init:
3443 	/* Fence driver */
3444 	r = amdgpu_fence_driver_init(adev);
3445 	if (r) {
3446 		dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
3447 		amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0);
3448 		goto failed;
3449 	}
3450 
3451 	/* init the mode config */
3452 	drm_mode_config_init(adev_to_drm(adev));
3453 
3454 	r = amdgpu_device_ip_init(adev);
3455 	if (r) {
3456 		/* failed in exclusive mode due to timeout */
3457 		if (amdgpu_sriov_vf(adev) &&
3458 		    !amdgpu_sriov_runtime(adev) &&
3459 		    amdgpu_virt_mmio_blocked(adev) &&
3460 		    !amdgpu_virt_wait_reset(adev)) {
3461 			dev_err(adev->dev, "VF exclusive mode timeout\n");
3462 			/* Don't send request since VF is inactive. */
3463 			adev->virt.caps &= ~AMDGPU_SRIOV_CAPS_RUNTIME;
3464 			adev->virt.ops = NULL;
3465 			r = -EAGAIN;
3466 			goto failed;
3467 		}
3468 		dev_err(adev->dev, "amdgpu_device_ip_init failed\n");
3469 		amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0);
3470 		goto failed;
3471 	}
3472 
3473 	dev_info(adev->dev,
3474 		"SE %d, SH per SE %d, CU per SH %d, active_cu_number %d\n",
3475 			adev->gfx.config.max_shader_engines,
3476 			adev->gfx.config.max_sh_per_se,
3477 			adev->gfx.config.max_cu_per_sh,
3478 			adev->gfx.cu_info.number);
3479 
3480 	adev->accel_working = true;
3481 
3482 	amdgpu_vm_check_compute_bug(adev);
3483 
3484 	/* Initialize the buffer migration limit. */
3485 	if (amdgpu_moverate >= 0)
3486 		max_MBps = amdgpu_moverate;
3487 	else
3488 		max_MBps = 8; /* Allow 8 MB/s. */
3489 	/* Get a log2 for easy divisions. */
3490 	adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
3491 
3492 	amdgpu_fbdev_init(adev);
3493 
3494 	r = amdgpu_pm_sysfs_init(adev);
3495 	if (r) {
3496 		adev->pm_sysfs_en = false;
3497 		DRM_ERROR("registering pm debugfs failed (%d).\n", r);
3498 	} else
3499 		adev->pm_sysfs_en = true;
3500 
3501 	r = amdgpu_ucode_sysfs_init(adev);
3502 	if (r) {
3503 		adev->ucode_sysfs_en = false;
3504 		DRM_ERROR("Creating firmware sysfs failed (%d).\n", r);
3505 	} else
3506 		adev->ucode_sysfs_en = true;
3507 
3508 	if ((amdgpu_testing & 1)) {
3509 		if (adev->accel_working)
3510 			amdgpu_test_moves(adev);
3511 		else
3512 			DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
3513 	}
3514 	if (amdgpu_benchmarking) {
3515 		if (adev->accel_working)
3516 			amdgpu_benchmark(adev, amdgpu_benchmarking);
3517 		else
3518 			DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
3519 	}
3520 
3521 	/*
3522 	 * Register gpu instance before amdgpu_device_enable_mgpu_fan_boost.
3523 	 * Otherwise the mgpu fan boost feature will be skipped due to the
3524 	 * gpu instance is counted less.
3525 	 */
3526 	amdgpu_register_gpu_instance(adev);
3527 
3528 	/* enable clockgating, etc. after ib tests, etc. since some blocks require
3529 	 * explicit gating rather than handling it automatically.
3530 	 */
3531 	r = amdgpu_device_ip_late_init(adev);
3532 	if (r) {
3533 		dev_err(adev->dev, "amdgpu_device_ip_late_init failed\n");
3534 		amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r);
3535 		goto failed;
3536 	}
3537 
3538 	/* must succeed. */
3539 	amdgpu_ras_resume(adev);
3540 
3541 	queue_delayed_work(system_wq, &adev->delayed_init_work,
3542 			   msecs_to_jiffies(AMDGPU_RESUME_MS));
3543 
3544 	if (amdgpu_sriov_vf(adev))
3545 		flush_delayed_work(&adev->delayed_init_work);
3546 
3547 	r = sysfs_create_files(&adev->dev->kobj, amdgpu_dev_attributes);
3548 	if (r)
3549 		dev_err(adev->dev, "Could not create amdgpu device attr\n");
3550 
3551 	if (IS_ENABLED(CONFIG_PERF_EVENTS))
3552 		r = amdgpu_pmu_init(adev);
3553 	if (r)
3554 		dev_err(adev->dev, "amdgpu_pmu_init failed\n");
3555 
3556 	/* Have stored pci confspace at hand for restore in sudden PCI error */
3557 	if (amdgpu_device_cache_pci_state(adev->pdev))
3558 		pci_restore_state(pdev);
3559 
3560 	return 0;
3561 
3562 failed:
3563 	amdgpu_vf_error_trans_all(adev);
3564 	if (atpx)
3565 		vga_switcheroo_fini_domain_pm_ops(adev->dev);
3566 
3567 failed_unmap:
3568 	iounmap(adev->rmmio);
3569 	adev->rmmio = NULL;
3570 
3571 	return r;
3572 }
3573 
3574 /**
3575  * amdgpu_device_fini - tear down the driver
3576  *
3577  * @adev: amdgpu_device pointer
3578  *
3579  * Tear down the driver info (all asics).
3580  * Called at driver shutdown.
3581  */
3582 void amdgpu_device_fini(struct amdgpu_device *adev)
3583 {
3584 	dev_info(adev->dev, "amdgpu: finishing device.\n");
3585 	flush_delayed_work(&adev->delayed_init_work);
3586 	adev->shutdown = true;
3587 
3588 	kfree(adev->pci_state);
3589 
3590 	/* make sure IB test finished before entering exclusive mode
3591 	 * to avoid preemption on IB test
3592 	 * */
3593 	if (amdgpu_sriov_vf(adev)) {
3594 		amdgpu_virt_request_full_gpu(adev, false);
3595 		amdgpu_virt_fini_data_exchange(adev);
3596 	}
3597 
3598 	/* disable all interrupts */
3599 	amdgpu_irq_disable_all(adev);
3600 	if (adev->mode_info.mode_config_initialized){
3601 		if (!amdgpu_device_has_dc_support(adev))
3602 			drm_helper_force_disable_all(adev_to_drm(adev));
3603 		else
3604 			drm_atomic_helper_shutdown(adev_to_drm(adev));
3605 	}
3606 	amdgpu_fence_driver_fini(adev);
3607 	if (adev->pm_sysfs_en)
3608 		amdgpu_pm_sysfs_fini(adev);
3609 	amdgpu_fbdev_fini(adev);
3610 	amdgpu_device_ip_fini(adev);
3611 	release_firmware(adev->firmware.gpu_info_fw);
3612 	adev->firmware.gpu_info_fw = NULL;
3613 	adev->accel_working = false;
3614 	/* free i2c buses */
3615 	if (!amdgpu_device_has_dc_support(adev))
3616 		amdgpu_i2c_fini(adev);
3617 
3618 	if (amdgpu_emu_mode != 1)
3619 		amdgpu_atombios_fini(adev);
3620 
3621 	kfree(adev->bios);
3622 	adev->bios = NULL;
3623 	if (amdgpu_has_atpx() &&
3624 	    (amdgpu_is_atpx_hybrid() ||
3625 	     amdgpu_has_atpx_dgpu_power_cntl()) &&
3626 	    !pci_is_thunderbolt_attached(adev->pdev))
3627 		vga_switcheroo_unregister_client(adev->pdev);
3628 	if (amdgpu_device_supports_atpx(adev_to_drm(adev)))
3629 		vga_switcheroo_fini_domain_pm_ops(adev->dev);
3630 	if ((adev->pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
3631 		vga_client_register(adev->pdev, NULL, NULL, NULL);
3632 	if (adev->rio_mem)
3633 		pci_iounmap(adev->pdev, adev->rio_mem);
3634 	adev->rio_mem = NULL;
3635 	iounmap(adev->rmmio);
3636 	adev->rmmio = NULL;
3637 	amdgpu_device_doorbell_fini(adev);
3638 
3639 	if (adev->ucode_sysfs_en)
3640 		amdgpu_ucode_sysfs_fini(adev);
3641 
3642 	sysfs_remove_files(&adev->dev->kobj, amdgpu_dev_attributes);
3643 	if (IS_ENABLED(CONFIG_PERF_EVENTS))
3644 		amdgpu_pmu_fini(adev);
3645 	if (adev->mman.discovery_bin)
3646 		amdgpu_discovery_fini(adev);
3647 }
3648 
3649 
3650 /*
3651  * Suspend & resume.
3652  */
3653 /**
3654  * amdgpu_device_suspend - initiate device suspend
3655  *
3656  * @dev: drm dev pointer
3657  * @fbcon : notify the fbdev of suspend
3658  *
3659  * Puts the hw in the suspend state (all asics).
3660  * Returns 0 for success or an error on failure.
3661  * Called at driver suspend.
3662  */
3663 int amdgpu_device_suspend(struct drm_device *dev, bool fbcon)
3664 {
3665 	struct amdgpu_device *adev;
3666 	struct drm_crtc *crtc;
3667 	struct drm_connector *connector;
3668 	struct drm_connector_list_iter iter;
3669 	int r;
3670 
3671 	adev = drm_to_adev(dev);
3672 
3673 	if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
3674 		return 0;
3675 
3676 	adev->in_suspend = true;
3677 	drm_kms_helper_poll_disable(dev);
3678 
3679 	if (fbcon)
3680 		amdgpu_fbdev_set_suspend(adev, 1);
3681 
3682 	cancel_delayed_work_sync(&adev->delayed_init_work);
3683 
3684 	if (!amdgpu_device_has_dc_support(adev)) {
3685 		/* turn off display hw */
3686 		drm_modeset_lock_all(dev);
3687 		drm_connector_list_iter_begin(dev, &iter);
3688 		drm_for_each_connector_iter(connector, &iter)
3689 			drm_helper_connector_dpms(connector,
3690 						  DRM_MODE_DPMS_OFF);
3691 		drm_connector_list_iter_end(&iter);
3692 		drm_modeset_unlock_all(dev);
3693 			/* unpin the front buffers and cursors */
3694 		list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
3695 			struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
3696 			struct drm_framebuffer *fb = crtc->primary->fb;
3697 			struct amdgpu_bo *robj;
3698 
3699 			if (amdgpu_crtc->cursor_bo && !adev->enable_virtual_display) {
3700 				struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
3701 				r = amdgpu_bo_reserve(aobj, true);
3702 				if (r == 0) {
3703 					amdgpu_bo_unpin(aobj);
3704 					amdgpu_bo_unreserve(aobj);
3705 				}
3706 			}
3707 
3708 			if (fb == NULL || fb->obj[0] == NULL) {
3709 				continue;
3710 			}
3711 			robj = gem_to_amdgpu_bo(fb->obj[0]);
3712 			/* don't unpin kernel fb objects */
3713 			if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
3714 				r = amdgpu_bo_reserve(robj, true);
3715 				if (r == 0) {
3716 					amdgpu_bo_unpin(robj);
3717 					amdgpu_bo_unreserve(robj);
3718 				}
3719 			}
3720 		}
3721 	}
3722 
3723 	amdgpu_ras_suspend(adev);
3724 
3725 	r = amdgpu_device_ip_suspend_phase1(adev);
3726 
3727 	amdgpu_amdkfd_suspend(adev, !fbcon);
3728 
3729 	/* evict vram memory */
3730 	amdgpu_bo_evict_vram(adev);
3731 
3732 	amdgpu_fence_driver_suspend(adev);
3733 
3734 	if (!amdgpu_acpi_is_s0ix_supported(adev) || amdgpu_in_reset(adev))
3735 		r = amdgpu_device_ip_suspend_phase2(adev);
3736 	else
3737 		amdgpu_gfx_state_change_set(adev, sGpuChangeState_D3Entry);
3738 	/* evict remaining vram memory
3739 	 * This second call to evict vram is to evict the gart page table
3740 	 * using the CPU.
3741 	 */
3742 	amdgpu_bo_evict_vram(adev);
3743 
3744 	return 0;
3745 }
3746 
3747 /**
3748  * amdgpu_device_resume - initiate device resume
3749  *
3750  * @dev: drm dev pointer
3751  * @fbcon : notify the fbdev of resume
3752  *
3753  * Bring the hw back to operating state (all asics).
3754  * Returns 0 for success or an error on failure.
3755  * Called at driver resume.
3756  */
3757 int amdgpu_device_resume(struct drm_device *dev, bool fbcon)
3758 {
3759 	struct drm_connector *connector;
3760 	struct drm_connector_list_iter iter;
3761 	struct amdgpu_device *adev = drm_to_adev(dev);
3762 	struct drm_crtc *crtc;
3763 	int r = 0;
3764 
3765 	if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
3766 		return 0;
3767 
3768 	if (amdgpu_acpi_is_s0ix_supported(adev))
3769 		amdgpu_gfx_state_change_set(adev, sGpuChangeState_D0Entry);
3770 
3771 	/* post card */
3772 	if (amdgpu_device_need_post(adev)) {
3773 		r = amdgpu_device_asic_init(adev);
3774 		if (r)
3775 			dev_err(adev->dev, "amdgpu asic init failed\n");
3776 	}
3777 
3778 	r = amdgpu_device_ip_resume(adev);
3779 	if (r) {
3780 		dev_err(adev->dev, "amdgpu_device_ip_resume failed (%d).\n", r);
3781 		return r;
3782 	}
3783 	amdgpu_fence_driver_resume(adev);
3784 
3785 
3786 	r = amdgpu_device_ip_late_init(adev);
3787 	if (r)
3788 		return r;
3789 
3790 	queue_delayed_work(system_wq, &adev->delayed_init_work,
3791 			   msecs_to_jiffies(AMDGPU_RESUME_MS));
3792 
3793 	if (!amdgpu_device_has_dc_support(adev)) {
3794 		/* pin cursors */
3795 		list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
3796 			struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
3797 
3798 			if (amdgpu_crtc->cursor_bo && !adev->enable_virtual_display) {
3799 				struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
3800 				r = amdgpu_bo_reserve(aobj, true);
3801 				if (r == 0) {
3802 					r = amdgpu_bo_pin(aobj, AMDGPU_GEM_DOMAIN_VRAM);
3803 					if (r != 0)
3804 						dev_err(adev->dev, "Failed to pin cursor BO (%d)\n", r);
3805 					amdgpu_crtc->cursor_addr = amdgpu_bo_gpu_offset(aobj);
3806 					amdgpu_bo_unreserve(aobj);
3807 				}
3808 			}
3809 		}
3810 	}
3811 	r = amdgpu_amdkfd_resume(adev, !fbcon);
3812 	if (r)
3813 		return r;
3814 
3815 	/* Make sure IB tests flushed */
3816 	flush_delayed_work(&adev->delayed_init_work);
3817 
3818 	/* blat the mode back in */
3819 	if (fbcon) {
3820 		if (!amdgpu_device_has_dc_support(adev)) {
3821 			/* pre DCE11 */
3822 			drm_helper_resume_force_mode(dev);
3823 
3824 			/* turn on display hw */
3825 			drm_modeset_lock_all(dev);
3826 
3827 			drm_connector_list_iter_begin(dev, &iter);
3828 			drm_for_each_connector_iter(connector, &iter)
3829 				drm_helper_connector_dpms(connector,
3830 							  DRM_MODE_DPMS_ON);
3831 			drm_connector_list_iter_end(&iter);
3832 
3833 			drm_modeset_unlock_all(dev);
3834 		}
3835 		amdgpu_fbdev_set_suspend(adev, 0);
3836 	}
3837 
3838 	drm_kms_helper_poll_enable(dev);
3839 
3840 	amdgpu_ras_resume(adev);
3841 
3842 	/*
3843 	 * Most of the connector probing functions try to acquire runtime pm
3844 	 * refs to ensure that the GPU is powered on when connector polling is
3845 	 * performed. Since we're calling this from a runtime PM callback,
3846 	 * trying to acquire rpm refs will cause us to deadlock.
3847 	 *
3848 	 * Since we're guaranteed to be holding the rpm lock, it's safe to
3849 	 * temporarily disable the rpm helpers so this doesn't deadlock us.
3850 	 */
3851 #ifdef CONFIG_PM
3852 	dev->dev->power.disable_depth++;
3853 #endif
3854 	if (!amdgpu_device_has_dc_support(adev))
3855 		drm_helper_hpd_irq_event(dev);
3856 	else
3857 		drm_kms_helper_hotplug_event(dev);
3858 #ifdef CONFIG_PM
3859 	dev->dev->power.disable_depth--;
3860 #endif
3861 	adev->in_suspend = false;
3862 
3863 	return 0;
3864 }
3865 
3866 /**
3867  * amdgpu_device_ip_check_soft_reset - did soft reset succeed
3868  *
3869  * @adev: amdgpu_device pointer
3870  *
3871  * The list of all the hardware IPs that make up the asic is walked and
3872  * the check_soft_reset callbacks are run.  check_soft_reset determines
3873  * if the asic is still hung or not.
3874  * Returns true if any of the IPs are still in a hung state, false if not.
3875  */
3876 static bool amdgpu_device_ip_check_soft_reset(struct amdgpu_device *adev)
3877 {
3878 	int i;
3879 	bool asic_hang = false;
3880 
3881 	if (amdgpu_sriov_vf(adev))
3882 		return true;
3883 
3884 	if (amdgpu_asic_need_full_reset(adev))
3885 		return true;
3886 
3887 	for (i = 0; i < adev->num_ip_blocks; i++) {
3888 		if (!adev->ip_blocks[i].status.valid)
3889 			continue;
3890 		if (adev->ip_blocks[i].version->funcs->check_soft_reset)
3891 			adev->ip_blocks[i].status.hang =
3892 				adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
3893 		if (adev->ip_blocks[i].status.hang) {
3894 			dev_info(adev->dev, "IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
3895 			asic_hang = true;
3896 		}
3897 	}
3898 	return asic_hang;
3899 }
3900 
3901 /**
3902  * amdgpu_device_ip_pre_soft_reset - prepare for soft reset
3903  *
3904  * @adev: amdgpu_device pointer
3905  *
3906  * The list of all the hardware IPs that make up the asic is walked and the
3907  * pre_soft_reset callbacks are run if the block is hung.  pre_soft_reset
3908  * handles any IP specific hardware or software state changes that are
3909  * necessary for a soft reset to succeed.
3910  * Returns 0 on success, negative error code on failure.
3911  */
3912 static int amdgpu_device_ip_pre_soft_reset(struct amdgpu_device *adev)
3913 {
3914 	int i, r = 0;
3915 
3916 	for (i = 0; i < adev->num_ip_blocks; i++) {
3917 		if (!adev->ip_blocks[i].status.valid)
3918 			continue;
3919 		if (adev->ip_blocks[i].status.hang &&
3920 		    adev->ip_blocks[i].version->funcs->pre_soft_reset) {
3921 			r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
3922 			if (r)
3923 				return r;
3924 		}
3925 	}
3926 
3927 	return 0;
3928 }
3929 
3930 /**
3931  * amdgpu_device_ip_need_full_reset - check if a full asic reset is needed
3932  *
3933  * @adev: amdgpu_device pointer
3934  *
3935  * Some hardware IPs cannot be soft reset.  If they are hung, a full gpu
3936  * reset is necessary to recover.
3937  * Returns true if a full asic reset is required, false if not.
3938  */
3939 static bool amdgpu_device_ip_need_full_reset(struct amdgpu_device *adev)
3940 {
3941 	int i;
3942 
3943 	if (amdgpu_asic_need_full_reset(adev))
3944 		return true;
3945 
3946 	for (i = 0; i < adev->num_ip_blocks; i++) {
3947 		if (!adev->ip_blocks[i].status.valid)
3948 			continue;
3949 		if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
3950 		    (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
3951 		    (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
3952 		    (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) ||
3953 		     adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
3954 			if (adev->ip_blocks[i].status.hang) {
3955 				dev_info(adev->dev, "Some block need full reset!\n");
3956 				return true;
3957 			}
3958 		}
3959 	}
3960 	return false;
3961 }
3962 
3963 /**
3964  * amdgpu_device_ip_soft_reset - do a soft reset
3965  *
3966  * @adev: amdgpu_device pointer
3967  *
3968  * The list of all the hardware IPs that make up the asic is walked and the
3969  * soft_reset callbacks are run if the block is hung.  soft_reset handles any
3970  * IP specific hardware or software state changes that are necessary to soft
3971  * reset the IP.
3972  * Returns 0 on success, negative error code on failure.
3973  */
3974 static int amdgpu_device_ip_soft_reset(struct amdgpu_device *adev)
3975 {
3976 	int i, r = 0;
3977 
3978 	for (i = 0; i < adev->num_ip_blocks; i++) {
3979 		if (!adev->ip_blocks[i].status.valid)
3980 			continue;
3981 		if (adev->ip_blocks[i].status.hang &&
3982 		    adev->ip_blocks[i].version->funcs->soft_reset) {
3983 			r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
3984 			if (r)
3985 				return r;
3986 		}
3987 	}
3988 
3989 	return 0;
3990 }
3991 
3992 /**
3993  * amdgpu_device_ip_post_soft_reset - clean up from soft reset
3994  *
3995  * @adev: amdgpu_device pointer
3996  *
3997  * The list of all the hardware IPs that make up the asic is walked and the
3998  * post_soft_reset callbacks are run if the asic was hung.  post_soft_reset
3999  * handles any IP specific hardware or software state changes that are
4000  * necessary after the IP has been soft reset.
4001  * Returns 0 on success, negative error code on failure.
4002  */
4003 static int amdgpu_device_ip_post_soft_reset(struct amdgpu_device *adev)
4004 {
4005 	int i, r = 0;
4006 
4007 	for (i = 0; i < adev->num_ip_blocks; i++) {
4008 		if (!adev->ip_blocks[i].status.valid)
4009 			continue;
4010 		if (adev->ip_blocks[i].status.hang &&
4011 		    adev->ip_blocks[i].version->funcs->post_soft_reset)
4012 			r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
4013 		if (r)
4014 			return r;
4015 	}
4016 
4017 	return 0;
4018 }
4019 
4020 /**
4021  * amdgpu_device_recover_vram - Recover some VRAM contents
4022  *
4023  * @adev: amdgpu_device pointer
4024  *
4025  * Restores the contents of VRAM buffers from the shadows in GTT.  Used to
4026  * restore things like GPUVM page tables after a GPU reset where
4027  * the contents of VRAM might be lost.
4028  *
4029  * Returns:
4030  * 0 on success, negative error code on failure.
4031  */
4032 static int amdgpu_device_recover_vram(struct amdgpu_device *adev)
4033 {
4034 	struct dma_fence *fence = NULL, *next = NULL;
4035 	struct amdgpu_bo *shadow;
4036 	long r = 1, tmo;
4037 
4038 	if (amdgpu_sriov_runtime(adev))
4039 		tmo = msecs_to_jiffies(8000);
4040 	else
4041 		tmo = msecs_to_jiffies(100);
4042 
4043 	dev_info(adev->dev, "recover vram bo from shadow start\n");
4044 	mutex_lock(&adev->shadow_list_lock);
4045 	list_for_each_entry(shadow, &adev->shadow_list, shadow_list) {
4046 
4047 		/* No need to recover an evicted BO */
4048 		if (shadow->tbo.mem.mem_type != TTM_PL_TT ||
4049 		    shadow->tbo.mem.start == AMDGPU_BO_INVALID_OFFSET ||
4050 		    shadow->parent->tbo.mem.mem_type != TTM_PL_VRAM)
4051 			continue;
4052 
4053 		r = amdgpu_bo_restore_shadow(shadow, &next);
4054 		if (r)
4055 			break;
4056 
4057 		if (fence) {
4058 			tmo = dma_fence_wait_timeout(fence, false, tmo);
4059 			dma_fence_put(fence);
4060 			fence = next;
4061 			if (tmo == 0) {
4062 				r = -ETIMEDOUT;
4063 				break;
4064 			} else if (tmo < 0) {
4065 				r = tmo;
4066 				break;
4067 			}
4068 		} else {
4069 			fence = next;
4070 		}
4071 	}
4072 	mutex_unlock(&adev->shadow_list_lock);
4073 
4074 	if (fence)
4075 		tmo = dma_fence_wait_timeout(fence, false, tmo);
4076 	dma_fence_put(fence);
4077 
4078 	if (r < 0 || tmo <= 0) {
4079 		dev_err(adev->dev, "recover vram bo from shadow failed, r is %ld, tmo is %ld\n", r, tmo);
4080 		return -EIO;
4081 	}
4082 
4083 	dev_info(adev->dev, "recover vram bo from shadow done\n");
4084 	return 0;
4085 }
4086 
4087 
4088 /**
4089  * amdgpu_device_reset_sriov - reset ASIC for SR-IOV vf
4090  *
4091  * @adev: amdgpu_device pointer
4092  * @from_hypervisor: request from hypervisor
4093  *
4094  * do VF FLR and reinitialize Asic
4095  * return 0 means succeeded otherwise failed
4096  */
4097 static int amdgpu_device_reset_sriov(struct amdgpu_device *adev,
4098 				     bool from_hypervisor)
4099 {
4100 	int r;
4101 
4102 	if (from_hypervisor)
4103 		r = amdgpu_virt_request_full_gpu(adev, true);
4104 	else
4105 		r = amdgpu_virt_reset_gpu(adev);
4106 	if (r)
4107 		return r;
4108 
4109 	amdgpu_amdkfd_pre_reset(adev);
4110 
4111 	/* Resume IP prior to SMC */
4112 	r = amdgpu_device_ip_reinit_early_sriov(adev);
4113 	if (r)
4114 		goto error;
4115 
4116 	amdgpu_virt_init_data_exchange(adev);
4117 	/* we need recover gart prior to run SMC/CP/SDMA resume */
4118 	amdgpu_gtt_mgr_recover(ttm_manager_type(&adev->mman.bdev, TTM_PL_TT));
4119 
4120 	r = amdgpu_device_fw_loading(adev);
4121 	if (r)
4122 		return r;
4123 
4124 	/* now we are okay to resume SMC/CP/SDMA */
4125 	r = amdgpu_device_ip_reinit_late_sriov(adev);
4126 	if (r)
4127 		goto error;
4128 
4129 	amdgpu_irq_gpu_reset_resume_helper(adev);
4130 	r = amdgpu_ib_ring_tests(adev);
4131 	amdgpu_amdkfd_post_reset(adev);
4132 
4133 error:
4134 	amdgpu_virt_release_full_gpu(adev, true);
4135 	if (!r && adev->virt.gim_feature & AMDGIM_FEATURE_GIM_FLR_VRAMLOST) {
4136 		amdgpu_inc_vram_lost(adev);
4137 		r = amdgpu_device_recover_vram(adev);
4138 	}
4139 
4140 	return r;
4141 }
4142 
4143 /**
4144  * amdgpu_device_has_job_running - check if there is any job in mirror list
4145  *
4146  * @adev: amdgpu_device pointer
4147  *
4148  * check if there is any job in mirror list
4149  */
4150 bool amdgpu_device_has_job_running(struct amdgpu_device *adev)
4151 {
4152 	int i;
4153 	struct drm_sched_job *job;
4154 
4155 	for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
4156 		struct amdgpu_ring *ring = adev->rings[i];
4157 
4158 		if (!ring || !ring->sched.thread)
4159 			continue;
4160 
4161 		spin_lock(&ring->sched.job_list_lock);
4162 		job = list_first_entry_or_null(&ring->sched.pending_list,
4163 					       struct drm_sched_job, list);
4164 		spin_unlock(&ring->sched.job_list_lock);
4165 		if (job)
4166 			return true;
4167 	}
4168 	return false;
4169 }
4170 
4171 /**
4172  * amdgpu_device_should_recover_gpu - check if we should try GPU recovery
4173  *
4174  * @adev: amdgpu_device pointer
4175  *
4176  * Check amdgpu_gpu_recovery and SRIOV status to see if we should try to recover
4177  * a hung GPU.
4178  */
4179 bool amdgpu_device_should_recover_gpu(struct amdgpu_device *adev)
4180 {
4181 	if (!amdgpu_device_ip_check_soft_reset(adev)) {
4182 		dev_info(adev->dev, "Timeout, but no hardware hang detected.\n");
4183 		return false;
4184 	}
4185 
4186 	if (amdgpu_gpu_recovery == 0)
4187 		goto disabled;
4188 
4189 	if (amdgpu_sriov_vf(adev))
4190 		return true;
4191 
4192 	if (amdgpu_gpu_recovery == -1) {
4193 		switch (adev->asic_type) {
4194 		case CHIP_BONAIRE:
4195 		case CHIP_HAWAII:
4196 		case CHIP_TOPAZ:
4197 		case CHIP_TONGA:
4198 		case CHIP_FIJI:
4199 		case CHIP_POLARIS10:
4200 		case CHIP_POLARIS11:
4201 		case CHIP_POLARIS12:
4202 		case CHIP_VEGAM:
4203 		case CHIP_VEGA20:
4204 		case CHIP_VEGA10:
4205 		case CHIP_VEGA12:
4206 		case CHIP_RAVEN:
4207 		case CHIP_ARCTURUS:
4208 		case CHIP_RENOIR:
4209 		case CHIP_NAVI10:
4210 		case CHIP_NAVI14:
4211 		case CHIP_NAVI12:
4212 		case CHIP_SIENNA_CICHLID:
4213 			break;
4214 		default:
4215 			goto disabled;
4216 		}
4217 	}
4218 
4219 	return true;
4220 
4221 disabled:
4222 		dev_info(adev->dev, "GPU recovery disabled.\n");
4223 		return false;
4224 }
4225 
4226 
4227 static int amdgpu_device_pre_asic_reset(struct amdgpu_device *adev,
4228 					struct amdgpu_job *job,
4229 					bool *need_full_reset_arg)
4230 {
4231 	int i, r = 0;
4232 	bool need_full_reset  = *need_full_reset_arg;
4233 
4234 	amdgpu_debugfs_wait_dump(adev);
4235 
4236 	if (amdgpu_sriov_vf(adev)) {
4237 		/* stop the data exchange thread */
4238 		amdgpu_virt_fini_data_exchange(adev);
4239 	}
4240 
4241 	/* block all schedulers and reset given job's ring */
4242 	for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
4243 		struct amdgpu_ring *ring = adev->rings[i];
4244 
4245 		if (!ring || !ring->sched.thread)
4246 			continue;
4247 
4248 		/* after all hw jobs are reset, hw fence is meaningless, so force_completion */
4249 		amdgpu_fence_driver_force_completion(ring);
4250 	}
4251 
4252 	if(job)
4253 		drm_sched_increase_karma(&job->base);
4254 
4255 	/* Don't suspend on bare metal if we are not going to HW reset the ASIC */
4256 	if (!amdgpu_sriov_vf(adev)) {
4257 
4258 		if (!need_full_reset)
4259 			need_full_reset = amdgpu_device_ip_need_full_reset(adev);
4260 
4261 		if (!need_full_reset) {
4262 			amdgpu_device_ip_pre_soft_reset(adev);
4263 			r = amdgpu_device_ip_soft_reset(adev);
4264 			amdgpu_device_ip_post_soft_reset(adev);
4265 			if (r || amdgpu_device_ip_check_soft_reset(adev)) {
4266 				dev_info(adev->dev, "soft reset failed, will fallback to full reset!\n");
4267 				need_full_reset = true;
4268 			}
4269 		}
4270 
4271 		if (need_full_reset)
4272 			r = amdgpu_device_ip_suspend(adev);
4273 
4274 		*need_full_reset_arg = need_full_reset;
4275 	}
4276 
4277 	return r;
4278 }
4279 
4280 static int amdgpu_do_asic_reset(struct amdgpu_hive_info *hive,
4281 			       struct list_head *device_list_handle,
4282 			       bool *need_full_reset_arg,
4283 			       bool skip_hw_reset)
4284 {
4285 	struct amdgpu_device *tmp_adev = NULL;
4286 	bool need_full_reset = *need_full_reset_arg, vram_lost = false;
4287 	int r = 0;
4288 
4289 	/*
4290 	 * ASIC reset has to be done on all HGMI hive nodes ASAP
4291 	 * to allow proper links negotiation in FW (within 1 sec)
4292 	 */
4293 	if (!skip_hw_reset && need_full_reset) {
4294 		list_for_each_entry(tmp_adev, device_list_handle, gmc.xgmi.head) {
4295 			/* For XGMI run all resets in parallel to speed up the process */
4296 			if (tmp_adev->gmc.xgmi.num_physical_nodes > 1) {
4297 				if (!queue_work(system_unbound_wq, &tmp_adev->xgmi_reset_work))
4298 					r = -EALREADY;
4299 			} else
4300 				r = amdgpu_asic_reset(tmp_adev);
4301 
4302 			if (r) {
4303 				dev_err(tmp_adev->dev, "ASIC reset failed with error, %d for drm dev, %s",
4304 					 r, adev_to_drm(tmp_adev)->unique);
4305 				break;
4306 			}
4307 		}
4308 
4309 		/* For XGMI wait for all resets to complete before proceed */
4310 		if (!r) {
4311 			list_for_each_entry(tmp_adev, device_list_handle,
4312 					    gmc.xgmi.head) {
4313 				if (tmp_adev->gmc.xgmi.num_physical_nodes > 1) {
4314 					flush_work(&tmp_adev->xgmi_reset_work);
4315 					r = tmp_adev->asic_reset_res;
4316 					if (r)
4317 						break;
4318 				}
4319 			}
4320 		}
4321 	}
4322 
4323 	if (!r && amdgpu_ras_intr_triggered()) {
4324 		list_for_each_entry(tmp_adev, device_list_handle, gmc.xgmi.head) {
4325 			if (tmp_adev->mmhub.funcs &&
4326 			    tmp_adev->mmhub.funcs->reset_ras_error_count)
4327 				tmp_adev->mmhub.funcs->reset_ras_error_count(tmp_adev);
4328 		}
4329 
4330 		amdgpu_ras_intr_cleared();
4331 	}
4332 
4333 	list_for_each_entry(tmp_adev, device_list_handle, gmc.xgmi.head) {
4334 		if (need_full_reset) {
4335 			/* post card */
4336 			if (amdgpu_device_asic_init(tmp_adev))
4337 				dev_warn(tmp_adev->dev, "asic atom init failed!");
4338 
4339 			if (!r) {
4340 				dev_info(tmp_adev->dev, "GPU reset succeeded, trying to resume\n");
4341 				r = amdgpu_device_ip_resume_phase1(tmp_adev);
4342 				if (r)
4343 					goto out;
4344 
4345 				vram_lost = amdgpu_device_check_vram_lost(tmp_adev);
4346 				if (vram_lost) {
4347 					DRM_INFO("VRAM is lost due to GPU reset!\n");
4348 					amdgpu_inc_vram_lost(tmp_adev);
4349 				}
4350 
4351 				r = amdgpu_gtt_mgr_recover(ttm_manager_type(&tmp_adev->mman.bdev, TTM_PL_TT));
4352 				if (r)
4353 					goto out;
4354 
4355 				r = amdgpu_device_fw_loading(tmp_adev);
4356 				if (r)
4357 					return r;
4358 
4359 				r = amdgpu_device_ip_resume_phase2(tmp_adev);
4360 				if (r)
4361 					goto out;
4362 
4363 				if (vram_lost)
4364 					amdgpu_device_fill_reset_magic(tmp_adev);
4365 
4366 				/*
4367 				 * Add this ASIC as tracked as reset was already
4368 				 * complete successfully.
4369 				 */
4370 				amdgpu_register_gpu_instance(tmp_adev);
4371 
4372 				r = amdgpu_device_ip_late_init(tmp_adev);
4373 				if (r)
4374 					goto out;
4375 
4376 				amdgpu_fbdev_set_suspend(tmp_adev, 0);
4377 
4378 				/*
4379 				 * The GPU enters bad state once faulty pages
4380 				 * by ECC has reached the threshold, and ras
4381 				 * recovery is scheduled next. So add one check
4382 				 * here to break recovery if it indeed exceeds
4383 				 * bad page threshold, and remind user to
4384 				 * retire this GPU or setting one bigger
4385 				 * bad_page_threshold value to fix this once
4386 				 * probing driver again.
4387 				 */
4388 				if (!amdgpu_ras_check_err_threshold(tmp_adev)) {
4389 					/* must succeed. */
4390 					amdgpu_ras_resume(tmp_adev);
4391 				} else {
4392 					r = -EINVAL;
4393 					goto out;
4394 				}
4395 
4396 				/* Update PSP FW topology after reset */
4397 				if (hive && tmp_adev->gmc.xgmi.num_physical_nodes > 1)
4398 					r = amdgpu_xgmi_update_topology(hive, tmp_adev);
4399 			}
4400 		}
4401 
4402 out:
4403 		if (!r) {
4404 			amdgpu_irq_gpu_reset_resume_helper(tmp_adev);
4405 			r = amdgpu_ib_ring_tests(tmp_adev);
4406 			if (r) {
4407 				dev_err(tmp_adev->dev, "ib ring test failed (%d).\n", r);
4408 				r = amdgpu_device_ip_suspend(tmp_adev);
4409 				need_full_reset = true;
4410 				r = -EAGAIN;
4411 				goto end;
4412 			}
4413 		}
4414 
4415 		if (!r)
4416 			r = amdgpu_device_recover_vram(tmp_adev);
4417 		else
4418 			tmp_adev->asic_reset_res = r;
4419 	}
4420 
4421 end:
4422 	*need_full_reset_arg = need_full_reset;
4423 	return r;
4424 }
4425 
4426 static bool amdgpu_device_lock_adev(struct amdgpu_device *adev,
4427 				struct amdgpu_hive_info *hive)
4428 {
4429 	if (atomic_cmpxchg(&adev->in_gpu_reset, 0, 1) != 0)
4430 		return false;
4431 
4432 	if (hive) {
4433 		down_write_nest_lock(&adev->reset_sem, &hive->hive_lock);
4434 	} else {
4435 		down_write(&adev->reset_sem);
4436 	}
4437 
4438 	atomic_inc(&adev->gpu_reset_counter);
4439 	switch (amdgpu_asic_reset_method(adev)) {
4440 	case AMD_RESET_METHOD_MODE1:
4441 		adev->mp1_state = PP_MP1_STATE_SHUTDOWN;
4442 		break;
4443 	case AMD_RESET_METHOD_MODE2:
4444 		adev->mp1_state = PP_MP1_STATE_RESET;
4445 		break;
4446 	default:
4447 		adev->mp1_state = PP_MP1_STATE_NONE;
4448 		break;
4449 	}
4450 
4451 	return true;
4452 }
4453 
4454 static void amdgpu_device_unlock_adev(struct amdgpu_device *adev)
4455 {
4456 	amdgpu_vf_error_trans_all(adev);
4457 	adev->mp1_state = PP_MP1_STATE_NONE;
4458 	atomic_set(&adev->in_gpu_reset, 0);
4459 	up_write(&adev->reset_sem);
4460 }
4461 
4462 static void amdgpu_device_resume_display_audio(struct amdgpu_device *adev)
4463 {
4464 	struct pci_dev *p = NULL;
4465 
4466 	p = pci_get_domain_bus_and_slot(pci_domain_nr(adev->pdev->bus),
4467 			adev->pdev->bus->number, 1);
4468 	if (p) {
4469 		pm_runtime_enable(&(p->dev));
4470 		pm_runtime_resume(&(p->dev));
4471 	}
4472 }
4473 
4474 static int amdgpu_device_suspend_display_audio(struct amdgpu_device *adev)
4475 {
4476 	enum amd_reset_method reset_method;
4477 	struct pci_dev *p = NULL;
4478 	u64 expires;
4479 
4480 	/*
4481 	 * For now, only BACO and mode1 reset are confirmed
4482 	 * to suffer the audio issue without proper suspended.
4483 	 */
4484 	reset_method = amdgpu_asic_reset_method(adev);
4485 	if ((reset_method != AMD_RESET_METHOD_BACO) &&
4486 	     (reset_method != AMD_RESET_METHOD_MODE1))
4487 		return -EINVAL;
4488 
4489 	p = pci_get_domain_bus_and_slot(pci_domain_nr(adev->pdev->bus),
4490 			adev->pdev->bus->number, 1);
4491 	if (!p)
4492 		return -ENODEV;
4493 
4494 	expires = pm_runtime_autosuspend_expiration(&(p->dev));
4495 	if (!expires)
4496 		/*
4497 		 * If we cannot get the audio device autosuspend delay,
4498 		 * a fixed 4S interval will be used. Considering 3S is
4499 		 * the audio controller default autosuspend delay setting.
4500 		 * 4S used here is guaranteed to cover that.
4501 		 */
4502 		expires = ktime_get_mono_fast_ns() + NSEC_PER_SEC * 4ULL;
4503 
4504 	while (!pm_runtime_status_suspended(&(p->dev))) {
4505 		if (!pm_runtime_suspend(&(p->dev)))
4506 			break;
4507 
4508 		if (expires < ktime_get_mono_fast_ns()) {
4509 			dev_warn(adev->dev, "failed to suspend display audio\n");
4510 			/* TODO: abort the succeeding gpu reset? */
4511 			return -ETIMEDOUT;
4512 		}
4513 	}
4514 
4515 	pm_runtime_disable(&(p->dev));
4516 
4517 	return 0;
4518 }
4519 
4520 /**
4521  * amdgpu_device_gpu_recover - reset the asic and recover scheduler
4522  *
4523  * @adev: amdgpu_device pointer
4524  * @job: which job trigger hang
4525  *
4526  * Attempt to reset the GPU if it has hung (all asics).
4527  * Attempt to do soft-reset or full-reset and reinitialize Asic
4528  * Returns 0 for success or an error on failure.
4529  */
4530 
4531 int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
4532 			      struct amdgpu_job *job)
4533 {
4534 	struct list_head device_list, *device_list_handle =  NULL;
4535 	bool need_full_reset = false;
4536 	bool job_signaled = false;
4537 	struct amdgpu_hive_info *hive = NULL;
4538 	struct amdgpu_device *tmp_adev = NULL;
4539 	int i, r = 0;
4540 	bool need_emergency_restart = false;
4541 	bool audio_suspended = false;
4542 
4543 	/*
4544 	 * Special case: RAS triggered and full reset isn't supported
4545 	 */
4546 	need_emergency_restart = amdgpu_ras_need_emergency_restart(adev);
4547 
4548 	/*
4549 	 * Flush RAM to disk so that after reboot
4550 	 * the user can read log and see why the system rebooted.
4551 	 */
4552 	if (need_emergency_restart && amdgpu_ras_get_context(adev)->reboot) {
4553 		DRM_WARN("Emergency reboot.");
4554 
4555 		ksys_sync_helper();
4556 		emergency_restart();
4557 	}
4558 
4559 	dev_info(adev->dev, "GPU %s begin!\n",
4560 		need_emergency_restart ? "jobs stop":"reset");
4561 
4562 	/*
4563 	 * Here we trylock to avoid chain of resets executing from
4564 	 * either trigger by jobs on different adevs in XGMI hive or jobs on
4565 	 * different schedulers for same device while this TO handler is running.
4566 	 * We always reset all schedulers for device and all devices for XGMI
4567 	 * hive so that should take care of them too.
4568 	 */
4569 	hive = amdgpu_get_xgmi_hive(adev);
4570 	if (hive) {
4571 		if (atomic_cmpxchg(&hive->in_reset, 0, 1) != 0) {
4572 			DRM_INFO("Bailing on TDR for s_job:%llx, hive: %llx as another already in progress",
4573 				job ? job->base.id : -1, hive->hive_id);
4574 			amdgpu_put_xgmi_hive(hive);
4575 			return 0;
4576 		}
4577 		mutex_lock(&hive->hive_lock);
4578 	}
4579 
4580 	/*
4581 	 * Build list of devices to reset.
4582 	 * In case we are in XGMI hive mode, resort the device list
4583 	 * to put adev in the 1st position.
4584 	 */
4585 	INIT_LIST_HEAD(&device_list);
4586 	if (adev->gmc.xgmi.num_physical_nodes > 1) {
4587 		if (!hive)
4588 			return -ENODEV;
4589 		if (!list_is_first(&adev->gmc.xgmi.head, &hive->device_list))
4590 			list_rotate_to_front(&adev->gmc.xgmi.head, &hive->device_list);
4591 		device_list_handle = &hive->device_list;
4592 	} else {
4593 		list_add_tail(&adev->gmc.xgmi.head, &device_list);
4594 		device_list_handle = &device_list;
4595 	}
4596 
4597 	/* block all schedulers and reset given job's ring */
4598 	list_for_each_entry(tmp_adev, device_list_handle, gmc.xgmi.head) {
4599 		if (!amdgpu_device_lock_adev(tmp_adev, hive)) {
4600 			dev_info(tmp_adev->dev, "Bailing on TDR for s_job:%llx, as another already in progress",
4601 				  job ? job->base.id : -1);
4602 			r = 0;
4603 			goto skip_recovery;
4604 		}
4605 
4606 		/*
4607 		 * Try to put the audio codec into suspend state
4608 		 * before gpu reset started.
4609 		 *
4610 		 * Due to the power domain of the graphics device
4611 		 * is shared with AZ power domain. Without this,
4612 		 * we may change the audio hardware from behind
4613 		 * the audio driver's back. That will trigger
4614 		 * some audio codec errors.
4615 		 */
4616 		if (!amdgpu_device_suspend_display_audio(tmp_adev))
4617 			audio_suspended = true;
4618 
4619 		amdgpu_ras_set_error_query_ready(tmp_adev, false);
4620 
4621 		cancel_delayed_work_sync(&tmp_adev->delayed_init_work);
4622 
4623 		if (!amdgpu_sriov_vf(tmp_adev))
4624 			amdgpu_amdkfd_pre_reset(tmp_adev);
4625 
4626 		/*
4627 		 * Mark these ASICs to be reseted as untracked first
4628 		 * And add them back after reset completed
4629 		 */
4630 		amdgpu_unregister_gpu_instance(tmp_adev);
4631 
4632 		amdgpu_fbdev_set_suspend(tmp_adev, 1);
4633 
4634 		/* disable ras on ALL IPs */
4635 		if (!need_emergency_restart &&
4636 		      amdgpu_device_ip_need_full_reset(tmp_adev))
4637 			amdgpu_ras_suspend(tmp_adev);
4638 
4639 		for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
4640 			struct amdgpu_ring *ring = tmp_adev->rings[i];
4641 
4642 			if (!ring || !ring->sched.thread)
4643 				continue;
4644 
4645 			drm_sched_stop(&ring->sched, job ? &job->base : NULL);
4646 
4647 			if (need_emergency_restart)
4648 				amdgpu_job_stop_all_jobs_on_sched(&ring->sched);
4649 		}
4650 	}
4651 
4652 	if (need_emergency_restart)
4653 		goto skip_sched_resume;
4654 
4655 	/*
4656 	 * Must check guilty signal here since after this point all old
4657 	 * HW fences are force signaled.
4658 	 *
4659 	 * job->base holds a reference to parent fence
4660 	 */
4661 	if (job && job->base.s_fence->parent &&
4662 	    dma_fence_is_signaled(job->base.s_fence->parent)) {
4663 		job_signaled = true;
4664 		dev_info(adev->dev, "Guilty job already signaled, skipping HW reset");
4665 		goto skip_hw_reset;
4666 	}
4667 
4668 retry:	/* Rest of adevs pre asic reset from XGMI hive. */
4669 	list_for_each_entry(tmp_adev, device_list_handle, gmc.xgmi.head) {
4670 		r = amdgpu_device_pre_asic_reset(tmp_adev,
4671 						 (tmp_adev == adev) ? job : NULL,
4672 						 &need_full_reset);
4673 		/*TODO Should we stop ?*/
4674 		if (r) {
4675 			dev_err(tmp_adev->dev, "GPU pre asic reset failed with err, %d for drm dev, %s ",
4676 				  r, adev_to_drm(tmp_adev)->unique);
4677 			tmp_adev->asic_reset_res = r;
4678 		}
4679 	}
4680 
4681 	/* Actual ASIC resets if needed.*/
4682 	/* TODO Implement XGMI hive reset logic for SRIOV */
4683 	if (amdgpu_sriov_vf(adev)) {
4684 		r = amdgpu_device_reset_sriov(adev, job ? false : true);
4685 		if (r)
4686 			adev->asic_reset_res = r;
4687 	} else {
4688 		r  = amdgpu_do_asic_reset(hive, device_list_handle, &need_full_reset, false);
4689 		if (r && r == -EAGAIN)
4690 			goto retry;
4691 	}
4692 
4693 skip_hw_reset:
4694 
4695 	/* Post ASIC reset for all devs .*/
4696 	list_for_each_entry(tmp_adev, device_list_handle, gmc.xgmi.head) {
4697 
4698 		for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
4699 			struct amdgpu_ring *ring = tmp_adev->rings[i];
4700 
4701 			if (!ring || !ring->sched.thread)
4702 				continue;
4703 
4704 			/* No point to resubmit jobs if we didn't HW reset*/
4705 			if (!tmp_adev->asic_reset_res && !job_signaled)
4706 				drm_sched_resubmit_jobs(&ring->sched);
4707 
4708 			drm_sched_start(&ring->sched, !tmp_adev->asic_reset_res);
4709 		}
4710 
4711 		if (!amdgpu_device_has_dc_support(tmp_adev) && !job_signaled) {
4712 			drm_helper_resume_force_mode(adev_to_drm(tmp_adev));
4713 		}
4714 
4715 		tmp_adev->asic_reset_res = 0;
4716 
4717 		if (r) {
4718 			/* bad news, how to tell it to userspace ? */
4719 			dev_info(tmp_adev->dev, "GPU reset(%d) failed\n", atomic_read(&tmp_adev->gpu_reset_counter));
4720 			amdgpu_vf_error_put(tmp_adev, AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, r);
4721 		} else {
4722 			dev_info(tmp_adev->dev, "GPU reset(%d) succeeded!\n", atomic_read(&tmp_adev->gpu_reset_counter));
4723 		}
4724 	}
4725 
4726 skip_sched_resume:
4727 	list_for_each_entry(tmp_adev, device_list_handle, gmc.xgmi.head) {
4728 		/*unlock kfd: SRIOV would do it separately */
4729 		if (!need_emergency_restart && !amdgpu_sriov_vf(tmp_adev))
4730 	                amdgpu_amdkfd_post_reset(tmp_adev);
4731 		if (audio_suspended)
4732 			amdgpu_device_resume_display_audio(tmp_adev);
4733 		amdgpu_device_unlock_adev(tmp_adev);
4734 	}
4735 
4736 skip_recovery:
4737 	if (hive) {
4738 		atomic_set(&hive->in_reset, 0);
4739 		mutex_unlock(&hive->hive_lock);
4740 		amdgpu_put_xgmi_hive(hive);
4741 	}
4742 
4743 	if (r)
4744 		dev_info(adev->dev, "GPU reset end with ret = %d\n", r);
4745 	return r;
4746 }
4747 
4748 /**
4749  * amdgpu_device_get_pcie_info - fence pcie info about the PCIE slot
4750  *
4751  * @adev: amdgpu_device pointer
4752  *
4753  * Fetchs and stores in the driver the PCIE capabilities (gen speed
4754  * and lanes) of the slot the device is in. Handles APUs and
4755  * virtualized environments where PCIE config space may not be available.
4756  */
4757 static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev)
4758 {
4759 	struct pci_dev *pdev;
4760 	enum pci_bus_speed speed_cap, platform_speed_cap;
4761 	enum pcie_link_width platform_link_width;
4762 
4763 	if (amdgpu_pcie_gen_cap)
4764 		adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
4765 
4766 	if (amdgpu_pcie_lane_cap)
4767 		adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
4768 
4769 	/* covers APUs as well */
4770 	if (pci_is_root_bus(adev->pdev->bus)) {
4771 		if (adev->pm.pcie_gen_mask == 0)
4772 			adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
4773 		if (adev->pm.pcie_mlw_mask == 0)
4774 			adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
4775 		return;
4776 	}
4777 
4778 	if (adev->pm.pcie_gen_mask && adev->pm.pcie_mlw_mask)
4779 		return;
4780 
4781 	pcie_bandwidth_available(adev->pdev, NULL,
4782 				 &platform_speed_cap, &platform_link_width);
4783 
4784 	if (adev->pm.pcie_gen_mask == 0) {
4785 		/* asic caps */
4786 		pdev = adev->pdev;
4787 		speed_cap = pcie_get_speed_cap(pdev);
4788 		if (speed_cap == PCI_SPEED_UNKNOWN) {
4789 			adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
4790 						  CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
4791 						  CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
4792 		} else {
4793 			if (speed_cap == PCIE_SPEED_16_0GT)
4794 				adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
4795 							  CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
4796 							  CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3 |
4797 							  CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN4);
4798 			else if (speed_cap == PCIE_SPEED_8_0GT)
4799 				adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
4800 							  CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
4801 							  CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
4802 			else if (speed_cap == PCIE_SPEED_5_0GT)
4803 				adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
4804 							  CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2);
4805 			else
4806 				adev->pm.pcie_gen_mask |= CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1;
4807 		}
4808 		/* platform caps */
4809 		if (platform_speed_cap == PCI_SPEED_UNKNOWN) {
4810 			adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
4811 						   CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2);
4812 		} else {
4813 			if (platform_speed_cap == PCIE_SPEED_16_0GT)
4814 				adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
4815 							   CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
4816 							   CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3 |
4817 							   CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4);
4818 			else if (platform_speed_cap == PCIE_SPEED_8_0GT)
4819 				adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
4820 							   CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
4821 							   CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3);
4822 			else if (platform_speed_cap == PCIE_SPEED_5_0GT)
4823 				adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
4824 							   CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2);
4825 			else
4826 				adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
4827 
4828 		}
4829 	}
4830 	if (adev->pm.pcie_mlw_mask == 0) {
4831 		if (platform_link_width == PCIE_LNK_WIDTH_UNKNOWN) {
4832 			adev->pm.pcie_mlw_mask |= AMDGPU_DEFAULT_PCIE_MLW_MASK;
4833 		} else {
4834 			switch (platform_link_width) {
4835 			case PCIE_LNK_X32:
4836 				adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
4837 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
4838 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
4839 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
4840 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
4841 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
4842 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
4843 				break;
4844 			case PCIE_LNK_X16:
4845 				adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
4846 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
4847 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
4848 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
4849 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
4850 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
4851 				break;
4852 			case PCIE_LNK_X12:
4853 				adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
4854 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
4855 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
4856 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
4857 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
4858 				break;
4859 			case PCIE_LNK_X8:
4860 				adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
4861 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
4862 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
4863 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
4864 				break;
4865 			case PCIE_LNK_X4:
4866 				adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
4867 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
4868 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
4869 				break;
4870 			case PCIE_LNK_X2:
4871 				adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
4872 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
4873 				break;
4874 			case PCIE_LNK_X1:
4875 				adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
4876 				break;
4877 			default:
4878 				break;
4879 			}
4880 		}
4881 	}
4882 }
4883 
4884 int amdgpu_device_baco_enter(struct drm_device *dev)
4885 {
4886 	struct amdgpu_device *adev = drm_to_adev(dev);
4887 	struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
4888 
4889 	if (!amdgpu_device_supports_baco(adev_to_drm(adev)))
4890 		return -ENOTSUPP;
4891 
4892 	if (ras && ras->supported && adev->nbio.funcs->enable_doorbell_interrupt)
4893 		adev->nbio.funcs->enable_doorbell_interrupt(adev, false);
4894 
4895 	return amdgpu_dpm_baco_enter(adev);
4896 }
4897 
4898 int amdgpu_device_baco_exit(struct drm_device *dev)
4899 {
4900 	struct amdgpu_device *adev = drm_to_adev(dev);
4901 	struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
4902 	int ret = 0;
4903 
4904 	if (!amdgpu_device_supports_baco(adev_to_drm(adev)))
4905 		return -ENOTSUPP;
4906 
4907 	ret = amdgpu_dpm_baco_exit(adev);
4908 	if (ret)
4909 		return ret;
4910 
4911 	if (ras && ras->supported && adev->nbio.funcs->enable_doorbell_interrupt)
4912 		adev->nbio.funcs->enable_doorbell_interrupt(adev, true);
4913 
4914 	return 0;
4915 }
4916 
4917 static void amdgpu_cancel_all_tdr(struct amdgpu_device *adev)
4918 {
4919 	int i;
4920 
4921 	for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
4922 		struct amdgpu_ring *ring = adev->rings[i];
4923 
4924 		if (!ring || !ring->sched.thread)
4925 			continue;
4926 
4927 		cancel_delayed_work_sync(&ring->sched.work_tdr);
4928 	}
4929 }
4930 
4931 /**
4932  * amdgpu_pci_error_detected - Called when a PCI error is detected.
4933  * @pdev: PCI device struct
4934  * @state: PCI channel state
4935  *
4936  * Description: Called when a PCI error is detected.
4937  *
4938  * Return: PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT.
4939  */
4940 pci_ers_result_t amdgpu_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
4941 {
4942 	struct drm_device *dev = pci_get_drvdata(pdev);
4943 	struct amdgpu_device *adev = drm_to_adev(dev);
4944 	int i;
4945 
4946 	DRM_INFO("PCI error: detected callback, state(%d)!!\n", state);
4947 
4948 	if (adev->gmc.xgmi.num_physical_nodes > 1) {
4949 		DRM_WARN("No support for XGMI hive yet...");
4950 		return PCI_ERS_RESULT_DISCONNECT;
4951 	}
4952 
4953 	switch (state) {
4954 	case pci_channel_io_normal:
4955 		return PCI_ERS_RESULT_CAN_RECOVER;
4956 	/* Fatal error, prepare for slot reset */
4957 	case pci_channel_io_frozen:
4958 		/*
4959 		 * Cancel and wait for all TDRs in progress if failing to
4960 		 * set  adev->in_gpu_reset in amdgpu_device_lock_adev
4961 		 *
4962 		 * Locking adev->reset_sem will prevent any external access
4963 		 * to GPU during PCI error recovery
4964 		 */
4965 		while (!amdgpu_device_lock_adev(adev, NULL))
4966 			amdgpu_cancel_all_tdr(adev);
4967 
4968 		/*
4969 		 * Block any work scheduling as we do for regular GPU reset
4970 		 * for the duration of the recovery
4971 		 */
4972 		for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
4973 			struct amdgpu_ring *ring = adev->rings[i];
4974 
4975 			if (!ring || !ring->sched.thread)
4976 				continue;
4977 
4978 			drm_sched_stop(&ring->sched, NULL);
4979 		}
4980 		return PCI_ERS_RESULT_NEED_RESET;
4981 	case pci_channel_io_perm_failure:
4982 		/* Permanent error, prepare for device removal */
4983 		return PCI_ERS_RESULT_DISCONNECT;
4984 	}
4985 
4986 	return PCI_ERS_RESULT_NEED_RESET;
4987 }
4988 
4989 /**
4990  * amdgpu_pci_mmio_enabled - Enable MMIO and dump debug registers
4991  * @pdev: pointer to PCI device
4992  */
4993 pci_ers_result_t amdgpu_pci_mmio_enabled(struct pci_dev *pdev)
4994 {
4995 
4996 	DRM_INFO("PCI error: mmio enabled callback!!\n");
4997 
4998 	/* TODO - dump whatever for debugging purposes */
4999 
5000 	/* This called only if amdgpu_pci_error_detected returns
5001 	 * PCI_ERS_RESULT_CAN_RECOVER. Read/write to the device still
5002 	 * works, no need to reset slot.
5003 	 */
5004 
5005 	return PCI_ERS_RESULT_RECOVERED;
5006 }
5007 
5008 /**
5009  * amdgpu_pci_slot_reset - Called when PCI slot has been reset.
5010  * @pdev: PCI device struct
5011  *
5012  * Description: This routine is called by the pci error recovery
5013  * code after the PCI slot has been reset, just before we
5014  * should resume normal operations.
5015  */
5016 pci_ers_result_t amdgpu_pci_slot_reset(struct pci_dev *pdev)
5017 {
5018 	struct drm_device *dev = pci_get_drvdata(pdev);
5019 	struct amdgpu_device *adev = drm_to_adev(dev);
5020 	int r, i;
5021 	bool need_full_reset = true;
5022 	u32 memsize;
5023 	struct list_head device_list;
5024 
5025 	DRM_INFO("PCI error: slot reset callback!!\n");
5026 
5027 	INIT_LIST_HEAD(&device_list);
5028 	list_add_tail(&adev->gmc.xgmi.head, &device_list);
5029 
5030 	/* wait for asic to come out of reset */
5031 	msleep(500);
5032 
5033 	/* Restore PCI confspace */
5034 	amdgpu_device_load_pci_state(pdev);
5035 
5036 	/* confirm  ASIC came out of reset */
5037 	for (i = 0; i < adev->usec_timeout; i++) {
5038 		memsize = amdgpu_asic_get_config_memsize(adev);
5039 
5040 		if (memsize != 0xffffffff)
5041 			break;
5042 		udelay(1);
5043 	}
5044 	if (memsize == 0xffffffff) {
5045 		r = -ETIME;
5046 		goto out;
5047 	}
5048 
5049 	adev->in_pci_err_recovery = true;
5050 	r = amdgpu_device_pre_asic_reset(adev, NULL, &need_full_reset);
5051 	adev->in_pci_err_recovery = false;
5052 	if (r)
5053 		goto out;
5054 
5055 	r = amdgpu_do_asic_reset(NULL, &device_list, &need_full_reset, true);
5056 
5057 out:
5058 	if (!r) {
5059 		if (amdgpu_device_cache_pci_state(adev->pdev))
5060 			pci_restore_state(adev->pdev);
5061 
5062 		DRM_INFO("PCIe error recovery succeeded\n");
5063 	} else {
5064 		DRM_ERROR("PCIe error recovery failed, err:%d", r);
5065 		amdgpu_device_unlock_adev(adev);
5066 	}
5067 
5068 	return r ? PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_RECOVERED;
5069 }
5070 
5071 /**
5072  * amdgpu_pci_resume() - resume normal ops after PCI reset
5073  * @pdev: pointer to PCI device
5074  *
5075  * Called when the error recovery driver tells us that its
5076  * OK to resume normal operation.
5077  */
5078 void amdgpu_pci_resume(struct pci_dev *pdev)
5079 {
5080 	struct drm_device *dev = pci_get_drvdata(pdev);
5081 	struct amdgpu_device *adev = drm_to_adev(dev);
5082 	int i;
5083 
5084 
5085 	DRM_INFO("PCI error: resume callback!!\n");
5086 
5087 	for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
5088 		struct amdgpu_ring *ring = adev->rings[i];
5089 
5090 		if (!ring || !ring->sched.thread)
5091 			continue;
5092 
5093 
5094 		drm_sched_resubmit_jobs(&ring->sched);
5095 		drm_sched_start(&ring->sched, true);
5096 	}
5097 
5098 	amdgpu_device_unlock_adev(adev);
5099 }
5100 
5101 bool amdgpu_device_cache_pci_state(struct pci_dev *pdev)
5102 {
5103 	struct drm_device *dev = pci_get_drvdata(pdev);
5104 	struct amdgpu_device *adev = drm_to_adev(dev);
5105 	int r;
5106 
5107 	r = pci_save_state(pdev);
5108 	if (!r) {
5109 		kfree(adev->pci_state);
5110 
5111 		adev->pci_state = pci_store_saved_state(pdev);
5112 
5113 		if (!adev->pci_state) {
5114 			DRM_ERROR("Failed to store PCI saved state");
5115 			return false;
5116 		}
5117 	} else {
5118 		DRM_WARN("Failed to save PCI state, err:%d\n", r);
5119 		return false;
5120 	}
5121 
5122 	return true;
5123 }
5124 
5125 bool amdgpu_device_load_pci_state(struct pci_dev *pdev)
5126 {
5127 	struct drm_device *dev = pci_get_drvdata(pdev);
5128 	struct amdgpu_device *adev = drm_to_adev(dev);
5129 	int r;
5130 
5131 	if (!adev->pci_state)
5132 		return false;
5133 
5134 	r = pci_load_saved_state(pdev, adev->pci_state);
5135 
5136 	if (!r) {
5137 		pci_restore_state(pdev);
5138 	} else {
5139 		DRM_WARN("Failed to load PCI state, err:%d\n", r);
5140 		return false;
5141 	}
5142 
5143 	return true;
5144 }
5145 
5146 
5147