blob: 79869827985f76cfc9d9ebd08896b973b8dc5930 [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 }
Andrey Grodzovsky88546952017-12-13 14:36:53 -05001232
1233 if (amdgpu_lockup_timeout == 0) {
1234 dev_warn(adev->dev, "lockup_timeout msut be > 0, adjusting to 10000\n");
1235 amdgpu_lockup_timeout = 10000;
1236 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001237}
1238
1239/**
1240 * amdgpu_switcheroo_set_state - set switcheroo state
1241 *
1242 * @pdev: pci dev pointer
Lukas Wunner16944672015-09-05 11:17:35 +02001243 * @state: vga_switcheroo state
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001244 *
1245 * Callback for the switcheroo driver. Suspends or resumes the
1246 * the asics before or after it is powered up using ACPI methods.
1247 */
1248static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1249{
1250 struct drm_device *dev = pci_get_drvdata(pdev);
1251
1252 if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
1253 return;
1254
1255 if (state == VGA_SWITCHEROO_ON) {
Joe Perches7ca85292017-02-28 04:55:52 -08001256 pr_info("amdgpu: switched on\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001257 /* don't suspend or resume card normally */
1258 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1259
Alex Deucher810ddc32016-08-23 13:25:49 -04001260 amdgpu_device_resume(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001261
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001262 dev->switch_power_state = DRM_SWITCH_POWER_ON;
1263 drm_kms_helper_poll_enable(dev);
1264 } else {
Joe Perches7ca85292017-02-28 04:55:52 -08001265 pr_info("amdgpu: switched off\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001266 drm_kms_helper_poll_disable(dev);
1267 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Alex Deucher810ddc32016-08-23 13:25:49 -04001268 amdgpu_device_suspend(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001269 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1270 }
1271}
1272
1273/**
1274 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1275 *
1276 * @pdev: pci dev pointer
1277 *
1278 * Callback for the switcheroo driver. Check of the switcheroo
1279 * state can be changed.
1280 * Returns true if the state can be changed, false if not.
1281 */
1282static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1283{
1284 struct drm_device *dev = pci_get_drvdata(pdev);
1285
1286 /*
1287 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1288 * locking inversion with the driver load path. And the access here is
1289 * completely racy anyway. So don't bother with locking for now.
1290 */
1291 return dev->open_count == 0;
1292}
1293
1294static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1295 .set_gpu_state = amdgpu_switcheroo_set_state,
1296 .reprobe = NULL,
1297 .can_switch = amdgpu_switcheroo_can_switch,
1298};
1299
1300int amdgpu_set_clockgating_state(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001301 enum amd_ip_block_type block_type,
1302 enum amd_clockgating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001303{
1304 int i, r = 0;
1305
1306 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001307 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001308 continue;
Rex Zhuc7228652017-02-22 15:33:46 +08001309 if (adev->ip_blocks[i].version->type != block_type)
1310 continue;
1311 if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
1312 continue;
1313 r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
1314 (void *)adev, state);
1315 if (r)
1316 DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
1317 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001318 }
1319 return r;
1320}
1321
1322int amdgpu_set_powergating_state(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001323 enum amd_ip_block_type block_type,
1324 enum amd_powergating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001325{
1326 int i, r = 0;
1327
1328 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001329 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001330 continue;
Rex Zhuc7228652017-02-22 15:33:46 +08001331 if (adev->ip_blocks[i].version->type != block_type)
1332 continue;
1333 if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
1334 continue;
1335 r = adev->ip_blocks[i].version->funcs->set_powergating_state(
1336 (void *)adev, state);
1337 if (r)
1338 DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
1339 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001340 }
1341 return r;
1342}
1343
Huang Rui6cb2d4e2017-01-05 18:44:41 +08001344void amdgpu_get_clockgating_state(struct amdgpu_device *adev, u32 *flags)
1345{
1346 int i;
1347
1348 for (i = 0; i < adev->num_ip_blocks; i++) {
1349 if (!adev->ip_blocks[i].status.valid)
1350 continue;
1351 if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
1352 adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
1353 }
1354}
1355
Alex Deucher5dbbb602016-06-23 11:41:04 -04001356int amdgpu_wait_for_idle(struct amdgpu_device *adev,
1357 enum amd_ip_block_type block_type)
1358{
1359 int i, r;
1360
1361 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001362 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001363 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001364 if (adev->ip_blocks[i].version->type == block_type) {
1365 r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -04001366 if (r)
1367 return r;
1368 break;
1369 }
1370 }
1371 return 0;
1372
1373}
1374
1375bool amdgpu_is_idle(struct amdgpu_device *adev,
1376 enum amd_ip_block_type block_type)
1377{
1378 int i;
1379
1380 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001381 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001382 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001383 if (adev->ip_blocks[i].version->type == block_type)
1384 return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -04001385 }
1386 return true;
1387
1388}
1389
Alex Deuchera1255102016-10-13 17:41:13 -04001390struct amdgpu_ip_block * amdgpu_get_ip_block(struct amdgpu_device *adev,
1391 enum amd_ip_block_type type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001392{
1393 int i;
1394
1395 for (i = 0; i < adev->num_ip_blocks; i++)
Alex Deuchera1255102016-10-13 17:41:13 -04001396 if (adev->ip_blocks[i].version->type == type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001397 return &adev->ip_blocks[i];
1398
1399 return NULL;
1400}
1401
1402/**
1403 * amdgpu_ip_block_version_cmp
1404 *
1405 * @adev: amdgpu_device pointer
yanyang15fc3aee2015-05-22 14:39:35 -04001406 * @type: enum amd_ip_block_type
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001407 * @major: major version
1408 * @minor: minor version
1409 *
1410 * return 0 if equal or greater
1411 * return 1 if smaller or the ip_block doesn't exist
1412 */
1413int amdgpu_ip_block_version_cmp(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001414 enum amd_ip_block_type type,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001415 u32 major, u32 minor)
1416{
Alex Deuchera1255102016-10-13 17:41:13 -04001417 struct amdgpu_ip_block *ip_block = amdgpu_get_ip_block(adev, type);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001418
Alex Deuchera1255102016-10-13 17:41:13 -04001419 if (ip_block && ((ip_block->version->major > major) ||
1420 ((ip_block->version->major == major) &&
1421 (ip_block->version->minor >= minor))))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001422 return 0;
1423
1424 return 1;
1425}
1426
Alex Deuchera1255102016-10-13 17:41:13 -04001427/**
1428 * amdgpu_ip_block_add
1429 *
1430 * @adev: amdgpu_device pointer
1431 * @ip_block_version: pointer to the IP to add
1432 *
1433 * Adds the IP block driver information to the collection of IPs
1434 * on the asic.
1435 */
1436int amdgpu_ip_block_add(struct amdgpu_device *adev,
1437 const struct amdgpu_ip_block_version *ip_block_version)
1438{
1439 if (!ip_block_version)
1440 return -EINVAL;
1441
Huang Ruia0bae352017-05-03 09:52:06 +08001442 DRM_DEBUG("add ip block number %d <%s>\n", adev->num_ip_blocks,
1443 ip_block_version->funcs->name);
1444
Alex Deuchera1255102016-10-13 17:41:13 -04001445 adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
1446
1447 return 0;
1448}
1449
Alex Deucher483ef982016-09-30 12:43:04 -04001450static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
Emily Deng9accf2f2016-08-10 16:01:25 +08001451{
1452 adev->enable_virtual_display = false;
1453
1454 if (amdgpu_virtual_display) {
1455 struct drm_device *ddev = adev->ddev;
1456 const char *pci_address_name = pci_name(ddev->pdev);
Emily Deng0f663562016-09-30 13:02:18 -04001457 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
Emily Deng9accf2f2016-08-10 16:01:25 +08001458
1459 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1460 pciaddstr_tmp = pciaddstr;
Emily Deng0f663562016-09-30 13:02:18 -04001461 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1462 pciaddname = strsep(&pciaddname_tmp, ",");
Yintian Tao967de2a2017-01-22 15:16:51 +08001463 if (!strcmp("all", pciaddname)
1464 || !strcmp(pci_address_name, pciaddname)) {
Emily Deng0f663562016-09-30 13:02:18 -04001465 long num_crtc;
1466 int res = -1;
1467
Emily Deng9accf2f2016-08-10 16:01:25 +08001468 adev->enable_virtual_display = true;
Emily Deng0f663562016-09-30 13:02:18 -04001469
1470 if (pciaddname_tmp)
1471 res = kstrtol(pciaddname_tmp, 10,
1472 &num_crtc);
1473
1474 if (!res) {
1475 if (num_crtc < 1)
1476 num_crtc = 1;
1477 if (num_crtc > 6)
1478 num_crtc = 6;
1479 adev->mode_info.num_crtc = num_crtc;
1480 } else {
1481 adev->mode_info.num_crtc = 1;
1482 }
Emily Deng9accf2f2016-08-10 16:01:25 +08001483 break;
1484 }
1485 }
1486
Emily Deng0f663562016-09-30 13:02:18 -04001487 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
1488 amdgpu_virtual_display, pci_address_name,
1489 adev->enable_virtual_display, adev->mode_info.num_crtc);
Emily Deng9accf2f2016-08-10 16:01:25 +08001490
1491 kfree(pciaddstr);
1492 }
1493}
1494
Alex Deuchere2a75f82017-04-27 16:58:01 -04001495static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev)
1496{
Alex Deuchere2a75f82017-04-27 16:58:01 -04001497 const char *chip_name;
1498 char fw_name[30];
1499 int err;
1500 const struct gpu_info_firmware_header_v1_0 *hdr;
1501
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001502 adev->firmware.gpu_info_fw = NULL;
1503
Alex Deuchere2a75f82017-04-27 16:58:01 -04001504 switch (adev->asic_type) {
1505 case CHIP_TOPAZ:
1506 case CHIP_TONGA:
1507 case CHIP_FIJI:
1508 case CHIP_POLARIS11:
1509 case CHIP_POLARIS10:
1510 case CHIP_POLARIS12:
1511 case CHIP_CARRIZO:
1512 case CHIP_STONEY:
1513#ifdef CONFIG_DRM_AMDGPU_SI
1514 case CHIP_VERDE:
1515 case CHIP_TAHITI:
1516 case CHIP_PITCAIRN:
1517 case CHIP_OLAND:
1518 case CHIP_HAINAN:
1519#endif
1520#ifdef CONFIG_DRM_AMDGPU_CIK
1521 case CHIP_BONAIRE:
1522 case CHIP_HAWAII:
1523 case CHIP_KAVERI:
1524 case CHIP_KABINI:
1525 case CHIP_MULLINS:
1526#endif
1527 default:
1528 return 0;
1529 case CHIP_VEGA10:
1530 chip_name = "vega10";
1531 break;
Alex Deucher2d2e5e72017-05-09 12:27:35 -04001532 case CHIP_RAVEN:
1533 chip_name = "raven";
1534 break;
Alex Deuchere2a75f82017-04-27 16:58:01 -04001535 }
1536
1537 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name);
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001538 err = request_firmware(&adev->firmware.gpu_info_fw, fw_name, adev->dev);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001539 if (err) {
1540 dev_err(adev->dev,
1541 "Failed to load gpu_info firmware \"%s\"\n",
1542 fw_name);
1543 goto out;
1544 }
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001545 err = amdgpu_ucode_validate(adev->firmware.gpu_info_fw);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001546 if (err) {
1547 dev_err(adev->dev,
1548 "Failed to validate gpu_info firmware \"%s\"\n",
1549 fw_name);
1550 goto out;
1551 }
1552
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001553 hdr = (const struct gpu_info_firmware_header_v1_0 *)adev->firmware.gpu_info_fw->data;
Alex Deuchere2a75f82017-04-27 16:58:01 -04001554 amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
1555
1556 switch (hdr->version_major) {
1557 case 1:
1558 {
1559 const struct gpu_info_firmware_v1_0 *gpu_info_fw =
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001560 (const struct gpu_info_firmware_v1_0 *)(adev->firmware.gpu_info_fw->data +
Alex Deuchere2a75f82017-04-27 16:58:01 -04001561 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1562
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001563 adev->gfx.config.max_shader_engines = le32_to_cpu(gpu_info_fw->gc_num_se);
1564 adev->gfx.config.max_cu_per_sh = le32_to_cpu(gpu_info_fw->gc_num_cu_per_sh);
1565 adev->gfx.config.max_sh_per_se = le32_to_cpu(gpu_info_fw->gc_num_sh_per_se);
1566 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 -04001567 adev->gfx.config.max_texture_channel_caches =
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001568 le32_to_cpu(gpu_info_fw->gc_num_tccs);
1569 adev->gfx.config.max_gprs = le32_to_cpu(gpu_info_fw->gc_num_gprs);
1570 adev->gfx.config.max_gs_threads = le32_to_cpu(gpu_info_fw->gc_num_max_gs_thds);
1571 adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gpu_info_fw->gc_gs_table_depth);
1572 adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gpu_info_fw->gc_gsprim_buff_depth);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001573 adev->gfx.config.double_offchip_lds_buf =
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001574 le32_to_cpu(gpu_info_fw->gc_double_offchip_lds_buffer);
1575 adev->gfx.cu_info.wave_front_size = le32_to_cpu(gpu_info_fw->gc_wave_size);
Hawking Zhang51fd0372017-06-09 22:30:52 +08001576 adev->gfx.cu_info.max_waves_per_simd =
1577 le32_to_cpu(gpu_info_fw->gc_max_waves_per_simd);
1578 adev->gfx.cu_info.max_scratch_slots_per_cu =
1579 le32_to_cpu(gpu_info_fw->gc_max_scratch_slots_per_cu);
1580 adev->gfx.cu_info.lds_size = le32_to_cpu(gpu_info_fw->gc_lds_size);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001581 break;
1582 }
1583 default:
1584 dev_err(adev->dev,
1585 "Unsupported gpu_info table %d\n", hdr->header.ucode_version);
1586 err = -EINVAL;
1587 goto out;
1588 }
1589out:
Alex Deuchere2a75f82017-04-27 16:58:01 -04001590 return err;
1591}
1592
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001593static int amdgpu_early_init(struct amdgpu_device *adev)
1594{
Alex Deucheraaa36a92015-04-20 17:31:14 -04001595 int i, r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001596
Alex Deucher483ef982016-09-30 12:43:04 -04001597 amdgpu_device_enable_virtual_display(adev);
Emily Denga6be7572016-08-08 11:37:50 +08001598
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001599 switch (adev->asic_type) {
Alex Deucheraaa36a92015-04-20 17:31:14 -04001600 case CHIP_TOPAZ:
1601 case CHIP_TONGA:
David Zhang48299f92015-07-08 01:05:16 +08001602 case CHIP_FIJI:
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001603 case CHIP_POLARIS11:
1604 case CHIP_POLARIS10:
Junwei Zhangc4642a42016-12-14 15:32:28 -05001605 case CHIP_POLARIS12:
Alex Deucheraaa36a92015-04-20 17:31:14 -04001606 case CHIP_CARRIZO:
Samuel Li39bb0c92015-10-08 16:31:43 -04001607 case CHIP_STONEY:
1608 if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
Alex Deucheraaa36a92015-04-20 17:31:14 -04001609 adev->family = AMDGPU_FAMILY_CZ;
1610 else
1611 adev->family = AMDGPU_FAMILY_VI;
1612
1613 r = vi_set_ip_blocks(adev);
1614 if (r)
1615 return r;
1616 break;
Ken Wang33f34802016-01-21 17:29:41 +08001617#ifdef CONFIG_DRM_AMDGPU_SI
1618 case CHIP_VERDE:
1619 case CHIP_TAHITI:
1620 case CHIP_PITCAIRN:
1621 case CHIP_OLAND:
1622 case CHIP_HAINAN:
Ken Wang295d0da2016-05-24 21:02:53 +08001623 adev->family = AMDGPU_FAMILY_SI;
Ken Wang33f34802016-01-21 17:29:41 +08001624 r = si_set_ip_blocks(adev);
1625 if (r)
1626 return r;
1627 break;
1628#endif
Alex Deuchera2e73f52015-04-20 17:09:27 -04001629#ifdef CONFIG_DRM_AMDGPU_CIK
1630 case CHIP_BONAIRE:
1631 case CHIP_HAWAII:
1632 case CHIP_KAVERI:
1633 case CHIP_KABINI:
1634 case CHIP_MULLINS:
1635 if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
1636 adev->family = AMDGPU_FAMILY_CI;
1637 else
1638 adev->family = AMDGPU_FAMILY_KV;
1639
1640 r = cik_set_ip_blocks(adev);
1641 if (r)
1642 return r;
1643 break;
1644#endif
Chunming Zhou2ca8a5d2016-12-07 17:31:19 +08001645 case CHIP_VEGA10:
1646 case CHIP_RAVEN:
1647 if (adev->asic_type == CHIP_RAVEN)
1648 adev->family = AMDGPU_FAMILY_RV;
1649 else
1650 adev->family = AMDGPU_FAMILY_AI;
Ken Wang460826e2017-03-06 14:53:16 -05001651
1652 r = soc15_set_ip_blocks(adev);
1653 if (r)
1654 return r;
1655 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001656 default:
1657 /* FIXME: not supported yet */
1658 return -EINVAL;
1659 }
1660
Alex Deuchere2a75f82017-04-27 16:58:01 -04001661 r = amdgpu_device_parse_gpu_info_fw(adev);
1662 if (r)
1663 return r;
1664
pding18847342017-11-06 10:21:26 +08001665 amdgpu_amdkfd_device_probe(adev);
1666
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001667 if (amdgpu_sriov_vf(adev)) {
1668 r = amdgpu_virt_request_full_gpu(adev, true);
1669 if (r)
pding5ffa61c2017-10-30 14:07:24 +08001670 return -EAGAIN;
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001671 }
1672
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001673 for (i = 0; i < adev->num_ip_blocks; i++) {
1674 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
Huang Ruied8cf002017-05-03 09:40:17 +08001675 DRM_ERROR("disabled ip block: %d <%s>\n",
1676 i, adev->ip_blocks[i].version->funcs->name);
Alex Deuchera1255102016-10-13 17:41:13 -04001677 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001678 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001679 if (adev->ip_blocks[i].version->funcs->early_init) {
1680 r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001681 if (r == -ENOENT) {
Alex Deuchera1255102016-10-13 17:41:13 -04001682 adev->ip_blocks[i].status.valid = false;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001683 } else if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001684 DRM_ERROR("early_init of IP block <%s> failed %d\n",
1685 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001686 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001687 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001688 adev->ip_blocks[i].status.valid = true;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001689 }
Alex Deucher974e6b62015-07-10 13:59:44 -04001690 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001691 adev->ip_blocks[i].status.valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001692 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001693 }
1694 }
1695
Nicolai Hähnle395d1fb2016-06-02 12:32:07 +02001696 adev->cg_flags &= amdgpu_cg_mask;
1697 adev->pg_flags &= amdgpu_pg_mask;
1698
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001699 return 0;
1700}
1701
1702static int amdgpu_init(struct amdgpu_device *adev)
1703{
1704 int i, r;
1705
1706 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001707 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001708 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001709 r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001710 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001711 DRM_ERROR("sw_init of IP block <%s> failed %d\n",
1712 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001713 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001714 }
Alex Deuchera1255102016-10-13 17:41:13 -04001715 adev->ip_blocks[i].status.sw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001716 /* need to do gmc hw init early so we can allocate gpu mem */
Alex Deuchera1255102016-10-13 17:41:13 -04001717 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001718 r = amdgpu_vram_scratch_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001719 if (r) {
1720 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001721 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001722 }
Alex Deuchera1255102016-10-13 17:41:13 -04001723 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001724 if (r) {
1725 DRM_ERROR("hw_init %d failed %d\n", i, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001726 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001727 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001728 r = amdgpu_wb_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001729 if (r) {
1730 DRM_ERROR("amdgpu_wb_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001731 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001732 }
Alex Deuchera1255102016-10-13 17:41:13 -04001733 adev->ip_blocks[i].status.hw = true;
Monk Liu24936642017-01-09 15:54:32 +08001734
1735 /* right after GMC hw init, we create CSA */
1736 if (amdgpu_sriov_vf(adev)) {
1737 r = amdgpu_allocate_static_csa(adev);
1738 if (r) {
1739 DRM_ERROR("allocate CSA failed %d\n", r);
1740 return r;
1741 }
1742 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001743 }
1744 }
1745
1746 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001747 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001748 continue;
1749 /* gmc hw init is done early */
Alex Deuchera1255102016-10-13 17:41:13 -04001750 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001751 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001752 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001753 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001754 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
1755 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001756 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001757 }
Alex Deuchera1255102016-10-13 17:41:13 -04001758 adev->ip_blocks[i].status.hw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001759 }
1760
pding18847342017-11-06 10:21:26 +08001761 amdgpu_amdkfd_device_init(adev);
pdingc6332b92017-11-06 11:21:55 +08001762
1763 if (amdgpu_sriov_vf(adev))
1764 amdgpu_virt_release_full_gpu(adev, true);
1765
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001766 return 0;
1767}
1768
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08001769static void amdgpu_fill_reset_magic(struct amdgpu_device *adev)
1770{
1771 memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM);
1772}
1773
1774static bool amdgpu_check_vram_lost(struct amdgpu_device *adev)
1775{
1776 return !!memcmp(adev->gart.ptr, adev->reset_magic,
1777 AMDGPU_RESET_MAGIC_NUM);
1778}
1779
Shirish S2dc80b02017-05-25 10:05:25 +05301780static int amdgpu_late_set_cg_state(struct amdgpu_device *adev)
1781{
1782 int i = 0, r;
1783
1784 for (i = 0; i < adev->num_ip_blocks; i++) {
1785 if (!adev->ip_blocks[i].status.valid)
1786 continue;
1787 /* skip CG for VCE/UVD, it's handled specially */
1788 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1789 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1790 /* enable clockgating to save power */
1791 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1792 AMD_CG_STATE_GATE);
1793 if (r) {
1794 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
1795 adev->ip_blocks[i].version->funcs->name, r);
1796 return r;
1797 }
1798 }
1799 }
1800 return 0;
1801}
1802
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001803static int amdgpu_late_init(struct amdgpu_device *adev)
1804{
1805 int i = 0, r;
1806
1807 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001808 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001809 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001810 if (adev->ip_blocks[i].version->funcs->late_init) {
1811 r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001812 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001813 DRM_ERROR("late_init of IP block <%s> failed %d\n",
1814 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001815 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001816 }
Alex Deuchera1255102016-10-13 17:41:13 -04001817 adev->ip_blocks[i].status.late_initialized = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001818 }
1819 }
1820
Shirish S2dc80b02017-05-25 10:05:25 +05301821 mod_delayed_work(system_wq, &adev->late_init_work,
1822 msecs_to_jiffies(AMDGPU_RESUME_MS));
1823
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08001824 amdgpu_fill_reset_magic(adev);
1825
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001826 return 0;
1827}
1828
1829static int amdgpu_fini(struct amdgpu_device *adev)
1830{
1831 int i, r;
1832
pding18847342017-11-06 10:21:26 +08001833 amdgpu_amdkfd_device_fini(adev);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001834 /* need to disable SMC first */
1835 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001836 if (!adev->ip_blocks[i].status.hw)
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001837 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001838 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001839 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
Alex Deuchera1255102016-10-13 17:41:13 -04001840 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1841 AMD_CG_STATE_UNGATE);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001842 if (r) {
1843 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001844 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001845 return r;
1846 }
Alex Deuchera1255102016-10-13 17:41:13 -04001847 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001848 /* XXX handle errors */
1849 if (r) {
1850 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001851 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001852 }
Alex Deuchera1255102016-10-13 17:41:13 -04001853 adev->ip_blocks[i].status.hw = false;
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001854 break;
1855 }
1856 }
1857
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001858 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001859 if (!adev->ip_blocks[i].status.hw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001860 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001861 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
Monk Liu84e5b512017-11-14 16:52:14 +08001862 amdgpu_free_static_csa(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001863 amdgpu_wb_fini(adev);
1864 amdgpu_vram_scratch_fini(adev);
1865 }
Rex Zhu8201a672016-11-24 21:44:44 +08001866
1867 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1868 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1869 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
1870 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1871 AMD_CG_STATE_UNGATE);
1872 if (r) {
1873 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1874 adev->ip_blocks[i].version->funcs->name, r);
1875 return r;
1876 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001877 }
Rex Zhu8201a672016-11-24 21:44:44 +08001878
Alex Deuchera1255102016-10-13 17:41:13 -04001879 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001880 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001881 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001882 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
1883 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001884 }
Rex Zhu8201a672016-11-24 21:44:44 +08001885
Alex Deuchera1255102016-10-13 17:41:13 -04001886 adev->ip_blocks[i].status.hw = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001887 }
1888
1889 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001890 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001891 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001892 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001893 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001894 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001895 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
1896 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001897 }
Alex Deuchera1255102016-10-13 17:41:13 -04001898 adev->ip_blocks[i].status.sw = false;
1899 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001900 }
1901
Monk Liua6dcfd92016-05-19 14:36:34 +08001902 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001903 if (!adev->ip_blocks[i].status.late_initialized)
Grazvydas Ignotas8a2eef12016-10-03 00:06:44 +03001904 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001905 if (adev->ip_blocks[i].version->funcs->late_fini)
1906 adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
1907 adev->ip_blocks[i].status.late_initialized = false;
Monk Liua6dcfd92016-05-19 14:36:34 +08001908 }
1909
Monk Liu030308f2017-09-15 15:34:52 +08001910 if (amdgpu_sriov_vf(adev))
Monk Liu24136132017-11-14 16:56:55 +08001911 if (amdgpu_virt_release_full_gpu(adev, false))
1912 DRM_ERROR("failed to release exclusive mode on fini\n");
Monk Liu24936642017-01-09 15:54:32 +08001913
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001914 return 0;
1915}
1916
Shirish S2dc80b02017-05-25 10:05:25 +05301917static void amdgpu_late_init_func_handler(struct work_struct *work)
1918{
1919 struct amdgpu_device *adev =
1920 container_of(work, struct amdgpu_device, late_init_work.work);
1921 amdgpu_late_set_cg_state(adev);
1922}
1923
Alex Deucherfaefba92016-12-06 10:38:29 -05001924int amdgpu_suspend(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001925{
1926 int i, r;
1927
Xiangliang Yue941ea92017-01-18 12:47:55 +08001928 if (amdgpu_sriov_vf(adev))
1929 amdgpu_virt_request_full_gpu(adev, false);
1930
Flora Cuic5a93a22016-02-26 10:45:25 +08001931 /* ungate SMC block first */
1932 r = amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC,
1933 AMD_CG_STATE_UNGATE);
1934 if (r) {
1935 DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n",r);
1936 }
1937
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001938 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001939 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001940 continue;
1941 /* ungate blocks so that suspend can properly shut them down */
Flora Cuic5a93a22016-02-26 10:45:25 +08001942 if (i != AMD_IP_BLOCK_TYPE_SMC) {
Alex Deuchera1255102016-10-13 17:41:13 -04001943 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1944 AMD_CG_STATE_UNGATE);
Flora Cuic5a93a22016-02-26 10:45:25 +08001945 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001946 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1947 adev->ip_blocks[i].version->funcs->name, r);
Flora Cuic5a93a22016-02-26 10:45:25 +08001948 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001949 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001950 /* XXX handle errors */
Alex Deuchera1255102016-10-13 17:41:13 -04001951 r = adev->ip_blocks[i].version->funcs->suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001952 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001953 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001954 DRM_ERROR("suspend of IP block <%s> failed %d\n",
1955 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001956 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001957 }
1958
Xiangliang Yue941ea92017-01-18 12:47:55 +08001959 if (amdgpu_sriov_vf(adev))
1960 amdgpu_virt_release_full_gpu(adev, false);
1961
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001962 return 0;
1963}
1964
Monk Liue4f0fdc2017-02-09 11:55:49 +08001965static int amdgpu_sriov_reinit_early(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08001966{
1967 int i, r;
1968
Monk Liu2cb681b2017-04-26 12:00:49 +08001969 static enum amd_ip_block_type ip_order[] = {
1970 AMD_IP_BLOCK_TYPE_GMC,
1971 AMD_IP_BLOCK_TYPE_COMMON,
Monk Liu2cb681b2017-04-26 12:00:49 +08001972 AMD_IP_BLOCK_TYPE_IH,
1973 };
Monk Liua90ad3c2017-01-23 14:22:08 +08001974
Monk Liu2cb681b2017-04-26 12:00:49 +08001975 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1976 int j;
1977 struct amdgpu_ip_block *block;
Monk Liua90ad3c2017-01-23 14:22:08 +08001978
Monk Liu2cb681b2017-04-26 12:00:49 +08001979 for (j = 0; j < adev->num_ip_blocks; j++) {
1980 block = &adev->ip_blocks[j];
1981
1982 if (block->version->type != ip_order[i] ||
1983 !block->status.valid)
1984 continue;
1985
1986 r = block->version->funcs->hw_init(adev);
1987 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
Monk Liua90ad3c2017-01-23 14:22:08 +08001988 }
1989 }
1990
1991 return 0;
1992}
1993
Monk Liue4f0fdc2017-02-09 11:55:49 +08001994static int amdgpu_sriov_reinit_late(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08001995{
1996 int i, r;
1997
Monk Liu2cb681b2017-04-26 12:00:49 +08001998 static enum amd_ip_block_type ip_order[] = {
1999 AMD_IP_BLOCK_TYPE_SMC,
Monk Liuef4c1662017-09-22 16:23:34 +08002000 AMD_IP_BLOCK_TYPE_PSP,
Monk Liu2cb681b2017-04-26 12:00:49 +08002001 AMD_IP_BLOCK_TYPE_DCE,
2002 AMD_IP_BLOCK_TYPE_GFX,
2003 AMD_IP_BLOCK_TYPE_SDMA,
Frank Min257deb82017-06-15 20:07:36 +08002004 AMD_IP_BLOCK_TYPE_UVD,
2005 AMD_IP_BLOCK_TYPE_VCE
Monk Liu2cb681b2017-04-26 12:00:49 +08002006 };
Monk Liua90ad3c2017-01-23 14:22:08 +08002007
Monk Liu2cb681b2017-04-26 12:00:49 +08002008 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
2009 int j;
2010 struct amdgpu_ip_block *block;
Monk Liua90ad3c2017-01-23 14:22:08 +08002011
Monk Liu2cb681b2017-04-26 12:00:49 +08002012 for (j = 0; j < adev->num_ip_blocks; j++) {
2013 block = &adev->ip_blocks[j];
2014
2015 if (block->version->type != ip_order[i] ||
2016 !block->status.valid)
2017 continue;
2018
2019 r = block->version->funcs->hw_init(adev);
2020 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
Monk Liua90ad3c2017-01-23 14:22:08 +08002021 }
2022 }
2023
2024 return 0;
2025}
2026
Chunming Zhoufcf06492017-05-05 10:33:33 +08002027static int amdgpu_resume_phase1(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002028{
2029 int i, r;
2030
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002031 for (i = 0; i < adev->num_ip_blocks; i++) {
2032 if (!adev->ip_blocks[i].status.valid)
2033 continue;
Chunming Zhoufcf06492017-05-05 10:33:33 +08002034 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
2035 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
2036 adev->ip_blocks[i].version->type ==
2037 AMD_IP_BLOCK_TYPE_IH) {
2038 r = adev->ip_blocks[i].version->funcs->resume(adev);
2039 if (r) {
2040 DRM_ERROR("resume of IP block <%s> failed %d\n",
2041 adev->ip_blocks[i].version->funcs->name, r);
2042 return r;
2043 }
2044 }
2045 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002046
Chunming Zhoufcf06492017-05-05 10:33:33 +08002047 return 0;
2048}
2049
2050static int amdgpu_resume_phase2(struct amdgpu_device *adev)
2051{
2052 int i, r;
2053
2054 for (i = 0; i < adev->num_ip_blocks; i++) {
2055 if (!adev->ip_blocks[i].status.valid)
2056 continue;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002057 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
2058 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
2059 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH )
2060 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002061 r = adev->ip_blocks[i].version->funcs->resume(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002062 if (r) {
2063 DRM_ERROR("resume of IP block <%s> failed %d\n",
2064 adev->ip_blocks[i].version->funcs->name, r);
2065 return r;
2066 }
2067 }
2068
2069 return 0;
2070}
2071
2072static int amdgpu_resume(struct amdgpu_device *adev)
2073{
Chunming Zhoufcf06492017-05-05 10:33:33 +08002074 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002075
Chunming Zhoufcf06492017-05-05 10:33:33 +08002076 r = amdgpu_resume_phase1(adev);
2077 if (r)
2078 return r;
2079 r = amdgpu_resume_phase2(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002080
Chunming Zhoufcf06492017-05-05 10:33:33 +08002081 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002082}
2083
Monk Liu4e99a442016-03-31 13:26:59 +08002084static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
Andres Rodriguez048765a2016-06-11 02:51:32 -04002085{
Monk Liu6867e1b2017-10-16 19:50:44 +08002086 if (amdgpu_sriov_vf(adev)) {
2087 if (adev->is_atom_fw) {
2088 if (amdgpu_atomfirmware_gpu_supports_virtualization(adev))
2089 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
2090 } else {
2091 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
2092 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
2093 }
2094
2095 if (!(adev->virt.caps & AMDGPU_SRIOV_CAPS_SRIOV_VBIOS))
2096 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_NO_VBIOS, 0, 0);
Alex Deuchera5bde2f2016-09-23 16:23:41 -04002097 }
Andres Rodriguez048765a2016-06-11 02:51:32 -04002098}
2099
Harry Wentland45622362017-09-12 15:58:20 -04002100bool amdgpu_device_asic_has_dc_support(enum amd_asic_type asic_type)
2101{
2102 switch (asic_type) {
2103#if defined(CONFIG_DRM_AMD_DC)
2104 case CHIP_BONAIRE:
2105 case CHIP_HAWAII:
Alex Deucher0d6fbcc2017-08-10 14:39:48 -04002106 case CHIP_KAVERI:
Harry Wentland45622362017-09-12 15:58:20 -04002107 case CHIP_CARRIZO:
2108 case CHIP_STONEY:
2109 case CHIP_POLARIS11:
2110 case CHIP_POLARIS10:
Alex Deucher2c8ad2d2017-06-15 16:20:24 -04002111 case CHIP_POLARIS12:
Harry Wentland45622362017-09-12 15:58:20 -04002112 case CHIP_TONGA:
2113 case CHIP_FIJI:
2114#if defined(CONFIG_DRM_AMD_DC_PRE_VEGA)
2115 return amdgpu_dc != 0;
Harry Wentland45622362017-09-12 15:58:20 -04002116#endif
Alex Deucher17b7cf82017-08-23 09:42:22 -04002117 case CHIP_KABINI:
2118 case CHIP_MULLINS:
2119 return amdgpu_dc > 0;
Harry Wentland42f8ffa2017-09-15 14:07:30 -04002120 case CHIP_VEGA10:
2121#if defined(CONFIG_DRM_AMD_DC_DCN1_0)
Hawking Zhangfd187852017-03-06 14:01:11 +08002122 case CHIP_RAVEN:
Harry Wentland42f8ffa2017-09-15 14:07:30 -04002123#endif
Hawking Zhangfd187852017-03-06 14:01:11 +08002124 return amdgpu_dc != 0;
2125#endif
Harry Wentland45622362017-09-12 15:58:20 -04002126 default:
2127 return false;
2128 }
2129}
2130
2131/**
2132 * amdgpu_device_has_dc_support - check if dc is supported
2133 *
2134 * @adev: amdgpu_device_pointer
2135 *
2136 * Returns true for supported, false for not supported
2137 */
2138bool amdgpu_device_has_dc_support(struct amdgpu_device *adev)
2139{
Xiangliang Yu2555039d2017-01-10 17:34:52 +08002140 if (amdgpu_sriov_vf(adev))
2141 return false;
2142
Harry Wentland45622362017-09-12 15:58:20 -04002143 return amdgpu_device_asic_has_dc_support(adev->asic_type);
2144}
2145
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002146/**
2147 * amdgpu_device_init - initialize the driver
2148 *
2149 * @adev: amdgpu_device pointer
2150 * @pdev: drm dev pointer
2151 * @pdev: pci dev pointer
2152 * @flags: driver flags
2153 *
2154 * Initializes the driver info and hw (all asics).
2155 * Returns 0 for success or an error on failure.
2156 * Called at driver startup.
2157 */
2158int amdgpu_device_init(struct amdgpu_device *adev,
2159 struct drm_device *ddev,
2160 struct pci_dev *pdev,
2161 uint32_t flags)
2162{
2163 int r, i;
2164 bool runtime = false;
Marek Olšák95844d22016-08-17 23:49:27 +02002165 u32 max_MBps;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002166
2167 adev->shutdown = false;
2168 adev->dev = &pdev->dev;
2169 adev->ddev = ddev;
2170 adev->pdev = pdev;
2171 adev->flags = flags;
Jammy Zhou2f7d10b2015-07-22 11:29:01 +08002172 adev->asic_type = flags & AMD_ASIC_MASK;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002173 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
Christian König6f02a692017-07-07 11:56:59 +02002174 adev->mc.gart_size = 512 * 1024 * 1024;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002175 adev->accel_working = false;
2176 adev->num_rings = 0;
2177 adev->mman.buffer_funcs = NULL;
2178 adev->mman.buffer_funcs_ring = NULL;
2179 adev->vm_manager.vm_pte_funcs = NULL;
Christian König2d55e452016-02-08 17:37:38 +01002180 adev->vm_manager.vm_pte_num_rings = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002181 adev->gart.gart_funcs = NULL;
Chris Wilsonf54d1862016-10-25 13:00:45 +01002182 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
Andres Rodriguezb8866c22017-04-28 20:05:51 -04002183 bitmap_zero(adev->gfx.pipe_reserve_bitmap, AMDGPU_MAX_COMPUTE_QUEUES);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002184
2185 adev->smc_rreg = &amdgpu_invalid_rreg;
2186 adev->smc_wreg = &amdgpu_invalid_wreg;
2187 adev->pcie_rreg = &amdgpu_invalid_rreg;
2188 adev->pcie_wreg = &amdgpu_invalid_wreg;
Huang Rui36b9a952016-08-31 13:23:25 +08002189 adev->pciep_rreg = &amdgpu_invalid_rreg;
2190 adev->pciep_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002191 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
2192 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
2193 adev->didt_rreg = &amdgpu_invalid_rreg;
2194 adev->didt_wreg = &amdgpu_invalid_wreg;
Rex Zhuccdbb202016-06-08 12:47:41 +08002195 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
2196 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002197 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
2198 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
2199
Alex Deucher3e39ab92015-06-05 15:04:33 -04002200 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
2201 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
2202 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002203
2204 /* mutex initialization are all done here so we
2205 * can recall function without having locking issues */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002206 atomic_set(&adev->irq.ih.lock, 0);
Huang Rui0e5ca0d2017-03-03 18:37:23 -05002207 mutex_init(&adev->firmware.mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002208 mutex_init(&adev->pm.mutex);
2209 mutex_init(&adev->gfx.gpu_clock_mutex);
2210 mutex_init(&adev->srbm_mutex);
Andres Rodriguezb8866c22017-04-28 20:05:51 -04002211 mutex_init(&adev->gfx.pipe_reserve_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002212 mutex_init(&adev->grbm_idx_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002213 mutex_init(&adev->mn_lock);
Alex Deuchere23b74a2017-09-28 09:47:32 -04002214 mutex_init(&adev->virt.vf_errors.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002215 hash_init(adev->mn_hash);
Monk Liu13a752e2017-10-17 15:11:12 +08002216 mutex_init(&adev->lock_reset);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002217
2218 amdgpu_check_arguments(adev);
2219
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002220 spin_lock_init(&adev->mmio_idx_lock);
2221 spin_lock_init(&adev->smc_idx_lock);
2222 spin_lock_init(&adev->pcie_idx_lock);
2223 spin_lock_init(&adev->uvd_ctx_idx_lock);
2224 spin_lock_init(&adev->didt_idx_lock);
Rex Zhuccdbb202016-06-08 12:47:41 +08002225 spin_lock_init(&adev->gc_cac_idx_lock);
Evan Quan16abb5d2017-07-04 09:21:50 +08002226 spin_lock_init(&adev->se_cac_idx_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002227 spin_lock_init(&adev->audio_endpt_idx_lock);
Marek Olšák95844d22016-08-17 23:49:27 +02002228 spin_lock_init(&adev->mm_stats.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002229
Chunming Zhou0c4e7fa2016-08-17 11:41:30 +08002230 INIT_LIST_HEAD(&adev->shadow_list);
2231 mutex_init(&adev->shadow_list_lock);
2232
Andres Rodriguez795f2812017-03-06 16:27:55 -05002233 INIT_LIST_HEAD(&adev->ring_lru_list);
2234 spin_lock_init(&adev->ring_lru_list_lock);
2235
Shirish S2dc80b02017-05-25 10:05:25 +05302236 INIT_DELAYED_WORK(&adev->late_init_work, amdgpu_late_init_func_handler);
2237
Alex Xie0fa49552017-06-08 14:58:05 -04002238 /* Registers mapping */
2239 /* TODO: block userspace mapping of io register */
Ken Wangda69c1612016-01-21 19:08:55 +08002240 if (adev->asic_type >= CHIP_BONAIRE) {
2241 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
2242 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
2243 } else {
2244 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
2245 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
2246 }
Chunming Zhou5c1354b2016-08-30 16:13:10 +08002247
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002248 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
2249 if (adev->rmmio == NULL) {
2250 return -ENOMEM;
2251 }
2252 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
2253 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
2254
Christian König705e5192017-06-08 11:15:16 +02002255 /* doorbell bar mapping */
2256 amdgpu_doorbell_init(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002257
2258 /* io port mapping */
2259 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
2260 if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
2261 adev->rio_mem_size = pci_resource_len(adev->pdev, i);
2262 adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
2263 break;
2264 }
2265 }
2266 if (adev->rio_mem == NULL)
Amber Linb64a18c2017-01-04 08:06:58 -05002267 DRM_INFO("PCI I/O BAR is not found.\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002268
2269 /* early init functions */
2270 r = amdgpu_early_init(adev);
2271 if (r)
2272 return r;
2273
2274 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
2275 /* this will fail for cards that aren't VGA class devices, just
2276 * ignore it */
2277 vga_client_register(adev->pdev, adev, NULL, amdgpu_vga_set_decode);
2278
2279 if (amdgpu_runtime_pm == 1)
2280 runtime = true;
Alex Deuchere9bef452016-04-25 13:12:18 -04002281 if (amdgpu_device_is_px(ddev))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002282 runtime = true;
Lukas Wunner84c8b222017-03-10 21:23:45 +01002283 if (!pci_is_thunderbolt_attached(adev->pdev))
2284 vga_switcheroo_register_client(adev->pdev,
2285 &amdgpu_switcheroo_ops, runtime);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002286 if (runtime)
2287 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
2288
2289 /* Read BIOS */
Alex Deucher83ba1262016-06-03 18:21:41 -04002290 if (!amdgpu_get_bios(adev)) {
2291 r = -EINVAL;
2292 goto failed;
2293 }
Nils Wallméniusf7e9e9f2016-12-14 21:52:45 +01002294
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002295 r = amdgpu_atombios_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002296 if (r) {
2297 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002298 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_INIT_FAIL, 0, 0);
Alex Deucher83ba1262016-06-03 18:21:41 -04002299 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002300 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002301
Monk Liu4e99a442016-03-31 13:26:59 +08002302 /* detect if we are with an SRIOV vbios */
2303 amdgpu_device_detect_sriov_bios(adev);
Andres Rodriguez048765a2016-06-11 02:51:32 -04002304
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002305 /* Post card if necessary */
pding91fe77e2017-10-19 09:38:39 +08002306 if (amdgpu_need_post(adev)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002307 if (!adev->bios) {
Monk Liubec86372016-09-14 19:38:08 +08002308 dev_err(adev->dev, "no vBIOS found\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04002309 r = -EINVAL;
2310 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002311 }
Monk Liubec86372016-09-14 19:38:08 +08002312 DRM_INFO("GPU posting now...\n");
Monk Liu4e99a442016-03-31 13:26:59 +08002313 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2314 if (r) {
2315 dev_err(adev->dev, "gpu post error!\n");
2316 goto failed;
2317 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002318 }
2319
Alex Deucher88b64e92017-07-10 10:43:10 -04002320 if (adev->is_atom_fw) {
2321 /* Initialize clocks */
2322 r = amdgpu_atomfirmware_get_clock_info(adev);
2323 if (r) {
2324 dev_err(adev->dev, "amdgpu_atomfirmware_get_clock_info failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002325 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
Alex Deucher88b64e92017-07-10 10:43:10 -04002326 goto failed;
2327 }
2328 } else {
Alex Deuchera5bde2f2016-09-23 16:23:41 -04002329 /* Initialize clocks */
2330 r = amdgpu_atombios_get_clock_info(adev);
2331 if (r) {
2332 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002333 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
Gavin Wan89041942017-06-23 13:55:15 -04002334 goto failed;
Alex Deuchera5bde2f2016-09-23 16:23:41 -04002335 }
2336 /* init i2c buses */
Harry Wentland45622362017-09-12 15:58:20 -04002337 if (!amdgpu_device_has_dc_support(adev))
2338 amdgpu_atombios_i2c_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002339 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002340
2341 /* Fence driver */
2342 r = amdgpu_fence_driver_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002343 if (r) {
2344 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002345 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0);
Alex Deucher83ba1262016-06-03 18:21:41 -04002346 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002347 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002348
2349 /* init the mode config */
2350 drm_mode_config_init(adev->ddev);
2351
2352 r = amdgpu_init(adev);
2353 if (r) {
pding8840a382017-10-23 17:22:09 +08002354 /* failed in exclusive mode due to timeout */
2355 if (amdgpu_sriov_vf(adev) &&
2356 !amdgpu_sriov_runtime(adev) &&
2357 amdgpu_virt_mmio_blocked(adev) &&
2358 !amdgpu_virt_wait_reset(adev)) {
2359 dev_err(adev->dev, "VF exclusive mode timeout\n");
Pixel Ding1daee8b2017-11-08 11:03:14 +08002360 /* Don't send request since VF is inactive. */
2361 adev->virt.caps &= ~AMDGPU_SRIOV_CAPS_RUNTIME;
2362 adev->virt.ops = NULL;
pding8840a382017-10-23 17:22:09 +08002363 r = -EAGAIN;
2364 goto failed;
2365 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05002366 dev_err(adev->dev, "amdgpu_init failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002367 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002368 amdgpu_fini(adev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002369 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002370 }
2371
2372 adev->accel_working = true;
2373
Alex Xiee59c0202017-06-01 09:42:59 -04002374 amdgpu_vm_check_compute_bug(adev);
2375
Marek Olšák95844d22016-08-17 23:49:27 +02002376 /* Initialize the buffer migration limit. */
2377 if (amdgpu_moverate >= 0)
2378 max_MBps = amdgpu_moverate;
2379 else
2380 max_MBps = 8; /* Allow 8 MB/s. */
2381 /* Get a log2 for easy divisions. */
2382 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
2383
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002384 r = amdgpu_ib_pool_init(adev);
2385 if (r) {
2386 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
Alex Deuchere23b74a2017-09-28 09:47:32 -04002387 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r);
Alex Deucher83ba1262016-06-03 18:21:41 -04002388 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002389 }
2390
2391 r = amdgpu_ib_ring_tests(adev);
2392 if (r)
2393 DRM_ERROR("ib ring test failed (%d).\n", r);
2394
Horace Chen2dc8f812017-10-09 16:17:16 +08002395 if (amdgpu_sriov_vf(adev))
2396 amdgpu_virt_init_data_exchange(adev);
2397
Monk Liu9bc92b92017-02-08 17:38:13 +08002398 amdgpu_fbdev_init(adev);
2399
Rex Zhud2f52ac2017-09-22 17:47:27 +08002400 r = amdgpu_pm_sysfs_init(adev);
2401 if (r)
2402 DRM_ERROR("registering pm debugfs failed (%d).\n", r);
2403
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002404 r = amdgpu_gem_debugfs_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002405 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002406 DRM_ERROR("registering gem debugfs failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002407
2408 r = amdgpu_debugfs_regs_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002409 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002410 DRM_ERROR("registering register debugfs failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002411
Huang Rui50ab2532016-06-12 15:51:09 +08002412 r = amdgpu_debugfs_firmware_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002413 if (r)
Huang Rui50ab2532016-06-12 15:51:09 +08002414 DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
Huang Rui50ab2532016-06-12 15:51:09 +08002415
Christian König763efb62017-12-06 15:44:51 +01002416 r = amdgpu_debugfs_init(adev);
Kent Russelldb95e212017-08-22 12:31:43 -04002417 if (r)
Christian König763efb62017-12-06 15:44:51 +01002418 DRM_ERROR("Creating debugfs files failed (%d).\n", r);
Kent Russelldb95e212017-08-22 12:31:43 -04002419
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002420 if ((amdgpu_testing & 1)) {
2421 if (adev->accel_working)
2422 amdgpu_test_moves(adev);
2423 else
2424 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
2425 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002426 if (amdgpu_benchmarking) {
2427 if (adev->accel_working)
2428 amdgpu_benchmark(adev, amdgpu_benchmarking);
2429 else
2430 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
2431 }
2432
2433 /* enable clockgating, etc. after ib tests, etc. since some blocks require
2434 * explicit gating rather than handling it automatically.
2435 */
2436 r = amdgpu_late_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002437 if (r) {
2438 dev_err(adev->dev, "amdgpu_late_init failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002439 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r);
Alex Deucher83ba1262016-06-03 18:21:41 -04002440 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002441 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002442
2443 return 0;
Alex Deucher83ba1262016-06-03 18:21:41 -04002444
2445failed:
Gavin Wan89041942017-06-23 13:55:15 -04002446 amdgpu_vf_error_trans_all(adev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002447 if (runtime)
2448 vga_switcheroo_fini_domain_pm_ops(adev->dev);
pding8840a382017-10-23 17:22:09 +08002449
Alex Deucher83ba1262016-06-03 18:21:41 -04002450 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002451}
2452
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002453/**
2454 * amdgpu_device_fini - tear down the driver
2455 *
2456 * @adev: amdgpu_device pointer
2457 *
2458 * Tear down the driver info (all asics).
2459 * Called at driver shutdown.
2460 */
2461void amdgpu_device_fini(struct amdgpu_device *adev)
2462{
2463 int r;
2464
2465 DRM_INFO("amdgpu: finishing device.\n");
2466 adev->shutdown = true;
Pixel Dingdb2c2a92017-04-25 16:47:42 +08002467 if (adev->mode_info.mode_config_initialized)
2468 drm_crtc_force_disable_all(adev->ddev);
Monk Liub9141cd2017-11-22 19:21:43 +08002469
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002470 amdgpu_ib_pool_fini(adev);
2471 amdgpu_fence_driver_fini(adev);
2472 amdgpu_fbdev_fini(adev);
2473 r = amdgpu_fini(adev);
Huang Ruiab4fe3e2017-06-05 22:11:59 +08002474 if (adev->firmware.gpu_info_fw) {
2475 release_firmware(adev->firmware.gpu_info_fw);
2476 adev->firmware.gpu_info_fw = NULL;
2477 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002478 adev->accel_working = false;
Shirish S2dc80b02017-05-25 10:05:25 +05302479 cancel_delayed_work_sync(&adev->late_init_work);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002480 /* free i2c buses */
Harry Wentland45622362017-09-12 15:58:20 -04002481 if (!amdgpu_device_has_dc_support(adev))
2482 amdgpu_i2c_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002483 amdgpu_atombios_fini(adev);
2484 kfree(adev->bios);
2485 adev->bios = NULL;
Lukas Wunner84c8b222017-03-10 21:23:45 +01002486 if (!pci_is_thunderbolt_attached(adev->pdev))
2487 vga_switcheroo_unregister_client(adev->pdev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002488 if (adev->flags & AMD_IS_PX)
2489 vga_switcheroo_fini_domain_pm_ops(adev->dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002490 vga_client_register(adev->pdev, NULL, NULL, NULL);
2491 if (adev->rio_mem)
2492 pci_iounmap(adev->pdev, adev->rio_mem);
2493 adev->rio_mem = NULL;
2494 iounmap(adev->rmmio);
2495 adev->rmmio = NULL;
Christian König705e5192017-06-08 11:15:16 +02002496 amdgpu_doorbell_fini(adev);
Rex Zhud2f52ac2017-09-22 17:47:27 +08002497 amdgpu_pm_sysfs_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002498 amdgpu_debugfs_regs_cleanup(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002499}
2500
2501
2502/*
2503 * Suspend & resume.
2504 */
2505/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002506 * amdgpu_device_suspend - initiate device suspend
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002507 *
2508 * @pdev: drm dev pointer
2509 * @state: suspend state
2510 *
2511 * Puts the hw in the suspend state (all asics).
2512 * Returns 0 for success or an error on failure.
2513 * Called at driver suspend.
2514 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002515int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002516{
2517 struct amdgpu_device *adev;
2518 struct drm_crtc *crtc;
2519 struct drm_connector *connector;
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002520 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002521
2522 if (dev == NULL || dev->dev_private == NULL) {
2523 return -ENODEV;
2524 }
2525
2526 adev = dev->dev_private;
2527
2528 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2529 return 0;
2530
2531 drm_kms_helper_poll_disable(dev);
2532
Harry Wentland45622362017-09-12 15:58:20 -04002533 if (!amdgpu_device_has_dc_support(adev)) {
2534 /* turn off display hw */
2535 drm_modeset_lock_all(dev);
2536 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2537 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
2538 }
2539 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002540 }
2541
Yong Zhaoba997702015-11-09 17:21:45 -05002542 amdgpu_amdkfd_suspend(adev);
2543
Alex Deucher756e6882015-10-08 00:03:36 -04002544 /* unpin the front buffers and cursors */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002545 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Alex Deucher756e6882015-10-08 00:03:36 -04002546 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002547 struct amdgpu_framebuffer *rfb = to_amdgpu_framebuffer(crtc->primary->fb);
2548 struct amdgpu_bo *robj;
2549
Alex Deucher756e6882015-10-08 00:03:36 -04002550 if (amdgpu_crtc->cursor_bo) {
2551 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
Alex Xie7a6901d2017-04-24 13:52:41 -04002552 r = amdgpu_bo_reserve(aobj, true);
Alex Deucher756e6882015-10-08 00:03:36 -04002553 if (r == 0) {
2554 amdgpu_bo_unpin(aobj);
2555 amdgpu_bo_unreserve(aobj);
2556 }
2557 }
2558
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002559 if (rfb == NULL || rfb->obj == NULL) {
2560 continue;
2561 }
2562 robj = gem_to_amdgpu_bo(rfb->obj);
2563 /* don't unpin kernel fb objects */
2564 if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
Alex Xie7a6901d2017-04-24 13:52:41 -04002565 r = amdgpu_bo_reserve(robj, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002566 if (r == 0) {
2567 amdgpu_bo_unpin(robj);
2568 amdgpu_bo_unreserve(robj);
2569 }
2570 }
2571 }
2572 /* evict vram memory */
2573 amdgpu_bo_evict_vram(adev);
2574
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002575 amdgpu_fence_driver_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002576
2577 r = amdgpu_suspend(adev);
2578
Alex Deuchera0a71e42016-10-10 12:41:36 -04002579 /* evict remaining vram memory
2580 * This second call to evict vram is to evict the gart page table
2581 * using the CPU.
2582 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002583 amdgpu_bo_evict_vram(adev);
2584
2585 pci_save_state(dev->pdev);
2586 if (suspend) {
2587 /* Shut down the device */
2588 pci_disable_device(dev->pdev);
2589 pci_set_power_state(dev->pdev, PCI_D3hot);
jimqu74b0b152016-09-07 17:09:12 +08002590 } else {
2591 r = amdgpu_asic_reset(adev);
2592 if (r)
2593 DRM_ERROR("amdgpu asic reset failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002594 }
2595
2596 if (fbcon) {
2597 console_lock();
2598 amdgpu_fbdev_set_suspend(adev, 1);
2599 console_unlock();
2600 }
2601 return 0;
2602}
2603
2604/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002605 * amdgpu_device_resume - initiate device resume
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002606 *
2607 * @pdev: drm dev pointer
2608 *
2609 * Bring the hw back to operating state (all asics).
2610 * Returns 0 for success or an error on failure.
2611 * Called at driver resume.
2612 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002613int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002614{
2615 struct drm_connector *connector;
2616 struct amdgpu_device *adev = dev->dev_private;
Alex Deucher756e6882015-10-08 00:03:36 -04002617 struct drm_crtc *crtc;
Huang Rui03161a62017-04-13 16:12:26 +08002618 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002619
2620 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2621 return 0;
2622
jimqu74b0b152016-09-07 17:09:12 +08002623 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002624 console_lock();
jimqu74b0b152016-09-07 17:09:12 +08002625
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002626 if (resume) {
2627 pci_set_power_state(dev->pdev, PCI_D0);
2628 pci_restore_state(dev->pdev);
jimqu74b0b152016-09-07 17:09:12 +08002629 r = pci_enable_device(dev->pdev);
Huang Rui03161a62017-04-13 16:12:26 +08002630 if (r)
2631 goto unlock;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002632 }
2633
2634 /* post card */
Jim Quc836fec2017-02-10 15:59:59 +08002635 if (amdgpu_need_post(adev)) {
jimqu74b0b152016-09-07 17:09:12 +08002636 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2637 if (r)
2638 DRM_ERROR("amdgpu asic init failed\n");
2639 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002640
2641 r = amdgpu_resume(adev);
Rex Zhue6707212017-03-30 13:21:01 +08002642 if (r) {
Flora Cuica198522016-02-04 15:10:08 +08002643 DRM_ERROR("amdgpu_resume failed (%d).\n", r);
Huang Rui03161a62017-04-13 16:12:26 +08002644 goto unlock;
Rex Zhue6707212017-03-30 13:21:01 +08002645 }
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002646 amdgpu_fence_driver_resume(adev);
2647
Flora Cuica198522016-02-04 15:10:08 +08002648 if (resume) {
2649 r = amdgpu_ib_ring_tests(adev);
2650 if (r)
2651 DRM_ERROR("ib ring test failed (%d).\n", r);
2652 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002653
2654 r = amdgpu_late_init(adev);
Huang Rui03161a62017-04-13 16:12:26 +08002655 if (r)
2656 goto unlock;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002657
Alex Deucher756e6882015-10-08 00:03:36 -04002658 /* pin cursors */
2659 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2660 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2661
2662 if (amdgpu_crtc->cursor_bo) {
2663 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
Alex Xie7a6901d2017-04-24 13:52:41 -04002664 r = amdgpu_bo_reserve(aobj, true);
Alex Deucher756e6882015-10-08 00:03:36 -04002665 if (r == 0) {
2666 r = amdgpu_bo_pin(aobj,
2667 AMDGPU_GEM_DOMAIN_VRAM,
2668 &amdgpu_crtc->cursor_addr);
2669 if (r != 0)
2670 DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
2671 amdgpu_bo_unreserve(aobj);
2672 }
2673 }
2674 }
Yong Zhaoba997702015-11-09 17:21:45 -05002675 r = amdgpu_amdkfd_resume(adev);
2676 if (r)
2677 return r;
Alex Deucher756e6882015-10-08 00:03:36 -04002678
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002679 /* blat the mode back in */
2680 if (fbcon) {
Harry Wentland45622362017-09-12 15:58:20 -04002681 if (!amdgpu_device_has_dc_support(adev)) {
2682 /* pre DCE11 */
2683 drm_helper_resume_force_mode(dev);
2684
2685 /* turn on display hw */
2686 drm_modeset_lock_all(dev);
2687 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2688 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
2689 }
2690 drm_modeset_unlock_all(dev);
2691 } else {
2692 /*
2693 * There is no equivalent atomic helper to turn on
2694 * display, so we defined our own function for this,
2695 * once suspend resume is supported by the atomic
2696 * framework this will be reworked
2697 */
2698 amdgpu_dm_display_resume(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002699 }
2700 }
2701
2702 drm_kms_helper_poll_enable(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002703
2704 /*
2705 * Most of the connector probing functions try to acquire runtime pm
2706 * refs to ensure that the GPU is powered on when connector polling is
2707 * performed. Since we're calling this from a runtime PM callback,
2708 * trying to acquire rpm refs will cause us to deadlock.
2709 *
2710 * Since we're guaranteed to be holding the rpm lock, it's safe to
2711 * temporarily disable the rpm helpers so this doesn't deadlock us.
2712 */
2713#ifdef CONFIG_PM
2714 dev->dev->power.disable_depth++;
2715#endif
Harry Wentland45622362017-09-12 15:58:20 -04002716 if (!amdgpu_device_has_dc_support(adev))
2717 drm_helper_hpd_irq_event(dev);
2718 else
2719 drm_kms_helper_hotplug_event(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002720#ifdef CONFIG_PM
2721 dev->dev->power.disable_depth--;
2722#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002723
Huang Rui03161a62017-04-13 16:12:26 +08002724 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002725 amdgpu_fbdev_set_suspend(adev, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002726
Huang Rui03161a62017-04-13 16:12:26 +08002727unlock:
2728 if (fbcon)
2729 console_unlock();
2730
2731 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002732}
2733
Chunming Zhou63fbf422016-07-15 11:19:20 +08002734static bool amdgpu_check_soft_reset(struct amdgpu_device *adev)
2735{
2736 int i;
2737 bool asic_hang = false;
2738
Monk Liuf993d622017-10-16 19:46:01 +08002739 if (amdgpu_sriov_vf(adev))
2740 return true;
2741
Chunming Zhou63fbf422016-07-15 11:19:20 +08002742 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002743 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou63fbf422016-07-15 11:19:20 +08002744 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002745 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
2746 adev->ip_blocks[i].status.hang =
2747 adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
2748 if (adev->ip_blocks[i].status.hang) {
2749 DRM_INFO("IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
Chunming Zhou63fbf422016-07-15 11:19:20 +08002750 asic_hang = true;
2751 }
2752 }
2753 return asic_hang;
2754}
2755
Baoyou Xie4d446652016-09-18 22:09:35 +08002756static int amdgpu_pre_soft_reset(struct amdgpu_device *adev)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002757{
2758 int i, r = 0;
2759
2760 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002761 if (!adev->ip_blocks[i].status.valid)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002762 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002763 if (adev->ip_blocks[i].status.hang &&
2764 adev->ip_blocks[i].version->funcs->pre_soft_reset) {
2765 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
Chunming Zhoud31a5012016-07-18 10:04:34 +08002766 if (r)
2767 return r;
2768 }
2769 }
2770
2771 return 0;
2772}
2773
Chunming Zhou35d782f2016-07-15 15:57:13 +08002774static bool amdgpu_need_full_reset(struct amdgpu_device *adev)
2775{
Alex Deucherda146d32016-10-13 16:07:03 -04002776 int i;
2777
2778 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002779 if (!adev->ip_blocks[i].status.valid)
Alex Deucherda146d32016-10-13 16:07:03 -04002780 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002781 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
2782 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
2783 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
Ken Wang98512bb2017-09-14 16:25:19 +08002784 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) ||
2785 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
Alex Deuchera1255102016-10-13 17:41:13 -04002786 if (adev->ip_blocks[i].status.hang) {
Alex Deucherda146d32016-10-13 16:07:03 -04002787 DRM_INFO("Some block need full reset!\n");
2788 return true;
2789 }
2790 }
Chunming Zhou35d782f2016-07-15 15:57:13 +08002791 }
2792 return false;
2793}
2794
2795static int amdgpu_soft_reset(struct amdgpu_device *adev)
2796{
2797 int i, r = 0;
2798
2799 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002800 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002801 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002802 if (adev->ip_blocks[i].status.hang &&
2803 adev->ip_blocks[i].version->funcs->soft_reset) {
2804 r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002805 if (r)
2806 return r;
2807 }
2808 }
2809
2810 return 0;
2811}
2812
2813static int amdgpu_post_soft_reset(struct amdgpu_device *adev)
2814{
2815 int i, r = 0;
2816
2817 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002818 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002819 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002820 if (adev->ip_blocks[i].status.hang &&
2821 adev->ip_blocks[i].version->funcs->post_soft_reset)
2822 r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002823 if (r)
2824 return r;
2825 }
2826
2827 return 0;
2828}
2829
Chunming Zhou3ad81f12016-08-05 17:30:17 +08002830bool amdgpu_need_backup(struct amdgpu_device *adev)
2831{
2832 if (adev->flags & AMD_IS_APU)
2833 return false;
2834
Andrey Grodzovsky88546952017-12-13 14:36:53 -05002835 return amdgpu_gpu_recovery;
Chunming Zhou3ad81f12016-08-05 17:30:17 +08002836}
2837
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002838static int amdgpu_recover_vram_from_shadow(struct amdgpu_device *adev,
2839 struct amdgpu_ring *ring,
2840 struct amdgpu_bo *bo,
Chris Wilsonf54d1862016-10-25 13:00:45 +01002841 struct dma_fence **fence)
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002842{
2843 uint32_t domain;
2844 int r;
2845
Roger.He23d2e502017-04-21 14:24:26 +08002846 if (!bo->shadow)
2847 return 0;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002848
Alex Xie1d284792017-04-24 13:53:04 -04002849 r = amdgpu_bo_reserve(bo, true);
Roger.He23d2e502017-04-21 14:24:26 +08002850 if (r)
2851 return r;
2852 domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
2853 /* if bo has been evicted, then no need to recover */
2854 if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
Roger.He82521312017-04-21 13:08:43 +08002855 r = amdgpu_bo_validate(bo->shadow);
2856 if (r) {
2857 DRM_ERROR("bo validate failed!\n");
2858 goto err;
2859 }
2860
Roger.He23d2e502017-04-21 14:24:26 +08002861 r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002862 NULL, fence, true);
Roger.He23d2e502017-04-21 14:24:26 +08002863 if (r) {
2864 DRM_ERROR("recover page table failed!\n");
2865 goto err;
2866 }
2867 }
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002868err:
Roger.He23d2e502017-04-21 14:24:26 +08002869 amdgpu_bo_unreserve(bo);
2870 return r;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002871}
2872
Monk Liu57406822017-10-25 16:37:02 +08002873/*
2874 * amdgpu_reset - reset ASIC/GPU for bare-metal or passthrough
Monk Liua90ad3c2017-01-23 14:22:08 +08002875 *
2876 * @adev: amdgpu device pointer
Monk Liu57406822017-10-25 16:37:02 +08002877 * @reset_flags: output param tells caller the reset result
Monk Liua90ad3c2017-01-23 14:22:08 +08002878 *
Monk Liu57406822017-10-25 16:37:02 +08002879 * attempt to do soft-reset or full-reset and reinitialize Asic
2880 * return 0 means successed otherwise failed
2881*/
2882static int amdgpu_reset(struct amdgpu_device *adev, uint64_t* reset_flags)
Monk Liua90ad3c2017-01-23 14:22:08 +08002883{
Monk Liu57406822017-10-25 16:37:02 +08002884 bool need_full_reset, vram_lost = 0;
2885 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002886
Chunming Zhou35d782f2016-07-15 15:57:13 +08002887 need_full_reset = amdgpu_need_full_reset(adev);
2888
2889 if (!need_full_reset) {
2890 amdgpu_pre_soft_reset(adev);
2891 r = amdgpu_soft_reset(adev);
2892 amdgpu_post_soft_reset(adev);
2893 if (r || amdgpu_check_soft_reset(adev)) {
2894 DRM_INFO("soft reset failed, will fallback to full reset!\n");
2895 need_full_reset = true;
2896 }
Monk Liu57406822017-10-25 16:37:02 +08002897
Chunming Zhou35d782f2016-07-15 15:57:13 +08002898 }
2899
2900 if (need_full_reset) {
Chunming Zhou35d782f2016-07-15 15:57:13 +08002901 r = amdgpu_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002902
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002903retry:
Chunming Zhou35d782f2016-07-15 15:57:13 +08002904 r = amdgpu_asic_reset(adev);
2905 /* 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
Andrey Grodzovskydcebf022017-12-12 14:09:30 -05003017 * @force forces reset regardless of amdgpu_gpu_recovery
Monk Liu57406822017-10-25 16:37:02 +08003018 *
3019 * Attempt to reset the GPU if it has hung (all asics).
3020 * Returns 0 for success or an error on failure.
3021 */
Andrey Grodzovskydcebf022017-12-12 14:09:30 -05003022int amdgpu_gpu_recover(struct amdgpu_device *adev, struct amdgpu_job *job, bool force)
Monk Liu57406822017-10-25 16:37:02 +08003023{
3024 struct drm_atomic_state *state = NULL;
3025 uint64_t reset_flags = 0;
3026 int i, r, resched;
3027
3028 if (!amdgpu_check_soft_reset(adev)) {
3029 DRM_INFO("No hardware hang detected. Did some blocks stall?\n");
3030 return 0;
3031 }
3032
Andrey Grodzovskydcebf022017-12-12 14:09:30 -05003033 if (!force && (amdgpu_gpu_recovery == 0 ||
3034 (amdgpu_gpu_recovery == -1 && !amdgpu_sriov_vf(adev)))) {
3035 DRM_INFO("GPU recovery disabled.\n");
3036 return 0;
3037 }
3038
Monk Liu57406822017-10-25 16:37:02 +08003039 dev_info(adev->dev, "GPU reset begin!\n");
3040
Monk Liu13a752e2017-10-17 15:11:12 +08003041 mutex_lock(&adev->lock_reset);
Monk Liu57406822017-10-25 16:37:02 +08003042 atomic_inc(&adev->gpu_reset_counter);
Monk Liu13a752e2017-10-17 15:11:12 +08003043 adev->in_gpu_reset = 1;
Monk Liu57406822017-10-25 16:37:02 +08003044
3045 /* block TTM */
3046 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
3047 /* store modesetting */
3048 if (amdgpu_device_has_dc_support(adev))
3049 state = drm_atomic_helper_suspend(adev->ddev);
3050
3051 /* block scheduler */
3052 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
3053 struct amdgpu_ring *ring = adev->rings[i];
3054
3055 if (!ring || !ring->sched.thread)
3056 continue;
3057
3058 /* only focus on the ring hit timeout if &job not NULL */
3059 if (job && job->ring->idx != i)
3060 continue;
3061
3062 kthread_park(ring->sched.thread);
Lucas Stach1b1f42d2017-12-06 17:49:39 +01003063 drm_sched_hw_job_reset(&ring->sched, &job->base);
Monk Liu57406822017-10-25 16:37:02 +08003064
3065 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
3066 amdgpu_fence_driver_force_completion(ring);
3067 }
3068
3069 if (amdgpu_sriov_vf(adev))
3070 r = amdgpu_reset_sriov(adev, &reset_flags, job ? false : true);
3071 else
3072 r = amdgpu_reset(adev, &reset_flags);
3073
3074 if (!r) {
3075 if (((reset_flags & AMDGPU_RESET_INFO_FULLRESET) && !(adev->flags & AMD_IS_APU)) ||
3076 (reset_flags & AMDGPU_RESET_INFO_VRAM_LOST)) {
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003077 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
3078 struct amdgpu_bo *bo, *tmp;
Chris Wilsonf54d1862016-10-25 13:00:45 +01003079 struct dma_fence *fence = NULL, *next = NULL;
Chunming Zhou1f465082016-06-30 15:02:26 +08003080
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003081 DRM_INFO("recover vram bo from shadow\n");
3082 mutex_lock(&adev->shadow_list_lock);
3083 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
Monk Liu236763d2017-05-01 16:15:31 +08003084 next = NULL;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003085 amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
3086 if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01003087 r = dma_fence_wait(fence, false);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003088 if (r) {
Monk Liu1d7b17b2017-01-22 18:52:56 +08003089 WARN(r, "recovery from shadow isn't completed\n");
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003090 break;
3091 }
3092 }
3093
Chris Wilsonf54d1862016-10-25 13:00:45 +01003094 dma_fence_put(fence);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003095 fence = next;
3096 }
3097 mutex_unlock(&adev->shadow_list_lock);
3098 if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01003099 r = dma_fence_wait(fence, false);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003100 if (r)
Monk Liu1d7b17b2017-01-22 18:52:56 +08003101 WARN(r, "recovery from shadow isn't completed\n");
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003102 }
Chris Wilsonf54d1862016-10-25 13:00:45 +01003103 dma_fence_put(fence);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003104 }
Monk Liu57406822017-10-25 16:37:02 +08003105
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003106 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
3107 struct amdgpu_ring *ring = adev->rings[i];
Chunming Zhou51687752017-04-24 17:09:15 +08003108
3109 if (!ring || !ring->sched.thread)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003110 continue;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003111
Monk Liu57406822017-10-25 16:37:02 +08003112 /* only focus on the ring hit timeout if &job not NULL */
3113 if (job && job->ring->idx != i)
3114 continue;
3115
Lucas Stach1b1f42d2017-12-06 17:49:39 +01003116 drm_sched_job_recovery(&ring->sched);
Chunming Zhou0875dc92016-06-12 15:41:58 +08003117 kthread_unpark(ring->sched.thread);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003118 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003119 } else {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003120 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
Monk Liu57406822017-10-25 16:37:02 +08003121 struct amdgpu_ring *ring = adev->rings[i];
3122
3123 if (!ring || !ring->sched.thread)
3124 continue;
3125
3126 /* only focus on the ring hit timeout if &job not NULL */
3127 if (job && job->ring->idx != i)
3128 continue;
3129
3130 kthread_unpark(adev->rings[i]->sched.thread);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003131 }
3132 }
3133
Harry Wentland45622362017-09-12 15:58:20 -04003134 if (amdgpu_device_has_dc_support(adev)) {
Monk Liu57406822017-10-25 16:37:02 +08003135 if (drm_atomic_helper_resume(adev->ddev, state))
3136 dev_info(adev->dev, "drm resume failed:%d\n", r);
Harry Wentland45622362017-09-12 15:58:20 -04003137 amdgpu_dm_display_resume(adev);
Monk Liu57406822017-10-25 16:37:02 +08003138 } else {
Harry Wentland45622362017-09-12 15:58:20 -04003139 drm_helper_resume_force_mode(adev->ddev);
Monk Liu57406822017-10-25 16:37:02 +08003140 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003141
3142 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
Monk Liu57406822017-10-25 16:37:02 +08003143
Gavin Wan89041942017-06-23 13:55:15 -04003144 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003145 /* bad news, how to tell it to userspace ? */
Monk Liu57406822017-10-25 16:37:02 +08003146 dev_info(adev->dev, "GPU reset(%d) failed\n", atomic_read(&adev->gpu_reset_counter));
3147 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, r);
3148 } else {
3149 dev_info(adev->dev, "GPU reset(%d) successed!\n",atomic_read(&adev->gpu_reset_counter));
Gavin Wan89041942017-06-23 13:55:15 -04003150 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003151
Gavin Wan89041942017-06-23 13:55:15 -04003152 amdgpu_vf_error_trans_all(adev);
Monk Liu13a752e2017-10-17 15:11:12 +08003153 adev->in_gpu_reset = 0;
3154 mutex_unlock(&adev->lock_reset);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003155 return r;
3156}
3157
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003158void amdgpu_get_pcie_info(struct amdgpu_device *adev)
3159{
3160 u32 mask;
3161 int ret;
3162
Alex Deuchercd474ba2016-02-04 10:21:23 -05003163 if (amdgpu_pcie_gen_cap)
3164 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
3165
3166 if (amdgpu_pcie_lane_cap)
3167 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
3168
3169 /* covers APUs as well */
3170 if (pci_is_root_bus(adev->pdev->bus)) {
3171 if (adev->pm.pcie_gen_mask == 0)
3172 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
3173 if (adev->pm.pcie_mlw_mask == 0)
3174 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003175 return;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003176 }
Alex Deuchercd474ba2016-02-04 10:21:23 -05003177
3178 if (adev->pm.pcie_gen_mask == 0) {
3179 ret = drm_pcie_get_speed_cap_mask(adev->ddev, &mask);
3180 if (!ret) {
3181 adev->pm.pcie_gen_mask = (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
3182 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
3183 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
3184
3185 if (mask & DRM_PCIE_SPEED_25)
3186 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
3187 if (mask & DRM_PCIE_SPEED_50)
3188 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2;
3189 if (mask & DRM_PCIE_SPEED_80)
3190 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3;
3191 } else {
3192 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
3193 }
3194 }
3195 if (adev->pm.pcie_mlw_mask == 0) {
3196 ret = drm_pcie_get_max_link_width(adev->ddev, &mask);
3197 if (!ret) {
3198 switch (mask) {
3199 case 32:
3200 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
3201 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
3202 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3203 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3204 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3205 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3206 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3207 break;
3208 case 16:
3209 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
3210 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 12:
3217 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3218 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3219 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3220 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3221 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3222 break;
3223 case 8:
3224 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3225 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3226 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3227 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3228 break;
3229 case 4:
3230 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3231 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3232 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3233 break;
3234 case 2:
3235 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3236 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3237 break;
3238 case 1:
3239 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
3240 break;
3241 default:
3242 break;
3243 }
3244 } else {
3245 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003246 }
3247 }
3248}
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003249
3250/*
3251 * Debugfs
3252 */
3253int amdgpu_debugfs_add_files(struct amdgpu_device *adev,
Nils Wallménius06ab6832016-05-02 12:46:15 -04003254 const struct drm_info_list *files,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003255 unsigned nfiles)
3256{
3257 unsigned i;
3258
3259 for (i = 0; i < adev->debugfs_count; i++) {
3260 if (adev->debugfs[i].files == files) {
3261 /* Already registered */
3262 return 0;
3263 }
3264 }
3265
3266 i = adev->debugfs_count + 1;
3267 if (i > AMDGPU_DEBUGFS_MAX_COMPONENTS) {
3268 DRM_ERROR("Reached maximum number of debugfs components.\n");
3269 DRM_ERROR("Report so we increase "
3270 "AMDGPU_DEBUGFS_MAX_COMPONENTS.\n");
3271 return -EINVAL;
3272 }
3273 adev->debugfs[adev->debugfs_count].files = files;
3274 adev->debugfs[adev->debugfs_count].num_files = nfiles;
3275 adev->debugfs_count = i;
3276#if defined(CONFIG_DEBUG_FS)
3277 drm_debugfs_create_files(files, nfiles,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003278 adev->ddev->primary->debugfs_root,
3279 adev->ddev->primary);
3280#endif
3281 return 0;
3282}
3283
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003284#if defined(CONFIG_DEBUG_FS)
3285
3286static ssize_t amdgpu_debugfs_regs_read(struct file *f, char __user *buf,
3287 size_t size, loff_t *pos)
3288{
Al Viro45063092016-12-04 18:24:56 -05003289 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003290 ssize_t result = 0;
3291 int r;
Tom St Denisbd122672016-07-28 09:39:22 -04003292 bool pm_pg_lock, use_bank;
Tom St Denis566281592016-06-27 11:55:07 -04003293 unsigned instance_bank, sh_bank, se_bank;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003294
3295 if (size & 0x3 || *pos & 0x3)
3296 return -EINVAL;
3297
Tom St Denisbd122672016-07-28 09:39:22 -04003298 /* are we reading registers for which a PG lock is necessary? */
3299 pm_pg_lock = (*pos >> 23) & 1;
3300
Tom St Denis566281592016-06-27 11:55:07 -04003301 if (*pos & (1ULL << 62)) {
Tom St Denis0b968652017-11-10 12:54:50 -05003302 se_bank = (*pos & GENMASK_ULL(33, 24)) >> 24;
3303 sh_bank = (*pos & GENMASK_ULL(43, 34)) >> 34;
3304 instance_bank = (*pos & GENMASK_ULL(53, 44)) >> 44;
Tom St Denis32977f92016-10-09 07:41:26 -04003305
3306 if (se_bank == 0x3FF)
3307 se_bank = 0xFFFFFFFF;
3308 if (sh_bank == 0x3FF)
3309 sh_bank = 0xFFFFFFFF;
3310 if (instance_bank == 0x3FF)
3311 instance_bank = 0xFFFFFFFF;
Tom St Denis566281592016-06-27 11:55:07 -04003312 use_bank = 1;
Tom St Denis566281592016-06-27 11:55:07 -04003313 } else {
3314 use_bank = 0;
3315 }
3316
Tom St Denis801a6aa9a62017-03-15 05:34:25 -04003317 *pos &= (1UL << 22) - 1;
Tom St Denisbd122672016-07-28 09:39:22 -04003318
Tom St Denis566281592016-06-27 11:55:07 -04003319 if (use_bank) {
Tom St Denis32977f92016-10-09 07:41:26 -04003320 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
3321 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
Tom St Denis566281592016-06-27 11:55:07 -04003322 return -EINVAL;
3323 mutex_lock(&adev->grbm_idx_mutex);
3324 amdgpu_gfx_select_se_sh(adev, se_bank,
3325 sh_bank, instance_bank);
3326 }
3327
Tom St Denisbd122672016-07-28 09:39:22 -04003328 if (pm_pg_lock)
3329 mutex_lock(&adev->pm.mutex);
3330
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003331 while (size) {
3332 uint32_t value;
3333
3334 if (*pos > adev->rmmio_size)
Tom St Denis566281592016-06-27 11:55:07 -04003335 goto end;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003336
3337 value = RREG32(*pos >> 2);
3338 r = put_user(value, (uint32_t *)buf);
Tom St Denis566281592016-06-27 11:55:07 -04003339 if (r) {
3340 result = r;
3341 goto end;
3342 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003343
3344 result += 4;
3345 buf += 4;
3346 *pos += 4;
3347 size -= 4;
3348 }
3349
Tom St Denis566281592016-06-27 11:55:07 -04003350end:
3351 if (use_bank) {
3352 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
3353 mutex_unlock(&adev->grbm_idx_mutex);
3354 }
3355
Tom St Denisbd122672016-07-28 09:39:22 -04003356 if (pm_pg_lock)
3357 mutex_unlock(&adev->pm.mutex);
3358
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003359 return result;
3360}
3361
3362static ssize_t amdgpu_debugfs_regs_write(struct file *f, const char __user *buf,
3363 size_t size, loff_t *pos)
3364{
Al Viro45063092016-12-04 18:24:56 -05003365 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003366 ssize_t result = 0;
3367 int r;
Tom St Denis394fdde2016-10-10 07:31:23 -04003368 bool pm_pg_lock, use_bank;
3369 unsigned instance_bank, sh_bank, se_bank;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003370
3371 if (size & 0x3 || *pos & 0x3)
3372 return -EINVAL;
3373
Tom St Denis394fdde2016-10-10 07:31:23 -04003374 /* are we reading registers for which a PG lock is necessary? */
3375 pm_pg_lock = (*pos >> 23) & 1;
3376
3377 if (*pos & (1ULL << 62)) {
Tom St Denis0b968652017-11-10 12:54:50 -05003378 se_bank = (*pos & GENMASK_ULL(33, 24)) >> 24;
3379 sh_bank = (*pos & GENMASK_ULL(43, 34)) >> 34;
3380 instance_bank = (*pos & GENMASK_ULL(53, 44)) >> 44;
Tom St Denis394fdde2016-10-10 07:31:23 -04003381
3382 if (se_bank == 0x3FF)
3383 se_bank = 0xFFFFFFFF;
3384 if (sh_bank == 0x3FF)
3385 sh_bank = 0xFFFFFFFF;
3386 if (instance_bank == 0x3FF)
3387 instance_bank = 0xFFFFFFFF;
3388 use_bank = 1;
3389 } else {
3390 use_bank = 0;
3391 }
3392
Tom St Denis801a6aa9a62017-03-15 05:34:25 -04003393 *pos &= (1UL << 22) - 1;
Tom St Denis394fdde2016-10-10 07:31:23 -04003394
3395 if (use_bank) {
3396 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
3397 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
3398 return -EINVAL;
3399 mutex_lock(&adev->grbm_idx_mutex);
3400 amdgpu_gfx_select_se_sh(adev, se_bank,
3401 sh_bank, instance_bank);
3402 }
3403
3404 if (pm_pg_lock)
3405 mutex_lock(&adev->pm.mutex);
3406
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003407 while (size) {
3408 uint32_t value;
3409
3410 if (*pos > adev->rmmio_size)
3411 return result;
3412
3413 r = get_user(value, (uint32_t *)buf);
3414 if (r)
3415 return r;
3416
3417 WREG32(*pos >> 2, value);
3418
3419 result += 4;
3420 buf += 4;
3421 *pos += 4;
3422 size -= 4;
3423 }
3424
Tom St Denis394fdde2016-10-10 07:31:23 -04003425 if (use_bank) {
3426 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
3427 mutex_unlock(&adev->grbm_idx_mutex);
3428 }
3429
3430 if (pm_pg_lock)
3431 mutex_unlock(&adev->pm.mutex);
3432
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003433 return result;
3434}
3435
Tom St Denisadcec282016-04-15 13:08:44 -04003436static ssize_t amdgpu_debugfs_regs_pcie_read(struct file *f, char __user *buf,
3437 size_t size, loff_t *pos)
3438{
Al Viro45063092016-12-04 18:24:56 -05003439 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003440 ssize_t result = 0;
3441 int r;
3442
3443 if (size & 0x3 || *pos & 0x3)
3444 return -EINVAL;
3445
3446 while (size) {
3447 uint32_t value;
3448
3449 value = RREG32_PCIE(*pos >> 2);
3450 r = put_user(value, (uint32_t *)buf);
3451 if (r)
3452 return r;
3453
3454 result += 4;
3455 buf += 4;
3456 *pos += 4;
3457 size -= 4;
3458 }
3459
3460 return result;
3461}
3462
3463static ssize_t amdgpu_debugfs_regs_pcie_write(struct file *f, const char __user *buf,
3464 size_t size, loff_t *pos)
3465{
Al Viro45063092016-12-04 18:24:56 -05003466 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003467 ssize_t result = 0;
3468 int r;
3469
3470 if (size & 0x3 || *pos & 0x3)
3471 return -EINVAL;
3472
3473 while (size) {
3474 uint32_t value;
3475
3476 r = get_user(value, (uint32_t *)buf);
3477 if (r)
3478 return r;
3479
3480 WREG32_PCIE(*pos >> 2, value);
3481
3482 result += 4;
3483 buf += 4;
3484 *pos += 4;
3485 size -= 4;
3486 }
3487
3488 return result;
3489}
3490
3491static ssize_t amdgpu_debugfs_regs_didt_read(struct file *f, char __user *buf,
3492 size_t size, loff_t *pos)
3493{
Al Viro45063092016-12-04 18:24:56 -05003494 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003495 ssize_t result = 0;
3496 int r;
3497
3498 if (size & 0x3 || *pos & 0x3)
3499 return -EINVAL;
3500
3501 while (size) {
3502 uint32_t value;
3503
3504 value = RREG32_DIDT(*pos >> 2);
3505 r = put_user(value, (uint32_t *)buf);
3506 if (r)
3507 return r;
3508
3509 result += 4;
3510 buf += 4;
3511 *pos += 4;
3512 size -= 4;
3513 }
3514
3515 return result;
3516}
3517
3518static ssize_t amdgpu_debugfs_regs_didt_write(struct file *f, const char __user *buf,
3519 size_t size, loff_t *pos)
3520{
Al Viro45063092016-12-04 18:24:56 -05003521 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003522 ssize_t result = 0;
3523 int r;
3524
3525 if (size & 0x3 || *pos & 0x3)
3526 return -EINVAL;
3527
3528 while (size) {
3529 uint32_t value;
3530
3531 r = get_user(value, (uint32_t *)buf);
3532 if (r)
3533 return r;
3534
3535 WREG32_DIDT(*pos >> 2, value);
3536
3537 result += 4;
3538 buf += 4;
3539 *pos += 4;
3540 size -= 4;
3541 }
3542
3543 return result;
3544}
3545
3546static ssize_t amdgpu_debugfs_regs_smc_read(struct file *f, char __user *buf,
3547 size_t size, loff_t *pos)
3548{
Al Viro45063092016-12-04 18:24:56 -05003549 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003550 ssize_t result = 0;
3551 int r;
3552
3553 if (size & 0x3 || *pos & 0x3)
3554 return -EINVAL;
3555
3556 while (size) {
3557 uint32_t value;
3558
Tom St Denis6fc0dea2016-08-29 08:39:29 -04003559 value = RREG32_SMC(*pos);
Tom St Denisadcec282016-04-15 13:08:44 -04003560 r = put_user(value, (uint32_t *)buf);
3561 if (r)
3562 return r;
3563
3564 result += 4;
3565 buf += 4;
3566 *pos += 4;
3567 size -= 4;
3568 }
3569
3570 return result;
3571}
3572
3573static ssize_t amdgpu_debugfs_regs_smc_write(struct file *f, const char __user *buf,
3574 size_t size, loff_t *pos)
3575{
Al Viro45063092016-12-04 18:24:56 -05003576 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003577 ssize_t result = 0;
3578 int r;
3579
3580 if (size & 0x3 || *pos & 0x3)
3581 return -EINVAL;
3582
3583 while (size) {
3584 uint32_t value;
3585
3586 r = get_user(value, (uint32_t *)buf);
3587 if (r)
3588 return r;
3589
Tom St Denis6fc0dea2016-08-29 08:39:29 -04003590 WREG32_SMC(*pos, value);
Tom St Denisadcec282016-04-15 13:08:44 -04003591
3592 result += 4;
3593 buf += 4;
3594 *pos += 4;
3595 size -= 4;
3596 }
3597
3598 return result;
3599}
3600
Tom St Denis1e051412016-06-27 09:57:18 -04003601static ssize_t amdgpu_debugfs_gca_config_read(struct file *f, char __user *buf,
3602 size_t size, loff_t *pos)
3603{
Al Viro45063092016-12-04 18:24:56 -05003604 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denis1e051412016-06-27 09:57:18 -04003605 ssize_t result = 0;
3606 int r;
3607 uint32_t *config, no_regs = 0;
3608
3609 if (size & 0x3 || *pos & 0x3)
3610 return -EINVAL;
3611
Markus Elfringecab7662016-09-18 17:00:52 +02003612 config = kmalloc_array(256, sizeof(*config), GFP_KERNEL);
Tom St Denis1e051412016-06-27 09:57:18 -04003613 if (!config)
3614 return -ENOMEM;
3615
3616 /* version, increment each time something is added */
Tom St Denis9a999352017-01-18 13:01:25 -05003617 config[no_regs++] = 3;
Tom St Denis1e051412016-06-27 09:57:18 -04003618 config[no_regs++] = adev->gfx.config.max_shader_engines;
3619 config[no_regs++] = adev->gfx.config.max_tile_pipes;
3620 config[no_regs++] = adev->gfx.config.max_cu_per_sh;
3621 config[no_regs++] = adev->gfx.config.max_sh_per_se;
3622 config[no_regs++] = adev->gfx.config.max_backends_per_se;
3623 config[no_regs++] = adev->gfx.config.max_texture_channel_caches;
3624 config[no_regs++] = adev->gfx.config.max_gprs;
3625 config[no_regs++] = adev->gfx.config.max_gs_threads;
3626 config[no_regs++] = adev->gfx.config.max_hw_contexts;
3627 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_frontend;
3628 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_backend;
3629 config[no_regs++] = adev->gfx.config.sc_hiz_tile_fifo_size;
3630 config[no_regs++] = adev->gfx.config.sc_earlyz_tile_fifo_size;
3631 config[no_regs++] = adev->gfx.config.num_tile_pipes;
3632 config[no_regs++] = adev->gfx.config.backend_enable_mask;
3633 config[no_regs++] = adev->gfx.config.mem_max_burst_length_bytes;
3634 config[no_regs++] = adev->gfx.config.mem_row_size_in_kb;
3635 config[no_regs++] = adev->gfx.config.shader_engine_tile_size;
3636 config[no_regs++] = adev->gfx.config.num_gpus;
3637 config[no_regs++] = adev->gfx.config.multi_gpu_tile_size;
3638 config[no_regs++] = adev->gfx.config.mc_arb_ramcfg;
3639 config[no_regs++] = adev->gfx.config.gb_addr_config;
3640 config[no_regs++] = adev->gfx.config.num_rbs;
3641
Tom St Denis89a8f302016-08-12 15:14:31 -04003642 /* rev==1 */
3643 config[no_regs++] = adev->rev_id;
3644 config[no_regs++] = adev->pg_flags;
3645 config[no_regs++] = adev->cg_flags;
3646
Tom St Denise9f11dc2016-08-17 12:00:51 -04003647 /* rev==2 */
3648 config[no_regs++] = adev->family;
3649 config[no_regs++] = adev->external_rev_id;
3650
Tom St Denis9a999352017-01-18 13:01:25 -05003651 /* rev==3 */
3652 config[no_regs++] = adev->pdev->device;
3653 config[no_regs++] = adev->pdev->revision;
3654 config[no_regs++] = adev->pdev->subsystem_device;
3655 config[no_regs++] = adev->pdev->subsystem_vendor;
3656
Tom St Denis1e051412016-06-27 09:57:18 -04003657 while (size && (*pos < no_regs * 4)) {
3658 uint32_t value;
3659
3660 value = config[*pos >> 2];
3661 r = put_user(value, (uint32_t *)buf);
3662 if (r) {
3663 kfree(config);
3664 return r;
3665 }
3666
3667 result += 4;
3668 buf += 4;
3669 *pos += 4;
3670 size -= 4;
3671 }
3672
3673 kfree(config);
3674 return result;
3675}
3676
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003677static ssize_t amdgpu_debugfs_sensor_read(struct file *f, char __user *buf,
3678 size_t size, loff_t *pos)
3679{
Al Viro45063092016-12-04 18:24:56 -05003680 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003681 int idx, x, outsize, r, valuesize;
3682 uint32_t values[16];
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003683
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003684 if (size & 3 || *pos & 0x3)
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003685 return -EINVAL;
3686
Samuel Pitoiset3cbc6142017-02-15 19:32:29 +01003687 if (amdgpu_dpm == 0)
3688 return -EINVAL;
3689
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003690 /* convert offset to sensor number */
3691 idx = *pos >> 2;
3692
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003693 valuesize = sizeof(values);
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003694 if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->read_sensor)
Rex Zhucd4d7462017-09-06 18:43:52 +08003695 r = amdgpu_dpm_read_sensor(adev, idx, &values[0], &valuesize);
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003696 else
3697 return -EINVAL;
3698
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003699 if (size > valuesize)
3700 return -EINVAL;
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003701
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003702 outsize = 0;
3703 x = 0;
3704 if (!r) {
3705 while (size) {
3706 r = put_user(values[x++], (int32_t *)buf);
3707 buf += 4;
3708 size -= 4;
3709 outsize += 4;
3710 }
3711 }
3712
3713 return !r ? outsize : r;
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003714}
Tom St Denis1e051412016-06-27 09:57:18 -04003715
Tom St Denis273d7aa2016-10-11 14:48:55 -04003716static ssize_t amdgpu_debugfs_wave_read(struct file *f, char __user *buf,
3717 size_t size, loff_t *pos)
3718{
3719 struct amdgpu_device *adev = f->f_inode->i_private;
3720 int r, x;
3721 ssize_t result=0;
Tom St Denis472259f2016-10-14 09:49:09 -04003722 uint32_t offset, se, sh, cu, wave, simd, data[32];
Tom St Denis273d7aa2016-10-11 14:48:55 -04003723
3724 if (size & 3 || *pos & 3)
3725 return -EINVAL;
3726
3727 /* decode offset */
Tom St Denis0b968652017-11-10 12:54:50 -05003728 offset = (*pos & GENMASK_ULL(6, 0));
3729 se = (*pos & GENMASK_ULL(14, 7)) >> 7;
3730 sh = (*pos & GENMASK_ULL(22, 15)) >> 15;
3731 cu = (*pos & GENMASK_ULL(30, 23)) >> 23;
3732 wave = (*pos & GENMASK_ULL(36, 31)) >> 31;
3733 simd = (*pos & GENMASK_ULL(44, 37)) >> 37;
Tom St Denis273d7aa2016-10-11 14:48:55 -04003734
3735 /* switch to the specific se/sh/cu */
3736 mutex_lock(&adev->grbm_idx_mutex);
3737 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
3738
3739 x = 0;
Tom St Denis472259f2016-10-14 09:49:09 -04003740 if (adev->gfx.funcs->read_wave_data)
3741 adev->gfx.funcs->read_wave_data(adev, simd, wave, data, &x);
Tom St Denis273d7aa2016-10-11 14:48:55 -04003742
3743 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
3744 mutex_unlock(&adev->grbm_idx_mutex);
3745
Tom St Denis5ecfb3b2016-10-13 12:15:03 -04003746 if (!x)
3747 return -EINVAL;
3748
Tom St Denis472259f2016-10-14 09:49:09 -04003749 while (size && (offset < x * 4)) {
Tom St Denis273d7aa2016-10-11 14:48:55 -04003750 uint32_t value;
3751
Tom St Denis472259f2016-10-14 09:49:09 -04003752 value = data[offset >> 2];
Tom St Denis273d7aa2016-10-11 14:48:55 -04003753 r = put_user(value, (uint32_t *)buf);
3754 if (r)
3755 return r;
3756
3757 result += 4;
3758 buf += 4;
Tom St Denis472259f2016-10-14 09:49:09 -04003759 offset += 4;
Tom St Denis273d7aa2016-10-11 14:48:55 -04003760 size -= 4;
3761 }
3762
3763 return result;
3764}
3765
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003766static ssize_t amdgpu_debugfs_gpr_read(struct file *f, char __user *buf,
3767 size_t size, loff_t *pos)
3768{
3769 struct amdgpu_device *adev = f->f_inode->i_private;
3770 int r;
3771 ssize_t result = 0;
3772 uint32_t offset, se, sh, cu, wave, simd, thread, bank, *data;
3773
3774 if (size & 3 || *pos & 3)
3775 return -EINVAL;
3776
3777 /* decode offset */
Tom St Denis0b968652017-11-10 12:54:50 -05003778 offset = *pos & GENMASK_ULL(11, 0);
3779 se = (*pos & GENMASK_ULL(19, 12)) >> 12;
3780 sh = (*pos & GENMASK_ULL(27, 20)) >> 20;
3781 cu = (*pos & GENMASK_ULL(35, 28)) >> 28;
3782 wave = (*pos & GENMASK_ULL(43, 36)) >> 36;
3783 simd = (*pos & GENMASK_ULL(51, 44)) >> 44;
3784 thread = (*pos & GENMASK_ULL(59, 52)) >> 52;
3785 bank = (*pos & GENMASK_ULL(61, 60)) >> 60;
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003786
3787 data = kmalloc_array(1024, sizeof(*data), GFP_KERNEL);
3788 if (!data)
3789 return -ENOMEM;
3790
3791 /* switch to the specific se/sh/cu */
3792 mutex_lock(&adev->grbm_idx_mutex);
3793 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
3794
3795 if (bank == 0) {
3796 if (adev->gfx.funcs->read_wave_vgprs)
3797 adev->gfx.funcs->read_wave_vgprs(adev, simd, wave, thread, offset, size>>2, data);
3798 } else {
3799 if (adev->gfx.funcs->read_wave_sgprs)
3800 adev->gfx.funcs->read_wave_sgprs(adev, simd, wave, offset, size>>2, data);
3801 }
3802
3803 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
3804 mutex_unlock(&adev->grbm_idx_mutex);
3805
3806 while (size) {
3807 uint32_t value;
3808
3809 value = data[offset++];
3810 r = put_user(value, (uint32_t *)buf);
3811 if (r) {
3812 result = r;
3813 goto err;
3814 }
3815
3816 result += 4;
3817 buf += 4;
3818 size -= 4;
3819 }
3820
3821err:
3822 kfree(data);
3823 return result;
3824}
3825
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003826static const struct file_operations amdgpu_debugfs_regs_fops = {
3827 .owner = THIS_MODULE,
3828 .read = amdgpu_debugfs_regs_read,
3829 .write = amdgpu_debugfs_regs_write,
3830 .llseek = default_llseek
3831};
Tom St Denisadcec282016-04-15 13:08:44 -04003832static const struct file_operations amdgpu_debugfs_regs_didt_fops = {
3833 .owner = THIS_MODULE,
3834 .read = amdgpu_debugfs_regs_didt_read,
3835 .write = amdgpu_debugfs_regs_didt_write,
3836 .llseek = default_llseek
3837};
3838static const struct file_operations amdgpu_debugfs_regs_pcie_fops = {
3839 .owner = THIS_MODULE,
3840 .read = amdgpu_debugfs_regs_pcie_read,
3841 .write = amdgpu_debugfs_regs_pcie_write,
3842 .llseek = default_llseek
3843};
3844static const struct file_operations amdgpu_debugfs_regs_smc_fops = {
3845 .owner = THIS_MODULE,
3846 .read = amdgpu_debugfs_regs_smc_read,
3847 .write = amdgpu_debugfs_regs_smc_write,
3848 .llseek = default_llseek
3849};
3850
Tom St Denis1e051412016-06-27 09:57:18 -04003851static const struct file_operations amdgpu_debugfs_gca_config_fops = {
3852 .owner = THIS_MODULE,
3853 .read = amdgpu_debugfs_gca_config_read,
3854 .llseek = default_llseek
3855};
3856
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003857static const struct file_operations amdgpu_debugfs_sensors_fops = {
3858 .owner = THIS_MODULE,
3859 .read = amdgpu_debugfs_sensor_read,
3860 .llseek = default_llseek
3861};
3862
Tom St Denis273d7aa2016-10-11 14:48:55 -04003863static const struct file_operations amdgpu_debugfs_wave_fops = {
3864 .owner = THIS_MODULE,
3865 .read = amdgpu_debugfs_wave_read,
3866 .llseek = default_llseek
3867};
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003868static const struct file_operations amdgpu_debugfs_gpr_fops = {
3869 .owner = THIS_MODULE,
3870 .read = amdgpu_debugfs_gpr_read,
3871 .llseek = default_llseek
3872};
Tom St Denis273d7aa2016-10-11 14:48:55 -04003873
Tom St Denisadcec282016-04-15 13:08:44 -04003874static const struct file_operations *debugfs_regs[] = {
3875 &amdgpu_debugfs_regs_fops,
3876 &amdgpu_debugfs_regs_didt_fops,
3877 &amdgpu_debugfs_regs_pcie_fops,
3878 &amdgpu_debugfs_regs_smc_fops,
Tom St Denis1e051412016-06-27 09:57:18 -04003879 &amdgpu_debugfs_gca_config_fops,
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003880 &amdgpu_debugfs_sensors_fops,
Tom St Denis273d7aa2016-10-11 14:48:55 -04003881 &amdgpu_debugfs_wave_fops,
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003882 &amdgpu_debugfs_gpr_fops,
Tom St Denisadcec282016-04-15 13:08:44 -04003883};
3884
3885static const char *debugfs_regs_names[] = {
3886 "amdgpu_regs",
3887 "amdgpu_regs_didt",
3888 "amdgpu_regs_pcie",
3889 "amdgpu_regs_smc",
Tom St Denis1e051412016-06-27 09:57:18 -04003890 "amdgpu_gca_config",
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003891 "amdgpu_sensors",
Tom St Denis273d7aa2016-10-11 14:48:55 -04003892 "amdgpu_wave",
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003893 "amdgpu_gpr",
Tom St Denisadcec282016-04-15 13:08:44 -04003894};
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003895
3896static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
3897{
3898 struct drm_minor *minor = adev->ddev->primary;
3899 struct dentry *ent, *root = minor->debugfs_root;
Tom St Denisadcec282016-04-15 13:08:44 -04003900 unsigned i, j;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003901
Tom St Denisadcec282016-04-15 13:08:44 -04003902 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3903 ent = debugfs_create_file(debugfs_regs_names[i],
3904 S_IFREG | S_IRUGO, root,
3905 adev, debugfs_regs[i]);
3906 if (IS_ERR(ent)) {
3907 for (j = 0; j < i; j++) {
3908 debugfs_remove(adev->debugfs_regs[i]);
3909 adev->debugfs_regs[i] = NULL;
3910 }
3911 return PTR_ERR(ent);
3912 }
3913
3914 if (!i)
3915 i_size_write(ent->d_inode, adev->rmmio_size);
3916 adev->debugfs_regs[i] = ent;
3917 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003918
3919 return 0;
3920}
3921
3922static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev)
3923{
Tom St Denisadcec282016-04-15 13:08:44 -04003924 unsigned i;
3925
3926 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3927 if (adev->debugfs_regs[i]) {
3928 debugfs_remove(adev->debugfs_regs[i]);
3929 adev->debugfs_regs[i] = NULL;
3930 }
3931 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003932}
3933
Huang Rui4f0955f2017-05-10 23:04:06 +08003934static int amdgpu_debugfs_test_ib(struct seq_file *m, void *data)
3935{
3936 struct drm_info_node *node = (struct drm_info_node *) m->private;
3937 struct drm_device *dev = node->minor->dev;
3938 struct amdgpu_device *adev = dev->dev_private;
3939 int r = 0, i;
3940
3941 /* hold on the scheduler */
3942 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
3943 struct amdgpu_ring *ring = adev->rings[i];
3944
3945 if (!ring || !ring->sched.thread)
3946 continue;
3947 kthread_park(ring->sched.thread);
3948 }
3949
3950 seq_printf(m, "run ib test:\n");
3951 r = amdgpu_ib_ring_tests(adev);
3952 if (r)
3953 seq_printf(m, "ib ring tests failed (%d).\n", r);
3954 else
3955 seq_printf(m, "ib ring tests passed.\n");
3956
3957 /* go on the scheduler */
3958 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
3959 struct amdgpu_ring *ring = adev->rings[i];
3960
3961 if (!ring || !ring->sched.thread)
3962 continue;
3963 kthread_unpark(ring->sched.thread);
3964 }
3965
3966 return 0;
3967}
3968
Kent Russelldb95e212017-08-22 12:31:43 -04003969static int amdgpu_debugfs_get_vbios_dump(struct seq_file *m, void *data)
3970{
3971 struct drm_info_node *node = (struct drm_info_node *) m->private;
3972 struct drm_device *dev = node->minor->dev;
3973 struct amdgpu_device *adev = dev->dev_private;
3974
3975 seq_write(m, adev->bios, adev->bios_size);
3976 return 0;
3977}
3978
Christian König79588d22017-12-06 16:24:49 +01003979static int amdgpu_debugfs_evict_vram(struct seq_file *m, void *data)
3980{
3981 struct drm_info_node *node = (struct drm_info_node *)m->private;
3982 struct drm_device *dev = node->minor->dev;
3983 struct amdgpu_device *adev = dev->dev_private;
3984
3985 seq_printf(m, "(%d)\n", amdgpu_bo_evict_vram(adev));
3986 return 0;
3987}
3988
Christian König763efb62017-12-06 15:44:51 +01003989static const struct drm_info_list amdgpu_debugfs_list[] = {
3990 {"amdgpu_vbios", amdgpu_debugfs_get_vbios_dump},
Christian König79588d22017-12-06 16:24:49 +01003991 {"amdgpu_test_ib", &amdgpu_debugfs_test_ib},
3992 {"amdgpu_evict_vram", &amdgpu_debugfs_evict_vram}
Kent Russelldb95e212017-08-22 12:31:43 -04003993};
3994
Christian König763efb62017-12-06 15:44:51 +01003995static int amdgpu_debugfs_init(struct amdgpu_device *adev)
Kent Russelldb95e212017-08-22 12:31:43 -04003996{
Christian König763efb62017-12-06 15:44:51 +01003997 return amdgpu_debugfs_add_files(adev, amdgpu_debugfs_list,
3998 ARRAY_SIZE(amdgpu_debugfs_list));
Kent Russelldb95e212017-08-22 12:31:43 -04003999}
Christian König763efb62017-12-06 15:44:51 +01004000
Alexander Kuleshov7cebc722015-06-27 13:16:05 +06004001#else
Christian König763efb62017-12-06 15:44:51 +01004002static int amdgpu_debugfs_init(struct amdgpu_device *adev)
Huang Rui4f0955f2017-05-10 23:04:06 +08004003{
4004 return 0;
4005}
Alexander Kuleshov7cebc722015-06-27 13:16:05 +06004006static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
4007{
4008 return 0;
4009}
4010static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev) { }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04004011#endif