blob: 7963c54e5d032806c952c7e4311ab8f0cb034d20 [file] [log] [blame]
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001/*
2 * Copyright 2008 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 * Copyright 2009 Jerome Glisse.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors: Dave Airlie
25 * Alex Deucher
26 * Jerome Glisse
27 */
Chunming Zhou0875dc92016-06-12 15:41:58 +080028#include <linux/kthread.h>
Alex Deucherd38ceaf2015-04-20 16:55:21 -040029#include <linux/console.h>
30#include <linux/slab.h>
31#include <linux/debugfs.h>
32#include <drm/drmP.h>
33#include <drm/drm_crtc_helper.h>
34#include <drm/amdgpu_drm.h>
35#include <linux/vgaarb.h>
36#include <linux/vga_switcheroo.h>
37#include <linux/efi.h>
38#include "amdgpu.h"
Tom St Denisf4b373f2016-05-31 08:02:27 -040039#include "amdgpu_trace.h"
Alex Deucherd38ceaf2015-04-20 16:55:21 -040040#include "amdgpu_i2c.h"
41#include "atom.h"
42#include "amdgpu_atombios.h"
Alex Deuchera5bde2f2016-09-23 16:23:41 -040043#include "amdgpu_atomfirmware.h"
Alex Deucherd0dd7f02015-11-11 19:45:06 -050044#include "amd_pcie.h"
Ken Wang33f34802016-01-21 17:29:41 +080045#ifdef CONFIG_DRM_AMDGPU_SI
46#include "si.h"
47#endif
Alex Deuchera2e73f52015-04-20 17:09:27 -040048#ifdef CONFIG_DRM_AMDGPU_CIK
49#include "cik.h"
50#endif
Alex Deucheraaa36a92015-04-20 17:31:14 -040051#include "vi.h"
Ken Wang460826e2017-03-06 14:53:16 -050052#include "soc15.h"
Alex Deucherd38ceaf2015-04-20 16:55:21 -040053#include "bif/bif_4_1_d.h"
Emily Deng9accf2f2016-08-10 16:01:25 +080054#include <linux/pci.h>
Monk Liubec86372016-09-14 19:38:08 +080055#include <linux/firmware.h>
Gavin Wan89041942017-06-23 13:55:15 -040056#include "amdgpu_vf_error.h"
Alex Deucherd38ceaf2015-04-20 16:55:21 -040057
Alex Deuchere2a75f82017-04-27 16:58:01 -040058MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin");
Alex Deucher2d2e5e72017-05-09 12:27:35 -040059MODULE_FIRMWARE("amdgpu/raven_gpu_info.bin");
Alex Deuchere2a75f82017-04-27 16:58:01 -040060
Shirish S2dc80b02017-05-25 10:05:25 +053061#define AMDGPU_RESUME_MS 2000
62
Alex Deucherd38ceaf2015-04-20 16:55:21 -040063static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev);
64static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev);
Huang Rui4f0955f2017-05-10 23:04:06 +080065static int amdgpu_debugfs_test_ib_ring_init(struct amdgpu_device *adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040066
67static const char *amdgpu_asic_name[] = {
Ken Wangda69c1612016-01-21 19:08:55 +080068 "TAHITI",
69 "PITCAIRN",
70 "VERDE",
71 "OLAND",
72 "HAINAN",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040073 "BONAIRE",
74 "KAVERI",
75 "KABINI",
76 "HAWAII",
77 "MULLINS",
78 "TOPAZ",
79 "TONGA",
David Zhang48299f92015-07-08 01:05:16 +080080 "FIJI",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040081 "CARRIZO",
Samuel Li139f4912015-10-08 14:50:27 -040082 "STONEY",
Flora Cui2cc0c0b2016-03-14 18:33:29 -040083 "POLARIS10",
84 "POLARIS11",
Junwei Zhangc4642a42016-12-14 15:32:28 -050085 "POLARIS12",
Ken Wangd4196f02016-03-09 09:28:32 +080086 "VEGA10",
Chunming Zhou2ca8a5d2016-12-07 17:31:19 +080087 "RAVEN",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040088 "LAST",
89};
90
91bool amdgpu_device_is_px(struct drm_device *dev)
92{
93 struct amdgpu_device *adev = dev->dev_private;
94
Jammy Zhou2f7d10b2015-07-22 11:29:01 +080095 if (adev->flags & AMD_IS_PX)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040096 return true;
97 return false;
98}
99
100/*
101 * MMIO register access helper functions.
102 */
103uint32_t amdgpu_mm_rreg(struct amdgpu_device *adev, uint32_t reg,
Monk Liu15d72fd2017-01-25 15:07:40 +0800104 uint32_t acc_flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400105{
Tom St Denisf4b373f2016-05-31 08:02:27 -0400106 uint32_t ret;
107
Monk Liu15d72fd2017-01-25 15:07:40 +0800108 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev)) {
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800109 BUG_ON(in_interrupt());
110 return amdgpu_virt_kiq_rreg(adev, reg);
111 }
112
Monk Liu15d72fd2017-01-25 15:07:40 +0800113 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
Tom St Denisf4b373f2016-05-31 08:02:27 -0400114 ret = readl(((void __iomem *)adev->rmmio) + (reg * 4));
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400115 else {
116 unsigned long flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400117
118 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
119 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
120 ret = readl(((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
121 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400122 }
Tom St Denisf4b373f2016-05-31 08:02:27 -0400123 trace_amdgpu_mm_rreg(adev->pdev->device, reg, ret);
124 return ret;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400125}
126
127void amdgpu_mm_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v,
Monk Liu15d72fd2017-01-25 15:07:40 +0800128 uint32_t acc_flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400129{
Tom St Denisf4b373f2016-05-31 08:02:27 -0400130 trace_amdgpu_mm_wreg(adev->pdev->device, reg, v);
Monk Liu4e99a442016-03-31 13:26:59 +0800131
Monk Liu15d72fd2017-01-25 15:07:40 +0800132 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev)) {
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800133 BUG_ON(in_interrupt());
134 return amdgpu_virt_kiq_wreg(adev, reg, v);
135 }
136
Monk Liu15d72fd2017-01-25 15:07:40 +0800137 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400138 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
139 else {
140 unsigned long flags;
141
142 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
143 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
144 writel(v, ((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
145 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
146 }
147}
148
149u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 reg)
150{
151 if ((reg * 4) < adev->rio_mem_size)
152 return ioread32(adev->rio_mem + (reg * 4));
153 else {
154 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
155 return ioread32(adev->rio_mem + (mmMM_DATA * 4));
156 }
157}
158
159void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
160{
161
162 if ((reg * 4) < adev->rio_mem_size)
163 iowrite32(v, adev->rio_mem + (reg * 4));
164 else {
165 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
166 iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
167 }
168}
169
170/**
171 * amdgpu_mm_rdoorbell - read a doorbell dword
172 *
173 * @adev: amdgpu_device pointer
174 * @index: doorbell index
175 *
176 * Returns the value in the doorbell aperture at the
177 * requested doorbell index (CIK).
178 */
179u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
180{
181 if (index < adev->doorbell.num_doorbells) {
182 return readl(adev->doorbell.ptr + index);
183 } else {
184 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
185 return 0;
186 }
187}
188
189/**
190 * amdgpu_mm_wdoorbell - write a doorbell dword
191 *
192 * @adev: amdgpu_device pointer
193 * @index: doorbell index
194 * @v: value to write
195 *
196 * Writes @v to the doorbell aperture at the
197 * requested doorbell index (CIK).
198 */
199void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
200{
201 if (index < adev->doorbell.num_doorbells) {
202 writel(v, adev->doorbell.ptr + index);
203 } else {
204 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
205 }
206}
207
208/**
Ken Wang832be402016-03-18 15:23:08 +0800209 * amdgpu_mm_rdoorbell64 - read a doorbell Qword
210 *
211 * @adev: amdgpu_device pointer
212 * @index: doorbell index
213 *
214 * Returns the value in the doorbell aperture at the
215 * requested doorbell index (VEGA10+).
216 */
217u64 amdgpu_mm_rdoorbell64(struct amdgpu_device *adev, u32 index)
218{
219 if (index < adev->doorbell.num_doorbells) {
220 return atomic64_read((atomic64_t *)(adev->doorbell.ptr + index));
221 } else {
222 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
223 return 0;
224 }
225}
226
227/**
228 * amdgpu_mm_wdoorbell64 - write a doorbell Qword
229 *
230 * @adev: amdgpu_device pointer
231 * @index: doorbell index
232 * @v: value to write
233 *
234 * Writes @v to the doorbell aperture at the
235 * requested doorbell index (VEGA10+).
236 */
237void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v)
238{
239 if (index < adev->doorbell.num_doorbells) {
240 atomic64_set((atomic64_t *)(adev->doorbell.ptr + index), v);
241 } else {
242 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
243 }
244}
245
246/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400247 * amdgpu_invalid_rreg - dummy reg read function
248 *
249 * @adev: amdgpu device pointer
250 * @reg: offset of register
251 *
252 * Dummy register read function. Used for register blocks
253 * that certain asics don't have (all asics).
254 * Returns the value in the register.
255 */
256static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
257{
258 DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
259 BUG();
260 return 0;
261}
262
263/**
264 * amdgpu_invalid_wreg - dummy reg write function
265 *
266 * @adev: amdgpu device pointer
267 * @reg: offset of register
268 * @v: value to write to the register
269 *
270 * Dummy register read function. Used for register blocks
271 * that certain asics don't have (all asics).
272 */
273static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
274{
275 DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
276 reg, v);
277 BUG();
278}
279
280/**
281 * amdgpu_block_invalid_rreg - dummy reg read function
282 *
283 * @adev: amdgpu device pointer
284 * @block: offset of instance
285 * @reg: offset of register
286 *
287 * Dummy register read function. Used for register blocks
288 * that certain asics don't have (all asics).
289 * Returns the value in the register.
290 */
291static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
292 uint32_t block, uint32_t reg)
293{
294 DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
295 reg, block);
296 BUG();
297 return 0;
298}
299
300/**
301 * amdgpu_block_invalid_wreg - dummy reg write function
302 *
303 * @adev: amdgpu device pointer
304 * @block: offset of instance
305 * @reg: offset of register
306 * @v: value to write to the register
307 *
308 * Dummy register read function. Used for register blocks
309 * that certain asics don't have (all asics).
310 */
311static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
312 uint32_t block,
313 uint32_t reg, uint32_t v)
314{
315 DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
316 reg, block, v);
317 BUG();
318}
319
320static int amdgpu_vram_scratch_init(struct amdgpu_device *adev)
321{
322 int r;
323
324 if (adev->vram_scratch.robj == NULL) {
325 r = amdgpu_bo_create(adev, AMDGPU_GPU_PAGE_SIZE,
Alex Deucher857d9132015-08-27 00:14:16 -0400326 PAGE_SIZE, true, AMDGPU_GEM_DOMAIN_VRAM,
Christian König03f48dd2016-08-15 17:00:22 +0200327 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
328 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS,
Christian König72d76682015-09-03 17:34:59 +0200329 NULL, NULL, &adev->vram_scratch.robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400330 if (r) {
331 return r;
332 }
333 }
334
335 r = amdgpu_bo_reserve(adev->vram_scratch.robj, false);
336 if (unlikely(r != 0))
337 return r;
338 r = amdgpu_bo_pin(adev->vram_scratch.robj,
339 AMDGPU_GEM_DOMAIN_VRAM, &adev->vram_scratch.gpu_addr);
340 if (r) {
341 amdgpu_bo_unreserve(adev->vram_scratch.robj);
342 return r;
343 }
344 r = amdgpu_bo_kmap(adev->vram_scratch.robj,
345 (void **)&adev->vram_scratch.ptr);
346 if (r)
347 amdgpu_bo_unpin(adev->vram_scratch.robj);
348 amdgpu_bo_unreserve(adev->vram_scratch.robj);
349
350 return r;
351}
352
353static void amdgpu_vram_scratch_fini(struct amdgpu_device *adev)
354{
355 int r;
356
357 if (adev->vram_scratch.robj == NULL) {
358 return;
359 }
Alex Xie8ab25b42017-04-24 13:30:43 -0400360 r = amdgpu_bo_reserve(adev->vram_scratch.robj, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400361 if (likely(r == 0)) {
362 amdgpu_bo_kunmap(adev->vram_scratch.robj);
363 amdgpu_bo_unpin(adev->vram_scratch.robj);
364 amdgpu_bo_unreserve(adev->vram_scratch.robj);
365 }
366 amdgpu_bo_unref(&adev->vram_scratch.robj);
367}
368
369/**
370 * amdgpu_program_register_sequence - program an array of registers.
371 *
372 * @adev: amdgpu_device pointer
373 * @registers: pointer to the register array
374 * @array_size: size of the register array
375 *
376 * Programs an array or registers with and and or masks.
377 * This is a helper for setting golden registers.
378 */
379void amdgpu_program_register_sequence(struct amdgpu_device *adev,
380 const u32 *registers,
381 const u32 array_size)
382{
383 u32 tmp, reg, and_mask, or_mask;
384 int i;
385
386 if (array_size % 3)
387 return;
388
389 for (i = 0; i < array_size; i +=3) {
390 reg = registers[i + 0];
391 and_mask = registers[i + 1];
392 or_mask = registers[i + 2];
393
394 if (and_mask == 0xffffffff) {
395 tmp = or_mask;
396 } else {
397 tmp = RREG32(reg);
398 tmp &= ~and_mask;
399 tmp |= or_mask;
400 }
401 WREG32(reg, tmp);
402 }
403}
404
405void amdgpu_pci_config_reset(struct amdgpu_device *adev)
406{
407 pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
408}
409
410/*
411 * GPU doorbell aperture helpers function.
412 */
413/**
414 * amdgpu_doorbell_init - Init doorbell driver information.
415 *
416 * @adev: amdgpu_device pointer
417 *
418 * Init doorbell driver information (CIK)
419 * Returns 0 on success, error on failure.
420 */
421static int amdgpu_doorbell_init(struct amdgpu_device *adev)
422{
423 /* doorbell bar mapping */
424 adev->doorbell.base = pci_resource_start(adev->pdev, 2);
425 adev->doorbell.size = pci_resource_len(adev->pdev, 2);
426
Christian Königedf600d2016-05-03 15:54:54 +0200427 adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400428 AMDGPU_DOORBELL_MAX_ASSIGNMENT+1);
429 if (adev->doorbell.num_doorbells == 0)
430 return -EINVAL;
431
Christian König8972e5d2017-03-06 13:34:57 +0100432 adev->doorbell.ptr = ioremap(adev->doorbell.base,
433 adev->doorbell.num_doorbells *
434 sizeof(u32));
435 if (adev->doorbell.ptr == NULL)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400436 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400437
438 return 0;
439}
440
441/**
442 * amdgpu_doorbell_fini - Tear down doorbell driver information.
443 *
444 * @adev: amdgpu_device pointer
445 *
446 * Tear down doorbell driver information (CIK)
447 */
448static void amdgpu_doorbell_fini(struct amdgpu_device *adev)
449{
450 iounmap(adev->doorbell.ptr);
451 adev->doorbell.ptr = NULL;
452}
453
454/**
455 * amdgpu_doorbell_get_kfd_info - Report doorbell configuration required to
456 * setup amdkfd
457 *
458 * @adev: amdgpu_device pointer
459 * @aperture_base: output returning doorbell aperture base physical address
460 * @aperture_size: output returning doorbell aperture size in bytes
461 * @start_offset: output returning # of doorbell bytes reserved for amdgpu.
462 *
463 * amdgpu and amdkfd share the doorbell aperture. amdgpu sets it up,
464 * takes doorbells required for its own rings and reports the setup to amdkfd.
465 * amdgpu reserved doorbells are at the start of the doorbell aperture.
466 */
467void amdgpu_doorbell_get_kfd_info(struct amdgpu_device *adev,
468 phys_addr_t *aperture_base,
469 size_t *aperture_size,
470 size_t *start_offset)
471{
472 /*
473 * The first num_doorbells are used by amdgpu.
474 * amdkfd takes whatever's left in the aperture.
475 */
476 if (adev->doorbell.size > adev->doorbell.num_doorbells * sizeof(u32)) {
477 *aperture_base = adev->doorbell.base;
478 *aperture_size = adev->doorbell.size;
479 *start_offset = adev->doorbell.num_doorbells * sizeof(u32);
480 } else {
481 *aperture_base = 0;
482 *aperture_size = 0;
483 *start_offset = 0;
484 }
485}
486
487/*
488 * amdgpu_wb_*()
Alex Xie455a7bc2017-05-08 21:36:03 -0400489 * Writeback is the method by which the GPU updates special pages in memory
Alex Xieea81a172017-05-08 13:41:11 -0400490 * with the status of certain GPU events (fences, ring pointers,etc.).
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400491 */
492
493/**
494 * amdgpu_wb_fini - Disable Writeback and free memory
495 *
496 * @adev: amdgpu_device pointer
497 *
498 * Disables Writeback and frees the Writeback memory (all asics).
499 * Used at driver shutdown.
500 */
501static void amdgpu_wb_fini(struct amdgpu_device *adev)
502{
503 if (adev->wb.wb_obj) {
Alex Deuchera76ed482016-10-21 15:30:36 -0400504 amdgpu_bo_free_kernel(&adev->wb.wb_obj,
505 &adev->wb.gpu_addr,
506 (void **)&adev->wb.wb);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400507 adev->wb.wb_obj = NULL;
508 }
509}
510
511/**
512 * amdgpu_wb_init- Init Writeback driver info and allocate memory
513 *
514 * @adev: amdgpu_device pointer
515 *
Alex Xie455a7bc2017-05-08 21:36:03 -0400516 * Initializes writeback and allocates writeback memory (all asics).
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400517 * Used at driver startup.
518 * Returns 0 on success or an -error on failure.
519 */
520static int amdgpu_wb_init(struct amdgpu_device *adev)
521{
522 int r;
523
524 if (adev->wb.wb_obj == NULL) {
Huang Rui60a970a62017-03-15 10:13:32 +0800525 r = amdgpu_bo_create_kernel(adev, AMDGPU_MAX_WB * sizeof(uint32_t),
Alex Deuchera76ed482016-10-21 15:30:36 -0400526 PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
527 &adev->wb.wb_obj, &adev->wb.gpu_addr,
528 (void **)&adev->wb.wb);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400529 if (r) {
530 dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
531 return r;
532 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400533
534 adev->wb.num_wb = AMDGPU_MAX_WB;
535 memset(&adev->wb.used, 0, sizeof(adev->wb.used));
536
537 /* clear wb memory */
Huang Rui60a970a62017-03-15 10:13:32 +0800538 memset((char *)adev->wb.wb, 0, AMDGPU_MAX_WB * sizeof(uint32_t));
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400539 }
540
541 return 0;
542}
543
544/**
545 * amdgpu_wb_get - Allocate a wb entry
546 *
547 * @adev: amdgpu_device pointer
548 * @wb: wb index
549 *
550 * Allocate a wb slot for use by the driver (all asics).
551 * Returns 0 on success or -EINVAL on failure.
552 */
553int amdgpu_wb_get(struct amdgpu_device *adev, u32 *wb)
554{
555 unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
556 if (offset < adev->wb.num_wb) {
557 __set_bit(offset, adev->wb.used);
558 *wb = offset;
559 return 0;
560 } else {
561 return -EINVAL;
562 }
563}
564
565/**
Ken Wang70142852016-03-18 15:08:49 +0800566 * amdgpu_wb_get_64bit - Allocate a wb entry
567 *
568 * @adev: amdgpu_device pointer
569 * @wb: wb index
570 *
571 * Allocate a wb slot for use by the driver (all asics).
572 * Returns 0 on success or -EINVAL on failure.
573 */
574int amdgpu_wb_get_64bit(struct amdgpu_device *adev, u32 *wb)
575{
576 unsigned long offset = bitmap_find_next_zero_area_off(adev->wb.used,
577 adev->wb.num_wb, 0, 2, 7, 0);
578 if ((offset + 1) < adev->wb.num_wb) {
579 __set_bit(offset, adev->wb.used);
580 __set_bit(offset + 1, adev->wb.used);
581 *wb = offset;
582 return 0;
583 } else {
584 return -EINVAL;
585 }
586}
587
588/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400589 * amdgpu_wb_free - Free a wb entry
590 *
591 * @adev: amdgpu_device pointer
592 * @wb: wb index
593 *
594 * Free a wb slot allocated for use by the driver (all asics)
595 */
596void amdgpu_wb_free(struct amdgpu_device *adev, u32 wb)
597{
598 if (wb < adev->wb.num_wb)
599 __clear_bit(wb, adev->wb.used);
600}
601
602/**
Ken Wang70142852016-03-18 15:08:49 +0800603 * amdgpu_wb_free_64bit - Free a wb entry
604 *
605 * @adev: amdgpu_device pointer
606 * @wb: wb index
607 *
608 * Free a wb slot allocated for use by the driver (all asics)
609 */
610void amdgpu_wb_free_64bit(struct amdgpu_device *adev, u32 wb)
611{
612 if ((wb + 1) < adev->wb.num_wb) {
613 __clear_bit(wb, adev->wb.used);
614 __clear_bit(wb + 1, adev->wb.used);
615 }
616}
617
618/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400619 * amdgpu_vram_location - try to find VRAM location
620 * @adev: amdgpu device structure holding all necessary informations
621 * @mc: memory controller structure holding memory informations
622 * @base: base address at which to put VRAM
623 *
Alex Xie455a7bc2017-05-08 21:36:03 -0400624 * Function will try to place VRAM at base address provided
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400625 * as parameter (which is so far either PCI aperture address or
626 * for IGP TOM base address).
627 *
628 * If there is not enough space to fit the unvisible VRAM in the 32bits
629 * address space then we limit the VRAM size to the aperture.
630 *
631 * Note: We don't explicitly enforce VRAM start to be aligned on VRAM size,
632 * this shouldn't be a problem as we are using the PCI aperture as a reference.
633 * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but
634 * not IGP.
635 *
636 * Note: we use mc_vram_size as on some board we need to program the mc to
637 * cover the whole aperture even if VRAM size is inferior to aperture size
638 * Novell bug 204882 + along with lots of ubuntu ones
639 *
640 * Note: when limiting vram it's safe to overwritte real_vram_size because
641 * we are not in case where real_vram_size is inferior to mc_vram_size (ie
642 * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu
643 * ones)
644 *
645 * Note: IGP TOM addr should be the same as the aperture addr, we don't
Alex Xie455a7bc2017-05-08 21:36:03 -0400646 * explicitly check for that though.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400647 *
648 * FIXME: when reducing VRAM size align new size on power of 2.
649 */
650void amdgpu_vram_location(struct amdgpu_device *adev, struct amdgpu_mc *mc, u64 base)
651{
652 uint64_t limit = (uint64_t)amdgpu_vram_limit << 20;
653
654 mc->vram_start = base;
655 if (mc->mc_vram_size > (adev->mc.mc_mask - base + 1)) {
656 dev_warn(adev->dev, "limiting VRAM to PCI aperture size\n");
657 mc->real_vram_size = mc->aper_size;
658 mc->mc_vram_size = mc->aper_size;
659 }
660 mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
661 if (limit && limit < mc->real_vram_size)
662 mc->real_vram_size = limit;
663 dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
664 mc->mc_vram_size >> 20, mc->vram_start,
665 mc->vram_end, mc->real_vram_size >> 20);
666}
667
668/**
669 * amdgpu_gtt_location - try to find GTT location
670 * @adev: amdgpu device structure holding all necessary informations
671 * @mc: memory controller structure holding memory informations
672 *
673 * Function will place try to place GTT before or after VRAM.
674 *
675 * If GTT size is bigger than space left then we ajust GTT size.
676 * Thus function will never fails.
677 *
678 * FIXME: when reducing GTT size align new size on power of 2.
679 */
680void amdgpu_gtt_location(struct amdgpu_device *adev, struct amdgpu_mc *mc)
681{
682 u64 size_af, size_bf;
683
684 size_af = ((adev->mc.mc_mask - mc->vram_end) + mc->gtt_base_align) & ~mc->gtt_base_align;
685 size_bf = mc->vram_start & ~mc->gtt_base_align;
686 if (size_bf > size_af) {
687 if (mc->gtt_size > size_bf) {
688 dev_warn(adev->dev, "limiting GTT\n");
689 mc->gtt_size = size_bf;
690 }
Alex Deucher9dc5a912016-11-17 15:40:22 -0500691 mc->gtt_start = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400692 } else {
693 if (mc->gtt_size > size_af) {
694 dev_warn(adev->dev, "limiting GTT\n");
695 mc->gtt_size = size_af;
696 }
697 mc->gtt_start = (mc->vram_end + 1 + mc->gtt_base_align) & ~mc->gtt_base_align;
698 }
699 mc->gtt_end = mc->gtt_start + mc->gtt_size - 1;
700 dev_info(adev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
701 mc->gtt_size >> 20, mc->gtt_start, mc->gtt_end);
702}
703
704/*
705 * GPU helpers function.
706 */
707/**
Jim Quc836fec2017-02-10 15:59:59 +0800708 * amdgpu_need_post - check if the hw need post or not
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400709 *
710 * @adev: amdgpu_device pointer
711 *
Jim Quc836fec2017-02-10 15:59:59 +0800712 * Check if the asic has been initialized (all asics) at driver startup
713 * or post is needed if hw reset is performed.
714 * Returns true if need or false if not.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400715 */
Jim Quc836fec2017-02-10 15:59:59 +0800716bool amdgpu_need_post(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400717{
718 uint32_t reg;
719
Jim Quc836fec2017-02-10 15:59:59 +0800720 if (adev->has_hw_reset) {
721 adev->has_hw_reset = false;
722 return true;
723 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400724 /* then check MEM_SIZE, in case the crtcs are off */
Alex Deucherbbf282d2017-03-03 17:26:10 -0500725 reg = amdgpu_asic_get_config_memsize(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400726
Alex Deucherf2713e82017-03-28 12:19:31 -0400727 if ((reg != 0) && (reg != 0xffffffff))
Jim Quc836fec2017-02-10 15:59:59 +0800728 return false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400729
Jim Quc836fec2017-02-10 15:59:59 +0800730 return true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400731
732}
733
Monk Liubec86372016-09-14 19:38:08 +0800734static bool amdgpu_vpost_needed(struct amdgpu_device *adev)
735{
736 if (amdgpu_sriov_vf(adev))
737 return false;
738
739 if (amdgpu_passthrough(adev)) {
Monk Liu1da2c322016-11-11 11:24:29 +0800740 /* for FIJI: In whole GPU pass-through virtualization case, after VM reboot
741 * some old smc fw still need driver do vPost otherwise gpu hang, while
742 * those smc fw version above 22.15 doesn't have this flaw, so we force
743 * vpost executed for smc version below 22.15
Monk Liubec86372016-09-14 19:38:08 +0800744 */
745 if (adev->asic_type == CHIP_FIJI) {
746 int err;
747 uint32_t fw_ver;
748 err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
749 /* force vPost if error occured */
750 if (err)
751 return true;
752
753 fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
Monk Liu1da2c322016-11-11 11:24:29 +0800754 if (fw_ver < 0x00160e00)
755 return true;
Monk Liubec86372016-09-14 19:38:08 +0800756 }
Monk Liubec86372016-09-14 19:38:08 +0800757 }
Jim Quc836fec2017-02-10 15:59:59 +0800758 return amdgpu_need_post(adev);
Monk Liubec86372016-09-14 19:38:08 +0800759}
760
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400761/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400762 * amdgpu_dummy_page_init - init dummy page used by the driver
763 *
764 * @adev: amdgpu_device pointer
765 *
766 * Allocate the dummy page used by the driver (all asics).
767 * This dummy page is used by the driver as a filler for gart entries
768 * when pages are taken out of the GART
769 * Returns 0 on sucess, -ENOMEM on failure.
770 */
771int amdgpu_dummy_page_init(struct amdgpu_device *adev)
772{
773 if (adev->dummy_page.page)
774 return 0;
775 adev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
776 if (adev->dummy_page.page == NULL)
777 return -ENOMEM;
778 adev->dummy_page.addr = pci_map_page(adev->pdev, adev->dummy_page.page,
779 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
780 if (pci_dma_mapping_error(adev->pdev, adev->dummy_page.addr)) {
781 dev_err(&adev->pdev->dev, "Failed to DMA MAP the dummy page\n");
782 __free_page(adev->dummy_page.page);
783 adev->dummy_page.page = NULL;
784 return -ENOMEM;
785 }
786 return 0;
787}
788
789/**
790 * amdgpu_dummy_page_fini - free dummy page used by the driver
791 *
792 * @adev: amdgpu_device pointer
793 *
794 * Frees the dummy page used by the driver (all asics).
795 */
796void amdgpu_dummy_page_fini(struct amdgpu_device *adev)
797{
798 if (adev->dummy_page.page == NULL)
799 return;
800 pci_unmap_page(adev->pdev, adev->dummy_page.addr,
801 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
802 __free_page(adev->dummy_page.page);
803 adev->dummy_page.page = NULL;
804}
805
806
807/* ATOM accessor methods */
808/*
809 * ATOM is an interpreted byte code stored in tables in the vbios. The
810 * driver registers callbacks to access registers and the interpreter
811 * in the driver parses the tables and executes then to program specific
812 * actions (set display modes, asic init, etc.). See amdgpu_atombios.c,
813 * atombios.h, and atom.c
814 */
815
816/**
817 * cail_pll_read - read PLL register
818 *
819 * @info: atom card_info pointer
820 * @reg: PLL register offset
821 *
822 * Provides a PLL register accessor for the atom interpreter (r4xx+).
823 * Returns the value of the PLL register.
824 */
825static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
826{
827 return 0;
828}
829
830/**
831 * cail_pll_write - write PLL register
832 *
833 * @info: atom card_info pointer
834 * @reg: PLL register offset
835 * @val: value to write to the pll register
836 *
837 * Provides a PLL register accessor for the atom interpreter (r4xx+).
838 */
839static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
840{
841
842}
843
844/**
845 * cail_mc_read - read MC (Memory Controller) register
846 *
847 * @info: atom card_info pointer
848 * @reg: MC register offset
849 *
850 * Provides an MC register accessor for the atom interpreter (r4xx+).
851 * Returns the value of the MC register.
852 */
853static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
854{
855 return 0;
856}
857
858/**
859 * cail_mc_write - write MC (Memory Controller) register
860 *
861 * @info: atom card_info pointer
862 * @reg: MC register offset
863 * @val: value to write to the pll register
864 *
865 * Provides a MC register accessor for the atom interpreter (r4xx+).
866 */
867static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
868{
869
870}
871
872/**
873 * cail_reg_write - write MMIO register
874 *
875 * @info: atom card_info pointer
876 * @reg: MMIO register offset
877 * @val: value to write to the pll register
878 *
879 * Provides a MMIO register accessor for the atom interpreter (r4xx+).
880 */
881static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
882{
883 struct amdgpu_device *adev = info->dev->dev_private;
884
885 WREG32(reg, val);
886}
887
888/**
889 * cail_reg_read - read MMIO register
890 *
891 * @info: atom card_info pointer
892 * @reg: MMIO register offset
893 *
894 * Provides an MMIO register accessor for the atom interpreter (r4xx+).
895 * Returns the value of the MMIO register.
896 */
897static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
898{
899 struct amdgpu_device *adev = info->dev->dev_private;
900 uint32_t r;
901
902 r = RREG32(reg);
903 return r;
904}
905
906/**
907 * cail_ioreg_write - write IO register
908 *
909 * @info: atom card_info pointer
910 * @reg: IO register offset
911 * @val: value to write to the pll register
912 *
913 * Provides a IO register accessor for the atom interpreter (r4xx+).
914 */
915static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
916{
917 struct amdgpu_device *adev = info->dev->dev_private;
918
919 WREG32_IO(reg, val);
920}
921
922/**
923 * cail_ioreg_read - read IO register
924 *
925 * @info: atom card_info pointer
926 * @reg: IO register offset
927 *
928 * Provides an IO register accessor for the atom interpreter (r4xx+).
929 * Returns the value of the IO register.
930 */
931static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
932{
933 struct amdgpu_device *adev = info->dev->dev_private;
934 uint32_t r;
935
936 r = RREG32_IO(reg);
937 return r;
938}
939
940/**
941 * amdgpu_atombios_fini - free the driver info and callbacks for atombios
942 *
943 * @adev: amdgpu_device pointer
944 *
945 * Frees the driver info and register access callbacks for the ATOM
946 * interpreter (r4xx+).
947 * Called at driver shutdown.
948 */
949static void amdgpu_atombios_fini(struct amdgpu_device *adev)
950{
Monk Liu89e0ec9f2016-05-27 19:34:11 +0800951 if (adev->mode_info.atom_context) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400952 kfree(adev->mode_info.atom_context->scratch);
Monk Liu89e0ec9f2016-05-27 19:34:11 +0800953 kfree(adev->mode_info.atom_context->iio);
954 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400955 kfree(adev->mode_info.atom_context);
956 adev->mode_info.atom_context = NULL;
957 kfree(adev->mode_info.atom_card_info);
958 adev->mode_info.atom_card_info = NULL;
959}
960
961/**
962 * amdgpu_atombios_init - init the driver info and callbacks for atombios
963 *
964 * @adev: amdgpu_device pointer
965 *
966 * Initializes the driver info and register access callbacks for the
967 * ATOM interpreter (r4xx+).
968 * Returns 0 on sucess, -ENOMEM on failure.
969 * Called at driver startup.
970 */
971static int amdgpu_atombios_init(struct amdgpu_device *adev)
972{
973 struct card_info *atom_card_info =
974 kzalloc(sizeof(struct card_info), GFP_KERNEL);
975
976 if (!atom_card_info)
977 return -ENOMEM;
978
979 adev->mode_info.atom_card_info = atom_card_info;
980 atom_card_info->dev = adev->ddev;
981 atom_card_info->reg_read = cail_reg_read;
982 atom_card_info->reg_write = cail_reg_write;
983 /* needed for iio ops */
984 if (adev->rio_mem) {
985 atom_card_info->ioreg_read = cail_ioreg_read;
986 atom_card_info->ioreg_write = cail_ioreg_write;
987 } else {
Amber Linb64a18c2017-01-04 08:06:58 -0500988 DRM_INFO("PCI I/O BAR is not found. Using MMIO to access ATOM BIOS\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400989 atom_card_info->ioreg_read = cail_reg_read;
990 atom_card_info->ioreg_write = cail_reg_write;
991 }
992 atom_card_info->mc_read = cail_mc_read;
993 atom_card_info->mc_write = cail_mc_write;
994 atom_card_info->pll_read = cail_pll_read;
995 atom_card_info->pll_write = cail_pll_write;
996
997 adev->mode_info.atom_context = amdgpu_atom_parse(atom_card_info, adev->bios);
998 if (!adev->mode_info.atom_context) {
999 amdgpu_atombios_fini(adev);
1000 return -ENOMEM;
1001 }
1002
1003 mutex_init(&adev->mode_info.atom_context->mutex);
Alex Deuchera5bde2f2016-09-23 16:23:41 -04001004 if (adev->is_atom_fw) {
1005 amdgpu_atomfirmware_scratch_regs_init(adev);
1006 amdgpu_atomfirmware_allocate_fb_scratch(adev);
1007 } else {
1008 amdgpu_atombios_scratch_regs_init(adev);
1009 amdgpu_atombios_allocate_fb_scratch(adev);
1010 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001011 return 0;
1012}
1013
1014/* if we get transitioned to only one device, take VGA back */
1015/**
1016 * amdgpu_vga_set_decode - enable/disable vga decode
1017 *
1018 * @cookie: amdgpu_device pointer
1019 * @state: enable/disable vga decode
1020 *
1021 * Enable/disable vga decode (all asics).
1022 * Returns VGA resource flags.
1023 */
1024static unsigned int amdgpu_vga_set_decode(void *cookie, bool state)
1025{
1026 struct amdgpu_device *adev = cookie;
1027 amdgpu_asic_set_vga_state(adev, state);
1028 if (state)
1029 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
1030 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1031 else
1032 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1033}
1034
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001035static void amdgpu_check_block_size(struct amdgpu_device *adev)
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001036{
1037 /* defines number of bits in page table versus page directory,
1038 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
1039 * page table and the remaining bits are in the page directory */
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001040 if (amdgpu_vm_block_size == -1)
1041 return;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001042
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001043 if (amdgpu_vm_block_size < 9) {
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001044 dev_warn(adev->dev, "VM page table size (%d) too small\n",
1045 amdgpu_vm_block_size);
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001046 goto def_value;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001047 }
1048
1049 if (amdgpu_vm_block_size > 24 ||
1050 (amdgpu_vm_size * 1024) < (1ull << amdgpu_vm_block_size)) {
1051 dev_warn(adev->dev, "VM page table size (%d) too large\n",
1052 amdgpu_vm_block_size);
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001053 goto def_value;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001054 }
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001055
1056 return;
1057
1058def_value:
1059 amdgpu_vm_block_size = -1;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001060}
1061
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001062static void amdgpu_check_vm_size(struct amdgpu_device *adev)
1063{
Alex Deucher64dab072017-06-15 18:20:09 -04001064 /* no need to check the default value */
1065 if (amdgpu_vm_size == -1)
1066 return;
1067
Alex Deucher76117502017-06-21 12:31:41 -04001068 if (!is_power_of_2(amdgpu_vm_size)) {
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001069 dev_warn(adev->dev, "VM size (%d) must be a power of 2\n",
1070 amdgpu_vm_size);
1071 goto def_value;
1072 }
1073
1074 if (amdgpu_vm_size < 1) {
1075 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
1076 amdgpu_vm_size);
1077 goto def_value;
1078 }
1079
1080 /*
1081 * Max GPUVM size for Cayman, SI, CI VI are 40 bits.
1082 */
1083 if (amdgpu_vm_size > 1024) {
1084 dev_warn(adev->dev, "VM size (%d) too large, max is 1TB\n",
1085 amdgpu_vm_size);
1086 goto def_value;
1087 }
1088
1089 return;
1090
1091def_value:
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001092 amdgpu_vm_size = -1;
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001093}
1094
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001095/**
1096 * amdgpu_check_arguments - validate module params
1097 *
1098 * @adev: amdgpu_device pointer
1099 *
1100 * Validates certain module parameters and updates
1101 * the associated values used by the driver (all asics).
1102 */
1103static void amdgpu_check_arguments(struct amdgpu_device *adev)
1104{
Chunming Zhou5b011232015-12-10 17:34:33 +08001105 if (amdgpu_sched_jobs < 4) {
1106 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
1107 amdgpu_sched_jobs);
1108 amdgpu_sched_jobs = 4;
Alex Deucher76117502017-06-21 12:31:41 -04001109 } else if (!is_power_of_2(amdgpu_sched_jobs)){
Chunming Zhou5b011232015-12-10 17:34:33 +08001110 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
1111 amdgpu_sched_jobs);
1112 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
1113 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001114
1115 if (amdgpu_gart_size != -1) {
Christian Königc4e1a132016-03-17 16:25:15 +01001116 /* gtt size must be greater or equal to 32M */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001117 if (amdgpu_gart_size < 32) {
1118 dev_warn(adev->dev, "gart size (%d) too small\n",
1119 amdgpu_gart_size);
1120 amdgpu_gart_size = -1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001121 }
1122 }
1123
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001124 amdgpu_check_vm_size(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001125
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001126 amdgpu_check_block_size(adev);
Christian König6a7f76e2016-08-24 15:51:49 +02001127
jimqu526bae32016-11-07 09:53:10 +08001128 if (amdgpu_vram_page_split != -1 && (amdgpu_vram_page_split < 16 ||
Alex Deucher76117502017-06-21 12:31:41 -04001129 !is_power_of_2(amdgpu_vram_page_split))) {
Christian König6a7f76e2016-08-24 15:51:49 +02001130 dev_warn(adev->dev, "invalid VRAM page split (%d)\n",
1131 amdgpu_vram_page_split);
1132 amdgpu_vram_page_split = 1024;
1133 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001134}
1135
1136/**
1137 * amdgpu_switcheroo_set_state - set switcheroo state
1138 *
1139 * @pdev: pci dev pointer
Lukas Wunner16944672015-09-05 11:17:35 +02001140 * @state: vga_switcheroo state
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001141 *
1142 * Callback for the switcheroo driver. Suspends or resumes the
1143 * the asics before or after it is powered up using ACPI methods.
1144 */
1145static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1146{
1147 struct drm_device *dev = pci_get_drvdata(pdev);
1148
1149 if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
1150 return;
1151
1152 if (state == VGA_SWITCHEROO_ON) {
1153 unsigned d3_delay = dev->pdev->d3_delay;
1154
Joe Perches7ca85292017-02-28 04:55:52 -08001155 pr_info("amdgpu: switched on\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001156 /* don't suspend or resume card normally */
1157 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1158
Alex Deucher810ddc32016-08-23 13:25:49 -04001159 amdgpu_device_resume(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001160
1161 dev->pdev->d3_delay = d3_delay;
1162
1163 dev->switch_power_state = DRM_SWITCH_POWER_ON;
1164 drm_kms_helper_poll_enable(dev);
1165 } else {
Joe Perches7ca85292017-02-28 04:55:52 -08001166 pr_info("amdgpu: switched off\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001167 drm_kms_helper_poll_disable(dev);
1168 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Alex Deucher810ddc32016-08-23 13:25:49 -04001169 amdgpu_device_suspend(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001170 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1171 }
1172}
1173
1174/**
1175 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1176 *
1177 * @pdev: pci dev pointer
1178 *
1179 * Callback for the switcheroo driver. Check of the switcheroo
1180 * state can be changed.
1181 * Returns true if the state can be changed, false if not.
1182 */
1183static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1184{
1185 struct drm_device *dev = pci_get_drvdata(pdev);
1186
1187 /*
1188 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1189 * locking inversion with the driver load path. And the access here is
1190 * completely racy anyway. So don't bother with locking for now.
1191 */
1192 return dev->open_count == 0;
1193}
1194
1195static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1196 .set_gpu_state = amdgpu_switcheroo_set_state,
1197 .reprobe = NULL,
1198 .can_switch = amdgpu_switcheroo_can_switch,
1199};
1200
1201int amdgpu_set_clockgating_state(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001202 enum amd_ip_block_type block_type,
1203 enum amd_clockgating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001204{
1205 int i, r = 0;
1206
1207 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001208 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001209 continue;
Rex Zhuc7228652017-02-22 15:33:46 +08001210 if (adev->ip_blocks[i].version->type != block_type)
1211 continue;
1212 if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
1213 continue;
1214 r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
1215 (void *)adev, state);
1216 if (r)
1217 DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
1218 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001219 }
1220 return r;
1221}
1222
1223int amdgpu_set_powergating_state(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001224 enum amd_ip_block_type block_type,
1225 enum amd_powergating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001226{
1227 int i, r = 0;
1228
1229 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001230 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001231 continue;
Rex Zhuc7228652017-02-22 15:33:46 +08001232 if (adev->ip_blocks[i].version->type != block_type)
1233 continue;
1234 if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
1235 continue;
1236 r = adev->ip_blocks[i].version->funcs->set_powergating_state(
1237 (void *)adev, state);
1238 if (r)
1239 DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
1240 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001241 }
1242 return r;
1243}
1244
Huang Rui6cb2d4e2017-01-05 18:44:41 +08001245void amdgpu_get_clockgating_state(struct amdgpu_device *adev, u32 *flags)
1246{
1247 int i;
1248
1249 for (i = 0; i < adev->num_ip_blocks; i++) {
1250 if (!adev->ip_blocks[i].status.valid)
1251 continue;
1252 if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
1253 adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
1254 }
1255}
1256
Alex Deucher5dbbb602016-06-23 11:41:04 -04001257int amdgpu_wait_for_idle(struct amdgpu_device *adev,
1258 enum amd_ip_block_type block_type)
1259{
1260 int i, r;
1261
1262 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001263 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001264 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001265 if (adev->ip_blocks[i].version->type == block_type) {
1266 r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -04001267 if (r)
1268 return r;
1269 break;
1270 }
1271 }
1272 return 0;
1273
1274}
1275
1276bool amdgpu_is_idle(struct amdgpu_device *adev,
1277 enum amd_ip_block_type block_type)
1278{
1279 int i;
1280
1281 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001282 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001283 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001284 if (adev->ip_blocks[i].version->type == block_type)
1285 return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -04001286 }
1287 return true;
1288
1289}
1290
Alex Deuchera1255102016-10-13 17:41:13 -04001291struct amdgpu_ip_block * amdgpu_get_ip_block(struct amdgpu_device *adev,
1292 enum amd_ip_block_type type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001293{
1294 int i;
1295
1296 for (i = 0; i < adev->num_ip_blocks; i++)
Alex Deuchera1255102016-10-13 17:41:13 -04001297 if (adev->ip_blocks[i].version->type == type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001298 return &adev->ip_blocks[i];
1299
1300 return NULL;
1301}
1302
1303/**
1304 * amdgpu_ip_block_version_cmp
1305 *
1306 * @adev: amdgpu_device pointer
yanyang15fc3aee2015-05-22 14:39:35 -04001307 * @type: enum amd_ip_block_type
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001308 * @major: major version
1309 * @minor: minor version
1310 *
1311 * return 0 if equal or greater
1312 * return 1 if smaller or the ip_block doesn't exist
1313 */
1314int amdgpu_ip_block_version_cmp(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001315 enum amd_ip_block_type type,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001316 u32 major, u32 minor)
1317{
Alex Deuchera1255102016-10-13 17:41:13 -04001318 struct amdgpu_ip_block *ip_block = amdgpu_get_ip_block(adev, type);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001319
Alex Deuchera1255102016-10-13 17:41:13 -04001320 if (ip_block && ((ip_block->version->major > major) ||
1321 ((ip_block->version->major == major) &&
1322 (ip_block->version->minor >= minor))))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001323 return 0;
1324
1325 return 1;
1326}
1327
Alex Deuchera1255102016-10-13 17:41:13 -04001328/**
1329 * amdgpu_ip_block_add
1330 *
1331 * @adev: amdgpu_device pointer
1332 * @ip_block_version: pointer to the IP to add
1333 *
1334 * Adds the IP block driver information to the collection of IPs
1335 * on the asic.
1336 */
1337int amdgpu_ip_block_add(struct amdgpu_device *adev,
1338 const struct amdgpu_ip_block_version *ip_block_version)
1339{
1340 if (!ip_block_version)
1341 return -EINVAL;
1342
Huang Ruia0bae352017-05-03 09:52:06 +08001343 DRM_DEBUG("add ip block number %d <%s>\n", adev->num_ip_blocks,
1344 ip_block_version->funcs->name);
1345
Alex Deuchera1255102016-10-13 17:41:13 -04001346 adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
1347
1348 return 0;
1349}
1350
Alex Deucher483ef982016-09-30 12:43:04 -04001351static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
Emily Deng9accf2f2016-08-10 16:01:25 +08001352{
1353 adev->enable_virtual_display = false;
1354
1355 if (amdgpu_virtual_display) {
1356 struct drm_device *ddev = adev->ddev;
1357 const char *pci_address_name = pci_name(ddev->pdev);
Emily Deng0f663562016-09-30 13:02:18 -04001358 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
Emily Deng9accf2f2016-08-10 16:01:25 +08001359
1360 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1361 pciaddstr_tmp = pciaddstr;
Emily Deng0f663562016-09-30 13:02:18 -04001362 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1363 pciaddname = strsep(&pciaddname_tmp, ",");
Yintian Tao967de2a2017-01-22 15:16:51 +08001364 if (!strcmp("all", pciaddname)
1365 || !strcmp(pci_address_name, pciaddname)) {
Emily Deng0f663562016-09-30 13:02:18 -04001366 long num_crtc;
1367 int res = -1;
1368
Emily Deng9accf2f2016-08-10 16:01:25 +08001369 adev->enable_virtual_display = true;
Emily Deng0f663562016-09-30 13:02:18 -04001370
1371 if (pciaddname_tmp)
1372 res = kstrtol(pciaddname_tmp, 10,
1373 &num_crtc);
1374
1375 if (!res) {
1376 if (num_crtc < 1)
1377 num_crtc = 1;
1378 if (num_crtc > 6)
1379 num_crtc = 6;
1380 adev->mode_info.num_crtc = num_crtc;
1381 } else {
1382 adev->mode_info.num_crtc = 1;
1383 }
Emily Deng9accf2f2016-08-10 16:01:25 +08001384 break;
1385 }
1386 }
1387
Emily Deng0f663562016-09-30 13:02:18 -04001388 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
1389 amdgpu_virtual_display, pci_address_name,
1390 adev->enable_virtual_display, adev->mode_info.num_crtc);
Emily Deng9accf2f2016-08-10 16:01:25 +08001391
1392 kfree(pciaddstr);
1393 }
1394}
1395
Alex Deuchere2a75f82017-04-27 16:58:01 -04001396static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev)
1397{
Alex Deuchere2a75f82017-04-27 16:58:01 -04001398 const char *chip_name;
1399 char fw_name[30];
1400 int err;
1401 const struct gpu_info_firmware_header_v1_0 *hdr;
1402
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001403 adev->firmware.gpu_info_fw = NULL;
1404
Alex Deuchere2a75f82017-04-27 16:58:01 -04001405 switch (adev->asic_type) {
1406 case CHIP_TOPAZ:
1407 case CHIP_TONGA:
1408 case CHIP_FIJI:
1409 case CHIP_POLARIS11:
1410 case CHIP_POLARIS10:
1411 case CHIP_POLARIS12:
1412 case CHIP_CARRIZO:
1413 case CHIP_STONEY:
1414#ifdef CONFIG_DRM_AMDGPU_SI
1415 case CHIP_VERDE:
1416 case CHIP_TAHITI:
1417 case CHIP_PITCAIRN:
1418 case CHIP_OLAND:
1419 case CHIP_HAINAN:
1420#endif
1421#ifdef CONFIG_DRM_AMDGPU_CIK
1422 case CHIP_BONAIRE:
1423 case CHIP_HAWAII:
1424 case CHIP_KAVERI:
1425 case CHIP_KABINI:
1426 case CHIP_MULLINS:
1427#endif
1428 default:
1429 return 0;
1430 case CHIP_VEGA10:
1431 chip_name = "vega10";
1432 break;
Alex Deucher2d2e5e72017-05-09 12:27:35 -04001433 case CHIP_RAVEN:
1434 chip_name = "raven";
1435 break;
Alex Deuchere2a75f82017-04-27 16:58:01 -04001436 }
1437
1438 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name);
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001439 err = request_firmware(&adev->firmware.gpu_info_fw, fw_name, adev->dev);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001440 if (err) {
1441 dev_err(adev->dev,
1442 "Failed to load gpu_info firmware \"%s\"\n",
1443 fw_name);
1444 goto out;
1445 }
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001446 err = amdgpu_ucode_validate(adev->firmware.gpu_info_fw);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001447 if (err) {
1448 dev_err(adev->dev,
1449 "Failed to validate gpu_info firmware \"%s\"\n",
1450 fw_name);
1451 goto out;
1452 }
1453
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001454 hdr = (const struct gpu_info_firmware_header_v1_0 *)adev->firmware.gpu_info_fw->data;
Alex Deuchere2a75f82017-04-27 16:58:01 -04001455 amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
1456
1457 switch (hdr->version_major) {
1458 case 1:
1459 {
1460 const struct gpu_info_firmware_v1_0 *gpu_info_fw =
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001461 (const struct gpu_info_firmware_v1_0 *)(adev->firmware.gpu_info_fw->data +
Alex Deuchere2a75f82017-04-27 16:58:01 -04001462 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1463
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001464 adev->gfx.config.max_shader_engines = le32_to_cpu(gpu_info_fw->gc_num_se);
1465 adev->gfx.config.max_cu_per_sh = le32_to_cpu(gpu_info_fw->gc_num_cu_per_sh);
1466 adev->gfx.config.max_sh_per_se = le32_to_cpu(gpu_info_fw->gc_num_sh_per_se);
1467 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 -04001468 adev->gfx.config.max_texture_channel_caches =
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001469 le32_to_cpu(gpu_info_fw->gc_num_tccs);
1470 adev->gfx.config.max_gprs = le32_to_cpu(gpu_info_fw->gc_num_gprs);
1471 adev->gfx.config.max_gs_threads = le32_to_cpu(gpu_info_fw->gc_num_max_gs_thds);
1472 adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gpu_info_fw->gc_gs_table_depth);
1473 adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gpu_info_fw->gc_gsprim_buff_depth);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001474 adev->gfx.config.double_offchip_lds_buf =
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001475 le32_to_cpu(gpu_info_fw->gc_double_offchip_lds_buffer);
1476 adev->gfx.cu_info.wave_front_size = le32_to_cpu(gpu_info_fw->gc_wave_size);
Hawking Zhang51fd0372017-06-09 22:30:52 +08001477 adev->gfx.cu_info.max_waves_per_simd =
1478 le32_to_cpu(gpu_info_fw->gc_max_waves_per_simd);
1479 adev->gfx.cu_info.max_scratch_slots_per_cu =
1480 le32_to_cpu(gpu_info_fw->gc_max_scratch_slots_per_cu);
1481 adev->gfx.cu_info.lds_size = le32_to_cpu(gpu_info_fw->gc_lds_size);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001482 break;
1483 }
1484 default:
1485 dev_err(adev->dev,
1486 "Unsupported gpu_info table %d\n", hdr->header.ucode_version);
1487 err = -EINVAL;
1488 goto out;
1489 }
1490out:
Alex Deuchere2a75f82017-04-27 16:58:01 -04001491 return err;
1492}
1493
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001494static int amdgpu_early_init(struct amdgpu_device *adev)
1495{
Alex Deucheraaa36a92015-04-20 17:31:14 -04001496 int i, r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001497
Alex Deucher483ef982016-09-30 12:43:04 -04001498 amdgpu_device_enable_virtual_display(adev);
Emily Denga6be7572016-08-08 11:37:50 +08001499
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001500 switch (adev->asic_type) {
Alex Deucheraaa36a92015-04-20 17:31:14 -04001501 case CHIP_TOPAZ:
1502 case CHIP_TONGA:
David Zhang48299f92015-07-08 01:05:16 +08001503 case CHIP_FIJI:
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001504 case CHIP_POLARIS11:
1505 case CHIP_POLARIS10:
Junwei Zhangc4642a42016-12-14 15:32:28 -05001506 case CHIP_POLARIS12:
Alex Deucheraaa36a92015-04-20 17:31:14 -04001507 case CHIP_CARRIZO:
Samuel Li39bb0c92015-10-08 16:31:43 -04001508 case CHIP_STONEY:
1509 if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
Alex Deucheraaa36a92015-04-20 17:31:14 -04001510 adev->family = AMDGPU_FAMILY_CZ;
1511 else
1512 adev->family = AMDGPU_FAMILY_VI;
1513
1514 r = vi_set_ip_blocks(adev);
1515 if (r)
1516 return r;
1517 break;
Ken Wang33f34802016-01-21 17:29:41 +08001518#ifdef CONFIG_DRM_AMDGPU_SI
1519 case CHIP_VERDE:
1520 case CHIP_TAHITI:
1521 case CHIP_PITCAIRN:
1522 case CHIP_OLAND:
1523 case CHIP_HAINAN:
Ken Wang295d0da2016-05-24 21:02:53 +08001524 adev->family = AMDGPU_FAMILY_SI;
Ken Wang33f34802016-01-21 17:29:41 +08001525 r = si_set_ip_blocks(adev);
1526 if (r)
1527 return r;
1528 break;
1529#endif
Alex Deuchera2e73f52015-04-20 17:09:27 -04001530#ifdef CONFIG_DRM_AMDGPU_CIK
1531 case CHIP_BONAIRE:
1532 case CHIP_HAWAII:
1533 case CHIP_KAVERI:
1534 case CHIP_KABINI:
1535 case CHIP_MULLINS:
1536 if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
1537 adev->family = AMDGPU_FAMILY_CI;
1538 else
1539 adev->family = AMDGPU_FAMILY_KV;
1540
1541 r = cik_set_ip_blocks(adev);
1542 if (r)
1543 return r;
1544 break;
1545#endif
Chunming Zhou2ca8a5d2016-12-07 17:31:19 +08001546 case CHIP_VEGA10:
1547 case CHIP_RAVEN:
1548 if (adev->asic_type == CHIP_RAVEN)
1549 adev->family = AMDGPU_FAMILY_RV;
1550 else
1551 adev->family = AMDGPU_FAMILY_AI;
Ken Wang460826e2017-03-06 14:53:16 -05001552
1553 r = soc15_set_ip_blocks(adev);
1554 if (r)
1555 return r;
1556 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001557 default:
1558 /* FIXME: not supported yet */
1559 return -EINVAL;
1560 }
1561
Alex Deuchere2a75f82017-04-27 16:58:01 -04001562 r = amdgpu_device_parse_gpu_info_fw(adev);
1563 if (r)
1564 return r;
1565
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001566 if (amdgpu_sriov_vf(adev)) {
1567 r = amdgpu_virt_request_full_gpu(adev, true);
1568 if (r)
1569 return r;
1570 }
1571
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001572 for (i = 0; i < adev->num_ip_blocks; i++) {
1573 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
Huang Ruied8cf002017-05-03 09:40:17 +08001574 DRM_ERROR("disabled ip block: %d <%s>\n",
1575 i, adev->ip_blocks[i].version->funcs->name);
Alex Deuchera1255102016-10-13 17:41:13 -04001576 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001577 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001578 if (adev->ip_blocks[i].version->funcs->early_init) {
1579 r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001580 if (r == -ENOENT) {
Alex Deuchera1255102016-10-13 17:41:13 -04001581 adev->ip_blocks[i].status.valid = false;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001582 } else if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001583 DRM_ERROR("early_init of IP block <%s> failed %d\n",
1584 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001585 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001586 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001587 adev->ip_blocks[i].status.valid = true;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001588 }
Alex Deucher974e6b62015-07-10 13:59:44 -04001589 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001590 adev->ip_blocks[i].status.valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001591 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001592 }
1593 }
1594
Nicolai Hähnle395d1fb2016-06-02 12:32:07 +02001595 adev->cg_flags &= amdgpu_cg_mask;
1596 adev->pg_flags &= amdgpu_pg_mask;
1597
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001598 return 0;
1599}
1600
1601static int amdgpu_init(struct amdgpu_device *adev)
1602{
1603 int i, r;
1604
1605 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001606 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001607 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001608 r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001609 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001610 DRM_ERROR("sw_init of IP block <%s> failed %d\n",
1611 adev->ip_blocks[i].version->funcs->name, 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.sw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001615 /* need to do gmc hw init early so we can allocate gpu mem */
Alex Deuchera1255102016-10-13 17:41:13 -04001616 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001617 r = amdgpu_vram_scratch_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001618 if (r) {
1619 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001620 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001621 }
Alex Deuchera1255102016-10-13 17:41:13 -04001622 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001623 if (r) {
1624 DRM_ERROR("hw_init %d failed %d\n", i, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001625 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001626 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001627 r = amdgpu_wb_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001628 if (r) {
1629 DRM_ERROR("amdgpu_wb_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001630 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001631 }
Alex Deuchera1255102016-10-13 17:41:13 -04001632 adev->ip_blocks[i].status.hw = true;
Monk Liu24936642017-01-09 15:54:32 +08001633
1634 /* right after GMC hw init, we create CSA */
1635 if (amdgpu_sriov_vf(adev)) {
1636 r = amdgpu_allocate_static_csa(adev);
1637 if (r) {
1638 DRM_ERROR("allocate CSA failed %d\n", r);
1639 return r;
1640 }
1641 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001642 }
1643 }
1644
1645 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001646 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001647 continue;
1648 /* gmc hw init is done early */
Alex Deuchera1255102016-10-13 17:41:13 -04001649 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001650 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001651 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001652 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001653 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
1654 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001655 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001656 }
Alex Deuchera1255102016-10-13 17:41:13 -04001657 adev->ip_blocks[i].status.hw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001658 }
1659
1660 return 0;
1661}
1662
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08001663static void amdgpu_fill_reset_magic(struct amdgpu_device *adev)
1664{
1665 memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM);
1666}
1667
1668static bool amdgpu_check_vram_lost(struct amdgpu_device *adev)
1669{
1670 return !!memcmp(adev->gart.ptr, adev->reset_magic,
1671 AMDGPU_RESET_MAGIC_NUM);
1672}
1673
Shirish S2dc80b02017-05-25 10:05:25 +05301674static int amdgpu_late_set_cg_state(struct amdgpu_device *adev)
1675{
1676 int i = 0, r;
1677
1678 for (i = 0; i < adev->num_ip_blocks; i++) {
1679 if (!adev->ip_blocks[i].status.valid)
1680 continue;
1681 /* skip CG for VCE/UVD, it's handled specially */
1682 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1683 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1684 /* enable clockgating to save power */
1685 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1686 AMD_CG_STATE_GATE);
1687 if (r) {
1688 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
1689 adev->ip_blocks[i].version->funcs->name, r);
1690 return r;
1691 }
1692 }
1693 }
1694 return 0;
1695}
1696
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001697static int amdgpu_late_init(struct amdgpu_device *adev)
1698{
1699 int i = 0, r;
1700
1701 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001702 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001703 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001704 if (adev->ip_blocks[i].version->funcs->late_init) {
1705 r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001706 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001707 DRM_ERROR("late_init of IP block <%s> failed %d\n",
1708 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001709 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001710 }
Alex Deuchera1255102016-10-13 17:41:13 -04001711 adev->ip_blocks[i].status.late_initialized = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001712 }
1713 }
1714
Shirish S2dc80b02017-05-25 10:05:25 +05301715 mod_delayed_work(system_wq, &adev->late_init_work,
1716 msecs_to_jiffies(AMDGPU_RESUME_MS));
1717
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08001718 amdgpu_fill_reset_magic(adev);
1719
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001720 return 0;
1721}
1722
1723static int amdgpu_fini(struct amdgpu_device *adev)
1724{
1725 int i, r;
1726
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001727 /* need to disable SMC first */
1728 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001729 if (!adev->ip_blocks[i].status.hw)
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001730 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001731 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001732 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
Alex Deuchera1255102016-10-13 17:41:13 -04001733 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1734 AMD_CG_STATE_UNGATE);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001735 if (r) {
1736 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001737 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001738 return r;
1739 }
Alex Deuchera1255102016-10-13 17:41:13 -04001740 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001741 /* XXX handle errors */
1742 if (r) {
1743 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001744 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001745 }
Alex Deuchera1255102016-10-13 17:41:13 -04001746 adev->ip_blocks[i].status.hw = false;
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001747 break;
1748 }
1749 }
1750
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001751 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001752 if (!adev->ip_blocks[i].status.hw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001753 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001754 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001755 amdgpu_wb_fini(adev);
1756 amdgpu_vram_scratch_fini(adev);
1757 }
Rex Zhu8201a672016-11-24 21:44:44 +08001758
1759 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1760 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1761 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
1762 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1763 AMD_CG_STATE_UNGATE);
1764 if (r) {
1765 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1766 adev->ip_blocks[i].version->funcs->name, r);
1767 return r;
1768 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001769 }
Rex Zhu8201a672016-11-24 21:44:44 +08001770
Alex Deuchera1255102016-10-13 17:41:13 -04001771 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001772 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001773 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001774 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
1775 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001776 }
Rex Zhu8201a672016-11-24 21:44:44 +08001777
Alex Deuchera1255102016-10-13 17:41:13 -04001778 adev->ip_blocks[i].status.hw = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001779 }
1780
1781 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001782 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001783 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001784 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001785 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001786 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001787 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
1788 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001789 }
Alex Deuchera1255102016-10-13 17:41:13 -04001790 adev->ip_blocks[i].status.sw = false;
1791 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001792 }
1793
Monk Liua6dcfd92016-05-19 14:36:34 +08001794 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001795 if (!adev->ip_blocks[i].status.late_initialized)
Grazvydas Ignotas8a2eef12016-10-03 00:06:44 +03001796 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001797 if (adev->ip_blocks[i].version->funcs->late_fini)
1798 adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
1799 adev->ip_blocks[i].status.late_initialized = false;
Monk Liua6dcfd92016-05-19 14:36:34 +08001800 }
1801
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001802 if (amdgpu_sriov_vf(adev)) {
Monk Liu24936642017-01-09 15:54:32 +08001803 amdgpu_bo_free_kernel(&adev->virt.csa_obj, &adev->virt.csa_vmid0_addr, NULL);
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001804 amdgpu_virt_release_full_gpu(adev, false);
1805 }
Monk Liu24936642017-01-09 15:54:32 +08001806
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001807 return 0;
1808}
1809
Shirish S2dc80b02017-05-25 10:05:25 +05301810static void amdgpu_late_init_func_handler(struct work_struct *work)
1811{
1812 struct amdgpu_device *adev =
1813 container_of(work, struct amdgpu_device, late_init_work.work);
1814 amdgpu_late_set_cg_state(adev);
1815}
1816
Alex Deucherfaefba92016-12-06 10:38:29 -05001817int amdgpu_suspend(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001818{
1819 int i, r;
1820
Xiangliang Yue941ea92017-01-18 12:47:55 +08001821 if (amdgpu_sriov_vf(adev))
1822 amdgpu_virt_request_full_gpu(adev, false);
1823
Flora Cuic5a93a22016-02-26 10:45:25 +08001824 /* ungate SMC block first */
1825 r = amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC,
1826 AMD_CG_STATE_UNGATE);
1827 if (r) {
1828 DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n",r);
1829 }
1830
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001831 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001832 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001833 continue;
1834 /* ungate blocks so that suspend can properly shut them down */
Flora Cuic5a93a22016-02-26 10:45:25 +08001835 if (i != AMD_IP_BLOCK_TYPE_SMC) {
Alex Deuchera1255102016-10-13 17:41:13 -04001836 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1837 AMD_CG_STATE_UNGATE);
Flora Cuic5a93a22016-02-26 10:45:25 +08001838 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001839 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1840 adev->ip_blocks[i].version->funcs->name, r);
Flora Cuic5a93a22016-02-26 10:45:25 +08001841 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001842 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001843 /* XXX handle errors */
Alex Deuchera1255102016-10-13 17:41:13 -04001844 r = adev->ip_blocks[i].version->funcs->suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001845 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001846 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001847 DRM_ERROR("suspend of IP block <%s> failed %d\n",
1848 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001849 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001850 }
1851
Xiangliang Yue941ea92017-01-18 12:47:55 +08001852 if (amdgpu_sriov_vf(adev))
1853 amdgpu_virt_release_full_gpu(adev, false);
1854
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001855 return 0;
1856}
1857
Monk Liue4f0fdc2017-02-09 11:55:49 +08001858static int amdgpu_sriov_reinit_early(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08001859{
1860 int i, r;
1861
Monk Liu2cb681b2017-04-26 12:00:49 +08001862 static enum amd_ip_block_type ip_order[] = {
1863 AMD_IP_BLOCK_TYPE_GMC,
1864 AMD_IP_BLOCK_TYPE_COMMON,
Monk Liu2cb681b2017-04-26 12:00:49 +08001865 AMD_IP_BLOCK_TYPE_IH,
1866 };
Monk Liua90ad3c2017-01-23 14:22:08 +08001867
Monk Liu2cb681b2017-04-26 12:00:49 +08001868 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1869 int j;
1870 struct amdgpu_ip_block *block;
Monk Liua90ad3c2017-01-23 14:22:08 +08001871
Monk Liu2cb681b2017-04-26 12:00:49 +08001872 for (j = 0; j < adev->num_ip_blocks; j++) {
1873 block = &adev->ip_blocks[j];
1874
1875 if (block->version->type != ip_order[i] ||
1876 !block->status.valid)
1877 continue;
1878
1879 r = block->version->funcs->hw_init(adev);
1880 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
Monk Liua90ad3c2017-01-23 14:22:08 +08001881 }
1882 }
1883
1884 return 0;
1885}
1886
Monk Liue4f0fdc2017-02-09 11:55:49 +08001887static int amdgpu_sriov_reinit_late(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08001888{
1889 int i, r;
1890
Monk Liu2cb681b2017-04-26 12:00:49 +08001891 static enum amd_ip_block_type ip_order[] = {
1892 AMD_IP_BLOCK_TYPE_SMC,
1893 AMD_IP_BLOCK_TYPE_DCE,
1894 AMD_IP_BLOCK_TYPE_GFX,
1895 AMD_IP_BLOCK_TYPE_SDMA,
1896 AMD_IP_BLOCK_TYPE_VCE,
1897 };
Monk Liua90ad3c2017-01-23 14:22:08 +08001898
Monk Liu2cb681b2017-04-26 12:00:49 +08001899 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1900 int j;
1901 struct amdgpu_ip_block *block;
Monk Liua90ad3c2017-01-23 14:22:08 +08001902
Monk Liu2cb681b2017-04-26 12:00:49 +08001903 for (j = 0; j < adev->num_ip_blocks; j++) {
1904 block = &adev->ip_blocks[j];
1905
1906 if (block->version->type != ip_order[i] ||
1907 !block->status.valid)
1908 continue;
1909
1910 r = block->version->funcs->hw_init(adev);
1911 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
Monk Liua90ad3c2017-01-23 14:22:08 +08001912 }
1913 }
1914
1915 return 0;
1916}
1917
Chunming Zhoufcf06492017-05-05 10:33:33 +08001918static int amdgpu_resume_phase1(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001919{
1920 int i, r;
1921
1922 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001923 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001924 continue;
Chunming Zhoufcf06492017-05-05 10:33:33 +08001925 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
1926 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
1927 adev->ip_blocks[i].version->type ==
1928 AMD_IP_BLOCK_TYPE_IH) {
1929 r = adev->ip_blocks[i].version->funcs->resume(adev);
1930 if (r) {
1931 DRM_ERROR("resume of IP block <%s> failed %d\n",
1932 adev->ip_blocks[i].version->funcs->name, r);
1933 return r;
1934 }
1935 }
1936 }
1937
1938 return 0;
1939}
1940
1941static int amdgpu_resume_phase2(struct amdgpu_device *adev)
1942{
1943 int i, r;
1944
1945 for (i = 0; i < adev->num_ip_blocks; i++) {
1946 if (!adev->ip_blocks[i].status.valid)
1947 continue;
1948 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
1949 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
1950 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH )
1951 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001952 r = adev->ip_blocks[i].version->funcs->resume(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001953 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001954 DRM_ERROR("resume of IP block <%s> failed %d\n",
1955 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001956 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001957 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001958 }
1959
1960 return 0;
1961}
1962
Chunming Zhoufcf06492017-05-05 10:33:33 +08001963static int amdgpu_resume(struct amdgpu_device *adev)
1964{
1965 int r;
1966
1967 r = amdgpu_resume_phase1(adev);
1968 if (r)
1969 return r;
1970 r = amdgpu_resume_phase2(adev);
1971
1972 return r;
1973}
1974
Monk Liu4e99a442016-03-31 13:26:59 +08001975static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
Andres Rodriguez048765a2016-06-11 02:51:32 -04001976{
Alex Deuchera5bde2f2016-09-23 16:23:41 -04001977 if (adev->is_atom_fw) {
1978 if (amdgpu_atomfirmware_gpu_supports_virtualization(adev))
1979 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
1980 } else {
1981 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
1982 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
1983 }
Andres Rodriguez048765a2016-06-11 02:51:32 -04001984}
1985
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001986/**
1987 * amdgpu_device_init - initialize the driver
1988 *
1989 * @adev: amdgpu_device pointer
1990 * @pdev: drm dev pointer
1991 * @pdev: pci dev pointer
1992 * @flags: driver flags
1993 *
1994 * Initializes the driver info and hw (all asics).
1995 * Returns 0 for success or an error on failure.
1996 * Called at driver startup.
1997 */
1998int amdgpu_device_init(struct amdgpu_device *adev,
1999 struct drm_device *ddev,
2000 struct pci_dev *pdev,
2001 uint32_t flags)
2002{
2003 int r, i;
2004 bool runtime = false;
Marek Olšák95844d22016-08-17 23:49:27 +02002005 u32 max_MBps;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002006
2007 adev->shutdown = false;
2008 adev->dev = &pdev->dev;
2009 adev->ddev = ddev;
2010 adev->pdev = pdev;
2011 adev->flags = flags;
Jammy Zhou2f7d10b2015-07-22 11:29:01 +08002012 adev->asic_type = flags & AMD_ASIC_MASK;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002013 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
2014 adev->mc.gtt_size = 512 * 1024 * 1024;
2015 adev->accel_working = false;
2016 adev->num_rings = 0;
2017 adev->mman.buffer_funcs = NULL;
2018 adev->mman.buffer_funcs_ring = NULL;
2019 adev->vm_manager.vm_pte_funcs = NULL;
Christian König2d55e452016-02-08 17:37:38 +01002020 adev->vm_manager.vm_pte_num_rings = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002021 adev->gart.gart_funcs = NULL;
Chris Wilsonf54d1862016-10-25 13:00:45 +01002022 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002023
2024 adev->smc_rreg = &amdgpu_invalid_rreg;
2025 adev->smc_wreg = &amdgpu_invalid_wreg;
2026 adev->pcie_rreg = &amdgpu_invalid_rreg;
2027 adev->pcie_wreg = &amdgpu_invalid_wreg;
Huang Rui36b9a952016-08-31 13:23:25 +08002028 adev->pciep_rreg = &amdgpu_invalid_rreg;
2029 adev->pciep_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002030 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
2031 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
2032 adev->didt_rreg = &amdgpu_invalid_rreg;
2033 adev->didt_wreg = &amdgpu_invalid_wreg;
Rex Zhuccdbb202016-06-08 12:47:41 +08002034 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
2035 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002036 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
2037 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
2038
Rex Zhuccdbb202016-06-08 12:47:41 +08002039
Alex Deucher3e39ab92015-06-05 15:04:33 -04002040 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
2041 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
2042 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002043
2044 /* mutex initialization are all done here so we
2045 * can recall function without having locking issues */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002046 atomic_set(&adev->irq.ih.lock, 0);
Huang Rui0e5ca0d2017-03-03 18:37:23 -05002047 mutex_init(&adev->firmware.mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002048 mutex_init(&adev->pm.mutex);
2049 mutex_init(&adev->gfx.gpu_clock_mutex);
2050 mutex_init(&adev->srbm_mutex);
2051 mutex_init(&adev->grbm_idx_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002052 mutex_init(&adev->mn_lock);
2053 hash_init(adev->mn_hash);
2054
2055 amdgpu_check_arguments(adev);
2056
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002057 spin_lock_init(&adev->mmio_idx_lock);
2058 spin_lock_init(&adev->smc_idx_lock);
2059 spin_lock_init(&adev->pcie_idx_lock);
2060 spin_lock_init(&adev->uvd_ctx_idx_lock);
2061 spin_lock_init(&adev->didt_idx_lock);
Rex Zhuccdbb202016-06-08 12:47:41 +08002062 spin_lock_init(&adev->gc_cac_idx_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002063 spin_lock_init(&adev->audio_endpt_idx_lock);
Marek Olšák95844d22016-08-17 23:49:27 +02002064 spin_lock_init(&adev->mm_stats.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002065
Chunming Zhou0c4e7fa2016-08-17 11:41:30 +08002066 INIT_LIST_HEAD(&adev->shadow_list);
2067 mutex_init(&adev->shadow_list_lock);
2068
Chunming Zhou5c1354b2016-08-30 16:13:10 +08002069 INIT_LIST_HEAD(&adev->gtt_list);
2070 spin_lock_init(&adev->gtt_list_lock);
2071
Andres Rodriguez795f2812017-03-06 16:27:55 -05002072 INIT_LIST_HEAD(&adev->ring_lru_list);
2073 spin_lock_init(&adev->ring_lru_list_lock);
2074
Shirish S2dc80b02017-05-25 10:05:25 +05302075 INIT_DELAYED_WORK(&adev->late_init_work, amdgpu_late_init_func_handler);
2076
Alex Xie0fa49552017-06-08 14:58:05 -04002077 /* Registers mapping */
2078 /* TODO: block userspace mapping of io register */
Ken Wangda69c1612016-01-21 19:08:55 +08002079 if (adev->asic_type >= CHIP_BONAIRE) {
2080 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
2081 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
2082 } else {
2083 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
2084 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
2085 }
Chunming Zhou5c1354b2016-08-30 16:13:10 +08002086
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002087 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
2088 if (adev->rmmio == NULL) {
2089 return -ENOMEM;
2090 }
2091 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
2092 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
2093
Ken Wangda69c1612016-01-21 19:08:55 +08002094 if (adev->asic_type >= CHIP_BONAIRE)
2095 /* doorbell bar mapping */
2096 amdgpu_doorbell_init(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002097
2098 /* io port mapping */
2099 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
2100 if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
2101 adev->rio_mem_size = pci_resource_len(adev->pdev, i);
2102 adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
2103 break;
2104 }
2105 }
2106 if (adev->rio_mem == NULL)
Amber Linb64a18c2017-01-04 08:06:58 -05002107 DRM_INFO("PCI I/O BAR is not found.\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002108
2109 /* early init functions */
2110 r = amdgpu_early_init(adev);
2111 if (r)
2112 return r;
2113
2114 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
2115 /* this will fail for cards that aren't VGA class devices, just
2116 * ignore it */
2117 vga_client_register(adev->pdev, adev, NULL, amdgpu_vga_set_decode);
2118
2119 if (amdgpu_runtime_pm == 1)
2120 runtime = true;
Alex Deuchere9bef452016-04-25 13:12:18 -04002121 if (amdgpu_device_is_px(ddev))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002122 runtime = true;
Lukas Wunner84c8b222017-03-10 21:23:45 +01002123 if (!pci_is_thunderbolt_attached(adev->pdev))
2124 vga_switcheroo_register_client(adev->pdev,
2125 &amdgpu_switcheroo_ops, runtime);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002126 if (runtime)
2127 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
2128
2129 /* Read BIOS */
Alex Deucher83ba1262016-06-03 18:21:41 -04002130 if (!amdgpu_get_bios(adev)) {
2131 r = -EINVAL;
2132 goto failed;
2133 }
Nils Wallméniusf7e9e9f2016-12-14 21:52:45 +01002134
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002135 r = amdgpu_atombios_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002136 if (r) {
2137 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
Gavin Wan89041942017-06-23 13:55:15 -04002138 amdgpu_vf_error_put(AMDGIM_ERROR_VF_ATOMBIOS_INIT_FAIL, 0, 0);
Alex Deucher83ba1262016-06-03 18:21:41 -04002139 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002140 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002141
Monk Liu4e99a442016-03-31 13:26:59 +08002142 /* detect if we are with an SRIOV vbios */
2143 amdgpu_device_detect_sriov_bios(adev);
Andres Rodriguez048765a2016-06-11 02:51:32 -04002144
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002145 /* Post card if necessary */
Monk Liubec86372016-09-14 19:38:08 +08002146 if (amdgpu_vpost_needed(adev)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002147 if (!adev->bios) {
Monk Liubec86372016-09-14 19:38:08 +08002148 dev_err(adev->dev, "no vBIOS found\n");
Gavin Wan89041942017-06-23 13:55:15 -04002149 amdgpu_vf_error_put(AMDGIM_ERROR_VF_NO_VBIOS, 0, 0);
Alex Deucher83ba1262016-06-03 18:21:41 -04002150 r = -EINVAL;
2151 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002152 }
Monk Liubec86372016-09-14 19:38:08 +08002153 DRM_INFO("GPU posting now...\n");
Monk Liu4e99a442016-03-31 13:26:59 +08002154 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2155 if (r) {
2156 dev_err(adev->dev, "gpu post error!\n");
Gavin Wan89041942017-06-23 13:55:15 -04002157 amdgpu_vf_error_put(AMDGIM_ERROR_VF_GPU_POST_ERROR, 0, 0);
Monk Liu4e99a442016-03-31 13:26:59 +08002158 goto failed;
2159 }
2160 } else {
2161 DRM_INFO("GPU post is not needed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002162 }
2163
Alex Deuchera5bde2f2016-09-23 16:23:41 -04002164 if (!adev->is_atom_fw) {
2165 /* Initialize clocks */
2166 r = amdgpu_atombios_get_clock_info(adev);
2167 if (r) {
2168 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
Gavin Wan89041942017-06-23 13:55:15 -04002169 amdgpu_vf_error_put(AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
2170 goto failed;
Alex Deuchera5bde2f2016-09-23 16:23:41 -04002171 }
2172 /* init i2c buses */
2173 amdgpu_atombios_i2c_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002174 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002175
2176 /* Fence driver */
2177 r = amdgpu_fence_driver_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002178 if (r) {
2179 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
Gavin Wan89041942017-06-23 13:55:15 -04002180 amdgpu_vf_error_put(AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0);
Alex Deucher83ba1262016-06-03 18:21:41 -04002181 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002182 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002183
2184 /* init the mode config */
2185 drm_mode_config_init(adev->ddev);
2186
2187 r = amdgpu_init(adev);
2188 if (r) {
Alex Deucher2c1a2782015-12-07 17:02:53 -05002189 dev_err(adev->dev, "amdgpu_init failed\n");
Gavin Wan89041942017-06-23 13:55:15 -04002190 amdgpu_vf_error_put(AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002191 amdgpu_fini(adev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002192 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002193 }
2194
2195 adev->accel_working = true;
2196
Alex Xiee59c0202017-06-01 09:42:59 -04002197 amdgpu_vm_check_compute_bug(adev);
2198
Marek Olšák95844d22016-08-17 23:49:27 +02002199 /* Initialize the buffer migration limit. */
2200 if (amdgpu_moverate >= 0)
2201 max_MBps = amdgpu_moverate;
2202 else
2203 max_MBps = 8; /* Allow 8 MB/s. */
2204 /* Get a log2 for easy divisions. */
2205 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
2206
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002207 r = amdgpu_ib_pool_init(adev);
2208 if (r) {
2209 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
Gavin Wan89041942017-06-23 13:55:15 -04002210 amdgpu_vf_error_put(AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r);
Alex Deucher83ba1262016-06-03 18:21:41 -04002211 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002212 }
2213
2214 r = amdgpu_ib_ring_tests(adev);
2215 if (r)
2216 DRM_ERROR("ib ring test failed (%d).\n", r);
2217
Monk Liu9bc92b92017-02-08 17:38:13 +08002218 amdgpu_fbdev_init(adev);
2219
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002220 r = amdgpu_gem_debugfs_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002221 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002222 DRM_ERROR("registering gem debugfs failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002223
2224 r = amdgpu_debugfs_regs_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002225 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002226 DRM_ERROR("registering register debugfs failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002227
Huang Rui4f0955f2017-05-10 23:04:06 +08002228 r = amdgpu_debugfs_test_ib_ring_init(adev);
2229 if (r)
2230 DRM_ERROR("registering register test ib ring debugfs failed (%d).\n", r);
2231
Huang Rui50ab2532016-06-12 15:51:09 +08002232 r = amdgpu_debugfs_firmware_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002233 if (r)
Huang Rui50ab2532016-06-12 15:51:09 +08002234 DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
Huang Rui50ab2532016-06-12 15:51:09 +08002235
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002236 if ((amdgpu_testing & 1)) {
2237 if (adev->accel_working)
2238 amdgpu_test_moves(adev);
2239 else
2240 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
2241 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002242 if (amdgpu_benchmarking) {
2243 if (adev->accel_working)
2244 amdgpu_benchmark(adev, amdgpu_benchmarking);
2245 else
2246 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
2247 }
2248
2249 /* enable clockgating, etc. after ib tests, etc. since some blocks require
2250 * explicit gating rather than handling it automatically.
2251 */
2252 r = amdgpu_late_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002253 if (r) {
2254 dev_err(adev->dev, "amdgpu_late_init failed\n");
Gavin Wan89041942017-06-23 13:55:15 -04002255 amdgpu_vf_error_put(AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r);
Alex Deucher83ba1262016-06-03 18:21:41 -04002256 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002257 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002258
2259 return 0;
Alex Deucher83ba1262016-06-03 18:21:41 -04002260
2261failed:
Gavin Wan89041942017-06-23 13:55:15 -04002262 amdgpu_vf_error_trans_all(adev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002263 if (runtime)
2264 vga_switcheroo_fini_domain_pm_ops(adev->dev);
2265 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002266}
2267
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002268/**
2269 * amdgpu_device_fini - tear down the driver
2270 *
2271 * @adev: amdgpu_device pointer
2272 *
2273 * Tear down the driver info (all asics).
2274 * Called at driver shutdown.
2275 */
2276void amdgpu_device_fini(struct amdgpu_device *adev)
2277{
2278 int r;
2279
2280 DRM_INFO("amdgpu: finishing device.\n");
2281 adev->shutdown = true;
Pixel Dingdb2c2a92017-04-25 16:47:42 +08002282 if (adev->mode_info.mode_config_initialized)
2283 drm_crtc_force_disable_all(adev->ddev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002284 /* evict vram memory */
2285 amdgpu_bo_evict_vram(adev);
2286 amdgpu_ib_pool_fini(adev);
2287 amdgpu_fence_driver_fini(adev);
2288 amdgpu_fbdev_fini(adev);
2289 r = amdgpu_fini(adev);
Huang Ruiab4fe3e2017-06-05 22:11:59 +08002290 if (adev->firmware.gpu_info_fw) {
2291 release_firmware(adev->firmware.gpu_info_fw);
2292 adev->firmware.gpu_info_fw = NULL;
2293 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002294 adev->accel_working = false;
Shirish S2dc80b02017-05-25 10:05:25 +05302295 cancel_delayed_work_sync(&adev->late_init_work);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002296 /* free i2c buses */
2297 amdgpu_i2c_fini(adev);
2298 amdgpu_atombios_fini(adev);
2299 kfree(adev->bios);
2300 adev->bios = NULL;
Lukas Wunner84c8b222017-03-10 21:23:45 +01002301 if (!pci_is_thunderbolt_attached(adev->pdev))
2302 vga_switcheroo_unregister_client(adev->pdev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002303 if (adev->flags & AMD_IS_PX)
2304 vga_switcheroo_fini_domain_pm_ops(adev->dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002305 vga_client_register(adev->pdev, NULL, NULL, NULL);
2306 if (adev->rio_mem)
2307 pci_iounmap(adev->pdev, adev->rio_mem);
2308 adev->rio_mem = NULL;
2309 iounmap(adev->rmmio);
2310 adev->rmmio = NULL;
Ken Wangda69c1612016-01-21 19:08:55 +08002311 if (adev->asic_type >= CHIP_BONAIRE)
2312 amdgpu_doorbell_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002313 amdgpu_debugfs_regs_cleanup(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002314}
2315
2316
2317/*
2318 * Suspend & resume.
2319 */
2320/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002321 * amdgpu_device_suspend - initiate device suspend
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002322 *
2323 * @pdev: drm dev pointer
2324 * @state: suspend state
2325 *
2326 * Puts the hw in the suspend state (all asics).
2327 * Returns 0 for success or an error on failure.
2328 * Called at driver suspend.
2329 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002330int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002331{
2332 struct amdgpu_device *adev;
2333 struct drm_crtc *crtc;
2334 struct drm_connector *connector;
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002335 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002336
2337 if (dev == NULL || dev->dev_private == NULL) {
2338 return -ENODEV;
2339 }
2340
2341 adev = dev->dev_private;
2342
2343 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2344 return 0;
2345
2346 drm_kms_helper_poll_disable(dev);
2347
2348 /* turn off display hw */
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002349 drm_modeset_lock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002350 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2351 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
2352 }
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002353 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002354
Alex Deucher756e6882015-10-08 00:03:36 -04002355 /* unpin the front buffers and cursors */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002356 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Alex Deucher756e6882015-10-08 00:03:36 -04002357 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002358 struct amdgpu_framebuffer *rfb = to_amdgpu_framebuffer(crtc->primary->fb);
2359 struct amdgpu_bo *robj;
2360
Alex Deucher756e6882015-10-08 00:03:36 -04002361 if (amdgpu_crtc->cursor_bo) {
2362 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
Alex Xie7a6901d2017-04-24 13:52:41 -04002363 r = amdgpu_bo_reserve(aobj, true);
Alex Deucher756e6882015-10-08 00:03:36 -04002364 if (r == 0) {
2365 amdgpu_bo_unpin(aobj);
2366 amdgpu_bo_unreserve(aobj);
2367 }
2368 }
2369
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002370 if (rfb == NULL || rfb->obj == NULL) {
2371 continue;
2372 }
2373 robj = gem_to_amdgpu_bo(rfb->obj);
2374 /* don't unpin kernel fb objects */
2375 if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
Alex Xie7a6901d2017-04-24 13:52:41 -04002376 r = amdgpu_bo_reserve(robj, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002377 if (r == 0) {
2378 amdgpu_bo_unpin(robj);
2379 amdgpu_bo_unreserve(robj);
2380 }
2381 }
2382 }
2383 /* evict vram memory */
2384 amdgpu_bo_evict_vram(adev);
2385
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002386 amdgpu_fence_driver_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002387
2388 r = amdgpu_suspend(adev);
2389
Alex Deuchera0a71e42016-10-10 12:41:36 -04002390 /* evict remaining vram memory
2391 * This second call to evict vram is to evict the gart page table
2392 * using the CPU.
2393 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002394 amdgpu_bo_evict_vram(adev);
2395
Alex Deucherd05da0e2017-06-30 17:08:45 -04002396 amdgpu_atombios_scratch_regs_save(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002397 pci_save_state(dev->pdev);
2398 if (suspend) {
2399 /* Shut down the device */
2400 pci_disable_device(dev->pdev);
2401 pci_set_power_state(dev->pdev, PCI_D3hot);
jimqu74b0b152016-09-07 17:09:12 +08002402 } else {
2403 r = amdgpu_asic_reset(adev);
2404 if (r)
2405 DRM_ERROR("amdgpu asic reset failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002406 }
2407
2408 if (fbcon) {
2409 console_lock();
2410 amdgpu_fbdev_set_suspend(adev, 1);
2411 console_unlock();
2412 }
2413 return 0;
2414}
2415
2416/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002417 * amdgpu_device_resume - initiate device resume
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002418 *
2419 * @pdev: drm dev pointer
2420 *
2421 * Bring the hw back to operating state (all asics).
2422 * Returns 0 for success or an error on failure.
2423 * Called at driver resume.
2424 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002425int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002426{
2427 struct drm_connector *connector;
2428 struct amdgpu_device *adev = dev->dev_private;
Alex Deucher756e6882015-10-08 00:03:36 -04002429 struct drm_crtc *crtc;
Huang Rui03161a62017-04-13 16:12:26 +08002430 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002431
2432 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2433 return 0;
2434
jimqu74b0b152016-09-07 17:09:12 +08002435 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002436 console_lock();
jimqu74b0b152016-09-07 17:09:12 +08002437
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002438 if (resume) {
2439 pci_set_power_state(dev->pdev, PCI_D0);
2440 pci_restore_state(dev->pdev);
jimqu74b0b152016-09-07 17:09:12 +08002441 r = pci_enable_device(dev->pdev);
Huang Rui03161a62017-04-13 16:12:26 +08002442 if (r)
2443 goto unlock;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002444 }
Alex Deucherd05da0e2017-06-30 17:08:45 -04002445 amdgpu_atombios_scratch_regs_restore(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002446
2447 /* post card */
Jim Quc836fec2017-02-10 15:59:59 +08002448 if (amdgpu_need_post(adev)) {
jimqu74b0b152016-09-07 17:09:12 +08002449 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2450 if (r)
2451 DRM_ERROR("amdgpu asic init failed\n");
2452 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002453
2454 r = amdgpu_resume(adev);
Rex Zhue6707212017-03-30 13:21:01 +08002455 if (r) {
Flora Cuica198522016-02-04 15:10:08 +08002456 DRM_ERROR("amdgpu_resume failed (%d).\n", r);
Huang Rui03161a62017-04-13 16:12:26 +08002457 goto unlock;
Rex Zhue6707212017-03-30 13:21:01 +08002458 }
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002459 amdgpu_fence_driver_resume(adev);
2460
Flora Cuica198522016-02-04 15:10:08 +08002461 if (resume) {
2462 r = amdgpu_ib_ring_tests(adev);
2463 if (r)
2464 DRM_ERROR("ib ring test failed (%d).\n", r);
2465 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002466
2467 r = amdgpu_late_init(adev);
Huang Rui03161a62017-04-13 16:12:26 +08002468 if (r)
2469 goto unlock;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002470
Alex Deucher756e6882015-10-08 00:03:36 -04002471 /* pin cursors */
2472 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2473 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2474
2475 if (amdgpu_crtc->cursor_bo) {
2476 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
Alex Xie7a6901d2017-04-24 13:52:41 -04002477 r = amdgpu_bo_reserve(aobj, true);
Alex Deucher756e6882015-10-08 00:03:36 -04002478 if (r == 0) {
2479 r = amdgpu_bo_pin(aobj,
2480 AMDGPU_GEM_DOMAIN_VRAM,
2481 &amdgpu_crtc->cursor_addr);
2482 if (r != 0)
2483 DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
2484 amdgpu_bo_unreserve(aobj);
2485 }
2486 }
2487 }
2488
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002489 /* blat the mode back in */
2490 if (fbcon) {
2491 drm_helper_resume_force_mode(dev);
2492 /* turn on display hw */
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002493 drm_modeset_lock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002494 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2495 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
2496 }
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002497 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002498 }
2499
2500 drm_kms_helper_poll_enable(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002501
2502 /*
2503 * Most of the connector probing functions try to acquire runtime pm
2504 * refs to ensure that the GPU is powered on when connector polling is
2505 * performed. Since we're calling this from a runtime PM callback,
2506 * trying to acquire rpm refs will cause us to deadlock.
2507 *
2508 * Since we're guaranteed to be holding the rpm lock, it's safe to
2509 * temporarily disable the rpm helpers so this doesn't deadlock us.
2510 */
2511#ifdef CONFIG_PM
2512 dev->dev->power.disable_depth++;
2513#endif
Alex Deucher54fb2a52015-11-24 14:30:56 -05002514 drm_helper_hpd_irq_event(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002515#ifdef CONFIG_PM
2516 dev->dev->power.disable_depth--;
2517#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002518
Huang Rui03161a62017-04-13 16:12:26 +08002519 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002520 amdgpu_fbdev_set_suspend(adev, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002521
Huang Rui03161a62017-04-13 16:12:26 +08002522unlock:
2523 if (fbcon)
2524 console_unlock();
2525
2526 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002527}
2528
Chunming Zhou63fbf422016-07-15 11:19:20 +08002529static bool amdgpu_check_soft_reset(struct amdgpu_device *adev)
2530{
2531 int i;
2532 bool asic_hang = false;
2533
2534 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002535 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou63fbf422016-07-15 11:19:20 +08002536 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002537 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
2538 adev->ip_blocks[i].status.hang =
2539 adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
2540 if (adev->ip_blocks[i].status.hang) {
2541 DRM_INFO("IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
Chunming Zhou63fbf422016-07-15 11:19:20 +08002542 asic_hang = true;
2543 }
2544 }
2545 return asic_hang;
2546}
2547
Baoyou Xie4d446652016-09-18 22:09:35 +08002548static int amdgpu_pre_soft_reset(struct amdgpu_device *adev)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002549{
2550 int i, r = 0;
2551
2552 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002553 if (!adev->ip_blocks[i].status.valid)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002554 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002555 if (adev->ip_blocks[i].status.hang &&
2556 adev->ip_blocks[i].version->funcs->pre_soft_reset) {
2557 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
Chunming Zhoud31a5012016-07-18 10:04:34 +08002558 if (r)
2559 return r;
2560 }
2561 }
2562
2563 return 0;
2564}
2565
Chunming Zhou35d782f2016-07-15 15:57:13 +08002566static bool amdgpu_need_full_reset(struct amdgpu_device *adev)
2567{
Alex Deucherda146d32016-10-13 16:07:03 -04002568 int i;
2569
2570 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002571 if (!adev->ip_blocks[i].status.valid)
Alex Deucherda146d32016-10-13 16:07:03 -04002572 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002573 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
2574 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
2575 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
2576 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE)) {
2577 if (adev->ip_blocks[i].status.hang) {
Alex Deucherda146d32016-10-13 16:07:03 -04002578 DRM_INFO("Some block need full reset!\n");
2579 return true;
2580 }
2581 }
Chunming Zhou35d782f2016-07-15 15:57:13 +08002582 }
2583 return false;
2584}
2585
2586static int amdgpu_soft_reset(struct amdgpu_device *adev)
2587{
2588 int i, r = 0;
2589
2590 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002591 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002592 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002593 if (adev->ip_blocks[i].status.hang &&
2594 adev->ip_blocks[i].version->funcs->soft_reset) {
2595 r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002596 if (r)
2597 return r;
2598 }
2599 }
2600
2601 return 0;
2602}
2603
2604static int amdgpu_post_soft_reset(struct amdgpu_device *adev)
2605{
2606 int i, r = 0;
2607
2608 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002609 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002610 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002611 if (adev->ip_blocks[i].status.hang &&
2612 adev->ip_blocks[i].version->funcs->post_soft_reset)
2613 r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002614 if (r)
2615 return r;
2616 }
2617
2618 return 0;
2619}
2620
Chunming Zhou3ad81f12016-08-05 17:30:17 +08002621bool amdgpu_need_backup(struct amdgpu_device *adev)
2622{
2623 if (adev->flags & AMD_IS_APU)
2624 return false;
2625
2626 return amdgpu_lockup_timeout > 0 ? true : false;
2627}
2628
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002629static int amdgpu_recover_vram_from_shadow(struct amdgpu_device *adev,
2630 struct amdgpu_ring *ring,
2631 struct amdgpu_bo *bo,
Chris Wilsonf54d1862016-10-25 13:00:45 +01002632 struct dma_fence **fence)
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002633{
2634 uint32_t domain;
2635 int r;
2636
Roger.He23d2e502017-04-21 14:24:26 +08002637 if (!bo->shadow)
2638 return 0;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002639
Alex Xie1d284792017-04-24 13:53:04 -04002640 r = amdgpu_bo_reserve(bo, true);
Roger.He23d2e502017-04-21 14:24:26 +08002641 if (r)
2642 return r;
2643 domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
2644 /* if bo has been evicted, then no need to recover */
2645 if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
Roger.He82521312017-04-21 13:08:43 +08002646 r = amdgpu_bo_validate(bo->shadow);
2647 if (r) {
2648 DRM_ERROR("bo validate failed!\n");
2649 goto err;
2650 }
2651
2652 r = amdgpu_ttm_bind(&bo->shadow->tbo, &bo->shadow->tbo.mem);
2653 if (r) {
2654 DRM_ERROR("%p bind failed\n", bo->shadow);
2655 goto err;
2656 }
2657
Roger.He23d2e502017-04-21 14:24:26 +08002658 r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002659 NULL, fence, true);
Roger.He23d2e502017-04-21 14:24:26 +08002660 if (r) {
2661 DRM_ERROR("recover page table failed!\n");
2662 goto err;
2663 }
2664 }
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002665err:
Roger.He23d2e502017-04-21 14:24:26 +08002666 amdgpu_bo_unreserve(bo);
2667 return r;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002668}
2669
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002670/**
Monk Liua90ad3c2017-01-23 14:22:08 +08002671 * amdgpu_sriov_gpu_reset - reset the asic
2672 *
2673 * @adev: amdgpu device pointer
Monk Liu7225f872017-04-26 14:51:54 +08002674 * @job: which job trigger hang
Monk Liua90ad3c2017-01-23 14:22:08 +08002675 *
2676 * Attempt the reset the GPU if it has hung (all asics).
2677 * for SRIOV case.
2678 * Returns 0 for success or an error on failure.
2679 */
Monk Liu7225f872017-04-26 14:51:54 +08002680int amdgpu_sriov_gpu_reset(struct amdgpu_device *adev, struct amdgpu_job *job)
Monk Liua90ad3c2017-01-23 14:22:08 +08002681{
Monk Liu65781c72017-05-11 13:36:44 +08002682 int i, j, r = 0;
Monk Liua90ad3c2017-01-23 14:22:08 +08002683 int resched;
2684 struct amdgpu_bo *bo, *tmp;
2685 struct amdgpu_ring *ring;
2686 struct dma_fence *fence = NULL, *next = NULL;
2687
Monk Liu147b5982017-01-25 15:48:01 +08002688 mutex_lock(&adev->virt.lock_reset);
Monk Liua90ad3c2017-01-23 14:22:08 +08002689 atomic_inc(&adev->gpu_reset_counter);
Monk Liu1fb37a32017-01-26 15:36:37 +08002690 adev->gfx.in_reset = true;
Monk Liua90ad3c2017-01-23 14:22:08 +08002691
2692 /* block TTM */
2693 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
2694
Monk Liu65781c72017-05-11 13:36:44 +08002695 /* we start from the ring trigger GPU hang */
2696 j = job ? job->ring->idx : 0;
Monk Liua90ad3c2017-01-23 14:22:08 +08002697
Monk Liu65781c72017-05-11 13:36:44 +08002698 /* block scheduler */
2699 for (i = j; i < j + AMDGPU_MAX_RINGS; ++i) {
2700 ring = adev->rings[i % AMDGPU_MAX_RINGS];
Monk Liua90ad3c2017-01-23 14:22:08 +08002701 if (!ring || !ring->sched.thread)
2702 continue;
2703
2704 kthread_park(ring->sched.thread);
Monk Liua90ad3c2017-01-23 14:22:08 +08002705
Monk Liu65781c72017-05-11 13:36:44 +08002706 if (job && j != i)
2707 continue;
2708
Monk Liu4f059ec2017-05-11 13:59:15 +08002709 /* here give the last chance to check if job removed from mirror-list
Monk Liu65781c72017-05-11 13:36:44 +08002710 * since we already pay some time on kthread_park */
Monk Liu4f059ec2017-05-11 13:59:15 +08002711 if (job && list_empty(&job->base.node)) {
Monk Liu65781c72017-05-11 13:36:44 +08002712 kthread_unpark(ring->sched.thread);
2713 goto give_up_reset;
2714 }
2715
2716 if (amd_sched_invalidate_job(&job->base, amdgpu_job_hang_limit))
2717 amd_sched_job_kickout(&job->base);
2718
2719 /* only do job_reset on the hang ring if @job not NULL */
2720 amd_sched_hw_job_reset(&ring->sched);
2721
2722 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
2723 amdgpu_fence_driver_force_completion_ring(ring);
2724 }
Monk Liua90ad3c2017-01-23 14:22:08 +08002725
2726 /* request to take full control of GPU before re-initialization */
Monk Liu7225f872017-04-26 14:51:54 +08002727 if (job)
Monk Liua90ad3c2017-01-23 14:22:08 +08002728 amdgpu_virt_reset_gpu(adev);
2729 else
2730 amdgpu_virt_request_full_gpu(adev, true);
2731
2732
2733 /* Resume IP prior to SMC */
Monk Liue4f0fdc2017-02-09 11:55:49 +08002734 amdgpu_sriov_reinit_early(adev);
Monk Liua90ad3c2017-01-23 14:22:08 +08002735
2736 /* we need recover gart prior to run SMC/CP/SDMA resume */
2737 amdgpu_ttm_recover_gart(adev);
2738
2739 /* now we are okay to resume SMC/CP/SDMA */
Monk Liue4f0fdc2017-02-09 11:55:49 +08002740 amdgpu_sriov_reinit_late(adev);
Monk Liua90ad3c2017-01-23 14:22:08 +08002741
2742 amdgpu_irq_gpu_reset_resume_helper(adev);
2743
2744 if (amdgpu_ib_ring_tests(adev))
2745 dev_err(adev->dev, "[GPU_RESET] ib ring test failed (%d).\n", r);
2746
2747 /* release full control of GPU after ib test */
2748 amdgpu_virt_release_full_gpu(adev, true);
2749
2750 DRM_INFO("recover vram bo from shadow\n");
2751
2752 ring = adev->mman.buffer_funcs_ring;
2753 mutex_lock(&adev->shadow_list_lock);
2754 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
Monk Liu236763d2017-05-01 16:15:31 +08002755 next = NULL;
Monk Liua90ad3c2017-01-23 14:22:08 +08002756 amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
2757 if (fence) {
2758 r = dma_fence_wait(fence, false);
2759 if (r) {
2760 WARN(r, "recovery from shadow isn't completed\n");
2761 break;
2762 }
2763 }
2764
2765 dma_fence_put(fence);
2766 fence = next;
2767 }
2768 mutex_unlock(&adev->shadow_list_lock);
2769
2770 if (fence) {
2771 r = dma_fence_wait(fence, false);
2772 if (r)
2773 WARN(r, "recovery from shadow isn't completed\n");
2774 }
2775 dma_fence_put(fence);
2776
Monk Liu65781c72017-05-11 13:36:44 +08002777 for (i = j; i < j + AMDGPU_MAX_RINGS; ++i) {
2778 ring = adev->rings[i % AMDGPU_MAX_RINGS];
Monk Liua90ad3c2017-01-23 14:22:08 +08002779 if (!ring || !ring->sched.thread)
2780 continue;
2781
Monk Liu65781c72017-05-11 13:36:44 +08002782 if (job && j != i) {
2783 kthread_unpark(ring->sched.thread);
2784 continue;
2785 }
2786
Monk Liua90ad3c2017-01-23 14:22:08 +08002787 amd_sched_job_recovery(&ring->sched);
2788 kthread_unpark(ring->sched.thread);
2789 }
2790
2791 drm_helper_resume_force_mode(adev->ddev);
Monk Liu65781c72017-05-11 13:36:44 +08002792give_up_reset:
Monk Liua90ad3c2017-01-23 14:22:08 +08002793 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
2794 if (r) {
2795 /* bad news, how to tell it to userspace ? */
2796 dev_info(adev->dev, "GPU reset failed\n");
Monk Liu65781c72017-05-11 13:36:44 +08002797 } else {
2798 dev_info(adev->dev, "GPU reset successed!\n");
Monk Liua90ad3c2017-01-23 14:22:08 +08002799 }
2800
Monk Liu1fb37a32017-01-26 15:36:37 +08002801 adev->gfx.in_reset = false;
Monk Liu147b5982017-01-25 15:48:01 +08002802 mutex_unlock(&adev->virt.lock_reset);
Monk Liua90ad3c2017-01-23 14:22:08 +08002803 return r;
2804}
2805
2806/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002807 * amdgpu_gpu_reset - reset the asic
2808 *
2809 * @adev: amdgpu device pointer
2810 *
2811 * Attempt the reset the GPU if it has hung (all asics).
2812 * Returns 0 for success or an error on failure.
2813 */
2814int amdgpu_gpu_reset(struct amdgpu_device *adev)
2815{
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002816 int i, r;
2817 int resched;
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002818 bool need_full_reset, vram_lost = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002819
Chunming Zhou63fbf422016-07-15 11:19:20 +08002820 if (!amdgpu_check_soft_reset(adev)) {
2821 DRM_INFO("No hardware hang detected. Did some blocks stall?\n");
2822 return 0;
2823 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002824
Marek Olšákd94aed52015-05-05 21:13:49 +02002825 atomic_inc(&adev->gpu_reset_counter);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002826
Chunming Zhoua3c47d62016-06-30 16:44:41 +08002827 /* block TTM */
2828 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
2829
Chunming Zhou0875dc92016-06-12 15:41:58 +08002830 /* block scheduler */
2831 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2832 struct amdgpu_ring *ring = adev->rings[i];
2833
Chunming Zhou51687752017-04-24 17:09:15 +08002834 if (!ring || !ring->sched.thread)
Chunming Zhou0875dc92016-06-12 15:41:58 +08002835 continue;
2836 kthread_park(ring->sched.thread);
Chunming Zhouaa1c8902016-06-30 13:56:02 +08002837 amd_sched_hw_job_reset(&ring->sched);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002838 }
Chunming Zhou2200eda2016-06-30 16:53:02 +08002839 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
2840 amdgpu_fence_driver_force_completion(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002841
Chunming Zhou35d782f2016-07-15 15:57:13 +08002842 need_full_reset = amdgpu_need_full_reset(adev);
2843
2844 if (!need_full_reset) {
2845 amdgpu_pre_soft_reset(adev);
2846 r = amdgpu_soft_reset(adev);
2847 amdgpu_post_soft_reset(adev);
2848 if (r || amdgpu_check_soft_reset(adev)) {
2849 DRM_INFO("soft reset failed, will fallback to full reset!\n");
2850 need_full_reset = true;
2851 }
2852 }
2853
2854 if (need_full_reset) {
Chunming Zhou35d782f2016-07-15 15:57:13 +08002855 r = amdgpu_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002856
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002857retry:
Alex Deucherd05da0e2017-06-30 17:08:45 -04002858 amdgpu_atombios_scratch_regs_save(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002859 r = amdgpu_asic_reset(adev);
Alex Deucherd05da0e2017-06-30 17:08:45 -04002860 amdgpu_atombios_scratch_regs_restore(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002861 /* post card */
2862 amdgpu_atom_asic_init(adev->mode_info.atom_context);
Alex Deucherbfa99262016-01-15 11:59:48 -05002863
Chunming Zhou35d782f2016-07-15 15:57:13 +08002864 if (!r) {
2865 dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
Chunming Zhoufcf06492017-05-05 10:33:33 +08002866 r = amdgpu_resume_phase1(adev);
2867 if (r)
2868 goto out;
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002869 vram_lost = amdgpu_check_vram_lost(adev);
Chunming Zhouf1892132017-05-15 16:48:27 +08002870 if (vram_lost) {
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002871 DRM_ERROR("VRAM is lost!\n");
Chunming Zhouf1892132017-05-15 16:48:27 +08002872 atomic_inc(&adev->vram_lost_counter);
2873 }
Chunming Zhou2c0d7312016-08-30 16:36:25 +08002874 r = amdgpu_ttm_recover_gart(adev);
2875 if (r)
Chunming Zhoufcf06492017-05-05 10:33:33 +08002876 goto out;
2877 r = amdgpu_resume_phase2(adev);
2878 if (r)
2879 goto out;
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002880 if (vram_lost)
2881 amdgpu_fill_reset_magic(adev);
Chunming Zhou2c0d7312016-08-30 16:36:25 +08002882 }
Chunming Zhoufcf06492017-05-05 10:33:33 +08002883 }
2884out:
2885 if (!r) {
2886 amdgpu_irq_gpu_reset_resume_helper(adev);
Chunming Zhou1f465082016-06-30 15:02:26 +08002887 r = amdgpu_ib_ring_tests(adev);
2888 if (r) {
2889 dev_err(adev->dev, "ib ring test failed (%d).\n", r);
Chunming Zhou40019dc2016-06-29 16:01:49 +08002890 r = amdgpu_suspend(adev);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002891 need_full_reset = true;
Chunming Zhou40019dc2016-06-29 16:01:49 +08002892 goto retry;
Chunming Zhou1f465082016-06-30 15:02:26 +08002893 }
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002894 /**
2895 * recovery vm page tables, since we cannot depend on VRAM is
2896 * consistent after gpu full reset.
2897 */
2898 if (need_full_reset && amdgpu_need_backup(adev)) {
2899 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
2900 struct amdgpu_bo *bo, *tmp;
Chris Wilsonf54d1862016-10-25 13:00:45 +01002901 struct dma_fence *fence = NULL, *next = NULL;
Chunming Zhou1f465082016-06-30 15:02:26 +08002902
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002903 DRM_INFO("recover vram bo from shadow\n");
2904 mutex_lock(&adev->shadow_list_lock);
2905 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
Monk Liu236763d2017-05-01 16:15:31 +08002906 next = NULL;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002907 amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
2908 if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01002909 r = dma_fence_wait(fence, false);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002910 if (r) {
Monk Liu1d7b17b2017-01-22 18:52:56 +08002911 WARN(r, "recovery from shadow isn't completed\n");
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002912 break;
2913 }
2914 }
2915
Chris Wilsonf54d1862016-10-25 13:00:45 +01002916 dma_fence_put(fence);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002917 fence = next;
2918 }
2919 mutex_unlock(&adev->shadow_list_lock);
2920 if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01002921 r = dma_fence_wait(fence, false);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002922 if (r)
Monk Liu1d7b17b2017-01-22 18:52:56 +08002923 WARN(r, "recovery from shadow isn't completed\n");
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002924 }
Chris Wilsonf54d1862016-10-25 13:00:45 +01002925 dma_fence_put(fence);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002926 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002927 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2928 struct amdgpu_ring *ring = adev->rings[i];
Chunming Zhou51687752017-04-24 17:09:15 +08002929
2930 if (!ring || !ring->sched.thread)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002931 continue;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002932
Chunming Zhouaa1c8902016-06-30 13:56:02 +08002933 amd_sched_job_recovery(&ring->sched);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002934 kthread_unpark(ring->sched.thread);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002935 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002936 } else {
Chunming Zhou2200eda2016-06-30 16:53:02 +08002937 dev_err(adev->dev, "asic resume failed (%d).\n", r);
Gavin Wan89041942017-06-23 13:55:15 -04002938 amdgpu_vf_error_put(AMDGIM_ERROR_VF_ASIC_RESUME_FAIL, 0, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002939 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
Chunming Zhou51687752017-04-24 17:09:15 +08002940 if (adev->rings[i] && adev->rings[i]->sched.thread) {
Chunming Zhou0875dc92016-06-12 15:41:58 +08002941 kthread_unpark(adev->rings[i]->sched.thread);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002942 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002943 }
2944 }
2945
2946 drm_helper_resume_force_mode(adev->ddev);
2947
2948 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
Gavin Wan89041942017-06-23 13:55:15 -04002949 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002950 /* bad news, how to tell it to userspace ? */
2951 dev_info(adev->dev, "GPU reset failed\n");
Gavin Wan89041942017-06-23 13:55:15 -04002952 amdgpu_vf_error_put(AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, r);
2953 }
2954 else {
Chunming Zhou6643be62017-05-05 10:50:09 +08002955 dev_info(adev->dev, "GPU reset successed!\n");
Gavin Wan89041942017-06-23 13:55:15 -04002956 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002957
Gavin Wan89041942017-06-23 13:55:15 -04002958 amdgpu_vf_error_trans_all(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002959 return r;
2960}
2961
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002962void amdgpu_get_pcie_info(struct amdgpu_device *adev)
2963{
2964 u32 mask;
2965 int ret;
2966
Alex Deuchercd474ba2016-02-04 10:21:23 -05002967 if (amdgpu_pcie_gen_cap)
2968 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
2969
2970 if (amdgpu_pcie_lane_cap)
2971 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
2972
2973 /* covers APUs as well */
2974 if (pci_is_root_bus(adev->pdev->bus)) {
2975 if (adev->pm.pcie_gen_mask == 0)
2976 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
2977 if (adev->pm.pcie_mlw_mask == 0)
2978 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002979 return;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002980 }
Alex Deuchercd474ba2016-02-04 10:21:23 -05002981
2982 if (adev->pm.pcie_gen_mask == 0) {
2983 ret = drm_pcie_get_speed_cap_mask(adev->ddev, &mask);
2984 if (!ret) {
2985 adev->pm.pcie_gen_mask = (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
2986 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
2987 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
2988
2989 if (mask & DRM_PCIE_SPEED_25)
2990 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
2991 if (mask & DRM_PCIE_SPEED_50)
2992 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2;
2993 if (mask & DRM_PCIE_SPEED_80)
2994 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3;
2995 } else {
2996 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
2997 }
2998 }
2999 if (adev->pm.pcie_mlw_mask == 0) {
3000 ret = drm_pcie_get_max_link_width(adev->ddev, &mask);
3001 if (!ret) {
3002 switch (mask) {
3003 case 32:
3004 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
3005 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
3006 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3007 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3008 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3009 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3010 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3011 break;
3012 case 16:
3013 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
3014 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3015 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3016 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3017 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3018 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3019 break;
3020 case 12:
3021 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3022 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3023 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3024 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3025 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3026 break;
3027 case 8:
3028 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3029 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3030 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3031 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3032 break;
3033 case 4:
3034 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3035 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3036 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3037 break;
3038 case 2:
3039 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3040 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3041 break;
3042 case 1:
3043 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
3044 break;
3045 default:
3046 break;
3047 }
3048 } else {
3049 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003050 }
3051 }
3052}
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003053
3054/*
3055 * Debugfs
3056 */
3057int amdgpu_debugfs_add_files(struct amdgpu_device *adev,
Nils Wallménius06ab6832016-05-02 12:46:15 -04003058 const struct drm_info_list *files,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003059 unsigned nfiles)
3060{
3061 unsigned i;
3062
3063 for (i = 0; i < adev->debugfs_count; i++) {
3064 if (adev->debugfs[i].files == files) {
3065 /* Already registered */
3066 return 0;
3067 }
3068 }
3069
3070 i = adev->debugfs_count + 1;
3071 if (i > AMDGPU_DEBUGFS_MAX_COMPONENTS) {
3072 DRM_ERROR("Reached maximum number of debugfs components.\n");
3073 DRM_ERROR("Report so we increase "
3074 "AMDGPU_DEBUGFS_MAX_COMPONENTS.\n");
3075 return -EINVAL;
3076 }
3077 adev->debugfs[adev->debugfs_count].files = files;
3078 adev->debugfs[adev->debugfs_count].num_files = nfiles;
3079 adev->debugfs_count = i;
3080#if defined(CONFIG_DEBUG_FS)
3081 drm_debugfs_create_files(files, nfiles,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003082 adev->ddev->primary->debugfs_root,
3083 adev->ddev->primary);
3084#endif
3085 return 0;
3086}
3087
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003088#if defined(CONFIG_DEBUG_FS)
3089
3090static ssize_t amdgpu_debugfs_regs_read(struct file *f, char __user *buf,
3091 size_t size, loff_t *pos)
3092{
Al Viro45063092016-12-04 18:24:56 -05003093 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003094 ssize_t result = 0;
3095 int r;
Tom St Denisbd122672016-07-28 09:39:22 -04003096 bool pm_pg_lock, use_bank;
Tom St Denis566281592016-06-27 11:55:07 -04003097 unsigned instance_bank, sh_bank, se_bank;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003098
3099 if (size & 0x3 || *pos & 0x3)
3100 return -EINVAL;
3101
Tom St Denisbd122672016-07-28 09:39:22 -04003102 /* are we reading registers for which a PG lock is necessary? */
3103 pm_pg_lock = (*pos >> 23) & 1;
3104
Tom St Denis566281592016-06-27 11:55:07 -04003105 if (*pos & (1ULL << 62)) {
3106 se_bank = (*pos >> 24) & 0x3FF;
3107 sh_bank = (*pos >> 34) & 0x3FF;
3108 instance_bank = (*pos >> 44) & 0x3FF;
Tom St Denis32977f92016-10-09 07:41:26 -04003109
3110 if (se_bank == 0x3FF)
3111 se_bank = 0xFFFFFFFF;
3112 if (sh_bank == 0x3FF)
3113 sh_bank = 0xFFFFFFFF;
3114 if (instance_bank == 0x3FF)
3115 instance_bank = 0xFFFFFFFF;
Tom St Denis566281592016-06-27 11:55:07 -04003116 use_bank = 1;
Tom St Denis566281592016-06-27 11:55:07 -04003117 } else {
3118 use_bank = 0;
3119 }
3120
Tom St Denis801a6aa9a62017-03-15 05:34:25 -04003121 *pos &= (1UL << 22) - 1;
Tom St Denisbd122672016-07-28 09:39:22 -04003122
Tom St Denis566281592016-06-27 11:55:07 -04003123 if (use_bank) {
Tom St Denis32977f92016-10-09 07:41:26 -04003124 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
3125 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
Tom St Denis566281592016-06-27 11:55:07 -04003126 return -EINVAL;
3127 mutex_lock(&adev->grbm_idx_mutex);
3128 amdgpu_gfx_select_se_sh(adev, se_bank,
3129 sh_bank, instance_bank);
3130 }
3131
Tom St Denisbd122672016-07-28 09:39:22 -04003132 if (pm_pg_lock)
3133 mutex_lock(&adev->pm.mutex);
3134
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003135 while (size) {
3136 uint32_t value;
3137
3138 if (*pos > adev->rmmio_size)
Tom St Denis566281592016-06-27 11:55:07 -04003139 goto end;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003140
3141 value = RREG32(*pos >> 2);
3142 r = put_user(value, (uint32_t *)buf);
Tom St Denis566281592016-06-27 11:55:07 -04003143 if (r) {
3144 result = r;
3145 goto end;
3146 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003147
3148 result += 4;
3149 buf += 4;
3150 *pos += 4;
3151 size -= 4;
3152 }
3153
Tom St Denis566281592016-06-27 11:55:07 -04003154end:
3155 if (use_bank) {
3156 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
3157 mutex_unlock(&adev->grbm_idx_mutex);
3158 }
3159
Tom St Denisbd122672016-07-28 09:39:22 -04003160 if (pm_pg_lock)
3161 mutex_unlock(&adev->pm.mutex);
3162
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003163 return result;
3164}
3165
3166static ssize_t amdgpu_debugfs_regs_write(struct file *f, const char __user *buf,
3167 size_t size, loff_t *pos)
3168{
Al Viro45063092016-12-04 18:24:56 -05003169 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003170 ssize_t result = 0;
3171 int r;
Tom St Denis394fdde2016-10-10 07:31:23 -04003172 bool pm_pg_lock, use_bank;
3173 unsigned instance_bank, sh_bank, se_bank;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003174
3175 if (size & 0x3 || *pos & 0x3)
3176 return -EINVAL;
3177
Tom St Denis394fdde2016-10-10 07:31:23 -04003178 /* are we reading registers for which a PG lock is necessary? */
3179 pm_pg_lock = (*pos >> 23) & 1;
3180
3181 if (*pos & (1ULL << 62)) {
3182 se_bank = (*pos >> 24) & 0x3FF;
3183 sh_bank = (*pos >> 34) & 0x3FF;
3184 instance_bank = (*pos >> 44) & 0x3FF;
3185
3186 if (se_bank == 0x3FF)
3187 se_bank = 0xFFFFFFFF;
3188 if (sh_bank == 0x3FF)
3189 sh_bank = 0xFFFFFFFF;
3190 if (instance_bank == 0x3FF)
3191 instance_bank = 0xFFFFFFFF;
3192 use_bank = 1;
3193 } else {
3194 use_bank = 0;
3195 }
3196
Tom St Denis801a6aa9a62017-03-15 05:34:25 -04003197 *pos &= (1UL << 22) - 1;
Tom St Denis394fdde2016-10-10 07:31:23 -04003198
3199 if (use_bank) {
3200 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
3201 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
3202 return -EINVAL;
3203 mutex_lock(&adev->grbm_idx_mutex);
3204 amdgpu_gfx_select_se_sh(adev, se_bank,
3205 sh_bank, instance_bank);
3206 }
3207
3208 if (pm_pg_lock)
3209 mutex_lock(&adev->pm.mutex);
3210
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003211 while (size) {
3212 uint32_t value;
3213
3214 if (*pos > adev->rmmio_size)
3215 return result;
3216
3217 r = get_user(value, (uint32_t *)buf);
3218 if (r)
3219 return r;
3220
3221 WREG32(*pos >> 2, value);
3222
3223 result += 4;
3224 buf += 4;
3225 *pos += 4;
3226 size -= 4;
3227 }
3228
Tom St Denis394fdde2016-10-10 07:31:23 -04003229 if (use_bank) {
3230 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
3231 mutex_unlock(&adev->grbm_idx_mutex);
3232 }
3233
3234 if (pm_pg_lock)
3235 mutex_unlock(&adev->pm.mutex);
3236
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003237 return result;
3238}
3239
Tom St Denisadcec282016-04-15 13:08:44 -04003240static ssize_t amdgpu_debugfs_regs_pcie_read(struct file *f, char __user *buf,
3241 size_t size, loff_t *pos)
3242{
Al Viro45063092016-12-04 18:24:56 -05003243 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003244 ssize_t result = 0;
3245 int r;
3246
3247 if (size & 0x3 || *pos & 0x3)
3248 return -EINVAL;
3249
3250 while (size) {
3251 uint32_t value;
3252
3253 value = RREG32_PCIE(*pos >> 2);
3254 r = put_user(value, (uint32_t *)buf);
3255 if (r)
3256 return r;
3257
3258 result += 4;
3259 buf += 4;
3260 *pos += 4;
3261 size -= 4;
3262 }
3263
3264 return result;
3265}
3266
3267static ssize_t amdgpu_debugfs_regs_pcie_write(struct file *f, const char __user *buf,
3268 size_t size, loff_t *pos)
3269{
Al Viro45063092016-12-04 18:24:56 -05003270 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003271 ssize_t result = 0;
3272 int r;
3273
3274 if (size & 0x3 || *pos & 0x3)
3275 return -EINVAL;
3276
3277 while (size) {
3278 uint32_t value;
3279
3280 r = get_user(value, (uint32_t *)buf);
3281 if (r)
3282 return r;
3283
3284 WREG32_PCIE(*pos >> 2, value);
3285
3286 result += 4;
3287 buf += 4;
3288 *pos += 4;
3289 size -= 4;
3290 }
3291
3292 return result;
3293}
3294
3295static ssize_t amdgpu_debugfs_regs_didt_read(struct file *f, char __user *buf,
3296 size_t size, loff_t *pos)
3297{
Al Viro45063092016-12-04 18:24:56 -05003298 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003299 ssize_t result = 0;
3300 int r;
3301
3302 if (size & 0x3 || *pos & 0x3)
3303 return -EINVAL;
3304
3305 while (size) {
3306 uint32_t value;
3307
3308 value = RREG32_DIDT(*pos >> 2);
3309 r = put_user(value, (uint32_t *)buf);
3310 if (r)
3311 return r;
3312
3313 result += 4;
3314 buf += 4;
3315 *pos += 4;
3316 size -= 4;
3317 }
3318
3319 return result;
3320}
3321
3322static ssize_t amdgpu_debugfs_regs_didt_write(struct file *f, const char __user *buf,
3323 size_t size, loff_t *pos)
3324{
Al Viro45063092016-12-04 18:24:56 -05003325 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003326 ssize_t result = 0;
3327 int r;
3328
3329 if (size & 0x3 || *pos & 0x3)
3330 return -EINVAL;
3331
3332 while (size) {
3333 uint32_t value;
3334
3335 r = get_user(value, (uint32_t *)buf);
3336 if (r)
3337 return r;
3338
3339 WREG32_DIDT(*pos >> 2, value);
3340
3341 result += 4;
3342 buf += 4;
3343 *pos += 4;
3344 size -= 4;
3345 }
3346
3347 return result;
3348}
3349
3350static ssize_t amdgpu_debugfs_regs_smc_read(struct file *f, char __user *buf,
3351 size_t size, loff_t *pos)
3352{
Al Viro45063092016-12-04 18:24:56 -05003353 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003354 ssize_t result = 0;
3355 int r;
3356
3357 if (size & 0x3 || *pos & 0x3)
3358 return -EINVAL;
3359
3360 while (size) {
3361 uint32_t value;
3362
Tom St Denis6fc0dea2016-08-29 08:39:29 -04003363 value = RREG32_SMC(*pos);
Tom St Denisadcec282016-04-15 13:08:44 -04003364 r = put_user(value, (uint32_t *)buf);
3365 if (r)
3366 return r;
3367
3368 result += 4;
3369 buf += 4;
3370 *pos += 4;
3371 size -= 4;
3372 }
3373
3374 return result;
3375}
3376
3377static ssize_t amdgpu_debugfs_regs_smc_write(struct file *f, const char __user *buf,
3378 size_t size, loff_t *pos)
3379{
Al Viro45063092016-12-04 18:24:56 -05003380 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003381 ssize_t result = 0;
3382 int r;
3383
3384 if (size & 0x3 || *pos & 0x3)
3385 return -EINVAL;
3386
3387 while (size) {
3388 uint32_t value;
3389
3390 r = get_user(value, (uint32_t *)buf);
3391 if (r)
3392 return r;
3393
Tom St Denis6fc0dea2016-08-29 08:39:29 -04003394 WREG32_SMC(*pos, value);
Tom St Denisadcec282016-04-15 13:08:44 -04003395
3396 result += 4;
3397 buf += 4;
3398 *pos += 4;
3399 size -= 4;
3400 }
3401
3402 return result;
3403}
3404
Tom St Denis1e051412016-06-27 09:57:18 -04003405static ssize_t amdgpu_debugfs_gca_config_read(struct file *f, char __user *buf,
3406 size_t size, loff_t *pos)
3407{
Al Viro45063092016-12-04 18:24:56 -05003408 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denis1e051412016-06-27 09:57:18 -04003409 ssize_t result = 0;
3410 int r;
3411 uint32_t *config, no_regs = 0;
3412
3413 if (size & 0x3 || *pos & 0x3)
3414 return -EINVAL;
3415
Markus Elfringecab7662016-09-18 17:00:52 +02003416 config = kmalloc_array(256, sizeof(*config), GFP_KERNEL);
Tom St Denis1e051412016-06-27 09:57:18 -04003417 if (!config)
3418 return -ENOMEM;
3419
3420 /* version, increment each time something is added */
Tom St Denis9a999352017-01-18 13:01:25 -05003421 config[no_regs++] = 3;
Tom St Denis1e051412016-06-27 09:57:18 -04003422 config[no_regs++] = adev->gfx.config.max_shader_engines;
3423 config[no_regs++] = adev->gfx.config.max_tile_pipes;
3424 config[no_regs++] = adev->gfx.config.max_cu_per_sh;
3425 config[no_regs++] = adev->gfx.config.max_sh_per_se;
3426 config[no_regs++] = adev->gfx.config.max_backends_per_se;
3427 config[no_regs++] = adev->gfx.config.max_texture_channel_caches;
3428 config[no_regs++] = adev->gfx.config.max_gprs;
3429 config[no_regs++] = adev->gfx.config.max_gs_threads;
3430 config[no_regs++] = adev->gfx.config.max_hw_contexts;
3431 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_frontend;
3432 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_backend;
3433 config[no_regs++] = adev->gfx.config.sc_hiz_tile_fifo_size;
3434 config[no_regs++] = adev->gfx.config.sc_earlyz_tile_fifo_size;
3435 config[no_regs++] = adev->gfx.config.num_tile_pipes;
3436 config[no_regs++] = adev->gfx.config.backend_enable_mask;
3437 config[no_regs++] = adev->gfx.config.mem_max_burst_length_bytes;
3438 config[no_regs++] = adev->gfx.config.mem_row_size_in_kb;
3439 config[no_regs++] = adev->gfx.config.shader_engine_tile_size;
3440 config[no_regs++] = adev->gfx.config.num_gpus;
3441 config[no_regs++] = adev->gfx.config.multi_gpu_tile_size;
3442 config[no_regs++] = adev->gfx.config.mc_arb_ramcfg;
3443 config[no_regs++] = adev->gfx.config.gb_addr_config;
3444 config[no_regs++] = adev->gfx.config.num_rbs;
3445
Tom St Denis89a8f302016-08-12 15:14:31 -04003446 /* rev==1 */
3447 config[no_regs++] = adev->rev_id;
3448 config[no_regs++] = adev->pg_flags;
3449 config[no_regs++] = adev->cg_flags;
3450
Tom St Denise9f11dc2016-08-17 12:00:51 -04003451 /* rev==2 */
3452 config[no_regs++] = adev->family;
3453 config[no_regs++] = adev->external_rev_id;
3454
Tom St Denis9a999352017-01-18 13:01:25 -05003455 /* rev==3 */
3456 config[no_regs++] = adev->pdev->device;
3457 config[no_regs++] = adev->pdev->revision;
3458 config[no_regs++] = adev->pdev->subsystem_device;
3459 config[no_regs++] = adev->pdev->subsystem_vendor;
3460
Tom St Denis1e051412016-06-27 09:57:18 -04003461 while (size && (*pos < no_regs * 4)) {
3462 uint32_t value;
3463
3464 value = config[*pos >> 2];
3465 r = put_user(value, (uint32_t *)buf);
3466 if (r) {
3467 kfree(config);
3468 return r;
3469 }
3470
3471 result += 4;
3472 buf += 4;
3473 *pos += 4;
3474 size -= 4;
3475 }
3476
3477 kfree(config);
3478 return result;
3479}
3480
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003481static ssize_t amdgpu_debugfs_sensor_read(struct file *f, char __user *buf,
3482 size_t size, loff_t *pos)
3483{
Al Viro45063092016-12-04 18:24:56 -05003484 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003485 int idx, x, outsize, r, valuesize;
3486 uint32_t values[16];
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003487
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003488 if (size & 3 || *pos & 0x3)
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003489 return -EINVAL;
3490
Samuel Pitoiset3cbc6142017-02-15 19:32:29 +01003491 if (amdgpu_dpm == 0)
3492 return -EINVAL;
3493
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003494 /* convert offset to sensor number */
3495 idx = *pos >> 2;
3496
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003497 valuesize = sizeof(values);
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003498 if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->read_sensor)
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003499 r = adev->powerplay.pp_funcs->read_sensor(adev->powerplay.pp_handle, idx, &values[0], &valuesize);
Samuel Pitoiset3cbc6142017-02-15 19:32:29 +01003500 else if (adev->pm.funcs && adev->pm.funcs->read_sensor)
3501 r = adev->pm.funcs->read_sensor(adev, idx, &values[0],
3502 &valuesize);
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003503 else
3504 return -EINVAL;
3505
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003506 if (size > valuesize)
3507 return -EINVAL;
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003508
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003509 outsize = 0;
3510 x = 0;
3511 if (!r) {
3512 while (size) {
3513 r = put_user(values[x++], (int32_t *)buf);
3514 buf += 4;
3515 size -= 4;
3516 outsize += 4;
3517 }
3518 }
3519
3520 return !r ? outsize : r;
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003521}
Tom St Denis1e051412016-06-27 09:57:18 -04003522
Tom St Denis273d7aa2016-10-11 14:48:55 -04003523static ssize_t amdgpu_debugfs_wave_read(struct file *f, char __user *buf,
3524 size_t size, loff_t *pos)
3525{
3526 struct amdgpu_device *adev = f->f_inode->i_private;
3527 int r, x;
3528 ssize_t result=0;
Tom St Denis472259f2016-10-14 09:49:09 -04003529 uint32_t offset, se, sh, cu, wave, simd, data[32];
Tom St Denis273d7aa2016-10-11 14:48:55 -04003530
3531 if (size & 3 || *pos & 3)
3532 return -EINVAL;
3533
3534 /* decode offset */
3535 offset = (*pos & 0x7F);
3536 se = ((*pos >> 7) & 0xFF);
3537 sh = ((*pos >> 15) & 0xFF);
3538 cu = ((*pos >> 23) & 0xFF);
3539 wave = ((*pos >> 31) & 0xFF);
3540 simd = ((*pos >> 37) & 0xFF);
Tom St Denis273d7aa2016-10-11 14:48:55 -04003541
3542 /* switch to the specific se/sh/cu */
3543 mutex_lock(&adev->grbm_idx_mutex);
3544 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
3545
3546 x = 0;
Tom St Denis472259f2016-10-14 09:49:09 -04003547 if (adev->gfx.funcs->read_wave_data)
3548 adev->gfx.funcs->read_wave_data(adev, simd, wave, data, &x);
Tom St Denis273d7aa2016-10-11 14:48:55 -04003549
3550 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
3551 mutex_unlock(&adev->grbm_idx_mutex);
3552
Tom St Denis5ecfb3b2016-10-13 12:15:03 -04003553 if (!x)
3554 return -EINVAL;
3555
Tom St Denis472259f2016-10-14 09:49:09 -04003556 while (size && (offset < x * 4)) {
Tom St Denis273d7aa2016-10-11 14:48:55 -04003557 uint32_t value;
3558
Tom St Denis472259f2016-10-14 09:49:09 -04003559 value = data[offset >> 2];
Tom St Denis273d7aa2016-10-11 14:48:55 -04003560 r = put_user(value, (uint32_t *)buf);
3561 if (r)
3562 return r;
3563
3564 result += 4;
3565 buf += 4;
Tom St Denis472259f2016-10-14 09:49:09 -04003566 offset += 4;
Tom St Denis273d7aa2016-10-11 14:48:55 -04003567 size -= 4;
3568 }
3569
3570 return result;
3571}
3572
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003573static ssize_t amdgpu_debugfs_gpr_read(struct file *f, char __user *buf,
3574 size_t size, loff_t *pos)
3575{
3576 struct amdgpu_device *adev = f->f_inode->i_private;
3577 int r;
3578 ssize_t result = 0;
3579 uint32_t offset, se, sh, cu, wave, simd, thread, bank, *data;
3580
3581 if (size & 3 || *pos & 3)
3582 return -EINVAL;
3583
3584 /* decode offset */
3585 offset = (*pos & 0xFFF); /* in dwords */
3586 se = ((*pos >> 12) & 0xFF);
3587 sh = ((*pos >> 20) & 0xFF);
3588 cu = ((*pos >> 28) & 0xFF);
3589 wave = ((*pos >> 36) & 0xFF);
3590 simd = ((*pos >> 44) & 0xFF);
3591 thread = ((*pos >> 52) & 0xFF);
3592 bank = ((*pos >> 60) & 1);
3593
3594 data = kmalloc_array(1024, sizeof(*data), GFP_KERNEL);
3595 if (!data)
3596 return -ENOMEM;
3597
3598 /* switch to the specific se/sh/cu */
3599 mutex_lock(&adev->grbm_idx_mutex);
3600 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
3601
3602 if (bank == 0) {
3603 if (adev->gfx.funcs->read_wave_vgprs)
3604 adev->gfx.funcs->read_wave_vgprs(adev, simd, wave, thread, offset, size>>2, data);
3605 } else {
3606 if (adev->gfx.funcs->read_wave_sgprs)
3607 adev->gfx.funcs->read_wave_sgprs(adev, simd, wave, offset, size>>2, data);
3608 }
3609
3610 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
3611 mutex_unlock(&adev->grbm_idx_mutex);
3612
3613 while (size) {
3614 uint32_t value;
3615
3616 value = data[offset++];
3617 r = put_user(value, (uint32_t *)buf);
3618 if (r) {
3619 result = r;
3620 goto err;
3621 }
3622
3623 result += 4;
3624 buf += 4;
3625 size -= 4;
3626 }
3627
3628err:
3629 kfree(data);
3630 return result;
3631}
3632
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003633static const struct file_operations amdgpu_debugfs_regs_fops = {
3634 .owner = THIS_MODULE,
3635 .read = amdgpu_debugfs_regs_read,
3636 .write = amdgpu_debugfs_regs_write,
3637 .llseek = default_llseek
3638};
Tom St Denisadcec282016-04-15 13:08:44 -04003639static const struct file_operations amdgpu_debugfs_regs_didt_fops = {
3640 .owner = THIS_MODULE,
3641 .read = amdgpu_debugfs_regs_didt_read,
3642 .write = amdgpu_debugfs_regs_didt_write,
3643 .llseek = default_llseek
3644};
3645static const struct file_operations amdgpu_debugfs_regs_pcie_fops = {
3646 .owner = THIS_MODULE,
3647 .read = amdgpu_debugfs_regs_pcie_read,
3648 .write = amdgpu_debugfs_regs_pcie_write,
3649 .llseek = default_llseek
3650};
3651static const struct file_operations amdgpu_debugfs_regs_smc_fops = {
3652 .owner = THIS_MODULE,
3653 .read = amdgpu_debugfs_regs_smc_read,
3654 .write = amdgpu_debugfs_regs_smc_write,
3655 .llseek = default_llseek
3656};
3657
Tom St Denis1e051412016-06-27 09:57:18 -04003658static const struct file_operations amdgpu_debugfs_gca_config_fops = {
3659 .owner = THIS_MODULE,
3660 .read = amdgpu_debugfs_gca_config_read,
3661 .llseek = default_llseek
3662};
3663
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003664static const struct file_operations amdgpu_debugfs_sensors_fops = {
3665 .owner = THIS_MODULE,
3666 .read = amdgpu_debugfs_sensor_read,
3667 .llseek = default_llseek
3668};
3669
Tom St Denis273d7aa2016-10-11 14:48:55 -04003670static const struct file_operations amdgpu_debugfs_wave_fops = {
3671 .owner = THIS_MODULE,
3672 .read = amdgpu_debugfs_wave_read,
3673 .llseek = default_llseek
3674};
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003675static const struct file_operations amdgpu_debugfs_gpr_fops = {
3676 .owner = THIS_MODULE,
3677 .read = amdgpu_debugfs_gpr_read,
3678 .llseek = default_llseek
3679};
Tom St Denis273d7aa2016-10-11 14:48:55 -04003680
Tom St Denisadcec282016-04-15 13:08:44 -04003681static const struct file_operations *debugfs_regs[] = {
3682 &amdgpu_debugfs_regs_fops,
3683 &amdgpu_debugfs_regs_didt_fops,
3684 &amdgpu_debugfs_regs_pcie_fops,
3685 &amdgpu_debugfs_regs_smc_fops,
Tom St Denis1e051412016-06-27 09:57:18 -04003686 &amdgpu_debugfs_gca_config_fops,
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003687 &amdgpu_debugfs_sensors_fops,
Tom St Denis273d7aa2016-10-11 14:48:55 -04003688 &amdgpu_debugfs_wave_fops,
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003689 &amdgpu_debugfs_gpr_fops,
Tom St Denisadcec282016-04-15 13:08:44 -04003690};
3691
3692static const char *debugfs_regs_names[] = {
3693 "amdgpu_regs",
3694 "amdgpu_regs_didt",
3695 "amdgpu_regs_pcie",
3696 "amdgpu_regs_smc",
Tom St Denis1e051412016-06-27 09:57:18 -04003697 "amdgpu_gca_config",
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003698 "amdgpu_sensors",
Tom St Denis273d7aa2016-10-11 14:48:55 -04003699 "amdgpu_wave",
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003700 "amdgpu_gpr",
Tom St Denisadcec282016-04-15 13:08:44 -04003701};
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003702
3703static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
3704{
3705 struct drm_minor *minor = adev->ddev->primary;
3706 struct dentry *ent, *root = minor->debugfs_root;
Tom St Denisadcec282016-04-15 13:08:44 -04003707 unsigned i, j;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003708
Tom St Denisadcec282016-04-15 13:08:44 -04003709 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3710 ent = debugfs_create_file(debugfs_regs_names[i],
3711 S_IFREG | S_IRUGO, root,
3712 adev, debugfs_regs[i]);
3713 if (IS_ERR(ent)) {
3714 for (j = 0; j < i; j++) {
3715 debugfs_remove(adev->debugfs_regs[i]);
3716 adev->debugfs_regs[i] = NULL;
3717 }
3718 return PTR_ERR(ent);
3719 }
3720
3721 if (!i)
3722 i_size_write(ent->d_inode, adev->rmmio_size);
3723 adev->debugfs_regs[i] = ent;
3724 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003725
3726 return 0;
3727}
3728
3729static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev)
3730{
Tom St Denisadcec282016-04-15 13:08:44 -04003731 unsigned i;
3732
3733 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3734 if (adev->debugfs_regs[i]) {
3735 debugfs_remove(adev->debugfs_regs[i]);
3736 adev->debugfs_regs[i] = NULL;
3737 }
3738 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003739}
3740
Huang Rui4f0955f2017-05-10 23:04:06 +08003741static int amdgpu_debugfs_test_ib(struct seq_file *m, void *data)
3742{
3743 struct drm_info_node *node = (struct drm_info_node *) m->private;
3744 struct drm_device *dev = node->minor->dev;
3745 struct amdgpu_device *adev = dev->dev_private;
3746 int r = 0, i;
3747
3748 /* hold on the scheduler */
3749 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
3750 struct amdgpu_ring *ring = adev->rings[i];
3751
3752 if (!ring || !ring->sched.thread)
3753 continue;
3754 kthread_park(ring->sched.thread);
3755 }
3756
3757 seq_printf(m, "run ib test:\n");
3758 r = amdgpu_ib_ring_tests(adev);
3759 if (r)
3760 seq_printf(m, "ib ring tests failed (%d).\n", r);
3761 else
3762 seq_printf(m, "ib ring tests passed.\n");
3763
3764 /* go on the scheduler */
3765 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
3766 struct amdgpu_ring *ring = adev->rings[i];
3767
3768 if (!ring || !ring->sched.thread)
3769 continue;
3770 kthread_unpark(ring->sched.thread);
3771 }
3772
3773 return 0;
3774}
3775
3776static const struct drm_info_list amdgpu_debugfs_test_ib_ring_list[] = {
3777 {"amdgpu_test_ib", &amdgpu_debugfs_test_ib}
3778};
3779
3780static int amdgpu_debugfs_test_ib_ring_init(struct amdgpu_device *adev)
3781{
3782 return amdgpu_debugfs_add_files(adev,
3783 amdgpu_debugfs_test_ib_ring_list, 1);
3784}
3785
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003786int amdgpu_debugfs_init(struct drm_minor *minor)
3787{
3788 return 0;
3789}
Alexander Kuleshov7cebc722015-06-27 13:16:05 +06003790#else
Arnd Bergmann27bad5b2017-06-21 23:51:02 +02003791static int amdgpu_debugfs_test_ib_ring_init(struct amdgpu_device *adev)
Huang Rui4f0955f2017-05-10 23:04:06 +08003792{
3793 return 0;
3794}
Alexander Kuleshov7cebc722015-06-27 13:16:05 +06003795static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
3796{
3797 return 0;
3798}
3799static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev) { }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003800#endif