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