xref: /openbsd-src/sys/dev/pci/drm/amd/amdgpu/amdgpu_device.c (revision 7350f337b9e3eb4461d99580e625c7ef148d107c)
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/console.h>
31 #include <linux/slab.h>
32 #include <drm/drmP.h>
33 #include <drm/drm_crtc_helper.h>
34 #include <drm/drm_atomic_helper.h>
35 #include <drm/amdgpu_drm.h>
36 #include <linux/vgaarb.h>
37 #include <linux/vga_switcheroo.h>
38 #include <linux/efi.h>
39 #include "amdgpu.h"
40 #include "amdgpu_trace.h"
41 #include "amdgpu_i2c.h"
42 #include "atom.h"
43 #include "amdgpu_atombios.h"
44 #include "amdgpu_atomfirmware.h"
45 #include "amd_pcie.h"
46 #ifdef CONFIG_DRM_AMDGPU_SI
47 #include "si.h"
48 #endif
49 #ifdef CONFIG_DRM_AMDGPU_CIK
50 #include "cik.h"
51 #endif
52 #include "vi.h"
53 #include "soc15.h"
54 #include "bif/bif_4_1_d.h"
55 #include <linux/pci.h>
56 #include <linux/firmware.h>
57 #include "amdgpu_vf_error.h"
58 
59 #include "amdgpu_amdkfd.h"
60 #include "amdgpu_pm.h"
61 
62 MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin");
63 MODULE_FIRMWARE("amdgpu/vega12_gpu_info.bin");
64 MODULE_FIRMWARE("amdgpu/raven_gpu_info.bin");
65 
66 #define AMDGPU_RESUME_MS		2000
67 
68 static const char *amdgpu_asic_name[] = {
69 	"TAHITI",
70 	"PITCAIRN",
71 	"VERDE",
72 	"OLAND",
73 	"HAINAN",
74 	"BONAIRE",
75 	"KAVERI",
76 	"KABINI",
77 	"HAWAII",
78 	"MULLINS",
79 	"TOPAZ",
80 	"TONGA",
81 	"FIJI",
82 	"CARRIZO",
83 	"STONEY",
84 	"POLARIS10",
85 	"POLARIS11",
86 	"POLARIS12",
87 	"VEGAM",
88 	"VEGA10",
89 	"VEGA12",
90 	"VEGA20",
91 	"RAVEN",
92 	"LAST",
93 };
94 
95 static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev);
96 
97 /**
98  * amdgpu_device_is_px - Is the device is a dGPU with HG/PX power control
99  *
100  * @dev: drm_device pointer
101  *
102  * Returns true if the device is a dGPU with HG/PX power control,
103  * otherwise return false.
104  */
105 bool amdgpu_device_is_px(struct drm_device *dev)
106 {
107 	struct amdgpu_device *adev = dev->dev_private;
108 
109 	if (adev->flags & AMD_IS_PX)
110 		return true;
111 	return false;
112 }
113 
114 /*
115  * MMIO register access helper functions.
116  */
117 /**
118  * amdgpu_mm_rreg - read a memory mapped IO register
119  *
120  * @adev: amdgpu_device pointer
121  * @reg: dword aligned register offset
122  * @acc_flags: access flags which require special behavior
123  *
124  * Returns the 32 bit value from the offset specified.
125  */
126 uint32_t amdgpu_mm_rreg(struct amdgpu_device *adev, uint32_t reg,
127 			uint32_t acc_flags)
128 {
129 	uint32_t ret;
130 
131 	if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev))
132 		return amdgpu_virt_kiq_rreg(adev, reg);
133 
134 	if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
135 		ret = bus_space_read_4(adev->rmmio_bst, adev->rmmio_bsh,
136 		    reg * 4);
137 	else {
138 		unsigned long flags;
139 
140 		spin_lock_irqsave(&adev->mmio_idx_lock, flags);
141 		bus_space_write_4(adev->rmmio_bst, adev->rmmio_bsh,
142 		    mmMM_INDEX * 4, reg * 4);
143 		ret = bus_space_read_4(adev->rmmio_bst, adev->rmmio_bsh,
144 		    mmMM_DATA * 4);
145 		spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
146 	}
147 	trace_amdgpu_mm_rreg(adev->pdev->device, reg, ret);
148 	return ret;
149 }
150 
151 /*
152  * MMIO register read with bytes helper functions
153  * @offset:bytes offset from MMIO start
154  *
155 */
156 
157 /**
158  * amdgpu_mm_rreg8 - read a memory mapped IO register
159  *
160  * @adev: amdgpu_device pointer
161  * @offset: byte aligned register offset
162  *
163  * Returns the 8 bit value from the offset specified.
164  */
165 uint8_t amdgpu_mm_rreg8(struct amdgpu_device *adev, uint32_t offset) {
166 	if (offset < adev->rmmio_size)
167 		return bus_space_read_1(adev->rmmio_bst, adev->rmmio_bsh,
168 		    offset);
169 	BUG();
170 }
171 
172 /*
173  * MMIO register write with bytes helper functions
174  * @offset:bytes offset from MMIO start
175  * @value: the value want to be written to the register
176  *
177 */
178 /**
179  * amdgpu_mm_wreg8 - read a memory mapped IO register
180  *
181  * @adev: amdgpu_device pointer
182  * @offset: byte aligned register offset
183  * @value: 8 bit value to write
184  *
185  * Writes the value specified to the offset specified.
186  */
187 void amdgpu_mm_wreg8(struct amdgpu_device *adev, uint32_t offset, uint8_t value) {
188 	if (offset < adev->rmmio_size)
189 		bus_space_write_1(adev->rmmio_bst, adev->rmmio_bsh,
190 		    offset, value);
191 	else
192 		BUG();
193 }
194 
195 /**
196  * amdgpu_mm_wreg - write to a memory mapped IO register
197  *
198  * @adev: amdgpu_device pointer
199  * @reg: dword aligned register offset
200  * @v: 32 bit value to write to the register
201  * @acc_flags: access flags which require special behavior
202  *
203  * Writes the value specified to the offset specified.
204  */
205 void amdgpu_mm_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v,
206 		    uint32_t acc_flags)
207 {
208 	trace_amdgpu_mm_wreg(adev->pdev->device, reg, v);
209 
210 	if (adev->asic_type >= CHIP_VEGA10 && reg == 0) {
211 		adev->last_mm_index = v;
212 	}
213 
214 	if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev))
215 		return amdgpu_virt_kiq_wreg(adev, reg, v);
216 
217 	if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
218 		bus_space_write_4(adev->rmmio_bst, adev->rmmio_bsh,
219 		    reg * 4, v);
220 	else {
221 		unsigned long flags;
222 
223 		spin_lock_irqsave(&adev->mmio_idx_lock, flags);
224 		bus_space_write_4(adev->rmmio_bst, adev->rmmio_bsh,
225 		    mmMM_INDEX * 4, reg * 4);
226 		bus_space_write_4(adev->rmmio_bst, adev->rmmio_bsh,
227 		    mmMM_DATA * 4, v);
228 		spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
229 	}
230 
231 	if (adev->asic_type >= CHIP_VEGA10 && reg == 1 && adev->last_mm_index == 0x5702C) {
232 		udelay(500);
233 	}
234 }
235 
236 /**
237  * amdgpu_io_rreg - read an IO register
238  *
239  * @adev: amdgpu_device pointer
240  * @reg: dword aligned register offset
241  *
242  * Returns the 32 bit value from the offset specified.
243  */
244 u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 reg)
245 {
246 	if ((reg * 4) < adev->rio_mem_size)
247 		return bus_space_read_4(adev->rio_mem_bst, adev->rio_mem_bsh, reg);
248 	else {
249 		bus_space_write_4(adev->rio_mem_bst, adev->rio_mem_bsh,
250 		    mmMM_INDEX * 4, reg * 4);
251 		return bus_space_read_4(adev->rio_mem_bst, adev->rio_mem_bsh,
252 		    mmMM_INDEX * 4);
253 	}
254 }
255 
256 /**
257  * amdgpu_io_wreg - write to an IO register
258  *
259  * @adev: amdgpu_device pointer
260  * @reg: dword aligned register offset
261  * @v: 32 bit value to write to the register
262  *
263  * Writes the value specified to the offset specified.
264  */
265 void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
266 {
267 	if (adev->asic_type >= CHIP_VEGA10 && reg == 0) {
268 		adev->last_mm_index = v;
269 	}
270 
271 	if ((reg * 4) < adev->rio_mem_size)
272 		bus_space_write_4(adev->rio_mem_bst, adev->rio_mem_bsh,
273 		    reg * 4, v);
274 	else {
275 		bus_space_write_4(adev->rio_mem_bst, adev->rio_mem_bsh,
276 		    mmMM_INDEX * 4, reg * 4);
277 		bus_space_write_4(adev->rio_mem_bst, adev->rio_mem_bsh,
278 		    mmMM_DATA * 4, v);
279 
280 	}
281 
282 	if (adev->asic_type >= CHIP_VEGA10 && reg == 1 && adev->last_mm_index == 0x5702C) {
283 		udelay(500);
284 	}
285 }
286 
287 /**
288  * amdgpu_mm_rdoorbell - read a doorbell dword
289  *
290  * @adev: amdgpu_device pointer
291  * @index: doorbell index
292  *
293  * Returns the value in the doorbell aperture at the
294  * requested doorbell index (CIK).
295  */
296 u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
297 {
298 	if (index < adev->doorbell.num_doorbells) {
299 		return bus_space_read_4(adev->doorbell.bst, adev->doorbell.bsh,
300 		    index * 4);
301 	} else {
302 		DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
303 		return 0;
304 	}
305 }
306 
307 /**
308  * amdgpu_mm_wdoorbell - write a doorbell dword
309  *
310  * @adev: amdgpu_device pointer
311  * @index: doorbell index
312  * @v: value to write
313  *
314  * Writes @v to the doorbell aperture at the
315  * requested doorbell index (CIK).
316  */
317 void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
318 {
319 	if (index < adev->doorbell.num_doorbells) {
320 		bus_space_write_4(adev->doorbell.bst, adev->doorbell.bsh,
321 		    index * 4, v);
322 	} else {
323 		DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
324 	}
325 }
326 
327 /**
328  * amdgpu_mm_rdoorbell64 - read a doorbell Qword
329  *
330  * @adev: amdgpu_device pointer
331  * @index: doorbell index
332  *
333  * Returns the value in the doorbell aperture at the
334  * requested doorbell index (VEGA10+).
335  */
336 u64 amdgpu_mm_rdoorbell64(struct amdgpu_device *adev, u32 index)
337 {
338 	if (index < adev->doorbell.num_doorbells) {
339 		return bus_space_read_8(adev->doorbell.bst, adev->doorbell.bsh,
340 		    index * 4);
341 	} else {
342 		DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
343 		return 0;
344 	}
345 }
346 
347 /**
348  * amdgpu_mm_wdoorbell64 - write a doorbell Qword
349  *
350  * @adev: amdgpu_device pointer
351  * @index: doorbell index
352  * @v: value to write
353  *
354  * Writes @v to the doorbell aperture at the
355  * requested doorbell index (VEGA10+).
356  */
357 void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v)
358 {
359 	if (index < adev->doorbell.num_doorbells) {
360 		bus_space_write_8(adev->doorbell.bst, adev->doorbell.bsh,
361 		    index * 4, v);
362 	} else {
363 		DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
364 	}
365 }
366 
367 /**
368  * amdgpu_invalid_rreg - dummy reg read function
369  *
370  * @adev: amdgpu device pointer
371  * @reg: offset of register
372  *
373  * Dummy register read function.  Used for register blocks
374  * that certain asics don't have (all asics).
375  * Returns the value in the register.
376  */
377 static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
378 {
379 	DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
380 	BUG();
381 	return 0;
382 }
383 
384 /**
385  * amdgpu_invalid_wreg - dummy reg write function
386  *
387  * @adev: amdgpu device pointer
388  * @reg: offset of register
389  * @v: value to write to the register
390  *
391  * Dummy register read function.  Used for register blocks
392  * that certain asics don't have (all asics).
393  */
394 static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
395 {
396 	DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
397 		  reg, v);
398 	BUG();
399 }
400 
401 /**
402  * amdgpu_block_invalid_rreg - dummy reg read function
403  *
404  * @adev: amdgpu device pointer
405  * @block: offset of instance
406  * @reg: offset of register
407  *
408  * Dummy register read function.  Used for register blocks
409  * that certain asics don't have (all asics).
410  * Returns the value in the register.
411  */
412 static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
413 					  uint32_t block, uint32_t reg)
414 {
415 	DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
416 		  reg, block);
417 	BUG();
418 	return 0;
419 }
420 
421 /**
422  * amdgpu_block_invalid_wreg - dummy reg write function
423  *
424  * @adev: amdgpu device pointer
425  * @block: offset of instance
426  * @reg: offset of register
427  * @v: value to write to the register
428  *
429  * Dummy register read function.  Used for register blocks
430  * that certain asics don't have (all asics).
431  */
432 static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
433 				      uint32_t block,
434 				      uint32_t reg, uint32_t v)
435 {
436 	DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
437 		  reg, block, v);
438 	BUG();
439 }
440 
441 /**
442  * amdgpu_device_vram_scratch_init - allocate the VRAM scratch page
443  *
444  * @adev: amdgpu device pointer
445  *
446  * Allocates a scratch page of VRAM for use by various things in the
447  * driver.
448  */
449 static int amdgpu_device_vram_scratch_init(struct amdgpu_device *adev)
450 {
451 	return amdgpu_bo_create_kernel(adev, AMDGPU_GPU_PAGE_SIZE,
452 				       PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
453 				       &adev->vram_scratch.robj,
454 				       &adev->vram_scratch.gpu_addr,
455 				       (void **)&adev->vram_scratch.ptr);
456 }
457 
458 /**
459  * amdgpu_device_vram_scratch_fini - Free the VRAM scratch page
460  *
461  * @adev: amdgpu device pointer
462  *
463  * Frees the VRAM scratch page.
464  */
465 static void amdgpu_device_vram_scratch_fini(struct amdgpu_device *adev)
466 {
467 	amdgpu_bo_free_kernel(&adev->vram_scratch.robj, NULL, NULL);
468 }
469 
470 /**
471  * amdgpu_device_program_register_sequence - program an array of registers.
472  *
473  * @adev: amdgpu_device pointer
474  * @registers: pointer to the register array
475  * @array_size: size of the register array
476  *
477  * Programs an array or registers with and and or masks.
478  * This is a helper for setting golden registers.
479  */
480 void amdgpu_device_program_register_sequence(struct amdgpu_device *adev,
481 					     const u32 *registers,
482 					     const u32 array_size)
483 {
484 	u32 tmp, reg, and_mask, or_mask;
485 	int i;
486 
487 	if (array_size % 3)
488 		return;
489 
490 	for (i = 0; i < array_size; i +=3) {
491 		reg = registers[i + 0];
492 		and_mask = registers[i + 1];
493 		or_mask = registers[i + 2];
494 
495 		if (and_mask == 0xffffffff) {
496 			tmp = or_mask;
497 		} else {
498 			tmp = RREG32(reg);
499 			tmp &= ~and_mask;
500 			tmp |= or_mask;
501 		}
502 		WREG32(reg, tmp);
503 	}
504 }
505 
506 /**
507  * amdgpu_device_pci_config_reset - reset the GPU
508  *
509  * @adev: amdgpu_device pointer
510  *
511  * Resets the GPU using the pci config reset sequence.
512  * Only applicable to asics prior to vega10.
513  */
514 void amdgpu_device_pci_config_reset(struct amdgpu_device *adev)
515 {
516 	pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
517 }
518 
519 /*
520  * GPU doorbell aperture helpers function.
521  */
522 /**
523  * amdgpu_device_doorbell_init - Init doorbell driver information.
524  *
525  * @adev: amdgpu_device pointer
526  *
527  * Init doorbell driver information (CIK)
528  * Returns 0 on success, error on failure.
529  */
530 static int amdgpu_device_doorbell_init(struct amdgpu_device *adev)
531 {
532 	/* No doorbell on SI hardware generation */
533 	if (adev->asic_type < CHIP_BONAIRE) {
534 		adev->doorbell.base = 0;
535 		adev->doorbell.size = 0;
536 		adev->doorbell.num_doorbells = 0;
537 #ifdef __linux__
538 		adev->doorbell.ptr = NULL;
539 #endif
540 		return 0;
541 	}
542 
543 #ifdef __linux__
544 	if (pci_resource_flags(adev->pdev, 2) & IORESOURCE_UNSET)
545 		return -EINVAL;
546 
547 	/* doorbell bar mapping */
548 	adev->doorbell.base = pci_resource_start(adev->pdev, 2);
549 	adev->doorbell.size = pci_resource_len(adev->pdev, 2);
550 #endif
551 
552 	adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
553 					     AMDGPU_DOORBELL_MAX_ASSIGNMENT+1);
554 	if (adev->doorbell.num_doorbells == 0)
555 		return -EINVAL;
556 
557 #ifdef __linux__
558 	adev->doorbell.ptr = ioremap(adev->doorbell.base,
559 				     adev->doorbell.num_doorbells *
560 				     sizeof(u32));
561 	if (adev->doorbell.ptr == NULL)
562 		return -ENOMEM;
563 #endif
564 
565 	return 0;
566 }
567 
568 /**
569  * amdgpu_device_doorbell_fini - Tear down doorbell driver information.
570  *
571  * @adev: amdgpu_device pointer
572  *
573  * Tear down doorbell driver information (CIK)
574  */
575 static void amdgpu_device_doorbell_fini(struct amdgpu_device *adev)
576 {
577 #ifdef __linux__
578 	iounmap(adev->doorbell.ptr);
579 	adev->doorbell.ptr = NULL;
580 #else
581 	if (adev->doorbell.size > 0)
582 		bus_space_unmap(adev->doorbell.bst, adev->doorbell.bsh,
583 		    adev->doorbell.size);
584 #endif
585 }
586 
587 
588 
589 /*
590  * amdgpu_device_wb_*()
591  * Writeback is the method by which the GPU updates special pages in memory
592  * with the status of certain GPU events (fences, ring pointers,etc.).
593  */
594 
595 /**
596  * amdgpu_device_wb_fini - Disable Writeback and free memory
597  *
598  * @adev: amdgpu_device pointer
599  *
600  * Disables Writeback and frees the Writeback memory (all asics).
601  * Used at driver shutdown.
602  */
603 static void amdgpu_device_wb_fini(struct amdgpu_device *adev)
604 {
605 	if (adev->wb.wb_obj) {
606 		amdgpu_bo_free_kernel(&adev->wb.wb_obj,
607 				      &adev->wb.gpu_addr,
608 				      (void **)&adev->wb.wb);
609 		adev->wb.wb_obj = NULL;
610 	}
611 }
612 
613 /**
614  * amdgpu_device_wb_init- Init Writeback driver info and allocate memory
615  *
616  * @adev: amdgpu_device pointer
617  *
618  * Initializes writeback and allocates writeback memory (all asics).
619  * Used at driver startup.
620  * Returns 0 on success or an -error on failure.
621  */
622 static int amdgpu_device_wb_init(struct amdgpu_device *adev)
623 {
624 	int r;
625 
626 	if (adev->wb.wb_obj == NULL) {
627 		/* AMDGPU_MAX_WB * sizeof(uint32_t) * 8 = AMDGPU_MAX_WB 256bit slots */
628 		r = amdgpu_bo_create_kernel(adev, AMDGPU_MAX_WB * sizeof(uint32_t) * 8,
629 					    PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
630 					    &adev->wb.wb_obj, &adev->wb.gpu_addr,
631 					    (void **)&adev->wb.wb);
632 		if (r) {
633 			dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
634 			return r;
635 		}
636 
637 		adev->wb.num_wb = AMDGPU_MAX_WB;
638 		memset(&adev->wb.used, 0, sizeof(adev->wb.used));
639 
640 		/* clear wb memory */
641 		memset((char *)adev->wb.wb, 0, AMDGPU_MAX_WB * sizeof(uint32_t) * 8);
642 	}
643 
644 	return 0;
645 }
646 
647 /**
648  * amdgpu_device_wb_get - Allocate a wb entry
649  *
650  * @adev: amdgpu_device pointer
651  * @wb: wb index
652  *
653  * Allocate a wb slot for use by the driver (all asics).
654  * Returns 0 on success or -EINVAL on failure.
655  */
656 int amdgpu_device_wb_get(struct amdgpu_device *adev, u32 *wb)
657 {
658 	unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
659 
660 	if (offset < adev->wb.num_wb) {
661 		__set_bit(offset, adev->wb.used);
662 		*wb = offset << 3; /* convert to dw offset */
663 		return 0;
664 	} else {
665 		return -EINVAL;
666 	}
667 }
668 
669 /**
670  * amdgpu_device_wb_free - Free a wb entry
671  *
672  * @adev: amdgpu_device pointer
673  * @wb: wb index
674  *
675  * Free a wb slot allocated for use by the driver (all asics)
676  */
677 void amdgpu_device_wb_free(struct amdgpu_device *adev, u32 wb)
678 {
679 	wb >>= 3;
680 	if (wb < adev->wb.num_wb)
681 		__clear_bit(wb, adev->wb.used);
682 }
683 
684 /**
685  * amdgpu_device_vram_location - try to find VRAM location
686  *
687  * @adev: amdgpu device structure holding all necessary informations
688  * @mc: memory controller structure holding memory informations
689  * @base: base address at which to put VRAM
690  *
691  * Function will try to place VRAM at base address provided
692  * as parameter.
693  */
694 void amdgpu_device_vram_location(struct amdgpu_device *adev,
695 				 struct amdgpu_gmc *mc, u64 base)
696 {
697 	uint64_t limit = (uint64_t)amdgpu_vram_limit << 20;
698 
699 	mc->vram_start = base;
700 	mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
701 	if (limit && limit < mc->real_vram_size)
702 		mc->real_vram_size = limit;
703 	dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
704 			mc->mc_vram_size >> 20, mc->vram_start,
705 			mc->vram_end, mc->real_vram_size >> 20);
706 }
707 
708 /**
709  * amdgpu_device_gart_location - try to find GART location
710  *
711  * @adev: amdgpu device structure holding all necessary informations
712  * @mc: memory controller structure holding memory informations
713  *
714  * Function will place try to place GART before or after VRAM.
715  *
716  * If GART size is bigger than space left then we ajust GART size.
717  * Thus function will never fails.
718  */
719 void amdgpu_device_gart_location(struct amdgpu_device *adev,
720 				 struct amdgpu_gmc *mc)
721 {
722 	u64 size_af, size_bf;
723 
724 	mc->gart_size += adev->pm.smu_prv_buffer_size;
725 
726 	size_af = adev->gmc.mc_mask - mc->vram_end;
727 	size_bf = mc->vram_start;
728 	if (size_bf > size_af) {
729 		if (mc->gart_size > size_bf) {
730 			dev_warn(adev->dev, "limiting GART\n");
731 			mc->gart_size = size_bf;
732 		}
733 		mc->gart_start = 0;
734 	} else {
735 		if (mc->gart_size > size_af) {
736 			dev_warn(adev->dev, "limiting GART\n");
737 			mc->gart_size = size_af;
738 		}
739 		/* VCE doesn't like it when BOs cross a 4GB segment, so align
740 		 * the GART base on a 4GB boundary as well.
741 		 */
742 		mc->gart_start = roundup2(mc->vram_end + 1, 0x100000000ULL);
743 	}
744 	mc->gart_end = mc->gart_start + mc->gart_size - 1;
745 	dev_info(adev->dev, "GART: %lluM 0x%016llX - 0x%016llX\n",
746 			mc->gart_size >> 20, mc->gart_start, mc->gart_end);
747 }
748 
749 /**
750  * amdgpu_device_resize_fb_bar - try to resize FB BAR
751  *
752  * @adev: amdgpu_device pointer
753  *
754  * Try to resize FB BAR to make all VRAM CPU accessible. We try very hard not
755  * to fail, but if any of the BARs is not accessible after the size we abort
756  * driver loading by returning -ENODEV.
757  */
758 int amdgpu_device_resize_fb_bar(struct amdgpu_device *adev)
759 {
760 	u64 space_needed = roundup_pow_of_two(adev->gmc.real_vram_size);
761 	u32 rbar_size = order_base_2(((space_needed >> 20) | 1)) - 1;
762 	struct pci_bus *root;
763 	struct resource *res;
764 	unsigned i;
765 	u16 cmd;
766 	int r;
767 	pcireg_t type;
768 
769 	/* XXX not right yet */
770 	STUB();
771 	return 0;
772 
773 	/* Bypass for VF */
774 	if (amdgpu_sriov_vf(adev))
775 		return 0;
776 #ifdef notyet
777 
778 	/* Check if the root BUS has 64bit memory resources */
779 	root = adev->pdev->bus;
780 	while (root->parent)
781 		root = root->parent;
782 
783 	pci_bus_for_each_resource(root, res, i) {
784 		if (res && res->flags & (IORESOURCE_MEM | IORESOURCE_MEM_64) &&
785 		    res->start > 0x100000000ull)
786 			break;
787 	}
788 
789 	/* Trying to resize is pointless without a root hub window above 4GB */
790 	if (!res)
791 		return 0;
792 #endif
793 
794 	/* Disable memory decoding while we change the BAR addresses and size */
795 	pci_read_config_word(adev->pdev, PCI_COMMAND, &cmd);
796 	pci_write_config_word(adev->pdev, PCI_COMMAND,
797 			      cmd & ~PCI_COMMAND_MEMORY);
798 
799 	/* Free the VRAM and doorbell BAR, we most likely need to move both. */
800 	amdgpu_device_doorbell_fini(adev);
801 #ifdef __linux__
802 	if (adev->asic_type >= CHIP_BONAIRE)
803 		pci_release_resource(adev->pdev, 2);
804 
805 	pci_release_resource(adev->pdev, 0);
806 #endif
807 
808 	r = pci_resize_resource(adev->pdev, 0, rbar_size);
809 	if (r == -ENOSPC)
810 		DRM_INFO("Not enough PCI address space for a large BAR.");
811 	else if (r && r != -ENOTSUPP)
812 		DRM_ERROR("Problem resizing BAR0 (%d).", r);
813 
814 #ifdef __linux__
815 	pci_assign_unassigned_bus_resources(adev->pdev->bus);
816 #else
817 #define AMDGPU_PCI_MEM		0x10
818 
819 	type = pci_mapreg_type(adev->pc, adev->pa_tag, AMDGPU_PCI_MEM);
820 	if (PCI_MAPREG_TYPE(type) != PCI_MAPREG_TYPE_MEM ||
821 	    pci_mapreg_info(adev->pc, adev->pa_tag, AMDGPU_PCI_MEM,
822 	    type, NULL, &adev->fb_aper_size, NULL)) {
823 		printf(": can't get frambuffer info\n");
824 		return -ENODEV;
825 	}
826 #endif
827 	/* When the doorbell or fb BAR isn't available we have no chance of
828 	 * using the device.
829 	 */
830 	r = amdgpu_device_doorbell_init(adev);
831 #ifdef notyet
832 	if (r || (pci_resource_flags(adev->pdev, 0) & IORESOURCE_UNSET))
833 #else
834 	if (r)
835 #endif
836 		return -ENODEV;
837 
838 	pci_write_config_word(adev->pdev, PCI_COMMAND, cmd);
839 
840 	return 0;
841 }
842 
843 /*
844  * GPU helpers function.
845  */
846 /**
847  * amdgpu_device_need_post - check if the hw need post or not
848  *
849  * @adev: amdgpu_device pointer
850  *
851  * Check if the asic has been initialized (all asics) at driver startup
852  * or post is needed if  hw reset is performed.
853  * Returns true if need or false if not.
854  */
855 bool amdgpu_device_need_post(struct amdgpu_device *adev)
856 {
857 	uint32_t reg;
858 
859 	if (amdgpu_sriov_vf(adev))
860 		return false;
861 
862 	if (amdgpu_passthrough(adev)) {
863 		/* for FIJI: In whole GPU pass-through virtualization case, after VM reboot
864 		 * some old smc fw still need driver do vPost otherwise gpu hang, while
865 		 * those smc fw version above 22.15 doesn't have this flaw, so we force
866 		 * vpost executed for smc version below 22.15
867 		 */
868 		if (adev->asic_type == CHIP_FIJI) {
869 			int err;
870 			uint32_t fw_ver;
871 			err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
872 			/* force vPost if error occured */
873 			if (err)
874 				return true;
875 
876 			fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
877 			if (fw_ver < 0x00160e00)
878 				return true;
879 		}
880 	}
881 
882 	if (adev->has_hw_reset) {
883 		adev->has_hw_reset = false;
884 		return true;
885 	}
886 
887 	/* bios scratch used on CIK+ */
888 	if (adev->asic_type >= CHIP_BONAIRE)
889 		return amdgpu_atombios_scratch_need_asic_init(adev);
890 
891 	/* check MEM_SIZE for older asics */
892 	reg = amdgpu_asic_get_config_memsize(adev);
893 
894 	if ((reg != 0) && (reg != 0xffffffff))
895 		return false;
896 
897 	return true;
898 }
899 
900 /* if we get transitioned to only one device, take VGA back */
901 /**
902  * amdgpu_device_vga_set_decode - enable/disable vga decode
903  *
904  * @cookie: amdgpu_device pointer
905  * @state: enable/disable vga decode
906  *
907  * Enable/disable vga decode (all asics).
908  * Returns VGA resource flags.
909  */
910 #ifdef notyet
911 static unsigned int amdgpu_device_vga_set_decode(void *cookie, bool state)
912 {
913 	struct amdgpu_device *adev = cookie;
914 	amdgpu_asic_set_vga_state(adev, state);
915 	if (state)
916 		return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
917 		       VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
918 	else
919 		return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
920 }
921 #endif
922 
923 /**
924  * amdgpu_device_check_block_size - validate the vm block size
925  *
926  * @adev: amdgpu_device pointer
927  *
928  * Validates the vm block size specified via module parameter.
929  * The vm block size defines number of bits in page table versus page directory,
930  * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
931  * page table and the remaining bits are in the page directory.
932  */
933 static void amdgpu_device_check_block_size(struct amdgpu_device *adev)
934 {
935 	/* defines number of bits in page table versus page directory,
936 	 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
937 	 * page table and the remaining bits are in the page directory */
938 	if (amdgpu_vm_block_size == -1)
939 		return;
940 
941 	if (amdgpu_vm_block_size < 9) {
942 		dev_warn(adev->dev, "VM page table size (%d) too small\n",
943 			 amdgpu_vm_block_size);
944 		amdgpu_vm_block_size = -1;
945 	}
946 }
947 
948 /**
949  * amdgpu_device_check_vm_size - validate the vm size
950  *
951  * @adev: amdgpu_device pointer
952  *
953  * Validates the vm size in GB specified via module parameter.
954  * The VM size is the size of the GPU virtual memory space in GB.
955  */
956 static void amdgpu_device_check_vm_size(struct amdgpu_device *adev)
957 {
958 	/* no need to check the default value */
959 	if (amdgpu_vm_size == -1)
960 		return;
961 
962 	if (amdgpu_vm_size < 1) {
963 		dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
964 			 amdgpu_vm_size);
965 		amdgpu_vm_size = -1;
966 	}
967 }
968 
969 static void amdgpu_device_check_smu_prv_buffer_size(struct amdgpu_device *adev)
970 {
971 #ifdef __linux__
972 	struct sysinfo si;
973 #endif
974 	bool is_os_64 = (sizeof(void *) == 8) ? true : false;
975 	uint64_t total_memory;
976 	uint64_t dram_size_seven_GB = 0x1B8000000;
977 	uint64_t dram_size_three_GB = 0xB8000000;
978 
979 	if (amdgpu_smu_memory_pool_size == 0)
980 		return;
981 
982 	if (!is_os_64) {
983 		DRM_WARN("Not 64-bit OS, feature not supported\n");
984 		goto def_value;
985 	}
986 #ifdef __linux__
987 	si_meminfo(&si);
988 	total_memory = (uint64_t)si.totalram * si.mem_unit;
989 #else
990 	total_memory = ptoa(physmem);
991 #endif
992 
993 	if ((amdgpu_smu_memory_pool_size == 1) ||
994 		(amdgpu_smu_memory_pool_size == 2)) {
995 		if (total_memory < dram_size_three_GB)
996 			goto def_value1;
997 	} else if ((amdgpu_smu_memory_pool_size == 4) ||
998 		(amdgpu_smu_memory_pool_size == 8)) {
999 		if (total_memory < dram_size_seven_GB)
1000 			goto def_value1;
1001 	} else {
1002 		DRM_WARN("Smu memory pool size not supported\n");
1003 		goto def_value;
1004 	}
1005 	adev->pm.smu_prv_buffer_size = amdgpu_smu_memory_pool_size << 28;
1006 
1007 	return;
1008 
1009 def_value1:
1010 	DRM_WARN("No enough system memory\n");
1011 def_value:
1012 	adev->pm.smu_prv_buffer_size = 0;
1013 }
1014 
1015 /**
1016  * amdgpu_device_check_arguments - validate module params
1017  *
1018  * @adev: amdgpu_device pointer
1019  *
1020  * Validates certain module parameters and updates
1021  * the associated values used by the driver (all asics).
1022  */
1023 static void amdgpu_device_check_arguments(struct amdgpu_device *adev)
1024 {
1025 	if (amdgpu_sched_jobs < 4) {
1026 		dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
1027 			 amdgpu_sched_jobs);
1028 		amdgpu_sched_jobs = 4;
1029 	} else if (!is_power_of_2(amdgpu_sched_jobs)){
1030 		dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
1031 			 amdgpu_sched_jobs);
1032 		amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
1033 	}
1034 
1035 	if (amdgpu_gart_size != -1 && amdgpu_gart_size < 32) {
1036 		/* gart size must be greater or equal to 32M */
1037 		dev_warn(adev->dev, "gart size (%d) too small\n",
1038 			 amdgpu_gart_size);
1039 		amdgpu_gart_size = -1;
1040 	}
1041 
1042 	if (amdgpu_gtt_size != -1 && amdgpu_gtt_size < 32) {
1043 		/* gtt size must be greater or equal to 32M */
1044 		dev_warn(adev->dev, "gtt size (%d) too small\n",
1045 				 amdgpu_gtt_size);
1046 		amdgpu_gtt_size = -1;
1047 	}
1048 
1049 	/* valid range is between 4 and 9 inclusive */
1050 	if (amdgpu_vm_fragment_size != -1 &&
1051 	    (amdgpu_vm_fragment_size > 9 || amdgpu_vm_fragment_size < 4)) {
1052 		dev_warn(adev->dev, "valid range is between 4 and 9\n");
1053 		amdgpu_vm_fragment_size = -1;
1054 	}
1055 
1056 	amdgpu_device_check_smu_prv_buffer_size(adev);
1057 
1058 	amdgpu_device_check_vm_size(adev);
1059 
1060 	amdgpu_device_check_block_size(adev);
1061 
1062 	if (amdgpu_vram_page_split != -1 && (amdgpu_vram_page_split < 16 ||
1063 	    !is_power_of_2(amdgpu_vram_page_split))) {
1064 		dev_warn(adev->dev, "invalid VRAM page split (%d)\n",
1065 			 amdgpu_vram_page_split);
1066 		amdgpu_vram_page_split = 1024;
1067 	}
1068 
1069 	if (amdgpu_lockup_timeout == 0) {
1070 		dev_warn(adev->dev, "lockup_timeout msut be > 0, adjusting to 10000\n");
1071 		amdgpu_lockup_timeout = 10000;
1072 	}
1073 
1074 	adev->firmware.load_type = amdgpu_ucode_get_load_type(adev, amdgpu_fw_load_type);
1075 }
1076 
1077 #ifdef __linux__
1078 /**
1079  * amdgpu_switcheroo_set_state - set switcheroo state
1080  *
1081  * @pdev: pci dev pointer
1082  * @state: vga_switcheroo state
1083  *
1084  * Callback for the switcheroo driver.  Suspends or resumes the
1085  * the asics before or after it is powered up using ACPI methods.
1086  */
1087 static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1088 {
1089 	struct drm_device *dev = pci_get_drvdata(pdev);
1090 
1091 	if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
1092 		return;
1093 
1094 	if (state == VGA_SWITCHEROO_ON) {
1095 		pr_info("amdgpu: switched on\n");
1096 		/* don't suspend or resume card normally */
1097 		dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1098 
1099 		amdgpu_device_resume(dev, true, true);
1100 
1101 		dev->switch_power_state = DRM_SWITCH_POWER_ON;
1102 		drm_kms_helper_poll_enable(dev);
1103 	} else {
1104 		pr_info("amdgpu: switched off\n");
1105 		drm_kms_helper_poll_disable(dev);
1106 		dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1107 		amdgpu_device_suspend(dev, true, true);
1108 		dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1109 	}
1110 }
1111 
1112 /**
1113  * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1114  *
1115  * @pdev: pci dev pointer
1116  *
1117  * Callback for the switcheroo driver.  Check of the switcheroo
1118  * state can be changed.
1119  * Returns true if the state can be changed, false if not.
1120  */
1121 static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1122 {
1123 	struct drm_device *dev = pci_get_drvdata(pdev);
1124 
1125 	/*
1126 	* FIXME: open_count is protected by drm_global_mutex but that would lead to
1127 	* locking inversion with the driver load path. And the access here is
1128 	* completely racy anyway. So don't bother with locking for now.
1129 	*/
1130 	return dev->open_count == 0;
1131 }
1132 
1133 static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1134 	.set_gpu_state = amdgpu_switcheroo_set_state,
1135 	.reprobe = NULL,
1136 	.can_switch = amdgpu_switcheroo_can_switch,
1137 };
1138 #endif /* __linux__ */
1139 
1140 /**
1141  * amdgpu_device_ip_set_clockgating_state - set the CG state
1142  *
1143  * @dev: amdgpu_device pointer
1144  * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1145  * @state: clockgating state (gate or ungate)
1146  *
1147  * Sets the requested clockgating state for all instances of
1148  * the hardware IP specified.
1149  * Returns the error code from the last instance.
1150  */
1151 int amdgpu_device_ip_set_clockgating_state(void *dev,
1152 					   enum amd_ip_block_type block_type,
1153 					   enum amd_clockgating_state state)
1154 {
1155 	struct amdgpu_device *adev = dev;
1156 	int i, r = 0;
1157 
1158 	for (i = 0; i < adev->num_ip_blocks; i++) {
1159 		if (!adev->ip_blocks[i].status.valid)
1160 			continue;
1161 		if (adev->ip_blocks[i].version->type != block_type)
1162 			continue;
1163 		if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
1164 			continue;
1165 		r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
1166 			(void *)adev, state);
1167 		if (r)
1168 			DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
1169 				  adev->ip_blocks[i].version->funcs->name, r);
1170 	}
1171 	return r;
1172 }
1173 
1174 /**
1175  * amdgpu_device_ip_set_powergating_state - set the PG state
1176  *
1177  * @dev: amdgpu_device pointer
1178  * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1179  * @state: powergating state (gate or ungate)
1180  *
1181  * Sets the requested powergating state for all instances of
1182  * the hardware IP specified.
1183  * Returns the error code from the last instance.
1184  */
1185 int amdgpu_device_ip_set_powergating_state(void *dev,
1186 					   enum amd_ip_block_type block_type,
1187 					   enum amd_powergating_state state)
1188 {
1189 	struct amdgpu_device *adev = dev;
1190 	int i, r = 0;
1191 
1192 	for (i = 0; i < adev->num_ip_blocks; i++) {
1193 		if (!adev->ip_blocks[i].status.valid)
1194 			continue;
1195 		if (adev->ip_blocks[i].version->type != block_type)
1196 			continue;
1197 		if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
1198 			continue;
1199 		r = adev->ip_blocks[i].version->funcs->set_powergating_state(
1200 			(void *)adev, state);
1201 		if (r)
1202 			DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
1203 				  adev->ip_blocks[i].version->funcs->name, r);
1204 	}
1205 	return r;
1206 }
1207 
1208 /**
1209  * amdgpu_device_ip_get_clockgating_state - get the CG state
1210  *
1211  * @adev: amdgpu_device pointer
1212  * @flags: clockgating feature flags
1213  *
1214  * Walks the list of IPs on the device and updates the clockgating
1215  * flags for each IP.
1216  * Updates @flags with the feature flags for each hardware IP where
1217  * clockgating is enabled.
1218  */
1219 void amdgpu_device_ip_get_clockgating_state(struct amdgpu_device *adev,
1220 					    u32 *flags)
1221 {
1222 	int i;
1223 
1224 	for (i = 0; i < adev->num_ip_blocks; i++) {
1225 		if (!adev->ip_blocks[i].status.valid)
1226 			continue;
1227 		if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
1228 			adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
1229 	}
1230 }
1231 
1232 /**
1233  * amdgpu_device_ip_wait_for_idle - wait for idle
1234  *
1235  * @adev: amdgpu_device pointer
1236  * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1237  *
1238  * Waits for the request hardware IP to be idle.
1239  * Returns 0 for success or a negative error code on failure.
1240  */
1241 int amdgpu_device_ip_wait_for_idle(struct amdgpu_device *adev,
1242 				   enum amd_ip_block_type block_type)
1243 {
1244 	int i, r;
1245 
1246 	for (i = 0; i < adev->num_ip_blocks; i++) {
1247 		if (!adev->ip_blocks[i].status.valid)
1248 			continue;
1249 		if (adev->ip_blocks[i].version->type == block_type) {
1250 			r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
1251 			if (r)
1252 				return r;
1253 			break;
1254 		}
1255 	}
1256 	return 0;
1257 
1258 }
1259 
1260 /**
1261  * amdgpu_device_ip_is_idle - is the hardware IP idle
1262  *
1263  * @adev: amdgpu_device pointer
1264  * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1265  *
1266  * Check if the hardware IP is idle or not.
1267  * Returns true if it the IP is idle, false if not.
1268  */
1269 bool amdgpu_device_ip_is_idle(struct amdgpu_device *adev,
1270 			      enum amd_ip_block_type block_type)
1271 {
1272 	int i;
1273 
1274 	for (i = 0; i < adev->num_ip_blocks; i++) {
1275 		if (!adev->ip_blocks[i].status.valid)
1276 			continue;
1277 		if (adev->ip_blocks[i].version->type == block_type)
1278 			return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
1279 	}
1280 	return true;
1281 
1282 }
1283 
1284 /**
1285  * amdgpu_device_ip_get_ip_block - get a hw IP pointer
1286  *
1287  * @adev: amdgpu_device pointer
1288  * @type: Type of hardware IP (SMU, GFX, UVD, etc.)
1289  *
1290  * Returns a pointer to the hardware IP block structure
1291  * if it exists for the asic, otherwise NULL.
1292  */
1293 struct amdgpu_ip_block *
1294 amdgpu_device_ip_get_ip_block(struct amdgpu_device *adev,
1295 			      enum amd_ip_block_type type)
1296 {
1297 	int i;
1298 
1299 	for (i = 0; i < adev->num_ip_blocks; i++)
1300 		if (adev->ip_blocks[i].version->type == type)
1301 			return &adev->ip_blocks[i];
1302 
1303 	return NULL;
1304 }
1305 
1306 /**
1307  * amdgpu_device_ip_block_version_cmp
1308  *
1309  * @adev: amdgpu_device pointer
1310  * @type: enum amd_ip_block_type
1311  * @major: major version
1312  * @minor: minor version
1313  *
1314  * return 0 if equal or greater
1315  * return 1 if smaller or the ip_block doesn't exist
1316  */
1317 int amdgpu_device_ip_block_version_cmp(struct amdgpu_device *adev,
1318 				       enum amd_ip_block_type type,
1319 				       u32 major, u32 minor)
1320 {
1321 	struct amdgpu_ip_block *ip_block = amdgpu_device_ip_get_ip_block(adev, type);
1322 
1323 	if (ip_block && ((ip_block->version->major > major) ||
1324 			((ip_block->version->major == major) &&
1325 			(ip_block->version->minor >= minor))))
1326 		return 0;
1327 
1328 	return 1;
1329 }
1330 
1331 /**
1332  * amdgpu_device_ip_block_add
1333  *
1334  * @adev: amdgpu_device pointer
1335  * @ip_block_version: pointer to the IP to add
1336  *
1337  * Adds the IP block driver information to the collection of IPs
1338  * on the asic.
1339  */
1340 int amdgpu_device_ip_block_add(struct amdgpu_device *adev,
1341 			       const struct amdgpu_ip_block_version *ip_block_version)
1342 {
1343 	if (!ip_block_version)
1344 		return -EINVAL;
1345 
1346 	DRM_INFO("add ip block number %d <%s>\n", adev->num_ip_blocks,
1347 		  ip_block_version->funcs->name);
1348 
1349 	adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
1350 
1351 	return 0;
1352 }
1353 
1354 /**
1355  * amdgpu_device_enable_virtual_display - enable virtual display feature
1356  *
1357  * @adev: amdgpu_device pointer
1358  *
1359  * Enabled the virtual display feature if the user has enabled it via
1360  * the module parameter virtual_display.  This feature provides a virtual
1361  * display hardware on headless boards or in virtualized environments.
1362  * This function parses and validates the configuration string specified by
1363  * the user and configues the virtual display configuration (number of
1364  * virtual connectors, crtcs, etc.) specified.
1365  */
1366 static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
1367 {
1368 	adev->enable_virtual_display = false;
1369 
1370 #ifdef notyet
1371 	if (amdgpu_virtual_display) {
1372 		struct drm_device *ddev = adev->ddev;
1373 		const char *pci_address_name = pci_name(ddev->pdev);
1374 		char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
1375 
1376 		pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1377 		pciaddstr_tmp = pciaddstr;
1378 		while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1379 			pciaddname = strsep(&pciaddname_tmp, ",");
1380 			if (!strcmp("all", pciaddname)
1381 			    || !strcmp(pci_address_name, pciaddname)) {
1382 				long num_crtc;
1383 				int res = -1;
1384 
1385 				adev->enable_virtual_display = true;
1386 
1387 				if (pciaddname_tmp)
1388 					res = kstrtol(pciaddname_tmp, 10,
1389 						      &num_crtc);
1390 
1391 				if (!res) {
1392 					if (num_crtc < 1)
1393 						num_crtc = 1;
1394 					if (num_crtc > 6)
1395 						num_crtc = 6;
1396 					adev->mode_info.num_crtc = num_crtc;
1397 				} else {
1398 					adev->mode_info.num_crtc = 1;
1399 				}
1400 				break;
1401 			}
1402 		}
1403 
1404 		DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
1405 			 amdgpu_virtual_display, pci_address_name,
1406 			 adev->enable_virtual_display, adev->mode_info.num_crtc);
1407 
1408 		kfree(pciaddstr);
1409 	}
1410 #endif
1411 }
1412 
1413 /**
1414  * amdgpu_device_parse_gpu_info_fw - parse gpu info firmware
1415  *
1416  * @adev: amdgpu_device pointer
1417  *
1418  * Parses the asic configuration parameters specified in the gpu info
1419  * firmware and makes them availale to the driver for use in configuring
1420  * the asic.
1421  * Returns 0 on success, -EINVAL on failure.
1422  */
1423 static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev)
1424 {
1425 	const char *chip_name;
1426 	char fw_name[30];
1427 	int err;
1428 	const struct gpu_info_firmware_header_v1_0 *hdr;
1429 
1430 	adev->firmware.gpu_info_fw = NULL;
1431 
1432 	switch (adev->asic_type) {
1433 	case CHIP_TOPAZ:
1434 	case CHIP_TONGA:
1435 	case CHIP_FIJI:
1436 	case CHIP_POLARIS10:
1437 	case CHIP_POLARIS11:
1438 	case CHIP_POLARIS12:
1439 	case CHIP_VEGAM:
1440 	case CHIP_CARRIZO:
1441 	case CHIP_STONEY:
1442 #ifdef CONFIG_DRM_AMDGPU_SI
1443 	case CHIP_VERDE:
1444 	case CHIP_TAHITI:
1445 	case CHIP_PITCAIRN:
1446 	case CHIP_OLAND:
1447 	case CHIP_HAINAN:
1448 #endif
1449 #ifdef CONFIG_DRM_AMDGPU_CIK
1450 	case CHIP_BONAIRE:
1451 	case CHIP_HAWAII:
1452 	case CHIP_KAVERI:
1453 	case CHIP_KABINI:
1454 	case CHIP_MULLINS:
1455 #endif
1456 	case CHIP_VEGA20:
1457 	default:
1458 		return 0;
1459 	case CHIP_VEGA10:
1460 		chip_name = "vega10";
1461 		break;
1462 	case CHIP_VEGA12:
1463 		chip_name = "vega12";
1464 		break;
1465 	case CHIP_RAVEN:
1466 		chip_name = "raven";
1467 		break;
1468 	}
1469 
1470 	snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name);
1471 	err = request_firmware(&adev->firmware.gpu_info_fw, fw_name, adev->dev);
1472 	if (err) {
1473 		dev_err(adev->dev,
1474 			"Failed to load gpu_info firmware \"%s\"\n",
1475 			fw_name);
1476 		goto out;
1477 	}
1478 	err = amdgpu_ucode_validate(adev->firmware.gpu_info_fw);
1479 	if (err) {
1480 		dev_err(adev->dev,
1481 			"Failed to validate gpu_info firmware \"%s\"\n",
1482 			fw_name);
1483 		goto out;
1484 	}
1485 
1486 	hdr = (const struct gpu_info_firmware_header_v1_0 *)adev->firmware.gpu_info_fw->data;
1487 	amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
1488 
1489 	switch (hdr->version_major) {
1490 	case 1:
1491 	{
1492 		const struct gpu_info_firmware_v1_0 *gpu_info_fw =
1493 			(const struct gpu_info_firmware_v1_0 *)(adev->firmware.gpu_info_fw->data +
1494 								le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1495 
1496 		adev->gfx.config.max_shader_engines = le32_to_cpu(gpu_info_fw->gc_num_se);
1497 		adev->gfx.config.max_cu_per_sh = le32_to_cpu(gpu_info_fw->gc_num_cu_per_sh);
1498 		adev->gfx.config.max_sh_per_se = le32_to_cpu(gpu_info_fw->gc_num_sh_per_se);
1499 		adev->gfx.config.max_backends_per_se = le32_to_cpu(gpu_info_fw->gc_num_rb_per_se);
1500 		adev->gfx.config.max_texture_channel_caches =
1501 			le32_to_cpu(gpu_info_fw->gc_num_tccs);
1502 		adev->gfx.config.max_gprs = le32_to_cpu(gpu_info_fw->gc_num_gprs);
1503 		adev->gfx.config.max_gs_threads = le32_to_cpu(gpu_info_fw->gc_num_max_gs_thds);
1504 		adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gpu_info_fw->gc_gs_table_depth);
1505 		adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gpu_info_fw->gc_gsprim_buff_depth);
1506 		adev->gfx.config.double_offchip_lds_buf =
1507 			le32_to_cpu(gpu_info_fw->gc_double_offchip_lds_buffer);
1508 		adev->gfx.cu_info.wave_front_size = le32_to_cpu(gpu_info_fw->gc_wave_size);
1509 		adev->gfx.cu_info.max_waves_per_simd =
1510 			le32_to_cpu(gpu_info_fw->gc_max_waves_per_simd);
1511 		adev->gfx.cu_info.max_scratch_slots_per_cu =
1512 			le32_to_cpu(gpu_info_fw->gc_max_scratch_slots_per_cu);
1513 		adev->gfx.cu_info.lds_size = le32_to_cpu(gpu_info_fw->gc_lds_size);
1514 		break;
1515 	}
1516 	default:
1517 		dev_err(adev->dev,
1518 			"Unsupported gpu_info table %d\n", hdr->header.ucode_version);
1519 		err = -EINVAL;
1520 		goto out;
1521 	}
1522 out:
1523 	return err;
1524 }
1525 
1526 /**
1527  * amdgpu_device_ip_early_init - run early init for hardware IPs
1528  *
1529  * @adev: amdgpu_device pointer
1530  *
1531  * Early initialization pass for hardware IPs.  The hardware IPs that make
1532  * up each asic are discovered each IP's early_init callback is run.  This
1533  * is the first stage in initializing the asic.
1534  * Returns 0 on success, negative error code on failure.
1535  */
1536 static int amdgpu_device_ip_early_init(struct amdgpu_device *adev)
1537 {
1538 	int i, r;
1539 
1540 	amdgpu_device_enable_virtual_display(adev);
1541 
1542 	switch (adev->asic_type) {
1543 	case CHIP_TOPAZ:
1544 	case CHIP_TONGA:
1545 	case CHIP_FIJI:
1546 	case CHIP_POLARIS10:
1547 	case CHIP_POLARIS11:
1548 	case CHIP_POLARIS12:
1549 	case CHIP_VEGAM:
1550 	case CHIP_CARRIZO:
1551 	case CHIP_STONEY:
1552 		if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
1553 			adev->family = AMDGPU_FAMILY_CZ;
1554 		else
1555 			adev->family = AMDGPU_FAMILY_VI;
1556 
1557 		r = vi_set_ip_blocks(adev);
1558 		if (r)
1559 			return r;
1560 		break;
1561 #ifdef CONFIG_DRM_AMDGPU_SI
1562 	case CHIP_VERDE:
1563 	case CHIP_TAHITI:
1564 	case CHIP_PITCAIRN:
1565 	case CHIP_OLAND:
1566 	case CHIP_HAINAN:
1567 		adev->family = AMDGPU_FAMILY_SI;
1568 		r = si_set_ip_blocks(adev);
1569 		if (r)
1570 			return r;
1571 		break;
1572 #endif
1573 #ifdef CONFIG_DRM_AMDGPU_CIK
1574 	case CHIP_BONAIRE:
1575 	case CHIP_HAWAII:
1576 	case CHIP_KAVERI:
1577 	case CHIP_KABINI:
1578 	case CHIP_MULLINS:
1579 		if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
1580 			adev->family = AMDGPU_FAMILY_CI;
1581 		else
1582 			adev->family = AMDGPU_FAMILY_KV;
1583 
1584 		r = cik_set_ip_blocks(adev);
1585 		if (r)
1586 			return r;
1587 		break;
1588 #endif
1589 	case CHIP_VEGA10:
1590 	case CHIP_VEGA12:
1591 	case CHIP_VEGA20:
1592 	case CHIP_RAVEN:
1593 		if (adev->asic_type == CHIP_RAVEN)
1594 			adev->family = AMDGPU_FAMILY_RV;
1595 		else
1596 			adev->family = AMDGPU_FAMILY_AI;
1597 
1598 		r = soc15_set_ip_blocks(adev);
1599 		if (r)
1600 			return r;
1601 		break;
1602 	default:
1603 		/* FIXME: not supported yet */
1604 		return -EINVAL;
1605 	}
1606 
1607 	r = amdgpu_device_parse_gpu_info_fw(adev);
1608 	if (r)
1609 		return r;
1610 
1611 	amdgpu_amdkfd_device_probe(adev);
1612 
1613 	if (amdgpu_sriov_vf(adev)) {
1614 		r = amdgpu_virt_request_full_gpu(adev, true);
1615 		if (r)
1616 			return -EAGAIN;
1617 	}
1618 
1619 	adev->powerplay.pp_feature = amdgpu_pp_feature_mask;
1620 
1621 	for (i = 0; i < adev->num_ip_blocks; i++) {
1622 		if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
1623 			DRM_ERROR("disabled ip block: %d <%s>\n",
1624 				  i, adev->ip_blocks[i].version->funcs->name);
1625 			adev->ip_blocks[i].status.valid = false;
1626 		} else {
1627 			if (adev->ip_blocks[i].version->funcs->early_init) {
1628 				r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
1629 				if (r == -ENOENT) {
1630 					adev->ip_blocks[i].status.valid = false;
1631 				} else if (r) {
1632 					DRM_ERROR("early_init of IP block <%s> failed %d\n",
1633 						  adev->ip_blocks[i].version->funcs->name, r);
1634 					return r;
1635 				} else {
1636 					adev->ip_blocks[i].status.valid = true;
1637 				}
1638 			} else {
1639 				adev->ip_blocks[i].status.valid = true;
1640 			}
1641 		}
1642 	}
1643 
1644 	adev->cg_flags &= amdgpu_cg_mask;
1645 	adev->pg_flags &= amdgpu_pg_mask;
1646 
1647 	return 0;
1648 }
1649 
1650 /**
1651  * amdgpu_device_ip_init - run init for hardware IPs
1652  *
1653  * @adev: amdgpu_device pointer
1654  *
1655  * Main initialization pass for hardware IPs.  The list of all the hardware
1656  * IPs that make up the asic is walked and the sw_init and hw_init callbacks
1657  * are run.  sw_init initializes the software state associated with each IP
1658  * and hw_init initializes the hardware associated with each IP.
1659  * Returns 0 on success, negative error code on failure.
1660  */
1661 static int amdgpu_device_ip_init(struct amdgpu_device *adev)
1662 {
1663 	int i, r;
1664 
1665 	for (i = 0; i < adev->num_ip_blocks; i++) {
1666 		if (!adev->ip_blocks[i].status.valid)
1667 			continue;
1668 		r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
1669 		if (r) {
1670 			DRM_ERROR("sw_init of IP block <%s> failed %d\n",
1671 				  adev->ip_blocks[i].version->funcs->name, r);
1672 			return r;
1673 		}
1674 		adev->ip_blocks[i].status.sw = true;
1675 
1676 		/* need to do gmc hw init early so we can allocate gpu mem */
1677 		if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
1678 			r = amdgpu_device_vram_scratch_init(adev);
1679 			if (r) {
1680 				DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
1681 				return r;
1682 			}
1683 			r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
1684 			if (r) {
1685 				DRM_ERROR("hw_init %d failed %d\n", i, r);
1686 				return r;
1687 			}
1688 			r = amdgpu_device_wb_init(adev);
1689 			if (r) {
1690 				DRM_ERROR("amdgpu_device_wb_init failed %d\n", r);
1691 				return r;
1692 			}
1693 			adev->ip_blocks[i].status.hw = true;
1694 
1695 			/* right after GMC hw init, we create CSA */
1696 			if (amdgpu_sriov_vf(adev)) {
1697 				r = amdgpu_allocate_static_csa(adev);
1698 				if (r) {
1699 					DRM_ERROR("allocate CSA failed %d\n", r);
1700 					return r;
1701 				}
1702 			}
1703 		}
1704 	}
1705 
1706 	for (i = 0; i < adev->num_ip_blocks; i++) {
1707 		if (!adev->ip_blocks[i].status.sw)
1708 			continue;
1709 		if (adev->ip_blocks[i].status.hw)
1710 			continue;
1711 		r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
1712 		if (r) {
1713 			DRM_ERROR("hw_init of IP block <%s> failed %d\n",
1714 				  adev->ip_blocks[i].version->funcs->name, r);
1715 			return r;
1716 		}
1717 		adev->ip_blocks[i].status.hw = true;
1718 	}
1719 
1720 	amdgpu_amdkfd_device_init(adev);
1721 
1722 	if (amdgpu_sriov_vf(adev)) {
1723 		amdgpu_virt_init_data_exchange(adev);
1724 		amdgpu_virt_release_full_gpu(adev, true);
1725 	}
1726 
1727 	return 0;
1728 }
1729 
1730 /**
1731  * amdgpu_device_fill_reset_magic - writes reset magic to gart pointer
1732  *
1733  * @adev: amdgpu_device pointer
1734  *
1735  * Writes a reset magic value to the gart pointer in VRAM.  The driver calls
1736  * this function before a GPU reset.  If the value is retained after a
1737  * GPU reset, VRAM has not been lost.  Some GPU resets may destry VRAM contents.
1738  */
1739 static void amdgpu_device_fill_reset_magic(struct amdgpu_device *adev)
1740 {
1741 	memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM);
1742 }
1743 
1744 /**
1745  * amdgpu_device_check_vram_lost - check if vram is valid
1746  *
1747  * @adev: amdgpu_device pointer
1748  *
1749  * Checks the reset magic value written to the gart pointer in VRAM.
1750  * The driver calls this after a GPU reset to see if the contents of
1751  * VRAM is lost or now.
1752  * returns true if vram is lost, false if not.
1753  */
1754 static bool amdgpu_device_check_vram_lost(struct amdgpu_device *adev)
1755 {
1756 	return !!memcmp(adev->gart.ptr, adev->reset_magic,
1757 			AMDGPU_RESET_MAGIC_NUM);
1758 }
1759 
1760 /**
1761  * amdgpu_device_ip_late_set_cg_state - late init for clockgating
1762  *
1763  * @adev: amdgpu_device pointer
1764  *
1765  * Late initialization pass enabling clockgating for hardware IPs.
1766  * The list of all the hardware IPs that make up the asic is walked and the
1767  * set_clockgating_state callbacks are run.  This stage is run late
1768  * in the init process.
1769  * Returns 0 on success, negative error code on failure.
1770  */
1771 static int amdgpu_device_ip_late_set_cg_state(struct amdgpu_device *adev)
1772 {
1773 	int i = 0, r;
1774 
1775 	if (amdgpu_emu_mode == 1)
1776 		return 0;
1777 
1778 	for (i = 0; i < adev->num_ip_blocks; i++) {
1779 		if (!adev->ip_blocks[i].status.valid)
1780 			continue;
1781 		/* skip CG for VCE/UVD, it's handled specially */
1782 		if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1783 		    adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
1784 		    adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCN &&
1785 		    adev->ip_blocks[i].version->funcs->set_clockgating_state) {
1786 			/* enable clockgating to save power */
1787 			r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1788 										     AMD_CG_STATE_GATE);
1789 			if (r) {
1790 				DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
1791 					  adev->ip_blocks[i].version->funcs->name, r);
1792 				return r;
1793 			}
1794 		}
1795 	}
1796 
1797 	return 0;
1798 }
1799 
1800 static int amdgpu_device_ip_late_set_pg_state(struct amdgpu_device *adev)
1801 {
1802 	int i = 0, r;
1803 
1804 	if (amdgpu_emu_mode == 1)
1805 		return 0;
1806 
1807 	for (i = 0; i < adev->num_ip_blocks; i++) {
1808 		if (!adev->ip_blocks[i].status.valid)
1809 			continue;
1810 		/* skip CG for VCE/UVD, it's handled specially */
1811 		if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1812 		    adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
1813 		    adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCN &&
1814 		    adev->ip_blocks[i].version->funcs->set_powergating_state) {
1815 			/* enable powergating to save power */
1816 			r = adev->ip_blocks[i].version->funcs->set_powergating_state((void *)adev,
1817 										     AMD_PG_STATE_GATE);
1818 			if (r) {
1819 				DRM_ERROR("set_powergating_state(gate) of IP block <%s> failed %d\n",
1820 					  adev->ip_blocks[i].version->funcs->name, r);
1821 				return r;
1822 			}
1823 		}
1824 	}
1825 	return 0;
1826 }
1827 
1828 /**
1829  * amdgpu_device_ip_late_init - run late init for hardware IPs
1830  *
1831  * @adev: amdgpu_device pointer
1832  *
1833  * Late initialization pass for hardware IPs.  The list of all the hardware
1834  * IPs that make up the asic is walked and the late_init callbacks are run.
1835  * late_init covers any special initialization that an IP requires
1836  * after all of the have been initialized or something that needs to happen
1837  * late in the init process.
1838  * Returns 0 on success, negative error code on failure.
1839  */
1840 static int amdgpu_device_ip_late_init(struct amdgpu_device *adev)
1841 {
1842 	int i = 0, r;
1843 
1844 	for (i = 0; i < adev->num_ip_blocks; i++) {
1845 		if (!adev->ip_blocks[i].status.valid)
1846 			continue;
1847 		if (adev->ip_blocks[i].version->funcs->late_init) {
1848 			r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
1849 			if (r) {
1850 				DRM_ERROR("late_init of IP block <%s> failed %d\n",
1851 					  adev->ip_blocks[i].version->funcs->name, r);
1852 				return r;
1853 			}
1854 			adev->ip_blocks[i].status.late_initialized = true;
1855 		}
1856 	}
1857 
1858 	amdgpu_device_ip_late_set_cg_state(adev);
1859 	amdgpu_device_ip_late_set_pg_state(adev);
1860 
1861 	queue_delayed_work(system_wq, &adev->late_init_work,
1862 			   msecs_to_jiffies(AMDGPU_RESUME_MS));
1863 
1864 	amdgpu_device_fill_reset_magic(adev);
1865 
1866 	return 0;
1867 }
1868 
1869 /**
1870  * amdgpu_device_ip_fini - run fini for hardware IPs
1871  *
1872  * @adev: amdgpu_device pointer
1873  *
1874  * Main teardown pass for hardware IPs.  The list of all the hardware
1875  * IPs that make up the asic is walked and the hw_fini and sw_fini callbacks
1876  * are run.  hw_fini tears down the hardware associated with each IP
1877  * and sw_fini tears down any software state associated with each IP.
1878  * Returns 0 on success, negative error code on failure.
1879  */
1880 static int amdgpu_device_ip_fini(struct amdgpu_device *adev)
1881 {
1882 	int i, r;
1883 
1884 	amdgpu_amdkfd_device_fini(adev);
1885 	/* need to disable SMC first */
1886 	for (i = 0; i < adev->num_ip_blocks; i++) {
1887 		if (!adev->ip_blocks[i].status.hw)
1888 			continue;
1889 		if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC &&
1890 			adev->ip_blocks[i].version->funcs->set_clockgating_state) {
1891 			/* ungate blocks before hw fini so that we can shutdown the blocks safely */
1892 			r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1893 										     AMD_CG_STATE_UNGATE);
1894 			if (r) {
1895 				DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1896 					  adev->ip_blocks[i].version->funcs->name, r);
1897 				return r;
1898 			}
1899 			if (adev->powerplay.pp_funcs->set_powergating_by_smu)
1900 				amdgpu_dpm_set_powergating_by_smu(adev, AMD_IP_BLOCK_TYPE_GFX, false);
1901 			r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
1902 			/* XXX handle errors */
1903 			if (r) {
1904 				DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
1905 					  adev->ip_blocks[i].version->funcs->name, r);
1906 			}
1907 			adev->ip_blocks[i].status.hw = false;
1908 			break;
1909 		}
1910 	}
1911 
1912 	for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
1913 		if (!adev->ip_blocks[i].status.hw)
1914 			continue;
1915 
1916 		if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1917 			adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
1918 			adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCN &&
1919 			adev->ip_blocks[i].version->funcs->set_clockgating_state) {
1920 			/* ungate blocks before hw fini so that we can shutdown the blocks safely */
1921 			r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1922 										     AMD_CG_STATE_UNGATE);
1923 			if (r) {
1924 				DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1925 					  adev->ip_blocks[i].version->funcs->name, r);
1926 				return r;
1927 			}
1928 		}
1929 
1930 		r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
1931 		/* XXX handle errors */
1932 		if (r) {
1933 			DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
1934 				  adev->ip_blocks[i].version->funcs->name, r);
1935 		}
1936 
1937 		adev->ip_blocks[i].status.hw = false;
1938 	}
1939 
1940 
1941 	for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
1942 		if (!adev->ip_blocks[i].status.sw)
1943 			continue;
1944 
1945 		if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
1946 			amdgpu_free_static_csa(adev);
1947 			amdgpu_device_wb_fini(adev);
1948 			amdgpu_device_vram_scratch_fini(adev);
1949 		}
1950 
1951 		r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
1952 		/* XXX handle errors */
1953 		if (r) {
1954 			DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
1955 				  adev->ip_blocks[i].version->funcs->name, r);
1956 		}
1957 		adev->ip_blocks[i].status.sw = false;
1958 		adev->ip_blocks[i].status.valid = false;
1959 	}
1960 
1961 	for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
1962 		if (!adev->ip_blocks[i].status.late_initialized)
1963 			continue;
1964 		if (adev->ip_blocks[i].version->funcs->late_fini)
1965 			adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
1966 		adev->ip_blocks[i].status.late_initialized = false;
1967 	}
1968 
1969 	if (amdgpu_sriov_vf(adev))
1970 		if (amdgpu_virt_release_full_gpu(adev, false))
1971 			DRM_ERROR("failed to release exclusive mode on fini\n");
1972 
1973 	return 0;
1974 }
1975 
1976 /**
1977  * amdgpu_device_ip_late_init_func_handler - work handler for clockgating
1978  *
1979  * @work: work_struct
1980  *
1981  * Work handler for amdgpu_device_ip_late_set_cg_state.  We put the
1982  * clockgating setup into a worker thread to speed up driver init and
1983  * resume from suspend.
1984  */
1985 static void amdgpu_device_ip_late_init_func_handler(struct work_struct *work)
1986 {
1987 	struct amdgpu_device *adev =
1988 		container_of(work, struct amdgpu_device, late_init_work.work);
1989 	int r;
1990 
1991 	r = amdgpu_ib_ring_tests(adev);
1992 	if (r)
1993 		DRM_ERROR("ib ring test failed (%d).\n", r);
1994 }
1995 
1996 /**
1997  * amdgpu_device_ip_suspend_phase1 - run suspend for hardware IPs (phase 1)
1998  *
1999  * @adev: amdgpu_device pointer
2000  *
2001  * Main suspend function for hardware IPs.  The list of all the hardware
2002  * IPs that make up the asic is walked, clockgating is disabled and the
2003  * suspend callbacks are run.  suspend puts the hardware and software state
2004  * in each IP into a state suitable for suspend.
2005  * Returns 0 on success, negative error code on failure.
2006  */
2007 static int amdgpu_device_ip_suspend_phase1(struct amdgpu_device *adev)
2008 {
2009 	int i, r;
2010 
2011 	if (amdgpu_sriov_vf(adev))
2012 		amdgpu_virt_request_full_gpu(adev, false);
2013 
2014 	for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2015 		if (!adev->ip_blocks[i].status.valid)
2016 			continue;
2017 		/* displays are handled separately */
2018 		if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) {
2019 			/* ungate blocks so that suspend can properly shut them down */
2020 			if (adev->ip_blocks[i].version->funcs->set_clockgating_state) {
2021 				r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
2022 											     AMD_CG_STATE_UNGATE);
2023 				if (r) {
2024 					DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
2025 						  adev->ip_blocks[i].version->funcs->name, r);
2026 				}
2027 			}
2028 			/* XXX handle errors */
2029 			r = adev->ip_blocks[i].version->funcs->suspend(adev);
2030 			/* XXX handle errors */
2031 			if (r) {
2032 				DRM_ERROR("suspend of IP block <%s> failed %d\n",
2033 					  adev->ip_blocks[i].version->funcs->name, r);
2034 			}
2035 		}
2036 	}
2037 
2038 	if (amdgpu_sriov_vf(adev))
2039 		amdgpu_virt_release_full_gpu(adev, false);
2040 
2041 	return 0;
2042 }
2043 
2044 /**
2045  * amdgpu_device_ip_suspend_phase2 - run suspend for hardware IPs (phase 2)
2046  *
2047  * @adev: amdgpu_device pointer
2048  *
2049  * Main suspend function for hardware IPs.  The list of all the hardware
2050  * IPs that make up the asic is walked, clockgating is disabled and the
2051  * suspend callbacks are run.  suspend puts the hardware and software state
2052  * in each IP into a state suitable for suspend.
2053  * Returns 0 on success, negative error code on failure.
2054  */
2055 static int amdgpu_device_ip_suspend_phase2(struct amdgpu_device *adev)
2056 {
2057 	int i, r;
2058 
2059 	if (amdgpu_sriov_vf(adev))
2060 		amdgpu_virt_request_full_gpu(adev, false);
2061 
2062 	/* ungate SMC block first */
2063 	r = amdgpu_device_ip_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC,
2064 						   AMD_CG_STATE_UNGATE);
2065 	if (r) {
2066 		DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n", r);
2067 	}
2068 
2069 	/* call smu to disable gfx off feature first when suspend */
2070 	if (adev->powerplay.pp_funcs->set_powergating_by_smu)
2071 		amdgpu_dpm_set_powergating_by_smu(adev, AMD_IP_BLOCK_TYPE_GFX, false);
2072 
2073 	for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
2074 		if (!adev->ip_blocks[i].status.valid)
2075 			continue;
2076 		/* displays are handled in phase1 */
2077 		if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE)
2078 			continue;
2079 		/* ungate blocks so that suspend can properly shut them down */
2080 		if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_SMC &&
2081 			adev->ip_blocks[i].version->funcs->set_clockgating_state) {
2082 			r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
2083 										     AMD_CG_STATE_UNGATE);
2084 			if (r) {
2085 				DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
2086 					  adev->ip_blocks[i].version->funcs->name, r);
2087 			}
2088 		}
2089 		/* XXX handle errors */
2090 		r = adev->ip_blocks[i].version->funcs->suspend(adev);
2091 		/* XXX handle errors */
2092 		if (r) {
2093 			DRM_ERROR("suspend of IP block <%s> failed %d\n",
2094 				  adev->ip_blocks[i].version->funcs->name, r);
2095 		}
2096 	}
2097 
2098 	if (amdgpu_sriov_vf(adev))
2099 		amdgpu_virt_release_full_gpu(adev, false);
2100 
2101 	return 0;
2102 }
2103 
2104 /**
2105  * amdgpu_device_ip_suspend - run suspend for hardware IPs
2106  *
2107  * @adev: amdgpu_device pointer
2108  *
2109  * Main suspend function for hardware IPs.  The list of all the hardware
2110  * IPs that make up the asic is walked, clockgating is disabled and the
2111  * suspend callbacks are run.  suspend puts the hardware and software state
2112  * in each IP into a state suitable for suspend.
2113  * Returns 0 on success, negative error code on failure.
2114  */
2115 int amdgpu_device_ip_suspend(struct amdgpu_device *adev)
2116 {
2117 	int r;
2118 
2119 	r = amdgpu_device_ip_suspend_phase1(adev);
2120 	if (r)
2121 		return r;
2122 	r = amdgpu_device_ip_suspend_phase2(adev);
2123 
2124 	return r;
2125 }
2126 
2127 static int amdgpu_device_ip_reinit_early_sriov(struct amdgpu_device *adev)
2128 {
2129 	int i, r;
2130 
2131 	static enum amd_ip_block_type ip_order[] = {
2132 		AMD_IP_BLOCK_TYPE_GMC,
2133 		AMD_IP_BLOCK_TYPE_COMMON,
2134 		AMD_IP_BLOCK_TYPE_PSP,
2135 		AMD_IP_BLOCK_TYPE_IH,
2136 	};
2137 
2138 	for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
2139 		int j;
2140 		struct amdgpu_ip_block *block;
2141 
2142 		for (j = 0; j < adev->num_ip_blocks; j++) {
2143 			block = &adev->ip_blocks[j];
2144 
2145 			if (block->version->type != ip_order[i] ||
2146 				!block->status.valid)
2147 				continue;
2148 
2149 			r = block->version->funcs->hw_init(adev);
2150 			DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"succeeded");
2151 			if (r)
2152 				return r;
2153 		}
2154 	}
2155 
2156 	return 0;
2157 }
2158 
2159 static int amdgpu_device_ip_reinit_late_sriov(struct amdgpu_device *adev)
2160 {
2161 	int i, r;
2162 
2163 	static enum amd_ip_block_type ip_order[] = {
2164 		AMD_IP_BLOCK_TYPE_SMC,
2165 		AMD_IP_BLOCK_TYPE_DCE,
2166 		AMD_IP_BLOCK_TYPE_GFX,
2167 		AMD_IP_BLOCK_TYPE_SDMA,
2168 		AMD_IP_BLOCK_TYPE_UVD,
2169 		AMD_IP_BLOCK_TYPE_VCE
2170 	};
2171 
2172 	for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
2173 		int j;
2174 		struct amdgpu_ip_block *block;
2175 
2176 		for (j = 0; j < adev->num_ip_blocks; j++) {
2177 			block = &adev->ip_blocks[j];
2178 
2179 			if (block->version->type != ip_order[i] ||
2180 				!block->status.valid)
2181 				continue;
2182 
2183 			r = block->version->funcs->hw_init(adev);
2184 			DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"succeeded");
2185 			if (r)
2186 				return r;
2187 		}
2188 	}
2189 
2190 	return 0;
2191 }
2192 
2193 /**
2194  * amdgpu_device_ip_resume_phase1 - run resume for hardware IPs
2195  *
2196  * @adev: amdgpu_device pointer
2197  *
2198  * First resume function for hardware IPs.  The list of all the hardware
2199  * IPs that make up the asic is walked and the resume callbacks are run for
2200  * COMMON, GMC, and IH.  resume puts the hardware into a functional state
2201  * after a suspend and updates the software state as necessary.  This
2202  * function is also used for restoring the GPU after a GPU reset.
2203  * Returns 0 on success, negative error code on failure.
2204  */
2205 static int amdgpu_device_ip_resume_phase1(struct amdgpu_device *adev)
2206 {
2207 	int i, r;
2208 
2209 	for (i = 0; i < adev->num_ip_blocks; i++) {
2210 		if (!adev->ip_blocks[i].status.valid)
2211 			continue;
2212 		if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
2213 		    adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
2214 		    adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH) {
2215 			r = adev->ip_blocks[i].version->funcs->resume(adev);
2216 			if (r) {
2217 				DRM_ERROR("resume of IP block <%s> failed %d\n",
2218 					  adev->ip_blocks[i].version->funcs->name, r);
2219 				return r;
2220 			}
2221 		}
2222 	}
2223 
2224 	return 0;
2225 }
2226 
2227 /**
2228  * amdgpu_device_ip_resume_phase2 - run resume for hardware IPs
2229  *
2230  * @adev: amdgpu_device pointer
2231  *
2232  * First resume function for hardware IPs.  The list of all the hardware
2233  * IPs that make up the asic is walked and the resume callbacks are run for
2234  * all blocks except COMMON, GMC, and IH.  resume puts the hardware into a
2235  * functional state after a suspend and updates the software state as
2236  * necessary.  This function is also used for restoring the GPU after a GPU
2237  * reset.
2238  * Returns 0 on success, negative error code on failure.
2239  */
2240 static int amdgpu_device_ip_resume_phase2(struct amdgpu_device *adev)
2241 {
2242 	int i, r;
2243 
2244 	for (i = 0; i < adev->num_ip_blocks; i++) {
2245 		if (!adev->ip_blocks[i].status.valid)
2246 			continue;
2247 		if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
2248 		    adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
2249 		    adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH)
2250 			continue;
2251 		r = adev->ip_blocks[i].version->funcs->resume(adev);
2252 		if (r) {
2253 			DRM_ERROR("resume of IP block <%s> failed %d\n",
2254 				  adev->ip_blocks[i].version->funcs->name, r);
2255 			return r;
2256 		}
2257 	}
2258 
2259 	return 0;
2260 }
2261 
2262 /**
2263  * amdgpu_device_ip_resume - run resume for hardware IPs
2264  *
2265  * @adev: amdgpu_device pointer
2266  *
2267  * Main resume function for hardware IPs.  The hardware IPs
2268  * are split into two resume functions because they are
2269  * are also used in in recovering from a GPU reset and some additional
2270  * steps need to be take between them.  In this case (S3/S4) they are
2271  * run sequentially.
2272  * Returns 0 on success, negative error code on failure.
2273  */
2274 static int amdgpu_device_ip_resume(struct amdgpu_device *adev)
2275 {
2276 	int r;
2277 
2278 	r = amdgpu_device_ip_resume_phase1(adev);
2279 	if (r)
2280 		return r;
2281 	r = amdgpu_device_ip_resume_phase2(adev);
2282 
2283 	return r;
2284 }
2285 
2286 /**
2287  * amdgpu_device_detect_sriov_bios - determine if the board supports SR-IOV
2288  *
2289  * @adev: amdgpu_device pointer
2290  *
2291  * Query the VBIOS data tables to determine if the board supports SR-IOV.
2292  */
2293 static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
2294 {
2295 	if (amdgpu_sriov_vf(adev)) {
2296 		if (adev->is_atom_fw) {
2297 			if (amdgpu_atomfirmware_gpu_supports_virtualization(adev))
2298 				adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
2299 		} else {
2300 			if (amdgpu_atombios_has_gpu_virtualization_table(adev))
2301 				adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
2302 		}
2303 
2304 		if (!(adev->virt.caps & AMDGPU_SRIOV_CAPS_SRIOV_VBIOS))
2305 			amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_NO_VBIOS, 0, 0);
2306 	}
2307 }
2308 
2309 /**
2310  * amdgpu_device_asic_has_dc_support - determine if DC supports the asic
2311  *
2312  * @asic_type: AMD asic type
2313  *
2314  * Check if there is DC (new modesetting infrastructre) support for an asic.
2315  * returns true if DC has support, false if not.
2316  */
2317 bool amdgpu_device_asic_has_dc_support(enum amd_asic_type asic_type)
2318 {
2319 	switch (asic_type) {
2320 #if defined(CONFIG_DRM_AMD_DC)
2321 	case CHIP_BONAIRE:
2322 	case CHIP_KAVERI:
2323 	case CHIP_KABINI:
2324 	case CHIP_MULLINS:
2325 		/*
2326 		 * We have systems in the wild with these ASICs that require
2327 		 * LVDS and VGA support which is not supported with DC.
2328 		 *
2329 		 * Fallback to the non-DC driver here by default so as not to
2330 		 * cause regressions.
2331 		 */
2332 		return amdgpu_dc > 0;
2333 	case CHIP_HAWAII:
2334 	case CHIP_CARRIZO:
2335 	case CHIP_STONEY:
2336 	case CHIP_POLARIS10:
2337 	case CHIP_POLARIS11:
2338 	case CHIP_POLARIS12:
2339 	case CHIP_VEGAM:
2340 	case CHIP_TONGA:
2341 	case CHIP_FIJI:
2342 	case CHIP_VEGA10:
2343 	case CHIP_VEGA12:
2344 	case CHIP_VEGA20:
2345 #if defined(CONFIG_DRM_AMD_DC_DCN1_0)
2346 	case CHIP_RAVEN:
2347 #endif
2348 		return amdgpu_dc != 0;
2349 #endif
2350 	default:
2351 		return false;
2352 	}
2353 }
2354 
2355 /**
2356  * amdgpu_device_has_dc_support - check if dc is supported
2357  *
2358  * @adev: amdgpu_device_pointer
2359  *
2360  * Returns true for supported, false for not supported
2361  */
2362 bool amdgpu_device_has_dc_support(struct amdgpu_device *adev)
2363 {
2364 	if (amdgpu_sriov_vf(adev))
2365 		return false;
2366 
2367 	return amdgpu_device_asic_has_dc_support(adev->asic_type);
2368 }
2369 
2370 /**
2371  * amdgpu_device_init - initialize the driver
2372  *
2373  * @adev: amdgpu_device pointer
2374  * @ddev: drm dev pointer
2375  * @pdev: pci dev pointer
2376  * @flags: driver flags
2377  *
2378  * Initializes the driver info and hw (all asics).
2379  * Returns 0 for success or an error on failure.
2380  * Called at driver startup.
2381  */
2382 int amdgpu_device_init(struct amdgpu_device *adev,
2383 		       struct drm_device *ddev,
2384 		       struct pci_dev *pdev,
2385 		       uint32_t flags)
2386 {
2387 	int r, i;
2388 	bool runtime = false;
2389 	u32 max_MBps;
2390 
2391 	adev->shutdown = false;
2392 #ifdef __linux__
2393 	adev->dev = &pdev->dev;
2394 #endif
2395 	adev->ddev = ddev;
2396 	adev->pdev = pdev;
2397 	adev->flags = flags;
2398 	adev->asic_type = flags & AMD_ASIC_MASK;
2399 	adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
2400 	if (amdgpu_emu_mode == 1)
2401 		adev->usec_timeout *= 2;
2402 	adev->gmc.gart_size = 512 * 1024 * 1024;
2403 	adev->accel_working = false;
2404 	adev->num_rings = 0;
2405 	adev->mman.buffer_funcs = NULL;
2406 	adev->mman.buffer_funcs_ring = NULL;
2407 	adev->vm_manager.vm_pte_funcs = NULL;
2408 	adev->vm_manager.vm_pte_num_rings = 0;
2409 	adev->gmc.gmc_funcs = NULL;
2410 	adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
2411 	bitmap_zero(adev->gfx.pipe_reserve_bitmap, AMDGPU_MAX_COMPUTE_QUEUES);
2412 
2413 	adev->smc_rreg = &amdgpu_invalid_rreg;
2414 	adev->smc_wreg = &amdgpu_invalid_wreg;
2415 	adev->pcie_rreg = &amdgpu_invalid_rreg;
2416 	adev->pcie_wreg = &amdgpu_invalid_wreg;
2417 	adev->pciep_rreg = &amdgpu_invalid_rreg;
2418 	adev->pciep_wreg = &amdgpu_invalid_wreg;
2419 	adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
2420 	adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
2421 	adev->didt_rreg = &amdgpu_invalid_rreg;
2422 	adev->didt_wreg = &amdgpu_invalid_wreg;
2423 	adev->gc_cac_rreg = &amdgpu_invalid_rreg;
2424 	adev->gc_cac_wreg = &amdgpu_invalid_wreg;
2425 	adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
2426 	adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
2427 
2428 	printf("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
2429 		 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
2430 		 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
2431 
2432 	/* mutex initialization are all done here so we
2433 	 * can recall function without having locking issues */
2434 	atomic_set(&adev->irq.ih.lock, 0);
2435 	rw_init(&adev->firmware.mutex, "agfw");
2436 	rw_init(&adev->pm.mutex, "agpm");
2437 	rw_init(&adev->gfx.gpu_clock_mutex, "gfxclk");
2438 	rw_init(&adev->srbm_mutex, "srbm");
2439 	rw_init(&adev->gfx.pipe_reserve_mutex, "pipers");
2440 	rw_init(&adev->grbm_idx_mutex, "grbmidx");
2441 	rw_init(&adev->mn_lock, "agpumn");
2442 	rw_init(&adev->virt.vf_errors.lock, "vferr");
2443 	hash_init(adev->mn_hash);
2444 	rw_init(&adev->lock_reset, "aglkrst");
2445 
2446 	amdgpu_device_check_arguments(adev);
2447 
2448 	mtx_init(&adev->mmio_idx_lock, IPL_TTY);
2449 	mtx_init(&adev->smc_idx_lock, IPL_TTY);
2450 	mtx_init(&adev->pcie_idx_lock, IPL_TTY);
2451 	mtx_init(&adev->uvd_ctx_idx_lock, IPL_TTY);
2452 	mtx_init(&adev->didt_idx_lock, IPL_TTY);
2453 	mtx_init(&adev->gc_cac_idx_lock, IPL_TTY);
2454 	mtx_init(&adev->se_cac_idx_lock, IPL_TTY);
2455 	mtx_init(&adev->audio_endpt_idx_lock, IPL_TTY);
2456 	mtx_init(&adev->mm_stats.lock, IPL_TTY);
2457 
2458 	INIT_LIST_HEAD(&adev->shadow_list);
2459 	rw_init(&adev->shadow_list_lock, "sdwlst");
2460 
2461 	INIT_LIST_HEAD(&adev->ring_lru_list);
2462 	mtx_init(&adev->ring_lru_list_lock, IPL_TTY);
2463 
2464 	INIT_DELAYED_WORK(&adev->late_init_work,
2465 			  amdgpu_device_ip_late_init_func_handler);
2466 
2467 	adev->pm.ac_power = power_supply_is_system_supplied() > 0 ? true : false;
2468 
2469 #ifdef __linux__
2470 	/* Registers mapping */
2471 	/* TODO: block userspace mapping of io register */
2472 	if (adev->asic_type >= CHIP_BONAIRE) {
2473 		adev->rmmio_base = pci_resource_start(adev->pdev, 5);
2474 		adev->rmmio_size = pci_resource_len(adev->pdev, 5);
2475 	} else {
2476 		adev->rmmio_base = pci_resource_start(adev->pdev, 2);
2477 		adev->rmmio_size = pci_resource_len(adev->pdev, 2);
2478 	}
2479 
2480 	adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
2481 	if (adev->rmmio == NULL) {
2482 		return -ENOMEM;
2483 	}
2484 #endif
2485 	DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
2486 	DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
2487 
2488 	/* doorbell bar mapping */
2489 	amdgpu_device_doorbell_init(adev);
2490 
2491 	/* io port mapping */
2492 #ifdef __linux__
2493 	for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
2494 		if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
2495 			adev->rio_mem_size = pci_resource_len(adev->pdev, i);
2496 			adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
2497 			break;
2498 		}
2499 	}
2500 	if (adev->rio_mem == NULL)
2501 		DRM_INFO("PCI I/O BAR is not found.\n");
2502 #endif
2503 
2504 	amdgpu_device_get_pcie_info(adev);
2505 
2506 	/* early init functions */
2507 	r = amdgpu_device_ip_early_init(adev);
2508 	if (r)
2509 		return r;
2510 
2511 	/* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
2512 	/* this will fail for cards that aren't VGA class devices, just
2513 	 * ignore it */
2514 #ifdef notyet
2515 	vga_client_register(adev->pdev, adev, NULL, amdgpu_device_vga_set_decode);
2516 #endif
2517 
2518 	if (amdgpu_device_is_px(ddev))
2519 		runtime = true;
2520 #ifdef notyet
2521 	if (!pci_is_thunderbolt_attached(adev->pdev))
2522 		vga_switcheroo_register_client(adev->pdev,
2523 					       &amdgpu_switcheroo_ops, runtime);
2524 	if (runtime)
2525 		vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
2526 #endif
2527 
2528 	if (amdgpu_emu_mode == 1) {
2529 		/* post the asic on emulation mode */
2530 		emu_soc_asic_init(adev);
2531 		goto fence_driver_init;
2532 	}
2533 
2534 	/* Read BIOS */
2535 	if (!amdgpu_get_bios(adev)) {
2536 		r = -EINVAL;
2537 		goto failed;
2538 	}
2539 
2540 	r = amdgpu_atombios_init(adev);
2541 	if (r) {
2542 		dev_err(adev->dev, "amdgpu_atombios_init failed\n");
2543 		amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_INIT_FAIL, 0, 0);
2544 		goto failed;
2545 	}
2546 
2547 	/* detect if we are with an SRIOV vbios */
2548 	amdgpu_device_detect_sriov_bios(adev);
2549 
2550 	/* Post card if necessary */
2551 	if (amdgpu_device_need_post(adev)) {
2552 		if (!adev->bios) {
2553 			dev_err(adev->dev, "no vBIOS found\n");
2554 			r = -EINVAL;
2555 			goto failed;
2556 		}
2557 		DRM_INFO("GPU posting now...\n");
2558 		r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2559 		if (r) {
2560 			dev_err(adev->dev, "gpu post error!\n");
2561 			goto failed;
2562 		}
2563 	}
2564 
2565 	if (adev->is_atom_fw) {
2566 		/* Initialize clocks */
2567 		r = amdgpu_atomfirmware_get_clock_info(adev);
2568 		if (r) {
2569 			dev_err(adev->dev, "amdgpu_atomfirmware_get_clock_info failed\n");
2570 			amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
2571 			goto failed;
2572 		}
2573 	} else {
2574 		/* Initialize clocks */
2575 		r = amdgpu_atombios_get_clock_info(adev);
2576 		if (r) {
2577 			dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
2578 			amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
2579 			goto failed;
2580 		}
2581 		/* init i2c buses */
2582 		if (!amdgpu_device_has_dc_support(adev))
2583 			amdgpu_atombios_i2c_init(adev);
2584 	}
2585 
2586 fence_driver_init:
2587 	/* Fence driver */
2588 	r = amdgpu_fence_driver_init(adev);
2589 	if (r) {
2590 		dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
2591 		amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0);
2592 		goto failed;
2593 	}
2594 
2595 	/* init the mode config */
2596 	drm_mode_config_init(adev->ddev);
2597 
2598 	r = amdgpu_device_ip_init(adev);
2599 	if (r) {
2600 		/* failed in exclusive mode due to timeout */
2601 		if (amdgpu_sriov_vf(adev) &&
2602 		    !amdgpu_sriov_runtime(adev) &&
2603 		    amdgpu_virt_mmio_blocked(adev) &&
2604 		    !amdgpu_virt_wait_reset(adev)) {
2605 			dev_err(adev->dev, "VF exclusive mode timeout\n");
2606 			/* Don't send request since VF is inactive. */
2607 			adev->virt.caps &= ~AMDGPU_SRIOV_CAPS_RUNTIME;
2608 			adev->virt.ops = NULL;
2609 			r = -EAGAIN;
2610 			goto failed;
2611 		}
2612 		dev_err(adev->dev, "amdgpu_device_ip_init failed\n");
2613 		amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0);
2614 		goto failed;
2615 	}
2616 
2617 	adev->accel_working = true;
2618 
2619 	amdgpu_vm_check_compute_bug(adev);
2620 
2621 	/* Initialize the buffer migration limit. */
2622 	if (amdgpu_moverate >= 0)
2623 		max_MBps = amdgpu_moverate;
2624 	else
2625 		max_MBps = 8; /* Allow 8 MB/s. */
2626 	/* Get a log2 for easy divisions. */
2627 	adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
2628 
2629 	r = amdgpu_ib_pool_init(adev);
2630 	if (r) {
2631 		dev_err(adev->dev, "IB initialization failed (%d).\n", r);
2632 		amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r);
2633 		goto failed;
2634 	}
2635 
2636 	amdgpu_fbdev_init(adev);
2637 
2638 	r = amdgpu_pm_sysfs_init(adev);
2639 	if (r)
2640 		DRM_ERROR("registering pm debugfs failed (%d).\n", r);
2641 
2642 	r = amdgpu_debugfs_gem_init(adev);
2643 	if (r)
2644 		DRM_ERROR("registering gem debugfs failed (%d).\n", r);
2645 
2646 	r = amdgpu_debugfs_regs_init(adev);
2647 	if (r)
2648 		DRM_ERROR("registering register debugfs failed (%d).\n", r);
2649 
2650 	r = amdgpu_debugfs_firmware_init(adev);
2651 	if (r)
2652 		DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
2653 
2654 	r = amdgpu_debugfs_init(adev);
2655 	if (r)
2656 		DRM_ERROR("Creating debugfs files failed (%d).\n", r);
2657 
2658 	if ((amdgpu_testing & 1)) {
2659 		if (adev->accel_working)
2660 			amdgpu_test_moves(adev);
2661 		else
2662 			DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
2663 	}
2664 	if (amdgpu_benchmarking) {
2665 		if (adev->accel_working)
2666 			amdgpu_benchmark(adev, amdgpu_benchmarking);
2667 		else
2668 			DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
2669 	}
2670 
2671 	/* enable clockgating, etc. after ib tests, etc. since some blocks require
2672 	 * explicit gating rather than handling it automatically.
2673 	 */
2674 	r = amdgpu_device_ip_late_init(adev);
2675 	if (r) {
2676 		dev_err(adev->dev, "amdgpu_device_ip_late_init failed\n");
2677 		amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r);
2678 		goto failed;
2679 	}
2680 
2681 	return 0;
2682 
2683 failed:
2684 	amdgpu_vf_error_trans_all(adev);
2685 	if (runtime)
2686 		vga_switcheroo_fini_domain_pm_ops(adev->dev);
2687 
2688 	return r;
2689 }
2690 
2691 /**
2692  * amdgpu_device_fini - tear down the driver
2693  *
2694  * @adev: amdgpu_device pointer
2695  *
2696  * Tear down the driver info (all asics).
2697  * Called at driver shutdown.
2698  */
2699 void amdgpu_device_fini(struct amdgpu_device *adev)
2700 {
2701 	int r;
2702 
2703 	DRM_INFO("amdgpu: finishing device.\n");
2704 	adev->shutdown = true;
2705 	/* disable all interrupts */
2706 	amdgpu_irq_disable_all(adev);
2707 	if (adev->mode_info.mode_config_initialized){
2708 		if (!amdgpu_device_has_dc_support(adev))
2709 			drm_crtc_force_disable_all(adev->ddev);
2710 		else
2711 			drm_atomic_helper_shutdown(adev->ddev);
2712 	}
2713 	amdgpu_ib_pool_fini(adev);
2714 	amdgpu_fence_driver_fini(adev);
2715 	amdgpu_pm_sysfs_fini(adev);
2716 	amdgpu_fbdev_fini(adev);
2717 	r = amdgpu_device_ip_fini(adev);
2718 	if (adev->firmware.gpu_info_fw) {
2719 		release_firmware(adev->firmware.gpu_info_fw);
2720 		adev->firmware.gpu_info_fw = NULL;
2721 	}
2722 	adev->accel_working = false;
2723 	cancel_delayed_work_sync(&adev->late_init_work);
2724 	/* free i2c buses */
2725 	if (!amdgpu_device_has_dc_support(adev))
2726 		amdgpu_i2c_fini(adev);
2727 
2728 	if (amdgpu_emu_mode != 1)
2729 		amdgpu_atombios_fini(adev);
2730 
2731 	kfree(adev->bios);
2732 	adev->bios = NULL;
2733 	if (!pci_is_thunderbolt_attached(adev->pdev))
2734 		vga_switcheroo_unregister_client(adev->pdev);
2735 	if (adev->flags & AMD_IS_PX)
2736 		vga_switcheroo_fini_domain_pm_ops(adev->dev);
2737 	vga_client_register(adev->pdev, NULL, NULL, NULL);
2738 #ifdef __linux__
2739 	if (adev->rio_mem)
2740 		pci_iounmap(adev->pdev, adev->rio_mem);
2741 	adev->rio_mem = NULL;
2742 	iounmap(adev->rmmio);
2743 	adev->rmmio = NULL;
2744 #else
2745 	if (adev->rio_mem_size > 0)
2746 		bus_space_unmap(adev->rio_mem_bst, adev->rio_mem_bsh,
2747 		    adev->rio_mem_size);
2748 	adev->rio_mem_size = 0;
2749 
2750 	if (adev->rmmio_size > 0)
2751 		bus_space_unmap(adev->rmmio_bst, adev->rmmio_bsh,
2752 		    adev->rmmio_size);
2753 	adev->rmmio_size = 0;
2754 #endif
2755 	amdgpu_device_doorbell_fini(adev);
2756 	amdgpu_debugfs_regs_cleanup(adev);
2757 }
2758 
2759 
2760 /*
2761  * Suspend & resume.
2762  */
2763 /**
2764  * amdgpu_device_suspend - initiate device suspend
2765  *
2766  * @dev: drm dev pointer
2767  * @suspend: suspend state
2768  * @fbcon : notify the fbdev of suspend
2769  *
2770  * Puts the hw in the suspend state (all asics).
2771  * Returns 0 for success or an error on failure.
2772  * Called at driver suspend.
2773  */
2774 int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
2775 {
2776 	struct amdgpu_device *adev;
2777 	struct drm_crtc *crtc;
2778 	struct drm_connector *connector;
2779 	int r;
2780 
2781 	if (dev == NULL || dev->dev_private == NULL) {
2782 		return -ENODEV;
2783 	}
2784 
2785 	adev = dev->dev_private;
2786 	if (adev->shutdown)
2787 		return 0;
2788 
2789 #ifdef notyet
2790 	if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2791 		return 0;
2792 #endif
2793 
2794 	drm_kms_helper_poll_disable(dev);
2795 
2796 	if (fbcon)
2797 		amdgpu_fbdev_set_suspend(adev, 1);
2798 
2799 	if (!amdgpu_device_has_dc_support(adev)) {
2800 		/* turn off display hw */
2801 		drm_modeset_lock_all(dev);
2802 		list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2803 			drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
2804 		}
2805 		drm_modeset_unlock_all(dev);
2806 			/* unpin the front buffers and cursors */
2807 		list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2808 			struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2809 			struct drm_framebuffer *fb = crtc->primary->fb;
2810 			struct amdgpu_bo *robj;
2811 
2812 			if (amdgpu_crtc->cursor_bo) {
2813 				struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
2814 				r = amdgpu_bo_reserve(aobj, true);
2815 				if (r == 0) {
2816 					amdgpu_bo_unpin(aobj);
2817 					amdgpu_bo_unreserve(aobj);
2818 				}
2819 			}
2820 
2821 			if (fb == NULL || fb->obj[0] == NULL) {
2822 				continue;
2823 			}
2824 			robj = gem_to_amdgpu_bo(fb->obj[0]);
2825 			/* don't unpin kernel fb objects */
2826 			if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
2827 				r = amdgpu_bo_reserve(robj, true);
2828 				if (r == 0) {
2829 					amdgpu_bo_unpin(robj);
2830 					amdgpu_bo_unreserve(robj);
2831 				}
2832 			}
2833 		}
2834 	}
2835 
2836 	amdgpu_amdkfd_suspend(adev);
2837 
2838 	r = amdgpu_device_ip_suspend_phase1(adev);
2839 
2840 	/* evict vram memory */
2841 	amdgpu_bo_evict_vram(adev);
2842 
2843 	amdgpu_fence_driver_suspend(adev);
2844 
2845 	r = amdgpu_device_ip_suspend_phase2(adev);
2846 
2847 	/* evict remaining vram memory
2848 	 * This second call to evict vram is to evict the gart page table
2849 	 * using the CPU.
2850 	 */
2851 	amdgpu_bo_evict_vram(adev);
2852 
2853 	pci_save_state(dev->pdev);
2854 	if (suspend) {
2855 		/* Shut down the device */
2856 		pci_disable_device(dev->pdev);
2857 		pci_set_power_state(dev->pdev, PCI_D3hot);
2858 	} else {
2859 		r = amdgpu_asic_reset(adev);
2860 		if (r)
2861 			DRM_ERROR("amdgpu asic reset failed\n");
2862 	}
2863 
2864 	return 0;
2865 }
2866 
2867 /**
2868  * amdgpu_device_resume - initiate device resume
2869  *
2870  * @dev: drm dev pointer
2871  * @resume: resume state
2872  * @fbcon : notify the fbdev of resume
2873  *
2874  * Bring the hw back to operating state (all asics).
2875  * Returns 0 for success or an error on failure.
2876  * Called at driver resume.
2877  */
2878 int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
2879 {
2880 	struct drm_connector *connector;
2881 	struct amdgpu_device *adev = dev->dev_private;
2882 	struct drm_crtc *crtc;
2883 	int r = 0;
2884 
2885 #ifdef notyet
2886 	if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2887 		return 0;
2888 #endif
2889 
2890 	if (resume) {
2891 		pci_set_power_state(dev->pdev, PCI_D0);
2892 		pci_restore_state(dev->pdev);
2893 		r = pci_enable_device(dev->pdev);
2894 		if (r)
2895 			return r;
2896 	}
2897 
2898 	/* post card */
2899 	if (amdgpu_device_need_post(adev)) {
2900 		r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2901 		if (r)
2902 			DRM_ERROR("amdgpu asic init failed\n");
2903 	}
2904 
2905 	r = amdgpu_device_ip_resume(adev);
2906 	if (r) {
2907 		DRM_ERROR("amdgpu_device_ip_resume failed (%d).\n", r);
2908 		return r;
2909 	}
2910 	amdgpu_fence_driver_resume(adev);
2911 
2912 
2913 	r = amdgpu_device_ip_late_init(adev);
2914 	if (r)
2915 		return r;
2916 
2917 	if (!amdgpu_device_has_dc_support(adev)) {
2918 		/* pin cursors */
2919 		list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2920 			struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2921 
2922 			if (amdgpu_crtc->cursor_bo) {
2923 				struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
2924 				r = amdgpu_bo_reserve(aobj, true);
2925 				if (r == 0) {
2926 					r = amdgpu_bo_pin(aobj, AMDGPU_GEM_DOMAIN_VRAM);
2927 					if (r != 0)
2928 						DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
2929 					amdgpu_crtc->cursor_addr = amdgpu_bo_gpu_offset(aobj);
2930 					amdgpu_bo_unreserve(aobj);
2931 				}
2932 			}
2933 		}
2934 	}
2935 	r = amdgpu_amdkfd_resume(adev);
2936 	if (r)
2937 		return r;
2938 
2939 	/* Make sure IB tests flushed */
2940 	flush_delayed_work(&adev->late_init_work);
2941 
2942 	/* blat the mode back in */
2943 	if (fbcon) {
2944 		if (!amdgpu_device_has_dc_support(adev)) {
2945 			/* pre DCE11 */
2946 			drm_helper_resume_force_mode(dev);
2947 
2948 			/* turn on display hw */
2949 			drm_modeset_lock_all(dev);
2950 			list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2951 				drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
2952 			}
2953 			drm_modeset_unlock_all(dev);
2954 		}
2955 		amdgpu_fbdev_set_suspend(adev, 0);
2956 	}
2957 
2958 	drm_kms_helper_poll_enable(dev);
2959 
2960 	/*
2961 	 * Most of the connector probing functions try to acquire runtime pm
2962 	 * refs to ensure that the GPU is powered on when connector polling is
2963 	 * performed. Since we're calling this from a runtime PM callback,
2964 	 * trying to acquire rpm refs will cause us to deadlock.
2965 	 *
2966 	 * Since we're guaranteed to be holding the rpm lock, it's safe to
2967 	 * temporarily disable the rpm helpers so this doesn't deadlock us.
2968 	 */
2969 #if defined(CONFIG_PM) && defined(__linux__)
2970 	dev->dev->power.disable_depth++;
2971 #endif
2972 	if (!amdgpu_device_has_dc_support(adev))
2973 		drm_helper_hpd_irq_event(dev);
2974 	else
2975 		drm_kms_helper_hotplug_event(dev);
2976 #if defined(CONFIG_PM) && defined(__linux__)
2977 	dev->dev->power.disable_depth--;
2978 #endif
2979 	return 0;
2980 }
2981 
2982 /**
2983  * amdgpu_device_ip_check_soft_reset - did soft reset succeed
2984  *
2985  * @adev: amdgpu_device pointer
2986  *
2987  * The list of all the hardware IPs that make up the asic is walked and
2988  * the check_soft_reset callbacks are run.  check_soft_reset determines
2989  * if the asic is still hung or not.
2990  * Returns true if any of the IPs are still in a hung state, false if not.
2991  */
2992 static bool amdgpu_device_ip_check_soft_reset(struct amdgpu_device *adev)
2993 {
2994 	int i;
2995 	bool asic_hang = false;
2996 
2997 	if (amdgpu_sriov_vf(adev))
2998 		return true;
2999 
3000 	if (amdgpu_asic_need_full_reset(adev))
3001 		return true;
3002 
3003 	for (i = 0; i < adev->num_ip_blocks; i++) {
3004 		if (!adev->ip_blocks[i].status.valid)
3005 			continue;
3006 		if (adev->ip_blocks[i].version->funcs->check_soft_reset)
3007 			adev->ip_blocks[i].status.hang =
3008 				adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
3009 		if (adev->ip_blocks[i].status.hang) {
3010 			DRM_INFO("IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
3011 			asic_hang = true;
3012 		}
3013 	}
3014 	return asic_hang;
3015 }
3016 
3017 /**
3018  * amdgpu_device_ip_pre_soft_reset - prepare for soft reset
3019  *
3020  * @adev: amdgpu_device pointer
3021  *
3022  * The list of all the hardware IPs that make up the asic is walked and the
3023  * pre_soft_reset callbacks are run if the block is hung.  pre_soft_reset
3024  * handles any IP specific hardware or software state changes that are
3025  * necessary for a soft reset to succeed.
3026  * Returns 0 on success, negative error code on failure.
3027  */
3028 static int amdgpu_device_ip_pre_soft_reset(struct amdgpu_device *adev)
3029 {
3030 	int i, r = 0;
3031 
3032 	for (i = 0; i < adev->num_ip_blocks; i++) {
3033 		if (!adev->ip_blocks[i].status.valid)
3034 			continue;
3035 		if (adev->ip_blocks[i].status.hang &&
3036 		    adev->ip_blocks[i].version->funcs->pre_soft_reset) {
3037 			r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
3038 			if (r)
3039 				return r;
3040 		}
3041 	}
3042 
3043 	return 0;
3044 }
3045 
3046 /**
3047  * amdgpu_device_ip_need_full_reset - check if a full asic reset is needed
3048  *
3049  * @adev: amdgpu_device pointer
3050  *
3051  * Some hardware IPs cannot be soft reset.  If they are hung, a full gpu
3052  * reset is necessary to recover.
3053  * Returns true if a full asic reset is required, false if not.
3054  */
3055 static bool amdgpu_device_ip_need_full_reset(struct amdgpu_device *adev)
3056 {
3057 	int i;
3058 
3059 	if (amdgpu_asic_need_full_reset(adev))
3060 		return true;
3061 
3062 	for (i = 0; i < adev->num_ip_blocks; i++) {
3063 		if (!adev->ip_blocks[i].status.valid)
3064 			continue;
3065 		if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
3066 		    (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
3067 		    (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
3068 		    (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) ||
3069 		     adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
3070 			if (adev->ip_blocks[i].status.hang) {
3071 				DRM_INFO("Some block need full reset!\n");
3072 				return true;
3073 			}
3074 		}
3075 	}
3076 	return false;
3077 }
3078 
3079 /**
3080  * amdgpu_device_ip_soft_reset - do a soft reset
3081  *
3082  * @adev: amdgpu_device pointer
3083  *
3084  * The list of all the hardware IPs that make up the asic is walked and the
3085  * soft_reset callbacks are run if the block is hung.  soft_reset handles any
3086  * IP specific hardware or software state changes that are necessary to soft
3087  * reset the IP.
3088  * Returns 0 on success, negative error code on failure.
3089  */
3090 static int amdgpu_device_ip_soft_reset(struct amdgpu_device *adev)
3091 {
3092 	int i, r = 0;
3093 
3094 	for (i = 0; i < adev->num_ip_blocks; i++) {
3095 		if (!adev->ip_blocks[i].status.valid)
3096 			continue;
3097 		if (adev->ip_blocks[i].status.hang &&
3098 		    adev->ip_blocks[i].version->funcs->soft_reset) {
3099 			r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
3100 			if (r)
3101 				return r;
3102 		}
3103 	}
3104 
3105 	return 0;
3106 }
3107 
3108 /**
3109  * amdgpu_device_ip_post_soft_reset - clean up from soft reset
3110  *
3111  * @adev: amdgpu_device pointer
3112  *
3113  * The list of all the hardware IPs that make up the asic is walked and the
3114  * post_soft_reset callbacks are run if the asic was hung.  post_soft_reset
3115  * handles any IP specific hardware or software state changes that are
3116  * necessary after the IP has been soft reset.
3117  * Returns 0 on success, negative error code on failure.
3118  */
3119 static int amdgpu_device_ip_post_soft_reset(struct amdgpu_device *adev)
3120 {
3121 	int i, r = 0;
3122 
3123 	for (i = 0; i < adev->num_ip_blocks; i++) {
3124 		if (!adev->ip_blocks[i].status.valid)
3125 			continue;
3126 		if (adev->ip_blocks[i].status.hang &&
3127 		    adev->ip_blocks[i].version->funcs->post_soft_reset)
3128 			r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
3129 		if (r)
3130 			return r;
3131 	}
3132 
3133 	return 0;
3134 }
3135 
3136 /**
3137  * amdgpu_device_recover_vram_from_shadow - restore shadowed VRAM buffers
3138  *
3139  * @adev: amdgpu_device pointer
3140  * @ring: amdgpu_ring for the engine handling the buffer operations
3141  * @bo: amdgpu_bo buffer whose shadow is being restored
3142  * @fence: dma_fence associated with the operation
3143  *
3144  * Restores the VRAM buffer contents from the shadow in GTT.  Used to
3145  * restore things like GPUVM page tables after a GPU reset where
3146  * the contents of VRAM might be lost.
3147  * Returns 0 on success, negative error code on failure.
3148  */
3149 static int amdgpu_device_recover_vram_from_shadow(struct amdgpu_device *adev,
3150 						  struct amdgpu_ring *ring,
3151 						  struct amdgpu_bo *bo,
3152 						  struct dma_fence **fence)
3153 {
3154 	uint32_t domain;
3155 	int r;
3156 
3157 	if (!bo->shadow)
3158 		return 0;
3159 
3160 	r = amdgpu_bo_reserve(bo, true);
3161 	if (r)
3162 		return r;
3163 	domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
3164 	/* if bo has been evicted, then no need to recover */
3165 	if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
3166 		r = amdgpu_bo_validate(bo->shadow);
3167 		if (r) {
3168 			DRM_ERROR("bo validate failed!\n");
3169 			goto err;
3170 		}
3171 
3172 		r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
3173 						 NULL, fence, true);
3174 		if (r) {
3175 			DRM_ERROR("recover page table failed!\n");
3176 			goto err;
3177 		}
3178 	}
3179 err:
3180 	amdgpu_bo_unreserve(bo);
3181 	return r;
3182 }
3183 
3184 /**
3185  * amdgpu_device_handle_vram_lost - Handle the loss of VRAM contents
3186  *
3187  * @adev: amdgpu_device pointer
3188  *
3189  * Restores the contents of VRAM buffers from the shadows in GTT.  Used to
3190  * restore things like GPUVM page tables after a GPU reset where
3191  * the contents of VRAM might be lost.
3192  * Returns 0 on success, 1 on failure.
3193  */
3194 static int amdgpu_device_handle_vram_lost(struct amdgpu_device *adev)
3195 {
3196 	struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
3197 	struct amdgpu_bo *bo, *tmp;
3198 	struct dma_fence *fence = NULL, *next = NULL;
3199 	long r = 1;
3200 	int i = 0;
3201 	long tmo;
3202 
3203 	if (amdgpu_sriov_runtime(adev))
3204 		tmo = msecs_to_jiffies(8000);
3205 	else
3206 		tmo = msecs_to_jiffies(100);
3207 
3208 	DRM_INFO("recover vram bo from shadow start\n");
3209 	mutex_lock(&adev->shadow_list_lock);
3210 	list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
3211 		next = NULL;
3212 		amdgpu_device_recover_vram_from_shadow(adev, ring, bo, &next);
3213 		if (fence) {
3214 			r = dma_fence_wait_timeout(fence, false, tmo);
3215 			if (r == 0)
3216 				pr_err("wait fence %p[%d] timeout\n", fence, i);
3217 			else if (r < 0)
3218 				pr_err("wait fence %p[%d] interrupted\n", fence, i);
3219 			if (r < 1) {
3220 				dma_fence_put(fence);
3221 				fence = next;
3222 				break;
3223 			}
3224 			i++;
3225 		}
3226 
3227 		dma_fence_put(fence);
3228 		fence = next;
3229 	}
3230 	mutex_unlock(&adev->shadow_list_lock);
3231 
3232 	if (fence) {
3233 		r = dma_fence_wait_timeout(fence, false, tmo);
3234 		if (r == 0)
3235 			pr_err("wait fence %p[%d] timeout\n", fence, i);
3236 		else if (r < 0)
3237 			pr_err("wait fence %p[%d] interrupted\n", fence, i);
3238 
3239 	}
3240 	dma_fence_put(fence);
3241 
3242 	if (r > 0)
3243 		DRM_INFO("recover vram bo from shadow done\n");
3244 	else
3245 		DRM_ERROR("recover vram bo from shadow failed\n");
3246 
3247 	return (r > 0) ? 0 : 1;
3248 }
3249 
3250 /**
3251  * amdgpu_device_reset - reset ASIC/GPU for bare-metal or passthrough
3252  *
3253  * @adev: amdgpu device pointer
3254  *
3255  * attempt to do soft-reset or full-reset and reinitialize Asic
3256  * return 0 means succeeded otherwise failed
3257  */
3258 static int amdgpu_device_reset(struct amdgpu_device *adev)
3259 {
3260 	bool need_full_reset, vram_lost = 0;
3261 	int r;
3262 
3263 	need_full_reset = amdgpu_device_ip_need_full_reset(adev);
3264 
3265 	if (!need_full_reset) {
3266 		amdgpu_device_ip_pre_soft_reset(adev);
3267 		r = amdgpu_device_ip_soft_reset(adev);
3268 		amdgpu_device_ip_post_soft_reset(adev);
3269 		if (r || amdgpu_device_ip_check_soft_reset(adev)) {
3270 			DRM_INFO("soft reset failed, will fallback to full reset!\n");
3271 			need_full_reset = true;
3272 		}
3273 	}
3274 
3275 	if (need_full_reset) {
3276 		r = amdgpu_device_ip_suspend(adev);
3277 
3278 retry:
3279 		r = amdgpu_asic_reset(adev);
3280 		/* post card */
3281 		amdgpu_atom_asic_init(adev->mode_info.atom_context);
3282 
3283 		if (!r) {
3284 			dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
3285 			r = amdgpu_device_ip_resume_phase1(adev);
3286 			if (r)
3287 				goto out;
3288 
3289 			vram_lost = amdgpu_device_check_vram_lost(adev);
3290 			if (vram_lost) {
3291 				DRM_ERROR("VRAM is lost!\n");
3292 				atomic_inc(&adev->vram_lost_counter);
3293 			}
3294 
3295 			r = amdgpu_gtt_mgr_recover(
3296 				&adev->mman.bdev.man[TTM_PL_TT]);
3297 			if (r)
3298 				goto out;
3299 
3300 			r = amdgpu_device_ip_resume_phase2(adev);
3301 			if (r)
3302 				goto out;
3303 
3304 			if (vram_lost)
3305 				amdgpu_device_fill_reset_magic(adev);
3306 		}
3307 	}
3308 
3309 out:
3310 	if (!r) {
3311 		amdgpu_irq_gpu_reset_resume_helper(adev);
3312 		r = amdgpu_ib_ring_tests(adev);
3313 		if (r) {
3314 			dev_err(adev->dev, "ib ring test failed (%d).\n", r);
3315 			r = amdgpu_device_ip_suspend(adev);
3316 			need_full_reset = true;
3317 			goto retry;
3318 		}
3319 	}
3320 
3321 	if (!r && ((need_full_reset && !(adev->flags & AMD_IS_APU)) || vram_lost))
3322 		r = amdgpu_device_handle_vram_lost(adev);
3323 
3324 	return r;
3325 }
3326 
3327 /**
3328  * amdgpu_device_reset_sriov - reset ASIC for SR-IOV vf
3329  *
3330  * @adev: amdgpu device pointer
3331  * @from_hypervisor: request from hypervisor
3332  *
3333  * do VF FLR and reinitialize Asic
3334  * return 0 means succeeded otherwise failed
3335  */
3336 static int amdgpu_device_reset_sriov(struct amdgpu_device *adev,
3337 				     bool from_hypervisor)
3338 {
3339 	int r;
3340 
3341 	if (from_hypervisor)
3342 		r = amdgpu_virt_request_full_gpu(adev, true);
3343 	else
3344 		r = amdgpu_virt_reset_gpu(adev);
3345 	if (r)
3346 		return r;
3347 
3348 	/* Resume IP prior to SMC */
3349 	r = amdgpu_device_ip_reinit_early_sriov(adev);
3350 	if (r)
3351 		goto error;
3352 
3353 	/* we need recover gart prior to run SMC/CP/SDMA resume */
3354 	amdgpu_gtt_mgr_recover(&adev->mman.bdev.man[TTM_PL_TT]);
3355 
3356 	/* now we are okay to resume SMC/CP/SDMA */
3357 	r = amdgpu_device_ip_reinit_late_sriov(adev);
3358 	if (r)
3359 		goto error;
3360 
3361 	amdgpu_irq_gpu_reset_resume_helper(adev);
3362 	r = amdgpu_ib_ring_tests(adev);
3363 
3364 error:
3365 	amdgpu_virt_init_data_exchange(adev);
3366 	amdgpu_virt_release_full_gpu(adev, true);
3367 	if (!r && adev->virt.gim_feature & AMDGIM_FEATURE_GIM_FLR_VRAMLOST) {
3368 		atomic_inc(&adev->vram_lost_counter);
3369 		r = amdgpu_device_handle_vram_lost(adev);
3370 	}
3371 
3372 	return r;
3373 }
3374 
3375 /**
3376  * amdgpu_device_gpu_recover - reset the asic and recover scheduler
3377  *
3378  * @adev: amdgpu device pointer
3379  * @job: which job trigger hang
3380  * @force: forces reset regardless of amdgpu_gpu_recovery
3381  *
3382  * Attempt to reset the GPU if it has hung (all asics).
3383  * Returns 0 for success or an error on failure.
3384  */
3385 int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
3386 			      struct amdgpu_job *job, bool force)
3387 {
3388 	int i, r, resched;
3389 
3390 	if (!force && !amdgpu_device_ip_check_soft_reset(adev)) {
3391 		DRM_INFO("No hardware hang detected. Did some blocks stall?\n");
3392 		return 0;
3393 	}
3394 
3395 	if (!force && (amdgpu_gpu_recovery == 0 ||
3396 			(amdgpu_gpu_recovery == -1  && !amdgpu_sriov_vf(adev)))) {
3397 		DRM_INFO("GPU recovery disabled.\n");
3398 		return 0;
3399 	}
3400 
3401 	dev_info(adev->dev, "GPU reset begin!\n");
3402 
3403 	mutex_lock(&adev->lock_reset);
3404 	atomic_inc(&adev->gpu_reset_counter);
3405 	adev->in_gpu_reset = 1;
3406 
3407 	/* Block kfd */
3408 	amdgpu_amdkfd_pre_reset(adev);
3409 
3410 	/* block TTM */
3411 	resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
3412 
3413 	/* block all schedulers and reset given job's ring */
3414 	for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
3415 		struct amdgpu_ring *ring = adev->rings[i];
3416 
3417 		if (!ring || !ring->sched.thread)
3418 			continue;
3419 
3420 		kthread_park(ring->sched.thread);
3421 
3422 		if (job && job->base.sched == &ring->sched)
3423 			continue;
3424 
3425 		drm_sched_hw_job_reset(&ring->sched, job ? &job->base : NULL);
3426 
3427 		/* after all hw jobs are reset, hw fence is meaningless, so force_completion */
3428 		amdgpu_fence_driver_force_completion(ring);
3429 	}
3430 
3431 	if (amdgpu_sriov_vf(adev))
3432 		r = amdgpu_device_reset_sriov(adev, job ? false : true);
3433 	else
3434 		r = amdgpu_device_reset(adev);
3435 
3436 	for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
3437 		struct amdgpu_ring *ring = adev->rings[i];
3438 
3439 		if (!ring || !ring->sched.thread)
3440 			continue;
3441 
3442 		/* only need recovery sched of the given job's ring
3443 		 * or all rings (in the case @job is NULL)
3444 		 * after above amdgpu_reset accomplished
3445 		 */
3446 		if ((!job || job->base.sched == &ring->sched) && !r)
3447 			drm_sched_job_recovery(&ring->sched);
3448 
3449 		kthread_unpark(ring->sched.thread);
3450 	}
3451 
3452 	if (!amdgpu_device_has_dc_support(adev)) {
3453 		drm_helper_resume_force_mode(adev->ddev);
3454 	}
3455 
3456 	ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
3457 
3458 	if (r) {
3459 		/* bad news, how to tell it to userspace ? */
3460 		dev_info(adev->dev, "GPU reset(%d) failed\n", atomic_read(&adev->gpu_reset_counter));
3461 		amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, r);
3462 	} else {
3463 		dev_info(adev->dev, "GPU reset(%d) succeeded!\n",atomic_read(&adev->gpu_reset_counter));
3464 	}
3465 
3466 	/*unlock kfd */
3467 	amdgpu_amdkfd_post_reset(adev);
3468 	amdgpu_vf_error_trans_all(adev);
3469 	adev->in_gpu_reset = 0;
3470 	mutex_unlock(&adev->lock_reset);
3471 	return r;
3472 }
3473 
3474 /**
3475  * amdgpu_device_get_pcie_info - fence pcie info about the PCIE slot
3476  *
3477  * @adev: amdgpu_device pointer
3478  *
3479  * Fetchs and stores in the driver the PCIE capabilities (gen speed
3480  * and lanes) of the slot the device is in. Handles APUs and
3481  * virtualized environments where PCIE config space may not be available.
3482  */
3483 static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev)
3484 {
3485 	struct pci_dev *pdev;
3486 	enum pci_bus_speed speed_cap;
3487 	enum pcie_link_width link_width;
3488 
3489 	if (amdgpu_pcie_gen_cap)
3490 		adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
3491 
3492 	if (amdgpu_pcie_lane_cap)
3493 		adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
3494 
3495 	/* covers APUs as well */
3496 	if (pci_is_root_bus(adev->pdev->bus)) {
3497 		if (adev->pm.pcie_gen_mask == 0)
3498 			adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
3499 		if (adev->pm.pcie_mlw_mask == 0)
3500 			adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
3501 		return;
3502 	}
3503 
3504 	if (adev->pm.pcie_gen_mask == 0) {
3505 		/* asic caps */
3506 		pdev = adev->pdev;
3507 		speed_cap = pcie_get_speed_cap(pdev);
3508 		if (speed_cap == PCI_SPEED_UNKNOWN) {
3509 			adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
3510 						  CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
3511 						  CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
3512 		} else {
3513 			if (speed_cap == PCIE_SPEED_16_0GT)
3514 				adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
3515 							  CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
3516 							  CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3 |
3517 							  CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN4);
3518 			else if (speed_cap == PCIE_SPEED_8_0GT)
3519 				adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
3520 							  CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
3521 							  CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
3522 			else if (speed_cap == PCIE_SPEED_5_0GT)
3523 				adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
3524 							  CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2);
3525 			else
3526 				adev->pm.pcie_gen_mask |= CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1;
3527 		}
3528 		/* platform caps */
3529 		pdev = adev->ddev->pdev->bus->self;
3530 		speed_cap = pcie_get_speed_cap(pdev);
3531 		if (speed_cap == PCI_SPEED_UNKNOWN) {
3532 			adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
3533 						   CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2);
3534 		} else {
3535 			if (speed_cap == PCIE_SPEED_16_0GT)
3536 				adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
3537 							   CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
3538 							   CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3 |
3539 							   CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4);
3540 			else if (speed_cap == PCIE_SPEED_8_0GT)
3541 				adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
3542 							   CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
3543 							   CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3);
3544 			else if (speed_cap == PCIE_SPEED_5_0GT)
3545 				adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
3546 							   CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2);
3547 			else
3548 				adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
3549 
3550 		}
3551 	}
3552 	if (adev->pm.pcie_mlw_mask == 0) {
3553 		pdev = adev->ddev->pdev->bus->self;
3554 		link_width = pcie_get_width_cap(pdev);
3555 		if (link_width == PCIE_LNK_WIDTH_UNKNOWN) {
3556 			adev->pm.pcie_mlw_mask |= AMDGPU_DEFAULT_PCIE_MLW_MASK;
3557 		} else {
3558 			switch (link_width) {
3559 			case PCIE_LNK_X32:
3560 				adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
3561 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
3562 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3563 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3564 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3565 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3566 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3567 				break;
3568 			case PCIE_LNK_X16:
3569 				adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
3570 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3571 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3572 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3573 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3574 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3575 				break;
3576 			case PCIE_LNK_X12:
3577 				adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3578 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3579 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3580 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3581 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3582 				break;
3583 			case PCIE_LNK_X8:
3584 				adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3585 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3586 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3587 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3588 				break;
3589 			case PCIE_LNK_X4:
3590 				adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3591 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3592 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3593 				break;
3594 			case PCIE_LNK_X2:
3595 				adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3596 							  CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3597 				break;
3598 			case PCIE_LNK_X1:
3599 				adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
3600 				break;
3601 			default:
3602 				break;
3603 			}
3604 		}
3605 	}
3606 }
3607 
3608