blob: 1c5c44acaad2f93a8910f8bb355f51a66a86a75d [file] [log] [blame]
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001/*
2 * Copyright 2008 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 * Copyright 2009 Jerome Glisse.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors: Dave Airlie
25 * Alex Deucher
26 * Jerome Glisse
27 */
Chunming Zhou0875dc92016-06-12 15:41:58 +080028#include <linux/kthread.h>
Alex Deucherd38ceaf2015-04-20 16:55:21 -040029#include <linux/console.h>
30#include <linux/slab.h>
31#include <linux/debugfs.h>
32#include <drm/drmP.h>
33#include <drm/drm_crtc_helper.h>
34#include <drm/amdgpu_drm.h>
35#include <linux/vgaarb.h>
36#include <linux/vga_switcheroo.h>
37#include <linux/efi.h>
38#include "amdgpu.h"
Tom St Denisf4b373f2016-05-31 08:02:27 -040039#include "amdgpu_trace.h"
Alex Deucherd38ceaf2015-04-20 16:55:21 -040040#include "amdgpu_i2c.h"
41#include "atom.h"
42#include "amdgpu_atombios.h"
Alex Deuchera5bde2f2016-09-23 16:23:41 -040043#include "amdgpu_atomfirmware.h"
Alex Deucherd0dd7f02015-11-11 19:45:06 -050044#include "amd_pcie.h"
Ken Wang33f34802016-01-21 17:29:41 +080045#ifdef CONFIG_DRM_AMDGPU_SI
46#include "si.h"
47#endif
Alex Deuchera2e73f52015-04-20 17:09:27 -040048#ifdef CONFIG_DRM_AMDGPU_CIK
49#include "cik.h"
50#endif
Alex Deucheraaa36a92015-04-20 17:31:14 -040051#include "vi.h"
Ken Wang460826e2017-03-06 14:53:16 -050052#include "soc15.h"
Alex Deucherd38ceaf2015-04-20 16:55:21 -040053#include "bif/bif_4_1_d.h"
Emily Deng9accf2f2016-08-10 16:01:25 +080054#include <linux/pci.h>
Monk Liubec86372016-09-14 19:38:08 +080055#include <linux/firmware.h>
Gavin Wan89041942017-06-23 13:55:15 -040056#include "amdgpu_vf_error.h"
Alex Deucherd38ceaf2015-04-20 16:55:21 -040057
Yong Zhaoba997702015-11-09 17:21:45 -050058#include "amdgpu_amdkfd.h"
Alex Deucherd38ceaf2015-04-20 16:55:21 -040059
Alex Deuchere2a75f82017-04-27 16:58:01 -040060MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin");
Alex Deucher2d2e5e72017-05-09 12:27:35 -040061MODULE_FIRMWARE("amdgpu/raven_gpu_info.bin");
Alex Deuchere2a75f82017-04-27 16:58:01 -040062
Shirish S2dc80b02017-05-25 10:05:25 +053063#define AMDGPU_RESUME_MS 2000
64
Alex Deucherd38ceaf2015-04-20 16:55:21 -040065static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev);
66static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev);
Huang Rui4f0955f2017-05-10 23:04:06 +080067static int amdgpu_debugfs_test_ib_ring_init(struct amdgpu_device *adev);
Kent Russelldb95e212017-08-22 12:31:43 -040068static int amdgpu_debugfs_vbios_dump_init(struct amdgpu_device *adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040069
70static const char *amdgpu_asic_name[] = {
Ken Wangda69c1612016-01-21 19:08:55 +080071 "TAHITI",
72 "PITCAIRN",
73 "VERDE",
74 "OLAND",
75 "HAINAN",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040076 "BONAIRE",
77 "KAVERI",
78 "KABINI",
79 "HAWAII",
80 "MULLINS",
81 "TOPAZ",
82 "TONGA",
David Zhang48299f92015-07-08 01:05:16 +080083 "FIJI",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040084 "CARRIZO",
Samuel Li139f4912015-10-08 14:50:27 -040085 "STONEY",
Flora Cui2cc0c0b2016-03-14 18:33:29 -040086 "POLARIS10",
87 "POLARIS11",
Junwei Zhangc4642a42016-12-14 15:32:28 -050088 "POLARIS12",
Ken Wangd4196f02016-03-09 09:28:32 +080089 "VEGA10",
Chunming Zhou2ca8a5d2016-12-07 17:31:19 +080090 "RAVEN",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040091 "LAST",
92};
93
94bool amdgpu_device_is_px(struct drm_device *dev)
95{
96 struct amdgpu_device *adev = dev->dev_private;
97
Jammy Zhou2f7d10b2015-07-22 11:29:01 +080098 if (adev->flags & AMD_IS_PX)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040099 return true;
100 return false;
101}
102
103/*
104 * MMIO register access helper functions.
105 */
106uint32_t amdgpu_mm_rreg(struct amdgpu_device *adev, uint32_t reg,
Monk Liu15d72fd2017-01-25 15:07:40 +0800107 uint32_t acc_flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400108{
Tom St Denisf4b373f2016-05-31 08:02:27 -0400109 uint32_t ret;
110
Monk Liu15d72fd2017-01-25 15:07:40 +0800111 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev)) {
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800112 BUG_ON(in_interrupt());
113 return amdgpu_virt_kiq_rreg(adev, reg);
114 }
115
Monk Liu15d72fd2017-01-25 15:07:40 +0800116 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
Tom St Denisf4b373f2016-05-31 08:02:27 -0400117 ret = readl(((void __iomem *)adev->rmmio) + (reg * 4));
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400118 else {
119 unsigned long flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400120
121 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
122 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
123 ret = readl(((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
124 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400125 }
Tom St Denisf4b373f2016-05-31 08:02:27 -0400126 trace_amdgpu_mm_rreg(adev->pdev->device, reg, ret);
127 return ret;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400128}
129
130void amdgpu_mm_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v,
Monk Liu15d72fd2017-01-25 15:07:40 +0800131 uint32_t acc_flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400132{
Tom St Denisf4b373f2016-05-31 08:02:27 -0400133 trace_amdgpu_mm_wreg(adev->pdev->device, reg, v);
Monk Liu4e99a442016-03-31 13:26:59 +0800134
Ken Wang47ed4e12017-07-04 13:11:52 +0800135 if (adev->asic_type >= CHIP_VEGA10 && reg == 0) {
136 adev->last_mm_index = v;
137 }
138
Monk Liu15d72fd2017-01-25 15:07:40 +0800139 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev)) {
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800140 BUG_ON(in_interrupt());
141 return amdgpu_virt_kiq_wreg(adev, reg, v);
142 }
143
Monk Liu15d72fd2017-01-25 15:07:40 +0800144 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400145 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
146 else {
147 unsigned long flags;
148
149 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
150 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
151 writel(v, ((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
152 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
153 }
Ken Wang47ed4e12017-07-04 13:11:52 +0800154
155 if (adev->asic_type >= CHIP_VEGA10 && reg == 1 && adev->last_mm_index == 0x5702C) {
156 udelay(500);
157 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400158}
159
160u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 reg)
161{
162 if ((reg * 4) < adev->rio_mem_size)
163 return ioread32(adev->rio_mem + (reg * 4));
164 else {
165 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
166 return ioread32(adev->rio_mem + (mmMM_DATA * 4));
167 }
168}
169
170void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
171{
Ken Wang47ed4e12017-07-04 13:11:52 +0800172 if (adev->asic_type >= CHIP_VEGA10 && reg == 0) {
173 adev->last_mm_index = v;
174 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400175
176 if ((reg * 4) < adev->rio_mem_size)
177 iowrite32(v, adev->rio_mem + (reg * 4));
178 else {
179 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
180 iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
181 }
Ken Wang47ed4e12017-07-04 13:11:52 +0800182
183 if (adev->asic_type >= CHIP_VEGA10 && reg == 1 && adev->last_mm_index == 0x5702C) {
184 udelay(500);
185 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400186}
187
188/**
189 * amdgpu_mm_rdoorbell - read a doorbell dword
190 *
191 * @adev: amdgpu_device pointer
192 * @index: doorbell index
193 *
194 * Returns the value in the doorbell aperture at the
195 * requested doorbell index (CIK).
196 */
197u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
198{
199 if (index < adev->doorbell.num_doorbells) {
200 return readl(adev->doorbell.ptr + index);
201 } else {
202 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
203 return 0;
204 }
205}
206
207/**
208 * amdgpu_mm_wdoorbell - write a doorbell dword
209 *
210 * @adev: amdgpu_device pointer
211 * @index: doorbell index
212 * @v: value to write
213 *
214 * Writes @v to the doorbell aperture at the
215 * requested doorbell index (CIK).
216 */
217void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
218{
219 if (index < adev->doorbell.num_doorbells) {
220 writel(v, adev->doorbell.ptr + index);
221 } else {
222 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
223 }
224}
225
226/**
Ken Wang832be402016-03-18 15:23:08 +0800227 * amdgpu_mm_rdoorbell64 - read a doorbell Qword
228 *
229 * @adev: amdgpu_device pointer
230 * @index: doorbell index
231 *
232 * Returns the value in the doorbell aperture at the
233 * requested doorbell index (VEGA10+).
234 */
235u64 amdgpu_mm_rdoorbell64(struct amdgpu_device *adev, u32 index)
236{
237 if (index < adev->doorbell.num_doorbells) {
238 return atomic64_read((atomic64_t *)(adev->doorbell.ptr + index));
239 } else {
240 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
241 return 0;
242 }
243}
244
245/**
246 * amdgpu_mm_wdoorbell64 - write a doorbell Qword
247 *
248 * @adev: amdgpu_device pointer
249 * @index: doorbell index
250 * @v: value to write
251 *
252 * Writes @v to the doorbell aperture at the
253 * requested doorbell index (VEGA10+).
254 */
255void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v)
256{
257 if (index < adev->doorbell.num_doorbells) {
258 atomic64_set((atomic64_t *)(adev->doorbell.ptr + index), v);
259 } else {
260 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
261 }
262}
263
264/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400265 * amdgpu_invalid_rreg - dummy reg read function
266 *
267 * @adev: amdgpu device pointer
268 * @reg: offset of register
269 *
270 * Dummy register read function. Used for register blocks
271 * that certain asics don't have (all asics).
272 * Returns the value in the register.
273 */
274static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
275{
276 DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
277 BUG();
278 return 0;
279}
280
281/**
282 * amdgpu_invalid_wreg - dummy reg write function
283 *
284 * @adev: amdgpu device pointer
285 * @reg: offset of register
286 * @v: value to write to the register
287 *
288 * Dummy register read function. Used for register blocks
289 * that certain asics don't have (all asics).
290 */
291static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
292{
293 DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
294 reg, v);
295 BUG();
296}
297
298/**
299 * amdgpu_block_invalid_rreg - dummy reg read function
300 *
301 * @adev: amdgpu device pointer
302 * @block: offset of instance
303 * @reg: offset of register
304 *
305 * Dummy register read function. Used for register blocks
306 * that certain asics don't have (all asics).
307 * Returns the value in the register.
308 */
309static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
310 uint32_t block, uint32_t reg)
311{
312 DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
313 reg, block);
314 BUG();
315 return 0;
316}
317
318/**
319 * amdgpu_block_invalid_wreg - dummy reg write function
320 *
321 * @adev: amdgpu device pointer
322 * @block: offset of instance
323 * @reg: offset of register
324 * @v: value to write to the register
325 *
326 * Dummy register read function. Used for register blocks
327 * that certain asics don't have (all asics).
328 */
329static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
330 uint32_t block,
331 uint32_t reg, uint32_t v)
332{
333 DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
334 reg, block, v);
335 BUG();
336}
337
338static int amdgpu_vram_scratch_init(struct amdgpu_device *adev)
339{
Christian Königa4a02772017-07-27 17:24:36 +0200340 return amdgpu_bo_create_kernel(adev, AMDGPU_GPU_PAGE_SIZE,
341 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
342 &adev->vram_scratch.robj,
343 &adev->vram_scratch.gpu_addr,
344 (void **)&adev->vram_scratch.ptr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400345}
346
347static void amdgpu_vram_scratch_fini(struct amdgpu_device *adev)
348{
Christian König078af1a2017-07-27 17:43:00 +0200349 amdgpu_bo_free_kernel(&adev->vram_scratch.robj, NULL, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400350}
351
352/**
353 * amdgpu_program_register_sequence - program an array of registers.
354 *
355 * @adev: amdgpu_device pointer
356 * @registers: pointer to the register array
357 * @array_size: size of the register array
358 *
359 * Programs an array or registers with and and or masks.
360 * This is a helper for setting golden registers.
361 */
362void amdgpu_program_register_sequence(struct amdgpu_device *adev,
363 const u32 *registers,
364 const u32 array_size)
365{
366 u32 tmp, reg, and_mask, or_mask;
367 int i;
368
369 if (array_size % 3)
370 return;
371
372 for (i = 0; i < array_size; i +=3) {
373 reg = registers[i + 0];
374 and_mask = registers[i + 1];
375 or_mask = registers[i + 2];
376
377 if (and_mask == 0xffffffff) {
378 tmp = or_mask;
379 } else {
380 tmp = RREG32(reg);
381 tmp &= ~and_mask;
382 tmp |= or_mask;
383 }
384 WREG32(reg, tmp);
385 }
386}
387
388void amdgpu_pci_config_reset(struct amdgpu_device *adev)
389{
390 pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
391}
392
393/*
394 * GPU doorbell aperture helpers function.
395 */
396/**
397 * amdgpu_doorbell_init - Init doorbell driver information.
398 *
399 * @adev: amdgpu_device pointer
400 *
401 * Init doorbell driver information (CIK)
402 * Returns 0 on success, error on failure.
403 */
404static int amdgpu_doorbell_init(struct amdgpu_device *adev)
405{
406 /* doorbell bar mapping */
407 adev->doorbell.base = pci_resource_start(adev->pdev, 2);
408 adev->doorbell.size = pci_resource_len(adev->pdev, 2);
409
Christian Königedf600d2016-05-03 15:54:54 +0200410 adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400411 AMDGPU_DOORBELL_MAX_ASSIGNMENT+1);
412 if (adev->doorbell.num_doorbells == 0)
413 return -EINVAL;
414
Christian König8972e5d2017-03-06 13:34:57 +0100415 adev->doorbell.ptr = ioremap(adev->doorbell.base,
416 adev->doorbell.num_doorbells *
417 sizeof(u32));
418 if (adev->doorbell.ptr == NULL)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400419 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400420
421 return 0;
422}
423
424/**
425 * amdgpu_doorbell_fini - Tear down doorbell driver information.
426 *
427 * @adev: amdgpu_device pointer
428 *
429 * Tear down doorbell driver information (CIK)
430 */
431static void amdgpu_doorbell_fini(struct amdgpu_device *adev)
432{
433 iounmap(adev->doorbell.ptr);
434 adev->doorbell.ptr = NULL;
435}
436
437/**
438 * amdgpu_doorbell_get_kfd_info - Report doorbell configuration required to
439 * setup amdkfd
440 *
441 * @adev: amdgpu_device pointer
442 * @aperture_base: output returning doorbell aperture base physical address
443 * @aperture_size: output returning doorbell aperture size in bytes
444 * @start_offset: output returning # of doorbell bytes reserved for amdgpu.
445 *
446 * amdgpu and amdkfd share the doorbell aperture. amdgpu sets it up,
447 * takes doorbells required for its own rings and reports the setup to amdkfd.
448 * amdgpu reserved doorbells are at the start of the doorbell aperture.
449 */
450void amdgpu_doorbell_get_kfd_info(struct amdgpu_device *adev,
451 phys_addr_t *aperture_base,
452 size_t *aperture_size,
453 size_t *start_offset)
454{
455 /*
456 * The first num_doorbells are used by amdgpu.
457 * amdkfd takes whatever's left in the aperture.
458 */
459 if (adev->doorbell.size > adev->doorbell.num_doorbells * sizeof(u32)) {
460 *aperture_base = adev->doorbell.base;
461 *aperture_size = adev->doorbell.size;
462 *start_offset = adev->doorbell.num_doorbells * sizeof(u32);
463 } else {
464 *aperture_base = 0;
465 *aperture_size = 0;
466 *start_offset = 0;
467 }
468}
469
470/*
471 * amdgpu_wb_*()
Alex Xie455a7bc2017-05-08 21:36:03 -0400472 * Writeback is the method by which the GPU updates special pages in memory
Alex Xieea81a172017-05-08 13:41:11 -0400473 * with the status of certain GPU events (fences, ring pointers,etc.).
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400474 */
475
476/**
477 * amdgpu_wb_fini - Disable Writeback and free memory
478 *
479 * @adev: amdgpu_device pointer
480 *
481 * Disables Writeback and frees the Writeback memory (all asics).
482 * Used at driver shutdown.
483 */
484static void amdgpu_wb_fini(struct amdgpu_device *adev)
485{
486 if (adev->wb.wb_obj) {
Alex Deuchera76ed482016-10-21 15:30:36 -0400487 amdgpu_bo_free_kernel(&adev->wb.wb_obj,
488 &adev->wb.gpu_addr,
489 (void **)&adev->wb.wb);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400490 adev->wb.wb_obj = NULL;
491 }
492}
493
494/**
495 * amdgpu_wb_init- Init Writeback driver info and allocate memory
496 *
497 * @adev: amdgpu_device pointer
498 *
Alex Xie455a7bc2017-05-08 21:36:03 -0400499 * Initializes writeback and allocates writeback memory (all asics).
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400500 * Used at driver startup.
501 * Returns 0 on success or an -error on failure.
502 */
503static int amdgpu_wb_init(struct amdgpu_device *adev)
504{
505 int r;
506
507 if (adev->wb.wb_obj == NULL) {
Alex Deucher97407b62017-07-28 12:14:15 -0400508 /* AMDGPU_MAX_WB * sizeof(uint32_t) * 8 = AMDGPU_MAX_WB 256bit slots */
509 r = amdgpu_bo_create_kernel(adev, AMDGPU_MAX_WB * sizeof(uint32_t) * 8,
Alex Deuchera76ed482016-10-21 15:30:36 -0400510 PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
511 &adev->wb.wb_obj, &adev->wb.gpu_addr,
512 (void **)&adev->wb.wb);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400513 if (r) {
514 dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
515 return r;
516 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400517
518 adev->wb.num_wb = AMDGPU_MAX_WB;
519 memset(&adev->wb.used, 0, sizeof(adev->wb.used));
520
521 /* clear wb memory */
Huang Rui60a970a62017-03-15 10:13:32 +0800522 memset((char *)adev->wb.wb, 0, AMDGPU_MAX_WB * sizeof(uint32_t));
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400523 }
524
525 return 0;
526}
527
528/**
529 * amdgpu_wb_get - Allocate a wb entry
530 *
531 * @adev: amdgpu_device pointer
532 * @wb: wb index
533 *
534 * Allocate a wb slot for use by the driver (all asics).
535 * Returns 0 on success or -EINVAL on failure.
536 */
537int amdgpu_wb_get(struct amdgpu_device *adev, u32 *wb)
538{
539 unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
Alex Deucher97407b62017-07-28 12:14:15 -0400540
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400541 if (offset < adev->wb.num_wb) {
542 __set_bit(offset, adev->wb.used);
Alex Deucher97407b62017-07-28 12:14:15 -0400543 *wb = offset * 8; /* convert to dw offset */
Monk Liu0915fdb2017-06-19 10:19:41 -0400544 return 0;
545 } else {
546 return -EINVAL;
547 }
548}
549
Ken Wang70142852016-03-18 15:08:49 +0800550/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400551 * amdgpu_wb_free - Free a wb entry
552 *
553 * @adev: amdgpu_device pointer
554 * @wb: wb index
555 *
556 * Free a wb slot allocated for use by the driver (all asics)
557 */
558void amdgpu_wb_free(struct amdgpu_device *adev, u32 wb)
559{
560 if (wb < adev->wb.num_wb)
561 __clear_bit(wb, adev->wb.used);
562}
563
564/**
565 * amdgpu_vram_location - try to find VRAM location
566 * @adev: amdgpu device structure holding all necessary informations
567 * @mc: memory controller structure holding memory informations
568 * @base: base address at which to put VRAM
569 *
Alex Xie455a7bc2017-05-08 21:36:03 -0400570 * Function will try to place VRAM at base address provided
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400571 * as parameter (which is so far either PCI aperture address or
572 * for IGP TOM base address).
573 *
574 * If there is not enough space to fit the unvisible VRAM in the 32bits
575 * address space then we limit the VRAM size to the aperture.
576 *
577 * Note: We don't explicitly enforce VRAM start to be aligned on VRAM size,
578 * this shouldn't be a problem as we are using the PCI aperture as a reference.
579 * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but
580 * not IGP.
581 *
582 * Note: we use mc_vram_size as on some board we need to program the mc to
583 * cover the whole aperture even if VRAM size is inferior to aperture size
584 * Novell bug 204882 + along with lots of ubuntu ones
585 *
586 * Note: when limiting vram it's safe to overwritte real_vram_size because
587 * we are not in case where real_vram_size is inferior to mc_vram_size (ie
588 * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu
589 * ones)
590 *
591 * Note: IGP TOM addr should be the same as the aperture addr, we don't
Alex Xie455a7bc2017-05-08 21:36:03 -0400592 * explicitly check for that though.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400593 *
594 * FIXME: when reducing VRAM size align new size on power of 2.
595 */
596void amdgpu_vram_location(struct amdgpu_device *adev, struct amdgpu_mc *mc, u64 base)
597{
598 uint64_t limit = (uint64_t)amdgpu_vram_limit << 20;
599
600 mc->vram_start = base;
601 if (mc->mc_vram_size > (adev->mc.mc_mask - base + 1)) {
602 dev_warn(adev->dev, "limiting VRAM to PCI aperture size\n");
603 mc->real_vram_size = mc->aper_size;
604 mc->mc_vram_size = mc->aper_size;
605 }
606 mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
607 if (limit && limit < mc->real_vram_size)
608 mc->real_vram_size = limit;
609 dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
610 mc->mc_vram_size >> 20, mc->vram_start,
611 mc->vram_end, mc->real_vram_size >> 20);
612}
613
614/**
Christian König6f02a692017-07-07 11:56:59 +0200615 * amdgpu_gart_location - try to find GTT location
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400616 * @adev: amdgpu device structure holding all necessary informations
617 * @mc: memory controller structure holding memory informations
618 *
619 * Function will place try to place GTT before or after VRAM.
620 *
621 * If GTT size is bigger than space left then we ajust GTT size.
622 * Thus function will never fails.
623 *
624 * FIXME: when reducing GTT size align new size on power of 2.
625 */
Christian König6f02a692017-07-07 11:56:59 +0200626void amdgpu_gart_location(struct amdgpu_device *adev, struct amdgpu_mc *mc)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400627{
628 u64 size_af, size_bf;
629
Christian Königed21c042017-07-06 22:26:05 +0200630 size_af = adev->mc.mc_mask - mc->vram_end;
631 size_bf = mc->vram_start;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400632 if (size_bf > size_af) {
Christian König6f02a692017-07-07 11:56:59 +0200633 if (mc->gart_size > size_bf) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400634 dev_warn(adev->dev, "limiting GTT\n");
Christian König6f02a692017-07-07 11:56:59 +0200635 mc->gart_size = size_bf;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400636 }
Christian König6f02a692017-07-07 11:56:59 +0200637 mc->gart_start = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400638 } else {
Christian König6f02a692017-07-07 11:56:59 +0200639 if (mc->gart_size > size_af) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400640 dev_warn(adev->dev, "limiting GTT\n");
Christian König6f02a692017-07-07 11:56:59 +0200641 mc->gart_size = size_af;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400642 }
Christian König6f02a692017-07-07 11:56:59 +0200643 mc->gart_start = mc->vram_end + 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400644 }
Christian König6f02a692017-07-07 11:56:59 +0200645 mc->gart_end = mc->gart_start + mc->gart_size - 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400646 dev_info(adev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
Christian König6f02a692017-07-07 11:56:59 +0200647 mc->gart_size >> 20, mc->gart_start, mc->gart_end);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400648}
649
650/*
651 * GPU helpers function.
652 */
653/**
Jim Quc836fec2017-02-10 15:59:59 +0800654 * amdgpu_need_post - check if the hw need post or not
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400655 *
656 * @adev: amdgpu_device pointer
657 *
Jim Quc836fec2017-02-10 15:59:59 +0800658 * Check if the asic has been initialized (all asics) at driver startup
659 * or post is needed if hw reset is performed.
660 * Returns true if need or false if not.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400661 */
Jim Quc836fec2017-02-10 15:59:59 +0800662bool amdgpu_need_post(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400663{
664 uint32_t reg;
665
Jim Quc836fec2017-02-10 15:59:59 +0800666 if (adev->has_hw_reset) {
667 adev->has_hw_reset = false;
668 return true;
669 }
Alex Deucher70d17a22017-06-30 17:26:47 -0400670
671 /* bios scratch used on CIK+ */
672 if (adev->asic_type >= CHIP_BONAIRE)
673 return amdgpu_atombios_scratch_need_asic_init(adev);
674
675 /* check MEM_SIZE for older asics */
Alex Deucherbbf282d2017-03-03 17:26:10 -0500676 reg = amdgpu_asic_get_config_memsize(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400677
Alex Deucherf2713e82017-03-28 12:19:31 -0400678 if ((reg != 0) && (reg != 0xffffffff))
Jim Quc836fec2017-02-10 15:59:59 +0800679 return false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400680
Jim Quc836fec2017-02-10 15:59:59 +0800681 return true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400682
683}
684
Monk Liubec86372016-09-14 19:38:08 +0800685static bool amdgpu_vpost_needed(struct amdgpu_device *adev)
686{
687 if (amdgpu_sriov_vf(adev))
688 return false;
689
690 if (amdgpu_passthrough(adev)) {
Monk Liu1da2c322016-11-11 11:24:29 +0800691 /* for FIJI: In whole GPU pass-through virtualization case, after VM reboot
692 * some old smc fw still need driver do vPost otherwise gpu hang, while
693 * those smc fw version above 22.15 doesn't have this flaw, so we force
694 * vpost executed for smc version below 22.15
Monk Liubec86372016-09-14 19:38:08 +0800695 */
696 if (adev->asic_type == CHIP_FIJI) {
697 int err;
698 uint32_t fw_ver;
699 err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
700 /* force vPost if error occured */
701 if (err)
702 return true;
703
704 fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
Monk Liu1da2c322016-11-11 11:24:29 +0800705 if (fw_ver < 0x00160e00)
706 return true;
Monk Liubec86372016-09-14 19:38:08 +0800707 }
Monk Liubec86372016-09-14 19:38:08 +0800708 }
Jim Quc836fec2017-02-10 15:59:59 +0800709 return amdgpu_need_post(adev);
Monk Liubec86372016-09-14 19:38:08 +0800710}
711
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400712/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400713 * amdgpu_dummy_page_init - init dummy page used by the driver
714 *
715 * @adev: amdgpu_device pointer
716 *
717 * Allocate the dummy page used by the driver (all asics).
718 * This dummy page is used by the driver as a filler for gart entries
719 * when pages are taken out of the GART
720 * Returns 0 on sucess, -ENOMEM on failure.
721 */
722int amdgpu_dummy_page_init(struct amdgpu_device *adev)
723{
724 if (adev->dummy_page.page)
725 return 0;
726 adev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
727 if (adev->dummy_page.page == NULL)
728 return -ENOMEM;
729 adev->dummy_page.addr = pci_map_page(adev->pdev, adev->dummy_page.page,
730 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
731 if (pci_dma_mapping_error(adev->pdev, adev->dummy_page.addr)) {
732 dev_err(&adev->pdev->dev, "Failed to DMA MAP the dummy page\n");
733 __free_page(adev->dummy_page.page);
734 adev->dummy_page.page = NULL;
735 return -ENOMEM;
736 }
737 return 0;
738}
739
740/**
741 * amdgpu_dummy_page_fini - free dummy page used by the driver
742 *
743 * @adev: amdgpu_device pointer
744 *
745 * Frees the dummy page used by the driver (all asics).
746 */
747void amdgpu_dummy_page_fini(struct amdgpu_device *adev)
748{
749 if (adev->dummy_page.page == NULL)
750 return;
751 pci_unmap_page(adev->pdev, adev->dummy_page.addr,
752 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
753 __free_page(adev->dummy_page.page);
754 adev->dummy_page.page = NULL;
755}
756
757
758/* ATOM accessor methods */
759/*
760 * ATOM is an interpreted byte code stored in tables in the vbios. The
761 * driver registers callbacks to access registers and the interpreter
762 * in the driver parses the tables and executes then to program specific
763 * actions (set display modes, asic init, etc.). See amdgpu_atombios.c,
764 * atombios.h, and atom.c
765 */
766
767/**
768 * cail_pll_read - read PLL register
769 *
770 * @info: atom card_info pointer
771 * @reg: PLL register offset
772 *
773 * Provides a PLL register accessor for the atom interpreter (r4xx+).
774 * Returns the value of the PLL register.
775 */
776static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
777{
778 return 0;
779}
780
781/**
782 * cail_pll_write - write PLL register
783 *
784 * @info: atom card_info pointer
785 * @reg: PLL register offset
786 * @val: value to write to the pll register
787 *
788 * Provides a PLL register accessor for the atom interpreter (r4xx+).
789 */
790static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
791{
792
793}
794
795/**
796 * cail_mc_read - read MC (Memory Controller) register
797 *
798 * @info: atom card_info pointer
799 * @reg: MC register offset
800 *
801 * Provides an MC register accessor for the atom interpreter (r4xx+).
802 * Returns the value of the MC register.
803 */
804static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
805{
806 return 0;
807}
808
809/**
810 * cail_mc_write - write MC (Memory Controller) register
811 *
812 * @info: atom card_info pointer
813 * @reg: MC register offset
814 * @val: value to write to the pll register
815 *
816 * Provides a MC register accessor for the atom interpreter (r4xx+).
817 */
818static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
819{
820
821}
822
823/**
824 * cail_reg_write - write MMIO register
825 *
826 * @info: atom card_info pointer
827 * @reg: MMIO register offset
828 * @val: value to write to the pll register
829 *
830 * Provides a MMIO register accessor for the atom interpreter (r4xx+).
831 */
832static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
833{
834 struct amdgpu_device *adev = info->dev->dev_private;
835
836 WREG32(reg, val);
837}
838
839/**
840 * cail_reg_read - read MMIO register
841 *
842 * @info: atom card_info pointer
843 * @reg: MMIO register offset
844 *
845 * Provides an MMIO register accessor for the atom interpreter (r4xx+).
846 * Returns the value of the MMIO register.
847 */
848static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
849{
850 struct amdgpu_device *adev = info->dev->dev_private;
851 uint32_t r;
852
853 r = RREG32(reg);
854 return r;
855}
856
857/**
858 * cail_ioreg_write - write IO register
859 *
860 * @info: atom card_info pointer
861 * @reg: IO register offset
862 * @val: value to write to the pll register
863 *
864 * Provides a IO register accessor for the atom interpreter (r4xx+).
865 */
866static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
867{
868 struct amdgpu_device *adev = info->dev->dev_private;
869
870 WREG32_IO(reg, val);
871}
872
873/**
874 * cail_ioreg_read - read IO register
875 *
876 * @info: atom card_info pointer
877 * @reg: IO register offset
878 *
879 * Provides an IO register accessor for the atom interpreter (r4xx+).
880 * Returns the value of the IO register.
881 */
882static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
883{
884 struct amdgpu_device *adev = info->dev->dev_private;
885 uint32_t r;
886
887 r = RREG32_IO(reg);
888 return r;
889}
890
Kent Russell5b41d942017-08-22 12:31:43 -0400891static ssize_t amdgpu_atombios_get_vbios_version(struct device *dev,
892 struct device_attribute *attr,
893 char *buf)
894{
895 struct drm_device *ddev = dev_get_drvdata(dev);
896 struct amdgpu_device *adev = ddev->dev_private;
897 struct atom_context *ctx = adev->mode_info.atom_context;
898
899 return snprintf(buf, PAGE_SIZE, "%s\n", ctx->vbios_version);
900}
901
902static DEVICE_ATTR(vbios_version, 0444, amdgpu_atombios_get_vbios_version,
903 NULL);
904
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400905/**
906 * amdgpu_atombios_fini - free the driver info and callbacks for atombios
907 *
908 * @adev: amdgpu_device pointer
909 *
910 * Frees the driver info and register access callbacks for the ATOM
911 * interpreter (r4xx+).
912 * Called at driver shutdown.
913 */
914static void amdgpu_atombios_fini(struct amdgpu_device *adev)
915{
Monk Liu89e0ec9f2016-05-27 19:34:11 +0800916 if (adev->mode_info.atom_context) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400917 kfree(adev->mode_info.atom_context->scratch);
Monk Liu89e0ec9f2016-05-27 19:34:11 +0800918 kfree(adev->mode_info.atom_context->iio);
919 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400920 kfree(adev->mode_info.atom_context);
921 adev->mode_info.atom_context = NULL;
922 kfree(adev->mode_info.atom_card_info);
923 adev->mode_info.atom_card_info = NULL;
Kent Russell5b41d942017-08-22 12:31:43 -0400924 device_remove_file(adev->dev, &dev_attr_vbios_version);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400925}
926
927/**
928 * amdgpu_atombios_init - init the driver info and callbacks for atombios
929 *
930 * @adev: amdgpu_device pointer
931 *
932 * Initializes the driver info and register access callbacks for the
933 * ATOM interpreter (r4xx+).
934 * Returns 0 on sucess, -ENOMEM on failure.
935 * Called at driver startup.
936 */
937static int amdgpu_atombios_init(struct amdgpu_device *adev)
938{
939 struct card_info *atom_card_info =
940 kzalloc(sizeof(struct card_info), GFP_KERNEL);
Kent Russell5b41d942017-08-22 12:31:43 -0400941 int ret;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400942
943 if (!atom_card_info)
944 return -ENOMEM;
945
946 adev->mode_info.atom_card_info = atom_card_info;
947 atom_card_info->dev = adev->ddev;
948 atom_card_info->reg_read = cail_reg_read;
949 atom_card_info->reg_write = cail_reg_write;
950 /* needed for iio ops */
951 if (adev->rio_mem) {
952 atom_card_info->ioreg_read = cail_ioreg_read;
953 atom_card_info->ioreg_write = cail_ioreg_write;
954 } else {
Amber Linb64a18c2017-01-04 08:06:58 -0500955 DRM_INFO("PCI I/O BAR is not found. Using MMIO to access ATOM BIOS\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400956 atom_card_info->ioreg_read = cail_reg_read;
957 atom_card_info->ioreg_write = cail_reg_write;
958 }
959 atom_card_info->mc_read = cail_mc_read;
960 atom_card_info->mc_write = cail_mc_write;
961 atom_card_info->pll_read = cail_pll_read;
962 atom_card_info->pll_write = cail_pll_write;
963
964 adev->mode_info.atom_context = amdgpu_atom_parse(atom_card_info, adev->bios);
965 if (!adev->mode_info.atom_context) {
966 amdgpu_atombios_fini(adev);
967 return -ENOMEM;
968 }
969
970 mutex_init(&adev->mode_info.atom_context->mutex);
Alex Deuchera5bde2f2016-09-23 16:23:41 -0400971 if (adev->is_atom_fw) {
972 amdgpu_atomfirmware_scratch_regs_init(adev);
973 amdgpu_atomfirmware_allocate_fb_scratch(adev);
974 } else {
975 amdgpu_atombios_scratch_regs_init(adev);
976 amdgpu_atombios_allocate_fb_scratch(adev);
977 }
Kent Russell5b41d942017-08-22 12:31:43 -0400978
979 ret = device_create_file(adev->dev, &dev_attr_vbios_version);
980 if (ret) {
981 DRM_ERROR("Failed to create device file for VBIOS version\n");
982 return ret;
983 }
984
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400985 return 0;
986}
987
988/* if we get transitioned to only one device, take VGA back */
989/**
990 * amdgpu_vga_set_decode - enable/disable vga decode
991 *
992 * @cookie: amdgpu_device pointer
993 * @state: enable/disable vga decode
994 *
995 * Enable/disable vga decode (all asics).
996 * Returns VGA resource flags.
997 */
998static unsigned int amdgpu_vga_set_decode(void *cookie, bool state)
999{
1000 struct amdgpu_device *adev = cookie;
1001 amdgpu_asic_set_vga_state(adev, state);
1002 if (state)
1003 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
1004 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1005 else
1006 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1007}
1008
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001009static void amdgpu_check_block_size(struct amdgpu_device *adev)
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001010{
1011 /* defines number of bits in page table versus page directory,
1012 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
1013 * page table and the remaining bits are in the page directory */
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001014 if (amdgpu_vm_block_size == -1)
1015 return;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001016
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001017 if (amdgpu_vm_block_size < 9) {
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001018 dev_warn(adev->dev, "VM page table size (%d) too small\n",
1019 amdgpu_vm_block_size);
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001020 goto def_value;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001021 }
1022
1023 if (amdgpu_vm_block_size > 24 ||
1024 (amdgpu_vm_size * 1024) < (1ull << amdgpu_vm_block_size)) {
1025 dev_warn(adev->dev, "VM page table size (%d) too large\n",
1026 amdgpu_vm_block_size);
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001027 goto def_value;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001028 }
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001029
1030 return;
1031
1032def_value:
1033 amdgpu_vm_block_size = -1;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001034}
1035
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001036static void amdgpu_check_vm_size(struct amdgpu_device *adev)
1037{
Alex Deucher64dab072017-06-15 18:20:09 -04001038 /* no need to check the default value */
1039 if (amdgpu_vm_size == -1)
1040 return;
1041
Alex Deucher76117502017-06-21 12:31:41 -04001042 if (!is_power_of_2(amdgpu_vm_size)) {
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001043 dev_warn(adev->dev, "VM size (%d) must be a power of 2\n",
1044 amdgpu_vm_size);
1045 goto def_value;
1046 }
1047
1048 if (amdgpu_vm_size < 1) {
1049 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
1050 amdgpu_vm_size);
1051 goto def_value;
1052 }
1053
1054 /*
1055 * Max GPUVM size for Cayman, SI, CI VI are 40 bits.
1056 */
1057 if (amdgpu_vm_size > 1024) {
1058 dev_warn(adev->dev, "VM size (%d) too large, max is 1TB\n",
1059 amdgpu_vm_size);
1060 goto def_value;
1061 }
1062
1063 return;
1064
1065def_value:
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001066 amdgpu_vm_size = -1;
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001067}
1068
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001069/**
1070 * amdgpu_check_arguments - validate module params
1071 *
1072 * @adev: amdgpu_device pointer
1073 *
1074 * Validates certain module parameters and updates
1075 * the associated values used by the driver (all asics).
1076 */
1077static void amdgpu_check_arguments(struct amdgpu_device *adev)
1078{
Chunming Zhou5b011232015-12-10 17:34:33 +08001079 if (amdgpu_sched_jobs < 4) {
1080 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
1081 amdgpu_sched_jobs);
1082 amdgpu_sched_jobs = 4;
Alex Deucher76117502017-06-21 12:31:41 -04001083 } else if (!is_power_of_2(amdgpu_sched_jobs)){
Chunming Zhou5b011232015-12-10 17:34:33 +08001084 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
1085 amdgpu_sched_jobs);
1086 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
1087 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001088
Alex Deucher83e74db2017-08-21 11:58:25 -04001089 if (amdgpu_gart_size != -1 && amdgpu_gart_size < 32) {
Christian Königf9321cc2017-07-07 13:44:05 +02001090 /* gart size must be greater or equal to 32M */
1091 dev_warn(adev->dev, "gart size (%d) too small\n",
1092 amdgpu_gart_size);
Alex Deucher83e74db2017-08-21 11:58:25 -04001093 amdgpu_gart_size = -1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001094 }
1095
Christian König36d38372017-07-07 13:17:45 +02001096 if (amdgpu_gtt_size != -1 && amdgpu_gtt_size < 32) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001097 /* gtt size must be greater or equal to 32M */
Christian König36d38372017-07-07 13:17:45 +02001098 dev_warn(adev->dev, "gtt size (%d) too small\n",
1099 amdgpu_gtt_size);
1100 amdgpu_gtt_size = -1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001101 }
1102
Roger Hed07f14b2017-08-15 16:05:59 +08001103 /* valid range is between 4 and 9 inclusive */
1104 if (amdgpu_vm_fragment_size != -1 &&
1105 (amdgpu_vm_fragment_size > 9 || amdgpu_vm_fragment_size < 4)) {
1106 dev_warn(adev->dev, "valid range is between 4 and 9\n");
1107 amdgpu_vm_fragment_size = -1;
1108 }
1109
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001110 amdgpu_check_vm_size(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001111
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001112 amdgpu_check_block_size(adev);
Christian König6a7f76e2016-08-24 15:51:49 +02001113
jimqu526bae32016-11-07 09:53:10 +08001114 if (amdgpu_vram_page_split != -1 && (amdgpu_vram_page_split < 16 ||
Alex Deucher76117502017-06-21 12:31:41 -04001115 !is_power_of_2(amdgpu_vram_page_split))) {
Christian König6a7f76e2016-08-24 15:51:49 +02001116 dev_warn(adev->dev, "invalid VRAM page split (%d)\n",
1117 amdgpu_vram_page_split);
1118 amdgpu_vram_page_split = 1024;
1119 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001120}
1121
1122/**
1123 * amdgpu_switcheroo_set_state - set switcheroo state
1124 *
1125 * @pdev: pci dev pointer
Lukas Wunner16944672015-09-05 11:17:35 +02001126 * @state: vga_switcheroo state
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001127 *
1128 * Callback for the switcheroo driver. Suspends or resumes the
1129 * the asics before or after it is powered up using ACPI methods.
1130 */
1131static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1132{
1133 struct drm_device *dev = pci_get_drvdata(pdev);
1134
1135 if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
1136 return;
1137
1138 if (state == VGA_SWITCHEROO_ON) {
Joe Perches7ca85292017-02-28 04:55:52 -08001139 pr_info("amdgpu: switched on\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001140 /* don't suspend or resume card normally */
1141 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1142
Alex Deucher810ddc32016-08-23 13:25:49 -04001143 amdgpu_device_resume(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001144
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001145 dev->switch_power_state = DRM_SWITCH_POWER_ON;
1146 drm_kms_helper_poll_enable(dev);
1147 } else {
Joe Perches7ca85292017-02-28 04:55:52 -08001148 pr_info("amdgpu: switched off\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001149 drm_kms_helper_poll_disable(dev);
1150 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Alex Deucher810ddc32016-08-23 13:25:49 -04001151 amdgpu_device_suspend(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001152 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1153 }
1154}
1155
1156/**
1157 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1158 *
1159 * @pdev: pci dev pointer
1160 *
1161 * Callback for the switcheroo driver. Check of the switcheroo
1162 * state can be changed.
1163 * Returns true if the state can be changed, false if not.
1164 */
1165static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1166{
1167 struct drm_device *dev = pci_get_drvdata(pdev);
1168
1169 /*
1170 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1171 * locking inversion with the driver load path. And the access here is
1172 * completely racy anyway. So don't bother with locking for now.
1173 */
1174 return dev->open_count == 0;
1175}
1176
1177static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1178 .set_gpu_state = amdgpu_switcheroo_set_state,
1179 .reprobe = NULL,
1180 .can_switch = amdgpu_switcheroo_can_switch,
1181};
1182
1183int amdgpu_set_clockgating_state(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001184 enum amd_ip_block_type block_type,
1185 enum amd_clockgating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001186{
1187 int i, r = 0;
1188
1189 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001190 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001191 continue;
Rex Zhuc7228652017-02-22 15:33:46 +08001192 if (adev->ip_blocks[i].version->type != block_type)
1193 continue;
1194 if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
1195 continue;
1196 r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
1197 (void *)adev, state);
1198 if (r)
1199 DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
1200 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001201 }
1202 return r;
1203}
1204
1205int amdgpu_set_powergating_state(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001206 enum amd_ip_block_type block_type,
1207 enum amd_powergating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001208{
1209 int i, r = 0;
1210
1211 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001212 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001213 continue;
Rex Zhuc7228652017-02-22 15:33:46 +08001214 if (adev->ip_blocks[i].version->type != block_type)
1215 continue;
1216 if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
1217 continue;
1218 r = adev->ip_blocks[i].version->funcs->set_powergating_state(
1219 (void *)adev, state);
1220 if (r)
1221 DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
1222 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001223 }
1224 return r;
1225}
1226
Huang Rui6cb2d4e2017-01-05 18:44:41 +08001227void amdgpu_get_clockgating_state(struct amdgpu_device *adev, u32 *flags)
1228{
1229 int i;
1230
1231 for (i = 0; i < adev->num_ip_blocks; i++) {
1232 if (!adev->ip_blocks[i].status.valid)
1233 continue;
1234 if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
1235 adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
1236 }
1237}
1238
Alex Deucher5dbbb602016-06-23 11:41:04 -04001239int amdgpu_wait_for_idle(struct amdgpu_device *adev,
1240 enum amd_ip_block_type block_type)
1241{
1242 int i, r;
1243
1244 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001245 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001246 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001247 if (adev->ip_blocks[i].version->type == block_type) {
1248 r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -04001249 if (r)
1250 return r;
1251 break;
1252 }
1253 }
1254 return 0;
1255
1256}
1257
1258bool amdgpu_is_idle(struct amdgpu_device *adev,
1259 enum amd_ip_block_type block_type)
1260{
1261 int i;
1262
1263 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001264 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001265 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001266 if (adev->ip_blocks[i].version->type == block_type)
1267 return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -04001268 }
1269 return true;
1270
1271}
1272
Alex Deuchera1255102016-10-13 17:41:13 -04001273struct amdgpu_ip_block * amdgpu_get_ip_block(struct amdgpu_device *adev,
1274 enum amd_ip_block_type type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001275{
1276 int i;
1277
1278 for (i = 0; i < adev->num_ip_blocks; i++)
Alex Deuchera1255102016-10-13 17:41:13 -04001279 if (adev->ip_blocks[i].version->type == type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001280 return &adev->ip_blocks[i];
1281
1282 return NULL;
1283}
1284
1285/**
1286 * amdgpu_ip_block_version_cmp
1287 *
1288 * @adev: amdgpu_device pointer
yanyang15fc3aee2015-05-22 14:39:35 -04001289 * @type: enum amd_ip_block_type
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001290 * @major: major version
1291 * @minor: minor version
1292 *
1293 * return 0 if equal or greater
1294 * return 1 if smaller or the ip_block doesn't exist
1295 */
1296int amdgpu_ip_block_version_cmp(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001297 enum amd_ip_block_type type,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001298 u32 major, u32 minor)
1299{
Alex Deuchera1255102016-10-13 17:41:13 -04001300 struct amdgpu_ip_block *ip_block = amdgpu_get_ip_block(adev, type);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001301
Alex Deuchera1255102016-10-13 17:41:13 -04001302 if (ip_block && ((ip_block->version->major > major) ||
1303 ((ip_block->version->major == major) &&
1304 (ip_block->version->minor >= minor))))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001305 return 0;
1306
1307 return 1;
1308}
1309
Alex Deuchera1255102016-10-13 17:41:13 -04001310/**
1311 * amdgpu_ip_block_add
1312 *
1313 * @adev: amdgpu_device pointer
1314 * @ip_block_version: pointer to the IP to add
1315 *
1316 * Adds the IP block driver information to the collection of IPs
1317 * on the asic.
1318 */
1319int amdgpu_ip_block_add(struct amdgpu_device *adev,
1320 const struct amdgpu_ip_block_version *ip_block_version)
1321{
1322 if (!ip_block_version)
1323 return -EINVAL;
1324
Huang Ruia0bae352017-05-03 09:52:06 +08001325 DRM_DEBUG("add ip block number %d <%s>\n", adev->num_ip_blocks,
1326 ip_block_version->funcs->name);
1327
Alex Deuchera1255102016-10-13 17:41:13 -04001328 adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
1329
1330 return 0;
1331}
1332
Alex Deucher483ef982016-09-30 12:43:04 -04001333static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
Emily Deng9accf2f2016-08-10 16:01:25 +08001334{
1335 adev->enable_virtual_display = false;
1336
1337 if (amdgpu_virtual_display) {
1338 struct drm_device *ddev = adev->ddev;
1339 const char *pci_address_name = pci_name(ddev->pdev);
Emily Deng0f663562016-09-30 13:02:18 -04001340 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
Emily Deng9accf2f2016-08-10 16:01:25 +08001341
1342 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1343 pciaddstr_tmp = pciaddstr;
Emily Deng0f663562016-09-30 13:02:18 -04001344 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1345 pciaddname = strsep(&pciaddname_tmp, ",");
Yintian Tao967de2a2017-01-22 15:16:51 +08001346 if (!strcmp("all", pciaddname)
1347 || !strcmp(pci_address_name, pciaddname)) {
Emily Deng0f663562016-09-30 13:02:18 -04001348 long num_crtc;
1349 int res = -1;
1350
Emily Deng9accf2f2016-08-10 16:01:25 +08001351 adev->enable_virtual_display = true;
Emily Deng0f663562016-09-30 13:02:18 -04001352
1353 if (pciaddname_tmp)
1354 res = kstrtol(pciaddname_tmp, 10,
1355 &num_crtc);
1356
1357 if (!res) {
1358 if (num_crtc < 1)
1359 num_crtc = 1;
1360 if (num_crtc > 6)
1361 num_crtc = 6;
1362 adev->mode_info.num_crtc = num_crtc;
1363 } else {
1364 adev->mode_info.num_crtc = 1;
1365 }
Emily Deng9accf2f2016-08-10 16:01:25 +08001366 break;
1367 }
1368 }
1369
Emily Deng0f663562016-09-30 13:02:18 -04001370 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
1371 amdgpu_virtual_display, pci_address_name,
1372 adev->enable_virtual_display, adev->mode_info.num_crtc);
Emily Deng9accf2f2016-08-10 16:01:25 +08001373
1374 kfree(pciaddstr);
1375 }
1376}
1377
Alex Deuchere2a75f82017-04-27 16:58:01 -04001378static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev)
1379{
Alex Deuchere2a75f82017-04-27 16:58:01 -04001380 const char *chip_name;
1381 char fw_name[30];
1382 int err;
1383 const struct gpu_info_firmware_header_v1_0 *hdr;
1384
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001385 adev->firmware.gpu_info_fw = NULL;
1386
Alex Deuchere2a75f82017-04-27 16:58:01 -04001387 switch (adev->asic_type) {
1388 case CHIP_TOPAZ:
1389 case CHIP_TONGA:
1390 case CHIP_FIJI:
1391 case CHIP_POLARIS11:
1392 case CHIP_POLARIS10:
1393 case CHIP_POLARIS12:
1394 case CHIP_CARRIZO:
1395 case CHIP_STONEY:
1396#ifdef CONFIG_DRM_AMDGPU_SI
1397 case CHIP_VERDE:
1398 case CHIP_TAHITI:
1399 case CHIP_PITCAIRN:
1400 case CHIP_OLAND:
1401 case CHIP_HAINAN:
1402#endif
1403#ifdef CONFIG_DRM_AMDGPU_CIK
1404 case CHIP_BONAIRE:
1405 case CHIP_HAWAII:
1406 case CHIP_KAVERI:
1407 case CHIP_KABINI:
1408 case CHIP_MULLINS:
1409#endif
1410 default:
1411 return 0;
1412 case CHIP_VEGA10:
1413 chip_name = "vega10";
1414 break;
Alex Deucher2d2e5e72017-05-09 12:27:35 -04001415 case CHIP_RAVEN:
1416 chip_name = "raven";
1417 break;
Alex Deuchere2a75f82017-04-27 16:58:01 -04001418 }
1419
1420 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name);
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001421 err = request_firmware(&adev->firmware.gpu_info_fw, fw_name, adev->dev);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001422 if (err) {
1423 dev_err(adev->dev,
1424 "Failed to load gpu_info firmware \"%s\"\n",
1425 fw_name);
1426 goto out;
1427 }
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001428 err = amdgpu_ucode_validate(adev->firmware.gpu_info_fw);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001429 if (err) {
1430 dev_err(adev->dev,
1431 "Failed to validate gpu_info firmware \"%s\"\n",
1432 fw_name);
1433 goto out;
1434 }
1435
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001436 hdr = (const struct gpu_info_firmware_header_v1_0 *)adev->firmware.gpu_info_fw->data;
Alex Deuchere2a75f82017-04-27 16:58:01 -04001437 amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
1438
1439 switch (hdr->version_major) {
1440 case 1:
1441 {
1442 const struct gpu_info_firmware_v1_0 *gpu_info_fw =
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001443 (const struct gpu_info_firmware_v1_0 *)(adev->firmware.gpu_info_fw->data +
Alex Deuchere2a75f82017-04-27 16:58:01 -04001444 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1445
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001446 adev->gfx.config.max_shader_engines = le32_to_cpu(gpu_info_fw->gc_num_se);
1447 adev->gfx.config.max_cu_per_sh = le32_to_cpu(gpu_info_fw->gc_num_cu_per_sh);
1448 adev->gfx.config.max_sh_per_se = le32_to_cpu(gpu_info_fw->gc_num_sh_per_se);
1449 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 -04001450 adev->gfx.config.max_texture_channel_caches =
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001451 le32_to_cpu(gpu_info_fw->gc_num_tccs);
1452 adev->gfx.config.max_gprs = le32_to_cpu(gpu_info_fw->gc_num_gprs);
1453 adev->gfx.config.max_gs_threads = le32_to_cpu(gpu_info_fw->gc_num_max_gs_thds);
1454 adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gpu_info_fw->gc_gs_table_depth);
1455 adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gpu_info_fw->gc_gsprim_buff_depth);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001456 adev->gfx.config.double_offchip_lds_buf =
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001457 le32_to_cpu(gpu_info_fw->gc_double_offchip_lds_buffer);
1458 adev->gfx.cu_info.wave_front_size = le32_to_cpu(gpu_info_fw->gc_wave_size);
Hawking Zhang51fd0372017-06-09 22:30:52 +08001459 adev->gfx.cu_info.max_waves_per_simd =
1460 le32_to_cpu(gpu_info_fw->gc_max_waves_per_simd);
1461 adev->gfx.cu_info.max_scratch_slots_per_cu =
1462 le32_to_cpu(gpu_info_fw->gc_max_scratch_slots_per_cu);
1463 adev->gfx.cu_info.lds_size = le32_to_cpu(gpu_info_fw->gc_lds_size);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001464 break;
1465 }
1466 default:
1467 dev_err(adev->dev,
1468 "Unsupported gpu_info table %d\n", hdr->header.ucode_version);
1469 err = -EINVAL;
1470 goto out;
1471 }
1472out:
Alex Deuchere2a75f82017-04-27 16:58:01 -04001473 return err;
1474}
1475
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001476static int amdgpu_early_init(struct amdgpu_device *adev)
1477{
Alex Deucheraaa36a92015-04-20 17:31:14 -04001478 int i, r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001479
Alex Deucher483ef982016-09-30 12:43:04 -04001480 amdgpu_device_enable_virtual_display(adev);
Emily Denga6be7572016-08-08 11:37:50 +08001481
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001482 switch (adev->asic_type) {
Alex Deucheraaa36a92015-04-20 17:31:14 -04001483 case CHIP_TOPAZ:
1484 case CHIP_TONGA:
David Zhang48299f92015-07-08 01:05:16 +08001485 case CHIP_FIJI:
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001486 case CHIP_POLARIS11:
1487 case CHIP_POLARIS10:
Junwei Zhangc4642a42016-12-14 15:32:28 -05001488 case CHIP_POLARIS12:
Alex Deucheraaa36a92015-04-20 17:31:14 -04001489 case CHIP_CARRIZO:
Samuel Li39bb0c92015-10-08 16:31:43 -04001490 case CHIP_STONEY:
1491 if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
Alex Deucheraaa36a92015-04-20 17:31:14 -04001492 adev->family = AMDGPU_FAMILY_CZ;
1493 else
1494 adev->family = AMDGPU_FAMILY_VI;
1495
1496 r = vi_set_ip_blocks(adev);
1497 if (r)
1498 return r;
1499 break;
Ken Wang33f34802016-01-21 17:29:41 +08001500#ifdef CONFIG_DRM_AMDGPU_SI
1501 case CHIP_VERDE:
1502 case CHIP_TAHITI:
1503 case CHIP_PITCAIRN:
1504 case CHIP_OLAND:
1505 case CHIP_HAINAN:
Ken Wang295d0da2016-05-24 21:02:53 +08001506 adev->family = AMDGPU_FAMILY_SI;
Ken Wang33f34802016-01-21 17:29:41 +08001507 r = si_set_ip_blocks(adev);
1508 if (r)
1509 return r;
1510 break;
1511#endif
Alex Deuchera2e73f52015-04-20 17:09:27 -04001512#ifdef CONFIG_DRM_AMDGPU_CIK
1513 case CHIP_BONAIRE:
1514 case CHIP_HAWAII:
1515 case CHIP_KAVERI:
1516 case CHIP_KABINI:
1517 case CHIP_MULLINS:
1518 if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
1519 adev->family = AMDGPU_FAMILY_CI;
1520 else
1521 adev->family = AMDGPU_FAMILY_KV;
1522
1523 r = cik_set_ip_blocks(adev);
1524 if (r)
1525 return r;
1526 break;
1527#endif
Chunming Zhou2ca8a5d2016-12-07 17:31:19 +08001528 case CHIP_VEGA10:
1529 case CHIP_RAVEN:
1530 if (adev->asic_type == CHIP_RAVEN)
1531 adev->family = AMDGPU_FAMILY_RV;
1532 else
1533 adev->family = AMDGPU_FAMILY_AI;
Ken Wang460826e2017-03-06 14:53:16 -05001534
1535 r = soc15_set_ip_blocks(adev);
1536 if (r)
1537 return r;
1538 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001539 default:
1540 /* FIXME: not supported yet */
1541 return -EINVAL;
1542 }
1543
Alex Deuchere2a75f82017-04-27 16:58:01 -04001544 r = amdgpu_device_parse_gpu_info_fw(adev);
1545 if (r)
1546 return r;
1547
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001548 if (amdgpu_sriov_vf(adev)) {
1549 r = amdgpu_virt_request_full_gpu(adev, true);
1550 if (r)
1551 return r;
1552 }
1553
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001554 for (i = 0; i < adev->num_ip_blocks; i++) {
1555 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
Huang Ruied8cf002017-05-03 09:40:17 +08001556 DRM_ERROR("disabled ip block: %d <%s>\n",
1557 i, adev->ip_blocks[i].version->funcs->name);
Alex Deuchera1255102016-10-13 17:41:13 -04001558 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001559 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001560 if (adev->ip_blocks[i].version->funcs->early_init) {
1561 r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001562 if (r == -ENOENT) {
Alex Deuchera1255102016-10-13 17:41:13 -04001563 adev->ip_blocks[i].status.valid = false;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001564 } else if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001565 DRM_ERROR("early_init of IP block <%s> failed %d\n",
1566 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001567 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001568 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001569 adev->ip_blocks[i].status.valid = true;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001570 }
Alex Deucher974e6b62015-07-10 13:59:44 -04001571 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001572 adev->ip_blocks[i].status.valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001573 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001574 }
1575 }
1576
Nicolai Hähnle395d1fb2016-06-02 12:32:07 +02001577 adev->cg_flags &= amdgpu_cg_mask;
1578 adev->pg_flags &= amdgpu_pg_mask;
1579
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001580 return 0;
1581}
1582
1583static int amdgpu_init(struct amdgpu_device *adev)
1584{
1585 int i, r;
1586
1587 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001588 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001589 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001590 r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001591 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001592 DRM_ERROR("sw_init of IP block <%s> failed %d\n",
1593 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001594 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001595 }
Alex Deuchera1255102016-10-13 17:41:13 -04001596 adev->ip_blocks[i].status.sw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001597 /* need to do gmc hw init early so we can allocate gpu mem */
Alex Deuchera1255102016-10-13 17:41:13 -04001598 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001599 r = amdgpu_vram_scratch_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001600 if (r) {
1601 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001602 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001603 }
Alex Deuchera1255102016-10-13 17:41:13 -04001604 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001605 if (r) {
1606 DRM_ERROR("hw_init %d failed %d\n", i, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001607 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001608 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001609 r = amdgpu_wb_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001610 if (r) {
1611 DRM_ERROR("amdgpu_wb_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 adev->ip_blocks[i].status.hw = true;
Monk Liu24936642017-01-09 15:54:32 +08001615
1616 /* right after GMC hw init, we create CSA */
1617 if (amdgpu_sriov_vf(adev)) {
1618 r = amdgpu_allocate_static_csa(adev);
1619 if (r) {
1620 DRM_ERROR("allocate CSA failed %d\n", r);
1621 return r;
1622 }
1623 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001624 }
1625 }
1626
1627 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001628 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001629 continue;
1630 /* gmc hw init is done early */
Alex Deuchera1255102016-10-13 17:41:13 -04001631 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001632 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001633 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001634 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001635 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
1636 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001637 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001638 }
Alex Deuchera1255102016-10-13 17:41:13 -04001639 adev->ip_blocks[i].status.hw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001640 }
1641
1642 return 0;
1643}
1644
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08001645static void amdgpu_fill_reset_magic(struct amdgpu_device *adev)
1646{
1647 memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM);
1648}
1649
1650static bool amdgpu_check_vram_lost(struct amdgpu_device *adev)
1651{
1652 return !!memcmp(adev->gart.ptr, adev->reset_magic,
1653 AMDGPU_RESET_MAGIC_NUM);
1654}
1655
Shirish S2dc80b02017-05-25 10:05:25 +05301656static int amdgpu_late_set_cg_state(struct amdgpu_device *adev)
1657{
1658 int i = 0, r;
1659
1660 for (i = 0; i < adev->num_ip_blocks; i++) {
1661 if (!adev->ip_blocks[i].status.valid)
1662 continue;
1663 /* skip CG for VCE/UVD, it's handled specially */
1664 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1665 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1666 /* enable clockgating to save power */
1667 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1668 AMD_CG_STATE_GATE);
1669 if (r) {
1670 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
1671 adev->ip_blocks[i].version->funcs->name, r);
1672 return r;
1673 }
1674 }
1675 }
1676 return 0;
1677}
1678
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001679static int amdgpu_late_init(struct amdgpu_device *adev)
1680{
1681 int i = 0, r;
1682
1683 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001684 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001685 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001686 if (adev->ip_blocks[i].version->funcs->late_init) {
1687 r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001688 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001689 DRM_ERROR("late_init of IP block <%s> failed %d\n",
1690 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001691 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001692 }
Alex Deuchera1255102016-10-13 17:41:13 -04001693 adev->ip_blocks[i].status.late_initialized = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001694 }
1695 }
1696
Shirish S2dc80b02017-05-25 10:05:25 +05301697 mod_delayed_work(system_wq, &adev->late_init_work,
1698 msecs_to_jiffies(AMDGPU_RESUME_MS));
1699
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08001700 amdgpu_fill_reset_magic(adev);
1701
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001702 return 0;
1703}
1704
1705static int amdgpu_fini(struct amdgpu_device *adev)
1706{
1707 int i, r;
1708
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001709 /* need to disable SMC first */
1710 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001711 if (!adev->ip_blocks[i].status.hw)
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001712 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001713 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001714 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
Alex Deuchera1255102016-10-13 17:41:13 -04001715 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1716 AMD_CG_STATE_UNGATE);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001717 if (r) {
1718 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001719 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001720 return r;
1721 }
Alex Deuchera1255102016-10-13 17:41:13 -04001722 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001723 /* XXX handle errors */
1724 if (r) {
1725 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001726 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001727 }
Alex Deuchera1255102016-10-13 17:41:13 -04001728 adev->ip_blocks[i].status.hw = false;
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001729 break;
1730 }
1731 }
1732
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001733 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001734 if (!adev->ip_blocks[i].status.hw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001735 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001736 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001737 amdgpu_wb_fini(adev);
1738 amdgpu_vram_scratch_fini(adev);
1739 }
Rex Zhu8201a672016-11-24 21:44:44 +08001740
1741 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1742 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1743 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
1744 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1745 AMD_CG_STATE_UNGATE);
1746 if (r) {
1747 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1748 adev->ip_blocks[i].version->funcs->name, r);
1749 return r;
1750 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001751 }
Rex Zhu8201a672016-11-24 21:44:44 +08001752
Alex Deuchera1255102016-10-13 17:41:13 -04001753 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001754 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001755 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001756 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
1757 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001758 }
Rex Zhu8201a672016-11-24 21:44:44 +08001759
Alex Deuchera1255102016-10-13 17:41:13 -04001760 adev->ip_blocks[i].status.hw = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001761 }
1762
1763 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001764 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001765 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001766 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001767 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001768 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001769 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
1770 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001771 }
Alex Deuchera1255102016-10-13 17:41:13 -04001772 adev->ip_blocks[i].status.sw = false;
1773 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001774 }
1775
Monk Liua6dcfd92016-05-19 14:36:34 +08001776 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001777 if (!adev->ip_blocks[i].status.late_initialized)
Grazvydas Ignotas8a2eef12016-10-03 00:06:44 +03001778 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001779 if (adev->ip_blocks[i].version->funcs->late_fini)
1780 adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
1781 adev->ip_blocks[i].status.late_initialized = false;
Monk Liua6dcfd92016-05-19 14:36:34 +08001782 }
1783
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001784 if (amdgpu_sriov_vf(adev)) {
Monk Liu24936642017-01-09 15:54:32 +08001785 amdgpu_bo_free_kernel(&adev->virt.csa_obj, &adev->virt.csa_vmid0_addr, NULL);
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001786 amdgpu_virt_release_full_gpu(adev, false);
1787 }
Monk Liu24936642017-01-09 15:54:32 +08001788
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001789 return 0;
1790}
1791
Shirish S2dc80b02017-05-25 10:05:25 +05301792static void amdgpu_late_init_func_handler(struct work_struct *work)
1793{
1794 struct amdgpu_device *adev =
1795 container_of(work, struct amdgpu_device, late_init_work.work);
1796 amdgpu_late_set_cg_state(adev);
1797}
1798
Alex Deucherfaefba92016-12-06 10:38:29 -05001799int amdgpu_suspend(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001800{
1801 int i, r;
1802
Xiangliang Yue941ea92017-01-18 12:47:55 +08001803 if (amdgpu_sriov_vf(adev))
1804 amdgpu_virt_request_full_gpu(adev, false);
1805
Flora Cuic5a93a22016-02-26 10:45:25 +08001806 /* ungate SMC block first */
1807 r = amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC,
1808 AMD_CG_STATE_UNGATE);
1809 if (r) {
1810 DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n",r);
1811 }
1812
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001813 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001814 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001815 continue;
1816 /* ungate blocks so that suspend can properly shut them down */
Flora Cuic5a93a22016-02-26 10:45:25 +08001817 if (i != AMD_IP_BLOCK_TYPE_SMC) {
Alex Deuchera1255102016-10-13 17:41:13 -04001818 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1819 AMD_CG_STATE_UNGATE);
Flora Cuic5a93a22016-02-26 10:45:25 +08001820 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001821 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1822 adev->ip_blocks[i].version->funcs->name, r);
Flora Cuic5a93a22016-02-26 10:45:25 +08001823 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001824 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001825 /* XXX handle errors */
Alex Deuchera1255102016-10-13 17:41:13 -04001826 r = adev->ip_blocks[i].version->funcs->suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001827 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001828 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001829 DRM_ERROR("suspend of IP block <%s> failed %d\n",
1830 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001831 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001832 }
1833
Xiangliang Yue941ea92017-01-18 12:47:55 +08001834 if (amdgpu_sriov_vf(adev))
1835 amdgpu_virt_release_full_gpu(adev, false);
1836
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001837 return 0;
1838}
1839
Monk Liue4f0fdc2017-02-09 11:55:49 +08001840static int amdgpu_sriov_reinit_early(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08001841{
1842 int i, r;
1843
Monk Liu2cb681b2017-04-26 12:00:49 +08001844 static enum amd_ip_block_type ip_order[] = {
1845 AMD_IP_BLOCK_TYPE_GMC,
1846 AMD_IP_BLOCK_TYPE_COMMON,
Monk Liu2cb681b2017-04-26 12:00:49 +08001847 AMD_IP_BLOCK_TYPE_IH,
1848 };
Monk Liua90ad3c2017-01-23 14:22:08 +08001849
Monk Liu2cb681b2017-04-26 12:00:49 +08001850 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1851 int j;
1852 struct amdgpu_ip_block *block;
Monk Liua90ad3c2017-01-23 14:22:08 +08001853
Monk Liu2cb681b2017-04-26 12:00:49 +08001854 for (j = 0; j < adev->num_ip_blocks; j++) {
1855 block = &adev->ip_blocks[j];
1856
1857 if (block->version->type != ip_order[i] ||
1858 !block->status.valid)
1859 continue;
1860
1861 r = block->version->funcs->hw_init(adev);
1862 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
Monk Liua90ad3c2017-01-23 14:22:08 +08001863 }
1864 }
1865
1866 return 0;
1867}
1868
Monk Liue4f0fdc2017-02-09 11:55:49 +08001869static int amdgpu_sriov_reinit_late(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08001870{
1871 int i, r;
1872
Monk Liu2cb681b2017-04-26 12:00:49 +08001873 static enum amd_ip_block_type ip_order[] = {
1874 AMD_IP_BLOCK_TYPE_SMC,
1875 AMD_IP_BLOCK_TYPE_DCE,
1876 AMD_IP_BLOCK_TYPE_GFX,
1877 AMD_IP_BLOCK_TYPE_SDMA,
Frank Min257deb82017-06-15 20:07:36 +08001878 AMD_IP_BLOCK_TYPE_UVD,
1879 AMD_IP_BLOCK_TYPE_VCE
Monk Liu2cb681b2017-04-26 12:00:49 +08001880 };
Monk Liua90ad3c2017-01-23 14:22:08 +08001881
Monk Liu2cb681b2017-04-26 12:00:49 +08001882 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1883 int j;
1884 struct amdgpu_ip_block *block;
Monk Liua90ad3c2017-01-23 14:22:08 +08001885
Monk Liu2cb681b2017-04-26 12:00:49 +08001886 for (j = 0; j < adev->num_ip_blocks; j++) {
1887 block = &adev->ip_blocks[j];
1888
1889 if (block->version->type != ip_order[i] ||
1890 !block->status.valid)
1891 continue;
1892
1893 r = block->version->funcs->hw_init(adev);
1894 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
Monk Liua90ad3c2017-01-23 14:22:08 +08001895 }
1896 }
1897
1898 return 0;
1899}
1900
Chunming Zhoufcf06492017-05-05 10:33:33 +08001901static int amdgpu_resume_phase1(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001902{
1903 int i, r;
1904
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001905 for (i = 0; i < adev->num_ip_blocks; i++) {
1906 if (!adev->ip_blocks[i].status.valid)
1907 continue;
Chunming Zhoufcf06492017-05-05 10:33:33 +08001908 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
1909 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
1910 adev->ip_blocks[i].version->type ==
1911 AMD_IP_BLOCK_TYPE_IH) {
1912 r = adev->ip_blocks[i].version->funcs->resume(adev);
1913 if (r) {
1914 DRM_ERROR("resume of IP block <%s> failed %d\n",
1915 adev->ip_blocks[i].version->funcs->name, r);
1916 return r;
1917 }
1918 }
1919 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001920
Chunming Zhoufcf06492017-05-05 10:33:33 +08001921 return 0;
1922}
1923
1924static int amdgpu_resume_phase2(struct amdgpu_device *adev)
1925{
1926 int i, r;
1927
1928 for (i = 0; i < adev->num_ip_blocks; i++) {
1929 if (!adev->ip_blocks[i].status.valid)
1930 continue;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001931 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
1932 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
1933 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH )
1934 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001935 r = adev->ip_blocks[i].version->funcs->resume(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001936 if (r) {
1937 DRM_ERROR("resume of IP block <%s> failed %d\n",
1938 adev->ip_blocks[i].version->funcs->name, r);
1939 return r;
1940 }
1941 }
1942
1943 return 0;
1944}
1945
1946static int amdgpu_resume(struct amdgpu_device *adev)
1947{
Chunming Zhoufcf06492017-05-05 10:33:33 +08001948 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001949
Chunming Zhoufcf06492017-05-05 10:33:33 +08001950 r = amdgpu_resume_phase1(adev);
1951 if (r)
1952 return r;
1953 r = amdgpu_resume_phase2(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001954
Chunming Zhoufcf06492017-05-05 10:33:33 +08001955 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001956}
1957
Monk Liu4e99a442016-03-31 13:26:59 +08001958static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
Andres Rodriguez048765a2016-06-11 02:51:32 -04001959{
Alex Deuchera5bde2f2016-09-23 16:23:41 -04001960 if (adev->is_atom_fw) {
1961 if (amdgpu_atomfirmware_gpu_supports_virtualization(adev))
1962 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
1963 } else {
1964 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
1965 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
1966 }
Andres Rodriguez048765a2016-06-11 02:51:32 -04001967}
1968
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001969/**
1970 * amdgpu_device_init - initialize the driver
1971 *
1972 * @adev: amdgpu_device pointer
1973 * @pdev: drm dev pointer
1974 * @pdev: pci dev pointer
1975 * @flags: driver flags
1976 *
1977 * Initializes the driver info and hw (all asics).
1978 * Returns 0 for success or an error on failure.
1979 * Called at driver startup.
1980 */
1981int amdgpu_device_init(struct amdgpu_device *adev,
1982 struct drm_device *ddev,
1983 struct pci_dev *pdev,
1984 uint32_t flags)
1985{
1986 int r, i;
1987 bool runtime = false;
Marek Olšák95844d22016-08-17 23:49:27 +02001988 u32 max_MBps;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001989
1990 adev->shutdown = false;
1991 adev->dev = &pdev->dev;
1992 adev->ddev = ddev;
1993 adev->pdev = pdev;
1994 adev->flags = flags;
Jammy Zhou2f7d10b2015-07-22 11:29:01 +08001995 adev->asic_type = flags & AMD_ASIC_MASK;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001996 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
Christian König6f02a692017-07-07 11:56:59 +02001997 adev->mc.gart_size = 512 * 1024 * 1024;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001998 adev->accel_working = false;
1999 adev->num_rings = 0;
2000 adev->mman.buffer_funcs = NULL;
2001 adev->mman.buffer_funcs_ring = NULL;
2002 adev->vm_manager.vm_pte_funcs = NULL;
Christian König2d55e452016-02-08 17:37:38 +01002003 adev->vm_manager.vm_pte_num_rings = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002004 adev->gart.gart_funcs = NULL;
Chris Wilsonf54d1862016-10-25 13:00:45 +01002005 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002006
2007 adev->smc_rreg = &amdgpu_invalid_rreg;
2008 adev->smc_wreg = &amdgpu_invalid_wreg;
2009 adev->pcie_rreg = &amdgpu_invalid_rreg;
2010 adev->pcie_wreg = &amdgpu_invalid_wreg;
Huang Rui36b9a952016-08-31 13:23:25 +08002011 adev->pciep_rreg = &amdgpu_invalid_rreg;
2012 adev->pciep_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002013 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
2014 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
2015 adev->didt_rreg = &amdgpu_invalid_rreg;
2016 adev->didt_wreg = &amdgpu_invalid_wreg;
Rex Zhuccdbb202016-06-08 12:47:41 +08002017 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
2018 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002019 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
2020 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
2021
Rex Zhuccdbb202016-06-08 12:47:41 +08002022
Alex Deucher3e39ab92015-06-05 15:04:33 -04002023 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
2024 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
2025 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002026
2027 /* mutex initialization are all done here so we
2028 * can recall function without having locking issues */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002029 atomic_set(&adev->irq.ih.lock, 0);
Huang Rui0e5ca0d2017-03-03 18:37:23 -05002030 mutex_init(&adev->firmware.mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002031 mutex_init(&adev->pm.mutex);
2032 mutex_init(&adev->gfx.gpu_clock_mutex);
2033 mutex_init(&adev->srbm_mutex);
2034 mutex_init(&adev->grbm_idx_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002035 mutex_init(&adev->mn_lock);
2036 hash_init(adev->mn_hash);
2037
2038 amdgpu_check_arguments(adev);
2039
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002040 spin_lock_init(&adev->mmio_idx_lock);
2041 spin_lock_init(&adev->smc_idx_lock);
2042 spin_lock_init(&adev->pcie_idx_lock);
2043 spin_lock_init(&adev->uvd_ctx_idx_lock);
2044 spin_lock_init(&adev->didt_idx_lock);
Rex Zhuccdbb202016-06-08 12:47:41 +08002045 spin_lock_init(&adev->gc_cac_idx_lock);
Evan Quan16abb5d2017-07-04 09:21:50 +08002046 spin_lock_init(&adev->se_cac_idx_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002047 spin_lock_init(&adev->audio_endpt_idx_lock);
Marek Olšák95844d22016-08-17 23:49:27 +02002048 spin_lock_init(&adev->mm_stats.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002049
Chunming Zhou0c4e7fa2016-08-17 11:41:30 +08002050 INIT_LIST_HEAD(&adev->shadow_list);
2051 mutex_init(&adev->shadow_list_lock);
2052
Chunming Zhou5c1354b2016-08-30 16:13:10 +08002053 INIT_LIST_HEAD(&adev->gtt_list);
2054 spin_lock_init(&adev->gtt_list_lock);
2055
Andres Rodriguez795f2812017-03-06 16:27:55 -05002056 INIT_LIST_HEAD(&adev->ring_lru_list);
2057 spin_lock_init(&adev->ring_lru_list_lock);
2058
Shirish S2dc80b02017-05-25 10:05:25 +05302059 INIT_DELAYED_WORK(&adev->late_init_work, amdgpu_late_init_func_handler);
2060
Alex Xie0fa49552017-06-08 14:58:05 -04002061 /* Registers mapping */
2062 /* TODO: block userspace mapping of io register */
Ken Wangda69c1612016-01-21 19:08:55 +08002063 if (adev->asic_type >= CHIP_BONAIRE) {
2064 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
2065 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
2066 } else {
2067 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
2068 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
2069 }
Chunming Zhou5c1354b2016-08-30 16:13:10 +08002070
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002071 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
2072 if (adev->rmmio == NULL) {
2073 return -ENOMEM;
2074 }
2075 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
2076 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
2077
Ken Wangda69c1612016-01-21 19:08:55 +08002078 if (adev->asic_type >= CHIP_BONAIRE)
2079 /* doorbell bar mapping */
2080 amdgpu_doorbell_init(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002081
2082 /* io port mapping */
2083 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
2084 if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
2085 adev->rio_mem_size = pci_resource_len(adev->pdev, i);
2086 adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
2087 break;
2088 }
2089 }
2090 if (adev->rio_mem == NULL)
Amber Linb64a18c2017-01-04 08:06:58 -05002091 DRM_INFO("PCI I/O BAR is not found.\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002092
2093 /* early init functions */
2094 r = amdgpu_early_init(adev);
2095 if (r)
2096 return r;
2097
2098 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
2099 /* this will fail for cards that aren't VGA class devices, just
2100 * ignore it */
2101 vga_client_register(adev->pdev, adev, NULL, amdgpu_vga_set_decode);
2102
2103 if (amdgpu_runtime_pm == 1)
2104 runtime = true;
Alex Deuchere9bef452016-04-25 13:12:18 -04002105 if (amdgpu_device_is_px(ddev))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002106 runtime = true;
Lukas Wunner84c8b222017-03-10 21:23:45 +01002107 if (!pci_is_thunderbolt_attached(adev->pdev))
2108 vga_switcheroo_register_client(adev->pdev,
2109 &amdgpu_switcheroo_ops, runtime);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002110 if (runtime)
2111 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
2112
2113 /* Read BIOS */
Alex Deucher83ba1262016-06-03 18:21:41 -04002114 if (!amdgpu_get_bios(adev)) {
2115 r = -EINVAL;
2116 goto failed;
2117 }
Nils Wallméniusf7e9e9f2016-12-14 21:52:45 +01002118
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002119 r = amdgpu_atombios_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002120 if (r) {
2121 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
Gavin Wan89041942017-06-23 13:55:15 -04002122 amdgpu_vf_error_put(AMDGIM_ERROR_VF_ATOMBIOS_INIT_FAIL, 0, 0);
Alex Deucher83ba1262016-06-03 18:21:41 -04002123 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002124 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002125
Monk Liu4e99a442016-03-31 13:26:59 +08002126 /* detect if we are with an SRIOV vbios */
2127 amdgpu_device_detect_sriov_bios(adev);
Andres Rodriguez048765a2016-06-11 02:51:32 -04002128
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002129 /* Post card if necessary */
Monk Liubec86372016-09-14 19:38:08 +08002130 if (amdgpu_vpost_needed(adev)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002131 if (!adev->bios) {
Monk Liubec86372016-09-14 19:38:08 +08002132 dev_err(adev->dev, "no vBIOS found\n");
Gavin Wan89041942017-06-23 13:55:15 -04002133 amdgpu_vf_error_put(AMDGIM_ERROR_VF_NO_VBIOS, 0, 0);
Alex Deucher83ba1262016-06-03 18:21:41 -04002134 r = -EINVAL;
2135 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002136 }
Monk Liubec86372016-09-14 19:38:08 +08002137 DRM_INFO("GPU posting now...\n");
Monk Liu4e99a442016-03-31 13:26:59 +08002138 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2139 if (r) {
2140 dev_err(adev->dev, "gpu post error!\n");
Gavin Wan89041942017-06-23 13:55:15 -04002141 amdgpu_vf_error_put(AMDGIM_ERROR_VF_GPU_POST_ERROR, 0, 0);
Monk Liu4e99a442016-03-31 13:26:59 +08002142 goto failed;
2143 }
2144 } else {
2145 DRM_INFO("GPU post is not needed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002146 }
2147
Alex Deucher88b64e92017-07-10 10:43:10 -04002148 if (adev->is_atom_fw) {
2149 /* Initialize clocks */
2150 r = amdgpu_atomfirmware_get_clock_info(adev);
2151 if (r) {
2152 dev_err(adev->dev, "amdgpu_atomfirmware_get_clock_info failed\n");
2153 amdgpu_vf_error_put(AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
2154 goto failed;
2155 }
2156 } else {
Alex Deuchera5bde2f2016-09-23 16:23:41 -04002157 /* Initialize clocks */
2158 r = amdgpu_atombios_get_clock_info(adev);
2159 if (r) {
2160 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
Gavin Wan89041942017-06-23 13:55:15 -04002161 amdgpu_vf_error_put(AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
2162 goto failed;
Alex Deuchera5bde2f2016-09-23 16:23:41 -04002163 }
2164 /* init i2c buses */
2165 amdgpu_atombios_i2c_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002166 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002167
2168 /* Fence driver */
2169 r = amdgpu_fence_driver_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002170 if (r) {
2171 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
Gavin Wan89041942017-06-23 13:55:15 -04002172 amdgpu_vf_error_put(AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0);
Alex Deucher83ba1262016-06-03 18:21:41 -04002173 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002174 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002175
2176 /* init the mode config */
2177 drm_mode_config_init(adev->ddev);
2178
2179 r = amdgpu_init(adev);
2180 if (r) {
Alex Deucher2c1a2782015-12-07 17:02:53 -05002181 dev_err(adev->dev, "amdgpu_init failed\n");
Gavin Wan89041942017-06-23 13:55:15 -04002182 amdgpu_vf_error_put(AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002183 amdgpu_fini(adev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002184 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002185 }
2186
2187 adev->accel_working = true;
2188
Alex Xiee59c0202017-06-01 09:42:59 -04002189 amdgpu_vm_check_compute_bug(adev);
2190
Marek Olšák95844d22016-08-17 23:49:27 +02002191 /* Initialize the buffer migration limit. */
2192 if (amdgpu_moverate >= 0)
2193 max_MBps = amdgpu_moverate;
2194 else
2195 max_MBps = 8; /* Allow 8 MB/s. */
2196 /* Get a log2 for easy divisions. */
2197 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
2198
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002199 r = amdgpu_ib_pool_init(adev);
2200 if (r) {
2201 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
Gavin Wan89041942017-06-23 13:55:15 -04002202 amdgpu_vf_error_put(AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r);
Alex Deucher83ba1262016-06-03 18:21:41 -04002203 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002204 }
2205
2206 r = amdgpu_ib_ring_tests(adev);
2207 if (r)
2208 DRM_ERROR("ib ring test failed (%d).\n", r);
2209
Monk Liu9bc92b92017-02-08 17:38:13 +08002210 amdgpu_fbdev_init(adev);
2211
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002212 r = amdgpu_gem_debugfs_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002213 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002214 DRM_ERROR("registering gem debugfs failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002215
2216 r = amdgpu_debugfs_regs_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002217 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002218 DRM_ERROR("registering register debugfs failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002219
Huang Rui4f0955f2017-05-10 23:04:06 +08002220 r = amdgpu_debugfs_test_ib_ring_init(adev);
2221 if (r)
2222 DRM_ERROR("registering register test ib ring debugfs failed (%d).\n", r);
2223
Huang Rui50ab2532016-06-12 15:51:09 +08002224 r = amdgpu_debugfs_firmware_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002225 if (r)
Huang Rui50ab2532016-06-12 15:51:09 +08002226 DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
Huang Rui50ab2532016-06-12 15:51:09 +08002227
Kent Russelldb95e212017-08-22 12:31:43 -04002228 r = amdgpu_debugfs_vbios_dump_init(adev);
2229 if (r)
2230 DRM_ERROR("Creating vbios dump debugfs failed (%d).\n", r);
2231
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002232 if ((amdgpu_testing & 1)) {
2233 if (adev->accel_working)
2234 amdgpu_test_moves(adev);
2235 else
2236 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
2237 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002238 if (amdgpu_benchmarking) {
2239 if (adev->accel_working)
2240 amdgpu_benchmark(adev, amdgpu_benchmarking);
2241 else
2242 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
2243 }
2244
2245 /* enable clockgating, etc. after ib tests, etc. since some blocks require
2246 * explicit gating rather than handling it automatically.
2247 */
2248 r = amdgpu_late_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002249 if (r) {
2250 dev_err(adev->dev, "amdgpu_late_init failed\n");
Gavin Wan89041942017-06-23 13:55:15 -04002251 amdgpu_vf_error_put(AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r);
Alex Deucher83ba1262016-06-03 18:21:41 -04002252 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002253 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002254
2255 return 0;
Alex Deucher83ba1262016-06-03 18:21:41 -04002256
2257failed:
Gavin Wan89041942017-06-23 13:55:15 -04002258 amdgpu_vf_error_trans_all(adev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002259 if (runtime)
2260 vga_switcheroo_fini_domain_pm_ops(adev->dev);
2261 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002262}
2263
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002264/**
2265 * amdgpu_device_fini - tear down the driver
2266 *
2267 * @adev: amdgpu_device pointer
2268 *
2269 * Tear down the driver info (all asics).
2270 * Called at driver shutdown.
2271 */
2272void amdgpu_device_fini(struct amdgpu_device *adev)
2273{
2274 int r;
2275
2276 DRM_INFO("amdgpu: finishing device.\n");
2277 adev->shutdown = true;
Pixel Dingdb2c2a92017-04-25 16:47:42 +08002278 if (adev->mode_info.mode_config_initialized)
2279 drm_crtc_force_disable_all(adev->ddev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002280 /* evict vram memory */
2281 amdgpu_bo_evict_vram(adev);
2282 amdgpu_ib_pool_fini(adev);
2283 amdgpu_fence_driver_fini(adev);
2284 amdgpu_fbdev_fini(adev);
2285 r = amdgpu_fini(adev);
Huang Ruiab4fe3e2017-06-05 22:11:59 +08002286 if (adev->firmware.gpu_info_fw) {
2287 release_firmware(adev->firmware.gpu_info_fw);
2288 adev->firmware.gpu_info_fw = NULL;
2289 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002290 adev->accel_working = false;
Shirish S2dc80b02017-05-25 10:05:25 +05302291 cancel_delayed_work_sync(&adev->late_init_work);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002292 /* free i2c buses */
2293 amdgpu_i2c_fini(adev);
2294 amdgpu_atombios_fini(adev);
2295 kfree(adev->bios);
2296 adev->bios = NULL;
Lukas Wunner84c8b222017-03-10 21:23:45 +01002297 if (!pci_is_thunderbolt_attached(adev->pdev))
2298 vga_switcheroo_unregister_client(adev->pdev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002299 if (adev->flags & AMD_IS_PX)
2300 vga_switcheroo_fini_domain_pm_ops(adev->dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002301 vga_client_register(adev->pdev, NULL, NULL, NULL);
2302 if (adev->rio_mem)
2303 pci_iounmap(adev->pdev, adev->rio_mem);
2304 adev->rio_mem = NULL;
2305 iounmap(adev->rmmio);
2306 adev->rmmio = NULL;
Ken Wangda69c1612016-01-21 19:08:55 +08002307 if (adev->asic_type >= CHIP_BONAIRE)
2308 amdgpu_doorbell_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002309 amdgpu_debugfs_regs_cleanup(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002310}
2311
2312
2313/*
2314 * Suspend & resume.
2315 */
2316/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002317 * amdgpu_device_suspend - initiate device suspend
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002318 *
2319 * @pdev: drm dev pointer
2320 * @state: suspend state
2321 *
2322 * Puts the hw in the suspend state (all asics).
2323 * Returns 0 for success or an error on failure.
2324 * Called at driver suspend.
2325 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002326int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002327{
2328 struct amdgpu_device *adev;
2329 struct drm_crtc *crtc;
2330 struct drm_connector *connector;
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002331 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002332
2333 if (dev == NULL || dev->dev_private == NULL) {
2334 return -ENODEV;
2335 }
2336
2337 adev = dev->dev_private;
2338
2339 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2340 return 0;
2341
2342 drm_kms_helper_poll_disable(dev);
2343
2344 /* turn off display hw */
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002345 drm_modeset_lock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002346 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2347 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
2348 }
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002349 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002350
Yong Zhaoba997702015-11-09 17:21:45 -05002351 amdgpu_amdkfd_suspend(adev);
2352
Alex Deucher756e6882015-10-08 00:03:36 -04002353 /* unpin the front buffers and cursors */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002354 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Alex Deucher756e6882015-10-08 00:03:36 -04002355 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002356 struct amdgpu_framebuffer *rfb = to_amdgpu_framebuffer(crtc->primary->fb);
2357 struct amdgpu_bo *robj;
2358
Alex Deucher756e6882015-10-08 00:03:36 -04002359 if (amdgpu_crtc->cursor_bo) {
2360 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
Alex Xie7a6901d2017-04-24 13:52:41 -04002361 r = amdgpu_bo_reserve(aobj, true);
Alex Deucher756e6882015-10-08 00:03:36 -04002362 if (r == 0) {
2363 amdgpu_bo_unpin(aobj);
2364 amdgpu_bo_unreserve(aobj);
2365 }
2366 }
2367
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002368 if (rfb == NULL || rfb->obj == NULL) {
2369 continue;
2370 }
2371 robj = gem_to_amdgpu_bo(rfb->obj);
2372 /* don't unpin kernel fb objects */
2373 if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
Alex Xie7a6901d2017-04-24 13:52:41 -04002374 r = amdgpu_bo_reserve(robj, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002375 if (r == 0) {
2376 amdgpu_bo_unpin(robj);
2377 amdgpu_bo_unreserve(robj);
2378 }
2379 }
2380 }
2381 /* evict vram memory */
2382 amdgpu_bo_evict_vram(adev);
2383
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002384 amdgpu_fence_driver_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002385
2386 r = amdgpu_suspend(adev);
2387
Alex Deuchera0a71e42016-10-10 12:41:36 -04002388 /* evict remaining vram memory
2389 * This second call to evict vram is to evict the gart page table
2390 * using the CPU.
2391 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002392 amdgpu_bo_evict_vram(adev);
2393
Alex Deucherd05da0e2017-06-30 17:08:45 -04002394 amdgpu_atombios_scratch_regs_save(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002395 pci_save_state(dev->pdev);
2396 if (suspend) {
2397 /* Shut down the device */
2398 pci_disable_device(dev->pdev);
2399 pci_set_power_state(dev->pdev, PCI_D3hot);
jimqu74b0b152016-09-07 17:09:12 +08002400 } else {
2401 r = amdgpu_asic_reset(adev);
2402 if (r)
2403 DRM_ERROR("amdgpu asic reset failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002404 }
2405
2406 if (fbcon) {
2407 console_lock();
2408 amdgpu_fbdev_set_suspend(adev, 1);
2409 console_unlock();
2410 }
2411 return 0;
2412}
2413
2414/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002415 * amdgpu_device_resume - initiate device resume
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002416 *
2417 * @pdev: drm dev pointer
2418 *
2419 * Bring the hw back to operating state (all asics).
2420 * Returns 0 for success or an error on failure.
2421 * Called at driver resume.
2422 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002423int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002424{
2425 struct drm_connector *connector;
2426 struct amdgpu_device *adev = dev->dev_private;
Alex Deucher756e6882015-10-08 00:03:36 -04002427 struct drm_crtc *crtc;
Huang Rui03161a62017-04-13 16:12:26 +08002428 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002429
2430 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2431 return 0;
2432
jimqu74b0b152016-09-07 17:09:12 +08002433 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002434 console_lock();
jimqu74b0b152016-09-07 17:09:12 +08002435
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002436 if (resume) {
2437 pci_set_power_state(dev->pdev, PCI_D0);
2438 pci_restore_state(dev->pdev);
jimqu74b0b152016-09-07 17:09:12 +08002439 r = pci_enable_device(dev->pdev);
Huang Rui03161a62017-04-13 16:12:26 +08002440 if (r)
2441 goto unlock;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002442 }
Alex Deucherd05da0e2017-06-30 17:08:45 -04002443 amdgpu_atombios_scratch_regs_restore(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002444
2445 /* post card */
Jim Quc836fec2017-02-10 15:59:59 +08002446 if (amdgpu_need_post(adev)) {
jimqu74b0b152016-09-07 17:09:12 +08002447 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2448 if (r)
2449 DRM_ERROR("amdgpu asic init failed\n");
2450 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002451
2452 r = amdgpu_resume(adev);
Rex Zhue6707212017-03-30 13:21:01 +08002453 if (r) {
Flora Cuica198522016-02-04 15:10:08 +08002454 DRM_ERROR("amdgpu_resume failed (%d).\n", r);
Huang Rui03161a62017-04-13 16:12:26 +08002455 goto unlock;
Rex Zhue6707212017-03-30 13:21:01 +08002456 }
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002457 amdgpu_fence_driver_resume(adev);
2458
Flora Cuica198522016-02-04 15:10:08 +08002459 if (resume) {
2460 r = amdgpu_ib_ring_tests(adev);
2461 if (r)
2462 DRM_ERROR("ib ring test failed (%d).\n", r);
2463 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002464
2465 r = amdgpu_late_init(adev);
Huang Rui03161a62017-04-13 16:12:26 +08002466 if (r)
2467 goto unlock;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002468
Alex Deucher756e6882015-10-08 00:03:36 -04002469 /* pin cursors */
2470 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2471 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2472
2473 if (amdgpu_crtc->cursor_bo) {
2474 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
Alex Xie7a6901d2017-04-24 13:52:41 -04002475 r = amdgpu_bo_reserve(aobj, true);
Alex Deucher756e6882015-10-08 00:03:36 -04002476 if (r == 0) {
2477 r = amdgpu_bo_pin(aobj,
2478 AMDGPU_GEM_DOMAIN_VRAM,
2479 &amdgpu_crtc->cursor_addr);
2480 if (r != 0)
2481 DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
2482 amdgpu_bo_unreserve(aobj);
2483 }
2484 }
2485 }
Yong Zhaoba997702015-11-09 17:21:45 -05002486 r = amdgpu_amdkfd_resume(adev);
2487 if (r)
2488 return r;
Alex Deucher756e6882015-10-08 00:03:36 -04002489
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002490 /* blat the mode back in */
2491 if (fbcon) {
2492 drm_helper_resume_force_mode(dev);
2493 /* turn on display hw */
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002494 drm_modeset_lock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002495 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2496 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
2497 }
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002498 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002499 }
2500
2501 drm_kms_helper_poll_enable(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002502
2503 /*
2504 * Most of the connector probing functions try to acquire runtime pm
2505 * refs to ensure that the GPU is powered on when connector polling is
2506 * performed. Since we're calling this from a runtime PM callback,
2507 * trying to acquire rpm refs will cause us to deadlock.
2508 *
2509 * Since we're guaranteed to be holding the rpm lock, it's safe to
2510 * temporarily disable the rpm helpers so this doesn't deadlock us.
2511 */
2512#ifdef CONFIG_PM
2513 dev->dev->power.disable_depth++;
2514#endif
Alex Deucher54fb2a52015-11-24 14:30:56 -05002515 drm_helper_hpd_irq_event(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002516#ifdef CONFIG_PM
2517 dev->dev->power.disable_depth--;
2518#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002519
Huang Rui03161a62017-04-13 16:12:26 +08002520 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002521 amdgpu_fbdev_set_suspend(adev, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002522
Huang Rui03161a62017-04-13 16:12:26 +08002523unlock:
2524 if (fbcon)
2525 console_unlock();
2526
2527 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002528}
2529
Chunming Zhou63fbf422016-07-15 11:19:20 +08002530static bool amdgpu_check_soft_reset(struct amdgpu_device *adev)
2531{
2532 int i;
2533 bool asic_hang = false;
2534
2535 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002536 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou63fbf422016-07-15 11:19:20 +08002537 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002538 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
2539 adev->ip_blocks[i].status.hang =
2540 adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
2541 if (adev->ip_blocks[i].status.hang) {
2542 DRM_INFO("IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
Chunming Zhou63fbf422016-07-15 11:19:20 +08002543 asic_hang = true;
2544 }
2545 }
2546 return asic_hang;
2547}
2548
Baoyou Xie4d446652016-09-18 22:09:35 +08002549static int amdgpu_pre_soft_reset(struct amdgpu_device *adev)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002550{
2551 int i, r = 0;
2552
2553 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002554 if (!adev->ip_blocks[i].status.valid)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002555 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002556 if (adev->ip_blocks[i].status.hang &&
2557 adev->ip_blocks[i].version->funcs->pre_soft_reset) {
2558 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
Chunming Zhoud31a5012016-07-18 10:04:34 +08002559 if (r)
2560 return r;
2561 }
2562 }
2563
2564 return 0;
2565}
2566
Chunming Zhou35d782f2016-07-15 15:57:13 +08002567static bool amdgpu_need_full_reset(struct amdgpu_device *adev)
2568{
Alex Deucherda146d32016-10-13 16:07:03 -04002569 int i;
2570
2571 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002572 if (!adev->ip_blocks[i].status.valid)
Alex Deucherda146d32016-10-13 16:07:03 -04002573 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002574 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
2575 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
2576 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
2577 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE)) {
2578 if (adev->ip_blocks[i].status.hang) {
Alex Deucherda146d32016-10-13 16:07:03 -04002579 DRM_INFO("Some block need full reset!\n");
2580 return true;
2581 }
2582 }
Chunming Zhou35d782f2016-07-15 15:57:13 +08002583 }
2584 return false;
2585}
2586
2587static int amdgpu_soft_reset(struct amdgpu_device *adev)
2588{
2589 int i, r = 0;
2590
2591 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002592 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002593 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002594 if (adev->ip_blocks[i].status.hang &&
2595 adev->ip_blocks[i].version->funcs->soft_reset) {
2596 r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002597 if (r)
2598 return r;
2599 }
2600 }
2601
2602 return 0;
2603}
2604
2605static int amdgpu_post_soft_reset(struct amdgpu_device *adev)
2606{
2607 int i, r = 0;
2608
2609 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002610 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002611 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002612 if (adev->ip_blocks[i].status.hang &&
2613 adev->ip_blocks[i].version->funcs->post_soft_reset)
2614 r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002615 if (r)
2616 return r;
2617 }
2618
2619 return 0;
2620}
2621
Chunming Zhou3ad81f12016-08-05 17:30:17 +08002622bool amdgpu_need_backup(struct amdgpu_device *adev)
2623{
2624 if (adev->flags & AMD_IS_APU)
2625 return false;
2626
2627 return amdgpu_lockup_timeout > 0 ? true : false;
2628}
2629
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002630static int amdgpu_recover_vram_from_shadow(struct amdgpu_device *adev,
2631 struct amdgpu_ring *ring,
2632 struct amdgpu_bo *bo,
Chris Wilsonf54d1862016-10-25 13:00:45 +01002633 struct dma_fence **fence)
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002634{
2635 uint32_t domain;
2636 int r;
2637
Roger.He23d2e502017-04-21 14:24:26 +08002638 if (!bo->shadow)
2639 return 0;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002640
Alex Xie1d284792017-04-24 13:53:04 -04002641 r = amdgpu_bo_reserve(bo, true);
Roger.He23d2e502017-04-21 14:24:26 +08002642 if (r)
2643 return r;
2644 domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
2645 /* if bo has been evicted, then no need to recover */
2646 if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
Roger.He82521312017-04-21 13:08:43 +08002647 r = amdgpu_bo_validate(bo->shadow);
2648 if (r) {
2649 DRM_ERROR("bo validate failed!\n");
2650 goto err;
2651 }
2652
Roger.He23d2e502017-04-21 14:24:26 +08002653 r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002654 NULL, fence, true);
Roger.He23d2e502017-04-21 14:24:26 +08002655 if (r) {
2656 DRM_ERROR("recover page table failed!\n");
2657 goto err;
2658 }
2659 }
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002660err:
Roger.He23d2e502017-04-21 14:24:26 +08002661 amdgpu_bo_unreserve(bo);
2662 return r;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002663}
2664
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002665/**
Monk Liua90ad3c2017-01-23 14:22:08 +08002666 * amdgpu_sriov_gpu_reset - reset the asic
2667 *
2668 * @adev: amdgpu device pointer
Monk Liu7225f872017-04-26 14:51:54 +08002669 * @job: which job trigger hang
Monk Liua90ad3c2017-01-23 14:22:08 +08002670 *
2671 * Attempt the reset the GPU if it has hung (all asics).
2672 * for SRIOV case.
2673 * Returns 0 for success or an error on failure.
2674 */
Monk Liu7225f872017-04-26 14:51:54 +08002675int amdgpu_sriov_gpu_reset(struct amdgpu_device *adev, struct amdgpu_job *job)
Monk Liua90ad3c2017-01-23 14:22:08 +08002676{
Monk Liu65781c72017-05-11 13:36:44 +08002677 int i, j, r = 0;
Monk Liua90ad3c2017-01-23 14:22:08 +08002678 int resched;
2679 struct amdgpu_bo *bo, *tmp;
2680 struct amdgpu_ring *ring;
2681 struct dma_fence *fence = NULL, *next = NULL;
2682
Monk Liu147b5982017-01-25 15:48:01 +08002683 mutex_lock(&adev->virt.lock_reset);
Monk Liua90ad3c2017-01-23 14:22:08 +08002684 atomic_inc(&adev->gpu_reset_counter);
Monk Liu1fb37a32017-01-26 15:36:37 +08002685 adev->gfx.in_reset = true;
Monk Liua90ad3c2017-01-23 14:22:08 +08002686
2687 /* block TTM */
2688 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
2689
Monk Liu65781c72017-05-11 13:36:44 +08002690 /* we start from the ring trigger GPU hang */
2691 j = job ? job->ring->idx : 0;
Monk Liua90ad3c2017-01-23 14:22:08 +08002692
Monk Liu65781c72017-05-11 13:36:44 +08002693 /* block scheduler */
2694 for (i = j; i < j + AMDGPU_MAX_RINGS; ++i) {
2695 ring = adev->rings[i % AMDGPU_MAX_RINGS];
Monk Liua90ad3c2017-01-23 14:22:08 +08002696 if (!ring || !ring->sched.thread)
2697 continue;
2698
2699 kthread_park(ring->sched.thread);
Monk Liua90ad3c2017-01-23 14:22:08 +08002700
Monk Liu65781c72017-05-11 13:36:44 +08002701 if (job && j != i)
2702 continue;
2703
Monk Liu4f059ec2017-05-11 13:59:15 +08002704 /* here give the last chance to check if job removed from mirror-list
Monk Liu65781c72017-05-11 13:36:44 +08002705 * since we already pay some time on kthread_park */
Monk Liu4f059ec2017-05-11 13:59:15 +08002706 if (job && list_empty(&job->base.node)) {
Monk Liu65781c72017-05-11 13:36:44 +08002707 kthread_unpark(ring->sched.thread);
2708 goto give_up_reset;
2709 }
2710
2711 if (amd_sched_invalidate_job(&job->base, amdgpu_job_hang_limit))
2712 amd_sched_job_kickout(&job->base);
2713
2714 /* only do job_reset on the hang ring if @job not NULL */
Monk Liua90ad3c2017-01-23 14:22:08 +08002715 amd_sched_hw_job_reset(&ring->sched);
Monk Liu65781c72017-05-11 13:36:44 +08002716
2717 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
2718 amdgpu_fence_driver_force_completion_ring(ring);
Monk Liua90ad3c2017-01-23 14:22:08 +08002719 }
2720
Monk Liua90ad3c2017-01-23 14:22:08 +08002721 /* request to take full control of GPU before re-initialization */
Monk Liu7225f872017-04-26 14:51:54 +08002722 if (job)
Monk Liua90ad3c2017-01-23 14:22:08 +08002723 amdgpu_virt_reset_gpu(adev);
2724 else
2725 amdgpu_virt_request_full_gpu(adev, true);
2726
2727
2728 /* Resume IP prior to SMC */
Monk Liue4f0fdc2017-02-09 11:55:49 +08002729 amdgpu_sriov_reinit_early(adev);
Monk Liua90ad3c2017-01-23 14:22:08 +08002730
2731 /* we need recover gart prior to run SMC/CP/SDMA resume */
2732 amdgpu_ttm_recover_gart(adev);
2733
2734 /* now we are okay to resume SMC/CP/SDMA */
Monk Liue4f0fdc2017-02-09 11:55:49 +08002735 amdgpu_sriov_reinit_late(adev);
Monk Liua90ad3c2017-01-23 14:22:08 +08002736
2737 amdgpu_irq_gpu_reset_resume_helper(adev);
2738
2739 if (amdgpu_ib_ring_tests(adev))
2740 dev_err(adev->dev, "[GPU_RESET] ib ring test failed (%d).\n", r);
2741
2742 /* release full control of GPU after ib test */
2743 amdgpu_virt_release_full_gpu(adev, true);
2744
2745 DRM_INFO("recover vram bo from shadow\n");
2746
2747 ring = adev->mman.buffer_funcs_ring;
2748 mutex_lock(&adev->shadow_list_lock);
2749 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
Monk Liu236763d2017-05-01 16:15:31 +08002750 next = NULL;
Monk Liua90ad3c2017-01-23 14:22:08 +08002751 amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
2752 if (fence) {
2753 r = dma_fence_wait(fence, false);
2754 if (r) {
2755 WARN(r, "recovery from shadow isn't completed\n");
2756 break;
2757 }
2758 }
2759
2760 dma_fence_put(fence);
2761 fence = next;
2762 }
2763 mutex_unlock(&adev->shadow_list_lock);
2764
2765 if (fence) {
2766 r = dma_fence_wait(fence, false);
2767 if (r)
2768 WARN(r, "recovery from shadow isn't completed\n");
2769 }
2770 dma_fence_put(fence);
2771
Monk Liu65781c72017-05-11 13:36:44 +08002772 for (i = j; i < j + AMDGPU_MAX_RINGS; ++i) {
2773 ring = adev->rings[i % AMDGPU_MAX_RINGS];
Monk Liua90ad3c2017-01-23 14:22:08 +08002774 if (!ring || !ring->sched.thread)
2775 continue;
2776
Monk Liu65781c72017-05-11 13:36:44 +08002777 if (job && j != i) {
2778 kthread_unpark(ring->sched.thread);
2779 continue;
2780 }
2781
Monk Liua90ad3c2017-01-23 14:22:08 +08002782 amd_sched_job_recovery(&ring->sched);
2783 kthread_unpark(ring->sched.thread);
2784 }
2785
2786 drm_helper_resume_force_mode(adev->ddev);
Monk Liu65781c72017-05-11 13:36:44 +08002787give_up_reset:
Monk Liua90ad3c2017-01-23 14:22:08 +08002788 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
2789 if (r) {
2790 /* bad news, how to tell it to userspace ? */
2791 dev_info(adev->dev, "GPU reset failed\n");
Monk Liu65781c72017-05-11 13:36:44 +08002792 } else {
2793 dev_info(adev->dev, "GPU reset successed!\n");
Monk Liua90ad3c2017-01-23 14:22:08 +08002794 }
2795
Monk Liu1fb37a32017-01-26 15:36:37 +08002796 adev->gfx.in_reset = false;
Monk Liu147b5982017-01-25 15:48:01 +08002797 mutex_unlock(&adev->virt.lock_reset);
Monk Liua90ad3c2017-01-23 14:22:08 +08002798 return r;
2799}
2800
2801/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002802 * amdgpu_gpu_reset - reset the asic
2803 *
2804 * @adev: amdgpu device pointer
2805 *
2806 * Attempt the reset the GPU if it has hung (all asics).
2807 * Returns 0 for success or an error on failure.
2808 */
2809int amdgpu_gpu_reset(struct amdgpu_device *adev)
2810{
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002811 int i, r;
2812 int resched;
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002813 bool need_full_reset, vram_lost = false;
Xiangliang Yufb140b22016-12-17 22:48:57 +08002814
Chunming Zhou63fbf422016-07-15 11:19:20 +08002815 if (!amdgpu_check_soft_reset(adev)) {
2816 DRM_INFO("No hardware hang detected. Did some blocks stall?\n");
2817 return 0;
2818 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002819
Marek Olšákd94aed52015-05-05 21:13:49 +02002820 atomic_inc(&adev->gpu_reset_counter);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002821
Chunming Zhoua3c47d62016-06-30 16:44:41 +08002822 /* block TTM */
2823 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
2824
Chunming Zhou0875dc92016-06-12 15:41:58 +08002825 /* block scheduler */
2826 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2827 struct amdgpu_ring *ring = adev->rings[i];
2828
Chunming Zhou51687752017-04-24 17:09:15 +08002829 if (!ring || !ring->sched.thread)
Chunming Zhou0875dc92016-06-12 15:41:58 +08002830 continue;
2831 kthread_park(ring->sched.thread);
Chunming Zhouaa1c8902016-06-30 13:56:02 +08002832 amd_sched_hw_job_reset(&ring->sched);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002833 }
Chunming Zhou2200eda2016-06-30 16:53:02 +08002834 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
2835 amdgpu_fence_driver_force_completion(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002836
Chunming Zhou35d782f2016-07-15 15:57:13 +08002837 need_full_reset = amdgpu_need_full_reset(adev);
2838
2839 if (!need_full_reset) {
2840 amdgpu_pre_soft_reset(adev);
2841 r = amdgpu_soft_reset(adev);
2842 amdgpu_post_soft_reset(adev);
2843 if (r || amdgpu_check_soft_reset(adev)) {
2844 DRM_INFO("soft reset failed, will fallback to full reset!\n");
2845 need_full_reset = true;
2846 }
2847 }
2848
2849 if (need_full_reset) {
Chunming Zhou35d782f2016-07-15 15:57:13 +08002850 r = amdgpu_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002851
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002852retry:
Alex Deucherd05da0e2017-06-30 17:08:45 -04002853 amdgpu_atombios_scratch_regs_save(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002854 r = amdgpu_asic_reset(adev);
Alex Deucherd05da0e2017-06-30 17:08:45 -04002855 amdgpu_atombios_scratch_regs_restore(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002856 /* post card */
2857 amdgpu_atom_asic_init(adev->mode_info.atom_context);
Alex Deucherbfa99262016-01-15 11:59:48 -05002858
Chunming Zhou35d782f2016-07-15 15:57:13 +08002859 if (!r) {
2860 dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
Chunming Zhoufcf06492017-05-05 10:33:33 +08002861 r = amdgpu_resume_phase1(adev);
2862 if (r)
2863 goto out;
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002864 vram_lost = amdgpu_check_vram_lost(adev);
Chunming Zhouf1892132017-05-15 16:48:27 +08002865 if (vram_lost) {
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002866 DRM_ERROR("VRAM is lost!\n");
Chunming Zhouf1892132017-05-15 16:48:27 +08002867 atomic_inc(&adev->vram_lost_counter);
2868 }
Chunming Zhou2c0d7312016-08-30 16:36:25 +08002869 r = amdgpu_ttm_recover_gart(adev);
2870 if (r)
Chunming Zhoufcf06492017-05-05 10:33:33 +08002871 goto out;
2872 r = amdgpu_resume_phase2(adev);
2873 if (r)
2874 goto out;
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002875 if (vram_lost)
2876 amdgpu_fill_reset_magic(adev);
Chunming Zhou2c0d7312016-08-30 16:36:25 +08002877 }
Chunming Zhoufcf06492017-05-05 10:33:33 +08002878 }
2879out:
2880 if (!r) {
2881 amdgpu_irq_gpu_reset_resume_helper(adev);
Chunming Zhou1f465082016-06-30 15:02:26 +08002882 r = amdgpu_ib_ring_tests(adev);
2883 if (r) {
2884 dev_err(adev->dev, "ib ring test failed (%d).\n", r);
Chunming Zhou40019dc2016-06-29 16:01:49 +08002885 r = amdgpu_suspend(adev);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002886 need_full_reset = true;
Chunming Zhou40019dc2016-06-29 16:01:49 +08002887 goto retry;
Chunming Zhou1f465082016-06-30 15:02:26 +08002888 }
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002889 /**
2890 * recovery vm page tables, since we cannot depend on VRAM is
2891 * consistent after gpu full reset.
2892 */
2893 if (need_full_reset && amdgpu_need_backup(adev)) {
2894 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
2895 struct amdgpu_bo *bo, *tmp;
Chris Wilsonf54d1862016-10-25 13:00:45 +01002896 struct dma_fence *fence = NULL, *next = NULL;
Chunming Zhou1f465082016-06-30 15:02:26 +08002897
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002898 DRM_INFO("recover vram bo from shadow\n");
2899 mutex_lock(&adev->shadow_list_lock);
2900 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
Monk Liu236763d2017-05-01 16:15:31 +08002901 next = NULL;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002902 amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
2903 if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01002904 r = dma_fence_wait(fence, false);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002905 if (r) {
Monk Liu1d7b17b2017-01-22 18:52:56 +08002906 WARN(r, "recovery from shadow isn't completed\n");
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002907 break;
2908 }
2909 }
2910
Chris Wilsonf54d1862016-10-25 13:00:45 +01002911 dma_fence_put(fence);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002912 fence = next;
2913 }
2914 mutex_unlock(&adev->shadow_list_lock);
2915 if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01002916 r = dma_fence_wait(fence, false);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002917 if (r)
Monk Liu1d7b17b2017-01-22 18:52:56 +08002918 WARN(r, "recovery from shadow isn't completed\n");
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002919 }
Chris Wilsonf54d1862016-10-25 13:00:45 +01002920 dma_fence_put(fence);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002921 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002922 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2923 struct amdgpu_ring *ring = adev->rings[i];
Chunming Zhou51687752017-04-24 17:09:15 +08002924
2925 if (!ring || !ring->sched.thread)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002926 continue;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002927
Chunming Zhouaa1c8902016-06-30 13:56:02 +08002928 amd_sched_job_recovery(&ring->sched);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002929 kthread_unpark(ring->sched.thread);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002930 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002931 } else {
Chunming Zhou2200eda2016-06-30 16:53:02 +08002932 dev_err(adev->dev, "asic resume failed (%d).\n", r);
Gavin Wan89041942017-06-23 13:55:15 -04002933 amdgpu_vf_error_put(AMDGIM_ERROR_VF_ASIC_RESUME_FAIL, 0, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002934 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
Chunming Zhou51687752017-04-24 17:09:15 +08002935 if (adev->rings[i] && adev->rings[i]->sched.thread) {
Chunming Zhou0875dc92016-06-12 15:41:58 +08002936 kthread_unpark(adev->rings[i]->sched.thread);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002937 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002938 }
2939 }
2940
2941 drm_helper_resume_force_mode(adev->ddev);
2942
2943 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
Gavin Wan89041942017-06-23 13:55:15 -04002944 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002945 /* bad news, how to tell it to userspace ? */
2946 dev_info(adev->dev, "GPU reset failed\n");
Gavin Wan89041942017-06-23 13:55:15 -04002947 amdgpu_vf_error_put(AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, r);
2948 }
2949 else {
Chunming Zhou6643be62017-05-05 10:50:09 +08002950 dev_info(adev->dev, "GPU reset successed!\n");
Gavin Wan89041942017-06-23 13:55:15 -04002951 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002952
Gavin Wan89041942017-06-23 13:55:15 -04002953 amdgpu_vf_error_trans_all(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002954 return r;
2955}
2956
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002957void amdgpu_get_pcie_info(struct amdgpu_device *adev)
2958{
2959 u32 mask;
2960 int ret;
2961
Alex Deuchercd474ba2016-02-04 10:21:23 -05002962 if (amdgpu_pcie_gen_cap)
2963 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
2964
2965 if (amdgpu_pcie_lane_cap)
2966 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
2967
2968 /* covers APUs as well */
2969 if (pci_is_root_bus(adev->pdev->bus)) {
2970 if (adev->pm.pcie_gen_mask == 0)
2971 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
2972 if (adev->pm.pcie_mlw_mask == 0)
2973 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002974 return;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002975 }
Alex Deuchercd474ba2016-02-04 10:21:23 -05002976
2977 if (adev->pm.pcie_gen_mask == 0) {
2978 ret = drm_pcie_get_speed_cap_mask(adev->ddev, &mask);
2979 if (!ret) {
2980 adev->pm.pcie_gen_mask = (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
2981 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
2982 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
2983
2984 if (mask & DRM_PCIE_SPEED_25)
2985 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
2986 if (mask & DRM_PCIE_SPEED_50)
2987 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2;
2988 if (mask & DRM_PCIE_SPEED_80)
2989 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3;
2990 } else {
2991 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
2992 }
2993 }
2994 if (adev->pm.pcie_mlw_mask == 0) {
2995 ret = drm_pcie_get_max_link_width(adev->ddev, &mask);
2996 if (!ret) {
2997 switch (mask) {
2998 case 32:
2999 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
3000 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
3001 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3002 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3003 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3004 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3005 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3006 break;
3007 case 16:
3008 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
3009 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3010 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3011 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3012 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3013 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3014 break;
3015 case 12:
3016 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3017 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3018 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3019 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3020 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3021 break;
3022 case 8:
3023 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3024 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3025 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3026 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3027 break;
3028 case 4:
3029 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3030 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3031 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3032 break;
3033 case 2:
3034 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3035 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3036 break;
3037 case 1:
3038 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
3039 break;
3040 default:
3041 break;
3042 }
3043 } else {
3044 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003045 }
3046 }
3047}
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003048
3049/*
3050 * Debugfs
3051 */
3052int amdgpu_debugfs_add_files(struct amdgpu_device *adev,
Nils Wallménius06ab6832016-05-02 12:46:15 -04003053 const struct drm_info_list *files,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003054 unsigned nfiles)
3055{
3056 unsigned i;
3057
3058 for (i = 0; i < adev->debugfs_count; i++) {
3059 if (adev->debugfs[i].files == files) {
3060 /* Already registered */
3061 return 0;
3062 }
3063 }
3064
3065 i = adev->debugfs_count + 1;
3066 if (i > AMDGPU_DEBUGFS_MAX_COMPONENTS) {
3067 DRM_ERROR("Reached maximum number of debugfs components.\n");
3068 DRM_ERROR("Report so we increase "
3069 "AMDGPU_DEBUGFS_MAX_COMPONENTS.\n");
3070 return -EINVAL;
3071 }
3072 adev->debugfs[adev->debugfs_count].files = files;
3073 adev->debugfs[adev->debugfs_count].num_files = nfiles;
3074 adev->debugfs_count = i;
3075#if defined(CONFIG_DEBUG_FS)
3076 drm_debugfs_create_files(files, nfiles,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003077 adev->ddev->primary->debugfs_root,
3078 adev->ddev->primary);
3079#endif
3080 return 0;
3081}
3082
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003083#if defined(CONFIG_DEBUG_FS)
3084
3085static ssize_t amdgpu_debugfs_regs_read(struct file *f, char __user *buf,
3086 size_t size, loff_t *pos)
3087{
Al Viro45063092016-12-04 18:24:56 -05003088 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003089 ssize_t result = 0;
3090 int r;
Tom St Denisbd122672016-07-28 09:39:22 -04003091 bool pm_pg_lock, use_bank;
Tom St Denis566281592016-06-27 11:55:07 -04003092 unsigned instance_bank, sh_bank, se_bank;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003093
3094 if (size & 0x3 || *pos & 0x3)
3095 return -EINVAL;
3096
Tom St Denisbd122672016-07-28 09:39:22 -04003097 /* are we reading registers for which a PG lock is necessary? */
3098 pm_pg_lock = (*pos >> 23) & 1;
3099
Tom St Denis566281592016-06-27 11:55:07 -04003100 if (*pos & (1ULL << 62)) {
3101 se_bank = (*pos >> 24) & 0x3FF;
3102 sh_bank = (*pos >> 34) & 0x3FF;
3103 instance_bank = (*pos >> 44) & 0x3FF;
Tom St Denis32977f92016-10-09 07:41:26 -04003104
3105 if (se_bank == 0x3FF)
3106 se_bank = 0xFFFFFFFF;
3107 if (sh_bank == 0x3FF)
3108 sh_bank = 0xFFFFFFFF;
3109 if (instance_bank == 0x3FF)
3110 instance_bank = 0xFFFFFFFF;
Tom St Denis566281592016-06-27 11:55:07 -04003111 use_bank = 1;
Tom St Denis566281592016-06-27 11:55:07 -04003112 } else {
3113 use_bank = 0;
3114 }
3115
Tom St Denis801a6aa9a62017-03-15 05:34:25 -04003116 *pos &= (1UL << 22) - 1;
Tom St Denisbd122672016-07-28 09:39:22 -04003117
Tom St Denis566281592016-06-27 11:55:07 -04003118 if (use_bank) {
Tom St Denis32977f92016-10-09 07:41:26 -04003119 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
3120 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
Tom St Denis566281592016-06-27 11:55:07 -04003121 return -EINVAL;
3122 mutex_lock(&adev->grbm_idx_mutex);
3123 amdgpu_gfx_select_se_sh(adev, se_bank,
3124 sh_bank, instance_bank);
3125 }
3126
Tom St Denisbd122672016-07-28 09:39:22 -04003127 if (pm_pg_lock)
3128 mutex_lock(&adev->pm.mutex);
3129
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003130 while (size) {
3131 uint32_t value;
3132
3133 if (*pos > adev->rmmio_size)
Tom St Denis566281592016-06-27 11:55:07 -04003134 goto end;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003135
3136 value = RREG32(*pos >> 2);
3137 r = put_user(value, (uint32_t *)buf);
Tom St Denis566281592016-06-27 11:55:07 -04003138 if (r) {
3139 result = r;
3140 goto end;
3141 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003142
3143 result += 4;
3144 buf += 4;
3145 *pos += 4;
3146 size -= 4;
3147 }
3148
Tom St Denis566281592016-06-27 11:55:07 -04003149end:
3150 if (use_bank) {
3151 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
3152 mutex_unlock(&adev->grbm_idx_mutex);
3153 }
3154
Tom St Denisbd122672016-07-28 09:39:22 -04003155 if (pm_pg_lock)
3156 mutex_unlock(&adev->pm.mutex);
3157
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003158 return result;
3159}
3160
3161static ssize_t amdgpu_debugfs_regs_write(struct file *f, const 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 Denis394fdde2016-10-10 07:31:23 -04003167 bool pm_pg_lock, use_bank;
3168 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 Denis394fdde2016-10-10 07:31:23 -04003173 /* are we reading registers for which a PG lock is necessary? */
3174 pm_pg_lock = (*pos >> 23) & 1;
3175
3176 if (*pos & (1ULL << 62)) {
3177 se_bank = (*pos >> 24) & 0x3FF;
3178 sh_bank = (*pos >> 34) & 0x3FF;
3179 instance_bank = (*pos >> 44) & 0x3FF;
3180
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;
3187 use_bank = 1;
3188 } else {
3189 use_bank = 0;
3190 }
3191
Tom St Denis801a6aa9a62017-03-15 05:34:25 -04003192 *pos &= (1UL << 22) - 1;
Tom St Denis394fdde2016-10-10 07:31:23 -04003193
3194 if (use_bank) {
3195 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))
3197 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
3203 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)
3210 return result;
3211
3212 r = get_user(value, (uint32_t *)buf);
3213 if (r)
3214 return r;
3215
3216 WREG32(*pos >> 2, value);
3217
3218 result += 4;
3219 buf += 4;
3220 *pos += 4;
3221 size -= 4;
3222 }
3223
Tom St Denis394fdde2016-10-10 07:31:23 -04003224 if (use_bank) {
3225 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
3226 mutex_unlock(&adev->grbm_idx_mutex);
3227 }
3228
3229 if (pm_pg_lock)
3230 mutex_unlock(&adev->pm.mutex);
3231
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003232 return result;
3233}
3234
Tom St Denisadcec282016-04-15 13:08:44 -04003235static ssize_t amdgpu_debugfs_regs_pcie_read(struct file *f, char __user *buf,
3236 size_t size, loff_t *pos)
3237{
Al Viro45063092016-12-04 18:24:56 -05003238 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003239 ssize_t result = 0;
3240 int r;
3241
3242 if (size & 0x3 || *pos & 0x3)
3243 return -EINVAL;
3244
3245 while (size) {
3246 uint32_t value;
3247
3248 value = RREG32_PCIE(*pos >> 2);
3249 r = put_user(value, (uint32_t *)buf);
3250 if (r)
3251 return r;
3252
3253 result += 4;
3254 buf += 4;
3255 *pos += 4;
3256 size -= 4;
3257 }
3258
3259 return result;
3260}
3261
3262static ssize_t amdgpu_debugfs_regs_pcie_write(struct file *f, const char __user *buf,
3263 size_t size, loff_t *pos)
3264{
Al Viro45063092016-12-04 18:24:56 -05003265 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003266 ssize_t result = 0;
3267 int r;
3268
3269 if (size & 0x3 || *pos & 0x3)
3270 return -EINVAL;
3271
3272 while (size) {
3273 uint32_t value;
3274
3275 r = get_user(value, (uint32_t *)buf);
3276 if (r)
3277 return r;
3278
3279 WREG32_PCIE(*pos >> 2, value);
3280
3281 result += 4;
3282 buf += 4;
3283 *pos += 4;
3284 size -= 4;
3285 }
3286
3287 return result;
3288}
3289
3290static ssize_t amdgpu_debugfs_regs_didt_read(struct file *f, char __user *buf,
3291 size_t size, loff_t *pos)
3292{
Al Viro45063092016-12-04 18:24:56 -05003293 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003294 ssize_t result = 0;
3295 int r;
3296
3297 if (size & 0x3 || *pos & 0x3)
3298 return -EINVAL;
3299
3300 while (size) {
3301 uint32_t value;
3302
3303 value = RREG32_DIDT(*pos >> 2);
3304 r = put_user(value, (uint32_t *)buf);
3305 if (r)
3306 return r;
3307
3308 result += 4;
3309 buf += 4;
3310 *pos += 4;
3311 size -= 4;
3312 }
3313
3314 return result;
3315}
3316
3317static ssize_t amdgpu_debugfs_regs_didt_write(struct file *f, const char __user *buf,
3318 size_t size, loff_t *pos)
3319{
Al Viro45063092016-12-04 18:24:56 -05003320 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003321 ssize_t result = 0;
3322 int r;
3323
3324 if (size & 0x3 || *pos & 0x3)
3325 return -EINVAL;
3326
3327 while (size) {
3328 uint32_t value;
3329
3330 r = get_user(value, (uint32_t *)buf);
3331 if (r)
3332 return r;
3333
3334 WREG32_DIDT(*pos >> 2, value);
3335
3336 result += 4;
3337 buf += 4;
3338 *pos += 4;
3339 size -= 4;
3340 }
3341
3342 return result;
3343}
3344
3345static ssize_t amdgpu_debugfs_regs_smc_read(struct file *f, char __user *buf,
3346 size_t size, loff_t *pos)
3347{
Al Viro45063092016-12-04 18:24:56 -05003348 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003349 ssize_t result = 0;
3350 int r;
3351
3352 if (size & 0x3 || *pos & 0x3)
3353 return -EINVAL;
3354
3355 while (size) {
3356 uint32_t value;
3357
Tom St Denis6fc0dea2016-08-29 08:39:29 -04003358 value = RREG32_SMC(*pos);
Tom St Denisadcec282016-04-15 13:08:44 -04003359 r = put_user(value, (uint32_t *)buf);
3360 if (r)
3361 return r;
3362
3363 result += 4;
3364 buf += 4;
3365 *pos += 4;
3366 size -= 4;
3367 }
3368
3369 return result;
3370}
3371
3372static ssize_t amdgpu_debugfs_regs_smc_write(struct file *f, const char __user *buf,
3373 size_t size, loff_t *pos)
3374{
Al Viro45063092016-12-04 18:24:56 -05003375 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003376 ssize_t result = 0;
3377 int r;
3378
3379 if (size & 0x3 || *pos & 0x3)
3380 return -EINVAL;
3381
3382 while (size) {
3383 uint32_t value;
3384
3385 r = get_user(value, (uint32_t *)buf);
3386 if (r)
3387 return r;
3388
Tom St Denis6fc0dea2016-08-29 08:39:29 -04003389 WREG32_SMC(*pos, value);
Tom St Denisadcec282016-04-15 13:08:44 -04003390
3391 result += 4;
3392 buf += 4;
3393 *pos += 4;
3394 size -= 4;
3395 }
3396
3397 return result;
3398}
3399
Tom St Denis1e051412016-06-27 09:57:18 -04003400static ssize_t amdgpu_debugfs_gca_config_read(struct file *f, char __user *buf,
3401 size_t size, loff_t *pos)
3402{
Al Viro45063092016-12-04 18:24:56 -05003403 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denis1e051412016-06-27 09:57:18 -04003404 ssize_t result = 0;
3405 int r;
3406 uint32_t *config, no_regs = 0;
3407
3408 if (size & 0x3 || *pos & 0x3)
3409 return -EINVAL;
3410
Markus Elfringecab7662016-09-18 17:00:52 +02003411 config = kmalloc_array(256, sizeof(*config), GFP_KERNEL);
Tom St Denis1e051412016-06-27 09:57:18 -04003412 if (!config)
3413 return -ENOMEM;
3414
3415 /* version, increment each time something is added */
Tom St Denis9a999352017-01-18 13:01:25 -05003416 config[no_regs++] = 3;
Tom St Denis1e051412016-06-27 09:57:18 -04003417 config[no_regs++] = adev->gfx.config.max_shader_engines;
3418 config[no_regs++] = adev->gfx.config.max_tile_pipes;
3419 config[no_regs++] = adev->gfx.config.max_cu_per_sh;
3420 config[no_regs++] = adev->gfx.config.max_sh_per_se;
3421 config[no_regs++] = adev->gfx.config.max_backends_per_se;
3422 config[no_regs++] = adev->gfx.config.max_texture_channel_caches;
3423 config[no_regs++] = adev->gfx.config.max_gprs;
3424 config[no_regs++] = adev->gfx.config.max_gs_threads;
3425 config[no_regs++] = adev->gfx.config.max_hw_contexts;
3426 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_frontend;
3427 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_backend;
3428 config[no_regs++] = adev->gfx.config.sc_hiz_tile_fifo_size;
3429 config[no_regs++] = adev->gfx.config.sc_earlyz_tile_fifo_size;
3430 config[no_regs++] = adev->gfx.config.num_tile_pipes;
3431 config[no_regs++] = adev->gfx.config.backend_enable_mask;
3432 config[no_regs++] = adev->gfx.config.mem_max_burst_length_bytes;
3433 config[no_regs++] = adev->gfx.config.mem_row_size_in_kb;
3434 config[no_regs++] = adev->gfx.config.shader_engine_tile_size;
3435 config[no_regs++] = adev->gfx.config.num_gpus;
3436 config[no_regs++] = adev->gfx.config.multi_gpu_tile_size;
3437 config[no_regs++] = adev->gfx.config.mc_arb_ramcfg;
3438 config[no_regs++] = adev->gfx.config.gb_addr_config;
3439 config[no_regs++] = adev->gfx.config.num_rbs;
3440
Tom St Denis89a8f302016-08-12 15:14:31 -04003441 /* rev==1 */
3442 config[no_regs++] = adev->rev_id;
3443 config[no_regs++] = adev->pg_flags;
3444 config[no_regs++] = adev->cg_flags;
3445
Tom St Denise9f11dc2016-08-17 12:00:51 -04003446 /* rev==2 */
3447 config[no_regs++] = adev->family;
3448 config[no_regs++] = adev->external_rev_id;
3449
Tom St Denis9a999352017-01-18 13:01:25 -05003450 /* rev==3 */
3451 config[no_regs++] = adev->pdev->device;
3452 config[no_regs++] = adev->pdev->revision;
3453 config[no_regs++] = adev->pdev->subsystem_device;
3454 config[no_regs++] = adev->pdev->subsystem_vendor;
3455
Tom St Denis1e051412016-06-27 09:57:18 -04003456 while (size && (*pos < no_regs * 4)) {
3457 uint32_t value;
3458
3459 value = config[*pos >> 2];
3460 r = put_user(value, (uint32_t *)buf);
3461 if (r) {
3462 kfree(config);
3463 return r;
3464 }
3465
3466 result += 4;
3467 buf += 4;
3468 *pos += 4;
3469 size -= 4;
3470 }
3471
3472 kfree(config);
3473 return result;
3474}
3475
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003476static ssize_t amdgpu_debugfs_sensor_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 Denis9f8df7d2017-02-09 14:29:01 -05003480 int idx, x, outsize, r, valuesize;
3481 uint32_t values[16];
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003482
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003483 if (size & 3 || *pos & 0x3)
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003484 return -EINVAL;
3485
Samuel Pitoiset3cbc6142017-02-15 19:32:29 +01003486 if (amdgpu_dpm == 0)
3487 return -EINVAL;
3488
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003489 /* convert offset to sensor number */
3490 idx = *pos >> 2;
3491
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003492 valuesize = sizeof(values);
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003493 if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->read_sensor)
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003494 r = adev->powerplay.pp_funcs->read_sensor(adev->powerplay.pp_handle, idx, &values[0], &valuesize);
Samuel Pitoiset3cbc6142017-02-15 19:32:29 +01003495 else if (adev->pm.funcs && adev->pm.funcs->read_sensor)
3496 r = adev->pm.funcs->read_sensor(adev, idx, &values[0],
3497 &valuesize);
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003498 else
3499 return -EINVAL;
3500
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003501 if (size > valuesize)
3502 return -EINVAL;
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003503
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003504 outsize = 0;
3505 x = 0;
3506 if (!r) {
3507 while (size) {
3508 r = put_user(values[x++], (int32_t *)buf);
3509 buf += 4;
3510 size -= 4;
3511 outsize += 4;
3512 }
3513 }
3514
3515 return !r ? outsize : r;
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003516}
Tom St Denis1e051412016-06-27 09:57:18 -04003517
Tom St Denis273d7aa2016-10-11 14:48:55 -04003518static ssize_t amdgpu_debugfs_wave_read(struct file *f, char __user *buf,
3519 size_t size, loff_t *pos)
3520{
3521 struct amdgpu_device *adev = f->f_inode->i_private;
3522 int r, x;
3523 ssize_t result=0;
Tom St Denis472259f2016-10-14 09:49:09 -04003524 uint32_t offset, se, sh, cu, wave, simd, data[32];
Tom St Denis273d7aa2016-10-11 14:48:55 -04003525
3526 if (size & 3 || *pos & 3)
3527 return -EINVAL;
3528
3529 /* decode offset */
3530 offset = (*pos & 0x7F);
3531 se = ((*pos >> 7) & 0xFF);
3532 sh = ((*pos >> 15) & 0xFF);
3533 cu = ((*pos >> 23) & 0xFF);
3534 wave = ((*pos >> 31) & 0xFF);
3535 simd = ((*pos >> 37) & 0xFF);
Tom St Denis273d7aa2016-10-11 14:48:55 -04003536
3537 /* switch to the specific se/sh/cu */
3538 mutex_lock(&adev->grbm_idx_mutex);
3539 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
3540
3541 x = 0;
Tom St Denis472259f2016-10-14 09:49:09 -04003542 if (adev->gfx.funcs->read_wave_data)
3543 adev->gfx.funcs->read_wave_data(adev, simd, wave, data, &x);
Tom St Denis273d7aa2016-10-11 14:48:55 -04003544
3545 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
3546 mutex_unlock(&adev->grbm_idx_mutex);
3547
Tom St Denis5ecfb3b2016-10-13 12:15:03 -04003548 if (!x)
3549 return -EINVAL;
3550
Tom St Denis472259f2016-10-14 09:49:09 -04003551 while (size && (offset < x * 4)) {
Tom St Denis273d7aa2016-10-11 14:48:55 -04003552 uint32_t value;
3553
Tom St Denis472259f2016-10-14 09:49:09 -04003554 value = data[offset >> 2];
Tom St Denis273d7aa2016-10-11 14:48:55 -04003555 r = put_user(value, (uint32_t *)buf);
3556 if (r)
3557 return r;
3558
3559 result += 4;
3560 buf += 4;
Tom St Denis472259f2016-10-14 09:49:09 -04003561 offset += 4;
Tom St Denis273d7aa2016-10-11 14:48:55 -04003562 size -= 4;
3563 }
3564
3565 return result;
3566}
3567
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003568static ssize_t amdgpu_debugfs_gpr_read(struct file *f, char __user *buf,
3569 size_t size, loff_t *pos)
3570{
3571 struct amdgpu_device *adev = f->f_inode->i_private;
3572 int r;
3573 ssize_t result = 0;
3574 uint32_t offset, se, sh, cu, wave, simd, thread, bank, *data;
3575
3576 if (size & 3 || *pos & 3)
3577 return -EINVAL;
3578
3579 /* decode offset */
3580 offset = (*pos & 0xFFF); /* in dwords */
3581 se = ((*pos >> 12) & 0xFF);
3582 sh = ((*pos >> 20) & 0xFF);
3583 cu = ((*pos >> 28) & 0xFF);
3584 wave = ((*pos >> 36) & 0xFF);
3585 simd = ((*pos >> 44) & 0xFF);
3586 thread = ((*pos >> 52) & 0xFF);
3587 bank = ((*pos >> 60) & 1);
3588
3589 data = kmalloc_array(1024, sizeof(*data), GFP_KERNEL);
3590 if (!data)
3591 return -ENOMEM;
3592
3593 /* switch to the specific se/sh/cu */
3594 mutex_lock(&adev->grbm_idx_mutex);
3595 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
3596
3597 if (bank == 0) {
3598 if (adev->gfx.funcs->read_wave_vgprs)
3599 adev->gfx.funcs->read_wave_vgprs(adev, simd, wave, thread, offset, size>>2, data);
3600 } else {
3601 if (adev->gfx.funcs->read_wave_sgprs)
3602 adev->gfx.funcs->read_wave_sgprs(adev, simd, wave, offset, size>>2, data);
3603 }
3604
3605 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
3606 mutex_unlock(&adev->grbm_idx_mutex);
3607
3608 while (size) {
3609 uint32_t value;
3610
3611 value = data[offset++];
3612 r = put_user(value, (uint32_t *)buf);
3613 if (r) {
3614 result = r;
3615 goto err;
3616 }
3617
3618 result += 4;
3619 buf += 4;
3620 size -= 4;
3621 }
3622
3623err:
3624 kfree(data);
3625 return result;
3626}
3627
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003628static const struct file_operations amdgpu_debugfs_regs_fops = {
3629 .owner = THIS_MODULE,
3630 .read = amdgpu_debugfs_regs_read,
3631 .write = amdgpu_debugfs_regs_write,
3632 .llseek = default_llseek
3633};
Tom St Denisadcec282016-04-15 13:08:44 -04003634static const struct file_operations amdgpu_debugfs_regs_didt_fops = {
3635 .owner = THIS_MODULE,
3636 .read = amdgpu_debugfs_regs_didt_read,
3637 .write = amdgpu_debugfs_regs_didt_write,
3638 .llseek = default_llseek
3639};
3640static const struct file_operations amdgpu_debugfs_regs_pcie_fops = {
3641 .owner = THIS_MODULE,
3642 .read = amdgpu_debugfs_regs_pcie_read,
3643 .write = amdgpu_debugfs_regs_pcie_write,
3644 .llseek = default_llseek
3645};
3646static const struct file_operations amdgpu_debugfs_regs_smc_fops = {
3647 .owner = THIS_MODULE,
3648 .read = amdgpu_debugfs_regs_smc_read,
3649 .write = amdgpu_debugfs_regs_smc_write,
3650 .llseek = default_llseek
3651};
3652
Tom St Denis1e051412016-06-27 09:57:18 -04003653static const struct file_operations amdgpu_debugfs_gca_config_fops = {
3654 .owner = THIS_MODULE,
3655 .read = amdgpu_debugfs_gca_config_read,
3656 .llseek = default_llseek
3657};
3658
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003659static const struct file_operations amdgpu_debugfs_sensors_fops = {
3660 .owner = THIS_MODULE,
3661 .read = amdgpu_debugfs_sensor_read,
3662 .llseek = default_llseek
3663};
3664
Tom St Denis273d7aa2016-10-11 14:48:55 -04003665static const struct file_operations amdgpu_debugfs_wave_fops = {
3666 .owner = THIS_MODULE,
3667 .read = amdgpu_debugfs_wave_read,
3668 .llseek = default_llseek
3669};
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003670static const struct file_operations amdgpu_debugfs_gpr_fops = {
3671 .owner = THIS_MODULE,
3672 .read = amdgpu_debugfs_gpr_read,
3673 .llseek = default_llseek
3674};
Tom St Denis273d7aa2016-10-11 14:48:55 -04003675
Tom St Denisadcec282016-04-15 13:08:44 -04003676static const struct file_operations *debugfs_regs[] = {
3677 &amdgpu_debugfs_regs_fops,
3678 &amdgpu_debugfs_regs_didt_fops,
3679 &amdgpu_debugfs_regs_pcie_fops,
3680 &amdgpu_debugfs_regs_smc_fops,
Tom St Denis1e051412016-06-27 09:57:18 -04003681 &amdgpu_debugfs_gca_config_fops,
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003682 &amdgpu_debugfs_sensors_fops,
Tom St Denis273d7aa2016-10-11 14:48:55 -04003683 &amdgpu_debugfs_wave_fops,
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003684 &amdgpu_debugfs_gpr_fops,
Tom St Denisadcec282016-04-15 13:08:44 -04003685};
3686
3687static const char *debugfs_regs_names[] = {
3688 "amdgpu_regs",
3689 "amdgpu_regs_didt",
3690 "amdgpu_regs_pcie",
3691 "amdgpu_regs_smc",
Tom St Denis1e051412016-06-27 09:57:18 -04003692 "amdgpu_gca_config",
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003693 "amdgpu_sensors",
Tom St Denis273d7aa2016-10-11 14:48:55 -04003694 "amdgpu_wave",
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003695 "amdgpu_gpr",
Tom St Denisadcec282016-04-15 13:08:44 -04003696};
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003697
3698static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
3699{
3700 struct drm_minor *minor = adev->ddev->primary;
3701 struct dentry *ent, *root = minor->debugfs_root;
Tom St Denisadcec282016-04-15 13:08:44 -04003702 unsigned i, j;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003703
Tom St Denisadcec282016-04-15 13:08:44 -04003704 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3705 ent = debugfs_create_file(debugfs_regs_names[i],
3706 S_IFREG | S_IRUGO, root,
3707 adev, debugfs_regs[i]);
3708 if (IS_ERR(ent)) {
3709 for (j = 0; j < i; j++) {
3710 debugfs_remove(adev->debugfs_regs[i]);
3711 adev->debugfs_regs[i] = NULL;
3712 }
3713 return PTR_ERR(ent);
3714 }
3715
3716 if (!i)
3717 i_size_write(ent->d_inode, adev->rmmio_size);
3718 adev->debugfs_regs[i] = ent;
3719 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003720
3721 return 0;
3722}
3723
3724static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev)
3725{
Tom St Denisadcec282016-04-15 13:08:44 -04003726 unsigned i;
3727
3728 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3729 if (adev->debugfs_regs[i]) {
3730 debugfs_remove(adev->debugfs_regs[i]);
3731 adev->debugfs_regs[i] = NULL;
3732 }
3733 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003734}
3735
Huang Rui4f0955f2017-05-10 23:04:06 +08003736static int amdgpu_debugfs_test_ib(struct seq_file *m, void *data)
3737{
3738 struct drm_info_node *node = (struct drm_info_node *) m->private;
3739 struct drm_device *dev = node->minor->dev;
3740 struct amdgpu_device *adev = dev->dev_private;
3741 int r = 0, i;
3742
3743 /* hold on the scheduler */
3744 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
3745 struct amdgpu_ring *ring = adev->rings[i];
3746
3747 if (!ring || !ring->sched.thread)
3748 continue;
3749 kthread_park(ring->sched.thread);
3750 }
3751
3752 seq_printf(m, "run ib test:\n");
3753 r = amdgpu_ib_ring_tests(adev);
3754 if (r)
3755 seq_printf(m, "ib ring tests failed (%d).\n", r);
3756 else
3757 seq_printf(m, "ib ring tests passed.\n");
3758
3759 /* go on the scheduler */
3760 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
3761 struct amdgpu_ring *ring = adev->rings[i];
3762
3763 if (!ring || !ring->sched.thread)
3764 continue;
3765 kthread_unpark(ring->sched.thread);
3766 }
3767
3768 return 0;
3769}
3770
3771static const struct drm_info_list amdgpu_debugfs_test_ib_ring_list[] = {
3772 {"amdgpu_test_ib", &amdgpu_debugfs_test_ib}
3773};
3774
3775static int amdgpu_debugfs_test_ib_ring_init(struct amdgpu_device *adev)
3776{
3777 return amdgpu_debugfs_add_files(adev,
3778 amdgpu_debugfs_test_ib_ring_list, 1);
3779}
3780
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003781int amdgpu_debugfs_init(struct drm_minor *minor)
3782{
3783 return 0;
3784}
Kent Russelldb95e212017-08-22 12:31:43 -04003785
3786static int amdgpu_debugfs_get_vbios_dump(struct seq_file *m, void *data)
3787{
3788 struct drm_info_node *node = (struct drm_info_node *) m->private;
3789 struct drm_device *dev = node->minor->dev;
3790 struct amdgpu_device *adev = dev->dev_private;
3791
3792 seq_write(m, adev->bios, adev->bios_size);
3793 return 0;
3794}
3795
Kent Russelldb95e212017-08-22 12:31:43 -04003796static const struct drm_info_list amdgpu_vbios_dump_list[] = {
3797 {"amdgpu_vbios",
3798 amdgpu_debugfs_get_vbios_dump,
3799 0, NULL},
3800};
3801
Kent Russelldb95e212017-08-22 12:31:43 -04003802static int amdgpu_debugfs_vbios_dump_init(struct amdgpu_device *adev)
3803{
3804 return amdgpu_debugfs_add_files(adev,
3805 amdgpu_vbios_dump_list, 1);
3806}
Alexander Kuleshov7cebc722015-06-27 13:16:05 +06003807#else
Arnd Bergmann27bad5b2017-06-21 23:51:02 +02003808static int amdgpu_debugfs_test_ib_ring_init(struct amdgpu_device *adev)
Huang Rui4f0955f2017-05-10 23:04:06 +08003809{
3810 return 0;
3811}
Alexander Kuleshov7cebc722015-06-27 13:16:05 +06003812static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
3813{
3814 return 0;
3815}
Kent Russelldb95e212017-08-22 12:31:43 -04003816static int amdgpu_debugfs_vbios_dump_init(struct amdgpu_device *adev)
3817{
3818 return 0;
3819}
Alexander Kuleshov7cebc722015-06-27 13:16:05 +06003820static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev) { }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003821#endif