blob: 96955c9b4cdf834e63b3ea470d84a5cd5656b07d [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>
Harry Wentland45622362017-09-12 15:58:20 -040034#include <drm/drm_atomic_helper.h>
Alex Deucherd38ceaf2015-04-20 16:55:21 -040035#include <drm/amdgpu_drm.h>
36#include <linux/vgaarb.h>
37#include <linux/vga_switcheroo.h>
38#include <linux/efi.h>
39#include "amdgpu.h"
Tom St Denisf4b373f2016-05-31 08:02:27 -040040#include "amdgpu_trace.h"
Alex Deucherd38ceaf2015-04-20 16:55:21 -040041#include "amdgpu_i2c.h"
42#include "atom.h"
43#include "amdgpu_atombios.h"
Alex Deuchera5bde2f2016-09-23 16:23:41 -040044#include "amdgpu_atomfirmware.h"
Alex Deucherd0dd7f02015-11-11 19:45:06 -050045#include "amd_pcie.h"
Ken Wang33f34802016-01-21 17:29:41 +080046#ifdef CONFIG_DRM_AMDGPU_SI
47#include "si.h"
48#endif
Alex Deuchera2e73f52015-04-20 17:09:27 -040049#ifdef CONFIG_DRM_AMDGPU_CIK
50#include "cik.h"
51#endif
Alex Deucheraaa36a92015-04-20 17:31:14 -040052#include "vi.h"
Ken Wang460826e2017-03-06 14:53:16 -050053#include "soc15.h"
Alex Deucherd38ceaf2015-04-20 16:55:21 -040054#include "bif/bif_4_1_d.h"
Emily Deng9accf2f2016-08-10 16:01:25 +080055#include <linux/pci.h>
Monk Liubec86372016-09-14 19:38:08 +080056#include <linux/firmware.h>
Gavin Wan89041942017-06-23 13:55:15 -040057#include "amdgpu_vf_error.h"
Alex Deucherd38ceaf2015-04-20 16:55:21 -040058
Yong Zhaoba997702015-11-09 17:21:45 -050059#include "amdgpu_amdkfd.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
Monk Liu15d72fd2017-01-25 15:07:40 +0800112 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev)) {
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800113 BUG_ON(in_interrupt());
114 return amdgpu_virt_kiq_rreg(adev, reg);
115 }
116
Monk Liu15d72fd2017-01-25 15:07:40 +0800117 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
Tom St Denisf4b373f2016-05-31 08:02:27 -0400118 ret = readl(((void __iomem *)adev->rmmio) + (reg * 4));
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400119 else {
120 unsigned long flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400121
122 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
123 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
124 ret = readl(((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
125 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400126 }
Tom St Denisf4b373f2016-05-31 08:02:27 -0400127 trace_amdgpu_mm_rreg(adev->pdev->device, reg, ret);
128 return ret;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400129}
130
131void amdgpu_mm_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v,
Monk Liu15d72fd2017-01-25 15:07:40 +0800132 uint32_t acc_flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400133{
Tom St Denisf4b373f2016-05-31 08:02:27 -0400134 trace_amdgpu_mm_wreg(adev->pdev->device, reg, v);
Monk Liu4e99a442016-03-31 13:26:59 +0800135
Ken Wang47ed4e12017-07-04 13:11:52 +0800136 if (adev->asic_type >= CHIP_VEGA10 && reg == 0) {
137 adev->last_mm_index = v;
138 }
139
Monk Liu15d72fd2017-01-25 15:07:40 +0800140 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev)) {
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800141 BUG_ON(in_interrupt());
142 return amdgpu_virt_kiq_wreg(adev, reg, v);
143 }
144
Monk Liu15d72fd2017-01-25 15:07:40 +0800145 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400146 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
147 else {
148 unsigned long flags;
149
150 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
151 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
152 writel(v, ((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
153 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
154 }
Ken Wang47ed4e12017-07-04 13:11:52 +0800155
156 if (adev->asic_type >= CHIP_VEGA10 && reg == 1 && adev->last_mm_index == 0x5702C) {
157 udelay(500);
158 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400159}
160
161u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 reg)
162{
163 if ((reg * 4) < adev->rio_mem_size)
164 return ioread32(adev->rio_mem + (reg * 4));
165 else {
166 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
167 return ioread32(adev->rio_mem + (mmMM_DATA * 4));
168 }
169}
170
171void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
172{
Ken Wang47ed4e12017-07-04 13:11:52 +0800173 if (adev->asic_type >= CHIP_VEGA10 && reg == 0) {
174 adev->last_mm_index = v;
175 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400176
177 if ((reg * 4) < adev->rio_mem_size)
178 iowrite32(v, adev->rio_mem + (reg * 4));
179 else {
180 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
181 iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
182 }
Ken Wang47ed4e12017-07-04 13:11:52 +0800183
184 if (adev->asic_type >= CHIP_VEGA10 && reg == 1 && adev->last_mm_index == 0x5702C) {
185 udelay(500);
186 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400187}
188
189/**
190 * amdgpu_mm_rdoorbell - read a doorbell dword
191 *
192 * @adev: amdgpu_device pointer
193 * @index: doorbell index
194 *
195 * Returns the value in the doorbell aperture at the
196 * requested doorbell index (CIK).
197 */
198u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
199{
200 if (index < adev->doorbell.num_doorbells) {
201 return readl(adev->doorbell.ptr + index);
202 } else {
203 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
204 return 0;
205 }
206}
207
208/**
209 * amdgpu_mm_wdoorbell - write a doorbell dword
210 *
211 * @adev: amdgpu_device pointer
212 * @index: doorbell index
213 * @v: value to write
214 *
215 * Writes @v to the doorbell aperture at the
216 * requested doorbell index (CIK).
217 */
218void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
219{
220 if (index < adev->doorbell.num_doorbells) {
221 writel(v, adev->doorbell.ptr + index);
222 } else {
223 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
224 }
225}
226
227/**
Ken Wang832be402016-03-18 15:23:08 +0800228 * amdgpu_mm_rdoorbell64 - read a doorbell Qword
229 *
230 * @adev: amdgpu_device pointer
231 * @index: doorbell index
232 *
233 * Returns the value in the doorbell aperture at the
234 * requested doorbell index (VEGA10+).
235 */
236u64 amdgpu_mm_rdoorbell64(struct amdgpu_device *adev, u32 index)
237{
238 if (index < adev->doorbell.num_doorbells) {
239 return atomic64_read((atomic64_t *)(adev->doorbell.ptr + index));
240 } else {
241 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
242 return 0;
243 }
244}
245
246/**
247 * amdgpu_mm_wdoorbell64 - write a doorbell Qword
248 *
249 * @adev: amdgpu_device pointer
250 * @index: doorbell index
251 * @v: value to write
252 *
253 * Writes @v to the doorbell aperture at the
254 * requested doorbell index (VEGA10+).
255 */
256void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v)
257{
258 if (index < adev->doorbell.num_doorbells) {
259 atomic64_set((atomic64_t *)(adev->doorbell.ptr + index), v);
260 } else {
261 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
262 }
263}
264
265/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400266 * amdgpu_invalid_rreg - dummy reg read function
267 *
268 * @adev: amdgpu device pointer
269 * @reg: offset of register
270 *
271 * Dummy register read function. Used for register blocks
272 * that certain asics don't have (all asics).
273 * Returns the value in the register.
274 */
275static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
276{
277 DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
278 BUG();
279 return 0;
280}
281
282/**
283 * amdgpu_invalid_wreg - dummy reg write function
284 *
285 * @adev: amdgpu device pointer
286 * @reg: offset of register
287 * @v: value to write to the register
288 *
289 * Dummy register read function. Used for register blocks
290 * that certain asics don't have (all asics).
291 */
292static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
293{
294 DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
295 reg, v);
296 BUG();
297}
298
299/**
300 * amdgpu_block_invalid_rreg - dummy reg read function
301 *
302 * @adev: amdgpu device pointer
303 * @block: offset of instance
304 * @reg: offset of register
305 *
306 * Dummy register read function. Used for register blocks
307 * that certain asics don't have (all asics).
308 * Returns the value in the register.
309 */
310static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
311 uint32_t block, uint32_t reg)
312{
313 DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
314 reg, block);
315 BUG();
316 return 0;
317}
318
319/**
320 * amdgpu_block_invalid_wreg - dummy reg write function
321 *
322 * @adev: amdgpu device pointer
323 * @block: offset of instance
324 * @reg: offset of register
325 * @v: value to write to the register
326 *
327 * Dummy register read function. Used for register blocks
328 * that certain asics don't have (all asics).
329 */
330static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
331 uint32_t block,
332 uint32_t reg, uint32_t v)
333{
334 DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
335 reg, block, v);
336 BUG();
337}
338
339static int amdgpu_vram_scratch_init(struct amdgpu_device *adev)
340{
Christian Königa4a02772017-07-27 17:24:36 +0200341 return amdgpu_bo_create_kernel(adev, AMDGPU_GPU_PAGE_SIZE,
342 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
343 &adev->vram_scratch.robj,
344 &adev->vram_scratch.gpu_addr,
345 (void **)&adev->vram_scratch.ptr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400346}
347
348static void amdgpu_vram_scratch_fini(struct amdgpu_device *adev)
349{
Christian König078af1a2017-07-27 17:43:00 +0200350 amdgpu_bo_free_kernel(&adev->vram_scratch.robj, NULL, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400351}
352
353/**
354 * amdgpu_program_register_sequence - program an array of registers.
355 *
356 * @adev: amdgpu_device pointer
357 * @registers: pointer to the register array
358 * @array_size: size of the register array
359 *
360 * Programs an array or registers with and and or masks.
361 * This is a helper for setting golden registers.
362 */
363void amdgpu_program_register_sequence(struct amdgpu_device *adev,
364 const u32 *registers,
365 const u32 array_size)
366{
367 u32 tmp, reg, and_mask, or_mask;
368 int i;
369
370 if (array_size % 3)
371 return;
372
373 for (i = 0; i < array_size; i +=3) {
374 reg = registers[i + 0];
375 and_mask = registers[i + 1];
376 or_mask = registers[i + 2];
377
378 if (and_mask == 0xffffffff) {
379 tmp = or_mask;
380 } else {
381 tmp = RREG32(reg);
382 tmp &= ~and_mask;
383 tmp |= or_mask;
384 }
385 WREG32(reg, tmp);
386 }
387}
388
389void amdgpu_pci_config_reset(struct amdgpu_device *adev)
390{
391 pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
392}
393
394/*
395 * GPU doorbell aperture helpers function.
396 */
397/**
398 * amdgpu_doorbell_init - Init doorbell driver information.
399 *
400 * @adev: amdgpu_device pointer
401 *
402 * Init doorbell driver information (CIK)
403 * Returns 0 on success, error on failure.
404 */
405static int amdgpu_doorbell_init(struct amdgpu_device *adev)
406{
Christian König705e5192017-06-08 11:15:16 +0200407 /* No doorbell on SI hardware generation */
408 if (adev->asic_type < CHIP_BONAIRE) {
409 adev->doorbell.base = 0;
410 adev->doorbell.size = 0;
411 adev->doorbell.num_doorbells = 0;
412 adev->doorbell.ptr = NULL;
413 return 0;
414 }
415
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400416 /* doorbell bar mapping */
417 adev->doorbell.base = pci_resource_start(adev->pdev, 2);
418 adev->doorbell.size = pci_resource_len(adev->pdev, 2);
419
Christian Königedf600d2016-05-03 15:54:54 +0200420 adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400421 AMDGPU_DOORBELL_MAX_ASSIGNMENT+1);
422 if (adev->doorbell.num_doorbells == 0)
423 return -EINVAL;
424
Christian König8972e5d2017-03-06 13:34:57 +0100425 adev->doorbell.ptr = ioremap(adev->doorbell.base,
426 adev->doorbell.num_doorbells *
427 sizeof(u32));
428 if (adev->doorbell.ptr == NULL)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400429 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400430
431 return 0;
432}
433
434/**
435 * amdgpu_doorbell_fini - Tear down doorbell driver information.
436 *
437 * @adev: amdgpu_device pointer
438 *
439 * Tear down doorbell driver information (CIK)
440 */
441static void amdgpu_doorbell_fini(struct amdgpu_device *adev)
442{
443 iounmap(adev->doorbell.ptr);
444 adev->doorbell.ptr = NULL;
445}
446
447/**
448 * amdgpu_doorbell_get_kfd_info - Report doorbell configuration required to
449 * setup amdkfd
450 *
451 * @adev: amdgpu_device pointer
452 * @aperture_base: output returning doorbell aperture base physical address
453 * @aperture_size: output returning doorbell aperture size in bytes
454 * @start_offset: output returning # of doorbell bytes reserved for amdgpu.
455 *
456 * amdgpu and amdkfd share the doorbell aperture. amdgpu sets it up,
457 * takes doorbells required for its own rings and reports the setup to amdkfd.
458 * amdgpu reserved doorbells are at the start of the doorbell aperture.
459 */
460void amdgpu_doorbell_get_kfd_info(struct amdgpu_device *adev,
461 phys_addr_t *aperture_base,
462 size_t *aperture_size,
463 size_t *start_offset)
464{
465 /*
466 * The first num_doorbells are used by amdgpu.
467 * amdkfd takes whatever's left in the aperture.
468 */
469 if (adev->doorbell.size > adev->doorbell.num_doorbells * sizeof(u32)) {
470 *aperture_base = adev->doorbell.base;
471 *aperture_size = adev->doorbell.size;
472 *start_offset = adev->doorbell.num_doorbells * sizeof(u32);
473 } else {
474 *aperture_base = 0;
475 *aperture_size = 0;
476 *start_offset = 0;
477 }
478}
479
480/*
481 * amdgpu_wb_*()
Alex Xie455a7bc2017-05-08 21:36:03 -0400482 * Writeback is the method by which the GPU updates special pages in memory
Alex Xieea81a172017-05-08 13:41:11 -0400483 * with the status of certain GPU events (fences, ring pointers,etc.).
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400484 */
485
486/**
487 * amdgpu_wb_fini - Disable Writeback and free memory
488 *
489 * @adev: amdgpu_device pointer
490 *
491 * Disables Writeback and frees the Writeback memory (all asics).
492 * Used at driver shutdown.
493 */
494static void amdgpu_wb_fini(struct amdgpu_device *adev)
495{
496 if (adev->wb.wb_obj) {
Alex Deuchera76ed482016-10-21 15:30:36 -0400497 amdgpu_bo_free_kernel(&adev->wb.wb_obj,
498 &adev->wb.gpu_addr,
499 (void **)&adev->wb.wb);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400500 adev->wb.wb_obj = NULL;
501 }
502}
503
504/**
505 * amdgpu_wb_init- Init Writeback driver info and allocate memory
506 *
507 * @adev: amdgpu_device pointer
508 *
Alex Xie455a7bc2017-05-08 21:36:03 -0400509 * Initializes writeback and allocates writeback memory (all asics).
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400510 * Used at driver startup.
511 * Returns 0 on success or an -error on failure.
512 */
513static int amdgpu_wb_init(struct amdgpu_device *adev)
514{
515 int r;
516
517 if (adev->wb.wb_obj == NULL) {
Alex Deucher97407b62017-07-28 12:14:15 -0400518 /* AMDGPU_MAX_WB * sizeof(uint32_t) * 8 = AMDGPU_MAX_WB 256bit slots */
519 r = amdgpu_bo_create_kernel(adev, AMDGPU_MAX_WB * sizeof(uint32_t) * 8,
Alex Deuchera76ed482016-10-21 15:30:36 -0400520 PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
521 &adev->wb.wb_obj, &adev->wb.gpu_addr,
522 (void **)&adev->wb.wb);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400523 if (r) {
524 dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
525 return r;
526 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400527
528 adev->wb.num_wb = AMDGPU_MAX_WB;
529 memset(&adev->wb.used, 0, sizeof(adev->wb.used));
530
531 /* clear wb memory */
Huang Rui60a970a62017-03-15 10:13:32 +0800532 memset((char *)adev->wb.wb, 0, AMDGPU_MAX_WB * sizeof(uint32_t));
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400533 }
534
535 return 0;
536}
537
538/**
539 * amdgpu_wb_get - Allocate a wb entry
540 *
541 * @adev: amdgpu_device pointer
542 * @wb: wb index
543 *
544 * Allocate a wb slot for use by the driver (all asics).
545 * Returns 0 on success or -EINVAL on failure.
546 */
547int amdgpu_wb_get(struct amdgpu_device *adev, u32 *wb)
548{
549 unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
Alex Deucher97407b62017-07-28 12:14:15 -0400550
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400551 if (offset < adev->wb.num_wb) {
552 __set_bit(offset, adev->wb.used);
Alex Deucher97407b62017-07-28 12:14:15 -0400553 *wb = offset * 8; /* convert to dw offset */
Monk Liu0915fdb2017-06-19 10:19:41 -0400554 return 0;
555 } else {
556 return -EINVAL;
557 }
558}
559
Ken Wang70142852016-03-18 15:08:49 +0800560/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400561 * amdgpu_wb_free - Free a wb entry
562 *
563 * @adev: amdgpu_device pointer
564 * @wb: wb index
565 *
566 * Free a wb slot allocated for use by the driver (all asics)
567 */
568void amdgpu_wb_free(struct amdgpu_device *adev, u32 wb)
569{
570 if (wb < adev->wb.num_wb)
571 __clear_bit(wb, adev->wb.used);
572}
573
574/**
575 * amdgpu_vram_location - try to find VRAM location
576 * @adev: amdgpu device structure holding all necessary informations
577 * @mc: memory controller structure holding memory informations
578 * @base: base address at which to put VRAM
579 *
Alex Xie455a7bc2017-05-08 21:36:03 -0400580 * Function will try to place VRAM at base address provided
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400581 * as parameter (which is so far either PCI aperture address or
582 * for IGP TOM base address).
583 *
584 * If there is not enough space to fit the unvisible VRAM in the 32bits
585 * address space then we limit the VRAM size to the aperture.
586 *
587 * Note: We don't explicitly enforce VRAM start to be aligned on VRAM size,
588 * this shouldn't be a problem as we are using the PCI aperture as a reference.
589 * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but
590 * not IGP.
591 *
592 * Note: we use mc_vram_size as on some board we need to program the mc to
593 * cover the whole aperture even if VRAM size is inferior to aperture size
594 * Novell bug 204882 + along with lots of ubuntu ones
595 *
596 * Note: when limiting vram it's safe to overwritte real_vram_size because
597 * we are not in case where real_vram_size is inferior to mc_vram_size (ie
598 * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu
599 * ones)
600 *
601 * Note: IGP TOM addr should be the same as the aperture addr, we don't
Alex Xie455a7bc2017-05-08 21:36:03 -0400602 * explicitly check for that though.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400603 *
604 * FIXME: when reducing VRAM size align new size on power of 2.
605 */
606void amdgpu_vram_location(struct amdgpu_device *adev, struct amdgpu_mc *mc, u64 base)
607{
608 uint64_t limit = (uint64_t)amdgpu_vram_limit << 20;
609
610 mc->vram_start = base;
611 if (mc->mc_vram_size > (adev->mc.mc_mask - base + 1)) {
612 dev_warn(adev->dev, "limiting VRAM to PCI aperture size\n");
613 mc->real_vram_size = mc->aper_size;
614 mc->mc_vram_size = mc->aper_size;
615 }
616 mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
617 if (limit && limit < mc->real_vram_size)
618 mc->real_vram_size = limit;
619 dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
620 mc->mc_vram_size >> 20, mc->vram_start,
621 mc->vram_end, mc->real_vram_size >> 20);
622}
623
624/**
Christian König6f02a692017-07-07 11:56:59 +0200625 * amdgpu_gart_location - try to find GTT location
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400626 * @adev: amdgpu device structure holding all necessary informations
627 * @mc: memory controller structure holding memory informations
628 *
629 * Function will place try to place GTT before or after VRAM.
630 *
631 * If GTT size is bigger than space left then we ajust GTT size.
632 * Thus function will never fails.
633 *
634 * FIXME: when reducing GTT size align new size on power of 2.
635 */
Christian König6f02a692017-07-07 11:56:59 +0200636void amdgpu_gart_location(struct amdgpu_device *adev, struct amdgpu_mc *mc)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400637{
638 u64 size_af, size_bf;
639
Christian Königed21c042017-07-06 22:26:05 +0200640 size_af = adev->mc.mc_mask - mc->vram_end;
641 size_bf = mc->vram_start;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400642 if (size_bf > size_af) {
Christian König6f02a692017-07-07 11:56:59 +0200643 if (mc->gart_size > size_bf) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400644 dev_warn(adev->dev, "limiting GTT\n");
Christian König6f02a692017-07-07 11:56:59 +0200645 mc->gart_size = size_bf;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400646 }
Christian König6f02a692017-07-07 11:56:59 +0200647 mc->gart_start = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400648 } else {
Christian König6f02a692017-07-07 11:56:59 +0200649 if (mc->gart_size > size_af) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400650 dev_warn(adev->dev, "limiting GTT\n");
Christian König6f02a692017-07-07 11:56:59 +0200651 mc->gart_size = size_af;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400652 }
Christian König6f02a692017-07-07 11:56:59 +0200653 mc->gart_start = mc->vram_end + 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400654 }
Christian König6f02a692017-07-07 11:56:59 +0200655 mc->gart_end = mc->gart_start + mc->gart_size - 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400656 dev_info(adev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
Christian König6f02a692017-07-07 11:56:59 +0200657 mc->gart_size >> 20, mc->gart_start, mc->gart_end);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400658}
659
660/*
661 * GPU helpers function.
662 */
663/**
Jim Quc836fec2017-02-10 15:59:59 +0800664 * amdgpu_need_post - check if the hw need post or not
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400665 *
666 * @adev: amdgpu_device pointer
667 *
Jim Quc836fec2017-02-10 15:59:59 +0800668 * Check if the asic has been initialized (all asics) at driver startup
669 * or post is needed if hw reset is performed.
670 * Returns true if need or false if not.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400671 */
Jim Quc836fec2017-02-10 15:59:59 +0800672bool amdgpu_need_post(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400673{
674 uint32_t reg;
675
Jim Quc836fec2017-02-10 15:59:59 +0800676 if (adev->has_hw_reset) {
677 adev->has_hw_reset = false;
678 return true;
679 }
Alex Deucher70d17a22017-06-30 17:26:47 -0400680
681 /* bios scratch used on CIK+ */
682 if (adev->asic_type >= CHIP_BONAIRE)
683 return amdgpu_atombios_scratch_need_asic_init(adev);
684
685 /* check MEM_SIZE for older asics */
Alex Deucherbbf282d2017-03-03 17:26:10 -0500686 reg = amdgpu_asic_get_config_memsize(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400687
Alex Deucherf2713e82017-03-28 12:19:31 -0400688 if ((reg != 0) && (reg != 0xffffffff))
Jim Quc836fec2017-02-10 15:59:59 +0800689 return false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400690
Jim Quc836fec2017-02-10 15:59:59 +0800691 return true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400692
693}
694
Monk Liubec86372016-09-14 19:38:08 +0800695static bool amdgpu_vpost_needed(struct amdgpu_device *adev)
696{
697 if (amdgpu_sriov_vf(adev))
698 return false;
699
700 if (amdgpu_passthrough(adev)) {
Monk Liu1da2c322016-11-11 11:24:29 +0800701 /* for FIJI: In whole GPU pass-through virtualization case, after VM reboot
702 * some old smc fw still need driver do vPost otherwise gpu hang, while
703 * those smc fw version above 22.15 doesn't have this flaw, so we force
704 * vpost executed for smc version below 22.15
Monk Liubec86372016-09-14 19:38:08 +0800705 */
706 if (adev->asic_type == CHIP_FIJI) {
707 int err;
708 uint32_t fw_ver;
709 err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
710 /* force vPost if error occured */
711 if (err)
712 return true;
713
714 fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
Monk Liu1da2c322016-11-11 11:24:29 +0800715 if (fw_ver < 0x00160e00)
716 return true;
Monk Liubec86372016-09-14 19:38:08 +0800717 }
Monk Liubec86372016-09-14 19:38:08 +0800718 }
Jim Quc836fec2017-02-10 15:59:59 +0800719 return amdgpu_need_post(adev);
Monk Liubec86372016-09-14 19:38:08 +0800720}
721
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400722/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400723 * amdgpu_dummy_page_init - init dummy page used by the driver
724 *
725 * @adev: amdgpu_device pointer
726 *
727 * Allocate the dummy page used by the driver (all asics).
728 * This dummy page is used by the driver as a filler for gart entries
729 * when pages are taken out of the GART
730 * Returns 0 on sucess, -ENOMEM on failure.
731 */
732int amdgpu_dummy_page_init(struct amdgpu_device *adev)
733{
734 if (adev->dummy_page.page)
735 return 0;
736 adev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
737 if (adev->dummy_page.page == NULL)
738 return -ENOMEM;
739 adev->dummy_page.addr = pci_map_page(adev->pdev, adev->dummy_page.page,
740 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
741 if (pci_dma_mapping_error(adev->pdev, adev->dummy_page.addr)) {
742 dev_err(&adev->pdev->dev, "Failed to DMA MAP the dummy page\n");
743 __free_page(adev->dummy_page.page);
744 adev->dummy_page.page = NULL;
745 return -ENOMEM;
746 }
747 return 0;
748}
749
750/**
751 * amdgpu_dummy_page_fini - free dummy page used by the driver
752 *
753 * @adev: amdgpu_device pointer
754 *
755 * Frees the dummy page used by the driver (all asics).
756 */
757void amdgpu_dummy_page_fini(struct amdgpu_device *adev)
758{
759 if (adev->dummy_page.page == NULL)
760 return;
761 pci_unmap_page(adev->pdev, adev->dummy_page.addr,
762 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
763 __free_page(adev->dummy_page.page);
764 adev->dummy_page.page = NULL;
765}
766
767
768/* ATOM accessor methods */
769/*
770 * ATOM is an interpreted byte code stored in tables in the vbios. The
771 * driver registers callbacks to access registers and the interpreter
772 * in the driver parses the tables and executes then to program specific
773 * actions (set display modes, asic init, etc.). See amdgpu_atombios.c,
774 * atombios.h, and atom.c
775 */
776
777/**
778 * cail_pll_read - read PLL register
779 *
780 * @info: atom card_info pointer
781 * @reg: PLL register offset
782 *
783 * Provides a PLL register accessor for the atom interpreter (r4xx+).
784 * Returns the value of the PLL register.
785 */
786static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
787{
788 return 0;
789}
790
791/**
792 * cail_pll_write - write PLL register
793 *
794 * @info: atom card_info pointer
795 * @reg: PLL register offset
796 * @val: value to write to the pll register
797 *
798 * Provides a PLL register accessor for the atom interpreter (r4xx+).
799 */
800static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
801{
802
803}
804
805/**
806 * cail_mc_read - read MC (Memory Controller) register
807 *
808 * @info: atom card_info pointer
809 * @reg: MC register offset
810 *
811 * Provides an MC register accessor for the atom interpreter (r4xx+).
812 * Returns the value of the MC register.
813 */
814static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
815{
816 return 0;
817}
818
819/**
820 * cail_mc_write - write MC (Memory Controller) register
821 *
822 * @info: atom card_info pointer
823 * @reg: MC register offset
824 * @val: value to write to the pll register
825 *
826 * Provides a MC register accessor for the atom interpreter (r4xx+).
827 */
828static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
829{
830
831}
832
833/**
834 * cail_reg_write - write MMIO register
835 *
836 * @info: atom card_info pointer
837 * @reg: MMIO register offset
838 * @val: value to write to the pll register
839 *
840 * Provides a MMIO register accessor for the atom interpreter (r4xx+).
841 */
842static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
843{
844 struct amdgpu_device *adev = info->dev->dev_private;
845
846 WREG32(reg, val);
847}
848
849/**
850 * cail_reg_read - read MMIO register
851 *
852 * @info: atom card_info pointer
853 * @reg: MMIO register offset
854 *
855 * Provides an MMIO register accessor for the atom interpreter (r4xx+).
856 * Returns the value of the MMIO register.
857 */
858static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
859{
860 struct amdgpu_device *adev = info->dev->dev_private;
861 uint32_t r;
862
863 r = RREG32(reg);
864 return r;
865}
866
867/**
868 * cail_ioreg_write - write IO register
869 *
870 * @info: atom card_info pointer
871 * @reg: IO register offset
872 * @val: value to write to the pll register
873 *
874 * Provides a IO register accessor for the atom interpreter (r4xx+).
875 */
876static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
877{
878 struct amdgpu_device *adev = info->dev->dev_private;
879
880 WREG32_IO(reg, val);
881}
882
883/**
884 * cail_ioreg_read - read IO register
885 *
886 * @info: atom card_info pointer
887 * @reg: IO register offset
888 *
889 * Provides an IO register accessor for the atom interpreter (r4xx+).
890 * Returns the value of the IO register.
891 */
892static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
893{
894 struct amdgpu_device *adev = info->dev->dev_private;
895 uint32_t r;
896
897 r = RREG32_IO(reg);
898 return r;
899}
900
Kent Russell5b41d942017-08-22 12:31:43 -0400901static ssize_t amdgpu_atombios_get_vbios_version(struct device *dev,
902 struct device_attribute *attr,
903 char *buf)
904{
905 struct drm_device *ddev = dev_get_drvdata(dev);
906 struct amdgpu_device *adev = ddev->dev_private;
907 struct atom_context *ctx = adev->mode_info.atom_context;
908
909 return snprintf(buf, PAGE_SIZE, "%s\n", ctx->vbios_version);
910}
911
912static DEVICE_ATTR(vbios_version, 0444, amdgpu_atombios_get_vbios_version,
913 NULL);
914
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400915/**
916 * amdgpu_atombios_fini - free the driver info and callbacks for atombios
917 *
918 * @adev: amdgpu_device pointer
919 *
920 * Frees the driver info and register access callbacks for the ATOM
921 * interpreter (r4xx+).
922 * Called at driver shutdown.
923 */
924static void amdgpu_atombios_fini(struct amdgpu_device *adev)
925{
Monk Liu89e0ec9f2016-05-27 19:34:11 +0800926 if (adev->mode_info.atom_context) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400927 kfree(adev->mode_info.atom_context->scratch);
Monk Liu89e0ec9f2016-05-27 19:34:11 +0800928 kfree(adev->mode_info.atom_context->iio);
929 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400930 kfree(adev->mode_info.atom_context);
931 adev->mode_info.atom_context = NULL;
932 kfree(adev->mode_info.atom_card_info);
933 adev->mode_info.atom_card_info = NULL;
Kent Russell5b41d942017-08-22 12:31:43 -0400934 device_remove_file(adev->dev, &dev_attr_vbios_version);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400935}
936
937/**
938 * amdgpu_atombios_init - init the driver info and callbacks for atombios
939 *
940 * @adev: amdgpu_device pointer
941 *
942 * Initializes the driver info and register access callbacks for the
943 * ATOM interpreter (r4xx+).
944 * Returns 0 on sucess, -ENOMEM on failure.
945 * Called at driver startup.
946 */
947static int amdgpu_atombios_init(struct amdgpu_device *adev)
948{
949 struct card_info *atom_card_info =
950 kzalloc(sizeof(struct card_info), GFP_KERNEL);
Kent Russell5b41d942017-08-22 12:31:43 -0400951 int ret;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400952
953 if (!atom_card_info)
954 return -ENOMEM;
955
956 adev->mode_info.atom_card_info = atom_card_info;
957 atom_card_info->dev = adev->ddev;
958 atom_card_info->reg_read = cail_reg_read;
959 atom_card_info->reg_write = cail_reg_write;
960 /* needed for iio ops */
961 if (adev->rio_mem) {
962 atom_card_info->ioreg_read = cail_ioreg_read;
963 atom_card_info->ioreg_write = cail_ioreg_write;
964 } else {
Amber Linb64a18c2017-01-04 08:06:58 -0500965 DRM_INFO("PCI I/O BAR is not found. Using MMIO to access ATOM BIOS\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400966 atom_card_info->ioreg_read = cail_reg_read;
967 atom_card_info->ioreg_write = cail_reg_write;
968 }
969 atom_card_info->mc_read = cail_mc_read;
970 atom_card_info->mc_write = cail_mc_write;
971 atom_card_info->pll_read = cail_pll_read;
972 atom_card_info->pll_write = cail_pll_write;
973
974 adev->mode_info.atom_context = amdgpu_atom_parse(atom_card_info, adev->bios);
975 if (!adev->mode_info.atom_context) {
976 amdgpu_atombios_fini(adev);
977 return -ENOMEM;
978 }
979
980 mutex_init(&adev->mode_info.atom_context->mutex);
Alex Deuchera5bde2f2016-09-23 16:23:41 -0400981 if (adev->is_atom_fw) {
982 amdgpu_atomfirmware_scratch_regs_init(adev);
983 amdgpu_atomfirmware_allocate_fb_scratch(adev);
984 } else {
985 amdgpu_atombios_scratch_regs_init(adev);
986 amdgpu_atombios_allocate_fb_scratch(adev);
987 }
Kent Russell5b41d942017-08-22 12:31:43 -0400988
989 ret = device_create_file(adev->dev, &dev_attr_vbios_version);
990 if (ret) {
991 DRM_ERROR("Failed to create device file for VBIOS version\n");
992 return ret;
993 }
994
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400995 return 0;
996}
997
998/* if we get transitioned to only one device, take VGA back */
999/**
1000 * amdgpu_vga_set_decode - enable/disable vga decode
1001 *
1002 * @cookie: amdgpu_device pointer
1003 * @state: enable/disable vga decode
1004 *
1005 * Enable/disable vga decode (all asics).
1006 * Returns VGA resource flags.
1007 */
1008static unsigned int amdgpu_vga_set_decode(void *cookie, bool state)
1009{
1010 struct amdgpu_device *adev = cookie;
1011 amdgpu_asic_set_vga_state(adev, state);
1012 if (state)
1013 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
1014 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1015 else
1016 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1017}
1018
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001019static void amdgpu_check_block_size(struct amdgpu_device *adev)
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001020{
1021 /* defines number of bits in page table versus page directory,
1022 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
1023 * page table and the remaining bits are in the page directory */
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001024 if (amdgpu_vm_block_size == -1)
1025 return;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001026
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001027 if (amdgpu_vm_block_size < 9) {
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001028 dev_warn(adev->dev, "VM page table size (%d) too small\n",
1029 amdgpu_vm_block_size);
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001030 goto def_value;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001031 }
1032
1033 if (amdgpu_vm_block_size > 24 ||
1034 (amdgpu_vm_size * 1024) < (1ull << amdgpu_vm_block_size)) {
1035 dev_warn(adev->dev, "VM page table size (%d) too large\n",
1036 amdgpu_vm_block_size);
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001037 goto def_value;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001038 }
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001039
1040 return;
1041
1042def_value:
1043 amdgpu_vm_block_size = -1;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001044}
1045
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001046static void amdgpu_check_vm_size(struct amdgpu_device *adev)
1047{
Alex Deucher64dab072017-06-15 18:20:09 -04001048 /* no need to check the default value */
1049 if (amdgpu_vm_size == -1)
1050 return;
1051
Alex Deucher76117502017-06-21 12:31:41 -04001052 if (!is_power_of_2(amdgpu_vm_size)) {
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001053 dev_warn(adev->dev, "VM size (%d) must be a power of 2\n",
1054 amdgpu_vm_size);
1055 goto def_value;
1056 }
1057
1058 if (amdgpu_vm_size < 1) {
1059 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
1060 amdgpu_vm_size);
1061 goto def_value;
1062 }
1063
1064 /*
1065 * Max GPUVM size for Cayman, SI, CI VI are 40 bits.
1066 */
1067 if (amdgpu_vm_size > 1024) {
1068 dev_warn(adev->dev, "VM size (%d) too large, max is 1TB\n",
1069 amdgpu_vm_size);
1070 goto def_value;
1071 }
1072
1073 return;
1074
1075def_value:
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001076 amdgpu_vm_size = -1;
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001077}
1078
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001079/**
1080 * amdgpu_check_arguments - validate module params
1081 *
1082 * @adev: amdgpu_device pointer
1083 *
1084 * Validates certain module parameters and updates
1085 * the associated values used by the driver (all asics).
1086 */
1087static void amdgpu_check_arguments(struct amdgpu_device *adev)
1088{
Chunming Zhou5b011232015-12-10 17:34:33 +08001089 if (amdgpu_sched_jobs < 4) {
1090 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
1091 amdgpu_sched_jobs);
1092 amdgpu_sched_jobs = 4;
Alex Deucher76117502017-06-21 12:31:41 -04001093 } else if (!is_power_of_2(amdgpu_sched_jobs)){
Chunming Zhou5b011232015-12-10 17:34:33 +08001094 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
1095 amdgpu_sched_jobs);
1096 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
1097 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001098
Alex Deucher83e74db2017-08-21 11:58:25 -04001099 if (amdgpu_gart_size != -1 && amdgpu_gart_size < 32) {
Christian Königf9321cc2017-07-07 13:44:05 +02001100 /* gart size must be greater or equal to 32M */
1101 dev_warn(adev->dev, "gart size (%d) too small\n",
1102 amdgpu_gart_size);
Alex Deucher83e74db2017-08-21 11:58:25 -04001103 amdgpu_gart_size = -1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001104 }
1105
Christian König36d38372017-07-07 13:17:45 +02001106 if (amdgpu_gtt_size != -1 && amdgpu_gtt_size < 32) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001107 /* gtt size must be greater or equal to 32M */
Christian König36d38372017-07-07 13:17:45 +02001108 dev_warn(adev->dev, "gtt size (%d) too small\n",
1109 amdgpu_gtt_size);
1110 amdgpu_gtt_size = -1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001111 }
1112
Roger Hed07f14b2017-08-15 16:05:59 +08001113 /* valid range is between 4 and 9 inclusive */
1114 if (amdgpu_vm_fragment_size != -1 &&
1115 (amdgpu_vm_fragment_size > 9 || amdgpu_vm_fragment_size < 4)) {
1116 dev_warn(adev->dev, "valid range is between 4 and 9\n");
1117 amdgpu_vm_fragment_size = -1;
1118 }
1119
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001120 amdgpu_check_vm_size(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001121
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001122 amdgpu_check_block_size(adev);
Christian König6a7f76e2016-08-24 15:51:49 +02001123
jimqu526bae32016-11-07 09:53:10 +08001124 if (amdgpu_vram_page_split != -1 && (amdgpu_vram_page_split < 16 ||
Alex Deucher76117502017-06-21 12:31:41 -04001125 !is_power_of_2(amdgpu_vram_page_split))) {
Christian König6a7f76e2016-08-24 15:51:49 +02001126 dev_warn(adev->dev, "invalid VRAM page split (%d)\n",
1127 amdgpu_vram_page_split);
1128 amdgpu_vram_page_split = 1024;
1129 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001130}
1131
1132/**
1133 * amdgpu_switcheroo_set_state - set switcheroo state
1134 *
1135 * @pdev: pci dev pointer
Lukas Wunner16944672015-09-05 11:17:35 +02001136 * @state: vga_switcheroo state
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001137 *
1138 * Callback for the switcheroo driver. Suspends or resumes the
1139 * the asics before or after it is powered up using ACPI methods.
1140 */
1141static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1142{
1143 struct drm_device *dev = pci_get_drvdata(pdev);
1144
1145 if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
1146 return;
1147
1148 if (state == VGA_SWITCHEROO_ON) {
Joe Perches7ca85292017-02-28 04:55:52 -08001149 pr_info("amdgpu: switched on\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001150 /* don't suspend or resume card normally */
1151 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1152
Alex Deucher810ddc32016-08-23 13:25:49 -04001153 amdgpu_device_resume(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001154
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001155 dev->switch_power_state = DRM_SWITCH_POWER_ON;
1156 drm_kms_helper_poll_enable(dev);
1157 } else {
Joe Perches7ca85292017-02-28 04:55:52 -08001158 pr_info("amdgpu: switched off\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001159 drm_kms_helper_poll_disable(dev);
1160 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Alex Deucher810ddc32016-08-23 13:25:49 -04001161 amdgpu_device_suspend(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001162 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1163 }
1164}
1165
1166/**
1167 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1168 *
1169 * @pdev: pci dev pointer
1170 *
1171 * Callback for the switcheroo driver. Check of the switcheroo
1172 * state can be changed.
1173 * Returns true if the state can be changed, false if not.
1174 */
1175static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1176{
1177 struct drm_device *dev = pci_get_drvdata(pdev);
1178
1179 /*
1180 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1181 * locking inversion with the driver load path. And the access here is
1182 * completely racy anyway. So don't bother with locking for now.
1183 */
1184 return dev->open_count == 0;
1185}
1186
1187static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1188 .set_gpu_state = amdgpu_switcheroo_set_state,
1189 .reprobe = NULL,
1190 .can_switch = amdgpu_switcheroo_can_switch,
1191};
1192
1193int amdgpu_set_clockgating_state(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001194 enum amd_ip_block_type block_type,
1195 enum amd_clockgating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001196{
1197 int i, r = 0;
1198
1199 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001200 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001201 continue;
Rex Zhuc7228652017-02-22 15:33:46 +08001202 if (adev->ip_blocks[i].version->type != block_type)
1203 continue;
1204 if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
1205 continue;
1206 r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
1207 (void *)adev, state);
1208 if (r)
1209 DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
1210 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001211 }
1212 return r;
1213}
1214
1215int amdgpu_set_powergating_state(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001216 enum amd_ip_block_type block_type,
1217 enum amd_powergating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001218{
1219 int i, r = 0;
1220
1221 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001222 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001223 continue;
Rex Zhuc7228652017-02-22 15:33:46 +08001224 if (adev->ip_blocks[i].version->type != block_type)
1225 continue;
1226 if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
1227 continue;
1228 r = adev->ip_blocks[i].version->funcs->set_powergating_state(
1229 (void *)adev, state);
1230 if (r)
1231 DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
1232 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001233 }
1234 return r;
1235}
1236
Huang Rui6cb2d4e2017-01-05 18:44:41 +08001237void amdgpu_get_clockgating_state(struct amdgpu_device *adev, u32 *flags)
1238{
1239 int i;
1240
1241 for (i = 0; i < adev->num_ip_blocks; i++) {
1242 if (!adev->ip_blocks[i].status.valid)
1243 continue;
1244 if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
1245 adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
1246 }
1247}
1248
Alex Deucher5dbbb602016-06-23 11:41:04 -04001249int amdgpu_wait_for_idle(struct amdgpu_device *adev,
1250 enum amd_ip_block_type block_type)
1251{
1252 int i, r;
1253
1254 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001255 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001256 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001257 if (adev->ip_blocks[i].version->type == block_type) {
1258 r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -04001259 if (r)
1260 return r;
1261 break;
1262 }
1263 }
1264 return 0;
1265
1266}
1267
1268bool amdgpu_is_idle(struct amdgpu_device *adev,
1269 enum amd_ip_block_type block_type)
1270{
1271 int i;
1272
1273 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001274 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001275 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001276 if (adev->ip_blocks[i].version->type == block_type)
1277 return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -04001278 }
1279 return true;
1280
1281}
1282
Alex Deuchera1255102016-10-13 17:41:13 -04001283struct amdgpu_ip_block * amdgpu_get_ip_block(struct amdgpu_device *adev,
1284 enum amd_ip_block_type type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001285{
1286 int i;
1287
1288 for (i = 0; i < adev->num_ip_blocks; i++)
Alex Deuchera1255102016-10-13 17:41:13 -04001289 if (adev->ip_blocks[i].version->type == type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001290 return &adev->ip_blocks[i];
1291
1292 return NULL;
1293}
1294
1295/**
1296 * amdgpu_ip_block_version_cmp
1297 *
1298 * @adev: amdgpu_device pointer
yanyang15fc3aee2015-05-22 14:39:35 -04001299 * @type: enum amd_ip_block_type
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001300 * @major: major version
1301 * @minor: minor version
1302 *
1303 * return 0 if equal or greater
1304 * return 1 if smaller or the ip_block doesn't exist
1305 */
1306int amdgpu_ip_block_version_cmp(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001307 enum amd_ip_block_type type,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001308 u32 major, u32 minor)
1309{
Alex Deuchera1255102016-10-13 17:41:13 -04001310 struct amdgpu_ip_block *ip_block = amdgpu_get_ip_block(adev, type);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001311
Alex Deuchera1255102016-10-13 17:41:13 -04001312 if (ip_block && ((ip_block->version->major > major) ||
1313 ((ip_block->version->major == major) &&
1314 (ip_block->version->minor >= minor))))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001315 return 0;
1316
1317 return 1;
1318}
1319
Alex Deuchera1255102016-10-13 17:41:13 -04001320/**
1321 * amdgpu_ip_block_add
1322 *
1323 * @adev: amdgpu_device pointer
1324 * @ip_block_version: pointer to the IP to add
1325 *
1326 * Adds the IP block driver information to the collection of IPs
1327 * on the asic.
1328 */
1329int amdgpu_ip_block_add(struct amdgpu_device *adev,
1330 const struct amdgpu_ip_block_version *ip_block_version)
1331{
1332 if (!ip_block_version)
1333 return -EINVAL;
1334
Huang Ruia0bae352017-05-03 09:52:06 +08001335 DRM_DEBUG("add ip block number %d <%s>\n", adev->num_ip_blocks,
1336 ip_block_version->funcs->name);
1337
Alex Deuchera1255102016-10-13 17:41:13 -04001338 adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
1339
1340 return 0;
1341}
1342
Alex Deucher483ef982016-09-30 12:43:04 -04001343static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
Emily Deng9accf2f2016-08-10 16:01:25 +08001344{
1345 adev->enable_virtual_display = false;
1346
1347 if (amdgpu_virtual_display) {
1348 struct drm_device *ddev = adev->ddev;
1349 const char *pci_address_name = pci_name(ddev->pdev);
Emily Deng0f663562016-09-30 13:02:18 -04001350 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
Emily Deng9accf2f2016-08-10 16:01:25 +08001351
1352 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1353 pciaddstr_tmp = pciaddstr;
Emily Deng0f663562016-09-30 13:02:18 -04001354 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1355 pciaddname = strsep(&pciaddname_tmp, ",");
Yintian Tao967de2a2017-01-22 15:16:51 +08001356 if (!strcmp("all", pciaddname)
1357 || !strcmp(pci_address_name, pciaddname)) {
Emily Deng0f663562016-09-30 13:02:18 -04001358 long num_crtc;
1359 int res = -1;
1360
Emily Deng9accf2f2016-08-10 16:01:25 +08001361 adev->enable_virtual_display = true;
Emily Deng0f663562016-09-30 13:02:18 -04001362
1363 if (pciaddname_tmp)
1364 res = kstrtol(pciaddname_tmp, 10,
1365 &num_crtc);
1366
1367 if (!res) {
1368 if (num_crtc < 1)
1369 num_crtc = 1;
1370 if (num_crtc > 6)
1371 num_crtc = 6;
1372 adev->mode_info.num_crtc = num_crtc;
1373 } else {
1374 adev->mode_info.num_crtc = 1;
1375 }
Emily Deng9accf2f2016-08-10 16:01:25 +08001376 break;
1377 }
1378 }
1379
Emily Deng0f663562016-09-30 13:02:18 -04001380 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
1381 amdgpu_virtual_display, pci_address_name,
1382 adev->enable_virtual_display, adev->mode_info.num_crtc);
Emily Deng9accf2f2016-08-10 16:01:25 +08001383
1384 kfree(pciaddstr);
1385 }
1386}
1387
Alex Deuchere2a75f82017-04-27 16:58:01 -04001388static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev)
1389{
Alex Deuchere2a75f82017-04-27 16:58:01 -04001390 const char *chip_name;
1391 char fw_name[30];
1392 int err;
1393 const struct gpu_info_firmware_header_v1_0 *hdr;
1394
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001395 adev->firmware.gpu_info_fw = NULL;
1396
Alex Deuchere2a75f82017-04-27 16:58:01 -04001397 switch (adev->asic_type) {
1398 case CHIP_TOPAZ:
1399 case CHIP_TONGA:
1400 case CHIP_FIJI:
1401 case CHIP_POLARIS11:
1402 case CHIP_POLARIS10:
1403 case CHIP_POLARIS12:
1404 case CHIP_CARRIZO:
1405 case CHIP_STONEY:
1406#ifdef CONFIG_DRM_AMDGPU_SI
1407 case CHIP_VERDE:
1408 case CHIP_TAHITI:
1409 case CHIP_PITCAIRN:
1410 case CHIP_OLAND:
1411 case CHIP_HAINAN:
1412#endif
1413#ifdef CONFIG_DRM_AMDGPU_CIK
1414 case CHIP_BONAIRE:
1415 case CHIP_HAWAII:
1416 case CHIP_KAVERI:
1417 case CHIP_KABINI:
1418 case CHIP_MULLINS:
1419#endif
1420 default:
1421 return 0;
1422 case CHIP_VEGA10:
1423 chip_name = "vega10";
1424 break;
Alex Deucher2d2e5e72017-05-09 12:27:35 -04001425 case CHIP_RAVEN:
1426 chip_name = "raven";
1427 break;
Alex Deuchere2a75f82017-04-27 16:58:01 -04001428 }
1429
1430 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name);
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001431 err = request_firmware(&adev->firmware.gpu_info_fw, fw_name, adev->dev);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001432 if (err) {
1433 dev_err(adev->dev,
1434 "Failed to load gpu_info firmware \"%s\"\n",
1435 fw_name);
1436 goto out;
1437 }
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001438 err = amdgpu_ucode_validate(adev->firmware.gpu_info_fw);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001439 if (err) {
1440 dev_err(adev->dev,
1441 "Failed to validate gpu_info firmware \"%s\"\n",
1442 fw_name);
1443 goto out;
1444 }
1445
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001446 hdr = (const struct gpu_info_firmware_header_v1_0 *)adev->firmware.gpu_info_fw->data;
Alex Deuchere2a75f82017-04-27 16:58:01 -04001447 amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
1448
1449 switch (hdr->version_major) {
1450 case 1:
1451 {
1452 const struct gpu_info_firmware_v1_0 *gpu_info_fw =
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001453 (const struct gpu_info_firmware_v1_0 *)(adev->firmware.gpu_info_fw->data +
Alex Deuchere2a75f82017-04-27 16:58:01 -04001454 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1455
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001456 adev->gfx.config.max_shader_engines = le32_to_cpu(gpu_info_fw->gc_num_se);
1457 adev->gfx.config.max_cu_per_sh = le32_to_cpu(gpu_info_fw->gc_num_cu_per_sh);
1458 adev->gfx.config.max_sh_per_se = le32_to_cpu(gpu_info_fw->gc_num_sh_per_se);
1459 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 -04001460 adev->gfx.config.max_texture_channel_caches =
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001461 le32_to_cpu(gpu_info_fw->gc_num_tccs);
1462 adev->gfx.config.max_gprs = le32_to_cpu(gpu_info_fw->gc_num_gprs);
1463 adev->gfx.config.max_gs_threads = le32_to_cpu(gpu_info_fw->gc_num_max_gs_thds);
1464 adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gpu_info_fw->gc_gs_table_depth);
1465 adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gpu_info_fw->gc_gsprim_buff_depth);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001466 adev->gfx.config.double_offchip_lds_buf =
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001467 le32_to_cpu(gpu_info_fw->gc_double_offchip_lds_buffer);
1468 adev->gfx.cu_info.wave_front_size = le32_to_cpu(gpu_info_fw->gc_wave_size);
Hawking Zhang51fd0372017-06-09 22:30:52 +08001469 adev->gfx.cu_info.max_waves_per_simd =
1470 le32_to_cpu(gpu_info_fw->gc_max_waves_per_simd);
1471 adev->gfx.cu_info.max_scratch_slots_per_cu =
1472 le32_to_cpu(gpu_info_fw->gc_max_scratch_slots_per_cu);
1473 adev->gfx.cu_info.lds_size = le32_to_cpu(gpu_info_fw->gc_lds_size);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001474 break;
1475 }
1476 default:
1477 dev_err(adev->dev,
1478 "Unsupported gpu_info table %d\n", hdr->header.ucode_version);
1479 err = -EINVAL;
1480 goto out;
1481 }
1482out:
Alex Deuchere2a75f82017-04-27 16:58:01 -04001483 return err;
1484}
1485
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001486static int amdgpu_early_init(struct amdgpu_device *adev)
1487{
Alex Deucheraaa36a92015-04-20 17:31:14 -04001488 int i, r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001489
Alex Deucher483ef982016-09-30 12:43:04 -04001490 amdgpu_device_enable_virtual_display(adev);
Emily Denga6be7572016-08-08 11:37:50 +08001491
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001492 switch (adev->asic_type) {
Alex Deucheraaa36a92015-04-20 17:31:14 -04001493 case CHIP_TOPAZ:
1494 case CHIP_TONGA:
David Zhang48299f92015-07-08 01:05:16 +08001495 case CHIP_FIJI:
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001496 case CHIP_POLARIS11:
1497 case CHIP_POLARIS10:
Junwei Zhangc4642a42016-12-14 15:32:28 -05001498 case CHIP_POLARIS12:
Alex Deucheraaa36a92015-04-20 17:31:14 -04001499 case CHIP_CARRIZO:
Samuel Li39bb0c92015-10-08 16:31:43 -04001500 case CHIP_STONEY:
1501 if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
Alex Deucheraaa36a92015-04-20 17:31:14 -04001502 adev->family = AMDGPU_FAMILY_CZ;
1503 else
1504 adev->family = AMDGPU_FAMILY_VI;
1505
1506 r = vi_set_ip_blocks(adev);
1507 if (r)
1508 return r;
1509 break;
Ken Wang33f34802016-01-21 17:29:41 +08001510#ifdef CONFIG_DRM_AMDGPU_SI
1511 case CHIP_VERDE:
1512 case CHIP_TAHITI:
1513 case CHIP_PITCAIRN:
1514 case CHIP_OLAND:
1515 case CHIP_HAINAN:
Ken Wang295d0da2016-05-24 21:02:53 +08001516 adev->family = AMDGPU_FAMILY_SI;
Ken Wang33f34802016-01-21 17:29:41 +08001517 r = si_set_ip_blocks(adev);
1518 if (r)
1519 return r;
1520 break;
1521#endif
Alex Deuchera2e73f52015-04-20 17:09:27 -04001522#ifdef CONFIG_DRM_AMDGPU_CIK
1523 case CHIP_BONAIRE:
1524 case CHIP_HAWAII:
1525 case CHIP_KAVERI:
1526 case CHIP_KABINI:
1527 case CHIP_MULLINS:
1528 if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
1529 adev->family = AMDGPU_FAMILY_CI;
1530 else
1531 adev->family = AMDGPU_FAMILY_KV;
1532
1533 r = cik_set_ip_blocks(adev);
1534 if (r)
1535 return r;
1536 break;
1537#endif
Chunming Zhou2ca8a5d2016-12-07 17:31:19 +08001538 case CHIP_VEGA10:
1539 case CHIP_RAVEN:
1540 if (adev->asic_type == CHIP_RAVEN)
1541 adev->family = AMDGPU_FAMILY_RV;
1542 else
1543 adev->family = AMDGPU_FAMILY_AI;
Ken Wang460826e2017-03-06 14:53:16 -05001544
1545 r = soc15_set_ip_blocks(adev);
1546 if (r)
1547 return r;
1548 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001549 default:
1550 /* FIXME: not supported yet */
1551 return -EINVAL;
1552 }
1553
Alex Deuchere2a75f82017-04-27 16:58:01 -04001554 r = amdgpu_device_parse_gpu_info_fw(adev);
1555 if (r)
1556 return r;
1557
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001558 if (amdgpu_sriov_vf(adev)) {
1559 r = amdgpu_virt_request_full_gpu(adev, true);
1560 if (r)
1561 return r;
1562 }
1563
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001564 for (i = 0; i < adev->num_ip_blocks; i++) {
1565 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
Huang Ruied8cf002017-05-03 09:40:17 +08001566 DRM_ERROR("disabled ip block: %d <%s>\n",
1567 i, adev->ip_blocks[i].version->funcs->name);
Alex Deuchera1255102016-10-13 17:41:13 -04001568 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001569 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001570 if (adev->ip_blocks[i].version->funcs->early_init) {
1571 r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001572 if (r == -ENOENT) {
Alex Deuchera1255102016-10-13 17:41:13 -04001573 adev->ip_blocks[i].status.valid = false;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001574 } else if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001575 DRM_ERROR("early_init of IP block <%s> failed %d\n",
1576 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001577 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001578 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001579 adev->ip_blocks[i].status.valid = true;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001580 }
Alex Deucher974e6b62015-07-10 13:59:44 -04001581 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001582 adev->ip_blocks[i].status.valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001583 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001584 }
1585 }
1586
Nicolai Hähnle395d1fb2016-06-02 12:32:07 +02001587 adev->cg_flags &= amdgpu_cg_mask;
1588 adev->pg_flags &= amdgpu_pg_mask;
1589
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001590 return 0;
1591}
1592
1593static int amdgpu_init(struct amdgpu_device *adev)
1594{
1595 int i, r;
1596
1597 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001598 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001599 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001600 r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001601 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001602 DRM_ERROR("sw_init of IP block <%s> failed %d\n",
1603 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001604 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001605 }
Alex Deuchera1255102016-10-13 17:41:13 -04001606 adev->ip_blocks[i].status.sw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001607 /* need to do gmc hw init early so we can allocate gpu mem */
Alex Deuchera1255102016-10-13 17:41:13 -04001608 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001609 r = amdgpu_vram_scratch_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001610 if (r) {
1611 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001612 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001613 }
Alex Deuchera1255102016-10-13 17:41:13 -04001614 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001615 if (r) {
1616 DRM_ERROR("hw_init %d failed %d\n", i, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001617 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001618 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001619 r = amdgpu_wb_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001620 if (r) {
1621 DRM_ERROR("amdgpu_wb_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001622 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001623 }
Alex Deuchera1255102016-10-13 17:41:13 -04001624 adev->ip_blocks[i].status.hw = true;
Monk Liu24936642017-01-09 15:54:32 +08001625
1626 /* right after GMC hw init, we create CSA */
1627 if (amdgpu_sriov_vf(adev)) {
1628 r = amdgpu_allocate_static_csa(adev);
1629 if (r) {
1630 DRM_ERROR("allocate CSA failed %d\n", r);
1631 return r;
1632 }
1633 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001634 }
1635 }
1636
1637 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001638 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001639 continue;
1640 /* gmc hw init is done early */
Alex Deuchera1255102016-10-13 17:41:13 -04001641 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001642 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001643 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001644 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001645 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
1646 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001647 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001648 }
Alex Deuchera1255102016-10-13 17:41:13 -04001649 adev->ip_blocks[i].status.hw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001650 }
1651
1652 return 0;
1653}
1654
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08001655static void amdgpu_fill_reset_magic(struct amdgpu_device *adev)
1656{
1657 memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM);
1658}
1659
1660static bool amdgpu_check_vram_lost(struct amdgpu_device *adev)
1661{
1662 return !!memcmp(adev->gart.ptr, adev->reset_magic,
1663 AMDGPU_RESET_MAGIC_NUM);
1664}
1665
Shirish S2dc80b02017-05-25 10:05:25 +05301666static int amdgpu_late_set_cg_state(struct amdgpu_device *adev)
1667{
1668 int i = 0, r;
1669
1670 for (i = 0; i < adev->num_ip_blocks; i++) {
1671 if (!adev->ip_blocks[i].status.valid)
1672 continue;
1673 /* skip CG for VCE/UVD, it's handled specially */
1674 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1675 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1676 /* enable clockgating to save power */
1677 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1678 AMD_CG_STATE_GATE);
1679 if (r) {
1680 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
1681 adev->ip_blocks[i].version->funcs->name, r);
1682 return r;
1683 }
1684 }
1685 }
1686 return 0;
1687}
1688
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001689static int amdgpu_late_init(struct amdgpu_device *adev)
1690{
1691 int i = 0, r;
1692
1693 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001694 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001695 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001696 if (adev->ip_blocks[i].version->funcs->late_init) {
1697 r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001698 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001699 DRM_ERROR("late_init of IP block <%s> failed %d\n",
1700 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001701 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001702 }
Alex Deuchera1255102016-10-13 17:41:13 -04001703 adev->ip_blocks[i].status.late_initialized = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001704 }
1705 }
1706
Shirish S2dc80b02017-05-25 10:05:25 +05301707 mod_delayed_work(system_wq, &adev->late_init_work,
1708 msecs_to_jiffies(AMDGPU_RESUME_MS));
1709
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08001710 amdgpu_fill_reset_magic(adev);
1711
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001712 return 0;
1713}
1714
1715static int amdgpu_fini(struct amdgpu_device *adev)
1716{
1717 int i, r;
1718
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001719 /* need to disable SMC first */
1720 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001721 if (!adev->ip_blocks[i].status.hw)
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001722 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001723 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001724 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
Alex Deuchera1255102016-10-13 17:41:13 -04001725 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1726 AMD_CG_STATE_UNGATE);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001727 if (r) {
1728 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001729 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001730 return r;
1731 }
Alex Deuchera1255102016-10-13 17:41:13 -04001732 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001733 /* XXX handle errors */
1734 if (r) {
1735 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001736 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001737 }
Alex Deuchera1255102016-10-13 17:41:13 -04001738 adev->ip_blocks[i].status.hw = false;
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001739 break;
1740 }
1741 }
1742
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001743 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001744 if (!adev->ip_blocks[i].status.hw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001745 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001746 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001747 amdgpu_wb_fini(adev);
1748 amdgpu_vram_scratch_fini(adev);
1749 }
Rex Zhu8201a672016-11-24 21:44:44 +08001750
1751 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1752 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1753 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
1754 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1755 AMD_CG_STATE_UNGATE);
1756 if (r) {
1757 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1758 adev->ip_blocks[i].version->funcs->name, r);
1759 return r;
1760 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001761 }
Rex Zhu8201a672016-11-24 21:44:44 +08001762
Alex Deuchera1255102016-10-13 17:41:13 -04001763 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001764 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001765 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001766 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
1767 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001768 }
Rex Zhu8201a672016-11-24 21:44:44 +08001769
Alex Deuchera1255102016-10-13 17:41:13 -04001770 adev->ip_blocks[i].status.hw = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001771 }
1772
1773 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001774 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001775 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001776 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001777 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001778 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001779 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
1780 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001781 }
Alex Deuchera1255102016-10-13 17:41:13 -04001782 adev->ip_blocks[i].status.sw = false;
1783 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001784 }
1785
Monk Liua6dcfd92016-05-19 14:36:34 +08001786 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001787 if (!adev->ip_blocks[i].status.late_initialized)
Grazvydas Ignotas8a2eef12016-10-03 00:06:44 +03001788 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001789 if (adev->ip_blocks[i].version->funcs->late_fini)
1790 adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
1791 adev->ip_blocks[i].status.late_initialized = false;
Monk Liua6dcfd92016-05-19 14:36:34 +08001792 }
1793
Monk Liu030308f2017-09-15 15:34:52 +08001794 if (amdgpu_sriov_vf(adev))
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001795 amdgpu_virt_release_full_gpu(adev, false);
Monk Liu24936642017-01-09 15:54:32 +08001796
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001797 return 0;
1798}
1799
Shirish S2dc80b02017-05-25 10:05:25 +05301800static void amdgpu_late_init_func_handler(struct work_struct *work)
1801{
1802 struct amdgpu_device *adev =
1803 container_of(work, struct amdgpu_device, late_init_work.work);
1804 amdgpu_late_set_cg_state(adev);
1805}
1806
Alex Deucherfaefba92016-12-06 10:38:29 -05001807int amdgpu_suspend(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001808{
1809 int i, r;
1810
Xiangliang Yue941ea92017-01-18 12:47:55 +08001811 if (amdgpu_sriov_vf(adev))
1812 amdgpu_virt_request_full_gpu(adev, false);
1813
Flora Cuic5a93a22016-02-26 10:45:25 +08001814 /* ungate SMC block first */
1815 r = amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC,
1816 AMD_CG_STATE_UNGATE);
1817 if (r) {
1818 DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n",r);
1819 }
1820
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001821 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001822 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001823 continue;
1824 /* ungate blocks so that suspend can properly shut them down */
Flora Cuic5a93a22016-02-26 10:45:25 +08001825 if (i != AMD_IP_BLOCK_TYPE_SMC) {
Alex Deuchera1255102016-10-13 17:41:13 -04001826 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1827 AMD_CG_STATE_UNGATE);
Flora Cuic5a93a22016-02-26 10:45:25 +08001828 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001829 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1830 adev->ip_blocks[i].version->funcs->name, r);
Flora Cuic5a93a22016-02-26 10:45:25 +08001831 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001832 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001833 /* XXX handle errors */
Alex Deuchera1255102016-10-13 17:41:13 -04001834 r = adev->ip_blocks[i].version->funcs->suspend(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_ERROR("suspend of IP block <%s> failed %d\n",
1838 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001839 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001840 }
1841
Xiangliang Yue941ea92017-01-18 12:47:55 +08001842 if (amdgpu_sriov_vf(adev))
1843 amdgpu_virt_release_full_gpu(adev, false);
1844
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001845 return 0;
1846}
1847
Monk Liue4f0fdc2017-02-09 11:55:49 +08001848static int amdgpu_sriov_reinit_early(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08001849{
1850 int i, r;
1851
Monk Liu2cb681b2017-04-26 12:00:49 +08001852 static enum amd_ip_block_type ip_order[] = {
1853 AMD_IP_BLOCK_TYPE_GMC,
1854 AMD_IP_BLOCK_TYPE_COMMON,
Monk Liu2cb681b2017-04-26 12:00:49 +08001855 AMD_IP_BLOCK_TYPE_IH,
1856 };
Monk Liua90ad3c2017-01-23 14:22:08 +08001857
Monk Liu2cb681b2017-04-26 12:00:49 +08001858 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1859 int j;
1860 struct amdgpu_ip_block *block;
Monk Liua90ad3c2017-01-23 14:22:08 +08001861
Monk Liu2cb681b2017-04-26 12:00:49 +08001862 for (j = 0; j < adev->num_ip_blocks; j++) {
1863 block = &adev->ip_blocks[j];
1864
1865 if (block->version->type != ip_order[i] ||
1866 !block->status.valid)
1867 continue;
1868
1869 r = block->version->funcs->hw_init(adev);
1870 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
Monk Liua90ad3c2017-01-23 14:22:08 +08001871 }
1872 }
1873
1874 return 0;
1875}
1876
Monk Liue4f0fdc2017-02-09 11:55:49 +08001877static int amdgpu_sriov_reinit_late(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08001878{
1879 int i, r;
1880
Monk Liu2cb681b2017-04-26 12:00:49 +08001881 static enum amd_ip_block_type ip_order[] = {
1882 AMD_IP_BLOCK_TYPE_SMC,
1883 AMD_IP_BLOCK_TYPE_DCE,
1884 AMD_IP_BLOCK_TYPE_GFX,
1885 AMD_IP_BLOCK_TYPE_SDMA,
Frank Min257deb82017-06-15 20:07:36 +08001886 AMD_IP_BLOCK_TYPE_UVD,
1887 AMD_IP_BLOCK_TYPE_VCE
Monk Liu2cb681b2017-04-26 12:00:49 +08001888 };
Monk Liua90ad3c2017-01-23 14:22:08 +08001889
Monk Liu2cb681b2017-04-26 12:00:49 +08001890 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1891 int j;
1892 struct amdgpu_ip_block *block;
Monk Liua90ad3c2017-01-23 14:22:08 +08001893
Monk Liu2cb681b2017-04-26 12:00:49 +08001894 for (j = 0; j < adev->num_ip_blocks; j++) {
1895 block = &adev->ip_blocks[j];
1896
1897 if (block->version->type != ip_order[i] ||
1898 !block->status.valid)
1899 continue;
1900
1901 r = block->version->funcs->hw_init(adev);
1902 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
Monk Liua90ad3c2017-01-23 14:22:08 +08001903 }
1904 }
1905
1906 return 0;
1907}
1908
Chunming Zhoufcf06492017-05-05 10:33:33 +08001909static int amdgpu_resume_phase1(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001910{
1911 int i, r;
1912
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001913 for (i = 0; i < adev->num_ip_blocks; i++) {
1914 if (!adev->ip_blocks[i].status.valid)
1915 continue;
Chunming Zhoufcf06492017-05-05 10:33:33 +08001916 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
1917 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
1918 adev->ip_blocks[i].version->type ==
1919 AMD_IP_BLOCK_TYPE_IH) {
1920 r = adev->ip_blocks[i].version->funcs->resume(adev);
1921 if (r) {
1922 DRM_ERROR("resume of IP block <%s> failed %d\n",
1923 adev->ip_blocks[i].version->funcs->name, r);
1924 return r;
1925 }
1926 }
1927 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001928
Chunming Zhoufcf06492017-05-05 10:33:33 +08001929 return 0;
1930}
1931
1932static int amdgpu_resume_phase2(struct amdgpu_device *adev)
1933{
1934 int i, r;
1935
1936 for (i = 0; i < adev->num_ip_blocks; i++) {
1937 if (!adev->ip_blocks[i].status.valid)
1938 continue;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001939 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
1940 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
1941 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH )
1942 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001943 r = adev->ip_blocks[i].version->funcs->resume(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001944 if (r) {
1945 DRM_ERROR("resume of IP block <%s> failed %d\n",
1946 adev->ip_blocks[i].version->funcs->name, r);
1947 return r;
1948 }
1949 }
1950
1951 return 0;
1952}
1953
1954static int amdgpu_resume(struct amdgpu_device *adev)
1955{
Chunming Zhoufcf06492017-05-05 10:33:33 +08001956 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001957
Chunming Zhoufcf06492017-05-05 10:33:33 +08001958 r = amdgpu_resume_phase1(adev);
1959 if (r)
1960 return r;
1961 r = amdgpu_resume_phase2(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001962
Chunming Zhoufcf06492017-05-05 10:33:33 +08001963 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001964}
1965
Monk Liu4e99a442016-03-31 13:26:59 +08001966static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
Andres Rodriguez048765a2016-06-11 02:51:32 -04001967{
Alex Deuchera5bde2f2016-09-23 16:23:41 -04001968 if (adev->is_atom_fw) {
1969 if (amdgpu_atomfirmware_gpu_supports_virtualization(adev))
1970 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
1971 } else {
1972 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
1973 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
1974 }
Andres Rodriguez048765a2016-06-11 02:51:32 -04001975}
1976
Harry Wentland45622362017-09-12 15:58:20 -04001977bool amdgpu_device_asic_has_dc_support(enum amd_asic_type asic_type)
1978{
1979 switch (asic_type) {
1980#if defined(CONFIG_DRM_AMD_DC)
1981 case CHIP_BONAIRE:
1982 case CHIP_HAWAII:
1983 case CHIP_CARRIZO:
1984 case CHIP_STONEY:
1985 case CHIP_POLARIS11:
1986 case CHIP_POLARIS10:
Alex Deucher2c8ad2d2017-06-15 16:20:24 -04001987 case CHIP_POLARIS12:
Harry Wentland45622362017-09-12 15:58:20 -04001988 case CHIP_TONGA:
1989 case CHIP_FIJI:
Alex Deucher2c8ad2d2017-06-15 16:20:24 -04001990#if defined(CONFIG_DRM_AMD_DC_DCE12_0)
1991 case CHIP_VEGA10:
1992#endif
Harry Wentland45622362017-09-12 15:58:20 -04001993#if defined(CONFIG_DRM_AMD_DC_PRE_VEGA)
1994 return amdgpu_dc != 0;
1995#else
1996 return amdgpu_dc > 0;
1997#endif
1998#endif
1999 default:
2000 return false;
2001 }
2002}
2003
2004/**
2005 * amdgpu_device_has_dc_support - check if dc is supported
2006 *
2007 * @adev: amdgpu_device_pointer
2008 *
2009 * Returns true for supported, false for not supported
2010 */
2011bool amdgpu_device_has_dc_support(struct amdgpu_device *adev)
2012{
Xiangliang Yu2555039d2017-01-10 17:34:52 +08002013 if (amdgpu_sriov_vf(adev))
2014 return false;
2015
Harry Wentland45622362017-09-12 15:58:20 -04002016 return amdgpu_device_asic_has_dc_support(adev->asic_type);
2017}
2018
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002019/**
2020 * amdgpu_device_init - initialize the driver
2021 *
2022 * @adev: amdgpu_device pointer
2023 * @pdev: drm dev pointer
2024 * @pdev: pci dev pointer
2025 * @flags: driver flags
2026 *
2027 * Initializes the driver info and hw (all asics).
2028 * Returns 0 for success or an error on failure.
2029 * Called at driver startup.
2030 */
2031int amdgpu_device_init(struct amdgpu_device *adev,
2032 struct drm_device *ddev,
2033 struct pci_dev *pdev,
2034 uint32_t flags)
2035{
2036 int r, i;
2037 bool runtime = false;
Marek Olšák95844d22016-08-17 23:49:27 +02002038 u32 max_MBps;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002039
2040 adev->shutdown = false;
2041 adev->dev = &pdev->dev;
2042 adev->ddev = ddev;
2043 adev->pdev = pdev;
2044 adev->flags = flags;
Jammy Zhou2f7d10b2015-07-22 11:29:01 +08002045 adev->asic_type = flags & AMD_ASIC_MASK;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002046 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
Christian König6f02a692017-07-07 11:56:59 +02002047 adev->mc.gart_size = 512 * 1024 * 1024;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002048 adev->accel_working = false;
2049 adev->num_rings = 0;
2050 adev->mman.buffer_funcs = NULL;
2051 adev->mman.buffer_funcs_ring = NULL;
2052 adev->vm_manager.vm_pte_funcs = NULL;
Christian König2d55e452016-02-08 17:37:38 +01002053 adev->vm_manager.vm_pte_num_rings = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002054 adev->gart.gart_funcs = NULL;
Chris Wilsonf54d1862016-10-25 13:00:45 +01002055 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002056
2057 adev->smc_rreg = &amdgpu_invalid_rreg;
2058 adev->smc_wreg = &amdgpu_invalid_wreg;
2059 adev->pcie_rreg = &amdgpu_invalid_rreg;
2060 adev->pcie_wreg = &amdgpu_invalid_wreg;
Huang Rui36b9a952016-08-31 13:23:25 +08002061 adev->pciep_rreg = &amdgpu_invalid_rreg;
2062 adev->pciep_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002063 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
2064 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
2065 adev->didt_rreg = &amdgpu_invalid_rreg;
2066 adev->didt_wreg = &amdgpu_invalid_wreg;
Rex Zhuccdbb202016-06-08 12:47:41 +08002067 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
2068 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002069 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
2070 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
2071
Rex Zhuccdbb202016-06-08 12:47:41 +08002072
Alex Deucher3e39ab92015-06-05 15:04:33 -04002073 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
2074 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
2075 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002076
2077 /* mutex initialization are all done here so we
2078 * can recall function without having locking issues */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002079 atomic_set(&adev->irq.ih.lock, 0);
Huang Rui0e5ca0d2017-03-03 18:37:23 -05002080 mutex_init(&adev->firmware.mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002081 mutex_init(&adev->pm.mutex);
2082 mutex_init(&adev->gfx.gpu_clock_mutex);
2083 mutex_init(&adev->srbm_mutex);
2084 mutex_init(&adev->grbm_idx_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002085 mutex_init(&adev->mn_lock);
2086 hash_init(adev->mn_hash);
2087
2088 amdgpu_check_arguments(adev);
2089
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002090 spin_lock_init(&adev->mmio_idx_lock);
2091 spin_lock_init(&adev->smc_idx_lock);
2092 spin_lock_init(&adev->pcie_idx_lock);
2093 spin_lock_init(&adev->uvd_ctx_idx_lock);
2094 spin_lock_init(&adev->didt_idx_lock);
Rex Zhuccdbb202016-06-08 12:47:41 +08002095 spin_lock_init(&adev->gc_cac_idx_lock);
Evan Quan16abb5d2017-07-04 09:21:50 +08002096 spin_lock_init(&adev->se_cac_idx_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002097 spin_lock_init(&adev->audio_endpt_idx_lock);
Marek Olšák95844d22016-08-17 23:49:27 +02002098 spin_lock_init(&adev->mm_stats.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002099
Chunming Zhou0c4e7fa2016-08-17 11:41:30 +08002100 INIT_LIST_HEAD(&adev->shadow_list);
2101 mutex_init(&adev->shadow_list_lock);
2102
Chunming Zhou5c1354b2016-08-30 16:13:10 +08002103 INIT_LIST_HEAD(&adev->gtt_list);
2104 spin_lock_init(&adev->gtt_list_lock);
2105
Andres Rodriguez795f2812017-03-06 16:27:55 -05002106 INIT_LIST_HEAD(&adev->ring_lru_list);
2107 spin_lock_init(&adev->ring_lru_list_lock);
2108
Shirish S2dc80b02017-05-25 10:05:25 +05302109 INIT_DELAYED_WORK(&adev->late_init_work, amdgpu_late_init_func_handler);
2110
Alex Xie0fa49552017-06-08 14:58:05 -04002111 /* Registers mapping */
2112 /* TODO: block userspace mapping of io register */
Ken Wangda69c1612016-01-21 19:08:55 +08002113 if (adev->asic_type >= CHIP_BONAIRE) {
2114 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
2115 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
2116 } else {
2117 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
2118 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
2119 }
Chunming Zhou5c1354b2016-08-30 16:13:10 +08002120
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002121 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
2122 if (adev->rmmio == NULL) {
2123 return -ENOMEM;
2124 }
2125 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
2126 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
2127
Christian König705e5192017-06-08 11:15:16 +02002128 /* doorbell bar mapping */
2129 amdgpu_doorbell_init(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002130
2131 /* io port mapping */
2132 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
2133 if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
2134 adev->rio_mem_size = pci_resource_len(adev->pdev, i);
2135 adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
2136 break;
2137 }
2138 }
2139 if (adev->rio_mem == NULL)
Amber Linb64a18c2017-01-04 08:06:58 -05002140 DRM_INFO("PCI I/O BAR is not found.\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002141
2142 /* early init functions */
2143 r = amdgpu_early_init(adev);
2144 if (r)
2145 return r;
2146
2147 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
2148 /* this will fail for cards that aren't VGA class devices, just
2149 * ignore it */
2150 vga_client_register(adev->pdev, adev, NULL, amdgpu_vga_set_decode);
2151
2152 if (amdgpu_runtime_pm == 1)
2153 runtime = true;
Alex Deuchere9bef452016-04-25 13:12:18 -04002154 if (amdgpu_device_is_px(ddev))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002155 runtime = true;
Lukas Wunner84c8b222017-03-10 21:23:45 +01002156 if (!pci_is_thunderbolt_attached(adev->pdev))
2157 vga_switcheroo_register_client(adev->pdev,
2158 &amdgpu_switcheroo_ops, runtime);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002159 if (runtime)
2160 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
2161
2162 /* Read BIOS */
Alex Deucher83ba1262016-06-03 18:21:41 -04002163 if (!amdgpu_get_bios(adev)) {
2164 r = -EINVAL;
2165 goto failed;
2166 }
Nils Wallméniusf7e9e9f2016-12-14 21:52:45 +01002167
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002168 r = amdgpu_atombios_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002169 if (r) {
2170 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
Gavin Wan89041942017-06-23 13:55:15 -04002171 amdgpu_vf_error_put(AMDGIM_ERROR_VF_ATOMBIOS_INIT_FAIL, 0, 0);
Alex Deucher83ba1262016-06-03 18:21:41 -04002172 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002173 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002174
Monk Liu4e99a442016-03-31 13:26:59 +08002175 /* detect if we are with an SRIOV vbios */
2176 amdgpu_device_detect_sriov_bios(adev);
Andres Rodriguez048765a2016-06-11 02:51:32 -04002177
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002178 /* Post card if necessary */
Monk Liubec86372016-09-14 19:38:08 +08002179 if (amdgpu_vpost_needed(adev)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002180 if (!adev->bios) {
Monk Liubec86372016-09-14 19:38:08 +08002181 dev_err(adev->dev, "no vBIOS found\n");
Gavin Wan89041942017-06-23 13:55:15 -04002182 amdgpu_vf_error_put(AMDGIM_ERROR_VF_NO_VBIOS, 0, 0);
Alex Deucher83ba1262016-06-03 18:21:41 -04002183 r = -EINVAL;
2184 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002185 }
Monk Liubec86372016-09-14 19:38:08 +08002186 DRM_INFO("GPU posting now...\n");
Monk Liu4e99a442016-03-31 13:26:59 +08002187 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2188 if (r) {
2189 dev_err(adev->dev, "gpu post error!\n");
Gavin Wan89041942017-06-23 13:55:15 -04002190 amdgpu_vf_error_put(AMDGIM_ERROR_VF_GPU_POST_ERROR, 0, 0);
Monk Liu4e99a442016-03-31 13:26:59 +08002191 goto failed;
2192 }
2193 } else {
2194 DRM_INFO("GPU post is not needed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002195 }
2196
Alex Deucher88b64e92017-07-10 10:43:10 -04002197 if (adev->is_atom_fw) {
2198 /* Initialize clocks */
2199 r = amdgpu_atomfirmware_get_clock_info(adev);
2200 if (r) {
2201 dev_err(adev->dev, "amdgpu_atomfirmware_get_clock_info failed\n");
2202 amdgpu_vf_error_put(AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
2203 goto failed;
2204 }
2205 } else {
Alex Deuchera5bde2f2016-09-23 16:23:41 -04002206 /* Initialize clocks */
2207 r = amdgpu_atombios_get_clock_info(adev);
2208 if (r) {
2209 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
Gavin Wan89041942017-06-23 13:55:15 -04002210 amdgpu_vf_error_put(AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
2211 goto failed;
Alex Deuchera5bde2f2016-09-23 16:23:41 -04002212 }
2213 /* init i2c buses */
Harry Wentland45622362017-09-12 15:58:20 -04002214 if (!amdgpu_device_has_dc_support(adev))
2215 amdgpu_atombios_i2c_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002216 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002217
2218 /* Fence driver */
2219 r = amdgpu_fence_driver_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002220 if (r) {
2221 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
Gavin Wan89041942017-06-23 13:55:15 -04002222 amdgpu_vf_error_put(AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0);
Alex Deucher83ba1262016-06-03 18:21:41 -04002223 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002224 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002225
2226 /* init the mode config */
2227 drm_mode_config_init(adev->ddev);
2228
2229 r = amdgpu_init(adev);
2230 if (r) {
Alex Deucher2c1a2782015-12-07 17:02:53 -05002231 dev_err(adev->dev, "amdgpu_init failed\n");
Gavin Wan89041942017-06-23 13:55:15 -04002232 amdgpu_vf_error_put(AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002233 amdgpu_fini(adev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002234 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002235 }
2236
2237 adev->accel_working = true;
2238
Alex Xiee59c0202017-06-01 09:42:59 -04002239 amdgpu_vm_check_compute_bug(adev);
2240
Marek Olšák95844d22016-08-17 23:49:27 +02002241 /* Initialize the buffer migration limit. */
2242 if (amdgpu_moverate >= 0)
2243 max_MBps = amdgpu_moverate;
2244 else
2245 max_MBps = 8; /* Allow 8 MB/s. */
2246 /* Get a log2 for easy divisions. */
2247 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
2248
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002249 r = amdgpu_ib_pool_init(adev);
2250 if (r) {
2251 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
Gavin Wan89041942017-06-23 13:55:15 -04002252 amdgpu_vf_error_put(AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r);
Alex Deucher83ba1262016-06-03 18:21:41 -04002253 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002254 }
2255
2256 r = amdgpu_ib_ring_tests(adev);
2257 if (r)
2258 DRM_ERROR("ib ring test failed (%d).\n", r);
2259
Monk Liu9bc92b92017-02-08 17:38:13 +08002260 amdgpu_fbdev_init(adev);
2261
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002262 r = amdgpu_gem_debugfs_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002263 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002264 DRM_ERROR("registering gem debugfs failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002265
2266 r = amdgpu_debugfs_regs_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002267 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002268 DRM_ERROR("registering register debugfs failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002269
Huang Rui4f0955f2017-05-10 23:04:06 +08002270 r = amdgpu_debugfs_test_ib_ring_init(adev);
2271 if (r)
2272 DRM_ERROR("registering register test ib ring debugfs failed (%d).\n", r);
2273
Huang Rui50ab2532016-06-12 15:51:09 +08002274 r = amdgpu_debugfs_firmware_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002275 if (r)
Huang Rui50ab2532016-06-12 15:51:09 +08002276 DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
Huang Rui50ab2532016-06-12 15:51:09 +08002277
Kent Russelldb95e212017-08-22 12:31:43 -04002278 r = amdgpu_debugfs_vbios_dump_init(adev);
2279 if (r)
2280 DRM_ERROR("Creating vbios dump debugfs failed (%d).\n", r);
2281
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002282 if ((amdgpu_testing & 1)) {
2283 if (adev->accel_working)
2284 amdgpu_test_moves(adev);
2285 else
2286 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
2287 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002288 if (amdgpu_benchmarking) {
2289 if (adev->accel_working)
2290 amdgpu_benchmark(adev, amdgpu_benchmarking);
2291 else
2292 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
2293 }
2294
2295 /* enable clockgating, etc. after ib tests, etc. since some blocks require
2296 * explicit gating rather than handling it automatically.
2297 */
2298 r = amdgpu_late_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002299 if (r) {
2300 dev_err(adev->dev, "amdgpu_late_init failed\n");
Gavin Wan89041942017-06-23 13:55:15 -04002301 amdgpu_vf_error_put(AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r);
Alex Deucher83ba1262016-06-03 18:21:41 -04002302 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002303 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002304
2305 return 0;
Alex Deucher83ba1262016-06-03 18:21:41 -04002306
2307failed:
Gavin Wan89041942017-06-23 13:55:15 -04002308 amdgpu_vf_error_trans_all(adev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002309 if (runtime)
2310 vga_switcheroo_fini_domain_pm_ops(adev->dev);
2311 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002312}
2313
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002314/**
2315 * amdgpu_device_fini - tear down the driver
2316 *
2317 * @adev: amdgpu_device pointer
2318 *
2319 * Tear down the driver info (all asics).
2320 * Called at driver shutdown.
2321 */
2322void amdgpu_device_fini(struct amdgpu_device *adev)
2323{
2324 int r;
2325
2326 DRM_INFO("amdgpu: finishing device.\n");
2327 adev->shutdown = true;
Pixel Dingdb2c2a92017-04-25 16:47:42 +08002328 if (adev->mode_info.mode_config_initialized)
2329 drm_crtc_force_disable_all(adev->ddev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002330 /* evict vram memory */
2331 amdgpu_bo_evict_vram(adev);
2332 amdgpu_ib_pool_fini(adev);
2333 amdgpu_fence_driver_fini(adev);
2334 amdgpu_fbdev_fini(adev);
2335 r = amdgpu_fini(adev);
Huang Ruiab4fe3e2017-06-05 22:11:59 +08002336 if (adev->firmware.gpu_info_fw) {
2337 release_firmware(adev->firmware.gpu_info_fw);
2338 adev->firmware.gpu_info_fw = NULL;
2339 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002340 adev->accel_working = false;
Shirish S2dc80b02017-05-25 10:05:25 +05302341 cancel_delayed_work_sync(&adev->late_init_work);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002342 /* free i2c buses */
Harry Wentland45622362017-09-12 15:58:20 -04002343 if (!amdgpu_device_has_dc_support(adev))
2344 amdgpu_i2c_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002345 amdgpu_atombios_fini(adev);
2346 kfree(adev->bios);
2347 adev->bios = NULL;
Lukas Wunner84c8b222017-03-10 21:23:45 +01002348 if (!pci_is_thunderbolt_attached(adev->pdev))
2349 vga_switcheroo_unregister_client(adev->pdev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002350 if (adev->flags & AMD_IS_PX)
2351 vga_switcheroo_fini_domain_pm_ops(adev->dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002352 vga_client_register(adev->pdev, NULL, NULL, NULL);
2353 if (adev->rio_mem)
2354 pci_iounmap(adev->pdev, adev->rio_mem);
2355 adev->rio_mem = NULL;
2356 iounmap(adev->rmmio);
2357 adev->rmmio = NULL;
Christian König705e5192017-06-08 11:15:16 +02002358 amdgpu_doorbell_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002359 amdgpu_debugfs_regs_cleanup(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002360}
2361
2362
2363/*
2364 * Suspend & resume.
2365 */
2366/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002367 * amdgpu_device_suspend - initiate device suspend
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002368 *
2369 * @pdev: drm dev pointer
2370 * @state: suspend state
2371 *
2372 * Puts the hw in the suspend state (all asics).
2373 * Returns 0 for success or an error on failure.
2374 * Called at driver suspend.
2375 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002376int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002377{
2378 struct amdgpu_device *adev;
2379 struct drm_crtc *crtc;
2380 struct drm_connector *connector;
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002381 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002382
2383 if (dev == NULL || dev->dev_private == NULL) {
2384 return -ENODEV;
2385 }
2386
2387 adev = dev->dev_private;
2388
2389 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2390 return 0;
2391
2392 drm_kms_helper_poll_disable(dev);
2393
Harry Wentland45622362017-09-12 15:58:20 -04002394 if (!amdgpu_device_has_dc_support(adev)) {
2395 /* turn off display hw */
2396 drm_modeset_lock_all(dev);
2397 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2398 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
2399 }
2400 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002401 }
2402
Yong Zhaoba997702015-11-09 17:21:45 -05002403 amdgpu_amdkfd_suspend(adev);
2404
Alex Deucher756e6882015-10-08 00:03:36 -04002405 /* unpin the front buffers and cursors */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002406 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Alex Deucher756e6882015-10-08 00:03:36 -04002407 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002408 struct amdgpu_framebuffer *rfb = to_amdgpu_framebuffer(crtc->primary->fb);
2409 struct amdgpu_bo *robj;
2410
Alex Deucher756e6882015-10-08 00:03:36 -04002411 if (amdgpu_crtc->cursor_bo) {
2412 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
Alex Xie7a6901d2017-04-24 13:52:41 -04002413 r = amdgpu_bo_reserve(aobj, true);
Alex Deucher756e6882015-10-08 00:03:36 -04002414 if (r == 0) {
2415 amdgpu_bo_unpin(aobj);
2416 amdgpu_bo_unreserve(aobj);
2417 }
2418 }
2419
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002420 if (rfb == NULL || rfb->obj == NULL) {
2421 continue;
2422 }
2423 robj = gem_to_amdgpu_bo(rfb->obj);
2424 /* don't unpin kernel fb objects */
2425 if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
Alex Xie7a6901d2017-04-24 13:52:41 -04002426 r = amdgpu_bo_reserve(robj, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002427 if (r == 0) {
2428 amdgpu_bo_unpin(robj);
2429 amdgpu_bo_unreserve(robj);
2430 }
2431 }
2432 }
2433 /* evict vram memory */
2434 amdgpu_bo_evict_vram(adev);
2435
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002436 amdgpu_fence_driver_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002437
2438 r = amdgpu_suspend(adev);
2439
Alex Deuchera0a71e42016-10-10 12:41:36 -04002440 /* evict remaining vram memory
2441 * This second call to evict vram is to evict the gart page table
2442 * using the CPU.
2443 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002444 amdgpu_bo_evict_vram(adev);
2445
Alex Deucherd05da0e2017-06-30 17:08:45 -04002446 amdgpu_atombios_scratch_regs_save(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002447 pci_save_state(dev->pdev);
2448 if (suspend) {
2449 /* Shut down the device */
2450 pci_disable_device(dev->pdev);
2451 pci_set_power_state(dev->pdev, PCI_D3hot);
jimqu74b0b152016-09-07 17:09:12 +08002452 } else {
2453 r = amdgpu_asic_reset(adev);
2454 if (r)
2455 DRM_ERROR("amdgpu asic reset failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002456 }
2457
2458 if (fbcon) {
2459 console_lock();
2460 amdgpu_fbdev_set_suspend(adev, 1);
2461 console_unlock();
2462 }
2463 return 0;
2464}
2465
2466/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002467 * amdgpu_device_resume - initiate device resume
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002468 *
2469 * @pdev: drm dev pointer
2470 *
2471 * Bring the hw back to operating state (all asics).
2472 * Returns 0 for success or an error on failure.
2473 * Called at driver resume.
2474 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002475int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002476{
2477 struct drm_connector *connector;
2478 struct amdgpu_device *adev = dev->dev_private;
Alex Deucher756e6882015-10-08 00:03:36 -04002479 struct drm_crtc *crtc;
Huang Rui03161a62017-04-13 16:12:26 +08002480 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002481
2482 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2483 return 0;
2484
jimqu74b0b152016-09-07 17:09:12 +08002485 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002486 console_lock();
jimqu74b0b152016-09-07 17:09:12 +08002487
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002488 if (resume) {
2489 pci_set_power_state(dev->pdev, PCI_D0);
2490 pci_restore_state(dev->pdev);
jimqu74b0b152016-09-07 17:09:12 +08002491 r = pci_enable_device(dev->pdev);
Huang Rui03161a62017-04-13 16:12:26 +08002492 if (r)
2493 goto unlock;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002494 }
Alex Deucherd05da0e2017-06-30 17:08:45 -04002495 amdgpu_atombios_scratch_regs_restore(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002496
2497 /* post card */
Jim Quc836fec2017-02-10 15:59:59 +08002498 if (amdgpu_need_post(adev)) {
jimqu74b0b152016-09-07 17:09:12 +08002499 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2500 if (r)
2501 DRM_ERROR("amdgpu asic init failed\n");
2502 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002503
2504 r = amdgpu_resume(adev);
Rex Zhue6707212017-03-30 13:21:01 +08002505 if (r) {
Flora Cuica198522016-02-04 15:10:08 +08002506 DRM_ERROR("amdgpu_resume failed (%d).\n", r);
Huang Rui03161a62017-04-13 16:12:26 +08002507 goto unlock;
Rex Zhue6707212017-03-30 13:21:01 +08002508 }
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002509 amdgpu_fence_driver_resume(adev);
2510
Flora Cuica198522016-02-04 15:10:08 +08002511 if (resume) {
2512 r = amdgpu_ib_ring_tests(adev);
2513 if (r)
2514 DRM_ERROR("ib ring test failed (%d).\n", r);
2515 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002516
2517 r = amdgpu_late_init(adev);
Huang Rui03161a62017-04-13 16:12:26 +08002518 if (r)
2519 goto unlock;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002520
Alex Deucher756e6882015-10-08 00:03:36 -04002521 /* pin cursors */
2522 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2523 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2524
2525 if (amdgpu_crtc->cursor_bo) {
2526 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
Alex Xie7a6901d2017-04-24 13:52:41 -04002527 r = amdgpu_bo_reserve(aobj, true);
Alex Deucher756e6882015-10-08 00:03:36 -04002528 if (r == 0) {
2529 r = amdgpu_bo_pin(aobj,
2530 AMDGPU_GEM_DOMAIN_VRAM,
2531 &amdgpu_crtc->cursor_addr);
2532 if (r != 0)
2533 DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
2534 amdgpu_bo_unreserve(aobj);
2535 }
2536 }
2537 }
Yong Zhaoba997702015-11-09 17:21:45 -05002538 r = amdgpu_amdkfd_resume(adev);
2539 if (r)
2540 return r;
Alex Deucher756e6882015-10-08 00:03:36 -04002541
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002542 /* blat the mode back in */
2543 if (fbcon) {
Harry Wentland45622362017-09-12 15:58:20 -04002544 if (!amdgpu_device_has_dc_support(adev)) {
2545 /* pre DCE11 */
2546 drm_helper_resume_force_mode(dev);
2547
2548 /* turn on display hw */
2549 drm_modeset_lock_all(dev);
2550 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2551 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
2552 }
2553 drm_modeset_unlock_all(dev);
2554 } else {
2555 /*
2556 * There is no equivalent atomic helper to turn on
2557 * display, so we defined our own function for this,
2558 * once suspend resume is supported by the atomic
2559 * framework this will be reworked
2560 */
2561 amdgpu_dm_display_resume(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002562 }
2563 }
2564
2565 drm_kms_helper_poll_enable(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002566
2567 /*
2568 * Most of the connector probing functions try to acquire runtime pm
2569 * refs to ensure that the GPU is powered on when connector polling is
2570 * performed. Since we're calling this from a runtime PM callback,
2571 * trying to acquire rpm refs will cause us to deadlock.
2572 *
2573 * Since we're guaranteed to be holding the rpm lock, it's safe to
2574 * temporarily disable the rpm helpers so this doesn't deadlock us.
2575 */
2576#ifdef CONFIG_PM
2577 dev->dev->power.disable_depth++;
2578#endif
Harry Wentland45622362017-09-12 15:58:20 -04002579 if (!amdgpu_device_has_dc_support(adev))
2580 drm_helper_hpd_irq_event(dev);
2581 else
2582 drm_kms_helper_hotplug_event(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002583#ifdef CONFIG_PM
2584 dev->dev->power.disable_depth--;
2585#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002586
Huang Rui03161a62017-04-13 16:12:26 +08002587 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002588 amdgpu_fbdev_set_suspend(adev, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002589
Huang Rui03161a62017-04-13 16:12:26 +08002590unlock:
2591 if (fbcon)
2592 console_unlock();
2593
2594 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002595}
2596
Chunming Zhou63fbf422016-07-15 11:19:20 +08002597static bool amdgpu_check_soft_reset(struct amdgpu_device *adev)
2598{
2599 int i;
2600 bool asic_hang = false;
2601
2602 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002603 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou63fbf422016-07-15 11:19:20 +08002604 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002605 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
2606 adev->ip_blocks[i].status.hang =
2607 adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
2608 if (adev->ip_blocks[i].status.hang) {
2609 DRM_INFO("IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
Chunming Zhou63fbf422016-07-15 11:19:20 +08002610 asic_hang = true;
2611 }
2612 }
2613 return asic_hang;
2614}
2615
Baoyou Xie4d446652016-09-18 22:09:35 +08002616static int amdgpu_pre_soft_reset(struct amdgpu_device *adev)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002617{
2618 int i, r = 0;
2619
2620 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002621 if (!adev->ip_blocks[i].status.valid)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002622 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002623 if (adev->ip_blocks[i].status.hang &&
2624 adev->ip_blocks[i].version->funcs->pre_soft_reset) {
2625 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
Chunming Zhoud31a5012016-07-18 10:04:34 +08002626 if (r)
2627 return r;
2628 }
2629 }
2630
2631 return 0;
2632}
2633
Chunming Zhou35d782f2016-07-15 15:57:13 +08002634static bool amdgpu_need_full_reset(struct amdgpu_device *adev)
2635{
Alex Deucherda146d32016-10-13 16:07:03 -04002636 int i;
2637
2638 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002639 if (!adev->ip_blocks[i].status.valid)
Alex Deucherda146d32016-10-13 16:07:03 -04002640 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002641 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
2642 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
2643 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
Ken Wang98512bb2017-09-14 16:25:19 +08002644 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) ||
2645 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
Alex Deuchera1255102016-10-13 17:41:13 -04002646 if (adev->ip_blocks[i].status.hang) {
Alex Deucherda146d32016-10-13 16:07:03 -04002647 DRM_INFO("Some block need full reset!\n");
2648 return true;
2649 }
2650 }
Chunming Zhou35d782f2016-07-15 15:57:13 +08002651 }
2652 return false;
2653}
2654
2655static int amdgpu_soft_reset(struct amdgpu_device *adev)
2656{
2657 int i, r = 0;
2658
2659 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002660 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002661 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002662 if (adev->ip_blocks[i].status.hang &&
2663 adev->ip_blocks[i].version->funcs->soft_reset) {
2664 r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002665 if (r)
2666 return r;
2667 }
2668 }
2669
2670 return 0;
2671}
2672
2673static int amdgpu_post_soft_reset(struct amdgpu_device *adev)
2674{
2675 int i, r = 0;
2676
2677 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002678 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002679 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002680 if (adev->ip_blocks[i].status.hang &&
2681 adev->ip_blocks[i].version->funcs->post_soft_reset)
2682 r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002683 if (r)
2684 return r;
2685 }
2686
2687 return 0;
2688}
2689
Chunming Zhou3ad81f12016-08-05 17:30:17 +08002690bool amdgpu_need_backup(struct amdgpu_device *adev)
2691{
2692 if (adev->flags & AMD_IS_APU)
2693 return false;
2694
2695 return amdgpu_lockup_timeout > 0 ? true : false;
2696}
2697
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002698static int amdgpu_recover_vram_from_shadow(struct amdgpu_device *adev,
2699 struct amdgpu_ring *ring,
2700 struct amdgpu_bo *bo,
Chris Wilsonf54d1862016-10-25 13:00:45 +01002701 struct dma_fence **fence)
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002702{
2703 uint32_t domain;
2704 int r;
2705
Roger.He23d2e502017-04-21 14:24:26 +08002706 if (!bo->shadow)
2707 return 0;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002708
Alex Xie1d284792017-04-24 13:53:04 -04002709 r = amdgpu_bo_reserve(bo, true);
Roger.He23d2e502017-04-21 14:24:26 +08002710 if (r)
2711 return r;
2712 domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
2713 /* if bo has been evicted, then no need to recover */
2714 if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
Roger.He82521312017-04-21 13:08:43 +08002715 r = amdgpu_bo_validate(bo->shadow);
2716 if (r) {
2717 DRM_ERROR("bo validate failed!\n");
2718 goto err;
2719 }
2720
Roger.He23d2e502017-04-21 14:24:26 +08002721 r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002722 NULL, fence, true);
Roger.He23d2e502017-04-21 14:24:26 +08002723 if (r) {
2724 DRM_ERROR("recover page table failed!\n");
2725 goto err;
2726 }
2727 }
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002728err:
Roger.He23d2e502017-04-21 14:24:26 +08002729 amdgpu_bo_unreserve(bo);
2730 return r;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002731}
2732
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002733/**
Monk Liua90ad3c2017-01-23 14:22:08 +08002734 * amdgpu_sriov_gpu_reset - reset the asic
2735 *
2736 * @adev: amdgpu device pointer
Monk Liu7225f872017-04-26 14:51:54 +08002737 * @job: which job trigger hang
Monk Liua90ad3c2017-01-23 14:22:08 +08002738 *
2739 * Attempt the reset the GPU if it has hung (all asics).
2740 * for SRIOV case.
2741 * Returns 0 for success or an error on failure.
2742 */
Monk Liu7225f872017-04-26 14:51:54 +08002743int amdgpu_sriov_gpu_reset(struct amdgpu_device *adev, struct amdgpu_job *job)
Monk Liua90ad3c2017-01-23 14:22:08 +08002744{
Monk Liu65781c72017-05-11 13:36:44 +08002745 int i, j, r = 0;
Monk Liua90ad3c2017-01-23 14:22:08 +08002746 int resched;
2747 struct amdgpu_bo *bo, *tmp;
2748 struct amdgpu_ring *ring;
2749 struct dma_fence *fence = NULL, *next = NULL;
2750
Monk Liu147b5982017-01-25 15:48:01 +08002751 mutex_lock(&adev->virt.lock_reset);
Monk Liua90ad3c2017-01-23 14:22:08 +08002752 atomic_inc(&adev->gpu_reset_counter);
Monk Liu3224a122017-09-15 18:57:12 +08002753 adev->in_sriov_reset = true;
Monk Liua90ad3c2017-01-23 14:22:08 +08002754
2755 /* block TTM */
2756 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
2757
Monk Liu65781c72017-05-11 13:36:44 +08002758 /* we start from the ring trigger GPU hang */
2759 j = job ? job->ring->idx : 0;
Monk Liua90ad3c2017-01-23 14:22:08 +08002760
Monk Liu65781c72017-05-11 13:36:44 +08002761 /* block scheduler */
2762 for (i = j; i < j + AMDGPU_MAX_RINGS; ++i) {
2763 ring = adev->rings[i % AMDGPU_MAX_RINGS];
Monk Liua90ad3c2017-01-23 14:22:08 +08002764 if (!ring || !ring->sched.thread)
2765 continue;
2766
2767 kthread_park(ring->sched.thread);
Monk Liua90ad3c2017-01-23 14:22:08 +08002768
Monk Liu65781c72017-05-11 13:36:44 +08002769 if (job && j != i)
2770 continue;
2771
Monk Liu4f059ec2017-05-11 13:59:15 +08002772 /* here give the last chance to check if job removed from mirror-list
Monk Liu65781c72017-05-11 13:36:44 +08002773 * since we already pay some time on kthread_park */
Monk Liu4f059ec2017-05-11 13:59:15 +08002774 if (job && list_empty(&job->base.node)) {
Monk Liu65781c72017-05-11 13:36:44 +08002775 kthread_unpark(ring->sched.thread);
2776 goto give_up_reset;
2777 }
2778
2779 if (amd_sched_invalidate_job(&job->base, amdgpu_job_hang_limit))
2780 amd_sched_job_kickout(&job->base);
2781
2782 /* only do job_reset on the hang ring if @job not NULL */
Monk Liua90ad3c2017-01-23 14:22:08 +08002783 amd_sched_hw_job_reset(&ring->sched);
Monk Liu65781c72017-05-11 13:36:44 +08002784
2785 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
2786 amdgpu_fence_driver_force_completion_ring(ring);
Monk Liua90ad3c2017-01-23 14:22:08 +08002787 }
2788
Monk Liua90ad3c2017-01-23 14:22:08 +08002789 /* request to take full control of GPU before re-initialization */
Monk Liu7225f872017-04-26 14:51:54 +08002790 if (job)
Monk Liua90ad3c2017-01-23 14:22:08 +08002791 amdgpu_virt_reset_gpu(adev);
2792 else
2793 amdgpu_virt_request_full_gpu(adev, true);
2794
2795
2796 /* Resume IP prior to SMC */
Monk Liue4f0fdc2017-02-09 11:55:49 +08002797 amdgpu_sriov_reinit_early(adev);
Monk Liua90ad3c2017-01-23 14:22:08 +08002798
2799 /* we need recover gart prior to run SMC/CP/SDMA resume */
2800 amdgpu_ttm_recover_gart(adev);
2801
2802 /* now we are okay to resume SMC/CP/SDMA */
Monk Liue4f0fdc2017-02-09 11:55:49 +08002803 amdgpu_sriov_reinit_late(adev);
Monk Liua90ad3c2017-01-23 14:22:08 +08002804
2805 amdgpu_irq_gpu_reset_resume_helper(adev);
2806
2807 if (amdgpu_ib_ring_tests(adev))
2808 dev_err(adev->dev, "[GPU_RESET] ib ring test failed (%d).\n", r);
2809
2810 /* release full control of GPU after ib test */
2811 amdgpu_virt_release_full_gpu(adev, true);
2812
2813 DRM_INFO("recover vram bo from shadow\n");
2814
2815 ring = adev->mman.buffer_funcs_ring;
2816 mutex_lock(&adev->shadow_list_lock);
2817 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
Monk Liu236763d2017-05-01 16:15:31 +08002818 next = NULL;
Monk Liua90ad3c2017-01-23 14:22:08 +08002819 amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
2820 if (fence) {
2821 r = dma_fence_wait(fence, false);
2822 if (r) {
2823 WARN(r, "recovery from shadow isn't completed\n");
2824 break;
2825 }
2826 }
2827
2828 dma_fence_put(fence);
2829 fence = next;
2830 }
2831 mutex_unlock(&adev->shadow_list_lock);
2832
2833 if (fence) {
2834 r = dma_fence_wait(fence, false);
2835 if (r)
2836 WARN(r, "recovery from shadow isn't completed\n");
2837 }
2838 dma_fence_put(fence);
2839
Monk Liu65781c72017-05-11 13:36:44 +08002840 for (i = j; i < j + AMDGPU_MAX_RINGS; ++i) {
2841 ring = adev->rings[i % AMDGPU_MAX_RINGS];
Monk Liua90ad3c2017-01-23 14:22:08 +08002842 if (!ring || !ring->sched.thread)
2843 continue;
2844
Monk Liu65781c72017-05-11 13:36:44 +08002845 if (job && j != i) {
2846 kthread_unpark(ring->sched.thread);
2847 continue;
2848 }
2849
Monk Liua90ad3c2017-01-23 14:22:08 +08002850 amd_sched_job_recovery(&ring->sched);
2851 kthread_unpark(ring->sched.thread);
2852 }
2853
2854 drm_helper_resume_force_mode(adev->ddev);
Monk Liu65781c72017-05-11 13:36:44 +08002855give_up_reset:
Monk Liua90ad3c2017-01-23 14:22:08 +08002856 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
2857 if (r) {
2858 /* bad news, how to tell it to userspace ? */
2859 dev_info(adev->dev, "GPU reset failed\n");
Monk Liu65781c72017-05-11 13:36:44 +08002860 } else {
2861 dev_info(adev->dev, "GPU reset successed!\n");
Monk Liua90ad3c2017-01-23 14:22:08 +08002862 }
2863
Monk Liu3224a122017-09-15 18:57:12 +08002864 adev->in_sriov_reset = false;
Monk Liu147b5982017-01-25 15:48:01 +08002865 mutex_unlock(&adev->virt.lock_reset);
Monk Liua90ad3c2017-01-23 14:22:08 +08002866 return r;
2867}
2868
2869/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002870 * amdgpu_gpu_reset - reset the asic
2871 *
2872 * @adev: amdgpu device pointer
2873 *
2874 * Attempt the reset the GPU if it has hung (all asics).
2875 * Returns 0 for success or an error on failure.
2876 */
2877int amdgpu_gpu_reset(struct amdgpu_device *adev)
2878{
Harry Wentland45622362017-09-12 15:58:20 -04002879 struct drm_atomic_state *state = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002880 int i, r;
2881 int resched;
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002882 bool need_full_reset, vram_lost = false;
Xiangliang Yufb140b22016-12-17 22:48:57 +08002883
Chunming Zhou63fbf422016-07-15 11:19:20 +08002884 if (!amdgpu_check_soft_reset(adev)) {
2885 DRM_INFO("No hardware hang detected. Did some blocks stall?\n");
2886 return 0;
2887 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002888
Marek Olšákd94aed52015-05-05 21:13:49 +02002889 atomic_inc(&adev->gpu_reset_counter);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002890
Chunming Zhoua3c47d62016-06-30 16:44:41 +08002891 /* block TTM */
2892 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
Harry Wentland45622362017-09-12 15:58:20 -04002893 /* store modesetting */
2894 if (amdgpu_device_has_dc_support(adev))
2895 state = drm_atomic_helper_suspend(adev->ddev);
Chunming Zhoua3c47d62016-06-30 16:44:41 +08002896
Chunming Zhou0875dc92016-06-12 15:41:58 +08002897 /* block scheduler */
2898 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2899 struct amdgpu_ring *ring = adev->rings[i];
2900
Chunming Zhou51687752017-04-24 17:09:15 +08002901 if (!ring || !ring->sched.thread)
Chunming Zhou0875dc92016-06-12 15:41:58 +08002902 continue;
2903 kthread_park(ring->sched.thread);
Chunming Zhouaa1c8902016-06-30 13:56:02 +08002904 amd_sched_hw_job_reset(&ring->sched);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002905 }
Chunming Zhou2200eda2016-06-30 16:53:02 +08002906 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
2907 amdgpu_fence_driver_force_completion(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002908
Chunming Zhou35d782f2016-07-15 15:57:13 +08002909 need_full_reset = amdgpu_need_full_reset(adev);
2910
2911 if (!need_full_reset) {
2912 amdgpu_pre_soft_reset(adev);
2913 r = amdgpu_soft_reset(adev);
2914 amdgpu_post_soft_reset(adev);
2915 if (r || amdgpu_check_soft_reset(adev)) {
2916 DRM_INFO("soft reset failed, will fallback to full reset!\n");
2917 need_full_reset = true;
2918 }
2919 }
2920
2921 if (need_full_reset) {
Chunming Zhou35d782f2016-07-15 15:57:13 +08002922 r = amdgpu_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002923
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002924retry:
Alex Deucherd05da0e2017-06-30 17:08:45 -04002925 amdgpu_atombios_scratch_regs_save(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002926 r = amdgpu_asic_reset(adev);
Alex Deucherd05da0e2017-06-30 17:08:45 -04002927 amdgpu_atombios_scratch_regs_restore(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002928 /* post card */
2929 amdgpu_atom_asic_init(adev->mode_info.atom_context);
Alex Deucherbfa99262016-01-15 11:59:48 -05002930
Chunming Zhou35d782f2016-07-15 15:57:13 +08002931 if (!r) {
2932 dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
Chunming Zhoufcf06492017-05-05 10:33:33 +08002933 r = amdgpu_resume_phase1(adev);
2934 if (r)
2935 goto out;
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002936 vram_lost = amdgpu_check_vram_lost(adev);
Chunming Zhouf1892132017-05-15 16:48:27 +08002937 if (vram_lost) {
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002938 DRM_ERROR("VRAM is lost!\n");
Chunming Zhouf1892132017-05-15 16:48:27 +08002939 atomic_inc(&adev->vram_lost_counter);
2940 }
Chunming Zhou2c0d7312016-08-30 16:36:25 +08002941 r = amdgpu_ttm_recover_gart(adev);
2942 if (r)
Chunming Zhoufcf06492017-05-05 10:33:33 +08002943 goto out;
2944 r = amdgpu_resume_phase2(adev);
2945 if (r)
2946 goto out;
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002947 if (vram_lost)
2948 amdgpu_fill_reset_magic(adev);
Chunming Zhou2c0d7312016-08-30 16:36:25 +08002949 }
Chunming Zhoufcf06492017-05-05 10:33:33 +08002950 }
2951out:
2952 if (!r) {
2953 amdgpu_irq_gpu_reset_resume_helper(adev);
Chunming Zhou1f465082016-06-30 15:02:26 +08002954 r = amdgpu_ib_ring_tests(adev);
2955 if (r) {
2956 dev_err(adev->dev, "ib ring test failed (%d).\n", r);
Chunming Zhou40019dc2016-06-29 16:01:49 +08002957 r = amdgpu_suspend(adev);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002958 need_full_reset = true;
Chunming Zhou40019dc2016-06-29 16:01:49 +08002959 goto retry;
Chunming Zhou1f465082016-06-30 15:02:26 +08002960 }
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002961 /**
2962 * recovery vm page tables, since we cannot depend on VRAM is
2963 * consistent after gpu full reset.
2964 */
2965 if (need_full_reset && amdgpu_need_backup(adev)) {
2966 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
2967 struct amdgpu_bo *bo, *tmp;
Chris Wilsonf54d1862016-10-25 13:00:45 +01002968 struct dma_fence *fence = NULL, *next = NULL;
Chunming Zhou1f465082016-06-30 15:02:26 +08002969
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002970 DRM_INFO("recover vram bo from shadow\n");
2971 mutex_lock(&adev->shadow_list_lock);
2972 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
Monk Liu236763d2017-05-01 16:15:31 +08002973 next = NULL;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002974 amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
2975 if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01002976 r = dma_fence_wait(fence, false);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002977 if (r) {
Monk Liu1d7b17b2017-01-22 18:52:56 +08002978 WARN(r, "recovery from shadow isn't completed\n");
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002979 break;
2980 }
2981 }
2982
Chris Wilsonf54d1862016-10-25 13:00:45 +01002983 dma_fence_put(fence);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002984 fence = next;
2985 }
2986 mutex_unlock(&adev->shadow_list_lock);
2987 if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01002988 r = dma_fence_wait(fence, false);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002989 if (r)
Monk Liu1d7b17b2017-01-22 18:52:56 +08002990 WARN(r, "recovery from shadow isn't completed\n");
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002991 }
Chris Wilsonf54d1862016-10-25 13:00:45 +01002992 dma_fence_put(fence);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002993 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002994 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2995 struct amdgpu_ring *ring = adev->rings[i];
Chunming Zhou51687752017-04-24 17:09:15 +08002996
2997 if (!ring || !ring->sched.thread)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002998 continue;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002999
Chunming Zhouaa1c8902016-06-30 13:56:02 +08003000 amd_sched_job_recovery(&ring->sched);
Chunming Zhou0875dc92016-06-12 15:41:58 +08003001 kthread_unpark(ring->sched.thread);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003002 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003003 } else {
Chunming Zhou2200eda2016-06-30 16:53:02 +08003004 dev_err(adev->dev, "asic resume failed (%d).\n", r);
Gavin Wan89041942017-06-23 13:55:15 -04003005 amdgpu_vf_error_put(AMDGIM_ERROR_VF_ASIC_RESUME_FAIL, 0, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003006 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
Chunming Zhou51687752017-04-24 17:09:15 +08003007 if (adev->rings[i] && adev->rings[i]->sched.thread) {
Chunming Zhou0875dc92016-06-12 15:41:58 +08003008 kthread_unpark(adev->rings[i]->sched.thread);
Chunming Zhou0875dc92016-06-12 15:41:58 +08003009 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003010 }
3011 }
3012
Harry Wentland45622362017-09-12 15:58:20 -04003013 if (amdgpu_device_has_dc_support(adev)) {
3014 r = drm_atomic_helper_resume(adev->ddev, state);
3015 amdgpu_dm_display_resume(adev);
3016 } else
3017 drm_helper_resume_force_mode(adev->ddev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003018
3019 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
Gavin Wan89041942017-06-23 13:55:15 -04003020 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003021 /* bad news, how to tell it to userspace ? */
3022 dev_info(adev->dev, "GPU reset failed\n");
Gavin Wan89041942017-06-23 13:55:15 -04003023 amdgpu_vf_error_put(AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, r);
3024 }
3025 else {
Chunming Zhou6643be62017-05-05 10:50:09 +08003026 dev_info(adev->dev, "GPU reset successed!\n");
Gavin Wan89041942017-06-23 13:55:15 -04003027 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003028
Gavin Wan89041942017-06-23 13:55:15 -04003029 amdgpu_vf_error_trans_all(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003030 return r;
3031}
3032
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003033void amdgpu_get_pcie_info(struct amdgpu_device *adev)
3034{
3035 u32 mask;
3036 int ret;
3037
Alex Deuchercd474ba2016-02-04 10:21:23 -05003038 if (amdgpu_pcie_gen_cap)
3039 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
3040
3041 if (amdgpu_pcie_lane_cap)
3042 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
3043
3044 /* covers APUs as well */
3045 if (pci_is_root_bus(adev->pdev->bus)) {
3046 if (adev->pm.pcie_gen_mask == 0)
3047 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
3048 if (adev->pm.pcie_mlw_mask == 0)
3049 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003050 return;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003051 }
Alex Deuchercd474ba2016-02-04 10:21:23 -05003052
3053 if (adev->pm.pcie_gen_mask == 0) {
3054 ret = drm_pcie_get_speed_cap_mask(adev->ddev, &mask);
3055 if (!ret) {
3056 adev->pm.pcie_gen_mask = (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
3057 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
3058 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
3059
3060 if (mask & DRM_PCIE_SPEED_25)
3061 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
3062 if (mask & DRM_PCIE_SPEED_50)
3063 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2;
3064 if (mask & DRM_PCIE_SPEED_80)
3065 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3;
3066 } else {
3067 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
3068 }
3069 }
3070 if (adev->pm.pcie_mlw_mask == 0) {
3071 ret = drm_pcie_get_max_link_width(adev->ddev, &mask);
3072 if (!ret) {
3073 switch (mask) {
3074 case 32:
3075 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
3076 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
3077 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3078 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3079 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3080 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3081 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3082 break;
3083 case 16:
3084 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
3085 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3086 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3087 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3088 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3089 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3090 break;
3091 case 12:
3092 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3093 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3094 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3095 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3096 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3097 break;
3098 case 8:
3099 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3100 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3101 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3102 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3103 break;
3104 case 4:
3105 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3106 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3107 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3108 break;
3109 case 2:
3110 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3111 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3112 break;
3113 case 1:
3114 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
3115 break;
3116 default:
3117 break;
3118 }
3119 } else {
3120 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003121 }
3122 }
3123}
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003124
3125/*
3126 * Debugfs
3127 */
3128int amdgpu_debugfs_add_files(struct amdgpu_device *adev,
Nils Wallménius06ab6832016-05-02 12:46:15 -04003129 const struct drm_info_list *files,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003130 unsigned nfiles)
3131{
3132 unsigned i;
3133
3134 for (i = 0; i < adev->debugfs_count; i++) {
3135 if (adev->debugfs[i].files == files) {
3136 /* Already registered */
3137 return 0;
3138 }
3139 }
3140
3141 i = adev->debugfs_count + 1;
3142 if (i > AMDGPU_DEBUGFS_MAX_COMPONENTS) {
3143 DRM_ERROR("Reached maximum number of debugfs components.\n");
3144 DRM_ERROR("Report so we increase "
3145 "AMDGPU_DEBUGFS_MAX_COMPONENTS.\n");
3146 return -EINVAL;
3147 }
3148 adev->debugfs[adev->debugfs_count].files = files;
3149 adev->debugfs[adev->debugfs_count].num_files = nfiles;
3150 adev->debugfs_count = i;
3151#if defined(CONFIG_DEBUG_FS)
3152 drm_debugfs_create_files(files, nfiles,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003153 adev->ddev->primary->debugfs_root,
3154 adev->ddev->primary);
3155#endif
3156 return 0;
3157}
3158
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003159#if defined(CONFIG_DEBUG_FS)
3160
3161static ssize_t amdgpu_debugfs_regs_read(struct file *f, char __user *buf,
3162 size_t size, loff_t *pos)
3163{
Al Viro45063092016-12-04 18:24:56 -05003164 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003165 ssize_t result = 0;
3166 int r;
Tom St Denisbd122672016-07-28 09:39:22 -04003167 bool pm_pg_lock, use_bank;
Tom St Denis566281592016-06-27 11:55:07 -04003168 unsigned instance_bank, sh_bank, se_bank;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003169
3170 if (size & 0x3 || *pos & 0x3)
3171 return -EINVAL;
3172
Tom St Denisbd122672016-07-28 09:39:22 -04003173 /* are we reading registers for which a PG lock is necessary? */
3174 pm_pg_lock = (*pos >> 23) & 1;
3175
Tom St Denis566281592016-06-27 11:55:07 -04003176 if (*pos & (1ULL << 62)) {
3177 se_bank = (*pos >> 24) & 0x3FF;
3178 sh_bank = (*pos >> 34) & 0x3FF;
3179 instance_bank = (*pos >> 44) & 0x3FF;
Tom St Denis32977f92016-10-09 07:41:26 -04003180
3181 if (se_bank == 0x3FF)
3182 se_bank = 0xFFFFFFFF;
3183 if (sh_bank == 0x3FF)
3184 sh_bank = 0xFFFFFFFF;
3185 if (instance_bank == 0x3FF)
3186 instance_bank = 0xFFFFFFFF;
Tom St Denis566281592016-06-27 11:55:07 -04003187 use_bank = 1;
Tom St Denis566281592016-06-27 11:55:07 -04003188 } else {
3189 use_bank = 0;
3190 }
3191
Tom St Denis801a6aa9a62017-03-15 05:34:25 -04003192 *pos &= (1UL << 22) - 1;
Tom St Denisbd122672016-07-28 09:39:22 -04003193
Tom St Denis566281592016-06-27 11:55:07 -04003194 if (use_bank) {
Tom St Denis32977f92016-10-09 07:41:26 -04003195 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
3196 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
Tom St Denis566281592016-06-27 11:55:07 -04003197 return -EINVAL;
3198 mutex_lock(&adev->grbm_idx_mutex);
3199 amdgpu_gfx_select_se_sh(adev, se_bank,
3200 sh_bank, instance_bank);
3201 }
3202
Tom St Denisbd122672016-07-28 09:39:22 -04003203 if (pm_pg_lock)
3204 mutex_lock(&adev->pm.mutex);
3205
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003206 while (size) {
3207 uint32_t value;
3208
3209 if (*pos > adev->rmmio_size)
Tom St Denis566281592016-06-27 11:55:07 -04003210 goto end;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003211
3212 value = RREG32(*pos >> 2);
3213 r = put_user(value, (uint32_t *)buf);
Tom St Denis566281592016-06-27 11:55:07 -04003214 if (r) {
3215 result = r;
3216 goto end;
3217 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003218
3219 result += 4;
3220 buf += 4;
3221 *pos += 4;
3222 size -= 4;
3223 }
3224
Tom St Denis566281592016-06-27 11:55:07 -04003225end:
3226 if (use_bank) {
3227 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
3228 mutex_unlock(&adev->grbm_idx_mutex);
3229 }
3230
Tom St Denisbd122672016-07-28 09:39:22 -04003231 if (pm_pg_lock)
3232 mutex_unlock(&adev->pm.mutex);
3233
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003234 return result;
3235}
3236
3237static ssize_t amdgpu_debugfs_regs_write(struct file *f, const char __user *buf,
3238 size_t size, loff_t *pos)
3239{
Al Viro45063092016-12-04 18:24:56 -05003240 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003241 ssize_t result = 0;
3242 int r;
Tom St Denis394fdde2016-10-10 07:31:23 -04003243 bool pm_pg_lock, use_bank;
3244 unsigned instance_bank, sh_bank, se_bank;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003245
3246 if (size & 0x3 || *pos & 0x3)
3247 return -EINVAL;
3248
Tom St Denis394fdde2016-10-10 07:31:23 -04003249 /* are we reading registers for which a PG lock is necessary? */
3250 pm_pg_lock = (*pos >> 23) & 1;
3251
3252 if (*pos & (1ULL << 62)) {
3253 se_bank = (*pos >> 24) & 0x3FF;
3254 sh_bank = (*pos >> 34) & 0x3FF;
3255 instance_bank = (*pos >> 44) & 0x3FF;
3256
3257 if (se_bank == 0x3FF)
3258 se_bank = 0xFFFFFFFF;
3259 if (sh_bank == 0x3FF)
3260 sh_bank = 0xFFFFFFFF;
3261 if (instance_bank == 0x3FF)
3262 instance_bank = 0xFFFFFFFF;
3263 use_bank = 1;
3264 } else {
3265 use_bank = 0;
3266 }
3267
Tom St Denis801a6aa9a62017-03-15 05:34:25 -04003268 *pos &= (1UL << 22) - 1;
Tom St Denis394fdde2016-10-10 07:31:23 -04003269
3270 if (use_bank) {
3271 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
3272 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
3273 return -EINVAL;
3274 mutex_lock(&adev->grbm_idx_mutex);
3275 amdgpu_gfx_select_se_sh(adev, se_bank,
3276 sh_bank, instance_bank);
3277 }
3278
3279 if (pm_pg_lock)
3280 mutex_lock(&adev->pm.mutex);
3281
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003282 while (size) {
3283 uint32_t value;
3284
3285 if (*pos > adev->rmmio_size)
3286 return result;
3287
3288 r = get_user(value, (uint32_t *)buf);
3289 if (r)
3290 return r;
3291
3292 WREG32(*pos >> 2, value);
3293
3294 result += 4;
3295 buf += 4;
3296 *pos += 4;
3297 size -= 4;
3298 }
3299
Tom St Denis394fdde2016-10-10 07:31:23 -04003300 if (use_bank) {
3301 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
3302 mutex_unlock(&adev->grbm_idx_mutex);
3303 }
3304
3305 if (pm_pg_lock)
3306 mutex_unlock(&adev->pm.mutex);
3307
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003308 return result;
3309}
3310
Tom St Denisadcec282016-04-15 13:08:44 -04003311static ssize_t amdgpu_debugfs_regs_pcie_read(struct file *f, char __user *buf,
3312 size_t size, loff_t *pos)
3313{
Al Viro45063092016-12-04 18:24:56 -05003314 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003315 ssize_t result = 0;
3316 int r;
3317
3318 if (size & 0x3 || *pos & 0x3)
3319 return -EINVAL;
3320
3321 while (size) {
3322 uint32_t value;
3323
3324 value = RREG32_PCIE(*pos >> 2);
3325 r = put_user(value, (uint32_t *)buf);
3326 if (r)
3327 return r;
3328
3329 result += 4;
3330 buf += 4;
3331 *pos += 4;
3332 size -= 4;
3333 }
3334
3335 return result;
3336}
3337
3338static ssize_t amdgpu_debugfs_regs_pcie_write(struct file *f, const char __user *buf,
3339 size_t size, loff_t *pos)
3340{
Al Viro45063092016-12-04 18:24:56 -05003341 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003342 ssize_t result = 0;
3343 int r;
3344
3345 if (size & 0x3 || *pos & 0x3)
3346 return -EINVAL;
3347
3348 while (size) {
3349 uint32_t value;
3350
3351 r = get_user(value, (uint32_t *)buf);
3352 if (r)
3353 return r;
3354
3355 WREG32_PCIE(*pos >> 2, value);
3356
3357 result += 4;
3358 buf += 4;
3359 *pos += 4;
3360 size -= 4;
3361 }
3362
3363 return result;
3364}
3365
3366static ssize_t amdgpu_debugfs_regs_didt_read(struct file *f, char __user *buf,
3367 size_t size, loff_t *pos)
3368{
Al Viro45063092016-12-04 18:24:56 -05003369 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003370 ssize_t result = 0;
3371 int r;
3372
3373 if (size & 0x3 || *pos & 0x3)
3374 return -EINVAL;
3375
3376 while (size) {
3377 uint32_t value;
3378
3379 value = RREG32_DIDT(*pos >> 2);
3380 r = put_user(value, (uint32_t *)buf);
3381 if (r)
3382 return r;
3383
3384 result += 4;
3385 buf += 4;
3386 *pos += 4;
3387 size -= 4;
3388 }
3389
3390 return result;
3391}
3392
3393static ssize_t amdgpu_debugfs_regs_didt_write(struct file *f, const char __user *buf,
3394 size_t size, loff_t *pos)
3395{
Al Viro45063092016-12-04 18:24:56 -05003396 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003397 ssize_t result = 0;
3398 int r;
3399
3400 if (size & 0x3 || *pos & 0x3)
3401 return -EINVAL;
3402
3403 while (size) {
3404 uint32_t value;
3405
3406 r = get_user(value, (uint32_t *)buf);
3407 if (r)
3408 return r;
3409
3410 WREG32_DIDT(*pos >> 2, value);
3411
3412 result += 4;
3413 buf += 4;
3414 *pos += 4;
3415 size -= 4;
3416 }
3417
3418 return result;
3419}
3420
3421static ssize_t amdgpu_debugfs_regs_smc_read(struct file *f, char __user *buf,
3422 size_t size, loff_t *pos)
3423{
Al Viro45063092016-12-04 18:24:56 -05003424 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003425 ssize_t result = 0;
3426 int r;
3427
3428 if (size & 0x3 || *pos & 0x3)
3429 return -EINVAL;
3430
3431 while (size) {
3432 uint32_t value;
3433
Tom St Denis6fc0dea2016-08-29 08:39:29 -04003434 value = RREG32_SMC(*pos);
Tom St Denisadcec282016-04-15 13:08:44 -04003435 r = put_user(value, (uint32_t *)buf);
3436 if (r)
3437 return r;
3438
3439 result += 4;
3440 buf += 4;
3441 *pos += 4;
3442 size -= 4;
3443 }
3444
3445 return result;
3446}
3447
3448static ssize_t amdgpu_debugfs_regs_smc_write(struct file *f, const char __user *buf,
3449 size_t size, loff_t *pos)
3450{
Al Viro45063092016-12-04 18:24:56 -05003451 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003452 ssize_t result = 0;
3453 int r;
3454
3455 if (size & 0x3 || *pos & 0x3)
3456 return -EINVAL;
3457
3458 while (size) {
3459 uint32_t value;
3460
3461 r = get_user(value, (uint32_t *)buf);
3462 if (r)
3463 return r;
3464
Tom St Denis6fc0dea2016-08-29 08:39:29 -04003465 WREG32_SMC(*pos, value);
Tom St Denisadcec282016-04-15 13:08:44 -04003466
3467 result += 4;
3468 buf += 4;
3469 *pos += 4;
3470 size -= 4;
3471 }
3472
3473 return result;
3474}
3475
Tom St Denis1e051412016-06-27 09:57:18 -04003476static ssize_t amdgpu_debugfs_gca_config_read(struct file *f, char __user *buf,
3477 size_t size, loff_t *pos)
3478{
Al Viro45063092016-12-04 18:24:56 -05003479 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denis1e051412016-06-27 09:57:18 -04003480 ssize_t result = 0;
3481 int r;
3482 uint32_t *config, no_regs = 0;
3483
3484 if (size & 0x3 || *pos & 0x3)
3485 return -EINVAL;
3486
Markus Elfringecab7662016-09-18 17:00:52 +02003487 config = kmalloc_array(256, sizeof(*config), GFP_KERNEL);
Tom St Denis1e051412016-06-27 09:57:18 -04003488 if (!config)
3489 return -ENOMEM;
3490
3491 /* version, increment each time something is added */
Tom St Denis9a999352017-01-18 13:01:25 -05003492 config[no_regs++] = 3;
Tom St Denis1e051412016-06-27 09:57:18 -04003493 config[no_regs++] = adev->gfx.config.max_shader_engines;
3494 config[no_regs++] = adev->gfx.config.max_tile_pipes;
3495 config[no_regs++] = adev->gfx.config.max_cu_per_sh;
3496 config[no_regs++] = adev->gfx.config.max_sh_per_se;
3497 config[no_regs++] = adev->gfx.config.max_backends_per_se;
3498 config[no_regs++] = adev->gfx.config.max_texture_channel_caches;
3499 config[no_regs++] = adev->gfx.config.max_gprs;
3500 config[no_regs++] = adev->gfx.config.max_gs_threads;
3501 config[no_regs++] = adev->gfx.config.max_hw_contexts;
3502 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_frontend;
3503 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_backend;
3504 config[no_regs++] = adev->gfx.config.sc_hiz_tile_fifo_size;
3505 config[no_regs++] = adev->gfx.config.sc_earlyz_tile_fifo_size;
3506 config[no_regs++] = adev->gfx.config.num_tile_pipes;
3507 config[no_regs++] = adev->gfx.config.backend_enable_mask;
3508 config[no_regs++] = adev->gfx.config.mem_max_burst_length_bytes;
3509 config[no_regs++] = adev->gfx.config.mem_row_size_in_kb;
3510 config[no_regs++] = adev->gfx.config.shader_engine_tile_size;
3511 config[no_regs++] = adev->gfx.config.num_gpus;
3512 config[no_regs++] = adev->gfx.config.multi_gpu_tile_size;
3513 config[no_regs++] = adev->gfx.config.mc_arb_ramcfg;
3514 config[no_regs++] = adev->gfx.config.gb_addr_config;
3515 config[no_regs++] = adev->gfx.config.num_rbs;
3516
Tom St Denis89a8f302016-08-12 15:14:31 -04003517 /* rev==1 */
3518 config[no_regs++] = adev->rev_id;
3519 config[no_regs++] = adev->pg_flags;
3520 config[no_regs++] = adev->cg_flags;
3521
Tom St Denise9f11dc2016-08-17 12:00:51 -04003522 /* rev==2 */
3523 config[no_regs++] = adev->family;
3524 config[no_regs++] = adev->external_rev_id;
3525
Tom St Denis9a999352017-01-18 13:01:25 -05003526 /* rev==3 */
3527 config[no_regs++] = adev->pdev->device;
3528 config[no_regs++] = adev->pdev->revision;
3529 config[no_regs++] = adev->pdev->subsystem_device;
3530 config[no_regs++] = adev->pdev->subsystem_vendor;
3531
Tom St Denis1e051412016-06-27 09:57:18 -04003532 while (size && (*pos < no_regs * 4)) {
3533 uint32_t value;
3534
3535 value = config[*pos >> 2];
3536 r = put_user(value, (uint32_t *)buf);
3537 if (r) {
3538 kfree(config);
3539 return r;
3540 }
3541
3542 result += 4;
3543 buf += 4;
3544 *pos += 4;
3545 size -= 4;
3546 }
3547
3548 kfree(config);
3549 return result;
3550}
3551
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003552static ssize_t amdgpu_debugfs_sensor_read(struct file *f, char __user *buf,
3553 size_t size, loff_t *pos)
3554{
Al Viro45063092016-12-04 18:24:56 -05003555 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003556 int idx, x, outsize, r, valuesize;
3557 uint32_t values[16];
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003558
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003559 if (size & 3 || *pos & 0x3)
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003560 return -EINVAL;
3561
Samuel Pitoiset3cbc6142017-02-15 19:32:29 +01003562 if (amdgpu_dpm == 0)
3563 return -EINVAL;
3564
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003565 /* convert offset to sensor number */
3566 idx = *pos >> 2;
3567
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003568 valuesize = sizeof(values);
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003569 if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->read_sensor)
Rex Zhucd4d7462017-09-06 18:43:52 +08003570 r = amdgpu_dpm_read_sensor(adev, idx, &values[0], &valuesize);
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003571 else
3572 return -EINVAL;
3573
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003574 if (size > valuesize)
3575 return -EINVAL;
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003576
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003577 outsize = 0;
3578 x = 0;
3579 if (!r) {
3580 while (size) {
3581 r = put_user(values[x++], (int32_t *)buf);
3582 buf += 4;
3583 size -= 4;
3584 outsize += 4;
3585 }
3586 }
3587
3588 return !r ? outsize : r;
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003589}
Tom St Denis1e051412016-06-27 09:57:18 -04003590
Tom St Denis273d7aa2016-10-11 14:48:55 -04003591static ssize_t amdgpu_debugfs_wave_read(struct file *f, char __user *buf,
3592 size_t size, loff_t *pos)
3593{
3594 struct amdgpu_device *adev = f->f_inode->i_private;
3595 int r, x;
3596 ssize_t result=0;
Tom St Denis472259f2016-10-14 09:49:09 -04003597 uint32_t offset, se, sh, cu, wave, simd, data[32];
Tom St Denis273d7aa2016-10-11 14:48:55 -04003598
3599 if (size & 3 || *pos & 3)
3600 return -EINVAL;
3601
3602 /* decode offset */
3603 offset = (*pos & 0x7F);
3604 se = ((*pos >> 7) & 0xFF);
3605 sh = ((*pos >> 15) & 0xFF);
3606 cu = ((*pos >> 23) & 0xFF);
3607 wave = ((*pos >> 31) & 0xFF);
3608 simd = ((*pos >> 37) & 0xFF);
Tom St Denis273d7aa2016-10-11 14:48:55 -04003609
3610 /* switch to the specific se/sh/cu */
3611 mutex_lock(&adev->grbm_idx_mutex);
3612 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
3613
3614 x = 0;
Tom St Denis472259f2016-10-14 09:49:09 -04003615 if (adev->gfx.funcs->read_wave_data)
3616 adev->gfx.funcs->read_wave_data(adev, simd, wave, data, &x);
Tom St Denis273d7aa2016-10-11 14:48:55 -04003617
3618 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
3619 mutex_unlock(&adev->grbm_idx_mutex);
3620
Tom St Denis5ecfb3b2016-10-13 12:15:03 -04003621 if (!x)
3622 return -EINVAL;
3623
Tom St Denis472259f2016-10-14 09:49:09 -04003624 while (size && (offset < x * 4)) {
Tom St Denis273d7aa2016-10-11 14:48:55 -04003625 uint32_t value;
3626
Tom St Denis472259f2016-10-14 09:49:09 -04003627 value = data[offset >> 2];
Tom St Denis273d7aa2016-10-11 14:48:55 -04003628 r = put_user(value, (uint32_t *)buf);
3629 if (r)
3630 return r;
3631
3632 result += 4;
3633 buf += 4;
Tom St Denis472259f2016-10-14 09:49:09 -04003634 offset += 4;
Tom St Denis273d7aa2016-10-11 14:48:55 -04003635 size -= 4;
3636 }
3637
3638 return result;
3639}
3640
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003641static ssize_t amdgpu_debugfs_gpr_read(struct file *f, char __user *buf,
3642 size_t size, loff_t *pos)
3643{
3644 struct amdgpu_device *adev = f->f_inode->i_private;
3645 int r;
3646 ssize_t result = 0;
3647 uint32_t offset, se, sh, cu, wave, simd, thread, bank, *data;
3648
3649 if (size & 3 || *pos & 3)
3650 return -EINVAL;
3651
3652 /* decode offset */
3653 offset = (*pos & 0xFFF); /* in dwords */
3654 se = ((*pos >> 12) & 0xFF);
3655 sh = ((*pos >> 20) & 0xFF);
3656 cu = ((*pos >> 28) & 0xFF);
3657 wave = ((*pos >> 36) & 0xFF);
3658 simd = ((*pos >> 44) & 0xFF);
3659 thread = ((*pos >> 52) & 0xFF);
3660 bank = ((*pos >> 60) & 1);
3661
3662 data = kmalloc_array(1024, sizeof(*data), GFP_KERNEL);
3663 if (!data)
3664 return -ENOMEM;
3665
3666 /* switch to the specific se/sh/cu */
3667 mutex_lock(&adev->grbm_idx_mutex);
3668 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
3669
3670 if (bank == 0) {
3671 if (adev->gfx.funcs->read_wave_vgprs)
3672 adev->gfx.funcs->read_wave_vgprs(adev, simd, wave, thread, offset, size>>2, data);
3673 } else {
3674 if (adev->gfx.funcs->read_wave_sgprs)
3675 adev->gfx.funcs->read_wave_sgprs(adev, simd, wave, offset, size>>2, data);
3676 }
3677
3678 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
3679 mutex_unlock(&adev->grbm_idx_mutex);
3680
3681 while (size) {
3682 uint32_t value;
3683
3684 value = data[offset++];
3685 r = put_user(value, (uint32_t *)buf);
3686 if (r) {
3687 result = r;
3688 goto err;
3689 }
3690
3691 result += 4;
3692 buf += 4;
3693 size -= 4;
3694 }
3695
3696err:
3697 kfree(data);
3698 return result;
3699}
3700
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003701static const struct file_operations amdgpu_debugfs_regs_fops = {
3702 .owner = THIS_MODULE,
3703 .read = amdgpu_debugfs_regs_read,
3704 .write = amdgpu_debugfs_regs_write,
3705 .llseek = default_llseek
3706};
Tom St Denisadcec282016-04-15 13:08:44 -04003707static const struct file_operations amdgpu_debugfs_regs_didt_fops = {
3708 .owner = THIS_MODULE,
3709 .read = amdgpu_debugfs_regs_didt_read,
3710 .write = amdgpu_debugfs_regs_didt_write,
3711 .llseek = default_llseek
3712};
3713static const struct file_operations amdgpu_debugfs_regs_pcie_fops = {
3714 .owner = THIS_MODULE,
3715 .read = amdgpu_debugfs_regs_pcie_read,
3716 .write = amdgpu_debugfs_regs_pcie_write,
3717 .llseek = default_llseek
3718};
3719static const struct file_operations amdgpu_debugfs_regs_smc_fops = {
3720 .owner = THIS_MODULE,
3721 .read = amdgpu_debugfs_regs_smc_read,
3722 .write = amdgpu_debugfs_regs_smc_write,
3723 .llseek = default_llseek
3724};
3725
Tom St Denis1e051412016-06-27 09:57:18 -04003726static const struct file_operations amdgpu_debugfs_gca_config_fops = {
3727 .owner = THIS_MODULE,
3728 .read = amdgpu_debugfs_gca_config_read,
3729 .llseek = default_llseek
3730};
3731
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003732static const struct file_operations amdgpu_debugfs_sensors_fops = {
3733 .owner = THIS_MODULE,
3734 .read = amdgpu_debugfs_sensor_read,
3735 .llseek = default_llseek
3736};
3737
Tom St Denis273d7aa2016-10-11 14:48:55 -04003738static const struct file_operations amdgpu_debugfs_wave_fops = {
3739 .owner = THIS_MODULE,
3740 .read = amdgpu_debugfs_wave_read,
3741 .llseek = default_llseek
3742};
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003743static const struct file_operations amdgpu_debugfs_gpr_fops = {
3744 .owner = THIS_MODULE,
3745 .read = amdgpu_debugfs_gpr_read,
3746 .llseek = default_llseek
3747};
Tom St Denis273d7aa2016-10-11 14:48:55 -04003748
Tom St Denisadcec282016-04-15 13:08:44 -04003749static const struct file_operations *debugfs_regs[] = {
3750 &amdgpu_debugfs_regs_fops,
3751 &amdgpu_debugfs_regs_didt_fops,
3752 &amdgpu_debugfs_regs_pcie_fops,
3753 &amdgpu_debugfs_regs_smc_fops,
Tom St Denis1e051412016-06-27 09:57:18 -04003754 &amdgpu_debugfs_gca_config_fops,
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003755 &amdgpu_debugfs_sensors_fops,
Tom St Denis273d7aa2016-10-11 14:48:55 -04003756 &amdgpu_debugfs_wave_fops,
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003757 &amdgpu_debugfs_gpr_fops,
Tom St Denisadcec282016-04-15 13:08:44 -04003758};
3759
3760static const char *debugfs_regs_names[] = {
3761 "amdgpu_regs",
3762 "amdgpu_regs_didt",
3763 "amdgpu_regs_pcie",
3764 "amdgpu_regs_smc",
Tom St Denis1e051412016-06-27 09:57:18 -04003765 "amdgpu_gca_config",
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003766 "amdgpu_sensors",
Tom St Denis273d7aa2016-10-11 14:48:55 -04003767 "amdgpu_wave",
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003768 "amdgpu_gpr",
Tom St Denisadcec282016-04-15 13:08:44 -04003769};
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003770
3771static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
3772{
3773 struct drm_minor *minor = adev->ddev->primary;
3774 struct dentry *ent, *root = minor->debugfs_root;
Tom St Denisadcec282016-04-15 13:08:44 -04003775 unsigned i, j;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003776
Tom St Denisadcec282016-04-15 13:08:44 -04003777 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3778 ent = debugfs_create_file(debugfs_regs_names[i],
3779 S_IFREG | S_IRUGO, root,
3780 adev, debugfs_regs[i]);
3781 if (IS_ERR(ent)) {
3782 for (j = 0; j < i; j++) {
3783 debugfs_remove(adev->debugfs_regs[i]);
3784 adev->debugfs_regs[i] = NULL;
3785 }
3786 return PTR_ERR(ent);
3787 }
3788
3789 if (!i)
3790 i_size_write(ent->d_inode, adev->rmmio_size);
3791 adev->debugfs_regs[i] = ent;
3792 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003793
3794 return 0;
3795}
3796
3797static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev)
3798{
Tom St Denisadcec282016-04-15 13:08:44 -04003799 unsigned i;
3800
3801 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3802 if (adev->debugfs_regs[i]) {
3803 debugfs_remove(adev->debugfs_regs[i]);
3804 adev->debugfs_regs[i] = NULL;
3805 }
3806 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003807}
3808
Huang Rui4f0955f2017-05-10 23:04:06 +08003809static int amdgpu_debugfs_test_ib(struct seq_file *m, void *data)
3810{
3811 struct drm_info_node *node = (struct drm_info_node *) m->private;
3812 struct drm_device *dev = node->minor->dev;
3813 struct amdgpu_device *adev = dev->dev_private;
3814 int r = 0, i;
3815
3816 /* hold on the scheduler */
3817 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
3818 struct amdgpu_ring *ring = adev->rings[i];
3819
3820 if (!ring || !ring->sched.thread)
3821 continue;
3822 kthread_park(ring->sched.thread);
3823 }
3824
3825 seq_printf(m, "run ib test:\n");
3826 r = amdgpu_ib_ring_tests(adev);
3827 if (r)
3828 seq_printf(m, "ib ring tests failed (%d).\n", r);
3829 else
3830 seq_printf(m, "ib ring tests passed.\n");
3831
3832 /* go on the scheduler */
3833 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
3834 struct amdgpu_ring *ring = adev->rings[i];
3835
3836 if (!ring || !ring->sched.thread)
3837 continue;
3838 kthread_unpark(ring->sched.thread);
3839 }
3840
3841 return 0;
3842}
3843
3844static const struct drm_info_list amdgpu_debugfs_test_ib_ring_list[] = {
3845 {"amdgpu_test_ib", &amdgpu_debugfs_test_ib}
3846};
3847
3848static int amdgpu_debugfs_test_ib_ring_init(struct amdgpu_device *adev)
3849{
3850 return amdgpu_debugfs_add_files(adev,
3851 amdgpu_debugfs_test_ib_ring_list, 1);
3852}
3853
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003854int amdgpu_debugfs_init(struct drm_minor *minor)
3855{
3856 return 0;
3857}
Kent Russelldb95e212017-08-22 12:31:43 -04003858
3859static int amdgpu_debugfs_get_vbios_dump(struct seq_file *m, void *data)
3860{
3861 struct drm_info_node *node = (struct drm_info_node *) m->private;
3862 struct drm_device *dev = node->minor->dev;
3863 struct amdgpu_device *adev = dev->dev_private;
3864
3865 seq_write(m, adev->bios, adev->bios_size);
3866 return 0;
3867}
3868
Kent Russelldb95e212017-08-22 12:31:43 -04003869static const struct drm_info_list amdgpu_vbios_dump_list[] = {
3870 {"amdgpu_vbios",
3871 amdgpu_debugfs_get_vbios_dump,
3872 0, NULL},
3873};
3874
Kent Russelldb95e212017-08-22 12:31:43 -04003875static int amdgpu_debugfs_vbios_dump_init(struct amdgpu_device *adev)
3876{
3877 return amdgpu_debugfs_add_files(adev,
3878 amdgpu_vbios_dump_list, 1);
3879}
Alexander Kuleshov7cebc722015-06-27 13:16:05 +06003880#else
Arnd Bergmann27bad5b2017-06-21 23:51:02 +02003881static int amdgpu_debugfs_test_ib_ring_init(struct amdgpu_device *adev)
Huang Rui4f0955f2017-05-10 23:04:06 +08003882{
3883 return 0;
3884}
Alexander Kuleshov7cebc722015-06-27 13:16:05 +06003885static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
3886{
3887 return 0;
3888}
Kent Russelldb95e212017-08-22 12:31:43 -04003889static int amdgpu_debugfs_vbios_dump_init(struct amdgpu_device *adev)
3890{
3891 return 0;
3892}
Alexander Kuleshov7cebc722015-06-27 13:16:05 +06003893static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev) { }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003894#endif