blob: 27bfee2c7cc57c468fc58b28fa4874e0726f9bad [file] [log] [blame]
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001/*
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 */
Chunming Zhou0875dc92016-06-12 15:41:58 +080028#include <linux/kthread.h>
Alex Deucherd38ceaf2015-04-20 16:55:21 -040029#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"
Tom St Denisf4b373f2016-05-31 08:02:27 -040039#include "amdgpu_trace.h"
Alex Deucherd38ceaf2015-04-20 16:55:21 -040040#include "amdgpu_i2c.h"
41#include "atom.h"
42#include "amdgpu_atombios.h"
Alex Deucherd0dd7f02015-11-11 19:45:06 -050043#include "amd_pcie.h"
Alex Deuchera2e73f52015-04-20 17:09:27 -040044#ifdef CONFIG_DRM_AMDGPU_CIK
45#include "cik.h"
46#endif
Alex Deucheraaa36a92015-04-20 17:31:14 -040047#include "vi.h"
Alex Deucherd38ceaf2015-04-20 16:55:21 -040048#include "bif/bif_4_1_d.h"
Emily Deng9accf2f2016-08-10 16:01:25 +080049#include <linux/pci.h>
Alex Deucherd38ceaf2015-04-20 16:55:21 -040050
51static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev);
52static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev);
53
54static const char *amdgpu_asic_name[] = {
55 "BONAIRE",
56 "KAVERI",
57 "KABINI",
58 "HAWAII",
59 "MULLINS",
60 "TOPAZ",
61 "TONGA",
David Zhang48299f92015-07-08 01:05:16 +080062 "FIJI",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040063 "CARRIZO",
Samuel Li139f4912015-10-08 14:50:27 -040064 "STONEY",
Flora Cui2cc0c0b2016-03-14 18:33:29 -040065 "POLARIS10",
66 "POLARIS11",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040067 "LAST",
68};
69
70bool amdgpu_device_is_px(struct drm_device *dev)
71{
72 struct amdgpu_device *adev = dev->dev_private;
73
Jammy Zhou2f7d10b2015-07-22 11:29:01 +080074 if (adev->flags & AMD_IS_PX)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040075 return true;
76 return false;
77}
78
79/*
80 * MMIO register access helper functions.
81 */
82uint32_t amdgpu_mm_rreg(struct amdgpu_device *adev, uint32_t reg,
83 bool always_indirect)
84{
Tom St Denisf4b373f2016-05-31 08:02:27 -040085 uint32_t ret;
86
Alex Deucherd38ceaf2015-04-20 16:55:21 -040087 if ((reg * 4) < adev->rmmio_size && !always_indirect)
Tom St Denisf4b373f2016-05-31 08:02:27 -040088 ret = readl(((void __iomem *)adev->rmmio) + (reg * 4));
Alex Deucherd38ceaf2015-04-20 16:55:21 -040089 else {
90 unsigned long flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040091
92 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
93 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
94 ret = readl(((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
95 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040096 }
Tom St Denisf4b373f2016-05-31 08:02:27 -040097 trace_amdgpu_mm_rreg(adev->pdev->device, reg, ret);
98 return ret;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040099}
100
101void amdgpu_mm_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v,
102 bool always_indirect)
103{
Tom St Denisf4b373f2016-05-31 08:02:27 -0400104 trace_amdgpu_mm_wreg(adev->pdev->device, reg, v);
105
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400106 if ((reg * 4) < adev->rmmio_size && !always_indirect)
107 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
108 else {
109 unsigned long flags;
110
111 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
112 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
113 writel(v, ((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
114 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
115 }
116}
117
118u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 reg)
119{
120 if ((reg * 4) < adev->rio_mem_size)
121 return ioread32(adev->rio_mem + (reg * 4));
122 else {
123 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
124 return ioread32(adev->rio_mem + (mmMM_DATA * 4));
125 }
126}
127
128void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
129{
130
131 if ((reg * 4) < adev->rio_mem_size)
132 iowrite32(v, adev->rio_mem + (reg * 4));
133 else {
134 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
135 iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
136 }
137}
138
139/**
140 * amdgpu_mm_rdoorbell - read a doorbell dword
141 *
142 * @adev: amdgpu_device pointer
143 * @index: doorbell index
144 *
145 * Returns the value in the doorbell aperture at the
146 * requested doorbell index (CIK).
147 */
148u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
149{
150 if (index < adev->doorbell.num_doorbells) {
151 return readl(adev->doorbell.ptr + index);
152 } else {
153 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
154 return 0;
155 }
156}
157
158/**
159 * amdgpu_mm_wdoorbell - write a doorbell dword
160 *
161 * @adev: amdgpu_device pointer
162 * @index: doorbell index
163 * @v: value to write
164 *
165 * Writes @v to the doorbell aperture at the
166 * requested doorbell index (CIK).
167 */
168void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
169{
170 if (index < adev->doorbell.num_doorbells) {
171 writel(v, adev->doorbell.ptr + index);
172 } else {
173 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
174 }
175}
176
177/**
178 * amdgpu_invalid_rreg - dummy reg read function
179 *
180 * @adev: amdgpu device pointer
181 * @reg: offset of register
182 *
183 * Dummy register read function. Used for register blocks
184 * that certain asics don't have (all asics).
185 * Returns the value in the register.
186 */
187static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
188{
189 DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
190 BUG();
191 return 0;
192}
193
194/**
195 * amdgpu_invalid_wreg - dummy reg write function
196 *
197 * @adev: amdgpu device pointer
198 * @reg: offset of register
199 * @v: value to write to the register
200 *
201 * Dummy register read function. Used for register blocks
202 * that certain asics don't have (all asics).
203 */
204static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
205{
206 DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
207 reg, v);
208 BUG();
209}
210
211/**
212 * amdgpu_block_invalid_rreg - dummy reg read function
213 *
214 * @adev: amdgpu device pointer
215 * @block: offset of instance
216 * @reg: offset of register
217 *
218 * Dummy register read function. Used for register blocks
219 * that certain asics don't have (all asics).
220 * Returns the value in the register.
221 */
222static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
223 uint32_t block, uint32_t reg)
224{
225 DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
226 reg, block);
227 BUG();
228 return 0;
229}
230
231/**
232 * amdgpu_block_invalid_wreg - dummy reg write function
233 *
234 * @adev: amdgpu device pointer
235 * @block: offset of instance
236 * @reg: offset of register
237 * @v: value to write to the register
238 *
239 * Dummy register read function. Used for register blocks
240 * that certain asics don't have (all asics).
241 */
242static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
243 uint32_t block,
244 uint32_t reg, uint32_t v)
245{
246 DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
247 reg, block, v);
248 BUG();
249}
250
251static int amdgpu_vram_scratch_init(struct amdgpu_device *adev)
252{
253 int r;
254
255 if (adev->vram_scratch.robj == NULL) {
256 r = amdgpu_bo_create(adev, AMDGPU_GPU_PAGE_SIZE,
Alex Deucher857d9132015-08-27 00:14:16 -0400257 PAGE_SIZE, true, AMDGPU_GEM_DOMAIN_VRAM,
258 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED,
Christian König72d76682015-09-03 17:34:59 +0200259 NULL, NULL, &adev->vram_scratch.robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400260 if (r) {
261 return r;
262 }
263 }
264
265 r = amdgpu_bo_reserve(adev->vram_scratch.robj, false);
266 if (unlikely(r != 0))
267 return r;
268 r = amdgpu_bo_pin(adev->vram_scratch.robj,
269 AMDGPU_GEM_DOMAIN_VRAM, &adev->vram_scratch.gpu_addr);
270 if (r) {
271 amdgpu_bo_unreserve(adev->vram_scratch.robj);
272 return r;
273 }
274 r = amdgpu_bo_kmap(adev->vram_scratch.robj,
275 (void **)&adev->vram_scratch.ptr);
276 if (r)
277 amdgpu_bo_unpin(adev->vram_scratch.robj);
278 amdgpu_bo_unreserve(adev->vram_scratch.robj);
279
280 return r;
281}
282
283static void amdgpu_vram_scratch_fini(struct amdgpu_device *adev)
284{
285 int r;
286
287 if (adev->vram_scratch.robj == NULL) {
288 return;
289 }
290 r = amdgpu_bo_reserve(adev->vram_scratch.robj, false);
291 if (likely(r == 0)) {
292 amdgpu_bo_kunmap(adev->vram_scratch.robj);
293 amdgpu_bo_unpin(adev->vram_scratch.robj);
294 amdgpu_bo_unreserve(adev->vram_scratch.robj);
295 }
296 amdgpu_bo_unref(&adev->vram_scratch.robj);
297}
298
299/**
300 * amdgpu_program_register_sequence - program an array of registers.
301 *
302 * @adev: amdgpu_device pointer
303 * @registers: pointer to the register array
304 * @array_size: size of the register array
305 *
306 * Programs an array or registers with and and or masks.
307 * This is a helper for setting golden registers.
308 */
309void amdgpu_program_register_sequence(struct amdgpu_device *adev,
310 const u32 *registers,
311 const u32 array_size)
312{
313 u32 tmp, reg, and_mask, or_mask;
314 int i;
315
316 if (array_size % 3)
317 return;
318
319 for (i = 0; i < array_size; i +=3) {
320 reg = registers[i + 0];
321 and_mask = registers[i + 1];
322 or_mask = registers[i + 2];
323
324 if (and_mask == 0xffffffff) {
325 tmp = or_mask;
326 } else {
327 tmp = RREG32(reg);
328 tmp &= ~and_mask;
329 tmp |= or_mask;
330 }
331 WREG32(reg, tmp);
332 }
333}
334
335void amdgpu_pci_config_reset(struct amdgpu_device *adev)
336{
337 pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
338}
339
340/*
341 * GPU doorbell aperture helpers function.
342 */
343/**
344 * amdgpu_doorbell_init - Init doorbell driver information.
345 *
346 * @adev: amdgpu_device pointer
347 *
348 * Init doorbell driver information (CIK)
349 * Returns 0 on success, error on failure.
350 */
351static int amdgpu_doorbell_init(struct amdgpu_device *adev)
352{
353 /* doorbell bar mapping */
354 adev->doorbell.base = pci_resource_start(adev->pdev, 2);
355 adev->doorbell.size = pci_resource_len(adev->pdev, 2);
356
Christian Königedf600d2016-05-03 15:54:54 +0200357 adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400358 AMDGPU_DOORBELL_MAX_ASSIGNMENT+1);
359 if (adev->doorbell.num_doorbells == 0)
360 return -EINVAL;
361
362 adev->doorbell.ptr = ioremap(adev->doorbell.base, adev->doorbell.num_doorbells * sizeof(u32));
363 if (adev->doorbell.ptr == NULL) {
364 return -ENOMEM;
365 }
366 DRM_INFO("doorbell mmio base: 0x%08X\n", (uint32_t)adev->doorbell.base);
367 DRM_INFO("doorbell mmio size: %u\n", (unsigned)adev->doorbell.size);
368
369 return 0;
370}
371
372/**
373 * amdgpu_doorbell_fini - Tear down doorbell driver information.
374 *
375 * @adev: amdgpu_device pointer
376 *
377 * Tear down doorbell driver information (CIK)
378 */
379static void amdgpu_doorbell_fini(struct amdgpu_device *adev)
380{
381 iounmap(adev->doorbell.ptr);
382 adev->doorbell.ptr = NULL;
383}
384
385/**
386 * amdgpu_doorbell_get_kfd_info - Report doorbell configuration required to
387 * setup amdkfd
388 *
389 * @adev: amdgpu_device pointer
390 * @aperture_base: output returning doorbell aperture base physical address
391 * @aperture_size: output returning doorbell aperture size in bytes
392 * @start_offset: output returning # of doorbell bytes reserved for amdgpu.
393 *
394 * amdgpu and amdkfd share the doorbell aperture. amdgpu sets it up,
395 * takes doorbells required for its own rings and reports the setup to amdkfd.
396 * amdgpu reserved doorbells are at the start of the doorbell aperture.
397 */
398void amdgpu_doorbell_get_kfd_info(struct amdgpu_device *adev,
399 phys_addr_t *aperture_base,
400 size_t *aperture_size,
401 size_t *start_offset)
402{
403 /*
404 * The first num_doorbells are used by amdgpu.
405 * amdkfd takes whatever's left in the aperture.
406 */
407 if (adev->doorbell.size > adev->doorbell.num_doorbells * sizeof(u32)) {
408 *aperture_base = adev->doorbell.base;
409 *aperture_size = adev->doorbell.size;
410 *start_offset = adev->doorbell.num_doorbells * sizeof(u32);
411 } else {
412 *aperture_base = 0;
413 *aperture_size = 0;
414 *start_offset = 0;
415 }
416}
417
418/*
419 * amdgpu_wb_*()
420 * Writeback is the the method by which the the GPU updates special pages
421 * in memory with the status of certain GPU events (fences, ring pointers,
422 * etc.).
423 */
424
425/**
426 * amdgpu_wb_fini - Disable Writeback and free memory
427 *
428 * @adev: amdgpu_device pointer
429 *
430 * Disables Writeback and frees the Writeback memory (all asics).
431 * Used at driver shutdown.
432 */
433static void amdgpu_wb_fini(struct amdgpu_device *adev)
434{
435 if (adev->wb.wb_obj) {
436 if (!amdgpu_bo_reserve(adev->wb.wb_obj, false)) {
437 amdgpu_bo_kunmap(adev->wb.wb_obj);
438 amdgpu_bo_unpin(adev->wb.wb_obj);
439 amdgpu_bo_unreserve(adev->wb.wb_obj);
440 }
441 amdgpu_bo_unref(&adev->wb.wb_obj);
442 adev->wb.wb = NULL;
443 adev->wb.wb_obj = NULL;
444 }
445}
446
447/**
448 * amdgpu_wb_init- Init Writeback driver info and allocate memory
449 *
450 * @adev: amdgpu_device pointer
451 *
452 * Disables Writeback and frees the Writeback memory (all asics).
453 * Used at driver startup.
454 * Returns 0 on success or an -error on failure.
455 */
456static int amdgpu_wb_init(struct amdgpu_device *adev)
457{
458 int r;
459
460 if (adev->wb.wb_obj == NULL) {
461 r = amdgpu_bo_create(adev, AMDGPU_MAX_WB * 4, PAGE_SIZE, true,
Christian König72d76682015-09-03 17:34:59 +0200462 AMDGPU_GEM_DOMAIN_GTT, 0, NULL, NULL,
463 &adev->wb.wb_obj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400464 if (r) {
465 dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
466 return r;
467 }
468 r = amdgpu_bo_reserve(adev->wb.wb_obj, false);
469 if (unlikely(r != 0)) {
470 amdgpu_wb_fini(adev);
471 return r;
472 }
473 r = amdgpu_bo_pin(adev->wb.wb_obj, AMDGPU_GEM_DOMAIN_GTT,
474 &adev->wb.gpu_addr);
475 if (r) {
476 amdgpu_bo_unreserve(adev->wb.wb_obj);
477 dev_warn(adev->dev, "(%d) pin WB bo failed\n", r);
478 amdgpu_wb_fini(adev);
479 return r;
480 }
481 r = amdgpu_bo_kmap(adev->wb.wb_obj, (void **)&adev->wb.wb);
482 amdgpu_bo_unreserve(adev->wb.wb_obj);
483 if (r) {
484 dev_warn(adev->dev, "(%d) map WB bo failed\n", r);
485 amdgpu_wb_fini(adev);
486 return r;
487 }
488
489 adev->wb.num_wb = AMDGPU_MAX_WB;
490 memset(&adev->wb.used, 0, sizeof(adev->wb.used));
491
492 /* clear wb memory */
493 memset((char *)adev->wb.wb, 0, AMDGPU_GPU_PAGE_SIZE);
494 }
495
496 return 0;
497}
498
499/**
500 * amdgpu_wb_get - Allocate a wb entry
501 *
502 * @adev: amdgpu_device pointer
503 * @wb: wb index
504 *
505 * Allocate a wb slot for use by the driver (all asics).
506 * Returns 0 on success or -EINVAL on failure.
507 */
508int amdgpu_wb_get(struct amdgpu_device *adev, u32 *wb)
509{
510 unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
511 if (offset < adev->wb.num_wb) {
512 __set_bit(offset, adev->wb.used);
513 *wb = offset;
514 return 0;
515 } else {
516 return -EINVAL;
517 }
518}
519
520/**
521 * amdgpu_wb_free - Free a wb entry
522 *
523 * @adev: amdgpu_device pointer
524 * @wb: wb index
525 *
526 * Free a wb slot allocated for use by the driver (all asics)
527 */
528void amdgpu_wb_free(struct amdgpu_device *adev, u32 wb)
529{
530 if (wb < adev->wb.num_wb)
531 __clear_bit(wb, adev->wb.used);
532}
533
534/**
535 * amdgpu_vram_location - try to find VRAM location
536 * @adev: amdgpu device structure holding all necessary informations
537 * @mc: memory controller structure holding memory informations
538 * @base: base address at which to put VRAM
539 *
540 * Function will place try to place VRAM at base address provided
541 * as parameter (which is so far either PCI aperture address or
542 * for IGP TOM base address).
543 *
544 * If there is not enough space to fit the unvisible VRAM in the 32bits
545 * address space then we limit the VRAM size to the aperture.
546 *
547 * Note: We don't explicitly enforce VRAM start to be aligned on VRAM size,
548 * this shouldn't be a problem as we are using the PCI aperture as a reference.
549 * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but
550 * not IGP.
551 *
552 * Note: we use mc_vram_size as on some board we need to program the mc to
553 * cover the whole aperture even if VRAM size is inferior to aperture size
554 * Novell bug 204882 + along with lots of ubuntu ones
555 *
556 * Note: when limiting vram it's safe to overwritte real_vram_size because
557 * we are not in case where real_vram_size is inferior to mc_vram_size (ie
558 * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu
559 * ones)
560 *
561 * Note: IGP TOM addr should be the same as the aperture addr, we don't
562 * explicitly check for that thought.
563 *
564 * FIXME: when reducing VRAM size align new size on power of 2.
565 */
566void amdgpu_vram_location(struct amdgpu_device *adev, struct amdgpu_mc *mc, u64 base)
567{
568 uint64_t limit = (uint64_t)amdgpu_vram_limit << 20;
569
570 mc->vram_start = base;
571 if (mc->mc_vram_size > (adev->mc.mc_mask - base + 1)) {
572 dev_warn(adev->dev, "limiting VRAM to PCI aperture size\n");
573 mc->real_vram_size = mc->aper_size;
574 mc->mc_vram_size = mc->aper_size;
575 }
576 mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
577 if (limit && limit < mc->real_vram_size)
578 mc->real_vram_size = limit;
579 dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
580 mc->mc_vram_size >> 20, mc->vram_start,
581 mc->vram_end, mc->real_vram_size >> 20);
582}
583
584/**
585 * amdgpu_gtt_location - try to find GTT location
586 * @adev: amdgpu device structure holding all necessary informations
587 * @mc: memory controller structure holding memory informations
588 *
589 * Function will place try to place GTT before or after VRAM.
590 *
591 * If GTT size is bigger than space left then we ajust GTT size.
592 * Thus function will never fails.
593 *
594 * FIXME: when reducing GTT size align new size on power of 2.
595 */
596void amdgpu_gtt_location(struct amdgpu_device *adev, struct amdgpu_mc *mc)
597{
598 u64 size_af, size_bf;
599
600 size_af = ((adev->mc.mc_mask - mc->vram_end) + mc->gtt_base_align) & ~mc->gtt_base_align;
601 size_bf = mc->vram_start & ~mc->gtt_base_align;
602 if (size_bf > size_af) {
603 if (mc->gtt_size > size_bf) {
604 dev_warn(adev->dev, "limiting GTT\n");
605 mc->gtt_size = size_bf;
606 }
607 mc->gtt_start = (mc->vram_start & ~mc->gtt_base_align) - mc->gtt_size;
608 } else {
609 if (mc->gtt_size > size_af) {
610 dev_warn(adev->dev, "limiting GTT\n");
611 mc->gtt_size = size_af;
612 }
613 mc->gtt_start = (mc->vram_end + 1 + mc->gtt_base_align) & ~mc->gtt_base_align;
614 }
615 mc->gtt_end = mc->gtt_start + mc->gtt_size - 1;
616 dev_info(adev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
617 mc->gtt_size >> 20, mc->gtt_start, mc->gtt_end);
618}
619
620/*
621 * GPU helpers function.
622 */
623/**
624 * amdgpu_card_posted - check if the hw has already been initialized
625 *
626 * @adev: amdgpu_device pointer
627 *
628 * Check if the asic has been initialized (all asics).
629 * Used at driver startup.
630 * Returns true if initialized or false if not.
631 */
632bool amdgpu_card_posted(struct amdgpu_device *adev)
633{
634 uint32_t reg;
635
636 /* then check MEM_SIZE, in case the crtcs are off */
637 reg = RREG32(mmCONFIG_MEMSIZE);
638
639 if (reg)
640 return true;
641
642 return false;
643
644}
645
646/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400647 * amdgpu_dummy_page_init - init dummy page used by the driver
648 *
649 * @adev: amdgpu_device pointer
650 *
651 * Allocate the dummy page used by the driver (all asics).
652 * This dummy page is used by the driver as a filler for gart entries
653 * when pages are taken out of the GART
654 * Returns 0 on sucess, -ENOMEM on failure.
655 */
656int amdgpu_dummy_page_init(struct amdgpu_device *adev)
657{
658 if (adev->dummy_page.page)
659 return 0;
660 adev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
661 if (adev->dummy_page.page == NULL)
662 return -ENOMEM;
663 adev->dummy_page.addr = pci_map_page(adev->pdev, adev->dummy_page.page,
664 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
665 if (pci_dma_mapping_error(adev->pdev, adev->dummy_page.addr)) {
666 dev_err(&adev->pdev->dev, "Failed to DMA MAP the dummy page\n");
667 __free_page(adev->dummy_page.page);
668 adev->dummy_page.page = NULL;
669 return -ENOMEM;
670 }
671 return 0;
672}
673
674/**
675 * amdgpu_dummy_page_fini - free dummy page used by the driver
676 *
677 * @adev: amdgpu_device pointer
678 *
679 * Frees the dummy page used by the driver (all asics).
680 */
681void amdgpu_dummy_page_fini(struct amdgpu_device *adev)
682{
683 if (adev->dummy_page.page == NULL)
684 return;
685 pci_unmap_page(adev->pdev, adev->dummy_page.addr,
686 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
687 __free_page(adev->dummy_page.page);
688 adev->dummy_page.page = NULL;
689}
690
691
692/* ATOM accessor methods */
693/*
694 * ATOM is an interpreted byte code stored in tables in the vbios. The
695 * driver registers callbacks to access registers and the interpreter
696 * in the driver parses the tables and executes then to program specific
697 * actions (set display modes, asic init, etc.). See amdgpu_atombios.c,
698 * atombios.h, and atom.c
699 */
700
701/**
702 * cail_pll_read - read PLL register
703 *
704 * @info: atom card_info pointer
705 * @reg: PLL register offset
706 *
707 * Provides a PLL register accessor for the atom interpreter (r4xx+).
708 * Returns the value of the PLL register.
709 */
710static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
711{
712 return 0;
713}
714
715/**
716 * cail_pll_write - write PLL register
717 *
718 * @info: atom card_info pointer
719 * @reg: PLL register offset
720 * @val: value to write to the pll register
721 *
722 * Provides a PLL register accessor for the atom interpreter (r4xx+).
723 */
724static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
725{
726
727}
728
729/**
730 * cail_mc_read - read MC (Memory Controller) register
731 *
732 * @info: atom card_info pointer
733 * @reg: MC register offset
734 *
735 * Provides an MC register accessor for the atom interpreter (r4xx+).
736 * Returns the value of the MC register.
737 */
738static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
739{
740 return 0;
741}
742
743/**
744 * cail_mc_write - write MC (Memory Controller) register
745 *
746 * @info: atom card_info pointer
747 * @reg: MC register offset
748 * @val: value to write to the pll register
749 *
750 * Provides a MC register accessor for the atom interpreter (r4xx+).
751 */
752static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
753{
754
755}
756
757/**
758 * cail_reg_write - write MMIO register
759 *
760 * @info: atom card_info pointer
761 * @reg: MMIO register offset
762 * @val: value to write to the pll register
763 *
764 * Provides a MMIO register accessor for the atom interpreter (r4xx+).
765 */
766static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
767{
768 struct amdgpu_device *adev = info->dev->dev_private;
769
770 WREG32(reg, val);
771}
772
773/**
774 * cail_reg_read - read MMIO register
775 *
776 * @info: atom card_info pointer
777 * @reg: MMIO register offset
778 *
779 * Provides an MMIO register accessor for the atom interpreter (r4xx+).
780 * Returns the value of the MMIO register.
781 */
782static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
783{
784 struct amdgpu_device *adev = info->dev->dev_private;
785 uint32_t r;
786
787 r = RREG32(reg);
788 return r;
789}
790
791/**
792 * cail_ioreg_write - write IO register
793 *
794 * @info: atom card_info pointer
795 * @reg: IO register offset
796 * @val: value to write to the pll register
797 *
798 * Provides a IO register accessor for the atom interpreter (r4xx+).
799 */
800static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
801{
802 struct amdgpu_device *adev = info->dev->dev_private;
803
804 WREG32_IO(reg, val);
805}
806
807/**
808 * cail_ioreg_read - read IO register
809 *
810 * @info: atom card_info pointer
811 * @reg: IO register offset
812 *
813 * Provides an IO register accessor for the atom interpreter (r4xx+).
814 * Returns the value of the IO register.
815 */
816static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
817{
818 struct amdgpu_device *adev = info->dev->dev_private;
819 uint32_t r;
820
821 r = RREG32_IO(reg);
822 return r;
823}
824
825/**
826 * amdgpu_atombios_fini - free the driver info and callbacks for atombios
827 *
828 * @adev: amdgpu_device pointer
829 *
830 * Frees the driver info and register access callbacks for the ATOM
831 * interpreter (r4xx+).
832 * Called at driver shutdown.
833 */
834static void amdgpu_atombios_fini(struct amdgpu_device *adev)
835{
Monk Liu89e0ec9f2016-05-27 19:34:11 +0800836 if (adev->mode_info.atom_context) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400837 kfree(adev->mode_info.atom_context->scratch);
Monk Liu89e0ec9f2016-05-27 19:34:11 +0800838 kfree(adev->mode_info.atom_context->iio);
839 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400840 kfree(adev->mode_info.atom_context);
841 adev->mode_info.atom_context = NULL;
842 kfree(adev->mode_info.atom_card_info);
843 adev->mode_info.atom_card_info = NULL;
844}
845
846/**
847 * amdgpu_atombios_init - init the driver info and callbacks for atombios
848 *
849 * @adev: amdgpu_device pointer
850 *
851 * Initializes the driver info and register access callbacks for the
852 * ATOM interpreter (r4xx+).
853 * Returns 0 on sucess, -ENOMEM on failure.
854 * Called at driver startup.
855 */
856static int amdgpu_atombios_init(struct amdgpu_device *adev)
857{
858 struct card_info *atom_card_info =
859 kzalloc(sizeof(struct card_info), GFP_KERNEL);
860
861 if (!atom_card_info)
862 return -ENOMEM;
863
864 adev->mode_info.atom_card_info = atom_card_info;
865 atom_card_info->dev = adev->ddev;
866 atom_card_info->reg_read = cail_reg_read;
867 atom_card_info->reg_write = cail_reg_write;
868 /* needed for iio ops */
869 if (adev->rio_mem) {
870 atom_card_info->ioreg_read = cail_ioreg_read;
871 atom_card_info->ioreg_write = cail_ioreg_write;
872 } else {
873 DRM_ERROR("Unable to find PCI I/O BAR; using MMIO for ATOM IIO\n");
874 atom_card_info->ioreg_read = cail_reg_read;
875 atom_card_info->ioreg_write = cail_reg_write;
876 }
877 atom_card_info->mc_read = cail_mc_read;
878 atom_card_info->mc_write = cail_mc_write;
879 atom_card_info->pll_read = cail_pll_read;
880 atom_card_info->pll_write = cail_pll_write;
881
882 adev->mode_info.atom_context = amdgpu_atom_parse(atom_card_info, adev->bios);
883 if (!adev->mode_info.atom_context) {
884 amdgpu_atombios_fini(adev);
885 return -ENOMEM;
886 }
887
888 mutex_init(&adev->mode_info.atom_context->mutex);
889 amdgpu_atombios_scratch_regs_init(adev);
890 amdgpu_atom_allocate_fb_scratch(adev->mode_info.atom_context);
891 return 0;
892}
893
894/* if we get transitioned to only one device, take VGA back */
895/**
896 * amdgpu_vga_set_decode - enable/disable vga decode
897 *
898 * @cookie: amdgpu_device pointer
899 * @state: enable/disable vga decode
900 *
901 * Enable/disable vga decode (all asics).
902 * Returns VGA resource flags.
903 */
904static unsigned int amdgpu_vga_set_decode(void *cookie, bool state)
905{
906 struct amdgpu_device *adev = cookie;
907 amdgpu_asic_set_vga_state(adev, state);
908 if (state)
909 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
910 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
911 else
912 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
913}
914
915/**
916 * amdgpu_check_pot_argument - check that argument is a power of two
917 *
918 * @arg: value to check
919 *
920 * Validates that a certain argument is a power of two (all asics).
921 * Returns true if argument is valid.
922 */
923static bool amdgpu_check_pot_argument(int arg)
924{
925 return (arg & (arg - 1)) == 0;
926}
927
928/**
929 * amdgpu_check_arguments - validate module params
930 *
931 * @adev: amdgpu_device pointer
932 *
933 * Validates certain module parameters and updates
934 * the associated values used by the driver (all asics).
935 */
936static void amdgpu_check_arguments(struct amdgpu_device *adev)
937{
Chunming Zhou5b011232015-12-10 17:34:33 +0800938 if (amdgpu_sched_jobs < 4) {
939 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
940 amdgpu_sched_jobs);
941 amdgpu_sched_jobs = 4;
942 } else if (!amdgpu_check_pot_argument(amdgpu_sched_jobs)){
943 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
944 amdgpu_sched_jobs);
945 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
946 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400947
948 if (amdgpu_gart_size != -1) {
Christian Königc4e1a132016-03-17 16:25:15 +0100949 /* gtt size must be greater or equal to 32M */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400950 if (amdgpu_gart_size < 32) {
951 dev_warn(adev->dev, "gart size (%d) too small\n",
952 amdgpu_gart_size);
953 amdgpu_gart_size = -1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400954 }
955 }
956
957 if (!amdgpu_check_pot_argument(amdgpu_vm_size)) {
958 dev_warn(adev->dev, "VM size (%d) must be a power of 2\n",
959 amdgpu_vm_size);
Alex Deucher8dacc122015-05-11 16:20:58 -0400960 amdgpu_vm_size = 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400961 }
962
963 if (amdgpu_vm_size < 1) {
964 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
965 amdgpu_vm_size);
Alex Deucher8dacc122015-05-11 16:20:58 -0400966 amdgpu_vm_size = 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400967 }
968
969 /*
970 * Max GPUVM size for Cayman, SI and CI are 40 bits.
971 */
972 if (amdgpu_vm_size > 1024) {
973 dev_warn(adev->dev, "VM size (%d) too large, max is 1TB\n",
974 amdgpu_vm_size);
Alex Deucher8dacc122015-05-11 16:20:58 -0400975 amdgpu_vm_size = 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400976 }
977
978 /* defines number of bits in page table versus page directory,
979 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
980 * page table and the remaining bits are in the page directory */
981 if (amdgpu_vm_block_size == -1) {
982
983 /* Total bits covered by PD + PTs */
984 unsigned bits = ilog2(amdgpu_vm_size) + 18;
985
986 /* Make sure the PD is 4K in size up to 8GB address space.
987 Above that split equal between PD and PTs */
988 if (amdgpu_vm_size <= 8)
989 amdgpu_vm_block_size = bits - 9;
990 else
991 amdgpu_vm_block_size = (bits + 3) / 2;
992
993 } else if (amdgpu_vm_block_size < 9) {
994 dev_warn(adev->dev, "VM page table size (%d) too small\n",
995 amdgpu_vm_block_size);
996 amdgpu_vm_block_size = 9;
997 }
998
999 if (amdgpu_vm_block_size > 24 ||
1000 (amdgpu_vm_size * 1024) < (1ull << amdgpu_vm_block_size)) {
1001 dev_warn(adev->dev, "VM page table size (%d) too large\n",
1002 amdgpu_vm_block_size);
1003 amdgpu_vm_block_size = 9;
1004 }
1005}
1006
1007/**
1008 * amdgpu_switcheroo_set_state - set switcheroo state
1009 *
1010 * @pdev: pci dev pointer
Lukas Wunner16944672015-09-05 11:17:35 +02001011 * @state: vga_switcheroo state
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001012 *
1013 * Callback for the switcheroo driver. Suspends or resumes the
1014 * the asics before or after it is powered up using ACPI methods.
1015 */
1016static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1017{
1018 struct drm_device *dev = pci_get_drvdata(pdev);
1019
1020 if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
1021 return;
1022
1023 if (state == VGA_SWITCHEROO_ON) {
1024 unsigned d3_delay = dev->pdev->d3_delay;
1025
1026 printk(KERN_INFO "amdgpu: switched on\n");
1027 /* don't suspend or resume card normally */
1028 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1029
Alex Deucher810ddc32016-08-23 13:25:49 -04001030 amdgpu_device_resume(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001031
1032 dev->pdev->d3_delay = d3_delay;
1033
1034 dev->switch_power_state = DRM_SWITCH_POWER_ON;
1035 drm_kms_helper_poll_enable(dev);
1036 } else {
1037 printk(KERN_INFO "amdgpu: switched off\n");
1038 drm_kms_helper_poll_disable(dev);
1039 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Alex Deucher810ddc32016-08-23 13:25:49 -04001040 amdgpu_device_suspend(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001041 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1042 }
1043}
1044
1045/**
1046 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1047 *
1048 * @pdev: pci dev pointer
1049 *
1050 * Callback for the switcheroo driver. Check of the switcheroo
1051 * state can be changed.
1052 * Returns true if the state can be changed, false if not.
1053 */
1054static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1055{
1056 struct drm_device *dev = pci_get_drvdata(pdev);
1057
1058 /*
1059 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1060 * locking inversion with the driver load path. And the access here is
1061 * completely racy anyway. So don't bother with locking for now.
1062 */
1063 return dev->open_count == 0;
1064}
1065
1066static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1067 .set_gpu_state = amdgpu_switcheroo_set_state,
1068 .reprobe = NULL,
1069 .can_switch = amdgpu_switcheroo_can_switch,
1070};
1071
1072int amdgpu_set_clockgating_state(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001073 enum amd_ip_block_type block_type,
1074 enum amd_clockgating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001075{
1076 int i, r = 0;
1077
1078 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001079 if (!adev->ip_block_status[i].valid)
1080 continue;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001081 if (adev->ip_blocks[i].type == block_type) {
yanyang15fc3aee2015-05-22 14:39:35 -04001082 r = adev->ip_blocks[i].funcs->set_clockgating_state((void *)adev,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001083 state);
1084 if (r)
1085 return r;
Alex Deuchera225bf12016-06-23 11:48:30 -04001086 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001087 }
1088 }
1089 return r;
1090}
1091
1092int amdgpu_set_powergating_state(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001093 enum amd_ip_block_type block_type,
1094 enum amd_powergating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001095{
1096 int i, r = 0;
1097
1098 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001099 if (!adev->ip_block_status[i].valid)
1100 continue;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001101 if (adev->ip_blocks[i].type == block_type) {
yanyang15fc3aee2015-05-22 14:39:35 -04001102 r = adev->ip_blocks[i].funcs->set_powergating_state((void *)adev,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001103 state);
1104 if (r)
1105 return r;
Alex Deuchera225bf12016-06-23 11:48:30 -04001106 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001107 }
1108 }
1109 return r;
1110}
1111
Alex Deucher5dbbb602016-06-23 11:41:04 -04001112int amdgpu_wait_for_idle(struct amdgpu_device *adev,
1113 enum amd_ip_block_type block_type)
1114{
1115 int i, r;
1116
1117 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001118 if (!adev->ip_block_status[i].valid)
1119 continue;
Alex Deucher5dbbb602016-06-23 11:41:04 -04001120 if (adev->ip_blocks[i].type == block_type) {
1121 r = adev->ip_blocks[i].funcs->wait_for_idle((void *)adev);
1122 if (r)
1123 return r;
1124 break;
1125 }
1126 }
1127 return 0;
1128
1129}
1130
1131bool amdgpu_is_idle(struct amdgpu_device *adev,
1132 enum amd_ip_block_type block_type)
1133{
1134 int i;
1135
1136 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001137 if (!adev->ip_block_status[i].valid)
1138 continue;
Alex Deucher5dbbb602016-06-23 11:41:04 -04001139 if (adev->ip_blocks[i].type == block_type)
1140 return adev->ip_blocks[i].funcs->is_idle((void *)adev);
1141 }
1142 return true;
1143
1144}
1145
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001146const struct amdgpu_ip_block_version * amdgpu_get_ip_block(
1147 struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001148 enum amd_ip_block_type type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001149{
1150 int i;
1151
1152 for (i = 0; i < adev->num_ip_blocks; i++)
1153 if (adev->ip_blocks[i].type == type)
1154 return &adev->ip_blocks[i];
1155
1156 return NULL;
1157}
1158
1159/**
1160 * amdgpu_ip_block_version_cmp
1161 *
1162 * @adev: amdgpu_device pointer
yanyang15fc3aee2015-05-22 14:39:35 -04001163 * @type: enum amd_ip_block_type
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001164 * @major: major version
1165 * @minor: minor version
1166 *
1167 * return 0 if equal or greater
1168 * return 1 if smaller or the ip_block doesn't exist
1169 */
1170int amdgpu_ip_block_version_cmp(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001171 enum amd_ip_block_type type,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001172 u32 major, u32 minor)
1173{
1174 const struct amdgpu_ip_block_version *ip_block;
1175 ip_block = amdgpu_get_ip_block(adev, type);
1176
1177 if (ip_block && ((ip_block->major > major) ||
1178 ((ip_block->major == major) &&
1179 (ip_block->minor >= minor))))
1180 return 0;
1181
1182 return 1;
1183}
1184
Emily Deng9accf2f2016-08-10 16:01:25 +08001185static void amdgpu_whether_enable_virtual_display(struct amdgpu_device *adev)
1186{
1187 adev->enable_virtual_display = false;
1188
1189 if (amdgpu_virtual_display) {
1190 struct drm_device *ddev = adev->ddev;
1191 const char *pci_address_name = pci_name(ddev->pdev);
1192 char *pciaddstr, *pciaddstr_tmp, *pciaddname;
1193
1194 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1195 pciaddstr_tmp = pciaddstr;
1196 while ((pciaddname = strsep(&pciaddstr_tmp, ";"))) {
1197 if (!strcmp(pci_address_name, pciaddname)) {
1198 adev->enable_virtual_display = true;
1199 break;
1200 }
1201 }
1202
1203 DRM_INFO("virtual display string:%s, %s:virtual_display:%d\n",
1204 amdgpu_virtual_display, pci_address_name,
1205 adev->enable_virtual_display);
1206
1207 kfree(pciaddstr);
1208 }
1209}
1210
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001211static int amdgpu_early_init(struct amdgpu_device *adev)
1212{
Alex Deucheraaa36a92015-04-20 17:31:14 -04001213 int i, r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001214
Emily Deng9accf2f2016-08-10 16:01:25 +08001215 amdgpu_whether_enable_virtual_display(adev);
Emily Denga6be7572016-08-08 11:37:50 +08001216
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001217 switch (adev->asic_type) {
Alex Deucheraaa36a92015-04-20 17:31:14 -04001218 case CHIP_TOPAZ:
1219 case CHIP_TONGA:
David Zhang48299f92015-07-08 01:05:16 +08001220 case CHIP_FIJI:
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001221 case CHIP_POLARIS11:
1222 case CHIP_POLARIS10:
Alex Deucheraaa36a92015-04-20 17:31:14 -04001223 case CHIP_CARRIZO:
Samuel Li39bb0c92015-10-08 16:31:43 -04001224 case CHIP_STONEY:
1225 if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
Alex Deucheraaa36a92015-04-20 17:31:14 -04001226 adev->family = AMDGPU_FAMILY_CZ;
1227 else
1228 adev->family = AMDGPU_FAMILY_VI;
1229
1230 r = vi_set_ip_blocks(adev);
1231 if (r)
1232 return r;
1233 break;
Alex Deuchera2e73f52015-04-20 17:09:27 -04001234#ifdef CONFIG_DRM_AMDGPU_CIK
1235 case CHIP_BONAIRE:
1236 case CHIP_HAWAII:
1237 case CHIP_KAVERI:
1238 case CHIP_KABINI:
1239 case CHIP_MULLINS:
1240 if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
1241 adev->family = AMDGPU_FAMILY_CI;
1242 else
1243 adev->family = AMDGPU_FAMILY_KV;
1244
1245 r = cik_set_ip_blocks(adev);
1246 if (r)
1247 return r;
1248 break;
1249#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001250 default:
1251 /* FIXME: not supported yet */
1252 return -EINVAL;
1253 }
1254
Alex Deucher8faf0e02015-07-28 11:50:31 -04001255 adev->ip_block_status = kcalloc(adev->num_ip_blocks,
1256 sizeof(struct amdgpu_ip_block_status), GFP_KERNEL);
1257 if (adev->ip_block_status == NULL)
Alex Deucherd8d090b2015-06-26 13:02:57 -04001258 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001259
1260 if (adev->ip_blocks == NULL) {
1261 DRM_ERROR("No IP blocks found!\n");
1262 return r;
1263 }
1264
1265 for (i = 0; i < adev->num_ip_blocks; i++) {
1266 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
1267 DRM_ERROR("disabled ip block: %d\n", i);
Alex Deucher8faf0e02015-07-28 11:50:31 -04001268 adev->ip_block_status[i].valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001269 } else {
1270 if (adev->ip_blocks[i].funcs->early_init) {
yanyang15fc3aee2015-05-22 14:39:35 -04001271 r = adev->ip_blocks[i].funcs->early_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001272 if (r == -ENOENT) {
Alex Deucher8faf0e02015-07-28 11:50:31 -04001273 adev->ip_block_status[i].valid = false;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001274 } else if (r) {
Tom St Denis88a907d2016-05-04 14:28:35 -04001275 DRM_ERROR("early_init of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001276 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001277 } else {
Alex Deucher8faf0e02015-07-28 11:50:31 -04001278 adev->ip_block_status[i].valid = true;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001279 }
Alex Deucher974e6b62015-07-10 13:59:44 -04001280 } else {
Alex Deucher8faf0e02015-07-28 11:50:31 -04001281 adev->ip_block_status[i].valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001282 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001283 }
1284 }
1285
Nicolai Hähnle395d1fb2016-06-02 12:32:07 +02001286 adev->cg_flags &= amdgpu_cg_mask;
1287 adev->pg_flags &= amdgpu_pg_mask;
1288
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001289 return 0;
1290}
1291
1292static int amdgpu_init(struct amdgpu_device *adev)
1293{
1294 int i, r;
1295
1296 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deucher8faf0e02015-07-28 11:50:31 -04001297 if (!adev->ip_block_status[i].valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001298 continue;
yanyang15fc3aee2015-05-22 14:39:35 -04001299 r = adev->ip_blocks[i].funcs->sw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001300 if (r) {
Tom St Denis822b2ce2016-05-05 10:23:40 -04001301 DRM_ERROR("sw_init of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001302 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001303 }
Alex Deucher8faf0e02015-07-28 11:50:31 -04001304 adev->ip_block_status[i].sw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001305 /* need to do gmc hw init early so we can allocate gpu mem */
yanyang15fc3aee2015-05-22 14:39:35 -04001306 if (adev->ip_blocks[i].type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001307 r = amdgpu_vram_scratch_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001308 if (r) {
1309 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001310 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001311 }
yanyang15fc3aee2015-05-22 14:39:35 -04001312 r = adev->ip_blocks[i].funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001313 if (r) {
1314 DRM_ERROR("hw_init %d failed %d\n", i, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001315 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001316 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001317 r = amdgpu_wb_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001318 if (r) {
1319 DRM_ERROR("amdgpu_wb_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001320 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001321 }
Alex Deucher8faf0e02015-07-28 11:50:31 -04001322 adev->ip_block_status[i].hw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001323 }
1324 }
1325
1326 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deucher8faf0e02015-07-28 11:50:31 -04001327 if (!adev->ip_block_status[i].sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001328 continue;
1329 /* gmc hw init is done early */
yanyang15fc3aee2015-05-22 14:39:35 -04001330 if (adev->ip_blocks[i].type == AMD_IP_BLOCK_TYPE_GMC)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001331 continue;
yanyang15fc3aee2015-05-22 14:39:35 -04001332 r = adev->ip_blocks[i].funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001333 if (r) {
Tom St Denis822b2ce2016-05-05 10:23:40 -04001334 DRM_ERROR("hw_init of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001335 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001336 }
Alex Deucher8faf0e02015-07-28 11:50:31 -04001337 adev->ip_block_status[i].hw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001338 }
1339
1340 return 0;
1341}
1342
1343static int amdgpu_late_init(struct amdgpu_device *adev)
1344{
1345 int i = 0, r;
1346
1347 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deucher8faf0e02015-07-28 11:50:31 -04001348 if (!adev->ip_block_status[i].valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001349 continue;
1350 /* enable clockgating to save power */
yanyang15fc3aee2015-05-22 14:39:35 -04001351 r = adev->ip_blocks[i].funcs->set_clockgating_state((void *)adev,
1352 AMD_CG_STATE_GATE);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001353 if (r) {
Tom St Denis822b2ce2016-05-05 10:23:40 -04001354 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001355 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001356 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001357 if (adev->ip_blocks[i].funcs->late_init) {
yanyang15fc3aee2015-05-22 14:39:35 -04001358 r = adev->ip_blocks[i].funcs->late_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001359 if (r) {
Tom St Denis822b2ce2016-05-05 10:23:40 -04001360 DRM_ERROR("late_init of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001361 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001362 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001363 }
1364 }
1365
1366 return 0;
1367}
1368
1369static int amdgpu_fini(struct amdgpu_device *adev)
1370{
1371 int i, r;
1372
1373 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deucher8faf0e02015-07-28 11:50:31 -04001374 if (!adev->ip_block_status[i].hw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001375 continue;
yanyang15fc3aee2015-05-22 14:39:35 -04001376 if (adev->ip_blocks[i].type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001377 amdgpu_wb_fini(adev);
1378 amdgpu_vram_scratch_fini(adev);
1379 }
1380 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
yanyang15fc3aee2015-05-22 14:39:35 -04001381 r = adev->ip_blocks[i].funcs->set_clockgating_state((void *)adev,
1382 AMD_CG_STATE_UNGATE);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001383 if (r) {
Tom St Denis822b2ce2016-05-05 10:23:40 -04001384 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001385 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001386 }
yanyang15fc3aee2015-05-22 14:39:35 -04001387 r = adev->ip_blocks[i].funcs->hw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001388 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001389 if (r) {
Tom St Denis822b2ce2016-05-05 10:23:40 -04001390 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001391 }
Alex Deucher8faf0e02015-07-28 11:50:31 -04001392 adev->ip_block_status[i].hw = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001393 }
1394
1395 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deucher8faf0e02015-07-28 11:50:31 -04001396 if (!adev->ip_block_status[i].sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001397 continue;
yanyang15fc3aee2015-05-22 14:39:35 -04001398 r = adev->ip_blocks[i].funcs->sw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001399 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001400 if (r) {
Tom St Denis822b2ce2016-05-05 10:23:40 -04001401 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001402 }
Alex Deucher8faf0e02015-07-28 11:50:31 -04001403 adev->ip_block_status[i].sw = false;
1404 adev->ip_block_status[i].valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001405 }
1406
Monk Liua6dcfd92016-05-19 14:36:34 +08001407 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
1408 if (adev->ip_blocks[i].funcs->late_fini)
1409 adev->ip_blocks[i].funcs->late_fini((void *)adev);
1410 }
1411
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001412 return 0;
1413}
1414
1415static int amdgpu_suspend(struct amdgpu_device *adev)
1416{
1417 int i, r;
1418
Flora Cuic5a93a22016-02-26 10:45:25 +08001419 /* ungate SMC block first */
1420 r = amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC,
1421 AMD_CG_STATE_UNGATE);
1422 if (r) {
1423 DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n",r);
1424 }
1425
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001426 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deucher8faf0e02015-07-28 11:50:31 -04001427 if (!adev->ip_block_status[i].valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001428 continue;
1429 /* ungate blocks so that suspend can properly shut them down */
Flora Cuic5a93a22016-02-26 10:45:25 +08001430 if (i != AMD_IP_BLOCK_TYPE_SMC) {
1431 r = adev->ip_blocks[i].funcs->set_clockgating_state((void *)adev,
1432 AMD_CG_STATE_UNGATE);
1433 if (r) {
Tom St Denis822b2ce2016-05-05 10:23:40 -04001434 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r);
Flora Cuic5a93a22016-02-26 10:45:25 +08001435 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001436 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001437 /* XXX handle errors */
1438 r = adev->ip_blocks[i].funcs->suspend(adev);
1439 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001440 if (r) {
Tom St Denis822b2ce2016-05-05 10:23:40 -04001441 DRM_ERROR("suspend of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001442 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001443 }
1444
1445 return 0;
1446}
1447
1448static int amdgpu_resume(struct amdgpu_device *adev)
1449{
1450 int i, r;
1451
1452 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deucher8faf0e02015-07-28 11:50:31 -04001453 if (!adev->ip_block_status[i].valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001454 continue;
1455 r = adev->ip_blocks[i].funcs->resume(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001456 if (r) {
Tom St Denis822b2ce2016-05-05 10:23:40 -04001457 DRM_ERROR("resume of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001458 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001459 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001460 }
1461
1462 return 0;
1463}
1464
Andres Rodriguez048765a2016-06-11 02:51:32 -04001465static bool amdgpu_device_is_virtual(void)
1466{
1467#ifdef CONFIG_X86
1468 return boot_cpu_has(X86_FEATURE_HYPERVISOR);
1469#else
1470 return false;
1471#endif
1472}
1473
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001474/**
1475 * amdgpu_device_init - initialize the driver
1476 *
1477 * @adev: amdgpu_device pointer
1478 * @pdev: drm dev pointer
1479 * @pdev: pci dev pointer
1480 * @flags: driver flags
1481 *
1482 * Initializes the driver info and hw (all asics).
1483 * Returns 0 for success or an error on failure.
1484 * Called at driver startup.
1485 */
1486int amdgpu_device_init(struct amdgpu_device *adev,
1487 struct drm_device *ddev,
1488 struct pci_dev *pdev,
1489 uint32_t flags)
1490{
1491 int r, i;
1492 bool runtime = false;
Marek Olšák95844d22016-08-17 23:49:27 +02001493 u32 max_MBps;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001494
1495 adev->shutdown = false;
1496 adev->dev = &pdev->dev;
1497 adev->ddev = ddev;
1498 adev->pdev = pdev;
1499 adev->flags = flags;
Jammy Zhou2f7d10b2015-07-22 11:29:01 +08001500 adev->asic_type = flags & AMD_ASIC_MASK;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001501 adev->is_atom_bios = false;
1502 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
1503 adev->mc.gtt_size = 512 * 1024 * 1024;
1504 adev->accel_working = false;
1505 adev->num_rings = 0;
1506 adev->mman.buffer_funcs = NULL;
1507 adev->mman.buffer_funcs_ring = NULL;
1508 adev->vm_manager.vm_pte_funcs = NULL;
Christian König2d55e452016-02-08 17:37:38 +01001509 adev->vm_manager.vm_pte_num_rings = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001510 adev->gart.gart_funcs = NULL;
1511 adev->fence_context = fence_context_alloc(AMDGPU_MAX_RINGS);
1512
1513 adev->smc_rreg = &amdgpu_invalid_rreg;
1514 adev->smc_wreg = &amdgpu_invalid_wreg;
1515 adev->pcie_rreg = &amdgpu_invalid_rreg;
1516 adev->pcie_wreg = &amdgpu_invalid_wreg;
Huang Rui36b9a952016-08-31 13:23:25 +08001517 adev->pciep_rreg = &amdgpu_invalid_rreg;
1518 adev->pciep_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001519 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
1520 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
1521 adev->didt_rreg = &amdgpu_invalid_rreg;
1522 adev->didt_wreg = &amdgpu_invalid_wreg;
Rex Zhuccdbb202016-06-08 12:47:41 +08001523 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
1524 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001525 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
1526 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
1527
Rex Zhuccdbb202016-06-08 12:47:41 +08001528
Alex Deucher3e39ab92015-06-05 15:04:33 -04001529 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
1530 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
1531 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001532
1533 /* mutex initialization are all done here so we
1534 * can recall function without having locking issues */
Christian König8d0a7ce2015-11-03 20:58:50 +01001535 mutex_init(&adev->vm_manager.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001536 atomic_set(&adev->irq.ih.lock, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001537 mutex_init(&adev->pm.mutex);
1538 mutex_init(&adev->gfx.gpu_clock_mutex);
1539 mutex_init(&adev->srbm_mutex);
1540 mutex_init(&adev->grbm_idx_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001541 mutex_init(&adev->mn_lock);
1542 hash_init(adev->mn_hash);
1543
1544 amdgpu_check_arguments(adev);
1545
1546 /* Registers mapping */
1547 /* TODO: block userspace mapping of io register */
1548 spin_lock_init(&adev->mmio_idx_lock);
1549 spin_lock_init(&adev->smc_idx_lock);
1550 spin_lock_init(&adev->pcie_idx_lock);
1551 spin_lock_init(&adev->uvd_ctx_idx_lock);
1552 spin_lock_init(&adev->didt_idx_lock);
Rex Zhuccdbb202016-06-08 12:47:41 +08001553 spin_lock_init(&adev->gc_cac_idx_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001554 spin_lock_init(&adev->audio_endpt_idx_lock);
Marek Olšák95844d22016-08-17 23:49:27 +02001555 spin_lock_init(&adev->mm_stats.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001556
Chunming Zhou0c4e7fa2016-08-17 11:41:30 +08001557 INIT_LIST_HEAD(&adev->shadow_list);
1558 mutex_init(&adev->shadow_list_lock);
1559
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001560 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
1561 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
1562 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
1563 if (adev->rmmio == NULL) {
1564 return -ENOMEM;
1565 }
1566 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
1567 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
1568
1569 /* doorbell bar mapping */
1570 amdgpu_doorbell_init(adev);
1571
1572 /* io port mapping */
1573 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
1574 if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
1575 adev->rio_mem_size = pci_resource_len(adev->pdev, i);
1576 adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
1577 break;
1578 }
1579 }
1580 if (adev->rio_mem == NULL)
1581 DRM_ERROR("Unable to find PCI I/O BAR\n");
1582
1583 /* early init functions */
1584 r = amdgpu_early_init(adev);
1585 if (r)
1586 return r;
1587
1588 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
1589 /* this will fail for cards that aren't VGA class devices, just
1590 * ignore it */
1591 vga_client_register(adev->pdev, adev, NULL, amdgpu_vga_set_decode);
1592
1593 if (amdgpu_runtime_pm == 1)
1594 runtime = true;
Alex Deuchere9bef452016-04-25 13:12:18 -04001595 if (amdgpu_device_is_px(ddev))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001596 runtime = true;
1597 vga_switcheroo_register_client(adev->pdev, &amdgpu_switcheroo_ops, runtime);
1598 if (runtime)
1599 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
1600
1601 /* Read BIOS */
Alex Deucher83ba1262016-06-03 18:21:41 -04001602 if (!amdgpu_get_bios(adev)) {
1603 r = -EINVAL;
1604 goto failed;
1605 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001606 /* Must be an ATOMBIOS */
1607 if (!adev->is_atom_bios) {
1608 dev_err(adev->dev, "Expecting atombios for GPU\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001609 r = -EINVAL;
1610 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001611 }
1612 r = amdgpu_atombios_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001613 if (r) {
1614 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001615 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001616 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001617
Alex Deucher7e471e62016-02-01 11:13:04 -05001618 /* See if the asic supports SR-IOV */
1619 adev->virtualization.supports_sr_iov =
1620 amdgpu_atombios_has_gpu_virtualization_table(adev);
1621
Andres Rodriguez048765a2016-06-11 02:51:32 -04001622 /* Check if we are executing in a virtualized environment */
1623 adev->virtualization.is_virtual = amdgpu_device_is_virtual();
1624 adev->virtualization.caps = amdgpu_asic_get_virtual_caps(adev);
1625
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001626 /* Post card if necessary */
Andres Rodriguez048765a2016-06-11 02:51:32 -04001627 if (!amdgpu_card_posted(adev) ||
1628 (adev->virtualization.is_virtual &&
Dan Carpenter48a70e12016-06-18 11:38:44 +03001629 !(adev->virtualization.caps & AMDGPU_VIRT_CAPS_SRIOV_EN))) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001630 if (!adev->bios) {
1631 dev_err(adev->dev, "Card not posted and no BIOS - ignoring\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001632 r = -EINVAL;
1633 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001634 }
1635 DRM_INFO("GPU not posted. posting now...\n");
1636 amdgpu_atom_asic_init(adev->mode_info.atom_context);
1637 }
1638
1639 /* Initialize clocks */
1640 r = amdgpu_atombios_get_clock_info(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001641 if (r) {
1642 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001643 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001644 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001645 /* init i2c buses */
1646 amdgpu_atombios_i2c_init(adev);
1647
1648 /* Fence driver */
1649 r = amdgpu_fence_driver_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001650 if (r) {
1651 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001652 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001653 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001654
1655 /* init the mode config */
1656 drm_mode_config_init(adev->ddev);
1657
1658 r = amdgpu_init(adev);
1659 if (r) {
Alex Deucher2c1a2782015-12-07 17:02:53 -05001660 dev_err(adev->dev, "amdgpu_init failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001661 amdgpu_fini(adev);
Alex Deucher83ba1262016-06-03 18:21:41 -04001662 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001663 }
1664
1665 adev->accel_working = true;
1666
Marek Olšák95844d22016-08-17 23:49:27 +02001667 /* Initialize the buffer migration limit. */
1668 if (amdgpu_moverate >= 0)
1669 max_MBps = amdgpu_moverate;
1670 else
1671 max_MBps = 8; /* Allow 8 MB/s. */
1672 /* Get a log2 for easy divisions. */
1673 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
1674
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001675 amdgpu_fbdev_init(adev);
1676
1677 r = amdgpu_ib_pool_init(adev);
1678 if (r) {
1679 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
Alex Deucher83ba1262016-06-03 18:21:41 -04001680 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001681 }
1682
1683 r = amdgpu_ib_ring_tests(adev);
1684 if (r)
1685 DRM_ERROR("ib ring test failed (%d).\n", r);
1686
1687 r = amdgpu_gem_debugfs_init(adev);
1688 if (r) {
1689 DRM_ERROR("registering gem debugfs failed (%d).\n", r);
1690 }
1691
1692 r = amdgpu_debugfs_regs_init(adev);
1693 if (r) {
1694 DRM_ERROR("registering register debugfs failed (%d).\n", r);
1695 }
1696
Huang Rui50ab2532016-06-12 15:51:09 +08001697 r = amdgpu_debugfs_firmware_init(adev);
1698 if (r) {
1699 DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
1700 return r;
1701 }
1702
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001703 if ((amdgpu_testing & 1)) {
1704 if (adev->accel_working)
1705 amdgpu_test_moves(adev);
1706 else
1707 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
1708 }
1709 if ((amdgpu_testing & 2)) {
1710 if (adev->accel_working)
1711 amdgpu_test_syncing(adev);
1712 else
1713 DRM_INFO("amdgpu: acceleration disabled, skipping sync tests\n");
1714 }
1715 if (amdgpu_benchmarking) {
1716 if (adev->accel_working)
1717 amdgpu_benchmark(adev, amdgpu_benchmarking);
1718 else
1719 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
1720 }
1721
1722 /* enable clockgating, etc. after ib tests, etc. since some blocks require
1723 * explicit gating rather than handling it automatically.
1724 */
1725 r = amdgpu_late_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001726 if (r) {
1727 dev_err(adev->dev, "amdgpu_late_init failed\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001728 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001729 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001730
1731 return 0;
Alex Deucher83ba1262016-06-03 18:21:41 -04001732
1733failed:
1734 if (runtime)
1735 vga_switcheroo_fini_domain_pm_ops(adev->dev);
1736 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001737}
1738
1739static void amdgpu_debugfs_remove_files(struct amdgpu_device *adev);
1740
1741/**
1742 * amdgpu_device_fini - tear down the driver
1743 *
1744 * @adev: amdgpu_device pointer
1745 *
1746 * Tear down the driver info (all asics).
1747 * Called at driver shutdown.
1748 */
1749void amdgpu_device_fini(struct amdgpu_device *adev)
1750{
1751 int r;
1752
1753 DRM_INFO("amdgpu: finishing device.\n");
1754 adev->shutdown = true;
1755 /* evict vram memory */
1756 amdgpu_bo_evict_vram(adev);
1757 amdgpu_ib_pool_fini(adev);
1758 amdgpu_fence_driver_fini(adev);
Lukas Wunner84b89bd2016-06-08 18:47:27 +02001759 drm_crtc_force_disable_all(adev->ddev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001760 amdgpu_fbdev_fini(adev);
1761 r = amdgpu_fini(adev);
Alex Deucher8faf0e02015-07-28 11:50:31 -04001762 kfree(adev->ip_block_status);
1763 adev->ip_block_status = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001764 adev->accel_working = false;
1765 /* free i2c buses */
1766 amdgpu_i2c_fini(adev);
1767 amdgpu_atombios_fini(adev);
1768 kfree(adev->bios);
1769 adev->bios = NULL;
1770 vga_switcheroo_unregister_client(adev->pdev);
Alex Deucher83ba1262016-06-03 18:21:41 -04001771 if (adev->flags & AMD_IS_PX)
1772 vga_switcheroo_fini_domain_pm_ops(adev->dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001773 vga_client_register(adev->pdev, NULL, NULL, NULL);
1774 if (adev->rio_mem)
1775 pci_iounmap(adev->pdev, adev->rio_mem);
1776 adev->rio_mem = NULL;
1777 iounmap(adev->rmmio);
1778 adev->rmmio = NULL;
1779 amdgpu_doorbell_fini(adev);
1780 amdgpu_debugfs_regs_cleanup(adev);
1781 amdgpu_debugfs_remove_files(adev);
1782}
1783
1784
1785/*
1786 * Suspend & resume.
1787 */
1788/**
Alex Deucher810ddc32016-08-23 13:25:49 -04001789 * amdgpu_device_suspend - initiate device suspend
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001790 *
1791 * @pdev: drm dev pointer
1792 * @state: suspend state
1793 *
1794 * Puts the hw in the suspend state (all asics).
1795 * Returns 0 for success or an error on failure.
1796 * Called at driver suspend.
1797 */
Alex Deucher810ddc32016-08-23 13:25:49 -04001798int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001799{
1800 struct amdgpu_device *adev;
1801 struct drm_crtc *crtc;
1802 struct drm_connector *connector;
Alex Deucher5ceb54c2015-08-05 12:41:48 -04001803 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001804
1805 if (dev == NULL || dev->dev_private == NULL) {
1806 return -ENODEV;
1807 }
1808
1809 adev = dev->dev_private;
1810
1811 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
1812 return 0;
1813
1814 drm_kms_helper_poll_disable(dev);
1815
1816 /* turn off display hw */
Alex Deucher4c7fbc32015-09-23 14:32:06 -04001817 drm_modeset_lock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001818 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1819 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
1820 }
Alex Deucher4c7fbc32015-09-23 14:32:06 -04001821 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001822
Alex Deucher756e6882015-10-08 00:03:36 -04001823 /* unpin the front buffers and cursors */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001824 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Alex Deucher756e6882015-10-08 00:03:36 -04001825 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001826 struct amdgpu_framebuffer *rfb = to_amdgpu_framebuffer(crtc->primary->fb);
1827 struct amdgpu_bo *robj;
1828
Alex Deucher756e6882015-10-08 00:03:36 -04001829 if (amdgpu_crtc->cursor_bo) {
1830 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
1831 r = amdgpu_bo_reserve(aobj, false);
1832 if (r == 0) {
1833 amdgpu_bo_unpin(aobj);
1834 amdgpu_bo_unreserve(aobj);
1835 }
1836 }
1837
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001838 if (rfb == NULL || rfb->obj == NULL) {
1839 continue;
1840 }
1841 robj = gem_to_amdgpu_bo(rfb->obj);
1842 /* don't unpin kernel fb objects */
1843 if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
1844 r = amdgpu_bo_reserve(robj, false);
1845 if (r == 0) {
1846 amdgpu_bo_unpin(robj);
1847 amdgpu_bo_unreserve(robj);
1848 }
1849 }
1850 }
1851 /* evict vram memory */
1852 amdgpu_bo_evict_vram(adev);
1853
Alex Deucher5ceb54c2015-08-05 12:41:48 -04001854 amdgpu_fence_driver_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001855
1856 r = amdgpu_suspend(adev);
1857
1858 /* evict remaining vram memory */
1859 amdgpu_bo_evict_vram(adev);
1860
1861 pci_save_state(dev->pdev);
1862 if (suspend) {
1863 /* Shut down the device */
1864 pci_disable_device(dev->pdev);
1865 pci_set_power_state(dev->pdev, PCI_D3hot);
1866 }
1867
1868 if (fbcon) {
1869 console_lock();
1870 amdgpu_fbdev_set_suspend(adev, 1);
1871 console_unlock();
1872 }
1873 return 0;
1874}
1875
1876/**
Alex Deucher810ddc32016-08-23 13:25:49 -04001877 * amdgpu_device_resume - initiate device resume
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001878 *
1879 * @pdev: drm dev pointer
1880 *
1881 * Bring the hw back to operating state (all asics).
1882 * Returns 0 for success or an error on failure.
1883 * Called at driver resume.
1884 */
Alex Deucher810ddc32016-08-23 13:25:49 -04001885int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001886{
1887 struct drm_connector *connector;
1888 struct amdgpu_device *adev = dev->dev_private;
Alex Deucher756e6882015-10-08 00:03:36 -04001889 struct drm_crtc *crtc;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001890 int r;
1891
1892 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
1893 return 0;
1894
1895 if (fbcon) {
1896 console_lock();
1897 }
1898 if (resume) {
1899 pci_set_power_state(dev->pdev, PCI_D0);
1900 pci_restore_state(dev->pdev);
1901 if (pci_enable_device(dev->pdev)) {
1902 if (fbcon)
1903 console_unlock();
1904 return -1;
1905 }
1906 }
1907
1908 /* post card */
Flora Cuica198522016-02-04 15:10:08 +08001909 if (!amdgpu_card_posted(adev))
1910 amdgpu_atom_asic_init(adev->mode_info.atom_context);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001911
1912 r = amdgpu_resume(adev);
Flora Cuica198522016-02-04 15:10:08 +08001913 if (r)
1914 DRM_ERROR("amdgpu_resume failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001915
Alex Deucher5ceb54c2015-08-05 12:41:48 -04001916 amdgpu_fence_driver_resume(adev);
1917
Flora Cuica198522016-02-04 15:10:08 +08001918 if (resume) {
1919 r = amdgpu_ib_ring_tests(adev);
1920 if (r)
1921 DRM_ERROR("ib ring test failed (%d).\n", r);
1922 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001923
1924 r = amdgpu_late_init(adev);
1925 if (r)
1926 return r;
1927
Alex Deucher756e6882015-10-08 00:03:36 -04001928 /* pin cursors */
1929 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1930 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
1931
1932 if (amdgpu_crtc->cursor_bo) {
1933 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
1934 r = amdgpu_bo_reserve(aobj, false);
1935 if (r == 0) {
1936 r = amdgpu_bo_pin(aobj,
1937 AMDGPU_GEM_DOMAIN_VRAM,
1938 &amdgpu_crtc->cursor_addr);
1939 if (r != 0)
1940 DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
1941 amdgpu_bo_unreserve(aobj);
1942 }
1943 }
1944 }
1945
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001946 /* blat the mode back in */
1947 if (fbcon) {
1948 drm_helper_resume_force_mode(dev);
1949 /* turn on display hw */
Alex Deucher4c7fbc32015-09-23 14:32:06 -04001950 drm_modeset_lock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001951 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1952 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
1953 }
Alex Deucher4c7fbc32015-09-23 14:32:06 -04001954 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001955 }
1956
1957 drm_kms_helper_poll_enable(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04001958
1959 /*
1960 * Most of the connector probing functions try to acquire runtime pm
1961 * refs to ensure that the GPU is powered on when connector polling is
1962 * performed. Since we're calling this from a runtime PM callback,
1963 * trying to acquire rpm refs will cause us to deadlock.
1964 *
1965 * Since we're guaranteed to be holding the rpm lock, it's safe to
1966 * temporarily disable the rpm helpers so this doesn't deadlock us.
1967 */
1968#ifdef CONFIG_PM
1969 dev->dev->power.disable_depth++;
1970#endif
Alex Deucher54fb2a52015-11-24 14:30:56 -05001971 drm_helper_hpd_irq_event(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04001972#ifdef CONFIG_PM
1973 dev->dev->power.disable_depth--;
1974#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001975
1976 if (fbcon) {
1977 amdgpu_fbdev_set_suspend(adev, 0);
1978 console_unlock();
1979 }
1980
1981 return 0;
1982}
1983
Chunming Zhou63fbf422016-07-15 11:19:20 +08001984static bool amdgpu_check_soft_reset(struct amdgpu_device *adev)
1985{
1986 int i;
1987 bool asic_hang = false;
1988
1989 for (i = 0; i < adev->num_ip_blocks; i++) {
1990 if (!adev->ip_block_status[i].valid)
1991 continue;
1992 if (adev->ip_blocks[i].funcs->check_soft_reset)
1993 adev->ip_blocks[i].funcs->check_soft_reset(adev);
1994 if (adev->ip_block_status[i].hang) {
1995 DRM_INFO("IP block:%d is hang!\n", i);
1996 asic_hang = true;
1997 }
1998 }
1999 return asic_hang;
2000}
2001
Chunming Zhoud31a5012016-07-18 10:04:34 +08002002int amdgpu_pre_soft_reset(struct amdgpu_device *adev)
2003{
2004 int i, r = 0;
2005
2006 for (i = 0; i < adev->num_ip_blocks; i++) {
2007 if (!adev->ip_block_status[i].valid)
2008 continue;
Chunming Zhou35d782f2016-07-15 15:57:13 +08002009 if (adev->ip_block_status[i].hang &&
2010 adev->ip_blocks[i].funcs->pre_soft_reset) {
Chunming Zhoud31a5012016-07-18 10:04:34 +08002011 r = adev->ip_blocks[i].funcs->pre_soft_reset(adev);
2012 if (r)
2013 return r;
2014 }
2015 }
2016
2017 return 0;
2018}
2019
Chunming Zhou35d782f2016-07-15 15:57:13 +08002020static bool amdgpu_need_full_reset(struct amdgpu_device *adev)
2021{
2022 if (adev->ip_block_status[AMD_IP_BLOCK_TYPE_GMC].hang ||
Chunming Zhou35d782f2016-07-15 15:57:13 +08002023 adev->ip_block_status[AMD_IP_BLOCK_TYPE_SMC].hang ||
Chunming Zhou35d782f2016-07-15 15:57:13 +08002024 adev->ip_block_status[AMD_IP_BLOCK_TYPE_ACP].hang ||
2025 adev->ip_block_status[AMD_IP_BLOCK_TYPE_DCE].hang) {
2026 DRM_INFO("Some block need full reset!\n");
2027 return true;
2028 }
2029 return false;
2030}
2031
2032static int amdgpu_soft_reset(struct amdgpu_device *adev)
2033{
2034 int i, r = 0;
2035
2036 for (i = 0; i < adev->num_ip_blocks; i++) {
2037 if (!adev->ip_block_status[i].valid)
2038 continue;
2039 if (adev->ip_block_status[i].hang &&
2040 adev->ip_blocks[i].funcs->soft_reset) {
2041 r = adev->ip_blocks[i].funcs->soft_reset(adev);
2042 if (r)
2043 return r;
2044 }
2045 }
2046
2047 return 0;
2048}
2049
2050static int amdgpu_post_soft_reset(struct amdgpu_device *adev)
2051{
2052 int i, r = 0;
2053
2054 for (i = 0; i < adev->num_ip_blocks; i++) {
2055 if (!adev->ip_block_status[i].valid)
2056 continue;
2057 if (adev->ip_block_status[i].hang &&
2058 adev->ip_blocks[i].funcs->post_soft_reset)
2059 r = adev->ip_blocks[i].funcs->post_soft_reset(adev);
2060 if (r)
2061 return r;
2062 }
2063
2064 return 0;
2065}
2066
Chunming Zhou3ad81f12016-08-05 17:30:17 +08002067bool amdgpu_need_backup(struct amdgpu_device *adev)
2068{
2069 if (adev->flags & AMD_IS_APU)
2070 return false;
2071
2072 return amdgpu_lockup_timeout > 0 ? true : false;
2073}
2074
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002075static int amdgpu_recover_vram_from_shadow(struct amdgpu_device *adev,
2076 struct amdgpu_ring *ring,
2077 struct amdgpu_bo *bo,
2078 struct fence **fence)
2079{
2080 uint32_t domain;
2081 int r;
2082
2083 if (!bo->shadow)
2084 return 0;
2085
2086 r = amdgpu_bo_reserve(bo, false);
2087 if (r)
2088 return r;
2089 domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
2090 /* if bo has been evicted, then no need to recover */
2091 if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
2092 r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
2093 NULL, fence, true);
2094 if (r) {
2095 DRM_ERROR("recover page table failed!\n");
2096 goto err;
2097 }
2098 }
2099err:
2100 amdgpu_bo_unreserve(bo);
2101 return r;
2102}
2103
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002104/**
2105 * amdgpu_gpu_reset - reset the asic
2106 *
2107 * @adev: amdgpu device pointer
2108 *
2109 * Attempt the reset the GPU if it has hung (all asics).
2110 * Returns 0 for success or an error on failure.
2111 */
2112int amdgpu_gpu_reset(struct amdgpu_device *adev)
2113{
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002114 int i, r;
2115 int resched;
Chunming Zhou35d782f2016-07-15 15:57:13 +08002116 bool need_full_reset;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002117
Chunming Zhou63fbf422016-07-15 11:19:20 +08002118 if (!amdgpu_check_soft_reset(adev)) {
2119 DRM_INFO("No hardware hang detected. Did some blocks stall?\n");
2120 return 0;
2121 }
2122
Marek Olšákd94aed52015-05-05 21:13:49 +02002123 atomic_inc(&adev->gpu_reset_counter);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002124
Chunming Zhoua3c47d62016-06-30 16:44:41 +08002125 /* block TTM */
2126 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
2127
Chunming Zhou0875dc92016-06-12 15:41:58 +08002128 /* block scheduler */
2129 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2130 struct amdgpu_ring *ring = adev->rings[i];
2131
2132 if (!ring)
2133 continue;
2134 kthread_park(ring->sched.thread);
Chunming Zhouaa1c8902016-06-30 13:56:02 +08002135 amd_sched_hw_job_reset(&ring->sched);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002136 }
Chunming Zhou2200eda2016-06-30 16:53:02 +08002137 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
2138 amdgpu_fence_driver_force_completion(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002139
Chunming Zhou35d782f2016-07-15 15:57:13 +08002140 need_full_reset = amdgpu_need_full_reset(adev);
2141
2142 if (!need_full_reset) {
2143 amdgpu_pre_soft_reset(adev);
2144 r = amdgpu_soft_reset(adev);
2145 amdgpu_post_soft_reset(adev);
2146 if (r || amdgpu_check_soft_reset(adev)) {
2147 DRM_INFO("soft reset failed, will fallback to full reset!\n");
2148 need_full_reset = true;
2149 }
2150 }
2151
2152 if (need_full_reset) {
2153 /* save scratch */
2154 amdgpu_atombios_scratch_regs_save(adev);
2155 r = amdgpu_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002156
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002157retry:
Chunming Zhou35d782f2016-07-15 15:57:13 +08002158 /* Disable fb access */
2159 if (adev->mode_info.num_crtc) {
2160 struct amdgpu_mode_mc_save save;
2161 amdgpu_display_stop_mc_access(adev, &save);
2162 amdgpu_wait_for_idle(adev, AMD_IP_BLOCK_TYPE_GMC);
2163 }
Chunming Zhouf1aa7e02016-06-28 10:38:50 +08002164
Chunming Zhou35d782f2016-07-15 15:57:13 +08002165 r = amdgpu_asic_reset(adev);
2166 /* post card */
2167 amdgpu_atom_asic_init(adev->mode_info.atom_context);
Alex Deucherbfa99262016-01-15 11:59:48 -05002168
Chunming Zhou35d782f2016-07-15 15:57:13 +08002169 if (!r) {
2170 dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
2171 r = amdgpu_resume(adev);
2172 }
2173 /* restore scratch */
2174 amdgpu_atombios_scratch_regs_restore(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002175 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002176 if (!r) {
Chunming Zhoue72cfd52016-07-27 13:15:20 +08002177 amdgpu_irq_gpu_reset_resume_helper(adev);
Chunming Zhou1f465082016-06-30 15:02:26 +08002178 r = amdgpu_ib_ring_tests(adev);
2179 if (r) {
2180 dev_err(adev->dev, "ib ring test failed (%d).\n", r);
Chunming Zhou40019dc2016-06-29 16:01:49 +08002181 r = amdgpu_suspend(adev);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002182 need_full_reset = true;
Chunming Zhou40019dc2016-06-29 16:01:49 +08002183 goto retry;
Chunming Zhou1f465082016-06-30 15:02:26 +08002184 }
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002185 /**
2186 * recovery vm page tables, since we cannot depend on VRAM is
2187 * consistent after gpu full reset.
2188 */
2189 if (need_full_reset && amdgpu_need_backup(adev)) {
2190 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
2191 struct amdgpu_bo *bo, *tmp;
2192 struct fence *fence = NULL, *next = NULL;
Chunming Zhou1f465082016-06-30 15:02:26 +08002193
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002194 DRM_INFO("recover vram bo from shadow\n");
2195 mutex_lock(&adev->shadow_list_lock);
2196 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
2197 amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
2198 if (fence) {
2199 r = fence_wait(fence, false);
2200 if (r) {
2201 WARN(r, "recovery from shadow isn't comleted\n");
2202 break;
2203 }
2204 }
2205
2206 fence_put(fence);
2207 fence = next;
2208 }
2209 mutex_unlock(&adev->shadow_list_lock);
2210 if (fence) {
2211 r = fence_wait(fence, false);
2212 if (r)
2213 WARN(r, "recovery from shadow isn't comleted\n");
2214 }
2215 fence_put(fence);
2216 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002217 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2218 struct amdgpu_ring *ring = adev->rings[i];
2219 if (!ring)
2220 continue;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002221
Chunming Zhouaa1c8902016-06-30 13:56:02 +08002222 amd_sched_job_recovery(&ring->sched);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002223 kthread_unpark(ring->sched.thread);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002224 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002225 } else {
Chunming Zhou2200eda2016-06-30 16:53:02 +08002226 dev_err(adev->dev, "asic resume failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002227 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
Chunming Zhou0875dc92016-06-12 15:41:58 +08002228 if (adev->rings[i]) {
2229 kthread_unpark(adev->rings[i]->sched.thread);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002230 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002231 }
2232 }
2233
2234 drm_helper_resume_force_mode(adev->ddev);
2235
2236 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
2237 if (r) {
2238 /* bad news, how to tell it to userspace ? */
2239 dev_info(adev->dev, "GPU reset failed\n");
2240 }
2241
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002242 return r;
2243}
2244
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002245void amdgpu_get_pcie_info(struct amdgpu_device *adev)
2246{
2247 u32 mask;
2248 int ret;
2249
Alex Deuchercd474ba2016-02-04 10:21:23 -05002250 if (amdgpu_pcie_gen_cap)
2251 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
2252
2253 if (amdgpu_pcie_lane_cap)
2254 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
2255
2256 /* covers APUs as well */
2257 if (pci_is_root_bus(adev->pdev->bus)) {
2258 if (adev->pm.pcie_gen_mask == 0)
2259 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
2260 if (adev->pm.pcie_mlw_mask == 0)
2261 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002262 return;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002263 }
Alex Deuchercd474ba2016-02-04 10:21:23 -05002264
2265 if (adev->pm.pcie_gen_mask == 0) {
2266 ret = drm_pcie_get_speed_cap_mask(adev->ddev, &mask);
2267 if (!ret) {
2268 adev->pm.pcie_gen_mask = (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
2269 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
2270 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
2271
2272 if (mask & DRM_PCIE_SPEED_25)
2273 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
2274 if (mask & DRM_PCIE_SPEED_50)
2275 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2;
2276 if (mask & DRM_PCIE_SPEED_80)
2277 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3;
2278 } else {
2279 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
2280 }
2281 }
2282 if (adev->pm.pcie_mlw_mask == 0) {
2283 ret = drm_pcie_get_max_link_width(adev->ddev, &mask);
2284 if (!ret) {
2285 switch (mask) {
2286 case 32:
2287 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
2288 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
2289 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2290 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2291 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2292 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2293 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2294 break;
2295 case 16:
2296 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
2297 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2298 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2299 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2300 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2301 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2302 break;
2303 case 12:
2304 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2305 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2306 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2307 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2308 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2309 break;
2310 case 8:
2311 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2312 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2313 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2314 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2315 break;
2316 case 4:
2317 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2318 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2319 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2320 break;
2321 case 2:
2322 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2323 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2324 break;
2325 case 1:
2326 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
2327 break;
2328 default:
2329 break;
2330 }
2331 } else {
2332 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002333 }
2334 }
2335}
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002336
2337/*
2338 * Debugfs
2339 */
2340int amdgpu_debugfs_add_files(struct amdgpu_device *adev,
Nils Wallménius06ab6832016-05-02 12:46:15 -04002341 const struct drm_info_list *files,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002342 unsigned nfiles)
2343{
2344 unsigned i;
2345
2346 for (i = 0; i < adev->debugfs_count; i++) {
2347 if (adev->debugfs[i].files == files) {
2348 /* Already registered */
2349 return 0;
2350 }
2351 }
2352
2353 i = adev->debugfs_count + 1;
2354 if (i > AMDGPU_DEBUGFS_MAX_COMPONENTS) {
2355 DRM_ERROR("Reached maximum number of debugfs components.\n");
2356 DRM_ERROR("Report so we increase "
2357 "AMDGPU_DEBUGFS_MAX_COMPONENTS.\n");
2358 return -EINVAL;
2359 }
2360 adev->debugfs[adev->debugfs_count].files = files;
2361 adev->debugfs[adev->debugfs_count].num_files = nfiles;
2362 adev->debugfs_count = i;
2363#if defined(CONFIG_DEBUG_FS)
2364 drm_debugfs_create_files(files, nfiles,
2365 adev->ddev->control->debugfs_root,
2366 adev->ddev->control);
2367 drm_debugfs_create_files(files, nfiles,
2368 adev->ddev->primary->debugfs_root,
2369 adev->ddev->primary);
2370#endif
2371 return 0;
2372}
2373
2374static void amdgpu_debugfs_remove_files(struct amdgpu_device *adev)
2375{
2376#if defined(CONFIG_DEBUG_FS)
2377 unsigned i;
2378
2379 for (i = 0; i < adev->debugfs_count; i++) {
2380 drm_debugfs_remove_files(adev->debugfs[i].files,
2381 adev->debugfs[i].num_files,
2382 adev->ddev->control);
2383 drm_debugfs_remove_files(adev->debugfs[i].files,
2384 adev->debugfs[i].num_files,
2385 adev->ddev->primary);
2386 }
2387#endif
2388}
2389
2390#if defined(CONFIG_DEBUG_FS)
2391
2392static ssize_t amdgpu_debugfs_regs_read(struct file *f, char __user *buf,
2393 size_t size, loff_t *pos)
2394{
2395 struct amdgpu_device *adev = f->f_inode->i_private;
2396 ssize_t result = 0;
2397 int r;
Tom St Denisbd122672016-07-28 09:39:22 -04002398 bool pm_pg_lock, use_bank;
Tom St Denis566281592016-06-27 11:55:07 -04002399 unsigned instance_bank, sh_bank, se_bank;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002400
2401 if (size & 0x3 || *pos & 0x3)
2402 return -EINVAL;
2403
Tom St Denisbd122672016-07-28 09:39:22 -04002404 /* are we reading registers for which a PG lock is necessary? */
2405 pm_pg_lock = (*pos >> 23) & 1;
2406
Tom St Denis566281592016-06-27 11:55:07 -04002407 if (*pos & (1ULL << 62)) {
2408 se_bank = (*pos >> 24) & 0x3FF;
2409 sh_bank = (*pos >> 34) & 0x3FF;
2410 instance_bank = (*pos >> 44) & 0x3FF;
2411 use_bank = 1;
Tom St Denis566281592016-06-27 11:55:07 -04002412 } else {
2413 use_bank = 0;
2414 }
2415
Tom St Denisbd122672016-07-28 09:39:22 -04002416 *pos &= 0x3FFFF;
2417
Tom St Denis566281592016-06-27 11:55:07 -04002418 if (use_bank) {
2419 if (sh_bank >= adev->gfx.config.max_sh_per_se ||
2420 se_bank >= adev->gfx.config.max_shader_engines)
2421 return -EINVAL;
2422 mutex_lock(&adev->grbm_idx_mutex);
2423 amdgpu_gfx_select_se_sh(adev, se_bank,
2424 sh_bank, instance_bank);
2425 }
2426
Tom St Denisbd122672016-07-28 09:39:22 -04002427 if (pm_pg_lock)
2428 mutex_lock(&adev->pm.mutex);
2429
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002430 while (size) {
2431 uint32_t value;
2432
2433 if (*pos > adev->rmmio_size)
Tom St Denis566281592016-06-27 11:55:07 -04002434 goto end;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002435
2436 value = RREG32(*pos >> 2);
2437 r = put_user(value, (uint32_t *)buf);
Tom St Denis566281592016-06-27 11:55:07 -04002438 if (r) {
2439 result = r;
2440 goto end;
2441 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002442
2443 result += 4;
2444 buf += 4;
2445 *pos += 4;
2446 size -= 4;
2447 }
2448
Tom St Denis566281592016-06-27 11:55:07 -04002449end:
2450 if (use_bank) {
2451 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
2452 mutex_unlock(&adev->grbm_idx_mutex);
2453 }
2454
Tom St Denisbd122672016-07-28 09:39:22 -04002455 if (pm_pg_lock)
2456 mutex_unlock(&adev->pm.mutex);
2457
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002458 return result;
2459}
2460
2461static ssize_t amdgpu_debugfs_regs_write(struct file *f, const char __user *buf,
2462 size_t size, loff_t *pos)
2463{
2464 struct amdgpu_device *adev = f->f_inode->i_private;
2465 ssize_t result = 0;
2466 int r;
2467
2468 if (size & 0x3 || *pos & 0x3)
2469 return -EINVAL;
2470
2471 while (size) {
2472 uint32_t value;
2473
2474 if (*pos > adev->rmmio_size)
2475 return result;
2476
2477 r = get_user(value, (uint32_t *)buf);
2478 if (r)
2479 return r;
2480
2481 WREG32(*pos >> 2, value);
2482
2483 result += 4;
2484 buf += 4;
2485 *pos += 4;
2486 size -= 4;
2487 }
2488
2489 return result;
2490}
2491
Tom St Denisadcec282016-04-15 13:08:44 -04002492static ssize_t amdgpu_debugfs_regs_pcie_read(struct file *f, char __user *buf,
2493 size_t size, loff_t *pos)
2494{
2495 struct amdgpu_device *adev = f->f_inode->i_private;
2496 ssize_t result = 0;
2497 int r;
2498
2499 if (size & 0x3 || *pos & 0x3)
2500 return -EINVAL;
2501
2502 while (size) {
2503 uint32_t value;
2504
2505 value = RREG32_PCIE(*pos >> 2);
2506 r = put_user(value, (uint32_t *)buf);
2507 if (r)
2508 return r;
2509
2510 result += 4;
2511 buf += 4;
2512 *pos += 4;
2513 size -= 4;
2514 }
2515
2516 return result;
2517}
2518
2519static ssize_t amdgpu_debugfs_regs_pcie_write(struct file *f, const char __user *buf,
2520 size_t size, loff_t *pos)
2521{
2522 struct amdgpu_device *adev = f->f_inode->i_private;
2523 ssize_t result = 0;
2524 int r;
2525
2526 if (size & 0x3 || *pos & 0x3)
2527 return -EINVAL;
2528
2529 while (size) {
2530 uint32_t value;
2531
2532 r = get_user(value, (uint32_t *)buf);
2533 if (r)
2534 return r;
2535
2536 WREG32_PCIE(*pos >> 2, value);
2537
2538 result += 4;
2539 buf += 4;
2540 *pos += 4;
2541 size -= 4;
2542 }
2543
2544 return result;
2545}
2546
2547static ssize_t amdgpu_debugfs_regs_didt_read(struct file *f, char __user *buf,
2548 size_t size, loff_t *pos)
2549{
2550 struct amdgpu_device *adev = f->f_inode->i_private;
2551 ssize_t result = 0;
2552 int r;
2553
2554 if (size & 0x3 || *pos & 0x3)
2555 return -EINVAL;
2556
2557 while (size) {
2558 uint32_t value;
2559
2560 value = RREG32_DIDT(*pos >> 2);
2561 r = put_user(value, (uint32_t *)buf);
2562 if (r)
2563 return r;
2564
2565 result += 4;
2566 buf += 4;
2567 *pos += 4;
2568 size -= 4;
2569 }
2570
2571 return result;
2572}
2573
2574static ssize_t amdgpu_debugfs_regs_didt_write(struct file *f, const char __user *buf,
2575 size_t size, loff_t *pos)
2576{
2577 struct amdgpu_device *adev = f->f_inode->i_private;
2578 ssize_t result = 0;
2579 int r;
2580
2581 if (size & 0x3 || *pos & 0x3)
2582 return -EINVAL;
2583
2584 while (size) {
2585 uint32_t value;
2586
2587 r = get_user(value, (uint32_t *)buf);
2588 if (r)
2589 return r;
2590
2591 WREG32_DIDT(*pos >> 2, value);
2592
2593 result += 4;
2594 buf += 4;
2595 *pos += 4;
2596 size -= 4;
2597 }
2598
2599 return result;
2600}
2601
2602static ssize_t amdgpu_debugfs_regs_smc_read(struct file *f, char __user *buf,
2603 size_t size, loff_t *pos)
2604{
2605 struct amdgpu_device *adev = f->f_inode->i_private;
2606 ssize_t result = 0;
2607 int r;
2608
2609 if (size & 0x3 || *pos & 0x3)
2610 return -EINVAL;
2611
2612 while (size) {
2613 uint32_t value;
2614
Tom St Denis6fc0dea2016-08-29 08:39:29 -04002615 value = RREG32_SMC(*pos);
Tom St Denisadcec282016-04-15 13:08:44 -04002616 r = put_user(value, (uint32_t *)buf);
2617 if (r)
2618 return r;
2619
2620 result += 4;
2621 buf += 4;
2622 *pos += 4;
2623 size -= 4;
2624 }
2625
2626 return result;
2627}
2628
2629static ssize_t amdgpu_debugfs_regs_smc_write(struct file *f, const char __user *buf,
2630 size_t size, loff_t *pos)
2631{
2632 struct amdgpu_device *adev = f->f_inode->i_private;
2633 ssize_t result = 0;
2634 int r;
2635
2636 if (size & 0x3 || *pos & 0x3)
2637 return -EINVAL;
2638
2639 while (size) {
2640 uint32_t value;
2641
2642 r = get_user(value, (uint32_t *)buf);
2643 if (r)
2644 return r;
2645
Tom St Denis6fc0dea2016-08-29 08:39:29 -04002646 WREG32_SMC(*pos, value);
Tom St Denisadcec282016-04-15 13:08:44 -04002647
2648 result += 4;
2649 buf += 4;
2650 *pos += 4;
2651 size -= 4;
2652 }
2653
2654 return result;
2655}
2656
Tom St Denis1e051412016-06-27 09:57:18 -04002657static ssize_t amdgpu_debugfs_gca_config_read(struct file *f, char __user *buf,
2658 size_t size, loff_t *pos)
2659{
2660 struct amdgpu_device *adev = f->f_inode->i_private;
2661 ssize_t result = 0;
2662 int r;
2663 uint32_t *config, no_regs = 0;
2664
2665 if (size & 0x3 || *pos & 0x3)
2666 return -EINVAL;
2667
2668 config = kmalloc(256 * sizeof(*config), GFP_KERNEL);
2669 if (!config)
2670 return -ENOMEM;
2671
2672 /* version, increment each time something is added */
Tom St Denise9f11dc2016-08-17 12:00:51 -04002673 config[no_regs++] = 2;
Tom St Denis1e051412016-06-27 09:57:18 -04002674 config[no_regs++] = adev->gfx.config.max_shader_engines;
2675 config[no_regs++] = adev->gfx.config.max_tile_pipes;
2676 config[no_regs++] = adev->gfx.config.max_cu_per_sh;
2677 config[no_regs++] = adev->gfx.config.max_sh_per_se;
2678 config[no_regs++] = adev->gfx.config.max_backends_per_se;
2679 config[no_regs++] = adev->gfx.config.max_texture_channel_caches;
2680 config[no_regs++] = adev->gfx.config.max_gprs;
2681 config[no_regs++] = adev->gfx.config.max_gs_threads;
2682 config[no_regs++] = adev->gfx.config.max_hw_contexts;
2683 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_frontend;
2684 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_backend;
2685 config[no_regs++] = adev->gfx.config.sc_hiz_tile_fifo_size;
2686 config[no_regs++] = adev->gfx.config.sc_earlyz_tile_fifo_size;
2687 config[no_regs++] = adev->gfx.config.num_tile_pipes;
2688 config[no_regs++] = adev->gfx.config.backend_enable_mask;
2689 config[no_regs++] = adev->gfx.config.mem_max_burst_length_bytes;
2690 config[no_regs++] = adev->gfx.config.mem_row_size_in_kb;
2691 config[no_regs++] = adev->gfx.config.shader_engine_tile_size;
2692 config[no_regs++] = adev->gfx.config.num_gpus;
2693 config[no_regs++] = adev->gfx.config.multi_gpu_tile_size;
2694 config[no_regs++] = adev->gfx.config.mc_arb_ramcfg;
2695 config[no_regs++] = adev->gfx.config.gb_addr_config;
2696 config[no_regs++] = adev->gfx.config.num_rbs;
2697
Tom St Denis89a8f302016-08-12 15:14:31 -04002698 /* rev==1 */
2699 config[no_regs++] = adev->rev_id;
2700 config[no_regs++] = adev->pg_flags;
2701 config[no_regs++] = adev->cg_flags;
2702
Tom St Denise9f11dc2016-08-17 12:00:51 -04002703 /* rev==2 */
2704 config[no_regs++] = adev->family;
2705 config[no_regs++] = adev->external_rev_id;
2706
Tom St Denis1e051412016-06-27 09:57:18 -04002707 while (size && (*pos < no_regs * 4)) {
2708 uint32_t value;
2709
2710 value = config[*pos >> 2];
2711 r = put_user(value, (uint32_t *)buf);
2712 if (r) {
2713 kfree(config);
2714 return r;
2715 }
2716
2717 result += 4;
2718 buf += 4;
2719 *pos += 4;
2720 size -= 4;
2721 }
2722
2723 kfree(config);
2724 return result;
2725}
2726
2727
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002728static const struct file_operations amdgpu_debugfs_regs_fops = {
2729 .owner = THIS_MODULE,
2730 .read = amdgpu_debugfs_regs_read,
2731 .write = amdgpu_debugfs_regs_write,
2732 .llseek = default_llseek
2733};
Tom St Denisadcec282016-04-15 13:08:44 -04002734static const struct file_operations amdgpu_debugfs_regs_didt_fops = {
2735 .owner = THIS_MODULE,
2736 .read = amdgpu_debugfs_regs_didt_read,
2737 .write = amdgpu_debugfs_regs_didt_write,
2738 .llseek = default_llseek
2739};
2740static const struct file_operations amdgpu_debugfs_regs_pcie_fops = {
2741 .owner = THIS_MODULE,
2742 .read = amdgpu_debugfs_regs_pcie_read,
2743 .write = amdgpu_debugfs_regs_pcie_write,
2744 .llseek = default_llseek
2745};
2746static const struct file_operations amdgpu_debugfs_regs_smc_fops = {
2747 .owner = THIS_MODULE,
2748 .read = amdgpu_debugfs_regs_smc_read,
2749 .write = amdgpu_debugfs_regs_smc_write,
2750 .llseek = default_llseek
2751};
2752
Tom St Denis1e051412016-06-27 09:57:18 -04002753static const struct file_operations amdgpu_debugfs_gca_config_fops = {
2754 .owner = THIS_MODULE,
2755 .read = amdgpu_debugfs_gca_config_read,
2756 .llseek = default_llseek
2757};
2758
Tom St Denisadcec282016-04-15 13:08:44 -04002759static const struct file_operations *debugfs_regs[] = {
2760 &amdgpu_debugfs_regs_fops,
2761 &amdgpu_debugfs_regs_didt_fops,
2762 &amdgpu_debugfs_regs_pcie_fops,
2763 &amdgpu_debugfs_regs_smc_fops,
Tom St Denis1e051412016-06-27 09:57:18 -04002764 &amdgpu_debugfs_gca_config_fops,
Tom St Denisadcec282016-04-15 13:08:44 -04002765};
2766
2767static const char *debugfs_regs_names[] = {
2768 "amdgpu_regs",
2769 "amdgpu_regs_didt",
2770 "amdgpu_regs_pcie",
2771 "amdgpu_regs_smc",
Tom St Denis1e051412016-06-27 09:57:18 -04002772 "amdgpu_gca_config",
Tom St Denisadcec282016-04-15 13:08:44 -04002773};
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002774
2775static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
2776{
2777 struct drm_minor *minor = adev->ddev->primary;
2778 struct dentry *ent, *root = minor->debugfs_root;
Tom St Denisadcec282016-04-15 13:08:44 -04002779 unsigned i, j;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002780
Tom St Denisadcec282016-04-15 13:08:44 -04002781 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
2782 ent = debugfs_create_file(debugfs_regs_names[i],
2783 S_IFREG | S_IRUGO, root,
2784 adev, debugfs_regs[i]);
2785 if (IS_ERR(ent)) {
2786 for (j = 0; j < i; j++) {
2787 debugfs_remove(adev->debugfs_regs[i]);
2788 adev->debugfs_regs[i] = NULL;
2789 }
2790 return PTR_ERR(ent);
2791 }
2792
2793 if (!i)
2794 i_size_write(ent->d_inode, adev->rmmio_size);
2795 adev->debugfs_regs[i] = ent;
2796 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002797
2798 return 0;
2799}
2800
2801static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev)
2802{
Tom St Denisadcec282016-04-15 13:08:44 -04002803 unsigned i;
2804
2805 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
2806 if (adev->debugfs_regs[i]) {
2807 debugfs_remove(adev->debugfs_regs[i]);
2808 adev->debugfs_regs[i] = NULL;
2809 }
2810 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002811}
2812
2813int amdgpu_debugfs_init(struct drm_minor *minor)
2814{
2815 return 0;
2816}
2817
2818void amdgpu_debugfs_cleanup(struct drm_minor *minor)
2819{
2820}
Alexander Kuleshov7cebc722015-06-27 13:16:05 +06002821#else
2822static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
2823{
2824 return 0;
2825}
2826static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev) { }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002827#endif