blob: 077d4cc51a8c5ffd4fb26c757fb0299dfedd0b57 [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",
Alex Deucherb8122302015-10-14 17:14:16 -040062 "ELLESMERE",
63 "BAFFIN",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040064 "LAST",
65};
66
Alex Deucherbedf2a62016-03-02 12:10:20 -050067#if defined(CONFIG_VGA_SWITCHEROO)
68bool amdgpu_has_atpx_dgpu_power_cntl(void);
69#else
70static inline bool amdgpu_has_atpx_dgpu_power_cntl(void) { return false; }
71#endif
72
Alex Deucherd38ceaf2015-04-20 16:55:21 -040073bool amdgpu_device_is_px(struct drm_device *dev)
74{
75 struct amdgpu_device *adev = dev->dev_private;
76
Jammy Zhou2f7d10b2015-07-22 11:29:01 +080077 if (adev->flags & AMD_IS_PX)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040078 return true;
79 return false;
80}
81
82/*
83 * MMIO register access helper functions.
84 */
85uint32_t amdgpu_mm_rreg(struct amdgpu_device *adev, uint32_t reg,
86 bool always_indirect)
87{
88 if ((reg * 4) < adev->rmmio_size && !always_indirect)
89 return readl(((void __iomem *)adev->rmmio) + (reg * 4));
90 else {
91 unsigned long flags;
92 uint32_t ret;
93
94 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
95 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
96 ret = readl(((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
97 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
98
99 return ret;
100 }
101}
102
103void amdgpu_mm_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v,
104 bool always_indirect)
105{
106 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
357 adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
358 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{
836 if (adev->mode_info.atom_context)
837 kfree(adev->mode_info.atom_context->scratch);
838 kfree(adev->mode_info.atom_context);
839 adev->mode_info.atom_context = NULL;
840 kfree(adev->mode_info.atom_card_info);
841 adev->mode_info.atom_card_info = NULL;
842}
843
844/**
845 * amdgpu_atombios_init - init the driver info and callbacks for atombios
846 *
847 * @adev: amdgpu_device pointer
848 *
849 * Initializes the driver info and register access callbacks for the
850 * ATOM interpreter (r4xx+).
851 * Returns 0 on sucess, -ENOMEM on failure.
852 * Called at driver startup.
853 */
854static int amdgpu_atombios_init(struct amdgpu_device *adev)
855{
856 struct card_info *atom_card_info =
857 kzalloc(sizeof(struct card_info), GFP_KERNEL);
858
859 if (!atom_card_info)
860 return -ENOMEM;
861
862 adev->mode_info.atom_card_info = atom_card_info;
863 atom_card_info->dev = adev->ddev;
864 atom_card_info->reg_read = cail_reg_read;
865 atom_card_info->reg_write = cail_reg_write;
866 /* needed for iio ops */
867 if (adev->rio_mem) {
868 atom_card_info->ioreg_read = cail_ioreg_read;
869 atom_card_info->ioreg_write = cail_ioreg_write;
870 } else {
871 DRM_ERROR("Unable to find PCI I/O BAR; using MMIO for ATOM IIO\n");
872 atom_card_info->ioreg_read = cail_reg_read;
873 atom_card_info->ioreg_write = cail_reg_write;
874 }
875 atom_card_info->mc_read = cail_mc_read;
876 atom_card_info->mc_write = cail_mc_write;
877 atom_card_info->pll_read = cail_pll_read;
878 atom_card_info->pll_write = cail_pll_write;
879
880 adev->mode_info.atom_context = amdgpu_atom_parse(atom_card_info, adev->bios);
881 if (!adev->mode_info.atom_context) {
882 amdgpu_atombios_fini(adev);
883 return -ENOMEM;
884 }
885
886 mutex_init(&adev->mode_info.atom_context->mutex);
887 amdgpu_atombios_scratch_regs_init(adev);
888 amdgpu_atom_allocate_fb_scratch(adev->mode_info.atom_context);
889 return 0;
890}
891
892/* if we get transitioned to only one device, take VGA back */
893/**
894 * amdgpu_vga_set_decode - enable/disable vga decode
895 *
896 * @cookie: amdgpu_device pointer
897 * @state: enable/disable vga decode
898 *
899 * Enable/disable vga decode (all asics).
900 * Returns VGA resource flags.
901 */
902static unsigned int amdgpu_vga_set_decode(void *cookie, bool state)
903{
904 struct amdgpu_device *adev = cookie;
905 amdgpu_asic_set_vga_state(adev, state);
906 if (state)
907 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
908 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
909 else
910 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
911}
912
913/**
914 * amdgpu_check_pot_argument - check that argument is a power of two
915 *
916 * @arg: value to check
917 *
918 * Validates that a certain argument is a power of two (all asics).
919 * Returns true if argument is valid.
920 */
921static bool amdgpu_check_pot_argument(int arg)
922{
923 return (arg & (arg - 1)) == 0;
924}
925
926/**
927 * amdgpu_check_arguments - validate module params
928 *
929 * @adev: amdgpu_device pointer
930 *
931 * Validates certain module parameters and updates
932 * the associated values used by the driver (all asics).
933 */
934static void amdgpu_check_arguments(struct amdgpu_device *adev)
935{
Chunming Zhou5b011232015-12-10 17:34:33 +0800936 if (amdgpu_sched_jobs < 4) {
937 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
938 amdgpu_sched_jobs);
939 amdgpu_sched_jobs = 4;
940 } else if (!amdgpu_check_pot_argument(amdgpu_sched_jobs)){
941 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
942 amdgpu_sched_jobs);
943 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
944 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400945
946 if (amdgpu_gart_size != -1) {
Christian Königc4e1a132016-03-17 16:25:15 +0100947 /* gtt size must be greater or equal to 32M */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400948 if (amdgpu_gart_size < 32) {
949 dev_warn(adev->dev, "gart size (%d) too small\n",
950 amdgpu_gart_size);
951 amdgpu_gart_size = -1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400952 }
953 }
954
955 if (!amdgpu_check_pot_argument(amdgpu_vm_size)) {
956 dev_warn(adev->dev, "VM size (%d) must be a power of 2\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 if (amdgpu_vm_size < 1) {
962 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
963 amdgpu_vm_size);
Alex Deucher8dacc122015-05-11 16:20:58 -0400964 amdgpu_vm_size = 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400965 }
966
967 /*
968 * Max GPUVM size for Cayman, SI and CI are 40 bits.
969 */
970 if (amdgpu_vm_size > 1024) {
971 dev_warn(adev->dev, "VM size (%d) too large, max is 1TB\n",
972 amdgpu_vm_size);
Alex Deucher8dacc122015-05-11 16:20:58 -0400973 amdgpu_vm_size = 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400974 }
975
976 /* defines number of bits in page table versus page directory,
977 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
978 * page table and the remaining bits are in the page directory */
979 if (amdgpu_vm_block_size == -1) {
980
981 /* Total bits covered by PD + PTs */
982 unsigned bits = ilog2(amdgpu_vm_size) + 18;
983
984 /* Make sure the PD is 4K in size up to 8GB address space.
985 Above that split equal between PD and PTs */
986 if (amdgpu_vm_size <= 8)
987 amdgpu_vm_block_size = bits - 9;
988 else
989 amdgpu_vm_block_size = (bits + 3) / 2;
990
991 } else if (amdgpu_vm_block_size < 9) {
992 dev_warn(adev->dev, "VM page table size (%d) too small\n",
993 amdgpu_vm_block_size);
994 amdgpu_vm_block_size = 9;
995 }
996
997 if (amdgpu_vm_block_size > 24 ||
998 (amdgpu_vm_size * 1024) < (1ull << amdgpu_vm_block_size)) {
999 dev_warn(adev->dev, "VM page table size (%d) too large\n",
1000 amdgpu_vm_block_size);
1001 amdgpu_vm_block_size = 9;
1002 }
1003}
1004
1005/**
1006 * amdgpu_switcheroo_set_state - set switcheroo state
1007 *
1008 * @pdev: pci dev pointer
Lukas Wunner16944672015-09-05 11:17:35 +02001009 * @state: vga_switcheroo state
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001010 *
1011 * Callback for the switcheroo driver. Suspends or resumes the
1012 * the asics before or after it is powered up using ACPI methods.
1013 */
1014static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1015{
1016 struct drm_device *dev = pci_get_drvdata(pdev);
1017
1018 if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
1019 return;
1020
1021 if (state == VGA_SWITCHEROO_ON) {
1022 unsigned d3_delay = dev->pdev->d3_delay;
1023
1024 printk(KERN_INFO "amdgpu: switched on\n");
1025 /* don't suspend or resume card normally */
1026 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1027
1028 amdgpu_resume_kms(dev, true, true);
1029
1030 dev->pdev->d3_delay = d3_delay;
1031
1032 dev->switch_power_state = DRM_SWITCH_POWER_ON;
1033 drm_kms_helper_poll_enable(dev);
1034 } else {
1035 printk(KERN_INFO "amdgpu: switched off\n");
1036 drm_kms_helper_poll_disable(dev);
1037 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1038 amdgpu_suspend_kms(dev, true, true);
1039 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1040 }
1041}
1042
1043/**
1044 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1045 *
1046 * @pdev: pci dev pointer
1047 *
1048 * Callback for the switcheroo driver. Check of the switcheroo
1049 * state can be changed.
1050 * Returns true if the state can be changed, false if not.
1051 */
1052static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1053{
1054 struct drm_device *dev = pci_get_drvdata(pdev);
1055
1056 /*
1057 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1058 * locking inversion with the driver load path. And the access here is
1059 * completely racy anyway. So don't bother with locking for now.
1060 */
1061 return dev->open_count == 0;
1062}
1063
1064static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1065 .set_gpu_state = amdgpu_switcheroo_set_state,
1066 .reprobe = NULL,
1067 .can_switch = amdgpu_switcheroo_can_switch,
1068};
1069
1070int amdgpu_set_clockgating_state(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001071 enum amd_ip_block_type block_type,
1072 enum amd_clockgating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001073{
1074 int i, r = 0;
1075
1076 for (i = 0; i < adev->num_ip_blocks; i++) {
1077 if (adev->ip_blocks[i].type == block_type) {
yanyang15fc3aee2015-05-22 14:39:35 -04001078 r = adev->ip_blocks[i].funcs->set_clockgating_state((void *)adev,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001079 state);
1080 if (r)
1081 return r;
1082 }
1083 }
1084 return r;
1085}
1086
1087int amdgpu_set_powergating_state(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001088 enum amd_ip_block_type block_type,
1089 enum amd_powergating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001090{
1091 int i, r = 0;
1092
1093 for (i = 0; i < adev->num_ip_blocks; i++) {
1094 if (adev->ip_blocks[i].type == block_type) {
yanyang15fc3aee2015-05-22 14:39:35 -04001095 r = adev->ip_blocks[i].funcs->set_powergating_state((void *)adev,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001096 state);
1097 if (r)
1098 return r;
1099 }
1100 }
1101 return r;
1102}
1103
1104const struct amdgpu_ip_block_version * amdgpu_get_ip_block(
1105 struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001106 enum amd_ip_block_type type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001107{
1108 int i;
1109
1110 for (i = 0; i < adev->num_ip_blocks; i++)
1111 if (adev->ip_blocks[i].type == type)
1112 return &adev->ip_blocks[i];
1113
1114 return NULL;
1115}
1116
1117/**
1118 * amdgpu_ip_block_version_cmp
1119 *
1120 * @adev: amdgpu_device pointer
yanyang15fc3aee2015-05-22 14:39:35 -04001121 * @type: enum amd_ip_block_type
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001122 * @major: major version
1123 * @minor: minor version
1124 *
1125 * return 0 if equal or greater
1126 * return 1 if smaller or the ip_block doesn't exist
1127 */
1128int amdgpu_ip_block_version_cmp(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001129 enum amd_ip_block_type type,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001130 u32 major, u32 minor)
1131{
1132 const struct amdgpu_ip_block_version *ip_block;
1133 ip_block = amdgpu_get_ip_block(adev, type);
1134
1135 if (ip_block && ((ip_block->major > major) ||
1136 ((ip_block->major == major) &&
1137 (ip_block->minor >= minor))))
1138 return 0;
1139
1140 return 1;
1141}
1142
1143static int amdgpu_early_init(struct amdgpu_device *adev)
1144{
Alex Deucheraaa36a92015-04-20 17:31:14 -04001145 int i, r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001146
1147 switch (adev->asic_type) {
Alex Deucheraaa36a92015-04-20 17:31:14 -04001148 case CHIP_TOPAZ:
1149 case CHIP_TONGA:
David Zhang48299f92015-07-08 01:05:16 +08001150 case CHIP_FIJI:
Alex Deucheraaa36a92015-04-20 17:31:14 -04001151 case CHIP_CARRIZO:
Samuel Li39bb0c92015-10-08 16:31:43 -04001152 case CHIP_STONEY:
1153 if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
Alex Deucheraaa36a92015-04-20 17:31:14 -04001154 adev->family = AMDGPU_FAMILY_CZ;
1155 else
1156 adev->family = AMDGPU_FAMILY_VI;
1157
1158 r = vi_set_ip_blocks(adev);
1159 if (r)
1160 return r;
1161 break;
Alex Deuchera2e73f52015-04-20 17:09:27 -04001162#ifdef CONFIG_DRM_AMDGPU_CIK
1163 case CHIP_BONAIRE:
1164 case CHIP_HAWAII:
1165 case CHIP_KAVERI:
1166 case CHIP_KABINI:
1167 case CHIP_MULLINS:
1168 if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
1169 adev->family = AMDGPU_FAMILY_CI;
1170 else
1171 adev->family = AMDGPU_FAMILY_KV;
1172
1173 r = cik_set_ip_blocks(adev);
1174 if (r)
1175 return r;
1176 break;
1177#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001178 default:
1179 /* FIXME: not supported yet */
1180 return -EINVAL;
1181 }
1182
Alex Deucher8faf0e082015-07-28 11:50:31 -04001183 adev->ip_block_status = kcalloc(adev->num_ip_blocks,
1184 sizeof(struct amdgpu_ip_block_status), GFP_KERNEL);
1185 if (adev->ip_block_status == NULL)
Alex Deucherd8d090b2015-06-26 13:02:57 -04001186 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001187
1188 if (adev->ip_blocks == NULL) {
1189 DRM_ERROR("No IP blocks found!\n");
1190 return r;
1191 }
1192
1193 for (i = 0; i < adev->num_ip_blocks; i++) {
1194 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
1195 DRM_ERROR("disabled ip block: %d\n", i);
Alex Deucher8faf0e082015-07-28 11:50:31 -04001196 adev->ip_block_status[i].valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001197 } else {
1198 if (adev->ip_blocks[i].funcs->early_init) {
yanyang15fc3aee2015-05-22 14:39:35 -04001199 r = adev->ip_blocks[i].funcs->early_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001200 if (r == -ENOENT) {
Alex Deucher8faf0e082015-07-28 11:50:31 -04001201 adev->ip_block_status[i].valid = false;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001202 } else if (r) {
1203 DRM_ERROR("early_init %d failed %d\n", i, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001204 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001205 } else {
Alex Deucher8faf0e082015-07-28 11:50:31 -04001206 adev->ip_block_status[i].valid = true;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001207 }
Alex Deucher974e6b62015-07-10 13:59:44 -04001208 } else {
Alex Deucher8faf0e082015-07-28 11:50:31 -04001209 adev->ip_block_status[i].valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001210 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001211 }
1212 }
1213
1214 return 0;
1215}
1216
1217static int amdgpu_init(struct amdgpu_device *adev)
1218{
1219 int i, r;
1220
1221 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deucher8faf0e082015-07-28 11:50:31 -04001222 if (!adev->ip_block_status[i].valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001223 continue;
yanyang15fc3aee2015-05-22 14:39:35 -04001224 r = adev->ip_blocks[i].funcs->sw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001225 if (r) {
1226 DRM_ERROR("sw_init %d failed %d\n", i, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001227 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001228 }
Alex Deucher8faf0e082015-07-28 11:50:31 -04001229 adev->ip_block_status[i].sw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001230 /* need to do gmc hw init early so we can allocate gpu mem */
yanyang15fc3aee2015-05-22 14:39:35 -04001231 if (adev->ip_blocks[i].type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001232 r = amdgpu_vram_scratch_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001233 if (r) {
1234 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001235 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001236 }
yanyang15fc3aee2015-05-22 14:39:35 -04001237 r = adev->ip_blocks[i].funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001238 if (r) {
1239 DRM_ERROR("hw_init %d failed %d\n", i, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001240 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001241 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001242 r = amdgpu_wb_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001243 if (r) {
1244 DRM_ERROR("amdgpu_wb_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001245 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001246 }
Alex Deucher8faf0e082015-07-28 11:50:31 -04001247 adev->ip_block_status[i].hw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001248 }
1249 }
1250
1251 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deucher8faf0e082015-07-28 11:50:31 -04001252 if (!adev->ip_block_status[i].sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001253 continue;
1254 /* gmc hw init is done early */
yanyang15fc3aee2015-05-22 14:39:35 -04001255 if (adev->ip_blocks[i].type == AMD_IP_BLOCK_TYPE_GMC)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001256 continue;
yanyang15fc3aee2015-05-22 14:39:35 -04001257 r = adev->ip_blocks[i].funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001258 if (r) {
1259 DRM_ERROR("hw_init %d failed %d\n", i, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001260 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001261 }
Alex Deucher8faf0e082015-07-28 11:50:31 -04001262 adev->ip_block_status[i].hw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001263 }
1264
1265 return 0;
1266}
1267
1268static int amdgpu_late_init(struct amdgpu_device *adev)
1269{
1270 int i = 0, r;
1271
1272 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deucher8faf0e082015-07-28 11:50:31 -04001273 if (!adev->ip_block_status[i].valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001274 continue;
1275 /* enable clockgating to save power */
yanyang15fc3aee2015-05-22 14:39:35 -04001276 r = adev->ip_blocks[i].funcs->set_clockgating_state((void *)adev,
1277 AMD_CG_STATE_GATE);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001278 if (r) {
1279 DRM_ERROR("set_clockgating_state(gate) %d failed %d\n", i, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001280 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001281 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001282 if (adev->ip_blocks[i].funcs->late_init) {
yanyang15fc3aee2015-05-22 14:39:35 -04001283 r = adev->ip_blocks[i].funcs->late_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001284 if (r) {
1285 DRM_ERROR("late_init %d failed %d\n", i, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001286 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001287 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001288 }
1289 }
1290
1291 return 0;
1292}
1293
1294static int amdgpu_fini(struct amdgpu_device *adev)
1295{
1296 int i, r;
1297
1298 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deucher8faf0e082015-07-28 11:50:31 -04001299 if (!adev->ip_block_status[i].hw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001300 continue;
yanyang15fc3aee2015-05-22 14:39:35 -04001301 if (adev->ip_blocks[i].type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001302 amdgpu_wb_fini(adev);
1303 amdgpu_vram_scratch_fini(adev);
1304 }
1305 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
yanyang15fc3aee2015-05-22 14:39:35 -04001306 r = adev->ip_blocks[i].funcs->set_clockgating_state((void *)adev,
1307 AMD_CG_STATE_UNGATE);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001308 if (r) {
1309 DRM_ERROR("set_clockgating_state(ungate) %d failed %d\n", i, 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_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001313 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001314 if (r) {
1315 DRM_DEBUG("hw_fini %d failed %d\n", i, r);
1316 }
Alex Deucher8faf0e082015-07-28 11:50:31 -04001317 adev->ip_block_status[i].hw = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001318 }
1319
1320 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deucher8faf0e082015-07-28 11:50:31 -04001321 if (!adev->ip_block_status[i].sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001322 continue;
yanyang15fc3aee2015-05-22 14:39:35 -04001323 r = adev->ip_blocks[i].funcs->sw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001324 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001325 if (r) {
1326 DRM_DEBUG("sw_fini %d failed %d\n", i, r);
1327 }
Alex Deucher8faf0e082015-07-28 11:50:31 -04001328 adev->ip_block_status[i].sw = false;
1329 adev->ip_block_status[i].valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001330 }
1331
1332 return 0;
1333}
1334
1335static int amdgpu_suspend(struct amdgpu_device *adev)
1336{
1337 int i, r;
1338
1339 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deucher8faf0e082015-07-28 11:50:31 -04001340 if (!adev->ip_block_status[i].valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001341 continue;
1342 /* ungate blocks so that suspend can properly shut them down */
yanyang15fc3aee2015-05-22 14:39:35 -04001343 r = adev->ip_blocks[i].funcs->set_clockgating_state((void *)adev,
1344 AMD_CG_STATE_UNGATE);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001345 if (r) {
1346 DRM_ERROR("set_clockgating_state(ungate) %d failed %d\n", i, r);
1347 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001348 /* XXX handle errors */
1349 r = adev->ip_blocks[i].funcs->suspend(adev);
1350 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001351 if (r) {
1352 DRM_ERROR("suspend %d failed %d\n", i, r);
1353 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001354 }
1355
1356 return 0;
1357}
1358
1359static int amdgpu_resume(struct amdgpu_device *adev)
1360{
1361 int i, r;
1362
1363 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deucher8faf0e082015-07-28 11:50:31 -04001364 if (!adev->ip_block_status[i].valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001365 continue;
1366 r = adev->ip_blocks[i].funcs->resume(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001367 if (r) {
1368 DRM_ERROR("resume %d failed %d\n", i, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001369 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001370 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001371 }
1372
1373 return 0;
1374}
1375
1376/**
1377 * amdgpu_device_init - initialize the driver
1378 *
1379 * @adev: amdgpu_device pointer
1380 * @pdev: drm dev pointer
1381 * @pdev: pci dev pointer
1382 * @flags: driver flags
1383 *
1384 * Initializes the driver info and hw (all asics).
1385 * Returns 0 for success or an error on failure.
1386 * Called at driver startup.
1387 */
1388int amdgpu_device_init(struct amdgpu_device *adev,
1389 struct drm_device *ddev,
1390 struct pci_dev *pdev,
1391 uint32_t flags)
1392{
1393 int r, i;
1394 bool runtime = false;
1395
1396 adev->shutdown = false;
1397 adev->dev = &pdev->dev;
1398 adev->ddev = ddev;
1399 adev->pdev = pdev;
1400 adev->flags = flags;
Jammy Zhou2f7d10b2015-07-22 11:29:01 +08001401 adev->asic_type = flags & AMD_ASIC_MASK;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001402 adev->is_atom_bios = false;
1403 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
1404 adev->mc.gtt_size = 512 * 1024 * 1024;
1405 adev->accel_working = false;
1406 adev->num_rings = 0;
1407 adev->mman.buffer_funcs = NULL;
1408 adev->mman.buffer_funcs_ring = NULL;
1409 adev->vm_manager.vm_pte_funcs = NULL;
Christian König2d55e452016-02-08 17:37:38 +01001410 adev->vm_manager.vm_pte_num_rings = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001411 adev->gart.gart_funcs = NULL;
1412 adev->fence_context = fence_context_alloc(AMDGPU_MAX_RINGS);
1413
1414 adev->smc_rreg = &amdgpu_invalid_rreg;
1415 adev->smc_wreg = &amdgpu_invalid_wreg;
1416 adev->pcie_rreg = &amdgpu_invalid_rreg;
1417 adev->pcie_wreg = &amdgpu_invalid_wreg;
1418 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
1419 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
1420 adev->didt_rreg = &amdgpu_invalid_rreg;
1421 adev->didt_wreg = &amdgpu_invalid_wreg;
1422 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
1423 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
1424
Alex Deucher3e39ab92015-06-05 15:04:33 -04001425 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
1426 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
1427 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001428
1429 /* mutex initialization are all done here so we
1430 * can recall function without having locking issues */
Christian König8d0a7ce2015-11-03 20:58:50 +01001431 mutex_init(&adev->vm_manager.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001432 atomic_set(&adev->irq.ih.lock, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001433 mutex_init(&adev->pm.mutex);
1434 mutex_init(&adev->gfx.gpu_clock_mutex);
1435 mutex_init(&adev->srbm_mutex);
1436 mutex_init(&adev->grbm_idx_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001437 mutex_init(&adev->mn_lock);
1438 hash_init(adev->mn_hash);
1439
1440 amdgpu_check_arguments(adev);
1441
1442 /* Registers mapping */
1443 /* TODO: block userspace mapping of io register */
1444 spin_lock_init(&adev->mmio_idx_lock);
1445 spin_lock_init(&adev->smc_idx_lock);
1446 spin_lock_init(&adev->pcie_idx_lock);
1447 spin_lock_init(&adev->uvd_ctx_idx_lock);
1448 spin_lock_init(&adev->didt_idx_lock);
1449 spin_lock_init(&adev->audio_endpt_idx_lock);
1450
1451 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
1452 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
1453 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
1454 if (adev->rmmio == NULL) {
1455 return -ENOMEM;
1456 }
1457 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
1458 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
1459
1460 /* doorbell bar mapping */
1461 amdgpu_doorbell_init(adev);
1462
1463 /* io port mapping */
1464 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
1465 if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
1466 adev->rio_mem_size = pci_resource_len(adev->pdev, i);
1467 adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
1468 break;
1469 }
1470 }
1471 if (adev->rio_mem == NULL)
1472 DRM_ERROR("Unable to find PCI I/O BAR\n");
1473
1474 /* early init functions */
1475 r = amdgpu_early_init(adev);
1476 if (r)
1477 return r;
1478
1479 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
1480 /* this will fail for cards that aren't VGA class devices, just
1481 * ignore it */
1482 vga_client_register(adev->pdev, adev, NULL, amdgpu_vga_set_decode);
1483
1484 if (amdgpu_runtime_pm == 1)
1485 runtime = true;
Alex Deucherbedf2a62016-03-02 12:10:20 -05001486 if (amdgpu_device_is_px(ddev) && amdgpu_has_atpx_dgpu_power_cntl())
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001487 runtime = true;
1488 vga_switcheroo_register_client(adev->pdev, &amdgpu_switcheroo_ops, runtime);
1489 if (runtime)
1490 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
1491
1492 /* Read BIOS */
1493 if (!amdgpu_get_bios(adev))
1494 return -EINVAL;
1495 /* Must be an ATOMBIOS */
1496 if (!adev->is_atom_bios) {
1497 dev_err(adev->dev, "Expecting atombios for GPU\n");
1498 return -EINVAL;
1499 }
1500 r = amdgpu_atombios_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001501 if (r) {
1502 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001503 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001504 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001505
Alex Deucher7e471e62016-02-01 11:13:04 -05001506 /* See if the asic supports SR-IOV */
1507 adev->virtualization.supports_sr_iov =
1508 amdgpu_atombios_has_gpu_virtualization_table(adev);
1509
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001510 /* Post card if necessary */
Alex Deucher8cce2442016-02-01 11:23:15 -05001511 if (!amdgpu_card_posted(adev) ||
1512 adev->virtualization.supports_sr_iov) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001513 if (!adev->bios) {
1514 dev_err(adev->dev, "Card not posted and no BIOS - ignoring\n");
1515 return -EINVAL;
1516 }
1517 DRM_INFO("GPU not posted. posting now...\n");
1518 amdgpu_atom_asic_init(adev->mode_info.atom_context);
1519 }
1520
1521 /* Initialize clocks */
1522 r = amdgpu_atombios_get_clock_info(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001523 if (r) {
1524 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001525 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001526 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001527 /* init i2c buses */
1528 amdgpu_atombios_i2c_init(adev);
1529
1530 /* Fence driver */
1531 r = amdgpu_fence_driver_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001532 if (r) {
1533 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001534 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001535 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001536
1537 /* init the mode config */
1538 drm_mode_config_init(adev->ddev);
1539
1540 r = amdgpu_init(adev);
1541 if (r) {
Alex Deucher2c1a2782015-12-07 17:02:53 -05001542 dev_err(adev->dev, "amdgpu_init failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001543 amdgpu_fini(adev);
1544 return r;
1545 }
1546
1547 adev->accel_working = true;
1548
1549 amdgpu_fbdev_init(adev);
1550
1551 r = amdgpu_ib_pool_init(adev);
1552 if (r) {
1553 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
1554 return r;
1555 }
1556
1557 r = amdgpu_ib_ring_tests(adev);
1558 if (r)
1559 DRM_ERROR("ib ring test failed (%d).\n", r);
1560
1561 r = amdgpu_gem_debugfs_init(adev);
1562 if (r) {
1563 DRM_ERROR("registering gem debugfs failed (%d).\n", r);
1564 }
1565
1566 r = amdgpu_debugfs_regs_init(adev);
1567 if (r) {
1568 DRM_ERROR("registering register debugfs failed (%d).\n", r);
1569 }
1570
1571 if ((amdgpu_testing & 1)) {
1572 if (adev->accel_working)
1573 amdgpu_test_moves(adev);
1574 else
1575 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
1576 }
1577 if ((amdgpu_testing & 2)) {
1578 if (adev->accel_working)
1579 amdgpu_test_syncing(adev);
1580 else
1581 DRM_INFO("amdgpu: acceleration disabled, skipping sync tests\n");
1582 }
1583 if (amdgpu_benchmarking) {
1584 if (adev->accel_working)
1585 amdgpu_benchmark(adev, amdgpu_benchmarking);
1586 else
1587 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
1588 }
1589
1590 /* enable clockgating, etc. after ib tests, etc. since some blocks require
1591 * explicit gating rather than handling it automatically.
1592 */
1593 r = amdgpu_late_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001594 if (r) {
1595 dev_err(adev->dev, "amdgpu_late_init failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001596 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001597 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001598
1599 return 0;
1600}
1601
1602static void amdgpu_debugfs_remove_files(struct amdgpu_device *adev);
1603
1604/**
1605 * amdgpu_device_fini - tear down the driver
1606 *
1607 * @adev: amdgpu_device pointer
1608 *
1609 * Tear down the driver info (all asics).
1610 * Called at driver shutdown.
1611 */
1612void amdgpu_device_fini(struct amdgpu_device *adev)
1613{
1614 int r;
1615
1616 DRM_INFO("amdgpu: finishing device.\n");
1617 adev->shutdown = true;
1618 /* evict vram memory */
1619 amdgpu_bo_evict_vram(adev);
1620 amdgpu_ib_pool_fini(adev);
1621 amdgpu_fence_driver_fini(adev);
1622 amdgpu_fbdev_fini(adev);
1623 r = amdgpu_fini(adev);
Alex Deucher8faf0e082015-07-28 11:50:31 -04001624 kfree(adev->ip_block_status);
1625 adev->ip_block_status = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001626 adev->accel_working = false;
1627 /* free i2c buses */
1628 amdgpu_i2c_fini(adev);
1629 amdgpu_atombios_fini(adev);
1630 kfree(adev->bios);
1631 adev->bios = NULL;
1632 vga_switcheroo_unregister_client(adev->pdev);
1633 vga_client_register(adev->pdev, NULL, NULL, NULL);
1634 if (adev->rio_mem)
1635 pci_iounmap(adev->pdev, adev->rio_mem);
1636 adev->rio_mem = NULL;
1637 iounmap(adev->rmmio);
1638 adev->rmmio = NULL;
1639 amdgpu_doorbell_fini(adev);
1640 amdgpu_debugfs_regs_cleanup(adev);
1641 amdgpu_debugfs_remove_files(adev);
1642}
1643
1644
1645/*
1646 * Suspend & resume.
1647 */
1648/**
1649 * amdgpu_suspend_kms - initiate device suspend
1650 *
1651 * @pdev: drm dev pointer
1652 * @state: suspend state
1653 *
1654 * Puts the hw in the suspend state (all asics).
1655 * Returns 0 for success or an error on failure.
1656 * Called at driver suspend.
1657 */
1658int amdgpu_suspend_kms(struct drm_device *dev, bool suspend, bool fbcon)
1659{
1660 struct amdgpu_device *adev;
1661 struct drm_crtc *crtc;
1662 struct drm_connector *connector;
Alex Deucher5ceb54c2015-08-05 12:41:48 -04001663 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001664
1665 if (dev == NULL || dev->dev_private == NULL) {
1666 return -ENODEV;
1667 }
1668
1669 adev = dev->dev_private;
1670
1671 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
1672 return 0;
1673
1674 drm_kms_helper_poll_disable(dev);
1675
1676 /* turn off display hw */
Alex Deucher4c7fbc32015-09-23 14:32:06 -04001677 drm_modeset_lock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001678 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1679 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
1680 }
Alex Deucher4c7fbc32015-09-23 14:32:06 -04001681 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001682
Alex Deucher756e6882015-10-08 00:03:36 -04001683 /* unpin the front buffers and cursors */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001684 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Alex Deucher756e6882015-10-08 00:03:36 -04001685 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001686 struct amdgpu_framebuffer *rfb = to_amdgpu_framebuffer(crtc->primary->fb);
1687 struct amdgpu_bo *robj;
1688
Alex Deucher756e6882015-10-08 00:03:36 -04001689 if (amdgpu_crtc->cursor_bo) {
1690 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
1691 r = amdgpu_bo_reserve(aobj, false);
1692 if (r == 0) {
1693 amdgpu_bo_unpin(aobj);
1694 amdgpu_bo_unreserve(aobj);
1695 }
1696 }
1697
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001698 if (rfb == NULL || rfb->obj == NULL) {
1699 continue;
1700 }
1701 robj = gem_to_amdgpu_bo(rfb->obj);
1702 /* don't unpin kernel fb objects */
1703 if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
1704 r = amdgpu_bo_reserve(robj, false);
1705 if (r == 0) {
1706 amdgpu_bo_unpin(robj);
1707 amdgpu_bo_unreserve(robj);
1708 }
1709 }
1710 }
1711 /* evict vram memory */
1712 amdgpu_bo_evict_vram(adev);
1713
Alex Deucher5ceb54c2015-08-05 12:41:48 -04001714 amdgpu_fence_driver_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001715
1716 r = amdgpu_suspend(adev);
1717
1718 /* evict remaining vram memory */
1719 amdgpu_bo_evict_vram(adev);
1720
1721 pci_save_state(dev->pdev);
1722 if (suspend) {
1723 /* Shut down the device */
1724 pci_disable_device(dev->pdev);
1725 pci_set_power_state(dev->pdev, PCI_D3hot);
1726 }
1727
1728 if (fbcon) {
1729 console_lock();
1730 amdgpu_fbdev_set_suspend(adev, 1);
1731 console_unlock();
1732 }
1733 return 0;
1734}
1735
1736/**
1737 * amdgpu_resume_kms - initiate device resume
1738 *
1739 * @pdev: drm dev pointer
1740 *
1741 * Bring the hw back to operating state (all asics).
1742 * Returns 0 for success or an error on failure.
1743 * Called at driver resume.
1744 */
1745int amdgpu_resume_kms(struct drm_device *dev, bool resume, bool fbcon)
1746{
1747 struct drm_connector *connector;
1748 struct amdgpu_device *adev = dev->dev_private;
Alex Deucher756e6882015-10-08 00:03:36 -04001749 struct drm_crtc *crtc;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001750 int r;
1751
1752 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
1753 return 0;
1754
1755 if (fbcon) {
1756 console_lock();
1757 }
1758 if (resume) {
1759 pci_set_power_state(dev->pdev, PCI_D0);
1760 pci_restore_state(dev->pdev);
1761 if (pci_enable_device(dev->pdev)) {
1762 if (fbcon)
1763 console_unlock();
1764 return -1;
1765 }
1766 }
1767
1768 /* post card */
Flora Cuica198522016-02-04 15:10:08 +08001769 if (!amdgpu_card_posted(adev))
1770 amdgpu_atom_asic_init(adev->mode_info.atom_context);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001771
1772 r = amdgpu_resume(adev);
Flora Cuica198522016-02-04 15:10:08 +08001773 if (r)
1774 DRM_ERROR("amdgpu_resume failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001775
Alex Deucher5ceb54c2015-08-05 12:41:48 -04001776 amdgpu_fence_driver_resume(adev);
1777
Flora Cuica198522016-02-04 15:10:08 +08001778 if (resume) {
1779 r = amdgpu_ib_ring_tests(adev);
1780 if (r)
1781 DRM_ERROR("ib ring test failed (%d).\n", r);
1782 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001783
1784 r = amdgpu_late_init(adev);
1785 if (r)
1786 return r;
1787
Alex Deucher756e6882015-10-08 00:03:36 -04001788 /* pin cursors */
1789 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1790 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
1791
1792 if (amdgpu_crtc->cursor_bo) {
1793 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
1794 r = amdgpu_bo_reserve(aobj, false);
1795 if (r == 0) {
1796 r = amdgpu_bo_pin(aobj,
1797 AMDGPU_GEM_DOMAIN_VRAM,
1798 &amdgpu_crtc->cursor_addr);
1799 if (r != 0)
1800 DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
1801 amdgpu_bo_unreserve(aobj);
1802 }
1803 }
1804 }
1805
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001806 /* blat the mode back in */
1807 if (fbcon) {
1808 drm_helper_resume_force_mode(dev);
1809 /* turn on display hw */
Alex Deucher4c7fbc32015-09-23 14:32:06 -04001810 drm_modeset_lock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001811 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1812 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
1813 }
Alex Deucher4c7fbc32015-09-23 14:32:06 -04001814 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001815 }
1816
1817 drm_kms_helper_poll_enable(dev);
Alex Deucher54fb2a52015-11-24 14:30:56 -05001818 drm_helper_hpd_irq_event(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001819
1820 if (fbcon) {
1821 amdgpu_fbdev_set_suspend(adev, 0);
1822 console_unlock();
1823 }
1824
1825 return 0;
1826}
1827
1828/**
1829 * amdgpu_gpu_reset - reset the asic
1830 *
1831 * @adev: amdgpu device pointer
1832 *
1833 * Attempt the reset the GPU if it has hung (all asics).
1834 * Returns 0 for success or an error on failure.
1835 */
1836int amdgpu_gpu_reset(struct amdgpu_device *adev)
1837{
1838 unsigned ring_sizes[AMDGPU_MAX_RINGS];
1839 uint32_t *ring_data[AMDGPU_MAX_RINGS];
1840
1841 bool saved = false;
1842
1843 int i, r;
1844 int resched;
1845
Marek Olšákd94aed52015-05-05 21:13:49 +02001846 atomic_inc(&adev->gpu_reset_counter);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001847
1848 /* block TTM */
1849 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
1850
1851 r = amdgpu_suspend(adev);
1852
1853 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
1854 struct amdgpu_ring *ring = adev->rings[i];
1855 if (!ring)
1856 continue;
1857
1858 ring_sizes[i] = amdgpu_ring_backup(ring, &ring_data[i]);
1859 if (ring_sizes[i]) {
1860 saved = true;
1861 dev_info(adev->dev, "Saved %d dwords of commands "
1862 "on ring %d.\n", ring_sizes[i], i);
1863 }
1864 }
1865
1866retry:
1867 r = amdgpu_asic_reset(adev);
Alex Deucherbfa99262016-01-15 11:59:48 -05001868 /* post card */
1869 amdgpu_atom_asic_init(adev->mode_info.atom_context);
1870
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001871 if (!r) {
1872 dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
1873 r = amdgpu_resume(adev);
1874 }
1875
1876 if (!r) {
1877 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
1878 struct amdgpu_ring *ring = adev->rings[i];
1879 if (!ring)
1880 continue;
1881
1882 amdgpu_ring_restore(ring, ring_sizes[i], ring_data[i]);
1883 ring_sizes[i] = 0;
1884 ring_data[i] = NULL;
1885 }
1886
1887 r = amdgpu_ib_ring_tests(adev);
1888 if (r) {
1889 dev_err(adev->dev, "ib ring test failed (%d).\n", r);
1890 if (saved) {
1891 saved = false;
1892 r = amdgpu_suspend(adev);
1893 goto retry;
1894 }
1895 }
1896 } else {
1897 amdgpu_fence_driver_force_completion(adev);
1898 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
1899 if (adev->rings[i])
1900 kfree(ring_data[i]);
1901 }
1902 }
1903
1904 drm_helper_resume_force_mode(adev->ddev);
1905
1906 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
1907 if (r) {
1908 /* bad news, how to tell it to userspace ? */
1909 dev_info(adev->dev, "GPU reset failed\n");
1910 }
1911
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001912 return r;
1913}
1914
Alex Deuchercd474ba2016-02-04 10:21:23 -05001915#define AMDGPU_DEFAULT_PCIE_GEN_MASK 0x30007 /* gen: chipset 1/2, asic 1/2/3 */
1916#define AMDGPU_DEFAULT_PCIE_MLW_MASK 0x2f0000 /* 1/2/4/8/16 lanes */
1917
Alex Deucherd0dd7f02015-11-11 19:45:06 -05001918void amdgpu_get_pcie_info(struct amdgpu_device *adev)
1919{
1920 u32 mask;
1921 int ret;
1922
Alex Deuchercd474ba2016-02-04 10:21:23 -05001923 if (amdgpu_pcie_gen_cap)
1924 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
1925
1926 if (amdgpu_pcie_lane_cap)
1927 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
1928
1929 /* covers APUs as well */
1930 if (pci_is_root_bus(adev->pdev->bus)) {
1931 if (adev->pm.pcie_gen_mask == 0)
1932 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
1933 if (adev->pm.pcie_mlw_mask == 0)
1934 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05001935 return;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05001936 }
Alex Deuchercd474ba2016-02-04 10:21:23 -05001937
1938 if (adev->pm.pcie_gen_mask == 0) {
1939 ret = drm_pcie_get_speed_cap_mask(adev->ddev, &mask);
1940 if (!ret) {
1941 adev->pm.pcie_gen_mask = (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
1942 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
1943 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
1944
1945 if (mask & DRM_PCIE_SPEED_25)
1946 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
1947 if (mask & DRM_PCIE_SPEED_50)
1948 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2;
1949 if (mask & DRM_PCIE_SPEED_80)
1950 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3;
1951 } else {
1952 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
1953 }
1954 }
1955 if (adev->pm.pcie_mlw_mask == 0) {
1956 ret = drm_pcie_get_max_link_width(adev->ddev, &mask);
1957 if (!ret) {
1958 switch (mask) {
1959 case 32:
1960 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
1961 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
1962 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
1963 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
1964 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
1965 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
1966 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
1967 break;
1968 case 16:
1969 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
1970 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
1971 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
1972 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
1973 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
1974 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
1975 break;
1976 case 12:
1977 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
1978 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
1979 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
1980 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
1981 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
1982 break;
1983 case 8:
1984 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
1985 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
1986 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
1987 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
1988 break;
1989 case 4:
1990 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
1991 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
1992 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
1993 break;
1994 case 2:
1995 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
1996 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
1997 break;
1998 case 1:
1999 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
2000 break;
2001 default:
2002 break;
2003 }
2004 } else {
2005 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002006 }
2007 }
2008}
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002009
2010/*
2011 * Debugfs
2012 */
2013int amdgpu_debugfs_add_files(struct amdgpu_device *adev,
Nils Wallménius06ab6832016-05-02 12:46:15 -04002014 const struct drm_info_list *files,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002015 unsigned nfiles)
2016{
2017 unsigned i;
2018
2019 for (i = 0; i < adev->debugfs_count; i++) {
2020 if (adev->debugfs[i].files == files) {
2021 /* Already registered */
2022 return 0;
2023 }
2024 }
2025
2026 i = adev->debugfs_count + 1;
2027 if (i > AMDGPU_DEBUGFS_MAX_COMPONENTS) {
2028 DRM_ERROR("Reached maximum number of debugfs components.\n");
2029 DRM_ERROR("Report so we increase "
2030 "AMDGPU_DEBUGFS_MAX_COMPONENTS.\n");
2031 return -EINVAL;
2032 }
2033 adev->debugfs[adev->debugfs_count].files = files;
2034 adev->debugfs[adev->debugfs_count].num_files = nfiles;
2035 adev->debugfs_count = i;
2036#if defined(CONFIG_DEBUG_FS)
2037 drm_debugfs_create_files(files, nfiles,
2038 adev->ddev->control->debugfs_root,
2039 adev->ddev->control);
2040 drm_debugfs_create_files(files, nfiles,
2041 adev->ddev->primary->debugfs_root,
2042 adev->ddev->primary);
2043#endif
2044 return 0;
2045}
2046
2047static void amdgpu_debugfs_remove_files(struct amdgpu_device *adev)
2048{
2049#if defined(CONFIG_DEBUG_FS)
2050 unsigned i;
2051
2052 for (i = 0; i < adev->debugfs_count; i++) {
2053 drm_debugfs_remove_files(adev->debugfs[i].files,
2054 adev->debugfs[i].num_files,
2055 adev->ddev->control);
2056 drm_debugfs_remove_files(adev->debugfs[i].files,
2057 adev->debugfs[i].num_files,
2058 adev->ddev->primary);
2059 }
2060#endif
2061}
2062
2063#if defined(CONFIG_DEBUG_FS)
2064
2065static ssize_t amdgpu_debugfs_regs_read(struct file *f, char __user *buf,
2066 size_t size, loff_t *pos)
2067{
2068 struct amdgpu_device *adev = f->f_inode->i_private;
2069 ssize_t result = 0;
2070 int r;
2071
2072 if (size & 0x3 || *pos & 0x3)
2073 return -EINVAL;
2074
2075 while (size) {
2076 uint32_t value;
2077
2078 if (*pos > adev->rmmio_size)
2079 return result;
2080
2081 value = RREG32(*pos >> 2);
2082 r = put_user(value, (uint32_t *)buf);
2083 if (r)
2084 return r;
2085
2086 result += 4;
2087 buf += 4;
2088 *pos += 4;
2089 size -= 4;
2090 }
2091
2092 return result;
2093}
2094
2095static ssize_t amdgpu_debugfs_regs_write(struct file *f, const char __user *buf,
2096 size_t size, loff_t *pos)
2097{
2098 struct amdgpu_device *adev = f->f_inode->i_private;
2099 ssize_t result = 0;
2100 int r;
2101
2102 if (size & 0x3 || *pos & 0x3)
2103 return -EINVAL;
2104
2105 while (size) {
2106 uint32_t value;
2107
2108 if (*pos > adev->rmmio_size)
2109 return result;
2110
2111 r = get_user(value, (uint32_t *)buf);
2112 if (r)
2113 return r;
2114
2115 WREG32(*pos >> 2, value);
2116
2117 result += 4;
2118 buf += 4;
2119 *pos += 4;
2120 size -= 4;
2121 }
2122
2123 return result;
2124}
2125
2126static const struct file_operations amdgpu_debugfs_regs_fops = {
2127 .owner = THIS_MODULE,
2128 .read = amdgpu_debugfs_regs_read,
2129 .write = amdgpu_debugfs_regs_write,
2130 .llseek = default_llseek
2131};
2132
2133static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
2134{
2135 struct drm_minor *minor = adev->ddev->primary;
2136 struct dentry *ent, *root = minor->debugfs_root;
2137
2138 ent = debugfs_create_file("amdgpu_regs", S_IFREG | S_IRUGO, root,
2139 adev, &amdgpu_debugfs_regs_fops);
2140 if (IS_ERR(ent))
2141 return PTR_ERR(ent);
2142 i_size_write(ent->d_inode, adev->rmmio_size);
2143 adev->debugfs_regs = ent;
2144
2145 return 0;
2146}
2147
2148static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev)
2149{
2150 debugfs_remove(adev->debugfs_regs);
2151 adev->debugfs_regs = NULL;
2152}
2153
2154int amdgpu_debugfs_init(struct drm_minor *minor)
2155{
2156 return 0;
2157}
2158
2159void amdgpu_debugfs_cleanup(struct drm_minor *minor)
2160{
2161}
Alexander Kuleshov7cebc722015-06-27 13:16:05 +06002162#else
2163static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
2164{
2165 return 0;
2166}
2167static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev) { }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002168#endif