blob: 0e629931210c6a05ed4df893aef8f7456fe99192 [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 Deuchera5bde2f2016-09-23 16:23:41 -040043#include "amdgpu_atomfirmware.h"
Alex Deucherd0dd7f02015-11-11 19:45:06 -050044#include "amd_pcie.h"
Ken Wang33f34802016-01-21 17:29:41 +080045#ifdef CONFIG_DRM_AMDGPU_SI
46#include "si.h"
47#endif
Alex Deuchera2e73f52015-04-20 17:09:27 -040048#ifdef CONFIG_DRM_AMDGPU_CIK
49#include "cik.h"
50#endif
Alex Deucheraaa36a92015-04-20 17:31:14 -040051#include "vi.h"
Ken Wang460826e2017-03-06 14:53:16 -050052#include "soc15.h"
Alex Deucherd38ceaf2015-04-20 16:55:21 -040053#include "bif/bif_4_1_d.h"
Emily Deng9accf2f2016-08-10 16:01:25 +080054#include <linux/pci.h>
Monk Liubec86372016-09-14 19:38:08 +080055#include <linux/firmware.h>
Gavin Wan89041942017-06-23 13:55:15 -040056#include "amdgpu_vf_error.h"
Alex Deucherd38ceaf2015-04-20 16:55:21 -040057
Alex Deuchere2a75f82017-04-27 16:58:01 -040058MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin");
Alex Deucher2d2e5e72017-05-09 12:27:35 -040059MODULE_FIRMWARE("amdgpu/raven_gpu_info.bin");
Alex Deuchere2a75f82017-04-27 16:58:01 -040060
Shirish S2dc80b02017-05-25 10:05:25 +053061#define AMDGPU_RESUME_MS 2000
62
Alex Deucherd38ceaf2015-04-20 16:55:21 -040063static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev);
64static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev);
Huang Rui4f0955f2017-05-10 23:04:06 +080065static int amdgpu_debugfs_test_ib_ring_init(struct amdgpu_device *adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040066
67static const char *amdgpu_asic_name[] = {
Ken Wangda69c1612016-01-21 19:08:55 +080068 "TAHITI",
69 "PITCAIRN",
70 "VERDE",
71 "OLAND",
72 "HAINAN",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040073 "BONAIRE",
74 "KAVERI",
75 "KABINI",
76 "HAWAII",
77 "MULLINS",
78 "TOPAZ",
79 "TONGA",
David Zhang48299f92015-07-08 01:05:16 +080080 "FIJI",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040081 "CARRIZO",
Samuel Li139f4912015-10-08 14:50:27 -040082 "STONEY",
Flora Cui2cc0c0b2016-03-14 18:33:29 -040083 "POLARIS10",
84 "POLARIS11",
Junwei Zhangc4642a42016-12-14 15:32:28 -050085 "POLARIS12",
Ken Wangd4196f02016-03-09 09:28:32 +080086 "VEGA10",
Chunming Zhou2ca8a5d2016-12-07 17:31:19 +080087 "RAVEN",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040088 "LAST",
89};
90
91bool amdgpu_device_is_px(struct drm_device *dev)
92{
93 struct amdgpu_device *adev = dev->dev_private;
94
Jammy Zhou2f7d10b2015-07-22 11:29:01 +080095 if (adev->flags & AMD_IS_PX)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040096 return true;
97 return false;
98}
99
100/*
101 * MMIO register access helper functions.
102 */
103uint32_t amdgpu_mm_rreg(struct amdgpu_device *adev, uint32_t reg,
Monk Liu15d72fd2017-01-25 15:07:40 +0800104 uint32_t acc_flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400105{
Tom St Denisf4b373f2016-05-31 08:02:27 -0400106 uint32_t ret;
107
Monk Liu15d72fd2017-01-25 15:07:40 +0800108 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev)) {
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800109 BUG_ON(in_interrupt());
110 return amdgpu_virt_kiq_rreg(adev, reg);
111 }
112
Monk Liu15d72fd2017-01-25 15:07:40 +0800113 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
Tom St Denisf4b373f2016-05-31 08:02:27 -0400114 ret = readl(((void __iomem *)adev->rmmio) + (reg * 4));
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400115 else {
116 unsigned long flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400117
118 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
119 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
120 ret = readl(((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
121 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400122 }
Tom St Denisf4b373f2016-05-31 08:02:27 -0400123 trace_amdgpu_mm_rreg(adev->pdev->device, reg, ret);
124 return ret;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400125}
126
127void amdgpu_mm_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v,
Monk Liu15d72fd2017-01-25 15:07:40 +0800128 uint32_t acc_flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400129{
Tom St Denisf4b373f2016-05-31 08:02:27 -0400130 trace_amdgpu_mm_wreg(adev->pdev->device, reg, v);
Monk Liu4e99a442016-03-31 13:26:59 +0800131
Ken Wang47ed4e12017-07-04 13:11:52 +0800132 if (adev->asic_type >= CHIP_VEGA10 && reg == 0) {
133 adev->last_mm_index = v;
134 }
135
Monk Liu15d72fd2017-01-25 15:07:40 +0800136 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev)) {
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800137 BUG_ON(in_interrupt());
138 return amdgpu_virt_kiq_wreg(adev, reg, v);
139 }
140
Monk Liu15d72fd2017-01-25 15:07:40 +0800141 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400142 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
143 else {
144 unsigned long flags;
145
146 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
147 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
148 writel(v, ((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
149 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
150 }
Ken Wang47ed4e12017-07-04 13:11:52 +0800151
152 if (adev->asic_type >= CHIP_VEGA10 && reg == 1 && adev->last_mm_index == 0x5702C) {
153 udelay(500);
154 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400155}
156
157u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 reg)
158{
159 if ((reg * 4) < adev->rio_mem_size)
160 return ioread32(adev->rio_mem + (reg * 4));
161 else {
162 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
163 return ioread32(adev->rio_mem + (mmMM_DATA * 4));
164 }
165}
166
167void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
168{
Ken Wang47ed4e12017-07-04 13:11:52 +0800169 if (adev->asic_type >= CHIP_VEGA10 && reg == 0) {
170 adev->last_mm_index = v;
171 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400172
173 if ((reg * 4) < adev->rio_mem_size)
174 iowrite32(v, adev->rio_mem + (reg * 4));
175 else {
176 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
177 iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
178 }
Ken Wang47ed4e12017-07-04 13:11:52 +0800179
180 if (adev->asic_type >= CHIP_VEGA10 && reg == 1 && adev->last_mm_index == 0x5702C) {
181 udelay(500);
182 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400183}
184
185/**
186 * amdgpu_mm_rdoorbell - read a doorbell dword
187 *
188 * @adev: amdgpu_device pointer
189 * @index: doorbell index
190 *
191 * Returns the value in the doorbell aperture at the
192 * requested doorbell index (CIK).
193 */
194u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
195{
196 if (index < adev->doorbell.num_doorbells) {
197 return readl(adev->doorbell.ptr + index);
198 } else {
199 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
200 return 0;
201 }
202}
203
204/**
205 * amdgpu_mm_wdoorbell - write a doorbell dword
206 *
207 * @adev: amdgpu_device pointer
208 * @index: doorbell index
209 * @v: value to write
210 *
211 * Writes @v to the doorbell aperture at the
212 * requested doorbell index (CIK).
213 */
214void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
215{
216 if (index < adev->doorbell.num_doorbells) {
217 writel(v, adev->doorbell.ptr + index);
218 } else {
219 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
220 }
221}
222
223/**
Ken Wang832be402016-03-18 15:23:08 +0800224 * amdgpu_mm_rdoorbell64 - read a doorbell Qword
225 *
226 * @adev: amdgpu_device pointer
227 * @index: doorbell index
228 *
229 * Returns the value in the doorbell aperture at the
230 * requested doorbell index (VEGA10+).
231 */
232u64 amdgpu_mm_rdoorbell64(struct amdgpu_device *adev, u32 index)
233{
234 if (index < adev->doorbell.num_doorbells) {
235 return atomic64_read((atomic64_t *)(adev->doorbell.ptr + index));
236 } else {
237 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
238 return 0;
239 }
240}
241
242/**
243 * amdgpu_mm_wdoorbell64 - write a doorbell Qword
244 *
245 * @adev: amdgpu_device pointer
246 * @index: doorbell index
247 * @v: value to write
248 *
249 * Writes @v to the doorbell aperture at the
250 * requested doorbell index (VEGA10+).
251 */
252void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v)
253{
254 if (index < adev->doorbell.num_doorbells) {
255 atomic64_set((atomic64_t *)(adev->doorbell.ptr + index), v);
256 } else {
257 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
258 }
259}
260
261/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400262 * amdgpu_invalid_rreg - dummy reg read function
263 *
264 * @adev: amdgpu device pointer
265 * @reg: offset of register
266 *
267 * Dummy register read function. Used for register blocks
268 * that certain asics don't have (all asics).
269 * Returns the value in the register.
270 */
271static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
272{
273 DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
274 BUG();
275 return 0;
276}
277
278/**
279 * amdgpu_invalid_wreg - dummy reg write function
280 *
281 * @adev: amdgpu device pointer
282 * @reg: offset of register
283 * @v: value to write to the register
284 *
285 * Dummy register read function. Used for register blocks
286 * that certain asics don't have (all asics).
287 */
288static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
289{
290 DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
291 reg, v);
292 BUG();
293}
294
295/**
296 * amdgpu_block_invalid_rreg - dummy reg read function
297 *
298 * @adev: amdgpu device pointer
299 * @block: offset of instance
300 * @reg: offset of register
301 *
302 * Dummy register read function. Used for register blocks
303 * that certain asics don't have (all asics).
304 * Returns the value in the register.
305 */
306static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
307 uint32_t block, uint32_t reg)
308{
309 DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
310 reg, block);
311 BUG();
312 return 0;
313}
314
315/**
316 * amdgpu_block_invalid_wreg - dummy reg write function
317 *
318 * @adev: amdgpu device pointer
319 * @block: offset of instance
320 * @reg: offset of register
321 * @v: value to write to the register
322 *
323 * Dummy register read function. Used for register blocks
324 * that certain asics don't have (all asics).
325 */
326static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
327 uint32_t block,
328 uint32_t reg, uint32_t v)
329{
330 DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
331 reg, block, v);
332 BUG();
333}
334
335static int amdgpu_vram_scratch_init(struct amdgpu_device *adev)
336{
337 int r;
338
339 if (adev->vram_scratch.robj == NULL) {
340 r = amdgpu_bo_create(adev, AMDGPU_GPU_PAGE_SIZE,
Alex Deucher857d9132015-08-27 00:14:16 -0400341 PAGE_SIZE, true, AMDGPU_GEM_DOMAIN_VRAM,
Christian König03f48dd2016-08-15 17:00:22 +0200342 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
343 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS,
Christian König72d76682015-09-03 17:34:59 +0200344 NULL, NULL, &adev->vram_scratch.robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400345 if (r) {
346 return r;
347 }
348 }
349
350 r = amdgpu_bo_reserve(adev->vram_scratch.robj, false);
351 if (unlikely(r != 0))
352 return r;
353 r = amdgpu_bo_pin(adev->vram_scratch.robj,
354 AMDGPU_GEM_DOMAIN_VRAM, &adev->vram_scratch.gpu_addr);
355 if (r) {
356 amdgpu_bo_unreserve(adev->vram_scratch.robj);
357 return r;
358 }
359 r = amdgpu_bo_kmap(adev->vram_scratch.robj,
360 (void **)&adev->vram_scratch.ptr);
361 if (r)
362 amdgpu_bo_unpin(adev->vram_scratch.robj);
363 amdgpu_bo_unreserve(adev->vram_scratch.robj);
364
365 return r;
366}
367
368static void amdgpu_vram_scratch_fini(struct amdgpu_device *adev)
369{
370 int r;
371
372 if (adev->vram_scratch.robj == NULL) {
373 return;
374 }
Alex Xie8ab25b42017-04-24 13:30:43 -0400375 r = amdgpu_bo_reserve(adev->vram_scratch.robj, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400376 if (likely(r == 0)) {
377 amdgpu_bo_kunmap(adev->vram_scratch.robj);
378 amdgpu_bo_unpin(adev->vram_scratch.robj);
379 amdgpu_bo_unreserve(adev->vram_scratch.robj);
380 }
381 amdgpu_bo_unref(&adev->vram_scratch.robj);
382}
383
384/**
385 * amdgpu_program_register_sequence - program an array of registers.
386 *
387 * @adev: amdgpu_device pointer
388 * @registers: pointer to the register array
389 * @array_size: size of the register array
390 *
391 * Programs an array or registers with and and or masks.
392 * This is a helper for setting golden registers.
393 */
394void amdgpu_program_register_sequence(struct amdgpu_device *adev,
395 const u32 *registers,
396 const u32 array_size)
397{
398 u32 tmp, reg, and_mask, or_mask;
399 int i;
400
401 if (array_size % 3)
402 return;
403
404 for (i = 0; i < array_size; i +=3) {
405 reg = registers[i + 0];
406 and_mask = registers[i + 1];
407 or_mask = registers[i + 2];
408
409 if (and_mask == 0xffffffff) {
410 tmp = or_mask;
411 } else {
412 tmp = RREG32(reg);
413 tmp &= ~and_mask;
414 tmp |= or_mask;
415 }
416 WREG32(reg, tmp);
417 }
418}
419
420void amdgpu_pci_config_reset(struct amdgpu_device *adev)
421{
422 pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
423}
424
425/*
426 * GPU doorbell aperture helpers function.
427 */
428/**
429 * amdgpu_doorbell_init - Init doorbell driver information.
430 *
431 * @adev: amdgpu_device pointer
432 *
433 * Init doorbell driver information (CIK)
434 * Returns 0 on success, error on failure.
435 */
436static int amdgpu_doorbell_init(struct amdgpu_device *adev)
437{
438 /* doorbell bar mapping */
439 adev->doorbell.base = pci_resource_start(adev->pdev, 2);
440 adev->doorbell.size = pci_resource_len(adev->pdev, 2);
441
Christian Königedf600d2016-05-03 15:54:54 +0200442 adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400443 AMDGPU_DOORBELL_MAX_ASSIGNMENT+1);
444 if (adev->doorbell.num_doorbells == 0)
445 return -EINVAL;
446
Christian König8972e5d2017-03-06 13:34:57 +0100447 adev->doorbell.ptr = ioremap(adev->doorbell.base,
448 adev->doorbell.num_doorbells *
449 sizeof(u32));
450 if (adev->doorbell.ptr == NULL)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400451 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400452
453 return 0;
454}
455
456/**
457 * amdgpu_doorbell_fini - Tear down doorbell driver information.
458 *
459 * @adev: amdgpu_device pointer
460 *
461 * Tear down doorbell driver information (CIK)
462 */
463static void amdgpu_doorbell_fini(struct amdgpu_device *adev)
464{
465 iounmap(adev->doorbell.ptr);
466 adev->doorbell.ptr = NULL;
467}
468
469/**
470 * amdgpu_doorbell_get_kfd_info - Report doorbell configuration required to
471 * setup amdkfd
472 *
473 * @adev: amdgpu_device pointer
474 * @aperture_base: output returning doorbell aperture base physical address
475 * @aperture_size: output returning doorbell aperture size in bytes
476 * @start_offset: output returning # of doorbell bytes reserved for amdgpu.
477 *
478 * amdgpu and amdkfd share the doorbell aperture. amdgpu sets it up,
479 * takes doorbells required for its own rings and reports the setup to amdkfd.
480 * amdgpu reserved doorbells are at the start of the doorbell aperture.
481 */
482void amdgpu_doorbell_get_kfd_info(struct amdgpu_device *adev,
483 phys_addr_t *aperture_base,
484 size_t *aperture_size,
485 size_t *start_offset)
486{
487 /*
488 * The first num_doorbells are used by amdgpu.
489 * amdkfd takes whatever's left in the aperture.
490 */
491 if (adev->doorbell.size > adev->doorbell.num_doorbells * sizeof(u32)) {
492 *aperture_base = adev->doorbell.base;
493 *aperture_size = adev->doorbell.size;
494 *start_offset = adev->doorbell.num_doorbells * sizeof(u32);
495 } else {
496 *aperture_base = 0;
497 *aperture_size = 0;
498 *start_offset = 0;
499 }
500}
501
502/*
503 * amdgpu_wb_*()
Alex Xie455a7bc2017-05-08 21:36:03 -0400504 * Writeback is the method by which the GPU updates special pages in memory
Alex Xieea81a172017-05-08 13:41:11 -0400505 * with the status of certain GPU events (fences, ring pointers,etc.).
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400506 */
507
508/**
509 * amdgpu_wb_fini - Disable Writeback and free memory
510 *
511 * @adev: amdgpu_device pointer
512 *
513 * Disables Writeback and frees the Writeback memory (all asics).
514 * Used at driver shutdown.
515 */
516static void amdgpu_wb_fini(struct amdgpu_device *adev)
517{
518 if (adev->wb.wb_obj) {
Alex Deuchera76ed482016-10-21 15:30:36 -0400519 amdgpu_bo_free_kernel(&adev->wb.wb_obj,
520 &adev->wb.gpu_addr,
521 (void **)&adev->wb.wb);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400522 adev->wb.wb_obj = NULL;
523 }
524}
525
526/**
527 * amdgpu_wb_init- Init Writeback driver info and allocate memory
528 *
529 * @adev: amdgpu_device pointer
530 *
Alex Xie455a7bc2017-05-08 21:36:03 -0400531 * Initializes writeback and allocates writeback memory (all asics).
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400532 * Used at driver startup.
533 * Returns 0 on success or an -error on failure.
534 */
535static int amdgpu_wb_init(struct amdgpu_device *adev)
536{
537 int r;
538
539 if (adev->wb.wb_obj == NULL) {
Huang Rui60a970a62017-03-15 10:13:32 +0800540 r = amdgpu_bo_create_kernel(adev, AMDGPU_MAX_WB * sizeof(uint32_t),
Alex Deuchera76ed482016-10-21 15:30:36 -0400541 PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
542 &adev->wb.wb_obj, &adev->wb.gpu_addr,
543 (void **)&adev->wb.wb);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400544 if (r) {
545 dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
546 return r;
547 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400548
549 adev->wb.num_wb = AMDGPU_MAX_WB;
550 memset(&adev->wb.used, 0, sizeof(adev->wb.used));
551
552 /* clear wb memory */
Huang Rui60a970a62017-03-15 10:13:32 +0800553 memset((char *)adev->wb.wb, 0, AMDGPU_MAX_WB * sizeof(uint32_t));
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400554 }
555
556 return 0;
557}
558
559/**
560 * amdgpu_wb_get - Allocate a wb entry
561 *
562 * @adev: amdgpu_device pointer
563 * @wb: wb index
564 *
565 * Allocate a wb slot for use by the driver (all asics).
566 * Returns 0 on success or -EINVAL on failure.
567 */
568int amdgpu_wb_get(struct amdgpu_device *adev, u32 *wb)
569{
570 unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
571 if (offset < adev->wb.num_wb) {
572 __set_bit(offset, adev->wb.used);
573 *wb = offset;
574 return 0;
575 } else {
576 return -EINVAL;
577 }
578}
579
580/**
Ken Wang70142852016-03-18 15:08:49 +0800581 * amdgpu_wb_get_64bit - Allocate a wb entry
582 *
583 * @adev: amdgpu_device pointer
584 * @wb: wb index
585 *
586 * Allocate a wb slot for use by the driver (all asics).
587 * Returns 0 on success or -EINVAL on failure.
588 */
589int amdgpu_wb_get_64bit(struct amdgpu_device *adev, u32 *wb)
590{
591 unsigned long offset = bitmap_find_next_zero_area_off(adev->wb.used,
592 adev->wb.num_wb, 0, 2, 7, 0);
593 if ((offset + 1) < adev->wb.num_wb) {
594 __set_bit(offset, adev->wb.used);
595 __set_bit(offset + 1, adev->wb.used);
596 *wb = offset;
597 return 0;
598 } else {
599 return -EINVAL;
600 }
601}
602
603/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400604 * amdgpu_wb_free - Free a wb entry
605 *
606 * @adev: amdgpu_device pointer
607 * @wb: wb index
608 *
609 * Free a wb slot allocated for use by the driver (all asics)
610 */
611void amdgpu_wb_free(struct amdgpu_device *adev, u32 wb)
612{
613 if (wb < adev->wb.num_wb)
614 __clear_bit(wb, adev->wb.used);
615}
616
617/**
Ken Wang70142852016-03-18 15:08:49 +0800618 * amdgpu_wb_free_64bit - Free a wb entry
619 *
620 * @adev: amdgpu_device pointer
621 * @wb: wb index
622 *
623 * Free a wb slot allocated for use by the driver (all asics)
624 */
625void amdgpu_wb_free_64bit(struct amdgpu_device *adev, u32 wb)
626{
627 if ((wb + 1) < adev->wb.num_wb) {
628 __clear_bit(wb, adev->wb.used);
629 __clear_bit(wb + 1, adev->wb.used);
630 }
631}
632
633/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400634 * amdgpu_vram_location - try to find VRAM location
635 * @adev: amdgpu device structure holding all necessary informations
636 * @mc: memory controller structure holding memory informations
637 * @base: base address at which to put VRAM
638 *
Alex Xie455a7bc2017-05-08 21:36:03 -0400639 * Function will try to place VRAM at base address provided
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400640 * as parameter (which is so far either PCI aperture address or
641 * for IGP TOM base address).
642 *
643 * If there is not enough space to fit the unvisible VRAM in the 32bits
644 * address space then we limit the VRAM size to the aperture.
645 *
646 * Note: We don't explicitly enforce VRAM start to be aligned on VRAM size,
647 * this shouldn't be a problem as we are using the PCI aperture as a reference.
648 * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but
649 * not IGP.
650 *
651 * Note: we use mc_vram_size as on some board we need to program the mc to
652 * cover the whole aperture even if VRAM size is inferior to aperture size
653 * Novell bug 204882 + along with lots of ubuntu ones
654 *
655 * Note: when limiting vram it's safe to overwritte real_vram_size because
656 * we are not in case where real_vram_size is inferior to mc_vram_size (ie
657 * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu
658 * ones)
659 *
660 * Note: IGP TOM addr should be the same as the aperture addr, we don't
Alex Xie455a7bc2017-05-08 21:36:03 -0400661 * explicitly check for that though.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400662 *
663 * FIXME: when reducing VRAM size align new size on power of 2.
664 */
665void amdgpu_vram_location(struct amdgpu_device *adev, struct amdgpu_mc *mc, u64 base)
666{
667 uint64_t limit = (uint64_t)amdgpu_vram_limit << 20;
668
669 mc->vram_start = base;
670 if (mc->mc_vram_size > (adev->mc.mc_mask - base + 1)) {
671 dev_warn(adev->dev, "limiting VRAM to PCI aperture size\n");
672 mc->real_vram_size = mc->aper_size;
673 mc->mc_vram_size = mc->aper_size;
674 }
675 mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
676 if (limit && limit < mc->real_vram_size)
677 mc->real_vram_size = limit;
678 dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
679 mc->mc_vram_size >> 20, mc->vram_start,
680 mc->vram_end, mc->real_vram_size >> 20);
681}
682
683/**
Christian König6f02a692017-07-07 11:56:59 +0200684 * amdgpu_gart_location - try to find GTT location
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400685 * @adev: amdgpu device structure holding all necessary informations
686 * @mc: memory controller structure holding memory informations
687 *
688 * Function will place try to place GTT before or after VRAM.
689 *
690 * If GTT size is bigger than space left then we ajust GTT size.
691 * Thus function will never fails.
692 *
693 * FIXME: when reducing GTT size align new size on power of 2.
694 */
Christian König6f02a692017-07-07 11:56:59 +0200695void amdgpu_gart_location(struct amdgpu_device *adev, struct amdgpu_mc *mc)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400696{
697 u64 size_af, size_bf;
698
Christian Königed21c042017-07-06 22:26:05 +0200699 size_af = adev->mc.mc_mask - mc->vram_end;
700 size_bf = mc->vram_start;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400701 if (size_bf > size_af) {
Christian König6f02a692017-07-07 11:56:59 +0200702 if (mc->gart_size > size_bf) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400703 dev_warn(adev->dev, "limiting GTT\n");
Christian König6f02a692017-07-07 11:56:59 +0200704 mc->gart_size = size_bf;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400705 }
Christian König6f02a692017-07-07 11:56:59 +0200706 mc->gart_start = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400707 } else {
Christian König6f02a692017-07-07 11:56:59 +0200708 if (mc->gart_size > size_af) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400709 dev_warn(adev->dev, "limiting GTT\n");
Christian König6f02a692017-07-07 11:56:59 +0200710 mc->gart_size = size_af;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400711 }
Christian König6f02a692017-07-07 11:56:59 +0200712 mc->gart_start = mc->vram_end + 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400713 }
Christian König6f02a692017-07-07 11:56:59 +0200714 mc->gart_end = mc->gart_start + mc->gart_size - 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400715 dev_info(adev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
Christian König6f02a692017-07-07 11:56:59 +0200716 mc->gart_size >> 20, mc->gart_start, mc->gart_end);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400717}
718
719/*
720 * GPU helpers function.
721 */
722/**
Jim Quc836fec2017-02-10 15:59:59 +0800723 * amdgpu_need_post - check if the hw need post or not
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400724 *
725 * @adev: amdgpu_device pointer
726 *
Jim Quc836fec2017-02-10 15:59:59 +0800727 * Check if the asic has been initialized (all asics) at driver startup
728 * or post is needed if hw reset is performed.
729 * Returns true if need or false if not.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400730 */
Jim Quc836fec2017-02-10 15:59:59 +0800731bool amdgpu_need_post(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400732{
733 uint32_t reg;
734
Jim Quc836fec2017-02-10 15:59:59 +0800735 if (adev->has_hw_reset) {
736 adev->has_hw_reset = false;
737 return true;
738 }
Alex Deucher70d17a22017-06-30 17:26:47 -0400739
740 /* bios scratch used on CIK+ */
741 if (adev->asic_type >= CHIP_BONAIRE)
742 return amdgpu_atombios_scratch_need_asic_init(adev);
743
744 /* check MEM_SIZE for older asics */
Alex Deucherbbf282d2017-03-03 17:26:10 -0500745 reg = amdgpu_asic_get_config_memsize(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400746
Alex Deucherf2713e82017-03-28 12:19:31 -0400747 if ((reg != 0) && (reg != 0xffffffff))
Jim Quc836fec2017-02-10 15:59:59 +0800748 return false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400749
Jim Quc836fec2017-02-10 15:59:59 +0800750 return true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400751
752}
753
Monk Liubec86372016-09-14 19:38:08 +0800754static bool amdgpu_vpost_needed(struct amdgpu_device *adev)
755{
756 if (amdgpu_sriov_vf(adev))
757 return false;
758
759 if (amdgpu_passthrough(adev)) {
Monk Liu1da2c322016-11-11 11:24:29 +0800760 /* for FIJI: In whole GPU pass-through virtualization case, after VM reboot
761 * some old smc fw still need driver do vPost otherwise gpu hang, while
762 * those smc fw version above 22.15 doesn't have this flaw, so we force
763 * vpost executed for smc version below 22.15
Monk Liubec86372016-09-14 19:38:08 +0800764 */
765 if (adev->asic_type == CHIP_FIJI) {
766 int err;
767 uint32_t fw_ver;
768 err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
769 /* force vPost if error occured */
770 if (err)
771 return true;
772
773 fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
Monk Liu1da2c322016-11-11 11:24:29 +0800774 if (fw_ver < 0x00160e00)
775 return true;
Monk Liubec86372016-09-14 19:38:08 +0800776 }
Monk Liubec86372016-09-14 19:38:08 +0800777 }
Jim Quc836fec2017-02-10 15:59:59 +0800778 return amdgpu_need_post(adev);
Monk Liubec86372016-09-14 19:38:08 +0800779}
780
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400781/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400782 * amdgpu_dummy_page_init - init dummy page used by the driver
783 *
784 * @adev: amdgpu_device pointer
785 *
786 * Allocate the dummy page used by the driver (all asics).
787 * This dummy page is used by the driver as a filler for gart entries
788 * when pages are taken out of the GART
789 * Returns 0 on sucess, -ENOMEM on failure.
790 */
791int amdgpu_dummy_page_init(struct amdgpu_device *adev)
792{
793 if (adev->dummy_page.page)
794 return 0;
795 adev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
796 if (adev->dummy_page.page == NULL)
797 return -ENOMEM;
798 adev->dummy_page.addr = pci_map_page(adev->pdev, adev->dummy_page.page,
799 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
800 if (pci_dma_mapping_error(adev->pdev, adev->dummy_page.addr)) {
801 dev_err(&adev->pdev->dev, "Failed to DMA MAP the dummy page\n");
802 __free_page(adev->dummy_page.page);
803 adev->dummy_page.page = NULL;
804 return -ENOMEM;
805 }
806 return 0;
807}
808
809/**
810 * amdgpu_dummy_page_fini - free dummy page used by the driver
811 *
812 * @adev: amdgpu_device pointer
813 *
814 * Frees the dummy page used by the driver (all asics).
815 */
816void amdgpu_dummy_page_fini(struct amdgpu_device *adev)
817{
818 if (adev->dummy_page.page == NULL)
819 return;
820 pci_unmap_page(adev->pdev, adev->dummy_page.addr,
821 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
822 __free_page(adev->dummy_page.page);
823 adev->dummy_page.page = NULL;
824}
825
826
827/* ATOM accessor methods */
828/*
829 * ATOM is an interpreted byte code stored in tables in the vbios. The
830 * driver registers callbacks to access registers and the interpreter
831 * in the driver parses the tables and executes then to program specific
832 * actions (set display modes, asic init, etc.). See amdgpu_atombios.c,
833 * atombios.h, and atom.c
834 */
835
836/**
837 * cail_pll_read - read PLL register
838 *
839 * @info: atom card_info pointer
840 * @reg: PLL register offset
841 *
842 * Provides a PLL register accessor for the atom interpreter (r4xx+).
843 * Returns the value of the PLL register.
844 */
845static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
846{
847 return 0;
848}
849
850/**
851 * cail_pll_write - write PLL register
852 *
853 * @info: atom card_info pointer
854 * @reg: PLL register offset
855 * @val: value to write to the pll register
856 *
857 * Provides a PLL register accessor for the atom interpreter (r4xx+).
858 */
859static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
860{
861
862}
863
864/**
865 * cail_mc_read - read MC (Memory Controller) register
866 *
867 * @info: atom card_info pointer
868 * @reg: MC register offset
869 *
870 * Provides an MC register accessor for the atom interpreter (r4xx+).
871 * Returns the value of the MC register.
872 */
873static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
874{
875 return 0;
876}
877
878/**
879 * cail_mc_write - write MC (Memory Controller) register
880 *
881 * @info: atom card_info pointer
882 * @reg: MC register offset
883 * @val: value to write to the pll register
884 *
885 * Provides a MC register accessor for the atom interpreter (r4xx+).
886 */
887static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
888{
889
890}
891
892/**
893 * cail_reg_write - write MMIO register
894 *
895 * @info: atom card_info pointer
896 * @reg: MMIO register offset
897 * @val: value to write to the pll register
898 *
899 * Provides a MMIO register accessor for the atom interpreter (r4xx+).
900 */
901static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
902{
903 struct amdgpu_device *adev = info->dev->dev_private;
904
905 WREG32(reg, val);
906}
907
908/**
909 * cail_reg_read - read MMIO register
910 *
911 * @info: atom card_info pointer
912 * @reg: MMIO register offset
913 *
914 * Provides an MMIO register accessor for the atom interpreter (r4xx+).
915 * Returns the value of the MMIO register.
916 */
917static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
918{
919 struct amdgpu_device *adev = info->dev->dev_private;
920 uint32_t r;
921
922 r = RREG32(reg);
923 return r;
924}
925
926/**
927 * cail_ioreg_write - write IO register
928 *
929 * @info: atom card_info pointer
930 * @reg: IO register offset
931 * @val: value to write to the pll register
932 *
933 * Provides a IO register accessor for the atom interpreter (r4xx+).
934 */
935static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
936{
937 struct amdgpu_device *adev = info->dev->dev_private;
938
939 WREG32_IO(reg, val);
940}
941
942/**
943 * cail_ioreg_read - read IO register
944 *
945 * @info: atom card_info pointer
946 * @reg: IO register offset
947 *
948 * Provides an IO register accessor for the atom interpreter (r4xx+).
949 * Returns the value of the IO register.
950 */
951static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
952{
953 struct amdgpu_device *adev = info->dev->dev_private;
954 uint32_t r;
955
956 r = RREG32_IO(reg);
957 return r;
958}
959
960/**
961 * amdgpu_atombios_fini - free the driver info and callbacks for atombios
962 *
963 * @adev: amdgpu_device pointer
964 *
965 * Frees the driver info and register access callbacks for the ATOM
966 * interpreter (r4xx+).
967 * Called at driver shutdown.
968 */
969static void amdgpu_atombios_fini(struct amdgpu_device *adev)
970{
Monk Liu89e0ec9f2016-05-27 19:34:11 +0800971 if (adev->mode_info.atom_context) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400972 kfree(adev->mode_info.atom_context->scratch);
Monk Liu89e0ec9f2016-05-27 19:34:11 +0800973 kfree(adev->mode_info.atom_context->iio);
974 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400975 kfree(adev->mode_info.atom_context);
976 adev->mode_info.atom_context = NULL;
977 kfree(adev->mode_info.atom_card_info);
978 adev->mode_info.atom_card_info = NULL;
979}
980
981/**
982 * amdgpu_atombios_init - init the driver info and callbacks for atombios
983 *
984 * @adev: amdgpu_device pointer
985 *
986 * Initializes the driver info and register access callbacks for the
987 * ATOM interpreter (r4xx+).
988 * Returns 0 on sucess, -ENOMEM on failure.
989 * Called at driver startup.
990 */
991static int amdgpu_atombios_init(struct amdgpu_device *adev)
992{
993 struct card_info *atom_card_info =
994 kzalloc(sizeof(struct card_info), GFP_KERNEL);
995
996 if (!atom_card_info)
997 return -ENOMEM;
998
999 adev->mode_info.atom_card_info = atom_card_info;
1000 atom_card_info->dev = adev->ddev;
1001 atom_card_info->reg_read = cail_reg_read;
1002 atom_card_info->reg_write = cail_reg_write;
1003 /* needed for iio ops */
1004 if (adev->rio_mem) {
1005 atom_card_info->ioreg_read = cail_ioreg_read;
1006 atom_card_info->ioreg_write = cail_ioreg_write;
1007 } else {
Amber Linb64a18c2017-01-04 08:06:58 -05001008 DRM_INFO("PCI I/O BAR is not found. Using MMIO to access ATOM BIOS\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001009 atom_card_info->ioreg_read = cail_reg_read;
1010 atom_card_info->ioreg_write = cail_reg_write;
1011 }
1012 atom_card_info->mc_read = cail_mc_read;
1013 atom_card_info->mc_write = cail_mc_write;
1014 atom_card_info->pll_read = cail_pll_read;
1015 atom_card_info->pll_write = cail_pll_write;
1016
1017 adev->mode_info.atom_context = amdgpu_atom_parse(atom_card_info, adev->bios);
1018 if (!adev->mode_info.atom_context) {
1019 amdgpu_atombios_fini(adev);
1020 return -ENOMEM;
1021 }
1022
1023 mutex_init(&adev->mode_info.atom_context->mutex);
Alex Deuchera5bde2f2016-09-23 16:23:41 -04001024 if (adev->is_atom_fw) {
1025 amdgpu_atomfirmware_scratch_regs_init(adev);
1026 amdgpu_atomfirmware_allocate_fb_scratch(adev);
1027 } else {
1028 amdgpu_atombios_scratch_regs_init(adev);
1029 amdgpu_atombios_allocate_fb_scratch(adev);
1030 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001031 return 0;
1032}
1033
1034/* if we get transitioned to only one device, take VGA back */
1035/**
1036 * amdgpu_vga_set_decode - enable/disable vga decode
1037 *
1038 * @cookie: amdgpu_device pointer
1039 * @state: enable/disable vga decode
1040 *
1041 * Enable/disable vga decode (all asics).
1042 * Returns VGA resource flags.
1043 */
1044static unsigned int amdgpu_vga_set_decode(void *cookie, bool state)
1045{
1046 struct amdgpu_device *adev = cookie;
1047 amdgpu_asic_set_vga_state(adev, state);
1048 if (state)
1049 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
1050 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1051 else
1052 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1053}
1054
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001055static void amdgpu_check_block_size(struct amdgpu_device *adev)
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001056{
1057 /* defines number of bits in page table versus page directory,
1058 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
1059 * page table and the remaining bits are in the page directory */
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001060 if (amdgpu_vm_block_size == -1)
1061 return;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001062
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001063 if (amdgpu_vm_block_size < 9) {
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001064 dev_warn(adev->dev, "VM page table size (%d) too small\n",
1065 amdgpu_vm_block_size);
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001066 goto def_value;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001067 }
1068
1069 if (amdgpu_vm_block_size > 24 ||
1070 (amdgpu_vm_size * 1024) < (1ull << amdgpu_vm_block_size)) {
1071 dev_warn(adev->dev, "VM page table size (%d) too large\n",
1072 amdgpu_vm_block_size);
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001073 goto def_value;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001074 }
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001075
1076 return;
1077
1078def_value:
1079 amdgpu_vm_block_size = -1;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001080}
1081
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001082static void amdgpu_check_vm_size(struct amdgpu_device *adev)
1083{
Alex Deucher64dab072017-06-15 18:20:09 -04001084 /* no need to check the default value */
1085 if (amdgpu_vm_size == -1)
1086 return;
1087
Alex Deucher76117502017-06-21 12:31:41 -04001088 if (!is_power_of_2(amdgpu_vm_size)) {
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001089 dev_warn(adev->dev, "VM size (%d) must be a power of 2\n",
1090 amdgpu_vm_size);
1091 goto def_value;
1092 }
1093
1094 if (amdgpu_vm_size < 1) {
1095 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
1096 amdgpu_vm_size);
1097 goto def_value;
1098 }
1099
1100 /*
1101 * Max GPUVM size for Cayman, SI, CI VI are 40 bits.
1102 */
1103 if (amdgpu_vm_size > 1024) {
1104 dev_warn(adev->dev, "VM size (%d) too large, max is 1TB\n",
1105 amdgpu_vm_size);
1106 goto def_value;
1107 }
1108
1109 return;
1110
1111def_value:
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001112 amdgpu_vm_size = -1;
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001113}
1114
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001115/**
1116 * amdgpu_check_arguments - validate module params
1117 *
1118 * @adev: amdgpu_device pointer
1119 *
1120 * Validates certain module parameters and updates
1121 * the associated values used by the driver (all asics).
1122 */
1123static void amdgpu_check_arguments(struct amdgpu_device *adev)
1124{
Chunming Zhou5b011232015-12-10 17:34:33 +08001125 if (amdgpu_sched_jobs < 4) {
1126 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
1127 amdgpu_sched_jobs);
1128 amdgpu_sched_jobs = 4;
Alex Deucher76117502017-06-21 12:31:41 -04001129 } else if (!is_power_of_2(amdgpu_sched_jobs)){
Chunming Zhou5b011232015-12-10 17:34:33 +08001130 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
1131 amdgpu_sched_jobs);
1132 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
1133 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001134
Christian Königf9321cc2017-07-07 13:44:05 +02001135 if (amdgpu_gart_size < 32) {
1136 /* gart size must be greater or equal to 32M */
1137 dev_warn(adev->dev, "gart size (%d) too small\n",
1138 amdgpu_gart_size);
1139 amdgpu_gart_size = 32;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001140 }
1141
Christian König36d38372017-07-07 13:17:45 +02001142 if (amdgpu_gtt_size != -1 && amdgpu_gtt_size < 32) {
1143 /* gtt size must be greater or equal to 32M */
1144 dev_warn(adev->dev, "gtt size (%d) too small\n",
1145 amdgpu_gtt_size);
1146 amdgpu_gtt_size = -1;
1147 }
1148
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001149 amdgpu_check_vm_size(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001150
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001151 amdgpu_check_block_size(adev);
Christian König6a7f76e2016-08-24 15:51:49 +02001152
jimqu526bae32016-11-07 09:53:10 +08001153 if (amdgpu_vram_page_split != -1 && (amdgpu_vram_page_split < 16 ||
Alex Deucher76117502017-06-21 12:31:41 -04001154 !is_power_of_2(amdgpu_vram_page_split))) {
Christian König6a7f76e2016-08-24 15:51:49 +02001155 dev_warn(adev->dev, "invalid VRAM page split (%d)\n",
1156 amdgpu_vram_page_split);
1157 amdgpu_vram_page_split = 1024;
1158 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001159}
1160
1161/**
1162 * amdgpu_switcheroo_set_state - set switcheroo state
1163 *
1164 * @pdev: pci dev pointer
Lukas Wunner16944672015-09-05 11:17:35 +02001165 * @state: vga_switcheroo state
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001166 *
1167 * Callback for the switcheroo driver. Suspends or resumes the
1168 * the asics before or after it is powered up using ACPI methods.
1169 */
1170static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1171{
1172 struct drm_device *dev = pci_get_drvdata(pdev);
1173
1174 if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
1175 return;
1176
1177 if (state == VGA_SWITCHEROO_ON) {
1178 unsigned d3_delay = dev->pdev->d3_delay;
1179
Joe Perches7ca85292017-02-28 04:55:52 -08001180 pr_info("amdgpu: switched on\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001181 /* don't suspend or resume card normally */
1182 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1183
Alex Deucher810ddc32016-08-23 13:25:49 -04001184 amdgpu_device_resume(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001185
1186 dev->pdev->d3_delay = d3_delay;
1187
1188 dev->switch_power_state = DRM_SWITCH_POWER_ON;
1189 drm_kms_helper_poll_enable(dev);
1190 } else {
Joe Perches7ca85292017-02-28 04:55:52 -08001191 pr_info("amdgpu: switched off\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001192 drm_kms_helper_poll_disable(dev);
1193 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Alex Deucher810ddc32016-08-23 13:25:49 -04001194 amdgpu_device_suspend(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001195 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1196 }
1197}
1198
1199/**
1200 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1201 *
1202 * @pdev: pci dev pointer
1203 *
1204 * Callback for the switcheroo driver. Check of the switcheroo
1205 * state can be changed.
1206 * Returns true if the state can be changed, false if not.
1207 */
1208static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1209{
1210 struct drm_device *dev = pci_get_drvdata(pdev);
1211
1212 /*
1213 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1214 * locking inversion with the driver load path. And the access here is
1215 * completely racy anyway. So don't bother with locking for now.
1216 */
1217 return dev->open_count == 0;
1218}
1219
1220static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1221 .set_gpu_state = amdgpu_switcheroo_set_state,
1222 .reprobe = NULL,
1223 .can_switch = amdgpu_switcheroo_can_switch,
1224};
1225
1226int amdgpu_set_clockgating_state(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001227 enum amd_ip_block_type block_type,
1228 enum amd_clockgating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001229{
1230 int i, r = 0;
1231
1232 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001233 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001234 continue;
Rex Zhuc7228652017-02-22 15:33:46 +08001235 if (adev->ip_blocks[i].version->type != block_type)
1236 continue;
1237 if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
1238 continue;
1239 r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
1240 (void *)adev, state);
1241 if (r)
1242 DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
1243 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001244 }
1245 return r;
1246}
1247
1248int amdgpu_set_powergating_state(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001249 enum amd_ip_block_type block_type,
1250 enum amd_powergating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001251{
1252 int i, r = 0;
1253
1254 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001255 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001256 continue;
Rex Zhuc7228652017-02-22 15:33:46 +08001257 if (adev->ip_blocks[i].version->type != block_type)
1258 continue;
1259 if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
1260 continue;
1261 r = adev->ip_blocks[i].version->funcs->set_powergating_state(
1262 (void *)adev, state);
1263 if (r)
1264 DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
1265 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001266 }
1267 return r;
1268}
1269
Huang Rui6cb2d4e2017-01-05 18:44:41 +08001270void amdgpu_get_clockgating_state(struct amdgpu_device *adev, u32 *flags)
1271{
1272 int i;
1273
1274 for (i = 0; i < adev->num_ip_blocks; i++) {
1275 if (!adev->ip_blocks[i].status.valid)
1276 continue;
1277 if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
1278 adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
1279 }
1280}
1281
Alex Deucher5dbbb602016-06-23 11:41:04 -04001282int amdgpu_wait_for_idle(struct amdgpu_device *adev,
1283 enum amd_ip_block_type block_type)
1284{
1285 int i, r;
1286
1287 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001288 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001289 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001290 if (adev->ip_blocks[i].version->type == block_type) {
1291 r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -04001292 if (r)
1293 return r;
1294 break;
1295 }
1296 }
1297 return 0;
1298
1299}
1300
1301bool amdgpu_is_idle(struct amdgpu_device *adev,
1302 enum amd_ip_block_type block_type)
1303{
1304 int i;
1305
1306 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001307 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001308 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001309 if (adev->ip_blocks[i].version->type == block_type)
1310 return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -04001311 }
1312 return true;
1313
1314}
1315
Alex Deuchera1255102016-10-13 17:41:13 -04001316struct amdgpu_ip_block * amdgpu_get_ip_block(struct amdgpu_device *adev,
1317 enum amd_ip_block_type type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001318{
1319 int i;
1320
1321 for (i = 0; i < adev->num_ip_blocks; i++)
Alex Deuchera1255102016-10-13 17:41:13 -04001322 if (adev->ip_blocks[i].version->type == type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001323 return &adev->ip_blocks[i];
1324
1325 return NULL;
1326}
1327
1328/**
1329 * amdgpu_ip_block_version_cmp
1330 *
1331 * @adev: amdgpu_device pointer
yanyang15fc3aee2015-05-22 14:39:35 -04001332 * @type: enum amd_ip_block_type
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001333 * @major: major version
1334 * @minor: minor version
1335 *
1336 * return 0 if equal or greater
1337 * return 1 if smaller or the ip_block doesn't exist
1338 */
1339int amdgpu_ip_block_version_cmp(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001340 enum amd_ip_block_type type,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001341 u32 major, u32 minor)
1342{
Alex Deuchera1255102016-10-13 17:41:13 -04001343 struct amdgpu_ip_block *ip_block = amdgpu_get_ip_block(adev, type);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001344
Alex Deuchera1255102016-10-13 17:41:13 -04001345 if (ip_block && ((ip_block->version->major > major) ||
1346 ((ip_block->version->major == major) &&
1347 (ip_block->version->minor >= minor))))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001348 return 0;
1349
1350 return 1;
1351}
1352
Alex Deuchera1255102016-10-13 17:41:13 -04001353/**
1354 * amdgpu_ip_block_add
1355 *
1356 * @adev: amdgpu_device pointer
1357 * @ip_block_version: pointer to the IP to add
1358 *
1359 * Adds the IP block driver information to the collection of IPs
1360 * on the asic.
1361 */
1362int amdgpu_ip_block_add(struct amdgpu_device *adev,
1363 const struct amdgpu_ip_block_version *ip_block_version)
1364{
1365 if (!ip_block_version)
1366 return -EINVAL;
1367
Huang Ruia0bae352017-05-03 09:52:06 +08001368 DRM_DEBUG("add ip block number %d <%s>\n", adev->num_ip_blocks,
1369 ip_block_version->funcs->name);
1370
Alex Deuchera1255102016-10-13 17:41:13 -04001371 adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
1372
1373 return 0;
1374}
1375
Alex Deucher483ef982016-09-30 12:43:04 -04001376static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
Emily Deng9accf2f2016-08-10 16:01:25 +08001377{
1378 adev->enable_virtual_display = false;
1379
1380 if (amdgpu_virtual_display) {
1381 struct drm_device *ddev = adev->ddev;
1382 const char *pci_address_name = pci_name(ddev->pdev);
Emily Deng0f663562016-09-30 13:02:18 -04001383 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
Emily Deng9accf2f2016-08-10 16:01:25 +08001384
1385 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1386 pciaddstr_tmp = pciaddstr;
Emily Deng0f663562016-09-30 13:02:18 -04001387 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1388 pciaddname = strsep(&pciaddname_tmp, ",");
Yintian Tao967de2a2017-01-22 15:16:51 +08001389 if (!strcmp("all", pciaddname)
1390 || !strcmp(pci_address_name, pciaddname)) {
Emily Deng0f663562016-09-30 13:02:18 -04001391 long num_crtc;
1392 int res = -1;
1393
Emily Deng9accf2f2016-08-10 16:01:25 +08001394 adev->enable_virtual_display = true;
Emily Deng0f663562016-09-30 13:02:18 -04001395
1396 if (pciaddname_tmp)
1397 res = kstrtol(pciaddname_tmp, 10,
1398 &num_crtc);
1399
1400 if (!res) {
1401 if (num_crtc < 1)
1402 num_crtc = 1;
1403 if (num_crtc > 6)
1404 num_crtc = 6;
1405 adev->mode_info.num_crtc = num_crtc;
1406 } else {
1407 adev->mode_info.num_crtc = 1;
1408 }
Emily Deng9accf2f2016-08-10 16:01:25 +08001409 break;
1410 }
1411 }
1412
Emily Deng0f663562016-09-30 13:02:18 -04001413 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
1414 amdgpu_virtual_display, pci_address_name,
1415 adev->enable_virtual_display, adev->mode_info.num_crtc);
Emily Deng9accf2f2016-08-10 16:01:25 +08001416
1417 kfree(pciaddstr);
1418 }
1419}
1420
Alex Deuchere2a75f82017-04-27 16:58:01 -04001421static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev)
1422{
Alex Deuchere2a75f82017-04-27 16:58:01 -04001423 const char *chip_name;
1424 char fw_name[30];
1425 int err;
1426 const struct gpu_info_firmware_header_v1_0 *hdr;
1427
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001428 adev->firmware.gpu_info_fw = NULL;
1429
Alex Deuchere2a75f82017-04-27 16:58:01 -04001430 switch (adev->asic_type) {
1431 case CHIP_TOPAZ:
1432 case CHIP_TONGA:
1433 case CHIP_FIJI:
1434 case CHIP_POLARIS11:
1435 case CHIP_POLARIS10:
1436 case CHIP_POLARIS12:
1437 case CHIP_CARRIZO:
1438 case CHIP_STONEY:
1439#ifdef CONFIG_DRM_AMDGPU_SI
1440 case CHIP_VERDE:
1441 case CHIP_TAHITI:
1442 case CHIP_PITCAIRN:
1443 case CHIP_OLAND:
1444 case CHIP_HAINAN:
1445#endif
1446#ifdef CONFIG_DRM_AMDGPU_CIK
1447 case CHIP_BONAIRE:
1448 case CHIP_HAWAII:
1449 case CHIP_KAVERI:
1450 case CHIP_KABINI:
1451 case CHIP_MULLINS:
1452#endif
1453 default:
1454 return 0;
1455 case CHIP_VEGA10:
1456 chip_name = "vega10";
1457 break;
Alex Deucher2d2e5e72017-05-09 12:27:35 -04001458 case CHIP_RAVEN:
1459 chip_name = "raven";
1460 break;
Alex Deuchere2a75f82017-04-27 16:58:01 -04001461 }
1462
1463 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name);
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001464 err = request_firmware(&adev->firmware.gpu_info_fw, fw_name, adev->dev);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001465 if (err) {
1466 dev_err(adev->dev,
1467 "Failed to load gpu_info firmware \"%s\"\n",
1468 fw_name);
1469 goto out;
1470 }
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001471 err = amdgpu_ucode_validate(adev->firmware.gpu_info_fw);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001472 if (err) {
1473 dev_err(adev->dev,
1474 "Failed to validate gpu_info firmware \"%s\"\n",
1475 fw_name);
1476 goto out;
1477 }
1478
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001479 hdr = (const struct gpu_info_firmware_header_v1_0 *)adev->firmware.gpu_info_fw->data;
Alex Deuchere2a75f82017-04-27 16:58:01 -04001480 amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
1481
1482 switch (hdr->version_major) {
1483 case 1:
1484 {
1485 const struct gpu_info_firmware_v1_0 *gpu_info_fw =
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001486 (const struct gpu_info_firmware_v1_0 *)(adev->firmware.gpu_info_fw->data +
Alex Deuchere2a75f82017-04-27 16:58:01 -04001487 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1488
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001489 adev->gfx.config.max_shader_engines = le32_to_cpu(gpu_info_fw->gc_num_se);
1490 adev->gfx.config.max_cu_per_sh = le32_to_cpu(gpu_info_fw->gc_num_cu_per_sh);
1491 adev->gfx.config.max_sh_per_se = le32_to_cpu(gpu_info_fw->gc_num_sh_per_se);
1492 adev->gfx.config.max_backends_per_se = le32_to_cpu(gpu_info_fw->gc_num_rb_per_se);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001493 adev->gfx.config.max_texture_channel_caches =
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001494 le32_to_cpu(gpu_info_fw->gc_num_tccs);
1495 adev->gfx.config.max_gprs = le32_to_cpu(gpu_info_fw->gc_num_gprs);
1496 adev->gfx.config.max_gs_threads = le32_to_cpu(gpu_info_fw->gc_num_max_gs_thds);
1497 adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gpu_info_fw->gc_gs_table_depth);
1498 adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gpu_info_fw->gc_gsprim_buff_depth);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001499 adev->gfx.config.double_offchip_lds_buf =
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001500 le32_to_cpu(gpu_info_fw->gc_double_offchip_lds_buffer);
1501 adev->gfx.cu_info.wave_front_size = le32_to_cpu(gpu_info_fw->gc_wave_size);
Hawking Zhang51fd0372017-06-09 22:30:52 +08001502 adev->gfx.cu_info.max_waves_per_simd =
1503 le32_to_cpu(gpu_info_fw->gc_max_waves_per_simd);
1504 adev->gfx.cu_info.max_scratch_slots_per_cu =
1505 le32_to_cpu(gpu_info_fw->gc_max_scratch_slots_per_cu);
1506 adev->gfx.cu_info.lds_size = le32_to_cpu(gpu_info_fw->gc_lds_size);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001507 break;
1508 }
1509 default:
1510 dev_err(adev->dev,
1511 "Unsupported gpu_info table %d\n", hdr->header.ucode_version);
1512 err = -EINVAL;
1513 goto out;
1514 }
1515out:
Alex Deuchere2a75f82017-04-27 16:58:01 -04001516 return err;
1517}
1518
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001519static int amdgpu_early_init(struct amdgpu_device *adev)
1520{
Alex Deucheraaa36a92015-04-20 17:31:14 -04001521 int i, r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001522
Alex Deucher483ef982016-09-30 12:43:04 -04001523 amdgpu_device_enable_virtual_display(adev);
Emily Denga6be7572016-08-08 11:37:50 +08001524
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001525 switch (adev->asic_type) {
Alex Deucheraaa36a92015-04-20 17:31:14 -04001526 case CHIP_TOPAZ:
1527 case CHIP_TONGA:
David Zhang48299f92015-07-08 01:05:16 +08001528 case CHIP_FIJI:
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001529 case CHIP_POLARIS11:
1530 case CHIP_POLARIS10:
Junwei Zhangc4642a42016-12-14 15:32:28 -05001531 case CHIP_POLARIS12:
Alex Deucheraaa36a92015-04-20 17:31:14 -04001532 case CHIP_CARRIZO:
Samuel Li39bb0c92015-10-08 16:31:43 -04001533 case CHIP_STONEY:
1534 if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
Alex Deucheraaa36a92015-04-20 17:31:14 -04001535 adev->family = AMDGPU_FAMILY_CZ;
1536 else
1537 adev->family = AMDGPU_FAMILY_VI;
1538
1539 r = vi_set_ip_blocks(adev);
1540 if (r)
1541 return r;
1542 break;
Ken Wang33f34802016-01-21 17:29:41 +08001543#ifdef CONFIG_DRM_AMDGPU_SI
1544 case CHIP_VERDE:
1545 case CHIP_TAHITI:
1546 case CHIP_PITCAIRN:
1547 case CHIP_OLAND:
1548 case CHIP_HAINAN:
Ken Wang295d0da2016-05-24 21:02:53 +08001549 adev->family = AMDGPU_FAMILY_SI;
Ken Wang33f34802016-01-21 17:29:41 +08001550 r = si_set_ip_blocks(adev);
1551 if (r)
1552 return r;
1553 break;
1554#endif
Alex Deuchera2e73f52015-04-20 17:09:27 -04001555#ifdef CONFIG_DRM_AMDGPU_CIK
1556 case CHIP_BONAIRE:
1557 case CHIP_HAWAII:
1558 case CHIP_KAVERI:
1559 case CHIP_KABINI:
1560 case CHIP_MULLINS:
1561 if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
1562 adev->family = AMDGPU_FAMILY_CI;
1563 else
1564 adev->family = AMDGPU_FAMILY_KV;
1565
1566 r = cik_set_ip_blocks(adev);
1567 if (r)
1568 return r;
1569 break;
1570#endif
Chunming Zhou2ca8a5d2016-12-07 17:31:19 +08001571 case CHIP_VEGA10:
1572 case CHIP_RAVEN:
1573 if (adev->asic_type == CHIP_RAVEN)
1574 adev->family = AMDGPU_FAMILY_RV;
1575 else
1576 adev->family = AMDGPU_FAMILY_AI;
Ken Wang460826e2017-03-06 14:53:16 -05001577
1578 r = soc15_set_ip_blocks(adev);
1579 if (r)
1580 return r;
1581 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001582 default:
1583 /* FIXME: not supported yet */
1584 return -EINVAL;
1585 }
1586
Alex Deuchere2a75f82017-04-27 16:58:01 -04001587 r = amdgpu_device_parse_gpu_info_fw(adev);
1588 if (r)
1589 return r;
1590
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001591 if (amdgpu_sriov_vf(adev)) {
1592 r = amdgpu_virt_request_full_gpu(adev, true);
1593 if (r)
1594 return r;
1595 }
1596
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001597 for (i = 0; i < adev->num_ip_blocks; i++) {
1598 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
Huang Ruied8cf002017-05-03 09:40:17 +08001599 DRM_ERROR("disabled ip block: %d <%s>\n",
1600 i, adev->ip_blocks[i].version->funcs->name);
Alex Deuchera1255102016-10-13 17:41:13 -04001601 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001602 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001603 if (adev->ip_blocks[i].version->funcs->early_init) {
1604 r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001605 if (r == -ENOENT) {
Alex Deuchera1255102016-10-13 17:41:13 -04001606 adev->ip_blocks[i].status.valid = false;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001607 } else if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001608 DRM_ERROR("early_init of IP block <%s> failed %d\n",
1609 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001610 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001611 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001612 adev->ip_blocks[i].status.valid = true;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001613 }
Alex Deucher974e6b62015-07-10 13:59:44 -04001614 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001615 adev->ip_blocks[i].status.valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001616 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001617 }
1618 }
1619
Nicolai Hähnle395d1fb2016-06-02 12:32:07 +02001620 adev->cg_flags &= amdgpu_cg_mask;
1621 adev->pg_flags &= amdgpu_pg_mask;
1622
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001623 return 0;
1624}
1625
1626static int amdgpu_init(struct amdgpu_device *adev)
1627{
1628 int i, r;
1629
1630 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001631 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001632 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001633 r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001634 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001635 DRM_ERROR("sw_init of IP block <%s> failed %d\n",
1636 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001637 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001638 }
Alex Deuchera1255102016-10-13 17:41:13 -04001639 adev->ip_blocks[i].status.sw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001640 /* need to do gmc hw init early so we can allocate gpu mem */
Alex Deuchera1255102016-10-13 17:41:13 -04001641 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001642 r = amdgpu_vram_scratch_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001643 if (r) {
1644 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001645 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001646 }
Alex Deuchera1255102016-10-13 17:41:13 -04001647 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001648 if (r) {
1649 DRM_ERROR("hw_init %d failed %d\n", i, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001650 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001651 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001652 r = amdgpu_wb_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001653 if (r) {
1654 DRM_ERROR("amdgpu_wb_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001655 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001656 }
Alex Deuchera1255102016-10-13 17:41:13 -04001657 adev->ip_blocks[i].status.hw = true;
Monk Liu24936642017-01-09 15:54:32 +08001658
1659 /* right after GMC hw init, we create CSA */
1660 if (amdgpu_sriov_vf(adev)) {
1661 r = amdgpu_allocate_static_csa(adev);
1662 if (r) {
1663 DRM_ERROR("allocate CSA failed %d\n", r);
1664 return r;
1665 }
1666 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001667 }
1668 }
1669
1670 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001671 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001672 continue;
1673 /* gmc hw init is done early */
Alex Deuchera1255102016-10-13 17:41:13 -04001674 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001675 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001676 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001677 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001678 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
1679 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001680 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001681 }
Alex Deuchera1255102016-10-13 17:41:13 -04001682 adev->ip_blocks[i].status.hw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001683 }
1684
1685 return 0;
1686}
1687
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08001688static void amdgpu_fill_reset_magic(struct amdgpu_device *adev)
1689{
1690 memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM);
1691}
1692
1693static bool amdgpu_check_vram_lost(struct amdgpu_device *adev)
1694{
1695 return !!memcmp(adev->gart.ptr, adev->reset_magic,
1696 AMDGPU_RESET_MAGIC_NUM);
1697}
1698
Shirish S2dc80b02017-05-25 10:05:25 +05301699static int amdgpu_late_set_cg_state(struct amdgpu_device *adev)
1700{
1701 int i = 0, r;
1702
1703 for (i = 0; i < adev->num_ip_blocks; i++) {
1704 if (!adev->ip_blocks[i].status.valid)
1705 continue;
1706 /* skip CG for VCE/UVD, it's handled specially */
1707 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1708 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1709 /* enable clockgating to save power */
1710 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1711 AMD_CG_STATE_GATE);
1712 if (r) {
1713 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
1714 adev->ip_blocks[i].version->funcs->name, r);
1715 return r;
1716 }
1717 }
1718 }
1719 return 0;
1720}
1721
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001722static int amdgpu_late_init(struct amdgpu_device *adev)
1723{
1724 int i = 0, r;
1725
1726 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001727 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001728 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001729 if (adev->ip_blocks[i].version->funcs->late_init) {
1730 r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001731 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001732 DRM_ERROR("late_init of IP block <%s> failed %d\n",
1733 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001734 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001735 }
Alex Deuchera1255102016-10-13 17:41:13 -04001736 adev->ip_blocks[i].status.late_initialized = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001737 }
1738 }
1739
Shirish S2dc80b02017-05-25 10:05:25 +05301740 mod_delayed_work(system_wq, &adev->late_init_work,
1741 msecs_to_jiffies(AMDGPU_RESUME_MS));
1742
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08001743 amdgpu_fill_reset_magic(adev);
1744
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001745 return 0;
1746}
1747
1748static int amdgpu_fini(struct amdgpu_device *adev)
1749{
1750 int i, r;
1751
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001752 /* need to disable SMC first */
1753 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001754 if (!adev->ip_blocks[i].status.hw)
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001755 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001756 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001757 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
Alex Deuchera1255102016-10-13 17:41:13 -04001758 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1759 AMD_CG_STATE_UNGATE);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001760 if (r) {
1761 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001762 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001763 return r;
1764 }
Alex Deuchera1255102016-10-13 17:41:13 -04001765 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001766 /* XXX handle errors */
1767 if (r) {
1768 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001769 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001770 }
Alex Deuchera1255102016-10-13 17:41:13 -04001771 adev->ip_blocks[i].status.hw = false;
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001772 break;
1773 }
1774 }
1775
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001776 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001777 if (!adev->ip_blocks[i].status.hw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001778 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001779 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001780 amdgpu_wb_fini(adev);
1781 amdgpu_vram_scratch_fini(adev);
1782 }
Rex Zhu8201a672016-11-24 21:44:44 +08001783
1784 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1785 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1786 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
1787 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1788 AMD_CG_STATE_UNGATE);
1789 if (r) {
1790 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1791 adev->ip_blocks[i].version->funcs->name, r);
1792 return r;
1793 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001794 }
Rex Zhu8201a672016-11-24 21:44:44 +08001795
Alex Deuchera1255102016-10-13 17:41:13 -04001796 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001797 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001798 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001799 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
1800 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001801 }
Rex Zhu8201a672016-11-24 21:44:44 +08001802
Alex Deuchera1255102016-10-13 17:41:13 -04001803 adev->ip_blocks[i].status.hw = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001804 }
1805
1806 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001807 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001808 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001809 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001810 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001811 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001812 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
1813 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001814 }
Alex Deuchera1255102016-10-13 17:41:13 -04001815 adev->ip_blocks[i].status.sw = false;
1816 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001817 }
1818
Monk Liua6dcfd92016-05-19 14:36:34 +08001819 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001820 if (!adev->ip_blocks[i].status.late_initialized)
Grazvydas Ignotas8a2eef12016-10-03 00:06:44 +03001821 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001822 if (adev->ip_blocks[i].version->funcs->late_fini)
1823 adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
1824 adev->ip_blocks[i].status.late_initialized = false;
Monk Liua6dcfd92016-05-19 14:36:34 +08001825 }
1826
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001827 if (amdgpu_sriov_vf(adev)) {
Monk Liu24936642017-01-09 15:54:32 +08001828 amdgpu_bo_free_kernel(&adev->virt.csa_obj, &adev->virt.csa_vmid0_addr, NULL);
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001829 amdgpu_virt_release_full_gpu(adev, false);
1830 }
Monk Liu24936642017-01-09 15:54:32 +08001831
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001832 return 0;
1833}
1834
Shirish S2dc80b02017-05-25 10:05:25 +05301835static void amdgpu_late_init_func_handler(struct work_struct *work)
1836{
1837 struct amdgpu_device *adev =
1838 container_of(work, struct amdgpu_device, late_init_work.work);
1839 amdgpu_late_set_cg_state(adev);
1840}
1841
Alex Deucherfaefba92016-12-06 10:38:29 -05001842int amdgpu_suspend(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001843{
1844 int i, r;
1845
Xiangliang Yue941ea92017-01-18 12:47:55 +08001846 if (amdgpu_sriov_vf(adev))
1847 amdgpu_virt_request_full_gpu(adev, false);
1848
Flora Cuic5a93a22016-02-26 10:45:25 +08001849 /* ungate SMC block first */
1850 r = amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC,
1851 AMD_CG_STATE_UNGATE);
1852 if (r) {
1853 DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n",r);
1854 }
1855
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001856 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001857 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001858 continue;
1859 /* ungate blocks so that suspend can properly shut them down */
Flora Cuic5a93a22016-02-26 10:45:25 +08001860 if (i != AMD_IP_BLOCK_TYPE_SMC) {
Alex Deuchera1255102016-10-13 17:41:13 -04001861 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1862 AMD_CG_STATE_UNGATE);
Flora Cuic5a93a22016-02-26 10:45:25 +08001863 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001864 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1865 adev->ip_blocks[i].version->funcs->name, r);
Flora Cuic5a93a22016-02-26 10:45:25 +08001866 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001867 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001868 /* XXX handle errors */
Alex Deuchera1255102016-10-13 17:41:13 -04001869 r = adev->ip_blocks[i].version->funcs->suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001870 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001871 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001872 DRM_ERROR("suspend of IP block <%s> failed %d\n",
1873 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001874 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001875 }
1876
Xiangliang Yue941ea92017-01-18 12:47:55 +08001877 if (amdgpu_sriov_vf(adev))
1878 amdgpu_virt_release_full_gpu(adev, false);
1879
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001880 return 0;
1881}
1882
Monk Liue4f0fdc2017-02-09 11:55:49 +08001883static int amdgpu_sriov_reinit_early(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08001884{
1885 int i, r;
1886
Monk Liu2cb681b2017-04-26 12:00:49 +08001887 static enum amd_ip_block_type ip_order[] = {
1888 AMD_IP_BLOCK_TYPE_GMC,
1889 AMD_IP_BLOCK_TYPE_COMMON,
Monk Liu2cb681b2017-04-26 12:00:49 +08001890 AMD_IP_BLOCK_TYPE_IH,
1891 };
Monk Liua90ad3c2017-01-23 14:22:08 +08001892
Monk Liu2cb681b2017-04-26 12:00:49 +08001893 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1894 int j;
1895 struct amdgpu_ip_block *block;
Monk Liua90ad3c2017-01-23 14:22:08 +08001896
Monk Liu2cb681b2017-04-26 12:00:49 +08001897 for (j = 0; j < adev->num_ip_blocks; j++) {
1898 block = &adev->ip_blocks[j];
1899
1900 if (block->version->type != ip_order[i] ||
1901 !block->status.valid)
1902 continue;
1903
1904 r = block->version->funcs->hw_init(adev);
1905 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
Monk Liua90ad3c2017-01-23 14:22:08 +08001906 }
1907 }
1908
1909 return 0;
1910}
1911
Monk Liue4f0fdc2017-02-09 11:55:49 +08001912static int amdgpu_sriov_reinit_late(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08001913{
1914 int i, r;
1915
Monk Liu2cb681b2017-04-26 12:00:49 +08001916 static enum amd_ip_block_type ip_order[] = {
1917 AMD_IP_BLOCK_TYPE_SMC,
1918 AMD_IP_BLOCK_TYPE_DCE,
1919 AMD_IP_BLOCK_TYPE_GFX,
1920 AMD_IP_BLOCK_TYPE_SDMA,
1921 AMD_IP_BLOCK_TYPE_VCE,
1922 };
Monk Liua90ad3c2017-01-23 14:22:08 +08001923
Monk Liu2cb681b2017-04-26 12:00:49 +08001924 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1925 int j;
1926 struct amdgpu_ip_block *block;
Monk Liua90ad3c2017-01-23 14:22:08 +08001927
Monk Liu2cb681b2017-04-26 12:00:49 +08001928 for (j = 0; j < adev->num_ip_blocks; j++) {
1929 block = &adev->ip_blocks[j];
1930
1931 if (block->version->type != ip_order[i] ||
1932 !block->status.valid)
1933 continue;
1934
1935 r = block->version->funcs->hw_init(adev);
1936 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
Monk Liua90ad3c2017-01-23 14:22:08 +08001937 }
1938 }
1939
1940 return 0;
1941}
1942
Chunming Zhoufcf06492017-05-05 10:33:33 +08001943static int amdgpu_resume_phase1(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001944{
1945 int i, r;
1946
1947 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001948 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001949 continue;
Chunming Zhoufcf06492017-05-05 10:33:33 +08001950 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
1951 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
1952 adev->ip_blocks[i].version->type ==
1953 AMD_IP_BLOCK_TYPE_IH) {
1954 r = adev->ip_blocks[i].version->funcs->resume(adev);
1955 if (r) {
1956 DRM_ERROR("resume of IP block <%s> failed %d\n",
1957 adev->ip_blocks[i].version->funcs->name, r);
1958 return r;
1959 }
1960 }
1961 }
1962
1963 return 0;
1964}
1965
1966static int amdgpu_resume_phase2(struct amdgpu_device *adev)
1967{
1968 int i, r;
1969
1970 for (i = 0; i < adev->num_ip_blocks; i++) {
1971 if (!adev->ip_blocks[i].status.valid)
1972 continue;
1973 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
1974 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
1975 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH )
1976 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001977 r = adev->ip_blocks[i].version->funcs->resume(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001978 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001979 DRM_ERROR("resume of IP block <%s> failed %d\n",
1980 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001981 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001982 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001983 }
1984
1985 return 0;
1986}
1987
Chunming Zhoufcf06492017-05-05 10:33:33 +08001988static int amdgpu_resume(struct amdgpu_device *adev)
1989{
1990 int r;
1991
1992 r = amdgpu_resume_phase1(adev);
1993 if (r)
1994 return r;
1995 r = amdgpu_resume_phase2(adev);
1996
1997 return r;
1998}
1999
Monk Liu4e99a442016-03-31 13:26:59 +08002000static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
Andres Rodriguez048765a2016-06-11 02:51:32 -04002001{
Alex Deuchera5bde2f2016-09-23 16:23:41 -04002002 if (adev->is_atom_fw) {
2003 if (amdgpu_atomfirmware_gpu_supports_virtualization(adev))
2004 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
2005 } else {
2006 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
2007 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
2008 }
Andres Rodriguez048765a2016-06-11 02:51:32 -04002009}
2010
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002011/**
2012 * amdgpu_device_init - initialize the driver
2013 *
2014 * @adev: amdgpu_device pointer
2015 * @pdev: drm dev pointer
2016 * @pdev: pci dev pointer
2017 * @flags: driver flags
2018 *
2019 * Initializes the driver info and hw (all asics).
2020 * Returns 0 for success or an error on failure.
2021 * Called at driver startup.
2022 */
2023int amdgpu_device_init(struct amdgpu_device *adev,
2024 struct drm_device *ddev,
2025 struct pci_dev *pdev,
2026 uint32_t flags)
2027{
2028 int r, i;
2029 bool runtime = false;
Marek Olšák95844d22016-08-17 23:49:27 +02002030 u32 max_MBps;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002031
2032 adev->shutdown = false;
2033 adev->dev = &pdev->dev;
2034 adev->ddev = ddev;
2035 adev->pdev = pdev;
2036 adev->flags = flags;
Jammy Zhou2f7d10b2015-07-22 11:29:01 +08002037 adev->asic_type = flags & AMD_ASIC_MASK;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002038 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
Christian König6f02a692017-07-07 11:56:59 +02002039 adev->mc.gart_size = 512 * 1024 * 1024;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002040 adev->accel_working = false;
2041 adev->num_rings = 0;
2042 adev->mman.buffer_funcs = NULL;
2043 adev->mman.buffer_funcs_ring = NULL;
2044 adev->vm_manager.vm_pte_funcs = NULL;
Christian König2d55e452016-02-08 17:37:38 +01002045 adev->vm_manager.vm_pte_num_rings = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002046 adev->gart.gart_funcs = NULL;
Chris Wilsonf54d1862016-10-25 13:00:45 +01002047 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002048
2049 adev->smc_rreg = &amdgpu_invalid_rreg;
2050 adev->smc_wreg = &amdgpu_invalid_wreg;
2051 adev->pcie_rreg = &amdgpu_invalid_rreg;
2052 adev->pcie_wreg = &amdgpu_invalid_wreg;
Huang Rui36b9a952016-08-31 13:23:25 +08002053 adev->pciep_rreg = &amdgpu_invalid_rreg;
2054 adev->pciep_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002055 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
2056 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
2057 adev->didt_rreg = &amdgpu_invalid_rreg;
2058 adev->didt_wreg = &amdgpu_invalid_wreg;
Rex Zhuccdbb202016-06-08 12:47:41 +08002059 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
2060 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002061 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
2062 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
2063
Rex Zhuccdbb202016-06-08 12:47:41 +08002064
Alex Deucher3e39ab92015-06-05 15:04:33 -04002065 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
2066 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
2067 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002068
2069 /* mutex initialization are all done here so we
2070 * can recall function without having locking issues */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002071 atomic_set(&adev->irq.ih.lock, 0);
Huang Rui0e5ca0d2017-03-03 18:37:23 -05002072 mutex_init(&adev->firmware.mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002073 mutex_init(&adev->pm.mutex);
2074 mutex_init(&adev->gfx.gpu_clock_mutex);
2075 mutex_init(&adev->srbm_mutex);
2076 mutex_init(&adev->grbm_idx_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002077 mutex_init(&adev->mn_lock);
2078 hash_init(adev->mn_hash);
2079
2080 amdgpu_check_arguments(adev);
2081
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002082 spin_lock_init(&adev->mmio_idx_lock);
2083 spin_lock_init(&adev->smc_idx_lock);
2084 spin_lock_init(&adev->pcie_idx_lock);
2085 spin_lock_init(&adev->uvd_ctx_idx_lock);
2086 spin_lock_init(&adev->didt_idx_lock);
Rex Zhuccdbb202016-06-08 12:47:41 +08002087 spin_lock_init(&adev->gc_cac_idx_lock);
Evan Quan16abb5d2017-07-04 09:21:50 +08002088 spin_lock_init(&adev->se_cac_idx_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002089 spin_lock_init(&adev->audio_endpt_idx_lock);
Marek Olšák95844d22016-08-17 23:49:27 +02002090 spin_lock_init(&adev->mm_stats.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002091
Chunming Zhou0c4e7fa2016-08-17 11:41:30 +08002092 INIT_LIST_HEAD(&adev->shadow_list);
2093 mutex_init(&adev->shadow_list_lock);
2094
Chunming Zhou5c1354b2016-08-30 16:13:10 +08002095 INIT_LIST_HEAD(&adev->gtt_list);
2096 spin_lock_init(&adev->gtt_list_lock);
2097
Andres Rodriguez795f2812017-03-06 16:27:55 -05002098 INIT_LIST_HEAD(&adev->ring_lru_list);
2099 spin_lock_init(&adev->ring_lru_list_lock);
2100
Shirish S2dc80b02017-05-25 10:05:25 +05302101 INIT_DELAYED_WORK(&adev->late_init_work, amdgpu_late_init_func_handler);
2102
Alex Xie0fa49552017-06-08 14:58:05 -04002103 /* Registers mapping */
2104 /* TODO: block userspace mapping of io register */
Ken Wangda69c1612016-01-21 19:08:55 +08002105 if (adev->asic_type >= CHIP_BONAIRE) {
2106 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
2107 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
2108 } else {
2109 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
2110 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
2111 }
Chunming Zhou5c1354b2016-08-30 16:13:10 +08002112
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002113 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
2114 if (adev->rmmio == NULL) {
2115 return -ENOMEM;
2116 }
2117 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
2118 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
2119
Ken Wangda69c1612016-01-21 19:08:55 +08002120 if (adev->asic_type >= CHIP_BONAIRE)
2121 /* doorbell bar mapping */
2122 amdgpu_doorbell_init(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002123
2124 /* io port mapping */
2125 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
2126 if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
2127 adev->rio_mem_size = pci_resource_len(adev->pdev, i);
2128 adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
2129 break;
2130 }
2131 }
2132 if (adev->rio_mem == NULL)
Amber Linb64a18c2017-01-04 08:06:58 -05002133 DRM_INFO("PCI I/O BAR is not found.\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002134
2135 /* early init functions */
2136 r = amdgpu_early_init(adev);
2137 if (r)
2138 return r;
2139
2140 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
2141 /* this will fail for cards that aren't VGA class devices, just
2142 * ignore it */
2143 vga_client_register(adev->pdev, adev, NULL, amdgpu_vga_set_decode);
2144
2145 if (amdgpu_runtime_pm == 1)
2146 runtime = true;
Alex Deuchere9bef452016-04-25 13:12:18 -04002147 if (amdgpu_device_is_px(ddev))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002148 runtime = true;
Lukas Wunner84c8b222017-03-10 21:23:45 +01002149 if (!pci_is_thunderbolt_attached(adev->pdev))
2150 vga_switcheroo_register_client(adev->pdev,
2151 &amdgpu_switcheroo_ops, runtime);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002152 if (runtime)
2153 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
2154
2155 /* Read BIOS */
Alex Deucher83ba1262016-06-03 18:21:41 -04002156 if (!amdgpu_get_bios(adev)) {
2157 r = -EINVAL;
2158 goto failed;
2159 }
Nils Wallméniusf7e9e9f2016-12-14 21:52:45 +01002160
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002161 r = amdgpu_atombios_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002162 if (r) {
2163 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
Gavin Wan89041942017-06-23 13:55:15 -04002164 amdgpu_vf_error_put(AMDGIM_ERROR_VF_ATOMBIOS_INIT_FAIL, 0, 0);
Alex Deucher83ba1262016-06-03 18:21:41 -04002165 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002166 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002167
Monk Liu4e99a442016-03-31 13:26:59 +08002168 /* detect if we are with an SRIOV vbios */
2169 amdgpu_device_detect_sriov_bios(adev);
Andres Rodriguez048765a2016-06-11 02:51:32 -04002170
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002171 /* Post card if necessary */
Monk Liubec86372016-09-14 19:38:08 +08002172 if (amdgpu_vpost_needed(adev)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002173 if (!adev->bios) {
Monk Liubec86372016-09-14 19:38:08 +08002174 dev_err(adev->dev, "no vBIOS found\n");
Gavin Wan89041942017-06-23 13:55:15 -04002175 amdgpu_vf_error_put(AMDGIM_ERROR_VF_NO_VBIOS, 0, 0);
Alex Deucher83ba1262016-06-03 18:21:41 -04002176 r = -EINVAL;
2177 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002178 }
Monk Liubec86372016-09-14 19:38:08 +08002179 DRM_INFO("GPU posting now...\n");
Monk Liu4e99a442016-03-31 13:26:59 +08002180 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2181 if (r) {
2182 dev_err(adev->dev, "gpu post error!\n");
Gavin Wan89041942017-06-23 13:55:15 -04002183 amdgpu_vf_error_put(AMDGIM_ERROR_VF_GPU_POST_ERROR, 0, 0);
Monk Liu4e99a442016-03-31 13:26:59 +08002184 goto failed;
2185 }
2186 } else {
2187 DRM_INFO("GPU post is not needed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002188 }
2189
Alex Deuchera5bde2f2016-09-23 16:23:41 -04002190 if (!adev->is_atom_fw) {
2191 /* Initialize clocks */
2192 r = amdgpu_atombios_get_clock_info(adev);
2193 if (r) {
2194 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
Gavin Wan89041942017-06-23 13:55:15 -04002195 amdgpu_vf_error_put(AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
2196 goto failed;
Alex Deuchera5bde2f2016-09-23 16:23:41 -04002197 }
2198 /* init i2c buses */
2199 amdgpu_atombios_i2c_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002200 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002201
2202 /* Fence driver */
2203 r = amdgpu_fence_driver_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002204 if (r) {
2205 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
Gavin Wan89041942017-06-23 13:55:15 -04002206 amdgpu_vf_error_put(AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0);
Alex Deucher83ba1262016-06-03 18:21:41 -04002207 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002208 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002209
2210 /* init the mode config */
2211 drm_mode_config_init(adev->ddev);
2212
2213 r = amdgpu_init(adev);
2214 if (r) {
Alex Deucher2c1a2782015-12-07 17:02:53 -05002215 dev_err(adev->dev, "amdgpu_init failed\n");
Gavin Wan89041942017-06-23 13:55:15 -04002216 amdgpu_vf_error_put(AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002217 amdgpu_fini(adev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002218 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002219 }
2220
2221 adev->accel_working = true;
2222
Alex Xiee59c0202017-06-01 09:42:59 -04002223 amdgpu_vm_check_compute_bug(adev);
2224
Marek Olšák95844d22016-08-17 23:49:27 +02002225 /* Initialize the buffer migration limit. */
2226 if (amdgpu_moverate >= 0)
2227 max_MBps = amdgpu_moverate;
2228 else
2229 max_MBps = 8; /* Allow 8 MB/s. */
2230 /* Get a log2 for easy divisions. */
2231 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
2232
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002233 r = amdgpu_ib_pool_init(adev);
2234 if (r) {
2235 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
Gavin Wan89041942017-06-23 13:55:15 -04002236 amdgpu_vf_error_put(AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r);
Alex Deucher83ba1262016-06-03 18:21:41 -04002237 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002238 }
2239
2240 r = amdgpu_ib_ring_tests(adev);
2241 if (r)
2242 DRM_ERROR("ib ring test failed (%d).\n", r);
2243
Monk Liu9bc92b92017-02-08 17:38:13 +08002244 amdgpu_fbdev_init(adev);
2245
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002246 r = amdgpu_gem_debugfs_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002247 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002248 DRM_ERROR("registering gem debugfs failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002249
2250 r = amdgpu_debugfs_regs_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002251 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002252 DRM_ERROR("registering register debugfs failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002253
Huang Rui4f0955f2017-05-10 23:04:06 +08002254 r = amdgpu_debugfs_test_ib_ring_init(adev);
2255 if (r)
2256 DRM_ERROR("registering register test ib ring debugfs failed (%d).\n", r);
2257
Huang Rui50ab2532016-06-12 15:51:09 +08002258 r = amdgpu_debugfs_firmware_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002259 if (r)
Huang Rui50ab2532016-06-12 15:51:09 +08002260 DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
Huang Rui50ab2532016-06-12 15:51:09 +08002261
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002262 if ((amdgpu_testing & 1)) {
2263 if (adev->accel_working)
2264 amdgpu_test_moves(adev);
2265 else
2266 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
2267 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002268 if (amdgpu_benchmarking) {
2269 if (adev->accel_working)
2270 amdgpu_benchmark(adev, amdgpu_benchmarking);
2271 else
2272 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
2273 }
2274
2275 /* enable clockgating, etc. after ib tests, etc. since some blocks require
2276 * explicit gating rather than handling it automatically.
2277 */
2278 r = amdgpu_late_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002279 if (r) {
2280 dev_err(adev->dev, "amdgpu_late_init failed\n");
Gavin Wan89041942017-06-23 13:55:15 -04002281 amdgpu_vf_error_put(AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r);
Alex Deucher83ba1262016-06-03 18:21:41 -04002282 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002283 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002284
2285 return 0;
Alex Deucher83ba1262016-06-03 18:21:41 -04002286
2287failed:
Gavin Wan89041942017-06-23 13:55:15 -04002288 amdgpu_vf_error_trans_all(adev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002289 if (runtime)
2290 vga_switcheroo_fini_domain_pm_ops(adev->dev);
2291 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002292}
2293
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002294/**
2295 * amdgpu_device_fini - tear down the driver
2296 *
2297 * @adev: amdgpu_device pointer
2298 *
2299 * Tear down the driver info (all asics).
2300 * Called at driver shutdown.
2301 */
2302void amdgpu_device_fini(struct amdgpu_device *adev)
2303{
2304 int r;
2305
2306 DRM_INFO("amdgpu: finishing device.\n");
2307 adev->shutdown = true;
Pixel Dingdb2c2a92017-04-25 16:47:42 +08002308 if (adev->mode_info.mode_config_initialized)
2309 drm_crtc_force_disable_all(adev->ddev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002310 /* evict vram memory */
2311 amdgpu_bo_evict_vram(adev);
2312 amdgpu_ib_pool_fini(adev);
2313 amdgpu_fence_driver_fini(adev);
2314 amdgpu_fbdev_fini(adev);
2315 r = amdgpu_fini(adev);
Huang Ruiab4fe3e2017-06-05 22:11:59 +08002316 if (adev->firmware.gpu_info_fw) {
2317 release_firmware(adev->firmware.gpu_info_fw);
2318 adev->firmware.gpu_info_fw = NULL;
2319 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002320 adev->accel_working = false;
Shirish S2dc80b02017-05-25 10:05:25 +05302321 cancel_delayed_work_sync(&adev->late_init_work);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002322 /* free i2c buses */
2323 amdgpu_i2c_fini(adev);
2324 amdgpu_atombios_fini(adev);
2325 kfree(adev->bios);
2326 adev->bios = NULL;
Lukas Wunner84c8b222017-03-10 21:23:45 +01002327 if (!pci_is_thunderbolt_attached(adev->pdev))
2328 vga_switcheroo_unregister_client(adev->pdev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002329 if (adev->flags & AMD_IS_PX)
2330 vga_switcheroo_fini_domain_pm_ops(adev->dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002331 vga_client_register(adev->pdev, NULL, NULL, NULL);
2332 if (adev->rio_mem)
2333 pci_iounmap(adev->pdev, adev->rio_mem);
2334 adev->rio_mem = NULL;
2335 iounmap(adev->rmmio);
2336 adev->rmmio = NULL;
Ken Wangda69c1612016-01-21 19:08:55 +08002337 if (adev->asic_type >= CHIP_BONAIRE)
2338 amdgpu_doorbell_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002339 amdgpu_debugfs_regs_cleanup(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002340}
2341
2342
2343/*
2344 * Suspend & resume.
2345 */
2346/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002347 * amdgpu_device_suspend - initiate device suspend
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002348 *
2349 * @pdev: drm dev pointer
2350 * @state: suspend state
2351 *
2352 * Puts the hw in the suspend state (all asics).
2353 * Returns 0 for success or an error on failure.
2354 * Called at driver suspend.
2355 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002356int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002357{
2358 struct amdgpu_device *adev;
2359 struct drm_crtc *crtc;
2360 struct drm_connector *connector;
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002361 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002362
2363 if (dev == NULL || dev->dev_private == NULL) {
2364 return -ENODEV;
2365 }
2366
2367 adev = dev->dev_private;
2368
2369 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2370 return 0;
2371
2372 drm_kms_helper_poll_disable(dev);
2373
2374 /* turn off display hw */
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002375 drm_modeset_lock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002376 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2377 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
2378 }
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002379 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002380
Alex Deucher756e6882015-10-08 00:03:36 -04002381 /* unpin the front buffers and cursors */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002382 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Alex Deucher756e6882015-10-08 00:03:36 -04002383 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002384 struct amdgpu_framebuffer *rfb = to_amdgpu_framebuffer(crtc->primary->fb);
2385 struct amdgpu_bo *robj;
2386
Alex Deucher756e6882015-10-08 00:03:36 -04002387 if (amdgpu_crtc->cursor_bo) {
2388 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
Alex Xie7a6901d2017-04-24 13:52:41 -04002389 r = amdgpu_bo_reserve(aobj, true);
Alex Deucher756e6882015-10-08 00:03:36 -04002390 if (r == 0) {
2391 amdgpu_bo_unpin(aobj);
2392 amdgpu_bo_unreserve(aobj);
2393 }
2394 }
2395
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002396 if (rfb == NULL || rfb->obj == NULL) {
2397 continue;
2398 }
2399 robj = gem_to_amdgpu_bo(rfb->obj);
2400 /* don't unpin kernel fb objects */
2401 if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
Alex Xie7a6901d2017-04-24 13:52:41 -04002402 r = amdgpu_bo_reserve(robj, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002403 if (r == 0) {
2404 amdgpu_bo_unpin(robj);
2405 amdgpu_bo_unreserve(robj);
2406 }
2407 }
2408 }
2409 /* evict vram memory */
2410 amdgpu_bo_evict_vram(adev);
2411
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002412 amdgpu_fence_driver_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002413
2414 r = amdgpu_suspend(adev);
2415
Alex Deuchera0a71e42016-10-10 12:41:36 -04002416 /* evict remaining vram memory
2417 * This second call to evict vram is to evict the gart page table
2418 * using the CPU.
2419 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002420 amdgpu_bo_evict_vram(adev);
2421
Alex Deucherd05da0e2017-06-30 17:08:45 -04002422 amdgpu_atombios_scratch_regs_save(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002423 pci_save_state(dev->pdev);
2424 if (suspend) {
2425 /* Shut down the device */
2426 pci_disable_device(dev->pdev);
2427 pci_set_power_state(dev->pdev, PCI_D3hot);
jimqu74b0b152016-09-07 17:09:12 +08002428 } else {
2429 r = amdgpu_asic_reset(adev);
2430 if (r)
2431 DRM_ERROR("amdgpu asic reset failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002432 }
2433
2434 if (fbcon) {
2435 console_lock();
2436 amdgpu_fbdev_set_suspend(adev, 1);
2437 console_unlock();
2438 }
2439 return 0;
2440}
2441
2442/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002443 * amdgpu_device_resume - initiate device resume
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002444 *
2445 * @pdev: drm dev pointer
2446 *
2447 * Bring the hw back to operating state (all asics).
2448 * Returns 0 for success or an error on failure.
2449 * Called at driver resume.
2450 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002451int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002452{
2453 struct drm_connector *connector;
2454 struct amdgpu_device *adev = dev->dev_private;
Alex Deucher756e6882015-10-08 00:03:36 -04002455 struct drm_crtc *crtc;
Huang Rui03161a62017-04-13 16:12:26 +08002456 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002457
2458 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2459 return 0;
2460
jimqu74b0b152016-09-07 17:09:12 +08002461 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002462 console_lock();
jimqu74b0b152016-09-07 17:09:12 +08002463
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002464 if (resume) {
2465 pci_set_power_state(dev->pdev, PCI_D0);
2466 pci_restore_state(dev->pdev);
jimqu74b0b152016-09-07 17:09:12 +08002467 r = pci_enable_device(dev->pdev);
Huang Rui03161a62017-04-13 16:12:26 +08002468 if (r)
2469 goto unlock;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002470 }
Alex Deucherd05da0e2017-06-30 17:08:45 -04002471 amdgpu_atombios_scratch_regs_restore(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002472
2473 /* post card */
Jim Quc836fec2017-02-10 15:59:59 +08002474 if (amdgpu_need_post(adev)) {
jimqu74b0b152016-09-07 17:09:12 +08002475 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2476 if (r)
2477 DRM_ERROR("amdgpu asic init failed\n");
2478 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002479
2480 r = amdgpu_resume(adev);
Rex Zhue6707212017-03-30 13:21:01 +08002481 if (r) {
Flora Cuica198522016-02-04 15:10:08 +08002482 DRM_ERROR("amdgpu_resume failed (%d).\n", r);
Huang Rui03161a62017-04-13 16:12:26 +08002483 goto unlock;
Rex Zhue6707212017-03-30 13:21:01 +08002484 }
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002485 amdgpu_fence_driver_resume(adev);
2486
Flora Cuica198522016-02-04 15:10:08 +08002487 if (resume) {
2488 r = amdgpu_ib_ring_tests(adev);
2489 if (r)
2490 DRM_ERROR("ib ring test failed (%d).\n", r);
2491 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002492
2493 r = amdgpu_late_init(adev);
Huang Rui03161a62017-04-13 16:12:26 +08002494 if (r)
2495 goto unlock;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002496
Alex Deucher756e6882015-10-08 00:03:36 -04002497 /* pin cursors */
2498 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2499 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2500
2501 if (amdgpu_crtc->cursor_bo) {
2502 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
Alex Xie7a6901d2017-04-24 13:52:41 -04002503 r = amdgpu_bo_reserve(aobj, true);
Alex Deucher756e6882015-10-08 00:03:36 -04002504 if (r == 0) {
2505 r = amdgpu_bo_pin(aobj,
2506 AMDGPU_GEM_DOMAIN_VRAM,
2507 &amdgpu_crtc->cursor_addr);
2508 if (r != 0)
2509 DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
2510 amdgpu_bo_unreserve(aobj);
2511 }
2512 }
2513 }
2514
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002515 /* blat the mode back in */
2516 if (fbcon) {
2517 drm_helper_resume_force_mode(dev);
2518 /* turn on display hw */
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002519 drm_modeset_lock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002520 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2521 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
2522 }
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002523 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002524 }
2525
2526 drm_kms_helper_poll_enable(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002527
2528 /*
2529 * Most of the connector probing functions try to acquire runtime pm
2530 * refs to ensure that the GPU is powered on when connector polling is
2531 * performed. Since we're calling this from a runtime PM callback,
2532 * trying to acquire rpm refs will cause us to deadlock.
2533 *
2534 * Since we're guaranteed to be holding the rpm lock, it's safe to
2535 * temporarily disable the rpm helpers so this doesn't deadlock us.
2536 */
2537#ifdef CONFIG_PM
2538 dev->dev->power.disable_depth++;
2539#endif
Alex Deucher54fb2a52015-11-24 14:30:56 -05002540 drm_helper_hpd_irq_event(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002541#ifdef CONFIG_PM
2542 dev->dev->power.disable_depth--;
2543#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002544
Huang Rui03161a62017-04-13 16:12:26 +08002545 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002546 amdgpu_fbdev_set_suspend(adev, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002547
Huang Rui03161a62017-04-13 16:12:26 +08002548unlock:
2549 if (fbcon)
2550 console_unlock();
2551
2552 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002553}
2554
Chunming Zhou63fbf422016-07-15 11:19:20 +08002555static bool amdgpu_check_soft_reset(struct amdgpu_device *adev)
2556{
2557 int i;
2558 bool asic_hang = false;
2559
2560 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002561 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou63fbf422016-07-15 11:19:20 +08002562 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002563 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
2564 adev->ip_blocks[i].status.hang =
2565 adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
2566 if (adev->ip_blocks[i].status.hang) {
2567 DRM_INFO("IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
Chunming Zhou63fbf422016-07-15 11:19:20 +08002568 asic_hang = true;
2569 }
2570 }
2571 return asic_hang;
2572}
2573
Baoyou Xie4d446652016-09-18 22:09:35 +08002574static int amdgpu_pre_soft_reset(struct amdgpu_device *adev)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002575{
2576 int i, r = 0;
2577
2578 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002579 if (!adev->ip_blocks[i].status.valid)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002580 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002581 if (adev->ip_blocks[i].status.hang &&
2582 adev->ip_blocks[i].version->funcs->pre_soft_reset) {
2583 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
Chunming Zhoud31a5012016-07-18 10:04:34 +08002584 if (r)
2585 return r;
2586 }
2587 }
2588
2589 return 0;
2590}
2591
Chunming Zhou35d782f2016-07-15 15:57:13 +08002592static bool amdgpu_need_full_reset(struct amdgpu_device *adev)
2593{
Alex Deucherda146d32016-10-13 16:07:03 -04002594 int i;
2595
2596 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002597 if (!adev->ip_blocks[i].status.valid)
Alex Deucherda146d32016-10-13 16:07:03 -04002598 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002599 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
2600 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
2601 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
2602 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE)) {
2603 if (adev->ip_blocks[i].status.hang) {
Alex Deucherda146d32016-10-13 16:07:03 -04002604 DRM_INFO("Some block need full reset!\n");
2605 return true;
2606 }
2607 }
Chunming Zhou35d782f2016-07-15 15:57:13 +08002608 }
2609 return false;
2610}
2611
2612static int amdgpu_soft_reset(struct amdgpu_device *adev)
2613{
2614 int i, r = 0;
2615
2616 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002617 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002618 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002619 if (adev->ip_blocks[i].status.hang &&
2620 adev->ip_blocks[i].version->funcs->soft_reset) {
2621 r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002622 if (r)
2623 return r;
2624 }
2625 }
2626
2627 return 0;
2628}
2629
2630static int amdgpu_post_soft_reset(struct amdgpu_device *adev)
2631{
2632 int i, r = 0;
2633
2634 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002635 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002636 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002637 if (adev->ip_blocks[i].status.hang &&
2638 adev->ip_blocks[i].version->funcs->post_soft_reset)
2639 r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002640 if (r)
2641 return r;
2642 }
2643
2644 return 0;
2645}
2646
Chunming Zhou3ad81f12016-08-05 17:30:17 +08002647bool amdgpu_need_backup(struct amdgpu_device *adev)
2648{
2649 if (adev->flags & AMD_IS_APU)
2650 return false;
2651
2652 return amdgpu_lockup_timeout > 0 ? true : false;
2653}
2654
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002655static int amdgpu_recover_vram_from_shadow(struct amdgpu_device *adev,
2656 struct amdgpu_ring *ring,
2657 struct amdgpu_bo *bo,
Chris Wilsonf54d1862016-10-25 13:00:45 +01002658 struct dma_fence **fence)
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002659{
2660 uint32_t domain;
2661 int r;
2662
Roger.He23d2e502017-04-21 14:24:26 +08002663 if (!bo->shadow)
2664 return 0;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002665
Alex Xie1d284792017-04-24 13:53:04 -04002666 r = amdgpu_bo_reserve(bo, true);
Roger.He23d2e502017-04-21 14:24:26 +08002667 if (r)
2668 return r;
2669 domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
2670 /* if bo has been evicted, then no need to recover */
2671 if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
Roger.He82521312017-04-21 13:08:43 +08002672 r = amdgpu_bo_validate(bo->shadow);
2673 if (r) {
2674 DRM_ERROR("bo validate failed!\n");
2675 goto err;
2676 }
2677
2678 r = amdgpu_ttm_bind(&bo->shadow->tbo, &bo->shadow->tbo.mem);
2679 if (r) {
2680 DRM_ERROR("%p bind failed\n", bo->shadow);
2681 goto err;
2682 }
2683
Roger.He23d2e502017-04-21 14:24:26 +08002684 r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002685 NULL, fence, true);
Roger.He23d2e502017-04-21 14:24:26 +08002686 if (r) {
2687 DRM_ERROR("recover page table failed!\n");
2688 goto err;
2689 }
2690 }
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002691err:
Roger.He23d2e502017-04-21 14:24:26 +08002692 amdgpu_bo_unreserve(bo);
2693 return r;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002694}
2695
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002696/**
Monk Liua90ad3c2017-01-23 14:22:08 +08002697 * amdgpu_sriov_gpu_reset - reset the asic
2698 *
2699 * @adev: amdgpu device pointer
Monk Liu7225f872017-04-26 14:51:54 +08002700 * @job: which job trigger hang
Monk Liua90ad3c2017-01-23 14:22:08 +08002701 *
2702 * Attempt the reset the GPU if it has hung (all asics).
2703 * for SRIOV case.
2704 * Returns 0 for success or an error on failure.
2705 */
Monk Liu7225f872017-04-26 14:51:54 +08002706int amdgpu_sriov_gpu_reset(struct amdgpu_device *adev, struct amdgpu_job *job)
Monk Liua90ad3c2017-01-23 14:22:08 +08002707{
Monk Liu65781c72017-05-11 13:36:44 +08002708 int i, j, r = 0;
Monk Liua90ad3c2017-01-23 14:22:08 +08002709 int resched;
2710 struct amdgpu_bo *bo, *tmp;
2711 struct amdgpu_ring *ring;
2712 struct dma_fence *fence = NULL, *next = NULL;
2713
Monk Liu147b5982017-01-25 15:48:01 +08002714 mutex_lock(&adev->virt.lock_reset);
Monk Liua90ad3c2017-01-23 14:22:08 +08002715 atomic_inc(&adev->gpu_reset_counter);
Monk Liu1fb37a32017-01-26 15:36:37 +08002716 adev->gfx.in_reset = true;
Monk Liua90ad3c2017-01-23 14:22:08 +08002717
2718 /* block TTM */
2719 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
2720
Monk Liu65781c72017-05-11 13:36:44 +08002721 /* we start from the ring trigger GPU hang */
2722 j = job ? job->ring->idx : 0;
Monk Liua90ad3c2017-01-23 14:22:08 +08002723
Monk Liu65781c72017-05-11 13:36:44 +08002724 /* block scheduler */
2725 for (i = j; i < j + AMDGPU_MAX_RINGS; ++i) {
2726 ring = adev->rings[i % AMDGPU_MAX_RINGS];
Monk Liua90ad3c2017-01-23 14:22:08 +08002727 if (!ring || !ring->sched.thread)
2728 continue;
2729
2730 kthread_park(ring->sched.thread);
Monk Liua90ad3c2017-01-23 14:22:08 +08002731
Monk Liu65781c72017-05-11 13:36:44 +08002732 if (job && j != i)
2733 continue;
2734
Monk Liu4f059ec2017-05-11 13:59:15 +08002735 /* here give the last chance to check if job removed from mirror-list
Monk Liu65781c72017-05-11 13:36:44 +08002736 * since we already pay some time on kthread_park */
Monk Liu4f059ec2017-05-11 13:59:15 +08002737 if (job && list_empty(&job->base.node)) {
Monk Liu65781c72017-05-11 13:36:44 +08002738 kthread_unpark(ring->sched.thread);
2739 goto give_up_reset;
2740 }
2741
2742 if (amd_sched_invalidate_job(&job->base, amdgpu_job_hang_limit))
2743 amd_sched_job_kickout(&job->base);
2744
2745 /* only do job_reset on the hang ring if @job not NULL */
2746 amd_sched_hw_job_reset(&ring->sched);
2747
2748 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
2749 amdgpu_fence_driver_force_completion_ring(ring);
2750 }
Monk Liua90ad3c2017-01-23 14:22:08 +08002751
2752 /* request to take full control of GPU before re-initialization */
Monk Liu7225f872017-04-26 14:51:54 +08002753 if (job)
Monk Liua90ad3c2017-01-23 14:22:08 +08002754 amdgpu_virt_reset_gpu(adev);
2755 else
2756 amdgpu_virt_request_full_gpu(adev, true);
2757
2758
2759 /* Resume IP prior to SMC */
Monk Liue4f0fdc2017-02-09 11:55:49 +08002760 amdgpu_sriov_reinit_early(adev);
Monk Liua90ad3c2017-01-23 14:22:08 +08002761
2762 /* we need recover gart prior to run SMC/CP/SDMA resume */
2763 amdgpu_ttm_recover_gart(adev);
2764
2765 /* now we are okay to resume SMC/CP/SDMA */
Monk Liue4f0fdc2017-02-09 11:55:49 +08002766 amdgpu_sriov_reinit_late(adev);
Monk Liua90ad3c2017-01-23 14:22:08 +08002767
2768 amdgpu_irq_gpu_reset_resume_helper(adev);
2769
2770 if (amdgpu_ib_ring_tests(adev))
2771 dev_err(adev->dev, "[GPU_RESET] ib ring test failed (%d).\n", r);
2772
2773 /* release full control of GPU after ib test */
2774 amdgpu_virt_release_full_gpu(adev, true);
2775
2776 DRM_INFO("recover vram bo from shadow\n");
2777
2778 ring = adev->mman.buffer_funcs_ring;
2779 mutex_lock(&adev->shadow_list_lock);
2780 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
Monk Liu236763d2017-05-01 16:15:31 +08002781 next = NULL;
Monk Liua90ad3c2017-01-23 14:22:08 +08002782 amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
2783 if (fence) {
2784 r = dma_fence_wait(fence, false);
2785 if (r) {
2786 WARN(r, "recovery from shadow isn't completed\n");
2787 break;
2788 }
2789 }
2790
2791 dma_fence_put(fence);
2792 fence = next;
2793 }
2794 mutex_unlock(&adev->shadow_list_lock);
2795
2796 if (fence) {
2797 r = dma_fence_wait(fence, false);
2798 if (r)
2799 WARN(r, "recovery from shadow isn't completed\n");
2800 }
2801 dma_fence_put(fence);
2802
Monk Liu65781c72017-05-11 13:36:44 +08002803 for (i = j; i < j + AMDGPU_MAX_RINGS; ++i) {
2804 ring = adev->rings[i % AMDGPU_MAX_RINGS];
Monk Liua90ad3c2017-01-23 14:22:08 +08002805 if (!ring || !ring->sched.thread)
2806 continue;
2807
Monk Liu65781c72017-05-11 13:36:44 +08002808 if (job && j != i) {
2809 kthread_unpark(ring->sched.thread);
2810 continue;
2811 }
2812
Monk Liua90ad3c2017-01-23 14:22:08 +08002813 amd_sched_job_recovery(&ring->sched);
2814 kthread_unpark(ring->sched.thread);
2815 }
2816
2817 drm_helper_resume_force_mode(adev->ddev);
Monk Liu65781c72017-05-11 13:36:44 +08002818give_up_reset:
Monk Liua90ad3c2017-01-23 14:22:08 +08002819 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
2820 if (r) {
2821 /* bad news, how to tell it to userspace ? */
2822 dev_info(adev->dev, "GPU reset failed\n");
Monk Liu65781c72017-05-11 13:36:44 +08002823 } else {
2824 dev_info(adev->dev, "GPU reset successed!\n");
Monk Liua90ad3c2017-01-23 14:22:08 +08002825 }
2826
Monk Liu1fb37a32017-01-26 15:36:37 +08002827 adev->gfx.in_reset = false;
Monk Liu147b5982017-01-25 15:48:01 +08002828 mutex_unlock(&adev->virt.lock_reset);
Monk Liua90ad3c2017-01-23 14:22:08 +08002829 return r;
2830}
2831
2832/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002833 * amdgpu_gpu_reset - reset the asic
2834 *
2835 * @adev: amdgpu device pointer
2836 *
2837 * Attempt the reset the GPU if it has hung (all asics).
2838 * Returns 0 for success or an error on failure.
2839 */
2840int amdgpu_gpu_reset(struct amdgpu_device *adev)
2841{
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002842 int i, r;
2843 int resched;
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002844 bool need_full_reset, vram_lost = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002845
Chunming Zhou63fbf422016-07-15 11:19:20 +08002846 if (!amdgpu_check_soft_reset(adev)) {
2847 DRM_INFO("No hardware hang detected. Did some blocks stall?\n");
2848 return 0;
2849 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002850
Marek Olšákd94aed52015-05-05 21:13:49 +02002851 atomic_inc(&adev->gpu_reset_counter);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002852
Chunming Zhoua3c47d62016-06-30 16:44:41 +08002853 /* block TTM */
2854 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
2855
Chunming Zhou0875dc92016-06-12 15:41:58 +08002856 /* block scheduler */
2857 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2858 struct amdgpu_ring *ring = adev->rings[i];
2859
Chunming Zhou51687752017-04-24 17:09:15 +08002860 if (!ring || !ring->sched.thread)
Chunming Zhou0875dc92016-06-12 15:41:58 +08002861 continue;
2862 kthread_park(ring->sched.thread);
Chunming Zhouaa1c8902016-06-30 13:56:02 +08002863 amd_sched_hw_job_reset(&ring->sched);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002864 }
Chunming Zhou2200eda2016-06-30 16:53:02 +08002865 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
2866 amdgpu_fence_driver_force_completion(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002867
Chunming Zhou35d782f2016-07-15 15:57:13 +08002868 need_full_reset = amdgpu_need_full_reset(adev);
2869
2870 if (!need_full_reset) {
2871 amdgpu_pre_soft_reset(adev);
2872 r = amdgpu_soft_reset(adev);
2873 amdgpu_post_soft_reset(adev);
2874 if (r || amdgpu_check_soft_reset(adev)) {
2875 DRM_INFO("soft reset failed, will fallback to full reset!\n");
2876 need_full_reset = true;
2877 }
2878 }
2879
2880 if (need_full_reset) {
Chunming Zhou35d782f2016-07-15 15:57:13 +08002881 r = amdgpu_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002882
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002883retry:
Alex Deucherd05da0e2017-06-30 17:08:45 -04002884 amdgpu_atombios_scratch_regs_save(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002885 r = amdgpu_asic_reset(adev);
Alex Deucherd05da0e2017-06-30 17:08:45 -04002886 amdgpu_atombios_scratch_regs_restore(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002887 /* post card */
2888 amdgpu_atom_asic_init(adev->mode_info.atom_context);
Alex Deucherbfa99262016-01-15 11:59:48 -05002889
Chunming Zhou35d782f2016-07-15 15:57:13 +08002890 if (!r) {
2891 dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
Chunming Zhoufcf06492017-05-05 10:33:33 +08002892 r = amdgpu_resume_phase1(adev);
2893 if (r)
2894 goto out;
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002895 vram_lost = amdgpu_check_vram_lost(adev);
Chunming Zhouf1892132017-05-15 16:48:27 +08002896 if (vram_lost) {
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002897 DRM_ERROR("VRAM is lost!\n");
Chunming Zhouf1892132017-05-15 16:48:27 +08002898 atomic_inc(&adev->vram_lost_counter);
2899 }
Chunming Zhou2c0d7312016-08-30 16:36:25 +08002900 r = amdgpu_ttm_recover_gart(adev);
2901 if (r)
Chunming Zhoufcf06492017-05-05 10:33:33 +08002902 goto out;
2903 r = amdgpu_resume_phase2(adev);
2904 if (r)
2905 goto out;
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002906 if (vram_lost)
2907 amdgpu_fill_reset_magic(adev);
Chunming Zhou2c0d7312016-08-30 16:36:25 +08002908 }
Chunming Zhoufcf06492017-05-05 10:33:33 +08002909 }
2910out:
2911 if (!r) {
2912 amdgpu_irq_gpu_reset_resume_helper(adev);
Chunming Zhou1f465082016-06-30 15:02:26 +08002913 r = amdgpu_ib_ring_tests(adev);
2914 if (r) {
2915 dev_err(adev->dev, "ib ring test failed (%d).\n", r);
Chunming Zhou40019dc2016-06-29 16:01:49 +08002916 r = amdgpu_suspend(adev);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002917 need_full_reset = true;
Chunming Zhou40019dc2016-06-29 16:01:49 +08002918 goto retry;
Chunming Zhou1f465082016-06-30 15:02:26 +08002919 }
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002920 /**
2921 * recovery vm page tables, since we cannot depend on VRAM is
2922 * consistent after gpu full reset.
2923 */
2924 if (need_full_reset && amdgpu_need_backup(adev)) {
2925 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
2926 struct amdgpu_bo *bo, *tmp;
Chris Wilsonf54d1862016-10-25 13:00:45 +01002927 struct dma_fence *fence = NULL, *next = NULL;
Chunming Zhou1f465082016-06-30 15:02:26 +08002928
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002929 DRM_INFO("recover vram bo from shadow\n");
2930 mutex_lock(&adev->shadow_list_lock);
2931 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
Monk Liu236763d2017-05-01 16:15:31 +08002932 next = NULL;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002933 amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
2934 if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01002935 r = dma_fence_wait(fence, false);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002936 if (r) {
Monk Liu1d7b17b2017-01-22 18:52:56 +08002937 WARN(r, "recovery from shadow isn't completed\n");
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002938 break;
2939 }
2940 }
2941
Chris Wilsonf54d1862016-10-25 13:00:45 +01002942 dma_fence_put(fence);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002943 fence = next;
2944 }
2945 mutex_unlock(&adev->shadow_list_lock);
2946 if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01002947 r = dma_fence_wait(fence, false);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002948 if (r)
Monk Liu1d7b17b2017-01-22 18:52:56 +08002949 WARN(r, "recovery from shadow isn't completed\n");
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002950 }
Chris Wilsonf54d1862016-10-25 13:00:45 +01002951 dma_fence_put(fence);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002952 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002953 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2954 struct amdgpu_ring *ring = adev->rings[i];
Chunming Zhou51687752017-04-24 17:09:15 +08002955
2956 if (!ring || !ring->sched.thread)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002957 continue;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002958
Chunming Zhouaa1c8902016-06-30 13:56:02 +08002959 amd_sched_job_recovery(&ring->sched);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002960 kthread_unpark(ring->sched.thread);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002961 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002962 } else {
Chunming Zhou2200eda2016-06-30 16:53:02 +08002963 dev_err(adev->dev, "asic resume failed (%d).\n", r);
Gavin Wan89041942017-06-23 13:55:15 -04002964 amdgpu_vf_error_put(AMDGIM_ERROR_VF_ASIC_RESUME_FAIL, 0, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002965 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
Chunming Zhou51687752017-04-24 17:09:15 +08002966 if (adev->rings[i] && adev->rings[i]->sched.thread) {
Chunming Zhou0875dc92016-06-12 15:41:58 +08002967 kthread_unpark(adev->rings[i]->sched.thread);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002968 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002969 }
2970 }
2971
2972 drm_helper_resume_force_mode(adev->ddev);
2973
2974 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
Gavin Wan89041942017-06-23 13:55:15 -04002975 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002976 /* bad news, how to tell it to userspace ? */
2977 dev_info(adev->dev, "GPU reset failed\n");
Gavin Wan89041942017-06-23 13:55:15 -04002978 amdgpu_vf_error_put(AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, r);
2979 }
2980 else {
Chunming Zhou6643be62017-05-05 10:50:09 +08002981 dev_info(adev->dev, "GPU reset successed!\n");
Gavin Wan89041942017-06-23 13:55:15 -04002982 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002983
Gavin Wan89041942017-06-23 13:55:15 -04002984 amdgpu_vf_error_trans_all(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002985 return r;
2986}
2987
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002988void amdgpu_get_pcie_info(struct amdgpu_device *adev)
2989{
2990 u32 mask;
2991 int ret;
2992
Alex Deuchercd474ba2016-02-04 10:21:23 -05002993 if (amdgpu_pcie_gen_cap)
2994 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
2995
2996 if (amdgpu_pcie_lane_cap)
2997 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
2998
2999 /* covers APUs as well */
3000 if (pci_is_root_bus(adev->pdev->bus)) {
3001 if (adev->pm.pcie_gen_mask == 0)
3002 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
3003 if (adev->pm.pcie_mlw_mask == 0)
3004 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003005 return;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003006 }
Alex Deuchercd474ba2016-02-04 10:21:23 -05003007
3008 if (adev->pm.pcie_gen_mask == 0) {
3009 ret = drm_pcie_get_speed_cap_mask(adev->ddev, &mask);
3010 if (!ret) {
3011 adev->pm.pcie_gen_mask = (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
3012 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
3013 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
3014
3015 if (mask & DRM_PCIE_SPEED_25)
3016 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
3017 if (mask & DRM_PCIE_SPEED_50)
3018 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2;
3019 if (mask & DRM_PCIE_SPEED_80)
3020 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3;
3021 } else {
3022 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
3023 }
3024 }
3025 if (adev->pm.pcie_mlw_mask == 0) {
3026 ret = drm_pcie_get_max_link_width(adev->ddev, &mask);
3027 if (!ret) {
3028 switch (mask) {
3029 case 32:
3030 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
3031 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
3032 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3033 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3034 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3035 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3036 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3037 break;
3038 case 16:
3039 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
3040 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3041 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3042 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3043 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3044 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3045 break;
3046 case 12:
3047 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3048 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3049 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3050 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3051 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3052 break;
3053 case 8:
3054 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3055 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3056 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3057 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3058 break;
3059 case 4:
3060 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3061 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3062 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3063 break;
3064 case 2:
3065 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3066 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3067 break;
3068 case 1:
3069 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
3070 break;
3071 default:
3072 break;
3073 }
3074 } else {
3075 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003076 }
3077 }
3078}
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003079
3080/*
3081 * Debugfs
3082 */
3083int amdgpu_debugfs_add_files(struct amdgpu_device *adev,
Nils Wallménius06ab6832016-05-02 12:46:15 -04003084 const struct drm_info_list *files,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003085 unsigned nfiles)
3086{
3087 unsigned i;
3088
3089 for (i = 0; i < adev->debugfs_count; i++) {
3090 if (adev->debugfs[i].files == files) {
3091 /* Already registered */
3092 return 0;
3093 }
3094 }
3095
3096 i = adev->debugfs_count + 1;
3097 if (i > AMDGPU_DEBUGFS_MAX_COMPONENTS) {
3098 DRM_ERROR("Reached maximum number of debugfs components.\n");
3099 DRM_ERROR("Report so we increase "
3100 "AMDGPU_DEBUGFS_MAX_COMPONENTS.\n");
3101 return -EINVAL;
3102 }
3103 adev->debugfs[adev->debugfs_count].files = files;
3104 adev->debugfs[adev->debugfs_count].num_files = nfiles;
3105 adev->debugfs_count = i;
3106#if defined(CONFIG_DEBUG_FS)
3107 drm_debugfs_create_files(files, nfiles,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003108 adev->ddev->primary->debugfs_root,
3109 adev->ddev->primary);
3110#endif
3111 return 0;
3112}
3113
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003114#if defined(CONFIG_DEBUG_FS)
3115
3116static ssize_t amdgpu_debugfs_regs_read(struct file *f, char __user *buf,
3117 size_t size, loff_t *pos)
3118{
Al Viro45063092016-12-04 18:24:56 -05003119 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003120 ssize_t result = 0;
3121 int r;
Tom St Denisbd122672016-07-28 09:39:22 -04003122 bool pm_pg_lock, use_bank;
Tom St Denis566281592016-06-27 11:55:07 -04003123 unsigned instance_bank, sh_bank, se_bank;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003124
3125 if (size & 0x3 || *pos & 0x3)
3126 return -EINVAL;
3127
Tom St Denisbd122672016-07-28 09:39:22 -04003128 /* are we reading registers for which a PG lock is necessary? */
3129 pm_pg_lock = (*pos >> 23) & 1;
3130
Tom St Denis566281592016-06-27 11:55:07 -04003131 if (*pos & (1ULL << 62)) {
3132 se_bank = (*pos >> 24) & 0x3FF;
3133 sh_bank = (*pos >> 34) & 0x3FF;
3134 instance_bank = (*pos >> 44) & 0x3FF;
Tom St Denis32977f92016-10-09 07:41:26 -04003135
3136 if (se_bank == 0x3FF)
3137 se_bank = 0xFFFFFFFF;
3138 if (sh_bank == 0x3FF)
3139 sh_bank = 0xFFFFFFFF;
3140 if (instance_bank == 0x3FF)
3141 instance_bank = 0xFFFFFFFF;
Tom St Denis566281592016-06-27 11:55:07 -04003142 use_bank = 1;
Tom St Denis566281592016-06-27 11:55:07 -04003143 } else {
3144 use_bank = 0;
3145 }
3146
Tom St Denis801a6aa9a62017-03-15 05:34:25 -04003147 *pos &= (1UL << 22) - 1;
Tom St Denisbd122672016-07-28 09:39:22 -04003148
Tom St Denis566281592016-06-27 11:55:07 -04003149 if (use_bank) {
Tom St Denis32977f92016-10-09 07:41:26 -04003150 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
3151 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
Tom St Denis566281592016-06-27 11:55:07 -04003152 return -EINVAL;
3153 mutex_lock(&adev->grbm_idx_mutex);
3154 amdgpu_gfx_select_se_sh(adev, se_bank,
3155 sh_bank, instance_bank);
3156 }
3157
Tom St Denisbd122672016-07-28 09:39:22 -04003158 if (pm_pg_lock)
3159 mutex_lock(&adev->pm.mutex);
3160
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003161 while (size) {
3162 uint32_t value;
3163
3164 if (*pos > adev->rmmio_size)
Tom St Denis566281592016-06-27 11:55:07 -04003165 goto end;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003166
3167 value = RREG32(*pos >> 2);
3168 r = put_user(value, (uint32_t *)buf);
Tom St Denis566281592016-06-27 11:55:07 -04003169 if (r) {
3170 result = r;
3171 goto end;
3172 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003173
3174 result += 4;
3175 buf += 4;
3176 *pos += 4;
3177 size -= 4;
3178 }
3179
Tom St Denis566281592016-06-27 11:55:07 -04003180end:
3181 if (use_bank) {
3182 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
3183 mutex_unlock(&adev->grbm_idx_mutex);
3184 }
3185
Tom St Denisbd122672016-07-28 09:39:22 -04003186 if (pm_pg_lock)
3187 mutex_unlock(&adev->pm.mutex);
3188
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003189 return result;
3190}
3191
3192static ssize_t amdgpu_debugfs_regs_write(struct file *f, const char __user *buf,
3193 size_t size, loff_t *pos)
3194{
Al Viro45063092016-12-04 18:24:56 -05003195 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003196 ssize_t result = 0;
3197 int r;
Tom St Denis394fdde2016-10-10 07:31:23 -04003198 bool pm_pg_lock, use_bank;
3199 unsigned instance_bank, sh_bank, se_bank;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003200
3201 if (size & 0x3 || *pos & 0x3)
3202 return -EINVAL;
3203
Tom St Denis394fdde2016-10-10 07:31:23 -04003204 /* are we reading registers for which a PG lock is necessary? */
3205 pm_pg_lock = (*pos >> 23) & 1;
3206
3207 if (*pos & (1ULL << 62)) {
3208 se_bank = (*pos >> 24) & 0x3FF;
3209 sh_bank = (*pos >> 34) & 0x3FF;
3210 instance_bank = (*pos >> 44) & 0x3FF;
3211
3212 if (se_bank == 0x3FF)
3213 se_bank = 0xFFFFFFFF;
3214 if (sh_bank == 0x3FF)
3215 sh_bank = 0xFFFFFFFF;
3216 if (instance_bank == 0x3FF)
3217 instance_bank = 0xFFFFFFFF;
3218 use_bank = 1;
3219 } else {
3220 use_bank = 0;
3221 }
3222
Tom St Denis801a6aa9a62017-03-15 05:34:25 -04003223 *pos &= (1UL << 22) - 1;
Tom St Denis394fdde2016-10-10 07:31:23 -04003224
3225 if (use_bank) {
3226 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
3227 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
3228 return -EINVAL;
3229 mutex_lock(&adev->grbm_idx_mutex);
3230 amdgpu_gfx_select_se_sh(adev, se_bank,
3231 sh_bank, instance_bank);
3232 }
3233
3234 if (pm_pg_lock)
3235 mutex_lock(&adev->pm.mutex);
3236
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003237 while (size) {
3238 uint32_t value;
3239
3240 if (*pos > adev->rmmio_size)
3241 return result;
3242
3243 r = get_user(value, (uint32_t *)buf);
3244 if (r)
3245 return r;
3246
3247 WREG32(*pos >> 2, value);
3248
3249 result += 4;
3250 buf += 4;
3251 *pos += 4;
3252 size -= 4;
3253 }
3254
Tom St Denis394fdde2016-10-10 07:31:23 -04003255 if (use_bank) {
3256 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
3257 mutex_unlock(&adev->grbm_idx_mutex);
3258 }
3259
3260 if (pm_pg_lock)
3261 mutex_unlock(&adev->pm.mutex);
3262
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003263 return result;
3264}
3265
Tom St Denisadcec282016-04-15 13:08:44 -04003266static ssize_t amdgpu_debugfs_regs_pcie_read(struct file *f, char __user *buf,
3267 size_t size, loff_t *pos)
3268{
Al Viro45063092016-12-04 18:24:56 -05003269 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003270 ssize_t result = 0;
3271 int r;
3272
3273 if (size & 0x3 || *pos & 0x3)
3274 return -EINVAL;
3275
3276 while (size) {
3277 uint32_t value;
3278
3279 value = RREG32_PCIE(*pos >> 2);
3280 r = put_user(value, (uint32_t *)buf);
3281 if (r)
3282 return r;
3283
3284 result += 4;
3285 buf += 4;
3286 *pos += 4;
3287 size -= 4;
3288 }
3289
3290 return result;
3291}
3292
3293static ssize_t amdgpu_debugfs_regs_pcie_write(struct file *f, const char __user *buf,
3294 size_t size, loff_t *pos)
3295{
Al Viro45063092016-12-04 18:24:56 -05003296 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003297 ssize_t result = 0;
3298 int r;
3299
3300 if (size & 0x3 || *pos & 0x3)
3301 return -EINVAL;
3302
3303 while (size) {
3304 uint32_t value;
3305
3306 r = get_user(value, (uint32_t *)buf);
3307 if (r)
3308 return r;
3309
3310 WREG32_PCIE(*pos >> 2, value);
3311
3312 result += 4;
3313 buf += 4;
3314 *pos += 4;
3315 size -= 4;
3316 }
3317
3318 return result;
3319}
3320
3321static ssize_t amdgpu_debugfs_regs_didt_read(struct file *f, char __user *buf,
3322 size_t size, loff_t *pos)
3323{
Al Viro45063092016-12-04 18:24:56 -05003324 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003325 ssize_t result = 0;
3326 int r;
3327
3328 if (size & 0x3 || *pos & 0x3)
3329 return -EINVAL;
3330
3331 while (size) {
3332 uint32_t value;
3333
3334 value = RREG32_DIDT(*pos >> 2);
3335 r = put_user(value, (uint32_t *)buf);
3336 if (r)
3337 return r;
3338
3339 result += 4;
3340 buf += 4;
3341 *pos += 4;
3342 size -= 4;
3343 }
3344
3345 return result;
3346}
3347
3348static ssize_t amdgpu_debugfs_regs_didt_write(struct file *f, const char __user *buf,
3349 size_t size, loff_t *pos)
3350{
Al Viro45063092016-12-04 18:24:56 -05003351 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003352 ssize_t result = 0;
3353 int r;
3354
3355 if (size & 0x3 || *pos & 0x3)
3356 return -EINVAL;
3357
3358 while (size) {
3359 uint32_t value;
3360
3361 r = get_user(value, (uint32_t *)buf);
3362 if (r)
3363 return r;
3364
3365 WREG32_DIDT(*pos >> 2, value);
3366
3367 result += 4;
3368 buf += 4;
3369 *pos += 4;
3370 size -= 4;
3371 }
3372
3373 return result;
3374}
3375
3376static ssize_t amdgpu_debugfs_regs_smc_read(struct file *f, char __user *buf,
3377 size_t size, loff_t *pos)
3378{
Al Viro45063092016-12-04 18:24:56 -05003379 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003380 ssize_t result = 0;
3381 int r;
3382
3383 if (size & 0x3 || *pos & 0x3)
3384 return -EINVAL;
3385
3386 while (size) {
3387 uint32_t value;
3388
Tom St Denis6fc0dea2016-08-29 08:39:29 -04003389 value = RREG32_SMC(*pos);
Tom St Denisadcec282016-04-15 13:08:44 -04003390 r = put_user(value, (uint32_t *)buf);
3391 if (r)
3392 return r;
3393
3394 result += 4;
3395 buf += 4;
3396 *pos += 4;
3397 size -= 4;
3398 }
3399
3400 return result;
3401}
3402
3403static ssize_t amdgpu_debugfs_regs_smc_write(struct file *f, const char __user *buf,
3404 size_t size, loff_t *pos)
3405{
Al Viro45063092016-12-04 18:24:56 -05003406 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003407 ssize_t result = 0;
3408 int r;
3409
3410 if (size & 0x3 || *pos & 0x3)
3411 return -EINVAL;
3412
3413 while (size) {
3414 uint32_t value;
3415
3416 r = get_user(value, (uint32_t *)buf);
3417 if (r)
3418 return r;
3419
Tom St Denis6fc0dea2016-08-29 08:39:29 -04003420 WREG32_SMC(*pos, value);
Tom St Denisadcec282016-04-15 13:08:44 -04003421
3422 result += 4;
3423 buf += 4;
3424 *pos += 4;
3425 size -= 4;
3426 }
3427
3428 return result;
3429}
3430
Tom St Denis1e051412016-06-27 09:57:18 -04003431static ssize_t amdgpu_debugfs_gca_config_read(struct file *f, char __user *buf,
3432 size_t size, loff_t *pos)
3433{
Al Viro45063092016-12-04 18:24:56 -05003434 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denis1e051412016-06-27 09:57:18 -04003435 ssize_t result = 0;
3436 int r;
3437 uint32_t *config, no_regs = 0;
3438
3439 if (size & 0x3 || *pos & 0x3)
3440 return -EINVAL;
3441
Markus Elfringecab7662016-09-18 17:00:52 +02003442 config = kmalloc_array(256, sizeof(*config), GFP_KERNEL);
Tom St Denis1e051412016-06-27 09:57:18 -04003443 if (!config)
3444 return -ENOMEM;
3445
3446 /* version, increment each time something is added */
Tom St Denis9a999352017-01-18 13:01:25 -05003447 config[no_regs++] = 3;
Tom St Denis1e051412016-06-27 09:57:18 -04003448 config[no_regs++] = adev->gfx.config.max_shader_engines;
3449 config[no_regs++] = adev->gfx.config.max_tile_pipes;
3450 config[no_regs++] = adev->gfx.config.max_cu_per_sh;
3451 config[no_regs++] = adev->gfx.config.max_sh_per_se;
3452 config[no_regs++] = adev->gfx.config.max_backends_per_se;
3453 config[no_regs++] = adev->gfx.config.max_texture_channel_caches;
3454 config[no_regs++] = adev->gfx.config.max_gprs;
3455 config[no_regs++] = adev->gfx.config.max_gs_threads;
3456 config[no_regs++] = adev->gfx.config.max_hw_contexts;
3457 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_frontend;
3458 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_backend;
3459 config[no_regs++] = adev->gfx.config.sc_hiz_tile_fifo_size;
3460 config[no_regs++] = adev->gfx.config.sc_earlyz_tile_fifo_size;
3461 config[no_regs++] = adev->gfx.config.num_tile_pipes;
3462 config[no_regs++] = adev->gfx.config.backend_enable_mask;
3463 config[no_regs++] = adev->gfx.config.mem_max_burst_length_bytes;
3464 config[no_regs++] = adev->gfx.config.mem_row_size_in_kb;
3465 config[no_regs++] = adev->gfx.config.shader_engine_tile_size;
3466 config[no_regs++] = adev->gfx.config.num_gpus;
3467 config[no_regs++] = adev->gfx.config.multi_gpu_tile_size;
3468 config[no_regs++] = adev->gfx.config.mc_arb_ramcfg;
3469 config[no_regs++] = adev->gfx.config.gb_addr_config;
3470 config[no_regs++] = adev->gfx.config.num_rbs;
3471
Tom St Denis89a8f302016-08-12 15:14:31 -04003472 /* rev==1 */
3473 config[no_regs++] = adev->rev_id;
3474 config[no_regs++] = adev->pg_flags;
3475 config[no_regs++] = adev->cg_flags;
3476
Tom St Denise9f11dc2016-08-17 12:00:51 -04003477 /* rev==2 */
3478 config[no_regs++] = adev->family;
3479 config[no_regs++] = adev->external_rev_id;
3480
Tom St Denis9a999352017-01-18 13:01:25 -05003481 /* rev==3 */
3482 config[no_regs++] = adev->pdev->device;
3483 config[no_regs++] = adev->pdev->revision;
3484 config[no_regs++] = adev->pdev->subsystem_device;
3485 config[no_regs++] = adev->pdev->subsystem_vendor;
3486
Tom St Denis1e051412016-06-27 09:57:18 -04003487 while (size && (*pos < no_regs * 4)) {
3488 uint32_t value;
3489
3490 value = config[*pos >> 2];
3491 r = put_user(value, (uint32_t *)buf);
3492 if (r) {
3493 kfree(config);
3494 return r;
3495 }
3496
3497 result += 4;
3498 buf += 4;
3499 *pos += 4;
3500 size -= 4;
3501 }
3502
3503 kfree(config);
3504 return result;
3505}
3506
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003507static ssize_t amdgpu_debugfs_sensor_read(struct file *f, char __user *buf,
3508 size_t size, loff_t *pos)
3509{
Al Viro45063092016-12-04 18:24:56 -05003510 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003511 int idx, x, outsize, r, valuesize;
3512 uint32_t values[16];
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003513
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003514 if (size & 3 || *pos & 0x3)
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003515 return -EINVAL;
3516
Samuel Pitoiset3cbc6142017-02-15 19:32:29 +01003517 if (amdgpu_dpm == 0)
3518 return -EINVAL;
3519
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003520 /* convert offset to sensor number */
3521 idx = *pos >> 2;
3522
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003523 valuesize = sizeof(values);
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003524 if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->read_sensor)
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003525 r = adev->powerplay.pp_funcs->read_sensor(adev->powerplay.pp_handle, idx, &values[0], &valuesize);
Samuel Pitoiset3cbc6142017-02-15 19:32:29 +01003526 else if (adev->pm.funcs && adev->pm.funcs->read_sensor)
3527 r = adev->pm.funcs->read_sensor(adev, idx, &values[0],
3528 &valuesize);
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003529 else
3530 return -EINVAL;
3531
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003532 if (size > valuesize)
3533 return -EINVAL;
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003534
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003535 outsize = 0;
3536 x = 0;
3537 if (!r) {
3538 while (size) {
3539 r = put_user(values[x++], (int32_t *)buf);
3540 buf += 4;
3541 size -= 4;
3542 outsize += 4;
3543 }
3544 }
3545
3546 return !r ? outsize : r;
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003547}
Tom St Denis1e051412016-06-27 09:57:18 -04003548
Tom St Denis273d7aa2016-10-11 14:48:55 -04003549static ssize_t amdgpu_debugfs_wave_read(struct file *f, char __user *buf,
3550 size_t size, loff_t *pos)
3551{
3552 struct amdgpu_device *adev = f->f_inode->i_private;
3553 int r, x;
3554 ssize_t result=0;
Tom St Denis472259f2016-10-14 09:49:09 -04003555 uint32_t offset, se, sh, cu, wave, simd, data[32];
Tom St Denis273d7aa2016-10-11 14:48:55 -04003556
3557 if (size & 3 || *pos & 3)
3558 return -EINVAL;
3559
3560 /* decode offset */
3561 offset = (*pos & 0x7F);
3562 se = ((*pos >> 7) & 0xFF);
3563 sh = ((*pos >> 15) & 0xFF);
3564 cu = ((*pos >> 23) & 0xFF);
3565 wave = ((*pos >> 31) & 0xFF);
3566 simd = ((*pos >> 37) & 0xFF);
Tom St Denis273d7aa2016-10-11 14:48:55 -04003567
3568 /* switch to the specific se/sh/cu */
3569 mutex_lock(&adev->grbm_idx_mutex);
3570 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
3571
3572 x = 0;
Tom St Denis472259f2016-10-14 09:49:09 -04003573 if (adev->gfx.funcs->read_wave_data)
3574 adev->gfx.funcs->read_wave_data(adev, simd, wave, data, &x);
Tom St Denis273d7aa2016-10-11 14:48:55 -04003575
3576 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
3577 mutex_unlock(&adev->grbm_idx_mutex);
3578
Tom St Denis5ecfb3b2016-10-13 12:15:03 -04003579 if (!x)
3580 return -EINVAL;
3581
Tom St Denis472259f2016-10-14 09:49:09 -04003582 while (size && (offset < x * 4)) {
Tom St Denis273d7aa2016-10-11 14:48:55 -04003583 uint32_t value;
3584
Tom St Denis472259f2016-10-14 09:49:09 -04003585 value = data[offset >> 2];
Tom St Denis273d7aa2016-10-11 14:48:55 -04003586 r = put_user(value, (uint32_t *)buf);
3587 if (r)
3588 return r;
3589
3590 result += 4;
3591 buf += 4;
Tom St Denis472259f2016-10-14 09:49:09 -04003592 offset += 4;
Tom St Denis273d7aa2016-10-11 14:48:55 -04003593 size -= 4;
3594 }
3595
3596 return result;
3597}
3598
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003599static ssize_t amdgpu_debugfs_gpr_read(struct file *f, char __user *buf,
3600 size_t size, loff_t *pos)
3601{
3602 struct amdgpu_device *adev = f->f_inode->i_private;
3603 int r;
3604 ssize_t result = 0;
3605 uint32_t offset, se, sh, cu, wave, simd, thread, bank, *data;
3606
3607 if (size & 3 || *pos & 3)
3608 return -EINVAL;
3609
3610 /* decode offset */
3611 offset = (*pos & 0xFFF); /* in dwords */
3612 se = ((*pos >> 12) & 0xFF);
3613 sh = ((*pos >> 20) & 0xFF);
3614 cu = ((*pos >> 28) & 0xFF);
3615 wave = ((*pos >> 36) & 0xFF);
3616 simd = ((*pos >> 44) & 0xFF);
3617 thread = ((*pos >> 52) & 0xFF);
3618 bank = ((*pos >> 60) & 1);
3619
3620 data = kmalloc_array(1024, sizeof(*data), GFP_KERNEL);
3621 if (!data)
3622 return -ENOMEM;
3623
3624 /* switch to the specific se/sh/cu */
3625 mutex_lock(&adev->grbm_idx_mutex);
3626 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
3627
3628 if (bank == 0) {
3629 if (adev->gfx.funcs->read_wave_vgprs)
3630 adev->gfx.funcs->read_wave_vgprs(adev, simd, wave, thread, offset, size>>2, data);
3631 } else {
3632 if (adev->gfx.funcs->read_wave_sgprs)
3633 adev->gfx.funcs->read_wave_sgprs(adev, simd, wave, offset, size>>2, data);
3634 }
3635
3636 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
3637 mutex_unlock(&adev->grbm_idx_mutex);
3638
3639 while (size) {
3640 uint32_t value;
3641
3642 value = data[offset++];
3643 r = put_user(value, (uint32_t *)buf);
3644 if (r) {
3645 result = r;
3646 goto err;
3647 }
3648
3649 result += 4;
3650 buf += 4;
3651 size -= 4;
3652 }
3653
3654err:
3655 kfree(data);
3656 return result;
3657}
3658
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003659static const struct file_operations amdgpu_debugfs_regs_fops = {
3660 .owner = THIS_MODULE,
3661 .read = amdgpu_debugfs_regs_read,
3662 .write = amdgpu_debugfs_regs_write,
3663 .llseek = default_llseek
3664};
Tom St Denisadcec282016-04-15 13:08:44 -04003665static const struct file_operations amdgpu_debugfs_regs_didt_fops = {
3666 .owner = THIS_MODULE,
3667 .read = amdgpu_debugfs_regs_didt_read,
3668 .write = amdgpu_debugfs_regs_didt_write,
3669 .llseek = default_llseek
3670};
3671static const struct file_operations amdgpu_debugfs_regs_pcie_fops = {
3672 .owner = THIS_MODULE,
3673 .read = amdgpu_debugfs_regs_pcie_read,
3674 .write = amdgpu_debugfs_regs_pcie_write,
3675 .llseek = default_llseek
3676};
3677static const struct file_operations amdgpu_debugfs_regs_smc_fops = {
3678 .owner = THIS_MODULE,
3679 .read = amdgpu_debugfs_regs_smc_read,
3680 .write = amdgpu_debugfs_regs_smc_write,
3681 .llseek = default_llseek
3682};
3683
Tom St Denis1e051412016-06-27 09:57:18 -04003684static const struct file_operations amdgpu_debugfs_gca_config_fops = {
3685 .owner = THIS_MODULE,
3686 .read = amdgpu_debugfs_gca_config_read,
3687 .llseek = default_llseek
3688};
3689
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003690static const struct file_operations amdgpu_debugfs_sensors_fops = {
3691 .owner = THIS_MODULE,
3692 .read = amdgpu_debugfs_sensor_read,
3693 .llseek = default_llseek
3694};
3695
Tom St Denis273d7aa2016-10-11 14:48:55 -04003696static const struct file_operations amdgpu_debugfs_wave_fops = {
3697 .owner = THIS_MODULE,
3698 .read = amdgpu_debugfs_wave_read,
3699 .llseek = default_llseek
3700};
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003701static const struct file_operations amdgpu_debugfs_gpr_fops = {
3702 .owner = THIS_MODULE,
3703 .read = amdgpu_debugfs_gpr_read,
3704 .llseek = default_llseek
3705};
Tom St Denis273d7aa2016-10-11 14:48:55 -04003706
Tom St Denisadcec282016-04-15 13:08:44 -04003707static const struct file_operations *debugfs_regs[] = {
3708 &amdgpu_debugfs_regs_fops,
3709 &amdgpu_debugfs_regs_didt_fops,
3710 &amdgpu_debugfs_regs_pcie_fops,
3711 &amdgpu_debugfs_regs_smc_fops,
Tom St Denis1e051412016-06-27 09:57:18 -04003712 &amdgpu_debugfs_gca_config_fops,
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003713 &amdgpu_debugfs_sensors_fops,
Tom St Denis273d7aa2016-10-11 14:48:55 -04003714 &amdgpu_debugfs_wave_fops,
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003715 &amdgpu_debugfs_gpr_fops,
Tom St Denisadcec282016-04-15 13:08:44 -04003716};
3717
3718static const char *debugfs_regs_names[] = {
3719 "amdgpu_regs",
3720 "amdgpu_regs_didt",
3721 "amdgpu_regs_pcie",
3722 "amdgpu_regs_smc",
Tom St Denis1e051412016-06-27 09:57:18 -04003723 "amdgpu_gca_config",
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003724 "amdgpu_sensors",
Tom St Denis273d7aa2016-10-11 14:48:55 -04003725 "amdgpu_wave",
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003726 "amdgpu_gpr",
Tom St Denisadcec282016-04-15 13:08:44 -04003727};
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003728
3729static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
3730{
3731 struct drm_minor *minor = adev->ddev->primary;
3732 struct dentry *ent, *root = minor->debugfs_root;
Tom St Denisadcec282016-04-15 13:08:44 -04003733 unsigned i, j;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003734
Tom St Denisadcec282016-04-15 13:08:44 -04003735 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3736 ent = debugfs_create_file(debugfs_regs_names[i],
3737 S_IFREG | S_IRUGO, root,
3738 adev, debugfs_regs[i]);
3739 if (IS_ERR(ent)) {
3740 for (j = 0; j < i; j++) {
3741 debugfs_remove(adev->debugfs_regs[i]);
3742 adev->debugfs_regs[i] = NULL;
3743 }
3744 return PTR_ERR(ent);
3745 }
3746
3747 if (!i)
3748 i_size_write(ent->d_inode, adev->rmmio_size);
3749 adev->debugfs_regs[i] = ent;
3750 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003751
3752 return 0;
3753}
3754
3755static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev)
3756{
Tom St Denisadcec282016-04-15 13:08:44 -04003757 unsigned i;
3758
3759 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3760 if (adev->debugfs_regs[i]) {
3761 debugfs_remove(adev->debugfs_regs[i]);
3762 adev->debugfs_regs[i] = NULL;
3763 }
3764 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003765}
3766
Huang Rui4f0955f2017-05-10 23:04:06 +08003767static int amdgpu_debugfs_test_ib(struct seq_file *m, void *data)
3768{
3769 struct drm_info_node *node = (struct drm_info_node *) m->private;
3770 struct drm_device *dev = node->minor->dev;
3771 struct amdgpu_device *adev = dev->dev_private;
3772 int r = 0, i;
3773
3774 /* hold on the scheduler */
3775 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
3776 struct amdgpu_ring *ring = adev->rings[i];
3777
3778 if (!ring || !ring->sched.thread)
3779 continue;
3780 kthread_park(ring->sched.thread);
3781 }
3782
3783 seq_printf(m, "run ib test:\n");
3784 r = amdgpu_ib_ring_tests(adev);
3785 if (r)
3786 seq_printf(m, "ib ring tests failed (%d).\n", r);
3787 else
3788 seq_printf(m, "ib ring tests passed.\n");
3789
3790 /* go on the scheduler */
3791 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
3792 struct amdgpu_ring *ring = adev->rings[i];
3793
3794 if (!ring || !ring->sched.thread)
3795 continue;
3796 kthread_unpark(ring->sched.thread);
3797 }
3798
3799 return 0;
3800}
3801
3802static const struct drm_info_list amdgpu_debugfs_test_ib_ring_list[] = {
3803 {"amdgpu_test_ib", &amdgpu_debugfs_test_ib}
3804};
3805
3806static int amdgpu_debugfs_test_ib_ring_init(struct amdgpu_device *adev)
3807{
3808 return amdgpu_debugfs_add_files(adev,
3809 amdgpu_debugfs_test_ib_ring_list, 1);
3810}
3811
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003812int amdgpu_debugfs_init(struct drm_minor *minor)
3813{
3814 return 0;
3815}
Alexander Kuleshov7cebc722015-06-27 13:16:05 +06003816#else
Arnd Bergmann27bad5b2017-06-21 23:51:02 +02003817static int amdgpu_debugfs_test_ib_ring_init(struct amdgpu_device *adev)
Huang Rui4f0955f2017-05-10 23:04:06 +08003818{
3819 return 0;
3820}
Alexander Kuleshov7cebc722015-06-27 13:16:05 +06003821static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
3822{
3823 return 0;
3824}
3825static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev) { }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003826#endif