blob: 22d331947aca6bd74d25440fbc15d14db790819e [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"
Ken Wang33f34802016-01-21 17:29:41 +080044#ifdef CONFIG_DRM_AMDGPU_SI
45#include "si.h"
46#endif
Alex Deuchera2e73f52015-04-20 17:09:27 -040047#ifdef CONFIG_DRM_AMDGPU_CIK
48#include "cik.h"
49#endif
Alex Deucheraaa36a92015-04-20 17:31:14 -040050#include "vi.h"
Alex Deucherd38ceaf2015-04-20 16:55:21 -040051#include "bif/bif_4_1_d.h"
Emily Deng9accf2f2016-08-10 16:01:25 +080052#include <linux/pci.h>
Monk Liubec86372016-09-14 19:38:08 +080053#include <linux/firmware.h>
Alex Deucherd38ceaf2015-04-20 16:55:21 -040054
55static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev);
56static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev);
57
58static const char *amdgpu_asic_name[] = {
Ken Wangda69c1612016-01-21 19:08:55 +080059 "TAHITI",
60 "PITCAIRN",
61 "VERDE",
62 "OLAND",
63 "HAINAN",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040064 "BONAIRE",
65 "KAVERI",
66 "KABINI",
67 "HAWAII",
68 "MULLINS",
69 "TOPAZ",
70 "TONGA",
David Zhang48299f92015-07-08 01:05:16 +080071 "FIJI",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040072 "CARRIZO",
Samuel Li139f4912015-10-08 14:50:27 -040073 "STONEY",
Flora Cui2cc0c0b2016-03-14 18:33:29 -040074 "POLARIS10",
75 "POLARIS11",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040076 "LAST",
77};
78
79bool amdgpu_device_is_px(struct drm_device *dev)
80{
81 struct amdgpu_device *adev = dev->dev_private;
82
Jammy Zhou2f7d10b2015-07-22 11:29:01 +080083 if (adev->flags & AMD_IS_PX)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040084 return true;
85 return false;
86}
87
88/*
89 * MMIO register access helper functions.
90 */
91uint32_t amdgpu_mm_rreg(struct amdgpu_device *adev, uint32_t reg,
92 bool always_indirect)
93{
Tom St Denisf4b373f2016-05-31 08:02:27 -040094 uint32_t ret;
95
Alex Deucherd38ceaf2015-04-20 16:55:21 -040096 if ((reg * 4) < adev->rmmio_size && !always_indirect)
Tom St Denisf4b373f2016-05-31 08:02:27 -040097 ret = readl(((void __iomem *)adev->rmmio) + (reg * 4));
Alex Deucherd38ceaf2015-04-20 16:55:21 -040098 else {
99 unsigned long flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400100
101 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
102 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
103 ret = readl(((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
104 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400105 }
Tom St Denisf4b373f2016-05-31 08:02:27 -0400106 trace_amdgpu_mm_rreg(adev->pdev->device, reg, ret);
107 return ret;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400108}
109
110void amdgpu_mm_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v,
111 bool always_indirect)
112{
Tom St Denisf4b373f2016-05-31 08:02:27 -0400113 trace_amdgpu_mm_wreg(adev->pdev->device, reg, v);
Monk Liu4e99a442016-03-31 13:26:59 +0800114
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400115 if ((reg * 4) < adev->rmmio_size && !always_indirect)
116 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
117 else {
118 unsigned long flags;
119
120 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
121 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
122 writel(v, ((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
123 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
124 }
125}
126
127u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 reg)
128{
129 if ((reg * 4) < adev->rio_mem_size)
130 return ioread32(adev->rio_mem + (reg * 4));
131 else {
132 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
133 return ioread32(adev->rio_mem + (mmMM_DATA * 4));
134 }
135}
136
137void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
138{
139
140 if ((reg * 4) < adev->rio_mem_size)
141 iowrite32(v, adev->rio_mem + (reg * 4));
142 else {
143 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
144 iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
145 }
146}
147
148/**
149 * amdgpu_mm_rdoorbell - read a doorbell dword
150 *
151 * @adev: amdgpu_device pointer
152 * @index: doorbell index
153 *
154 * Returns the value in the doorbell aperture at the
155 * requested doorbell index (CIK).
156 */
157u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
158{
159 if (index < adev->doorbell.num_doorbells) {
160 return readl(adev->doorbell.ptr + index);
161 } else {
162 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
163 return 0;
164 }
165}
166
167/**
168 * amdgpu_mm_wdoorbell - write a doorbell dword
169 *
170 * @adev: amdgpu_device pointer
171 * @index: doorbell index
172 * @v: value to write
173 *
174 * Writes @v to the doorbell aperture at the
175 * requested doorbell index (CIK).
176 */
177void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
178{
179 if (index < adev->doorbell.num_doorbells) {
180 writel(v, adev->doorbell.ptr + index);
181 } else {
182 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
183 }
184}
185
186/**
187 * amdgpu_invalid_rreg - dummy reg read function
188 *
189 * @adev: amdgpu device pointer
190 * @reg: offset of register
191 *
192 * Dummy register read function. Used for register blocks
193 * that certain asics don't have (all asics).
194 * Returns the value in the register.
195 */
196static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
197{
198 DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
199 BUG();
200 return 0;
201}
202
203/**
204 * amdgpu_invalid_wreg - dummy reg write function
205 *
206 * @adev: amdgpu device pointer
207 * @reg: offset of register
208 * @v: value to write to the register
209 *
210 * Dummy register read function. Used for register blocks
211 * that certain asics don't have (all asics).
212 */
213static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
214{
215 DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
216 reg, v);
217 BUG();
218}
219
220/**
221 * amdgpu_block_invalid_rreg - dummy reg read function
222 *
223 * @adev: amdgpu device pointer
224 * @block: offset of instance
225 * @reg: offset of register
226 *
227 * Dummy register read function. Used for register blocks
228 * that certain asics don't have (all asics).
229 * Returns the value in the register.
230 */
231static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
232 uint32_t block, uint32_t reg)
233{
234 DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
235 reg, block);
236 BUG();
237 return 0;
238}
239
240/**
241 * amdgpu_block_invalid_wreg - dummy reg write function
242 *
243 * @adev: amdgpu device pointer
244 * @block: offset of instance
245 * @reg: offset of register
246 * @v: value to write to the register
247 *
248 * Dummy register read function. Used for register blocks
249 * that certain asics don't have (all asics).
250 */
251static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
252 uint32_t block,
253 uint32_t reg, uint32_t v)
254{
255 DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
256 reg, block, v);
257 BUG();
258}
259
260static int amdgpu_vram_scratch_init(struct amdgpu_device *adev)
261{
262 int r;
263
264 if (adev->vram_scratch.robj == NULL) {
265 r = amdgpu_bo_create(adev, AMDGPU_GPU_PAGE_SIZE,
Alex Deucher857d9132015-08-27 00:14:16 -0400266 PAGE_SIZE, true, AMDGPU_GEM_DOMAIN_VRAM,
267 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED,
Christian König72d76682015-09-03 17:34:59 +0200268 NULL, NULL, &adev->vram_scratch.robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400269 if (r) {
270 return r;
271 }
272 }
273
274 r = amdgpu_bo_reserve(adev->vram_scratch.robj, false);
275 if (unlikely(r != 0))
276 return r;
277 r = amdgpu_bo_pin(adev->vram_scratch.robj,
278 AMDGPU_GEM_DOMAIN_VRAM, &adev->vram_scratch.gpu_addr);
279 if (r) {
280 amdgpu_bo_unreserve(adev->vram_scratch.robj);
281 return r;
282 }
283 r = amdgpu_bo_kmap(adev->vram_scratch.robj,
284 (void **)&adev->vram_scratch.ptr);
285 if (r)
286 amdgpu_bo_unpin(adev->vram_scratch.robj);
287 amdgpu_bo_unreserve(adev->vram_scratch.robj);
288
289 return r;
290}
291
292static void amdgpu_vram_scratch_fini(struct amdgpu_device *adev)
293{
294 int r;
295
296 if (adev->vram_scratch.robj == NULL) {
297 return;
298 }
299 r = amdgpu_bo_reserve(adev->vram_scratch.robj, false);
300 if (likely(r == 0)) {
301 amdgpu_bo_kunmap(adev->vram_scratch.robj);
302 amdgpu_bo_unpin(adev->vram_scratch.robj);
303 amdgpu_bo_unreserve(adev->vram_scratch.robj);
304 }
305 amdgpu_bo_unref(&adev->vram_scratch.robj);
306}
307
308/**
309 * amdgpu_program_register_sequence - program an array of registers.
310 *
311 * @adev: amdgpu_device pointer
312 * @registers: pointer to the register array
313 * @array_size: size of the register array
314 *
315 * Programs an array or registers with and and or masks.
316 * This is a helper for setting golden registers.
317 */
318void amdgpu_program_register_sequence(struct amdgpu_device *adev,
319 const u32 *registers,
320 const u32 array_size)
321{
322 u32 tmp, reg, and_mask, or_mask;
323 int i;
324
325 if (array_size % 3)
326 return;
327
328 for (i = 0; i < array_size; i +=3) {
329 reg = registers[i + 0];
330 and_mask = registers[i + 1];
331 or_mask = registers[i + 2];
332
333 if (and_mask == 0xffffffff) {
334 tmp = or_mask;
335 } else {
336 tmp = RREG32(reg);
337 tmp &= ~and_mask;
338 tmp |= or_mask;
339 }
340 WREG32(reg, tmp);
341 }
342}
343
344void amdgpu_pci_config_reset(struct amdgpu_device *adev)
345{
346 pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
347}
348
349/*
350 * GPU doorbell aperture helpers function.
351 */
352/**
353 * amdgpu_doorbell_init - Init doorbell driver information.
354 *
355 * @adev: amdgpu_device pointer
356 *
357 * Init doorbell driver information (CIK)
358 * Returns 0 on success, error on failure.
359 */
360static int amdgpu_doorbell_init(struct amdgpu_device *adev)
361{
362 /* doorbell bar mapping */
363 adev->doorbell.base = pci_resource_start(adev->pdev, 2);
364 adev->doorbell.size = pci_resource_len(adev->pdev, 2);
365
Christian Königedf600d2016-05-03 15:54:54 +0200366 adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400367 AMDGPU_DOORBELL_MAX_ASSIGNMENT+1);
368 if (adev->doorbell.num_doorbells == 0)
369 return -EINVAL;
370
371 adev->doorbell.ptr = ioremap(adev->doorbell.base, adev->doorbell.num_doorbells * sizeof(u32));
372 if (adev->doorbell.ptr == NULL) {
373 return -ENOMEM;
374 }
375 DRM_INFO("doorbell mmio base: 0x%08X\n", (uint32_t)adev->doorbell.base);
376 DRM_INFO("doorbell mmio size: %u\n", (unsigned)adev->doorbell.size);
377
378 return 0;
379}
380
381/**
382 * amdgpu_doorbell_fini - Tear down doorbell driver information.
383 *
384 * @adev: amdgpu_device pointer
385 *
386 * Tear down doorbell driver information (CIK)
387 */
388static void amdgpu_doorbell_fini(struct amdgpu_device *adev)
389{
390 iounmap(adev->doorbell.ptr);
391 adev->doorbell.ptr = NULL;
392}
393
394/**
395 * amdgpu_doorbell_get_kfd_info - Report doorbell configuration required to
396 * setup amdkfd
397 *
398 * @adev: amdgpu_device pointer
399 * @aperture_base: output returning doorbell aperture base physical address
400 * @aperture_size: output returning doorbell aperture size in bytes
401 * @start_offset: output returning # of doorbell bytes reserved for amdgpu.
402 *
403 * amdgpu and amdkfd share the doorbell aperture. amdgpu sets it up,
404 * takes doorbells required for its own rings and reports the setup to amdkfd.
405 * amdgpu reserved doorbells are at the start of the doorbell aperture.
406 */
407void amdgpu_doorbell_get_kfd_info(struct amdgpu_device *adev,
408 phys_addr_t *aperture_base,
409 size_t *aperture_size,
410 size_t *start_offset)
411{
412 /*
413 * The first num_doorbells are used by amdgpu.
414 * amdkfd takes whatever's left in the aperture.
415 */
416 if (adev->doorbell.size > adev->doorbell.num_doorbells * sizeof(u32)) {
417 *aperture_base = adev->doorbell.base;
418 *aperture_size = adev->doorbell.size;
419 *start_offset = adev->doorbell.num_doorbells * sizeof(u32);
420 } else {
421 *aperture_base = 0;
422 *aperture_size = 0;
423 *start_offset = 0;
424 }
425}
426
427/*
428 * amdgpu_wb_*()
429 * Writeback is the the method by which the the GPU updates special pages
430 * in memory with the status of certain GPU events (fences, ring pointers,
431 * etc.).
432 */
433
434/**
435 * amdgpu_wb_fini - Disable Writeback and free memory
436 *
437 * @adev: amdgpu_device pointer
438 *
439 * Disables Writeback and frees the Writeback memory (all asics).
440 * Used at driver shutdown.
441 */
442static void amdgpu_wb_fini(struct amdgpu_device *adev)
443{
444 if (adev->wb.wb_obj) {
445 if (!amdgpu_bo_reserve(adev->wb.wb_obj, false)) {
446 amdgpu_bo_kunmap(adev->wb.wb_obj);
447 amdgpu_bo_unpin(adev->wb.wb_obj);
448 amdgpu_bo_unreserve(adev->wb.wb_obj);
449 }
450 amdgpu_bo_unref(&adev->wb.wb_obj);
451 adev->wb.wb = NULL;
452 adev->wb.wb_obj = NULL;
453 }
454}
455
456/**
457 * amdgpu_wb_init- Init Writeback driver info and allocate memory
458 *
459 * @adev: amdgpu_device pointer
460 *
461 * Disables Writeback and frees the Writeback memory (all asics).
462 * Used at driver startup.
463 * Returns 0 on success or an -error on failure.
464 */
465static int amdgpu_wb_init(struct amdgpu_device *adev)
466{
467 int r;
468
469 if (adev->wb.wb_obj == NULL) {
470 r = amdgpu_bo_create(adev, AMDGPU_MAX_WB * 4, PAGE_SIZE, true,
Christian König72d76682015-09-03 17:34:59 +0200471 AMDGPU_GEM_DOMAIN_GTT, 0, NULL, NULL,
472 &adev->wb.wb_obj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400473 if (r) {
474 dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
475 return r;
476 }
477 r = amdgpu_bo_reserve(adev->wb.wb_obj, false);
478 if (unlikely(r != 0)) {
479 amdgpu_wb_fini(adev);
480 return r;
481 }
482 r = amdgpu_bo_pin(adev->wb.wb_obj, AMDGPU_GEM_DOMAIN_GTT,
483 &adev->wb.gpu_addr);
484 if (r) {
485 amdgpu_bo_unreserve(adev->wb.wb_obj);
486 dev_warn(adev->dev, "(%d) pin WB bo failed\n", r);
487 amdgpu_wb_fini(adev);
488 return r;
489 }
490 r = amdgpu_bo_kmap(adev->wb.wb_obj, (void **)&adev->wb.wb);
491 amdgpu_bo_unreserve(adev->wb.wb_obj);
492 if (r) {
493 dev_warn(adev->dev, "(%d) map WB bo failed\n", r);
494 amdgpu_wb_fini(adev);
495 return r;
496 }
497
498 adev->wb.num_wb = AMDGPU_MAX_WB;
499 memset(&adev->wb.used, 0, sizeof(adev->wb.used));
500
501 /* clear wb memory */
502 memset((char *)adev->wb.wb, 0, AMDGPU_GPU_PAGE_SIZE);
503 }
504
505 return 0;
506}
507
508/**
509 * amdgpu_wb_get - Allocate a wb entry
510 *
511 * @adev: amdgpu_device pointer
512 * @wb: wb index
513 *
514 * Allocate a wb slot for use by the driver (all asics).
515 * Returns 0 on success or -EINVAL on failure.
516 */
517int amdgpu_wb_get(struct amdgpu_device *adev, u32 *wb)
518{
519 unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
520 if (offset < adev->wb.num_wb) {
521 __set_bit(offset, adev->wb.used);
522 *wb = offset;
523 return 0;
524 } else {
525 return -EINVAL;
526 }
527}
528
529/**
530 * amdgpu_wb_free - Free a wb entry
531 *
532 * @adev: amdgpu_device pointer
533 * @wb: wb index
534 *
535 * Free a wb slot allocated for use by the driver (all asics)
536 */
537void amdgpu_wb_free(struct amdgpu_device *adev, u32 wb)
538{
539 if (wb < adev->wb.num_wb)
540 __clear_bit(wb, adev->wb.used);
541}
542
543/**
544 * amdgpu_vram_location - try to find VRAM location
545 * @adev: amdgpu device structure holding all necessary informations
546 * @mc: memory controller structure holding memory informations
547 * @base: base address at which to put VRAM
548 *
549 * Function will place try to place VRAM at base address provided
550 * as parameter (which is so far either PCI aperture address or
551 * for IGP TOM base address).
552 *
553 * If there is not enough space to fit the unvisible VRAM in the 32bits
554 * address space then we limit the VRAM size to the aperture.
555 *
556 * Note: We don't explicitly enforce VRAM start to be aligned on VRAM size,
557 * this shouldn't be a problem as we are using the PCI aperture as a reference.
558 * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but
559 * not IGP.
560 *
561 * Note: we use mc_vram_size as on some board we need to program the mc to
562 * cover the whole aperture even if VRAM size is inferior to aperture size
563 * Novell bug 204882 + along with lots of ubuntu ones
564 *
565 * Note: when limiting vram it's safe to overwritte real_vram_size because
566 * we are not in case where real_vram_size is inferior to mc_vram_size (ie
567 * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu
568 * ones)
569 *
570 * Note: IGP TOM addr should be the same as the aperture addr, we don't
571 * explicitly check for that thought.
572 *
573 * FIXME: when reducing VRAM size align new size on power of 2.
574 */
575void amdgpu_vram_location(struct amdgpu_device *adev, struct amdgpu_mc *mc, u64 base)
576{
577 uint64_t limit = (uint64_t)amdgpu_vram_limit << 20;
578
579 mc->vram_start = base;
580 if (mc->mc_vram_size > (adev->mc.mc_mask - base + 1)) {
581 dev_warn(adev->dev, "limiting VRAM to PCI aperture size\n");
582 mc->real_vram_size = mc->aper_size;
583 mc->mc_vram_size = mc->aper_size;
584 }
585 mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
586 if (limit && limit < mc->real_vram_size)
587 mc->real_vram_size = limit;
588 dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
589 mc->mc_vram_size >> 20, mc->vram_start,
590 mc->vram_end, mc->real_vram_size >> 20);
591}
592
593/**
594 * amdgpu_gtt_location - try to find GTT location
595 * @adev: amdgpu device structure holding all necessary informations
596 * @mc: memory controller structure holding memory informations
597 *
598 * Function will place try to place GTT before or after VRAM.
599 *
600 * If GTT size is bigger than space left then we ajust GTT size.
601 * Thus function will never fails.
602 *
603 * FIXME: when reducing GTT size align new size on power of 2.
604 */
605void amdgpu_gtt_location(struct amdgpu_device *adev, struct amdgpu_mc *mc)
606{
607 u64 size_af, size_bf;
608
609 size_af = ((adev->mc.mc_mask - mc->vram_end) + mc->gtt_base_align) & ~mc->gtt_base_align;
610 size_bf = mc->vram_start & ~mc->gtt_base_align;
611 if (size_bf > size_af) {
612 if (mc->gtt_size > size_bf) {
613 dev_warn(adev->dev, "limiting GTT\n");
614 mc->gtt_size = size_bf;
615 }
616 mc->gtt_start = (mc->vram_start & ~mc->gtt_base_align) - mc->gtt_size;
617 } else {
618 if (mc->gtt_size > size_af) {
619 dev_warn(adev->dev, "limiting GTT\n");
620 mc->gtt_size = size_af;
621 }
622 mc->gtt_start = (mc->vram_end + 1 + mc->gtt_base_align) & ~mc->gtt_base_align;
623 }
624 mc->gtt_end = mc->gtt_start + mc->gtt_size - 1;
625 dev_info(adev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
626 mc->gtt_size >> 20, mc->gtt_start, mc->gtt_end);
627}
628
629/*
630 * GPU helpers function.
631 */
632/**
633 * amdgpu_card_posted - check if the hw has already been initialized
634 *
635 * @adev: amdgpu_device pointer
636 *
637 * Check if the asic has been initialized (all asics).
638 * Used at driver startup.
639 * Returns true if initialized or false if not.
640 */
641bool amdgpu_card_posted(struct amdgpu_device *adev)
642{
643 uint32_t reg;
644
645 /* then check MEM_SIZE, in case the crtcs are off */
646 reg = RREG32(mmCONFIG_MEMSIZE);
647
648 if (reg)
649 return true;
650
651 return false;
652
653}
654
Monk Liubec86372016-09-14 19:38:08 +0800655static bool amdgpu_vpost_needed(struct amdgpu_device *adev)
656{
657 if (amdgpu_sriov_vf(adev))
658 return false;
659
660 if (amdgpu_passthrough(adev)) {
661 /* for FIJI: In whole GPU pass-through virtualization case
662 * old smc fw won't clear some registers (e.g. MEM_SIZE, BIOS_SCRATCH)
663 * so amdgpu_card_posted return false and driver will incorrectly skip vPost.
664 * but if we force vPost do in pass-through case, the driver reload will hang.
665 * whether doing vPost depends on amdgpu_card_posted if smc version is above
666 * 00160e00 for FIJI.
667 */
668 if (adev->asic_type == CHIP_FIJI) {
669 int err;
670 uint32_t fw_ver;
671 err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
672 /* force vPost if error occured */
673 if (err)
674 return true;
675
676 fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
677 if (fw_ver >= 0x00160e00)
678 return !amdgpu_card_posted(adev);
679 }
680 } else {
681 /* in bare-metal case, amdgpu_card_posted return false
682 * after system reboot/boot, and return true if driver
683 * reloaded.
684 * we shouldn't do vPost after driver reload otherwise GPU
685 * could hang.
686 */
687 if (amdgpu_card_posted(adev))
688 return false;
689 }
690
691 /* we assume vPost is neede for all other cases */
692 return true;
693}
694
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400695/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400696 * amdgpu_dummy_page_init - init dummy page used by the driver
697 *
698 * @adev: amdgpu_device pointer
699 *
700 * Allocate the dummy page used by the driver (all asics).
701 * This dummy page is used by the driver as a filler for gart entries
702 * when pages are taken out of the GART
703 * Returns 0 on sucess, -ENOMEM on failure.
704 */
705int amdgpu_dummy_page_init(struct amdgpu_device *adev)
706{
707 if (adev->dummy_page.page)
708 return 0;
709 adev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
710 if (adev->dummy_page.page == NULL)
711 return -ENOMEM;
712 adev->dummy_page.addr = pci_map_page(adev->pdev, adev->dummy_page.page,
713 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
714 if (pci_dma_mapping_error(adev->pdev, adev->dummy_page.addr)) {
715 dev_err(&adev->pdev->dev, "Failed to DMA MAP the dummy page\n");
716 __free_page(adev->dummy_page.page);
717 adev->dummy_page.page = NULL;
718 return -ENOMEM;
719 }
720 return 0;
721}
722
723/**
724 * amdgpu_dummy_page_fini - free dummy page used by the driver
725 *
726 * @adev: amdgpu_device pointer
727 *
728 * Frees the dummy page used by the driver (all asics).
729 */
730void amdgpu_dummy_page_fini(struct amdgpu_device *adev)
731{
732 if (adev->dummy_page.page == NULL)
733 return;
734 pci_unmap_page(adev->pdev, adev->dummy_page.addr,
735 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
736 __free_page(adev->dummy_page.page);
737 adev->dummy_page.page = NULL;
738}
739
740
741/* ATOM accessor methods */
742/*
743 * ATOM is an interpreted byte code stored in tables in the vbios. The
744 * driver registers callbacks to access registers and the interpreter
745 * in the driver parses the tables and executes then to program specific
746 * actions (set display modes, asic init, etc.). See amdgpu_atombios.c,
747 * atombios.h, and atom.c
748 */
749
750/**
751 * cail_pll_read - read PLL register
752 *
753 * @info: atom card_info pointer
754 * @reg: PLL register offset
755 *
756 * Provides a PLL register accessor for the atom interpreter (r4xx+).
757 * Returns the value of the PLL register.
758 */
759static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
760{
761 return 0;
762}
763
764/**
765 * cail_pll_write - write PLL register
766 *
767 * @info: atom card_info pointer
768 * @reg: PLL register offset
769 * @val: value to write to the pll register
770 *
771 * Provides a PLL register accessor for the atom interpreter (r4xx+).
772 */
773static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
774{
775
776}
777
778/**
779 * cail_mc_read - read MC (Memory Controller) register
780 *
781 * @info: atom card_info pointer
782 * @reg: MC register offset
783 *
784 * Provides an MC register accessor for the atom interpreter (r4xx+).
785 * Returns the value of the MC register.
786 */
787static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
788{
789 return 0;
790}
791
792/**
793 * cail_mc_write - write MC (Memory Controller) register
794 *
795 * @info: atom card_info pointer
796 * @reg: MC register offset
797 * @val: value to write to the pll register
798 *
799 * Provides a MC register accessor for the atom interpreter (r4xx+).
800 */
801static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
802{
803
804}
805
806/**
807 * cail_reg_write - write MMIO register
808 *
809 * @info: atom card_info pointer
810 * @reg: MMIO register offset
811 * @val: value to write to the pll register
812 *
813 * Provides a MMIO register accessor for the atom interpreter (r4xx+).
814 */
815static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
816{
817 struct amdgpu_device *adev = info->dev->dev_private;
818
819 WREG32(reg, val);
820}
821
822/**
823 * cail_reg_read - read MMIO register
824 *
825 * @info: atom card_info pointer
826 * @reg: MMIO register offset
827 *
828 * Provides an MMIO register accessor for the atom interpreter (r4xx+).
829 * Returns the value of the MMIO register.
830 */
831static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
832{
833 struct amdgpu_device *adev = info->dev->dev_private;
834 uint32_t r;
835
836 r = RREG32(reg);
837 return r;
838}
839
840/**
841 * cail_ioreg_write - write IO register
842 *
843 * @info: atom card_info pointer
844 * @reg: IO register offset
845 * @val: value to write to the pll register
846 *
847 * Provides a IO register accessor for the atom interpreter (r4xx+).
848 */
849static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
850{
851 struct amdgpu_device *adev = info->dev->dev_private;
852
853 WREG32_IO(reg, val);
854}
855
856/**
857 * cail_ioreg_read - read IO register
858 *
859 * @info: atom card_info pointer
860 * @reg: IO register offset
861 *
862 * Provides an IO register accessor for the atom interpreter (r4xx+).
863 * Returns the value of the IO register.
864 */
865static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
866{
867 struct amdgpu_device *adev = info->dev->dev_private;
868 uint32_t r;
869
870 r = RREG32_IO(reg);
871 return r;
872}
873
874/**
875 * amdgpu_atombios_fini - free the driver info and callbacks for atombios
876 *
877 * @adev: amdgpu_device pointer
878 *
879 * Frees the driver info and register access callbacks for the ATOM
880 * interpreter (r4xx+).
881 * Called at driver shutdown.
882 */
883static void amdgpu_atombios_fini(struct amdgpu_device *adev)
884{
Monk Liu89e0ec92016-05-27 19:34:11 +0800885 if (adev->mode_info.atom_context) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400886 kfree(adev->mode_info.atom_context->scratch);
Monk Liu89e0ec92016-05-27 19:34:11 +0800887 kfree(adev->mode_info.atom_context->iio);
888 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400889 kfree(adev->mode_info.atom_context);
890 adev->mode_info.atom_context = NULL;
891 kfree(adev->mode_info.atom_card_info);
892 adev->mode_info.atom_card_info = NULL;
893}
894
895/**
896 * amdgpu_atombios_init - init the driver info and callbacks for atombios
897 *
898 * @adev: amdgpu_device pointer
899 *
900 * Initializes the driver info and register access callbacks for the
901 * ATOM interpreter (r4xx+).
902 * Returns 0 on sucess, -ENOMEM on failure.
903 * Called at driver startup.
904 */
905static int amdgpu_atombios_init(struct amdgpu_device *adev)
906{
907 struct card_info *atom_card_info =
908 kzalloc(sizeof(struct card_info), GFP_KERNEL);
909
910 if (!atom_card_info)
911 return -ENOMEM;
912
913 adev->mode_info.atom_card_info = atom_card_info;
914 atom_card_info->dev = adev->ddev;
915 atom_card_info->reg_read = cail_reg_read;
916 atom_card_info->reg_write = cail_reg_write;
917 /* needed for iio ops */
918 if (adev->rio_mem) {
919 atom_card_info->ioreg_read = cail_ioreg_read;
920 atom_card_info->ioreg_write = cail_ioreg_write;
921 } else {
922 DRM_ERROR("Unable to find PCI I/O BAR; using MMIO for ATOM IIO\n");
923 atom_card_info->ioreg_read = cail_reg_read;
924 atom_card_info->ioreg_write = cail_reg_write;
925 }
926 atom_card_info->mc_read = cail_mc_read;
927 atom_card_info->mc_write = cail_mc_write;
928 atom_card_info->pll_read = cail_pll_read;
929 atom_card_info->pll_write = cail_pll_write;
930
931 adev->mode_info.atom_context = amdgpu_atom_parse(atom_card_info, adev->bios);
932 if (!adev->mode_info.atom_context) {
933 amdgpu_atombios_fini(adev);
934 return -ENOMEM;
935 }
936
937 mutex_init(&adev->mode_info.atom_context->mutex);
938 amdgpu_atombios_scratch_regs_init(adev);
939 amdgpu_atom_allocate_fb_scratch(adev->mode_info.atom_context);
940 return 0;
941}
942
943/* if we get transitioned to only one device, take VGA back */
944/**
945 * amdgpu_vga_set_decode - enable/disable vga decode
946 *
947 * @cookie: amdgpu_device pointer
948 * @state: enable/disable vga decode
949 *
950 * Enable/disable vga decode (all asics).
951 * Returns VGA resource flags.
952 */
953static unsigned int amdgpu_vga_set_decode(void *cookie, bool state)
954{
955 struct amdgpu_device *adev = cookie;
956 amdgpu_asic_set_vga_state(adev, state);
957 if (state)
958 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
959 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
960 else
961 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
962}
963
964/**
965 * amdgpu_check_pot_argument - check that argument is a power of two
966 *
967 * @arg: value to check
968 *
969 * Validates that a certain argument is a power of two (all asics).
970 * Returns true if argument is valid.
971 */
972static bool amdgpu_check_pot_argument(int arg)
973{
974 return (arg & (arg - 1)) == 0;
975}
976
977/**
978 * amdgpu_check_arguments - validate module params
979 *
980 * @adev: amdgpu_device pointer
981 *
982 * Validates certain module parameters and updates
983 * the associated values used by the driver (all asics).
984 */
985static void amdgpu_check_arguments(struct amdgpu_device *adev)
986{
Chunming Zhou5b011232015-12-10 17:34:33 +0800987 if (amdgpu_sched_jobs < 4) {
988 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
989 amdgpu_sched_jobs);
990 amdgpu_sched_jobs = 4;
991 } else if (!amdgpu_check_pot_argument(amdgpu_sched_jobs)){
992 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
993 amdgpu_sched_jobs);
994 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
995 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400996
997 if (amdgpu_gart_size != -1) {
Christian Königc4e1a132016-03-17 16:25:15 +0100998 /* gtt size must be greater or equal to 32M */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400999 if (amdgpu_gart_size < 32) {
1000 dev_warn(adev->dev, "gart size (%d) too small\n",
1001 amdgpu_gart_size);
1002 amdgpu_gart_size = -1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001003 }
1004 }
1005
1006 if (!amdgpu_check_pot_argument(amdgpu_vm_size)) {
1007 dev_warn(adev->dev, "VM size (%d) must be a power of 2\n",
1008 amdgpu_vm_size);
Alex Deucher8dacc122015-05-11 16:20:58 -04001009 amdgpu_vm_size = 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001010 }
1011
1012 if (amdgpu_vm_size < 1) {
1013 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
1014 amdgpu_vm_size);
Alex Deucher8dacc122015-05-11 16:20:58 -04001015 amdgpu_vm_size = 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001016 }
1017
1018 /*
1019 * Max GPUVM size for Cayman, SI and CI are 40 bits.
1020 */
1021 if (amdgpu_vm_size > 1024) {
1022 dev_warn(adev->dev, "VM size (%d) too large, max is 1TB\n",
1023 amdgpu_vm_size);
Alex Deucher8dacc122015-05-11 16:20:58 -04001024 amdgpu_vm_size = 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001025 }
1026
1027 /* defines number of bits in page table versus page directory,
1028 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
1029 * page table and the remaining bits are in the page directory */
1030 if (amdgpu_vm_block_size == -1) {
1031
1032 /* Total bits covered by PD + PTs */
1033 unsigned bits = ilog2(amdgpu_vm_size) + 18;
1034
1035 /* Make sure the PD is 4K in size up to 8GB address space.
1036 Above that split equal between PD and PTs */
1037 if (amdgpu_vm_size <= 8)
1038 amdgpu_vm_block_size = bits - 9;
1039 else
1040 amdgpu_vm_block_size = (bits + 3) / 2;
1041
1042 } else if (amdgpu_vm_block_size < 9) {
1043 dev_warn(adev->dev, "VM page table size (%d) too small\n",
1044 amdgpu_vm_block_size);
1045 amdgpu_vm_block_size = 9;
1046 }
1047
1048 if (amdgpu_vm_block_size > 24 ||
1049 (amdgpu_vm_size * 1024) < (1ull << amdgpu_vm_block_size)) {
1050 dev_warn(adev->dev, "VM page table size (%d) too large\n",
1051 amdgpu_vm_block_size);
1052 amdgpu_vm_block_size = 9;
1053 }
1054}
1055
1056/**
1057 * amdgpu_switcheroo_set_state - set switcheroo state
1058 *
1059 * @pdev: pci dev pointer
Lukas Wunner16944672015-09-05 11:17:35 +02001060 * @state: vga_switcheroo state
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001061 *
1062 * Callback for the switcheroo driver. Suspends or resumes the
1063 * the asics before or after it is powered up using ACPI methods.
1064 */
1065static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1066{
1067 struct drm_device *dev = pci_get_drvdata(pdev);
1068
1069 if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
1070 return;
1071
1072 if (state == VGA_SWITCHEROO_ON) {
1073 unsigned d3_delay = dev->pdev->d3_delay;
1074
1075 printk(KERN_INFO "amdgpu: switched on\n");
1076 /* don't suspend or resume card normally */
1077 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1078
Alex Deucher810ddc32016-08-23 13:25:49 -04001079 amdgpu_device_resume(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001080
1081 dev->pdev->d3_delay = d3_delay;
1082
1083 dev->switch_power_state = DRM_SWITCH_POWER_ON;
1084 drm_kms_helper_poll_enable(dev);
1085 } else {
1086 printk(KERN_INFO "amdgpu: switched off\n");
1087 drm_kms_helper_poll_disable(dev);
1088 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Alex Deucher810ddc32016-08-23 13:25:49 -04001089 amdgpu_device_suspend(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001090 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1091 }
1092}
1093
1094/**
1095 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1096 *
1097 * @pdev: pci dev pointer
1098 *
1099 * Callback for the switcheroo driver. Check of the switcheroo
1100 * state can be changed.
1101 * Returns true if the state can be changed, false if not.
1102 */
1103static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1104{
1105 struct drm_device *dev = pci_get_drvdata(pdev);
1106
1107 /*
1108 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1109 * locking inversion with the driver load path. And the access here is
1110 * completely racy anyway. So don't bother with locking for now.
1111 */
1112 return dev->open_count == 0;
1113}
1114
1115static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1116 .set_gpu_state = amdgpu_switcheroo_set_state,
1117 .reprobe = NULL,
1118 .can_switch = amdgpu_switcheroo_can_switch,
1119};
1120
1121int amdgpu_set_clockgating_state(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001122 enum amd_ip_block_type block_type,
1123 enum amd_clockgating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001124{
1125 int i, r = 0;
1126
1127 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001128 if (!adev->ip_block_status[i].valid)
1129 continue;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001130 if (adev->ip_blocks[i].type == block_type) {
yanyang15fc3aee2015-05-22 14:39:35 -04001131 r = adev->ip_blocks[i].funcs->set_clockgating_state((void *)adev,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001132 state);
1133 if (r)
1134 return r;
Alex Deuchera225bf12016-06-23 11:48:30 -04001135 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001136 }
1137 }
1138 return r;
1139}
1140
1141int amdgpu_set_powergating_state(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001142 enum amd_ip_block_type block_type,
1143 enum amd_powergating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001144{
1145 int i, r = 0;
1146
1147 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001148 if (!adev->ip_block_status[i].valid)
1149 continue;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001150 if (adev->ip_blocks[i].type == block_type) {
yanyang15fc3aee2015-05-22 14:39:35 -04001151 r = adev->ip_blocks[i].funcs->set_powergating_state((void *)adev,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001152 state);
1153 if (r)
1154 return r;
Alex Deuchera225bf12016-06-23 11:48:30 -04001155 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001156 }
1157 }
1158 return r;
1159}
1160
Alex Deucher5dbbb602016-06-23 11:41:04 -04001161int amdgpu_wait_for_idle(struct amdgpu_device *adev,
1162 enum amd_ip_block_type block_type)
1163{
1164 int i, r;
1165
1166 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001167 if (!adev->ip_block_status[i].valid)
1168 continue;
Alex Deucher5dbbb602016-06-23 11:41:04 -04001169 if (adev->ip_blocks[i].type == block_type) {
1170 r = adev->ip_blocks[i].funcs->wait_for_idle((void *)adev);
1171 if (r)
1172 return r;
1173 break;
1174 }
1175 }
1176 return 0;
1177
1178}
1179
1180bool amdgpu_is_idle(struct amdgpu_device *adev,
1181 enum amd_ip_block_type block_type)
1182{
1183 int i;
1184
1185 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001186 if (!adev->ip_block_status[i].valid)
1187 continue;
Alex Deucher5dbbb602016-06-23 11:41:04 -04001188 if (adev->ip_blocks[i].type == block_type)
1189 return adev->ip_blocks[i].funcs->is_idle((void *)adev);
1190 }
1191 return true;
1192
1193}
1194
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001195const struct amdgpu_ip_block_version * amdgpu_get_ip_block(
1196 struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001197 enum amd_ip_block_type type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001198{
1199 int i;
1200
1201 for (i = 0; i < adev->num_ip_blocks; i++)
1202 if (adev->ip_blocks[i].type == type)
1203 return &adev->ip_blocks[i];
1204
1205 return NULL;
1206}
1207
1208/**
1209 * amdgpu_ip_block_version_cmp
1210 *
1211 * @adev: amdgpu_device pointer
yanyang15fc3aee2015-05-22 14:39:35 -04001212 * @type: enum amd_ip_block_type
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001213 * @major: major version
1214 * @minor: minor version
1215 *
1216 * return 0 if equal or greater
1217 * return 1 if smaller or the ip_block doesn't exist
1218 */
1219int amdgpu_ip_block_version_cmp(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001220 enum amd_ip_block_type type,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001221 u32 major, u32 minor)
1222{
1223 const struct amdgpu_ip_block_version *ip_block;
1224 ip_block = amdgpu_get_ip_block(adev, type);
1225
1226 if (ip_block && ((ip_block->major > major) ||
1227 ((ip_block->major == major) &&
1228 (ip_block->minor >= minor))))
1229 return 0;
1230
1231 return 1;
1232}
1233
Alex Deucher483ef982016-09-30 12:43:04 -04001234static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
Emily Deng9accf2f2016-08-10 16:01:25 +08001235{
1236 adev->enable_virtual_display = false;
1237
1238 if (amdgpu_virtual_display) {
1239 struct drm_device *ddev = adev->ddev;
1240 const char *pci_address_name = pci_name(ddev->pdev);
Emily Deng0f663562016-09-30 13:02:18 -04001241 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
Emily Deng9accf2f2016-08-10 16:01:25 +08001242
1243 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1244 pciaddstr_tmp = pciaddstr;
Emily Deng0f663562016-09-30 13:02:18 -04001245 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1246 pciaddname = strsep(&pciaddname_tmp, ",");
Emily Deng9accf2f2016-08-10 16:01:25 +08001247 if (!strcmp(pci_address_name, pciaddname)) {
Emily Deng0f663562016-09-30 13:02:18 -04001248 long num_crtc;
1249 int res = -1;
1250
Emily Deng9accf2f2016-08-10 16:01:25 +08001251 adev->enable_virtual_display = true;
Emily Deng0f663562016-09-30 13:02:18 -04001252
1253 if (pciaddname_tmp)
1254 res = kstrtol(pciaddname_tmp, 10,
1255 &num_crtc);
1256
1257 if (!res) {
1258 if (num_crtc < 1)
1259 num_crtc = 1;
1260 if (num_crtc > 6)
1261 num_crtc = 6;
1262 adev->mode_info.num_crtc = num_crtc;
1263 } else {
1264 adev->mode_info.num_crtc = 1;
1265 }
Emily Deng9accf2f2016-08-10 16:01:25 +08001266 break;
1267 }
1268 }
1269
Emily Deng0f663562016-09-30 13:02:18 -04001270 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
1271 amdgpu_virtual_display, pci_address_name,
1272 adev->enable_virtual_display, adev->mode_info.num_crtc);
Emily Deng9accf2f2016-08-10 16:01:25 +08001273
1274 kfree(pciaddstr);
1275 }
1276}
1277
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001278static int amdgpu_early_init(struct amdgpu_device *adev)
1279{
Alex Deucheraaa36a92015-04-20 17:31:14 -04001280 int i, r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001281
Alex Deucher483ef982016-09-30 12:43:04 -04001282 amdgpu_device_enable_virtual_display(adev);
Emily Denga6be7572016-08-08 11:37:50 +08001283
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001284 switch (adev->asic_type) {
Alex Deucheraaa36a92015-04-20 17:31:14 -04001285 case CHIP_TOPAZ:
1286 case CHIP_TONGA:
David Zhang48299f92015-07-08 01:05:16 +08001287 case CHIP_FIJI:
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001288 case CHIP_POLARIS11:
1289 case CHIP_POLARIS10:
Alex Deucheraaa36a92015-04-20 17:31:14 -04001290 case CHIP_CARRIZO:
Samuel Li39bb0c92015-10-08 16:31:43 -04001291 case CHIP_STONEY:
1292 if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
Alex Deucheraaa36a92015-04-20 17:31:14 -04001293 adev->family = AMDGPU_FAMILY_CZ;
1294 else
1295 adev->family = AMDGPU_FAMILY_VI;
1296
1297 r = vi_set_ip_blocks(adev);
1298 if (r)
1299 return r;
1300 break;
Ken Wang33f34802016-01-21 17:29:41 +08001301#ifdef CONFIG_DRM_AMDGPU_SI
1302 case CHIP_VERDE:
1303 case CHIP_TAHITI:
1304 case CHIP_PITCAIRN:
1305 case CHIP_OLAND:
1306 case CHIP_HAINAN:
Ken Wang295d0da2016-05-24 21:02:53 +08001307 adev->family = AMDGPU_FAMILY_SI;
Ken Wang33f34802016-01-21 17:29:41 +08001308 r = si_set_ip_blocks(adev);
1309 if (r)
1310 return r;
1311 break;
1312#endif
Alex Deuchera2e73f52015-04-20 17:09:27 -04001313#ifdef CONFIG_DRM_AMDGPU_CIK
1314 case CHIP_BONAIRE:
1315 case CHIP_HAWAII:
1316 case CHIP_KAVERI:
1317 case CHIP_KABINI:
1318 case CHIP_MULLINS:
1319 if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
1320 adev->family = AMDGPU_FAMILY_CI;
1321 else
1322 adev->family = AMDGPU_FAMILY_KV;
1323
1324 r = cik_set_ip_blocks(adev);
1325 if (r)
1326 return r;
1327 break;
1328#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001329 default:
1330 /* FIXME: not supported yet */
1331 return -EINVAL;
1332 }
1333
Alex Deucher8faf0e082015-07-28 11:50:31 -04001334 adev->ip_block_status = kcalloc(adev->num_ip_blocks,
1335 sizeof(struct amdgpu_ip_block_status), GFP_KERNEL);
1336 if (adev->ip_block_status == NULL)
Alex Deucherd8d090b2015-06-26 13:02:57 -04001337 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001338
1339 if (adev->ip_blocks == NULL) {
1340 DRM_ERROR("No IP blocks found!\n");
1341 return r;
1342 }
1343
1344 for (i = 0; i < adev->num_ip_blocks; i++) {
1345 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
1346 DRM_ERROR("disabled ip block: %d\n", i);
Alex Deucher8faf0e082015-07-28 11:50:31 -04001347 adev->ip_block_status[i].valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001348 } else {
1349 if (adev->ip_blocks[i].funcs->early_init) {
yanyang15fc3aee2015-05-22 14:39:35 -04001350 r = adev->ip_blocks[i].funcs->early_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001351 if (r == -ENOENT) {
Alex Deucher8faf0e082015-07-28 11:50:31 -04001352 adev->ip_block_status[i].valid = false;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001353 } else if (r) {
Tom St Denis88a907d2016-05-04 14:28:35 -04001354 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 -04001355 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001356 } else {
Alex Deucher8faf0e082015-07-28 11:50:31 -04001357 adev->ip_block_status[i].valid = true;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001358 }
Alex Deucher974e6b62015-07-10 13:59:44 -04001359 } else {
Alex Deucher8faf0e082015-07-28 11:50:31 -04001360 adev->ip_block_status[i].valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001361 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001362 }
1363 }
1364
Nicolai Hähnle395d1fb2016-06-02 12:32:07 +02001365 adev->cg_flags &= amdgpu_cg_mask;
1366 adev->pg_flags &= amdgpu_pg_mask;
1367
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001368 return 0;
1369}
1370
1371static int amdgpu_init(struct amdgpu_device *adev)
1372{
1373 int i, r;
1374
1375 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deucher8faf0e082015-07-28 11:50:31 -04001376 if (!adev->ip_block_status[i].valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001377 continue;
yanyang15fc3aee2015-05-22 14:39:35 -04001378 r = adev->ip_blocks[i].funcs->sw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001379 if (r) {
Tom St Denis822b2ce2016-05-05 10:23:40 -04001380 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 -04001381 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001382 }
Alex Deucher8faf0e082015-07-28 11:50:31 -04001383 adev->ip_block_status[i].sw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001384 /* need to do gmc hw init early so we can allocate gpu mem */
yanyang15fc3aee2015-05-22 14:39:35 -04001385 if (adev->ip_blocks[i].type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001386 r = amdgpu_vram_scratch_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001387 if (r) {
1388 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001389 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001390 }
yanyang15fc3aee2015-05-22 14:39:35 -04001391 r = adev->ip_blocks[i].funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001392 if (r) {
1393 DRM_ERROR("hw_init %d failed %d\n", i, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001394 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001395 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001396 r = amdgpu_wb_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001397 if (r) {
1398 DRM_ERROR("amdgpu_wb_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001399 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001400 }
Alex Deucher8faf0e082015-07-28 11:50:31 -04001401 adev->ip_block_status[i].hw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001402 }
1403 }
1404
1405 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deucher8faf0e082015-07-28 11:50:31 -04001406 if (!adev->ip_block_status[i].sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001407 continue;
1408 /* gmc hw init is done early */
yanyang15fc3aee2015-05-22 14:39:35 -04001409 if (adev->ip_blocks[i].type == AMD_IP_BLOCK_TYPE_GMC)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001410 continue;
yanyang15fc3aee2015-05-22 14:39:35 -04001411 r = adev->ip_blocks[i].funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001412 if (r) {
Tom St Denis822b2ce2016-05-05 10:23:40 -04001413 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 -04001414 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001415 }
Alex Deucher8faf0e082015-07-28 11:50:31 -04001416 adev->ip_block_status[i].hw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001417 }
1418
1419 return 0;
1420}
1421
1422static int amdgpu_late_init(struct amdgpu_device *adev)
1423{
1424 int i = 0, r;
1425
1426 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deucher8faf0e082015-07-28 11:50:31 -04001427 if (!adev->ip_block_status[i].valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001428 continue;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001429 if (adev->ip_blocks[i].funcs->late_init) {
yanyang15fc3aee2015-05-22 14:39:35 -04001430 r = adev->ip_blocks[i].funcs->late_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001431 if (r) {
Tom St Denis822b2ce2016-05-05 10:23:40 -04001432 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 -04001433 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001434 }
Grazvydas Ignotas8a2eef12016-10-03 00:06:44 +03001435 adev->ip_block_status[i].late_initialized = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001436 }
Alex Deucher4a446d52016-10-07 14:48:18 -04001437 /* skip CG for VCE/UVD, it's handled specially */
1438 if (adev->ip_blocks[i].type != AMD_IP_BLOCK_TYPE_UVD &&
1439 adev->ip_blocks[i].type != AMD_IP_BLOCK_TYPE_VCE) {
1440 /* enable clockgating to save power */
1441 r = adev->ip_blocks[i].funcs->set_clockgating_state((void *)adev,
1442 AMD_CG_STATE_GATE);
1443 if (r) {
1444 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
1445 adev->ip_blocks[i].funcs->name, r);
1446 return r;
1447 }
Arindam Nathb0b00ff2016-10-07 19:01:37 +05301448 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001449 }
1450
1451 return 0;
1452}
1453
1454static int amdgpu_fini(struct amdgpu_device *adev)
1455{
1456 int i, r;
1457
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001458 /* need to disable SMC first */
1459 for (i = 0; i < adev->num_ip_blocks; i++) {
1460 if (!adev->ip_block_status[i].hw)
1461 continue;
1462 if (adev->ip_blocks[i].type == AMD_IP_BLOCK_TYPE_SMC) {
1463 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
1464 r = adev->ip_blocks[i].funcs->set_clockgating_state((void *)adev,
1465 AMD_CG_STATE_UNGATE);
1466 if (r) {
1467 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1468 adev->ip_blocks[i].funcs->name, r);
1469 return r;
1470 }
1471 r = adev->ip_blocks[i].funcs->hw_fini((void *)adev);
1472 /* XXX handle errors */
1473 if (r) {
1474 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
1475 adev->ip_blocks[i].funcs->name, r);
1476 }
1477 adev->ip_block_status[i].hw = false;
1478 break;
1479 }
1480 }
1481
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001482 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deucher8faf0e082015-07-28 11:50:31 -04001483 if (!adev->ip_block_status[i].hw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001484 continue;
yanyang15fc3aee2015-05-22 14:39:35 -04001485 if (adev->ip_blocks[i].type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001486 amdgpu_wb_fini(adev);
1487 amdgpu_vram_scratch_fini(adev);
1488 }
1489 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
yanyang15fc3aee2015-05-22 14:39:35 -04001490 r = adev->ip_blocks[i].funcs->set_clockgating_state((void *)adev,
1491 AMD_CG_STATE_UNGATE);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001492 if (r) {
Tom St Denis822b2ce2016-05-05 10:23:40 -04001493 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 -04001494 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001495 }
yanyang15fc3aee2015-05-22 14:39:35 -04001496 r = adev->ip_blocks[i].funcs->hw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001497 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001498 if (r) {
Tom St Denis822b2ce2016-05-05 10:23:40 -04001499 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 -05001500 }
Alex Deucher8faf0e082015-07-28 11:50:31 -04001501 adev->ip_block_status[i].hw = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001502 }
1503
1504 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deucher8faf0e082015-07-28 11:50:31 -04001505 if (!adev->ip_block_status[i].sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001506 continue;
yanyang15fc3aee2015-05-22 14:39:35 -04001507 r = adev->ip_blocks[i].funcs->sw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001508 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001509 if (r) {
Tom St Denis822b2ce2016-05-05 10:23:40 -04001510 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 -05001511 }
Alex Deucher8faf0e082015-07-28 11:50:31 -04001512 adev->ip_block_status[i].sw = false;
1513 adev->ip_block_status[i].valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001514 }
1515
Monk Liua6dcfd92016-05-19 14:36:34 +08001516 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Grazvydas Ignotas8a2eef12016-10-03 00:06:44 +03001517 if (!adev->ip_block_status[i].late_initialized)
1518 continue;
Monk Liua6dcfd92016-05-19 14:36:34 +08001519 if (adev->ip_blocks[i].funcs->late_fini)
1520 adev->ip_blocks[i].funcs->late_fini((void *)adev);
Grazvydas Ignotas8a2eef12016-10-03 00:06:44 +03001521 adev->ip_block_status[i].late_initialized = false;
Monk Liua6dcfd92016-05-19 14:36:34 +08001522 }
1523
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001524 return 0;
1525}
1526
1527static int amdgpu_suspend(struct amdgpu_device *adev)
1528{
1529 int i, r;
1530
Flora Cuic5a93a22016-02-26 10:45:25 +08001531 /* ungate SMC block first */
1532 r = amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC,
1533 AMD_CG_STATE_UNGATE);
1534 if (r) {
1535 DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n",r);
1536 }
1537
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001538 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deucher8faf0e082015-07-28 11:50:31 -04001539 if (!adev->ip_block_status[i].valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001540 continue;
1541 /* ungate blocks so that suspend can properly shut them down */
Flora Cuic5a93a22016-02-26 10:45:25 +08001542 if (i != AMD_IP_BLOCK_TYPE_SMC) {
1543 r = adev->ip_blocks[i].funcs->set_clockgating_state((void *)adev,
1544 AMD_CG_STATE_UNGATE);
1545 if (r) {
Tom St Denis822b2ce2016-05-05 10:23:40 -04001546 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 +08001547 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001548 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001549 /* XXX handle errors */
1550 r = adev->ip_blocks[i].funcs->suspend(adev);
1551 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001552 if (r) {
Tom St Denis822b2ce2016-05-05 10:23:40 -04001553 DRM_ERROR("suspend of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001554 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001555 }
1556
1557 return 0;
1558}
1559
1560static int amdgpu_resume(struct amdgpu_device *adev)
1561{
1562 int i, r;
1563
1564 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deucher8faf0e082015-07-28 11:50:31 -04001565 if (!adev->ip_block_status[i].valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001566 continue;
1567 r = adev->ip_blocks[i].funcs->resume(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001568 if (r) {
Tom St Denis822b2ce2016-05-05 10:23:40 -04001569 DRM_ERROR("resume of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001570 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001571 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001572 }
1573
1574 return 0;
1575}
1576
Monk Liu4e99a442016-03-31 13:26:59 +08001577static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
Andres Rodriguez048765a2016-06-11 02:51:32 -04001578{
Monk Liu4e99a442016-03-31 13:26:59 +08001579 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
1580 adev->virtualization.virtual_caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
Andres Rodriguez048765a2016-06-11 02:51:32 -04001581}
1582
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001583/**
1584 * amdgpu_device_init - initialize the driver
1585 *
1586 * @adev: amdgpu_device pointer
1587 * @pdev: drm dev pointer
1588 * @pdev: pci dev pointer
1589 * @flags: driver flags
1590 *
1591 * Initializes the driver info and hw (all asics).
1592 * Returns 0 for success or an error on failure.
1593 * Called at driver startup.
1594 */
1595int amdgpu_device_init(struct amdgpu_device *adev,
1596 struct drm_device *ddev,
1597 struct pci_dev *pdev,
1598 uint32_t flags)
1599{
1600 int r, i;
1601 bool runtime = false;
Marek Olšák95844d22016-08-17 23:49:27 +02001602 u32 max_MBps;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001603
1604 adev->shutdown = false;
1605 adev->dev = &pdev->dev;
1606 adev->ddev = ddev;
1607 adev->pdev = pdev;
1608 adev->flags = flags;
Jammy Zhou2f7d10b2015-07-22 11:29:01 +08001609 adev->asic_type = flags & AMD_ASIC_MASK;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001610 adev->is_atom_bios = false;
1611 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
1612 adev->mc.gtt_size = 512 * 1024 * 1024;
1613 adev->accel_working = false;
1614 adev->num_rings = 0;
1615 adev->mman.buffer_funcs = NULL;
1616 adev->mman.buffer_funcs_ring = NULL;
1617 adev->vm_manager.vm_pte_funcs = NULL;
Christian König2d55e452016-02-08 17:37:38 +01001618 adev->vm_manager.vm_pte_num_rings = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001619 adev->gart.gart_funcs = NULL;
1620 adev->fence_context = fence_context_alloc(AMDGPU_MAX_RINGS);
1621
1622 adev->smc_rreg = &amdgpu_invalid_rreg;
1623 adev->smc_wreg = &amdgpu_invalid_wreg;
1624 adev->pcie_rreg = &amdgpu_invalid_rreg;
1625 adev->pcie_wreg = &amdgpu_invalid_wreg;
Huang Rui36b9a952016-08-31 13:23:25 +08001626 adev->pciep_rreg = &amdgpu_invalid_rreg;
1627 adev->pciep_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001628 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
1629 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
1630 adev->didt_rreg = &amdgpu_invalid_rreg;
1631 adev->didt_wreg = &amdgpu_invalid_wreg;
Rex Zhuccdbb202016-06-08 12:47:41 +08001632 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
1633 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001634 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
1635 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
1636
Rex Zhuccdbb202016-06-08 12:47:41 +08001637
Alex Deucher3e39ab92015-06-05 15:04:33 -04001638 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
1639 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
1640 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001641
1642 /* mutex initialization are all done here so we
1643 * can recall function without having locking issues */
Christian König8d0a7ce2015-11-03 20:58:50 +01001644 mutex_init(&adev->vm_manager.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001645 atomic_set(&adev->irq.ih.lock, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001646 mutex_init(&adev->pm.mutex);
1647 mutex_init(&adev->gfx.gpu_clock_mutex);
1648 mutex_init(&adev->srbm_mutex);
1649 mutex_init(&adev->grbm_idx_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001650 mutex_init(&adev->mn_lock);
1651 hash_init(adev->mn_hash);
1652
1653 amdgpu_check_arguments(adev);
1654
1655 /* Registers mapping */
1656 /* TODO: block userspace mapping of io register */
1657 spin_lock_init(&adev->mmio_idx_lock);
1658 spin_lock_init(&adev->smc_idx_lock);
1659 spin_lock_init(&adev->pcie_idx_lock);
1660 spin_lock_init(&adev->uvd_ctx_idx_lock);
1661 spin_lock_init(&adev->didt_idx_lock);
Rex Zhuccdbb202016-06-08 12:47:41 +08001662 spin_lock_init(&adev->gc_cac_idx_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001663 spin_lock_init(&adev->audio_endpt_idx_lock);
Marek Olšák95844d22016-08-17 23:49:27 +02001664 spin_lock_init(&adev->mm_stats.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001665
Chunming Zhou0c4e7fa2016-08-17 11:41:30 +08001666 INIT_LIST_HEAD(&adev->shadow_list);
1667 mutex_init(&adev->shadow_list_lock);
1668
Chunming Zhou5c1354b2016-08-30 16:13:10 +08001669 INIT_LIST_HEAD(&adev->gtt_list);
1670 spin_lock_init(&adev->gtt_list_lock);
1671
Ken Wangda69c1612016-01-21 19:08:55 +08001672 if (adev->asic_type >= CHIP_BONAIRE) {
1673 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
1674 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
1675 } else {
1676 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
1677 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
1678 }
Chunming Zhou5c1354b2016-08-30 16:13:10 +08001679
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001680 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
1681 if (adev->rmmio == NULL) {
1682 return -ENOMEM;
1683 }
1684 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
1685 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
1686
Ken Wangda69c1612016-01-21 19:08:55 +08001687 if (adev->asic_type >= CHIP_BONAIRE)
1688 /* doorbell bar mapping */
1689 amdgpu_doorbell_init(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001690
1691 /* io port mapping */
1692 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
1693 if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
1694 adev->rio_mem_size = pci_resource_len(adev->pdev, i);
1695 adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
1696 break;
1697 }
1698 }
1699 if (adev->rio_mem == NULL)
1700 DRM_ERROR("Unable to find PCI I/O BAR\n");
1701
1702 /* early init functions */
1703 r = amdgpu_early_init(adev);
1704 if (r)
1705 return r;
1706
1707 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
1708 /* this will fail for cards that aren't VGA class devices, just
1709 * ignore it */
1710 vga_client_register(adev->pdev, adev, NULL, amdgpu_vga_set_decode);
1711
1712 if (amdgpu_runtime_pm == 1)
1713 runtime = true;
Alex Deuchere9bef452016-04-25 13:12:18 -04001714 if (amdgpu_device_is_px(ddev))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001715 runtime = true;
1716 vga_switcheroo_register_client(adev->pdev, &amdgpu_switcheroo_ops, runtime);
1717 if (runtime)
1718 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
1719
1720 /* Read BIOS */
Alex Deucher83ba1262016-06-03 18:21:41 -04001721 if (!amdgpu_get_bios(adev)) {
1722 r = -EINVAL;
1723 goto failed;
1724 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001725 /* Must be an ATOMBIOS */
1726 if (!adev->is_atom_bios) {
1727 dev_err(adev->dev, "Expecting atombios for GPU\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001728 r = -EINVAL;
1729 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001730 }
1731 r = amdgpu_atombios_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001732 if (r) {
1733 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001734 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001735 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001736
Monk Liu4e99a442016-03-31 13:26:59 +08001737 /* detect if we are with an SRIOV vbios */
1738 amdgpu_device_detect_sriov_bios(adev);
Andres Rodriguez048765a2016-06-11 02:51:32 -04001739
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001740 /* Post card if necessary */
Monk Liubec86372016-09-14 19:38:08 +08001741 if (amdgpu_vpost_needed(adev)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001742 if (!adev->bios) {
Monk Liubec86372016-09-14 19:38:08 +08001743 dev_err(adev->dev, "no vBIOS found\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001744 r = -EINVAL;
1745 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001746 }
Monk Liubec86372016-09-14 19:38:08 +08001747 DRM_INFO("GPU posting now...\n");
Monk Liu4e99a442016-03-31 13:26:59 +08001748 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
1749 if (r) {
1750 dev_err(adev->dev, "gpu post error!\n");
1751 goto failed;
1752 }
1753 } else {
1754 DRM_INFO("GPU post is not needed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001755 }
1756
1757 /* Initialize clocks */
1758 r = amdgpu_atombios_get_clock_info(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001759 if (r) {
1760 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001761 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001762 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001763 /* init i2c buses */
1764 amdgpu_atombios_i2c_init(adev);
1765
1766 /* Fence driver */
1767 r = amdgpu_fence_driver_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001768 if (r) {
1769 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001770 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001771 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001772
1773 /* init the mode config */
1774 drm_mode_config_init(adev->ddev);
1775
1776 r = amdgpu_init(adev);
1777 if (r) {
Alex Deucher2c1a2782015-12-07 17:02:53 -05001778 dev_err(adev->dev, "amdgpu_init failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001779 amdgpu_fini(adev);
Alex Deucher83ba1262016-06-03 18:21:41 -04001780 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001781 }
1782
1783 adev->accel_working = true;
1784
Marek Olšák95844d22016-08-17 23:49:27 +02001785 /* Initialize the buffer migration limit. */
1786 if (amdgpu_moverate >= 0)
1787 max_MBps = amdgpu_moverate;
1788 else
1789 max_MBps = 8; /* Allow 8 MB/s. */
1790 /* Get a log2 for easy divisions. */
1791 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
1792
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001793 amdgpu_fbdev_init(adev);
1794
1795 r = amdgpu_ib_pool_init(adev);
1796 if (r) {
1797 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
Alex Deucher83ba1262016-06-03 18:21:41 -04001798 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001799 }
1800
1801 r = amdgpu_ib_ring_tests(adev);
1802 if (r)
1803 DRM_ERROR("ib ring test failed (%d).\n", r);
1804
1805 r = amdgpu_gem_debugfs_init(adev);
1806 if (r) {
1807 DRM_ERROR("registering gem debugfs failed (%d).\n", r);
1808 }
1809
1810 r = amdgpu_debugfs_regs_init(adev);
1811 if (r) {
1812 DRM_ERROR("registering register debugfs failed (%d).\n", r);
1813 }
1814
Huang Rui50ab2532016-06-12 15:51:09 +08001815 r = amdgpu_debugfs_firmware_init(adev);
1816 if (r) {
1817 DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
1818 return r;
1819 }
1820
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001821 if ((amdgpu_testing & 1)) {
1822 if (adev->accel_working)
1823 amdgpu_test_moves(adev);
1824 else
1825 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
1826 }
1827 if ((amdgpu_testing & 2)) {
1828 if (adev->accel_working)
1829 amdgpu_test_syncing(adev);
1830 else
1831 DRM_INFO("amdgpu: acceleration disabled, skipping sync tests\n");
1832 }
1833 if (amdgpu_benchmarking) {
1834 if (adev->accel_working)
1835 amdgpu_benchmark(adev, amdgpu_benchmarking);
1836 else
1837 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
1838 }
1839
1840 /* enable clockgating, etc. after ib tests, etc. since some blocks require
1841 * explicit gating rather than handling it automatically.
1842 */
1843 r = amdgpu_late_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001844 if (r) {
1845 dev_err(adev->dev, "amdgpu_late_init failed\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001846 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001847 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001848
1849 return 0;
Alex Deucher83ba1262016-06-03 18:21:41 -04001850
1851failed:
1852 if (runtime)
1853 vga_switcheroo_fini_domain_pm_ops(adev->dev);
1854 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001855}
1856
1857static void amdgpu_debugfs_remove_files(struct amdgpu_device *adev);
1858
1859/**
1860 * amdgpu_device_fini - tear down the driver
1861 *
1862 * @adev: amdgpu_device pointer
1863 *
1864 * Tear down the driver info (all asics).
1865 * Called at driver shutdown.
1866 */
1867void amdgpu_device_fini(struct amdgpu_device *adev)
1868{
1869 int r;
1870
1871 DRM_INFO("amdgpu: finishing device.\n");
1872 adev->shutdown = true;
Grazvydas Ignotasa951ed82016-09-25 23:34:48 +03001873 drm_crtc_force_disable_all(adev->ddev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001874 /* evict vram memory */
1875 amdgpu_bo_evict_vram(adev);
1876 amdgpu_ib_pool_fini(adev);
1877 amdgpu_fence_driver_fini(adev);
1878 amdgpu_fbdev_fini(adev);
1879 r = amdgpu_fini(adev);
Alex Deucher8faf0e082015-07-28 11:50:31 -04001880 kfree(adev->ip_block_status);
1881 adev->ip_block_status = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001882 adev->accel_working = false;
1883 /* free i2c buses */
1884 amdgpu_i2c_fini(adev);
1885 amdgpu_atombios_fini(adev);
1886 kfree(adev->bios);
1887 adev->bios = NULL;
1888 vga_switcheroo_unregister_client(adev->pdev);
Alex Deucher83ba1262016-06-03 18:21:41 -04001889 if (adev->flags & AMD_IS_PX)
1890 vga_switcheroo_fini_domain_pm_ops(adev->dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001891 vga_client_register(adev->pdev, NULL, NULL, NULL);
1892 if (adev->rio_mem)
1893 pci_iounmap(adev->pdev, adev->rio_mem);
1894 adev->rio_mem = NULL;
1895 iounmap(adev->rmmio);
1896 adev->rmmio = NULL;
Ken Wangda69c1612016-01-21 19:08:55 +08001897 if (adev->asic_type >= CHIP_BONAIRE)
1898 amdgpu_doorbell_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001899 amdgpu_debugfs_regs_cleanup(adev);
1900 amdgpu_debugfs_remove_files(adev);
1901}
1902
1903
1904/*
1905 * Suspend & resume.
1906 */
1907/**
Alex Deucher810ddc32016-08-23 13:25:49 -04001908 * amdgpu_device_suspend - initiate device suspend
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001909 *
1910 * @pdev: drm dev pointer
1911 * @state: suspend state
1912 *
1913 * Puts the hw in the suspend state (all asics).
1914 * Returns 0 for success or an error on failure.
1915 * Called at driver suspend.
1916 */
Alex Deucher810ddc32016-08-23 13:25:49 -04001917int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001918{
1919 struct amdgpu_device *adev;
1920 struct drm_crtc *crtc;
1921 struct drm_connector *connector;
Alex Deucher5ceb54c2015-08-05 12:41:48 -04001922 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001923
1924 if (dev == NULL || dev->dev_private == NULL) {
1925 return -ENODEV;
1926 }
1927
1928 adev = dev->dev_private;
1929
1930 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
1931 return 0;
1932
1933 drm_kms_helper_poll_disable(dev);
1934
1935 /* turn off display hw */
Alex Deucher4c7fbc32015-09-23 14:32:06 -04001936 drm_modeset_lock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001937 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1938 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
1939 }
Alex Deucher4c7fbc32015-09-23 14:32:06 -04001940 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001941
Alex Deucher756e6882015-10-08 00:03:36 -04001942 /* unpin the front buffers and cursors */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001943 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Alex Deucher756e6882015-10-08 00:03:36 -04001944 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001945 struct amdgpu_framebuffer *rfb = to_amdgpu_framebuffer(crtc->primary->fb);
1946 struct amdgpu_bo *robj;
1947
Alex Deucher756e6882015-10-08 00:03:36 -04001948 if (amdgpu_crtc->cursor_bo) {
1949 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
1950 r = amdgpu_bo_reserve(aobj, false);
1951 if (r == 0) {
1952 amdgpu_bo_unpin(aobj);
1953 amdgpu_bo_unreserve(aobj);
1954 }
1955 }
1956
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001957 if (rfb == NULL || rfb->obj == NULL) {
1958 continue;
1959 }
1960 robj = gem_to_amdgpu_bo(rfb->obj);
1961 /* don't unpin kernel fb objects */
1962 if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
1963 r = amdgpu_bo_reserve(robj, false);
1964 if (r == 0) {
1965 amdgpu_bo_unpin(robj);
1966 amdgpu_bo_unreserve(robj);
1967 }
1968 }
1969 }
1970 /* evict vram memory */
1971 amdgpu_bo_evict_vram(adev);
1972
Alex Deucher5ceb54c2015-08-05 12:41:48 -04001973 amdgpu_fence_driver_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001974
1975 r = amdgpu_suspend(adev);
1976
1977 /* evict remaining vram memory */
1978 amdgpu_bo_evict_vram(adev);
1979
1980 pci_save_state(dev->pdev);
1981 if (suspend) {
1982 /* Shut down the device */
1983 pci_disable_device(dev->pdev);
1984 pci_set_power_state(dev->pdev, PCI_D3hot);
jimqu74b0b152016-09-07 17:09:12 +08001985 } else {
1986 r = amdgpu_asic_reset(adev);
1987 if (r)
1988 DRM_ERROR("amdgpu asic reset failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001989 }
1990
1991 if (fbcon) {
1992 console_lock();
1993 amdgpu_fbdev_set_suspend(adev, 1);
1994 console_unlock();
1995 }
1996 return 0;
1997}
1998
1999/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002000 * amdgpu_device_resume - initiate device resume
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002001 *
2002 * @pdev: drm dev pointer
2003 *
2004 * Bring the hw back to operating state (all asics).
2005 * Returns 0 for success or an error on failure.
2006 * Called at driver resume.
2007 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002008int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002009{
2010 struct drm_connector *connector;
2011 struct amdgpu_device *adev = dev->dev_private;
Alex Deucher756e6882015-10-08 00:03:36 -04002012 struct drm_crtc *crtc;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002013 int r;
2014
2015 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2016 return 0;
2017
jimqu74b0b152016-09-07 17:09:12 +08002018 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002019 console_lock();
jimqu74b0b152016-09-07 17:09:12 +08002020
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002021 if (resume) {
2022 pci_set_power_state(dev->pdev, PCI_D0);
2023 pci_restore_state(dev->pdev);
jimqu74b0b152016-09-07 17:09:12 +08002024 r = pci_enable_device(dev->pdev);
2025 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002026 if (fbcon)
2027 console_unlock();
jimqu74b0b152016-09-07 17:09:12 +08002028 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002029 }
2030 }
2031
2032 /* post card */
jimqu74b0b152016-09-07 17:09:12 +08002033 if (!amdgpu_card_posted(adev) || !resume) {
2034 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2035 if (r)
2036 DRM_ERROR("amdgpu asic init failed\n");
2037 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002038
2039 r = amdgpu_resume(adev);
Flora Cuica198522016-02-04 15:10:08 +08002040 if (r)
2041 DRM_ERROR("amdgpu_resume failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002042
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002043 amdgpu_fence_driver_resume(adev);
2044
Flora Cuica198522016-02-04 15:10:08 +08002045 if (resume) {
2046 r = amdgpu_ib_ring_tests(adev);
2047 if (r)
2048 DRM_ERROR("ib ring test failed (%d).\n", r);
2049 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002050
2051 r = amdgpu_late_init(adev);
2052 if (r)
2053 return r;
2054
Alex Deucher756e6882015-10-08 00:03:36 -04002055 /* pin cursors */
2056 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2057 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2058
2059 if (amdgpu_crtc->cursor_bo) {
2060 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
2061 r = amdgpu_bo_reserve(aobj, false);
2062 if (r == 0) {
2063 r = amdgpu_bo_pin(aobj,
2064 AMDGPU_GEM_DOMAIN_VRAM,
2065 &amdgpu_crtc->cursor_addr);
2066 if (r != 0)
2067 DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
2068 amdgpu_bo_unreserve(aobj);
2069 }
2070 }
2071 }
2072
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002073 /* blat the mode back in */
2074 if (fbcon) {
2075 drm_helper_resume_force_mode(dev);
2076 /* turn on display hw */
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002077 drm_modeset_lock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002078 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2079 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
2080 }
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002081 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002082 }
2083
2084 drm_kms_helper_poll_enable(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002085
2086 /*
2087 * Most of the connector probing functions try to acquire runtime pm
2088 * refs to ensure that the GPU is powered on when connector polling is
2089 * performed. Since we're calling this from a runtime PM callback,
2090 * trying to acquire rpm refs will cause us to deadlock.
2091 *
2092 * Since we're guaranteed to be holding the rpm lock, it's safe to
2093 * temporarily disable the rpm helpers so this doesn't deadlock us.
2094 */
2095#ifdef CONFIG_PM
2096 dev->dev->power.disable_depth++;
2097#endif
Alex Deucher54fb2a52015-11-24 14:30:56 -05002098 drm_helper_hpd_irq_event(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002099#ifdef CONFIG_PM
2100 dev->dev->power.disable_depth--;
2101#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002102
2103 if (fbcon) {
2104 amdgpu_fbdev_set_suspend(adev, 0);
2105 console_unlock();
2106 }
2107
2108 return 0;
2109}
2110
Chunming Zhou63fbf422016-07-15 11:19:20 +08002111static bool amdgpu_check_soft_reset(struct amdgpu_device *adev)
2112{
2113 int i;
2114 bool asic_hang = false;
2115
2116 for (i = 0; i < adev->num_ip_blocks; i++) {
2117 if (!adev->ip_block_status[i].valid)
2118 continue;
2119 if (adev->ip_blocks[i].funcs->check_soft_reset)
Alex Deucherda146d32016-10-13 16:07:03 -04002120 adev->ip_block_status[i].hang =
2121 adev->ip_blocks[i].funcs->check_soft_reset(adev);
Chunming Zhou63fbf422016-07-15 11:19:20 +08002122 if (adev->ip_block_status[i].hang) {
2123 DRM_INFO("IP block:%d is hang!\n", i);
2124 asic_hang = true;
2125 }
2126 }
2127 return asic_hang;
2128}
2129
Baoyou Xie4d446652016-09-18 22:09:35 +08002130static int amdgpu_pre_soft_reset(struct amdgpu_device *adev)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002131{
2132 int i, r = 0;
2133
2134 for (i = 0; i < adev->num_ip_blocks; i++) {
2135 if (!adev->ip_block_status[i].valid)
2136 continue;
Chunming Zhou35d782f2016-07-15 15:57:13 +08002137 if (adev->ip_block_status[i].hang &&
2138 adev->ip_blocks[i].funcs->pre_soft_reset) {
Chunming Zhoud31a5012016-07-18 10:04:34 +08002139 r = adev->ip_blocks[i].funcs->pre_soft_reset(adev);
2140 if (r)
2141 return r;
2142 }
2143 }
2144
2145 return 0;
2146}
2147
Chunming Zhou35d782f2016-07-15 15:57:13 +08002148static bool amdgpu_need_full_reset(struct amdgpu_device *adev)
2149{
Alex Deucherda146d32016-10-13 16:07:03 -04002150 int i;
2151
2152 for (i = 0; i < adev->num_ip_blocks; i++) {
2153 if (!adev->ip_block_status[i].valid)
2154 continue;
2155 if ((adev->ip_blocks[i].type == AMD_IP_BLOCK_TYPE_GMC) ||
2156 (adev->ip_blocks[i].type == AMD_IP_BLOCK_TYPE_SMC) ||
2157 (adev->ip_blocks[i].type == AMD_IP_BLOCK_TYPE_ACP) ||
2158 (adev->ip_blocks[i].type == AMD_IP_BLOCK_TYPE_DCE)) {
2159 if (adev->ip_block_status[i].hang) {
2160 DRM_INFO("Some block need full reset!\n");
2161 return true;
2162 }
2163 }
Chunming Zhou35d782f2016-07-15 15:57:13 +08002164 }
2165 return false;
2166}
2167
2168static int amdgpu_soft_reset(struct amdgpu_device *adev)
2169{
2170 int i, r = 0;
2171
2172 for (i = 0; i < adev->num_ip_blocks; i++) {
2173 if (!adev->ip_block_status[i].valid)
2174 continue;
2175 if (adev->ip_block_status[i].hang &&
2176 adev->ip_blocks[i].funcs->soft_reset) {
2177 r = adev->ip_blocks[i].funcs->soft_reset(adev);
2178 if (r)
2179 return r;
2180 }
2181 }
2182
2183 return 0;
2184}
2185
2186static int amdgpu_post_soft_reset(struct amdgpu_device *adev)
2187{
2188 int i, r = 0;
2189
2190 for (i = 0; i < adev->num_ip_blocks; i++) {
2191 if (!adev->ip_block_status[i].valid)
2192 continue;
2193 if (adev->ip_block_status[i].hang &&
2194 adev->ip_blocks[i].funcs->post_soft_reset)
2195 r = adev->ip_blocks[i].funcs->post_soft_reset(adev);
2196 if (r)
2197 return r;
2198 }
2199
2200 return 0;
2201}
2202
Chunming Zhou3ad81f12016-08-05 17:30:17 +08002203bool amdgpu_need_backup(struct amdgpu_device *adev)
2204{
2205 if (adev->flags & AMD_IS_APU)
2206 return false;
2207
2208 return amdgpu_lockup_timeout > 0 ? true : false;
2209}
2210
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002211static int amdgpu_recover_vram_from_shadow(struct amdgpu_device *adev,
2212 struct amdgpu_ring *ring,
2213 struct amdgpu_bo *bo,
2214 struct fence **fence)
2215{
2216 uint32_t domain;
2217 int r;
2218
2219 if (!bo->shadow)
2220 return 0;
2221
2222 r = amdgpu_bo_reserve(bo, false);
2223 if (r)
2224 return r;
2225 domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
2226 /* if bo has been evicted, then no need to recover */
2227 if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
2228 r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
2229 NULL, fence, true);
2230 if (r) {
2231 DRM_ERROR("recover page table failed!\n");
2232 goto err;
2233 }
2234 }
2235err:
2236 amdgpu_bo_unreserve(bo);
2237 return r;
2238}
2239
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002240/**
2241 * amdgpu_gpu_reset - reset the asic
2242 *
2243 * @adev: amdgpu device pointer
2244 *
2245 * Attempt the reset the GPU if it has hung (all asics).
2246 * Returns 0 for success or an error on failure.
2247 */
2248int amdgpu_gpu_reset(struct amdgpu_device *adev)
2249{
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002250 int i, r;
2251 int resched;
Chunming Zhou35d782f2016-07-15 15:57:13 +08002252 bool need_full_reset;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002253
Chunming Zhou63fbf422016-07-15 11:19:20 +08002254 if (!amdgpu_check_soft_reset(adev)) {
2255 DRM_INFO("No hardware hang detected. Did some blocks stall?\n");
2256 return 0;
2257 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002258
Marek Olšákd94aed52015-05-05 21:13:49 +02002259 atomic_inc(&adev->gpu_reset_counter);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002260
Chunming Zhoua3c47d62016-06-30 16:44:41 +08002261 /* block TTM */
2262 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
2263
Chunming Zhou0875dc92016-06-12 15:41:58 +08002264 /* block scheduler */
2265 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2266 struct amdgpu_ring *ring = adev->rings[i];
2267
2268 if (!ring)
2269 continue;
2270 kthread_park(ring->sched.thread);
Chunming Zhouaa1c8902016-06-30 13:56:02 +08002271 amd_sched_hw_job_reset(&ring->sched);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002272 }
Chunming Zhou2200eda2016-06-30 16:53:02 +08002273 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
2274 amdgpu_fence_driver_force_completion(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002275
Chunming Zhou35d782f2016-07-15 15:57:13 +08002276 need_full_reset = amdgpu_need_full_reset(adev);
2277
2278 if (!need_full_reset) {
2279 amdgpu_pre_soft_reset(adev);
2280 r = amdgpu_soft_reset(adev);
2281 amdgpu_post_soft_reset(adev);
2282 if (r || amdgpu_check_soft_reset(adev)) {
2283 DRM_INFO("soft reset failed, will fallback to full reset!\n");
2284 need_full_reset = true;
2285 }
2286 }
2287
2288 if (need_full_reset) {
2289 /* save scratch */
2290 amdgpu_atombios_scratch_regs_save(adev);
2291 r = amdgpu_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002292
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002293retry:
Chunming Zhou35d782f2016-07-15 15:57:13 +08002294 /* Disable fb access */
2295 if (adev->mode_info.num_crtc) {
2296 struct amdgpu_mode_mc_save save;
2297 amdgpu_display_stop_mc_access(adev, &save);
2298 amdgpu_wait_for_idle(adev, AMD_IP_BLOCK_TYPE_GMC);
2299 }
Chunming Zhouf1aa7e02016-06-28 10:38:50 +08002300
Chunming Zhou35d782f2016-07-15 15:57:13 +08002301 r = amdgpu_asic_reset(adev);
2302 /* post card */
2303 amdgpu_atom_asic_init(adev->mode_info.atom_context);
Alex Deucherbfa99262016-01-15 11:59:48 -05002304
Chunming Zhou35d782f2016-07-15 15:57:13 +08002305 if (!r) {
2306 dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
2307 r = amdgpu_resume(adev);
2308 }
2309 /* restore scratch */
2310 amdgpu_atombios_scratch_regs_restore(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002311 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002312 if (!r) {
Chunming Zhoue72cfd52016-07-27 13:15:20 +08002313 amdgpu_irq_gpu_reset_resume_helper(adev);
Chunming Zhou2c0d7312016-08-30 16:36:25 +08002314 if (need_full_reset && amdgpu_need_backup(adev)) {
2315 r = amdgpu_ttm_recover_gart(adev);
2316 if (r)
2317 DRM_ERROR("gart recovery failed!!!\n");
2318 }
Chunming Zhou1f465082016-06-30 15:02:26 +08002319 r = amdgpu_ib_ring_tests(adev);
2320 if (r) {
2321 dev_err(adev->dev, "ib ring test failed (%d).\n", r);
Chunming Zhou40019dc2016-06-29 16:01:49 +08002322 r = amdgpu_suspend(adev);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002323 need_full_reset = true;
Chunming Zhou40019dc2016-06-29 16:01:49 +08002324 goto retry;
Chunming Zhou1f465082016-06-30 15:02:26 +08002325 }
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002326 /**
2327 * recovery vm page tables, since we cannot depend on VRAM is
2328 * consistent after gpu full reset.
2329 */
2330 if (need_full_reset && amdgpu_need_backup(adev)) {
2331 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
2332 struct amdgpu_bo *bo, *tmp;
2333 struct fence *fence = NULL, *next = NULL;
Chunming Zhou1f465082016-06-30 15:02:26 +08002334
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002335 DRM_INFO("recover vram bo from shadow\n");
2336 mutex_lock(&adev->shadow_list_lock);
2337 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
2338 amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
2339 if (fence) {
2340 r = fence_wait(fence, false);
2341 if (r) {
2342 WARN(r, "recovery from shadow isn't comleted\n");
2343 break;
2344 }
2345 }
2346
2347 fence_put(fence);
2348 fence = next;
2349 }
2350 mutex_unlock(&adev->shadow_list_lock);
2351 if (fence) {
2352 r = fence_wait(fence, false);
2353 if (r)
2354 WARN(r, "recovery from shadow isn't comleted\n");
2355 }
2356 fence_put(fence);
2357 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002358 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2359 struct amdgpu_ring *ring = adev->rings[i];
2360 if (!ring)
2361 continue;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002362
Chunming Zhouaa1c8902016-06-30 13:56:02 +08002363 amd_sched_job_recovery(&ring->sched);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002364 kthread_unpark(ring->sched.thread);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002365 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002366 } else {
Chunming Zhou2200eda2016-06-30 16:53:02 +08002367 dev_err(adev->dev, "asic resume failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002368 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
Chunming Zhou0875dc92016-06-12 15:41:58 +08002369 if (adev->rings[i]) {
2370 kthread_unpark(adev->rings[i]->sched.thread);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002371 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002372 }
2373 }
2374
2375 drm_helper_resume_force_mode(adev->ddev);
2376
2377 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
2378 if (r) {
2379 /* bad news, how to tell it to userspace ? */
2380 dev_info(adev->dev, "GPU reset failed\n");
2381 }
2382
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002383 return r;
2384}
2385
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002386void amdgpu_get_pcie_info(struct amdgpu_device *adev)
2387{
2388 u32 mask;
2389 int ret;
2390
Alex Deuchercd474ba2016-02-04 10:21:23 -05002391 if (amdgpu_pcie_gen_cap)
2392 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
2393
2394 if (amdgpu_pcie_lane_cap)
2395 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
2396
2397 /* covers APUs as well */
2398 if (pci_is_root_bus(adev->pdev->bus)) {
2399 if (adev->pm.pcie_gen_mask == 0)
2400 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
2401 if (adev->pm.pcie_mlw_mask == 0)
2402 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002403 return;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002404 }
Alex Deuchercd474ba2016-02-04 10:21:23 -05002405
2406 if (adev->pm.pcie_gen_mask == 0) {
2407 ret = drm_pcie_get_speed_cap_mask(adev->ddev, &mask);
2408 if (!ret) {
2409 adev->pm.pcie_gen_mask = (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
2410 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
2411 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
2412
2413 if (mask & DRM_PCIE_SPEED_25)
2414 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
2415 if (mask & DRM_PCIE_SPEED_50)
2416 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2;
2417 if (mask & DRM_PCIE_SPEED_80)
2418 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3;
2419 } else {
2420 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
2421 }
2422 }
2423 if (adev->pm.pcie_mlw_mask == 0) {
2424 ret = drm_pcie_get_max_link_width(adev->ddev, &mask);
2425 if (!ret) {
2426 switch (mask) {
2427 case 32:
2428 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
2429 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
2430 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2431 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2432 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2433 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2434 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2435 break;
2436 case 16:
2437 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
2438 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2439 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2440 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2441 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2442 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2443 break;
2444 case 12:
2445 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2446 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2447 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2448 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2449 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2450 break;
2451 case 8:
2452 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2453 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2454 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2455 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2456 break;
2457 case 4:
2458 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2459 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2460 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2461 break;
2462 case 2:
2463 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2464 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2465 break;
2466 case 1:
2467 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
2468 break;
2469 default:
2470 break;
2471 }
2472 } else {
2473 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002474 }
2475 }
2476}
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002477
2478/*
2479 * Debugfs
2480 */
2481int amdgpu_debugfs_add_files(struct amdgpu_device *adev,
Nils Wallménius06ab6832016-05-02 12:46:15 -04002482 const struct drm_info_list *files,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002483 unsigned nfiles)
2484{
2485 unsigned i;
2486
2487 for (i = 0; i < adev->debugfs_count; i++) {
2488 if (adev->debugfs[i].files == files) {
2489 /* Already registered */
2490 return 0;
2491 }
2492 }
2493
2494 i = adev->debugfs_count + 1;
2495 if (i > AMDGPU_DEBUGFS_MAX_COMPONENTS) {
2496 DRM_ERROR("Reached maximum number of debugfs components.\n");
2497 DRM_ERROR("Report so we increase "
2498 "AMDGPU_DEBUGFS_MAX_COMPONENTS.\n");
2499 return -EINVAL;
2500 }
2501 adev->debugfs[adev->debugfs_count].files = files;
2502 adev->debugfs[adev->debugfs_count].num_files = nfiles;
2503 adev->debugfs_count = i;
2504#if defined(CONFIG_DEBUG_FS)
2505 drm_debugfs_create_files(files, nfiles,
2506 adev->ddev->control->debugfs_root,
2507 adev->ddev->control);
2508 drm_debugfs_create_files(files, nfiles,
2509 adev->ddev->primary->debugfs_root,
2510 adev->ddev->primary);
2511#endif
2512 return 0;
2513}
2514
2515static void amdgpu_debugfs_remove_files(struct amdgpu_device *adev)
2516{
2517#if defined(CONFIG_DEBUG_FS)
2518 unsigned i;
2519
2520 for (i = 0; i < adev->debugfs_count; i++) {
2521 drm_debugfs_remove_files(adev->debugfs[i].files,
2522 adev->debugfs[i].num_files,
2523 adev->ddev->control);
2524 drm_debugfs_remove_files(adev->debugfs[i].files,
2525 adev->debugfs[i].num_files,
2526 adev->ddev->primary);
2527 }
2528#endif
2529}
2530
2531#if defined(CONFIG_DEBUG_FS)
2532
2533static ssize_t amdgpu_debugfs_regs_read(struct file *f, char __user *buf,
2534 size_t size, loff_t *pos)
2535{
2536 struct amdgpu_device *adev = f->f_inode->i_private;
2537 ssize_t result = 0;
2538 int r;
Tom St Denisbd122672016-07-28 09:39:22 -04002539 bool pm_pg_lock, use_bank;
Tom St Denis566281592016-06-27 11:55:07 -04002540 unsigned instance_bank, sh_bank, se_bank;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002541
2542 if (size & 0x3 || *pos & 0x3)
2543 return -EINVAL;
2544
Tom St Denisbd122672016-07-28 09:39:22 -04002545 /* are we reading registers for which a PG lock is necessary? */
2546 pm_pg_lock = (*pos >> 23) & 1;
2547
Tom St Denis566281592016-06-27 11:55:07 -04002548 if (*pos & (1ULL << 62)) {
2549 se_bank = (*pos >> 24) & 0x3FF;
2550 sh_bank = (*pos >> 34) & 0x3FF;
2551 instance_bank = (*pos >> 44) & 0x3FF;
2552 use_bank = 1;
Tom St Denis566281592016-06-27 11:55:07 -04002553 } else {
2554 use_bank = 0;
2555 }
2556
Tom St Denisbd122672016-07-28 09:39:22 -04002557 *pos &= 0x3FFFF;
2558
Tom St Denis566281592016-06-27 11:55:07 -04002559 if (use_bank) {
2560 if (sh_bank >= adev->gfx.config.max_sh_per_se ||
2561 se_bank >= adev->gfx.config.max_shader_engines)
2562 return -EINVAL;
2563 mutex_lock(&adev->grbm_idx_mutex);
2564 amdgpu_gfx_select_se_sh(adev, se_bank,
2565 sh_bank, instance_bank);
2566 }
2567
Tom St Denisbd122672016-07-28 09:39:22 -04002568 if (pm_pg_lock)
2569 mutex_lock(&adev->pm.mutex);
2570
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002571 while (size) {
2572 uint32_t value;
2573
2574 if (*pos > adev->rmmio_size)
Tom St Denis566281592016-06-27 11:55:07 -04002575 goto end;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002576
2577 value = RREG32(*pos >> 2);
2578 r = put_user(value, (uint32_t *)buf);
Tom St Denis566281592016-06-27 11:55:07 -04002579 if (r) {
2580 result = r;
2581 goto end;
2582 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002583
2584 result += 4;
2585 buf += 4;
2586 *pos += 4;
2587 size -= 4;
2588 }
2589
Tom St Denis566281592016-06-27 11:55:07 -04002590end:
2591 if (use_bank) {
2592 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
2593 mutex_unlock(&adev->grbm_idx_mutex);
2594 }
2595
Tom St Denisbd122672016-07-28 09:39:22 -04002596 if (pm_pg_lock)
2597 mutex_unlock(&adev->pm.mutex);
2598
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002599 return result;
2600}
2601
2602static ssize_t amdgpu_debugfs_regs_write(struct file *f, const char __user *buf,
2603 size_t size, loff_t *pos)
2604{
2605 struct amdgpu_device *adev = f->f_inode->i_private;
2606 ssize_t result = 0;
2607 int r;
2608
2609 if (size & 0x3 || *pos & 0x3)
2610 return -EINVAL;
2611
2612 while (size) {
2613 uint32_t value;
2614
2615 if (*pos > adev->rmmio_size)
2616 return result;
2617
2618 r = get_user(value, (uint32_t *)buf);
2619 if (r)
2620 return r;
2621
2622 WREG32(*pos >> 2, value);
2623
2624 result += 4;
2625 buf += 4;
2626 *pos += 4;
2627 size -= 4;
2628 }
2629
2630 return result;
2631}
2632
Tom St Denisadcec282016-04-15 13:08:44 -04002633static ssize_t amdgpu_debugfs_regs_pcie_read(struct file *f, char __user *buf,
2634 size_t size, loff_t *pos)
2635{
2636 struct amdgpu_device *adev = f->f_inode->i_private;
2637 ssize_t result = 0;
2638 int r;
2639
2640 if (size & 0x3 || *pos & 0x3)
2641 return -EINVAL;
2642
2643 while (size) {
2644 uint32_t value;
2645
2646 value = RREG32_PCIE(*pos >> 2);
2647 r = put_user(value, (uint32_t *)buf);
2648 if (r)
2649 return r;
2650
2651 result += 4;
2652 buf += 4;
2653 *pos += 4;
2654 size -= 4;
2655 }
2656
2657 return result;
2658}
2659
2660static ssize_t amdgpu_debugfs_regs_pcie_write(struct file *f, const char __user *buf,
2661 size_t size, loff_t *pos)
2662{
2663 struct amdgpu_device *adev = f->f_inode->i_private;
2664 ssize_t result = 0;
2665 int r;
2666
2667 if (size & 0x3 || *pos & 0x3)
2668 return -EINVAL;
2669
2670 while (size) {
2671 uint32_t value;
2672
2673 r = get_user(value, (uint32_t *)buf);
2674 if (r)
2675 return r;
2676
2677 WREG32_PCIE(*pos >> 2, value);
2678
2679 result += 4;
2680 buf += 4;
2681 *pos += 4;
2682 size -= 4;
2683 }
2684
2685 return result;
2686}
2687
2688static ssize_t amdgpu_debugfs_regs_didt_read(struct file *f, char __user *buf,
2689 size_t size, loff_t *pos)
2690{
2691 struct amdgpu_device *adev = f->f_inode->i_private;
2692 ssize_t result = 0;
2693 int r;
2694
2695 if (size & 0x3 || *pos & 0x3)
2696 return -EINVAL;
2697
2698 while (size) {
2699 uint32_t value;
2700
2701 value = RREG32_DIDT(*pos >> 2);
2702 r = put_user(value, (uint32_t *)buf);
2703 if (r)
2704 return r;
2705
2706 result += 4;
2707 buf += 4;
2708 *pos += 4;
2709 size -= 4;
2710 }
2711
2712 return result;
2713}
2714
2715static ssize_t amdgpu_debugfs_regs_didt_write(struct file *f, const char __user *buf,
2716 size_t size, loff_t *pos)
2717{
2718 struct amdgpu_device *adev = f->f_inode->i_private;
2719 ssize_t result = 0;
2720 int r;
2721
2722 if (size & 0x3 || *pos & 0x3)
2723 return -EINVAL;
2724
2725 while (size) {
2726 uint32_t value;
2727
2728 r = get_user(value, (uint32_t *)buf);
2729 if (r)
2730 return r;
2731
2732 WREG32_DIDT(*pos >> 2, value);
2733
2734 result += 4;
2735 buf += 4;
2736 *pos += 4;
2737 size -= 4;
2738 }
2739
2740 return result;
2741}
2742
2743static ssize_t amdgpu_debugfs_regs_smc_read(struct file *f, char __user *buf,
2744 size_t size, loff_t *pos)
2745{
2746 struct amdgpu_device *adev = f->f_inode->i_private;
2747 ssize_t result = 0;
2748 int r;
2749
2750 if (size & 0x3 || *pos & 0x3)
2751 return -EINVAL;
2752
2753 while (size) {
2754 uint32_t value;
2755
Tom St Denis6fc0dea2016-08-29 08:39:29 -04002756 value = RREG32_SMC(*pos);
Tom St Denisadcec282016-04-15 13:08:44 -04002757 r = put_user(value, (uint32_t *)buf);
2758 if (r)
2759 return r;
2760
2761 result += 4;
2762 buf += 4;
2763 *pos += 4;
2764 size -= 4;
2765 }
2766
2767 return result;
2768}
2769
2770static ssize_t amdgpu_debugfs_regs_smc_write(struct file *f, const char __user *buf,
2771 size_t size, loff_t *pos)
2772{
2773 struct amdgpu_device *adev = f->f_inode->i_private;
2774 ssize_t result = 0;
2775 int r;
2776
2777 if (size & 0x3 || *pos & 0x3)
2778 return -EINVAL;
2779
2780 while (size) {
2781 uint32_t value;
2782
2783 r = get_user(value, (uint32_t *)buf);
2784 if (r)
2785 return r;
2786
Tom St Denis6fc0dea2016-08-29 08:39:29 -04002787 WREG32_SMC(*pos, value);
Tom St Denisadcec282016-04-15 13:08:44 -04002788
2789 result += 4;
2790 buf += 4;
2791 *pos += 4;
2792 size -= 4;
2793 }
2794
2795 return result;
2796}
2797
Tom St Denis1e051412016-06-27 09:57:18 -04002798static ssize_t amdgpu_debugfs_gca_config_read(struct file *f, char __user *buf,
2799 size_t size, loff_t *pos)
2800{
2801 struct amdgpu_device *adev = f->f_inode->i_private;
2802 ssize_t result = 0;
2803 int r;
2804 uint32_t *config, no_regs = 0;
2805
2806 if (size & 0x3 || *pos & 0x3)
2807 return -EINVAL;
2808
Markus Elfringecab7662016-09-18 17:00:52 +02002809 config = kmalloc_array(256, sizeof(*config), GFP_KERNEL);
Tom St Denis1e051412016-06-27 09:57:18 -04002810 if (!config)
2811 return -ENOMEM;
2812
2813 /* version, increment each time something is added */
Tom St Denise9f11dc2016-08-17 12:00:51 -04002814 config[no_regs++] = 2;
Tom St Denis1e051412016-06-27 09:57:18 -04002815 config[no_regs++] = adev->gfx.config.max_shader_engines;
2816 config[no_regs++] = adev->gfx.config.max_tile_pipes;
2817 config[no_regs++] = adev->gfx.config.max_cu_per_sh;
2818 config[no_regs++] = adev->gfx.config.max_sh_per_se;
2819 config[no_regs++] = adev->gfx.config.max_backends_per_se;
2820 config[no_regs++] = adev->gfx.config.max_texture_channel_caches;
2821 config[no_regs++] = adev->gfx.config.max_gprs;
2822 config[no_regs++] = adev->gfx.config.max_gs_threads;
2823 config[no_regs++] = adev->gfx.config.max_hw_contexts;
2824 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_frontend;
2825 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_backend;
2826 config[no_regs++] = adev->gfx.config.sc_hiz_tile_fifo_size;
2827 config[no_regs++] = adev->gfx.config.sc_earlyz_tile_fifo_size;
2828 config[no_regs++] = adev->gfx.config.num_tile_pipes;
2829 config[no_regs++] = adev->gfx.config.backend_enable_mask;
2830 config[no_regs++] = adev->gfx.config.mem_max_burst_length_bytes;
2831 config[no_regs++] = adev->gfx.config.mem_row_size_in_kb;
2832 config[no_regs++] = adev->gfx.config.shader_engine_tile_size;
2833 config[no_regs++] = adev->gfx.config.num_gpus;
2834 config[no_regs++] = adev->gfx.config.multi_gpu_tile_size;
2835 config[no_regs++] = adev->gfx.config.mc_arb_ramcfg;
2836 config[no_regs++] = adev->gfx.config.gb_addr_config;
2837 config[no_regs++] = adev->gfx.config.num_rbs;
2838
Tom St Denis89a8f302016-08-12 15:14:31 -04002839 /* rev==1 */
2840 config[no_regs++] = adev->rev_id;
2841 config[no_regs++] = adev->pg_flags;
2842 config[no_regs++] = adev->cg_flags;
2843
Tom St Denise9f11dc2016-08-17 12:00:51 -04002844 /* rev==2 */
2845 config[no_regs++] = adev->family;
2846 config[no_regs++] = adev->external_rev_id;
2847
Tom St Denis1e051412016-06-27 09:57:18 -04002848 while (size && (*pos < no_regs * 4)) {
2849 uint32_t value;
2850
2851 value = config[*pos >> 2];
2852 r = put_user(value, (uint32_t *)buf);
2853 if (r) {
2854 kfree(config);
2855 return r;
2856 }
2857
2858 result += 4;
2859 buf += 4;
2860 *pos += 4;
2861 size -= 4;
2862 }
2863
2864 kfree(config);
2865 return result;
2866}
2867
Tom St Denisf2cdaf22016-09-15 10:08:44 -04002868static ssize_t amdgpu_debugfs_sensor_read(struct file *f, char __user *buf,
2869 size_t size, loff_t *pos)
2870{
2871 struct amdgpu_device *adev = f->f_inode->i_private;
2872 int idx, r;
2873 int32_t value;
2874
2875 if (size != 4 || *pos & 0x3)
2876 return -EINVAL;
2877
2878 /* convert offset to sensor number */
2879 idx = *pos >> 2;
2880
2881 if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->read_sensor)
2882 r = adev->powerplay.pp_funcs->read_sensor(adev->powerplay.pp_handle, idx, &value);
2883 else
2884 return -EINVAL;
2885
2886 if (!r)
2887 r = put_user(value, (int32_t *)buf);
2888
2889 return !r ? 4 : r;
2890}
Tom St Denis1e051412016-06-27 09:57:18 -04002891
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002892static const struct file_operations amdgpu_debugfs_regs_fops = {
2893 .owner = THIS_MODULE,
2894 .read = amdgpu_debugfs_regs_read,
2895 .write = amdgpu_debugfs_regs_write,
2896 .llseek = default_llseek
2897};
Tom St Denisadcec282016-04-15 13:08:44 -04002898static const struct file_operations amdgpu_debugfs_regs_didt_fops = {
2899 .owner = THIS_MODULE,
2900 .read = amdgpu_debugfs_regs_didt_read,
2901 .write = amdgpu_debugfs_regs_didt_write,
2902 .llseek = default_llseek
2903};
2904static const struct file_operations amdgpu_debugfs_regs_pcie_fops = {
2905 .owner = THIS_MODULE,
2906 .read = amdgpu_debugfs_regs_pcie_read,
2907 .write = amdgpu_debugfs_regs_pcie_write,
2908 .llseek = default_llseek
2909};
2910static const struct file_operations amdgpu_debugfs_regs_smc_fops = {
2911 .owner = THIS_MODULE,
2912 .read = amdgpu_debugfs_regs_smc_read,
2913 .write = amdgpu_debugfs_regs_smc_write,
2914 .llseek = default_llseek
2915};
2916
Tom St Denis1e051412016-06-27 09:57:18 -04002917static const struct file_operations amdgpu_debugfs_gca_config_fops = {
2918 .owner = THIS_MODULE,
2919 .read = amdgpu_debugfs_gca_config_read,
2920 .llseek = default_llseek
2921};
2922
Tom St Denisf2cdaf22016-09-15 10:08:44 -04002923static const struct file_operations amdgpu_debugfs_sensors_fops = {
2924 .owner = THIS_MODULE,
2925 .read = amdgpu_debugfs_sensor_read,
2926 .llseek = default_llseek
2927};
2928
Tom St Denisadcec282016-04-15 13:08:44 -04002929static const struct file_operations *debugfs_regs[] = {
2930 &amdgpu_debugfs_regs_fops,
2931 &amdgpu_debugfs_regs_didt_fops,
2932 &amdgpu_debugfs_regs_pcie_fops,
2933 &amdgpu_debugfs_regs_smc_fops,
Tom St Denis1e051412016-06-27 09:57:18 -04002934 &amdgpu_debugfs_gca_config_fops,
Tom St Denisf2cdaf22016-09-15 10:08:44 -04002935 &amdgpu_debugfs_sensors_fops,
Tom St Denisadcec282016-04-15 13:08:44 -04002936};
2937
2938static const char *debugfs_regs_names[] = {
2939 "amdgpu_regs",
2940 "amdgpu_regs_didt",
2941 "amdgpu_regs_pcie",
2942 "amdgpu_regs_smc",
Tom St Denis1e051412016-06-27 09:57:18 -04002943 "amdgpu_gca_config",
Tom St Denisf2cdaf22016-09-15 10:08:44 -04002944 "amdgpu_sensors",
Tom St Denisadcec282016-04-15 13:08:44 -04002945};
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002946
2947static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
2948{
2949 struct drm_minor *minor = adev->ddev->primary;
2950 struct dentry *ent, *root = minor->debugfs_root;
Tom St Denisadcec282016-04-15 13:08:44 -04002951 unsigned i, j;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002952
Tom St Denisadcec282016-04-15 13:08:44 -04002953 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
2954 ent = debugfs_create_file(debugfs_regs_names[i],
2955 S_IFREG | S_IRUGO, root,
2956 adev, debugfs_regs[i]);
2957 if (IS_ERR(ent)) {
2958 for (j = 0; j < i; j++) {
2959 debugfs_remove(adev->debugfs_regs[i]);
2960 adev->debugfs_regs[i] = NULL;
2961 }
2962 return PTR_ERR(ent);
2963 }
2964
2965 if (!i)
2966 i_size_write(ent->d_inode, adev->rmmio_size);
2967 adev->debugfs_regs[i] = ent;
2968 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002969
2970 return 0;
2971}
2972
2973static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev)
2974{
Tom St Denisadcec282016-04-15 13:08:44 -04002975 unsigned i;
2976
2977 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
2978 if (adev->debugfs_regs[i]) {
2979 debugfs_remove(adev->debugfs_regs[i]);
2980 adev->debugfs_regs[i] = NULL;
2981 }
2982 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002983}
2984
2985int amdgpu_debugfs_init(struct drm_minor *minor)
2986{
2987 return 0;
2988}
2989
2990void amdgpu_debugfs_cleanup(struct drm_minor *minor)
2991{
2992}
Alexander Kuleshov7cebc722015-06-27 13:16:05 +06002993#else
2994static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
2995{
2996 return 0;
2997}
2998static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev) { }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002999#endif