blob: 078886c6b7587fd51df397094434ab94522bd56d [file] [log] [blame]
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001/*
2 * Copyright 2008 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 * Copyright 2009 Jerome Glisse.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors: Dave Airlie
25 * Alex Deucher
26 * Jerome Glisse
27 */
Chunming Zhou0875dc92016-06-12 15:41:58 +080028#include <linux/kthread.h>
Alex Deucherd38ceaf2015-04-20 16:55:21 -040029#include <linux/console.h>
30#include <linux/slab.h>
31#include <linux/debugfs.h>
32#include <drm/drmP.h>
33#include <drm/drm_crtc_helper.h>
34#include <drm/amdgpu_drm.h>
35#include <linux/vgaarb.h>
36#include <linux/vga_switcheroo.h>
37#include <linux/efi.h>
38#include "amdgpu.h"
Tom St Denisf4b373f2016-05-31 08:02:27 -040039#include "amdgpu_trace.h"
Alex Deucherd38ceaf2015-04-20 16:55:21 -040040#include "amdgpu_i2c.h"
41#include "atom.h"
42#include "amdgpu_atombios.h"
Alex Deuchera5bde2f2016-09-23 16:23:41 -040043#include "amdgpu_atomfirmware.h"
Alex Deucherd0dd7f02015-11-11 19:45:06 -050044#include "amd_pcie.h"
Ken Wang33f34802016-01-21 17:29:41 +080045#ifdef CONFIG_DRM_AMDGPU_SI
46#include "si.h"
47#endif
Alex Deuchera2e73f52015-04-20 17:09:27 -040048#ifdef CONFIG_DRM_AMDGPU_CIK
49#include "cik.h"
50#endif
Alex Deucheraaa36a92015-04-20 17:31:14 -040051#include "vi.h"
Ken Wang460826e2017-03-06 14:53:16 -050052#include "soc15.h"
Alex Deucherd38ceaf2015-04-20 16:55:21 -040053#include "bif/bif_4_1_d.h"
Emily Deng9accf2f2016-08-10 16:01:25 +080054#include <linux/pci.h>
Monk Liubec86372016-09-14 19:38:08 +080055#include <linux/firmware.h>
Gavin Wan89041942017-06-23 13:55:15 -040056#include "amdgpu_vf_error.h"
Alex Deucherd38ceaf2015-04-20 16:55:21 -040057
Yong Zhaoba997702015-11-09 17:21:45 -050058#include "amdgpu_amdkfd.h"
59
Alex Deuchere2a75f82017-04-27 16:58:01 -040060MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin");
Alex Deucher2d2e5e72017-05-09 12:27:35 -040061MODULE_FIRMWARE("amdgpu/raven_gpu_info.bin");
Alex Deuchere2a75f82017-04-27 16:58:01 -040062
Shirish S2dc80b02017-05-25 10:05:25 +053063#define AMDGPU_RESUME_MS 2000
64
Alex Deucherd38ceaf2015-04-20 16:55:21 -040065static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev);
66static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev);
Huang Rui4f0955f2017-05-10 23:04:06 +080067static int amdgpu_debugfs_test_ib_ring_init(struct amdgpu_device *adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040068
69static const char *amdgpu_asic_name[] = {
Ken Wangda69c1612016-01-21 19:08:55 +080070 "TAHITI",
71 "PITCAIRN",
72 "VERDE",
73 "OLAND",
74 "HAINAN",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040075 "BONAIRE",
76 "KAVERI",
77 "KABINI",
78 "HAWAII",
79 "MULLINS",
80 "TOPAZ",
81 "TONGA",
David Zhang48299f92015-07-08 01:05:16 +080082 "FIJI",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040083 "CARRIZO",
Samuel Li139f4912015-10-08 14:50:27 -040084 "STONEY",
Flora Cui2cc0c0b2016-03-14 18:33:29 -040085 "POLARIS10",
86 "POLARIS11",
Junwei Zhangc4642a42016-12-14 15:32:28 -050087 "POLARIS12",
Ken Wangd4196f02016-03-09 09:28:32 +080088 "VEGA10",
Chunming Zhou2ca8a5d2016-12-07 17:31:19 +080089 "RAVEN",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040090 "LAST",
91};
92
93bool amdgpu_device_is_px(struct drm_device *dev)
94{
95 struct amdgpu_device *adev = dev->dev_private;
96
Jammy Zhou2f7d10b2015-07-22 11:29:01 +080097 if (adev->flags & AMD_IS_PX)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040098 return true;
99 return false;
100}
101
102/*
103 * MMIO register access helper functions.
104 */
105uint32_t amdgpu_mm_rreg(struct amdgpu_device *adev, uint32_t reg,
Monk Liu15d72fd2017-01-25 15:07:40 +0800106 uint32_t acc_flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400107{
Tom St Denisf4b373f2016-05-31 08:02:27 -0400108 uint32_t ret;
109
Monk Liu15d72fd2017-01-25 15:07:40 +0800110 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev)) {
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800111 BUG_ON(in_interrupt());
112 return amdgpu_virt_kiq_rreg(adev, reg);
113 }
114
Monk Liu15d72fd2017-01-25 15:07:40 +0800115 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
Tom St Denisf4b373f2016-05-31 08:02:27 -0400116 ret = readl(((void __iomem *)adev->rmmio) + (reg * 4));
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400117 else {
118 unsigned long flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400119
120 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
121 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
122 ret = readl(((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
123 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400124 }
Tom St Denisf4b373f2016-05-31 08:02:27 -0400125 trace_amdgpu_mm_rreg(adev->pdev->device, reg, ret);
126 return ret;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400127}
128
129void amdgpu_mm_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v,
Monk Liu15d72fd2017-01-25 15:07:40 +0800130 uint32_t acc_flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400131{
Tom St Denisf4b373f2016-05-31 08:02:27 -0400132 trace_amdgpu_mm_wreg(adev->pdev->device, reg, v);
Monk Liu4e99a442016-03-31 13:26:59 +0800133
Ken Wang47ed4e12017-07-04 13:11:52 +0800134 if (adev->asic_type >= CHIP_VEGA10 && reg == 0) {
135 adev->last_mm_index = v;
136 }
137
Monk Liu15d72fd2017-01-25 15:07:40 +0800138 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev)) {
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800139 BUG_ON(in_interrupt());
140 return amdgpu_virt_kiq_wreg(adev, reg, v);
141 }
142
Monk Liu15d72fd2017-01-25 15:07:40 +0800143 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400144 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
145 else {
146 unsigned long flags;
147
148 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
149 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
150 writel(v, ((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
151 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
152 }
Ken Wang47ed4e12017-07-04 13:11:52 +0800153
154 if (adev->asic_type >= CHIP_VEGA10 && reg == 1 && adev->last_mm_index == 0x5702C) {
155 udelay(500);
156 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400157}
158
159u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 reg)
160{
161 if ((reg * 4) < adev->rio_mem_size)
162 return ioread32(adev->rio_mem + (reg * 4));
163 else {
164 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
165 return ioread32(adev->rio_mem + (mmMM_DATA * 4));
166 }
167}
168
169void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
170{
Ken Wang47ed4e12017-07-04 13:11:52 +0800171 if (adev->asic_type >= CHIP_VEGA10 && reg == 0) {
172 adev->last_mm_index = v;
173 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400174
175 if ((reg * 4) < adev->rio_mem_size)
176 iowrite32(v, adev->rio_mem + (reg * 4));
177 else {
178 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
179 iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
180 }
Ken Wang47ed4e12017-07-04 13:11:52 +0800181
182 if (adev->asic_type >= CHIP_VEGA10 && reg == 1 && adev->last_mm_index == 0x5702C) {
183 udelay(500);
184 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400185}
186
187/**
188 * amdgpu_mm_rdoorbell - read a doorbell dword
189 *
190 * @adev: amdgpu_device pointer
191 * @index: doorbell index
192 *
193 * Returns the value in the doorbell aperture at the
194 * requested doorbell index (CIK).
195 */
196u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
197{
198 if (index < adev->doorbell.num_doorbells) {
199 return readl(adev->doorbell.ptr + index);
200 } else {
201 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
202 return 0;
203 }
204}
205
206/**
207 * amdgpu_mm_wdoorbell - write a doorbell dword
208 *
209 * @adev: amdgpu_device pointer
210 * @index: doorbell index
211 * @v: value to write
212 *
213 * Writes @v to the doorbell aperture at the
214 * requested doorbell index (CIK).
215 */
216void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
217{
218 if (index < adev->doorbell.num_doorbells) {
219 writel(v, adev->doorbell.ptr + index);
220 } else {
221 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
222 }
223}
224
225/**
Ken Wang832be402016-03-18 15:23:08 +0800226 * amdgpu_mm_rdoorbell64 - read a doorbell Qword
227 *
228 * @adev: amdgpu_device pointer
229 * @index: doorbell index
230 *
231 * Returns the value in the doorbell aperture at the
232 * requested doorbell index (VEGA10+).
233 */
234u64 amdgpu_mm_rdoorbell64(struct amdgpu_device *adev, u32 index)
235{
236 if (index < adev->doorbell.num_doorbells) {
237 return atomic64_read((atomic64_t *)(adev->doorbell.ptr + index));
238 } else {
239 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
240 return 0;
241 }
242}
243
244/**
245 * amdgpu_mm_wdoorbell64 - write a doorbell Qword
246 *
247 * @adev: amdgpu_device pointer
248 * @index: doorbell index
249 * @v: value to write
250 *
251 * Writes @v to the doorbell aperture at the
252 * requested doorbell index (VEGA10+).
253 */
254void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v)
255{
256 if (index < adev->doorbell.num_doorbells) {
257 atomic64_set((atomic64_t *)(adev->doorbell.ptr + index), v);
258 } else {
259 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
260 }
261}
262
263/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400264 * amdgpu_invalid_rreg - dummy reg read function
265 *
266 * @adev: amdgpu device pointer
267 * @reg: offset of register
268 *
269 * Dummy register read function. Used for register blocks
270 * that certain asics don't have (all asics).
271 * Returns the value in the register.
272 */
273static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
274{
275 DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
276 BUG();
277 return 0;
278}
279
280/**
281 * amdgpu_invalid_wreg - dummy reg write function
282 *
283 * @adev: amdgpu device pointer
284 * @reg: offset of register
285 * @v: value to write to the register
286 *
287 * Dummy register read function. Used for register blocks
288 * that certain asics don't have (all asics).
289 */
290static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
291{
292 DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
293 reg, v);
294 BUG();
295}
296
297/**
298 * amdgpu_block_invalid_rreg - dummy reg read function
299 *
300 * @adev: amdgpu device pointer
301 * @block: offset of instance
302 * @reg: offset of register
303 *
304 * Dummy register read function. Used for register blocks
305 * that certain asics don't have (all asics).
306 * Returns the value in the register.
307 */
308static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
309 uint32_t block, uint32_t reg)
310{
311 DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
312 reg, block);
313 BUG();
314 return 0;
315}
316
317/**
318 * amdgpu_block_invalid_wreg - dummy reg write function
319 *
320 * @adev: amdgpu device pointer
321 * @block: offset of instance
322 * @reg: offset of register
323 * @v: value to write to the register
324 *
325 * Dummy register read function. Used for register blocks
326 * that certain asics don't have (all asics).
327 */
328static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
329 uint32_t block,
330 uint32_t reg, uint32_t v)
331{
332 DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
333 reg, block, v);
334 BUG();
335}
336
337static int amdgpu_vram_scratch_init(struct amdgpu_device *adev)
338{
339 int r;
340
341 if (adev->vram_scratch.robj == NULL) {
342 r = amdgpu_bo_create(adev, AMDGPU_GPU_PAGE_SIZE,
Alex Deucher857d9132015-08-27 00:14:16 -0400343 PAGE_SIZE, true, AMDGPU_GEM_DOMAIN_VRAM,
Christian König03f48dd2016-08-15 17:00:22 +0200344 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
345 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS,
Christian König72d76682015-09-03 17:34:59 +0200346 NULL, NULL, &adev->vram_scratch.robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400347 if (r) {
348 return r;
349 }
350 }
351
352 r = amdgpu_bo_reserve(adev->vram_scratch.robj, false);
353 if (unlikely(r != 0))
354 return r;
355 r = amdgpu_bo_pin(adev->vram_scratch.robj,
356 AMDGPU_GEM_DOMAIN_VRAM, &adev->vram_scratch.gpu_addr);
357 if (r) {
358 amdgpu_bo_unreserve(adev->vram_scratch.robj);
359 return r;
360 }
361 r = amdgpu_bo_kmap(adev->vram_scratch.robj,
362 (void **)&adev->vram_scratch.ptr);
363 if (r)
364 amdgpu_bo_unpin(adev->vram_scratch.robj);
365 amdgpu_bo_unreserve(adev->vram_scratch.robj);
366
367 return r;
368}
369
370static void amdgpu_vram_scratch_fini(struct amdgpu_device *adev)
371{
372 int r;
373
374 if (adev->vram_scratch.robj == NULL) {
375 return;
376 }
Alex Xie8ab25b42017-04-24 13:30:43 -0400377 r = amdgpu_bo_reserve(adev->vram_scratch.robj, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400378 if (likely(r == 0)) {
379 amdgpu_bo_kunmap(adev->vram_scratch.robj);
380 amdgpu_bo_unpin(adev->vram_scratch.robj);
381 amdgpu_bo_unreserve(adev->vram_scratch.robj);
382 }
383 amdgpu_bo_unref(&adev->vram_scratch.robj);
384}
385
386/**
387 * amdgpu_program_register_sequence - program an array of registers.
388 *
389 * @adev: amdgpu_device pointer
390 * @registers: pointer to the register array
391 * @array_size: size of the register array
392 *
393 * Programs an array or registers with and and or masks.
394 * This is a helper for setting golden registers.
395 */
396void amdgpu_program_register_sequence(struct amdgpu_device *adev,
397 const u32 *registers,
398 const u32 array_size)
399{
400 u32 tmp, reg, and_mask, or_mask;
401 int i;
402
403 if (array_size % 3)
404 return;
405
406 for (i = 0; i < array_size; i +=3) {
407 reg = registers[i + 0];
408 and_mask = registers[i + 1];
409 or_mask = registers[i + 2];
410
411 if (and_mask == 0xffffffff) {
412 tmp = or_mask;
413 } else {
414 tmp = RREG32(reg);
415 tmp &= ~and_mask;
416 tmp |= or_mask;
417 }
418 WREG32(reg, tmp);
419 }
420}
421
422void amdgpu_pci_config_reset(struct amdgpu_device *adev)
423{
424 pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
425}
426
427/*
428 * GPU doorbell aperture helpers function.
429 */
430/**
431 * amdgpu_doorbell_init - Init doorbell driver information.
432 *
433 * @adev: amdgpu_device pointer
434 *
435 * Init doorbell driver information (CIK)
436 * Returns 0 on success, error on failure.
437 */
438static int amdgpu_doorbell_init(struct amdgpu_device *adev)
439{
440 /* doorbell bar mapping */
441 adev->doorbell.base = pci_resource_start(adev->pdev, 2);
442 adev->doorbell.size = pci_resource_len(adev->pdev, 2);
443
Christian Königedf600d2016-05-03 15:54:54 +0200444 adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400445 AMDGPU_DOORBELL_MAX_ASSIGNMENT+1);
446 if (adev->doorbell.num_doorbells == 0)
447 return -EINVAL;
448
Christian König8972e5d2017-03-06 13:34:57 +0100449 adev->doorbell.ptr = ioremap(adev->doorbell.base,
450 adev->doorbell.num_doorbells *
451 sizeof(u32));
452 if (adev->doorbell.ptr == NULL)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400453 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400454
455 return 0;
456}
457
458/**
459 * amdgpu_doorbell_fini - Tear down doorbell driver information.
460 *
461 * @adev: amdgpu_device pointer
462 *
463 * Tear down doorbell driver information (CIK)
464 */
465static void amdgpu_doorbell_fini(struct amdgpu_device *adev)
466{
467 iounmap(adev->doorbell.ptr);
468 adev->doorbell.ptr = NULL;
469}
470
471/**
472 * amdgpu_doorbell_get_kfd_info - Report doorbell configuration required to
473 * setup amdkfd
474 *
475 * @adev: amdgpu_device pointer
476 * @aperture_base: output returning doorbell aperture base physical address
477 * @aperture_size: output returning doorbell aperture size in bytes
478 * @start_offset: output returning # of doorbell bytes reserved for amdgpu.
479 *
480 * amdgpu and amdkfd share the doorbell aperture. amdgpu sets it up,
481 * takes doorbells required for its own rings and reports the setup to amdkfd.
482 * amdgpu reserved doorbells are at the start of the doorbell aperture.
483 */
484void amdgpu_doorbell_get_kfd_info(struct amdgpu_device *adev,
485 phys_addr_t *aperture_base,
486 size_t *aperture_size,
487 size_t *start_offset)
488{
489 /*
490 * The first num_doorbells are used by amdgpu.
491 * amdkfd takes whatever's left in the aperture.
492 */
493 if (adev->doorbell.size > adev->doorbell.num_doorbells * sizeof(u32)) {
494 *aperture_base = adev->doorbell.base;
495 *aperture_size = adev->doorbell.size;
496 *start_offset = adev->doorbell.num_doorbells * sizeof(u32);
497 } else {
498 *aperture_base = 0;
499 *aperture_size = 0;
500 *start_offset = 0;
501 }
502}
503
504/*
505 * amdgpu_wb_*()
Alex Xie455a7bc2017-05-08 21:36:03 -0400506 * Writeback is the method by which the GPU updates special pages in memory
Alex Xieea81a172017-05-08 13:41:11 -0400507 * with the status of certain GPU events (fences, ring pointers,etc.).
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400508 */
509
510/**
511 * amdgpu_wb_fini - Disable Writeback and free memory
512 *
513 * @adev: amdgpu_device pointer
514 *
515 * Disables Writeback and frees the Writeback memory (all asics).
516 * Used at driver shutdown.
517 */
518static void amdgpu_wb_fini(struct amdgpu_device *adev)
519{
520 if (adev->wb.wb_obj) {
Alex Deuchera76ed482016-10-21 15:30:36 -0400521 amdgpu_bo_free_kernel(&adev->wb.wb_obj,
522 &adev->wb.gpu_addr,
523 (void **)&adev->wb.wb);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400524 adev->wb.wb_obj = NULL;
525 }
526}
527
528/**
529 * amdgpu_wb_init- Init Writeback driver info and allocate memory
530 *
531 * @adev: amdgpu_device pointer
532 *
Alex Xie455a7bc2017-05-08 21:36:03 -0400533 * Initializes writeback and allocates writeback memory (all asics).
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400534 * Used at driver startup.
535 * Returns 0 on success or an -error on failure.
536 */
537static int amdgpu_wb_init(struct amdgpu_device *adev)
538{
539 int r;
540
541 if (adev->wb.wb_obj == NULL) {
Huang Rui60a970a62017-03-15 10:13:32 +0800542 r = amdgpu_bo_create_kernel(adev, AMDGPU_MAX_WB * sizeof(uint32_t),
Alex Deuchera76ed482016-10-21 15:30:36 -0400543 PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
544 &adev->wb.wb_obj, &adev->wb.gpu_addr,
545 (void **)&adev->wb.wb);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400546 if (r) {
547 dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
548 return r;
549 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400550
551 adev->wb.num_wb = AMDGPU_MAX_WB;
552 memset(&adev->wb.used, 0, sizeof(adev->wb.used));
553
554 /* clear wb memory */
Huang Rui60a970a62017-03-15 10:13:32 +0800555 memset((char *)adev->wb.wb, 0, AMDGPU_MAX_WB * sizeof(uint32_t));
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400556 }
557
558 return 0;
559}
560
561/**
562 * amdgpu_wb_get - Allocate a wb entry
563 *
564 * @adev: amdgpu_device pointer
565 * @wb: wb index
566 *
567 * Allocate a wb slot for use by the driver (all asics).
568 * Returns 0 on success or -EINVAL on failure.
569 */
570int amdgpu_wb_get(struct amdgpu_device *adev, u32 *wb)
571{
572 unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
573 if (offset < adev->wb.num_wb) {
574 __set_bit(offset, adev->wb.used);
575 *wb = offset;
576 return 0;
577 } else {
578 return -EINVAL;
579 }
580}
581
582/**
Ken Wang70142852016-03-18 15:08:49 +0800583 * amdgpu_wb_get_64bit - Allocate a wb entry
584 *
585 * @adev: amdgpu_device pointer
586 * @wb: wb index
587 *
588 * Allocate a wb slot for use by the driver (all asics).
589 * Returns 0 on success or -EINVAL on failure.
590 */
591int amdgpu_wb_get_64bit(struct amdgpu_device *adev, u32 *wb)
592{
593 unsigned long offset = bitmap_find_next_zero_area_off(adev->wb.used,
594 adev->wb.num_wb, 0, 2, 7, 0);
595 if ((offset + 1) < adev->wb.num_wb) {
596 __set_bit(offset, adev->wb.used);
597 __set_bit(offset + 1, adev->wb.used);
598 *wb = offset;
599 return 0;
600 } else {
601 return -EINVAL;
602 }
603}
604
605/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400606 * amdgpu_wb_free - Free a wb entry
607 *
608 * @adev: amdgpu_device pointer
609 * @wb: wb index
610 *
611 * Free a wb slot allocated for use by the driver (all asics)
612 */
613void amdgpu_wb_free(struct amdgpu_device *adev, u32 wb)
614{
615 if (wb < adev->wb.num_wb)
616 __clear_bit(wb, adev->wb.used);
617}
618
619/**
Ken Wang70142852016-03-18 15:08:49 +0800620 * amdgpu_wb_free_64bit - Free a wb entry
621 *
622 * @adev: amdgpu_device pointer
623 * @wb: wb index
624 *
625 * Free a wb slot allocated for use by the driver (all asics)
626 */
627void amdgpu_wb_free_64bit(struct amdgpu_device *adev, u32 wb)
628{
629 if ((wb + 1) < adev->wb.num_wb) {
630 __clear_bit(wb, adev->wb.used);
631 __clear_bit(wb + 1, adev->wb.used);
632 }
633}
634
635/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400636 * amdgpu_vram_location - try to find VRAM location
637 * @adev: amdgpu device structure holding all necessary informations
638 * @mc: memory controller structure holding memory informations
639 * @base: base address at which to put VRAM
640 *
Alex Xie455a7bc2017-05-08 21:36:03 -0400641 * Function will try to place VRAM at base address provided
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400642 * as parameter (which is so far either PCI aperture address or
643 * for IGP TOM base address).
644 *
645 * If there is not enough space to fit the unvisible VRAM in the 32bits
646 * address space then we limit the VRAM size to the aperture.
647 *
648 * Note: We don't explicitly enforce VRAM start to be aligned on VRAM size,
649 * this shouldn't be a problem as we are using the PCI aperture as a reference.
650 * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but
651 * not IGP.
652 *
653 * Note: we use mc_vram_size as on some board we need to program the mc to
654 * cover the whole aperture even if VRAM size is inferior to aperture size
655 * Novell bug 204882 + along with lots of ubuntu ones
656 *
657 * Note: when limiting vram it's safe to overwritte real_vram_size because
658 * we are not in case where real_vram_size is inferior to mc_vram_size (ie
659 * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu
660 * ones)
661 *
662 * Note: IGP TOM addr should be the same as the aperture addr, we don't
Alex Xie455a7bc2017-05-08 21:36:03 -0400663 * explicitly check for that though.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400664 *
665 * FIXME: when reducing VRAM size align new size on power of 2.
666 */
667void amdgpu_vram_location(struct amdgpu_device *adev, struct amdgpu_mc *mc, u64 base)
668{
669 uint64_t limit = (uint64_t)amdgpu_vram_limit << 20;
670
671 mc->vram_start = base;
672 if (mc->mc_vram_size > (adev->mc.mc_mask - base + 1)) {
673 dev_warn(adev->dev, "limiting VRAM to PCI aperture size\n");
674 mc->real_vram_size = mc->aper_size;
675 mc->mc_vram_size = mc->aper_size;
676 }
677 mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
678 if (limit && limit < mc->real_vram_size)
679 mc->real_vram_size = limit;
680 dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
681 mc->mc_vram_size >> 20, mc->vram_start,
682 mc->vram_end, mc->real_vram_size >> 20);
683}
684
685/**
Christian König6f02a692017-07-07 11:56:59 +0200686 * amdgpu_gart_location - try to find GTT location
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400687 * @adev: amdgpu device structure holding all necessary informations
688 * @mc: memory controller structure holding memory informations
689 *
690 * Function will place try to place GTT before or after VRAM.
691 *
692 * If GTT size is bigger than space left then we ajust GTT size.
693 * Thus function will never fails.
694 *
695 * FIXME: when reducing GTT size align new size on power of 2.
696 */
Christian König6f02a692017-07-07 11:56:59 +0200697void amdgpu_gart_location(struct amdgpu_device *adev, struct amdgpu_mc *mc)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400698{
699 u64 size_af, size_bf;
700
Christian Königed21c042017-07-06 22:26:05 +0200701 size_af = adev->mc.mc_mask - mc->vram_end;
702 size_bf = mc->vram_start;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400703 if (size_bf > size_af) {
Christian König6f02a692017-07-07 11:56:59 +0200704 if (mc->gart_size > size_bf) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400705 dev_warn(adev->dev, "limiting GTT\n");
Christian König6f02a692017-07-07 11:56:59 +0200706 mc->gart_size = size_bf;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400707 }
Christian König6f02a692017-07-07 11:56:59 +0200708 mc->gart_start = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400709 } else {
Christian König6f02a692017-07-07 11:56:59 +0200710 if (mc->gart_size > size_af) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400711 dev_warn(adev->dev, "limiting GTT\n");
Christian König6f02a692017-07-07 11:56:59 +0200712 mc->gart_size = size_af;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400713 }
Christian König6f02a692017-07-07 11:56:59 +0200714 mc->gart_start = mc->vram_end + 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400715 }
Christian König6f02a692017-07-07 11:56:59 +0200716 mc->gart_end = mc->gart_start + mc->gart_size - 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400717 dev_info(adev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
Christian König6f02a692017-07-07 11:56:59 +0200718 mc->gart_size >> 20, mc->gart_start, mc->gart_end);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400719}
720
721/*
722 * GPU helpers function.
723 */
724/**
Jim Quc836fec2017-02-10 15:59:59 +0800725 * amdgpu_need_post - check if the hw need post or not
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400726 *
727 * @adev: amdgpu_device pointer
728 *
Jim Quc836fec2017-02-10 15:59:59 +0800729 * Check if the asic has been initialized (all asics) at driver startup
730 * or post is needed if hw reset is performed.
731 * Returns true if need or false if not.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400732 */
Jim Quc836fec2017-02-10 15:59:59 +0800733bool amdgpu_need_post(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400734{
735 uint32_t reg;
736
Jim Quc836fec2017-02-10 15:59:59 +0800737 if (adev->has_hw_reset) {
738 adev->has_hw_reset = false;
739 return true;
740 }
Alex Deucher70d17a22017-06-30 17:26:47 -0400741
742 /* bios scratch used on CIK+ */
743 if (adev->asic_type >= CHIP_BONAIRE)
744 return amdgpu_atombios_scratch_need_asic_init(adev);
745
746 /* check MEM_SIZE for older asics */
Alex Deucherbbf282d2017-03-03 17:26:10 -0500747 reg = amdgpu_asic_get_config_memsize(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400748
Alex Deucherf2713e82017-03-28 12:19:31 -0400749 if ((reg != 0) && (reg != 0xffffffff))
Jim Quc836fec2017-02-10 15:59:59 +0800750 return false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400751
Jim Quc836fec2017-02-10 15:59:59 +0800752 return true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400753
754}
755
Monk Liubec86372016-09-14 19:38:08 +0800756static bool amdgpu_vpost_needed(struct amdgpu_device *adev)
757{
758 if (amdgpu_sriov_vf(adev))
759 return false;
760
761 if (amdgpu_passthrough(adev)) {
Monk Liu1da2c322016-11-11 11:24:29 +0800762 /* for FIJI: In whole GPU pass-through virtualization case, after VM reboot
763 * some old smc fw still need driver do vPost otherwise gpu hang, while
764 * those smc fw version above 22.15 doesn't have this flaw, so we force
765 * vpost executed for smc version below 22.15
Monk Liubec86372016-09-14 19:38:08 +0800766 */
767 if (adev->asic_type == CHIP_FIJI) {
768 int err;
769 uint32_t fw_ver;
770 err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
771 /* force vPost if error occured */
772 if (err)
773 return true;
774
775 fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
Monk Liu1da2c322016-11-11 11:24:29 +0800776 if (fw_ver < 0x00160e00)
777 return true;
Monk Liubec86372016-09-14 19:38:08 +0800778 }
Monk Liubec86372016-09-14 19:38:08 +0800779 }
Jim Quc836fec2017-02-10 15:59:59 +0800780 return amdgpu_need_post(adev);
Monk Liubec86372016-09-14 19:38:08 +0800781}
782
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400783/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400784 * amdgpu_dummy_page_init - init dummy page used by the driver
785 *
786 * @adev: amdgpu_device pointer
787 *
788 * Allocate the dummy page used by the driver (all asics).
789 * This dummy page is used by the driver as a filler for gart entries
790 * when pages are taken out of the GART
791 * Returns 0 on sucess, -ENOMEM on failure.
792 */
793int amdgpu_dummy_page_init(struct amdgpu_device *adev)
794{
795 if (adev->dummy_page.page)
796 return 0;
797 adev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
798 if (adev->dummy_page.page == NULL)
799 return -ENOMEM;
800 adev->dummy_page.addr = pci_map_page(adev->pdev, adev->dummy_page.page,
801 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
802 if (pci_dma_mapping_error(adev->pdev, adev->dummy_page.addr)) {
803 dev_err(&adev->pdev->dev, "Failed to DMA MAP the dummy page\n");
804 __free_page(adev->dummy_page.page);
805 adev->dummy_page.page = NULL;
806 return -ENOMEM;
807 }
808 return 0;
809}
810
811/**
812 * amdgpu_dummy_page_fini - free dummy page used by the driver
813 *
814 * @adev: amdgpu_device pointer
815 *
816 * Frees the dummy page used by the driver (all asics).
817 */
818void amdgpu_dummy_page_fini(struct amdgpu_device *adev)
819{
820 if (adev->dummy_page.page == NULL)
821 return;
822 pci_unmap_page(adev->pdev, adev->dummy_page.addr,
823 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
824 __free_page(adev->dummy_page.page);
825 adev->dummy_page.page = NULL;
826}
827
828
829/* ATOM accessor methods */
830/*
831 * ATOM is an interpreted byte code stored in tables in the vbios. The
832 * driver registers callbacks to access registers and the interpreter
833 * in the driver parses the tables and executes then to program specific
834 * actions (set display modes, asic init, etc.). See amdgpu_atombios.c,
835 * atombios.h, and atom.c
836 */
837
838/**
839 * cail_pll_read - read PLL register
840 *
841 * @info: atom card_info pointer
842 * @reg: PLL register offset
843 *
844 * Provides a PLL register accessor for the atom interpreter (r4xx+).
845 * Returns the value of the PLL register.
846 */
847static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
848{
849 return 0;
850}
851
852/**
853 * cail_pll_write - write PLL register
854 *
855 * @info: atom card_info pointer
856 * @reg: PLL register offset
857 * @val: value to write to the pll register
858 *
859 * Provides a PLL register accessor for the atom interpreter (r4xx+).
860 */
861static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
862{
863
864}
865
866/**
867 * cail_mc_read - read MC (Memory Controller) register
868 *
869 * @info: atom card_info pointer
870 * @reg: MC register offset
871 *
872 * Provides an MC register accessor for the atom interpreter (r4xx+).
873 * Returns the value of the MC register.
874 */
875static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
876{
877 return 0;
878}
879
880/**
881 * cail_mc_write - write MC (Memory Controller) register
882 *
883 * @info: atom card_info pointer
884 * @reg: MC register offset
885 * @val: value to write to the pll register
886 *
887 * Provides a MC register accessor for the atom interpreter (r4xx+).
888 */
889static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
890{
891
892}
893
894/**
895 * cail_reg_write - write MMIO register
896 *
897 * @info: atom card_info pointer
898 * @reg: MMIO register offset
899 * @val: value to write to the pll register
900 *
901 * Provides a MMIO register accessor for the atom interpreter (r4xx+).
902 */
903static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
904{
905 struct amdgpu_device *adev = info->dev->dev_private;
906
907 WREG32(reg, val);
908}
909
910/**
911 * cail_reg_read - read MMIO register
912 *
913 * @info: atom card_info pointer
914 * @reg: MMIO register offset
915 *
916 * Provides an MMIO register accessor for the atom interpreter (r4xx+).
917 * Returns the value of the MMIO register.
918 */
919static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
920{
921 struct amdgpu_device *adev = info->dev->dev_private;
922 uint32_t r;
923
924 r = RREG32(reg);
925 return r;
926}
927
928/**
929 * cail_ioreg_write - write IO register
930 *
931 * @info: atom card_info pointer
932 * @reg: IO register offset
933 * @val: value to write to the pll register
934 *
935 * Provides a IO register accessor for the atom interpreter (r4xx+).
936 */
937static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
938{
939 struct amdgpu_device *adev = info->dev->dev_private;
940
941 WREG32_IO(reg, val);
942}
943
944/**
945 * cail_ioreg_read - read IO register
946 *
947 * @info: atom card_info pointer
948 * @reg: IO register offset
949 *
950 * Provides an IO register accessor for the atom interpreter (r4xx+).
951 * Returns the value of the IO register.
952 */
953static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
954{
955 struct amdgpu_device *adev = info->dev->dev_private;
956 uint32_t r;
957
958 r = RREG32_IO(reg);
959 return r;
960}
961
962/**
963 * amdgpu_atombios_fini - free the driver info and callbacks for atombios
964 *
965 * @adev: amdgpu_device pointer
966 *
967 * Frees the driver info and register access callbacks for the ATOM
968 * interpreter (r4xx+).
969 * Called at driver shutdown.
970 */
971static void amdgpu_atombios_fini(struct amdgpu_device *adev)
972{
Monk Liu89e0ec9f2016-05-27 19:34:11 +0800973 if (adev->mode_info.atom_context) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400974 kfree(adev->mode_info.atom_context->scratch);
Monk Liu89e0ec9f2016-05-27 19:34:11 +0800975 kfree(adev->mode_info.atom_context->iio);
976 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400977 kfree(adev->mode_info.atom_context);
978 adev->mode_info.atom_context = NULL;
979 kfree(adev->mode_info.atom_card_info);
980 adev->mode_info.atom_card_info = NULL;
981}
982
983/**
984 * amdgpu_atombios_init - init the driver info and callbacks for atombios
985 *
986 * @adev: amdgpu_device pointer
987 *
988 * Initializes the driver info and register access callbacks for the
989 * ATOM interpreter (r4xx+).
990 * Returns 0 on sucess, -ENOMEM on failure.
991 * Called at driver startup.
992 */
993static int amdgpu_atombios_init(struct amdgpu_device *adev)
994{
995 struct card_info *atom_card_info =
996 kzalloc(sizeof(struct card_info), GFP_KERNEL);
997
998 if (!atom_card_info)
999 return -ENOMEM;
1000
1001 adev->mode_info.atom_card_info = atom_card_info;
1002 atom_card_info->dev = adev->ddev;
1003 atom_card_info->reg_read = cail_reg_read;
1004 atom_card_info->reg_write = cail_reg_write;
1005 /* needed for iio ops */
1006 if (adev->rio_mem) {
1007 atom_card_info->ioreg_read = cail_ioreg_read;
1008 atom_card_info->ioreg_write = cail_ioreg_write;
1009 } else {
Amber Linb64a18c2017-01-04 08:06:58 -05001010 DRM_INFO("PCI I/O BAR is not found. Using MMIO to access ATOM BIOS\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001011 atom_card_info->ioreg_read = cail_reg_read;
1012 atom_card_info->ioreg_write = cail_reg_write;
1013 }
1014 atom_card_info->mc_read = cail_mc_read;
1015 atom_card_info->mc_write = cail_mc_write;
1016 atom_card_info->pll_read = cail_pll_read;
1017 atom_card_info->pll_write = cail_pll_write;
1018
1019 adev->mode_info.atom_context = amdgpu_atom_parse(atom_card_info, adev->bios);
1020 if (!adev->mode_info.atom_context) {
1021 amdgpu_atombios_fini(adev);
1022 return -ENOMEM;
1023 }
1024
1025 mutex_init(&adev->mode_info.atom_context->mutex);
Alex Deuchera5bde2f2016-09-23 16:23:41 -04001026 if (adev->is_atom_fw) {
1027 amdgpu_atomfirmware_scratch_regs_init(adev);
1028 amdgpu_atomfirmware_allocate_fb_scratch(adev);
1029 } else {
1030 amdgpu_atombios_scratch_regs_init(adev);
1031 amdgpu_atombios_allocate_fb_scratch(adev);
1032 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001033 return 0;
1034}
1035
1036/* if we get transitioned to only one device, take VGA back */
1037/**
1038 * amdgpu_vga_set_decode - enable/disable vga decode
1039 *
1040 * @cookie: amdgpu_device pointer
1041 * @state: enable/disable vga decode
1042 *
1043 * Enable/disable vga decode (all asics).
1044 * Returns VGA resource flags.
1045 */
1046static unsigned int amdgpu_vga_set_decode(void *cookie, bool state)
1047{
1048 struct amdgpu_device *adev = cookie;
1049 amdgpu_asic_set_vga_state(adev, state);
1050 if (state)
1051 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
1052 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1053 else
1054 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1055}
1056
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001057static void amdgpu_check_block_size(struct amdgpu_device *adev)
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001058{
1059 /* defines number of bits in page table versus page directory,
1060 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
1061 * page table and the remaining bits are in the page directory */
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001062 if (amdgpu_vm_block_size == -1)
1063 return;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001064
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001065 if (amdgpu_vm_block_size < 9) {
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001066 dev_warn(adev->dev, "VM page table size (%d) too small\n",
1067 amdgpu_vm_block_size);
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001068 goto def_value;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001069 }
1070
1071 if (amdgpu_vm_block_size > 24 ||
1072 (amdgpu_vm_size * 1024) < (1ull << amdgpu_vm_block_size)) {
1073 dev_warn(adev->dev, "VM page table size (%d) too large\n",
1074 amdgpu_vm_block_size);
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001075 goto def_value;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001076 }
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001077
1078 return;
1079
1080def_value:
1081 amdgpu_vm_block_size = -1;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001082}
1083
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001084static void amdgpu_check_vm_size(struct amdgpu_device *adev)
1085{
Alex Deucher64dab072017-06-15 18:20:09 -04001086 /* no need to check the default value */
1087 if (amdgpu_vm_size == -1)
1088 return;
1089
Alex Deucher76117502017-06-21 12:31:41 -04001090 if (!is_power_of_2(amdgpu_vm_size)) {
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001091 dev_warn(adev->dev, "VM size (%d) must be a power of 2\n",
1092 amdgpu_vm_size);
1093 goto def_value;
1094 }
1095
1096 if (amdgpu_vm_size < 1) {
1097 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
1098 amdgpu_vm_size);
1099 goto def_value;
1100 }
1101
1102 /*
1103 * Max GPUVM size for Cayman, SI, CI VI are 40 bits.
1104 */
1105 if (amdgpu_vm_size > 1024) {
1106 dev_warn(adev->dev, "VM size (%d) too large, max is 1TB\n",
1107 amdgpu_vm_size);
1108 goto def_value;
1109 }
1110
1111 return;
1112
1113def_value:
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001114 amdgpu_vm_size = -1;
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001115}
1116
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001117/**
1118 * amdgpu_check_arguments - validate module params
1119 *
1120 * @adev: amdgpu_device pointer
1121 *
1122 * Validates certain module parameters and updates
1123 * the associated values used by the driver (all asics).
1124 */
1125static void amdgpu_check_arguments(struct amdgpu_device *adev)
1126{
Chunming Zhou5b011232015-12-10 17:34:33 +08001127 if (amdgpu_sched_jobs < 4) {
1128 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
1129 amdgpu_sched_jobs);
1130 amdgpu_sched_jobs = 4;
Alex Deucher76117502017-06-21 12:31:41 -04001131 } else if (!is_power_of_2(amdgpu_sched_jobs)){
Chunming Zhou5b011232015-12-10 17:34:33 +08001132 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
1133 amdgpu_sched_jobs);
1134 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
1135 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001136
Christian Königf9321cc2017-07-07 13:44:05 +02001137 if (amdgpu_gart_size < 32) {
1138 /* gart size must be greater or equal to 32M */
1139 dev_warn(adev->dev, "gart size (%d) too small\n",
1140 amdgpu_gart_size);
1141 amdgpu_gart_size = 32;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001142 }
1143
Christian König36d38372017-07-07 13:17:45 +02001144 if (amdgpu_gtt_size != -1 && amdgpu_gtt_size < 32) {
1145 /* gtt size must be greater or equal to 32M */
1146 dev_warn(adev->dev, "gtt size (%d) too small\n",
1147 amdgpu_gtt_size);
1148 amdgpu_gtt_size = -1;
1149 }
1150
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001151 amdgpu_check_vm_size(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001152
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001153 amdgpu_check_block_size(adev);
Christian König6a7f76e2016-08-24 15:51:49 +02001154
jimqu526bae32016-11-07 09:53:10 +08001155 if (amdgpu_vram_page_split != -1 && (amdgpu_vram_page_split < 16 ||
Alex Deucher76117502017-06-21 12:31:41 -04001156 !is_power_of_2(amdgpu_vram_page_split))) {
Christian König6a7f76e2016-08-24 15:51:49 +02001157 dev_warn(adev->dev, "invalid VRAM page split (%d)\n",
1158 amdgpu_vram_page_split);
1159 amdgpu_vram_page_split = 1024;
1160 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001161}
1162
1163/**
1164 * amdgpu_switcheroo_set_state - set switcheroo state
1165 *
1166 * @pdev: pci dev pointer
Lukas Wunner16944672015-09-05 11:17:35 +02001167 * @state: vga_switcheroo state
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001168 *
1169 * Callback for the switcheroo driver. Suspends or resumes the
1170 * the asics before or after it is powered up using ACPI methods.
1171 */
1172static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1173{
1174 struct drm_device *dev = pci_get_drvdata(pdev);
1175
1176 if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
1177 return;
1178
1179 if (state == VGA_SWITCHEROO_ON) {
1180 unsigned d3_delay = dev->pdev->d3_delay;
1181
Joe Perches7ca85292017-02-28 04:55:52 -08001182 pr_info("amdgpu: switched on\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001183 /* don't suspend or resume card normally */
1184 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1185
Alex Deucher810ddc32016-08-23 13:25:49 -04001186 amdgpu_device_resume(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001187
1188 dev->pdev->d3_delay = d3_delay;
1189
1190 dev->switch_power_state = DRM_SWITCH_POWER_ON;
1191 drm_kms_helper_poll_enable(dev);
1192 } else {
Joe Perches7ca85292017-02-28 04:55:52 -08001193 pr_info("amdgpu: switched off\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001194 drm_kms_helper_poll_disable(dev);
1195 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Alex Deucher810ddc32016-08-23 13:25:49 -04001196 amdgpu_device_suspend(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001197 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1198 }
1199}
1200
1201/**
1202 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1203 *
1204 * @pdev: pci dev pointer
1205 *
1206 * Callback for the switcheroo driver. Check of the switcheroo
1207 * state can be changed.
1208 * Returns true if the state can be changed, false if not.
1209 */
1210static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1211{
1212 struct drm_device *dev = pci_get_drvdata(pdev);
1213
1214 /*
1215 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1216 * locking inversion with the driver load path. And the access here is
1217 * completely racy anyway. So don't bother with locking for now.
1218 */
1219 return dev->open_count == 0;
1220}
1221
1222static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1223 .set_gpu_state = amdgpu_switcheroo_set_state,
1224 .reprobe = NULL,
1225 .can_switch = amdgpu_switcheroo_can_switch,
1226};
1227
1228int amdgpu_set_clockgating_state(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001229 enum amd_ip_block_type block_type,
1230 enum amd_clockgating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001231{
1232 int i, r = 0;
1233
1234 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001235 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001236 continue;
Rex Zhuc7228652017-02-22 15:33:46 +08001237 if (adev->ip_blocks[i].version->type != block_type)
1238 continue;
1239 if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
1240 continue;
1241 r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
1242 (void *)adev, state);
1243 if (r)
1244 DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
1245 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001246 }
1247 return r;
1248}
1249
1250int amdgpu_set_powergating_state(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001251 enum amd_ip_block_type block_type,
1252 enum amd_powergating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001253{
1254 int i, r = 0;
1255
1256 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001257 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001258 continue;
Rex Zhuc7228652017-02-22 15:33:46 +08001259 if (adev->ip_blocks[i].version->type != block_type)
1260 continue;
1261 if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
1262 continue;
1263 r = adev->ip_blocks[i].version->funcs->set_powergating_state(
1264 (void *)adev, state);
1265 if (r)
1266 DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
1267 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001268 }
1269 return r;
1270}
1271
Huang Rui6cb2d4e2017-01-05 18:44:41 +08001272void amdgpu_get_clockgating_state(struct amdgpu_device *adev, u32 *flags)
1273{
1274 int i;
1275
1276 for (i = 0; i < adev->num_ip_blocks; i++) {
1277 if (!adev->ip_blocks[i].status.valid)
1278 continue;
1279 if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
1280 adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
1281 }
1282}
1283
Alex Deucher5dbbb602016-06-23 11:41:04 -04001284int amdgpu_wait_for_idle(struct amdgpu_device *adev,
1285 enum amd_ip_block_type block_type)
1286{
1287 int i, r;
1288
1289 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001290 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001291 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001292 if (adev->ip_blocks[i].version->type == block_type) {
1293 r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -04001294 if (r)
1295 return r;
1296 break;
1297 }
1298 }
1299 return 0;
1300
1301}
1302
1303bool amdgpu_is_idle(struct amdgpu_device *adev,
1304 enum amd_ip_block_type block_type)
1305{
1306 int i;
1307
1308 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001309 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001310 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001311 if (adev->ip_blocks[i].version->type == block_type)
1312 return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -04001313 }
1314 return true;
1315
1316}
1317
Alex Deuchera1255102016-10-13 17:41:13 -04001318struct amdgpu_ip_block * amdgpu_get_ip_block(struct amdgpu_device *adev,
1319 enum amd_ip_block_type type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001320{
1321 int i;
1322
1323 for (i = 0; i < adev->num_ip_blocks; i++)
Alex Deuchera1255102016-10-13 17:41:13 -04001324 if (adev->ip_blocks[i].version->type == type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001325 return &adev->ip_blocks[i];
1326
1327 return NULL;
1328}
1329
1330/**
1331 * amdgpu_ip_block_version_cmp
1332 *
1333 * @adev: amdgpu_device pointer
yanyang15fc3aee2015-05-22 14:39:35 -04001334 * @type: enum amd_ip_block_type
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001335 * @major: major version
1336 * @minor: minor version
1337 *
1338 * return 0 if equal or greater
1339 * return 1 if smaller or the ip_block doesn't exist
1340 */
1341int amdgpu_ip_block_version_cmp(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001342 enum amd_ip_block_type type,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001343 u32 major, u32 minor)
1344{
Alex Deuchera1255102016-10-13 17:41:13 -04001345 struct amdgpu_ip_block *ip_block = amdgpu_get_ip_block(adev, type);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001346
Alex Deuchera1255102016-10-13 17:41:13 -04001347 if (ip_block && ((ip_block->version->major > major) ||
1348 ((ip_block->version->major == major) &&
1349 (ip_block->version->minor >= minor))))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001350 return 0;
1351
1352 return 1;
1353}
1354
Alex Deuchera1255102016-10-13 17:41:13 -04001355/**
1356 * amdgpu_ip_block_add
1357 *
1358 * @adev: amdgpu_device pointer
1359 * @ip_block_version: pointer to the IP to add
1360 *
1361 * Adds the IP block driver information to the collection of IPs
1362 * on the asic.
1363 */
1364int amdgpu_ip_block_add(struct amdgpu_device *adev,
1365 const struct amdgpu_ip_block_version *ip_block_version)
1366{
1367 if (!ip_block_version)
1368 return -EINVAL;
1369
Huang Ruia0bae352017-05-03 09:52:06 +08001370 DRM_DEBUG("add ip block number %d <%s>\n", adev->num_ip_blocks,
1371 ip_block_version->funcs->name);
1372
Alex Deuchera1255102016-10-13 17:41:13 -04001373 adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
1374
1375 return 0;
1376}
1377
Alex Deucher483ef982016-09-30 12:43:04 -04001378static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
Emily Deng9accf2f2016-08-10 16:01:25 +08001379{
1380 adev->enable_virtual_display = false;
1381
1382 if (amdgpu_virtual_display) {
1383 struct drm_device *ddev = adev->ddev;
1384 const char *pci_address_name = pci_name(ddev->pdev);
Emily Deng0f663562016-09-30 13:02:18 -04001385 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
Emily Deng9accf2f2016-08-10 16:01:25 +08001386
1387 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1388 pciaddstr_tmp = pciaddstr;
Emily Deng0f663562016-09-30 13:02:18 -04001389 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1390 pciaddname = strsep(&pciaddname_tmp, ",");
Yintian Tao967de2a2017-01-22 15:16:51 +08001391 if (!strcmp("all", pciaddname)
1392 || !strcmp(pci_address_name, pciaddname)) {
Emily Deng0f663562016-09-30 13:02:18 -04001393 long num_crtc;
1394 int res = -1;
1395
Emily Deng9accf2f2016-08-10 16:01:25 +08001396 adev->enable_virtual_display = true;
Emily Deng0f663562016-09-30 13:02:18 -04001397
1398 if (pciaddname_tmp)
1399 res = kstrtol(pciaddname_tmp, 10,
1400 &num_crtc);
1401
1402 if (!res) {
1403 if (num_crtc < 1)
1404 num_crtc = 1;
1405 if (num_crtc > 6)
1406 num_crtc = 6;
1407 adev->mode_info.num_crtc = num_crtc;
1408 } else {
1409 adev->mode_info.num_crtc = 1;
1410 }
Emily Deng9accf2f2016-08-10 16:01:25 +08001411 break;
1412 }
1413 }
1414
Emily Deng0f663562016-09-30 13:02:18 -04001415 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
1416 amdgpu_virtual_display, pci_address_name,
1417 adev->enable_virtual_display, adev->mode_info.num_crtc);
Emily Deng9accf2f2016-08-10 16:01:25 +08001418
1419 kfree(pciaddstr);
1420 }
1421}
1422
Alex Deuchere2a75f82017-04-27 16:58:01 -04001423static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev)
1424{
Alex Deuchere2a75f82017-04-27 16:58:01 -04001425 const char *chip_name;
1426 char fw_name[30];
1427 int err;
1428 const struct gpu_info_firmware_header_v1_0 *hdr;
1429
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001430 adev->firmware.gpu_info_fw = NULL;
1431
Alex Deuchere2a75f82017-04-27 16:58:01 -04001432 switch (adev->asic_type) {
1433 case CHIP_TOPAZ:
1434 case CHIP_TONGA:
1435 case CHIP_FIJI:
1436 case CHIP_POLARIS11:
1437 case CHIP_POLARIS10:
1438 case CHIP_POLARIS12:
1439 case CHIP_CARRIZO:
1440 case CHIP_STONEY:
1441#ifdef CONFIG_DRM_AMDGPU_SI
1442 case CHIP_VERDE:
1443 case CHIP_TAHITI:
1444 case CHIP_PITCAIRN:
1445 case CHIP_OLAND:
1446 case CHIP_HAINAN:
1447#endif
1448#ifdef CONFIG_DRM_AMDGPU_CIK
1449 case CHIP_BONAIRE:
1450 case CHIP_HAWAII:
1451 case CHIP_KAVERI:
1452 case CHIP_KABINI:
1453 case CHIP_MULLINS:
1454#endif
1455 default:
1456 return 0;
1457 case CHIP_VEGA10:
1458 chip_name = "vega10";
1459 break;
Alex Deucher2d2e5e72017-05-09 12:27:35 -04001460 case CHIP_RAVEN:
1461 chip_name = "raven";
1462 break;
Alex Deuchere2a75f82017-04-27 16:58:01 -04001463 }
1464
1465 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name);
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001466 err = request_firmware(&adev->firmware.gpu_info_fw, fw_name, adev->dev);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001467 if (err) {
1468 dev_err(adev->dev,
1469 "Failed to load gpu_info firmware \"%s\"\n",
1470 fw_name);
1471 goto out;
1472 }
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001473 err = amdgpu_ucode_validate(adev->firmware.gpu_info_fw);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001474 if (err) {
1475 dev_err(adev->dev,
1476 "Failed to validate gpu_info firmware \"%s\"\n",
1477 fw_name);
1478 goto out;
1479 }
1480
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001481 hdr = (const struct gpu_info_firmware_header_v1_0 *)adev->firmware.gpu_info_fw->data;
Alex Deuchere2a75f82017-04-27 16:58:01 -04001482 amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
1483
1484 switch (hdr->version_major) {
1485 case 1:
1486 {
1487 const struct gpu_info_firmware_v1_0 *gpu_info_fw =
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001488 (const struct gpu_info_firmware_v1_0 *)(adev->firmware.gpu_info_fw->data +
Alex Deuchere2a75f82017-04-27 16:58:01 -04001489 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1490
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001491 adev->gfx.config.max_shader_engines = le32_to_cpu(gpu_info_fw->gc_num_se);
1492 adev->gfx.config.max_cu_per_sh = le32_to_cpu(gpu_info_fw->gc_num_cu_per_sh);
1493 adev->gfx.config.max_sh_per_se = le32_to_cpu(gpu_info_fw->gc_num_sh_per_se);
1494 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 -04001495 adev->gfx.config.max_texture_channel_caches =
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001496 le32_to_cpu(gpu_info_fw->gc_num_tccs);
1497 adev->gfx.config.max_gprs = le32_to_cpu(gpu_info_fw->gc_num_gprs);
1498 adev->gfx.config.max_gs_threads = le32_to_cpu(gpu_info_fw->gc_num_max_gs_thds);
1499 adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gpu_info_fw->gc_gs_table_depth);
1500 adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gpu_info_fw->gc_gsprim_buff_depth);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001501 adev->gfx.config.double_offchip_lds_buf =
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001502 le32_to_cpu(gpu_info_fw->gc_double_offchip_lds_buffer);
1503 adev->gfx.cu_info.wave_front_size = le32_to_cpu(gpu_info_fw->gc_wave_size);
Hawking Zhang51fd0372017-06-09 22:30:52 +08001504 adev->gfx.cu_info.max_waves_per_simd =
1505 le32_to_cpu(gpu_info_fw->gc_max_waves_per_simd);
1506 adev->gfx.cu_info.max_scratch_slots_per_cu =
1507 le32_to_cpu(gpu_info_fw->gc_max_scratch_slots_per_cu);
1508 adev->gfx.cu_info.lds_size = le32_to_cpu(gpu_info_fw->gc_lds_size);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001509 break;
1510 }
1511 default:
1512 dev_err(adev->dev,
1513 "Unsupported gpu_info table %d\n", hdr->header.ucode_version);
1514 err = -EINVAL;
1515 goto out;
1516 }
1517out:
Alex Deuchere2a75f82017-04-27 16:58:01 -04001518 return err;
1519}
1520
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001521static int amdgpu_early_init(struct amdgpu_device *adev)
1522{
Alex Deucheraaa36a92015-04-20 17:31:14 -04001523 int i, r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001524
Alex Deucher483ef982016-09-30 12:43:04 -04001525 amdgpu_device_enable_virtual_display(adev);
Emily Denga6be7572016-08-08 11:37:50 +08001526
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001527 switch (adev->asic_type) {
Alex Deucheraaa36a92015-04-20 17:31:14 -04001528 case CHIP_TOPAZ:
1529 case CHIP_TONGA:
David Zhang48299f92015-07-08 01:05:16 +08001530 case CHIP_FIJI:
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001531 case CHIP_POLARIS11:
1532 case CHIP_POLARIS10:
Junwei Zhangc4642a42016-12-14 15:32:28 -05001533 case CHIP_POLARIS12:
Alex Deucheraaa36a92015-04-20 17:31:14 -04001534 case CHIP_CARRIZO:
Samuel Li39bb0c92015-10-08 16:31:43 -04001535 case CHIP_STONEY:
1536 if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
Alex Deucheraaa36a92015-04-20 17:31:14 -04001537 adev->family = AMDGPU_FAMILY_CZ;
1538 else
1539 adev->family = AMDGPU_FAMILY_VI;
1540
1541 r = vi_set_ip_blocks(adev);
1542 if (r)
1543 return r;
1544 break;
Ken Wang33f34802016-01-21 17:29:41 +08001545#ifdef CONFIG_DRM_AMDGPU_SI
1546 case CHIP_VERDE:
1547 case CHIP_TAHITI:
1548 case CHIP_PITCAIRN:
1549 case CHIP_OLAND:
1550 case CHIP_HAINAN:
Ken Wang295d0da2016-05-24 21:02:53 +08001551 adev->family = AMDGPU_FAMILY_SI;
Ken Wang33f34802016-01-21 17:29:41 +08001552 r = si_set_ip_blocks(adev);
1553 if (r)
1554 return r;
1555 break;
1556#endif
Alex Deuchera2e73f52015-04-20 17:09:27 -04001557#ifdef CONFIG_DRM_AMDGPU_CIK
1558 case CHIP_BONAIRE:
1559 case CHIP_HAWAII:
1560 case CHIP_KAVERI:
1561 case CHIP_KABINI:
1562 case CHIP_MULLINS:
1563 if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
1564 adev->family = AMDGPU_FAMILY_CI;
1565 else
1566 adev->family = AMDGPU_FAMILY_KV;
1567
1568 r = cik_set_ip_blocks(adev);
1569 if (r)
1570 return r;
1571 break;
1572#endif
Chunming Zhou2ca8a5d2016-12-07 17:31:19 +08001573 case CHIP_VEGA10:
1574 case CHIP_RAVEN:
1575 if (adev->asic_type == CHIP_RAVEN)
1576 adev->family = AMDGPU_FAMILY_RV;
1577 else
1578 adev->family = AMDGPU_FAMILY_AI;
Ken Wang460826e2017-03-06 14:53:16 -05001579
1580 r = soc15_set_ip_blocks(adev);
1581 if (r)
1582 return r;
1583 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001584 default:
1585 /* FIXME: not supported yet */
1586 return -EINVAL;
1587 }
1588
Alex Deuchere2a75f82017-04-27 16:58:01 -04001589 r = amdgpu_device_parse_gpu_info_fw(adev);
1590 if (r)
1591 return r;
1592
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001593 if (amdgpu_sriov_vf(adev)) {
1594 r = amdgpu_virt_request_full_gpu(adev, true);
1595 if (r)
1596 return r;
1597 }
1598
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001599 for (i = 0; i < adev->num_ip_blocks; i++) {
1600 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
Huang Ruied8cf002017-05-03 09:40:17 +08001601 DRM_ERROR("disabled ip block: %d <%s>\n",
1602 i, adev->ip_blocks[i].version->funcs->name);
Alex Deuchera1255102016-10-13 17:41:13 -04001603 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001604 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001605 if (adev->ip_blocks[i].version->funcs->early_init) {
1606 r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001607 if (r == -ENOENT) {
Alex Deuchera1255102016-10-13 17:41:13 -04001608 adev->ip_blocks[i].status.valid = false;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001609 } else if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001610 DRM_ERROR("early_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 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001614 adev->ip_blocks[i].status.valid = true;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001615 }
Alex Deucher974e6b62015-07-10 13:59:44 -04001616 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001617 adev->ip_blocks[i].status.valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001618 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001619 }
1620 }
1621
Nicolai Hähnle395d1fb2016-06-02 12:32:07 +02001622 adev->cg_flags &= amdgpu_cg_mask;
1623 adev->pg_flags &= amdgpu_pg_mask;
1624
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001625 return 0;
1626}
1627
1628static int amdgpu_init(struct amdgpu_device *adev)
1629{
1630 int i, r;
1631
1632 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001633 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001634 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001635 r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001636 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001637 DRM_ERROR("sw_init of IP block <%s> failed %d\n",
1638 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001639 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001640 }
Alex Deuchera1255102016-10-13 17:41:13 -04001641 adev->ip_blocks[i].status.sw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001642 /* need to do gmc hw init early so we can allocate gpu mem */
Alex Deuchera1255102016-10-13 17:41:13 -04001643 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001644 r = amdgpu_vram_scratch_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001645 if (r) {
1646 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001647 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001648 }
Alex Deuchera1255102016-10-13 17:41:13 -04001649 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001650 if (r) {
1651 DRM_ERROR("hw_init %d failed %d\n", i, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001652 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001653 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001654 r = amdgpu_wb_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001655 if (r) {
1656 DRM_ERROR("amdgpu_wb_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001657 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001658 }
Alex Deuchera1255102016-10-13 17:41:13 -04001659 adev->ip_blocks[i].status.hw = true;
Monk Liu24936642017-01-09 15:54:32 +08001660
1661 /* right after GMC hw init, we create CSA */
1662 if (amdgpu_sriov_vf(adev)) {
1663 r = amdgpu_allocate_static_csa(adev);
1664 if (r) {
1665 DRM_ERROR("allocate CSA failed %d\n", r);
1666 return r;
1667 }
1668 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001669 }
1670 }
1671
1672 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001673 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001674 continue;
1675 /* gmc hw init is done early */
Alex Deuchera1255102016-10-13 17:41:13 -04001676 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001677 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001678 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001679 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001680 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
1681 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001682 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001683 }
Alex Deuchera1255102016-10-13 17:41:13 -04001684 adev->ip_blocks[i].status.hw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001685 }
1686
1687 return 0;
1688}
1689
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08001690static void amdgpu_fill_reset_magic(struct amdgpu_device *adev)
1691{
1692 memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM);
1693}
1694
1695static bool amdgpu_check_vram_lost(struct amdgpu_device *adev)
1696{
1697 return !!memcmp(adev->gart.ptr, adev->reset_magic,
1698 AMDGPU_RESET_MAGIC_NUM);
1699}
1700
Shirish S2dc80b02017-05-25 10:05:25 +05301701static int amdgpu_late_set_cg_state(struct amdgpu_device *adev)
1702{
1703 int i = 0, r;
1704
1705 for (i = 0; i < adev->num_ip_blocks; i++) {
1706 if (!adev->ip_blocks[i].status.valid)
1707 continue;
1708 /* skip CG for VCE/UVD, it's handled specially */
1709 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1710 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1711 /* enable clockgating to save power */
1712 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1713 AMD_CG_STATE_GATE);
1714 if (r) {
1715 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
1716 adev->ip_blocks[i].version->funcs->name, r);
1717 return r;
1718 }
1719 }
1720 }
1721 return 0;
1722}
1723
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001724static int amdgpu_late_init(struct amdgpu_device *adev)
1725{
1726 int i = 0, r;
1727
1728 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001729 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001730 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001731 if (adev->ip_blocks[i].version->funcs->late_init) {
1732 r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001733 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001734 DRM_ERROR("late_init of IP block <%s> failed %d\n",
1735 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001736 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001737 }
Alex Deuchera1255102016-10-13 17:41:13 -04001738 adev->ip_blocks[i].status.late_initialized = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001739 }
1740 }
1741
Shirish S2dc80b02017-05-25 10:05:25 +05301742 mod_delayed_work(system_wq, &adev->late_init_work,
1743 msecs_to_jiffies(AMDGPU_RESUME_MS));
1744
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08001745 amdgpu_fill_reset_magic(adev);
1746
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001747 return 0;
1748}
1749
1750static int amdgpu_fini(struct amdgpu_device *adev)
1751{
1752 int i, r;
1753
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001754 /* need to disable SMC first */
1755 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001756 if (!adev->ip_blocks[i].status.hw)
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001757 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001758 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001759 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
Alex Deuchera1255102016-10-13 17:41:13 -04001760 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1761 AMD_CG_STATE_UNGATE);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001762 if (r) {
1763 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001764 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001765 return r;
1766 }
Alex Deuchera1255102016-10-13 17:41:13 -04001767 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001768 /* XXX handle errors */
1769 if (r) {
1770 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001771 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001772 }
Alex Deuchera1255102016-10-13 17:41:13 -04001773 adev->ip_blocks[i].status.hw = false;
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001774 break;
1775 }
1776 }
1777
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001778 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001779 if (!adev->ip_blocks[i].status.hw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001780 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001781 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001782 amdgpu_wb_fini(adev);
1783 amdgpu_vram_scratch_fini(adev);
1784 }
Rex Zhu8201a672016-11-24 21:44:44 +08001785
1786 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1787 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1788 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
1789 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1790 AMD_CG_STATE_UNGATE);
1791 if (r) {
1792 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1793 adev->ip_blocks[i].version->funcs->name, r);
1794 return r;
1795 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001796 }
Rex Zhu8201a672016-11-24 21:44:44 +08001797
Alex Deuchera1255102016-10-13 17:41:13 -04001798 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001799 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001800 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001801 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
1802 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001803 }
Rex Zhu8201a672016-11-24 21:44:44 +08001804
Alex Deuchera1255102016-10-13 17:41:13 -04001805 adev->ip_blocks[i].status.hw = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001806 }
1807
1808 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001809 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001810 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001811 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001812 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001813 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001814 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
1815 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001816 }
Alex Deuchera1255102016-10-13 17:41:13 -04001817 adev->ip_blocks[i].status.sw = false;
1818 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001819 }
1820
Monk Liua6dcfd92016-05-19 14:36:34 +08001821 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001822 if (!adev->ip_blocks[i].status.late_initialized)
Grazvydas Ignotas8a2eef12016-10-03 00:06:44 +03001823 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001824 if (adev->ip_blocks[i].version->funcs->late_fini)
1825 adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
1826 adev->ip_blocks[i].status.late_initialized = false;
Monk Liua6dcfd92016-05-19 14:36:34 +08001827 }
1828
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001829 if (amdgpu_sriov_vf(adev)) {
Monk Liu24936642017-01-09 15:54:32 +08001830 amdgpu_bo_free_kernel(&adev->virt.csa_obj, &adev->virt.csa_vmid0_addr, NULL);
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001831 amdgpu_virt_release_full_gpu(adev, false);
1832 }
Monk Liu24936642017-01-09 15:54:32 +08001833
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001834 return 0;
1835}
1836
Shirish S2dc80b02017-05-25 10:05:25 +05301837static void amdgpu_late_init_func_handler(struct work_struct *work)
1838{
1839 struct amdgpu_device *adev =
1840 container_of(work, struct amdgpu_device, late_init_work.work);
1841 amdgpu_late_set_cg_state(adev);
1842}
1843
Alex Deucherfaefba92016-12-06 10:38:29 -05001844int amdgpu_suspend(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001845{
1846 int i, r;
1847
Xiangliang Yue941ea92017-01-18 12:47:55 +08001848 if (amdgpu_sriov_vf(adev))
1849 amdgpu_virt_request_full_gpu(adev, false);
1850
Flora Cuic5a93a22016-02-26 10:45:25 +08001851 /* ungate SMC block first */
1852 r = amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC,
1853 AMD_CG_STATE_UNGATE);
1854 if (r) {
1855 DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n",r);
1856 }
1857
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001858 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001859 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001860 continue;
1861 /* ungate blocks so that suspend can properly shut them down */
Flora Cuic5a93a22016-02-26 10:45:25 +08001862 if (i != AMD_IP_BLOCK_TYPE_SMC) {
Alex Deuchera1255102016-10-13 17:41:13 -04001863 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1864 AMD_CG_STATE_UNGATE);
Flora Cuic5a93a22016-02-26 10:45:25 +08001865 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001866 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1867 adev->ip_blocks[i].version->funcs->name, r);
Flora Cuic5a93a22016-02-26 10:45:25 +08001868 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001869 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001870 /* XXX handle errors */
Alex Deuchera1255102016-10-13 17:41:13 -04001871 r = adev->ip_blocks[i].version->funcs->suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001872 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001873 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001874 DRM_ERROR("suspend of IP block <%s> failed %d\n",
1875 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001876 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001877 }
1878
Xiangliang Yue941ea92017-01-18 12:47:55 +08001879 if (amdgpu_sriov_vf(adev))
1880 amdgpu_virt_release_full_gpu(adev, false);
1881
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001882 return 0;
1883}
1884
Monk Liue4f0fdc2017-02-09 11:55:49 +08001885static int amdgpu_sriov_reinit_early(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08001886{
1887 int i, r;
1888
Monk Liu2cb681b2017-04-26 12:00:49 +08001889 static enum amd_ip_block_type ip_order[] = {
1890 AMD_IP_BLOCK_TYPE_GMC,
1891 AMD_IP_BLOCK_TYPE_COMMON,
Monk Liu2cb681b2017-04-26 12:00:49 +08001892 AMD_IP_BLOCK_TYPE_IH,
1893 };
Monk Liua90ad3c2017-01-23 14:22:08 +08001894
Monk Liu2cb681b2017-04-26 12:00:49 +08001895 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1896 int j;
1897 struct amdgpu_ip_block *block;
Monk Liua90ad3c2017-01-23 14:22:08 +08001898
Monk Liu2cb681b2017-04-26 12:00:49 +08001899 for (j = 0; j < adev->num_ip_blocks; j++) {
1900 block = &adev->ip_blocks[j];
1901
1902 if (block->version->type != ip_order[i] ||
1903 !block->status.valid)
1904 continue;
1905
1906 r = block->version->funcs->hw_init(adev);
1907 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
Monk Liua90ad3c2017-01-23 14:22:08 +08001908 }
1909 }
1910
1911 return 0;
1912}
1913
Monk Liue4f0fdc2017-02-09 11:55:49 +08001914static int amdgpu_sriov_reinit_late(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08001915{
1916 int i, r;
1917
Monk Liu2cb681b2017-04-26 12:00:49 +08001918 static enum amd_ip_block_type ip_order[] = {
1919 AMD_IP_BLOCK_TYPE_SMC,
1920 AMD_IP_BLOCK_TYPE_DCE,
1921 AMD_IP_BLOCK_TYPE_GFX,
1922 AMD_IP_BLOCK_TYPE_SDMA,
1923 AMD_IP_BLOCK_TYPE_VCE,
1924 };
Monk Liua90ad3c2017-01-23 14:22:08 +08001925
Monk Liu2cb681b2017-04-26 12:00:49 +08001926 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1927 int j;
1928 struct amdgpu_ip_block *block;
Monk Liua90ad3c2017-01-23 14:22:08 +08001929
Monk Liu2cb681b2017-04-26 12:00:49 +08001930 for (j = 0; j < adev->num_ip_blocks; j++) {
1931 block = &adev->ip_blocks[j];
1932
1933 if (block->version->type != ip_order[i] ||
1934 !block->status.valid)
1935 continue;
1936
1937 r = block->version->funcs->hw_init(adev);
1938 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
Monk Liua90ad3c2017-01-23 14:22:08 +08001939 }
1940 }
1941
1942 return 0;
1943}
1944
Chunming Zhoufcf06492017-05-05 10:33:33 +08001945static int amdgpu_resume_phase1(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001946{
1947 int i, r;
1948
1949 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001950 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001951 continue;
Chunming Zhoufcf06492017-05-05 10:33:33 +08001952 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
1953 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
1954 adev->ip_blocks[i].version->type ==
1955 AMD_IP_BLOCK_TYPE_IH) {
1956 r = adev->ip_blocks[i].version->funcs->resume(adev);
1957 if (r) {
1958 DRM_ERROR("resume of IP block <%s> failed %d\n",
1959 adev->ip_blocks[i].version->funcs->name, r);
1960 return r;
1961 }
1962 }
1963 }
1964
1965 return 0;
1966}
1967
1968static int amdgpu_resume_phase2(struct amdgpu_device *adev)
1969{
1970 int i, r;
1971
1972 for (i = 0; i < adev->num_ip_blocks; i++) {
1973 if (!adev->ip_blocks[i].status.valid)
1974 continue;
1975 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
1976 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
1977 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH )
1978 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001979 r = adev->ip_blocks[i].version->funcs->resume(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001980 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001981 DRM_ERROR("resume of IP block <%s> failed %d\n",
1982 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001983 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001984 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001985 }
1986
1987 return 0;
1988}
1989
Chunming Zhoufcf06492017-05-05 10:33:33 +08001990static int amdgpu_resume(struct amdgpu_device *adev)
1991{
1992 int r;
1993
1994 r = amdgpu_resume_phase1(adev);
1995 if (r)
1996 return r;
1997 r = amdgpu_resume_phase2(adev);
1998
1999 return r;
2000}
2001
Monk Liu4e99a442016-03-31 13:26:59 +08002002static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
Andres Rodriguez048765a2016-06-11 02:51:32 -04002003{
Alex Deuchera5bde2f2016-09-23 16:23:41 -04002004 if (adev->is_atom_fw) {
2005 if (amdgpu_atomfirmware_gpu_supports_virtualization(adev))
2006 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
2007 } else {
2008 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
2009 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
2010 }
Andres Rodriguez048765a2016-06-11 02:51:32 -04002011}
2012
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002013/**
2014 * amdgpu_device_init - initialize the driver
2015 *
2016 * @adev: amdgpu_device pointer
2017 * @pdev: drm dev pointer
2018 * @pdev: pci dev pointer
2019 * @flags: driver flags
2020 *
2021 * Initializes the driver info and hw (all asics).
2022 * Returns 0 for success or an error on failure.
2023 * Called at driver startup.
2024 */
2025int amdgpu_device_init(struct amdgpu_device *adev,
2026 struct drm_device *ddev,
2027 struct pci_dev *pdev,
2028 uint32_t flags)
2029{
2030 int r, i;
2031 bool runtime = false;
Marek Olšák95844d22016-08-17 23:49:27 +02002032 u32 max_MBps;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002033
2034 adev->shutdown = false;
2035 adev->dev = &pdev->dev;
2036 adev->ddev = ddev;
2037 adev->pdev = pdev;
2038 adev->flags = flags;
Jammy Zhou2f7d10b2015-07-22 11:29:01 +08002039 adev->asic_type = flags & AMD_ASIC_MASK;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002040 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
Christian König6f02a692017-07-07 11:56:59 +02002041 adev->mc.gart_size = 512 * 1024 * 1024;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002042 adev->accel_working = false;
2043 adev->num_rings = 0;
2044 adev->mman.buffer_funcs = NULL;
2045 adev->mman.buffer_funcs_ring = NULL;
2046 adev->vm_manager.vm_pte_funcs = NULL;
Christian König2d55e452016-02-08 17:37:38 +01002047 adev->vm_manager.vm_pte_num_rings = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002048 adev->gart.gart_funcs = NULL;
Chris Wilsonf54d1862016-10-25 13:00:45 +01002049 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002050
2051 adev->smc_rreg = &amdgpu_invalid_rreg;
2052 adev->smc_wreg = &amdgpu_invalid_wreg;
2053 adev->pcie_rreg = &amdgpu_invalid_rreg;
2054 adev->pcie_wreg = &amdgpu_invalid_wreg;
Huang Rui36b9a952016-08-31 13:23:25 +08002055 adev->pciep_rreg = &amdgpu_invalid_rreg;
2056 adev->pciep_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002057 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
2058 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
2059 adev->didt_rreg = &amdgpu_invalid_rreg;
2060 adev->didt_wreg = &amdgpu_invalid_wreg;
Rex Zhuccdbb202016-06-08 12:47:41 +08002061 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
2062 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002063 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
2064 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
2065
Rex Zhuccdbb202016-06-08 12:47:41 +08002066
Alex Deucher3e39ab92015-06-05 15:04:33 -04002067 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
2068 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
2069 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002070
2071 /* mutex initialization are all done here so we
2072 * can recall function without having locking issues */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002073 atomic_set(&adev->irq.ih.lock, 0);
Huang Rui0e5ca0d2017-03-03 18:37:23 -05002074 mutex_init(&adev->firmware.mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002075 mutex_init(&adev->pm.mutex);
2076 mutex_init(&adev->gfx.gpu_clock_mutex);
2077 mutex_init(&adev->srbm_mutex);
2078 mutex_init(&adev->grbm_idx_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002079 mutex_init(&adev->mn_lock);
2080 hash_init(adev->mn_hash);
2081
2082 amdgpu_check_arguments(adev);
2083
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002084 spin_lock_init(&adev->mmio_idx_lock);
2085 spin_lock_init(&adev->smc_idx_lock);
2086 spin_lock_init(&adev->pcie_idx_lock);
2087 spin_lock_init(&adev->uvd_ctx_idx_lock);
2088 spin_lock_init(&adev->didt_idx_lock);
Rex Zhuccdbb202016-06-08 12:47:41 +08002089 spin_lock_init(&adev->gc_cac_idx_lock);
Evan Quan16abb5d2017-07-04 09:21:50 +08002090 spin_lock_init(&adev->se_cac_idx_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002091 spin_lock_init(&adev->audio_endpt_idx_lock);
Marek Olšák95844d22016-08-17 23:49:27 +02002092 spin_lock_init(&adev->mm_stats.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002093
Chunming Zhou0c4e7fa2016-08-17 11:41:30 +08002094 INIT_LIST_HEAD(&adev->shadow_list);
2095 mutex_init(&adev->shadow_list_lock);
2096
Chunming Zhou5c1354b2016-08-30 16:13:10 +08002097 INIT_LIST_HEAD(&adev->gtt_list);
2098 spin_lock_init(&adev->gtt_list_lock);
2099
Andres Rodriguez795f2812017-03-06 16:27:55 -05002100 INIT_LIST_HEAD(&adev->ring_lru_list);
2101 spin_lock_init(&adev->ring_lru_list_lock);
2102
Shirish S2dc80b02017-05-25 10:05:25 +05302103 INIT_DELAYED_WORK(&adev->late_init_work, amdgpu_late_init_func_handler);
2104
Alex Xie0fa49552017-06-08 14:58:05 -04002105 /* Registers mapping */
2106 /* TODO: block userspace mapping of io register */
Ken Wangda69c1612016-01-21 19:08:55 +08002107 if (adev->asic_type >= CHIP_BONAIRE) {
2108 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
2109 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
2110 } else {
2111 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
2112 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
2113 }
Chunming Zhou5c1354b2016-08-30 16:13:10 +08002114
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002115 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
2116 if (adev->rmmio == NULL) {
2117 return -ENOMEM;
2118 }
2119 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
2120 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
2121
Ken Wangda69c1612016-01-21 19:08:55 +08002122 if (adev->asic_type >= CHIP_BONAIRE)
2123 /* doorbell bar mapping */
2124 amdgpu_doorbell_init(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002125
2126 /* io port mapping */
2127 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
2128 if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
2129 adev->rio_mem_size = pci_resource_len(adev->pdev, i);
2130 adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
2131 break;
2132 }
2133 }
2134 if (adev->rio_mem == NULL)
Amber Linb64a18c2017-01-04 08:06:58 -05002135 DRM_INFO("PCI I/O BAR is not found.\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002136
2137 /* early init functions */
2138 r = amdgpu_early_init(adev);
2139 if (r)
2140 return r;
2141
2142 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
2143 /* this will fail for cards that aren't VGA class devices, just
2144 * ignore it */
2145 vga_client_register(adev->pdev, adev, NULL, amdgpu_vga_set_decode);
2146
2147 if (amdgpu_runtime_pm == 1)
2148 runtime = true;
Alex Deuchere9bef452016-04-25 13:12:18 -04002149 if (amdgpu_device_is_px(ddev))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002150 runtime = true;
Lukas Wunner84c8b222017-03-10 21:23:45 +01002151 if (!pci_is_thunderbolt_attached(adev->pdev))
2152 vga_switcheroo_register_client(adev->pdev,
2153 &amdgpu_switcheroo_ops, runtime);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002154 if (runtime)
2155 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
2156
2157 /* Read BIOS */
Alex Deucher83ba1262016-06-03 18:21:41 -04002158 if (!amdgpu_get_bios(adev)) {
2159 r = -EINVAL;
2160 goto failed;
2161 }
Nils Wallméniusf7e9e9f2016-12-14 21:52:45 +01002162
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002163 r = amdgpu_atombios_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002164 if (r) {
2165 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
Gavin Wan89041942017-06-23 13:55:15 -04002166 amdgpu_vf_error_put(AMDGIM_ERROR_VF_ATOMBIOS_INIT_FAIL, 0, 0);
Alex Deucher83ba1262016-06-03 18:21:41 -04002167 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002168 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002169
Monk Liu4e99a442016-03-31 13:26:59 +08002170 /* detect if we are with an SRIOV vbios */
2171 amdgpu_device_detect_sriov_bios(adev);
Andres Rodriguez048765a2016-06-11 02:51:32 -04002172
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002173 /* Post card if necessary */
Monk Liubec86372016-09-14 19:38:08 +08002174 if (amdgpu_vpost_needed(adev)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002175 if (!adev->bios) {
Monk Liubec86372016-09-14 19:38:08 +08002176 dev_err(adev->dev, "no vBIOS found\n");
Gavin Wan89041942017-06-23 13:55:15 -04002177 amdgpu_vf_error_put(AMDGIM_ERROR_VF_NO_VBIOS, 0, 0);
Alex Deucher83ba1262016-06-03 18:21:41 -04002178 r = -EINVAL;
2179 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002180 }
Monk Liubec86372016-09-14 19:38:08 +08002181 DRM_INFO("GPU posting now...\n");
Monk Liu4e99a442016-03-31 13:26:59 +08002182 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2183 if (r) {
2184 dev_err(adev->dev, "gpu post error!\n");
Gavin Wan89041942017-06-23 13:55:15 -04002185 amdgpu_vf_error_put(AMDGIM_ERROR_VF_GPU_POST_ERROR, 0, 0);
Monk Liu4e99a442016-03-31 13:26:59 +08002186 goto failed;
2187 }
2188 } else {
2189 DRM_INFO("GPU post is not needed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002190 }
2191
Alex Deuchera5bde2f2016-09-23 16:23:41 -04002192 if (!adev->is_atom_fw) {
2193 /* Initialize clocks */
2194 r = amdgpu_atombios_get_clock_info(adev);
2195 if (r) {
2196 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
Gavin Wan89041942017-06-23 13:55:15 -04002197 amdgpu_vf_error_put(AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
2198 goto failed;
Alex Deuchera5bde2f2016-09-23 16:23:41 -04002199 }
2200 /* init i2c buses */
2201 amdgpu_atombios_i2c_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002202 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002203
2204 /* Fence driver */
2205 r = amdgpu_fence_driver_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002206 if (r) {
2207 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
Gavin Wan89041942017-06-23 13:55:15 -04002208 amdgpu_vf_error_put(AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0);
Alex Deucher83ba1262016-06-03 18:21:41 -04002209 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002210 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002211
2212 /* init the mode config */
2213 drm_mode_config_init(adev->ddev);
2214
2215 r = amdgpu_init(adev);
2216 if (r) {
Alex Deucher2c1a2782015-12-07 17:02:53 -05002217 dev_err(adev->dev, "amdgpu_init failed\n");
Gavin Wan89041942017-06-23 13:55:15 -04002218 amdgpu_vf_error_put(AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002219 amdgpu_fini(adev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002220 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002221 }
2222
2223 adev->accel_working = true;
2224
Alex Xiee59c0202017-06-01 09:42:59 -04002225 amdgpu_vm_check_compute_bug(adev);
2226
Marek Olšák95844d22016-08-17 23:49:27 +02002227 /* Initialize the buffer migration limit. */
2228 if (amdgpu_moverate >= 0)
2229 max_MBps = amdgpu_moverate;
2230 else
2231 max_MBps = 8; /* Allow 8 MB/s. */
2232 /* Get a log2 for easy divisions. */
2233 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
2234
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002235 r = amdgpu_ib_pool_init(adev);
2236 if (r) {
2237 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
Gavin Wan89041942017-06-23 13:55:15 -04002238 amdgpu_vf_error_put(AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r);
Alex Deucher83ba1262016-06-03 18:21:41 -04002239 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002240 }
2241
2242 r = amdgpu_ib_ring_tests(adev);
2243 if (r)
2244 DRM_ERROR("ib ring test failed (%d).\n", r);
2245
Monk Liu9bc92b92017-02-08 17:38:13 +08002246 amdgpu_fbdev_init(adev);
2247
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002248 r = amdgpu_gem_debugfs_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002249 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002250 DRM_ERROR("registering gem debugfs failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002251
2252 r = amdgpu_debugfs_regs_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002253 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002254 DRM_ERROR("registering register debugfs failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002255
Huang Rui4f0955f2017-05-10 23:04:06 +08002256 r = amdgpu_debugfs_test_ib_ring_init(adev);
2257 if (r)
2258 DRM_ERROR("registering register test ib ring debugfs failed (%d).\n", r);
2259
Huang Rui50ab2532016-06-12 15:51:09 +08002260 r = amdgpu_debugfs_firmware_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002261 if (r)
Huang Rui50ab2532016-06-12 15:51:09 +08002262 DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
Huang Rui50ab2532016-06-12 15:51:09 +08002263
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002264 if ((amdgpu_testing & 1)) {
2265 if (adev->accel_working)
2266 amdgpu_test_moves(adev);
2267 else
2268 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
2269 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002270 if (amdgpu_benchmarking) {
2271 if (adev->accel_working)
2272 amdgpu_benchmark(adev, amdgpu_benchmarking);
2273 else
2274 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
2275 }
2276
2277 /* enable clockgating, etc. after ib tests, etc. since some blocks require
2278 * explicit gating rather than handling it automatically.
2279 */
2280 r = amdgpu_late_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002281 if (r) {
2282 dev_err(adev->dev, "amdgpu_late_init failed\n");
Gavin Wan89041942017-06-23 13:55:15 -04002283 amdgpu_vf_error_put(AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r);
Alex Deucher83ba1262016-06-03 18:21:41 -04002284 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002285 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002286
2287 return 0;
Alex Deucher83ba1262016-06-03 18:21:41 -04002288
2289failed:
Gavin Wan89041942017-06-23 13:55:15 -04002290 amdgpu_vf_error_trans_all(adev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002291 if (runtime)
2292 vga_switcheroo_fini_domain_pm_ops(adev->dev);
2293 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002294}
2295
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002296/**
2297 * amdgpu_device_fini - tear down the driver
2298 *
2299 * @adev: amdgpu_device pointer
2300 *
2301 * Tear down the driver info (all asics).
2302 * Called at driver shutdown.
2303 */
2304void amdgpu_device_fini(struct amdgpu_device *adev)
2305{
2306 int r;
2307
2308 DRM_INFO("amdgpu: finishing device.\n");
2309 adev->shutdown = true;
Pixel Dingdb2c2a92017-04-25 16:47:42 +08002310 if (adev->mode_info.mode_config_initialized)
2311 drm_crtc_force_disable_all(adev->ddev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002312 /* evict vram memory */
2313 amdgpu_bo_evict_vram(adev);
2314 amdgpu_ib_pool_fini(adev);
2315 amdgpu_fence_driver_fini(adev);
2316 amdgpu_fbdev_fini(adev);
2317 r = amdgpu_fini(adev);
Huang Ruiab4fe3e2017-06-05 22:11:59 +08002318 if (adev->firmware.gpu_info_fw) {
2319 release_firmware(adev->firmware.gpu_info_fw);
2320 adev->firmware.gpu_info_fw = NULL;
2321 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002322 adev->accel_working = false;
Shirish S2dc80b02017-05-25 10:05:25 +05302323 cancel_delayed_work_sync(&adev->late_init_work);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002324 /* free i2c buses */
2325 amdgpu_i2c_fini(adev);
2326 amdgpu_atombios_fini(adev);
2327 kfree(adev->bios);
2328 adev->bios = NULL;
Lukas Wunner84c8b222017-03-10 21:23:45 +01002329 if (!pci_is_thunderbolt_attached(adev->pdev))
2330 vga_switcheroo_unregister_client(adev->pdev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002331 if (adev->flags & AMD_IS_PX)
2332 vga_switcheroo_fini_domain_pm_ops(adev->dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002333 vga_client_register(adev->pdev, NULL, NULL, NULL);
2334 if (adev->rio_mem)
2335 pci_iounmap(adev->pdev, adev->rio_mem);
2336 adev->rio_mem = NULL;
2337 iounmap(adev->rmmio);
2338 adev->rmmio = NULL;
Ken Wangda69c1612016-01-21 19:08:55 +08002339 if (adev->asic_type >= CHIP_BONAIRE)
2340 amdgpu_doorbell_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002341 amdgpu_debugfs_regs_cleanup(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002342}
2343
2344
2345/*
2346 * Suspend & resume.
2347 */
2348/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002349 * amdgpu_device_suspend - initiate device suspend
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002350 *
2351 * @pdev: drm dev pointer
2352 * @state: suspend state
2353 *
2354 * Puts the hw in the suspend state (all asics).
2355 * Returns 0 for success or an error on failure.
2356 * Called at driver suspend.
2357 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002358int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002359{
2360 struct amdgpu_device *adev;
2361 struct drm_crtc *crtc;
2362 struct drm_connector *connector;
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002363 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002364
2365 if (dev == NULL || dev->dev_private == NULL) {
2366 return -ENODEV;
2367 }
2368
2369 adev = dev->dev_private;
2370
2371 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2372 return 0;
2373
2374 drm_kms_helper_poll_disable(dev);
2375
2376 /* turn off display hw */
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002377 drm_modeset_lock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002378 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2379 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
2380 }
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002381 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002382
Yong Zhaoba997702015-11-09 17:21:45 -05002383 amdgpu_amdkfd_suspend(adev);
2384
Alex Deucher756e6882015-10-08 00:03:36 -04002385 /* unpin the front buffers and cursors */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002386 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Alex Deucher756e6882015-10-08 00:03:36 -04002387 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002388 struct amdgpu_framebuffer *rfb = to_amdgpu_framebuffer(crtc->primary->fb);
2389 struct amdgpu_bo *robj;
2390
Alex Deucher756e6882015-10-08 00:03:36 -04002391 if (amdgpu_crtc->cursor_bo) {
2392 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
Alex Xie7a6901d2017-04-24 13:52:41 -04002393 r = amdgpu_bo_reserve(aobj, true);
Alex Deucher756e6882015-10-08 00:03:36 -04002394 if (r == 0) {
2395 amdgpu_bo_unpin(aobj);
2396 amdgpu_bo_unreserve(aobj);
2397 }
2398 }
2399
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002400 if (rfb == NULL || rfb->obj == NULL) {
2401 continue;
2402 }
2403 robj = gem_to_amdgpu_bo(rfb->obj);
2404 /* don't unpin kernel fb objects */
2405 if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
Alex Xie7a6901d2017-04-24 13:52:41 -04002406 r = amdgpu_bo_reserve(robj, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002407 if (r == 0) {
2408 amdgpu_bo_unpin(robj);
2409 amdgpu_bo_unreserve(robj);
2410 }
2411 }
2412 }
2413 /* evict vram memory */
2414 amdgpu_bo_evict_vram(adev);
2415
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002416 amdgpu_fence_driver_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002417
2418 r = amdgpu_suspend(adev);
2419
Alex Deuchera0a71e42016-10-10 12:41:36 -04002420 /* evict remaining vram memory
2421 * This second call to evict vram is to evict the gart page table
2422 * using the CPU.
2423 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002424 amdgpu_bo_evict_vram(adev);
2425
Alex Deucherd05da0e2017-06-30 17:08:45 -04002426 amdgpu_atombios_scratch_regs_save(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002427 pci_save_state(dev->pdev);
2428 if (suspend) {
2429 /* Shut down the device */
2430 pci_disable_device(dev->pdev);
2431 pci_set_power_state(dev->pdev, PCI_D3hot);
jimqu74b0b152016-09-07 17:09:12 +08002432 } else {
2433 r = amdgpu_asic_reset(adev);
2434 if (r)
2435 DRM_ERROR("amdgpu asic reset failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002436 }
2437
2438 if (fbcon) {
2439 console_lock();
2440 amdgpu_fbdev_set_suspend(adev, 1);
2441 console_unlock();
2442 }
2443 return 0;
2444}
2445
2446/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002447 * amdgpu_device_resume - initiate device resume
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002448 *
2449 * @pdev: drm dev pointer
2450 *
2451 * Bring the hw back to operating state (all asics).
2452 * Returns 0 for success or an error on failure.
2453 * Called at driver resume.
2454 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002455int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002456{
2457 struct drm_connector *connector;
2458 struct amdgpu_device *adev = dev->dev_private;
Alex Deucher756e6882015-10-08 00:03:36 -04002459 struct drm_crtc *crtc;
Huang Rui03161a62017-04-13 16:12:26 +08002460 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002461
2462 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2463 return 0;
2464
jimqu74b0b152016-09-07 17:09:12 +08002465 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002466 console_lock();
jimqu74b0b152016-09-07 17:09:12 +08002467
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002468 if (resume) {
2469 pci_set_power_state(dev->pdev, PCI_D0);
2470 pci_restore_state(dev->pdev);
jimqu74b0b152016-09-07 17:09:12 +08002471 r = pci_enable_device(dev->pdev);
Huang Rui03161a62017-04-13 16:12:26 +08002472 if (r)
2473 goto unlock;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002474 }
Alex Deucherd05da0e2017-06-30 17:08:45 -04002475 amdgpu_atombios_scratch_regs_restore(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002476
2477 /* post card */
Jim Quc836fec2017-02-10 15:59:59 +08002478 if (amdgpu_need_post(adev)) {
jimqu74b0b152016-09-07 17:09:12 +08002479 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2480 if (r)
2481 DRM_ERROR("amdgpu asic init failed\n");
2482 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002483
2484 r = amdgpu_resume(adev);
Rex Zhue6707212017-03-30 13:21:01 +08002485 if (r) {
Flora Cuica198522016-02-04 15:10:08 +08002486 DRM_ERROR("amdgpu_resume failed (%d).\n", r);
Huang Rui03161a62017-04-13 16:12:26 +08002487 goto unlock;
Rex Zhue6707212017-03-30 13:21:01 +08002488 }
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002489 amdgpu_fence_driver_resume(adev);
2490
Flora Cuica198522016-02-04 15:10:08 +08002491 if (resume) {
2492 r = amdgpu_ib_ring_tests(adev);
2493 if (r)
2494 DRM_ERROR("ib ring test failed (%d).\n", r);
2495 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002496
2497 r = amdgpu_late_init(adev);
Huang Rui03161a62017-04-13 16:12:26 +08002498 if (r)
2499 goto unlock;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002500
Alex Deucher756e6882015-10-08 00:03:36 -04002501 /* pin cursors */
2502 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2503 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2504
2505 if (amdgpu_crtc->cursor_bo) {
2506 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
Alex Xie7a6901d2017-04-24 13:52:41 -04002507 r = amdgpu_bo_reserve(aobj, true);
Alex Deucher756e6882015-10-08 00:03:36 -04002508 if (r == 0) {
2509 r = amdgpu_bo_pin(aobj,
2510 AMDGPU_GEM_DOMAIN_VRAM,
2511 &amdgpu_crtc->cursor_addr);
2512 if (r != 0)
2513 DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
2514 amdgpu_bo_unreserve(aobj);
2515 }
2516 }
2517 }
Yong Zhaoba997702015-11-09 17:21:45 -05002518 r = amdgpu_amdkfd_resume(adev);
2519 if (r)
2520 return r;
Alex Deucher756e6882015-10-08 00:03:36 -04002521
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002522 /* blat the mode back in */
2523 if (fbcon) {
2524 drm_helper_resume_force_mode(dev);
2525 /* turn on display hw */
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002526 drm_modeset_lock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002527 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2528 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
2529 }
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002530 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002531 }
2532
2533 drm_kms_helper_poll_enable(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002534
2535 /*
2536 * Most of the connector probing functions try to acquire runtime pm
2537 * refs to ensure that the GPU is powered on when connector polling is
2538 * performed. Since we're calling this from a runtime PM callback,
2539 * trying to acquire rpm refs will cause us to deadlock.
2540 *
2541 * Since we're guaranteed to be holding the rpm lock, it's safe to
2542 * temporarily disable the rpm helpers so this doesn't deadlock us.
2543 */
2544#ifdef CONFIG_PM
2545 dev->dev->power.disable_depth++;
2546#endif
Alex Deucher54fb2a52015-11-24 14:30:56 -05002547 drm_helper_hpd_irq_event(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002548#ifdef CONFIG_PM
2549 dev->dev->power.disable_depth--;
2550#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002551
Huang Rui03161a62017-04-13 16:12:26 +08002552 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002553 amdgpu_fbdev_set_suspend(adev, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002554
Huang Rui03161a62017-04-13 16:12:26 +08002555unlock:
2556 if (fbcon)
2557 console_unlock();
2558
2559 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002560}
2561
Chunming Zhou63fbf422016-07-15 11:19:20 +08002562static bool amdgpu_check_soft_reset(struct amdgpu_device *adev)
2563{
2564 int i;
2565 bool asic_hang = false;
2566
2567 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002568 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou63fbf422016-07-15 11:19:20 +08002569 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002570 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
2571 adev->ip_blocks[i].status.hang =
2572 adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
2573 if (adev->ip_blocks[i].status.hang) {
2574 DRM_INFO("IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
Chunming Zhou63fbf422016-07-15 11:19:20 +08002575 asic_hang = true;
2576 }
2577 }
2578 return asic_hang;
2579}
2580
Baoyou Xie4d446652016-09-18 22:09:35 +08002581static int amdgpu_pre_soft_reset(struct amdgpu_device *adev)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002582{
2583 int i, r = 0;
2584
2585 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002586 if (!adev->ip_blocks[i].status.valid)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002587 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002588 if (adev->ip_blocks[i].status.hang &&
2589 adev->ip_blocks[i].version->funcs->pre_soft_reset) {
2590 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
Chunming Zhoud31a5012016-07-18 10:04:34 +08002591 if (r)
2592 return r;
2593 }
2594 }
2595
2596 return 0;
2597}
2598
Chunming Zhou35d782f2016-07-15 15:57:13 +08002599static bool amdgpu_need_full_reset(struct amdgpu_device *adev)
2600{
Alex Deucherda146d32016-10-13 16:07:03 -04002601 int i;
2602
2603 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002604 if (!adev->ip_blocks[i].status.valid)
Alex Deucherda146d32016-10-13 16:07:03 -04002605 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002606 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
2607 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
2608 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
2609 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE)) {
2610 if (adev->ip_blocks[i].status.hang) {
Alex Deucherda146d32016-10-13 16:07:03 -04002611 DRM_INFO("Some block need full reset!\n");
2612 return true;
2613 }
2614 }
Chunming Zhou35d782f2016-07-15 15:57:13 +08002615 }
2616 return false;
2617}
2618
2619static int amdgpu_soft_reset(struct amdgpu_device *adev)
2620{
2621 int i, r = 0;
2622
2623 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002624 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002625 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002626 if (adev->ip_blocks[i].status.hang &&
2627 adev->ip_blocks[i].version->funcs->soft_reset) {
2628 r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002629 if (r)
2630 return r;
2631 }
2632 }
2633
2634 return 0;
2635}
2636
2637static int amdgpu_post_soft_reset(struct amdgpu_device *adev)
2638{
2639 int i, r = 0;
2640
2641 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002642 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002643 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002644 if (adev->ip_blocks[i].status.hang &&
2645 adev->ip_blocks[i].version->funcs->post_soft_reset)
2646 r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002647 if (r)
2648 return r;
2649 }
2650
2651 return 0;
2652}
2653
Chunming Zhou3ad81f12016-08-05 17:30:17 +08002654bool amdgpu_need_backup(struct amdgpu_device *adev)
2655{
2656 if (adev->flags & AMD_IS_APU)
2657 return false;
2658
2659 return amdgpu_lockup_timeout > 0 ? true : false;
2660}
2661
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002662static int amdgpu_recover_vram_from_shadow(struct amdgpu_device *adev,
2663 struct amdgpu_ring *ring,
2664 struct amdgpu_bo *bo,
Chris Wilsonf54d1862016-10-25 13:00:45 +01002665 struct dma_fence **fence)
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002666{
2667 uint32_t domain;
2668 int r;
2669
Roger.He23d2e502017-04-21 14:24:26 +08002670 if (!bo->shadow)
2671 return 0;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002672
Alex Xie1d284792017-04-24 13:53:04 -04002673 r = amdgpu_bo_reserve(bo, true);
Roger.He23d2e502017-04-21 14:24:26 +08002674 if (r)
2675 return r;
2676 domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
2677 /* if bo has been evicted, then no need to recover */
2678 if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
Roger.He82521312017-04-21 13:08:43 +08002679 r = amdgpu_bo_validate(bo->shadow);
2680 if (r) {
2681 DRM_ERROR("bo validate failed!\n");
2682 goto err;
2683 }
2684
2685 r = amdgpu_ttm_bind(&bo->shadow->tbo, &bo->shadow->tbo.mem);
2686 if (r) {
2687 DRM_ERROR("%p bind failed\n", bo->shadow);
2688 goto err;
2689 }
2690
Roger.He23d2e502017-04-21 14:24:26 +08002691 r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002692 NULL, fence, true);
Roger.He23d2e502017-04-21 14:24:26 +08002693 if (r) {
2694 DRM_ERROR("recover page table failed!\n");
2695 goto err;
2696 }
2697 }
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002698err:
Roger.He23d2e502017-04-21 14:24:26 +08002699 amdgpu_bo_unreserve(bo);
2700 return r;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002701}
2702
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002703/**
Monk Liua90ad3c2017-01-23 14:22:08 +08002704 * amdgpu_sriov_gpu_reset - reset the asic
2705 *
2706 * @adev: amdgpu device pointer
Monk Liu7225f872017-04-26 14:51:54 +08002707 * @job: which job trigger hang
Monk Liua90ad3c2017-01-23 14:22:08 +08002708 *
2709 * Attempt the reset the GPU if it has hung (all asics).
2710 * for SRIOV case.
2711 * Returns 0 for success or an error on failure.
2712 */
Monk Liu7225f872017-04-26 14:51:54 +08002713int amdgpu_sriov_gpu_reset(struct amdgpu_device *adev, struct amdgpu_job *job)
Monk Liua90ad3c2017-01-23 14:22:08 +08002714{
Monk Liu65781c72017-05-11 13:36:44 +08002715 int i, j, r = 0;
Monk Liua90ad3c2017-01-23 14:22:08 +08002716 int resched;
2717 struct amdgpu_bo *bo, *tmp;
2718 struct amdgpu_ring *ring;
2719 struct dma_fence *fence = NULL, *next = NULL;
2720
Monk Liu147b5982017-01-25 15:48:01 +08002721 mutex_lock(&adev->virt.lock_reset);
Monk Liua90ad3c2017-01-23 14:22:08 +08002722 atomic_inc(&adev->gpu_reset_counter);
Monk Liu1fb37a32017-01-26 15:36:37 +08002723 adev->gfx.in_reset = true;
Monk Liua90ad3c2017-01-23 14:22:08 +08002724
2725 /* block TTM */
2726 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
2727
Monk Liu65781c72017-05-11 13:36:44 +08002728 /* we start from the ring trigger GPU hang */
2729 j = job ? job->ring->idx : 0;
Monk Liua90ad3c2017-01-23 14:22:08 +08002730
Monk Liu65781c72017-05-11 13:36:44 +08002731 /* block scheduler */
2732 for (i = j; i < j + AMDGPU_MAX_RINGS; ++i) {
2733 ring = adev->rings[i % AMDGPU_MAX_RINGS];
Monk Liua90ad3c2017-01-23 14:22:08 +08002734 if (!ring || !ring->sched.thread)
2735 continue;
2736
2737 kthread_park(ring->sched.thread);
Monk Liua90ad3c2017-01-23 14:22:08 +08002738
Monk Liu65781c72017-05-11 13:36:44 +08002739 if (job && j != i)
2740 continue;
2741
Monk Liu4f059ec2017-05-11 13:59:15 +08002742 /* here give the last chance to check if job removed from mirror-list
Monk Liu65781c72017-05-11 13:36:44 +08002743 * since we already pay some time on kthread_park */
Monk Liu4f059ec2017-05-11 13:59:15 +08002744 if (job && list_empty(&job->base.node)) {
Monk Liu65781c72017-05-11 13:36:44 +08002745 kthread_unpark(ring->sched.thread);
2746 goto give_up_reset;
2747 }
2748
2749 if (amd_sched_invalidate_job(&job->base, amdgpu_job_hang_limit))
2750 amd_sched_job_kickout(&job->base);
2751
2752 /* only do job_reset on the hang ring if @job not NULL */
2753 amd_sched_hw_job_reset(&ring->sched);
2754
2755 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
2756 amdgpu_fence_driver_force_completion_ring(ring);
2757 }
Monk Liua90ad3c2017-01-23 14:22:08 +08002758
2759 /* request to take full control of GPU before re-initialization */
Monk Liu7225f872017-04-26 14:51:54 +08002760 if (job)
Monk Liua90ad3c2017-01-23 14:22:08 +08002761 amdgpu_virt_reset_gpu(adev);
2762 else
2763 amdgpu_virt_request_full_gpu(adev, true);
2764
2765
2766 /* Resume IP prior to SMC */
Monk Liue4f0fdc2017-02-09 11:55:49 +08002767 amdgpu_sriov_reinit_early(adev);
Monk Liua90ad3c2017-01-23 14:22:08 +08002768
2769 /* we need recover gart prior to run SMC/CP/SDMA resume */
2770 amdgpu_ttm_recover_gart(adev);
2771
2772 /* now we are okay to resume SMC/CP/SDMA */
Monk Liue4f0fdc2017-02-09 11:55:49 +08002773 amdgpu_sriov_reinit_late(adev);
Monk Liua90ad3c2017-01-23 14:22:08 +08002774
2775 amdgpu_irq_gpu_reset_resume_helper(adev);
2776
2777 if (amdgpu_ib_ring_tests(adev))
2778 dev_err(adev->dev, "[GPU_RESET] ib ring test failed (%d).\n", r);
2779
2780 /* release full control of GPU after ib test */
2781 amdgpu_virt_release_full_gpu(adev, true);
2782
2783 DRM_INFO("recover vram bo from shadow\n");
2784
2785 ring = adev->mman.buffer_funcs_ring;
2786 mutex_lock(&adev->shadow_list_lock);
2787 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
Monk Liu236763d2017-05-01 16:15:31 +08002788 next = NULL;
Monk Liua90ad3c2017-01-23 14:22:08 +08002789 amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
2790 if (fence) {
2791 r = dma_fence_wait(fence, false);
2792 if (r) {
2793 WARN(r, "recovery from shadow isn't completed\n");
2794 break;
2795 }
2796 }
2797
2798 dma_fence_put(fence);
2799 fence = next;
2800 }
2801 mutex_unlock(&adev->shadow_list_lock);
2802
2803 if (fence) {
2804 r = dma_fence_wait(fence, false);
2805 if (r)
2806 WARN(r, "recovery from shadow isn't completed\n");
2807 }
2808 dma_fence_put(fence);
2809
Monk Liu65781c72017-05-11 13:36:44 +08002810 for (i = j; i < j + AMDGPU_MAX_RINGS; ++i) {
2811 ring = adev->rings[i % AMDGPU_MAX_RINGS];
Monk Liua90ad3c2017-01-23 14:22:08 +08002812 if (!ring || !ring->sched.thread)
2813 continue;
2814
Monk Liu65781c72017-05-11 13:36:44 +08002815 if (job && j != i) {
2816 kthread_unpark(ring->sched.thread);
2817 continue;
2818 }
2819
Monk Liua90ad3c2017-01-23 14:22:08 +08002820 amd_sched_job_recovery(&ring->sched);
2821 kthread_unpark(ring->sched.thread);
2822 }
2823
2824 drm_helper_resume_force_mode(adev->ddev);
Monk Liu65781c72017-05-11 13:36:44 +08002825give_up_reset:
Monk Liua90ad3c2017-01-23 14:22:08 +08002826 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
2827 if (r) {
2828 /* bad news, how to tell it to userspace ? */
2829 dev_info(adev->dev, "GPU reset failed\n");
Monk Liu65781c72017-05-11 13:36:44 +08002830 } else {
2831 dev_info(adev->dev, "GPU reset successed!\n");
Monk Liua90ad3c2017-01-23 14:22:08 +08002832 }
2833
Monk Liu1fb37a32017-01-26 15:36:37 +08002834 adev->gfx.in_reset = false;
Monk Liu147b5982017-01-25 15:48:01 +08002835 mutex_unlock(&adev->virt.lock_reset);
Monk Liua90ad3c2017-01-23 14:22:08 +08002836 return r;
2837}
2838
2839/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002840 * amdgpu_gpu_reset - reset the asic
2841 *
2842 * @adev: amdgpu device pointer
2843 *
2844 * Attempt the reset the GPU if it has hung (all asics).
2845 * Returns 0 for success or an error on failure.
2846 */
2847int amdgpu_gpu_reset(struct amdgpu_device *adev)
2848{
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002849 int i, r;
2850 int resched;
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002851 bool need_full_reset, vram_lost = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002852
Chunming Zhou63fbf422016-07-15 11:19:20 +08002853 if (!amdgpu_check_soft_reset(adev)) {
2854 DRM_INFO("No hardware hang detected. Did some blocks stall?\n");
2855 return 0;
2856 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002857
Marek Olšákd94aed52015-05-05 21:13:49 +02002858 atomic_inc(&adev->gpu_reset_counter);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002859
Chunming Zhoua3c47d62016-06-30 16:44:41 +08002860 /* block TTM */
2861 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
2862
Chunming Zhou0875dc92016-06-12 15:41:58 +08002863 /* block scheduler */
2864 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2865 struct amdgpu_ring *ring = adev->rings[i];
2866
Chunming Zhou51687752017-04-24 17:09:15 +08002867 if (!ring || !ring->sched.thread)
Chunming Zhou0875dc92016-06-12 15:41:58 +08002868 continue;
2869 kthread_park(ring->sched.thread);
Chunming Zhouaa1c8902016-06-30 13:56:02 +08002870 amd_sched_hw_job_reset(&ring->sched);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002871 }
Chunming Zhou2200eda2016-06-30 16:53:02 +08002872 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
2873 amdgpu_fence_driver_force_completion(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002874
Chunming Zhou35d782f2016-07-15 15:57:13 +08002875 need_full_reset = amdgpu_need_full_reset(adev);
2876
2877 if (!need_full_reset) {
2878 amdgpu_pre_soft_reset(adev);
2879 r = amdgpu_soft_reset(adev);
2880 amdgpu_post_soft_reset(adev);
2881 if (r || amdgpu_check_soft_reset(adev)) {
2882 DRM_INFO("soft reset failed, will fallback to full reset!\n");
2883 need_full_reset = true;
2884 }
2885 }
2886
2887 if (need_full_reset) {
Chunming Zhou35d782f2016-07-15 15:57:13 +08002888 r = amdgpu_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002889
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002890retry:
Alex Deucherd05da0e2017-06-30 17:08:45 -04002891 amdgpu_atombios_scratch_regs_save(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002892 r = amdgpu_asic_reset(adev);
Alex Deucherd05da0e2017-06-30 17:08:45 -04002893 amdgpu_atombios_scratch_regs_restore(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002894 /* post card */
2895 amdgpu_atom_asic_init(adev->mode_info.atom_context);
Alex Deucherbfa99262016-01-15 11:59:48 -05002896
Chunming Zhou35d782f2016-07-15 15:57:13 +08002897 if (!r) {
2898 dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
Chunming Zhoufcf06492017-05-05 10:33:33 +08002899 r = amdgpu_resume_phase1(adev);
2900 if (r)
2901 goto out;
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002902 vram_lost = amdgpu_check_vram_lost(adev);
Chunming Zhouf1892132017-05-15 16:48:27 +08002903 if (vram_lost) {
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002904 DRM_ERROR("VRAM is lost!\n");
Chunming Zhouf1892132017-05-15 16:48:27 +08002905 atomic_inc(&adev->vram_lost_counter);
2906 }
Chunming Zhou2c0d7312016-08-30 16:36:25 +08002907 r = amdgpu_ttm_recover_gart(adev);
2908 if (r)
Chunming Zhoufcf06492017-05-05 10:33:33 +08002909 goto out;
2910 r = amdgpu_resume_phase2(adev);
2911 if (r)
2912 goto out;
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002913 if (vram_lost)
2914 amdgpu_fill_reset_magic(adev);
Chunming Zhou2c0d7312016-08-30 16:36:25 +08002915 }
Chunming Zhoufcf06492017-05-05 10:33:33 +08002916 }
2917out:
2918 if (!r) {
2919 amdgpu_irq_gpu_reset_resume_helper(adev);
Chunming Zhou1f465082016-06-30 15:02:26 +08002920 r = amdgpu_ib_ring_tests(adev);
2921 if (r) {
2922 dev_err(adev->dev, "ib ring test failed (%d).\n", r);
Chunming Zhou40019dc2016-06-29 16:01:49 +08002923 r = amdgpu_suspend(adev);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002924 need_full_reset = true;
Chunming Zhou40019dc2016-06-29 16:01:49 +08002925 goto retry;
Chunming Zhou1f465082016-06-30 15:02:26 +08002926 }
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002927 /**
2928 * recovery vm page tables, since we cannot depend on VRAM is
2929 * consistent after gpu full reset.
2930 */
2931 if (need_full_reset && amdgpu_need_backup(adev)) {
2932 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
2933 struct amdgpu_bo *bo, *tmp;
Chris Wilsonf54d1862016-10-25 13:00:45 +01002934 struct dma_fence *fence = NULL, *next = NULL;
Chunming Zhou1f465082016-06-30 15:02:26 +08002935
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002936 DRM_INFO("recover vram bo from shadow\n");
2937 mutex_lock(&adev->shadow_list_lock);
2938 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
Monk Liu236763d2017-05-01 16:15:31 +08002939 next = NULL;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002940 amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
2941 if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01002942 r = dma_fence_wait(fence, false);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002943 if (r) {
Monk Liu1d7b17b2017-01-22 18:52:56 +08002944 WARN(r, "recovery from shadow isn't completed\n");
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002945 break;
2946 }
2947 }
2948
Chris Wilsonf54d1862016-10-25 13:00:45 +01002949 dma_fence_put(fence);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002950 fence = next;
2951 }
2952 mutex_unlock(&adev->shadow_list_lock);
2953 if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01002954 r = dma_fence_wait(fence, false);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002955 if (r)
Monk Liu1d7b17b2017-01-22 18:52:56 +08002956 WARN(r, "recovery from shadow isn't completed\n");
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002957 }
Chris Wilsonf54d1862016-10-25 13:00:45 +01002958 dma_fence_put(fence);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002959 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002960 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2961 struct amdgpu_ring *ring = adev->rings[i];
Chunming Zhou51687752017-04-24 17:09:15 +08002962
2963 if (!ring || !ring->sched.thread)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002964 continue;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002965
Chunming Zhouaa1c8902016-06-30 13:56:02 +08002966 amd_sched_job_recovery(&ring->sched);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002967 kthread_unpark(ring->sched.thread);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002968 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002969 } else {
Chunming Zhou2200eda2016-06-30 16:53:02 +08002970 dev_err(adev->dev, "asic resume failed (%d).\n", r);
Gavin Wan89041942017-06-23 13:55:15 -04002971 amdgpu_vf_error_put(AMDGIM_ERROR_VF_ASIC_RESUME_FAIL, 0, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002972 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
Chunming Zhou51687752017-04-24 17:09:15 +08002973 if (adev->rings[i] && adev->rings[i]->sched.thread) {
Chunming Zhou0875dc92016-06-12 15:41:58 +08002974 kthread_unpark(adev->rings[i]->sched.thread);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002975 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002976 }
2977 }
2978
2979 drm_helper_resume_force_mode(adev->ddev);
2980
2981 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
Gavin Wan89041942017-06-23 13:55:15 -04002982 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002983 /* bad news, how to tell it to userspace ? */
2984 dev_info(adev->dev, "GPU reset failed\n");
Gavin Wan89041942017-06-23 13:55:15 -04002985 amdgpu_vf_error_put(AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, r);
2986 }
2987 else {
Chunming Zhou6643be62017-05-05 10:50:09 +08002988 dev_info(adev->dev, "GPU reset successed!\n");
Gavin Wan89041942017-06-23 13:55:15 -04002989 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002990
Gavin Wan89041942017-06-23 13:55:15 -04002991 amdgpu_vf_error_trans_all(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002992 return r;
2993}
2994
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002995void amdgpu_get_pcie_info(struct amdgpu_device *adev)
2996{
2997 u32 mask;
2998 int ret;
2999
Alex Deuchercd474ba2016-02-04 10:21:23 -05003000 if (amdgpu_pcie_gen_cap)
3001 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
3002
3003 if (amdgpu_pcie_lane_cap)
3004 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
3005
3006 /* covers APUs as well */
3007 if (pci_is_root_bus(adev->pdev->bus)) {
3008 if (adev->pm.pcie_gen_mask == 0)
3009 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
3010 if (adev->pm.pcie_mlw_mask == 0)
3011 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003012 return;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003013 }
Alex Deuchercd474ba2016-02-04 10:21:23 -05003014
3015 if (adev->pm.pcie_gen_mask == 0) {
3016 ret = drm_pcie_get_speed_cap_mask(adev->ddev, &mask);
3017 if (!ret) {
3018 adev->pm.pcie_gen_mask = (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
3019 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
3020 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
3021
3022 if (mask & DRM_PCIE_SPEED_25)
3023 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
3024 if (mask & DRM_PCIE_SPEED_50)
3025 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2;
3026 if (mask & DRM_PCIE_SPEED_80)
3027 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3;
3028 } else {
3029 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
3030 }
3031 }
3032 if (adev->pm.pcie_mlw_mask == 0) {
3033 ret = drm_pcie_get_max_link_width(adev->ddev, &mask);
3034 if (!ret) {
3035 switch (mask) {
3036 case 32:
3037 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
3038 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
3039 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3040 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3041 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3042 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3043 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3044 break;
3045 case 16:
3046 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
3047 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3048 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3049 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3050 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3051 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3052 break;
3053 case 12:
3054 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3055 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3056 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3057 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3058 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3059 break;
3060 case 8:
3061 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3062 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3063 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3064 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3065 break;
3066 case 4:
3067 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3068 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3069 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3070 break;
3071 case 2:
3072 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3073 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3074 break;
3075 case 1:
3076 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
3077 break;
3078 default:
3079 break;
3080 }
3081 } else {
3082 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003083 }
3084 }
3085}
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003086
3087/*
3088 * Debugfs
3089 */
3090int amdgpu_debugfs_add_files(struct amdgpu_device *adev,
Nils Wallménius06ab6832016-05-02 12:46:15 -04003091 const struct drm_info_list *files,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003092 unsigned nfiles)
3093{
3094 unsigned i;
3095
3096 for (i = 0; i < adev->debugfs_count; i++) {
3097 if (adev->debugfs[i].files == files) {
3098 /* Already registered */
3099 return 0;
3100 }
3101 }
3102
3103 i = adev->debugfs_count + 1;
3104 if (i > AMDGPU_DEBUGFS_MAX_COMPONENTS) {
3105 DRM_ERROR("Reached maximum number of debugfs components.\n");
3106 DRM_ERROR("Report so we increase "
3107 "AMDGPU_DEBUGFS_MAX_COMPONENTS.\n");
3108 return -EINVAL;
3109 }
3110 adev->debugfs[adev->debugfs_count].files = files;
3111 adev->debugfs[adev->debugfs_count].num_files = nfiles;
3112 adev->debugfs_count = i;
3113#if defined(CONFIG_DEBUG_FS)
3114 drm_debugfs_create_files(files, nfiles,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003115 adev->ddev->primary->debugfs_root,
3116 adev->ddev->primary);
3117#endif
3118 return 0;
3119}
3120
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003121#if defined(CONFIG_DEBUG_FS)
3122
3123static ssize_t amdgpu_debugfs_regs_read(struct file *f, char __user *buf,
3124 size_t size, loff_t *pos)
3125{
Al Viro45063092016-12-04 18:24:56 -05003126 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003127 ssize_t result = 0;
3128 int r;
Tom St Denisbd122672016-07-28 09:39:22 -04003129 bool pm_pg_lock, use_bank;
Tom St Denis566281592016-06-27 11:55:07 -04003130 unsigned instance_bank, sh_bank, se_bank;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003131
3132 if (size & 0x3 || *pos & 0x3)
3133 return -EINVAL;
3134
Tom St Denisbd122672016-07-28 09:39:22 -04003135 /* are we reading registers for which a PG lock is necessary? */
3136 pm_pg_lock = (*pos >> 23) & 1;
3137
Tom St Denis566281592016-06-27 11:55:07 -04003138 if (*pos & (1ULL << 62)) {
3139 se_bank = (*pos >> 24) & 0x3FF;
3140 sh_bank = (*pos >> 34) & 0x3FF;
3141 instance_bank = (*pos >> 44) & 0x3FF;
Tom St Denis32977f92016-10-09 07:41:26 -04003142
3143 if (se_bank == 0x3FF)
3144 se_bank = 0xFFFFFFFF;
3145 if (sh_bank == 0x3FF)
3146 sh_bank = 0xFFFFFFFF;
3147 if (instance_bank == 0x3FF)
3148 instance_bank = 0xFFFFFFFF;
Tom St Denis566281592016-06-27 11:55:07 -04003149 use_bank = 1;
Tom St Denis566281592016-06-27 11:55:07 -04003150 } else {
3151 use_bank = 0;
3152 }
3153
Tom St Denis801a6aa9a62017-03-15 05:34:25 -04003154 *pos &= (1UL << 22) - 1;
Tom St Denisbd122672016-07-28 09:39:22 -04003155
Tom St Denis566281592016-06-27 11:55:07 -04003156 if (use_bank) {
Tom St Denis32977f92016-10-09 07:41:26 -04003157 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
3158 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
Tom St Denis566281592016-06-27 11:55:07 -04003159 return -EINVAL;
3160 mutex_lock(&adev->grbm_idx_mutex);
3161 amdgpu_gfx_select_se_sh(adev, se_bank,
3162 sh_bank, instance_bank);
3163 }
3164
Tom St Denisbd122672016-07-28 09:39:22 -04003165 if (pm_pg_lock)
3166 mutex_lock(&adev->pm.mutex);
3167
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003168 while (size) {
3169 uint32_t value;
3170
3171 if (*pos > adev->rmmio_size)
Tom St Denis566281592016-06-27 11:55:07 -04003172 goto end;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003173
3174 value = RREG32(*pos >> 2);
3175 r = put_user(value, (uint32_t *)buf);
Tom St Denis566281592016-06-27 11:55:07 -04003176 if (r) {
3177 result = r;
3178 goto end;
3179 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003180
3181 result += 4;
3182 buf += 4;
3183 *pos += 4;
3184 size -= 4;
3185 }
3186
Tom St Denis566281592016-06-27 11:55:07 -04003187end:
3188 if (use_bank) {
3189 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
3190 mutex_unlock(&adev->grbm_idx_mutex);
3191 }
3192
Tom St Denisbd122672016-07-28 09:39:22 -04003193 if (pm_pg_lock)
3194 mutex_unlock(&adev->pm.mutex);
3195
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003196 return result;
3197}
3198
3199static ssize_t amdgpu_debugfs_regs_write(struct file *f, const char __user *buf,
3200 size_t size, loff_t *pos)
3201{
Al Viro45063092016-12-04 18:24:56 -05003202 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003203 ssize_t result = 0;
3204 int r;
Tom St Denis394fdde2016-10-10 07:31:23 -04003205 bool pm_pg_lock, use_bank;
3206 unsigned instance_bank, sh_bank, se_bank;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003207
3208 if (size & 0x3 || *pos & 0x3)
3209 return -EINVAL;
3210
Tom St Denis394fdde2016-10-10 07:31:23 -04003211 /* are we reading registers for which a PG lock is necessary? */
3212 pm_pg_lock = (*pos >> 23) & 1;
3213
3214 if (*pos & (1ULL << 62)) {
3215 se_bank = (*pos >> 24) & 0x3FF;
3216 sh_bank = (*pos >> 34) & 0x3FF;
3217 instance_bank = (*pos >> 44) & 0x3FF;
3218
3219 if (se_bank == 0x3FF)
3220 se_bank = 0xFFFFFFFF;
3221 if (sh_bank == 0x3FF)
3222 sh_bank = 0xFFFFFFFF;
3223 if (instance_bank == 0x3FF)
3224 instance_bank = 0xFFFFFFFF;
3225 use_bank = 1;
3226 } else {
3227 use_bank = 0;
3228 }
3229
Tom St Denis801a6aa9a62017-03-15 05:34:25 -04003230 *pos &= (1UL << 22) - 1;
Tom St Denis394fdde2016-10-10 07:31:23 -04003231
3232 if (use_bank) {
3233 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
3234 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
3235 return -EINVAL;
3236 mutex_lock(&adev->grbm_idx_mutex);
3237 amdgpu_gfx_select_se_sh(adev, se_bank,
3238 sh_bank, instance_bank);
3239 }
3240
3241 if (pm_pg_lock)
3242 mutex_lock(&adev->pm.mutex);
3243
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003244 while (size) {
3245 uint32_t value;
3246
3247 if (*pos > adev->rmmio_size)
3248 return result;
3249
3250 r = get_user(value, (uint32_t *)buf);
3251 if (r)
3252 return r;
3253
3254 WREG32(*pos >> 2, value);
3255
3256 result += 4;
3257 buf += 4;
3258 *pos += 4;
3259 size -= 4;
3260 }
3261
Tom St Denis394fdde2016-10-10 07:31:23 -04003262 if (use_bank) {
3263 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
3264 mutex_unlock(&adev->grbm_idx_mutex);
3265 }
3266
3267 if (pm_pg_lock)
3268 mutex_unlock(&adev->pm.mutex);
3269
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003270 return result;
3271}
3272
Tom St Denisadcec282016-04-15 13:08:44 -04003273static ssize_t amdgpu_debugfs_regs_pcie_read(struct file *f, char __user *buf,
3274 size_t size, loff_t *pos)
3275{
Al Viro45063092016-12-04 18:24:56 -05003276 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003277 ssize_t result = 0;
3278 int r;
3279
3280 if (size & 0x3 || *pos & 0x3)
3281 return -EINVAL;
3282
3283 while (size) {
3284 uint32_t value;
3285
3286 value = RREG32_PCIE(*pos >> 2);
3287 r = put_user(value, (uint32_t *)buf);
3288 if (r)
3289 return r;
3290
3291 result += 4;
3292 buf += 4;
3293 *pos += 4;
3294 size -= 4;
3295 }
3296
3297 return result;
3298}
3299
3300static ssize_t amdgpu_debugfs_regs_pcie_write(struct file *f, const char __user *buf,
3301 size_t size, loff_t *pos)
3302{
Al Viro45063092016-12-04 18:24:56 -05003303 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003304 ssize_t result = 0;
3305 int r;
3306
3307 if (size & 0x3 || *pos & 0x3)
3308 return -EINVAL;
3309
3310 while (size) {
3311 uint32_t value;
3312
3313 r = get_user(value, (uint32_t *)buf);
3314 if (r)
3315 return r;
3316
3317 WREG32_PCIE(*pos >> 2, value);
3318
3319 result += 4;
3320 buf += 4;
3321 *pos += 4;
3322 size -= 4;
3323 }
3324
3325 return result;
3326}
3327
3328static ssize_t amdgpu_debugfs_regs_didt_read(struct file *f, char __user *buf,
3329 size_t size, loff_t *pos)
3330{
Al Viro45063092016-12-04 18:24:56 -05003331 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003332 ssize_t result = 0;
3333 int r;
3334
3335 if (size & 0x3 || *pos & 0x3)
3336 return -EINVAL;
3337
3338 while (size) {
3339 uint32_t value;
3340
3341 value = RREG32_DIDT(*pos >> 2);
3342 r = put_user(value, (uint32_t *)buf);
3343 if (r)
3344 return r;
3345
3346 result += 4;
3347 buf += 4;
3348 *pos += 4;
3349 size -= 4;
3350 }
3351
3352 return result;
3353}
3354
3355static ssize_t amdgpu_debugfs_regs_didt_write(struct file *f, const char __user *buf,
3356 size_t size, loff_t *pos)
3357{
Al Viro45063092016-12-04 18:24:56 -05003358 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003359 ssize_t result = 0;
3360 int r;
3361
3362 if (size & 0x3 || *pos & 0x3)
3363 return -EINVAL;
3364
3365 while (size) {
3366 uint32_t value;
3367
3368 r = get_user(value, (uint32_t *)buf);
3369 if (r)
3370 return r;
3371
3372 WREG32_DIDT(*pos >> 2, value);
3373
3374 result += 4;
3375 buf += 4;
3376 *pos += 4;
3377 size -= 4;
3378 }
3379
3380 return result;
3381}
3382
3383static ssize_t amdgpu_debugfs_regs_smc_read(struct file *f, char __user *buf,
3384 size_t size, loff_t *pos)
3385{
Al Viro45063092016-12-04 18:24:56 -05003386 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003387 ssize_t result = 0;
3388 int r;
3389
3390 if (size & 0x3 || *pos & 0x3)
3391 return -EINVAL;
3392
3393 while (size) {
3394 uint32_t value;
3395
Tom St Denis6fc0dea2016-08-29 08:39:29 -04003396 value = RREG32_SMC(*pos);
Tom St Denisadcec282016-04-15 13:08:44 -04003397 r = put_user(value, (uint32_t *)buf);
3398 if (r)
3399 return r;
3400
3401 result += 4;
3402 buf += 4;
3403 *pos += 4;
3404 size -= 4;
3405 }
3406
3407 return result;
3408}
3409
3410static ssize_t amdgpu_debugfs_regs_smc_write(struct file *f, const char __user *buf,
3411 size_t size, loff_t *pos)
3412{
Al Viro45063092016-12-04 18:24:56 -05003413 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003414 ssize_t result = 0;
3415 int r;
3416
3417 if (size & 0x3 || *pos & 0x3)
3418 return -EINVAL;
3419
3420 while (size) {
3421 uint32_t value;
3422
3423 r = get_user(value, (uint32_t *)buf);
3424 if (r)
3425 return r;
3426
Tom St Denis6fc0dea2016-08-29 08:39:29 -04003427 WREG32_SMC(*pos, value);
Tom St Denisadcec282016-04-15 13:08:44 -04003428
3429 result += 4;
3430 buf += 4;
3431 *pos += 4;
3432 size -= 4;
3433 }
3434
3435 return result;
3436}
3437
Tom St Denis1e051412016-06-27 09:57:18 -04003438static ssize_t amdgpu_debugfs_gca_config_read(struct file *f, char __user *buf,
3439 size_t size, loff_t *pos)
3440{
Al Viro45063092016-12-04 18:24:56 -05003441 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denis1e051412016-06-27 09:57:18 -04003442 ssize_t result = 0;
3443 int r;
3444 uint32_t *config, no_regs = 0;
3445
3446 if (size & 0x3 || *pos & 0x3)
3447 return -EINVAL;
3448
Markus Elfringecab7662016-09-18 17:00:52 +02003449 config = kmalloc_array(256, sizeof(*config), GFP_KERNEL);
Tom St Denis1e051412016-06-27 09:57:18 -04003450 if (!config)
3451 return -ENOMEM;
3452
3453 /* version, increment each time something is added */
Tom St Denis9a999352017-01-18 13:01:25 -05003454 config[no_regs++] = 3;
Tom St Denis1e051412016-06-27 09:57:18 -04003455 config[no_regs++] = adev->gfx.config.max_shader_engines;
3456 config[no_regs++] = adev->gfx.config.max_tile_pipes;
3457 config[no_regs++] = adev->gfx.config.max_cu_per_sh;
3458 config[no_regs++] = adev->gfx.config.max_sh_per_se;
3459 config[no_regs++] = adev->gfx.config.max_backends_per_se;
3460 config[no_regs++] = adev->gfx.config.max_texture_channel_caches;
3461 config[no_regs++] = adev->gfx.config.max_gprs;
3462 config[no_regs++] = adev->gfx.config.max_gs_threads;
3463 config[no_regs++] = adev->gfx.config.max_hw_contexts;
3464 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_frontend;
3465 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_backend;
3466 config[no_regs++] = adev->gfx.config.sc_hiz_tile_fifo_size;
3467 config[no_regs++] = adev->gfx.config.sc_earlyz_tile_fifo_size;
3468 config[no_regs++] = adev->gfx.config.num_tile_pipes;
3469 config[no_regs++] = adev->gfx.config.backend_enable_mask;
3470 config[no_regs++] = adev->gfx.config.mem_max_burst_length_bytes;
3471 config[no_regs++] = adev->gfx.config.mem_row_size_in_kb;
3472 config[no_regs++] = adev->gfx.config.shader_engine_tile_size;
3473 config[no_regs++] = adev->gfx.config.num_gpus;
3474 config[no_regs++] = adev->gfx.config.multi_gpu_tile_size;
3475 config[no_regs++] = adev->gfx.config.mc_arb_ramcfg;
3476 config[no_regs++] = adev->gfx.config.gb_addr_config;
3477 config[no_regs++] = adev->gfx.config.num_rbs;
3478
Tom St Denis89a8f302016-08-12 15:14:31 -04003479 /* rev==1 */
3480 config[no_regs++] = adev->rev_id;
3481 config[no_regs++] = adev->pg_flags;
3482 config[no_regs++] = adev->cg_flags;
3483
Tom St Denise9f11dc2016-08-17 12:00:51 -04003484 /* rev==2 */
3485 config[no_regs++] = adev->family;
3486 config[no_regs++] = adev->external_rev_id;
3487
Tom St Denis9a999352017-01-18 13:01:25 -05003488 /* rev==3 */
3489 config[no_regs++] = adev->pdev->device;
3490 config[no_regs++] = adev->pdev->revision;
3491 config[no_regs++] = adev->pdev->subsystem_device;
3492 config[no_regs++] = adev->pdev->subsystem_vendor;
3493
Tom St Denis1e051412016-06-27 09:57:18 -04003494 while (size && (*pos < no_regs * 4)) {
3495 uint32_t value;
3496
3497 value = config[*pos >> 2];
3498 r = put_user(value, (uint32_t *)buf);
3499 if (r) {
3500 kfree(config);
3501 return r;
3502 }
3503
3504 result += 4;
3505 buf += 4;
3506 *pos += 4;
3507 size -= 4;
3508 }
3509
3510 kfree(config);
3511 return result;
3512}
3513
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003514static ssize_t amdgpu_debugfs_sensor_read(struct file *f, char __user *buf,
3515 size_t size, loff_t *pos)
3516{
Al Viro45063092016-12-04 18:24:56 -05003517 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003518 int idx, x, outsize, r, valuesize;
3519 uint32_t values[16];
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003520
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003521 if (size & 3 || *pos & 0x3)
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003522 return -EINVAL;
3523
Samuel Pitoiset3cbc6142017-02-15 19:32:29 +01003524 if (amdgpu_dpm == 0)
3525 return -EINVAL;
3526
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003527 /* convert offset to sensor number */
3528 idx = *pos >> 2;
3529
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003530 valuesize = sizeof(values);
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003531 if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->read_sensor)
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003532 r = adev->powerplay.pp_funcs->read_sensor(adev->powerplay.pp_handle, idx, &values[0], &valuesize);
Samuel Pitoiset3cbc6142017-02-15 19:32:29 +01003533 else if (adev->pm.funcs && adev->pm.funcs->read_sensor)
3534 r = adev->pm.funcs->read_sensor(adev, idx, &values[0],
3535 &valuesize);
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003536 else
3537 return -EINVAL;
3538
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003539 if (size > valuesize)
3540 return -EINVAL;
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003541
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003542 outsize = 0;
3543 x = 0;
3544 if (!r) {
3545 while (size) {
3546 r = put_user(values[x++], (int32_t *)buf);
3547 buf += 4;
3548 size -= 4;
3549 outsize += 4;
3550 }
3551 }
3552
3553 return !r ? outsize : r;
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003554}
Tom St Denis1e051412016-06-27 09:57:18 -04003555
Tom St Denis273d7aa2016-10-11 14:48:55 -04003556static ssize_t amdgpu_debugfs_wave_read(struct file *f, char __user *buf,
3557 size_t size, loff_t *pos)
3558{
3559 struct amdgpu_device *adev = f->f_inode->i_private;
3560 int r, x;
3561 ssize_t result=0;
Tom St Denis472259f2016-10-14 09:49:09 -04003562 uint32_t offset, se, sh, cu, wave, simd, data[32];
Tom St Denis273d7aa2016-10-11 14:48:55 -04003563
3564 if (size & 3 || *pos & 3)
3565 return -EINVAL;
3566
3567 /* decode offset */
3568 offset = (*pos & 0x7F);
3569 se = ((*pos >> 7) & 0xFF);
3570 sh = ((*pos >> 15) & 0xFF);
3571 cu = ((*pos >> 23) & 0xFF);
3572 wave = ((*pos >> 31) & 0xFF);
3573 simd = ((*pos >> 37) & 0xFF);
Tom St Denis273d7aa2016-10-11 14:48:55 -04003574
3575 /* switch to the specific se/sh/cu */
3576 mutex_lock(&adev->grbm_idx_mutex);
3577 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
3578
3579 x = 0;
Tom St Denis472259f2016-10-14 09:49:09 -04003580 if (adev->gfx.funcs->read_wave_data)
3581 adev->gfx.funcs->read_wave_data(adev, simd, wave, data, &x);
Tom St Denis273d7aa2016-10-11 14:48:55 -04003582
3583 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
3584 mutex_unlock(&adev->grbm_idx_mutex);
3585
Tom St Denis5ecfb3b2016-10-13 12:15:03 -04003586 if (!x)
3587 return -EINVAL;
3588
Tom St Denis472259f2016-10-14 09:49:09 -04003589 while (size && (offset < x * 4)) {
Tom St Denis273d7aa2016-10-11 14:48:55 -04003590 uint32_t value;
3591
Tom St Denis472259f2016-10-14 09:49:09 -04003592 value = data[offset >> 2];
Tom St Denis273d7aa2016-10-11 14:48:55 -04003593 r = put_user(value, (uint32_t *)buf);
3594 if (r)
3595 return r;
3596
3597 result += 4;
3598 buf += 4;
Tom St Denis472259f2016-10-14 09:49:09 -04003599 offset += 4;
Tom St Denis273d7aa2016-10-11 14:48:55 -04003600 size -= 4;
3601 }
3602
3603 return result;
3604}
3605
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003606static ssize_t amdgpu_debugfs_gpr_read(struct file *f, char __user *buf,
3607 size_t size, loff_t *pos)
3608{
3609 struct amdgpu_device *adev = f->f_inode->i_private;
3610 int r;
3611 ssize_t result = 0;
3612 uint32_t offset, se, sh, cu, wave, simd, thread, bank, *data;
3613
3614 if (size & 3 || *pos & 3)
3615 return -EINVAL;
3616
3617 /* decode offset */
3618 offset = (*pos & 0xFFF); /* in dwords */
3619 se = ((*pos >> 12) & 0xFF);
3620 sh = ((*pos >> 20) & 0xFF);
3621 cu = ((*pos >> 28) & 0xFF);
3622 wave = ((*pos >> 36) & 0xFF);
3623 simd = ((*pos >> 44) & 0xFF);
3624 thread = ((*pos >> 52) & 0xFF);
3625 bank = ((*pos >> 60) & 1);
3626
3627 data = kmalloc_array(1024, sizeof(*data), GFP_KERNEL);
3628 if (!data)
3629 return -ENOMEM;
3630
3631 /* switch to the specific se/sh/cu */
3632 mutex_lock(&adev->grbm_idx_mutex);
3633 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
3634
3635 if (bank == 0) {
3636 if (adev->gfx.funcs->read_wave_vgprs)
3637 adev->gfx.funcs->read_wave_vgprs(adev, simd, wave, thread, offset, size>>2, data);
3638 } else {
3639 if (adev->gfx.funcs->read_wave_sgprs)
3640 adev->gfx.funcs->read_wave_sgprs(adev, simd, wave, offset, size>>2, data);
3641 }
3642
3643 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
3644 mutex_unlock(&adev->grbm_idx_mutex);
3645
3646 while (size) {
3647 uint32_t value;
3648
3649 value = data[offset++];
3650 r = put_user(value, (uint32_t *)buf);
3651 if (r) {
3652 result = r;
3653 goto err;
3654 }
3655
3656 result += 4;
3657 buf += 4;
3658 size -= 4;
3659 }
3660
3661err:
3662 kfree(data);
3663 return result;
3664}
3665
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003666static const struct file_operations amdgpu_debugfs_regs_fops = {
3667 .owner = THIS_MODULE,
3668 .read = amdgpu_debugfs_regs_read,
3669 .write = amdgpu_debugfs_regs_write,
3670 .llseek = default_llseek
3671};
Tom St Denisadcec282016-04-15 13:08:44 -04003672static const struct file_operations amdgpu_debugfs_regs_didt_fops = {
3673 .owner = THIS_MODULE,
3674 .read = amdgpu_debugfs_regs_didt_read,
3675 .write = amdgpu_debugfs_regs_didt_write,
3676 .llseek = default_llseek
3677};
3678static const struct file_operations amdgpu_debugfs_regs_pcie_fops = {
3679 .owner = THIS_MODULE,
3680 .read = amdgpu_debugfs_regs_pcie_read,
3681 .write = amdgpu_debugfs_regs_pcie_write,
3682 .llseek = default_llseek
3683};
3684static const struct file_operations amdgpu_debugfs_regs_smc_fops = {
3685 .owner = THIS_MODULE,
3686 .read = amdgpu_debugfs_regs_smc_read,
3687 .write = amdgpu_debugfs_regs_smc_write,
3688 .llseek = default_llseek
3689};
3690
Tom St Denis1e051412016-06-27 09:57:18 -04003691static const struct file_operations amdgpu_debugfs_gca_config_fops = {
3692 .owner = THIS_MODULE,
3693 .read = amdgpu_debugfs_gca_config_read,
3694 .llseek = default_llseek
3695};
3696
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003697static const struct file_operations amdgpu_debugfs_sensors_fops = {
3698 .owner = THIS_MODULE,
3699 .read = amdgpu_debugfs_sensor_read,
3700 .llseek = default_llseek
3701};
3702
Tom St Denis273d7aa2016-10-11 14:48:55 -04003703static const struct file_operations amdgpu_debugfs_wave_fops = {
3704 .owner = THIS_MODULE,
3705 .read = amdgpu_debugfs_wave_read,
3706 .llseek = default_llseek
3707};
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003708static const struct file_operations amdgpu_debugfs_gpr_fops = {
3709 .owner = THIS_MODULE,
3710 .read = amdgpu_debugfs_gpr_read,
3711 .llseek = default_llseek
3712};
Tom St Denis273d7aa2016-10-11 14:48:55 -04003713
Tom St Denisadcec282016-04-15 13:08:44 -04003714static const struct file_operations *debugfs_regs[] = {
3715 &amdgpu_debugfs_regs_fops,
3716 &amdgpu_debugfs_regs_didt_fops,
3717 &amdgpu_debugfs_regs_pcie_fops,
3718 &amdgpu_debugfs_regs_smc_fops,
Tom St Denis1e051412016-06-27 09:57:18 -04003719 &amdgpu_debugfs_gca_config_fops,
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003720 &amdgpu_debugfs_sensors_fops,
Tom St Denis273d7aa2016-10-11 14:48:55 -04003721 &amdgpu_debugfs_wave_fops,
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003722 &amdgpu_debugfs_gpr_fops,
Tom St Denisadcec282016-04-15 13:08:44 -04003723};
3724
3725static const char *debugfs_regs_names[] = {
3726 "amdgpu_regs",
3727 "amdgpu_regs_didt",
3728 "amdgpu_regs_pcie",
3729 "amdgpu_regs_smc",
Tom St Denis1e051412016-06-27 09:57:18 -04003730 "amdgpu_gca_config",
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003731 "amdgpu_sensors",
Tom St Denis273d7aa2016-10-11 14:48:55 -04003732 "amdgpu_wave",
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003733 "amdgpu_gpr",
Tom St Denisadcec282016-04-15 13:08:44 -04003734};
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003735
3736static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
3737{
3738 struct drm_minor *minor = adev->ddev->primary;
3739 struct dentry *ent, *root = minor->debugfs_root;
Tom St Denisadcec282016-04-15 13:08:44 -04003740 unsigned i, j;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003741
Tom St Denisadcec282016-04-15 13:08:44 -04003742 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3743 ent = debugfs_create_file(debugfs_regs_names[i],
3744 S_IFREG | S_IRUGO, root,
3745 adev, debugfs_regs[i]);
3746 if (IS_ERR(ent)) {
3747 for (j = 0; j < i; j++) {
3748 debugfs_remove(adev->debugfs_regs[i]);
3749 adev->debugfs_regs[i] = NULL;
3750 }
3751 return PTR_ERR(ent);
3752 }
3753
3754 if (!i)
3755 i_size_write(ent->d_inode, adev->rmmio_size);
3756 adev->debugfs_regs[i] = ent;
3757 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003758
3759 return 0;
3760}
3761
3762static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev)
3763{
Tom St Denisadcec282016-04-15 13:08:44 -04003764 unsigned i;
3765
3766 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3767 if (adev->debugfs_regs[i]) {
3768 debugfs_remove(adev->debugfs_regs[i]);
3769 adev->debugfs_regs[i] = NULL;
3770 }
3771 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003772}
3773
Huang Rui4f0955f2017-05-10 23:04:06 +08003774static int amdgpu_debugfs_test_ib(struct seq_file *m, void *data)
3775{
3776 struct drm_info_node *node = (struct drm_info_node *) m->private;
3777 struct drm_device *dev = node->minor->dev;
3778 struct amdgpu_device *adev = dev->dev_private;
3779 int r = 0, i;
3780
3781 /* hold on the scheduler */
3782 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
3783 struct amdgpu_ring *ring = adev->rings[i];
3784
3785 if (!ring || !ring->sched.thread)
3786 continue;
3787 kthread_park(ring->sched.thread);
3788 }
3789
3790 seq_printf(m, "run ib test:\n");
3791 r = amdgpu_ib_ring_tests(adev);
3792 if (r)
3793 seq_printf(m, "ib ring tests failed (%d).\n", r);
3794 else
3795 seq_printf(m, "ib ring tests passed.\n");
3796
3797 /* go on the scheduler */
3798 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
3799 struct amdgpu_ring *ring = adev->rings[i];
3800
3801 if (!ring || !ring->sched.thread)
3802 continue;
3803 kthread_unpark(ring->sched.thread);
3804 }
3805
3806 return 0;
3807}
3808
3809static const struct drm_info_list amdgpu_debugfs_test_ib_ring_list[] = {
3810 {"amdgpu_test_ib", &amdgpu_debugfs_test_ib}
3811};
3812
3813static int amdgpu_debugfs_test_ib_ring_init(struct amdgpu_device *adev)
3814{
3815 return amdgpu_debugfs_add_files(adev,
3816 amdgpu_debugfs_test_ib_ring_list, 1);
3817}
3818
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003819int amdgpu_debugfs_init(struct drm_minor *minor)
3820{
3821 return 0;
3822}
Alexander Kuleshov7cebc722015-06-27 13:16:05 +06003823#else
Arnd Bergmann27bad5b2017-06-21 23:51:02 +02003824static int amdgpu_debugfs_test_ib_ring_init(struct amdgpu_device *adev)
Huang Rui4f0955f2017-05-10 23:04:06 +08003825{
3826 return 0;
3827}
Alexander Kuleshov7cebc722015-06-27 13:16:05 +06003828static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
3829{
3830 return 0;
3831}
3832static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev) { }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003833#endif