blob: 8cf58911b17b28dd2e8a9402a4928facaea9df1f [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
Yong Zhaoba997702015-11-09 17:21:45 -050058#include "amdgpu_amdkfd.h"
Rex Zhud2f52ac2017-09-22 17:47:27 +080059#include "amdgpu_pm.h"
Alex Deucherd38ceaf2015-04-20 16:55:21 -040060
Alex Deuchere2a75f82017-04-27 16:58:01 -040061MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin");
Alex Deucher2d2e5e72017-05-09 12:27:35 -040062MODULE_FIRMWARE("amdgpu/raven_gpu_info.bin");
Alex Deuchere2a75f82017-04-27 16:58:01 -040063
Shirish S2dc80b02017-05-25 10:05:25 +053064#define AMDGPU_RESUME_MS 2000
65
Alex Deucherd38ceaf2015-04-20 16:55:21 -040066static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev);
67static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev);
Huang Rui4f0955f2017-05-10 23:04:06 +080068static int amdgpu_debugfs_test_ib_ring_init(struct amdgpu_device *adev);
Kent Russelldb95e212017-08-22 12:31:43 -040069static int amdgpu_debugfs_vbios_dump_init(struct amdgpu_device *adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040070
71static const char *amdgpu_asic_name[] = {
Ken Wangda69c1612016-01-21 19:08:55 +080072 "TAHITI",
73 "PITCAIRN",
74 "VERDE",
75 "OLAND",
76 "HAINAN",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040077 "BONAIRE",
78 "KAVERI",
79 "KABINI",
80 "HAWAII",
81 "MULLINS",
82 "TOPAZ",
83 "TONGA",
David Zhang48299f92015-07-08 01:05:16 +080084 "FIJI",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040085 "CARRIZO",
Samuel Li139f4912015-10-08 14:50:27 -040086 "STONEY",
Flora Cui2cc0c0b2016-03-14 18:33:29 -040087 "POLARIS10",
88 "POLARIS11",
Junwei Zhangc4642a42016-12-14 15:32:28 -050089 "POLARIS12",
Ken Wangd4196f02016-03-09 09:28:32 +080090 "VEGA10",
Chunming Zhou2ca8a5d2016-12-07 17:31:19 +080091 "RAVEN",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040092 "LAST",
93};
94
95bool amdgpu_device_is_px(struct drm_device *dev)
96{
97 struct amdgpu_device *adev = dev->dev_private;
98
Jammy Zhou2f7d10b2015-07-22 11:29:01 +080099 if (adev->flags & AMD_IS_PX)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400100 return true;
101 return false;
102}
103
104/*
105 * MMIO register access helper functions.
106 */
107uint32_t amdgpu_mm_rreg(struct amdgpu_device *adev, uint32_t reg,
Monk Liu15d72fd2017-01-25 15:07:40 +0800108 uint32_t acc_flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400109{
Tom St Denisf4b373f2016-05-31 08:02:27 -0400110 uint32_t ret;
111
pding43ca8ef2017-10-13 15:38:35 +0800112 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev))
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800113 return amdgpu_virt_kiq_rreg(adev, reg);
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800114
Monk Liu15d72fd2017-01-25 15:07:40 +0800115 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
Tom St Denisf4b373f2016-05-31 08:02:27 -0400116 ret = readl(((void __iomem *)adev->rmmio) + (reg * 4));
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400117 else {
118 unsigned long flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400119
120 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
121 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
122 ret = readl(((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
123 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400124 }
Tom St Denisf4b373f2016-05-31 08:02:27 -0400125 trace_amdgpu_mm_rreg(adev->pdev->device, reg, ret);
126 return ret;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400127}
128
129void amdgpu_mm_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v,
Monk Liu15d72fd2017-01-25 15:07:40 +0800130 uint32_t acc_flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400131{
Tom St Denisf4b373f2016-05-31 08:02:27 -0400132 trace_amdgpu_mm_wreg(adev->pdev->device, reg, v);
Monk Liu4e99a442016-03-31 13:26:59 +0800133
Ken Wang47ed4e12017-07-04 13:11:52 +0800134 if (adev->asic_type >= CHIP_VEGA10 && reg == 0) {
135 adev->last_mm_index = v;
136 }
137
pding43ca8ef2017-10-13 15:38:35 +0800138 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev))
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800139 return amdgpu_virt_kiq_wreg(adev, reg, v);
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800140
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{
Christian Königa4a02772017-07-27 17:24:36 +0200337 return amdgpu_bo_create_kernel(adev, AMDGPU_GPU_PAGE_SIZE,
338 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
339 &adev->vram_scratch.robj,
340 &adev->vram_scratch.gpu_addr,
341 (void **)&adev->vram_scratch.ptr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400342}
343
344static void amdgpu_vram_scratch_fini(struct amdgpu_device *adev)
345{
Christian König078af1a2017-07-27 17:43:00 +0200346 amdgpu_bo_free_kernel(&adev->vram_scratch.robj, NULL, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400347}
348
349/**
350 * amdgpu_program_register_sequence - program an array of registers.
351 *
352 * @adev: amdgpu_device pointer
353 * @registers: pointer to the register array
354 * @array_size: size of the register array
355 *
356 * Programs an array or registers with and and or masks.
357 * This is a helper for setting golden registers.
358 */
359void amdgpu_program_register_sequence(struct amdgpu_device *adev,
360 const u32 *registers,
361 const u32 array_size)
362{
363 u32 tmp, reg, and_mask, or_mask;
364 int i;
365
366 if (array_size % 3)
367 return;
368
369 for (i = 0; i < array_size; i +=3) {
370 reg = registers[i + 0];
371 and_mask = registers[i + 1];
372 or_mask = registers[i + 2];
373
374 if (and_mask == 0xffffffff) {
375 tmp = or_mask;
376 } else {
377 tmp = RREG32(reg);
378 tmp &= ~and_mask;
379 tmp |= or_mask;
380 }
381 WREG32(reg, tmp);
382 }
383}
384
385void amdgpu_pci_config_reset(struct amdgpu_device *adev)
386{
387 pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
388}
389
390/*
391 * GPU doorbell aperture helpers function.
392 */
393/**
394 * amdgpu_doorbell_init - Init doorbell driver information.
395 *
396 * @adev: amdgpu_device pointer
397 *
398 * Init doorbell driver information (CIK)
399 * Returns 0 on success, error on failure.
400 */
401static int amdgpu_doorbell_init(struct amdgpu_device *adev)
402{
Christian König705e5192017-06-08 11:15:16 +0200403 /* No doorbell on SI hardware generation */
404 if (adev->asic_type < CHIP_BONAIRE) {
405 adev->doorbell.base = 0;
406 adev->doorbell.size = 0;
407 adev->doorbell.num_doorbells = 0;
408 adev->doorbell.ptr = NULL;
409 return 0;
410 }
411
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400412 /* doorbell bar mapping */
413 adev->doorbell.base = pci_resource_start(adev->pdev, 2);
414 adev->doorbell.size = pci_resource_len(adev->pdev, 2);
415
Christian Königedf600d2016-05-03 15:54:54 +0200416 adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400417 AMDGPU_DOORBELL_MAX_ASSIGNMENT+1);
418 if (adev->doorbell.num_doorbells == 0)
419 return -EINVAL;
420
Christian König8972e5d2017-03-06 13:34:57 +0100421 adev->doorbell.ptr = ioremap(adev->doorbell.base,
422 adev->doorbell.num_doorbells *
423 sizeof(u32));
424 if (adev->doorbell.ptr == NULL)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400425 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400426
427 return 0;
428}
429
430/**
431 * amdgpu_doorbell_fini - Tear down doorbell driver information.
432 *
433 * @adev: amdgpu_device pointer
434 *
435 * Tear down doorbell driver information (CIK)
436 */
437static void amdgpu_doorbell_fini(struct amdgpu_device *adev)
438{
439 iounmap(adev->doorbell.ptr);
440 adev->doorbell.ptr = NULL;
441}
442
443/**
444 * amdgpu_doorbell_get_kfd_info - Report doorbell configuration required to
445 * setup amdkfd
446 *
447 * @adev: amdgpu_device pointer
448 * @aperture_base: output returning doorbell aperture base physical address
449 * @aperture_size: output returning doorbell aperture size in bytes
450 * @start_offset: output returning # of doorbell bytes reserved for amdgpu.
451 *
452 * amdgpu and amdkfd share the doorbell aperture. amdgpu sets it up,
453 * takes doorbells required for its own rings and reports the setup to amdkfd.
454 * amdgpu reserved doorbells are at the start of the doorbell aperture.
455 */
456void amdgpu_doorbell_get_kfd_info(struct amdgpu_device *adev,
457 phys_addr_t *aperture_base,
458 size_t *aperture_size,
459 size_t *start_offset)
460{
461 /*
462 * The first num_doorbells are used by amdgpu.
463 * amdkfd takes whatever's left in the aperture.
464 */
465 if (adev->doorbell.size > adev->doorbell.num_doorbells * sizeof(u32)) {
466 *aperture_base = adev->doorbell.base;
467 *aperture_size = adev->doorbell.size;
468 *start_offset = adev->doorbell.num_doorbells * sizeof(u32);
469 } else {
470 *aperture_base = 0;
471 *aperture_size = 0;
472 *start_offset = 0;
473 }
474}
475
476/*
477 * amdgpu_wb_*()
Alex Xie455a7bc2017-05-08 21:36:03 -0400478 * Writeback is the method by which the GPU updates special pages in memory
Alex Xieea81a172017-05-08 13:41:11 -0400479 * with the status of certain GPU events (fences, ring pointers,etc.).
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400480 */
481
482/**
483 * amdgpu_wb_fini - Disable Writeback and free memory
484 *
485 * @adev: amdgpu_device pointer
486 *
487 * Disables Writeback and frees the Writeback memory (all asics).
488 * Used at driver shutdown.
489 */
490static void amdgpu_wb_fini(struct amdgpu_device *adev)
491{
492 if (adev->wb.wb_obj) {
Alex Deuchera76ed482016-10-21 15:30:36 -0400493 amdgpu_bo_free_kernel(&adev->wb.wb_obj,
494 &adev->wb.gpu_addr,
495 (void **)&adev->wb.wb);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400496 adev->wb.wb_obj = NULL;
497 }
498}
499
500/**
501 * amdgpu_wb_init- Init Writeback driver info and allocate memory
502 *
503 * @adev: amdgpu_device pointer
504 *
Alex Xie455a7bc2017-05-08 21:36:03 -0400505 * Initializes writeback and allocates writeback memory (all asics).
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400506 * Used at driver startup.
507 * Returns 0 on success or an -error on failure.
508 */
509static int amdgpu_wb_init(struct amdgpu_device *adev)
510{
511 int r;
512
513 if (adev->wb.wb_obj == NULL) {
Alex Deucher97407b62017-07-28 12:14:15 -0400514 /* AMDGPU_MAX_WB * sizeof(uint32_t) * 8 = AMDGPU_MAX_WB 256bit slots */
515 r = amdgpu_bo_create_kernel(adev, AMDGPU_MAX_WB * sizeof(uint32_t) * 8,
Alex Deuchera76ed482016-10-21 15:30:36 -0400516 PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
517 &adev->wb.wb_obj, &adev->wb.gpu_addr,
518 (void **)&adev->wb.wb);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400519 if (r) {
520 dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
521 return r;
522 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400523
524 adev->wb.num_wb = AMDGPU_MAX_WB;
525 memset(&adev->wb.used, 0, sizeof(adev->wb.used));
526
527 /* clear wb memory */
Huang Rui60a970a62017-03-15 10:13:32 +0800528 memset((char *)adev->wb.wb, 0, AMDGPU_MAX_WB * sizeof(uint32_t));
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400529 }
530
531 return 0;
532}
533
534/**
535 * amdgpu_wb_get - Allocate a wb entry
536 *
537 * @adev: amdgpu_device pointer
538 * @wb: wb index
539 *
540 * Allocate a wb slot for use by the driver (all asics).
541 * Returns 0 on success or -EINVAL on failure.
542 */
543int amdgpu_wb_get(struct amdgpu_device *adev, u32 *wb)
544{
545 unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
Alex Deucher97407b62017-07-28 12:14:15 -0400546
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400547 if (offset < adev->wb.num_wb) {
548 __set_bit(offset, adev->wb.used);
Alex Deucher97407b62017-07-28 12:14:15 -0400549 *wb = offset * 8; /* convert to dw offset */
Monk Liu0915fdb2017-06-19 10:19:41 -0400550 return 0;
551 } else {
552 return -EINVAL;
553 }
554}
555
Ken Wang70142852016-03-18 15:08:49 +0800556/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400557 * amdgpu_wb_free - Free a wb entry
558 *
559 * @adev: amdgpu_device pointer
560 * @wb: wb index
561 *
562 * Free a wb slot allocated for use by the driver (all asics)
563 */
564void amdgpu_wb_free(struct amdgpu_device *adev, u32 wb)
565{
566 if (wb < adev->wb.num_wb)
567 __clear_bit(wb, adev->wb.used);
568}
569
570/**
571 * amdgpu_vram_location - try to find VRAM location
572 * @adev: amdgpu device structure holding all necessary informations
573 * @mc: memory controller structure holding memory informations
574 * @base: base address at which to put VRAM
575 *
Alex Xie455a7bc2017-05-08 21:36:03 -0400576 * Function will try to place VRAM at base address provided
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400577 * as parameter (which is so far either PCI aperture address or
578 * for IGP TOM base address).
579 *
580 * If there is not enough space to fit the unvisible VRAM in the 32bits
581 * address space then we limit the VRAM size to the aperture.
582 *
583 * Note: We don't explicitly enforce VRAM start to be aligned on VRAM size,
584 * this shouldn't be a problem as we are using the PCI aperture as a reference.
585 * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but
586 * not IGP.
587 *
588 * Note: we use mc_vram_size as on some board we need to program the mc to
589 * cover the whole aperture even if VRAM size is inferior to aperture size
590 * Novell bug 204882 + along with lots of ubuntu ones
591 *
592 * Note: when limiting vram it's safe to overwritte real_vram_size because
593 * we are not in case where real_vram_size is inferior to mc_vram_size (ie
594 * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu
595 * ones)
596 *
597 * Note: IGP TOM addr should be the same as the aperture addr, we don't
Alex Xie455a7bc2017-05-08 21:36:03 -0400598 * explicitly check for that though.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400599 *
600 * FIXME: when reducing VRAM size align new size on power of 2.
601 */
602void amdgpu_vram_location(struct amdgpu_device *adev, struct amdgpu_mc *mc, u64 base)
603{
604 uint64_t limit = (uint64_t)amdgpu_vram_limit << 20;
605
606 mc->vram_start = base;
607 if (mc->mc_vram_size > (adev->mc.mc_mask - base + 1)) {
608 dev_warn(adev->dev, "limiting VRAM to PCI aperture size\n");
609 mc->real_vram_size = mc->aper_size;
610 mc->mc_vram_size = mc->aper_size;
611 }
612 mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
613 if (limit && limit < mc->real_vram_size)
614 mc->real_vram_size = limit;
615 dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
616 mc->mc_vram_size >> 20, mc->vram_start,
617 mc->vram_end, mc->real_vram_size >> 20);
618}
619
620/**
Christian König6f02a692017-07-07 11:56:59 +0200621 * amdgpu_gart_location - try to find GTT location
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400622 * @adev: amdgpu device structure holding all necessary informations
623 * @mc: memory controller structure holding memory informations
624 *
625 * Function will place try to place GTT before or after VRAM.
626 *
627 * If GTT size is bigger than space left then we ajust GTT size.
628 * Thus function will never fails.
629 *
630 * FIXME: when reducing GTT size align new size on power of 2.
631 */
Christian König6f02a692017-07-07 11:56:59 +0200632void amdgpu_gart_location(struct amdgpu_device *adev, struct amdgpu_mc *mc)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400633{
634 u64 size_af, size_bf;
635
Christian Königed21c042017-07-06 22:26:05 +0200636 size_af = adev->mc.mc_mask - mc->vram_end;
637 size_bf = mc->vram_start;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400638 if (size_bf > size_af) {
Christian König6f02a692017-07-07 11:56:59 +0200639 if (mc->gart_size > size_bf) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400640 dev_warn(adev->dev, "limiting GTT\n");
Christian König6f02a692017-07-07 11:56:59 +0200641 mc->gart_size = size_bf;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400642 }
Christian König6f02a692017-07-07 11:56:59 +0200643 mc->gart_start = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400644 } else {
Christian König6f02a692017-07-07 11:56:59 +0200645 if (mc->gart_size > size_af) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400646 dev_warn(adev->dev, "limiting GTT\n");
Christian König6f02a692017-07-07 11:56:59 +0200647 mc->gart_size = size_af;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400648 }
Christian König6f02a692017-07-07 11:56:59 +0200649 mc->gart_start = mc->vram_end + 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400650 }
Christian König6f02a692017-07-07 11:56:59 +0200651 mc->gart_end = mc->gart_start + mc->gart_size - 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400652 dev_info(adev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
Christian König6f02a692017-07-07 11:56:59 +0200653 mc->gart_size >> 20, mc->gart_start, mc->gart_end);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400654}
655
656/*
Horace Chena05502e2017-09-29 14:41:57 +0800657 * Firmware Reservation functions
658 */
659/**
660 * amdgpu_fw_reserve_vram_fini - free fw reserved vram
661 *
662 * @adev: amdgpu_device pointer
663 *
664 * free fw reserved vram if it has been reserved.
665 */
666void amdgpu_fw_reserve_vram_fini(struct amdgpu_device *adev)
667{
668 amdgpu_bo_free_kernel(&adev->fw_vram_usage.reserved_bo,
669 NULL, &adev->fw_vram_usage.va);
670}
671
672/**
673 * amdgpu_fw_reserve_vram_init - create bo vram reservation from fw
674 *
675 * @adev: amdgpu_device pointer
676 *
677 * create bo vram reservation from fw.
678 */
679int amdgpu_fw_reserve_vram_init(struct amdgpu_device *adev)
680{
681 int r = 0;
682 u64 gpu_addr;
683 u64 vram_size = adev->mc.visible_vram_size;
684
685 adev->fw_vram_usage.va = NULL;
686 adev->fw_vram_usage.reserved_bo = NULL;
687
688 if (adev->fw_vram_usage.size > 0 &&
689 adev->fw_vram_usage.size <= vram_size) {
690
691 r = amdgpu_bo_create(adev, adev->fw_vram_usage.size,
692 PAGE_SIZE, true, 0,
693 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
694 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS, NULL, NULL, 0,
695 &adev->fw_vram_usage.reserved_bo);
696 if (r)
697 goto error_create;
698
699 r = amdgpu_bo_reserve(adev->fw_vram_usage.reserved_bo, false);
700 if (r)
701 goto error_reserve;
702 r = amdgpu_bo_pin_restricted(adev->fw_vram_usage.reserved_bo,
703 AMDGPU_GEM_DOMAIN_VRAM,
704 adev->fw_vram_usage.start_offset,
705 (adev->fw_vram_usage.start_offset +
706 adev->fw_vram_usage.size), &gpu_addr);
707 if (r)
708 goto error_pin;
709 r = amdgpu_bo_kmap(adev->fw_vram_usage.reserved_bo,
710 &adev->fw_vram_usage.va);
711 if (r)
712 goto error_kmap;
713
714 amdgpu_bo_unreserve(adev->fw_vram_usage.reserved_bo);
715 }
716 return r;
717
718error_kmap:
719 amdgpu_bo_unpin(adev->fw_vram_usage.reserved_bo);
720error_pin:
721 amdgpu_bo_unreserve(adev->fw_vram_usage.reserved_bo);
722error_reserve:
723 amdgpu_bo_unref(&adev->fw_vram_usage.reserved_bo);
724error_create:
725 adev->fw_vram_usage.va = NULL;
726 adev->fw_vram_usage.reserved_bo = NULL;
727 return r;
728}
729
730
731/*
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400732 * GPU helpers function.
733 */
734/**
Jim Quc836fec2017-02-10 15:59:59 +0800735 * amdgpu_need_post - check if the hw need post or not
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400736 *
737 * @adev: amdgpu_device pointer
738 *
Jim Quc836fec2017-02-10 15:59:59 +0800739 * Check if the asic has been initialized (all asics) at driver startup
740 * or post is needed if hw reset is performed.
741 * Returns true if need or false if not.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400742 */
Jim Quc836fec2017-02-10 15:59:59 +0800743bool amdgpu_need_post(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400744{
745 uint32_t reg;
746
Monk Liubec86372016-09-14 19:38:08 +0800747 if (amdgpu_sriov_vf(adev))
748 return false;
749
750 if (amdgpu_passthrough(adev)) {
Monk Liu1da2c322016-11-11 11:24:29 +0800751 /* for FIJI: In whole GPU pass-through virtualization case, after VM reboot
752 * some old smc fw still need driver do vPost otherwise gpu hang, while
753 * those smc fw version above 22.15 doesn't have this flaw, so we force
754 * vpost executed for smc version below 22.15
Monk Liubec86372016-09-14 19:38:08 +0800755 */
756 if (adev->asic_type == CHIP_FIJI) {
757 int err;
758 uint32_t fw_ver;
759 err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
760 /* force vPost if error occured */
761 if (err)
762 return true;
763
764 fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
Monk Liu1da2c322016-11-11 11:24:29 +0800765 if (fw_ver < 0x00160e00)
766 return true;
Monk Liubec86372016-09-14 19:38:08 +0800767 }
Monk Liubec86372016-09-14 19:38:08 +0800768 }
pding91fe77e2017-10-19 09:38:39 +0800769
770 if (adev->has_hw_reset) {
771 adev->has_hw_reset = false;
772 return true;
773 }
774
775 /* bios scratch used on CIK+ */
776 if (adev->asic_type >= CHIP_BONAIRE)
777 return amdgpu_atombios_scratch_need_asic_init(adev);
778
779 /* check MEM_SIZE for older asics */
780 reg = amdgpu_asic_get_config_memsize(adev);
781
782 if ((reg != 0) && (reg != 0xffffffff))
783 return false;
784
785 return true;
Monk Liubec86372016-09-14 19:38:08 +0800786}
787
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400788/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400789 * amdgpu_dummy_page_init - init dummy page used by the driver
790 *
791 * @adev: amdgpu_device pointer
792 *
793 * Allocate the dummy page used by the driver (all asics).
794 * This dummy page is used by the driver as a filler for gart entries
795 * when pages are taken out of the GART
796 * Returns 0 on sucess, -ENOMEM on failure.
797 */
798int amdgpu_dummy_page_init(struct amdgpu_device *adev)
799{
800 if (adev->dummy_page.page)
801 return 0;
802 adev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
803 if (adev->dummy_page.page == NULL)
804 return -ENOMEM;
805 adev->dummy_page.addr = pci_map_page(adev->pdev, adev->dummy_page.page,
806 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
807 if (pci_dma_mapping_error(adev->pdev, adev->dummy_page.addr)) {
808 dev_err(&adev->pdev->dev, "Failed to DMA MAP the dummy page\n");
809 __free_page(adev->dummy_page.page);
810 adev->dummy_page.page = NULL;
811 return -ENOMEM;
812 }
813 return 0;
814}
815
816/**
817 * amdgpu_dummy_page_fini - free dummy page used by the driver
818 *
819 * @adev: amdgpu_device pointer
820 *
821 * Frees the dummy page used by the driver (all asics).
822 */
823void amdgpu_dummy_page_fini(struct amdgpu_device *adev)
824{
825 if (adev->dummy_page.page == NULL)
826 return;
827 pci_unmap_page(adev->pdev, adev->dummy_page.addr,
828 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
829 __free_page(adev->dummy_page.page);
830 adev->dummy_page.page = NULL;
831}
832
833
834/* ATOM accessor methods */
835/*
836 * ATOM is an interpreted byte code stored in tables in the vbios. The
837 * driver registers callbacks to access registers and the interpreter
838 * in the driver parses the tables and executes then to program specific
839 * actions (set display modes, asic init, etc.). See amdgpu_atombios.c,
840 * atombios.h, and atom.c
841 */
842
843/**
844 * cail_pll_read - read PLL register
845 *
846 * @info: atom card_info pointer
847 * @reg: PLL register offset
848 *
849 * Provides a PLL register accessor for the atom interpreter (r4xx+).
850 * Returns the value of the PLL register.
851 */
852static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
853{
854 return 0;
855}
856
857/**
858 * cail_pll_write - write PLL register
859 *
860 * @info: atom card_info pointer
861 * @reg: PLL register offset
862 * @val: value to write to the pll register
863 *
864 * Provides a PLL register accessor for the atom interpreter (r4xx+).
865 */
866static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
867{
868
869}
870
871/**
872 * cail_mc_read - read MC (Memory Controller) register
873 *
874 * @info: atom card_info pointer
875 * @reg: MC register offset
876 *
877 * Provides an MC register accessor for the atom interpreter (r4xx+).
878 * Returns the value of the MC register.
879 */
880static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
881{
882 return 0;
883}
884
885/**
886 * cail_mc_write - write MC (Memory Controller) register
887 *
888 * @info: atom card_info pointer
889 * @reg: MC register offset
890 * @val: value to write to the pll register
891 *
892 * Provides a MC register accessor for the atom interpreter (r4xx+).
893 */
894static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
895{
896
897}
898
899/**
900 * cail_reg_write - write MMIO register
901 *
902 * @info: atom card_info pointer
903 * @reg: MMIO register offset
904 * @val: value to write to the pll register
905 *
906 * Provides a MMIO register accessor for the atom interpreter (r4xx+).
907 */
908static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
909{
910 struct amdgpu_device *adev = info->dev->dev_private;
911
912 WREG32(reg, val);
913}
914
915/**
916 * cail_reg_read - read MMIO register
917 *
918 * @info: atom card_info pointer
919 * @reg: MMIO register offset
920 *
921 * Provides an MMIO register accessor for the atom interpreter (r4xx+).
922 * Returns the value of the MMIO register.
923 */
924static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
925{
926 struct amdgpu_device *adev = info->dev->dev_private;
927 uint32_t r;
928
929 r = RREG32(reg);
930 return r;
931}
932
933/**
934 * cail_ioreg_write - write IO register
935 *
936 * @info: atom card_info pointer
937 * @reg: IO register offset
938 * @val: value to write to the pll register
939 *
940 * Provides a IO register accessor for the atom interpreter (r4xx+).
941 */
942static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
943{
944 struct amdgpu_device *adev = info->dev->dev_private;
945
946 WREG32_IO(reg, val);
947}
948
949/**
950 * cail_ioreg_read - read IO register
951 *
952 * @info: atom card_info pointer
953 * @reg: IO register offset
954 *
955 * Provides an IO register accessor for the atom interpreter (r4xx+).
956 * Returns the value of the IO register.
957 */
958static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
959{
960 struct amdgpu_device *adev = info->dev->dev_private;
961 uint32_t r;
962
963 r = RREG32_IO(reg);
964 return r;
965}
966
Kent Russell5b41d942017-08-22 12:31:43 -0400967static ssize_t amdgpu_atombios_get_vbios_version(struct device *dev,
968 struct device_attribute *attr,
969 char *buf)
970{
971 struct drm_device *ddev = dev_get_drvdata(dev);
972 struct amdgpu_device *adev = ddev->dev_private;
973 struct atom_context *ctx = adev->mode_info.atom_context;
974
975 return snprintf(buf, PAGE_SIZE, "%s\n", ctx->vbios_version);
976}
977
978static DEVICE_ATTR(vbios_version, 0444, amdgpu_atombios_get_vbios_version,
979 NULL);
980
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400981/**
982 * amdgpu_atombios_fini - free the driver info and callbacks for atombios
983 *
984 * @adev: amdgpu_device pointer
985 *
986 * Frees the driver info and register access callbacks for the ATOM
987 * interpreter (r4xx+).
988 * Called at driver shutdown.
989 */
990static void amdgpu_atombios_fini(struct amdgpu_device *adev)
991{
Monk Liu89e0ec9f2016-05-27 19:34:11 +0800992 if (adev->mode_info.atom_context) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400993 kfree(adev->mode_info.atom_context->scratch);
Monk Liu89e0ec9f2016-05-27 19:34:11 +0800994 kfree(adev->mode_info.atom_context->iio);
995 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400996 kfree(adev->mode_info.atom_context);
997 adev->mode_info.atom_context = NULL;
998 kfree(adev->mode_info.atom_card_info);
999 adev->mode_info.atom_card_info = NULL;
Kent Russell5b41d942017-08-22 12:31:43 -04001000 device_remove_file(adev->dev, &dev_attr_vbios_version);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001001}
1002
1003/**
1004 * amdgpu_atombios_init - init the driver info and callbacks for atombios
1005 *
1006 * @adev: amdgpu_device pointer
1007 *
1008 * Initializes the driver info and register access callbacks for the
1009 * ATOM interpreter (r4xx+).
1010 * Returns 0 on sucess, -ENOMEM on failure.
1011 * Called at driver startup.
1012 */
1013static int amdgpu_atombios_init(struct amdgpu_device *adev)
1014{
1015 struct card_info *atom_card_info =
1016 kzalloc(sizeof(struct card_info), GFP_KERNEL);
Kent Russell5b41d942017-08-22 12:31:43 -04001017 int ret;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001018
1019 if (!atom_card_info)
1020 return -ENOMEM;
1021
1022 adev->mode_info.atom_card_info = atom_card_info;
1023 atom_card_info->dev = adev->ddev;
1024 atom_card_info->reg_read = cail_reg_read;
1025 atom_card_info->reg_write = cail_reg_write;
1026 /* needed for iio ops */
1027 if (adev->rio_mem) {
1028 atom_card_info->ioreg_read = cail_ioreg_read;
1029 atom_card_info->ioreg_write = cail_ioreg_write;
1030 } else {
Amber Linb64a18c2017-01-04 08:06:58 -05001031 DRM_INFO("PCI I/O BAR is not found. Using MMIO to access ATOM BIOS\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001032 atom_card_info->ioreg_read = cail_reg_read;
1033 atom_card_info->ioreg_write = cail_reg_write;
1034 }
1035 atom_card_info->mc_read = cail_mc_read;
1036 atom_card_info->mc_write = cail_mc_write;
1037 atom_card_info->pll_read = cail_pll_read;
1038 atom_card_info->pll_write = cail_pll_write;
1039
1040 adev->mode_info.atom_context = amdgpu_atom_parse(atom_card_info, adev->bios);
1041 if (!adev->mode_info.atom_context) {
1042 amdgpu_atombios_fini(adev);
1043 return -ENOMEM;
1044 }
1045
1046 mutex_init(&adev->mode_info.atom_context->mutex);
Alex Deuchera5bde2f2016-09-23 16:23:41 -04001047 if (adev->is_atom_fw) {
1048 amdgpu_atomfirmware_scratch_regs_init(adev);
1049 amdgpu_atomfirmware_allocate_fb_scratch(adev);
1050 } else {
1051 amdgpu_atombios_scratch_regs_init(adev);
1052 amdgpu_atombios_allocate_fb_scratch(adev);
1053 }
Kent Russell5b41d942017-08-22 12:31:43 -04001054
1055 ret = device_create_file(adev->dev, &dev_attr_vbios_version);
1056 if (ret) {
1057 DRM_ERROR("Failed to create device file for VBIOS version\n");
1058 return ret;
1059 }
1060
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001061 return 0;
1062}
1063
1064/* if we get transitioned to only one device, take VGA back */
1065/**
1066 * amdgpu_vga_set_decode - enable/disable vga decode
1067 *
1068 * @cookie: amdgpu_device pointer
1069 * @state: enable/disable vga decode
1070 *
1071 * Enable/disable vga decode (all asics).
1072 * Returns VGA resource flags.
1073 */
1074static unsigned int amdgpu_vga_set_decode(void *cookie, bool state)
1075{
1076 struct amdgpu_device *adev = cookie;
1077 amdgpu_asic_set_vga_state(adev, state);
1078 if (state)
1079 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
1080 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1081 else
1082 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1083}
1084
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001085static void amdgpu_check_block_size(struct amdgpu_device *adev)
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001086{
1087 /* defines number of bits in page table versus page directory,
1088 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
1089 * page table and the remaining bits are in the page directory */
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001090 if (amdgpu_vm_block_size == -1)
1091 return;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001092
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001093 if (amdgpu_vm_block_size < 9) {
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001094 dev_warn(adev->dev, "VM page table size (%d) too small\n",
1095 amdgpu_vm_block_size);
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001096 goto def_value;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001097 }
1098
1099 if (amdgpu_vm_block_size > 24 ||
1100 (amdgpu_vm_size * 1024) < (1ull << amdgpu_vm_block_size)) {
1101 dev_warn(adev->dev, "VM page table size (%d) too large\n",
1102 amdgpu_vm_block_size);
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001103 goto def_value;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001104 }
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001105
1106 return;
1107
1108def_value:
1109 amdgpu_vm_block_size = -1;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001110}
1111
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001112static void amdgpu_check_vm_size(struct amdgpu_device *adev)
1113{
Alex Deucher64dab072017-06-15 18:20:09 -04001114 /* no need to check the default value */
1115 if (amdgpu_vm_size == -1)
1116 return;
1117
Alex Deucher76117502017-06-21 12:31:41 -04001118 if (!is_power_of_2(amdgpu_vm_size)) {
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001119 dev_warn(adev->dev, "VM size (%d) must be a power of 2\n",
1120 amdgpu_vm_size);
1121 goto def_value;
1122 }
1123
1124 if (amdgpu_vm_size < 1) {
1125 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
1126 amdgpu_vm_size);
1127 goto def_value;
1128 }
1129
1130 /*
1131 * Max GPUVM size for Cayman, SI, CI VI are 40 bits.
1132 */
1133 if (amdgpu_vm_size > 1024) {
1134 dev_warn(adev->dev, "VM size (%d) too large, max is 1TB\n",
1135 amdgpu_vm_size);
1136 goto def_value;
1137 }
1138
1139 return;
1140
1141def_value:
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001142 amdgpu_vm_size = -1;
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001143}
1144
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001145/**
1146 * amdgpu_check_arguments - validate module params
1147 *
1148 * @adev: amdgpu_device pointer
1149 *
1150 * Validates certain module parameters and updates
1151 * the associated values used by the driver (all asics).
1152 */
1153static void amdgpu_check_arguments(struct amdgpu_device *adev)
1154{
Chunming Zhou5b011232015-12-10 17:34:33 +08001155 if (amdgpu_sched_jobs < 4) {
1156 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
1157 amdgpu_sched_jobs);
1158 amdgpu_sched_jobs = 4;
Alex Deucher76117502017-06-21 12:31:41 -04001159 } else if (!is_power_of_2(amdgpu_sched_jobs)){
Chunming Zhou5b011232015-12-10 17:34:33 +08001160 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
1161 amdgpu_sched_jobs);
1162 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
1163 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001164
Alex Deucher83e74db2017-08-21 11:58:25 -04001165 if (amdgpu_gart_size != -1 && amdgpu_gart_size < 32) {
Christian Königf9321cc2017-07-07 13:44:05 +02001166 /* gart size must be greater or equal to 32M */
1167 dev_warn(adev->dev, "gart size (%d) too small\n",
1168 amdgpu_gart_size);
Alex Deucher83e74db2017-08-21 11:58:25 -04001169 amdgpu_gart_size = -1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001170 }
1171
Christian König36d38372017-07-07 13:17:45 +02001172 if (amdgpu_gtt_size != -1 && amdgpu_gtt_size < 32) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001173 /* gtt size must be greater or equal to 32M */
Christian König36d38372017-07-07 13:17:45 +02001174 dev_warn(adev->dev, "gtt size (%d) too small\n",
1175 amdgpu_gtt_size);
1176 amdgpu_gtt_size = -1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001177 }
1178
Roger Hed07f14b2017-08-15 16:05:59 +08001179 /* valid range is between 4 and 9 inclusive */
1180 if (amdgpu_vm_fragment_size != -1 &&
1181 (amdgpu_vm_fragment_size > 9 || amdgpu_vm_fragment_size < 4)) {
1182 dev_warn(adev->dev, "valid range is between 4 and 9\n");
1183 amdgpu_vm_fragment_size = -1;
1184 }
1185
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001186 amdgpu_check_vm_size(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001187
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001188 amdgpu_check_block_size(adev);
Christian König6a7f76e2016-08-24 15:51:49 +02001189
jimqu526bae32016-11-07 09:53:10 +08001190 if (amdgpu_vram_page_split != -1 && (amdgpu_vram_page_split < 16 ||
Alex Deucher76117502017-06-21 12:31:41 -04001191 !is_power_of_2(amdgpu_vram_page_split))) {
Christian König6a7f76e2016-08-24 15:51:49 +02001192 dev_warn(adev->dev, "invalid VRAM page split (%d)\n",
1193 amdgpu_vram_page_split);
1194 amdgpu_vram_page_split = 1024;
1195 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001196}
1197
1198/**
1199 * amdgpu_switcheroo_set_state - set switcheroo state
1200 *
1201 * @pdev: pci dev pointer
Lukas Wunner16944672015-09-05 11:17:35 +02001202 * @state: vga_switcheroo state
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001203 *
1204 * Callback for the switcheroo driver. Suspends or resumes the
1205 * the asics before or after it is powered up using ACPI methods.
1206 */
1207static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1208{
1209 struct drm_device *dev = pci_get_drvdata(pdev);
1210
1211 if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
1212 return;
1213
1214 if (state == VGA_SWITCHEROO_ON) {
Joe Perches7ca85292017-02-28 04:55:52 -08001215 pr_info("amdgpu: switched on\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001216 /* don't suspend or resume card normally */
1217 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1218
Alex Deucher810ddc32016-08-23 13:25:49 -04001219 amdgpu_device_resume(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001220
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001221 dev->switch_power_state = DRM_SWITCH_POWER_ON;
1222 drm_kms_helper_poll_enable(dev);
1223 } else {
Joe Perches7ca85292017-02-28 04:55:52 -08001224 pr_info("amdgpu: switched off\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001225 drm_kms_helper_poll_disable(dev);
1226 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Alex Deucher810ddc32016-08-23 13:25:49 -04001227 amdgpu_device_suspend(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001228 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1229 }
1230}
1231
1232/**
1233 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1234 *
1235 * @pdev: pci dev pointer
1236 *
1237 * Callback for the switcheroo driver. Check of the switcheroo
1238 * state can be changed.
1239 * Returns true if the state can be changed, false if not.
1240 */
1241static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1242{
1243 struct drm_device *dev = pci_get_drvdata(pdev);
1244
1245 /*
1246 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1247 * locking inversion with the driver load path. And the access here is
1248 * completely racy anyway. So don't bother with locking for now.
1249 */
1250 return dev->open_count == 0;
1251}
1252
1253static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1254 .set_gpu_state = amdgpu_switcheroo_set_state,
1255 .reprobe = NULL,
1256 .can_switch = amdgpu_switcheroo_can_switch,
1257};
1258
1259int amdgpu_set_clockgating_state(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001260 enum amd_ip_block_type block_type,
1261 enum amd_clockgating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001262{
1263 int i, r = 0;
1264
1265 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001266 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001267 continue;
Rex Zhuc7228652017-02-22 15:33:46 +08001268 if (adev->ip_blocks[i].version->type != block_type)
1269 continue;
1270 if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
1271 continue;
1272 r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
1273 (void *)adev, state);
1274 if (r)
1275 DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
1276 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001277 }
1278 return r;
1279}
1280
1281int amdgpu_set_powergating_state(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001282 enum amd_ip_block_type block_type,
1283 enum amd_powergating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001284{
1285 int i, r = 0;
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;
Rex Zhuc7228652017-02-22 15:33:46 +08001290 if (adev->ip_blocks[i].version->type != block_type)
1291 continue;
1292 if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
1293 continue;
1294 r = adev->ip_blocks[i].version->funcs->set_powergating_state(
1295 (void *)adev, state);
1296 if (r)
1297 DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
1298 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001299 }
1300 return r;
1301}
1302
Huang Rui6cb2d4e2017-01-05 18:44:41 +08001303void amdgpu_get_clockgating_state(struct amdgpu_device *adev, u32 *flags)
1304{
1305 int i;
1306
1307 for (i = 0; i < adev->num_ip_blocks; i++) {
1308 if (!adev->ip_blocks[i].status.valid)
1309 continue;
1310 if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
1311 adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
1312 }
1313}
1314
Alex Deucher5dbbb602016-06-23 11:41:04 -04001315int amdgpu_wait_for_idle(struct amdgpu_device *adev,
1316 enum amd_ip_block_type block_type)
1317{
1318 int i, r;
1319
1320 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001321 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001322 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001323 if (adev->ip_blocks[i].version->type == block_type) {
1324 r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -04001325 if (r)
1326 return r;
1327 break;
1328 }
1329 }
1330 return 0;
1331
1332}
1333
1334bool amdgpu_is_idle(struct amdgpu_device *adev,
1335 enum amd_ip_block_type block_type)
1336{
1337 int i;
1338
1339 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001340 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001341 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001342 if (adev->ip_blocks[i].version->type == block_type)
1343 return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -04001344 }
1345 return true;
1346
1347}
1348
Alex Deuchera1255102016-10-13 17:41:13 -04001349struct amdgpu_ip_block * amdgpu_get_ip_block(struct amdgpu_device *adev,
1350 enum amd_ip_block_type type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001351{
1352 int i;
1353
1354 for (i = 0; i < adev->num_ip_blocks; i++)
Alex Deuchera1255102016-10-13 17:41:13 -04001355 if (adev->ip_blocks[i].version->type == type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001356 return &adev->ip_blocks[i];
1357
1358 return NULL;
1359}
1360
1361/**
1362 * amdgpu_ip_block_version_cmp
1363 *
1364 * @adev: amdgpu_device pointer
yanyang15fc3aee2015-05-22 14:39:35 -04001365 * @type: enum amd_ip_block_type
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001366 * @major: major version
1367 * @minor: minor version
1368 *
1369 * return 0 if equal or greater
1370 * return 1 if smaller or the ip_block doesn't exist
1371 */
1372int amdgpu_ip_block_version_cmp(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001373 enum amd_ip_block_type type,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001374 u32 major, u32 minor)
1375{
Alex Deuchera1255102016-10-13 17:41:13 -04001376 struct amdgpu_ip_block *ip_block = amdgpu_get_ip_block(adev, type);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001377
Alex Deuchera1255102016-10-13 17:41:13 -04001378 if (ip_block && ((ip_block->version->major > major) ||
1379 ((ip_block->version->major == major) &&
1380 (ip_block->version->minor >= minor))))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001381 return 0;
1382
1383 return 1;
1384}
1385
Alex Deuchera1255102016-10-13 17:41:13 -04001386/**
1387 * amdgpu_ip_block_add
1388 *
1389 * @adev: amdgpu_device pointer
1390 * @ip_block_version: pointer to the IP to add
1391 *
1392 * Adds the IP block driver information to the collection of IPs
1393 * on the asic.
1394 */
1395int amdgpu_ip_block_add(struct amdgpu_device *adev,
1396 const struct amdgpu_ip_block_version *ip_block_version)
1397{
1398 if (!ip_block_version)
1399 return -EINVAL;
1400
Huang Ruia0bae352017-05-03 09:52:06 +08001401 DRM_DEBUG("add ip block number %d <%s>\n", adev->num_ip_blocks,
1402 ip_block_version->funcs->name);
1403
Alex Deuchera1255102016-10-13 17:41:13 -04001404 adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
1405
1406 return 0;
1407}
1408
Alex Deucher483ef982016-09-30 12:43:04 -04001409static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
Emily Deng9accf2f2016-08-10 16:01:25 +08001410{
1411 adev->enable_virtual_display = false;
1412
1413 if (amdgpu_virtual_display) {
1414 struct drm_device *ddev = adev->ddev;
1415 const char *pci_address_name = pci_name(ddev->pdev);
Emily Deng0f663562016-09-30 13:02:18 -04001416 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
Emily Deng9accf2f2016-08-10 16:01:25 +08001417
1418 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1419 pciaddstr_tmp = pciaddstr;
Emily Deng0f663562016-09-30 13:02:18 -04001420 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1421 pciaddname = strsep(&pciaddname_tmp, ",");
Yintian Tao967de2a2017-01-22 15:16:51 +08001422 if (!strcmp("all", pciaddname)
1423 || !strcmp(pci_address_name, pciaddname)) {
Emily Deng0f663562016-09-30 13:02:18 -04001424 long num_crtc;
1425 int res = -1;
1426
Emily Deng9accf2f2016-08-10 16:01:25 +08001427 adev->enable_virtual_display = true;
Emily Deng0f663562016-09-30 13:02:18 -04001428
1429 if (pciaddname_tmp)
1430 res = kstrtol(pciaddname_tmp, 10,
1431 &num_crtc);
1432
1433 if (!res) {
1434 if (num_crtc < 1)
1435 num_crtc = 1;
1436 if (num_crtc > 6)
1437 num_crtc = 6;
1438 adev->mode_info.num_crtc = num_crtc;
1439 } else {
1440 adev->mode_info.num_crtc = 1;
1441 }
Emily Deng9accf2f2016-08-10 16:01:25 +08001442 break;
1443 }
1444 }
1445
Emily Deng0f663562016-09-30 13:02:18 -04001446 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
1447 amdgpu_virtual_display, pci_address_name,
1448 adev->enable_virtual_display, adev->mode_info.num_crtc);
Emily Deng9accf2f2016-08-10 16:01:25 +08001449
1450 kfree(pciaddstr);
1451 }
1452}
1453
Alex Deuchere2a75f82017-04-27 16:58:01 -04001454static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev)
1455{
Alex Deuchere2a75f82017-04-27 16:58:01 -04001456 const char *chip_name;
1457 char fw_name[30];
1458 int err;
1459 const struct gpu_info_firmware_header_v1_0 *hdr;
1460
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001461 adev->firmware.gpu_info_fw = NULL;
1462
Alex Deuchere2a75f82017-04-27 16:58:01 -04001463 switch (adev->asic_type) {
1464 case CHIP_TOPAZ:
1465 case CHIP_TONGA:
1466 case CHIP_FIJI:
1467 case CHIP_POLARIS11:
1468 case CHIP_POLARIS10:
1469 case CHIP_POLARIS12:
1470 case CHIP_CARRIZO:
1471 case CHIP_STONEY:
1472#ifdef CONFIG_DRM_AMDGPU_SI
1473 case CHIP_VERDE:
1474 case CHIP_TAHITI:
1475 case CHIP_PITCAIRN:
1476 case CHIP_OLAND:
1477 case CHIP_HAINAN:
1478#endif
1479#ifdef CONFIG_DRM_AMDGPU_CIK
1480 case CHIP_BONAIRE:
1481 case CHIP_HAWAII:
1482 case CHIP_KAVERI:
1483 case CHIP_KABINI:
1484 case CHIP_MULLINS:
1485#endif
1486 default:
1487 return 0;
1488 case CHIP_VEGA10:
1489 chip_name = "vega10";
1490 break;
Alex Deucher2d2e5e72017-05-09 12:27:35 -04001491 case CHIP_RAVEN:
1492 chip_name = "raven";
1493 break;
Alex Deuchere2a75f82017-04-27 16:58:01 -04001494 }
1495
1496 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name);
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001497 err = request_firmware(&adev->firmware.gpu_info_fw, fw_name, adev->dev);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001498 if (err) {
1499 dev_err(adev->dev,
1500 "Failed to load gpu_info firmware \"%s\"\n",
1501 fw_name);
1502 goto out;
1503 }
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001504 err = amdgpu_ucode_validate(adev->firmware.gpu_info_fw);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001505 if (err) {
1506 dev_err(adev->dev,
1507 "Failed to validate gpu_info firmware \"%s\"\n",
1508 fw_name);
1509 goto out;
1510 }
1511
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001512 hdr = (const struct gpu_info_firmware_header_v1_0 *)adev->firmware.gpu_info_fw->data;
Alex Deuchere2a75f82017-04-27 16:58:01 -04001513 amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
1514
1515 switch (hdr->version_major) {
1516 case 1:
1517 {
1518 const struct gpu_info_firmware_v1_0 *gpu_info_fw =
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001519 (const struct gpu_info_firmware_v1_0 *)(adev->firmware.gpu_info_fw->data +
Alex Deuchere2a75f82017-04-27 16:58:01 -04001520 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1521
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001522 adev->gfx.config.max_shader_engines = le32_to_cpu(gpu_info_fw->gc_num_se);
1523 adev->gfx.config.max_cu_per_sh = le32_to_cpu(gpu_info_fw->gc_num_cu_per_sh);
1524 adev->gfx.config.max_sh_per_se = le32_to_cpu(gpu_info_fw->gc_num_sh_per_se);
1525 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 -04001526 adev->gfx.config.max_texture_channel_caches =
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001527 le32_to_cpu(gpu_info_fw->gc_num_tccs);
1528 adev->gfx.config.max_gprs = le32_to_cpu(gpu_info_fw->gc_num_gprs);
1529 adev->gfx.config.max_gs_threads = le32_to_cpu(gpu_info_fw->gc_num_max_gs_thds);
1530 adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gpu_info_fw->gc_gs_table_depth);
1531 adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gpu_info_fw->gc_gsprim_buff_depth);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001532 adev->gfx.config.double_offchip_lds_buf =
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001533 le32_to_cpu(gpu_info_fw->gc_double_offchip_lds_buffer);
1534 adev->gfx.cu_info.wave_front_size = le32_to_cpu(gpu_info_fw->gc_wave_size);
Hawking Zhang51fd0372017-06-09 22:30:52 +08001535 adev->gfx.cu_info.max_waves_per_simd =
1536 le32_to_cpu(gpu_info_fw->gc_max_waves_per_simd);
1537 adev->gfx.cu_info.max_scratch_slots_per_cu =
1538 le32_to_cpu(gpu_info_fw->gc_max_scratch_slots_per_cu);
1539 adev->gfx.cu_info.lds_size = le32_to_cpu(gpu_info_fw->gc_lds_size);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001540 break;
1541 }
1542 default:
1543 dev_err(adev->dev,
1544 "Unsupported gpu_info table %d\n", hdr->header.ucode_version);
1545 err = -EINVAL;
1546 goto out;
1547 }
1548out:
Alex Deuchere2a75f82017-04-27 16:58:01 -04001549 return err;
1550}
1551
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001552static int amdgpu_early_init(struct amdgpu_device *adev)
1553{
Alex Deucheraaa36a92015-04-20 17:31:14 -04001554 int i, r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001555
Alex Deucher483ef982016-09-30 12:43:04 -04001556 amdgpu_device_enable_virtual_display(adev);
Emily Denga6be7572016-08-08 11:37:50 +08001557
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001558 switch (adev->asic_type) {
Alex Deucheraaa36a92015-04-20 17:31:14 -04001559 case CHIP_TOPAZ:
1560 case CHIP_TONGA:
David Zhang48299f92015-07-08 01:05:16 +08001561 case CHIP_FIJI:
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001562 case CHIP_POLARIS11:
1563 case CHIP_POLARIS10:
Junwei Zhangc4642a42016-12-14 15:32:28 -05001564 case CHIP_POLARIS12:
Alex Deucheraaa36a92015-04-20 17:31:14 -04001565 case CHIP_CARRIZO:
Samuel Li39bb0c92015-10-08 16:31:43 -04001566 case CHIP_STONEY:
1567 if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
Alex Deucheraaa36a92015-04-20 17:31:14 -04001568 adev->family = AMDGPU_FAMILY_CZ;
1569 else
1570 adev->family = AMDGPU_FAMILY_VI;
1571
1572 r = vi_set_ip_blocks(adev);
1573 if (r)
1574 return r;
1575 break;
Ken Wang33f34802016-01-21 17:29:41 +08001576#ifdef CONFIG_DRM_AMDGPU_SI
1577 case CHIP_VERDE:
1578 case CHIP_TAHITI:
1579 case CHIP_PITCAIRN:
1580 case CHIP_OLAND:
1581 case CHIP_HAINAN:
Ken Wang295d0da2016-05-24 21:02:53 +08001582 adev->family = AMDGPU_FAMILY_SI;
Ken Wang33f34802016-01-21 17:29:41 +08001583 r = si_set_ip_blocks(adev);
1584 if (r)
1585 return r;
1586 break;
1587#endif
Alex Deuchera2e73f52015-04-20 17:09:27 -04001588#ifdef CONFIG_DRM_AMDGPU_CIK
1589 case CHIP_BONAIRE:
1590 case CHIP_HAWAII:
1591 case CHIP_KAVERI:
1592 case CHIP_KABINI:
1593 case CHIP_MULLINS:
1594 if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
1595 adev->family = AMDGPU_FAMILY_CI;
1596 else
1597 adev->family = AMDGPU_FAMILY_KV;
1598
1599 r = cik_set_ip_blocks(adev);
1600 if (r)
1601 return r;
1602 break;
1603#endif
Chunming Zhou2ca8a5d2016-12-07 17:31:19 +08001604 case CHIP_VEGA10:
1605 case CHIP_RAVEN:
1606 if (adev->asic_type == CHIP_RAVEN)
1607 adev->family = AMDGPU_FAMILY_RV;
1608 else
1609 adev->family = AMDGPU_FAMILY_AI;
Ken Wang460826e2017-03-06 14:53:16 -05001610
1611 r = soc15_set_ip_blocks(adev);
1612 if (r)
1613 return r;
1614 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001615 default:
1616 /* FIXME: not supported yet */
1617 return -EINVAL;
1618 }
1619
Alex Deuchere2a75f82017-04-27 16:58:01 -04001620 r = amdgpu_device_parse_gpu_info_fw(adev);
1621 if (r)
1622 return r;
1623
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001624 if (amdgpu_sriov_vf(adev)) {
1625 r = amdgpu_virt_request_full_gpu(adev, true);
1626 if (r)
1627 return r;
1628 }
1629
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001630 for (i = 0; i < adev->num_ip_blocks; i++) {
1631 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
Huang Ruied8cf002017-05-03 09:40:17 +08001632 DRM_ERROR("disabled ip block: %d <%s>\n",
1633 i, adev->ip_blocks[i].version->funcs->name);
Alex Deuchera1255102016-10-13 17:41:13 -04001634 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001635 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001636 if (adev->ip_blocks[i].version->funcs->early_init) {
1637 r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001638 if (r == -ENOENT) {
Alex Deuchera1255102016-10-13 17:41:13 -04001639 adev->ip_blocks[i].status.valid = false;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001640 } else if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001641 DRM_ERROR("early_init of IP block <%s> failed %d\n",
1642 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001643 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001644 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001645 adev->ip_blocks[i].status.valid = true;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001646 }
Alex Deucher974e6b62015-07-10 13:59:44 -04001647 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001648 adev->ip_blocks[i].status.valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001649 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001650 }
1651 }
1652
Nicolai Hähnle395d1fb2016-06-02 12:32:07 +02001653 adev->cg_flags &= amdgpu_cg_mask;
1654 adev->pg_flags &= amdgpu_pg_mask;
1655
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001656 return 0;
1657}
1658
1659static int amdgpu_init(struct amdgpu_device *adev)
1660{
1661 int i, r;
1662
1663 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001664 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001665 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001666 r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001667 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001668 DRM_ERROR("sw_init of IP block <%s> failed %d\n",
1669 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001670 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001671 }
Alex Deuchera1255102016-10-13 17:41:13 -04001672 adev->ip_blocks[i].status.sw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001673 /* need to do gmc hw init early so we can allocate gpu mem */
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 r = amdgpu_vram_scratch_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001676 if (r) {
1677 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001678 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001679 }
Alex Deuchera1255102016-10-13 17:41:13 -04001680 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001681 if (r) {
1682 DRM_ERROR("hw_init %d failed %d\n", i, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001683 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001684 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001685 r = amdgpu_wb_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001686 if (r) {
1687 DRM_ERROR("amdgpu_wb_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001688 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001689 }
Alex Deuchera1255102016-10-13 17:41:13 -04001690 adev->ip_blocks[i].status.hw = true;
Monk Liu24936642017-01-09 15:54:32 +08001691
1692 /* right after GMC hw init, we create CSA */
1693 if (amdgpu_sriov_vf(adev)) {
1694 r = amdgpu_allocate_static_csa(adev);
1695 if (r) {
1696 DRM_ERROR("allocate CSA failed %d\n", r);
1697 return r;
1698 }
1699 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001700 }
1701 }
1702
1703 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001704 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001705 continue;
1706 /* gmc hw init is done early */
Alex Deuchera1255102016-10-13 17:41:13 -04001707 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001708 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001709 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001710 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001711 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
1712 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001713 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001714 }
Alex Deuchera1255102016-10-13 17:41:13 -04001715 adev->ip_blocks[i].status.hw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001716 }
1717
1718 return 0;
1719}
1720
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08001721static void amdgpu_fill_reset_magic(struct amdgpu_device *adev)
1722{
1723 memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM);
1724}
1725
1726static bool amdgpu_check_vram_lost(struct amdgpu_device *adev)
1727{
1728 return !!memcmp(adev->gart.ptr, adev->reset_magic,
1729 AMDGPU_RESET_MAGIC_NUM);
1730}
1731
Shirish S2dc80b02017-05-25 10:05:25 +05301732static int amdgpu_late_set_cg_state(struct amdgpu_device *adev)
1733{
1734 int i = 0, r;
1735
1736 for (i = 0; i < adev->num_ip_blocks; i++) {
1737 if (!adev->ip_blocks[i].status.valid)
1738 continue;
1739 /* skip CG for VCE/UVD, it's handled specially */
1740 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1741 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1742 /* enable clockgating to save power */
1743 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1744 AMD_CG_STATE_GATE);
1745 if (r) {
1746 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
1747 adev->ip_blocks[i].version->funcs->name, r);
1748 return r;
1749 }
1750 }
1751 }
1752 return 0;
1753}
1754
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001755static int amdgpu_late_init(struct amdgpu_device *adev)
1756{
1757 int i = 0, r;
1758
1759 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001760 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001761 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001762 if (adev->ip_blocks[i].version->funcs->late_init) {
1763 r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001764 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001765 DRM_ERROR("late_init of IP block <%s> failed %d\n",
1766 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001767 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001768 }
Alex Deuchera1255102016-10-13 17:41:13 -04001769 adev->ip_blocks[i].status.late_initialized = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001770 }
1771 }
1772
Shirish S2dc80b02017-05-25 10:05:25 +05301773 mod_delayed_work(system_wq, &adev->late_init_work,
1774 msecs_to_jiffies(AMDGPU_RESUME_MS));
1775
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08001776 amdgpu_fill_reset_magic(adev);
1777
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001778 return 0;
1779}
1780
1781static int amdgpu_fini(struct amdgpu_device *adev)
1782{
1783 int i, r;
1784
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001785 /* need to disable SMC first */
1786 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001787 if (!adev->ip_blocks[i].status.hw)
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001788 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001789 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001790 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
Alex Deuchera1255102016-10-13 17:41:13 -04001791 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1792 AMD_CG_STATE_UNGATE);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001793 if (r) {
1794 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001795 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001796 return r;
1797 }
Alex Deuchera1255102016-10-13 17:41:13 -04001798 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001799 /* XXX handle errors */
1800 if (r) {
1801 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001802 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001803 }
Alex Deuchera1255102016-10-13 17:41:13 -04001804 adev->ip_blocks[i].status.hw = false;
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001805 break;
1806 }
1807 }
1808
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001809 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001810 if (!adev->ip_blocks[i].status.hw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001811 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001812 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001813 amdgpu_wb_fini(adev);
1814 amdgpu_vram_scratch_fini(adev);
1815 }
Rex Zhu8201a672016-11-24 21:44:44 +08001816
1817 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1818 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1819 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
1820 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1821 AMD_CG_STATE_UNGATE);
1822 if (r) {
1823 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1824 adev->ip_blocks[i].version->funcs->name, r);
1825 return r;
1826 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001827 }
Rex Zhu8201a672016-11-24 21:44:44 +08001828
Alex Deuchera1255102016-10-13 17:41:13 -04001829 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001830 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001831 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001832 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
1833 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001834 }
Rex Zhu8201a672016-11-24 21:44:44 +08001835
Alex Deuchera1255102016-10-13 17:41:13 -04001836 adev->ip_blocks[i].status.hw = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001837 }
1838
1839 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001840 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001841 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001842 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001843 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001844 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001845 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
1846 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001847 }
Alex Deuchera1255102016-10-13 17:41:13 -04001848 adev->ip_blocks[i].status.sw = false;
1849 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001850 }
1851
Monk Liua6dcfd92016-05-19 14:36:34 +08001852 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001853 if (!adev->ip_blocks[i].status.late_initialized)
Grazvydas Ignotas8a2eef12016-10-03 00:06:44 +03001854 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001855 if (adev->ip_blocks[i].version->funcs->late_fini)
1856 adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
1857 adev->ip_blocks[i].status.late_initialized = false;
Monk Liua6dcfd92016-05-19 14:36:34 +08001858 }
1859
Monk Liu030308f2017-09-15 15:34:52 +08001860 if (amdgpu_sriov_vf(adev))
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001861 amdgpu_virt_release_full_gpu(adev, false);
Monk Liu24936642017-01-09 15:54:32 +08001862
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001863 return 0;
1864}
1865
Shirish S2dc80b02017-05-25 10:05:25 +05301866static void amdgpu_late_init_func_handler(struct work_struct *work)
1867{
1868 struct amdgpu_device *adev =
1869 container_of(work, struct amdgpu_device, late_init_work.work);
1870 amdgpu_late_set_cg_state(adev);
1871}
1872
Alex Deucherfaefba92016-12-06 10:38:29 -05001873int amdgpu_suspend(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001874{
1875 int i, r;
1876
Xiangliang Yue941ea92017-01-18 12:47:55 +08001877 if (amdgpu_sriov_vf(adev))
1878 amdgpu_virt_request_full_gpu(adev, false);
1879
Flora Cuic5a93a22016-02-26 10:45:25 +08001880 /* ungate SMC block first */
1881 r = amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC,
1882 AMD_CG_STATE_UNGATE);
1883 if (r) {
1884 DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n",r);
1885 }
1886
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001887 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001888 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001889 continue;
1890 /* ungate blocks so that suspend can properly shut them down */
Flora Cuic5a93a22016-02-26 10:45:25 +08001891 if (i != AMD_IP_BLOCK_TYPE_SMC) {
Alex Deuchera1255102016-10-13 17:41:13 -04001892 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1893 AMD_CG_STATE_UNGATE);
Flora Cuic5a93a22016-02-26 10:45:25 +08001894 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001895 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1896 adev->ip_blocks[i].version->funcs->name, r);
Flora Cuic5a93a22016-02-26 10:45:25 +08001897 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001898 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001899 /* XXX handle errors */
Alex Deuchera1255102016-10-13 17:41:13 -04001900 r = adev->ip_blocks[i].version->funcs->suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001901 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001902 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001903 DRM_ERROR("suspend of IP block <%s> failed %d\n",
1904 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001905 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001906 }
1907
Xiangliang Yue941ea92017-01-18 12:47:55 +08001908 if (amdgpu_sriov_vf(adev))
1909 amdgpu_virt_release_full_gpu(adev, false);
1910
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001911 return 0;
1912}
1913
Monk Liue4f0fdc2017-02-09 11:55:49 +08001914static int amdgpu_sriov_reinit_early(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08001915{
1916 int i, r;
1917
Monk Liu2cb681b2017-04-26 12:00:49 +08001918 static enum amd_ip_block_type ip_order[] = {
1919 AMD_IP_BLOCK_TYPE_GMC,
1920 AMD_IP_BLOCK_TYPE_COMMON,
Monk Liu2cb681b2017-04-26 12:00:49 +08001921 AMD_IP_BLOCK_TYPE_IH,
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
Monk Liue4f0fdc2017-02-09 11:55:49 +08001943static int amdgpu_sriov_reinit_late(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08001944{
1945 int i, r;
1946
Monk Liu2cb681b2017-04-26 12:00:49 +08001947 static enum amd_ip_block_type ip_order[] = {
1948 AMD_IP_BLOCK_TYPE_SMC,
1949 AMD_IP_BLOCK_TYPE_DCE,
1950 AMD_IP_BLOCK_TYPE_GFX,
1951 AMD_IP_BLOCK_TYPE_SDMA,
Frank Min257deb82017-06-15 20:07:36 +08001952 AMD_IP_BLOCK_TYPE_UVD,
1953 AMD_IP_BLOCK_TYPE_VCE
Monk Liu2cb681b2017-04-26 12:00:49 +08001954 };
Monk Liua90ad3c2017-01-23 14:22:08 +08001955
Monk Liu2cb681b2017-04-26 12:00:49 +08001956 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1957 int j;
1958 struct amdgpu_ip_block *block;
Monk Liua90ad3c2017-01-23 14:22:08 +08001959
Monk Liu2cb681b2017-04-26 12:00:49 +08001960 for (j = 0; j < adev->num_ip_blocks; j++) {
1961 block = &adev->ip_blocks[j];
1962
1963 if (block->version->type != ip_order[i] ||
1964 !block->status.valid)
1965 continue;
1966
1967 r = block->version->funcs->hw_init(adev);
1968 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
Monk Liua90ad3c2017-01-23 14:22:08 +08001969 }
1970 }
1971
1972 return 0;
1973}
1974
Chunming Zhoufcf06492017-05-05 10:33:33 +08001975static int amdgpu_resume_phase1(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001976{
1977 int i, r;
1978
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001979 for (i = 0; i < adev->num_ip_blocks; i++) {
1980 if (!adev->ip_blocks[i].status.valid)
1981 continue;
Chunming Zhoufcf06492017-05-05 10:33:33 +08001982 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
1983 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
1984 adev->ip_blocks[i].version->type ==
1985 AMD_IP_BLOCK_TYPE_IH) {
1986 r = adev->ip_blocks[i].version->funcs->resume(adev);
1987 if (r) {
1988 DRM_ERROR("resume of IP block <%s> failed %d\n",
1989 adev->ip_blocks[i].version->funcs->name, r);
1990 return r;
1991 }
1992 }
1993 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001994
Chunming Zhoufcf06492017-05-05 10:33:33 +08001995 return 0;
1996}
1997
1998static int amdgpu_resume_phase2(struct amdgpu_device *adev)
1999{
2000 int i, r;
2001
2002 for (i = 0; i < adev->num_ip_blocks; i++) {
2003 if (!adev->ip_blocks[i].status.valid)
2004 continue;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002005 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
2006 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
2007 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH )
2008 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002009 r = adev->ip_blocks[i].version->funcs->resume(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002010 if (r) {
2011 DRM_ERROR("resume of IP block <%s> failed %d\n",
2012 adev->ip_blocks[i].version->funcs->name, r);
2013 return r;
2014 }
2015 }
2016
2017 return 0;
2018}
2019
2020static int amdgpu_resume(struct amdgpu_device *adev)
2021{
Chunming Zhoufcf06492017-05-05 10:33:33 +08002022 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002023
Chunming Zhoufcf06492017-05-05 10:33:33 +08002024 r = amdgpu_resume_phase1(adev);
2025 if (r)
2026 return r;
2027 r = amdgpu_resume_phase2(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002028
Chunming Zhoufcf06492017-05-05 10:33:33 +08002029 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002030}
2031
Monk Liu4e99a442016-03-31 13:26:59 +08002032static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
Andres Rodriguez048765a2016-06-11 02:51:32 -04002033{
Alex Deuchera5bde2f2016-09-23 16:23:41 -04002034 if (adev->is_atom_fw) {
2035 if (amdgpu_atomfirmware_gpu_supports_virtualization(adev))
2036 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
2037 } else {
2038 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
2039 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
2040 }
Andres Rodriguez048765a2016-06-11 02:51:32 -04002041}
2042
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002043/**
2044 * amdgpu_device_init - initialize the driver
2045 *
2046 * @adev: amdgpu_device pointer
2047 * @pdev: drm dev pointer
2048 * @pdev: pci dev pointer
2049 * @flags: driver flags
2050 *
2051 * Initializes the driver info and hw (all asics).
2052 * Returns 0 for success or an error on failure.
2053 * Called at driver startup.
2054 */
2055int amdgpu_device_init(struct amdgpu_device *adev,
2056 struct drm_device *ddev,
2057 struct pci_dev *pdev,
2058 uint32_t flags)
2059{
2060 int r, i;
2061 bool runtime = false;
Marek Olšák95844d22016-08-17 23:49:27 +02002062 u32 max_MBps;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002063
2064 adev->shutdown = false;
2065 adev->dev = &pdev->dev;
2066 adev->ddev = ddev;
2067 adev->pdev = pdev;
2068 adev->flags = flags;
Jammy Zhou2f7d10b2015-07-22 11:29:01 +08002069 adev->asic_type = flags & AMD_ASIC_MASK;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002070 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
Christian König6f02a692017-07-07 11:56:59 +02002071 adev->mc.gart_size = 512 * 1024 * 1024;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002072 adev->accel_working = false;
2073 adev->num_rings = 0;
2074 adev->mman.buffer_funcs = NULL;
2075 adev->mman.buffer_funcs_ring = NULL;
2076 adev->vm_manager.vm_pte_funcs = NULL;
Christian König2d55e452016-02-08 17:37:38 +01002077 adev->vm_manager.vm_pte_num_rings = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002078 adev->gart.gart_funcs = NULL;
Chris Wilsonf54d1862016-10-25 13:00:45 +01002079 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
Andres Rodriguezb8866c22017-04-28 20:05:51 -04002080 bitmap_zero(adev->gfx.pipe_reserve_bitmap, AMDGPU_MAX_COMPUTE_QUEUES);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002081
2082 adev->smc_rreg = &amdgpu_invalid_rreg;
2083 adev->smc_wreg = &amdgpu_invalid_wreg;
2084 adev->pcie_rreg = &amdgpu_invalid_rreg;
2085 adev->pcie_wreg = &amdgpu_invalid_wreg;
Huang Rui36b9a952016-08-31 13:23:25 +08002086 adev->pciep_rreg = &amdgpu_invalid_rreg;
2087 adev->pciep_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002088 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
2089 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
2090 adev->didt_rreg = &amdgpu_invalid_rreg;
2091 adev->didt_wreg = &amdgpu_invalid_wreg;
Rex Zhuccdbb202016-06-08 12:47:41 +08002092 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
2093 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002094 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
2095 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
2096
Rex Zhuccdbb202016-06-08 12:47:41 +08002097
Alex Deucher3e39ab92015-06-05 15:04:33 -04002098 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
2099 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
2100 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002101
2102 /* mutex initialization are all done here so we
2103 * can recall function without having locking issues */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002104 atomic_set(&adev->irq.ih.lock, 0);
Huang Rui0e5ca0d2017-03-03 18:37:23 -05002105 mutex_init(&adev->firmware.mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002106 mutex_init(&adev->pm.mutex);
2107 mutex_init(&adev->gfx.gpu_clock_mutex);
2108 mutex_init(&adev->srbm_mutex);
Andres Rodriguezb8866c22017-04-28 20:05:51 -04002109 mutex_init(&adev->gfx.pipe_reserve_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002110 mutex_init(&adev->grbm_idx_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002111 mutex_init(&adev->mn_lock);
Alex Deuchere23b74a2017-09-28 09:47:32 -04002112 mutex_init(&adev->virt.vf_errors.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002113 hash_init(adev->mn_hash);
2114
2115 amdgpu_check_arguments(adev);
2116
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002117 spin_lock_init(&adev->mmio_idx_lock);
2118 spin_lock_init(&adev->smc_idx_lock);
2119 spin_lock_init(&adev->pcie_idx_lock);
2120 spin_lock_init(&adev->uvd_ctx_idx_lock);
2121 spin_lock_init(&adev->didt_idx_lock);
Rex Zhuccdbb202016-06-08 12:47:41 +08002122 spin_lock_init(&adev->gc_cac_idx_lock);
Evan Quan16abb5d2017-07-04 09:21:50 +08002123 spin_lock_init(&adev->se_cac_idx_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002124 spin_lock_init(&adev->audio_endpt_idx_lock);
Marek Olšák95844d22016-08-17 23:49:27 +02002125 spin_lock_init(&adev->mm_stats.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002126
Chunming Zhou0c4e7fa2016-08-17 11:41:30 +08002127 INIT_LIST_HEAD(&adev->shadow_list);
2128 mutex_init(&adev->shadow_list_lock);
2129
Chunming Zhou5c1354b2016-08-30 16:13:10 +08002130 INIT_LIST_HEAD(&adev->gtt_list);
2131 spin_lock_init(&adev->gtt_list_lock);
2132
Andres Rodriguez795f2812017-03-06 16:27:55 -05002133 INIT_LIST_HEAD(&adev->ring_lru_list);
2134 spin_lock_init(&adev->ring_lru_list_lock);
2135
Shirish S2dc80b02017-05-25 10:05:25 +05302136 INIT_DELAYED_WORK(&adev->late_init_work, amdgpu_late_init_func_handler);
2137
Alex Xie0fa49552017-06-08 14:58:05 -04002138 /* Registers mapping */
2139 /* TODO: block userspace mapping of io register */
Ken Wangda69c1612016-01-21 19:08:55 +08002140 if (adev->asic_type >= CHIP_BONAIRE) {
2141 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
2142 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
2143 } else {
2144 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
2145 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
2146 }
Chunming Zhou5c1354b2016-08-30 16:13:10 +08002147
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002148 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
2149 if (adev->rmmio == NULL) {
2150 return -ENOMEM;
2151 }
2152 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
2153 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
2154
Christian König705e5192017-06-08 11:15:16 +02002155 /* doorbell bar mapping */
2156 amdgpu_doorbell_init(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002157
2158 /* io port mapping */
2159 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
2160 if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
2161 adev->rio_mem_size = pci_resource_len(adev->pdev, i);
2162 adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
2163 break;
2164 }
2165 }
2166 if (adev->rio_mem == NULL)
Amber Linb64a18c2017-01-04 08:06:58 -05002167 DRM_INFO("PCI I/O BAR is not found.\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002168
2169 /* early init functions */
2170 r = amdgpu_early_init(adev);
2171 if (r)
2172 return r;
2173
2174 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
2175 /* this will fail for cards that aren't VGA class devices, just
2176 * ignore it */
2177 vga_client_register(adev->pdev, adev, NULL, amdgpu_vga_set_decode);
2178
2179 if (amdgpu_runtime_pm == 1)
2180 runtime = true;
Alex Deuchere9bef452016-04-25 13:12:18 -04002181 if (amdgpu_device_is_px(ddev))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002182 runtime = true;
Lukas Wunner84c8b222017-03-10 21:23:45 +01002183 if (!pci_is_thunderbolt_attached(adev->pdev))
2184 vga_switcheroo_register_client(adev->pdev,
2185 &amdgpu_switcheroo_ops, runtime);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002186 if (runtime)
2187 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
2188
2189 /* Read BIOS */
Alex Deucher83ba1262016-06-03 18:21:41 -04002190 if (!amdgpu_get_bios(adev)) {
2191 r = -EINVAL;
2192 goto failed;
2193 }
Nils Wallméniusf7e9e9f2016-12-14 21:52:45 +01002194
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002195 r = amdgpu_atombios_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002196 if (r) {
2197 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002198 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_INIT_FAIL, 0, 0);
Alex Deucher83ba1262016-06-03 18:21:41 -04002199 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002200 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002201
Monk Liu4e99a442016-03-31 13:26:59 +08002202 /* detect if we are with an SRIOV vbios */
2203 amdgpu_device_detect_sriov_bios(adev);
Andres Rodriguez048765a2016-06-11 02:51:32 -04002204
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002205 /* Post card if necessary */
pding91fe77e2017-10-19 09:38:39 +08002206 if (amdgpu_need_post(adev)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002207 if (!adev->bios) {
Monk Liubec86372016-09-14 19:38:08 +08002208 dev_err(adev->dev, "no vBIOS found\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002209 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_NO_VBIOS, 0, 0);
Alex Deucher83ba1262016-06-03 18:21:41 -04002210 r = -EINVAL;
2211 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002212 }
Monk Liubec86372016-09-14 19:38:08 +08002213 DRM_INFO("GPU posting now...\n");
Monk Liu4e99a442016-03-31 13:26:59 +08002214 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2215 if (r) {
2216 dev_err(adev->dev, "gpu post error!\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002217 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_GPU_POST_ERROR, 0, 0);
Monk Liu4e99a442016-03-31 13:26:59 +08002218 goto failed;
2219 }
2220 } else {
2221 DRM_INFO("GPU post is not needed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002222 }
2223
Alex Deucher88b64e92017-07-10 10:43:10 -04002224 if (adev->is_atom_fw) {
2225 /* Initialize clocks */
2226 r = amdgpu_atomfirmware_get_clock_info(adev);
2227 if (r) {
2228 dev_err(adev->dev, "amdgpu_atomfirmware_get_clock_info failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002229 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
Alex Deucher88b64e92017-07-10 10:43:10 -04002230 goto failed;
2231 }
2232 } else {
Alex Deuchera5bde2f2016-09-23 16:23:41 -04002233 /* Initialize clocks */
2234 r = amdgpu_atombios_get_clock_info(adev);
2235 if (r) {
2236 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002237 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
Gavin Wan89041942017-06-23 13:55:15 -04002238 goto failed;
Alex Deuchera5bde2f2016-09-23 16:23:41 -04002239 }
2240 /* init i2c buses */
2241 amdgpu_atombios_i2c_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002242 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002243
2244 /* Fence driver */
2245 r = amdgpu_fence_driver_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002246 if (r) {
2247 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002248 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0);
Alex Deucher83ba1262016-06-03 18:21:41 -04002249 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002250 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002251
2252 /* init the mode config */
2253 drm_mode_config_init(adev->ddev);
2254
2255 r = amdgpu_init(adev);
2256 if (r) {
Alex Deucher2c1a2782015-12-07 17:02:53 -05002257 dev_err(adev->dev, "amdgpu_init failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002258 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002259 amdgpu_fini(adev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002260 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002261 }
2262
2263 adev->accel_working = true;
2264
Alex Xiee59c0202017-06-01 09:42:59 -04002265 amdgpu_vm_check_compute_bug(adev);
2266
Marek Olšák95844d22016-08-17 23:49:27 +02002267 /* Initialize the buffer migration limit. */
2268 if (amdgpu_moverate >= 0)
2269 max_MBps = amdgpu_moverate;
2270 else
2271 max_MBps = 8; /* Allow 8 MB/s. */
2272 /* Get a log2 for easy divisions. */
2273 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
2274
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002275 r = amdgpu_ib_pool_init(adev);
2276 if (r) {
2277 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
Alex Deuchere23b74a2017-09-28 09:47:32 -04002278 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r);
Alex Deucher83ba1262016-06-03 18:21:41 -04002279 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002280 }
2281
2282 r = amdgpu_ib_ring_tests(adev);
2283 if (r)
2284 DRM_ERROR("ib ring test failed (%d).\n", r);
2285
Horace Chen2dc8f812017-10-09 16:17:16 +08002286 if (amdgpu_sriov_vf(adev))
2287 amdgpu_virt_init_data_exchange(adev);
2288
Monk Liu9bc92b92017-02-08 17:38:13 +08002289 amdgpu_fbdev_init(adev);
2290
Rex Zhud2f52ac2017-09-22 17:47:27 +08002291 r = amdgpu_pm_sysfs_init(adev);
2292 if (r)
2293 DRM_ERROR("registering pm debugfs failed (%d).\n", r);
2294
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002295 r = amdgpu_gem_debugfs_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002296 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002297 DRM_ERROR("registering gem debugfs failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002298
2299 r = amdgpu_debugfs_regs_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002300 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002301 DRM_ERROR("registering register debugfs failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002302
Huang Rui4f0955f2017-05-10 23:04:06 +08002303 r = amdgpu_debugfs_test_ib_ring_init(adev);
2304 if (r)
2305 DRM_ERROR("registering register test ib ring debugfs failed (%d).\n", r);
2306
Huang Rui50ab2532016-06-12 15:51:09 +08002307 r = amdgpu_debugfs_firmware_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002308 if (r)
Huang Rui50ab2532016-06-12 15:51:09 +08002309 DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
Huang Rui50ab2532016-06-12 15:51:09 +08002310
Kent Russelldb95e212017-08-22 12:31:43 -04002311 r = amdgpu_debugfs_vbios_dump_init(adev);
2312 if (r)
2313 DRM_ERROR("Creating vbios dump debugfs failed (%d).\n", r);
2314
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002315 if ((amdgpu_testing & 1)) {
2316 if (adev->accel_working)
2317 amdgpu_test_moves(adev);
2318 else
2319 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
2320 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002321 if (amdgpu_benchmarking) {
2322 if (adev->accel_working)
2323 amdgpu_benchmark(adev, amdgpu_benchmarking);
2324 else
2325 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
2326 }
2327
2328 /* enable clockgating, etc. after ib tests, etc. since some blocks require
2329 * explicit gating rather than handling it automatically.
2330 */
2331 r = amdgpu_late_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002332 if (r) {
2333 dev_err(adev->dev, "amdgpu_late_init failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002334 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r);
Alex Deucher83ba1262016-06-03 18:21:41 -04002335 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002336 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002337
2338 return 0;
Alex Deucher83ba1262016-06-03 18:21:41 -04002339
2340failed:
Gavin Wan89041942017-06-23 13:55:15 -04002341 amdgpu_vf_error_trans_all(adev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002342 if (runtime)
2343 vga_switcheroo_fini_domain_pm_ops(adev->dev);
2344 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002345}
2346
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002347/**
2348 * amdgpu_device_fini - tear down the driver
2349 *
2350 * @adev: amdgpu_device pointer
2351 *
2352 * Tear down the driver info (all asics).
2353 * Called at driver shutdown.
2354 */
2355void amdgpu_device_fini(struct amdgpu_device *adev)
2356{
2357 int r;
2358
2359 DRM_INFO("amdgpu: finishing device.\n");
2360 adev->shutdown = true;
Pixel Dingdb2c2a92017-04-25 16:47:42 +08002361 if (adev->mode_info.mode_config_initialized)
2362 drm_crtc_force_disable_all(adev->ddev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002363 /* evict vram memory */
2364 amdgpu_bo_evict_vram(adev);
2365 amdgpu_ib_pool_fini(adev);
Horace Chena05502e2017-09-29 14:41:57 +08002366 amdgpu_fw_reserve_vram_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002367 amdgpu_fence_driver_fini(adev);
2368 amdgpu_fbdev_fini(adev);
2369 r = amdgpu_fini(adev);
Huang Ruiab4fe3e2017-06-05 22:11:59 +08002370 if (adev->firmware.gpu_info_fw) {
2371 release_firmware(adev->firmware.gpu_info_fw);
2372 adev->firmware.gpu_info_fw = NULL;
2373 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002374 adev->accel_working = false;
Shirish S2dc80b02017-05-25 10:05:25 +05302375 cancel_delayed_work_sync(&adev->late_init_work);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002376 /* free i2c buses */
2377 amdgpu_i2c_fini(adev);
2378 amdgpu_atombios_fini(adev);
2379 kfree(adev->bios);
2380 adev->bios = NULL;
Lukas Wunner84c8b222017-03-10 21:23:45 +01002381 if (!pci_is_thunderbolt_attached(adev->pdev))
2382 vga_switcheroo_unregister_client(adev->pdev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002383 if (adev->flags & AMD_IS_PX)
2384 vga_switcheroo_fini_domain_pm_ops(adev->dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002385 vga_client_register(adev->pdev, NULL, NULL, NULL);
2386 if (adev->rio_mem)
2387 pci_iounmap(adev->pdev, adev->rio_mem);
2388 adev->rio_mem = NULL;
2389 iounmap(adev->rmmio);
2390 adev->rmmio = NULL;
Christian König705e5192017-06-08 11:15:16 +02002391 amdgpu_doorbell_fini(adev);
Rex Zhud2f52ac2017-09-22 17:47:27 +08002392 amdgpu_pm_sysfs_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002393 amdgpu_debugfs_regs_cleanup(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002394}
2395
2396
2397/*
2398 * Suspend & resume.
2399 */
2400/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002401 * amdgpu_device_suspend - initiate device suspend
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002402 *
2403 * @pdev: drm dev pointer
2404 * @state: suspend state
2405 *
2406 * Puts the hw in the suspend state (all asics).
2407 * Returns 0 for success or an error on failure.
2408 * Called at driver suspend.
2409 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002410int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002411{
2412 struct amdgpu_device *adev;
2413 struct drm_crtc *crtc;
2414 struct drm_connector *connector;
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002415 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002416
2417 if (dev == NULL || dev->dev_private == NULL) {
2418 return -ENODEV;
2419 }
2420
2421 adev = dev->dev_private;
2422
2423 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2424 return 0;
2425
2426 drm_kms_helper_poll_disable(dev);
2427
2428 /* turn off display hw */
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002429 drm_modeset_lock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002430 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2431 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
2432 }
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002433 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002434
Yong Zhaoba997702015-11-09 17:21:45 -05002435 amdgpu_amdkfd_suspend(adev);
2436
Alex Deucher756e6882015-10-08 00:03:36 -04002437 /* unpin the front buffers and cursors */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002438 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Alex Deucher756e6882015-10-08 00:03:36 -04002439 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002440 struct amdgpu_framebuffer *rfb = to_amdgpu_framebuffer(crtc->primary->fb);
2441 struct amdgpu_bo *robj;
2442
Alex Deucher756e6882015-10-08 00:03:36 -04002443 if (amdgpu_crtc->cursor_bo) {
2444 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
Alex Xie7a6901d2017-04-24 13:52:41 -04002445 r = amdgpu_bo_reserve(aobj, true);
Alex Deucher756e6882015-10-08 00:03:36 -04002446 if (r == 0) {
2447 amdgpu_bo_unpin(aobj);
2448 amdgpu_bo_unreserve(aobj);
2449 }
2450 }
2451
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002452 if (rfb == NULL || rfb->obj == NULL) {
2453 continue;
2454 }
2455 robj = gem_to_amdgpu_bo(rfb->obj);
2456 /* don't unpin kernel fb objects */
2457 if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
Alex Xie7a6901d2017-04-24 13:52:41 -04002458 r = amdgpu_bo_reserve(robj, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002459 if (r == 0) {
2460 amdgpu_bo_unpin(robj);
2461 amdgpu_bo_unreserve(robj);
2462 }
2463 }
2464 }
2465 /* evict vram memory */
2466 amdgpu_bo_evict_vram(adev);
2467
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002468 amdgpu_fence_driver_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002469
2470 r = amdgpu_suspend(adev);
2471
Alex Deuchera0a71e42016-10-10 12:41:36 -04002472 /* evict remaining vram memory
2473 * This second call to evict vram is to evict the gart page table
2474 * using the CPU.
2475 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002476 amdgpu_bo_evict_vram(adev);
2477
Alex Deucherd05da0e2017-06-30 17:08:45 -04002478 amdgpu_atombios_scratch_regs_save(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002479 pci_save_state(dev->pdev);
2480 if (suspend) {
2481 /* Shut down the device */
2482 pci_disable_device(dev->pdev);
2483 pci_set_power_state(dev->pdev, PCI_D3hot);
jimqu74b0b152016-09-07 17:09:12 +08002484 } else {
2485 r = amdgpu_asic_reset(adev);
2486 if (r)
2487 DRM_ERROR("amdgpu asic reset failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002488 }
2489
2490 if (fbcon) {
2491 console_lock();
2492 amdgpu_fbdev_set_suspend(adev, 1);
2493 console_unlock();
2494 }
2495 return 0;
2496}
2497
2498/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002499 * amdgpu_device_resume - initiate device resume
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002500 *
2501 * @pdev: drm dev pointer
2502 *
2503 * Bring the hw back to operating state (all asics).
2504 * Returns 0 for success or an error on failure.
2505 * Called at driver resume.
2506 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002507int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002508{
2509 struct drm_connector *connector;
2510 struct amdgpu_device *adev = dev->dev_private;
Alex Deucher756e6882015-10-08 00:03:36 -04002511 struct drm_crtc *crtc;
Huang Rui03161a62017-04-13 16:12:26 +08002512 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002513
2514 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2515 return 0;
2516
jimqu74b0b152016-09-07 17:09:12 +08002517 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002518 console_lock();
jimqu74b0b152016-09-07 17:09:12 +08002519
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002520 if (resume) {
2521 pci_set_power_state(dev->pdev, PCI_D0);
2522 pci_restore_state(dev->pdev);
jimqu74b0b152016-09-07 17:09:12 +08002523 r = pci_enable_device(dev->pdev);
Huang Rui03161a62017-04-13 16:12:26 +08002524 if (r)
2525 goto unlock;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002526 }
Alex Deucherd05da0e2017-06-30 17:08:45 -04002527 amdgpu_atombios_scratch_regs_restore(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002528
2529 /* post card */
Jim Quc836fec2017-02-10 15:59:59 +08002530 if (amdgpu_need_post(adev)) {
jimqu74b0b152016-09-07 17:09:12 +08002531 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2532 if (r)
2533 DRM_ERROR("amdgpu asic init failed\n");
2534 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002535
2536 r = amdgpu_resume(adev);
Rex Zhue6707212017-03-30 13:21:01 +08002537 if (r) {
Flora Cuica198522016-02-04 15:10:08 +08002538 DRM_ERROR("amdgpu_resume failed (%d).\n", r);
Huang Rui03161a62017-04-13 16:12:26 +08002539 goto unlock;
Rex Zhue6707212017-03-30 13:21:01 +08002540 }
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002541 amdgpu_fence_driver_resume(adev);
2542
Flora Cuica198522016-02-04 15:10:08 +08002543 if (resume) {
2544 r = amdgpu_ib_ring_tests(adev);
2545 if (r)
2546 DRM_ERROR("ib ring test failed (%d).\n", r);
2547 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002548
2549 r = amdgpu_late_init(adev);
Huang Rui03161a62017-04-13 16:12:26 +08002550 if (r)
2551 goto unlock;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002552
Alex Deucher756e6882015-10-08 00:03:36 -04002553 /* pin cursors */
2554 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2555 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2556
2557 if (amdgpu_crtc->cursor_bo) {
2558 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
Alex Xie7a6901d2017-04-24 13:52:41 -04002559 r = amdgpu_bo_reserve(aobj, true);
Alex Deucher756e6882015-10-08 00:03:36 -04002560 if (r == 0) {
2561 r = amdgpu_bo_pin(aobj,
2562 AMDGPU_GEM_DOMAIN_VRAM,
2563 &amdgpu_crtc->cursor_addr);
2564 if (r != 0)
2565 DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
2566 amdgpu_bo_unreserve(aobj);
2567 }
2568 }
2569 }
Yong Zhaoba997702015-11-09 17:21:45 -05002570 r = amdgpu_amdkfd_resume(adev);
2571 if (r)
2572 return r;
Alex Deucher756e6882015-10-08 00:03:36 -04002573
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002574 /* blat the mode back in */
2575 if (fbcon) {
2576 drm_helper_resume_force_mode(dev);
2577 /* turn on display hw */
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002578 drm_modeset_lock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002579 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2580 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
2581 }
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002582 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002583 }
2584
2585 drm_kms_helper_poll_enable(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002586
2587 /*
2588 * Most of the connector probing functions try to acquire runtime pm
2589 * refs to ensure that the GPU is powered on when connector polling is
2590 * performed. Since we're calling this from a runtime PM callback,
2591 * trying to acquire rpm refs will cause us to deadlock.
2592 *
2593 * Since we're guaranteed to be holding the rpm lock, it's safe to
2594 * temporarily disable the rpm helpers so this doesn't deadlock us.
2595 */
2596#ifdef CONFIG_PM
2597 dev->dev->power.disable_depth++;
2598#endif
Alex Deucher54fb2a52015-11-24 14:30:56 -05002599 drm_helper_hpd_irq_event(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002600#ifdef CONFIG_PM
2601 dev->dev->power.disable_depth--;
2602#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002603
Huang Rui03161a62017-04-13 16:12:26 +08002604 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002605 amdgpu_fbdev_set_suspend(adev, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002606
Huang Rui03161a62017-04-13 16:12:26 +08002607unlock:
2608 if (fbcon)
2609 console_unlock();
2610
2611 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002612}
2613
Chunming Zhou63fbf422016-07-15 11:19:20 +08002614static bool amdgpu_check_soft_reset(struct amdgpu_device *adev)
2615{
2616 int i;
2617 bool asic_hang = false;
2618
Monk Liuf993d622017-10-16 19:46:01 +08002619 if (amdgpu_sriov_vf(adev))
2620 return true;
2621
Chunming Zhou63fbf422016-07-15 11:19:20 +08002622 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002623 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou63fbf422016-07-15 11:19:20 +08002624 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002625 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
2626 adev->ip_blocks[i].status.hang =
2627 adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
2628 if (adev->ip_blocks[i].status.hang) {
2629 DRM_INFO("IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
Chunming Zhou63fbf422016-07-15 11:19:20 +08002630 asic_hang = true;
2631 }
2632 }
2633 return asic_hang;
2634}
2635
Baoyou Xie4d446652016-09-18 22:09:35 +08002636static int amdgpu_pre_soft_reset(struct amdgpu_device *adev)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002637{
2638 int i, r = 0;
2639
2640 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002641 if (!adev->ip_blocks[i].status.valid)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002642 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002643 if (adev->ip_blocks[i].status.hang &&
2644 adev->ip_blocks[i].version->funcs->pre_soft_reset) {
2645 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
Chunming Zhoud31a5012016-07-18 10:04:34 +08002646 if (r)
2647 return r;
2648 }
2649 }
2650
2651 return 0;
2652}
2653
Chunming Zhou35d782f2016-07-15 15:57:13 +08002654static bool amdgpu_need_full_reset(struct amdgpu_device *adev)
2655{
Alex Deucherda146d32016-10-13 16:07:03 -04002656 int i;
2657
2658 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002659 if (!adev->ip_blocks[i].status.valid)
Alex Deucherda146d32016-10-13 16:07:03 -04002660 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002661 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
2662 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
2663 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
Ken Wang98512bb2017-09-14 16:25:19 +08002664 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) ||
2665 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
Alex Deuchera1255102016-10-13 17:41:13 -04002666 if (adev->ip_blocks[i].status.hang) {
Alex Deucherda146d32016-10-13 16:07:03 -04002667 DRM_INFO("Some block need full reset!\n");
2668 return true;
2669 }
2670 }
Chunming Zhou35d782f2016-07-15 15:57:13 +08002671 }
2672 return false;
2673}
2674
2675static int amdgpu_soft_reset(struct amdgpu_device *adev)
2676{
2677 int i, r = 0;
2678
2679 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002680 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002681 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002682 if (adev->ip_blocks[i].status.hang &&
2683 adev->ip_blocks[i].version->funcs->soft_reset) {
2684 r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002685 if (r)
2686 return r;
2687 }
2688 }
2689
2690 return 0;
2691}
2692
2693static int amdgpu_post_soft_reset(struct amdgpu_device *adev)
2694{
2695 int i, r = 0;
2696
2697 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002698 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002699 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002700 if (adev->ip_blocks[i].status.hang &&
2701 adev->ip_blocks[i].version->funcs->post_soft_reset)
2702 r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002703 if (r)
2704 return r;
2705 }
2706
2707 return 0;
2708}
2709
Chunming Zhou3ad81f12016-08-05 17:30:17 +08002710bool amdgpu_need_backup(struct amdgpu_device *adev)
2711{
2712 if (adev->flags & AMD_IS_APU)
2713 return false;
2714
2715 return amdgpu_lockup_timeout > 0 ? true : false;
2716}
2717
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002718static int amdgpu_recover_vram_from_shadow(struct amdgpu_device *adev,
2719 struct amdgpu_ring *ring,
2720 struct amdgpu_bo *bo,
Chris Wilsonf54d1862016-10-25 13:00:45 +01002721 struct dma_fence **fence)
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002722{
2723 uint32_t domain;
2724 int r;
2725
Roger.He23d2e502017-04-21 14:24:26 +08002726 if (!bo->shadow)
2727 return 0;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002728
Alex Xie1d284792017-04-24 13:53:04 -04002729 r = amdgpu_bo_reserve(bo, true);
Roger.He23d2e502017-04-21 14:24:26 +08002730 if (r)
2731 return r;
2732 domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
2733 /* if bo has been evicted, then no need to recover */
2734 if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
Roger.He82521312017-04-21 13:08:43 +08002735 r = amdgpu_bo_validate(bo->shadow);
2736 if (r) {
2737 DRM_ERROR("bo validate failed!\n");
2738 goto err;
2739 }
2740
Roger.He23d2e502017-04-21 14:24:26 +08002741 r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002742 NULL, fence, true);
Roger.He23d2e502017-04-21 14:24:26 +08002743 if (r) {
2744 DRM_ERROR("recover page table failed!\n");
2745 goto err;
2746 }
2747 }
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002748err:
Roger.He23d2e502017-04-21 14:24:26 +08002749 amdgpu_bo_unreserve(bo);
2750 return r;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002751}
2752
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002753/**
Monk Liua90ad3c2017-01-23 14:22:08 +08002754 * amdgpu_sriov_gpu_reset - reset the asic
2755 *
2756 * @adev: amdgpu device pointer
Monk Liu7225f872017-04-26 14:51:54 +08002757 * @job: which job trigger hang
Monk Liua90ad3c2017-01-23 14:22:08 +08002758 *
2759 * Attempt the reset the GPU if it has hung (all asics).
2760 * for SRIOV case.
2761 * Returns 0 for success or an error on failure.
2762 */
Monk Liu7225f872017-04-26 14:51:54 +08002763int amdgpu_sriov_gpu_reset(struct amdgpu_device *adev, struct amdgpu_job *job)
Monk Liua90ad3c2017-01-23 14:22:08 +08002764{
Monk Liu65781c72017-05-11 13:36:44 +08002765 int i, j, r = 0;
Monk Liua90ad3c2017-01-23 14:22:08 +08002766 int resched;
2767 struct amdgpu_bo *bo, *tmp;
2768 struct amdgpu_ring *ring;
2769 struct dma_fence *fence = NULL, *next = NULL;
2770
Monk Liu147b5982017-01-25 15:48:01 +08002771 mutex_lock(&adev->virt.lock_reset);
Monk Liua90ad3c2017-01-23 14:22:08 +08002772 atomic_inc(&adev->gpu_reset_counter);
Monk Liu3224a122017-09-15 18:57:12 +08002773 adev->in_sriov_reset = true;
Monk Liua90ad3c2017-01-23 14:22:08 +08002774
2775 /* block TTM */
2776 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
2777
Monk Liu65781c72017-05-11 13:36:44 +08002778 /* we start from the ring trigger GPU hang */
2779 j = job ? job->ring->idx : 0;
Monk Liua90ad3c2017-01-23 14:22:08 +08002780
Monk Liu65781c72017-05-11 13:36:44 +08002781 /* block scheduler */
2782 for (i = j; i < j + AMDGPU_MAX_RINGS; ++i) {
2783 ring = adev->rings[i % AMDGPU_MAX_RINGS];
Monk Liua90ad3c2017-01-23 14:22:08 +08002784 if (!ring || !ring->sched.thread)
2785 continue;
2786
2787 kthread_park(ring->sched.thread);
Monk Liua90ad3c2017-01-23 14:22:08 +08002788
Monk Liu65781c72017-05-11 13:36:44 +08002789 if (job && j != i)
2790 continue;
2791
Monk Liu4f059ec2017-05-11 13:59:15 +08002792 /* here give the last chance to check if job removed from mirror-list
Monk Liu65781c72017-05-11 13:36:44 +08002793 * since we already pay some time on kthread_park */
Monk Liu4f059ec2017-05-11 13:59:15 +08002794 if (job && list_empty(&job->base.node)) {
Monk Liu65781c72017-05-11 13:36:44 +08002795 kthread_unpark(ring->sched.thread);
2796 goto give_up_reset;
2797 }
2798
2799 if (amd_sched_invalidate_job(&job->base, amdgpu_job_hang_limit))
2800 amd_sched_job_kickout(&job->base);
2801
2802 /* only do job_reset on the hang ring if @job not NULL */
Monk Liua90ad3c2017-01-23 14:22:08 +08002803 amd_sched_hw_job_reset(&ring->sched);
Monk Liu65781c72017-05-11 13:36:44 +08002804
2805 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
2806 amdgpu_fence_driver_force_completion_ring(ring);
Monk Liua90ad3c2017-01-23 14:22:08 +08002807 }
2808
Monk Liua90ad3c2017-01-23 14:22:08 +08002809 /* request to take full control of GPU before re-initialization */
Monk Liu7225f872017-04-26 14:51:54 +08002810 if (job)
Monk Liua90ad3c2017-01-23 14:22:08 +08002811 amdgpu_virt_reset_gpu(adev);
2812 else
2813 amdgpu_virt_request_full_gpu(adev, true);
2814
2815
2816 /* Resume IP prior to SMC */
Monk Liue4f0fdc2017-02-09 11:55:49 +08002817 amdgpu_sriov_reinit_early(adev);
Monk Liua90ad3c2017-01-23 14:22:08 +08002818
2819 /* we need recover gart prior to run SMC/CP/SDMA resume */
2820 amdgpu_ttm_recover_gart(adev);
2821
2822 /* now we are okay to resume SMC/CP/SDMA */
Monk Liue4f0fdc2017-02-09 11:55:49 +08002823 amdgpu_sriov_reinit_late(adev);
Monk Liua90ad3c2017-01-23 14:22:08 +08002824
2825 amdgpu_irq_gpu_reset_resume_helper(adev);
2826
2827 if (amdgpu_ib_ring_tests(adev))
2828 dev_err(adev->dev, "[GPU_RESET] ib ring test failed (%d).\n", r);
2829
2830 /* release full control of GPU after ib test */
2831 amdgpu_virt_release_full_gpu(adev, true);
2832
2833 DRM_INFO("recover vram bo from shadow\n");
2834
2835 ring = adev->mman.buffer_funcs_ring;
2836 mutex_lock(&adev->shadow_list_lock);
2837 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
Monk Liu236763d2017-05-01 16:15:31 +08002838 next = NULL;
Monk Liua90ad3c2017-01-23 14:22:08 +08002839 amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
2840 if (fence) {
2841 r = dma_fence_wait(fence, false);
2842 if (r) {
2843 WARN(r, "recovery from shadow isn't completed\n");
2844 break;
2845 }
2846 }
2847
2848 dma_fence_put(fence);
2849 fence = next;
2850 }
2851 mutex_unlock(&adev->shadow_list_lock);
2852
2853 if (fence) {
2854 r = dma_fence_wait(fence, false);
2855 if (r)
2856 WARN(r, "recovery from shadow isn't completed\n");
2857 }
2858 dma_fence_put(fence);
2859
Monk Liu65781c72017-05-11 13:36:44 +08002860 for (i = j; i < j + AMDGPU_MAX_RINGS; ++i) {
2861 ring = adev->rings[i % AMDGPU_MAX_RINGS];
Monk Liua90ad3c2017-01-23 14:22:08 +08002862 if (!ring || !ring->sched.thread)
2863 continue;
2864
Monk Liu65781c72017-05-11 13:36:44 +08002865 if (job && j != i) {
2866 kthread_unpark(ring->sched.thread);
2867 continue;
2868 }
2869
Monk Liua90ad3c2017-01-23 14:22:08 +08002870 amd_sched_job_recovery(&ring->sched);
2871 kthread_unpark(ring->sched.thread);
2872 }
2873
2874 drm_helper_resume_force_mode(adev->ddev);
Monk Liu65781c72017-05-11 13:36:44 +08002875give_up_reset:
Monk Liua90ad3c2017-01-23 14:22:08 +08002876 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
2877 if (r) {
2878 /* bad news, how to tell it to userspace ? */
2879 dev_info(adev->dev, "GPU reset failed\n");
Monk Liu65781c72017-05-11 13:36:44 +08002880 } else {
2881 dev_info(adev->dev, "GPU reset successed!\n");
Monk Liua90ad3c2017-01-23 14:22:08 +08002882 }
2883
Monk Liu3224a122017-09-15 18:57:12 +08002884 adev->in_sriov_reset = false;
Monk Liu147b5982017-01-25 15:48:01 +08002885 mutex_unlock(&adev->virt.lock_reset);
Monk Liua90ad3c2017-01-23 14:22:08 +08002886 return r;
2887}
2888
2889/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002890 * amdgpu_gpu_reset - reset the asic
2891 *
2892 * @adev: amdgpu device pointer
2893 *
2894 * Attempt the reset the GPU if it has hung (all asics).
2895 * Returns 0 for success or an error on failure.
2896 */
2897int amdgpu_gpu_reset(struct amdgpu_device *adev)
2898{
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002899 int i, r;
2900 int resched;
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002901 bool need_full_reset, vram_lost = false;
Xiangliang Yufb140b22016-12-17 22:48:57 +08002902
Chunming Zhou63fbf422016-07-15 11:19:20 +08002903 if (!amdgpu_check_soft_reset(adev)) {
2904 DRM_INFO("No hardware hang detected. Did some blocks stall?\n");
2905 return 0;
2906 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002907
Marek Olšákd94aed52015-05-05 21:13:49 +02002908 atomic_inc(&adev->gpu_reset_counter);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002909
Chunming Zhoua3c47d62016-06-30 16:44:41 +08002910 /* block TTM */
2911 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
2912
Chunming Zhou0875dc92016-06-12 15:41:58 +08002913 /* block scheduler */
2914 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2915 struct amdgpu_ring *ring = adev->rings[i];
2916
Chunming Zhou51687752017-04-24 17:09:15 +08002917 if (!ring || !ring->sched.thread)
Chunming Zhou0875dc92016-06-12 15:41:58 +08002918 continue;
2919 kthread_park(ring->sched.thread);
Chunming Zhouaa1c8902016-06-30 13:56:02 +08002920 amd_sched_hw_job_reset(&ring->sched);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002921 }
Chunming Zhou2200eda2016-06-30 16:53:02 +08002922 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
2923 amdgpu_fence_driver_force_completion(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002924
Chunming Zhou35d782f2016-07-15 15:57:13 +08002925 need_full_reset = amdgpu_need_full_reset(adev);
2926
2927 if (!need_full_reset) {
2928 amdgpu_pre_soft_reset(adev);
2929 r = amdgpu_soft_reset(adev);
2930 amdgpu_post_soft_reset(adev);
2931 if (r || amdgpu_check_soft_reset(adev)) {
2932 DRM_INFO("soft reset failed, will fallback to full reset!\n");
2933 need_full_reset = true;
2934 }
2935 }
2936
2937 if (need_full_reset) {
Chunming Zhou35d782f2016-07-15 15:57:13 +08002938 r = amdgpu_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002939
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002940retry:
Alex Deucherd05da0e2017-06-30 17:08:45 -04002941 amdgpu_atombios_scratch_regs_save(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002942 r = amdgpu_asic_reset(adev);
Alex Deucherd05da0e2017-06-30 17:08:45 -04002943 amdgpu_atombios_scratch_regs_restore(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002944 /* post card */
2945 amdgpu_atom_asic_init(adev->mode_info.atom_context);
Alex Deucherbfa99262016-01-15 11:59:48 -05002946
Chunming Zhou35d782f2016-07-15 15:57:13 +08002947 if (!r) {
2948 dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
Chunming Zhoufcf06492017-05-05 10:33:33 +08002949 r = amdgpu_resume_phase1(adev);
2950 if (r)
2951 goto out;
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002952 vram_lost = amdgpu_check_vram_lost(adev);
Chunming Zhouf1892132017-05-15 16:48:27 +08002953 if (vram_lost) {
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002954 DRM_ERROR("VRAM is lost!\n");
Chunming Zhouf1892132017-05-15 16:48:27 +08002955 atomic_inc(&adev->vram_lost_counter);
2956 }
Chunming Zhou2c0d7312016-08-30 16:36:25 +08002957 r = amdgpu_ttm_recover_gart(adev);
2958 if (r)
Chunming Zhoufcf06492017-05-05 10:33:33 +08002959 goto out;
2960 r = amdgpu_resume_phase2(adev);
2961 if (r)
2962 goto out;
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002963 if (vram_lost)
2964 amdgpu_fill_reset_magic(adev);
Chunming Zhou2c0d7312016-08-30 16:36:25 +08002965 }
Chunming Zhoufcf06492017-05-05 10:33:33 +08002966 }
2967out:
2968 if (!r) {
2969 amdgpu_irq_gpu_reset_resume_helper(adev);
Chunming Zhou1f465082016-06-30 15:02:26 +08002970 r = amdgpu_ib_ring_tests(adev);
2971 if (r) {
2972 dev_err(adev->dev, "ib ring test failed (%d).\n", r);
Chunming Zhou40019dc2016-06-29 16:01:49 +08002973 r = amdgpu_suspend(adev);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002974 need_full_reset = true;
Chunming Zhou40019dc2016-06-29 16:01:49 +08002975 goto retry;
Chunming Zhou1f465082016-06-30 15:02:26 +08002976 }
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002977 /**
2978 * recovery vm page tables, since we cannot depend on VRAM is
2979 * consistent after gpu full reset.
2980 */
2981 if (need_full_reset && amdgpu_need_backup(adev)) {
2982 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
2983 struct amdgpu_bo *bo, *tmp;
Chris Wilsonf54d1862016-10-25 13:00:45 +01002984 struct dma_fence *fence = NULL, *next = NULL;
Chunming Zhou1f465082016-06-30 15:02:26 +08002985
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002986 DRM_INFO("recover vram bo from shadow\n");
2987 mutex_lock(&adev->shadow_list_lock);
2988 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
Monk Liu236763d2017-05-01 16:15:31 +08002989 next = NULL;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002990 amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
2991 if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01002992 r = dma_fence_wait(fence, false);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002993 if (r) {
Monk Liu1d7b17b2017-01-22 18:52:56 +08002994 WARN(r, "recovery from shadow isn't completed\n");
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002995 break;
2996 }
2997 }
2998
Chris Wilsonf54d1862016-10-25 13:00:45 +01002999 dma_fence_put(fence);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003000 fence = next;
3001 }
3002 mutex_unlock(&adev->shadow_list_lock);
3003 if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01003004 r = dma_fence_wait(fence, false);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003005 if (r)
Monk Liu1d7b17b2017-01-22 18:52:56 +08003006 WARN(r, "recovery from shadow isn't completed\n");
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003007 }
Chris Wilsonf54d1862016-10-25 13:00:45 +01003008 dma_fence_put(fence);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003009 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003010 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
3011 struct amdgpu_ring *ring = adev->rings[i];
Chunming Zhou51687752017-04-24 17:09:15 +08003012
3013 if (!ring || !ring->sched.thread)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003014 continue;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003015
Chunming Zhouaa1c8902016-06-30 13:56:02 +08003016 amd_sched_job_recovery(&ring->sched);
Chunming Zhou0875dc92016-06-12 15:41:58 +08003017 kthread_unpark(ring->sched.thread);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003018 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003019 } else {
Chunming Zhou2200eda2016-06-30 16:53:02 +08003020 dev_err(adev->dev, "asic resume failed (%d).\n", r);
Alex Deuchere23b74a2017-09-28 09:47:32 -04003021 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ASIC_RESUME_FAIL, 0, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003022 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
Chunming Zhou51687752017-04-24 17:09:15 +08003023 if (adev->rings[i] && adev->rings[i]->sched.thread) {
Chunming Zhou0875dc92016-06-12 15:41:58 +08003024 kthread_unpark(adev->rings[i]->sched.thread);
Chunming Zhou0875dc92016-06-12 15:41:58 +08003025 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003026 }
3027 }
3028
3029 drm_helper_resume_force_mode(adev->ddev);
3030
3031 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
Gavin Wan89041942017-06-23 13:55:15 -04003032 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003033 /* bad news, how to tell it to userspace ? */
3034 dev_info(adev->dev, "GPU reset failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04003035 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, r);
Gavin Wan89041942017-06-23 13:55:15 -04003036 }
3037 else {
Chunming Zhou6643be62017-05-05 10:50:09 +08003038 dev_info(adev->dev, "GPU reset successed!\n");
Gavin Wan89041942017-06-23 13:55:15 -04003039 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003040
Gavin Wan89041942017-06-23 13:55:15 -04003041 amdgpu_vf_error_trans_all(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003042 return r;
3043}
3044
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003045void amdgpu_get_pcie_info(struct amdgpu_device *adev)
3046{
3047 u32 mask;
3048 int ret;
3049
Alex Deuchercd474ba2016-02-04 10:21:23 -05003050 if (amdgpu_pcie_gen_cap)
3051 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
3052
3053 if (amdgpu_pcie_lane_cap)
3054 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
3055
3056 /* covers APUs as well */
3057 if (pci_is_root_bus(adev->pdev->bus)) {
3058 if (adev->pm.pcie_gen_mask == 0)
3059 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
3060 if (adev->pm.pcie_mlw_mask == 0)
3061 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003062 return;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003063 }
Alex Deuchercd474ba2016-02-04 10:21:23 -05003064
3065 if (adev->pm.pcie_gen_mask == 0) {
3066 ret = drm_pcie_get_speed_cap_mask(adev->ddev, &mask);
3067 if (!ret) {
3068 adev->pm.pcie_gen_mask = (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
3069 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
3070 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
3071
3072 if (mask & DRM_PCIE_SPEED_25)
3073 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
3074 if (mask & DRM_PCIE_SPEED_50)
3075 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2;
3076 if (mask & DRM_PCIE_SPEED_80)
3077 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3;
3078 } else {
3079 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
3080 }
3081 }
3082 if (adev->pm.pcie_mlw_mask == 0) {
3083 ret = drm_pcie_get_max_link_width(adev->ddev, &mask);
3084 if (!ret) {
3085 switch (mask) {
3086 case 32:
3087 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
3088 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
3089 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3090 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3091 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3092 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3093 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3094 break;
3095 case 16:
3096 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
3097 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3098 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3099 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3100 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3101 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3102 break;
3103 case 12:
3104 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3105 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3106 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3107 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3108 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3109 break;
3110 case 8:
3111 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3112 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3113 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3114 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3115 break;
3116 case 4:
3117 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3118 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3119 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3120 break;
3121 case 2:
3122 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3123 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3124 break;
3125 case 1:
3126 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
3127 break;
3128 default:
3129 break;
3130 }
3131 } else {
3132 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003133 }
3134 }
3135}
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003136
3137/*
3138 * Debugfs
3139 */
3140int amdgpu_debugfs_add_files(struct amdgpu_device *adev,
Nils Wallménius06ab6832016-05-02 12:46:15 -04003141 const struct drm_info_list *files,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003142 unsigned nfiles)
3143{
3144 unsigned i;
3145
3146 for (i = 0; i < adev->debugfs_count; i++) {
3147 if (adev->debugfs[i].files == files) {
3148 /* Already registered */
3149 return 0;
3150 }
3151 }
3152
3153 i = adev->debugfs_count + 1;
3154 if (i > AMDGPU_DEBUGFS_MAX_COMPONENTS) {
3155 DRM_ERROR("Reached maximum number of debugfs components.\n");
3156 DRM_ERROR("Report so we increase "
3157 "AMDGPU_DEBUGFS_MAX_COMPONENTS.\n");
3158 return -EINVAL;
3159 }
3160 adev->debugfs[adev->debugfs_count].files = files;
3161 adev->debugfs[adev->debugfs_count].num_files = nfiles;
3162 adev->debugfs_count = i;
3163#if defined(CONFIG_DEBUG_FS)
3164 drm_debugfs_create_files(files, nfiles,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003165 adev->ddev->primary->debugfs_root,
3166 adev->ddev->primary);
3167#endif
3168 return 0;
3169}
3170
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003171#if defined(CONFIG_DEBUG_FS)
3172
3173static ssize_t amdgpu_debugfs_regs_read(struct file *f, char __user *buf,
3174 size_t size, loff_t *pos)
3175{
Al Viro45063092016-12-04 18:24:56 -05003176 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003177 ssize_t result = 0;
3178 int r;
Tom St Denisbd122672016-07-28 09:39:22 -04003179 bool pm_pg_lock, use_bank;
Tom St Denis566281592016-06-27 11:55:07 -04003180 unsigned instance_bank, sh_bank, se_bank;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003181
3182 if (size & 0x3 || *pos & 0x3)
3183 return -EINVAL;
3184
Tom St Denisbd122672016-07-28 09:39:22 -04003185 /* are we reading registers for which a PG lock is necessary? */
3186 pm_pg_lock = (*pos >> 23) & 1;
3187
Tom St Denis566281592016-06-27 11:55:07 -04003188 if (*pos & (1ULL << 62)) {
3189 se_bank = (*pos >> 24) & 0x3FF;
3190 sh_bank = (*pos >> 34) & 0x3FF;
3191 instance_bank = (*pos >> 44) & 0x3FF;
Tom St Denis32977f92016-10-09 07:41:26 -04003192
3193 if (se_bank == 0x3FF)
3194 se_bank = 0xFFFFFFFF;
3195 if (sh_bank == 0x3FF)
3196 sh_bank = 0xFFFFFFFF;
3197 if (instance_bank == 0x3FF)
3198 instance_bank = 0xFFFFFFFF;
Tom St Denis566281592016-06-27 11:55:07 -04003199 use_bank = 1;
Tom St Denis566281592016-06-27 11:55:07 -04003200 } else {
3201 use_bank = 0;
3202 }
3203
Tom St Denis801a6aa9a62017-03-15 05:34:25 -04003204 *pos &= (1UL << 22) - 1;
Tom St Denisbd122672016-07-28 09:39:22 -04003205
Tom St Denis566281592016-06-27 11:55:07 -04003206 if (use_bank) {
Tom St Denis32977f92016-10-09 07:41:26 -04003207 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
3208 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
Tom St Denis566281592016-06-27 11:55:07 -04003209 return -EINVAL;
3210 mutex_lock(&adev->grbm_idx_mutex);
3211 amdgpu_gfx_select_se_sh(adev, se_bank,
3212 sh_bank, instance_bank);
3213 }
3214
Tom St Denisbd122672016-07-28 09:39:22 -04003215 if (pm_pg_lock)
3216 mutex_lock(&adev->pm.mutex);
3217
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003218 while (size) {
3219 uint32_t value;
3220
3221 if (*pos > adev->rmmio_size)
Tom St Denis566281592016-06-27 11:55:07 -04003222 goto end;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003223
3224 value = RREG32(*pos >> 2);
3225 r = put_user(value, (uint32_t *)buf);
Tom St Denis566281592016-06-27 11:55:07 -04003226 if (r) {
3227 result = r;
3228 goto end;
3229 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003230
3231 result += 4;
3232 buf += 4;
3233 *pos += 4;
3234 size -= 4;
3235 }
3236
Tom St Denis566281592016-06-27 11:55:07 -04003237end:
3238 if (use_bank) {
3239 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
3240 mutex_unlock(&adev->grbm_idx_mutex);
3241 }
3242
Tom St Denisbd122672016-07-28 09:39:22 -04003243 if (pm_pg_lock)
3244 mutex_unlock(&adev->pm.mutex);
3245
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003246 return result;
3247}
3248
3249static ssize_t amdgpu_debugfs_regs_write(struct file *f, const char __user *buf,
3250 size_t size, loff_t *pos)
3251{
Al Viro45063092016-12-04 18:24:56 -05003252 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003253 ssize_t result = 0;
3254 int r;
Tom St Denis394fdde2016-10-10 07:31:23 -04003255 bool pm_pg_lock, use_bank;
3256 unsigned instance_bank, sh_bank, se_bank;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003257
3258 if (size & 0x3 || *pos & 0x3)
3259 return -EINVAL;
3260
Tom St Denis394fdde2016-10-10 07:31:23 -04003261 /* are we reading registers for which a PG lock is necessary? */
3262 pm_pg_lock = (*pos >> 23) & 1;
3263
3264 if (*pos & (1ULL << 62)) {
3265 se_bank = (*pos >> 24) & 0x3FF;
3266 sh_bank = (*pos >> 34) & 0x3FF;
3267 instance_bank = (*pos >> 44) & 0x3FF;
3268
3269 if (se_bank == 0x3FF)
3270 se_bank = 0xFFFFFFFF;
3271 if (sh_bank == 0x3FF)
3272 sh_bank = 0xFFFFFFFF;
3273 if (instance_bank == 0x3FF)
3274 instance_bank = 0xFFFFFFFF;
3275 use_bank = 1;
3276 } else {
3277 use_bank = 0;
3278 }
3279
Tom St Denis801a6aa9a62017-03-15 05:34:25 -04003280 *pos &= (1UL << 22) - 1;
Tom St Denis394fdde2016-10-10 07:31:23 -04003281
3282 if (use_bank) {
3283 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
3284 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
3285 return -EINVAL;
3286 mutex_lock(&adev->grbm_idx_mutex);
3287 amdgpu_gfx_select_se_sh(adev, se_bank,
3288 sh_bank, instance_bank);
3289 }
3290
3291 if (pm_pg_lock)
3292 mutex_lock(&adev->pm.mutex);
3293
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003294 while (size) {
3295 uint32_t value;
3296
3297 if (*pos > adev->rmmio_size)
3298 return result;
3299
3300 r = get_user(value, (uint32_t *)buf);
3301 if (r)
3302 return r;
3303
3304 WREG32(*pos >> 2, value);
3305
3306 result += 4;
3307 buf += 4;
3308 *pos += 4;
3309 size -= 4;
3310 }
3311
Tom St Denis394fdde2016-10-10 07:31:23 -04003312 if (use_bank) {
3313 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
3314 mutex_unlock(&adev->grbm_idx_mutex);
3315 }
3316
3317 if (pm_pg_lock)
3318 mutex_unlock(&adev->pm.mutex);
3319
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003320 return result;
3321}
3322
Tom St Denisadcec282016-04-15 13:08:44 -04003323static ssize_t amdgpu_debugfs_regs_pcie_read(struct file *f, char __user *buf,
3324 size_t size, loff_t *pos)
3325{
Al Viro45063092016-12-04 18:24:56 -05003326 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003327 ssize_t result = 0;
3328 int r;
3329
3330 if (size & 0x3 || *pos & 0x3)
3331 return -EINVAL;
3332
3333 while (size) {
3334 uint32_t value;
3335
3336 value = RREG32_PCIE(*pos >> 2);
3337 r = put_user(value, (uint32_t *)buf);
3338 if (r)
3339 return r;
3340
3341 result += 4;
3342 buf += 4;
3343 *pos += 4;
3344 size -= 4;
3345 }
3346
3347 return result;
3348}
3349
3350static ssize_t amdgpu_debugfs_regs_pcie_write(struct file *f, const char __user *buf,
3351 size_t size, loff_t *pos)
3352{
Al Viro45063092016-12-04 18:24:56 -05003353 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003354 ssize_t result = 0;
3355 int r;
3356
3357 if (size & 0x3 || *pos & 0x3)
3358 return -EINVAL;
3359
3360 while (size) {
3361 uint32_t value;
3362
3363 r = get_user(value, (uint32_t *)buf);
3364 if (r)
3365 return r;
3366
3367 WREG32_PCIE(*pos >> 2, value);
3368
3369 result += 4;
3370 buf += 4;
3371 *pos += 4;
3372 size -= 4;
3373 }
3374
3375 return result;
3376}
3377
3378static ssize_t amdgpu_debugfs_regs_didt_read(struct file *f, char __user *buf,
3379 size_t size, loff_t *pos)
3380{
Al Viro45063092016-12-04 18:24:56 -05003381 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003382 ssize_t result = 0;
3383 int r;
3384
3385 if (size & 0x3 || *pos & 0x3)
3386 return -EINVAL;
3387
3388 while (size) {
3389 uint32_t value;
3390
3391 value = RREG32_DIDT(*pos >> 2);
3392 r = put_user(value, (uint32_t *)buf);
3393 if (r)
3394 return r;
3395
3396 result += 4;
3397 buf += 4;
3398 *pos += 4;
3399 size -= 4;
3400 }
3401
3402 return result;
3403}
3404
3405static ssize_t amdgpu_debugfs_regs_didt_write(struct file *f, const char __user *buf,
3406 size_t size, loff_t *pos)
3407{
Al Viro45063092016-12-04 18:24:56 -05003408 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003409 ssize_t result = 0;
3410 int r;
3411
3412 if (size & 0x3 || *pos & 0x3)
3413 return -EINVAL;
3414
3415 while (size) {
3416 uint32_t value;
3417
3418 r = get_user(value, (uint32_t *)buf);
3419 if (r)
3420 return r;
3421
3422 WREG32_DIDT(*pos >> 2, value);
3423
3424 result += 4;
3425 buf += 4;
3426 *pos += 4;
3427 size -= 4;
3428 }
3429
3430 return result;
3431}
3432
3433static ssize_t amdgpu_debugfs_regs_smc_read(struct file *f, char __user *buf,
3434 size_t size, loff_t *pos)
3435{
Al Viro45063092016-12-04 18:24:56 -05003436 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003437 ssize_t result = 0;
3438 int r;
3439
3440 if (size & 0x3 || *pos & 0x3)
3441 return -EINVAL;
3442
3443 while (size) {
3444 uint32_t value;
3445
Tom St Denis6fc0dea2016-08-29 08:39:29 -04003446 value = RREG32_SMC(*pos);
Tom St Denisadcec282016-04-15 13:08:44 -04003447 r = put_user(value, (uint32_t *)buf);
3448 if (r)
3449 return r;
3450
3451 result += 4;
3452 buf += 4;
3453 *pos += 4;
3454 size -= 4;
3455 }
3456
3457 return result;
3458}
3459
3460static ssize_t amdgpu_debugfs_regs_smc_write(struct file *f, const char __user *buf,
3461 size_t size, loff_t *pos)
3462{
Al Viro45063092016-12-04 18:24:56 -05003463 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003464 ssize_t result = 0;
3465 int r;
3466
3467 if (size & 0x3 || *pos & 0x3)
3468 return -EINVAL;
3469
3470 while (size) {
3471 uint32_t value;
3472
3473 r = get_user(value, (uint32_t *)buf);
3474 if (r)
3475 return r;
3476
Tom St Denis6fc0dea2016-08-29 08:39:29 -04003477 WREG32_SMC(*pos, value);
Tom St Denisadcec282016-04-15 13:08:44 -04003478
3479 result += 4;
3480 buf += 4;
3481 *pos += 4;
3482 size -= 4;
3483 }
3484
3485 return result;
3486}
3487
Tom St Denis1e051412016-06-27 09:57:18 -04003488static ssize_t amdgpu_debugfs_gca_config_read(struct file *f, char __user *buf,
3489 size_t size, loff_t *pos)
3490{
Al Viro45063092016-12-04 18:24:56 -05003491 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denis1e051412016-06-27 09:57:18 -04003492 ssize_t result = 0;
3493 int r;
3494 uint32_t *config, no_regs = 0;
3495
3496 if (size & 0x3 || *pos & 0x3)
3497 return -EINVAL;
3498
Markus Elfringecab7662016-09-18 17:00:52 +02003499 config = kmalloc_array(256, sizeof(*config), GFP_KERNEL);
Tom St Denis1e051412016-06-27 09:57:18 -04003500 if (!config)
3501 return -ENOMEM;
3502
3503 /* version, increment each time something is added */
Tom St Denis9a999352017-01-18 13:01:25 -05003504 config[no_regs++] = 3;
Tom St Denis1e051412016-06-27 09:57:18 -04003505 config[no_regs++] = adev->gfx.config.max_shader_engines;
3506 config[no_regs++] = adev->gfx.config.max_tile_pipes;
3507 config[no_regs++] = adev->gfx.config.max_cu_per_sh;
3508 config[no_regs++] = adev->gfx.config.max_sh_per_se;
3509 config[no_regs++] = adev->gfx.config.max_backends_per_se;
3510 config[no_regs++] = adev->gfx.config.max_texture_channel_caches;
3511 config[no_regs++] = adev->gfx.config.max_gprs;
3512 config[no_regs++] = adev->gfx.config.max_gs_threads;
3513 config[no_regs++] = adev->gfx.config.max_hw_contexts;
3514 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_frontend;
3515 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_backend;
3516 config[no_regs++] = adev->gfx.config.sc_hiz_tile_fifo_size;
3517 config[no_regs++] = adev->gfx.config.sc_earlyz_tile_fifo_size;
3518 config[no_regs++] = adev->gfx.config.num_tile_pipes;
3519 config[no_regs++] = adev->gfx.config.backend_enable_mask;
3520 config[no_regs++] = adev->gfx.config.mem_max_burst_length_bytes;
3521 config[no_regs++] = adev->gfx.config.mem_row_size_in_kb;
3522 config[no_regs++] = adev->gfx.config.shader_engine_tile_size;
3523 config[no_regs++] = adev->gfx.config.num_gpus;
3524 config[no_regs++] = adev->gfx.config.multi_gpu_tile_size;
3525 config[no_regs++] = adev->gfx.config.mc_arb_ramcfg;
3526 config[no_regs++] = adev->gfx.config.gb_addr_config;
3527 config[no_regs++] = adev->gfx.config.num_rbs;
3528
Tom St Denis89a8f302016-08-12 15:14:31 -04003529 /* rev==1 */
3530 config[no_regs++] = adev->rev_id;
3531 config[no_regs++] = adev->pg_flags;
3532 config[no_regs++] = adev->cg_flags;
3533
Tom St Denise9f11dc2016-08-17 12:00:51 -04003534 /* rev==2 */
3535 config[no_regs++] = adev->family;
3536 config[no_regs++] = adev->external_rev_id;
3537
Tom St Denis9a999352017-01-18 13:01:25 -05003538 /* rev==3 */
3539 config[no_regs++] = adev->pdev->device;
3540 config[no_regs++] = adev->pdev->revision;
3541 config[no_regs++] = adev->pdev->subsystem_device;
3542 config[no_regs++] = adev->pdev->subsystem_vendor;
3543
Tom St Denis1e051412016-06-27 09:57:18 -04003544 while (size && (*pos < no_regs * 4)) {
3545 uint32_t value;
3546
3547 value = config[*pos >> 2];
3548 r = put_user(value, (uint32_t *)buf);
3549 if (r) {
3550 kfree(config);
3551 return r;
3552 }
3553
3554 result += 4;
3555 buf += 4;
3556 *pos += 4;
3557 size -= 4;
3558 }
3559
3560 kfree(config);
3561 return result;
3562}
3563
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003564static ssize_t amdgpu_debugfs_sensor_read(struct file *f, char __user *buf,
3565 size_t size, loff_t *pos)
3566{
Al Viro45063092016-12-04 18:24:56 -05003567 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003568 int idx, x, outsize, r, valuesize;
3569 uint32_t values[16];
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003570
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003571 if (size & 3 || *pos & 0x3)
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003572 return -EINVAL;
3573
Samuel Pitoiset3cbc6142017-02-15 19:32:29 +01003574 if (amdgpu_dpm == 0)
3575 return -EINVAL;
3576
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003577 /* convert offset to sensor number */
3578 idx = *pos >> 2;
3579
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003580 valuesize = sizeof(values);
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003581 if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->read_sensor)
Rex Zhucd4d7462017-09-06 18:43:52 +08003582 r = amdgpu_dpm_read_sensor(adev, idx, &values[0], &valuesize);
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003583 else
3584 return -EINVAL;
3585
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003586 if (size > valuesize)
3587 return -EINVAL;
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003588
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003589 outsize = 0;
3590 x = 0;
3591 if (!r) {
3592 while (size) {
3593 r = put_user(values[x++], (int32_t *)buf);
3594 buf += 4;
3595 size -= 4;
3596 outsize += 4;
3597 }
3598 }
3599
3600 return !r ? outsize : r;
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003601}
Tom St Denis1e051412016-06-27 09:57:18 -04003602
Tom St Denis273d7aa2016-10-11 14:48:55 -04003603static ssize_t amdgpu_debugfs_wave_read(struct file *f, char __user *buf,
3604 size_t size, loff_t *pos)
3605{
3606 struct amdgpu_device *adev = f->f_inode->i_private;
3607 int r, x;
3608 ssize_t result=0;
Tom St Denis472259f2016-10-14 09:49:09 -04003609 uint32_t offset, se, sh, cu, wave, simd, data[32];
Tom St Denis273d7aa2016-10-11 14:48:55 -04003610
3611 if (size & 3 || *pos & 3)
3612 return -EINVAL;
3613
3614 /* decode offset */
3615 offset = (*pos & 0x7F);
3616 se = ((*pos >> 7) & 0xFF);
3617 sh = ((*pos >> 15) & 0xFF);
3618 cu = ((*pos >> 23) & 0xFF);
3619 wave = ((*pos >> 31) & 0xFF);
3620 simd = ((*pos >> 37) & 0xFF);
Tom St Denis273d7aa2016-10-11 14:48:55 -04003621
3622 /* switch to the specific se/sh/cu */
3623 mutex_lock(&adev->grbm_idx_mutex);
3624 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
3625
3626 x = 0;
Tom St Denis472259f2016-10-14 09:49:09 -04003627 if (adev->gfx.funcs->read_wave_data)
3628 adev->gfx.funcs->read_wave_data(adev, simd, wave, data, &x);
Tom St Denis273d7aa2016-10-11 14:48:55 -04003629
3630 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
3631 mutex_unlock(&adev->grbm_idx_mutex);
3632
Tom St Denis5ecfb3b2016-10-13 12:15:03 -04003633 if (!x)
3634 return -EINVAL;
3635
Tom St Denis472259f2016-10-14 09:49:09 -04003636 while (size && (offset < x * 4)) {
Tom St Denis273d7aa2016-10-11 14:48:55 -04003637 uint32_t value;
3638
Tom St Denis472259f2016-10-14 09:49:09 -04003639 value = data[offset >> 2];
Tom St Denis273d7aa2016-10-11 14:48:55 -04003640 r = put_user(value, (uint32_t *)buf);
3641 if (r)
3642 return r;
3643
3644 result += 4;
3645 buf += 4;
Tom St Denis472259f2016-10-14 09:49:09 -04003646 offset += 4;
Tom St Denis273d7aa2016-10-11 14:48:55 -04003647 size -= 4;
3648 }
3649
3650 return result;
3651}
3652
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003653static ssize_t amdgpu_debugfs_gpr_read(struct file *f, char __user *buf,
3654 size_t size, loff_t *pos)
3655{
3656 struct amdgpu_device *adev = f->f_inode->i_private;
3657 int r;
3658 ssize_t result = 0;
3659 uint32_t offset, se, sh, cu, wave, simd, thread, bank, *data;
3660
3661 if (size & 3 || *pos & 3)
3662 return -EINVAL;
3663
3664 /* decode offset */
3665 offset = (*pos & 0xFFF); /* in dwords */
3666 se = ((*pos >> 12) & 0xFF);
3667 sh = ((*pos >> 20) & 0xFF);
3668 cu = ((*pos >> 28) & 0xFF);
3669 wave = ((*pos >> 36) & 0xFF);
3670 simd = ((*pos >> 44) & 0xFF);
3671 thread = ((*pos >> 52) & 0xFF);
3672 bank = ((*pos >> 60) & 1);
3673
3674 data = kmalloc_array(1024, sizeof(*data), GFP_KERNEL);
3675 if (!data)
3676 return -ENOMEM;
3677
3678 /* switch to the specific se/sh/cu */
3679 mutex_lock(&adev->grbm_idx_mutex);
3680 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
3681
3682 if (bank == 0) {
3683 if (adev->gfx.funcs->read_wave_vgprs)
3684 adev->gfx.funcs->read_wave_vgprs(adev, simd, wave, thread, offset, size>>2, data);
3685 } else {
3686 if (adev->gfx.funcs->read_wave_sgprs)
3687 adev->gfx.funcs->read_wave_sgprs(adev, simd, wave, offset, size>>2, data);
3688 }
3689
3690 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
3691 mutex_unlock(&adev->grbm_idx_mutex);
3692
3693 while (size) {
3694 uint32_t value;
3695
3696 value = data[offset++];
3697 r = put_user(value, (uint32_t *)buf);
3698 if (r) {
3699 result = r;
3700 goto err;
3701 }
3702
3703 result += 4;
3704 buf += 4;
3705 size -= 4;
3706 }
3707
3708err:
3709 kfree(data);
3710 return result;
3711}
3712
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003713static const struct file_operations amdgpu_debugfs_regs_fops = {
3714 .owner = THIS_MODULE,
3715 .read = amdgpu_debugfs_regs_read,
3716 .write = amdgpu_debugfs_regs_write,
3717 .llseek = default_llseek
3718};
Tom St Denisadcec282016-04-15 13:08:44 -04003719static const struct file_operations amdgpu_debugfs_regs_didt_fops = {
3720 .owner = THIS_MODULE,
3721 .read = amdgpu_debugfs_regs_didt_read,
3722 .write = amdgpu_debugfs_regs_didt_write,
3723 .llseek = default_llseek
3724};
3725static const struct file_operations amdgpu_debugfs_regs_pcie_fops = {
3726 .owner = THIS_MODULE,
3727 .read = amdgpu_debugfs_regs_pcie_read,
3728 .write = amdgpu_debugfs_regs_pcie_write,
3729 .llseek = default_llseek
3730};
3731static const struct file_operations amdgpu_debugfs_regs_smc_fops = {
3732 .owner = THIS_MODULE,
3733 .read = amdgpu_debugfs_regs_smc_read,
3734 .write = amdgpu_debugfs_regs_smc_write,
3735 .llseek = default_llseek
3736};
3737
Tom St Denis1e051412016-06-27 09:57:18 -04003738static const struct file_operations amdgpu_debugfs_gca_config_fops = {
3739 .owner = THIS_MODULE,
3740 .read = amdgpu_debugfs_gca_config_read,
3741 .llseek = default_llseek
3742};
3743
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003744static const struct file_operations amdgpu_debugfs_sensors_fops = {
3745 .owner = THIS_MODULE,
3746 .read = amdgpu_debugfs_sensor_read,
3747 .llseek = default_llseek
3748};
3749
Tom St Denis273d7aa2016-10-11 14:48:55 -04003750static const struct file_operations amdgpu_debugfs_wave_fops = {
3751 .owner = THIS_MODULE,
3752 .read = amdgpu_debugfs_wave_read,
3753 .llseek = default_llseek
3754};
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003755static const struct file_operations amdgpu_debugfs_gpr_fops = {
3756 .owner = THIS_MODULE,
3757 .read = amdgpu_debugfs_gpr_read,
3758 .llseek = default_llseek
3759};
Tom St Denis273d7aa2016-10-11 14:48:55 -04003760
Tom St Denisadcec282016-04-15 13:08:44 -04003761static const struct file_operations *debugfs_regs[] = {
3762 &amdgpu_debugfs_regs_fops,
3763 &amdgpu_debugfs_regs_didt_fops,
3764 &amdgpu_debugfs_regs_pcie_fops,
3765 &amdgpu_debugfs_regs_smc_fops,
Tom St Denis1e051412016-06-27 09:57:18 -04003766 &amdgpu_debugfs_gca_config_fops,
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003767 &amdgpu_debugfs_sensors_fops,
Tom St Denis273d7aa2016-10-11 14:48:55 -04003768 &amdgpu_debugfs_wave_fops,
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003769 &amdgpu_debugfs_gpr_fops,
Tom St Denisadcec282016-04-15 13:08:44 -04003770};
3771
3772static const char *debugfs_regs_names[] = {
3773 "amdgpu_regs",
3774 "amdgpu_regs_didt",
3775 "amdgpu_regs_pcie",
3776 "amdgpu_regs_smc",
Tom St Denis1e051412016-06-27 09:57:18 -04003777 "amdgpu_gca_config",
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003778 "amdgpu_sensors",
Tom St Denis273d7aa2016-10-11 14:48:55 -04003779 "amdgpu_wave",
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003780 "amdgpu_gpr",
Tom St Denisadcec282016-04-15 13:08:44 -04003781};
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003782
3783static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
3784{
3785 struct drm_minor *minor = adev->ddev->primary;
3786 struct dentry *ent, *root = minor->debugfs_root;
Tom St Denisadcec282016-04-15 13:08:44 -04003787 unsigned i, j;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003788
Tom St Denisadcec282016-04-15 13:08:44 -04003789 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3790 ent = debugfs_create_file(debugfs_regs_names[i],
3791 S_IFREG | S_IRUGO, root,
3792 adev, debugfs_regs[i]);
3793 if (IS_ERR(ent)) {
3794 for (j = 0; j < i; j++) {
3795 debugfs_remove(adev->debugfs_regs[i]);
3796 adev->debugfs_regs[i] = NULL;
3797 }
3798 return PTR_ERR(ent);
3799 }
3800
3801 if (!i)
3802 i_size_write(ent->d_inode, adev->rmmio_size);
3803 adev->debugfs_regs[i] = ent;
3804 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003805
3806 return 0;
3807}
3808
3809static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev)
3810{
Tom St Denisadcec282016-04-15 13:08:44 -04003811 unsigned i;
3812
3813 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3814 if (adev->debugfs_regs[i]) {
3815 debugfs_remove(adev->debugfs_regs[i]);
3816 adev->debugfs_regs[i] = NULL;
3817 }
3818 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003819}
3820
Huang Rui4f0955f2017-05-10 23:04:06 +08003821static int amdgpu_debugfs_test_ib(struct seq_file *m, void *data)
3822{
3823 struct drm_info_node *node = (struct drm_info_node *) m->private;
3824 struct drm_device *dev = node->minor->dev;
3825 struct amdgpu_device *adev = dev->dev_private;
3826 int r = 0, i;
3827
3828 /* hold on the scheduler */
3829 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
3830 struct amdgpu_ring *ring = adev->rings[i];
3831
3832 if (!ring || !ring->sched.thread)
3833 continue;
3834 kthread_park(ring->sched.thread);
3835 }
3836
3837 seq_printf(m, "run ib test:\n");
3838 r = amdgpu_ib_ring_tests(adev);
3839 if (r)
3840 seq_printf(m, "ib ring tests failed (%d).\n", r);
3841 else
3842 seq_printf(m, "ib ring tests passed.\n");
3843
3844 /* go on the scheduler */
3845 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
3846 struct amdgpu_ring *ring = adev->rings[i];
3847
3848 if (!ring || !ring->sched.thread)
3849 continue;
3850 kthread_unpark(ring->sched.thread);
3851 }
3852
3853 return 0;
3854}
3855
3856static const struct drm_info_list amdgpu_debugfs_test_ib_ring_list[] = {
3857 {"amdgpu_test_ib", &amdgpu_debugfs_test_ib}
3858};
3859
3860static int amdgpu_debugfs_test_ib_ring_init(struct amdgpu_device *adev)
3861{
3862 return amdgpu_debugfs_add_files(adev,
3863 amdgpu_debugfs_test_ib_ring_list, 1);
3864}
3865
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003866int amdgpu_debugfs_init(struct drm_minor *minor)
3867{
3868 return 0;
3869}
Kent Russelldb95e212017-08-22 12:31:43 -04003870
3871static int amdgpu_debugfs_get_vbios_dump(struct seq_file *m, void *data)
3872{
3873 struct drm_info_node *node = (struct drm_info_node *) m->private;
3874 struct drm_device *dev = node->minor->dev;
3875 struct amdgpu_device *adev = dev->dev_private;
3876
3877 seq_write(m, adev->bios, adev->bios_size);
3878 return 0;
3879}
3880
Kent Russelldb95e212017-08-22 12:31:43 -04003881static const struct drm_info_list amdgpu_vbios_dump_list[] = {
3882 {"amdgpu_vbios",
3883 amdgpu_debugfs_get_vbios_dump,
3884 0, NULL},
3885};
3886
Kent Russelldb95e212017-08-22 12:31:43 -04003887static int amdgpu_debugfs_vbios_dump_init(struct amdgpu_device *adev)
3888{
3889 return amdgpu_debugfs_add_files(adev,
3890 amdgpu_vbios_dump_list, 1);
3891}
Alexander Kuleshov7cebc722015-06-27 13:16:05 +06003892#else
Arnd Bergmann27bad5b2017-06-21 23:51:02 +02003893static int amdgpu_debugfs_test_ib_ring_init(struct amdgpu_device *adev)
Huang Rui4f0955f2017-05-10 23:04:06 +08003894{
3895 return 0;
3896}
Alexander Kuleshov7cebc722015-06-27 13:16:05 +06003897static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
3898{
3899 return 0;
3900}
Kent Russelldb95e212017-08-22 12:31:43 -04003901static int amdgpu_debugfs_vbios_dump_init(struct amdgpu_device *adev)
3902{
3903 return 0;
3904}
Alexander Kuleshov7cebc722015-06-27 13:16:05 +06003905static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev) { }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003906#endif