blob: c281cfd65337bd0ce81249de41f72ce5bde64860 [file] [log] [blame]
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001/*
2 * Copyright 2008 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 * Copyright 2009 Jerome Glisse.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors: Dave Airlie
25 * Alex Deucher
26 * Jerome Glisse
27 */
Chunming Zhou0875dc92016-06-12 15:41:58 +080028#include <linux/kthread.h>
Alex Deucherd38ceaf2015-04-20 16:55:21 -040029#include <linux/console.h>
30#include <linux/slab.h>
31#include <linux/debugfs.h>
32#include <drm/drmP.h>
33#include <drm/drm_crtc_helper.h>
34#include <drm/amdgpu_drm.h>
35#include <linux/vgaarb.h>
36#include <linux/vga_switcheroo.h>
37#include <linux/efi.h>
38#include "amdgpu.h"
Tom St Denisf4b373f2016-05-31 08:02:27 -040039#include "amdgpu_trace.h"
Alex Deucherd38ceaf2015-04-20 16:55:21 -040040#include "amdgpu_i2c.h"
41#include "atom.h"
42#include "amdgpu_atombios.h"
Alex Deucherd0dd7f02015-11-11 19:45:06 -050043#include "amd_pcie.h"
Alex Deuchera2e73f52015-04-20 17:09:27 -040044#ifdef CONFIG_DRM_AMDGPU_CIK
45#include "cik.h"
46#endif
Alex Deucheraaa36a92015-04-20 17:31:14 -040047#include "vi.h"
Alex Deucherd38ceaf2015-04-20 16:55:21 -040048#include "bif/bif_4_1_d.h"
49
50static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev);
51static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev);
52
53static const char *amdgpu_asic_name[] = {
54 "BONAIRE",
55 "KAVERI",
56 "KABINI",
57 "HAWAII",
58 "MULLINS",
59 "TOPAZ",
60 "TONGA",
David Zhang48299f92015-07-08 01:05:16 +080061 "FIJI",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040062 "CARRIZO",
Samuel Li139f4912015-10-08 14:50:27 -040063 "STONEY",
Flora Cui2cc0c0b2016-03-14 18:33:29 -040064 "POLARIS10",
65 "POLARIS11",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040066 "LAST",
67};
68
69bool amdgpu_device_is_px(struct drm_device *dev)
70{
71 struct amdgpu_device *adev = dev->dev_private;
72
Jammy Zhou2f7d10b2015-07-22 11:29:01 +080073 if (adev->flags & AMD_IS_PX)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040074 return true;
75 return false;
76}
77
78/*
79 * MMIO register access helper functions.
80 */
81uint32_t amdgpu_mm_rreg(struct amdgpu_device *adev, uint32_t reg,
82 bool always_indirect)
83{
Tom St Denisf4b373f2016-05-31 08:02:27 -040084 uint32_t ret;
85
Alex Deucherd38ceaf2015-04-20 16:55:21 -040086 if ((reg * 4) < adev->rmmio_size && !always_indirect)
Tom St Denisf4b373f2016-05-31 08:02:27 -040087 ret = readl(((void __iomem *)adev->rmmio) + (reg * 4));
Alex Deucherd38ceaf2015-04-20 16:55:21 -040088 else {
89 unsigned long flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040090
91 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
92 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
93 ret = readl(((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
94 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040095 }
Tom St Denisf4b373f2016-05-31 08:02:27 -040096 trace_amdgpu_mm_rreg(adev->pdev->device, reg, ret);
97 return ret;
Alex Deucherd38ceaf2015-04-20 16:55:21 -040098}
99
100void amdgpu_mm_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v,
101 bool always_indirect)
102{
Tom St Denisf4b373f2016-05-31 08:02:27 -0400103 trace_amdgpu_mm_wreg(adev->pdev->device, reg, v);
104
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400105 if ((reg * 4) < adev->rmmio_size && !always_indirect)
106 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
107 else {
108 unsigned long flags;
109
110 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
111 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
112 writel(v, ((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
113 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
114 }
115}
116
117u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 reg)
118{
119 if ((reg * 4) < adev->rio_mem_size)
120 return ioread32(adev->rio_mem + (reg * 4));
121 else {
122 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
123 return ioread32(adev->rio_mem + (mmMM_DATA * 4));
124 }
125}
126
127void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
128{
129
130 if ((reg * 4) < adev->rio_mem_size)
131 iowrite32(v, adev->rio_mem + (reg * 4));
132 else {
133 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
134 iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
135 }
136}
137
138/**
139 * amdgpu_mm_rdoorbell - read a doorbell dword
140 *
141 * @adev: amdgpu_device pointer
142 * @index: doorbell index
143 *
144 * Returns the value in the doorbell aperture at the
145 * requested doorbell index (CIK).
146 */
147u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
148{
149 if (index < adev->doorbell.num_doorbells) {
150 return readl(adev->doorbell.ptr + index);
151 } else {
152 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
153 return 0;
154 }
155}
156
157/**
158 * amdgpu_mm_wdoorbell - write a doorbell dword
159 *
160 * @adev: amdgpu_device pointer
161 * @index: doorbell index
162 * @v: value to write
163 *
164 * Writes @v to the doorbell aperture at the
165 * requested doorbell index (CIK).
166 */
167void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
168{
169 if (index < adev->doorbell.num_doorbells) {
170 writel(v, adev->doorbell.ptr + index);
171 } else {
172 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
173 }
174}
175
176/**
177 * amdgpu_invalid_rreg - dummy reg read function
178 *
179 * @adev: amdgpu device pointer
180 * @reg: offset of register
181 *
182 * Dummy register read function. Used for register blocks
183 * that certain asics don't have (all asics).
184 * Returns the value in the register.
185 */
186static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
187{
188 DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
189 BUG();
190 return 0;
191}
192
193/**
194 * amdgpu_invalid_wreg - dummy reg write function
195 *
196 * @adev: amdgpu device pointer
197 * @reg: offset of register
198 * @v: value to write to the register
199 *
200 * Dummy register read function. Used for register blocks
201 * that certain asics don't have (all asics).
202 */
203static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
204{
205 DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
206 reg, v);
207 BUG();
208}
209
210/**
211 * amdgpu_block_invalid_rreg - dummy reg read function
212 *
213 * @adev: amdgpu device pointer
214 * @block: offset of instance
215 * @reg: offset of register
216 *
217 * Dummy register read function. Used for register blocks
218 * that certain asics don't have (all asics).
219 * Returns the value in the register.
220 */
221static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
222 uint32_t block, uint32_t reg)
223{
224 DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
225 reg, block);
226 BUG();
227 return 0;
228}
229
230/**
231 * amdgpu_block_invalid_wreg - dummy reg write function
232 *
233 * @adev: amdgpu device pointer
234 * @block: offset of instance
235 * @reg: offset of register
236 * @v: value to write to the register
237 *
238 * Dummy register read function. Used for register blocks
239 * that certain asics don't have (all asics).
240 */
241static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
242 uint32_t block,
243 uint32_t reg, uint32_t v)
244{
245 DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
246 reg, block, v);
247 BUG();
248}
249
250static int amdgpu_vram_scratch_init(struct amdgpu_device *adev)
251{
252 int r;
253
254 if (adev->vram_scratch.robj == NULL) {
255 r = amdgpu_bo_create(adev, AMDGPU_GPU_PAGE_SIZE,
Alex Deucher857d9132015-08-27 00:14:16 -0400256 PAGE_SIZE, true, AMDGPU_GEM_DOMAIN_VRAM,
257 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED,
Christian König72d76682015-09-03 17:34:59 +0200258 NULL, NULL, &adev->vram_scratch.robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400259 if (r) {
260 return r;
261 }
262 }
263
264 r = amdgpu_bo_reserve(adev->vram_scratch.robj, false);
265 if (unlikely(r != 0))
266 return r;
267 r = amdgpu_bo_pin(adev->vram_scratch.robj,
268 AMDGPU_GEM_DOMAIN_VRAM, &adev->vram_scratch.gpu_addr);
269 if (r) {
270 amdgpu_bo_unreserve(adev->vram_scratch.robj);
271 return r;
272 }
273 r = amdgpu_bo_kmap(adev->vram_scratch.robj,
274 (void **)&adev->vram_scratch.ptr);
275 if (r)
276 amdgpu_bo_unpin(adev->vram_scratch.robj);
277 amdgpu_bo_unreserve(adev->vram_scratch.robj);
278
279 return r;
280}
281
282static void amdgpu_vram_scratch_fini(struct amdgpu_device *adev)
283{
284 int r;
285
286 if (adev->vram_scratch.robj == NULL) {
287 return;
288 }
289 r = amdgpu_bo_reserve(adev->vram_scratch.robj, false);
290 if (likely(r == 0)) {
291 amdgpu_bo_kunmap(adev->vram_scratch.robj);
292 amdgpu_bo_unpin(adev->vram_scratch.robj);
293 amdgpu_bo_unreserve(adev->vram_scratch.robj);
294 }
295 amdgpu_bo_unref(&adev->vram_scratch.robj);
296}
297
298/**
299 * amdgpu_program_register_sequence - program an array of registers.
300 *
301 * @adev: amdgpu_device pointer
302 * @registers: pointer to the register array
303 * @array_size: size of the register array
304 *
305 * Programs an array or registers with and and or masks.
306 * This is a helper for setting golden registers.
307 */
308void amdgpu_program_register_sequence(struct amdgpu_device *adev,
309 const u32 *registers,
310 const u32 array_size)
311{
312 u32 tmp, reg, and_mask, or_mask;
313 int i;
314
315 if (array_size % 3)
316 return;
317
318 for (i = 0; i < array_size; i +=3) {
319 reg = registers[i + 0];
320 and_mask = registers[i + 1];
321 or_mask = registers[i + 2];
322
323 if (and_mask == 0xffffffff) {
324 tmp = or_mask;
325 } else {
326 tmp = RREG32(reg);
327 tmp &= ~and_mask;
328 tmp |= or_mask;
329 }
330 WREG32(reg, tmp);
331 }
332}
333
334void amdgpu_pci_config_reset(struct amdgpu_device *adev)
335{
336 pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
337}
338
339/*
340 * GPU doorbell aperture helpers function.
341 */
342/**
343 * amdgpu_doorbell_init - Init doorbell driver information.
344 *
345 * @adev: amdgpu_device pointer
346 *
347 * Init doorbell driver information (CIK)
348 * Returns 0 on success, error on failure.
349 */
350static int amdgpu_doorbell_init(struct amdgpu_device *adev)
351{
352 /* doorbell bar mapping */
353 adev->doorbell.base = pci_resource_start(adev->pdev, 2);
354 adev->doorbell.size = pci_resource_len(adev->pdev, 2);
355
Christian Königedf600d2016-05-03 15:54:54 +0200356 adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400357 AMDGPU_DOORBELL_MAX_ASSIGNMENT+1);
358 if (adev->doorbell.num_doorbells == 0)
359 return -EINVAL;
360
361 adev->doorbell.ptr = ioremap(adev->doorbell.base, adev->doorbell.num_doorbells * sizeof(u32));
362 if (adev->doorbell.ptr == NULL) {
363 return -ENOMEM;
364 }
365 DRM_INFO("doorbell mmio base: 0x%08X\n", (uint32_t)adev->doorbell.base);
366 DRM_INFO("doorbell mmio size: %u\n", (unsigned)adev->doorbell.size);
367
368 return 0;
369}
370
371/**
372 * amdgpu_doorbell_fini - Tear down doorbell driver information.
373 *
374 * @adev: amdgpu_device pointer
375 *
376 * Tear down doorbell driver information (CIK)
377 */
378static void amdgpu_doorbell_fini(struct amdgpu_device *adev)
379{
380 iounmap(adev->doorbell.ptr);
381 adev->doorbell.ptr = NULL;
382}
383
384/**
385 * amdgpu_doorbell_get_kfd_info - Report doorbell configuration required to
386 * setup amdkfd
387 *
388 * @adev: amdgpu_device pointer
389 * @aperture_base: output returning doorbell aperture base physical address
390 * @aperture_size: output returning doorbell aperture size in bytes
391 * @start_offset: output returning # of doorbell bytes reserved for amdgpu.
392 *
393 * amdgpu and amdkfd share the doorbell aperture. amdgpu sets it up,
394 * takes doorbells required for its own rings and reports the setup to amdkfd.
395 * amdgpu reserved doorbells are at the start of the doorbell aperture.
396 */
397void amdgpu_doorbell_get_kfd_info(struct amdgpu_device *adev,
398 phys_addr_t *aperture_base,
399 size_t *aperture_size,
400 size_t *start_offset)
401{
402 /*
403 * The first num_doorbells are used by amdgpu.
404 * amdkfd takes whatever's left in the aperture.
405 */
406 if (adev->doorbell.size > adev->doorbell.num_doorbells * sizeof(u32)) {
407 *aperture_base = adev->doorbell.base;
408 *aperture_size = adev->doorbell.size;
409 *start_offset = adev->doorbell.num_doorbells * sizeof(u32);
410 } else {
411 *aperture_base = 0;
412 *aperture_size = 0;
413 *start_offset = 0;
414 }
415}
416
417/*
418 * amdgpu_wb_*()
419 * Writeback is the the method by which the the GPU updates special pages
420 * in memory with the status of certain GPU events (fences, ring pointers,
421 * etc.).
422 */
423
424/**
425 * amdgpu_wb_fini - Disable Writeback and free memory
426 *
427 * @adev: amdgpu_device pointer
428 *
429 * Disables Writeback and frees the Writeback memory (all asics).
430 * Used at driver shutdown.
431 */
432static void amdgpu_wb_fini(struct amdgpu_device *adev)
433{
434 if (adev->wb.wb_obj) {
435 if (!amdgpu_bo_reserve(adev->wb.wb_obj, false)) {
436 amdgpu_bo_kunmap(adev->wb.wb_obj);
437 amdgpu_bo_unpin(adev->wb.wb_obj);
438 amdgpu_bo_unreserve(adev->wb.wb_obj);
439 }
440 amdgpu_bo_unref(&adev->wb.wb_obj);
441 adev->wb.wb = NULL;
442 adev->wb.wb_obj = NULL;
443 }
444}
445
446/**
447 * amdgpu_wb_init- Init Writeback driver info and allocate memory
448 *
449 * @adev: amdgpu_device pointer
450 *
451 * Disables Writeback and frees the Writeback memory (all asics).
452 * Used at driver startup.
453 * Returns 0 on success or an -error on failure.
454 */
455static int amdgpu_wb_init(struct amdgpu_device *adev)
456{
457 int r;
458
459 if (adev->wb.wb_obj == NULL) {
460 r = amdgpu_bo_create(adev, AMDGPU_MAX_WB * 4, PAGE_SIZE, true,
Christian König72d76682015-09-03 17:34:59 +0200461 AMDGPU_GEM_DOMAIN_GTT, 0, NULL, NULL,
462 &adev->wb.wb_obj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400463 if (r) {
464 dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
465 return r;
466 }
467 r = amdgpu_bo_reserve(adev->wb.wb_obj, false);
468 if (unlikely(r != 0)) {
469 amdgpu_wb_fini(adev);
470 return r;
471 }
472 r = amdgpu_bo_pin(adev->wb.wb_obj, AMDGPU_GEM_DOMAIN_GTT,
473 &adev->wb.gpu_addr);
474 if (r) {
475 amdgpu_bo_unreserve(adev->wb.wb_obj);
476 dev_warn(adev->dev, "(%d) pin WB bo failed\n", r);
477 amdgpu_wb_fini(adev);
478 return r;
479 }
480 r = amdgpu_bo_kmap(adev->wb.wb_obj, (void **)&adev->wb.wb);
481 amdgpu_bo_unreserve(adev->wb.wb_obj);
482 if (r) {
483 dev_warn(adev->dev, "(%d) map WB bo failed\n", r);
484 amdgpu_wb_fini(adev);
485 return r;
486 }
487
488 adev->wb.num_wb = AMDGPU_MAX_WB;
489 memset(&adev->wb.used, 0, sizeof(adev->wb.used));
490
491 /* clear wb memory */
492 memset((char *)adev->wb.wb, 0, AMDGPU_GPU_PAGE_SIZE);
493 }
494
495 return 0;
496}
497
498/**
499 * amdgpu_wb_get - Allocate a wb entry
500 *
501 * @adev: amdgpu_device pointer
502 * @wb: wb index
503 *
504 * Allocate a wb slot for use by the driver (all asics).
505 * Returns 0 on success or -EINVAL on failure.
506 */
507int amdgpu_wb_get(struct amdgpu_device *adev, u32 *wb)
508{
509 unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
510 if (offset < adev->wb.num_wb) {
511 __set_bit(offset, adev->wb.used);
512 *wb = offset;
513 return 0;
514 } else {
515 return -EINVAL;
516 }
517}
518
519/**
520 * amdgpu_wb_free - Free a wb entry
521 *
522 * @adev: amdgpu_device pointer
523 * @wb: wb index
524 *
525 * Free a wb slot allocated for use by the driver (all asics)
526 */
527void amdgpu_wb_free(struct amdgpu_device *adev, u32 wb)
528{
529 if (wb < adev->wb.num_wb)
530 __clear_bit(wb, adev->wb.used);
531}
532
533/**
534 * amdgpu_vram_location - try to find VRAM location
535 * @adev: amdgpu device structure holding all necessary informations
536 * @mc: memory controller structure holding memory informations
537 * @base: base address at which to put VRAM
538 *
539 * Function will place try to place VRAM at base address provided
540 * as parameter (which is so far either PCI aperture address or
541 * for IGP TOM base address).
542 *
543 * If there is not enough space to fit the unvisible VRAM in the 32bits
544 * address space then we limit the VRAM size to the aperture.
545 *
546 * Note: We don't explicitly enforce VRAM start to be aligned on VRAM size,
547 * this shouldn't be a problem as we are using the PCI aperture as a reference.
548 * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but
549 * not IGP.
550 *
551 * Note: we use mc_vram_size as on some board we need to program the mc to
552 * cover the whole aperture even if VRAM size is inferior to aperture size
553 * Novell bug 204882 + along with lots of ubuntu ones
554 *
555 * Note: when limiting vram it's safe to overwritte real_vram_size because
556 * we are not in case where real_vram_size is inferior to mc_vram_size (ie
557 * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu
558 * ones)
559 *
560 * Note: IGP TOM addr should be the same as the aperture addr, we don't
561 * explicitly check for that thought.
562 *
563 * FIXME: when reducing VRAM size align new size on power of 2.
564 */
565void amdgpu_vram_location(struct amdgpu_device *adev, struct amdgpu_mc *mc, u64 base)
566{
567 uint64_t limit = (uint64_t)amdgpu_vram_limit << 20;
568
569 mc->vram_start = base;
570 if (mc->mc_vram_size > (adev->mc.mc_mask - base + 1)) {
571 dev_warn(adev->dev, "limiting VRAM to PCI aperture size\n");
572 mc->real_vram_size = mc->aper_size;
573 mc->mc_vram_size = mc->aper_size;
574 }
575 mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
576 if (limit && limit < mc->real_vram_size)
577 mc->real_vram_size = limit;
578 dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
579 mc->mc_vram_size >> 20, mc->vram_start,
580 mc->vram_end, mc->real_vram_size >> 20);
581}
582
583/**
584 * amdgpu_gtt_location - try to find GTT location
585 * @adev: amdgpu device structure holding all necessary informations
586 * @mc: memory controller structure holding memory informations
587 *
588 * Function will place try to place GTT before or after VRAM.
589 *
590 * If GTT size is bigger than space left then we ajust GTT size.
591 * Thus function will never fails.
592 *
593 * FIXME: when reducing GTT size align new size on power of 2.
594 */
595void amdgpu_gtt_location(struct amdgpu_device *adev, struct amdgpu_mc *mc)
596{
597 u64 size_af, size_bf;
598
599 size_af = ((adev->mc.mc_mask - mc->vram_end) + mc->gtt_base_align) & ~mc->gtt_base_align;
600 size_bf = mc->vram_start & ~mc->gtt_base_align;
601 if (size_bf > size_af) {
602 if (mc->gtt_size > size_bf) {
603 dev_warn(adev->dev, "limiting GTT\n");
604 mc->gtt_size = size_bf;
605 }
606 mc->gtt_start = (mc->vram_start & ~mc->gtt_base_align) - mc->gtt_size;
607 } else {
608 if (mc->gtt_size > size_af) {
609 dev_warn(adev->dev, "limiting GTT\n");
610 mc->gtt_size = size_af;
611 }
612 mc->gtt_start = (mc->vram_end + 1 + mc->gtt_base_align) & ~mc->gtt_base_align;
613 }
614 mc->gtt_end = mc->gtt_start + mc->gtt_size - 1;
615 dev_info(adev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
616 mc->gtt_size >> 20, mc->gtt_start, mc->gtt_end);
617}
618
619/*
620 * GPU helpers function.
621 */
622/**
623 * amdgpu_card_posted - check if the hw has already been initialized
624 *
625 * @adev: amdgpu_device pointer
626 *
627 * Check if the asic has been initialized (all asics).
628 * Used at driver startup.
629 * Returns true if initialized or false if not.
630 */
631bool amdgpu_card_posted(struct amdgpu_device *adev)
632{
633 uint32_t reg;
634
635 /* then check MEM_SIZE, in case the crtcs are off */
636 reg = RREG32(mmCONFIG_MEMSIZE);
637
638 if (reg)
639 return true;
640
641 return false;
642
643}
644
645/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400646 * amdgpu_dummy_page_init - init dummy page used by the driver
647 *
648 * @adev: amdgpu_device pointer
649 *
650 * Allocate the dummy page used by the driver (all asics).
651 * This dummy page is used by the driver as a filler for gart entries
652 * when pages are taken out of the GART
653 * Returns 0 on sucess, -ENOMEM on failure.
654 */
655int amdgpu_dummy_page_init(struct amdgpu_device *adev)
656{
657 if (adev->dummy_page.page)
658 return 0;
659 adev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
660 if (adev->dummy_page.page == NULL)
661 return -ENOMEM;
662 adev->dummy_page.addr = pci_map_page(adev->pdev, adev->dummy_page.page,
663 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
664 if (pci_dma_mapping_error(adev->pdev, adev->dummy_page.addr)) {
665 dev_err(&adev->pdev->dev, "Failed to DMA MAP the dummy page\n");
666 __free_page(adev->dummy_page.page);
667 adev->dummy_page.page = NULL;
668 return -ENOMEM;
669 }
670 return 0;
671}
672
673/**
674 * amdgpu_dummy_page_fini - free dummy page used by the driver
675 *
676 * @adev: amdgpu_device pointer
677 *
678 * Frees the dummy page used by the driver (all asics).
679 */
680void amdgpu_dummy_page_fini(struct amdgpu_device *adev)
681{
682 if (adev->dummy_page.page == NULL)
683 return;
684 pci_unmap_page(adev->pdev, adev->dummy_page.addr,
685 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
686 __free_page(adev->dummy_page.page);
687 adev->dummy_page.page = NULL;
688}
689
690
691/* ATOM accessor methods */
692/*
693 * ATOM is an interpreted byte code stored in tables in the vbios. The
694 * driver registers callbacks to access registers and the interpreter
695 * in the driver parses the tables and executes then to program specific
696 * actions (set display modes, asic init, etc.). See amdgpu_atombios.c,
697 * atombios.h, and atom.c
698 */
699
700/**
701 * cail_pll_read - read PLL register
702 *
703 * @info: atom card_info pointer
704 * @reg: PLL register offset
705 *
706 * Provides a PLL register accessor for the atom interpreter (r4xx+).
707 * Returns the value of the PLL register.
708 */
709static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
710{
711 return 0;
712}
713
714/**
715 * cail_pll_write - write PLL register
716 *
717 * @info: atom card_info pointer
718 * @reg: PLL register offset
719 * @val: value to write to the pll register
720 *
721 * Provides a PLL register accessor for the atom interpreter (r4xx+).
722 */
723static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
724{
725
726}
727
728/**
729 * cail_mc_read - read MC (Memory Controller) register
730 *
731 * @info: atom card_info pointer
732 * @reg: MC register offset
733 *
734 * Provides an MC register accessor for the atom interpreter (r4xx+).
735 * Returns the value of the MC register.
736 */
737static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
738{
739 return 0;
740}
741
742/**
743 * cail_mc_write - write MC (Memory Controller) register
744 *
745 * @info: atom card_info pointer
746 * @reg: MC register offset
747 * @val: value to write to the pll register
748 *
749 * Provides a MC register accessor for the atom interpreter (r4xx+).
750 */
751static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
752{
753
754}
755
756/**
757 * cail_reg_write - write MMIO register
758 *
759 * @info: atom card_info pointer
760 * @reg: MMIO register offset
761 * @val: value to write to the pll register
762 *
763 * Provides a MMIO register accessor for the atom interpreter (r4xx+).
764 */
765static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
766{
767 struct amdgpu_device *adev = info->dev->dev_private;
768
769 WREG32(reg, val);
770}
771
772/**
773 * cail_reg_read - read MMIO register
774 *
775 * @info: atom card_info pointer
776 * @reg: MMIO register offset
777 *
778 * Provides an MMIO register accessor for the atom interpreter (r4xx+).
779 * Returns the value of the MMIO register.
780 */
781static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
782{
783 struct amdgpu_device *adev = info->dev->dev_private;
784 uint32_t r;
785
786 r = RREG32(reg);
787 return r;
788}
789
790/**
791 * cail_ioreg_write - write IO register
792 *
793 * @info: atom card_info pointer
794 * @reg: IO register offset
795 * @val: value to write to the pll register
796 *
797 * Provides a IO register accessor for the atom interpreter (r4xx+).
798 */
799static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
800{
801 struct amdgpu_device *adev = info->dev->dev_private;
802
803 WREG32_IO(reg, val);
804}
805
806/**
807 * cail_ioreg_read - read IO register
808 *
809 * @info: atom card_info pointer
810 * @reg: IO register offset
811 *
812 * Provides an IO register accessor for the atom interpreter (r4xx+).
813 * Returns the value of the IO register.
814 */
815static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
816{
817 struct amdgpu_device *adev = info->dev->dev_private;
818 uint32_t r;
819
820 r = RREG32_IO(reg);
821 return r;
822}
823
824/**
825 * amdgpu_atombios_fini - free the driver info and callbacks for atombios
826 *
827 * @adev: amdgpu_device pointer
828 *
829 * Frees the driver info and register access callbacks for the ATOM
830 * interpreter (r4xx+).
831 * Called at driver shutdown.
832 */
833static void amdgpu_atombios_fini(struct amdgpu_device *adev)
834{
Monk Liu89e0ec9f2016-05-27 19:34:11 +0800835 if (adev->mode_info.atom_context) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400836 kfree(adev->mode_info.atom_context->scratch);
Monk Liu89e0ec9f2016-05-27 19:34:11 +0800837 kfree(adev->mode_info.atom_context->iio);
838 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400839 kfree(adev->mode_info.atom_context);
840 adev->mode_info.atom_context = NULL;
841 kfree(adev->mode_info.atom_card_info);
842 adev->mode_info.atom_card_info = NULL;
843}
844
845/**
846 * amdgpu_atombios_init - init the driver info and callbacks for atombios
847 *
848 * @adev: amdgpu_device pointer
849 *
850 * Initializes the driver info and register access callbacks for the
851 * ATOM interpreter (r4xx+).
852 * Returns 0 on sucess, -ENOMEM on failure.
853 * Called at driver startup.
854 */
855static int amdgpu_atombios_init(struct amdgpu_device *adev)
856{
857 struct card_info *atom_card_info =
858 kzalloc(sizeof(struct card_info), GFP_KERNEL);
859
860 if (!atom_card_info)
861 return -ENOMEM;
862
863 adev->mode_info.atom_card_info = atom_card_info;
864 atom_card_info->dev = adev->ddev;
865 atom_card_info->reg_read = cail_reg_read;
866 atom_card_info->reg_write = cail_reg_write;
867 /* needed for iio ops */
868 if (adev->rio_mem) {
869 atom_card_info->ioreg_read = cail_ioreg_read;
870 atom_card_info->ioreg_write = cail_ioreg_write;
871 } else {
872 DRM_ERROR("Unable to find PCI I/O BAR; using MMIO for ATOM IIO\n");
873 atom_card_info->ioreg_read = cail_reg_read;
874 atom_card_info->ioreg_write = cail_reg_write;
875 }
876 atom_card_info->mc_read = cail_mc_read;
877 atom_card_info->mc_write = cail_mc_write;
878 atom_card_info->pll_read = cail_pll_read;
879 atom_card_info->pll_write = cail_pll_write;
880
881 adev->mode_info.atom_context = amdgpu_atom_parse(atom_card_info, adev->bios);
882 if (!adev->mode_info.atom_context) {
883 amdgpu_atombios_fini(adev);
884 return -ENOMEM;
885 }
886
887 mutex_init(&adev->mode_info.atom_context->mutex);
888 amdgpu_atombios_scratch_regs_init(adev);
889 amdgpu_atom_allocate_fb_scratch(adev->mode_info.atom_context);
890 return 0;
891}
892
893/* if we get transitioned to only one device, take VGA back */
894/**
895 * amdgpu_vga_set_decode - enable/disable vga decode
896 *
897 * @cookie: amdgpu_device pointer
898 * @state: enable/disable vga decode
899 *
900 * Enable/disable vga decode (all asics).
901 * Returns VGA resource flags.
902 */
903static unsigned int amdgpu_vga_set_decode(void *cookie, bool state)
904{
905 struct amdgpu_device *adev = cookie;
906 amdgpu_asic_set_vga_state(adev, state);
907 if (state)
908 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
909 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
910 else
911 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
912}
913
914/**
915 * amdgpu_check_pot_argument - check that argument is a power of two
916 *
917 * @arg: value to check
918 *
919 * Validates that a certain argument is a power of two (all asics).
920 * Returns true if argument is valid.
921 */
922static bool amdgpu_check_pot_argument(int arg)
923{
924 return (arg & (arg - 1)) == 0;
925}
926
927/**
928 * amdgpu_check_arguments - validate module params
929 *
930 * @adev: amdgpu_device pointer
931 *
932 * Validates certain module parameters and updates
933 * the associated values used by the driver (all asics).
934 */
935static void amdgpu_check_arguments(struct amdgpu_device *adev)
936{
Chunming Zhou5b011232015-12-10 17:34:33 +0800937 if (amdgpu_sched_jobs < 4) {
938 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
939 amdgpu_sched_jobs);
940 amdgpu_sched_jobs = 4;
941 } else if (!amdgpu_check_pot_argument(amdgpu_sched_jobs)){
942 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
943 amdgpu_sched_jobs);
944 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
945 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400946
947 if (amdgpu_gart_size != -1) {
Christian Königc4e1a132016-03-17 16:25:15 +0100948 /* gtt size must be greater or equal to 32M */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400949 if (amdgpu_gart_size < 32) {
950 dev_warn(adev->dev, "gart size (%d) too small\n",
951 amdgpu_gart_size);
952 amdgpu_gart_size = -1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400953 }
954 }
955
956 if (!amdgpu_check_pot_argument(amdgpu_vm_size)) {
957 dev_warn(adev->dev, "VM size (%d) must be a power of 2\n",
958 amdgpu_vm_size);
Alex Deucher8dacc122015-05-11 16:20:58 -0400959 amdgpu_vm_size = 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400960 }
961
962 if (amdgpu_vm_size < 1) {
963 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
964 amdgpu_vm_size);
Alex Deucher8dacc122015-05-11 16:20:58 -0400965 amdgpu_vm_size = 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400966 }
967
968 /*
969 * Max GPUVM size for Cayman, SI and CI are 40 bits.
970 */
971 if (amdgpu_vm_size > 1024) {
972 dev_warn(adev->dev, "VM size (%d) too large, max is 1TB\n",
973 amdgpu_vm_size);
Alex Deucher8dacc122015-05-11 16:20:58 -0400974 amdgpu_vm_size = 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400975 }
976
977 /* defines number of bits in page table versus page directory,
978 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
979 * page table and the remaining bits are in the page directory */
980 if (amdgpu_vm_block_size == -1) {
981
982 /* Total bits covered by PD + PTs */
983 unsigned bits = ilog2(amdgpu_vm_size) + 18;
984
985 /* Make sure the PD is 4K in size up to 8GB address space.
986 Above that split equal between PD and PTs */
987 if (amdgpu_vm_size <= 8)
988 amdgpu_vm_block_size = bits - 9;
989 else
990 amdgpu_vm_block_size = (bits + 3) / 2;
991
992 } else if (amdgpu_vm_block_size < 9) {
993 dev_warn(adev->dev, "VM page table size (%d) too small\n",
994 amdgpu_vm_block_size);
995 amdgpu_vm_block_size = 9;
996 }
997
998 if (amdgpu_vm_block_size > 24 ||
999 (amdgpu_vm_size * 1024) < (1ull << amdgpu_vm_block_size)) {
1000 dev_warn(adev->dev, "VM page table size (%d) too large\n",
1001 amdgpu_vm_block_size);
1002 amdgpu_vm_block_size = 9;
1003 }
1004}
1005
1006/**
1007 * amdgpu_switcheroo_set_state - set switcheroo state
1008 *
1009 * @pdev: pci dev pointer
Lukas Wunner16944672015-09-05 11:17:35 +02001010 * @state: vga_switcheroo state
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001011 *
1012 * Callback for the switcheroo driver. Suspends or resumes the
1013 * the asics before or after it is powered up using ACPI methods.
1014 */
1015static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1016{
1017 struct drm_device *dev = pci_get_drvdata(pdev);
1018
1019 if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
1020 return;
1021
1022 if (state == VGA_SWITCHEROO_ON) {
1023 unsigned d3_delay = dev->pdev->d3_delay;
1024
1025 printk(KERN_INFO "amdgpu: switched on\n");
1026 /* don't suspend or resume card normally */
1027 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1028
1029 amdgpu_resume_kms(dev, true, true);
1030
1031 dev->pdev->d3_delay = d3_delay;
1032
1033 dev->switch_power_state = DRM_SWITCH_POWER_ON;
1034 drm_kms_helper_poll_enable(dev);
1035 } else {
1036 printk(KERN_INFO "amdgpu: switched off\n");
1037 drm_kms_helper_poll_disable(dev);
1038 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1039 amdgpu_suspend_kms(dev, true, true);
1040 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1041 }
1042}
1043
1044/**
1045 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1046 *
1047 * @pdev: pci dev pointer
1048 *
1049 * Callback for the switcheroo driver. Check of the switcheroo
1050 * state can be changed.
1051 * Returns true if the state can be changed, false if not.
1052 */
1053static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1054{
1055 struct drm_device *dev = pci_get_drvdata(pdev);
1056
1057 /*
1058 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1059 * locking inversion with the driver load path. And the access here is
1060 * completely racy anyway. So don't bother with locking for now.
1061 */
1062 return dev->open_count == 0;
1063}
1064
1065static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1066 .set_gpu_state = amdgpu_switcheroo_set_state,
1067 .reprobe = NULL,
1068 .can_switch = amdgpu_switcheroo_can_switch,
1069};
1070
1071int amdgpu_set_clockgating_state(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001072 enum amd_ip_block_type block_type,
1073 enum amd_clockgating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001074{
1075 int i, r = 0;
1076
1077 for (i = 0; i < adev->num_ip_blocks; i++) {
1078 if (adev->ip_blocks[i].type == block_type) {
yanyang15fc3aee2015-05-22 14:39:35 -04001079 r = adev->ip_blocks[i].funcs->set_clockgating_state((void *)adev,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001080 state);
1081 if (r)
1082 return r;
1083 }
1084 }
1085 return r;
1086}
1087
1088int amdgpu_set_powergating_state(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001089 enum amd_ip_block_type block_type,
1090 enum amd_powergating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001091{
1092 int i, r = 0;
1093
1094 for (i = 0; i < adev->num_ip_blocks; i++) {
1095 if (adev->ip_blocks[i].type == block_type) {
yanyang15fc3aee2015-05-22 14:39:35 -04001096 r = adev->ip_blocks[i].funcs->set_powergating_state((void *)adev,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001097 state);
1098 if (r)
1099 return r;
1100 }
1101 }
1102 return r;
1103}
1104
Alex Deucher5dbbb602016-06-23 11:41:04 -04001105int amdgpu_wait_for_idle(struct amdgpu_device *adev,
1106 enum amd_ip_block_type block_type)
1107{
1108 int i, r;
1109
1110 for (i = 0; i < adev->num_ip_blocks; i++) {
1111 if (adev->ip_blocks[i].type == block_type) {
1112 r = adev->ip_blocks[i].funcs->wait_for_idle((void *)adev);
1113 if (r)
1114 return r;
1115 break;
1116 }
1117 }
1118 return 0;
1119
1120}
1121
1122bool amdgpu_is_idle(struct amdgpu_device *adev,
1123 enum amd_ip_block_type block_type)
1124{
1125 int i;
1126
1127 for (i = 0; i < adev->num_ip_blocks; i++) {
1128 if (adev->ip_blocks[i].type == block_type)
1129 return adev->ip_blocks[i].funcs->is_idle((void *)adev);
1130 }
1131 return true;
1132
1133}
1134
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001135const struct amdgpu_ip_block_version * amdgpu_get_ip_block(
1136 struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001137 enum amd_ip_block_type type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001138{
1139 int i;
1140
1141 for (i = 0; i < adev->num_ip_blocks; i++)
1142 if (adev->ip_blocks[i].type == type)
1143 return &adev->ip_blocks[i];
1144
1145 return NULL;
1146}
1147
1148/**
1149 * amdgpu_ip_block_version_cmp
1150 *
1151 * @adev: amdgpu_device pointer
yanyang15fc3aee2015-05-22 14:39:35 -04001152 * @type: enum amd_ip_block_type
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001153 * @major: major version
1154 * @minor: minor version
1155 *
1156 * return 0 if equal or greater
1157 * return 1 if smaller or the ip_block doesn't exist
1158 */
1159int amdgpu_ip_block_version_cmp(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001160 enum amd_ip_block_type type,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001161 u32 major, u32 minor)
1162{
1163 const struct amdgpu_ip_block_version *ip_block;
1164 ip_block = amdgpu_get_ip_block(adev, type);
1165
1166 if (ip_block && ((ip_block->major > major) ||
1167 ((ip_block->major == major) &&
1168 (ip_block->minor >= minor))))
1169 return 0;
1170
1171 return 1;
1172}
1173
1174static int amdgpu_early_init(struct amdgpu_device *adev)
1175{
Alex Deucheraaa36a92015-04-20 17:31:14 -04001176 int i, r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001177
1178 switch (adev->asic_type) {
Alex Deucheraaa36a92015-04-20 17:31:14 -04001179 case CHIP_TOPAZ:
1180 case CHIP_TONGA:
David Zhang48299f92015-07-08 01:05:16 +08001181 case CHIP_FIJI:
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001182 case CHIP_POLARIS11:
1183 case CHIP_POLARIS10:
Alex Deucheraaa36a92015-04-20 17:31:14 -04001184 case CHIP_CARRIZO:
Samuel Li39bb0c92015-10-08 16:31:43 -04001185 case CHIP_STONEY:
1186 if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
Alex Deucheraaa36a92015-04-20 17:31:14 -04001187 adev->family = AMDGPU_FAMILY_CZ;
1188 else
1189 adev->family = AMDGPU_FAMILY_VI;
1190
1191 r = vi_set_ip_blocks(adev);
1192 if (r)
1193 return r;
1194 break;
Alex Deuchera2e73f52015-04-20 17:09:27 -04001195#ifdef CONFIG_DRM_AMDGPU_CIK
1196 case CHIP_BONAIRE:
1197 case CHIP_HAWAII:
1198 case CHIP_KAVERI:
1199 case CHIP_KABINI:
1200 case CHIP_MULLINS:
1201 if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
1202 adev->family = AMDGPU_FAMILY_CI;
1203 else
1204 adev->family = AMDGPU_FAMILY_KV;
1205
1206 r = cik_set_ip_blocks(adev);
1207 if (r)
1208 return r;
1209 break;
1210#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001211 default:
1212 /* FIXME: not supported yet */
1213 return -EINVAL;
1214 }
1215
Alex Deucher8faf0e02015-07-28 11:50:31 -04001216 adev->ip_block_status = kcalloc(adev->num_ip_blocks,
1217 sizeof(struct amdgpu_ip_block_status), GFP_KERNEL);
1218 if (adev->ip_block_status == NULL)
Alex Deucherd8d090b2015-06-26 13:02:57 -04001219 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001220
1221 if (adev->ip_blocks == NULL) {
1222 DRM_ERROR("No IP blocks found!\n");
1223 return r;
1224 }
1225
1226 for (i = 0; i < adev->num_ip_blocks; i++) {
1227 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
1228 DRM_ERROR("disabled ip block: %d\n", i);
Alex Deucher8faf0e02015-07-28 11:50:31 -04001229 adev->ip_block_status[i].valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001230 } else {
1231 if (adev->ip_blocks[i].funcs->early_init) {
yanyang15fc3aee2015-05-22 14:39:35 -04001232 r = adev->ip_blocks[i].funcs->early_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001233 if (r == -ENOENT) {
Alex Deucher8faf0e02015-07-28 11:50:31 -04001234 adev->ip_block_status[i].valid = false;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001235 } else if (r) {
Tom St Denis88a907d2016-05-04 14:28:35 -04001236 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 -04001237 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001238 } else {
Alex Deucher8faf0e02015-07-28 11:50:31 -04001239 adev->ip_block_status[i].valid = true;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001240 }
Alex Deucher974e6b62015-07-10 13:59:44 -04001241 } else {
Alex Deucher8faf0e02015-07-28 11:50:31 -04001242 adev->ip_block_status[i].valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001243 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001244 }
1245 }
1246
Nicolai Hähnle395d1fb2016-06-02 12:32:07 +02001247 adev->cg_flags &= amdgpu_cg_mask;
1248 adev->pg_flags &= amdgpu_pg_mask;
1249
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001250 return 0;
1251}
1252
1253static int amdgpu_init(struct amdgpu_device *adev)
1254{
1255 int i, r;
1256
1257 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deucher8faf0e02015-07-28 11:50:31 -04001258 if (!adev->ip_block_status[i].valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001259 continue;
yanyang15fc3aee2015-05-22 14:39:35 -04001260 r = adev->ip_blocks[i].funcs->sw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001261 if (r) {
Tom St Denis822b2ce2016-05-05 10:23:40 -04001262 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 -04001263 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001264 }
Alex Deucher8faf0e02015-07-28 11:50:31 -04001265 adev->ip_block_status[i].sw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001266 /* need to do gmc hw init early so we can allocate gpu mem */
yanyang15fc3aee2015-05-22 14:39:35 -04001267 if (adev->ip_blocks[i].type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001268 r = amdgpu_vram_scratch_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001269 if (r) {
1270 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001271 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001272 }
yanyang15fc3aee2015-05-22 14:39:35 -04001273 r = adev->ip_blocks[i].funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001274 if (r) {
1275 DRM_ERROR("hw_init %d failed %d\n", i, 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 r = amdgpu_wb_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001279 if (r) {
1280 DRM_ERROR("amdgpu_wb_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001281 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001282 }
Alex Deucher8faf0e02015-07-28 11:50:31 -04001283 adev->ip_block_status[i].hw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001284 }
1285 }
1286
1287 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deucher8faf0e02015-07-28 11:50:31 -04001288 if (!adev->ip_block_status[i].sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001289 continue;
1290 /* gmc hw init is done early */
yanyang15fc3aee2015-05-22 14:39:35 -04001291 if (adev->ip_blocks[i].type == AMD_IP_BLOCK_TYPE_GMC)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001292 continue;
yanyang15fc3aee2015-05-22 14:39:35 -04001293 r = adev->ip_blocks[i].funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001294 if (r) {
Tom St Denis822b2ce2016-05-05 10:23:40 -04001295 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 -04001296 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001297 }
Alex Deucher8faf0e02015-07-28 11:50:31 -04001298 adev->ip_block_status[i].hw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001299 }
1300
1301 return 0;
1302}
1303
1304static int amdgpu_late_init(struct amdgpu_device *adev)
1305{
1306 int i = 0, r;
1307
1308 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deucher8faf0e02015-07-28 11:50:31 -04001309 if (!adev->ip_block_status[i].valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001310 continue;
1311 /* enable clockgating to save power */
yanyang15fc3aee2015-05-22 14:39:35 -04001312 r = adev->ip_blocks[i].funcs->set_clockgating_state((void *)adev,
1313 AMD_CG_STATE_GATE);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001314 if (r) {
Tom St Denis822b2ce2016-05-05 10:23:40 -04001315 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 -04001316 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001317 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001318 if (adev->ip_blocks[i].funcs->late_init) {
yanyang15fc3aee2015-05-22 14:39:35 -04001319 r = adev->ip_blocks[i].funcs->late_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001320 if (r) {
Tom St Denis822b2ce2016-05-05 10:23:40 -04001321 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 -04001322 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001323 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001324 }
1325 }
1326
1327 return 0;
1328}
1329
1330static int amdgpu_fini(struct amdgpu_device *adev)
1331{
1332 int i, r;
1333
1334 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deucher8faf0e02015-07-28 11:50:31 -04001335 if (!adev->ip_block_status[i].hw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001336 continue;
yanyang15fc3aee2015-05-22 14:39:35 -04001337 if (adev->ip_blocks[i].type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001338 amdgpu_wb_fini(adev);
1339 amdgpu_vram_scratch_fini(adev);
1340 }
1341 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
yanyang15fc3aee2015-05-22 14:39:35 -04001342 r = adev->ip_blocks[i].funcs->set_clockgating_state((void *)adev,
1343 AMD_CG_STATE_UNGATE);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001344 if (r) {
Tom St Denis822b2ce2016-05-05 10:23:40 -04001345 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 -04001346 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001347 }
yanyang15fc3aee2015-05-22 14:39:35 -04001348 r = adev->ip_blocks[i].funcs->hw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001349 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001350 if (r) {
Tom St Denis822b2ce2016-05-05 10:23:40 -04001351 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 -05001352 }
Alex Deucher8faf0e02015-07-28 11:50:31 -04001353 adev->ip_block_status[i].hw = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001354 }
1355
1356 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deucher8faf0e02015-07-28 11:50:31 -04001357 if (!adev->ip_block_status[i].sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001358 continue;
yanyang15fc3aee2015-05-22 14:39:35 -04001359 r = adev->ip_blocks[i].funcs->sw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001360 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001361 if (r) {
Tom St Denis822b2ce2016-05-05 10:23:40 -04001362 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 -05001363 }
Alex Deucher8faf0e02015-07-28 11:50:31 -04001364 adev->ip_block_status[i].sw = false;
1365 adev->ip_block_status[i].valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001366 }
1367
Monk Liua6dcfd92016-05-19 14:36:34 +08001368 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
1369 if (adev->ip_blocks[i].funcs->late_fini)
1370 adev->ip_blocks[i].funcs->late_fini((void *)adev);
1371 }
1372
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001373 return 0;
1374}
1375
1376static int amdgpu_suspend(struct amdgpu_device *adev)
1377{
1378 int i, r;
1379
Flora Cuic5a93a22016-02-26 10:45:25 +08001380 /* ungate SMC block first */
1381 r = amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC,
1382 AMD_CG_STATE_UNGATE);
1383 if (r) {
1384 DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n",r);
1385 }
1386
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001387 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deucher8faf0e02015-07-28 11:50:31 -04001388 if (!adev->ip_block_status[i].valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001389 continue;
1390 /* ungate blocks so that suspend can properly shut them down */
Flora Cuic5a93a22016-02-26 10:45:25 +08001391 if (i != AMD_IP_BLOCK_TYPE_SMC) {
1392 r = adev->ip_blocks[i].funcs->set_clockgating_state((void *)adev,
1393 AMD_CG_STATE_UNGATE);
1394 if (r) {
Tom St Denis822b2ce2016-05-05 10:23:40 -04001395 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 +08001396 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001397 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001398 /* XXX handle errors */
1399 r = adev->ip_blocks[i].funcs->suspend(adev);
1400 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001401 if (r) {
Tom St Denis822b2ce2016-05-05 10:23:40 -04001402 DRM_ERROR("suspend of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001403 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001404 }
1405
1406 return 0;
1407}
1408
1409static int amdgpu_resume(struct amdgpu_device *adev)
1410{
1411 int i, r;
1412
1413 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deucher8faf0e02015-07-28 11:50:31 -04001414 if (!adev->ip_block_status[i].valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001415 continue;
1416 r = adev->ip_blocks[i].funcs->resume(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001417 if (r) {
Tom St Denis822b2ce2016-05-05 10:23:40 -04001418 DRM_ERROR("resume of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001419 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001420 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001421 }
1422
1423 return 0;
1424}
1425
Andres Rodriguez048765a2016-06-11 02:51:32 -04001426static bool amdgpu_device_is_virtual(void)
1427{
1428#ifdef CONFIG_X86
1429 return boot_cpu_has(X86_FEATURE_HYPERVISOR);
1430#else
1431 return false;
1432#endif
1433}
1434
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001435/**
1436 * amdgpu_device_init - initialize the driver
1437 *
1438 * @adev: amdgpu_device pointer
1439 * @pdev: drm dev pointer
1440 * @pdev: pci dev pointer
1441 * @flags: driver flags
1442 *
1443 * Initializes the driver info and hw (all asics).
1444 * Returns 0 for success or an error on failure.
1445 * Called at driver startup.
1446 */
1447int amdgpu_device_init(struct amdgpu_device *adev,
1448 struct drm_device *ddev,
1449 struct pci_dev *pdev,
1450 uint32_t flags)
1451{
1452 int r, i;
1453 bool runtime = false;
1454
1455 adev->shutdown = false;
1456 adev->dev = &pdev->dev;
1457 adev->ddev = ddev;
1458 adev->pdev = pdev;
1459 adev->flags = flags;
Jammy Zhou2f7d10b2015-07-22 11:29:01 +08001460 adev->asic_type = flags & AMD_ASIC_MASK;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001461 adev->is_atom_bios = false;
1462 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
1463 adev->mc.gtt_size = 512 * 1024 * 1024;
1464 adev->accel_working = false;
1465 adev->num_rings = 0;
1466 adev->mman.buffer_funcs = NULL;
1467 adev->mman.buffer_funcs_ring = NULL;
1468 adev->vm_manager.vm_pte_funcs = NULL;
Christian König2d55e452016-02-08 17:37:38 +01001469 adev->vm_manager.vm_pte_num_rings = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001470 adev->gart.gart_funcs = NULL;
1471 adev->fence_context = fence_context_alloc(AMDGPU_MAX_RINGS);
1472
1473 adev->smc_rreg = &amdgpu_invalid_rreg;
1474 adev->smc_wreg = &amdgpu_invalid_wreg;
1475 adev->pcie_rreg = &amdgpu_invalid_rreg;
1476 adev->pcie_wreg = &amdgpu_invalid_wreg;
1477 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
1478 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
1479 adev->didt_rreg = &amdgpu_invalid_rreg;
1480 adev->didt_wreg = &amdgpu_invalid_wreg;
1481 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
1482 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
1483
Alex Deucher3e39ab92015-06-05 15:04:33 -04001484 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
1485 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
1486 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001487
1488 /* mutex initialization are all done here so we
1489 * can recall function without having locking issues */
Christian König8d0a7ce2015-11-03 20:58:50 +01001490 mutex_init(&adev->vm_manager.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001491 atomic_set(&adev->irq.ih.lock, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001492 mutex_init(&adev->pm.mutex);
1493 mutex_init(&adev->gfx.gpu_clock_mutex);
1494 mutex_init(&adev->srbm_mutex);
1495 mutex_init(&adev->grbm_idx_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001496 mutex_init(&adev->mn_lock);
1497 hash_init(adev->mn_hash);
1498
1499 amdgpu_check_arguments(adev);
1500
1501 /* Registers mapping */
1502 /* TODO: block userspace mapping of io register */
1503 spin_lock_init(&adev->mmio_idx_lock);
1504 spin_lock_init(&adev->smc_idx_lock);
1505 spin_lock_init(&adev->pcie_idx_lock);
1506 spin_lock_init(&adev->uvd_ctx_idx_lock);
1507 spin_lock_init(&adev->didt_idx_lock);
1508 spin_lock_init(&adev->audio_endpt_idx_lock);
1509
1510 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
1511 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
1512 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
1513 if (adev->rmmio == NULL) {
1514 return -ENOMEM;
1515 }
1516 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
1517 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
1518
1519 /* doorbell bar mapping */
1520 amdgpu_doorbell_init(adev);
1521
1522 /* io port mapping */
1523 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
1524 if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
1525 adev->rio_mem_size = pci_resource_len(adev->pdev, i);
1526 adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
1527 break;
1528 }
1529 }
1530 if (adev->rio_mem == NULL)
1531 DRM_ERROR("Unable to find PCI I/O BAR\n");
1532
1533 /* early init functions */
1534 r = amdgpu_early_init(adev);
1535 if (r)
1536 return r;
1537
1538 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
1539 /* this will fail for cards that aren't VGA class devices, just
1540 * ignore it */
1541 vga_client_register(adev->pdev, adev, NULL, amdgpu_vga_set_decode);
1542
1543 if (amdgpu_runtime_pm == 1)
1544 runtime = true;
Alex Deuchere9bef452016-04-25 13:12:18 -04001545 if (amdgpu_device_is_px(ddev))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001546 runtime = true;
1547 vga_switcheroo_register_client(adev->pdev, &amdgpu_switcheroo_ops, runtime);
1548 if (runtime)
1549 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
1550
1551 /* Read BIOS */
Alex Deucher83ba1262016-06-03 18:21:41 -04001552 if (!amdgpu_get_bios(adev)) {
1553 r = -EINVAL;
1554 goto failed;
1555 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001556 /* Must be an ATOMBIOS */
1557 if (!adev->is_atom_bios) {
1558 dev_err(adev->dev, "Expecting atombios for GPU\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001559 r = -EINVAL;
1560 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001561 }
1562 r = amdgpu_atombios_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001563 if (r) {
1564 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001565 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001566 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001567
Alex Deucher7e471e62016-02-01 11:13:04 -05001568 /* See if the asic supports SR-IOV */
1569 adev->virtualization.supports_sr_iov =
1570 amdgpu_atombios_has_gpu_virtualization_table(adev);
1571
Andres Rodriguez048765a2016-06-11 02:51:32 -04001572 /* Check if we are executing in a virtualized environment */
1573 adev->virtualization.is_virtual = amdgpu_device_is_virtual();
1574 adev->virtualization.caps = amdgpu_asic_get_virtual_caps(adev);
1575
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001576 /* Post card if necessary */
Andres Rodriguez048765a2016-06-11 02:51:32 -04001577 if (!amdgpu_card_posted(adev) ||
1578 (adev->virtualization.is_virtual &&
Dan Carpenter48a70e12016-06-18 11:38:44 +03001579 !(adev->virtualization.caps & AMDGPU_VIRT_CAPS_SRIOV_EN))) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001580 if (!adev->bios) {
1581 dev_err(adev->dev, "Card not posted and no BIOS - ignoring\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001582 r = -EINVAL;
1583 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001584 }
1585 DRM_INFO("GPU not posted. posting now...\n");
1586 amdgpu_atom_asic_init(adev->mode_info.atom_context);
1587 }
1588
1589 /* Initialize clocks */
1590 r = amdgpu_atombios_get_clock_info(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001591 if (r) {
1592 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001593 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001594 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001595 /* init i2c buses */
1596 amdgpu_atombios_i2c_init(adev);
1597
1598 /* Fence driver */
1599 r = amdgpu_fence_driver_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001600 if (r) {
1601 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001602 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001603 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001604
1605 /* init the mode config */
1606 drm_mode_config_init(adev->ddev);
1607
1608 r = amdgpu_init(adev);
1609 if (r) {
Alex Deucher2c1a2782015-12-07 17:02:53 -05001610 dev_err(adev->dev, "amdgpu_init failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001611 amdgpu_fini(adev);
Alex Deucher83ba1262016-06-03 18:21:41 -04001612 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001613 }
1614
1615 adev->accel_working = true;
1616
1617 amdgpu_fbdev_init(adev);
1618
1619 r = amdgpu_ib_pool_init(adev);
1620 if (r) {
1621 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
Alex Deucher83ba1262016-06-03 18:21:41 -04001622 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001623 }
1624
1625 r = amdgpu_ib_ring_tests(adev);
1626 if (r)
1627 DRM_ERROR("ib ring test failed (%d).\n", r);
1628
1629 r = amdgpu_gem_debugfs_init(adev);
1630 if (r) {
1631 DRM_ERROR("registering gem debugfs failed (%d).\n", r);
1632 }
1633
1634 r = amdgpu_debugfs_regs_init(adev);
1635 if (r) {
1636 DRM_ERROR("registering register debugfs failed (%d).\n", r);
1637 }
1638
Huang Rui50ab2532016-06-12 15:51:09 +08001639 r = amdgpu_debugfs_firmware_init(adev);
1640 if (r) {
1641 DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
1642 return r;
1643 }
1644
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001645 if ((amdgpu_testing & 1)) {
1646 if (adev->accel_working)
1647 amdgpu_test_moves(adev);
1648 else
1649 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
1650 }
1651 if ((amdgpu_testing & 2)) {
1652 if (adev->accel_working)
1653 amdgpu_test_syncing(adev);
1654 else
1655 DRM_INFO("amdgpu: acceleration disabled, skipping sync tests\n");
1656 }
1657 if (amdgpu_benchmarking) {
1658 if (adev->accel_working)
1659 amdgpu_benchmark(adev, amdgpu_benchmarking);
1660 else
1661 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
1662 }
1663
1664 /* enable clockgating, etc. after ib tests, etc. since some blocks require
1665 * explicit gating rather than handling it automatically.
1666 */
1667 r = amdgpu_late_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001668 if (r) {
1669 dev_err(adev->dev, "amdgpu_late_init failed\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001670 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001671 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001672
1673 return 0;
Alex Deucher83ba1262016-06-03 18:21:41 -04001674
1675failed:
1676 if (runtime)
1677 vga_switcheroo_fini_domain_pm_ops(adev->dev);
1678 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001679}
1680
1681static void amdgpu_debugfs_remove_files(struct amdgpu_device *adev);
1682
1683/**
1684 * amdgpu_device_fini - tear down the driver
1685 *
1686 * @adev: amdgpu_device pointer
1687 *
1688 * Tear down the driver info (all asics).
1689 * Called at driver shutdown.
1690 */
1691void amdgpu_device_fini(struct amdgpu_device *adev)
1692{
1693 int r;
1694
1695 DRM_INFO("amdgpu: finishing device.\n");
1696 adev->shutdown = true;
1697 /* evict vram memory */
1698 amdgpu_bo_evict_vram(adev);
1699 amdgpu_ib_pool_fini(adev);
1700 amdgpu_fence_driver_fini(adev);
1701 amdgpu_fbdev_fini(adev);
1702 r = amdgpu_fini(adev);
Alex Deucher8faf0e02015-07-28 11:50:31 -04001703 kfree(adev->ip_block_status);
1704 adev->ip_block_status = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001705 adev->accel_working = false;
1706 /* free i2c buses */
1707 amdgpu_i2c_fini(adev);
1708 amdgpu_atombios_fini(adev);
1709 kfree(adev->bios);
1710 adev->bios = NULL;
1711 vga_switcheroo_unregister_client(adev->pdev);
Alex Deucher83ba1262016-06-03 18:21:41 -04001712 if (adev->flags & AMD_IS_PX)
1713 vga_switcheroo_fini_domain_pm_ops(adev->dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001714 vga_client_register(adev->pdev, NULL, NULL, NULL);
1715 if (adev->rio_mem)
1716 pci_iounmap(adev->pdev, adev->rio_mem);
1717 adev->rio_mem = NULL;
1718 iounmap(adev->rmmio);
1719 adev->rmmio = NULL;
1720 amdgpu_doorbell_fini(adev);
1721 amdgpu_debugfs_regs_cleanup(adev);
1722 amdgpu_debugfs_remove_files(adev);
1723}
1724
1725
1726/*
1727 * Suspend & resume.
1728 */
1729/**
1730 * amdgpu_suspend_kms - initiate device suspend
1731 *
1732 * @pdev: drm dev pointer
1733 * @state: suspend state
1734 *
1735 * Puts the hw in the suspend state (all asics).
1736 * Returns 0 for success or an error on failure.
1737 * Called at driver suspend.
1738 */
1739int amdgpu_suspend_kms(struct drm_device *dev, bool suspend, bool fbcon)
1740{
1741 struct amdgpu_device *adev;
1742 struct drm_crtc *crtc;
1743 struct drm_connector *connector;
Alex Deucher5ceb54c2015-08-05 12:41:48 -04001744 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001745
1746 if (dev == NULL || dev->dev_private == NULL) {
1747 return -ENODEV;
1748 }
1749
1750 adev = dev->dev_private;
1751
1752 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
1753 return 0;
1754
1755 drm_kms_helper_poll_disable(dev);
1756
1757 /* turn off display hw */
Alex Deucher4c7fbc32015-09-23 14:32:06 -04001758 drm_modeset_lock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001759 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1760 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
1761 }
Alex Deucher4c7fbc32015-09-23 14:32:06 -04001762 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001763
Alex Deucher756e6882015-10-08 00:03:36 -04001764 /* unpin the front buffers and cursors */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001765 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Alex Deucher756e6882015-10-08 00:03:36 -04001766 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001767 struct amdgpu_framebuffer *rfb = to_amdgpu_framebuffer(crtc->primary->fb);
1768 struct amdgpu_bo *robj;
1769
Alex Deucher756e6882015-10-08 00:03:36 -04001770 if (amdgpu_crtc->cursor_bo) {
1771 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
1772 r = amdgpu_bo_reserve(aobj, false);
1773 if (r == 0) {
1774 amdgpu_bo_unpin(aobj);
1775 amdgpu_bo_unreserve(aobj);
1776 }
1777 }
1778
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001779 if (rfb == NULL || rfb->obj == NULL) {
1780 continue;
1781 }
1782 robj = gem_to_amdgpu_bo(rfb->obj);
1783 /* don't unpin kernel fb objects */
1784 if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
1785 r = amdgpu_bo_reserve(robj, false);
1786 if (r == 0) {
1787 amdgpu_bo_unpin(robj);
1788 amdgpu_bo_unreserve(robj);
1789 }
1790 }
1791 }
1792 /* evict vram memory */
1793 amdgpu_bo_evict_vram(adev);
1794
Alex Deucher5ceb54c2015-08-05 12:41:48 -04001795 amdgpu_fence_driver_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001796
1797 r = amdgpu_suspend(adev);
1798
1799 /* evict remaining vram memory */
1800 amdgpu_bo_evict_vram(adev);
1801
1802 pci_save_state(dev->pdev);
1803 if (suspend) {
1804 /* Shut down the device */
1805 pci_disable_device(dev->pdev);
1806 pci_set_power_state(dev->pdev, PCI_D3hot);
1807 }
1808
1809 if (fbcon) {
1810 console_lock();
1811 amdgpu_fbdev_set_suspend(adev, 1);
1812 console_unlock();
1813 }
1814 return 0;
1815}
1816
1817/**
1818 * amdgpu_resume_kms - initiate device resume
1819 *
1820 * @pdev: drm dev pointer
1821 *
1822 * Bring the hw back to operating state (all asics).
1823 * Returns 0 for success or an error on failure.
1824 * Called at driver resume.
1825 */
1826int amdgpu_resume_kms(struct drm_device *dev, bool resume, bool fbcon)
1827{
1828 struct drm_connector *connector;
1829 struct amdgpu_device *adev = dev->dev_private;
Alex Deucher756e6882015-10-08 00:03:36 -04001830 struct drm_crtc *crtc;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001831 int r;
1832
1833 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
1834 return 0;
1835
1836 if (fbcon) {
1837 console_lock();
1838 }
1839 if (resume) {
1840 pci_set_power_state(dev->pdev, PCI_D0);
1841 pci_restore_state(dev->pdev);
1842 if (pci_enable_device(dev->pdev)) {
1843 if (fbcon)
1844 console_unlock();
1845 return -1;
1846 }
1847 }
1848
1849 /* post card */
Flora Cuica198522016-02-04 15:10:08 +08001850 if (!amdgpu_card_posted(adev))
1851 amdgpu_atom_asic_init(adev->mode_info.atom_context);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001852
1853 r = amdgpu_resume(adev);
Flora Cuica198522016-02-04 15:10:08 +08001854 if (r)
1855 DRM_ERROR("amdgpu_resume failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001856
Alex Deucher5ceb54c2015-08-05 12:41:48 -04001857 amdgpu_fence_driver_resume(adev);
1858
Flora Cuica198522016-02-04 15:10:08 +08001859 if (resume) {
1860 r = amdgpu_ib_ring_tests(adev);
1861 if (r)
1862 DRM_ERROR("ib ring test failed (%d).\n", r);
1863 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001864
1865 r = amdgpu_late_init(adev);
1866 if (r)
1867 return r;
1868
Alex Deucher756e6882015-10-08 00:03:36 -04001869 /* pin cursors */
1870 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
1871 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
1872
1873 if (amdgpu_crtc->cursor_bo) {
1874 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
1875 r = amdgpu_bo_reserve(aobj, false);
1876 if (r == 0) {
1877 r = amdgpu_bo_pin(aobj,
1878 AMDGPU_GEM_DOMAIN_VRAM,
1879 &amdgpu_crtc->cursor_addr);
1880 if (r != 0)
1881 DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
1882 amdgpu_bo_unreserve(aobj);
1883 }
1884 }
1885 }
1886
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001887 /* blat the mode back in */
1888 if (fbcon) {
1889 drm_helper_resume_force_mode(dev);
1890 /* turn on display hw */
Alex Deucher4c7fbc32015-09-23 14:32:06 -04001891 drm_modeset_lock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001892 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1893 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
1894 }
Alex Deucher4c7fbc32015-09-23 14:32:06 -04001895 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001896 }
1897
1898 drm_kms_helper_poll_enable(dev);
Alex Deucher54fb2a52015-11-24 14:30:56 -05001899 drm_helper_hpd_irq_event(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001900
1901 if (fbcon) {
1902 amdgpu_fbdev_set_suspend(adev, 0);
1903 console_unlock();
1904 }
1905
1906 return 0;
1907}
1908
1909/**
1910 * amdgpu_gpu_reset - reset the asic
1911 *
1912 * @adev: amdgpu device pointer
1913 *
1914 * Attempt the reset the GPU if it has hung (all asics).
1915 * Returns 0 for success or an error on failure.
1916 */
1917int amdgpu_gpu_reset(struct amdgpu_device *adev)
1918{
1919 unsigned ring_sizes[AMDGPU_MAX_RINGS];
1920 uint32_t *ring_data[AMDGPU_MAX_RINGS];
1921
1922 bool saved = false;
1923
1924 int i, r;
1925 int resched;
1926
Marek Olšákd94aed52015-05-05 21:13:49 +02001927 atomic_inc(&adev->gpu_reset_counter);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001928
Chunming Zhou8b2ac102016-06-12 15:43:20 +08001929 /* evict vram memory */
1930 amdgpu_bo_evict_vram(adev);
1931
Chunming Zhou0875dc92016-06-12 15:41:58 +08001932 /* block scheduler */
1933 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
1934 struct amdgpu_ring *ring = adev->rings[i];
1935
1936 if (!ring)
1937 continue;
1938 kthread_park(ring->sched.thread);
1939 }
Chunming Zhou8b2ac102016-06-12 15:43:20 +08001940
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001941 /* block TTM */
1942 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
1943
1944 r = amdgpu_suspend(adev);
1945
1946 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
1947 struct amdgpu_ring *ring = adev->rings[i];
1948 if (!ring)
1949 continue;
1950
1951 ring_sizes[i] = amdgpu_ring_backup(ring, &ring_data[i]);
1952 if (ring_sizes[i]) {
1953 saved = true;
1954 dev_info(adev->dev, "Saved %d dwords of commands "
1955 "on ring %d.\n", ring_sizes[i], i);
1956 }
1957 }
1958
1959retry:
1960 r = amdgpu_asic_reset(adev);
Alex Deucherbfa99262016-01-15 11:59:48 -05001961 /* post card */
1962 amdgpu_atom_asic_init(adev->mode_info.atom_context);
1963
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001964 if (!r) {
1965 dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
1966 r = amdgpu_resume(adev);
1967 }
1968
1969 if (!r) {
1970 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
1971 struct amdgpu_ring *ring = adev->rings[i];
1972 if (!ring)
1973 continue;
Chunming Zhou0875dc92016-06-12 15:41:58 +08001974 kthread_unpark(ring->sched.thread);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001975 amdgpu_ring_restore(ring, ring_sizes[i], ring_data[i]);
1976 ring_sizes[i] = 0;
1977 ring_data[i] = NULL;
1978 }
1979
1980 r = amdgpu_ib_ring_tests(adev);
1981 if (r) {
1982 dev_err(adev->dev, "ib ring test failed (%d).\n", r);
1983 if (saved) {
1984 saved = false;
1985 r = amdgpu_suspend(adev);
1986 goto retry;
1987 }
1988 }
1989 } else {
1990 amdgpu_fence_driver_force_completion(adev);
1991 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
Chunming Zhou0875dc92016-06-12 15:41:58 +08001992 if (adev->rings[i]) {
1993 kthread_unpark(adev->rings[i]->sched.thread);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001994 kfree(ring_data[i]);
Chunming Zhou0875dc92016-06-12 15:41:58 +08001995 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001996 }
1997 }
1998
1999 drm_helper_resume_force_mode(adev->ddev);
2000
2001 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
2002 if (r) {
2003 /* bad news, how to tell it to userspace ? */
2004 dev_info(adev->dev, "GPU reset failed\n");
2005 }
Chunming Zhou0eaeb072016-06-16 16:54:53 +08002006 amdgpu_irq_gpu_reset_resume_helper(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002007
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002008 return r;
2009}
2010
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002011void amdgpu_get_pcie_info(struct amdgpu_device *adev)
2012{
2013 u32 mask;
2014 int ret;
2015
Alex Deuchercd474ba2016-02-04 10:21:23 -05002016 if (amdgpu_pcie_gen_cap)
2017 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
2018
2019 if (amdgpu_pcie_lane_cap)
2020 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
2021
2022 /* covers APUs as well */
2023 if (pci_is_root_bus(adev->pdev->bus)) {
2024 if (adev->pm.pcie_gen_mask == 0)
2025 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
2026 if (adev->pm.pcie_mlw_mask == 0)
2027 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002028 return;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002029 }
Alex Deuchercd474ba2016-02-04 10:21:23 -05002030
2031 if (adev->pm.pcie_gen_mask == 0) {
2032 ret = drm_pcie_get_speed_cap_mask(adev->ddev, &mask);
2033 if (!ret) {
2034 adev->pm.pcie_gen_mask = (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
2035 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
2036 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
2037
2038 if (mask & DRM_PCIE_SPEED_25)
2039 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
2040 if (mask & DRM_PCIE_SPEED_50)
2041 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2;
2042 if (mask & DRM_PCIE_SPEED_80)
2043 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3;
2044 } else {
2045 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
2046 }
2047 }
2048 if (adev->pm.pcie_mlw_mask == 0) {
2049 ret = drm_pcie_get_max_link_width(adev->ddev, &mask);
2050 if (!ret) {
2051 switch (mask) {
2052 case 32:
2053 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
2054 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
2055 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2056 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2057 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2058 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2059 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2060 break;
2061 case 16:
2062 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
2063 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2064 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2065 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2066 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2067 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2068 break;
2069 case 12:
2070 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2071 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2072 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2073 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2074 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2075 break;
2076 case 8:
2077 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2078 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2079 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2080 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2081 break;
2082 case 4:
2083 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2084 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2085 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2086 break;
2087 case 2:
2088 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2089 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2090 break;
2091 case 1:
2092 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
2093 break;
2094 default:
2095 break;
2096 }
2097 } else {
2098 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002099 }
2100 }
2101}
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002102
2103/*
2104 * Debugfs
2105 */
2106int amdgpu_debugfs_add_files(struct amdgpu_device *adev,
Nils Wallménius06ab6832016-05-02 12:46:15 -04002107 const struct drm_info_list *files,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002108 unsigned nfiles)
2109{
2110 unsigned i;
2111
2112 for (i = 0; i < adev->debugfs_count; i++) {
2113 if (adev->debugfs[i].files == files) {
2114 /* Already registered */
2115 return 0;
2116 }
2117 }
2118
2119 i = adev->debugfs_count + 1;
2120 if (i > AMDGPU_DEBUGFS_MAX_COMPONENTS) {
2121 DRM_ERROR("Reached maximum number of debugfs components.\n");
2122 DRM_ERROR("Report so we increase "
2123 "AMDGPU_DEBUGFS_MAX_COMPONENTS.\n");
2124 return -EINVAL;
2125 }
2126 adev->debugfs[adev->debugfs_count].files = files;
2127 adev->debugfs[adev->debugfs_count].num_files = nfiles;
2128 adev->debugfs_count = i;
2129#if defined(CONFIG_DEBUG_FS)
2130 drm_debugfs_create_files(files, nfiles,
2131 adev->ddev->control->debugfs_root,
2132 adev->ddev->control);
2133 drm_debugfs_create_files(files, nfiles,
2134 adev->ddev->primary->debugfs_root,
2135 adev->ddev->primary);
2136#endif
2137 return 0;
2138}
2139
2140static void amdgpu_debugfs_remove_files(struct amdgpu_device *adev)
2141{
2142#if defined(CONFIG_DEBUG_FS)
2143 unsigned i;
2144
2145 for (i = 0; i < adev->debugfs_count; i++) {
2146 drm_debugfs_remove_files(adev->debugfs[i].files,
2147 adev->debugfs[i].num_files,
2148 adev->ddev->control);
2149 drm_debugfs_remove_files(adev->debugfs[i].files,
2150 adev->debugfs[i].num_files,
2151 adev->ddev->primary);
2152 }
2153#endif
2154}
2155
2156#if defined(CONFIG_DEBUG_FS)
2157
2158static ssize_t amdgpu_debugfs_regs_read(struct file *f, char __user *buf,
2159 size_t size, loff_t *pos)
2160{
2161 struct amdgpu_device *adev = f->f_inode->i_private;
2162 ssize_t result = 0;
2163 int r;
2164
2165 if (size & 0x3 || *pos & 0x3)
2166 return -EINVAL;
2167
2168 while (size) {
2169 uint32_t value;
2170
2171 if (*pos > adev->rmmio_size)
2172 return result;
2173
2174 value = RREG32(*pos >> 2);
2175 r = put_user(value, (uint32_t *)buf);
2176 if (r)
2177 return r;
2178
2179 result += 4;
2180 buf += 4;
2181 *pos += 4;
2182 size -= 4;
2183 }
2184
2185 return result;
2186}
2187
2188static ssize_t amdgpu_debugfs_regs_write(struct file *f, const char __user *buf,
2189 size_t size, loff_t *pos)
2190{
2191 struct amdgpu_device *adev = f->f_inode->i_private;
2192 ssize_t result = 0;
2193 int r;
2194
2195 if (size & 0x3 || *pos & 0x3)
2196 return -EINVAL;
2197
2198 while (size) {
2199 uint32_t value;
2200
2201 if (*pos > adev->rmmio_size)
2202 return result;
2203
2204 r = get_user(value, (uint32_t *)buf);
2205 if (r)
2206 return r;
2207
2208 WREG32(*pos >> 2, value);
2209
2210 result += 4;
2211 buf += 4;
2212 *pos += 4;
2213 size -= 4;
2214 }
2215
2216 return result;
2217}
2218
Tom St Denisadcec282016-04-15 13:08:44 -04002219static ssize_t amdgpu_debugfs_regs_pcie_read(struct file *f, char __user *buf,
2220 size_t size, loff_t *pos)
2221{
2222 struct amdgpu_device *adev = f->f_inode->i_private;
2223 ssize_t result = 0;
2224 int r;
2225
2226 if (size & 0x3 || *pos & 0x3)
2227 return -EINVAL;
2228
2229 while (size) {
2230 uint32_t value;
2231
2232 value = RREG32_PCIE(*pos >> 2);
2233 r = put_user(value, (uint32_t *)buf);
2234 if (r)
2235 return r;
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_pcie_write(struct file *f, const 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 r = get_user(value, (uint32_t *)buf);
2260 if (r)
2261 return r;
2262
2263 WREG32_PCIE(*pos >> 2, value);
2264
2265 result += 4;
2266 buf += 4;
2267 *pos += 4;
2268 size -= 4;
2269 }
2270
2271 return result;
2272}
2273
2274static ssize_t amdgpu_debugfs_regs_didt_read(struct file *f, char __user *buf,
2275 size_t size, loff_t *pos)
2276{
2277 struct amdgpu_device *adev = f->f_inode->i_private;
2278 ssize_t result = 0;
2279 int r;
2280
2281 if (size & 0x3 || *pos & 0x3)
2282 return -EINVAL;
2283
2284 while (size) {
2285 uint32_t value;
2286
2287 value = RREG32_DIDT(*pos >> 2);
2288 r = put_user(value, (uint32_t *)buf);
2289 if (r)
2290 return r;
2291
2292 result += 4;
2293 buf += 4;
2294 *pos += 4;
2295 size -= 4;
2296 }
2297
2298 return result;
2299}
2300
2301static ssize_t amdgpu_debugfs_regs_didt_write(struct file *f, const char __user *buf,
2302 size_t size, loff_t *pos)
2303{
2304 struct amdgpu_device *adev = f->f_inode->i_private;
2305 ssize_t result = 0;
2306 int r;
2307
2308 if (size & 0x3 || *pos & 0x3)
2309 return -EINVAL;
2310
2311 while (size) {
2312 uint32_t value;
2313
2314 r = get_user(value, (uint32_t *)buf);
2315 if (r)
2316 return r;
2317
2318 WREG32_DIDT(*pos >> 2, value);
2319
2320 result += 4;
2321 buf += 4;
2322 *pos += 4;
2323 size -= 4;
2324 }
2325
2326 return result;
2327}
2328
2329static ssize_t amdgpu_debugfs_regs_smc_read(struct file *f, char __user *buf,
2330 size_t size, loff_t *pos)
2331{
2332 struct amdgpu_device *adev = f->f_inode->i_private;
2333 ssize_t result = 0;
2334 int r;
2335
2336 if (size & 0x3 || *pos & 0x3)
2337 return -EINVAL;
2338
2339 while (size) {
2340 uint32_t value;
2341
2342 value = RREG32_SMC(*pos >> 2);
2343 r = put_user(value, (uint32_t *)buf);
2344 if (r)
2345 return r;
2346
2347 result += 4;
2348 buf += 4;
2349 *pos += 4;
2350 size -= 4;
2351 }
2352
2353 return result;
2354}
2355
2356static ssize_t amdgpu_debugfs_regs_smc_write(struct file *f, const char __user *buf,
2357 size_t size, loff_t *pos)
2358{
2359 struct amdgpu_device *adev = f->f_inode->i_private;
2360 ssize_t result = 0;
2361 int r;
2362
2363 if (size & 0x3 || *pos & 0x3)
2364 return -EINVAL;
2365
2366 while (size) {
2367 uint32_t value;
2368
2369 r = get_user(value, (uint32_t *)buf);
2370 if (r)
2371 return r;
2372
2373 WREG32_SMC(*pos >> 2, value);
2374
2375 result += 4;
2376 buf += 4;
2377 *pos += 4;
2378 size -= 4;
2379 }
2380
2381 return result;
2382}
2383
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002384static const struct file_operations amdgpu_debugfs_regs_fops = {
2385 .owner = THIS_MODULE,
2386 .read = amdgpu_debugfs_regs_read,
2387 .write = amdgpu_debugfs_regs_write,
2388 .llseek = default_llseek
2389};
Tom St Denisadcec282016-04-15 13:08:44 -04002390static const struct file_operations amdgpu_debugfs_regs_didt_fops = {
2391 .owner = THIS_MODULE,
2392 .read = amdgpu_debugfs_regs_didt_read,
2393 .write = amdgpu_debugfs_regs_didt_write,
2394 .llseek = default_llseek
2395};
2396static const struct file_operations amdgpu_debugfs_regs_pcie_fops = {
2397 .owner = THIS_MODULE,
2398 .read = amdgpu_debugfs_regs_pcie_read,
2399 .write = amdgpu_debugfs_regs_pcie_write,
2400 .llseek = default_llseek
2401};
2402static const struct file_operations amdgpu_debugfs_regs_smc_fops = {
2403 .owner = THIS_MODULE,
2404 .read = amdgpu_debugfs_regs_smc_read,
2405 .write = amdgpu_debugfs_regs_smc_write,
2406 .llseek = default_llseek
2407};
2408
2409static const struct file_operations *debugfs_regs[] = {
2410 &amdgpu_debugfs_regs_fops,
2411 &amdgpu_debugfs_regs_didt_fops,
2412 &amdgpu_debugfs_regs_pcie_fops,
2413 &amdgpu_debugfs_regs_smc_fops,
2414};
2415
2416static const char *debugfs_regs_names[] = {
2417 "amdgpu_regs",
2418 "amdgpu_regs_didt",
2419 "amdgpu_regs_pcie",
2420 "amdgpu_regs_smc",
2421};
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002422
2423static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
2424{
2425 struct drm_minor *minor = adev->ddev->primary;
2426 struct dentry *ent, *root = minor->debugfs_root;
Tom St Denisadcec282016-04-15 13:08:44 -04002427 unsigned i, j;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002428
Tom St Denisadcec282016-04-15 13:08:44 -04002429 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
2430 ent = debugfs_create_file(debugfs_regs_names[i],
2431 S_IFREG | S_IRUGO, root,
2432 adev, debugfs_regs[i]);
2433 if (IS_ERR(ent)) {
2434 for (j = 0; j < i; j++) {
2435 debugfs_remove(adev->debugfs_regs[i]);
2436 adev->debugfs_regs[i] = NULL;
2437 }
2438 return PTR_ERR(ent);
2439 }
2440
2441 if (!i)
2442 i_size_write(ent->d_inode, adev->rmmio_size);
2443 adev->debugfs_regs[i] = ent;
2444 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002445
2446 return 0;
2447}
2448
2449static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev)
2450{
Tom St Denisadcec282016-04-15 13:08:44 -04002451 unsigned i;
2452
2453 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
2454 if (adev->debugfs_regs[i]) {
2455 debugfs_remove(adev->debugfs_regs[i]);
2456 adev->debugfs_regs[i] = NULL;
2457 }
2458 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002459}
2460
2461int amdgpu_debugfs_init(struct drm_minor *minor)
2462{
2463 return 0;
2464}
2465
2466void amdgpu_debugfs_cleanup(struct drm_minor *minor)
2467{
2468}
Alexander Kuleshov7cebc722015-06-27 13:16:05 +06002469#else
2470static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
2471{
2472 return 0;
2473}
2474static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev) { }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002475#endif