drm/amdgpu: Enable light SBR for SMU on passthrough and XGMI configuration
[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         atomic_set(&adev->irq.ih.lock, 0);
3288         mutex_init(&adev->firmware.mutex);
3289         mutex_init(&adev->pm.mutex);
3290         mutex_init(&adev->gfx.gpu_clock_mutex);
3291         mutex_init(&adev->srbm_mutex);
3292         mutex_init(&adev->gfx.pipe_reserve_mutex);
3293         mutex_init(&adev->gfx.gfx_off_mutex);
3294         mutex_init(&adev->grbm_idx_mutex);
3295         mutex_init(&adev->mn_lock);
3296         mutex_init(&adev->virt.vf_errors.lock);
3297         hash_init(adev->mn_hash);
3298         atomic_set(&adev->in_gpu_reset, 0);
3299         init_rwsem(&adev->reset_sem);
3300         mutex_init(&adev->psp.mutex);
3301         mutex_init(&adev->notifier_lock);
3302
3303         r = amdgpu_device_check_arguments(adev);
3304         if (r)
3305                 return r;
3306
3307         spin_lock_init(&adev->mmio_idx_lock);
3308         spin_lock_init(&adev->smc_idx_lock);
3309         spin_lock_init(&adev->pcie_idx_lock);
3310         spin_lock_init(&adev->uvd_ctx_idx_lock);
3311         spin_lock_init(&adev->didt_idx_lock);
3312         spin_lock_init(&adev->gc_cac_idx_lock);
3313         spin_lock_init(&adev->se_cac_idx_lock);
3314         spin_lock_init(&adev->audio_endpt_idx_lock);
3315         spin_lock_init(&adev->mm_stats.lock);
3316
3317         INIT_LIST_HEAD(&adev->shadow_list);
3318         mutex_init(&adev->shadow_list_lock);
3319
3320         INIT_LIST_HEAD(&adev->reset_list);
3321
3322         INIT_DELAYED_WORK(&adev->delayed_init_work,
3323                           amdgpu_device_delayed_init_work_handler);
3324         INIT_DELAYED_WORK(&adev->gfx.gfx_off_delay_work,
3325                           amdgpu_device_delay_enable_gfx_off);
3326
3327         INIT_WORK(&adev->xgmi_reset_work, amdgpu_device_xgmi_reset_func);
3328
3329         adev->gfx.gfx_off_req_count = 1;
3330         adev->pm.ac_power = power_supply_is_system_supplied() > 0;
3331
3332         atomic_set(&adev->throttling_logging_enabled, 1);
3333         /*
3334          * If throttling continues, logging will be performed every minute
3335          * to avoid log flooding. "-1" is subtracted since the thermal
3336          * throttling interrupt comes every second. Thus, the total logging
3337          * interval is 59 seconds(retelimited printk interval) + 1(waiting
3338          * for throttling interrupt) = 60 seconds.
3339          */
3340         ratelimit_state_init(&adev->throttling_logging_rs, (60 - 1) * HZ, 1);
3341         ratelimit_set_flags(&adev->throttling_logging_rs, RATELIMIT_MSG_ON_RELEASE);
3342
3343         /* Registers mapping */
3344         /* TODO: block userspace mapping of io register */
3345         if (adev->asic_type >= CHIP_BONAIRE) {
3346                 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
3347                 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
3348         } else {
3349                 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
3350                 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
3351         }
3352
3353         adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
3354         if (adev->rmmio == NULL) {
3355                 return -ENOMEM;
3356         }
3357         DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
3358         DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
3359
3360         /* io port mapping */
3361         for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
3362                 if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
3363                         adev->rio_mem_size = pci_resource_len(adev->pdev, i);
3364                         adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
3365                         break;
3366                 }
3367         }
3368         if (adev->rio_mem == NULL)
3369                 DRM_INFO("PCI I/O BAR is not found.\n");
3370
3371         /* enable PCIE atomic ops */
3372         r = pci_enable_atomic_ops_to_root(adev->pdev,
3373                                           PCI_EXP_DEVCAP2_ATOMIC_COMP32 |
3374                                           PCI_EXP_DEVCAP2_ATOMIC_COMP64);
3375         if (r) {
3376                 adev->have_atomics_support = false;
3377                 DRM_INFO("PCIE atomic ops is not supported\n");
3378         } else {
3379                 adev->have_atomics_support = true;
3380         }
3381
3382         amdgpu_device_get_pcie_info(adev);
3383
3384         if (amdgpu_mcbp)
3385                 DRM_INFO("MCBP is enabled\n");
3386
3387         if (amdgpu_mes && adev->asic_type >= CHIP_NAVI10)
3388                 adev->enable_mes = true;
3389
3390         /* detect hw virtualization here */
3391         amdgpu_detect_virtualization(adev);
3392
3393         r = amdgpu_device_get_job_timeout_settings(adev);
3394         if (r) {
3395                 dev_err(adev->dev, "invalid lockup_timeout parameter syntax\n");
3396                 goto failed_unmap;
3397         }
3398
3399         /* early init functions */
3400         r = amdgpu_device_ip_early_init(adev);
3401         if (r)
3402                 goto failed_unmap;
3403
3404         /* doorbell bar mapping and doorbell index init*/
3405         amdgpu_device_doorbell_init(adev);
3406
3407         /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
3408         /* this will fail for cards that aren't VGA class devices, just
3409          * ignore it */
3410         if ((adev->pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
3411                 vga_client_register(adev->pdev, adev, NULL, amdgpu_device_vga_set_decode);
3412
3413         if (amdgpu_device_supports_atpx(ddev))
3414                 atpx = true;
3415         if (amdgpu_has_atpx() &&
3416             (amdgpu_is_atpx_hybrid() ||
3417              amdgpu_has_atpx_dgpu_power_cntl()) &&
3418             !pci_is_thunderbolt_attached(adev->pdev))
3419                 vga_switcheroo_register_client(adev->pdev,
3420                                                &amdgpu_switcheroo_ops, atpx);
3421         if (atpx)
3422                 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
3423
3424         if (amdgpu_emu_mode == 1) {
3425                 /* post the asic on emulation mode */
3426                 emu_soc_asic_init(adev);
3427                 goto fence_driver_init;
3428         }
3429
3430         /* detect if we are with an SRIOV vbios */
3431         amdgpu_device_detect_sriov_bios(adev);
3432
3433         /* check if we need to reset the asic
3434          *  E.g., driver was not cleanly unloaded previously, etc.
3435          */
3436         if (!amdgpu_sriov_vf(adev) && amdgpu_asic_need_reset_on_init(adev)) {
3437                 if (adev->gmc.xgmi.num_physical_nodes) {
3438                         dev_info(adev->dev, "Pending hive reset.\n");
3439                         adev->gmc.xgmi.pending_reset = true;
3440                         /* Only need to init necessary block for SMU to handle the reset */
3441                         for (i = 0; i < adev->num_ip_blocks; i++) {
3442                                 if (!adev->ip_blocks[i].status.valid)
3443                                         continue;
3444                                 if (!(adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
3445                                       adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
3446                                       adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH ||
3447                                       adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC)) {
3448                                         DRM_DEBUG("IP %s disabed for hw_init.\n",
3449                                                 adev->ip_blocks[i].version->funcs->name);
3450                                         adev->ip_blocks[i].status.hw = true;
3451                                 }
3452                         }
3453                 } else {
3454                         r = amdgpu_asic_reset(adev);
3455                         if (r) {
3456                                 dev_err(adev->dev, "asic reset on init failed\n");
3457                                 goto failed;
3458                         }
3459                 }
3460         }
3461
3462         pci_enable_pcie_error_reporting(adev->pdev);
3463
3464         /* Post card if necessary */
3465         if (amdgpu_device_need_post(adev)) {
3466                 if (!adev->bios) {
3467                         dev_err(adev->dev, "no vBIOS found\n");
3468                         r = -EINVAL;
3469                         goto failed;
3470                 }
3471                 DRM_INFO("GPU posting now...\n");
3472                 r = amdgpu_device_asic_init(adev);
3473                 if (r) {
3474                         dev_err(adev->dev, "gpu post error!\n");
3475                         goto failed;
3476                 }
3477         }
3478
3479         if (adev->is_atom_fw) {
3480                 /* Initialize clocks */
3481                 r = amdgpu_atomfirmware_get_clock_info(adev);
3482                 if (r) {
3483                         dev_err(adev->dev, "amdgpu_atomfirmware_get_clock_info failed\n");
3484                         amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
3485                         goto failed;
3486                 }
3487         } else {
3488                 /* Initialize clocks */
3489                 r = amdgpu_atombios_get_clock_info(adev);
3490                 if (r) {
3491                         dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
3492                         amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
3493                         goto failed;
3494                 }
3495                 /* init i2c buses */
3496                 if (!amdgpu_device_has_dc_support(adev))
3497                         amdgpu_atombios_i2c_init(adev);
3498         }
3499
3500 fence_driver_init:
3501         /* Fence driver */
3502         r = amdgpu_fence_driver_init(adev);
3503         if (r) {
3504                 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
3505                 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0);
3506                 goto failed;
3507         }
3508
3509         /* init the mode config */
3510         drm_mode_config_init(adev_to_drm(adev));
3511
3512         r = amdgpu_device_ip_init(adev);
3513         if (r) {
3514                 /* failed in exclusive mode due to timeout */
3515                 if (amdgpu_sriov_vf(adev) &&
3516                     !amdgpu_sriov_runtime(adev) &&
3517                     amdgpu_virt_mmio_blocked(adev) &&
3518                     !amdgpu_virt_wait_reset(adev)) {
3519                         dev_err(adev->dev, "VF exclusive mode timeout\n");
3520                         /* Don't send request since VF is inactive. */
3521                         adev->virt.caps &= ~AMDGPU_SRIOV_CAPS_RUNTIME;
3522                         adev->virt.ops = NULL;
3523                         r = -EAGAIN;
3524                         goto failed;
3525                 }
3526                 dev_err(adev->dev, "amdgpu_device_ip_init failed\n");
3527                 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0);
3528                 goto failed;
3529         }
3530
3531         dev_info(adev->dev,
3532                 "SE %d, SH per SE %d, CU per SH %d, active_cu_number %d\n",
3533                         adev->gfx.config.max_shader_engines,
3534                         adev->gfx.config.max_sh_per_se,
3535                         adev->gfx.config.max_cu_per_sh,
3536                         adev->gfx.cu_info.number);
3537
3538         adev->accel_working = true;
3539
3540         amdgpu_vm_check_compute_bug(adev);
3541
3542         /* Initialize the buffer migration limit. */
3543         if (amdgpu_moverate >= 0)
3544                 max_MBps = amdgpu_moverate;
3545         else
3546                 max_MBps = 8; /* Allow 8 MB/s. */
3547         /* Get a log2 for easy divisions. */
3548         adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
3549
3550         amdgpu_fbdev_init(adev);
3551
3552         r = amdgpu_pm_sysfs_init(adev);
3553         if (r) {
3554                 adev->pm_sysfs_en = false;
3555                 DRM_ERROR("registering pm debugfs failed (%d).\n", r);
3556         } else
3557                 adev->pm_sysfs_en = true;
3558
3559         r = amdgpu_ucode_sysfs_init(adev);
3560         if (r) {
3561                 adev->ucode_sysfs_en = false;
3562                 DRM_ERROR("Creating firmware sysfs failed (%d).\n", r);
3563         } else
3564                 adev->ucode_sysfs_en = true;
3565
3566         if ((amdgpu_testing & 1)) {
3567                 if (adev->accel_working)
3568                         amdgpu_test_moves(adev);
3569                 else
3570                         DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
3571         }
3572         if (amdgpu_benchmarking) {
3573                 if (adev->accel_working)
3574                         amdgpu_benchmark(adev, amdgpu_benchmarking);
3575                 else
3576                         DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
3577         }
3578
3579         /*
3580          * Register gpu instance before amdgpu_device_enable_mgpu_fan_boost.
3581          * Otherwise the mgpu fan boost feature will be skipped due to the
3582          * gpu instance is counted less.
3583          */
3584         amdgpu_register_gpu_instance(adev);
3585
3586         /* enable clockgating, etc. after ib tests, etc. since some blocks require
3587          * explicit gating rather than handling it automatically.
3588          */
3589         if (!adev->gmc.xgmi.pending_reset) {
3590                 r = amdgpu_device_ip_late_init(adev);
3591                 if (r) {
3592                         dev_err(adev->dev, "amdgpu_device_ip_late_init failed\n");
3593                         amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r);
3594                         goto failed;
3595                 }
3596                 /* must succeed. */
3597                 amdgpu_ras_resume(adev);
3598                 queue_delayed_work(system_wq, &adev->delayed_init_work,
3599                                    msecs_to_jiffies(AMDGPU_RESUME_MS));
3600         }
3601
3602         if (amdgpu_sriov_vf(adev))
3603                 flush_delayed_work(&adev->delayed_init_work);
3604
3605         r = sysfs_create_files(&adev->dev->kobj, amdgpu_dev_attributes);
3606         if (r)
3607                 dev_err(adev->dev, "Could not create amdgpu device attr\n");
3608
3609         if (IS_ENABLED(CONFIG_PERF_EVENTS))
3610                 r = amdgpu_pmu_init(adev);
3611         if (r)
3612                 dev_err(adev->dev, "amdgpu_pmu_init failed\n");
3613
3614         /* Have stored pci confspace at hand for restore in sudden PCI error */
3615         if (amdgpu_device_cache_pci_state(adev->pdev))
3616                 pci_restore_state(pdev);
3617
3618         /* Enable lightSBR on SMU in passthrough + xgmi configuration */
3619         if (amdgpu_passthrough(adev) && adev->gmc.xgmi.num_physical_nodes > 1)
3620                 smu_set_light_sbr(&adev->smu, true);
3621
3622         if (adev->gmc.xgmi.pending_reset)
3623                 queue_delayed_work(system_wq, &mgpu_info.delayed_reset_work,
3624                                    msecs_to_jiffies(AMDGPU_RESUME_MS));
3625
3626         return 0;
3627
3628 failed:
3629         amdgpu_vf_error_trans_all(adev);
3630         if (atpx)
3631                 vga_switcheroo_fini_domain_pm_ops(adev->dev);
3632
3633 failed_unmap:
3634         iounmap(adev->rmmio);
3635         adev->rmmio = NULL;
3636
3637         return r;
3638 }
3639
3640 /**
3641  * amdgpu_device_fini - tear down the driver
3642  *
3643  * @adev: amdgpu_device pointer
3644  *
3645  * Tear down the driver info (all asics).
3646  * Called at driver shutdown.
3647  */
3648 void amdgpu_device_fini(struct amdgpu_device *adev)
3649 {
3650         dev_info(adev->dev, "amdgpu: finishing device.\n");
3651         flush_delayed_work(&adev->delayed_init_work);
3652         ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
3653         adev->shutdown = true;
3654
3655         kfree(adev->pci_state);
3656
3657         /* make sure IB test finished before entering exclusive mode
3658          * to avoid preemption on IB test
3659          * */
3660         if (amdgpu_sriov_vf(adev)) {
3661                 amdgpu_virt_request_full_gpu(adev, false);
3662                 amdgpu_virt_fini_data_exchange(adev);
3663         }
3664
3665         /* disable all interrupts */
3666         amdgpu_irq_disable_all(adev);
3667         if (adev->mode_info.mode_config_initialized){
3668                 if (!amdgpu_device_has_dc_support(adev))
3669                         drm_helper_force_disable_all(adev_to_drm(adev));
3670                 else
3671                         drm_atomic_helper_shutdown(adev_to_drm(adev));
3672         }
3673         amdgpu_fence_driver_fini(adev);
3674         if (adev->pm_sysfs_en)
3675                 amdgpu_pm_sysfs_fini(adev);
3676         amdgpu_fbdev_fini(adev);
3677         amdgpu_device_ip_fini(adev);
3678         release_firmware(adev->firmware.gpu_info_fw);
3679         adev->firmware.gpu_info_fw = NULL;
3680         adev->accel_working = false;
3681         /* free i2c buses */
3682         if (!amdgpu_device_has_dc_support(adev))
3683                 amdgpu_i2c_fini(adev);
3684
3685         if (amdgpu_emu_mode != 1)
3686                 amdgpu_atombios_fini(adev);
3687
3688         kfree(adev->bios);
3689         adev->bios = NULL;
3690         if (amdgpu_has_atpx() &&
3691             (amdgpu_is_atpx_hybrid() ||
3692              amdgpu_has_atpx_dgpu_power_cntl()) &&
3693             !pci_is_thunderbolt_attached(adev->pdev))
3694                 vga_switcheroo_unregister_client(adev->pdev);
3695         if (amdgpu_device_supports_atpx(adev_to_drm(adev)))
3696                 vga_switcheroo_fini_domain_pm_ops(adev->dev);
3697         if ((adev->pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA)
3698                 vga_client_register(adev->pdev, NULL, NULL, NULL);
3699         if (adev->rio_mem)
3700                 pci_iounmap(adev->pdev, adev->rio_mem);
3701         adev->rio_mem = NULL;
3702         iounmap(adev->rmmio);
3703         adev->rmmio = NULL;
3704         amdgpu_device_doorbell_fini(adev);
3705
3706         if (adev->ucode_sysfs_en)
3707                 amdgpu_ucode_sysfs_fini(adev);
3708
3709         sysfs_remove_files(&adev->dev->kobj, amdgpu_dev_attributes);
3710         if (IS_ENABLED(CONFIG_PERF_EVENTS))
3711                 amdgpu_pmu_fini(adev);
3712         if (adev->mman.discovery_bin)
3713                 amdgpu_discovery_fini(adev);
3714 }
3715
3716
3717 /*
3718  * Suspend & resume.
3719  */
3720 /**
3721  * amdgpu_device_suspend - initiate device suspend
3722  *
3723  * @dev: drm dev pointer
3724  * @fbcon : notify the fbdev of suspend
3725  *
3726  * Puts the hw in the suspend state (all asics).
3727  * Returns 0 for success or an error on failure.
3728  * Called at driver suspend.
3729  */
3730 int amdgpu_device_suspend(struct drm_device *dev, bool fbcon)
3731 {
3732         struct amdgpu_device *adev;
3733         struct drm_crtc *crtc;
3734         struct drm_connector *connector;
3735         struct drm_connector_list_iter iter;
3736         int r;
3737
3738         adev = drm_to_adev(dev);
3739
3740         if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
3741                 return 0;
3742
3743         adev->in_suspend = true;
3744         drm_kms_helper_poll_disable(dev);
3745
3746         if (fbcon)
3747                 amdgpu_fbdev_set_suspend(adev, 1);
3748
3749         cancel_delayed_work_sync(&adev->delayed_init_work);
3750
3751         if (!amdgpu_device_has_dc_support(adev)) {
3752                 /* turn off display hw */
3753                 drm_modeset_lock_all(dev);
3754                 drm_connector_list_iter_begin(dev, &iter);
3755                 drm_for_each_connector_iter(connector, &iter)
3756                         drm_helper_connector_dpms(connector,
3757                                                   DRM_MODE_DPMS_OFF);
3758                 drm_connector_list_iter_end(&iter);
3759                 drm_modeset_unlock_all(dev);
3760                         /* unpin the front buffers and cursors */
3761                 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
3762                         struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
3763                         struct drm_framebuffer *fb = crtc->primary->fb;
3764                         struct amdgpu_bo *robj;
3765
3766                         if (amdgpu_crtc->cursor_bo && !adev->enable_virtual_display) {
3767                                 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
3768                                 r = amdgpu_bo_reserve(aobj, true);
3769                                 if (r == 0) {
3770                                         amdgpu_bo_unpin(aobj);
3771                                         amdgpu_bo_unreserve(aobj);
3772                                 }
3773                         }
3774
3775                         if (fb == NULL || fb->obj[0] == NULL) {
3776                                 continue;
3777                         }
3778                         robj = gem_to_amdgpu_bo(fb->obj[0]);
3779                         /* don't unpin kernel fb objects */
3780                         if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
3781                                 r = amdgpu_bo_reserve(robj, true);
3782                                 if (r == 0) {
3783                                         amdgpu_bo_unpin(robj);
3784                                         amdgpu_bo_unreserve(robj);
3785                                 }
3786                         }
3787                 }
3788         }
3789
3790         amdgpu_ras_suspend(adev);
3791
3792         r = amdgpu_device_ip_suspend_phase1(adev);
3793
3794         amdgpu_amdkfd_suspend(adev, adev->in_runpm);
3795
3796         /* evict vram memory */
3797         amdgpu_bo_evict_vram(adev);
3798
3799         amdgpu_fence_driver_suspend(adev);
3800
3801         if (adev->in_poweroff_reboot_com ||
3802             !amdgpu_acpi_is_s0ix_supported(adev) || amdgpu_in_reset(adev))
3803                 r = amdgpu_device_ip_suspend_phase2(adev);
3804         else
3805                 amdgpu_gfx_state_change_set(adev, sGpuChangeState_D3Entry);
3806         /* evict remaining vram memory
3807          * This second call to evict vram is to evict the gart page table
3808          * using the CPU.
3809          */
3810         amdgpu_bo_evict_vram(adev);
3811
3812         return 0;
3813 }
3814
3815 /**
3816  * amdgpu_device_resume - initiate device resume
3817  *
3818  * @dev: drm dev pointer
3819  * @fbcon : notify the fbdev of resume
3820  *
3821  * Bring the hw back to operating state (all asics).
3822  * Returns 0 for success or an error on failure.
3823  * Called at driver resume.
3824  */
3825 int amdgpu_device_resume(struct drm_device *dev, bool fbcon)
3826 {
3827         struct drm_connector *connector;
3828         struct drm_connector_list_iter iter;
3829         struct amdgpu_device *adev = drm_to_adev(dev);
3830         struct drm_crtc *crtc;
3831         int r = 0;
3832
3833         if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
3834                 return 0;
3835
3836         if (amdgpu_acpi_is_s0ix_supported(adev))
3837                 amdgpu_gfx_state_change_set(adev, sGpuChangeState_D0Entry);
3838
3839         /* post card */
3840         if (amdgpu_device_need_post(adev)) {
3841                 r = amdgpu_device_asic_init(adev);
3842                 if (r)
3843                         dev_err(adev->dev, "amdgpu asic init failed\n");
3844         }
3845
3846         r = amdgpu_device_ip_resume(adev);
3847         if (r) {
3848                 dev_err(adev->dev, "amdgpu_device_ip_resume failed (%d).\n", r);
3849                 return r;
3850         }
3851         amdgpu_fence_driver_resume(adev);
3852
3853
3854         r = amdgpu_device_ip_late_init(adev);
3855         if (r)
3856                 return r;
3857
3858         queue_delayed_work(system_wq, &adev->delayed_init_work,
3859                            msecs_to_jiffies(AMDGPU_RESUME_MS));
3860
3861         if (!amdgpu_device_has_dc_support(adev)) {
3862                 /* pin cursors */
3863                 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
3864                         struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
3865
3866                         if (amdgpu_crtc->cursor_bo && !adev->enable_virtual_display) {
3867                                 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
3868                                 r = amdgpu_bo_reserve(aobj, true);
3869                                 if (r == 0) {
3870                                         r = amdgpu_bo_pin(aobj, AMDGPU_GEM_DOMAIN_VRAM);
3871                                         if (r != 0)
3872                                                 dev_err(adev->dev, "Failed to pin cursor BO (%d)\n", r);
3873                                         amdgpu_crtc->cursor_addr = amdgpu_bo_gpu_offset(aobj);
3874                                         amdgpu_bo_unreserve(aobj);
3875                                 }
3876                         }
3877                 }
3878         }
3879         r = amdgpu_amdkfd_resume(adev, adev->in_runpm);
3880         if (r)
3881                 return r;
3882
3883         /* Make sure IB tests flushed */
3884         flush_delayed_work(&adev->delayed_init_work);
3885
3886         /* blat the mode back in */
3887         if (fbcon) {
3888                 if (!amdgpu_device_has_dc_support(adev)) {
3889                         /* pre DCE11 */
3890                         drm_helper_resume_force_mode(dev);
3891
3892                         /* turn on display hw */
3893                         drm_modeset_lock_all(dev);
3894
3895                         drm_connector_list_iter_begin(dev, &iter);
3896                         drm_for_each_connector_iter(connector, &iter)
3897                                 drm_helper_connector_dpms(connector,
3898                                                           DRM_MODE_DPMS_ON);
3899                         drm_connector_list_iter_end(&iter);
3900
3901                         drm_modeset_unlock_all(dev);
3902                 }
3903                 amdgpu_fbdev_set_suspend(adev, 0);
3904         }
3905
3906         drm_kms_helper_poll_enable(dev);
3907
3908         amdgpu_ras_resume(adev);
3909
3910         /*
3911          * Most of the connector probing functions try to acquire runtime pm
3912          * refs to ensure that the GPU is powered on when connector polling is
3913          * performed. Since we're calling this from a runtime PM callback,
3914          * trying to acquire rpm refs will cause us to deadlock.
3915          *
3916          * Since we're guaranteed to be holding the rpm lock, it's safe to
3917          * temporarily disable the rpm helpers so this doesn't deadlock us.
3918          */
3919 #ifdef CONFIG_PM
3920         dev->dev->power.disable_depth++;
3921 #endif
3922         if (!amdgpu_device_has_dc_support(adev))
3923                 drm_helper_hpd_irq_event(dev);
3924         else
3925                 drm_kms_helper_hotplug_event(dev);
3926 #ifdef CONFIG_PM
3927         dev->dev->power.disable_depth--;
3928 #endif
3929         adev->in_suspend = false;
3930
3931         return 0;
3932 }
3933
3934 /**
3935  * amdgpu_device_ip_check_soft_reset - did soft reset succeed
3936  *
3937  * @adev: amdgpu_device pointer
3938  *
3939  * The list of all the hardware IPs that make up the asic is walked and
3940  * the check_soft_reset callbacks are run.  check_soft_reset determines
3941  * if the asic is still hung or not.
3942  * Returns true if any of the IPs are still in a hung state, false if not.
3943  */
3944 static bool amdgpu_device_ip_check_soft_reset(struct amdgpu_device *adev)
3945 {
3946         int i;
3947         bool asic_hang = false;
3948
3949         if (amdgpu_sriov_vf(adev))
3950                 return true;
3951
3952         if (amdgpu_asic_need_full_reset(adev))
3953                 return true;
3954
3955         for (i = 0; i < adev->num_ip_blocks; i++) {
3956                 if (!adev->ip_blocks[i].status.valid)
3957                         continue;
3958                 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
3959                         adev->ip_blocks[i].status.hang =
3960                                 adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
3961                 if (adev->ip_blocks[i].status.hang) {
3962                         dev_info(adev->dev, "IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
3963                         asic_hang = true;
3964                 }
3965         }
3966         return asic_hang;
3967 }
3968
3969 /**
3970  * amdgpu_device_ip_pre_soft_reset - prepare for soft reset
3971  *
3972  * @adev: amdgpu_device pointer
3973  *
3974  * The list of all the hardware IPs that make up the asic is walked and the
3975  * pre_soft_reset callbacks are run if the block is hung.  pre_soft_reset
3976  * handles any IP specific hardware or software state changes that are
3977  * necessary for a soft reset to succeed.
3978  * Returns 0 on success, negative error code on failure.
3979  */
3980 static int amdgpu_device_ip_pre_soft_reset(struct amdgpu_device *adev)
3981 {
3982         int i, r = 0;
3983
3984         for (i = 0; i < adev->num_ip_blocks; i++) {
3985                 if (!adev->ip_blocks[i].status.valid)
3986                         continue;
3987                 if (adev->ip_blocks[i].status.hang &&
3988                     adev->ip_blocks[i].version->funcs->pre_soft_reset) {
3989                         r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
3990                         if (r)
3991                                 return r;
3992                 }
3993         }
3994
3995         return 0;
3996 }
3997
3998 /**
3999  * amdgpu_device_ip_need_full_reset - check if a full asic reset is needed
4000  *
4001  * @adev: amdgpu_device pointer
4002  *
4003  * Some hardware IPs cannot be soft reset.  If they are hung, a full gpu
4004  * reset is necessary to recover.
4005  * Returns true if a full asic reset is required, false if not.
4006  */
4007 static bool amdgpu_device_ip_need_full_reset(struct amdgpu_device *adev)
4008 {
4009         int i;
4010
4011         if (amdgpu_asic_need_full_reset(adev))
4012                 return true;
4013
4014         for (i = 0; i < adev->num_ip_blocks; i++) {
4015                 if (!adev->ip_blocks[i].status.valid)
4016                         continue;
4017                 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
4018                     (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
4019                     (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
4020                     (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) ||
4021                      adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
4022                         if (adev->ip_blocks[i].status.hang) {
4023                                 dev_info(adev->dev, "Some block need full reset!\n");
4024                                 return true;
4025                         }
4026                 }
4027         }
4028         return false;
4029 }
4030
4031 /**
4032  * amdgpu_device_ip_soft_reset - do a soft reset
4033  *
4034  * @adev: amdgpu_device pointer
4035  *
4036  * The list of all the hardware IPs that make up the asic is walked and the
4037  * soft_reset callbacks are run if the block is hung.  soft_reset handles any
4038  * IP specific hardware or software state changes that are necessary to soft
4039  * reset the IP.
4040  * Returns 0 on success, negative error code on failure.
4041  */
4042 static int amdgpu_device_ip_soft_reset(struct amdgpu_device *adev)
4043 {
4044         int i, r = 0;
4045
4046         for (i = 0; i < adev->num_ip_blocks; i++) {
4047                 if (!adev->ip_blocks[i].status.valid)
4048                         continue;
4049                 if (adev->ip_blocks[i].status.hang &&
4050                     adev->ip_blocks[i].version->funcs->soft_reset) {
4051                         r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
4052                         if (r)
4053                                 return r;
4054                 }
4055         }
4056
4057         return 0;
4058 }
4059
4060 /**
4061  * amdgpu_device_ip_post_soft_reset - clean up from soft reset
4062  *
4063  * @adev: amdgpu_device pointer
4064  *
4065  * The list of all the hardware IPs that make up the asic is walked and the
4066  * post_soft_reset callbacks are run if the asic was hung.  post_soft_reset
4067  * handles any IP specific hardware or software state changes that are
4068  * necessary after the IP has been soft reset.
4069  * Returns 0 on success, negative error code on failure.
4070  */
4071 static int amdgpu_device_ip_post_soft_reset(struct amdgpu_device *adev)
4072 {
4073         int i, r = 0;
4074
4075         for (i = 0; i < adev->num_ip_blocks; i++) {
4076                 if (!adev->ip_blocks[i].status.valid)
4077                         continue;
4078                 if (adev->ip_blocks[i].status.hang &&
4079                     adev->ip_blocks[i].version->funcs->post_soft_reset)
4080                         r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
4081                 if (r)
4082                         return r;
4083         }
4084
4085         return 0;
4086 }
4087
4088 /**
4089  * amdgpu_device_recover_vram - Recover some VRAM contents
4090  *
4091  * @adev: amdgpu_device pointer
4092  *
4093  * Restores the contents of VRAM buffers from the shadows in GTT.  Used to
4094  * restore things like GPUVM page tables after a GPU reset where
4095  * the contents of VRAM might be lost.
4096  *
4097  * Returns:
4098  * 0 on success, negative error code on failure.
4099  */
4100 static int amdgpu_device_recover_vram(struct amdgpu_device *adev)
4101 {
4102         struct dma_fence *fence = NULL, *next = NULL;
4103         struct amdgpu_bo *shadow;
4104         long r = 1, tmo;
4105
4106         if (amdgpu_sriov_runtime(adev))
4107                 tmo = msecs_to_jiffies(8000);
4108         else
4109                 tmo = msecs_to_jiffies(100);
4110
4111         dev_info(adev->dev, "recover vram bo from shadow start\n");
4112         mutex_lock(&adev->shadow_list_lock);
4113         list_for_each_entry(shadow, &adev->shadow_list, shadow_list) {
4114
4115                 /* No need to recover an evicted BO */
4116                 if (shadow->tbo.mem.mem_type != TTM_PL_TT ||
4117                     shadow->tbo.mem.start == AMDGPU_BO_INVALID_OFFSET ||
4118                     shadow->parent->tbo.mem.mem_type != TTM_PL_VRAM)
4119                         continue;
4120
4121                 r = amdgpu_bo_restore_shadow(shadow, &next);
4122                 if (r)
4123                         break;
4124
4125                 if (fence) {
4126                         tmo = dma_fence_wait_timeout(fence, false, tmo);
4127                         dma_fence_put(fence);
4128                         fence = next;
4129                         if (tmo == 0) {
4130                                 r = -ETIMEDOUT;
4131                                 break;
4132                         } else if (tmo < 0) {
4133                                 r = tmo;
4134                                 break;
4135                         }
4136                 } else {
4137                         fence = next;
4138                 }
4139         }
4140         mutex_unlock(&adev->shadow_list_lock);
4141
4142         if (fence)
4143                 tmo = dma_fence_wait_timeout(fence, false, tmo);
4144         dma_fence_put(fence);
4145
4146         if (r < 0 || tmo <= 0) {
4147                 dev_err(adev->dev, "recover vram bo from shadow failed, r is %ld, tmo is %ld\n", r, tmo);
4148                 return -EIO;
4149         }
4150
4151         dev_info(adev->dev, "recover vram bo from shadow done\n");
4152         return 0;
4153 }
4154
4155
4156 /**
4157  * amdgpu_device_reset_sriov - reset ASIC for SR-IOV vf
4158  *
4159  * @adev: amdgpu_device pointer
4160  * @from_hypervisor: request from hypervisor
4161  *
4162  * do VF FLR and reinitialize Asic
4163  * return 0 means succeeded otherwise failed
4164  */
4165 static int amdgpu_device_reset_sriov(struct amdgpu_device *adev,
4166                                      bool from_hypervisor)
4167 {
4168         int r;
4169
4170         if (from_hypervisor)
4171                 r = amdgpu_virt_request_full_gpu(adev, true);
4172         else
4173                 r = amdgpu_virt_reset_gpu(adev);
4174         if (r)
4175                 return r;
4176
4177         amdgpu_amdkfd_pre_reset(adev);
4178
4179         /* Resume IP prior to SMC */
4180         r = amdgpu_device_ip_reinit_early_sriov(adev);
4181         if (r)
4182                 goto error;
4183
4184         amdgpu_virt_init_data_exchange(adev);
4185         /* we need recover gart prior to run SMC/CP/SDMA resume */
4186         amdgpu_gtt_mgr_recover(ttm_manager_type(&adev->mman.bdev, TTM_PL_TT));
4187
4188         r = amdgpu_device_fw_loading(adev);
4189         if (r)
4190                 return r;
4191
4192         /* now we are okay to resume SMC/CP/SDMA */
4193         r = amdgpu_device_ip_reinit_late_sriov(adev);
4194         if (r)
4195                 goto error;
4196
4197         amdgpu_irq_gpu_reset_resume_helper(adev);
4198         r = amdgpu_ib_ring_tests(adev);
4199         amdgpu_amdkfd_post_reset(adev);
4200
4201 error:
4202         amdgpu_virt_release_full_gpu(adev, true);
4203         if (!r && adev->virt.gim_feature & AMDGIM_FEATURE_GIM_FLR_VRAMLOST) {
4204                 amdgpu_inc_vram_lost(adev);
4205                 r = amdgpu_device_recover_vram(adev);
4206         }
4207
4208         return r;
4209 }
4210
4211 /**
4212  * amdgpu_device_has_job_running - check if there is any job in mirror list
4213  *
4214  * @adev: amdgpu_device pointer
4215  *
4216  * check if there is any job in mirror list
4217  */
4218 bool amdgpu_device_has_job_running(struct amdgpu_device *adev)
4219 {
4220         int i;
4221         struct drm_sched_job *job;
4222
4223         for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
4224                 struct amdgpu_ring *ring = adev->rings[i];
4225
4226                 if (!ring || !ring->sched.thread)
4227                         continue;
4228
4229                 spin_lock(&ring->sched.job_list_lock);
4230                 job = list_first_entry_or_null(&ring->sched.pending_list,
4231                                                struct drm_sched_job, list);
4232                 spin_unlock(&ring->sched.job_list_lock);
4233                 if (job)
4234                         return true;
4235         }
4236         return false;
4237 }
4238
4239 /**
4240  * amdgpu_device_should_recover_gpu - check if we should try GPU recovery
4241  *
4242  * @adev: amdgpu_device pointer
4243  *
4244  * Check amdgpu_gpu_recovery and SRIOV status to see if we should try to recover
4245  * a hung GPU.
4246  */
4247 bool amdgpu_device_should_recover_gpu(struct amdgpu_device *adev)
4248 {
4249         if (!amdgpu_device_ip_check_soft_reset(adev)) {
4250                 dev_info(adev->dev, "Timeout, but no hardware hang detected.\n");
4251                 return false;
4252         }
4253
4254         if (amdgpu_gpu_recovery == 0)
4255                 goto disabled;
4256
4257         if (amdgpu_sriov_vf(adev))
4258                 return true;
4259
4260         if (amdgpu_gpu_recovery == -1) {
4261                 switch (adev->asic_type) {
4262                 case CHIP_BONAIRE:
4263                 case CHIP_HAWAII:
4264                 case CHIP_TOPAZ:
4265                 case CHIP_TONGA:
4266                 case CHIP_FIJI:
4267                 case CHIP_POLARIS10:
4268                 case CHIP_POLARIS11:
4269                 case CHIP_POLARIS12:
4270                 case CHIP_VEGAM:
4271                 case CHIP_VEGA20:
4272                 case CHIP_VEGA10:
4273                 case CHIP_VEGA12:
4274                 case CHIP_RAVEN:
4275                 case CHIP_ARCTURUS:
4276                 case CHIP_RENOIR:
4277                 case CHIP_NAVI10:
4278                 case CHIP_NAVI14:
4279                 case CHIP_NAVI12:
4280                 case CHIP_SIENNA_CICHLID:
4281                 case CHIP_NAVY_FLOUNDER:
4282                 case CHIP_DIMGREY_CAVEFISH:
4283                         break;
4284                 default:
4285                         goto disabled;
4286                 }
4287         }
4288
4289         return true;
4290
4291 disabled:
4292                 dev_info(adev->dev, "GPU recovery disabled.\n");
4293                 return false;
4294 }
4295
4296 int amdgpu_device_mode1_reset(struct amdgpu_device *adev)
4297 {
4298         u32 i;
4299         int ret = 0;
4300
4301         amdgpu_atombios_scratch_regs_engine_hung(adev, true);
4302
4303         dev_info(adev->dev, "GPU mode1 reset\n");
4304
4305         /* disable BM */
4306         pci_clear_master(adev->pdev);
4307
4308         amdgpu_device_cache_pci_state(adev->pdev);
4309
4310         if (amdgpu_dpm_is_mode1_reset_supported(adev)) {
4311                 dev_info(adev->dev, "GPU smu mode1 reset\n");
4312                 ret = amdgpu_dpm_mode1_reset(adev);
4313         } else {
4314                 dev_info(adev->dev, "GPU psp mode1 reset\n");
4315                 ret = psp_gpu_reset(adev);
4316         }
4317
4318         if (ret)
4319                 dev_err(adev->dev, "GPU mode1 reset failed\n");
4320
4321         amdgpu_device_load_pci_state(adev->pdev);
4322
4323         /* wait for asic to come out of reset */
4324         for (i = 0; i < adev->usec_timeout; i++) {
4325                 u32 memsize = adev->nbio.funcs->get_memsize(adev);
4326
4327                 if (memsize != 0xffffffff)
4328                         break;
4329                 udelay(1);
4330         }
4331
4332         amdgpu_atombios_scratch_regs_engine_hung(adev, false);
4333         return ret;
4334 }
4335
4336 int amdgpu_device_pre_asic_reset(struct amdgpu_device *adev,
4337                                   struct amdgpu_job *job,
4338                                   bool *need_full_reset_arg)
4339 {
4340         int i, r = 0;
4341         bool need_full_reset  = *need_full_reset_arg;
4342
4343         /* no need to dump if device is not in good state during probe period */
4344         if (!adev->gmc.xgmi.pending_reset)
4345                 amdgpu_debugfs_wait_dump(adev);
4346
4347         if (amdgpu_sriov_vf(adev)) {
4348                 /* stop the data exchange thread */
4349                 amdgpu_virt_fini_data_exchange(adev);
4350         }
4351
4352         /* block all schedulers and reset given job's ring */
4353         for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
4354                 struct amdgpu_ring *ring = adev->rings[i];
4355
4356                 if (!ring || !ring->sched.thread)
4357                         continue;
4358
4359                 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
4360                 amdgpu_fence_driver_force_completion(ring);
4361         }
4362
4363         if(job)
4364                 drm_sched_increase_karma(&job->base);
4365
4366         /* Don't suspend on bare metal if we are not going to HW reset the ASIC */
4367         if (!amdgpu_sriov_vf(adev)) {
4368
4369                 if (!need_full_reset)
4370                         need_full_reset = amdgpu_device_ip_need_full_reset(adev);
4371
4372                 if (!need_full_reset) {
4373                         amdgpu_device_ip_pre_soft_reset(adev);
4374                         r = amdgpu_device_ip_soft_reset(adev);
4375                         amdgpu_device_ip_post_soft_reset(adev);
4376                         if (r || amdgpu_device_ip_check_soft_reset(adev)) {
4377                                 dev_info(adev->dev, "soft reset failed, will fallback to full reset!\n");
4378                                 need_full_reset = true;
4379                         }
4380                 }
4381
4382                 if (need_full_reset)
4383                         r = amdgpu_device_ip_suspend(adev);
4384
4385                 *need_full_reset_arg = need_full_reset;
4386         }
4387
4388         return r;
4389 }
4390
4391 int amdgpu_do_asic_reset(struct amdgpu_hive_info *hive,
4392                           struct list_head *device_list_handle,
4393                           bool *need_full_reset_arg,
4394                           bool skip_hw_reset)
4395 {
4396         struct amdgpu_device *tmp_adev = NULL;
4397         bool need_full_reset = *need_full_reset_arg, vram_lost = false;
4398         int r = 0;
4399
4400         /*
4401          * ASIC reset has to be done on all XGMI hive nodes ASAP
4402          * to allow proper links negotiation in FW (within 1 sec)
4403          */
4404         if (!skip_hw_reset && need_full_reset) {
4405                 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
4406                         /* For XGMI run all resets in parallel to speed up the process */
4407                         if (tmp_adev->gmc.xgmi.num_physical_nodes > 1) {
4408                                 tmp_adev->gmc.xgmi.pending_reset = false;
4409                                 if (!queue_work(system_unbound_wq, &tmp_adev->xgmi_reset_work))
4410                                         r = -EALREADY;
4411                         } else
4412                                 r = amdgpu_asic_reset(tmp_adev);
4413
4414                         if (r) {
4415                                 dev_err(tmp_adev->dev, "ASIC reset failed with error, %d for drm dev, %s",
4416                                          r, adev_to_drm(tmp_adev)->unique);
4417                                 break;
4418                         }
4419                 }
4420
4421                 /* For XGMI wait for all resets to complete before proceed */
4422                 if (!r) {
4423                         list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
4424                                 if (tmp_adev->gmc.xgmi.num_physical_nodes > 1) {
4425                                         flush_work(&tmp_adev->xgmi_reset_work);
4426                                         r = tmp_adev->asic_reset_res;
4427                                         if (r)
4428                                                 break;
4429                                 }
4430                         }
4431                 }
4432         }
4433
4434         if (!r && amdgpu_ras_intr_triggered()) {
4435                 list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
4436                         if (tmp_adev->mmhub.funcs &&
4437                             tmp_adev->mmhub.funcs->reset_ras_error_count)
4438                                 tmp_adev->mmhub.funcs->reset_ras_error_count(tmp_adev);
4439                 }
4440
4441                 amdgpu_ras_intr_cleared();
4442         }
4443
4444         list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
4445                 if (need_full_reset) {
4446                         /* post card */
4447                         r = amdgpu_device_asic_init(tmp_adev);
4448                         if (r) {
4449                                 dev_warn(tmp_adev->dev, "asic atom init failed!");
4450                         } else {
4451                                 dev_info(tmp_adev->dev, "GPU reset succeeded, trying to resume\n");
4452                                 r = amdgpu_device_ip_resume_phase1(tmp_adev);
4453                                 if (r)
4454                                         goto out;
4455
4456                                 vram_lost = amdgpu_device_check_vram_lost(tmp_adev);
4457                                 if (vram_lost) {
4458                                         DRM_INFO("VRAM is lost due to GPU reset!\n");
4459                                         amdgpu_inc_vram_lost(tmp_adev);
4460                                 }
4461
4462                                 r = amdgpu_gtt_mgr_recover(ttm_manager_type(&tmp_adev->mman.bdev, TTM_PL_TT));
4463                                 if (r)
4464                                         goto out;
4465
4466                                 r = amdgpu_device_fw_loading(tmp_adev);
4467                                 if (r)
4468                                         return r;
4469
4470                                 r = amdgpu_device_ip_resume_phase2(tmp_adev);
4471                                 if (r)
4472                                         goto out;
4473
4474                                 if (vram_lost)
4475                                         amdgpu_device_fill_reset_magic(tmp_adev);
4476
4477                                 /*
4478                                  * Add this ASIC as tracked as reset was already
4479                                  * complete successfully.
4480                                  */
4481                                 amdgpu_register_gpu_instance(tmp_adev);
4482
4483                                 if (!hive && tmp_adev->gmc.xgmi.num_physical_nodes > 1)
4484                                         amdgpu_xgmi_add_device(tmp_adev);
4485
4486                                 r = amdgpu_device_ip_late_init(tmp_adev);
4487                                 if (r)
4488                                         goto out;
4489
4490                                 amdgpu_fbdev_set_suspend(tmp_adev, 0);
4491
4492                                 /*
4493                                  * The GPU enters bad state once faulty pages
4494                                  * by ECC has reached the threshold, and ras
4495                                  * recovery is scheduled next. So add one check
4496                                  * here to break recovery if it indeed exceeds
4497                                  * bad page threshold, and remind user to
4498                                  * retire this GPU or setting one bigger
4499                                  * bad_page_threshold value to fix this once
4500                                  * probing driver again.
4501                                  */
4502                                 if (!amdgpu_ras_eeprom_check_err_threshold(tmp_adev)) {
4503                                         /* must succeed. */
4504                                         amdgpu_ras_resume(tmp_adev);
4505                                 } else {
4506                                         r = -EINVAL;
4507                                         goto out;
4508                                 }
4509
4510                                 /* Update PSP FW topology after reset */
4511                                 if (hive && tmp_adev->gmc.xgmi.num_physical_nodes > 1)
4512                                         r = amdgpu_xgmi_update_topology(hive, tmp_adev);
4513                         }
4514                 }
4515
4516 out:
4517                 if (!r) {
4518                         amdgpu_irq_gpu_reset_resume_helper(tmp_adev);
4519                         r = amdgpu_ib_ring_tests(tmp_adev);
4520                         if (r) {
4521                                 dev_err(tmp_adev->dev, "ib ring test failed (%d).\n", r);
4522                                 r = amdgpu_device_ip_suspend(tmp_adev);
4523                                 need_full_reset = true;
4524                                 r = -EAGAIN;
4525                                 goto end;
4526                         }
4527                 }
4528
4529                 if (!r)
4530                         r = amdgpu_device_recover_vram(tmp_adev);
4531                 else
4532                         tmp_adev->asic_reset_res = r;
4533         }
4534
4535 end:
4536         *need_full_reset_arg = need_full_reset;
4537         return r;
4538 }
4539
4540 static bool amdgpu_device_lock_adev(struct amdgpu_device *adev,
4541                                 struct amdgpu_hive_info *hive)
4542 {
4543         if (atomic_cmpxchg(&adev->in_gpu_reset, 0, 1) != 0)
4544                 return false;
4545
4546         if (hive) {
4547                 down_write_nest_lock(&adev->reset_sem, &hive->hive_lock);
4548         } else {
4549                 down_write(&adev->reset_sem);
4550         }
4551
4552         switch (amdgpu_asic_reset_method(adev)) {
4553         case AMD_RESET_METHOD_MODE1:
4554                 adev->mp1_state = PP_MP1_STATE_SHUTDOWN;
4555                 break;
4556         case AMD_RESET_METHOD_MODE2:
4557                 adev->mp1_state = PP_MP1_STATE_RESET;
4558                 break;
4559         default:
4560                 adev->mp1_state = PP_MP1_STATE_NONE;
4561                 break;
4562         }
4563
4564         return true;
4565 }
4566
4567 static void amdgpu_device_unlock_adev(struct amdgpu_device *adev)
4568 {
4569         amdgpu_vf_error_trans_all(adev);
4570         adev->mp1_state = PP_MP1_STATE_NONE;
4571         atomic_set(&adev->in_gpu_reset, 0);
4572         up_write(&adev->reset_sem);
4573 }
4574
4575 /*
4576  * to lockup a list of amdgpu devices in a hive safely, if not a hive
4577  * with multiple nodes, it will be similar as amdgpu_device_lock_adev.
4578  *
4579  * unlock won't require roll back.
4580  */
4581 static int amdgpu_device_lock_hive_adev(struct amdgpu_device *adev, struct amdgpu_hive_info *hive)
4582 {
4583         struct amdgpu_device *tmp_adev = NULL;
4584
4585         if (adev->gmc.xgmi.num_physical_nodes > 1) {
4586                 if (!hive) {
4587                         dev_err(adev->dev, "Hive is NULL while device has multiple xgmi nodes");
4588                         return -ENODEV;
4589                 }
4590                 list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head) {
4591                         if (!amdgpu_device_lock_adev(tmp_adev, hive))
4592                                 goto roll_back;
4593                 }
4594         } else if (!amdgpu_device_lock_adev(adev, hive))
4595                 return -EAGAIN;
4596
4597         return 0;
4598 roll_back:
4599         if (!list_is_first(&tmp_adev->gmc.xgmi.head, &hive->device_list)) {
4600                 /*
4601                  * if the lockup iteration break in the middle of a hive,
4602                  * it may means there may has a race issue,
4603                  * or a hive device locked up independently.
4604                  * we may be in trouble and may not, so will try to roll back
4605                  * the lock and give out a warnning.
4606                  */
4607                 dev_warn(tmp_adev->dev, "Hive lock iteration broke in the middle. Rolling back to unlock");
4608                 list_for_each_entry_continue_reverse(tmp_adev, &hive->device_list, gmc.xgmi.head) {
4609                         amdgpu_device_unlock_adev(tmp_adev);
4610                 }
4611         }
4612         return -EAGAIN;
4613 }
4614
4615 static void amdgpu_device_resume_display_audio(struct amdgpu_device *adev)
4616 {
4617         struct pci_dev *p = NULL;
4618
4619         p = pci_get_domain_bus_and_slot(pci_domain_nr(adev->pdev->bus),
4620                         adev->pdev->bus->number, 1);
4621         if (p) {
4622                 pm_runtime_enable(&(p->dev));
4623                 pm_runtime_resume(&(p->dev));
4624         }
4625 }
4626
4627 static int amdgpu_device_suspend_display_audio(struct amdgpu_device *adev)
4628 {
4629         enum amd_reset_method reset_method;
4630         struct pci_dev *p = NULL;
4631         u64 expires;
4632
4633         /*
4634          * For now, only BACO and mode1 reset are confirmed
4635          * to suffer the audio issue without proper suspended.
4636          */
4637         reset_method = amdgpu_asic_reset_method(adev);
4638         if ((reset_method != AMD_RESET_METHOD_BACO) &&
4639              (reset_method != AMD_RESET_METHOD_MODE1))
4640                 return -EINVAL;
4641
4642         p = pci_get_domain_bus_and_slot(pci_domain_nr(adev->pdev->bus),
4643                         adev->pdev->bus->number, 1);
4644         if (!p)
4645                 return -ENODEV;
4646
4647         expires = pm_runtime_autosuspend_expiration(&(p->dev));
4648         if (!expires)
4649                 /*
4650                  * If we cannot get the audio device autosuspend delay,
4651                  * a fixed 4S interval will be used. Considering 3S is
4652                  * the audio controller default autosuspend delay setting.
4653                  * 4S used here is guaranteed to cover that.
4654                  */
4655                 expires = ktime_get_mono_fast_ns() + NSEC_PER_SEC * 4ULL;
4656
4657         while (!pm_runtime_status_suspended(&(p->dev))) {
4658                 if (!pm_runtime_suspend(&(p->dev)))
4659                         break;
4660
4661                 if (expires < ktime_get_mono_fast_ns()) {
4662                         dev_warn(adev->dev, "failed to suspend display audio\n");
4663                         /* TODO: abort the succeeding gpu reset? */
4664                         return -ETIMEDOUT;
4665                 }
4666         }
4667
4668         pm_runtime_disable(&(p->dev));
4669
4670         return 0;
4671 }
4672
4673 /**
4674  * amdgpu_device_gpu_recover - reset the asic and recover scheduler
4675  *
4676  * @adev: amdgpu_device pointer
4677  * @job: which job trigger hang
4678  *
4679  * Attempt to reset the GPU if it has hung (all asics).
4680  * Attempt to do soft-reset or full-reset and reinitialize Asic
4681  * Returns 0 for success or an error on failure.
4682  */
4683
4684 int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
4685                               struct amdgpu_job *job)
4686 {
4687         struct list_head device_list, *device_list_handle =  NULL;
4688         bool need_full_reset = false;
4689         bool job_signaled = false;
4690         struct amdgpu_hive_info *hive = NULL;
4691         struct amdgpu_device *tmp_adev = NULL;
4692         int i, r = 0;
4693         bool need_emergency_restart = false;
4694         bool audio_suspended = false;
4695
4696         /*
4697          * Special case: RAS triggered and full reset isn't supported
4698          */
4699         need_emergency_restart = amdgpu_ras_need_emergency_restart(adev);
4700
4701         /*
4702          * Flush RAM to disk so that after reboot
4703          * the user can read log and see why the system rebooted.
4704          */
4705         if (need_emergency_restart && amdgpu_ras_get_context(adev)->reboot) {
4706                 DRM_WARN("Emergency reboot.");
4707
4708                 ksys_sync_helper();
4709                 emergency_restart();
4710         }
4711
4712         dev_info(adev->dev, "GPU %s begin!\n",
4713                 need_emergency_restart ? "jobs stop":"reset");
4714
4715         /*
4716          * Here we trylock to avoid chain of resets executing from
4717          * either trigger by jobs on different adevs in XGMI hive or jobs on
4718          * different schedulers for same device while this TO handler is running.
4719          * We always reset all schedulers for device and all devices for XGMI
4720          * hive so that should take care of them too.
4721          */
4722         hive = amdgpu_get_xgmi_hive(adev);
4723         if (hive) {
4724                 if (atomic_cmpxchg(&hive->in_reset, 0, 1) != 0) {
4725                         DRM_INFO("Bailing on TDR for s_job:%llx, hive: %llx as another already in progress",
4726                                 job ? job->base.id : -1, hive->hive_id);
4727                         amdgpu_put_xgmi_hive(hive);
4728                         if (job)
4729                                 drm_sched_increase_karma(&job->base);
4730                         return 0;
4731                 }
4732                 mutex_lock(&hive->hive_lock);
4733         }
4734
4735         /*
4736          * lock the device before we try to operate the linked list
4737          * if didn't get the device lock, don't touch the linked list since
4738          * others may iterating it.
4739          */
4740         r = amdgpu_device_lock_hive_adev(adev, hive);
4741         if (r) {
4742                 dev_info(adev->dev, "Bailing on TDR for s_job:%llx, as another already in progress",
4743                                         job ? job->base.id : -1);
4744
4745                 /* even we skipped this reset, still need to set the job to guilty */
4746                 if (job)
4747                         drm_sched_increase_karma(&job->base);
4748                 goto skip_recovery;
4749         }
4750
4751         /*
4752          * Build list of devices to reset.
4753          * In case we are in XGMI hive mode, resort the device list
4754          * to put adev in the 1st position.
4755          */
4756         INIT_LIST_HEAD(&device_list);
4757         if (adev->gmc.xgmi.num_physical_nodes > 1) {
4758                 list_for_each_entry(tmp_adev, &hive->device_list, gmc.xgmi.head)
4759                         list_add_tail(&tmp_adev->reset_list, &device_list);
4760                 if (!list_is_first(&adev->reset_list, &device_list))
4761                         list_rotate_to_front(&adev->reset_list, &device_list);
4762                 device_list_handle = &device_list;
4763         } else {
4764                 list_add_tail(&adev->reset_list, &device_list);
4765                 device_list_handle = &device_list;
4766         }
4767
4768         /* block all schedulers and reset given job's ring */
4769         list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
4770                 /*
4771                  * Try to put the audio codec into suspend state
4772                  * before gpu reset started.
4773                  *
4774                  * Due to the power domain of the graphics device
4775                  * is shared with AZ power domain. Without this,
4776                  * we may change the audio hardware from behind
4777                  * the audio driver's back. That will trigger
4778                  * some audio codec errors.
4779                  */
4780                 if (!amdgpu_device_suspend_display_audio(tmp_adev))
4781                         audio_suspended = true;
4782
4783                 amdgpu_ras_set_error_query_ready(tmp_adev, false);
4784
4785                 cancel_delayed_work_sync(&tmp_adev->delayed_init_work);
4786
4787                 if (!amdgpu_sriov_vf(tmp_adev))
4788                         amdgpu_amdkfd_pre_reset(tmp_adev);
4789
4790                 /*
4791                  * Mark these ASICs to be reseted as untracked first
4792                  * And add them back after reset completed
4793                  */
4794                 amdgpu_unregister_gpu_instance(tmp_adev);
4795
4796                 amdgpu_fbdev_set_suspend(tmp_adev, 1);
4797
4798                 /* disable ras on ALL IPs */
4799                 if (!need_emergency_restart &&
4800                       amdgpu_device_ip_need_full_reset(tmp_adev))
4801                         amdgpu_ras_suspend(tmp_adev);
4802
4803                 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
4804                         struct amdgpu_ring *ring = tmp_adev->rings[i];
4805
4806                         if (!ring || !ring->sched.thread)
4807                                 continue;
4808
4809                         drm_sched_stop(&ring->sched, job ? &job->base : NULL);
4810
4811                         if (need_emergency_restart)
4812                                 amdgpu_job_stop_all_jobs_on_sched(&ring->sched);
4813                 }
4814                 atomic_inc(&tmp_adev->gpu_reset_counter);
4815         }
4816
4817         if (need_emergency_restart)
4818                 goto skip_sched_resume;
4819
4820         /*
4821          * Must check guilty signal here since after this point all old
4822          * HW fences are force signaled.
4823          *
4824          * job->base holds a reference to parent fence
4825          */
4826         if (job && job->base.s_fence->parent &&
4827             dma_fence_is_signaled(job->base.s_fence->parent)) {
4828                 job_signaled = true;
4829                 dev_info(adev->dev, "Guilty job already signaled, skipping HW reset");
4830                 goto skip_hw_reset;
4831         }
4832
4833 retry:  /* Rest of adevs pre asic reset from XGMI hive. */
4834         list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
4835                 r = amdgpu_device_pre_asic_reset(tmp_adev,
4836                                                  (tmp_adev == adev) ? job : NULL,
4837                                                  &need_full_reset);
4838                 /*TODO Should we stop ?*/
4839                 if (r) {
4840                         dev_err(tmp_adev->dev, "GPU pre asic reset failed with err, %d for drm dev, %s ",
4841                                   r, adev_to_drm(tmp_adev)->unique);
4842                         tmp_adev->asic_reset_res = r;
4843                 }
4844         }
4845
4846         /* Actual ASIC resets if needed.*/
4847         /* TODO Implement XGMI hive reset logic for SRIOV */
4848         if (amdgpu_sriov_vf(adev)) {
4849                 r = amdgpu_device_reset_sriov(adev, job ? false : true);
4850                 if (r)
4851                         adev->asic_reset_res = r;
4852         } else {
4853                 r  = amdgpu_do_asic_reset(hive, device_list_handle, &need_full_reset, false);
4854                 if (r && r == -EAGAIN)
4855                         goto retry;
4856         }
4857
4858 skip_hw_reset:
4859
4860         /* Post ASIC reset for all devs .*/
4861         list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
4862
4863                 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
4864                         struct amdgpu_ring *ring = tmp_adev->rings[i];
4865
4866                         if (!ring || !ring->sched.thread)
4867                                 continue;
4868
4869                         /* No point to resubmit jobs if we didn't HW reset*/
4870                         if (!tmp_adev->asic_reset_res && !job_signaled)
4871                                 drm_sched_resubmit_jobs(&ring->sched);
4872
4873                         drm_sched_start(&ring->sched, !tmp_adev->asic_reset_res);
4874                 }
4875
4876                 if (!amdgpu_device_has_dc_support(tmp_adev) && !job_signaled) {
4877                         drm_helper_resume_force_mode(adev_to_drm(tmp_adev));
4878                 }
4879
4880                 tmp_adev->asic_reset_res = 0;
4881
4882                 if (r) {
4883                         /* bad news, how to tell it to userspace ? */
4884                         dev_info(tmp_adev->dev, "GPU reset(%d) failed\n", atomic_read(&tmp_adev->gpu_reset_counter));
4885                         amdgpu_vf_error_put(tmp_adev, AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, r);
4886                 } else {
4887                         dev_info(tmp_adev->dev, "GPU reset(%d) succeeded!\n", atomic_read(&tmp_adev->gpu_reset_counter));
4888                 }
4889         }
4890
4891 skip_sched_resume:
4892         list_for_each_entry(tmp_adev, device_list_handle, reset_list) {
4893                 /* unlock kfd: SRIOV would do it separately */
4894                 if (!need_emergency_restart && !amdgpu_sriov_vf(tmp_adev))
4895                         amdgpu_amdkfd_post_reset(tmp_adev);
4896
4897                 /* kfd_post_reset will do nothing if kfd device is not initialized,
4898                  * need to bring up kfd here if it's not be initialized before
4899                  */
4900                 if (!adev->kfd.init_complete)
4901                         amdgpu_amdkfd_device_init(adev);
4902
4903                 if (audio_suspended)
4904                         amdgpu_device_resume_display_audio(tmp_adev);
4905                 amdgpu_device_unlock_adev(tmp_adev);
4906         }
4907
4908 skip_recovery:
4909         if (hive) {
4910                 atomic_set(&hive->in_reset, 0);
4911                 mutex_unlock(&hive->hive_lock);
4912                 amdgpu_put_xgmi_hive(hive);
4913         }
4914
4915         if (r && r != -EAGAIN)
4916                 dev_info(adev->dev, "GPU reset end with ret = %d\n", r);
4917         return r;
4918 }
4919
4920 /**
4921  * amdgpu_device_get_pcie_info - fence pcie info about the PCIE slot
4922  *
4923  * @adev: amdgpu_device pointer
4924  *
4925  * Fetchs and stores in the driver the PCIE capabilities (gen speed
4926  * and lanes) of the slot the device is in. Handles APUs and
4927  * virtualized environments where PCIE config space may not be available.
4928  */
4929 static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev)
4930 {
4931         struct pci_dev *pdev;
4932         enum pci_bus_speed speed_cap, platform_speed_cap;
4933         enum pcie_link_width platform_link_width;
4934
4935         if (amdgpu_pcie_gen_cap)
4936                 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
4937
4938         if (amdgpu_pcie_lane_cap)
4939                 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
4940
4941         /* covers APUs as well */
4942         if (pci_is_root_bus(adev->pdev->bus)) {
4943                 if (adev->pm.pcie_gen_mask == 0)
4944                         adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
4945                 if (adev->pm.pcie_mlw_mask == 0)
4946                         adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
4947                 return;
4948         }
4949
4950         if (adev->pm.pcie_gen_mask && adev->pm.pcie_mlw_mask)
4951                 return;
4952
4953         pcie_bandwidth_available(adev->pdev, NULL,
4954                                  &platform_speed_cap, &platform_link_width);
4955
4956         if (adev->pm.pcie_gen_mask == 0) {
4957                 /* asic caps */
4958                 pdev = adev->pdev;
4959                 speed_cap = pcie_get_speed_cap(pdev);
4960                 if (speed_cap == PCI_SPEED_UNKNOWN) {
4961                         adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
4962                                                   CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
4963                                                   CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
4964                 } else {
4965                         if (speed_cap == PCIE_SPEED_32_0GT)
4966                                 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
4967                                                           CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
4968                                                           CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3 |
4969                                                           CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN4 |
4970                                                           CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN5);
4971                         else if (speed_cap == PCIE_SPEED_16_0GT)
4972                                 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
4973                                                           CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
4974                                                           CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3 |
4975                                                           CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN4);
4976                         else if (speed_cap == PCIE_SPEED_8_0GT)
4977                                 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
4978                                                           CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
4979                                                           CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
4980                         else if (speed_cap == PCIE_SPEED_5_0GT)
4981                                 adev->pm.pcie_gen_mask |= (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
4982                                                           CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2);
4983                         else
4984                                 adev->pm.pcie_gen_mask |= CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1;
4985                 }
4986                 /* platform caps */
4987                 if (platform_speed_cap == PCI_SPEED_UNKNOWN) {
4988                         adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
4989                                                    CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2);
4990                 } else {
4991                         if (platform_speed_cap == PCIE_SPEED_32_0GT)
4992                                 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
4993                                                            CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
4994                                                            CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3 |
4995                                                            CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4 |
4996                                                            CAIL_PCIE_LINK_SPEED_SUPPORT_GEN5);
4997                         else if (platform_speed_cap == PCIE_SPEED_16_0GT)
4998                                 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
4999                                                            CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5000                                                            CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3 |
5001                                                            CAIL_PCIE_LINK_SPEED_SUPPORT_GEN4);
5002                         else if (platform_speed_cap == PCIE_SPEED_8_0GT)
5003                                 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5004                                                            CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2 |
5005                                                            CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3);
5006                         else if (platform_speed_cap == PCIE_SPEED_5_0GT)
5007                                 adev->pm.pcie_gen_mask |= (CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1 |
5008                                                            CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2);
5009                         else
5010                                 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
5011
5012                 }
5013         }
5014         if (adev->pm.pcie_mlw_mask == 0) {
5015                 if (platform_link_width == PCIE_LNK_WIDTH_UNKNOWN) {
5016                         adev->pm.pcie_mlw_mask |= AMDGPU_DEFAULT_PCIE_MLW_MASK;
5017                 } else {
5018                         switch (platform_link_width) {
5019                         case PCIE_LNK_X32:
5020                                 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
5021                                                           CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
5022                                                           CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
5023                                                           CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
5024                                                           CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5025                                                           CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5026                                                           CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5027                                 break;
5028                         case PCIE_LNK_X16:
5029                                 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
5030                                                           CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
5031                                                           CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
5032                                                           CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5033                                                           CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5034                                                           CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5035                                 break;
5036                         case PCIE_LNK_X12:
5037                                 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
5038                                                           CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
5039                                                           CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5040                                                           CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5041                                                           CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5042                                 break;
5043                         case PCIE_LNK_X8:
5044                                 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
5045                                                           CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5046                                                           CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5047                                                           CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5048                                 break;
5049                         case PCIE_LNK_X4:
5050                                 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
5051                                                           CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5052                                                           CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5053                                 break;
5054                         case PCIE_LNK_X2:
5055                                 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
5056                                                           CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
5057                                 break;
5058                         case PCIE_LNK_X1:
5059                                 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
5060                                 break;
5061                         default:
5062                                 break;
5063                         }
5064                 }
5065         }
5066 }
5067
5068 int amdgpu_device_baco_enter(struct drm_device *dev)
5069 {
5070         struct amdgpu_device *adev = drm_to_adev(dev);
5071         struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
5072
5073         if (!amdgpu_device_supports_baco(adev_to_drm(adev)))
5074                 return -ENOTSUPP;
5075
5076         if (ras && ras->supported && adev->nbio.funcs->enable_doorbell_interrupt)
5077                 adev->nbio.funcs->enable_doorbell_interrupt(adev, false);
5078
5079         return amdgpu_dpm_baco_enter(adev);
5080 }
5081
5082 int amdgpu_device_baco_exit(struct drm_device *dev)
5083 {
5084         struct amdgpu_device *adev = drm_to_adev(dev);
5085         struct amdgpu_ras *ras = amdgpu_ras_get_context(adev);
5086         int ret = 0;
5087
5088         if (!amdgpu_device_supports_baco(adev_to_drm(adev)))
5089                 return -ENOTSUPP;
5090
5091         ret = amdgpu_dpm_baco_exit(adev);
5092         if (ret)
5093                 return ret;
5094
5095         if (ras && ras->supported && adev->nbio.funcs->enable_doorbell_interrupt)
5096                 adev->nbio.funcs->enable_doorbell_interrupt(adev, true);
5097
5098         return 0;
5099 }
5100
5101 static void amdgpu_cancel_all_tdr(struct amdgpu_device *adev)
5102 {
5103         int i;
5104
5105         for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
5106                 struct amdgpu_ring *ring = adev->rings[i];
5107
5108                 if (!ring || !ring->sched.thread)
5109                         continue;
5110
5111                 cancel_delayed_work_sync(&ring->sched.work_tdr);
5112         }
5113 }
5114
5115 /**
5116  * amdgpu_pci_error_detected - Called when a PCI error is detected.
5117  * @pdev: PCI device struct
5118  * @state: PCI channel state
5119  *
5120  * Description: Called when a PCI error is detected.
5121  *
5122  * Return: PCI_ERS_RESULT_NEED_RESET or PCI_ERS_RESULT_DISCONNECT.
5123  */
5124 pci_ers_result_t amdgpu_pci_error_detected(struct pci_dev *pdev, pci_channel_state_t state)
5125 {
5126         struct drm_device *dev = pci_get_drvdata(pdev);
5127         struct amdgpu_device *adev = drm_to_adev(dev);
5128         int i;
5129
5130         DRM_INFO("PCI error: detected callback, state(%d)!!\n", state);
5131
5132         if (adev->gmc.xgmi.num_physical_nodes > 1) {
5133                 DRM_WARN("No support for XGMI hive yet...");
5134                 return PCI_ERS_RESULT_DISCONNECT;
5135         }
5136
5137         switch (state) {
5138         case pci_channel_io_normal:
5139                 return PCI_ERS_RESULT_CAN_RECOVER;
5140         /* Fatal error, prepare for slot reset */
5141         case pci_channel_io_frozen:
5142                 /*
5143                  * Cancel and wait for all TDRs in progress if failing to
5144                  * set  adev->in_gpu_reset in amdgpu_device_lock_adev
5145                  *
5146                  * Locking adev->reset_sem will prevent any external access
5147                  * to GPU during PCI error recovery
5148                  */
5149                 while (!amdgpu_device_lock_adev(adev, NULL))
5150                         amdgpu_cancel_all_tdr(adev);
5151
5152                 /*
5153                  * Block any work scheduling as we do for regular GPU reset
5154                  * for the duration of the recovery
5155                  */
5156                 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
5157                         struct amdgpu_ring *ring = adev->rings[i];
5158
5159                         if (!ring || !ring->sched.thread)
5160                                 continue;
5161
5162                         drm_sched_stop(&ring->sched, NULL);
5163                 }
5164                 atomic_inc(&adev->gpu_reset_counter);
5165                 return PCI_ERS_RESULT_NEED_RESET;
5166         case pci_channel_io_perm_failure:
5167                 /* Permanent error, prepare for device removal */
5168                 return PCI_ERS_RESULT_DISCONNECT;
5169         }
5170
5171         return PCI_ERS_RESULT_NEED_RESET;
5172 }
5173
5174 /**
5175  * amdgpu_pci_mmio_enabled - Enable MMIO and dump debug registers
5176  * @pdev: pointer to PCI device
5177  */
5178 pci_ers_result_t amdgpu_pci_mmio_enabled(struct pci_dev *pdev)
5179 {
5180
5181         DRM_INFO("PCI error: mmio enabled callback!!\n");
5182
5183         /* TODO - dump whatever for debugging purposes */
5184
5185         /* This called only if amdgpu_pci_error_detected returns
5186          * PCI_ERS_RESULT_CAN_RECOVER. Read/write to the device still
5187          * works, no need to reset slot.
5188          */
5189
5190         return PCI_ERS_RESULT_RECOVERED;
5191 }
5192
5193 /**
5194  * amdgpu_pci_slot_reset - Called when PCI slot has been reset.
5195  * @pdev: PCI device struct
5196  *
5197  * Description: This routine is called by the pci error recovery
5198  * code after the PCI slot has been reset, just before we
5199  * should resume normal operations.
5200  */
5201 pci_ers_result_t amdgpu_pci_slot_reset(struct pci_dev *pdev)
5202 {
5203         struct drm_device *dev = pci_get_drvdata(pdev);
5204         struct amdgpu_device *adev = drm_to_adev(dev);
5205         int r, i;
5206         bool need_full_reset = true;
5207         u32 memsize;
5208         struct list_head device_list;
5209
5210         DRM_INFO("PCI error: slot reset callback!!\n");
5211
5212         INIT_LIST_HEAD(&device_list);
5213         list_add_tail(&adev->reset_list, &device_list);
5214
5215         /* wait for asic to come out of reset */
5216         msleep(500);
5217
5218         /* Restore PCI confspace */
5219         amdgpu_device_load_pci_state(pdev);
5220
5221         /* confirm  ASIC came out of reset */
5222         for (i = 0; i < adev->usec_timeout; i++) {
5223                 memsize = amdgpu_asic_get_config_memsize(adev);
5224
5225                 if (memsize != 0xffffffff)
5226                         break;
5227                 udelay(1);
5228         }
5229         if (memsize == 0xffffffff) {
5230                 r = -ETIME;
5231                 goto out;
5232         }
5233
5234         adev->in_pci_err_recovery = true;
5235         r = amdgpu_device_pre_asic_reset(adev, NULL, &need_full_reset);
5236         adev->in_pci_err_recovery = false;
5237         if (r)
5238                 goto out;
5239
5240         r = amdgpu_do_asic_reset(NULL, &device_list, &need_full_reset, true);
5241
5242 out:
5243         if (!r) {
5244                 if (amdgpu_device_cache_pci_state(adev->pdev))
5245                         pci_restore_state(adev->pdev);
5246
5247                 DRM_INFO("PCIe error recovery succeeded\n");
5248         } else {
5249                 DRM_ERROR("PCIe error recovery failed, err:%d", r);
5250                 amdgpu_device_unlock_adev(adev);
5251         }
5252
5253         return r ? PCI_ERS_RESULT_DISCONNECT : PCI_ERS_RESULT_RECOVERED;
5254 }
5255
5256 /**
5257  * amdgpu_pci_resume() - resume normal ops after PCI reset
5258  * @pdev: pointer to PCI device
5259  *
5260  * Called when the error recovery driver tells us that its
5261  * OK to resume normal operation.
5262  */
5263 void amdgpu_pci_resume(struct pci_dev *pdev)
5264 {
5265         struct drm_device *dev = pci_get_drvdata(pdev);
5266         struct amdgpu_device *adev = drm_to_adev(dev);
5267         int i;
5268
5269
5270         DRM_INFO("PCI error: resume callback!!\n");
5271
5272         for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
5273                 struct amdgpu_ring *ring = adev->rings[i];
5274
5275                 if (!ring || !ring->sched.thread)
5276                         continue;
5277
5278
5279                 drm_sched_resubmit_jobs(&ring->sched);
5280                 drm_sched_start(&ring->sched, true);
5281         }
5282
5283         amdgpu_device_unlock_adev(adev);
5284 }
5285
5286 bool amdgpu_device_cache_pci_state(struct pci_dev *pdev)
5287 {
5288         struct drm_device *dev = pci_get_drvdata(pdev);
5289         struct amdgpu_device *adev = drm_to_adev(dev);
5290         int r;
5291
5292         r = pci_save_state(pdev);
5293         if (!r) {
5294                 kfree(adev->pci_state);
5295
5296                 adev->pci_state = pci_store_saved_state(pdev);
5297
5298                 if (!adev->pci_state) {
5299                         DRM_ERROR("Failed to store PCI saved state");
5300                         return false;
5301                 }
5302         } else {
5303                 DRM_WARN("Failed to save PCI state, err:%d\n", r);
5304                 return false;
5305         }
5306
5307         return true;
5308 }
5309
5310 bool amdgpu_device_load_pci_state(struct pci_dev *pdev)
5311 {
5312         struct drm_device *dev = pci_get_drvdata(pdev);
5313         struct amdgpu_device *adev = drm_to_adev(dev);
5314         int r;
5315
5316         if (!adev->pci_state)
5317                 return false;
5318
5319         r = pci_load_saved_state(pdev, adev->pci_state);
5320
5321         if (!r) {
5322                 pci_restore_state(pdev);
5323         } else {
5324                 DRM_WARN("Failed to load PCI state, err:%d\n", r);
5325                 return false;
5326         }
5327
5328         return true;
5329 }
5330
5331