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