blob: 1996670b675151a18c1e180aff5e92a5f8af211b [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 */
28#include <linux/console.h>
29#include <linux/slab.h>
30#include <linux/debugfs.h>
31#include <drm/drmP.h>
32#include <drm/drm_crtc_helper.h>
33#include <drm/amdgpu_drm.h>
34#include <linux/vgaarb.h>
35#include <linux/vga_switcheroo.h>
36#include <linux/efi.h>
37#include "amdgpu.h"
38#include "amdgpu_i2c.h"
39#include "atom.h"
40#include "amdgpu_atombios.h"
Alex Deucherd0dd7f02015-11-11 19:45:06 -050041#include "amd_pcie.h"
Alex Deuchera2e73f52015-04-20 17:09:27 -040042#ifdef CONFIG_DRM_AMDGPU_CIK
43#include "cik.h"
44#endif
Alex Deucheraaa36a92015-04-20 17:31:14 -040045#include "vi.h"
Alex Deucherd38ceaf2015-04-20 16:55:21 -040046#include "bif/bif_4_1_d.h"
47
48static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev);
49static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev);
50
51static const char *amdgpu_asic_name[] = {
52 "BONAIRE",
53 "KAVERI",
54 "KABINI",
55 "HAWAII",
56 "MULLINS",
57 "TOPAZ",
58 "TONGA",
David Zhang48299f92015-07-08 01:05:16 +080059 "FIJI",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040060 "CARRIZO",
Samuel Li139f4912015-10-08 14:50:27 -040061 "STONEY",
Flora Cui2cc0c0b2016-03-14 18:33:29 -040062 "POLARIS10",
63 "POLARIS11",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040064 "LAST",
65};
66
67bool amdgpu_device_is_px(struct drm_device *dev)
68{
69 struct amdgpu_device *adev = dev->dev_private;
70
Jammy Zhou2f7d10b2015-07-22 11:29:01 +080071 if (adev->flags & AMD_IS_PX)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040072 return true;
73 return false;
74}
75
76/*
77 * MMIO register access helper functions.
78 */
79uint32_t amdgpu_mm_rreg(struct amdgpu_device *adev, uint32_t reg,
80 bool always_indirect)
81{
82 if ((reg * 4) < adev->rmmio_size && !always_indirect)
83 return readl(((void __iomem *)adev->rmmio) + (reg * 4));
84 else {
85 unsigned long flags;
86 uint32_t ret;
87
88 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
89 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
90 ret = readl(((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
91 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
92
93 return ret;
94 }
95}
96
97void amdgpu_mm_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v,
98 bool always_indirect)
99{
100 if ((reg * 4) < adev->rmmio_size && !always_indirect)
101 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
102 else {
103 unsigned long flags;
104
105 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
106 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
107 writel(v, ((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
108 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
109 }
110}
111
112u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 reg)
113{
114 if ((reg * 4) < adev->rio_mem_size)
115 return ioread32(adev->rio_mem + (reg * 4));
116 else {
117 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
118 return ioread32(adev->rio_mem + (mmMM_DATA * 4));
119 }
120}
121
122void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
123{
124
125 if ((reg * 4) < adev->rio_mem_size)
126 iowrite32(v, adev->rio_mem + (reg * 4));
127 else {
128 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
129 iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
130 }
131}
132
133/**
134 * amdgpu_mm_rdoorbell - read a doorbell dword
135 *
136 * @adev: amdgpu_device pointer
137 * @index: doorbell index
138 *
139 * Returns the value in the doorbell aperture at the
140 * requested doorbell index (CIK).
141 */
142u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
143{
144 if (index < adev->doorbell.num_doorbells) {
145 return readl(adev->doorbell.ptr + index);
146 } else {
147 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
148 return 0;
149 }
150}
151
152/**
153 * amdgpu_mm_wdoorbell - write a doorbell dword
154 *
155 * @adev: amdgpu_device pointer
156 * @index: doorbell index
157 * @v: value to write
158 *
159 * Writes @v to the doorbell aperture at the
160 * requested doorbell index (CIK).
161 */
162void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
163{
164 if (index < adev->doorbell.num_doorbells) {
165 writel(v, adev->doorbell.ptr + index);
166 } else {
167 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
168 }
169}
170
171/**
172 * amdgpu_invalid_rreg - dummy reg read function
173 *
174 * @adev: amdgpu device pointer
175 * @reg: offset of register
176 *
177 * Dummy register read function. Used for register blocks
178 * that certain asics don't have (all asics).
179 * Returns the value in the register.
180 */
181static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
182{
183 DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
184 BUG();
185 return 0;
186}
187
188/**
189 * amdgpu_invalid_wreg - dummy reg write function
190 *
191 * @adev: amdgpu device pointer
192 * @reg: offset of register
193 * @v: value to write to the register
194 *
195 * Dummy register read function. Used for register blocks
196 * that certain asics don't have (all asics).
197 */
198static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
199{
200 DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
201 reg, v);
202 BUG();
203}
204
205/**
206 * amdgpu_block_invalid_rreg - dummy reg read function
207 *
208 * @adev: amdgpu device pointer
209 * @block: offset of instance
210 * @reg: offset of register
211 *
212 * Dummy register read function. Used for register blocks
213 * that certain asics don't have (all asics).
214 * Returns the value in the register.
215 */
216static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
217 uint32_t block, uint32_t reg)
218{
219 DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
220 reg, block);
221 BUG();
222 return 0;
223}
224
225/**
226 * amdgpu_block_invalid_wreg - dummy reg write function
227 *
228 * @adev: amdgpu device pointer
229 * @block: offset of instance
230 * @reg: offset of register
231 * @v: value to write to the register
232 *
233 * Dummy register read function. Used for register blocks
234 * that certain asics don't have (all asics).
235 */
236static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
237 uint32_t block,
238 uint32_t reg, uint32_t v)
239{
240 DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
241 reg, block, v);
242 BUG();
243}
244
245static int amdgpu_vram_scratch_init(struct amdgpu_device *adev)
246{
247 int r;
248
249 if (adev->vram_scratch.robj == NULL) {
250 r = amdgpu_bo_create(adev, AMDGPU_GPU_PAGE_SIZE,
Alex Deucher857d9132015-08-27 00:14:16 -0400251 PAGE_SIZE, true, AMDGPU_GEM_DOMAIN_VRAM,
252 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED,
Christian König72d76682015-09-03 17:34:59 +0200253 NULL, NULL, &adev->vram_scratch.robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400254 if (r) {
255 return r;
256 }
257 }
258
259 r = amdgpu_bo_reserve(adev->vram_scratch.robj, false);
260 if (unlikely(r != 0))
261 return r;
262 r = amdgpu_bo_pin(adev->vram_scratch.robj,
263 AMDGPU_GEM_DOMAIN_VRAM, &adev->vram_scratch.gpu_addr);
264 if (r) {
265 amdgpu_bo_unreserve(adev->vram_scratch.robj);
266 return r;
267 }
268 r = amdgpu_bo_kmap(adev->vram_scratch.robj,
269 (void **)&adev->vram_scratch.ptr);
270 if (r)
271 amdgpu_bo_unpin(adev->vram_scratch.robj);
272 amdgpu_bo_unreserve(adev->vram_scratch.robj);
273
274 return r;
275}
276
277static void amdgpu_vram_scratch_fini(struct amdgpu_device *adev)
278{
279 int r;
280
281 if (adev->vram_scratch.robj == NULL) {
282 return;
283 }
284 r = amdgpu_bo_reserve(adev->vram_scratch.robj, false);
285 if (likely(r == 0)) {
286 amdgpu_bo_kunmap(adev->vram_scratch.robj);
287 amdgpu_bo_unpin(adev->vram_scratch.robj);
288 amdgpu_bo_unreserve(adev->vram_scratch.robj);
289 }
290 amdgpu_bo_unref(&adev->vram_scratch.robj);
291}
292
293/**
294 * amdgpu_program_register_sequence - program an array of registers.
295 *
296 * @adev: amdgpu_device pointer
297 * @registers: pointer to the register array
298 * @array_size: size of the register array
299 *
300 * Programs an array or registers with and and or masks.
301 * This is a helper for setting golden registers.
302 */
303void amdgpu_program_register_sequence(struct amdgpu_device *adev,
304 const u32 *registers,
305 const u32 array_size)
306{
307 u32 tmp, reg, and_mask, or_mask;
308 int i;
309
310 if (array_size % 3)
311 return;
312
313 for (i = 0; i < array_size; i +=3) {
314 reg = registers[i + 0];
315 and_mask = registers[i + 1];
316 or_mask = registers[i + 2];
317
318 if (and_mask == 0xffffffff) {
319 tmp = or_mask;
320 } else {
321 tmp = RREG32(reg);
322 tmp &= ~and_mask;
323 tmp |= or_mask;
324 }
325 WREG32(reg, tmp);
326 }
327}
328
329void amdgpu_pci_config_reset(struct amdgpu_device *adev)
330{
331 pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
332}
333
334/*
335 * GPU doorbell aperture helpers function.
336 */
337/**
338 * amdgpu_doorbell_init - Init doorbell driver information.
339 *
340 * @adev: amdgpu_device pointer
341 *
342 * Init doorbell driver information (CIK)
343 * Returns 0 on success, error on failure.
344 */
345static int amdgpu_doorbell_init(struct amdgpu_device *adev)
346{
347 /* doorbell bar mapping */
348 adev->doorbell.base = pci_resource_start(adev->pdev, 2);
349 adev->doorbell.size = pci_resource_len(adev->pdev, 2);
350
Christian Königedf600d2016-05-03 15:54:54 +0200351 adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400352 AMDGPU_DOORBELL_MAX_ASSIGNMENT+1);
353 if (adev->doorbell.num_doorbells == 0)
354 return -EINVAL;
355
356 adev->doorbell.ptr = ioremap(adev->doorbell.base, adev->doorbell.num_doorbells * sizeof(u32));
357 if (adev->doorbell.ptr == NULL) {
358 return -ENOMEM;
359 }
360 DRM_INFO("doorbell mmio base: 0x%08X\n", (uint32_t)adev->doorbell.base);
361 DRM_INFO("doorbell mmio size: %u\n", (unsigned)adev->doorbell.size);
362
363 return 0;
364}
365
366/**
367 * amdgpu_doorbell_fini - Tear down doorbell driver information.
368 *
369 * @adev: amdgpu_device pointer
370 *
371 * Tear down doorbell driver information (CIK)
372 */
373static void amdgpu_doorbell_fini(struct amdgpu_device *adev)
374{
375 iounmap(adev->doorbell.ptr);
376 adev->doorbell.ptr = NULL;
377}
378
379/**
380 * amdgpu_doorbell_get_kfd_info - Report doorbell configuration required to
381 * setup amdkfd
382 *
383 * @adev: amdgpu_device pointer
384 * @aperture_base: output returning doorbell aperture base physical address
385 * @aperture_size: output returning doorbell aperture size in bytes
386 * @start_offset: output returning # of doorbell bytes reserved for amdgpu.
387 *
388 * amdgpu and amdkfd share the doorbell aperture. amdgpu sets it up,
389 * takes doorbells required for its own rings and reports the setup to amdkfd.
390 * amdgpu reserved doorbells are at the start of the doorbell aperture.
391 */
392void amdgpu_doorbell_get_kfd_info(struct amdgpu_device *adev,
393 phys_addr_t *aperture_base,
394 size_t *aperture_size,
395 size_t *start_offset)
396{
397 /*
398 * The first num_doorbells are used by amdgpu.
399 * amdkfd takes whatever's left in the aperture.
400 */
401 if (adev->doorbell.size > adev->doorbell.num_doorbells * sizeof(u32)) {
402 *aperture_base = adev->doorbell.base;
403 *aperture_size = adev->doorbell.size;
404 *start_offset = adev->doorbell.num_doorbells * sizeof(u32);
405 } else {
406 *aperture_base = 0;
407 *aperture_size = 0;
408 *start_offset = 0;
409 }
410}
411
412/*
413 * amdgpu_wb_*()
414 * Writeback is the the method by which the the GPU updates special pages
415 * in memory with the status of certain GPU events (fences, ring pointers,
416 * etc.).
417 */
418
419/**
420 * amdgpu_wb_fini - Disable Writeback and free memory
421 *
422 * @adev: amdgpu_device pointer
423 *
424 * Disables Writeback and frees the Writeback memory (all asics).
425 * Used at driver shutdown.
426 */
427static void amdgpu_wb_fini(struct amdgpu_device *adev)
428{
429 if (adev->wb.wb_obj) {
430 if (!amdgpu_bo_reserve(adev->wb.wb_obj, false)) {
431 amdgpu_bo_kunmap(adev->wb.wb_obj);
432 amdgpu_bo_unpin(adev->wb.wb_obj);
433 amdgpu_bo_unreserve(adev->wb.wb_obj);
434 }
435 amdgpu_bo_unref(&adev->wb.wb_obj);
436 adev->wb.wb = NULL;
437 adev->wb.wb_obj = NULL;
438 }
439}
440
441/**
442 * amdgpu_wb_init- Init Writeback driver info and allocate memory
443 *
444 * @adev: amdgpu_device pointer
445 *
446 * Disables Writeback and frees the Writeback memory (all asics).
447 * Used at driver startup.
448 * Returns 0 on success or an -error on failure.
449 */
450static int amdgpu_wb_init(struct amdgpu_device *adev)
451{
452 int r;
453
454 if (adev->wb.wb_obj == NULL) {
455 r = amdgpu_bo_create(adev, AMDGPU_MAX_WB * 4, PAGE_SIZE, true,
Christian König72d76682015-09-03 17:34:59 +0200456 AMDGPU_GEM_DOMAIN_GTT, 0, NULL, NULL,
457 &adev->wb.wb_obj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400458 if (r) {
459 dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
460 return r;
461 }
462 r = amdgpu_bo_reserve(adev->wb.wb_obj, false);
463 if (unlikely(r != 0)) {
464 amdgpu_wb_fini(adev);
465 return r;
466 }
467 r = amdgpu_bo_pin(adev->wb.wb_obj, AMDGPU_GEM_DOMAIN_GTT,
468 &adev->wb.gpu_addr);
469 if (r) {
470 amdgpu_bo_unreserve(adev->wb.wb_obj);
471 dev_warn(adev->dev, "(%d) pin WB bo failed\n", r);
472 amdgpu_wb_fini(adev);
473 return r;
474 }
475 r = amdgpu_bo_kmap(adev->wb.wb_obj, (void **)&adev->wb.wb);
476 amdgpu_bo_unreserve(adev->wb.wb_obj);
477 if (r) {
478 dev_warn(adev->dev, "(%d) map WB bo failed\n", r);
479 amdgpu_wb_fini(adev);
480 return r;
481 }
482
483 adev->wb.num_wb = AMDGPU_MAX_WB;
484 memset(&adev->wb.used, 0, sizeof(adev->wb.used));
485
486 /* clear wb memory */
487 memset((char *)adev->wb.wb, 0, AMDGPU_GPU_PAGE_SIZE);
488 }
489
490 return 0;
491}
492
493/**
494 * amdgpu_wb_get - Allocate a wb entry
495 *
496 * @adev: amdgpu_device pointer
497 * @wb: wb index
498 *
499 * Allocate a wb slot for use by the driver (all asics).
500 * Returns 0 on success or -EINVAL on failure.
501 */
502int amdgpu_wb_get(struct amdgpu_device *adev, u32 *wb)
503{
504 unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
505 if (offset < adev->wb.num_wb) {
506 __set_bit(offset, adev->wb.used);
507 *wb = offset;
508 return 0;
509 } else {
510 return -EINVAL;
511 }
512}
513
514/**
515 * amdgpu_wb_free - Free a wb entry
516 *
517 * @adev: amdgpu_device pointer
518 * @wb: wb index
519 *
520 * Free a wb slot allocated for use by the driver (all asics)
521 */
522void amdgpu_wb_free(struct amdgpu_device *adev, u32 wb)
523{
524 if (wb < adev->wb.num_wb)
525 __clear_bit(wb, adev->wb.used);
526}
527
528/**
529 * amdgpu_vram_location - try to find VRAM location
530 * @adev: amdgpu device structure holding all necessary informations
531 * @mc: memory controller structure holding memory informations
532 * @base: base address at which to put VRAM
533 *
534 * Function will place try to place VRAM at base address provided
535 * as parameter (which is so far either PCI aperture address or
536 * for IGP TOM base address).
537 *
538 * If there is not enough space to fit the unvisible VRAM in the 32bits
539 * address space then we limit the VRAM size to the aperture.
540 *
541 * Note: We don't explicitly enforce VRAM start to be aligned on VRAM size,
542 * this shouldn't be a problem as we are using the PCI aperture as a reference.
543 * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but
544 * not IGP.
545 *
546 * Note: we use mc_vram_size as on some board we need to program the mc to
547 * cover the whole aperture even if VRAM size is inferior to aperture size
548 * Novell bug 204882 + along with lots of ubuntu ones
549 *
550 * Note: when limiting vram it's safe to overwritte real_vram_size because
551 * we are not in case where real_vram_size is inferior to mc_vram_size (ie
552 * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu
553 * ones)
554 *
555 * Note: IGP TOM addr should be the same as the aperture addr, we don't
556 * explicitly check for that thought.
557 *
558 * FIXME: when reducing VRAM size align new size on power of 2.
559 */
560void amdgpu_vram_location(struct amdgpu_device *adev, struct amdgpu_mc *mc, u64 base)
561{
562 uint64_t limit = (uint64_t)amdgpu_vram_limit << 20;
563
564 mc->vram_start = base;
565 if (mc->mc_vram_size > (adev->mc.mc_mask - base + 1)) {
566 dev_warn(adev->dev, "limiting VRAM to PCI aperture size\n");
567 mc->real_vram_size = mc->aper_size;
568 mc->mc_vram_size = mc->aper_size;
569 }
570 mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
571 if (limit && limit < mc->real_vram_size)
572 mc->real_vram_size = limit;
573 dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
574 mc->mc_vram_size >> 20, mc->vram_start,
575 mc->vram_end, mc->real_vram_size >> 20);
576}
577
578/**
579 * amdgpu_gtt_location - try to find GTT location
580 * @adev: amdgpu device structure holding all necessary informations
581 * @mc: memory controller structure holding memory informations
582 *
583 * Function will place try to place GTT before or after VRAM.
584 *
585 * If GTT size is bigger than space left then we ajust GTT size.
586 * Thus function will never fails.
587 *
588 * FIXME: when reducing GTT size align new size on power of 2.
589 */
590void amdgpu_gtt_location(struct amdgpu_device *adev, struct amdgpu_mc *mc)
591{
592 u64 size_af, size_bf;
593
594 size_af = ((adev->mc.mc_mask - mc->vram_end) + mc->gtt_base_align) & ~mc->gtt_base_align;
595 size_bf = mc->vram_start & ~mc->gtt_base_align;
596 if (size_bf > size_af) {
597 if (mc->gtt_size > size_bf) {
598 dev_warn(adev->dev, "limiting GTT\n");
599 mc->gtt_size = size_bf;
600 }
601 mc->gtt_start = (mc->vram_start & ~mc->gtt_base_align) - mc->gtt_size;
602 } else {
603 if (mc->gtt_size > size_af) {
604 dev_warn(adev->dev, "limiting GTT\n");
605 mc->gtt_size = size_af;
606 }
607 mc->gtt_start = (mc->vram_end + 1 + mc->gtt_base_align) & ~mc->gtt_base_align;
608 }
609 mc->gtt_end = mc->gtt_start + mc->gtt_size - 1;
610 dev_info(adev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
611 mc->gtt_size >> 20, mc->gtt_start, mc->gtt_end);
612}
613
614/*
615 * GPU helpers function.
616 */
617/**
618 * amdgpu_card_posted - check if the hw has already been initialized
619 *
620 * @adev: amdgpu_device pointer
621 *
622 * Check if the asic has been initialized (all asics).
623 * Used at driver startup.
624 * Returns true if initialized or false if not.
625 */
626bool amdgpu_card_posted(struct amdgpu_device *adev)
627{
628 uint32_t reg;
629
630 /* then check MEM_SIZE, in case the crtcs are off */
631 reg = RREG32(mmCONFIG_MEMSIZE);
632
633 if (reg)
634 return true;
635
636 return false;
637
638}
639
640/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400641 * amdgpu_dummy_page_init - init dummy page used by the driver
642 *
643 * @adev: amdgpu_device pointer
644 *
645 * Allocate the dummy page used by the driver (all asics).
646 * This dummy page is used by the driver as a filler for gart entries
647 * when pages are taken out of the GART
648 * Returns 0 on sucess, -ENOMEM on failure.
649 */
650int amdgpu_dummy_page_init(struct amdgpu_device *adev)
651{
652 if (adev->dummy_page.page)
653 return 0;
654 adev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
655 if (adev->dummy_page.page == NULL)
656 return -ENOMEM;
657 adev->dummy_page.addr = pci_map_page(adev->pdev, adev->dummy_page.page,
658 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
659 if (pci_dma_mapping_error(adev->pdev, adev->dummy_page.addr)) {
660 dev_err(&adev->pdev->dev, "Failed to DMA MAP the dummy page\n");
661 __free_page(adev->dummy_page.page);
662 adev->dummy_page.page = NULL;
663 return -ENOMEM;
664 }
665 return 0;
666}
667
668/**
669 * amdgpu_dummy_page_fini - free dummy page used by the driver
670 *
671 * @adev: amdgpu_device pointer
672 *
673 * Frees the dummy page used by the driver (all asics).
674 */
675void amdgpu_dummy_page_fini(struct amdgpu_device *adev)
676{
677 if (adev->dummy_page.page == NULL)
678 return;
679 pci_unmap_page(adev->pdev, adev->dummy_page.addr,
680 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
681 __free_page(adev->dummy_page.page);
682 adev->dummy_page.page = NULL;
683}
684
685
686/* ATOM accessor methods */
687/*
688 * ATOM is an interpreted byte code stored in tables in the vbios. The
689 * driver registers callbacks to access registers and the interpreter
690 * in the driver parses the tables and executes then to program specific
691 * actions (set display modes, asic init, etc.). See amdgpu_atombios.c,
692 * atombios.h, and atom.c
693 */
694
695/**
696 * cail_pll_read - read PLL register
697 *
698 * @info: atom card_info pointer
699 * @reg: PLL register offset
700 *
701 * Provides a PLL register accessor for the atom interpreter (r4xx+).
702 * Returns the value of the PLL register.
703 */
704static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
705{
706 return 0;
707}
708
709/**
710 * cail_pll_write - write PLL register
711 *
712 * @info: atom card_info pointer
713 * @reg: PLL register offset
714 * @val: value to write to the pll register
715 *
716 * Provides a PLL register accessor for the atom interpreter (r4xx+).
717 */
718static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
719{
720
721}
722
723/**
724 * cail_mc_read - read MC (Memory Controller) register
725 *
726 * @info: atom card_info pointer
727 * @reg: MC register offset
728 *
729 * Provides an MC register accessor for the atom interpreter (r4xx+).
730 * Returns the value of the MC register.
731 */
732static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
733{
734 return 0;
735}
736
737/**
738 * cail_mc_write - write MC (Memory Controller) register
739 *
740 * @info: atom card_info pointer
741 * @reg: MC register offset
742 * @val: value to write to the pll register
743 *
744 * Provides a MC register accessor for the atom interpreter (r4xx+).
745 */
746static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
747{
748
749}
750
751/**
752 * cail_reg_write - write MMIO register
753 *
754 * @info: atom card_info pointer
755 * @reg: MMIO register offset
756 * @val: value to write to the pll register
757 *
758 * Provides a MMIO register accessor for the atom interpreter (r4xx+).
759 */
760static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
761{
762 struct amdgpu_device *adev = info->dev->dev_private;
763
764 WREG32(reg, val);
765}
766
767/**
768 * cail_reg_read - read MMIO register
769 *
770 * @info: atom card_info pointer
771 * @reg: MMIO register offset
772 *
773 * Provides an MMIO register accessor for the atom interpreter (r4xx+).
774 * Returns the value of the MMIO register.
775 */
776static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
777{
778 struct amdgpu_device *adev = info->dev->dev_private;
779 uint32_t r;
780
781 r = RREG32(reg);
782 return r;
783}
784
785/**
786 * cail_ioreg_write - write IO register
787 *
788 * @info: atom card_info pointer
789 * @reg: IO register offset
790 * @val: value to write to the pll register
791 *
792 * Provides a IO register accessor for the atom interpreter (r4xx+).
793 */
794static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
795{
796 struct amdgpu_device *adev = info->dev->dev_private;
797
798 WREG32_IO(reg, val);
799}
800
801/**
802 * cail_ioreg_read - read IO register
803 *
804 * @info: atom card_info pointer
805 * @reg: IO register offset
806 *
807 * Provides an IO register accessor for the atom interpreter (r4xx+).
808 * Returns the value of the IO register.
809 */
810static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
811{
812 struct amdgpu_device *adev = info->dev->dev_private;
813 uint32_t r;
814
815 r = RREG32_IO(reg);
816 return r;
817}
818
819/**
820 * amdgpu_atombios_fini - free the driver info and callbacks for atombios
821 *
822 * @adev: amdgpu_device pointer
823 *
824 * Frees the driver info and register access callbacks for the ATOM
825 * interpreter (r4xx+).
826 * Called at driver shutdown.
827 */
828static void amdgpu_atombios_fini(struct amdgpu_device *adev)
829{
830 if (adev->mode_info.atom_context)
831 kfree(adev->mode_info.atom_context->scratch);
832 kfree(adev->mode_info.atom_context);
833 adev->mode_info.atom_context = NULL;
834 kfree(adev->mode_info.atom_card_info);
835 adev->mode_info.atom_card_info = NULL;
836}
837
838/**
839 * amdgpu_atombios_init - init the driver info and callbacks for atombios
840 *
841 * @adev: amdgpu_device pointer
842 *
843 * Initializes the driver info and register access callbacks for the
844 * ATOM interpreter (r4xx+).
845 * Returns 0 on sucess, -ENOMEM on failure.
846 * Called at driver startup.
847 */
848static int amdgpu_atombios_init(struct amdgpu_device *adev)
849{
850 struct card_info *atom_card_info =
851 kzalloc(sizeof(struct card_info), GFP_KERNEL);
852
853 if (!atom_card_info)
854 return -ENOMEM;
855
856 adev->mode_info.atom_card_info = atom_card_info;
857 atom_card_info->dev = adev->ddev;
858 atom_card_info->reg_read = cail_reg_read;
859 atom_card_info->reg_write = cail_reg_write;
860 /* needed for iio ops */
861 if (adev->rio_mem) {
862 atom_card_info->ioreg_read = cail_ioreg_read;
863 atom_card_info->ioreg_write = cail_ioreg_write;
864 } else {
865 DRM_ERROR("Unable to find PCI I/O BAR; using MMIO for ATOM IIO\n");
866 atom_card_info->ioreg_read = cail_reg_read;
867 atom_card_info->ioreg_write = cail_reg_write;
868 }
869 atom_card_info->mc_read = cail_mc_read;
870 atom_card_info->mc_write = cail_mc_write;
871 atom_card_info->pll_read = cail_pll_read;
872 atom_card_info->pll_write = cail_pll_write;
873
874 adev->mode_info.atom_context = amdgpu_atom_parse(atom_card_info, adev->bios);
875 if (!adev->mode_info.atom_context) {
876 amdgpu_atombios_fini(adev);
877 return -ENOMEM;
878 }
879
880 mutex_init(&adev->mode_info.atom_context->mutex);
881 amdgpu_atombios_scratch_regs_init(adev);
882 amdgpu_atom_allocate_fb_scratch(adev->mode_info.atom_context);
883 return 0;
884}
885
886/* if we get transitioned to only one device, take VGA back */
887/**
888 * amdgpu_vga_set_decode - enable/disable vga decode
889 *
890 * @cookie: amdgpu_device pointer
891 * @state: enable/disable vga decode
892 *
893 * Enable/disable vga decode (all asics).
894 * Returns VGA resource flags.
895 */
896static unsigned int amdgpu_vga_set_decode(void *cookie, bool state)
897{
898 struct amdgpu_device *adev = cookie;
899 amdgpu_asic_set_vga_state(adev, state);
900 if (state)
901 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
902 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
903 else
904 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
905}
906
907/**
908 * amdgpu_check_pot_argument - check that argument is a power of two
909 *
910 * @arg: value to check
911 *
912 * Validates that a certain argument is a power of two (all asics).
913 * Returns true if argument is valid.
914 */
915static bool amdgpu_check_pot_argument(int arg)
916{
917 return (arg & (arg - 1)) == 0;
918}
919
920/**
921 * amdgpu_check_arguments - validate module params
922 *
923 * @adev: amdgpu_device pointer
924 *
925 * Validates certain module parameters and updates
926 * the associated values used by the driver (all asics).
927 */
928static void amdgpu_check_arguments(struct amdgpu_device *adev)
929{
Chunming Zhou5b011232015-12-10 17:34:33 +0800930 if (amdgpu_sched_jobs < 4) {
931 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
932 amdgpu_sched_jobs);
933 amdgpu_sched_jobs = 4;
934 } else if (!amdgpu_check_pot_argument(amdgpu_sched_jobs)){
935 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
936 amdgpu_sched_jobs);
937 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
938 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400939
940 if (amdgpu_gart_size != -1) {
Christian Königc4e1a132016-03-17 16:25:15 +0100941 /* gtt size must be greater or equal to 32M */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400942 if (amdgpu_gart_size < 32) {
943 dev_warn(adev->dev, "gart size (%d) too small\n",
944 amdgpu_gart_size);
945 amdgpu_gart_size = -1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400946 }
947 }
948
949 if (!amdgpu_check_pot_argument(amdgpu_vm_size)) {
950 dev_warn(adev->dev, "VM size (%d) must be a power of 2\n",
951 amdgpu_vm_size);
Alex Deucher8dacc122015-05-11 16:20:58 -0400952 amdgpu_vm_size = 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400953 }
954
955 if (amdgpu_vm_size < 1) {
956 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
957 amdgpu_vm_size);
Alex Deucher8dacc122015-05-11 16:20:58 -0400958 amdgpu_vm_size = 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400959 }
960
961 /*
962 * Max GPUVM size for Cayman, SI and CI are 40 bits.
963 */
964 if (amdgpu_vm_size > 1024) {
965 dev_warn(adev->dev, "VM size (%d) too large, max is 1TB\n",
966 amdgpu_vm_size);
Alex Deucher8dacc122015-05-11 16:20:58 -0400967 amdgpu_vm_size = 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400968 }
969
970 /* defines number of bits in page table versus page directory,
971 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
972 * page table and the remaining bits are in the page directory */
973 if (amdgpu_vm_block_size == -1) {
974
975 /* Total bits covered by PD + PTs */
976 unsigned bits = ilog2(amdgpu_vm_size) + 18;
977
978 /* Make sure the PD is 4K in size up to 8GB address space.
979 Above that split equal between PD and PTs */
980 if (amdgpu_vm_size <= 8)
981 amdgpu_vm_block_size = bits - 9;
982 else
983 amdgpu_vm_block_size = (bits + 3) / 2;
984
985 } else if (amdgpu_vm_block_size < 9) {
986 dev_warn(adev->dev, "VM page table size (%d) too small\n",
987 amdgpu_vm_block_size);
988 amdgpu_vm_block_size = 9;
989 }
990
991 if (amdgpu_vm_block_size > 24 ||
992 (amdgpu_vm_size * 1024) < (1ull << amdgpu_vm_block_size)) {
993 dev_warn(adev->dev, "VM page table size (%d) too large\n",
994 amdgpu_vm_block_size);
995 amdgpu_vm_block_size = 9;
996 }
997}
998
999/**
1000 * amdgpu_switcheroo_set_state - set switcheroo state
1001 *
1002 * @pdev: pci dev pointer
Lukas Wunner16944672015-09-05 11:17:35 +02001003 * @state: vga_switcheroo state
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001004 *
1005 * Callback for the switcheroo driver. Suspends or resumes the
1006 * the asics before or after it is powered up using ACPI methods.
1007 */
1008static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1009{
1010 struct drm_device *dev = pci_get_drvdata(pdev);
1011
1012 if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
1013 return;
1014
1015 if (state == VGA_SWITCHEROO_ON) {
1016 unsigned d3_delay = dev->pdev->d3_delay;
1017
1018 printk(KERN_INFO "amdgpu: switched on\n");
1019 /* don't suspend or resume card normally */
1020 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1021
1022 amdgpu_resume_kms(dev, true, true);
1023
1024 dev->pdev->d3_delay = d3_delay;
1025
1026 dev->switch_power_state = DRM_SWITCH_POWER_ON;
1027 drm_kms_helper_poll_enable(dev);
1028 } else {
1029 printk(KERN_INFO "amdgpu: switched off\n");
1030 drm_kms_helper_poll_disable(dev);
1031 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1032 amdgpu_suspend_kms(dev, true, true);
1033 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1034 }
1035}
1036
1037/**
1038 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1039 *
1040 * @pdev: pci dev pointer
1041 *
1042 * Callback for the switcheroo driver. Check of the switcheroo
1043 * state can be changed.
1044 * Returns true if the state can be changed, false if not.
1045 */
1046static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1047{
1048 struct drm_device *dev = pci_get_drvdata(pdev);
1049
1050 /*
1051 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1052 * locking inversion with the driver load path. And the access here is
1053 * completely racy anyway. So don't bother with locking for now.
1054 */
1055 return dev->open_count == 0;
1056}
1057
1058static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1059 .set_gpu_state = amdgpu_switcheroo_set_state,
1060 .reprobe = NULL,
1061 .can_switch = amdgpu_switcheroo_can_switch,
1062};
1063
1064int amdgpu_set_clockgating_state(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001065 enum amd_ip_block_type block_type,
1066 enum amd_clockgating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001067{
1068 int i, r = 0;
1069
1070 for (i = 0; i < adev->num_ip_blocks; i++) {
1071 if (adev->ip_blocks[i].type == block_type) {
yanyang15fc3aee2015-05-22 14:39:35 -04001072 r = adev->ip_blocks[i].funcs->set_clockgating_state((void *)adev,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001073 state);
1074 if (r)
1075 return r;
1076 }
1077 }
1078 return r;
1079}
1080
1081int amdgpu_set_powergating_state(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001082 enum amd_ip_block_type block_type,
1083 enum amd_powergating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001084{
1085 int i, r = 0;
1086
1087 for (i = 0; i < adev->num_ip_blocks; i++) {
1088 if (adev->ip_blocks[i].type == block_type) {
yanyang15fc3aee2015-05-22 14:39:35 -04001089 r = adev->ip_blocks[i].funcs->set_powergating_state((void *)adev,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001090 state);
1091 if (r)
1092 return r;
1093 }
1094 }
1095 return r;
1096}
1097
1098const struct amdgpu_ip_block_version * amdgpu_get_ip_block(
1099 struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001100 enum amd_ip_block_type type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001101{
1102 int i;
1103
1104 for (i = 0; i < adev->num_ip_blocks; i++)
1105 if (adev->ip_blocks[i].type == type)
1106 return &adev->ip_blocks[i];
1107
1108 return NULL;
1109}
1110
1111/**
1112 * amdgpu_ip_block_version_cmp
1113 *
1114 * @adev: amdgpu_device pointer
yanyang15fc3aee2015-05-22 14:39:35 -04001115 * @type: enum amd_ip_block_type
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001116 * @major: major version
1117 * @minor: minor version
1118 *
1119 * return 0 if equal or greater
1120 * return 1 if smaller or the ip_block doesn't exist
1121 */
1122int amdgpu_ip_block_version_cmp(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001123 enum amd_ip_block_type type,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001124 u32 major, u32 minor)
1125{
1126 const struct amdgpu_ip_block_version *ip_block;
1127 ip_block = amdgpu_get_ip_block(adev, type);
1128
1129 if (ip_block && ((ip_block->major > major) ||
1130 ((ip_block->major == major) &&
1131 (ip_block->minor >= minor))))
1132 return 0;
1133
1134 return 1;
1135}
1136
1137static int amdgpu_early_init(struct amdgpu_device *adev)
1138{
Alex Deucheraaa36a92015-04-20 17:31:14 -04001139 int i, r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001140
1141 switch (adev->asic_type) {
Alex Deucheraaa36a92015-04-20 17:31:14 -04001142 case CHIP_TOPAZ:
1143 case CHIP_TONGA:
David Zhang48299f92015-07-08 01:05:16 +08001144 case CHIP_FIJI:
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001145 case CHIP_POLARIS11:
1146 case CHIP_POLARIS10:
Alex Deucheraaa36a92015-04-20 17:31:14 -04001147 case CHIP_CARRIZO:
Samuel Li39bb0c92015-10-08 16:31:43 -04001148 case CHIP_STONEY:
1149 if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
Alex Deucheraaa36a92015-04-20 17:31:14 -04001150 adev->family = AMDGPU_FAMILY_CZ;
1151 else
1152 adev->family = AMDGPU_FAMILY_VI;
1153
1154 r = vi_set_ip_blocks(adev);
1155 if (r)
1156 return r;
1157 break;
Alex Deuchera2e73f52015-04-20 17:09:27 -04001158#ifdef CONFIG_DRM_AMDGPU_CIK
1159 case CHIP_BONAIRE:
1160 case CHIP_HAWAII:
1161 case CHIP_KAVERI:
1162 case CHIP_KABINI:
1163 case CHIP_MULLINS:
1164 if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
1165 adev->family = AMDGPU_FAMILY_CI;
1166 else
1167 adev->family = AMDGPU_FAMILY_KV;
1168
1169 r = cik_set_ip_blocks(adev);
1170 if (r)
1171 return r;
1172 break;
1173#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001174 default:
1175 /* FIXME: not supported yet */
1176 return -EINVAL;
1177 }
1178
Alex Deucher8faf0e02015-07-28 11:50:31 -04001179 adev->ip_block_status = kcalloc(adev->num_ip_blocks,
1180 sizeof(struct amdgpu_ip_block_status), GFP_KERNEL);
1181 if (adev->ip_block_status == NULL)
Alex Deucherd8d090b2015-06-26 13:02:57 -04001182 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001183
1184 if (adev->ip_blocks == NULL) {
1185 DRM_ERROR("No IP blocks found!\n");
1186 return r;
1187 }
1188
1189 for (i = 0; i < adev->num_ip_blocks; i++) {
1190 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
1191 DRM_ERROR("disabled ip block: %d\n", i);
Alex Deucher8faf0e02015-07-28 11:50:31 -04001192 adev->ip_block_status[i].valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001193 } else {
1194 if (adev->ip_blocks[i].funcs->early_init) {
yanyang15fc3aee2015-05-22 14:39:35 -04001195 r = adev->ip_blocks[i].funcs->early_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001196 if (r == -ENOENT) {
Alex Deucher8faf0e02015-07-28 11:50:31 -04001197 adev->ip_block_status[i].valid = false;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001198 } else if (r) {
Tom St Denis88a907d2016-05-04 14:28:35 -04001199 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 -04001200 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001201 } else {
Alex Deucher8faf0e02015-07-28 11:50:31 -04001202 adev->ip_block_status[i].valid = true;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001203 }
Alex Deucher974e6b62015-07-10 13:59:44 -04001204 } else {
Alex Deucher8faf0e02015-07-28 11:50:31 -04001205 adev->ip_block_status[i].valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001206 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001207 }
1208 }
1209
1210 return 0;
1211}
1212
1213static int amdgpu_init(struct amdgpu_device *adev)
1214{
1215 int i, r;
1216
1217 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deucher8faf0e02015-07-28 11:50:31 -04001218 if (!adev->ip_block_status[i].valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001219 continue;
yanyang15fc3aee2015-05-22 14:39:35 -04001220 r = adev->ip_blocks[i].funcs->sw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001221 if (r) {
Tom St Denis822b2ce2016-05-05 10:23:40 -04001222 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 -04001223 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001224 }
Alex Deucher8faf0e02015-07-28 11:50:31 -04001225 adev->ip_block_status[i].sw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001226 /* need to do gmc hw init early so we can allocate gpu mem */
yanyang15fc3aee2015-05-22 14:39:35 -04001227 if (adev->ip_blocks[i].type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001228 r = amdgpu_vram_scratch_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001229 if (r) {
1230 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001231 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001232 }
yanyang15fc3aee2015-05-22 14:39:35 -04001233 r = adev->ip_blocks[i].funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001234 if (r) {
1235 DRM_ERROR("hw_init %d failed %d\n", i, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001236 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001237 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001238 r = amdgpu_wb_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001239 if (r) {
1240 DRM_ERROR("amdgpu_wb_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001241 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001242 }
Alex Deucher8faf0e02015-07-28 11:50:31 -04001243 adev->ip_block_status[i].hw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001244 }
1245 }
1246
1247 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deucher8faf0e02015-07-28 11:50:31 -04001248 if (!adev->ip_block_status[i].sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001249 continue;
1250 /* gmc hw init is done early */
yanyang15fc3aee2015-05-22 14:39:35 -04001251 if (adev->ip_blocks[i].type == AMD_IP_BLOCK_TYPE_GMC)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001252 continue;
yanyang15fc3aee2015-05-22 14:39:35 -04001253 r = adev->ip_blocks[i].funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001254 if (r) {
Tom St Denis822b2ce2016-05-05 10:23:40 -04001255 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 -04001256 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001257 }
Alex Deucher8faf0e02015-07-28 11:50:31 -04001258 adev->ip_block_status[i].hw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001259 }
1260
1261 return 0;
1262}
1263
1264static int amdgpu_late_init(struct amdgpu_device *adev)
1265{
1266 int i = 0, r;
1267
1268 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deucher8faf0e02015-07-28 11:50:31 -04001269 if (!adev->ip_block_status[i].valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001270 continue;
1271 /* enable clockgating to save power */
yanyang15fc3aee2015-05-22 14:39:35 -04001272 r = adev->ip_blocks[i].funcs->set_clockgating_state((void *)adev,
1273 AMD_CG_STATE_GATE);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001274 if (r) {
Tom St Denis822b2ce2016-05-05 10:23:40 -04001275 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 -04001276 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001277 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001278 if (adev->ip_blocks[i].funcs->late_init) {
yanyang15fc3aee2015-05-22 14:39:35 -04001279 r = adev->ip_blocks[i].funcs->late_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001280 if (r) {
Tom St Denis822b2ce2016-05-05 10:23:40 -04001281 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 -04001282 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001283 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001284 }
1285 }
1286
1287 return 0;
1288}
1289
1290static int amdgpu_fini(struct amdgpu_device *adev)
1291{
1292 int i, r;
1293
1294 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deucher8faf0e02015-07-28 11:50:31 -04001295 if (!adev->ip_block_status[i].hw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001296 continue;
yanyang15fc3aee2015-05-22 14:39:35 -04001297 if (adev->ip_blocks[i].type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001298 amdgpu_wb_fini(adev);
1299 amdgpu_vram_scratch_fini(adev);
1300 }
1301 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
yanyang15fc3aee2015-05-22 14:39:35 -04001302 r = adev->ip_blocks[i].funcs->set_clockgating_state((void *)adev,
1303 AMD_CG_STATE_UNGATE);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001304 if (r) {
Tom St Denis822b2ce2016-05-05 10:23:40 -04001305 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 -04001306 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001307 }
yanyang15fc3aee2015-05-22 14:39:35 -04001308 r = adev->ip_blocks[i].funcs->hw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001309 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001310 if (r) {
Tom St Denis822b2ce2016-05-05 10:23:40 -04001311 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 -05001312 }
Alex Deucher8faf0e02015-07-28 11:50:31 -04001313 adev->ip_block_status[i].hw = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001314 }
1315
1316 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deucher8faf0e02015-07-28 11:50:31 -04001317 if (!adev->ip_block_status[i].sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001318 continue;
yanyang15fc3aee2015-05-22 14:39:35 -04001319 r = adev->ip_blocks[i].funcs->sw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001320 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001321 if (r) {
Tom St Denis822b2ce2016-05-05 10:23:40 -04001322 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 -05001323 }
Alex Deucher8faf0e02015-07-28 11:50:31 -04001324 adev->ip_block_status[i].sw = false;
1325 adev->ip_block_status[i].valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001326 }
1327
Monk Liua6dcfd92016-05-19 14:36:34 +08001328 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
1329 if (adev->ip_blocks[i].funcs->late_fini)
1330 adev->ip_blocks[i].funcs->late_fini((void *)adev);
1331 }
1332
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001333 return 0;
1334}
1335
1336static int amdgpu_suspend(struct amdgpu_device *adev)
1337{
1338 int i, r;
1339
Flora Cuic5a93a22016-02-26 10:45:25 +08001340 /* ungate SMC block first */
1341 r = amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC,
1342 AMD_CG_STATE_UNGATE);
1343 if (r) {
1344 DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n",r);
1345 }
1346
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001347 for (i = adev->num_ip_blocks - 1; i >= 0; 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 /* ungate blocks so that suspend can properly shut them down */
Flora Cuic5a93a22016-02-26 10:45:25 +08001351 if (i != AMD_IP_BLOCK_TYPE_SMC) {
1352 r = adev->ip_blocks[i].funcs->set_clockgating_state((void *)adev,
1353 AMD_CG_STATE_UNGATE);
1354 if (r) {
Tom St Denis822b2ce2016-05-05 10:23:40 -04001355 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 +08001356 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001357 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001358 /* XXX handle errors */
1359 r = adev->ip_blocks[i].funcs->suspend(adev);
1360 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001361 if (r) {
Tom St Denis822b2ce2016-05-05 10:23:40 -04001362 DRM_ERROR("suspend of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001363 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001364 }
1365
1366 return 0;
1367}
1368
1369static int amdgpu_resume(struct amdgpu_device *adev)
1370{
1371 int i, r;
1372
1373 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deucher8faf0e02015-07-28 11:50:31 -04001374 if (!adev->ip_block_status[i].valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001375 continue;
1376 r = adev->ip_blocks[i].funcs->resume(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001377 if (r) {
Tom St Denis822b2ce2016-05-05 10:23:40 -04001378 DRM_ERROR("resume of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001379 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001380 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001381 }
1382
1383 return 0;
1384}
1385
1386/**
1387 * amdgpu_device_init - initialize the driver
1388 *
1389 * @adev: amdgpu_device pointer
1390 * @pdev: drm dev pointer
1391 * @pdev: pci dev pointer
1392 * @flags: driver flags
1393 *
1394 * Initializes the driver info and hw (all asics).
1395 * Returns 0 for success or an error on failure.
1396 * Called at driver startup.
1397 */
1398int amdgpu_device_init(struct amdgpu_device *adev,
1399 struct drm_device *ddev,
1400 struct pci_dev *pdev,
1401 uint32_t flags)
1402{
1403 int r, i;
1404 bool runtime = false;
1405
1406 adev->shutdown = false;
1407 adev->dev = &pdev->dev;
1408 adev->ddev = ddev;
1409 adev->pdev = pdev;
1410 adev->flags = flags;
Jammy Zhou2f7d10b2015-07-22 11:29:01 +08001411 adev->asic_type = flags & AMD_ASIC_MASK;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001412 adev->is_atom_bios = false;
1413 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
1414 adev->mc.gtt_size = 512 * 1024 * 1024;
1415 adev->accel_working = false;
1416 adev->num_rings = 0;
1417 adev->mman.buffer_funcs = NULL;
1418 adev->mman.buffer_funcs_ring = NULL;
1419 adev->vm_manager.vm_pte_funcs = NULL;
Christian König2d55e452016-02-08 17:37:38 +01001420 adev->vm_manager.vm_pte_num_rings = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001421 adev->gart.gart_funcs = NULL;
1422 adev->fence_context = fence_context_alloc(AMDGPU_MAX_RINGS);
1423
1424 adev->smc_rreg = &amdgpu_invalid_rreg;
1425 adev->smc_wreg = &amdgpu_invalid_wreg;
1426 adev->pcie_rreg = &amdgpu_invalid_rreg;
1427 adev->pcie_wreg = &amdgpu_invalid_wreg;
1428 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
1429 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
1430 adev->didt_rreg = &amdgpu_invalid_rreg;
1431 adev->didt_wreg = &amdgpu_invalid_wreg;
1432 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
1433 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
1434
Alex Deucher3e39ab92015-06-05 15:04:33 -04001435 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
1436 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
1437 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001438
1439 /* mutex initialization are all done here so we
1440 * can recall function without having locking issues */
Christian König8d0a7ce2015-11-03 20:58:50 +01001441 mutex_init(&adev->vm_manager.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001442 atomic_set(&adev->irq.ih.lock, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001443 mutex_init(&adev->pm.mutex);
1444 mutex_init(&adev->gfx.gpu_clock_mutex);
1445 mutex_init(&adev->srbm_mutex);
1446 mutex_init(&adev->grbm_idx_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001447 mutex_init(&adev->mn_lock);
1448 hash_init(adev->mn_hash);
1449
1450 amdgpu_check_arguments(adev);
1451
1452 /* Registers mapping */
1453 /* TODO: block userspace mapping of io register */
1454 spin_lock_init(&adev->mmio_idx_lock);
1455 spin_lock_init(&adev->smc_idx_lock);
1456 spin_lock_init(&adev->pcie_idx_lock);
1457 spin_lock_init(&adev->uvd_ctx_idx_lock);
1458 spin_lock_init(&adev->didt_idx_lock);
1459 spin_lock_init(&adev->audio_endpt_idx_lock);
1460
1461 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
1462 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
1463 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
1464 if (adev->rmmio == NULL) {
1465 return -ENOMEM;
1466 }
1467 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
1468 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
1469
1470 /* doorbell bar mapping */
1471 amdgpu_doorbell_init(adev);
1472
1473 /* io port mapping */
1474 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
1475 if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
1476 adev->rio_mem_size = pci_resource_len(adev->pdev, i);
1477 adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
1478 break;
1479 }
1480 }
1481 if (adev->rio_mem == NULL)
1482 DRM_ERROR("Unable to find PCI I/O BAR\n");
1483
1484 /* early init functions */
1485 r = amdgpu_early_init(adev);
1486 if (r)
1487 return r;
1488
1489 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
1490 /* this will fail for cards that aren't VGA class devices, just
1491 * ignore it */
1492 vga_client_register(adev->pdev, adev, NULL, amdgpu_vga_set_decode);
1493
1494 if (amdgpu_runtime_pm == 1)
1495 runtime = true;
Alex Deuchere9bef452016-04-25 13:12:18 -04001496 if (amdgpu_device_is_px(ddev))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001497 runtime = true;
1498 vga_switcheroo_register_client(adev->pdev, &amdgpu_switcheroo_ops, runtime);
1499 if (runtime)
1500 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
1501
1502 /* Read BIOS */
1503 if (!amdgpu_get_bios(adev))
1504 return -EINVAL;
1505 /* Must be an ATOMBIOS */
1506 if (!adev->is_atom_bios) {
1507 dev_err(adev->dev, "Expecting atombios for GPU\n");
1508 return -EINVAL;
1509 }
1510 r = amdgpu_atombios_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001511 if (r) {
1512 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001513 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001514 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001515
Alex Deucher7e471e62016-02-01 11:13:04 -05001516 /* See if the asic supports SR-IOV */
1517 adev->virtualization.supports_sr_iov =
1518 amdgpu_atombios_has_gpu_virtualization_table(adev);
1519
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001520 /* Post card if necessary */
Alex Deucher8cce2442016-02-01 11:23:15 -05001521 if (!amdgpu_card_posted(adev) ||
1522 adev->virtualization.supports_sr_iov) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001523 if (!adev->bios) {
1524 dev_err(adev->dev, "Card not posted and no BIOS - ignoring\n");
1525 return -EINVAL;
1526 }
1527 DRM_INFO("GPU not posted. posting now...\n");
1528 amdgpu_atom_asic_init(adev->mode_info.atom_context);
1529 }
1530
1531 /* Initialize clocks */
1532 r = amdgpu_atombios_get_clock_info(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001533 if (r) {
1534 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001535 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001536 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001537 /* init i2c buses */
1538 amdgpu_atombios_i2c_init(adev);
1539
1540 /* Fence driver */
1541 r = amdgpu_fence_driver_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001542 if (r) {
1543 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001544 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001545 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001546
1547 /* init the mode config */
1548 drm_mode_config_init(adev->ddev);
1549
1550 r = amdgpu_init(adev);
1551 if (r) {
Alex Deucher2c1a2782015-12-07 17:02:53 -05001552 dev_err(adev->dev, "amdgpu_init failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001553 amdgpu_fini(adev);
1554 return r;
1555 }
1556
1557 adev->accel_working = true;
1558
1559 amdgpu_fbdev_init(adev);
1560
1561 r = amdgpu_ib_pool_init(adev);
1562 if (r) {
1563 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
1564 return r;
1565 }
1566
1567 r = amdgpu_ib_ring_tests(adev);
1568 if (r)
1569 DRM_ERROR("ib ring test failed (%d).\n", r);
1570
1571 r = amdgpu_gem_debugfs_init(adev);
1572 if (r) {
1573 DRM_ERROR("registering gem debugfs failed (%d).\n", r);
1574 }
1575
1576 r = amdgpu_debugfs_regs_init(adev);
1577 if (r) {
1578 DRM_ERROR("registering register debugfs failed (%d).\n", r);
1579 }
1580
1581 if ((amdgpu_testing & 1)) {
1582 if (adev->accel_working)
1583 amdgpu_test_moves(adev);
1584 else
1585 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
1586 }
1587 if ((amdgpu_testing & 2)) {
1588 if (adev->accel_working)
1589 amdgpu_test_syncing(adev);
1590 else
1591 DRM_INFO("amdgpu: acceleration disabled, skipping sync tests\n");
1592 }
1593 if (amdgpu_benchmarking) {
1594 if (adev->accel_working)
1595 amdgpu_benchmark(adev, amdgpu_benchmarking);
1596 else
1597 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
1598 }
1599
1600 /* enable clockgating, etc. after ib tests, etc. since some blocks require
1601 * explicit gating rather than handling it automatically.
1602 */
1603 r = amdgpu_late_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001604 if (r) {
1605 dev_err(adev->dev, "amdgpu_late_init failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001606 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001607 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001608
1609 return 0;
1610}
1611
1612static void amdgpu_debugfs_remove_files(struct amdgpu_device *adev);
1613
1614/**
1615 * amdgpu_device_fini - tear down the driver
1616 *
1617 * @adev: amdgpu_device pointer
1618 *
1619 * Tear down the driver info (all asics).
1620 * Called at driver shutdown.
1621 */
1622void amdgpu_device_fini(struct amdgpu_device *adev)
1623{
1624 int r;
1625
1626 DRM_INFO("amdgpu: finishing device.\n");
1627 adev->shutdown = true;
1628 /* evict vram memory */
1629 amdgpu_bo_evict_vram(adev);
1630 amdgpu_ib_pool_fini(adev);
1631 amdgpu_fence_driver_fini(adev);
1632 amdgpu_fbdev_fini(adev);
1633 r = amdgpu_fini(adev);
Alex Deucher8faf0e02015-07-28 11:50:31 -04001634 kfree(adev->ip_block_status);
1635 adev->ip_block_status = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001636 adev->accel_working = false;
1637 /* free i2c buses */
1638 amdgpu_i2c_fini(adev);
1639 amdgpu_atombios_fini(adev);
1640 kfree(adev->bios);
1641 adev->bios = NULL;
1642 vga_switcheroo_unregister_client(adev->pdev);
1643 vga_client_register(adev->pdev, NULL, NULL, NULL);
1644 if (adev->rio_mem)
1645 pci_iounmap(adev->pdev, adev->rio_mem);
1646 adev->rio_mem = NULL;
1647 iounmap(adev->rmmio);
1648 adev->rmmio = NULL;
1649 amdgpu_doorbell_fini(adev);
1650 amdgpu_debugfs_regs_cleanup(adev);
1651 amdgpu_debugfs_remove_files(adev);
1652}
1653
1654
1655/*
1656 * Suspend & resume.
1657 */
1658/**
1659 * amdgpu_suspend_kms - initiate device suspend
1660 *
1661 * @pdev: drm dev pointer
1662 * @state: suspend state
1663 *
1664 * Puts the hw in the suspend state (all asics).
1665 * Returns 0 for success or an error on failure.
1666 * Called at driver suspend.
1667 */
1668int amdgpu_suspend_kms(struct drm_device *dev, bool suspend, bool fbcon)
1669{
1670 struct amdgpu_device *adev;
1671 struct drm_crtc *crtc;
1672 struct drm_connector *connector;
Alex Deucher5ceb54c2015-08-05 12:41:48 -04001673 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001674
1675 if (dev == NULL || dev->dev_private == NULL) {
1676 return -ENODEV;
1677 }
1678
1679 adev = dev->dev_private;
1680
1681 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
1682 return 0;
1683
1684 drm_kms_helper_poll_disable(dev);
1685
1686 /* turn off display hw */
Alex Deucher4c7fbc32015-09-23 14:32:06 -04001687 drm_modeset_lock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001688 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1689 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
1690 }
Alex Deucher4c7fbc32015-09-23 14:32:06 -04001691 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001692
Alex Deucher756e6882015-10-08 00:03:36 -04001693 /* unpin the front buffers and cursors */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001694 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Alex Deucher756e6882015-10-08 00:03:36 -04001695 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001696 struct amdgpu_framebuffer *rfb = to_amdgpu_framebuffer(crtc->primary->fb);
1697 struct amdgpu_bo *robj;
1698
Alex Deucher756e6882015-10-08 00:03:36 -04001699 if (amdgpu_crtc->cursor_bo) {
1700 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
1701 r = amdgpu_bo_reserve(aobj, false);
1702 if (r == 0) {
1703 amdgpu_bo_unpin(aobj);
1704 amdgpu_bo_unreserve(aobj);
1705 }
1706 }
1707
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001708 if (rfb == NULL || rfb->obj == NULL) {
1709 continue;
1710 }
1711 robj = gem_to_amdgpu_bo(rfb->obj);
1712 /* don't unpin kernel fb objects */
1713 if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
1714 r = amdgpu_bo_reserve(robj, false);
1715 if (r == 0) {
1716 amdgpu_bo_unpin(robj);
1717 amdgpu_bo_unreserve(robj);
1718 }
1719 }
1720 }
1721 /* evict vram memory */
1722 amdgpu_bo_evict_vram(adev);
1723
Alex Deucher5ceb54c2015-08-05 12:41:48 -04001724 amdgpu_fence_driver_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001725
1726 r = amdgpu_suspend(adev);
1727
1728 /* evict remaining vram memory */
1729 amdgpu_bo_evict_vram(adev);
1730
1731 pci_save_state(dev->pdev);
1732 if (suspend) {
1733 /* Shut down the device */
1734 pci_disable_device(dev->pdev);
1735 pci_set_power_state(dev->pdev, PCI_D3hot);
1736 }
1737
1738 if (fbcon) {
1739 console_lock();
1740 amdgpu_fbdev_set_suspend(adev, 1);
1741 console_unlock();
1742 }
1743 return 0;
1744}
1745
1746/**
1747 * amdgpu_resume_kms - initiate device resume
1748 *
1749 * @pdev: drm dev pointer
1750 *
1751 * Bring the hw back to operating state (all asics).
1752 * Returns 0 for success or an error on failure.
1753 * Called at driver resume.
1754 */
1755int amdgpu_resume_kms(struct drm_device *dev, bool resume, bool fbcon)
1756{
1757 struct drm_connector *connector;
1758 struct amdgpu_device *adev = dev->dev_private;
Alex Deucher756e6882015-10-08 00:03:36 -04001759 struct drm_crtc *crtc;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001760 int r;
1761
1762 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
1763 return 0;
1764
1765 if (fbcon) {
1766 console_lock();
1767 }
1768 if (resume) {
1769 pci_set_power_state(dev->pdev, PCI_D0);
1770 pci_restore_state(dev->pdev);
1771 if (pci_enable_device(dev->pdev)) {
1772 if (fbcon)
1773 console_unlock();
1774 return -1;
1775 }
1776 }
1777
1778 /* post card */
Flora Cuica198522016-02-04 15:10:08 +08001779 if (!amdgpu_card_posted(adev))
1780 amdgpu_atom_asic_init(adev->mode_info.atom_context);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001781
1782 r = amdgpu_resume(adev);
Flora Cuica198522016-02-04 15:10:08 +08001783 if (r)
1784 DRM_ERROR("amdgpu_resume failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001785
Alex Deucher5ceb54c2015-08-05 12:41:48 -04001786 amdgpu_fence_driver_resume(adev);
1787
Flora Cuica198522016-02-04 15:10:08 +08001788 if (resume) {
1789 r = amdgpu_ib_ring_tests(adev);
1790 if (r)
1791 DRM_ERROR("ib ring test failed (%d).\n", r);
1792 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001793
1794 r = amdgpu_late_init(adev);
1795 if (r)
1796 return r;
1797
Alex Deucher756e6882015-10-08 00:03:36 -04001798 /* pin cursors */
1799 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1800 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
1801
1802 if (amdgpu_crtc->cursor_bo) {
1803 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
1804 r = amdgpu_bo_reserve(aobj, false);
1805 if (r == 0) {
1806 r = amdgpu_bo_pin(aobj,
1807 AMDGPU_GEM_DOMAIN_VRAM,
1808 &amdgpu_crtc->cursor_addr);
1809 if (r != 0)
1810 DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
1811 amdgpu_bo_unreserve(aobj);
1812 }
1813 }
1814 }
1815
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001816 /* blat the mode back in */
1817 if (fbcon) {
1818 drm_helper_resume_force_mode(dev);
1819 /* turn on display hw */
Alex Deucher4c7fbc32015-09-23 14:32:06 -04001820 drm_modeset_lock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001821 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1822 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
1823 }
Alex Deucher4c7fbc32015-09-23 14:32:06 -04001824 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001825 }
1826
1827 drm_kms_helper_poll_enable(dev);
Alex Deucher54fb2a52015-11-24 14:30:56 -05001828 drm_helper_hpd_irq_event(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001829
1830 if (fbcon) {
1831 amdgpu_fbdev_set_suspend(adev, 0);
1832 console_unlock();
1833 }
1834
1835 return 0;
1836}
1837
1838/**
1839 * amdgpu_gpu_reset - reset the asic
1840 *
1841 * @adev: amdgpu device pointer
1842 *
1843 * Attempt the reset the GPU if it has hung (all asics).
1844 * Returns 0 for success or an error on failure.
1845 */
1846int amdgpu_gpu_reset(struct amdgpu_device *adev)
1847{
1848 unsigned ring_sizes[AMDGPU_MAX_RINGS];
1849 uint32_t *ring_data[AMDGPU_MAX_RINGS];
1850
1851 bool saved = false;
1852
1853 int i, r;
1854 int resched;
1855
Marek Olšákd94aed52015-05-05 21:13:49 +02001856 atomic_inc(&adev->gpu_reset_counter);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001857
1858 /* block TTM */
1859 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
1860
1861 r = amdgpu_suspend(adev);
1862
1863 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
1864 struct amdgpu_ring *ring = adev->rings[i];
1865 if (!ring)
1866 continue;
1867
1868 ring_sizes[i] = amdgpu_ring_backup(ring, &ring_data[i]);
1869 if (ring_sizes[i]) {
1870 saved = true;
1871 dev_info(adev->dev, "Saved %d dwords of commands "
1872 "on ring %d.\n", ring_sizes[i], i);
1873 }
1874 }
1875
1876retry:
1877 r = amdgpu_asic_reset(adev);
Alex Deucherbfa99262016-01-15 11:59:48 -05001878 /* post card */
1879 amdgpu_atom_asic_init(adev->mode_info.atom_context);
1880
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001881 if (!r) {
1882 dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
1883 r = amdgpu_resume(adev);
1884 }
1885
1886 if (!r) {
1887 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
1888 struct amdgpu_ring *ring = adev->rings[i];
1889 if (!ring)
1890 continue;
1891
1892 amdgpu_ring_restore(ring, ring_sizes[i], ring_data[i]);
1893 ring_sizes[i] = 0;
1894 ring_data[i] = NULL;
1895 }
1896
1897 r = amdgpu_ib_ring_tests(adev);
1898 if (r) {
1899 dev_err(adev->dev, "ib ring test failed (%d).\n", r);
1900 if (saved) {
1901 saved = false;
1902 r = amdgpu_suspend(adev);
1903 goto retry;
1904 }
1905 }
1906 } else {
1907 amdgpu_fence_driver_force_completion(adev);
1908 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
1909 if (adev->rings[i])
1910 kfree(ring_data[i]);
1911 }
1912 }
1913
1914 drm_helper_resume_force_mode(adev->ddev);
1915
1916 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
1917 if (r) {
1918 /* bad news, how to tell it to userspace ? */
1919 dev_info(adev->dev, "GPU reset failed\n");
1920 }
1921
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001922 return r;
1923}
1924
Alex Deuchercd474ba2016-02-04 10:21:23 -05001925#define AMDGPU_DEFAULT_PCIE_GEN_MASK 0x30007 /* gen: chipset 1/2, asic 1/2/3 */
1926#define AMDGPU_DEFAULT_PCIE_MLW_MASK 0x2f0000 /* 1/2/4/8/16 lanes */
1927
Alex Deucherd0dd7f02015-11-11 19:45:06 -05001928void amdgpu_get_pcie_info(struct amdgpu_device *adev)
1929{
1930 u32 mask;
1931 int ret;
1932
Alex Deuchercd474ba2016-02-04 10:21:23 -05001933 if (amdgpu_pcie_gen_cap)
1934 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
1935
1936 if (amdgpu_pcie_lane_cap)
1937 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
1938
1939 /* covers APUs as well */
1940 if (pci_is_root_bus(adev->pdev->bus)) {
1941 if (adev->pm.pcie_gen_mask == 0)
1942 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
1943 if (adev->pm.pcie_mlw_mask == 0)
1944 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05001945 return;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05001946 }
Alex Deuchercd474ba2016-02-04 10:21:23 -05001947
1948 if (adev->pm.pcie_gen_mask == 0) {
1949 ret = drm_pcie_get_speed_cap_mask(adev->ddev, &mask);
1950 if (!ret) {
1951 adev->pm.pcie_gen_mask = (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
1952 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
1953 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
1954
1955 if (mask & DRM_PCIE_SPEED_25)
1956 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
1957 if (mask & DRM_PCIE_SPEED_50)
1958 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2;
1959 if (mask & DRM_PCIE_SPEED_80)
1960 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3;
1961 } else {
1962 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
1963 }
1964 }
1965 if (adev->pm.pcie_mlw_mask == 0) {
1966 ret = drm_pcie_get_max_link_width(adev->ddev, &mask);
1967 if (!ret) {
1968 switch (mask) {
1969 case 32:
1970 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
1971 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
1972 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
1973 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
1974 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
1975 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
1976 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
1977 break;
1978 case 16:
1979 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
1980 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
1981 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
1982 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
1983 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
1984 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
1985 break;
1986 case 12:
1987 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
1988 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
1989 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
1990 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
1991 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
1992 break;
1993 case 8:
1994 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
1995 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
1996 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
1997 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
1998 break;
1999 case 4:
2000 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2001 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2002 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2003 break;
2004 case 2:
2005 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2006 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2007 break;
2008 case 1:
2009 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
2010 break;
2011 default:
2012 break;
2013 }
2014 } else {
2015 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002016 }
2017 }
2018}
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002019
2020/*
2021 * Debugfs
2022 */
2023int amdgpu_debugfs_add_files(struct amdgpu_device *adev,
Nils Wallménius06ab6832016-05-02 12:46:15 -04002024 const struct drm_info_list *files,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002025 unsigned nfiles)
2026{
2027 unsigned i;
2028
2029 for (i = 0; i < adev->debugfs_count; i++) {
2030 if (adev->debugfs[i].files == files) {
2031 /* Already registered */
2032 return 0;
2033 }
2034 }
2035
2036 i = adev->debugfs_count + 1;
2037 if (i > AMDGPU_DEBUGFS_MAX_COMPONENTS) {
2038 DRM_ERROR("Reached maximum number of debugfs components.\n");
2039 DRM_ERROR("Report so we increase "
2040 "AMDGPU_DEBUGFS_MAX_COMPONENTS.\n");
2041 return -EINVAL;
2042 }
2043 adev->debugfs[adev->debugfs_count].files = files;
2044 adev->debugfs[adev->debugfs_count].num_files = nfiles;
2045 adev->debugfs_count = i;
2046#if defined(CONFIG_DEBUG_FS)
2047 drm_debugfs_create_files(files, nfiles,
2048 adev->ddev->control->debugfs_root,
2049 adev->ddev->control);
2050 drm_debugfs_create_files(files, nfiles,
2051 adev->ddev->primary->debugfs_root,
2052 adev->ddev->primary);
2053#endif
2054 return 0;
2055}
2056
2057static void amdgpu_debugfs_remove_files(struct amdgpu_device *adev)
2058{
2059#if defined(CONFIG_DEBUG_FS)
2060 unsigned i;
2061
2062 for (i = 0; i < adev->debugfs_count; i++) {
2063 drm_debugfs_remove_files(adev->debugfs[i].files,
2064 adev->debugfs[i].num_files,
2065 adev->ddev->control);
2066 drm_debugfs_remove_files(adev->debugfs[i].files,
2067 adev->debugfs[i].num_files,
2068 adev->ddev->primary);
2069 }
2070#endif
2071}
2072
2073#if defined(CONFIG_DEBUG_FS)
2074
2075static ssize_t amdgpu_debugfs_regs_read(struct file *f, char __user *buf,
2076 size_t size, loff_t *pos)
2077{
2078 struct amdgpu_device *adev = f->f_inode->i_private;
2079 ssize_t result = 0;
2080 int r;
2081
2082 if (size & 0x3 || *pos & 0x3)
2083 return -EINVAL;
2084
2085 while (size) {
2086 uint32_t value;
2087
2088 if (*pos > adev->rmmio_size)
2089 return result;
2090
2091 value = RREG32(*pos >> 2);
2092 r = put_user(value, (uint32_t *)buf);
2093 if (r)
2094 return r;
2095
2096 result += 4;
2097 buf += 4;
2098 *pos += 4;
2099 size -= 4;
2100 }
2101
2102 return result;
2103}
2104
2105static ssize_t amdgpu_debugfs_regs_write(struct file *f, const char __user *buf,
2106 size_t size, loff_t *pos)
2107{
2108 struct amdgpu_device *adev = f->f_inode->i_private;
2109 ssize_t result = 0;
2110 int r;
2111
2112 if (size & 0x3 || *pos & 0x3)
2113 return -EINVAL;
2114
2115 while (size) {
2116 uint32_t value;
2117
2118 if (*pos > adev->rmmio_size)
2119 return result;
2120
2121 r = get_user(value, (uint32_t *)buf);
2122 if (r)
2123 return r;
2124
2125 WREG32(*pos >> 2, value);
2126
2127 result += 4;
2128 buf += 4;
2129 *pos += 4;
2130 size -= 4;
2131 }
2132
2133 return result;
2134}
2135
Tom St Denisadcec282016-04-15 13:08:44 -04002136static ssize_t amdgpu_debugfs_regs_pcie_read(struct file *f, char __user *buf,
2137 size_t size, loff_t *pos)
2138{
2139 struct amdgpu_device *adev = f->f_inode->i_private;
2140 ssize_t result = 0;
2141 int r;
2142
2143 if (size & 0x3 || *pos & 0x3)
2144 return -EINVAL;
2145
2146 while (size) {
2147 uint32_t value;
2148
2149 value = RREG32_PCIE(*pos >> 2);
2150 r = put_user(value, (uint32_t *)buf);
2151 if (r)
2152 return r;
2153
2154 result += 4;
2155 buf += 4;
2156 *pos += 4;
2157 size -= 4;
2158 }
2159
2160 return result;
2161}
2162
2163static ssize_t amdgpu_debugfs_regs_pcie_write(struct file *f, const char __user *buf,
2164 size_t size, loff_t *pos)
2165{
2166 struct amdgpu_device *adev = f->f_inode->i_private;
2167 ssize_t result = 0;
2168 int r;
2169
2170 if (size & 0x3 || *pos & 0x3)
2171 return -EINVAL;
2172
2173 while (size) {
2174 uint32_t value;
2175
2176 r = get_user(value, (uint32_t *)buf);
2177 if (r)
2178 return r;
2179
2180 WREG32_PCIE(*pos >> 2, value);
2181
2182 result += 4;
2183 buf += 4;
2184 *pos += 4;
2185 size -= 4;
2186 }
2187
2188 return result;
2189}
2190
2191static ssize_t amdgpu_debugfs_regs_didt_read(struct file *f, char __user *buf,
2192 size_t size, loff_t *pos)
2193{
2194 struct amdgpu_device *adev = f->f_inode->i_private;
2195 ssize_t result = 0;
2196 int r;
2197
2198 if (size & 0x3 || *pos & 0x3)
2199 return -EINVAL;
2200
2201 while (size) {
2202 uint32_t value;
2203
2204 value = RREG32_DIDT(*pos >> 2);
2205 r = put_user(value, (uint32_t *)buf);
2206 if (r)
2207 return r;
2208
2209 result += 4;
2210 buf += 4;
2211 *pos += 4;
2212 size -= 4;
2213 }
2214
2215 return result;
2216}
2217
2218static ssize_t amdgpu_debugfs_regs_didt_write(struct file *f, const char __user *buf,
2219 size_t size, loff_t *pos)
2220{
2221 struct amdgpu_device *adev = f->f_inode->i_private;
2222 ssize_t result = 0;
2223 int r;
2224
2225 if (size & 0x3 || *pos & 0x3)
2226 return -EINVAL;
2227
2228 while (size) {
2229 uint32_t value;
2230
2231 r = get_user(value, (uint32_t *)buf);
2232 if (r)
2233 return r;
2234
2235 WREG32_DIDT(*pos >> 2, value);
2236
2237 result += 4;
2238 buf += 4;
2239 *pos += 4;
2240 size -= 4;
2241 }
2242
2243 return result;
2244}
2245
2246static ssize_t amdgpu_debugfs_regs_smc_read(struct file *f, char __user *buf,
2247 size_t size, loff_t *pos)
2248{
2249 struct amdgpu_device *adev = f->f_inode->i_private;
2250 ssize_t result = 0;
2251 int r;
2252
2253 if (size & 0x3 || *pos & 0x3)
2254 return -EINVAL;
2255
2256 while (size) {
2257 uint32_t value;
2258
2259 value = RREG32_SMC(*pos >> 2);
2260 r = put_user(value, (uint32_t *)buf);
2261 if (r)
2262 return r;
2263
2264 result += 4;
2265 buf += 4;
2266 *pos += 4;
2267 size -= 4;
2268 }
2269
2270 return result;
2271}
2272
2273static ssize_t amdgpu_debugfs_regs_smc_write(struct file *f, const char __user *buf,
2274 size_t size, loff_t *pos)
2275{
2276 struct amdgpu_device *adev = f->f_inode->i_private;
2277 ssize_t result = 0;
2278 int r;
2279
2280 if (size & 0x3 || *pos & 0x3)
2281 return -EINVAL;
2282
2283 while (size) {
2284 uint32_t value;
2285
2286 r = get_user(value, (uint32_t *)buf);
2287 if (r)
2288 return r;
2289
2290 WREG32_SMC(*pos >> 2, value);
2291
2292 result += 4;
2293 buf += 4;
2294 *pos += 4;
2295 size -= 4;
2296 }
2297
2298 return result;
2299}
2300
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002301static const struct file_operations amdgpu_debugfs_regs_fops = {
2302 .owner = THIS_MODULE,
2303 .read = amdgpu_debugfs_regs_read,
2304 .write = amdgpu_debugfs_regs_write,
2305 .llseek = default_llseek
2306};
Tom St Denisadcec282016-04-15 13:08:44 -04002307static const struct file_operations amdgpu_debugfs_regs_didt_fops = {
2308 .owner = THIS_MODULE,
2309 .read = amdgpu_debugfs_regs_didt_read,
2310 .write = amdgpu_debugfs_regs_didt_write,
2311 .llseek = default_llseek
2312};
2313static const struct file_operations amdgpu_debugfs_regs_pcie_fops = {
2314 .owner = THIS_MODULE,
2315 .read = amdgpu_debugfs_regs_pcie_read,
2316 .write = amdgpu_debugfs_regs_pcie_write,
2317 .llseek = default_llseek
2318};
2319static const struct file_operations amdgpu_debugfs_regs_smc_fops = {
2320 .owner = THIS_MODULE,
2321 .read = amdgpu_debugfs_regs_smc_read,
2322 .write = amdgpu_debugfs_regs_smc_write,
2323 .llseek = default_llseek
2324};
2325
2326static const struct file_operations *debugfs_regs[] = {
2327 &amdgpu_debugfs_regs_fops,
2328 &amdgpu_debugfs_regs_didt_fops,
2329 &amdgpu_debugfs_regs_pcie_fops,
2330 &amdgpu_debugfs_regs_smc_fops,
2331};
2332
2333static const char *debugfs_regs_names[] = {
2334 "amdgpu_regs",
2335 "amdgpu_regs_didt",
2336 "amdgpu_regs_pcie",
2337 "amdgpu_regs_smc",
2338};
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002339
2340static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
2341{
2342 struct drm_minor *minor = adev->ddev->primary;
2343 struct dentry *ent, *root = minor->debugfs_root;
Tom St Denisadcec282016-04-15 13:08:44 -04002344 unsigned i, j;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002345
Tom St Denisadcec282016-04-15 13:08:44 -04002346 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
2347 ent = debugfs_create_file(debugfs_regs_names[i],
2348 S_IFREG | S_IRUGO, root,
2349 adev, debugfs_regs[i]);
2350 if (IS_ERR(ent)) {
2351 for (j = 0; j < i; j++) {
2352 debugfs_remove(adev->debugfs_regs[i]);
2353 adev->debugfs_regs[i] = NULL;
2354 }
2355 return PTR_ERR(ent);
2356 }
2357
2358 if (!i)
2359 i_size_write(ent->d_inode, adev->rmmio_size);
2360 adev->debugfs_regs[i] = ent;
2361 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002362
2363 return 0;
2364}
2365
2366static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev)
2367{
Tom St Denisadcec282016-04-15 13:08:44 -04002368 unsigned i;
2369
2370 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
2371 if (adev->debugfs_regs[i]) {
2372 debugfs_remove(adev->debugfs_regs[i]);
2373 adev->debugfs_regs[i] = NULL;
2374 }
2375 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002376}
2377
2378int amdgpu_debugfs_init(struct drm_minor *minor)
2379{
2380 return 0;
2381}
2382
2383void amdgpu_debugfs_cleanup(struct drm_minor *minor)
2384{
2385}
Alexander Kuleshov7cebc722015-06-27 13:16:05 +06002386#else
2387static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
2388{
2389 return 0;
2390}
2391static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev) { }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002392#endif