blob: 85b36edbdd61536ebff6cf731841abd3758e2e39 [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;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001398 }
1399 }
1400
1401 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001402 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001403 continue;
1404 /* gmc hw init is done early */
Alex Deuchera1255102016-10-13 17:41:13 -04001405 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001406 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001407 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001408 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001409 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
1410 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001411 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001412 }
Alex Deuchera1255102016-10-13 17:41:13 -04001413 adev->ip_blocks[i].status.hw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001414 }
1415
1416 return 0;
1417}
1418
1419static int amdgpu_late_init(struct amdgpu_device *adev)
1420{
1421 int i = 0, r;
1422
1423 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001424 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001425 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001426 if (adev->ip_blocks[i].version->funcs->late_init) {
1427 r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001428 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001429 DRM_ERROR("late_init of IP block <%s> failed %d\n",
1430 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001431 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001432 }
Alex Deuchera1255102016-10-13 17:41:13 -04001433 adev->ip_blocks[i].status.late_initialized = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001434 }
Alex Deucher4a446d52016-10-07 14:48:18 -04001435 /* skip CG for VCE/UVD, it's handled specially */
Alex Deuchera1255102016-10-13 17:41:13 -04001436 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1437 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
Alex Deucher4a446d52016-10-07 14:48:18 -04001438 /* enable clockgating to save power */
Alex Deuchera1255102016-10-13 17:41:13 -04001439 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1440 AMD_CG_STATE_GATE);
Alex Deucher4a446d52016-10-07 14:48:18 -04001441 if (r) {
1442 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001443 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher4a446d52016-10-07 14:48:18 -04001444 return r;
1445 }
Arindam Nathb0b00ff2016-10-07 19:01:37 +05301446 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001447 }
1448
1449 return 0;
1450}
1451
1452static int amdgpu_fini(struct amdgpu_device *adev)
1453{
1454 int i, r;
1455
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001456 /* need to disable SMC first */
1457 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001458 if (!adev->ip_blocks[i].status.hw)
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001459 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001460 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001461 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
Alex Deuchera1255102016-10-13 17:41:13 -04001462 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1463 AMD_CG_STATE_UNGATE);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001464 if (r) {
1465 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001466 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001467 return r;
1468 }
Alex Deuchera1255102016-10-13 17:41:13 -04001469 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001470 /* XXX handle errors */
1471 if (r) {
1472 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001473 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001474 }
Alex Deuchera1255102016-10-13 17:41:13 -04001475 adev->ip_blocks[i].status.hw = false;
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001476 break;
1477 }
1478 }
1479
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001480 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001481 if (!adev->ip_blocks[i].status.hw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001482 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001483 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001484 amdgpu_wb_fini(adev);
1485 amdgpu_vram_scratch_fini(adev);
1486 }
Rex Zhu8201a672016-11-24 21:44:44 +08001487
1488 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1489 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1490 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
1491 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1492 AMD_CG_STATE_UNGATE);
1493 if (r) {
1494 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1495 adev->ip_blocks[i].version->funcs->name, r);
1496 return r;
1497 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001498 }
Rex Zhu8201a672016-11-24 21:44:44 +08001499
Alex Deuchera1255102016-10-13 17:41:13 -04001500 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001501 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001502 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001503 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
1504 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001505 }
Rex Zhu8201a672016-11-24 21:44:44 +08001506
Alex Deuchera1255102016-10-13 17:41:13 -04001507 adev->ip_blocks[i].status.hw = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001508 }
1509
1510 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001511 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001512 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001513 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001514 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001515 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001516 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
1517 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001518 }
Alex Deuchera1255102016-10-13 17:41:13 -04001519 adev->ip_blocks[i].status.sw = false;
1520 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001521 }
1522
Monk Liua6dcfd92016-05-19 14:36:34 +08001523 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001524 if (!adev->ip_blocks[i].status.late_initialized)
Grazvydas Ignotas8a2eef12016-10-03 00:06:44 +03001525 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001526 if (adev->ip_blocks[i].version->funcs->late_fini)
1527 adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
1528 adev->ip_blocks[i].status.late_initialized = false;
Monk Liua6dcfd92016-05-19 14:36:34 +08001529 }
1530
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001531 return 0;
1532}
1533
Alex Deucherfaefba92016-12-06 10:38:29 -05001534int amdgpu_suspend(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001535{
1536 int i, r;
1537
Flora Cuic5a93a22016-02-26 10:45:25 +08001538 /* ungate SMC block first */
1539 r = amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC,
1540 AMD_CG_STATE_UNGATE);
1541 if (r) {
1542 DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n",r);
1543 }
1544
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001545 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001546 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001547 continue;
1548 /* ungate blocks so that suspend can properly shut them down */
Flora Cuic5a93a22016-02-26 10:45:25 +08001549 if (i != AMD_IP_BLOCK_TYPE_SMC) {
Alex Deuchera1255102016-10-13 17:41:13 -04001550 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1551 AMD_CG_STATE_UNGATE);
Flora Cuic5a93a22016-02-26 10:45:25 +08001552 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001553 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1554 adev->ip_blocks[i].version->funcs->name, r);
Flora Cuic5a93a22016-02-26 10:45:25 +08001555 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001556 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001557 /* XXX handle errors */
Alex Deuchera1255102016-10-13 17:41:13 -04001558 r = adev->ip_blocks[i].version->funcs->suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001559 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001560 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001561 DRM_ERROR("suspend of IP block <%s> failed %d\n",
1562 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001563 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001564 }
1565
1566 return 0;
1567}
1568
1569static int amdgpu_resume(struct amdgpu_device *adev)
1570{
1571 int i, r;
1572
1573 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001574 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001575 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001576 r = adev->ip_blocks[i].version->funcs->resume(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001577 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001578 DRM_ERROR("resume of IP block <%s> failed %d\n",
1579 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001580 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001581 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001582 }
1583
1584 return 0;
1585}
1586
Monk Liu4e99a442016-03-31 13:26:59 +08001587static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
Andres Rodriguez048765a2016-06-11 02:51:32 -04001588{
Monk Liu4e99a442016-03-31 13:26:59 +08001589 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
Xiangliang Yu5a5099c2017-01-09 18:06:57 -05001590 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
Andres Rodriguez048765a2016-06-11 02:51:32 -04001591}
1592
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001593/**
1594 * amdgpu_device_init - initialize the driver
1595 *
1596 * @adev: amdgpu_device pointer
1597 * @pdev: drm dev pointer
1598 * @pdev: pci dev pointer
1599 * @flags: driver flags
1600 *
1601 * Initializes the driver info and hw (all asics).
1602 * Returns 0 for success or an error on failure.
1603 * Called at driver startup.
1604 */
1605int amdgpu_device_init(struct amdgpu_device *adev,
1606 struct drm_device *ddev,
1607 struct pci_dev *pdev,
1608 uint32_t flags)
1609{
1610 int r, i;
1611 bool runtime = false;
Marek Olšák95844d22016-08-17 23:49:27 +02001612 u32 max_MBps;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001613
1614 adev->shutdown = false;
1615 adev->dev = &pdev->dev;
1616 adev->ddev = ddev;
1617 adev->pdev = pdev;
1618 adev->flags = flags;
Jammy Zhou2f7d10b2015-07-22 11:29:01 +08001619 adev->asic_type = flags & AMD_ASIC_MASK;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001620 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
1621 adev->mc.gtt_size = 512 * 1024 * 1024;
1622 adev->accel_working = false;
1623 adev->num_rings = 0;
1624 adev->mman.buffer_funcs = NULL;
1625 adev->mman.buffer_funcs_ring = NULL;
1626 adev->vm_manager.vm_pte_funcs = NULL;
Christian König2d55e452016-02-08 17:37:38 +01001627 adev->vm_manager.vm_pte_num_rings = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001628 adev->gart.gart_funcs = NULL;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001629 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001630
1631 adev->smc_rreg = &amdgpu_invalid_rreg;
1632 adev->smc_wreg = &amdgpu_invalid_wreg;
1633 adev->pcie_rreg = &amdgpu_invalid_rreg;
1634 adev->pcie_wreg = &amdgpu_invalid_wreg;
Huang Rui36b9a952016-08-31 13:23:25 +08001635 adev->pciep_rreg = &amdgpu_invalid_rreg;
1636 adev->pciep_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001637 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
1638 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
1639 adev->didt_rreg = &amdgpu_invalid_rreg;
1640 adev->didt_wreg = &amdgpu_invalid_wreg;
Rex Zhuccdbb202016-06-08 12:47:41 +08001641 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
1642 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001643 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
1644 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
1645
Rex Zhuccdbb202016-06-08 12:47:41 +08001646
Alex Deucher3e39ab92015-06-05 15:04:33 -04001647 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
1648 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
1649 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001650
1651 /* mutex initialization are all done here so we
1652 * can recall function without having locking issues */
Christian König8d0a7ce2015-11-03 20:58:50 +01001653 mutex_init(&adev->vm_manager.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001654 atomic_set(&adev->irq.ih.lock, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001655 mutex_init(&adev->pm.mutex);
1656 mutex_init(&adev->gfx.gpu_clock_mutex);
1657 mutex_init(&adev->srbm_mutex);
1658 mutex_init(&adev->grbm_idx_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001659 mutex_init(&adev->mn_lock);
1660 hash_init(adev->mn_hash);
1661
1662 amdgpu_check_arguments(adev);
1663
1664 /* Registers mapping */
1665 /* TODO: block userspace mapping of io register */
1666 spin_lock_init(&adev->mmio_idx_lock);
1667 spin_lock_init(&adev->smc_idx_lock);
1668 spin_lock_init(&adev->pcie_idx_lock);
1669 spin_lock_init(&adev->uvd_ctx_idx_lock);
1670 spin_lock_init(&adev->didt_idx_lock);
Rex Zhuccdbb202016-06-08 12:47:41 +08001671 spin_lock_init(&adev->gc_cac_idx_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001672 spin_lock_init(&adev->audio_endpt_idx_lock);
Marek Olšák95844d22016-08-17 23:49:27 +02001673 spin_lock_init(&adev->mm_stats.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001674
Chunming Zhou0c4e7fa2016-08-17 11:41:30 +08001675 INIT_LIST_HEAD(&adev->shadow_list);
1676 mutex_init(&adev->shadow_list_lock);
1677
Chunming Zhou5c1354b2016-08-30 16:13:10 +08001678 INIT_LIST_HEAD(&adev->gtt_list);
1679 spin_lock_init(&adev->gtt_list_lock);
1680
Ken Wangda69c1612016-01-21 19:08:55 +08001681 if (adev->asic_type >= CHIP_BONAIRE) {
1682 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
1683 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
1684 } else {
1685 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
1686 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
1687 }
Chunming Zhou5c1354b2016-08-30 16:13:10 +08001688
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001689 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
1690 if (adev->rmmio == NULL) {
1691 return -ENOMEM;
1692 }
1693 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
1694 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
1695
Ken Wangda69c1612016-01-21 19:08:55 +08001696 if (adev->asic_type >= CHIP_BONAIRE)
1697 /* doorbell bar mapping */
1698 amdgpu_doorbell_init(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001699
1700 /* io port mapping */
1701 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
1702 if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
1703 adev->rio_mem_size = pci_resource_len(adev->pdev, i);
1704 adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
1705 break;
1706 }
1707 }
1708 if (adev->rio_mem == NULL)
Amber Linb64a18c2017-01-04 08:06:58 -05001709 DRM_INFO("PCI I/O BAR is not found.\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001710
1711 /* early init functions */
1712 r = amdgpu_early_init(adev);
1713 if (r)
1714 return r;
1715
1716 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
1717 /* this will fail for cards that aren't VGA class devices, just
1718 * ignore it */
1719 vga_client_register(adev->pdev, adev, NULL, amdgpu_vga_set_decode);
1720
1721 if (amdgpu_runtime_pm == 1)
1722 runtime = true;
Alex Deuchere9bef452016-04-25 13:12:18 -04001723 if (amdgpu_device_is_px(ddev))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001724 runtime = true;
1725 vga_switcheroo_register_client(adev->pdev, &amdgpu_switcheroo_ops, runtime);
1726 if (runtime)
1727 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
1728
1729 /* Read BIOS */
Alex Deucher83ba1262016-06-03 18:21:41 -04001730 if (!amdgpu_get_bios(adev)) {
1731 r = -EINVAL;
1732 goto failed;
1733 }
Nils Wallméniusf7e9e9f2016-12-14 21:52:45 +01001734
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001735 r = amdgpu_atombios_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001736 if (r) {
1737 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001738 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001739 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001740
Monk Liu4e99a442016-03-31 13:26:59 +08001741 /* detect if we are with an SRIOV vbios */
1742 amdgpu_device_detect_sriov_bios(adev);
Andres Rodriguez048765a2016-06-11 02:51:32 -04001743
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001744 /* Post card if necessary */
Monk Liubec86372016-09-14 19:38:08 +08001745 if (amdgpu_vpost_needed(adev)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001746 if (!adev->bios) {
Monk Liubec86372016-09-14 19:38:08 +08001747 dev_err(adev->dev, "no vBIOS found\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001748 r = -EINVAL;
1749 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001750 }
Monk Liubec86372016-09-14 19:38:08 +08001751 DRM_INFO("GPU posting now...\n");
Monk Liu4e99a442016-03-31 13:26:59 +08001752 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
1753 if (r) {
1754 dev_err(adev->dev, "gpu post error!\n");
1755 goto failed;
1756 }
1757 } else {
1758 DRM_INFO("GPU post is not needed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001759 }
1760
1761 /* Initialize clocks */
1762 r = amdgpu_atombios_get_clock_info(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001763 if (r) {
1764 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001765 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001766 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001767 /* init i2c buses */
1768 amdgpu_atombios_i2c_init(adev);
1769
1770 /* Fence driver */
1771 r = amdgpu_fence_driver_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001772 if (r) {
1773 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001774 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001775 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001776
1777 /* init the mode config */
1778 drm_mode_config_init(adev->ddev);
1779
1780 r = amdgpu_init(adev);
1781 if (r) {
Alex Deucher2c1a2782015-12-07 17:02:53 -05001782 dev_err(adev->dev, "amdgpu_init failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001783 amdgpu_fini(adev);
Alex Deucher83ba1262016-06-03 18:21:41 -04001784 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001785 }
1786
1787 adev->accel_working = true;
1788
Marek Olšák95844d22016-08-17 23:49:27 +02001789 /* Initialize the buffer migration limit. */
1790 if (amdgpu_moverate >= 0)
1791 max_MBps = amdgpu_moverate;
1792 else
1793 max_MBps = 8; /* Allow 8 MB/s. */
1794 /* Get a log2 for easy divisions. */
1795 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
1796
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001797 amdgpu_fbdev_init(adev);
1798
1799 r = amdgpu_ib_pool_init(adev);
1800 if (r) {
1801 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
Alex Deucher83ba1262016-06-03 18:21:41 -04001802 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001803 }
1804
1805 r = amdgpu_ib_ring_tests(adev);
1806 if (r)
1807 DRM_ERROR("ib ring test failed (%d).\n", r);
1808
1809 r = amdgpu_gem_debugfs_init(adev);
1810 if (r) {
1811 DRM_ERROR("registering gem debugfs failed (%d).\n", r);
1812 }
1813
1814 r = amdgpu_debugfs_regs_init(adev);
1815 if (r) {
1816 DRM_ERROR("registering register debugfs failed (%d).\n", r);
1817 }
1818
Huang Rui50ab2532016-06-12 15:51:09 +08001819 r = amdgpu_debugfs_firmware_init(adev);
1820 if (r) {
1821 DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
1822 return r;
1823 }
1824
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001825 if ((amdgpu_testing & 1)) {
1826 if (adev->accel_working)
1827 amdgpu_test_moves(adev);
1828 else
1829 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
1830 }
1831 if ((amdgpu_testing & 2)) {
1832 if (adev->accel_working)
1833 amdgpu_test_syncing(adev);
1834 else
1835 DRM_INFO("amdgpu: acceleration disabled, skipping sync tests\n");
1836 }
1837 if (amdgpu_benchmarking) {
1838 if (adev->accel_working)
1839 amdgpu_benchmark(adev, amdgpu_benchmarking);
1840 else
1841 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
1842 }
1843
1844 /* enable clockgating, etc. after ib tests, etc. since some blocks require
1845 * explicit gating rather than handling it automatically.
1846 */
1847 r = amdgpu_late_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001848 if (r) {
1849 dev_err(adev->dev, "amdgpu_late_init failed\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001850 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001851 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001852
1853 return 0;
Alex Deucher83ba1262016-06-03 18:21:41 -04001854
1855failed:
1856 if (runtime)
1857 vga_switcheroo_fini_domain_pm_ops(adev->dev);
1858 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001859}
1860
1861static void amdgpu_debugfs_remove_files(struct amdgpu_device *adev);
1862
1863/**
1864 * amdgpu_device_fini - tear down the driver
1865 *
1866 * @adev: amdgpu_device pointer
1867 *
1868 * Tear down the driver info (all asics).
1869 * Called at driver shutdown.
1870 */
1871void amdgpu_device_fini(struct amdgpu_device *adev)
1872{
1873 int r;
1874
1875 DRM_INFO("amdgpu: finishing device.\n");
1876 adev->shutdown = true;
Grazvydas Ignotasa951ed82016-09-25 23:34:48 +03001877 drm_crtc_force_disable_all(adev->ddev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001878 /* evict vram memory */
1879 amdgpu_bo_evict_vram(adev);
1880 amdgpu_ib_pool_fini(adev);
1881 amdgpu_fence_driver_fini(adev);
1882 amdgpu_fbdev_fini(adev);
1883 r = amdgpu_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001884 adev->accel_working = false;
1885 /* free i2c buses */
1886 amdgpu_i2c_fini(adev);
1887 amdgpu_atombios_fini(adev);
1888 kfree(adev->bios);
1889 adev->bios = NULL;
1890 vga_switcheroo_unregister_client(adev->pdev);
Alex Deucher83ba1262016-06-03 18:21:41 -04001891 if (adev->flags & AMD_IS_PX)
1892 vga_switcheroo_fini_domain_pm_ops(adev->dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001893 vga_client_register(adev->pdev, NULL, NULL, NULL);
1894 if (adev->rio_mem)
1895 pci_iounmap(adev->pdev, adev->rio_mem);
1896 adev->rio_mem = NULL;
1897 iounmap(adev->rmmio);
1898 adev->rmmio = NULL;
Ken Wangda69c1612016-01-21 19:08:55 +08001899 if (adev->asic_type >= CHIP_BONAIRE)
1900 amdgpu_doorbell_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001901 amdgpu_debugfs_regs_cleanup(adev);
1902 amdgpu_debugfs_remove_files(adev);
1903}
1904
1905
1906/*
1907 * Suspend & resume.
1908 */
1909/**
Alex Deucher810ddc32016-08-23 13:25:49 -04001910 * amdgpu_device_suspend - initiate device suspend
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001911 *
1912 * @pdev: drm dev pointer
1913 * @state: suspend state
1914 *
1915 * Puts the hw in the suspend state (all asics).
1916 * Returns 0 for success or an error on failure.
1917 * Called at driver suspend.
1918 */
Alex Deucher810ddc32016-08-23 13:25:49 -04001919int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001920{
1921 struct amdgpu_device *adev;
1922 struct drm_crtc *crtc;
1923 struct drm_connector *connector;
Alex Deucher5ceb54c2015-08-05 12:41:48 -04001924 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001925
1926 if (dev == NULL || dev->dev_private == NULL) {
1927 return -ENODEV;
1928 }
1929
1930 adev = dev->dev_private;
1931
1932 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
1933 return 0;
1934
1935 drm_kms_helper_poll_disable(dev);
1936
1937 /* turn off display hw */
Alex Deucher4c7fbc32015-09-23 14:32:06 -04001938 drm_modeset_lock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001939 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1940 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
1941 }
Alex Deucher4c7fbc32015-09-23 14:32:06 -04001942 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001943
Alex Deucher756e6882015-10-08 00:03:36 -04001944 /* unpin the front buffers and cursors */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001945 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Alex Deucher756e6882015-10-08 00:03:36 -04001946 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001947 struct amdgpu_framebuffer *rfb = to_amdgpu_framebuffer(crtc->primary->fb);
1948 struct amdgpu_bo *robj;
1949
Alex Deucher756e6882015-10-08 00:03:36 -04001950 if (amdgpu_crtc->cursor_bo) {
1951 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
1952 r = amdgpu_bo_reserve(aobj, false);
1953 if (r == 0) {
1954 amdgpu_bo_unpin(aobj);
1955 amdgpu_bo_unreserve(aobj);
1956 }
1957 }
1958
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001959 if (rfb == NULL || rfb->obj == NULL) {
1960 continue;
1961 }
1962 robj = gem_to_amdgpu_bo(rfb->obj);
1963 /* don't unpin kernel fb objects */
1964 if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
1965 r = amdgpu_bo_reserve(robj, false);
1966 if (r == 0) {
1967 amdgpu_bo_unpin(robj);
1968 amdgpu_bo_unreserve(robj);
1969 }
1970 }
1971 }
1972 /* evict vram memory */
1973 amdgpu_bo_evict_vram(adev);
1974
Alex Deucher5ceb54c2015-08-05 12:41:48 -04001975 amdgpu_fence_driver_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001976
1977 r = amdgpu_suspend(adev);
1978
Alex Deuchera0a71e42016-10-10 12:41:36 -04001979 /* evict remaining vram memory
1980 * This second call to evict vram is to evict the gart page table
1981 * using the CPU.
1982 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001983 amdgpu_bo_evict_vram(adev);
1984
Alex Deuchere695e772016-10-19 14:40:58 -04001985 amdgpu_atombios_scratch_regs_save(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001986 pci_save_state(dev->pdev);
1987 if (suspend) {
1988 /* Shut down the device */
1989 pci_disable_device(dev->pdev);
1990 pci_set_power_state(dev->pdev, PCI_D3hot);
jimqu74b0b152016-09-07 17:09:12 +08001991 } else {
1992 r = amdgpu_asic_reset(adev);
1993 if (r)
1994 DRM_ERROR("amdgpu asic reset failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001995 }
1996
1997 if (fbcon) {
1998 console_lock();
1999 amdgpu_fbdev_set_suspend(adev, 1);
2000 console_unlock();
2001 }
2002 return 0;
2003}
2004
2005/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002006 * amdgpu_device_resume - initiate device resume
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002007 *
2008 * @pdev: drm dev pointer
2009 *
2010 * Bring the hw back to operating state (all asics).
2011 * Returns 0 for success or an error on failure.
2012 * Called at driver resume.
2013 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002014int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002015{
2016 struct drm_connector *connector;
2017 struct amdgpu_device *adev = dev->dev_private;
Alex Deucher756e6882015-10-08 00:03:36 -04002018 struct drm_crtc *crtc;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002019 int r;
2020
2021 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2022 return 0;
2023
jimqu74b0b152016-09-07 17:09:12 +08002024 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002025 console_lock();
jimqu74b0b152016-09-07 17:09:12 +08002026
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002027 if (resume) {
2028 pci_set_power_state(dev->pdev, PCI_D0);
2029 pci_restore_state(dev->pdev);
jimqu74b0b152016-09-07 17:09:12 +08002030 r = pci_enable_device(dev->pdev);
2031 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002032 if (fbcon)
2033 console_unlock();
jimqu74b0b152016-09-07 17:09:12 +08002034 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002035 }
2036 }
Alex Deuchere695e772016-10-19 14:40:58 -04002037 amdgpu_atombios_scratch_regs_restore(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002038
2039 /* post card */
jimqu74b0b152016-09-07 17:09:12 +08002040 if (!amdgpu_card_posted(adev) || !resume) {
2041 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2042 if (r)
2043 DRM_ERROR("amdgpu asic init failed\n");
2044 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002045
2046 r = amdgpu_resume(adev);
Flora Cuica198522016-02-04 15:10:08 +08002047 if (r)
2048 DRM_ERROR("amdgpu_resume failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002049
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002050 amdgpu_fence_driver_resume(adev);
2051
Flora Cuica198522016-02-04 15:10:08 +08002052 if (resume) {
2053 r = amdgpu_ib_ring_tests(adev);
2054 if (r)
2055 DRM_ERROR("ib ring test failed (%d).\n", r);
2056 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002057
2058 r = amdgpu_late_init(adev);
2059 if (r)
2060 return r;
2061
Alex Deucher756e6882015-10-08 00:03:36 -04002062 /* pin cursors */
2063 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2064 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2065
2066 if (amdgpu_crtc->cursor_bo) {
2067 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
2068 r = amdgpu_bo_reserve(aobj, false);
2069 if (r == 0) {
2070 r = amdgpu_bo_pin(aobj,
2071 AMDGPU_GEM_DOMAIN_VRAM,
2072 &amdgpu_crtc->cursor_addr);
2073 if (r != 0)
2074 DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
2075 amdgpu_bo_unreserve(aobj);
2076 }
2077 }
2078 }
2079
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002080 /* blat the mode back in */
2081 if (fbcon) {
2082 drm_helper_resume_force_mode(dev);
2083 /* turn on display hw */
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002084 drm_modeset_lock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002085 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2086 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
2087 }
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002088 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002089 }
2090
2091 drm_kms_helper_poll_enable(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002092
2093 /*
2094 * Most of the connector probing functions try to acquire runtime pm
2095 * refs to ensure that the GPU is powered on when connector polling is
2096 * performed. Since we're calling this from a runtime PM callback,
2097 * trying to acquire rpm refs will cause us to deadlock.
2098 *
2099 * Since we're guaranteed to be holding the rpm lock, it's safe to
2100 * temporarily disable the rpm helpers so this doesn't deadlock us.
2101 */
2102#ifdef CONFIG_PM
2103 dev->dev->power.disable_depth++;
2104#endif
Alex Deucher54fb2a52015-11-24 14:30:56 -05002105 drm_helper_hpd_irq_event(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002106#ifdef CONFIG_PM
2107 dev->dev->power.disable_depth--;
2108#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002109
2110 if (fbcon) {
2111 amdgpu_fbdev_set_suspend(adev, 0);
2112 console_unlock();
2113 }
2114
2115 return 0;
2116}
2117
Chunming Zhou63fbf422016-07-15 11:19:20 +08002118static bool amdgpu_check_soft_reset(struct amdgpu_device *adev)
2119{
2120 int i;
2121 bool asic_hang = false;
2122
2123 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002124 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou63fbf422016-07-15 11:19:20 +08002125 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002126 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
2127 adev->ip_blocks[i].status.hang =
2128 adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
2129 if (adev->ip_blocks[i].status.hang) {
2130 DRM_INFO("IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
Chunming Zhou63fbf422016-07-15 11:19:20 +08002131 asic_hang = true;
2132 }
2133 }
2134 return asic_hang;
2135}
2136
Baoyou Xie4d446652016-09-18 22:09:35 +08002137static int amdgpu_pre_soft_reset(struct amdgpu_device *adev)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002138{
2139 int i, r = 0;
2140
2141 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002142 if (!adev->ip_blocks[i].status.valid)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002143 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002144 if (adev->ip_blocks[i].status.hang &&
2145 adev->ip_blocks[i].version->funcs->pre_soft_reset) {
2146 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
Chunming Zhoud31a5012016-07-18 10:04:34 +08002147 if (r)
2148 return r;
2149 }
2150 }
2151
2152 return 0;
2153}
2154
Chunming Zhou35d782f2016-07-15 15:57:13 +08002155static bool amdgpu_need_full_reset(struct amdgpu_device *adev)
2156{
Alex Deucherda146d32016-10-13 16:07:03 -04002157 int i;
2158
2159 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002160 if (!adev->ip_blocks[i].status.valid)
Alex Deucherda146d32016-10-13 16:07:03 -04002161 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002162 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
2163 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
2164 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
2165 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE)) {
2166 if (adev->ip_blocks[i].status.hang) {
Alex Deucherda146d32016-10-13 16:07:03 -04002167 DRM_INFO("Some block need full reset!\n");
2168 return true;
2169 }
2170 }
Chunming Zhou35d782f2016-07-15 15:57:13 +08002171 }
2172 return false;
2173}
2174
2175static int amdgpu_soft_reset(struct amdgpu_device *adev)
2176{
2177 int i, r = 0;
2178
2179 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002180 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002181 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002182 if (adev->ip_blocks[i].status.hang &&
2183 adev->ip_blocks[i].version->funcs->soft_reset) {
2184 r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002185 if (r)
2186 return r;
2187 }
2188 }
2189
2190 return 0;
2191}
2192
2193static int amdgpu_post_soft_reset(struct amdgpu_device *adev)
2194{
2195 int i, r = 0;
2196
2197 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002198 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002199 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002200 if (adev->ip_blocks[i].status.hang &&
2201 adev->ip_blocks[i].version->funcs->post_soft_reset)
2202 r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002203 if (r)
2204 return r;
2205 }
2206
2207 return 0;
2208}
2209
Chunming Zhou3ad81f12016-08-05 17:30:17 +08002210bool amdgpu_need_backup(struct amdgpu_device *adev)
2211{
2212 if (adev->flags & AMD_IS_APU)
2213 return false;
2214
2215 return amdgpu_lockup_timeout > 0 ? true : false;
2216}
2217
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002218static int amdgpu_recover_vram_from_shadow(struct amdgpu_device *adev,
2219 struct amdgpu_ring *ring,
2220 struct amdgpu_bo *bo,
Chris Wilsonf54d1862016-10-25 13:00:45 +01002221 struct dma_fence **fence)
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002222{
2223 uint32_t domain;
2224 int r;
2225
2226 if (!bo->shadow)
2227 return 0;
2228
2229 r = amdgpu_bo_reserve(bo, false);
2230 if (r)
2231 return r;
2232 domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
2233 /* if bo has been evicted, then no need to recover */
2234 if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
2235 r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
2236 NULL, fence, true);
2237 if (r) {
2238 DRM_ERROR("recover page table failed!\n");
2239 goto err;
2240 }
2241 }
2242err:
2243 amdgpu_bo_unreserve(bo);
2244 return r;
2245}
2246
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002247/**
2248 * amdgpu_gpu_reset - reset the asic
2249 *
2250 * @adev: amdgpu device pointer
2251 *
2252 * Attempt the reset the GPU if it has hung (all asics).
2253 * Returns 0 for success or an error on failure.
2254 */
2255int amdgpu_gpu_reset(struct amdgpu_device *adev)
2256{
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002257 int i, r;
2258 int resched;
Chunming Zhou35d782f2016-07-15 15:57:13 +08002259 bool need_full_reset;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002260
Chunming Zhou63fbf422016-07-15 11:19:20 +08002261 if (!amdgpu_check_soft_reset(adev)) {
2262 DRM_INFO("No hardware hang detected. Did some blocks stall?\n");
2263 return 0;
2264 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002265
Marek Olšákd94aed52015-05-05 21:13:49 +02002266 atomic_inc(&adev->gpu_reset_counter);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002267
Chunming Zhoua3c47d62016-06-30 16:44:41 +08002268 /* block TTM */
2269 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
2270
Chunming Zhou0875dc92016-06-12 15:41:58 +08002271 /* block scheduler */
2272 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2273 struct amdgpu_ring *ring = adev->rings[i];
2274
2275 if (!ring)
2276 continue;
2277 kthread_park(ring->sched.thread);
Chunming Zhouaa1c8902016-06-30 13:56:02 +08002278 amd_sched_hw_job_reset(&ring->sched);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002279 }
Chunming Zhou2200eda2016-06-30 16:53:02 +08002280 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
2281 amdgpu_fence_driver_force_completion(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002282
Chunming Zhou35d782f2016-07-15 15:57:13 +08002283 need_full_reset = amdgpu_need_full_reset(adev);
2284
2285 if (!need_full_reset) {
2286 amdgpu_pre_soft_reset(adev);
2287 r = amdgpu_soft_reset(adev);
2288 amdgpu_post_soft_reset(adev);
2289 if (r || amdgpu_check_soft_reset(adev)) {
2290 DRM_INFO("soft reset failed, will fallback to full reset!\n");
2291 need_full_reset = true;
2292 }
2293 }
2294
2295 if (need_full_reset) {
Chunming Zhou35d782f2016-07-15 15:57:13 +08002296 r = amdgpu_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002297
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002298retry:
Chunming Zhou35d782f2016-07-15 15:57:13 +08002299 /* Disable fb access */
2300 if (adev->mode_info.num_crtc) {
2301 struct amdgpu_mode_mc_save save;
2302 amdgpu_display_stop_mc_access(adev, &save);
2303 amdgpu_wait_for_idle(adev, AMD_IP_BLOCK_TYPE_GMC);
2304 }
Alex Deuchere695e772016-10-19 14:40:58 -04002305 amdgpu_atombios_scratch_regs_save(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002306 r = amdgpu_asic_reset(adev);
Alex Deuchere695e772016-10-19 14:40:58 -04002307 amdgpu_atombios_scratch_regs_restore(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002308 /* post card */
2309 amdgpu_atom_asic_init(adev->mode_info.atom_context);
Alex Deucherbfa99262016-01-15 11:59:48 -05002310
Chunming Zhou35d782f2016-07-15 15:57:13 +08002311 if (!r) {
2312 dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
2313 r = amdgpu_resume(adev);
2314 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002315 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002316 if (!r) {
Chunming Zhoue72cfd52016-07-27 13:15:20 +08002317 amdgpu_irq_gpu_reset_resume_helper(adev);
Chunming Zhou2c0d7312016-08-30 16:36:25 +08002318 if (need_full_reset && amdgpu_need_backup(adev)) {
2319 r = amdgpu_ttm_recover_gart(adev);
2320 if (r)
2321 DRM_ERROR("gart recovery failed!!!\n");
2322 }
Chunming Zhou1f465082016-06-30 15:02:26 +08002323 r = amdgpu_ib_ring_tests(adev);
2324 if (r) {
2325 dev_err(adev->dev, "ib ring test failed (%d).\n", r);
Chunming Zhou40019dc2016-06-29 16:01:49 +08002326 r = amdgpu_suspend(adev);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002327 need_full_reset = true;
Chunming Zhou40019dc2016-06-29 16:01:49 +08002328 goto retry;
Chunming Zhou1f465082016-06-30 15:02:26 +08002329 }
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002330 /**
2331 * recovery vm page tables, since we cannot depend on VRAM is
2332 * consistent after gpu full reset.
2333 */
2334 if (need_full_reset && amdgpu_need_backup(adev)) {
2335 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
2336 struct amdgpu_bo *bo, *tmp;
Chris Wilsonf54d1862016-10-25 13:00:45 +01002337 struct dma_fence *fence = NULL, *next = NULL;
Chunming Zhou1f465082016-06-30 15:02:26 +08002338
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002339 DRM_INFO("recover vram bo from shadow\n");
2340 mutex_lock(&adev->shadow_list_lock);
2341 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
2342 amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
2343 if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01002344 r = dma_fence_wait(fence, false);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002345 if (r) {
2346 WARN(r, "recovery from shadow isn't comleted\n");
2347 break;
2348 }
2349 }
2350
Chris Wilsonf54d1862016-10-25 13:00:45 +01002351 dma_fence_put(fence);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002352 fence = next;
2353 }
2354 mutex_unlock(&adev->shadow_list_lock);
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 }
Chris Wilsonf54d1862016-10-25 13:00:45 +01002360 dma_fence_put(fence);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002361 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002362 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2363 struct amdgpu_ring *ring = adev->rings[i];
2364 if (!ring)
2365 continue;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002366
Chunming Zhouaa1c8902016-06-30 13:56:02 +08002367 amd_sched_job_recovery(&ring->sched);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002368 kthread_unpark(ring->sched.thread);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002369 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002370 } else {
Chunming Zhou2200eda2016-06-30 16:53:02 +08002371 dev_err(adev->dev, "asic resume failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002372 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
Chunming Zhou0875dc92016-06-12 15:41:58 +08002373 if (adev->rings[i]) {
2374 kthread_unpark(adev->rings[i]->sched.thread);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002375 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002376 }
2377 }
2378
2379 drm_helper_resume_force_mode(adev->ddev);
2380
2381 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
2382 if (r) {
2383 /* bad news, how to tell it to userspace ? */
2384 dev_info(adev->dev, "GPU reset failed\n");
2385 }
2386
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002387 return r;
2388}
2389
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002390void amdgpu_get_pcie_info(struct amdgpu_device *adev)
2391{
2392 u32 mask;
2393 int ret;
2394
Alex Deuchercd474ba2016-02-04 10:21:23 -05002395 if (amdgpu_pcie_gen_cap)
2396 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
2397
2398 if (amdgpu_pcie_lane_cap)
2399 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
2400
2401 /* covers APUs as well */
2402 if (pci_is_root_bus(adev->pdev->bus)) {
2403 if (adev->pm.pcie_gen_mask == 0)
2404 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
2405 if (adev->pm.pcie_mlw_mask == 0)
2406 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002407 return;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002408 }
Alex Deuchercd474ba2016-02-04 10:21:23 -05002409
2410 if (adev->pm.pcie_gen_mask == 0) {
2411 ret = drm_pcie_get_speed_cap_mask(adev->ddev, &mask);
2412 if (!ret) {
2413 adev->pm.pcie_gen_mask = (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
2414 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
2415 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
2416
2417 if (mask & DRM_PCIE_SPEED_25)
2418 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
2419 if (mask & DRM_PCIE_SPEED_50)
2420 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2;
2421 if (mask & DRM_PCIE_SPEED_80)
2422 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3;
2423 } else {
2424 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
2425 }
2426 }
2427 if (adev->pm.pcie_mlw_mask == 0) {
2428 ret = drm_pcie_get_max_link_width(adev->ddev, &mask);
2429 if (!ret) {
2430 switch (mask) {
2431 case 32:
2432 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
2433 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
2434 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2435 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2436 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2437 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2438 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2439 break;
2440 case 16:
2441 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
2442 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2443 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2444 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2445 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2446 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2447 break;
2448 case 12:
2449 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2450 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2451 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2452 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2453 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2454 break;
2455 case 8:
2456 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2457 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2458 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2459 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2460 break;
2461 case 4:
2462 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2463 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2464 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2465 break;
2466 case 2:
2467 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2468 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2469 break;
2470 case 1:
2471 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
2472 break;
2473 default:
2474 break;
2475 }
2476 } else {
2477 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002478 }
2479 }
2480}
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002481
2482/*
2483 * Debugfs
2484 */
2485int amdgpu_debugfs_add_files(struct amdgpu_device *adev,
Nils Wallménius06ab6832016-05-02 12:46:15 -04002486 const struct drm_info_list *files,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002487 unsigned nfiles)
2488{
2489 unsigned i;
2490
2491 for (i = 0; i < adev->debugfs_count; i++) {
2492 if (adev->debugfs[i].files == files) {
2493 /* Already registered */
2494 return 0;
2495 }
2496 }
2497
2498 i = adev->debugfs_count + 1;
2499 if (i > AMDGPU_DEBUGFS_MAX_COMPONENTS) {
2500 DRM_ERROR("Reached maximum number of debugfs components.\n");
2501 DRM_ERROR("Report so we increase "
2502 "AMDGPU_DEBUGFS_MAX_COMPONENTS.\n");
2503 return -EINVAL;
2504 }
2505 adev->debugfs[adev->debugfs_count].files = files;
2506 adev->debugfs[adev->debugfs_count].num_files = nfiles;
2507 adev->debugfs_count = i;
2508#if defined(CONFIG_DEBUG_FS)
2509 drm_debugfs_create_files(files, nfiles,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002510 adev->ddev->primary->debugfs_root,
2511 adev->ddev->primary);
2512#endif
2513 return 0;
2514}
2515
2516static void amdgpu_debugfs_remove_files(struct amdgpu_device *adev)
2517{
2518#if defined(CONFIG_DEBUG_FS)
2519 unsigned i;
2520
2521 for (i = 0; i < adev->debugfs_count; i++) {
2522 drm_debugfs_remove_files(adev->debugfs[i].files,
2523 adev->debugfs[i].num_files,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002524 adev->ddev->primary);
2525 }
2526#endif
2527}
2528
2529#if defined(CONFIG_DEBUG_FS)
2530
2531static ssize_t amdgpu_debugfs_regs_read(struct file *f, char __user *buf,
2532 size_t size, loff_t *pos)
2533{
Al Viro45063092016-12-04 18:24:56 -05002534 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002535 ssize_t result = 0;
2536 int r;
Tom St Denisbd122672016-07-28 09:39:22 -04002537 bool pm_pg_lock, use_bank;
Tom St Denis566281592016-06-27 11:55:07 -04002538 unsigned instance_bank, sh_bank, se_bank;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002539
2540 if (size & 0x3 || *pos & 0x3)
2541 return -EINVAL;
2542
Tom St Denisbd122672016-07-28 09:39:22 -04002543 /* are we reading registers for which a PG lock is necessary? */
2544 pm_pg_lock = (*pos >> 23) & 1;
2545
Tom St Denis566281592016-06-27 11:55:07 -04002546 if (*pos & (1ULL << 62)) {
2547 se_bank = (*pos >> 24) & 0x3FF;
2548 sh_bank = (*pos >> 34) & 0x3FF;
2549 instance_bank = (*pos >> 44) & 0x3FF;
Tom St Denis32977f92016-10-09 07:41:26 -04002550
2551 if (se_bank == 0x3FF)
2552 se_bank = 0xFFFFFFFF;
2553 if (sh_bank == 0x3FF)
2554 sh_bank = 0xFFFFFFFF;
2555 if (instance_bank == 0x3FF)
2556 instance_bank = 0xFFFFFFFF;
Tom St Denis566281592016-06-27 11:55:07 -04002557 use_bank = 1;
Tom St Denis566281592016-06-27 11:55:07 -04002558 } else {
2559 use_bank = 0;
2560 }
2561
Tom St Denisbd122672016-07-28 09:39:22 -04002562 *pos &= 0x3FFFF;
2563
Tom St Denis566281592016-06-27 11:55:07 -04002564 if (use_bank) {
Tom St Denis32977f92016-10-09 07:41:26 -04002565 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
2566 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
Tom St Denis566281592016-06-27 11:55:07 -04002567 return -EINVAL;
2568 mutex_lock(&adev->grbm_idx_mutex);
2569 amdgpu_gfx_select_se_sh(adev, se_bank,
2570 sh_bank, instance_bank);
2571 }
2572
Tom St Denisbd122672016-07-28 09:39:22 -04002573 if (pm_pg_lock)
2574 mutex_lock(&adev->pm.mutex);
2575
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002576 while (size) {
2577 uint32_t value;
2578
2579 if (*pos > adev->rmmio_size)
Tom St Denis566281592016-06-27 11:55:07 -04002580 goto end;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002581
2582 value = RREG32(*pos >> 2);
2583 r = put_user(value, (uint32_t *)buf);
Tom St Denis566281592016-06-27 11:55:07 -04002584 if (r) {
2585 result = r;
2586 goto end;
2587 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002588
2589 result += 4;
2590 buf += 4;
2591 *pos += 4;
2592 size -= 4;
2593 }
2594
Tom St Denis566281592016-06-27 11:55:07 -04002595end:
2596 if (use_bank) {
2597 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
2598 mutex_unlock(&adev->grbm_idx_mutex);
2599 }
2600
Tom St Denisbd122672016-07-28 09:39:22 -04002601 if (pm_pg_lock)
2602 mutex_unlock(&adev->pm.mutex);
2603
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002604 return result;
2605}
2606
2607static ssize_t amdgpu_debugfs_regs_write(struct file *f, const char __user *buf,
2608 size_t size, loff_t *pos)
2609{
Al Viro45063092016-12-04 18:24:56 -05002610 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002611 ssize_t result = 0;
2612 int r;
Tom St Denis394fdde2016-10-10 07:31:23 -04002613 bool pm_pg_lock, use_bank;
2614 unsigned instance_bank, sh_bank, se_bank;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002615
2616 if (size & 0x3 || *pos & 0x3)
2617 return -EINVAL;
2618
Tom St Denis394fdde2016-10-10 07:31:23 -04002619 /* are we reading registers for which a PG lock is necessary? */
2620 pm_pg_lock = (*pos >> 23) & 1;
2621
2622 if (*pos & (1ULL << 62)) {
2623 se_bank = (*pos >> 24) & 0x3FF;
2624 sh_bank = (*pos >> 34) & 0x3FF;
2625 instance_bank = (*pos >> 44) & 0x3FF;
2626
2627 if (se_bank == 0x3FF)
2628 se_bank = 0xFFFFFFFF;
2629 if (sh_bank == 0x3FF)
2630 sh_bank = 0xFFFFFFFF;
2631 if (instance_bank == 0x3FF)
2632 instance_bank = 0xFFFFFFFF;
2633 use_bank = 1;
2634 } else {
2635 use_bank = 0;
2636 }
2637
2638 *pos &= 0x3FFFF;
2639
2640 if (use_bank) {
2641 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
2642 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
2643 return -EINVAL;
2644 mutex_lock(&adev->grbm_idx_mutex);
2645 amdgpu_gfx_select_se_sh(adev, se_bank,
2646 sh_bank, instance_bank);
2647 }
2648
2649 if (pm_pg_lock)
2650 mutex_lock(&adev->pm.mutex);
2651
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002652 while (size) {
2653 uint32_t value;
2654
2655 if (*pos > adev->rmmio_size)
2656 return result;
2657
2658 r = get_user(value, (uint32_t *)buf);
2659 if (r)
2660 return r;
2661
2662 WREG32(*pos >> 2, value);
2663
2664 result += 4;
2665 buf += 4;
2666 *pos += 4;
2667 size -= 4;
2668 }
2669
Tom St Denis394fdde2016-10-10 07:31:23 -04002670 if (use_bank) {
2671 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
2672 mutex_unlock(&adev->grbm_idx_mutex);
2673 }
2674
2675 if (pm_pg_lock)
2676 mutex_unlock(&adev->pm.mutex);
2677
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002678 return result;
2679}
2680
Tom St Denisadcec282016-04-15 13:08:44 -04002681static ssize_t amdgpu_debugfs_regs_pcie_read(struct file *f, char __user *buf,
2682 size_t size, loff_t *pos)
2683{
Al Viro45063092016-12-04 18:24:56 -05002684 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04002685 ssize_t result = 0;
2686 int r;
2687
2688 if (size & 0x3 || *pos & 0x3)
2689 return -EINVAL;
2690
2691 while (size) {
2692 uint32_t value;
2693
2694 value = RREG32_PCIE(*pos >> 2);
2695 r = put_user(value, (uint32_t *)buf);
2696 if (r)
2697 return r;
2698
2699 result += 4;
2700 buf += 4;
2701 *pos += 4;
2702 size -= 4;
2703 }
2704
2705 return result;
2706}
2707
2708static ssize_t amdgpu_debugfs_regs_pcie_write(struct file *f, const char __user *buf,
2709 size_t size, loff_t *pos)
2710{
Al Viro45063092016-12-04 18:24:56 -05002711 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04002712 ssize_t result = 0;
2713 int r;
2714
2715 if (size & 0x3 || *pos & 0x3)
2716 return -EINVAL;
2717
2718 while (size) {
2719 uint32_t value;
2720
2721 r = get_user(value, (uint32_t *)buf);
2722 if (r)
2723 return r;
2724
2725 WREG32_PCIE(*pos >> 2, value);
2726
2727 result += 4;
2728 buf += 4;
2729 *pos += 4;
2730 size -= 4;
2731 }
2732
2733 return result;
2734}
2735
2736static ssize_t amdgpu_debugfs_regs_didt_read(struct file *f, char __user *buf,
2737 size_t size, loff_t *pos)
2738{
Al Viro45063092016-12-04 18:24:56 -05002739 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04002740 ssize_t result = 0;
2741 int r;
2742
2743 if (size & 0x3 || *pos & 0x3)
2744 return -EINVAL;
2745
2746 while (size) {
2747 uint32_t value;
2748
2749 value = RREG32_DIDT(*pos >> 2);
2750 r = put_user(value, (uint32_t *)buf);
2751 if (r)
2752 return r;
2753
2754 result += 4;
2755 buf += 4;
2756 *pos += 4;
2757 size -= 4;
2758 }
2759
2760 return result;
2761}
2762
2763static ssize_t amdgpu_debugfs_regs_didt_write(struct file *f, const char __user *buf,
2764 size_t size, loff_t *pos)
2765{
Al Viro45063092016-12-04 18:24:56 -05002766 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04002767 ssize_t result = 0;
2768 int r;
2769
2770 if (size & 0x3 || *pos & 0x3)
2771 return -EINVAL;
2772
2773 while (size) {
2774 uint32_t value;
2775
2776 r = get_user(value, (uint32_t *)buf);
2777 if (r)
2778 return r;
2779
2780 WREG32_DIDT(*pos >> 2, value);
2781
2782 result += 4;
2783 buf += 4;
2784 *pos += 4;
2785 size -= 4;
2786 }
2787
2788 return result;
2789}
2790
2791static ssize_t amdgpu_debugfs_regs_smc_read(struct file *f, char __user *buf,
2792 size_t size, loff_t *pos)
2793{
Al Viro45063092016-12-04 18:24:56 -05002794 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04002795 ssize_t result = 0;
2796 int r;
2797
2798 if (size & 0x3 || *pos & 0x3)
2799 return -EINVAL;
2800
2801 while (size) {
2802 uint32_t value;
2803
Tom St Denis6fc0dea2016-08-29 08:39:29 -04002804 value = RREG32_SMC(*pos);
Tom St Denisadcec282016-04-15 13:08:44 -04002805 r = put_user(value, (uint32_t *)buf);
2806 if (r)
2807 return r;
2808
2809 result += 4;
2810 buf += 4;
2811 *pos += 4;
2812 size -= 4;
2813 }
2814
2815 return result;
2816}
2817
2818static ssize_t amdgpu_debugfs_regs_smc_write(struct file *f, const char __user *buf,
2819 size_t size, loff_t *pos)
2820{
Al Viro45063092016-12-04 18:24:56 -05002821 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04002822 ssize_t result = 0;
2823 int r;
2824
2825 if (size & 0x3 || *pos & 0x3)
2826 return -EINVAL;
2827
2828 while (size) {
2829 uint32_t value;
2830
2831 r = get_user(value, (uint32_t *)buf);
2832 if (r)
2833 return r;
2834
Tom St Denis6fc0dea2016-08-29 08:39:29 -04002835 WREG32_SMC(*pos, value);
Tom St Denisadcec282016-04-15 13:08:44 -04002836
2837 result += 4;
2838 buf += 4;
2839 *pos += 4;
2840 size -= 4;
2841 }
2842
2843 return result;
2844}
2845
Tom St Denis1e051412016-06-27 09:57:18 -04002846static ssize_t amdgpu_debugfs_gca_config_read(struct file *f, char __user *buf,
2847 size_t size, loff_t *pos)
2848{
Al Viro45063092016-12-04 18:24:56 -05002849 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denis1e051412016-06-27 09:57:18 -04002850 ssize_t result = 0;
2851 int r;
2852 uint32_t *config, no_regs = 0;
2853
2854 if (size & 0x3 || *pos & 0x3)
2855 return -EINVAL;
2856
Markus Elfringecab7662016-09-18 17:00:52 +02002857 config = kmalloc_array(256, sizeof(*config), GFP_KERNEL);
Tom St Denis1e051412016-06-27 09:57:18 -04002858 if (!config)
2859 return -ENOMEM;
2860
2861 /* version, increment each time something is added */
Tom St Denise9f11dc2016-08-17 12:00:51 -04002862 config[no_regs++] = 2;
Tom St Denis1e051412016-06-27 09:57:18 -04002863 config[no_regs++] = adev->gfx.config.max_shader_engines;
2864 config[no_regs++] = adev->gfx.config.max_tile_pipes;
2865 config[no_regs++] = adev->gfx.config.max_cu_per_sh;
2866 config[no_regs++] = adev->gfx.config.max_sh_per_se;
2867 config[no_regs++] = adev->gfx.config.max_backends_per_se;
2868 config[no_regs++] = adev->gfx.config.max_texture_channel_caches;
2869 config[no_regs++] = adev->gfx.config.max_gprs;
2870 config[no_regs++] = adev->gfx.config.max_gs_threads;
2871 config[no_regs++] = adev->gfx.config.max_hw_contexts;
2872 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_frontend;
2873 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_backend;
2874 config[no_regs++] = adev->gfx.config.sc_hiz_tile_fifo_size;
2875 config[no_regs++] = adev->gfx.config.sc_earlyz_tile_fifo_size;
2876 config[no_regs++] = adev->gfx.config.num_tile_pipes;
2877 config[no_regs++] = adev->gfx.config.backend_enable_mask;
2878 config[no_regs++] = adev->gfx.config.mem_max_burst_length_bytes;
2879 config[no_regs++] = adev->gfx.config.mem_row_size_in_kb;
2880 config[no_regs++] = adev->gfx.config.shader_engine_tile_size;
2881 config[no_regs++] = adev->gfx.config.num_gpus;
2882 config[no_regs++] = adev->gfx.config.multi_gpu_tile_size;
2883 config[no_regs++] = adev->gfx.config.mc_arb_ramcfg;
2884 config[no_regs++] = adev->gfx.config.gb_addr_config;
2885 config[no_regs++] = adev->gfx.config.num_rbs;
2886
Tom St Denis89a8f302016-08-12 15:14:31 -04002887 /* rev==1 */
2888 config[no_regs++] = adev->rev_id;
2889 config[no_regs++] = adev->pg_flags;
2890 config[no_regs++] = adev->cg_flags;
2891
Tom St Denise9f11dc2016-08-17 12:00:51 -04002892 /* rev==2 */
2893 config[no_regs++] = adev->family;
2894 config[no_regs++] = adev->external_rev_id;
2895
Tom St Denis1e051412016-06-27 09:57:18 -04002896 while (size && (*pos < no_regs * 4)) {
2897 uint32_t value;
2898
2899 value = config[*pos >> 2];
2900 r = put_user(value, (uint32_t *)buf);
2901 if (r) {
2902 kfree(config);
2903 return r;
2904 }
2905
2906 result += 4;
2907 buf += 4;
2908 *pos += 4;
2909 size -= 4;
2910 }
2911
2912 kfree(config);
2913 return result;
2914}
2915
Tom St Denisf2cdaf22016-09-15 10:08:44 -04002916static ssize_t amdgpu_debugfs_sensor_read(struct file *f, char __user *buf,
2917 size_t size, loff_t *pos)
2918{
Al Viro45063092016-12-04 18:24:56 -05002919 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisf2cdaf22016-09-15 10:08:44 -04002920 int idx, r;
2921 int32_t value;
2922
2923 if (size != 4 || *pos & 0x3)
2924 return -EINVAL;
2925
2926 /* convert offset to sensor number */
2927 idx = *pos >> 2;
2928
2929 if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->read_sensor)
2930 r = adev->powerplay.pp_funcs->read_sensor(adev->powerplay.pp_handle, idx, &value);
2931 else
2932 return -EINVAL;
2933
2934 if (!r)
2935 r = put_user(value, (int32_t *)buf);
2936
2937 return !r ? 4 : r;
2938}
Tom St Denis1e051412016-06-27 09:57:18 -04002939
Tom St Denis273d7aa2016-10-11 14:48:55 -04002940static ssize_t amdgpu_debugfs_wave_read(struct file *f, char __user *buf,
2941 size_t size, loff_t *pos)
2942{
2943 struct amdgpu_device *adev = f->f_inode->i_private;
2944 int r, x;
2945 ssize_t result=0;
Tom St Denis472259f2016-10-14 09:49:09 -04002946 uint32_t offset, se, sh, cu, wave, simd, data[32];
Tom St Denis273d7aa2016-10-11 14:48:55 -04002947
2948 if (size & 3 || *pos & 3)
2949 return -EINVAL;
2950
2951 /* decode offset */
2952 offset = (*pos & 0x7F);
2953 se = ((*pos >> 7) & 0xFF);
2954 sh = ((*pos >> 15) & 0xFF);
2955 cu = ((*pos >> 23) & 0xFF);
2956 wave = ((*pos >> 31) & 0xFF);
2957 simd = ((*pos >> 37) & 0xFF);
Tom St Denis273d7aa2016-10-11 14:48:55 -04002958
2959 /* switch to the specific se/sh/cu */
2960 mutex_lock(&adev->grbm_idx_mutex);
2961 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
2962
2963 x = 0;
Tom St Denis472259f2016-10-14 09:49:09 -04002964 if (adev->gfx.funcs->read_wave_data)
2965 adev->gfx.funcs->read_wave_data(adev, simd, wave, data, &x);
Tom St Denis273d7aa2016-10-11 14:48:55 -04002966
2967 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
2968 mutex_unlock(&adev->grbm_idx_mutex);
2969
Tom St Denis5ecfb3b2016-10-13 12:15:03 -04002970 if (!x)
2971 return -EINVAL;
2972
Tom St Denis472259f2016-10-14 09:49:09 -04002973 while (size && (offset < x * 4)) {
Tom St Denis273d7aa2016-10-11 14:48:55 -04002974 uint32_t value;
2975
Tom St Denis472259f2016-10-14 09:49:09 -04002976 value = data[offset >> 2];
Tom St Denis273d7aa2016-10-11 14:48:55 -04002977 r = put_user(value, (uint32_t *)buf);
2978 if (r)
2979 return r;
2980
2981 result += 4;
2982 buf += 4;
Tom St Denis472259f2016-10-14 09:49:09 -04002983 offset += 4;
Tom St Denis273d7aa2016-10-11 14:48:55 -04002984 size -= 4;
2985 }
2986
2987 return result;
2988}
2989
Tom St Denisc5a60ce2016-12-05 11:39:19 -05002990static ssize_t amdgpu_debugfs_gpr_read(struct file *f, char __user *buf,
2991 size_t size, loff_t *pos)
2992{
2993 struct amdgpu_device *adev = f->f_inode->i_private;
2994 int r;
2995 ssize_t result = 0;
2996 uint32_t offset, se, sh, cu, wave, simd, thread, bank, *data;
2997
2998 if (size & 3 || *pos & 3)
2999 return -EINVAL;
3000
3001 /* decode offset */
3002 offset = (*pos & 0xFFF); /* in dwords */
3003 se = ((*pos >> 12) & 0xFF);
3004 sh = ((*pos >> 20) & 0xFF);
3005 cu = ((*pos >> 28) & 0xFF);
3006 wave = ((*pos >> 36) & 0xFF);
3007 simd = ((*pos >> 44) & 0xFF);
3008 thread = ((*pos >> 52) & 0xFF);
3009 bank = ((*pos >> 60) & 1);
3010
3011 data = kmalloc_array(1024, sizeof(*data), GFP_KERNEL);
3012 if (!data)
3013 return -ENOMEM;
3014
3015 /* switch to the specific se/sh/cu */
3016 mutex_lock(&adev->grbm_idx_mutex);
3017 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
3018
3019 if (bank == 0) {
3020 if (adev->gfx.funcs->read_wave_vgprs)
3021 adev->gfx.funcs->read_wave_vgprs(adev, simd, wave, thread, offset, size>>2, data);
3022 } else {
3023 if (adev->gfx.funcs->read_wave_sgprs)
3024 adev->gfx.funcs->read_wave_sgprs(adev, simd, wave, offset, size>>2, data);
3025 }
3026
3027 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
3028 mutex_unlock(&adev->grbm_idx_mutex);
3029
3030 while (size) {
3031 uint32_t value;
3032
3033 value = data[offset++];
3034 r = put_user(value, (uint32_t *)buf);
3035 if (r) {
3036 result = r;
3037 goto err;
3038 }
3039
3040 result += 4;
3041 buf += 4;
3042 size -= 4;
3043 }
3044
3045err:
3046 kfree(data);
3047 return result;
3048}
3049
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003050static const struct file_operations amdgpu_debugfs_regs_fops = {
3051 .owner = THIS_MODULE,
3052 .read = amdgpu_debugfs_regs_read,
3053 .write = amdgpu_debugfs_regs_write,
3054 .llseek = default_llseek
3055};
Tom St Denisadcec282016-04-15 13:08:44 -04003056static const struct file_operations amdgpu_debugfs_regs_didt_fops = {
3057 .owner = THIS_MODULE,
3058 .read = amdgpu_debugfs_regs_didt_read,
3059 .write = amdgpu_debugfs_regs_didt_write,
3060 .llseek = default_llseek
3061};
3062static const struct file_operations amdgpu_debugfs_regs_pcie_fops = {
3063 .owner = THIS_MODULE,
3064 .read = amdgpu_debugfs_regs_pcie_read,
3065 .write = amdgpu_debugfs_regs_pcie_write,
3066 .llseek = default_llseek
3067};
3068static const struct file_operations amdgpu_debugfs_regs_smc_fops = {
3069 .owner = THIS_MODULE,
3070 .read = amdgpu_debugfs_regs_smc_read,
3071 .write = amdgpu_debugfs_regs_smc_write,
3072 .llseek = default_llseek
3073};
3074
Tom St Denis1e051412016-06-27 09:57:18 -04003075static const struct file_operations amdgpu_debugfs_gca_config_fops = {
3076 .owner = THIS_MODULE,
3077 .read = amdgpu_debugfs_gca_config_read,
3078 .llseek = default_llseek
3079};
3080
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003081static const struct file_operations amdgpu_debugfs_sensors_fops = {
3082 .owner = THIS_MODULE,
3083 .read = amdgpu_debugfs_sensor_read,
3084 .llseek = default_llseek
3085};
3086
Tom St Denis273d7aa2016-10-11 14:48:55 -04003087static const struct file_operations amdgpu_debugfs_wave_fops = {
3088 .owner = THIS_MODULE,
3089 .read = amdgpu_debugfs_wave_read,
3090 .llseek = default_llseek
3091};
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003092static const struct file_operations amdgpu_debugfs_gpr_fops = {
3093 .owner = THIS_MODULE,
3094 .read = amdgpu_debugfs_gpr_read,
3095 .llseek = default_llseek
3096};
Tom St Denis273d7aa2016-10-11 14:48:55 -04003097
Tom St Denisadcec282016-04-15 13:08:44 -04003098static const struct file_operations *debugfs_regs[] = {
3099 &amdgpu_debugfs_regs_fops,
3100 &amdgpu_debugfs_regs_didt_fops,
3101 &amdgpu_debugfs_regs_pcie_fops,
3102 &amdgpu_debugfs_regs_smc_fops,
Tom St Denis1e051412016-06-27 09:57:18 -04003103 &amdgpu_debugfs_gca_config_fops,
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003104 &amdgpu_debugfs_sensors_fops,
Tom St Denis273d7aa2016-10-11 14:48:55 -04003105 &amdgpu_debugfs_wave_fops,
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003106 &amdgpu_debugfs_gpr_fops,
Tom St Denisadcec282016-04-15 13:08:44 -04003107};
3108
3109static const char *debugfs_regs_names[] = {
3110 "amdgpu_regs",
3111 "amdgpu_regs_didt",
3112 "amdgpu_regs_pcie",
3113 "amdgpu_regs_smc",
Tom St Denis1e051412016-06-27 09:57:18 -04003114 "amdgpu_gca_config",
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003115 "amdgpu_sensors",
Tom St Denis273d7aa2016-10-11 14:48:55 -04003116 "amdgpu_wave",
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003117 "amdgpu_gpr",
Tom St Denisadcec282016-04-15 13:08:44 -04003118};
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003119
3120static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
3121{
3122 struct drm_minor *minor = adev->ddev->primary;
3123 struct dentry *ent, *root = minor->debugfs_root;
Tom St Denisadcec282016-04-15 13:08:44 -04003124 unsigned i, j;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003125
Tom St Denisadcec282016-04-15 13:08:44 -04003126 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3127 ent = debugfs_create_file(debugfs_regs_names[i],
3128 S_IFREG | S_IRUGO, root,
3129 adev, debugfs_regs[i]);
3130 if (IS_ERR(ent)) {
3131 for (j = 0; j < i; j++) {
3132 debugfs_remove(adev->debugfs_regs[i]);
3133 adev->debugfs_regs[i] = NULL;
3134 }
3135 return PTR_ERR(ent);
3136 }
3137
3138 if (!i)
3139 i_size_write(ent->d_inode, adev->rmmio_size);
3140 adev->debugfs_regs[i] = ent;
3141 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003142
3143 return 0;
3144}
3145
3146static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev)
3147{
Tom St Denisadcec282016-04-15 13:08:44 -04003148 unsigned i;
3149
3150 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3151 if (adev->debugfs_regs[i]) {
3152 debugfs_remove(adev->debugfs_regs[i]);
3153 adev->debugfs_regs[i] = NULL;
3154 }
3155 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003156}
3157
3158int amdgpu_debugfs_init(struct drm_minor *minor)
3159{
3160 return 0;
3161}
3162
3163void amdgpu_debugfs_cleanup(struct drm_minor *minor)
3164{
3165}
Alexander Kuleshov7cebc722015-06-27 13:16:05 +06003166#else
3167static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
3168{
3169 return 0;
3170}
3171static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev) { }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003172#endif