blob: 0b9332e65a4c35552edf351fd1efc5503dd0599f [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
Jim Quc836fec2017-02-10 15:59:59 +0800747 if (adev->has_hw_reset) {
748 adev->has_hw_reset = false;
749 return true;
750 }
Alex Deucher70d17a22017-06-30 17:26:47 -0400751
752 /* bios scratch used on CIK+ */
753 if (adev->asic_type >= CHIP_BONAIRE)
754 return amdgpu_atombios_scratch_need_asic_init(adev);
755
756 /* check MEM_SIZE for older asics */
Alex Deucherbbf282d2017-03-03 17:26:10 -0500757 reg = amdgpu_asic_get_config_memsize(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400758
Alex Deucherf2713e82017-03-28 12:19:31 -0400759 if ((reg != 0) && (reg != 0xffffffff))
Jim Quc836fec2017-02-10 15:59:59 +0800760 return false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400761
Jim Quc836fec2017-02-10 15:59:59 +0800762 return true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400763
764}
765
Monk Liubec86372016-09-14 19:38:08 +0800766static bool amdgpu_vpost_needed(struct amdgpu_device *adev)
767{
768 if (amdgpu_sriov_vf(adev))
769 return false;
770
771 if (amdgpu_passthrough(adev)) {
Monk Liu1da2c322016-11-11 11:24:29 +0800772 /* for FIJI: In whole GPU pass-through virtualization case, after VM reboot
773 * some old smc fw still need driver do vPost otherwise gpu hang, while
774 * those smc fw version above 22.15 doesn't have this flaw, so we force
775 * vpost executed for smc version below 22.15
Monk Liubec86372016-09-14 19:38:08 +0800776 */
777 if (adev->asic_type == CHIP_FIJI) {
778 int err;
779 uint32_t fw_ver;
780 err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
781 /* force vPost if error occured */
782 if (err)
783 return true;
784
785 fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
Monk Liu1da2c322016-11-11 11:24:29 +0800786 if (fw_ver < 0x00160e00)
787 return true;
Monk Liubec86372016-09-14 19:38:08 +0800788 }
Monk Liubec86372016-09-14 19:38:08 +0800789 }
Jim Quc836fec2017-02-10 15:59:59 +0800790 return amdgpu_need_post(adev);
Monk Liubec86372016-09-14 19:38:08 +0800791}
792
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400793/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400794 * amdgpu_dummy_page_init - init dummy page used by the driver
795 *
796 * @adev: amdgpu_device pointer
797 *
798 * Allocate the dummy page used by the driver (all asics).
799 * This dummy page is used by the driver as a filler for gart entries
800 * when pages are taken out of the GART
801 * Returns 0 on sucess, -ENOMEM on failure.
802 */
803int amdgpu_dummy_page_init(struct amdgpu_device *adev)
804{
805 if (adev->dummy_page.page)
806 return 0;
807 adev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
808 if (adev->dummy_page.page == NULL)
809 return -ENOMEM;
810 adev->dummy_page.addr = pci_map_page(adev->pdev, adev->dummy_page.page,
811 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
812 if (pci_dma_mapping_error(adev->pdev, adev->dummy_page.addr)) {
813 dev_err(&adev->pdev->dev, "Failed to DMA MAP the dummy page\n");
814 __free_page(adev->dummy_page.page);
815 adev->dummy_page.page = NULL;
816 return -ENOMEM;
817 }
818 return 0;
819}
820
821/**
822 * amdgpu_dummy_page_fini - free dummy page used by the driver
823 *
824 * @adev: amdgpu_device pointer
825 *
826 * Frees the dummy page used by the driver (all asics).
827 */
828void amdgpu_dummy_page_fini(struct amdgpu_device *adev)
829{
830 if (adev->dummy_page.page == NULL)
831 return;
832 pci_unmap_page(adev->pdev, adev->dummy_page.addr,
833 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
834 __free_page(adev->dummy_page.page);
835 adev->dummy_page.page = NULL;
836}
837
838
839/* ATOM accessor methods */
840/*
841 * ATOM is an interpreted byte code stored in tables in the vbios. The
842 * driver registers callbacks to access registers and the interpreter
843 * in the driver parses the tables and executes then to program specific
844 * actions (set display modes, asic init, etc.). See amdgpu_atombios.c,
845 * atombios.h, and atom.c
846 */
847
848/**
849 * cail_pll_read - read PLL register
850 *
851 * @info: atom card_info pointer
852 * @reg: PLL register offset
853 *
854 * Provides a PLL register accessor for the atom interpreter (r4xx+).
855 * Returns the value of the PLL register.
856 */
857static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
858{
859 return 0;
860}
861
862/**
863 * cail_pll_write - write PLL register
864 *
865 * @info: atom card_info pointer
866 * @reg: PLL register offset
867 * @val: value to write to the pll register
868 *
869 * Provides a PLL register accessor for the atom interpreter (r4xx+).
870 */
871static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
872{
873
874}
875
876/**
877 * cail_mc_read - read MC (Memory Controller) register
878 *
879 * @info: atom card_info pointer
880 * @reg: MC register offset
881 *
882 * Provides an MC register accessor for the atom interpreter (r4xx+).
883 * Returns the value of the MC register.
884 */
885static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
886{
887 return 0;
888}
889
890/**
891 * cail_mc_write - write MC (Memory Controller) register
892 *
893 * @info: atom card_info pointer
894 * @reg: MC register offset
895 * @val: value to write to the pll register
896 *
897 * Provides a MC register accessor for the atom interpreter (r4xx+).
898 */
899static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
900{
901
902}
903
904/**
905 * cail_reg_write - write MMIO register
906 *
907 * @info: atom card_info pointer
908 * @reg: MMIO register offset
909 * @val: value to write to the pll register
910 *
911 * Provides a MMIO register accessor for the atom interpreter (r4xx+).
912 */
913static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
914{
915 struct amdgpu_device *adev = info->dev->dev_private;
916
917 WREG32(reg, val);
918}
919
920/**
921 * cail_reg_read - read MMIO register
922 *
923 * @info: atom card_info pointer
924 * @reg: MMIO register offset
925 *
926 * Provides an MMIO register accessor for the atom interpreter (r4xx+).
927 * Returns the value of the MMIO register.
928 */
929static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
930{
931 struct amdgpu_device *adev = info->dev->dev_private;
932 uint32_t r;
933
934 r = RREG32(reg);
935 return r;
936}
937
938/**
939 * cail_ioreg_write - write IO register
940 *
941 * @info: atom card_info pointer
942 * @reg: IO register offset
943 * @val: value to write to the pll register
944 *
945 * Provides a IO register accessor for the atom interpreter (r4xx+).
946 */
947static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
948{
949 struct amdgpu_device *adev = info->dev->dev_private;
950
951 WREG32_IO(reg, val);
952}
953
954/**
955 * cail_ioreg_read - read IO register
956 *
957 * @info: atom card_info pointer
958 * @reg: IO register offset
959 *
960 * Provides an IO register accessor for the atom interpreter (r4xx+).
961 * Returns the value of the IO register.
962 */
963static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
964{
965 struct amdgpu_device *adev = info->dev->dev_private;
966 uint32_t r;
967
968 r = RREG32_IO(reg);
969 return r;
970}
971
Kent Russell5b41d942017-08-22 12:31:43 -0400972static ssize_t amdgpu_atombios_get_vbios_version(struct device *dev,
973 struct device_attribute *attr,
974 char *buf)
975{
976 struct drm_device *ddev = dev_get_drvdata(dev);
977 struct amdgpu_device *adev = ddev->dev_private;
978 struct atom_context *ctx = adev->mode_info.atom_context;
979
980 return snprintf(buf, PAGE_SIZE, "%s\n", ctx->vbios_version);
981}
982
983static DEVICE_ATTR(vbios_version, 0444, amdgpu_atombios_get_vbios_version,
984 NULL);
985
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400986/**
987 * amdgpu_atombios_fini - free the driver info and callbacks for atombios
988 *
989 * @adev: amdgpu_device pointer
990 *
991 * Frees the driver info and register access callbacks for the ATOM
992 * interpreter (r4xx+).
993 * Called at driver shutdown.
994 */
995static void amdgpu_atombios_fini(struct amdgpu_device *adev)
996{
Monk Liu89e0ec9f2016-05-27 19:34:11 +0800997 if (adev->mode_info.atom_context) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400998 kfree(adev->mode_info.atom_context->scratch);
Monk Liu89e0ec9f2016-05-27 19:34:11 +0800999 kfree(adev->mode_info.atom_context->iio);
1000 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001001 kfree(adev->mode_info.atom_context);
1002 adev->mode_info.atom_context = NULL;
1003 kfree(adev->mode_info.atom_card_info);
1004 adev->mode_info.atom_card_info = NULL;
Kent Russell5b41d942017-08-22 12:31:43 -04001005 device_remove_file(adev->dev, &dev_attr_vbios_version);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001006}
1007
1008/**
1009 * amdgpu_atombios_init - init the driver info and callbacks for atombios
1010 *
1011 * @adev: amdgpu_device pointer
1012 *
1013 * Initializes the driver info and register access callbacks for the
1014 * ATOM interpreter (r4xx+).
1015 * Returns 0 on sucess, -ENOMEM on failure.
1016 * Called at driver startup.
1017 */
1018static int amdgpu_atombios_init(struct amdgpu_device *adev)
1019{
1020 struct card_info *atom_card_info =
1021 kzalloc(sizeof(struct card_info), GFP_KERNEL);
Kent Russell5b41d942017-08-22 12:31:43 -04001022 int ret;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001023
1024 if (!atom_card_info)
1025 return -ENOMEM;
1026
1027 adev->mode_info.atom_card_info = atom_card_info;
1028 atom_card_info->dev = adev->ddev;
1029 atom_card_info->reg_read = cail_reg_read;
1030 atom_card_info->reg_write = cail_reg_write;
1031 /* needed for iio ops */
1032 if (adev->rio_mem) {
1033 atom_card_info->ioreg_read = cail_ioreg_read;
1034 atom_card_info->ioreg_write = cail_ioreg_write;
1035 } else {
Amber Linb64a18c2017-01-04 08:06:58 -05001036 DRM_INFO("PCI I/O BAR is not found. Using MMIO to access ATOM BIOS\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001037 atom_card_info->ioreg_read = cail_reg_read;
1038 atom_card_info->ioreg_write = cail_reg_write;
1039 }
1040 atom_card_info->mc_read = cail_mc_read;
1041 atom_card_info->mc_write = cail_mc_write;
1042 atom_card_info->pll_read = cail_pll_read;
1043 atom_card_info->pll_write = cail_pll_write;
1044
1045 adev->mode_info.atom_context = amdgpu_atom_parse(atom_card_info, adev->bios);
1046 if (!adev->mode_info.atom_context) {
1047 amdgpu_atombios_fini(adev);
1048 return -ENOMEM;
1049 }
1050
1051 mutex_init(&adev->mode_info.atom_context->mutex);
Alex Deuchera5bde2f2016-09-23 16:23:41 -04001052 if (adev->is_atom_fw) {
1053 amdgpu_atomfirmware_scratch_regs_init(adev);
1054 amdgpu_atomfirmware_allocate_fb_scratch(adev);
1055 } else {
1056 amdgpu_atombios_scratch_regs_init(adev);
1057 amdgpu_atombios_allocate_fb_scratch(adev);
1058 }
Kent Russell5b41d942017-08-22 12:31:43 -04001059
1060 ret = device_create_file(adev->dev, &dev_attr_vbios_version);
1061 if (ret) {
1062 DRM_ERROR("Failed to create device file for VBIOS version\n");
1063 return ret;
1064 }
1065
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001066 return 0;
1067}
1068
1069/* if we get transitioned to only one device, take VGA back */
1070/**
1071 * amdgpu_vga_set_decode - enable/disable vga decode
1072 *
1073 * @cookie: amdgpu_device pointer
1074 * @state: enable/disable vga decode
1075 *
1076 * Enable/disable vga decode (all asics).
1077 * Returns VGA resource flags.
1078 */
1079static unsigned int amdgpu_vga_set_decode(void *cookie, bool state)
1080{
1081 struct amdgpu_device *adev = cookie;
1082 amdgpu_asic_set_vga_state(adev, state);
1083 if (state)
1084 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
1085 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1086 else
1087 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1088}
1089
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001090static void amdgpu_check_block_size(struct amdgpu_device *adev)
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001091{
1092 /* defines number of bits in page table versus page directory,
1093 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
1094 * page table and the remaining bits are in the page directory */
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001095 if (amdgpu_vm_block_size == -1)
1096 return;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001097
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001098 if (amdgpu_vm_block_size < 9) {
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001099 dev_warn(adev->dev, "VM page table size (%d) too small\n",
1100 amdgpu_vm_block_size);
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001101 goto def_value;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001102 }
1103
1104 if (amdgpu_vm_block_size > 24 ||
1105 (amdgpu_vm_size * 1024) < (1ull << amdgpu_vm_block_size)) {
1106 dev_warn(adev->dev, "VM page table size (%d) too large\n",
1107 amdgpu_vm_block_size);
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001108 goto def_value;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001109 }
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001110
1111 return;
1112
1113def_value:
1114 amdgpu_vm_block_size = -1;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001115}
1116
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001117static void amdgpu_check_vm_size(struct amdgpu_device *adev)
1118{
Alex Deucher64dab072017-06-15 18:20:09 -04001119 /* no need to check the default value */
1120 if (amdgpu_vm_size == -1)
1121 return;
1122
Alex Deucher76117502017-06-21 12:31:41 -04001123 if (!is_power_of_2(amdgpu_vm_size)) {
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001124 dev_warn(adev->dev, "VM size (%d) must be a power of 2\n",
1125 amdgpu_vm_size);
1126 goto def_value;
1127 }
1128
1129 if (amdgpu_vm_size < 1) {
1130 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
1131 amdgpu_vm_size);
1132 goto def_value;
1133 }
1134
1135 /*
1136 * Max GPUVM size for Cayman, SI, CI VI are 40 bits.
1137 */
1138 if (amdgpu_vm_size > 1024) {
1139 dev_warn(adev->dev, "VM size (%d) too large, max is 1TB\n",
1140 amdgpu_vm_size);
1141 goto def_value;
1142 }
1143
1144 return;
1145
1146def_value:
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001147 amdgpu_vm_size = -1;
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001148}
1149
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001150/**
1151 * amdgpu_check_arguments - validate module params
1152 *
1153 * @adev: amdgpu_device pointer
1154 *
1155 * Validates certain module parameters and updates
1156 * the associated values used by the driver (all asics).
1157 */
1158static void amdgpu_check_arguments(struct amdgpu_device *adev)
1159{
Chunming Zhou5b011232015-12-10 17:34:33 +08001160 if (amdgpu_sched_jobs < 4) {
1161 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
1162 amdgpu_sched_jobs);
1163 amdgpu_sched_jobs = 4;
Alex Deucher76117502017-06-21 12:31:41 -04001164 } else if (!is_power_of_2(amdgpu_sched_jobs)){
Chunming Zhou5b011232015-12-10 17:34:33 +08001165 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
1166 amdgpu_sched_jobs);
1167 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
1168 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001169
Alex Deucher83e74db2017-08-21 11:58:25 -04001170 if (amdgpu_gart_size != -1 && amdgpu_gart_size < 32) {
Christian Königf9321cc2017-07-07 13:44:05 +02001171 /* gart size must be greater or equal to 32M */
1172 dev_warn(adev->dev, "gart size (%d) too small\n",
1173 amdgpu_gart_size);
Alex Deucher83e74db2017-08-21 11:58:25 -04001174 amdgpu_gart_size = -1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001175 }
1176
Christian König36d38372017-07-07 13:17:45 +02001177 if (amdgpu_gtt_size != -1 && amdgpu_gtt_size < 32) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001178 /* gtt size must be greater or equal to 32M */
Christian König36d38372017-07-07 13:17:45 +02001179 dev_warn(adev->dev, "gtt size (%d) too small\n",
1180 amdgpu_gtt_size);
1181 amdgpu_gtt_size = -1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001182 }
1183
Roger Hed07f14b2017-08-15 16:05:59 +08001184 /* valid range is between 4 and 9 inclusive */
1185 if (amdgpu_vm_fragment_size != -1 &&
1186 (amdgpu_vm_fragment_size > 9 || amdgpu_vm_fragment_size < 4)) {
1187 dev_warn(adev->dev, "valid range is between 4 and 9\n");
1188 amdgpu_vm_fragment_size = -1;
1189 }
1190
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001191 amdgpu_check_vm_size(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001192
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001193 amdgpu_check_block_size(adev);
Christian König6a7f76e2016-08-24 15:51:49 +02001194
jimqu526bae32016-11-07 09:53:10 +08001195 if (amdgpu_vram_page_split != -1 && (amdgpu_vram_page_split < 16 ||
Alex Deucher76117502017-06-21 12:31:41 -04001196 !is_power_of_2(amdgpu_vram_page_split))) {
Christian König6a7f76e2016-08-24 15:51:49 +02001197 dev_warn(adev->dev, "invalid VRAM page split (%d)\n",
1198 amdgpu_vram_page_split);
1199 amdgpu_vram_page_split = 1024;
1200 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001201}
1202
1203/**
1204 * amdgpu_switcheroo_set_state - set switcheroo state
1205 *
1206 * @pdev: pci dev pointer
Lukas Wunner16944672015-09-05 11:17:35 +02001207 * @state: vga_switcheroo state
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001208 *
1209 * Callback for the switcheroo driver. Suspends or resumes the
1210 * the asics before or after it is powered up using ACPI methods.
1211 */
1212static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1213{
1214 struct drm_device *dev = pci_get_drvdata(pdev);
1215
1216 if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
1217 return;
1218
1219 if (state == VGA_SWITCHEROO_ON) {
Joe Perches7ca85292017-02-28 04:55:52 -08001220 pr_info("amdgpu: switched on\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001221 /* don't suspend or resume card normally */
1222 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1223
Alex Deucher810ddc32016-08-23 13:25:49 -04001224 amdgpu_device_resume(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001225
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001226 dev->switch_power_state = DRM_SWITCH_POWER_ON;
1227 drm_kms_helper_poll_enable(dev);
1228 } else {
Joe Perches7ca85292017-02-28 04:55:52 -08001229 pr_info("amdgpu: switched off\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001230 drm_kms_helper_poll_disable(dev);
1231 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Alex Deucher810ddc32016-08-23 13:25:49 -04001232 amdgpu_device_suspend(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001233 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1234 }
1235}
1236
1237/**
1238 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1239 *
1240 * @pdev: pci dev pointer
1241 *
1242 * Callback for the switcheroo driver. Check of the switcheroo
1243 * state can be changed.
1244 * Returns true if the state can be changed, false if not.
1245 */
1246static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1247{
1248 struct drm_device *dev = pci_get_drvdata(pdev);
1249
1250 /*
1251 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1252 * locking inversion with the driver load path. And the access here is
1253 * completely racy anyway. So don't bother with locking for now.
1254 */
1255 return dev->open_count == 0;
1256}
1257
1258static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1259 .set_gpu_state = amdgpu_switcheroo_set_state,
1260 .reprobe = NULL,
1261 .can_switch = amdgpu_switcheroo_can_switch,
1262};
1263
1264int amdgpu_set_clockgating_state(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001265 enum amd_ip_block_type block_type,
1266 enum amd_clockgating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001267{
1268 int i, r = 0;
1269
1270 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001271 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001272 continue;
Rex Zhuc7228652017-02-22 15:33:46 +08001273 if (adev->ip_blocks[i].version->type != block_type)
1274 continue;
1275 if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
1276 continue;
1277 r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
1278 (void *)adev, state);
1279 if (r)
1280 DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
1281 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001282 }
1283 return r;
1284}
1285
1286int amdgpu_set_powergating_state(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001287 enum amd_ip_block_type block_type,
1288 enum amd_powergating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001289{
1290 int i, r = 0;
1291
1292 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001293 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001294 continue;
Rex Zhuc7228652017-02-22 15:33:46 +08001295 if (adev->ip_blocks[i].version->type != block_type)
1296 continue;
1297 if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
1298 continue;
1299 r = adev->ip_blocks[i].version->funcs->set_powergating_state(
1300 (void *)adev, state);
1301 if (r)
1302 DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
1303 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001304 }
1305 return r;
1306}
1307
Huang Rui6cb2d4e2017-01-05 18:44:41 +08001308void amdgpu_get_clockgating_state(struct amdgpu_device *adev, u32 *flags)
1309{
1310 int i;
1311
1312 for (i = 0; i < adev->num_ip_blocks; i++) {
1313 if (!adev->ip_blocks[i].status.valid)
1314 continue;
1315 if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
1316 adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
1317 }
1318}
1319
Alex Deucher5dbbb602016-06-23 11:41:04 -04001320int amdgpu_wait_for_idle(struct amdgpu_device *adev,
1321 enum amd_ip_block_type block_type)
1322{
1323 int i, r;
1324
1325 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001326 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001327 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001328 if (adev->ip_blocks[i].version->type == block_type) {
1329 r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -04001330 if (r)
1331 return r;
1332 break;
1333 }
1334 }
1335 return 0;
1336
1337}
1338
1339bool amdgpu_is_idle(struct amdgpu_device *adev,
1340 enum amd_ip_block_type block_type)
1341{
1342 int i;
1343
1344 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001345 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001346 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001347 if (adev->ip_blocks[i].version->type == block_type)
1348 return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -04001349 }
1350 return true;
1351
1352}
1353
Alex Deuchera1255102016-10-13 17:41:13 -04001354struct amdgpu_ip_block * amdgpu_get_ip_block(struct amdgpu_device *adev,
1355 enum amd_ip_block_type type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001356{
1357 int i;
1358
1359 for (i = 0; i < adev->num_ip_blocks; i++)
Alex Deuchera1255102016-10-13 17:41:13 -04001360 if (adev->ip_blocks[i].version->type == type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001361 return &adev->ip_blocks[i];
1362
1363 return NULL;
1364}
1365
1366/**
1367 * amdgpu_ip_block_version_cmp
1368 *
1369 * @adev: amdgpu_device pointer
yanyang15fc3aee2015-05-22 14:39:35 -04001370 * @type: enum amd_ip_block_type
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001371 * @major: major version
1372 * @minor: minor version
1373 *
1374 * return 0 if equal or greater
1375 * return 1 if smaller or the ip_block doesn't exist
1376 */
1377int amdgpu_ip_block_version_cmp(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001378 enum amd_ip_block_type type,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001379 u32 major, u32 minor)
1380{
Alex Deuchera1255102016-10-13 17:41:13 -04001381 struct amdgpu_ip_block *ip_block = amdgpu_get_ip_block(adev, type);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001382
Alex Deuchera1255102016-10-13 17:41:13 -04001383 if (ip_block && ((ip_block->version->major > major) ||
1384 ((ip_block->version->major == major) &&
1385 (ip_block->version->minor >= minor))))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001386 return 0;
1387
1388 return 1;
1389}
1390
Alex Deuchera1255102016-10-13 17:41:13 -04001391/**
1392 * amdgpu_ip_block_add
1393 *
1394 * @adev: amdgpu_device pointer
1395 * @ip_block_version: pointer to the IP to add
1396 *
1397 * Adds the IP block driver information to the collection of IPs
1398 * on the asic.
1399 */
1400int amdgpu_ip_block_add(struct amdgpu_device *adev,
1401 const struct amdgpu_ip_block_version *ip_block_version)
1402{
1403 if (!ip_block_version)
1404 return -EINVAL;
1405
Huang Ruia0bae352017-05-03 09:52:06 +08001406 DRM_DEBUG("add ip block number %d <%s>\n", adev->num_ip_blocks,
1407 ip_block_version->funcs->name);
1408
Alex Deuchera1255102016-10-13 17:41:13 -04001409 adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
1410
1411 return 0;
1412}
1413
Alex Deucher483ef982016-09-30 12:43:04 -04001414static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
Emily Deng9accf2f2016-08-10 16:01:25 +08001415{
1416 adev->enable_virtual_display = false;
1417
1418 if (amdgpu_virtual_display) {
1419 struct drm_device *ddev = adev->ddev;
1420 const char *pci_address_name = pci_name(ddev->pdev);
Emily Deng0f663562016-09-30 13:02:18 -04001421 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
Emily Deng9accf2f2016-08-10 16:01:25 +08001422
1423 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1424 pciaddstr_tmp = pciaddstr;
Emily Deng0f663562016-09-30 13:02:18 -04001425 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1426 pciaddname = strsep(&pciaddname_tmp, ",");
Yintian Tao967de2a2017-01-22 15:16:51 +08001427 if (!strcmp("all", pciaddname)
1428 || !strcmp(pci_address_name, pciaddname)) {
Emily Deng0f663562016-09-30 13:02:18 -04001429 long num_crtc;
1430 int res = -1;
1431
Emily Deng9accf2f2016-08-10 16:01:25 +08001432 adev->enable_virtual_display = true;
Emily Deng0f663562016-09-30 13:02:18 -04001433
1434 if (pciaddname_tmp)
1435 res = kstrtol(pciaddname_tmp, 10,
1436 &num_crtc);
1437
1438 if (!res) {
1439 if (num_crtc < 1)
1440 num_crtc = 1;
1441 if (num_crtc > 6)
1442 num_crtc = 6;
1443 adev->mode_info.num_crtc = num_crtc;
1444 } else {
1445 adev->mode_info.num_crtc = 1;
1446 }
Emily Deng9accf2f2016-08-10 16:01:25 +08001447 break;
1448 }
1449 }
1450
Emily Deng0f663562016-09-30 13:02:18 -04001451 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
1452 amdgpu_virtual_display, pci_address_name,
1453 adev->enable_virtual_display, adev->mode_info.num_crtc);
Emily Deng9accf2f2016-08-10 16:01:25 +08001454
1455 kfree(pciaddstr);
1456 }
1457}
1458
Alex Deuchere2a75f82017-04-27 16:58:01 -04001459static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev)
1460{
Alex Deuchere2a75f82017-04-27 16:58:01 -04001461 const char *chip_name;
1462 char fw_name[30];
1463 int err;
1464 const struct gpu_info_firmware_header_v1_0 *hdr;
1465
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001466 adev->firmware.gpu_info_fw = NULL;
1467
Alex Deuchere2a75f82017-04-27 16:58:01 -04001468 switch (adev->asic_type) {
1469 case CHIP_TOPAZ:
1470 case CHIP_TONGA:
1471 case CHIP_FIJI:
1472 case CHIP_POLARIS11:
1473 case CHIP_POLARIS10:
1474 case CHIP_POLARIS12:
1475 case CHIP_CARRIZO:
1476 case CHIP_STONEY:
1477#ifdef CONFIG_DRM_AMDGPU_SI
1478 case CHIP_VERDE:
1479 case CHIP_TAHITI:
1480 case CHIP_PITCAIRN:
1481 case CHIP_OLAND:
1482 case CHIP_HAINAN:
1483#endif
1484#ifdef CONFIG_DRM_AMDGPU_CIK
1485 case CHIP_BONAIRE:
1486 case CHIP_HAWAII:
1487 case CHIP_KAVERI:
1488 case CHIP_KABINI:
1489 case CHIP_MULLINS:
1490#endif
1491 default:
1492 return 0;
1493 case CHIP_VEGA10:
1494 chip_name = "vega10";
1495 break;
Alex Deucher2d2e5e72017-05-09 12:27:35 -04001496 case CHIP_RAVEN:
1497 chip_name = "raven";
1498 break;
Alex Deuchere2a75f82017-04-27 16:58:01 -04001499 }
1500
1501 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name);
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001502 err = request_firmware(&adev->firmware.gpu_info_fw, fw_name, adev->dev);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001503 if (err) {
1504 dev_err(adev->dev,
1505 "Failed to load gpu_info firmware \"%s\"\n",
1506 fw_name);
1507 goto out;
1508 }
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001509 err = amdgpu_ucode_validate(adev->firmware.gpu_info_fw);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001510 if (err) {
1511 dev_err(adev->dev,
1512 "Failed to validate gpu_info firmware \"%s\"\n",
1513 fw_name);
1514 goto out;
1515 }
1516
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001517 hdr = (const struct gpu_info_firmware_header_v1_0 *)adev->firmware.gpu_info_fw->data;
Alex Deuchere2a75f82017-04-27 16:58:01 -04001518 amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
1519
1520 switch (hdr->version_major) {
1521 case 1:
1522 {
1523 const struct gpu_info_firmware_v1_0 *gpu_info_fw =
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001524 (const struct gpu_info_firmware_v1_0 *)(adev->firmware.gpu_info_fw->data +
Alex Deuchere2a75f82017-04-27 16:58:01 -04001525 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1526
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001527 adev->gfx.config.max_shader_engines = le32_to_cpu(gpu_info_fw->gc_num_se);
1528 adev->gfx.config.max_cu_per_sh = le32_to_cpu(gpu_info_fw->gc_num_cu_per_sh);
1529 adev->gfx.config.max_sh_per_se = le32_to_cpu(gpu_info_fw->gc_num_sh_per_se);
1530 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 -04001531 adev->gfx.config.max_texture_channel_caches =
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001532 le32_to_cpu(gpu_info_fw->gc_num_tccs);
1533 adev->gfx.config.max_gprs = le32_to_cpu(gpu_info_fw->gc_num_gprs);
1534 adev->gfx.config.max_gs_threads = le32_to_cpu(gpu_info_fw->gc_num_max_gs_thds);
1535 adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gpu_info_fw->gc_gs_table_depth);
1536 adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gpu_info_fw->gc_gsprim_buff_depth);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001537 adev->gfx.config.double_offchip_lds_buf =
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001538 le32_to_cpu(gpu_info_fw->gc_double_offchip_lds_buffer);
1539 adev->gfx.cu_info.wave_front_size = le32_to_cpu(gpu_info_fw->gc_wave_size);
Hawking Zhang51fd0372017-06-09 22:30:52 +08001540 adev->gfx.cu_info.max_waves_per_simd =
1541 le32_to_cpu(gpu_info_fw->gc_max_waves_per_simd);
1542 adev->gfx.cu_info.max_scratch_slots_per_cu =
1543 le32_to_cpu(gpu_info_fw->gc_max_scratch_slots_per_cu);
1544 adev->gfx.cu_info.lds_size = le32_to_cpu(gpu_info_fw->gc_lds_size);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001545 break;
1546 }
1547 default:
1548 dev_err(adev->dev,
1549 "Unsupported gpu_info table %d\n", hdr->header.ucode_version);
1550 err = -EINVAL;
1551 goto out;
1552 }
1553out:
Alex Deuchere2a75f82017-04-27 16:58:01 -04001554 return err;
1555}
1556
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001557static int amdgpu_early_init(struct amdgpu_device *adev)
1558{
Alex Deucheraaa36a92015-04-20 17:31:14 -04001559 int i, r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001560
Alex Deucher483ef982016-09-30 12:43:04 -04001561 amdgpu_device_enable_virtual_display(adev);
Emily Denga6be7572016-08-08 11:37:50 +08001562
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001563 switch (adev->asic_type) {
Alex Deucheraaa36a92015-04-20 17:31:14 -04001564 case CHIP_TOPAZ:
1565 case CHIP_TONGA:
David Zhang48299f92015-07-08 01:05:16 +08001566 case CHIP_FIJI:
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001567 case CHIP_POLARIS11:
1568 case CHIP_POLARIS10:
Junwei Zhangc4642a42016-12-14 15:32:28 -05001569 case CHIP_POLARIS12:
Alex Deucheraaa36a92015-04-20 17:31:14 -04001570 case CHIP_CARRIZO:
Samuel Li39bb0c92015-10-08 16:31:43 -04001571 case CHIP_STONEY:
1572 if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
Alex Deucheraaa36a92015-04-20 17:31:14 -04001573 adev->family = AMDGPU_FAMILY_CZ;
1574 else
1575 adev->family = AMDGPU_FAMILY_VI;
1576
1577 r = vi_set_ip_blocks(adev);
1578 if (r)
1579 return r;
1580 break;
Ken Wang33f34802016-01-21 17:29:41 +08001581#ifdef CONFIG_DRM_AMDGPU_SI
1582 case CHIP_VERDE:
1583 case CHIP_TAHITI:
1584 case CHIP_PITCAIRN:
1585 case CHIP_OLAND:
1586 case CHIP_HAINAN:
Ken Wang295d0da2016-05-24 21:02:53 +08001587 adev->family = AMDGPU_FAMILY_SI;
Ken Wang33f34802016-01-21 17:29:41 +08001588 r = si_set_ip_blocks(adev);
1589 if (r)
1590 return r;
1591 break;
1592#endif
Alex Deuchera2e73f52015-04-20 17:09:27 -04001593#ifdef CONFIG_DRM_AMDGPU_CIK
1594 case CHIP_BONAIRE:
1595 case CHIP_HAWAII:
1596 case CHIP_KAVERI:
1597 case CHIP_KABINI:
1598 case CHIP_MULLINS:
1599 if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
1600 adev->family = AMDGPU_FAMILY_CI;
1601 else
1602 adev->family = AMDGPU_FAMILY_KV;
1603
1604 r = cik_set_ip_blocks(adev);
1605 if (r)
1606 return r;
1607 break;
1608#endif
Chunming Zhou2ca8a5d2016-12-07 17:31:19 +08001609 case CHIP_VEGA10:
1610 case CHIP_RAVEN:
1611 if (adev->asic_type == CHIP_RAVEN)
1612 adev->family = AMDGPU_FAMILY_RV;
1613 else
1614 adev->family = AMDGPU_FAMILY_AI;
Ken Wang460826e2017-03-06 14:53:16 -05001615
1616 r = soc15_set_ip_blocks(adev);
1617 if (r)
1618 return r;
1619 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001620 default:
1621 /* FIXME: not supported yet */
1622 return -EINVAL;
1623 }
1624
Alex Deuchere2a75f82017-04-27 16:58:01 -04001625 r = amdgpu_device_parse_gpu_info_fw(adev);
1626 if (r)
1627 return r;
1628
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001629 if (amdgpu_sriov_vf(adev)) {
1630 r = amdgpu_virt_request_full_gpu(adev, true);
1631 if (r)
1632 return r;
1633 }
1634
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001635 for (i = 0; i < adev->num_ip_blocks; i++) {
1636 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
Huang Ruied8cf002017-05-03 09:40:17 +08001637 DRM_ERROR("disabled ip block: %d <%s>\n",
1638 i, adev->ip_blocks[i].version->funcs->name);
Alex Deuchera1255102016-10-13 17:41:13 -04001639 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001640 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001641 if (adev->ip_blocks[i].version->funcs->early_init) {
1642 r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001643 if (r == -ENOENT) {
Alex Deuchera1255102016-10-13 17:41:13 -04001644 adev->ip_blocks[i].status.valid = false;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001645 } else if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001646 DRM_ERROR("early_init of IP block <%s> failed %d\n",
1647 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001648 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001649 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001650 adev->ip_blocks[i].status.valid = true;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001651 }
Alex Deucher974e6b62015-07-10 13:59:44 -04001652 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001653 adev->ip_blocks[i].status.valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001654 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001655 }
1656 }
1657
Nicolai Hähnle395d1fb2016-06-02 12:32:07 +02001658 adev->cg_flags &= amdgpu_cg_mask;
1659 adev->pg_flags &= amdgpu_pg_mask;
1660
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001661 return 0;
1662}
1663
1664static int amdgpu_init(struct amdgpu_device *adev)
1665{
1666 int i, r;
1667
1668 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001669 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001670 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001671 r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001672 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001673 DRM_ERROR("sw_init of IP block <%s> failed %d\n",
1674 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001675 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001676 }
Alex Deuchera1255102016-10-13 17:41:13 -04001677 adev->ip_blocks[i].status.sw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001678 /* need to do gmc hw init early so we can allocate gpu mem */
Alex Deuchera1255102016-10-13 17:41:13 -04001679 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001680 r = amdgpu_vram_scratch_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001681 if (r) {
1682 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001683 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001684 }
Alex Deuchera1255102016-10-13 17:41:13 -04001685 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001686 if (r) {
1687 DRM_ERROR("hw_init %d failed %d\n", i, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001688 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001689 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001690 r = amdgpu_wb_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001691 if (r) {
1692 DRM_ERROR("amdgpu_wb_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001693 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001694 }
Alex Deuchera1255102016-10-13 17:41:13 -04001695 adev->ip_blocks[i].status.hw = true;
Monk Liu24936642017-01-09 15:54:32 +08001696
1697 /* right after GMC hw init, we create CSA */
1698 if (amdgpu_sriov_vf(adev)) {
1699 r = amdgpu_allocate_static_csa(adev);
1700 if (r) {
1701 DRM_ERROR("allocate CSA failed %d\n", r);
1702 return r;
1703 }
1704 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001705 }
1706 }
1707
1708 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001709 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001710 continue;
1711 /* gmc hw init is done early */
Alex Deuchera1255102016-10-13 17:41:13 -04001712 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001713 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001714 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001715 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001716 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
1717 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001718 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001719 }
Alex Deuchera1255102016-10-13 17:41:13 -04001720 adev->ip_blocks[i].status.hw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001721 }
1722
1723 return 0;
1724}
1725
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08001726static void amdgpu_fill_reset_magic(struct amdgpu_device *adev)
1727{
1728 memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM);
1729}
1730
1731static bool amdgpu_check_vram_lost(struct amdgpu_device *adev)
1732{
1733 return !!memcmp(adev->gart.ptr, adev->reset_magic,
1734 AMDGPU_RESET_MAGIC_NUM);
1735}
1736
Shirish S2dc80b02017-05-25 10:05:25 +05301737static int amdgpu_late_set_cg_state(struct amdgpu_device *adev)
1738{
1739 int i = 0, r;
1740
1741 for (i = 0; i < adev->num_ip_blocks; i++) {
1742 if (!adev->ip_blocks[i].status.valid)
1743 continue;
1744 /* skip CG for VCE/UVD, it's handled specially */
1745 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1746 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1747 /* enable clockgating to save power */
1748 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1749 AMD_CG_STATE_GATE);
1750 if (r) {
1751 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
1752 adev->ip_blocks[i].version->funcs->name, r);
1753 return r;
1754 }
1755 }
1756 }
1757 return 0;
1758}
1759
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001760static int amdgpu_late_init(struct amdgpu_device *adev)
1761{
1762 int i = 0, r;
1763
1764 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001765 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001766 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001767 if (adev->ip_blocks[i].version->funcs->late_init) {
1768 r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001769 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001770 DRM_ERROR("late_init of IP block <%s> failed %d\n",
1771 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001772 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001773 }
Alex Deuchera1255102016-10-13 17:41:13 -04001774 adev->ip_blocks[i].status.late_initialized = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001775 }
1776 }
1777
Shirish S2dc80b02017-05-25 10:05:25 +05301778 mod_delayed_work(system_wq, &adev->late_init_work,
1779 msecs_to_jiffies(AMDGPU_RESUME_MS));
1780
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08001781 amdgpu_fill_reset_magic(adev);
1782
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001783 return 0;
1784}
1785
1786static int amdgpu_fini(struct amdgpu_device *adev)
1787{
1788 int i, r;
1789
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001790 /* need to disable SMC first */
1791 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001792 if (!adev->ip_blocks[i].status.hw)
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001793 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001794 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001795 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
Alex Deuchera1255102016-10-13 17:41:13 -04001796 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1797 AMD_CG_STATE_UNGATE);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001798 if (r) {
1799 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001800 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001801 return r;
1802 }
Alex Deuchera1255102016-10-13 17:41:13 -04001803 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001804 /* XXX handle errors */
1805 if (r) {
1806 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001807 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001808 }
Alex Deuchera1255102016-10-13 17:41:13 -04001809 adev->ip_blocks[i].status.hw = false;
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001810 break;
1811 }
1812 }
1813
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001814 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001815 if (!adev->ip_blocks[i].status.hw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001816 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001817 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001818 amdgpu_wb_fini(adev);
1819 amdgpu_vram_scratch_fini(adev);
1820 }
Rex Zhu8201a672016-11-24 21:44:44 +08001821
1822 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1823 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1824 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
1825 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1826 AMD_CG_STATE_UNGATE);
1827 if (r) {
1828 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1829 adev->ip_blocks[i].version->funcs->name, r);
1830 return r;
1831 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001832 }
Rex Zhu8201a672016-11-24 21:44:44 +08001833
Alex Deuchera1255102016-10-13 17:41:13 -04001834 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001835 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001836 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001837 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
1838 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001839 }
Rex Zhu8201a672016-11-24 21:44:44 +08001840
Alex Deuchera1255102016-10-13 17:41:13 -04001841 adev->ip_blocks[i].status.hw = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001842 }
1843
1844 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001845 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001846 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001847 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001848 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001849 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001850 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
1851 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001852 }
Alex Deuchera1255102016-10-13 17:41:13 -04001853 adev->ip_blocks[i].status.sw = false;
1854 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001855 }
1856
Monk Liua6dcfd92016-05-19 14:36:34 +08001857 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001858 if (!adev->ip_blocks[i].status.late_initialized)
Grazvydas Ignotas8a2eef12016-10-03 00:06:44 +03001859 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001860 if (adev->ip_blocks[i].version->funcs->late_fini)
1861 adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
1862 adev->ip_blocks[i].status.late_initialized = false;
Monk Liua6dcfd92016-05-19 14:36:34 +08001863 }
1864
Monk Liu030308f2017-09-15 15:34:52 +08001865 if (amdgpu_sriov_vf(adev))
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001866 amdgpu_virt_release_full_gpu(adev, false);
Monk Liu24936642017-01-09 15:54:32 +08001867
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001868 return 0;
1869}
1870
Shirish S2dc80b02017-05-25 10:05:25 +05301871static void amdgpu_late_init_func_handler(struct work_struct *work)
1872{
1873 struct amdgpu_device *adev =
1874 container_of(work, struct amdgpu_device, late_init_work.work);
1875 amdgpu_late_set_cg_state(adev);
1876}
1877
Alex Deucherfaefba92016-12-06 10:38:29 -05001878int amdgpu_suspend(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001879{
1880 int i, r;
1881
Xiangliang Yue941ea92017-01-18 12:47:55 +08001882 if (amdgpu_sriov_vf(adev))
1883 amdgpu_virt_request_full_gpu(adev, false);
1884
Flora Cuic5a93a22016-02-26 10:45:25 +08001885 /* ungate SMC block first */
1886 r = amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC,
1887 AMD_CG_STATE_UNGATE);
1888 if (r) {
1889 DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n",r);
1890 }
1891
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001892 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001893 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001894 continue;
1895 /* ungate blocks so that suspend can properly shut them down */
Flora Cuic5a93a22016-02-26 10:45:25 +08001896 if (i != AMD_IP_BLOCK_TYPE_SMC) {
Alex Deuchera1255102016-10-13 17:41:13 -04001897 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1898 AMD_CG_STATE_UNGATE);
Flora Cuic5a93a22016-02-26 10:45:25 +08001899 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001900 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1901 adev->ip_blocks[i].version->funcs->name, r);
Flora Cuic5a93a22016-02-26 10:45:25 +08001902 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001903 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001904 /* XXX handle errors */
Alex Deuchera1255102016-10-13 17:41:13 -04001905 r = adev->ip_blocks[i].version->funcs->suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001906 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001907 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001908 DRM_ERROR("suspend of IP block <%s> failed %d\n",
1909 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001910 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001911 }
1912
Xiangliang Yue941ea92017-01-18 12:47:55 +08001913 if (amdgpu_sriov_vf(adev))
1914 amdgpu_virt_release_full_gpu(adev, false);
1915
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001916 return 0;
1917}
1918
Monk Liue4f0fdc2017-02-09 11:55:49 +08001919static int amdgpu_sriov_reinit_early(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08001920{
1921 int i, r;
1922
Monk Liu2cb681b2017-04-26 12:00:49 +08001923 static enum amd_ip_block_type ip_order[] = {
1924 AMD_IP_BLOCK_TYPE_GMC,
1925 AMD_IP_BLOCK_TYPE_COMMON,
Monk Liu2cb681b2017-04-26 12:00:49 +08001926 AMD_IP_BLOCK_TYPE_IH,
1927 };
Monk Liua90ad3c2017-01-23 14:22:08 +08001928
Monk Liu2cb681b2017-04-26 12:00:49 +08001929 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1930 int j;
1931 struct amdgpu_ip_block *block;
Monk Liua90ad3c2017-01-23 14:22:08 +08001932
Monk Liu2cb681b2017-04-26 12:00:49 +08001933 for (j = 0; j < adev->num_ip_blocks; j++) {
1934 block = &adev->ip_blocks[j];
1935
1936 if (block->version->type != ip_order[i] ||
1937 !block->status.valid)
1938 continue;
1939
1940 r = block->version->funcs->hw_init(adev);
1941 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
Monk Liua90ad3c2017-01-23 14:22:08 +08001942 }
1943 }
1944
1945 return 0;
1946}
1947
Monk Liue4f0fdc2017-02-09 11:55:49 +08001948static int amdgpu_sriov_reinit_late(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08001949{
1950 int i, r;
1951
Monk Liu2cb681b2017-04-26 12:00:49 +08001952 static enum amd_ip_block_type ip_order[] = {
1953 AMD_IP_BLOCK_TYPE_SMC,
1954 AMD_IP_BLOCK_TYPE_DCE,
1955 AMD_IP_BLOCK_TYPE_GFX,
1956 AMD_IP_BLOCK_TYPE_SDMA,
Frank Min257deb82017-06-15 20:07:36 +08001957 AMD_IP_BLOCK_TYPE_UVD,
1958 AMD_IP_BLOCK_TYPE_VCE
Monk Liu2cb681b2017-04-26 12:00:49 +08001959 };
Monk Liua90ad3c2017-01-23 14:22:08 +08001960
Monk Liu2cb681b2017-04-26 12:00:49 +08001961 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1962 int j;
1963 struct amdgpu_ip_block *block;
Monk Liua90ad3c2017-01-23 14:22:08 +08001964
Monk Liu2cb681b2017-04-26 12:00:49 +08001965 for (j = 0; j < adev->num_ip_blocks; j++) {
1966 block = &adev->ip_blocks[j];
1967
1968 if (block->version->type != ip_order[i] ||
1969 !block->status.valid)
1970 continue;
1971
1972 r = block->version->funcs->hw_init(adev);
1973 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
Monk Liua90ad3c2017-01-23 14:22:08 +08001974 }
1975 }
1976
1977 return 0;
1978}
1979
Chunming Zhoufcf06492017-05-05 10:33:33 +08001980static int amdgpu_resume_phase1(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001981{
1982 int i, r;
1983
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001984 for (i = 0; i < adev->num_ip_blocks; i++) {
1985 if (!adev->ip_blocks[i].status.valid)
1986 continue;
Chunming Zhoufcf06492017-05-05 10:33:33 +08001987 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
1988 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
1989 adev->ip_blocks[i].version->type ==
1990 AMD_IP_BLOCK_TYPE_IH) {
1991 r = adev->ip_blocks[i].version->funcs->resume(adev);
1992 if (r) {
1993 DRM_ERROR("resume of IP block <%s> failed %d\n",
1994 adev->ip_blocks[i].version->funcs->name, r);
1995 return r;
1996 }
1997 }
1998 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001999
Chunming Zhoufcf06492017-05-05 10:33:33 +08002000 return 0;
2001}
2002
2003static int amdgpu_resume_phase2(struct amdgpu_device *adev)
2004{
2005 int i, r;
2006
2007 for (i = 0; i < adev->num_ip_blocks; i++) {
2008 if (!adev->ip_blocks[i].status.valid)
2009 continue;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002010 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
2011 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
2012 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH )
2013 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002014 r = adev->ip_blocks[i].version->funcs->resume(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002015 if (r) {
2016 DRM_ERROR("resume of IP block <%s> failed %d\n",
2017 adev->ip_blocks[i].version->funcs->name, r);
2018 return r;
2019 }
2020 }
2021
2022 return 0;
2023}
2024
2025static int amdgpu_resume(struct amdgpu_device *adev)
2026{
Chunming Zhoufcf06492017-05-05 10:33:33 +08002027 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002028
Chunming Zhoufcf06492017-05-05 10:33:33 +08002029 r = amdgpu_resume_phase1(adev);
2030 if (r)
2031 return r;
2032 r = amdgpu_resume_phase2(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002033
Chunming Zhoufcf06492017-05-05 10:33:33 +08002034 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002035}
2036
Monk Liu4e99a442016-03-31 13:26:59 +08002037static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
Andres Rodriguez048765a2016-06-11 02:51:32 -04002038{
Alex Deuchera5bde2f2016-09-23 16:23:41 -04002039 if (adev->is_atom_fw) {
2040 if (amdgpu_atomfirmware_gpu_supports_virtualization(adev))
2041 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
2042 } else {
2043 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
2044 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
2045 }
Andres Rodriguez048765a2016-06-11 02:51:32 -04002046}
2047
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002048/**
2049 * amdgpu_device_init - initialize the driver
2050 *
2051 * @adev: amdgpu_device pointer
2052 * @pdev: drm dev pointer
2053 * @pdev: pci dev pointer
2054 * @flags: driver flags
2055 *
2056 * Initializes the driver info and hw (all asics).
2057 * Returns 0 for success or an error on failure.
2058 * Called at driver startup.
2059 */
2060int amdgpu_device_init(struct amdgpu_device *adev,
2061 struct drm_device *ddev,
2062 struct pci_dev *pdev,
2063 uint32_t flags)
2064{
2065 int r, i;
2066 bool runtime = false;
Marek Olšák95844d22016-08-17 23:49:27 +02002067 u32 max_MBps;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002068
2069 adev->shutdown = false;
2070 adev->dev = &pdev->dev;
2071 adev->ddev = ddev;
2072 adev->pdev = pdev;
2073 adev->flags = flags;
Jammy Zhou2f7d10b2015-07-22 11:29:01 +08002074 adev->asic_type = flags & AMD_ASIC_MASK;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002075 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
Christian König6f02a692017-07-07 11:56:59 +02002076 adev->mc.gart_size = 512 * 1024 * 1024;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002077 adev->accel_working = false;
2078 adev->num_rings = 0;
2079 adev->mman.buffer_funcs = NULL;
2080 adev->mman.buffer_funcs_ring = NULL;
2081 adev->vm_manager.vm_pte_funcs = NULL;
Christian König2d55e452016-02-08 17:37:38 +01002082 adev->vm_manager.vm_pte_num_rings = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002083 adev->gart.gart_funcs = NULL;
Chris Wilsonf54d1862016-10-25 13:00:45 +01002084 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
Andres Rodriguezb8866c22017-04-28 20:05:51 -04002085 bitmap_zero(adev->gfx.pipe_reserve_bitmap, AMDGPU_MAX_COMPUTE_QUEUES);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002086
2087 adev->smc_rreg = &amdgpu_invalid_rreg;
2088 adev->smc_wreg = &amdgpu_invalid_wreg;
2089 adev->pcie_rreg = &amdgpu_invalid_rreg;
2090 adev->pcie_wreg = &amdgpu_invalid_wreg;
Huang Rui36b9a952016-08-31 13:23:25 +08002091 adev->pciep_rreg = &amdgpu_invalid_rreg;
2092 adev->pciep_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002093 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
2094 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
2095 adev->didt_rreg = &amdgpu_invalid_rreg;
2096 adev->didt_wreg = &amdgpu_invalid_wreg;
Rex Zhuccdbb202016-06-08 12:47:41 +08002097 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
2098 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002099 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
2100 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
2101
Rex Zhuccdbb202016-06-08 12:47:41 +08002102
Alex Deucher3e39ab92015-06-05 15:04:33 -04002103 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
2104 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
2105 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002106
2107 /* mutex initialization are all done here so we
2108 * can recall function without having locking issues */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002109 atomic_set(&adev->irq.ih.lock, 0);
Huang Rui0e5ca0d2017-03-03 18:37:23 -05002110 mutex_init(&adev->firmware.mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002111 mutex_init(&adev->pm.mutex);
2112 mutex_init(&adev->gfx.gpu_clock_mutex);
2113 mutex_init(&adev->srbm_mutex);
Andres Rodriguezb8866c22017-04-28 20:05:51 -04002114 mutex_init(&adev->gfx.pipe_reserve_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002115 mutex_init(&adev->grbm_idx_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002116 mutex_init(&adev->mn_lock);
Alex Deuchere23b74a2017-09-28 09:47:32 -04002117 mutex_init(&adev->virt.vf_errors.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002118 hash_init(adev->mn_hash);
2119
2120 amdgpu_check_arguments(adev);
2121
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002122 spin_lock_init(&adev->mmio_idx_lock);
2123 spin_lock_init(&adev->smc_idx_lock);
2124 spin_lock_init(&adev->pcie_idx_lock);
2125 spin_lock_init(&adev->uvd_ctx_idx_lock);
2126 spin_lock_init(&adev->didt_idx_lock);
Rex Zhuccdbb202016-06-08 12:47:41 +08002127 spin_lock_init(&adev->gc_cac_idx_lock);
Evan Quan16abb5d2017-07-04 09:21:50 +08002128 spin_lock_init(&adev->se_cac_idx_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002129 spin_lock_init(&adev->audio_endpt_idx_lock);
Marek Olšák95844d22016-08-17 23:49:27 +02002130 spin_lock_init(&adev->mm_stats.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002131
Chunming Zhou0c4e7fa2016-08-17 11:41:30 +08002132 INIT_LIST_HEAD(&adev->shadow_list);
2133 mutex_init(&adev->shadow_list_lock);
2134
Chunming Zhou5c1354b2016-08-30 16:13:10 +08002135 INIT_LIST_HEAD(&adev->gtt_list);
2136 spin_lock_init(&adev->gtt_list_lock);
2137
Andres Rodriguez795f2812017-03-06 16:27:55 -05002138 INIT_LIST_HEAD(&adev->ring_lru_list);
2139 spin_lock_init(&adev->ring_lru_list_lock);
2140
Shirish S2dc80b02017-05-25 10:05:25 +05302141 INIT_DELAYED_WORK(&adev->late_init_work, amdgpu_late_init_func_handler);
2142
Alex Xie0fa49552017-06-08 14:58:05 -04002143 /* Registers mapping */
2144 /* TODO: block userspace mapping of io register */
Ken Wangda69c1612016-01-21 19:08:55 +08002145 if (adev->asic_type >= CHIP_BONAIRE) {
2146 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
2147 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
2148 } else {
2149 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
2150 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
2151 }
Chunming Zhou5c1354b2016-08-30 16:13:10 +08002152
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002153 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
2154 if (adev->rmmio == NULL) {
2155 return -ENOMEM;
2156 }
2157 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
2158 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
2159
Christian König705e5192017-06-08 11:15:16 +02002160 /* doorbell bar mapping */
2161 amdgpu_doorbell_init(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002162
2163 /* io port mapping */
2164 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
2165 if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
2166 adev->rio_mem_size = pci_resource_len(adev->pdev, i);
2167 adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
2168 break;
2169 }
2170 }
2171 if (adev->rio_mem == NULL)
Amber Linb64a18c2017-01-04 08:06:58 -05002172 DRM_INFO("PCI I/O BAR is not found.\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002173
2174 /* early init functions */
2175 r = amdgpu_early_init(adev);
2176 if (r)
2177 return r;
2178
2179 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
2180 /* this will fail for cards that aren't VGA class devices, just
2181 * ignore it */
2182 vga_client_register(adev->pdev, adev, NULL, amdgpu_vga_set_decode);
2183
2184 if (amdgpu_runtime_pm == 1)
2185 runtime = true;
Alex Deuchere9bef452016-04-25 13:12:18 -04002186 if (amdgpu_device_is_px(ddev))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002187 runtime = true;
Lukas Wunner84c8b222017-03-10 21:23:45 +01002188 if (!pci_is_thunderbolt_attached(adev->pdev))
2189 vga_switcheroo_register_client(adev->pdev,
2190 &amdgpu_switcheroo_ops, runtime);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002191 if (runtime)
2192 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
2193
2194 /* Read BIOS */
Alex Deucher83ba1262016-06-03 18:21:41 -04002195 if (!amdgpu_get_bios(adev)) {
2196 r = -EINVAL;
2197 goto failed;
2198 }
Nils Wallméniusf7e9e9f2016-12-14 21:52:45 +01002199
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002200 r = amdgpu_atombios_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002201 if (r) {
2202 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002203 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_INIT_FAIL, 0, 0);
Alex Deucher83ba1262016-06-03 18:21:41 -04002204 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002205 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002206
Monk Liu4e99a442016-03-31 13:26:59 +08002207 /* detect if we are with an SRIOV vbios */
2208 amdgpu_device_detect_sriov_bios(adev);
Andres Rodriguez048765a2016-06-11 02:51:32 -04002209
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002210 /* Post card if necessary */
Monk Liubec86372016-09-14 19:38:08 +08002211 if (amdgpu_vpost_needed(adev)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002212 if (!adev->bios) {
Monk Liubec86372016-09-14 19:38:08 +08002213 dev_err(adev->dev, "no vBIOS found\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002214 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_NO_VBIOS, 0, 0);
Alex Deucher83ba1262016-06-03 18:21:41 -04002215 r = -EINVAL;
2216 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002217 }
Monk Liubec86372016-09-14 19:38:08 +08002218 DRM_INFO("GPU posting now...\n");
Monk Liu4e99a442016-03-31 13:26:59 +08002219 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2220 if (r) {
2221 dev_err(adev->dev, "gpu post error!\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002222 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_GPU_POST_ERROR, 0, 0);
Monk Liu4e99a442016-03-31 13:26:59 +08002223 goto failed;
2224 }
2225 } else {
2226 DRM_INFO("GPU post is not needed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002227 }
2228
Alex Deucher88b64e92017-07-10 10:43:10 -04002229 if (adev->is_atom_fw) {
2230 /* Initialize clocks */
2231 r = amdgpu_atomfirmware_get_clock_info(adev);
2232 if (r) {
2233 dev_err(adev->dev, "amdgpu_atomfirmware_get_clock_info failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002234 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
Alex Deucher88b64e92017-07-10 10:43:10 -04002235 goto failed;
2236 }
2237 } else {
Alex Deuchera5bde2f2016-09-23 16:23:41 -04002238 /* Initialize clocks */
2239 r = amdgpu_atombios_get_clock_info(adev);
2240 if (r) {
2241 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002242 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
Gavin Wan89041942017-06-23 13:55:15 -04002243 goto failed;
Alex Deuchera5bde2f2016-09-23 16:23:41 -04002244 }
2245 /* init i2c buses */
2246 amdgpu_atombios_i2c_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002247 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002248
2249 /* Fence driver */
2250 r = amdgpu_fence_driver_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002251 if (r) {
2252 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002253 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0);
Alex Deucher83ba1262016-06-03 18:21:41 -04002254 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002255 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002256
2257 /* init the mode config */
2258 drm_mode_config_init(adev->ddev);
2259
2260 r = amdgpu_init(adev);
2261 if (r) {
Alex Deucher2c1a2782015-12-07 17:02:53 -05002262 dev_err(adev->dev, "amdgpu_init failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002263 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002264 amdgpu_fini(adev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002265 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002266 }
2267
2268 adev->accel_working = true;
2269
Alex Xiee59c0202017-06-01 09:42:59 -04002270 amdgpu_vm_check_compute_bug(adev);
2271
Marek Olšák95844d22016-08-17 23:49:27 +02002272 /* Initialize the buffer migration limit. */
2273 if (amdgpu_moverate >= 0)
2274 max_MBps = amdgpu_moverate;
2275 else
2276 max_MBps = 8; /* Allow 8 MB/s. */
2277 /* Get a log2 for easy divisions. */
2278 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
2279
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002280 r = amdgpu_ib_pool_init(adev);
2281 if (r) {
2282 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
Alex Deuchere23b74a2017-09-28 09:47:32 -04002283 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r);
Alex Deucher83ba1262016-06-03 18:21:41 -04002284 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002285 }
2286
2287 r = amdgpu_ib_ring_tests(adev);
2288 if (r)
2289 DRM_ERROR("ib ring test failed (%d).\n", r);
2290
Horace Chen2dc8f812017-10-09 16:17:16 +08002291 if (amdgpu_sriov_vf(adev))
2292 amdgpu_virt_init_data_exchange(adev);
2293
Monk Liu9bc92b92017-02-08 17:38:13 +08002294 amdgpu_fbdev_init(adev);
2295
Rex Zhud2f52ac2017-09-22 17:47:27 +08002296 r = amdgpu_pm_sysfs_init(adev);
2297 if (r)
2298 DRM_ERROR("registering pm debugfs failed (%d).\n", r);
2299
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002300 r = amdgpu_gem_debugfs_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002301 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002302 DRM_ERROR("registering gem debugfs failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002303
2304 r = amdgpu_debugfs_regs_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002305 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002306 DRM_ERROR("registering register debugfs failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002307
Huang Rui4f0955f2017-05-10 23:04:06 +08002308 r = amdgpu_debugfs_test_ib_ring_init(adev);
2309 if (r)
2310 DRM_ERROR("registering register test ib ring debugfs failed (%d).\n", r);
2311
Huang Rui50ab2532016-06-12 15:51:09 +08002312 r = amdgpu_debugfs_firmware_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002313 if (r)
Huang Rui50ab2532016-06-12 15:51:09 +08002314 DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
Huang Rui50ab2532016-06-12 15:51:09 +08002315
Kent Russelldb95e212017-08-22 12:31:43 -04002316 r = amdgpu_debugfs_vbios_dump_init(adev);
2317 if (r)
2318 DRM_ERROR("Creating vbios dump debugfs failed (%d).\n", r);
2319
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002320 if ((amdgpu_testing & 1)) {
2321 if (adev->accel_working)
2322 amdgpu_test_moves(adev);
2323 else
2324 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
2325 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002326 if (amdgpu_benchmarking) {
2327 if (adev->accel_working)
2328 amdgpu_benchmark(adev, amdgpu_benchmarking);
2329 else
2330 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
2331 }
2332
2333 /* enable clockgating, etc. after ib tests, etc. since some blocks require
2334 * explicit gating rather than handling it automatically.
2335 */
2336 r = amdgpu_late_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002337 if (r) {
2338 dev_err(adev->dev, "amdgpu_late_init failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002339 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r);
Alex Deucher83ba1262016-06-03 18:21:41 -04002340 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002341 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002342
2343 return 0;
Alex Deucher83ba1262016-06-03 18:21:41 -04002344
2345failed:
Gavin Wan89041942017-06-23 13:55:15 -04002346 amdgpu_vf_error_trans_all(adev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002347 if (runtime)
2348 vga_switcheroo_fini_domain_pm_ops(adev->dev);
2349 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002350}
2351
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002352/**
2353 * amdgpu_device_fini - tear down the driver
2354 *
2355 * @adev: amdgpu_device pointer
2356 *
2357 * Tear down the driver info (all asics).
2358 * Called at driver shutdown.
2359 */
2360void amdgpu_device_fini(struct amdgpu_device *adev)
2361{
2362 int r;
2363
2364 DRM_INFO("amdgpu: finishing device.\n");
2365 adev->shutdown = true;
Pixel Dingdb2c2a92017-04-25 16:47:42 +08002366 if (adev->mode_info.mode_config_initialized)
2367 drm_crtc_force_disable_all(adev->ddev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002368 /* evict vram memory */
2369 amdgpu_bo_evict_vram(adev);
2370 amdgpu_ib_pool_fini(adev);
Horace Chena05502e2017-09-29 14:41:57 +08002371 amdgpu_fw_reserve_vram_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002372 amdgpu_fence_driver_fini(adev);
2373 amdgpu_fbdev_fini(adev);
2374 r = amdgpu_fini(adev);
Huang Ruiab4fe3e2017-06-05 22:11:59 +08002375 if (adev->firmware.gpu_info_fw) {
2376 release_firmware(adev->firmware.gpu_info_fw);
2377 adev->firmware.gpu_info_fw = NULL;
2378 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002379 adev->accel_working = false;
Shirish S2dc80b02017-05-25 10:05:25 +05302380 cancel_delayed_work_sync(&adev->late_init_work);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002381 /* free i2c buses */
2382 amdgpu_i2c_fini(adev);
2383 amdgpu_atombios_fini(adev);
2384 kfree(adev->bios);
2385 adev->bios = NULL;
Lukas Wunner84c8b222017-03-10 21:23:45 +01002386 if (!pci_is_thunderbolt_attached(adev->pdev))
2387 vga_switcheroo_unregister_client(adev->pdev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002388 if (adev->flags & AMD_IS_PX)
2389 vga_switcheroo_fini_domain_pm_ops(adev->dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002390 vga_client_register(adev->pdev, NULL, NULL, NULL);
2391 if (adev->rio_mem)
2392 pci_iounmap(adev->pdev, adev->rio_mem);
2393 adev->rio_mem = NULL;
2394 iounmap(adev->rmmio);
2395 adev->rmmio = NULL;
Christian König705e5192017-06-08 11:15:16 +02002396 amdgpu_doorbell_fini(adev);
Rex Zhud2f52ac2017-09-22 17:47:27 +08002397 amdgpu_pm_sysfs_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002398 amdgpu_debugfs_regs_cleanup(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002399}
2400
2401
2402/*
2403 * Suspend & resume.
2404 */
2405/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002406 * amdgpu_device_suspend - initiate device suspend
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002407 *
2408 * @pdev: drm dev pointer
2409 * @state: suspend state
2410 *
2411 * Puts the hw in the suspend state (all asics).
2412 * Returns 0 for success or an error on failure.
2413 * Called at driver suspend.
2414 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002415int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002416{
2417 struct amdgpu_device *adev;
2418 struct drm_crtc *crtc;
2419 struct drm_connector *connector;
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002420 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002421
2422 if (dev == NULL || dev->dev_private == NULL) {
2423 return -ENODEV;
2424 }
2425
2426 adev = dev->dev_private;
2427
2428 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2429 return 0;
2430
2431 drm_kms_helper_poll_disable(dev);
2432
2433 /* turn off display hw */
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002434 drm_modeset_lock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002435 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2436 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
2437 }
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002438 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002439
Yong Zhaoba997702015-11-09 17:21:45 -05002440 amdgpu_amdkfd_suspend(adev);
2441
Alex Deucher756e6882015-10-08 00:03:36 -04002442 /* unpin the front buffers and cursors */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002443 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Alex Deucher756e6882015-10-08 00:03:36 -04002444 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002445 struct amdgpu_framebuffer *rfb = to_amdgpu_framebuffer(crtc->primary->fb);
2446 struct amdgpu_bo *robj;
2447
Alex Deucher756e6882015-10-08 00:03:36 -04002448 if (amdgpu_crtc->cursor_bo) {
2449 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
Alex Xie7a6901d2017-04-24 13:52:41 -04002450 r = amdgpu_bo_reserve(aobj, true);
Alex Deucher756e6882015-10-08 00:03:36 -04002451 if (r == 0) {
2452 amdgpu_bo_unpin(aobj);
2453 amdgpu_bo_unreserve(aobj);
2454 }
2455 }
2456
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002457 if (rfb == NULL || rfb->obj == NULL) {
2458 continue;
2459 }
2460 robj = gem_to_amdgpu_bo(rfb->obj);
2461 /* don't unpin kernel fb objects */
2462 if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
Alex Xie7a6901d2017-04-24 13:52:41 -04002463 r = amdgpu_bo_reserve(robj, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002464 if (r == 0) {
2465 amdgpu_bo_unpin(robj);
2466 amdgpu_bo_unreserve(robj);
2467 }
2468 }
2469 }
2470 /* evict vram memory */
2471 amdgpu_bo_evict_vram(adev);
2472
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002473 amdgpu_fence_driver_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002474
2475 r = amdgpu_suspend(adev);
2476
Alex Deuchera0a71e42016-10-10 12:41:36 -04002477 /* evict remaining vram memory
2478 * This second call to evict vram is to evict the gart page table
2479 * using the CPU.
2480 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002481 amdgpu_bo_evict_vram(adev);
2482
Alex Deucherd05da0e2017-06-30 17:08:45 -04002483 amdgpu_atombios_scratch_regs_save(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002484 pci_save_state(dev->pdev);
2485 if (suspend) {
2486 /* Shut down the device */
2487 pci_disable_device(dev->pdev);
2488 pci_set_power_state(dev->pdev, PCI_D3hot);
jimqu74b0b152016-09-07 17:09:12 +08002489 } else {
2490 r = amdgpu_asic_reset(adev);
2491 if (r)
2492 DRM_ERROR("amdgpu asic reset failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002493 }
2494
2495 if (fbcon) {
2496 console_lock();
2497 amdgpu_fbdev_set_suspend(adev, 1);
2498 console_unlock();
2499 }
2500 return 0;
2501}
2502
2503/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002504 * amdgpu_device_resume - initiate device resume
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002505 *
2506 * @pdev: drm dev pointer
2507 *
2508 * Bring the hw back to operating state (all asics).
2509 * Returns 0 for success or an error on failure.
2510 * Called at driver resume.
2511 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002512int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002513{
2514 struct drm_connector *connector;
2515 struct amdgpu_device *adev = dev->dev_private;
Alex Deucher756e6882015-10-08 00:03:36 -04002516 struct drm_crtc *crtc;
Huang Rui03161a62017-04-13 16:12:26 +08002517 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002518
2519 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2520 return 0;
2521
jimqu74b0b152016-09-07 17:09:12 +08002522 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002523 console_lock();
jimqu74b0b152016-09-07 17:09:12 +08002524
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002525 if (resume) {
2526 pci_set_power_state(dev->pdev, PCI_D0);
2527 pci_restore_state(dev->pdev);
jimqu74b0b152016-09-07 17:09:12 +08002528 r = pci_enable_device(dev->pdev);
Huang Rui03161a62017-04-13 16:12:26 +08002529 if (r)
2530 goto unlock;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002531 }
Alex Deucherd05da0e2017-06-30 17:08:45 -04002532 amdgpu_atombios_scratch_regs_restore(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002533
2534 /* post card */
Jim Quc836fec2017-02-10 15:59:59 +08002535 if (amdgpu_need_post(adev)) {
jimqu74b0b152016-09-07 17:09:12 +08002536 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2537 if (r)
2538 DRM_ERROR("amdgpu asic init failed\n");
2539 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002540
2541 r = amdgpu_resume(adev);
Rex Zhue6707212017-03-30 13:21:01 +08002542 if (r) {
Flora Cuica198522016-02-04 15:10:08 +08002543 DRM_ERROR("amdgpu_resume failed (%d).\n", r);
Huang Rui03161a62017-04-13 16:12:26 +08002544 goto unlock;
Rex Zhue6707212017-03-30 13:21:01 +08002545 }
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002546 amdgpu_fence_driver_resume(adev);
2547
Flora Cuica198522016-02-04 15:10:08 +08002548 if (resume) {
2549 r = amdgpu_ib_ring_tests(adev);
2550 if (r)
2551 DRM_ERROR("ib ring test failed (%d).\n", r);
2552 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002553
2554 r = amdgpu_late_init(adev);
Huang Rui03161a62017-04-13 16:12:26 +08002555 if (r)
2556 goto unlock;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002557
Alex Deucher756e6882015-10-08 00:03:36 -04002558 /* pin cursors */
2559 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2560 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2561
2562 if (amdgpu_crtc->cursor_bo) {
2563 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
Alex Xie7a6901d2017-04-24 13:52:41 -04002564 r = amdgpu_bo_reserve(aobj, true);
Alex Deucher756e6882015-10-08 00:03:36 -04002565 if (r == 0) {
2566 r = amdgpu_bo_pin(aobj,
2567 AMDGPU_GEM_DOMAIN_VRAM,
2568 &amdgpu_crtc->cursor_addr);
2569 if (r != 0)
2570 DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
2571 amdgpu_bo_unreserve(aobj);
2572 }
2573 }
2574 }
Yong Zhaoba997702015-11-09 17:21:45 -05002575 r = amdgpu_amdkfd_resume(adev);
2576 if (r)
2577 return r;
Alex Deucher756e6882015-10-08 00:03:36 -04002578
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002579 /* blat the mode back in */
2580 if (fbcon) {
2581 drm_helper_resume_force_mode(dev);
2582 /* turn on display hw */
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002583 drm_modeset_lock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002584 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2585 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
2586 }
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002587 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002588 }
2589
2590 drm_kms_helper_poll_enable(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002591
2592 /*
2593 * Most of the connector probing functions try to acquire runtime pm
2594 * refs to ensure that the GPU is powered on when connector polling is
2595 * performed. Since we're calling this from a runtime PM callback,
2596 * trying to acquire rpm refs will cause us to deadlock.
2597 *
2598 * Since we're guaranteed to be holding the rpm lock, it's safe to
2599 * temporarily disable the rpm helpers so this doesn't deadlock us.
2600 */
2601#ifdef CONFIG_PM
2602 dev->dev->power.disable_depth++;
2603#endif
Alex Deucher54fb2a52015-11-24 14:30:56 -05002604 drm_helper_hpd_irq_event(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002605#ifdef CONFIG_PM
2606 dev->dev->power.disable_depth--;
2607#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002608
Huang Rui03161a62017-04-13 16:12:26 +08002609 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002610 amdgpu_fbdev_set_suspend(adev, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002611
Huang Rui03161a62017-04-13 16:12:26 +08002612unlock:
2613 if (fbcon)
2614 console_unlock();
2615
2616 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002617}
2618
Chunming Zhou63fbf422016-07-15 11:19:20 +08002619static bool amdgpu_check_soft_reset(struct amdgpu_device *adev)
2620{
2621 int i;
2622 bool asic_hang = false;
2623
Monk Liuf993d622017-10-16 19:46:01 +08002624 if (amdgpu_sriov_vf(adev))
2625 return true;
2626
Chunming Zhou63fbf422016-07-15 11:19:20 +08002627 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002628 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou63fbf422016-07-15 11:19:20 +08002629 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002630 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
2631 adev->ip_blocks[i].status.hang =
2632 adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
2633 if (adev->ip_blocks[i].status.hang) {
2634 DRM_INFO("IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
Chunming Zhou63fbf422016-07-15 11:19:20 +08002635 asic_hang = true;
2636 }
2637 }
2638 return asic_hang;
2639}
2640
Baoyou Xie4d446652016-09-18 22:09:35 +08002641static int amdgpu_pre_soft_reset(struct amdgpu_device *adev)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002642{
2643 int i, r = 0;
2644
2645 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002646 if (!adev->ip_blocks[i].status.valid)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002647 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002648 if (adev->ip_blocks[i].status.hang &&
2649 adev->ip_blocks[i].version->funcs->pre_soft_reset) {
2650 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
Chunming Zhoud31a5012016-07-18 10:04:34 +08002651 if (r)
2652 return r;
2653 }
2654 }
2655
2656 return 0;
2657}
2658
Chunming Zhou35d782f2016-07-15 15:57:13 +08002659static bool amdgpu_need_full_reset(struct amdgpu_device *adev)
2660{
Alex Deucherda146d32016-10-13 16:07:03 -04002661 int i;
2662
2663 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002664 if (!adev->ip_blocks[i].status.valid)
Alex Deucherda146d32016-10-13 16:07:03 -04002665 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002666 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
2667 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
2668 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
Ken Wang98512bb2017-09-14 16:25:19 +08002669 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) ||
2670 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
Alex Deuchera1255102016-10-13 17:41:13 -04002671 if (adev->ip_blocks[i].status.hang) {
Alex Deucherda146d32016-10-13 16:07:03 -04002672 DRM_INFO("Some block need full reset!\n");
2673 return true;
2674 }
2675 }
Chunming Zhou35d782f2016-07-15 15:57:13 +08002676 }
2677 return false;
2678}
2679
2680static int amdgpu_soft_reset(struct amdgpu_device *adev)
2681{
2682 int i, r = 0;
2683
2684 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002685 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002686 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002687 if (adev->ip_blocks[i].status.hang &&
2688 adev->ip_blocks[i].version->funcs->soft_reset) {
2689 r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002690 if (r)
2691 return r;
2692 }
2693 }
2694
2695 return 0;
2696}
2697
2698static int amdgpu_post_soft_reset(struct amdgpu_device *adev)
2699{
2700 int i, r = 0;
2701
2702 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002703 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002704 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002705 if (adev->ip_blocks[i].status.hang &&
2706 adev->ip_blocks[i].version->funcs->post_soft_reset)
2707 r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002708 if (r)
2709 return r;
2710 }
2711
2712 return 0;
2713}
2714
Chunming Zhou3ad81f12016-08-05 17:30:17 +08002715bool amdgpu_need_backup(struct amdgpu_device *adev)
2716{
2717 if (adev->flags & AMD_IS_APU)
2718 return false;
2719
2720 return amdgpu_lockup_timeout > 0 ? true : false;
2721}
2722
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002723static int amdgpu_recover_vram_from_shadow(struct amdgpu_device *adev,
2724 struct amdgpu_ring *ring,
2725 struct amdgpu_bo *bo,
Chris Wilsonf54d1862016-10-25 13:00:45 +01002726 struct dma_fence **fence)
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002727{
2728 uint32_t domain;
2729 int r;
2730
Roger.He23d2e502017-04-21 14:24:26 +08002731 if (!bo->shadow)
2732 return 0;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002733
Alex Xie1d284792017-04-24 13:53:04 -04002734 r = amdgpu_bo_reserve(bo, true);
Roger.He23d2e502017-04-21 14:24:26 +08002735 if (r)
2736 return r;
2737 domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
2738 /* if bo has been evicted, then no need to recover */
2739 if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
Roger.He82521312017-04-21 13:08:43 +08002740 r = amdgpu_bo_validate(bo->shadow);
2741 if (r) {
2742 DRM_ERROR("bo validate failed!\n");
2743 goto err;
2744 }
2745
Roger.He23d2e502017-04-21 14:24:26 +08002746 r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002747 NULL, fence, true);
Roger.He23d2e502017-04-21 14:24:26 +08002748 if (r) {
2749 DRM_ERROR("recover page table failed!\n");
2750 goto err;
2751 }
2752 }
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002753err:
Roger.He23d2e502017-04-21 14:24:26 +08002754 amdgpu_bo_unreserve(bo);
2755 return r;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002756}
2757
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002758/**
Monk Liua90ad3c2017-01-23 14:22:08 +08002759 * amdgpu_sriov_gpu_reset - reset the asic
2760 *
2761 * @adev: amdgpu device pointer
Monk Liu7225f872017-04-26 14:51:54 +08002762 * @job: which job trigger hang
Monk Liua90ad3c2017-01-23 14:22:08 +08002763 *
2764 * Attempt the reset the GPU if it has hung (all asics).
2765 * for SRIOV case.
2766 * Returns 0 for success or an error on failure.
2767 */
Monk Liu7225f872017-04-26 14:51:54 +08002768int amdgpu_sriov_gpu_reset(struct amdgpu_device *adev, struct amdgpu_job *job)
Monk Liua90ad3c2017-01-23 14:22:08 +08002769{
Monk Liu65781c72017-05-11 13:36:44 +08002770 int i, j, r = 0;
Monk Liua90ad3c2017-01-23 14:22:08 +08002771 int resched;
2772 struct amdgpu_bo *bo, *tmp;
2773 struct amdgpu_ring *ring;
2774 struct dma_fence *fence = NULL, *next = NULL;
2775
Monk Liu147b5982017-01-25 15:48:01 +08002776 mutex_lock(&adev->virt.lock_reset);
Monk Liua90ad3c2017-01-23 14:22:08 +08002777 atomic_inc(&adev->gpu_reset_counter);
Monk Liu3224a122017-09-15 18:57:12 +08002778 adev->in_sriov_reset = true;
Monk Liua90ad3c2017-01-23 14:22:08 +08002779
2780 /* block TTM */
2781 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
2782
Monk Liu65781c72017-05-11 13:36:44 +08002783 /* we start from the ring trigger GPU hang */
2784 j = job ? job->ring->idx : 0;
Monk Liua90ad3c2017-01-23 14:22:08 +08002785
Monk Liu65781c72017-05-11 13:36:44 +08002786 /* block scheduler */
2787 for (i = j; i < j + AMDGPU_MAX_RINGS; ++i) {
2788 ring = adev->rings[i % AMDGPU_MAX_RINGS];
Monk Liua90ad3c2017-01-23 14:22:08 +08002789 if (!ring || !ring->sched.thread)
2790 continue;
2791
2792 kthread_park(ring->sched.thread);
Monk Liua90ad3c2017-01-23 14:22:08 +08002793
Monk Liu65781c72017-05-11 13:36:44 +08002794 if (job && j != i)
2795 continue;
2796
Monk Liu4f059ec2017-05-11 13:59:15 +08002797 /* here give the last chance to check if job removed from mirror-list
Monk Liu65781c72017-05-11 13:36:44 +08002798 * since we already pay some time on kthread_park */
Monk Liu4f059ec2017-05-11 13:59:15 +08002799 if (job && list_empty(&job->base.node)) {
Monk Liu65781c72017-05-11 13:36:44 +08002800 kthread_unpark(ring->sched.thread);
2801 goto give_up_reset;
2802 }
2803
2804 if (amd_sched_invalidate_job(&job->base, amdgpu_job_hang_limit))
2805 amd_sched_job_kickout(&job->base);
2806
2807 /* only do job_reset on the hang ring if @job not NULL */
Monk Liua90ad3c2017-01-23 14:22:08 +08002808 amd_sched_hw_job_reset(&ring->sched);
Monk Liu65781c72017-05-11 13:36:44 +08002809
2810 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
2811 amdgpu_fence_driver_force_completion_ring(ring);
Monk Liua90ad3c2017-01-23 14:22:08 +08002812 }
2813
Monk Liua90ad3c2017-01-23 14:22:08 +08002814 /* request to take full control of GPU before re-initialization */
Monk Liu7225f872017-04-26 14:51:54 +08002815 if (job)
Monk Liua90ad3c2017-01-23 14:22:08 +08002816 amdgpu_virt_reset_gpu(adev);
2817 else
2818 amdgpu_virt_request_full_gpu(adev, true);
2819
2820
2821 /* Resume IP prior to SMC */
Monk Liue4f0fdc2017-02-09 11:55:49 +08002822 amdgpu_sriov_reinit_early(adev);
Monk Liua90ad3c2017-01-23 14:22:08 +08002823
2824 /* we need recover gart prior to run SMC/CP/SDMA resume */
2825 amdgpu_ttm_recover_gart(adev);
2826
2827 /* now we are okay to resume SMC/CP/SDMA */
Monk Liue4f0fdc2017-02-09 11:55:49 +08002828 amdgpu_sriov_reinit_late(adev);
Monk Liua90ad3c2017-01-23 14:22:08 +08002829
2830 amdgpu_irq_gpu_reset_resume_helper(adev);
2831
2832 if (amdgpu_ib_ring_tests(adev))
2833 dev_err(adev->dev, "[GPU_RESET] ib ring test failed (%d).\n", r);
2834
2835 /* release full control of GPU after ib test */
2836 amdgpu_virt_release_full_gpu(adev, true);
2837
2838 DRM_INFO("recover vram bo from shadow\n");
2839
2840 ring = adev->mman.buffer_funcs_ring;
2841 mutex_lock(&adev->shadow_list_lock);
2842 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
Monk Liu236763d2017-05-01 16:15:31 +08002843 next = NULL;
Monk Liua90ad3c2017-01-23 14:22:08 +08002844 amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
2845 if (fence) {
2846 r = dma_fence_wait(fence, false);
2847 if (r) {
2848 WARN(r, "recovery from shadow isn't completed\n");
2849 break;
2850 }
2851 }
2852
2853 dma_fence_put(fence);
2854 fence = next;
2855 }
2856 mutex_unlock(&adev->shadow_list_lock);
2857
2858 if (fence) {
2859 r = dma_fence_wait(fence, false);
2860 if (r)
2861 WARN(r, "recovery from shadow isn't completed\n");
2862 }
2863 dma_fence_put(fence);
2864
Monk Liu65781c72017-05-11 13:36:44 +08002865 for (i = j; i < j + AMDGPU_MAX_RINGS; ++i) {
2866 ring = adev->rings[i % AMDGPU_MAX_RINGS];
Monk Liua90ad3c2017-01-23 14:22:08 +08002867 if (!ring || !ring->sched.thread)
2868 continue;
2869
Monk Liu65781c72017-05-11 13:36:44 +08002870 if (job && j != i) {
2871 kthread_unpark(ring->sched.thread);
2872 continue;
2873 }
2874
Monk Liua90ad3c2017-01-23 14:22:08 +08002875 amd_sched_job_recovery(&ring->sched);
2876 kthread_unpark(ring->sched.thread);
2877 }
2878
2879 drm_helper_resume_force_mode(adev->ddev);
Monk Liu65781c72017-05-11 13:36:44 +08002880give_up_reset:
Monk Liua90ad3c2017-01-23 14:22:08 +08002881 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
2882 if (r) {
2883 /* bad news, how to tell it to userspace ? */
2884 dev_info(adev->dev, "GPU reset failed\n");
Monk Liu65781c72017-05-11 13:36:44 +08002885 } else {
2886 dev_info(adev->dev, "GPU reset successed!\n");
Monk Liua90ad3c2017-01-23 14:22:08 +08002887 }
2888
Monk Liu3224a122017-09-15 18:57:12 +08002889 adev->in_sriov_reset = false;
Monk Liu147b5982017-01-25 15:48:01 +08002890 mutex_unlock(&adev->virt.lock_reset);
Monk Liua90ad3c2017-01-23 14:22:08 +08002891 return r;
2892}
2893
2894/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002895 * amdgpu_gpu_reset - reset the asic
2896 *
2897 * @adev: amdgpu device pointer
2898 *
2899 * Attempt the reset the GPU if it has hung (all asics).
2900 * Returns 0 for success or an error on failure.
2901 */
2902int amdgpu_gpu_reset(struct amdgpu_device *adev)
2903{
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002904 int i, r;
2905 int resched;
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002906 bool need_full_reset, vram_lost = false;
Xiangliang Yufb140b22016-12-17 22:48:57 +08002907
Chunming Zhou63fbf422016-07-15 11:19:20 +08002908 if (!amdgpu_check_soft_reset(adev)) {
2909 DRM_INFO("No hardware hang detected. Did some blocks stall?\n");
2910 return 0;
2911 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002912
Marek Olšákd94aed52015-05-05 21:13:49 +02002913 atomic_inc(&adev->gpu_reset_counter);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002914
Chunming Zhoua3c47d62016-06-30 16:44:41 +08002915 /* block TTM */
2916 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
2917
Chunming Zhou0875dc92016-06-12 15:41:58 +08002918 /* block scheduler */
2919 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2920 struct amdgpu_ring *ring = adev->rings[i];
2921
Chunming Zhou51687752017-04-24 17:09:15 +08002922 if (!ring || !ring->sched.thread)
Chunming Zhou0875dc92016-06-12 15:41:58 +08002923 continue;
2924 kthread_park(ring->sched.thread);
Chunming Zhouaa1c8902016-06-30 13:56:02 +08002925 amd_sched_hw_job_reset(&ring->sched);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002926 }
Chunming Zhou2200eda2016-06-30 16:53:02 +08002927 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
2928 amdgpu_fence_driver_force_completion(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002929
Chunming Zhou35d782f2016-07-15 15:57:13 +08002930 need_full_reset = amdgpu_need_full_reset(adev);
2931
2932 if (!need_full_reset) {
2933 amdgpu_pre_soft_reset(adev);
2934 r = amdgpu_soft_reset(adev);
2935 amdgpu_post_soft_reset(adev);
2936 if (r || amdgpu_check_soft_reset(adev)) {
2937 DRM_INFO("soft reset failed, will fallback to full reset!\n");
2938 need_full_reset = true;
2939 }
2940 }
2941
2942 if (need_full_reset) {
Chunming Zhou35d782f2016-07-15 15:57:13 +08002943 r = amdgpu_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002944
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002945retry:
Alex Deucherd05da0e2017-06-30 17:08:45 -04002946 amdgpu_atombios_scratch_regs_save(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002947 r = amdgpu_asic_reset(adev);
Alex Deucherd05da0e2017-06-30 17:08:45 -04002948 amdgpu_atombios_scratch_regs_restore(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002949 /* post card */
2950 amdgpu_atom_asic_init(adev->mode_info.atom_context);
Alex Deucherbfa99262016-01-15 11:59:48 -05002951
Chunming Zhou35d782f2016-07-15 15:57:13 +08002952 if (!r) {
2953 dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
Chunming Zhoufcf06492017-05-05 10:33:33 +08002954 r = amdgpu_resume_phase1(adev);
2955 if (r)
2956 goto out;
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002957 vram_lost = amdgpu_check_vram_lost(adev);
Chunming Zhouf1892132017-05-15 16:48:27 +08002958 if (vram_lost) {
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002959 DRM_ERROR("VRAM is lost!\n");
Chunming Zhouf1892132017-05-15 16:48:27 +08002960 atomic_inc(&adev->vram_lost_counter);
2961 }
Chunming Zhou2c0d7312016-08-30 16:36:25 +08002962 r = amdgpu_ttm_recover_gart(adev);
2963 if (r)
Chunming Zhoufcf06492017-05-05 10:33:33 +08002964 goto out;
2965 r = amdgpu_resume_phase2(adev);
2966 if (r)
2967 goto out;
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002968 if (vram_lost)
2969 amdgpu_fill_reset_magic(adev);
Chunming Zhou2c0d7312016-08-30 16:36:25 +08002970 }
Chunming Zhoufcf06492017-05-05 10:33:33 +08002971 }
2972out:
2973 if (!r) {
2974 amdgpu_irq_gpu_reset_resume_helper(adev);
Chunming Zhou1f465082016-06-30 15:02:26 +08002975 r = amdgpu_ib_ring_tests(adev);
2976 if (r) {
2977 dev_err(adev->dev, "ib ring test failed (%d).\n", r);
Chunming Zhou40019dc2016-06-29 16:01:49 +08002978 r = amdgpu_suspend(adev);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002979 need_full_reset = true;
Chunming Zhou40019dc2016-06-29 16:01:49 +08002980 goto retry;
Chunming Zhou1f465082016-06-30 15:02:26 +08002981 }
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002982 /**
2983 * recovery vm page tables, since we cannot depend on VRAM is
2984 * consistent after gpu full reset.
2985 */
2986 if (need_full_reset && amdgpu_need_backup(adev)) {
2987 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
2988 struct amdgpu_bo *bo, *tmp;
Chris Wilsonf54d1862016-10-25 13:00:45 +01002989 struct dma_fence *fence = NULL, *next = NULL;
Chunming Zhou1f465082016-06-30 15:02:26 +08002990
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002991 DRM_INFO("recover vram bo from shadow\n");
2992 mutex_lock(&adev->shadow_list_lock);
2993 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
Monk Liu236763d2017-05-01 16:15:31 +08002994 next = NULL;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002995 amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
2996 if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01002997 r = dma_fence_wait(fence, false);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002998 if (r) {
Monk Liu1d7b17b2017-01-22 18:52:56 +08002999 WARN(r, "recovery from shadow isn't completed\n");
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003000 break;
3001 }
3002 }
3003
Chris Wilsonf54d1862016-10-25 13:00:45 +01003004 dma_fence_put(fence);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003005 fence = next;
3006 }
3007 mutex_unlock(&adev->shadow_list_lock);
3008 if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01003009 r = dma_fence_wait(fence, false);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003010 if (r)
Monk Liu1d7b17b2017-01-22 18:52:56 +08003011 WARN(r, "recovery from shadow isn't completed\n");
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003012 }
Chris Wilsonf54d1862016-10-25 13:00:45 +01003013 dma_fence_put(fence);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003014 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003015 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
3016 struct amdgpu_ring *ring = adev->rings[i];
Chunming Zhou51687752017-04-24 17:09:15 +08003017
3018 if (!ring || !ring->sched.thread)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003019 continue;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003020
Chunming Zhouaa1c8902016-06-30 13:56:02 +08003021 amd_sched_job_recovery(&ring->sched);
Chunming Zhou0875dc92016-06-12 15:41:58 +08003022 kthread_unpark(ring->sched.thread);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003023 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003024 } else {
Chunming Zhou2200eda2016-06-30 16:53:02 +08003025 dev_err(adev->dev, "asic resume failed (%d).\n", r);
Alex Deuchere23b74a2017-09-28 09:47:32 -04003026 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ASIC_RESUME_FAIL, 0, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003027 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
Chunming Zhou51687752017-04-24 17:09:15 +08003028 if (adev->rings[i] && adev->rings[i]->sched.thread) {
Chunming Zhou0875dc92016-06-12 15:41:58 +08003029 kthread_unpark(adev->rings[i]->sched.thread);
Chunming Zhou0875dc92016-06-12 15:41:58 +08003030 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003031 }
3032 }
3033
3034 drm_helper_resume_force_mode(adev->ddev);
3035
3036 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
Gavin Wan89041942017-06-23 13:55:15 -04003037 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003038 /* bad news, how to tell it to userspace ? */
3039 dev_info(adev->dev, "GPU reset failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04003040 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, r);
Gavin Wan89041942017-06-23 13:55:15 -04003041 }
3042 else {
Chunming Zhou6643be62017-05-05 10:50:09 +08003043 dev_info(adev->dev, "GPU reset successed!\n");
Gavin Wan89041942017-06-23 13:55:15 -04003044 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003045
Gavin Wan89041942017-06-23 13:55:15 -04003046 amdgpu_vf_error_trans_all(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003047 return r;
3048}
3049
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003050void amdgpu_get_pcie_info(struct amdgpu_device *adev)
3051{
3052 u32 mask;
3053 int ret;
3054
Alex Deuchercd474ba2016-02-04 10:21:23 -05003055 if (amdgpu_pcie_gen_cap)
3056 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
3057
3058 if (amdgpu_pcie_lane_cap)
3059 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
3060
3061 /* covers APUs as well */
3062 if (pci_is_root_bus(adev->pdev->bus)) {
3063 if (adev->pm.pcie_gen_mask == 0)
3064 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
3065 if (adev->pm.pcie_mlw_mask == 0)
3066 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003067 return;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003068 }
Alex Deuchercd474ba2016-02-04 10:21:23 -05003069
3070 if (adev->pm.pcie_gen_mask == 0) {
3071 ret = drm_pcie_get_speed_cap_mask(adev->ddev, &mask);
3072 if (!ret) {
3073 adev->pm.pcie_gen_mask = (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
3074 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
3075 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
3076
3077 if (mask & DRM_PCIE_SPEED_25)
3078 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
3079 if (mask & DRM_PCIE_SPEED_50)
3080 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2;
3081 if (mask & DRM_PCIE_SPEED_80)
3082 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3;
3083 } else {
3084 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
3085 }
3086 }
3087 if (adev->pm.pcie_mlw_mask == 0) {
3088 ret = drm_pcie_get_max_link_width(adev->ddev, &mask);
3089 if (!ret) {
3090 switch (mask) {
3091 case 32:
3092 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
3093 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
3094 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3095 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3096 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3097 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3098 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3099 break;
3100 case 16:
3101 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
3102 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3103 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3104 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3105 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3106 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3107 break;
3108 case 12:
3109 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3110 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3111 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3112 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3113 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3114 break;
3115 case 8:
3116 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3117 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3118 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3119 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3120 break;
3121 case 4:
3122 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3123 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3124 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3125 break;
3126 case 2:
3127 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3128 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3129 break;
3130 case 1:
3131 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
3132 break;
3133 default:
3134 break;
3135 }
3136 } else {
3137 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003138 }
3139 }
3140}
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003141
3142/*
3143 * Debugfs
3144 */
3145int amdgpu_debugfs_add_files(struct amdgpu_device *adev,
Nils Wallménius06ab6832016-05-02 12:46:15 -04003146 const struct drm_info_list *files,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003147 unsigned nfiles)
3148{
3149 unsigned i;
3150
3151 for (i = 0; i < adev->debugfs_count; i++) {
3152 if (adev->debugfs[i].files == files) {
3153 /* Already registered */
3154 return 0;
3155 }
3156 }
3157
3158 i = adev->debugfs_count + 1;
3159 if (i > AMDGPU_DEBUGFS_MAX_COMPONENTS) {
3160 DRM_ERROR("Reached maximum number of debugfs components.\n");
3161 DRM_ERROR("Report so we increase "
3162 "AMDGPU_DEBUGFS_MAX_COMPONENTS.\n");
3163 return -EINVAL;
3164 }
3165 adev->debugfs[adev->debugfs_count].files = files;
3166 adev->debugfs[adev->debugfs_count].num_files = nfiles;
3167 adev->debugfs_count = i;
3168#if defined(CONFIG_DEBUG_FS)
3169 drm_debugfs_create_files(files, nfiles,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003170 adev->ddev->primary->debugfs_root,
3171 adev->ddev->primary);
3172#endif
3173 return 0;
3174}
3175
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003176#if defined(CONFIG_DEBUG_FS)
3177
3178static ssize_t amdgpu_debugfs_regs_read(struct file *f, char __user *buf,
3179 size_t size, loff_t *pos)
3180{
Al Viro45063092016-12-04 18:24:56 -05003181 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003182 ssize_t result = 0;
3183 int r;
Tom St Denisbd122672016-07-28 09:39:22 -04003184 bool pm_pg_lock, use_bank;
Tom St Denis566281592016-06-27 11:55:07 -04003185 unsigned instance_bank, sh_bank, se_bank;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003186
3187 if (size & 0x3 || *pos & 0x3)
3188 return -EINVAL;
3189
Tom St Denisbd122672016-07-28 09:39:22 -04003190 /* are we reading registers for which a PG lock is necessary? */
3191 pm_pg_lock = (*pos >> 23) & 1;
3192
Tom St Denis566281592016-06-27 11:55:07 -04003193 if (*pos & (1ULL << 62)) {
3194 se_bank = (*pos >> 24) & 0x3FF;
3195 sh_bank = (*pos >> 34) & 0x3FF;
3196 instance_bank = (*pos >> 44) & 0x3FF;
Tom St Denis32977f92016-10-09 07:41:26 -04003197
3198 if (se_bank == 0x3FF)
3199 se_bank = 0xFFFFFFFF;
3200 if (sh_bank == 0x3FF)
3201 sh_bank = 0xFFFFFFFF;
3202 if (instance_bank == 0x3FF)
3203 instance_bank = 0xFFFFFFFF;
Tom St Denis566281592016-06-27 11:55:07 -04003204 use_bank = 1;
Tom St Denis566281592016-06-27 11:55:07 -04003205 } else {
3206 use_bank = 0;
3207 }
3208
Tom St Denis801a6aa9a62017-03-15 05:34:25 -04003209 *pos &= (1UL << 22) - 1;
Tom St Denisbd122672016-07-28 09:39:22 -04003210
Tom St Denis566281592016-06-27 11:55:07 -04003211 if (use_bank) {
Tom St Denis32977f92016-10-09 07:41:26 -04003212 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
3213 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
Tom St Denis566281592016-06-27 11:55:07 -04003214 return -EINVAL;
3215 mutex_lock(&adev->grbm_idx_mutex);
3216 amdgpu_gfx_select_se_sh(adev, se_bank,
3217 sh_bank, instance_bank);
3218 }
3219
Tom St Denisbd122672016-07-28 09:39:22 -04003220 if (pm_pg_lock)
3221 mutex_lock(&adev->pm.mutex);
3222
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003223 while (size) {
3224 uint32_t value;
3225
3226 if (*pos > adev->rmmio_size)
Tom St Denis566281592016-06-27 11:55:07 -04003227 goto end;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003228
3229 value = RREG32(*pos >> 2);
3230 r = put_user(value, (uint32_t *)buf);
Tom St Denis566281592016-06-27 11:55:07 -04003231 if (r) {
3232 result = r;
3233 goto end;
3234 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003235
3236 result += 4;
3237 buf += 4;
3238 *pos += 4;
3239 size -= 4;
3240 }
3241
Tom St Denis566281592016-06-27 11:55:07 -04003242end:
3243 if (use_bank) {
3244 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
3245 mutex_unlock(&adev->grbm_idx_mutex);
3246 }
3247
Tom St Denisbd122672016-07-28 09:39:22 -04003248 if (pm_pg_lock)
3249 mutex_unlock(&adev->pm.mutex);
3250
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003251 return result;
3252}
3253
3254static ssize_t amdgpu_debugfs_regs_write(struct file *f, const char __user *buf,
3255 size_t size, loff_t *pos)
3256{
Al Viro45063092016-12-04 18:24:56 -05003257 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003258 ssize_t result = 0;
3259 int r;
Tom St Denis394fdde2016-10-10 07:31:23 -04003260 bool pm_pg_lock, use_bank;
3261 unsigned instance_bank, sh_bank, se_bank;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003262
3263 if (size & 0x3 || *pos & 0x3)
3264 return -EINVAL;
3265
Tom St Denis394fdde2016-10-10 07:31:23 -04003266 /* are we reading registers for which a PG lock is necessary? */
3267 pm_pg_lock = (*pos >> 23) & 1;
3268
3269 if (*pos & (1ULL << 62)) {
3270 se_bank = (*pos >> 24) & 0x3FF;
3271 sh_bank = (*pos >> 34) & 0x3FF;
3272 instance_bank = (*pos >> 44) & 0x3FF;
3273
3274 if (se_bank == 0x3FF)
3275 se_bank = 0xFFFFFFFF;
3276 if (sh_bank == 0x3FF)
3277 sh_bank = 0xFFFFFFFF;
3278 if (instance_bank == 0x3FF)
3279 instance_bank = 0xFFFFFFFF;
3280 use_bank = 1;
3281 } else {
3282 use_bank = 0;
3283 }
3284
Tom St Denis801a6aa9a62017-03-15 05:34:25 -04003285 *pos &= (1UL << 22) - 1;
Tom St Denis394fdde2016-10-10 07:31:23 -04003286
3287 if (use_bank) {
3288 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
3289 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
3290 return -EINVAL;
3291 mutex_lock(&adev->grbm_idx_mutex);
3292 amdgpu_gfx_select_se_sh(adev, se_bank,
3293 sh_bank, instance_bank);
3294 }
3295
3296 if (pm_pg_lock)
3297 mutex_lock(&adev->pm.mutex);
3298
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003299 while (size) {
3300 uint32_t value;
3301
3302 if (*pos > adev->rmmio_size)
3303 return result;
3304
3305 r = get_user(value, (uint32_t *)buf);
3306 if (r)
3307 return r;
3308
3309 WREG32(*pos >> 2, value);
3310
3311 result += 4;
3312 buf += 4;
3313 *pos += 4;
3314 size -= 4;
3315 }
3316
Tom St Denis394fdde2016-10-10 07:31:23 -04003317 if (use_bank) {
3318 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
3319 mutex_unlock(&adev->grbm_idx_mutex);
3320 }
3321
3322 if (pm_pg_lock)
3323 mutex_unlock(&adev->pm.mutex);
3324
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003325 return result;
3326}
3327
Tom St Denisadcec282016-04-15 13:08:44 -04003328static ssize_t amdgpu_debugfs_regs_pcie_read(struct file *f, char __user *buf,
3329 size_t size, loff_t *pos)
3330{
Al Viro45063092016-12-04 18:24:56 -05003331 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003332 ssize_t result = 0;
3333 int r;
3334
3335 if (size & 0x3 || *pos & 0x3)
3336 return -EINVAL;
3337
3338 while (size) {
3339 uint32_t value;
3340
3341 value = RREG32_PCIE(*pos >> 2);
3342 r = put_user(value, (uint32_t *)buf);
3343 if (r)
3344 return r;
3345
3346 result += 4;
3347 buf += 4;
3348 *pos += 4;
3349 size -= 4;
3350 }
3351
3352 return result;
3353}
3354
3355static ssize_t amdgpu_debugfs_regs_pcie_write(struct file *f, const char __user *buf,
3356 size_t size, loff_t *pos)
3357{
Al Viro45063092016-12-04 18:24:56 -05003358 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003359 ssize_t result = 0;
3360 int r;
3361
3362 if (size & 0x3 || *pos & 0x3)
3363 return -EINVAL;
3364
3365 while (size) {
3366 uint32_t value;
3367
3368 r = get_user(value, (uint32_t *)buf);
3369 if (r)
3370 return r;
3371
3372 WREG32_PCIE(*pos >> 2, value);
3373
3374 result += 4;
3375 buf += 4;
3376 *pos += 4;
3377 size -= 4;
3378 }
3379
3380 return result;
3381}
3382
3383static ssize_t amdgpu_debugfs_regs_didt_read(struct file *f, char __user *buf,
3384 size_t size, loff_t *pos)
3385{
Al Viro45063092016-12-04 18:24:56 -05003386 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003387 ssize_t result = 0;
3388 int r;
3389
3390 if (size & 0x3 || *pos & 0x3)
3391 return -EINVAL;
3392
3393 while (size) {
3394 uint32_t value;
3395
3396 value = RREG32_DIDT(*pos >> 2);
3397 r = put_user(value, (uint32_t *)buf);
3398 if (r)
3399 return r;
3400
3401 result += 4;
3402 buf += 4;
3403 *pos += 4;
3404 size -= 4;
3405 }
3406
3407 return result;
3408}
3409
3410static ssize_t amdgpu_debugfs_regs_didt_write(struct file *f, const char __user *buf,
3411 size_t size, loff_t *pos)
3412{
Al Viro45063092016-12-04 18:24:56 -05003413 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003414 ssize_t result = 0;
3415 int r;
3416
3417 if (size & 0x3 || *pos & 0x3)
3418 return -EINVAL;
3419
3420 while (size) {
3421 uint32_t value;
3422
3423 r = get_user(value, (uint32_t *)buf);
3424 if (r)
3425 return r;
3426
3427 WREG32_DIDT(*pos >> 2, value);
3428
3429 result += 4;
3430 buf += 4;
3431 *pos += 4;
3432 size -= 4;
3433 }
3434
3435 return result;
3436}
3437
3438static ssize_t amdgpu_debugfs_regs_smc_read(struct file *f, char __user *buf,
3439 size_t size, loff_t *pos)
3440{
Al Viro45063092016-12-04 18:24:56 -05003441 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003442 ssize_t result = 0;
3443 int r;
3444
3445 if (size & 0x3 || *pos & 0x3)
3446 return -EINVAL;
3447
3448 while (size) {
3449 uint32_t value;
3450
Tom St Denis6fc0dea2016-08-29 08:39:29 -04003451 value = RREG32_SMC(*pos);
Tom St Denisadcec282016-04-15 13:08:44 -04003452 r = put_user(value, (uint32_t *)buf);
3453 if (r)
3454 return r;
3455
3456 result += 4;
3457 buf += 4;
3458 *pos += 4;
3459 size -= 4;
3460 }
3461
3462 return result;
3463}
3464
3465static ssize_t amdgpu_debugfs_regs_smc_write(struct file *f, const char __user *buf,
3466 size_t size, loff_t *pos)
3467{
Al Viro45063092016-12-04 18:24:56 -05003468 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003469 ssize_t result = 0;
3470 int r;
3471
3472 if (size & 0x3 || *pos & 0x3)
3473 return -EINVAL;
3474
3475 while (size) {
3476 uint32_t value;
3477
3478 r = get_user(value, (uint32_t *)buf);
3479 if (r)
3480 return r;
3481
Tom St Denis6fc0dea2016-08-29 08:39:29 -04003482 WREG32_SMC(*pos, value);
Tom St Denisadcec282016-04-15 13:08:44 -04003483
3484 result += 4;
3485 buf += 4;
3486 *pos += 4;
3487 size -= 4;
3488 }
3489
3490 return result;
3491}
3492
Tom St Denis1e051412016-06-27 09:57:18 -04003493static ssize_t amdgpu_debugfs_gca_config_read(struct file *f, char __user *buf,
3494 size_t size, loff_t *pos)
3495{
Al Viro45063092016-12-04 18:24:56 -05003496 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denis1e051412016-06-27 09:57:18 -04003497 ssize_t result = 0;
3498 int r;
3499 uint32_t *config, no_regs = 0;
3500
3501 if (size & 0x3 || *pos & 0x3)
3502 return -EINVAL;
3503
Markus Elfringecab7662016-09-18 17:00:52 +02003504 config = kmalloc_array(256, sizeof(*config), GFP_KERNEL);
Tom St Denis1e051412016-06-27 09:57:18 -04003505 if (!config)
3506 return -ENOMEM;
3507
3508 /* version, increment each time something is added */
Tom St Denis9a999352017-01-18 13:01:25 -05003509 config[no_regs++] = 3;
Tom St Denis1e051412016-06-27 09:57:18 -04003510 config[no_regs++] = adev->gfx.config.max_shader_engines;
3511 config[no_regs++] = adev->gfx.config.max_tile_pipes;
3512 config[no_regs++] = adev->gfx.config.max_cu_per_sh;
3513 config[no_regs++] = adev->gfx.config.max_sh_per_se;
3514 config[no_regs++] = adev->gfx.config.max_backends_per_se;
3515 config[no_regs++] = adev->gfx.config.max_texture_channel_caches;
3516 config[no_regs++] = adev->gfx.config.max_gprs;
3517 config[no_regs++] = adev->gfx.config.max_gs_threads;
3518 config[no_regs++] = adev->gfx.config.max_hw_contexts;
3519 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_frontend;
3520 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_backend;
3521 config[no_regs++] = adev->gfx.config.sc_hiz_tile_fifo_size;
3522 config[no_regs++] = adev->gfx.config.sc_earlyz_tile_fifo_size;
3523 config[no_regs++] = adev->gfx.config.num_tile_pipes;
3524 config[no_regs++] = adev->gfx.config.backend_enable_mask;
3525 config[no_regs++] = adev->gfx.config.mem_max_burst_length_bytes;
3526 config[no_regs++] = adev->gfx.config.mem_row_size_in_kb;
3527 config[no_regs++] = adev->gfx.config.shader_engine_tile_size;
3528 config[no_regs++] = adev->gfx.config.num_gpus;
3529 config[no_regs++] = adev->gfx.config.multi_gpu_tile_size;
3530 config[no_regs++] = adev->gfx.config.mc_arb_ramcfg;
3531 config[no_regs++] = adev->gfx.config.gb_addr_config;
3532 config[no_regs++] = adev->gfx.config.num_rbs;
3533
Tom St Denis89a8f302016-08-12 15:14:31 -04003534 /* rev==1 */
3535 config[no_regs++] = adev->rev_id;
3536 config[no_regs++] = adev->pg_flags;
3537 config[no_regs++] = adev->cg_flags;
3538
Tom St Denise9f11dc2016-08-17 12:00:51 -04003539 /* rev==2 */
3540 config[no_regs++] = adev->family;
3541 config[no_regs++] = adev->external_rev_id;
3542
Tom St Denis9a999352017-01-18 13:01:25 -05003543 /* rev==3 */
3544 config[no_regs++] = adev->pdev->device;
3545 config[no_regs++] = adev->pdev->revision;
3546 config[no_regs++] = adev->pdev->subsystem_device;
3547 config[no_regs++] = adev->pdev->subsystem_vendor;
3548
Tom St Denis1e051412016-06-27 09:57:18 -04003549 while (size && (*pos < no_regs * 4)) {
3550 uint32_t value;
3551
3552 value = config[*pos >> 2];
3553 r = put_user(value, (uint32_t *)buf);
3554 if (r) {
3555 kfree(config);
3556 return r;
3557 }
3558
3559 result += 4;
3560 buf += 4;
3561 *pos += 4;
3562 size -= 4;
3563 }
3564
3565 kfree(config);
3566 return result;
3567}
3568
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003569static ssize_t amdgpu_debugfs_sensor_read(struct file *f, char __user *buf,
3570 size_t size, loff_t *pos)
3571{
Al Viro45063092016-12-04 18:24:56 -05003572 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003573 int idx, x, outsize, r, valuesize;
3574 uint32_t values[16];
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003575
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003576 if (size & 3 || *pos & 0x3)
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003577 return -EINVAL;
3578
Samuel Pitoiset3cbc6142017-02-15 19:32:29 +01003579 if (amdgpu_dpm == 0)
3580 return -EINVAL;
3581
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003582 /* convert offset to sensor number */
3583 idx = *pos >> 2;
3584
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003585 valuesize = sizeof(values);
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003586 if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->read_sensor)
Rex Zhucd4d7462017-09-06 18:43:52 +08003587 r = amdgpu_dpm_read_sensor(adev, idx, &values[0], &valuesize);
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003588 else
3589 return -EINVAL;
3590
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003591 if (size > valuesize)
3592 return -EINVAL;
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003593
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003594 outsize = 0;
3595 x = 0;
3596 if (!r) {
3597 while (size) {
3598 r = put_user(values[x++], (int32_t *)buf);
3599 buf += 4;
3600 size -= 4;
3601 outsize += 4;
3602 }
3603 }
3604
3605 return !r ? outsize : r;
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003606}
Tom St Denis1e051412016-06-27 09:57:18 -04003607
Tom St Denis273d7aa2016-10-11 14:48:55 -04003608static ssize_t amdgpu_debugfs_wave_read(struct file *f, char __user *buf,
3609 size_t size, loff_t *pos)
3610{
3611 struct amdgpu_device *adev = f->f_inode->i_private;
3612 int r, x;
3613 ssize_t result=0;
Tom St Denis472259f2016-10-14 09:49:09 -04003614 uint32_t offset, se, sh, cu, wave, simd, data[32];
Tom St Denis273d7aa2016-10-11 14:48:55 -04003615
3616 if (size & 3 || *pos & 3)
3617 return -EINVAL;
3618
3619 /* decode offset */
3620 offset = (*pos & 0x7F);
3621 se = ((*pos >> 7) & 0xFF);
3622 sh = ((*pos >> 15) & 0xFF);
3623 cu = ((*pos >> 23) & 0xFF);
3624 wave = ((*pos >> 31) & 0xFF);
3625 simd = ((*pos >> 37) & 0xFF);
Tom St Denis273d7aa2016-10-11 14:48:55 -04003626
3627 /* switch to the specific se/sh/cu */
3628 mutex_lock(&adev->grbm_idx_mutex);
3629 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
3630
3631 x = 0;
Tom St Denis472259f2016-10-14 09:49:09 -04003632 if (adev->gfx.funcs->read_wave_data)
3633 adev->gfx.funcs->read_wave_data(adev, simd, wave, data, &x);
Tom St Denis273d7aa2016-10-11 14:48:55 -04003634
3635 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
3636 mutex_unlock(&adev->grbm_idx_mutex);
3637
Tom St Denis5ecfb3b2016-10-13 12:15:03 -04003638 if (!x)
3639 return -EINVAL;
3640
Tom St Denis472259f2016-10-14 09:49:09 -04003641 while (size && (offset < x * 4)) {
Tom St Denis273d7aa2016-10-11 14:48:55 -04003642 uint32_t value;
3643
Tom St Denis472259f2016-10-14 09:49:09 -04003644 value = data[offset >> 2];
Tom St Denis273d7aa2016-10-11 14:48:55 -04003645 r = put_user(value, (uint32_t *)buf);
3646 if (r)
3647 return r;
3648
3649 result += 4;
3650 buf += 4;
Tom St Denis472259f2016-10-14 09:49:09 -04003651 offset += 4;
Tom St Denis273d7aa2016-10-11 14:48:55 -04003652 size -= 4;
3653 }
3654
3655 return result;
3656}
3657
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003658static ssize_t amdgpu_debugfs_gpr_read(struct file *f, char __user *buf,
3659 size_t size, loff_t *pos)
3660{
3661 struct amdgpu_device *adev = f->f_inode->i_private;
3662 int r;
3663 ssize_t result = 0;
3664 uint32_t offset, se, sh, cu, wave, simd, thread, bank, *data;
3665
3666 if (size & 3 || *pos & 3)
3667 return -EINVAL;
3668
3669 /* decode offset */
3670 offset = (*pos & 0xFFF); /* in dwords */
3671 se = ((*pos >> 12) & 0xFF);
3672 sh = ((*pos >> 20) & 0xFF);
3673 cu = ((*pos >> 28) & 0xFF);
3674 wave = ((*pos >> 36) & 0xFF);
3675 simd = ((*pos >> 44) & 0xFF);
3676 thread = ((*pos >> 52) & 0xFF);
3677 bank = ((*pos >> 60) & 1);
3678
3679 data = kmalloc_array(1024, sizeof(*data), GFP_KERNEL);
3680 if (!data)
3681 return -ENOMEM;
3682
3683 /* switch to the specific se/sh/cu */
3684 mutex_lock(&adev->grbm_idx_mutex);
3685 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
3686
3687 if (bank == 0) {
3688 if (adev->gfx.funcs->read_wave_vgprs)
3689 adev->gfx.funcs->read_wave_vgprs(adev, simd, wave, thread, offset, size>>2, data);
3690 } else {
3691 if (adev->gfx.funcs->read_wave_sgprs)
3692 adev->gfx.funcs->read_wave_sgprs(adev, simd, wave, offset, size>>2, data);
3693 }
3694
3695 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
3696 mutex_unlock(&adev->grbm_idx_mutex);
3697
3698 while (size) {
3699 uint32_t value;
3700
3701 value = data[offset++];
3702 r = put_user(value, (uint32_t *)buf);
3703 if (r) {
3704 result = r;
3705 goto err;
3706 }
3707
3708 result += 4;
3709 buf += 4;
3710 size -= 4;
3711 }
3712
3713err:
3714 kfree(data);
3715 return result;
3716}
3717
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003718static const struct file_operations amdgpu_debugfs_regs_fops = {
3719 .owner = THIS_MODULE,
3720 .read = amdgpu_debugfs_regs_read,
3721 .write = amdgpu_debugfs_regs_write,
3722 .llseek = default_llseek
3723};
Tom St Denisadcec282016-04-15 13:08:44 -04003724static const struct file_operations amdgpu_debugfs_regs_didt_fops = {
3725 .owner = THIS_MODULE,
3726 .read = amdgpu_debugfs_regs_didt_read,
3727 .write = amdgpu_debugfs_regs_didt_write,
3728 .llseek = default_llseek
3729};
3730static const struct file_operations amdgpu_debugfs_regs_pcie_fops = {
3731 .owner = THIS_MODULE,
3732 .read = amdgpu_debugfs_regs_pcie_read,
3733 .write = amdgpu_debugfs_regs_pcie_write,
3734 .llseek = default_llseek
3735};
3736static const struct file_operations amdgpu_debugfs_regs_smc_fops = {
3737 .owner = THIS_MODULE,
3738 .read = amdgpu_debugfs_regs_smc_read,
3739 .write = amdgpu_debugfs_regs_smc_write,
3740 .llseek = default_llseek
3741};
3742
Tom St Denis1e051412016-06-27 09:57:18 -04003743static const struct file_operations amdgpu_debugfs_gca_config_fops = {
3744 .owner = THIS_MODULE,
3745 .read = amdgpu_debugfs_gca_config_read,
3746 .llseek = default_llseek
3747};
3748
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003749static const struct file_operations amdgpu_debugfs_sensors_fops = {
3750 .owner = THIS_MODULE,
3751 .read = amdgpu_debugfs_sensor_read,
3752 .llseek = default_llseek
3753};
3754
Tom St Denis273d7aa2016-10-11 14:48:55 -04003755static const struct file_operations amdgpu_debugfs_wave_fops = {
3756 .owner = THIS_MODULE,
3757 .read = amdgpu_debugfs_wave_read,
3758 .llseek = default_llseek
3759};
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003760static const struct file_operations amdgpu_debugfs_gpr_fops = {
3761 .owner = THIS_MODULE,
3762 .read = amdgpu_debugfs_gpr_read,
3763 .llseek = default_llseek
3764};
Tom St Denis273d7aa2016-10-11 14:48:55 -04003765
Tom St Denisadcec282016-04-15 13:08:44 -04003766static const struct file_operations *debugfs_regs[] = {
3767 &amdgpu_debugfs_regs_fops,
3768 &amdgpu_debugfs_regs_didt_fops,
3769 &amdgpu_debugfs_regs_pcie_fops,
3770 &amdgpu_debugfs_regs_smc_fops,
Tom St Denis1e051412016-06-27 09:57:18 -04003771 &amdgpu_debugfs_gca_config_fops,
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003772 &amdgpu_debugfs_sensors_fops,
Tom St Denis273d7aa2016-10-11 14:48:55 -04003773 &amdgpu_debugfs_wave_fops,
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003774 &amdgpu_debugfs_gpr_fops,
Tom St Denisadcec282016-04-15 13:08:44 -04003775};
3776
3777static const char *debugfs_regs_names[] = {
3778 "amdgpu_regs",
3779 "amdgpu_regs_didt",
3780 "amdgpu_regs_pcie",
3781 "amdgpu_regs_smc",
Tom St Denis1e051412016-06-27 09:57:18 -04003782 "amdgpu_gca_config",
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003783 "amdgpu_sensors",
Tom St Denis273d7aa2016-10-11 14:48:55 -04003784 "amdgpu_wave",
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003785 "amdgpu_gpr",
Tom St Denisadcec282016-04-15 13:08:44 -04003786};
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003787
3788static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
3789{
3790 struct drm_minor *minor = adev->ddev->primary;
3791 struct dentry *ent, *root = minor->debugfs_root;
Tom St Denisadcec282016-04-15 13:08:44 -04003792 unsigned i, j;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003793
Tom St Denisadcec282016-04-15 13:08:44 -04003794 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3795 ent = debugfs_create_file(debugfs_regs_names[i],
3796 S_IFREG | S_IRUGO, root,
3797 adev, debugfs_regs[i]);
3798 if (IS_ERR(ent)) {
3799 for (j = 0; j < i; j++) {
3800 debugfs_remove(adev->debugfs_regs[i]);
3801 adev->debugfs_regs[i] = NULL;
3802 }
3803 return PTR_ERR(ent);
3804 }
3805
3806 if (!i)
3807 i_size_write(ent->d_inode, adev->rmmio_size);
3808 adev->debugfs_regs[i] = ent;
3809 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003810
3811 return 0;
3812}
3813
3814static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev)
3815{
Tom St Denisadcec282016-04-15 13:08:44 -04003816 unsigned i;
3817
3818 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3819 if (adev->debugfs_regs[i]) {
3820 debugfs_remove(adev->debugfs_regs[i]);
3821 adev->debugfs_regs[i] = NULL;
3822 }
3823 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003824}
3825
Huang Rui4f0955f2017-05-10 23:04:06 +08003826static int amdgpu_debugfs_test_ib(struct seq_file *m, void *data)
3827{
3828 struct drm_info_node *node = (struct drm_info_node *) m->private;
3829 struct drm_device *dev = node->minor->dev;
3830 struct amdgpu_device *adev = dev->dev_private;
3831 int r = 0, i;
3832
3833 /* hold on the scheduler */
3834 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
3835 struct amdgpu_ring *ring = adev->rings[i];
3836
3837 if (!ring || !ring->sched.thread)
3838 continue;
3839 kthread_park(ring->sched.thread);
3840 }
3841
3842 seq_printf(m, "run ib test:\n");
3843 r = amdgpu_ib_ring_tests(adev);
3844 if (r)
3845 seq_printf(m, "ib ring tests failed (%d).\n", r);
3846 else
3847 seq_printf(m, "ib ring tests passed.\n");
3848
3849 /* go on the scheduler */
3850 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
3851 struct amdgpu_ring *ring = adev->rings[i];
3852
3853 if (!ring || !ring->sched.thread)
3854 continue;
3855 kthread_unpark(ring->sched.thread);
3856 }
3857
3858 return 0;
3859}
3860
3861static const struct drm_info_list amdgpu_debugfs_test_ib_ring_list[] = {
3862 {"amdgpu_test_ib", &amdgpu_debugfs_test_ib}
3863};
3864
3865static int amdgpu_debugfs_test_ib_ring_init(struct amdgpu_device *adev)
3866{
3867 return amdgpu_debugfs_add_files(adev,
3868 amdgpu_debugfs_test_ib_ring_list, 1);
3869}
3870
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003871int amdgpu_debugfs_init(struct drm_minor *minor)
3872{
3873 return 0;
3874}
Kent Russelldb95e212017-08-22 12:31:43 -04003875
3876static int amdgpu_debugfs_get_vbios_dump(struct seq_file *m, void *data)
3877{
3878 struct drm_info_node *node = (struct drm_info_node *) m->private;
3879 struct drm_device *dev = node->minor->dev;
3880 struct amdgpu_device *adev = dev->dev_private;
3881
3882 seq_write(m, adev->bios, adev->bios_size);
3883 return 0;
3884}
3885
Kent Russelldb95e212017-08-22 12:31:43 -04003886static const struct drm_info_list amdgpu_vbios_dump_list[] = {
3887 {"amdgpu_vbios",
3888 amdgpu_debugfs_get_vbios_dump,
3889 0, NULL},
3890};
3891
Kent Russelldb95e212017-08-22 12:31:43 -04003892static int amdgpu_debugfs_vbios_dump_init(struct amdgpu_device *adev)
3893{
3894 return amdgpu_debugfs_add_files(adev,
3895 amdgpu_vbios_dump_list, 1);
3896}
Alexander Kuleshov7cebc722015-06-27 13:16:05 +06003897#else
Arnd Bergmann27bad5b2017-06-21 23:51:02 +02003898static int amdgpu_debugfs_test_ib_ring_init(struct amdgpu_device *adev)
Huang Rui4f0955f2017-05-10 23:04:06 +08003899{
3900 return 0;
3901}
Alexander Kuleshov7cebc722015-06-27 13:16:05 +06003902static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
3903{
3904 return 0;
3905}
Kent Russelldb95e212017-08-22 12:31:43 -04003906static int amdgpu_debugfs_vbios_dump_init(struct amdgpu_device *adev)
3907{
3908 return 0;
3909}
Alexander Kuleshov7cebc722015-06-27 13:16:05 +06003910static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev) { }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003911#endif