blob: 3cd9df890b005d515c8caec3ddceee626a8d0aa4 [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{
339 int r;
340
341 if (adev->vram_scratch.robj == NULL) {
342 r = amdgpu_bo_create(adev, AMDGPU_GPU_PAGE_SIZE,
Alex Deucher857d9132015-08-27 00:14:16 -0400343 PAGE_SIZE, true, AMDGPU_GEM_DOMAIN_VRAM,
Christian König03f48dd2016-08-15 17:00:22 +0200344 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
345 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS,
Christian König72d76682015-09-03 17:34:59 +0200346 NULL, NULL, &adev->vram_scratch.robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400347 if (r) {
348 return r;
349 }
350 }
351
352 r = amdgpu_bo_reserve(adev->vram_scratch.robj, false);
353 if (unlikely(r != 0))
354 return r;
355 r = amdgpu_bo_pin(adev->vram_scratch.robj,
356 AMDGPU_GEM_DOMAIN_VRAM, &adev->vram_scratch.gpu_addr);
357 if (r) {
358 amdgpu_bo_unreserve(adev->vram_scratch.robj);
359 return r;
360 }
361 r = amdgpu_bo_kmap(adev->vram_scratch.robj,
362 (void **)&adev->vram_scratch.ptr);
363 if (r)
364 amdgpu_bo_unpin(adev->vram_scratch.robj);
365 amdgpu_bo_unreserve(adev->vram_scratch.robj);
366
367 return r;
368}
369
370static void amdgpu_vram_scratch_fini(struct amdgpu_device *adev)
371{
372 int r;
373
374 if (adev->vram_scratch.robj == NULL) {
375 return;
376 }
Alex Xie8ab25b42017-04-24 13:30:43 -0400377 r = amdgpu_bo_reserve(adev->vram_scratch.robj, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400378 if (likely(r == 0)) {
379 amdgpu_bo_kunmap(adev->vram_scratch.robj);
380 amdgpu_bo_unpin(adev->vram_scratch.robj);
381 amdgpu_bo_unreserve(adev->vram_scratch.robj);
382 }
383 amdgpu_bo_unref(&adev->vram_scratch.robj);
384}
385
386/**
387 * amdgpu_program_register_sequence - program an array of registers.
388 *
389 * @adev: amdgpu_device pointer
390 * @registers: pointer to the register array
391 * @array_size: size of the register array
392 *
393 * Programs an array or registers with and and or masks.
394 * This is a helper for setting golden registers.
395 */
396void amdgpu_program_register_sequence(struct amdgpu_device *adev,
397 const u32 *registers,
398 const u32 array_size)
399{
400 u32 tmp, reg, and_mask, or_mask;
401 int i;
402
403 if (array_size % 3)
404 return;
405
406 for (i = 0; i < array_size; i +=3) {
407 reg = registers[i + 0];
408 and_mask = registers[i + 1];
409 or_mask = registers[i + 2];
410
411 if (and_mask == 0xffffffff) {
412 tmp = or_mask;
413 } else {
414 tmp = RREG32(reg);
415 tmp &= ~and_mask;
416 tmp |= or_mask;
417 }
418 WREG32(reg, tmp);
419 }
420}
421
422void amdgpu_pci_config_reset(struct amdgpu_device *adev)
423{
424 pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
425}
426
427/*
428 * GPU doorbell aperture helpers function.
429 */
430/**
431 * amdgpu_doorbell_init - Init doorbell driver information.
432 *
433 * @adev: amdgpu_device pointer
434 *
435 * Init doorbell driver information (CIK)
436 * Returns 0 on success, error on failure.
437 */
438static int amdgpu_doorbell_init(struct amdgpu_device *adev)
439{
440 /* doorbell bar mapping */
441 adev->doorbell.base = pci_resource_start(adev->pdev, 2);
442 adev->doorbell.size = pci_resource_len(adev->pdev, 2);
443
Christian Königedf600d2016-05-03 15:54:54 +0200444 adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400445 AMDGPU_DOORBELL_MAX_ASSIGNMENT+1);
446 if (adev->doorbell.num_doorbells == 0)
447 return -EINVAL;
448
Christian König8972e5d2017-03-06 13:34:57 +0100449 adev->doorbell.ptr = ioremap(adev->doorbell.base,
450 adev->doorbell.num_doorbells *
451 sizeof(u32));
452 if (adev->doorbell.ptr == NULL)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400453 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400454
455 return 0;
456}
457
458/**
459 * amdgpu_doorbell_fini - Tear down doorbell driver information.
460 *
461 * @adev: amdgpu_device pointer
462 *
463 * Tear down doorbell driver information (CIK)
464 */
465static void amdgpu_doorbell_fini(struct amdgpu_device *adev)
466{
467 iounmap(adev->doorbell.ptr);
468 adev->doorbell.ptr = NULL;
469}
470
471/**
472 * amdgpu_doorbell_get_kfd_info - Report doorbell configuration required to
473 * setup amdkfd
474 *
475 * @adev: amdgpu_device pointer
476 * @aperture_base: output returning doorbell aperture base physical address
477 * @aperture_size: output returning doorbell aperture size in bytes
478 * @start_offset: output returning # of doorbell bytes reserved for amdgpu.
479 *
480 * amdgpu and amdkfd share the doorbell aperture. amdgpu sets it up,
481 * takes doorbells required for its own rings and reports the setup to amdkfd.
482 * amdgpu reserved doorbells are at the start of the doorbell aperture.
483 */
484void amdgpu_doorbell_get_kfd_info(struct amdgpu_device *adev,
485 phys_addr_t *aperture_base,
486 size_t *aperture_size,
487 size_t *start_offset)
488{
489 /*
490 * The first num_doorbells are used by amdgpu.
491 * amdkfd takes whatever's left in the aperture.
492 */
493 if (adev->doorbell.size > adev->doorbell.num_doorbells * sizeof(u32)) {
494 *aperture_base = adev->doorbell.base;
495 *aperture_size = adev->doorbell.size;
496 *start_offset = adev->doorbell.num_doorbells * sizeof(u32);
497 } else {
498 *aperture_base = 0;
499 *aperture_size = 0;
500 *start_offset = 0;
501 }
502}
503
504/*
505 * amdgpu_wb_*()
Alex Xie455a7bc2017-05-08 21:36:03 -0400506 * Writeback is the method by which the GPU updates special pages in memory
Alex Xieea81a172017-05-08 13:41:11 -0400507 * with the status of certain GPU events (fences, ring pointers,etc.).
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400508 */
509
510/**
511 * amdgpu_wb_fini - Disable Writeback and free memory
512 *
513 * @adev: amdgpu_device pointer
514 *
515 * Disables Writeback and frees the Writeback memory (all asics).
516 * Used at driver shutdown.
517 */
518static void amdgpu_wb_fini(struct amdgpu_device *adev)
519{
520 if (adev->wb.wb_obj) {
Alex Deuchera76ed482016-10-21 15:30:36 -0400521 amdgpu_bo_free_kernel(&adev->wb.wb_obj,
522 &adev->wb.gpu_addr,
523 (void **)&adev->wb.wb);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400524 adev->wb.wb_obj = NULL;
525 }
526}
527
528/**
529 * amdgpu_wb_init- Init Writeback driver info and allocate memory
530 *
531 * @adev: amdgpu_device pointer
532 *
Alex Xie455a7bc2017-05-08 21:36:03 -0400533 * Initializes writeback and allocates writeback memory (all asics).
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400534 * Used at driver startup.
535 * Returns 0 on success or an -error on failure.
536 */
537static int amdgpu_wb_init(struct amdgpu_device *adev)
538{
539 int r;
540
541 if (adev->wb.wb_obj == NULL) {
Huang Rui60a970a62017-03-15 10:13:32 +0800542 r = amdgpu_bo_create_kernel(adev, AMDGPU_MAX_WB * sizeof(uint32_t),
Alex Deuchera76ed482016-10-21 15:30:36 -0400543 PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
544 &adev->wb.wb_obj, &adev->wb.gpu_addr,
545 (void **)&adev->wb.wb);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400546 if (r) {
547 dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
548 return r;
549 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400550
551 adev->wb.num_wb = AMDGPU_MAX_WB;
552 memset(&adev->wb.used, 0, sizeof(adev->wb.used));
553
554 /* clear wb memory */
Huang Rui60a970a62017-03-15 10:13:32 +0800555 memset((char *)adev->wb.wb, 0, AMDGPU_MAX_WB * sizeof(uint32_t));
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400556 }
557
558 return 0;
559}
560
561/**
562 * amdgpu_wb_get - Allocate a wb entry
563 *
564 * @adev: amdgpu_device pointer
565 * @wb: wb index
566 *
567 * Allocate a wb slot for use by the driver (all asics).
568 * Returns 0 on success or -EINVAL on failure.
569 */
570int amdgpu_wb_get(struct amdgpu_device *adev, u32 *wb)
571{
572 unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
573 if (offset < adev->wb.num_wb) {
574 __set_bit(offset, adev->wb.used);
575 *wb = offset;
576 return 0;
577 } else {
578 return -EINVAL;
579 }
580}
581
582/**
Ken Wang70142852016-03-18 15:08:49 +0800583 * amdgpu_wb_get_64bit - Allocate a wb entry
584 *
585 * @adev: amdgpu_device pointer
586 * @wb: wb index
587 *
588 * Allocate a wb slot for use by the driver (all asics).
589 * Returns 0 on success or -EINVAL on failure.
590 */
591int amdgpu_wb_get_64bit(struct amdgpu_device *adev, u32 *wb)
592{
593 unsigned long offset = bitmap_find_next_zero_area_off(adev->wb.used,
594 adev->wb.num_wb, 0, 2, 7, 0);
595 if ((offset + 1) < adev->wb.num_wb) {
596 __set_bit(offset, adev->wb.used);
597 __set_bit(offset + 1, adev->wb.used);
598 *wb = offset;
599 return 0;
600 } else {
601 return -EINVAL;
602 }
603}
604
Monk Liu0915fdb2017-06-19 10:19:41 -0400605int amdgpu_wb_get_256Bit(struct amdgpu_device *adev, u32 *wb)
606{
607 int i = 0;
608 unsigned long offset = bitmap_find_next_zero_area_off(adev->wb.used,
609 adev->wb.num_wb, 0, 8, 63, 0);
610 if ((offset + 7) < adev->wb.num_wb) {
611 for (i = 0; i < 8; i++)
612 __set_bit(offset + i, adev->wb.used);
613 *wb = offset;
614 return 0;
615 } else {
616 return -EINVAL;
617 }
618}
619
Ken Wang70142852016-03-18 15:08:49 +0800620/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400621 * amdgpu_wb_free - Free a wb entry
622 *
623 * @adev: amdgpu_device pointer
624 * @wb: wb index
625 *
626 * Free a wb slot allocated for use by the driver (all asics)
627 */
628void amdgpu_wb_free(struct amdgpu_device *adev, u32 wb)
629{
630 if (wb < adev->wb.num_wb)
631 __clear_bit(wb, adev->wb.used);
632}
633
634/**
Ken Wang70142852016-03-18 15:08:49 +0800635 * amdgpu_wb_free_64bit - Free a wb entry
636 *
637 * @adev: amdgpu_device pointer
638 * @wb: wb index
639 *
640 * Free a wb slot allocated for use by the driver (all asics)
641 */
642void amdgpu_wb_free_64bit(struct amdgpu_device *adev, u32 wb)
643{
644 if ((wb + 1) < adev->wb.num_wb) {
645 __clear_bit(wb, adev->wb.used);
646 __clear_bit(wb + 1, adev->wb.used);
647 }
648}
649
650/**
Monk Liu0915fdb2017-06-19 10:19:41 -0400651 * amdgpu_wb_free_256bit - Free a wb entry
652 *
653 * @adev: amdgpu_device pointer
654 * @wb: wb index
655 *
656 * Free a wb slot allocated for use by the driver (all asics)
657 */
658void amdgpu_wb_free_256bit(struct amdgpu_device *adev, u32 wb)
659{
660 int i = 0;
661
662 if ((wb + 7) < adev->wb.num_wb)
663 for (i = 0; i < 8; i++)
664 __clear_bit(wb + i, adev->wb.used);
665}
666
667/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400668 * amdgpu_vram_location - try to find VRAM location
669 * @adev: amdgpu device structure holding all necessary informations
670 * @mc: memory controller structure holding memory informations
671 * @base: base address at which to put VRAM
672 *
Alex Xie455a7bc2017-05-08 21:36:03 -0400673 * Function will try to place VRAM at base address provided
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400674 * as parameter (which is so far either PCI aperture address or
675 * for IGP TOM base address).
676 *
677 * If there is not enough space to fit the unvisible VRAM in the 32bits
678 * address space then we limit the VRAM size to the aperture.
679 *
680 * Note: We don't explicitly enforce VRAM start to be aligned on VRAM size,
681 * this shouldn't be a problem as we are using the PCI aperture as a reference.
682 * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but
683 * not IGP.
684 *
685 * Note: we use mc_vram_size as on some board we need to program the mc to
686 * cover the whole aperture even if VRAM size is inferior to aperture size
687 * Novell bug 204882 + along with lots of ubuntu ones
688 *
689 * Note: when limiting vram it's safe to overwritte real_vram_size because
690 * we are not in case where real_vram_size is inferior to mc_vram_size (ie
691 * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu
692 * ones)
693 *
694 * Note: IGP TOM addr should be the same as the aperture addr, we don't
Alex Xie455a7bc2017-05-08 21:36:03 -0400695 * explicitly check for that though.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400696 *
697 * FIXME: when reducing VRAM size align new size on power of 2.
698 */
699void amdgpu_vram_location(struct amdgpu_device *adev, struct amdgpu_mc *mc, u64 base)
700{
701 uint64_t limit = (uint64_t)amdgpu_vram_limit << 20;
702
703 mc->vram_start = base;
704 if (mc->mc_vram_size > (adev->mc.mc_mask - base + 1)) {
705 dev_warn(adev->dev, "limiting VRAM to PCI aperture size\n");
706 mc->real_vram_size = mc->aper_size;
707 mc->mc_vram_size = mc->aper_size;
708 }
709 mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
710 if (limit && limit < mc->real_vram_size)
711 mc->real_vram_size = limit;
712 dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
713 mc->mc_vram_size >> 20, mc->vram_start,
714 mc->vram_end, mc->real_vram_size >> 20);
715}
716
717/**
Christian König6f02a692017-07-07 11:56:59 +0200718 * amdgpu_gart_location - try to find GTT location
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400719 * @adev: amdgpu device structure holding all necessary informations
720 * @mc: memory controller structure holding memory informations
721 *
722 * Function will place try to place GTT before or after VRAM.
723 *
724 * If GTT size is bigger than space left then we ajust GTT size.
725 * Thus function will never fails.
726 *
727 * FIXME: when reducing GTT size align new size on power of 2.
728 */
Christian König6f02a692017-07-07 11:56:59 +0200729void amdgpu_gart_location(struct amdgpu_device *adev, struct amdgpu_mc *mc)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400730{
731 u64 size_af, size_bf;
732
Christian Königed21c042017-07-06 22:26:05 +0200733 size_af = adev->mc.mc_mask - mc->vram_end;
734 size_bf = mc->vram_start;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400735 if (size_bf > size_af) {
Christian König6f02a692017-07-07 11:56:59 +0200736 if (mc->gart_size > size_bf) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400737 dev_warn(adev->dev, "limiting GTT\n");
Christian König6f02a692017-07-07 11:56:59 +0200738 mc->gart_size = size_bf;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400739 }
Christian König6f02a692017-07-07 11:56:59 +0200740 mc->gart_start = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400741 } else {
Christian König6f02a692017-07-07 11:56:59 +0200742 if (mc->gart_size > size_af) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400743 dev_warn(adev->dev, "limiting GTT\n");
Christian König6f02a692017-07-07 11:56:59 +0200744 mc->gart_size = size_af;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400745 }
Christian König6f02a692017-07-07 11:56:59 +0200746 mc->gart_start = mc->vram_end + 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400747 }
Christian König6f02a692017-07-07 11:56:59 +0200748 mc->gart_end = mc->gart_start + mc->gart_size - 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400749 dev_info(adev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
Christian König6f02a692017-07-07 11:56:59 +0200750 mc->gart_size >> 20, mc->gart_start, mc->gart_end);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400751}
752
753/*
754 * GPU helpers function.
755 */
756/**
Jim Quc836fec2017-02-10 15:59:59 +0800757 * amdgpu_need_post - check if the hw need post or not
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400758 *
759 * @adev: amdgpu_device pointer
760 *
Jim Quc836fec2017-02-10 15:59:59 +0800761 * Check if the asic has been initialized (all asics) at driver startup
762 * or post is needed if hw reset is performed.
763 * Returns true if need or false if not.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400764 */
Jim Quc836fec2017-02-10 15:59:59 +0800765bool amdgpu_need_post(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400766{
767 uint32_t reg;
768
Jim Quc836fec2017-02-10 15:59:59 +0800769 if (adev->has_hw_reset) {
770 adev->has_hw_reset = false;
771 return true;
772 }
Alex Deucher70d17a22017-06-30 17:26:47 -0400773
774 /* bios scratch used on CIK+ */
775 if (adev->asic_type >= CHIP_BONAIRE)
776 return amdgpu_atombios_scratch_need_asic_init(adev);
777
778 /* check MEM_SIZE for older asics */
Alex Deucherbbf282d2017-03-03 17:26:10 -0500779 reg = amdgpu_asic_get_config_memsize(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400780
Alex Deucherf2713e82017-03-28 12:19:31 -0400781 if ((reg != 0) && (reg != 0xffffffff))
Jim Quc836fec2017-02-10 15:59:59 +0800782 return false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400783
Jim Quc836fec2017-02-10 15:59:59 +0800784 return true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400785
786}
787
Monk Liubec86372016-09-14 19:38:08 +0800788static bool amdgpu_vpost_needed(struct amdgpu_device *adev)
789{
790 if (amdgpu_sriov_vf(adev))
791 return false;
792
793 if (amdgpu_passthrough(adev)) {
Monk Liu1da2c322016-11-11 11:24:29 +0800794 /* for FIJI: In whole GPU pass-through virtualization case, after VM reboot
795 * some old smc fw still need driver do vPost otherwise gpu hang, while
796 * those smc fw version above 22.15 doesn't have this flaw, so we force
797 * vpost executed for smc version below 22.15
Monk Liubec86372016-09-14 19:38:08 +0800798 */
799 if (adev->asic_type == CHIP_FIJI) {
800 int err;
801 uint32_t fw_ver;
802 err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
803 /* force vPost if error occured */
804 if (err)
805 return true;
806
807 fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
Monk Liu1da2c322016-11-11 11:24:29 +0800808 if (fw_ver < 0x00160e00)
809 return true;
Monk Liubec86372016-09-14 19:38:08 +0800810 }
Monk Liubec86372016-09-14 19:38:08 +0800811 }
Jim Quc836fec2017-02-10 15:59:59 +0800812 return amdgpu_need_post(adev);
Monk Liubec86372016-09-14 19:38:08 +0800813}
814
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400815/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400816 * amdgpu_dummy_page_init - init dummy page used by the driver
817 *
818 * @adev: amdgpu_device pointer
819 *
820 * Allocate the dummy page used by the driver (all asics).
821 * This dummy page is used by the driver as a filler for gart entries
822 * when pages are taken out of the GART
823 * Returns 0 on sucess, -ENOMEM on failure.
824 */
825int amdgpu_dummy_page_init(struct amdgpu_device *adev)
826{
827 if (adev->dummy_page.page)
828 return 0;
829 adev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
830 if (adev->dummy_page.page == NULL)
831 return -ENOMEM;
832 adev->dummy_page.addr = pci_map_page(adev->pdev, adev->dummy_page.page,
833 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
834 if (pci_dma_mapping_error(adev->pdev, adev->dummy_page.addr)) {
835 dev_err(&adev->pdev->dev, "Failed to DMA MAP the dummy page\n");
836 __free_page(adev->dummy_page.page);
837 adev->dummy_page.page = NULL;
838 return -ENOMEM;
839 }
840 return 0;
841}
842
843/**
844 * amdgpu_dummy_page_fini - free dummy page used by the driver
845 *
846 * @adev: amdgpu_device pointer
847 *
848 * Frees the dummy page used by the driver (all asics).
849 */
850void amdgpu_dummy_page_fini(struct amdgpu_device *adev)
851{
852 if (adev->dummy_page.page == NULL)
853 return;
854 pci_unmap_page(adev->pdev, adev->dummy_page.addr,
855 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
856 __free_page(adev->dummy_page.page);
857 adev->dummy_page.page = NULL;
858}
859
860
861/* ATOM accessor methods */
862/*
863 * ATOM is an interpreted byte code stored in tables in the vbios. The
864 * driver registers callbacks to access registers and the interpreter
865 * in the driver parses the tables and executes then to program specific
866 * actions (set display modes, asic init, etc.). See amdgpu_atombios.c,
867 * atombios.h, and atom.c
868 */
869
870/**
871 * cail_pll_read - read PLL register
872 *
873 * @info: atom card_info pointer
874 * @reg: PLL register offset
875 *
876 * Provides a PLL register accessor for the atom interpreter (r4xx+).
877 * Returns the value of the PLL register.
878 */
879static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
880{
881 return 0;
882}
883
884/**
885 * cail_pll_write - write PLL register
886 *
887 * @info: atom card_info pointer
888 * @reg: PLL register offset
889 * @val: value to write to the pll register
890 *
891 * Provides a PLL register accessor for the atom interpreter (r4xx+).
892 */
893static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
894{
895
896}
897
898/**
899 * cail_mc_read - read MC (Memory Controller) register
900 *
901 * @info: atom card_info pointer
902 * @reg: MC register offset
903 *
904 * Provides an MC register accessor for the atom interpreter (r4xx+).
905 * Returns the value of the MC register.
906 */
907static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
908{
909 return 0;
910}
911
912/**
913 * cail_mc_write - write MC (Memory Controller) register
914 *
915 * @info: atom card_info pointer
916 * @reg: MC register offset
917 * @val: value to write to the pll register
918 *
919 * Provides a MC register accessor for the atom interpreter (r4xx+).
920 */
921static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
922{
923
924}
925
926/**
927 * cail_reg_write - write MMIO register
928 *
929 * @info: atom card_info pointer
930 * @reg: MMIO register offset
931 * @val: value to write to the pll register
932 *
933 * Provides a MMIO register accessor for the atom interpreter (r4xx+).
934 */
935static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
936{
937 struct amdgpu_device *adev = info->dev->dev_private;
938
939 WREG32(reg, val);
940}
941
942/**
943 * cail_reg_read - read MMIO register
944 *
945 * @info: atom card_info pointer
946 * @reg: MMIO register offset
947 *
948 * Provides an MMIO register accessor for the atom interpreter (r4xx+).
949 * Returns the value of the MMIO register.
950 */
951static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
952{
953 struct amdgpu_device *adev = info->dev->dev_private;
954 uint32_t r;
955
956 r = RREG32(reg);
957 return r;
958}
959
960/**
961 * cail_ioreg_write - write IO register
962 *
963 * @info: atom card_info pointer
964 * @reg: IO register offset
965 * @val: value to write to the pll register
966 *
967 * Provides a IO register accessor for the atom interpreter (r4xx+).
968 */
969static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
970{
971 struct amdgpu_device *adev = info->dev->dev_private;
972
973 WREG32_IO(reg, val);
974}
975
976/**
977 * cail_ioreg_read - read IO register
978 *
979 * @info: atom card_info pointer
980 * @reg: IO register offset
981 *
982 * Provides an IO register accessor for the atom interpreter (r4xx+).
983 * Returns the value of the IO register.
984 */
985static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
986{
987 struct amdgpu_device *adev = info->dev->dev_private;
988 uint32_t r;
989
990 r = RREG32_IO(reg);
991 return r;
992}
993
994/**
995 * amdgpu_atombios_fini - free the driver info and callbacks for atombios
996 *
997 * @adev: amdgpu_device pointer
998 *
999 * Frees the driver info and register access callbacks for the ATOM
1000 * interpreter (r4xx+).
1001 * Called at driver shutdown.
1002 */
1003static void amdgpu_atombios_fini(struct amdgpu_device *adev)
1004{
Monk Liu89e0ec9f2016-05-27 19:34:11 +08001005 if (adev->mode_info.atom_context) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001006 kfree(adev->mode_info.atom_context->scratch);
Monk Liu89e0ec9f2016-05-27 19:34:11 +08001007 kfree(adev->mode_info.atom_context->iio);
1008 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001009 kfree(adev->mode_info.atom_context);
1010 adev->mode_info.atom_context = NULL;
1011 kfree(adev->mode_info.atom_card_info);
1012 adev->mode_info.atom_card_info = NULL;
1013}
1014
1015/**
1016 * amdgpu_atombios_init - init the driver info and callbacks for atombios
1017 *
1018 * @adev: amdgpu_device pointer
1019 *
1020 * Initializes the driver info and register access callbacks for the
1021 * ATOM interpreter (r4xx+).
1022 * Returns 0 on sucess, -ENOMEM on failure.
1023 * Called at driver startup.
1024 */
1025static int amdgpu_atombios_init(struct amdgpu_device *adev)
1026{
1027 struct card_info *atom_card_info =
1028 kzalloc(sizeof(struct card_info), GFP_KERNEL);
1029
1030 if (!atom_card_info)
1031 return -ENOMEM;
1032
1033 adev->mode_info.atom_card_info = atom_card_info;
1034 atom_card_info->dev = adev->ddev;
1035 atom_card_info->reg_read = cail_reg_read;
1036 atom_card_info->reg_write = cail_reg_write;
1037 /* needed for iio ops */
1038 if (adev->rio_mem) {
1039 atom_card_info->ioreg_read = cail_ioreg_read;
1040 atom_card_info->ioreg_write = cail_ioreg_write;
1041 } else {
Amber Linb64a18c2017-01-04 08:06:58 -05001042 DRM_INFO("PCI I/O BAR is not found. Using MMIO to access ATOM BIOS\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001043 atom_card_info->ioreg_read = cail_reg_read;
1044 atom_card_info->ioreg_write = cail_reg_write;
1045 }
1046 atom_card_info->mc_read = cail_mc_read;
1047 atom_card_info->mc_write = cail_mc_write;
1048 atom_card_info->pll_read = cail_pll_read;
1049 atom_card_info->pll_write = cail_pll_write;
1050
1051 adev->mode_info.atom_context = amdgpu_atom_parse(atom_card_info, adev->bios);
1052 if (!adev->mode_info.atom_context) {
1053 amdgpu_atombios_fini(adev);
1054 return -ENOMEM;
1055 }
1056
1057 mutex_init(&adev->mode_info.atom_context->mutex);
Alex Deuchera5bde2f2016-09-23 16:23:41 -04001058 if (adev->is_atom_fw) {
1059 amdgpu_atomfirmware_scratch_regs_init(adev);
1060 amdgpu_atomfirmware_allocate_fb_scratch(adev);
1061 } else {
1062 amdgpu_atombios_scratch_regs_init(adev);
1063 amdgpu_atombios_allocate_fb_scratch(adev);
1064 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001065 return 0;
1066}
1067
1068/* if we get transitioned to only one device, take VGA back */
1069/**
1070 * amdgpu_vga_set_decode - enable/disable vga decode
1071 *
1072 * @cookie: amdgpu_device pointer
1073 * @state: enable/disable vga decode
1074 *
1075 * Enable/disable vga decode (all asics).
1076 * Returns VGA resource flags.
1077 */
1078static unsigned int amdgpu_vga_set_decode(void *cookie, bool state)
1079{
1080 struct amdgpu_device *adev = cookie;
1081 amdgpu_asic_set_vga_state(adev, state);
1082 if (state)
1083 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
1084 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1085 else
1086 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1087}
1088
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001089static void amdgpu_check_block_size(struct amdgpu_device *adev)
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001090{
1091 /* defines number of bits in page table versus page directory,
1092 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
1093 * page table and the remaining bits are in the page directory */
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001094 if (amdgpu_vm_block_size == -1)
1095 return;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001096
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001097 if (amdgpu_vm_block_size < 9) {
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001098 dev_warn(adev->dev, "VM page table size (%d) too small\n",
1099 amdgpu_vm_block_size);
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001100 goto def_value;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001101 }
1102
1103 if (amdgpu_vm_block_size > 24 ||
1104 (amdgpu_vm_size * 1024) < (1ull << amdgpu_vm_block_size)) {
1105 dev_warn(adev->dev, "VM page table size (%d) too large\n",
1106 amdgpu_vm_block_size);
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001107 goto def_value;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001108 }
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001109
1110 return;
1111
1112def_value:
1113 amdgpu_vm_block_size = -1;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001114}
1115
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001116static void amdgpu_check_vm_size(struct amdgpu_device *adev)
1117{
Alex Deucher64dab072017-06-15 18:20:09 -04001118 /* no need to check the default value */
1119 if (amdgpu_vm_size == -1)
1120 return;
1121
Alex Deucher76117502017-06-21 12:31:41 -04001122 if (!is_power_of_2(amdgpu_vm_size)) {
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001123 dev_warn(adev->dev, "VM size (%d) must be a power of 2\n",
1124 amdgpu_vm_size);
1125 goto def_value;
1126 }
1127
1128 if (amdgpu_vm_size < 1) {
1129 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
1130 amdgpu_vm_size);
1131 goto def_value;
1132 }
1133
1134 /*
1135 * Max GPUVM size for Cayman, SI, CI VI are 40 bits.
1136 */
1137 if (amdgpu_vm_size > 1024) {
1138 dev_warn(adev->dev, "VM size (%d) too large, max is 1TB\n",
1139 amdgpu_vm_size);
1140 goto def_value;
1141 }
1142
1143 return;
1144
1145def_value:
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001146 amdgpu_vm_size = -1;
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001147}
1148
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001149/**
1150 * amdgpu_check_arguments - validate module params
1151 *
1152 * @adev: amdgpu_device pointer
1153 *
1154 * Validates certain module parameters and updates
1155 * the associated values used by the driver (all asics).
1156 */
1157static void amdgpu_check_arguments(struct amdgpu_device *adev)
1158{
Chunming Zhou5b011232015-12-10 17:34:33 +08001159 if (amdgpu_sched_jobs < 4) {
1160 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
1161 amdgpu_sched_jobs);
1162 amdgpu_sched_jobs = 4;
Alex Deucher76117502017-06-21 12:31:41 -04001163 } else if (!is_power_of_2(amdgpu_sched_jobs)){
Chunming Zhou5b011232015-12-10 17:34:33 +08001164 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
1165 amdgpu_sched_jobs);
1166 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
1167 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001168
Christian Königf9321cc2017-07-07 13:44:05 +02001169 if (amdgpu_gart_size < 32) {
1170 /* gart size must be greater or equal to 32M */
1171 dev_warn(adev->dev, "gart size (%d) too small\n",
1172 amdgpu_gart_size);
1173 amdgpu_gart_size = 32;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001174 }
1175
Christian König36d38372017-07-07 13:17:45 +02001176 if (amdgpu_gtt_size != -1 && amdgpu_gtt_size < 32) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001177 /* gtt size must be greater or equal to 32M */
Christian König36d38372017-07-07 13:17:45 +02001178 dev_warn(adev->dev, "gtt size (%d) too small\n",
1179 amdgpu_gtt_size);
1180 amdgpu_gtt_size = -1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001181 }
1182
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001183 amdgpu_check_vm_size(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001184
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001185 amdgpu_check_block_size(adev);
Christian König6a7f76e2016-08-24 15:51:49 +02001186
jimqu526bae32016-11-07 09:53:10 +08001187 if (amdgpu_vram_page_split != -1 && (amdgpu_vram_page_split < 16 ||
Alex Deucher76117502017-06-21 12:31:41 -04001188 !is_power_of_2(amdgpu_vram_page_split))) {
Christian König6a7f76e2016-08-24 15:51:49 +02001189 dev_warn(adev->dev, "invalid VRAM page split (%d)\n",
1190 amdgpu_vram_page_split);
1191 amdgpu_vram_page_split = 1024;
1192 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001193}
1194
1195/**
1196 * amdgpu_switcheroo_set_state - set switcheroo state
1197 *
1198 * @pdev: pci dev pointer
Lukas Wunner16944672015-09-05 11:17:35 +02001199 * @state: vga_switcheroo state
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001200 *
1201 * Callback for the switcheroo driver. Suspends or resumes the
1202 * the asics before or after it is powered up using ACPI methods.
1203 */
1204static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1205{
1206 struct drm_device *dev = pci_get_drvdata(pdev);
1207
1208 if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
1209 return;
1210
1211 if (state == VGA_SWITCHEROO_ON) {
Joe Perches7ca85292017-02-28 04:55:52 -08001212 pr_info("amdgpu: switched on\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001213 /* don't suspend or resume card normally */
1214 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1215
Alex Deucher810ddc32016-08-23 13:25:49 -04001216 amdgpu_device_resume(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001217
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001218 dev->switch_power_state = DRM_SWITCH_POWER_ON;
1219 drm_kms_helper_poll_enable(dev);
1220 } else {
Joe Perches7ca85292017-02-28 04:55:52 -08001221 pr_info("amdgpu: switched off\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001222 drm_kms_helper_poll_disable(dev);
1223 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Alex Deucher810ddc32016-08-23 13:25:49 -04001224 amdgpu_device_suspend(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001225 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1226 }
1227}
1228
1229/**
1230 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1231 *
1232 * @pdev: pci dev pointer
1233 *
1234 * Callback for the switcheroo driver. Check of the switcheroo
1235 * state can be changed.
1236 * Returns true if the state can be changed, false if not.
1237 */
1238static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1239{
1240 struct drm_device *dev = pci_get_drvdata(pdev);
1241
1242 /*
1243 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1244 * locking inversion with the driver load path. And the access here is
1245 * completely racy anyway. So don't bother with locking for now.
1246 */
1247 return dev->open_count == 0;
1248}
1249
1250static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1251 .set_gpu_state = amdgpu_switcheroo_set_state,
1252 .reprobe = NULL,
1253 .can_switch = amdgpu_switcheroo_can_switch,
1254};
1255
1256int amdgpu_set_clockgating_state(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001257 enum amd_ip_block_type block_type,
1258 enum amd_clockgating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001259{
1260 int i, r = 0;
1261
1262 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001263 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001264 continue;
Rex Zhuc7228652017-02-22 15:33:46 +08001265 if (adev->ip_blocks[i].version->type != block_type)
1266 continue;
1267 if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
1268 continue;
1269 r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
1270 (void *)adev, state);
1271 if (r)
1272 DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
1273 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001274 }
1275 return r;
1276}
1277
1278int amdgpu_set_powergating_state(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001279 enum amd_ip_block_type block_type,
1280 enum amd_powergating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001281{
1282 int i, r = 0;
1283
1284 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001285 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001286 continue;
Rex Zhuc7228652017-02-22 15:33:46 +08001287 if (adev->ip_blocks[i].version->type != block_type)
1288 continue;
1289 if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
1290 continue;
1291 r = adev->ip_blocks[i].version->funcs->set_powergating_state(
1292 (void *)adev, state);
1293 if (r)
1294 DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
1295 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001296 }
1297 return r;
1298}
1299
Huang Rui6cb2d4e2017-01-05 18:44:41 +08001300void amdgpu_get_clockgating_state(struct amdgpu_device *adev, u32 *flags)
1301{
1302 int i;
1303
1304 for (i = 0; i < adev->num_ip_blocks; i++) {
1305 if (!adev->ip_blocks[i].status.valid)
1306 continue;
1307 if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
1308 adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
1309 }
1310}
1311
Alex Deucher5dbbb602016-06-23 11:41:04 -04001312int amdgpu_wait_for_idle(struct amdgpu_device *adev,
1313 enum amd_ip_block_type block_type)
1314{
1315 int i, r;
1316
1317 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001318 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001319 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001320 if (adev->ip_blocks[i].version->type == block_type) {
1321 r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -04001322 if (r)
1323 return r;
1324 break;
1325 }
1326 }
1327 return 0;
1328
1329}
1330
1331bool amdgpu_is_idle(struct amdgpu_device *adev,
1332 enum amd_ip_block_type block_type)
1333{
1334 int i;
1335
1336 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001337 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001338 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001339 if (adev->ip_blocks[i].version->type == block_type)
1340 return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -04001341 }
1342 return true;
1343
1344}
1345
Alex Deuchera1255102016-10-13 17:41:13 -04001346struct amdgpu_ip_block * amdgpu_get_ip_block(struct amdgpu_device *adev,
1347 enum amd_ip_block_type type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001348{
1349 int i;
1350
1351 for (i = 0; i < adev->num_ip_blocks; i++)
Alex Deuchera1255102016-10-13 17:41:13 -04001352 if (adev->ip_blocks[i].version->type == type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001353 return &adev->ip_blocks[i];
1354
1355 return NULL;
1356}
1357
1358/**
1359 * amdgpu_ip_block_version_cmp
1360 *
1361 * @adev: amdgpu_device pointer
yanyang15fc3aee2015-05-22 14:39:35 -04001362 * @type: enum amd_ip_block_type
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001363 * @major: major version
1364 * @minor: minor version
1365 *
1366 * return 0 if equal or greater
1367 * return 1 if smaller or the ip_block doesn't exist
1368 */
1369int amdgpu_ip_block_version_cmp(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001370 enum amd_ip_block_type type,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001371 u32 major, u32 minor)
1372{
Alex Deuchera1255102016-10-13 17:41:13 -04001373 struct amdgpu_ip_block *ip_block = amdgpu_get_ip_block(adev, type);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001374
Alex Deuchera1255102016-10-13 17:41:13 -04001375 if (ip_block && ((ip_block->version->major > major) ||
1376 ((ip_block->version->major == major) &&
1377 (ip_block->version->minor >= minor))))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001378 return 0;
1379
1380 return 1;
1381}
1382
Alex Deuchera1255102016-10-13 17:41:13 -04001383/**
1384 * amdgpu_ip_block_add
1385 *
1386 * @adev: amdgpu_device pointer
1387 * @ip_block_version: pointer to the IP to add
1388 *
1389 * Adds the IP block driver information to the collection of IPs
1390 * on the asic.
1391 */
1392int amdgpu_ip_block_add(struct amdgpu_device *adev,
1393 const struct amdgpu_ip_block_version *ip_block_version)
1394{
1395 if (!ip_block_version)
1396 return -EINVAL;
1397
Huang Ruia0bae352017-05-03 09:52:06 +08001398 DRM_DEBUG("add ip block number %d <%s>\n", adev->num_ip_blocks,
1399 ip_block_version->funcs->name);
1400
Alex Deuchera1255102016-10-13 17:41:13 -04001401 adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
1402
1403 return 0;
1404}
1405
Alex Deucher483ef982016-09-30 12:43:04 -04001406static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
Emily Deng9accf2f2016-08-10 16:01:25 +08001407{
1408 adev->enable_virtual_display = false;
1409
1410 if (amdgpu_virtual_display) {
1411 struct drm_device *ddev = adev->ddev;
1412 const char *pci_address_name = pci_name(ddev->pdev);
Emily Deng0f663562016-09-30 13:02:18 -04001413 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
Emily Deng9accf2f2016-08-10 16:01:25 +08001414
1415 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1416 pciaddstr_tmp = pciaddstr;
Emily Deng0f663562016-09-30 13:02:18 -04001417 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1418 pciaddname = strsep(&pciaddname_tmp, ",");
Yintian Tao967de2a2017-01-22 15:16:51 +08001419 if (!strcmp("all", pciaddname)
1420 || !strcmp(pci_address_name, pciaddname)) {
Emily Deng0f663562016-09-30 13:02:18 -04001421 long num_crtc;
1422 int res = -1;
1423
Emily Deng9accf2f2016-08-10 16:01:25 +08001424 adev->enable_virtual_display = true;
Emily Deng0f663562016-09-30 13:02:18 -04001425
1426 if (pciaddname_tmp)
1427 res = kstrtol(pciaddname_tmp, 10,
1428 &num_crtc);
1429
1430 if (!res) {
1431 if (num_crtc < 1)
1432 num_crtc = 1;
1433 if (num_crtc > 6)
1434 num_crtc = 6;
1435 adev->mode_info.num_crtc = num_crtc;
1436 } else {
1437 adev->mode_info.num_crtc = 1;
1438 }
Emily Deng9accf2f2016-08-10 16:01:25 +08001439 break;
1440 }
1441 }
1442
Emily Deng0f663562016-09-30 13:02:18 -04001443 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
1444 amdgpu_virtual_display, pci_address_name,
1445 adev->enable_virtual_display, adev->mode_info.num_crtc);
Emily Deng9accf2f2016-08-10 16:01:25 +08001446
1447 kfree(pciaddstr);
1448 }
1449}
1450
Alex Deuchere2a75f82017-04-27 16:58:01 -04001451static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev)
1452{
Alex Deuchere2a75f82017-04-27 16:58:01 -04001453 const char *chip_name;
1454 char fw_name[30];
1455 int err;
1456 const struct gpu_info_firmware_header_v1_0 *hdr;
1457
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001458 adev->firmware.gpu_info_fw = NULL;
1459
Alex Deuchere2a75f82017-04-27 16:58:01 -04001460 switch (adev->asic_type) {
1461 case CHIP_TOPAZ:
1462 case CHIP_TONGA:
1463 case CHIP_FIJI:
1464 case CHIP_POLARIS11:
1465 case CHIP_POLARIS10:
1466 case CHIP_POLARIS12:
1467 case CHIP_CARRIZO:
1468 case CHIP_STONEY:
1469#ifdef CONFIG_DRM_AMDGPU_SI
1470 case CHIP_VERDE:
1471 case CHIP_TAHITI:
1472 case CHIP_PITCAIRN:
1473 case CHIP_OLAND:
1474 case CHIP_HAINAN:
1475#endif
1476#ifdef CONFIG_DRM_AMDGPU_CIK
1477 case CHIP_BONAIRE:
1478 case CHIP_HAWAII:
1479 case CHIP_KAVERI:
1480 case CHIP_KABINI:
1481 case CHIP_MULLINS:
1482#endif
1483 default:
1484 return 0;
1485 case CHIP_VEGA10:
1486 chip_name = "vega10";
1487 break;
Alex Deucher2d2e5e72017-05-09 12:27:35 -04001488 case CHIP_RAVEN:
1489 chip_name = "raven";
1490 break;
Alex Deuchere2a75f82017-04-27 16:58:01 -04001491 }
1492
1493 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name);
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001494 err = request_firmware(&adev->firmware.gpu_info_fw, fw_name, adev->dev);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001495 if (err) {
1496 dev_err(adev->dev,
1497 "Failed to load gpu_info firmware \"%s\"\n",
1498 fw_name);
1499 goto out;
1500 }
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001501 err = amdgpu_ucode_validate(adev->firmware.gpu_info_fw);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001502 if (err) {
1503 dev_err(adev->dev,
1504 "Failed to validate gpu_info firmware \"%s\"\n",
1505 fw_name);
1506 goto out;
1507 }
1508
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001509 hdr = (const struct gpu_info_firmware_header_v1_0 *)adev->firmware.gpu_info_fw->data;
Alex Deuchere2a75f82017-04-27 16:58:01 -04001510 amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
1511
1512 switch (hdr->version_major) {
1513 case 1:
1514 {
1515 const struct gpu_info_firmware_v1_0 *gpu_info_fw =
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001516 (const struct gpu_info_firmware_v1_0 *)(adev->firmware.gpu_info_fw->data +
Alex Deuchere2a75f82017-04-27 16:58:01 -04001517 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1518
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001519 adev->gfx.config.max_shader_engines = le32_to_cpu(gpu_info_fw->gc_num_se);
1520 adev->gfx.config.max_cu_per_sh = le32_to_cpu(gpu_info_fw->gc_num_cu_per_sh);
1521 adev->gfx.config.max_sh_per_se = le32_to_cpu(gpu_info_fw->gc_num_sh_per_se);
1522 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 -04001523 adev->gfx.config.max_texture_channel_caches =
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001524 le32_to_cpu(gpu_info_fw->gc_num_tccs);
1525 adev->gfx.config.max_gprs = le32_to_cpu(gpu_info_fw->gc_num_gprs);
1526 adev->gfx.config.max_gs_threads = le32_to_cpu(gpu_info_fw->gc_num_max_gs_thds);
1527 adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gpu_info_fw->gc_gs_table_depth);
1528 adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gpu_info_fw->gc_gsprim_buff_depth);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001529 adev->gfx.config.double_offchip_lds_buf =
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001530 le32_to_cpu(gpu_info_fw->gc_double_offchip_lds_buffer);
1531 adev->gfx.cu_info.wave_front_size = le32_to_cpu(gpu_info_fw->gc_wave_size);
Hawking Zhang51fd0372017-06-09 22:30:52 +08001532 adev->gfx.cu_info.max_waves_per_simd =
1533 le32_to_cpu(gpu_info_fw->gc_max_waves_per_simd);
1534 adev->gfx.cu_info.max_scratch_slots_per_cu =
1535 le32_to_cpu(gpu_info_fw->gc_max_scratch_slots_per_cu);
1536 adev->gfx.cu_info.lds_size = le32_to_cpu(gpu_info_fw->gc_lds_size);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001537 break;
1538 }
1539 default:
1540 dev_err(adev->dev,
1541 "Unsupported gpu_info table %d\n", hdr->header.ucode_version);
1542 err = -EINVAL;
1543 goto out;
1544 }
1545out:
Alex Deuchere2a75f82017-04-27 16:58:01 -04001546 return err;
1547}
1548
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001549static int amdgpu_early_init(struct amdgpu_device *adev)
1550{
Alex Deucheraaa36a92015-04-20 17:31:14 -04001551 int i, r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001552
Alex Deucher483ef982016-09-30 12:43:04 -04001553 amdgpu_device_enable_virtual_display(adev);
Emily Denga6be7572016-08-08 11:37:50 +08001554
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001555 switch (adev->asic_type) {
Alex Deucheraaa36a92015-04-20 17:31:14 -04001556 case CHIP_TOPAZ:
1557 case CHIP_TONGA:
David Zhang48299f92015-07-08 01:05:16 +08001558 case CHIP_FIJI:
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001559 case CHIP_POLARIS11:
1560 case CHIP_POLARIS10:
Junwei Zhangc4642a42016-12-14 15:32:28 -05001561 case CHIP_POLARIS12:
Alex Deucheraaa36a92015-04-20 17:31:14 -04001562 case CHIP_CARRIZO:
Samuel Li39bb0c92015-10-08 16:31:43 -04001563 case CHIP_STONEY:
1564 if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
Alex Deucheraaa36a92015-04-20 17:31:14 -04001565 adev->family = AMDGPU_FAMILY_CZ;
1566 else
1567 adev->family = AMDGPU_FAMILY_VI;
1568
1569 r = vi_set_ip_blocks(adev);
1570 if (r)
1571 return r;
1572 break;
Ken Wang33f34802016-01-21 17:29:41 +08001573#ifdef CONFIG_DRM_AMDGPU_SI
1574 case CHIP_VERDE:
1575 case CHIP_TAHITI:
1576 case CHIP_PITCAIRN:
1577 case CHIP_OLAND:
1578 case CHIP_HAINAN:
Ken Wang295d0da2016-05-24 21:02:53 +08001579 adev->family = AMDGPU_FAMILY_SI;
Ken Wang33f34802016-01-21 17:29:41 +08001580 r = si_set_ip_blocks(adev);
1581 if (r)
1582 return r;
1583 break;
1584#endif
Alex Deuchera2e73f52015-04-20 17:09:27 -04001585#ifdef CONFIG_DRM_AMDGPU_CIK
1586 case CHIP_BONAIRE:
1587 case CHIP_HAWAII:
1588 case CHIP_KAVERI:
1589 case CHIP_KABINI:
1590 case CHIP_MULLINS:
1591 if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
1592 adev->family = AMDGPU_FAMILY_CI;
1593 else
1594 adev->family = AMDGPU_FAMILY_KV;
1595
1596 r = cik_set_ip_blocks(adev);
1597 if (r)
1598 return r;
1599 break;
1600#endif
Chunming Zhou2ca8a5d2016-12-07 17:31:19 +08001601 case CHIP_VEGA10:
1602 case CHIP_RAVEN:
1603 if (adev->asic_type == CHIP_RAVEN)
1604 adev->family = AMDGPU_FAMILY_RV;
1605 else
1606 adev->family = AMDGPU_FAMILY_AI;
Ken Wang460826e2017-03-06 14:53:16 -05001607
1608 r = soc15_set_ip_blocks(adev);
1609 if (r)
1610 return r;
1611 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001612 default:
1613 /* FIXME: not supported yet */
1614 return -EINVAL;
1615 }
1616
Alex Deuchere2a75f82017-04-27 16:58:01 -04001617 r = amdgpu_device_parse_gpu_info_fw(adev);
1618 if (r)
1619 return r;
1620
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001621 if (amdgpu_sriov_vf(adev)) {
1622 r = amdgpu_virt_request_full_gpu(adev, true);
1623 if (r)
1624 return r;
1625 }
1626
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001627 for (i = 0; i < adev->num_ip_blocks; i++) {
1628 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
Huang Ruied8cf002017-05-03 09:40:17 +08001629 DRM_ERROR("disabled ip block: %d <%s>\n",
1630 i, adev->ip_blocks[i].version->funcs->name);
Alex Deuchera1255102016-10-13 17:41:13 -04001631 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001632 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001633 if (adev->ip_blocks[i].version->funcs->early_init) {
1634 r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001635 if (r == -ENOENT) {
Alex Deuchera1255102016-10-13 17:41:13 -04001636 adev->ip_blocks[i].status.valid = false;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001637 } else if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001638 DRM_ERROR("early_init of IP block <%s> failed %d\n",
1639 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001640 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001641 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001642 adev->ip_blocks[i].status.valid = true;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001643 }
Alex Deucher974e6b62015-07-10 13:59:44 -04001644 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001645 adev->ip_blocks[i].status.valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001646 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001647 }
1648 }
1649
Nicolai Hähnle395d1fb2016-06-02 12:32:07 +02001650 adev->cg_flags &= amdgpu_cg_mask;
1651 adev->pg_flags &= amdgpu_pg_mask;
1652
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001653 return 0;
1654}
1655
1656static int amdgpu_init(struct amdgpu_device *adev)
1657{
1658 int i, r;
1659
1660 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001661 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001662 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001663 r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001664 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001665 DRM_ERROR("sw_init of IP block <%s> failed %d\n",
1666 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001667 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001668 }
Alex Deuchera1255102016-10-13 17:41:13 -04001669 adev->ip_blocks[i].status.sw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001670 /* need to do gmc hw init early so we can allocate gpu mem */
Alex Deuchera1255102016-10-13 17:41:13 -04001671 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001672 r = amdgpu_vram_scratch_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001673 if (r) {
1674 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", 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 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001678 if (r) {
1679 DRM_ERROR("hw_init %d failed %d\n", i, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001680 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001681 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001682 r = amdgpu_wb_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001683 if (r) {
1684 DRM_ERROR("amdgpu_wb_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001685 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001686 }
Alex Deuchera1255102016-10-13 17:41:13 -04001687 adev->ip_blocks[i].status.hw = true;
Monk Liu24936642017-01-09 15:54:32 +08001688
1689 /* right after GMC hw init, we create CSA */
1690 if (amdgpu_sriov_vf(adev)) {
1691 r = amdgpu_allocate_static_csa(adev);
1692 if (r) {
1693 DRM_ERROR("allocate CSA failed %d\n", r);
1694 return r;
1695 }
1696 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001697 }
1698 }
1699
1700 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001701 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001702 continue;
1703 /* gmc hw init is done early */
Alex Deuchera1255102016-10-13 17:41:13 -04001704 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001705 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001706 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001707 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001708 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
1709 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001710 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001711 }
Alex Deuchera1255102016-10-13 17:41:13 -04001712 adev->ip_blocks[i].status.hw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001713 }
1714
1715 return 0;
1716}
1717
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08001718static void amdgpu_fill_reset_magic(struct amdgpu_device *adev)
1719{
1720 memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM);
1721}
1722
1723static bool amdgpu_check_vram_lost(struct amdgpu_device *adev)
1724{
1725 return !!memcmp(adev->gart.ptr, adev->reset_magic,
1726 AMDGPU_RESET_MAGIC_NUM);
1727}
1728
Shirish S2dc80b02017-05-25 10:05:25 +05301729static int amdgpu_late_set_cg_state(struct amdgpu_device *adev)
1730{
1731 int i = 0, r;
1732
1733 for (i = 0; i < adev->num_ip_blocks; i++) {
1734 if (!adev->ip_blocks[i].status.valid)
1735 continue;
1736 /* skip CG for VCE/UVD, it's handled specially */
1737 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1738 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1739 /* enable clockgating to save power */
1740 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1741 AMD_CG_STATE_GATE);
1742 if (r) {
1743 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
1744 adev->ip_blocks[i].version->funcs->name, r);
1745 return r;
1746 }
1747 }
1748 }
1749 return 0;
1750}
1751
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001752static int amdgpu_late_init(struct amdgpu_device *adev)
1753{
1754 int i = 0, r;
1755
1756 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001757 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001758 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001759 if (adev->ip_blocks[i].version->funcs->late_init) {
1760 r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001761 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001762 DRM_ERROR("late_init of IP block <%s> failed %d\n",
1763 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001764 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001765 }
Alex Deuchera1255102016-10-13 17:41:13 -04001766 adev->ip_blocks[i].status.late_initialized = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001767 }
1768 }
1769
Shirish S2dc80b02017-05-25 10:05:25 +05301770 mod_delayed_work(system_wq, &adev->late_init_work,
1771 msecs_to_jiffies(AMDGPU_RESUME_MS));
1772
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08001773 amdgpu_fill_reset_magic(adev);
1774
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001775 return 0;
1776}
1777
1778static int amdgpu_fini(struct amdgpu_device *adev)
1779{
1780 int i, r;
1781
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001782 /* need to disable SMC first */
1783 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001784 if (!adev->ip_blocks[i].status.hw)
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001785 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001786 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001787 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
Alex Deuchera1255102016-10-13 17:41:13 -04001788 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1789 AMD_CG_STATE_UNGATE);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001790 if (r) {
1791 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001792 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001793 return r;
1794 }
Alex Deuchera1255102016-10-13 17:41:13 -04001795 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001796 /* XXX handle errors */
1797 if (r) {
1798 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001799 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001800 }
Alex Deuchera1255102016-10-13 17:41:13 -04001801 adev->ip_blocks[i].status.hw = false;
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001802 break;
1803 }
1804 }
1805
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001806 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001807 if (!adev->ip_blocks[i].status.hw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001808 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001809 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001810 amdgpu_wb_fini(adev);
1811 amdgpu_vram_scratch_fini(adev);
1812 }
Rex Zhu8201a672016-11-24 21:44:44 +08001813
1814 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1815 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1816 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
1817 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1818 AMD_CG_STATE_UNGATE);
1819 if (r) {
1820 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1821 adev->ip_blocks[i].version->funcs->name, r);
1822 return r;
1823 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001824 }
Rex Zhu8201a672016-11-24 21:44:44 +08001825
Alex Deuchera1255102016-10-13 17:41:13 -04001826 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001827 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001828 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001829 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
1830 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001831 }
Rex Zhu8201a672016-11-24 21:44:44 +08001832
Alex Deuchera1255102016-10-13 17:41:13 -04001833 adev->ip_blocks[i].status.hw = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001834 }
1835
1836 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001837 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001838 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001839 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001840 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001841 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001842 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
1843 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001844 }
Alex Deuchera1255102016-10-13 17:41:13 -04001845 adev->ip_blocks[i].status.sw = false;
1846 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001847 }
1848
Monk Liua6dcfd92016-05-19 14:36:34 +08001849 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001850 if (!adev->ip_blocks[i].status.late_initialized)
Grazvydas Ignotas8a2eef12016-10-03 00:06:44 +03001851 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001852 if (adev->ip_blocks[i].version->funcs->late_fini)
1853 adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
1854 adev->ip_blocks[i].status.late_initialized = false;
Monk Liua6dcfd92016-05-19 14:36:34 +08001855 }
1856
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001857 if (amdgpu_sriov_vf(adev)) {
Monk Liu24936642017-01-09 15:54:32 +08001858 amdgpu_bo_free_kernel(&adev->virt.csa_obj, &adev->virt.csa_vmid0_addr, NULL);
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001859 amdgpu_virt_release_full_gpu(adev, false);
1860 }
Monk Liu24936642017-01-09 15:54:32 +08001861
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001862 return 0;
1863}
1864
Shirish S2dc80b02017-05-25 10:05:25 +05301865static void amdgpu_late_init_func_handler(struct work_struct *work)
1866{
1867 struct amdgpu_device *adev =
1868 container_of(work, struct amdgpu_device, late_init_work.work);
1869 amdgpu_late_set_cg_state(adev);
1870}
1871
Alex Deucherfaefba92016-12-06 10:38:29 -05001872int amdgpu_suspend(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001873{
1874 int i, r;
1875
Xiangliang Yue941ea92017-01-18 12:47:55 +08001876 if (amdgpu_sriov_vf(adev))
1877 amdgpu_virt_request_full_gpu(adev, false);
1878
Flora Cuic5a93a22016-02-26 10:45:25 +08001879 /* ungate SMC block first */
1880 r = amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC,
1881 AMD_CG_STATE_UNGATE);
1882 if (r) {
1883 DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n",r);
1884 }
1885
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001886 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001887 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001888 continue;
1889 /* ungate blocks so that suspend can properly shut them down */
Flora Cuic5a93a22016-02-26 10:45:25 +08001890 if (i != AMD_IP_BLOCK_TYPE_SMC) {
Alex Deuchera1255102016-10-13 17:41:13 -04001891 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1892 AMD_CG_STATE_UNGATE);
Flora Cuic5a93a22016-02-26 10:45:25 +08001893 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001894 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1895 adev->ip_blocks[i].version->funcs->name, r);
Flora Cuic5a93a22016-02-26 10:45:25 +08001896 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001897 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001898 /* XXX handle errors */
Alex Deuchera1255102016-10-13 17:41:13 -04001899 r = adev->ip_blocks[i].version->funcs->suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001900 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001901 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001902 DRM_ERROR("suspend of IP block <%s> failed %d\n",
1903 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001904 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001905 }
1906
Xiangliang Yue941ea92017-01-18 12:47:55 +08001907 if (amdgpu_sriov_vf(adev))
1908 amdgpu_virt_release_full_gpu(adev, false);
1909
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001910 return 0;
1911}
1912
Monk Liue4f0fdc2017-02-09 11:55:49 +08001913static int amdgpu_sriov_reinit_early(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08001914{
1915 int i, r;
1916
Monk Liu2cb681b2017-04-26 12:00:49 +08001917 static enum amd_ip_block_type ip_order[] = {
1918 AMD_IP_BLOCK_TYPE_GMC,
1919 AMD_IP_BLOCK_TYPE_COMMON,
Monk Liu2cb681b2017-04-26 12:00:49 +08001920 AMD_IP_BLOCK_TYPE_IH,
1921 };
Monk Liua90ad3c2017-01-23 14:22:08 +08001922
Monk Liu2cb681b2017-04-26 12:00:49 +08001923 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1924 int j;
1925 struct amdgpu_ip_block *block;
Monk Liua90ad3c2017-01-23 14:22:08 +08001926
Monk Liu2cb681b2017-04-26 12:00:49 +08001927 for (j = 0; j < adev->num_ip_blocks; j++) {
1928 block = &adev->ip_blocks[j];
1929
1930 if (block->version->type != ip_order[i] ||
1931 !block->status.valid)
1932 continue;
1933
1934 r = block->version->funcs->hw_init(adev);
1935 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
Monk Liua90ad3c2017-01-23 14:22:08 +08001936 }
1937 }
1938
1939 return 0;
1940}
1941
Monk Liue4f0fdc2017-02-09 11:55:49 +08001942static int amdgpu_sriov_reinit_late(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08001943{
1944 int i, r;
1945
Monk Liu2cb681b2017-04-26 12:00:49 +08001946 static enum amd_ip_block_type ip_order[] = {
1947 AMD_IP_BLOCK_TYPE_SMC,
1948 AMD_IP_BLOCK_TYPE_DCE,
1949 AMD_IP_BLOCK_TYPE_GFX,
1950 AMD_IP_BLOCK_TYPE_SDMA,
Frank Min257deb82017-06-15 20:07:36 +08001951 AMD_IP_BLOCK_TYPE_UVD,
1952 AMD_IP_BLOCK_TYPE_VCE
Monk Liu2cb681b2017-04-26 12:00:49 +08001953 };
Monk Liua90ad3c2017-01-23 14:22:08 +08001954
Monk Liu2cb681b2017-04-26 12:00:49 +08001955 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1956 int j;
1957 struct amdgpu_ip_block *block;
Monk Liua90ad3c2017-01-23 14:22:08 +08001958
Monk Liu2cb681b2017-04-26 12:00:49 +08001959 for (j = 0; j < adev->num_ip_blocks; j++) {
1960 block = &adev->ip_blocks[j];
1961
1962 if (block->version->type != ip_order[i] ||
1963 !block->status.valid)
1964 continue;
1965
1966 r = block->version->funcs->hw_init(adev);
1967 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
Monk Liua90ad3c2017-01-23 14:22:08 +08001968 }
1969 }
1970
1971 return 0;
1972}
1973
Chunming Zhoufcf06492017-05-05 10:33:33 +08001974static int amdgpu_resume_phase1(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001975{
1976 int i, r;
1977
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001978 for (i = 0; i < adev->num_ip_blocks; i++) {
1979 if (!adev->ip_blocks[i].status.valid)
1980 continue;
Chunming Zhoufcf06492017-05-05 10:33:33 +08001981 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
1982 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
1983 adev->ip_blocks[i].version->type ==
1984 AMD_IP_BLOCK_TYPE_IH) {
1985 r = adev->ip_blocks[i].version->funcs->resume(adev);
1986 if (r) {
1987 DRM_ERROR("resume of IP block <%s> failed %d\n",
1988 adev->ip_blocks[i].version->funcs->name, r);
1989 return r;
1990 }
1991 }
1992 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001993
Chunming Zhoufcf06492017-05-05 10:33:33 +08001994 return 0;
1995}
1996
1997static int amdgpu_resume_phase2(struct amdgpu_device *adev)
1998{
1999 int i, r;
2000
2001 for (i = 0; i < adev->num_ip_blocks; i++) {
2002 if (!adev->ip_blocks[i].status.valid)
2003 continue;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002004 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
2005 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
2006 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH )
2007 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002008 r = adev->ip_blocks[i].version->funcs->resume(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002009 if (r) {
2010 DRM_ERROR("resume of IP block <%s> failed %d\n",
2011 adev->ip_blocks[i].version->funcs->name, r);
2012 return r;
2013 }
2014 }
2015
2016 return 0;
2017}
2018
2019static int amdgpu_resume(struct amdgpu_device *adev)
2020{
Chunming Zhoufcf06492017-05-05 10:33:33 +08002021 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002022
Chunming Zhoufcf06492017-05-05 10:33:33 +08002023 r = amdgpu_resume_phase1(adev);
2024 if (r)
2025 return r;
2026 r = amdgpu_resume_phase2(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002027
Chunming Zhoufcf06492017-05-05 10:33:33 +08002028 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002029}
2030
Monk Liu4e99a442016-03-31 13:26:59 +08002031static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
Andres Rodriguez048765a2016-06-11 02:51:32 -04002032{
Alex Deuchera5bde2f2016-09-23 16:23:41 -04002033 if (adev->is_atom_fw) {
2034 if (amdgpu_atomfirmware_gpu_supports_virtualization(adev))
2035 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
2036 } else {
2037 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
2038 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
2039 }
Andres Rodriguez048765a2016-06-11 02:51:32 -04002040}
2041
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002042/**
2043 * amdgpu_device_init - initialize the driver
2044 *
2045 * @adev: amdgpu_device pointer
2046 * @pdev: drm dev pointer
2047 * @pdev: pci dev pointer
2048 * @flags: driver flags
2049 *
2050 * Initializes the driver info and hw (all asics).
2051 * Returns 0 for success or an error on failure.
2052 * Called at driver startup.
2053 */
2054int amdgpu_device_init(struct amdgpu_device *adev,
2055 struct drm_device *ddev,
2056 struct pci_dev *pdev,
2057 uint32_t flags)
2058{
2059 int r, i;
2060 bool runtime = false;
Marek Olšák95844d22016-08-17 23:49:27 +02002061 u32 max_MBps;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002062
2063 adev->shutdown = false;
2064 adev->dev = &pdev->dev;
2065 adev->ddev = ddev;
2066 adev->pdev = pdev;
2067 adev->flags = flags;
Jammy Zhou2f7d10b2015-07-22 11:29:01 +08002068 adev->asic_type = flags & AMD_ASIC_MASK;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002069 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
Christian König6f02a692017-07-07 11:56:59 +02002070 adev->mc.gart_size = 512 * 1024 * 1024;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002071 adev->accel_working = false;
2072 adev->num_rings = 0;
2073 adev->mman.buffer_funcs = NULL;
2074 adev->mman.buffer_funcs_ring = NULL;
2075 adev->vm_manager.vm_pte_funcs = NULL;
Christian König2d55e452016-02-08 17:37:38 +01002076 adev->vm_manager.vm_pte_num_rings = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002077 adev->gart.gart_funcs = NULL;
Chris Wilsonf54d1862016-10-25 13:00:45 +01002078 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002079
2080 adev->smc_rreg = &amdgpu_invalid_rreg;
2081 adev->smc_wreg = &amdgpu_invalid_wreg;
2082 adev->pcie_rreg = &amdgpu_invalid_rreg;
2083 adev->pcie_wreg = &amdgpu_invalid_wreg;
Huang Rui36b9a952016-08-31 13:23:25 +08002084 adev->pciep_rreg = &amdgpu_invalid_rreg;
2085 adev->pciep_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002086 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
2087 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
2088 adev->didt_rreg = &amdgpu_invalid_rreg;
2089 adev->didt_wreg = &amdgpu_invalid_wreg;
Rex Zhuccdbb202016-06-08 12:47:41 +08002090 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
2091 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002092 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
2093 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
2094
Rex Zhuccdbb202016-06-08 12:47:41 +08002095
Alex Deucher3e39ab92015-06-05 15:04:33 -04002096 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
2097 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
2098 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002099
2100 /* mutex initialization are all done here so we
2101 * can recall function without having locking issues */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002102 atomic_set(&adev->irq.ih.lock, 0);
Huang Rui0e5ca0d2017-03-03 18:37:23 -05002103 mutex_init(&adev->firmware.mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002104 mutex_init(&adev->pm.mutex);
2105 mutex_init(&adev->gfx.gpu_clock_mutex);
2106 mutex_init(&adev->srbm_mutex);
2107 mutex_init(&adev->grbm_idx_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002108 mutex_init(&adev->mn_lock);
2109 hash_init(adev->mn_hash);
2110
2111 amdgpu_check_arguments(adev);
2112
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002113 spin_lock_init(&adev->mmio_idx_lock);
2114 spin_lock_init(&adev->smc_idx_lock);
2115 spin_lock_init(&adev->pcie_idx_lock);
2116 spin_lock_init(&adev->uvd_ctx_idx_lock);
2117 spin_lock_init(&adev->didt_idx_lock);
Rex Zhuccdbb202016-06-08 12:47:41 +08002118 spin_lock_init(&adev->gc_cac_idx_lock);
Evan Quan16abb5d2017-07-04 09:21:50 +08002119 spin_lock_init(&adev->se_cac_idx_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002120 spin_lock_init(&adev->audio_endpt_idx_lock);
Marek Olšák95844d22016-08-17 23:49:27 +02002121 spin_lock_init(&adev->mm_stats.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002122
Chunming Zhou0c4e7fa2016-08-17 11:41:30 +08002123 INIT_LIST_HEAD(&adev->shadow_list);
2124 mutex_init(&adev->shadow_list_lock);
2125
Chunming Zhou5c1354b2016-08-30 16:13:10 +08002126 INIT_LIST_HEAD(&adev->gtt_list);
2127 spin_lock_init(&adev->gtt_list_lock);
2128
Andres Rodriguez795f2812017-03-06 16:27:55 -05002129 INIT_LIST_HEAD(&adev->ring_lru_list);
2130 spin_lock_init(&adev->ring_lru_list_lock);
2131
Shirish S2dc80b02017-05-25 10:05:25 +05302132 INIT_DELAYED_WORK(&adev->late_init_work, amdgpu_late_init_func_handler);
2133
Alex Xie0fa49552017-06-08 14:58:05 -04002134 /* Registers mapping */
2135 /* TODO: block userspace mapping of io register */
Ken Wangda69c1612016-01-21 19:08:55 +08002136 if (adev->asic_type >= CHIP_BONAIRE) {
2137 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
2138 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
2139 } else {
2140 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
2141 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
2142 }
Chunming Zhou5c1354b2016-08-30 16:13:10 +08002143
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002144 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
2145 if (adev->rmmio == NULL) {
2146 return -ENOMEM;
2147 }
2148 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
2149 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
2150
Ken Wangda69c1612016-01-21 19:08:55 +08002151 if (adev->asic_type >= CHIP_BONAIRE)
2152 /* doorbell bar mapping */
2153 amdgpu_doorbell_init(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002154
2155 /* io port mapping */
2156 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
2157 if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
2158 adev->rio_mem_size = pci_resource_len(adev->pdev, i);
2159 adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
2160 break;
2161 }
2162 }
2163 if (adev->rio_mem == NULL)
Amber Linb64a18c2017-01-04 08:06:58 -05002164 DRM_INFO("PCI I/O BAR is not found.\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002165
2166 /* early init functions */
2167 r = amdgpu_early_init(adev);
2168 if (r)
2169 return r;
2170
2171 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
2172 /* this will fail for cards that aren't VGA class devices, just
2173 * ignore it */
2174 vga_client_register(adev->pdev, adev, NULL, amdgpu_vga_set_decode);
2175
2176 if (amdgpu_runtime_pm == 1)
2177 runtime = true;
Alex Deuchere9bef452016-04-25 13:12:18 -04002178 if (amdgpu_device_is_px(ddev))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002179 runtime = true;
Lukas Wunner84c8b222017-03-10 21:23:45 +01002180 if (!pci_is_thunderbolt_attached(adev->pdev))
2181 vga_switcheroo_register_client(adev->pdev,
2182 &amdgpu_switcheroo_ops, runtime);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002183 if (runtime)
2184 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
2185
2186 /* Read BIOS */
Alex Deucher83ba1262016-06-03 18:21:41 -04002187 if (!amdgpu_get_bios(adev)) {
2188 r = -EINVAL;
2189 goto failed;
2190 }
Nils Wallméniusf7e9e9f2016-12-14 21:52:45 +01002191
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002192 r = amdgpu_atombios_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002193 if (r) {
2194 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
Gavin Wan89041942017-06-23 13:55:15 -04002195 amdgpu_vf_error_put(AMDGIM_ERROR_VF_ATOMBIOS_INIT_FAIL, 0, 0);
Alex Deucher83ba1262016-06-03 18:21:41 -04002196 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002197 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002198
Monk Liu4e99a442016-03-31 13:26:59 +08002199 /* detect if we are with an SRIOV vbios */
2200 amdgpu_device_detect_sriov_bios(adev);
Andres Rodriguez048765a2016-06-11 02:51:32 -04002201
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002202 /* Post card if necessary */
Monk Liubec86372016-09-14 19:38:08 +08002203 if (amdgpu_vpost_needed(adev)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002204 if (!adev->bios) {
Monk Liubec86372016-09-14 19:38:08 +08002205 dev_err(adev->dev, "no vBIOS found\n");
Gavin Wan89041942017-06-23 13:55:15 -04002206 amdgpu_vf_error_put(AMDGIM_ERROR_VF_NO_VBIOS, 0, 0);
Alex Deucher83ba1262016-06-03 18:21:41 -04002207 r = -EINVAL;
2208 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002209 }
Monk Liubec86372016-09-14 19:38:08 +08002210 DRM_INFO("GPU posting now...\n");
Monk Liu4e99a442016-03-31 13:26:59 +08002211 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2212 if (r) {
2213 dev_err(adev->dev, "gpu post error!\n");
Gavin Wan89041942017-06-23 13:55:15 -04002214 amdgpu_vf_error_put(AMDGIM_ERROR_VF_GPU_POST_ERROR, 0, 0);
Monk Liu4e99a442016-03-31 13:26:59 +08002215 goto failed;
2216 }
2217 } else {
2218 DRM_INFO("GPU post is not needed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002219 }
2220
Alex Deucher88b64e92017-07-10 10:43:10 -04002221 if (adev->is_atom_fw) {
2222 /* Initialize clocks */
2223 r = amdgpu_atomfirmware_get_clock_info(adev);
2224 if (r) {
2225 dev_err(adev->dev, "amdgpu_atomfirmware_get_clock_info failed\n");
2226 amdgpu_vf_error_put(AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
2227 goto failed;
2228 }
2229 } else {
Alex Deuchera5bde2f2016-09-23 16:23:41 -04002230 /* Initialize clocks */
2231 r = amdgpu_atombios_get_clock_info(adev);
2232 if (r) {
2233 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
Gavin Wan89041942017-06-23 13:55:15 -04002234 amdgpu_vf_error_put(AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
2235 goto failed;
Alex Deuchera5bde2f2016-09-23 16:23:41 -04002236 }
2237 /* init i2c buses */
2238 amdgpu_atombios_i2c_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002239 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002240
2241 /* Fence driver */
2242 r = amdgpu_fence_driver_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002243 if (r) {
2244 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
Gavin Wan89041942017-06-23 13:55:15 -04002245 amdgpu_vf_error_put(AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0);
Alex Deucher83ba1262016-06-03 18:21:41 -04002246 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002247 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002248
2249 /* init the mode config */
2250 drm_mode_config_init(adev->ddev);
2251
2252 r = amdgpu_init(adev);
2253 if (r) {
Alex Deucher2c1a2782015-12-07 17:02:53 -05002254 dev_err(adev->dev, "amdgpu_init failed\n");
Gavin Wan89041942017-06-23 13:55:15 -04002255 amdgpu_vf_error_put(AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002256 amdgpu_fini(adev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002257 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002258 }
2259
2260 adev->accel_working = true;
2261
Alex Xiee59c0202017-06-01 09:42:59 -04002262 amdgpu_vm_check_compute_bug(adev);
2263
Marek Olšák95844d22016-08-17 23:49:27 +02002264 /* Initialize the buffer migration limit. */
2265 if (amdgpu_moverate >= 0)
2266 max_MBps = amdgpu_moverate;
2267 else
2268 max_MBps = 8; /* Allow 8 MB/s. */
2269 /* Get a log2 for easy divisions. */
2270 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
2271
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002272 r = amdgpu_ib_pool_init(adev);
2273 if (r) {
2274 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
Gavin Wan89041942017-06-23 13:55:15 -04002275 amdgpu_vf_error_put(AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r);
Alex Deucher83ba1262016-06-03 18:21:41 -04002276 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002277 }
2278
2279 r = amdgpu_ib_ring_tests(adev);
2280 if (r)
2281 DRM_ERROR("ib ring test failed (%d).\n", r);
2282
Monk Liu9bc92b92017-02-08 17:38:13 +08002283 amdgpu_fbdev_init(adev);
2284
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002285 r = amdgpu_gem_debugfs_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002286 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002287 DRM_ERROR("registering gem debugfs failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002288
2289 r = amdgpu_debugfs_regs_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002290 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002291 DRM_ERROR("registering register debugfs failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002292
Huang Rui4f0955f2017-05-10 23:04:06 +08002293 r = amdgpu_debugfs_test_ib_ring_init(adev);
2294 if (r)
2295 DRM_ERROR("registering register test ib ring debugfs failed (%d).\n", r);
2296
Huang Rui50ab2532016-06-12 15:51:09 +08002297 r = amdgpu_debugfs_firmware_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002298 if (r)
Huang Rui50ab2532016-06-12 15:51:09 +08002299 DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
Huang Rui50ab2532016-06-12 15:51:09 +08002300
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002301 if ((amdgpu_testing & 1)) {
2302 if (adev->accel_working)
2303 amdgpu_test_moves(adev);
2304 else
2305 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
2306 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002307 if (amdgpu_benchmarking) {
2308 if (adev->accel_working)
2309 amdgpu_benchmark(adev, amdgpu_benchmarking);
2310 else
2311 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
2312 }
2313
2314 /* enable clockgating, etc. after ib tests, etc. since some blocks require
2315 * explicit gating rather than handling it automatically.
2316 */
2317 r = amdgpu_late_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002318 if (r) {
2319 dev_err(adev->dev, "amdgpu_late_init failed\n");
Gavin Wan89041942017-06-23 13:55:15 -04002320 amdgpu_vf_error_put(AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r);
Alex Deucher83ba1262016-06-03 18:21:41 -04002321 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002322 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002323
2324 return 0;
Alex Deucher83ba1262016-06-03 18:21:41 -04002325
2326failed:
Gavin Wan89041942017-06-23 13:55:15 -04002327 amdgpu_vf_error_trans_all(adev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002328 if (runtime)
2329 vga_switcheroo_fini_domain_pm_ops(adev->dev);
2330 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002331}
2332
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002333/**
2334 * amdgpu_device_fini - tear down the driver
2335 *
2336 * @adev: amdgpu_device pointer
2337 *
2338 * Tear down the driver info (all asics).
2339 * Called at driver shutdown.
2340 */
2341void amdgpu_device_fini(struct amdgpu_device *adev)
2342{
2343 int r;
2344
2345 DRM_INFO("amdgpu: finishing device.\n");
2346 adev->shutdown = true;
Pixel Dingdb2c2a92017-04-25 16:47:42 +08002347 if (adev->mode_info.mode_config_initialized)
2348 drm_crtc_force_disable_all(adev->ddev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002349 /* evict vram memory */
2350 amdgpu_bo_evict_vram(adev);
2351 amdgpu_ib_pool_fini(adev);
2352 amdgpu_fence_driver_fini(adev);
2353 amdgpu_fbdev_fini(adev);
2354 r = amdgpu_fini(adev);
Huang Ruiab4fe3e2017-06-05 22:11:59 +08002355 if (adev->firmware.gpu_info_fw) {
2356 release_firmware(adev->firmware.gpu_info_fw);
2357 adev->firmware.gpu_info_fw = NULL;
2358 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002359 adev->accel_working = false;
Shirish S2dc80b02017-05-25 10:05:25 +05302360 cancel_delayed_work_sync(&adev->late_init_work);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002361 /* free i2c buses */
2362 amdgpu_i2c_fini(adev);
2363 amdgpu_atombios_fini(adev);
2364 kfree(adev->bios);
2365 adev->bios = NULL;
Lukas Wunner84c8b222017-03-10 21:23:45 +01002366 if (!pci_is_thunderbolt_attached(adev->pdev))
2367 vga_switcheroo_unregister_client(adev->pdev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002368 if (adev->flags & AMD_IS_PX)
2369 vga_switcheroo_fini_domain_pm_ops(adev->dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002370 vga_client_register(adev->pdev, NULL, NULL, NULL);
2371 if (adev->rio_mem)
2372 pci_iounmap(adev->pdev, adev->rio_mem);
2373 adev->rio_mem = NULL;
2374 iounmap(adev->rmmio);
2375 adev->rmmio = NULL;
Ken Wangda69c1612016-01-21 19:08:55 +08002376 if (adev->asic_type >= CHIP_BONAIRE)
2377 amdgpu_doorbell_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002378 amdgpu_debugfs_regs_cleanup(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002379}
2380
2381
2382/*
2383 * Suspend & resume.
2384 */
2385/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002386 * amdgpu_device_suspend - initiate device suspend
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002387 *
2388 * @pdev: drm dev pointer
2389 * @state: suspend state
2390 *
2391 * Puts the hw in the suspend state (all asics).
2392 * Returns 0 for success or an error on failure.
2393 * Called at driver suspend.
2394 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002395int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002396{
2397 struct amdgpu_device *adev;
2398 struct drm_crtc *crtc;
2399 struct drm_connector *connector;
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002400 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002401
2402 if (dev == NULL || dev->dev_private == NULL) {
2403 return -ENODEV;
2404 }
2405
2406 adev = dev->dev_private;
2407
2408 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2409 return 0;
2410
2411 drm_kms_helper_poll_disable(dev);
2412
2413 /* turn off display hw */
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002414 drm_modeset_lock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002415 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2416 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
2417 }
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002418 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002419
Yong Zhaoba997702015-11-09 17:21:45 -05002420 amdgpu_amdkfd_suspend(adev);
2421
Alex Deucher756e6882015-10-08 00:03:36 -04002422 /* unpin the front buffers and cursors */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002423 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Alex Deucher756e6882015-10-08 00:03:36 -04002424 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002425 struct amdgpu_framebuffer *rfb = to_amdgpu_framebuffer(crtc->primary->fb);
2426 struct amdgpu_bo *robj;
2427
Alex Deucher756e6882015-10-08 00:03:36 -04002428 if (amdgpu_crtc->cursor_bo) {
2429 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
Alex Xie7a6901d2017-04-24 13:52:41 -04002430 r = amdgpu_bo_reserve(aobj, true);
Alex Deucher756e6882015-10-08 00:03:36 -04002431 if (r == 0) {
2432 amdgpu_bo_unpin(aobj);
2433 amdgpu_bo_unreserve(aobj);
2434 }
2435 }
2436
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002437 if (rfb == NULL || rfb->obj == NULL) {
2438 continue;
2439 }
2440 robj = gem_to_amdgpu_bo(rfb->obj);
2441 /* don't unpin kernel fb objects */
2442 if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
Alex Xie7a6901d2017-04-24 13:52:41 -04002443 r = amdgpu_bo_reserve(robj, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002444 if (r == 0) {
2445 amdgpu_bo_unpin(robj);
2446 amdgpu_bo_unreserve(robj);
2447 }
2448 }
2449 }
2450 /* evict vram memory */
2451 amdgpu_bo_evict_vram(adev);
2452
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002453 amdgpu_fence_driver_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002454
2455 r = amdgpu_suspend(adev);
2456
Alex Deuchera0a71e42016-10-10 12:41:36 -04002457 /* evict remaining vram memory
2458 * This second call to evict vram is to evict the gart page table
2459 * using the CPU.
2460 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002461 amdgpu_bo_evict_vram(adev);
2462
Alex Deucherd05da0e2017-06-30 17:08:45 -04002463 amdgpu_atombios_scratch_regs_save(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002464 pci_save_state(dev->pdev);
2465 if (suspend) {
2466 /* Shut down the device */
2467 pci_disable_device(dev->pdev);
2468 pci_set_power_state(dev->pdev, PCI_D3hot);
jimqu74b0b152016-09-07 17:09:12 +08002469 } else {
2470 r = amdgpu_asic_reset(adev);
2471 if (r)
2472 DRM_ERROR("amdgpu asic reset failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002473 }
2474
2475 if (fbcon) {
2476 console_lock();
2477 amdgpu_fbdev_set_suspend(adev, 1);
2478 console_unlock();
2479 }
2480 return 0;
2481}
2482
2483/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002484 * amdgpu_device_resume - initiate device resume
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002485 *
2486 * @pdev: drm dev pointer
2487 *
2488 * Bring the hw back to operating state (all asics).
2489 * Returns 0 for success or an error on failure.
2490 * Called at driver resume.
2491 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002492int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002493{
2494 struct drm_connector *connector;
2495 struct amdgpu_device *adev = dev->dev_private;
Alex Deucher756e6882015-10-08 00:03:36 -04002496 struct drm_crtc *crtc;
Huang Rui03161a62017-04-13 16:12:26 +08002497 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002498
2499 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2500 return 0;
2501
jimqu74b0b152016-09-07 17:09:12 +08002502 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002503 console_lock();
jimqu74b0b152016-09-07 17:09:12 +08002504
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002505 if (resume) {
2506 pci_set_power_state(dev->pdev, PCI_D0);
2507 pci_restore_state(dev->pdev);
jimqu74b0b152016-09-07 17:09:12 +08002508 r = pci_enable_device(dev->pdev);
Huang Rui03161a62017-04-13 16:12:26 +08002509 if (r)
2510 goto unlock;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002511 }
Alex Deucherd05da0e2017-06-30 17:08:45 -04002512 amdgpu_atombios_scratch_regs_restore(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002513
2514 /* post card */
Jim Quc836fec2017-02-10 15:59:59 +08002515 if (amdgpu_need_post(adev)) {
jimqu74b0b152016-09-07 17:09:12 +08002516 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2517 if (r)
2518 DRM_ERROR("amdgpu asic init failed\n");
2519 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002520
2521 r = amdgpu_resume(adev);
Rex Zhue6707212017-03-30 13:21:01 +08002522 if (r) {
Flora Cuica198522016-02-04 15:10:08 +08002523 DRM_ERROR("amdgpu_resume failed (%d).\n", r);
Huang Rui03161a62017-04-13 16:12:26 +08002524 goto unlock;
Rex Zhue6707212017-03-30 13:21:01 +08002525 }
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002526 amdgpu_fence_driver_resume(adev);
2527
Flora Cuica198522016-02-04 15:10:08 +08002528 if (resume) {
2529 r = amdgpu_ib_ring_tests(adev);
2530 if (r)
2531 DRM_ERROR("ib ring test failed (%d).\n", r);
2532 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002533
2534 r = amdgpu_late_init(adev);
Huang Rui03161a62017-04-13 16:12:26 +08002535 if (r)
2536 goto unlock;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002537
Alex Deucher756e6882015-10-08 00:03:36 -04002538 /* pin cursors */
2539 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2540 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2541
2542 if (amdgpu_crtc->cursor_bo) {
2543 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
Alex Xie7a6901d2017-04-24 13:52:41 -04002544 r = amdgpu_bo_reserve(aobj, true);
Alex Deucher756e6882015-10-08 00:03:36 -04002545 if (r == 0) {
2546 r = amdgpu_bo_pin(aobj,
2547 AMDGPU_GEM_DOMAIN_VRAM,
2548 &amdgpu_crtc->cursor_addr);
2549 if (r != 0)
2550 DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
2551 amdgpu_bo_unreserve(aobj);
2552 }
2553 }
2554 }
Yong Zhaoba997702015-11-09 17:21:45 -05002555 r = amdgpu_amdkfd_resume(adev);
2556 if (r)
2557 return r;
Alex Deucher756e6882015-10-08 00:03:36 -04002558
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002559 /* blat the mode back in */
2560 if (fbcon) {
2561 drm_helper_resume_force_mode(dev);
2562 /* turn on display hw */
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002563 drm_modeset_lock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002564 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2565 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
2566 }
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002567 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002568 }
2569
2570 drm_kms_helper_poll_enable(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002571
2572 /*
2573 * Most of the connector probing functions try to acquire runtime pm
2574 * refs to ensure that the GPU is powered on when connector polling is
2575 * performed. Since we're calling this from a runtime PM callback,
2576 * trying to acquire rpm refs will cause us to deadlock.
2577 *
2578 * Since we're guaranteed to be holding the rpm lock, it's safe to
2579 * temporarily disable the rpm helpers so this doesn't deadlock us.
2580 */
2581#ifdef CONFIG_PM
2582 dev->dev->power.disable_depth++;
2583#endif
Alex Deucher54fb2a52015-11-24 14:30:56 -05002584 drm_helper_hpd_irq_event(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002585#ifdef CONFIG_PM
2586 dev->dev->power.disable_depth--;
2587#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002588
Huang Rui03161a62017-04-13 16:12:26 +08002589 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002590 amdgpu_fbdev_set_suspend(adev, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002591
Huang Rui03161a62017-04-13 16:12:26 +08002592unlock:
2593 if (fbcon)
2594 console_unlock();
2595
2596 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002597}
2598
Chunming Zhou63fbf422016-07-15 11:19:20 +08002599static bool amdgpu_check_soft_reset(struct amdgpu_device *adev)
2600{
2601 int i;
2602 bool asic_hang = false;
2603
2604 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002605 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou63fbf422016-07-15 11:19:20 +08002606 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002607 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
2608 adev->ip_blocks[i].status.hang =
2609 adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
2610 if (adev->ip_blocks[i].status.hang) {
2611 DRM_INFO("IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
Chunming Zhou63fbf422016-07-15 11:19:20 +08002612 asic_hang = true;
2613 }
2614 }
2615 return asic_hang;
2616}
2617
Baoyou Xie4d446652016-09-18 22:09:35 +08002618static int amdgpu_pre_soft_reset(struct amdgpu_device *adev)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002619{
2620 int i, r = 0;
2621
2622 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002623 if (!adev->ip_blocks[i].status.valid)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002624 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002625 if (adev->ip_blocks[i].status.hang &&
2626 adev->ip_blocks[i].version->funcs->pre_soft_reset) {
2627 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
Chunming Zhoud31a5012016-07-18 10:04:34 +08002628 if (r)
2629 return r;
2630 }
2631 }
2632
2633 return 0;
2634}
2635
Chunming Zhou35d782f2016-07-15 15:57:13 +08002636static bool amdgpu_need_full_reset(struct amdgpu_device *adev)
2637{
Alex Deucherda146d32016-10-13 16:07:03 -04002638 int i;
2639
2640 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002641 if (!adev->ip_blocks[i].status.valid)
Alex Deucherda146d32016-10-13 16:07:03 -04002642 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002643 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
2644 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
2645 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
2646 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE)) {
2647 if (adev->ip_blocks[i].status.hang) {
Alex Deucherda146d32016-10-13 16:07:03 -04002648 DRM_INFO("Some block need full reset!\n");
2649 return true;
2650 }
2651 }
Chunming Zhou35d782f2016-07-15 15:57:13 +08002652 }
2653 return false;
2654}
2655
2656static int amdgpu_soft_reset(struct amdgpu_device *adev)
2657{
2658 int i, r = 0;
2659
2660 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002661 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002662 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002663 if (adev->ip_blocks[i].status.hang &&
2664 adev->ip_blocks[i].version->funcs->soft_reset) {
2665 r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002666 if (r)
2667 return r;
2668 }
2669 }
2670
2671 return 0;
2672}
2673
2674static int amdgpu_post_soft_reset(struct amdgpu_device *adev)
2675{
2676 int i, r = 0;
2677
2678 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002679 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002680 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002681 if (adev->ip_blocks[i].status.hang &&
2682 adev->ip_blocks[i].version->funcs->post_soft_reset)
2683 r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002684 if (r)
2685 return r;
2686 }
2687
2688 return 0;
2689}
2690
Chunming Zhou3ad81f12016-08-05 17:30:17 +08002691bool amdgpu_need_backup(struct amdgpu_device *adev)
2692{
2693 if (adev->flags & AMD_IS_APU)
2694 return false;
2695
2696 return amdgpu_lockup_timeout > 0 ? true : false;
2697}
2698
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002699static int amdgpu_recover_vram_from_shadow(struct amdgpu_device *adev,
2700 struct amdgpu_ring *ring,
2701 struct amdgpu_bo *bo,
Chris Wilsonf54d1862016-10-25 13:00:45 +01002702 struct dma_fence **fence)
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002703{
2704 uint32_t domain;
2705 int r;
2706
Roger.He23d2e502017-04-21 14:24:26 +08002707 if (!bo->shadow)
2708 return 0;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002709
Alex Xie1d284792017-04-24 13:53:04 -04002710 r = amdgpu_bo_reserve(bo, true);
Roger.He23d2e502017-04-21 14:24:26 +08002711 if (r)
2712 return r;
2713 domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
2714 /* if bo has been evicted, then no need to recover */
2715 if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
Roger.He82521312017-04-21 13:08:43 +08002716 r = amdgpu_bo_validate(bo->shadow);
2717 if (r) {
2718 DRM_ERROR("bo validate failed!\n");
2719 goto err;
2720 }
2721
2722 r = amdgpu_ttm_bind(&bo->shadow->tbo, &bo->shadow->tbo.mem);
2723 if (r) {
2724 DRM_ERROR("%p bind failed\n", bo->shadow);
2725 goto err;
2726 }
2727
Roger.He23d2e502017-04-21 14:24:26 +08002728 r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002729 NULL, fence, true);
Roger.He23d2e502017-04-21 14:24:26 +08002730 if (r) {
2731 DRM_ERROR("recover page table failed!\n");
2732 goto err;
2733 }
2734 }
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002735err:
Roger.He23d2e502017-04-21 14:24:26 +08002736 amdgpu_bo_unreserve(bo);
2737 return r;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002738}
2739
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002740/**
Monk Liua90ad3c2017-01-23 14:22:08 +08002741 * amdgpu_sriov_gpu_reset - reset the asic
2742 *
2743 * @adev: amdgpu device pointer
Monk Liu7225f872017-04-26 14:51:54 +08002744 * @job: which job trigger hang
Monk Liua90ad3c2017-01-23 14:22:08 +08002745 *
2746 * Attempt the reset the GPU if it has hung (all asics).
2747 * for SRIOV case.
2748 * Returns 0 for success or an error on failure.
2749 */
Monk Liu7225f872017-04-26 14:51:54 +08002750int amdgpu_sriov_gpu_reset(struct amdgpu_device *adev, struct amdgpu_job *job)
Monk Liua90ad3c2017-01-23 14:22:08 +08002751{
Monk Liu65781c72017-05-11 13:36:44 +08002752 int i, j, r = 0;
Monk Liua90ad3c2017-01-23 14:22:08 +08002753 int resched;
2754 struct amdgpu_bo *bo, *tmp;
2755 struct amdgpu_ring *ring;
2756 struct dma_fence *fence = NULL, *next = NULL;
2757
Monk Liu147b5982017-01-25 15:48:01 +08002758 mutex_lock(&adev->virt.lock_reset);
Monk Liua90ad3c2017-01-23 14:22:08 +08002759 atomic_inc(&adev->gpu_reset_counter);
Monk Liu1fb37a32017-01-26 15:36:37 +08002760 adev->gfx.in_reset = true;
Monk Liua90ad3c2017-01-23 14:22:08 +08002761
2762 /* block TTM */
2763 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
2764
Monk Liu65781c72017-05-11 13:36:44 +08002765 /* we start from the ring trigger GPU hang */
2766 j = job ? job->ring->idx : 0;
Monk Liua90ad3c2017-01-23 14:22:08 +08002767
Monk Liu65781c72017-05-11 13:36:44 +08002768 /* block scheduler */
2769 for (i = j; i < j + AMDGPU_MAX_RINGS; ++i) {
2770 ring = adev->rings[i % AMDGPU_MAX_RINGS];
Monk Liua90ad3c2017-01-23 14:22:08 +08002771 if (!ring || !ring->sched.thread)
2772 continue;
2773
2774 kthread_park(ring->sched.thread);
Monk Liua90ad3c2017-01-23 14:22:08 +08002775
Monk Liu65781c72017-05-11 13:36:44 +08002776 if (job && j != i)
2777 continue;
2778
Monk Liu4f059ec2017-05-11 13:59:15 +08002779 /* here give the last chance to check if job removed from mirror-list
Monk Liu65781c72017-05-11 13:36:44 +08002780 * since we already pay some time on kthread_park */
Monk Liu4f059ec2017-05-11 13:59:15 +08002781 if (job && list_empty(&job->base.node)) {
Monk Liu65781c72017-05-11 13:36:44 +08002782 kthread_unpark(ring->sched.thread);
2783 goto give_up_reset;
2784 }
2785
2786 if (amd_sched_invalidate_job(&job->base, amdgpu_job_hang_limit))
2787 amd_sched_job_kickout(&job->base);
2788
2789 /* only do job_reset on the hang ring if @job not NULL */
Monk Liua90ad3c2017-01-23 14:22:08 +08002790 amd_sched_hw_job_reset(&ring->sched);
Monk Liu65781c72017-05-11 13:36:44 +08002791
2792 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
2793 amdgpu_fence_driver_force_completion_ring(ring);
Monk Liua90ad3c2017-01-23 14:22:08 +08002794 }
2795
Monk Liua90ad3c2017-01-23 14:22:08 +08002796 /* request to take full control of GPU before re-initialization */
Monk Liu7225f872017-04-26 14:51:54 +08002797 if (job)
Monk Liua90ad3c2017-01-23 14:22:08 +08002798 amdgpu_virt_reset_gpu(adev);
2799 else
2800 amdgpu_virt_request_full_gpu(adev, true);
2801
2802
2803 /* Resume IP prior to SMC */
Monk Liue4f0fdc2017-02-09 11:55:49 +08002804 amdgpu_sriov_reinit_early(adev);
Monk Liua90ad3c2017-01-23 14:22:08 +08002805
2806 /* we need recover gart prior to run SMC/CP/SDMA resume */
2807 amdgpu_ttm_recover_gart(adev);
2808
2809 /* now we are okay to resume SMC/CP/SDMA */
Monk Liue4f0fdc2017-02-09 11:55:49 +08002810 amdgpu_sriov_reinit_late(adev);
Monk Liua90ad3c2017-01-23 14:22:08 +08002811
2812 amdgpu_irq_gpu_reset_resume_helper(adev);
2813
2814 if (amdgpu_ib_ring_tests(adev))
2815 dev_err(adev->dev, "[GPU_RESET] ib ring test failed (%d).\n", r);
2816
2817 /* release full control of GPU after ib test */
2818 amdgpu_virt_release_full_gpu(adev, true);
2819
2820 DRM_INFO("recover vram bo from shadow\n");
2821
2822 ring = adev->mman.buffer_funcs_ring;
2823 mutex_lock(&adev->shadow_list_lock);
2824 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
Monk Liu236763d2017-05-01 16:15:31 +08002825 next = NULL;
Monk Liua90ad3c2017-01-23 14:22:08 +08002826 amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
2827 if (fence) {
2828 r = dma_fence_wait(fence, false);
2829 if (r) {
2830 WARN(r, "recovery from shadow isn't completed\n");
2831 break;
2832 }
2833 }
2834
2835 dma_fence_put(fence);
2836 fence = next;
2837 }
2838 mutex_unlock(&adev->shadow_list_lock);
2839
2840 if (fence) {
2841 r = dma_fence_wait(fence, false);
2842 if (r)
2843 WARN(r, "recovery from shadow isn't completed\n");
2844 }
2845 dma_fence_put(fence);
2846
Monk Liu65781c72017-05-11 13:36:44 +08002847 for (i = j; i < j + AMDGPU_MAX_RINGS; ++i) {
2848 ring = adev->rings[i % AMDGPU_MAX_RINGS];
Monk Liua90ad3c2017-01-23 14:22:08 +08002849 if (!ring || !ring->sched.thread)
2850 continue;
2851
Monk Liu65781c72017-05-11 13:36:44 +08002852 if (job && j != i) {
2853 kthread_unpark(ring->sched.thread);
2854 continue;
2855 }
2856
Monk Liua90ad3c2017-01-23 14:22:08 +08002857 amd_sched_job_recovery(&ring->sched);
2858 kthread_unpark(ring->sched.thread);
2859 }
2860
2861 drm_helper_resume_force_mode(adev->ddev);
Monk Liu65781c72017-05-11 13:36:44 +08002862give_up_reset:
Monk Liua90ad3c2017-01-23 14:22:08 +08002863 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
2864 if (r) {
2865 /* bad news, how to tell it to userspace ? */
2866 dev_info(adev->dev, "GPU reset failed\n");
Monk Liu65781c72017-05-11 13:36:44 +08002867 } else {
2868 dev_info(adev->dev, "GPU reset successed!\n");
Monk Liua90ad3c2017-01-23 14:22:08 +08002869 }
2870
Monk Liu1fb37a32017-01-26 15:36:37 +08002871 adev->gfx.in_reset = false;
Monk Liu147b5982017-01-25 15:48:01 +08002872 mutex_unlock(&adev->virt.lock_reset);
Monk Liua90ad3c2017-01-23 14:22:08 +08002873 return r;
2874}
2875
2876/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002877 * amdgpu_gpu_reset - reset the asic
2878 *
2879 * @adev: amdgpu device pointer
2880 *
2881 * Attempt the reset the GPU if it has hung (all asics).
2882 * Returns 0 for success or an error on failure.
2883 */
2884int amdgpu_gpu_reset(struct amdgpu_device *adev)
2885{
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002886 int i, r;
2887 int resched;
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002888 bool need_full_reset, vram_lost = false;
Xiangliang Yufb140b22016-12-17 22:48:57 +08002889
Chunming Zhou63fbf422016-07-15 11:19:20 +08002890 if (!amdgpu_check_soft_reset(adev)) {
2891 DRM_INFO("No hardware hang detected. Did some blocks stall?\n");
2892 return 0;
2893 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002894
Marek Olšákd94aed52015-05-05 21:13:49 +02002895 atomic_inc(&adev->gpu_reset_counter);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002896
Chunming Zhoua3c47d62016-06-30 16:44:41 +08002897 /* block TTM */
2898 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
2899
Chunming Zhou0875dc92016-06-12 15:41:58 +08002900 /* block scheduler */
2901 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2902 struct amdgpu_ring *ring = adev->rings[i];
2903
Chunming Zhou51687752017-04-24 17:09:15 +08002904 if (!ring || !ring->sched.thread)
Chunming Zhou0875dc92016-06-12 15:41:58 +08002905 continue;
2906 kthread_park(ring->sched.thread);
Chunming Zhouaa1c8902016-06-30 13:56:02 +08002907 amd_sched_hw_job_reset(&ring->sched);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002908 }
Chunming Zhou2200eda2016-06-30 16:53:02 +08002909 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
2910 amdgpu_fence_driver_force_completion(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002911
Chunming Zhou35d782f2016-07-15 15:57:13 +08002912 need_full_reset = amdgpu_need_full_reset(adev);
2913
2914 if (!need_full_reset) {
2915 amdgpu_pre_soft_reset(adev);
2916 r = amdgpu_soft_reset(adev);
2917 amdgpu_post_soft_reset(adev);
2918 if (r || amdgpu_check_soft_reset(adev)) {
2919 DRM_INFO("soft reset failed, will fallback to full reset!\n");
2920 need_full_reset = true;
2921 }
2922 }
2923
2924 if (need_full_reset) {
Chunming Zhou35d782f2016-07-15 15:57:13 +08002925 r = amdgpu_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002926
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002927retry:
Alex Deucherd05da0e2017-06-30 17:08:45 -04002928 amdgpu_atombios_scratch_regs_save(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002929 r = amdgpu_asic_reset(adev);
Alex Deucherd05da0e2017-06-30 17:08:45 -04002930 amdgpu_atombios_scratch_regs_restore(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002931 /* post card */
2932 amdgpu_atom_asic_init(adev->mode_info.atom_context);
Alex Deucherbfa99262016-01-15 11:59:48 -05002933
Chunming Zhou35d782f2016-07-15 15:57:13 +08002934 if (!r) {
2935 dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
Chunming Zhoufcf06492017-05-05 10:33:33 +08002936 r = amdgpu_resume_phase1(adev);
2937 if (r)
2938 goto out;
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002939 vram_lost = amdgpu_check_vram_lost(adev);
Chunming Zhouf1892132017-05-15 16:48:27 +08002940 if (vram_lost) {
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002941 DRM_ERROR("VRAM is lost!\n");
Chunming Zhouf1892132017-05-15 16:48:27 +08002942 atomic_inc(&adev->vram_lost_counter);
2943 }
Chunming Zhou2c0d7312016-08-30 16:36:25 +08002944 r = amdgpu_ttm_recover_gart(adev);
2945 if (r)
Chunming Zhoufcf06492017-05-05 10:33:33 +08002946 goto out;
2947 r = amdgpu_resume_phase2(adev);
2948 if (r)
2949 goto out;
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002950 if (vram_lost)
2951 amdgpu_fill_reset_magic(adev);
Chunming Zhou2c0d7312016-08-30 16:36:25 +08002952 }
Chunming Zhoufcf06492017-05-05 10:33:33 +08002953 }
2954out:
2955 if (!r) {
2956 amdgpu_irq_gpu_reset_resume_helper(adev);
Chunming Zhou1f465082016-06-30 15:02:26 +08002957 r = amdgpu_ib_ring_tests(adev);
2958 if (r) {
2959 dev_err(adev->dev, "ib ring test failed (%d).\n", r);
Chunming Zhou40019dc2016-06-29 16:01:49 +08002960 r = amdgpu_suspend(adev);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002961 need_full_reset = true;
Chunming Zhou40019dc2016-06-29 16:01:49 +08002962 goto retry;
Chunming Zhou1f465082016-06-30 15:02:26 +08002963 }
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002964 /**
2965 * recovery vm page tables, since we cannot depend on VRAM is
2966 * consistent after gpu full reset.
2967 */
2968 if (need_full_reset && amdgpu_need_backup(adev)) {
2969 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
2970 struct amdgpu_bo *bo, *tmp;
Chris Wilsonf54d1862016-10-25 13:00:45 +01002971 struct dma_fence *fence = NULL, *next = NULL;
Chunming Zhou1f465082016-06-30 15:02:26 +08002972
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002973 DRM_INFO("recover vram bo from shadow\n");
2974 mutex_lock(&adev->shadow_list_lock);
2975 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
Monk Liu236763d2017-05-01 16:15:31 +08002976 next = NULL;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002977 amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
2978 if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01002979 r = dma_fence_wait(fence, false);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002980 if (r) {
Monk Liu1d7b17b2017-01-22 18:52:56 +08002981 WARN(r, "recovery from shadow isn't completed\n");
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002982 break;
2983 }
2984 }
2985
Chris Wilsonf54d1862016-10-25 13:00:45 +01002986 dma_fence_put(fence);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002987 fence = next;
2988 }
2989 mutex_unlock(&adev->shadow_list_lock);
2990 if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01002991 r = dma_fence_wait(fence, false);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002992 if (r)
Monk Liu1d7b17b2017-01-22 18:52:56 +08002993 WARN(r, "recovery from shadow isn't completed\n");
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002994 }
Chris Wilsonf54d1862016-10-25 13:00:45 +01002995 dma_fence_put(fence);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002996 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002997 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2998 struct amdgpu_ring *ring = adev->rings[i];
Chunming Zhou51687752017-04-24 17:09:15 +08002999
3000 if (!ring || !ring->sched.thread)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003001 continue;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003002
Chunming Zhouaa1c8902016-06-30 13:56:02 +08003003 amd_sched_job_recovery(&ring->sched);
Chunming Zhou0875dc92016-06-12 15:41:58 +08003004 kthread_unpark(ring->sched.thread);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003005 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003006 } else {
Chunming Zhou2200eda2016-06-30 16:53:02 +08003007 dev_err(adev->dev, "asic resume failed (%d).\n", r);
Gavin Wan89041942017-06-23 13:55:15 -04003008 amdgpu_vf_error_put(AMDGIM_ERROR_VF_ASIC_RESUME_FAIL, 0, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003009 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
Chunming Zhou51687752017-04-24 17:09:15 +08003010 if (adev->rings[i] && adev->rings[i]->sched.thread) {
Chunming Zhou0875dc92016-06-12 15:41:58 +08003011 kthread_unpark(adev->rings[i]->sched.thread);
Chunming Zhou0875dc92016-06-12 15:41:58 +08003012 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003013 }
3014 }
3015
3016 drm_helper_resume_force_mode(adev->ddev);
3017
3018 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
Gavin Wan89041942017-06-23 13:55:15 -04003019 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003020 /* bad news, how to tell it to userspace ? */
3021 dev_info(adev->dev, "GPU reset failed\n");
Gavin Wan89041942017-06-23 13:55:15 -04003022 amdgpu_vf_error_put(AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, r);
3023 }
3024 else {
Chunming Zhou6643be62017-05-05 10:50:09 +08003025 dev_info(adev->dev, "GPU reset successed!\n");
Gavin Wan89041942017-06-23 13:55:15 -04003026 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003027
Gavin Wan89041942017-06-23 13:55:15 -04003028 amdgpu_vf_error_trans_all(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003029 return r;
3030}
3031
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003032void amdgpu_get_pcie_info(struct amdgpu_device *adev)
3033{
3034 u32 mask;
3035 int ret;
3036
Alex Deuchercd474ba2016-02-04 10:21:23 -05003037 if (amdgpu_pcie_gen_cap)
3038 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
3039
3040 if (amdgpu_pcie_lane_cap)
3041 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
3042
3043 /* covers APUs as well */
3044 if (pci_is_root_bus(adev->pdev->bus)) {
3045 if (adev->pm.pcie_gen_mask == 0)
3046 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
3047 if (adev->pm.pcie_mlw_mask == 0)
3048 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003049 return;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003050 }
Alex Deuchercd474ba2016-02-04 10:21:23 -05003051
3052 if (adev->pm.pcie_gen_mask == 0) {
3053 ret = drm_pcie_get_speed_cap_mask(adev->ddev, &mask);
3054 if (!ret) {
3055 adev->pm.pcie_gen_mask = (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
3056 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
3057 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
3058
3059 if (mask & DRM_PCIE_SPEED_25)
3060 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
3061 if (mask & DRM_PCIE_SPEED_50)
3062 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2;
3063 if (mask & DRM_PCIE_SPEED_80)
3064 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3;
3065 } else {
3066 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
3067 }
3068 }
3069 if (adev->pm.pcie_mlw_mask == 0) {
3070 ret = drm_pcie_get_max_link_width(adev->ddev, &mask);
3071 if (!ret) {
3072 switch (mask) {
3073 case 32:
3074 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
3075 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
3076 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3077 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3078 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3079 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3080 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3081 break;
3082 case 16:
3083 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
3084 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3085 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3086 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3087 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3088 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3089 break;
3090 case 12:
3091 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3092 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3093 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3094 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3095 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3096 break;
3097 case 8:
3098 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3099 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3100 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3101 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3102 break;
3103 case 4:
3104 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3105 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3106 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3107 break;
3108 case 2:
3109 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3110 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3111 break;
3112 case 1:
3113 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
3114 break;
3115 default:
3116 break;
3117 }
3118 } else {
3119 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003120 }
3121 }
3122}
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003123
3124/*
3125 * Debugfs
3126 */
3127int amdgpu_debugfs_add_files(struct amdgpu_device *adev,
Nils Wallménius06ab6832016-05-02 12:46:15 -04003128 const struct drm_info_list *files,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003129 unsigned nfiles)
3130{
3131 unsigned i;
3132
3133 for (i = 0; i < adev->debugfs_count; i++) {
3134 if (adev->debugfs[i].files == files) {
3135 /* Already registered */
3136 return 0;
3137 }
3138 }
3139
3140 i = adev->debugfs_count + 1;
3141 if (i > AMDGPU_DEBUGFS_MAX_COMPONENTS) {
3142 DRM_ERROR("Reached maximum number of debugfs components.\n");
3143 DRM_ERROR("Report so we increase "
3144 "AMDGPU_DEBUGFS_MAX_COMPONENTS.\n");
3145 return -EINVAL;
3146 }
3147 adev->debugfs[adev->debugfs_count].files = files;
3148 adev->debugfs[adev->debugfs_count].num_files = nfiles;
3149 adev->debugfs_count = i;
3150#if defined(CONFIG_DEBUG_FS)
3151 drm_debugfs_create_files(files, nfiles,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003152 adev->ddev->primary->debugfs_root,
3153 adev->ddev->primary);
3154#endif
3155 return 0;
3156}
3157
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003158#if defined(CONFIG_DEBUG_FS)
3159
3160static ssize_t amdgpu_debugfs_regs_read(struct file *f, char __user *buf,
3161 size_t size, loff_t *pos)
3162{
Al Viro45063092016-12-04 18:24:56 -05003163 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003164 ssize_t result = 0;
3165 int r;
Tom St Denisbd122672016-07-28 09:39:22 -04003166 bool pm_pg_lock, use_bank;
Tom St Denis566281592016-06-27 11:55:07 -04003167 unsigned instance_bank, sh_bank, se_bank;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003168
3169 if (size & 0x3 || *pos & 0x3)
3170 return -EINVAL;
3171
Tom St Denisbd122672016-07-28 09:39:22 -04003172 /* are we reading registers for which a PG lock is necessary? */
3173 pm_pg_lock = (*pos >> 23) & 1;
3174
Tom St Denis566281592016-06-27 11:55:07 -04003175 if (*pos & (1ULL << 62)) {
3176 se_bank = (*pos >> 24) & 0x3FF;
3177 sh_bank = (*pos >> 34) & 0x3FF;
3178 instance_bank = (*pos >> 44) & 0x3FF;
Tom St Denis32977f92016-10-09 07:41:26 -04003179
3180 if (se_bank == 0x3FF)
3181 se_bank = 0xFFFFFFFF;
3182 if (sh_bank == 0x3FF)
3183 sh_bank = 0xFFFFFFFF;
3184 if (instance_bank == 0x3FF)
3185 instance_bank = 0xFFFFFFFF;
Tom St Denis566281592016-06-27 11:55:07 -04003186 use_bank = 1;
Tom St Denis566281592016-06-27 11:55:07 -04003187 } else {
3188 use_bank = 0;
3189 }
3190
Tom St Denis801a6aa9a62017-03-15 05:34:25 -04003191 *pos &= (1UL << 22) - 1;
Tom St Denisbd122672016-07-28 09:39:22 -04003192
Tom St Denis566281592016-06-27 11:55:07 -04003193 if (use_bank) {
Tom St Denis32977f92016-10-09 07:41:26 -04003194 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
3195 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
Tom St Denis566281592016-06-27 11:55:07 -04003196 return -EINVAL;
3197 mutex_lock(&adev->grbm_idx_mutex);
3198 amdgpu_gfx_select_se_sh(adev, se_bank,
3199 sh_bank, instance_bank);
3200 }
3201
Tom St Denisbd122672016-07-28 09:39:22 -04003202 if (pm_pg_lock)
3203 mutex_lock(&adev->pm.mutex);
3204
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003205 while (size) {
3206 uint32_t value;
3207
3208 if (*pos > adev->rmmio_size)
Tom St Denis566281592016-06-27 11:55:07 -04003209 goto end;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003210
3211 value = RREG32(*pos >> 2);
3212 r = put_user(value, (uint32_t *)buf);
Tom St Denis566281592016-06-27 11:55:07 -04003213 if (r) {
3214 result = r;
3215 goto end;
3216 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003217
3218 result += 4;
3219 buf += 4;
3220 *pos += 4;
3221 size -= 4;
3222 }
3223
Tom St Denis566281592016-06-27 11:55:07 -04003224end:
3225 if (use_bank) {
3226 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
3227 mutex_unlock(&adev->grbm_idx_mutex);
3228 }
3229
Tom St Denisbd122672016-07-28 09:39:22 -04003230 if (pm_pg_lock)
3231 mutex_unlock(&adev->pm.mutex);
3232
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003233 return result;
3234}
3235
3236static ssize_t amdgpu_debugfs_regs_write(struct file *f, const char __user *buf,
3237 size_t size, loff_t *pos)
3238{
Al Viro45063092016-12-04 18:24:56 -05003239 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003240 ssize_t result = 0;
3241 int r;
Tom St Denis394fdde2016-10-10 07:31:23 -04003242 bool pm_pg_lock, use_bank;
3243 unsigned instance_bank, sh_bank, se_bank;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003244
3245 if (size & 0x3 || *pos & 0x3)
3246 return -EINVAL;
3247
Tom St Denis394fdde2016-10-10 07:31:23 -04003248 /* are we reading registers for which a PG lock is necessary? */
3249 pm_pg_lock = (*pos >> 23) & 1;
3250
3251 if (*pos & (1ULL << 62)) {
3252 se_bank = (*pos >> 24) & 0x3FF;
3253 sh_bank = (*pos >> 34) & 0x3FF;
3254 instance_bank = (*pos >> 44) & 0x3FF;
3255
3256 if (se_bank == 0x3FF)
3257 se_bank = 0xFFFFFFFF;
3258 if (sh_bank == 0x3FF)
3259 sh_bank = 0xFFFFFFFF;
3260 if (instance_bank == 0x3FF)
3261 instance_bank = 0xFFFFFFFF;
3262 use_bank = 1;
3263 } else {
3264 use_bank = 0;
3265 }
3266
Tom St Denis801a6aa9a62017-03-15 05:34:25 -04003267 *pos &= (1UL << 22) - 1;
Tom St Denis394fdde2016-10-10 07:31:23 -04003268
3269 if (use_bank) {
3270 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
3271 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
3272 return -EINVAL;
3273 mutex_lock(&adev->grbm_idx_mutex);
3274 amdgpu_gfx_select_se_sh(adev, se_bank,
3275 sh_bank, instance_bank);
3276 }
3277
3278 if (pm_pg_lock)
3279 mutex_lock(&adev->pm.mutex);
3280
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003281 while (size) {
3282 uint32_t value;
3283
3284 if (*pos > adev->rmmio_size)
3285 return result;
3286
3287 r = get_user(value, (uint32_t *)buf);
3288 if (r)
3289 return r;
3290
3291 WREG32(*pos >> 2, value);
3292
3293 result += 4;
3294 buf += 4;
3295 *pos += 4;
3296 size -= 4;
3297 }
3298
Tom St Denis394fdde2016-10-10 07:31:23 -04003299 if (use_bank) {
3300 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
3301 mutex_unlock(&adev->grbm_idx_mutex);
3302 }
3303
3304 if (pm_pg_lock)
3305 mutex_unlock(&adev->pm.mutex);
3306
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003307 return result;
3308}
3309
Tom St Denisadcec282016-04-15 13:08:44 -04003310static ssize_t amdgpu_debugfs_regs_pcie_read(struct file *f, char __user *buf,
3311 size_t size, loff_t *pos)
3312{
Al Viro45063092016-12-04 18:24:56 -05003313 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003314 ssize_t result = 0;
3315 int r;
3316
3317 if (size & 0x3 || *pos & 0x3)
3318 return -EINVAL;
3319
3320 while (size) {
3321 uint32_t value;
3322
3323 value = RREG32_PCIE(*pos >> 2);
3324 r = put_user(value, (uint32_t *)buf);
3325 if (r)
3326 return r;
3327
3328 result += 4;
3329 buf += 4;
3330 *pos += 4;
3331 size -= 4;
3332 }
3333
3334 return result;
3335}
3336
3337static ssize_t amdgpu_debugfs_regs_pcie_write(struct file *f, const char __user *buf,
3338 size_t size, loff_t *pos)
3339{
Al Viro45063092016-12-04 18:24:56 -05003340 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003341 ssize_t result = 0;
3342 int r;
3343
3344 if (size & 0x3 || *pos & 0x3)
3345 return -EINVAL;
3346
3347 while (size) {
3348 uint32_t value;
3349
3350 r = get_user(value, (uint32_t *)buf);
3351 if (r)
3352 return r;
3353
3354 WREG32_PCIE(*pos >> 2, value);
3355
3356 result += 4;
3357 buf += 4;
3358 *pos += 4;
3359 size -= 4;
3360 }
3361
3362 return result;
3363}
3364
3365static ssize_t amdgpu_debugfs_regs_didt_read(struct file *f, char __user *buf,
3366 size_t size, loff_t *pos)
3367{
Al Viro45063092016-12-04 18:24:56 -05003368 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003369 ssize_t result = 0;
3370 int r;
3371
3372 if (size & 0x3 || *pos & 0x3)
3373 return -EINVAL;
3374
3375 while (size) {
3376 uint32_t value;
3377
3378 value = RREG32_DIDT(*pos >> 2);
3379 r = put_user(value, (uint32_t *)buf);
3380 if (r)
3381 return r;
3382
3383 result += 4;
3384 buf += 4;
3385 *pos += 4;
3386 size -= 4;
3387 }
3388
3389 return result;
3390}
3391
3392static ssize_t amdgpu_debugfs_regs_didt_write(struct file *f, const char __user *buf,
3393 size_t size, loff_t *pos)
3394{
Al Viro45063092016-12-04 18:24:56 -05003395 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003396 ssize_t result = 0;
3397 int r;
3398
3399 if (size & 0x3 || *pos & 0x3)
3400 return -EINVAL;
3401
3402 while (size) {
3403 uint32_t value;
3404
3405 r = get_user(value, (uint32_t *)buf);
3406 if (r)
3407 return r;
3408
3409 WREG32_DIDT(*pos >> 2, value);
3410
3411 result += 4;
3412 buf += 4;
3413 *pos += 4;
3414 size -= 4;
3415 }
3416
3417 return result;
3418}
3419
3420static ssize_t amdgpu_debugfs_regs_smc_read(struct file *f, char __user *buf,
3421 size_t size, loff_t *pos)
3422{
Al Viro45063092016-12-04 18:24:56 -05003423 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003424 ssize_t result = 0;
3425 int r;
3426
3427 if (size & 0x3 || *pos & 0x3)
3428 return -EINVAL;
3429
3430 while (size) {
3431 uint32_t value;
3432
Tom St Denis6fc0dea2016-08-29 08:39:29 -04003433 value = RREG32_SMC(*pos);
Tom St Denisadcec282016-04-15 13:08:44 -04003434 r = put_user(value, (uint32_t *)buf);
3435 if (r)
3436 return r;
3437
3438 result += 4;
3439 buf += 4;
3440 *pos += 4;
3441 size -= 4;
3442 }
3443
3444 return result;
3445}
3446
3447static ssize_t amdgpu_debugfs_regs_smc_write(struct file *f, const char __user *buf,
3448 size_t size, loff_t *pos)
3449{
Al Viro45063092016-12-04 18:24:56 -05003450 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003451 ssize_t result = 0;
3452 int r;
3453
3454 if (size & 0x3 || *pos & 0x3)
3455 return -EINVAL;
3456
3457 while (size) {
3458 uint32_t value;
3459
3460 r = get_user(value, (uint32_t *)buf);
3461 if (r)
3462 return r;
3463
Tom St Denis6fc0dea2016-08-29 08:39:29 -04003464 WREG32_SMC(*pos, value);
Tom St Denisadcec282016-04-15 13:08:44 -04003465
3466 result += 4;
3467 buf += 4;
3468 *pos += 4;
3469 size -= 4;
3470 }
3471
3472 return result;
3473}
3474
Tom St Denis1e051412016-06-27 09:57:18 -04003475static ssize_t amdgpu_debugfs_gca_config_read(struct file *f, char __user *buf,
3476 size_t size, loff_t *pos)
3477{
Al Viro45063092016-12-04 18:24:56 -05003478 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denis1e051412016-06-27 09:57:18 -04003479 ssize_t result = 0;
3480 int r;
3481 uint32_t *config, no_regs = 0;
3482
3483 if (size & 0x3 || *pos & 0x3)
3484 return -EINVAL;
3485
Markus Elfringecab7662016-09-18 17:00:52 +02003486 config = kmalloc_array(256, sizeof(*config), GFP_KERNEL);
Tom St Denis1e051412016-06-27 09:57:18 -04003487 if (!config)
3488 return -ENOMEM;
3489
3490 /* version, increment each time something is added */
Tom St Denis9a999352017-01-18 13:01:25 -05003491 config[no_regs++] = 3;
Tom St Denis1e051412016-06-27 09:57:18 -04003492 config[no_regs++] = adev->gfx.config.max_shader_engines;
3493 config[no_regs++] = adev->gfx.config.max_tile_pipes;
3494 config[no_regs++] = adev->gfx.config.max_cu_per_sh;
3495 config[no_regs++] = adev->gfx.config.max_sh_per_se;
3496 config[no_regs++] = adev->gfx.config.max_backends_per_se;
3497 config[no_regs++] = adev->gfx.config.max_texture_channel_caches;
3498 config[no_regs++] = adev->gfx.config.max_gprs;
3499 config[no_regs++] = adev->gfx.config.max_gs_threads;
3500 config[no_regs++] = adev->gfx.config.max_hw_contexts;
3501 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_frontend;
3502 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_backend;
3503 config[no_regs++] = adev->gfx.config.sc_hiz_tile_fifo_size;
3504 config[no_regs++] = adev->gfx.config.sc_earlyz_tile_fifo_size;
3505 config[no_regs++] = adev->gfx.config.num_tile_pipes;
3506 config[no_regs++] = adev->gfx.config.backend_enable_mask;
3507 config[no_regs++] = adev->gfx.config.mem_max_burst_length_bytes;
3508 config[no_regs++] = adev->gfx.config.mem_row_size_in_kb;
3509 config[no_regs++] = adev->gfx.config.shader_engine_tile_size;
3510 config[no_regs++] = adev->gfx.config.num_gpus;
3511 config[no_regs++] = adev->gfx.config.multi_gpu_tile_size;
3512 config[no_regs++] = adev->gfx.config.mc_arb_ramcfg;
3513 config[no_regs++] = adev->gfx.config.gb_addr_config;
3514 config[no_regs++] = adev->gfx.config.num_rbs;
3515
Tom St Denis89a8f302016-08-12 15:14:31 -04003516 /* rev==1 */
3517 config[no_regs++] = adev->rev_id;
3518 config[no_regs++] = adev->pg_flags;
3519 config[no_regs++] = adev->cg_flags;
3520
Tom St Denise9f11dc2016-08-17 12:00:51 -04003521 /* rev==2 */
3522 config[no_regs++] = adev->family;
3523 config[no_regs++] = adev->external_rev_id;
3524
Tom St Denis9a999352017-01-18 13:01:25 -05003525 /* rev==3 */
3526 config[no_regs++] = adev->pdev->device;
3527 config[no_regs++] = adev->pdev->revision;
3528 config[no_regs++] = adev->pdev->subsystem_device;
3529 config[no_regs++] = adev->pdev->subsystem_vendor;
3530
Tom St Denis1e051412016-06-27 09:57:18 -04003531 while (size && (*pos < no_regs * 4)) {
3532 uint32_t value;
3533
3534 value = config[*pos >> 2];
3535 r = put_user(value, (uint32_t *)buf);
3536 if (r) {
3537 kfree(config);
3538 return r;
3539 }
3540
3541 result += 4;
3542 buf += 4;
3543 *pos += 4;
3544 size -= 4;
3545 }
3546
3547 kfree(config);
3548 return result;
3549}
3550
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003551static ssize_t amdgpu_debugfs_sensor_read(struct file *f, char __user *buf,
3552 size_t size, loff_t *pos)
3553{
Al Viro45063092016-12-04 18:24:56 -05003554 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003555 int idx, x, outsize, r, valuesize;
3556 uint32_t values[16];
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003557
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003558 if (size & 3 || *pos & 0x3)
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003559 return -EINVAL;
3560
Samuel Pitoiset3cbc6142017-02-15 19:32:29 +01003561 if (amdgpu_dpm == 0)
3562 return -EINVAL;
3563
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003564 /* convert offset to sensor number */
3565 idx = *pos >> 2;
3566
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003567 valuesize = sizeof(values);
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003568 if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->read_sensor)
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003569 r = adev->powerplay.pp_funcs->read_sensor(adev->powerplay.pp_handle, idx, &values[0], &valuesize);
Samuel Pitoiset3cbc6142017-02-15 19:32:29 +01003570 else if (adev->pm.funcs && adev->pm.funcs->read_sensor)
3571 r = adev->pm.funcs->read_sensor(adev, idx, &values[0],
3572 &valuesize);
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003573 else
3574 return -EINVAL;
3575
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003576 if (size > valuesize)
3577 return -EINVAL;
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003578
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003579 outsize = 0;
3580 x = 0;
3581 if (!r) {
3582 while (size) {
3583 r = put_user(values[x++], (int32_t *)buf);
3584 buf += 4;
3585 size -= 4;
3586 outsize += 4;
3587 }
3588 }
3589
3590 return !r ? outsize : r;
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003591}
Tom St Denis1e051412016-06-27 09:57:18 -04003592
Tom St Denis273d7aa2016-10-11 14:48:55 -04003593static ssize_t amdgpu_debugfs_wave_read(struct file *f, char __user *buf,
3594 size_t size, loff_t *pos)
3595{
3596 struct amdgpu_device *adev = f->f_inode->i_private;
3597 int r, x;
3598 ssize_t result=0;
Tom St Denis472259f2016-10-14 09:49:09 -04003599 uint32_t offset, se, sh, cu, wave, simd, data[32];
Tom St Denis273d7aa2016-10-11 14:48:55 -04003600
3601 if (size & 3 || *pos & 3)
3602 return -EINVAL;
3603
3604 /* decode offset */
3605 offset = (*pos & 0x7F);
3606 se = ((*pos >> 7) & 0xFF);
3607 sh = ((*pos >> 15) & 0xFF);
3608 cu = ((*pos >> 23) & 0xFF);
3609 wave = ((*pos >> 31) & 0xFF);
3610 simd = ((*pos >> 37) & 0xFF);
Tom St Denis273d7aa2016-10-11 14:48:55 -04003611
3612 /* switch to the specific se/sh/cu */
3613 mutex_lock(&adev->grbm_idx_mutex);
3614 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
3615
3616 x = 0;
Tom St Denis472259f2016-10-14 09:49:09 -04003617 if (adev->gfx.funcs->read_wave_data)
3618 adev->gfx.funcs->read_wave_data(adev, simd, wave, data, &x);
Tom St Denis273d7aa2016-10-11 14:48:55 -04003619
3620 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
3621 mutex_unlock(&adev->grbm_idx_mutex);
3622
Tom St Denis5ecfb3b2016-10-13 12:15:03 -04003623 if (!x)
3624 return -EINVAL;
3625
Tom St Denis472259f2016-10-14 09:49:09 -04003626 while (size && (offset < x * 4)) {
Tom St Denis273d7aa2016-10-11 14:48:55 -04003627 uint32_t value;
3628
Tom St Denis472259f2016-10-14 09:49:09 -04003629 value = data[offset >> 2];
Tom St Denis273d7aa2016-10-11 14:48:55 -04003630 r = put_user(value, (uint32_t *)buf);
3631 if (r)
3632 return r;
3633
3634 result += 4;
3635 buf += 4;
Tom St Denis472259f2016-10-14 09:49:09 -04003636 offset += 4;
Tom St Denis273d7aa2016-10-11 14:48:55 -04003637 size -= 4;
3638 }
3639
3640 return result;
3641}
3642
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003643static ssize_t amdgpu_debugfs_gpr_read(struct file *f, char __user *buf,
3644 size_t size, loff_t *pos)
3645{
3646 struct amdgpu_device *adev = f->f_inode->i_private;
3647 int r;
3648 ssize_t result = 0;
3649 uint32_t offset, se, sh, cu, wave, simd, thread, bank, *data;
3650
3651 if (size & 3 || *pos & 3)
3652 return -EINVAL;
3653
3654 /* decode offset */
3655 offset = (*pos & 0xFFF); /* in dwords */
3656 se = ((*pos >> 12) & 0xFF);
3657 sh = ((*pos >> 20) & 0xFF);
3658 cu = ((*pos >> 28) & 0xFF);
3659 wave = ((*pos >> 36) & 0xFF);
3660 simd = ((*pos >> 44) & 0xFF);
3661 thread = ((*pos >> 52) & 0xFF);
3662 bank = ((*pos >> 60) & 1);
3663
3664 data = kmalloc_array(1024, sizeof(*data), GFP_KERNEL);
3665 if (!data)
3666 return -ENOMEM;
3667
3668 /* switch to the specific se/sh/cu */
3669 mutex_lock(&adev->grbm_idx_mutex);
3670 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
3671
3672 if (bank == 0) {
3673 if (adev->gfx.funcs->read_wave_vgprs)
3674 adev->gfx.funcs->read_wave_vgprs(adev, simd, wave, thread, offset, size>>2, data);
3675 } else {
3676 if (adev->gfx.funcs->read_wave_sgprs)
3677 adev->gfx.funcs->read_wave_sgprs(adev, simd, wave, offset, size>>2, data);
3678 }
3679
3680 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
3681 mutex_unlock(&adev->grbm_idx_mutex);
3682
3683 while (size) {
3684 uint32_t value;
3685
3686 value = data[offset++];
3687 r = put_user(value, (uint32_t *)buf);
3688 if (r) {
3689 result = r;
3690 goto err;
3691 }
3692
3693 result += 4;
3694 buf += 4;
3695 size -= 4;
3696 }
3697
3698err:
3699 kfree(data);
3700 return result;
3701}
3702
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003703static const struct file_operations amdgpu_debugfs_regs_fops = {
3704 .owner = THIS_MODULE,
3705 .read = amdgpu_debugfs_regs_read,
3706 .write = amdgpu_debugfs_regs_write,
3707 .llseek = default_llseek
3708};
Tom St Denisadcec282016-04-15 13:08:44 -04003709static const struct file_operations amdgpu_debugfs_regs_didt_fops = {
3710 .owner = THIS_MODULE,
3711 .read = amdgpu_debugfs_regs_didt_read,
3712 .write = amdgpu_debugfs_regs_didt_write,
3713 .llseek = default_llseek
3714};
3715static const struct file_operations amdgpu_debugfs_regs_pcie_fops = {
3716 .owner = THIS_MODULE,
3717 .read = amdgpu_debugfs_regs_pcie_read,
3718 .write = amdgpu_debugfs_regs_pcie_write,
3719 .llseek = default_llseek
3720};
3721static const struct file_operations amdgpu_debugfs_regs_smc_fops = {
3722 .owner = THIS_MODULE,
3723 .read = amdgpu_debugfs_regs_smc_read,
3724 .write = amdgpu_debugfs_regs_smc_write,
3725 .llseek = default_llseek
3726};
3727
Tom St Denis1e051412016-06-27 09:57:18 -04003728static const struct file_operations amdgpu_debugfs_gca_config_fops = {
3729 .owner = THIS_MODULE,
3730 .read = amdgpu_debugfs_gca_config_read,
3731 .llseek = default_llseek
3732};
3733
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003734static const struct file_operations amdgpu_debugfs_sensors_fops = {
3735 .owner = THIS_MODULE,
3736 .read = amdgpu_debugfs_sensor_read,
3737 .llseek = default_llseek
3738};
3739
Tom St Denis273d7aa2016-10-11 14:48:55 -04003740static const struct file_operations amdgpu_debugfs_wave_fops = {
3741 .owner = THIS_MODULE,
3742 .read = amdgpu_debugfs_wave_read,
3743 .llseek = default_llseek
3744};
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003745static const struct file_operations amdgpu_debugfs_gpr_fops = {
3746 .owner = THIS_MODULE,
3747 .read = amdgpu_debugfs_gpr_read,
3748 .llseek = default_llseek
3749};
Tom St Denis273d7aa2016-10-11 14:48:55 -04003750
Tom St Denisadcec282016-04-15 13:08:44 -04003751static const struct file_operations *debugfs_regs[] = {
3752 &amdgpu_debugfs_regs_fops,
3753 &amdgpu_debugfs_regs_didt_fops,
3754 &amdgpu_debugfs_regs_pcie_fops,
3755 &amdgpu_debugfs_regs_smc_fops,
Tom St Denis1e051412016-06-27 09:57:18 -04003756 &amdgpu_debugfs_gca_config_fops,
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003757 &amdgpu_debugfs_sensors_fops,
Tom St Denis273d7aa2016-10-11 14:48:55 -04003758 &amdgpu_debugfs_wave_fops,
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003759 &amdgpu_debugfs_gpr_fops,
Tom St Denisadcec282016-04-15 13:08:44 -04003760};
3761
3762static const char *debugfs_regs_names[] = {
3763 "amdgpu_regs",
3764 "amdgpu_regs_didt",
3765 "amdgpu_regs_pcie",
3766 "amdgpu_regs_smc",
Tom St Denis1e051412016-06-27 09:57:18 -04003767 "amdgpu_gca_config",
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003768 "amdgpu_sensors",
Tom St Denis273d7aa2016-10-11 14:48:55 -04003769 "amdgpu_wave",
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003770 "amdgpu_gpr",
Tom St Denisadcec282016-04-15 13:08:44 -04003771};
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003772
3773static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
3774{
3775 struct drm_minor *minor = adev->ddev->primary;
3776 struct dentry *ent, *root = minor->debugfs_root;
Tom St Denisadcec282016-04-15 13:08:44 -04003777 unsigned i, j;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003778
Tom St Denisadcec282016-04-15 13:08:44 -04003779 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3780 ent = debugfs_create_file(debugfs_regs_names[i],
3781 S_IFREG | S_IRUGO, root,
3782 adev, debugfs_regs[i]);
3783 if (IS_ERR(ent)) {
3784 for (j = 0; j < i; j++) {
3785 debugfs_remove(adev->debugfs_regs[i]);
3786 adev->debugfs_regs[i] = NULL;
3787 }
3788 return PTR_ERR(ent);
3789 }
3790
3791 if (!i)
3792 i_size_write(ent->d_inode, adev->rmmio_size);
3793 adev->debugfs_regs[i] = ent;
3794 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003795
3796 return 0;
3797}
3798
3799static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev)
3800{
Tom St Denisadcec282016-04-15 13:08:44 -04003801 unsigned i;
3802
3803 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3804 if (adev->debugfs_regs[i]) {
3805 debugfs_remove(adev->debugfs_regs[i]);
3806 adev->debugfs_regs[i] = NULL;
3807 }
3808 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003809}
3810
Huang Rui4f0955f2017-05-10 23:04:06 +08003811static int amdgpu_debugfs_test_ib(struct seq_file *m, void *data)
3812{
3813 struct drm_info_node *node = (struct drm_info_node *) m->private;
3814 struct drm_device *dev = node->minor->dev;
3815 struct amdgpu_device *adev = dev->dev_private;
3816 int r = 0, i;
3817
3818 /* hold on the scheduler */
3819 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
3820 struct amdgpu_ring *ring = adev->rings[i];
3821
3822 if (!ring || !ring->sched.thread)
3823 continue;
3824 kthread_park(ring->sched.thread);
3825 }
3826
3827 seq_printf(m, "run ib test:\n");
3828 r = amdgpu_ib_ring_tests(adev);
3829 if (r)
3830 seq_printf(m, "ib ring tests failed (%d).\n", r);
3831 else
3832 seq_printf(m, "ib ring tests passed.\n");
3833
3834 /* go on the scheduler */
3835 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
3836 struct amdgpu_ring *ring = adev->rings[i];
3837
3838 if (!ring || !ring->sched.thread)
3839 continue;
3840 kthread_unpark(ring->sched.thread);
3841 }
3842
3843 return 0;
3844}
3845
3846static const struct drm_info_list amdgpu_debugfs_test_ib_ring_list[] = {
3847 {"amdgpu_test_ib", &amdgpu_debugfs_test_ib}
3848};
3849
3850static int amdgpu_debugfs_test_ib_ring_init(struct amdgpu_device *adev)
3851{
3852 return amdgpu_debugfs_add_files(adev,
3853 amdgpu_debugfs_test_ib_ring_list, 1);
3854}
3855
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003856int amdgpu_debugfs_init(struct drm_minor *minor)
3857{
3858 return 0;
3859}
Alexander Kuleshov7cebc722015-06-27 13:16:05 +06003860#else
Arnd Bergmann27bad5b2017-06-21 23:51:02 +02003861static int amdgpu_debugfs_test_ib_ring_init(struct amdgpu_device *adev)
Huang Rui4f0955f2017-05-10 23:04:06 +08003862{
3863 return 0;
3864}
Alexander Kuleshov7cebc722015-06-27 13:16:05 +06003865static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
3866{
3867 return 0;
3868}
3869static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev) { }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003870#endif