blob: e0fcfea99328aede215e6e1f400e7f40acc68b43 [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
Xiangliang Yubc992ba2017-01-12 14:29:34 +080097 if (amdgpu_sriov_runtime(adev)) {
98 BUG_ON(in_interrupt());
99 return amdgpu_virt_kiq_rreg(adev, reg);
100 }
101
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400102 if ((reg * 4) < adev->rmmio_size && !always_indirect)
Tom St Denisf4b373f2016-05-31 08:02:27 -0400103 ret = readl(((void __iomem *)adev->rmmio) + (reg * 4));
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400104 else {
105 unsigned long flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400106
107 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
108 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
109 ret = readl(((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
110 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400111 }
Tom St Denisf4b373f2016-05-31 08:02:27 -0400112 trace_amdgpu_mm_rreg(adev->pdev->device, reg, ret);
113 return ret;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400114}
115
116void amdgpu_mm_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v,
117 bool always_indirect)
118{
Tom St Denisf4b373f2016-05-31 08:02:27 -0400119 trace_amdgpu_mm_wreg(adev->pdev->device, reg, v);
Monk Liu4e99a442016-03-31 13:26:59 +0800120
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800121 if (amdgpu_sriov_runtime(adev)) {
122 BUG_ON(in_interrupt());
123 return amdgpu_virt_kiq_wreg(adev, reg, v);
124 }
125
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400126 if ((reg * 4) < adev->rmmio_size && !always_indirect)
127 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
128 else {
129 unsigned long flags;
130
131 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
132 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
133 writel(v, ((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
134 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
135 }
136}
137
138u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 reg)
139{
140 if ((reg * 4) < adev->rio_mem_size)
141 return ioread32(adev->rio_mem + (reg * 4));
142 else {
143 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
144 return ioread32(adev->rio_mem + (mmMM_DATA * 4));
145 }
146}
147
148void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
149{
150
151 if ((reg * 4) < adev->rio_mem_size)
152 iowrite32(v, adev->rio_mem + (reg * 4));
153 else {
154 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
155 iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
156 }
157}
158
159/**
160 * amdgpu_mm_rdoorbell - read a doorbell dword
161 *
162 * @adev: amdgpu_device pointer
163 * @index: doorbell index
164 *
165 * Returns the value in the doorbell aperture at the
166 * requested doorbell index (CIK).
167 */
168u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
169{
170 if (index < adev->doorbell.num_doorbells) {
171 return readl(adev->doorbell.ptr + index);
172 } else {
173 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
174 return 0;
175 }
176}
177
178/**
179 * amdgpu_mm_wdoorbell - write a doorbell dword
180 *
181 * @adev: amdgpu_device pointer
182 * @index: doorbell index
183 * @v: value to write
184 *
185 * Writes @v to the doorbell aperture at the
186 * requested doorbell index (CIK).
187 */
188void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
189{
190 if (index < adev->doorbell.num_doorbells) {
191 writel(v, adev->doorbell.ptr + index);
192 } else {
193 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
194 }
195}
196
197/**
198 * amdgpu_invalid_rreg - dummy reg read function
199 *
200 * @adev: amdgpu device pointer
201 * @reg: offset of register
202 *
203 * Dummy register read function. Used for register blocks
204 * that certain asics don't have (all asics).
205 * Returns the value in the register.
206 */
207static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
208{
209 DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
210 BUG();
211 return 0;
212}
213
214/**
215 * amdgpu_invalid_wreg - dummy reg write function
216 *
217 * @adev: amdgpu device pointer
218 * @reg: offset of register
219 * @v: value to write to the register
220 *
221 * Dummy register read function. Used for register blocks
222 * that certain asics don't have (all asics).
223 */
224static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
225{
226 DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
227 reg, v);
228 BUG();
229}
230
231/**
232 * amdgpu_block_invalid_rreg - dummy reg read function
233 *
234 * @adev: amdgpu device pointer
235 * @block: offset of instance
236 * @reg: offset of register
237 *
238 * Dummy register read function. Used for register blocks
239 * that certain asics don't have (all asics).
240 * Returns the value in the register.
241 */
242static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
243 uint32_t block, uint32_t reg)
244{
245 DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
246 reg, block);
247 BUG();
248 return 0;
249}
250
251/**
252 * amdgpu_block_invalid_wreg - dummy reg write function
253 *
254 * @adev: amdgpu device pointer
255 * @block: offset of instance
256 * @reg: offset of register
257 * @v: value to write to the register
258 *
259 * Dummy register read function. Used for register blocks
260 * that certain asics don't have (all asics).
261 */
262static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
263 uint32_t block,
264 uint32_t reg, uint32_t v)
265{
266 DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
267 reg, block, v);
268 BUG();
269}
270
271static int amdgpu_vram_scratch_init(struct amdgpu_device *adev)
272{
273 int r;
274
275 if (adev->vram_scratch.robj == NULL) {
276 r = amdgpu_bo_create(adev, AMDGPU_GPU_PAGE_SIZE,
Alex Deucher857d9132015-08-27 00:14:16 -0400277 PAGE_SIZE, true, AMDGPU_GEM_DOMAIN_VRAM,
Christian König03f48dd2016-08-15 17:00:22 +0200278 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
279 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS,
Christian König72d76682015-09-03 17:34:59 +0200280 NULL, NULL, &adev->vram_scratch.robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400281 if (r) {
282 return r;
283 }
284 }
285
286 r = amdgpu_bo_reserve(adev->vram_scratch.robj, false);
287 if (unlikely(r != 0))
288 return r;
289 r = amdgpu_bo_pin(adev->vram_scratch.robj,
290 AMDGPU_GEM_DOMAIN_VRAM, &adev->vram_scratch.gpu_addr);
291 if (r) {
292 amdgpu_bo_unreserve(adev->vram_scratch.robj);
293 return r;
294 }
295 r = amdgpu_bo_kmap(adev->vram_scratch.robj,
296 (void **)&adev->vram_scratch.ptr);
297 if (r)
298 amdgpu_bo_unpin(adev->vram_scratch.robj);
299 amdgpu_bo_unreserve(adev->vram_scratch.robj);
300
301 return r;
302}
303
304static void amdgpu_vram_scratch_fini(struct amdgpu_device *adev)
305{
306 int r;
307
308 if (adev->vram_scratch.robj == NULL) {
309 return;
310 }
311 r = amdgpu_bo_reserve(adev->vram_scratch.robj, false);
312 if (likely(r == 0)) {
313 amdgpu_bo_kunmap(adev->vram_scratch.robj);
314 amdgpu_bo_unpin(adev->vram_scratch.robj);
315 amdgpu_bo_unreserve(adev->vram_scratch.robj);
316 }
317 amdgpu_bo_unref(&adev->vram_scratch.robj);
318}
319
320/**
321 * amdgpu_program_register_sequence - program an array of registers.
322 *
323 * @adev: amdgpu_device pointer
324 * @registers: pointer to the register array
325 * @array_size: size of the register array
326 *
327 * Programs an array or registers with and and or masks.
328 * This is a helper for setting golden registers.
329 */
330void amdgpu_program_register_sequence(struct amdgpu_device *adev,
331 const u32 *registers,
332 const u32 array_size)
333{
334 u32 tmp, reg, and_mask, or_mask;
335 int i;
336
337 if (array_size % 3)
338 return;
339
340 for (i = 0; i < array_size; i +=3) {
341 reg = registers[i + 0];
342 and_mask = registers[i + 1];
343 or_mask = registers[i + 2];
344
345 if (and_mask == 0xffffffff) {
346 tmp = or_mask;
347 } else {
348 tmp = RREG32(reg);
349 tmp &= ~and_mask;
350 tmp |= or_mask;
351 }
352 WREG32(reg, tmp);
353 }
354}
355
356void amdgpu_pci_config_reset(struct amdgpu_device *adev)
357{
358 pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
359}
360
361/*
362 * GPU doorbell aperture helpers function.
363 */
364/**
365 * amdgpu_doorbell_init - Init doorbell driver information.
366 *
367 * @adev: amdgpu_device pointer
368 *
369 * Init doorbell driver information (CIK)
370 * Returns 0 on success, error on failure.
371 */
372static int amdgpu_doorbell_init(struct amdgpu_device *adev)
373{
374 /* doorbell bar mapping */
375 adev->doorbell.base = pci_resource_start(adev->pdev, 2);
376 adev->doorbell.size = pci_resource_len(adev->pdev, 2);
377
Christian Königedf600d2016-05-03 15:54:54 +0200378 adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400379 AMDGPU_DOORBELL_MAX_ASSIGNMENT+1);
380 if (adev->doorbell.num_doorbells == 0)
381 return -EINVAL;
382
383 adev->doorbell.ptr = ioremap(adev->doorbell.base, adev->doorbell.num_doorbells * sizeof(u32));
384 if (adev->doorbell.ptr == NULL) {
385 return -ENOMEM;
386 }
387 DRM_INFO("doorbell mmio base: 0x%08X\n", (uint32_t)adev->doorbell.base);
388 DRM_INFO("doorbell mmio size: %u\n", (unsigned)adev->doorbell.size);
389
390 return 0;
391}
392
393/**
394 * amdgpu_doorbell_fini - Tear down doorbell driver information.
395 *
396 * @adev: amdgpu_device pointer
397 *
398 * Tear down doorbell driver information (CIK)
399 */
400static void amdgpu_doorbell_fini(struct amdgpu_device *adev)
401{
402 iounmap(adev->doorbell.ptr);
403 adev->doorbell.ptr = NULL;
404}
405
406/**
407 * amdgpu_doorbell_get_kfd_info - Report doorbell configuration required to
408 * setup amdkfd
409 *
410 * @adev: amdgpu_device pointer
411 * @aperture_base: output returning doorbell aperture base physical address
412 * @aperture_size: output returning doorbell aperture size in bytes
413 * @start_offset: output returning # of doorbell bytes reserved for amdgpu.
414 *
415 * amdgpu and amdkfd share the doorbell aperture. amdgpu sets it up,
416 * takes doorbells required for its own rings and reports the setup to amdkfd.
417 * amdgpu reserved doorbells are at the start of the doorbell aperture.
418 */
419void amdgpu_doorbell_get_kfd_info(struct amdgpu_device *adev,
420 phys_addr_t *aperture_base,
421 size_t *aperture_size,
422 size_t *start_offset)
423{
424 /*
425 * The first num_doorbells are used by amdgpu.
426 * amdkfd takes whatever's left in the aperture.
427 */
428 if (adev->doorbell.size > adev->doorbell.num_doorbells * sizeof(u32)) {
429 *aperture_base = adev->doorbell.base;
430 *aperture_size = adev->doorbell.size;
431 *start_offset = adev->doorbell.num_doorbells * sizeof(u32);
432 } else {
433 *aperture_base = 0;
434 *aperture_size = 0;
435 *start_offset = 0;
436 }
437}
438
439/*
440 * amdgpu_wb_*()
441 * Writeback is the the method by which the the GPU updates special pages
442 * in memory with the status of certain GPU events (fences, ring pointers,
443 * etc.).
444 */
445
446/**
447 * amdgpu_wb_fini - Disable Writeback and free memory
448 *
449 * @adev: amdgpu_device pointer
450 *
451 * Disables Writeback and frees the Writeback memory (all asics).
452 * Used at driver shutdown.
453 */
454static void amdgpu_wb_fini(struct amdgpu_device *adev)
455{
456 if (adev->wb.wb_obj) {
Alex Deuchera76ed482016-10-21 15:30:36 -0400457 amdgpu_bo_free_kernel(&adev->wb.wb_obj,
458 &adev->wb.gpu_addr,
459 (void **)&adev->wb.wb);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400460 adev->wb.wb_obj = NULL;
461 }
462}
463
464/**
465 * amdgpu_wb_init- Init Writeback driver info and allocate memory
466 *
467 * @adev: amdgpu_device pointer
468 *
469 * Disables Writeback and frees the Writeback memory (all asics).
470 * Used at driver startup.
471 * Returns 0 on success or an -error on failure.
472 */
473static int amdgpu_wb_init(struct amdgpu_device *adev)
474{
475 int r;
476
477 if (adev->wb.wb_obj == NULL) {
Alex Deuchera76ed482016-10-21 15:30:36 -0400478 r = amdgpu_bo_create_kernel(adev, AMDGPU_MAX_WB * 4,
479 PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
480 &adev->wb.wb_obj, &adev->wb.gpu_addr,
481 (void **)&adev->wb.wb);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400482 if (r) {
483 dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
484 return r;
485 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400486
487 adev->wb.num_wb = AMDGPU_MAX_WB;
488 memset(&adev->wb.used, 0, sizeof(adev->wb.used));
489
490 /* clear wb memory */
491 memset((char *)adev->wb.wb, 0, AMDGPU_GPU_PAGE_SIZE);
492 }
493
494 return 0;
495}
496
497/**
498 * amdgpu_wb_get - Allocate a wb entry
499 *
500 * @adev: amdgpu_device pointer
501 * @wb: wb index
502 *
503 * Allocate a wb slot for use by the driver (all asics).
504 * Returns 0 on success or -EINVAL on failure.
505 */
506int amdgpu_wb_get(struct amdgpu_device *adev, u32 *wb)
507{
508 unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
509 if (offset < adev->wb.num_wb) {
510 __set_bit(offset, adev->wb.used);
511 *wb = offset;
512 return 0;
513 } else {
514 return -EINVAL;
515 }
516}
517
518/**
519 * amdgpu_wb_free - Free a wb entry
520 *
521 * @adev: amdgpu_device pointer
522 * @wb: wb index
523 *
524 * Free a wb slot allocated for use by the driver (all asics)
525 */
526void amdgpu_wb_free(struct amdgpu_device *adev, u32 wb)
527{
528 if (wb < adev->wb.num_wb)
529 __clear_bit(wb, adev->wb.used);
530}
531
532/**
533 * amdgpu_vram_location - try to find VRAM location
534 * @adev: amdgpu device structure holding all necessary informations
535 * @mc: memory controller structure holding memory informations
536 * @base: base address at which to put VRAM
537 *
538 * Function will place try to place VRAM at base address provided
539 * as parameter (which is so far either PCI aperture address or
540 * for IGP TOM base address).
541 *
542 * If there is not enough space to fit the unvisible VRAM in the 32bits
543 * address space then we limit the VRAM size to the aperture.
544 *
545 * Note: We don't explicitly enforce VRAM start to be aligned on VRAM size,
546 * this shouldn't be a problem as we are using the PCI aperture as a reference.
547 * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but
548 * not IGP.
549 *
550 * Note: we use mc_vram_size as on some board we need to program the mc to
551 * cover the whole aperture even if VRAM size is inferior to aperture size
552 * Novell bug 204882 + along with lots of ubuntu ones
553 *
554 * Note: when limiting vram it's safe to overwritte real_vram_size because
555 * we are not in case where real_vram_size is inferior to mc_vram_size (ie
556 * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu
557 * ones)
558 *
559 * Note: IGP TOM addr should be the same as the aperture addr, we don't
560 * explicitly check for that thought.
561 *
562 * FIXME: when reducing VRAM size align new size on power of 2.
563 */
564void amdgpu_vram_location(struct amdgpu_device *adev, struct amdgpu_mc *mc, u64 base)
565{
566 uint64_t limit = (uint64_t)amdgpu_vram_limit << 20;
567
568 mc->vram_start = base;
569 if (mc->mc_vram_size > (adev->mc.mc_mask - base + 1)) {
570 dev_warn(adev->dev, "limiting VRAM to PCI aperture size\n");
571 mc->real_vram_size = mc->aper_size;
572 mc->mc_vram_size = mc->aper_size;
573 }
574 mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
575 if (limit && limit < mc->real_vram_size)
576 mc->real_vram_size = limit;
577 dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
578 mc->mc_vram_size >> 20, mc->vram_start,
579 mc->vram_end, mc->real_vram_size >> 20);
580}
581
582/**
583 * amdgpu_gtt_location - try to find GTT location
584 * @adev: amdgpu device structure holding all necessary informations
585 * @mc: memory controller structure holding memory informations
586 *
587 * Function will place try to place GTT before or after VRAM.
588 *
589 * If GTT size is bigger than space left then we ajust GTT size.
590 * Thus function will never fails.
591 *
592 * FIXME: when reducing GTT size align new size on power of 2.
593 */
594void amdgpu_gtt_location(struct amdgpu_device *adev, struct amdgpu_mc *mc)
595{
596 u64 size_af, size_bf;
597
598 size_af = ((adev->mc.mc_mask - mc->vram_end) + mc->gtt_base_align) & ~mc->gtt_base_align;
599 size_bf = mc->vram_start & ~mc->gtt_base_align;
600 if (size_bf > size_af) {
601 if (mc->gtt_size > size_bf) {
602 dev_warn(adev->dev, "limiting GTT\n");
603 mc->gtt_size = size_bf;
604 }
605 mc->gtt_start = (mc->vram_start & ~mc->gtt_base_align) - mc->gtt_size;
606 } else {
607 if (mc->gtt_size > size_af) {
608 dev_warn(adev->dev, "limiting GTT\n");
609 mc->gtt_size = size_af;
610 }
611 mc->gtt_start = (mc->vram_end + 1 + mc->gtt_base_align) & ~mc->gtt_base_align;
612 }
613 mc->gtt_end = mc->gtt_start + mc->gtt_size - 1;
614 dev_info(adev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
615 mc->gtt_size >> 20, mc->gtt_start, mc->gtt_end);
616}
617
618/*
619 * GPU helpers function.
620 */
621/**
622 * amdgpu_card_posted - check if the hw has already been initialized
623 *
624 * @adev: amdgpu_device pointer
625 *
626 * Check if the asic has been initialized (all asics).
627 * Used at driver startup.
628 * Returns true if initialized or false if not.
629 */
630bool amdgpu_card_posted(struct amdgpu_device *adev)
631{
632 uint32_t reg;
633
634 /* then check MEM_SIZE, in case the crtcs are off */
635 reg = RREG32(mmCONFIG_MEMSIZE);
636
637 if (reg)
638 return true;
639
640 return false;
641
642}
643
Monk Liubec86372016-09-14 19:38:08 +0800644static bool amdgpu_vpost_needed(struct amdgpu_device *adev)
645{
646 if (amdgpu_sriov_vf(adev))
647 return false;
648
649 if (amdgpu_passthrough(adev)) {
Monk Liu1da2c322016-11-11 11:24:29 +0800650 /* for FIJI: In whole GPU pass-through virtualization case, after VM reboot
651 * some old smc fw still need driver do vPost otherwise gpu hang, while
652 * those smc fw version above 22.15 doesn't have this flaw, so we force
653 * vpost executed for smc version below 22.15
Monk Liubec86372016-09-14 19:38:08 +0800654 */
655 if (adev->asic_type == CHIP_FIJI) {
656 int err;
657 uint32_t fw_ver;
658 err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
659 /* force vPost if error occured */
660 if (err)
661 return true;
662
663 fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
Monk Liu1da2c322016-11-11 11:24:29 +0800664 if (fw_ver < 0x00160e00)
665 return true;
Monk Liubec86372016-09-14 19:38:08 +0800666 }
Monk Liubec86372016-09-14 19:38:08 +0800667 }
Monk Liu1da2c322016-11-11 11:24:29 +0800668 return !amdgpu_card_posted(adev);
Monk Liubec86372016-09-14 19:38:08 +0800669}
670
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400671/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400672 * amdgpu_dummy_page_init - init dummy page used by the driver
673 *
674 * @adev: amdgpu_device pointer
675 *
676 * Allocate the dummy page used by the driver (all asics).
677 * This dummy page is used by the driver as a filler for gart entries
678 * when pages are taken out of the GART
679 * Returns 0 on sucess, -ENOMEM on failure.
680 */
681int amdgpu_dummy_page_init(struct amdgpu_device *adev)
682{
683 if (adev->dummy_page.page)
684 return 0;
685 adev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
686 if (adev->dummy_page.page == NULL)
687 return -ENOMEM;
688 adev->dummy_page.addr = pci_map_page(adev->pdev, adev->dummy_page.page,
689 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
690 if (pci_dma_mapping_error(adev->pdev, adev->dummy_page.addr)) {
691 dev_err(&adev->pdev->dev, "Failed to DMA MAP the dummy page\n");
692 __free_page(adev->dummy_page.page);
693 adev->dummy_page.page = NULL;
694 return -ENOMEM;
695 }
696 return 0;
697}
698
699/**
700 * amdgpu_dummy_page_fini - free dummy page used by the driver
701 *
702 * @adev: amdgpu_device pointer
703 *
704 * Frees the dummy page used by the driver (all asics).
705 */
706void amdgpu_dummy_page_fini(struct amdgpu_device *adev)
707{
708 if (adev->dummy_page.page == NULL)
709 return;
710 pci_unmap_page(adev->pdev, adev->dummy_page.addr,
711 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
712 __free_page(adev->dummy_page.page);
713 adev->dummy_page.page = NULL;
714}
715
716
717/* ATOM accessor methods */
718/*
719 * ATOM is an interpreted byte code stored in tables in the vbios. The
720 * driver registers callbacks to access registers and the interpreter
721 * in the driver parses the tables and executes then to program specific
722 * actions (set display modes, asic init, etc.). See amdgpu_atombios.c,
723 * atombios.h, and atom.c
724 */
725
726/**
727 * cail_pll_read - read PLL register
728 *
729 * @info: atom card_info pointer
730 * @reg: PLL register offset
731 *
732 * Provides a PLL register accessor for the atom interpreter (r4xx+).
733 * Returns the value of the PLL register.
734 */
735static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
736{
737 return 0;
738}
739
740/**
741 * cail_pll_write - write PLL register
742 *
743 * @info: atom card_info pointer
744 * @reg: PLL register offset
745 * @val: value to write to the pll register
746 *
747 * Provides a PLL register accessor for the atom interpreter (r4xx+).
748 */
749static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
750{
751
752}
753
754/**
755 * cail_mc_read - read MC (Memory Controller) register
756 *
757 * @info: atom card_info pointer
758 * @reg: MC register offset
759 *
760 * Provides an MC register accessor for the atom interpreter (r4xx+).
761 * Returns the value of the MC register.
762 */
763static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
764{
765 return 0;
766}
767
768/**
769 * cail_mc_write - write MC (Memory Controller) register
770 *
771 * @info: atom card_info pointer
772 * @reg: MC register offset
773 * @val: value to write to the pll register
774 *
775 * Provides a MC register accessor for the atom interpreter (r4xx+).
776 */
777static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
778{
779
780}
781
782/**
783 * cail_reg_write - write MMIO register
784 *
785 * @info: atom card_info pointer
786 * @reg: MMIO register offset
787 * @val: value to write to the pll register
788 *
789 * Provides a MMIO register accessor for the atom interpreter (r4xx+).
790 */
791static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
792{
793 struct amdgpu_device *adev = info->dev->dev_private;
794
795 WREG32(reg, val);
796}
797
798/**
799 * cail_reg_read - read MMIO register
800 *
801 * @info: atom card_info pointer
802 * @reg: MMIO register offset
803 *
804 * Provides an MMIO register accessor for the atom interpreter (r4xx+).
805 * Returns the value of the MMIO register.
806 */
807static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
808{
809 struct amdgpu_device *adev = info->dev->dev_private;
810 uint32_t r;
811
812 r = RREG32(reg);
813 return r;
814}
815
816/**
817 * cail_ioreg_write - write IO register
818 *
819 * @info: atom card_info pointer
820 * @reg: IO register offset
821 * @val: value to write to the pll register
822 *
823 * Provides a IO register accessor for the atom interpreter (r4xx+).
824 */
825static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
826{
827 struct amdgpu_device *adev = info->dev->dev_private;
828
829 WREG32_IO(reg, val);
830}
831
832/**
833 * cail_ioreg_read - read IO register
834 *
835 * @info: atom card_info pointer
836 * @reg: IO register offset
837 *
838 * Provides an IO register accessor for the atom interpreter (r4xx+).
839 * Returns the value of the IO register.
840 */
841static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
842{
843 struct amdgpu_device *adev = info->dev->dev_private;
844 uint32_t r;
845
846 r = RREG32_IO(reg);
847 return r;
848}
849
850/**
851 * amdgpu_atombios_fini - free the driver info and callbacks for atombios
852 *
853 * @adev: amdgpu_device pointer
854 *
855 * Frees the driver info and register access callbacks for the ATOM
856 * interpreter (r4xx+).
857 * Called at driver shutdown.
858 */
859static void amdgpu_atombios_fini(struct amdgpu_device *adev)
860{
Monk Liu89e0ec9f2016-05-27 19:34:11 +0800861 if (adev->mode_info.atom_context) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400862 kfree(adev->mode_info.atom_context->scratch);
Monk Liu89e0ec9f2016-05-27 19:34:11 +0800863 kfree(adev->mode_info.atom_context->iio);
864 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400865 kfree(adev->mode_info.atom_context);
866 adev->mode_info.atom_context = NULL;
867 kfree(adev->mode_info.atom_card_info);
868 adev->mode_info.atom_card_info = NULL;
869}
870
871/**
872 * amdgpu_atombios_init - init the driver info and callbacks for atombios
873 *
874 * @adev: amdgpu_device pointer
875 *
876 * Initializes the driver info and register access callbacks for the
877 * ATOM interpreter (r4xx+).
878 * Returns 0 on sucess, -ENOMEM on failure.
879 * Called at driver startup.
880 */
881static int amdgpu_atombios_init(struct amdgpu_device *adev)
882{
883 struct card_info *atom_card_info =
884 kzalloc(sizeof(struct card_info), GFP_KERNEL);
885
886 if (!atom_card_info)
887 return -ENOMEM;
888
889 adev->mode_info.atom_card_info = atom_card_info;
890 atom_card_info->dev = adev->ddev;
891 atom_card_info->reg_read = cail_reg_read;
892 atom_card_info->reg_write = cail_reg_write;
893 /* needed for iio ops */
894 if (adev->rio_mem) {
895 atom_card_info->ioreg_read = cail_ioreg_read;
896 atom_card_info->ioreg_write = cail_ioreg_write;
897 } else {
Amber Linb64a18c2017-01-04 08:06:58 -0500898 DRM_INFO("PCI I/O BAR is not found. Using MMIO to access ATOM BIOS\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400899 atom_card_info->ioreg_read = cail_reg_read;
900 atom_card_info->ioreg_write = cail_reg_write;
901 }
902 atom_card_info->mc_read = cail_mc_read;
903 atom_card_info->mc_write = cail_mc_write;
904 atom_card_info->pll_read = cail_pll_read;
905 atom_card_info->pll_write = cail_pll_write;
906
907 adev->mode_info.atom_context = amdgpu_atom_parse(atom_card_info, adev->bios);
908 if (!adev->mode_info.atom_context) {
909 amdgpu_atombios_fini(adev);
910 return -ENOMEM;
911 }
912
913 mutex_init(&adev->mode_info.atom_context->mutex);
914 amdgpu_atombios_scratch_regs_init(adev);
915 amdgpu_atom_allocate_fb_scratch(adev->mode_info.atom_context);
916 return 0;
917}
918
919/* if we get transitioned to only one device, take VGA back */
920/**
921 * amdgpu_vga_set_decode - enable/disable vga decode
922 *
923 * @cookie: amdgpu_device pointer
924 * @state: enable/disable vga decode
925 *
926 * Enable/disable vga decode (all asics).
927 * Returns VGA resource flags.
928 */
929static unsigned int amdgpu_vga_set_decode(void *cookie, bool state)
930{
931 struct amdgpu_device *adev = cookie;
932 amdgpu_asic_set_vga_state(adev, state);
933 if (state)
934 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
935 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
936 else
937 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
938}
939
940/**
941 * amdgpu_check_pot_argument - check that argument is a power of two
942 *
943 * @arg: value to check
944 *
945 * Validates that a certain argument is a power of two (all asics).
946 * Returns true if argument is valid.
947 */
948static bool amdgpu_check_pot_argument(int arg)
949{
950 return (arg & (arg - 1)) == 0;
951}
952
953/**
954 * amdgpu_check_arguments - validate module params
955 *
956 * @adev: amdgpu_device pointer
957 *
958 * Validates certain module parameters and updates
959 * the associated values used by the driver (all asics).
960 */
961static void amdgpu_check_arguments(struct amdgpu_device *adev)
962{
Chunming Zhou5b011232015-12-10 17:34:33 +0800963 if (amdgpu_sched_jobs < 4) {
964 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
965 amdgpu_sched_jobs);
966 amdgpu_sched_jobs = 4;
967 } else if (!amdgpu_check_pot_argument(amdgpu_sched_jobs)){
968 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
969 amdgpu_sched_jobs);
970 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
971 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400972
973 if (amdgpu_gart_size != -1) {
Christian Königc4e1a132016-03-17 16:25:15 +0100974 /* gtt size must be greater or equal to 32M */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400975 if (amdgpu_gart_size < 32) {
976 dev_warn(adev->dev, "gart size (%d) too small\n",
977 amdgpu_gart_size);
978 amdgpu_gart_size = -1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400979 }
980 }
981
982 if (!amdgpu_check_pot_argument(amdgpu_vm_size)) {
983 dev_warn(adev->dev, "VM size (%d) must be a power of 2\n",
984 amdgpu_vm_size);
Alex Deucher8dacc122015-05-11 16:20:58 -0400985 amdgpu_vm_size = 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400986 }
987
988 if (amdgpu_vm_size < 1) {
989 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
990 amdgpu_vm_size);
Alex Deucher8dacc122015-05-11 16:20:58 -0400991 amdgpu_vm_size = 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400992 }
993
994 /*
995 * Max GPUVM size for Cayman, SI and CI are 40 bits.
996 */
997 if (amdgpu_vm_size > 1024) {
998 dev_warn(adev->dev, "VM size (%d) too large, max is 1TB\n",
999 amdgpu_vm_size);
Alex Deucher8dacc122015-05-11 16:20:58 -04001000 amdgpu_vm_size = 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001001 }
1002
1003 /* defines number of bits in page table versus page directory,
1004 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
1005 * page table and the remaining bits are in the page directory */
1006 if (amdgpu_vm_block_size == -1) {
1007
1008 /* Total bits covered by PD + PTs */
1009 unsigned bits = ilog2(amdgpu_vm_size) + 18;
1010
1011 /* Make sure the PD is 4K in size up to 8GB address space.
1012 Above that split equal between PD and PTs */
1013 if (amdgpu_vm_size <= 8)
1014 amdgpu_vm_block_size = bits - 9;
1015 else
1016 amdgpu_vm_block_size = (bits + 3) / 2;
1017
1018 } else if (amdgpu_vm_block_size < 9) {
1019 dev_warn(adev->dev, "VM page table size (%d) too small\n",
1020 amdgpu_vm_block_size);
1021 amdgpu_vm_block_size = 9;
1022 }
1023
1024 if (amdgpu_vm_block_size > 24 ||
1025 (amdgpu_vm_size * 1024) < (1ull << amdgpu_vm_block_size)) {
1026 dev_warn(adev->dev, "VM page table size (%d) too large\n",
1027 amdgpu_vm_block_size);
1028 amdgpu_vm_block_size = 9;
1029 }
Christian König6a7f76e2016-08-24 15:51:49 +02001030
jimqu526bae32016-11-07 09:53:10 +08001031 if (amdgpu_vram_page_split != -1 && (amdgpu_vram_page_split < 16 ||
1032 !amdgpu_check_pot_argument(amdgpu_vram_page_split))) {
Christian König6a7f76e2016-08-24 15:51:49 +02001033 dev_warn(adev->dev, "invalid VRAM page split (%d)\n",
1034 amdgpu_vram_page_split);
1035 amdgpu_vram_page_split = 1024;
1036 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001037}
1038
1039/**
1040 * amdgpu_switcheroo_set_state - set switcheroo state
1041 *
1042 * @pdev: pci dev pointer
Lukas Wunner16944672015-09-05 11:17:35 +02001043 * @state: vga_switcheroo state
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001044 *
1045 * Callback for the switcheroo driver. Suspends or resumes the
1046 * the asics before or after it is powered up using ACPI methods.
1047 */
1048static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1049{
1050 struct drm_device *dev = pci_get_drvdata(pdev);
1051
1052 if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
1053 return;
1054
1055 if (state == VGA_SWITCHEROO_ON) {
1056 unsigned d3_delay = dev->pdev->d3_delay;
1057
1058 printk(KERN_INFO "amdgpu: switched on\n");
1059 /* don't suspend or resume card normally */
1060 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1061
Alex Deucher810ddc32016-08-23 13:25:49 -04001062 amdgpu_device_resume(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001063
1064 dev->pdev->d3_delay = d3_delay;
1065
1066 dev->switch_power_state = DRM_SWITCH_POWER_ON;
1067 drm_kms_helper_poll_enable(dev);
1068 } else {
1069 printk(KERN_INFO "amdgpu: switched off\n");
1070 drm_kms_helper_poll_disable(dev);
1071 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Alex Deucher810ddc32016-08-23 13:25:49 -04001072 amdgpu_device_suspend(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001073 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1074 }
1075}
1076
1077/**
1078 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1079 *
1080 * @pdev: pci dev pointer
1081 *
1082 * Callback for the switcheroo driver. Check of the switcheroo
1083 * state can be changed.
1084 * Returns true if the state can be changed, false if not.
1085 */
1086static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1087{
1088 struct drm_device *dev = pci_get_drvdata(pdev);
1089
1090 /*
1091 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1092 * locking inversion with the driver load path. And the access here is
1093 * completely racy anyway. So don't bother with locking for now.
1094 */
1095 return dev->open_count == 0;
1096}
1097
1098static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1099 .set_gpu_state = amdgpu_switcheroo_set_state,
1100 .reprobe = NULL,
1101 .can_switch = amdgpu_switcheroo_can_switch,
1102};
1103
1104int amdgpu_set_clockgating_state(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001105 enum amd_ip_block_type block_type,
1106 enum amd_clockgating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001107{
1108 int i, r = 0;
1109
1110 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001111 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001112 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001113 if (adev->ip_blocks[i].version->type == block_type) {
1114 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1115 state);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001116 if (r)
1117 return r;
Alex Deuchera225bf12016-06-23 11:48:30 -04001118 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001119 }
1120 }
1121 return r;
1122}
1123
1124int amdgpu_set_powergating_state(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001125 enum amd_ip_block_type block_type,
1126 enum amd_powergating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001127{
1128 int i, r = 0;
1129
1130 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001131 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001132 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001133 if (adev->ip_blocks[i].version->type == block_type) {
1134 r = adev->ip_blocks[i].version->funcs->set_powergating_state((void *)adev,
1135 state);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001136 if (r)
1137 return r;
Alex Deuchera225bf12016-06-23 11:48:30 -04001138 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001139 }
1140 }
1141 return r;
1142}
1143
Huang Rui6cb2d4e2017-01-05 18:44:41 +08001144void amdgpu_get_clockgating_state(struct amdgpu_device *adev, u32 *flags)
1145{
1146 int i;
1147
1148 for (i = 0; i < adev->num_ip_blocks; i++) {
1149 if (!adev->ip_blocks[i].status.valid)
1150 continue;
1151 if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
1152 adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
1153 }
1154}
1155
Alex Deucher5dbbb602016-06-23 11:41:04 -04001156int amdgpu_wait_for_idle(struct amdgpu_device *adev,
1157 enum amd_ip_block_type block_type)
1158{
1159 int i, r;
1160
1161 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001162 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001163 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001164 if (adev->ip_blocks[i].version->type == block_type) {
1165 r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -04001166 if (r)
1167 return r;
1168 break;
1169 }
1170 }
1171 return 0;
1172
1173}
1174
1175bool amdgpu_is_idle(struct amdgpu_device *adev,
1176 enum amd_ip_block_type block_type)
1177{
1178 int i;
1179
1180 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001181 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001182 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001183 if (adev->ip_blocks[i].version->type == block_type)
1184 return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -04001185 }
1186 return true;
1187
1188}
1189
Alex Deuchera1255102016-10-13 17:41:13 -04001190struct amdgpu_ip_block * amdgpu_get_ip_block(struct amdgpu_device *adev,
1191 enum amd_ip_block_type type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001192{
1193 int i;
1194
1195 for (i = 0; i < adev->num_ip_blocks; i++)
Alex Deuchera1255102016-10-13 17:41:13 -04001196 if (adev->ip_blocks[i].version->type == type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001197 return &adev->ip_blocks[i];
1198
1199 return NULL;
1200}
1201
1202/**
1203 * amdgpu_ip_block_version_cmp
1204 *
1205 * @adev: amdgpu_device pointer
yanyang15fc3aee2015-05-22 14:39:35 -04001206 * @type: enum amd_ip_block_type
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001207 * @major: major version
1208 * @minor: minor version
1209 *
1210 * return 0 if equal or greater
1211 * return 1 if smaller or the ip_block doesn't exist
1212 */
1213int amdgpu_ip_block_version_cmp(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001214 enum amd_ip_block_type type,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001215 u32 major, u32 minor)
1216{
Alex Deuchera1255102016-10-13 17:41:13 -04001217 struct amdgpu_ip_block *ip_block = amdgpu_get_ip_block(adev, type);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001218
Alex Deuchera1255102016-10-13 17:41:13 -04001219 if (ip_block && ((ip_block->version->major > major) ||
1220 ((ip_block->version->major == major) &&
1221 (ip_block->version->minor >= minor))))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001222 return 0;
1223
1224 return 1;
1225}
1226
Alex Deuchera1255102016-10-13 17:41:13 -04001227/**
1228 * amdgpu_ip_block_add
1229 *
1230 * @adev: amdgpu_device pointer
1231 * @ip_block_version: pointer to the IP to add
1232 *
1233 * Adds the IP block driver information to the collection of IPs
1234 * on the asic.
1235 */
1236int amdgpu_ip_block_add(struct amdgpu_device *adev,
1237 const struct amdgpu_ip_block_version *ip_block_version)
1238{
1239 if (!ip_block_version)
1240 return -EINVAL;
1241
1242 adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
1243
1244 return 0;
1245}
1246
Alex Deucher483ef982016-09-30 12:43:04 -04001247static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
Emily Deng9accf2f2016-08-10 16:01:25 +08001248{
1249 adev->enable_virtual_display = false;
1250
1251 if (amdgpu_virtual_display) {
1252 struct drm_device *ddev = adev->ddev;
1253 const char *pci_address_name = pci_name(ddev->pdev);
Emily Deng0f663562016-09-30 13:02:18 -04001254 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
Emily Deng9accf2f2016-08-10 16:01:25 +08001255
1256 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1257 pciaddstr_tmp = pciaddstr;
Emily Deng0f663562016-09-30 13:02:18 -04001258 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1259 pciaddname = strsep(&pciaddname_tmp, ",");
Emily Deng9accf2f2016-08-10 16:01:25 +08001260 if (!strcmp(pci_address_name, pciaddname)) {
Emily Deng0f663562016-09-30 13:02:18 -04001261 long num_crtc;
1262 int res = -1;
1263
Emily Deng9accf2f2016-08-10 16:01:25 +08001264 adev->enable_virtual_display = true;
Emily Deng0f663562016-09-30 13:02:18 -04001265
1266 if (pciaddname_tmp)
1267 res = kstrtol(pciaddname_tmp, 10,
1268 &num_crtc);
1269
1270 if (!res) {
1271 if (num_crtc < 1)
1272 num_crtc = 1;
1273 if (num_crtc > 6)
1274 num_crtc = 6;
1275 adev->mode_info.num_crtc = num_crtc;
1276 } else {
1277 adev->mode_info.num_crtc = 1;
1278 }
Emily Deng9accf2f2016-08-10 16:01:25 +08001279 break;
1280 }
1281 }
1282
Emily Deng0f663562016-09-30 13:02:18 -04001283 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
1284 amdgpu_virtual_display, pci_address_name,
1285 adev->enable_virtual_display, adev->mode_info.num_crtc);
Emily Deng9accf2f2016-08-10 16:01:25 +08001286
1287 kfree(pciaddstr);
1288 }
1289}
1290
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001291static int amdgpu_early_init(struct amdgpu_device *adev)
1292{
Alex Deucheraaa36a92015-04-20 17:31:14 -04001293 int i, r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001294
Alex Deucher483ef982016-09-30 12:43:04 -04001295 amdgpu_device_enable_virtual_display(adev);
Emily Denga6be7572016-08-08 11:37:50 +08001296
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001297 switch (adev->asic_type) {
Alex Deucheraaa36a92015-04-20 17:31:14 -04001298 case CHIP_TOPAZ:
1299 case CHIP_TONGA:
David Zhang48299f92015-07-08 01:05:16 +08001300 case CHIP_FIJI:
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001301 case CHIP_POLARIS11:
1302 case CHIP_POLARIS10:
Junwei Zhangc4642a42016-12-14 15:32:28 -05001303 case CHIP_POLARIS12:
Alex Deucheraaa36a92015-04-20 17:31:14 -04001304 case CHIP_CARRIZO:
Samuel Li39bb0c92015-10-08 16:31:43 -04001305 case CHIP_STONEY:
1306 if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
Alex Deucheraaa36a92015-04-20 17:31:14 -04001307 adev->family = AMDGPU_FAMILY_CZ;
1308 else
1309 adev->family = AMDGPU_FAMILY_VI;
1310
1311 r = vi_set_ip_blocks(adev);
1312 if (r)
1313 return r;
1314 break;
Ken Wang33f34802016-01-21 17:29:41 +08001315#ifdef CONFIG_DRM_AMDGPU_SI
1316 case CHIP_VERDE:
1317 case CHIP_TAHITI:
1318 case CHIP_PITCAIRN:
1319 case CHIP_OLAND:
1320 case CHIP_HAINAN:
Ken Wang295d0da2016-05-24 21:02:53 +08001321 adev->family = AMDGPU_FAMILY_SI;
Ken Wang33f34802016-01-21 17:29:41 +08001322 r = si_set_ip_blocks(adev);
1323 if (r)
1324 return r;
1325 break;
1326#endif
Alex Deuchera2e73f52015-04-20 17:09:27 -04001327#ifdef CONFIG_DRM_AMDGPU_CIK
1328 case CHIP_BONAIRE:
1329 case CHIP_HAWAII:
1330 case CHIP_KAVERI:
1331 case CHIP_KABINI:
1332 case CHIP_MULLINS:
1333 if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
1334 adev->family = AMDGPU_FAMILY_CI;
1335 else
1336 adev->family = AMDGPU_FAMILY_KV;
1337
1338 r = cik_set_ip_blocks(adev);
1339 if (r)
1340 return r;
1341 break;
1342#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001343 default:
1344 /* FIXME: not supported yet */
1345 return -EINVAL;
1346 }
1347
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001348 if (amdgpu_sriov_vf(adev)) {
1349 r = amdgpu_virt_request_full_gpu(adev, true);
1350 if (r)
1351 return r;
1352 }
1353
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001354 for (i = 0; i < adev->num_ip_blocks; i++) {
1355 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
1356 DRM_ERROR("disabled ip block: %d\n", i);
Alex Deuchera1255102016-10-13 17:41:13 -04001357 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001358 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001359 if (adev->ip_blocks[i].version->funcs->early_init) {
1360 r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001361 if (r == -ENOENT) {
Alex Deuchera1255102016-10-13 17:41:13 -04001362 adev->ip_blocks[i].status.valid = false;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001363 } else if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001364 DRM_ERROR("early_init of IP block <%s> failed %d\n",
1365 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001366 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001367 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001368 adev->ip_blocks[i].status.valid = true;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001369 }
Alex Deucher974e6b62015-07-10 13:59:44 -04001370 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001371 adev->ip_blocks[i].status.valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001372 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001373 }
1374 }
1375
Nicolai Hähnle395d1fb2016-06-02 12:32:07 +02001376 adev->cg_flags &= amdgpu_cg_mask;
1377 adev->pg_flags &= amdgpu_pg_mask;
1378
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001379 return 0;
1380}
1381
1382static int amdgpu_init(struct amdgpu_device *adev)
1383{
1384 int i, r;
1385
1386 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001387 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001388 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001389 r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001390 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001391 DRM_ERROR("sw_init of IP block <%s> failed %d\n",
1392 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001393 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001394 }
Alex Deuchera1255102016-10-13 17:41:13 -04001395 adev->ip_blocks[i].status.sw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001396 /* need to do gmc hw init early so we can allocate gpu mem */
Alex Deuchera1255102016-10-13 17:41:13 -04001397 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001398 r = amdgpu_vram_scratch_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001399 if (r) {
1400 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001401 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001402 }
Alex Deuchera1255102016-10-13 17:41:13 -04001403 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001404 if (r) {
1405 DRM_ERROR("hw_init %d failed %d\n", i, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001406 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001407 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001408 r = amdgpu_wb_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001409 if (r) {
1410 DRM_ERROR("amdgpu_wb_init failed %d\n", 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;
Monk Liu24936642017-01-09 15:54:32 +08001414
1415 /* right after GMC hw init, we create CSA */
1416 if (amdgpu_sriov_vf(adev)) {
1417 r = amdgpu_allocate_static_csa(adev);
1418 if (r) {
1419 DRM_ERROR("allocate CSA failed %d\n", r);
1420 return r;
1421 }
1422 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001423 }
1424 }
1425
1426 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001427 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001428 continue;
1429 /* gmc hw init is done early */
Alex Deuchera1255102016-10-13 17:41:13 -04001430 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001431 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001432 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001433 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001434 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
1435 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001436 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001437 }
Alex Deuchera1255102016-10-13 17:41:13 -04001438 adev->ip_blocks[i].status.hw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001439 }
1440
1441 return 0;
1442}
1443
1444static int amdgpu_late_init(struct amdgpu_device *adev)
1445{
1446 int i = 0, r;
1447
1448 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001449 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001450 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001451 if (adev->ip_blocks[i].version->funcs->late_init) {
1452 r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001453 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001454 DRM_ERROR("late_init of IP block <%s> failed %d\n",
1455 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001456 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001457 }
Alex Deuchera1255102016-10-13 17:41:13 -04001458 adev->ip_blocks[i].status.late_initialized = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001459 }
Alex Deucher4a446d52016-10-07 14:48:18 -04001460 /* skip CG for VCE/UVD, it's handled specially */
Alex Deuchera1255102016-10-13 17:41:13 -04001461 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1462 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
Alex Deucher4a446d52016-10-07 14:48:18 -04001463 /* enable clockgating to save power */
Alex Deuchera1255102016-10-13 17:41:13 -04001464 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1465 AMD_CG_STATE_GATE);
Alex Deucher4a446d52016-10-07 14:48:18 -04001466 if (r) {
1467 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001468 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher4a446d52016-10-07 14:48:18 -04001469 return r;
1470 }
Arindam Nathb0b00ff2016-10-07 19:01:37 +05301471 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001472 }
1473
1474 return 0;
1475}
1476
1477static int amdgpu_fini(struct amdgpu_device *adev)
1478{
1479 int i, r;
1480
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001481 /* need to disable SMC first */
1482 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001483 if (!adev->ip_blocks[i].status.hw)
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001484 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001485 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001486 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
Alex Deuchera1255102016-10-13 17:41:13 -04001487 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1488 AMD_CG_STATE_UNGATE);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001489 if (r) {
1490 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001491 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001492 return r;
1493 }
Alex Deuchera1255102016-10-13 17:41:13 -04001494 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001495 /* XXX handle errors */
1496 if (r) {
1497 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001498 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001499 }
Alex Deuchera1255102016-10-13 17:41:13 -04001500 adev->ip_blocks[i].status.hw = false;
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001501 break;
1502 }
1503 }
1504
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001505 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001506 if (!adev->ip_blocks[i].status.hw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001507 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001508 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001509 amdgpu_wb_fini(adev);
1510 amdgpu_vram_scratch_fini(adev);
1511 }
Rex Zhu8201a672016-11-24 21:44:44 +08001512
1513 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1514 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1515 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
1516 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1517 AMD_CG_STATE_UNGATE);
1518 if (r) {
1519 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1520 adev->ip_blocks[i].version->funcs->name, r);
1521 return r;
1522 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001523 }
Rex Zhu8201a672016-11-24 21:44:44 +08001524
Alex Deuchera1255102016-10-13 17:41:13 -04001525 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001526 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001527 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001528 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
1529 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001530 }
Rex Zhu8201a672016-11-24 21:44:44 +08001531
Alex Deuchera1255102016-10-13 17:41:13 -04001532 adev->ip_blocks[i].status.hw = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001533 }
1534
1535 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001536 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001537 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001538 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001539 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001540 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001541 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
1542 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001543 }
Alex Deuchera1255102016-10-13 17:41:13 -04001544 adev->ip_blocks[i].status.sw = false;
1545 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001546 }
1547
Monk Liua6dcfd92016-05-19 14:36:34 +08001548 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001549 if (!adev->ip_blocks[i].status.late_initialized)
Grazvydas Ignotas8a2eef12016-10-03 00:06:44 +03001550 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001551 if (adev->ip_blocks[i].version->funcs->late_fini)
1552 adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
1553 adev->ip_blocks[i].status.late_initialized = false;
Monk Liua6dcfd92016-05-19 14:36:34 +08001554 }
1555
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001556 if (amdgpu_sriov_vf(adev)) {
Monk Liu24936642017-01-09 15:54:32 +08001557 amdgpu_bo_free_kernel(&adev->virt.csa_obj, &adev->virt.csa_vmid0_addr, NULL);
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001558 amdgpu_virt_release_full_gpu(adev, false);
1559 }
Monk Liu24936642017-01-09 15:54:32 +08001560
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001561 return 0;
1562}
1563
Alex Deucherfaefba92016-12-06 10:38:29 -05001564int amdgpu_suspend(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001565{
1566 int i, r;
1567
Flora Cuic5a93a22016-02-26 10:45:25 +08001568 /* ungate SMC block first */
1569 r = amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC,
1570 AMD_CG_STATE_UNGATE);
1571 if (r) {
1572 DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n",r);
1573 }
1574
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001575 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001576 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001577 continue;
1578 /* ungate blocks so that suspend can properly shut them down */
Flora Cuic5a93a22016-02-26 10:45:25 +08001579 if (i != AMD_IP_BLOCK_TYPE_SMC) {
Alex Deuchera1255102016-10-13 17:41:13 -04001580 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1581 AMD_CG_STATE_UNGATE);
Flora Cuic5a93a22016-02-26 10:45:25 +08001582 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001583 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1584 adev->ip_blocks[i].version->funcs->name, r);
Flora Cuic5a93a22016-02-26 10:45:25 +08001585 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001586 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001587 /* XXX handle errors */
Alex Deuchera1255102016-10-13 17:41:13 -04001588 r = adev->ip_blocks[i].version->funcs->suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001589 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001590 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001591 DRM_ERROR("suspend of IP block <%s> failed %d\n",
1592 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001593 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001594 }
1595
1596 return 0;
1597}
1598
1599static int amdgpu_resume(struct amdgpu_device *adev)
1600{
1601 int i, r;
1602
1603 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001604 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001605 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001606 r = adev->ip_blocks[i].version->funcs->resume(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001607 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001608 DRM_ERROR("resume of IP block <%s> failed %d\n",
1609 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001610 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001611 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001612 }
1613
1614 return 0;
1615}
1616
Monk Liu4e99a442016-03-31 13:26:59 +08001617static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
Andres Rodriguez048765a2016-06-11 02:51:32 -04001618{
Monk Liu4e99a442016-03-31 13:26:59 +08001619 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
Xiangliang Yu5a5099c2017-01-09 18:06:57 -05001620 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
Andres Rodriguez048765a2016-06-11 02:51:32 -04001621}
1622
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001623/**
1624 * amdgpu_device_init - initialize the driver
1625 *
1626 * @adev: amdgpu_device pointer
1627 * @pdev: drm dev pointer
1628 * @pdev: pci dev pointer
1629 * @flags: driver flags
1630 *
1631 * Initializes the driver info and hw (all asics).
1632 * Returns 0 for success or an error on failure.
1633 * Called at driver startup.
1634 */
1635int amdgpu_device_init(struct amdgpu_device *adev,
1636 struct drm_device *ddev,
1637 struct pci_dev *pdev,
1638 uint32_t flags)
1639{
1640 int r, i;
1641 bool runtime = false;
Marek Olšák95844d22016-08-17 23:49:27 +02001642 u32 max_MBps;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001643
1644 adev->shutdown = false;
1645 adev->dev = &pdev->dev;
1646 adev->ddev = ddev;
1647 adev->pdev = pdev;
1648 adev->flags = flags;
Jammy Zhou2f7d10b2015-07-22 11:29:01 +08001649 adev->asic_type = flags & AMD_ASIC_MASK;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001650 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
1651 adev->mc.gtt_size = 512 * 1024 * 1024;
1652 adev->accel_working = false;
1653 adev->num_rings = 0;
1654 adev->mman.buffer_funcs = NULL;
1655 adev->mman.buffer_funcs_ring = NULL;
1656 adev->vm_manager.vm_pte_funcs = NULL;
Christian König2d55e452016-02-08 17:37:38 +01001657 adev->vm_manager.vm_pte_num_rings = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001658 adev->gart.gart_funcs = NULL;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001659 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001660
1661 adev->smc_rreg = &amdgpu_invalid_rreg;
1662 adev->smc_wreg = &amdgpu_invalid_wreg;
1663 adev->pcie_rreg = &amdgpu_invalid_rreg;
1664 adev->pcie_wreg = &amdgpu_invalid_wreg;
Huang Rui36b9a952016-08-31 13:23:25 +08001665 adev->pciep_rreg = &amdgpu_invalid_rreg;
1666 adev->pciep_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001667 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
1668 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
1669 adev->didt_rreg = &amdgpu_invalid_rreg;
1670 adev->didt_wreg = &amdgpu_invalid_wreg;
Rex Zhuccdbb202016-06-08 12:47:41 +08001671 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
1672 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001673 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
1674 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
1675
Rex Zhuccdbb202016-06-08 12:47:41 +08001676
Alex Deucher3e39ab92015-06-05 15:04:33 -04001677 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
1678 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
1679 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001680
1681 /* mutex initialization are all done here so we
1682 * can recall function without having locking issues */
Christian König8d0a7ce2015-11-03 20:58:50 +01001683 mutex_init(&adev->vm_manager.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001684 atomic_set(&adev->irq.ih.lock, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001685 mutex_init(&adev->pm.mutex);
1686 mutex_init(&adev->gfx.gpu_clock_mutex);
1687 mutex_init(&adev->srbm_mutex);
1688 mutex_init(&adev->grbm_idx_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001689 mutex_init(&adev->mn_lock);
1690 hash_init(adev->mn_hash);
1691
1692 amdgpu_check_arguments(adev);
1693
1694 /* Registers mapping */
1695 /* TODO: block userspace mapping of io register */
1696 spin_lock_init(&adev->mmio_idx_lock);
1697 spin_lock_init(&adev->smc_idx_lock);
1698 spin_lock_init(&adev->pcie_idx_lock);
1699 spin_lock_init(&adev->uvd_ctx_idx_lock);
1700 spin_lock_init(&adev->didt_idx_lock);
Rex Zhuccdbb202016-06-08 12:47:41 +08001701 spin_lock_init(&adev->gc_cac_idx_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001702 spin_lock_init(&adev->audio_endpt_idx_lock);
Marek Olšák95844d22016-08-17 23:49:27 +02001703 spin_lock_init(&adev->mm_stats.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001704
Chunming Zhou0c4e7fa2016-08-17 11:41:30 +08001705 INIT_LIST_HEAD(&adev->shadow_list);
1706 mutex_init(&adev->shadow_list_lock);
1707
Chunming Zhou5c1354b2016-08-30 16:13:10 +08001708 INIT_LIST_HEAD(&adev->gtt_list);
1709 spin_lock_init(&adev->gtt_list_lock);
1710
Ken Wangda69c1612016-01-21 19:08:55 +08001711 if (adev->asic_type >= CHIP_BONAIRE) {
1712 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
1713 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
1714 } else {
1715 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
1716 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
1717 }
Chunming Zhou5c1354b2016-08-30 16:13:10 +08001718
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001719 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
1720 if (adev->rmmio == NULL) {
1721 return -ENOMEM;
1722 }
1723 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
1724 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
1725
Ken Wangda69c1612016-01-21 19:08:55 +08001726 if (adev->asic_type >= CHIP_BONAIRE)
1727 /* doorbell bar mapping */
1728 amdgpu_doorbell_init(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001729
1730 /* io port mapping */
1731 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
1732 if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
1733 adev->rio_mem_size = pci_resource_len(adev->pdev, i);
1734 adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
1735 break;
1736 }
1737 }
1738 if (adev->rio_mem == NULL)
Amber Linb64a18c2017-01-04 08:06:58 -05001739 DRM_INFO("PCI I/O BAR is not found.\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001740
1741 /* early init functions */
1742 r = amdgpu_early_init(adev);
1743 if (r)
1744 return r;
1745
1746 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
1747 /* this will fail for cards that aren't VGA class devices, just
1748 * ignore it */
1749 vga_client_register(adev->pdev, adev, NULL, amdgpu_vga_set_decode);
1750
1751 if (amdgpu_runtime_pm == 1)
1752 runtime = true;
Alex Deuchere9bef452016-04-25 13:12:18 -04001753 if (amdgpu_device_is_px(ddev))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001754 runtime = true;
1755 vga_switcheroo_register_client(adev->pdev, &amdgpu_switcheroo_ops, runtime);
1756 if (runtime)
1757 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
1758
1759 /* Read BIOS */
Alex Deucher83ba1262016-06-03 18:21:41 -04001760 if (!amdgpu_get_bios(adev)) {
1761 r = -EINVAL;
1762 goto failed;
1763 }
Nils Wallméniusf7e9e9f2016-12-14 21:52:45 +01001764
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001765 r = amdgpu_atombios_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001766 if (r) {
1767 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001768 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001769 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001770
Monk Liu4e99a442016-03-31 13:26:59 +08001771 /* detect if we are with an SRIOV vbios */
1772 amdgpu_device_detect_sriov_bios(adev);
Andres Rodriguez048765a2016-06-11 02:51:32 -04001773
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001774 /* Post card if necessary */
Monk Liubec86372016-09-14 19:38:08 +08001775 if (amdgpu_vpost_needed(adev)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001776 if (!adev->bios) {
Monk Liubec86372016-09-14 19:38:08 +08001777 dev_err(adev->dev, "no vBIOS found\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001778 r = -EINVAL;
1779 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001780 }
Monk Liubec86372016-09-14 19:38:08 +08001781 DRM_INFO("GPU posting now...\n");
Monk Liu4e99a442016-03-31 13:26:59 +08001782 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
1783 if (r) {
1784 dev_err(adev->dev, "gpu post error!\n");
1785 goto failed;
1786 }
1787 } else {
1788 DRM_INFO("GPU post is not needed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001789 }
1790
1791 /* Initialize clocks */
1792 r = amdgpu_atombios_get_clock_info(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001793 if (r) {
1794 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001795 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001796 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001797 /* init i2c buses */
1798 amdgpu_atombios_i2c_init(adev);
1799
1800 /* Fence driver */
1801 r = amdgpu_fence_driver_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001802 if (r) {
1803 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001804 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001805 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001806
1807 /* init the mode config */
1808 drm_mode_config_init(adev->ddev);
1809
1810 r = amdgpu_init(adev);
1811 if (r) {
Alex Deucher2c1a2782015-12-07 17:02:53 -05001812 dev_err(adev->dev, "amdgpu_init failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001813 amdgpu_fini(adev);
Alex Deucher83ba1262016-06-03 18:21:41 -04001814 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001815 }
1816
1817 adev->accel_working = true;
1818
Marek Olšák95844d22016-08-17 23:49:27 +02001819 /* Initialize the buffer migration limit. */
1820 if (amdgpu_moverate >= 0)
1821 max_MBps = amdgpu_moverate;
1822 else
1823 max_MBps = 8; /* Allow 8 MB/s. */
1824 /* Get a log2 for easy divisions. */
1825 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
1826
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001827 amdgpu_fbdev_init(adev);
1828
1829 r = amdgpu_ib_pool_init(adev);
1830 if (r) {
1831 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
Alex Deucher83ba1262016-06-03 18:21:41 -04001832 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001833 }
1834
1835 r = amdgpu_ib_ring_tests(adev);
1836 if (r)
1837 DRM_ERROR("ib ring test failed (%d).\n", r);
1838
1839 r = amdgpu_gem_debugfs_init(adev);
1840 if (r) {
1841 DRM_ERROR("registering gem debugfs failed (%d).\n", r);
1842 }
1843
1844 r = amdgpu_debugfs_regs_init(adev);
1845 if (r) {
1846 DRM_ERROR("registering register debugfs failed (%d).\n", r);
1847 }
1848
Huang Rui50ab2532016-06-12 15:51:09 +08001849 r = amdgpu_debugfs_firmware_init(adev);
1850 if (r) {
1851 DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
1852 return r;
1853 }
1854
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001855 if ((amdgpu_testing & 1)) {
1856 if (adev->accel_working)
1857 amdgpu_test_moves(adev);
1858 else
1859 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
1860 }
1861 if ((amdgpu_testing & 2)) {
1862 if (adev->accel_working)
1863 amdgpu_test_syncing(adev);
1864 else
1865 DRM_INFO("amdgpu: acceleration disabled, skipping sync tests\n");
1866 }
1867 if (amdgpu_benchmarking) {
1868 if (adev->accel_working)
1869 amdgpu_benchmark(adev, amdgpu_benchmarking);
1870 else
1871 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
1872 }
1873
1874 /* enable clockgating, etc. after ib tests, etc. since some blocks require
1875 * explicit gating rather than handling it automatically.
1876 */
1877 r = amdgpu_late_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001878 if (r) {
1879 dev_err(adev->dev, "amdgpu_late_init failed\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001880 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001881 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001882
1883 return 0;
Alex Deucher83ba1262016-06-03 18:21:41 -04001884
1885failed:
1886 if (runtime)
1887 vga_switcheroo_fini_domain_pm_ops(adev->dev);
1888 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001889}
1890
1891static void amdgpu_debugfs_remove_files(struct amdgpu_device *adev);
1892
1893/**
1894 * amdgpu_device_fini - tear down the driver
1895 *
1896 * @adev: amdgpu_device pointer
1897 *
1898 * Tear down the driver info (all asics).
1899 * Called at driver shutdown.
1900 */
1901void amdgpu_device_fini(struct amdgpu_device *adev)
1902{
1903 int r;
1904
1905 DRM_INFO("amdgpu: finishing device.\n");
1906 adev->shutdown = true;
Grazvydas Ignotasa951ed82016-09-25 23:34:48 +03001907 drm_crtc_force_disable_all(adev->ddev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001908 /* evict vram memory */
1909 amdgpu_bo_evict_vram(adev);
1910 amdgpu_ib_pool_fini(adev);
1911 amdgpu_fence_driver_fini(adev);
1912 amdgpu_fbdev_fini(adev);
1913 r = amdgpu_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001914 adev->accel_working = false;
1915 /* free i2c buses */
1916 amdgpu_i2c_fini(adev);
1917 amdgpu_atombios_fini(adev);
1918 kfree(adev->bios);
1919 adev->bios = NULL;
1920 vga_switcheroo_unregister_client(adev->pdev);
Alex Deucher83ba1262016-06-03 18:21:41 -04001921 if (adev->flags & AMD_IS_PX)
1922 vga_switcheroo_fini_domain_pm_ops(adev->dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001923 vga_client_register(adev->pdev, NULL, NULL, NULL);
1924 if (adev->rio_mem)
1925 pci_iounmap(adev->pdev, adev->rio_mem);
1926 adev->rio_mem = NULL;
1927 iounmap(adev->rmmio);
1928 adev->rmmio = NULL;
Ken Wangda69c1612016-01-21 19:08:55 +08001929 if (adev->asic_type >= CHIP_BONAIRE)
1930 amdgpu_doorbell_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001931 amdgpu_debugfs_regs_cleanup(adev);
1932 amdgpu_debugfs_remove_files(adev);
1933}
1934
1935
1936/*
1937 * Suspend & resume.
1938 */
1939/**
Alex Deucher810ddc32016-08-23 13:25:49 -04001940 * amdgpu_device_suspend - initiate device suspend
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001941 *
1942 * @pdev: drm dev pointer
1943 * @state: suspend state
1944 *
1945 * Puts the hw in the suspend state (all asics).
1946 * Returns 0 for success or an error on failure.
1947 * Called at driver suspend.
1948 */
Alex Deucher810ddc32016-08-23 13:25:49 -04001949int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001950{
1951 struct amdgpu_device *adev;
1952 struct drm_crtc *crtc;
1953 struct drm_connector *connector;
Alex Deucher5ceb54c2015-08-05 12:41:48 -04001954 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001955
1956 if (dev == NULL || dev->dev_private == NULL) {
1957 return -ENODEV;
1958 }
1959
1960 adev = dev->dev_private;
1961
1962 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
1963 return 0;
1964
1965 drm_kms_helper_poll_disable(dev);
1966
1967 /* turn off display hw */
Alex Deucher4c7fbc32015-09-23 14:32:06 -04001968 drm_modeset_lock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001969 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1970 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
1971 }
Alex Deucher4c7fbc32015-09-23 14:32:06 -04001972 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001973
Alex Deucher756e6882015-10-08 00:03:36 -04001974 /* unpin the front buffers and cursors */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001975 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Alex Deucher756e6882015-10-08 00:03:36 -04001976 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001977 struct amdgpu_framebuffer *rfb = to_amdgpu_framebuffer(crtc->primary->fb);
1978 struct amdgpu_bo *robj;
1979
Alex Deucher756e6882015-10-08 00:03:36 -04001980 if (amdgpu_crtc->cursor_bo) {
1981 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
1982 r = amdgpu_bo_reserve(aobj, false);
1983 if (r == 0) {
1984 amdgpu_bo_unpin(aobj);
1985 amdgpu_bo_unreserve(aobj);
1986 }
1987 }
1988
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001989 if (rfb == NULL || rfb->obj == NULL) {
1990 continue;
1991 }
1992 robj = gem_to_amdgpu_bo(rfb->obj);
1993 /* don't unpin kernel fb objects */
1994 if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
1995 r = amdgpu_bo_reserve(robj, false);
1996 if (r == 0) {
1997 amdgpu_bo_unpin(robj);
1998 amdgpu_bo_unreserve(robj);
1999 }
2000 }
2001 }
2002 /* evict vram memory */
2003 amdgpu_bo_evict_vram(adev);
2004
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002005 amdgpu_fence_driver_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002006
2007 r = amdgpu_suspend(adev);
2008
Alex Deuchera0a71e42016-10-10 12:41:36 -04002009 /* evict remaining vram memory
2010 * This second call to evict vram is to evict the gart page table
2011 * using the CPU.
2012 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002013 amdgpu_bo_evict_vram(adev);
2014
Alex Deuchere695e772016-10-19 14:40:58 -04002015 amdgpu_atombios_scratch_regs_save(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002016 pci_save_state(dev->pdev);
2017 if (suspend) {
2018 /* Shut down the device */
2019 pci_disable_device(dev->pdev);
2020 pci_set_power_state(dev->pdev, PCI_D3hot);
jimqu74b0b152016-09-07 17:09:12 +08002021 } else {
2022 r = amdgpu_asic_reset(adev);
2023 if (r)
2024 DRM_ERROR("amdgpu asic reset failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002025 }
2026
2027 if (fbcon) {
2028 console_lock();
2029 amdgpu_fbdev_set_suspend(adev, 1);
2030 console_unlock();
2031 }
2032 return 0;
2033}
2034
2035/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002036 * amdgpu_device_resume - initiate device resume
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002037 *
2038 * @pdev: drm dev pointer
2039 *
2040 * Bring the hw back to operating state (all asics).
2041 * Returns 0 for success or an error on failure.
2042 * Called at driver resume.
2043 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002044int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002045{
2046 struct drm_connector *connector;
2047 struct amdgpu_device *adev = dev->dev_private;
Alex Deucher756e6882015-10-08 00:03:36 -04002048 struct drm_crtc *crtc;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002049 int r;
2050
2051 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2052 return 0;
2053
jimqu74b0b152016-09-07 17:09:12 +08002054 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002055 console_lock();
jimqu74b0b152016-09-07 17:09:12 +08002056
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002057 if (resume) {
2058 pci_set_power_state(dev->pdev, PCI_D0);
2059 pci_restore_state(dev->pdev);
jimqu74b0b152016-09-07 17:09:12 +08002060 r = pci_enable_device(dev->pdev);
2061 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002062 if (fbcon)
2063 console_unlock();
jimqu74b0b152016-09-07 17:09:12 +08002064 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002065 }
2066 }
Alex Deuchere695e772016-10-19 14:40:58 -04002067 amdgpu_atombios_scratch_regs_restore(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002068
2069 /* post card */
jimqu74b0b152016-09-07 17:09:12 +08002070 if (!amdgpu_card_posted(adev) || !resume) {
2071 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2072 if (r)
2073 DRM_ERROR("amdgpu asic init failed\n");
2074 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002075
2076 r = amdgpu_resume(adev);
Flora Cuica198522016-02-04 15:10:08 +08002077 if (r)
2078 DRM_ERROR("amdgpu_resume failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002079
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002080 amdgpu_fence_driver_resume(adev);
2081
Flora Cuica198522016-02-04 15:10:08 +08002082 if (resume) {
2083 r = amdgpu_ib_ring_tests(adev);
2084 if (r)
2085 DRM_ERROR("ib ring test failed (%d).\n", r);
2086 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002087
2088 r = amdgpu_late_init(adev);
2089 if (r)
2090 return r;
2091
Alex Deucher756e6882015-10-08 00:03:36 -04002092 /* pin cursors */
2093 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2094 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2095
2096 if (amdgpu_crtc->cursor_bo) {
2097 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
2098 r = amdgpu_bo_reserve(aobj, false);
2099 if (r == 0) {
2100 r = amdgpu_bo_pin(aobj,
2101 AMDGPU_GEM_DOMAIN_VRAM,
2102 &amdgpu_crtc->cursor_addr);
2103 if (r != 0)
2104 DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
2105 amdgpu_bo_unreserve(aobj);
2106 }
2107 }
2108 }
2109
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002110 /* blat the mode back in */
2111 if (fbcon) {
2112 drm_helper_resume_force_mode(dev);
2113 /* turn on display hw */
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002114 drm_modeset_lock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002115 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2116 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
2117 }
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002118 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002119 }
2120
2121 drm_kms_helper_poll_enable(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002122
2123 /*
2124 * Most of the connector probing functions try to acquire runtime pm
2125 * refs to ensure that the GPU is powered on when connector polling is
2126 * performed. Since we're calling this from a runtime PM callback,
2127 * trying to acquire rpm refs will cause us to deadlock.
2128 *
2129 * Since we're guaranteed to be holding the rpm lock, it's safe to
2130 * temporarily disable the rpm helpers so this doesn't deadlock us.
2131 */
2132#ifdef CONFIG_PM
2133 dev->dev->power.disable_depth++;
2134#endif
Alex Deucher54fb2a52015-11-24 14:30:56 -05002135 drm_helper_hpd_irq_event(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002136#ifdef CONFIG_PM
2137 dev->dev->power.disable_depth--;
2138#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002139
2140 if (fbcon) {
2141 amdgpu_fbdev_set_suspend(adev, 0);
2142 console_unlock();
2143 }
2144
2145 return 0;
2146}
2147
Chunming Zhou63fbf422016-07-15 11:19:20 +08002148static bool amdgpu_check_soft_reset(struct amdgpu_device *adev)
2149{
2150 int i;
2151 bool asic_hang = false;
2152
2153 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002154 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou63fbf422016-07-15 11:19:20 +08002155 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002156 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
2157 adev->ip_blocks[i].status.hang =
2158 adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
2159 if (adev->ip_blocks[i].status.hang) {
2160 DRM_INFO("IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
Chunming Zhou63fbf422016-07-15 11:19:20 +08002161 asic_hang = true;
2162 }
2163 }
2164 return asic_hang;
2165}
2166
Baoyou Xie4d446652016-09-18 22:09:35 +08002167static int amdgpu_pre_soft_reset(struct amdgpu_device *adev)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002168{
2169 int i, r = 0;
2170
2171 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002172 if (!adev->ip_blocks[i].status.valid)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002173 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002174 if (adev->ip_blocks[i].status.hang &&
2175 adev->ip_blocks[i].version->funcs->pre_soft_reset) {
2176 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
Chunming Zhoud31a5012016-07-18 10:04:34 +08002177 if (r)
2178 return r;
2179 }
2180 }
2181
2182 return 0;
2183}
2184
Chunming Zhou35d782f2016-07-15 15:57:13 +08002185static bool amdgpu_need_full_reset(struct amdgpu_device *adev)
2186{
Alex Deucherda146d32016-10-13 16:07:03 -04002187 int i;
2188
2189 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002190 if (!adev->ip_blocks[i].status.valid)
Alex Deucherda146d32016-10-13 16:07:03 -04002191 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002192 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
2193 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
2194 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
2195 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE)) {
2196 if (adev->ip_blocks[i].status.hang) {
Alex Deucherda146d32016-10-13 16:07:03 -04002197 DRM_INFO("Some block need full reset!\n");
2198 return true;
2199 }
2200 }
Chunming Zhou35d782f2016-07-15 15:57:13 +08002201 }
2202 return false;
2203}
2204
2205static int amdgpu_soft_reset(struct amdgpu_device *adev)
2206{
2207 int i, r = 0;
2208
2209 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002210 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002211 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002212 if (adev->ip_blocks[i].status.hang &&
2213 adev->ip_blocks[i].version->funcs->soft_reset) {
2214 r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002215 if (r)
2216 return r;
2217 }
2218 }
2219
2220 return 0;
2221}
2222
2223static int amdgpu_post_soft_reset(struct amdgpu_device *adev)
2224{
2225 int i, r = 0;
2226
2227 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002228 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002229 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002230 if (adev->ip_blocks[i].status.hang &&
2231 adev->ip_blocks[i].version->funcs->post_soft_reset)
2232 r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002233 if (r)
2234 return r;
2235 }
2236
2237 return 0;
2238}
2239
Chunming Zhou3ad81f12016-08-05 17:30:17 +08002240bool amdgpu_need_backup(struct amdgpu_device *adev)
2241{
2242 if (adev->flags & AMD_IS_APU)
2243 return false;
2244
2245 return amdgpu_lockup_timeout > 0 ? true : false;
2246}
2247
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002248static int amdgpu_recover_vram_from_shadow(struct amdgpu_device *adev,
2249 struct amdgpu_ring *ring,
2250 struct amdgpu_bo *bo,
Chris Wilsonf54d1862016-10-25 13:00:45 +01002251 struct dma_fence **fence)
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002252{
2253 uint32_t domain;
2254 int r;
2255
2256 if (!bo->shadow)
2257 return 0;
2258
2259 r = amdgpu_bo_reserve(bo, false);
2260 if (r)
2261 return r;
2262 domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
2263 /* if bo has been evicted, then no need to recover */
2264 if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
2265 r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
2266 NULL, fence, true);
2267 if (r) {
2268 DRM_ERROR("recover page table failed!\n");
2269 goto err;
2270 }
2271 }
2272err:
2273 amdgpu_bo_unreserve(bo);
2274 return r;
2275}
2276
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002277/**
2278 * amdgpu_gpu_reset - reset the asic
2279 *
2280 * @adev: amdgpu device pointer
2281 *
2282 * Attempt the reset the GPU if it has hung (all asics).
2283 * Returns 0 for success or an error on failure.
2284 */
2285int amdgpu_gpu_reset(struct amdgpu_device *adev)
2286{
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002287 int i, r;
2288 int resched;
Chunming Zhou35d782f2016-07-15 15:57:13 +08002289 bool need_full_reset;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002290
Chunming Zhou63fbf422016-07-15 11:19:20 +08002291 if (!amdgpu_check_soft_reset(adev)) {
2292 DRM_INFO("No hardware hang detected. Did some blocks stall?\n");
2293 return 0;
2294 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002295
Marek Olšákd94aed52015-05-05 21:13:49 +02002296 atomic_inc(&adev->gpu_reset_counter);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002297
Chunming Zhoua3c47d62016-06-30 16:44:41 +08002298 /* block TTM */
2299 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
2300
Chunming Zhou0875dc92016-06-12 15:41:58 +08002301 /* block scheduler */
2302 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2303 struct amdgpu_ring *ring = adev->rings[i];
2304
2305 if (!ring)
2306 continue;
2307 kthread_park(ring->sched.thread);
Chunming Zhouaa1c8902016-06-30 13:56:02 +08002308 amd_sched_hw_job_reset(&ring->sched);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002309 }
Chunming Zhou2200eda2016-06-30 16:53:02 +08002310 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
2311 amdgpu_fence_driver_force_completion(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002312
Chunming Zhou35d782f2016-07-15 15:57:13 +08002313 need_full_reset = amdgpu_need_full_reset(adev);
2314
2315 if (!need_full_reset) {
2316 amdgpu_pre_soft_reset(adev);
2317 r = amdgpu_soft_reset(adev);
2318 amdgpu_post_soft_reset(adev);
2319 if (r || amdgpu_check_soft_reset(adev)) {
2320 DRM_INFO("soft reset failed, will fallback to full reset!\n");
2321 need_full_reset = true;
2322 }
2323 }
2324
2325 if (need_full_reset) {
Chunming Zhou35d782f2016-07-15 15:57:13 +08002326 r = amdgpu_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002327
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002328retry:
Chunming Zhou35d782f2016-07-15 15:57:13 +08002329 /* Disable fb access */
2330 if (adev->mode_info.num_crtc) {
2331 struct amdgpu_mode_mc_save save;
2332 amdgpu_display_stop_mc_access(adev, &save);
2333 amdgpu_wait_for_idle(adev, AMD_IP_BLOCK_TYPE_GMC);
2334 }
Alex Deuchere695e772016-10-19 14:40:58 -04002335 amdgpu_atombios_scratch_regs_save(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002336 r = amdgpu_asic_reset(adev);
Alex Deuchere695e772016-10-19 14:40:58 -04002337 amdgpu_atombios_scratch_regs_restore(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002338 /* post card */
2339 amdgpu_atom_asic_init(adev->mode_info.atom_context);
Alex Deucherbfa99262016-01-15 11:59:48 -05002340
Chunming Zhou35d782f2016-07-15 15:57:13 +08002341 if (!r) {
2342 dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
2343 r = amdgpu_resume(adev);
2344 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002345 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002346 if (!r) {
Chunming Zhoue72cfd52016-07-27 13:15:20 +08002347 amdgpu_irq_gpu_reset_resume_helper(adev);
Chunming Zhou2c0d7312016-08-30 16:36:25 +08002348 if (need_full_reset && amdgpu_need_backup(adev)) {
2349 r = amdgpu_ttm_recover_gart(adev);
2350 if (r)
2351 DRM_ERROR("gart recovery failed!!!\n");
2352 }
Chunming Zhou1f465082016-06-30 15:02:26 +08002353 r = amdgpu_ib_ring_tests(adev);
2354 if (r) {
2355 dev_err(adev->dev, "ib ring test failed (%d).\n", r);
Chunming Zhou40019dc2016-06-29 16:01:49 +08002356 r = amdgpu_suspend(adev);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002357 need_full_reset = true;
Chunming Zhou40019dc2016-06-29 16:01:49 +08002358 goto retry;
Chunming Zhou1f465082016-06-30 15:02:26 +08002359 }
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002360 /**
2361 * recovery vm page tables, since we cannot depend on VRAM is
2362 * consistent after gpu full reset.
2363 */
2364 if (need_full_reset && amdgpu_need_backup(adev)) {
2365 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
2366 struct amdgpu_bo *bo, *tmp;
Chris Wilsonf54d1862016-10-25 13:00:45 +01002367 struct dma_fence *fence = NULL, *next = NULL;
Chunming Zhou1f465082016-06-30 15:02:26 +08002368
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002369 DRM_INFO("recover vram bo from shadow\n");
2370 mutex_lock(&adev->shadow_list_lock);
2371 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
2372 amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
2373 if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01002374 r = dma_fence_wait(fence, false);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002375 if (r) {
2376 WARN(r, "recovery from shadow isn't comleted\n");
2377 break;
2378 }
2379 }
2380
Chris Wilsonf54d1862016-10-25 13:00:45 +01002381 dma_fence_put(fence);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002382 fence = next;
2383 }
2384 mutex_unlock(&adev->shadow_list_lock);
2385 if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01002386 r = dma_fence_wait(fence, false);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002387 if (r)
2388 WARN(r, "recovery from shadow isn't comleted\n");
2389 }
Chris Wilsonf54d1862016-10-25 13:00:45 +01002390 dma_fence_put(fence);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002391 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002392 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2393 struct amdgpu_ring *ring = adev->rings[i];
2394 if (!ring)
2395 continue;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002396
Chunming Zhouaa1c8902016-06-30 13:56:02 +08002397 amd_sched_job_recovery(&ring->sched);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002398 kthread_unpark(ring->sched.thread);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002399 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002400 } else {
Chunming Zhou2200eda2016-06-30 16:53:02 +08002401 dev_err(adev->dev, "asic resume failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002402 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
Chunming Zhou0875dc92016-06-12 15:41:58 +08002403 if (adev->rings[i]) {
2404 kthread_unpark(adev->rings[i]->sched.thread);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002405 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002406 }
2407 }
2408
2409 drm_helper_resume_force_mode(adev->ddev);
2410
2411 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
2412 if (r) {
2413 /* bad news, how to tell it to userspace ? */
2414 dev_info(adev->dev, "GPU reset failed\n");
2415 }
2416
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002417 return r;
2418}
2419
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002420void amdgpu_get_pcie_info(struct amdgpu_device *adev)
2421{
2422 u32 mask;
2423 int ret;
2424
Alex Deuchercd474ba2016-02-04 10:21:23 -05002425 if (amdgpu_pcie_gen_cap)
2426 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
2427
2428 if (amdgpu_pcie_lane_cap)
2429 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
2430
2431 /* covers APUs as well */
2432 if (pci_is_root_bus(adev->pdev->bus)) {
2433 if (adev->pm.pcie_gen_mask == 0)
2434 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
2435 if (adev->pm.pcie_mlw_mask == 0)
2436 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002437 return;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002438 }
Alex Deuchercd474ba2016-02-04 10:21:23 -05002439
2440 if (adev->pm.pcie_gen_mask == 0) {
2441 ret = drm_pcie_get_speed_cap_mask(adev->ddev, &mask);
2442 if (!ret) {
2443 adev->pm.pcie_gen_mask = (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
2444 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
2445 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
2446
2447 if (mask & DRM_PCIE_SPEED_25)
2448 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
2449 if (mask & DRM_PCIE_SPEED_50)
2450 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2;
2451 if (mask & DRM_PCIE_SPEED_80)
2452 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3;
2453 } else {
2454 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
2455 }
2456 }
2457 if (adev->pm.pcie_mlw_mask == 0) {
2458 ret = drm_pcie_get_max_link_width(adev->ddev, &mask);
2459 if (!ret) {
2460 switch (mask) {
2461 case 32:
2462 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
2463 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
2464 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2465 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2466 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2467 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2468 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2469 break;
2470 case 16:
2471 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
2472 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2473 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2474 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2475 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2476 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2477 break;
2478 case 12:
2479 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2480 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2481 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2482 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2483 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2484 break;
2485 case 8:
2486 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2487 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2488 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2489 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2490 break;
2491 case 4:
2492 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2493 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2494 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2495 break;
2496 case 2:
2497 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2498 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2499 break;
2500 case 1:
2501 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
2502 break;
2503 default:
2504 break;
2505 }
2506 } else {
2507 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002508 }
2509 }
2510}
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002511
2512/*
2513 * Debugfs
2514 */
2515int amdgpu_debugfs_add_files(struct amdgpu_device *adev,
Nils Wallménius06ab6832016-05-02 12:46:15 -04002516 const struct drm_info_list *files,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002517 unsigned nfiles)
2518{
2519 unsigned i;
2520
2521 for (i = 0; i < adev->debugfs_count; i++) {
2522 if (adev->debugfs[i].files == files) {
2523 /* Already registered */
2524 return 0;
2525 }
2526 }
2527
2528 i = adev->debugfs_count + 1;
2529 if (i > AMDGPU_DEBUGFS_MAX_COMPONENTS) {
2530 DRM_ERROR("Reached maximum number of debugfs components.\n");
2531 DRM_ERROR("Report so we increase "
2532 "AMDGPU_DEBUGFS_MAX_COMPONENTS.\n");
2533 return -EINVAL;
2534 }
2535 adev->debugfs[adev->debugfs_count].files = files;
2536 adev->debugfs[adev->debugfs_count].num_files = nfiles;
2537 adev->debugfs_count = i;
2538#if defined(CONFIG_DEBUG_FS)
2539 drm_debugfs_create_files(files, nfiles,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002540 adev->ddev->primary->debugfs_root,
2541 adev->ddev->primary);
2542#endif
2543 return 0;
2544}
2545
2546static void amdgpu_debugfs_remove_files(struct amdgpu_device *adev)
2547{
2548#if defined(CONFIG_DEBUG_FS)
2549 unsigned i;
2550
2551 for (i = 0; i < adev->debugfs_count; i++) {
2552 drm_debugfs_remove_files(adev->debugfs[i].files,
2553 adev->debugfs[i].num_files,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002554 adev->ddev->primary);
2555 }
2556#endif
2557}
2558
2559#if defined(CONFIG_DEBUG_FS)
2560
2561static ssize_t amdgpu_debugfs_regs_read(struct file *f, char __user *buf,
2562 size_t size, loff_t *pos)
2563{
Al Viro45063092016-12-04 18:24:56 -05002564 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002565 ssize_t result = 0;
2566 int r;
Tom St Denisbd122672016-07-28 09:39:22 -04002567 bool pm_pg_lock, use_bank;
Tom St Denis566281592016-06-27 11:55:07 -04002568 unsigned instance_bank, sh_bank, se_bank;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002569
2570 if (size & 0x3 || *pos & 0x3)
2571 return -EINVAL;
2572
Tom St Denisbd122672016-07-28 09:39:22 -04002573 /* are we reading registers for which a PG lock is necessary? */
2574 pm_pg_lock = (*pos >> 23) & 1;
2575
Tom St Denis566281592016-06-27 11:55:07 -04002576 if (*pos & (1ULL << 62)) {
2577 se_bank = (*pos >> 24) & 0x3FF;
2578 sh_bank = (*pos >> 34) & 0x3FF;
2579 instance_bank = (*pos >> 44) & 0x3FF;
Tom St Denis32977f92016-10-09 07:41:26 -04002580
2581 if (se_bank == 0x3FF)
2582 se_bank = 0xFFFFFFFF;
2583 if (sh_bank == 0x3FF)
2584 sh_bank = 0xFFFFFFFF;
2585 if (instance_bank == 0x3FF)
2586 instance_bank = 0xFFFFFFFF;
Tom St Denis566281592016-06-27 11:55:07 -04002587 use_bank = 1;
Tom St Denis566281592016-06-27 11:55:07 -04002588 } else {
2589 use_bank = 0;
2590 }
2591
Tom St Denisbd122672016-07-28 09:39:22 -04002592 *pos &= 0x3FFFF;
2593
Tom St Denis566281592016-06-27 11:55:07 -04002594 if (use_bank) {
Tom St Denis32977f92016-10-09 07:41:26 -04002595 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
2596 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
Tom St Denis566281592016-06-27 11:55:07 -04002597 return -EINVAL;
2598 mutex_lock(&adev->grbm_idx_mutex);
2599 amdgpu_gfx_select_se_sh(adev, se_bank,
2600 sh_bank, instance_bank);
2601 }
2602
Tom St Denisbd122672016-07-28 09:39:22 -04002603 if (pm_pg_lock)
2604 mutex_lock(&adev->pm.mutex);
2605
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002606 while (size) {
2607 uint32_t value;
2608
2609 if (*pos > adev->rmmio_size)
Tom St Denis566281592016-06-27 11:55:07 -04002610 goto end;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002611
2612 value = RREG32(*pos >> 2);
2613 r = put_user(value, (uint32_t *)buf);
Tom St Denis566281592016-06-27 11:55:07 -04002614 if (r) {
2615 result = r;
2616 goto end;
2617 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002618
2619 result += 4;
2620 buf += 4;
2621 *pos += 4;
2622 size -= 4;
2623 }
2624
Tom St Denis566281592016-06-27 11:55:07 -04002625end:
2626 if (use_bank) {
2627 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
2628 mutex_unlock(&adev->grbm_idx_mutex);
2629 }
2630
Tom St Denisbd122672016-07-28 09:39:22 -04002631 if (pm_pg_lock)
2632 mutex_unlock(&adev->pm.mutex);
2633
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002634 return result;
2635}
2636
2637static ssize_t amdgpu_debugfs_regs_write(struct file *f, const char __user *buf,
2638 size_t size, loff_t *pos)
2639{
Al Viro45063092016-12-04 18:24:56 -05002640 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002641 ssize_t result = 0;
2642 int r;
Tom St Denis394fdde2016-10-10 07:31:23 -04002643 bool pm_pg_lock, use_bank;
2644 unsigned instance_bank, sh_bank, se_bank;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002645
2646 if (size & 0x3 || *pos & 0x3)
2647 return -EINVAL;
2648
Tom St Denis394fdde2016-10-10 07:31:23 -04002649 /* are we reading registers for which a PG lock is necessary? */
2650 pm_pg_lock = (*pos >> 23) & 1;
2651
2652 if (*pos & (1ULL << 62)) {
2653 se_bank = (*pos >> 24) & 0x3FF;
2654 sh_bank = (*pos >> 34) & 0x3FF;
2655 instance_bank = (*pos >> 44) & 0x3FF;
2656
2657 if (se_bank == 0x3FF)
2658 se_bank = 0xFFFFFFFF;
2659 if (sh_bank == 0x3FF)
2660 sh_bank = 0xFFFFFFFF;
2661 if (instance_bank == 0x3FF)
2662 instance_bank = 0xFFFFFFFF;
2663 use_bank = 1;
2664 } else {
2665 use_bank = 0;
2666 }
2667
2668 *pos &= 0x3FFFF;
2669
2670 if (use_bank) {
2671 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
2672 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
2673 return -EINVAL;
2674 mutex_lock(&adev->grbm_idx_mutex);
2675 amdgpu_gfx_select_se_sh(adev, se_bank,
2676 sh_bank, instance_bank);
2677 }
2678
2679 if (pm_pg_lock)
2680 mutex_lock(&adev->pm.mutex);
2681
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002682 while (size) {
2683 uint32_t value;
2684
2685 if (*pos > adev->rmmio_size)
2686 return result;
2687
2688 r = get_user(value, (uint32_t *)buf);
2689 if (r)
2690 return r;
2691
2692 WREG32(*pos >> 2, value);
2693
2694 result += 4;
2695 buf += 4;
2696 *pos += 4;
2697 size -= 4;
2698 }
2699
Tom St Denis394fdde2016-10-10 07:31:23 -04002700 if (use_bank) {
2701 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
2702 mutex_unlock(&adev->grbm_idx_mutex);
2703 }
2704
2705 if (pm_pg_lock)
2706 mutex_unlock(&adev->pm.mutex);
2707
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002708 return result;
2709}
2710
Tom St Denisadcec282016-04-15 13:08:44 -04002711static ssize_t amdgpu_debugfs_regs_pcie_read(struct file *f, char __user *buf,
2712 size_t size, loff_t *pos)
2713{
Al Viro45063092016-12-04 18:24:56 -05002714 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04002715 ssize_t result = 0;
2716 int r;
2717
2718 if (size & 0x3 || *pos & 0x3)
2719 return -EINVAL;
2720
2721 while (size) {
2722 uint32_t value;
2723
2724 value = RREG32_PCIE(*pos >> 2);
2725 r = put_user(value, (uint32_t *)buf);
2726 if (r)
2727 return r;
2728
2729 result += 4;
2730 buf += 4;
2731 *pos += 4;
2732 size -= 4;
2733 }
2734
2735 return result;
2736}
2737
2738static ssize_t amdgpu_debugfs_regs_pcie_write(struct file *f, const char __user *buf,
2739 size_t size, loff_t *pos)
2740{
Al Viro45063092016-12-04 18:24:56 -05002741 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04002742 ssize_t result = 0;
2743 int r;
2744
2745 if (size & 0x3 || *pos & 0x3)
2746 return -EINVAL;
2747
2748 while (size) {
2749 uint32_t value;
2750
2751 r = get_user(value, (uint32_t *)buf);
2752 if (r)
2753 return r;
2754
2755 WREG32_PCIE(*pos >> 2, value);
2756
2757 result += 4;
2758 buf += 4;
2759 *pos += 4;
2760 size -= 4;
2761 }
2762
2763 return result;
2764}
2765
2766static ssize_t amdgpu_debugfs_regs_didt_read(struct file *f, char __user *buf,
2767 size_t size, loff_t *pos)
2768{
Al Viro45063092016-12-04 18:24:56 -05002769 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04002770 ssize_t result = 0;
2771 int r;
2772
2773 if (size & 0x3 || *pos & 0x3)
2774 return -EINVAL;
2775
2776 while (size) {
2777 uint32_t value;
2778
2779 value = RREG32_DIDT(*pos >> 2);
2780 r = put_user(value, (uint32_t *)buf);
2781 if (r)
2782 return r;
2783
2784 result += 4;
2785 buf += 4;
2786 *pos += 4;
2787 size -= 4;
2788 }
2789
2790 return result;
2791}
2792
2793static ssize_t amdgpu_debugfs_regs_didt_write(struct file *f, const char __user *buf,
2794 size_t size, loff_t *pos)
2795{
Al Viro45063092016-12-04 18:24:56 -05002796 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04002797 ssize_t result = 0;
2798 int r;
2799
2800 if (size & 0x3 || *pos & 0x3)
2801 return -EINVAL;
2802
2803 while (size) {
2804 uint32_t value;
2805
2806 r = get_user(value, (uint32_t *)buf);
2807 if (r)
2808 return r;
2809
2810 WREG32_DIDT(*pos >> 2, value);
2811
2812 result += 4;
2813 buf += 4;
2814 *pos += 4;
2815 size -= 4;
2816 }
2817
2818 return result;
2819}
2820
2821static ssize_t amdgpu_debugfs_regs_smc_read(struct file *f, char __user *buf,
2822 size_t size, loff_t *pos)
2823{
Al Viro45063092016-12-04 18:24:56 -05002824 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04002825 ssize_t result = 0;
2826 int r;
2827
2828 if (size & 0x3 || *pos & 0x3)
2829 return -EINVAL;
2830
2831 while (size) {
2832 uint32_t value;
2833
Tom St Denis6fc0dea2016-08-29 08:39:29 -04002834 value = RREG32_SMC(*pos);
Tom St Denisadcec282016-04-15 13:08:44 -04002835 r = put_user(value, (uint32_t *)buf);
2836 if (r)
2837 return r;
2838
2839 result += 4;
2840 buf += 4;
2841 *pos += 4;
2842 size -= 4;
2843 }
2844
2845 return result;
2846}
2847
2848static ssize_t amdgpu_debugfs_regs_smc_write(struct file *f, const char __user *buf,
2849 size_t size, loff_t *pos)
2850{
Al Viro45063092016-12-04 18:24:56 -05002851 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04002852 ssize_t result = 0;
2853 int r;
2854
2855 if (size & 0x3 || *pos & 0x3)
2856 return -EINVAL;
2857
2858 while (size) {
2859 uint32_t value;
2860
2861 r = get_user(value, (uint32_t *)buf);
2862 if (r)
2863 return r;
2864
Tom St Denis6fc0dea2016-08-29 08:39:29 -04002865 WREG32_SMC(*pos, value);
Tom St Denisadcec282016-04-15 13:08:44 -04002866
2867 result += 4;
2868 buf += 4;
2869 *pos += 4;
2870 size -= 4;
2871 }
2872
2873 return result;
2874}
2875
Tom St Denis1e051412016-06-27 09:57:18 -04002876static ssize_t amdgpu_debugfs_gca_config_read(struct file *f, char __user *buf,
2877 size_t size, loff_t *pos)
2878{
Al Viro45063092016-12-04 18:24:56 -05002879 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denis1e051412016-06-27 09:57:18 -04002880 ssize_t result = 0;
2881 int r;
2882 uint32_t *config, no_regs = 0;
2883
2884 if (size & 0x3 || *pos & 0x3)
2885 return -EINVAL;
2886
Markus Elfringecab7662016-09-18 17:00:52 +02002887 config = kmalloc_array(256, sizeof(*config), GFP_KERNEL);
Tom St Denis1e051412016-06-27 09:57:18 -04002888 if (!config)
2889 return -ENOMEM;
2890
2891 /* version, increment each time something is added */
Tom St Denise9f11dc2016-08-17 12:00:51 -04002892 config[no_regs++] = 2;
Tom St Denis1e051412016-06-27 09:57:18 -04002893 config[no_regs++] = adev->gfx.config.max_shader_engines;
2894 config[no_regs++] = adev->gfx.config.max_tile_pipes;
2895 config[no_regs++] = adev->gfx.config.max_cu_per_sh;
2896 config[no_regs++] = adev->gfx.config.max_sh_per_se;
2897 config[no_regs++] = adev->gfx.config.max_backends_per_se;
2898 config[no_regs++] = adev->gfx.config.max_texture_channel_caches;
2899 config[no_regs++] = adev->gfx.config.max_gprs;
2900 config[no_regs++] = adev->gfx.config.max_gs_threads;
2901 config[no_regs++] = adev->gfx.config.max_hw_contexts;
2902 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_frontend;
2903 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_backend;
2904 config[no_regs++] = adev->gfx.config.sc_hiz_tile_fifo_size;
2905 config[no_regs++] = adev->gfx.config.sc_earlyz_tile_fifo_size;
2906 config[no_regs++] = adev->gfx.config.num_tile_pipes;
2907 config[no_regs++] = adev->gfx.config.backend_enable_mask;
2908 config[no_regs++] = adev->gfx.config.mem_max_burst_length_bytes;
2909 config[no_regs++] = adev->gfx.config.mem_row_size_in_kb;
2910 config[no_regs++] = adev->gfx.config.shader_engine_tile_size;
2911 config[no_regs++] = adev->gfx.config.num_gpus;
2912 config[no_regs++] = adev->gfx.config.multi_gpu_tile_size;
2913 config[no_regs++] = adev->gfx.config.mc_arb_ramcfg;
2914 config[no_regs++] = adev->gfx.config.gb_addr_config;
2915 config[no_regs++] = adev->gfx.config.num_rbs;
2916
Tom St Denis89a8f302016-08-12 15:14:31 -04002917 /* rev==1 */
2918 config[no_regs++] = adev->rev_id;
2919 config[no_regs++] = adev->pg_flags;
2920 config[no_regs++] = adev->cg_flags;
2921
Tom St Denise9f11dc2016-08-17 12:00:51 -04002922 /* rev==2 */
2923 config[no_regs++] = adev->family;
2924 config[no_regs++] = adev->external_rev_id;
2925
Tom St Denis1e051412016-06-27 09:57:18 -04002926 while (size && (*pos < no_regs * 4)) {
2927 uint32_t value;
2928
2929 value = config[*pos >> 2];
2930 r = put_user(value, (uint32_t *)buf);
2931 if (r) {
2932 kfree(config);
2933 return r;
2934 }
2935
2936 result += 4;
2937 buf += 4;
2938 *pos += 4;
2939 size -= 4;
2940 }
2941
2942 kfree(config);
2943 return result;
2944}
2945
Tom St Denisf2cdaf22016-09-15 10:08:44 -04002946static ssize_t amdgpu_debugfs_sensor_read(struct file *f, char __user *buf,
2947 size_t size, loff_t *pos)
2948{
Al Viro45063092016-12-04 18:24:56 -05002949 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisf2cdaf22016-09-15 10:08:44 -04002950 int idx, r;
2951 int32_t value;
2952
2953 if (size != 4 || *pos & 0x3)
2954 return -EINVAL;
2955
2956 /* convert offset to sensor number */
2957 idx = *pos >> 2;
2958
2959 if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->read_sensor)
2960 r = adev->powerplay.pp_funcs->read_sensor(adev->powerplay.pp_handle, idx, &value);
2961 else
2962 return -EINVAL;
2963
2964 if (!r)
2965 r = put_user(value, (int32_t *)buf);
2966
2967 return !r ? 4 : r;
2968}
Tom St Denis1e051412016-06-27 09:57:18 -04002969
Tom St Denis273d7aa2016-10-11 14:48:55 -04002970static ssize_t amdgpu_debugfs_wave_read(struct file *f, char __user *buf,
2971 size_t size, loff_t *pos)
2972{
2973 struct amdgpu_device *adev = f->f_inode->i_private;
2974 int r, x;
2975 ssize_t result=0;
Tom St Denis472259f2016-10-14 09:49:09 -04002976 uint32_t offset, se, sh, cu, wave, simd, data[32];
Tom St Denis273d7aa2016-10-11 14:48:55 -04002977
2978 if (size & 3 || *pos & 3)
2979 return -EINVAL;
2980
2981 /* decode offset */
2982 offset = (*pos & 0x7F);
2983 se = ((*pos >> 7) & 0xFF);
2984 sh = ((*pos >> 15) & 0xFF);
2985 cu = ((*pos >> 23) & 0xFF);
2986 wave = ((*pos >> 31) & 0xFF);
2987 simd = ((*pos >> 37) & 0xFF);
Tom St Denis273d7aa2016-10-11 14:48:55 -04002988
2989 /* switch to the specific se/sh/cu */
2990 mutex_lock(&adev->grbm_idx_mutex);
2991 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
2992
2993 x = 0;
Tom St Denis472259f2016-10-14 09:49:09 -04002994 if (adev->gfx.funcs->read_wave_data)
2995 adev->gfx.funcs->read_wave_data(adev, simd, wave, data, &x);
Tom St Denis273d7aa2016-10-11 14:48:55 -04002996
2997 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
2998 mutex_unlock(&adev->grbm_idx_mutex);
2999
Tom St Denis5ecfb3b2016-10-13 12:15:03 -04003000 if (!x)
3001 return -EINVAL;
3002
Tom St Denis472259f2016-10-14 09:49:09 -04003003 while (size && (offset < x * 4)) {
Tom St Denis273d7aa2016-10-11 14:48:55 -04003004 uint32_t value;
3005
Tom St Denis472259f2016-10-14 09:49:09 -04003006 value = data[offset >> 2];
Tom St Denis273d7aa2016-10-11 14:48:55 -04003007 r = put_user(value, (uint32_t *)buf);
3008 if (r)
3009 return r;
3010
3011 result += 4;
3012 buf += 4;
Tom St Denis472259f2016-10-14 09:49:09 -04003013 offset += 4;
Tom St Denis273d7aa2016-10-11 14:48:55 -04003014 size -= 4;
3015 }
3016
3017 return result;
3018}
3019
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003020static ssize_t amdgpu_debugfs_gpr_read(struct file *f, char __user *buf,
3021 size_t size, loff_t *pos)
3022{
3023 struct amdgpu_device *adev = f->f_inode->i_private;
3024 int r;
3025 ssize_t result = 0;
3026 uint32_t offset, se, sh, cu, wave, simd, thread, bank, *data;
3027
3028 if (size & 3 || *pos & 3)
3029 return -EINVAL;
3030
3031 /* decode offset */
3032 offset = (*pos & 0xFFF); /* in dwords */
3033 se = ((*pos >> 12) & 0xFF);
3034 sh = ((*pos >> 20) & 0xFF);
3035 cu = ((*pos >> 28) & 0xFF);
3036 wave = ((*pos >> 36) & 0xFF);
3037 simd = ((*pos >> 44) & 0xFF);
3038 thread = ((*pos >> 52) & 0xFF);
3039 bank = ((*pos >> 60) & 1);
3040
3041 data = kmalloc_array(1024, sizeof(*data), GFP_KERNEL);
3042 if (!data)
3043 return -ENOMEM;
3044
3045 /* switch to the specific se/sh/cu */
3046 mutex_lock(&adev->grbm_idx_mutex);
3047 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
3048
3049 if (bank == 0) {
3050 if (adev->gfx.funcs->read_wave_vgprs)
3051 adev->gfx.funcs->read_wave_vgprs(adev, simd, wave, thread, offset, size>>2, data);
3052 } else {
3053 if (adev->gfx.funcs->read_wave_sgprs)
3054 adev->gfx.funcs->read_wave_sgprs(adev, simd, wave, offset, size>>2, data);
3055 }
3056
3057 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
3058 mutex_unlock(&adev->grbm_idx_mutex);
3059
3060 while (size) {
3061 uint32_t value;
3062
3063 value = data[offset++];
3064 r = put_user(value, (uint32_t *)buf);
3065 if (r) {
3066 result = r;
3067 goto err;
3068 }
3069
3070 result += 4;
3071 buf += 4;
3072 size -= 4;
3073 }
3074
3075err:
3076 kfree(data);
3077 return result;
3078}
3079
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003080static const struct file_operations amdgpu_debugfs_regs_fops = {
3081 .owner = THIS_MODULE,
3082 .read = amdgpu_debugfs_regs_read,
3083 .write = amdgpu_debugfs_regs_write,
3084 .llseek = default_llseek
3085};
Tom St Denisadcec282016-04-15 13:08:44 -04003086static const struct file_operations amdgpu_debugfs_regs_didt_fops = {
3087 .owner = THIS_MODULE,
3088 .read = amdgpu_debugfs_regs_didt_read,
3089 .write = amdgpu_debugfs_regs_didt_write,
3090 .llseek = default_llseek
3091};
3092static const struct file_operations amdgpu_debugfs_regs_pcie_fops = {
3093 .owner = THIS_MODULE,
3094 .read = amdgpu_debugfs_regs_pcie_read,
3095 .write = amdgpu_debugfs_regs_pcie_write,
3096 .llseek = default_llseek
3097};
3098static const struct file_operations amdgpu_debugfs_regs_smc_fops = {
3099 .owner = THIS_MODULE,
3100 .read = amdgpu_debugfs_regs_smc_read,
3101 .write = amdgpu_debugfs_regs_smc_write,
3102 .llseek = default_llseek
3103};
3104
Tom St Denis1e051412016-06-27 09:57:18 -04003105static const struct file_operations amdgpu_debugfs_gca_config_fops = {
3106 .owner = THIS_MODULE,
3107 .read = amdgpu_debugfs_gca_config_read,
3108 .llseek = default_llseek
3109};
3110
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003111static const struct file_operations amdgpu_debugfs_sensors_fops = {
3112 .owner = THIS_MODULE,
3113 .read = amdgpu_debugfs_sensor_read,
3114 .llseek = default_llseek
3115};
3116
Tom St Denis273d7aa2016-10-11 14:48:55 -04003117static const struct file_operations amdgpu_debugfs_wave_fops = {
3118 .owner = THIS_MODULE,
3119 .read = amdgpu_debugfs_wave_read,
3120 .llseek = default_llseek
3121};
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003122static const struct file_operations amdgpu_debugfs_gpr_fops = {
3123 .owner = THIS_MODULE,
3124 .read = amdgpu_debugfs_gpr_read,
3125 .llseek = default_llseek
3126};
Tom St Denis273d7aa2016-10-11 14:48:55 -04003127
Tom St Denisadcec282016-04-15 13:08:44 -04003128static const struct file_operations *debugfs_regs[] = {
3129 &amdgpu_debugfs_regs_fops,
3130 &amdgpu_debugfs_regs_didt_fops,
3131 &amdgpu_debugfs_regs_pcie_fops,
3132 &amdgpu_debugfs_regs_smc_fops,
Tom St Denis1e051412016-06-27 09:57:18 -04003133 &amdgpu_debugfs_gca_config_fops,
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003134 &amdgpu_debugfs_sensors_fops,
Tom St Denis273d7aa2016-10-11 14:48:55 -04003135 &amdgpu_debugfs_wave_fops,
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003136 &amdgpu_debugfs_gpr_fops,
Tom St Denisadcec282016-04-15 13:08:44 -04003137};
3138
3139static const char *debugfs_regs_names[] = {
3140 "amdgpu_regs",
3141 "amdgpu_regs_didt",
3142 "amdgpu_regs_pcie",
3143 "amdgpu_regs_smc",
Tom St Denis1e051412016-06-27 09:57:18 -04003144 "amdgpu_gca_config",
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003145 "amdgpu_sensors",
Tom St Denis273d7aa2016-10-11 14:48:55 -04003146 "amdgpu_wave",
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003147 "amdgpu_gpr",
Tom St Denisadcec282016-04-15 13:08:44 -04003148};
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003149
3150static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
3151{
3152 struct drm_minor *minor = adev->ddev->primary;
3153 struct dentry *ent, *root = minor->debugfs_root;
Tom St Denisadcec282016-04-15 13:08:44 -04003154 unsigned i, j;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003155
Tom St Denisadcec282016-04-15 13:08:44 -04003156 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3157 ent = debugfs_create_file(debugfs_regs_names[i],
3158 S_IFREG | S_IRUGO, root,
3159 adev, debugfs_regs[i]);
3160 if (IS_ERR(ent)) {
3161 for (j = 0; j < i; j++) {
3162 debugfs_remove(adev->debugfs_regs[i]);
3163 adev->debugfs_regs[i] = NULL;
3164 }
3165 return PTR_ERR(ent);
3166 }
3167
3168 if (!i)
3169 i_size_write(ent->d_inode, adev->rmmio_size);
3170 adev->debugfs_regs[i] = ent;
3171 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003172
3173 return 0;
3174}
3175
3176static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev)
3177{
Tom St Denisadcec282016-04-15 13:08:44 -04003178 unsigned i;
3179
3180 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3181 if (adev->debugfs_regs[i]) {
3182 debugfs_remove(adev->debugfs_regs[i]);
3183 adev->debugfs_regs[i] = NULL;
3184 }
3185 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003186}
3187
3188int amdgpu_debugfs_init(struct drm_minor *minor)
3189{
3190 return 0;
3191}
3192
3193void amdgpu_debugfs_cleanup(struct drm_minor *minor)
3194{
3195}
Alexander Kuleshov7cebc722015-06-27 13:16:05 +06003196#else
3197static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
3198{
3199 return 0;
3200}
3201static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev) { }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003202#endif