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