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