blob: d33e1061e04b97923480b6ebceff6dac598b90a2 [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);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040068
69static const char *amdgpu_asic_name[] = {
Ken Wangda69c1612016-01-21 19:08:55 +080070 "TAHITI",
71 "PITCAIRN",
72 "VERDE",
73 "OLAND",
74 "HAINAN",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040075 "BONAIRE",
76 "KAVERI",
77 "KABINI",
78 "HAWAII",
79 "MULLINS",
80 "TOPAZ",
81 "TONGA",
David Zhang48299f92015-07-08 01:05:16 +080082 "FIJI",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040083 "CARRIZO",
Samuel Li139f4912015-10-08 14:50:27 -040084 "STONEY",
Flora Cui2cc0c0b2016-03-14 18:33:29 -040085 "POLARIS10",
86 "POLARIS11",
Junwei Zhangc4642a42016-12-14 15:32:28 -050087 "POLARIS12",
Ken Wangd4196f02016-03-09 09:28:32 +080088 "VEGA10",
Chunming Zhou2ca8a5d2016-12-07 17:31:19 +080089 "RAVEN",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040090 "LAST",
91};
92
93bool amdgpu_device_is_px(struct drm_device *dev)
94{
95 struct amdgpu_device *adev = dev->dev_private;
96
Jammy Zhou2f7d10b2015-07-22 11:29:01 +080097 if (adev->flags & AMD_IS_PX)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040098 return true;
99 return false;
100}
101
102/*
103 * MMIO register access helper functions.
104 */
105uint32_t amdgpu_mm_rreg(struct amdgpu_device *adev, uint32_t reg,
Monk Liu15d72fd2017-01-25 15:07:40 +0800106 uint32_t acc_flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400107{
Tom St Denisf4b373f2016-05-31 08:02:27 -0400108 uint32_t ret;
109
Monk Liu15d72fd2017-01-25 15:07:40 +0800110 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev)) {
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800111 BUG_ON(in_interrupt());
112 return amdgpu_virt_kiq_rreg(adev, reg);
113 }
114
Monk Liu15d72fd2017-01-25 15:07:40 +0800115 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
Tom St Denisf4b373f2016-05-31 08:02:27 -0400116 ret = readl(((void __iomem *)adev->rmmio) + (reg * 4));
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400117 else {
118 unsigned long flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400119
120 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
121 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
122 ret = readl(((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
123 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400124 }
Tom St Denisf4b373f2016-05-31 08:02:27 -0400125 trace_amdgpu_mm_rreg(adev->pdev->device, reg, ret);
126 return ret;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400127}
128
129void amdgpu_mm_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v,
Monk Liu15d72fd2017-01-25 15:07:40 +0800130 uint32_t acc_flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400131{
Tom St Denisf4b373f2016-05-31 08:02:27 -0400132 trace_amdgpu_mm_wreg(adev->pdev->device, reg, v);
Monk Liu4e99a442016-03-31 13:26:59 +0800133
Ken Wang47ed4e12017-07-04 13:11:52 +0800134 if (adev->asic_type >= CHIP_VEGA10 && reg == 0) {
135 adev->last_mm_index = v;
136 }
137
Monk Liu15d72fd2017-01-25 15:07:40 +0800138 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev)) {
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800139 BUG_ON(in_interrupt());
140 return amdgpu_virt_kiq_wreg(adev, reg, v);
141 }
142
Monk Liu15d72fd2017-01-25 15:07:40 +0800143 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400144 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
145 else {
146 unsigned long flags;
147
148 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
149 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
150 writel(v, ((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
151 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
152 }
Ken Wang47ed4e12017-07-04 13:11:52 +0800153
154 if (adev->asic_type >= CHIP_VEGA10 && reg == 1 && adev->last_mm_index == 0x5702C) {
155 udelay(500);
156 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400157}
158
159u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 reg)
160{
161 if ((reg * 4) < adev->rio_mem_size)
162 return ioread32(adev->rio_mem + (reg * 4));
163 else {
164 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
165 return ioread32(adev->rio_mem + (mmMM_DATA * 4));
166 }
167}
168
169void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
170{
Ken Wang47ed4e12017-07-04 13:11:52 +0800171 if (adev->asic_type >= CHIP_VEGA10 && reg == 0) {
172 adev->last_mm_index = v;
173 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400174
175 if ((reg * 4) < adev->rio_mem_size)
176 iowrite32(v, adev->rio_mem + (reg * 4));
177 else {
178 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
179 iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
180 }
Ken Wang47ed4e12017-07-04 13:11:52 +0800181
182 if (adev->asic_type >= CHIP_VEGA10 && reg == 1 && adev->last_mm_index == 0x5702C) {
183 udelay(500);
184 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400185}
186
187/**
188 * amdgpu_mm_rdoorbell - read a doorbell dword
189 *
190 * @adev: amdgpu_device pointer
191 * @index: doorbell index
192 *
193 * Returns the value in the doorbell aperture at the
194 * requested doorbell index (CIK).
195 */
196u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
197{
198 if (index < adev->doorbell.num_doorbells) {
199 return readl(adev->doorbell.ptr + index);
200 } else {
201 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
202 return 0;
203 }
204}
205
206/**
207 * amdgpu_mm_wdoorbell - write a doorbell dword
208 *
209 * @adev: amdgpu_device pointer
210 * @index: doorbell index
211 * @v: value to write
212 *
213 * Writes @v to the doorbell aperture at the
214 * requested doorbell index (CIK).
215 */
216void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
217{
218 if (index < adev->doorbell.num_doorbells) {
219 writel(v, adev->doorbell.ptr + index);
220 } else {
221 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
222 }
223}
224
225/**
Ken Wang832be402016-03-18 15:23:08 +0800226 * amdgpu_mm_rdoorbell64 - read a doorbell Qword
227 *
228 * @adev: amdgpu_device pointer
229 * @index: doorbell index
230 *
231 * Returns the value in the doorbell aperture at the
232 * requested doorbell index (VEGA10+).
233 */
234u64 amdgpu_mm_rdoorbell64(struct amdgpu_device *adev, u32 index)
235{
236 if (index < adev->doorbell.num_doorbells) {
237 return atomic64_read((atomic64_t *)(adev->doorbell.ptr + index));
238 } else {
239 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
240 return 0;
241 }
242}
243
244/**
245 * amdgpu_mm_wdoorbell64 - write a doorbell Qword
246 *
247 * @adev: amdgpu_device pointer
248 * @index: doorbell index
249 * @v: value to write
250 *
251 * Writes @v to the doorbell aperture at the
252 * requested doorbell index (VEGA10+).
253 */
254void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v)
255{
256 if (index < adev->doorbell.num_doorbells) {
257 atomic64_set((atomic64_t *)(adev->doorbell.ptr + index), v);
258 } else {
259 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
260 }
261}
262
263/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400264 * amdgpu_invalid_rreg - dummy reg read function
265 *
266 * @adev: amdgpu device pointer
267 * @reg: offset of register
268 *
269 * Dummy register read function. Used for register blocks
270 * that certain asics don't have (all asics).
271 * Returns the value in the register.
272 */
273static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
274{
275 DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
276 BUG();
277 return 0;
278}
279
280/**
281 * amdgpu_invalid_wreg - dummy reg write function
282 *
283 * @adev: amdgpu device pointer
284 * @reg: offset of register
285 * @v: value to write to the register
286 *
287 * Dummy register read function. Used for register blocks
288 * that certain asics don't have (all asics).
289 */
290static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
291{
292 DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
293 reg, v);
294 BUG();
295}
296
297/**
298 * amdgpu_block_invalid_rreg - dummy reg read function
299 *
300 * @adev: amdgpu device pointer
301 * @block: offset of instance
302 * @reg: offset of register
303 *
304 * Dummy register read function. Used for register blocks
305 * that certain asics don't have (all asics).
306 * Returns the value in the register.
307 */
308static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
309 uint32_t block, uint32_t reg)
310{
311 DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
312 reg, block);
313 BUG();
314 return 0;
315}
316
317/**
318 * amdgpu_block_invalid_wreg - dummy reg write function
319 *
320 * @adev: amdgpu device pointer
321 * @block: offset of instance
322 * @reg: offset of register
323 * @v: value to write to the register
324 *
325 * Dummy register read function. Used for register blocks
326 * that certain asics don't have (all asics).
327 */
328static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
329 uint32_t block,
330 uint32_t reg, uint32_t v)
331{
332 DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
333 reg, block, v);
334 BUG();
335}
336
337static int amdgpu_vram_scratch_init(struct amdgpu_device *adev)
338{
Christian Königa4a02772017-07-27 17:24:36 +0200339 return amdgpu_bo_create_kernel(adev, AMDGPU_GPU_PAGE_SIZE,
340 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
341 &adev->vram_scratch.robj,
342 &adev->vram_scratch.gpu_addr,
343 (void **)&adev->vram_scratch.ptr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400344}
345
346static void amdgpu_vram_scratch_fini(struct amdgpu_device *adev)
347{
Christian König078af1a2017-07-27 17:43:00 +0200348 amdgpu_bo_free_kernel(&adev->vram_scratch.robj, NULL, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400349}
350
351/**
352 * amdgpu_program_register_sequence - program an array of registers.
353 *
354 * @adev: amdgpu_device pointer
355 * @registers: pointer to the register array
356 * @array_size: size of the register array
357 *
358 * Programs an array or registers with and and or masks.
359 * This is a helper for setting golden registers.
360 */
361void amdgpu_program_register_sequence(struct amdgpu_device *adev,
362 const u32 *registers,
363 const u32 array_size)
364{
365 u32 tmp, reg, and_mask, or_mask;
366 int i;
367
368 if (array_size % 3)
369 return;
370
371 for (i = 0; i < array_size; i +=3) {
372 reg = registers[i + 0];
373 and_mask = registers[i + 1];
374 or_mask = registers[i + 2];
375
376 if (and_mask == 0xffffffff) {
377 tmp = or_mask;
378 } else {
379 tmp = RREG32(reg);
380 tmp &= ~and_mask;
381 tmp |= or_mask;
382 }
383 WREG32(reg, tmp);
384 }
385}
386
387void amdgpu_pci_config_reset(struct amdgpu_device *adev)
388{
389 pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
390}
391
392/*
393 * GPU doorbell aperture helpers function.
394 */
395/**
396 * amdgpu_doorbell_init - Init doorbell driver information.
397 *
398 * @adev: amdgpu_device pointer
399 *
400 * Init doorbell driver information (CIK)
401 * Returns 0 on success, error on failure.
402 */
403static int amdgpu_doorbell_init(struct amdgpu_device *adev)
404{
405 /* doorbell bar mapping */
406 adev->doorbell.base = pci_resource_start(adev->pdev, 2);
407 adev->doorbell.size = pci_resource_len(adev->pdev, 2);
408
Christian Königedf600d2016-05-03 15:54:54 +0200409 adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400410 AMDGPU_DOORBELL_MAX_ASSIGNMENT+1);
411 if (adev->doorbell.num_doorbells == 0)
412 return -EINVAL;
413
Christian König8972e5d2017-03-06 13:34:57 +0100414 adev->doorbell.ptr = ioremap(adev->doorbell.base,
415 adev->doorbell.num_doorbells *
416 sizeof(u32));
417 if (adev->doorbell.ptr == NULL)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400418 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400419
420 return 0;
421}
422
423/**
424 * amdgpu_doorbell_fini - Tear down doorbell driver information.
425 *
426 * @adev: amdgpu_device pointer
427 *
428 * Tear down doorbell driver information (CIK)
429 */
430static void amdgpu_doorbell_fini(struct amdgpu_device *adev)
431{
432 iounmap(adev->doorbell.ptr);
433 adev->doorbell.ptr = NULL;
434}
435
436/**
437 * amdgpu_doorbell_get_kfd_info - Report doorbell configuration required to
438 * setup amdkfd
439 *
440 * @adev: amdgpu_device pointer
441 * @aperture_base: output returning doorbell aperture base physical address
442 * @aperture_size: output returning doorbell aperture size in bytes
443 * @start_offset: output returning # of doorbell bytes reserved for amdgpu.
444 *
445 * amdgpu and amdkfd share the doorbell aperture. amdgpu sets it up,
446 * takes doorbells required for its own rings and reports the setup to amdkfd.
447 * amdgpu reserved doorbells are at the start of the doorbell aperture.
448 */
449void amdgpu_doorbell_get_kfd_info(struct amdgpu_device *adev,
450 phys_addr_t *aperture_base,
451 size_t *aperture_size,
452 size_t *start_offset)
453{
454 /*
455 * The first num_doorbells are used by amdgpu.
456 * amdkfd takes whatever's left in the aperture.
457 */
458 if (adev->doorbell.size > adev->doorbell.num_doorbells * sizeof(u32)) {
459 *aperture_base = adev->doorbell.base;
460 *aperture_size = adev->doorbell.size;
461 *start_offset = adev->doorbell.num_doorbells * sizeof(u32);
462 } else {
463 *aperture_base = 0;
464 *aperture_size = 0;
465 *start_offset = 0;
466 }
467}
468
469/*
470 * amdgpu_wb_*()
Alex Xie455a7bc2017-05-08 21:36:03 -0400471 * Writeback is the method by which the GPU updates special pages in memory
Alex Xieea81a172017-05-08 13:41:11 -0400472 * with the status of certain GPU events (fences, ring pointers,etc.).
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400473 */
474
475/**
476 * amdgpu_wb_fini - Disable Writeback and free memory
477 *
478 * @adev: amdgpu_device pointer
479 *
480 * Disables Writeback and frees the Writeback memory (all asics).
481 * Used at driver shutdown.
482 */
483static void amdgpu_wb_fini(struct amdgpu_device *adev)
484{
485 if (adev->wb.wb_obj) {
Alex Deuchera76ed482016-10-21 15:30:36 -0400486 amdgpu_bo_free_kernel(&adev->wb.wb_obj,
487 &adev->wb.gpu_addr,
488 (void **)&adev->wb.wb);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400489 adev->wb.wb_obj = NULL;
490 }
491}
492
493/**
494 * amdgpu_wb_init- Init Writeback driver info and allocate memory
495 *
496 * @adev: amdgpu_device pointer
497 *
Alex Xie455a7bc2017-05-08 21:36:03 -0400498 * Initializes writeback and allocates writeback memory (all asics).
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400499 * Used at driver startup.
500 * Returns 0 on success or an -error on failure.
501 */
502static int amdgpu_wb_init(struct amdgpu_device *adev)
503{
504 int r;
505
506 if (adev->wb.wb_obj == NULL) {
Huang Rui60a970a62017-03-15 10:13:32 +0800507 r = amdgpu_bo_create_kernel(adev, AMDGPU_MAX_WB * sizeof(uint32_t),
Alex Deuchera76ed482016-10-21 15:30:36 -0400508 PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
509 &adev->wb.wb_obj, &adev->wb.gpu_addr,
510 (void **)&adev->wb.wb);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400511 if (r) {
512 dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
513 return r;
514 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400515
516 adev->wb.num_wb = AMDGPU_MAX_WB;
517 memset(&adev->wb.used, 0, sizeof(adev->wb.used));
518
519 /* clear wb memory */
Huang Rui60a970a62017-03-15 10:13:32 +0800520 memset((char *)adev->wb.wb, 0, AMDGPU_MAX_WB * sizeof(uint32_t));
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400521 }
522
523 return 0;
524}
525
526/**
527 * amdgpu_wb_get - Allocate a wb entry
528 *
529 * @adev: amdgpu_device pointer
530 * @wb: wb index
531 *
532 * Allocate a wb slot for use by the driver (all asics).
533 * Returns 0 on success or -EINVAL on failure.
534 */
535int amdgpu_wb_get(struct amdgpu_device *adev, u32 *wb)
536{
537 unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
538 if (offset < adev->wb.num_wb) {
539 __set_bit(offset, adev->wb.used);
540 *wb = offset;
541 return 0;
542 } else {
543 return -EINVAL;
544 }
545}
546
547/**
Ken Wang70142852016-03-18 15:08:49 +0800548 * amdgpu_wb_get_64bit - Allocate a wb entry
549 *
550 * @adev: amdgpu_device pointer
551 * @wb: wb index
552 *
553 * Allocate a wb slot for use by the driver (all asics).
554 * Returns 0 on success or -EINVAL on failure.
555 */
556int amdgpu_wb_get_64bit(struct amdgpu_device *adev, u32 *wb)
557{
558 unsigned long offset = bitmap_find_next_zero_area_off(adev->wb.used,
559 adev->wb.num_wb, 0, 2, 7, 0);
560 if ((offset + 1) < adev->wb.num_wb) {
561 __set_bit(offset, adev->wb.used);
562 __set_bit(offset + 1, adev->wb.used);
563 *wb = offset;
564 return 0;
565 } else {
566 return -EINVAL;
567 }
568}
569
Alex Deuchereacf3e12017-07-27 15:10:50 -0400570int amdgpu_wb_get_256bit(struct amdgpu_device *adev, u32 *wb)
Monk Liu0915fdb2017-06-19 10:19:41 -0400571{
572 int i = 0;
573 unsigned long offset = bitmap_find_next_zero_area_off(adev->wb.used,
574 adev->wb.num_wb, 0, 8, 63, 0);
575 if ((offset + 7) < adev->wb.num_wb) {
576 for (i = 0; i < 8; i++)
577 __set_bit(offset + i, adev->wb.used);
578 *wb = offset;
579 return 0;
580 } else {
581 return -EINVAL;
582 }
583}
584
Ken Wang70142852016-03-18 15:08:49 +0800585/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400586 * amdgpu_wb_free - Free a wb entry
587 *
588 * @adev: amdgpu_device pointer
589 * @wb: wb index
590 *
591 * Free a wb slot allocated for use by the driver (all asics)
592 */
593void amdgpu_wb_free(struct amdgpu_device *adev, u32 wb)
594{
595 if (wb < adev->wb.num_wb)
596 __clear_bit(wb, adev->wb.used);
597}
598
599/**
Ken Wang70142852016-03-18 15:08:49 +0800600 * amdgpu_wb_free_64bit - Free a wb entry
601 *
602 * @adev: amdgpu_device pointer
603 * @wb: wb index
604 *
605 * Free a wb slot allocated for use by the driver (all asics)
606 */
607void amdgpu_wb_free_64bit(struct amdgpu_device *adev, u32 wb)
608{
609 if ((wb + 1) < adev->wb.num_wb) {
610 __clear_bit(wb, adev->wb.used);
611 __clear_bit(wb + 1, adev->wb.used);
612 }
613}
614
615/**
Monk Liu0915fdb2017-06-19 10:19:41 -0400616 * amdgpu_wb_free_256bit - Free a wb entry
617 *
618 * @adev: amdgpu_device pointer
619 * @wb: wb index
620 *
621 * Free a wb slot allocated for use by the driver (all asics)
622 */
623void amdgpu_wb_free_256bit(struct amdgpu_device *adev, u32 wb)
624{
625 int i = 0;
626
627 if ((wb + 7) < adev->wb.num_wb)
628 for (i = 0; i < 8; i++)
629 __clear_bit(wb + i, adev->wb.used);
630}
631
632/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400633 * amdgpu_vram_location - try to find VRAM location
634 * @adev: amdgpu device structure holding all necessary informations
635 * @mc: memory controller structure holding memory informations
636 * @base: base address at which to put VRAM
637 *
Alex Xie455a7bc2017-05-08 21:36:03 -0400638 * Function will try to place VRAM at base address provided
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400639 * as parameter (which is so far either PCI aperture address or
640 * for IGP TOM base address).
641 *
642 * If there is not enough space to fit the unvisible VRAM in the 32bits
643 * address space then we limit the VRAM size to the aperture.
644 *
645 * Note: We don't explicitly enforce VRAM start to be aligned on VRAM size,
646 * this shouldn't be a problem as we are using the PCI aperture as a reference.
647 * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but
648 * not IGP.
649 *
650 * Note: we use mc_vram_size as on some board we need to program the mc to
651 * cover the whole aperture even if VRAM size is inferior to aperture size
652 * Novell bug 204882 + along with lots of ubuntu ones
653 *
654 * Note: when limiting vram it's safe to overwritte real_vram_size because
655 * we are not in case where real_vram_size is inferior to mc_vram_size (ie
656 * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu
657 * ones)
658 *
659 * Note: IGP TOM addr should be the same as the aperture addr, we don't
Alex Xie455a7bc2017-05-08 21:36:03 -0400660 * explicitly check for that though.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400661 *
662 * FIXME: when reducing VRAM size align new size on power of 2.
663 */
664void amdgpu_vram_location(struct amdgpu_device *adev, struct amdgpu_mc *mc, u64 base)
665{
666 uint64_t limit = (uint64_t)amdgpu_vram_limit << 20;
667
668 mc->vram_start = base;
669 if (mc->mc_vram_size > (adev->mc.mc_mask - base + 1)) {
670 dev_warn(adev->dev, "limiting VRAM to PCI aperture size\n");
671 mc->real_vram_size = mc->aper_size;
672 mc->mc_vram_size = mc->aper_size;
673 }
674 mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
675 if (limit && limit < mc->real_vram_size)
676 mc->real_vram_size = limit;
677 dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
678 mc->mc_vram_size >> 20, mc->vram_start,
679 mc->vram_end, mc->real_vram_size >> 20);
680}
681
682/**
Christian König6f02a692017-07-07 11:56:59 +0200683 * amdgpu_gart_location - try to find GTT location
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400684 * @adev: amdgpu device structure holding all necessary informations
685 * @mc: memory controller structure holding memory informations
686 *
687 * Function will place try to place GTT before or after VRAM.
688 *
689 * If GTT size is bigger than space left then we ajust GTT size.
690 * Thus function will never fails.
691 *
692 * FIXME: when reducing GTT size align new size on power of 2.
693 */
Christian König6f02a692017-07-07 11:56:59 +0200694void amdgpu_gart_location(struct amdgpu_device *adev, struct amdgpu_mc *mc)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400695{
696 u64 size_af, size_bf;
697
Christian Königed21c042017-07-06 22:26:05 +0200698 size_af = adev->mc.mc_mask - mc->vram_end;
699 size_bf = mc->vram_start;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400700 if (size_bf > size_af) {
Christian König6f02a692017-07-07 11:56:59 +0200701 if (mc->gart_size > size_bf) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400702 dev_warn(adev->dev, "limiting GTT\n");
Christian König6f02a692017-07-07 11:56:59 +0200703 mc->gart_size = size_bf;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400704 }
Christian König6f02a692017-07-07 11:56:59 +0200705 mc->gart_start = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400706 } else {
Christian König6f02a692017-07-07 11:56:59 +0200707 if (mc->gart_size > size_af) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400708 dev_warn(adev->dev, "limiting GTT\n");
Christian König6f02a692017-07-07 11:56:59 +0200709 mc->gart_size = size_af;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400710 }
Christian König6f02a692017-07-07 11:56:59 +0200711 mc->gart_start = mc->vram_end + 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400712 }
Christian König6f02a692017-07-07 11:56:59 +0200713 mc->gart_end = mc->gart_start + mc->gart_size - 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400714 dev_info(adev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
Christian König6f02a692017-07-07 11:56:59 +0200715 mc->gart_size >> 20, mc->gart_start, mc->gart_end);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400716}
717
718/*
719 * GPU helpers function.
720 */
721/**
Jim Quc836fec2017-02-10 15:59:59 +0800722 * amdgpu_need_post - check if the hw need post or not
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400723 *
724 * @adev: amdgpu_device pointer
725 *
Jim Quc836fec2017-02-10 15:59:59 +0800726 * Check if the asic has been initialized (all asics) at driver startup
727 * or post is needed if hw reset is performed.
728 * Returns true if need or false if not.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400729 */
Jim Quc836fec2017-02-10 15:59:59 +0800730bool amdgpu_need_post(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400731{
732 uint32_t reg;
733
Jim Quc836fec2017-02-10 15:59:59 +0800734 if (adev->has_hw_reset) {
735 adev->has_hw_reset = false;
736 return true;
737 }
Alex Deucher70d17a22017-06-30 17:26:47 -0400738
739 /* bios scratch used on CIK+ */
740 if (adev->asic_type >= CHIP_BONAIRE)
741 return amdgpu_atombios_scratch_need_asic_init(adev);
742
743 /* check MEM_SIZE for older asics */
Alex Deucherbbf282d2017-03-03 17:26:10 -0500744 reg = amdgpu_asic_get_config_memsize(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400745
Alex Deucherf2713e82017-03-28 12:19:31 -0400746 if ((reg != 0) && (reg != 0xffffffff))
Jim Quc836fec2017-02-10 15:59:59 +0800747 return false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400748
Jim Quc836fec2017-02-10 15:59:59 +0800749 return true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400750
751}
752
Monk Liubec86372016-09-14 19:38:08 +0800753static bool amdgpu_vpost_needed(struct amdgpu_device *adev)
754{
755 if (amdgpu_sriov_vf(adev))
756 return false;
757
758 if (amdgpu_passthrough(adev)) {
Monk Liu1da2c322016-11-11 11:24:29 +0800759 /* for FIJI: In whole GPU pass-through virtualization case, after VM reboot
760 * some old smc fw still need driver do vPost otherwise gpu hang, while
761 * those smc fw version above 22.15 doesn't have this flaw, so we force
762 * vpost executed for smc version below 22.15
Monk Liubec86372016-09-14 19:38:08 +0800763 */
764 if (adev->asic_type == CHIP_FIJI) {
765 int err;
766 uint32_t fw_ver;
767 err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
768 /* force vPost if error occured */
769 if (err)
770 return true;
771
772 fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
Monk Liu1da2c322016-11-11 11:24:29 +0800773 if (fw_ver < 0x00160e00)
774 return true;
Monk Liubec86372016-09-14 19:38:08 +0800775 }
Monk Liubec86372016-09-14 19:38:08 +0800776 }
Jim Quc836fec2017-02-10 15:59:59 +0800777 return amdgpu_need_post(adev);
Monk Liubec86372016-09-14 19:38:08 +0800778}
779
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400780/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400781 * amdgpu_dummy_page_init - init dummy page used by the driver
782 *
783 * @adev: amdgpu_device pointer
784 *
785 * Allocate the dummy page used by the driver (all asics).
786 * This dummy page is used by the driver as a filler for gart entries
787 * when pages are taken out of the GART
788 * Returns 0 on sucess, -ENOMEM on failure.
789 */
790int amdgpu_dummy_page_init(struct amdgpu_device *adev)
791{
792 if (adev->dummy_page.page)
793 return 0;
794 adev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
795 if (adev->dummy_page.page == NULL)
796 return -ENOMEM;
797 adev->dummy_page.addr = pci_map_page(adev->pdev, adev->dummy_page.page,
798 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
799 if (pci_dma_mapping_error(adev->pdev, adev->dummy_page.addr)) {
800 dev_err(&adev->pdev->dev, "Failed to DMA MAP the dummy page\n");
801 __free_page(adev->dummy_page.page);
802 adev->dummy_page.page = NULL;
803 return -ENOMEM;
804 }
805 return 0;
806}
807
808/**
809 * amdgpu_dummy_page_fini - free dummy page used by the driver
810 *
811 * @adev: amdgpu_device pointer
812 *
813 * Frees the dummy page used by the driver (all asics).
814 */
815void amdgpu_dummy_page_fini(struct amdgpu_device *adev)
816{
817 if (adev->dummy_page.page == NULL)
818 return;
819 pci_unmap_page(adev->pdev, adev->dummy_page.addr,
820 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
821 __free_page(adev->dummy_page.page);
822 adev->dummy_page.page = NULL;
823}
824
825
826/* ATOM accessor methods */
827/*
828 * ATOM is an interpreted byte code stored in tables in the vbios. The
829 * driver registers callbacks to access registers and the interpreter
830 * in the driver parses the tables and executes then to program specific
831 * actions (set display modes, asic init, etc.). See amdgpu_atombios.c,
832 * atombios.h, and atom.c
833 */
834
835/**
836 * cail_pll_read - read PLL register
837 *
838 * @info: atom card_info pointer
839 * @reg: PLL register offset
840 *
841 * Provides a PLL register accessor for the atom interpreter (r4xx+).
842 * Returns the value of the PLL register.
843 */
844static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
845{
846 return 0;
847}
848
849/**
850 * cail_pll_write - write PLL register
851 *
852 * @info: atom card_info pointer
853 * @reg: PLL register offset
854 * @val: value to write to the pll register
855 *
856 * Provides a PLL register accessor for the atom interpreter (r4xx+).
857 */
858static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
859{
860
861}
862
863/**
864 * cail_mc_read - read MC (Memory Controller) register
865 *
866 * @info: atom card_info pointer
867 * @reg: MC register offset
868 *
869 * Provides an MC register accessor for the atom interpreter (r4xx+).
870 * Returns the value of the MC register.
871 */
872static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
873{
874 return 0;
875}
876
877/**
878 * cail_mc_write - write MC (Memory Controller) register
879 *
880 * @info: atom card_info pointer
881 * @reg: MC register offset
882 * @val: value to write to the pll register
883 *
884 * Provides a MC register accessor for the atom interpreter (r4xx+).
885 */
886static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
887{
888
889}
890
891/**
892 * cail_reg_write - write MMIO register
893 *
894 * @info: atom card_info pointer
895 * @reg: MMIO register offset
896 * @val: value to write to the pll register
897 *
898 * Provides a MMIO register accessor for the atom interpreter (r4xx+).
899 */
900static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
901{
902 struct amdgpu_device *adev = info->dev->dev_private;
903
904 WREG32(reg, val);
905}
906
907/**
908 * cail_reg_read - read MMIO register
909 *
910 * @info: atom card_info pointer
911 * @reg: MMIO register offset
912 *
913 * Provides an MMIO register accessor for the atom interpreter (r4xx+).
914 * Returns the value of the MMIO register.
915 */
916static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
917{
918 struct amdgpu_device *adev = info->dev->dev_private;
919 uint32_t r;
920
921 r = RREG32(reg);
922 return r;
923}
924
925/**
926 * cail_ioreg_write - write IO register
927 *
928 * @info: atom card_info pointer
929 * @reg: IO register offset
930 * @val: value to write to the pll register
931 *
932 * Provides a IO register accessor for the atom interpreter (r4xx+).
933 */
934static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
935{
936 struct amdgpu_device *adev = info->dev->dev_private;
937
938 WREG32_IO(reg, val);
939}
940
941/**
942 * cail_ioreg_read - read IO register
943 *
944 * @info: atom card_info pointer
945 * @reg: IO register offset
946 *
947 * Provides an IO register accessor for the atom interpreter (r4xx+).
948 * Returns the value of the IO register.
949 */
950static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
951{
952 struct amdgpu_device *adev = info->dev->dev_private;
953 uint32_t r;
954
955 r = RREG32_IO(reg);
956 return r;
957}
958
959/**
960 * amdgpu_atombios_fini - free the driver info and callbacks for atombios
961 *
962 * @adev: amdgpu_device pointer
963 *
964 * Frees the driver info and register access callbacks for the ATOM
965 * interpreter (r4xx+).
966 * Called at driver shutdown.
967 */
968static void amdgpu_atombios_fini(struct amdgpu_device *adev)
969{
Monk Liu89e0ec9f2016-05-27 19:34:11 +0800970 if (adev->mode_info.atom_context) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400971 kfree(adev->mode_info.atom_context->scratch);
Monk Liu89e0ec9f2016-05-27 19:34:11 +0800972 kfree(adev->mode_info.atom_context->iio);
973 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400974 kfree(adev->mode_info.atom_context);
975 adev->mode_info.atom_context = NULL;
976 kfree(adev->mode_info.atom_card_info);
977 adev->mode_info.atom_card_info = NULL;
978}
979
980/**
981 * amdgpu_atombios_init - init the driver info and callbacks for atombios
982 *
983 * @adev: amdgpu_device pointer
984 *
985 * Initializes the driver info and register access callbacks for the
986 * ATOM interpreter (r4xx+).
987 * Returns 0 on sucess, -ENOMEM on failure.
988 * Called at driver startup.
989 */
990static int amdgpu_atombios_init(struct amdgpu_device *adev)
991{
992 struct card_info *atom_card_info =
993 kzalloc(sizeof(struct card_info), GFP_KERNEL);
994
995 if (!atom_card_info)
996 return -ENOMEM;
997
998 adev->mode_info.atom_card_info = atom_card_info;
999 atom_card_info->dev = adev->ddev;
1000 atom_card_info->reg_read = cail_reg_read;
1001 atom_card_info->reg_write = cail_reg_write;
1002 /* needed for iio ops */
1003 if (adev->rio_mem) {
1004 atom_card_info->ioreg_read = cail_ioreg_read;
1005 atom_card_info->ioreg_write = cail_ioreg_write;
1006 } else {
Amber Linb64a18c2017-01-04 08:06:58 -05001007 DRM_INFO("PCI I/O BAR is not found. Using MMIO to access ATOM BIOS\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001008 atom_card_info->ioreg_read = cail_reg_read;
1009 atom_card_info->ioreg_write = cail_reg_write;
1010 }
1011 atom_card_info->mc_read = cail_mc_read;
1012 atom_card_info->mc_write = cail_mc_write;
1013 atom_card_info->pll_read = cail_pll_read;
1014 atom_card_info->pll_write = cail_pll_write;
1015
1016 adev->mode_info.atom_context = amdgpu_atom_parse(atom_card_info, adev->bios);
1017 if (!adev->mode_info.atom_context) {
1018 amdgpu_atombios_fini(adev);
1019 return -ENOMEM;
1020 }
1021
1022 mutex_init(&adev->mode_info.atom_context->mutex);
Alex Deuchera5bde2f2016-09-23 16:23:41 -04001023 if (adev->is_atom_fw) {
1024 amdgpu_atomfirmware_scratch_regs_init(adev);
1025 amdgpu_atomfirmware_allocate_fb_scratch(adev);
1026 } else {
1027 amdgpu_atombios_scratch_regs_init(adev);
1028 amdgpu_atombios_allocate_fb_scratch(adev);
1029 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001030 return 0;
1031}
1032
1033/* if we get transitioned to only one device, take VGA back */
1034/**
1035 * amdgpu_vga_set_decode - enable/disable vga decode
1036 *
1037 * @cookie: amdgpu_device pointer
1038 * @state: enable/disable vga decode
1039 *
1040 * Enable/disable vga decode (all asics).
1041 * Returns VGA resource flags.
1042 */
1043static unsigned int amdgpu_vga_set_decode(void *cookie, bool state)
1044{
1045 struct amdgpu_device *adev = cookie;
1046 amdgpu_asic_set_vga_state(adev, state);
1047 if (state)
1048 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
1049 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1050 else
1051 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1052}
1053
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001054static void amdgpu_check_block_size(struct amdgpu_device *adev)
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001055{
1056 /* defines number of bits in page table versus page directory,
1057 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
1058 * page table and the remaining bits are in the page directory */
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001059 if (amdgpu_vm_block_size == -1)
1060 return;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001061
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001062 if (amdgpu_vm_block_size < 9) {
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001063 dev_warn(adev->dev, "VM page table size (%d) too small\n",
1064 amdgpu_vm_block_size);
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001065 goto def_value;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001066 }
1067
1068 if (amdgpu_vm_block_size > 24 ||
1069 (amdgpu_vm_size * 1024) < (1ull << amdgpu_vm_block_size)) {
1070 dev_warn(adev->dev, "VM page table size (%d) too large\n",
1071 amdgpu_vm_block_size);
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001072 goto def_value;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001073 }
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001074
1075 return;
1076
1077def_value:
1078 amdgpu_vm_block_size = -1;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001079}
1080
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001081static void amdgpu_check_vm_size(struct amdgpu_device *adev)
1082{
Alex Deucher64dab072017-06-15 18:20:09 -04001083 /* no need to check the default value */
1084 if (amdgpu_vm_size == -1)
1085 return;
1086
Alex Deucher76117502017-06-21 12:31:41 -04001087 if (!is_power_of_2(amdgpu_vm_size)) {
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001088 dev_warn(adev->dev, "VM size (%d) must be a power of 2\n",
1089 amdgpu_vm_size);
1090 goto def_value;
1091 }
1092
1093 if (amdgpu_vm_size < 1) {
1094 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
1095 amdgpu_vm_size);
1096 goto def_value;
1097 }
1098
1099 /*
1100 * Max GPUVM size for Cayman, SI, CI VI are 40 bits.
1101 */
1102 if (amdgpu_vm_size > 1024) {
1103 dev_warn(adev->dev, "VM size (%d) too large, max is 1TB\n",
1104 amdgpu_vm_size);
1105 goto def_value;
1106 }
1107
1108 return;
1109
1110def_value:
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001111 amdgpu_vm_size = -1;
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001112}
1113
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001114/**
1115 * amdgpu_check_arguments - validate module params
1116 *
1117 * @adev: amdgpu_device pointer
1118 *
1119 * Validates certain module parameters and updates
1120 * the associated values used by the driver (all asics).
1121 */
1122static void amdgpu_check_arguments(struct amdgpu_device *adev)
1123{
Chunming Zhou5b011232015-12-10 17:34:33 +08001124 if (amdgpu_sched_jobs < 4) {
1125 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
1126 amdgpu_sched_jobs);
1127 amdgpu_sched_jobs = 4;
Alex Deucher76117502017-06-21 12:31:41 -04001128 } else if (!is_power_of_2(amdgpu_sched_jobs)){
Chunming Zhou5b011232015-12-10 17:34:33 +08001129 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
1130 amdgpu_sched_jobs);
1131 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
1132 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001133
Christian Königf9321cc2017-07-07 13:44:05 +02001134 if (amdgpu_gart_size < 32) {
1135 /* gart size must be greater or equal to 32M */
1136 dev_warn(adev->dev, "gart size (%d) too small\n",
1137 amdgpu_gart_size);
1138 amdgpu_gart_size = 32;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001139 }
1140
Christian König36d38372017-07-07 13:17:45 +02001141 if (amdgpu_gtt_size != -1 && amdgpu_gtt_size < 32) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001142 /* gtt size must be greater or equal to 32M */
Christian König36d38372017-07-07 13:17:45 +02001143 dev_warn(adev->dev, "gtt size (%d) too small\n",
1144 amdgpu_gtt_size);
1145 amdgpu_gtt_size = -1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001146 }
1147
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001148 amdgpu_check_vm_size(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001149
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001150 amdgpu_check_block_size(adev);
Christian König6a7f76e2016-08-24 15:51:49 +02001151
jimqu526bae32016-11-07 09:53:10 +08001152 if (amdgpu_vram_page_split != -1 && (amdgpu_vram_page_split < 16 ||
Alex Deucher76117502017-06-21 12:31:41 -04001153 !is_power_of_2(amdgpu_vram_page_split))) {
Christian König6a7f76e2016-08-24 15:51:49 +02001154 dev_warn(adev->dev, "invalid VRAM page split (%d)\n",
1155 amdgpu_vram_page_split);
1156 amdgpu_vram_page_split = 1024;
1157 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001158}
1159
1160/**
1161 * amdgpu_switcheroo_set_state - set switcheroo state
1162 *
1163 * @pdev: pci dev pointer
Lukas Wunner16944672015-09-05 11:17:35 +02001164 * @state: vga_switcheroo state
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001165 *
1166 * Callback for the switcheroo driver. Suspends or resumes the
1167 * the asics before or after it is powered up using ACPI methods.
1168 */
1169static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1170{
1171 struct drm_device *dev = pci_get_drvdata(pdev);
1172
1173 if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
1174 return;
1175
1176 if (state == VGA_SWITCHEROO_ON) {
Joe Perches7ca85292017-02-28 04:55:52 -08001177 pr_info("amdgpu: switched on\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001178 /* don't suspend or resume card normally */
1179 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1180
Alex Deucher810ddc32016-08-23 13:25:49 -04001181 amdgpu_device_resume(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001182
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001183 dev->switch_power_state = DRM_SWITCH_POWER_ON;
1184 drm_kms_helper_poll_enable(dev);
1185 } else {
Joe Perches7ca85292017-02-28 04:55:52 -08001186 pr_info("amdgpu: switched off\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001187 drm_kms_helper_poll_disable(dev);
1188 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Alex Deucher810ddc32016-08-23 13:25:49 -04001189 amdgpu_device_suspend(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001190 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1191 }
1192}
1193
1194/**
1195 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1196 *
1197 * @pdev: pci dev pointer
1198 *
1199 * Callback for the switcheroo driver. Check of the switcheroo
1200 * state can be changed.
1201 * Returns true if the state can be changed, false if not.
1202 */
1203static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1204{
1205 struct drm_device *dev = pci_get_drvdata(pdev);
1206
1207 /*
1208 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1209 * locking inversion with the driver load path. And the access here is
1210 * completely racy anyway. So don't bother with locking for now.
1211 */
1212 return dev->open_count == 0;
1213}
1214
1215static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1216 .set_gpu_state = amdgpu_switcheroo_set_state,
1217 .reprobe = NULL,
1218 .can_switch = amdgpu_switcheroo_can_switch,
1219};
1220
1221int amdgpu_set_clockgating_state(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001222 enum amd_ip_block_type block_type,
1223 enum amd_clockgating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001224{
1225 int i, r = 0;
1226
1227 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001228 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001229 continue;
Rex Zhuc7228652017-02-22 15:33:46 +08001230 if (adev->ip_blocks[i].version->type != block_type)
1231 continue;
1232 if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
1233 continue;
1234 r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
1235 (void *)adev, state);
1236 if (r)
1237 DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
1238 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001239 }
1240 return r;
1241}
1242
1243int amdgpu_set_powergating_state(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001244 enum amd_ip_block_type block_type,
1245 enum amd_powergating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001246{
1247 int i, r = 0;
1248
1249 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001250 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001251 continue;
Rex Zhuc7228652017-02-22 15:33:46 +08001252 if (adev->ip_blocks[i].version->type != block_type)
1253 continue;
1254 if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
1255 continue;
1256 r = adev->ip_blocks[i].version->funcs->set_powergating_state(
1257 (void *)adev, state);
1258 if (r)
1259 DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
1260 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001261 }
1262 return r;
1263}
1264
Huang Rui6cb2d4e2017-01-05 18:44:41 +08001265void amdgpu_get_clockgating_state(struct amdgpu_device *adev, u32 *flags)
1266{
1267 int i;
1268
1269 for (i = 0; i < adev->num_ip_blocks; i++) {
1270 if (!adev->ip_blocks[i].status.valid)
1271 continue;
1272 if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
1273 adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
1274 }
1275}
1276
Alex Deucher5dbbb602016-06-23 11:41:04 -04001277int amdgpu_wait_for_idle(struct amdgpu_device *adev,
1278 enum amd_ip_block_type block_type)
1279{
1280 int i, r;
1281
1282 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001283 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001284 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001285 if (adev->ip_blocks[i].version->type == block_type) {
1286 r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -04001287 if (r)
1288 return r;
1289 break;
1290 }
1291 }
1292 return 0;
1293
1294}
1295
1296bool amdgpu_is_idle(struct amdgpu_device *adev,
1297 enum amd_ip_block_type block_type)
1298{
1299 int i;
1300
1301 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001302 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001303 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001304 if (adev->ip_blocks[i].version->type == block_type)
1305 return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -04001306 }
1307 return true;
1308
1309}
1310
Alex Deuchera1255102016-10-13 17:41:13 -04001311struct amdgpu_ip_block * amdgpu_get_ip_block(struct amdgpu_device *adev,
1312 enum amd_ip_block_type type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001313{
1314 int i;
1315
1316 for (i = 0; i < adev->num_ip_blocks; i++)
Alex Deuchera1255102016-10-13 17:41:13 -04001317 if (adev->ip_blocks[i].version->type == type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001318 return &adev->ip_blocks[i];
1319
1320 return NULL;
1321}
1322
1323/**
1324 * amdgpu_ip_block_version_cmp
1325 *
1326 * @adev: amdgpu_device pointer
yanyang15fc3aee2015-05-22 14:39:35 -04001327 * @type: enum amd_ip_block_type
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001328 * @major: major version
1329 * @minor: minor version
1330 *
1331 * return 0 if equal or greater
1332 * return 1 if smaller or the ip_block doesn't exist
1333 */
1334int amdgpu_ip_block_version_cmp(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001335 enum amd_ip_block_type type,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001336 u32 major, u32 minor)
1337{
Alex Deuchera1255102016-10-13 17:41:13 -04001338 struct amdgpu_ip_block *ip_block = amdgpu_get_ip_block(adev, type);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001339
Alex Deuchera1255102016-10-13 17:41:13 -04001340 if (ip_block && ((ip_block->version->major > major) ||
1341 ((ip_block->version->major == major) &&
1342 (ip_block->version->minor >= minor))))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001343 return 0;
1344
1345 return 1;
1346}
1347
Alex Deuchera1255102016-10-13 17:41:13 -04001348/**
1349 * amdgpu_ip_block_add
1350 *
1351 * @adev: amdgpu_device pointer
1352 * @ip_block_version: pointer to the IP to add
1353 *
1354 * Adds the IP block driver information to the collection of IPs
1355 * on the asic.
1356 */
1357int amdgpu_ip_block_add(struct amdgpu_device *adev,
1358 const struct amdgpu_ip_block_version *ip_block_version)
1359{
1360 if (!ip_block_version)
1361 return -EINVAL;
1362
Huang Ruia0bae352017-05-03 09:52:06 +08001363 DRM_DEBUG("add ip block number %d <%s>\n", adev->num_ip_blocks,
1364 ip_block_version->funcs->name);
1365
Alex Deuchera1255102016-10-13 17:41:13 -04001366 adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
1367
1368 return 0;
1369}
1370
Alex Deucher483ef982016-09-30 12:43:04 -04001371static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
Emily Deng9accf2f2016-08-10 16:01:25 +08001372{
1373 adev->enable_virtual_display = false;
1374
1375 if (amdgpu_virtual_display) {
1376 struct drm_device *ddev = adev->ddev;
1377 const char *pci_address_name = pci_name(ddev->pdev);
Emily Deng0f663562016-09-30 13:02:18 -04001378 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
Emily Deng9accf2f2016-08-10 16:01:25 +08001379
1380 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1381 pciaddstr_tmp = pciaddstr;
Emily Deng0f663562016-09-30 13:02:18 -04001382 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1383 pciaddname = strsep(&pciaddname_tmp, ",");
Yintian Tao967de2a2017-01-22 15:16:51 +08001384 if (!strcmp("all", pciaddname)
1385 || !strcmp(pci_address_name, pciaddname)) {
Emily Deng0f663562016-09-30 13:02:18 -04001386 long num_crtc;
1387 int res = -1;
1388
Emily Deng9accf2f2016-08-10 16:01:25 +08001389 adev->enable_virtual_display = true;
Emily Deng0f663562016-09-30 13:02:18 -04001390
1391 if (pciaddname_tmp)
1392 res = kstrtol(pciaddname_tmp, 10,
1393 &num_crtc);
1394
1395 if (!res) {
1396 if (num_crtc < 1)
1397 num_crtc = 1;
1398 if (num_crtc > 6)
1399 num_crtc = 6;
1400 adev->mode_info.num_crtc = num_crtc;
1401 } else {
1402 adev->mode_info.num_crtc = 1;
1403 }
Emily Deng9accf2f2016-08-10 16:01:25 +08001404 break;
1405 }
1406 }
1407
Emily Deng0f663562016-09-30 13:02:18 -04001408 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
1409 amdgpu_virtual_display, pci_address_name,
1410 adev->enable_virtual_display, adev->mode_info.num_crtc);
Emily Deng9accf2f2016-08-10 16:01:25 +08001411
1412 kfree(pciaddstr);
1413 }
1414}
1415
Alex Deuchere2a75f82017-04-27 16:58:01 -04001416static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev)
1417{
Alex Deuchere2a75f82017-04-27 16:58:01 -04001418 const char *chip_name;
1419 char fw_name[30];
1420 int err;
1421 const struct gpu_info_firmware_header_v1_0 *hdr;
1422
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001423 adev->firmware.gpu_info_fw = NULL;
1424
Alex Deuchere2a75f82017-04-27 16:58:01 -04001425 switch (adev->asic_type) {
1426 case CHIP_TOPAZ:
1427 case CHIP_TONGA:
1428 case CHIP_FIJI:
1429 case CHIP_POLARIS11:
1430 case CHIP_POLARIS10:
1431 case CHIP_POLARIS12:
1432 case CHIP_CARRIZO:
1433 case CHIP_STONEY:
1434#ifdef CONFIG_DRM_AMDGPU_SI
1435 case CHIP_VERDE:
1436 case CHIP_TAHITI:
1437 case CHIP_PITCAIRN:
1438 case CHIP_OLAND:
1439 case CHIP_HAINAN:
1440#endif
1441#ifdef CONFIG_DRM_AMDGPU_CIK
1442 case CHIP_BONAIRE:
1443 case CHIP_HAWAII:
1444 case CHIP_KAVERI:
1445 case CHIP_KABINI:
1446 case CHIP_MULLINS:
1447#endif
1448 default:
1449 return 0;
1450 case CHIP_VEGA10:
1451 chip_name = "vega10";
1452 break;
Alex Deucher2d2e5e72017-05-09 12:27:35 -04001453 case CHIP_RAVEN:
1454 chip_name = "raven";
1455 break;
Alex Deuchere2a75f82017-04-27 16:58:01 -04001456 }
1457
1458 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name);
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001459 err = request_firmware(&adev->firmware.gpu_info_fw, fw_name, adev->dev);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001460 if (err) {
1461 dev_err(adev->dev,
1462 "Failed to load gpu_info firmware \"%s\"\n",
1463 fw_name);
1464 goto out;
1465 }
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001466 err = amdgpu_ucode_validate(adev->firmware.gpu_info_fw);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001467 if (err) {
1468 dev_err(adev->dev,
1469 "Failed to validate gpu_info firmware \"%s\"\n",
1470 fw_name);
1471 goto out;
1472 }
1473
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001474 hdr = (const struct gpu_info_firmware_header_v1_0 *)adev->firmware.gpu_info_fw->data;
Alex Deuchere2a75f82017-04-27 16:58:01 -04001475 amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
1476
1477 switch (hdr->version_major) {
1478 case 1:
1479 {
1480 const struct gpu_info_firmware_v1_0 *gpu_info_fw =
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001481 (const struct gpu_info_firmware_v1_0 *)(adev->firmware.gpu_info_fw->data +
Alex Deuchere2a75f82017-04-27 16:58:01 -04001482 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1483
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001484 adev->gfx.config.max_shader_engines = le32_to_cpu(gpu_info_fw->gc_num_se);
1485 adev->gfx.config.max_cu_per_sh = le32_to_cpu(gpu_info_fw->gc_num_cu_per_sh);
1486 adev->gfx.config.max_sh_per_se = le32_to_cpu(gpu_info_fw->gc_num_sh_per_se);
1487 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 -04001488 adev->gfx.config.max_texture_channel_caches =
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001489 le32_to_cpu(gpu_info_fw->gc_num_tccs);
1490 adev->gfx.config.max_gprs = le32_to_cpu(gpu_info_fw->gc_num_gprs);
1491 adev->gfx.config.max_gs_threads = le32_to_cpu(gpu_info_fw->gc_num_max_gs_thds);
1492 adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gpu_info_fw->gc_gs_table_depth);
1493 adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gpu_info_fw->gc_gsprim_buff_depth);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001494 adev->gfx.config.double_offchip_lds_buf =
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001495 le32_to_cpu(gpu_info_fw->gc_double_offchip_lds_buffer);
1496 adev->gfx.cu_info.wave_front_size = le32_to_cpu(gpu_info_fw->gc_wave_size);
Hawking Zhang51fd0372017-06-09 22:30:52 +08001497 adev->gfx.cu_info.max_waves_per_simd =
1498 le32_to_cpu(gpu_info_fw->gc_max_waves_per_simd);
1499 adev->gfx.cu_info.max_scratch_slots_per_cu =
1500 le32_to_cpu(gpu_info_fw->gc_max_scratch_slots_per_cu);
1501 adev->gfx.cu_info.lds_size = le32_to_cpu(gpu_info_fw->gc_lds_size);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001502 break;
1503 }
1504 default:
1505 dev_err(adev->dev,
1506 "Unsupported gpu_info table %d\n", hdr->header.ucode_version);
1507 err = -EINVAL;
1508 goto out;
1509 }
1510out:
Alex Deuchere2a75f82017-04-27 16:58:01 -04001511 return err;
1512}
1513
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001514static int amdgpu_early_init(struct amdgpu_device *adev)
1515{
Alex Deucheraaa36a92015-04-20 17:31:14 -04001516 int i, r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001517
Alex Deucher483ef982016-09-30 12:43:04 -04001518 amdgpu_device_enable_virtual_display(adev);
Emily Denga6be7572016-08-08 11:37:50 +08001519
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001520 switch (adev->asic_type) {
Alex Deucheraaa36a92015-04-20 17:31:14 -04001521 case CHIP_TOPAZ:
1522 case CHIP_TONGA:
David Zhang48299f92015-07-08 01:05:16 +08001523 case CHIP_FIJI:
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001524 case CHIP_POLARIS11:
1525 case CHIP_POLARIS10:
Junwei Zhangc4642a42016-12-14 15:32:28 -05001526 case CHIP_POLARIS12:
Alex Deucheraaa36a92015-04-20 17:31:14 -04001527 case CHIP_CARRIZO:
Samuel Li39bb0c92015-10-08 16:31:43 -04001528 case CHIP_STONEY:
1529 if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
Alex Deucheraaa36a92015-04-20 17:31:14 -04001530 adev->family = AMDGPU_FAMILY_CZ;
1531 else
1532 adev->family = AMDGPU_FAMILY_VI;
1533
1534 r = vi_set_ip_blocks(adev);
1535 if (r)
1536 return r;
1537 break;
Ken Wang33f34802016-01-21 17:29:41 +08001538#ifdef CONFIG_DRM_AMDGPU_SI
1539 case CHIP_VERDE:
1540 case CHIP_TAHITI:
1541 case CHIP_PITCAIRN:
1542 case CHIP_OLAND:
1543 case CHIP_HAINAN:
Ken Wang295d0da2016-05-24 21:02:53 +08001544 adev->family = AMDGPU_FAMILY_SI;
Ken Wang33f34802016-01-21 17:29:41 +08001545 r = si_set_ip_blocks(adev);
1546 if (r)
1547 return r;
1548 break;
1549#endif
Alex Deuchera2e73f52015-04-20 17:09:27 -04001550#ifdef CONFIG_DRM_AMDGPU_CIK
1551 case CHIP_BONAIRE:
1552 case CHIP_HAWAII:
1553 case CHIP_KAVERI:
1554 case CHIP_KABINI:
1555 case CHIP_MULLINS:
1556 if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
1557 adev->family = AMDGPU_FAMILY_CI;
1558 else
1559 adev->family = AMDGPU_FAMILY_KV;
1560
1561 r = cik_set_ip_blocks(adev);
1562 if (r)
1563 return r;
1564 break;
1565#endif
Chunming Zhou2ca8a5d2016-12-07 17:31:19 +08001566 case CHIP_VEGA10:
1567 case CHIP_RAVEN:
1568 if (adev->asic_type == CHIP_RAVEN)
1569 adev->family = AMDGPU_FAMILY_RV;
1570 else
1571 adev->family = AMDGPU_FAMILY_AI;
Ken Wang460826e2017-03-06 14:53:16 -05001572
1573 r = soc15_set_ip_blocks(adev);
1574 if (r)
1575 return r;
1576 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001577 default:
1578 /* FIXME: not supported yet */
1579 return -EINVAL;
1580 }
1581
Alex Deuchere2a75f82017-04-27 16:58:01 -04001582 r = amdgpu_device_parse_gpu_info_fw(adev);
1583 if (r)
1584 return r;
1585
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001586 if (amdgpu_sriov_vf(adev)) {
1587 r = amdgpu_virt_request_full_gpu(adev, true);
1588 if (r)
1589 return r;
1590 }
1591
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001592 for (i = 0; i < adev->num_ip_blocks; i++) {
1593 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
Huang Ruied8cf002017-05-03 09:40:17 +08001594 DRM_ERROR("disabled ip block: %d <%s>\n",
1595 i, adev->ip_blocks[i].version->funcs->name);
Alex Deuchera1255102016-10-13 17:41:13 -04001596 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001597 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001598 if (adev->ip_blocks[i].version->funcs->early_init) {
1599 r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001600 if (r == -ENOENT) {
Alex Deuchera1255102016-10-13 17:41:13 -04001601 adev->ip_blocks[i].status.valid = false;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001602 } else if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001603 DRM_ERROR("early_init of IP block <%s> failed %d\n",
1604 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001605 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001606 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001607 adev->ip_blocks[i].status.valid = true;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001608 }
Alex Deucher974e6b62015-07-10 13:59:44 -04001609 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001610 adev->ip_blocks[i].status.valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001611 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001612 }
1613 }
1614
Nicolai Hähnle395d1fb2016-06-02 12:32:07 +02001615 adev->cg_flags &= amdgpu_cg_mask;
1616 adev->pg_flags &= amdgpu_pg_mask;
1617
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001618 return 0;
1619}
1620
1621static int amdgpu_init(struct amdgpu_device *adev)
1622{
1623 int i, r;
1624
1625 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001626 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001627 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001628 r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001629 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001630 DRM_ERROR("sw_init of IP block <%s> failed %d\n",
1631 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001632 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001633 }
Alex Deuchera1255102016-10-13 17:41:13 -04001634 adev->ip_blocks[i].status.sw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001635 /* need to do gmc hw init early so we can allocate gpu mem */
Alex Deuchera1255102016-10-13 17:41:13 -04001636 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001637 r = amdgpu_vram_scratch_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001638 if (r) {
1639 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001640 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001641 }
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) {
1644 DRM_ERROR("hw_init %d failed %d\n", i, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001645 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001646 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001647 r = amdgpu_wb_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001648 if (r) {
1649 DRM_ERROR("amdgpu_wb_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001650 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001651 }
Alex Deuchera1255102016-10-13 17:41:13 -04001652 adev->ip_blocks[i].status.hw = true;
Monk Liu24936642017-01-09 15:54:32 +08001653
1654 /* right after GMC hw init, we create CSA */
1655 if (amdgpu_sriov_vf(adev)) {
1656 r = amdgpu_allocate_static_csa(adev);
1657 if (r) {
1658 DRM_ERROR("allocate CSA failed %d\n", r);
1659 return r;
1660 }
1661 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001662 }
1663 }
1664
1665 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001666 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001667 continue;
1668 /* gmc hw init is done early */
Alex Deuchera1255102016-10-13 17:41:13 -04001669 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001670 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001671 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001672 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001673 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
1674 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001675 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001676 }
Alex Deuchera1255102016-10-13 17:41:13 -04001677 adev->ip_blocks[i].status.hw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001678 }
1679
1680 return 0;
1681}
1682
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08001683static void amdgpu_fill_reset_magic(struct amdgpu_device *adev)
1684{
1685 memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM);
1686}
1687
1688static bool amdgpu_check_vram_lost(struct amdgpu_device *adev)
1689{
1690 return !!memcmp(adev->gart.ptr, adev->reset_magic,
1691 AMDGPU_RESET_MAGIC_NUM);
1692}
1693
Shirish S2dc80b02017-05-25 10:05:25 +05301694static int amdgpu_late_set_cg_state(struct amdgpu_device *adev)
1695{
1696 int i = 0, r;
1697
1698 for (i = 0; i < adev->num_ip_blocks; i++) {
1699 if (!adev->ip_blocks[i].status.valid)
1700 continue;
1701 /* skip CG for VCE/UVD, it's handled specially */
1702 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1703 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1704 /* enable clockgating to save power */
1705 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1706 AMD_CG_STATE_GATE);
1707 if (r) {
1708 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
1709 adev->ip_blocks[i].version->funcs->name, r);
1710 return r;
1711 }
1712 }
1713 }
1714 return 0;
1715}
1716
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001717static int amdgpu_late_init(struct amdgpu_device *adev)
1718{
1719 int i = 0, r;
1720
1721 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001722 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001723 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001724 if (adev->ip_blocks[i].version->funcs->late_init) {
1725 r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001726 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001727 DRM_ERROR("late_init of IP block <%s> failed %d\n",
1728 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001729 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001730 }
Alex Deuchera1255102016-10-13 17:41:13 -04001731 adev->ip_blocks[i].status.late_initialized = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001732 }
1733 }
1734
Shirish S2dc80b02017-05-25 10:05:25 +05301735 mod_delayed_work(system_wq, &adev->late_init_work,
1736 msecs_to_jiffies(AMDGPU_RESUME_MS));
1737
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08001738 amdgpu_fill_reset_magic(adev);
1739
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001740 return 0;
1741}
1742
1743static int amdgpu_fini(struct amdgpu_device *adev)
1744{
1745 int i, r;
1746
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001747 /* need to disable SMC first */
1748 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001749 if (!adev->ip_blocks[i].status.hw)
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001750 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001751 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001752 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
Alex Deuchera1255102016-10-13 17:41:13 -04001753 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1754 AMD_CG_STATE_UNGATE);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001755 if (r) {
1756 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001757 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001758 return r;
1759 }
Alex Deuchera1255102016-10-13 17:41:13 -04001760 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001761 /* XXX handle errors */
1762 if (r) {
1763 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001764 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001765 }
Alex Deuchera1255102016-10-13 17:41:13 -04001766 adev->ip_blocks[i].status.hw = false;
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001767 break;
1768 }
1769 }
1770
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001771 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001772 if (!adev->ip_blocks[i].status.hw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001773 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001774 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001775 amdgpu_wb_fini(adev);
1776 amdgpu_vram_scratch_fini(adev);
1777 }
Rex Zhu8201a672016-11-24 21:44:44 +08001778
1779 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1780 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1781 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
1782 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1783 AMD_CG_STATE_UNGATE);
1784 if (r) {
1785 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1786 adev->ip_blocks[i].version->funcs->name, r);
1787 return r;
1788 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001789 }
Rex Zhu8201a672016-11-24 21:44:44 +08001790
Alex Deuchera1255102016-10-13 17:41:13 -04001791 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001792 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001793 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001794 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
1795 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001796 }
Rex Zhu8201a672016-11-24 21:44:44 +08001797
Alex Deuchera1255102016-10-13 17:41:13 -04001798 adev->ip_blocks[i].status.hw = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001799 }
1800
1801 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001802 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001803 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001804 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001805 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001806 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001807 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
1808 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001809 }
Alex Deuchera1255102016-10-13 17:41:13 -04001810 adev->ip_blocks[i].status.sw = false;
1811 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001812 }
1813
Monk Liua6dcfd92016-05-19 14:36:34 +08001814 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001815 if (!adev->ip_blocks[i].status.late_initialized)
Grazvydas Ignotas8a2eef12016-10-03 00:06:44 +03001816 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001817 if (adev->ip_blocks[i].version->funcs->late_fini)
1818 adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
1819 adev->ip_blocks[i].status.late_initialized = false;
Monk Liua6dcfd92016-05-19 14:36:34 +08001820 }
1821
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001822 if (amdgpu_sriov_vf(adev)) {
Monk Liu24936642017-01-09 15:54:32 +08001823 amdgpu_bo_free_kernel(&adev->virt.csa_obj, &adev->virt.csa_vmid0_addr, NULL);
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001824 amdgpu_virt_release_full_gpu(adev, false);
1825 }
Monk Liu24936642017-01-09 15:54:32 +08001826
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001827 return 0;
1828}
1829
Shirish S2dc80b02017-05-25 10:05:25 +05301830static void amdgpu_late_init_func_handler(struct work_struct *work)
1831{
1832 struct amdgpu_device *adev =
1833 container_of(work, struct amdgpu_device, late_init_work.work);
1834 amdgpu_late_set_cg_state(adev);
1835}
1836
Alex Deucherfaefba92016-12-06 10:38:29 -05001837int amdgpu_suspend(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001838{
1839 int i, r;
1840
Xiangliang Yue941ea92017-01-18 12:47:55 +08001841 if (amdgpu_sriov_vf(adev))
1842 amdgpu_virt_request_full_gpu(adev, false);
1843
Flora Cuic5a93a22016-02-26 10:45:25 +08001844 /* ungate SMC block first */
1845 r = amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC,
1846 AMD_CG_STATE_UNGATE);
1847 if (r) {
1848 DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n",r);
1849 }
1850
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001851 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001852 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001853 continue;
1854 /* ungate blocks so that suspend can properly shut them down */
Flora Cuic5a93a22016-02-26 10:45:25 +08001855 if (i != AMD_IP_BLOCK_TYPE_SMC) {
Alex Deuchera1255102016-10-13 17:41:13 -04001856 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1857 AMD_CG_STATE_UNGATE);
Flora Cuic5a93a22016-02-26 10:45:25 +08001858 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001859 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1860 adev->ip_blocks[i].version->funcs->name, r);
Flora Cuic5a93a22016-02-26 10:45:25 +08001861 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001862 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001863 /* XXX handle errors */
Alex Deuchera1255102016-10-13 17:41:13 -04001864 r = adev->ip_blocks[i].version->funcs->suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001865 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001866 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001867 DRM_ERROR("suspend of IP block <%s> failed %d\n",
1868 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001869 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001870 }
1871
Xiangliang Yue941ea92017-01-18 12:47:55 +08001872 if (amdgpu_sriov_vf(adev))
1873 amdgpu_virt_release_full_gpu(adev, false);
1874
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001875 return 0;
1876}
1877
Monk Liue4f0fdc2017-02-09 11:55:49 +08001878static int amdgpu_sriov_reinit_early(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_GMC,
1884 AMD_IP_BLOCK_TYPE_COMMON,
Monk Liu2cb681b2017-04-26 12:00:49 +08001885 AMD_IP_BLOCK_TYPE_IH,
1886 };
Monk Liua90ad3c2017-01-23 14:22:08 +08001887
Monk Liu2cb681b2017-04-26 12:00:49 +08001888 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1889 int j;
1890 struct amdgpu_ip_block *block;
Monk Liua90ad3c2017-01-23 14:22:08 +08001891
Monk Liu2cb681b2017-04-26 12:00:49 +08001892 for (j = 0; j < adev->num_ip_blocks; j++) {
1893 block = &adev->ip_blocks[j];
1894
1895 if (block->version->type != ip_order[i] ||
1896 !block->status.valid)
1897 continue;
1898
1899 r = block->version->funcs->hw_init(adev);
1900 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
Monk Liua90ad3c2017-01-23 14:22:08 +08001901 }
1902 }
1903
1904 return 0;
1905}
1906
Monk Liue4f0fdc2017-02-09 11:55:49 +08001907static int amdgpu_sriov_reinit_late(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08001908{
1909 int i, r;
1910
Monk Liu2cb681b2017-04-26 12:00:49 +08001911 static enum amd_ip_block_type ip_order[] = {
1912 AMD_IP_BLOCK_TYPE_SMC,
1913 AMD_IP_BLOCK_TYPE_DCE,
1914 AMD_IP_BLOCK_TYPE_GFX,
1915 AMD_IP_BLOCK_TYPE_SDMA,
Frank Min257deb82017-06-15 20:07:36 +08001916 AMD_IP_BLOCK_TYPE_UVD,
1917 AMD_IP_BLOCK_TYPE_VCE
Monk Liu2cb681b2017-04-26 12:00:49 +08001918 };
Monk Liua90ad3c2017-01-23 14:22:08 +08001919
Monk Liu2cb681b2017-04-26 12:00:49 +08001920 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1921 int j;
1922 struct amdgpu_ip_block *block;
Monk Liua90ad3c2017-01-23 14:22:08 +08001923
Monk Liu2cb681b2017-04-26 12:00:49 +08001924 for (j = 0; j < adev->num_ip_blocks; j++) {
1925 block = &adev->ip_blocks[j];
1926
1927 if (block->version->type != ip_order[i] ||
1928 !block->status.valid)
1929 continue;
1930
1931 r = block->version->funcs->hw_init(adev);
1932 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
Monk Liua90ad3c2017-01-23 14:22:08 +08001933 }
1934 }
1935
1936 return 0;
1937}
1938
Chunming Zhoufcf06492017-05-05 10:33:33 +08001939static int amdgpu_resume_phase1(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001940{
1941 int i, r;
1942
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001943 for (i = 0; i < adev->num_ip_blocks; i++) {
1944 if (!adev->ip_blocks[i].status.valid)
1945 continue;
Chunming Zhoufcf06492017-05-05 10:33:33 +08001946 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
1947 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
1948 adev->ip_blocks[i].version->type ==
1949 AMD_IP_BLOCK_TYPE_IH) {
1950 r = adev->ip_blocks[i].version->funcs->resume(adev);
1951 if (r) {
1952 DRM_ERROR("resume of IP block <%s> failed %d\n",
1953 adev->ip_blocks[i].version->funcs->name, r);
1954 return r;
1955 }
1956 }
1957 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001958
Chunming Zhoufcf06492017-05-05 10:33:33 +08001959 return 0;
1960}
1961
1962static int amdgpu_resume_phase2(struct amdgpu_device *adev)
1963{
1964 int i, r;
1965
1966 for (i = 0; i < adev->num_ip_blocks; i++) {
1967 if (!adev->ip_blocks[i].status.valid)
1968 continue;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001969 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
1970 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
1971 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH )
1972 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001973 r = adev->ip_blocks[i].version->funcs->resume(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001974 if (r) {
1975 DRM_ERROR("resume of IP block <%s> failed %d\n",
1976 adev->ip_blocks[i].version->funcs->name, r);
1977 return r;
1978 }
1979 }
1980
1981 return 0;
1982}
1983
1984static int amdgpu_resume(struct amdgpu_device *adev)
1985{
Chunming Zhoufcf06492017-05-05 10:33:33 +08001986 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001987
Chunming Zhoufcf06492017-05-05 10:33:33 +08001988 r = amdgpu_resume_phase1(adev);
1989 if (r)
1990 return r;
1991 r = amdgpu_resume_phase2(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001992
Chunming Zhoufcf06492017-05-05 10:33:33 +08001993 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001994}
1995
Monk Liu4e99a442016-03-31 13:26:59 +08001996static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
Andres Rodriguez048765a2016-06-11 02:51:32 -04001997{
Alex Deuchera5bde2f2016-09-23 16:23:41 -04001998 if (adev->is_atom_fw) {
1999 if (amdgpu_atomfirmware_gpu_supports_virtualization(adev))
2000 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
2001 } else {
2002 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
2003 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
2004 }
Andres Rodriguez048765a2016-06-11 02:51:32 -04002005}
2006
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002007/**
2008 * amdgpu_device_init - initialize the driver
2009 *
2010 * @adev: amdgpu_device pointer
2011 * @pdev: drm dev pointer
2012 * @pdev: pci dev pointer
2013 * @flags: driver flags
2014 *
2015 * Initializes the driver info and hw (all asics).
2016 * Returns 0 for success or an error on failure.
2017 * Called at driver startup.
2018 */
2019int amdgpu_device_init(struct amdgpu_device *adev,
2020 struct drm_device *ddev,
2021 struct pci_dev *pdev,
2022 uint32_t flags)
2023{
2024 int r, i;
2025 bool runtime = false;
Marek Olšák95844d22016-08-17 23:49:27 +02002026 u32 max_MBps;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002027
2028 adev->shutdown = false;
2029 adev->dev = &pdev->dev;
2030 adev->ddev = ddev;
2031 adev->pdev = pdev;
2032 adev->flags = flags;
Jammy Zhou2f7d10b2015-07-22 11:29:01 +08002033 adev->asic_type = flags & AMD_ASIC_MASK;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002034 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
Christian König6f02a692017-07-07 11:56:59 +02002035 adev->mc.gart_size = 512 * 1024 * 1024;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002036 adev->accel_working = false;
2037 adev->num_rings = 0;
2038 adev->mman.buffer_funcs = NULL;
2039 adev->mman.buffer_funcs_ring = NULL;
2040 adev->vm_manager.vm_pte_funcs = NULL;
Christian König2d55e452016-02-08 17:37:38 +01002041 adev->vm_manager.vm_pte_num_rings = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002042 adev->gart.gart_funcs = NULL;
Chris Wilsonf54d1862016-10-25 13:00:45 +01002043 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002044
2045 adev->smc_rreg = &amdgpu_invalid_rreg;
2046 adev->smc_wreg = &amdgpu_invalid_wreg;
2047 adev->pcie_rreg = &amdgpu_invalid_rreg;
2048 adev->pcie_wreg = &amdgpu_invalid_wreg;
Huang Rui36b9a952016-08-31 13:23:25 +08002049 adev->pciep_rreg = &amdgpu_invalid_rreg;
2050 adev->pciep_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002051 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
2052 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
2053 adev->didt_rreg = &amdgpu_invalid_rreg;
2054 adev->didt_wreg = &amdgpu_invalid_wreg;
Rex Zhuccdbb202016-06-08 12:47:41 +08002055 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
2056 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002057 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
2058 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
2059
Rex Zhuccdbb202016-06-08 12:47:41 +08002060
Alex Deucher3e39ab92015-06-05 15:04:33 -04002061 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
2062 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
2063 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002064
2065 /* mutex initialization are all done here so we
2066 * can recall function without having locking issues */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002067 atomic_set(&adev->irq.ih.lock, 0);
Huang Rui0e5ca0d2017-03-03 18:37:23 -05002068 mutex_init(&adev->firmware.mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002069 mutex_init(&adev->pm.mutex);
2070 mutex_init(&adev->gfx.gpu_clock_mutex);
2071 mutex_init(&adev->srbm_mutex);
2072 mutex_init(&adev->grbm_idx_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002073 mutex_init(&adev->mn_lock);
2074 hash_init(adev->mn_hash);
2075
2076 amdgpu_check_arguments(adev);
2077
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002078 spin_lock_init(&adev->mmio_idx_lock);
2079 spin_lock_init(&adev->smc_idx_lock);
2080 spin_lock_init(&adev->pcie_idx_lock);
2081 spin_lock_init(&adev->uvd_ctx_idx_lock);
2082 spin_lock_init(&adev->didt_idx_lock);
Rex Zhuccdbb202016-06-08 12:47:41 +08002083 spin_lock_init(&adev->gc_cac_idx_lock);
Evan Quan16abb5d2017-07-04 09:21:50 +08002084 spin_lock_init(&adev->se_cac_idx_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002085 spin_lock_init(&adev->audio_endpt_idx_lock);
Marek Olšák95844d22016-08-17 23:49:27 +02002086 spin_lock_init(&adev->mm_stats.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002087
Chunming Zhou0c4e7fa2016-08-17 11:41:30 +08002088 INIT_LIST_HEAD(&adev->shadow_list);
2089 mutex_init(&adev->shadow_list_lock);
2090
Chunming Zhou5c1354b2016-08-30 16:13:10 +08002091 INIT_LIST_HEAD(&adev->gtt_list);
2092 spin_lock_init(&adev->gtt_list_lock);
2093
Andres Rodriguez795f2812017-03-06 16:27:55 -05002094 INIT_LIST_HEAD(&adev->ring_lru_list);
2095 spin_lock_init(&adev->ring_lru_list_lock);
2096
Shirish S2dc80b02017-05-25 10:05:25 +05302097 INIT_DELAYED_WORK(&adev->late_init_work, amdgpu_late_init_func_handler);
2098
Alex Xie0fa49552017-06-08 14:58:05 -04002099 /* Registers mapping */
2100 /* TODO: block userspace mapping of io register */
Ken Wangda69c1612016-01-21 19:08:55 +08002101 if (adev->asic_type >= CHIP_BONAIRE) {
2102 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
2103 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
2104 } else {
2105 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
2106 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
2107 }
Chunming Zhou5c1354b2016-08-30 16:13:10 +08002108
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002109 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
2110 if (adev->rmmio == NULL) {
2111 return -ENOMEM;
2112 }
2113 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
2114 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
2115
Ken Wangda69c1612016-01-21 19:08:55 +08002116 if (adev->asic_type >= CHIP_BONAIRE)
2117 /* doorbell bar mapping */
2118 amdgpu_doorbell_init(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002119
2120 /* io port mapping */
2121 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
2122 if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
2123 adev->rio_mem_size = pci_resource_len(adev->pdev, i);
2124 adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
2125 break;
2126 }
2127 }
2128 if (adev->rio_mem == NULL)
Amber Linb64a18c2017-01-04 08:06:58 -05002129 DRM_INFO("PCI I/O BAR is not found.\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002130
2131 /* early init functions */
2132 r = amdgpu_early_init(adev);
2133 if (r)
2134 return r;
2135
2136 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
2137 /* this will fail for cards that aren't VGA class devices, just
2138 * ignore it */
2139 vga_client_register(adev->pdev, adev, NULL, amdgpu_vga_set_decode);
2140
2141 if (amdgpu_runtime_pm == 1)
2142 runtime = true;
Alex Deuchere9bef452016-04-25 13:12:18 -04002143 if (amdgpu_device_is_px(ddev))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002144 runtime = true;
Lukas Wunner84c8b222017-03-10 21:23:45 +01002145 if (!pci_is_thunderbolt_attached(adev->pdev))
2146 vga_switcheroo_register_client(adev->pdev,
2147 &amdgpu_switcheroo_ops, runtime);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002148 if (runtime)
2149 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
2150
2151 /* Read BIOS */
Alex Deucher83ba1262016-06-03 18:21:41 -04002152 if (!amdgpu_get_bios(adev)) {
2153 r = -EINVAL;
2154 goto failed;
2155 }
Nils Wallméniusf7e9e9f2016-12-14 21:52:45 +01002156
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002157 r = amdgpu_atombios_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002158 if (r) {
2159 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
Gavin Wan89041942017-06-23 13:55:15 -04002160 amdgpu_vf_error_put(AMDGIM_ERROR_VF_ATOMBIOS_INIT_FAIL, 0, 0);
Alex Deucher83ba1262016-06-03 18:21:41 -04002161 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002162 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002163
Monk Liu4e99a442016-03-31 13:26:59 +08002164 /* detect if we are with an SRIOV vbios */
2165 amdgpu_device_detect_sriov_bios(adev);
Andres Rodriguez048765a2016-06-11 02:51:32 -04002166
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002167 /* Post card if necessary */
Monk Liubec86372016-09-14 19:38:08 +08002168 if (amdgpu_vpost_needed(adev)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002169 if (!adev->bios) {
Monk Liubec86372016-09-14 19:38:08 +08002170 dev_err(adev->dev, "no vBIOS found\n");
Gavin Wan89041942017-06-23 13:55:15 -04002171 amdgpu_vf_error_put(AMDGIM_ERROR_VF_NO_VBIOS, 0, 0);
Alex Deucher83ba1262016-06-03 18:21:41 -04002172 r = -EINVAL;
2173 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002174 }
Monk Liubec86372016-09-14 19:38:08 +08002175 DRM_INFO("GPU posting now...\n");
Monk Liu4e99a442016-03-31 13:26:59 +08002176 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2177 if (r) {
2178 dev_err(adev->dev, "gpu post error!\n");
Gavin Wan89041942017-06-23 13:55:15 -04002179 amdgpu_vf_error_put(AMDGIM_ERROR_VF_GPU_POST_ERROR, 0, 0);
Monk Liu4e99a442016-03-31 13:26:59 +08002180 goto failed;
2181 }
2182 } else {
2183 DRM_INFO("GPU post is not needed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002184 }
2185
Alex Deucher88b64e92017-07-10 10:43:10 -04002186 if (adev->is_atom_fw) {
2187 /* Initialize clocks */
2188 r = amdgpu_atomfirmware_get_clock_info(adev);
2189 if (r) {
2190 dev_err(adev->dev, "amdgpu_atomfirmware_get_clock_info failed\n");
2191 amdgpu_vf_error_put(AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
2192 goto failed;
2193 }
2194 } else {
Alex Deuchera5bde2f2016-09-23 16:23:41 -04002195 /* Initialize clocks */
2196 r = amdgpu_atombios_get_clock_info(adev);
2197 if (r) {
2198 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
Gavin Wan89041942017-06-23 13:55:15 -04002199 amdgpu_vf_error_put(AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
2200 goto failed;
Alex Deuchera5bde2f2016-09-23 16:23:41 -04002201 }
2202 /* init i2c buses */
2203 amdgpu_atombios_i2c_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002204 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002205
2206 /* Fence driver */
2207 r = amdgpu_fence_driver_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002208 if (r) {
2209 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
Gavin Wan89041942017-06-23 13:55:15 -04002210 amdgpu_vf_error_put(AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0);
Alex Deucher83ba1262016-06-03 18:21:41 -04002211 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002212 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002213
2214 /* init the mode config */
2215 drm_mode_config_init(adev->ddev);
2216
2217 r = amdgpu_init(adev);
2218 if (r) {
Alex Deucher2c1a2782015-12-07 17:02:53 -05002219 dev_err(adev->dev, "amdgpu_init failed\n");
Gavin Wan89041942017-06-23 13:55:15 -04002220 amdgpu_vf_error_put(AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002221 amdgpu_fini(adev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002222 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002223 }
2224
2225 adev->accel_working = true;
2226
Alex Xiee59c0202017-06-01 09:42:59 -04002227 amdgpu_vm_check_compute_bug(adev);
2228
Marek Olšák95844d22016-08-17 23:49:27 +02002229 /* Initialize the buffer migration limit. */
2230 if (amdgpu_moverate >= 0)
2231 max_MBps = amdgpu_moverate;
2232 else
2233 max_MBps = 8; /* Allow 8 MB/s. */
2234 /* Get a log2 for easy divisions. */
2235 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
2236
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002237 r = amdgpu_ib_pool_init(adev);
2238 if (r) {
2239 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
Gavin Wan89041942017-06-23 13:55:15 -04002240 amdgpu_vf_error_put(AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r);
Alex Deucher83ba1262016-06-03 18:21:41 -04002241 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002242 }
2243
2244 r = amdgpu_ib_ring_tests(adev);
2245 if (r)
2246 DRM_ERROR("ib ring test failed (%d).\n", r);
2247
Monk Liu9bc92b92017-02-08 17:38:13 +08002248 amdgpu_fbdev_init(adev);
2249
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002250 r = amdgpu_gem_debugfs_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002251 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002252 DRM_ERROR("registering gem debugfs failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002253
2254 r = amdgpu_debugfs_regs_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002255 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002256 DRM_ERROR("registering register debugfs failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002257
Huang Rui4f0955f2017-05-10 23:04:06 +08002258 r = amdgpu_debugfs_test_ib_ring_init(adev);
2259 if (r)
2260 DRM_ERROR("registering register test ib ring debugfs failed (%d).\n", r);
2261
Huang Rui50ab2532016-06-12 15:51:09 +08002262 r = amdgpu_debugfs_firmware_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002263 if (r)
Huang Rui50ab2532016-06-12 15:51:09 +08002264 DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
Huang Rui50ab2532016-06-12 15:51:09 +08002265
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002266 if ((amdgpu_testing & 1)) {
2267 if (adev->accel_working)
2268 amdgpu_test_moves(adev);
2269 else
2270 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
2271 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002272 if (amdgpu_benchmarking) {
2273 if (adev->accel_working)
2274 amdgpu_benchmark(adev, amdgpu_benchmarking);
2275 else
2276 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
2277 }
2278
2279 /* enable clockgating, etc. after ib tests, etc. since some blocks require
2280 * explicit gating rather than handling it automatically.
2281 */
2282 r = amdgpu_late_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002283 if (r) {
2284 dev_err(adev->dev, "amdgpu_late_init failed\n");
Gavin Wan89041942017-06-23 13:55:15 -04002285 amdgpu_vf_error_put(AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r);
Alex Deucher83ba1262016-06-03 18:21:41 -04002286 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002287 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002288
2289 return 0;
Alex Deucher83ba1262016-06-03 18:21:41 -04002290
2291failed:
Gavin Wan89041942017-06-23 13:55:15 -04002292 amdgpu_vf_error_trans_all(adev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002293 if (runtime)
2294 vga_switcheroo_fini_domain_pm_ops(adev->dev);
2295 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002296}
2297
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002298/**
2299 * amdgpu_device_fini - tear down the driver
2300 *
2301 * @adev: amdgpu_device pointer
2302 *
2303 * Tear down the driver info (all asics).
2304 * Called at driver shutdown.
2305 */
2306void amdgpu_device_fini(struct amdgpu_device *adev)
2307{
2308 int r;
2309
2310 DRM_INFO("amdgpu: finishing device.\n");
2311 adev->shutdown = true;
Pixel Dingdb2c2a92017-04-25 16:47:42 +08002312 if (adev->mode_info.mode_config_initialized)
2313 drm_crtc_force_disable_all(adev->ddev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002314 /* evict vram memory */
2315 amdgpu_bo_evict_vram(adev);
2316 amdgpu_ib_pool_fini(adev);
2317 amdgpu_fence_driver_fini(adev);
2318 amdgpu_fbdev_fini(adev);
2319 r = amdgpu_fini(adev);
Huang Ruiab4fe3e2017-06-05 22:11:59 +08002320 if (adev->firmware.gpu_info_fw) {
2321 release_firmware(adev->firmware.gpu_info_fw);
2322 adev->firmware.gpu_info_fw = NULL;
2323 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002324 adev->accel_working = false;
Shirish S2dc80b02017-05-25 10:05:25 +05302325 cancel_delayed_work_sync(&adev->late_init_work);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002326 /* free i2c buses */
2327 amdgpu_i2c_fini(adev);
2328 amdgpu_atombios_fini(adev);
2329 kfree(adev->bios);
2330 adev->bios = NULL;
Lukas Wunner84c8b222017-03-10 21:23:45 +01002331 if (!pci_is_thunderbolt_attached(adev->pdev))
2332 vga_switcheroo_unregister_client(adev->pdev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002333 if (adev->flags & AMD_IS_PX)
2334 vga_switcheroo_fini_domain_pm_ops(adev->dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002335 vga_client_register(adev->pdev, NULL, NULL, NULL);
2336 if (adev->rio_mem)
2337 pci_iounmap(adev->pdev, adev->rio_mem);
2338 adev->rio_mem = NULL;
2339 iounmap(adev->rmmio);
2340 adev->rmmio = NULL;
Ken Wangda69c1612016-01-21 19:08:55 +08002341 if (adev->asic_type >= CHIP_BONAIRE)
2342 amdgpu_doorbell_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002343 amdgpu_debugfs_regs_cleanup(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002344}
2345
2346
2347/*
2348 * Suspend & resume.
2349 */
2350/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002351 * amdgpu_device_suspend - initiate device suspend
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002352 *
2353 * @pdev: drm dev pointer
2354 * @state: suspend state
2355 *
2356 * Puts the hw in the suspend state (all asics).
2357 * Returns 0 for success or an error on failure.
2358 * Called at driver suspend.
2359 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002360int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002361{
2362 struct amdgpu_device *adev;
2363 struct drm_crtc *crtc;
2364 struct drm_connector *connector;
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002365 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002366
2367 if (dev == NULL || dev->dev_private == NULL) {
2368 return -ENODEV;
2369 }
2370
2371 adev = dev->dev_private;
2372
2373 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2374 return 0;
2375
2376 drm_kms_helper_poll_disable(dev);
2377
2378 /* turn off display hw */
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002379 drm_modeset_lock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002380 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2381 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
2382 }
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002383 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002384
Yong Zhaoba997702015-11-09 17:21:45 -05002385 amdgpu_amdkfd_suspend(adev);
2386
Alex Deucher756e6882015-10-08 00:03:36 -04002387 /* unpin the front buffers and cursors */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002388 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Alex Deucher756e6882015-10-08 00:03:36 -04002389 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002390 struct amdgpu_framebuffer *rfb = to_amdgpu_framebuffer(crtc->primary->fb);
2391 struct amdgpu_bo *robj;
2392
Alex Deucher756e6882015-10-08 00:03:36 -04002393 if (amdgpu_crtc->cursor_bo) {
2394 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
Alex Xie7a6901d2017-04-24 13:52:41 -04002395 r = amdgpu_bo_reserve(aobj, true);
Alex Deucher756e6882015-10-08 00:03:36 -04002396 if (r == 0) {
2397 amdgpu_bo_unpin(aobj);
2398 amdgpu_bo_unreserve(aobj);
2399 }
2400 }
2401
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002402 if (rfb == NULL || rfb->obj == NULL) {
2403 continue;
2404 }
2405 robj = gem_to_amdgpu_bo(rfb->obj);
2406 /* don't unpin kernel fb objects */
2407 if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
Alex Xie7a6901d2017-04-24 13:52:41 -04002408 r = amdgpu_bo_reserve(robj, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002409 if (r == 0) {
2410 amdgpu_bo_unpin(robj);
2411 amdgpu_bo_unreserve(robj);
2412 }
2413 }
2414 }
2415 /* evict vram memory */
2416 amdgpu_bo_evict_vram(adev);
2417
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002418 amdgpu_fence_driver_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002419
2420 r = amdgpu_suspend(adev);
2421
Alex Deuchera0a71e42016-10-10 12:41:36 -04002422 /* evict remaining vram memory
2423 * This second call to evict vram is to evict the gart page table
2424 * using the CPU.
2425 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002426 amdgpu_bo_evict_vram(adev);
2427
Alex Deucherd05da0e2017-06-30 17:08:45 -04002428 amdgpu_atombios_scratch_regs_save(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002429 pci_save_state(dev->pdev);
2430 if (suspend) {
2431 /* Shut down the device */
2432 pci_disable_device(dev->pdev);
2433 pci_set_power_state(dev->pdev, PCI_D3hot);
jimqu74b0b152016-09-07 17:09:12 +08002434 } else {
2435 r = amdgpu_asic_reset(adev);
2436 if (r)
2437 DRM_ERROR("amdgpu asic reset failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002438 }
2439
2440 if (fbcon) {
2441 console_lock();
2442 amdgpu_fbdev_set_suspend(adev, 1);
2443 console_unlock();
2444 }
2445 return 0;
2446}
2447
2448/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002449 * amdgpu_device_resume - initiate device resume
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002450 *
2451 * @pdev: drm dev pointer
2452 *
2453 * Bring the hw back to operating state (all asics).
2454 * Returns 0 for success or an error on failure.
2455 * Called at driver resume.
2456 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002457int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002458{
2459 struct drm_connector *connector;
2460 struct amdgpu_device *adev = dev->dev_private;
Alex Deucher756e6882015-10-08 00:03:36 -04002461 struct drm_crtc *crtc;
Huang Rui03161a62017-04-13 16:12:26 +08002462 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002463
2464 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2465 return 0;
2466
jimqu74b0b152016-09-07 17:09:12 +08002467 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002468 console_lock();
jimqu74b0b152016-09-07 17:09:12 +08002469
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002470 if (resume) {
2471 pci_set_power_state(dev->pdev, PCI_D0);
2472 pci_restore_state(dev->pdev);
jimqu74b0b152016-09-07 17:09:12 +08002473 r = pci_enable_device(dev->pdev);
Huang Rui03161a62017-04-13 16:12:26 +08002474 if (r)
2475 goto unlock;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002476 }
Alex Deucherd05da0e2017-06-30 17:08:45 -04002477 amdgpu_atombios_scratch_regs_restore(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002478
2479 /* post card */
Jim Quc836fec2017-02-10 15:59:59 +08002480 if (amdgpu_need_post(adev)) {
jimqu74b0b152016-09-07 17:09:12 +08002481 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2482 if (r)
2483 DRM_ERROR("amdgpu asic init failed\n");
2484 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002485
2486 r = amdgpu_resume(adev);
Rex Zhue6707212017-03-30 13:21:01 +08002487 if (r) {
Flora Cuica198522016-02-04 15:10:08 +08002488 DRM_ERROR("amdgpu_resume failed (%d).\n", r);
Huang Rui03161a62017-04-13 16:12:26 +08002489 goto unlock;
Rex Zhue6707212017-03-30 13:21:01 +08002490 }
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002491 amdgpu_fence_driver_resume(adev);
2492
Flora Cuica198522016-02-04 15:10:08 +08002493 if (resume) {
2494 r = amdgpu_ib_ring_tests(adev);
2495 if (r)
2496 DRM_ERROR("ib ring test failed (%d).\n", r);
2497 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002498
2499 r = amdgpu_late_init(adev);
Huang Rui03161a62017-04-13 16:12:26 +08002500 if (r)
2501 goto unlock;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002502
Alex Deucher756e6882015-10-08 00:03:36 -04002503 /* pin cursors */
2504 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2505 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2506
2507 if (amdgpu_crtc->cursor_bo) {
2508 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
Alex Xie7a6901d2017-04-24 13:52:41 -04002509 r = amdgpu_bo_reserve(aobj, true);
Alex Deucher756e6882015-10-08 00:03:36 -04002510 if (r == 0) {
2511 r = amdgpu_bo_pin(aobj,
2512 AMDGPU_GEM_DOMAIN_VRAM,
2513 &amdgpu_crtc->cursor_addr);
2514 if (r != 0)
2515 DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
2516 amdgpu_bo_unreserve(aobj);
2517 }
2518 }
2519 }
Yong Zhaoba997702015-11-09 17:21:45 -05002520 r = amdgpu_amdkfd_resume(adev);
2521 if (r)
2522 return r;
Alex Deucher756e6882015-10-08 00:03:36 -04002523
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002524 /* blat the mode back in */
2525 if (fbcon) {
2526 drm_helper_resume_force_mode(dev);
2527 /* turn on display hw */
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002528 drm_modeset_lock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002529 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2530 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
2531 }
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002532 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002533 }
2534
2535 drm_kms_helper_poll_enable(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002536
2537 /*
2538 * Most of the connector probing functions try to acquire runtime pm
2539 * refs to ensure that the GPU is powered on when connector polling is
2540 * performed. Since we're calling this from a runtime PM callback,
2541 * trying to acquire rpm refs will cause us to deadlock.
2542 *
2543 * Since we're guaranteed to be holding the rpm lock, it's safe to
2544 * temporarily disable the rpm helpers so this doesn't deadlock us.
2545 */
2546#ifdef CONFIG_PM
2547 dev->dev->power.disable_depth++;
2548#endif
Alex Deucher54fb2a52015-11-24 14:30:56 -05002549 drm_helper_hpd_irq_event(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002550#ifdef CONFIG_PM
2551 dev->dev->power.disable_depth--;
2552#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002553
Huang Rui03161a62017-04-13 16:12:26 +08002554 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002555 amdgpu_fbdev_set_suspend(adev, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002556
Huang Rui03161a62017-04-13 16:12:26 +08002557unlock:
2558 if (fbcon)
2559 console_unlock();
2560
2561 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002562}
2563
Chunming Zhou63fbf422016-07-15 11:19:20 +08002564static bool amdgpu_check_soft_reset(struct amdgpu_device *adev)
2565{
2566 int i;
2567 bool asic_hang = false;
2568
2569 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002570 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou63fbf422016-07-15 11:19:20 +08002571 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002572 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
2573 adev->ip_blocks[i].status.hang =
2574 adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
2575 if (adev->ip_blocks[i].status.hang) {
2576 DRM_INFO("IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
Chunming Zhou63fbf422016-07-15 11:19:20 +08002577 asic_hang = true;
2578 }
2579 }
2580 return asic_hang;
2581}
2582
Baoyou Xie4d446652016-09-18 22:09:35 +08002583static int amdgpu_pre_soft_reset(struct amdgpu_device *adev)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002584{
2585 int i, r = 0;
2586
2587 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002588 if (!adev->ip_blocks[i].status.valid)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002589 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002590 if (adev->ip_blocks[i].status.hang &&
2591 adev->ip_blocks[i].version->funcs->pre_soft_reset) {
2592 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
Chunming Zhoud31a5012016-07-18 10:04:34 +08002593 if (r)
2594 return r;
2595 }
2596 }
2597
2598 return 0;
2599}
2600
Chunming Zhou35d782f2016-07-15 15:57:13 +08002601static bool amdgpu_need_full_reset(struct amdgpu_device *adev)
2602{
Alex Deucherda146d32016-10-13 16:07:03 -04002603 int i;
2604
2605 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002606 if (!adev->ip_blocks[i].status.valid)
Alex Deucherda146d32016-10-13 16:07:03 -04002607 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002608 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
2609 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
2610 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
2611 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE)) {
2612 if (adev->ip_blocks[i].status.hang) {
Alex Deucherda146d32016-10-13 16:07:03 -04002613 DRM_INFO("Some block need full reset!\n");
2614 return true;
2615 }
2616 }
Chunming Zhou35d782f2016-07-15 15:57:13 +08002617 }
2618 return false;
2619}
2620
2621static int amdgpu_soft_reset(struct amdgpu_device *adev)
2622{
2623 int i, r = 0;
2624
2625 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002626 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002627 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002628 if (adev->ip_blocks[i].status.hang &&
2629 adev->ip_blocks[i].version->funcs->soft_reset) {
2630 r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002631 if (r)
2632 return r;
2633 }
2634 }
2635
2636 return 0;
2637}
2638
2639static int amdgpu_post_soft_reset(struct amdgpu_device *adev)
2640{
2641 int i, r = 0;
2642
2643 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002644 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002645 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002646 if (adev->ip_blocks[i].status.hang &&
2647 adev->ip_blocks[i].version->funcs->post_soft_reset)
2648 r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002649 if (r)
2650 return r;
2651 }
2652
2653 return 0;
2654}
2655
Chunming Zhou3ad81f12016-08-05 17:30:17 +08002656bool amdgpu_need_backup(struct amdgpu_device *adev)
2657{
2658 if (adev->flags & AMD_IS_APU)
2659 return false;
2660
2661 return amdgpu_lockup_timeout > 0 ? true : false;
2662}
2663
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002664static int amdgpu_recover_vram_from_shadow(struct amdgpu_device *adev,
2665 struct amdgpu_ring *ring,
2666 struct amdgpu_bo *bo,
Chris Wilsonf54d1862016-10-25 13:00:45 +01002667 struct dma_fence **fence)
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002668{
2669 uint32_t domain;
2670 int r;
2671
Roger.He23d2e502017-04-21 14:24:26 +08002672 if (!bo->shadow)
2673 return 0;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002674
Alex Xie1d284792017-04-24 13:53:04 -04002675 r = amdgpu_bo_reserve(bo, true);
Roger.He23d2e502017-04-21 14:24:26 +08002676 if (r)
2677 return r;
2678 domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
2679 /* if bo has been evicted, then no need to recover */
2680 if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
Roger.He82521312017-04-21 13:08:43 +08002681 r = amdgpu_bo_validate(bo->shadow);
2682 if (r) {
2683 DRM_ERROR("bo validate failed!\n");
2684 goto err;
2685 }
2686
2687 r = amdgpu_ttm_bind(&bo->shadow->tbo, &bo->shadow->tbo.mem);
2688 if (r) {
2689 DRM_ERROR("%p bind failed\n", bo->shadow);
2690 goto err;
2691 }
2692
Roger.He23d2e502017-04-21 14:24:26 +08002693 r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002694 NULL, fence, true);
Roger.He23d2e502017-04-21 14:24:26 +08002695 if (r) {
2696 DRM_ERROR("recover page table failed!\n");
2697 goto err;
2698 }
2699 }
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002700err:
Roger.He23d2e502017-04-21 14:24:26 +08002701 amdgpu_bo_unreserve(bo);
2702 return r;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002703}
2704
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002705/**
Monk Liua90ad3c2017-01-23 14:22:08 +08002706 * amdgpu_sriov_gpu_reset - reset the asic
2707 *
2708 * @adev: amdgpu device pointer
Monk Liu7225f872017-04-26 14:51:54 +08002709 * @job: which job trigger hang
Monk Liua90ad3c2017-01-23 14:22:08 +08002710 *
2711 * Attempt the reset the GPU if it has hung (all asics).
2712 * for SRIOV case.
2713 * Returns 0 for success or an error on failure.
2714 */
Monk Liu7225f872017-04-26 14:51:54 +08002715int amdgpu_sriov_gpu_reset(struct amdgpu_device *adev, struct amdgpu_job *job)
Monk Liua90ad3c2017-01-23 14:22:08 +08002716{
Monk Liu65781c72017-05-11 13:36:44 +08002717 int i, j, r = 0;
Monk Liua90ad3c2017-01-23 14:22:08 +08002718 int resched;
2719 struct amdgpu_bo *bo, *tmp;
2720 struct amdgpu_ring *ring;
2721 struct dma_fence *fence = NULL, *next = NULL;
2722
Monk Liu147b5982017-01-25 15:48:01 +08002723 mutex_lock(&adev->virt.lock_reset);
Monk Liua90ad3c2017-01-23 14:22:08 +08002724 atomic_inc(&adev->gpu_reset_counter);
Monk Liu1fb37a32017-01-26 15:36:37 +08002725 adev->gfx.in_reset = true;
Monk Liua90ad3c2017-01-23 14:22:08 +08002726
2727 /* block TTM */
2728 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
2729
Monk Liu65781c72017-05-11 13:36:44 +08002730 /* we start from the ring trigger GPU hang */
2731 j = job ? job->ring->idx : 0;
Monk Liua90ad3c2017-01-23 14:22:08 +08002732
Monk Liu65781c72017-05-11 13:36:44 +08002733 /* block scheduler */
2734 for (i = j; i < j + AMDGPU_MAX_RINGS; ++i) {
2735 ring = adev->rings[i % AMDGPU_MAX_RINGS];
Monk Liua90ad3c2017-01-23 14:22:08 +08002736 if (!ring || !ring->sched.thread)
2737 continue;
2738
2739 kthread_park(ring->sched.thread);
Monk Liua90ad3c2017-01-23 14:22:08 +08002740
Monk Liu65781c72017-05-11 13:36:44 +08002741 if (job && j != i)
2742 continue;
2743
Monk Liu4f059ec2017-05-11 13:59:15 +08002744 /* here give the last chance to check if job removed from mirror-list
Monk Liu65781c72017-05-11 13:36:44 +08002745 * since we already pay some time on kthread_park */
Monk Liu4f059ec2017-05-11 13:59:15 +08002746 if (job && list_empty(&job->base.node)) {
Monk Liu65781c72017-05-11 13:36:44 +08002747 kthread_unpark(ring->sched.thread);
2748 goto give_up_reset;
2749 }
2750
2751 if (amd_sched_invalidate_job(&job->base, amdgpu_job_hang_limit))
2752 amd_sched_job_kickout(&job->base);
2753
2754 /* only do job_reset on the hang ring if @job not NULL */
Monk Liua90ad3c2017-01-23 14:22:08 +08002755 amd_sched_hw_job_reset(&ring->sched);
Monk Liu65781c72017-05-11 13:36:44 +08002756
2757 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
2758 amdgpu_fence_driver_force_completion_ring(ring);
Monk Liua90ad3c2017-01-23 14:22:08 +08002759 }
2760
Monk Liua90ad3c2017-01-23 14:22:08 +08002761 /* request to take full control of GPU before re-initialization */
Monk Liu7225f872017-04-26 14:51:54 +08002762 if (job)
Monk Liua90ad3c2017-01-23 14:22:08 +08002763 amdgpu_virt_reset_gpu(adev);
2764 else
2765 amdgpu_virt_request_full_gpu(adev, true);
2766
2767
2768 /* Resume IP prior to SMC */
Monk Liue4f0fdc2017-02-09 11:55:49 +08002769 amdgpu_sriov_reinit_early(adev);
Monk Liua90ad3c2017-01-23 14:22:08 +08002770
2771 /* we need recover gart prior to run SMC/CP/SDMA resume */
2772 amdgpu_ttm_recover_gart(adev);
2773
2774 /* now we are okay to resume SMC/CP/SDMA */
Monk Liue4f0fdc2017-02-09 11:55:49 +08002775 amdgpu_sriov_reinit_late(adev);
Monk Liua90ad3c2017-01-23 14:22:08 +08002776
2777 amdgpu_irq_gpu_reset_resume_helper(adev);
2778
2779 if (amdgpu_ib_ring_tests(adev))
2780 dev_err(adev->dev, "[GPU_RESET] ib ring test failed (%d).\n", r);
2781
2782 /* release full control of GPU after ib test */
2783 amdgpu_virt_release_full_gpu(adev, true);
2784
2785 DRM_INFO("recover vram bo from shadow\n");
2786
2787 ring = adev->mman.buffer_funcs_ring;
2788 mutex_lock(&adev->shadow_list_lock);
2789 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
Monk Liu236763d2017-05-01 16:15:31 +08002790 next = NULL;
Monk Liua90ad3c2017-01-23 14:22:08 +08002791 amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
2792 if (fence) {
2793 r = dma_fence_wait(fence, false);
2794 if (r) {
2795 WARN(r, "recovery from shadow isn't completed\n");
2796 break;
2797 }
2798 }
2799
2800 dma_fence_put(fence);
2801 fence = next;
2802 }
2803 mutex_unlock(&adev->shadow_list_lock);
2804
2805 if (fence) {
2806 r = dma_fence_wait(fence, false);
2807 if (r)
2808 WARN(r, "recovery from shadow isn't completed\n");
2809 }
2810 dma_fence_put(fence);
2811
Monk Liu65781c72017-05-11 13:36:44 +08002812 for (i = j; i < j + AMDGPU_MAX_RINGS; ++i) {
2813 ring = adev->rings[i % AMDGPU_MAX_RINGS];
Monk Liua90ad3c2017-01-23 14:22:08 +08002814 if (!ring || !ring->sched.thread)
2815 continue;
2816
Monk Liu65781c72017-05-11 13:36:44 +08002817 if (job && j != i) {
2818 kthread_unpark(ring->sched.thread);
2819 continue;
2820 }
2821
Monk Liua90ad3c2017-01-23 14:22:08 +08002822 amd_sched_job_recovery(&ring->sched);
2823 kthread_unpark(ring->sched.thread);
2824 }
2825
2826 drm_helper_resume_force_mode(adev->ddev);
Monk Liu65781c72017-05-11 13:36:44 +08002827give_up_reset:
Monk Liua90ad3c2017-01-23 14:22:08 +08002828 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
2829 if (r) {
2830 /* bad news, how to tell it to userspace ? */
2831 dev_info(adev->dev, "GPU reset failed\n");
Monk Liu65781c72017-05-11 13:36:44 +08002832 } else {
2833 dev_info(adev->dev, "GPU reset successed!\n");
Monk Liua90ad3c2017-01-23 14:22:08 +08002834 }
2835
Monk Liu1fb37a32017-01-26 15:36:37 +08002836 adev->gfx.in_reset = false;
Monk Liu147b5982017-01-25 15:48:01 +08002837 mutex_unlock(&adev->virt.lock_reset);
Monk Liua90ad3c2017-01-23 14:22:08 +08002838 return r;
2839}
2840
2841/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002842 * amdgpu_gpu_reset - reset the asic
2843 *
2844 * @adev: amdgpu device pointer
2845 *
2846 * Attempt the reset the GPU if it has hung (all asics).
2847 * Returns 0 for success or an error on failure.
2848 */
2849int amdgpu_gpu_reset(struct amdgpu_device *adev)
2850{
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002851 int i, r;
2852 int resched;
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002853 bool need_full_reset, vram_lost = false;
Xiangliang Yufb140b22016-12-17 22:48:57 +08002854
Chunming Zhou63fbf422016-07-15 11:19:20 +08002855 if (!amdgpu_check_soft_reset(adev)) {
2856 DRM_INFO("No hardware hang detected. Did some blocks stall?\n");
2857 return 0;
2858 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002859
Marek Olšákd94aed52015-05-05 21:13:49 +02002860 atomic_inc(&adev->gpu_reset_counter);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002861
Chunming Zhoua3c47d62016-06-30 16:44:41 +08002862 /* block TTM */
2863 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
2864
Chunming Zhou0875dc92016-06-12 15:41:58 +08002865 /* block scheduler */
2866 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2867 struct amdgpu_ring *ring = adev->rings[i];
2868
Chunming Zhou51687752017-04-24 17:09:15 +08002869 if (!ring || !ring->sched.thread)
Chunming Zhou0875dc92016-06-12 15:41:58 +08002870 continue;
2871 kthread_park(ring->sched.thread);
Chunming Zhouaa1c8902016-06-30 13:56:02 +08002872 amd_sched_hw_job_reset(&ring->sched);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002873 }
Chunming Zhou2200eda2016-06-30 16:53:02 +08002874 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
2875 amdgpu_fence_driver_force_completion(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002876
Chunming Zhou35d782f2016-07-15 15:57:13 +08002877 need_full_reset = amdgpu_need_full_reset(adev);
2878
2879 if (!need_full_reset) {
2880 amdgpu_pre_soft_reset(adev);
2881 r = amdgpu_soft_reset(adev);
2882 amdgpu_post_soft_reset(adev);
2883 if (r || amdgpu_check_soft_reset(adev)) {
2884 DRM_INFO("soft reset failed, will fallback to full reset!\n");
2885 need_full_reset = true;
2886 }
2887 }
2888
2889 if (need_full_reset) {
Chunming Zhou35d782f2016-07-15 15:57:13 +08002890 r = amdgpu_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002891
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002892retry:
Alex Deucherd05da0e2017-06-30 17:08:45 -04002893 amdgpu_atombios_scratch_regs_save(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002894 r = amdgpu_asic_reset(adev);
Alex Deucherd05da0e2017-06-30 17:08:45 -04002895 amdgpu_atombios_scratch_regs_restore(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002896 /* post card */
2897 amdgpu_atom_asic_init(adev->mode_info.atom_context);
Alex Deucherbfa99262016-01-15 11:59:48 -05002898
Chunming Zhou35d782f2016-07-15 15:57:13 +08002899 if (!r) {
2900 dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
Chunming Zhoufcf06492017-05-05 10:33:33 +08002901 r = amdgpu_resume_phase1(adev);
2902 if (r)
2903 goto out;
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002904 vram_lost = amdgpu_check_vram_lost(adev);
Chunming Zhouf1892132017-05-15 16:48:27 +08002905 if (vram_lost) {
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002906 DRM_ERROR("VRAM is lost!\n");
Chunming Zhouf1892132017-05-15 16:48:27 +08002907 atomic_inc(&adev->vram_lost_counter);
2908 }
Chunming Zhou2c0d7312016-08-30 16:36:25 +08002909 r = amdgpu_ttm_recover_gart(adev);
2910 if (r)
Chunming Zhoufcf06492017-05-05 10:33:33 +08002911 goto out;
2912 r = amdgpu_resume_phase2(adev);
2913 if (r)
2914 goto out;
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002915 if (vram_lost)
2916 amdgpu_fill_reset_magic(adev);
Chunming Zhou2c0d7312016-08-30 16:36:25 +08002917 }
Chunming Zhoufcf06492017-05-05 10:33:33 +08002918 }
2919out:
2920 if (!r) {
2921 amdgpu_irq_gpu_reset_resume_helper(adev);
Chunming Zhou1f465082016-06-30 15:02:26 +08002922 r = amdgpu_ib_ring_tests(adev);
2923 if (r) {
2924 dev_err(adev->dev, "ib ring test failed (%d).\n", r);
Chunming Zhou40019dc2016-06-29 16:01:49 +08002925 r = amdgpu_suspend(adev);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002926 need_full_reset = true;
Chunming Zhou40019dc2016-06-29 16:01:49 +08002927 goto retry;
Chunming Zhou1f465082016-06-30 15:02:26 +08002928 }
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002929 /**
2930 * recovery vm page tables, since we cannot depend on VRAM is
2931 * consistent after gpu full reset.
2932 */
2933 if (need_full_reset && amdgpu_need_backup(adev)) {
2934 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
2935 struct amdgpu_bo *bo, *tmp;
Chris Wilsonf54d1862016-10-25 13:00:45 +01002936 struct dma_fence *fence = NULL, *next = NULL;
Chunming Zhou1f465082016-06-30 15:02:26 +08002937
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002938 DRM_INFO("recover vram bo from shadow\n");
2939 mutex_lock(&adev->shadow_list_lock);
2940 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
Monk Liu236763d2017-05-01 16:15:31 +08002941 next = NULL;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002942 amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
2943 if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01002944 r = dma_fence_wait(fence, false);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002945 if (r) {
Monk Liu1d7b17b2017-01-22 18:52:56 +08002946 WARN(r, "recovery from shadow isn't completed\n");
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002947 break;
2948 }
2949 }
2950
Chris Wilsonf54d1862016-10-25 13:00:45 +01002951 dma_fence_put(fence);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002952 fence = next;
2953 }
2954 mutex_unlock(&adev->shadow_list_lock);
2955 if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01002956 r = dma_fence_wait(fence, false);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002957 if (r)
Monk Liu1d7b17b2017-01-22 18:52:56 +08002958 WARN(r, "recovery from shadow isn't completed\n");
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002959 }
Chris Wilsonf54d1862016-10-25 13:00:45 +01002960 dma_fence_put(fence);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002961 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002962 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2963 struct amdgpu_ring *ring = adev->rings[i];
Chunming Zhou51687752017-04-24 17:09:15 +08002964
2965 if (!ring || !ring->sched.thread)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002966 continue;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002967
Chunming Zhouaa1c8902016-06-30 13:56:02 +08002968 amd_sched_job_recovery(&ring->sched);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002969 kthread_unpark(ring->sched.thread);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002970 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002971 } else {
Chunming Zhou2200eda2016-06-30 16:53:02 +08002972 dev_err(adev->dev, "asic resume failed (%d).\n", r);
Gavin Wan89041942017-06-23 13:55:15 -04002973 amdgpu_vf_error_put(AMDGIM_ERROR_VF_ASIC_RESUME_FAIL, 0, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002974 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
Chunming Zhou51687752017-04-24 17:09:15 +08002975 if (adev->rings[i] && adev->rings[i]->sched.thread) {
Chunming Zhou0875dc92016-06-12 15:41:58 +08002976 kthread_unpark(adev->rings[i]->sched.thread);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002977 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002978 }
2979 }
2980
2981 drm_helper_resume_force_mode(adev->ddev);
2982
2983 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
Gavin Wan89041942017-06-23 13:55:15 -04002984 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002985 /* bad news, how to tell it to userspace ? */
2986 dev_info(adev->dev, "GPU reset failed\n");
Gavin Wan89041942017-06-23 13:55:15 -04002987 amdgpu_vf_error_put(AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, r);
2988 }
2989 else {
Chunming Zhou6643be62017-05-05 10:50:09 +08002990 dev_info(adev->dev, "GPU reset successed!\n");
Gavin Wan89041942017-06-23 13:55:15 -04002991 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002992
Gavin Wan89041942017-06-23 13:55:15 -04002993 amdgpu_vf_error_trans_all(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002994 return r;
2995}
2996
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002997void amdgpu_get_pcie_info(struct amdgpu_device *adev)
2998{
2999 u32 mask;
3000 int ret;
3001
Alex Deuchercd474ba2016-02-04 10:21:23 -05003002 if (amdgpu_pcie_gen_cap)
3003 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
3004
3005 if (amdgpu_pcie_lane_cap)
3006 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
3007
3008 /* covers APUs as well */
3009 if (pci_is_root_bus(adev->pdev->bus)) {
3010 if (adev->pm.pcie_gen_mask == 0)
3011 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
3012 if (adev->pm.pcie_mlw_mask == 0)
3013 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003014 return;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003015 }
Alex Deuchercd474ba2016-02-04 10:21:23 -05003016
3017 if (adev->pm.pcie_gen_mask == 0) {
3018 ret = drm_pcie_get_speed_cap_mask(adev->ddev, &mask);
3019 if (!ret) {
3020 adev->pm.pcie_gen_mask = (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
3021 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
3022 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
3023
3024 if (mask & DRM_PCIE_SPEED_25)
3025 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
3026 if (mask & DRM_PCIE_SPEED_50)
3027 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2;
3028 if (mask & DRM_PCIE_SPEED_80)
3029 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3;
3030 } else {
3031 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
3032 }
3033 }
3034 if (adev->pm.pcie_mlw_mask == 0) {
3035 ret = drm_pcie_get_max_link_width(adev->ddev, &mask);
3036 if (!ret) {
3037 switch (mask) {
3038 case 32:
3039 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
3040 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
3041 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3042 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3043 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3044 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3045 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3046 break;
3047 case 16:
3048 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
3049 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3050 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3051 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3052 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3053 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3054 break;
3055 case 12:
3056 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3057 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3058 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3059 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3060 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3061 break;
3062 case 8:
3063 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3064 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3065 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3066 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3067 break;
3068 case 4:
3069 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3070 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3071 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3072 break;
3073 case 2:
3074 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3075 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3076 break;
3077 case 1:
3078 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
3079 break;
3080 default:
3081 break;
3082 }
3083 } else {
3084 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003085 }
3086 }
3087}
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003088
3089/*
3090 * Debugfs
3091 */
3092int amdgpu_debugfs_add_files(struct amdgpu_device *adev,
Nils Wallménius06ab6832016-05-02 12:46:15 -04003093 const struct drm_info_list *files,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003094 unsigned nfiles)
3095{
3096 unsigned i;
3097
3098 for (i = 0; i < adev->debugfs_count; i++) {
3099 if (adev->debugfs[i].files == files) {
3100 /* Already registered */
3101 return 0;
3102 }
3103 }
3104
3105 i = adev->debugfs_count + 1;
3106 if (i > AMDGPU_DEBUGFS_MAX_COMPONENTS) {
3107 DRM_ERROR("Reached maximum number of debugfs components.\n");
3108 DRM_ERROR("Report so we increase "
3109 "AMDGPU_DEBUGFS_MAX_COMPONENTS.\n");
3110 return -EINVAL;
3111 }
3112 adev->debugfs[adev->debugfs_count].files = files;
3113 adev->debugfs[adev->debugfs_count].num_files = nfiles;
3114 adev->debugfs_count = i;
3115#if defined(CONFIG_DEBUG_FS)
3116 drm_debugfs_create_files(files, nfiles,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003117 adev->ddev->primary->debugfs_root,
3118 adev->ddev->primary);
3119#endif
3120 return 0;
3121}
3122
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003123#if defined(CONFIG_DEBUG_FS)
3124
3125static ssize_t amdgpu_debugfs_regs_read(struct file *f, char __user *buf,
3126 size_t size, loff_t *pos)
3127{
Al Viro45063092016-12-04 18:24:56 -05003128 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003129 ssize_t result = 0;
3130 int r;
Tom St Denisbd122672016-07-28 09:39:22 -04003131 bool pm_pg_lock, use_bank;
Tom St Denis566281592016-06-27 11:55:07 -04003132 unsigned instance_bank, sh_bank, se_bank;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003133
3134 if (size & 0x3 || *pos & 0x3)
3135 return -EINVAL;
3136
Tom St Denisbd122672016-07-28 09:39:22 -04003137 /* are we reading registers for which a PG lock is necessary? */
3138 pm_pg_lock = (*pos >> 23) & 1;
3139
Tom St Denis566281592016-06-27 11:55:07 -04003140 if (*pos & (1ULL << 62)) {
3141 se_bank = (*pos >> 24) & 0x3FF;
3142 sh_bank = (*pos >> 34) & 0x3FF;
3143 instance_bank = (*pos >> 44) & 0x3FF;
Tom St Denis32977f92016-10-09 07:41:26 -04003144
3145 if (se_bank == 0x3FF)
3146 se_bank = 0xFFFFFFFF;
3147 if (sh_bank == 0x3FF)
3148 sh_bank = 0xFFFFFFFF;
3149 if (instance_bank == 0x3FF)
3150 instance_bank = 0xFFFFFFFF;
Tom St Denis566281592016-06-27 11:55:07 -04003151 use_bank = 1;
Tom St Denis566281592016-06-27 11:55:07 -04003152 } else {
3153 use_bank = 0;
3154 }
3155
Tom St Denis801a6aa9a62017-03-15 05:34:25 -04003156 *pos &= (1UL << 22) - 1;
Tom St Denisbd122672016-07-28 09:39:22 -04003157
Tom St Denis566281592016-06-27 11:55:07 -04003158 if (use_bank) {
Tom St Denis32977f92016-10-09 07:41:26 -04003159 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
3160 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
Tom St Denis566281592016-06-27 11:55:07 -04003161 return -EINVAL;
3162 mutex_lock(&adev->grbm_idx_mutex);
3163 amdgpu_gfx_select_se_sh(adev, se_bank,
3164 sh_bank, instance_bank);
3165 }
3166
Tom St Denisbd122672016-07-28 09:39:22 -04003167 if (pm_pg_lock)
3168 mutex_lock(&adev->pm.mutex);
3169
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003170 while (size) {
3171 uint32_t value;
3172
3173 if (*pos > adev->rmmio_size)
Tom St Denis566281592016-06-27 11:55:07 -04003174 goto end;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003175
3176 value = RREG32(*pos >> 2);
3177 r = put_user(value, (uint32_t *)buf);
Tom St Denis566281592016-06-27 11:55:07 -04003178 if (r) {
3179 result = r;
3180 goto end;
3181 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003182
3183 result += 4;
3184 buf += 4;
3185 *pos += 4;
3186 size -= 4;
3187 }
3188
Tom St Denis566281592016-06-27 11:55:07 -04003189end:
3190 if (use_bank) {
3191 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
3192 mutex_unlock(&adev->grbm_idx_mutex);
3193 }
3194
Tom St Denisbd122672016-07-28 09:39:22 -04003195 if (pm_pg_lock)
3196 mutex_unlock(&adev->pm.mutex);
3197
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003198 return result;
3199}
3200
3201static ssize_t amdgpu_debugfs_regs_write(struct file *f, const char __user *buf,
3202 size_t size, loff_t *pos)
3203{
Al Viro45063092016-12-04 18:24:56 -05003204 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003205 ssize_t result = 0;
3206 int r;
Tom St Denis394fdde2016-10-10 07:31:23 -04003207 bool pm_pg_lock, use_bank;
3208 unsigned instance_bank, sh_bank, se_bank;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003209
3210 if (size & 0x3 || *pos & 0x3)
3211 return -EINVAL;
3212
Tom St Denis394fdde2016-10-10 07:31:23 -04003213 /* are we reading registers for which a PG lock is necessary? */
3214 pm_pg_lock = (*pos >> 23) & 1;
3215
3216 if (*pos & (1ULL << 62)) {
3217 se_bank = (*pos >> 24) & 0x3FF;
3218 sh_bank = (*pos >> 34) & 0x3FF;
3219 instance_bank = (*pos >> 44) & 0x3FF;
3220
3221 if (se_bank == 0x3FF)
3222 se_bank = 0xFFFFFFFF;
3223 if (sh_bank == 0x3FF)
3224 sh_bank = 0xFFFFFFFF;
3225 if (instance_bank == 0x3FF)
3226 instance_bank = 0xFFFFFFFF;
3227 use_bank = 1;
3228 } else {
3229 use_bank = 0;
3230 }
3231
Tom St Denis801a6aa9a62017-03-15 05:34:25 -04003232 *pos &= (1UL << 22) - 1;
Tom St Denis394fdde2016-10-10 07:31:23 -04003233
3234 if (use_bank) {
3235 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
3236 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
3237 return -EINVAL;
3238 mutex_lock(&adev->grbm_idx_mutex);
3239 amdgpu_gfx_select_se_sh(adev, se_bank,
3240 sh_bank, instance_bank);
3241 }
3242
3243 if (pm_pg_lock)
3244 mutex_lock(&adev->pm.mutex);
3245
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003246 while (size) {
3247 uint32_t value;
3248
3249 if (*pos > adev->rmmio_size)
3250 return result;
3251
3252 r = get_user(value, (uint32_t *)buf);
3253 if (r)
3254 return r;
3255
3256 WREG32(*pos >> 2, value);
3257
3258 result += 4;
3259 buf += 4;
3260 *pos += 4;
3261 size -= 4;
3262 }
3263
Tom St Denis394fdde2016-10-10 07:31:23 -04003264 if (use_bank) {
3265 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
3266 mutex_unlock(&adev->grbm_idx_mutex);
3267 }
3268
3269 if (pm_pg_lock)
3270 mutex_unlock(&adev->pm.mutex);
3271
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003272 return result;
3273}
3274
Tom St Denisadcec282016-04-15 13:08:44 -04003275static ssize_t amdgpu_debugfs_regs_pcie_read(struct file *f, char __user *buf,
3276 size_t size, loff_t *pos)
3277{
Al Viro45063092016-12-04 18:24:56 -05003278 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003279 ssize_t result = 0;
3280 int r;
3281
3282 if (size & 0x3 || *pos & 0x3)
3283 return -EINVAL;
3284
3285 while (size) {
3286 uint32_t value;
3287
3288 value = RREG32_PCIE(*pos >> 2);
3289 r = put_user(value, (uint32_t *)buf);
3290 if (r)
3291 return r;
3292
3293 result += 4;
3294 buf += 4;
3295 *pos += 4;
3296 size -= 4;
3297 }
3298
3299 return result;
3300}
3301
3302static ssize_t amdgpu_debugfs_regs_pcie_write(struct file *f, const char __user *buf,
3303 size_t size, loff_t *pos)
3304{
Al Viro45063092016-12-04 18:24:56 -05003305 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003306 ssize_t result = 0;
3307 int r;
3308
3309 if (size & 0x3 || *pos & 0x3)
3310 return -EINVAL;
3311
3312 while (size) {
3313 uint32_t value;
3314
3315 r = get_user(value, (uint32_t *)buf);
3316 if (r)
3317 return r;
3318
3319 WREG32_PCIE(*pos >> 2, value);
3320
3321 result += 4;
3322 buf += 4;
3323 *pos += 4;
3324 size -= 4;
3325 }
3326
3327 return result;
3328}
3329
3330static ssize_t amdgpu_debugfs_regs_didt_read(struct file *f, char __user *buf,
3331 size_t size, loff_t *pos)
3332{
Al Viro45063092016-12-04 18:24:56 -05003333 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003334 ssize_t result = 0;
3335 int r;
3336
3337 if (size & 0x3 || *pos & 0x3)
3338 return -EINVAL;
3339
3340 while (size) {
3341 uint32_t value;
3342
3343 value = RREG32_DIDT(*pos >> 2);
3344 r = put_user(value, (uint32_t *)buf);
3345 if (r)
3346 return r;
3347
3348 result += 4;
3349 buf += 4;
3350 *pos += 4;
3351 size -= 4;
3352 }
3353
3354 return result;
3355}
3356
3357static ssize_t amdgpu_debugfs_regs_didt_write(struct file *f, const char __user *buf,
3358 size_t size, loff_t *pos)
3359{
Al Viro45063092016-12-04 18:24:56 -05003360 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003361 ssize_t result = 0;
3362 int r;
3363
3364 if (size & 0x3 || *pos & 0x3)
3365 return -EINVAL;
3366
3367 while (size) {
3368 uint32_t value;
3369
3370 r = get_user(value, (uint32_t *)buf);
3371 if (r)
3372 return r;
3373
3374 WREG32_DIDT(*pos >> 2, value);
3375
3376 result += 4;
3377 buf += 4;
3378 *pos += 4;
3379 size -= 4;
3380 }
3381
3382 return result;
3383}
3384
3385static ssize_t amdgpu_debugfs_regs_smc_read(struct file *f, char __user *buf,
3386 size_t size, loff_t *pos)
3387{
Al Viro45063092016-12-04 18:24:56 -05003388 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003389 ssize_t result = 0;
3390 int r;
3391
3392 if (size & 0x3 || *pos & 0x3)
3393 return -EINVAL;
3394
3395 while (size) {
3396 uint32_t value;
3397
Tom St Denis6fc0dea2016-08-29 08:39:29 -04003398 value = RREG32_SMC(*pos);
Tom St Denisadcec282016-04-15 13:08:44 -04003399 r = put_user(value, (uint32_t *)buf);
3400 if (r)
3401 return r;
3402
3403 result += 4;
3404 buf += 4;
3405 *pos += 4;
3406 size -= 4;
3407 }
3408
3409 return result;
3410}
3411
3412static ssize_t amdgpu_debugfs_regs_smc_write(struct file *f, const char __user *buf,
3413 size_t size, loff_t *pos)
3414{
Al Viro45063092016-12-04 18:24:56 -05003415 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003416 ssize_t result = 0;
3417 int r;
3418
3419 if (size & 0x3 || *pos & 0x3)
3420 return -EINVAL;
3421
3422 while (size) {
3423 uint32_t value;
3424
3425 r = get_user(value, (uint32_t *)buf);
3426 if (r)
3427 return r;
3428
Tom St Denis6fc0dea2016-08-29 08:39:29 -04003429 WREG32_SMC(*pos, value);
Tom St Denisadcec282016-04-15 13:08:44 -04003430
3431 result += 4;
3432 buf += 4;
3433 *pos += 4;
3434 size -= 4;
3435 }
3436
3437 return result;
3438}
3439
Tom St Denis1e051412016-06-27 09:57:18 -04003440static ssize_t amdgpu_debugfs_gca_config_read(struct file *f, char __user *buf,
3441 size_t size, loff_t *pos)
3442{
Al Viro45063092016-12-04 18:24:56 -05003443 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denis1e051412016-06-27 09:57:18 -04003444 ssize_t result = 0;
3445 int r;
3446 uint32_t *config, no_regs = 0;
3447
3448 if (size & 0x3 || *pos & 0x3)
3449 return -EINVAL;
3450
Markus Elfringecab7662016-09-18 17:00:52 +02003451 config = kmalloc_array(256, sizeof(*config), GFP_KERNEL);
Tom St Denis1e051412016-06-27 09:57:18 -04003452 if (!config)
3453 return -ENOMEM;
3454
3455 /* version, increment each time something is added */
Tom St Denis9a999352017-01-18 13:01:25 -05003456 config[no_regs++] = 3;
Tom St Denis1e051412016-06-27 09:57:18 -04003457 config[no_regs++] = adev->gfx.config.max_shader_engines;
3458 config[no_regs++] = adev->gfx.config.max_tile_pipes;
3459 config[no_regs++] = adev->gfx.config.max_cu_per_sh;
3460 config[no_regs++] = adev->gfx.config.max_sh_per_se;
3461 config[no_regs++] = adev->gfx.config.max_backends_per_se;
3462 config[no_regs++] = adev->gfx.config.max_texture_channel_caches;
3463 config[no_regs++] = adev->gfx.config.max_gprs;
3464 config[no_regs++] = adev->gfx.config.max_gs_threads;
3465 config[no_regs++] = adev->gfx.config.max_hw_contexts;
3466 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_frontend;
3467 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_backend;
3468 config[no_regs++] = adev->gfx.config.sc_hiz_tile_fifo_size;
3469 config[no_regs++] = adev->gfx.config.sc_earlyz_tile_fifo_size;
3470 config[no_regs++] = adev->gfx.config.num_tile_pipes;
3471 config[no_regs++] = adev->gfx.config.backend_enable_mask;
3472 config[no_regs++] = adev->gfx.config.mem_max_burst_length_bytes;
3473 config[no_regs++] = adev->gfx.config.mem_row_size_in_kb;
3474 config[no_regs++] = adev->gfx.config.shader_engine_tile_size;
3475 config[no_regs++] = adev->gfx.config.num_gpus;
3476 config[no_regs++] = adev->gfx.config.multi_gpu_tile_size;
3477 config[no_regs++] = adev->gfx.config.mc_arb_ramcfg;
3478 config[no_regs++] = adev->gfx.config.gb_addr_config;
3479 config[no_regs++] = adev->gfx.config.num_rbs;
3480
Tom St Denis89a8f302016-08-12 15:14:31 -04003481 /* rev==1 */
3482 config[no_regs++] = adev->rev_id;
3483 config[no_regs++] = adev->pg_flags;
3484 config[no_regs++] = adev->cg_flags;
3485
Tom St Denise9f11dc2016-08-17 12:00:51 -04003486 /* rev==2 */
3487 config[no_regs++] = adev->family;
3488 config[no_regs++] = adev->external_rev_id;
3489
Tom St Denis9a999352017-01-18 13:01:25 -05003490 /* rev==3 */
3491 config[no_regs++] = adev->pdev->device;
3492 config[no_regs++] = adev->pdev->revision;
3493 config[no_regs++] = adev->pdev->subsystem_device;
3494 config[no_regs++] = adev->pdev->subsystem_vendor;
3495
Tom St Denis1e051412016-06-27 09:57:18 -04003496 while (size && (*pos < no_regs * 4)) {
3497 uint32_t value;
3498
3499 value = config[*pos >> 2];
3500 r = put_user(value, (uint32_t *)buf);
3501 if (r) {
3502 kfree(config);
3503 return r;
3504 }
3505
3506 result += 4;
3507 buf += 4;
3508 *pos += 4;
3509 size -= 4;
3510 }
3511
3512 kfree(config);
3513 return result;
3514}
3515
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003516static ssize_t amdgpu_debugfs_sensor_read(struct file *f, char __user *buf,
3517 size_t size, loff_t *pos)
3518{
Al Viro45063092016-12-04 18:24:56 -05003519 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003520 int idx, x, outsize, r, valuesize;
3521 uint32_t values[16];
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003522
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003523 if (size & 3 || *pos & 0x3)
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003524 return -EINVAL;
3525
Samuel Pitoiset3cbc6142017-02-15 19:32:29 +01003526 if (amdgpu_dpm == 0)
3527 return -EINVAL;
3528
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003529 /* convert offset to sensor number */
3530 idx = *pos >> 2;
3531
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003532 valuesize = sizeof(values);
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003533 if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->read_sensor)
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003534 r = adev->powerplay.pp_funcs->read_sensor(adev->powerplay.pp_handle, idx, &values[0], &valuesize);
Samuel Pitoiset3cbc6142017-02-15 19:32:29 +01003535 else if (adev->pm.funcs && adev->pm.funcs->read_sensor)
3536 r = adev->pm.funcs->read_sensor(adev, idx, &values[0],
3537 &valuesize);
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003538 else
3539 return -EINVAL;
3540
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003541 if (size > valuesize)
3542 return -EINVAL;
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003543
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003544 outsize = 0;
3545 x = 0;
3546 if (!r) {
3547 while (size) {
3548 r = put_user(values[x++], (int32_t *)buf);
3549 buf += 4;
3550 size -= 4;
3551 outsize += 4;
3552 }
3553 }
3554
3555 return !r ? outsize : r;
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003556}
Tom St Denis1e051412016-06-27 09:57:18 -04003557
Tom St Denis273d7aa2016-10-11 14:48:55 -04003558static ssize_t amdgpu_debugfs_wave_read(struct file *f, char __user *buf,
3559 size_t size, loff_t *pos)
3560{
3561 struct amdgpu_device *adev = f->f_inode->i_private;
3562 int r, x;
3563 ssize_t result=0;
Tom St Denis472259f2016-10-14 09:49:09 -04003564 uint32_t offset, se, sh, cu, wave, simd, data[32];
Tom St Denis273d7aa2016-10-11 14:48:55 -04003565
3566 if (size & 3 || *pos & 3)
3567 return -EINVAL;
3568
3569 /* decode offset */
3570 offset = (*pos & 0x7F);
3571 se = ((*pos >> 7) & 0xFF);
3572 sh = ((*pos >> 15) & 0xFF);
3573 cu = ((*pos >> 23) & 0xFF);
3574 wave = ((*pos >> 31) & 0xFF);
3575 simd = ((*pos >> 37) & 0xFF);
Tom St Denis273d7aa2016-10-11 14:48:55 -04003576
3577 /* switch to the specific se/sh/cu */
3578 mutex_lock(&adev->grbm_idx_mutex);
3579 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
3580
3581 x = 0;
Tom St Denis472259f2016-10-14 09:49:09 -04003582 if (adev->gfx.funcs->read_wave_data)
3583 adev->gfx.funcs->read_wave_data(adev, simd, wave, data, &x);
Tom St Denis273d7aa2016-10-11 14:48:55 -04003584
3585 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
3586 mutex_unlock(&adev->grbm_idx_mutex);
3587
Tom St Denis5ecfb3b2016-10-13 12:15:03 -04003588 if (!x)
3589 return -EINVAL;
3590
Tom St Denis472259f2016-10-14 09:49:09 -04003591 while (size && (offset < x * 4)) {
Tom St Denis273d7aa2016-10-11 14:48:55 -04003592 uint32_t value;
3593
Tom St Denis472259f2016-10-14 09:49:09 -04003594 value = data[offset >> 2];
Tom St Denis273d7aa2016-10-11 14:48:55 -04003595 r = put_user(value, (uint32_t *)buf);
3596 if (r)
3597 return r;
3598
3599 result += 4;
3600 buf += 4;
Tom St Denis472259f2016-10-14 09:49:09 -04003601 offset += 4;
Tom St Denis273d7aa2016-10-11 14:48:55 -04003602 size -= 4;
3603 }
3604
3605 return result;
3606}
3607
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003608static ssize_t amdgpu_debugfs_gpr_read(struct file *f, char __user *buf,
3609 size_t size, loff_t *pos)
3610{
3611 struct amdgpu_device *adev = f->f_inode->i_private;
3612 int r;
3613 ssize_t result = 0;
3614 uint32_t offset, se, sh, cu, wave, simd, thread, bank, *data;
3615
3616 if (size & 3 || *pos & 3)
3617 return -EINVAL;
3618
3619 /* decode offset */
3620 offset = (*pos & 0xFFF); /* in dwords */
3621 se = ((*pos >> 12) & 0xFF);
3622 sh = ((*pos >> 20) & 0xFF);
3623 cu = ((*pos >> 28) & 0xFF);
3624 wave = ((*pos >> 36) & 0xFF);
3625 simd = ((*pos >> 44) & 0xFF);
3626 thread = ((*pos >> 52) & 0xFF);
3627 bank = ((*pos >> 60) & 1);
3628
3629 data = kmalloc_array(1024, sizeof(*data), GFP_KERNEL);
3630 if (!data)
3631 return -ENOMEM;
3632
3633 /* switch to the specific se/sh/cu */
3634 mutex_lock(&adev->grbm_idx_mutex);
3635 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
3636
3637 if (bank == 0) {
3638 if (adev->gfx.funcs->read_wave_vgprs)
3639 adev->gfx.funcs->read_wave_vgprs(adev, simd, wave, thread, offset, size>>2, data);
3640 } else {
3641 if (adev->gfx.funcs->read_wave_sgprs)
3642 adev->gfx.funcs->read_wave_sgprs(adev, simd, wave, offset, size>>2, data);
3643 }
3644
3645 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
3646 mutex_unlock(&adev->grbm_idx_mutex);
3647
3648 while (size) {
3649 uint32_t value;
3650
3651 value = data[offset++];
3652 r = put_user(value, (uint32_t *)buf);
3653 if (r) {
3654 result = r;
3655 goto err;
3656 }
3657
3658 result += 4;
3659 buf += 4;
3660 size -= 4;
3661 }
3662
3663err:
3664 kfree(data);
3665 return result;
3666}
3667
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003668static const struct file_operations amdgpu_debugfs_regs_fops = {
3669 .owner = THIS_MODULE,
3670 .read = amdgpu_debugfs_regs_read,
3671 .write = amdgpu_debugfs_regs_write,
3672 .llseek = default_llseek
3673};
Tom St Denisadcec282016-04-15 13:08:44 -04003674static const struct file_operations amdgpu_debugfs_regs_didt_fops = {
3675 .owner = THIS_MODULE,
3676 .read = amdgpu_debugfs_regs_didt_read,
3677 .write = amdgpu_debugfs_regs_didt_write,
3678 .llseek = default_llseek
3679};
3680static const struct file_operations amdgpu_debugfs_regs_pcie_fops = {
3681 .owner = THIS_MODULE,
3682 .read = amdgpu_debugfs_regs_pcie_read,
3683 .write = amdgpu_debugfs_regs_pcie_write,
3684 .llseek = default_llseek
3685};
3686static const struct file_operations amdgpu_debugfs_regs_smc_fops = {
3687 .owner = THIS_MODULE,
3688 .read = amdgpu_debugfs_regs_smc_read,
3689 .write = amdgpu_debugfs_regs_smc_write,
3690 .llseek = default_llseek
3691};
3692
Tom St Denis1e051412016-06-27 09:57:18 -04003693static const struct file_operations amdgpu_debugfs_gca_config_fops = {
3694 .owner = THIS_MODULE,
3695 .read = amdgpu_debugfs_gca_config_read,
3696 .llseek = default_llseek
3697};
3698
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003699static const struct file_operations amdgpu_debugfs_sensors_fops = {
3700 .owner = THIS_MODULE,
3701 .read = amdgpu_debugfs_sensor_read,
3702 .llseek = default_llseek
3703};
3704
Tom St Denis273d7aa2016-10-11 14:48:55 -04003705static const struct file_operations amdgpu_debugfs_wave_fops = {
3706 .owner = THIS_MODULE,
3707 .read = amdgpu_debugfs_wave_read,
3708 .llseek = default_llseek
3709};
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003710static const struct file_operations amdgpu_debugfs_gpr_fops = {
3711 .owner = THIS_MODULE,
3712 .read = amdgpu_debugfs_gpr_read,
3713 .llseek = default_llseek
3714};
Tom St Denis273d7aa2016-10-11 14:48:55 -04003715
Tom St Denisadcec282016-04-15 13:08:44 -04003716static const struct file_operations *debugfs_regs[] = {
3717 &amdgpu_debugfs_regs_fops,
3718 &amdgpu_debugfs_regs_didt_fops,
3719 &amdgpu_debugfs_regs_pcie_fops,
3720 &amdgpu_debugfs_regs_smc_fops,
Tom St Denis1e051412016-06-27 09:57:18 -04003721 &amdgpu_debugfs_gca_config_fops,
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003722 &amdgpu_debugfs_sensors_fops,
Tom St Denis273d7aa2016-10-11 14:48:55 -04003723 &amdgpu_debugfs_wave_fops,
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003724 &amdgpu_debugfs_gpr_fops,
Tom St Denisadcec282016-04-15 13:08:44 -04003725};
3726
3727static const char *debugfs_regs_names[] = {
3728 "amdgpu_regs",
3729 "amdgpu_regs_didt",
3730 "amdgpu_regs_pcie",
3731 "amdgpu_regs_smc",
Tom St Denis1e051412016-06-27 09:57:18 -04003732 "amdgpu_gca_config",
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003733 "amdgpu_sensors",
Tom St Denis273d7aa2016-10-11 14:48:55 -04003734 "amdgpu_wave",
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003735 "amdgpu_gpr",
Tom St Denisadcec282016-04-15 13:08:44 -04003736};
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003737
3738static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
3739{
3740 struct drm_minor *minor = adev->ddev->primary;
3741 struct dentry *ent, *root = minor->debugfs_root;
Tom St Denisadcec282016-04-15 13:08:44 -04003742 unsigned i, j;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003743
Tom St Denisadcec282016-04-15 13:08:44 -04003744 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3745 ent = debugfs_create_file(debugfs_regs_names[i],
3746 S_IFREG | S_IRUGO, root,
3747 adev, debugfs_regs[i]);
3748 if (IS_ERR(ent)) {
3749 for (j = 0; j < i; j++) {
3750 debugfs_remove(adev->debugfs_regs[i]);
3751 adev->debugfs_regs[i] = NULL;
3752 }
3753 return PTR_ERR(ent);
3754 }
3755
3756 if (!i)
3757 i_size_write(ent->d_inode, adev->rmmio_size);
3758 adev->debugfs_regs[i] = ent;
3759 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003760
3761 return 0;
3762}
3763
3764static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev)
3765{
Tom St Denisadcec282016-04-15 13:08:44 -04003766 unsigned i;
3767
3768 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3769 if (adev->debugfs_regs[i]) {
3770 debugfs_remove(adev->debugfs_regs[i]);
3771 adev->debugfs_regs[i] = NULL;
3772 }
3773 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003774}
3775
Huang Rui4f0955f2017-05-10 23:04:06 +08003776static int amdgpu_debugfs_test_ib(struct seq_file *m, void *data)
3777{
3778 struct drm_info_node *node = (struct drm_info_node *) m->private;
3779 struct drm_device *dev = node->minor->dev;
3780 struct amdgpu_device *adev = dev->dev_private;
3781 int r = 0, i;
3782
3783 /* hold on the scheduler */
3784 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
3785 struct amdgpu_ring *ring = adev->rings[i];
3786
3787 if (!ring || !ring->sched.thread)
3788 continue;
3789 kthread_park(ring->sched.thread);
3790 }
3791
3792 seq_printf(m, "run ib test:\n");
3793 r = amdgpu_ib_ring_tests(adev);
3794 if (r)
3795 seq_printf(m, "ib ring tests failed (%d).\n", r);
3796 else
3797 seq_printf(m, "ib ring tests passed.\n");
3798
3799 /* go on the scheduler */
3800 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
3801 struct amdgpu_ring *ring = adev->rings[i];
3802
3803 if (!ring || !ring->sched.thread)
3804 continue;
3805 kthread_unpark(ring->sched.thread);
3806 }
3807
3808 return 0;
3809}
3810
3811static const struct drm_info_list amdgpu_debugfs_test_ib_ring_list[] = {
3812 {"amdgpu_test_ib", &amdgpu_debugfs_test_ib}
3813};
3814
3815static int amdgpu_debugfs_test_ib_ring_init(struct amdgpu_device *adev)
3816{
3817 return amdgpu_debugfs_add_files(adev,
3818 amdgpu_debugfs_test_ib_ring_list, 1);
3819}
3820
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003821int amdgpu_debugfs_init(struct drm_minor *minor)
3822{
3823 return 0;
3824}
Alexander Kuleshov7cebc722015-06-27 13:16:05 +06003825#else
Arnd Bergmann27bad5b2017-06-21 23:51:02 +02003826static int amdgpu_debugfs_test_ib_ring_init(struct amdgpu_device *adev)
Huang Rui4f0955f2017-05-10 23:04:06 +08003827{
3828 return 0;
3829}
Alexander Kuleshov7cebc722015-06-27 13:16:05 +06003830static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
3831{
3832 return 0;
3833}
3834static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev) { }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003835#endif