blob: 29965498c91f353f8e620e8d71e172b13f3c6e82 [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/**
Jim Quc836fec2017-02-10 15:59:59 +0800622 * amdgpu_need_post - check if the hw need post or not
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400623 *
624 * @adev: amdgpu_device pointer
625 *
Jim Quc836fec2017-02-10 15:59:59 +0800626 * Check if the asic has been initialized (all asics) at driver startup
627 * or post is needed if hw reset is performed.
628 * Returns true if need or false if not.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400629 */
Jim Quc836fec2017-02-10 15:59:59 +0800630bool amdgpu_need_post(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400631{
632 uint32_t reg;
633
Jim Quc836fec2017-02-10 15:59:59 +0800634 if (adev->has_hw_reset) {
635 adev->has_hw_reset = false;
636 return true;
637 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400638 /* then check MEM_SIZE, in case the crtcs are off */
639 reg = RREG32(mmCONFIG_MEMSIZE);
640
641 if (reg)
Jim Quc836fec2017-02-10 15:59:59 +0800642 return false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400643
Jim Quc836fec2017-02-10 15:59:59 +0800644 return true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400645
646}
647
Monk Liubec86372016-09-14 19:38:08 +0800648static bool amdgpu_vpost_needed(struct amdgpu_device *adev)
649{
650 if (amdgpu_sriov_vf(adev))
651 return false;
652
653 if (amdgpu_passthrough(adev)) {
Monk Liu1da2c322016-11-11 11:24:29 +0800654 /* for FIJI: In whole GPU pass-through virtualization case, after VM reboot
655 * some old smc fw still need driver do vPost otherwise gpu hang, while
656 * those smc fw version above 22.15 doesn't have this flaw, so we force
657 * vpost executed for smc version below 22.15
Monk Liubec86372016-09-14 19:38:08 +0800658 */
659 if (adev->asic_type == CHIP_FIJI) {
660 int err;
661 uint32_t fw_ver;
662 err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
663 /* force vPost if error occured */
664 if (err)
665 return true;
666
667 fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
Monk Liu1da2c322016-11-11 11:24:29 +0800668 if (fw_ver < 0x00160e00)
669 return true;
Monk Liubec86372016-09-14 19:38:08 +0800670 }
Monk Liubec86372016-09-14 19:38:08 +0800671 }
Jim Quc836fec2017-02-10 15:59:59 +0800672 return amdgpu_need_post(adev);
Monk Liubec86372016-09-14 19:38:08 +0800673}
674
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400675/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400676 * amdgpu_dummy_page_init - init dummy page used by the driver
677 *
678 * @adev: amdgpu_device pointer
679 *
680 * Allocate the dummy page used by the driver (all asics).
681 * This dummy page is used by the driver as a filler for gart entries
682 * when pages are taken out of the GART
683 * Returns 0 on sucess, -ENOMEM on failure.
684 */
685int amdgpu_dummy_page_init(struct amdgpu_device *adev)
686{
687 if (adev->dummy_page.page)
688 return 0;
689 adev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
690 if (adev->dummy_page.page == NULL)
691 return -ENOMEM;
692 adev->dummy_page.addr = pci_map_page(adev->pdev, adev->dummy_page.page,
693 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
694 if (pci_dma_mapping_error(adev->pdev, adev->dummy_page.addr)) {
695 dev_err(&adev->pdev->dev, "Failed to DMA MAP the dummy page\n");
696 __free_page(adev->dummy_page.page);
697 adev->dummy_page.page = NULL;
698 return -ENOMEM;
699 }
700 return 0;
701}
702
703/**
704 * amdgpu_dummy_page_fini - free dummy page used by the driver
705 *
706 * @adev: amdgpu_device pointer
707 *
708 * Frees the dummy page used by the driver (all asics).
709 */
710void amdgpu_dummy_page_fini(struct amdgpu_device *adev)
711{
712 if (adev->dummy_page.page == NULL)
713 return;
714 pci_unmap_page(adev->pdev, adev->dummy_page.addr,
715 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
716 __free_page(adev->dummy_page.page);
717 adev->dummy_page.page = NULL;
718}
719
720
721/* ATOM accessor methods */
722/*
723 * ATOM is an interpreted byte code stored in tables in the vbios. The
724 * driver registers callbacks to access registers and the interpreter
725 * in the driver parses the tables and executes then to program specific
726 * actions (set display modes, asic init, etc.). See amdgpu_atombios.c,
727 * atombios.h, and atom.c
728 */
729
730/**
731 * cail_pll_read - read PLL register
732 *
733 * @info: atom card_info pointer
734 * @reg: PLL register offset
735 *
736 * Provides a PLL register accessor for the atom interpreter (r4xx+).
737 * Returns the value of the PLL register.
738 */
739static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
740{
741 return 0;
742}
743
744/**
745 * cail_pll_write - write PLL register
746 *
747 * @info: atom card_info pointer
748 * @reg: PLL register offset
749 * @val: value to write to the pll register
750 *
751 * Provides a PLL register accessor for the atom interpreter (r4xx+).
752 */
753static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
754{
755
756}
757
758/**
759 * cail_mc_read - read MC (Memory Controller) register
760 *
761 * @info: atom card_info pointer
762 * @reg: MC register offset
763 *
764 * Provides an MC register accessor for the atom interpreter (r4xx+).
765 * Returns the value of the MC register.
766 */
767static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
768{
769 return 0;
770}
771
772/**
773 * cail_mc_write - write MC (Memory Controller) register
774 *
775 * @info: atom card_info pointer
776 * @reg: MC register offset
777 * @val: value to write to the pll register
778 *
779 * Provides a MC register accessor for the atom interpreter (r4xx+).
780 */
781static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
782{
783
784}
785
786/**
787 * cail_reg_write - write MMIO register
788 *
789 * @info: atom card_info pointer
790 * @reg: MMIO register offset
791 * @val: value to write to the pll register
792 *
793 * Provides a MMIO register accessor for the atom interpreter (r4xx+).
794 */
795static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
796{
797 struct amdgpu_device *adev = info->dev->dev_private;
798
799 WREG32(reg, val);
800}
801
802/**
803 * cail_reg_read - read MMIO register
804 *
805 * @info: atom card_info pointer
806 * @reg: MMIO register offset
807 *
808 * Provides an MMIO register accessor for the atom interpreter (r4xx+).
809 * Returns the value of the MMIO register.
810 */
811static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
812{
813 struct amdgpu_device *adev = info->dev->dev_private;
814 uint32_t r;
815
816 r = RREG32(reg);
817 return r;
818}
819
820/**
821 * cail_ioreg_write - write IO register
822 *
823 * @info: atom card_info pointer
824 * @reg: IO register offset
825 * @val: value to write to the pll register
826 *
827 * Provides a IO register accessor for the atom interpreter (r4xx+).
828 */
829static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
830{
831 struct amdgpu_device *adev = info->dev->dev_private;
832
833 WREG32_IO(reg, val);
834}
835
836/**
837 * cail_ioreg_read - read IO register
838 *
839 * @info: atom card_info pointer
840 * @reg: IO register offset
841 *
842 * Provides an IO register accessor for the atom interpreter (r4xx+).
843 * Returns the value of the IO register.
844 */
845static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
846{
847 struct amdgpu_device *adev = info->dev->dev_private;
848 uint32_t r;
849
850 r = RREG32_IO(reg);
851 return r;
852}
853
854/**
855 * amdgpu_atombios_fini - free the driver info and callbacks for atombios
856 *
857 * @adev: amdgpu_device pointer
858 *
859 * Frees the driver info and register access callbacks for the ATOM
860 * interpreter (r4xx+).
861 * Called at driver shutdown.
862 */
863static void amdgpu_atombios_fini(struct amdgpu_device *adev)
864{
Monk Liu89e0ec9f2016-05-27 19:34:11 +0800865 if (adev->mode_info.atom_context) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400866 kfree(adev->mode_info.atom_context->scratch);
Monk Liu89e0ec9f2016-05-27 19:34:11 +0800867 kfree(adev->mode_info.atom_context->iio);
868 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400869 kfree(adev->mode_info.atom_context);
870 adev->mode_info.atom_context = NULL;
871 kfree(adev->mode_info.atom_card_info);
872 adev->mode_info.atom_card_info = NULL;
873}
874
875/**
876 * amdgpu_atombios_init - init the driver info and callbacks for atombios
877 *
878 * @adev: amdgpu_device pointer
879 *
880 * Initializes the driver info and register access callbacks for the
881 * ATOM interpreter (r4xx+).
882 * Returns 0 on sucess, -ENOMEM on failure.
883 * Called at driver startup.
884 */
885static int amdgpu_atombios_init(struct amdgpu_device *adev)
886{
887 struct card_info *atom_card_info =
888 kzalloc(sizeof(struct card_info), GFP_KERNEL);
889
890 if (!atom_card_info)
891 return -ENOMEM;
892
893 adev->mode_info.atom_card_info = atom_card_info;
894 atom_card_info->dev = adev->ddev;
895 atom_card_info->reg_read = cail_reg_read;
896 atom_card_info->reg_write = cail_reg_write;
897 /* needed for iio ops */
898 if (adev->rio_mem) {
899 atom_card_info->ioreg_read = cail_ioreg_read;
900 atom_card_info->ioreg_write = cail_ioreg_write;
901 } else {
Amber Linb64a18c2017-01-04 08:06:58 -0500902 DRM_INFO("PCI I/O BAR is not found. Using MMIO to access ATOM BIOS\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400903 atom_card_info->ioreg_read = cail_reg_read;
904 atom_card_info->ioreg_write = cail_reg_write;
905 }
906 atom_card_info->mc_read = cail_mc_read;
907 atom_card_info->mc_write = cail_mc_write;
908 atom_card_info->pll_read = cail_pll_read;
909 atom_card_info->pll_write = cail_pll_write;
910
911 adev->mode_info.atom_context = amdgpu_atom_parse(atom_card_info, adev->bios);
912 if (!adev->mode_info.atom_context) {
913 amdgpu_atombios_fini(adev);
914 return -ENOMEM;
915 }
916
917 mutex_init(&adev->mode_info.atom_context->mutex);
918 amdgpu_atombios_scratch_regs_init(adev);
919 amdgpu_atom_allocate_fb_scratch(adev->mode_info.atom_context);
920 return 0;
921}
922
923/* if we get transitioned to only one device, take VGA back */
924/**
925 * amdgpu_vga_set_decode - enable/disable vga decode
926 *
927 * @cookie: amdgpu_device pointer
928 * @state: enable/disable vga decode
929 *
930 * Enable/disable vga decode (all asics).
931 * Returns VGA resource flags.
932 */
933static unsigned int amdgpu_vga_set_decode(void *cookie, bool state)
934{
935 struct amdgpu_device *adev = cookie;
936 amdgpu_asic_set_vga_state(adev, state);
937 if (state)
938 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
939 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
940 else
941 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
942}
943
944/**
945 * amdgpu_check_pot_argument - check that argument is a power of two
946 *
947 * @arg: value to check
948 *
949 * Validates that a certain argument is a power of two (all asics).
950 * Returns true if argument is valid.
951 */
952static bool amdgpu_check_pot_argument(int arg)
953{
954 return (arg & (arg - 1)) == 0;
955}
956
957/**
958 * amdgpu_check_arguments - validate module params
959 *
960 * @adev: amdgpu_device pointer
961 *
962 * Validates certain module parameters and updates
963 * the associated values used by the driver (all asics).
964 */
965static void amdgpu_check_arguments(struct amdgpu_device *adev)
966{
Chunming Zhou5b011232015-12-10 17:34:33 +0800967 if (amdgpu_sched_jobs < 4) {
968 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
969 amdgpu_sched_jobs);
970 amdgpu_sched_jobs = 4;
971 } else if (!amdgpu_check_pot_argument(amdgpu_sched_jobs)){
972 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
973 amdgpu_sched_jobs);
974 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
975 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400976
977 if (amdgpu_gart_size != -1) {
Christian Königc4e1a132016-03-17 16:25:15 +0100978 /* gtt size must be greater or equal to 32M */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400979 if (amdgpu_gart_size < 32) {
980 dev_warn(adev->dev, "gart size (%d) too small\n",
981 amdgpu_gart_size);
982 amdgpu_gart_size = -1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400983 }
984 }
985
986 if (!amdgpu_check_pot_argument(amdgpu_vm_size)) {
987 dev_warn(adev->dev, "VM size (%d) must be a power of 2\n",
988 amdgpu_vm_size);
Alex Deucher8dacc122015-05-11 16:20:58 -0400989 amdgpu_vm_size = 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400990 }
991
992 if (amdgpu_vm_size < 1) {
993 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
994 amdgpu_vm_size);
Alex Deucher8dacc122015-05-11 16:20:58 -0400995 amdgpu_vm_size = 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400996 }
997
998 /*
999 * Max GPUVM size for Cayman, SI and CI are 40 bits.
1000 */
1001 if (amdgpu_vm_size > 1024) {
1002 dev_warn(adev->dev, "VM size (%d) too large, max is 1TB\n",
1003 amdgpu_vm_size);
Alex Deucher8dacc122015-05-11 16:20:58 -04001004 amdgpu_vm_size = 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001005 }
1006
1007 /* defines number of bits in page table versus page directory,
1008 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
1009 * page table and the remaining bits are in the page directory */
1010 if (amdgpu_vm_block_size == -1) {
1011
1012 /* Total bits covered by PD + PTs */
1013 unsigned bits = ilog2(amdgpu_vm_size) + 18;
1014
1015 /* Make sure the PD is 4K in size up to 8GB address space.
1016 Above that split equal between PD and PTs */
1017 if (amdgpu_vm_size <= 8)
1018 amdgpu_vm_block_size = bits - 9;
1019 else
1020 amdgpu_vm_block_size = (bits + 3) / 2;
1021
1022 } else if (amdgpu_vm_block_size < 9) {
1023 dev_warn(adev->dev, "VM page table size (%d) too small\n",
1024 amdgpu_vm_block_size);
1025 amdgpu_vm_block_size = 9;
1026 }
1027
1028 if (amdgpu_vm_block_size > 24 ||
1029 (amdgpu_vm_size * 1024) < (1ull << amdgpu_vm_block_size)) {
1030 dev_warn(adev->dev, "VM page table size (%d) too large\n",
1031 amdgpu_vm_block_size);
1032 amdgpu_vm_block_size = 9;
1033 }
Christian König6a7f76e2016-08-24 15:51:49 +02001034
jimqu526bae32016-11-07 09:53:10 +08001035 if (amdgpu_vram_page_split != -1 && (amdgpu_vram_page_split < 16 ||
1036 !amdgpu_check_pot_argument(amdgpu_vram_page_split))) {
Christian König6a7f76e2016-08-24 15:51:49 +02001037 dev_warn(adev->dev, "invalid VRAM page split (%d)\n",
1038 amdgpu_vram_page_split);
1039 amdgpu_vram_page_split = 1024;
1040 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001041}
1042
1043/**
1044 * amdgpu_switcheroo_set_state - set switcheroo state
1045 *
1046 * @pdev: pci dev pointer
Lukas Wunner16944672015-09-05 11:17:35 +02001047 * @state: vga_switcheroo state
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001048 *
1049 * Callback for the switcheroo driver. Suspends or resumes the
1050 * the asics before or after it is powered up using ACPI methods.
1051 */
1052static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1053{
1054 struct drm_device *dev = pci_get_drvdata(pdev);
1055
1056 if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
1057 return;
1058
1059 if (state == VGA_SWITCHEROO_ON) {
1060 unsigned d3_delay = dev->pdev->d3_delay;
1061
1062 printk(KERN_INFO "amdgpu: switched on\n");
1063 /* don't suspend or resume card normally */
1064 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1065
Alex Deucher810ddc32016-08-23 13:25:49 -04001066 amdgpu_device_resume(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001067
1068 dev->pdev->d3_delay = d3_delay;
1069
1070 dev->switch_power_state = DRM_SWITCH_POWER_ON;
1071 drm_kms_helper_poll_enable(dev);
1072 } else {
1073 printk(KERN_INFO "amdgpu: switched off\n");
1074 drm_kms_helper_poll_disable(dev);
1075 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Alex Deucher810ddc32016-08-23 13:25:49 -04001076 amdgpu_device_suspend(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001077 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1078 }
1079}
1080
1081/**
1082 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1083 *
1084 * @pdev: pci dev pointer
1085 *
1086 * Callback for the switcheroo driver. Check of the switcheroo
1087 * state can be changed.
1088 * Returns true if the state can be changed, false if not.
1089 */
1090static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1091{
1092 struct drm_device *dev = pci_get_drvdata(pdev);
1093
1094 /*
1095 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1096 * locking inversion with the driver load path. And the access here is
1097 * completely racy anyway. So don't bother with locking for now.
1098 */
1099 return dev->open_count == 0;
1100}
1101
1102static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1103 .set_gpu_state = amdgpu_switcheroo_set_state,
1104 .reprobe = NULL,
1105 .can_switch = amdgpu_switcheroo_can_switch,
1106};
1107
1108int amdgpu_set_clockgating_state(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001109 enum amd_ip_block_type block_type,
1110 enum amd_clockgating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001111{
1112 int i, r = 0;
1113
1114 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001115 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001116 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001117 if (adev->ip_blocks[i].version->type == block_type) {
1118 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1119 state);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001120 if (r)
1121 return r;
Alex Deuchera225bf12016-06-23 11:48:30 -04001122 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001123 }
1124 }
1125 return r;
1126}
1127
1128int amdgpu_set_powergating_state(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001129 enum amd_ip_block_type block_type,
1130 enum amd_powergating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001131{
1132 int i, r = 0;
1133
1134 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001135 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001136 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001137 if (adev->ip_blocks[i].version->type == block_type) {
1138 r = adev->ip_blocks[i].version->funcs->set_powergating_state((void *)adev,
1139 state);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001140 if (r)
1141 return r;
Alex Deuchera225bf12016-06-23 11:48:30 -04001142 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001143 }
1144 }
1145 return r;
1146}
1147
Huang Rui6cb2d4e2017-01-05 18:44:41 +08001148void amdgpu_get_clockgating_state(struct amdgpu_device *adev, u32 *flags)
1149{
1150 int i;
1151
1152 for (i = 0; i < adev->num_ip_blocks; i++) {
1153 if (!adev->ip_blocks[i].status.valid)
1154 continue;
1155 if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
1156 adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
1157 }
1158}
1159
Alex Deucher5dbbb602016-06-23 11:41:04 -04001160int amdgpu_wait_for_idle(struct amdgpu_device *adev,
1161 enum amd_ip_block_type block_type)
1162{
1163 int i, r;
1164
1165 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001166 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001167 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001168 if (adev->ip_blocks[i].version->type == block_type) {
1169 r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -04001170 if (r)
1171 return r;
1172 break;
1173 }
1174 }
1175 return 0;
1176
1177}
1178
1179bool amdgpu_is_idle(struct amdgpu_device *adev,
1180 enum amd_ip_block_type block_type)
1181{
1182 int i;
1183
1184 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001185 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001186 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001187 if (adev->ip_blocks[i].version->type == block_type)
1188 return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -04001189 }
1190 return true;
1191
1192}
1193
Alex Deuchera1255102016-10-13 17:41:13 -04001194struct amdgpu_ip_block * amdgpu_get_ip_block(struct amdgpu_device *adev,
1195 enum amd_ip_block_type type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001196{
1197 int i;
1198
1199 for (i = 0; i < adev->num_ip_blocks; i++)
Alex Deuchera1255102016-10-13 17:41:13 -04001200 if (adev->ip_blocks[i].version->type == type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001201 return &adev->ip_blocks[i];
1202
1203 return NULL;
1204}
1205
1206/**
1207 * amdgpu_ip_block_version_cmp
1208 *
1209 * @adev: amdgpu_device pointer
yanyang15fc3aee2015-05-22 14:39:35 -04001210 * @type: enum amd_ip_block_type
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001211 * @major: major version
1212 * @minor: minor version
1213 *
1214 * return 0 if equal or greater
1215 * return 1 if smaller or the ip_block doesn't exist
1216 */
1217int amdgpu_ip_block_version_cmp(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001218 enum amd_ip_block_type type,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001219 u32 major, u32 minor)
1220{
Alex Deuchera1255102016-10-13 17:41:13 -04001221 struct amdgpu_ip_block *ip_block = amdgpu_get_ip_block(adev, type);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001222
Alex Deuchera1255102016-10-13 17:41:13 -04001223 if (ip_block && ((ip_block->version->major > major) ||
1224 ((ip_block->version->major == major) &&
1225 (ip_block->version->minor >= minor))))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001226 return 0;
1227
1228 return 1;
1229}
1230
Alex Deuchera1255102016-10-13 17:41:13 -04001231/**
1232 * amdgpu_ip_block_add
1233 *
1234 * @adev: amdgpu_device pointer
1235 * @ip_block_version: pointer to the IP to add
1236 *
1237 * Adds the IP block driver information to the collection of IPs
1238 * on the asic.
1239 */
1240int amdgpu_ip_block_add(struct amdgpu_device *adev,
1241 const struct amdgpu_ip_block_version *ip_block_version)
1242{
1243 if (!ip_block_version)
1244 return -EINVAL;
1245
1246 adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
1247
1248 return 0;
1249}
1250
Alex Deucher483ef982016-09-30 12:43:04 -04001251static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
Emily Deng9accf2f2016-08-10 16:01:25 +08001252{
1253 adev->enable_virtual_display = false;
1254
1255 if (amdgpu_virtual_display) {
1256 struct drm_device *ddev = adev->ddev;
1257 const char *pci_address_name = pci_name(ddev->pdev);
Emily Deng0f663562016-09-30 13:02:18 -04001258 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
Emily Deng9accf2f2016-08-10 16:01:25 +08001259
1260 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1261 pciaddstr_tmp = pciaddstr;
Emily Deng0f663562016-09-30 13:02:18 -04001262 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1263 pciaddname = strsep(&pciaddname_tmp, ",");
Yintian Tao967de2a2017-01-22 15:16:51 +08001264 if (!strcmp("all", pciaddname)
1265 || !strcmp(pci_address_name, pciaddname)) {
Emily Deng0f663562016-09-30 13:02:18 -04001266 long num_crtc;
1267 int res = -1;
1268
Emily Deng9accf2f2016-08-10 16:01:25 +08001269 adev->enable_virtual_display = true;
Emily Deng0f663562016-09-30 13:02:18 -04001270
1271 if (pciaddname_tmp)
1272 res = kstrtol(pciaddname_tmp, 10,
1273 &num_crtc);
1274
1275 if (!res) {
1276 if (num_crtc < 1)
1277 num_crtc = 1;
1278 if (num_crtc > 6)
1279 num_crtc = 6;
1280 adev->mode_info.num_crtc = num_crtc;
1281 } else {
1282 adev->mode_info.num_crtc = 1;
1283 }
Emily Deng9accf2f2016-08-10 16:01:25 +08001284 break;
1285 }
1286 }
1287
Emily Deng0f663562016-09-30 13:02:18 -04001288 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
1289 amdgpu_virtual_display, pci_address_name,
1290 adev->enable_virtual_display, adev->mode_info.num_crtc);
Emily Deng9accf2f2016-08-10 16:01:25 +08001291
1292 kfree(pciaddstr);
1293 }
1294}
1295
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001296static int amdgpu_early_init(struct amdgpu_device *adev)
1297{
Alex Deucheraaa36a92015-04-20 17:31:14 -04001298 int i, r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001299
Alex Deucher483ef982016-09-30 12:43:04 -04001300 amdgpu_device_enable_virtual_display(adev);
Emily Denga6be7572016-08-08 11:37:50 +08001301
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001302 switch (adev->asic_type) {
Alex Deucheraaa36a92015-04-20 17:31:14 -04001303 case CHIP_TOPAZ:
1304 case CHIP_TONGA:
David Zhang48299f92015-07-08 01:05:16 +08001305 case CHIP_FIJI:
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001306 case CHIP_POLARIS11:
1307 case CHIP_POLARIS10:
Junwei Zhangc4642a42016-12-14 15:32:28 -05001308 case CHIP_POLARIS12:
Alex Deucheraaa36a92015-04-20 17:31:14 -04001309 case CHIP_CARRIZO:
Samuel Li39bb0c92015-10-08 16:31:43 -04001310 case CHIP_STONEY:
1311 if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
Alex Deucheraaa36a92015-04-20 17:31:14 -04001312 adev->family = AMDGPU_FAMILY_CZ;
1313 else
1314 adev->family = AMDGPU_FAMILY_VI;
1315
1316 r = vi_set_ip_blocks(adev);
1317 if (r)
1318 return r;
1319 break;
Ken Wang33f34802016-01-21 17:29:41 +08001320#ifdef CONFIG_DRM_AMDGPU_SI
1321 case CHIP_VERDE:
1322 case CHIP_TAHITI:
1323 case CHIP_PITCAIRN:
1324 case CHIP_OLAND:
1325 case CHIP_HAINAN:
Ken Wang295d0da2016-05-24 21:02:53 +08001326 adev->family = AMDGPU_FAMILY_SI;
Ken Wang33f34802016-01-21 17:29:41 +08001327 r = si_set_ip_blocks(adev);
1328 if (r)
1329 return r;
1330 break;
1331#endif
Alex Deuchera2e73f52015-04-20 17:09:27 -04001332#ifdef CONFIG_DRM_AMDGPU_CIK
1333 case CHIP_BONAIRE:
1334 case CHIP_HAWAII:
1335 case CHIP_KAVERI:
1336 case CHIP_KABINI:
1337 case CHIP_MULLINS:
1338 if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
1339 adev->family = AMDGPU_FAMILY_CI;
1340 else
1341 adev->family = AMDGPU_FAMILY_KV;
1342
1343 r = cik_set_ip_blocks(adev);
1344 if (r)
1345 return r;
1346 break;
1347#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001348 default:
1349 /* FIXME: not supported yet */
1350 return -EINVAL;
1351 }
1352
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001353 if (amdgpu_sriov_vf(adev)) {
1354 r = amdgpu_virt_request_full_gpu(adev, true);
1355 if (r)
1356 return r;
1357 }
1358
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001359 for (i = 0; i < adev->num_ip_blocks; i++) {
1360 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
1361 DRM_ERROR("disabled ip block: %d\n", i);
Alex Deuchera1255102016-10-13 17:41:13 -04001362 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001363 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001364 if (adev->ip_blocks[i].version->funcs->early_init) {
1365 r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001366 if (r == -ENOENT) {
Alex Deuchera1255102016-10-13 17:41:13 -04001367 adev->ip_blocks[i].status.valid = false;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001368 } else if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001369 DRM_ERROR("early_init of IP block <%s> failed %d\n",
1370 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001371 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001372 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001373 adev->ip_blocks[i].status.valid = true;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001374 }
Alex Deucher974e6b62015-07-10 13:59:44 -04001375 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001376 adev->ip_blocks[i].status.valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001377 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001378 }
1379 }
1380
Nicolai Hähnle395d1fb2016-06-02 12:32:07 +02001381 adev->cg_flags &= amdgpu_cg_mask;
1382 adev->pg_flags &= amdgpu_pg_mask;
1383
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001384 return 0;
1385}
1386
1387static int amdgpu_init(struct amdgpu_device *adev)
1388{
1389 int i, r;
1390
1391 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001392 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001393 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001394 r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001395 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001396 DRM_ERROR("sw_init of IP block <%s> failed %d\n",
1397 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001398 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001399 }
Alex Deuchera1255102016-10-13 17:41:13 -04001400 adev->ip_blocks[i].status.sw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001401 /* need to do gmc hw init early so we can allocate gpu mem */
Alex Deuchera1255102016-10-13 17:41:13 -04001402 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001403 r = amdgpu_vram_scratch_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001404 if (r) {
1405 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001406 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001407 }
Alex Deuchera1255102016-10-13 17:41:13 -04001408 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001409 if (r) {
1410 DRM_ERROR("hw_init %d failed %d\n", i, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001411 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001412 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001413 r = amdgpu_wb_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001414 if (r) {
1415 DRM_ERROR("amdgpu_wb_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001416 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001417 }
Alex Deuchera1255102016-10-13 17:41:13 -04001418 adev->ip_blocks[i].status.hw = true;
Monk Liu24936642017-01-09 15:54:32 +08001419
1420 /* right after GMC hw init, we create CSA */
1421 if (amdgpu_sriov_vf(adev)) {
1422 r = amdgpu_allocate_static_csa(adev);
1423 if (r) {
1424 DRM_ERROR("allocate CSA failed %d\n", r);
1425 return r;
1426 }
1427 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001428 }
1429 }
1430
1431 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001432 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001433 continue;
1434 /* gmc hw init is done early */
Alex Deuchera1255102016-10-13 17:41:13 -04001435 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001436 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001437 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001438 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001439 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
1440 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001441 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001442 }
Alex Deuchera1255102016-10-13 17:41:13 -04001443 adev->ip_blocks[i].status.hw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001444 }
1445
1446 return 0;
1447}
1448
1449static int amdgpu_late_init(struct amdgpu_device *adev)
1450{
1451 int i = 0, r;
1452
1453 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001454 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001455 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001456 if (adev->ip_blocks[i].version->funcs->late_init) {
1457 r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001458 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001459 DRM_ERROR("late_init of IP block <%s> failed %d\n",
1460 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001461 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001462 }
Alex Deuchera1255102016-10-13 17:41:13 -04001463 adev->ip_blocks[i].status.late_initialized = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001464 }
Alex Deucher4a446d52016-10-07 14:48:18 -04001465 /* skip CG for VCE/UVD, it's handled specially */
Alex Deuchera1255102016-10-13 17:41:13 -04001466 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1467 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
Alex Deucher4a446d52016-10-07 14:48:18 -04001468 /* enable clockgating to save power */
Alex Deuchera1255102016-10-13 17:41:13 -04001469 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1470 AMD_CG_STATE_GATE);
Alex Deucher4a446d52016-10-07 14:48:18 -04001471 if (r) {
1472 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001473 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher4a446d52016-10-07 14:48:18 -04001474 return r;
1475 }
Arindam Nathb0b00ff2016-10-07 19:01:37 +05301476 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001477 }
1478
1479 return 0;
1480}
1481
1482static int amdgpu_fini(struct amdgpu_device *adev)
1483{
1484 int i, r;
1485
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001486 /* need to disable SMC first */
1487 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001488 if (!adev->ip_blocks[i].status.hw)
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001489 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001490 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001491 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
Alex Deuchera1255102016-10-13 17:41:13 -04001492 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1493 AMD_CG_STATE_UNGATE);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001494 if (r) {
1495 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001496 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001497 return r;
1498 }
Alex Deuchera1255102016-10-13 17:41:13 -04001499 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001500 /* XXX handle errors */
1501 if (r) {
1502 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001503 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001504 }
Alex Deuchera1255102016-10-13 17:41:13 -04001505 adev->ip_blocks[i].status.hw = false;
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001506 break;
1507 }
1508 }
1509
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001510 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001511 if (!adev->ip_blocks[i].status.hw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001512 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001513 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001514 amdgpu_wb_fini(adev);
1515 amdgpu_vram_scratch_fini(adev);
1516 }
Rex Zhu8201a672016-11-24 21:44:44 +08001517
1518 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1519 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1520 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
1521 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1522 AMD_CG_STATE_UNGATE);
1523 if (r) {
1524 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1525 adev->ip_blocks[i].version->funcs->name, r);
1526 return r;
1527 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001528 }
Rex Zhu8201a672016-11-24 21:44:44 +08001529
Alex Deuchera1255102016-10-13 17:41:13 -04001530 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001531 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001532 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001533 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
1534 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001535 }
Rex Zhu8201a672016-11-24 21:44:44 +08001536
Alex Deuchera1255102016-10-13 17:41:13 -04001537 adev->ip_blocks[i].status.hw = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001538 }
1539
1540 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001541 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001542 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001543 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001544 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001545 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001546 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
1547 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001548 }
Alex Deuchera1255102016-10-13 17:41:13 -04001549 adev->ip_blocks[i].status.sw = false;
1550 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001551 }
1552
Monk Liua6dcfd92016-05-19 14:36:34 +08001553 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001554 if (!adev->ip_blocks[i].status.late_initialized)
Grazvydas Ignotas8a2eef12016-10-03 00:06:44 +03001555 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001556 if (adev->ip_blocks[i].version->funcs->late_fini)
1557 adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
1558 adev->ip_blocks[i].status.late_initialized = false;
Monk Liua6dcfd92016-05-19 14:36:34 +08001559 }
1560
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001561 if (amdgpu_sriov_vf(adev)) {
Monk Liu24936642017-01-09 15:54:32 +08001562 amdgpu_bo_free_kernel(&adev->virt.csa_obj, &adev->virt.csa_vmid0_addr, NULL);
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001563 amdgpu_virt_release_full_gpu(adev, false);
1564 }
Monk Liu24936642017-01-09 15:54:32 +08001565
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001566 return 0;
1567}
1568
Alex Deucherfaefba92016-12-06 10:38:29 -05001569int amdgpu_suspend(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001570{
1571 int i, r;
1572
Xiangliang Yue941ea92017-01-18 12:47:55 +08001573 if (amdgpu_sriov_vf(adev))
1574 amdgpu_virt_request_full_gpu(adev, false);
1575
Flora Cuic5a93a22016-02-26 10:45:25 +08001576 /* ungate SMC block first */
1577 r = amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC,
1578 AMD_CG_STATE_UNGATE);
1579 if (r) {
1580 DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n",r);
1581 }
1582
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001583 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001584 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001585 continue;
1586 /* ungate blocks so that suspend can properly shut them down */
Flora Cuic5a93a22016-02-26 10:45:25 +08001587 if (i != AMD_IP_BLOCK_TYPE_SMC) {
Alex Deuchera1255102016-10-13 17:41:13 -04001588 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1589 AMD_CG_STATE_UNGATE);
Flora Cuic5a93a22016-02-26 10:45:25 +08001590 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001591 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1592 adev->ip_blocks[i].version->funcs->name, r);
Flora Cuic5a93a22016-02-26 10:45:25 +08001593 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001594 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001595 /* XXX handle errors */
Alex Deuchera1255102016-10-13 17:41:13 -04001596 r = adev->ip_blocks[i].version->funcs->suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001597 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001598 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001599 DRM_ERROR("suspend of IP block <%s> failed %d\n",
1600 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001601 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001602 }
1603
Xiangliang Yue941ea92017-01-18 12:47:55 +08001604 if (amdgpu_sriov_vf(adev))
1605 amdgpu_virt_release_full_gpu(adev, false);
1606
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001607 return 0;
1608}
1609
1610static int amdgpu_resume(struct amdgpu_device *adev)
1611{
1612 int i, r;
1613
1614 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001615 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001616 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001617 r = adev->ip_blocks[i].version->funcs->resume(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001618 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001619 DRM_ERROR("resume of IP block <%s> failed %d\n",
1620 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001621 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001622 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001623 }
1624
1625 return 0;
1626}
1627
Monk Liu4e99a442016-03-31 13:26:59 +08001628static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
Andres Rodriguez048765a2016-06-11 02:51:32 -04001629{
Monk Liu4e99a442016-03-31 13:26:59 +08001630 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
Xiangliang Yu5a5099c2017-01-09 18:06:57 -05001631 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
Andres Rodriguez048765a2016-06-11 02:51:32 -04001632}
1633
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001634/**
1635 * amdgpu_device_init - initialize the driver
1636 *
1637 * @adev: amdgpu_device pointer
1638 * @pdev: drm dev pointer
1639 * @pdev: pci dev pointer
1640 * @flags: driver flags
1641 *
1642 * Initializes the driver info and hw (all asics).
1643 * Returns 0 for success or an error on failure.
1644 * Called at driver startup.
1645 */
1646int amdgpu_device_init(struct amdgpu_device *adev,
1647 struct drm_device *ddev,
1648 struct pci_dev *pdev,
1649 uint32_t flags)
1650{
1651 int r, i;
1652 bool runtime = false;
Marek Olšák95844d22016-08-17 23:49:27 +02001653 u32 max_MBps;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001654
1655 adev->shutdown = false;
1656 adev->dev = &pdev->dev;
1657 adev->ddev = ddev;
1658 adev->pdev = pdev;
1659 adev->flags = flags;
Jammy Zhou2f7d10b2015-07-22 11:29:01 +08001660 adev->asic_type = flags & AMD_ASIC_MASK;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001661 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
1662 adev->mc.gtt_size = 512 * 1024 * 1024;
1663 adev->accel_working = false;
1664 adev->num_rings = 0;
1665 adev->mman.buffer_funcs = NULL;
1666 adev->mman.buffer_funcs_ring = NULL;
1667 adev->vm_manager.vm_pte_funcs = NULL;
Christian König2d55e452016-02-08 17:37:38 +01001668 adev->vm_manager.vm_pte_num_rings = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001669 adev->gart.gart_funcs = NULL;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001670 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001671
1672 adev->smc_rreg = &amdgpu_invalid_rreg;
1673 adev->smc_wreg = &amdgpu_invalid_wreg;
1674 adev->pcie_rreg = &amdgpu_invalid_rreg;
1675 adev->pcie_wreg = &amdgpu_invalid_wreg;
Huang Rui36b9a952016-08-31 13:23:25 +08001676 adev->pciep_rreg = &amdgpu_invalid_rreg;
1677 adev->pciep_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001678 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
1679 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
1680 adev->didt_rreg = &amdgpu_invalid_rreg;
1681 adev->didt_wreg = &amdgpu_invalid_wreg;
Rex Zhuccdbb202016-06-08 12:47:41 +08001682 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
1683 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001684 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
1685 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
1686
Rex Zhuccdbb202016-06-08 12:47:41 +08001687
Alex Deucher3e39ab92015-06-05 15:04:33 -04001688 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
1689 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
1690 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001691
1692 /* mutex initialization are all done here so we
1693 * can recall function without having locking issues */
Christian König8d0a7ce2015-11-03 20:58:50 +01001694 mutex_init(&adev->vm_manager.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001695 atomic_set(&adev->irq.ih.lock, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001696 mutex_init(&adev->pm.mutex);
1697 mutex_init(&adev->gfx.gpu_clock_mutex);
1698 mutex_init(&adev->srbm_mutex);
1699 mutex_init(&adev->grbm_idx_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001700 mutex_init(&adev->mn_lock);
1701 hash_init(adev->mn_hash);
1702
1703 amdgpu_check_arguments(adev);
1704
1705 /* Registers mapping */
1706 /* TODO: block userspace mapping of io register */
1707 spin_lock_init(&adev->mmio_idx_lock);
1708 spin_lock_init(&adev->smc_idx_lock);
1709 spin_lock_init(&adev->pcie_idx_lock);
1710 spin_lock_init(&adev->uvd_ctx_idx_lock);
1711 spin_lock_init(&adev->didt_idx_lock);
Rex Zhuccdbb202016-06-08 12:47:41 +08001712 spin_lock_init(&adev->gc_cac_idx_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001713 spin_lock_init(&adev->audio_endpt_idx_lock);
Marek Olšák95844d22016-08-17 23:49:27 +02001714 spin_lock_init(&adev->mm_stats.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001715
Chunming Zhou0c4e7fa2016-08-17 11:41:30 +08001716 INIT_LIST_HEAD(&adev->shadow_list);
1717 mutex_init(&adev->shadow_list_lock);
1718
Chunming Zhou5c1354b2016-08-30 16:13:10 +08001719 INIT_LIST_HEAD(&adev->gtt_list);
1720 spin_lock_init(&adev->gtt_list_lock);
1721
Ken Wangda69c1612016-01-21 19:08:55 +08001722 if (adev->asic_type >= CHIP_BONAIRE) {
1723 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
1724 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
1725 } else {
1726 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
1727 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
1728 }
Chunming Zhou5c1354b2016-08-30 16:13:10 +08001729
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001730 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
1731 if (adev->rmmio == NULL) {
1732 return -ENOMEM;
1733 }
1734 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
1735 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
1736
Ken Wangda69c1612016-01-21 19:08:55 +08001737 if (adev->asic_type >= CHIP_BONAIRE)
1738 /* doorbell bar mapping */
1739 amdgpu_doorbell_init(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001740
1741 /* io port mapping */
1742 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
1743 if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
1744 adev->rio_mem_size = pci_resource_len(adev->pdev, i);
1745 adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
1746 break;
1747 }
1748 }
1749 if (adev->rio_mem == NULL)
Amber Linb64a18c2017-01-04 08:06:58 -05001750 DRM_INFO("PCI I/O BAR is not found.\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001751
1752 /* early init functions */
1753 r = amdgpu_early_init(adev);
1754 if (r)
1755 return r;
1756
1757 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
1758 /* this will fail for cards that aren't VGA class devices, just
1759 * ignore it */
1760 vga_client_register(adev->pdev, adev, NULL, amdgpu_vga_set_decode);
1761
1762 if (amdgpu_runtime_pm == 1)
1763 runtime = true;
Alex Deuchere9bef452016-04-25 13:12:18 -04001764 if (amdgpu_device_is_px(ddev))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001765 runtime = true;
Lukas Wunner84c8b222017-03-10 21:23:45 +01001766 if (!pci_is_thunderbolt_attached(adev->pdev))
1767 vga_switcheroo_register_client(adev->pdev,
1768 &amdgpu_switcheroo_ops, runtime);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001769 if (runtime)
1770 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
1771
1772 /* Read BIOS */
Alex Deucher83ba1262016-06-03 18:21:41 -04001773 if (!amdgpu_get_bios(adev)) {
1774 r = -EINVAL;
1775 goto failed;
1776 }
Nils Wallméniusf7e9e9f2016-12-14 21:52:45 +01001777
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001778 r = amdgpu_atombios_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001779 if (r) {
1780 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001781 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001782 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001783
Monk Liu4e99a442016-03-31 13:26:59 +08001784 /* detect if we are with an SRIOV vbios */
1785 amdgpu_device_detect_sriov_bios(adev);
Andres Rodriguez048765a2016-06-11 02:51:32 -04001786
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001787 /* Post card if necessary */
Monk Liubec86372016-09-14 19:38:08 +08001788 if (amdgpu_vpost_needed(adev)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001789 if (!adev->bios) {
Monk Liubec86372016-09-14 19:38:08 +08001790 dev_err(adev->dev, "no vBIOS found\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001791 r = -EINVAL;
1792 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001793 }
Monk Liubec86372016-09-14 19:38:08 +08001794 DRM_INFO("GPU posting now...\n");
Monk Liu4e99a442016-03-31 13:26:59 +08001795 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
1796 if (r) {
1797 dev_err(adev->dev, "gpu post error!\n");
1798 goto failed;
1799 }
1800 } else {
1801 DRM_INFO("GPU post is not needed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001802 }
1803
1804 /* Initialize clocks */
1805 r = amdgpu_atombios_get_clock_info(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001806 if (r) {
1807 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001808 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001809 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001810 /* init i2c buses */
1811 amdgpu_atombios_i2c_init(adev);
1812
1813 /* Fence driver */
1814 r = amdgpu_fence_driver_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001815 if (r) {
1816 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001817 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001818 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001819
1820 /* init the mode config */
1821 drm_mode_config_init(adev->ddev);
1822
1823 r = amdgpu_init(adev);
1824 if (r) {
Alex Deucher2c1a2782015-12-07 17:02:53 -05001825 dev_err(adev->dev, "amdgpu_init failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001826 amdgpu_fini(adev);
Alex Deucher83ba1262016-06-03 18:21:41 -04001827 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001828 }
1829
1830 adev->accel_working = true;
1831
Marek Olšák95844d22016-08-17 23:49:27 +02001832 /* Initialize the buffer migration limit. */
1833 if (amdgpu_moverate >= 0)
1834 max_MBps = amdgpu_moverate;
1835 else
1836 max_MBps = 8; /* Allow 8 MB/s. */
1837 /* Get a log2 for easy divisions. */
1838 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
1839
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001840 amdgpu_fbdev_init(adev);
1841
1842 r = amdgpu_ib_pool_init(adev);
1843 if (r) {
1844 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
Alex Deucher83ba1262016-06-03 18:21:41 -04001845 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001846 }
1847
1848 r = amdgpu_ib_ring_tests(adev);
1849 if (r)
1850 DRM_ERROR("ib ring test failed (%d).\n", r);
1851
1852 r = amdgpu_gem_debugfs_init(adev);
1853 if (r) {
1854 DRM_ERROR("registering gem debugfs failed (%d).\n", r);
1855 }
1856
1857 r = amdgpu_debugfs_regs_init(adev);
1858 if (r) {
1859 DRM_ERROR("registering register debugfs failed (%d).\n", r);
1860 }
1861
Huang Rui50ab2532016-06-12 15:51:09 +08001862 r = amdgpu_debugfs_firmware_init(adev);
1863 if (r) {
1864 DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
1865 return r;
1866 }
1867
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001868 if ((amdgpu_testing & 1)) {
1869 if (adev->accel_working)
1870 amdgpu_test_moves(adev);
1871 else
1872 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
1873 }
1874 if ((amdgpu_testing & 2)) {
1875 if (adev->accel_working)
1876 amdgpu_test_syncing(adev);
1877 else
1878 DRM_INFO("amdgpu: acceleration disabled, skipping sync tests\n");
1879 }
1880 if (amdgpu_benchmarking) {
1881 if (adev->accel_working)
1882 amdgpu_benchmark(adev, amdgpu_benchmarking);
1883 else
1884 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
1885 }
1886
1887 /* enable clockgating, etc. after ib tests, etc. since some blocks require
1888 * explicit gating rather than handling it automatically.
1889 */
1890 r = amdgpu_late_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001891 if (r) {
1892 dev_err(adev->dev, "amdgpu_late_init failed\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001893 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001894 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001895
1896 return 0;
Alex Deucher83ba1262016-06-03 18:21:41 -04001897
1898failed:
1899 if (runtime)
1900 vga_switcheroo_fini_domain_pm_ops(adev->dev);
1901 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001902}
1903
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001904/**
1905 * amdgpu_device_fini - tear down the driver
1906 *
1907 * @adev: amdgpu_device pointer
1908 *
1909 * Tear down the driver info (all asics).
1910 * Called at driver shutdown.
1911 */
1912void amdgpu_device_fini(struct amdgpu_device *adev)
1913{
1914 int r;
1915
1916 DRM_INFO("amdgpu: finishing device.\n");
1917 adev->shutdown = true;
Grazvydas Ignotasa951ed82016-09-25 23:34:48 +03001918 drm_crtc_force_disable_all(adev->ddev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001919 /* evict vram memory */
1920 amdgpu_bo_evict_vram(adev);
1921 amdgpu_ib_pool_fini(adev);
1922 amdgpu_fence_driver_fini(adev);
1923 amdgpu_fbdev_fini(adev);
1924 r = amdgpu_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001925 adev->accel_working = false;
1926 /* free i2c buses */
1927 amdgpu_i2c_fini(adev);
1928 amdgpu_atombios_fini(adev);
1929 kfree(adev->bios);
1930 adev->bios = NULL;
Lukas Wunner84c8b222017-03-10 21:23:45 +01001931 if (!pci_is_thunderbolt_attached(adev->pdev))
1932 vga_switcheroo_unregister_client(adev->pdev);
Alex Deucher83ba1262016-06-03 18:21:41 -04001933 if (adev->flags & AMD_IS_PX)
1934 vga_switcheroo_fini_domain_pm_ops(adev->dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001935 vga_client_register(adev->pdev, NULL, NULL, NULL);
1936 if (adev->rio_mem)
1937 pci_iounmap(adev->pdev, adev->rio_mem);
1938 adev->rio_mem = NULL;
1939 iounmap(adev->rmmio);
1940 adev->rmmio = NULL;
Ken Wangda69c1612016-01-21 19:08:55 +08001941 if (adev->asic_type >= CHIP_BONAIRE)
1942 amdgpu_doorbell_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001943 amdgpu_debugfs_regs_cleanup(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001944}
1945
1946
1947/*
1948 * Suspend & resume.
1949 */
1950/**
Alex Deucher810ddc32016-08-23 13:25:49 -04001951 * amdgpu_device_suspend - initiate device suspend
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001952 *
1953 * @pdev: drm dev pointer
1954 * @state: suspend state
1955 *
1956 * Puts the hw in the suspend state (all asics).
1957 * Returns 0 for success or an error on failure.
1958 * Called at driver suspend.
1959 */
Alex Deucher810ddc32016-08-23 13:25:49 -04001960int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001961{
1962 struct amdgpu_device *adev;
1963 struct drm_crtc *crtc;
1964 struct drm_connector *connector;
Alex Deucher5ceb54c2015-08-05 12:41:48 -04001965 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001966
1967 if (dev == NULL || dev->dev_private == NULL) {
1968 return -ENODEV;
1969 }
1970
1971 adev = dev->dev_private;
1972
1973 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
1974 return 0;
1975
1976 drm_kms_helper_poll_disable(dev);
1977
1978 /* turn off display hw */
Alex Deucher4c7fbc32015-09-23 14:32:06 -04001979 drm_modeset_lock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001980 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1981 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
1982 }
Alex Deucher4c7fbc32015-09-23 14:32:06 -04001983 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001984
Alex Deucher756e6882015-10-08 00:03:36 -04001985 /* unpin the front buffers and cursors */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001986 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Alex Deucher756e6882015-10-08 00:03:36 -04001987 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001988 struct amdgpu_framebuffer *rfb = to_amdgpu_framebuffer(crtc->primary->fb);
1989 struct amdgpu_bo *robj;
1990
Alex Deucher756e6882015-10-08 00:03:36 -04001991 if (amdgpu_crtc->cursor_bo) {
1992 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
1993 r = amdgpu_bo_reserve(aobj, false);
1994 if (r == 0) {
1995 amdgpu_bo_unpin(aobj);
1996 amdgpu_bo_unreserve(aobj);
1997 }
1998 }
1999
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002000 if (rfb == NULL || rfb->obj == NULL) {
2001 continue;
2002 }
2003 robj = gem_to_amdgpu_bo(rfb->obj);
2004 /* don't unpin kernel fb objects */
2005 if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
2006 r = amdgpu_bo_reserve(robj, false);
2007 if (r == 0) {
2008 amdgpu_bo_unpin(robj);
2009 amdgpu_bo_unreserve(robj);
2010 }
2011 }
2012 }
2013 /* evict vram memory */
2014 amdgpu_bo_evict_vram(adev);
2015
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002016 amdgpu_fence_driver_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002017
2018 r = amdgpu_suspend(adev);
2019
Alex Deuchera0a71e42016-10-10 12:41:36 -04002020 /* evict remaining vram memory
2021 * This second call to evict vram is to evict the gart page table
2022 * using the CPU.
2023 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002024 amdgpu_bo_evict_vram(adev);
2025
Alex Deuchere695e772016-10-19 14:40:58 -04002026 amdgpu_atombios_scratch_regs_save(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002027 pci_save_state(dev->pdev);
2028 if (suspend) {
2029 /* Shut down the device */
2030 pci_disable_device(dev->pdev);
2031 pci_set_power_state(dev->pdev, PCI_D3hot);
jimqu74b0b152016-09-07 17:09:12 +08002032 } else {
2033 r = amdgpu_asic_reset(adev);
2034 if (r)
2035 DRM_ERROR("amdgpu asic reset failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002036 }
2037
2038 if (fbcon) {
2039 console_lock();
2040 amdgpu_fbdev_set_suspend(adev, 1);
2041 console_unlock();
2042 }
2043 return 0;
2044}
2045
2046/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002047 * amdgpu_device_resume - initiate device resume
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002048 *
2049 * @pdev: drm dev pointer
2050 *
2051 * Bring the hw back to operating state (all asics).
2052 * Returns 0 for success or an error on failure.
2053 * Called at driver resume.
2054 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002055int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002056{
2057 struct drm_connector *connector;
2058 struct amdgpu_device *adev = dev->dev_private;
Alex Deucher756e6882015-10-08 00:03:36 -04002059 struct drm_crtc *crtc;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002060 int r;
2061
2062 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2063 return 0;
2064
jimqu74b0b152016-09-07 17:09:12 +08002065 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002066 console_lock();
jimqu74b0b152016-09-07 17:09:12 +08002067
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002068 if (resume) {
2069 pci_set_power_state(dev->pdev, PCI_D0);
2070 pci_restore_state(dev->pdev);
jimqu74b0b152016-09-07 17:09:12 +08002071 r = pci_enable_device(dev->pdev);
2072 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002073 if (fbcon)
2074 console_unlock();
jimqu74b0b152016-09-07 17:09:12 +08002075 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002076 }
2077 }
Alex Deuchere695e772016-10-19 14:40:58 -04002078 amdgpu_atombios_scratch_regs_restore(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002079
2080 /* post card */
Jim Quc836fec2017-02-10 15:59:59 +08002081 if (amdgpu_need_post(adev)) {
jimqu74b0b152016-09-07 17:09:12 +08002082 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2083 if (r)
2084 DRM_ERROR("amdgpu asic init failed\n");
2085 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002086
2087 r = amdgpu_resume(adev);
Flora Cuica198522016-02-04 15:10:08 +08002088 if (r)
2089 DRM_ERROR("amdgpu_resume failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002090
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002091 amdgpu_fence_driver_resume(adev);
2092
Flora Cuica198522016-02-04 15:10:08 +08002093 if (resume) {
2094 r = amdgpu_ib_ring_tests(adev);
2095 if (r)
2096 DRM_ERROR("ib ring test failed (%d).\n", r);
2097 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002098
2099 r = amdgpu_late_init(adev);
Jim Quc085bd52017-03-01 15:53:29 +08002100 if (r) {
2101 if (fbcon)
2102 console_unlock();
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002103 return r;
Jim Quc085bd52017-03-01 15:53:29 +08002104 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002105
Alex Deucher756e6882015-10-08 00:03:36 -04002106 /* pin cursors */
2107 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2108 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2109
2110 if (amdgpu_crtc->cursor_bo) {
2111 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
2112 r = amdgpu_bo_reserve(aobj, false);
2113 if (r == 0) {
2114 r = amdgpu_bo_pin(aobj,
2115 AMDGPU_GEM_DOMAIN_VRAM,
2116 &amdgpu_crtc->cursor_addr);
2117 if (r != 0)
2118 DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
2119 amdgpu_bo_unreserve(aobj);
2120 }
2121 }
2122 }
2123
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002124 /* blat the mode back in */
2125 if (fbcon) {
2126 drm_helper_resume_force_mode(dev);
2127 /* turn on display hw */
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002128 drm_modeset_lock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002129 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2130 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
2131 }
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002132 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002133 }
2134
2135 drm_kms_helper_poll_enable(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002136
2137 /*
2138 * Most of the connector probing functions try to acquire runtime pm
2139 * refs to ensure that the GPU is powered on when connector polling is
2140 * performed. Since we're calling this from a runtime PM callback,
2141 * trying to acquire rpm refs will cause us to deadlock.
2142 *
2143 * Since we're guaranteed to be holding the rpm lock, it's safe to
2144 * temporarily disable the rpm helpers so this doesn't deadlock us.
2145 */
2146#ifdef CONFIG_PM
2147 dev->dev->power.disable_depth++;
2148#endif
Alex Deucher54fb2a52015-11-24 14:30:56 -05002149 drm_helper_hpd_irq_event(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002150#ifdef CONFIG_PM
2151 dev->dev->power.disable_depth--;
2152#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002153
2154 if (fbcon) {
2155 amdgpu_fbdev_set_suspend(adev, 0);
2156 console_unlock();
2157 }
2158
2159 return 0;
2160}
2161
Chunming Zhou63fbf422016-07-15 11:19:20 +08002162static bool amdgpu_check_soft_reset(struct amdgpu_device *adev)
2163{
2164 int i;
2165 bool asic_hang = false;
2166
2167 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002168 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou63fbf422016-07-15 11:19:20 +08002169 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002170 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
2171 adev->ip_blocks[i].status.hang =
2172 adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
2173 if (adev->ip_blocks[i].status.hang) {
2174 DRM_INFO("IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
Chunming Zhou63fbf422016-07-15 11:19:20 +08002175 asic_hang = true;
2176 }
2177 }
2178 return asic_hang;
2179}
2180
Baoyou Xie4d446652016-09-18 22:09:35 +08002181static int amdgpu_pre_soft_reset(struct amdgpu_device *adev)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002182{
2183 int i, r = 0;
2184
2185 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002186 if (!adev->ip_blocks[i].status.valid)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002187 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002188 if (adev->ip_blocks[i].status.hang &&
2189 adev->ip_blocks[i].version->funcs->pre_soft_reset) {
2190 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
Chunming Zhoud31a5012016-07-18 10:04:34 +08002191 if (r)
2192 return r;
2193 }
2194 }
2195
2196 return 0;
2197}
2198
Chunming Zhou35d782f2016-07-15 15:57:13 +08002199static bool amdgpu_need_full_reset(struct amdgpu_device *adev)
2200{
Alex Deucherda146d32016-10-13 16:07:03 -04002201 int i;
2202
2203 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002204 if (!adev->ip_blocks[i].status.valid)
Alex Deucherda146d32016-10-13 16:07:03 -04002205 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002206 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
2207 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
2208 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
2209 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE)) {
2210 if (adev->ip_blocks[i].status.hang) {
Alex Deucherda146d32016-10-13 16:07:03 -04002211 DRM_INFO("Some block need full reset!\n");
2212 return true;
2213 }
2214 }
Chunming Zhou35d782f2016-07-15 15:57:13 +08002215 }
2216 return false;
2217}
2218
2219static int amdgpu_soft_reset(struct amdgpu_device *adev)
2220{
2221 int i, r = 0;
2222
2223 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002224 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002225 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002226 if (adev->ip_blocks[i].status.hang &&
2227 adev->ip_blocks[i].version->funcs->soft_reset) {
2228 r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002229 if (r)
2230 return r;
2231 }
2232 }
2233
2234 return 0;
2235}
2236
2237static int amdgpu_post_soft_reset(struct amdgpu_device *adev)
2238{
2239 int i, r = 0;
2240
2241 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002242 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002243 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002244 if (adev->ip_blocks[i].status.hang &&
2245 adev->ip_blocks[i].version->funcs->post_soft_reset)
2246 r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002247 if (r)
2248 return r;
2249 }
2250
2251 return 0;
2252}
2253
Chunming Zhou3ad81f12016-08-05 17:30:17 +08002254bool amdgpu_need_backup(struct amdgpu_device *adev)
2255{
2256 if (adev->flags & AMD_IS_APU)
2257 return false;
2258
2259 return amdgpu_lockup_timeout > 0 ? true : false;
2260}
2261
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002262static int amdgpu_recover_vram_from_shadow(struct amdgpu_device *adev,
2263 struct amdgpu_ring *ring,
2264 struct amdgpu_bo *bo,
Chris Wilsonf54d1862016-10-25 13:00:45 +01002265 struct dma_fence **fence)
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002266{
2267 uint32_t domain;
2268 int r;
2269
2270 if (!bo->shadow)
2271 return 0;
2272
2273 r = amdgpu_bo_reserve(bo, false);
2274 if (r)
2275 return r;
2276 domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
2277 /* if bo has been evicted, then no need to recover */
2278 if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
2279 r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
2280 NULL, fence, true);
2281 if (r) {
2282 DRM_ERROR("recover page table failed!\n");
2283 goto err;
2284 }
2285 }
2286err:
2287 amdgpu_bo_unreserve(bo);
2288 return r;
2289}
2290
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002291/**
2292 * amdgpu_gpu_reset - reset the asic
2293 *
2294 * @adev: amdgpu device pointer
2295 *
2296 * Attempt the reset the GPU if it has hung (all asics).
2297 * Returns 0 for success or an error on failure.
2298 */
2299int amdgpu_gpu_reset(struct amdgpu_device *adev)
2300{
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002301 int i, r;
2302 int resched;
Chunming Zhou35d782f2016-07-15 15:57:13 +08002303 bool need_full_reset;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002304
Xiangliang Yufb140b22016-12-17 22:48:57 +08002305 if (amdgpu_sriov_vf(adev))
2306 return 0;
2307
Chunming Zhou63fbf422016-07-15 11:19:20 +08002308 if (!amdgpu_check_soft_reset(adev)) {
2309 DRM_INFO("No hardware hang detected. Did some blocks stall?\n");
2310 return 0;
2311 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002312
Marek Olšákd94aed52015-05-05 21:13:49 +02002313 atomic_inc(&adev->gpu_reset_counter);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002314
Chunming Zhoua3c47d62016-06-30 16:44:41 +08002315 /* block TTM */
2316 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
2317
Chunming Zhou0875dc92016-06-12 15:41:58 +08002318 /* block scheduler */
2319 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2320 struct amdgpu_ring *ring = adev->rings[i];
2321
2322 if (!ring)
2323 continue;
2324 kthread_park(ring->sched.thread);
Chunming Zhouaa1c8902016-06-30 13:56:02 +08002325 amd_sched_hw_job_reset(&ring->sched);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002326 }
Chunming Zhou2200eda2016-06-30 16:53:02 +08002327 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
2328 amdgpu_fence_driver_force_completion(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002329
Chunming Zhou35d782f2016-07-15 15:57:13 +08002330 need_full_reset = amdgpu_need_full_reset(adev);
2331
2332 if (!need_full_reset) {
2333 amdgpu_pre_soft_reset(adev);
2334 r = amdgpu_soft_reset(adev);
2335 amdgpu_post_soft_reset(adev);
2336 if (r || amdgpu_check_soft_reset(adev)) {
2337 DRM_INFO("soft reset failed, will fallback to full reset!\n");
2338 need_full_reset = true;
2339 }
2340 }
2341
2342 if (need_full_reset) {
Chunming Zhou35d782f2016-07-15 15:57:13 +08002343 r = amdgpu_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002344
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002345retry:
Chunming Zhou35d782f2016-07-15 15:57:13 +08002346 /* Disable fb access */
2347 if (adev->mode_info.num_crtc) {
2348 struct amdgpu_mode_mc_save save;
2349 amdgpu_display_stop_mc_access(adev, &save);
2350 amdgpu_wait_for_idle(adev, AMD_IP_BLOCK_TYPE_GMC);
2351 }
Alex Deuchere695e772016-10-19 14:40:58 -04002352 amdgpu_atombios_scratch_regs_save(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002353 r = amdgpu_asic_reset(adev);
Alex Deuchere695e772016-10-19 14:40:58 -04002354 amdgpu_atombios_scratch_regs_restore(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002355 /* post card */
2356 amdgpu_atom_asic_init(adev->mode_info.atom_context);
Alex Deucherbfa99262016-01-15 11:59:48 -05002357
Chunming Zhou35d782f2016-07-15 15:57:13 +08002358 if (!r) {
2359 dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
2360 r = amdgpu_resume(adev);
2361 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002362 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002363 if (!r) {
Chunming Zhoue72cfd52016-07-27 13:15:20 +08002364 amdgpu_irq_gpu_reset_resume_helper(adev);
Chunming Zhou2c0d7312016-08-30 16:36:25 +08002365 if (need_full_reset && amdgpu_need_backup(adev)) {
2366 r = amdgpu_ttm_recover_gart(adev);
2367 if (r)
2368 DRM_ERROR("gart recovery failed!!!\n");
2369 }
Chunming Zhou1f465082016-06-30 15:02:26 +08002370 r = amdgpu_ib_ring_tests(adev);
2371 if (r) {
2372 dev_err(adev->dev, "ib ring test failed (%d).\n", r);
Chunming Zhou40019dc2016-06-29 16:01:49 +08002373 r = amdgpu_suspend(adev);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002374 need_full_reset = true;
Chunming Zhou40019dc2016-06-29 16:01:49 +08002375 goto retry;
Chunming Zhou1f465082016-06-30 15:02:26 +08002376 }
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002377 /**
2378 * recovery vm page tables, since we cannot depend on VRAM is
2379 * consistent after gpu full reset.
2380 */
2381 if (need_full_reset && amdgpu_need_backup(adev)) {
2382 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
2383 struct amdgpu_bo *bo, *tmp;
Chris Wilsonf54d1862016-10-25 13:00:45 +01002384 struct dma_fence *fence = NULL, *next = NULL;
Chunming Zhou1f465082016-06-30 15:02:26 +08002385
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002386 DRM_INFO("recover vram bo from shadow\n");
2387 mutex_lock(&adev->shadow_list_lock);
2388 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
2389 amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
2390 if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01002391 r = dma_fence_wait(fence, false);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002392 if (r) {
2393 WARN(r, "recovery from shadow isn't comleted\n");
2394 break;
2395 }
2396 }
2397
Chris Wilsonf54d1862016-10-25 13:00:45 +01002398 dma_fence_put(fence);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002399 fence = next;
2400 }
2401 mutex_unlock(&adev->shadow_list_lock);
2402 if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01002403 r = dma_fence_wait(fence, false);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002404 if (r)
2405 WARN(r, "recovery from shadow isn't comleted\n");
2406 }
Chris Wilsonf54d1862016-10-25 13:00:45 +01002407 dma_fence_put(fence);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002408 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002409 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2410 struct amdgpu_ring *ring = adev->rings[i];
2411 if (!ring)
2412 continue;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002413
Chunming Zhouaa1c8902016-06-30 13:56:02 +08002414 amd_sched_job_recovery(&ring->sched);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002415 kthread_unpark(ring->sched.thread);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002416 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002417 } else {
Chunming Zhou2200eda2016-06-30 16:53:02 +08002418 dev_err(adev->dev, "asic resume failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002419 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
Chunming Zhou0875dc92016-06-12 15:41:58 +08002420 if (adev->rings[i]) {
2421 kthread_unpark(adev->rings[i]->sched.thread);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002422 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002423 }
2424 }
2425
2426 drm_helper_resume_force_mode(adev->ddev);
2427
2428 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
2429 if (r) {
2430 /* bad news, how to tell it to userspace ? */
2431 dev_info(adev->dev, "GPU reset failed\n");
2432 }
2433
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002434 return r;
2435}
2436
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002437void amdgpu_get_pcie_info(struct amdgpu_device *adev)
2438{
2439 u32 mask;
2440 int ret;
2441
Alex Deuchercd474ba2016-02-04 10:21:23 -05002442 if (amdgpu_pcie_gen_cap)
2443 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
2444
2445 if (amdgpu_pcie_lane_cap)
2446 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
2447
2448 /* covers APUs as well */
2449 if (pci_is_root_bus(adev->pdev->bus)) {
2450 if (adev->pm.pcie_gen_mask == 0)
2451 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
2452 if (adev->pm.pcie_mlw_mask == 0)
2453 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002454 return;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002455 }
Alex Deuchercd474ba2016-02-04 10:21:23 -05002456
2457 if (adev->pm.pcie_gen_mask == 0) {
2458 ret = drm_pcie_get_speed_cap_mask(adev->ddev, &mask);
2459 if (!ret) {
2460 adev->pm.pcie_gen_mask = (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
2461 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
2462 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
2463
2464 if (mask & DRM_PCIE_SPEED_25)
2465 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
2466 if (mask & DRM_PCIE_SPEED_50)
2467 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2;
2468 if (mask & DRM_PCIE_SPEED_80)
2469 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3;
2470 } else {
2471 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
2472 }
2473 }
2474 if (adev->pm.pcie_mlw_mask == 0) {
2475 ret = drm_pcie_get_max_link_width(adev->ddev, &mask);
2476 if (!ret) {
2477 switch (mask) {
2478 case 32:
2479 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
2480 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
2481 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2482 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2483 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2484 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2485 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2486 break;
2487 case 16:
2488 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
2489 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2490 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2491 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2492 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2493 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2494 break;
2495 case 12:
2496 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2497 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2498 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2499 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2500 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2501 break;
2502 case 8:
2503 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2504 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2505 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2506 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2507 break;
2508 case 4:
2509 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2510 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2511 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2512 break;
2513 case 2:
2514 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2515 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2516 break;
2517 case 1:
2518 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
2519 break;
2520 default:
2521 break;
2522 }
2523 } else {
2524 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002525 }
2526 }
2527}
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002528
2529/*
2530 * Debugfs
2531 */
2532int amdgpu_debugfs_add_files(struct amdgpu_device *adev,
Nils Wallménius06ab6832016-05-02 12:46:15 -04002533 const struct drm_info_list *files,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002534 unsigned nfiles)
2535{
2536 unsigned i;
2537
2538 for (i = 0; i < adev->debugfs_count; i++) {
2539 if (adev->debugfs[i].files == files) {
2540 /* Already registered */
2541 return 0;
2542 }
2543 }
2544
2545 i = adev->debugfs_count + 1;
2546 if (i > AMDGPU_DEBUGFS_MAX_COMPONENTS) {
2547 DRM_ERROR("Reached maximum number of debugfs components.\n");
2548 DRM_ERROR("Report so we increase "
2549 "AMDGPU_DEBUGFS_MAX_COMPONENTS.\n");
2550 return -EINVAL;
2551 }
2552 adev->debugfs[adev->debugfs_count].files = files;
2553 adev->debugfs[adev->debugfs_count].num_files = nfiles;
2554 adev->debugfs_count = i;
2555#if defined(CONFIG_DEBUG_FS)
2556 drm_debugfs_create_files(files, nfiles,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002557 adev->ddev->primary->debugfs_root,
2558 adev->ddev->primary);
2559#endif
2560 return 0;
2561}
2562
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002563#if defined(CONFIG_DEBUG_FS)
2564
2565static ssize_t amdgpu_debugfs_regs_read(struct file *f, char __user *buf,
2566 size_t size, loff_t *pos)
2567{
Al Viro45063092016-12-04 18:24:56 -05002568 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002569 ssize_t result = 0;
2570 int r;
Tom St Denisbd122672016-07-28 09:39:22 -04002571 bool pm_pg_lock, use_bank;
Tom St Denis566281592016-06-27 11:55:07 -04002572 unsigned instance_bank, sh_bank, se_bank;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002573
2574 if (size & 0x3 || *pos & 0x3)
2575 return -EINVAL;
2576
Tom St Denisbd122672016-07-28 09:39:22 -04002577 /* are we reading registers for which a PG lock is necessary? */
2578 pm_pg_lock = (*pos >> 23) & 1;
2579
Tom St Denis566281592016-06-27 11:55:07 -04002580 if (*pos & (1ULL << 62)) {
2581 se_bank = (*pos >> 24) & 0x3FF;
2582 sh_bank = (*pos >> 34) & 0x3FF;
2583 instance_bank = (*pos >> 44) & 0x3FF;
Tom St Denis32977f92016-10-09 07:41:26 -04002584
2585 if (se_bank == 0x3FF)
2586 se_bank = 0xFFFFFFFF;
2587 if (sh_bank == 0x3FF)
2588 sh_bank = 0xFFFFFFFF;
2589 if (instance_bank == 0x3FF)
2590 instance_bank = 0xFFFFFFFF;
Tom St Denis566281592016-06-27 11:55:07 -04002591 use_bank = 1;
Tom St Denis566281592016-06-27 11:55:07 -04002592 } else {
2593 use_bank = 0;
2594 }
2595
Tom St Denis801a6aa9a62017-03-15 05:34:25 -04002596 *pos &= (1UL << 22) - 1;
Tom St Denisbd122672016-07-28 09:39:22 -04002597
Tom St Denis566281592016-06-27 11:55:07 -04002598 if (use_bank) {
Tom St Denis32977f92016-10-09 07:41:26 -04002599 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
2600 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
Tom St Denis566281592016-06-27 11:55:07 -04002601 return -EINVAL;
2602 mutex_lock(&adev->grbm_idx_mutex);
2603 amdgpu_gfx_select_se_sh(adev, se_bank,
2604 sh_bank, instance_bank);
2605 }
2606
Tom St Denisbd122672016-07-28 09:39:22 -04002607 if (pm_pg_lock)
2608 mutex_lock(&adev->pm.mutex);
2609
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002610 while (size) {
2611 uint32_t value;
2612
2613 if (*pos > adev->rmmio_size)
Tom St Denis566281592016-06-27 11:55:07 -04002614 goto end;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002615
2616 value = RREG32(*pos >> 2);
2617 r = put_user(value, (uint32_t *)buf);
Tom St Denis566281592016-06-27 11:55:07 -04002618 if (r) {
2619 result = r;
2620 goto end;
2621 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002622
2623 result += 4;
2624 buf += 4;
2625 *pos += 4;
2626 size -= 4;
2627 }
2628
Tom St Denis566281592016-06-27 11:55:07 -04002629end:
2630 if (use_bank) {
2631 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
2632 mutex_unlock(&adev->grbm_idx_mutex);
2633 }
2634
Tom St Denisbd122672016-07-28 09:39:22 -04002635 if (pm_pg_lock)
2636 mutex_unlock(&adev->pm.mutex);
2637
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002638 return result;
2639}
2640
2641static ssize_t amdgpu_debugfs_regs_write(struct file *f, const char __user *buf,
2642 size_t size, loff_t *pos)
2643{
Al Viro45063092016-12-04 18:24:56 -05002644 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002645 ssize_t result = 0;
2646 int r;
Tom St Denis394fdde2016-10-10 07:31:23 -04002647 bool pm_pg_lock, use_bank;
2648 unsigned instance_bank, sh_bank, se_bank;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002649
2650 if (size & 0x3 || *pos & 0x3)
2651 return -EINVAL;
2652
Tom St Denis394fdde2016-10-10 07:31:23 -04002653 /* are we reading registers for which a PG lock is necessary? */
2654 pm_pg_lock = (*pos >> 23) & 1;
2655
2656 if (*pos & (1ULL << 62)) {
2657 se_bank = (*pos >> 24) & 0x3FF;
2658 sh_bank = (*pos >> 34) & 0x3FF;
2659 instance_bank = (*pos >> 44) & 0x3FF;
2660
2661 if (se_bank == 0x3FF)
2662 se_bank = 0xFFFFFFFF;
2663 if (sh_bank == 0x3FF)
2664 sh_bank = 0xFFFFFFFF;
2665 if (instance_bank == 0x3FF)
2666 instance_bank = 0xFFFFFFFF;
2667 use_bank = 1;
2668 } else {
2669 use_bank = 0;
2670 }
2671
Tom St Denis801a6aa9a62017-03-15 05:34:25 -04002672 *pos &= (1UL << 22) - 1;
Tom St Denis394fdde2016-10-10 07:31:23 -04002673
2674 if (use_bank) {
2675 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
2676 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
2677 return -EINVAL;
2678 mutex_lock(&adev->grbm_idx_mutex);
2679 amdgpu_gfx_select_se_sh(adev, se_bank,
2680 sh_bank, instance_bank);
2681 }
2682
2683 if (pm_pg_lock)
2684 mutex_lock(&adev->pm.mutex);
2685
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002686 while (size) {
2687 uint32_t value;
2688
2689 if (*pos > adev->rmmio_size)
2690 return result;
2691
2692 r = get_user(value, (uint32_t *)buf);
2693 if (r)
2694 return r;
2695
2696 WREG32(*pos >> 2, value);
2697
2698 result += 4;
2699 buf += 4;
2700 *pos += 4;
2701 size -= 4;
2702 }
2703
Tom St Denis394fdde2016-10-10 07:31:23 -04002704 if (use_bank) {
2705 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
2706 mutex_unlock(&adev->grbm_idx_mutex);
2707 }
2708
2709 if (pm_pg_lock)
2710 mutex_unlock(&adev->pm.mutex);
2711
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002712 return result;
2713}
2714
Tom St Denisadcec282016-04-15 13:08:44 -04002715static ssize_t amdgpu_debugfs_regs_pcie_read(struct file *f, char __user *buf,
2716 size_t size, loff_t *pos)
2717{
Al Viro45063092016-12-04 18:24:56 -05002718 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04002719 ssize_t result = 0;
2720 int r;
2721
2722 if (size & 0x3 || *pos & 0x3)
2723 return -EINVAL;
2724
2725 while (size) {
2726 uint32_t value;
2727
2728 value = RREG32_PCIE(*pos >> 2);
2729 r = put_user(value, (uint32_t *)buf);
2730 if (r)
2731 return r;
2732
2733 result += 4;
2734 buf += 4;
2735 *pos += 4;
2736 size -= 4;
2737 }
2738
2739 return result;
2740}
2741
2742static ssize_t amdgpu_debugfs_regs_pcie_write(struct file *f, const char __user *buf,
2743 size_t size, loff_t *pos)
2744{
Al Viro45063092016-12-04 18:24:56 -05002745 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04002746 ssize_t result = 0;
2747 int r;
2748
2749 if (size & 0x3 || *pos & 0x3)
2750 return -EINVAL;
2751
2752 while (size) {
2753 uint32_t value;
2754
2755 r = get_user(value, (uint32_t *)buf);
2756 if (r)
2757 return r;
2758
2759 WREG32_PCIE(*pos >> 2, value);
2760
2761 result += 4;
2762 buf += 4;
2763 *pos += 4;
2764 size -= 4;
2765 }
2766
2767 return result;
2768}
2769
2770static ssize_t amdgpu_debugfs_regs_didt_read(struct file *f, char __user *buf,
2771 size_t size, loff_t *pos)
2772{
Al Viro45063092016-12-04 18:24:56 -05002773 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04002774 ssize_t result = 0;
2775 int r;
2776
2777 if (size & 0x3 || *pos & 0x3)
2778 return -EINVAL;
2779
2780 while (size) {
2781 uint32_t value;
2782
2783 value = RREG32_DIDT(*pos >> 2);
2784 r = put_user(value, (uint32_t *)buf);
2785 if (r)
2786 return r;
2787
2788 result += 4;
2789 buf += 4;
2790 *pos += 4;
2791 size -= 4;
2792 }
2793
2794 return result;
2795}
2796
2797static ssize_t amdgpu_debugfs_regs_didt_write(struct file *f, const char __user *buf,
2798 size_t size, loff_t *pos)
2799{
Al Viro45063092016-12-04 18:24:56 -05002800 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04002801 ssize_t result = 0;
2802 int r;
2803
2804 if (size & 0x3 || *pos & 0x3)
2805 return -EINVAL;
2806
2807 while (size) {
2808 uint32_t value;
2809
2810 r = get_user(value, (uint32_t *)buf);
2811 if (r)
2812 return r;
2813
2814 WREG32_DIDT(*pos >> 2, value);
2815
2816 result += 4;
2817 buf += 4;
2818 *pos += 4;
2819 size -= 4;
2820 }
2821
2822 return result;
2823}
2824
2825static ssize_t amdgpu_debugfs_regs_smc_read(struct file *f, char __user *buf,
2826 size_t size, loff_t *pos)
2827{
Al Viro45063092016-12-04 18:24:56 -05002828 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04002829 ssize_t result = 0;
2830 int r;
2831
2832 if (size & 0x3 || *pos & 0x3)
2833 return -EINVAL;
2834
2835 while (size) {
2836 uint32_t value;
2837
Tom St Denis6fc0dea2016-08-29 08:39:29 -04002838 value = RREG32_SMC(*pos);
Tom St Denisadcec282016-04-15 13:08:44 -04002839 r = put_user(value, (uint32_t *)buf);
2840 if (r)
2841 return r;
2842
2843 result += 4;
2844 buf += 4;
2845 *pos += 4;
2846 size -= 4;
2847 }
2848
2849 return result;
2850}
2851
2852static ssize_t amdgpu_debugfs_regs_smc_write(struct file *f, const char __user *buf,
2853 size_t size, loff_t *pos)
2854{
Al Viro45063092016-12-04 18:24:56 -05002855 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04002856 ssize_t result = 0;
2857 int r;
2858
2859 if (size & 0x3 || *pos & 0x3)
2860 return -EINVAL;
2861
2862 while (size) {
2863 uint32_t value;
2864
2865 r = get_user(value, (uint32_t *)buf);
2866 if (r)
2867 return r;
2868
Tom St Denis6fc0dea2016-08-29 08:39:29 -04002869 WREG32_SMC(*pos, value);
Tom St Denisadcec282016-04-15 13:08:44 -04002870
2871 result += 4;
2872 buf += 4;
2873 *pos += 4;
2874 size -= 4;
2875 }
2876
2877 return result;
2878}
2879
Tom St Denis1e051412016-06-27 09:57:18 -04002880static ssize_t amdgpu_debugfs_gca_config_read(struct file *f, char __user *buf,
2881 size_t size, loff_t *pos)
2882{
Al Viro45063092016-12-04 18:24:56 -05002883 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denis1e051412016-06-27 09:57:18 -04002884 ssize_t result = 0;
2885 int r;
2886 uint32_t *config, no_regs = 0;
2887
2888 if (size & 0x3 || *pos & 0x3)
2889 return -EINVAL;
2890
Markus Elfringecab7662016-09-18 17:00:52 +02002891 config = kmalloc_array(256, sizeof(*config), GFP_KERNEL);
Tom St Denis1e051412016-06-27 09:57:18 -04002892 if (!config)
2893 return -ENOMEM;
2894
2895 /* version, increment each time something is added */
Tom St Denis9a999352017-01-18 13:01:25 -05002896 config[no_regs++] = 3;
Tom St Denis1e051412016-06-27 09:57:18 -04002897 config[no_regs++] = adev->gfx.config.max_shader_engines;
2898 config[no_regs++] = adev->gfx.config.max_tile_pipes;
2899 config[no_regs++] = adev->gfx.config.max_cu_per_sh;
2900 config[no_regs++] = adev->gfx.config.max_sh_per_se;
2901 config[no_regs++] = adev->gfx.config.max_backends_per_se;
2902 config[no_regs++] = adev->gfx.config.max_texture_channel_caches;
2903 config[no_regs++] = adev->gfx.config.max_gprs;
2904 config[no_regs++] = adev->gfx.config.max_gs_threads;
2905 config[no_regs++] = adev->gfx.config.max_hw_contexts;
2906 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_frontend;
2907 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_backend;
2908 config[no_regs++] = adev->gfx.config.sc_hiz_tile_fifo_size;
2909 config[no_regs++] = adev->gfx.config.sc_earlyz_tile_fifo_size;
2910 config[no_regs++] = adev->gfx.config.num_tile_pipes;
2911 config[no_regs++] = adev->gfx.config.backend_enable_mask;
2912 config[no_regs++] = adev->gfx.config.mem_max_burst_length_bytes;
2913 config[no_regs++] = adev->gfx.config.mem_row_size_in_kb;
2914 config[no_regs++] = adev->gfx.config.shader_engine_tile_size;
2915 config[no_regs++] = adev->gfx.config.num_gpus;
2916 config[no_regs++] = adev->gfx.config.multi_gpu_tile_size;
2917 config[no_regs++] = adev->gfx.config.mc_arb_ramcfg;
2918 config[no_regs++] = adev->gfx.config.gb_addr_config;
2919 config[no_regs++] = adev->gfx.config.num_rbs;
2920
Tom St Denis89a8f302016-08-12 15:14:31 -04002921 /* rev==1 */
2922 config[no_regs++] = adev->rev_id;
2923 config[no_regs++] = adev->pg_flags;
2924 config[no_regs++] = adev->cg_flags;
2925
Tom St Denise9f11dc2016-08-17 12:00:51 -04002926 /* rev==2 */
2927 config[no_regs++] = adev->family;
2928 config[no_regs++] = adev->external_rev_id;
2929
Tom St Denis9a999352017-01-18 13:01:25 -05002930 /* rev==3 */
2931 config[no_regs++] = adev->pdev->device;
2932 config[no_regs++] = adev->pdev->revision;
2933 config[no_regs++] = adev->pdev->subsystem_device;
2934 config[no_regs++] = adev->pdev->subsystem_vendor;
2935
Tom St Denis1e051412016-06-27 09:57:18 -04002936 while (size && (*pos < no_regs * 4)) {
2937 uint32_t value;
2938
2939 value = config[*pos >> 2];
2940 r = put_user(value, (uint32_t *)buf);
2941 if (r) {
2942 kfree(config);
2943 return r;
2944 }
2945
2946 result += 4;
2947 buf += 4;
2948 *pos += 4;
2949 size -= 4;
2950 }
2951
2952 kfree(config);
2953 return result;
2954}
2955
Tom St Denisf2cdaf22016-09-15 10:08:44 -04002956static ssize_t amdgpu_debugfs_sensor_read(struct file *f, char __user *buf,
2957 size_t size, loff_t *pos)
2958{
Al Viro45063092016-12-04 18:24:56 -05002959 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisf2cdaf22016-09-15 10:08:44 -04002960 int idx, r;
2961 int32_t value;
2962
2963 if (size != 4 || *pos & 0x3)
2964 return -EINVAL;
2965
2966 /* convert offset to sensor number */
2967 idx = *pos >> 2;
2968
2969 if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->read_sensor)
2970 r = adev->powerplay.pp_funcs->read_sensor(adev->powerplay.pp_handle, idx, &value);
2971 else
2972 return -EINVAL;
2973
2974 if (!r)
2975 r = put_user(value, (int32_t *)buf);
2976
2977 return !r ? 4 : r;
2978}
Tom St Denis1e051412016-06-27 09:57:18 -04002979
Tom St Denis273d7aa2016-10-11 14:48:55 -04002980static ssize_t amdgpu_debugfs_wave_read(struct file *f, char __user *buf,
2981 size_t size, loff_t *pos)
2982{
2983 struct amdgpu_device *adev = f->f_inode->i_private;
2984 int r, x;
2985 ssize_t result=0;
Tom St Denis472259f2016-10-14 09:49:09 -04002986 uint32_t offset, se, sh, cu, wave, simd, data[32];
Tom St Denis273d7aa2016-10-11 14:48:55 -04002987
2988 if (size & 3 || *pos & 3)
2989 return -EINVAL;
2990
2991 /* decode offset */
2992 offset = (*pos & 0x7F);
2993 se = ((*pos >> 7) & 0xFF);
2994 sh = ((*pos >> 15) & 0xFF);
2995 cu = ((*pos >> 23) & 0xFF);
2996 wave = ((*pos >> 31) & 0xFF);
2997 simd = ((*pos >> 37) & 0xFF);
Tom St Denis273d7aa2016-10-11 14:48:55 -04002998
2999 /* switch to the specific se/sh/cu */
3000 mutex_lock(&adev->grbm_idx_mutex);
3001 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
3002
3003 x = 0;
Tom St Denis472259f2016-10-14 09:49:09 -04003004 if (adev->gfx.funcs->read_wave_data)
3005 adev->gfx.funcs->read_wave_data(adev, simd, wave, data, &x);
Tom St Denis273d7aa2016-10-11 14:48:55 -04003006
3007 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
3008 mutex_unlock(&adev->grbm_idx_mutex);
3009
Tom St Denis5ecfb3b2016-10-13 12:15:03 -04003010 if (!x)
3011 return -EINVAL;
3012
Tom St Denis472259f2016-10-14 09:49:09 -04003013 while (size && (offset < x * 4)) {
Tom St Denis273d7aa2016-10-11 14:48:55 -04003014 uint32_t value;
3015
Tom St Denis472259f2016-10-14 09:49:09 -04003016 value = data[offset >> 2];
Tom St Denis273d7aa2016-10-11 14:48:55 -04003017 r = put_user(value, (uint32_t *)buf);
3018 if (r)
3019 return r;
3020
3021 result += 4;
3022 buf += 4;
Tom St Denis472259f2016-10-14 09:49:09 -04003023 offset += 4;
Tom St Denis273d7aa2016-10-11 14:48:55 -04003024 size -= 4;
3025 }
3026
3027 return result;
3028}
3029
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003030static ssize_t amdgpu_debugfs_gpr_read(struct file *f, char __user *buf,
3031 size_t size, loff_t *pos)
3032{
3033 struct amdgpu_device *adev = f->f_inode->i_private;
3034 int r;
3035 ssize_t result = 0;
3036 uint32_t offset, se, sh, cu, wave, simd, thread, bank, *data;
3037
3038 if (size & 3 || *pos & 3)
3039 return -EINVAL;
3040
3041 /* decode offset */
3042 offset = (*pos & 0xFFF); /* in dwords */
3043 se = ((*pos >> 12) & 0xFF);
3044 sh = ((*pos >> 20) & 0xFF);
3045 cu = ((*pos >> 28) & 0xFF);
3046 wave = ((*pos >> 36) & 0xFF);
3047 simd = ((*pos >> 44) & 0xFF);
3048 thread = ((*pos >> 52) & 0xFF);
3049 bank = ((*pos >> 60) & 1);
3050
3051 data = kmalloc_array(1024, sizeof(*data), GFP_KERNEL);
3052 if (!data)
3053 return -ENOMEM;
3054
3055 /* switch to the specific se/sh/cu */
3056 mutex_lock(&adev->grbm_idx_mutex);
3057 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
3058
3059 if (bank == 0) {
3060 if (adev->gfx.funcs->read_wave_vgprs)
3061 adev->gfx.funcs->read_wave_vgprs(adev, simd, wave, thread, offset, size>>2, data);
3062 } else {
3063 if (adev->gfx.funcs->read_wave_sgprs)
3064 adev->gfx.funcs->read_wave_sgprs(adev, simd, wave, offset, size>>2, data);
3065 }
3066
3067 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
3068 mutex_unlock(&adev->grbm_idx_mutex);
3069
3070 while (size) {
3071 uint32_t value;
3072
3073 value = data[offset++];
3074 r = put_user(value, (uint32_t *)buf);
3075 if (r) {
3076 result = r;
3077 goto err;
3078 }
3079
3080 result += 4;
3081 buf += 4;
3082 size -= 4;
3083 }
3084
3085err:
3086 kfree(data);
3087 return result;
3088}
3089
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003090static const struct file_operations amdgpu_debugfs_regs_fops = {
3091 .owner = THIS_MODULE,
3092 .read = amdgpu_debugfs_regs_read,
3093 .write = amdgpu_debugfs_regs_write,
3094 .llseek = default_llseek
3095};
Tom St Denisadcec282016-04-15 13:08:44 -04003096static const struct file_operations amdgpu_debugfs_regs_didt_fops = {
3097 .owner = THIS_MODULE,
3098 .read = amdgpu_debugfs_regs_didt_read,
3099 .write = amdgpu_debugfs_regs_didt_write,
3100 .llseek = default_llseek
3101};
3102static const struct file_operations amdgpu_debugfs_regs_pcie_fops = {
3103 .owner = THIS_MODULE,
3104 .read = amdgpu_debugfs_regs_pcie_read,
3105 .write = amdgpu_debugfs_regs_pcie_write,
3106 .llseek = default_llseek
3107};
3108static const struct file_operations amdgpu_debugfs_regs_smc_fops = {
3109 .owner = THIS_MODULE,
3110 .read = amdgpu_debugfs_regs_smc_read,
3111 .write = amdgpu_debugfs_regs_smc_write,
3112 .llseek = default_llseek
3113};
3114
Tom St Denis1e051412016-06-27 09:57:18 -04003115static const struct file_operations amdgpu_debugfs_gca_config_fops = {
3116 .owner = THIS_MODULE,
3117 .read = amdgpu_debugfs_gca_config_read,
3118 .llseek = default_llseek
3119};
3120
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003121static const struct file_operations amdgpu_debugfs_sensors_fops = {
3122 .owner = THIS_MODULE,
3123 .read = amdgpu_debugfs_sensor_read,
3124 .llseek = default_llseek
3125};
3126
Tom St Denis273d7aa2016-10-11 14:48:55 -04003127static const struct file_operations amdgpu_debugfs_wave_fops = {
3128 .owner = THIS_MODULE,
3129 .read = amdgpu_debugfs_wave_read,
3130 .llseek = default_llseek
3131};
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003132static const struct file_operations amdgpu_debugfs_gpr_fops = {
3133 .owner = THIS_MODULE,
3134 .read = amdgpu_debugfs_gpr_read,
3135 .llseek = default_llseek
3136};
Tom St Denis273d7aa2016-10-11 14:48:55 -04003137
Tom St Denisadcec282016-04-15 13:08:44 -04003138static const struct file_operations *debugfs_regs[] = {
3139 &amdgpu_debugfs_regs_fops,
3140 &amdgpu_debugfs_regs_didt_fops,
3141 &amdgpu_debugfs_regs_pcie_fops,
3142 &amdgpu_debugfs_regs_smc_fops,
Tom St Denis1e051412016-06-27 09:57:18 -04003143 &amdgpu_debugfs_gca_config_fops,
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003144 &amdgpu_debugfs_sensors_fops,
Tom St Denis273d7aa2016-10-11 14:48:55 -04003145 &amdgpu_debugfs_wave_fops,
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003146 &amdgpu_debugfs_gpr_fops,
Tom St Denisadcec282016-04-15 13:08:44 -04003147};
3148
3149static const char *debugfs_regs_names[] = {
3150 "amdgpu_regs",
3151 "amdgpu_regs_didt",
3152 "amdgpu_regs_pcie",
3153 "amdgpu_regs_smc",
Tom St Denis1e051412016-06-27 09:57:18 -04003154 "amdgpu_gca_config",
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003155 "amdgpu_sensors",
Tom St Denis273d7aa2016-10-11 14:48:55 -04003156 "amdgpu_wave",
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003157 "amdgpu_gpr",
Tom St Denisadcec282016-04-15 13:08:44 -04003158};
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003159
3160static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
3161{
3162 struct drm_minor *minor = adev->ddev->primary;
3163 struct dentry *ent, *root = minor->debugfs_root;
Tom St Denisadcec282016-04-15 13:08:44 -04003164 unsigned i, j;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003165
Tom St Denisadcec282016-04-15 13:08:44 -04003166 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3167 ent = debugfs_create_file(debugfs_regs_names[i],
3168 S_IFREG | S_IRUGO, root,
3169 adev, debugfs_regs[i]);
3170 if (IS_ERR(ent)) {
3171 for (j = 0; j < i; j++) {
3172 debugfs_remove(adev->debugfs_regs[i]);
3173 adev->debugfs_regs[i] = NULL;
3174 }
3175 return PTR_ERR(ent);
3176 }
3177
3178 if (!i)
3179 i_size_write(ent->d_inode, adev->rmmio_size);
3180 adev->debugfs_regs[i] = ent;
3181 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003182
3183 return 0;
3184}
3185
3186static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev)
3187{
Tom St Denisadcec282016-04-15 13:08:44 -04003188 unsigned i;
3189
3190 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3191 if (adev->debugfs_regs[i]) {
3192 debugfs_remove(adev->debugfs_regs[i]);
3193 adev->debugfs_regs[i] = NULL;
3194 }
3195 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003196}
3197
3198int amdgpu_debugfs_init(struct drm_minor *minor)
3199{
3200 return 0;
3201}
Alexander Kuleshov7cebc722015-06-27 13:16:05 +06003202#else
3203static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
3204{
3205 return 0;
3206}
3207static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev) { }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003208#endif