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