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