drm/amdgpu: add virtual dce support for iceland
[linux-2.6-block.git] / drivers / gpu / drm / amd / amdgpu / amdgpu_device.c
CommitLineData
d38ceaf9
AD
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 */
0875dc9e 28#include <linux/kthread.h>
d38ceaf9
AD
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"
f4b373f4 39#include "amdgpu_trace.h"
d38ceaf9
AD
40#include "amdgpu_i2c.h"
41#include "atom.h"
42#include "amdgpu_atombios.h"
d0dd7f0c 43#include "amd_pcie.h"
a2e73f56
AD
44#ifdef CONFIG_DRM_AMDGPU_CIK
45#include "cik.h"
46#endif
aaa36a97 47#include "vi.h"
d38ceaf9
AD
48#include "bif/bif_4_1_d.h"
49
50static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev);
51static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev);
52
53static const char *amdgpu_asic_name[] = {
54 "BONAIRE",
55 "KAVERI",
56 "KABINI",
57 "HAWAII",
58 "MULLINS",
59 "TOPAZ",
60 "TONGA",
48299f95 61 "FIJI",
d38ceaf9 62 "CARRIZO",
139f4917 63 "STONEY",
2cc0c0b5
FC
64 "POLARIS10",
65 "POLARIS11",
d38ceaf9
AD
66 "LAST",
67};
68
69bool amdgpu_device_is_px(struct drm_device *dev)
70{
71 struct amdgpu_device *adev = dev->dev_private;
72
2f7d10b3 73 if (adev->flags & AMD_IS_PX)
d38ceaf9
AD
74 return true;
75 return false;
76}
77
78/*
79 * MMIO register access helper functions.
80 */
81uint32_t amdgpu_mm_rreg(struct amdgpu_device *adev, uint32_t reg,
82 bool always_indirect)
83{
f4b373f4
TSD
84 uint32_t ret;
85
d38ceaf9 86 if ((reg * 4) < adev->rmmio_size && !always_indirect)
f4b373f4 87 ret = readl(((void __iomem *)adev->rmmio) + (reg * 4));
d38ceaf9
AD
88 else {
89 unsigned long flags;
d38ceaf9
AD
90
91 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
92 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
93 ret = readl(((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
94 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
d38ceaf9 95 }
f4b373f4
TSD
96 trace_amdgpu_mm_rreg(adev->pdev->device, reg, ret);
97 return ret;
d38ceaf9
AD
98}
99
100void amdgpu_mm_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v,
101 bool always_indirect)
102{
f4b373f4
TSD
103 trace_amdgpu_mm_wreg(adev->pdev->device, reg, v);
104
d38ceaf9
AD
105 if ((reg * 4) < adev->rmmio_size && !always_indirect)
106 writel(v, ((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 writel(v, ((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
113 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
114 }
115}
116
117u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 reg)
118{
119 if ((reg * 4) < adev->rio_mem_size)
120 return ioread32(adev->rio_mem + (reg * 4));
121 else {
122 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
123 return ioread32(adev->rio_mem + (mmMM_DATA * 4));
124 }
125}
126
127void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
128{
129
130 if ((reg * 4) < adev->rio_mem_size)
131 iowrite32(v, adev->rio_mem + (reg * 4));
132 else {
133 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
134 iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
135 }
136}
137
138/**
139 * amdgpu_mm_rdoorbell - read a doorbell dword
140 *
141 * @adev: amdgpu_device pointer
142 * @index: doorbell index
143 *
144 * Returns the value in the doorbell aperture at the
145 * requested doorbell index (CIK).
146 */
147u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
148{
149 if (index < adev->doorbell.num_doorbells) {
150 return readl(adev->doorbell.ptr + index);
151 } else {
152 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
153 return 0;
154 }
155}
156
157/**
158 * amdgpu_mm_wdoorbell - write a doorbell dword
159 *
160 * @adev: amdgpu_device pointer
161 * @index: doorbell index
162 * @v: value to write
163 *
164 * Writes @v to the doorbell aperture at the
165 * requested doorbell index (CIK).
166 */
167void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
168{
169 if (index < adev->doorbell.num_doorbells) {
170 writel(v, adev->doorbell.ptr + index);
171 } else {
172 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
173 }
174}
175
176/**
177 * amdgpu_invalid_rreg - dummy reg read function
178 *
179 * @adev: amdgpu device pointer
180 * @reg: offset of register
181 *
182 * Dummy register read function. Used for register blocks
183 * that certain asics don't have (all asics).
184 * Returns the value in the register.
185 */
186static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
187{
188 DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
189 BUG();
190 return 0;
191}
192
193/**
194 * amdgpu_invalid_wreg - dummy reg write function
195 *
196 * @adev: amdgpu device pointer
197 * @reg: offset of register
198 * @v: value to write to the register
199 *
200 * Dummy register read function. Used for register blocks
201 * that certain asics don't have (all asics).
202 */
203static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
204{
205 DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
206 reg, v);
207 BUG();
208}
209
210/**
211 * amdgpu_block_invalid_rreg - dummy reg read function
212 *
213 * @adev: amdgpu device pointer
214 * @block: offset of instance
215 * @reg: offset of register
216 *
217 * Dummy register read function. Used for register blocks
218 * that certain asics don't have (all asics).
219 * Returns the value in the register.
220 */
221static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
222 uint32_t block, uint32_t reg)
223{
224 DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
225 reg, block);
226 BUG();
227 return 0;
228}
229
230/**
231 * amdgpu_block_invalid_wreg - dummy reg write function
232 *
233 * @adev: amdgpu device pointer
234 * @block: offset of instance
235 * @reg: offset of register
236 * @v: value to write to the register
237 *
238 * Dummy register read function. Used for register blocks
239 * that certain asics don't have (all asics).
240 */
241static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
242 uint32_t block,
243 uint32_t reg, uint32_t v)
244{
245 DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
246 reg, block, v);
247 BUG();
248}
249
250static int amdgpu_vram_scratch_init(struct amdgpu_device *adev)
251{
252 int r;
253
254 if (adev->vram_scratch.robj == NULL) {
255 r = amdgpu_bo_create(adev, AMDGPU_GPU_PAGE_SIZE,
857d913d
AD
256 PAGE_SIZE, true, AMDGPU_GEM_DOMAIN_VRAM,
257 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED,
72d7668b 258 NULL, NULL, &adev->vram_scratch.robj);
d38ceaf9
AD
259 if (r) {
260 return r;
261 }
262 }
263
264 r = amdgpu_bo_reserve(adev->vram_scratch.robj, false);
265 if (unlikely(r != 0))
266 return r;
267 r = amdgpu_bo_pin(adev->vram_scratch.robj,
268 AMDGPU_GEM_DOMAIN_VRAM, &adev->vram_scratch.gpu_addr);
269 if (r) {
270 amdgpu_bo_unreserve(adev->vram_scratch.robj);
271 return r;
272 }
273 r = amdgpu_bo_kmap(adev->vram_scratch.robj,
274 (void **)&adev->vram_scratch.ptr);
275 if (r)
276 amdgpu_bo_unpin(adev->vram_scratch.robj);
277 amdgpu_bo_unreserve(adev->vram_scratch.robj);
278
279 return r;
280}
281
282static void amdgpu_vram_scratch_fini(struct amdgpu_device *adev)
283{
284 int r;
285
286 if (adev->vram_scratch.robj == NULL) {
287 return;
288 }
289 r = amdgpu_bo_reserve(adev->vram_scratch.robj, false);
290 if (likely(r == 0)) {
291 amdgpu_bo_kunmap(adev->vram_scratch.robj);
292 amdgpu_bo_unpin(adev->vram_scratch.robj);
293 amdgpu_bo_unreserve(adev->vram_scratch.robj);
294 }
295 amdgpu_bo_unref(&adev->vram_scratch.robj);
296}
297
298/**
299 * amdgpu_program_register_sequence - program an array of registers.
300 *
301 * @adev: amdgpu_device pointer
302 * @registers: pointer to the register array
303 * @array_size: size of the register array
304 *
305 * Programs an array or registers with and and or masks.
306 * This is a helper for setting golden registers.
307 */
308void amdgpu_program_register_sequence(struct amdgpu_device *adev,
309 const u32 *registers,
310 const u32 array_size)
311{
312 u32 tmp, reg, and_mask, or_mask;
313 int i;
314
315 if (array_size % 3)
316 return;
317
318 for (i = 0; i < array_size; i +=3) {
319 reg = registers[i + 0];
320 and_mask = registers[i + 1];
321 or_mask = registers[i + 2];
322
323 if (and_mask == 0xffffffff) {
324 tmp = or_mask;
325 } else {
326 tmp = RREG32(reg);
327 tmp &= ~and_mask;
328 tmp |= or_mask;
329 }
330 WREG32(reg, tmp);
331 }
332}
333
334void amdgpu_pci_config_reset(struct amdgpu_device *adev)
335{
336 pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
337}
338
339/*
340 * GPU doorbell aperture helpers function.
341 */
342/**
343 * amdgpu_doorbell_init - Init doorbell driver information.
344 *
345 * @adev: amdgpu_device pointer
346 *
347 * Init doorbell driver information (CIK)
348 * Returns 0 on success, error on failure.
349 */
350static int amdgpu_doorbell_init(struct amdgpu_device *adev)
351{
352 /* doorbell bar mapping */
353 adev->doorbell.base = pci_resource_start(adev->pdev, 2);
354 adev->doorbell.size = pci_resource_len(adev->pdev, 2);
355
edf600da 356 adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
d38ceaf9
AD
357 AMDGPU_DOORBELL_MAX_ASSIGNMENT+1);
358 if (adev->doorbell.num_doorbells == 0)
359 return -EINVAL;
360
361 adev->doorbell.ptr = ioremap(adev->doorbell.base, adev->doorbell.num_doorbells * sizeof(u32));
362 if (adev->doorbell.ptr == NULL) {
363 return -ENOMEM;
364 }
365 DRM_INFO("doorbell mmio base: 0x%08X\n", (uint32_t)adev->doorbell.base);
366 DRM_INFO("doorbell mmio size: %u\n", (unsigned)adev->doorbell.size);
367
368 return 0;
369}
370
371/**
372 * amdgpu_doorbell_fini - Tear down doorbell driver information.
373 *
374 * @adev: amdgpu_device pointer
375 *
376 * Tear down doorbell driver information (CIK)
377 */
378static void amdgpu_doorbell_fini(struct amdgpu_device *adev)
379{
380 iounmap(adev->doorbell.ptr);
381 adev->doorbell.ptr = NULL;
382}
383
384/**
385 * amdgpu_doorbell_get_kfd_info - Report doorbell configuration required to
386 * setup amdkfd
387 *
388 * @adev: amdgpu_device pointer
389 * @aperture_base: output returning doorbell aperture base physical address
390 * @aperture_size: output returning doorbell aperture size in bytes
391 * @start_offset: output returning # of doorbell bytes reserved for amdgpu.
392 *
393 * amdgpu and amdkfd share the doorbell aperture. amdgpu sets it up,
394 * takes doorbells required for its own rings and reports the setup to amdkfd.
395 * amdgpu reserved doorbells are at the start of the doorbell aperture.
396 */
397void amdgpu_doorbell_get_kfd_info(struct amdgpu_device *adev,
398 phys_addr_t *aperture_base,
399 size_t *aperture_size,
400 size_t *start_offset)
401{
402 /*
403 * The first num_doorbells are used by amdgpu.
404 * amdkfd takes whatever's left in the aperture.
405 */
406 if (adev->doorbell.size > adev->doorbell.num_doorbells * sizeof(u32)) {
407 *aperture_base = adev->doorbell.base;
408 *aperture_size = adev->doorbell.size;
409 *start_offset = adev->doorbell.num_doorbells * sizeof(u32);
410 } else {
411 *aperture_base = 0;
412 *aperture_size = 0;
413 *start_offset = 0;
414 }
415}
416
417/*
418 * amdgpu_wb_*()
419 * Writeback is the the method by which the the GPU updates special pages
420 * in memory with the status of certain GPU events (fences, ring pointers,
421 * etc.).
422 */
423
424/**
425 * amdgpu_wb_fini - Disable Writeback and free memory
426 *
427 * @adev: amdgpu_device pointer
428 *
429 * Disables Writeback and frees the Writeback memory (all asics).
430 * Used at driver shutdown.
431 */
432static void amdgpu_wb_fini(struct amdgpu_device *adev)
433{
434 if (adev->wb.wb_obj) {
435 if (!amdgpu_bo_reserve(adev->wb.wb_obj, false)) {
436 amdgpu_bo_kunmap(adev->wb.wb_obj);
437 amdgpu_bo_unpin(adev->wb.wb_obj);
438 amdgpu_bo_unreserve(adev->wb.wb_obj);
439 }
440 amdgpu_bo_unref(&adev->wb.wb_obj);
441 adev->wb.wb = NULL;
442 adev->wb.wb_obj = NULL;
443 }
444}
445
446/**
447 * amdgpu_wb_init- Init Writeback driver info and allocate memory
448 *
449 * @adev: amdgpu_device pointer
450 *
451 * Disables Writeback and frees the Writeback memory (all asics).
452 * Used at driver startup.
453 * Returns 0 on success or an -error on failure.
454 */
455static int amdgpu_wb_init(struct amdgpu_device *adev)
456{
457 int r;
458
459 if (adev->wb.wb_obj == NULL) {
460 r = amdgpu_bo_create(adev, AMDGPU_MAX_WB * 4, PAGE_SIZE, true,
72d7668b
CK
461 AMDGPU_GEM_DOMAIN_GTT, 0, NULL, NULL,
462 &adev->wb.wb_obj);
d38ceaf9
AD
463 if (r) {
464 dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
465 return r;
466 }
467 r = amdgpu_bo_reserve(adev->wb.wb_obj, false);
468 if (unlikely(r != 0)) {
469 amdgpu_wb_fini(adev);
470 return r;
471 }
472 r = amdgpu_bo_pin(adev->wb.wb_obj, AMDGPU_GEM_DOMAIN_GTT,
473 &adev->wb.gpu_addr);
474 if (r) {
475 amdgpu_bo_unreserve(adev->wb.wb_obj);
476 dev_warn(adev->dev, "(%d) pin WB bo failed\n", r);
477 amdgpu_wb_fini(adev);
478 return r;
479 }
480 r = amdgpu_bo_kmap(adev->wb.wb_obj, (void **)&adev->wb.wb);
481 amdgpu_bo_unreserve(adev->wb.wb_obj);
482 if (r) {
483 dev_warn(adev->dev, "(%d) map WB bo failed\n", r);
484 amdgpu_wb_fini(adev);
485 return r;
486 }
487
488 adev->wb.num_wb = AMDGPU_MAX_WB;
489 memset(&adev->wb.used, 0, sizeof(adev->wb.used));
490
491 /* clear wb memory */
492 memset((char *)adev->wb.wb, 0, AMDGPU_GPU_PAGE_SIZE);
493 }
494
495 return 0;
496}
497
498/**
499 * amdgpu_wb_get - Allocate a wb entry
500 *
501 * @adev: amdgpu_device pointer
502 * @wb: wb index
503 *
504 * Allocate a wb slot for use by the driver (all asics).
505 * Returns 0 on success or -EINVAL on failure.
506 */
507int amdgpu_wb_get(struct amdgpu_device *adev, u32 *wb)
508{
509 unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
510 if (offset < adev->wb.num_wb) {
511 __set_bit(offset, adev->wb.used);
512 *wb = offset;
513 return 0;
514 } else {
515 return -EINVAL;
516 }
517}
518
519/**
520 * amdgpu_wb_free - Free a wb entry
521 *
522 * @adev: amdgpu_device pointer
523 * @wb: wb index
524 *
525 * Free a wb slot allocated for use by the driver (all asics)
526 */
527void amdgpu_wb_free(struct amdgpu_device *adev, u32 wb)
528{
529 if (wb < adev->wb.num_wb)
530 __clear_bit(wb, adev->wb.used);
531}
532
533/**
534 * amdgpu_vram_location - try to find VRAM location
535 * @adev: amdgpu device structure holding all necessary informations
536 * @mc: memory controller structure holding memory informations
537 * @base: base address at which to put VRAM
538 *
539 * Function will place try to place VRAM at base address provided
540 * as parameter (which is so far either PCI aperture address or
541 * for IGP TOM base address).
542 *
543 * If there is not enough space to fit the unvisible VRAM in the 32bits
544 * address space then we limit the VRAM size to the aperture.
545 *
546 * Note: We don't explicitly enforce VRAM start to be aligned on VRAM size,
547 * this shouldn't be a problem as we are using the PCI aperture as a reference.
548 * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but
549 * not IGP.
550 *
551 * Note: we use mc_vram_size as on some board we need to program the mc to
552 * cover the whole aperture even if VRAM size is inferior to aperture size
553 * Novell bug 204882 + along with lots of ubuntu ones
554 *
555 * Note: when limiting vram it's safe to overwritte real_vram_size because
556 * we are not in case where real_vram_size is inferior to mc_vram_size (ie
557 * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu
558 * ones)
559 *
560 * Note: IGP TOM addr should be the same as the aperture addr, we don't
561 * explicitly check for that thought.
562 *
563 * FIXME: when reducing VRAM size align new size on power of 2.
564 */
565void amdgpu_vram_location(struct amdgpu_device *adev, struct amdgpu_mc *mc, u64 base)
566{
567 uint64_t limit = (uint64_t)amdgpu_vram_limit << 20;
568
569 mc->vram_start = base;
570 if (mc->mc_vram_size > (adev->mc.mc_mask - base + 1)) {
571 dev_warn(adev->dev, "limiting VRAM to PCI aperture size\n");
572 mc->real_vram_size = mc->aper_size;
573 mc->mc_vram_size = mc->aper_size;
574 }
575 mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
576 if (limit && limit < mc->real_vram_size)
577 mc->real_vram_size = limit;
578 dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
579 mc->mc_vram_size >> 20, mc->vram_start,
580 mc->vram_end, mc->real_vram_size >> 20);
581}
582
583/**
584 * amdgpu_gtt_location - try to find GTT location
585 * @adev: amdgpu device structure holding all necessary informations
586 * @mc: memory controller structure holding memory informations
587 *
588 * Function will place try to place GTT before or after VRAM.
589 *
590 * If GTT size is bigger than space left then we ajust GTT size.
591 * Thus function will never fails.
592 *
593 * FIXME: when reducing GTT size align new size on power of 2.
594 */
595void amdgpu_gtt_location(struct amdgpu_device *adev, struct amdgpu_mc *mc)
596{
597 u64 size_af, size_bf;
598
599 size_af = ((adev->mc.mc_mask - mc->vram_end) + mc->gtt_base_align) & ~mc->gtt_base_align;
600 size_bf = mc->vram_start & ~mc->gtt_base_align;
601 if (size_bf > size_af) {
602 if (mc->gtt_size > size_bf) {
603 dev_warn(adev->dev, "limiting GTT\n");
604 mc->gtt_size = size_bf;
605 }
606 mc->gtt_start = (mc->vram_start & ~mc->gtt_base_align) - mc->gtt_size;
607 } else {
608 if (mc->gtt_size > size_af) {
609 dev_warn(adev->dev, "limiting GTT\n");
610 mc->gtt_size = size_af;
611 }
612 mc->gtt_start = (mc->vram_end + 1 + mc->gtt_base_align) & ~mc->gtt_base_align;
613 }
614 mc->gtt_end = mc->gtt_start + mc->gtt_size - 1;
615 dev_info(adev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
616 mc->gtt_size >> 20, mc->gtt_start, mc->gtt_end);
617}
618
619/*
620 * GPU helpers function.
621 */
622/**
623 * amdgpu_card_posted - check if the hw has already been initialized
624 *
625 * @adev: amdgpu_device pointer
626 *
627 * Check if the asic has been initialized (all asics).
628 * Used at driver startup.
629 * Returns true if initialized or false if not.
630 */
631bool amdgpu_card_posted(struct amdgpu_device *adev)
632{
633 uint32_t reg;
634
635 /* then check MEM_SIZE, in case the crtcs are off */
636 reg = RREG32(mmCONFIG_MEMSIZE);
637
638 if (reg)
639 return true;
640
641 return false;
642
643}
644
d38ceaf9
AD
645/**
646 * amdgpu_dummy_page_init - init dummy page used by the driver
647 *
648 * @adev: amdgpu_device pointer
649 *
650 * Allocate the dummy page used by the driver (all asics).
651 * This dummy page is used by the driver as a filler for gart entries
652 * when pages are taken out of the GART
653 * Returns 0 on sucess, -ENOMEM on failure.
654 */
655int amdgpu_dummy_page_init(struct amdgpu_device *adev)
656{
657 if (adev->dummy_page.page)
658 return 0;
659 adev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
660 if (adev->dummy_page.page == NULL)
661 return -ENOMEM;
662 adev->dummy_page.addr = pci_map_page(adev->pdev, adev->dummy_page.page,
663 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
664 if (pci_dma_mapping_error(adev->pdev, adev->dummy_page.addr)) {
665 dev_err(&adev->pdev->dev, "Failed to DMA MAP the dummy page\n");
666 __free_page(adev->dummy_page.page);
667 adev->dummy_page.page = NULL;
668 return -ENOMEM;
669 }
670 return 0;
671}
672
673/**
674 * amdgpu_dummy_page_fini - free dummy page used by the driver
675 *
676 * @adev: amdgpu_device pointer
677 *
678 * Frees the dummy page used by the driver (all asics).
679 */
680void amdgpu_dummy_page_fini(struct amdgpu_device *adev)
681{
682 if (adev->dummy_page.page == NULL)
683 return;
684 pci_unmap_page(adev->pdev, adev->dummy_page.addr,
685 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
686 __free_page(adev->dummy_page.page);
687 adev->dummy_page.page = NULL;
688}
689
690
691/* ATOM accessor methods */
692/*
693 * ATOM is an interpreted byte code stored in tables in the vbios. The
694 * driver registers callbacks to access registers and the interpreter
695 * in the driver parses the tables and executes then to program specific
696 * actions (set display modes, asic init, etc.). See amdgpu_atombios.c,
697 * atombios.h, and atom.c
698 */
699
700/**
701 * cail_pll_read - read PLL register
702 *
703 * @info: atom card_info pointer
704 * @reg: PLL register offset
705 *
706 * Provides a PLL register accessor for the atom interpreter (r4xx+).
707 * Returns the value of the PLL register.
708 */
709static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
710{
711 return 0;
712}
713
714/**
715 * cail_pll_write - write PLL register
716 *
717 * @info: atom card_info pointer
718 * @reg: PLL register offset
719 * @val: value to write to the pll register
720 *
721 * Provides a PLL register accessor for the atom interpreter (r4xx+).
722 */
723static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
724{
725
726}
727
728/**
729 * cail_mc_read - read MC (Memory Controller) register
730 *
731 * @info: atom card_info pointer
732 * @reg: MC register offset
733 *
734 * Provides an MC register accessor for the atom interpreter (r4xx+).
735 * Returns the value of the MC register.
736 */
737static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
738{
739 return 0;
740}
741
742/**
743 * cail_mc_write - write MC (Memory Controller) register
744 *
745 * @info: atom card_info pointer
746 * @reg: MC register offset
747 * @val: value to write to the pll register
748 *
749 * Provides a MC register accessor for the atom interpreter (r4xx+).
750 */
751static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
752{
753
754}
755
756/**
757 * cail_reg_write - write MMIO register
758 *
759 * @info: atom card_info pointer
760 * @reg: MMIO register offset
761 * @val: value to write to the pll register
762 *
763 * Provides a MMIO register accessor for the atom interpreter (r4xx+).
764 */
765static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
766{
767 struct amdgpu_device *adev = info->dev->dev_private;
768
769 WREG32(reg, val);
770}
771
772/**
773 * cail_reg_read - read MMIO register
774 *
775 * @info: atom card_info pointer
776 * @reg: MMIO register offset
777 *
778 * Provides an MMIO register accessor for the atom interpreter (r4xx+).
779 * Returns the value of the MMIO register.
780 */
781static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
782{
783 struct amdgpu_device *adev = info->dev->dev_private;
784 uint32_t r;
785
786 r = RREG32(reg);
787 return r;
788}
789
790/**
791 * cail_ioreg_write - write IO register
792 *
793 * @info: atom card_info pointer
794 * @reg: IO register offset
795 * @val: value to write to the pll register
796 *
797 * Provides a IO register accessor for the atom interpreter (r4xx+).
798 */
799static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
800{
801 struct amdgpu_device *adev = info->dev->dev_private;
802
803 WREG32_IO(reg, val);
804}
805
806/**
807 * cail_ioreg_read - read IO register
808 *
809 * @info: atom card_info pointer
810 * @reg: IO register offset
811 *
812 * Provides an IO register accessor for the atom interpreter (r4xx+).
813 * Returns the value of the IO register.
814 */
815static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
816{
817 struct amdgpu_device *adev = info->dev->dev_private;
818 uint32_t r;
819
820 r = RREG32_IO(reg);
821 return r;
822}
823
824/**
825 * amdgpu_atombios_fini - free the driver info and callbacks for atombios
826 *
827 * @adev: amdgpu_device pointer
828 *
829 * Frees the driver info and register access callbacks for the ATOM
830 * interpreter (r4xx+).
831 * Called at driver shutdown.
832 */
833static void amdgpu_atombios_fini(struct amdgpu_device *adev)
834{
89e0ec9f 835 if (adev->mode_info.atom_context) {
d38ceaf9 836 kfree(adev->mode_info.atom_context->scratch);
89e0ec9f
ML
837 kfree(adev->mode_info.atom_context->iio);
838 }
d38ceaf9
AD
839 kfree(adev->mode_info.atom_context);
840 adev->mode_info.atom_context = NULL;
841 kfree(adev->mode_info.atom_card_info);
842 adev->mode_info.atom_card_info = NULL;
843}
844
845/**
846 * amdgpu_atombios_init - init the driver info and callbacks for atombios
847 *
848 * @adev: amdgpu_device pointer
849 *
850 * Initializes the driver info and register access callbacks for the
851 * ATOM interpreter (r4xx+).
852 * Returns 0 on sucess, -ENOMEM on failure.
853 * Called at driver startup.
854 */
855static int amdgpu_atombios_init(struct amdgpu_device *adev)
856{
857 struct card_info *atom_card_info =
858 kzalloc(sizeof(struct card_info), GFP_KERNEL);
859
860 if (!atom_card_info)
861 return -ENOMEM;
862
863 adev->mode_info.atom_card_info = atom_card_info;
864 atom_card_info->dev = adev->ddev;
865 atom_card_info->reg_read = cail_reg_read;
866 atom_card_info->reg_write = cail_reg_write;
867 /* needed for iio ops */
868 if (adev->rio_mem) {
869 atom_card_info->ioreg_read = cail_ioreg_read;
870 atom_card_info->ioreg_write = cail_ioreg_write;
871 } else {
872 DRM_ERROR("Unable to find PCI I/O BAR; using MMIO for ATOM IIO\n");
873 atom_card_info->ioreg_read = cail_reg_read;
874 atom_card_info->ioreg_write = cail_reg_write;
875 }
876 atom_card_info->mc_read = cail_mc_read;
877 atom_card_info->mc_write = cail_mc_write;
878 atom_card_info->pll_read = cail_pll_read;
879 atom_card_info->pll_write = cail_pll_write;
880
881 adev->mode_info.atom_context = amdgpu_atom_parse(atom_card_info, adev->bios);
882 if (!adev->mode_info.atom_context) {
883 amdgpu_atombios_fini(adev);
884 return -ENOMEM;
885 }
886
887 mutex_init(&adev->mode_info.atom_context->mutex);
888 amdgpu_atombios_scratch_regs_init(adev);
889 amdgpu_atom_allocate_fb_scratch(adev->mode_info.atom_context);
890 return 0;
891}
892
893/* if we get transitioned to only one device, take VGA back */
894/**
895 * amdgpu_vga_set_decode - enable/disable vga decode
896 *
897 * @cookie: amdgpu_device pointer
898 * @state: enable/disable vga decode
899 *
900 * Enable/disable vga decode (all asics).
901 * Returns VGA resource flags.
902 */
903static unsigned int amdgpu_vga_set_decode(void *cookie, bool state)
904{
905 struct amdgpu_device *adev = cookie;
906 amdgpu_asic_set_vga_state(adev, state);
907 if (state)
908 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
909 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
910 else
911 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
912}
913
914/**
915 * amdgpu_check_pot_argument - check that argument is a power of two
916 *
917 * @arg: value to check
918 *
919 * Validates that a certain argument is a power of two (all asics).
920 * Returns true if argument is valid.
921 */
922static bool amdgpu_check_pot_argument(int arg)
923{
924 return (arg & (arg - 1)) == 0;
925}
926
927/**
928 * amdgpu_check_arguments - validate module params
929 *
930 * @adev: amdgpu_device pointer
931 *
932 * Validates certain module parameters and updates
933 * the associated values used by the driver (all asics).
934 */
935static void amdgpu_check_arguments(struct amdgpu_device *adev)
936{
5b011235
CZ
937 if (amdgpu_sched_jobs < 4) {
938 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
939 amdgpu_sched_jobs);
940 amdgpu_sched_jobs = 4;
941 } else if (!amdgpu_check_pot_argument(amdgpu_sched_jobs)){
942 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
943 amdgpu_sched_jobs);
944 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
945 }
d38ceaf9
AD
946
947 if (amdgpu_gart_size != -1) {
c4e1a13a 948 /* gtt size must be greater or equal to 32M */
d38ceaf9
AD
949 if (amdgpu_gart_size < 32) {
950 dev_warn(adev->dev, "gart size (%d) too small\n",
951 amdgpu_gart_size);
952 amdgpu_gart_size = -1;
d38ceaf9
AD
953 }
954 }
955
956 if (!amdgpu_check_pot_argument(amdgpu_vm_size)) {
957 dev_warn(adev->dev, "VM size (%d) must be a power of 2\n",
958 amdgpu_vm_size);
8dacc127 959 amdgpu_vm_size = 8;
d38ceaf9
AD
960 }
961
962 if (amdgpu_vm_size < 1) {
963 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
964 amdgpu_vm_size);
8dacc127 965 amdgpu_vm_size = 8;
d38ceaf9
AD
966 }
967
968 /*
969 * Max GPUVM size for Cayman, SI and CI are 40 bits.
970 */
971 if (amdgpu_vm_size > 1024) {
972 dev_warn(adev->dev, "VM size (%d) too large, max is 1TB\n",
973 amdgpu_vm_size);
8dacc127 974 amdgpu_vm_size = 8;
d38ceaf9
AD
975 }
976
977 /* defines number of bits in page table versus page directory,
978 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
979 * page table and the remaining bits are in the page directory */
980 if (amdgpu_vm_block_size == -1) {
981
982 /* Total bits covered by PD + PTs */
983 unsigned bits = ilog2(amdgpu_vm_size) + 18;
984
985 /* Make sure the PD is 4K in size up to 8GB address space.
986 Above that split equal between PD and PTs */
987 if (amdgpu_vm_size <= 8)
988 amdgpu_vm_block_size = bits - 9;
989 else
990 amdgpu_vm_block_size = (bits + 3) / 2;
991
992 } else if (amdgpu_vm_block_size < 9) {
993 dev_warn(adev->dev, "VM page table size (%d) too small\n",
994 amdgpu_vm_block_size);
995 amdgpu_vm_block_size = 9;
996 }
997
998 if (amdgpu_vm_block_size > 24 ||
999 (amdgpu_vm_size * 1024) < (1ull << amdgpu_vm_block_size)) {
1000 dev_warn(adev->dev, "VM page table size (%d) too large\n",
1001 amdgpu_vm_block_size);
1002 amdgpu_vm_block_size = 9;
1003 }
1004}
1005
1006/**
1007 * amdgpu_switcheroo_set_state - set switcheroo state
1008 *
1009 * @pdev: pci dev pointer
1694467b 1010 * @state: vga_switcheroo state
d38ceaf9
AD
1011 *
1012 * Callback for the switcheroo driver. Suspends or resumes the
1013 * the asics before or after it is powered up using ACPI methods.
1014 */
1015static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1016{
1017 struct drm_device *dev = pci_get_drvdata(pdev);
1018
1019 if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
1020 return;
1021
1022 if (state == VGA_SWITCHEROO_ON) {
1023 unsigned d3_delay = dev->pdev->d3_delay;
1024
1025 printk(KERN_INFO "amdgpu: switched on\n");
1026 /* don't suspend or resume card normally */
1027 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1028
1029 amdgpu_resume_kms(dev, true, true);
1030
1031 dev->pdev->d3_delay = d3_delay;
1032
1033 dev->switch_power_state = DRM_SWITCH_POWER_ON;
1034 drm_kms_helper_poll_enable(dev);
1035 } else {
1036 printk(KERN_INFO "amdgpu: switched off\n");
1037 drm_kms_helper_poll_disable(dev);
1038 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1039 amdgpu_suspend_kms(dev, true, true);
1040 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1041 }
1042}
1043
1044/**
1045 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1046 *
1047 * @pdev: pci dev pointer
1048 *
1049 * Callback for the switcheroo driver. Check of the switcheroo
1050 * state can be changed.
1051 * Returns true if the state can be changed, false if not.
1052 */
1053static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1054{
1055 struct drm_device *dev = pci_get_drvdata(pdev);
1056
1057 /*
1058 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1059 * locking inversion with the driver load path. And the access here is
1060 * completely racy anyway. So don't bother with locking for now.
1061 */
1062 return dev->open_count == 0;
1063}
1064
1065static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1066 .set_gpu_state = amdgpu_switcheroo_set_state,
1067 .reprobe = NULL,
1068 .can_switch = amdgpu_switcheroo_can_switch,
1069};
1070
1071int amdgpu_set_clockgating_state(struct amdgpu_device *adev,
5fc3aeeb 1072 enum amd_ip_block_type block_type,
1073 enum amd_clockgating_state state)
d38ceaf9
AD
1074{
1075 int i, r = 0;
1076
1077 for (i = 0; i < adev->num_ip_blocks; i++) {
9ecbe7f5
AD
1078 if (!adev->ip_block_status[i].valid)
1079 continue;
d38ceaf9 1080 if (adev->ip_blocks[i].type == block_type) {
5fc3aeeb 1081 r = adev->ip_blocks[i].funcs->set_clockgating_state((void *)adev,
d38ceaf9
AD
1082 state);
1083 if (r)
1084 return r;
a225bf1c 1085 break;
d38ceaf9
AD
1086 }
1087 }
1088 return r;
1089}
1090
1091int amdgpu_set_powergating_state(struct amdgpu_device *adev,
5fc3aeeb 1092 enum amd_ip_block_type block_type,
1093 enum amd_powergating_state state)
d38ceaf9
AD
1094{
1095 int i, r = 0;
1096
1097 for (i = 0; i < adev->num_ip_blocks; i++) {
9ecbe7f5
AD
1098 if (!adev->ip_block_status[i].valid)
1099 continue;
d38ceaf9 1100 if (adev->ip_blocks[i].type == block_type) {
5fc3aeeb 1101 r = adev->ip_blocks[i].funcs->set_powergating_state((void *)adev,
d38ceaf9
AD
1102 state);
1103 if (r)
1104 return r;
a225bf1c 1105 break;
d38ceaf9
AD
1106 }
1107 }
1108 return r;
1109}
1110
5dbbb60b
AD
1111int amdgpu_wait_for_idle(struct amdgpu_device *adev,
1112 enum amd_ip_block_type block_type)
1113{
1114 int i, r;
1115
1116 for (i = 0; i < adev->num_ip_blocks; i++) {
9ecbe7f5
AD
1117 if (!adev->ip_block_status[i].valid)
1118 continue;
5dbbb60b
AD
1119 if (adev->ip_blocks[i].type == block_type) {
1120 r = adev->ip_blocks[i].funcs->wait_for_idle((void *)adev);
1121 if (r)
1122 return r;
1123 break;
1124 }
1125 }
1126 return 0;
1127
1128}
1129
1130bool amdgpu_is_idle(struct amdgpu_device *adev,
1131 enum amd_ip_block_type block_type)
1132{
1133 int i;
1134
1135 for (i = 0; i < adev->num_ip_blocks; i++) {
9ecbe7f5
AD
1136 if (!adev->ip_block_status[i].valid)
1137 continue;
5dbbb60b
AD
1138 if (adev->ip_blocks[i].type == block_type)
1139 return adev->ip_blocks[i].funcs->is_idle((void *)adev);
1140 }
1141 return true;
1142
1143}
1144
d38ceaf9
AD
1145const struct amdgpu_ip_block_version * amdgpu_get_ip_block(
1146 struct amdgpu_device *adev,
5fc3aeeb 1147 enum amd_ip_block_type type)
d38ceaf9
AD
1148{
1149 int i;
1150
1151 for (i = 0; i < adev->num_ip_blocks; i++)
1152 if (adev->ip_blocks[i].type == type)
1153 return &adev->ip_blocks[i];
1154
1155 return NULL;
1156}
1157
1158/**
1159 * amdgpu_ip_block_version_cmp
1160 *
1161 * @adev: amdgpu_device pointer
5fc3aeeb 1162 * @type: enum amd_ip_block_type
d38ceaf9
AD
1163 * @major: major version
1164 * @minor: minor version
1165 *
1166 * return 0 if equal or greater
1167 * return 1 if smaller or the ip_block doesn't exist
1168 */
1169int amdgpu_ip_block_version_cmp(struct amdgpu_device *adev,
5fc3aeeb 1170 enum amd_ip_block_type type,
d38ceaf9
AD
1171 u32 major, u32 minor)
1172{
1173 const struct amdgpu_ip_block_version *ip_block;
1174 ip_block = amdgpu_get_ip_block(adev, type);
1175
1176 if (ip_block && ((ip_block->major > major) ||
1177 ((ip_block->major == major) &&
1178 (ip_block->minor >= minor))))
1179 return 0;
1180
1181 return 1;
1182}
1183
1184static int amdgpu_early_init(struct amdgpu_device *adev)
1185{
aaa36a97 1186 int i, r;
d38ceaf9 1187
a6be7570
ED
1188 DRM_INFO("virtual display enabled:%d\n", amdgpu_virtual_display);
1189
d38ceaf9 1190 switch (adev->asic_type) {
aaa36a97
AD
1191 case CHIP_TOPAZ:
1192 case CHIP_TONGA:
48299f95 1193 case CHIP_FIJI:
2cc0c0b5
FC
1194 case CHIP_POLARIS11:
1195 case CHIP_POLARIS10:
aaa36a97 1196 case CHIP_CARRIZO:
39bb0c92
SL
1197 case CHIP_STONEY:
1198 if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
aaa36a97
AD
1199 adev->family = AMDGPU_FAMILY_CZ;
1200 else
1201 adev->family = AMDGPU_FAMILY_VI;
1202
1203 r = vi_set_ip_blocks(adev);
1204 if (r)
1205 return r;
1206 break;
a2e73f56
AD
1207#ifdef CONFIG_DRM_AMDGPU_CIK
1208 case CHIP_BONAIRE:
1209 case CHIP_HAWAII:
1210 case CHIP_KAVERI:
1211 case CHIP_KABINI:
1212 case CHIP_MULLINS:
1213 if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
1214 adev->family = AMDGPU_FAMILY_CI;
1215 else
1216 adev->family = AMDGPU_FAMILY_KV;
1217
1218 r = cik_set_ip_blocks(adev);
1219 if (r)
1220 return r;
1221 break;
1222#endif
d38ceaf9
AD
1223 default:
1224 /* FIXME: not supported yet */
1225 return -EINVAL;
1226 }
1227
8faf0e08
AD
1228 adev->ip_block_status = kcalloc(adev->num_ip_blocks,
1229 sizeof(struct amdgpu_ip_block_status), GFP_KERNEL);
1230 if (adev->ip_block_status == NULL)
d8d090b7 1231 return -ENOMEM;
d38ceaf9
AD
1232
1233 if (adev->ip_blocks == NULL) {
1234 DRM_ERROR("No IP blocks found!\n");
1235 return r;
1236 }
1237
1238 for (i = 0; i < adev->num_ip_blocks; i++) {
1239 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
1240 DRM_ERROR("disabled ip block: %d\n", i);
8faf0e08 1241 adev->ip_block_status[i].valid = false;
d38ceaf9
AD
1242 } else {
1243 if (adev->ip_blocks[i].funcs->early_init) {
5fc3aeeb 1244 r = adev->ip_blocks[i].funcs->early_init((void *)adev);
2c1a2784 1245 if (r == -ENOENT) {
8faf0e08 1246 adev->ip_block_status[i].valid = false;
2c1a2784 1247 } else if (r) {
88a907d6 1248 DRM_ERROR("early_init of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r);
d38ceaf9 1249 return r;
2c1a2784 1250 } else {
8faf0e08 1251 adev->ip_block_status[i].valid = true;
2c1a2784 1252 }
974e6b64 1253 } else {
8faf0e08 1254 adev->ip_block_status[i].valid = true;
d38ceaf9 1255 }
d38ceaf9
AD
1256 }
1257 }
1258
395d1fb9
NH
1259 adev->cg_flags &= amdgpu_cg_mask;
1260 adev->pg_flags &= amdgpu_pg_mask;
1261
d38ceaf9
AD
1262 return 0;
1263}
1264
1265static int amdgpu_init(struct amdgpu_device *adev)
1266{
1267 int i, r;
1268
1269 for (i = 0; i < adev->num_ip_blocks; i++) {
8faf0e08 1270 if (!adev->ip_block_status[i].valid)
d38ceaf9 1271 continue;
5fc3aeeb 1272 r = adev->ip_blocks[i].funcs->sw_init((void *)adev);
2c1a2784 1273 if (r) {
822b2cef 1274 DRM_ERROR("sw_init of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r);
d38ceaf9 1275 return r;
2c1a2784 1276 }
8faf0e08 1277 adev->ip_block_status[i].sw = true;
d38ceaf9 1278 /* need to do gmc hw init early so we can allocate gpu mem */
5fc3aeeb 1279 if (adev->ip_blocks[i].type == AMD_IP_BLOCK_TYPE_GMC) {
d38ceaf9 1280 r = amdgpu_vram_scratch_init(adev);
2c1a2784
AD
1281 if (r) {
1282 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
d38ceaf9 1283 return r;
2c1a2784 1284 }
5fc3aeeb 1285 r = adev->ip_blocks[i].funcs->hw_init((void *)adev);
2c1a2784
AD
1286 if (r) {
1287 DRM_ERROR("hw_init %d failed %d\n", i, r);
d38ceaf9 1288 return r;
2c1a2784 1289 }
d38ceaf9 1290 r = amdgpu_wb_init(adev);
2c1a2784
AD
1291 if (r) {
1292 DRM_ERROR("amdgpu_wb_init failed %d\n", r);
d38ceaf9 1293 return r;
2c1a2784 1294 }
8faf0e08 1295 adev->ip_block_status[i].hw = true;
d38ceaf9
AD
1296 }
1297 }
1298
1299 for (i = 0; i < adev->num_ip_blocks; i++) {
8faf0e08 1300 if (!adev->ip_block_status[i].sw)
d38ceaf9
AD
1301 continue;
1302 /* gmc hw init is done early */
5fc3aeeb 1303 if (adev->ip_blocks[i].type == AMD_IP_BLOCK_TYPE_GMC)
d38ceaf9 1304 continue;
5fc3aeeb 1305 r = adev->ip_blocks[i].funcs->hw_init((void *)adev);
2c1a2784 1306 if (r) {
822b2cef 1307 DRM_ERROR("hw_init of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r);
d38ceaf9 1308 return r;
2c1a2784 1309 }
8faf0e08 1310 adev->ip_block_status[i].hw = true;
d38ceaf9
AD
1311 }
1312
1313 return 0;
1314}
1315
1316static int amdgpu_late_init(struct amdgpu_device *adev)
1317{
1318 int i = 0, r;
1319
1320 for (i = 0; i < adev->num_ip_blocks; i++) {
8faf0e08 1321 if (!adev->ip_block_status[i].valid)
d38ceaf9
AD
1322 continue;
1323 /* enable clockgating to save power */
5fc3aeeb 1324 r = adev->ip_blocks[i].funcs->set_clockgating_state((void *)adev,
1325 AMD_CG_STATE_GATE);
2c1a2784 1326 if (r) {
822b2cef 1327 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r);
d38ceaf9 1328 return r;
2c1a2784 1329 }
d38ceaf9 1330 if (adev->ip_blocks[i].funcs->late_init) {
5fc3aeeb 1331 r = adev->ip_blocks[i].funcs->late_init((void *)adev);
2c1a2784 1332 if (r) {
822b2cef 1333 DRM_ERROR("late_init of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r);
d38ceaf9 1334 return r;
2c1a2784 1335 }
d38ceaf9
AD
1336 }
1337 }
1338
1339 return 0;
1340}
1341
1342static int amdgpu_fini(struct amdgpu_device *adev)
1343{
1344 int i, r;
1345
1346 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
8faf0e08 1347 if (!adev->ip_block_status[i].hw)
d38ceaf9 1348 continue;
5fc3aeeb 1349 if (adev->ip_blocks[i].type == AMD_IP_BLOCK_TYPE_GMC) {
d38ceaf9
AD
1350 amdgpu_wb_fini(adev);
1351 amdgpu_vram_scratch_fini(adev);
1352 }
1353 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
5fc3aeeb 1354 r = adev->ip_blocks[i].funcs->set_clockgating_state((void *)adev,
1355 AMD_CG_STATE_UNGATE);
2c1a2784 1356 if (r) {
822b2cef 1357 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r);
d38ceaf9 1358 return r;
2c1a2784 1359 }
5fc3aeeb 1360 r = adev->ip_blocks[i].funcs->hw_fini((void *)adev);
d38ceaf9 1361 /* XXX handle errors */
2c1a2784 1362 if (r) {
822b2cef 1363 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r);
2c1a2784 1364 }
8faf0e08 1365 adev->ip_block_status[i].hw = false;
d38ceaf9
AD
1366 }
1367
1368 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
8faf0e08 1369 if (!adev->ip_block_status[i].sw)
d38ceaf9 1370 continue;
5fc3aeeb 1371 r = adev->ip_blocks[i].funcs->sw_fini((void *)adev);
d38ceaf9 1372 /* XXX handle errors */
2c1a2784 1373 if (r) {
822b2cef 1374 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r);
2c1a2784 1375 }
8faf0e08
AD
1376 adev->ip_block_status[i].sw = false;
1377 adev->ip_block_status[i].valid = false;
d38ceaf9
AD
1378 }
1379
a6dcfd9c
ML
1380 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
1381 if (adev->ip_blocks[i].funcs->late_fini)
1382 adev->ip_blocks[i].funcs->late_fini((void *)adev);
1383 }
1384
d38ceaf9
AD
1385 return 0;
1386}
1387
1388static int amdgpu_suspend(struct amdgpu_device *adev)
1389{
1390 int i, r;
1391
c5a93a28
FC
1392 /* ungate SMC block first */
1393 r = amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC,
1394 AMD_CG_STATE_UNGATE);
1395 if (r) {
1396 DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n",r);
1397 }
1398
d38ceaf9 1399 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
8faf0e08 1400 if (!adev->ip_block_status[i].valid)
d38ceaf9
AD
1401 continue;
1402 /* ungate blocks so that suspend can properly shut them down */
c5a93a28
FC
1403 if (i != AMD_IP_BLOCK_TYPE_SMC) {
1404 r = adev->ip_blocks[i].funcs->set_clockgating_state((void *)adev,
1405 AMD_CG_STATE_UNGATE);
1406 if (r) {
822b2cef 1407 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r);
c5a93a28 1408 }
2c1a2784 1409 }
d38ceaf9
AD
1410 /* XXX handle errors */
1411 r = adev->ip_blocks[i].funcs->suspend(adev);
1412 /* XXX handle errors */
2c1a2784 1413 if (r) {
822b2cef 1414 DRM_ERROR("suspend of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r);
2c1a2784 1415 }
d38ceaf9
AD
1416 }
1417
1418 return 0;
1419}
1420
1421static int amdgpu_resume(struct amdgpu_device *adev)
1422{
1423 int i, r;
1424
1425 for (i = 0; i < adev->num_ip_blocks; i++) {
8faf0e08 1426 if (!adev->ip_block_status[i].valid)
d38ceaf9
AD
1427 continue;
1428 r = adev->ip_blocks[i].funcs->resume(adev);
2c1a2784 1429 if (r) {
822b2cef 1430 DRM_ERROR("resume of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r);
d38ceaf9 1431 return r;
2c1a2784 1432 }
d38ceaf9
AD
1433 }
1434
1435 return 0;
1436}
1437
048765ad
AR
1438static bool amdgpu_device_is_virtual(void)
1439{
1440#ifdef CONFIG_X86
1441 return boot_cpu_has(X86_FEATURE_HYPERVISOR);
1442#else
1443 return false;
1444#endif
1445}
1446
d38ceaf9
AD
1447/**
1448 * amdgpu_device_init - initialize the driver
1449 *
1450 * @adev: amdgpu_device pointer
1451 * @pdev: drm dev pointer
1452 * @pdev: pci dev pointer
1453 * @flags: driver flags
1454 *
1455 * Initializes the driver info and hw (all asics).
1456 * Returns 0 for success or an error on failure.
1457 * Called at driver startup.
1458 */
1459int amdgpu_device_init(struct amdgpu_device *adev,
1460 struct drm_device *ddev,
1461 struct pci_dev *pdev,
1462 uint32_t flags)
1463{
1464 int r, i;
1465 bool runtime = false;
1466
1467 adev->shutdown = false;
1468 adev->dev = &pdev->dev;
1469 adev->ddev = ddev;
1470 adev->pdev = pdev;
1471 adev->flags = flags;
2f7d10b3 1472 adev->asic_type = flags & AMD_ASIC_MASK;
d38ceaf9
AD
1473 adev->is_atom_bios = false;
1474 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
1475 adev->mc.gtt_size = 512 * 1024 * 1024;
1476 adev->accel_working = false;
1477 adev->num_rings = 0;
1478 adev->mman.buffer_funcs = NULL;
1479 adev->mman.buffer_funcs_ring = NULL;
1480 adev->vm_manager.vm_pte_funcs = NULL;
2d55e45a 1481 adev->vm_manager.vm_pte_num_rings = 0;
d38ceaf9
AD
1482 adev->gart.gart_funcs = NULL;
1483 adev->fence_context = fence_context_alloc(AMDGPU_MAX_RINGS);
1484
1485 adev->smc_rreg = &amdgpu_invalid_rreg;
1486 adev->smc_wreg = &amdgpu_invalid_wreg;
1487 adev->pcie_rreg = &amdgpu_invalid_rreg;
1488 adev->pcie_wreg = &amdgpu_invalid_wreg;
1489 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
1490 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
1491 adev->didt_rreg = &amdgpu_invalid_rreg;
1492 adev->didt_wreg = &amdgpu_invalid_wreg;
ccdbb20a
RZ
1493 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
1494 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
d38ceaf9
AD
1495 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
1496 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
1497
ccdbb20a 1498
3e39ab90
AD
1499 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
1500 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
1501 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
d38ceaf9
AD
1502
1503 /* mutex initialization are all done here so we
1504 * can recall function without having locking issues */
8d0a7cea 1505 mutex_init(&adev->vm_manager.lock);
d38ceaf9 1506 atomic_set(&adev->irq.ih.lock, 0);
d38ceaf9
AD
1507 mutex_init(&adev->pm.mutex);
1508 mutex_init(&adev->gfx.gpu_clock_mutex);
1509 mutex_init(&adev->srbm_mutex);
1510 mutex_init(&adev->grbm_idx_mutex);
d38ceaf9
AD
1511 mutex_init(&adev->mn_lock);
1512 hash_init(adev->mn_hash);
1513
1514 amdgpu_check_arguments(adev);
1515
1516 /* Registers mapping */
1517 /* TODO: block userspace mapping of io register */
1518 spin_lock_init(&adev->mmio_idx_lock);
1519 spin_lock_init(&adev->smc_idx_lock);
1520 spin_lock_init(&adev->pcie_idx_lock);
1521 spin_lock_init(&adev->uvd_ctx_idx_lock);
1522 spin_lock_init(&adev->didt_idx_lock);
ccdbb20a 1523 spin_lock_init(&adev->gc_cac_idx_lock);
d38ceaf9
AD
1524 spin_lock_init(&adev->audio_endpt_idx_lock);
1525
1526 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
1527 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
1528 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
1529 if (adev->rmmio == NULL) {
1530 return -ENOMEM;
1531 }
1532 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
1533 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
1534
1535 /* doorbell bar mapping */
1536 amdgpu_doorbell_init(adev);
1537
1538 /* io port mapping */
1539 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
1540 if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
1541 adev->rio_mem_size = pci_resource_len(adev->pdev, i);
1542 adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
1543 break;
1544 }
1545 }
1546 if (adev->rio_mem == NULL)
1547 DRM_ERROR("Unable to find PCI I/O BAR\n");
1548
1549 /* early init functions */
1550 r = amdgpu_early_init(adev);
1551 if (r)
1552 return r;
1553
1554 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
1555 /* this will fail for cards that aren't VGA class devices, just
1556 * ignore it */
1557 vga_client_register(adev->pdev, adev, NULL, amdgpu_vga_set_decode);
1558
1559 if (amdgpu_runtime_pm == 1)
1560 runtime = true;
e9bef455 1561 if (amdgpu_device_is_px(ddev))
d38ceaf9
AD
1562 runtime = true;
1563 vga_switcheroo_register_client(adev->pdev, &amdgpu_switcheroo_ops, runtime);
1564 if (runtime)
1565 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
1566
1567 /* Read BIOS */
83ba126a
AD
1568 if (!amdgpu_get_bios(adev)) {
1569 r = -EINVAL;
1570 goto failed;
1571 }
d38ceaf9
AD
1572 /* Must be an ATOMBIOS */
1573 if (!adev->is_atom_bios) {
1574 dev_err(adev->dev, "Expecting atombios for GPU\n");
83ba126a
AD
1575 r = -EINVAL;
1576 goto failed;
d38ceaf9
AD
1577 }
1578 r = amdgpu_atombios_init(adev);
2c1a2784
AD
1579 if (r) {
1580 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
83ba126a 1581 goto failed;
2c1a2784 1582 }
d38ceaf9 1583
7e471e6f
AD
1584 /* See if the asic supports SR-IOV */
1585 adev->virtualization.supports_sr_iov =
1586 amdgpu_atombios_has_gpu_virtualization_table(adev);
1587
048765ad
AR
1588 /* Check if we are executing in a virtualized environment */
1589 adev->virtualization.is_virtual = amdgpu_device_is_virtual();
1590 adev->virtualization.caps = amdgpu_asic_get_virtual_caps(adev);
1591
d38ceaf9 1592 /* Post card if necessary */
048765ad
AR
1593 if (!amdgpu_card_posted(adev) ||
1594 (adev->virtualization.is_virtual &&
48a70e1c 1595 !(adev->virtualization.caps & AMDGPU_VIRT_CAPS_SRIOV_EN))) {
d38ceaf9
AD
1596 if (!adev->bios) {
1597 dev_err(adev->dev, "Card not posted and no BIOS - ignoring\n");
83ba126a
AD
1598 r = -EINVAL;
1599 goto failed;
d38ceaf9
AD
1600 }
1601 DRM_INFO("GPU not posted. posting now...\n");
1602 amdgpu_atom_asic_init(adev->mode_info.atom_context);
1603 }
1604
1605 /* Initialize clocks */
1606 r = amdgpu_atombios_get_clock_info(adev);
2c1a2784
AD
1607 if (r) {
1608 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
83ba126a 1609 goto failed;
2c1a2784 1610 }
d38ceaf9
AD
1611 /* init i2c buses */
1612 amdgpu_atombios_i2c_init(adev);
1613
1614 /* Fence driver */
1615 r = amdgpu_fence_driver_init(adev);
2c1a2784
AD
1616 if (r) {
1617 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
83ba126a 1618 goto failed;
2c1a2784 1619 }
d38ceaf9
AD
1620
1621 /* init the mode config */
1622 drm_mode_config_init(adev->ddev);
1623
1624 r = amdgpu_init(adev);
1625 if (r) {
2c1a2784 1626 dev_err(adev->dev, "amdgpu_init failed\n");
d38ceaf9 1627 amdgpu_fini(adev);
83ba126a 1628 goto failed;
d38ceaf9
AD
1629 }
1630
1631 adev->accel_working = true;
1632
1633 amdgpu_fbdev_init(adev);
1634
1635 r = amdgpu_ib_pool_init(adev);
1636 if (r) {
1637 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
83ba126a 1638 goto failed;
d38ceaf9
AD
1639 }
1640
1641 r = amdgpu_ib_ring_tests(adev);
1642 if (r)
1643 DRM_ERROR("ib ring test failed (%d).\n", r);
1644
1645 r = amdgpu_gem_debugfs_init(adev);
1646 if (r) {
1647 DRM_ERROR("registering gem debugfs failed (%d).\n", r);
1648 }
1649
1650 r = amdgpu_debugfs_regs_init(adev);
1651 if (r) {
1652 DRM_ERROR("registering register debugfs failed (%d).\n", r);
1653 }
1654
50ab2533
HR
1655 r = amdgpu_debugfs_firmware_init(adev);
1656 if (r) {
1657 DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
1658 return r;
1659 }
1660
d38ceaf9
AD
1661 if ((amdgpu_testing & 1)) {
1662 if (adev->accel_working)
1663 amdgpu_test_moves(adev);
1664 else
1665 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
1666 }
1667 if ((amdgpu_testing & 2)) {
1668 if (adev->accel_working)
1669 amdgpu_test_syncing(adev);
1670 else
1671 DRM_INFO("amdgpu: acceleration disabled, skipping sync tests\n");
1672 }
1673 if (amdgpu_benchmarking) {
1674 if (adev->accel_working)
1675 amdgpu_benchmark(adev, amdgpu_benchmarking);
1676 else
1677 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
1678 }
1679
1680 /* enable clockgating, etc. after ib tests, etc. since some blocks require
1681 * explicit gating rather than handling it automatically.
1682 */
1683 r = amdgpu_late_init(adev);
2c1a2784
AD
1684 if (r) {
1685 dev_err(adev->dev, "amdgpu_late_init failed\n");
83ba126a 1686 goto failed;
2c1a2784 1687 }
d38ceaf9
AD
1688
1689 return 0;
83ba126a
AD
1690
1691failed:
1692 if (runtime)
1693 vga_switcheroo_fini_domain_pm_ops(adev->dev);
1694 return r;
d38ceaf9
AD
1695}
1696
1697static void amdgpu_debugfs_remove_files(struct amdgpu_device *adev);
1698
1699/**
1700 * amdgpu_device_fini - tear down the driver
1701 *
1702 * @adev: amdgpu_device pointer
1703 *
1704 * Tear down the driver info (all asics).
1705 * Called at driver shutdown.
1706 */
1707void amdgpu_device_fini(struct amdgpu_device *adev)
1708{
1709 int r;
1710
1711 DRM_INFO("amdgpu: finishing device.\n");
1712 adev->shutdown = true;
1713 /* evict vram memory */
1714 amdgpu_bo_evict_vram(adev);
1715 amdgpu_ib_pool_fini(adev);
1716 amdgpu_fence_driver_fini(adev);
84b89bdc 1717 drm_crtc_force_disable_all(adev->ddev);
d38ceaf9
AD
1718 amdgpu_fbdev_fini(adev);
1719 r = amdgpu_fini(adev);
8faf0e08
AD
1720 kfree(adev->ip_block_status);
1721 adev->ip_block_status = NULL;
d38ceaf9
AD
1722 adev->accel_working = false;
1723 /* free i2c buses */
1724 amdgpu_i2c_fini(adev);
1725 amdgpu_atombios_fini(adev);
1726 kfree(adev->bios);
1727 adev->bios = NULL;
1728 vga_switcheroo_unregister_client(adev->pdev);
83ba126a
AD
1729 if (adev->flags & AMD_IS_PX)
1730 vga_switcheroo_fini_domain_pm_ops(adev->dev);
d38ceaf9
AD
1731 vga_client_register(adev->pdev, NULL, NULL, NULL);
1732 if (adev->rio_mem)
1733 pci_iounmap(adev->pdev, adev->rio_mem);
1734 adev->rio_mem = NULL;
1735 iounmap(adev->rmmio);
1736 adev->rmmio = NULL;
1737 amdgpu_doorbell_fini(adev);
1738 amdgpu_debugfs_regs_cleanup(adev);
1739 amdgpu_debugfs_remove_files(adev);
1740}
1741
1742
1743/*
1744 * Suspend & resume.
1745 */
1746/**
1747 * amdgpu_suspend_kms - initiate device suspend
1748 *
1749 * @pdev: drm dev pointer
1750 * @state: suspend state
1751 *
1752 * Puts the hw in the suspend state (all asics).
1753 * Returns 0 for success or an error on failure.
1754 * Called at driver suspend.
1755 */
1756int amdgpu_suspend_kms(struct drm_device *dev, bool suspend, bool fbcon)
1757{
1758 struct amdgpu_device *adev;
1759 struct drm_crtc *crtc;
1760 struct drm_connector *connector;
5ceb54c6 1761 int r;
d38ceaf9
AD
1762
1763 if (dev == NULL || dev->dev_private == NULL) {
1764 return -ENODEV;
1765 }
1766
1767 adev = dev->dev_private;
1768
1769 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
1770 return 0;
1771
1772 drm_kms_helper_poll_disable(dev);
1773
1774 /* turn off display hw */
4c7fbc39 1775 drm_modeset_lock_all(dev);
d38ceaf9
AD
1776 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1777 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
1778 }
4c7fbc39 1779 drm_modeset_unlock_all(dev);
d38ceaf9 1780
756e6880 1781 /* unpin the front buffers and cursors */
d38ceaf9 1782 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
756e6880 1783 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
d38ceaf9
AD
1784 struct amdgpu_framebuffer *rfb = to_amdgpu_framebuffer(crtc->primary->fb);
1785 struct amdgpu_bo *robj;
1786
756e6880
AD
1787 if (amdgpu_crtc->cursor_bo) {
1788 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
1789 r = amdgpu_bo_reserve(aobj, false);
1790 if (r == 0) {
1791 amdgpu_bo_unpin(aobj);
1792 amdgpu_bo_unreserve(aobj);
1793 }
1794 }
1795
d38ceaf9
AD
1796 if (rfb == NULL || rfb->obj == NULL) {
1797 continue;
1798 }
1799 robj = gem_to_amdgpu_bo(rfb->obj);
1800 /* don't unpin kernel fb objects */
1801 if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
1802 r = amdgpu_bo_reserve(robj, false);
1803 if (r == 0) {
1804 amdgpu_bo_unpin(robj);
1805 amdgpu_bo_unreserve(robj);
1806 }
1807 }
1808 }
1809 /* evict vram memory */
1810 amdgpu_bo_evict_vram(adev);
1811
5ceb54c6 1812 amdgpu_fence_driver_suspend(adev);
d38ceaf9
AD
1813
1814 r = amdgpu_suspend(adev);
1815
1816 /* evict remaining vram memory */
1817 amdgpu_bo_evict_vram(adev);
1818
1819 pci_save_state(dev->pdev);
1820 if (suspend) {
1821 /* Shut down the device */
1822 pci_disable_device(dev->pdev);
1823 pci_set_power_state(dev->pdev, PCI_D3hot);
1824 }
1825
1826 if (fbcon) {
1827 console_lock();
1828 amdgpu_fbdev_set_suspend(adev, 1);
1829 console_unlock();
1830 }
1831 return 0;
1832}
1833
1834/**
1835 * amdgpu_resume_kms - initiate device resume
1836 *
1837 * @pdev: drm dev pointer
1838 *
1839 * Bring the hw back to operating state (all asics).
1840 * Returns 0 for success or an error on failure.
1841 * Called at driver resume.
1842 */
1843int amdgpu_resume_kms(struct drm_device *dev, bool resume, bool fbcon)
1844{
1845 struct drm_connector *connector;
1846 struct amdgpu_device *adev = dev->dev_private;
756e6880 1847 struct drm_crtc *crtc;
d38ceaf9
AD
1848 int r;
1849
1850 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
1851 return 0;
1852
1853 if (fbcon) {
1854 console_lock();
1855 }
1856 if (resume) {
1857 pci_set_power_state(dev->pdev, PCI_D0);
1858 pci_restore_state(dev->pdev);
1859 if (pci_enable_device(dev->pdev)) {
1860 if (fbcon)
1861 console_unlock();
1862 return -1;
1863 }
1864 }
1865
1866 /* post card */
ca198528
FC
1867 if (!amdgpu_card_posted(adev))
1868 amdgpu_atom_asic_init(adev->mode_info.atom_context);
d38ceaf9
AD
1869
1870 r = amdgpu_resume(adev);
ca198528
FC
1871 if (r)
1872 DRM_ERROR("amdgpu_resume failed (%d).\n", r);
d38ceaf9 1873
5ceb54c6
AD
1874 amdgpu_fence_driver_resume(adev);
1875
ca198528
FC
1876 if (resume) {
1877 r = amdgpu_ib_ring_tests(adev);
1878 if (r)
1879 DRM_ERROR("ib ring test failed (%d).\n", r);
1880 }
d38ceaf9
AD
1881
1882 r = amdgpu_late_init(adev);
1883 if (r)
1884 return r;
1885
756e6880
AD
1886 /* pin cursors */
1887 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1888 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
1889
1890 if (amdgpu_crtc->cursor_bo) {
1891 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
1892 r = amdgpu_bo_reserve(aobj, false);
1893 if (r == 0) {
1894 r = amdgpu_bo_pin(aobj,
1895 AMDGPU_GEM_DOMAIN_VRAM,
1896 &amdgpu_crtc->cursor_addr);
1897 if (r != 0)
1898 DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
1899 amdgpu_bo_unreserve(aobj);
1900 }
1901 }
1902 }
1903
d38ceaf9
AD
1904 /* blat the mode back in */
1905 if (fbcon) {
1906 drm_helper_resume_force_mode(dev);
1907 /* turn on display hw */
4c7fbc39 1908 drm_modeset_lock_all(dev);
d38ceaf9
AD
1909 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1910 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
1911 }
4c7fbc39 1912 drm_modeset_unlock_all(dev);
d38ceaf9
AD
1913 }
1914
1915 drm_kms_helper_poll_enable(dev);
23a1a9e5
L
1916
1917 /*
1918 * Most of the connector probing functions try to acquire runtime pm
1919 * refs to ensure that the GPU is powered on when connector polling is
1920 * performed. Since we're calling this from a runtime PM callback,
1921 * trying to acquire rpm refs will cause us to deadlock.
1922 *
1923 * Since we're guaranteed to be holding the rpm lock, it's safe to
1924 * temporarily disable the rpm helpers so this doesn't deadlock us.
1925 */
1926#ifdef CONFIG_PM
1927 dev->dev->power.disable_depth++;
1928#endif
54fb2a5c 1929 drm_helper_hpd_irq_event(dev);
23a1a9e5
L
1930#ifdef CONFIG_PM
1931 dev->dev->power.disable_depth--;
1932#endif
d38ceaf9
AD
1933
1934 if (fbcon) {
1935 amdgpu_fbdev_set_suspend(adev, 0);
1936 console_unlock();
1937 }
1938
1939 return 0;
1940}
1941
63fbf42f
CZ
1942static bool amdgpu_check_soft_reset(struct amdgpu_device *adev)
1943{
1944 int i;
1945 bool asic_hang = false;
1946
1947 for (i = 0; i < adev->num_ip_blocks; i++) {
1948 if (!adev->ip_block_status[i].valid)
1949 continue;
1950 if (adev->ip_blocks[i].funcs->check_soft_reset)
1951 adev->ip_blocks[i].funcs->check_soft_reset(adev);
1952 if (adev->ip_block_status[i].hang) {
1953 DRM_INFO("IP block:%d is hang!\n", i);
1954 asic_hang = true;
1955 }
1956 }
1957 return asic_hang;
1958}
1959
d31a501e
CZ
1960int amdgpu_pre_soft_reset(struct amdgpu_device *adev)
1961{
1962 int i, r = 0;
1963
1964 for (i = 0; i < adev->num_ip_blocks; i++) {
1965 if (!adev->ip_block_status[i].valid)
1966 continue;
35d782fe
CZ
1967 if (adev->ip_block_status[i].hang &&
1968 adev->ip_blocks[i].funcs->pre_soft_reset) {
d31a501e
CZ
1969 r = adev->ip_blocks[i].funcs->pre_soft_reset(adev);
1970 if (r)
1971 return r;
1972 }
1973 }
1974
1975 return 0;
1976}
1977
35d782fe
CZ
1978static bool amdgpu_need_full_reset(struct amdgpu_device *adev)
1979{
1980 if (adev->ip_block_status[AMD_IP_BLOCK_TYPE_GMC].hang ||
35d782fe 1981 adev->ip_block_status[AMD_IP_BLOCK_TYPE_SMC].hang ||
35d782fe
CZ
1982 adev->ip_block_status[AMD_IP_BLOCK_TYPE_ACP].hang ||
1983 adev->ip_block_status[AMD_IP_BLOCK_TYPE_DCE].hang) {
1984 DRM_INFO("Some block need full reset!\n");
1985 return true;
1986 }
1987 return false;
1988}
1989
1990static int amdgpu_soft_reset(struct amdgpu_device *adev)
1991{
1992 int i, r = 0;
1993
1994 for (i = 0; i < adev->num_ip_blocks; i++) {
1995 if (!adev->ip_block_status[i].valid)
1996 continue;
1997 if (adev->ip_block_status[i].hang &&
1998 adev->ip_blocks[i].funcs->soft_reset) {
1999 r = adev->ip_blocks[i].funcs->soft_reset(adev);
2000 if (r)
2001 return r;
2002 }
2003 }
2004
2005 return 0;
2006}
2007
2008static int amdgpu_post_soft_reset(struct amdgpu_device *adev)
2009{
2010 int i, r = 0;
2011
2012 for (i = 0; i < adev->num_ip_blocks; i++) {
2013 if (!adev->ip_block_status[i].valid)
2014 continue;
2015 if (adev->ip_block_status[i].hang &&
2016 adev->ip_blocks[i].funcs->post_soft_reset)
2017 r = adev->ip_blocks[i].funcs->post_soft_reset(adev);
2018 if (r)
2019 return r;
2020 }
2021
2022 return 0;
2023}
2024
d38ceaf9
AD
2025/**
2026 * amdgpu_gpu_reset - reset the asic
2027 *
2028 * @adev: amdgpu device pointer
2029 *
2030 * Attempt the reset the GPU if it has hung (all asics).
2031 * Returns 0 for success or an error on failure.
2032 */
2033int amdgpu_gpu_reset(struct amdgpu_device *adev)
2034{
d38ceaf9
AD
2035 int i, r;
2036 int resched;
35d782fe 2037 bool need_full_reset;
d38ceaf9 2038
63fbf42f
CZ
2039 if (!amdgpu_check_soft_reset(adev)) {
2040 DRM_INFO("No hardware hang detected. Did some blocks stall?\n");
2041 return 0;
2042 }
2043
d94aed5a 2044 atomic_inc(&adev->gpu_reset_counter);
d38ceaf9 2045
a3c47d6b
CZ
2046 /* block TTM */
2047 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
2048
0875dc9e
CZ
2049 /* block scheduler */
2050 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2051 struct amdgpu_ring *ring = adev->rings[i];
2052
2053 if (!ring)
2054 continue;
2055 kthread_park(ring->sched.thread);
aa1c8900 2056 amd_sched_hw_job_reset(&ring->sched);
0875dc9e 2057 }
2200edac
CZ
2058 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
2059 amdgpu_fence_driver_force_completion(adev);
d38ceaf9 2060
35d782fe 2061 need_full_reset = amdgpu_need_full_reset(adev);
d38ceaf9 2062
35d782fe
CZ
2063 if (!need_full_reset) {
2064 amdgpu_pre_soft_reset(adev);
2065 r = amdgpu_soft_reset(adev);
2066 amdgpu_post_soft_reset(adev);
2067 if (r || amdgpu_check_soft_reset(adev)) {
2068 DRM_INFO("soft reset failed, will fallback to full reset!\n");
2069 need_full_reset = true;
2070 }
f1aa7e08
CZ
2071 }
2072
35d782fe
CZ
2073 if (need_full_reset) {
2074 /* save scratch */
2075 amdgpu_atombios_scratch_regs_save(adev);
2076 r = amdgpu_suspend(adev);
bfa99269 2077
35d782fe
CZ
2078retry:
2079 /* Disable fb access */
2080 if (adev->mode_info.num_crtc) {
2081 struct amdgpu_mode_mc_save save;
2082 amdgpu_display_stop_mc_access(adev, &save);
2083 amdgpu_wait_for_idle(adev, AMD_IP_BLOCK_TYPE_GMC);
2084 }
2085
2086 r = amdgpu_asic_reset(adev);
2087 /* post card */
2088 amdgpu_atom_asic_init(adev->mode_info.atom_context);
2089
2090 if (!r) {
2091 dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
2092 r = amdgpu_resume(adev);
2093 }
2094 /* restore scratch */
2095 amdgpu_atombios_scratch_regs_restore(adev);
d38ceaf9 2096 }
d38ceaf9 2097 if (!r) {
e72cfd58 2098 amdgpu_irq_gpu_reset_resume_helper(adev);
1f465087
CZ
2099 r = amdgpu_ib_ring_tests(adev);
2100 if (r) {
2101 dev_err(adev->dev, "ib ring test failed (%d).\n", r);
40019dc4
CZ
2102 r = amdgpu_suspend(adev);
2103 goto retry;
1f465087
CZ
2104 }
2105
d38ceaf9
AD
2106 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2107 struct amdgpu_ring *ring = adev->rings[i];
2108 if (!ring)
2109 continue;
aa1c8900 2110 amd_sched_job_recovery(&ring->sched);
0875dc9e 2111 kthread_unpark(ring->sched.thread);
d38ceaf9 2112 }
d38ceaf9 2113 } else {
2200edac 2114 dev_err(adev->dev, "asic resume failed (%d).\n", r);
d38ceaf9 2115 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
0875dc9e
CZ
2116 if (adev->rings[i]) {
2117 kthread_unpark(adev->rings[i]->sched.thread);
0875dc9e 2118 }
d38ceaf9
AD
2119 }
2120 }
2121
2122 drm_helper_resume_force_mode(adev->ddev);
2123
2124 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
2125 if (r) {
2126 /* bad news, how to tell it to userspace ? */
2127 dev_info(adev->dev, "GPU reset failed\n");
2128 }
2129
d38ceaf9
AD
2130 return r;
2131}
2132
d0dd7f0c
AD
2133void amdgpu_get_pcie_info(struct amdgpu_device *adev)
2134{
2135 u32 mask;
2136 int ret;
2137
cd474ba0
AD
2138 if (amdgpu_pcie_gen_cap)
2139 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
d0dd7f0c 2140
cd474ba0
AD
2141 if (amdgpu_pcie_lane_cap)
2142 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
d0dd7f0c 2143
cd474ba0
AD
2144 /* covers APUs as well */
2145 if (pci_is_root_bus(adev->pdev->bus)) {
2146 if (adev->pm.pcie_gen_mask == 0)
2147 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
2148 if (adev->pm.pcie_mlw_mask == 0)
2149 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
d0dd7f0c 2150 return;
cd474ba0 2151 }
d0dd7f0c 2152
cd474ba0
AD
2153 if (adev->pm.pcie_gen_mask == 0) {
2154 ret = drm_pcie_get_speed_cap_mask(adev->ddev, &mask);
2155 if (!ret) {
2156 adev->pm.pcie_gen_mask = (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
2157 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
2158 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
2159
2160 if (mask & DRM_PCIE_SPEED_25)
2161 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
2162 if (mask & DRM_PCIE_SPEED_50)
2163 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2;
2164 if (mask & DRM_PCIE_SPEED_80)
2165 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3;
2166 } else {
2167 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
2168 }
2169 }
2170 if (adev->pm.pcie_mlw_mask == 0) {
2171 ret = drm_pcie_get_max_link_width(adev->ddev, &mask);
2172 if (!ret) {
2173 switch (mask) {
2174 case 32:
2175 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
2176 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
2177 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2178 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2179 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2180 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2181 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2182 break;
2183 case 16:
2184 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
2185 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2186 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2187 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2188 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2189 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2190 break;
2191 case 12:
2192 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2193 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2194 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2195 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2196 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2197 break;
2198 case 8:
2199 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2200 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2201 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2202 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2203 break;
2204 case 4:
2205 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2206 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2207 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2208 break;
2209 case 2:
2210 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2211 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2212 break;
2213 case 1:
2214 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
2215 break;
2216 default:
2217 break;
2218 }
2219 } else {
2220 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
d0dd7f0c
AD
2221 }
2222 }
2223}
d38ceaf9
AD
2224
2225/*
2226 * Debugfs
2227 */
2228int amdgpu_debugfs_add_files(struct amdgpu_device *adev,
06ab6832 2229 const struct drm_info_list *files,
d38ceaf9
AD
2230 unsigned nfiles)
2231{
2232 unsigned i;
2233
2234 for (i = 0; i < adev->debugfs_count; i++) {
2235 if (adev->debugfs[i].files == files) {
2236 /* Already registered */
2237 return 0;
2238 }
2239 }
2240
2241 i = adev->debugfs_count + 1;
2242 if (i > AMDGPU_DEBUGFS_MAX_COMPONENTS) {
2243 DRM_ERROR("Reached maximum number of debugfs components.\n");
2244 DRM_ERROR("Report so we increase "
2245 "AMDGPU_DEBUGFS_MAX_COMPONENTS.\n");
2246 return -EINVAL;
2247 }
2248 adev->debugfs[adev->debugfs_count].files = files;
2249 adev->debugfs[adev->debugfs_count].num_files = nfiles;
2250 adev->debugfs_count = i;
2251#if defined(CONFIG_DEBUG_FS)
2252 drm_debugfs_create_files(files, nfiles,
2253 adev->ddev->control->debugfs_root,
2254 adev->ddev->control);
2255 drm_debugfs_create_files(files, nfiles,
2256 adev->ddev->primary->debugfs_root,
2257 adev->ddev->primary);
2258#endif
2259 return 0;
2260}
2261
2262static void amdgpu_debugfs_remove_files(struct amdgpu_device *adev)
2263{
2264#if defined(CONFIG_DEBUG_FS)
2265 unsigned i;
2266
2267 for (i = 0; i < adev->debugfs_count; i++) {
2268 drm_debugfs_remove_files(adev->debugfs[i].files,
2269 adev->debugfs[i].num_files,
2270 adev->ddev->control);
2271 drm_debugfs_remove_files(adev->debugfs[i].files,
2272 adev->debugfs[i].num_files,
2273 adev->ddev->primary);
2274 }
2275#endif
2276}
2277
2278#if defined(CONFIG_DEBUG_FS)
2279
2280static ssize_t amdgpu_debugfs_regs_read(struct file *f, char __user *buf,
2281 size_t size, loff_t *pos)
2282{
2283 struct amdgpu_device *adev = f->f_inode->i_private;
2284 ssize_t result = 0;
2285 int r;
bd12267d 2286 bool pm_pg_lock, use_bank;
56628159 2287 unsigned instance_bank, sh_bank, se_bank;
d38ceaf9
AD
2288
2289 if (size & 0x3 || *pos & 0x3)
2290 return -EINVAL;
2291
bd12267d
TSD
2292 /* are we reading registers for which a PG lock is necessary? */
2293 pm_pg_lock = (*pos >> 23) & 1;
2294
56628159
TSD
2295 if (*pos & (1ULL << 62)) {
2296 se_bank = (*pos >> 24) & 0x3FF;
2297 sh_bank = (*pos >> 34) & 0x3FF;
2298 instance_bank = (*pos >> 44) & 0x3FF;
2299 use_bank = 1;
56628159
TSD
2300 } else {
2301 use_bank = 0;
2302 }
2303
bd12267d
TSD
2304 *pos &= 0x3FFFF;
2305
56628159
TSD
2306 if (use_bank) {
2307 if (sh_bank >= adev->gfx.config.max_sh_per_se ||
2308 se_bank >= adev->gfx.config.max_shader_engines)
2309 return -EINVAL;
2310 mutex_lock(&adev->grbm_idx_mutex);
2311 amdgpu_gfx_select_se_sh(adev, se_bank,
2312 sh_bank, instance_bank);
2313 }
2314
bd12267d
TSD
2315 if (pm_pg_lock)
2316 mutex_lock(&adev->pm.mutex);
2317
d38ceaf9
AD
2318 while (size) {
2319 uint32_t value;
2320
2321 if (*pos > adev->rmmio_size)
56628159 2322 goto end;
d38ceaf9
AD
2323
2324 value = RREG32(*pos >> 2);
2325 r = put_user(value, (uint32_t *)buf);
56628159
TSD
2326 if (r) {
2327 result = r;
2328 goto end;
2329 }
d38ceaf9
AD
2330
2331 result += 4;
2332 buf += 4;
2333 *pos += 4;
2334 size -= 4;
2335 }
2336
56628159
TSD
2337end:
2338 if (use_bank) {
2339 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
2340 mutex_unlock(&adev->grbm_idx_mutex);
2341 }
2342
bd12267d
TSD
2343 if (pm_pg_lock)
2344 mutex_unlock(&adev->pm.mutex);
2345
d38ceaf9
AD
2346 return result;
2347}
2348
2349static ssize_t amdgpu_debugfs_regs_write(struct file *f, const char __user *buf,
2350 size_t size, loff_t *pos)
2351{
2352 struct amdgpu_device *adev = f->f_inode->i_private;
2353 ssize_t result = 0;
2354 int r;
2355
2356 if (size & 0x3 || *pos & 0x3)
2357 return -EINVAL;
2358
2359 while (size) {
2360 uint32_t value;
2361
2362 if (*pos > adev->rmmio_size)
2363 return result;
2364
2365 r = get_user(value, (uint32_t *)buf);
2366 if (r)
2367 return r;
2368
2369 WREG32(*pos >> 2, value);
2370
2371 result += 4;
2372 buf += 4;
2373 *pos += 4;
2374 size -= 4;
2375 }
2376
2377 return result;
2378}
2379
adcec288
TSD
2380static ssize_t amdgpu_debugfs_regs_pcie_read(struct file *f, char __user *buf,
2381 size_t size, loff_t *pos)
2382{
2383 struct amdgpu_device *adev = f->f_inode->i_private;
2384 ssize_t result = 0;
2385 int r;
2386
2387 if (size & 0x3 || *pos & 0x3)
2388 return -EINVAL;
2389
2390 while (size) {
2391 uint32_t value;
2392
2393 value = RREG32_PCIE(*pos >> 2);
2394 r = put_user(value, (uint32_t *)buf);
2395 if (r)
2396 return r;
2397
2398 result += 4;
2399 buf += 4;
2400 *pos += 4;
2401 size -= 4;
2402 }
2403
2404 return result;
2405}
2406
2407static ssize_t amdgpu_debugfs_regs_pcie_write(struct file *f, const char __user *buf,
2408 size_t size, loff_t *pos)
2409{
2410 struct amdgpu_device *adev = f->f_inode->i_private;
2411 ssize_t result = 0;
2412 int r;
2413
2414 if (size & 0x3 || *pos & 0x3)
2415 return -EINVAL;
2416
2417 while (size) {
2418 uint32_t value;
2419
2420 r = get_user(value, (uint32_t *)buf);
2421 if (r)
2422 return r;
2423
2424 WREG32_PCIE(*pos >> 2, value);
2425
2426 result += 4;
2427 buf += 4;
2428 *pos += 4;
2429 size -= 4;
2430 }
2431
2432 return result;
2433}
2434
2435static ssize_t amdgpu_debugfs_regs_didt_read(struct file *f, char __user *buf,
2436 size_t size, loff_t *pos)
2437{
2438 struct amdgpu_device *adev = f->f_inode->i_private;
2439 ssize_t result = 0;
2440 int r;
2441
2442 if (size & 0x3 || *pos & 0x3)
2443 return -EINVAL;
2444
2445 while (size) {
2446 uint32_t value;
2447
2448 value = RREG32_DIDT(*pos >> 2);
2449 r = put_user(value, (uint32_t *)buf);
2450 if (r)
2451 return r;
2452
2453 result += 4;
2454 buf += 4;
2455 *pos += 4;
2456 size -= 4;
2457 }
2458
2459 return result;
2460}
2461
2462static ssize_t amdgpu_debugfs_regs_didt_write(struct file *f, const char __user *buf,
2463 size_t size, loff_t *pos)
2464{
2465 struct amdgpu_device *adev = f->f_inode->i_private;
2466 ssize_t result = 0;
2467 int r;
2468
2469 if (size & 0x3 || *pos & 0x3)
2470 return -EINVAL;
2471
2472 while (size) {
2473 uint32_t value;
2474
2475 r = get_user(value, (uint32_t *)buf);
2476 if (r)
2477 return r;
2478
2479 WREG32_DIDT(*pos >> 2, value);
2480
2481 result += 4;
2482 buf += 4;
2483 *pos += 4;
2484 size -= 4;
2485 }
2486
2487 return result;
2488}
2489
2490static ssize_t amdgpu_debugfs_regs_smc_read(struct file *f, char __user *buf,
2491 size_t size, loff_t *pos)
2492{
2493 struct amdgpu_device *adev = f->f_inode->i_private;
2494 ssize_t result = 0;
2495 int r;
2496
2497 if (size & 0x3 || *pos & 0x3)
2498 return -EINVAL;
2499
2500 while (size) {
2501 uint32_t value;
2502
2503 value = RREG32_SMC(*pos >> 2);
2504 r = put_user(value, (uint32_t *)buf);
2505 if (r)
2506 return r;
2507
2508 result += 4;
2509 buf += 4;
2510 *pos += 4;
2511 size -= 4;
2512 }
2513
2514 return result;
2515}
2516
2517static ssize_t amdgpu_debugfs_regs_smc_write(struct file *f, const char __user *buf,
2518 size_t size, loff_t *pos)
2519{
2520 struct amdgpu_device *adev = f->f_inode->i_private;
2521 ssize_t result = 0;
2522 int r;
2523
2524 if (size & 0x3 || *pos & 0x3)
2525 return -EINVAL;
2526
2527 while (size) {
2528 uint32_t value;
2529
2530 r = get_user(value, (uint32_t *)buf);
2531 if (r)
2532 return r;
2533
2534 WREG32_SMC(*pos >> 2, value);
2535
2536 result += 4;
2537 buf += 4;
2538 *pos += 4;
2539 size -= 4;
2540 }
2541
2542 return result;
2543}
2544
1e051413
TSD
2545static ssize_t amdgpu_debugfs_gca_config_read(struct file *f, char __user *buf,
2546 size_t size, loff_t *pos)
2547{
2548 struct amdgpu_device *adev = f->f_inode->i_private;
2549 ssize_t result = 0;
2550 int r;
2551 uint32_t *config, no_regs = 0;
2552
2553 if (size & 0x3 || *pos & 0x3)
2554 return -EINVAL;
2555
2556 config = kmalloc(256 * sizeof(*config), GFP_KERNEL);
2557 if (!config)
2558 return -ENOMEM;
2559
2560 /* version, increment each time something is added */
2561 config[no_regs++] = 0;
2562 config[no_regs++] = adev->gfx.config.max_shader_engines;
2563 config[no_regs++] = adev->gfx.config.max_tile_pipes;
2564 config[no_regs++] = adev->gfx.config.max_cu_per_sh;
2565 config[no_regs++] = adev->gfx.config.max_sh_per_se;
2566 config[no_regs++] = adev->gfx.config.max_backends_per_se;
2567 config[no_regs++] = adev->gfx.config.max_texture_channel_caches;
2568 config[no_regs++] = adev->gfx.config.max_gprs;
2569 config[no_regs++] = adev->gfx.config.max_gs_threads;
2570 config[no_regs++] = adev->gfx.config.max_hw_contexts;
2571 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_frontend;
2572 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_backend;
2573 config[no_regs++] = adev->gfx.config.sc_hiz_tile_fifo_size;
2574 config[no_regs++] = adev->gfx.config.sc_earlyz_tile_fifo_size;
2575 config[no_regs++] = adev->gfx.config.num_tile_pipes;
2576 config[no_regs++] = adev->gfx.config.backend_enable_mask;
2577 config[no_regs++] = adev->gfx.config.mem_max_burst_length_bytes;
2578 config[no_regs++] = adev->gfx.config.mem_row_size_in_kb;
2579 config[no_regs++] = adev->gfx.config.shader_engine_tile_size;
2580 config[no_regs++] = adev->gfx.config.num_gpus;
2581 config[no_regs++] = adev->gfx.config.multi_gpu_tile_size;
2582 config[no_regs++] = adev->gfx.config.mc_arb_ramcfg;
2583 config[no_regs++] = adev->gfx.config.gb_addr_config;
2584 config[no_regs++] = adev->gfx.config.num_rbs;
2585
2586 while (size && (*pos < no_regs * 4)) {
2587 uint32_t value;
2588
2589 value = config[*pos >> 2];
2590 r = put_user(value, (uint32_t *)buf);
2591 if (r) {
2592 kfree(config);
2593 return r;
2594 }
2595
2596 result += 4;
2597 buf += 4;
2598 *pos += 4;
2599 size -= 4;
2600 }
2601
2602 kfree(config);
2603 return result;
2604}
2605
2606
d38ceaf9
AD
2607static const struct file_operations amdgpu_debugfs_regs_fops = {
2608 .owner = THIS_MODULE,
2609 .read = amdgpu_debugfs_regs_read,
2610 .write = amdgpu_debugfs_regs_write,
2611 .llseek = default_llseek
2612};
adcec288
TSD
2613static const struct file_operations amdgpu_debugfs_regs_didt_fops = {
2614 .owner = THIS_MODULE,
2615 .read = amdgpu_debugfs_regs_didt_read,
2616 .write = amdgpu_debugfs_regs_didt_write,
2617 .llseek = default_llseek
2618};
2619static const struct file_operations amdgpu_debugfs_regs_pcie_fops = {
2620 .owner = THIS_MODULE,
2621 .read = amdgpu_debugfs_regs_pcie_read,
2622 .write = amdgpu_debugfs_regs_pcie_write,
2623 .llseek = default_llseek
2624};
2625static const struct file_operations amdgpu_debugfs_regs_smc_fops = {
2626 .owner = THIS_MODULE,
2627 .read = amdgpu_debugfs_regs_smc_read,
2628 .write = amdgpu_debugfs_regs_smc_write,
2629 .llseek = default_llseek
2630};
2631
1e051413
TSD
2632static const struct file_operations amdgpu_debugfs_gca_config_fops = {
2633 .owner = THIS_MODULE,
2634 .read = amdgpu_debugfs_gca_config_read,
2635 .llseek = default_llseek
2636};
2637
adcec288
TSD
2638static const struct file_operations *debugfs_regs[] = {
2639 &amdgpu_debugfs_regs_fops,
2640 &amdgpu_debugfs_regs_didt_fops,
2641 &amdgpu_debugfs_regs_pcie_fops,
2642 &amdgpu_debugfs_regs_smc_fops,
1e051413 2643 &amdgpu_debugfs_gca_config_fops,
adcec288
TSD
2644};
2645
2646static const char *debugfs_regs_names[] = {
2647 "amdgpu_regs",
2648 "amdgpu_regs_didt",
2649 "amdgpu_regs_pcie",
2650 "amdgpu_regs_smc",
1e051413 2651 "amdgpu_gca_config",
adcec288 2652};
d38ceaf9
AD
2653
2654static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
2655{
2656 struct drm_minor *minor = adev->ddev->primary;
2657 struct dentry *ent, *root = minor->debugfs_root;
adcec288
TSD
2658 unsigned i, j;
2659
2660 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
2661 ent = debugfs_create_file(debugfs_regs_names[i],
2662 S_IFREG | S_IRUGO, root,
2663 adev, debugfs_regs[i]);
2664 if (IS_ERR(ent)) {
2665 for (j = 0; j < i; j++) {
2666 debugfs_remove(adev->debugfs_regs[i]);
2667 adev->debugfs_regs[i] = NULL;
2668 }
2669 return PTR_ERR(ent);
2670 }
d38ceaf9 2671
adcec288
TSD
2672 if (!i)
2673 i_size_write(ent->d_inode, adev->rmmio_size);
2674 adev->debugfs_regs[i] = ent;
2675 }
d38ceaf9
AD
2676
2677 return 0;
2678}
2679
2680static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev)
2681{
adcec288
TSD
2682 unsigned i;
2683
2684 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
2685 if (adev->debugfs_regs[i]) {
2686 debugfs_remove(adev->debugfs_regs[i]);
2687 adev->debugfs_regs[i] = NULL;
2688 }
2689 }
d38ceaf9
AD
2690}
2691
2692int amdgpu_debugfs_init(struct drm_minor *minor)
2693{
2694 return 0;
2695}
2696
2697void amdgpu_debugfs_cleanup(struct drm_minor *minor)
2698{
2699}
7cebc728
AK
2700#else
2701static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
2702{
2703 return 0;
2704}
2705static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev) { }
d38ceaf9 2706#endif