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