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