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