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