blob: 121b8440cf40aa2933fde49f76fbadc73abb96f7 [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",
Junwei Zhangc4642a42016-12-14 15:32:28 -050076 "POLARIS12",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040077 "LAST",
78};
79
80bool amdgpu_device_is_px(struct drm_device *dev)
81{
82 struct amdgpu_device *adev = dev->dev_private;
83
Jammy Zhou2f7d10b2015-07-22 11:29:01 +080084 if (adev->flags & AMD_IS_PX)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040085 return true;
86 return false;
87}
88
89/*
90 * MMIO register access helper functions.
91 */
92uint32_t amdgpu_mm_rreg(struct amdgpu_device *adev, uint32_t reg,
93 bool always_indirect)
94{
Tom St Denisf4b373f2016-05-31 08:02:27 -040095 uint32_t ret;
96
Alex Deucherd38ceaf2015-04-20 16:55:21 -040097 if ((reg * 4) < adev->rmmio_size && !always_indirect)
Tom St Denisf4b373f2016-05-31 08:02:27 -040098 ret = readl(((void __iomem *)adev->rmmio) + (reg * 4));
Alex Deucherd38ceaf2015-04-20 16:55:21 -040099 else {
100 unsigned long flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400101
102 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
103 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
104 ret = readl(((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
105 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400106 }
Tom St Denisf4b373f2016-05-31 08:02:27 -0400107 trace_amdgpu_mm_rreg(adev->pdev->device, reg, ret);
108 return ret;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400109}
110
111void amdgpu_mm_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v,
112 bool always_indirect)
113{
Tom St Denisf4b373f2016-05-31 08:02:27 -0400114 trace_amdgpu_mm_wreg(adev->pdev->device, reg, v);
Monk Liu4e99a442016-03-31 13:26:59 +0800115
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400116 if ((reg * 4) < adev->rmmio_size && !always_indirect)
117 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
118 else {
119 unsigned long flags;
120
121 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
122 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
123 writel(v, ((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
124 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
125 }
126}
127
128u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 reg)
129{
130 if ((reg * 4) < adev->rio_mem_size)
131 return ioread32(adev->rio_mem + (reg * 4));
132 else {
133 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
134 return ioread32(adev->rio_mem + (mmMM_DATA * 4));
135 }
136}
137
138void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
139{
140
141 if ((reg * 4) < adev->rio_mem_size)
142 iowrite32(v, adev->rio_mem + (reg * 4));
143 else {
144 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
145 iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
146 }
147}
148
149/**
150 * amdgpu_mm_rdoorbell - read a doorbell dword
151 *
152 * @adev: amdgpu_device pointer
153 * @index: doorbell index
154 *
155 * Returns the value in the doorbell aperture at the
156 * requested doorbell index (CIK).
157 */
158u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
159{
160 if (index < adev->doorbell.num_doorbells) {
161 return readl(adev->doorbell.ptr + index);
162 } else {
163 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
164 return 0;
165 }
166}
167
168/**
169 * amdgpu_mm_wdoorbell - write a doorbell dword
170 *
171 * @adev: amdgpu_device pointer
172 * @index: doorbell index
173 * @v: value to write
174 *
175 * Writes @v to the doorbell aperture at the
176 * requested doorbell index (CIK).
177 */
178void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
179{
180 if (index < adev->doorbell.num_doorbells) {
181 writel(v, adev->doorbell.ptr + index);
182 } else {
183 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
184 }
185}
186
187/**
188 * amdgpu_invalid_rreg - dummy reg read function
189 *
190 * @adev: amdgpu device pointer
191 * @reg: offset of register
192 *
193 * Dummy register read function. Used for register blocks
194 * that certain asics don't have (all asics).
195 * Returns the value in the register.
196 */
197static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
198{
199 DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
200 BUG();
201 return 0;
202}
203
204/**
205 * amdgpu_invalid_wreg - dummy reg write function
206 *
207 * @adev: amdgpu device pointer
208 * @reg: offset of register
209 * @v: value to write to the register
210 *
211 * Dummy register read function. Used for register blocks
212 * that certain asics don't have (all asics).
213 */
214static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
215{
216 DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
217 reg, v);
218 BUG();
219}
220
221/**
222 * amdgpu_block_invalid_rreg - dummy reg read function
223 *
224 * @adev: amdgpu device pointer
225 * @block: offset of instance
226 * @reg: offset of register
227 *
228 * Dummy register read function. Used for register blocks
229 * that certain asics don't have (all asics).
230 * Returns the value in the register.
231 */
232static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
233 uint32_t block, uint32_t reg)
234{
235 DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
236 reg, block);
237 BUG();
238 return 0;
239}
240
241/**
242 * amdgpu_block_invalid_wreg - dummy reg write function
243 *
244 * @adev: amdgpu device pointer
245 * @block: offset of instance
246 * @reg: offset of register
247 * @v: value to write to the register
248 *
249 * Dummy register read function. Used for register blocks
250 * that certain asics don't have (all asics).
251 */
252static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
253 uint32_t block,
254 uint32_t reg, uint32_t v)
255{
256 DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
257 reg, block, v);
258 BUG();
259}
260
261static int amdgpu_vram_scratch_init(struct amdgpu_device *adev)
262{
263 int r;
264
265 if (adev->vram_scratch.robj == NULL) {
266 r = amdgpu_bo_create(adev, AMDGPU_GPU_PAGE_SIZE,
Alex Deucher857d9132015-08-27 00:14:16 -0400267 PAGE_SIZE, true, AMDGPU_GEM_DOMAIN_VRAM,
Christian König03f48dd2016-08-15 17:00:22 +0200268 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
269 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS,
Christian König72d76682015-09-03 17:34:59 +0200270 NULL, NULL, &adev->vram_scratch.robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400271 if (r) {
272 return r;
273 }
274 }
275
276 r = amdgpu_bo_reserve(adev->vram_scratch.robj, false);
277 if (unlikely(r != 0))
278 return r;
279 r = amdgpu_bo_pin(adev->vram_scratch.robj,
280 AMDGPU_GEM_DOMAIN_VRAM, &adev->vram_scratch.gpu_addr);
281 if (r) {
282 amdgpu_bo_unreserve(adev->vram_scratch.robj);
283 return r;
284 }
285 r = amdgpu_bo_kmap(adev->vram_scratch.robj,
286 (void **)&adev->vram_scratch.ptr);
287 if (r)
288 amdgpu_bo_unpin(adev->vram_scratch.robj);
289 amdgpu_bo_unreserve(adev->vram_scratch.robj);
290
291 return r;
292}
293
294static void amdgpu_vram_scratch_fini(struct amdgpu_device *adev)
295{
296 int r;
297
298 if (adev->vram_scratch.robj == NULL) {
299 return;
300 }
301 r = amdgpu_bo_reserve(adev->vram_scratch.robj, false);
302 if (likely(r == 0)) {
303 amdgpu_bo_kunmap(adev->vram_scratch.robj);
304 amdgpu_bo_unpin(adev->vram_scratch.robj);
305 amdgpu_bo_unreserve(adev->vram_scratch.robj);
306 }
307 amdgpu_bo_unref(&adev->vram_scratch.robj);
308}
309
310/**
311 * amdgpu_program_register_sequence - program an array of registers.
312 *
313 * @adev: amdgpu_device pointer
314 * @registers: pointer to the register array
315 * @array_size: size of the register array
316 *
317 * Programs an array or registers with and and or masks.
318 * This is a helper for setting golden registers.
319 */
320void amdgpu_program_register_sequence(struct amdgpu_device *adev,
321 const u32 *registers,
322 const u32 array_size)
323{
324 u32 tmp, reg, and_mask, or_mask;
325 int i;
326
327 if (array_size % 3)
328 return;
329
330 for (i = 0; i < array_size; i +=3) {
331 reg = registers[i + 0];
332 and_mask = registers[i + 1];
333 or_mask = registers[i + 2];
334
335 if (and_mask == 0xffffffff) {
336 tmp = or_mask;
337 } else {
338 tmp = RREG32(reg);
339 tmp &= ~and_mask;
340 tmp |= or_mask;
341 }
342 WREG32(reg, tmp);
343 }
344}
345
346void amdgpu_pci_config_reset(struct amdgpu_device *adev)
347{
348 pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
349}
350
351/*
352 * GPU doorbell aperture helpers function.
353 */
354/**
355 * amdgpu_doorbell_init - Init doorbell driver information.
356 *
357 * @adev: amdgpu_device pointer
358 *
359 * Init doorbell driver information (CIK)
360 * Returns 0 on success, error on failure.
361 */
362static int amdgpu_doorbell_init(struct amdgpu_device *adev)
363{
364 /* doorbell bar mapping */
365 adev->doorbell.base = pci_resource_start(adev->pdev, 2);
366 adev->doorbell.size = pci_resource_len(adev->pdev, 2);
367
Christian Königedf600d2016-05-03 15:54:54 +0200368 adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400369 AMDGPU_DOORBELL_MAX_ASSIGNMENT+1);
370 if (adev->doorbell.num_doorbells == 0)
371 return -EINVAL;
372
373 adev->doorbell.ptr = ioremap(adev->doorbell.base, adev->doorbell.num_doorbells * sizeof(u32));
374 if (adev->doorbell.ptr == NULL) {
375 return -ENOMEM;
376 }
377 DRM_INFO("doorbell mmio base: 0x%08X\n", (uint32_t)adev->doorbell.base);
378 DRM_INFO("doorbell mmio size: %u\n", (unsigned)adev->doorbell.size);
379
380 return 0;
381}
382
383/**
384 * amdgpu_doorbell_fini - Tear down doorbell driver information.
385 *
386 * @adev: amdgpu_device pointer
387 *
388 * Tear down doorbell driver information (CIK)
389 */
390static void amdgpu_doorbell_fini(struct amdgpu_device *adev)
391{
392 iounmap(adev->doorbell.ptr);
393 adev->doorbell.ptr = NULL;
394}
395
396/**
397 * amdgpu_doorbell_get_kfd_info - Report doorbell configuration required to
398 * setup amdkfd
399 *
400 * @adev: amdgpu_device pointer
401 * @aperture_base: output returning doorbell aperture base physical address
402 * @aperture_size: output returning doorbell aperture size in bytes
403 * @start_offset: output returning # of doorbell bytes reserved for amdgpu.
404 *
405 * amdgpu and amdkfd share the doorbell aperture. amdgpu sets it up,
406 * takes doorbells required for its own rings and reports the setup to amdkfd.
407 * amdgpu reserved doorbells are at the start of the doorbell aperture.
408 */
409void amdgpu_doorbell_get_kfd_info(struct amdgpu_device *adev,
410 phys_addr_t *aperture_base,
411 size_t *aperture_size,
412 size_t *start_offset)
413{
414 /*
415 * The first num_doorbells are used by amdgpu.
416 * amdkfd takes whatever's left in the aperture.
417 */
418 if (adev->doorbell.size > adev->doorbell.num_doorbells * sizeof(u32)) {
419 *aperture_base = adev->doorbell.base;
420 *aperture_size = adev->doorbell.size;
421 *start_offset = adev->doorbell.num_doorbells * sizeof(u32);
422 } else {
423 *aperture_base = 0;
424 *aperture_size = 0;
425 *start_offset = 0;
426 }
427}
428
429/*
430 * amdgpu_wb_*()
431 * Writeback is the the method by which the the GPU updates special pages
432 * in memory with the status of certain GPU events (fences, ring pointers,
433 * etc.).
434 */
435
436/**
437 * amdgpu_wb_fini - Disable Writeback and free memory
438 *
439 * @adev: amdgpu_device pointer
440 *
441 * Disables Writeback and frees the Writeback memory (all asics).
442 * Used at driver shutdown.
443 */
444static void amdgpu_wb_fini(struct amdgpu_device *adev)
445{
446 if (adev->wb.wb_obj) {
Alex Deuchera76ed482016-10-21 15:30:36 -0400447 amdgpu_bo_free_kernel(&adev->wb.wb_obj,
448 &adev->wb.gpu_addr,
449 (void **)&adev->wb.wb);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400450 adev->wb.wb_obj = NULL;
451 }
452}
453
454/**
455 * amdgpu_wb_init- Init Writeback driver info and allocate memory
456 *
457 * @adev: amdgpu_device pointer
458 *
459 * Disables Writeback and frees the Writeback memory (all asics).
460 * Used at driver startup.
461 * Returns 0 on success or an -error on failure.
462 */
463static int amdgpu_wb_init(struct amdgpu_device *adev)
464{
465 int r;
466
467 if (adev->wb.wb_obj == NULL) {
Alex Deuchera76ed482016-10-21 15:30:36 -0400468 r = amdgpu_bo_create_kernel(adev, AMDGPU_MAX_WB * 4,
469 PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
470 &adev->wb.wb_obj, &adev->wb.gpu_addr,
471 (void **)&adev->wb.wb);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400472 if (r) {
473 dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
474 return r;
475 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400476
477 adev->wb.num_wb = AMDGPU_MAX_WB;
478 memset(&adev->wb.used, 0, sizeof(adev->wb.used));
479
480 /* clear wb memory */
481 memset((char *)adev->wb.wb, 0, AMDGPU_GPU_PAGE_SIZE);
482 }
483
484 return 0;
485}
486
487/**
488 * amdgpu_wb_get - Allocate a wb entry
489 *
490 * @adev: amdgpu_device pointer
491 * @wb: wb index
492 *
493 * Allocate a wb slot for use by the driver (all asics).
494 * Returns 0 on success or -EINVAL on failure.
495 */
496int amdgpu_wb_get(struct amdgpu_device *adev, u32 *wb)
497{
498 unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
499 if (offset < adev->wb.num_wb) {
500 __set_bit(offset, adev->wb.used);
501 *wb = offset;
502 return 0;
503 } else {
504 return -EINVAL;
505 }
506}
507
508/**
509 * amdgpu_wb_free - Free a wb entry
510 *
511 * @adev: amdgpu_device pointer
512 * @wb: wb index
513 *
514 * Free a wb slot allocated for use by the driver (all asics)
515 */
516void amdgpu_wb_free(struct amdgpu_device *adev, u32 wb)
517{
518 if (wb < adev->wb.num_wb)
519 __clear_bit(wb, adev->wb.used);
520}
521
522/**
523 * amdgpu_vram_location - try to find VRAM location
524 * @adev: amdgpu device structure holding all necessary informations
525 * @mc: memory controller structure holding memory informations
526 * @base: base address at which to put VRAM
527 *
528 * Function will place try to place VRAM at base address provided
529 * as parameter (which is so far either PCI aperture address or
530 * for IGP TOM base address).
531 *
532 * If there is not enough space to fit the unvisible VRAM in the 32bits
533 * address space then we limit the VRAM size to the aperture.
534 *
535 * Note: We don't explicitly enforce VRAM start to be aligned on VRAM size,
536 * this shouldn't be a problem as we are using the PCI aperture as a reference.
537 * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but
538 * not IGP.
539 *
540 * Note: we use mc_vram_size as on some board we need to program the mc to
541 * cover the whole aperture even if VRAM size is inferior to aperture size
542 * Novell bug 204882 + along with lots of ubuntu ones
543 *
544 * Note: when limiting vram it's safe to overwritte real_vram_size because
545 * we are not in case where real_vram_size is inferior to mc_vram_size (ie
546 * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu
547 * ones)
548 *
549 * Note: IGP TOM addr should be the same as the aperture addr, we don't
550 * explicitly check for that thought.
551 *
552 * FIXME: when reducing VRAM size align new size on power of 2.
553 */
554void amdgpu_vram_location(struct amdgpu_device *adev, struct amdgpu_mc *mc, u64 base)
555{
556 uint64_t limit = (uint64_t)amdgpu_vram_limit << 20;
557
558 mc->vram_start = base;
559 if (mc->mc_vram_size > (adev->mc.mc_mask - base + 1)) {
560 dev_warn(adev->dev, "limiting VRAM to PCI aperture size\n");
561 mc->real_vram_size = mc->aper_size;
562 mc->mc_vram_size = mc->aper_size;
563 }
564 mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
565 if (limit && limit < mc->real_vram_size)
566 mc->real_vram_size = limit;
567 dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
568 mc->mc_vram_size >> 20, mc->vram_start,
569 mc->vram_end, mc->real_vram_size >> 20);
570}
571
572/**
573 * amdgpu_gtt_location - try to find GTT location
574 * @adev: amdgpu device structure holding all necessary informations
575 * @mc: memory controller structure holding memory informations
576 *
577 * Function will place try to place GTT before or after VRAM.
578 *
579 * If GTT size is bigger than space left then we ajust GTT size.
580 * Thus function will never fails.
581 *
582 * FIXME: when reducing GTT size align new size on power of 2.
583 */
584void amdgpu_gtt_location(struct amdgpu_device *adev, struct amdgpu_mc *mc)
585{
586 u64 size_af, size_bf;
587
588 size_af = ((adev->mc.mc_mask - mc->vram_end) + mc->gtt_base_align) & ~mc->gtt_base_align;
589 size_bf = mc->vram_start & ~mc->gtt_base_align;
590 if (size_bf > size_af) {
591 if (mc->gtt_size > size_bf) {
592 dev_warn(adev->dev, "limiting GTT\n");
593 mc->gtt_size = size_bf;
594 }
595 mc->gtt_start = (mc->vram_start & ~mc->gtt_base_align) - mc->gtt_size;
596 } else {
597 if (mc->gtt_size > size_af) {
598 dev_warn(adev->dev, "limiting GTT\n");
599 mc->gtt_size = size_af;
600 }
601 mc->gtt_start = (mc->vram_end + 1 + mc->gtt_base_align) & ~mc->gtt_base_align;
602 }
603 mc->gtt_end = mc->gtt_start + mc->gtt_size - 1;
604 dev_info(adev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
605 mc->gtt_size >> 20, mc->gtt_start, mc->gtt_end);
606}
607
608/*
609 * GPU helpers function.
610 */
611/**
612 * amdgpu_card_posted - check if the hw has already been initialized
613 *
614 * @adev: amdgpu_device pointer
615 *
616 * Check if the asic has been initialized (all asics).
617 * Used at driver startup.
618 * Returns true if initialized or false if not.
619 */
620bool amdgpu_card_posted(struct amdgpu_device *adev)
621{
622 uint32_t reg;
623
624 /* then check MEM_SIZE, in case the crtcs are off */
625 reg = RREG32(mmCONFIG_MEMSIZE);
626
627 if (reg)
628 return true;
629
630 return false;
631
632}
633
Monk Liubec86372016-09-14 19:38:08 +0800634static bool amdgpu_vpost_needed(struct amdgpu_device *adev)
635{
636 if (amdgpu_sriov_vf(adev))
637 return false;
638
639 if (amdgpu_passthrough(adev)) {
Monk Liu1da2c322016-11-11 11:24:29 +0800640 /* for FIJI: In whole GPU pass-through virtualization case, after VM reboot
641 * some old smc fw still need driver do vPost otherwise gpu hang, while
642 * those smc fw version above 22.15 doesn't have this flaw, so we force
643 * vpost executed for smc version below 22.15
Monk Liubec86372016-09-14 19:38:08 +0800644 */
645 if (adev->asic_type == CHIP_FIJI) {
646 int err;
647 uint32_t fw_ver;
648 err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
649 /* force vPost if error occured */
650 if (err)
651 return true;
652
653 fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
Monk Liu1da2c322016-11-11 11:24:29 +0800654 if (fw_ver < 0x00160e00)
655 return true;
Monk Liubec86372016-09-14 19:38:08 +0800656 }
Monk Liubec86372016-09-14 19:38:08 +0800657 }
Monk Liu1da2c322016-11-11 11:24:29 +0800658 return !amdgpu_card_posted(adev);
Monk Liubec86372016-09-14 19:38:08 +0800659}
660
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400661/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400662 * amdgpu_dummy_page_init - init dummy page used by the driver
663 *
664 * @adev: amdgpu_device pointer
665 *
666 * Allocate the dummy page used by the driver (all asics).
667 * This dummy page is used by the driver as a filler for gart entries
668 * when pages are taken out of the GART
669 * Returns 0 on sucess, -ENOMEM on failure.
670 */
671int amdgpu_dummy_page_init(struct amdgpu_device *adev)
672{
673 if (adev->dummy_page.page)
674 return 0;
675 adev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
676 if (adev->dummy_page.page == NULL)
677 return -ENOMEM;
678 adev->dummy_page.addr = pci_map_page(adev->pdev, adev->dummy_page.page,
679 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
680 if (pci_dma_mapping_error(adev->pdev, adev->dummy_page.addr)) {
681 dev_err(&adev->pdev->dev, "Failed to DMA MAP the dummy page\n");
682 __free_page(adev->dummy_page.page);
683 adev->dummy_page.page = NULL;
684 return -ENOMEM;
685 }
686 return 0;
687}
688
689/**
690 * amdgpu_dummy_page_fini - free dummy page used by the driver
691 *
692 * @adev: amdgpu_device pointer
693 *
694 * Frees the dummy page used by the driver (all asics).
695 */
696void amdgpu_dummy_page_fini(struct amdgpu_device *adev)
697{
698 if (adev->dummy_page.page == NULL)
699 return;
700 pci_unmap_page(adev->pdev, adev->dummy_page.addr,
701 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
702 __free_page(adev->dummy_page.page);
703 adev->dummy_page.page = NULL;
704}
705
706
707/* ATOM accessor methods */
708/*
709 * ATOM is an interpreted byte code stored in tables in the vbios. The
710 * driver registers callbacks to access registers and the interpreter
711 * in the driver parses the tables and executes then to program specific
712 * actions (set display modes, asic init, etc.). See amdgpu_atombios.c,
713 * atombios.h, and atom.c
714 */
715
716/**
717 * cail_pll_read - read PLL register
718 *
719 * @info: atom card_info pointer
720 * @reg: PLL register offset
721 *
722 * Provides a PLL register accessor for the atom interpreter (r4xx+).
723 * Returns the value of the PLL register.
724 */
725static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
726{
727 return 0;
728}
729
730/**
731 * cail_pll_write - write PLL register
732 *
733 * @info: atom card_info pointer
734 * @reg: PLL register offset
735 * @val: value to write to the pll register
736 *
737 * Provides a PLL register accessor for the atom interpreter (r4xx+).
738 */
739static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
740{
741
742}
743
744/**
745 * cail_mc_read - read MC (Memory Controller) register
746 *
747 * @info: atom card_info pointer
748 * @reg: MC register offset
749 *
750 * Provides an MC register accessor for the atom interpreter (r4xx+).
751 * Returns the value of the MC register.
752 */
753static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
754{
755 return 0;
756}
757
758/**
759 * cail_mc_write - write MC (Memory Controller) register
760 *
761 * @info: atom card_info pointer
762 * @reg: MC register offset
763 * @val: value to write to the pll register
764 *
765 * Provides a MC register accessor for the atom interpreter (r4xx+).
766 */
767static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
768{
769
770}
771
772/**
773 * cail_reg_write - write MMIO register
774 *
775 * @info: atom card_info pointer
776 * @reg: MMIO register offset
777 * @val: value to write to the pll register
778 *
779 * Provides a MMIO register accessor for the atom interpreter (r4xx+).
780 */
781static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
782{
783 struct amdgpu_device *adev = info->dev->dev_private;
784
785 WREG32(reg, val);
786}
787
788/**
789 * cail_reg_read - read MMIO register
790 *
791 * @info: atom card_info pointer
792 * @reg: MMIO register offset
793 *
794 * Provides an MMIO register accessor for the atom interpreter (r4xx+).
795 * Returns the value of the MMIO register.
796 */
797static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
798{
799 struct amdgpu_device *adev = info->dev->dev_private;
800 uint32_t r;
801
802 r = RREG32(reg);
803 return r;
804}
805
806/**
807 * cail_ioreg_write - write IO register
808 *
809 * @info: atom card_info pointer
810 * @reg: IO register offset
811 * @val: value to write to the pll register
812 *
813 * Provides a IO register accessor for the atom interpreter (r4xx+).
814 */
815static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
816{
817 struct amdgpu_device *adev = info->dev->dev_private;
818
819 WREG32_IO(reg, val);
820}
821
822/**
823 * cail_ioreg_read - read IO register
824 *
825 * @info: atom card_info pointer
826 * @reg: IO register offset
827 *
828 * Provides an IO register accessor for the atom interpreter (r4xx+).
829 * Returns the value of the IO register.
830 */
831static uint32_t cail_ioreg_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_IO(reg);
837 return r;
838}
839
840/**
841 * amdgpu_atombios_fini - free the driver info and callbacks for atombios
842 *
843 * @adev: amdgpu_device pointer
844 *
845 * Frees the driver info and register access callbacks for the ATOM
846 * interpreter (r4xx+).
847 * Called at driver shutdown.
848 */
849static void amdgpu_atombios_fini(struct amdgpu_device *adev)
850{
Monk Liu89e0ec9f2016-05-27 19:34:11 +0800851 if (adev->mode_info.atom_context) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400852 kfree(adev->mode_info.atom_context->scratch);
Monk Liu89e0ec9f2016-05-27 19:34:11 +0800853 kfree(adev->mode_info.atom_context->iio);
854 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400855 kfree(adev->mode_info.atom_context);
856 adev->mode_info.atom_context = NULL;
857 kfree(adev->mode_info.atom_card_info);
858 adev->mode_info.atom_card_info = NULL;
859}
860
861/**
862 * amdgpu_atombios_init - init the driver info and callbacks for atombios
863 *
864 * @adev: amdgpu_device pointer
865 *
866 * Initializes the driver info and register access callbacks for the
867 * ATOM interpreter (r4xx+).
868 * Returns 0 on sucess, -ENOMEM on failure.
869 * Called at driver startup.
870 */
871static int amdgpu_atombios_init(struct amdgpu_device *adev)
872{
873 struct card_info *atom_card_info =
874 kzalloc(sizeof(struct card_info), GFP_KERNEL);
875
876 if (!atom_card_info)
877 return -ENOMEM;
878
879 adev->mode_info.atom_card_info = atom_card_info;
880 atom_card_info->dev = adev->ddev;
881 atom_card_info->reg_read = cail_reg_read;
882 atom_card_info->reg_write = cail_reg_write;
883 /* needed for iio ops */
884 if (adev->rio_mem) {
885 atom_card_info->ioreg_read = cail_ioreg_read;
886 atom_card_info->ioreg_write = cail_ioreg_write;
887 } else {
Amber Linb64a18c2017-01-04 08:06:58 -0500888 DRM_INFO("PCI I/O BAR is not found. Using MMIO to access ATOM BIOS\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400889 atom_card_info->ioreg_read = cail_reg_read;
890 atom_card_info->ioreg_write = cail_reg_write;
891 }
892 atom_card_info->mc_read = cail_mc_read;
893 atom_card_info->mc_write = cail_mc_write;
894 atom_card_info->pll_read = cail_pll_read;
895 atom_card_info->pll_write = cail_pll_write;
896
897 adev->mode_info.atom_context = amdgpu_atom_parse(atom_card_info, adev->bios);
898 if (!adev->mode_info.atom_context) {
899 amdgpu_atombios_fini(adev);
900 return -ENOMEM;
901 }
902
903 mutex_init(&adev->mode_info.atom_context->mutex);
904 amdgpu_atombios_scratch_regs_init(adev);
905 amdgpu_atom_allocate_fb_scratch(adev->mode_info.atom_context);
906 return 0;
907}
908
909/* if we get transitioned to only one device, take VGA back */
910/**
911 * amdgpu_vga_set_decode - enable/disable vga decode
912 *
913 * @cookie: amdgpu_device pointer
914 * @state: enable/disable vga decode
915 *
916 * Enable/disable vga decode (all asics).
917 * Returns VGA resource flags.
918 */
919static unsigned int amdgpu_vga_set_decode(void *cookie, bool state)
920{
921 struct amdgpu_device *adev = cookie;
922 amdgpu_asic_set_vga_state(adev, state);
923 if (state)
924 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
925 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
926 else
927 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
928}
929
930/**
931 * amdgpu_check_pot_argument - check that argument is a power of two
932 *
933 * @arg: value to check
934 *
935 * Validates that a certain argument is a power of two (all asics).
936 * Returns true if argument is valid.
937 */
938static bool amdgpu_check_pot_argument(int arg)
939{
940 return (arg & (arg - 1)) == 0;
941}
942
943/**
944 * amdgpu_check_arguments - validate module params
945 *
946 * @adev: amdgpu_device pointer
947 *
948 * Validates certain module parameters and updates
949 * the associated values used by the driver (all asics).
950 */
951static void amdgpu_check_arguments(struct amdgpu_device *adev)
952{
Chunming Zhou5b011232015-12-10 17:34:33 +0800953 if (amdgpu_sched_jobs < 4) {
954 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
955 amdgpu_sched_jobs);
956 amdgpu_sched_jobs = 4;
957 } else if (!amdgpu_check_pot_argument(amdgpu_sched_jobs)){
958 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
959 amdgpu_sched_jobs);
960 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
961 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400962
963 if (amdgpu_gart_size != -1) {
Christian Königc4e1a132016-03-17 16:25:15 +0100964 /* gtt size must be greater or equal to 32M */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400965 if (amdgpu_gart_size < 32) {
966 dev_warn(adev->dev, "gart size (%d) too small\n",
967 amdgpu_gart_size);
968 amdgpu_gart_size = -1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400969 }
970 }
971
972 if (!amdgpu_check_pot_argument(amdgpu_vm_size)) {
973 dev_warn(adev->dev, "VM size (%d) must be a power of 2\n",
974 amdgpu_vm_size);
Alex Deucher8dacc122015-05-11 16:20:58 -0400975 amdgpu_vm_size = 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400976 }
977
978 if (amdgpu_vm_size < 1) {
979 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
980 amdgpu_vm_size);
Alex Deucher8dacc122015-05-11 16:20:58 -0400981 amdgpu_vm_size = 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400982 }
983
984 /*
985 * Max GPUVM size for Cayman, SI and CI are 40 bits.
986 */
987 if (amdgpu_vm_size > 1024) {
988 dev_warn(adev->dev, "VM size (%d) too large, max is 1TB\n",
989 amdgpu_vm_size);
Alex Deucher8dacc122015-05-11 16:20:58 -0400990 amdgpu_vm_size = 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400991 }
992
993 /* defines number of bits in page table versus page directory,
994 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
995 * page table and the remaining bits are in the page directory */
996 if (amdgpu_vm_block_size == -1) {
997
998 /* Total bits covered by PD + PTs */
999 unsigned bits = ilog2(amdgpu_vm_size) + 18;
1000
1001 /* Make sure the PD is 4K in size up to 8GB address space.
1002 Above that split equal between PD and PTs */
1003 if (amdgpu_vm_size <= 8)
1004 amdgpu_vm_block_size = bits - 9;
1005 else
1006 amdgpu_vm_block_size = (bits + 3) / 2;
1007
1008 } else if (amdgpu_vm_block_size < 9) {
1009 dev_warn(adev->dev, "VM page table size (%d) too small\n",
1010 amdgpu_vm_block_size);
1011 amdgpu_vm_block_size = 9;
1012 }
1013
1014 if (amdgpu_vm_block_size > 24 ||
1015 (amdgpu_vm_size * 1024) < (1ull << amdgpu_vm_block_size)) {
1016 dev_warn(adev->dev, "VM page table size (%d) too large\n",
1017 amdgpu_vm_block_size);
1018 amdgpu_vm_block_size = 9;
1019 }
Christian König6a7f76e2016-08-24 15:51:49 +02001020
jimqu526bae32016-11-07 09:53:10 +08001021 if (amdgpu_vram_page_split != -1 && (amdgpu_vram_page_split < 16 ||
1022 !amdgpu_check_pot_argument(amdgpu_vram_page_split))) {
Christian König6a7f76e2016-08-24 15:51:49 +02001023 dev_warn(adev->dev, "invalid VRAM page split (%d)\n",
1024 amdgpu_vram_page_split);
1025 amdgpu_vram_page_split = 1024;
1026 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001027}
1028
1029/**
1030 * amdgpu_switcheroo_set_state - set switcheroo state
1031 *
1032 * @pdev: pci dev pointer
Lukas Wunner16944672015-09-05 11:17:35 +02001033 * @state: vga_switcheroo state
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001034 *
1035 * Callback for the switcheroo driver. Suspends or resumes the
1036 * the asics before or after it is powered up using ACPI methods.
1037 */
1038static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1039{
1040 struct drm_device *dev = pci_get_drvdata(pdev);
1041
1042 if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
1043 return;
1044
1045 if (state == VGA_SWITCHEROO_ON) {
1046 unsigned d3_delay = dev->pdev->d3_delay;
1047
1048 printk(KERN_INFO "amdgpu: switched on\n");
1049 /* don't suspend or resume card normally */
1050 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1051
Alex Deucher810ddc32016-08-23 13:25:49 -04001052 amdgpu_device_resume(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001053
1054 dev->pdev->d3_delay = d3_delay;
1055
1056 dev->switch_power_state = DRM_SWITCH_POWER_ON;
1057 drm_kms_helper_poll_enable(dev);
1058 } else {
1059 printk(KERN_INFO "amdgpu: switched off\n");
1060 drm_kms_helper_poll_disable(dev);
1061 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Alex Deucher810ddc32016-08-23 13:25:49 -04001062 amdgpu_device_suspend(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001063 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1064 }
1065}
1066
1067/**
1068 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1069 *
1070 * @pdev: pci dev pointer
1071 *
1072 * Callback for the switcheroo driver. Check of the switcheroo
1073 * state can be changed.
1074 * Returns true if the state can be changed, false if not.
1075 */
1076static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1077{
1078 struct drm_device *dev = pci_get_drvdata(pdev);
1079
1080 /*
1081 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1082 * locking inversion with the driver load path. And the access here is
1083 * completely racy anyway. So don't bother with locking for now.
1084 */
1085 return dev->open_count == 0;
1086}
1087
1088static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1089 .set_gpu_state = amdgpu_switcheroo_set_state,
1090 .reprobe = NULL,
1091 .can_switch = amdgpu_switcheroo_can_switch,
1092};
1093
1094int amdgpu_set_clockgating_state(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001095 enum amd_ip_block_type block_type,
1096 enum amd_clockgating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001097{
1098 int i, r = 0;
1099
1100 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001101 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001102 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001103 if (adev->ip_blocks[i].version->type == block_type) {
1104 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1105 state);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001106 if (r)
1107 return r;
Alex Deuchera225bf12016-06-23 11:48:30 -04001108 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001109 }
1110 }
1111 return r;
1112}
1113
1114int amdgpu_set_powergating_state(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001115 enum amd_ip_block_type block_type,
1116 enum amd_powergating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001117{
1118 int i, r = 0;
1119
1120 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001121 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001122 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001123 if (adev->ip_blocks[i].version->type == block_type) {
1124 r = adev->ip_blocks[i].version->funcs->set_powergating_state((void *)adev,
1125 state);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001126 if (r)
1127 return r;
Alex Deuchera225bf12016-06-23 11:48:30 -04001128 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001129 }
1130 }
1131 return r;
1132}
1133
Huang Rui6cb2d4e2017-01-05 18:44:41 +08001134void amdgpu_get_clockgating_state(struct amdgpu_device *adev, u32 *flags)
1135{
1136 int i;
1137
1138 for (i = 0; i < adev->num_ip_blocks; i++) {
1139 if (!adev->ip_blocks[i].status.valid)
1140 continue;
1141 if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
1142 adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
1143 }
1144}
1145
Alex Deucher5dbbb602016-06-23 11:41:04 -04001146int amdgpu_wait_for_idle(struct amdgpu_device *adev,
1147 enum amd_ip_block_type block_type)
1148{
1149 int i, r;
1150
1151 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001152 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001153 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001154 if (adev->ip_blocks[i].version->type == block_type) {
1155 r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -04001156 if (r)
1157 return r;
1158 break;
1159 }
1160 }
1161 return 0;
1162
1163}
1164
1165bool amdgpu_is_idle(struct amdgpu_device *adev,
1166 enum amd_ip_block_type block_type)
1167{
1168 int i;
1169
1170 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001171 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001172 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001173 if (adev->ip_blocks[i].version->type == block_type)
1174 return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -04001175 }
1176 return true;
1177
1178}
1179
Alex Deuchera1255102016-10-13 17:41:13 -04001180struct amdgpu_ip_block * amdgpu_get_ip_block(struct amdgpu_device *adev,
1181 enum amd_ip_block_type type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001182{
1183 int i;
1184
1185 for (i = 0; i < adev->num_ip_blocks; i++)
Alex Deuchera1255102016-10-13 17:41:13 -04001186 if (adev->ip_blocks[i].version->type == type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001187 return &adev->ip_blocks[i];
1188
1189 return NULL;
1190}
1191
1192/**
1193 * amdgpu_ip_block_version_cmp
1194 *
1195 * @adev: amdgpu_device pointer
yanyang15fc3aee2015-05-22 14:39:35 -04001196 * @type: enum amd_ip_block_type
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001197 * @major: major version
1198 * @minor: minor version
1199 *
1200 * return 0 if equal or greater
1201 * return 1 if smaller or the ip_block doesn't exist
1202 */
1203int amdgpu_ip_block_version_cmp(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001204 enum amd_ip_block_type type,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001205 u32 major, u32 minor)
1206{
Alex Deuchera1255102016-10-13 17:41:13 -04001207 struct amdgpu_ip_block *ip_block = amdgpu_get_ip_block(adev, type);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001208
Alex Deuchera1255102016-10-13 17:41:13 -04001209 if (ip_block && ((ip_block->version->major > major) ||
1210 ((ip_block->version->major == major) &&
1211 (ip_block->version->minor >= minor))))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001212 return 0;
1213
1214 return 1;
1215}
1216
Alex Deuchera1255102016-10-13 17:41:13 -04001217/**
1218 * amdgpu_ip_block_add
1219 *
1220 * @adev: amdgpu_device pointer
1221 * @ip_block_version: pointer to the IP to add
1222 *
1223 * Adds the IP block driver information to the collection of IPs
1224 * on the asic.
1225 */
1226int amdgpu_ip_block_add(struct amdgpu_device *adev,
1227 const struct amdgpu_ip_block_version *ip_block_version)
1228{
1229 if (!ip_block_version)
1230 return -EINVAL;
1231
1232 adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
1233
1234 return 0;
1235}
1236
Alex Deucher483ef982016-09-30 12:43:04 -04001237static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
Emily Deng9accf2f2016-08-10 16:01:25 +08001238{
1239 adev->enable_virtual_display = false;
1240
1241 if (amdgpu_virtual_display) {
1242 struct drm_device *ddev = adev->ddev;
1243 const char *pci_address_name = pci_name(ddev->pdev);
Emily Deng0f663562016-09-30 13:02:18 -04001244 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
Emily Deng9accf2f2016-08-10 16:01:25 +08001245
1246 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1247 pciaddstr_tmp = pciaddstr;
Emily Deng0f663562016-09-30 13:02:18 -04001248 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1249 pciaddname = strsep(&pciaddname_tmp, ",");
Emily Deng9accf2f2016-08-10 16:01:25 +08001250 if (!strcmp(pci_address_name, pciaddname)) {
Emily Deng0f663562016-09-30 13:02:18 -04001251 long num_crtc;
1252 int res = -1;
1253
Emily Deng9accf2f2016-08-10 16:01:25 +08001254 adev->enable_virtual_display = true;
Emily Deng0f663562016-09-30 13:02:18 -04001255
1256 if (pciaddname_tmp)
1257 res = kstrtol(pciaddname_tmp, 10,
1258 &num_crtc);
1259
1260 if (!res) {
1261 if (num_crtc < 1)
1262 num_crtc = 1;
1263 if (num_crtc > 6)
1264 num_crtc = 6;
1265 adev->mode_info.num_crtc = num_crtc;
1266 } else {
1267 adev->mode_info.num_crtc = 1;
1268 }
Emily Deng9accf2f2016-08-10 16:01:25 +08001269 break;
1270 }
1271 }
1272
Emily Deng0f663562016-09-30 13:02:18 -04001273 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
1274 amdgpu_virtual_display, pci_address_name,
1275 adev->enable_virtual_display, adev->mode_info.num_crtc);
Emily Deng9accf2f2016-08-10 16:01:25 +08001276
1277 kfree(pciaddstr);
1278 }
1279}
1280
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001281static int amdgpu_early_init(struct amdgpu_device *adev)
1282{
Alex Deucheraaa36a92015-04-20 17:31:14 -04001283 int i, r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001284
Alex Deucher483ef982016-09-30 12:43:04 -04001285 amdgpu_device_enable_virtual_display(adev);
Emily Denga6be7572016-08-08 11:37:50 +08001286
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001287 switch (adev->asic_type) {
Alex Deucheraaa36a92015-04-20 17:31:14 -04001288 case CHIP_TOPAZ:
1289 case CHIP_TONGA:
David Zhang48299f92015-07-08 01:05:16 +08001290 case CHIP_FIJI:
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001291 case CHIP_POLARIS11:
1292 case CHIP_POLARIS10:
Junwei Zhangc4642a42016-12-14 15:32:28 -05001293 case CHIP_POLARIS12:
Alex Deucheraaa36a92015-04-20 17:31:14 -04001294 case CHIP_CARRIZO:
Samuel Li39bb0c92015-10-08 16:31:43 -04001295 case CHIP_STONEY:
1296 if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
Alex Deucheraaa36a92015-04-20 17:31:14 -04001297 adev->family = AMDGPU_FAMILY_CZ;
1298 else
1299 adev->family = AMDGPU_FAMILY_VI;
1300
1301 r = vi_set_ip_blocks(adev);
1302 if (r)
1303 return r;
1304 break;
Ken Wang33f34802016-01-21 17:29:41 +08001305#ifdef CONFIG_DRM_AMDGPU_SI
1306 case CHIP_VERDE:
1307 case CHIP_TAHITI:
1308 case CHIP_PITCAIRN:
1309 case CHIP_OLAND:
1310 case CHIP_HAINAN:
Ken Wang295d0da2016-05-24 21:02:53 +08001311 adev->family = AMDGPU_FAMILY_SI;
Ken Wang33f34802016-01-21 17:29:41 +08001312 r = si_set_ip_blocks(adev);
1313 if (r)
1314 return r;
1315 break;
1316#endif
Alex Deuchera2e73f52015-04-20 17:09:27 -04001317#ifdef CONFIG_DRM_AMDGPU_CIK
1318 case CHIP_BONAIRE:
1319 case CHIP_HAWAII:
1320 case CHIP_KAVERI:
1321 case CHIP_KABINI:
1322 case CHIP_MULLINS:
1323 if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
1324 adev->family = AMDGPU_FAMILY_CI;
1325 else
1326 adev->family = AMDGPU_FAMILY_KV;
1327
1328 r = cik_set_ip_blocks(adev);
1329 if (r)
1330 return r;
1331 break;
1332#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001333 default:
1334 /* FIXME: not supported yet */
1335 return -EINVAL;
1336 }
1337
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001338 for (i = 0; i < adev->num_ip_blocks; i++) {
1339 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
1340 DRM_ERROR("disabled ip block: %d\n", i);
Alex Deuchera1255102016-10-13 17:41:13 -04001341 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001342 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001343 if (adev->ip_blocks[i].version->funcs->early_init) {
1344 r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001345 if (r == -ENOENT) {
Alex Deuchera1255102016-10-13 17:41:13 -04001346 adev->ip_blocks[i].status.valid = false;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001347 } else if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001348 DRM_ERROR("early_init of IP block <%s> failed %d\n",
1349 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001350 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001351 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001352 adev->ip_blocks[i].status.valid = true;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001353 }
Alex Deucher974e6b62015-07-10 13:59:44 -04001354 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001355 adev->ip_blocks[i].status.valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001356 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001357 }
1358 }
1359
Nicolai Hähnle395d1fb2016-06-02 12:32:07 +02001360 adev->cg_flags &= amdgpu_cg_mask;
1361 adev->pg_flags &= amdgpu_pg_mask;
1362
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001363 return 0;
1364}
1365
1366static int amdgpu_init(struct amdgpu_device *adev)
1367{
1368 int i, r;
1369
1370 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001371 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001372 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001373 r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001374 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001375 DRM_ERROR("sw_init of IP block <%s> failed %d\n",
1376 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001377 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001378 }
Alex Deuchera1255102016-10-13 17:41:13 -04001379 adev->ip_blocks[i].status.sw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001380 /* need to do gmc hw init early so we can allocate gpu mem */
Alex Deuchera1255102016-10-13 17:41:13 -04001381 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001382 r = amdgpu_vram_scratch_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001383 if (r) {
1384 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001385 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001386 }
Alex Deuchera1255102016-10-13 17:41:13 -04001387 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001388 if (r) {
1389 DRM_ERROR("hw_init %d failed %d\n", i, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001390 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001391 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001392 r = amdgpu_wb_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001393 if (r) {
1394 DRM_ERROR("amdgpu_wb_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001395 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001396 }
Alex Deuchera1255102016-10-13 17:41:13 -04001397 adev->ip_blocks[i].status.hw = true;
Monk Liu24936642017-01-09 15:54:32 +08001398
1399 /* right after GMC hw init, we create CSA */
1400 if (amdgpu_sriov_vf(adev)) {
1401 r = amdgpu_allocate_static_csa(adev);
1402 if (r) {
1403 DRM_ERROR("allocate CSA failed %d\n", r);
1404 return r;
1405 }
1406 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001407 }
1408 }
1409
1410 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001411 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001412 continue;
1413 /* gmc hw init is done early */
Alex Deuchera1255102016-10-13 17:41:13 -04001414 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001415 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001416 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001417 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001418 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
1419 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001420 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001421 }
Alex Deuchera1255102016-10-13 17:41:13 -04001422 adev->ip_blocks[i].status.hw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001423 }
1424
1425 return 0;
1426}
1427
1428static int amdgpu_late_init(struct amdgpu_device *adev)
1429{
1430 int i = 0, r;
1431
1432 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001433 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001434 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001435 if (adev->ip_blocks[i].version->funcs->late_init) {
1436 r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001437 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001438 DRM_ERROR("late_init of IP block <%s> failed %d\n",
1439 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001440 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001441 }
Alex Deuchera1255102016-10-13 17:41:13 -04001442 adev->ip_blocks[i].status.late_initialized = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001443 }
Alex Deucher4a446d52016-10-07 14:48:18 -04001444 /* skip CG for VCE/UVD, it's handled specially */
Alex Deuchera1255102016-10-13 17:41:13 -04001445 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1446 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
Alex Deucher4a446d52016-10-07 14:48:18 -04001447 /* enable clockgating to save power */
Alex Deuchera1255102016-10-13 17:41:13 -04001448 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1449 AMD_CG_STATE_GATE);
Alex Deucher4a446d52016-10-07 14:48:18 -04001450 if (r) {
1451 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001452 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher4a446d52016-10-07 14:48:18 -04001453 return r;
1454 }
Arindam Nathb0b00ff2016-10-07 19:01:37 +05301455 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001456 }
1457
1458 return 0;
1459}
1460
1461static int amdgpu_fini(struct amdgpu_device *adev)
1462{
1463 int i, r;
1464
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001465 /* need to disable SMC first */
1466 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001467 if (!adev->ip_blocks[i].status.hw)
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001468 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001469 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001470 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
Alex Deuchera1255102016-10-13 17:41:13 -04001471 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1472 AMD_CG_STATE_UNGATE);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001473 if (r) {
1474 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001475 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001476 return r;
1477 }
Alex Deuchera1255102016-10-13 17:41:13 -04001478 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001479 /* XXX handle errors */
1480 if (r) {
1481 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001482 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001483 }
Alex Deuchera1255102016-10-13 17:41:13 -04001484 adev->ip_blocks[i].status.hw = false;
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001485 break;
1486 }
1487 }
1488
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001489 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001490 if (!adev->ip_blocks[i].status.hw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001491 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001492 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001493 amdgpu_wb_fini(adev);
1494 amdgpu_vram_scratch_fini(adev);
1495 }
Rex Zhu8201a672016-11-24 21:44:44 +08001496
1497 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1498 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1499 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
1500 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1501 AMD_CG_STATE_UNGATE);
1502 if (r) {
1503 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1504 adev->ip_blocks[i].version->funcs->name, r);
1505 return r;
1506 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001507 }
Rex Zhu8201a672016-11-24 21:44:44 +08001508
Alex Deuchera1255102016-10-13 17:41:13 -04001509 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001510 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001511 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001512 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
1513 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001514 }
Rex Zhu8201a672016-11-24 21:44:44 +08001515
Alex Deuchera1255102016-10-13 17:41:13 -04001516 adev->ip_blocks[i].status.hw = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001517 }
1518
1519 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001520 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001521 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001522 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001523 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001524 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001525 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
1526 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001527 }
Alex Deuchera1255102016-10-13 17:41:13 -04001528 adev->ip_blocks[i].status.sw = false;
1529 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001530 }
1531
Monk Liua6dcfd92016-05-19 14:36:34 +08001532 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001533 if (!adev->ip_blocks[i].status.late_initialized)
Grazvydas Ignotas8a2eef12016-10-03 00:06:44 +03001534 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001535 if (adev->ip_blocks[i].version->funcs->late_fini)
1536 adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
1537 adev->ip_blocks[i].status.late_initialized = false;
Monk Liua6dcfd92016-05-19 14:36:34 +08001538 }
1539
Monk Liu24936642017-01-09 15:54:32 +08001540 if (amdgpu_sriov_vf(adev))
1541 amdgpu_bo_free_kernel(&adev->virt.csa_obj, &adev->virt.csa_vmid0_addr, NULL);
1542
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001543 return 0;
1544}
1545
Alex Deucherfaefba92016-12-06 10:38:29 -05001546int amdgpu_suspend(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001547{
1548 int i, r;
1549
Flora Cuic5a93a22016-02-26 10:45:25 +08001550 /* ungate SMC block first */
1551 r = amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC,
1552 AMD_CG_STATE_UNGATE);
1553 if (r) {
1554 DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n",r);
1555 }
1556
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001557 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001558 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001559 continue;
1560 /* ungate blocks so that suspend can properly shut them down */
Flora Cuic5a93a22016-02-26 10:45:25 +08001561 if (i != AMD_IP_BLOCK_TYPE_SMC) {
Alex Deuchera1255102016-10-13 17:41:13 -04001562 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1563 AMD_CG_STATE_UNGATE);
Flora Cuic5a93a22016-02-26 10:45:25 +08001564 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001565 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1566 adev->ip_blocks[i].version->funcs->name, r);
Flora Cuic5a93a22016-02-26 10:45:25 +08001567 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001568 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001569 /* XXX handle errors */
Alex Deuchera1255102016-10-13 17:41:13 -04001570 r = adev->ip_blocks[i].version->funcs->suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001571 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001572 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001573 DRM_ERROR("suspend of IP block <%s> failed %d\n",
1574 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001575 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001576 }
1577
1578 return 0;
1579}
1580
1581static int amdgpu_resume(struct amdgpu_device *adev)
1582{
1583 int i, r;
1584
1585 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001586 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001587 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001588 r = adev->ip_blocks[i].version->funcs->resume(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001589 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001590 DRM_ERROR("resume of IP block <%s> failed %d\n",
1591 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001592 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001593 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001594 }
1595
1596 return 0;
1597}
1598
Monk Liu4e99a442016-03-31 13:26:59 +08001599static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
Andres Rodriguez048765a2016-06-11 02:51:32 -04001600{
Monk Liu4e99a442016-03-31 13:26:59 +08001601 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
Xiangliang Yu5a5099c2017-01-09 18:06:57 -05001602 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
Andres Rodriguez048765a2016-06-11 02:51:32 -04001603}
1604
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001605/**
1606 * amdgpu_device_init - initialize the driver
1607 *
1608 * @adev: amdgpu_device pointer
1609 * @pdev: drm dev pointer
1610 * @pdev: pci dev pointer
1611 * @flags: driver flags
1612 *
1613 * Initializes the driver info and hw (all asics).
1614 * Returns 0 for success or an error on failure.
1615 * Called at driver startup.
1616 */
1617int amdgpu_device_init(struct amdgpu_device *adev,
1618 struct drm_device *ddev,
1619 struct pci_dev *pdev,
1620 uint32_t flags)
1621{
1622 int r, i;
1623 bool runtime = false;
Marek Olšák95844d22016-08-17 23:49:27 +02001624 u32 max_MBps;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001625
1626 adev->shutdown = false;
1627 adev->dev = &pdev->dev;
1628 adev->ddev = ddev;
1629 adev->pdev = pdev;
1630 adev->flags = flags;
Jammy Zhou2f7d10b2015-07-22 11:29:01 +08001631 adev->asic_type = flags & AMD_ASIC_MASK;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001632 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
1633 adev->mc.gtt_size = 512 * 1024 * 1024;
1634 adev->accel_working = false;
1635 adev->num_rings = 0;
1636 adev->mman.buffer_funcs = NULL;
1637 adev->mman.buffer_funcs_ring = NULL;
1638 adev->vm_manager.vm_pte_funcs = NULL;
Christian König2d55e452016-02-08 17:37:38 +01001639 adev->vm_manager.vm_pte_num_rings = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001640 adev->gart.gart_funcs = NULL;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001641 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001642
1643 adev->smc_rreg = &amdgpu_invalid_rreg;
1644 adev->smc_wreg = &amdgpu_invalid_wreg;
1645 adev->pcie_rreg = &amdgpu_invalid_rreg;
1646 adev->pcie_wreg = &amdgpu_invalid_wreg;
Huang Rui36b9a952016-08-31 13:23:25 +08001647 adev->pciep_rreg = &amdgpu_invalid_rreg;
1648 adev->pciep_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001649 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
1650 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
1651 adev->didt_rreg = &amdgpu_invalid_rreg;
1652 adev->didt_wreg = &amdgpu_invalid_wreg;
Rex Zhuccdbb202016-06-08 12:47:41 +08001653 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
1654 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001655 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
1656 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
1657
Rex Zhuccdbb202016-06-08 12:47:41 +08001658
Alex Deucher3e39ab92015-06-05 15:04:33 -04001659 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
1660 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
1661 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001662
1663 /* mutex initialization are all done here so we
1664 * can recall function without having locking issues */
Christian König8d0a7ce2015-11-03 20:58:50 +01001665 mutex_init(&adev->vm_manager.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001666 atomic_set(&adev->irq.ih.lock, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001667 mutex_init(&adev->pm.mutex);
1668 mutex_init(&adev->gfx.gpu_clock_mutex);
1669 mutex_init(&adev->srbm_mutex);
1670 mutex_init(&adev->grbm_idx_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001671 mutex_init(&adev->mn_lock);
1672 hash_init(adev->mn_hash);
1673
1674 amdgpu_check_arguments(adev);
1675
1676 /* Registers mapping */
1677 /* TODO: block userspace mapping of io register */
1678 spin_lock_init(&adev->mmio_idx_lock);
1679 spin_lock_init(&adev->smc_idx_lock);
1680 spin_lock_init(&adev->pcie_idx_lock);
1681 spin_lock_init(&adev->uvd_ctx_idx_lock);
1682 spin_lock_init(&adev->didt_idx_lock);
Rex Zhuccdbb202016-06-08 12:47:41 +08001683 spin_lock_init(&adev->gc_cac_idx_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001684 spin_lock_init(&adev->audio_endpt_idx_lock);
Marek Olšák95844d22016-08-17 23:49:27 +02001685 spin_lock_init(&adev->mm_stats.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001686
Chunming Zhou0c4e7fa2016-08-17 11:41:30 +08001687 INIT_LIST_HEAD(&adev->shadow_list);
1688 mutex_init(&adev->shadow_list_lock);
1689
Chunming Zhou5c1354b2016-08-30 16:13:10 +08001690 INIT_LIST_HEAD(&adev->gtt_list);
1691 spin_lock_init(&adev->gtt_list_lock);
1692
Ken Wangda69c1612016-01-21 19:08:55 +08001693 if (adev->asic_type >= CHIP_BONAIRE) {
1694 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
1695 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
1696 } else {
1697 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
1698 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
1699 }
Chunming Zhou5c1354b2016-08-30 16:13:10 +08001700
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001701 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
1702 if (adev->rmmio == NULL) {
1703 return -ENOMEM;
1704 }
1705 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
1706 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
1707
Ken Wangda69c1612016-01-21 19:08:55 +08001708 if (adev->asic_type >= CHIP_BONAIRE)
1709 /* doorbell bar mapping */
1710 amdgpu_doorbell_init(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001711
1712 /* io port mapping */
1713 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
1714 if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
1715 adev->rio_mem_size = pci_resource_len(adev->pdev, i);
1716 adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
1717 break;
1718 }
1719 }
1720 if (adev->rio_mem == NULL)
Amber Linb64a18c2017-01-04 08:06:58 -05001721 DRM_INFO("PCI I/O BAR is not found.\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001722
1723 /* early init functions */
1724 r = amdgpu_early_init(adev);
1725 if (r)
1726 return r;
1727
1728 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
1729 /* this will fail for cards that aren't VGA class devices, just
1730 * ignore it */
1731 vga_client_register(adev->pdev, adev, NULL, amdgpu_vga_set_decode);
1732
1733 if (amdgpu_runtime_pm == 1)
1734 runtime = true;
Alex Deuchere9bef452016-04-25 13:12:18 -04001735 if (amdgpu_device_is_px(ddev))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001736 runtime = true;
1737 vga_switcheroo_register_client(adev->pdev, &amdgpu_switcheroo_ops, runtime);
1738 if (runtime)
1739 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
1740
1741 /* Read BIOS */
Alex Deucher83ba1262016-06-03 18:21:41 -04001742 if (!amdgpu_get_bios(adev)) {
1743 r = -EINVAL;
1744 goto failed;
1745 }
Nils Wallméniusf7e9e9f2016-12-14 21:52:45 +01001746
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001747 r = amdgpu_atombios_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001748 if (r) {
1749 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001750 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001751 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001752
Monk Liu4e99a442016-03-31 13:26:59 +08001753 /* detect if we are with an SRIOV vbios */
1754 amdgpu_device_detect_sriov_bios(adev);
Andres Rodriguez048765a2016-06-11 02:51:32 -04001755
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001756 /* Post card if necessary */
Monk Liubec86372016-09-14 19:38:08 +08001757 if (amdgpu_vpost_needed(adev)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001758 if (!adev->bios) {
Monk Liubec86372016-09-14 19:38:08 +08001759 dev_err(adev->dev, "no vBIOS found\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001760 r = -EINVAL;
1761 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001762 }
Monk Liubec86372016-09-14 19:38:08 +08001763 DRM_INFO("GPU posting now...\n");
Monk Liu4e99a442016-03-31 13:26:59 +08001764 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
1765 if (r) {
1766 dev_err(adev->dev, "gpu post error!\n");
1767 goto failed;
1768 }
1769 } else {
1770 DRM_INFO("GPU post is not needed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001771 }
1772
1773 /* Initialize clocks */
1774 r = amdgpu_atombios_get_clock_info(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001775 if (r) {
1776 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001777 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001778 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001779 /* init i2c buses */
1780 amdgpu_atombios_i2c_init(adev);
1781
1782 /* Fence driver */
1783 r = amdgpu_fence_driver_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001784 if (r) {
1785 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001786 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001787 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001788
1789 /* init the mode config */
1790 drm_mode_config_init(adev->ddev);
1791
1792 r = amdgpu_init(adev);
1793 if (r) {
Alex Deucher2c1a2782015-12-07 17:02:53 -05001794 dev_err(adev->dev, "amdgpu_init failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001795 amdgpu_fini(adev);
Alex Deucher83ba1262016-06-03 18:21:41 -04001796 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001797 }
1798
1799 adev->accel_working = true;
1800
Marek Olšák95844d22016-08-17 23:49:27 +02001801 /* Initialize the buffer migration limit. */
1802 if (amdgpu_moverate >= 0)
1803 max_MBps = amdgpu_moverate;
1804 else
1805 max_MBps = 8; /* Allow 8 MB/s. */
1806 /* Get a log2 for easy divisions. */
1807 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
1808
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001809 amdgpu_fbdev_init(adev);
1810
1811 r = amdgpu_ib_pool_init(adev);
1812 if (r) {
1813 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
Alex Deucher83ba1262016-06-03 18:21:41 -04001814 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001815 }
1816
1817 r = amdgpu_ib_ring_tests(adev);
1818 if (r)
1819 DRM_ERROR("ib ring test failed (%d).\n", r);
1820
1821 r = amdgpu_gem_debugfs_init(adev);
1822 if (r) {
1823 DRM_ERROR("registering gem debugfs failed (%d).\n", r);
1824 }
1825
1826 r = amdgpu_debugfs_regs_init(adev);
1827 if (r) {
1828 DRM_ERROR("registering register debugfs failed (%d).\n", r);
1829 }
1830
Huang Rui50ab2532016-06-12 15:51:09 +08001831 r = amdgpu_debugfs_firmware_init(adev);
1832 if (r) {
1833 DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
1834 return r;
1835 }
1836
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001837 if ((amdgpu_testing & 1)) {
1838 if (adev->accel_working)
1839 amdgpu_test_moves(adev);
1840 else
1841 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
1842 }
1843 if ((amdgpu_testing & 2)) {
1844 if (adev->accel_working)
1845 amdgpu_test_syncing(adev);
1846 else
1847 DRM_INFO("amdgpu: acceleration disabled, skipping sync tests\n");
1848 }
1849 if (amdgpu_benchmarking) {
1850 if (adev->accel_working)
1851 amdgpu_benchmark(adev, amdgpu_benchmarking);
1852 else
1853 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
1854 }
1855
1856 /* enable clockgating, etc. after ib tests, etc. since some blocks require
1857 * explicit gating rather than handling it automatically.
1858 */
1859 r = amdgpu_late_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001860 if (r) {
1861 dev_err(adev->dev, "amdgpu_late_init failed\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001862 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001863 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001864
1865 return 0;
Alex Deucher83ba1262016-06-03 18:21:41 -04001866
1867failed:
1868 if (runtime)
1869 vga_switcheroo_fini_domain_pm_ops(adev->dev);
1870 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001871}
1872
1873static void amdgpu_debugfs_remove_files(struct amdgpu_device *adev);
1874
1875/**
1876 * amdgpu_device_fini - tear down the driver
1877 *
1878 * @adev: amdgpu_device pointer
1879 *
1880 * Tear down the driver info (all asics).
1881 * Called at driver shutdown.
1882 */
1883void amdgpu_device_fini(struct amdgpu_device *adev)
1884{
1885 int r;
1886
1887 DRM_INFO("amdgpu: finishing device.\n");
1888 adev->shutdown = true;
Grazvydas Ignotasa951ed82016-09-25 23:34:48 +03001889 drm_crtc_force_disable_all(adev->ddev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001890 /* evict vram memory */
1891 amdgpu_bo_evict_vram(adev);
1892 amdgpu_ib_pool_fini(adev);
1893 amdgpu_fence_driver_fini(adev);
1894 amdgpu_fbdev_fini(adev);
1895 r = amdgpu_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001896 adev->accel_working = false;
1897 /* free i2c buses */
1898 amdgpu_i2c_fini(adev);
1899 amdgpu_atombios_fini(adev);
1900 kfree(adev->bios);
1901 adev->bios = NULL;
1902 vga_switcheroo_unregister_client(adev->pdev);
Alex Deucher83ba1262016-06-03 18:21:41 -04001903 if (adev->flags & AMD_IS_PX)
1904 vga_switcheroo_fini_domain_pm_ops(adev->dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001905 vga_client_register(adev->pdev, NULL, NULL, NULL);
1906 if (adev->rio_mem)
1907 pci_iounmap(adev->pdev, adev->rio_mem);
1908 adev->rio_mem = NULL;
1909 iounmap(adev->rmmio);
1910 adev->rmmio = NULL;
Ken Wangda69c1612016-01-21 19:08:55 +08001911 if (adev->asic_type >= CHIP_BONAIRE)
1912 amdgpu_doorbell_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001913 amdgpu_debugfs_regs_cleanup(adev);
1914 amdgpu_debugfs_remove_files(adev);
1915}
1916
1917
1918/*
1919 * Suspend & resume.
1920 */
1921/**
Alex Deucher810ddc32016-08-23 13:25:49 -04001922 * amdgpu_device_suspend - initiate device suspend
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001923 *
1924 * @pdev: drm dev pointer
1925 * @state: suspend state
1926 *
1927 * Puts the hw in the suspend state (all asics).
1928 * Returns 0 for success or an error on failure.
1929 * Called at driver suspend.
1930 */
Alex Deucher810ddc32016-08-23 13:25:49 -04001931int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001932{
1933 struct amdgpu_device *adev;
1934 struct drm_crtc *crtc;
1935 struct drm_connector *connector;
Alex Deucher5ceb54c2015-08-05 12:41:48 -04001936 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001937
1938 if (dev == NULL || dev->dev_private == NULL) {
1939 return -ENODEV;
1940 }
1941
1942 adev = dev->dev_private;
1943
1944 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
1945 return 0;
1946
1947 drm_kms_helper_poll_disable(dev);
1948
1949 /* turn off display hw */
Alex Deucher4c7fbc32015-09-23 14:32:06 -04001950 drm_modeset_lock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001951 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1952 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
1953 }
Alex Deucher4c7fbc32015-09-23 14:32:06 -04001954 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001955
Alex Deucher756e6882015-10-08 00:03:36 -04001956 /* unpin the front buffers and cursors */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001957 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Alex Deucher756e6882015-10-08 00:03:36 -04001958 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001959 struct amdgpu_framebuffer *rfb = to_amdgpu_framebuffer(crtc->primary->fb);
1960 struct amdgpu_bo *robj;
1961
Alex Deucher756e6882015-10-08 00:03:36 -04001962 if (amdgpu_crtc->cursor_bo) {
1963 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
1964 r = amdgpu_bo_reserve(aobj, false);
1965 if (r == 0) {
1966 amdgpu_bo_unpin(aobj);
1967 amdgpu_bo_unreserve(aobj);
1968 }
1969 }
1970
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001971 if (rfb == NULL || rfb->obj == NULL) {
1972 continue;
1973 }
1974 robj = gem_to_amdgpu_bo(rfb->obj);
1975 /* don't unpin kernel fb objects */
1976 if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
1977 r = amdgpu_bo_reserve(robj, false);
1978 if (r == 0) {
1979 amdgpu_bo_unpin(robj);
1980 amdgpu_bo_unreserve(robj);
1981 }
1982 }
1983 }
1984 /* evict vram memory */
1985 amdgpu_bo_evict_vram(adev);
1986
Alex Deucher5ceb54c2015-08-05 12:41:48 -04001987 amdgpu_fence_driver_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001988
1989 r = amdgpu_suspend(adev);
1990
Alex Deuchera0a71e42016-10-10 12:41:36 -04001991 /* evict remaining vram memory
1992 * This second call to evict vram is to evict the gart page table
1993 * using the CPU.
1994 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001995 amdgpu_bo_evict_vram(adev);
1996
Alex Deuchere695e772016-10-19 14:40:58 -04001997 amdgpu_atombios_scratch_regs_save(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001998 pci_save_state(dev->pdev);
1999 if (suspend) {
2000 /* Shut down the device */
2001 pci_disable_device(dev->pdev);
2002 pci_set_power_state(dev->pdev, PCI_D3hot);
jimqu74b0b152016-09-07 17:09:12 +08002003 } else {
2004 r = amdgpu_asic_reset(adev);
2005 if (r)
2006 DRM_ERROR("amdgpu asic reset failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002007 }
2008
2009 if (fbcon) {
2010 console_lock();
2011 amdgpu_fbdev_set_suspend(adev, 1);
2012 console_unlock();
2013 }
2014 return 0;
2015}
2016
2017/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002018 * amdgpu_device_resume - initiate device resume
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002019 *
2020 * @pdev: drm dev pointer
2021 *
2022 * Bring the hw back to operating state (all asics).
2023 * Returns 0 for success or an error on failure.
2024 * Called at driver resume.
2025 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002026int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002027{
2028 struct drm_connector *connector;
2029 struct amdgpu_device *adev = dev->dev_private;
Alex Deucher756e6882015-10-08 00:03:36 -04002030 struct drm_crtc *crtc;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002031 int r;
2032
2033 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2034 return 0;
2035
jimqu74b0b152016-09-07 17:09:12 +08002036 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002037 console_lock();
jimqu74b0b152016-09-07 17:09:12 +08002038
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002039 if (resume) {
2040 pci_set_power_state(dev->pdev, PCI_D0);
2041 pci_restore_state(dev->pdev);
jimqu74b0b152016-09-07 17:09:12 +08002042 r = pci_enable_device(dev->pdev);
2043 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002044 if (fbcon)
2045 console_unlock();
jimqu74b0b152016-09-07 17:09:12 +08002046 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002047 }
2048 }
Alex Deuchere695e772016-10-19 14:40:58 -04002049 amdgpu_atombios_scratch_regs_restore(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002050
2051 /* post card */
jimqu74b0b152016-09-07 17:09:12 +08002052 if (!amdgpu_card_posted(adev) || !resume) {
2053 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2054 if (r)
2055 DRM_ERROR("amdgpu asic init failed\n");
2056 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002057
2058 r = amdgpu_resume(adev);
Flora Cuica198522016-02-04 15:10:08 +08002059 if (r)
2060 DRM_ERROR("amdgpu_resume failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002061
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002062 amdgpu_fence_driver_resume(adev);
2063
Flora Cuica198522016-02-04 15:10:08 +08002064 if (resume) {
2065 r = amdgpu_ib_ring_tests(adev);
2066 if (r)
2067 DRM_ERROR("ib ring test failed (%d).\n", r);
2068 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002069
2070 r = amdgpu_late_init(adev);
2071 if (r)
2072 return r;
2073
Alex Deucher756e6882015-10-08 00:03:36 -04002074 /* pin cursors */
2075 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2076 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2077
2078 if (amdgpu_crtc->cursor_bo) {
2079 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
2080 r = amdgpu_bo_reserve(aobj, false);
2081 if (r == 0) {
2082 r = amdgpu_bo_pin(aobj,
2083 AMDGPU_GEM_DOMAIN_VRAM,
2084 &amdgpu_crtc->cursor_addr);
2085 if (r != 0)
2086 DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
2087 amdgpu_bo_unreserve(aobj);
2088 }
2089 }
2090 }
2091
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002092 /* blat the mode back in */
2093 if (fbcon) {
2094 drm_helper_resume_force_mode(dev);
2095 /* turn on display hw */
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002096 drm_modeset_lock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002097 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2098 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
2099 }
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002100 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002101 }
2102
2103 drm_kms_helper_poll_enable(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002104
2105 /*
2106 * Most of the connector probing functions try to acquire runtime pm
2107 * refs to ensure that the GPU is powered on when connector polling is
2108 * performed. Since we're calling this from a runtime PM callback,
2109 * trying to acquire rpm refs will cause us to deadlock.
2110 *
2111 * Since we're guaranteed to be holding the rpm lock, it's safe to
2112 * temporarily disable the rpm helpers so this doesn't deadlock us.
2113 */
2114#ifdef CONFIG_PM
2115 dev->dev->power.disable_depth++;
2116#endif
Alex Deucher54fb2a52015-11-24 14:30:56 -05002117 drm_helper_hpd_irq_event(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002118#ifdef CONFIG_PM
2119 dev->dev->power.disable_depth--;
2120#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002121
2122 if (fbcon) {
2123 amdgpu_fbdev_set_suspend(adev, 0);
2124 console_unlock();
2125 }
2126
2127 return 0;
2128}
2129
Chunming Zhou63fbf422016-07-15 11:19:20 +08002130static bool amdgpu_check_soft_reset(struct amdgpu_device *adev)
2131{
2132 int i;
2133 bool asic_hang = false;
2134
2135 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002136 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou63fbf422016-07-15 11:19:20 +08002137 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002138 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
2139 adev->ip_blocks[i].status.hang =
2140 adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
2141 if (adev->ip_blocks[i].status.hang) {
2142 DRM_INFO("IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
Chunming Zhou63fbf422016-07-15 11:19:20 +08002143 asic_hang = true;
2144 }
2145 }
2146 return asic_hang;
2147}
2148
Baoyou Xie4d446652016-09-18 22:09:35 +08002149static int amdgpu_pre_soft_reset(struct amdgpu_device *adev)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002150{
2151 int i, r = 0;
2152
2153 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002154 if (!adev->ip_blocks[i].status.valid)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002155 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002156 if (adev->ip_blocks[i].status.hang &&
2157 adev->ip_blocks[i].version->funcs->pre_soft_reset) {
2158 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
Chunming Zhoud31a5012016-07-18 10:04:34 +08002159 if (r)
2160 return r;
2161 }
2162 }
2163
2164 return 0;
2165}
2166
Chunming Zhou35d782f2016-07-15 15:57:13 +08002167static bool amdgpu_need_full_reset(struct amdgpu_device *adev)
2168{
Alex Deucherda146d32016-10-13 16:07:03 -04002169 int i;
2170
2171 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002172 if (!adev->ip_blocks[i].status.valid)
Alex Deucherda146d32016-10-13 16:07:03 -04002173 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002174 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
2175 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
2176 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
2177 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE)) {
2178 if (adev->ip_blocks[i].status.hang) {
Alex Deucherda146d32016-10-13 16:07:03 -04002179 DRM_INFO("Some block need full reset!\n");
2180 return true;
2181 }
2182 }
Chunming Zhou35d782f2016-07-15 15:57:13 +08002183 }
2184 return false;
2185}
2186
2187static int amdgpu_soft_reset(struct amdgpu_device *adev)
2188{
2189 int i, r = 0;
2190
2191 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002192 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002193 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002194 if (adev->ip_blocks[i].status.hang &&
2195 adev->ip_blocks[i].version->funcs->soft_reset) {
2196 r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002197 if (r)
2198 return r;
2199 }
2200 }
2201
2202 return 0;
2203}
2204
2205static int amdgpu_post_soft_reset(struct amdgpu_device *adev)
2206{
2207 int i, r = 0;
2208
2209 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002210 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002211 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002212 if (adev->ip_blocks[i].status.hang &&
2213 adev->ip_blocks[i].version->funcs->post_soft_reset)
2214 r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002215 if (r)
2216 return r;
2217 }
2218
2219 return 0;
2220}
2221
Chunming Zhou3ad81f12016-08-05 17:30:17 +08002222bool amdgpu_need_backup(struct amdgpu_device *adev)
2223{
2224 if (adev->flags & AMD_IS_APU)
2225 return false;
2226
2227 return amdgpu_lockup_timeout > 0 ? true : false;
2228}
2229
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002230static int amdgpu_recover_vram_from_shadow(struct amdgpu_device *adev,
2231 struct amdgpu_ring *ring,
2232 struct amdgpu_bo *bo,
Chris Wilsonf54d1862016-10-25 13:00:45 +01002233 struct dma_fence **fence)
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002234{
2235 uint32_t domain;
2236 int r;
2237
2238 if (!bo->shadow)
2239 return 0;
2240
2241 r = amdgpu_bo_reserve(bo, false);
2242 if (r)
2243 return r;
2244 domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
2245 /* if bo has been evicted, then no need to recover */
2246 if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
2247 r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
2248 NULL, fence, true);
2249 if (r) {
2250 DRM_ERROR("recover page table failed!\n");
2251 goto err;
2252 }
2253 }
2254err:
2255 amdgpu_bo_unreserve(bo);
2256 return r;
2257}
2258
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002259/**
2260 * amdgpu_gpu_reset - reset the asic
2261 *
2262 * @adev: amdgpu device pointer
2263 *
2264 * Attempt the reset the GPU if it has hung (all asics).
2265 * Returns 0 for success or an error on failure.
2266 */
2267int amdgpu_gpu_reset(struct amdgpu_device *adev)
2268{
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002269 int i, r;
2270 int resched;
Chunming Zhou35d782f2016-07-15 15:57:13 +08002271 bool need_full_reset;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002272
Chunming Zhou63fbf422016-07-15 11:19:20 +08002273 if (!amdgpu_check_soft_reset(adev)) {
2274 DRM_INFO("No hardware hang detected. Did some blocks stall?\n");
2275 return 0;
2276 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002277
Marek Olšákd94aed52015-05-05 21:13:49 +02002278 atomic_inc(&adev->gpu_reset_counter);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002279
Chunming Zhoua3c47d62016-06-30 16:44:41 +08002280 /* block TTM */
2281 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
2282
Chunming Zhou0875dc92016-06-12 15:41:58 +08002283 /* block scheduler */
2284 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2285 struct amdgpu_ring *ring = adev->rings[i];
2286
2287 if (!ring)
2288 continue;
2289 kthread_park(ring->sched.thread);
Chunming Zhouaa1c8902016-06-30 13:56:02 +08002290 amd_sched_hw_job_reset(&ring->sched);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002291 }
Chunming Zhou2200eda2016-06-30 16:53:02 +08002292 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
2293 amdgpu_fence_driver_force_completion(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002294
Chunming Zhou35d782f2016-07-15 15:57:13 +08002295 need_full_reset = amdgpu_need_full_reset(adev);
2296
2297 if (!need_full_reset) {
2298 amdgpu_pre_soft_reset(adev);
2299 r = amdgpu_soft_reset(adev);
2300 amdgpu_post_soft_reset(adev);
2301 if (r || amdgpu_check_soft_reset(adev)) {
2302 DRM_INFO("soft reset failed, will fallback to full reset!\n");
2303 need_full_reset = true;
2304 }
2305 }
2306
2307 if (need_full_reset) {
Chunming Zhou35d782f2016-07-15 15:57:13 +08002308 r = amdgpu_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002309
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002310retry:
Chunming Zhou35d782f2016-07-15 15:57:13 +08002311 /* Disable fb access */
2312 if (adev->mode_info.num_crtc) {
2313 struct amdgpu_mode_mc_save save;
2314 amdgpu_display_stop_mc_access(adev, &save);
2315 amdgpu_wait_for_idle(adev, AMD_IP_BLOCK_TYPE_GMC);
2316 }
Alex Deuchere695e772016-10-19 14:40:58 -04002317 amdgpu_atombios_scratch_regs_save(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002318 r = amdgpu_asic_reset(adev);
Alex Deuchere695e772016-10-19 14:40:58 -04002319 amdgpu_atombios_scratch_regs_restore(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002320 /* post card */
2321 amdgpu_atom_asic_init(adev->mode_info.atom_context);
Alex Deucherbfa99262016-01-15 11:59:48 -05002322
Chunming Zhou35d782f2016-07-15 15:57:13 +08002323 if (!r) {
2324 dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
2325 r = amdgpu_resume(adev);
2326 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002327 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002328 if (!r) {
Chunming Zhoue72cfd52016-07-27 13:15:20 +08002329 amdgpu_irq_gpu_reset_resume_helper(adev);
Chunming Zhou2c0d7312016-08-30 16:36:25 +08002330 if (need_full_reset && amdgpu_need_backup(adev)) {
2331 r = amdgpu_ttm_recover_gart(adev);
2332 if (r)
2333 DRM_ERROR("gart recovery failed!!!\n");
2334 }
Chunming Zhou1f465082016-06-30 15:02:26 +08002335 r = amdgpu_ib_ring_tests(adev);
2336 if (r) {
2337 dev_err(adev->dev, "ib ring test failed (%d).\n", r);
Chunming Zhou40019dc2016-06-29 16:01:49 +08002338 r = amdgpu_suspend(adev);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002339 need_full_reset = true;
Chunming Zhou40019dc2016-06-29 16:01:49 +08002340 goto retry;
Chunming Zhou1f465082016-06-30 15:02:26 +08002341 }
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002342 /**
2343 * recovery vm page tables, since we cannot depend on VRAM is
2344 * consistent after gpu full reset.
2345 */
2346 if (need_full_reset && amdgpu_need_backup(adev)) {
2347 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
2348 struct amdgpu_bo *bo, *tmp;
Chris Wilsonf54d1862016-10-25 13:00:45 +01002349 struct dma_fence *fence = NULL, *next = NULL;
Chunming Zhou1f465082016-06-30 15:02:26 +08002350
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002351 DRM_INFO("recover vram bo from shadow\n");
2352 mutex_lock(&adev->shadow_list_lock);
2353 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
2354 amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
2355 if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01002356 r = dma_fence_wait(fence, false);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002357 if (r) {
2358 WARN(r, "recovery from shadow isn't comleted\n");
2359 break;
2360 }
2361 }
2362
Chris Wilsonf54d1862016-10-25 13:00:45 +01002363 dma_fence_put(fence);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002364 fence = next;
2365 }
2366 mutex_unlock(&adev->shadow_list_lock);
2367 if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01002368 r = dma_fence_wait(fence, false);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002369 if (r)
2370 WARN(r, "recovery from shadow isn't comleted\n");
2371 }
Chris Wilsonf54d1862016-10-25 13:00:45 +01002372 dma_fence_put(fence);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002373 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002374 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2375 struct amdgpu_ring *ring = adev->rings[i];
2376 if (!ring)
2377 continue;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002378
Chunming Zhouaa1c8902016-06-30 13:56:02 +08002379 amd_sched_job_recovery(&ring->sched);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002380 kthread_unpark(ring->sched.thread);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002381 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002382 } else {
Chunming Zhou2200eda2016-06-30 16:53:02 +08002383 dev_err(adev->dev, "asic resume failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002384 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
Chunming Zhou0875dc92016-06-12 15:41:58 +08002385 if (adev->rings[i]) {
2386 kthread_unpark(adev->rings[i]->sched.thread);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002387 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002388 }
2389 }
2390
2391 drm_helper_resume_force_mode(adev->ddev);
2392
2393 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
2394 if (r) {
2395 /* bad news, how to tell it to userspace ? */
2396 dev_info(adev->dev, "GPU reset failed\n");
2397 }
2398
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002399 return r;
2400}
2401
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002402void amdgpu_get_pcie_info(struct amdgpu_device *adev)
2403{
2404 u32 mask;
2405 int ret;
2406
Alex Deuchercd474ba2016-02-04 10:21:23 -05002407 if (amdgpu_pcie_gen_cap)
2408 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
2409
2410 if (amdgpu_pcie_lane_cap)
2411 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
2412
2413 /* covers APUs as well */
2414 if (pci_is_root_bus(adev->pdev->bus)) {
2415 if (adev->pm.pcie_gen_mask == 0)
2416 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
2417 if (adev->pm.pcie_mlw_mask == 0)
2418 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002419 return;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002420 }
Alex Deuchercd474ba2016-02-04 10:21:23 -05002421
2422 if (adev->pm.pcie_gen_mask == 0) {
2423 ret = drm_pcie_get_speed_cap_mask(adev->ddev, &mask);
2424 if (!ret) {
2425 adev->pm.pcie_gen_mask = (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
2426 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
2427 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
2428
2429 if (mask & DRM_PCIE_SPEED_25)
2430 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
2431 if (mask & DRM_PCIE_SPEED_50)
2432 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2;
2433 if (mask & DRM_PCIE_SPEED_80)
2434 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3;
2435 } else {
2436 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
2437 }
2438 }
2439 if (adev->pm.pcie_mlw_mask == 0) {
2440 ret = drm_pcie_get_max_link_width(adev->ddev, &mask);
2441 if (!ret) {
2442 switch (mask) {
2443 case 32:
2444 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
2445 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
2446 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2447 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2448 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2449 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2450 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2451 break;
2452 case 16:
2453 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
2454 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2455 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2456 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2457 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2458 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2459 break;
2460 case 12:
2461 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2462 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2463 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2464 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2465 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2466 break;
2467 case 8:
2468 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2469 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2470 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2471 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2472 break;
2473 case 4:
2474 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2475 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2476 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2477 break;
2478 case 2:
2479 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2480 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2481 break;
2482 case 1:
2483 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
2484 break;
2485 default:
2486 break;
2487 }
2488 } else {
2489 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002490 }
2491 }
2492}
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002493
2494/*
2495 * Debugfs
2496 */
2497int amdgpu_debugfs_add_files(struct amdgpu_device *adev,
Nils Wallménius06ab6832016-05-02 12:46:15 -04002498 const struct drm_info_list *files,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002499 unsigned nfiles)
2500{
2501 unsigned i;
2502
2503 for (i = 0; i < adev->debugfs_count; i++) {
2504 if (adev->debugfs[i].files == files) {
2505 /* Already registered */
2506 return 0;
2507 }
2508 }
2509
2510 i = adev->debugfs_count + 1;
2511 if (i > AMDGPU_DEBUGFS_MAX_COMPONENTS) {
2512 DRM_ERROR("Reached maximum number of debugfs components.\n");
2513 DRM_ERROR("Report so we increase "
2514 "AMDGPU_DEBUGFS_MAX_COMPONENTS.\n");
2515 return -EINVAL;
2516 }
2517 adev->debugfs[adev->debugfs_count].files = files;
2518 adev->debugfs[adev->debugfs_count].num_files = nfiles;
2519 adev->debugfs_count = i;
2520#if defined(CONFIG_DEBUG_FS)
2521 drm_debugfs_create_files(files, nfiles,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002522 adev->ddev->primary->debugfs_root,
2523 adev->ddev->primary);
2524#endif
2525 return 0;
2526}
2527
2528static void amdgpu_debugfs_remove_files(struct amdgpu_device *adev)
2529{
2530#if defined(CONFIG_DEBUG_FS)
2531 unsigned i;
2532
2533 for (i = 0; i < adev->debugfs_count; i++) {
2534 drm_debugfs_remove_files(adev->debugfs[i].files,
2535 adev->debugfs[i].num_files,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002536 adev->ddev->primary);
2537 }
2538#endif
2539}
2540
2541#if defined(CONFIG_DEBUG_FS)
2542
2543static ssize_t amdgpu_debugfs_regs_read(struct file *f, char __user *buf,
2544 size_t size, loff_t *pos)
2545{
Al Viro45063092016-12-04 18:24:56 -05002546 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002547 ssize_t result = 0;
2548 int r;
Tom St Denisbd122672016-07-28 09:39:22 -04002549 bool pm_pg_lock, use_bank;
Tom St Denis566281592016-06-27 11:55:07 -04002550 unsigned instance_bank, sh_bank, se_bank;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002551
2552 if (size & 0x3 || *pos & 0x3)
2553 return -EINVAL;
2554
Tom St Denisbd122672016-07-28 09:39:22 -04002555 /* are we reading registers for which a PG lock is necessary? */
2556 pm_pg_lock = (*pos >> 23) & 1;
2557
Tom St Denis566281592016-06-27 11:55:07 -04002558 if (*pos & (1ULL << 62)) {
2559 se_bank = (*pos >> 24) & 0x3FF;
2560 sh_bank = (*pos >> 34) & 0x3FF;
2561 instance_bank = (*pos >> 44) & 0x3FF;
Tom St Denis32977f92016-10-09 07:41:26 -04002562
2563 if (se_bank == 0x3FF)
2564 se_bank = 0xFFFFFFFF;
2565 if (sh_bank == 0x3FF)
2566 sh_bank = 0xFFFFFFFF;
2567 if (instance_bank == 0x3FF)
2568 instance_bank = 0xFFFFFFFF;
Tom St Denis566281592016-06-27 11:55:07 -04002569 use_bank = 1;
Tom St Denis566281592016-06-27 11:55:07 -04002570 } else {
2571 use_bank = 0;
2572 }
2573
Tom St Denisbd122672016-07-28 09:39:22 -04002574 *pos &= 0x3FFFF;
2575
Tom St Denis566281592016-06-27 11:55:07 -04002576 if (use_bank) {
Tom St Denis32977f92016-10-09 07:41:26 -04002577 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
2578 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
Tom St Denis566281592016-06-27 11:55:07 -04002579 return -EINVAL;
2580 mutex_lock(&adev->grbm_idx_mutex);
2581 amdgpu_gfx_select_se_sh(adev, se_bank,
2582 sh_bank, instance_bank);
2583 }
2584
Tom St Denisbd122672016-07-28 09:39:22 -04002585 if (pm_pg_lock)
2586 mutex_lock(&adev->pm.mutex);
2587
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002588 while (size) {
2589 uint32_t value;
2590
2591 if (*pos > adev->rmmio_size)
Tom St Denis566281592016-06-27 11:55:07 -04002592 goto end;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002593
2594 value = RREG32(*pos >> 2);
2595 r = put_user(value, (uint32_t *)buf);
Tom St Denis566281592016-06-27 11:55:07 -04002596 if (r) {
2597 result = r;
2598 goto end;
2599 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002600
2601 result += 4;
2602 buf += 4;
2603 *pos += 4;
2604 size -= 4;
2605 }
2606
Tom St Denis566281592016-06-27 11:55:07 -04002607end:
2608 if (use_bank) {
2609 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
2610 mutex_unlock(&adev->grbm_idx_mutex);
2611 }
2612
Tom St Denisbd122672016-07-28 09:39:22 -04002613 if (pm_pg_lock)
2614 mutex_unlock(&adev->pm.mutex);
2615
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002616 return result;
2617}
2618
2619static ssize_t amdgpu_debugfs_regs_write(struct file *f, const char __user *buf,
2620 size_t size, loff_t *pos)
2621{
Al Viro45063092016-12-04 18:24:56 -05002622 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002623 ssize_t result = 0;
2624 int r;
Tom St Denis394fdde2016-10-10 07:31:23 -04002625 bool pm_pg_lock, use_bank;
2626 unsigned instance_bank, sh_bank, se_bank;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002627
2628 if (size & 0x3 || *pos & 0x3)
2629 return -EINVAL;
2630
Tom St Denis394fdde2016-10-10 07:31:23 -04002631 /* are we reading registers for which a PG lock is necessary? */
2632 pm_pg_lock = (*pos >> 23) & 1;
2633
2634 if (*pos & (1ULL << 62)) {
2635 se_bank = (*pos >> 24) & 0x3FF;
2636 sh_bank = (*pos >> 34) & 0x3FF;
2637 instance_bank = (*pos >> 44) & 0x3FF;
2638
2639 if (se_bank == 0x3FF)
2640 se_bank = 0xFFFFFFFF;
2641 if (sh_bank == 0x3FF)
2642 sh_bank = 0xFFFFFFFF;
2643 if (instance_bank == 0x3FF)
2644 instance_bank = 0xFFFFFFFF;
2645 use_bank = 1;
2646 } else {
2647 use_bank = 0;
2648 }
2649
2650 *pos &= 0x3FFFF;
2651
2652 if (use_bank) {
2653 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
2654 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
2655 return -EINVAL;
2656 mutex_lock(&adev->grbm_idx_mutex);
2657 amdgpu_gfx_select_se_sh(adev, se_bank,
2658 sh_bank, instance_bank);
2659 }
2660
2661 if (pm_pg_lock)
2662 mutex_lock(&adev->pm.mutex);
2663
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002664 while (size) {
2665 uint32_t value;
2666
2667 if (*pos > adev->rmmio_size)
2668 return result;
2669
2670 r = get_user(value, (uint32_t *)buf);
2671 if (r)
2672 return r;
2673
2674 WREG32(*pos >> 2, value);
2675
2676 result += 4;
2677 buf += 4;
2678 *pos += 4;
2679 size -= 4;
2680 }
2681
Tom St Denis394fdde2016-10-10 07:31:23 -04002682 if (use_bank) {
2683 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
2684 mutex_unlock(&adev->grbm_idx_mutex);
2685 }
2686
2687 if (pm_pg_lock)
2688 mutex_unlock(&adev->pm.mutex);
2689
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002690 return result;
2691}
2692
Tom St Denisadcec282016-04-15 13:08:44 -04002693static ssize_t amdgpu_debugfs_regs_pcie_read(struct file *f, char __user *buf,
2694 size_t size, loff_t *pos)
2695{
Al Viro45063092016-12-04 18:24:56 -05002696 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04002697 ssize_t result = 0;
2698 int r;
2699
2700 if (size & 0x3 || *pos & 0x3)
2701 return -EINVAL;
2702
2703 while (size) {
2704 uint32_t value;
2705
2706 value = RREG32_PCIE(*pos >> 2);
2707 r = put_user(value, (uint32_t *)buf);
2708 if (r)
2709 return r;
2710
2711 result += 4;
2712 buf += 4;
2713 *pos += 4;
2714 size -= 4;
2715 }
2716
2717 return result;
2718}
2719
2720static ssize_t amdgpu_debugfs_regs_pcie_write(struct file *f, const char __user *buf,
2721 size_t size, loff_t *pos)
2722{
Al Viro45063092016-12-04 18:24:56 -05002723 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04002724 ssize_t result = 0;
2725 int r;
2726
2727 if (size & 0x3 || *pos & 0x3)
2728 return -EINVAL;
2729
2730 while (size) {
2731 uint32_t value;
2732
2733 r = get_user(value, (uint32_t *)buf);
2734 if (r)
2735 return r;
2736
2737 WREG32_PCIE(*pos >> 2, value);
2738
2739 result += 4;
2740 buf += 4;
2741 *pos += 4;
2742 size -= 4;
2743 }
2744
2745 return result;
2746}
2747
2748static ssize_t amdgpu_debugfs_regs_didt_read(struct file *f, char __user *buf,
2749 size_t size, loff_t *pos)
2750{
Al Viro45063092016-12-04 18:24:56 -05002751 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04002752 ssize_t result = 0;
2753 int r;
2754
2755 if (size & 0x3 || *pos & 0x3)
2756 return -EINVAL;
2757
2758 while (size) {
2759 uint32_t value;
2760
2761 value = RREG32_DIDT(*pos >> 2);
2762 r = put_user(value, (uint32_t *)buf);
2763 if (r)
2764 return r;
2765
2766 result += 4;
2767 buf += 4;
2768 *pos += 4;
2769 size -= 4;
2770 }
2771
2772 return result;
2773}
2774
2775static ssize_t amdgpu_debugfs_regs_didt_write(struct file *f, const char __user *buf,
2776 size_t size, loff_t *pos)
2777{
Al Viro45063092016-12-04 18:24:56 -05002778 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04002779 ssize_t result = 0;
2780 int r;
2781
2782 if (size & 0x3 || *pos & 0x3)
2783 return -EINVAL;
2784
2785 while (size) {
2786 uint32_t value;
2787
2788 r = get_user(value, (uint32_t *)buf);
2789 if (r)
2790 return r;
2791
2792 WREG32_DIDT(*pos >> 2, value);
2793
2794 result += 4;
2795 buf += 4;
2796 *pos += 4;
2797 size -= 4;
2798 }
2799
2800 return result;
2801}
2802
2803static ssize_t amdgpu_debugfs_regs_smc_read(struct file *f, char __user *buf,
2804 size_t size, loff_t *pos)
2805{
Al Viro45063092016-12-04 18:24:56 -05002806 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04002807 ssize_t result = 0;
2808 int r;
2809
2810 if (size & 0x3 || *pos & 0x3)
2811 return -EINVAL;
2812
2813 while (size) {
2814 uint32_t value;
2815
Tom St Denis6fc0dea2016-08-29 08:39:29 -04002816 value = RREG32_SMC(*pos);
Tom St Denisadcec282016-04-15 13:08:44 -04002817 r = put_user(value, (uint32_t *)buf);
2818 if (r)
2819 return r;
2820
2821 result += 4;
2822 buf += 4;
2823 *pos += 4;
2824 size -= 4;
2825 }
2826
2827 return result;
2828}
2829
2830static ssize_t amdgpu_debugfs_regs_smc_write(struct file *f, const char __user *buf,
2831 size_t size, loff_t *pos)
2832{
Al Viro45063092016-12-04 18:24:56 -05002833 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04002834 ssize_t result = 0;
2835 int r;
2836
2837 if (size & 0x3 || *pos & 0x3)
2838 return -EINVAL;
2839
2840 while (size) {
2841 uint32_t value;
2842
2843 r = get_user(value, (uint32_t *)buf);
2844 if (r)
2845 return r;
2846
Tom St Denis6fc0dea2016-08-29 08:39:29 -04002847 WREG32_SMC(*pos, value);
Tom St Denisadcec282016-04-15 13:08:44 -04002848
2849 result += 4;
2850 buf += 4;
2851 *pos += 4;
2852 size -= 4;
2853 }
2854
2855 return result;
2856}
2857
Tom St Denis1e051412016-06-27 09:57:18 -04002858static ssize_t amdgpu_debugfs_gca_config_read(struct file *f, char __user *buf,
2859 size_t size, loff_t *pos)
2860{
Al Viro45063092016-12-04 18:24:56 -05002861 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denis1e051412016-06-27 09:57:18 -04002862 ssize_t result = 0;
2863 int r;
2864 uint32_t *config, no_regs = 0;
2865
2866 if (size & 0x3 || *pos & 0x3)
2867 return -EINVAL;
2868
Markus Elfringecab7662016-09-18 17:00:52 +02002869 config = kmalloc_array(256, sizeof(*config), GFP_KERNEL);
Tom St Denis1e051412016-06-27 09:57:18 -04002870 if (!config)
2871 return -ENOMEM;
2872
2873 /* version, increment each time something is added */
Tom St Denise9f11dc2016-08-17 12:00:51 -04002874 config[no_regs++] = 2;
Tom St Denis1e051412016-06-27 09:57:18 -04002875 config[no_regs++] = adev->gfx.config.max_shader_engines;
2876 config[no_regs++] = adev->gfx.config.max_tile_pipes;
2877 config[no_regs++] = adev->gfx.config.max_cu_per_sh;
2878 config[no_regs++] = adev->gfx.config.max_sh_per_se;
2879 config[no_regs++] = adev->gfx.config.max_backends_per_se;
2880 config[no_regs++] = adev->gfx.config.max_texture_channel_caches;
2881 config[no_regs++] = adev->gfx.config.max_gprs;
2882 config[no_regs++] = adev->gfx.config.max_gs_threads;
2883 config[no_regs++] = adev->gfx.config.max_hw_contexts;
2884 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_frontend;
2885 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_backend;
2886 config[no_regs++] = adev->gfx.config.sc_hiz_tile_fifo_size;
2887 config[no_regs++] = adev->gfx.config.sc_earlyz_tile_fifo_size;
2888 config[no_regs++] = adev->gfx.config.num_tile_pipes;
2889 config[no_regs++] = adev->gfx.config.backend_enable_mask;
2890 config[no_regs++] = adev->gfx.config.mem_max_burst_length_bytes;
2891 config[no_regs++] = adev->gfx.config.mem_row_size_in_kb;
2892 config[no_regs++] = adev->gfx.config.shader_engine_tile_size;
2893 config[no_regs++] = adev->gfx.config.num_gpus;
2894 config[no_regs++] = adev->gfx.config.multi_gpu_tile_size;
2895 config[no_regs++] = adev->gfx.config.mc_arb_ramcfg;
2896 config[no_regs++] = adev->gfx.config.gb_addr_config;
2897 config[no_regs++] = adev->gfx.config.num_rbs;
2898
Tom St Denis89a8f302016-08-12 15:14:31 -04002899 /* rev==1 */
2900 config[no_regs++] = adev->rev_id;
2901 config[no_regs++] = adev->pg_flags;
2902 config[no_regs++] = adev->cg_flags;
2903
Tom St Denise9f11dc2016-08-17 12:00:51 -04002904 /* rev==2 */
2905 config[no_regs++] = adev->family;
2906 config[no_regs++] = adev->external_rev_id;
2907
Tom St Denis1e051412016-06-27 09:57:18 -04002908 while (size && (*pos < no_regs * 4)) {
2909 uint32_t value;
2910
2911 value = config[*pos >> 2];
2912 r = put_user(value, (uint32_t *)buf);
2913 if (r) {
2914 kfree(config);
2915 return r;
2916 }
2917
2918 result += 4;
2919 buf += 4;
2920 *pos += 4;
2921 size -= 4;
2922 }
2923
2924 kfree(config);
2925 return result;
2926}
2927
Tom St Denisf2cdaf22016-09-15 10:08:44 -04002928static ssize_t amdgpu_debugfs_sensor_read(struct file *f, char __user *buf,
2929 size_t size, loff_t *pos)
2930{
Al Viro45063092016-12-04 18:24:56 -05002931 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisf2cdaf22016-09-15 10:08:44 -04002932 int idx, r;
2933 int32_t value;
2934
2935 if (size != 4 || *pos & 0x3)
2936 return -EINVAL;
2937
2938 /* convert offset to sensor number */
2939 idx = *pos >> 2;
2940
2941 if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->read_sensor)
2942 r = adev->powerplay.pp_funcs->read_sensor(adev->powerplay.pp_handle, idx, &value);
2943 else
2944 return -EINVAL;
2945
2946 if (!r)
2947 r = put_user(value, (int32_t *)buf);
2948
2949 return !r ? 4 : r;
2950}
Tom St Denis1e051412016-06-27 09:57:18 -04002951
Tom St Denis273d7aa2016-10-11 14:48:55 -04002952static ssize_t amdgpu_debugfs_wave_read(struct file *f, char __user *buf,
2953 size_t size, loff_t *pos)
2954{
2955 struct amdgpu_device *adev = f->f_inode->i_private;
2956 int r, x;
2957 ssize_t result=0;
Tom St Denis472259f2016-10-14 09:49:09 -04002958 uint32_t offset, se, sh, cu, wave, simd, data[32];
Tom St Denis273d7aa2016-10-11 14:48:55 -04002959
2960 if (size & 3 || *pos & 3)
2961 return -EINVAL;
2962
2963 /* decode offset */
2964 offset = (*pos & 0x7F);
2965 se = ((*pos >> 7) & 0xFF);
2966 sh = ((*pos >> 15) & 0xFF);
2967 cu = ((*pos >> 23) & 0xFF);
2968 wave = ((*pos >> 31) & 0xFF);
2969 simd = ((*pos >> 37) & 0xFF);
Tom St Denis273d7aa2016-10-11 14:48:55 -04002970
2971 /* switch to the specific se/sh/cu */
2972 mutex_lock(&adev->grbm_idx_mutex);
2973 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
2974
2975 x = 0;
Tom St Denis472259f2016-10-14 09:49:09 -04002976 if (adev->gfx.funcs->read_wave_data)
2977 adev->gfx.funcs->read_wave_data(adev, simd, wave, data, &x);
Tom St Denis273d7aa2016-10-11 14:48:55 -04002978
2979 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
2980 mutex_unlock(&adev->grbm_idx_mutex);
2981
Tom St Denis5ecfb3b2016-10-13 12:15:03 -04002982 if (!x)
2983 return -EINVAL;
2984
Tom St Denis472259f2016-10-14 09:49:09 -04002985 while (size && (offset < x * 4)) {
Tom St Denis273d7aa2016-10-11 14:48:55 -04002986 uint32_t value;
2987
Tom St Denis472259f2016-10-14 09:49:09 -04002988 value = data[offset >> 2];
Tom St Denis273d7aa2016-10-11 14:48:55 -04002989 r = put_user(value, (uint32_t *)buf);
2990 if (r)
2991 return r;
2992
2993 result += 4;
2994 buf += 4;
Tom St Denis472259f2016-10-14 09:49:09 -04002995 offset += 4;
Tom St Denis273d7aa2016-10-11 14:48:55 -04002996 size -= 4;
2997 }
2998
2999 return result;
3000}
3001
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003002static ssize_t amdgpu_debugfs_gpr_read(struct file *f, char __user *buf,
3003 size_t size, loff_t *pos)
3004{
3005 struct amdgpu_device *adev = f->f_inode->i_private;
3006 int r;
3007 ssize_t result = 0;
3008 uint32_t offset, se, sh, cu, wave, simd, thread, bank, *data;
3009
3010 if (size & 3 || *pos & 3)
3011 return -EINVAL;
3012
3013 /* decode offset */
3014 offset = (*pos & 0xFFF); /* in dwords */
3015 se = ((*pos >> 12) & 0xFF);
3016 sh = ((*pos >> 20) & 0xFF);
3017 cu = ((*pos >> 28) & 0xFF);
3018 wave = ((*pos >> 36) & 0xFF);
3019 simd = ((*pos >> 44) & 0xFF);
3020 thread = ((*pos >> 52) & 0xFF);
3021 bank = ((*pos >> 60) & 1);
3022
3023 data = kmalloc_array(1024, sizeof(*data), GFP_KERNEL);
3024 if (!data)
3025 return -ENOMEM;
3026
3027 /* switch to the specific se/sh/cu */
3028 mutex_lock(&adev->grbm_idx_mutex);
3029 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
3030
3031 if (bank == 0) {
3032 if (adev->gfx.funcs->read_wave_vgprs)
3033 adev->gfx.funcs->read_wave_vgprs(adev, simd, wave, thread, offset, size>>2, data);
3034 } else {
3035 if (adev->gfx.funcs->read_wave_sgprs)
3036 adev->gfx.funcs->read_wave_sgprs(adev, simd, wave, offset, size>>2, data);
3037 }
3038
3039 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
3040 mutex_unlock(&adev->grbm_idx_mutex);
3041
3042 while (size) {
3043 uint32_t value;
3044
3045 value = data[offset++];
3046 r = put_user(value, (uint32_t *)buf);
3047 if (r) {
3048 result = r;
3049 goto err;
3050 }
3051
3052 result += 4;
3053 buf += 4;
3054 size -= 4;
3055 }
3056
3057err:
3058 kfree(data);
3059 return result;
3060}
3061
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003062static const struct file_operations amdgpu_debugfs_regs_fops = {
3063 .owner = THIS_MODULE,
3064 .read = amdgpu_debugfs_regs_read,
3065 .write = amdgpu_debugfs_regs_write,
3066 .llseek = default_llseek
3067};
Tom St Denisadcec282016-04-15 13:08:44 -04003068static const struct file_operations amdgpu_debugfs_regs_didt_fops = {
3069 .owner = THIS_MODULE,
3070 .read = amdgpu_debugfs_regs_didt_read,
3071 .write = amdgpu_debugfs_regs_didt_write,
3072 .llseek = default_llseek
3073};
3074static const struct file_operations amdgpu_debugfs_regs_pcie_fops = {
3075 .owner = THIS_MODULE,
3076 .read = amdgpu_debugfs_regs_pcie_read,
3077 .write = amdgpu_debugfs_regs_pcie_write,
3078 .llseek = default_llseek
3079};
3080static const struct file_operations amdgpu_debugfs_regs_smc_fops = {
3081 .owner = THIS_MODULE,
3082 .read = amdgpu_debugfs_regs_smc_read,
3083 .write = amdgpu_debugfs_regs_smc_write,
3084 .llseek = default_llseek
3085};
3086
Tom St Denis1e051412016-06-27 09:57:18 -04003087static const struct file_operations amdgpu_debugfs_gca_config_fops = {
3088 .owner = THIS_MODULE,
3089 .read = amdgpu_debugfs_gca_config_read,
3090 .llseek = default_llseek
3091};
3092
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003093static const struct file_operations amdgpu_debugfs_sensors_fops = {
3094 .owner = THIS_MODULE,
3095 .read = amdgpu_debugfs_sensor_read,
3096 .llseek = default_llseek
3097};
3098
Tom St Denis273d7aa2016-10-11 14:48:55 -04003099static const struct file_operations amdgpu_debugfs_wave_fops = {
3100 .owner = THIS_MODULE,
3101 .read = amdgpu_debugfs_wave_read,
3102 .llseek = default_llseek
3103};
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003104static const struct file_operations amdgpu_debugfs_gpr_fops = {
3105 .owner = THIS_MODULE,
3106 .read = amdgpu_debugfs_gpr_read,
3107 .llseek = default_llseek
3108};
Tom St Denis273d7aa2016-10-11 14:48:55 -04003109
Tom St Denisadcec282016-04-15 13:08:44 -04003110static const struct file_operations *debugfs_regs[] = {
3111 &amdgpu_debugfs_regs_fops,
3112 &amdgpu_debugfs_regs_didt_fops,
3113 &amdgpu_debugfs_regs_pcie_fops,
3114 &amdgpu_debugfs_regs_smc_fops,
Tom St Denis1e051412016-06-27 09:57:18 -04003115 &amdgpu_debugfs_gca_config_fops,
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003116 &amdgpu_debugfs_sensors_fops,
Tom St Denis273d7aa2016-10-11 14:48:55 -04003117 &amdgpu_debugfs_wave_fops,
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003118 &amdgpu_debugfs_gpr_fops,
Tom St Denisadcec282016-04-15 13:08:44 -04003119};
3120
3121static const char *debugfs_regs_names[] = {
3122 "amdgpu_regs",
3123 "amdgpu_regs_didt",
3124 "amdgpu_regs_pcie",
3125 "amdgpu_regs_smc",
Tom St Denis1e051412016-06-27 09:57:18 -04003126 "amdgpu_gca_config",
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003127 "amdgpu_sensors",
Tom St Denis273d7aa2016-10-11 14:48:55 -04003128 "amdgpu_wave",
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003129 "amdgpu_gpr",
Tom St Denisadcec282016-04-15 13:08:44 -04003130};
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003131
3132static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
3133{
3134 struct drm_minor *minor = adev->ddev->primary;
3135 struct dentry *ent, *root = minor->debugfs_root;
Tom St Denisadcec282016-04-15 13:08:44 -04003136 unsigned i, j;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003137
Tom St Denisadcec282016-04-15 13:08:44 -04003138 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3139 ent = debugfs_create_file(debugfs_regs_names[i],
3140 S_IFREG | S_IRUGO, root,
3141 adev, debugfs_regs[i]);
3142 if (IS_ERR(ent)) {
3143 for (j = 0; j < i; j++) {
3144 debugfs_remove(adev->debugfs_regs[i]);
3145 adev->debugfs_regs[i] = NULL;
3146 }
3147 return PTR_ERR(ent);
3148 }
3149
3150 if (!i)
3151 i_size_write(ent->d_inode, adev->rmmio_size);
3152 adev->debugfs_regs[i] = ent;
3153 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003154
3155 return 0;
3156}
3157
3158static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev)
3159{
Tom St Denisadcec282016-04-15 13:08:44 -04003160 unsigned i;
3161
3162 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3163 if (adev->debugfs_regs[i]) {
3164 debugfs_remove(adev->debugfs_regs[i]);
3165 adev->debugfs_regs[i] = NULL;
3166 }
3167 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003168}
3169
3170int amdgpu_debugfs_init(struct drm_minor *minor)
3171{
3172 return 0;
3173}
3174
3175void amdgpu_debugfs_cleanup(struct drm_minor *minor)
3176{
3177}
Alexander Kuleshov7cebc722015-06-27 13:16:05 +06003178#else
3179static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
3180{
3181 return 0;
3182}
3183static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev) { }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003184#endif