]> Git Repo - linux.git/blob - drivers/gpu/drm/amd/amdgpu/amdgpu_device.c
drm/amdgpu:cleanup flag not used
[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/kthread.h>
29 #include <linux/console.h>
30 #include <linux/slab.h>
31 #include <linux/debugfs.h>
32 #include <drm/drmP.h>
33 #include <drm/drm_crtc_helper.h>
34 #include <drm/amdgpu_drm.h>
35 #include <linux/vgaarb.h>
36 #include <linux/vga_switcheroo.h>
37 #include <linux/efi.h>
38 #include "amdgpu.h"
39 #include "amdgpu_trace.h"
40 #include "amdgpu_i2c.h"
41 #include "atom.h"
42 #include "amdgpu_atombios.h"
43 #include "amdgpu_atomfirmware.h"
44 #include "amd_pcie.h"
45 #ifdef CONFIG_DRM_AMDGPU_SI
46 #include "si.h"
47 #endif
48 #ifdef CONFIG_DRM_AMDGPU_CIK
49 #include "cik.h"
50 #endif
51 #include "vi.h"
52 #include "soc15.h"
53 #include "bif/bif_4_1_d.h"
54 #include <linux/pci.h>
55 #include <linux/firmware.h>
56
57 MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin");
58
59 static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev);
60 static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev);
61
62 static const char *amdgpu_asic_name[] = {
63         "TAHITI",
64         "PITCAIRN",
65         "VERDE",
66         "OLAND",
67         "HAINAN",
68         "BONAIRE",
69         "KAVERI",
70         "KABINI",
71         "HAWAII",
72         "MULLINS",
73         "TOPAZ",
74         "TONGA",
75         "FIJI",
76         "CARRIZO",
77         "STONEY",
78         "POLARIS10",
79         "POLARIS11",
80         "POLARIS12",
81         "VEGA10",
82         "LAST",
83 };
84
85 bool amdgpu_device_is_px(struct drm_device *dev)
86 {
87         struct amdgpu_device *adev = dev->dev_private;
88
89         if (adev->flags & AMD_IS_PX)
90                 return true;
91         return false;
92 }
93
94 /*
95  * MMIO register access helper functions.
96  */
97 uint32_t amdgpu_mm_rreg(struct amdgpu_device *adev, uint32_t reg,
98                         uint32_t acc_flags)
99 {
100         uint32_t ret;
101
102         if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev)) {
103                 BUG_ON(in_interrupt());
104                 return amdgpu_virt_kiq_rreg(adev, reg);
105         }
106
107         if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
108                 ret = readl(((void __iomem *)adev->rmmio) + (reg * 4));
109         else {
110                 unsigned long flags;
111
112                 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
113                 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
114                 ret = readl(((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
115                 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
116         }
117         trace_amdgpu_mm_rreg(adev->pdev->device, reg, ret);
118         return ret;
119 }
120
121 void amdgpu_mm_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v,
122                     uint32_t acc_flags)
123 {
124         trace_amdgpu_mm_wreg(adev->pdev->device, reg, v);
125
126         if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev)) {
127                 BUG_ON(in_interrupt());
128                 return amdgpu_virt_kiq_wreg(adev, reg, v);
129         }
130
131         if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
132                 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
133         else {
134                 unsigned long flags;
135
136                 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
137                 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
138                 writel(v, ((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
139                 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
140         }
141 }
142
143 u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 reg)
144 {
145         if ((reg * 4) < adev->rio_mem_size)
146                 return ioread32(adev->rio_mem + (reg * 4));
147         else {
148                 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
149                 return ioread32(adev->rio_mem + (mmMM_DATA * 4));
150         }
151 }
152
153 void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
154 {
155
156         if ((reg * 4) < adev->rio_mem_size)
157                 iowrite32(v, adev->rio_mem + (reg * 4));
158         else {
159                 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
160                 iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
161         }
162 }
163
164 /**
165  * amdgpu_mm_rdoorbell - read a doorbell dword
166  *
167  * @adev: amdgpu_device pointer
168  * @index: doorbell index
169  *
170  * Returns the value in the doorbell aperture at the
171  * requested doorbell index (CIK).
172  */
173 u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
174 {
175         if (index < adev->doorbell.num_doorbells) {
176                 return readl(adev->doorbell.ptr + index);
177         } else {
178                 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
179                 return 0;
180         }
181 }
182
183 /**
184  * amdgpu_mm_wdoorbell - write a doorbell dword
185  *
186  * @adev: amdgpu_device pointer
187  * @index: doorbell index
188  * @v: value to write
189  *
190  * Writes @v to the doorbell aperture at the
191  * requested doorbell index (CIK).
192  */
193 void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
194 {
195         if (index < adev->doorbell.num_doorbells) {
196                 writel(v, adev->doorbell.ptr + index);
197         } else {
198                 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
199         }
200 }
201
202 /**
203  * amdgpu_mm_rdoorbell64 - read a doorbell Qword
204  *
205  * @adev: amdgpu_device pointer
206  * @index: doorbell index
207  *
208  * Returns the value in the doorbell aperture at the
209  * requested doorbell index (VEGA10+).
210  */
211 u64 amdgpu_mm_rdoorbell64(struct amdgpu_device *adev, u32 index)
212 {
213         if (index < adev->doorbell.num_doorbells) {
214                 return atomic64_read((atomic64_t *)(adev->doorbell.ptr + index));
215         } else {
216                 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
217                 return 0;
218         }
219 }
220
221 /**
222  * amdgpu_mm_wdoorbell64 - write a doorbell Qword
223  *
224  * @adev: amdgpu_device pointer
225  * @index: doorbell index
226  * @v: value to write
227  *
228  * Writes @v to the doorbell aperture at the
229  * requested doorbell index (VEGA10+).
230  */
231 void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v)
232 {
233         if (index < adev->doorbell.num_doorbells) {
234                 atomic64_set((atomic64_t *)(adev->doorbell.ptr + index), v);
235         } else {
236                 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
237         }
238 }
239
240 /**
241  * amdgpu_invalid_rreg - dummy reg read function
242  *
243  * @adev: amdgpu device pointer
244  * @reg: offset of register
245  *
246  * Dummy register read function.  Used for register blocks
247  * that certain asics don't have (all asics).
248  * Returns the value in the register.
249  */
250 static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
251 {
252         DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
253         BUG();
254         return 0;
255 }
256
257 /**
258  * amdgpu_invalid_wreg - dummy reg write function
259  *
260  * @adev: amdgpu device pointer
261  * @reg: offset of register
262  * @v: value to write to the register
263  *
264  * Dummy register read function.  Used for register blocks
265  * that certain asics don't have (all asics).
266  */
267 static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
268 {
269         DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
270                   reg, v);
271         BUG();
272 }
273
274 /**
275  * amdgpu_block_invalid_rreg - dummy reg read function
276  *
277  * @adev: amdgpu device pointer
278  * @block: offset of instance
279  * @reg: offset of register
280  *
281  * Dummy register read function.  Used for register blocks
282  * that certain asics don't have (all asics).
283  * Returns the value in the register.
284  */
285 static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
286                                           uint32_t block, uint32_t reg)
287 {
288         DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
289                   reg, block);
290         BUG();
291         return 0;
292 }
293
294 /**
295  * amdgpu_block_invalid_wreg - dummy reg write function
296  *
297  * @adev: amdgpu device pointer
298  * @block: offset of instance
299  * @reg: offset of register
300  * @v: value to write to the register
301  *
302  * Dummy register read function.  Used for register blocks
303  * that certain asics don't have (all asics).
304  */
305 static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
306                                       uint32_t block,
307                                       uint32_t reg, uint32_t v)
308 {
309         DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
310                   reg, block, v);
311         BUG();
312 }
313
314 static int amdgpu_vram_scratch_init(struct amdgpu_device *adev)
315 {
316         int r;
317
318         if (adev->vram_scratch.robj == NULL) {
319                 r = amdgpu_bo_create(adev, AMDGPU_GPU_PAGE_SIZE,
320                                      PAGE_SIZE, true, AMDGPU_GEM_DOMAIN_VRAM,
321                                      AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
322                                      AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS,
323                                      NULL, NULL, &adev->vram_scratch.robj);
324                 if (r) {
325                         return r;
326                 }
327         }
328
329         r = amdgpu_bo_reserve(adev->vram_scratch.robj, false);
330         if (unlikely(r != 0))
331                 return r;
332         r = amdgpu_bo_pin(adev->vram_scratch.robj,
333                           AMDGPU_GEM_DOMAIN_VRAM, &adev->vram_scratch.gpu_addr);
334         if (r) {
335                 amdgpu_bo_unreserve(adev->vram_scratch.robj);
336                 return r;
337         }
338         r = amdgpu_bo_kmap(adev->vram_scratch.robj,
339                                 (void **)&adev->vram_scratch.ptr);
340         if (r)
341                 amdgpu_bo_unpin(adev->vram_scratch.robj);
342         amdgpu_bo_unreserve(adev->vram_scratch.robj);
343
344         return r;
345 }
346
347 static void amdgpu_vram_scratch_fini(struct amdgpu_device *adev)
348 {
349         int r;
350
351         if (adev->vram_scratch.robj == NULL) {
352                 return;
353         }
354         r = amdgpu_bo_reserve(adev->vram_scratch.robj, true);
355         if (likely(r == 0)) {
356                 amdgpu_bo_kunmap(adev->vram_scratch.robj);
357                 amdgpu_bo_unpin(adev->vram_scratch.robj);
358                 amdgpu_bo_unreserve(adev->vram_scratch.robj);
359         }
360         amdgpu_bo_unref(&adev->vram_scratch.robj);
361 }
362
363 /**
364  * amdgpu_program_register_sequence - program an array of registers.
365  *
366  * @adev: amdgpu_device pointer
367  * @registers: pointer to the register array
368  * @array_size: size of the register array
369  *
370  * Programs an array or registers with and and or masks.
371  * This is a helper for setting golden registers.
372  */
373 void amdgpu_program_register_sequence(struct amdgpu_device *adev,
374                                       const u32 *registers,
375                                       const u32 array_size)
376 {
377         u32 tmp, reg, and_mask, or_mask;
378         int i;
379
380         if (array_size % 3)
381                 return;
382
383         for (i = 0; i < array_size; i +=3) {
384                 reg = registers[i + 0];
385                 and_mask = registers[i + 1];
386                 or_mask = registers[i + 2];
387
388                 if (and_mask == 0xffffffff) {
389                         tmp = or_mask;
390                 } else {
391                         tmp = RREG32(reg);
392                         tmp &= ~and_mask;
393                         tmp |= or_mask;
394                 }
395                 WREG32(reg, tmp);
396         }
397 }
398
399 void amdgpu_pci_config_reset(struct amdgpu_device *adev)
400 {
401         pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
402 }
403
404 /*
405  * GPU doorbell aperture helpers function.
406  */
407 /**
408  * amdgpu_doorbell_init - Init doorbell driver information.
409  *
410  * @adev: amdgpu_device pointer
411  *
412  * Init doorbell driver information (CIK)
413  * Returns 0 on success, error on failure.
414  */
415 static int amdgpu_doorbell_init(struct amdgpu_device *adev)
416 {
417         /* doorbell bar mapping */
418         adev->doorbell.base = pci_resource_start(adev->pdev, 2);
419         adev->doorbell.size = pci_resource_len(adev->pdev, 2);
420
421         adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
422                                              AMDGPU_DOORBELL_MAX_ASSIGNMENT+1);
423         if (adev->doorbell.num_doorbells == 0)
424                 return -EINVAL;
425
426         adev->doorbell.ptr = ioremap(adev->doorbell.base,
427                                      adev->doorbell.num_doorbells *
428                                      sizeof(u32));
429         if (adev->doorbell.ptr == NULL)
430                 return -ENOMEM;
431
432         return 0;
433 }
434
435 /**
436  * amdgpu_doorbell_fini - Tear down doorbell driver information.
437  *
438  * @adev: amdgpu_device pointer
439  *
440  * Tear down doorbell driver information (CIK)
441  */
442 static void amdgpu_doorbell_fini(struct amdgpu_device *adev)
443 {
444         iounmap(adev->doorbell.ptr);
445         adev->doorbell.ptr = NULL;
446 }
447
448 /**
449  * amdgpu_doorbell_get_kfd_info - Report doorbell configuration required to
450  *                                setup amdkfd
451  *
452  * @adev: amdgpu_device pointer
453  * @aperture_base: output returning doorbell aperture base physical address
454  * @aperture_size: output returning doorbell aperture size in bytes
455  * @start_offset: output returning # of doorbell bytes reserved for amdgpu.
456  *
457  * amdgpu and amdkfd share the doorbell aperture. amdgpu sets it up,
458  * takes doorbells required for its own rings and reports the setup to amdkfd.
459  * amdgpu reserved doorbells are at the start of the doorbell aperture.
460  */
461 void amdgpu_doorbell_get_kfd_info(struct amdgpu_device *adev,
462                                 phys_addr_t *aperture_base,
463                                 size_t *aperture_size,
464                                 size_t *start_offset)
465 {
466         /*
467          * The first num_doorbells are used by amdgpu.
468          * amdkfd takes whatever's left in the aperture.
469          */
470         if (adev->doorbell.size > adev->doorbell.num_doorbells * sizeof(u32)) {
471                 *aperture_base = adev->doorbell.base;
472                 *aperture_size = adev->doorbell.size;
473                 *start_offset = adev->doorbell.num_doorbells * sizeof(u32);
474         } else {
475                 *aperture_base = 0;
476                 *aperture_size = 0;
477                 *start_offset = 0;
478         }
479 }
480
481 /*
482  * amdgpu_wb_*()
483  * Writeback is the the method by which the the GPU updates special pages
484  * in memory with the status of certain GPU events (fences, ring pointers,
485  * etc.).
486  */
487
488 /**
489  * amdgpu_wb_fini - Disable Writeback and free memory
490  *
491  * @adev: amdgpu_device pointer
492  *
493  * Disables Writeback and frees the Writeback memory (all asics).
494  * Used at driver shutdown.
495  */
496 static void amdgpu_wb_fini(struct amdgpu_device *adev)
497 {
498         if (adev->wb.wb_obj) {
499                 amdgpu_bo_free_kernel(&adev->wb.wb_obj,
500                                       &adev->wb.gpu_addr,
501                                       (void **)&adev->wb.wb);
502                 adev->wb.wb_obj = NULL;
503         }
504 }
505
506 /**
507  * amdgpu_wb_init- Init Writeback driver info and allocate memory
508  *
509  * @adev: amdgpu_device pointer
510  *
511  * Disables Writeback and frees the Writeback memory (all asics).
512  * Used at driver startup.
513  * Returns 0 on success or an -error on failure.
514  */
515 static int amdgpu_wb_init(struct amdgpu_device *adev)
516 {
517         int r;
518
519         if (adev->wb.wb_obj == NULL) {
520                 r = amdgpu_bo_create_kernel(adev, AMDGPU_MAX_WB * sizeof(uint32_t),
521                                             PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
522                                             &adev->wb.wb_obj, &adev->wb.gpu_addr,
523                                             (void **)&adev->wb.wb);
524                 if (r) {
525                         dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
526                         return r;
527                 }
528
529                 adev->wb.num_wb = AMDGPU_MAX_WB;
530                 memset(&adev->wb.used, 0, sizeof(adev->wb.used));
531
532                 /* clear wb memory */
533                 memset((char *)adev->wb.wb, 0, AMDGPU_MAX_WB * sizeof(uint32_t));
534         }
535
536         return 0;
537 }
538
539 /**
540  * amdgpu_wb_get - Allocate a wb entry
541  *
542  * @adev: amdgpu_device pointer
543  * @wb: wb index
544  *
545  * Allocate a wb slot for use by the driver (all asics).
546  * Returns 0 on success or -EINVAL on failure.
547  */
548 int amdgpu_wb_get(struct amdgpu_device *adev, u32 *wb)
549 {
550         unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
551         if (offset < adev->wb.num_wb) {
552                 __set_bit(offset, adev->wb.used);
553                 *wb = offset;
554                 return 0;
555         } else {
556                 return -EINVAL;
557         }
558 }
559
560 /**
561  * amdgpu_wb_get_64bit - Allocate a wb entry
562  *
563  * @adev: amdgpu_device pointer
564  * @wb: wb index
565  *
566  * Allocate a wb slot for use by the driver (all asics).
567  * Returns 0 on success or -EINVAL on failure.
568  */
569 int amdgpu_wb_get_64bit(struct amdgpu_device *adev, u32 *wb)
570 {
571         unsigned long offset = bitmap_find_next_zero_area_off(adev->wb.used,
572                                 adev->wb.num_wb, 0, 2, 7, 0);
573         if ((offset + 1) < adev->wb.num_wb) {
574                 __set_bit(offset, adev->wb.used);
575                 __set_bit(offset + 1, adev->wb.used);
576                 *wb = offset;
577                 return 0;
578         } else {
579                 return -EINVAL;
580         }
581 }
582
583 /**
584  * amdgpu_wb_free - Free a wb entry
585  *
586  * @adev: amdgpu_device pointer
587  * @wb: wb index
588  *
589  * Free a wb slot allocated for use by the driver (all asics)
590  */
591 void amdgpu_wb_free(struct amdgpu_device *adev, u32 wb)
592 {
593         if (wb < adev->wb.num_wb)
594                 __clear_bit(wb, adev->wb.used);
595 }
596
597 /**
598  * amdgpu_wb_free_64bit - Free a wb entry
599  *
600  * @adev: amdgpu_device pointer
601  * @wb: wb index
602  *
603  * Free a wb slot allocated for use by the driver (all asics)
604  */
605 void amdgpu_wb_free_64bit(struct amdgpu_device *adev, u32 wb)
606 {
607         if ((wb + 1) < adev->wb.num_wb) {
608                 __clear_bit(wb, adev->wb.used);
609                 __clear_bit(wb + 1, adev->wb.used);
610         }
611 }
612
613 /**
614  * amdgpu_vram_location - try to find VRAM location
615  * @adev: amdgpu device structure holding all necessary informations
616  * @mc: memory controller structure holding memory informations
617  * @base: base address at which to put VRAM
618  *
619  * Function will place try to place VRAM at base address provided
620  * as parameter (which is so far either PCI aperture address or
621  * for IGP TOM base address).
622  *
623  * If there is not enough space to fit the unvisible VRAM in the 32bits
624  * address space then we limit the VRAM size to the aperture.
625  *
626  * Note: We don't explicitly enforce VRAM start to be aligned on VRAM size,
627  * this shouldn't be a problem as we are using the PCI aperture as a reference.
628  * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but
629  * not IGP.
630  *
631  * Note: we use mc_vram_size as on some board we need to program the mc to
632  * cover the whole aperture even if VRAM size is inferior to aperture size
633  * Novell bug 204882 + along with lots of ubuntu ones
634  *
635  * Note: when limiting vram it's safe to overwritte real_vram_size because
636  * we are not in case where real_vram_size is inferior to mc_vram_size (ie
637  * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu
638  * ones)
639  *
640  * Note: IGP TOM addr should be the same as the aperture addr, we don't
641  * explicitly check for that thought.
642  *
643  * FIXME: when reducing VRAM size align new size on power of 2.
644  */
645 void amdgpu_vram_location(struct amdgpu_device *adev, struct amdgpu_mc *mc, u64 base)
646 {
647         uint64_t limit = (uint64_t)amdgpu_vram_limit << 20;
648
649         mc->vram_start = base;
650         if (mc->mc_vram_size > (adev->mc.mc_mask - base + 1)) {
651                 dev_warn(adev->dev, "limiting VRAM to PCI aperture size\n");
652                 mc->real_vram_size = mc->aper_size;
653                 mc->mc_vram_size = mc->aper_size;
654         }
655         mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
656         if (limit && limit < mc->real_vram_size)
657                 mc->real_vram_size = limit;
658         dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
659                         mc->mc_vram_size >> 20, mc->vram_start,
660                         mc->vram_end, mc->real_vram_size >> 20);
661 }
662
663 /**
664  * amdgpu_gtt_location - try to find GTT location
665  * @adev: amdgpu device structure holding all necessary informations
666  * @mc: memory controller structure holding memory informations
667  *
668  * Function will place try to place GTT before or after VRAM.
669  *
670  * If GTT size is bigger than space left then we ajust GTT size.
671  * Thus function will never fails.
672  *
673  * FIXME: when reducing GTT size align new size on power of 2.
674  */
675 void amdgpu_gtt_location(struct amdgpu_device *adev, struct amdgpu_mc *mc)
676 {
677         u64 size_af, size_bf;
678
679         size_af = ((adev->mc.mc_mask - mc->vram_end) + mc->gtt_base_align) & ~mc->gtt_base_align;
680         size_bf = mc->vram_start & ~mc->gtt_base_align;
681         if (size_bf > size_af) {
682                 if (mc->gtt_size > size_bf) {
683                         dev_warn(adev->dev, "limiting GTT\n");
684                         mc->gtt_size = size_bf;
685                 }
686                 mc->gtt_start = 0;
687         } else {
688                 if (mc->gtt_size > size_af) {
689                         dev_warn(adev->dev, "limiting GTT\n");
690                         mc->gtt_size = size_af;
691                 }
692                 mc->gtt_start = (mc->vram_end + 1 + mc->gtt_base_align) & ~mc->gtt_base_align;
693         }
694         mc->gtt_end = mc->gtt_start + mc->gtt_size - 1;
695         dev_info(adev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
696                         mc->gtt_size >> 20, mc->gtt_start, mc->gtt_end);
697 }
698
699 /*
700  * GPU helpers function.
701  */
702 /**
703  * amdgpu_need_post - check if the hw need post or not
704  *
705  * @adev: amdgpu_device pointer
706  *
707  * Check if the asic has been initialized (all asics) at driver startup
708  * or post is needed if  hw reset is performed.
709  * Returns true if need or false if not.
710  */
711 bool amdgpu_need_post(struct amdgpu_device *adev)
712 {
713         uint32_t reg;
714
715         if (adev->has_hw_reset) {
716                 adev->has_hw_reset = false;
717                 return true;
718         }
719         /* then check MEM_SIZE, in case the crtcs are off */
720         reg = amdgpu_asic_get_config_memsize(adev);
721
722         if ((reg != 0) && (reg != 0xffffffff))
723                 return false;
724
725         return true;
726
727 }
728
729 static bool amdgpu_vpost_needed(struct amdgpu_device *adev)
730 {
731         if (amdgpu_sriov_vf(adev))
732                 return false;
733
734         if (amdgpu_passthrough(adev)) {
735                 /* for FIJI: In whole GPU pass-through virtualization case, after VM reboot
736                  * some old smc fw still need driver do vPost otherwise gpu hang, while
737                  * those smc fw version above 22.15 doesn't have this flaw, so we force
738                  * vpost executed for smc version below 22.15
739                  */
740                 if (adev->asic_type == CHIP_FIJI) {
741                         int err;
742                         uint32_t fw_ver;
743                         err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
744                         /* force vPost if error occured */
745                         if (err)
746                                 return true;
747
748                         fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
749                         if (fw_ver < 0x00160e00)
750                                 return true;
751                 }
752         }
753         return amdgpu_need_post(adev);
754 }
755
756 /**
757  * amdgpu_dummy_page_init - init dummy page used by the driver
758  *
759  * @adev: amdgpu_device pointer
760  *
761  * Allocate the dummy page used by the driver (all asics).
762  * This dummy page is used by the driver as a filler for gart entries
763  * when pages are taken out of the GART
764  * Returns 0 on sucess, -ENOMEM on failure.
765  */
766 int amdgpu_dummy_page_init(struct amdgpu_device *adev)
767 {
768         if (adev->dummy_page.page)
769                 return 0;
770         adev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
771         if (adev->dummy_page.page == NULL)
772                 return -ENOMEM;
773         adev->dummy_page.addr = pci_map_page(adev->pdev, adev->dummy_page.page,
774                                         0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
775         if (pci_dma_mapping_error(adev->pdev, adev->dummy_page.addr)) {
776                 dev_err(&adev->pdev->dev, "Failed to DMA MAP the dummy page\n");
777                 __free_page(adev->dummy_page.page);
778                 adev->dummy_page.page = NULL;
779                 return -ENOMEM;
780         }
781         return 0;
782 }
783
784 /**
785  * amdgpu_dummy_page_fini - free dummy page used by the driver
786  *
787  * @adev: amdgpu_device pointer
788  *
789  * Frees the dummy page used by the driver (all asics).
790  */
791 void amdgpu_dummy_page_fini(struct amdgpu_device *adev)
792 {
793         if (adev->dummy_page.page == NULL)
794                 return;
795         pci_unmap_page(adev->pdev, adev->dummy_page.addr,
796                         PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
797         __free_page(adev->dummy_page.page);
798         adev->dummy_page.page = NULL;
799 }
800
801
802 /* ATOM accessor methods */
803 /*
804  * ATOM is an interpreted byte code stored in tables in the vbios.  The
805  * driver registers callbacks to access registers and the interpreter
806  * in the driver parses the tables and executes then to program specific
807  * actions (set display modes, asic init, etc.).  See amdgpu_atombios.c,
808  * atombios.h, and atom.c
809  */
810
811 /**
812  * cail_pll_read - read PLL register
813  *
814  * @info: atom card_info pointer
815  * @reg: PLL register offset
816  *
817  * Provides a PLL register accessor for the atom interpreter (r4xx+).
818  * Returns the value of the PLL register.
819  */
820 static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
821 {
822         return 0;
823 }
824
825 /**
826  * cail_pll_write - write PLL register
827  *
828  * @info: atom card_info pointer
829  * @reg: PLL register offset
830  * @val: value to write to the pll register
831  *
832  * Provides a PLL register accessor for the atom interpreter (r4xx+).
833  */
834 static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
835 {
836
837 }
838
839 /**
840  * cail_mc_read - read MC (Memory Controller) register
841  *
842  * @info: atom card_info pointer
843  * @reg: MC register offset
844  *
845  * Provides an MC register accessor for the atom interpreter (r4xx+).
846  * Returns the value of the MC register.
847  */
848 static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
849 {
850         return 0;
851 }
852
853 /**
854  * cail_mc_write - write MC (Memory Controller) register
855  *
856  * @info: atom card_info pointer
857  * @reg: MC register offset
858  * @val: value to write to the pll register
859  *
860  * Provides a MC register accessor for the atom interpreter (r4xx+).
861  */
862 static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
863 {
864
865 }
866
867 /**
868  * cail_reg_write - write MMIO register
869  *
870  * @info: atom card_info pointer
871  * @reg: MMIO register offset
872  * @val: value to write to the pll register
873  *
874  * Provides a MMIO register accessor for the atom interpreter (r4xx+).
875  */
876 static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
877 {
878         struct amdgpu_device *adev = info->dev->dev_private;
879
880         WREG32(reg, val);
881 }
882
883 /**
884  * cail_reg_read - read MMIO register
885  *
886  * @info: atom card_info pointer
887  * @reg: MMIO register offset
888  *
889  * Provides an MMIO register accessor for the atom interpreter (r4xx+).
890  * Returns the value of the MMIO register.
891  */
892 static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
893 {
894         struct amdgpu_device *adev = info->dev->dev_private;
895         uint32_t r;
896
897         r = RREG32(reg);
898         return r;
899 }
900
901 /**
902  * cail_ioreg_write - write IO register
903  *
904  * @info: atom card_info pointer
905  * @reg: IO register offset
906  * @val: value to write to the pll register
907  *
908  * Provides a IO register accessor for the atom interpreter (r4xx+).
909  */
910 static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
911 {
912         struct amdgpu_device *adev = info->dev->dev_private;
913
914         WREG32_IO(reg, val);
915 }
916
917 /**
918  * cail_ioreg_read - read IO register
919  *
920  * @info: atom card_info pointer
921  * @reg: IO register offset
922  *
923  * Provides an IO register accessor for the atom interpreter (r4xx+).
924  * Returns the value of the IO register.
925  */
926 static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
927 {
928         struct amdgpu_device *adev = info->dev->dev_private;
929         uint32_t r;
930
931         r = RREG32_IO(reg);
932         return r;
933 }
934
935 /**
936  * amdgpu_atombios_fini - free the driver info and callbacks for atombios
937  *
938  * @adev: amdgpu_device pointer
939  *
940  * Frees the driver info and register access callbacks for the ATOM
941  * interpreter (r4xx+).
942  * Called at driver shutdown.
943  */
944 static void amdgpu_atombios_fini(struct amdgpu_device *adev)
945 {
946         if (adev->mode_info.atom_context) {
947                 kfree(adev->mode_info.atom_context->scratch);
948                 kfree(adev->mode_info.atom_context->iio);
949         }
950         kfree(adev->mode_info.atom_context);
951         adev->mode_info.atom_context = NULL;
952         kfree(adev->mode_info.atom_card_info);
953         adev->mode_info.atom_card_info = NULL;
954 }
955
956 /**
957  * amdgpu_atombios_init - init the driver info and callbacks for atombios
958  *
959  * @adev: amdgpu_device pointer
960  *
961  * Initializes the driver info and register access callbacks for the
962  * ATOM interpreter (r4xx+).
963  * Returns 0 on sucess, -ENOMEM on failure.
964  * Called at driver startup.
965  */
966 static int amdgpu_atombios_init(struct amdgpu_device *adev)
967 {
968         struct card_info *atom_card_info =
969             kzalloc(sizeof(struct card_info), GFP_KERNEL);
970
971         if (!atom_card_info)
972                 return -ENOMEM;
973
974         adev->mode_info.atom_card_info = atom_card_info;
975         atom_card_info->dev = adev->ddev;
976         atom_card_info->reg_read = cail_reg_read;
977         atom_card_info->reg_write = cail_reg_write;
978         /* needed for iio ops */
979         if (adev->rio_mem) {
980                 atom_card_info->ioreg_read = cail_ioreg_read;
981                 atom_card_info->ioreg_write = cail_ioreg_write;
982         } else {
983                 DRM_INFO("PCI I/O BAR is not found. Using MMIO to access ATOM BIOS\n");
984                 atom_card_info->ioreg_read = cail_reg_read;
985                 atom_card_info->ioreg_write = cail_reg_write;
986         }
987         atom_card_info->mc_read = cail_mc_read;
988         atom_card_info->mc_write = cail_mc_write;
989         atom_card_info->pll_read = cail_pll_read;
990         atom_card_info->pll_write = cail_pll_write;
991
992         adev->mode_info.atom_context = amdgpu_atom_parse(atom_card_info, adev->bios);
993         if (!adev->mode_info.atom_context) {
994                 amdgpu_atombios_fini(adev);
995                 return -ENOMEM;
996         }
997
998         mutex_init(&adev->mode_info.atom_context->mutex);
999         if (adev->is_atom_fw) {
1000                 amdgpu_atomfirmware_scratch_regs_init(adev);
1001                 amdgpu_atomfirmware_allocate_fb_scratch(adev);
1002         } else {
1003                 amdgpu_atombios_scratch_regs_init(adev);
1004                 amdgpu_atombios_allocate_fb_scratch(adev);
1005         }
1006         return 0;
1007 }
1008
1009 /* if we get transitioned to only one device, take VGA back */
1010 /**
1011  * amdgpu_vga_set_decode - enable/disable vga decode
1012  *
1013  * @cookie: amdgpu_device pointer
1014  * @state: enable/disable vga decode
1015  *
1016  * Enable/disable vga decode (all asics).
1017  * Returns VGA resource flags.
1018  */
1019 static unsigned int amdgpu_vga_set_decode(void *cookie, bool state)
1020 {
1021         struct amdgpu_device *adev = cookie;
1022         amdgpu_asic_set_vga_state(adev, state);
1023         if (state)
1024                 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
1025                        VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1026         else
1027                 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1028 }
1029
1030 /**
1031  * amdgpu_check_pot_argument - check that argument is a power of two
1032  *
1033  * @arg: value to check
1034  *
1035  * Validates that a certain argument is a power of two (all asics).
1036  * Returns true if argument is valid.
1037  */
1038 static bool amdgpu_check_pot_argument(int arg)
1039 {
1040         return (arg & (arg - 1)) == 0;
1041 }
1042
1043 static void amdgpu_check_block_size(struct amdgpu_device *adev)
1044 {
1045         /* defines number of bits in page table versus page directory,
1046          * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
1047          * page table and the remaining bits are in the page directory */
1048         if (amdgpu_vm_block_size == -1)
1049                 return;
1050
1051         if (amdgpu_vm_block_size < 9) {
1052                 dev_warn(adev->dev, "VM page table size (%d) too small\n",
1053                          amdgpu_vm_block_size);
1054                 goto def_value;
1055         }
1056
1057         if (amdgpu_vm_block_size > 24 ||
1058             (amdgpu_vm_size * 1024) < (1ull << amdgpu_vm_block_size)) {
1059                 dev_warn(adev->dev, "VM page table size (%d) too large\n",
1060                          amdgpu_vm_block_size);
1061                 goto def_value;
1062         }
1063
1064         return;
1065
1066 def_value:
1067         amdgpu_vm_block_size = -1;
1068 }
1069
1070 static void amdgpu_check_vm_size(struct amdgpu_device *adev)
1071 {
1072         if (!amdgpu_check_pot_argument(amdgpu_vm_size)) {
1073                 dev_warn(adev->dev, "VM size (%d) must be a power of 2\n",
1074                          amdgpu_vm_size);
1075                 goto def_value;
1076         }
1077
1078         if (amdgpu_vm_size < 1) {
1079                 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
1080                          amdgpu_vm_size);
1081                 goto def_value;
1082         }
1083
1084         /*
1085          * Max GPUVM size for Cayman, SI, CI VI are 40 bits.
1086          */
1087         if (amdgpu_vm_size > 1024) {
1088                 dev_warn(adev->dev, "VM size (%d) too large, max is 1TB\n",
1089                          amdgpu_vm_size);
1090                 goto def_value;
1091         }
1092
1093         return;
1094
1095 def_value:
1096         amdgpu_vm_size = -1;
1097 }
1098
1099 /**
1100  * amdgpu_check_arguments - validate module params
1101  *
1102  * @adev: amdgpu_device pointer
1103  *
1104  * Validates certain module parameters and updates
1105  * the associated values used by the driver (all asics).
1106  */
1107 static void amdgpu_check_arguments(struct amdgpu_device *adev)
1108 {
1109         if (amdgpu_sched_jobs < 4) {
1110                 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
1111                          amdgpu_sched_jobs);
1112                 amdgpu_sched_jobs = 4;
1113         } else if (!amdgpu_check_pot_argument(amdgpu_sched_jobs)){
1114                 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
1115                          amdgpu_sched_jobs);
1116                 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
1117         }
1118
1119         if (amdgpu_gart_size != -1) {
1120                 /* gtt size must be greater or equal to 32M */
1121                 if (amdgpu_gart_size < 32) {
1122                         dev_warn(adev->dev, "gart size (%d) too small\n",
1123                                  amdgpu_gart_size);
1124                         amdgpu_gart_size = -1;
1125                 }
1126         }
1127
1128         amdgpu_check_vm_size(adev);
1129
1130         amdgpu_check_block_size(adev);
1131
1132         if (amdgpu_vram_page_split != -1 && (amdgpu_vram_page_split < 16 ||
1133             !amdgpu_check_pot_argument(amdgpu_vram_page_split))) {
1134                 dev_warn(adev->dev, "invalid VRAM page split (%d)\n",
1135                          amdgpu_vram_page_split);
1136                 amdgpu_vram_page_split = 1024;
1137         }
1138 }
1139
1140 /**
1141  * amdgpu_switcheroo_set_state - set switcheroo state
1142  *
1143  * @pdev: pci dev pointer
1144  * @state: vga_switcheroo state
1145  *
1146  * Callback for the switcheroo driver.  Suspends or resumes the
1147  * the asics before or after it is powered up using ACPI methods.
1148  */
1149 static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1150 {
1151         struct drm_device *dev = pci_get_drvdata(pdev);
1152
1153         if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
1154                 return;
1155
1156         if (state == VGA_SWITCHEROO_ON) {
1157                 unsigned d3_delay = dev->pdev->d3_delay;
1158
1159                 pr_info("amdgpu: switched on\n");
1160                 /* don't suspend or resume card normally */
1161                 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1162
1163                 amdgpu_device_resume(dev, true, true);
1164
1165                 dev->pdev->d3_delay = d3_delay;
1166
1167                 dev->switch_power_state = DRM_SWITCH_POWER_ON;
1168                 drm_kms_helper_poll_enable(dev);
1169         } else {
1170                 pr_info("amdgpu: switched off\n");
1171                 drm_kms_helper_poll_disable(dev);
1172                 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1173                 amdgpu_device_suspend(dev, true, true);
1174                 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1175         }
1176 }
1177
1178 /**
1179  * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1180  *
1181  * @pdev: pci dev pointer
1182  *
1183  * Callback for the switcheroo driver.  Check of the switcheroo
1184  * state can be changed.
1185  * Returns true if the state can be changed, false if not.
1186  */
1187 static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1188 {
1189         struct drm_device *dev = pci_get_drvdata(pdev);
1190
1191         /*
1192         * FIXME: open_count is protected by drm_global_mutex but that would lead to
1193         * locking inversion with the driver load path. And the access here is
1194         * completely racy anyway. So don't bother with locking for now.
1195         */
1196         return dev->open_count == 0;
1197 }
1198
1199 static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1200         .set_gpu_state = amdgpu_switcheroo_set_state,
1201         .reprobe = NULL,
1202         .can_switch = amdgpu_switcheroo_can_switch,
1203 };
1204
1205 int amdgpu_set_clockgating_state(struct amdgpu_device *adev,
1206                                   enum amd_ip_block_type block_type,
1207                                   enum amd_clockgating_state state)
1208 {
1209         int i, r = 0;
1210
1211         for (i = 0; i < adev->num_ip_blocks; i++) {
1212                 if (!adev->ip_blocks[i].status.valid)
1213                         continue;
1214                 if (adev->ip_blocks[i].version->type != block_type)
1215                         continue;
1216                 if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
1217                         continue;
1218                 r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
1219                         (void *)adev, state);
1220                 if (r)
1221                         DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
1222                                   adev->ip_blocks[i].version->funcs->name, r);
1223         }
1224         return r;
1225 }
1226
1227 int amdgpu_set_powergating_state(struct amdgpu_device *adev,
1228                                   enum amd_ip_block_type block_type,
1229                                   enum amd_powergating_state state)
1230 {
1231         int i, r = 0;
1232
1233         for (i = 0; i < adev->num_ip_blocks; i++) {
1234                 if (!adev->ip_blocks[i].status.valid)
1235                         continue;
1236                 if (adev->ip_blocks[i].version->type != block_type)
1237                         continue;
1238                 if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
1239                         continue;
1240                 r = adev->ip_blocks[i].version->funcs->set_powergating_state(
1241                         (void *)adev, state);
1242                 if (r)
1243                         DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
1244                                   adev->ip_blocks[i].version->funcs->name, r);
1245         }
1246         return r;
1247 }
1248
1249 void amdgpu_get_clockgating_state(struct amdgpu_device *adev, u32 *flags)
1250 {
1251         int i;
1252
1253         for (i = 0; i < adev->num_ip_blocks; i++) {
1254                 if (!adev->ip_blocks[i].status.valid)
1255                         continue;
1256                 if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
1257                         adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
1258         }
1259 }
1260
1261 int amdgpu_wait_for_idle(struct amdgpu_device *adev,
1262                          enum amd_ip_block_type block_type)
1263 {
1264         int i, r;
1265
1266         for (i = 0; i < adev->num_ip_blocks; i++) {
1267                 if (!adev->ip_blocks[i].status.valid)
1268                         continue;
1269                 if (adev->ip_blocks[i].version->type == block_type) {
1270                         r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
1271                         if (r)
1272                                 return r;
1273                         break;
1274                 }
1275         }
1276         return 0;
1277
1278 }
1279
1280 bool amdgpu_is_idle(struct amdgpu_device *adev,
1281                     enum amd_ip_block_type block_type)
1282 {
1283         int i;
1284
1285         for (i = 0; i < adev->num_ip_blocks; i++) {
1286                 if (!adev->ip_blocks[i].status.valid)
1287                         continue;
1288                 if (adev->ip_blocks[i].version->type == block_type)
1289                         return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
1290         }
1291         return true;
1292
1293 }
1294
1295 struct amdgpu_ip_block * amdgpu_get_ip_block(struct amdgpu_device *adev,
1296                                              enum amd_ip_block_type type)
1297 {
1298         int i;
1299
1300         for (i = 0; i < adev->num_ip_blocks; i++)
1301                 if (adev->ip_blocks[i].version->type == type)
1302                         return &adev->ip_blocks[i];
1303
1304         return NULL;
1305 }
1306
1307 /**
1308  * amdgpu_ip_block_version_cmp
1309  *
1310  * @adev: amdgpu_device pointer
1311  * @type: enum amd_ip_block_type
1312  * @major: major version
1313  * @minor: minor version
1314  *
1315  * return 0 if equal or greater
1316  * return 1 if smaller or the ip_block doesn't exist
1317  */
1318 int amdgpu_ip_block_version_cmp(struct amdgpu_device *adev,
1319                                 enum amd_ip_block_type type,
1320                                 u32 major, u32 minor)
1321 {
1322         struct amdgpu_ip_block *ip_block = amdgpu_get_ip_block(adev, type);
1323
1324         if (ip_block && ((ip_block->version->major > major) ||
1325                         ((ip_block->version->major == major) &&
1326                         (ip_block->version->minor >= minor))))
1327                 return 0;
1328
1329         return 1;
1330 }
1331
1332 /**
1333  * amdgpu_ip_block_add
1334  *
1335  * @adev: amdgpu_device pointer
1336  * @ip_block_version: pointer to the IP to add
1337  *
1338  * Adds the IP block driver information to the collection of IPs
1339  * on the asic.
1340  */
1341 int amdgpu_ip_block_add(struct amdgpu_device *adev,
1342                         const struct amdgpu_ip_block_version *ip_block_version)
1343 {
1344         if (!ip_block_version)
1345                 return -EINVAL;
1346
1347         adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
1348
1349         return 0;
1350 }
1351
1352 static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
1353 {
1354         adev->enable_virtual_display = false;
1355
1356         if (amdgpu_virtual_display) {
1357                 struct drm_device *ddev = adev->ddev;
1358                 const char *pci_address_name = pci_name(ddev->pdev);
1359                 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
1360
1361                 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1362                 pciaddstr_tmp = pciaddstr;
1363                 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1364                         pciaddname = strsep(&pciaddname_tmp, ",");
1365                         if (!strcmp("all", pciaddname)
1366                             || !strcmp(pci_address_name, pciaddname)) {
1367                                 long num_crtc;
1368                                 int res = -1;
1369
1370                                 adev->enable_virtual_display = true;
1371
1372                                 if (pciaddname_tmp)
1373                                         res = kstrtol(pciaddname_tmp, 10,
1374                                                       &num_crtc);
1375
1376                                 if (!res) {
1377                                         if (num_crtc < 1)
1378                                                 num_crtc = 1;
1379                                         if (num_crtc > 6)
1380                                                 num_crtc = 6;
1381                                         adev->mode_info.num_crtc = num_crtc;
1382                                 } else {
1383                                         adev->mode_info.num_crtc = 1;
1384                                 }
1385                                 break;
1386                         }
1387                 }
1388
1389                 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
1390                          amdgpu_virtual_display, pci_address_name,
1391                          adev->enable_virtual_display, adev->mode_info.num_crtc);
1392
1393                 kfree(pciaddstr);
1394         }
1395 }
1396
1397 static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev)
1398 {
1399         const struct firmware *fw;
1400         const char *chip_name;
1401         char fw_name[30];
1402         int err;
1403         const struct gpu_info_firmware_header_v1_0 *hdr;
1404
1405         switch (adev->asic_type) {
1406         case CHIP_TOPAZ:
1407         case CHIP_TONGA:
1408         case CHIP_FIJI:
1409         case CHIP_POLARIS11:
1410         case CHIP_POLARIS10:
1411         case CHIP_POLARIS12:
1412         case CHIP_CARRIZO:
1413         case CHIP_STONEY:
1414 #ifdef CONFIG_DRM_AMDGPU_SI
1415         case CHIP_VERDE:
1416         case CHIP_TAHITI:
1417         case CHIP_PITCAIRN:
1418         case CHIP_OLAND:
1419         case CHIP_HAINAN:
1420 #endif
1421 #ifdef CONFIG_DRM_AMDGPU_CIK
1422         case CHIP_BONAIRE:
1423         case CHIP_HAWAII:
1424         case CHIP_KAVERI:
1425         case CHIP_KABINI:
1426         case CHIP_MULLINS:
1427 #endif
1428         default:
1429                 return 0;
1430         case CHIP_VEGA10:
1431                 chip_name = "vega10";
1432                 break;
1433         }
1434
1435         snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name);
1436         err = request_firmware(&fw, fw_name, adev->dev);
1437         if (err) {
1438                 dev_err(adev->dev,
1439                         "Failed to load gpu_info firmware \"%s\"\n",
1440                         fw_name);
1441                 goto out;
1442         }
1443         err = amdgpu_ucode_validate(fw);
1444         if (err) {
1445                 dev_err(adev->dev,
1446                         "Failed to validate gpu_info firmware \"%s\"\n",
1447                         fw_name);
1448                 goto out;
1449         }
1450
1451         hdr = (const struct gpu_info_firmware_header_v1_0 *)fw->data;
1452         amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
1453
1454         switch (hdr->version_major) {
1455         case 1:
1456         {
1457                 const struct gpu_info_firmware_v1_0 *gpu_info_fw =
1458                         (const struct gpu_info_firmware_v1_0 *)(fw->data +
1459                                                                 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1460
1461                 adev->gfx.config.max_shader_engines = gpu_info_fw->gc_num_se;
1462                 adev->gfx.config.max_cu_per_sh = gpu_info_fw->gc_num_cu_per_sh;
1463                 adev->gfx.config.max_sh_per_se = gpu_info_fw->gc_num_sh_per_se;
1464                 adev->gfx.config.max_backends_per_se = gpu_info_fw->gc_num_rb_per_se;
1465                 adev->gfx.config.max_texture_channel_caches =
1466                         gpu_info_fw->gc_num_tccs;
1467                 adev->gfx.config.max_gprs = gpu_info_fw->gc_num_gprs;
1468                 adev->gfx.config.max_gs_threads = gpu_info_fw->gc_num_max_gs_thds;
1469                 adev->gfx.config.gs_vgt_table_depth = gpu_info_fw->gc_gs_table_depth;
1470                 adev->gfx.config.gs_prim_buffer_depth = gpu_info_fw->gc_gsprim_buff_depth;
1471                 adev->gfx.config.double_offchip_lds_buf =
1472                         gpu_info_fw->gc_double_offchip_lds_buffer;
1473                 adev->gfx.cu_info.wave_front_size = gpu_info_fw->gc_wave_size;
1474                 break;
1475         }
1476         default:
1477                 dev_err(adev->dev,
1478                         "Unsupported gpu_info table %d\n", hdr->header.ucode_version);
1479                 err = -EINVAL;
1480                 goto out;
1481         }
1482 out:
1483         release_firmware(fw);
1484         fw = NULL;
1485
1486         return err;
1487 }
1488
1489 static int amdgpu_early_init(struct amdgpu_device *adev)
1490 {
1491         int i, r;
1492
1493         amdgpu_device_enable_virtual_display(adev);
1494
1495         switch (adev->asic_type) {
1496         case CHIP_TOPAZ:
1497         case CHIP_TONGA:
1498         case CHIP_FIJI:
1499         case CHIP_POLARIS11:
1500         case CHIP_POLARIS10:
1501         case CHIP_POLARIS12:
1502         case CHIP_CARRIZO:
1503         case CHIP_STONEY:
1504                 if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
1505                         adev->family = AMDGPU_FAMILY_CZ;
1506                 else
1507                         adev->family = AMDGPU_FAMILY_VI;
1508
1509                 r = vi_set_ip_blocks(adev);
1510                 if (r)
1511                         return r;
1512                 break;
1513 #ifdef CONFIG_DRM_AMDGPU_SI
1514         case CHIP_VERDE:
1515         case CHIP_TAHITI:
1516         case CHIP_PITCAIRN:
1517         case CHIP_OLAND:
1518         case CHIP_HAINAN:
1519                 adev->family = AMDGPU_FAMILY_SI;
1520                 r = si_set_ip_blocks(adev);
1521                 if (r)
1522                         return r;
1523                 break;
1524 #endif
1525 #ifdef CONFIG_DRM_AMDGPU_CIK
1526         case CHIP_BONAIRE:
1527         case CHIP_HAWAII:
1528         case CHIP_KAVERI:
1529         case CHIP_KABINI:
1530         case CHIP_MULLINS:
1531                 if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
1532                         adev->family = AMDGPU_FAMILY_CI;
1533                 else
1534                         adev->family = AMDGPU_FAMILY_KV;
1535
1536                 r = cik_set_ip_blocks(adev);
1537                 if (r)
1538                         return r;
1539                 break;
1540 #endif
1541         case CHIP_VEGA10:
1542                 adev->family = AMDGPU_FAMILY_AI;
1543
1544                 r = soc15_set_ip_blocks(adev);
1545                 if (r)
1546                         return r;
1547                 break;
1548         default:
1549                 /* FIXME: not supported yet */
1550                 return -EINVAL;
1551         }
1552
1553         r = amdgpu_device_parse_gpu_info_fw(adev);
1554         if (r)
1555                 return r;
1556
1557         if (amdgpu_sriov_vf(adev)) {
1558                 r = amdgpu_virt_request_full_gpu(adev, true);
1559                 if (r)
1560                         return r;
1561         }
1562
1563         for (i = 0; i < adev->num_ip_blocks; i++) {
1564                 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
1565                         DRM_ERROR("disabled ip block: %d\n", i);
1566                         adev->ip_blocks[i].status.valid = false;
1567                 } else {
1568                         if (adev->ip_blocks[i].version->funcs->early_init) {
1569                                 r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
1570                                 if (r == -ENOENT) {
1571                                         adev->ip_blocks[i].status.valid = false;
1572                                 } else if (r) {
1573                                         DRM_ERROR("early_init of IP block <%s> failed %d\n",
1574                                                   adev->ip_blocks[i].version->funcs->name, r);
1575                                         return r;
1576                                 } else {
1577                                         adev->ip_blocks[i].status.valid = true;
1578                                 }
1579                         } else {
1580                                 adev->ip_blocks[i].status.valid = true;
1581                         }
1582                 }
1583         }
1584
1585         adev->cg_flags &= amdgpu_cg_mask;
1586         adev->pg_flags &= amdgpu_pg_mask;
1587
1588         return 0;
1589 }
1590
1591 static int amdgpu_init(struct amdgpu_device *adev)
1592 {
1593         int i, r;
1594
1595         for (i = 0; i < adev->num_ip_blocks; i++) {
1596                 if (!adev->ip_blocks[i].status.valid)
1597                         continue;
1598                 r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
1599                 if (r) {
1600                         DRM_ERROR("sw_init of IP block <%s> failed %d\n",
1601                                   adev->ip_blocks[i].version->funcs->name, r);
1602                         return r;
1603                 }
1604                 adev->ip_blocks[i].status.sw = true;
1605                 /* need to do gmc hw init early so we can allocate gpu mem */
1606                 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
1607                         r = amdgpu_vram_scratch_init(adev);
1608                         if (r) {
1609                                 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
1610                                 return r;
1611                         }
1612                         r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
1613                         if (r) {
1614                                 DRM_ERROR("hw_init %d failed %d\n", i, r);
1615                                 return r;
1616                         }
1617                         r = amdgpu_wb_init(adev);
1618                         if (r) {
1619                                 DRM_ERROR("amdgpu_wb_init failed %d\n", r);
1620                                 return r;
1621                         }
1622                         adev->ip_blocks[i].status.hw = true;
1623
1624                         /* right after GMC hw init, we create CSA */
1625                         if (amdgpu_sriov_vf(adev)) {
1626                                 r = amdgpu_allocate_static_csa(adev);
1627                                 if (r) {
1628                                         DRM_ERROR("allocate CSA failed %d\n", r);
1629                                         return r;
1630                                 }
1631                         }
1632                 }
1633         }
1634
1635         for (i = 0; i < adev->num_ip_blocks; i++) {
1636                 if (!adev->ip_blocks[i].status.sw)
1637                         continue;
1638                 /* gmc hw init is done early */
1639                 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC)
1640                         continue;
1641                 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
1642                 if (r) {
1643                         DRM_ERROR("hw_init of IP block <%s> failed %d\n",
1644                                   adev->ip_blocks[i].version->funcs->name, r);
1645                         return r;
1646                 }
1647                 adev->ip_blocks[i].status.hw = true;
1648         }
1649
1650         return 0;
1651 }
1652
1653 static int amdgpu_late_init(struct amdgpu_device *adev)
1654 {
1655         int i = 0, r;
1656
1657         for (i = 0; i < adev->num_ip_blocks; i++) {
1658                 if (!adev->ip_blocks[i].status.valid)
1659                         continue;
1660                 if (adev->ip_blocks[i].version->funcs->late_init) {
1661                         r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
1662                         if (r) {
1663                                 DRM_ERROR("late_init of IP block <%s> failed %d\n",
1664                                           adev->ip_blocks[i].version->funcs->name, r);
1665                                 return r;
1666                         }
1667                         adev->ip_blocks[i].status.late_initialized = true;
1668                 }
1669                 /* skip CG for VCE/UVD, it's handled specially */
1670                 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1671                     adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1672                         /* enable clockgating to save power */
1673                         r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1674                                                                                      AMD_CG_STATE_GATE);
1675                         if (r) {
1676                                 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
1677                                           adev->ip_blocks[i].version->funcs->name, r);
1678                                 return r;
1679                         }
1680                 }
1681         }
1682
1683         return 0;
1684 }
1685
1686 static int amdgpu_fini(struct amdgpu_device *adev)
1687 {
1688         int i, r;
1689
1690         /* need to disable SMC first */
1691         for (i = 0; i < adev->num_ip_blocks; i++) {
1692                 if (!adev->ip_blocks[i].status.hw)
1693                         continue;
1694                 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
1695                         /* ungate blocks before hw fini so that we can shutdown the blocks safely */
1696                         r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1697                                                                                      AMD_CG_STATE_UNGATE);
1698                         if (r) {
1699                                 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1700                                           adev->ip_blocks[i].version->funcs->name, r);
1701                                 return r;
1702                         }
1703                         r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
1704                         /* XXX handle errors */
1705                         if (r) {
1706                                 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
1707                                           adev->ip_blocks[i].version->funcs->name, r);
1708                         }
1709                         adev->ip_blocks[i].status.hw = false;
1710                         break;
1711                 }
1712         }
1713
1714         for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
1715                 if (!adev->ip_blocks[i].status.hw)
1716                         continue;
1717                 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
1718                         amdgpu_wb_fini(adev);
1719                         amdgpu_vram_scratch_fini(adev);
1720                 }
1721
1722                 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1723                         adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1724                         /* ungate blocks before hw fini so that we can shutdown the blocks safely */
1725                         r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1726                                                                                      AMD_CG_STATE_UNGATE);
1727                         if (r) {
1728                                 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1729                                           adev->ip_blocks[i].version->funcs->name, r);
1730                                 return r;
1731                         }
1732                 }
1733
1734                 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
1735                 /* XXX handle errors */
1736                 if (r) {
1737                         DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
1738                                   adev->ip_blocks[i].version->funcs->name, r);
1739                 }
1740
1741                 adev->ip_blocks[i].status.hw = false;
1742         }
1743
1744         for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
1745                 if (!adev->ip_blocks[i].status.sw)
1746                         continue;
1747                 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
1748                 /* XXX handle errors */
1749                 if (r) {
1750                         DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
1751                                   adev->ip_blocks[i].version->funcs->name, r);
1752                 }
1753                 adev->ip_blocks[i].status.sw = false;
1754                 adev->ip_blocks[i].status.valid = false;
1755         }
1756
1757         for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
1758                 if (!adev->ip_blocks[i].status.late_initialized)
1759                         continue;
1760                 if (adev->ip_blocks[i].version->funcs->late_fini)
1761                         adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
1762                 adev->ip_blocks[i].status.late_initialized = false;
1763         }
1764
1765         if (amdgpu_sriov_vf(adev)) {
1766                 amdgpu_bo_free_kernel(&adev->virt.csa_obj, &adev->virt.csa_vmid0_addr, NULL);
1767                 amdgpu_virt_release_full_gpu(adev, false);
1768         }
1769
1770         return 0;
1771 }
1772
1773 int amdgpu_suspend(struct amdgpu_device *adev)
1774 {
1775         int i, r;
1776
1777         if (amdgpu_sriov_vf(adev))
1778                 amdgpu_virt_request_full_gpu(adev, false);
1779
1780         /* ungate SMC block first */
1781         r = amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC,
1782                                          AMD_CG_STATE_UNGATE);
1783         if (r) {
1784                 DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n",r);
1785         }
1786
1787         for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
1788                 if (!adev->ip_blocks[i].status.valid)
1789                         continue;
1790                 /* ungate blocks so that suspend can properly shut them down */
1791                 if (i != AMD_IP_BLOCK_TYPE_SMC) {
1792                         r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1793                                                                                      AMD_CG_STATE_UNGATE);
1794                         if (r) {
1795                                 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1796                                           adev->ip_blocks[i].version->funcs->name, r);
1797                         }
1798                 }
1799                 /* XXX handle errors */
1800                 r = adev->ip_blocks[i].version->funcs->suspend(adev);
1801                 /* XXX handle errors */
1802                 if (r) {
1803                         DRM_ERROR("suspend of IP block <%s> failed %d\n",
1804                                   adev->ip_blocks[i].version->funcs->name, r);
1805                 }
1806         }
1807
1808         if (amdgpu_sriov_vf(adev))
1809                 amdgpu_virt_release_full_gpu(adev, false);
1810
1811         return 0;
1812 }
1813
1814 static int amdgpu_sriov_reinit_early(struct amdgpu_device *adev)
1815 {
1816         int i, r;
1817
1818         static enum amd_ip_block_type ip_order[] = {
1819                 AMD_IP_BLOCK_TYPE_GMC,
1820                 AMD_IP_BLOCK_TYPE_COMMON,
1821                 AMD_IP_BLOCK_TYPE_GFXHUB,
1822                 AMD_IP_BLOCK_TYPE_MMHUB,
1823                 AMD_IP_BLOCK_TYPE_IH,
1824         };
1825
1826         for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1827                 int j;
1828                 struct amdgpu_ip_block *block;
1829
1830                 for (j = 0; j < adev->num_ip_blocks; j++) {
1831                         block = &adev->ip_blocks[j];
1832
1833                         if (block->version->type != ip_order[i] ||
1834                                 !block->status.valid)
1835                                 continue;
1836
1837                         r = block->version->funcs->hw_init(adev);
1838                         DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
1839                 }
1840         }
1841
1842         return 0;
1843 }
1844
1845 static int amdgpu_sriov_reinit_late(struct amdgpu_device *adev)
1846 {
1847         int i, r;
1848
1849         static enum amd_ip_block_type ip_order[] = {
1850                 AMD_IP_BLOCK_TYPE_SMC,
1851                 AMD_IP_BLOCK_TYPE_DCE,
1852                 AMD_IP_BLOCK_TYPE_GFX,
1853                 AMD_IP_BLOCK_TYPE_SDMA,
1854                 AMD_IP_BLOCK_TYPE_VCE,
1855         };
1856
1857         for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1858                 int j;
1859                 struct amdgpu_ip_block *block;
1860
1861                 for (j = 0; j < adev->num_ip_blocks; j++) {
1862                         block = &adev->ip_blocks[j];
1863
1864                         if (block->version->type != ip_order[i] ||
1865                                 !block->status.valid)
1866                                 continue;
1867
1868                         r = block->version->funcs->hw_init(adev);
1869                         DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
1870                 }
1871         }
1872
1873         return 0;
1874 }
1875
1876 static int amdgpu_resume(struct amdgpu_device *adev)
1877 {
1878         int i, r;
1879
1880         for (i = 0; i < adev->num_ip_blocks; i++) {
1881                 if (!adev->ip_blocks[i].status.valid)
1882                         continue;
1883                 r = adev->ip_blocks[i].version->funcs->resume(adev);
1884                 if (r) {
1885                         DRM_ERROR("resume of IP block <%s> failed %d\n",
1886                                   adev->ip_blocks[i].version->funcs->name, r);
1887                         return r;
1888                 }
1889         }
1890
1891         return 0;
1892 }
1893
1894 static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
1895 {
1896         if (adev->is_atom_fw) {
1897                 if (amdgpu_atomfirmware_gpu_supports_virtualization(adev))
1898                         adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
1899         } else {
1900                 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
1901                         adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
1902         }
1903 }
1904
1905 /**
1906  * amdgpu_device_init - initialize the driver
1907  *
1908  * @adev: amdgpu_device pointer
1909  * @pdev: drm dev pointer
1910  * @pdev: pci dev pointer
1911  * @flags: driver flags
1912  *
1913  * Initializes the driver info and hw (all asics).
1914  * Returns 0 for success or an error on failure.
1915  * Called at driver startup.
1916  */
1917 int amdgpu_device_init(struct amdgpu_device *adev,
1918                        struct drm_device *ddev,
1919                        struct pci_dev *pdev,
1920                        uint32_t flags)
1921 {
1922         int r, i;
1923         bool runtime = false;
1924         u32 max_MBps;
1925
1926         adev->shutdown = false;
1927         adev->dev = &pdev->dev;
1928         adev->ddev = ddev;
1929         adev->pdev = pdev;
1930         adev->flags = flags;
1931         adev->asic_type = flags & AMD_ASIC_MASK;
1932         adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
1933         adev->mc.gtt_size = 512 * 1024 * 1024;
1934         adev->accel_working = false;
1935         adev->num_rings = 0;
1936         adev->mman.buffer_funcs = NULL;
1937         adev->mman.buffer_funcs_ring = NULL;
1938         adev->vm_manager.vm_pte_funcs = NULL;
1939         adev->vm_manager.vm_pte_num_rings = 0;
1940         adev->gart.gart_funcs = NULL;
1941         adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
1942
1943         adev->smc_rreg = &amdgpu_invalid_rreg;
1944         adev->smc_wreg = &amdgpu_invalid_wreg;
1945         adev->pcie_rreg = &amdgpu_invalid_rreg;
1946         adev->pcie_wreg = &amdgpu_invalid_wreg;
1947         adev->pciep_rreg = &amdgpu_invalid_rreg;
1948         adev->pciep_wreg = &amdgpu_invalid_wreg;
1949         adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
1950         adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
1951         adev->didt_rreg = &amdgpu_invalid_rreg;
1952         adev->didt_wreg = &amdgpu_invalid_wreg;
1953         adev->gc_cac_rreg = &amdgpu_invalid_rreg;
1954         adev->gc_cac_wreg = &amdgpu_invalid_wreg;
1955         adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
1956         adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
1957
1958
1959         DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
1960                  amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
1961                  pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
1962
1963         /* mutex initialization are all done here so we
1964          * can recall function without having locking issues */
1965         atomic_set(&adev->irq.ih.lock, 0);
1966         mutex_init(&adev->firmware.mutex);
1967         mutex_init(&adev->pm.mutex);
1968         mutex_init(&adev->gfx.gpu_clock_mutex);
1969         mutex_init(&adev->srbm_mutex);
1970         mutex_init(&adev->grbm_idx_mutex);
1971         mutex_init(&adev->mn_lock);
1972         hash_init(adev->mn_hash);
1973
1974         amdgpu_check_arguments(adev);
1975
1976         /* Registers mapping */
1977         /* TODO: block userspace mapping of io register */
1978         spin_lock_init(&adev->mmio_idx_lock);
1979         spin_lock_init(&adev->smc_idx_lock);
1980         spin_lock_init(&adev->pcie_idx_lock);
1981         spin_lock_init(&adev->uvd_ctx_idx_lock);
1982         spin_lock_init(&adev->didt_idx_lock);
1983         spin_lock_init(&adev->gc_cac_idx_lock);
1984         spin_lock_init(&adev->audio_endpt_idx_lock);
1985         spin_lock_init(&adev->mm_stats.lock);
1986
1987         INIT_LIST_HEAD(&adev->shadow_list);
1988         mutex_init(&adev->shadow_list_lock);
1989
1990         INIT_LIST_HEAD(&adev->gtt_list);
1991         spin_lock_init(&adev->gtt_list_lock);
1992
1993         if (adev->asic_type >= CHIP_BONAIRE) {
1994                 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
1995                 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
1996         } else {
1997                 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
1998                 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
1999         }
2000
2001         adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
2002         if (adev->rmmio == NULL) {
2003                 return -ENOMEM;
2004         }
2005         DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
2006         DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
2007
2008         if (adev->asic_type >= CHIP_BONAIRE)
2009                 /* doorbell bar mapping */
2010                 amdgpu_doorbell_init(adev);
2011
2012         /* io port mapping */
2013         for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
2014                 if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
2015                         adev->rio_mem_size = pci_resource_len(adev->pdev, i);
2016                         adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
2017                         break;
2018                 }
2019         }
2020         if (adev->rio_mem == NULL)
2021                 DRM_INFO("PCI I/O BAR is not found.\n");
2022
2023         /* early init functions */
2024         r = amdgpu_early_init(adev);
2025         if (r)
2026                 return r;
2027
2028         /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
2029         /* this will fail for cards that aren't VGA class devices, just
2030          * ignore it */
2031         vga_client_register(adev->pdev, adev, NULL, amdgpu_vga_set_decode);
2032
2033         if (amdgpu_runtime_pm == 1)
2034                 runtime = true;
2035         if (amdgpu_device_is_px(ddev))
2036                 runtime = true;
2037         if (!pci_is_thunderbolt_attached(adev->pdev))
2038                 vga_switcheroo_register_client(adev->pdev,
2039                                                &amdgpu_switcheroo_ops, runtime);
2040         if (runtime)
2041                 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
2042
2043         /* Read BIOS */
2044         if (!amdgpu_get_bios(adev)) {
2045                 r = -EINVAL;
2046                 goto failed;
2047         }
2048
2049         r = amdgpu_atombios_init(adev);
2050         if (r) {
2051                 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
2052                 goto failed;
2053         }
2054
2055         /* detect if we are with an SRIOV vbios */
2056         amdgpu_device_detect_sriov_bios(adev);
2057
2058         /* Post card if necessary */
2059         if (amdgpu_vpost_needed(adev)) {
2060                 if (!adev->bios) {
2061                         dev_err(adev->dev, "no vBIOS found\n");
2062                         r = -EINVAL;
2063                         goto failed;
2064                 }
2065                 DRM_INFO("GPU posting now...\n");
2066                 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2067                 if (r) {
2068                         dev_err(adev->dev, "gpu post error!\n");
2069                         goto failed;
2070                 }
2071         } else {
2072                 DRM_INFO("GPU post is not needed\n");
2073         }
2074
2075         if (!adev->is_atom_fw) {
2076                 /* Initialize clocks */
2077                 r = amdgpu_atombios_get_clock_info(adev);
2078                 if (r) {
2079                         dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
2080                         return r;
2081                 }
2082                 /* init i2c buses */
2083                 amdgpu_atombios_i2c_init(adev);
2084         }
2085
2086         /* Fence driver */
2087         r = amdgpu_fence_driver_init(adev);
2088         if (r) {
2089                 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
2090                 goto failed;
2091         }
2092
2093         /* init the mode config */
2094         drm_mode_config_init(adev->ddev);
2095
2096         r = amdgpu_init(adev);
2097         if (r) {
2098                 dev_err(adev->dev, "amdgpu_init failed\n");
2099                 amdgpu_fini(adev);
2100                 goto failed;
2101         }
2102
2103         adev->accel_working = true;
2104
2105         /* Initialize the buffer migration limit. */
2106         if (amdgpu_moverate >= 0)
2107                 max_MBps = amdgpu_moverate;
2108         else
2109                 max_MBps = 8; /* Allow 8 MB/s. */
2110         /* Get a log2 for easy divisions. */
2111         adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
2112
2113         r = amdgpu_ib_pool_init(adev);
2114         if (r) {
2115                 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
2116                 goto failed;
2117         }
2118
2119         r = amdgpu_ib_ring_tests(adev);
2120         if (r)
2121                 DRM_ERROR("ib ring test failed (%d).\n", r);
2122
2123         amdgpu_fbdev_init(adev);
2124
2125         r = amdgpu_gem_debugfs_init(adev);
2126         if (r)
2127                 DRM_ERROR("registering gem debugfs failed (%d).\n", r);
2128
2129         r = amdgpu_debugfs_regs_init(adev);
2130         if (r)
2131                 DRM_ERROR("registering register debugfs failed (%d).\n", r);
2132
2133         r = amdgpu_debugfs_firmware_init(adev);
2134         if (r)
2135                 DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
2136
2137         if ((amdgpu_testing & 1)) {
2138                 if (adev->accel_working)
2139                         amdgpu_test_moves(adev);
2140                 else
2141                         DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
2142         }
2143         if (amdgpu_benchmarking) {
2144                 if (adev->accel_working)
2145                         amdgpu_benchmark(adev, amdgpu_benchmarking);
2146                 else
2147                         DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
2148         }
2149
2150         /* enable clockgating, etc. after ib tests, etc. since some blocks require
2151          * explicit gating rather than handling it automatically.
2152          */
2153         r = amdgpu_late_init(adev);
2154         if (r) {
2155                 dev_err(adev->dev, "amdgpu_late_init failed\n");
2156                 goto failed;
2157         }
2158
2159         return 0;
2160
2161 failed:
2162         if (runtime)
2163                 vga_switcheroo_fini_domain_pm_ops(adev->dev);
2164         return r;
2165 }
2166
2167 /**
2168  * amdgpu_device_fini - tear down the driver
2169  *
2170  * @adev: amdgpu_device pointer
2171  *
2172  * Tear down the driver info (all asics).
2173  * Called at driver shutdown.
2174  */
2175 void amdgpu_device_fini(struct amdgpu_device *adev)
2176 {
2177         int r;
2178
2179         DRM_INFO("amdgpu: finishing device.\n");
2180         adev->shutdown = true;
2181         if (adev->mode_info.mode_config_initialized)
2182                 drm_crtc_force_disable_all(adev->ddev);
2183         /* evict vram memory */
2184         amdgpu_bo_evict_vram(adev);
2185         amdgpu_ib_pool_fini(adev);
2186         amdgpu_fence_driver_fini(adev);
2187         amdgpu_fbdev_fini(adev);
2188         r = amdgpu_fini(adev);
2189         adev->accel_working = false;
2190         /* free i2c buses */
2191         amdgpu_i2c_fini(adev);
2192         amdgpu_atombios_fini(adev);
2193         kfree(adev->bios);
2194         adev->bios = NULL;
2195         if (!pci_is_thunderbolt_attached(adev->pdev))
2196                 vga_switcheroo_unregister_client(adev->pdev);
2197         if (adev->flags & AMD_IS_PX)
2198                 vga_switcheroo_fini_domain_pm_ops(adev->dev);
2199         vga_client_register(adev->pdev, NULL, NULL, NULL);
2200         if (adev->rio_mem)
2201                 pci_iounmap(adev->pdev, adev->rio_mem);
2202         adev->rio_mem = NULL;
2203         iounmap(adev->rmmio);
2204         adev->rmmio = NULL;
2205         if (adev->asic_type >= CHIP_BONAIRE)
2206                 amdgpu_doorbell_fini(adev);
2207         amdgpu_debugfs_regs_cleanup(adev);
2208 }
2209
2210
2211 /*
2212  * Suspend & resume.
2213  */
2214 /**
2215  * amdgpu_device_suspend - initiate device suspend
2216  *
2217  * @pdev: drm dev pointer
2218  * @state: suspend state
2219  *
2220  * Puts the hw in the suspend state (all asics).
2221  * Returns 0 for success or an error on failure.
2222  * Called at driver suspend.
2223  */
2224 int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
2225 {
2226         struct amdgpu_device *adev;
2227         struct drm_crtc *crtc;
2228         struct drm_connector *connector;
2229         int r;
2230
2231         if (dev == NULL || dev->dev_private == NULL) {
2232                 return -ENODEV;
2233         }
2234
2235         adev = dev->dev_private;
2236
2237         if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2238                 return 0;
2239
2240         drm_kms_helper_poll_disable(dev);
2241
2242         /* turn off display hw */
2243         drm_modeset_lock_all(dev);
2244         list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2245                 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
2246         }
2247         drm_modeset_unlock_all(dev);
2248
2249         /* unpin the front buffers and cursors */
2250         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2251                 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2252                 struct amdgpu_framebuffer *rfb = to_amdgpu_framebuffer(crtc->primary->fb);
2253                 struct amdgpu_bo *robj;
2254
2255                 if (amdgpu_crtc->cursor_bo) {
2256                         struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
2257                         r = amdgpu_bo_reserve(aobj, true);
2258                         if (r == 0) {
2259                                 amdgpu_bo_unpin(aobj);
2260                                 amdgpu_bo_unreserve(aobj);
2261                         }
2262                 }
2263
2264                 if (rfb == NULL || rfb->obj == NULL) {
2265                         continue;
2266                 }
2267                 robj = gem_to_amdgpu_bo(rfb->obj);
2268                 /* don't unpin kernel fb objects */
2269                 if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
2270                         r = amdgpu_bo_reserve(robj, true);
2271                         if (r == 0) {
2272                                 amdgpu_bo_unpin(robj);
2273                                 amdgpu_bo_unreserve(robj);
2274                         }
2275                 }
2276         }
2277         /* evict vram memory */
2278         amdgpu_bo_evict_vram(adev);
2279
2280         amdgpu_fence_driver_suspend(adev);
2281
2282         r = amdgpu_suspend(adev);
2283
2284         /* evict remaining vram memory
2285          * This second call to evict vram is to evict the gart page table
2286          * using the CPU.
2287          */
2288         amdgpu_bo_evict_vram(adev);
2289
2290         if (adev->is_atom_fw)
2291                 amdgpu_atomfirmware_scratch_regs_save(adev);
2292         else
2293                 amdgpu_atombios_scratch_regs_save(adev);
2294         pci_save_state(dev->pdev);
2295         if (suspend) {
2296                 /* Shut down the device */
2297                 pci_disable_device(dev->pdev);
2298                 pci_set_power_state(dev->pdev, PCI_D3hot);
2299         } else {
2300                 r = amdgpu_asic_reset(adev);
2301                 if (r)
2302                         DRM_ERROR("amdgpu asic reset failed\n");
2303         }
2304
2305         if (fbcon) {
2306                 console_lock();
2307                 amdgpu_fbdev_set_suspend(adev, 1);
2308                 console_unlock();
2309         }
2310         return 0;
2311 }
2312
2313 /**
2314  * amdgpu_device_resume - initiate device resume
2315  *
2316  * @pdev: drm dev pointer
2317  *
2318  * Bring the hw back to operating state (all asics).
2319  * Returns 0 for success or an error on failure.
2320  * Called at driver resume.
2321  */
2322 int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
2323 {
2324         struct drm_connector *connector;
2325         struct amdgpu_device *adev = dev->dev_private;
2326         struct drm_crtc *crtc;
2327         int r = 0;
2328
2329         if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2330                 return 0;
2331
2332         if (fbcon)
2333                 console_lock();
2334
2335         if (resume) {
2336                 pci_set_power_state(dev->pdev, PCI_D0);
2337                 pci_restore_state(dev->pdev);
2338                 r = pci_enable_device(dev->pdev);
2339                 if (r)
2340                         goto unlock;
2341         }
2342         if (adev->is_atom_fw)
2343                 amdgpu_atomfirmware_scratch_regs_restore(adev);
2344         else
2345                 amdgpu_atombios_scratch_regs_restore(adev);
2346
2347         /* post card */
2348         if (amdgpu_need_post(adev)) {
2349                 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2350                 if (r)
2351                         DRM_ERROR("amdgpu asic init failed\n");
2352         }
2353
2354         r = amdgpu_resume(adev);
2355         if (r) {
2356                 DRM_ERROR("amdgpu_resume failed (%d).\n", r);
2357                 goto unlock;
2358         }
2359         amdgpu_fence_driver_resume(adev);
2360
2361         if (resume) {
2362                 r = amdgpu_ib_ring_tests(adev);
2363                 if (r)
2364                         DRM_ERROR("ib ring test failed (%d).\n", r);
2365         }
2366
2367         r = amdgpu_late_init(adev);
2368         if (r)
2369                 goto unlock;
2370
2371         /* pin cursors */
2372         list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2373                 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2374
2375                 if (amdgpu_crtc->cursor_bo) {
2376                         struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
2377                         r = amdgpu_bo_reserve(aobj, true);
2378                         if (r == 0) {
2379                                 r = amdgpu_bo_pin(aobj,
2380                                                   AMDGPU_GEM_DOMAIN_VRAM,
2381                                                   &amdgpu_crtc->cursor_addr);
2382                                 if (r != 0)
2383                                         DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
2384                                 amdgpu_bo_unreserve(aobj);
2385                         }
2386                 }
2387         }
2388
2389         /* blat the mode back in */
2390         if (fbcon) {
2391                 drm_helper_resume_force_mode(dev);
2392                 /* turn on display hw */
2393                 drm_modeset_lock_all(dev);
2394                 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2395                         drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
2396                 }
2397                 drm_modeset_unlock_all(dev);
2398         }
2399
2400         drm_kms_helper_poll_enable(dev);
2401
2402         /*
2403          * Most of the connector probing functions try to acquire runtime pm
2404          * refs to ensure that the GPU is powered on when connector polling is
2405          * performed. Since we're calling this from a runtime PM callback,
2406          * trying to acquire rpm refs will cause us to deadlock.
2407          *
2408          * Since we're guaranteed to be holding the rpm lock, it's safe to
2409          * temporarily disable the rpm helpers so this doesn't deadlock us.
2410          */
2411 #ifdef CONFIG_PM
2412         dev->dev->power.disable_depth++;
2413 #endif
2414         drm_helper_hpd_irq_event(dev);
2415 #ifdef CONFIG_PM
2416         dev->dev->power.disable_depth--;
2417 #endif
2418
2419         if (fbcon)
2420                 amdgpu_fbdev_set_suspend(adev, 0);
2421
2422 unlock:
2423         if (fbcon)
2424                 console_unlock();
2425
2426         return r;
2427 }
2428
2429 static bool amdgpu_check_soft_reset(struct amdgpu_device *adev)
2430 {
2431         int i;
2432         bool asic_hang = false;
2433
2434         for (i = 0; i < adev->num_ip_blocks; i++) {
2435                 if (!adev->ip_blocks[i].status.valid)
2436                         continue;
2437                 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
2438                         adev->ip_blocks[i].status.hang =
2439                                 adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
2440                 if (adev->ip_blocks[i].status.hang) {
2441                         DRM_INFO("IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
2442                         asic_hang = true;
2443                 }
2444         }
2445         return asic_hang;
2446 }
2447
2448 static int amdgpu_pre_soft_reset(struct amdgpu_device *adev)
2449 {
2450         int i, r = 0;
2451
2452         for (i = 0; i < adev->num_ip_blocks; i++) {
2453                 if (!adev->ip_blocks[i].status.valid)
2454                         continue;
2455                 if (adev->ip_blocks[i].status.hang &&
2456                     adev->ip_blocks[i].version->funcs->pre_soft_reset) {
2457                         r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
2458                         if (r)
2459                                 return r;
2460                 }
2461         }
2462
2463         return 0;
2464 }
2465
2466 static bool amdgpu_need_full_reset(struct amdgpu_device *adev)
2467 {
2468         int i;
2469
2470         for (i = 0; i < adev->num_ip_blocks; i++) {
2471                 if (!adev->ip_blocks[i].status.valid)
2472                         continue;
2473                 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
2474                     (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
2475                     (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
2476                     (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE)) {
2477                         if (adev->ip_blocks[i].status.hang) {
2478                                 DRM_INFO("Some block need full reset!\n");
2479                                 return true;
2480                         }
2481                 }
2482         }
2483         return false;
2484 }
2485
2486 static int amdgpu_soft_reset(struct amdgpu_device *adev)
2487 {
2488         int i, r = 0;
2489
2490         for (i = 0; i < adev->num_ip_blocks; i++) {
2491                 if (!adev->ip_blocks[i].status.valid)
2492                         continue;
2493                 if (adev->ip_blocks[i].status.hang &&
2494                     adev->ip_blocks[i].version->funcs->soft_reset) {
2495                         r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
2496                         if (r)
2497                                 return r;
2498                 }
2499         }
2500
2501         return 0;
2502 }
2503
2504 static int amdgpu_post_soft_reset(struct amdgpu_device *adev)
2505 {
2506         int i, r = 0;
2507
2508         for (i = 0; i < adev->num_ip_blocks; i++) {
2509                 if (!adev->ip_blocks[i].status.valid)
2510                         continue;
2511                 if (adev->ip_blocks[i].status.hang &&
2512                     adev->ip_blocks[i].version->funcs->post_soft_reset)
2513                         r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
2514                 if (r)
2515                         return r;
2516         }
2517
2518         return 0;
2519 }
2520
2521 bool amdgpu_need_backup(struct amdgpu_device *adev)
2522 {
2523         if (adev->flags & AMD_IS_APU)
2524                 return false;
2525
2526         return amdgpu_lockup_timeout > 0 ? true : false;
2527 }
2528
2529 static int amdgpu_recover_vram_from_shadow(struct amdgpu_device *adev,
2530                                            struct amdgpu_ring *ring,
2531                                            struct amdgpu_bo *bo,
2532                                            struct dma_fence **fence)
2533 {
2534         uint32_t domain;
2535         int r;
2536
2537         if (!bo->shadow)
2538                 return 0;
2539
2540         r = amdgpu_bo_reserve(bo, true);
2541         if (r)
2542                 return r;
2543         domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
2544         /* if bo has been evicted, then no need to recover */
2545         if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
2546                 r = amdgpu_bo_validate(bo->shadow);
2547                 if (r) {
2548                         DRM_ERROR("bo validate failed!\n");
2549                         goto err;
2550                 }
2551
2552                 r = amdgpu_ttm_bind(&bo->shadow->tbo, &bo->shadow->tbo.mem);
2553                 if (r) {
2554                         DRM_ERROR("%p bind failed\n", bo->shadow);
2555                         goto err;
2556                 }
2557
2558                 r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
2559                                                  NULL, fence, true);
2560                 if (r) {
2561                         DRM_ERROR("recover page table failed!\n");
2562                         goto err;
2563                 }
2564         }
2565 err:
2566         amdgpu_bo_unreserve(bo);
2567         return r;
2568 }
2569
2570 /**
2571  * amdgpu_sriov_gpu_reset - reset the asic
2572  *
2573  * @adev: amdgpu device pointer
2574  * @voluntary: if this reset is requested by guest.
2575  *             (true means by guest and false means by HYPERVISOR )
2576  *
2577  * Attempt the reset the GPU if it has hung (all asics).
2578  * for SRIOV case.
2579  * Returns 0 for success or an error on failure.
2580  */
2581 int amdgpu_sriov_gpu_reset(struct amdgpu_device *adev, bool voluntary)
2582 {
2583         int i, r = 0;
2584         int resched;
2585         struct amdgpu_bo *bo, *tmp;
2586         struct amdgpu_ring *ring;
2587         struct dma_fence *fence = NULL, *next = NULL;
2588
2589         mutex_lock(&adev->virt.lock_reset);
2590         atomic_inc(&adev->gpu_reset_counter);
2591         adev->gfx.in_reset = true;
2592
2593         /* block TTM */
2594         resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
2595
2596         /* block scheduler */
2597         for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2598                 ring = adev->rings[i];
2599
2600                 if (!ring || !ring->sched.thread)
2601                         continue;
2602
2603                 kthread_park(ring->sched.thread);
2604                 amd_sched_hw_job_reset(&ring->sched);
2605         }
2606
2607         /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
2608         amdgpu_fence_driver_force_completion(adev);
2609
2610         /* request to take full control of GPU before re-initialization  */
2611         if (voluntary)
2612                 amdgpu_virt_reset_gpu(adev);
2613         else
2614                 amdgpu_virt_request_full_gpu(adev, true);
2615
2616
2617         /* Resume IP prior to SMC */
2618         amdgpu_sriov_reinit_early(adev);
2619
2620         /* we need recover gart prior to run SMC/CP/SDMA resume */
2621         amdgpu_ttm_recover_gart(adev);
2622
2623         /* now we are okay to resume SMC/CP/SDMA */
2624         amdgpu_sriov_reinit_late(adev);
2625
2626         amdgpu_irq_gpu_reset_resume_helper(adev);
2627
2628         if (amdgpu_ib_ring_tests(adev))
2629                 dev_err(adev->dev, "[GPU_RESET] ib ring test failed (%d).\n", r);
2630
2631         /* release full control of GPU after ib test */
2632         amdgpu_virt_release_full_gpu(adev, true);
2633
2634         DRM_INFO("recover vram bo from shadow\n");
2635
2636         ring = adev->mman.buffer_funcs_ring;
2637         mutex_lock(&adev->shadow_list_lock);
2638         list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
2639                 next = NULL;
2640                 amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
2641                 if (fence) {
2642                         r = dma_fence_wait(fence, false);
2643                         if (r) {
2644                                 WARN(r, "recovery from shadow isn't completed\n");
2645                                 break;
2646                         }
2647                 }
2648
2649                 dma_fence_put(fence);
2650                 fence = next;
2651         }
2652         mutex_unlock(&adev->shadow_list_lock);
2653
2654         if (fence) {
2655                 r = dma_fence_wait(fence, false);
2656                 if (r)
2657                         WARN(r, "recovery from shadow isn't completed\n");
2658         }
2659         dma_fence_put(fence);
2660
2661         for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2662                 struct amdgpu_ring *ring = adev->rings[i];
2663                 if (!ring || !ring->sched.thread)
2664                         continue;
2665
2666                 amd_sched_job_recovery(&ring->sched);
2667                 kthread_unpark(ring->sched.thread);
2668         }
2669
2670         drm_helper_resume_force_mode(adev->ddev);
2671         ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
2672         if (r) {
2673                 /* bad news, how to tell it to userspace ? */
2674                 dev_info(adev->dev, "GPU reset failed\n");
2675         }
2676
2677         adev->gfx.in_reset = false;
2678         mutex_unlock(&adev->virt.lock_reset);
2679         return r;
2680 }
2681
2682 /**
2683  * amdgpu_gpu_reset - reset the asic
2684  *
2685  * @adev: amdgpu device pointer
2686  *
2687  * Attempt the reset the GPU if it has hung (all asics).
2688  * Returns 0 for success or an error on failure.
2689  */
2690 int amdgpu_gpu_reset(struct amdgpu_device *adev)
2691 {
2692         int i, r;
2693         int resched;
2694         bool need_full_reset;
2695
2696         if (amdgpu_sriov_vf(adev))
2697                 return amdgpu_sriov_gpu_reset(adev, true);
2698
2699         if (!amdgpu_check_soft_reset(adev)) {
2700                 DRM_INFO("No hardware hang detected. Did some blocks stall?\n");
2701                 return 0;
2702         }
2703
2704         atomic_inc(&adev->gpu_reset_counter);
2705
2706         /* block TTM */
2707         resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
2708
2709         /* block scheduler */
2710         for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2711                 struct amdgpu_ring *ring = adev->rings[i];
2712
2713                 if (!ring || !ring->sched.thread)
2714                         continue;
2715                 kthread_park(ring->sched.thread);
2716                 amd_sched_hw_job_reset(&ring->sched);
2717         }
2718         /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
2719         amdgpu_fence_driver_force_completion(adev);
2720
2721         need_full_reset = amdgpu_need_full_reset(adev);
2722
2723         if (!need_full_reset) {
2724                 amdgpu_pre_soft_reset(adev);
2725                 r = amdgpu_soft_reset(adev);
2726                 amdgpu_post_soft_reset(adev);
2727                 if (r || amdgpu_check_soft_reset(adev)) {
2728                         DRM_INFO("soft reset failed, will fallback to full reset!\n");
2729                         need_full_reset = true;
2730                 }
2731         }
2732
2733         if (need_full_reset) {
2734                 r = amdgpu_suspend(adev);
2735
2736 retry:
2737                 /* Disable fb access */
2738                 if (adev->mode_info.num_crtc) {
2739                         struct amdgpu_mode_mc_save save;
2740                         amdgpu_display_stop_mc_access(adev, &save);
2741                         amdgpu_wait_for_idle(adev, AMD_IP_BLOCK_TYPE_GMC);
2742                 }
2743                 if (adev->is_atom_fw)
2744                         amdgpu_atomfirmware_scratch_regs_save(adev);
2745                 else
2746                         amdgpu_atombios_scratch_regs_save(adev);
2747                 r = amdgpu_asic_reset(adev);
2748                 if (adev->is_atom_fw)
2749                         amdgpu_atomfirmware_scratch_regs_restore(adev);
2750                 else
2751                         amdgpu_atombios_scratch_regs_restore(adev);
2752                 /* post card */
2753                 amdgpu_atom_asic_init(adev->mode_info.atom_context);
2754
2755                 if (!r) {
2756                         dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
2757                         r = amdgpu_resume(adev);
2758                 }
2759         }
2760         if (!r) {
2761                 amdgpu_irq_gpu_reset_resume_helper(adev);
2762                 if (need_full_reset && amdgpu_need_backup(adev)) {
2763                         r = amdgpu_ttm_recover_gart(adev);
2764                         if (r)
2765                                 DRM_ERROR("gart recovery failed!!!\n");
2766                 }
2767                 r = amdgpu_ib_ring_tests(adev);
2768                 if (r) {
2769                         dev_err(adev->dev, "ib ring test failed (%d).\n", r);
2770                         r = amdgpu_suspend(adev);
2771                         need_full_reset = true;
2772                         goto retry;
2773                 }
2774                 /**
2775                  * recovery vm page tables, since we cannot depend on VRAM is
2776                  * consistent after gpu full reset.
2777                  */
2778                 if (need_full_reset && amdgpu_need_backup(adev)) {
2779                         struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
2780                         struct amdgpu_bo *bo, *tmp;
2781                         struct dma_fence *fence = NULL, *next = NULL;
2782
2783                         DRM_INFO("recover vram bo from shadow\n");
2784                         mutex_lock(&adev->shadow_list_lock);
2785                         list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
2786                                 next = NULL;
2787                                 amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
2788                                 if (fence) {
2789                                         r = dma_fence_wait(fence, false);
2790                                         if (r) {
2791                                                 WARN(r, "recovery from shadow isn't completed\n");
2792                                                 break;
2793                                         }
2794                                 }
2795
2796                                 dma_fence_put(fence);
2797                                 fence = next;
2798                         }
2799                         mutex_unlock(&adev->shadow_list_lock);
2800                         if (fence) {
2801                                 r = dma_fence_wait(fence, false);
2802                                 if (r)
2803                                         WARN(r, "recovery from shadow isn't completed\n");
2804                         }
2805                         dma_fence_put(fence);
2806                 }
2807                 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2808                         struct amdgpu_ring *ring = adev->rings[i];
2809
2810                         if (!ring || !ring->sched.thread)
2811                                 continue;
2812
2813                         amd_sched_job_recovery(&ring->sched);
2814                         kthread_unpark(ring->sched.thread);
2815                 }
2816         } else {
2817                 dev_err(adev->dev, "asic resume failed (%d).\n", r);
2818                 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2819                         if (adev->rings[i] && adev->rings[i]->sched.thread) {
2820                                 kthread_unpark(adev->rings[i]->sched.thread);
2821                         }
2822                 }
2823         }
2824
2825         drm_helper_resume_force_mode(adev->ddev);
2826
2827         ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
2828         if (r) {
2829                 /* bad news, how to tell it to userspace ? */
2830                 dev_info(adev->dev, "GPU reset failed\n");
2831         }
2832
2833         return r;
2834 }
2835
2836 void amdgpu_get_pcie_info(struct amdgpu_device *adev)
2837 {
2838         u32 mask;
2839         int ret;
2840
2841         if (amdgpu_pcie_gen_cap)
2842                 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
2843
2844         if (amdgpu_pcie_lane_cap)
2845                 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
2846
2847         /* covers APUs as well */
2848         if (pci_is_root_bus(adev->pdev->bus)) {
2849                 if (adev->pm.pcie_gen_mask == 0)
2850                         adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
2851                 if (adev->pm.pcie_mlw_mask == 0)
2852                         adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
2853                 return;
2854         }
2855
2856         if (adev->pm.pcie_gen_mask == 0) {
2857                 ret = drm_pcie_get_speed_cap_mask(adev->ddev, &mask);
2858                 if (!ret) {
2859                         adev->pm.pcie_gen_mask = (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
2860                                                   CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
2861                                                   CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
2862
2863                         if (mask & DRM_PCIE_SPEED_25)
2864                                 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
2865                         if (mask & DRM_PCIE_SPEED_50)
2866                                 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2;
2867                         if (mask & DRM_PCIE_SPEED_80)
2868                                 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3;
2869                 } else {
2870                         adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
2871                 }
2872         }
2873         if (adev->pm.pcie_mlw_mask == 0) {
2874                 ret = drm_pcie_get_max_link_width(adev->ddev, &mask);
2875                 if (!ret) {
2876                         switch (mask) {
2877                         case 32:
2878                                 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
2879                                                           CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
2880                                                           CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2881                                                           CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2882                                                           CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2883                                                           CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2884                                                           CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2885                                 break;
2886                         case 16:
2887                                 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
2888                                                           CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2889                                                           CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2890                                                           CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2891                                                           CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2892                                                           CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2893                                 break;
2894                         case 12:
2895                                 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2896                                                           CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2897                                                           CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2898                                                           CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2899                                                           CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2900                                 break;
2901                         case 8:
2902                                 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2903                                                           CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2904                                                           CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2905                                                           CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2906                                 break;
2907                         case 4:
2908                                 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2909                                                           CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2910                                                           CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2911                                 break;
2912                         case 2:
2913                                 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2914                                                           CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2915                                 break;
2916                         case 1:
2917                                 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
2918                                 break;
2919                         default:
2920                                 break;
2921                         }
2922                 } else {
2923                         adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
2924                 }
2925         }
2926 }
2927
2928 /*
2929  * Debugfs
2930  */
2931 int amdgpu_debugfs_add_files(struct amdgpu_device *adev,
2932                              const struct drm_info_list *files,
2933                              unsigned nfiles)
2934 {
2935         unsigned i;
2936
2937         for (i = 0; i < adev->debugfs_count; i++) {
2938                 if (adev->debugfs[i].files == files) {
2939                         /* Already registered */
2940                         return 0;
2941                 }
2942         }
2943
2944         i = adev->debugfs_count + 1;
2945         if (i > AMDGPU_DEBUGFS_MAX_COMPONENTS) {
2946                 DRM_ERROR("Reached maximum number of debugfs components.\n");
2947                 DRM_ERROR("Report so we increase "
2948                           "AMDGPU_DEBUGFS_MAX_COMPONENTS.\n");
2949                 return -EINVAL;
2950         }
2951         adev->debugfs[adev->debugfs_count].files = files;
2952         adev->debugfs[adev->debugfs_count].num_files = nfiles;
2953         adev->debugfs_count = i;
2954 #if defined(CONFIG_DEBUG_FS)
2955         drm_debugfs_create_files(files, nfiles,
2956                                  adev->ddev->primary->debugfs_root,
2957                                  adev->ddev->primary);
2958 #endif
2959         return 0;
2960 }
2961
2962 #if defined(CONFIG_DEBUG_FS)
2963
2964 static ssize_t amdgpu_debugfs_regs_read(struct file *f, char __user *buf,
2965                                         size_t size, loff_t *pos)
2966 {
2967         struct amdgpu_device *adev = file_inode(f)->i_private;
2968         ssize_t result = 0;
2969         int r;
2970         bool pm_pg_lock, use_bank;
2971         unsigned instance_bank, sh_bank, se_bank;
2972
2973         if (size & 0x3 || *pos & 0x3)
2974                 return -EINVAL;
2975
2976         /* are we reading registers for which a PG lock is necessary? */
2977         pm_pg_lock = (*pos >> 23) & 1;
2978
2979         if (*pos & (1ULL << 62)) {
2980                 se_bank = (*pos >> 24) & 0x3FF;
2981                 sh_bank = (*pos >> 34) & 0x3FF;
2982                 instance_bank = (*pos >> 44) & 0x3FF;
2983
2984                 if (se_bank == 0x3FF)
2985                         se_bank = 0xFFFFFFFF;
2986                 if (sh_bank == 0x3FF)
2987                         sh_bank = 0xFFFFFFFF;
2988                 if (instance_bank == 0x3FF)
2989                         instance_bank = 0xFFFFFFFF;
2990                 use_bank = 1;
2991         } else {
2992                 use_bank = 0;
2993         }
2994
2995         *pos &= (1UL << 22) - 1;
2996
2997         if (use_bank) {
2998                 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
2999                     (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
3000                         return -EINVAL;
3001                 mutex_lock(&adev->grbm_idx_mutex);
3002                 amdgpu_gfx_select_se_sh(adev, se_bank,
3003                                         sh_bank, instance_bank);
3004         }
3005
3006         if (pm_pg_lock)
3007                 mutex_lock(&adev->pm.mutex);
3008
3009         while (size) {
3010                 uint32_t value;
3011
3012                 if (*pos > adev->rmmio_size)
3013                         goto end;
3014
3015                 value = RREG32(*pos >> 2);
3016                 r = put_user(value, (uint32_t *)buf);
3017                 if (r) {
3018                         result = r;
3019                         goto end;
3020                 }
3021
3022                 result += 4;
3023                 buf += 4;
3024                 *pos += 4;
3025                 size -= 4;
3026         }
3027
3028 end:
3029         if (use_bank) {
3030                 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
3031                 mutex_unlock(&adev->grbm_idx_mutex);
3032         }
3033
3034         if (pm_pg_lock)
3035                 mutex_unlock(&adev->pm.mutex);
3036
3037         return result;
3038 }
3039
3040 static ssize_t amdgpu_debugfs_regs_write(struct file *f, const char __user *buf,
3041                                          size_t size, loff_t *pos)
3042 {
3043         struct amdgpu_device *adev = file_inode(f)->i_private;
3044         ssize_t result = 0;
3045         int r;
3046         bool pm_pg_lock, use_bank;
3047         unsigned instance_bank, sh_bank, se_bank;
3048
3049         if (size & 0x3 || *pos & 0x3)
3050                 return -EINVAL;
3051
3052         /* are we reading registers for which a PG lock is necessary? */
3053         pm_pg_lock = (*pos >> 23) & 1;
3054
3055         if (*pos & (1ULL << 62)) {
3056                 se_bank = (*pos >> 24) & 0x3FF;
3057                 sh_bank = (*pos >> 34) & 0x3FF;
3058                 instance_bank = (*pos >> 44) & 0x3FF;
3059
3060                 if (se_bank == 0x3FF)
3061                         se_bank = 0xFFFFFFFF;
3062                 if (sh_bank == 0x3FF)
3063                         sh_bank = 0xFFFFFFFF;
3064                 if (instance_bank == 0x3FF)
3065                         instance_bank = 0xFFFFFFFF;
3066                 use_bank = 1;
3067         } else {
3068                 use_bank = 0;
3069         }
3070
3071         *pos &= (1UL << 22) - 1;
3072
3073         if (use_bank) {
3074                 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
3075                     (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
3076                         return -EINVAL;
3077                 mutex_lock(&adev->grbm_idx_mutex);
3078                 amdgpu_gfx_select_se_sh(adev, se_bank,
3079                                         sh_bank, instance_bank);
3080         }
3081
3082         if (pm_pg_lock)
3083                 mutex_lock(&adev->pm.mutex);
3084
3085         while (size) {
3086                 uint32_t value;
3087
3088                 if (*pos > adev->rmmio_size)
3089                         return result;
3090
3091                 r = get_user(value, (uint32_t *)buf);
3092                 if (r)
3093                         return r;
3094
3095                 WREG32(*pos >> 2, value);
3096
3097                 result += 4;
3098                 buf += 4;
3099                 *pos += 4;
3100                 size -= 4;
3101         }
3102
3103         if (use_bank) {
3104                 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
3105                 mutex_unlock(&adev->grbm_idx_mutex);
3106         }
3107
3108         if (pm_pg_lock)
3109                 mutex_unlock(&adev->pm.mutex);
3110
3111         return result;
3112 }
3113
3114 static ssize_t amdgpu_debugfs_regs_pcie_read(struct file *f, char __user *buf,
3115                                         size_t size, loff_t *pos)
3116 {
3117         struct amdgpu_device *adev = file_inode(f)->i_private;
3118         ssize_t result = 0;
3119         int r;
3120
3121         if (size & 0x3 || *pos & 0x3)
3122                 return -EINVAL;
3123
3124         while (size) {
3125                 uint32_t value;
3126
3127                 value = RREG32_PCIE(*pos >> 2);
3128                 r = put_user(value, (uint32_t *)buf);
3129                 if (r)
3130                         return r;
3131
3132                 result += 4;
3133                 buf += 4;
3134                 *pos += 4;
3135                 size -= 4;
3136         }
3137
3138         return result;
3139 }
3140
3141 static ssize_t amdgpu_debugfs_regs_pcie_write(struct file *f, const char __user *buf,
3142                                          size_t size, loff_t *pos)
3143 {
3144         struct amdgpu_device *adev = file_inode(f)->i_private;
3145         ssize_t result = 0;
3146         int r;
3147
3148         if (size & 0x3 || *pos & 0x3)
3149                 return -EINVAL;
3150
3151         while (size) {
3152                 uint32_t value;
3153
3154                 r = get_user(value, (uint32_t *)buf);
3155                 if (r)
3156                         return r;
3157
3158                 WREG32_PCIE(*pos >> 2, value);
3159
3160                 result += 4;
3161                 buf += 4;
3162                 *pos += 4;
3163                 size -= 4;
3164         }
3165
3166         return result;
3167 }
3168
3169 static ssize_t amdgpu_debugfs_regs_didt_read(struct file *f, char __user *buf,
3170                                         size_t size, loff_t *pos)
3171 {
3172         struct amdgpu_device *adev = file_inode(f)->i_private;
3173         ssize_t result = 0;
3174         int r;
3175
3176         if (size & 0x3 || *pos & 0x3)
3177                 return -EINVAL;
3178
3179         while (size) {
3180                 uint32_t value;
3181
3182                 value = RREG32_DIDT(*pos >> 2);
3183                 r = put_user(value, (uint32_t *)buf);
3184                 if (r)
3185                         return r;
3186
3187                 result += 4;
3188                 buf += 4;
3189                 *pos += 4;
3190                 size -= 4;
3191         }
3192
3193         return result;
3194 }
3195
3196 static ssize_t amdgpu_debugfs_regs_didt_write(struct file *f, const char __user *buf,
3197                                          size_t size, loff_t *pos)
3198 {
3199         struct amdgpu_device *adev = file_inode(f)->i_private;
3200         ssize_t result = 0;
3201         int r;
3202
3203         if (size & 0x3 || *pos & 0x3)
3204                 return -EINVAL;
3205
3206         while (size) {
3207                 uint32_t value;
3208
3209                 r = get_user(value, (uint32_t *)buf);
3210                 if (r)
3211                         return r;
3212
3213                 WREG32_DIDT(*pos >> 2, value);
3214
3215                 result += 4;
3216                 buf += 4;
3217                 *pos += 4;
3218                 size -= 4;
3219         }
3220
3221         return result;
3222 }
3223
3224 static ssize_t amdgpu_debugfs_regs_smc_read(struct file *f, char __user *buf,
3225                                         size_t size, loff_t *pos)
3226 {
3227         struct amdgpu_device *adev = file_inode(f)->i_private;
3228         ssize_t result = 0;
3229         int r;
3230
3231         if (size & 0x3 || *pos & 0x3)
3232                 return -EINVAL;
3233
3234         while (size) {
3235                 uint32_t value;
3236
3237                 value = RREG32_SMC(*pos);
3238                 r = put_user(value, (uint32_t *)buf);
3239                 if (r)
3240                         return r;
3241
3242                 result += 4;
3243                 buf += 4;
3244                 *pos += 4;
3245                 size -= 4;
3246         }
3247
3248         return result;
3249 }
3250
3251 static ssize_t amdgpu_debugfs_regs_smc_write(struct file *f, const char __user *buf,
3252                                          size_t size, loff_t *pos)
3253 {
3254         struct amdgpu_device *adev = file_inode(f)->i_private;
3255         ssize_t result = 0;
3256         int r;
3257
3258         if (size & 0x3 || *pos & 0x3)
3259                 return -EINVAL;
3260
3261         while (size) {
3262                 uint32_t value;
3263
3264                 r = get_user(value, (uint32_t *)buf);
3265                 if (r)
3266                         return r;
3267
3268                 WREG32_SMC(*pos, value);
3269
3270                 result += 4;
3271                 buf += 4;
3272                 *pos += 4;
3273                 size -= 4;
3274         }
3275
3276         return result;
3277 }
3278
3279 static ssize_t amdgpu_debugfs_gca_config_read(struct file *f, char __user *buf,
3280                                         size_t size, loff_t *pos)
3281 {
3282         struct amdgpu_device *adev = file_inode(f)->i_private;
3283         ssize_t result = 0;
3284         int r;
3285         uint32_t *config, no_regs = 0;
3286
3287         if (size & 0x3 || *pos & 0x3)
3288                 return -EINVAL;
3289
3290         config = kmalloc_array(256, sizeof(*config), GFP_KERNEL);
3291         if (!config)
3292                 return -ENOMEM;
3293
3294         /* version, increment each time something is added */
3295         config[no_regs++] = 3;
3296         config[no_regs++] = adev->gfx.config.max_shader_engines;
3297         config[no_regs++] = adev->gfx.config.max_tile_pipes;
3298         config[no_regs++] = adev->gfx.config.max_cu_per_sh;
3299         config[no_regs++] = adev->gfx.config.max_sh_per_se;
3300         config[no_regs++] = adev->gfx.config.max_backends_per_se;
3301         config[no_regs++] = adev->gfx.config.max_texture_channel_caches;
3302         config[no_regs++] = adev->gfx.config.max_gprs;
3303         config[no_regs++] = adev->gfx.config.max_gs_threads;
3304         config[no_regs++] = adev->gfx.config.max_hw_contexts;
3305         config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_frontend;
3306         config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_backend;
3307         config[no_regs++] = adev->gfx.config.sc_hiz_tile_fifo_size;
3308         config[no_regs++] = adev->gfx.config.sc_earlyz_tile_fifo_size;
3309         config[no_regs++] = adev->gfx.config.num_tile_pipes;
3310         config[no_regs++] = adev->gfx.config.backend_enable_mask;
3311         config[no_regs++] = adev->gfx.config.mem_max_burst_length_bytes;
3312         config[no_regs++] = adev->gfx.config.mem_row_size_in_kb;
3313         config[no_regs++] = adev->gfx.config.shader_engine_tile_size;
3314         config[no_regs++] = adev->gfx.config.num_gpus;
3315         config[no_regs++] = adev->gfx.config.multi_gpu_tile_size;
3316         config[no_regs++] = adev->gfx.config.mc_arb_ramcfg;
3317         config[no_regs++] = adev->gfx.config.gb_addr_config;
3318         config[no_regs++] = adev->gfx.config.num_rbs;
3319
3320         /* rev==1 */
3321         config[no_regs++] = adev->rev_id;
3322         config[no_regs++] = adev->pg_flags;
3323         config[no_regs++] = adev->cg_flags;
3324
3325         /* rev==2 */
3326         config[no_regs++] = adev->family;
3327         config[no_regs++] = adev->external_rev_id;
3328
3329         /* rev==3 */
3330         config[no_regs++] = adev->pdev->device;
3331         config[no_regs++] = adev->pdev->revision;
3332         config[no_regs++] = adev->pdev->subsystem_device;
3333         config[no_regs++] = adev->pdev->subsystem_vendor;
3334
3335         while (size && (*pos < no_regs * 4)) {
3336                 uint32_t value;
3337
3338                 value = config[*pos >> 2];
3339                 r = put_user(value, (uint32_t *)buf);
3340                 if (r) {
3341                         kfree(config);
3342                         return r;
3343                 }
3344
3345                 result += 4;
3346                 buf += 4;
3347                 *pos += 4;
3348                 size -= 4;
3349         }
3350
3351         kfree(config);
3352         return result;
3353 }
3354
3355 static ssize_t amdgpu_debugfs_sensor_read(struct file *f, char __user *buf,
3356                                         size_t size, loff_t *pos)
3357 {
3358         struct amdgpu_device *adev = file_inode(f)->i_private;
3359         int idx, x, outsize, r, valuesize;
3360         uint32_t values[16];
3361
3362         if (size & 3 || *pos & 0x3)
3363                 return -EINVAL;
3364
3365         if (amdgpu_dpm == 0)
3366                 return -EINVAL;
3367
3368         /* convert offset to sensor number */
3369         idx = *pos >> 2;
3370
3371         valuesize = sizeof(values);
3372         if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->read_sensor)
3373                 r = adev->powerplay.pp_funcs->read_sensor(adev->powerplay.pp_handle, idx, &values[0], &valuesize);
3374         else if (adev->pm.funcs && adev->pm.funcs->read_sensor)
3375                 r = adev->pm.funcs->read_sensor(adev, idx, &values[0],
3376                                                 &valuesize);
3377         else
3378                 return -EINVAL;
3379
3380         if (size > valuesize)
3381                 return -EINVAL;
3382
3383         outsize = 0;
3384         x = 0;
3385         if (!r) {
3386                 while (size) {
3387                         r = put_user(values[x++], (int32_t *)buf);
3388                         buf += 4;
3389                         size -= 4;
3390                         outsize += 4;
3391                 }
3392         }
3393
3394         return !r ? outsize : r;
3395 }
3396
3397 static ssize_t amdgpu_debugfs_wave_read(struct file *f, char __user *buf,
3398                                         size_t size, loff_t *pos)
3399 {
3400         struct amdgpu_device *adev = f->f_inode->i_private;
3401         int r, x;
3402         ssize_t result=0;
3403         uint32_t offset, se, sh, cu, wave, simd, data[32];
3404
3405         if (size & 3 || *pos & 3)
3406                 return -EINVAL;
3407
3408         /* decode offset */
3409         offset = (*pos & 0x7F);
3410         se = ((*pos >> 7) & 0xFF);
3411         sh = ((*pos >> 15) & 0xFF);
3412         cu = ((*pos >> 23) & 0xFF);
3413         wave = ((*pos >> 31) & 0xFF);
3414         simd = ((*pos >> 37) & 0xFF);
3415
3416         /* switch to the specific se/sh/cu */
3417         mutex_lock(&adev->grbm_idx_mutex);
3418         amdgpu_gfx_select_se_sh(adev, se, sh, cu);
3419
3420         x = 0;
3421         if (adev->gfx.funcs->read_wave_data)
3422                 adev->gfx.funcs->read_wave_data(adev, simd, wave, data, &x);
3423
3424         amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
3425         mutex_unlock(&adev->grbm_idx_mutex);
3426
3427         if (!x)
3428                 return -EINVAL;
3429
3430         while (size && (offset < x * 4)) {
3431                 uint32_t value;
3432
3433                 value = data[offset >> 2];
3434                 r = put_user(value, (uint32_t *)buf);
3435                 if (r)
3436                         return r;
3437
3438                 result += 4;
3439                 buf += 4;
3440                 offset += 4;
3441                 size -= 4;
3442         }
3443
3444         return result;
3445 }
3446
3447 static ssize_t amdgpu_debugfs_gpr_read(struct file *f, char __user *buf,
3448                                         size_t size, loff_t *pos)
3449 {
3450         struct amdgpu_device *adev = f->f_inode->i_private;
3451         int r;
3452         ssize_t result = 0;
3453         uint32_t offset, se, sh, cu, wave, simd, thread, bank, *data;
3454
3455         if (size & 3 || *pos & 3)
3456                 return -EINVAL;
3457
3458         /* decode offset */
3459         offset = (*pos & 0xFFF);       /* in dwords */
3460         se = ((*pos >> 12) & 0xFF);
3461         sh = ((*pos >> 20) & 0xFF);
3462         cu = ((*pos >> 28) & 0xFF);
3463         wave = ((*pos >> 36) & 0xFF);
3464         simd = ((*pos >> 44) & 0xFF);
3465         thread = ((*pos >> 52) & 0xFF);
3466         bank = ((*pos >> 60) & 1);
3467
3468         data = kmalloc_array(1024, sizeof(*data), GFP_KERNEL);
3469         if (!data)
3470                 return -ENOMEM;
3471
3472         /* switch to the specific se/sh/cu */
3473         mutex_lock(&adev->grbm_idx_mutex);
3474         amdgpu_gfx_select_se_sh(adev, se, sh, cu);
3475
3476         if (bank == 0) {
3477                 if (adev->gfx.funcs->read_wave_vgprs)
3478                         adev->gfx.funcs->read_wave_vgprs(adev, simd, wave, thread, offset, size>>2, data);
3479         } else {
3480                 if (adev->gfx.funcs->read_wave_sgprs)
3481                         adev->gfx.funcs->read_wave_sgprs(adev, simd, wave, offset, size>>2, data);
3482         }
3483
3484         amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
3485         mutex_unlock(&adev->grbm_idx_mutex);
3486
3487         while (size) {
3488                 uint32_t value;
3489
3490                 value = data[offset++];
3491                 r = put_user(value, (uint32_t *)buf);
3492                 if (r) {
3493                         result = r;
3494                         goto err;
3495                 }
3496
3497                 result += 4;
3498                 buf += 4;
3499                 size -= 4;
3500         }
3501
3502 err:
3503         kfree(data);
3504         return result;
3505 }
3506
3507 static const struct file_operations amdgpu_debugfs_regs_fops = {
3508         .owner = THIS_MODULE,
3509         .read = amdgpu_debugfs_regs_read,
3510         .write = amdgpu_debugfs_regs_write,
3511         .llseek = default_llseek
3512 };
3513 static const struct file_operations amdgpu_debugfs_regs_didt_fops = {
3514         .owner = THIS_MODULE,
3515         .read = amdgpu_debugfs_regs_didt_read,
3516         .write = amdgpu_debugfs_regs_didt_write,
3517         .llseek = default_llseek
3518 };
3519 static const struct file_operations amdgpu_debugfs_regs_pcie_fops = {
3520         .owner = THIS_MODULE,
3521         .read = amdgpu_debugfs_regs_pcie_read,
3522         .write = amdgpu_debugfs_regs_pcie_write,
3523         .llseek = default_llseek
3524 };
3525 static const struct file_operations amdgpu_debugfs_regs_smc_fops = {
3526         .owner = THIS_MODULE,
3527         .read = amdgpu_debugfs_regs_smc_read,
3528         .write = amdgpu_debugfs_regs_smc_write,
3529         .llseek = default_llseek
3530 };
3531
3532 static const struct file_operations amdgpu_debugfs_gca_config_fops = {
3533         .owner = THIS_MODULE,
3534         .read = amdgpu_debugfs_gca_config_read,
3535         .llseek = default_llseek
3536 };
3537
3538 static const struct file_operations amdgpu_debugfs_sensors_fops = {
3539         .owner = THIS_MODULE,
3540         .read = amdgpu_debugfs_sensor_read,
3541         .llseek = default_llseek
3542 };
3543
3544 static const struct file_operations amdgpu_debugfs_wave_fops = {
3545         .owner = THIS_MODULE,
3546         .read = amdgpu_debugfs_wave_read,
3547         .llseek = default_llseek
3548 };
3549 static const struct file_operations amdgpu_debugfs_gpr_fops = {
3550         .owner = THIS_MODULE,
3551         .read = amdgpu_debugfs_gpr_read,
3552         .llseek = default_llseek
3553 };
3554
3555 static const struct file_operations *debugfs_regs[] = {
3556         &amdgpu_debugfs_regs_fops,
3557         &amdgpu_debugfs_regs_didt_fops,
3558         &amdgpu_debugfs_regs_pcie_fops,
3559         &amdgpu_debugfs_regs_smc_fops,
3560         &amdgpu_debugfs_gca_config_fops,
3561         &amdgpu_debugfs_sensors_fops,
3562         &amdgpu_debugfs_wave_fops,
3563         &amdgpu_debugfs_gpr_fops,
3564 };
3565
3566 static const char *debugfs_regs_names[] = {
3567         "amdgpu_regs",
3568         "amdgpu_regs_didt",
3569         "amdgpu_regs_pcie",
3570         "amdgpu_regs_smc",
3571         "amdgpu_gca_config",
3572         "amdgpu_sensors",
3573         "amdgpu_wave",
3574         "amdgpu_gpr",
3575 };
3576
3577 static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
3578 {
3579         struct drm_minor *minor = adev->ddev->primary;
3580         struct dentry *ent, *root = minor->debugfs_root;
3581         unsigned i, j;
3582
3583         for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3584                 ent = debugfs_create_file(debugfs_regs_names[i],
3585                                           S_IFREG | S_IRUGO, root,
3586                                           adev, debugfs_regs[i]);
3587                 if (IS_ERR(ent)) {
3588                         for (j = 0; j < i; j++) {
3589                                 debugfs_remove(adev->debugfs_regs[i]);
3590                                 adev->debugfs_regs[i] = NULL;
3591                         }
3592                         return PTR_ERR(ent);
3593                 }
3594
3595                 if (!i)
3596                         i_size_write(ent->d_inode, adev->rmmio_size);
3597                 adev->debugfs_regs[i] = ent;
3598         }
3599
3600         return 0;
3601 }
3602
3603 static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev)
3604 {
3605         unsigned i;
3606
3607         for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3608                 if (adev->debugfs_regs[i]) {
3609                         debugfs_remove(adev->debugfs_regs[i]);
3610                         adev->debugfs_regs[i] = NULL;
3611                 }
3612         }
3613 }
3614
3615 int amdgpu_debugfs_init(struct drm_minor *minor)
3616 {
3617         return 0;
3618 }
3619 #else
3620 static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
3621 {
3622         return 0;
3623 }
3624 static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev) { }
3625 #endif
This page took 0.251803 seconds and 4 git commands to generate.