blob: 77a32b79e8f2b36a0c8e4df00a90fbc27939bd5b [file] [log] [blame]
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001/*
2 * Copyright 2008 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 * Copyright 2009 Jerome Glisse.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors: Dave Airlie
25 * Alex Deucher
26 * Jerome Glisse
27 */
Chunming Zhou0875dc92016-06-12 15:41:58 +080028#include <linux/kthread.h>
Alex Deucherd38ceaf2015-04-20 16:55:21 -040029#include <linux/console.h>
30#include <linux/slab.h>
31#include <linux/debugfs.h>
32#include <drm/drmP.h>
33#include <drm/drm_crtc_helper.h>
34#include <drm/amdgpu_drm.h>
35#include <linux/vgaarb.h>
36#include <linux/vga_switcheroo.h>
37#include <linux/efi.h>
38#include "amdgpu.h"
Tom St Denisf4b373f2016-05-31 08:02:27 -040039#include "amdgpu_trace.h"
Alex Deucherd38ceaf2015-04-20 16:55:21 -040040#include "amdgpu_i2c.h"
41#include "atom.h"
42#include "amdgpu_atombios.h"
Alex Deuchera5bde2f2016-09-23 16:23:41 -040043#include "amdgpu_atomfirmware.h"
Alex Deucherd0dd7f02015-11-11 19:45:06 -050044#include "amd_pcie.h"
Ken Wang33f34802016-01-21 17:29:41 +080045#ifdef CONFIG_DRM_AMDGPU_SI
46#include "si.h"
47#endif
Alex Deuchera2e73f52015-04-20 17:09:27 -040048#ifdef CONFIG_DRM_AMDGPU_CIK
49#include "cik.h"
50#endif
Alex Deucheraaa36a92015-04-20 17:31:14 -040051#include "vi.h"
Ken Wang460826e2017-03-06 14:53:16 -050052#include "soc15.h"
Alex Deucherd38ceaf2015-04-20 16:55:21 -040053#include "bif/bif_4_1_d.h"
Emily Deng9accf2f2016-08-10 16:01:25 +080054#include <linux/pci.h>
Monk Liubec86372016-09-14 19:38:08 +080055#include <linux/firmware.h>
Gavin Wan89041942017-06-23 13:55:15 -040056#include "amdgpu_vf_error.h"
Alex Deucherd38ceaf2015-04-20 16:55:21 -040057
Yong Zhaoba997702015-11-09 17:21:45 -050058#include "amdgpu_amdkfd.h"
Alex Deucherd38ceaf2015-04-20 16:55:21 -040059
Alex Deuchere2a75f82017-04-27 16:58:01 -040060MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin");
Alex Deucher2d2e5e72017-05-09 12:27:35 -040061MODULE_FIRMWARE("amdgpu/raven_gpu_info.bin");
Alex Deuchere2a75f82017-04-27 16:58:01 -040062
Shirish S2dc80b02017-05-25 10:05:25 +053063#define AMDGPU_RESUME_MS 2000
64
Alex Deucherd38ceaf2015-04-20 16:55:21 -040065static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev);
66static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev);
Huang Rui4f0955f2017-05-10 23:04:06 +080067static int amdgpu_debugfs_test_ib_ring_init(struct amdgpu_device *adev);
Kent Russelldb95e212017-08-22 12:31:43 -040068static int amdgpu_debugfs_vbios_dump_init(struct amdgpu_device *adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040069
70static const char *amdgpu_asic_name[] = {
Ken Wangda69c1612016-01-21 19:08:55 +080071 "TAHITI",
72 "PITCAIRN",
73 "VERDE",
74 "OLAND",
75 "HAINAN",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040076 "BONAIRE",
77 "KAVERI",
78 "KABINI",
79 "HAWAII",
80 "MULLINS",
81 "TOPAZ",
82 "TONGA",
David Zhang48299f92015-07-08 01:05:16 +080083 "FIJI",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040084 "CARRIZO",
Samuel Li139f4912015-10-08 14:50:27 -040085 "STONEY",
Flora Cui2cc0c0b2016-03-14 18:33:29 -040086 "POLARIS10",
87 "POLARIS11",
Junwei Zhangc4642a42016-12-14 15:32:28 -050088 "POLARIS12",
Ken Wangd4196f02016-03-09 09:28:32 +080089 "VEGA10",
Chunming Zhou2ca8a5d2016-12-07 17:31:19 +080090 "RAVEN",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040091 "LAST",
92};
93
94bool amdgpu_device_is_px(struct drm_device *dev)
95{
96 struct amdgpu_device *adev = dev->dev_private;
97
Jammy Zhou2f7d10b2015-07-22 11:29:01 +080098 if (adev->flags & AMD_IS_PX)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040099 return true;
100 return false;
101}
102
103/*
104 * MMIO register access helper functions.
105 */
106uint32_t amdgpu_mm_rreg(struct amdgpu_device *adev, uint32_t reg,
Monk Liu15d72fd2017-01-25 15:07:40 +0800107 uint32_t acc_flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400108{
Tom St Denisf4b373f2016-05-31 08:02:27 -0400109 uint32_t ret;
110
Monk Liu15d72fd2017-01-25 15:07:40 +0800111 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev)) {
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800112 BUG_ON(in_interrupt());
113 return amdgpu_virt_kiq_rreg(adev, reg);
114 }
115
Monk Liu15d72fd2017-01-25 15:07:40 +0800116 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
Tom St Denisf4b373f2016-05-31 08:02:27 -0400117 ret = readl(((void __iomem *)adev->rmmio) + (reg * 4));
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400118 else {
119 unsigned long flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400120
121 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
122 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
123 ret = readl(((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
124 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400125 }
Tom St Denisf4b373f2016-05-31 08:02:27 -0400126 trace_amdgpu_mm_rreg(adev->pdev->device, reg, ret);
127 return ret;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400128}
129
130void amdgpu_mm_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v,
Monk Liu15d72fd2017-01-25 15:07:40 +0800131 uint32_t acc_flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400132{
Tom St Denisf4b373f2016-05-31 08:02:27 -0400133 trace_amdgpu_mm_wreg(adev->pdev->device, reg, v);
Monk Liu4e99a442016-03-31 13:26:59 +0800134
Ken Wang47ed4e12017-07-04 13:11:52 +0800135 if (adev->asic_type >= CHIP_VEGA10 && reg == 0) {
136 adev->last_mm_index = v;
137 }
138
Monk Liu15d72fd2017-01-25 15:07:40 +0800139 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev)) {
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800140 BUG_ON(in_interrupt());
141 return amdgpu_virt_kiq_wreg(adev, reg, v);
142 }
143
Monk Liu15d72fd2017-01-25 15:07:40 +0800144 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400145 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
146 else {
147 unsigned long flags;
148
149 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
150 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
151 writel(v, ((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
152 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
153 }
Ken Wang47ed4e12017-07-04 13:11:52 +0800154
155 if (adev->asic_type >= CHIP_VEGA10 && reg == 1 && adev->last_mm_index == 0x5702C) {
156 udelay(500);
157 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400158}
159
160u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 reg)
161{
162 if ((reg * 4) < adev->rio_mem_size)
163 return ioread32(adev->rio_mem + (reg * 4));
164 else {
165 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
166 return ioread32(adev->rio_mem + (mmMM_DATA * 4));
167 }
168}
169
170void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
171{
Ken Wang47ed4e12017-07-04 13:11:52 +0800172 if (adev->asic_type >= CHIP_VEGA10 && reg == 0) {
173 adev->last_mm_index = v;
174 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400175
176 if ((reg * 4) < adev->rio_mem_size)
177 iowrite32(v, adev->rio_mem + (reg * 4));
178 else {
179 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
180 iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
181 }
Ken Wang47ed4e12017-07-04 13:11:52 +0800182
183 if (adev->asic_type >= CHIP_VEGA10 && reg == 1 && adev->last_mm_index == 0x5702C) {
184 udelay(500);
185 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400186}
187
188/**
189 * amdgpu_mm_rdoorbell - read a doorbell dword
190 *
191 * @adev: amdgpu_device pointer
192 * @index: doorbell index
193 *
194 * Returns the value in the doorbell aperture at the
195 * requested doorbell index (CIK).
196 */
197u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
198{
199 if (index < adev->doorbell.num_doorbells) {
200 return readl(adev->doorbell.ptr + index);
201 } else {
202 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
203 return 0;
204 }
205}
206
207/**
208 * amdgpu_mm_wdoorbell - write a doorbell dword
209 *
210 * @adev: amdgpu_device pointer
211 * @index: doorbell index
212 * @v: value to write
213 *
214 * Writes @v to the doorbell aperture at the
215 * requested doorbell index (CIK).
216 */
217void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
218{
219 if (index < adev->doorbell.num_doorbells) {
220 writel(v, adev->doorbell.ptr + index);
221 } else {
222 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
223 }
224}
225
226/**
Ken Wang832be402016-03-18 15:23:08 +0800227 * amdgpu_mm_rdoorbell64 - read a doorbell Qword
228 *
229 * @adev: amdgpu_device pointer
230 * @index: doorbell index
231 *
232 * Returns the value in the doorbell aperture at the
233 * requested doorbell index (VEGA10+).
234 */
235u64 amdgpu_mm_rdoorbell64(struct amdgpu_device *adev, u32 index)
236{
237 if (index < adev->doorbell.num_doorbells) {
238 return atomic64_read((atomic64_t *)(adev->doorbell.ptr + index));
239 } else {
240 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
241 return 0;
242 }
243}
244
245/**
246 * amdgpu_mm_wdoorbell64 - write a doorbell Qword
247 *
248 * @adev: amdgpu_device pointer
249 * @index: doorbell index
250 * @v: value to write
251 *
252 * Writes @v to the doorbell aperture at the
253 * requested doorbell index (VEGA10+).
254 */
255void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v)
256{
257 if (index < adev->doorbell.num_doorbells) {
258 atomic64_set((atomic64_t *)(adev->doorbell.ptr + index), v);
259 } else {
260 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
261 }
262}
263
264/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400265 * amdgpu_invalid_rreg - dummy reg read function
266 *
267 * @adev: amdgpu device pointer
268 * @reg: offset of register
269 *
270 * Dummy register read function. Used for register blocks
271 * that certain asics don't have (all asics).
272 * Returns the value in the register.
273 */
274static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
275{
276 DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
277 BUG();
278 return 0;
279}
280
281/**
282 * amdgpu_invalid_wreg - dummy reg write function
283 *
284 * @adev: amdgpu device pointer
285 * @reg: offset of register
286 * @v: value to write to the register
287 *
288 * Dummy register read function. Used for register blocks
289 * that certain asics don't have (all asics).
290 */
291static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
292{
293 DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
294 reg, v);
295 BUG();
296}
297
298/**
299 * amdgpu_block_invalid_rreg - dummy reg read function
300 *
301 * @adev: amdgpu device pointer
302 * @block: offset of instance
303 * @reg: offset of register
304 *
305 * Dummy register read function. Used for register blocks
306 * that certain asics don't have (all asics).
307 * Returns the value in the register.
308 */
309static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
310 uint32_t block, uint32_t reg)
311{
312 DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
313 reg, block);
314 BUG();
315 return 0;
316}
317
318/**
319 * amdgpu_block_invalid_wreg - dummy reg write function
320 *
321 * @adev: amdgpu device pointer
322 * @block: offset of instance
323 * @reg: offset of register
324 * @v: value to write to the register
325 *
326 * Dummy register read function. Used for register blocks
327 * that certain asics don't have (all asics).
328 */
329static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
330 uint32_t block,
331 uint32_t reg, uint32_t v)
332{
333 DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
334 reg, block, v);
335 BUG();
336}
337
338static int amdgpu_vram_scratch_init(struct amdgpu_device *adev)
339{
Christian Königa4a02772017-07-27 17:24:36 +0200340 return amdgpu_bo_create_kernel(adev, AMDGPU_GPU_PAGE_SIZE,
341 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
342 &adev->vram_scratch.robj,
343 &adev->vram_scratch.gpu_addr,
344 (void **)&adev->vram_scratch.ptr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400345}
346
347static void amdgpu_vram_scratch_fini(struct amdgpu_device *adev)
348{
Christian König078af1a2017-07-27 17:43:00 +0200349 amdgpu_bo_free_kernel(&adev->vram_scratch.robj, NULL, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400350}
351
352/**
353 * amdgpu_program_register_sequence - program an array of registers.
354 *
355 * @adev: amdgpu_device pointer
356 * @registers: pointer to the register array
357 * @array_size: size of the register array
358 *
359 * Programs an array or registers with and and or masks.
360 * This is a helper for setting golden registers.
361 */
362void amdgpu_program_register_sequence(struct amdgpu_device *adev,
363 const u32 *registers,
364 const u32 array_size)
365{
366 u32 tmp, reg, and_mask, or_mask;
367 int i;
368
369 if (array_size % 3)
370 return;
371
372 for (i = 0; i < array_size; i +=3) {
373 reg = registers[i + 0];
374 and_mask = registers[i + 1];
375 or_mask = registers[i + 2];
376
377 if (and_mask == 0xffffffff) {
378 tmp = or_mask;
379 } else {
380 tmp = RREG32(reg);
381 tmp &= ~and_mask;
382 tmp |= or_mask;
383 }
384 WREG32(reg, tmp);
385 }
386}
387
388void amdgpu_pci_config_reset(struct amdgpu_device *adev)
389{
390 pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
391}
392
393/*
394 * GPU doorbell aperture helpers function.
395 */
396/**
397 * amdgpu_doorbell_init - Init doorbell driver information.
398 *
399 * @adev: amdgpu_device pointer
400 *
401 * Init doorbell driver information (CIK)
402 * Returns 0 on success, error on failure.
403 */
404static int amdgpu_doorbell_init(struct amdgpu_device *adev)
405{
Christian König705e5192017-06-08 11:15:16 +0200406 /* No doorbell on SI hardware generation */
407 if (adev->asic_type < CHIP_BONAIRE) {
408 adev->doorbell.base = 0;
409 adev->doorbell.size = 0;
410 adev->doorbell.num_doorbells = 0;
411 adev->doorbell.ptr = NULL;
412 return 0;
413 }
414
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400415 /* doorbell bar mapping */
416 adev->doorbell.base = pci_resource_start(adev->pdev, 2);
417 adev->doorbell.size = pci_resource_len(adev->pdev, 2);
418
Christian Königedf600d2016-05-03 15:54:54 +0200419 adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400420 AMDGPU_DOORBELL_MAX_ASSIGNMENT+1);
421 if (adev->doorbell.num_doorbells == 0)
422 return -EINVAL;
423
Christian König8972e5d2017-03-06 13:34:57 +0100424 adev->doorbell.ptr = ioremap(adev->doorbell.base,
425 adev->doorbell.num_doorbells *
426 sizeof(u32));
427 if (adev->doorbell.ptr == NULL)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400428 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400429
430 return 0;
431}
432
433/**
434 * amdgpu_doorbell_fini - Tear down doorbell driver information.
435 *
436 * @adev: amdgpu_device pointer
437 *
438 * Tear down doorbell driver information (CIK)
439 */
440static void amdgpu_doorbell_fini(struct amdgpu_device *adev)
441{
442 iounmap(adev->doorbell.ptr);
443 adev->doorbell.ptr = NULL;
444}
445
446/**
447 * amdgpu_doorbell_get_kfd_info - Report doorbell configuration required to
448 * setup amdkfd
449 *
450 * @adev: amdgpu_device pointer
451 * @aperture_base: output returning doorbell aperture base physical address
452 * @aperture_size: output returning doorbell aperture size in bytes
453 * @start_offset: output returning # of doorbell bytes reserved for amdgpu.
454 *
455 * amdgpu and amdkfd share the doorbell aperture. amdgpu sets it up,
456 * takes doorbells required for its own rings and reports the setup to amdkfd.
457 * amdgpu reserved doorbells are at the start of the doorbell aperture.
458 */
459void amdgpu_doorbell_get_kfd_info(struct amdgpu_device *adev,
460 phys_addr_t *aperture_base,
461 size_t *aperture_size,
462 size_t *start_offset)
463{
464 /*
465 * The first num_doorbells are used by amdgpu.
466 * amdkfd takes whatever's left in the aperture.
467 */
468 if (adev->doorbell.size > adev->doorbell.num_doorbells * sizeof(u32)) {
469 *aperture_base = adev->doorbell.base;
470 *aperture_size = adev->doorbell.size;
471 *start_offset = adev->doorbell.num_doorbells * sizeof(u32);
472 } else {
473 *aperture_base = 0;
474 *aperture_size = 0;
475 *start_offset = 0;
476 }
477}
478
479/*
480 * amdgpu_wb_*()
Alex Xie455a7bc2017-05-08 21:36:03 -0400481 * Writeback is the method by which the GPU updates special pages in memory
Alex Xieea81a172017-05-08 13:41:11 -0400482 * with the status of certain GPU events (fences, ring pointers,etc.).
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400483 */
484
485/**
486 * amdgpu_wb_fini - Disable Writeback and free memory
487 *
488 * @adev: amdgpu_device pointer
489 *
490 * Disables Writeback and frees the Writeback memory (all asics).
491 * Used at driver shutdown.
492 */
493static void amdgpu_wb_fini(struct amdgpu_device *adev)
494{
495 if (adev->wb.wb_obj) {
Alex Deuchera76ed482016-10-21 15:30:36 -0400496 amdgpu_bo_free_kernel(&adev->wb.wb_obj,
497 &adev->wb.gpu_addr,
498 (void **)&adev->wb.wb);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400499 adev->wb.wb_obj = NULL;
500 }
501}
502
503/**
504 * amdgpu_wb_init- Init Writeback driver info and allocate memory
505 *
506 * @adev: amdgpu_device pointer
507 *
Alex Xie455a7bc2017-05-08 21:36:03 -0400508 * Initializes writeback and allocates writeback memory (all asics).
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400509 * Used at driver startup.
510 * Returns 0 on success or an -error on failure.
511 */
512static int amdgpu_wb_init(struct amdgpu_device *adev)
513{
514 int r;
515
516 if (adev->wb.wb_obj == NULL) {
Alex Deucher97407b62017-07-28 12:14:15 -0400517 /* AMDGPU_MAX_WB * sizeof(uint32_t) * 8 = AMDGPU_MAX_WB 256bit slots */
518 r = amdgpu_bo_create_kernel(adev, AMDGPU_MAX_WB * sizeof(uint32_t) * 8,
Alex Deuchera76ed482016-10-21 15:30:36 -0400519 PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
520 &adev->wb.wb_obj, &adev->wb.gpu_addr,
521 (void **)&adev->wb.wb);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400522 if (r) {
523 dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
524 return r;
525 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400526
527 adev->wb.num_wb = AMDGPU_MAX_WB;
528 memset(&adev->wb.used, 0, sizeof(adev->wb.used));
529
530 /* clear wb memory */
Huang Rui60a970a62017-03-15 10:13:32 +0800531 memset((char *)adev->wb.wb, 0, AMDGPU_MAX_WB * sizeof(uint32_t));
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400532 }
533
534 return 0;
535}
536
537/**
538 * amdgpu_wb_get - Allocate a wb entry
539 *
540 * @adev: amdgpu_device pointer
541 * @wb: wb index
542 *
543 * Allocate a wb slot for use by the driver (all asics).
544 * Returns 0 on success or -EINVAL on failure.
545 */
546int amdgpu_wb_get(struct amdgpu_device *adev, u32 *wb)
547{
548 unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
Alex Deucher97407b62017-07-28 12:14:15 -0400549
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400550 if (offset < adev->wb.num_wb) {
551 __set_bit(offset, adev->wb.used);
Alex Deucher97407b62017-07-28 12:14:15 -0400552 *wb = offset * 8; /* convert to dw offset */
Monk Liu0915fdb2017-06-19 10:19:41 -0400553 return 0;
554 } else {
555 return -EINVAL;
556 }
557}
558
Ken Wang70142852016-03-18 15:08:49 +0800559/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400560 * amdgpu_wb_free - Free a wb entry
561 *
562 * @adev: amdgpu_device pointer
563 * @wb: wb index
564 *
565 * Free a wb slot allocated for use by the driver (all asics)
566 */
567void amdgpu_wb_free(struct amdgpu_device *adev, u32 wb)
568{
569 if (wb < adev->wb.num_wb)
570 __clear_bit(wb, adev->wb.used);
571}
572
573/**
574 * amdgpu_vram_location - try to find VRAM location
575 * @adev: amdgpu device structure holding all necessary informations
576 * @mc: memory controller structure holding memory informations
577 * @base: base address at which to put VRAM
578 *
Alex Xie455a7bc2017-05-08 21:36:03 -0400579 * Function will try to place VRAM at base address provided
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400580 * as parameter (which is so far either PCI aperture address or
581 * for IGP TOM base address).
582 *
583 * If there is not enough space to fit the unvisible VRAM in the 32bits
584 * address space then we limit the VRAM size to the aperture.
585 *
586 * Note: We don't explicitly enforce VRAM start to be aligned on VRAM size,
587 * this shouldn't be a problem as we are using the PCI aperture as a reference.
588 * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but
589 * not IGP.
590 *
591 * Note: we use mc_vram_size as on some board we need to program the mc to
592 * cover the whole aperture even if VRAM size is inferior to aperture size
593 * Novell bug 204882 + along with lots of ubuntu ones
594 *
595 * Note: when limiting vram it's safe to overwritte real_vram_size because
596 * we are not in case where real_vram_size is inferior to mc_vram_size (ie
597 * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu
598 * ones)
599 *
600 * Note: IGP TOM addr should be the same as the aperture addr, we don't
Alex Xie455a7bc2017-05-08 21:36:03 -0400601 * explicitly check for that though.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400602 *
603 * FIXME: when reducing VRAM size align new size on power of 2.
604 */
605void amdgpu_vram_location(struct amdgpu_device *adev, struct amdgpu_mc *mc, u64 base)
606{
607 uint64_t limit = (uint64_t)amdgpu_vram_limit << 20;
608
609 mc->vram_start = base;
610 if (mc->mc_vram_size > (adev->mc.mc_mask - base + 1)) {
611 dev_warn(adev->dev, "limiting VRAM to PCI aperture size\n");
612 mc->real_vram_size = mc->aper_size;
613 mc->mc_vram_size = mc->aper_size;
614 }
615 mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
616 if (limit && limit < mc->real_vram_size)
617 mc->real_vram_size = limit;
618 dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
619 mc->mc_vram_size >> 20, mc->vram_start,
620 mc->vram_end, mc->real_vram_size >> 20);
621}
622
623/**
Christian König6f02a692017-07-07 11:56:59 +0200624 * amdgpu_gart_location - try to find GTT location
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400625 * @adev: amdgpu device structure holding all necessary informations
626 * @mc: memory controller structure holding memory informations
627 *
628 * Function will place try to place GTT before or after VRAM.
629 *
630 * If GTT size is bigger than space left then we ajust GTT size.
631 * Thus function will never fails.
632 *
633 * FIXME: when reducing GTT size align new size on power of 2.
634 */
Christian König6f02a692017-07-07 11:56:59 +0200635void amdgpu_gart_location(struct amdgpu_device *adev, struct amdgpu_mc *mc)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400636{
637 u64 size_af, size_bf;
638
Christian Königed21c042017-07-06 22:26:05 +0200639 size_af = adev->mc.mc_mask - mc->vram_end;
640 size_bf = mc->vram_start;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400641 if (size_bf > size_af) {
Christian König6f02a692017-07-07 11:56:59 +0200642 if (mc->gart_size > size_bf) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400643 dev_warn(adev->dev, "limiting GTT\n");
Christian König6f02a692017-07-07 11:56:59 +0200644 mc->gart_size = size_bf;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400645 }
Christian König6f02a692017-07-07 11:56:59 +0200646 mc->gart_start = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400647 } else {
Christian König6f02a692017-07-07 11:56:59 +0200648 if (mc->gart_size > size_af) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400649 dev_warn(adev->dev, "limiting GTT\n");
Christian König6f02a692017-07-07 11:56:59 +0200650 mc->gart_size = size_af;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400651 }
Christian König6f02a692017-07-07 11:56:59 +0200652 mc->gart_start = mc->vram_end + 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400653 }
Christian König6f02a692017-07-07 11:56:59 +0200654 mc->gart_end = mc->gart_start + mc->gart_size - 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400655 dev_info(adev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
Christian König6f02a692017-07-07 11:56:59 +0200656 mc->gart_size >> 20, mc->gart_start, mc->gart_end);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400657}
658
659/*
660 * GPU helpers function.
661 */
662/**
Jim Quc836fec2017-02-10 15:59:59 +0800663 * amdgpu_need_post - check if the hw need post or not
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400664 *
665 * @adev: amdgpu_device pointer
666 *
Jim Quc836fec2017-02-10 15:59:59 +0800667 * Check if the asic has been initialized (all asics) at driver startup
668 * or post is needed if hw reset is performed.
669 * Returns true if need or false if not.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400670 */
Jim Quc836fec2017-02-10 15:59:59 +0800671bool amdgpu_need_post(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400672{
673 uint32_t reg;
674
Jim Quc836fec2017-02-10 15:59:59 +0800675 if (adev->has_hw_reset) {
676 adev->has_hw_reset = false;
677 return true;
678 }
Alex Deucher70d17a22017-06-30 17:26:47 -0400679
680 /* bios scratch used on CIK+ */
681 if (adev->asic_type >= CHIP_BONAIRE)
682 return amdgpu_atombios_scratch_need_asic_init(adev);
683
684 /* check MEM_SIZE for older asics */
Alex Deucherbbf282d2017-03-03 17:26:10 -0500685 reg = amdgpu_asic_get_config_memsize(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400686
Alex Deucherf2713e82017-03-28 12:19:31 -0400687 if ((reg != 0) && (reg != 0xffffffff))
Jim Quc836fec2017-02-10 15:59:59 +0800688 return false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400689
Jim Quc836fec2017-02-10 15:59:59 +0800690 return true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400691
692}
693
Monk Liubec86372016-09-14 19:38:08 +0800694static bool amdgpu_vpost_needed(struct amdgpu_device *adev)
695{
696 if (amdgpu_sriov_vf(adev))
697 return false;
698
699 if (amdgpu_passthrough(adev)) {
Monk Liu1da2c322016-11-11 11:24:29 +0800700 /* for FIJI: In whole GPU pass-through virtualization case, after VM reboot
701 * some old smc fw still need driver do vPost otherwise gpu hang, while
702 * those smc fw version above 22.15 doesn't have this flaw, so we force
703 * vpost executed for smc version below 22.15
Monk Liubec86372016-09-14 19:38:08 +0800704 */
705 if (adev->asic_type == CHIP_FIJI) {
706 int err;
707 uint32_t fw_ver;
708 err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
709 /* force vPost if error occured */
710 if (err)
711 return true;
712
713 fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
Monk Liu1da2c322016-11-11 11:24:29 +0800714 if (fw_ver < 0x00160e00)
715 return true;
Monk Liubec86372016-09-14 19:38:08 +0800716 }
Monk Liubec86372016-09-14 19:38:08 +0800717 }
Jim Quc836fec2017-02-10 15:59:59 +0800718 return amdgpu_need_post(adev);
Monk Liubec86372016-09-14 19:38:08 +0800719}
720
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400721/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400722 * amdgpu_dummy_page_init - init dummy page used by the driver
723 *
724 * @adev: amdgpu_device pointer
725 *
726 * Allocate the dummy page used by the driver (all asics).
727 * This dummy page is used by the driver as a filler for gart entries
728 * when pages are taken out of the GART
729 * Returns 0 on sucess, -ENOMEM on failure.
730 */
731int amdgpu_dummy_page_init(struct amdgpu_device *adev)
732{
733 if (adev->dummy_page.page)
734 return 0;
735 adev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
736 if (adev->dummy_page.page == NULL)
737 return -ENOMEM;
738 adev->dummy_page.addr = pci_map_page(adev->pdev, adev->dummy_page.page,
739 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
740 if (pci_dma_mapping_error(adev->pdev, adev->dummy_page.addr)) {
741 dev_err(&adev->pdev->dev, "Failed to DMA MAP the dummy page\n");
742 __free_page(adev->dummy_page.page);
743 adev->dummy_page.page = NULL;
744 return -ENOMEM;
745 }
746 return 0;
747}
748
749/**
750 * amdgpu_dummy_page_fini - free dummy page used by the driver
751 *
752 * @adev: amdgpu_device pointer
753 *
754 * Frees the dummy page used by the driver (all asics).
755 */
756void amdgpu_dummy_page_fini(struct amdgpu_device *adev)
757{
758 if (adev->dummy_page.page == NULL)
759 return;
760 pci_unmap_page(adev->pdev, adev->dummy_page.addr,
761 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
762 __free_page(adev->dummy_page.page);
763 adev->dummy_page.page = NULL;
764}
765
766
767/* ATOM accessor methods */
768/*
769 * ATOM is an interpreted byte code stored in tables in the vbios. The
770 * driver registers callbacks to access registers and the interpreter
771 * in the driver parses the tables and executes then to program specific
772 * actions (set display modes, asic init, etc.). See amdgpu_atombios.c,
773 * atombios.h, and atom.c
774 */
775
776/**
777 * cail_pll_read - read PLL register
778 *
779 * @info: atom card_info pointer
780 * @reg: PLL register offset
781 *
782 * Provides a PLL register accessor for the atom interpreter (r4xx+).
783 * Returns the value of the PLL register.
784 */
785static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
786{
787 return 0;
788}
789
790/**
791 * cail_pll_write - write PLL register
792 *
793 * @info: atom card_info pointer
794 * @reg: PLL register offset
795 * @val: value to write to the pll register
796 *
797 * Provides a PLL register accessor for the atom interpreter (r4xx+).
798 */
799static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
800{
801
802}
803
804/**
805 * cail_mc_read - read MC (Memory Controller) register
806 *
807 * @info: atom card_info pointer
808 * @reg: MC register offset
809 *
810 * Provides an MC register accessor for the atom interpreter (r4xx+).
811 * Returns the value of the MC register.
812 */
813static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
814{
815 return 0;
816}
817
818/**
819 * cail_mc_write - write MC (Memory Controller) register
820 *
821 * @info: atom card_info pointer
822 * @reg: MC register offset
823 * @val: value to write to the pll register
824 *
825 * Provides a MC register accessor for the atom interpreter (r4xx+).
826 */
827static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
828{
829
830}
831
832/**
833 * cail_reg_write - write MMIO register
834 *
835 * @info: atom card_info pointer
836 * @reg: MMIO register offset
837 * @val: value to write to the pll register
838 *
839 * Provides a MMIO register accessor for the atom interpreter (r4xx+).
840 */
841static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
842{
843 struct amdgpu_device *adev = info->dev->dev_private;
844
845 WREG32(reg, val);
846}
847
848/**
849 * cail_reg_read - read MMIO register
850 *
851 * @info: atom card_info pointer
852 * @reg: MMIO register offset
853 *
854 * Provides an MMIO register accessor for the atom interpreter (r4xx+).
855 * Returns the value of the MMIO register.
856 */
857static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
858{
859 struct amdgpu_device *adev = info->dev->dev_private;
860 uint32_t r;
861
862 r = RREG32(reg);
863 return r;
864}
865
866/**
867 * cail_ioreg_write - write IO register
868 *
869 * @info: atom card_info pointer
870 * @reg: IO register offset
871 * @val: value to write to the pll register
872 *
873 * Provides a IO register accessor for the atom interpreter (r4xx+).
874 */
875static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
876{
877 struct amdgpu_device *adev = info->dev->dev_private;
878
879 WREG32_IO(reg, val);
880}
881
882/**
883 * cail_ioreg_read - read IO register
884 *
885 * @info: atom card_info pointer
886 * @reg: IO register offset
887 *
888 * Provides an IO register accessor for the atom interpreter (r4xx+).
889 * Returns the value of the IO register.
890 */
891static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
892{
893 struct amdgpu_device *adev = info->dev->dev_private;
894 uint32_t r;
895
896 r = RREG32_IO(reg);
897 return r;
898}
899
Kent Russell5b41d942017-08-22 12:31:43 -0400900static ssize_t amdgpu_atombios_get_vbios_version(struct device *dev,
901 struct device_attribute *attr,
902 char *buf)
903{
904 struct drm_device *ddev = dev_get_drvdata(dev);
905 struct amdgpu_device *adev = ddev->dev_private;
906 struct atom_context *ctx = adev->mode_info.atom_context;
907
908 return snprintf(buf, PAGE_SIZE, "%s\n", ctx->vbios_version);
909}
910
911static DEVICE_ATTR(vbios_version, 0444, amdgpu_atombios_get_vbios_version,
912 NULL);
913
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400914/**
915 * amdgpu_atombios_fini - free the driver info and callbacks for atombios
916 *
917 * @adev: amdgpu_device pointer
918 *
919 * Frees the driver info and register access callbacks for the ATOM
920 * interpreter (r4xx+).
921 * Called at driver shutdown.
922 */
923static void amdgpu_atombios_fini(struct amdgpu_device *adev)
924{
Monk Liu89e0ec9f2016-05-27 19:34:11 +0800925 if (adev->mode_info.atom_context) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400926 kfree(adev->mode_info.atom_context->scratch);
Monk Liu89e0ec9f2016-05-27 19:34:11 +0800927 kfree(adev->mode_info.atom_context->iio);
928 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400929 kfree(adev->mode_info.atom_context);
930 adev->mode_info.atom_context = NULL;
931 kfree(adev->mode_info.atom_card_info);
932 adev->mode_info.atom_card_info = NULL;
Kent Russell5b41d942017-08-22 12:31:43 -0400933 device_remove_file(adev->dev, &dev_attr_vbios_version);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400934}
935
936/**
937 * amdgpu_atombios_init - init the driver info and callbacks for atombios
938 *
939 * @adev: amdgpu_device pointer
940 *
941 * Initializes the driver info and register access callbacks for the
942 * ATOM interpreter (r4xx+).
943 * Returns 0 on sucess, -ENOMEM on failure.
944 * Called at driver startup.
945 */
946static int amdgpu_atombios_init(struct amdgpu_device *adev)
947{
948 struct card_info *atom_card_info =
949 kzalloc(sizeof(struct card_info), GFP_KERNEL);
Kent Russell5b41d942017-08-22 12:31:43 -0400950 int ret;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400951
952 if (!atom_card_info)
953 return -ENOMEM;
954
955 adev->mode_info.atom_card_info = atom_card_info;
956 atom_card_info->dev = adev->ddev;
957 atom_card_info->reg_read = cail_reg_read;
958 atom_card_info->reg_write = cail_reg_write;
959 /* needed for iio ops */
960 if (adev->rio_mem) {
961 atom_card_info->ioreg_read = cail_ioreg_read;
962 atom_card_info->ioreg_write = cail_ioreg_write;
963 } else {
Amber Linb64a18c2017-01-04 08:06:58 -0500964 DRM_INFO("PCI I/O BAR is not found. Using MMIO to access ATOM BIOS\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400965 atom_card_info->ioreg_read = cail_reg_read;
966 atom_card_info->ioreg_write = cail_reg_write;
967 }
968 atom_card_info->mc_read = cail_mc_read;
969 atom_card_info->mc_write = cail_mc_write;
970 atom_card_info->pll_read = cail_pll_read;
971 atom_card_info->pll_write = cail_pll_write;
972
973 adev->mode_info.atom_context = amdgpu_atom_parse(atom_card_info, adev->bios);
974 if (!adev->mode_info.atom_context) {
975 amdgpu_atombios_fini(adev);
976 return -ENOMEM;
977 }
978
979 mutex_init(&adev->mode_info.atom_context->mutex);
Alex Deuchera5bde2f2016-09-23 16:23:41 -0400980 if (adev->is_atom_fw) {
981 amdgpu_atomfirmware_scratch_regs_init(adev);
982 amdgpu_atomfirmware_allocate_fb_scratch(adev);
983 } else {
984 amdgpu_atombios_scratch_regs_init(adev);
985 amdgpu_atombios_allocate_fb_scratch(adev);
986 }
Kent Russell5b41d942017-08-22 12:31:43 -0400987
988 ret = device_create_file(adev->dev, &dev_attr_vbios_version);
989 if (ret) {
990 DRM_ERROR("Failed to create device file for VBIOS version\n");
991 return ret;
992 }
993
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400994 return 0;
995}
996
997/* if we get transitioned to only one device, take VGA back */
998/**
999 * amdgpu_vga_set_decode - enable/disable vga decode
1000 *
1001 * @cookie: amdgpu_device pointer
1002 * @state: enable/disable vga decode
1003 *
1004 * Enable/disable vga decode (all asics).
1005 * Returns VGA resource flags.
1006 */
1007static unsigned int amdgpu_vga_set_decode(void *cookie, bool state)
1008{
1009 struct amdgpu_device *adev = cookie;
1010 amdgpu_asic_set_vga_state(adev, state);
1011 if (state)
1012 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
1013 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1014 else
1015 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1016}
1017
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001018static void amdgpu_check_block_size(struct amdgpu_device *adev)
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001019{
1020 /* defines number of bits in page table versus page directory,
1021 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
1022 * page table and the remaining bits are in the page directory */
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001023 if (amdgpu_vm_block_size == -1)
1024 return;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001025
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001026 if (amdgpu_vm_block_size < 9) {
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001027 dev_warn(adev->dev, "VM page table size (%d) too small\n",
1028 amdgpu_vm_block_size);
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001029 goto def_value;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001030 }
1031
1032 if (amdgpu_vm_block_size > 24 ||
1033 (amdgpu_vm_size * 1024) < (1ull << amdgpu_vm_block_size)) {
1034 dev_warn(adev->dev, "VM page table size (%d) too large\n",
1035 amdgpu_vm_block_size);
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001036 goto def_value;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001037 }
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001038
1039 return;
1040
1041def_value:
1042 amdgpu_vm_block_size = -1;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001043}
1044
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001045static void amdgpu_check_vm_size(struct amdgpu_device *adev)
1046{
Alex Deucher64dab072017-06-15 18:20:09 -04001047 /* no need to check the default value */
1048 if (amdgpu_vm_size == -1)
1049 return;
1050
Alex Deucher76117502017-06-21 12:31:41 -04001051 if (!is_power_of_2(amdgpu_vm_size)) {
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001052 dev_warn(adev->dev, "VM size (%d) must be a power of 2\n",
1053 amdgpu_vm_size);
1054 goto def_value;
1055 }
1056
1057 if (amdgpu_vm_size < 1) {
1058 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
1059 amdgpu_vm_size);
1060 goto def_value;
1061 }
1062
1063 /*
1064 * Max GPUVM size for Cayman, SI, CI VI are 40 bits.
1065 */
1066 if (amdgpu_vm_size > 1024) {
1067 dev_warn(adev->dev, "VM size (%d) too large, max is 1TB\n",
1068 amdgpu_vm_size);
1069 goto def_value;
1070 }
1071
1072 return;
1073
1074def_value:
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001075 amdgpu_vm_size = -1;
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001076}
1077
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001078/**
1079 * amdgpu_check_arguments - validate module params
1080 *
1081 * @adev: amdgpu_device pointer
1082 *
1083 * Validates certain module parameters and updates
1084 * the associated values used by the driver (all asics).
1085 */
1086static void amdgpu_check_arguments(struct amdgpu_device *adev)
1087{
Chunming Zhou5b011232015-12-10 17:34:33 +08001088 if (amdgpu_sched_jobs < 4) {
1089 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
1090 amdgpu_sched_jobs);
1091 amdgpu_sched_jobs = 4;
Alex Deucher76117502017-06-21 12:31:41 -04001092 } else if (!is_power_of_2(amdgpu_sched_jobs)){
Chunming Zhou5b011232015-12-10 17:34:33 +08001093 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
1094 amdgpu_sched_jobs);
1095 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
1096 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001097
Alex Deucher83e74db2017-08-21 11:58:25 -04001098 if (amdgpu_gart_size != -1 && amdgpu_gart_size < 32) {
Christian Königf9321cc2017-07-07 13:44:05 +02001099 /* gart size must be greater or equal to 32M */
1100 dev_warn(adev->dev, "gart size (%d) too small\n",
1101 amdgpu_gart_size);
Alex Deucher83e74db2017-08-21 11:58:25 -04001102 amdgpu_gart_size = -1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001103 }
1104
Christian König36d38372017-07-07 13:17:45 +02001105 if (amdgpu_gtt_size != -1 && amdgpu_gtt_size < 32) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001106 /* gtt size must be greater or equal to 32M */
Christian König36d38372017-07-07 13:17:45 +02001107 dev_warn(adev->dev, "gtt size (%d) too small\n",
1108 amdgpu_gtt_size);
1109 amdgpu_gtt_size = -1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001110 }
1111
Roger Hed07f14b2017-08-15 16:05:59 +08001112 /* valid range is between 4 and 9 inclusive */
1113 if (amdgpu_vm_fragment_size != -1 &&
1114 (amdgpu_vm_fragment_size > 9 || amdgpu_vm_fragment_size < 4)) {
1115 dev_warn(adev->dev, "valid range is between 4 and 9\n");
1116 amdgpu_vm_fragment_size = -1;
1117 }
1118
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001119 amdgpu_check_vm_size(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001120
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001121 amdgpu_check_block_size(adev);
Christian König6a7f76e2016-08-24 15:51:49 +02001122
jimqu526bae32016-11-07 09:53:10 +08001123 if (amdgpu_vram_page_split != -1 && (amdgpu_vram_page_split < 16 ||
Alex Deucher76117502017-06-21 12:31:41 -04001124 !is_power_of_2(amdgpu_vram_page_split))) {
Christian König6a7f76e2016-08-24 15:51:49 +02001125 dev_warn(adev->dev, "invalid VRAM page split (%d)\n",
1126 amdgpu_vram_page_split);
1127 amdgpu_vram_page_split = 1024;
1128 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001129}
1130
1131/**
1132 * amdgpu_switcheroo_set_state - set switcheroo state
1133 *
1134 * @pdev: pci dev pointer
Lukas Wunner16944672015-09-05 11:17:35 +02001135 * @state: vga_switcheroo state
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001136 *
1137 * Callback for the switcheroo driver. Suspends or resumes the
1138 * the asics before or after it is powered up using ACPI methods.
1139 */
1140static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1141{
1142 struct drm_device *dev = pci_get_drvdata(pdev);
1143
1144 if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
1145 return;
1146
1147 if (state == VGA_SWITCHEROO_ON) {
Joe Perches7ca85292017-02-28 04:55:52 -08001148 pr_info("amdgpu: switched on\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001149 /* don't suspend or resume card normally */
1150 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1151
Alex Deucher810ddc32016-08-23 13:25:49 -04001152 amdgpu_device_resume(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001153
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001154 dev->switch_power_state = DRM_SWITCH_POWER_ON;
1155 drm_kms_helper_poll_enable(dev);
1156 } else {
Joe Perches7ca85292017-02-28 04:55:52 -08001157 pr_info("amdgpu: switched off\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001158 drm_kms_helper_poll_disable(dev);
1159 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Alex Deucher810ddc32016-08-23 13:25:49 -04001160 amdgpu_device_suspend(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001161 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1162 }
1163}
1164
1165/**
1166 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1167 *
1168 * @pdev: pci dev pointer
1169 *
1170 * Callback for the switcheroo driver. Check of the switcheroo
1171 * state can be changed.
1172 * Returns true if the state can be changed, false if not.
1173 */
1174static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1175{
1176 struct drm_device *dev = pci_get_drvdata(pdev);
1177
1178 /*
1179 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1180 * locking inversion with the driver load path. And the access here is
1181 * completely racy anyway. So don't bother with locking for now.
1182 */
1183 return dev->open_count == 0;
1184}
1185
1186static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1187 .set_gpu_state = amdgpu_switcheroo_set_state,
1188 .reprobe = NULL,
1189 .can_switch = amdgpu_switcheroo_can_switch,
1190};
1191
1192int amdgpu_set_clockgating_state(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001193 enum amd_ip_block_type block_type,
1194 enum amd_clockgating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001195{
1196 int i, r = 0;
1197
1198 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001199 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001200 continue;
Rex Zhuc7228652017-02-22 15:33:46 +08001201 if (adev->ip_blocks[i].version->type != block_type)
1202 continue;
1203 if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
1204 continue;
1205 r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
1206 (void *)adev, state);
1207 if (r)
1208 DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
1209 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001210 }
1211 return r;
1212}
1213
1214int amdgpu_set_powergating_state(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001215 enum amd_ip_block_type block_type,
1216 enum amd_powergating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001217{
1218 int i, r = 0;
1219
1220 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001221 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001222 continue;
Rex Zhuc7228652017-02-22 15:33:46 +08001223 if (adev->ip_blocks[i].version->type != block_type)
1224 continue;
1225 if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
1226 continue;
1227 r = adev->ip_blocks[i].version->funcs->set_powergating_state(
1228 (void *)adev, state);
1229 if (r)
1230 DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
1231 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001232 }
1233 return r;
1234}
1235
Huang Rui6cb2d4e2017-01-05 18:44:41 +08001236void amdgpu_get_clockgating_state(struct amdgpu_device *adev, u32 *flags)
1237{
1238 int i;
1239
1240 for (i = 0; i < adev->num_ip_blocks; i++) {
1241 if (!adev->ip_blocks[i].status.valid)
1242 continue;
1243 if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
1244 adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
1245 }
1246}
1247
Alex Deucher5dbbb602016-06-23 11:41:04 -04001248int amdgpu_wait_for_idle(struct amdgpu_device *adev,
1249 enum amd_ip_block_type block_type)
1250{
1251 int i, r;
1252
1253 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001254 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001255 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001256 if (adev->ip_blocks[i].version->type == block_type) {
1257 r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -04001258 if (r)
1259 return r;
1260 break;
1261 }
1262 }
1263 return 0;
1264
1265}
1266
1267bool amdgpu_is_idle(struct amdgpu_device *adev,
1268 enum amd_ip_block_type block_type)
1269{
1270 int i;
1271
1272 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001273 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001274 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001275 if (adev->ip_blocks[i].version->type == block_type)
1276 return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -04001277 }
1278 return true;
1279
1280}
1281
Alex Deuchera1255102016-10-13 17:41:13 -04001282struct amdgpu_ip_block * amdgpu_get_ip_block(struct amdgpu_device *adev,
1283 enum amd_ip_block_type type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001284{
1285 int i;
1286
1287 for (i = 0; i < adev->num_ip_blocks; i++)
Alex Deuchera1255102016-10-13 17:41:13 -04001288 if (adev->ip_blocks[i].version->type == type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001289 return &adev->ip_blocks[i];
1290
1291 return NULL;
1292}
1293
1294/**
1295 * amdgpu_ip_block_version_cmp
1296 *
1297 * @adev: amdgpu_device pointer
yanyang15fc3aee2015-05-22 14:39:35 -04001298 * @type: enum amd_ip_block_type
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001299 * @major: major version
1300 * @minor: minor version
1301 *
1302 * return 0 if equal or greater
1303 * return 1 if smaller or the ip_block doesn't exist
1304 */
1305int amdgpu_ip_block_version_cmp(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001306 enum amd_ip_block_type type,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001307 u32 major, u32 minor)
1308{
Alex Deuchera1255102016-10-13 17:41:13 -04001309 struct amdgpu_ip_block *ip_block = amdgpu_get_ip_block(adev, type);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001310
Alex Deuchera1255102016-10-13 17:41:13 -04001311 if (ip_block && ((ip_block->version->major > major) ||
1312 ((ip_block->version->major == major) &&
1313 (ip_block->version->minor >= minor))))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001314 return 0;
1315
1316 return 1;
1317}
1318
Alex Deuchera1255102016-10-13 17:41:13 -04001319/**
1320 * amdgpu_ip_block_add
1321 *
1322 * @adev: amdgpu_device pointer
1323 * @ip_block_version: pointer to the IP to add
1324 *
1325 * Adds the IP block driver information to the collection of IPs
1326 * on the asic.
1327 */
1328int amdgpu_ip_block_add(struct amdgpu_device *adev,
1329 const struct amdgpu_ip_block_version *ip_block_version)
1330{
1331 if (!ip_block_version)
1332 return -EINVAL;
1333
Huang Ruia0bae352017-05-03 09:52:06 +08001334 DRM_DEBUG("add ip block number %d <%s>\n", adev->num_ip_blocks,
1335 ip_block_version->funcs->name);
1336
Alex Deuchera1255102016-10-13 17:41:13 -04001337 adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
1338
1339 return 0;
1340}
1341
Alex Deucher483ef982016-09-30 12:43:04 -04001342static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
Emily Deng9accf2f2016-08-10 16:01:25 +08001343{
1344 adev->enable_virtual_display = false;
1345
1346 if (amdgpu_virtual_display) {
1347 struct drm_device *ddev = adev->ddev;
1348 const char *pci_address_name = pci_name(ddev->pdev);
Emily Deng0f663562016-09-30 13:02:18 -04001349 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
Emily Deng9accf2f2016-08-10 16:01:25 +08001350
1351 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1352 pciaddstr_tmp = pciaddstr;
Emily Deng0f663562016-09-30 13:02:18 -04001353 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1354 pciaddname = strsep(&pciaddname_tmp, ",");
Yintian Tao967de2a2017-01-22 15:16:51 +08001355 if (!strcmp("all", pciaddname)
1356 || !strcmp(pci_address_name, pciaddname)) {
Emily Deng0f663562016-09-30 13:02:18 -04001357 long num_crtc;
1358 int res = -1;
1359
Emily Deng9accf2f2016-08-10 16:01:25 +08001360 adev->enable_virtual_display = true;
Emily Deng0f663562016-09-30 13:02:18 -04001361
1362 if (pciaddname_tmp)
1363 res = kstrtol(pciaddname_tmp, 10,
1364 &num_crtc);
1365
1366 if (!res) {
1367 if (num_crtc < 1)
1368 num_crtc = 1;
1369 if (num_crtc > 6)
1370 num_crtc = 6;
1371 adev->mode_info.num_crtc = num_crtc;
1372 } else {
1373 adev->mode_info.num_crtc = 1;
1374 }
Emily Deng9accf2f2016-08-10 16:01:25 +08001375 break;
1376 }
1377 }
1378
Emily Deng0f663562016-09-30 13:02:18 -04001379 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
1380 amdgpu_virtual_display, pci_address_name,
1381 adev->enable_virtual_display, adev->mode_info.num_crtc);
Emily Deng9accf2f2016-08-10 16:01:25 +08001382
1383 kfree(pciaddstr);
1384 }
1385}
1386
Alex Deuchere2a75f82017-04-27 16:58:01 -04001387static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev)
1388{
Alex Deuchere2a75f82017-04-27 16:58:01 -04001389 const char *chip_name;
1390 char fw_name[30];
1391 int err;
1392 const struct gpu_info_firmware_header_v1_0 *hdr;
1393
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001394 adev->firmware.gpu_info_fw = NULL;
1395
Alex Deuchere2a75f82017-04-27 16:58:01 -04001396 switch (adev->asic_type) {
1397 case CHIP_TOPAZ:
1398 case CHIP_TONGA:
1399 case CHIP_FIJI:
1400 case CHIP_POLARIS11:
1401 case CHIP_POLARIS10:
1402 case CHIP_POLARIS12:
1403 case CHIP_CARRIZO:
1404 case CHIP_STONEY:
1405#ifdef CONFIG_DRM_AMDGPU_SI
1406 case CHIP_VERDE:
1407 case CHIP_TAHITI:
1408 case CHIP_PITCAIRN:
1409 case CHIP_OLAND:
1410 case CHIP_HAINAN:
1411#endif
1412#ifdef CONFIG_DRM_AMDGPU_CIK
1413 case CHIP_BONAIRE:
1414 case CHIP_HAWAII:
1415 case CHIP_KAVERI:
1416 case CHIP_KABINI:
1417 case CHIP_MULLINS:
1418#endif
1419 default:
1420 return 0;
1421 case CHIP_VEGA10:
1422 chip_name = "vega10";
1423 break;
Alex Deucher2d2e5e72017-05-09 12:27:35 -04001424 case CHIP_RAVEN:
1425 chip_name = "raven";
1426 break;
Alex Deuchere2a75f82017-04-27 16:58:01 -04001427 }
1428
1429 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name);
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001430 err = request_firmware(&adev->firmware.gpu_info_fw, fw_name, adev->dev);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001431 if (err) {
1432 dev_err(adev->dev,
1433 "Failed to load gpu_info firmware \"%s\"\n",
1434 fw_name);
1435 goto out;
1436 }
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001437 err = amdgpu_ucode_validate(adev->firmware.gpu_info_fw);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001438 if (err) {
1439 dev_err(adev->dev,
1440 "Failed to validate gpu_info firmware \"%s\"\n",
1441 fw_name);
1442 goto out;
1443 }
1444
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001445 hdr = (const struct gpu_info_firmware_header_v1_0 *)adev->firmware.gpu_info_fw->data;
Alex Deuchere2a75f82017-04-27 16:58:01 -04001446 amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
1447
1448 switch (hdr->version_major) {
1449 case 1:
1450 {
1451 const struct gpu_info_firmware_v1_0 *gpu_info_fw =
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001452 (const struct gpu_info_firmware_v1_0 *)(adev->firmware.gpu_info_fw->data +
Alex Deuchere2a75f82017-04-27 16:58:01 -04001453 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1454
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001455 adev->gfx.config.max_shader_engines = le32_to_cpu(gpu_info_fw->gc_num_se);
1456 adev->gfx.config.max_cu_per_sh = le32_to_cpu(gpu_info_fw->gc_num_cu_per_sh);
1457 adev->gfx.config.max_sh_per_se = le32_to_cpu(gpu_info_fw->gc_num_sh_per_se);
1458 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 -04001459 adev->gfx.config.max_texture_channel_caches =
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001460 le32_to_cpu(gpu_info_fw->gc_num_tccs);
1461 adev->gfx.config.max_gprs = le32_to_cpu(gpu_info_fw->gc_num_gprs);
1462 adev->gfx.config.max_gs_threads = le32_to_cpu(gpu_info_fw->gc_num_max_gs_thds);
1463 adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gpu_info_fw->gc_gs_table_depth);
1464 adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gpu_info_fw->gc_gsprim_buff_depth);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001465 adev->gfx.config.double_offchip_lds_buf =
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001466 le32_to_cpu(gpu_info_fw->gc_double_offchip_lds_buffer);
1467 adev->gfx.cu_info.wave_front_size = le32_to_cpu(gpu_info_fw->gc_wave_size);
Hawking Zhang51fd0372017-06-09 22:30:52 +08001468 adev->gfx.cu_info.max_waves_per_simd =
1469 le32_to_cpu(gpu_info_fw->gc_max_waves_per_simd);
1470 adev->gfx.cu_info.max_scratch_slots_per_cu =
1471 le32_to_cpu(gpu_info_fw->gc_max_scratch_slots_per_cu);
1472 adev->gfx.cu_info.lds_size = le32_to_cpu(gpu_info_fw->gc_lds_size);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001473 break;
1474 }
1475 default:
1476 dev_err(adev->dev,
1477 "Unsupported gpu_info table %d\n", hdr->header.ucode_version);
1478 err = -EINVAL;
1479 goto out;
1480 }
1481out:
Alex Deuchere2a75f82017-04-27 16:58:01 -04001482 return err;
1483}
1484
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001485static int amdgpu_early_init(struct amdgpu_device *adev)
1486{
Alex Deucheraaa36a92015-04-20 17:31:14 -04001487 int i, r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001488
Alex Deucher483ef982016-09-30 12:43:04 -04001489 amdgpu_device_enable_virtual_display(adev);
Emily Denga6be7572016-08-08 11:37:50 +08001490
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001491 switch (adev->asic_type) {
Alex Deucheraaa36a92015-04-20 17:31:14 -04001492 case CHIP_TOPAZ:
1493 case CHIP_TONGA:
David Zhang48299f92015-07-08 01:05:16 +08001494 case CHIP_FIJI:
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001495 case CHIP_POLARIS11:
1496 case CHIP_POLARIS10:
Junwei Zhangc4642a42016-12-14 15:32:28 -05001497 case CHIP_POLARIS12:
Alex Deucheraaa36a92015-04-20 17:31:14 -04001498 case CHIP_CARRIZO:
Samuel Li39bb0c92015-10-08 16:31:43 -04001499 case CHIP_STONEY:
1500 if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
Alex Deucheraaa36a92015-04-20 17:31:14 -04001501 adev->family = AMDGPU_FAMILY_CZ;
1502 else
1503 adev->family = AMDGPU_FAMILY_VI;
1504
1505 r = vi_set_ip_blocks(adev);
1506 if (r)
1507 return r;
1508 break;
Ken Wang33f34802016-01-21 17:29:41 +08001509#ifdef CONFIG_DRM_AMDGPU_SI
1510 case CHIP_VERDE:
1511 case CHIP_TAHITI:
1512 case CHIP_PITCAIRN:
1513 case CHIP_OLAND:
1514 case CHIP_HAINAN:
Ken Wang295d0da2016-05-24 21:02:53 +08001515 adev->family = AMDGPU_FAMILY_SI;
Ken Wang33f34802016-01-21 17:29:41 +08001516 r = si_set_ip_blocks(adev);
1517 if (r)
1518 return r;
1519 break;
1520#endif
Alex Deuchera2e73f52015-04-20 17:09:27 -04001521#ifdef CONFIG_DRM_AMDGPU_CIK
1522 case CHIP_BONAIRE:
1523 case CHIP_HAWAII:
1524 case CHIP_KAVERI:
1525 case CHIP_KABINI:
1526 case CHIP_MULLINS:
1527 if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
1528 adev->family = AMDGPU_FAMILY_CI;
1529 else
1530 adev->family = AMDGPU_FAMILY_KV;
1531
1532 r = cik_set_ip_blocks(adev);
1533 if (r)
1534 return r;
1535 break;
1536#endif
Chunming Zhou2ca8a5d2016-12-07 17:31:19 +08001537 case CHIP_VEGA10:
1538 case CHIP_RAVEN:
1539 if (adev->asic_type == CHIP_RAVEN)
1540 adev->family = AMDGPU_FAMILY_RV;
1541 else
1542 adev->family = AMDGPU_FAMILY_AI;
Ken Wang460826e2017-03-06 14:53:16 -05001543
1544 r = soc15_set_ip_blocks(adev);
1545 if (r)
1546 return r;
1547 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001548 default:
1549 /* FIXME: not supported yet */
1550 return -EINVAL;
1551 }
1552
Alex Deuchere2a75f82017-04-27 16:58:01 -04001553 r = amdgpu_device_parse_gpu_info_fw(adev);
1554 if (r)
1555 return r;
1556
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001557 if (amdgpu_sriov_vf(adev)) {
1558 r = amdgpu_virt_request_full_gpu(adev, true);
1559 if (r)
1560 return r;
1561 }
1562
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001563 for (i = 0; i < adev->num_ip_blocks; i++) {
1564 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
Huang Ruied8cf002017-05-03 09:40:17 +08001565 DRM_ERROR("disabled ip block: %d <%s>\n",
1566 i, adev->ip_blocks[i].version->funcs->name);
Alex Deuchera1255102016-10-13 17:41:13 -04001567 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001568 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001569 if (adev->ip_blocks[i].version->funcs->early_init) {
1570 r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001571 if (r == -ENOENT) {
Alex Deuchera1255102016-10-13 17:41:13 -04001572 adev->ip_blocks[i].status.valid = false;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001573 } else if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001574 DRM_ERROR("early_init of IP block <%s> failed %d\n",
1575 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001576 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001577 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001578 adev->ip_blocks[i].status.valid = true;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001579 }
Alex Deucher974e6b62015-07-10 13:59:44 -04001580 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001581 adev->ip_blocks[i].status.valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001582 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001583 }
1584 }
1585
Nicolai Hähnle395d1fb2016-06-02 12:32:07 +02001586 adev->cg_flags &= amdgpu_cg_mask;
1587 adev->pg_flags &= amdgpu_pg_mask;
1588
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001589 return 0;
1590}
1591
1592static int amdgpu_init(struct amdgpu_device *adev)
1593{
1594 int i, r;
1595
1596 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001597 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001598 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001599 r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001600 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001601 DRM_ERROR("sw_init of IP block <%s> failed %d\n",
1602 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001603 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001604 }
Alex Deuchera1255102016-10-13 17:41:13 -04001605 adev->ip_blocks[i].status.sw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001606 /* need to do gmc hw init early so we can allocate gpu mem */
Alex Deuchera1255102016-10-13 17:41:13 -04001607 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001608 r = amdgpu_vram_scratch_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001609 if (r) {
1610 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001611 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001612 }
Alex Deuchera1255102016-10-13 17:41:13 -04001613 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001614 if (r) {
1615 DRM_ERROR("hw_init %d failed %d\n", i, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001616 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001617 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001618 r = amdgpu_wb_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001619 if (r) {
1620 DRM_ERROR("amdgpu_wb_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001621 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001622 }
Alex Deuchera1255102016-10-13 17:41:13 -04001623 adev->ip_blocks[i].status.hw = true;
Monk Liu24936642017-01-09 15:54:32 +08001624
1625 /* right after GMC hw init, we create CSA */
1626 if (amdgpu_sriov_vf(adev)) {
1627 r = amdgpu_allocate_static_csa(adev);
1628 if (r) {
1629 DRM_ERROR("allocate CSA failed %d\n", r);
1630 return r;
1631 }
1632 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001633 }
1634 }
1635
1636 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001637 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001638 continue;
1639 /* gmc hw init is done early */
Alex Deuchera1255102016-10-13 17:41:13 -04001640 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001641 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001642 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001643 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001644 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
1645 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001646 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001647 }
Alex Deuchera1255102016-10-13 17:41:13 -04001648 adev->ip_blocks[i].status.hw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001649 }
1650
1651 return 0;
1652}
1653
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08001654static void amdgpu_fill_reset_magic(struct amdgpu_device *adev)
1655{
1656 memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM);
1657}
1658
1659static bool amdgpu_check_vram_lost(struct amdgpu_device *adev)
1660{
1661 return !!memcmp(adev->gart.ptr, adev->reset_magic,
1662 AMDGPU_RESET_MAGIC_NUM);
1663}
1664
Shirish S2dc80b02017-05-25 10:05:25 +05301665static int amdgpu_late_set_cg_state(struct amdgpu_device *adev)
1666{
1667 int i = 0, r;
1668
1669 for (i = 0; i < adev->num_ip_blocks; i++) {
1670 if (!adev->ip_blocks[i].status.valid)
1671 continue;
1672 /* skip CG for VCE/UVD, it's handled specially */
1673 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1674 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1675 /* enable clockgating to save power */
1676 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1677 AMD_CG_STATE_GATE);
1678 if (r) {
1679 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
1680 adev->ip_blocks[i].version->funcs->name, r);
1681 return r;
1682 }
1683 }
1684 }
1685 return 0;
1686}
1687
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001688static int amdgpu_late_init(struct amdgpu_device *adev)
1689{
1690 int i = 0, r;
1691
1692 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001693 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001694 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001695 if (adev->ip_blocks[i].version->funcs->late_init) {
1696 r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001697 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001698 DRM_ERROR("late_init of IP block <%s> failed %d\n",
1699 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001700 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001701 }
Alex Deuchera1255102016-10-13 17:41:13 -04001702 adev->ip_blocks[i].status.late_initialized = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001703 }
1704 }
1705
Shirish S2dc80b02017-05-25 10:05:25 +05301706 mod_delayed_work(system_wq, &adev->late_init_work,
1707 msecs_to_jiffies(AMDGPU_RESUME_MS));
1708
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08001709 amdgpu_fill_reset_magic(adev);
1710
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001711 return 0;
1712}
1713
1714static int amdgpu_fini(struct amdgpu_device *adev)
1715{
1716 int i, r;
1717
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001718 /* need to disable SMC first */
1719 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001720 if (!adev->ip_blocks[i].status.hw)
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001721 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001722 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001723 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
Alex Deuchera1255102016-10-13 17:41:13 -04001724 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1725 AMD_CG_STATE_UNGATE);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001726 if (r) {
1727 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001728 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001729 return r;
1730 }
Alex Deuchera1255102016-10-13 17:41:13 -04001731 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001732 /* XXX handle errors */
1733 if (r) {
1734 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001735 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001736 }
Alex Deuchera1255102016-10-13 17:41:13 -04001737 adev->ip_blocks[i].status.hw = false;
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001738 break;
1739 }
1740 }
1741
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001742 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001743 if (!adev->ip_blocks[i].status.hw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001744 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001745 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001746 amdgpu_wb_fini(adev);
1747 amdgpu_vram_scratch_fini(adev);
1748 }
Rex Zhu8201a672016-11-24 21:44:44 +08001749
1750 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1751 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1752 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
1753 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1754 AMD_CG_STATE_UNGATE);
1755 if (r) {
1756 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1757 adev->ip_blocks[i].version->funcs->name, r);
1758 return r;
1759 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001760 }
Rex Zhu8201a672016-11-24 21:44:44 +08001761
Alex Deuchera1255102016-10-13 17:41:13 -04001762 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001763 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001764 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001765 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
1766 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001767 }
Rex Zhu8201a672016-11-24 21:44:44 +08001768
Alex Deuchera1255102016-10-13 17:41:13 -04001769 adev->ip_blocks[i].status.hw = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001770 }
1771
1772 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001773 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001774 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001775 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001776 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001777 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001778 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
1779 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001780 }
Alex Deuchera1255102016-10-13 17:41:13 -04001781 adev->ip_blocks[i].status.sw = false;
1782 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001783 }
1784
Monk Liua6dcfd92016-05-19 14:36:34 +08001785 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001786 if (!adev->ip_blocks[i].status.late_initialized)
Grazvydas Ignotas8a2eef12016-10-03 00:06:44 +03001787 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001788 if (adev->ip_blocks[i].version->funcs->late_fini)
1789 adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
1790 adev->ip_blocks[i].status.late_initialized = false;
Monk Liua6dcfd92016-05-19 14:36:34 +08001791 }
1792
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001793 if (amdgpu_sriov_vf(adev)) {
Monk Liu24936642017-01-09 15:54:32 +08001794 amdgpu_bo_free_kernel(&adev->virt.csa_obj, &adev->virt.csa_vmid0_addr, NULL);
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001795 amdgpu_virt_release_full_gpu(adev, false);
1796 }
Monk Liu24936642017-01-09 15:54:32 +08001797
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001798 return 0;
1799}
1800
Shirish S2dc80b02017-05-25 10:05:25 +05301801static void amdgpu_late_init_func_handler(struct work_struct *work)
1802{
1803 struct amdgpu_device *adev =
1804 container_of(work, struct amdgpu_device, late_init_work.work);
1805 amdgpu_late_set_cg_state(adev);
1806}
1807
Alex Deucherfaefba92016-12-06 10:38:29 -05001808int amdgpu_suspend(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001809{
1810 int i, r;
1811
Xiangliang Yue941ea92017-01-18 12:47:55 +08001812 if (amdgpu_sriov_vf(adev))
1813 amdgpu_virt_request_full_gpu(adev, false);
1814
Flora Cuic5a93a22016-02-26 10:45:25 +08001815 /* ungate SMC block first */
1816 r = amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC,
1817 AMD_CG_STATE_UNGATE);
1818 if (r) {
1819 DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n",r);
1820 }
1821
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001822 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001823 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001824 continue;
1825 /* ungate blocks so that suspend can properly shut them down */
Flora Cuic5a93a22016-02-26 10:45:25 +08001826 if (i != AMD_IP_BLOCK_TYPE_SMC) {
Alex Deuchera1255102016-10-13 17:41:13 -04001827 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1828 AMD_CG_STATE_UNGATE);
Flora Cuic5a93a22016-02-26 10:45:25 +08001829 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001830 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1831 adev->ip_blocks[i].version->funcs->name, r);
Flora Cuic5a93a22016-02-26 10:45:25 +08001832 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001833 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001834 /* XXX handle errors */
Alex Deuchera1255102016-10-13 17:41:13 -04001835 r = adev->ip_blocks[i].version->funcs->suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001836 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001837 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001838 DRM_ERROR("suspend of IP block <%s> failed %d\n",
1839 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001840 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001841 }
1842
Xiangliang Yue941ea92017-01-18 12:47:55 +08001843 if (amdgpu_sriov_vf(adev))
1844 amdgpu_virt_release_full_gpu(adev, false);
1845
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001846 return 0;
1847}
1848
Monk Liue4f0fdc2017-02-09 11:55:49 +08001849static int amdgpu_sriov_reinit_early(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08001850{
1851 int i, r;
1852
Monk Liu2cb681b2017-04-26 12:00:49 +08001853 static enum amd_ip_block_type ip_order[] = {
1854 AMD_IP_BLOCK_TYPE_GMC,
1855 AMD_IP_BLOCK_TYPE_COMMON,
Monk Liu2cb681b2017-04-26 12:00:49 +08001856 AMD_IP_BLOCK_TYPE_IH,
1857 };
Monk Liua90ad3c2017-01-23 14:22:08 +08001858
Monk Liu2cb681b2017-04-26 12:00:49 +08001859 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1860 int j;
1861 struct amdgpu_ip_block *block;
Monk Liua90ad3c2017-01-23 14:22:08 +08001862
Monk Liu2cb681b2017-04-26 12:00:49 +08001863 for (j = 0; j < adev->num_ip_blocks; j++) {
1864 block = &adev->ip_blocks[j];
1865
1866 if (block->version->type != ip_order[i] ||
1867 !block->status.valid)
1868 continue;
1869
1870 r = block->version->funcs->hw_init(adev);
1871 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
Monk Liua90ad3c2017-01-23 14:22:08 +08001872 }
1873 }
1874
1875 return 0;
1876}
1877
Monk Liue4f0fdc2017-02-09 11:55:49 +08001878static int amdgpu_sriov_reinit_late(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08001879{
1880 int i, r;
1881
Monk Liu2cb681b2017-04-26 12:00:49 +08001882 static enum amd_ip_block_type ip_order[] = {
1883 AMD_IP_BLOCK_TYPE_SMC,
1884 AMD_IP_BLOCK_TYPE_DCE,
1885 AMD_IP_BLOCK_TYPE_GFX,
1886 AMD_IP_BLOCK_TYPE_SDMA,
Frank Min257deb82017-06-15 20:07:36 +08001887 AMD_IP_BLOCK_TYPE_UVD,
1888 AMD_IP_BLOCK_TYPE_VCE
Monk Liu2cb681b2017-04-26 12:00:49 +08001889 };
Monk Liua90ad3c2017-01-23 14:22:08 +08001890
Monk Liu2cb681b2017-04-26 12:00:49 +08001891 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1892 int j;
1893 struct amdgpu_ip_block *block;
Monk Liua90ad3c2017-01-23 14:22:08 +08001894
Monk Liu2cb681b2017-04-26 12:00:49 +08001895 for (j = 0; j < adev->num_ip_blocks; j++) {
1896 block = &adev->ip_blocks[j];
1897
1898 if (block->version->type != ip_order[i] ||
1899 !block->status.valid)
1900 continue;
1901
1902 r = block->version->funcs->hw_init(adev);
1903 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
Monk Liua90ad3c2017-01-23 14:22:08 +08001904 }
1905 }
1906
1907 return 0;
1908}
1909
Chunming Zhoufcf06492017-05-05 10:33:33 +08001910static int amdgpu_resume_phase1(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001911{
1912 int i, r;
1913
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001914 for (i = 0; i < adev->num_ip_blocks; i++) {
1915 if (!adev->ip_blocks[i].status.valid)
1916 continue;
Chunming Zhoufcf06492017-05-05 10:33:33 +08001917 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
1918 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
1919 adev->ip_blocks[i].version->type ==
1920 AMD_IP_BLOCK_TYPE_IH) {
1921 r = adev->ip_blocks[i].version->funcs->resume(adev);
1922 if (r) {
1923 DRM_ERROR("resume of IP block <%s> failed %d\n",
1924 adev->ip_blocks[i].version->funcs->name, r);
1925 return r;
1926 }
1927 }
1928 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001929
Chunming Zhoufcf06492017-05-05 10:33:33 +08001930 return 0;
1931}
1932
1933static int amdgpu_resume_phase2(struct amdgpu_device *adev)
1934{
1935 int i, r;
1936
1937 for (i = 0; i < adev->num_ip_blocks; i++) {
1938 if (!adev->ip_blocks[i].status.valid)
1939 continue;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001940 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
1941 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
1942 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH )
1943 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001944 r = adev->ip_blocks[i].version->funcs->resume(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001945 if (r) {
1946 DRM_ERROR("resume of IP block <%s> failed %d\n",
1947 adev->ip_blocks[i].version->funcs->name, r);
1948 return r;
1949 }
1950 }
1951
1952 return 0;
1953}
1954
1955static int amdgpu_resume(struct amdgpu_device *adev)
1956{
Chunming Zhoufcf06492017-05-05 10:33:33 +08001957 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001958
Chunming Zhoufcf06492017-05-05 10:33:33 +08001959 r = amdgpu_resume_phase1(adev);
1960 if (r)
1961 return r;
1962 r = amdgpu_resume_phase2(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001963
Chunming Zhoufcf06492017-05-05 10:33:33 +08001964 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001965}
1966
Monk Liu4e99a442016-03-31 13:26:59 +08001967static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
Andres Rodriguez048765a2016-06-11 02:51:32 -04001968{
Alex Deuchera5bde2f2016-09-23 16:23:41 -04001969 if (adev->is_atom_fw) {
1970 if (amdgpu_atomfirmware_gpu_supports_virtualization(adev))
1971 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
1972 } else {
1973 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
1974 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
1975 }
Andres Rodriguez048765a2016-06-11 02:51:32 -04001976}
1977
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001978/**
1979 * amdgpu_device_init - initialize the driver
1980 *
1981 * @adev: amdgpu_device pointer
1982 * @pdev: drm dev pointer
1983 * @pdev: pci dev pointer
1984 * @flags: driver flags
1985 *
1986 * Initializes the driver info and hw (all asics).
1987 * Returns 0 for success or an error on failure.
1988 * Called at driver startup.
1989 */
1990int amdgpu_device_init(struct amdgpu_device *adev,
1991 struct drm_device *ddev,
1992 struct pci_dev *pdev,
1993 uint32_t flags)
1994{
1995 int r, i;
1996 bool runtime = false;
Marek Olšák95844d22016-08-17 23:49:27 +02001997 u32 max_MBps;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001998
1999 adev->shutdown = false;
2000 adev->dev = &pdev->dev;
2001 adev->ddev = ddev;
2002 adev->pdev = pdev;
2003 adev->flags = flags;
Jammy Zhou2f7d10b2015-07-22 11:29:01 +08002004 adev->asic_type = flags & AMD_ASIC_MASK;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002005 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
Christian König6f02a692017-07-07 11:56:59 +02002006 adev->mc.gart_size = 512 * 1024 * 1024;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002007 adev->accel_working = false;
2008 adev->num_rings = 0;
2009 adev->mman.buffer_funcs = NULL;
2010 adev->mman.buffer_funcs_ring = NULL;
2011 adev->vm_manager.vm_pte_funcs = NULL;
Christian König2d55e452016-02-08 17:37:38 +01002012 adev->vm_manager.vm_pte_num_rings = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002013 adev->gart.gart_funcs = NULL;
Chris Wilsonf54d1862016-10-25 13:00:45 +01002014 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002015
2016 adev->smc_rreg = &amdgpu_invalid_rreg;
2017 adev->smc_wreg = &amdgpu_invalid_wreg;
2018 adev->pcie_rreg = &amdgpu_invalid_rreg;
2019 adev->pcie_wreg = &amdgpu_invalid_wreg;
Huang Rui36b9a952016-08-31 13:23:25 +08002020 adev->pciep_rreg = &amdgpu_invalid_rreg;
2021 adev->pciep_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002022 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
2023 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
2024 adev->didt_rreg = &amdgpu_invalid_rreg;
2025 adev->didt_wreg = &amdgpu_invalid_wreg;
Rex Zhuccdbb202016-06-08 12:47:41 +08002026 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
2027 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002028 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
2029 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
2030
Rex Zhuccdbb202016-06-08 12:47:41 +08002031
Alex Deucher3e39ab92015-06-05 15:04:33 -04002032 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
2033 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
2034 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002035
2036 /* mutex initialization are all done here so we
2037 * can recall function without having locking issues */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002038 atomic_set(&adev->irq.ih.lock, 0);
Huang Rui0e5ca0d2017-03-03 18:37:23 -05002039 mutex_init(&adev->firmware.mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002040 mutex_init(&adev->pm.mutex);
2041 mutex_init(&adev->gfx.gpu_clock_mutex);
2042 mutex_init(&adev->srbm_mutex);
2043 mutex_init(&adev->grbm_idx_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002044 mutex_init(&adev->mn_lock);
2045 hash_init(adev->mn_hash);
2046
2047 amdgpu_check_arguments(adev);
2048
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002049 spin_lock_init(&adev->mmio_idx_lock);
2050 spin_lock_init(&adev->smc_idx_lock);
2051 spin_lock_init(&adev->pcie_idx_lock);
2052 spin_lock_init(&adev->uvd_ctx_idx_lock);
2053 spin_lock_init(&adev->didt_idx_lock);
Rex Zhuccdbb202016-06-08 12:47:41 +08002054 spin_lock_init(&adev->gc_cac_idx_lock);
Evan Quan16abb5d2017-07-04 09:21:50 +08002055 spin_lock_init(&adev->se_cac_idx_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002056 spin_lock_init(&adev->audio_endpt_idx_lock);
Marek Olšák95844d22016-08-17 23:49:27 +02002057 spin_lock_init(&adev->mm_stats.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002058
Chunming Zhou0c4e7fa2016-08-17 11:41:30 +08002059 INIT_LIST_HEAD(&adev->shadow_list);
2060 mutex_init(&adev->shadow_list_lock);
2061
Chunming Zhou5c1354b2016-08-30 16:13:10 +08002062 INIT_LIST_HEAD(&adev->gtt_list);
2063 spin_lock_init(&adev->gtt_list_lock);
2064
Andres Rodriguez795f2812017-03-06 16:27:55 -05002065 INIT_LIST_HEAD(&adev->ring_lru_list);
2066 spin_lock_init(&adev->ring_lru_list_lock);
2067
Shirish S2dc80b02017-05-25 10:05:25 +05302068 INIT_DELAYED_WORK(&adev->late_init_work, amdgpu_late_init_func_handler);
2069
Alex Xie0fa49552017-06-08 14:58:05 -04002070 /* Registers mapping */
2071 /* TODO: block userspace mapping of io register */
Ken Wangda69c1612016-01-21 19:08:55 +08002072 if (adev->asic_type >= CHIP_BONAIRE) {
2073 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
2074 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
2075 } else {
2076 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
2077 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
2078 }
Chunming Zhou5c1354b2016-08-30 16:13:10 +08002079
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002080 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
2081 if (adev->rmmio == NULL) {
2082 return -ENOMEM;
2083 }
2084 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
2085 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
2086
Christian König705e5192017-06-08 11:15:16 +02002087 /* doorbell bar mapping */
2088 amdgpu_doorbell_init(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002089
2090 /* io port mapping */
2091 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
2092 if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
2093 adev->rio_mem_size = pci_resource_len(adev->pdev, i);
2094 adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
2095 break;
2096 }
2097 }
2098 if (adev->rio_mem == NULL)
Amber Linb64a18c2017-01-04 08:06:58 -05002099 DRM_INFO("PCI I/O BAR is not found.\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002100
2101 /* early init functions */
2102 r = amdgpu_early_init(adev);
2103 if (r)
2104 return r;
2105
2106 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
2107 /* this will fail for cards that aren't VGA class devices, just
2108 * ignore it */
2109 vga_client_register(adev->pdev, adev, NULL, amdgpu_vga_set_decode);
2110
2111 if (amdgpu_runtime_pm == 1)
2112 runtime = true;
Alex Deuchere9bef452016-04-25 13:12:18 -04002113 if (amdgpu_device_is_px(ddev))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002114 runtime = true;
Lukas Wunner84c8b222017-03-10 21:23:45 +01002115 if (!pci_is_thunderbolt_attached(adev->pdev))
2116 vga_switcheroo_register_client(adev->pdev,
2117 &amdgpu_switcheroo_ops, runtime);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002118 if (runtime)
2119 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
2120
2121 /* Read BIOS */
Alex Deucher83ba1262016-06-03 18:21:41 -04002122 if (!amdgpu_get_bios(adev)) {
2123 r = -EINVAL;
2124 goto failed;
2125 }
Nils Wallméniusf7e9e9f2016-12-14 21:52:45 +01002126
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002127 r = amdgpu_atombios_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002128 if (r) {
2129 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
Gavin Wan89041942017-06-23 13:55:15 -04002130 amdgpu_vf_error_put(AMDGIM_ERROR_VF_ATOMBIOS_INIT_FAIL, 0, 0);
Alex Deucher83ba1262016-06-03 18:21:41 -04002131 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002132 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002133
Monk Liu4e99a442016-03-31 13:26:59 +08002134 /* detect if we are with an SRIOV vbios */
2135 amdgpu_device_detect_sriov_bios(adev);
Andres Rodriguez048765a2016-06-11 02:51:32 -04002136
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002137 /* Post card if necessary */
Monk Liubec86372016-09-14 19:38:08 +08002138 if (amdgpu_vpost_needed(adev)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002139 if (!adev->bios) {
Monk Liubec86372016-09-14 19:38:08 +08002140 dev_err(adev->dev, "no vBIOS found\n");
Gavin Wan89041942017-06-23 13:55:15 -04002141 amdgpu_vf_error_put(AMDGIM_ERROR_VF_NO_VBIOS, 0, 0);
Alex Deucher83ba1262016-06-03 18:21:41 -04002142 r = -EINVAL;
2143 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002144 }
Monk Liubec86372016-09-14 19:38:08 +08002145 DRM_INFO("GPU posting now...\n");
Monk Liu4e99a442016-03-31 13:26:59 +08002146 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2147 if (r) {
2148 dev_err(adev->dev, "gpu post error!\n");
Gavin Wan89041942017-06-23 13:55:15 -04002149 amdgpu_vf_error_put(AMDGIM_ERROR_VF_GPU_POST_ERROR, 0, 0);
Monk Liu4e99a442016-03-31 13:26:59 +08002150 goto failed;
2151 }
2152 } else {
2153 DRM_INFO("GPU post is not needed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002154 }
2155
Alex Deucher88b64e92017-07-10 10:43:10 -04002156 if (adev->is_atom_fw) {
2157 /* Initialize clocks */
2158 r = amdgpu_atomfirmware_get_clock_info(adev);
2159 if (r) {
2160 dev_err(adev->dev, "amdgpu_atomfirmware_get_clock_info failed\n");
2161 amdgpu_vf_error_put(AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
2162 goto failed;
2163 }
2164 } else {
Alex Deuchera5bde2f2016-09-23 16:23:41 -04002165 /* Initialize clocks */
2166 r = amdgpu_atombios_get_clock_info(adev);
2167 if (r) {
2168 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
Gavin Wan89041942017-06-23 13:55:15 -04002169 amdgpu_vf_error_put(AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
2170 goto failed;
Alex Deuchera5bde2f2016-09-23 16:23:41 -04002171 }
2172 /* init i2c buses */
2173 amdgpu_atombios_i2c_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002174 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002175
2176 /* Fence driver */
2177 r = amdgpu_fence_driver_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002178 if (r) {
2179 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
Gavin Wan89041942017-06-23 13:55:15 -04002180 amdgpu_vf_error_put(AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0);
Alex Deucher83ba1262016-06-03 18:21:41 -04002181 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002182 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002183
2184 /* init the mode config */
2185 drm_mode_config_init(adev->ddev);
2186
2187 r = amdgpu_init(adev);
2188 if (r) {
Alex Deucher2c1a2782015-12-07 17:02:53 -05002189 dev_err(adev->dev, "amdgpu_init failed\n");
Gavin Wan89041942017-06-23 13:55:15 -04002190 amdgpu_vf_error_put(AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002191 amdgpu_fini(adev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002192 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002193 }
2194
2195 adev->accel_working = true;
2196
Alex Xiee59c0202017-06-01 09:42:59 -04002197 amdgpu_vm_check_compute_bug(adev);
2198
Marek Olšák95844d22016-08-17 23:49:27 +02002199 /* Initialize the buffer migration limit. */
2200 if (amdgpu_moverate >= 0)
2201 max_MBps = amdgpu_moverate;
2202 else
2203 max_MBps = 8; /* Allow 8 MB/s. */
2204 /* Get a log2 for easy divisions. */
2205 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
2206
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002207 r = amdgpu_ib_pool_init(adev);
2208 if (r) {
2209 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
Gavin Wan89041942017-06-23 13:55:15 -04002210 amdgpu_vf_error_put(AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r);
Alex Deucher83ba1262016-06-03 18:21:41 -04002211 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002212 }
2213
2214 r = amdgpu_ib_ring_tests(adev);
2215 if (r)
2216 DRM_ERROR("ib ring test failed (%d).\n", r);
2217
Monk Liu9bc92b92017-02-08 17:38:13 +08002218 amdgpu_fbdev_init(adev);
2219
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002220 r = amdgpu_gem_debugfs_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002221 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002222 DRM_ERROR("registering gem debugfs failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002223
2224 r = amdgpu_debugfs_regs_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002225 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002226 DRM_ERROR("registering register debugfs failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002227
Huang Rui4f0955f2017-05-10 23:04:06 +08002228 r = amdgpu_debugfs_test_ib_ring_init(adev);
2229 if (r)
2230 DRM_ERROR("registering register test ib ring debugfs failed (%d).\n", r);
2231
Huang Rui50ab2532016-06-12 15:51:09 +08002232 r = amdgpu_debugfs_firmware_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002233 if (r)
Huang Rui50ab2532016-06-12 15:51:09 +08002234 DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
Huang Rui50ab2532016-06-12 15:51:09 +08002235
Kent Russelldb95e212017-08-22 12:31:43 -04002236 r = amdgpu_debugfs_vbios_dump_init(adev);
2237 if (r)
2238 DRM_ERROR("Creating vbios dump debugfs failed (%d).\n", r);
2239
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002240 if ((amdgpu_testing & 1)) {
2241 if (adev->accel_working)
2242 amdgpu_test_moves(adev);
2243 else
2244 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
2245 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002246 if (amdgpu_benchmarking) {
2247 if (adev->accel_working)
2248 amdgpu_benchmark(adev, amdgpu_benchmarking);
2249 else
2250 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
2251 }
2252
2253 /* enable clockgating, etc. after ib tests, etc. since some blocks require
2254 * explicit gating rather than handling it automatically.
2255 */
2256 r = amdgpu_late_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002257 if (r) {
2258 dev_err(adev->dev, "amdgpu_late_init failed\n");
Gavin Wan89041942017-06-23 13:55:15 -04002259 amdgpu_vf_error_put(AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r);
Alex Deucher83ba1262016-06-03 18:21:41 -04002260 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002261 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002262
2263 return 0;
Alex Deucher83ba1262016-06-03 18:21:41 -04002264
2265failed:
Gavin Wan89041942017-06-23 13:55:15 -04002266 amdgpu_vf_error_trans_all(adev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002267 if (runtime)
2268 vga_switcheroo_fini_domain_pm_ops(adev->dev);
2269 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002270}
2271
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002272/**
2273 * amdgpu_device_fini - tear down the driver
2274 *
2275 * @adev: amdgpu_device pointer
2276 *
2277 * Tear down the driver info (all asics).
2278 * Called at driver shutdown.
2279 */
2280void amdgpu_device_fini(struct amdgpu_device *adev)
2281{
2282 int r;
2283
2284 DRM_INFO("amdgpu: finishing device.\n");
2285 adev->shutdown = true;
Pixel Dingdb2c2a92017-04-25 16:47:42 +08002286 if (adev->mode_info.mode_config_initialized)
2287 drm_crtc_force_disable_all(adev->ddev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002288 /* evict vram memory */
2289 amdgpu_bo_evict_vram(adev);
2290 amdgpu_ib_pool_fini(adev);
2291 amdgpu_fence_driver_fini(adev);
2292 amdgpu_fbdev_fini(adev);
2293 r = amdgpu_fini(adev);
Huang Ruiab4fe3e2017-06-05 22:11:59 +08002294 if (adev->firmware.gpu_info_fw) {
2295 release_firmware(adev->firmware.gpu_info_fw);
2296 adev->firmware.gpu_info_fw = NULL;
2297 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002298 adev->accel_working = false;
Shirish S2dc80b02017-05-25 10:05:25 +05302299 cancel_delayed_work_sync(&adev->late_init_work);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002300 /* free i2c buses */
2301 amdgpu_i2c_fini(adev);
2302 amdgpu_atombios_fini(adev);
2303 kfree(adev->bios);
2304 adev->bios = NULL;
Lukas Wunner84c8b222017-03-10 21:23:45 +01002305 if (!pci_is_thunderbolt_attached(adev->pdev))
2306 vga_switcheroo_unregister_client(adev->pdev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002307 if (adev->flags & AMD_IS_PX)
2308 vga_switcheroo_fini_domain_pm_ops(adev->dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002309 vga_client_register(adev->pdev, NULL, NULL, NULL);
2310 if (adev->rio_mem)
2311 pci_iounmap(adev->pdev, adev->rio_mem);
2312 adev->rio_mem = NULL;
2313 iounmap(adev->rmmio);
2314 adev->rmmio = NULL;
Christian König705e5192017-06-08 11:15:16 +02002315 amdgpu_doorbell_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002316 amdgpu_debugfs_regs_cleanup(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002317}
2318
2319
2320/*
2321 * Suspend & resume.
2322 */
2323/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002324 * amdgpu_device_suspend - initiate device suspend
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002325 *
2326 * @pdev: drm dev pointer
2327 * @state: suspend state
2328 *
2329 * Puts the hw in the suspend state (all asics).
2330 * Returns 0 for success or an error on failure.
2331 * Called at driver suspend.
2332 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002333int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002334{
2335 struct amdgpu_device *adev;
2336 struct drm_crtc *crtc;
2337 struct drm_connector *connector;
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002338 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002339
2340 if (dev == NULL || dev->dev_private == NULL) {
2341 return -ENODEV;
2342 }
2343
2344 adev = dev->dev_private;
2345
2346 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2347 return 0;
2348
2349 drm_kms_helper_poll_disable(dev);
2350
2351 /* turn off display hw */
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002352 drm_modeset_lock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002353 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2354 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
2355 }
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002356 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002357
Yong Zhaoba997702015-11-09 17:21:45 -05002358 amdgpu_amdkfd_suspend(adev);
2359
Alex Deucher756e6882015-10-08 00:03:36 -04002360 /* unpin the front buffers and cursors */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002361 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Alex Deucher756e6882015-10-08 00:03:36 -04002362 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002363 struct amdgpu_framebuffer *rfb = to_amdgpu_framebuffer(crtc->primary->fb);
2364 struct amdgpu_bo *robj;
2365
Alex Deucher756e6882015-10-08 00:03:36 -04002366 if (amdgpu_crtc->cursor_bo) {
2367 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
Alex Xie7a6901d2017-04-24 13:52:41 -04002368 r = amdgpu_bo_reserve(aobj, true);
Alex Deucher756e6882015-10-08 00:03:36 -04002369 if (r == 0) {
2370 amdgpu_bo_unpin(aobj);
2371 amdgpu_bo_unreserve(aobj);
2372 }
2373 }
2374
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002375 if (rfb == NULL || rfb->obj == NULL) {
2376 continue;
2377 }
2378 robj = gem_to_amdgpu_bo(rfb->obj);
2379 /* don't unpin kernel fb objects */
2380 if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
Alex Xie7a6901d2017-04-24 13:52:41 -04002381 r = amdgpu_bo_reserve(robj, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002382 if (r == 0) {
2383 amdgpu_bo_unpin(robj);
2384 amdgpu_bo_unreserve(robj);
2385 }
2386 }
2387 }
2388 /* evict vram memory */
2389 amdgpu_bo_evict_vram(adev);
2390
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002391 amdgpu_fence_driver_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002392
2393 r = amdgpu_suspend(adev);
2394
Alex Deuchera0a71e42016-10-10 12:41:36 -04002395 /* evict remaining vram memory
2396 * This second call to evict vram is to evict the gart page table
2397 * using the CPU.
2398 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002399 amdgpu_bo_evict_vram(adev);
2400
Alex Deucherd05da0e2017-06-30 17:08:45 -04002401 amdgpu_atombios_scratch_regs_save(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002402 pci_save_state(dev->pdev);
2403 if (suspend) {
2404 /* Shut down the device */
2405 pci_disable_device(dev->pdev);
2406 pci_set_power_state(dev->pdev, PCI_D3hot);
jimqu74b0b152016-09-07 17:09:12 +08002407 } else {
2408 r = amdgpu_asic_reset(adev);
2409 if (r)
2410 DRM_ERROR("amdgpu asic reset failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002411 }
2412
2413 if (fbcon) {
2414 console_lock();
2415 amdgpu_fbdev_set_suspend(adev, 1);
2416 console_unlock();
2417 }
2418 return 0;
2419}
2420
2421/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002422 * amdgpu_device_resume - initiate device resume
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002423 *
2424 * @pdev: drm dev pointer
2425 *
2426 * Bring the hw back to operating state (all asics).
2427 * Returns 0 for success or an error on failure.
2428 * Called at driver resume.
2429 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002430int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002431{
2432 struct drm_connector *connector;
2433 struct amdgpu_device *adev = dev->dev_private;
Alex Deucher756e6882015-10-08 00:03:36 -04002434 struct drm_crtc *crtc;
Huang Rui03161a62017-04-13 16:12:26 +08002435 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002436
2437 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2438 return 0;
2439
jimqu74b0b152016-09-07 17:09:12 +08002440 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002441 console_lock();
jimqu74b0b152016-09-07 17:09:12 +08002442
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002443 if (resume) {
2444 pci_set_power_state(dev->pdev, PCI_D0);
2445 pci_restore_state(dev->pdev);
jimqu74b0b152016-09-07 17:09:12 +08002446 r = pci_enable_device(dev->pdev);
Huang Rui03161a62017-04-13 16:12:26 +08002447 if (r)
2448 goto unlock;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002449 }
Alex Deucherd05da0e2017-06-30 17:08:45 -04002450 amdgpu_atombios_scratch_regs_restore(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002451
2452 /* post card */
Jim Quc836fec2017-02-10 15:59:59 +08002453 if (amdgpu_need_post(adev)) {
jimqu74b0b152016-09-07 17:09:12 +08002454 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2455 if (r)
2456 DRM_ERROR("amdgpu asic init failed\n");
2457 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002458
2459 r = amdgpu_resume(adev);
Rex Zhue6707212017-03-30 13:21:01 +08002460 if (r) {
Flora Cuica198522016-02-04 15:10:08 +08002461 DRM_ERROR("amdgpu_resume failed (%d).\n", r);
Huang Rui03161a62017-04-13 16:12:26 +08002462 goto unlock;
Rex Zhue6707212017-03-30 13:21:01 +08002463 }
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002464 amdgpu_fence_driver_resume(adev);
2465
Flora Cuica198522016-02-04 15:10:08 +08002466 if (resume) {
2467 r = amdgpu_ib_ring_tests(adev);
2468 if (r)
2469 DRM_ERROR("ib ring test failed (%d).\n", r);
2470 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002471
2472 r = amdgpu_late_init(adev);
Huang Rui03161a62017-04-13 16:12:26 +08002473 if (r)
2474 goto unlock;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002475
Alex Deucher756e6882015-10-08 00:03:36 -04002476 /* pin cursors */
2477 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2478 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2479
2480 if (amdgpu_crtc->cursor_bo) {
2481 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
Alex Xie7a6901d2017-04-24 13:52:41 -04002482 r = amdgpu_bo_reserve(aobj, true);
Alex Deucher756e6882015-10-08 00:03:36 -04002483 if (r == 0) {
2484 r = amdgpu_bo_pin(aobj,
2485 AMDGPU_GEM_DOMAIN_VRAM,
2486 &amdgpu_crtc->cursor_addr);
2487 if (r != 0)
2488 DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
2489 amdgpu_bo_unreserve(aobj);
2490 }
2491 }
2492 }
Yong Zhaoba997702015-11-09 17:21:45 -05002493 r = amdgpu_amdkfd_resume(adev);
2494 if (r)
2495 return r;
Alex Deucher756e6882015-10-08 00:03:36 -04002496
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002497 /* blat the mode back in */
2498 if (fbcon) {
2499 drm_helper_resume_force_mode(dev);
2500 /* turn on display hw */
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002501 drm_modeset_lock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002502 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2503 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
2504 }
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002505 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002506 }
2507
2508 drm_kms_helper_poll_enable(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002509
2510 /*
2511 * Most of the connector probing functions try to acquire runtime pm
2512 * refs to ensure that the GPU is powered on when connector polling is
2513 * performed. Since we're calling this from a runtime PM callback,
2514 * trying to acquire rpm refs will cause us to deadlock.
2515 *
2516 * Since we're guaranteed to be holding the rpm lock, it's safe to
2517 * temporarily disable the rpm helpers so this doesn't deadlock us.
2518 */
2519#ifdef CONFIG_PM
2520 dev->dev->power.disable_depth++;
2521#endif
Alex Deucher54fb2a52015-11-24 14:30:56 -05002522 drm_helper_hpd_irq_event(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002523#ifdef CONFIG_PM
2524 dev->dev->power.disable_depth--;
2525#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002526
Huang Rui03161a62017-04-13 16:12:26 +08002527 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002528 amdgpu_fbdev_set_suspend(adev, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002529
Huang Rui03161a62017-04-13 16:12:26 +08002530unlock:
2531 if (fbcon)
2532 console_unlock();
2533
2534 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002535}
2536
Chunming Zhou63fbf422016-07-15 11:19:20 +08002537static bool amdgpu_check_soft_reset(struct amdgpu_device *adev)
2538{
2539 int i;
2540 bool asic_hang = false;
2541
2542 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002543 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou63fbf422016-07-15 11:19:20 +08002544 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002545 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
2546 adev->ip_blocks[i].status.hang =
2547 adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
2548 if (adev->ip_blocks[i].status.hang) {
2549 DRM_INFO("IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
Chunming Zhou63fbf422016-07-15 11:19:20 +08002550 asic_hang = true;
2551 }
2552 }
2553 return asic_hang;
2554}
2555
Baoyou Xie4d446652016-09-18 22:09:35 +08002556static int amdgpu_pre_soft_reset(struct amdgpu_device *adev)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002557{
2558 int i, r = 0;
2559
2560 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002561 if (!adev->ip_blocks[i].status.valid)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002562 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002563 if (adev->ip_blocks[i].status.hang &&
2564 adev->ip_blocks[i].version->funcs->pre_soft_reset) {
2565 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
Chunming Zhoud31a5012016-07-18 10:04:34 +08002566 if (r)
2567 return r;
2568 }
2569 }
2570
2571 return 0;
2572}
2573
Chunming Zhou35d782f2016-07-15 15:57:13 +08002574static bool amdgpu_need_full_reset(struct amdgpu_device *adev)
2575{
Alex Deucherda146d32016-10-13 16:07:03 -04002576 int i;
2577
2578 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002579 if (!adev->ip_blocks[i].status.valid)
Alex Deucherda146d32016-10-13 16:07:03 -04002580 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002581 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
2582 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
2583 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
2584 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE)) {
2585 if (adev->ip_blocks[i].status.hang) {
Alex Deucherda146d32016-10-13 16:07:03 -04002586 DRM_INFO("Some block need full reset!\n");
2587 return true;
2588 }
2589 }
Chunming Zhou35d782f2016-07-15 15:57:13 +08002590 }
2591 return false;
2592}
2593
2594static int amdgpu_soft_reset(struct amdgpu_device *adev)
2595{
2596 int i, r = 0;
2597
2598 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002599 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002600 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002601 if (adev->ip_blocks[i].status.hang &&
2602 adev->ip_blocks[i].version->funcs->soft_reset) {
2603 r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002604 if (r)
2605 return r;
2606 }
2607 }
2608
2609 return 0;
2610}
2611
2612static int amdgpu_post_soft_reset(struct amdgpu_device *adev)
2613{
2614 int i, r = 0;
2615
2616 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002617 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002618 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002619 if (adev->ip_blocks[i].status.hang &&
2620 adev->ip_blocks[i].version->funcs->post_soft_reset)
2621 r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002622 if (r)
2623 return r;
2624 }
2625
2626 return 0;
2627}
2628
Chunming Zhou3ad81f12016-08-05 17:30:17 +08002629bool amdgpu_need_backup(struct amdgpu_device *adev)
2630{
2631 if (adev->flags & AMD_IS_APU)
2632 return false;
2633
2634 return amdgpu_lockup_timeout > 0 ? true : false;
2635}
2636
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002637static int amdgpu_recover_vram_from_shadow(struct amdgpu_device *adev,
2638 struct amdgpu_ring *ring,
2639 struct amdgpu_bo *bo,
Chris Wilsonf54d1862016-10-25 13:00:45 +01002640 struct dma_fence **fence)
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002641{
2642 uint32_t domain;
2643 int r;
2644
Roger.He23d2e502017-04-21 14:24:26 +08002645 if (!bo->shadow)
2646 return 0;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002647
Alex Xie1d284792017-04-24 13:53:04 -04002648 r = amdgpu_bo_reserve(bo, true);
Roger.He23d2e502017-04-21 14:24:26 +08002649 if (r)
2650 return r;
2651 domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
2652 /* if bo has been evicted, then no need to recover */
2653 if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
Roger.He82521312017-04-21 13:08:43 +08002654 r = amdgpu_bo_validate(bo->shadow);
2655 if (r) {
2656 DRM_ERROR("bo validate failed!\n");
2657 goto err;
2658 }
2659
Roger.He23d2e502017-04-21 14:24:26 +08002660 r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002661 NULL, fence, true);
Roger.He23d2e502017-04-21 14:24:26 +08002662 if (r) {
2663 DRM_ERROR("recover page table failed!\n");
2664 goto err;
2665 }
2666 }
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002667err:
Roger.He23d2e502017-04-21 14:24:26 +08002668 amdgpu_bo_unreserve(bo);
2669 return r;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002670}
2671
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002672/**
Monk Liua90ad3c2017-01-23 14:22:08 +08002673 * amdgpu_sriov_gpu_reset - reset the asic
2674 *
2675 * @adev: amdgpu device pointer
Monk Liu7225f872017-04-26 14:51:54 +08002676 * @job: which job trigger hang
Monk Liua90ad3c2017-01-23 14:22:08 +08002677 *
2678 * Attempt the reset the GPU if it has hung (all asics).
2679 * for SRIOV case.
2680 * Returns 0 for success or an error on failure.
2681 */
Monk Liu7225f872017-04-26 14:51:54 +08002682int amdgpu_sriov_gpu_reset(struct amdgpu_device *adev, struct amdgpu_job *job)
Monk Liua90ad3c2017-01-23 14:22:08 +08002683{
Monk Liu65781c72017-05-11 13:36:44 +08002684 int i, j, r = 0;
Monk Liua90ad3c2017-01-23 14:22:08 +08002685 int resched;
2686 struct amdgpu_bo *bo, *tmp;
2687 struct amdgpu_ring *ring;
2688 struct dma_fence *fence = NULL, *next = NULL;
2689
Monk Liu147b5982017-01-25 15:48:01 +08002690 mutex_lock(&adev->virt.lock_reset);
Monk Liua90ad3c2017-01-23 14:22:08 +08002691 atomic_inc(&adev->gpu_reset_counter);
Monk Liu1fb37a32017-01-26 15:36:37 +08002692 adev->gfx.in_reset = true;
Monk Liua90ad3c2017-01-23 14:22:08 +08002693
2694 /* block TTM */
2695 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
2696
Monk Liu65781c72017-05-11 13:36:44 +08002697 /* we start from the ring trigger GPU hang */
2698 j = job ? job->ring->idx : 0;
Monk Liua90ad3c2017-01-23 14:22:08 +08002699
Monk Liu65781c72017-05-11 13:36:44 +08002700 /* block scheduler */
2701 for (i = j; i < j + AMDGPU_MAX_RINGS; ++i) {
2702 ring = adev->rings[i % AMDGPU_MAX_RINGS];
Monk Liua90ad3c2017-01-23 14:22:08 +08002703 if (!ring || !ring->sched.thread)
2704 continue;
2705
2706 kthread_park(ring->sched.thread);
Monk Liua90ad3c2017-01-23 14:22:08 +08002707
Monk Liu65781c72017-05-11 13:36:44 +08002708 if (job && j != i)
2709 continue;
2710
Monk Liu4f059ec2017-05-11 13:59:15 +08002711 /* here give the last chance to check if job removed from mirror-list
Monk Liu65781c72017-05-11 13:36:44 +08002712 * since we already pay some time on kthread_park */
Monk Liu4f059ec2017-05-11 13:59:15 +08002713 if (job && list_empty(&job->base.node)) {
Monk Liu65781c72017-05-11 13:36:44 +08002714 kthread_unpark(ring->sched.thread);
2715 goto give_up_reset;
2716 }
2717
2718 if (amd_sched_invalidate_job(&job->base, amdgpu_job_hang_limit))
2719 amd_sched_job_kickout(&job->base);
2720
2721 /* only do job_reset on the hang ring if @job not NULL */
Monk Liua90ad3c2017-01-23 14:22:08 +08002722 amd_sched_hw_job_reset(&ring->sched);
Monk Liu65781c72017-05-11 13:36:44 +08002723
2724 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
2725 amdgpu_fence_driver_force_completion_ring(ring);
Monk Liua90ad3c2017-01-23 14:22:08 +08002726 }
2727
Monk Liua90ad3c2017-01-23 14:22:08 +08002728 /* request to take full control of GPU before re-initialization */
Monk Liu7225f872017-04-26 14:51:54 +08002729 if (job)
Monk Liua90ad3c2017-01-23 14:22:08 +08002730 amdgpu_virt_reset_gpu(adev);
2731 else
2732 amdgpu_virt_request_full_gpu(adev, true);
2733
2734
2735 /* Resume IP prior to SMC */
Monk Liue4f0fdc2017-02-09 11:55:49 +08002736 amdgpu_sriov_reinit_early(adev);
Monk Liua90ad3c2017-01-23 14:22:08 +08002737
2738 /* we need recover gart prior to run SMC/CP/SDMA resume */
2739 amdgpu_ttm_recover_gart(adev);
2740
2741 /* now we are okay to resume SMC/CP/SDMA */
Monk Liue4f0fdc2017-02-09 11:55:49 +08002742 amdgpu_sriov_reinit_late(adev);
Monk Liua90ad3c2017-01-23 14:22:08 +08002743
2744 amdgpu_irq_gpu_reset_resume_helper(adev);
2745
2746 if (amdgpu_ib_ring_tests(adev))
2747 dev_err(adev->dev, "[GPU_RESET] ib ring test failed (%d).\n", r);
2748
2749 /* release full control of GPU after ib test */
2750 amdgpu_virt_release_full_gpu(adev, true);
2751
2752 DRM_INFO("recover vram bo from shadow\n");
2753
2754 ring = adev->mman.buffer_funcs_ring;
2755 mutex_lock(&adev->shadow_list_lock);
2756 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
Monk Liu236763d2017-05-01 16:15:31 +08002757 next = NULL;
Monk Liua90ad3c2017-01-23 14:22:08 +08002758 amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
2759 if (fence) {
2760 r = dma_fence_wait(fence, false);
2761 if (r) {
2762 WARN(r, "recovery from shadow isn't completed\n");
2763 break;
2764 }
2765 }
2766
2767 dma_fence_put(fence);
2768 fence = next;
2769 }
2770 mutex_unlock(&adev->shadow_list_lock);
2771
2772 if (fence) {
2773 r = dma_fence_wait(fence, false);
2774 if (r)
2775 WARN(r, "recovery from shadow isn't completed\n");
2776 }
2777 dma_fence_put(fence);
2778
Monk Liu65781c72017-05-11 13:36:44 +08002779 for (i = j; i < j + AMDGPU_MAX_RINGS; ++i) {
2780 ring = adev->rings[i % AMDGPU_MAX_RINGS];
Monk Liua90ad3c2017-01-23 14:22:08 +08002781 if (!ring || !ring->sched.thread)
2782 continue;
2783
Monk Liu65781c72017-05-11 13:36:44 +08002784 if (job && j != i) {
2785 kthread_unpark(ring->sched.thread);
2786 continue;
2787 }
2788
Monk Liua90ad3c2017-01-23 14:22:08 +08002789 amd_sched_job_recovery(&ring->sched);
2790 kthread_unpark(ring->sched.thread);
2791 }
2792
2793 drm_helper_resume_force_mode(adev->ddev);
Monk Liu65781c72017-05-11 13:36:44 +08002794give_up_reset:
Monk Liua90ad3c2017-01-23 14:22:08 +08002795 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
2796 if (r) {
2797 /* bad news, how to tell it to userspace ? */
2798 dev_info(adev->dev, "GPU reset failed\n");
Monk Liu65781c72017-05-11 13:36:44 +08002799 } else {
2800 dev_info(adev->dev, "GPU reset successed!\n");
Monk Liua90ad3c2017-01-23 14:22:08 +08002801 }
2802
Monk Liu1fb37a32017-01-26 15:36:37 +08002803 adev->gfx.in_reset = false;
Monk Liu147b5982017-01-25 15:48:01 +08002804 mutex_unlock(&adev->virt.lock_reset);
Monk Liua90ad3c2017-01-23 14:22:08 +08002805 return r;
2806}
2807
2808/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002809 * amdgpu_gpu_reset - reset the asic
2810 *
2811 * @adev: amdgpu device pointer
2812 *
2813 * Attempt the reset the GPU if it has hung (all asics).
2814 * Returns 0 for success or an error on failure.
2815 */
2816int amdgpu_gpu_reset(struct amdgpu_device *adev)
2817{
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002818 int i, r;
2819 int resched;
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002820 bool need_full_reset, vram_lost = false;
Xiangliang Yufb140b22016-12-17 22:48:57 +08002821
Chunming Zhou63fbf422016-07-15 11:19:20 +08002822 if (!amdgpu_check_soft_reset(adev)) {
2823 DRM_INFO("No hardware hang detected. Did some blocks stall?\n");
2824 return 0;
2825 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002826
Marek Olšákd94aed52015-05-05 21:13:49 +02002827 atomic_inc(&adev->gpu_reset_counter);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002828
Chunming Zhoua3c47d62016-06-30 16:44:41 +08002829 /* block TTM */
2830 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
2831
Chunming Zhou0875dc92016-06-12 15:41:58 +08002832 /* block scheduler */
2833 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2834 struct amdgpu_ring *ring = adev->rings[i];
2835
Chunming Zhou51687752017-04-24 17:09:15 +08002836 if (!ring || !ring->sched.thread)
Chunming Zhou0875dc92016-06-12 15:41:58 +08002837 continue;
2838 kthread_park(ring->sched.thread);
Chunming Zhouaa1c8902016-06-30 13:56:02 +08002839 amd_sched_hw_job_reset(&ring->sched);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002840 }
Chunming Zhou2200eda2016-06-30 16:53:02 +08002841 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
2842 amdgpu_fence_driver_force_completion(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002843
Chunming Zhou35d782f2016-07-15 15:57:13 +08002844 need_full_reset = amdgpu_need_full_reset(adev);
2845
2846 if (!need_full_reset) {
2847 amdgpu_pre_soft_reset(adev);
2848 r = amdgpu_soft_reset(adev);
2849 amdgpu_post_soft_reset(adev);
2850 if (r || amdgpu_check_soft_reset(adev)) {
2851 DRM_INFO("soft reset failed, will fallback to full reset!\n");
2852 need_full_reset = true;
2853 }
2854 }
2855
2856 if (need_full_reset) {
Chunming Zhou35d782f2016-07-15 15:57:13 +08002857 r = amdgpu_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002858
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002859retry:
Alex Deucherd05da0e2017-06-30 17:08:45 -04002860 amdgpu_atombios_scratch_regs_save(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002861 r = amdgpu_asic_reset(adev);
Alex Deucherd05da0e2017-06-30 17:08:45 -04002862 amdgpu_atombios_scratch_regs_restore(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002863 /* post card */
2864 amdgpu_atom_asic_init(adev->mode_info.atom_context);
Alex Deucherbfa99262016-01-15 11:59:48 -05002865
Chunming Zhou35d782f2016-07-15 15:57:13 +08002866 if (!r) {
2867 dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
Chunming Zhoufcf06492017-05-05 10:33:33 +08002868 r = amdgpu_resume_phase1(adev);
2869 if (r)
2870 goto out;
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002871 vram_lost = amdgpu_check_vram_lost(adev);
Chunming Zhouf1892132017-05-15 16:48:27 +08002872 if (vram_lost) {
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002873 DRM_ERROR("VRAM is lost!\n");
Chunming Zhouf1892132017-05-15 16:48:27 +08002874 atomic_inc(&adev->vram_lost_counter);
2875 }
Chunming Zhou2c0d7312016-08-30 16:36:25 +08002876 r = amdgpu_ttm_recover_gart(adev);
2877 if (r)
Chunming Zhoufcf06492017-05-05 10:33:33 +08002878 goto out;
2879 r = amdgpu_resume_phase2(adev);
2880 if (r)
2881 goto out;
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002882 if (vram_lost)
2883 amdgpu_fill_reset_magic(adev);
Chunming Zhou2c0d7312016-08-30 16:36:25 +08002884 }
Chunming Zhoufcf06492017-05-05 10:33:33 +08002885 }
2886out:
2887 if (!r) {
2888 amdgpu_irq_gpu_reset_resume_helper(adev);
Chunming Zhou1f465082016-06-30 15:02:26 +08002889 r = amdgpu_ib_ring_tests(adev);
2890 if (r) {
2891 dev_err(adev->dev, "ib ring test failed (%d).\n", r);
Chunming Zhou40019dc2016-06-29 16:01:49 +08002892 r = amdgpu_suspend(adev);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002893 need_full_reset = true;
Chunming Zhou40019dc2016-06-29 16:01:49 +08002894 goto retry;
Chunming Zhou1f465082016-06-30 15:02:26 +08002895 }
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002896 /**
2897 * recovery vm page tables, since we cannot depend on VRAM is
2898 * consistent after gpu full reset.
2899 */
2900 if (need_full_reset && amdgpu_need_backup(adev)) {
2901 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
2902 struct amdgpu_bo *bo, *tmp;
Chris Wilsonf54d1862016-10-25 13:00:45 +01002903 struct dma_fence *fence = NULL, *next = NULL;
Chunming Zhou1f465082016-06-30 15:02:26 +08002904
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002905 DRM_INFO("recover vram bo from shadow\n");
2906 mutex_lock(&adev->shadow_list_lock);
2907 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
Monk Liu236763d2017-05-01 16:15:31 +08002908 next = NULL;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002909 amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
2910 if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01002911 r = dma_fence_wait(fence, false);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002912 if (r) {
Monk Liu1d7b17b2017-01-22 18:52:56 +08002913 WARN(r, "recovery from shadow isn't completed\n");
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002914 break;
2915 }
2916 }
2917
Chris Wilsonf54d1862016-10-25 13:00:45 +01002918 dma_fence_put(fence);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002919 fence = next;
2920 }
2921 mutex_unlock(&adev->shadow_list_lock);
2922 if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01002923 r = dma_fence_wait(fence, false);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002924 if (r)
Monk Liu1d7b17b2017-01-22 18:52:56 +08002925 WARN(r, "recovery from shadow isn't completed\n");
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002926 }
Chris Wilsonf54d1862016-10-25 13:00:45 +01002927 dma_fence_put(fence);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002928 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002929 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2930 struct amdgpu_ring *ring = adev->rings[i];
Chunming Zhou51687752017-04-24 17:09:15 +08002931
2932 if (!ring || !ring->sched.thread)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002933 continue;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002934
Chunming Zhouaa1c8902016-06-30 13:56:02 +08002935 amd_sched_job_recovery(&ring->sched);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002936 kthread_unpark(ring->sched.thread);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002937 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002938 } else {
Chunming Zhou2200eda2016-06-30 16:53:02 +08002939 dev_err(adev->dev, "asic resume failed (%d).\n", r);
Gavin Wan89041942017-06-23 13:55:15 -04002940 amdgpu_vf_error_put(AMDGIM_ERROR_VF_ASIC_RESUME_FAIL, 0, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002941 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
Chunming Zhou51687752017-04-24 17:09:15 +08002942 if (adev->rings[i] && adev->rings[i]->sched.thread) {
Chunming Zhou0875dc92016-06-12 15:41:58 +08002943 kthread_unpark(adev->rings[i]->sched.thread);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002944 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002945 }
2946 }
2947
2948 drm_helper_resume_force_mode(adev->ddev);
2949
2950 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
Gavin Wan89041942017-06-23 13:55:15 -04002951 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002952 /* bad news, how to tell it to userspace ? */
2953 dev_info(adev->dev, "GPU reset failed\n");
Gavin Wan89041942017-06-23 13:55:15 -04002954 amdgpu_vf_error_put(AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, r);
2955 }
2956 else {
Chunming Zhou6643be62017-05-05 10:50:09 +08002957 dev_info(adev->dev, "GPU reset successed!\n");
Gavin Wan89041942017-06-23 13:55:15 -04002958 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002959
Gavin Wan89041942017-06-23 13:55:15 -04002960 amdgpu_vf_error_trans_all(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002961 return r;
2962}
2963
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002964void amdgpu_get_pcie_info(struct amdgpu_device *adev)
2965{
2966 u32 mask;
2967 int ret;
2968
Alex Deuchercd474ba2016-02-04 10:21:23 -05002969 if (amdgpu_pcie_gen_cap)
2970 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
2971
2972 if (amdgpu_pcie_lane_cap)
2973 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
2974
2975 /* covers APUs as well */
2976 if (pci_is_root_bus(adev->pdev->bus)) {
2977 if (adev->pm.pcie_gen_mask == 0)
2978 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
2979 if (adev->pm.pcie_mlw_mask == 0)
2980 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002981 return;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002982 }
Alex Deuchercd474ba2016-02-04 10:21:23 -05002983
2984 if (adev->pm.pcie_gen_mask == 0) {
2985 ret = drm_pcie_get_speed_cap_mask(adev->ddev, &mask);
2986 if (!ret) {
2987 adev->pm.pcie_gen_mask = (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
2988 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
2989 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
2990
2991 if (mask & DRM_PCIE_SPEED_25)
2992 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
2993 if (mask & DRM_PCIE_SPEED_50)
2994 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2;
2995 if (mask & DRM_PCIE_SPEED_80)
2996 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3;
2997 } else {
2998 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
2999 }
3000 }
3001 if (adev->pm.pcie_mlw_mask == 0) {
3002 ret = drm_pcie_get_max_link_width(adev->ddev, &mask);
3003 if (!ret) {
3004 switch (mask) {
3005 case 32:
3006 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
3007 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
3008 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3009 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3010 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3011 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3012 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3013 break;
3014 case 16:
3015 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
3016 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3017 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3018 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3019 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3020 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3021 break;
3022 case 12:
3023 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3024 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3025 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3026 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3027 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3028 break;
3029 case 8:
3030 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3031 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3032 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3033 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3034 break;
3035 case 4:
3036 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3037 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3038 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3039 break;
3040 case 2:
3041 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3042 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3043 break;
3044 case 1:
3045 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
3046 break;
3047 default:
3048 break;
3049 }
3050 } else {
3051 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003052 }
3053 }
3054}
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003055
3056/*
3057 * Debugfs
3058 */
3059int amdgpu_debugfs_add_files(struct amdgpu_device *adev,
Nils Wallménius06ab6832016-05-02 12:46:15 -04003060 const struct drm_info_list *files,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003061 unsigned nfiles)
3062{
3063 unsigned i;
3064
3065 for (i = 0; i < adev->debugfs_count; i++) {
3066 if (adev->debugfs[i].files == files) {
3067 /* Already registered */
3068 return 0;
3069 }
3070 }
3071
3072 i = adev->debugfs_count + 1;
3073 if (i > AMDGPU_DEBUGFS_MAX_COMPONENTS) {
3074 DRM_ERROR("Reached maximum number of debugfs components.\n");
3075 DRM_ERROR("Report so we increase "
3076 "AMDGPU_DEBUGFS_MAX_COMPONENTS.\n");
3077 return -EINVAL;
3078 }
3079 adev->debugfs[adev->debugfs_count].files = files;
3080 adev->debugfs[adev->debugfs_count].num_files = nfiles;
3081 adev->debugfs_count = i;
3082#if defined(CONFIG_DEBUG_FS)
3083 drm_debugfs_create_files(files, nfiles,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003084 adev->ddev->primary->debugfs_root,
3085 adev->ddev->primary);
3086#endif
3087 return 0;
3088}
3089
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003090#if defined(CONFIG_DEBUG_FS)
3091
3092static ssize_t amdgpu_debugfs_regs_read(struct file *f, char __user *buf,
3093 size_t size, loff_t *pos)
3094{
Al Viro45063092016-12-04 18:24:56 -05003095 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003096 ssize_t result = 0;
3097 int r;
Tom St Denisbd122672016-07-28 09:39:22 -04003098 bool pm_pg_lock, use_bank;
Tom St Denis566281592016-06-27 11:55:07 -04003099 unsigned instance_bank, sh_bank, se_bank;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003100
3101 if (size & 0x3 || *pos & 0x3)
3102 return -EINVAL;
3103
Tom St Denisbd122672016-07-28 09:39:22 -04003104 /* are we reading registers for which a PG lock is necessary? */
3105 pm_pg_lock = (*pos >> 23) & 1;
3106
Tom St Denis566281592016-06-27 11:55:07 -04003107 if (*pos & (1ULL << 62)) {
3108 se_bank = (*pos >> 24) & 0x3FF;
3109 sh_bank = (*pos >> 34) & 0x3FF;
3110 instance_bank = (*pos >> 44) & 0x3FF;
Tom St Denis32977f92016-10-09 07:41:26 -04003111
3112 if (se_bank == 0x3FF)
3113 se_bank = 0xFFFFFFFF;
3114 if (sh_bank == 0x3FF)
3115 sh_bank = 0xFFFFFFFF;
3116 if (instance_bank == 0x3FF)
3117 instance_bank = 0xFFFFFFFF;
Tom St Denis566281592016-06-27 11:55:07 -04003118 use_bank = 1;
Tom St Denis566281592016-06-27 11:55:07 -04003119 } else {
3120 use_bank = 0;
3121 }
3122
Tom St Denis801a6aa9a62017-03-15 05:34:25 -04003123 *pos &= (1UL << 22) - 1;
Tom St Denisbd122672016-07-28 09:39:22 -04003124
Tom St Denis566281592016-06-27 11:55:07 -04003125 if (use_bank) {
Tom St Denis32977f92016-10-09 07:41:26 -04003126 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
3127 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
Tom St Denis566281592016-06-27 11:55:07 -04003128 return -EINVAL;
3129 mutex_lock(&adev->grbm_idx_mutex);
3130 amdgpu_gfx_select_se_sh(adev, se_bank,
3131 sh_bank, instance_bank);
3132 }
3133
Tom St Denisbd122672016-07-28 09:39:22 -04003134 if (pm_pg_lock)
3135 mutex_lock(&adev->pm.mutex);
3136
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003137 while (size) {
3138 uint32_t value;
3139
3140 if (*pos > adev->rmmio_size)
Tom St Denis566281592016-06-27 11:55:07 -04003141 goto end;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003142
3143 value = RREG32(*pos >> 2);
3144 r = put_user(value, (uint32_t *)buf);
Tom St Denis566281592016-06-27 11:55:07 -04003145 if (r) {
3146 result = r;
3147 goto end;
3148 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003149
3150 result += 4;
3151 buf += 4;
3152 *pos += 4;
3153 size -= 4;
3154 }
3155
Tom St Denis566281592016-06-27 11:55:07 -04003156end:
3157 if (use_bank) {
3158 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
3159 mutex_unlock(&adev->grbm_idx_mutex);
3160 }
3161
Tom St Denisbd122672016-07-28 09:39:22 -04003162 if (pm_pg_lock)
3163 mutex_unlock(&adev->pm.mutex);
3164
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003165 return result;
3166}
3167
3168static ssize_t amdgpu_debugfs_regs_write(struct file *f, const char __user *buf,
3169 size_t size, loff_t *pos)
3170{
Al Viro45063092016-12-04 18:24:56 -05003171 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003172 ssize_t result = 0;
3173 int r;
Tom St Denis394fdde2016-10-10 07:31:23 -04003174 bool pm_pg_lock, use_bank;
3175 unsigned instance_bank, sh_bank, se_bank;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003176
3177 if (size & 0x3 || *pos & 0x3)
3178 return -EINVAL;
3179
Tom St Denis394fdde2016-10-10 07:31:23 -04003180 /* are we reading registers for which a PG lock is necessary? */
3181 pm_pg_lock = (*pos >> 23) & 1;
3182
3183 if (*pos & (1ULL << 62)) {
3184 se_bank = (*pos >> 24) & 0x3FF;
3185 sh_bank = (*pos >> 34) & 0x3FF;
3186 instance_bank = (*pos >> 44) & 0x3FF;
3187
3188 if (se_bank == 0x3FF)
3189 se_bank = 0xFFFFFFFF;
3190 if (sh_bank == 0x3FF)
3191 sh_bank = 0xFFFFFFFF;
3192 if (instance_bank == 0x3FF)
3193 instance_bank = 0xFFFFFFFF;
3194 use_bank = 1;
3195 } else {
3196 use_bank = 0;
3197 }
3198
Tom St Denis801a6aa9a62017-03-15 05:34:25 -04003199 *pos &= (1UL << 22) - 1;
Tom St Denis394fdde2016-10-10 07:31:23 -04003200
3201 if (use_bank) {
3202 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
3203 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
3204 return -EINVAL;
3205 mutex_lock(&adev->grbm_idx_mutex);
3206 amdgpu_gfx_select_se_sh(adev, se_bank,
3207 sh_bank, instance_bank);
3208 }
3209
3210 if (pm_pg_lock)
3211 mutex_lock(&adev->pm.mutex);
3212
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003213 while (size) {
3214 uint32_t value;
3215
3216 if (*pos > adev->rmmio_size)
3217 return result;
3218
3219 r = get_user(value, (uint32_t *)buf);
3220 if (r)
3221 return r;
3222
3223 WREG32(*pos >> 2, value);
3224
3225 result += 4;
3226 buf += 4;
3227 *pos += 4;
3228 size -= 4;
3229 }
3230
Tom St Denis394fdde2016-10-10 07:31:23 -04003231 if (use_bank) {
3232 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
3233 mutex_unlock(&adev->grbm_idx_mutex);
3234 }
3235
3236 if (pm_pg_lock)
3237 mutex_unlock(&adev->pm.mutex);
3238
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003239 return result;
3240}
3241
Tom St Denisadcec282016-04-15 13:08:44 -04003242static ssize_t amdgpu_debugfs_regs_pcie_read(struct file *f, char __user *buf,
3243 size_t size, loff_t *pos)
3244{
Al Viro45063092016-12-04 18:24:56 -05003245 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003246 ssize_t result = 0;
3247 int r;
3248
3249 if (size & 0x3 || *pos & 0x3)
3250 return -EINVAL;
3251
3252 while (size) {
3253 uint32_t value;
3254
3255 value = RREG32_PCIE(*pos >> 2);
3256 r = put_user(value, (uint32_t *)buf);
3257 if (r)
3258 return r;
3259
3260 result += 4;
3261 buf += 4;
3262 *pos += 4;
3263 size -= 4;
3264 }
3265
3266 return result;
3267}
3268
3269static ssize_t amdgpu_debugfs_regs_pcie_write(struct file *f, const char __user *buf,
3270 size_t size, loff_t *pos)
3271{
Al Viro45063092016-12-04 18:24:56 -05003272 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003273 ssize_t result = 0;
3274 int r;
3275
3276 if (size & 0x3 || *pos & 0x3)
3277 return -EINVAL;
3278
3279 while (size) {
3280 uint32_t value;
3281
3282 r = get_user(value, (uint32_t *)buf);
3283 if (r)
3284 return r;
3285
3286 WREG32_PCIE(*pos >> 2, value);
3287
3288 result += 4;
3289 buf += 4;
3290 *pos += 4;
3291 size -= 4;
3292 }
3293
3294 return result;
3295}
3296
3297static ssize_t amdgpu_debugfs_regs_didt_read(struct file *f, char __user *buf,
3298 size_t size, loff_t *pos)
3299{
Al Viro45063092016-12-04 18:24:56 -05003300 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003301 ssize_t result = 0;
3302 int r;
3303
3304 if (size & 0x3 || *pos & 0x3)
3305 return -EINVAL;
3306
3307 while (size) {
3308 uint32_t value;
3309
3310 value = RREG32_DIDT(*pos >> 2);
3311 r = put_user(value, (uint32_t *)buf);
3312 if (r)
3313 return r;
3314
3315 result += 4;
3316 buf += 4;
3317 *pos += 4;
3318 size -= 4;
3319 }
3320
3321 return result;
3322}
3323
3324static ssize_t amdgpu_debugfs_regs_didt_write(struct file *f, const char __user *buf,
3325 size_t size, loff_t *pos)
3326{
Al Viro45063092016-12-04 18:24:56 -05003327 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003328 ssize_t result = 0;
3329 int r;
3330
3331 if (size & 0x3 || *pos & 0x3)
3332 return -EINVAL;
3333
3334 while (size) {
3335 uint32_t value;
3336
3337 r = get_user(value, (uint32_t *)buf);
3338 if (r)
3339 return r;
3340
3341 WREG32_DIDT(*pos >> 2, value);
3342
3343 result += 4;
3344 buf += 4;
3345 *pos += 4;
3346 size -= 4;
3347 }
3348
3349 return result;
3350}
3351
3352static ssize_t amdgpu_debugfs_regs_smc_read(struct file *f, char __user *buf,
3353 size_t size, loff_t *pos)
3354{
Al Viro45063092016-12-04 18:24:56 -05003355 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003356 ssize_t result = 0;
3357 int r;
3358
3359 if (size & 0x3 || *pos & 0x3)
3360 return -EINVAL;
3361
3362 while (size) {
3363 uint32_t value;
3364
Tom St Denis6fc0dea2016-08-29 08:39:29 -04003365 value = RREG32_SMC(*pos);
Tom St Denisadcec282016-04-15 13:08:44 -04003366 r = put_user(value, (uint32_t *)buf);
3367 if (r)
3368 return r;
3369
3370 result += 4;
3371 buf += 4;
3372 *pos += 4;
3373 size -= 4;
3374 }
3375
3376 return result;
3377}
3378
3379static ssize_t amdgpu_debugfs_regs_smc_write(struct file *f, const char __user *buf,
3380 size_t size, loff_t *pos)
3381{
Al Viro45063092016-12-04 18:24:56 -05003382 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003383 ssize_t result = 0;
3384 int r;
3385
3386 if (size & 0x3 || *pos & 0x3)
3387 return -EINVAL;
3388
3389 while (size) {
3390 uint32_t value;
3391
3392 r = get_user(value, (uint32_t *)buf);
3393 if (r)
3394 return r;
3395
Tom St Denis6fc0dea2016-08-29 08:39:29 -04003396 WREG32_SMC(*pos, value);
Tom St Denisadcec282016-04-15 13:08:44 -04003397
3398 result += 4;
3399 buf += 4;
3400 *pos += 4;
3401 size -= 4;
3402 }
3403
3404 return result;
3405}
3406
Tom St Denis1e051412016-06-27 09:57:18 -04003407static ssize_t amdgpu_debugfs_gca_config_read(struct file *f, char __user *buf,
3408 size_t size, loff_t *pos)
3409{
Al Viro45063092016-12-04 18:24:56 -05003410 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denis1e051412016-06-27 09:57:18 -04003411 ssize_t result = 0;
3412 int r;
3413 uint32_t *config, no_regs = 0;
3414
3415 if (size & 0x3 || *pos & 0x3)
3416 return -EINVAL;
3417
Markus Elfringecab7662016-09-18 17:00:52 +02003418 config = kmalloc_array(256, sizeof(*config), GFP_KERNEL);
Tom St Denis1e051412016-06-27 09:57:18 -04003419 if (!config)
3420 return -ENOMEM;
3421
3422 /* version, increment each time something is added */
Tom St Denis9a999352017-01-18 13:01:25 -05003423 config[no_regs++] = 3;
Tom St Denis1e051412016-06-27 09:57:18 -04003424 config[no_regs++] = adev->gfx.config.max_shader_engines;
3425 config[no_regs++] = adev->gfx.config.max_tile_pipes;
3426 config[no_regs++] = adev->gfx.config.max_cu_per_sh;
3427 config[no_regs++] = adev->gfx.config.max_sh_per_se;
3428 config[no_regs++] = adev->gfx.config.max_backends_per_se;
3429 config[no_regs++] = adev->gfx.config.max_texture_channel_caches;
3430 config[no_regs++] = adev->gfx.config.max_gprs;
3431 config[no_regs++] = adev->gfx.config.max_gs_threads;
3432 config[no_regs++] = adev->gfx.config.max_hw_contexts;
3433 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_frontend;
3434 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_backend;
3435 config[no_regs++] = adev->gfx.config.sc_hiz_tile_fifo_size;
3436 config[no_regs++] = adev->gfx.config.sc_earlyz_tile_fifo_size;
3437 config[no_regs++] = adev->gfx.config.num_tile_pipes;
3438 config[no_regs++] = adev->gfx.config.backend_enable_mask;
3439 config[no_regs++] = adev->gfx.config.mem_max_burst_length_bytes;
3440 config[no_regs++] = adev->gfx.config.mem_row_size_in_kb;
3441 config[no_regs++] = adev->gfx.config.shader_engine_tile_size;
3442 config[no_regs++] = adev->gfx.config.num_gpus;
3443 config[no_regs++] = adev->gfx.config.multi_gpu_tile_size;
3444 config[no_regs++] = adev->gfx.config.mc_arb_ramcfg;
3445 config[no_regs++] = adev->gfx.config.gb_addr_config;
3446 config[no_regs++] = adev->gfx.config.num_rbs;
3447
Tom St Denis89a8f302016-08-12 15:14:31 -04003448 /* rev==1 */
3449 config[no_regs++] = adev->rev_id;
3450 config[no_regs++] = adev->pg_flags;
3451 config[no_regs++] = adev->cg_flags;
3452
Tom St Denise9f11dc2016-08-17 12:00:51 -04003453 /* rev==2 */
3454 config[no_regs++] = adev->family;
3455 config[no_regs++] = adev->external_rev_id;
3456
Tom St Denis9a999352017-01-18 13:01:25 -05003457 /* rev==3 */
3458 config[no_regs++] = adev->pdev->device;
3459 config[no_regs++] = adev->pdev->revision;
3460 config[no_regs++] = adev->pdev->subsystem_device;
3461 config[no_regs++] = adev->pdev->subsystem_vendor;
3462
Tom St Denis1e051412016-06-27 09:57:18 -04003463 while (size && (*pos < no_regs * 4)) {
3464 uint32_t value;
3465
3466 value = config[*pos >> 2];
3467 r = put_user(value, (uint32_t *)buf);
3468 if (r) {
3469 kfree(config);
3470 return r;
3471 }
3472
3473 result += 4;
3474 buf += 4;
3475 *pos += 4;
3476 size -= 4;
3477 }
3478
3479 kfree(config);
3480 return result;
3481}
3482
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003483static ssize_t amdgpu_debugfs_sensor_read(struct file *f, char __user *buf,
3484 size_t size, loff_t *pos)
3485{
Al Viro45063092016-12-04 18:24:56 -05003486 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003487 int idx, x, outsize, r, valuesize;
3488 uint32_t values[16];
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003489
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003490 if (size & 3 || *pos & 0x3)
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003491 return -EINVAL;
3492
Samuel Pitoiset3cbc6142017-02-15 19:32:29 +01003493 if (amdgpu_dpm == 0)
3494 return -EINVAL;
3495
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003496 /* convert offset to sensor number */
3497 idx = *pos >> 2;
3498
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003499 valuesize = sizeof(values);
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003500 if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->read_sensor)
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003501 r = adev->powerplay.pp_funcs->read_sensor(adev->powerplay.pp_handle, idx, &values[0], &valuesize);
Samuel Pitoiset3cbc6142017-02-15 19:32:29 +01003502 else if (adev->pm.funcs && adev->pm.funcs->read_sensor)
3503 r = adev->pm.funcs->read_sensor(adev, idx, &values[0],
3504 &valuesize);
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003505 else
3506 return -EINVAL;
3507
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003508 if (size > valuesize)
3509 return -EINVAL;
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003510
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003511 outsize = 0;
3512 x = 0;
3513 if (!r) {
3514 while (size) {
3515 r = put_user(values[x++], (int32_t *)buf);
3516 buf += 4;
3517 size -= 4;
3518 outsize += 4;
3519 }
3520 }
3521
3522 return !r ? outsize : r;
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003523}
Tom St Denis1e051412016-06-27 09:57:18 -04003524
Tom St Denis273d7aa2016-10-11 14:48:55 -04003525static ssize_t amdgpu_debugfs_wave_read(struct file *f, char __user *buf,
3526 size_t size, loff_t *pos)
3527{
3528 struct amdgpu_device *adev = f->f_inode->i_private;
3529 int r, x;
3530 ssize_t result=0;
Tom St Denis472259f2016-10-14 09:49:09 -04003531 uint32_t offset, se, sh, cu, wave, simd, data[32];
Tom St Denis273d7aa2016-10-11 14:48:55 -04003532
3533 if (size & 3 || *pos & 3)
3534 return -EINVAL;
3535
3536 /* decode offset */
3537 offset = (*pos & 0x7F);
3538 se = ((*pos >> 7) & 0xFF);
3539 sh = ((*pos >> 15) & 0xFF);
3540 cu = ((*pos >> 23) & 0xFF);
3541 wave = ((*pos >> 31) & 0xFF);
3542 simd = ((*pos >> 37) & 0xFF);
Tom St Denis273d7aa2016-10-11 14:48:55 -04003543
3544 /* switch to the specific se/sh/cu */
3545 mutex_lock(&adev->grbm_idx_mutex);
3546 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
3547
3548 x = 0;
Tom St Denis472259f2016-10-14 09:49:09 -04003549 if (adev->gfx.funcs->read_wave_data)
3550 adev->gfx.funcs->read_wave_data(adev, simd, wave, data, &x);
Tom St Denis273d7aa2016-10-11 14:48:55 -04003551
3552 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
3553 mutex_unlock(&adev->grbm_idx_mutex);
3554
Tom St Denis5ecfb3b2016-10-13 12:15:03 -04003555 if (!x)
3556 return -EINVAL;
3557
Tom St Denis472259f2016-10-14 09:49:09 -04003558 while (size && (offset < x * 4)) {
Tom St Denis273d7aa2016-10-11 14:48:55 -04003559 uint32_t value;
3560
Tom St Denis472259f2016-10-14 09:49:09 -04003561 value = data[offset >> 2];
Tom St Denis273d7aa2016-10-11 14:48:55 -04003562 r = put_user(value, (uint32_t *)buf);
3563 if (r)
3564 return r;
3565
3566 result += 4;
3567 buf += 4;
Tom St Denis472259f2016-10-14 09:49:09 -04003568 offset += 4;
Tom St Denis273d7aa2016-10-11 14:48:55 -04003569 size -= 4;
3570 }
3571
3572 return result;
3573}
3574
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003575static ssize_t amdgpu_debugfs_gpr_read(struct file *f, char __user *buf,
3576 size_t size, loff_t *pos)
3577{
3578 struct amdgpu_device *adev = f->f_inode->i_private;
3579 int r;
3580 ssize_t result = 0;
3581 uint32_t offset, se, sh, cu, wave, simd, thread, bank, *data;
3582
3583 if (size & 3 || *pos & 3)
3584 return -EINVAL;
3585
3586 /* decode offset */
3587 offset = (*pos & 0xFFF); /* in dwords */
3588 se = ((*pos >> 12) & 0xFF);
3589 sh = ((*pos >> 20) & 0xFF);
3590 cu = ((*pos >> 28) & 0xFF);
3591 wave = ((*pos >> 36) & 0xFF);
3592 simd = ((*pos >> 44) & 0xFF);
3593 thread = ((*pos >> 52) & 0xFF);
3594 bank = ((*pos >> 60) & 1);
3595
3596 data = kmalloc_array(1024, sizeof(*data), GFP_KERNEL);
3597 if (!data)
3598 return -ENOMEM;
3599
3600 /* switch to the specific se/sh/cu */
3601 mutex_lock(&adev->grbm_idx_mutex);
3602 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
3603
3604 if (bank == 0) {
3605 if (adev->gfx.funcs->read_wave_vgprs)
3606 adev->gfx.funcs->read_wave_vgprs(adev, simd, wave, thread, offset, size>>2, data);
3607 } else {
3608 if (adev->gfx.funcs->read_wave_sgprs)
3609 adev->gfx.funcs->read_wave_sgprs(adev, simd, wave, offset, size>>2, data);
3610 }
3611
3612 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
3613 mutex_unlock(&adev->grbm_idx_mutex);
3614
3615 while (size) {
3616 uint32_t value;
3617
3618 value = data[offset++];
3619 r = put_user(value, (uint32_t *)buf);
3620 if (r) {
3621 result = r;
3622 goto err;
3623 }
3624
3625 result += 4;
3626 buf += 4;
3627 size -= 4;
3628 }
3629
3630err:
3631 kfree(data);
3632 return result;
3633}
3634
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003635static const struct file_operations amdgpu_debugfs_regs_fops = {
3636 .owner = THIS_MODULE,
3637 .read = amdgpu_debugfs_regs_read,
3638 .write = amdgpu_debugfs_regs_write,
3639 .llseek = default_llseek
3640};
Tom St Denisadcec282016-04-15 13:08:44 -04003641static const struct file_operations amdgpu_debugfs_regs_didt_fops = {
3642 .owner = THIS_MODULE,
3643 .read = amdgpu_debugfs_regs_didt_read,
3644 .write = amdgpu_debugfs_regs_didt_write,
3645 .llseek = default_llseek
3646};
3647static const struct file_operations amdgpu_debugfs_regs_pcie_fops = {
3648 .owner = THIS_MODULE,
3649 .read = amdgpu_debugfs_regs_pcie_read,
3650 .write = amdgpu_debugfs_regs_pcie_write,
3651 .llseek = default_llseek
3652};
3653static const struct file_operations amdgpu_debugfs_regs_smc_fops = {
3654 .owner = THIS_MODULE,
3655 .read = amdgpu_debugfs_regs_smc_read,
3656 .write = amdgpu_debugfs_regs_smc_write,
3657 .llseek = default_llseek
3658};
3659
Tom St Denis1e051412016-06-27 09:57:18 -04003660static const struct file_operations amdgpu_debugfs_gca_config_fops = {
3661 .owner = THIS_MODULE,
3662 .read = amdgpu_debugfs_gca_config_read,
3663 .llseek = default_llseek
3664};
3665
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003666static const struct file_operations amdgpu_debugfs_sensors_fops = {
3667 .owner = THIS_MODULE,
3668 .read = amdgpu_debugfs_sensor_read,
3669 .llseek = default_llseek
3670};
3671
Tom St Denis273d7aa2016-10-11 14:48:55 -04003672static const struct file_operations amdgpu_debugfs_wave_fops = {
3673 .owner = THIS_MODULE,
3674 .read = amdgpu_debugfs_wave_read,
3675 .llseek = default_llseek
3676};
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003677static const struct file_operations amdgpu_debugfs_gpr_fops = {
3678 .owner = THIS_MODULE,
3679 .read = amdgpu_debugfs_gpr_read,
3680 .llseek = default_llseek
3681};
Tom St Denis273d7aa2016-10-11 14:48:55 -04003682
Tom St Denisadcec282016-04-15 13:08:44 -04003683static const struct file_operations *debugfs_regs[] = {
3684 &amdgpu_debugfs_regs_fops,
3685 &amdgpu_debugfs_regs_didt_fops,
3686 &amdgpu_debugfs_regs_pcie_fops,
3687 &amdgpu_debugfs_regs_smc_fops,
Tom St Denis1e051412016-06-27 09:57:18 -04003688 &amdgpu_debugfs_gca_config_fops,
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003689 &amdgpu_debugfs_sensors_fops,
Tom St Denis273d7aa2016-10-11 14:48:55 -04003690 &amdgpu_debugfs_wave_fops,
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003691 &amdgpu_debugfs_gpr_fops,
Tom St Denisadcec282016-04-15 13:08:44 -04003692};
3693
3694static const char *debugfs_regs_names[] = {
3695 "amdgpu_regs",
3696 "amdgpu_regs_didt",
3697 "amdgpu_regs_pcie",
3698 "amdgpu_regs_smc",
Tom St Denis1e051412016-06-27 09:57:18 -04003699 "amdgpu_gca_config",
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003700 "amdgpu_sensors",
Tom St Denis273d7aa2016-10-11 14:48:55 -04003701 "amdgpu_wave",
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003702 "amdgpu_gpr",
Tom St Denisadcec282016-04-15 13:08:44 -04003703};
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003704
3705static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
3706{
3707 struct drm_minor *minor = adev->ddev->primary;
3708 struct dentry *ent, *root = minor->debugfs_root;
Tom St Denisadcec282016-04-15 13:08:44 -04003709 unsigned i, j;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003710
Tom St Denisadcec282016-04-15 13:08:44 -04003711 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3712 ent = debugfs_create_file(debugfs_regs_names[i],
3713 S_IFREG | S_IRUGO, root,
3714 adev, debugfs_regs[i]);
3715 if (IS_ERR(ent)) {
3716 for (j = 0; j < i; j++) {
3717 debugfs_remove(adev->debugfs_regs[i]);
3718 adev->debugfs_regs[i] = NULL;
3719 }
3720 return PTR_ERR(ent);
3721 }
3722
3723 if (!i)
3724 i_size_write(ent->d_inode, adev->rmmio_size);
3725 adev->debugfs_regs[i] = ent;
3726 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003727
3728 return 0;
3729}
3730
3731static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev)
3732{
Tom St Denisadcec282016-04-15 13:08:44 -04003733 unsigned i;
3734
3735 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3736 if (adev->debugfs_regs[i]) {
3737 debugfs_remove(adev->debugfs_regs[i]);
3738 adev->debugfs_regs[i] = NULL;
3739 }
3740 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003741}
3742
Huang Rui4f0955f2017-05-10 23:04:06 +08003743static int amdgpu_debugfs_test_ib(struct seq_file *m, void *data)
3744{
3745 struct drm_info_node *node = (struct drm_info_node *) m->private;
3746 struct drm_device *dev = node->minor->dev;
3747 struct amdgpu_device *adev = dev->dev_private;
3748 int r = 0, i;
3749
3750 /* hold on the scheduler */
3751 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
3752 struct amdgpu_ring *ring = adev->rings[i];
3753
3754 if (!ring || !ring->sched.thread)
3755 continue;
3756 kthread_park(ring->sched.thread);
3757 }
3758
3759 seq_printf(m, "run ib test:\n");
3760 r = amdgpu_ib_ring_tests(adev);
3761 if (r)
3762 seq_printf(m, "ib ring tests failed (%d).\n", r);
3763 else
3764 seq_printf(m, "ib ring tests passed.\n");
3765
3766 /* go on the scheduler */
3767 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
3768 struct amdgpu_ring *ring = adev->rings[i];
3769
3770 if (!ring || !ring->sched.thread)
3771 continue;
3772 kthread_unpark(ring->sched.thread);
3773 }
3774
3775 return 0;
3776}
3777
3778static const struct drm_info_list amdgpu_debugfs_test_ib_ring_list[] = {
3779 {"amdgpu_test_ib", &amdgpu_debugfs_test_ib}
3780};
3781
3782static int amdgpu_debugfs_test_ib_ring_init(struct amdgpu_device *adev)
3783{
3784 return amdgpu_debugfs_add_files(adev,
3785 amdgpu_debugfs_test_ib_ring_list, 1);
3786}
3787
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003788int amdgpu_debugfs_init(struct drm_minor *minor)
3789{
3790 return 0;
3791}
Kent Russelldb95e212017-08-22 12:31:43 -04003792
3793static int amdgpu_debugfs_get_vbios_dump(struct seq_file *m, void *data)
3794{
3795 struct drm_info_node *node = (struct drm_info_node *) m->private;
3796 struct drm_device *dev = node->minor->dev;
3797 struct amdgpu_device *adev = dev->dev_private;
3798
3799 seq_write(m, adev->bios, adev->bios_size);
3800 return 0;
3801}
3802
Kent Russelldb95e212017-08-22 12:31:43 -04003803static const struct drm_info_list amdgpu_vbios_dump_list[] = {
3804 {"amdgpu_vbios",
3805 amdgpu_debugfs_get_vbios_dump,
3806 0, NULL},
3807};
3808
Kent Russelldb95e212017-08-22 12:31:43 -04003809static int amdgpu_debugfs_vbios_dump_init(struct amdgpu_device *adev)
3810{
3811 return amdgpu_debugfs_add_files(adev,
3812 amdgpu_vbios_dump_list, 1);
3813}
Alexander Kuleshov7cebc722015-06-27 13:16:05 +06003814#else
Arnd Bergmann27bad5b2017-06-21 23:51:02 +02003815static int amdgpu_debugfs_test_ib_ring_init(struct amdgpu_device *adev)
Huang Rui4f0955f2017-05-10 23:04:06 +08003816{
3817 return 0;
3818}
Alexander Kuleshov7cebc722015-06-27 13:16:05 +06003819static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
3820{
3821 return 0;
3822}
Kent Russelldb95e212017-08-22 12:31:43 -04003823static int amdgpu_debugfs_vbios_dump_init(struct amdgpu_device *adev)
3824{
3825 return 0;
3826}
Alexander Kuleshov7cebc722015-06-27 13:16:05 +06003827static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev) { }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003828#endif