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