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