blob: b10aecc7d146a6ddd7591b66b970f8b91103b837 [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>
Harry Wentland45622362017-09-12 15:58:20 -040034#include <drm/drm_atomic_helper.h>
Alex Deucherd38ceaf2015-04-20 16:55:21 -040035#include <drm/amdgpu_drm.h>
36#include <linux/vgaarb.h>
37#include <linux/vga_switcheroo.h>
38#include <linux/efi.h>
39#include "amdgpu.h"
Tom St Denisf4b373f2016-05-31 08:02:27 -040040#include "amdgpu_trace.h"
Alex Deucherd38ceaf2015-04-20 16:55:21 -040041#include "amdgpu_i2c.h"
42#include "atom.h"
43#include "amdgpu_atombios.h"
Alex Deuchera5bde2f2016-09-23 16:23:41 -040044#include "amdgpu_atomfirmware.h"
Alex Deucherd0dd7f02015-11-11 19:45:06 -050045#include "amd_pcie.h"
Ken Wang33f34802016-01-21 17:29:41 +080046#ifdef CONFIG_DRM_AMDGPU_SI
47#include "si.h"
48#endif
Alex Deuchera2e73f52015-04-20 17:09:27 -040049#ifdef CONFIG_DRM_AMDGPU_CIK
50#include "cik.h"
51#endif
Alex Deucheraaa36a92015-04-20 17:31:14 -040052#include "vi.h"
Ken Wang460826e2017-03-06 14:53:16 -050053#include "soc15.h"
Alex Deucherd38ceaf2015-04-20 16:55:21 -040054#include "bif/bif_4_1_d.h"
Emily Deng9accf2f2016-08-10 16:01:25 +080055#include <linux/pci.h>
Monk Liubec86372016-09-14 19:38:08 +080056#include <linux/firmware.h>
Gavin Wan89041942017-06-23 13:55:15 -040057#include "amdgpu_vf_error.h"
Alex Deucherd38ceaf2015-04-20 16:55:21 -040058
Yong Zhaoba997702015-11-09 17:21:45 -050059#include "amdgpu_amdkfd.h"
Rex Zhud2f52ac2017-09-22 17:47:27 +080060#include "amdgpu_pm.h"
Alex Deucherd38ceaf2015-04-20 16:55:21 -040061
Alex Deuchere2a75f82017-04-27 16:58:01 -040062MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin");
Alex Deucher2d2e5e72017-05-09 12:27:35 -040063MODULE_FIRMWARE("amdgpu/raven_gpu_info.bin");
Alex Deuchere2a75f82017-04-27 16:58:01 -040064
Shirish S2dc80b02017-05-25 10:05:25 +053065#define AMDGPU_RESUME_MS 2000
66
Alex Deucherd38ceaf2015-04-20 16:55:21 -040067static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev);
68static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev);
Christian König763efb62017-12-06 15:44:51 +010069static int amdgpu_debugfs_init(struct amdgpu_device *adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040070
71static const char *amdgpu_asic_name[] = {
Ken Wangda69c1612016-01-21 19:08:55 +080072 "TAHITI",
73 "PITCAIRN",
74 "VERDE",
75 "OLAND",
76 "HAINAN",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040077 "BONAIRE",
78 "KAVERI",
79 "KABINI",
80 "HAWAII",
81 "MULLINS",
82 "TOPAZ",
83 "TONGA",
David Zhang48299f92015-07-08 01:05:16 +080084 "FIJI",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040085 "CARRIZO",
Samuel Li139f4912015-10-08 14:50:27 -040086 "STONEY",
Flora Cui2cc0c0b2016-03-14 18:33:29 -040087 "POLARIS10",
88 "POLARIS11",
Junwei Zhangc4642a42016-12-14 15:32:28 -050089 "POLARIS12",
Ken Wangd4196f02016-03-09 09:28:32 +080090 "VEGA10",
Chunming Zhou2ca8a5d2016-12-07 17:31:19 +080091 "RAVEN",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040092 "LAST",
93};
94
95bool amdgpu_device_is_px(struct drm_device *dev)
96{
97 struct amdgpu_device *adev = dev->dev_private;
98
Jammy Zhou2f7d10b2015-07-22 11:29:01 +080099 if (adev->flags & AMD_IS_PX)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400100 return true;
101 return false;
102}
103
104/*
105 * MMIO register access helper functions.
106 */
107uint32_t amdgpu_mm_rreg(struct amdgpu_device *adev, uint32_t reg,
Monk Liu15d72fd2017-01-25 15:07:40 +0800108 uint32_t acc_flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400109{
Tom St Denisf4b373f2016-05-31 08:02:27 -0400110 uint32_t ret;
111
pding43ca8ef2017-10-13 15:38:35 +0800112 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev))
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800113 return amdgpu_virt_kiq_rreg(adev, reg);
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800114
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
pding43ca8ef2017-10-13 15:38:35 +0800138 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev))
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800139 return amdgpu_virt_kiq_wreg(adev, reg, v);
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800140
Monk Liu15d72fd2017-01-25 15:07:40 +0800141 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400142 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
143 else {
144 unsigned long flags;
145
146 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
147 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
148 writel(v, ((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
149 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
150 }
Ken Wang47ed4e12017-07-04 13:11:52 +0800151
152 if (adev->asic_type >= CHIP_VEGA10 && reg == 1 && adev->last_mm_index == 0x5702C) {
153 udelay(500);
154 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400155}
156
157u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 reg)
158{
159 if ((reg * 4) < adev->rio_mem_size)
160 return ioread32(adev->rio_mem + (reg * 4));
161 else {
162 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
163 return ioread32(adev->rio_mem + (mmMM_DATA * 4));
164 }
165}
166
167void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
168{
Ken Wang47ed4e12017-07-04 13:11:52 +0800169 if (adev->asic_type >= CHIP_VEGA10 && reg == 0) {
170 adev->last_mm_index = v;
171 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400172
173 if ((reg * 4) < adev->rio_mem_size)
174 iowrite32(v, adev->rio_mem + (reg * 4));
175 else {
176 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
177 iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
178 }
Ken Wang47ed4e12017-07-04 13:11:52 +0800179
180 if (adev->asic_type >= CHIP_VEGA10 && reg == 1 && adev->last_mm_index == 0x5702C) {
181 udelay(500);
182 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400183}
184
185/**
186 * amdgpu_mm_rdoorbell - read a doorbell dword
187 *
188 * @adev: amdgpu_device pointer
189 * @index: doorbell index
190 *
191 * Returns the value in the doorbell aperture at the
192 * requested doorbell index (CIK).
193 */
194u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
195{
196 if (index < adev->doorbell.num_doorbells) {
197 return readl(adev->doorbell.ptr + index);
198 } else {
199 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
200 return 0;
201 }
202}
203
204/**
205 * amdgpu_mm_wdoorbell - write a doorbell dword
206 *
207 * @adev: amdgpu_device pointer
208 * @index: doorbell index
209 * @v: value to write
210 *
211 * Writes @v to the doorbell aperture at the
212 * requested doorbell index (CIK).
213 */
214void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
215{
216 if (index < adev->doorbell.num_doorbells) {
217 writel(v, adev->doorbell.ptr + index);
218 } else {
219 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
220 }
221}
222
223/**
Ken Wang832be402016-03-18 15:23:08 +0800224 * amdgpu_mm_rdoorbell64 - read a doorbell Qword
225 *
226 * @adev: amdgpu_device pointer
227 * @index: doorbell index
228 *
229 * Returns the value in the doorbell aperture at the
230 * requested doorbell index (VEGA10+).
231 */
232u64 amdgpu_mm_rdoorbell64(struct amdgpu_device *adev, u32 index)
233{
234 if (index < adev->doorbell.num_doorbells) {
235 return atomic64_read((atomic64_t *)(adev->doorbell.ptr + index));
236 } else {
237 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
238 return 0;
239 }
240}
241
242/**
243 * amdgpu_mm_wdoorbell64 - write a doorbell Qword
244 *
245 * @adev: amdgpu_device pointer
246 * @index: doorbell index
247 * @v: value to write
248 *
249 * Writes @v to the doorbell aperture at the
250 * requested doorbell index (VEGA10+).
251 */
252void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v)
253{
254 if (index < adev->doorbell.num_doorbells) {
255 atomic64_set((atomic64_t *)(adev->doorbell.ptr + index), v);
256 } else {
257 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
258 }
259}
260
261/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400262 * amdgpu_invalid_rreg - dummy reg read function
263 *
264 * @adev: amdgpu device pointer
265 * @reg: offset of register
266 *
267 * Dummy register read function. Used for register blocks
268 * that certain asics don't have (all asics).
269 * Returns the value in the register.
270 */
271static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
272{
273 DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
274 BUG();
275 return 0;
276}
277
278/**
279 * amdgpu_invalid_wreg - dummy reg write function
280 *
281 * @adev: amdgpu device pointer
282 * @reg: offset of register
283 * @v: value to write to the register
284 *
285 * Dummy register read function. Used for register blocks
286 * that certain asics don't have (all asics).
287 */
288static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
289{
290 DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
291 reg, v);
292 BUG();
293}
294
295/**
296 * amdgpu_block_invalid_rreg - dummy reg read function
297 *
298 * @adev: amdgpu device pointer
299 * @block: offset of instance
300 * @reg: offset of register
301 *
302 * Dummy register read function. Used for register blocks
303 * that certain asics don't have (all asics).
304 * Returns the value in the register.
305 */
306static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
307 uint32_t block, uint32_t reg)
308{
309 DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
310 reg, block);
311 BUG();
312 return 0;
313}
314
315/**
316 * amdgpu_block_invalid_wreg - dummy reg write function
317 *
318 * @adev: amdgpu device pointer
319 * @block: offset of instance
320 * @reg: offset of register
321 * @v: value to write to the register
322 *
323 * Dummy register read function. Used for register blocks
324 * that certain asics don't have (all asics).
325 */
326static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
327 uint32_t block,
328 uint32_t reg, uint32_t v)
329{
330 DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
331 reg, block, v);
332 BUG();
333}
334
335static int amdgpu_vram_scratch_init(struct amdgpu_device *adev)
336{
Christian Königa4a02772017-07-27 17:24:36 +0200337 return amdgpu_bo_create_kernel(adev, AMDGPU_GPU_PAGE_SIZE,
338 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
339 &adev->vram_scratch.robj,
340 &adev->vram_scratch.gpu_addr,
341 (void **)&adev->vram_scratch.ptr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400342}
343
344static void amdgpu_vram_scratch_fini(struct amdgpu_device *adev)
345{
Christian König078af1a2017-07-27 17:43:00 +0200346 amdgpu_bo_free_kernel(&adev->vram_scratch.robj, NULL, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400347}
348
349/**
350 * amdgpu_program_register_sequence - program an array of registers.
351 *
352 * @adev: amdgpu_device pointer
353 * @registers: pointer to the register array
354 * @array_size: size of the register array
355 *
356 * Programs an array or registers with and and or masks.
357 * This is a helper for setting golden registers.
358 */
359void amdgpu_program_register_sequence(struct amdgpu_device *adev,
360 const u32 *registers,
361 const u32 array_size)
362{
363 u32 tmp, reg, and_mask, or_mask;
364 int i;
365
366 if (array_size % 3)
367 return;
368
369 for (i = 0; i < array_size; i +=3) {
370 reg = registers[i + 0];
371 and_mask = registers[i + 1];
372 or_mask = registers[i + 2];
373
374 if (and_mask == 0xffffffff) {
375 tmp = or_mask;
376 } else {
377 tmp = RREG32(reg);
378 tmp &= ~and_mask;
379 tmp |= or_mask;
380 }
381 WREG32(reg, tmp);
382 }
383}
384
385void amdgpu_pci_config_reset(struct amdgpu_device *adev)
386{
387 pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
388}
389
390/*
391 * GPU doorbell aperture helpers function.
392 */
393/**
394 * amdgpu_doorbell_init - Init doorbell driver information.
395 *
396 * @adev: amdgpu_device pointer
397 *
398 * Init doorbell driver information (CIK)
399 * Returns 0 on success, error on failure.
400 */
401static int amdgpu_doorbell_init(struct amdgpu_device *adev)
402{
Christian König705e5192017-06-08 11:15:16 +0200403 /* No doorbell on SI hardware generation */
404 if (adev->asic_type < CHIP_BONAIRE) {
405 adev->doorbell.base = 0;
406 adev->doorbell.size = 0;
407 adev->doorbell.num_doorbells = 0;
408 adev->doorbell.ptr = NULL;
409 return 0;
410 }
411
Christian Königd6895ad2017-02-28 10:36:43 +0100412 if (pci_resource_flags(adev->pdev, 2) & IORESOURCE_UNSET)
413 return -EINVAL;
414
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400415 /* doorbell bar mapping */
416 adev->doorbell.base = pci_resource_start(adev->pdev, 2);
417 adev->doorbell.size = pci_resource_len(adev->pdev, 2);
418
Christian Königedf600d2016-05-03 15:54:54 +0200419 adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400420 AMDGPU_DOORBELL_MAX_ASSIGNMENT+1);
421 if (adev->doorbell.num_doorbells == 0)
422 return -EINVAL;
423
Christian König8972e5d2017-03-06 13:34:57 +0100424 adev->doorbell.ptr = ioremap(adev->doorbell.base,
425 adev->doorbell.num_doorbells *
426 sizeof(u32));
427 if (adev->doorbell.ptr == NULL)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400428 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400429
430 return 0;
431}
432
433/**
434 * amdgpu_doorbell_fini - Tear down doorbell driver information.
435 *
436 * @adev: amdgpu_device pointer
437 *
438 * Tear down doorbell driver information (CIK)
439 */
440static void amdgpu_doorbell_fini(struct amdgpu_device *adev)
441{
442 iounmap(adev->doorbell.ptr);
443 adev->doorbell.ptr = NULL;
444}
445
446/**
447 * amdgpu_doorbell_get_kfd_info - Report doorbell configuration required to
448 * setup amdkfd
449 *
450 * @adev: amdgpu_device pointer
451 * @aperture_base: output returning doorbell aperture base physical address
452 * @aperture_size: output returning doorbell aperture size in bytes
453 * @start_offset: output returning # of doorbell bytes reserved for amdgpu.
454 *
455 * amdgpu and amdkfd share the doorbell aperture. amdgpu sets it up,
456 * takes doorbells required for its own rings and reports the setup to amdkfd.
457 * amdgpu reserved doorbells are at the start of the doorbell aperture.
458 */
459void amdgpu_doorbell_get_kfd_info(struct amdgpu_device *adev,
460 phys_addr_t *aperture_base,
461 size_t *aperture_size,
462 size_t *start_offset)
463{
464 /*
465 * The first num_doorbells are used by amdgpu.
466 * amdkfd takes whatever's left in the aperture.
467 */
468 if (adev->doorbell.size > adev->doorbell.num_doorbells * sizeof(u32)) {
469 *aperture_base = adev->doorbell.base;
470 *aperture_size = adev->doorbell.size;
471 *start_offset = adev->doorbell.num_doorbells * sizeof(u32);
472 } else {
473 *aperture_base = 0;
474 *aperture_size = 0;
475 *start_offset = 0;
476 }
477}
478
479/*
480 * amdgpu_wb_*()
Alex Xie455a7bc2017-05-08 21:36:03 -0400481 * Writeback is the method by which the GPU updates special pages in memory
Alex Xieea81a172017-05-08 13:41:11 -0400482 * with the status of certain GPU events (fences, ring pointers,etc.).
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400483 */
484
485/**
486 * amdgpu_wb_fini - Disable Writeback and free memory
487 *
488 * @adev: amdgpu_device pointer
489 *
490 * Disables Writeback and frees the Writeback memory (all asics).
491 * Used at driver shutdown.
492 */
493static void amdgpu_wb_fini(struct amdgpu_device *adev)
494{
495 if (adev->wb.wb_obj) {
Alex Deuchera76ed482016-10-21 15:30:36 -0400496 amdgpu_bo_free_kernel(&adev->wb.wb_obj,
497 &adev->wb.gpu_addr,
498 (void **)&adev->wb.wb);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400499 adev->wb.wb_obj = NULL;
500 }
501}
502
503/**
504 * amdgpu_wb_init- Init Writeback driver info and allocate memory
505 *
506 * @adev: amdgpu_device pointer
507 *
Alex Xie455a7bc2017-05-08 21:36:03 -0400508 * Initializes writeback and allocates writeback memory (all asics).
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400509 * Used at driver startup.
510 * Returns 0 on success or an -error on failure.
511 */
512static int amdgpu_wb_init(struct amdgpu_device *adev)
513{
514 int r;
515
516 if (adev->wb.wb_obj == NULL) {
Alex Deucher97407b62017-07-28 12:14:15 -0400517 /* AMDGPU_MAX_WB * sizeof(uint32_t) * 8 = AMDGPU_MAX_WB 256bit slots */
518 r = amdgpu_bo_create_kernel(adev, AMDGPU_MAX_WB * sizeof(uint32_t) * 8,
Alex Deuchera76ed482016-10-21 15:30:36 -0400519 PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
520 &adev->wb.wb_obj, &adev->wb.gpu_addr,
521 (void **)&adev->wb.wb);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400522 if (r) {
523 dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
524 return r;
525 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400526
527 adev->wb.num_wb = AMDGPU_MAX_WB;
528 memset(&adev->wb.used, 0, sizeof(adev->wb.used));
529
530 /* clear wb memory */
Huang Rui60a970a62017-03-15 10:13:32 +0800531 memset((char *)adev->wb.wb, 0, AMDGPU_MAX_WB * sizeof(uint32_t));
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400532 }
533
534 return 0;
535}
536
537/**
538 * amdgpu_wb_get - Allocate a wb entry
539 *
540 * @adev: amdgpu_device pointer
541 * @wb: wb index
542 *
543 * Allocate a wb slot for use by the driver (all asics).
544 * Returns 0 on success or -EINVAL on failure.
545 */
546int amdgpu_wb_get(struct amdgpu_device *adev, u32 *wb)
547{
548 unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
Alex Deucher97407b62017-07-28 12:14:15 -0400549
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400550 if (offset < adev->wb.num_wb) {
551 __set_bit(offset, adev->wb.used);
Monk Liu63ae07c2017-10-17 19:18:56 +0800552 *wb = offset << 3; /* convert to dw offset */
Monk Liu0915fdb2017-06-19 10:19:41 -0400553 return 0;
554 } else {
555 return -EINVAL;
556 }
557}
558
Ken Wang70142852016-03-18 15:08:49 +0800559/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400560 * amdgpu_wb_free - Free a wb entry
561 *
562 * @adev: amdgpu_device pointer
563 * @wb: wb index
564 *
565 * Free a wb slot allocated for use by the driver (all asics)
566 */
567void amdgpu_wb_free(struct amdgpu_device *adev, u32 wb)
568{
569 if (wb < adev->wb.num_wb)
Monk Liu63ae07c2017-10-17 19:18:56 +0800570 __clear_bit(wb >> 3, adev->wb.used);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400571}
572
573/**
574 * amdgpu_vram_location - try to find VRAM location
575 * @adev: amdgpu device structure holding all necessary informations
576 * @mc: memory controller structure holding memory informations
577 * @base: base address at which to put VRAM
578 *
Alex Xie455a7bc2017-05-08 21:36:03 -0400579 * Function will try to place VRAM at base address provided
Christian König3d647c82017-11-16 19:36:10 +0100580 * as parameter.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400581 */
582void amdgpu_vram_location(struct amdgpu_device *adev, struct amdgpu_mc *mc, u64 base)
583{
584 uint64_t limit = (uint64_t)amdgpu_vram_limit << 20;
585
586 mc->vram_start = base;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400587 mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
588 if (limit && limit < mc->real_vram_size)
589 mc->real_vram_size = limit;
590 dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
591 mc->mc_vram_size >> 20, mc->vram_start,
592 mc->vram_end, mc->real_vram_size >> 20);
593}
594
595/**
Christian König6f02a692017-07-07 11:56:59 +0200596 * amdgpu_gart_location - try to find GTT location
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400597 * @adev: amdgpu device structure holding all necessary informations
598 * @mc: memory controller structure holding memory informations
599 *
600 * Function will place try to place GTT before or after VRAM.
601 *
602 * If GTT size is bigger than space left then we ajust GTT size.
603 * Thus function will never fails.
604 *
605 * FIXME: when reducing GTT size align new size on power of 2.
606 */
Christian König6f02a692017-07-07 11:56:59 +0200607void amdgpu_gart_location(struct amdgpu_device *adev, struct amdgpu_mc *mc)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400608{
609 u64 size_af, size_bf;
610
Christian Königed21c042017-07-06 22:26:05 +0200611 size_af = adev->mc.mc_mask - mc->vram_end;
612 size_bf = mc->vram_start;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400613 if (size_bf > size_af) {
Christian König6f02a692017-07-07 11:56:59 +0200614 if (mc->gart_size > size_bf) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400615 dev_warn(adev->dev, "limiting GTT\n");
Christian König6f02a692017-07-07 11:56:59 +0200616 mc->gart_size = size_bf;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400617 }
Christian König6f02a692017-07-07 11:56:59 +0200618 mc->gart_start = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400619 } else {
Christian König6f02a692017-07-07 11:56:59 +0200620 if (mc->gart_size > size_af) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400621 dev_warn(adev->dev, "limiting GTT\n");
Christian König6f02a692017-07-07 11:56:59 +0200622 mc->gart_size = size_af;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400623 }
Christian Königb98f1b92017-11-16 20:12:51 +0100624 /* VCE doesn't like it when BOs cross a 4GB segment, so align
625 * the GART base on a 4GB boundary as well.
626 */
627 mc->gart_start = ALIGN(mc->vram_end + 1, 0x100000000ULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400628 }
Christian König6f02a692017-07-07 11:56:59 +0200629 mc->gart_end = mc->gart_start + mc->gart_size - 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400630 dev_info(adev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
Christian König6f02a692017-07-07 11:56:59 +0200631 mc->gart_size >> 20, mc->gart_start, mc->gart_end);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400632}
633
634/*
Horace Chena05502e2017-09-29 14:41:57 +0800635 * Firmware Reservation functions
636 */
637/**
638 * amdgpu_fw_reserve_vram_fini - free fw reserved vram
639 *
640 * @adev: amdgpu_device pointer
641 *
642 * free fw reserved vram if it has been reserved.
643 */
644void amdgpu_fw_reserve_vram_fini(struct amdgpu_device *adev)
645{
646 amdgpu_bo_free_kernel(&adev->fw_vram_usage.reserved_bo,
647 NULL, &adev->fw_vram_usage.va);
648}
649
650/**
651 * amdgpu_fw_reserve_vram_init - create bo vram reservation from fw
652 *
653 * @adev: amdgpu_device pointer
654 *
655 * create bo vram reservation from fw.
656 */
657int amdgpu_fw_reserve_vram_init(struct amdgpu_device *adev)
658{
Christian Königc13c55d2017-04-12 15:33:00 +0200659 struct ttm_operation_ctx ctx = { false, false };
Horace Chena05502e2017-09-29 14:41:57 +0800660 int r = 0;
Horace Chen3c738892017-11-01 19:32:11 +0800661 int i;
Horace Chena05502e2017-09-29 14:41:57 +0800662 u64 vram_size = adev->mc.visible_vram_size;
Horace Chen3c738892017-11-01 19:32:11 +0800663 u64 offset = adev->fw_vram_usage.start_offset;
664 u64 size = adev->fw_vram_usage.size;
665 struct amdgpu_bo *bo;
Horace Chena05502e2017-09-29 14:41:57 +0800666
667 adev->fw_vram_usage.va = NULL;
668 adev->fw_vram_usage.reserved_bo = NULL;
669
670 if (adev->fw_vram_usage.size > 0 &&
671 adev->fw_vram_usage.size <= vram_size) {
672
673 r = amdgpu_bo_create(adev, adev->fw_vram_usage.size,
Horace Chen3c738892017-11-01 19:32:11 +0800674 PAGE_SIZE, true, AMDGPU_GEM_DOMAIN_VRAM,
Horace Chena05502e2017-09-29 14:41:57 +0800675 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
676 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS, NULL, NULL, 0,
677 &adev->fw_vram_usage.reserved_bo);
678 if (r)
679 goto error_create;
680
681 r = amdgpu_bo_reserve(adev->fw_vram_usage.reserved_bo, false);
682 if (r)
683 goto error_reserve;
Horace Chen3c738892017-11-01 19:32:11 +0800684
685 /* remove the original mem node and create a new one at the
686 * request position
687 */
688 bo = adev->fw_vram_usage.reserved_bo;
689 offset = ALIGN(offset, PAGE_SIZE);
690 for (i = 0; i < bo->placement.num_placement; ++i) {
691 bo->placements[i].fpfn = offset >> PAGE_SHIFT;
692 bo->placements[i].lpfn = (offset + size) >> PAGE_SHIFT;
693 }
694
695 ttm_bo_mem_put(&bo->tbo, &bo->tbo.mem);
Christian Königc13c55d2017-04-12 15:33:00 +0200696 r = ttm_bo_mem_space(&bo->tbo, &bo->placement,
697 &bo->tbo.mem, &ctx);
Horace Chen3c738892017-11-01 19:32:11 +0800698 if (r)
699 goto error_pin;
700
Horace Chena05502e2017-09-29 14:41:57 +0800701 r = amdgpu_bo_pin_restricted(adev->fw_vram_usage.reserved_bo,
702 AMDGPU_GEM_DOMAIN_VRAM,
703 adev->fw_vram_usage.start_offset,
704 (adev->fw_vram_usage.start_offset +
Monk Liu99211672017-11-14 16:50:31 +0800705 adev->fw_vram_usage.size), NULL);
Horace Chena05502e2017-09-29 14:41:57 +0800706 if (r)
707 goto error_pin;
708 r = amdgpu_bo_kmap(adev->fw_vram_usage.reserved_bo,
709 &adev->fw_vram_usage.va);
710 if (r)
711 goto error_kmap;
712
713 amdgpu_bo_unreserve(adev->fw_vram_usage.reserved_bo);
714 }
715 return r;
716
717error_kmap:
718 amdgpu_bo_unpin(adev->fw_vram_usage.reserved_bo);
719error_pin:
720 amdgpu_bo_unreserve(adev->fw_vram_usage.reserved_bo);
721error_reserve:
722 amdgpu_bo_unref(&adev->fw_vram_usage.reserved_bo);
723error_create:
724 adev->fw_vram_usage.va = NULL;
725 adev->fw_vram_usage.reserved_bo = NULL;
726 return r;
727}
728
Christian Königd6895ad2017-02-28 10:36:43 +0100729/**
730 * amdgpu_device_resize_fb_bar - try to resize FB BAR
731 *
732 * @adev: amdgpu_device pointer
733 *
734 * Try to resize FB BAR to make all VRAM CPU accessible. We try very hard not
735 * to fail, but if any of the BARs is not accessible after the size we abort
736 * driver loading by returning -ENODEV.
737 */
738int amdgpu_device_resize_fb_bar(struct amdgpu_device *adev)
739{
740 u64 space_needed = roundup_pow_of_two(adev->mc.real_vram_size);
741 u32 rbar_size = order_base_2(((space_needed >> 20) | 1)) - 1;
Christian König31b8ada2017-11-15 20:07:38 +0100742 struct pci_bus *root;
743 struct resource *res;
744 unsigned i;
Christian Königd6895ad2017-02-28 10:36:43 +0100745 u16 cmd;
746 int r;
747
pding0c03b912017-11-07 11:02:00 +0800748 /* Bypass for VF */
749 if (amdgpu_sriov_vf(adev))
750 return 0;
751
Christian König31b8ada2017-11-15 20:07:38 +0100752 /* Check if the root BUS has 64bit memory resources */
753 root = adev->pdev->bus;
754 while (root->parent)
755 root = root->parent;
756
757 pci_bus_for_each_resource(root, res, i) {
758 if (res && res->flags & IORESOURCE_MEM_64 &&
759 res->start > 0x100000000ull)
760 break;
761 }
762
763 /* Trying to resize is pointless without a root hub window above 4GB */
764 if (!res)
765 return 0;
766
Christian Königd6895ad2017-02-28 10:36:43 +0100767 /* Disable memory decoding while we change the BAR addresses and size */
768 pci_read_config_word(adev->pdev, PCI_COMMAND, &cmd);
769 pci_write_config_word(adev->pdev, PCI_COMMAND,
770 cmd & ~PCI_COMMAND_MEMORY);
771
772 /* Free the VRAM and doorbell BAR, we most likely need to move both. */
773 amdgpu_doorbell_fini(adev);
774 if (adev->asic_type >= CHIP_BONAIRE)
775 pci_release_resource(adev->pdev, 2);
776
777 pci_release_resource(adev->pdev, 0);
778
779 r = pci_resize_resource(adev->pdev, 0, rbar_size);
780 if (r == -ENOSPC)
781 DRM_INFO("Not enough PCI address space for a large BAR.");
782 else if (r && r != -ENOTSUPP)
783 DRM_ERROR("Problem resizing BAR0 (%d).", r);
784
785 pci_assign_unassigned_bus_resources(adev->pdev->bus);
786
787 /* When the doorbell or fb BAR isn't available we have no chance of
788 * using the device.
789 */
790 r = amdgpu_doorbell_init(adev);
791 if (r || (pci_resource_flags(adev->pdev, 0) & IORESOURCE_UNSET))
792 return -ENODEV;
793
794 pci_write_config_word(adev->pdev, PCI_COMMAND, cmd);
795
796 return 0;
797}
Horace Chena05502e2017-09-29 14:41:57 +0800798
799/*
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400800 * GPU helpers function.
801 */
802/**
Jim Quc836fec2017-02-10 15:59:59 +0800803 * amdgpu_need_post - check if the hw need post or not
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400804 *
805 * @adev: amdgpu_device pointer
806 *
Jim Quc836fec2017-02-10 15:59:59 +0800807 * Check if the asic has been initialized (all asics) at driver startup
808 * or post is needed if hw reset is performed.
809 * Returns true if need or false if not.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400810 */
Jim Quc836fec2017-02-10 15:59:59 +0800811bool amdgpu_need_post(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400812{
813 uint32_t reg;
814
Monk Liubec86372016-09-14 19:38:08 +0800815 if (amdgpu_sriov_vf(adev))
816 return false;
817
818 if (amdgpu_passthrough(adev)) {
Monk Liu1da2c322016-11-11 11:24:29 +0800819 /* for FIJI: In whole GPU pass-through virtualization case, after VM reboot
820 * some old smc fw still need driver do vPost otherwise gpu hang, while
821 * those smc fw version above 22.15 doesn't have this flaw, so we force
822 * vpost executed for smc version below 22.15
Monk Liubec86372016-09-14 19:38:08 +0800823 */
824 if (adev->asic_type == CHIP_FIJI) {
825 int err;
826 uint32_t fw_ver;
827 err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
828 /* force vPost if error occured */
829 if (err)
830 return true;
831
832 fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
Monk Liu1da2c322016-11-11 11:24:29 +0800833 if (fw_ver < 0x00160e00)
834 return true;
Monk Liubec86372016-09-14 19:38:08 +0800835 }
Monk Liubec86372016-09-14 19:38:08 +0800836 }
pding91fe77e2017-10-19 09:38:39 +0800837
838 if (adev->has_hw_reset) {
839 adev->has_hw_reset = false;
840 return true;
841 }
842
843 /* bios scratch used on CIK+ */
844 if (adev->asic_type >= CHIP_BONAIRE)
845 return amdgpu_atombios_scratch_need_asic_init(adev);
846
847 /* check MEM_SIZE for older asics */
848 reg = amdgpu_asic_get_config_memsize(adev);
849
850 if ((reg != 0) && (reg != 0xffffffff))
851 return false;
852
853 return true;
Monk Liubec86372016-09-14 19:38:08 +0800854}
855
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400856/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400857 * amdgpu_dummy_page_init - init dummy page used by the driver
858 *
859 * @adev: amdgpu_device pointer
860 *
861 * Allocate the dummy page used by the driver (all asics).
862 * This dummy page is used by the driver as a filler for gart entries
863 * when pages are taken out of the GART
864 * Returns 0 on sucess, -ENOMEM on failure.
865 */
866int amdgpu_dummy_page_init(struct amdgpu_device *adev)
867{
868 if (adev->dummy_page.page)
869 return 0;
870 adev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
871 if (adev->dummy_page.page == NULL)
872 return -ENOMEM;
873 adev->dummy_page.addr = pci_map_page(adev->pdev, adev->dummy_page.page,
874 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
875 if (pci_dma_mapping_error(adev->pdev, adev->dummy_page.addr)) {
876 dev_err(&adev->pdev->dev, "Failed to DMA MAP the dummy page\n");
877 __free_page(adev->dummy_page.page);
878 adev->dummy_page.page = NULL;
879 return -ENOMEM;
880 }
881 return 0;
882}
883
884/**
885 * amdgpu_dummy_page_fini - free dummy page used by the driver
886 *
887 * @adev: amdgpu_device pointer
888 *
889 * Frees the dummy page used by the driver (all asics).
890 */
891void amdgpu_dummy_page_fini(struct amdgpu_device *adev)
892{
893 if (adev->dummy_page.page == NULL)
894 return;
895 pci_unmap_page(adev->pdev, adev->dummy_page.addr,
896 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
897 __free_page(adev->dummy_page.page);
898 adev->dummy_page.page = NULL;
899}
900
901
902/* ATOM accessor methods */
903/*
904 * ATOM is an interpreted byte code stored in tables in the vbios. The
905 * driver registers callbacks to access registers and the interpreter
906 * in the driver parses the tables and executes then to program specific
907 * actions (set display modes, asic init, etc.). See amdgpu_atombios.c,
908 * atombios.h, and atom.c
909 */
910
911/**
912 * cail_pll_read - read PLL register
913 *
914 * @info: atom card_info pointer
915 * @reg: PLL register offset
916 *
917 * Provides a PLL register accessor for the atom interpreter (r4xx+).
918 * Returns the value of the PLL register.
919 */
920static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
921{
922 return 0;
923}
924
925/**
926 * cail_pll_write - write PLL register
927 *
928 * @info: atom card_info pointer
929 * @reg: PLL register offset
930 * @val: value to write to the pll register
931 *
932 * Provides a PLL register accessor for the atom interpreter (r4xx+).
933 */
934static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
935{
936
937}
938
939/**
940 * cail_mc_read - read MC (Memory Controller) register
941 *
942 * @info: atom card_info pointer
943 * @reg: MC register offset
944 *
945 * Provides an MC register accessor for the atom interpreter (r4xx+).
946 * Returns the value of the MC register.
947 */
948static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
949{
950 return 0;
951}
952
953/**
954 * cail_mc_write - write MC (Memory Controller) register
955 *
956 * @info: atom card_info pointer
957 * @reg: MC register offset
958 * @val: value to write to the pll register
959 *
960 * Provides a MC register accessor for the atom interpreter (r4xx+).
961 */
962static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
963{
964
965}
966
967/**
968 * cail_reg_write - write MMIO register
969 *
970 * @info: atom card_info pointer
971 * @reg: MMIO register offset
972 * @val: value to write to the pll register
973 *
974 * Provides a MMIO register accessor for the atom interpreter (r4xx+).
975 */
976static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
977{
978 struct amdgpu_device *adev = info->dev->dev_private;
979
980 WREG32(reg, val);
981}
982
983/**
984 * cail_reg_read - read MMIO register
985 *
986 * @info: atom card_info pointer
987 * @reg: MMIO register offset
988 *
989 * Provides an MMIO register accessor for the atom interpreter (r4xx+).
990 * Returns the value of the MMIO register.
991 */
992static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
993{
994 struct amdgpu_device *adev = info->dev->dev_private;
995 uint32_t r;
996
997 r = RREG32(reg);
998 return r;
999}
1000
1001/**
1002 * cail_ioreg_write - write IO register
1003 *
1004 * @info: atom card_info pointer
1005 * @reg: IO register offset
1006 * @val: value to write to the pll register
1007 *
1008 * Provides a IO register accessor for the atom interpreter (r4xx+).
1009 */
1010static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
1011{
1012 struct amdgpu_device *adev = info->dev->dev_private;
1013
1014 WREG32_IO(reg, val);
1015}
1016
1017/**
1018 * cail_ioreg_read - read IO register
1019 *
1020 * @info: atom card_info pointer
1021 * @reg: IO register offset
1022 *
1023 * Provides an IO register accessor for the atom interpreter (r4xx+).
1024 * Returns the value of the IO register.
1025 */
1026static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
1027{
1028 struct amdgpu_device *adev = info->dev->dev_private;
1029 uint32_t r;
1030
1031 r = RREG32_IO(reg);
1032 return r;
1033}
1034
Kent Russell5b41d942017-08-22 12:31:43 -04001035static ssize_t amdgpu_atombios_get_vbios_version(struct device *dev,
1036 struct device_attribute *attr,
1037 char *buf)
1038{
1039 struct drm_device *ddev = dev_get_drvdata(dev);
1040 struct amdgpu_device *adev = ddev->dev_private;
1041 struct atom_context *ctx = adev->mode_info.atom_context;
1042
1043 return snprintf(buf, PAGE_SIZE, "%s\n", ctx->vbios_version);
1044}
1045
1046static DEVICE_ATTR(vbios_version, 0444, amdgpu_atombios_get_vbios_version,
1047 NULL);
1048
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001049/**
1050 * amdgpu_atombios_fini - free the driver info and callbacks for atombios
1051 *
1052 * @adev: amdgpu_device pointer
1053 *
1054 * Frees the driver info and register access callbacks for the ATOM
1055 * interpreter (r4xx+).
1056 * Called at driver shutdown.
1057 */
1058static void amdgpu_atombios_fini(struct amdgpu_device *adev)
1059{
Monk Liu89e0ec92016-05-27 19:34:11 +08001060 if (adev->mode_info.atom_context) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001061 kfree(adev->mode_info.atom_context->scratch);
Monk Liu89e0ec92016-05-27 19:34:11 +08001062 kfree(adev->mode_info.atom_context->iio);
1063 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001064 kfree(adev->mode_info.atom_context);
1065 adev->mode_info.atom_context = NULL;
1066 kfree(adev->mode_info.atom_card_info);
1067 adev->mode_info.atom_card_info = NULL;
Kent Russell5b41d942017-08-22 12:31:43 -04001068 device_remove_file(adev->dev, &dev_attr_vbios_version);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001069}
1070
1071/**
1072 * amdgpu_atombios_init - init the driver info and callbacks for atombios
1073 *
1074 * @adev: amdgpu_device pointer
1075 *
1076 * Initializes the driver info and register access callbacks for the
1077 * ATOM interpreter (r4xx+).
1078 * Returns 0 on sucess, -ENOMEM on failure.
1079 * Called at driver startup.
1080 */
1081static int amdgpu_atombios_init(struct amdgpu_device *adev)
1082{
1083 struct card_info *atom_card_info =
1084 kzalloc(sizeof(struct card_info), GFP_KERNEL);
Kent Russell5b41d942017-08-22 12:31:43 -04001085 int ret;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001086
1087 if (!atom_card_info)
1088 return -ENOMEM;
1089
1090 adev->mode_info.atom_card_info = atom_card_info;
1091 atom_card_info->dev = adev->ddev;
1092 atom_card_info->reg_read = cail_reg_read;
1093 atom_card_info->reg_write = cail_reg_write;
1094 /* needed for iio ops */
1095 if (adev->rio_mem) {
1096 atom_card_info->ioreg_read = cail_ioreg_read;
1097 atom_card_info->ioreg_write = cail_ioreg_write;
1098 } else {
pding9953b722017-10-26 09:30:38 +08001099 DRM_DEBUG("PCI I/O BAR is not found. Using MMIO to access ATOM BIOS\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001100 atom_card_info->ioreg_read = cail_reg_read;
1101 atom_card_info->ioreg_write = cail_reg_write;
1102 }
1103 atom_card_info->mc_read = cail_mc_read;
1104 atom_card_info->mc_write = cail_mc_write;
1105 atom_card_info->pll_read = cail_pll_read;
1106 atom_card_info->pll_write = cail_pll_write;
1107
1108 adev->mode_info.atom_context = amdgpu_atom_parse(atom_card_info, adev->bios);
1109 if (!adev->mode_info.atom_context) {
1110 amdgpu_atombios_fini(adev);
1111 return -ENOMEM;
1112 }
1113
1114 mutex_init(&adev->mode_info.atom_context->mutex);
Alex Deuchera5bde2f2016-09-23 16:23:41 -04001115 if (adev->is_atom_fw) {
1116 amdgpu_atomfirmware_scratch_regs_init(adev);
1117 amdgpu_atomfirmware_allocate_fb_scratch(adev);
1118 } else {
1119 amdgpu_atombios_scratch_regs_init(adev);
1120 amdgpu_atombios_allocate_fb_scratch(adev);
1121 }
Kent Russell5b41d942017-08-22 12:31:43 -04001122
1123 ret = device_create_file(adev->dev, &dev_attr_vbios_version);
1124 if (ret) {
1125 DRM_ERROR("Failed to create device file for VBIOS version\n");
1126 return ret;
1127 }
1128
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001129 return 0;
1130}
1131
1132/* if we get transitioned to only one device, take VGA back */
1133/**
1134 * amdgpu_vga_set_decode - enable/disable vga decode
1135 *
1136 * @cookie: amdgpu_device pointer
1137 * @state: enable/disable vga decode
1138 *
1139 * Enable/disable vga decode (all asics).
1140 * Returns VGA resource flags.
1141 */
1142static unsigned int amdgpu_vga_set_decode(void *cookie, bool state)
1143{
1144 struct amdgpu_device *adev = cookie;
1145 amdgpu_asic_set_vga_state(adev, state);
1146 if (state)
1147 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
1148 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1149 else
1150 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1151}
1152
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001153static void amdgpu_check_block_size(struct amdgpu_device *adev)
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001154{
1155 /* defines number of bits in page table versus page directory,
1156 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
1157 * page table and the remaining bits are in the page directory */
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001158 if (amdgpu_vm_block_size == -1)
1159 return;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001160
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001161 if (amdgpu_vm_block_size < 9) {
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001162 dev_warn(adev->dev, "VM page table size (%d) too small\n",
1163 amdgpu_vm_block_size);
Christian König97489122017-11-27 16:22:05 +01001164 amdgpu_vm_block_size = -1;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001165 }
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001166}
1167
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001168static void amdgpu_check_vm_size(struct amdgpu_device *adev)
1169{
Alex Deucher64dab072017-06-15 18:20:09 -04001170 /* no need to check the default value */
1171 if (amdgpu_vm_size == -1)
1172 return;
1173
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001174 if (amdgpu_vm_size < 1) {
1175 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
1176 amdgpu_vm_size);
Christian Königf3368122017-11-23 12:57:18 +01001177 amdgpu_vm_size = -1;
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001178 }
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001179}
1180
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001181/**
1182 * amdgpu_check_arguments - validate module params
1183 *
1184 * @adev: amdgpu_device pointer
1185 *
1186 * Validates certain module parameters and updates
1187 * the associated values used by the driver (all asics).
1188 */
1189static void amdgpu_check_arguments(struct amdgpu_device *adev)
1190{
Chunming Zhou5b011232015-12-10 17:34:33 +08001191 if (amdgpu_sched_jobs < 4) {
1192 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
1193 amdgpu_sched_jobs);
1194 amdgpu_sched_jobs = 4;
Alex Deucher76117502017-06-21 12:31:41 -04001195 } else if (!is_power_of_2(amdgpu_sched_jobs)){
Chunming Zhou5b011232015-12-10 17:34:33 +08001196 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
1197 amdgpu_sched_jobs);
1198 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
1199 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001200
Alex Deucher83e74db2017-08-21 11:58:25 -04001201 if (amdgpu_gart_size != -1 && amdgpu_gart_size < 32) {
Christian Königf9321cc2017-07-07 13:44:05 +02001202 /* gart size must be greater or equal to 32M */
1203 dev_warn(adev->dev, "gart size (%d) too small\n",
1204 amdgpu_gart_size);
Alex Deucher83e74db2017-08-21 11:58:25 -04001205 amdgpu_gart_size = -1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001206 }
1207
Christian König36d38372017-07-07 13:17:45 +02001208 if (amdgpu_gtt_size != -1 && amdgpu_gtt_size < 32) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001209 /* gtt size must be greater or equal to 32M */
Christian König36d38372017-07-07 13:17:45 +02001210 dev_warn(adev->dev, "gtt size (%d) too small\n",
1211 amdgpu_gtt_size);
1212 amdgpu_gtt_size = -1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001213 }
1214
Roger Hed07f14b2017-08-15 16:05:59 +08001215 /* valid range is between 4 and 9 inclusive */
1216 if (amdgpu_vm_fragment_size != -1 &&
1217 (amdgpu_vm_fragment_size > 9 || amdgpu_vm_fragment_size < 4)) {
1218 dev_warn(adev->dev, "valid range is between 4 and 9\n");
1219 amdgpu_vm_fragment_size = -1;
1220 }
1221
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001222 amdgpu_check_vm_size(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001223
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001224 amdgpu_check_block_size(adev);
Christian König6a7f76e2016-08-24 15:51:49 +02001225
jimqu526bae32016-11-07 09:53:10 +08001226 if (amdgpu_vram_page_split != -1 && (amdgpu_vram_page_split < 16 ||
Alex Deucher76117502017-06-21 12:31:41 -04001227 !is_power_of_2(amdgpu_vram_page_split))) {
Christian König6a7f76e2016-08-24 15:51:49 +02001228 dev_warn(adev->dev, "invalid VRAM page split (%d)\n",
1229 amdgpu_vram_page_split);
1230 amdgpu_vram_page_split = 1024;
1231 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001232}
1233
1234/**
1235 * amdgpu_switcheroo_set_state - set switcheroo state
1236 *
1237 * @pdev: pci dev pointer
Lukas Wunner16944672015-09-05 11:17:35 +02001238 * @state: vga_switcheroo state
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001239 *
1240 * Callback for the switcheroo driver. Suspends or resumes the
1241 * the asics before or after it is powered up using ACPI methods.
1242 */
1243static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1244{
1245 struct drm_device *dev = pci_get_drvdata(pdev);
1246
1247 if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
1248 return;
1249
1250 if (state == VGA_SWITCHEROO_ON) {
Joe Perches7ca85292017-02-28 04:55:52 -08001251 pr_info("amdgpu: switched on\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001252 /* don't suspend or resume card normally */
1253 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1254
Alex Deucher810ddc32016-08-23 13:25:49 -04001255 amdgpu_device_resume(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001256
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001257 dev->switch_power_state = DRM_SWITCH_POWER_ON;
1258 drm_kms_helper_poll_enable(dev);
1259 } else {
Joe Perches7ca85292017-02-28 04:55:52 -08001260 pr_info("amdgpu: switched off\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001261 drm_kms_helper_poll_disable(dev);
1262 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Alex Deucher810ddc32016-08-23 13:25:49 -04001263 amdgpu_device_suspend(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001264 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1265 }
1266}
1267
1268/**
1269 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1270 *
1271 * @pdev: pci dev pointer
1272 *
1273 * Callback for the switcheroo driver. Check of the switcheroo
1274 * state can be changed.
1275 * Returns true if the state can be changed, false if not.
1276 */
1277static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1278{
1279 struct drm_device *dev = pci_get_drvdata(pdev);
1280
1281 /*
1282 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1283 * locking inversion with the driver load path. And the access here is
1284 * completely racy anyway. So don't bother with locking for now.
1285 */
1286 return dev->open_count == 0;
1287}
1288
1289static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1290 .set_gpu_state = amdgpu_switcheroo_set_state,
1291 .reprobe = NULL,
1292 .can_switch = amdgpu_switcheroo_can_switch,
1293};
1294
1295int amdgpu_set_clockgating_state(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001296 enum amd_ip_block_type block_type,
1297 enum amd_clockgating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001298{
1299 int i, r = 0;
1300
1301 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001302 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001303 continue;
Rex Zhuc7228652017-02-22 15:33:46 +08001304 if (adev->ip_blocks[i].version->type != block_type)
1305 continue;
1306 if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
1307 continue;
1308 r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
1309 (void *)adev, state);
1310 if (r)
1311 DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
1312 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001313 }
1314 return r;
1315}
1316
1317int amdgpu_set_powergating_state(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001318 enum amd_ip_block_type block_type,
1319 enum amd_powergating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001320{
1321 int i, r = 0;
1322
1323 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001324 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001325 continue;
Rex Zhuc7228652017-02-22 15:33:46 +08001326 if (adev->ip_blocks[i].version->type != block_type)
1327 continue;
1328 if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
1329 continue;
1330 r = adev->ip_blocks[i].version->funcs->set_powergating_state(
1331 (void *)adev, state);
1332 if (r)
1333 DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
1334 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001335 }
1336 return r;
1337}
1338
Huang Rui6cb2d4e2017-01-05 18:44:41 +08001339void amdgpu_get_clockgating_state(struct amdgpu_device *adev, u32 *flags)
1340{
1341 int i;
1342
1343 for (i = 0; i < adev->num_ip_blocks; i++) {
1344 if (!adev->ip_blocks[i].status.valid)
1345 continue;
1346 if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
1347 adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
1348 }
1349}
1350
Alex Deucher5dbbb602016-06-23 11:41:04 -04001351int amdgpu_wait_for_idle(struct amdgpu_device *adev,
1352 enum amd_ip_block_type block_type)
1353{
1354 int i, r;
1355
1356 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001357 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001358 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001359 if (adev->ip_blocks[i].version->type == block_type) {
1360 r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -04001361 if (r)
1362 return r;
1363 break;
1364 }
1365 }
1366 return 0;
1367
1368}
1369
1370bool amdgpu_is_idle(struct amdgpu_device *adev,
1371 enum amd_ip_block_type block_type)
1372{
1373 int i;
1374
1375 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001376 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001377 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001378 if (adev->ip_blocks[i].version->type == block_type)
1379 return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -04001380 }
1381 return true;
1382
1383}
1384
Alex Deuchera1255102016-10-13 17:41:13 -04001385struct amdgpu_ip_block * amdgpu_get_ip_block(struct amdgpu_device *adev,
1386 enum amd_ip_block_type type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001387{
1388 int i;
1389
1390 for (i = 0; i < adev->num_ip_blocks; i++)
Alex Deuchera1255102016-10-13 17:41:13 -04001391 if (adev->ip_blocks[i].version->type == type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001392 return &adev->ip_blocks[i];
1393
1394 return NULL;
1395}
1396
1397/**
1398 * amdgpu_ip_block_version_cmp
1399 *
1400 * @adev: amdgpu_device pointer
yanyang15fc3aee2015-05-22 14:39:35 -04001401 * @type: enum amd_ip_block_type
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001402 * @major: major version
1403 * @minor: minor version
1404 *
1405 * return 0 if equal or greater
1406 * return 1 if smaller or the ip_block doesn't exist
1407 */
1408int amdgpu_ip_block_version_cmp(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001409 enum amd_ip_block_type type,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001410 u32 major, u32 minor)
1411{
Alex Deuchera1255102016-10-13 17:41:13 -04001412 struct amdgpu_ip_block *ip_block = amdgpu_get_ip_block(adev, type);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001413
Alex Deuchera1255102016-10-13 17:41:13 -04001414 if (ip_block && ((ip_block->version->major > major) ||
1415 ((ip_block->version->major == major) &&
1416 (ip_block->version->minor >= minor))))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001417 return 0;
1418
1419 return 1;
1420}
1421
Alex Deuchera1255102016-10-13 17:41:13 -04001422/**
1423 * amdgpu_ip_block_add
1424 *
1425 * @adev: amdgpu_device pointer
1426 * @ip_block_version: pointer to the IP to add
1427 *
1428 * Adds the IP block driver information to the collection of IPs
1429 * on the asic.
1430 */
1431int amdgpu_ip_block_add(struct amdgpu_device *adev,
1432 const struct amdgpu_ip_block_version *ip_block_version)
1433{
1434 if (!ip_block_version)
1435 return -EINVAL;
1436
Huang Ruia0bae352017-05-03 09:52:06 +08001437 DRM_DEBUG("add ip block number %d <%s>\n", adev->num_ip_blocks,
1438 ip_block_version->funcs->name);
1439
Alex Deuchera1255102016-10-13 17:41:13 -04001440 adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
1441
1442 return 0;
1443}
1444
Alex Deucher483ef982016-09-30 12:43:04 -04001445static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
Emily Deng9accf2f2016-08-10 16:01:25 +08001446{
1447 adev->enable_virtual_display = false;
1448
1449 if (amdgpu_virtual_display) {
1450 struct drm_device *ddev = adev->ddev;
1451 const char *pci_address_name = pci_name(ddev->pdev);
Emily Deng0f663562016-09-30 13:02:18 -04001452 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
Emily Deng9accf2f2016-08-10 16:01:25 +08001453
1454 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1455 pciaddstr_tmp = pciaddstr;
Emily Deng0f663562016-09-30 13:02:18 -04001456 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1457 pciaddname = strsep(&pciaddname_tmp, ",");
Yintian Tao967de2a2017-01-22 15:16:51 +08001458 if (!strcmp("all", pciaddname)
1459 || !strcmp(pci_address_name, pciaddname)) {
Emily Deng0f663562016-09-30 13:02:18 -04001460 long num_crtc;
1461 int res = -1;
1462
Emily Deng9accf2f2016-08-10 16:01:25 +08001463 adev->enable_virtual_display = true;
Emily Deng0f663562016-09-30 13:02:18 -04001464
1465 if (pciaddname_tmp)
1466 res = kstrtol(pciaddname_tmp, 10,
1467 &num_crtc);
1468
1469 if (!res) {
1470 if (num_crtc < 1)
1471 num_crtc = 1;
1472 if (num_crtc > 6)
1473 num_crtc = 6;
1474 adev->mode_info.num_crtc = num_crtc;
1475 } else {
1476 adev->mode_info.num_crtc = 1;
1477 }
Emily Deng9accf2f2016-08-10 16:01:25 +08001478 break;
1479 }
1480 }
1481
Emily Deng0f663562016-09-30 13:02:18 -04001482 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
1483 amdgpu_virtual_display, pci_address_name,
1484 adev->enable_virtual_display, adev->mode_info.num_crtc);
Emily Deng9accf2f2016-08-10 16:01:25 +08001485
1486 kfree(pciaddstr);
1487 }
1488}
1489
Alex Deuchere2a75f82017-04-27 16:58:01 -04001490static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev)
1491{
Alex Deuchere2a75f82017-04-27 16:58:01 -04001492 const char *chip_name;
1493 char fw_name[30];
1494 int err;
1495 const struct gpu_info_firmware_header_v1_0 *hdr;
1496
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001497 adev->firmware.gpu_info_fw = NULL;
1498
Alex Deuchere2a75f82017-04-27 16:58:01 -04001499 switch (adev->asic_type) {
1500 case CHIP_TOPAZ:
1501 case CHIP_TONGA:
1502 case CHIP_FIJI:
1503 case CHIP_POLARIS11:
1504 case CHIP_POLARIS10:
1505 case CHIP_POLARIS12:
1506 case CHIP_CARRIZO:
1507 case CHIP_STONEY:
1508#ifdef CONFIG_DRM_AMDGPU_SI
1509 case CHIP_VERDE:
1510 case CHIP_TAHITI:
1511 case CHIP_PITCAIRN:
1512 case CHIP_OLAND:
1513 case CHIP_HAINAN:
1514#endif
1515#ifdef CONFIG_DRM_AMDGPU_CIK
1516 case CHIP_BONAIRE:
1517 case CHIP_HAWAII:
1518 case CHIP_KAVERI:
1519 case CHIP_KABINI:
1520 case CHIP_MULLINS:
1521#endif
1522 default:
1523 return 0;
1524 case CHIP_VEGA10:
1525 chip_name = "vega10";
1526 break;
Alex Deucher2d2e5e72017-05-09 12:27:35 -04001527 case CHIP_RAVEN:
1528 chip_name = "raven";
1529 break;
Alex Deuchere2a75f82017-04-27 16:58:01 -04001530 }
1531
1532 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name);
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001533 err = request_firmware(&adev->firmware.gpu_info_fw, fw_name, adev->dev);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001534 if (err) {
1535 dev_err(adev->dev,
1536 "Failed to load gpu_info firmware \"%s\"\n",
1537 fw_name);
1538 goto out;
1539 }
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001540 err = amdgpu_ucode_validate(adev->firmware.gpu_info_fw);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001541 if (err) {
1542 dev_err(adev->dev,
1543 "Failed to validate gpu_info firmware \"%s\"\n",
1544 fw_name);
1545 goto out;
1546 }
1547
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001548 hdr = (const struct gpu_info_firmware_header_v1_0 *)adev->firmware.gpu_info_fw->data;
Alex Deuchere2a75f82017-04-27 16:58:01 -04001549 amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
1550
1551 switch (hdr->version_major) {
1552 case 1:
1553 {
1554 const struct gpu_info_firmware_v1_0 *gpu_info_fw =
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001555 (const struct gpu_info_firmware_v1_0 *)(adev->firmware.gpu_info_fw->data +
Alex Deuchere2a75f82017-04-27 16:58:01 -04001556 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1557
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001558 adev->gfx.config.max_shader_engines = le32_to_cpu(gpu_info_fw->gc_num_se);
1559 adev->gfx.config.max_cu_per_sh = le32_to_cpu(gpu_info_fw->gc_num_cu_per_sh);
1560 adev->gfx.config.max_sh_per_se = le32_to_cpu(gpu_info_fw->gc_num_sh_per_se);
1561 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 -04001562 adev->gfx.config.max_texture_channel_caches =
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001563 le32_to_cpu(gpu_info_fw->gc_num_tccs);
1564 adev->gfx.config.max_gprs = le32_to_cpu(gpu_info_fw->gc_num_gprs);
1565 adev->gfx.config.max_gs_threads = le32_to_cpu(gpu_info_fw->gc_num_max_gs_thds);
1566 adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gpu_info_fw->gc_gs_table_depth);
1567 adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gpu_info_fw->gc_gsprim_buff_depth);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001568 adev->gfx.config.double_offchip_lds_buf =
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001569 le32_to_cpu(gpu_info_fw->gc_double_offchip_lds_buffer);
1570 adev->gfx.cu_info.wave_front_size = le32_to_cpu(gpu_info_fw->gc_wave_size);
Hawking Zhang51fd0372017-06-09 22:30:52 +08001571 adev->gfx.cu_info.max_waves_per_simd =
1572 le32_to_cpu(gpu_info_fw->gc_max_waves_per_simd);
1573 adev->gfx.cu_info.max_scratch_slots_per_cu =
1574 le32_to_cpu(gpu_info_fw->gc_max_scratch_slots_per_cu);
1575 adev->gfx.cu_info.lds_size = le32_to_cpu(gpu_info_fw->gc_lds_size);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001576 break;
1577 }
1578 default:
1579 dev_err(adev->dev,
1580 "Unsupported gpu_info table %d\n", hdr->header.ucode_version);
1581 err = -EINVAL;
1582 goto out;
1583 }
1584out:
Alex Deuchere2a75f82017-04-27 16:58:01 -04001585 return err;
1586}
1587
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001588static int amdgpu_early_init(struct amdgpu_device *adev)
1589{
Alex Deucheraaa36a92015-04-20 17:31:14 -04001590 int i, r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001591
Alex Deucher483ef982016-09-30 12:43:04 -04001592 amdgpu_device_enable_virtual_display(adev);
Emily Denga6be7572016-08-08 11:37:50 +08001593
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001594 switch (adev->asic_type) {
Alex Deucheraaa36a92015-04-20 17:31:14 -04001595 case CHIP_TOPAZ:
1596 case CHIP_TONGA:
David Zhang48299f92015-07-08 01:05:16 +08001597 case CHIP_FIJI:
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001598 case CHIP_POLARIS11:
1599 case CHIP_POLARIS10:
Junwei Zhangc4642a42016-12-14 15:32:28 -05001600 case CHIP_POLARIS12:
Alex Deucheraaa36a92015-04-20 17:31:14 -04001601 case CHIP_CARRIZO:
Samuel Li39bb0c92015-10-08 16:31:43 -04001602 case CHIP_STONEY:
1603 if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
Alex Deucheraaa36a92015-04-20 17:31:14 -04001604 adev->family = AMDGPU_FAMILY_CZ;
1605 else
1606 adev->family = AMDGPU_FAMILY_VI;
1607
1608 r = vi_set_ip_blocks(adev);
1609 if (r)
1610 return r;
1611 break;
Ken Wang33f34802016-01-21 17:29:41 +08001612#ifdef CONFIG_DRM_AMDGPU_SI
1613 case CHIP_VERDE:
1614 case CHIP_TAHITI:
1615 case CHIP_PITCAIRN:
1616 case CHIP_OLAND:
1617 case CHIP_HAINAN:
Ken Wang295d0da2016-05-24 21:02:53 +08001618 adev->family = AMDGPU_FAMILY_SI;
Ken Wang33f34802016-01-21 17:29:41 +08001619 r = si_set_ip_blocks(adev);
1620 if (r)
1621 return r;
1622 break;
1623#endif
Alex Deuchera2e73f52015-04-20 17:09:27 -04001624#ifdef CONFIG_DRM_AMDGPU_CIK
1625 case CHIP_BONAIRE:
1626 case CHIP_HAWAII:
1627 case CHIP_KAVERI:
1628 case CHIP_KABINI:
1629 case CHIP_MULLINS:
1630 if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
1631 adev->family = AMDGPU_FAMILY_CI;
1632 else
1633 adev->family = AMDGPU_FAMILY_KV;
1634
1635 r = cik_set_ip_blocks(adev);
1636 if (r)
1637 return r;
1638 break;
1639#endif
Chunming Zhou2ca8a5d2016-12-07 17:31:19 +08001640 case CHIP_VEGA10:
1641 case CHIP_RAVEN:
1642 if (adev->asic_type == CHIP_RAVEN)
1643 adev->family = AMDGPU_FAMILY_RV;
1644 else
1645 adev->family = AMDGPU_FAMILY_AI;
Ken Wang460826e2017-03-06 14:53:16 -05001646
1647 r = soc15_set_ip_blocks(adev);
1648 if (r)
1649 return r;
1650 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001651 default:
1652 /* FIXME: not supported yet */
1653 return -EINVAL;
1654 }
1655
Alex Deuchere2a75f82017-04-27 16:58:01 -04001656 r = amdgpu_device_parse_gpu_info_fw(adev);
1657 if (r)
1658 return r;
1659
pding18847342017-11-06 10:21:26 +08001660 amdgpu_amdkfd_device_probe(adev);
1661
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001662 if (amdgpu_sriov_vf(adev)) {
1663 r = amdgpu_virt_request_full_gpu(adev, true);
1664 if (r)
pding5ffa61c2017-10-30 14:07:24 +08001665 return -EAGAIN;
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001666 }
1667
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001668 for (i = 0; i < adev->num_ip_blocks; i++) {
1669 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
Huang Ruied8cf002017-05-03 09:40:17 +08001670 DRM_ERROR("disabled ip block: %d <%s>\n",
1671 i, adev->ip_blocks[i].version->funcs->name);
Alex Deuchera1255102016-10-13 17:41:13 -04001672 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001673 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001674 if (adev->ip_blocks[i].version->funcs->early_init) {
1675 r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001676 if (r == -ENOENT) {
Alex Deuchera1255102016-10-13 17:41:13 -04001677 adev->ip_blocks[i].status.valid = false;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001678 } else if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001679 DRM_ERROR("early_init of IP block <%s> failed %d\n",
1680 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001681 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001682 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001683 adev->ip_blocks[i].status.valid = true;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001684 }
Alex Deucher974e6b62015-07-10 13:59:44 -04001685 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001686 adev->ip_blocks[i].status.valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001687 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001688 }
1689 }
1690
Nicolai Hähnle395d1fb2016-06-02 12:32:07 +02001691 adev->cg_flags &= amdgpu_cg_mask;
1692 adev->pg_flags &= amdgpu_pg_mask;
1693
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001694 return 0;
1695}
1696
1697static int amdgpu_init(struct amdgpu_device *adev)
1698{
1699 int i, r;
1700
1701 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001702 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001703 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001704 r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001705 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001706 DRM_ERROR("sw_init of IP block <%s> failed %d\n",
1707 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001708 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001709 }
Alex Deuchera1255102016-10-13 17:41:13 -04001710 adev->ip_blocks[i].status.sw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001711 /* need to do gmc hw init early so we can allocate gpu mem */
Alex Deuchera1255102016-10-13 17:41:13 -04001712 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001713 r = amdgpu_vram_scratch_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001714 if (r) {
1715 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001716 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001717 }
Alex Deuchera1255102016-10-13 17:41:13 -04001718 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001719 if (r) {
1720 DRM_ERROR("hw_init %d failed %d\n", i, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001721 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001722 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001723 r = amdgpu_wb_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001724 if (r) {
1725 DRM_ERROR("amdgpu_wb_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001726 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001727 }
Alex Deuchera1255102016-10-13 17:41:13 -04001728 adev->ip_blocks[i].status.hw = true;
Monk Liu24936642017-01-09 15:54:32 +08001729
1730 /* right after GMC hw init, we create CSA */
1731 if (amdgpu_sriov_vf(adev)) {
1732 r = amdgpu_allocate_static_csa(adev);
1733 if (r) {
1734 DRM_ERROR("allocate CSA failed %d\n", r);
1735 return r;
1736 }
1737 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001738 }
1739 }
1740
1741 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001742 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001743 continue;
1744 /* gmc hw init is done early */
Alex Deuchera1255102016-10-13 17:41:13 -04001745 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001746 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001747 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001748 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001749 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
1750 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001751 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001752 }
Alex Deuchera1255102016-10-13 17:41:13 -04001753 adev->ip_blocks[i].status.hw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001754 }
1755
pding18847342017-11-06 10:21:26 +08001756 amdgpu_amdkfd_device_init(adev);
pdingc6332b92017-11-06 11:21:55 +08001757
1758 if (amdgpu_sriov_vf(adev))
1759 amdgpu_virt_release_full_gpu(adev, true);
1760
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001761 return 0;
1762}
1763
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08001764static void amdgpu_fill_reset_magic(struct amdgpu_device *adev)
1765{
1766 memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM);
1767}
1768
1769static bool amdgpu_check_vram_lost(struct amdgpu_device *adev)
1770{
1771 return !!memcmp(adev->gart.ptr, adev->reset_magic,
1772 AMDGPU_RESET_MAGIC_NUM);
1773}
1774
Shirish S2dc80b02017-05-25 10:05:25 +05301775static int amdgpu_late_set_cg_state(struct amdgpu_device *adev)
1776{
1777 int i = 0, r;
1778
1779 for (i = 0; i < adev->num_ip_blocks; i++) {
1780 if (!adev->ip_blocks[i].status.valid)
1781 continue;
1782 /* skip CG for VCE/UVD, it's handled specially */
1783 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1784 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1785 /* enable clockgating to save power */
1786 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1787 AMD_CG_STATE_GATE);
1788 if (r) {
1789 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
1790 adev->ip_blocks[i].version->funcs->name, r);
1791 return r;
1792 }
1793 }
1794 }
1795 return 0;
1796}
1797
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001798static int amdgpu_late_init(struct amdgpu_device *adev)
1799{
1800 int i = 0, r;
1801
1802 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001803 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001804 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001805 if (adev->ip_blocks[i].version->funcs->late_init) {
1806 r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001807 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001808 DRM_ERROR("late_init of IP block <%s> failed %d\n",
1809 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001810 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001811 }
Alex Deuchera1255102016-10-13 17:41:13 -04001812 adev->ip_blocks[i].status.late_initialized = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001813 }
1814 }
1815
Shirish S2dc80b02017-05-25 10:05:25 +05301816 mod_delayed_work(system_wq, &adev->late_init_work,
1817 msecs_to_jiffies(AMDGPU_RESUME_MS));
1818
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08001819 amdgpu_fill_reset_magic(adev);
1820
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001821 return 0;
1822}
1823
1824static int amdgpu_fini(struct amdgpu_device *adev)
1825{
1826 int i, r;
1827
pding18847342017-11-06 10:21:26 +08001828 amdgpu_amdkfd_device_fini(adev);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001829 /* need to disable SMC first */
1830 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001831 if (!adev->ip_blocks[i].status.hw)
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001832 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001833 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001834 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
Alex Deuchera1255102016-10-13 17:41:13 -04001835 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1836 AMD_CG_STATE_UNGATE);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001837 if (r) {
1838 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001839 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001840 return r;
1841 }
Alex Deuchera1255102016-10-13 17:41:13 -04001842 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001843 /* XXX handle errors */
1844 if (r) {
1845 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001846 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001847 }
Alex Deuchera1255102016-10-13 17:41:13 -04001848 adev->ip_blocks[i].status.hw = false;
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001849 break;
1850 }
1851 }
1852
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001853 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001854 if (!adev->ip_blocks[i].status.hw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001855 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001856 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
Monk Liu84e5b512017-11-14 16:52:14 +08001857 amdgpu_free_static_csa(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001858 amdgpu_wb_fini(adev);
1859 amdgpu_vram_scratch_fini(adev);
1860 }
Rex Zhu8201a672016-11-24 21:44:44 +08001861
1862 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1863 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1864 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
1865 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1866 AMD_CG_STATE_UNGATE);
1867 if (r) {
1868 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1869 adev->ip_blocks[i].version->funcs->name, r);
1870 return r;
1871 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001872 }
Rex Zhu8201a672016-11-24 21:44:44 +08001873
Alex Deuchera1255102016-10-13 17:41:13 -04001874 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001875 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001876 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001877 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
1878 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001879 }
Rex Zhu8201a672016-11-24 21:44:44 +08001880
Alex Deuchera1255102016-10-13 17:41:13 -04001881 adev->ip_blocks[i].status.hw = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001882 }
1883
1884 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001885 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001886 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001887 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001888 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001889 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001890 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
1891 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001892 }
Alex Deuchera1255102016-10-13 17:41:13 -04001893 adev->ip_blocks[i].status.sw = false;
1894 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001895 }
1896
Monk Liua6dcfd92016-05-19 14:36:34 +08001897 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001898 if (!adev->ip_blocks[i].status.late_initialized)
Grazvydas Ignotas8a2eef12016-10-03 00:06:44 +03001899 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001900 if (adev->ip_blocks[i].version->funcs->late_fini)
1901 adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
1902 adev->ip_blocks[i].status.late_initialized = false;
Monk Liua6dcfd92016-05-19 14:36:34 +08001903 }
1904
Monk Liu030308f2017-09-15 15:34:52 +08001905 if (amdgpu_sriov_vf(adev))
Monk Liu24136132017-11-14 16:56:55 +08001906 if (amdgpu_virt_release_full_gpu(adev, false))
1907 DRM_ERROR("failed to release exclusive mode on fini\n");
Monk Liu24936642017-01-09 15:54:32 +08001908
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001909 return 0;
1910}
1911
Shirish S2dc80b02017-05-25 10:05:25 +05301912static void amdgpu_late_init_func_handler(struct work_struct *work)
1913{
1914 struct amdgpu_device *adev =
1915 container_of(work, struct amdgpu_device, late_init_work.work);
1916 amdgpu_late_set_cg_state(adev);
1917}
1918
Alex Deucherfaefba92016-12-06 10:38:29 -05001919int amdgpu_suspend(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001920{
1921 int i, r;
1922
Xiangliang Yue941ea92017-01-18 12:47:55 +08001923 if (amdgpu_sriov_vf(adev))
1924 amdgpu_virt_request_full_gpu(adev, false);
1925
Flora Cuic5a93a22016-02-26 10:45:25 +08001926 /* ungate SMC block first */
1927 r = amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC,
1928 AMD_CG_STATE_UNGATE);
1929 if (r) {
1930 DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n",r);
1931 }
1932
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001933 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001934 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001935 continue;
1936 /* ungate blocks so that suspend can properly shut them down */
Flora Cuic5a93a22016-02-26 10:45:25 +08001937 if (i != AMD_IP_BLOCK_TYPE_SMC) {
Alex Deuchera1255102016-10-13 17:41:13 -04001938 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1939 AMD_CG_STATE_UNGATE);
Flora Cuic5a93a22016-02-26 10:45:25 +08001940 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001941 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1942 adev->ip_blocks[i].version->funcs->name, r);
Flora Cuic5a93a22016-02-26 10:45:25 +08001943 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001944 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001945 /* XXX handle errors */
Alex Deuchera1255102016-10-13 17:41:13 -04001946 r = adev->ip_blocks[i].version->funcs->suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001947 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001948 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001949 DRM_ERROR("suspend of IP block <%s> failed %d\n",
1950 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001951 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001952 }
1953
Xiangliang Yue941ea92017-01-18 12:47:55 +08001954 if (amdgpu_sriov_vf(adev))
1955 amdgpu_virt_release_full_gpu(adev, false);
1956
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001957 return 0;
1958}
1959
Monk Liue4f0fdc2017-02-09 11:55:49 +08001960static int amdgpu_sriov_reinit_early(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08001961{
1962 int i, r;
1963
Monk Liu2cb681b2017-04-26 12:00:49 +08001964 static enum amd_ip_block_type ip_order[] = {
1965 AMD_IP_BLOCK_TYPE_GMC,
1966 AMD_IP_BLOCK_TYPE_COMMON,
Monk Liu2cb681b2017-04-26 12:00:49 +08001967 AMD_IP_BLOCK_TYPE_IH,
1968 };
Monk Liua90ad3c2017-01-23 14:22:08 +08001969
Monk Liu2cb681b2017-04-26 12:00:49 +08001970 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1971 int j;
1972 struct amdgpu_ip_block *block;
Monk Liua90ad3c2017-01-23 14:22:08 +08001973
Monk Liu2cb681b2017-04-26 12:00:49 +08001974 for (j = 0; j < adev->num_ip_blocks; j++) {
1975 block = &adev->ip_blocks[j];
1976
1977 if (block->version->type != ip_order[i] ||
1978 !block->status.valid)
1979 continue;
1980
1981 r = block->version->funcs->hw_init(adev);
1982 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
Monk Liua90ad3c2017-01-23 14:22:08 +08001983 }
1984 }
1985
1986 return 0;
1987}
1988
Monk Liue4f0fdc2017-02-09 11:55:49 +08001989static int amdgpu_sriov_reinit_late(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08001990{
1991 int i, r;
1992
Monk Liu2cb681b2017-04-26 12:00:49 +08001993 static enum amd_ip_block_type ip_order[] = {
1994 AMD_IP_BLOCK_TYPE_SMC,
Monk Liuef4c1662017-09-22 16:23:34 +08001995 AMD_IP_BLOCK_TYPE_PSP,
Monk Liu2cb681b2017-04-26 12:00:49 +08001996 AMD_IP_BLOCK_TYPE_DCE,
1997 AMD_IP_BLOCK_TYPE_GFX,
1998 AMD_IP_BLOCK_TYPE_SDMA,
Frank Min257deb82017-06-15 20:07:36 +08001999 AMD_IP_BLOCK_TYPE_UVD,
2000 AMD_IP_BLOCK_TYPE_VCE
Monk Liu2cb681b2017-04-26 12:00:49 +08002001 };
Monk Liua90ad3c2017-01-23 14:22:08 +08002002
Monk Liu2cb681b2017-04-26 12:00:49 +08002003 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
2004 int j;
2005 struct amdgpu_ip_block *block;
Monk Liua90ad3c2017-01-23 14:22:08 +08002006
Monk Liu2cb681b2017-04-26 12:00:49 +08002007 for (j = 0; j < adev->num_ip_blocks; j++) {
2008 block = &adev->ip_blocks[j];
2009
2010 if (block->version->type != ip_order[i] ||
2011 !block->status.valid)
2012 continue;
2013
2014 r = block->version->funcs->hw_init(adev);
2015 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
Monk Liua90ad3c2017-01-23 14:22:08 +08002016 }
2017 }
2018
2019 return 0;
2020}
2021
Chunming Zhoufcf06492017-05-05 10:33:33 +08002022static int amdgpu_resume_phase1(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002023{
2024 int i, r;
2025
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002026 for (i = 0; i < adev->num_ip_blocks; i++) {
2027 if (!adev->ip_blocks[i].status.valid)
2028 continue;
Chunming Zhoufcf06492017-05-05 10:33:33 +08002029 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
2030 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
2031 adev->ip_blocks[i].version->type ==
2032 AMD_IP_BLOCK_TYPE_IH) {
2033 r = adev->ip_blocks[i].version->funcs->resume(adev);
2034 if (r) {
2035 DRM_ERROR("resume of IP block <%s> failed %d\n",
2036 adev->ip_blocks[i].version->funcs->name, r);
2037 return r;
2038 }
2039 }
2040 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002041
Chunming Zhoufcf06492017-05-05 10:33:33 +08002042 return 0;
2043}
2044
2045static int amdgpu_resume_phase2(struct amdgpu_device *adev)
2046{
2047 int i, r;
2048
2049 for (i = 0; i < adev->num_ip_blocks; i++) {
2050 if (!adev->ip_blocks[i].status.valid)
2051 continue;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002052 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
2053 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
2054 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH )
2055 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002056 r = adev->ip_blocks[i].version->funcs->resume(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002057 if (r) {
2058 DRM_ERROR("resume of IP block <%s> failed %d\n",
2059 adev->ip_blocks[i].version->funcs->name, r);
2060 return r;
2061 }
2062 }
2063
2064 return 0;
2065}
2066
2067static int amdgpu_resume(struct amdgpu_device *adev)
2068{
Chunming Zhoufcf06492017-05-05 10:33:33 +08002069 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002070
Chunming Zhoufcf06492017-05-05 10:33:33 +08002071 r = amdgpu_resume_phase1(adev);
2072 if (r)
2073 return r;
2074 r = amdgpu_resume_phase2(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002075
Chunming Zhoufcf06492017-05-05 10:33:33 +08002076 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002077}
2078
Monk Liu4e99a442016-03-31 13:26:59 +08002079static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
Andres Rodriguez048765a2016-06-11 02:51:32 -04002080{
Monk Liu6867e1b2017-10-16 19:50:44 +08002081 if (amdgpu_sriov_vf(adev)) {
2082 if (adev->is_atom_fw) {
2083 if (amdgpu_atomfirmware_gpu_supports_virtualization(adev))
2084 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
2085 } else {
2086 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
2087 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
2088 }
2089
2090 if (!(adev->virt.caps & AMDGPU_SRIOV_CAPS_SRIOV_VBIOS))
2091 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_NO_VBIOS, 0, 0);
Alex Deuchera5bde2f2016-09-23 16:23:41 -04002092 }
Andres Rodriguez048765a2016-06-11 02:51:32 -04002093}
2094
Harry Wentland45622362017-09-12 15:58:20 -04002095bool amdgpu_device_asic_has_dc_support(enum amd_asic_type asic_type)
2096{
2097 switch (asic_type) {
2098#if defined(CONFIG_DRM_AMD_DC)
2099 case CHIP_BONAIRE:
2100 case CHIP_HAWAII:
Alex Deucher0d6fbcc2017-08-10 14:39:48 -04002101 case CHIP_KAVERI:
Harry Wentland45622362017-09-12 15:58:20 -04002102 case CHIP_CARRIZO:
2103 case CHIP_STONEY:
2104 case CHIP_POLARIS11:
2105 case CHIP_POLARIS10:
Alex Deucher2c8ad2d2017-06-15 16:20:24 -04002106 case CHIP_POLARIS12:
Harry Wentland45622362017-09-12 15:58:20 -04002107 case CHIP_TONGA:
2108 case CHIP_FIJI:
2109#if defined(CONFIG_DRM_AMD_DC_PRE_VEGA)
2110 return amdgpu_dc != 0;
Harry Wentland45622362017-09-12 15:58:20 -04002111#endif
Alex Deucher17b7cf82017-08-23 09:42:22 -04002112 case CHIP_KABINI:
2113 case CHIP_MULLINS:
2114 return amdgpu_dc > 0;
Harry Wentland42f8ffa2017-09-15 14:07:30 -04002115 case CHIP_VEGA10:
2116#if defined(CONFIG_DRM_AMD_DC_DCN1_0)
Hawking Zhangfd187852017-03-06 14:01:11 +08002117 case CHIP_RAVEN:
Harry Wentland42f8ffa2017-09-15 14:07:30 -04002118#endif
Hawking Zhangfd187852017-03-06 14:01:11 +08002119 return amdgpu_dc != 0;
2120#endif
Harry Wentland45622362017-09-12 15:58:20 -04002121 default:
2122 return false;
2123 }
2124}
2125
2126/**
2127 * amdgpu_device_has_dc_support - check if dc is supported
2128 *
2129 * @adev: amdgpu_device_pointer
2130 *
2131 * Returns true for supported, false for not supported
2132 */
2133bool amdgpu_device_has_dc_support(struct amdgpu_device *adev)
2134{
Xiangliang Yu2555039d2017-01-10 17:34:52 +08002135 if (amdgpu_sriov_vf(adev))
2136 return false;
2137
Harry Wentland45622362017-09-12 15:58:20 -04002138 return amdgpu_device_asic_has_dc_support(adev->asic_type);
2139}
2140
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002141/**
2142 * amdgpu_device_init - initialize the driver
2143 *
2144 * @adev: amdgpu_device pointer
2145 * @pdev: drm dev pointer
2146 * @pdev: pci dev pointer
2147 * @flags: driver flags
2148 *
2149 * Initializes the driver info and hw (all asics).
2150 * Returns 0 for success or an error on failure.
2151 * Called at driver startup.
2152 */
2153int amdgpu_device_init(struct amdgpu_device *adev,
2154 struct drm_device *ddev,
2155 struct pci_dev *pdev,
2156 uint32_t flags)
2157{
2158 int r, i;
2159 bool runtime = false;
Marek Olšák95844d22016-08-17 23:49:27 +02002160 u32 max_MBps;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002161
2162 adev->shutdown = false;
2163 adev->dev = &pdev->dev;
2164 adev->ddev = ddev;
2165 adev->pdev = pdev;
2166 adev->flags = flags;
Jammy Zhou2f7d10b2015-07-22 11:29:01 +08002167 adev->asic_type = flags & AMD_ASIC_MASK;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002168 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
Christian König6f02a692017-07-07 11:56:59 +02002169 adev->mc.gart_size = 512 * 1024 * 1024;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002170 adev->accel_working = false;
2171 adev->num_rings = 0;
2172 adev->mman.buffer_funcs = NULL;
2173 adev->mman.buffer_funcs_ring = NULL;
2174 adev->vm_manager.vm_pte_funcs = NULL;
Christian König2d55e452016-02-08 17:37:38 +01002175 adev->vm_manager.vm_pte_num_rings = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002176 adev->gart.gart_funcs = NULL;
Chris Wilsonf54d1862016-10-25 13:00:45 +01002177 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
Andres Rodriguezb8866c22017-04-28 20:05:51 -04002178 bitmap_zero(adev->gfx.pipe_reserve_bitmap, AMDGPU_MAX_COMPUTE_QUEUES);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002179
2180 adev->smc_rreg = &amdgpu_invalid_rreg;
2181 adev->smc_wreg = &amdgpu_invalid_wreg;
2182 adev->pcie_rreg = &amdgpu_invalid_rreg;
2183 adev->pcie_wreg = &amdgpu_invalid_wreg;
Huang Rui36b9a952016-08-31 13:23:25 +08002184 adev->pciep_rreg = &amdgpu_invalid_rreg;
2185 adev->pciep_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002186 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
2187 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
2188 adev->didt_rreg = &amdgpu_invalid_rreg;
2189 adev->didt_wreg = &amdgpu_invalid_wreg;
Rex Zhuccdbb202016-06-08 12:47:41 +08002190 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
2191 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002192 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
2193 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
2194
Alex Deucher3e39ab92015-06-05 15:04:33 -04002195 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
2196 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
2197 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002198
2199 /* mutex initialization are all done here so we
2200 * can recall function without having locking issues */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002201 atomic_set(&adev->irq.ih.lock, 0);
Huang Rui0e5ca0d2017-03-03 18:37:23 -05002202 mutex_init(&adev->firmware.mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002203 mutex_init(&adev->pm.mutex);
2204 mutex_init(&adev->gfx.gpu_clock_mutex);
2205 mutex_init(&adev->srbm_mutex);
Andres Rodriguezb8866c22017-04-28 20:05:51 -04002206 mutex_init(&adev->gfx.pipe_reserve_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002207 mutex_init(&adev->grbm_idx_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002208 mutex_init(&adev->mn_lock);
Alex Deuchere23b74a2017-09-28 09:47:32 -04002209 mutex_init(&adev->virt.vf_errors.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002210 hash_init(adev->mn_hash);
Monk Liu13a752e2017-10-17 15:11:12 +08002211 mutex_init(&adev->lock_reset);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002212
2213 amdgpu_check_arguments(adev);
2214
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002215 spin_lock_init(&adev->mmio_idx_lock);
2216 spin_lock_init(&adev->smc_idx_lock);
2217 spin_lock_init(&adev->pcie_idx_lock);
2218 spin_lock_init(&adev->uvd_ctx_idx_lock);
2219 spin_lock_init(&adev->didt_idx_lock);
Rex Zhuccdbb202016-06-08 12:47:41 +08002220 spin_lock_init(&adev->gc_cac_idx_lock);
Evan Quan16abb5d2017-07-04 09:21:50 +08002221 spin_lock_init(&adev->se_cac_idx_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002222 spin_lock_init(&adev->audio_endpt_idx_lock);
Marek Olšák95844d22016-08-17 23:49:27 +02002223 spin_lock_init(&adev->mm_stats.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002224
Chunming Zhou0c4e7fa2016-08-17 11:41:30 +08002225 INIT_LIST_HEAD(&adev->shadow_list);
2226 mutex_init(&adev->shadow_list_lock);
2227
Andres Rodriguez795f2812017-03-06 16:27:55 -05002228 INIT_LIST_HEAD(&adev->ring_lru_list);
2229 spin_lock_init(&adev->ring_lru_list_lock);
2230
Shirish S2dc80b02017-05-25 10:05:25 +05302231 INIT_DELAYED_WORK(&adev->late_init_work, amdgpu_late_init_func_handler);
2232
Alex Xie0fa49552017-06-08 14:58:05 -04002233 /* Registers mapping */
2234 /* TODO: block userspace mapping of io register */
Ken Wangda69c1612016-01-21 19:08:55 +08002235 if (adev->asic_type >= CHIP_BONAIRE) {
2236 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
2237 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
2238 } else {
2239 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
2240 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
2241 }
Chunming Zhou5c1354b2016-08-30 16:13:10 +08002242
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002243 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
2244 if (adev->rmmio == NULL) {
2245 return -ENOMEM;
2246 }
2247 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
2248 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
2249
Christian König705e5192017-06-08 11:15:16 +02002250 /* doorbell bar mapping */
2251 amdgpu_doorbell_init(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002252
2253 /* io port mapping */
2254 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
2255 if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
2256 adev->rio_mem_size = pci_resource_len(adev->pdev, i);
2257 adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
2258 break;
2259 }
2260 }
2261 if (adev->rio_mem == NULL)
Amber Linb64a18c2017-01-04 08:06:58 -05002262 DRM_INFO("PCI I/O BAR is not found.\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002263
2264 /* early init functions */
2265 r = amdgpu_early_init(adev);
2266 if (r)
2267 return r;
2268
2269 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
2270 /* this will fail for cards that aren't VGA class devices, just
2271 * ignore it */
2272 vga_client_register(adev->pdev, adev, NULL, amdgpu_vga_set_decode);
2273
2274 if (amdgpu_runtime_pm == 1)
2275 runtime = true;
Alex Deuchere9bef452016-04-25 13:12:18 -04002276 if (amdgpu_device_is_px(ddev))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002277 runtime = true;
Lukas Wunner84c8b222017-03-10 21:23:45 +01002278 if (!pci_is_thunderbolt_attached(adev->pdev))
2279 vga_switcheroo_register_client(adev->pdev,
2280 &amdgpu_switcheroo_ops, runtime);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002281 if (runtime)
2282 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
2283
2284 /* Read BIOS */
Alex Deucher83ba1262016-06-03 18:21:41 -04002285 if (!amdgpu_get_bios(adev)) {
2286 r = -EINVAL;
2287 goto failed;
2288 }
Nils Wallméniusf7e9e9f2016-12-14 21:52:45 +01002289
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002290 r = amdgpu_atombios_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002291 if (r) {
2292 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002293 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_INIT_FAIL, 0, 0);
Alex Deucher83ba1262016-06-03 18:21:41 -04002294 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002295 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002296
Monk Liu4e99a442016-03-31 13:26:59 +08002297 /* detect if we are with an SRIOV vbios */
2298 amdgpu_device_detect_sriov_bios(adev);
Andres Rodriguez048765a2016-06-11 02:51:32 -04002299
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002300 /* Post card if necessary */
pding91fe77e2017-10-19 09:38:39 +08002301 if (amdgpu_need_post(adev)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002302 if (!adev->bios) {
Monk Liubec86372016-09-14 19:38:08 +08002303 dev_err(adev->dev, "no vBIOS found\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04002304 r = -EINVAL;
2305 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002306 }
Monk Liubec86372016-09-14 19:38:08 +08002307 DRM_INFO("GPU posting now...\n");
Monk Liu4e99a442016-03-31 13:26:59 +08002308 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2309 if (r) {
2310 dev_err(adev->dev, "gpu post error!\n");
2311 goto failed;
2312 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002313 }
2314
Alex Deucher88b64e92017-07-10 10:43:10 -04002315 if (adev->is_atom_fw) {
2316 /* Initialize clocks */
2317 r = amdgpu_atomfirmware_get_clock_info(adev);
2318 if (r) {
2319 dev_err(adev->dev, "amdgpu_atomfirmware_get_clock_info failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002320 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
Alex Deucher88b64e92017-07-10 10:43:10 -04002321 goto failed;
2322 }
2323 } else {
Alex Deuchera5bde2f2016-09-23 16:23:41 -04002324 /* Initialize clocks */
2325 r = amdgpu_atombios_get_clock_info(adev);
2326 if (r) {
2327 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002328 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
Gavin Wan89041942017-06-23 13:55:15 -04002329 goto failed;
Alex Deuchera5bde2f2016-09-23 16:23:41 -04002330 }
2331 /* init i2c buses */
Harry Wentland45622362017-09-12 15:58:20 -04002332 if (!amdgpu_device_has_dc_support(adev))
2333 amdgpu_atombios_i2c_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002334 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002335
2336 /* Fence driver */
2337 r = amdgpu_fence_driver_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002338 if (r) {
2339 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002340 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0);
Alex Deucher83ba1262016-06-03 18:21:41 -04002341 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002342 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002343
2344 /* init the mode config */
2345 drm_mode_config_init(adev->ddev);
2346
2347 r = amdgpu_init(adev);
2348 if (r) {
pding8840a382017-10-23 17:22:09 +08002349 /* failed in exclusive mode due to timeout */
2350 if (amdgpu_sriov_vf(adev) &&
2351 !amdgpu_sriov_runtime(adev) &&
2352 amdgpu_virt_mmio_blocked(adev) &&
2353 !amdgpu_virt_wait_reset(adev)) {
2354 dev_err(adev->dev, "VF exclusive mode timeout\n");
Pixel Ding1daee8b2017-11-08 11:03:14 +08002355 /* Don't send request since VF is inactive. */
2356 adev->virt.caps &= ~AMDGPU_SRIOV_CAPS_RUNTIME;
2357 adev->virt.ops = NULL;
pding8840a382017-10-23 17:22:09 +08002358 r = -EAGAIN;
2359 goto failed;
2360 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05002361 dev_err(adev->dev, "amdgpu_init failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002362 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002363 amdgpu_fini(adev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002364 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002365 }
2366
2367 adev->accel_working = true;
2368
Alex Xiee59c0202017-06-01 09:42:59 -04002369 amdgpu_vm_check_compute_bug(adev);
2370
Marek Olšák95844d22016-08-17 23:49:27 +02002371 /* Initialize the buffer migration limit. */
2372 if (amdgpu_moverate >= 0)
2373 max_MBps = amdgpu_moverate;
2374 else
2375 max_MBps = 8; /* Allow 8 MB/s. */
2376 /* Get a log2 for easy divisions. */
2377 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
2378
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002379 r = amdgpu_ib_pool_init(adev);
2380 if (r) {
2381 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
Alex Deuchere23b74a2017-09-28 09:47:32 -04002382 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r);
Alex Deucher83ba1262016-06-03 18:21:41 -04002383 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002384 }
2385
2386 r = amdgpu_ib_ring_tests(adev);
2387 if (r)
2388 DRM_ERROR("ib ring test failed (%d).\n", r);
2389
Horace Chen2dc8f812017-10-09 16:17:16 +08002390 if (amdgpu_sriov_vf(adev))
2391 amdgpu_virt_init_data_exchange(adev);
2392
Monk Liu9bc92b92017-02-08 17:38:13 +08002393 amdgpu_fbdev_init(adev);
2394
Rex Zhud2f52ac2017-09-22 17:47:27 +08002395 r = amdgpu_pm_sysfs_init(adev);
2396 if (r)
2397 DRM_ERROR("registering pm debugfs failed (%d).\n", r);
2398
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002399 r = amdgpu_gem_debugfs_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002400 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002401 DRM_ERROR("registering gem debugfs failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002402
2403 r = amdgpu_debugfs_regs_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002404 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002405 DRM_ERROR("registering register debugfs failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002406
Huang Rui50ab2532016-06-12 15:51:09 +08002407 r = amdgpu_debugfs_firmware_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002408 if (r)
Huang Rui50ab2532016-06-12 15:51:09 +08002409 DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
Huang Rui50ab2532016-06-12 15:51:09 +08002410
Christian König763efb62017-12-06 15:44:51 +01002411 r = amdgpu_debugfs_init(adev);
Kent Russelldb95e212017-08-22 12:31:43 -04002412 if (r)
Christian König763efb62017-12-06 15:44:51 +01002413 DRM_ERROR("Creating debugfs files failed (%d).\n", r);
Kent Russelldb95e212017-08-22 12:31:43 -04002414
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002415 if ((amdgpu_testing & 1)) {
2416 if (adev->accel_working)
2417 amdgpu_test_moves(adev);
2418 else
2419 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
2420 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002421 if (amdgpu_benchmarking) {
2422 if (adev->accel_working)
2423 amdgpu_benchmark(adev, amdgpu_benchmarking);
2424 else
2425 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
2426 }
2427
2428 /* enable clockgating, etc. after ib tests, etc. since some blocks require
2429 * explicit gating rather than handling it automatically.
2430 */
2431 r = amdgpu_late_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002432 if (r) {
2433 dev_err(adev->dev, "amdgpu_late_init failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002434 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r);
Alex Deucher83ba1262016-06-03 18:21:41 -04002435 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002436 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002437
2438 return 0;
Alex Deucher83ba1262016-06-03 18:21:41 -04002439
2440failed:
Gavin Wan89041942017-06-23 13:55:15 -04002441 amdgpu_vf_error_trans_all(adev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002442 if (runtime)
2443 vga_switcheroo_fini_domain_pm_ops(adev->dev);
pding8840a382017-10-23 17:22:09 +08002444
Alex Deucher83ba1262016-06-03 18:21:41 -04002445 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002446}
2447
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002448/**
2449 * amdgpu_device_fini - tear down the driver
2450 *
2451 * @adev: amdgpu_device pointer
2452 *
2453 * Tear down the driver info (all asics).
2454 * Called at driver shutdown.
2455 */
2456void amdgpu_device_fini(struct amdgpu_device *adev)
2457{
2458 int r;
2459
2460 DRM_INFO("amdgpu: finishing device.\n");
2461 adev->shutdown = true;
Pixel Dingdb2c2a92017-04-25 16:47:42 +08002462 if (adev->mode_info.mode_config_initialized)
2463 drm_crtc_force_disable_all(adev->ddev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002464 /* evict vram memory */
2465 amdgpu_bo_evict_vram(adev);
2466 amdgpu_ib_pool_fini(adev);
2467 amdgpu_fence_driver_fini(adev);
2468 amdgpu_fbdev_fini(adev);
2469 r = amdgpu_fini(adev);
Huang Ruiab4fe3e2017-06-05 22:11:59 +08002470 if (adev->firmware.gpu_info_fw) {
2471 release_firmware(adev->firmware.gpu_info_fw);
2472 adev->firmware.gpu_info_fw = NULL;
2473 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002474 adev->accel_working = false;
Shirish S2dc80b02017-05-25 10:05:25 +05302475 cancel_delayed_work_sync(&adev->late_init_work);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002476 /* free i2c buses */
Harry Wentland45622362017-09-12 15:58:20 -04002477 if (!amdgpu_device_has_dc_support(adev))
2478 amdgpu_i2c_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002479 amdgpu_atombios_fini(adev);
2480 kfree(adev->bios);
2481 adev->bios = NULL;
Lukas Wunner84c8b222017-03-10 21:23:45 +01002482 if (!pci_is_thunderbolt_attached(adev->pdev))
2483 vga_switcheroo_unregister_client(adev->pdev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002484 if (adev->flags & AMD_IS_PX)
2485 vga_switcheroo_fini_domain_pm_ops(adev->dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002486 vga_client_register(adev->pdev, NULL, NULL, NULL);
2487 if (adev->rio_mem)
2488 pci_iounmap(adev->pdev, adev->rio_mem);
2489 adev->rio_mem = NULL;
2490 iounmap(adev->rmmio);
2491 adev->rmmio = NULL;
Christian König705e5192017-06-08 11:15:16 +02002492 amdgpu_doorbell_fini(adev);
Rex Zhud2f52ac2017-09-22 17:47:27 +08002493 amdgpu_pm_sysfs_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002494 amdgpu_debugfs_regs_cleanup(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002495}
2496
2497
2498/*
2499 * Suspend & resume.
2500 */
2501/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002502 * amdgpu_device_suspend - initiate device suspend
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002503 *
2504 * @pdev: drm dev pointer
2505 * @state: suspend state
2506 *
2507 * Puts the hw in the suspend state (all asics).
2508 * Returns 0 for success or an error on failure.
2509 * Called at driver suspend.
2510 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002511int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002512{
2513 struct amdgpu_device *adev;
2514 struct drm_crtc *crtc;
2515 struct drm_connector *connector;
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002516 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002517
2518 if (dev == NULL || dev->dev_private == NULL) {
2519 return -ENODEV;
2520 }
2521
2522 adev = dev->dev_private;
2523
2524 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2525 return 0;
2526
2527 drm_kms_helper_poll_disable(dev);
2528
Harry Wentland45622362017-09-12 15:58:20 -04002529 if (!amdgpu_device_has_dc_support(adev)) {
2530 /* turn off display hw */
2531 drm_modeset_lock_all(dev);
2532 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2533 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
2534 }
2535 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002536 }
2537
Yong Zhaoba997702015-11-09 17:21:45 -05002538 amdgpu_amdkfd_suspend(adev);
2539
Alex Deucher756e6882015-10-08 00:03:36 -04002540 /* unpin the front buffers and cursors */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002541 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Alex Deucher756e6882015-10-08 00:03:36 -04002542 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002543 struct amdgpu_framebuffer *rfb = to_amdgpu_framebuffer(crtc->primary->fb);
2544 struct amdgpu_bo *robj;
2545
Alex Deucher756e6882015-10-08 00:03:36 -04002546 if (amdgpu_crtc->cursor_bo) {
2547 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
Alex Xie7a6901d2017-04-24 13:52:41 -04002548 r = amdgpu_bo_reserve(aobj, true);
Alex Deucher756e6882015-10-08 00:03:36 -04002549 if (r == 0) {
2550 amdgpu_bo_unpin(aobj);
2551 amdgpu_bo_unreserve(aobj);
2552 }
2553 }
2554
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002555 if (rfb == NULL || rfb->obj == NULL) {
2556 continue;
2557 }
2558 robj = gem_to_amdgpu_bo(rfb->obj);
2559 /* don't unpin kernel fb objects */
2560 if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
Alex Xie7a6901d2017-04-24 13:52:41 -04002561 r = amdgpu_bo_reserve(robj, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002562 if (r == 0) {
2563 amdgpu_bo_unpin(robj);
2564 amdgpu_bo_unreserve(robj);
2565 }
2566 }
2567 }
2568 /* evict vram memory */
2569 amdgpu_bo_evict_vram(adev);
2570
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002571 amdgpu_fence_driver_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002572
2573 r = amdgpu_suspend(adev);
2574
Alex Deuchera0a71e42016-10-10 12:41:36 -04002575 /* evict remaining vram memory
2576 * This second call to evict vram is to evict the gart page table
2577 * using the CPU.
2578 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002579 amdgpu_bo_evict_vram(adev);
2580
Alex Deucherd05da0e2017-06-30 17:08:45 -04002581 amdgpu_atombios_scratch_regs_save(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002582 pci_save_state(dev->pdev);
2583 if (suspend) {
2584 /* Shut down the device */
2585 pci_disable_device(dev->pdev);
2586 pci_set_power_state(dev->pdev, PCI_D3hot);
jimqu74b0b152016-09-07 17:09:12 +08002587 } else {
2588 r = amdgpu_asic_reset(adev);
2589 if (r)
2590 DRM_ERROR("amdgpu asic reset failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002591 }
2592
2593 if (fbcon) {
2594 console_lock();
2595 amdgpu_fbdev_set_suspend(adev, 1);
2596 console_unlock();
2597 }
2598 return 0;
2599}
2600
2601/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002602 * amdgpu_device_resume - initiate device resume
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002603 *
2604 * @pdev: drm dev pointer
2605 *
2606 * Bring the hw back to operating state (all asics).
2607 * Returns 0 for success or an error on failure.
2608 * Called at driver resume.
2609 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002610int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002611{
2612 struct drm_connector *connector;
2613 struct amdgpu_device *adev = dev->dev_private;
Alex Deucher756e6882015-10-08 00:03:36 -04002614 struct drm_crtc *crtc;
Huang Rui03161a62017-04-13 16:12:26 +08002615 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002616
2617 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2618 return 0;
2619
jimqu74b0b152016-09-07 17:09:12 +08002620 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002621 console_lock();
jimqu74b0b152016-09-07 17:09:12 +08002622
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002623 if (resume) {
2624 pci_set_power_state(dev->pdev, PCI_D0);
2625 pci_restore_state(dev->pdev);
jimqu74b0b152016-09-07 17:09:12 +08002626 r = pci_enable_device(dev->pdev);
Huang Rui03161a62017-04-13 16:12:26 +08002627 if (r)
2628 goto unlock;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002629 }
Alex Deucherd05da0e2017-06-30 17:08:45 -04002630 amdgpu_atombios_scratch_regs_restore(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002631
2632 /* post card */
Jim Quc836fec2017-02-10 15:59:59 +08002633 if (amdgpu_need_post(adev)) {
jimqu74b0b152016-09-07 17:09:12 +08002634 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2635 if (r)
2636 DRM_ERROR("amdgpu asic init failed\n");
2637 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002638
2639 r = amdgpu_resume(adev);
Rex Zhue6707212017-03-30 13:21:01 +08002640 if (r) {
Flora Cuica198522016-02-04 15:10:08 +08002641 DRM_ERROR("amdgpu_resume failed (%d).\n", r);
Huang Rui03161a62017-04-13 16:12:26 +08002642 goto unlock;
Rex Zhue6707212017-03-30 13:21:01 +08002643 }
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002644 amdgpu_fence_driver_resume(adev);
2645
Flora Cuica198522016-02-04 15:10:08 +08002646 if (resume) {
2647 r = amdgpu_ib_ring_tests(adev);
2648 if (r)
2649 DRM_ERROR("ib ring test failed (%d).\n", r);
2650 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002651
2652 r = amdgpu_late_init(adev);
Huang Rui03161a62017-04-13 16:12:26 +08002653 if (r)
2654 goto unlock;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002655
Alex Deucher756e6882015-10-08 00:03:36 -04002656 /* pin cursors */
2657 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2658 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2659
2660 if (amdgpu_crtc->cursor_bo) {
2661 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
Alex Xie7a6901d2017-04-24 13:52:41 -04002662 r = amdgpu_bo_reserve(aobj, true);
Alex Deucher756e6882015-10-08 00:03:36 -04002663 if (r == 0) {
2664 r = amdgpu_bo_pin(aobj,
2665 AMDGPU_GEM_DOMAIN_VRAM,
2666 &amdgpu_crtc->cursor_addr);
2667 if (r != 0)
2668 DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
2669 amdgpu_bo_unreserve(aobj);
2670 }
2671 }
2672 }
Yong Zhaoba997702015-11-09 17:21:45 -05002673 r = amdgpu_amdkfd_resume(adev);
2674 if (r)
2675 return r;
Alex Deucher756e6882015-10-08 00:03:36 -04002676
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002677 /* blat the mode back in */
2678 if (fbcon) {
Harry Wentland45622362017-09-12 15:58:20 -04002679 if (!amdgpu_device_has_dc_support(adev)) {
2680 /* pre DCE11 */
2681 drm_helper_resume_force_mode(dev);
2682
2683 /* turn on display hw */
2684 drm_modeset_lock_all(dev);
2685 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2686 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
2687 }
2688 drm_modeset_unlock_all(dev);
2689 } else {
2690 /*
2691 * There is no equivalent atomic helper to turn on
2692 * display, so we defined our own function for this,
2693 * once suspend resume is supported by the atomic
2694 * framework this will be reworked
2695 */
2696 amdgpu_dm_display_resume(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002697 }
2698 }
2699
2700 drm_kms_helper_poll_enable(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002701
2702 /*
2703 * Most of the connector probing functions try to acquire runtime pm
2704 * refs to ensure that the GPU is powered on when connector polling is
2705 * performed. Since we're calling this from a runtime PM callback,
2706 * trying to acquire rpm refs will cause us to deadlock.
2707 *
2708 * Since we're guaranteed to be holding the rpm lock, it's safe to
2709 * temporarily disable the rpm helpers so this doesn't deadlock us.
2710 */
2711#ifdef CONFIG_PM
2712 dev->dev->power.disable_depth++;
2713#endif
Harry Wentland45622362017-09-12 15:58:20 -04002714 if (!amdgpu_device_has_dc_support(adev))
2715 drm_helper_hpd_irq_event(dev);
2716 else
2717 drm_kms_helper_hotplug_event(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002718#ifdef CONFIG_PM
2719 dev->dev->power.disable_depth--;
2720#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002721
Huang Rui03161a62017-04-13 16:12:26 +08002722 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002723 amdgpu_fbdev_set_suspend(adev, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002724
Huang Rui03161a62017-04-13 16:12:26 +08002725unlock:
2726 if (fbcon)
2727 console_unlock();
2728
2729 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002730}
2731
Chunming Zhou63fbf422016-07-15 11:19:20 +08002732static bool amdgpu_check_soft_reset(struct amdgpu_device *adev)
2733{
2734 int i;
2735 bool asic_hang = false;
2736
Monk Liuf993d622017-10-16 19:46:01 +08002737 if (amdgpu_sriov_vf(adev))
2738 return true;
2739
Chunming Zhou63fbf422016-07-15 11:19:20 +08002740 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002741 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou63fbf422016-07-15 11:19:20 +08002742 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002743 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
2744 adev->ip_blocks[i].status.hang =
2745 adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
2746 if (adev->ip_blocks[i].status.hang) {
2747 DRM_INFO("IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
Chunming Zhou63fbf422016-07-15 11:19:20 +08002748 asic_hang = true;
2749 }
2750 }
2751 return asic_hang;
2752}
2753
Baoyou Xie4d446652016-09-18 22:09:35 +08002754static int amdgpu_pre_soft_reset(struct amdgpu_device *adev)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002755{
2756 int i, r = 0;
2757
2758 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002759 if (!adev->ip_blocks[i].status.valid)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002760 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002761 if (adev->ip_blocks[i].status.hang &&
2762 adev->ip_blocks[i].version->funcs->pre_soft_reset) {
2763 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
Chunming Zhoud31a5012016-07-18 10:04:34 +08002764 if (r)
2765 return r;
2766 }
2767 }
2768
2769 return 0;
2770}
2771
Chunming Zhou35d782f2016-07-15 15:57:13 +08002772static bool amdgpu_need_full_reset(struct amdgpu_device *adev)
2773{
Alex Deucherda146d32016-10-13 16:07:03 -04002774 int i;
2775
2776 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002777 if (!adev->ip_blocks[i].status.valid)
Alex Deucherda146d32016-10-13 16:07:03 -04002778 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002779 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
2780 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
2781 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
Ken Wang98512bb2017-09-14 16:25:19 +08002782 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) ||
2783 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
Alex Deuchera1255102016-10-13 17:41:13 -04002784 if (adev->ip_blocks[i].status.hang) {
Alex Deucherda146d32016-10-13 16:07:03 -04002785 DRM_INFO("Some block need full reset!\n");
2786 return true;
2787 }
2788 }
Chunming Zhou35d782f2016-07-15 15:57:13 +08002789 }
2790 return false;
2791}
2792
2793static int amdgpu_soft_reset(struct amdgpu_device *adev)
2794{
2795 int i, r = 0;
2796
2797 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002798 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002799 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002800 if (adev->ip_blocks[i].status.hang &&
2801 adev->ip_blocks[i].version->funcs->soft_reset) {
2802 r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002803 if (r)
2804 return r;
2805 }
2806 }
2807
2808 return 0;
2809}
2810
2811static int amdgpu_post_soft_reset(struct amdgpu_device *adev)
2812{
2813 int i, r = 0;
2814
2815 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002816 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002817 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002818 if (adev->ip_blocks[i].status.hang &&
2819 adev->ip_blocks[i].version->funcs->post_soft_reset)
2820 r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002821 if (r)
2822 return r;
2823 }
2824
2825 return 0;
2826}
2827
Chunming Zhou3ad81f12016-08-05 17:30:17 +08002828bool amdgpu_need_backup(struct amdgpu_device *adev)
2829{
2830 if (adev->flags & AMD_IS_APU)
2831 return false;
2832
2833 return amdgpu_lockup_timeout > 0 ? true : false;
2834}
2835
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002836static int amdgpu_recover_vram_from_shadow(struct amdgpu_device *adev,
2837 struct amdgpu_ring *ring,
2838 struct amdgpu_bo *bo,
Chris Wilsonf54d1862016-10-25 13:00:45 +01002839 struct dma_fence **fence)
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002840{
2841 uint32_t domain;
2842 int r;
2843
Roger.He23d2e502017-04-21 14:24:26 +08002844 if (!bo->shadow)
2845 return 0;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002846
Alex Xie1d284792017-04-24 13:53:04 -04002847 r = amdgpu_bo_reserve(bo, true);
Roger.He23d2e502017-04-21 14:24:26 +08002848 if (r)
2849 return r;
2850 domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
2851 /* if bo has been evicted, then no need to recover */
2852 if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
Roger.He82521312017-04-21 13:08:43 +08002853 r = amdgpu_bo_validate(bo->shadow);
2854 if (r) {
2855 DRM_ERROR("bo validate failed!\n");
2856 goto err;
2857 }
2858
Roger.He23d2e502017-04-21 14:24:26 +08002859 r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002860 NULL, fence, true);
Roger.He23d2e502017-04-21 14:24:26 +08002861 if (r) {
2862 DRM_ERROR("recover page table failed!\n");
2863 goto err;
2864 }
2865 }
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002866err:
Roger.He23d2e502017-04-21 14:24:26 +08002867 amdgpu_bo_unreserve(bo);
2868 return r;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002869}
2870
Monk Liu57406822017-10-25 16:37:02 +08002871/*
2872 * amdgpu_reset - reset ASIC/GPU for bare-metal or passthrough
Monk Liua90ad3c2017-01-23 14:22:08 +08002873 *
2874 * @adev: amdgpu device pointer
Monk Liu57406822017-10-25 16:37:02 +08002875 * @reset_flags: output param tells caller the reset result
Monk Liua90ad3c2017-01-23 14:22:08 +08002876 *
Monk Liu57406822017-10-25 16:37:02 +08002877 * attempt to do soft-reset or full-reset and reinitialize Asic
2878 * return 0 means successed otherwise failed
2879*/
2880static int amdgpu_reset(struct amdgpu_device *adev, uint64_t* reset_flags)
Monk Liua90ad3c2017-01-23 14:22:08 +08002881{
Monk Liu57406822017-10-25 16:37:02 +08002882 bool need_full_reset, vram_lost = 0;
2883 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002884
Chunming Zhou35d782f2016-07-15 15:57:13 +08002885 need_full_reset = amdgpu_need_full_reset(adev);
2886
2887 if (!need_full_reset) {
2888 amdgpu_pre_soft_reset(adev);
2889 r = amdgpu_soft_reset(adev);
2890 amdgpu_post_soft_reset(adev);
2891 if (r || amdgpu_check_soft_reset(adev)) {
2892 DRM_INFO("soft reset failed, will fallback to full reset!\n");
2893 need_full_reset = true;
2894 }
Monk Liu57406822017-10-25 16:37:02 +08002895
Chunming Zhou35d782f2016-07-15 15:57:13 +08002896 }
2897
2898 if (need_full_reset) {
Chunming Zhou35d782f2016-07-15 15:57:13 +08002899 r = amdgpu_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002900
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002901retry:
Alex Deucherd05da0e2017-06-30 17:08:45 -04002902 amdgpu_atombios_scratch_regs_save(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002903 r = amdgpu_asic_reset(adev);
Alex Deucherd05da0e2017-06-30 17:08:45 -04002904 amdgpu_atombios_scratch_regs_restore(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002905 /* post card */
2906 amdgpu_atom_asic_init(adev->mode_info.atom_context);
Alex Deucherbfa99262016-01-15 11:59:48 -05002907
Chunming Zhou35d782f2016-07-15 15:57:13 +08002908 if (!r) {
2909 dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
Chunming Zhoufcf06492017-05-05 10:33:33 +08002910 r = amdgpu_resume_phase1(adev);
2911 if (r)
2912 goto out;
Monk Liu57406822017-10-25 16:37:02 +08002913
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002914 vram_lost = amdgpu_check_vram_lost(adev);
Chunming Zhouf1892132017-05-15 16:48:27 +08002915 if (vram_lost) {
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002916 DRM_ERROR("VRAM is lost!\n");
Chunming Zhouf1892132017-05-15 16:48:27 +08002917 atomic_inc(&adev->vram_lost_counter);
2918 }
Monk Liu57406822017-10-25 16:37:02 +08002919
Christian Königc1c7ce82017-10-16 16:50:32 +02002920 r = amdgpu_gtt_mgr_recover(
2921 &adev->mman.bdev.man[TTM_PL_TT]);
Chunming Zhou2c0d7312016-08-30 16:36:25 +08002922 if (r)
Chunming Zhoufcf06492017-05-05 10:33:33 +08002923 goto out;
Monk Liu57406822017-10-25 16:37:02 +08002924
Chunming Zhoufcf06492017-05-05 10:33:33 +08002925 r = amdgpu_resume_phase2(adev);
2926 if (r)
2927 goto out;
Monk Liu57406822017-10-25 16:37:02 +08002928
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002929 if (vram_lost)
2930 amdgpu_fill_reset_magic(adev);
Chunming Zhou2c0d7312016-08-30 16:36:25 +08002931 }
Chunming Zhoufcf06492017-05-05 10:33:33 +08002932 }
Monk Liu57406822017-10-25 16:37:02 +08002933
Chunming Zhoufcf06492017-05-05 10:33:33 +08002934out:
2935 if (!r) {
2936 amdgpu_irq_gpu_reset_resume_helper(adev);
Chunming Zhou1f465082016-06-30 15:02:26 +08002937 r = amdgpu_ib_ring_tests(adev);
2938 if (r) {
2939 dev_err(adev->dev, "ib ring test failed (%d).\n", r);
Chunming Zhou40019dc2016-06-29 16:01:49 +08002940 r = amdgpu_suspend(adev);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002941 need_full_reset = true;
Chunming Zhou40019dc2016-06-29 16:01:49 +08002942 goto retry;
Chunming Zhou1f465082016-06-30 15:02:26 +08002943 }
Monk Liu57406822017-10-25 16:37:02 +08002944 }
2945
2946 if (reset_flags) {
2947 if (vram_lost)
2948 (*reset_flags) |= AMDGPU_RESET_INFO_VRAM_LOST;
2949
2950 if (need_full_reset)
2951 (*reset_flags) |= AMDGPU_RESET_INFO_FULLRESET;
2952 }
2953
2954 return r;
2955}
2956
2957/*
2958 * amdgpu_reset_sriov - reset ASIC for SR-IOV vf
2959 *
2960 * @adev: amdgpu device pointer
2961 * @reset_flags: output param tells caller the reset result
2962 *
2963 * do VF FLR and reinitialize Asic
2964 * return 0 means successed otherwise failed
2965*/
2966static int amdgpu_reset_sriov(struct amdgpu_device *adev, uint64_t *reset_flags, bool from_hypervisor)
2967{
2968 int r;
2969
2970 if (from_hypervisor)
2971 r = amdgpu_virt_request_full_gpu(adev, true);
2972 else
2973 r = amdgpu_virt_reset_gpu(adev);
2974 if (r)
2975 return r;
2976
2977 /* Resume IP prior to SMC */
2978 r = amdgpu_sriov_reinit_early(adev);
2979 if (r)
2980 goto error;
2981
2982 /* we need recover gart prior to run SMC/CP/SDMA resume */
Christian Königc1c7ce82017-10-16 16:50:32 +02002983 amdgpu_gtt_mgr_recover(&adev->mman.bdev.man[TTM_PL_TT]);
Monk Liu57406822017-10-25 16:37:02 +08002984
2985 /* now we are okay to resume SMC/CP/SDMA */
2986 r = amdgpu_sriov_reinit_late(adev);
2987 if (r)
2988 goto error;
2989
2990 amdgpu_irq_gpu_reset_resume_helper(adev);
2991 r = amdgpu_ib_ring_tests(adev);
2992 if (r)
2993 dev_err(adev->dev, "[GPU_RESET] ib ring test failed (%d).\n", r);
2994
2995error:
2996 /* release full control of GPU after ib test */
2997 amdgpu_virt_release_full_gpu(adev, true);
2998
2999 if (reset_flags) {
Monk Liu75bc6092017-10-30 20:11:54 +08003000 if (adev->virt.gim_feature & AMDGIM_FEATURE_GIM_FLR_VRAMLOST) {
3001 (*reset_flags) |= AMDGPU_RESET_INFO_VRAM_LOST;
3002 atomic_inc(&adev->vram_lost_counter);
3003 }
Monk Liu57406822017-10-25 16:37:02 +08003004
3005 /* VF FLR or hotlink reset is always full-reset */
3006 (*reset_flags) |= AMDGPU_RESET_INFO_FULLRESET;
3007 }
3008
3009 return r;
3010}
3011
3012/**
3013 * amdgpu_gpu_recover - reset the asic and recover scheduler
3014 *
3015 * @adev: amdgpu device pointer
3016 * @job: which job trigger hang
3017 *
3018 * Attempt to reset the GPU if it has hung (all asics).
3019 * Returns 0 for success or an error on failure.
3020 */
3021int amdgpu_gpu_recover(struct amdgpu_device *adev, struct amdgpu_job *job)
3022{
3023 struct drm_atomic_state *state = NULL;
3024 uint64_t reset_flags = 0;
3025 int i, r, resched;
3026
3027 if (!amdgpu_check_soft_reset(adev)) {
3028 DRM_INFO("No hardware hang detected. Did some blocks stall?\n");
3029 return 0;
3030 }
3031
3032 dev_info(adev->dev, "GPU reset begin!\n");
3033
Monk Liu13a752e2017-10-17 15:11:12 +08003034 mutex_lock(&adev->lock_reset);
Monk Liu57406822017-10-25 16:37:02 +08003035 atomic_inc(&adev->gpu_reset_counter);
Monk Liu13a752e2017-10-17 15:11:12 +08003036 adev->in_gpu_reset = 1;
Monk Liu57406822017-10-25 16:37:02 +08003037
3038 /* block TTM */
3039 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
3040 /* store modesetting */
3041 if (amdgpu_device_has_dc_support(adev))
3042 state = drm_atomic_helper_suspend(adev->ddev);
3043
3044 /* block scheduler */
3045 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
3046 struct amdgpu_ring *ring = adev->rings[i];
3047
3048 if (!ring || !ring->sched.thread)
3049 continue;
3050
3051 /* only focus on the ring hit timeout if &job not NULL */
3052 if (job && job->ring->idx != i)
3053 continue;
3054
3055 kthread_park(ring->sched.thread);
Lucas Stach1b1f42d2017-12-06 17:49:39 +01003056 drm_sched_hw_job_reset(&ring->sched, &job->base);
Monk Liu57406822017-10-25 16:37:02 +08003057
3058 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
3059 amdgpu_fence_driver_force_completion(ring);
3060 }
3061
3062 if (amdgpu_sriov_vf(adev))
3063 r = amdgpu_reset_sriov(adev, &reset_flags, job ? false : true);
3064 else
3065 r = amdgpu_reset(adev, &reset_flags);
3066
3067 if (!r) {
3068 if (((reset_flags & AMDGPU_RESET_INFO_FULLRESET) && !(adev->flags & AMD_IS_APU)) ||
3069 (reset_flags & AMDGPU_RESET_INFO_VRAM_LOST)) {
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003070 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
3071 struct amdgpu_bo *bo, *tmp;
Chris Wilsonf54d1862016-10-25 13:00:45 +01003072 struct dma_fence *fence = NULL, *next = NULL;
Chunming Zhou1f465082016-06-30 15:02:26 +08003073
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003074 DRM_INFO("recover vram bo from shadow\n");
3075 mutex_lock(&adev->shadow_list_lock);
3076 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
Monk Liu236763d2017-05-01 16:15:31 +08003077 next = NULL;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003078 amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
3079 if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01003080 r = dma_fence_wait(fence, false);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003081 if (r) {
Monk Liu1d7b17b2017-01-22 18:52:56 +08003082 WARN(r, "recovery from shadow isn't completed\n");
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003083 break;
3084 }
3085 }
3086
Chris Wilsonf54d1862016-10-25 13:00:45 +01003087 dma_fence_put(fence);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003088 fence = next;
3089 }
3090 mutex_unlock(&adev->shadow_list_lock);
3091 if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01003092 r = dma_fence_wait(fence, false);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003093 if (r)
Monk Liu1d7b17b2017-01-22 18:52:56 +08003094 WARN(r, "recovery from shadow isn't completed\n");
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003095 }
Chris Wilsonf54d1862016-10-25 13:00:45 +01003096 dma_fence_put(fence);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003097 }
Monk Liu57406822017-10-25 16:37:02 +08003098
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003099 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
3100 struct amdgpu_ring *ring = adev->rings[i];
Chunming Zhou51687752017-04-24 17:09:15 +08003101
3102 if (!ring || !ring->sched.thread)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003103 continue;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003104
Monk Liu57406822017-10-25 16:37:02 +08003105 /* only focus on the ring hit timeout if &job not NULL */
3106 if (job && job->ring->idx != i)
3107 continue;
3108
Lucas Stach1b1f42d2017-12-06 17:49:39 +01003109 drm_sched_job_recovery(&ring->sched);
Chunming Zhou0875dc92016-06-12 15:41:58 +08003110 kthread_unpark(ring->sched.thread);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003111 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003112 } else {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003113 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
Monk Liu57406822017-10-25 16:37:02 +08003114 struct amdgpu_ring *ring = adev->rings[i];
3115
3116 if (!ring || !ring->sched.thread)
3117 continue;
3118
3119 /* only focus on the ring hit timeout if &job not NULL */
3120 if (job && job->ring->idx != i)
3121 continue;
3122
3123 kthread_unpark(adev->rings[i]->sched.thread);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003124 }
3125 }
3126
Harry Wentland45622362017-09-12 15:58:20 -04003127 if (amdgpu_device_has_dc_support(adev)) {
Monk Liu57406822017-10-25 16:37:02 +08003128 if (drm_atomic_helper_resume(adev->ddev, state))
3129 dev_info(adev->dev, "drm resume failed:%d\n", r);
Harry Wentland45622362017-09-12 15:58:20 -04003130 amdgpu_dm_display_resume(adev);
Monk Liu57406822017-10-25 16:37:02 +08003131 } else {
Harry Wentland45622362017-09-12 15:58:20 -04003132 drm_helper_resume_force_mode(adev->ddev);
Monk Liu57406822017-10-25 16:37:02 +08003133 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003134
3135 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
Monk Liu57406822017-10-25 16:37:02 +08003136
Gavin Wan89041942017-06-23 13:55:15 -04003137 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003138 /* bad news, how to tell it to userspace ? */
Monk Liu57406822017-10-25 16:37:02 +08003139 dev_info(adev->dev, "GPU reset(%d) failed\n", atomic_read(&adev->gpu_reset_counter));
3140 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, r);
3141 } else {
3142 dev_info(adev->dev, "GPU reset(%d) successed!\n",atomic_read(&adev->gpu_reset_counter));
Gavin Wan89041942017-06-23 13:55:15 -04003143 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003144
Gavin Wan89041942017-06-23 13:55:15 -04003145 amdgpu_vf_error_trans_all(adev);
Monk Liu13a752e2017-10-17 15:11:12 +08003146 adev->in_gpu_reset = 0;
3147 mutex_unlock(&adev->lock_reset);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003148 return r;
3149}
3150
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003151void amdgpu_get_pcie_info(struct amdgpu_device *adev)
3152{
3153 u32 mask;
3154 int ret;
3155
Alex Deuchercd474ba2016-02-04 10:21:23 -05003156 if (amdgpu_pcie_gen_cap)
3157 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
3158
3159 if (amdgpu_pcie_lane_cap)
3160 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
3161
3162 /* covers APUs as well */
3163 if (pci_is_root_bus(adev->pdev->bus)) {
3164 if (adev->pm.pcie_gen_mask == 0)
3165 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
3166 if (adev->pm.pcie_mlw_mask == 0)
3167 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003168 return;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003169 }
Alex Deuchercd474ba2016-02-04 10:21:23 -05003170
3171 if (adev->pm.pcie_gen_mask == 0) {
3172 ret = drm_pcie_get_speed_cap_mask(adev->ddev, &mask);
3173 if (!ret) {
3174 adev->pm.pcie_gen_mask = (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
3175 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
3176 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
3177
3178 if (mask & DRM_PCIE_SPEED_25)
3179 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
3180 if (mask & DRM_PCIE_SPEED_50)
3181 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2;
3182 if (mask & DRM_PCIE_SPEED_80)
3183 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3;
3184 } else {
3185 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
3186 }
3187 }
3188 if (adev->pm.pcie_mlw_mask == 0) {
3189 ret = drm_pcie_get_max_link_width(adev->ddev, &mask);
3190 if (!ret) {
3191 switch (mask) {
3192 case 32:
3193 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
3194 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
3195 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3196 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3197 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3198 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3199 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3200 break;
3201 case 16:
3202 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
3203 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3204 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3205 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3206 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3207 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3208 break;
3209 case 12:
3210 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3211 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3212 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3213 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3214 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3215 break;
3216 case 8:
3217 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3218 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3219 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3220 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3221 break;
3222 case 4:
3223 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3224 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3225 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3226 break;
3227 case 2:
3228 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3229 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3230 break;
3231 case 1:
3232 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
3233 break;
3234 default:
3235 break;
3236 }
3237 } else {
3238 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003239 }
3240 }
3241}
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003242
3243/*
3244 * Debugfs
3245 */
3246int amdgpu_debugfs_add_files(struct amdgpu_device *adev,
Nils Wallménius06ab6832016-05-02 12:46:15 -04003247 const struct drm_info_list *files,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003248 unsigned nfiles)
3249{
3250 unsigned i;
3251
3252 for (i = 0; i < adev->debugfs_count; i++) {
3253 if (adev->debugfs[i].files == files) {
3254 /* Already registered */
3255 return 0;
3256 }
3257 }
3258
3259 i = adev->debugfs_count + 1;
3260 if (i > AMDGPU_DEBUGFS_MAX_COMPONENTS) {
3261 DRM_ERROR("Reached maximum number of debugfs components.\n");
3262 DRM_ERROR("Report so we increase "
3263 "AMDGPU_DEBUGFS_MAX_COMPONENTS.\n");
3264 return -EINVAL;
3265 }
3266 adev->debugfs[adev->debugfs_count].files = files;
3267 adev->debugfs[adev->debugfs_count].num_files = nfiles;
3268 adev->debugfs_count = i;
3269#if defined(CONFIG_DEBUG_FS)
3270 drm_debugfs_create_files(files, nfiles,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003271 adev->ddev->primary->debugfs_root,
3272 adev->ddev->primary);
3273#endif
3274 return 0;
3275}
3276
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003277#if defined(CONFIG_DEBUG_FS)
3278
3279static ssize_t amdgpu_debugfs_regs_read(struct file *f, char __user *buf,
3280 size_t size, loff_t *pos)
3281{
Al Viro45063092016-12-04 18:24:56 -05003282 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003283 ssize_t result = 0;
3284 int r;
Tom St Denisbd122672016-07-28 09:39:22 -04003285 bool pm_pg_lock, use_bank;
Tom St Denis566281592016-06-27 11:55:07 -04003286 unsigned instance_bank, sh_bank, se_bank;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003287
3288 if (size & 0x3 || *pos & 0x3)
3289 return -EINVAL;
3290
Tom St Denisbd122672016-07-28 09:39:22 -04003291 /* are we reading registers for which a PG lock is necessary? */
3292 pm_pg_lock = (*pos >> 23) & 1;
3293
Tom St Denis566281592016-06-27 11:55:07 -04003294 if (*pos & (1ULL << 62)) {
Tom St Denis0b968652017-11-10 12:54:50 -05003295 se_bank = (*pos & GENMASK_ULL(33, 24)) >> 24;
3296 sh_bank = (*pos & GENMASK_ULL(43, 34)) >> 34;
3297 instance_bank = (*pos & GENMASK_ULL(53, 44)) >> 44;
Tom St Denis32977f92016-10-09 07:41:26 -04003298
3299 if (se_bank == 0x3FF)
3300 se_bank = 0xFFFFFFFF;
3301 if (sh_bank == 0x3FF)
3302 sh_bank = 0xFFFFFFFF;
3303 if (instance_bank == 0x3FF)
3304 instance_bank = 0xFFFFFFFF;
Tom St Denis566281592016-06-27 11:55:07 -04003305 use_bank = 1;
Tom St Denis566281592016-06-27 11:55:07 -04003306 } else {
3307 use_bank = 0;
3308 }
3309
Tom St Denis801a6aa9a62017-03-15 05:34:25 -04003310 *pos &= (1UL << 22) - 1;
Tom St Denisbd122672016-07-28 09:39:22 -04003311
Tom St Denis566281592016-06-27 11:55:07 -04003312 if (use_bank) {
Tom St Denis32977f92016-10-09 07:41:26 -04003313 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
3314 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
Tom St Denis566281592016-06-27 11:55:07 -04003315 return -EINVAL;
3316 mutex_lock(&adev->grbm_idx_mutex);
3317 amdgpu_gfx_select_se_sh(adev, se_bank,
3318 sh_bank, instance_bank);
3319 }
3320
Tom St Denisbd122672016-07-28 09:39:22 -04003321 if (pm_pg_lock)
3322 mutex_lock(&adev->pm.mutex);
3323
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003324 while (size) {
3325 uint32_t value;
3326
3327 if (*pos > adev->rmmio_size)
Tom St Denis566281592016-06-27 11:55:07 -04003328 goto end;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003329
3330 value = RREG32(*pos >> 2);
3331 r = put_user(value, (uint32_t *)buf);
Tom St Denis566281592016-06-27 11:55:07 -04003332 if (r) {
3333 result = r;
3334 goto end;
3335 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003336
3337 result += 4;
3338 buf += 4;
3339 *pos += 4;
3340 size -= 4;
3341 }
3342
Tom St Denis566281592016-06-27 11:55:07 -04003343end:
3344 if (use_bank) {
3345 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
3346 mutex_unlock(&adev->grbm_idx_mutex);
3347 }
3348
Tom St Denisbd122672016-07-28 09:39:22 -04003349 if (pm_pg_lock)
3350 mutex_unlock(&adev->pm.mutex);
3351
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003352 return result;
3353}
3354
3355static ssize_t amdgpu_debugfs_regs_write(struct file *f, const char __user *buf,
3356 size_t size, loff_t *pos)
3357{
Al Viro45063092016-12-04 18:24:56 -05003358 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003359 ssize_t result = 0;
3360 int r;
Tom St Denis394fdde2016-10-10 07:31:23 -04003361 bool pm_pg_lock, use_bank;
3362 unsigned instance_bank, sh_bank, se_bank;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003363
3364 if (size & 0x3 || *pos & 0x3)
3365 return -EINVAL;
3366
Tom St Denis394fdde2016-10-10 07:31:23 -04003367 /* are we reading registers for which a PG lock is necessary? */
3368 pm_pg_lock = (*pos >> 23) & 1;
3369
3370 if (*pos & (1ULL << 62)) {
Tom St Denis0b968652017-11-10 12:54:50 -05003371 se_bank = (*pos & GENMASK_ULL(33, 24)) >> 24;
3372 sh_bank = (*pos & GENMASK_ULL(43, 34)) >> 34;
3373 instance_bank = (*pos & GENMASK_ULL(53, 44)) >> 44;
Tom St Denis394fdde2016-10-10 07:31:23 -04003374
3375 if (se_bank == 0x3FF)
3376 se_bank = 0xFFFFFFFF;
3377 if (sh_bank == 0x3FF)
3378 sh_bank = 0xFFFFFFFF;
3379 if (instance_bank == 0x3FF)
3380 instance_bank = 0xFFFFFFFF;
3381 use_bank = 1;
3382 } else {
3383 use_bank = 0;
3384 }
3385
Tom St Denis801a6aa9a62017-03-15 05:34:25 -04003386 *pos &= (1UL << 22) - 1;
Tom St Denis394fdde2016-10-10 07:31:23 -04003387
3388 if (use_bank) {
3389 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
3390 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
3391 return -EINVAL;
3392 mutex_lock(&adev->grbm_idx_mutex);
3393 amdgpu_gfx_select_se_sh(adev, se_bank,
3394 sh_bank, instance_bank);
3395 }
3396
3397 if (pm_pg_lock)
3398 mutex_lock(&adev->pm.mutex);
3399
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003400 while (size) {
3401 uint32_t value;
3402
3403 if (*pos > adev->rmmio_size)
3404 return result;
3405
3406 r = get_user(value, (uint32_t *)buf);
3407 if (r)
3408 return r;
3409
3410 WREG32(*pos >> 2, value);
3411
3412 result += 4;
3413 buf += 4;
3414 *pos += 4;
3415 size -= 4;
3416 }
3417
Tom St Denis394fdde2016-10-10 07:31:23 -04003418 if (use_bank) {
3419 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
3420 mutex_unlock(&adev->grbm_idx_mutex);
3421 }
3422
3423 if (pm_pg_lock)
3424 mutex_unlock(&adev->pm.mutex);
3425
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003426 return result;
3427}
3428
Tom St Denisadcec282016-04-15 13:08:44 -04003429static ssize_t amdgpu_debugfs_regs_pcie_read(struct file *f, char __user *buf,
3430 size_t size, loff_t *pos)
3431{
Al Viro45063092016-12-04 18:24:56 -05003432 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003433 ssize_t result = 0;
3434 int r;
3435
3436 if (size & 0x3 || *pos & 0x3)
3437 return -EINVAL;
3438
3439 while (size) {
3440 uint32_t value;
3441
3442 value = RREG32_PCIE(*pos >> 2);
3443 r = put_user(value, (uint32_t *)buf);
3444 if (r)
3445 return r;
3446
3447 result += 4;
3448 buf += 4;
3449 *pos += 4;
3450 size -= 4;
3451 }
3452
3453 return result;
3454}
3455
3456static ssize_t amdgpu_debugfs_regs_pcie_write(struct file *f, const char __user *buf,
3457 size_t size, loff_t *pos)
3458{
Al Viro45063092016-12-04 18:24:56 -05003459 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003460 ssize_t result = 0;
3461 int r;
3462
3463 if (size & 0x3 || *pos & 0x3)
3464 return -EINVAL;
3465
3466 while (size) {
3467 uint32_t value;
3468
3469 r = get_user(value, (uint32_t *)buf);
3470 if (r)
3471 return r;
3472
3473 WREG32_PCIE(*pos >> 2, value);
3474
3475 result += 4;
3476 buf += 4;
3477 *pos += 4;
3478 size -= 4;
3479 }
3480
3481 return result;
3482}
3483
3484static ssize_t amdgpu_debugfs_regs_didt_read(struct file *f, char __user *buf,
3485 size_t size, loff_t *pos)
3486{
Al Viro45063092016-12-04 18:24:56 -05003487 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003488 ssize_t result = 0;
3489 int r;
3490
3491 if (size & 0x3 || *pos & 0x3)
3492 return -EINVAL;
3493
3494 while (size) {
3495 uint32_t value;
3496
3497 value = RREG32_DIDT(*pos >> 2);
3498 r = put_user(value, (uint32_t *)buf);
3499 if (r)
3500 return r;
3501
3502 result += 4;
3503 buf += 4;
3504 *pos += 4;
3505 size -= 4;
3506 }
3507
3508 return result;
3509}
3510
3511static ssize_t amdgpu_debugfs_regs_didt_write(struct file *f, const char __user *buf,
3512 size_t size, loff_t *pos)
3513{
Al Viro45063092016-12-04 18:24:56 -05003514 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003515 ssize_t result = 0;
3516 int r;
3517
3518 if (size & 0x3 || *pos & 0x3)
3519 return -EINVAL;
3520
3521 while (size) {
3522 uint32_t value;
3523
3524 r = get_user(value, (uint32_t *)buf);
3525 if (r)
3526 return r;
3527
3528 WREG32_DIDT(*pos >> 2, value);
3529
3530 result += 4;
3531 buf += 4;
3532 *pos += 4;
3533 size -= 4;
3534 }
3535
3536 return result;
3537}
3538
3539static ssize_t amdgpu_debugfs_regs_smc_read(struct file *f, char __user *buf,
3540 size_t size, loff_t *pos)
3541{
Al Viro45063092016-12-04 18:24:56 -05003542 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003543 ssize_t result = 0;
3544 int r;
3545
3546 if (size & 0x3 || *pos & 0x3)
3547 return -EINVAL;
3548
3549 while (size) {
3550 uint32_t value;
3551
Tom St Denis6fc0dea2016-08-29 08:39:29 -04003552 value = RREG32_SMC(*pos);
Tom St Denisadcec282016-04-15 13:08:44 -04003553 r = put_user(value, (uint32_t *)buf);
3554 if (r)
3555 return r;
3556
3557 result += 4;
3558 buf += 4;
3559 *pos += 4;
3560 size -= 4;
3561 }
3562
3563 return result;
3564}
3565
3566static ssize_t amdgpu_debugfs_regs_smc_write(struct file *f, const char __user *buf,
3567 size_t size, loff_t *pos)
3568{
Al Viro45063092016-12-04 18:24:56 -05003569 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003570 ssize_t result = 0;
3571 int r;
3572
3573 if (size & 0x3 || *pos & 0x3)
3574 return -EINVAL;
3575
3576 while (size) {
3577 uint32_t value;
3578
3579 r = get_user(value, (uint32_t *)buf);
3580 if (r)
3581 return r;
3582
Tom St Denis6fc0dea2016-08-29 08:39:29 -04003583 WREG32_SMC(*pos, value);
Tom St Denisadcec282016-04-15 13:08:44 -04003584
3585 result += 4;
3586 buf += 4;
3587 *pos += 4;
3588 size -= 4;
3589 }
3590
3591 return result;
3592}
3593
Tom St Denis1e051412016-06-27 09:57:18 -04003594static ssize_t amdgpu_debugfs_gca_config_read(struct file *f, char __user *buf,
3595 size_t size, loff_t *pos)
3596{
Al Viro45063092016-12-04 18:24:56 -05003597 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denis1e051412016-06-27 09:57:18 -04003598 ssize_t result = 0;
3599 int r;
3600 uint32_t *config, no_regs = 0;
3601
3602 if (size & 0x3 || *pos & 0x3)
3603 return -EINVAL;
3604
Markus Elfringecab7662016-09-18 17:00:52 +02003605 config = kmalloc_array(256, sizeof(*config), GFP_KERNEL);
Tom St Denis1e051412016-06-27 09:57:18 -04003606 if (!config)
3607 return -ENOMEM;
3608
3609 /* version, increment each time something is added */
Tom St Denis9a999352017-01-18 13:01:25 -05003610 config[no_regs++] = 3;
Tom St Denis1e051412016-06-27 09:57:18 -04003611 config[no_regs++] = adev->gfx.config.max_shader_engines;
3612 config[no_regs++] = adev->gfx.config.max_tile_pipes;
3613 config[no_regs++] = adev->gfx.config.max_cu_per_sh;
3614 config[no_regs++] = adev->gfx.config.max_sh_per_se;
3615 config[no_regs++] = adev->gfx.config.max_backends_per_se;
3616 config[no_regs++] = adev->gfx.config.max_texture_channel_caches;
3617 config[no_regs++] = adev->gfx.config.max_gprs;
3618 config[no_regs++] = adev->gfx.config.max_gs_threads;
3619 config[no_regs++] = adev->gfx.config.max_hw_contexts;
3620 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_frontend;
3621 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_backend;
3622 config[no_regs++] = adev->gfx.config.sc_hiz_tile_fifo_size;
3623 config[no_regs++] = adev->gfx.config.sc_earlyz_tile_fifo_size;
3624 config[no_regs++] = adev->gfx.config.num_tile_pipes;
3625 config[no_regs++] = adev->gfx.config.backend_enable_mask;
3626 config[no_regs++] = adev->gfx.config.mem_max_burst_length_bytes;
3627 config[no_regs++] = adev->gfx.config.mem_row_size_in_kb;
3628 config[no_regs++] = adev->gfx.config.shader_engine_tile_size;
3629 config[no_regs++] = adev->gfx.config.num_gpus;
3630 config[no_regs++] = adev->gfx.config.multi_gpu_tile_size;
3631 config[no_regs++] = adev->gfx.config.mc_arb_ramcfg;
3632 config[no_regs++] = adev->gfx.config.gb_addr_config;
3633 config[no_regs++] = adev->gfx.config.num_rbs;
3634
Tom St Denis89a8f302016-08-12 15:14:31 -04003635 /* rev==1 */
3636 config[no_regs++] = adev->rev_id;
3637 config[no_regs++] = adev->pg_flags;
3638 config[no_regs++] = adev->cg_flags;
3639
Tom St Denise9f11dc2016-08-17 12:00:51 -04003640 /* rev==2 */
3641 config[no_regs++] = adev->family;
3642 config[no_regs++] = adev->external_rev_id;
3643
Tom St Denis9a999352017-01-18 13:01:25 -05003644 /* rev==3 */
3645 config[no_regs++] = adev->pdev->device;
3646 config[no_regs++] = adev->pdev->revision;
3647 config[no_regs++] = adev->pdev->subsystem_device;
3648 config[no_regs++] = adev->pdev->subsystem_vendor;
3649
Tom St Denis1e051412016-06-27 09:57:18 -04003650 while (size && (*pos < no_regs * 4)) {
3651 uint32_t value;
3652
3653 value = config[*pos >> 2];
3654 r = put_user(value, (uint32_t *)buf);
3655 if (r) {
3656 kfree(config);
3657 return r;
3658 }
3659
3660 result += 4;
3661 buf += 4;
3662 *pos += 4;
3663 size -= 4;
3664 }
3665
3666 kfree(config);
3667 return result;
3668}
3669
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003670static ssize_t amdgpu_debugfs_sensor_read(struct file *f, char __user *buf,
3671 size_t size, loff_t *pos)
3672{
Al Viro45063092016-12-04 18:24:56 -05003673 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003674 int idx, x, outsize, r, valuesize;
3675 uint32_t values[16];
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003676
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003677 if (size & 3 || *pos & 0x3)
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003678 return -EINVAL;
3679
Samuel Pitoiset3cbc6142017-02-15 19:32:29 +01003680 if (amdgpu_dpm == 0)
3681 return -EINVAL;
3682
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003683 /* convert offset to sensor number */
3684 idx = *pos >> 2;
3685
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003686 valuesize = sizeof(values);
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003687 if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->read_sensor)
Rex Zhucd4d7462017-09-06 18:43:52 +08003688 r = amdgpu_dpm_read_sensor(adev, idx, &values[0], &valuesize);
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003689 else
3690 return -EINVAL;
3691
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003692 if (size > valuesize)
3693 return -EINVAL;
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003694
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003695 outsize = 0;
3696 x = 0;
3697 if (!r) {
3698 while (size) {
3699 r = put_user(values[x++], (int32_t *)buf);
3700 buf += 4;
3701 size -= 4;
3702 outsize += 4;
3703 }
3704 }
3705
3706 return !r ? outsize : r;
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003707}
Tom St Denis1e051412016-06-27 09:57:18 -04003708
Tom St Denis273d7aa2016-10-11 14:48:55 -04003709static ssize_t amdgpu_debugfs_wave_read(struct file *f, char __user *buf,
3710 size_t size, loff_t *pos)
3711{
3712 struct amdgpu_device *adev = f->f_inode->i_private;
3713 int r, x;
3714 ssize_t result=0;
Tom St Denis472259f2016-10-14 09:49:09 -04003715 uint32_t offset, se, sh, cu, wave, simd, data[32];
Tom St Denis273d7aa2016-10-11 14:48:55 -04003716
3717 if (size & 3 || *pos & 3)
3718 return -EINVAL;
3719
3720 /* decode offset */
Tom St Denis0b968652017-11-10 12:54:50 -05003721 offset = (*pos & GENMASK_ULL(6, 0));
3722 se = (*pos & GENMASK_ULL(14, 7)) >> 7;
3723 sh = (*pos & GENMASK_ULL(22, 15)) >> 15;
3724 cu = (*pos & GENMASK_ULL(30, 23)) >> 23;
3725 wave = (*pos & GENMASK_ULL(36, 31)) >> 31;
3726 simd = (*pos & GENMASK_ULL(44, 37)) >> 37;
Tom St Denis273d7aa2016-10-11 14:48:55 -04003727
3728 /* switch to the specific se/sh/cu */
3729 mutex_lock(&adev->grbm_idx_mutex);
3730 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
3731
3732 x = 0;
Tom St Denis472259f2016-10-14 09:49:09 -04003733 if (adev->gfx.funcs->read_wave_data)
3734 adev->gfx.funcs->read_wave_data(adev, simd, wave, data, &x);
Tom St Denis273d7aa2016-10-11 14:48:55 -04003735
3736 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
3737 mutex_unlock(&adev->grbm_idx_mutex);
3738
Tom St Denis5ecfb3b2016-10-13 12:15:03 -04003739 if (!x)
3740 return -EINVAL;
3741
Tom St Denis472259f2016-10-14 09:49:09 -04003742 while (size && (offset < x * 4)) {
Tom St Denis273d7aa2016-10-11 14:48:55 -04003743 uint32_t value;
3744
Tom St Denis472259f2016-10-14 09:49:09 -04003745 value = data[offset >> 2];
Tom St Denis273d7aa2016-10-11 14:48:55 -04003746 r = put_user(value, (uint32_t *)buf);
3747 if (r)
3748 return r;
3749
3750 result += 4;
3751 buf += 4;
Tom St Denis472259f2016-10-14 09:49:09 -04003752 offset += 4;
Tom St Denis273d7aa2016-10-11 14:48:55 -04003753 size -= 4;
3754 }
3755
3756 return result;
3757}
3758
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003759static ssize_t amdgpu_debugfs_gpr_read(struct file *f, char __user *buf,
3760 size_t size, loff_t *pos)
3761{
3762 struct amdgpu_device *adev = f->f_inode->i_private;
3763 int r;
3764 ssize_t result = 0;
3765 uint32_t offset, se, sh, cu, wave, simd, thread, bank, *data;
3766
3767 if (size & 3 || *pos & 3)
3768 return -EINVAL;
3769
3770 /* decode offset */
Tom St Denis0b968652017-11-10 12:54:50 -05003771 offset = *pos & GENMASK_ULL(11, 0);
3772 se = (*pos & GENMASK_ULL(19, 12)) >> 12;
3773 sh = (*pos & GENMASK_ULL(27, 20)) >> 20;
3774 cu = (*pos & GENMASK_ULL(35, 28)) >> 28;
3775 wave = (*pos & GENMASK_ULL(43, 36)) >> 36;
3776 simd = (*pos & GENMASK_ULL(51, 44)) >> 44;
3777 thread = (*pos & GENMASK_ULL(59, 52)) >> 52;
3778 bank = (*pos & GENMASK_ULL(61, 60)) >> 60;
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003779
3780 data = kmalloc_array(1024, sizeof(*data), GFP_KERNEL);
3781 if (!data)
3782 return -ENOMEM;
3783
3784 /* switch to the specific se/sh/cu */
3785 mutex_lock(&adev->grbm_idx_mutex);
3786 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
3787
3788 if (bank == 0) {
3789 if (adev->gfx.funcs->read_wave_vgprs)
3790 adev->gfx.funcs->read_wave_vgprs(adev, simd, wave, thread, offset, size>>2, data);
3791 } else {
3792 if (adev->gfx.funcs->read_wave_sgprs)
3793 adev->gfx.funcs->read_wave_sgprs(adev, simd, wave, offset, size>>2, data);
3794 }
3795
3796 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
3797 mutex_unlock(&adev->grbm_idx_mutex);
3798
3799 while (size) {
3800 uint32_t value;
3801
3802 value = data[offset++];
3803 r = put_user(value, (uint32_t *)buf);
3804 if (r) {
3805 result = r;
3806 goto err;
3807 }
3808
3809 result += 4;
3810 buf += 4;
3811 size -= 4;
3812 }
3813
3814err:
3815 kfree(data);
3816 return result;
3817}
3818
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003819static const struct file_operations amdgpu_debugfs_regs_fops = {
3820 .owner = THIS_MODULE,
3821 .read = amdgpu_debugfs_regs_read,
3822 .write = amdgpu_debugfs_regs_write,
3823 .llseek = default_llseek
3824};
Tom St Denisadcec282016-04-15 13:08:44 -04003825static const struct file_operations amdgpu_debugfs_regs_didt_fops = {
3826 .owner = THIS_MODULE,
3827 .read = amdgpu_debugfs_regs_didt_read,
3828 .write = amdgpu_debugfs_regs_didt_write,
3829 .llseek = default_llseek
3830};
3831static const struct file_operations amdgpu_debugfs_regs_pcie_fops = {
3832 .owner = THIS_MODULE,
3833 .read = amdgpu_debugfs_regs_pcie_read,
3834 .write = amdgpu_debugfs_regs_pcie_write,
3835 .llseek = default_llseek
3836};
3837static const struct file_operations amdgpu_debugfs_regs_smc_fops = {
3838 .owner = THIS_MODULE,
3839 .read = amdgpu_debugfs_regs_smc_read,
3840 .write = amdgpu_debugfs_regs_smc_write,
3841 .llseek = default_llseek
3842};
3843
Tom St Denis1e051412016-06-27 09:57:18 -04003844static const struct file_operations amdgpu_debugfs_gca_config_fops = {
3845 .owner = THIS_MODULE,
3846 .read = amdgpu_debugfs_gca_config_read,
3847 .llseek = default_llseek
3848};
3849
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003850static const struct file_operations amdgpu_debugfs_sensors_fops = {
3851 .owner = THIS_MODULE,
3852 .read = amdgpu_debugfs_sensor_read,
3853 .llseek = default_llseek
3854};
3855
Tom St Denis273d7aa2016-10-11 14:48:55 -04003856static const struct file_operations amdgpu_debugfs_wave_fops = {
3857 .owner = THIS_MODULE,
3858 .read = amdgpu_debugfs_wave_read,
3859 .llseek = default_llseek
3860};
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003861static const struct file_operations amdgpu_debugfs_gpr_fops = {
3862 .owner = THIS_MODULE,
3863 .read = amdgpu_debugfs_gpr_read,
3864 .llseek = default_llseek
3865};
Tom St Denis273d7aa2016-10-11 14:48:55 -04003866
Tom St Denisadcec282016-04-15 13:08:44 -04003867static const struct file_operations *debugfs_regs[] = {
3868 &amdgpu_debugfs_regs_fops,
3869 &amdgpu_debugfs_regs_didt_fops,
3870 &amdgpu_debugfs_regs_pcie_fops,
3871 &amdgpu_debugfs_regs_smc_fops,
Tom St Denis1e051412016-06-27 09:57:18 -04003872 &amdgpu_debugfs_gca_config_fops,
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003873 &amdgpu_debugfs_sensors_fops,
Tom St Denis273d7aa2016-10-11 14:48:55 -04003874 &amdgpu_debugfs_wave_fops,
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003875 &amdgpu_debugfs_gpr_fops,
Tom St Denisadcec282016-04-15 13:08:44 -04003876};
3877
3878static const char *debugfs_regs_names[] = {
3879 "amdgpu_regs",
3880 "amdgpu_regs_didt",
3881 "amdgpu_regs_pcie",
3882 "amdgpu_regs_smc",
Tom St Denis1e051412016-06-27 09:57:18 -04003883 "amdgpu_gca_config",
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003884 "amdgpu_sensors",
Tom St Denis273d7aa2016-10-11 14:48:55 -04003885 "amdgpu_wave",
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003886 "amdgpu_gpr",
Tom St Denisadcec282016-04-15 13:08:44 -04003887};
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003888
3889static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
3890{
3891 struct drm_minor *minor = adev->ddev->primary;
3892 struct dentry *ent, *root = minor->debugfs_root;
Tom St Denisadcec282016-04-15 13:08:44 -04003893 unsigned i, j;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003894
Tom St Denisadcec282016-04-15 13:08:44 -04003895 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3896 ent = debugfs_create_file(debugfs_regs_names[i],
3897 S_IFREG | S_IRUGO, root,
3898 adev, debugfs_regs[i]);
3899 if (IS_ERR(ent)) {
3900 for (j = 0; j < i; j++) {
3901 debugfs_remove(adev->debugfs_regs[i]);
3902 adev->debugfs_regs[i] = NULL;
3903 }
3904 return PTR_ERR(ent);
3905 }
3906
3907 if (!i)
3908 i_size_write(ent->d_inode, adev->rmmio_size);
3909 adev->debugfs_regs[i] = ent;
3910 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003911
3912 return 0;
3913}
3914
3915static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev)
3916{
Tom St Denisadcec282016-04-15 13:08:44 -04003917 unsigned i;
3918
3919 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3920 if (adev->debugfs_regs[i]) {
3921 debugfs_remove(adev->debugfs_regs[i]);
3922 adev->debugfs_regs[i] = NULL;
3923 }
3924 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003925}
3926
Huang Rui4f0955f2017-05-10 23:04:06 +08003927static int amdgpu_debugfs_test_ib(struct seq_file *m, void *data)
3928{
3929 struct drm_info_node *node = (struct drm_info_node *) m->private;
3930 struct drm_device *dev = node->minor->dev;
3931 struct amdgpu_device *adev = dev->dev_private;
3932 int r = 0, i;
3933
3934 /* hold on the scheduler */
3935 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
3936 struct amdgpu_ring *ring = adev->rings[i];
3937
3938 if (!ring || !ring->sched.thread)
3939 continue;
3940 kthread_park(ring->sched.thread);
3941 }
3942
3943 seq_printf(m, "run ib test:\n");
3944 r = amdgpu_ib_ring_tests(adev);
3945 if (r)
3946 seq_printf(m, "ib ring tests failed (%d).\n", r);
3947 else
3948 seq_printf(m, "ib ring tests passed.\n");
3949
3950 /* go on the scheduler */
3951 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
3952 struct amdgpu_ring *ring = adev->rings[i];
3953
3954 if (!ring || !ring->sched.thread)
3955 continue;
3956 kthread_unpark(ring->sched.thread);
3957 }
3958
3959 return 0;
3960}
3961
Kent Russelldb95e212017-08-22 12:31:43 -04003962static int amdgpu_debugfs_get_vbios_dump(struct seq_file *m, void *data)
3963{
3964 struct drm_info_node *node = (struct drm_info_node *) m->private;
3965 struct drm_device *dev = node->minor->dev;
3966 struct amdgpu_device *adev = dev->dev_private;
3967
3968 seq_write(m, adev->bios, adev->bios_size);
3969 return 0;
3970}
3971
Christian König79588d22017-12-06 16:24:49 +01003972static int amdgpu_debugfs_evict_vram(struct seq_file *m, void *data)
3973{
3974 struct drm_info_node *node = (struct drm_info_node *)m->private;
3975 struct drm_device *dev = node->minor->dev;
3976 struct amdgpu_device *adev = dev->dev_private;
3977
3978 seq_printf(m, "(%d)\n", amdgpu_bo_evict_vram(adev));
3979 return 0;
3980}
3981
Christian König763efb62017-12-06 15:44:51 +01003982static const struct drm_info_list amdgpu_debugfs_list[] = {
3983 {"amdgpu_vbios", amdgpu_debugfs_get_vbios_dump},
Christian König79588d22017-12-06 16:24:49 +01003984 {"amdgpu_test_ib", &amdgpu_debugfs_test_ib},
3985 {"amdgpu_evict_vram", &amdgpu_debugfs_evict_vram}
Kent Russelldb95e212017-08-22 12:31:43 -04003986};
3987
Christian König763efb62017-12-06 15:44:51 +01003988static int amdgpu_debugfs_init(struct amdgpu_device *adev)
Kent Russelldb95e212017-08-22 12:31:43 -04003989{
Christian König763efb62017-12-06 15:44:51 +01003990 return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_list,
3991 ARRAY_SIZE(amdgpu_debugfs_list));
Kent Russelldb95e212017-08-22 12:31:43 -04003992}
Christian König763efb62017-12-06 15:44:51 +01003993
Alexander Kuleshov7cebc722015-06-27 13:16:05 +06003994#else
Christian König763efb62017-12-06 15:44:51 +01003995static int amdgpu_debugfs_init(struct amdgpu_device *adev)
Huang Rui4f0955f2017-05-10 23:04:06 +08003996{
3997 return 0;
3998}
Alexander Kuleshov7cebc722015-06-27 13:16:05 +06003999static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
4000{
4001 return 0;
4002}
4003static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev) { }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04004004#endif