blob: 70c9e5756b02a66fdda95f5340ad860337ec1328 [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);
Huang Rui4f0955f2017-05-10 23:04:06 +080069static int amdgpu_debugfs_test_ib_ring_init(struct amdgpu_device *adev);
Kent Russelldb95e212017-08-22 12:31:43 -040070static int amdgpu_debugfs_vbios_dump_init(struct amdgpu_device *adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -040071
72static const char *amdgpu_asic_name[] = {
Ken Wangda69c1612016-01-21 19:08:55 +080073 "TAHITI",
74 "PITCAIRN",
75 "VERDE",
76 "OLAND",
77 "HAINAN",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040078 "BONAIRE",
79 "KAVERI",
80 "KABINI",
81 "HAWAII",
82 "MULLINS",
83 "TOPAZ",
84 "TONGA",
David Zhang48299f92015-07-08 01:05:16 +080085 "FIJI",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040086 "CARRIZO",
Samuel Li139f4912015-10-08 14:50:27 -040087 "STONEY",
Flora Cui2cc0c0b2016-03-14 18:33:29 -040088 "POLARIS10",
89 "POLARIS11",
Junwei Zhangc4642a42016-12-14 15:32:28 -050090 "POLARIS12",
Ken Wangd4196f02016-03-09 09:28:32 +080091 "VEGA10",
Chunming Zhou2ca8a5d2016-12-07 17:31:19 +080092 "RAVEN",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040093 "LAST",
94};
95
96bool amdgpu_device_is_px(struct drm_device *dev)
97{
98 struct amdgpu_device *adev = dev->dev_private;
99
Jammy Zhou2f7d10b2015-07-22 11:29:01 +0800100 if (adev->flags & AMD_IS_PX)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400101 return true;
102 return false;
103}
104
105/*
106 * MMIO register access helper functions.
107 */
108uint32_t amdgpu_mm_rreg(struct amdgpu_device *adev, uint32_t reg,
Monk Liu15d72fd2017-01-25 15:07:40 +0800109 uint32_t acc_flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400110{
Tom St Denisf4b373f2016-05-31 08:02:27 -0400111 uint32_t ret;
112
pding43ca8ef2017-10-13 15:38:35 +0800113 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev))
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800114 return amdgpu_virt_kiq_rreg(adev, reg);
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800115
Monk Liu15d72fd2017-01-25 15:07:40 +0800116 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
Tom St Denisf4b373f2016-05-31 08:02:27 -0400117 ret = readl(((void __iomem *)adev->rmmio) + (reg * 4));
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400118 else {
119 unsigned long flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400120
121 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
122 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
123 ret = readl(((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
124 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400125 }
Tom St Denisf4b373f2016-05-31 08:02:27 -0400126 trace_amdgpu_mm_rreg(adev->pdev->device, reg, ret);
127 return ret;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400128}
129
130void amdgpu_mm_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v,
Monk Liu15d72fd2017-01-25 15:07:40 +0800131 uint32_t acc_flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400132{
Tom St Denisf4b373f2016-05-31 08:02:27 -0400133 trace_amdgpu_mm_wreg(adev->pdev->device, reg, v);
Monk Liu4e99a442016-03-31 13:26:59 +0800134
Ken Wang47ed4e12017-07-04 13:11:52 +0800135 if (adev->asic_type >= CHIP_VEGA10 && reg == 0) {
136 adev->last_mm_index = v;
137 }
138
pding43ca8ef2017-10-13 15:38:35 +0800139 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev))
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800140 return amdgpu_virt_kiq_wreg(adev, reg, v);
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800141
Monk Liu15d72fd2017-01-25 15:07:40 +0800142 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400143 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
144 else {
145 unsigned long flags;
146
147 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
148 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
149 writel(v, ((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
150 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
151 }
Ken Wang47ed4e12017-07-04 13:11:52 +0800152
153 if (adev->asic_type >= CHIP_VEGA10 && reg == 1 && adev->last_mm_index == 0x5702C) {
154 udelay(500);
155 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400156}
157
158u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 reg)
159{
160 if ((reg * 4) < adev->rio_mem_size)
161 return ioread32(adev->rio_mem + (reg * 4));
162 else {
163 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
164 return ioread32(adev->rio_mem + (mmMM_DATA * 4));
165 }
166}
167
168void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
169{
Ken Wang47ed4e12017-07-04 13:11:52 +0800170 if (adev->asic_type >= CHIP_VEGA10 && reg == 0) {
171 adev->last_mm_index = v;
172 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400173
174 if ((reg * 4) < adev->rio_mem_size)
175 iowrite32(v, adev->rio_mem + (reg * 4));
176 else {
177 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
178 iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
179 }
Ken Wang47ed4e12017-07-04 13:11:52 +0800180
181 if (adev->asic_type >= CHIP_VEGA10 && reg == 1 && adev->last_mm_index == 0x5702C) {
182 udelay(500);
183 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400184}
185
186/**
187 * amdgpu_mm_rdoorbell - read a doorbell dword
188 *
189 * @adev: amdgpu_device pointer
190 * @index: doorbell index
191 *
192 * Returns the value in the doorbell aperture at the
193 * requested doorbell index (CIK).
194 */
195u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
196{
197 if (index < adev->doorbell.num_doorbells) {
198 return readl(adev->doorbell.ptr + index);
199 } else {
200 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
201 return 0;
202 }
203}
204
205/**
206 * amdgpu_mm_wdoorbell - write a doorbell dword
207 *
208 * @adev: amdgpu_device pointer
209 * @index: doorbell index
210 * @v: value to write
211 *
212 * Writes @v to the doorbell aperture at the
213 * requested doorbell index (CIK).
214 */
215void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
216{
217 if (index < adev->doorbell.num_doorbells) {
218 writel(v, adev->doorbell.ptr + index);
219 } else {
220 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
221 }
222}
223
224/**
Ken Wang832be402016-03-18 15:23:08 +0800225 * amdgpu_mm_rdoorbell64 - read a doorbell Qword
226 *
227 * @adev: amdgpu_device pointer
228 * @index: doorbell index
229 *
230 * Returns the value in the doorbell aperture at the
231 * requested doorbell index (VEGA10+).
232 */
233u64 amdgpu_mm_rdoorbell64(struct amdgpu_device *adev, u32 index)
234{
235 if (index < adev->doorbell.num_doorbells) {
236 return atomic64_read((atomic64_t *)(adev->doorbell.ptr + index));
237 } else {
238 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
239 return 0;
240 }
241}
242
243/**
244 * amdgpu_mm_wdoorbell64 - write a doorbell Qword
245 *
246 * @adev: amdgpu_device pointer
247 * @index: doorbell index
248 * @v: value to write
249 *
250 * Writes @v to the doorbell aperture at the
251 * requested doorbell index (VEGA10+).
252 */
253void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v)
254{
255 if (index < adev->doorbell.num_doorbells) {
256 atomic64_set((atomic64_t *)(adev->doorbell.ptr + index), v);
257 } else {
258 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
259 }
260}
261
262/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400263 * amdgpu_invalid_rreg - dummy reg read function
264 *
265 * @adev: amdgpu device pointer
266 * @reg: offset of register
267 *
268 * Dummy register read function. Used for register blocks
269 * that certain asics don't have (all asics).
270 * Returns the value in the register.
271 */
272static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
273{
274 DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
275 BUG();
276 return 0;
277}
278
279/**
280 * amdgpu_invalid_wreg - dummy reg write function
281 *
282 * @adev: amdgpu device pointer
283 * @reg: offset of register
284 * @v: value to write to the register
285 *
286 * Dummy register read function. Used for register blocks
287 * that certain asics don't have (all asics).
288 */
289static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
290{
291 DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
292 reg, v);
293 BUG();
294}
295
296/**
297 * amdgpu_block_invalid_rreg - dummy reg read function
298 *
299 * @adev: amdgpu device pointer
300 * @block: offset of instance
301 * @reg: offset of register
302 *
303 * Dummy register read function. Used for register blocks
304 * that certain asics don't have (all asics).
305 * Returns the value in the register.
306 */
307static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
308 uint32_t block, uint32_t reg)
309{
310 DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
311 reg, block);
312 BUG();
313 return 0;
314}
315
316/**
317 * amdgpu_block_invalid_wreg - dummy reg write function
318 *
319 * @adev: amdgpu device pointer
320 * @block: offset of instance
321 * @reg: offset of register
322 * @v: value to write to the register
323 *
324 * Dummy register read function. Used for register blocks
325 * that certain asics don't have (all asics).
326 */
327static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
328 uint32_t block,
329 uint32_t reg, uint32_t v)
330{
331 DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
332 reg, block, v);
333 BUG();
334}
335
336static int amdgpu_vram_scratch_init(struct amdgpu_device *adev)
337{
Christian Königa4a02772017-07-27 17:24:36 +0200338 return amdgpu_bo_create_kernel(adev, AMDGPU_GPU_PAGE_SIZE,
339 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
340 &adev->vram_scratch.robj,
341 &adev->vram_scratch.gpu_addr,
342 (void **)&adev->vram_scratch.ptr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400343}
344
345static void amdgpu_vram_scratch_fini(struct amdgpu_device *adev)
346{
Christian König078af1a2017-07-27 17:43:00 +0200347 amdgpu_bo_free_kernel(&adev->vram_scratch.robj, NULL, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400348}
349
350/**
351 * amdgpu_program_register_sequence - program an array of registers.
352 *
353 * @adev: amdgpu_device pointer
354 * @registers: pointer to the register array
355 * @array_size: size of the register array
356 *
357 * Programs an array or registers with and and or masks.
358 * This is a helper for setting golden registers.
359 */
360void amdgpu_program_register_sequence(struct amdgpu_device *adev,
361 const u32 *registers,
362 const u32 array_size)
363{
364 u32 tmp, reg, and_mask, or_mask;
365 int i;
366
367 if (array_size % 3)
368 return;
369
370 for (i = 0; i < array_size; i +=3) {
371 reg = registers[i + 0];
372 and_mask = registers[i + 1];
373 or_mask = registers[i + 2];
374
375 if (and_mask == 0xffffffff) {
376 tmp = or_mask;
377 } else {
378 tmp = RREG32(reg);
379 tmp &= ~and_mask;
380 tmp |= or_mask;
381 }
382 WREG32(reg, tmp);
383 }
384}
385
386void amdgpu_pci_config_reset(struct amdgpu_device *adev)
387{
388 pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
389}
390
391/*
392 * GPU doorbell aperture helpers function.
393 */
394/**
395 * amdgpu_doorbell_init - Init doorbell driver information.
396 *
397 * @adev: amdgpu_device pointer
398 *
399 * Init doorbell driver information (CIK)
400 * Returns 0 on success, error on failure.
401 */
402static int amdgpu_doorbell_init(struct amdgpu_device *adev)
403{
Christian König705e5192017-06-08 11:15:16 +0200404 /* No doorbell on SI hardware generation */
405 if (adev->asic_type < CHIP_BONAIRE) {
406 adev->doorbell.base = 0;
407 adev->doorbell.size = 0;
408 adev->doorbell.num_doorbells = 0;
409 adev->doorbell.ptr = NULL;
410 return 0;
411 }
412
Christian Königd6895ad2017-02-28 10:36:43 +0100413 if (pci_resource_flags(adev->pdev, 2) & IORESOURCE_UNSET)
414 return -EINVAL;
415
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400416 /* doorbell bar mapping */
417 adev->doorbell.base = pci_resource_start(adev->pdev, 2);
418 adev->doorbell.size = pci_resource_len(adev->pdev, 2);
419
Christian Königedf600d2016-05-03 15:54:54 +0200420 adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400421 AMDGPU_DOORBELL_MAX_ASSIGNMENT+1);
422 if (adev->doorbell.num_doorbells == 0)
423 return -EINVAL;
424
Christian König8972e5d2017-03-06 13:34:57 +0100425 adev->doorbell.ptr = ioremap(adev->doorbell.base,
426 adev->doorbell.num_doorbells *
427 sizeof(u32));
428 if (adev->doorbell.ptr == NULL)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400429 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400430
431 return 0;
432}
433
434/**
435 * amdgpu_doorbell_fini - Tear down doorbell driver information.
436 *
437 * @adev: amdgpu_device pointer
438 *
439 * Tear down doorbell driver information (CIK)
440 */
441static void amdgpu_doorbell_fini(struct amdgpu_device *adev)
442{
443 iounmap(adev->doorbell.ptr);
444 adev->doorbell.ptr = NULL;
445}
446
447/**
448 * amdgpu_doorbell_get_kfd_info - Report doorbell configuration required to
449 * setup amdkfd
450 *
451 * @adev: amdgpu_device pointer
452 * @aperture_base: output returning doorbell aperture base physical address
453 * @aperture_size: output returning doorbell aperture size in bytes
454 * @start_offset: output returning # of doorbell bytes reserved for amdgpu.
455 *
456 * amdgpu and amdkfd share the doorbell aperture. amdgpu sets it up,
457 * takes doorbells required for its own rings and reports the setup to amdkfd.
458 * amdgpu reserved doorbells are at the start of the doorbell aperture.
459 */
460void amdgpu_doorbell_get_kfd_info(struct amdgpu_device *adev,
461 phys_addr_t *aperture_base,
462 size_t *aperture_size,
463 size_t *start_offset)
464{
465 /*
466 * The first num_doorbells are used by amdgpu.
467 * amdkfd takes whatever's left in the aperture.
468 */
469 if (adev->doorbell.size > adev->doorbell.num_doorbells * sizeof(u32)) {
470 *aperture_base = adev->doorbell.base;
471 *aperture_size = adev->doorbell.size;
472 *start_offset = adev->doorbell.num_doorbells * sizeof(u32);
473 } else {
474 *aperture_base = 0;
475 *aperture_size = 0;
476 *start_offset = 0;
477 }
478}
479
480/*
481 * amdgpu_wb_*()
Alex Xie455a7bc2017-05-08 21:36:03 -0400482 * Writeback is the method by which the GPU updates special pages in memory
Alex Xieea81a172017-05-08 13:41:11 -0400483 * with the status of certain GPU events (fences, ring pointers,etc.).
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400484 */
485
486/**
487 * amdgpu_wb_fini - Disable Writeback and free memory
488 *
489 * @adev: amdgpu_device pointer
490 *
491 * Disables Writeback and frees the Writeback memory (all asics).
492 * Used at driver shutdown.
493 */
494static void amdgpu_wb_fini(struct amdgpu_device *adev)
495{
496 if (adev->wb.wb_obj) {
Alex Deuchera76ed482016-10-21 15:30:36 -0400497 amdgpu_bo_free_kernel(&adev->wb.wb_obj,
498 &adev->wb.gpu_addr,
499 (void **)&adev->wb.wb);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400500 adev->wb.wb_obj = NULL;
501 }
502}
503
504/**
505 * amdgpu_wb_init- Init Writeback driver info and allocate memory
506 *
507 * @adev: amdgpu_device pointer
508 *
Alex Xie455a7bc2017-05-08 21:36:03 -0400509 * Initializes writeback and allocates writeback memory (all asics).
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400510 * Used at driver startup.
511 * Returns 0 on success or an -error on failure.
512 */
513static int amdgpu_wb_init(struct amdgpu_device *adev)
514{
515 int r;
516
517 if (adev->wb.wb_obj == NULL) {
Alex Deucher97407b62017-07-28 12:14:15 -0400518 /* AMDGPU_MAX_WB * sizeof(uint32_t) * 8 = AMDGPU_MAX_WB 256bit slots */
519 r = amdgpu_bo_create_kernel(adev, AMDGPU_MAX_WB * sizeof(uint32_t) * 8,
Alex Deuchera76ed482016-10-21 15:30:36 -0400520 PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
521 &adev->wb.wb_obj, &adev->wb.gpu_addr,
522 (void **)&adev->wb.wb);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400523 if (r) {
524 dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
525 return r;
526 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400527
528 adev->wb.num_wb = AMDGPU_MAX_WB;
529 memset(&adev->wb.used, 0, sizeof(adev->wb.used));
530
531 /* clear wb memory */
Huang Rui60a970a62017-03-15 10:13:32 +0800532 memset((char *)adev->wb.wb, 0, AMDGPU_MAX_WB * sizeof(uint32_t));
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400533 }
534
535 return 0;
536}
537
538/**
539 * amdgpu_wb_get - Allocate a wb entry
540 *
541 * @adev: amdgpu_device pointer
542 * @wb: wb index
543 *
544 * Allocate a wb slot for use by the driver (all asics).
545 * Returns 0 on success or -EINVAL on failure.
546 */
547int amdgpu_wb_get(struct amdgpu_device *adev, u32 *wb)
548{
549 unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
Alex Deucher97407b62017-07-28 12:14:15 -0400550
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400551 if (offset < adev->wb.num_wb) {
552 __set_bit(offset, adev->wb.used);
Monk Liu63ae07c2017-10-17 19:18:56 +0800553 *wb = offset << 3; /* convert to dw offset */
Monk Liu0915fdb2017-06-19 10:19:41 -0400554 return 0;
555 } else {
556 return -EINVAL;
557 }
558}
559
Ken Wang70142852016-03-18 15:08:49 +0800560/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400561 * amdgpu_wb_free - Free a wb entry
562 *
563 * @adev: amdgpu_device pointer
564 * @wb: wb index
565 *
566 * Free a wb slot allocated for use by the driver (all asics)
567 */
568void amdgpu_wb_free(struct amdgpu_device *adev, u32 wb)
569{
570 if (wb < adev->wb.num_wb)
Monk Liu63ae07c2017-10-17 19:18:56 +0800571 __clear_bit(wb >> 3, adev->wb.used);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400572}
573
574/**
575 * amdgpu_vram_location - try to find VRAM location
576 * @adev: amdgpu device structure holding all necessary informations
577 * @mc: memory controller structure holding memory informations
578 * @base: base address at which to put VRAM
579 *
Alex Xie455a7bc2017-05-08 21:36:03 -0400580 * Function will try to place VRAM at base address provided
Christian König3d647c82017-11-16 19:36:10 +0100581 * as parameter.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400582 */
583void amdgpu_vram_location(struct amdgpu_device *adev, struct amdgpu_mc *mc, u64 base)
584{
585 uint64_t limit = (uint64_t)amdgpu_vram_limit << 20;
586
587 mc->vram_start = base;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400588 mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
589 if (limit && limit < mc->real_vram_size)
590 mc->real_vram_size = limit;
591 dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
592 mc->mc_vram_size >> 20, mc->vram_start,
593 mc->vram_end, mc->real_vram_size >> 20);
594}
595
596/**
Christian König6f02a692017-07-07 11:56:59 +0200597 * amdgpu_gart_location - try to find GTT location
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400598 * @adev: amdgpu device structure holding all necessary informations
599 * @mc: memory controller structure holding memory informations
600 *
601 * Function will place try to place GTT before or after VRAM.
602 *
603 * If GTT size is bigger than space left then we ajust GTT size.
604 * Thus function will never fails.
605 *
606 * FIXME: when reducing GTT size align new size on power of 2.
607 */
Christian König6f02a692017-07-07 11:56:59 +0200608void amdgpu_gart_location(struct amdgpu_device *adev, struct amdgpu_mc *mc)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400609{
610 u64 size_af, size_bf;
611
Christian Königed21c042017-07-06 22:26:05 +0200612 size_af = adev->mc.mc_mask - mc->vram_end;
613 size_bf = mc->vram_start;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400614 if (size_bf > size_af) {
Christian König6f02a692017-07-07 11:56:59 +0200615 if (mc->gart_size > size_bf) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400616 dev_warn(adev->dev, "limiting GTT\n");
Christian König6f02a692017-07-07 11:56:59 +0200617 mc->gart_size = size_bf;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400618 }
Christian König6f02a692017-07-07 11:56:59 +0200619 mc->gart_start = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400620 } else {
Christian König6f02a692017-07-07 11:56:59 +0200621 if (mc->gart_size > size_af) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400622 dev_warn(adev->dev, "limiting GTT\n");
Christian König6f02a692017-07-07 11:56:59 +0200623 mc->gart_size = size_af;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400624 }
Christian Königb98f1b92017-11-16 20:12:51 +0100625 /* VCE doesn't like it when BOs cross a 4GB segment, so align
626 * the GART base on a 4GB boundary as well.
627 */
628 mc->gart_start = ALIGN(mc->vram_end + 1, 0x100000000ULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400629 }
Christian König6f02a692017-07-07 11:56:59 +0200630 mc->gart_end = mc->gart_start + mc->gart_size - 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400631 dev_info(adev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
Christian König6f02a692017-07-07 11:56:59 +0200632 mc->gart_size >> 20, mc->gart_start, mc->gart_end);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400633}
634
635/*
Horace Chena05502e2017-09-29 14:41:57 +0800636 * Firmware Reservation functions
637 */
638/**
639 * amdgpu_fw_reserve_vram_fini - free fw reserved vram
640 *
641 * @adev: amdgpu_device pointer
642 *
643 * free fw reserved vram if it has been reserved.
644 */
645void amdgpu_fw_reserve_vram_fini(struct amdgpu_device *adev)
646{
647 amdgpu_bo_free_kernel(&adev->fw_vram_usage.reserved_bo,
648 NULL, &adev->fw_vram_usage.va);
649}
650
651/**
652 * amdgpu_fw_reserve_vram_init - create bo vram reservation from fw
653 *
654 * @adev: amdgpu_device pointer
655 *
656 * create bo vram reservation from fw.
657 */
658int amdgpu_fw_reserve_vram_init(struct amdgpu_device *adev)
659{
Christian Königc13c55d2017-04-12 15:33:00 +0200660 struct ttm_operation_ctx ctx = { false, false };
Horace Chena05502e2017-09-29 14:41:57 +0800661 int r = 0;
Horace Chen3c738892017-11-01 19:32:11 +0800662 int i;
Horace Chena05502e2017-09-29 14:41:57 +0800663 u64 vram_size = adev->mc.visible_vram_size;
Horace Chen3c738892017-11-01 19:32:11 +0800664 u64 offset = adev->fw_vram_usage.start_offset;
665 u64 size = adev->fw_vram_usage.size;
666 struct amdgpu_bo *bo;
Horace Chena05502e2017-09-29 14:41:57 +0800667
668 adev->fw_vram_usage.va = NULL;
669 adev->fw_vram_usage.reserved_bo = NULL;
670
671 if (adev->fw_vram_usage.size > 0 &&
672 adev->fw_vram_usage.size <= vram_size) {
673
674 r = amdgpu_bo_create(adev, adev->fw_vram_usage.size,
Horace Chen3c738892017-11-01 19:32:11 +0800675 PAGE_SIZE, true, AMDGPU_GEM_DOMAIN_VRAM,
Horace Chena05502e2017-09-29 14:41:57 +0800676 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
677 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS, NULL, NULL, 0,
678 &adev->fw_vram_usage.reserved_bo);
679 if (r)
680 goto error_create;
681
682 r = amdgpu_bo_reserve(adev->fw_vram_usage.reserved_bo, false);
683 if (r)
684 goto error_reserve;
Horace Chen3c738892017-11-01 19:32:11 +0800685
686 /* remove the original mem node and create a new one at the
687 * request position
688 */
689 bo = adev->fw_vram_usage.reserved_bo;
690 offset = ALIGN(offset, PAGE_SIZE);
691 for (i = 0; i < bo->placement.num_placement; ++i) {
692 bo->placements[i].fpfn = offset >> PAGE_SHIFT;
693 bo->placements[i].lpfn = (offset + size) >> PAGE_SHIFT;
694 }
695
696 ttm_bo_mem_put(&bo->tbo, &bo->tbo.mem);
Christian Königc13c55d2017-04-12 15:33:00 +0200697 r = ttm_bo_mem_space(&bo->tbo, &bo->placement,
698 &bo->tbo.mem, &ctx);
Horace Chen3c738892017-11-01 19:32:11 +0800699 if (r)
700 goto error_pin;
701
Horace Chena05502e2017-09-29 14:41:57 +0800702 r = amdgpu_bo_pin_restricted(adev->fw_vram_usage.reserved_bo,
703 AMDGPU_GEM_DOMAIN_VRAM,
704 adev->fw_vram_usage.start_offset,
705 (adev->fw_vram_usage.start_offset +
Monk Liu99211672017-11-14 16:50:31 +0800706 adev->fw_vram_usage.size), NULL);
Horace Chena05502e2017-09-29 14:41:57 +0800707 if (r)
708 goto error_pin;
709 r = amdgpu_bo_kmap(adev->fw_vram_usage.reserved_bo,
710 &adev->fw_vram_usage.va);
711 if (r)
712 goto error_kmap;
713
714 amdgpu_bo_unreserve(adev->fw_vram_usage.reserved_bo);
715 }
716 return r;
717
718error_kmap:
719 amdgpu_bo_unpin(adev->fw_vram_usage.reserved_bo);
720error_pin:
721 amdgpu_bo_unreserve(adev->fw_vram_usage.reserved_bo);
722error_reserve:
723 amdgpu_bo_unref(&adev->fw_vram_usage.reserved_bo);
724error_create:
725 adev->fw_vram_usage.va = NULL;
726 adev->fw_vram_usage.reserved_bo = NULL;
727 return r;
728}
729
Christian Königd6895ad2017-02-28 10:36:43 +0100730/**
731 * amdgpu_device_resize_fb_bar - try to resize FB BAR
732 *
733 * @adev: amdgpu_device pointer
734 *
735 * Try to resize FB BAR to make all VRAM CPU accessible. We try very hard not
736 * to fail, but if any of the BARs is not accessible after the size we abort
737 * driver loading by returning -ENODEV.
738 */
739int amdgpu_device_resize_fb_bar(struct amdgpu_device *adev)
740{
741 u64 space_needed = roundup_pow_of_two(adev->mc.real_vram_size);
742 u32 rbar_size = order_base_2(((space_needed >> 20) | 1)) - 1;
Christian König31b8ada2017-11-15 20:07:38 +0100743 struct pci_bus *root;
744 struct resource *res;
745 unsigned i;
Christian Königd6895ad2017-02-28 10:36:43 +0100746 u16 cmd;
747 int r;
748
pding0c03b912017-11-07 11:02:00 +0800749 /* Bypass for VF */
750 if (amdgpu_sriov_vf(adev))
751 return 0;
752
Christian König31b8ada2017-11-15 20:07:38 +0100753 /* Check if the root BUS has 64bit memory resources */
754 root = adev->pdev->bus;
755 while (root->parent)
756 root = root->parent;
757
758 pci_bus_for_each_resource(root, res, i) {
759 if (res && res->flags & IORESOURCE_MEM_64 &&
760 res->start > 0x100000000ull)
761 break;
762 }
763
764 /* Trying to resize is pointless without a root hub window above 4GB */
765 if (!res)
766 return 0;
767
Christian Königd6895ad2017-02-28 10:36:43 +0100768 /* Disable memory decoding while we change the BAR addresses and size */
769 pci_read_config_word(adev->pdev, PCI_COMMAND, &cmd);
770 pci_write_config_word(adev->pdev, PCI_COMMAND,
771 cmd & ~PCI_COMMAND_MEMORY);
772
773 /* Free the VRAM and doorbell BAR, we most likely need to move both. */
774 amdgpu_doorbell_fini(adev);
775 if (adev->asic_type >= CHIP_BONAIRE)
776 pci_release_resource(adev->pdev, 2);
777
778 pci_release_resource(adev->pdev, 0);
779
780 r = pci_resize_resource(adev->pdev, 0, rbar_size);
781 if (r == -ENOSPC)
782 DRM_INFO("Not enough PCI address space for a large BAR.");
783 else if (r && r != -ENOTSUPP)
784 DRM_ERROR("Problem resizing BAR0 (%d).", r);
785
786 pci_assign_unassigned_bus_resources(adev->pdev->bus);
787
788 /* When the doorbell or fb BAR isn't available we have no chance of
789 * using the device.
790 */
791 r = amdgpu_doorbell_init(adev);
792 if (r || (pci_resource_flags(adev->pdev, 0) & IORESOURCE_UNSET))
793 return -ENODEV;
794
795 pci_write_config_word(adev->pdev, PCI_COMMAND, cmd);
796
797 return 0;
798}
Horace Chena05502e2017-09-29 14:41:57 +0800799
800/*
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400801 * GPU helpers function.
802 */
803/**
Jim Quc836fec2017-02-10 15:59:59 +0800804 * amdgpu_need_post - check if the hw need post or not
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400805 *
806 * @adev: amdgpu_device pointer
807 *
Jim Quc836fec2017-02-10 15:59:59 +0800808 * Check if the asic has been initialized (all asics) at driver startup
809 * or post is needed if hw reset is performed.
810 * Returns true if need or false if not.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400811 */
Jim Quc836fec2017-02-10 15:59:59 +0800812bool amdgpu_need_post(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400813{
814 uint32_t reg;
815
Monk Liubec86372016-09-14 19:38:08 +0800816 if (amdgpu_sriov_vf(adev))
817 return false;
818
819 if (amdgpu_passthrough(adev)) {
Monk Liu1da2c322016-11-11 11:24:29 +0800820 /* for FIJI: In whole GPU pass-through virtualization case, after VM reboot
821 * some old smc fw still need driver do vPost otherwise gpu hang, while
822 * those smc fw version above 22.15 doesn't have this flaw, so we force
823 * vpost executed for smc version below 22.15
Monk Liubec86372016-09-14 19:38:08 +0800824 */
825 if (adev->asic_type == CHIP_FIJI) {
826 int err;
827 uint32_t fw_ver;
828 err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
829 /* force vPost if error occured */
830 if (err)
831 return true;
832
833 fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
Monk Liu1da2c322016-11-11 11:24:29 +0800834 if (fw_ver < 0x00160e00)
835 return true;
Monk Liubec86372016-09-14 19:38:08 +0800836 }
Monk Liubec86372016-09-14 19:38:08 +0800837 }
pding91fe77e2017-10-19 09:38:39 +0800838
839 if (adev->has_hw_reset) {
840 adev->has_hw_reset = false;
841 return true;
842 }
843
844 /* bios scratch used on CIK+ */
845 if (adev->asic_type >= CHIP_BONAIRE)
846 return amdgpu_atombios_scratch_need_asic_init(adev);
847
848 /* check MEM_SIZE for older asics */
849 reg = amdgpu_asic_get_config_memsize(adev);
850
851 if ((reg != 0) && (reg != 0xffffffff))
852 return false;
853
854 return true;
Monk Liubec86372016-09-14 19:38:08 +0800855}
856
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400857/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400858 * amdgpu_dummy_page_init - init dummy page used by the driver
859 *
860 * @adev: amdgpu_device pointer
861 *
862 * Allocate the dummy page used by the driver (all asics).
863 * This dummy page is used by the driver as a filler for gart entries
864 * when pages are taken out of the GART
865 * Returns 0 on sucess, -ENOMEM on failure.
866 */
867int amdgpu_dummy_page_init(struct amdgpu_device *adev)
868{
869 if (adev->dummy_page.page)
870 return 0;
871 adev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
872 if (adev->dummy_page.page == NULL)
873 return -ENOMEM;
874 adev->dummy_page.addr = pci_map_page(adev->pdev, adev->dummy_page.page,
875 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
876 if (pci_dma_mapping_error(adev->pdev, adev->dummy_page.addr)) {
877 dev_err(&adev->pdev->dev, "Failed to DMA MAP the dummy page\n");
878 __free_page(adev->dummy_page.page);
879 adev->dummy_page.page = NULL;
880 return -ENOMEM;
881 }
882 return 0;
883}
884
885/**
886 * amdgpu_dummy_page_fini - free dummy page used by the driver
887 *
888 * @adev: amdgpu_device pointer
889 *
890 * Frees the dummy page used by the driver (all asics).
891 */
892void amdgpu_dummy_page_fini(struct amdgpu_device *adev)
893{
894 if (adev->dummy_page.page == NULL)
895 return;
896 pci_unmap_page(adev->pdev, adev->dummy_page.addr,
897 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
898 __free_page(adev->dummy_page.page);
899 adev->dummy_page.page = NULL;
900}
901
902
903/* ATOM accessor methods */
904/*
905 * ATOM is an interpreted byte code stored in tables in the vbios. The
906 * driver registers callbacks to access registers and the interpreter
907 * in the driver parses the tables and executes then to program specific
908 * actions (set display modes, asic init, etc.). See amdgpu_atombios.c,
909 * atombios.h, and atom.c
910 */
911
912/**
913 * cail_pll_read - read PLL register
914 *
915 * @info: atom card_info pointer
916 * @reg: PLL register offset
917 *
918 * Provides a PLL register accessor for the atom interpreter (r4xx+).
919 * Returns the value of the PLL register.
920 */
921static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
922{
923 return 0;
924}
925
926/**
927 * cail_pll_write - write PLL register
928 *
929 * @info: atom card_info pointer
930 * @reg: PLL register offset
931 * @val: value to write to the pll register
932 *
933 * Provides a PLL register accessor for the atom interpreter (r4xx+).
934 */
935static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
936{
937
938}
939
940/**
941 * cail_mc_read - read MC (Memory Controller) register
942 *
943 * @info: atom card_info pointer
944 * @reg: MC register offset
945 *
946 * Provides an MC register accessor for the atom interpreter (r4xx+).
947 * Returns the value of the MC register.
948 */
949static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
950{
951 return 0;
952}
953
954/**
955 * cail_mc_write - write MC (Memory Controller) register
956 *
957 * @info: atom card_info pointer
958 * @reg: MC register offset
959 * @val: value to write to the pll register
960 *
961 * Provides a MC register accessor for the atom interpreter (r4xx+).
962 */
963static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
964{
965
966}
967
968/**
969 * cail_reg_write - write MMIO register
970 *
971 * @info: atom card_info pointer
972 * @reg: MMIO register offset
973 * @val: value to write to the pll register
974 *
975 * Provides a MMIO register accessor for the atom interpreter (r4xx+).
976 */
977static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
978{
979 struct amdgpu_device *adev = info->dev->dev_private;
980
981 WREG32(reg, val);
982}
983
984/**
985 * cail_reg_read - read MMIO register
986 *
987 * @info: atom card_info pointer
988 * @reg: MMIO register offset
989 *
990 * Provides an MMIO register accessor for the atom interpreter (r4xx+).
991 * Returns the value of the MMIO register.
992 */
993static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
994{
995 struct amdgpu_device *adev = info->dev->dev_private;
996 uint32_t r;
997
998 r = RREG32(reg);
999 return r;
1000}
1001
1002/**
1003 * cail_ioreg_write - write IO register
1004 *
1005 * @info: atom card_info pointer
1006 * @reg: IO register offset
1007 * @val: value to write to the pll register
1008 *
1009 * Provides a IO register accessor for the atom interpreter (r4xx+).
1010 */
1011static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
1012{
1013 struct amdgpu_device *adev = info->dev->dev_private;
1014
1015 WREG32_IO(reg, val);
1016}
1017
1018/**
1019 * cail_ioreg_read - read IO register
1020 *
1021 * @info: atom card_info pointer
1022 * @reg: IO register offset
1023 *
1024 * Provides an IO register accessor for the atom interpreter (r4xx+).
1025 * Returns the value of the IO register.
1026 */
1027static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
1028{
1029 struct amdgpu_device *adev = info->dev->dev_private;
1030 uint32_t r;
1031
1032 r = RREG32_IO(reg);
1033 return r;
1034}
1035
Kent Russell5b41d942017-08-22 12:31:43 -04001036static ssize_t amdgpu_atombios_get_vbios_version(struct device *dev,
1037 struct device_attribute *attr,
1038 char *buf)
1039{
1040 struct drm_device *ddev = dev_get_drvdata(dev);
1041 struct amdgpu_device *adev = ddev->dev_private;
1042 struct atom_context *ctx = adev->mode_info.atom_context;
1043
1044 return snprintf(buf, PAGE_SIZE, "%s\n", ctx->vbios_version);
1045}
1046
1047static DEVICE_ATTR(vbios_version, 0444, amdgpu_atombios_get_vbios_version,
1048 NULL);
1049
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001050/**
1051 * amdgpu_atombios_fini - free the driver info and callbacks for atombios
1052 *
1053 * @adev: amdgpu_device pointer
1054 *
1055 * Frees the driver info and register access callbacks for the ATOM
1056 * interpreter (r4xx+).
1057 * Called at driver shutdown.
1058 */
1059static void amdgpu_atombios_fini(struct amdgpu_device *adev)
1060{
Monk Liu89e0ec92016-05-27 19:34:11 +08001061 if (adev->mode_info.atom_context) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001062 kfree(adev->mode_info.atom_context->scratch);
Monk Liu89e0ec92016-05-27 19:34:11 +08001063 kfree(adev->mode_info.atom_context->iio);
1064 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001065 kfree(adev->mode_info.atom_context);
1066 adev->mode_info.atom_context = NULL;
1067 kfree(adev->mode_info.atom_card_info);
1068 adev->mode_info.atom_card_info = NULL;
Kent Russell5b41d942017-08-22 12:31:43 -04001069 device_remove_file(adev->dev, &dev_attr_vbios_version);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001070}
1071
1072/**
1073 * amdgpu_atombios_init - init the driver info and callbacks for atombios
1074 *
1075 * @adev: amdgpu_device pointer
1076 *
1077 * Initializes the driver info and register access callbacks for the
1078 * ATOM interpreter (r4xx+).
1079 * Returns 0 on sucess, -ENOMEM on failure.
1080 * Called at driver startup.
1081 */
1082static int amdgpu_atombios_init(struct amdgpu_device *adev)
1083{
1084 struct card_info *atom_card_info =
1085 kzalloc(sizeof(struct card_info), GFP_KERNEL);
Kent Russell5b41d942017-08-22 12:31:43 -04001086 int ret;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001087
1088 if (!atom_card_info)
1089 return -ENOMEM;
1090
1091 adev->mode_info.atom_card_info = atom_card_info;
1092 atom_card_info->dev = adev->ddev;
1093 atom_card_info->reg_read = cail_reg_read;
1094 atom_card_info->reg_write = cail_reg_write;
1095 /* needed for iio ops */
1096 if (adev->rio_mem) {
1097 atom_card_info->ioreg_read = cail_ioreg_read;
1098 atom_card_info->ioreg_write = cail_ioreg_write;
1099 } else {
pding9953b722017-10-26 09:30:38 +08001100 DRM_DEBUG("PCI I/O BAR is not found. Using MMIO to access ATOM BIOS\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001101 atom_card_info->ioreg_read = cail_reg_read;
1102 atom_card_info->ioreg_write = cail_reg_write;
1103 }
1104 atom_card_info->mc_read = cail_mc_read;
1105 atom_card_info->mc_write = cail_mc_write;
1106 atom_card_info->pll_read = cail_pll_read;
1107 atom_card_info->pll_write = cail_pll_write;
1108
1109 adev->mode_info.atom_context = amdgpu_atom_parse(atom_card_info, adev->bios);
1110 if (!adev->mode_info.atom_context) {
1111 amdgpu_atombios_fini(adev);
1112 return -ENOMEM;
1113 }
1114
1115 mutex_init(&adev->mode_info.atom_context->mutex);
Alex Deuchera5bde2f2016-09-23 16:23:41 -04001116 if (adev->is_atom_fw) {
1117 amdgpu_atomfirmware_scratch_regs_init(adev);
1118 amdgpu_atomfirmware_allocate_fb_scratch(adev);
1119 } else {
1120 amdgpu_atombios_scratch_regs_init(adev);
1121 amdgpu_atombios_allocate_fb_scratch(adev);
1122 }
Kent Russell5b41d942017-08-22 12:31:43 -04001123
1124 ret = device_create_file(adev->dev, &dev_attr_vbios_version);
1125 if (ret) {
1126 DRM_ERROR("Failed to create device file for VBIOS version\n");
1127 return ret;
1128 }
1129
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001130 return 0;
1131}
1132
1133/* if we get transitioned to only one device, take VGA back */
1134/**
1135 * amdgpu_vga_set_decode - enable/disable vga decode
1136 *
1137 * @cookie: amdgpu_device pointer
1138 * @state: enable/disable vga decode
1139 *
1140 * Enable/disable vga decode (all asics).
1141 * Returns VGA resource flags.
1142 */
1143static unsigned int amdgpu_vga_set_decode(void *cookie, bool state)
1144{
1145 struct amdgpu_device *adev = cookie;
1146 amdgpu_asic_set_vga_state(adev, state);
1147 if (state)
1148 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
1149 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1150 else
1151 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1152}
1153
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001154static void amdgpu_check_block_size(struct amdgpu_device *adev)
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001155{
1156 /* defines number of bits in page table versus page directory,
1157 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
1158 * page table and the remaining bits are in the page directory */
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001159 if (amdgpu_vm_block_size == -1)
1160 return;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001161
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001162 if (amdgpu_vm_block_size < 9) {
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001163 dev_warn(adev->dev, "VM page table size (%d) too small\n",
1164 amdgpu_vm_block_size);
Christian König97489122017-11-27 16:22:05 +01001165 amdgpu_vm_block_size = -1;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001166 }
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001167}
1168
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001169static void amdgpu_check_vm_size(struct amdgpu_device *adev)
1170{
Alex Deucher64dab072017-06-15 18:20:09 -04001171 /* no need to check the default value */
1172 if (amdgpu_vm_size == -1)
1173 return;
1174
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001175 if (amdgpu_vm_size < 1) {
1176 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
1177 amdgpu_vm_size);
Christian Königf3368122017-11-23 12:57:18 +01001178 amdgpu_vm_size = -1;
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001179 }
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001180}
1181
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001182/**
1183 * amdgpu_check_arguments - validate module params
1184 *
1185 * @adev: amdgpu_device pointer
1186 *
1187 * Validates certain module parameters and updates
1188 * the associated values used by the driver (all asics).
1189 */
1190static void amdgpu_check_arguments(struct amdgpu_device *adev)
1191{
Chunming Zhou5b011232015-12-10 17:34:33 +08001192 if (amdgpu_sched_jobs < 4) {
1193 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
1194 amdgpu_sched_jobs);
1195 amdgpu_sched_jobs = 4;
Alex Deucher76117502017-06-21 12:31:41 -04001196 } else if (!is_power_of_2(amdgpu_sched_jobs)){
Chunming Zhou5b011232015-12-10 17:34:33 +08001197 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
1198 amdgpu_sched_jobs);
1199 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
1200 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001201
Alex Deucher83e74db2017-08-21 11:58:25 -04001202 if (amdgpu_gart_size != -1 && amdgpu_gart_size < 32) {
Christian Königf9321cc2017-07-07 13:44:05 +02001203 /* gart size must be greater or equal to 32M */
1204 dev_warn(adev->dev, "gart size (%d) too small\n",
1205 amdgpu_gart_size);
Alex Deucher83e74db2017-08-21 11:58:25 -04001206 amdgpu_gart_size = -1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001207 }
1208
Christian König36d38372017-07-07 13:17:45 +02001209 if (amdgpu_gtt_size != -1 && amdgpu_gtt_size < 32) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001210 /* gtt size must be greater or equal to 32M */
Christian König36d38372017-07-07 13:17:45 +02001211 dev_warn(adev->dev, "gtt size (%d) too small\n",
1212 amdgpu_gtt_size);
1213 amdgpu_gtt_size = -1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001214 }
1215
Roger Hed07f14b2017-08-15 16:05:59 +08001216 /* valid range is between 4 and 9 inclusive */
1217 if (amdgpu_vm_fragment_size != -1 &&
1218 (amdgpu_vm_fragment_size > 9 || amdgpu_vm_fragment_size < 4)) {
1219 dev_warn(adev->dev, "valid range is between 4 and 9\n");
1220 amdgpu_vm_fragment_size = -1;
1221 }
1222
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001223 amdgpu_check_vm_size(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001224
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001225 amdgpu_check_block_size(adev);
Christian König6a7f76e2016-08-24 15:51:49 +02001226
jimqu526bae32016-11-07 09:53:10 +08001227 if (amdgpu_vram_page_split != -1 && (amdgpu_vram_page_split < 16 ||
Alex Deucher76117502017-06-21 12:31:41 -04001228 !is_power_of_2(amdgpu_vram_page_split))) {
Christian König6a7f76e2016-08-24 15:51:49 +02001229 dev_warn(adev->dev, "invalid VRAM page split (%d)\n",
1230 amdgpu_vram_page_split);
1231 amdgpu_vram_page_split = 1024;
1232 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001233}
1234
1235/**
1236 * amdgpu_switcheroo_set_state - set switcheroo state
1237 *
1238 * @pdev: pci dev pointer
Lukas Wunner16944672015-09-05 11:17:35 +02001239 * @state: vga_switcheroo state
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001240 *
1241 * Callback for the switcheroo driver. Suspends or resumes the
1242 * the asics before or after it is powered up using ACPI methods.
1243 */
1244static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1245{
1246 struct drm_device *dev = pci_get_drvdata(pdev);
1247
1248 if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
1249 return;
1250
1251 if (state == VGA_SWITCHEROO_ON) {
Joe Perches7ca85292017-02-28 04:55:52 -08001252 pr_info("amdgpu: switched on\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001253 /* don't suspend or resume card normally */
1254 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1255
Alex Deucher810ddc32016-08-23 13:25:49 -04001256 amdgpu_device_resume(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001257
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001258 dev->switch_power_state = DRM_SWITCH_POWER_ON;
1259 drm_kms_helper_poll_enable(dev);
1260 } else {
Joe Perches7ca85292017-02-28 04:55:52 -08001261 pr_info("amdgpu: switched off\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001262 drm_kms_helper_poll_disable(dev);
1263 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Alex Deucher810ddc32016-08-23 13:25:49 -04001264 amdgpu_device_suspend(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001265 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1266 }
1267}
1268
1269/**
1270 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1271 *
1272 * @pdev: pci dev pointer
1273 *
1274 * Callback for the switcheroo driver. Check of the switcheroo
1275 * state can be changed.
1276 * Returns true if the state can be changed, false if not.
1277 */
1278static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1279{
1280 struct drm_device *dev = pci_get_drvdata(pdev);
1281
1282 /*
1283 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1284 * locking inversion with the driver load path. And the access here is
1285 * completely racy anyway. So don't bother with locking for now.
1286 */
1287 return dev->open_count == 0;
1288}
1289
1290static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1291 .set_gpu_state = amdgpu_switcheroo_set_state,
1292 .reprobe = NULL,
1293 .can_switch = amdgpu_switcheroo_can_switch,
1294};
1295
1296int amdgpu_set_clockgating_state(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001297 enum amd_ip_block_type block_type,
1298 enum amd_clockgating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001299{
1300 int i, r = 0;
1301
1302 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001303 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001304 continue;
Rex Zhuc7228652017-02-22 15:33:46 +08001305 if (adev->ip_blocks[i].version->type != block_type)
1306 continue;
1307 if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
1308 continue;
1309 r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
1310 (void *)adev, state);
1311 if (r)
1312 DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
1313 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001314 }
1315 return r;
1316}
1317
1318int amdgpu_set_powergating_state(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001319 enum amd_ip_block_type block_type,
1320 enum amd_powergating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001321{
1322 int i, r = 0;
1323
1324 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001325 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001326 continue;
Rex Zhuc7228652017-02-22 15:33:46 +08001327 if (adev->ip_blocks[i].version->type != block_type)
1328 continue;
1329 if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
1330 continue;
1331 r = adev->ip_blocks[i].version->funcs->set_powergating_state(
1332 (void *)adev, state);
1333 if (r)
1334 DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
1335 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001336 }
1337 return r;
1338}
1339
Huang Rui6cb2d4e2017-01-05 18:44:41 +08001340void amdgpu_get_clockgating_state(struct amdgpu_device *adev, u32 *flags)
1341{
1342 int i;
1343
1344 for (i = 0; i < adev->num_ip_blocks; i++) {
1345 if (!adev->ip_blocks[i].status.valid)
1346 continue;
1347 if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
1348 adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
1349 }
1350}
1351
Alex Deucher5dbbb602016-06-23 11:41:04 -04001352int amdgpu_wait_for_idle(struct amdgpu_device *adev,
1353 enum amd_ip_block_type block_type)
1354{
1355 int i, r;
1356
1357 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001358 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001359 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001360 if (adev->ip_blocks[i].version->type == block_type) {
1361 r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -04001362 if (r)
1363 return r;
1364 break;
1365 }
1366 }
1367 return 0;
1368
1369}
1370
1371bool amdgpu_is_idle(struct amdgpu_device *adev,
1372 enum amd_ip_block_type block_type)
1373{
1374 int i;
1375
1376 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001377 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001378 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001379 if (adev->ip_blocks[i].version->type == block_type)
1380 return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -04001381 }
1382 return true;
1383
1384}
1385
Alex Deuchera1255102016-10-13 17:41:13 -04001386struct amdgpu_ip_block * amdgpu_get_ip_block(struct amdgpu_device *adev,
1387 enum amd_ip_block_type type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001388{
1389 int i;
1390
1391 for (i = 0; i < adev->num_ip_blocks; i++)
Alex Deuchera1255102016-10-13 17:41:13 -04001392 if (adev->ip_blocks[i].version->type == type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001393 return &adev->ip_blocks[i];
1394
1395 return NULL;
1396}
1397
1398/**
1399 * amdgpu_ip_block_version_cmp
1400 *
1401 * @adev: amdgpu_device pointer
yanyang15fc3aee2015-05-22 14:39:35 -04001402 * @type: enum amd_ip_block_type
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001403 * @major: major version
1404 * @minor: minor version
1405 *
1406 * return 0 if equal or greater
1407 * return 1 if smaller or the ip_block doesn't exist
1408 */
1409int amdgpu_ip_block_version_cmp(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001410 enum amd_ip_block_type type,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001411 u32 major, u32 minor)
1412{
Alex Deuchera1255102016-10-13 17:41:13 -04001413 struct amdgpu_ip_block *ip_block = amdgpu_get_ip_block(adev, type);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001414
Alex Deuchera1255102016-10-13 17:41:13 -04001415 if (ip_block && ((ip_block->version->major > major) ||
1416 ((ip_block->version->major == major) &&
1417 (ip_block->version->minor >= minor))))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001418 return 0;
1419
1420 return 1;
1421}
1422
Alex Deuchera1255102016-10-13 17:41:13 -04001423/**
1424 * amdgpu_ip_block_add
1425 *
1426 * @adev: amdgpu_device pointer
1427 * @ip_block_version: pointer to the IP to add
1428 *
1429 * Adds the IP block driver information to the collection of IPs
1430 * on the asic.
1431 */
1432int amdgpu_ip_block_add(struct amdgpu_device *adev,
1433 const struct amdgpu_ip_block_version *ip_block_version)
1434{
1435 if (!ip_block_version)
1436 return -EINVAL;
1437
Huang Ruia0bae352017-05-03 09:52:06 +08001438 DRM_DEBUG("add ip block number %d <%s>\n", adev->num_ip_blocks,
1439 ip_block_version->funcs->name);
1440
Alex Deuchera1255102016-10-13 17:41:13 -04001441 adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
1442
1443 return 0;
1444}
1445
Alex Deucher483ef982016-09-30 12:43:04 -04001446static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
Emily Deng9accf2f2016-08-10 16:01:25 +08001447{
1448 adev->enable_virtual_display = false;
1449
1450 if (amdgpu_virtual_display) {
1451 struct drm_device *ddev = adev->ddev;
1452 const char *pci_address_name = pci_name(ddev->pdev);
Emily Deng0f663562016-09-30 13:02:18 -04001453 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
Emily Deng9accf2f2016-08-10 16:01:25 +08001454
1455 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1456 pciaddstr_tmp = pciaddstr;
Emily Deng0f663562016-09-30 13:02:18 -04001457 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1458 pciaddname = strsep(&pciaddname_tmp, ",");
Yintian Tao967de2a2017-01-22 15:16:51 +08001459 if (!strcmp("all", pciaddname)
1460 || !strcmp(pci_address_name, pciaddname)) {
Emily Deng0f663562016-09-30 13:02:18 -04001461 long num_crtc;
1462 int res = -1;
1463
Emily Deng9accf2f2016-08-10 16:01:25 +08001464 adev->enable_virtual_display = true;
Emily Deng0f663562016-09-30 13:02:18 -04001465
1466 if (pciaddname_tmp)
1467 res = kstrtol(pciaddname_tmp, 10,
1468 &num_crtc);
1469
1470 if (!res) {
1471 if (num_crtc < 1)
1472 num_crtc = 1;
1473 if (num_crtc > 6)
1474 num_crtc = 6;
1475 adev->mode_info.num_crtc = num_crtc;
1476 } else {
1477 adev->mode_info.num_crtc = 1;
1478 }
Emily Deng9accf2f2016-08-10 16:01:25 +08001479 break;
1480 }
1481 }
1482
Emily Deng0f663562016-09-30 13:02:18 -04001483 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
1484 amdgpu_virtual_display, pci_address_name,
1485 adev->enable_virtual_display, adev->mode_info.num_crtc);
Emily Deng9accf2f2016-08-10 16:01:25 +08001486
1487 kfree(pciaddstr);
1488 }
1489}
1490
Alex Deuchere2a75f82017-04-27 16:58:01 -04001491static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev)
1492{
Alex Deuchere2a75f82017-04-27 16:58:01 -04001493 const char *chip_name;
1494 char fw_name[30];
1495 int err;
1496 const struct gpu_info_firmware_header_v1_0 *hdr;
1497
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001498 adev->firmware.gpu_info_fw = NULL;
1499
Alex Deuchere2a75f82017-04-27 16:58:01 -04001500 switch (adev->asic_type) {
1501 case CHIP_TOPAZ:
1502 case CHIP_TONGA:
1503 case CHIP_FIJI:
1504 case CHIP_POLARIS11:
1505 case CHIP_POLARIS10:
1506 case CHIP_POLARIS12:
1507 case CHIP_CARRIZO:
1508 case CHIP_STONEY:
1509#ifdef CONFIG_DRM_AMDGPU_SI
1510 case CHIP_VERDE:
1511 case CHIP_TAHITI:
1512 case CHIP_PITCAIRN:
1513 case CHIP_OLAND:
1514 case CHIP_HAINAN:
1515#endif
1516#ifdef CONFIG_DRM_AMDGPU_CIK
1517 case CHIP_BONAIRE:
1518 case CHIP_HAWAII:
1519 case CHIP_KAVERI:
1520 case CHIP_KABINI:
1521 case CHIP_MULLINS:
1522#endif
1523 default:
1524 return 0;
1525 case CHIP_VEGA10:
1526 chip_name = "vega10";
1527 break;
Alex Deucher2d2e5e72017-05-09 12:27:35 -04001528 case CHIP_RAVEN:
1529 chip_name = "raven";
1530 break;
Alex Deuchere2a75f82017-04-27 16:58:01 -04001531 }
1532
1533 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name);
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001534 err = request_firmware(&adev->firmware.gpu_info_fw, fw_name, adev->dev);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001535 if (err) {
1536 dev_err(adev->dev,
1537 "Failed to load gpu_info firmware \"%s\"\n",
1538 fw_name);
1539 goto out;
1540 }
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001541 err = amdgpu_ucode_validate(adev->firmware.gpu_info_fw);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001542 if (err) {
1543 dev_err(adev->dev,
1544 "Failed to validate gpu_info firmware \"%s\"\n",
1545 fw_name);
1546 goto out;
1547 }
1548
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001549 hdr = (const struct gpu_info_firmware_header_v1_0 *)adev->firmware.gpu_info_fw->data;
Alex Deuchere2a75f82017-04-27 16:58:01 -04001550 amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
1551
1552 switch (hdr->version_major) {
1553 case 1:
1554 {
1555 const struct gpu_info_firmware_v1_0 *gpu_info_fw =
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001556 (const struct gpu_info_firmware_v1_0 *)(adev->firmware.gpu_info_fw->data +
Alex Deuchere2a75f82017-04-27 16:58:01 -04001557 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1558
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001559 adev->gfx.config.max_shader_engines = le32_to_cpu(gpu_info_fw->gc_num_se);
1560 adev->gfx.config.max_cu_per_sh = le32_to_cpu(gpu_info_fw->gc_num_cu_per_sh);
1561 adev->gfx.config.max_sh_per_se = le32_to_cpu(gpu_info_fw->gc_num_sh_per_se);
1562 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 -04001563 adev->gfx.config.max_texture_channel_caches =
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001564 le32_to_cpu(gpu_info_fw->gc_num_tccs);
1565 adev->gfx.config.max_gprs = le32_to_cpu(gpu_info_fw->gc_num_gprs);
1566 adev->gfx.config.max_gs_threads = le32_to_cpu(gpu_info_fw->gc_num_max_gs_thds);
1567 adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gpu_info_fw->gc_gs_table_depth);
1568 adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gpu_info_fw->gc_gsprim_buff_depth);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001569 adev->gfx.config.double_offchip_lds_buf =
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001570 le32_to_cpu(gpu_info_fw->gc_double_offchip_lds_buffer);
1571 adev->gfx.cu_info.wave_front_size = le32_to_cpu(gpu_info_fw->gc_wave_size);
Hawking Zhang51fd0372017-06-09 22:30:52 +08001572 adev->gfx.cu_info.max_waves_per_simd =
1573 le32_to_cpu(gpu_info_fw->gc_max_waves_per_simd);
1574 adev->gfx.cu_info.max_scratch_slots_per_cu =
1575 le32_to_cpu(gpu_info_fw->gc_max_scratch_slots_per_cu);
1576 adev->gfx.cu_info.lds_size = le32_to_cpu(gpu_info_fw->gc_lds_size);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001577 break;
1578 }
1579 default:
1580 dev_err(adev->dev,
1581 "Unsupported gpu_info table %d\n", hdr->header.ucode_version);
1582 err = -EINVAL;
1583 goto out;
1584 }
1585out:
Alex Deuchere2a75f82017-04-27 16:58:01 -04001586 return err;
1587}
1588
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001589static int amdgpu_early_init(struct amdgpu_device *adev)
1590{
Alex Deucheraaa36a92015-04-20 17:31:14 -04001591 int i, r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001592
Alex Deucher483ef982016-09-30 12:43:04 -04001593 amdgpu_device_enable_virtual_display(adev);
Emily Denga6be7572016-08-08 11:37:50 +08001594
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001595 switch (adev->asic_type) {
Alex Deucheraaa36a92015-04-20 17:31:14 -04001596 case CHIP_TOPAZ:
1597 case CHIP_TONGA:
David Zhang48299f92015-07-08 01:05:16 +08001598 case CHIP_FIJI:
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001599 case CHIP_POLARIS11:
1600 case CHIP_POLARIS10:
Junwei Zhangc4642a42016-12-14 15:32:28 -05001601 case CHIP_POLARIS12:
Alex Deucheraaa36a92015-04-20 17:31:14 -04001602 case CHIP_CARRIZO:
Samuel Li39bb0c92015-10-08 16:31:43 -04001603 case CHIP_STONEY:
1604 if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
Alex Deucheraaa36a92015-04-20 17:31:14 -04001605 adev->family = AMDGPU_FAMILY_CZ;
1606 else
1607 adev->family = AMDGPU_FAMILY_VI;
1608
1609 r = vi_set_ip_blocks(adev);
1610 if (r)
1611 return r;
1612 break;
Ken Wang33f34802016-01-21 17:29:41 +08001613#ifdef CONFIG_DRM_AMDGPU_SI
1614 case CHIP_VERDE:
1615 case CHIP_TAHITI:
1616 case CHIP_PITCAIRN:
1617 case CHIP_OLAND:
1618 case CHIP_HAINAN:
Ken Wang295d0da2016-05-24 21:02:53 +08001619 adev->family = AMDGPU_FAMILY_SI;
Ken Wang33f34802016-01-21 17:29:41 +08001620 r = si_set_ip_blocks(adev);
1621 if (r)
1622 return r;
1623 break;
1624#endif
Alex Deuchera2e73f52015-04-20 17:09:27 -04001625#ifdef CONFIG_DRM_AMDGPU_CIK
1626 case CHIP_BONAIRE:
1627 case CHIP_HAWAII:
1628 case CHIP_KAVERI:
1629 case CHIP_KABINI:
1630 case CHIP_MULLINS:
1631 if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
1632 adev->family = AMDGPU_FAMILY_CI;
1633 else
1634 adev->family = AMDGPU_FAMILY_KV;
1635
1636 r = cik_set_ip_blocks(adev);
1637 if (r)
1638 return r;
1639 break;
1640#endif
Chunming Zhou2ca8a5d2016-12-07 17:31:19 +08001641 case CHIP_VEGA10:
1642 case CHIP_RAVEN:
1643 if (adev->asic_type == CHIP_RAVEN)
1644 adev->family = AMDGPU_FAMILY_RV;
1645 else
1646 adev->family = AMDGPU_FAMILY_AI;
Ken Wang460826e2017-03-06 14:53:16 -05001647
1648 r = soc15_set_ip_blocks(adev);
1649 if (r)
1650 return r;
1651 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001652 default:
1653 /* FIXME: not supported yet */
1654 return -EINVAL;
1655 }
1656
Alex Deuchere2a75f82017-04-27 16:58:01 -04001657 r = amdgpu_device_parse_gpu_info_fw(adev);
1658 if (r)
1659 return r;
1660
pding18847342017-11-06 10:21:26 +08001661 amdgpu_amdkfd_device_probe(adev);
1662
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001663 if (amdgpu_sriov_vf(adev)) {
1664 r = amdgpu_virt_request_full_gpu(adev, true);
1665 if (r)
pding5ffa61c2017-10-30 14:07:24 +08001666 return -EAGAIN;
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001667 }
1668
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001669 for (i = 0; i < adev->num_ip_blocks; i++) {
1670 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
Huang Ruied8cf002017-05-03 09:40:17 +08001671 DRM_ERROR("disabled ip block: %d <%s>\n",
1672 i, adev->ip_blocks[i].version->funcs->name);
Alex Deuchera1255102016-10-13 17:41:13 -04001673 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001674 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001675 if (adev->ip_blocks[i].version->funcs->early_init) {
1676 r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001677 if (r == -ENOENT) {
Alex Deuchera1255102016-10-13 17:41:13 -04001678 adev->ip_blocks[i].status.valid = false;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001679 } else if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001680 DRM_ERROR("early_init of IP block <%s> failed %d\n",
1681 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001682 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001683 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001684 adev->ip_blocks[i].status.valid = true;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001685 }
Alex Deucher974e6b62015-07-10 13:59:44 -04001686 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001687 adev->ip_blocks[i].status.valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001688 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001689 }
1690 }
1691
Nicolai Hähnle395d1fb2016-06-02 12:32:07 +02001692 adev->cg_flags &= amdgpu_cg_mask;
1693 adev->pg_flags &= amdgpu_pg_mask;
1694
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001695 return 0;
1696}
1697
1698static int amdgpu_init(struct amdgpu_device *adev)
1699{
1700 int i, r;
1701
1702 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001703 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001704 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001705 r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001706 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001707 DRM_ERROR("sw_init of IP block <%s> failed %d\n",
1708 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001709 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001710 }
Alex Deuchera1255102016-10-13 17:41:13 -04001711 adev->ip_blocks[i].status.sw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001712 /* need to do gmc hw init early so we can allocate gpu mem */
Alex Deuchera1255102016-10-13 17:41:13 -04001713 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001714 r = amdgpu_vram_scratch_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001715 if (r) {
1716 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001717 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001718 }
Alex Deuchera1255102016-10-13 17:41:13 -04001719 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001720 if (r) {
1721 DRM_ERROR("hw_init %d failed %d\n", i, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001722 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001723 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001724 r = amdgpu_wb_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001725 if (r) {
1726 DRM_ERROR("amdgpu_wb_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001727 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001728 }
Alex Deuchera1255102016-10-13 17:41:13 -04001729 adev->ip_blocks[i].status.hw = true;
Monk Liu24936642017-01-09 15:54:32 +08001730
1731 /* right after GMC hw init, we create CSA */
1732 if (amdgpu_sriov_vf(adev)) {
1733 r = amdgpu_allocate_static_csa(adev);
1734 if (r) {
1735 DRM_ERROR("allocate CSA failed %d\n", r);
1736 return r;
1737 }
1738 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001739 }
1740 }
1741
1742 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001743 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001744 continue;
1745 /* gmc hw init is done early */
Alex Deuchera1255102016-10-13 17:41:13 -04001746 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001747 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001748 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001749 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001750 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
1751 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001752 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001753 }
Alex Deuchera1255102016-10-13 17:41:13 -04001754 adev->ip_blocks[i].status.hw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001755 }
1756
pding18847342017-11-06 10:21:26 +08001757 amdgpu_amdkfd_device_init(adev);
pdingc6332b92017-11-06 11:21:55 +08001758
1759 if (amdgpu_sriov_vf(adev))
1760 amdgpu_virt_release_full_gpu(adev, true);
1761
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001762 return 0;
1763}
1764
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08001765static void amdgpu_fill_reset_magic(struct amdgpu_device *adev)
1766{
1767 memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM);
1768}
1769
1770static bool amdgpu_check_vram_lost(struct amdgpu_device *adev)
1771{
1772 return !!memcmp(adev->gart.ptr, adev->reset_magic,
1773 AMDGPU_RESET_MAGIC_NUM);
1774}
1775
Shirish S2dc80b02017-05-25 10:05:25 +05301776static int amdgpu_late_set_cg_state(struct amdgpu_device *adev)
1777{
1778 int i = 0, r;
1779
1780 for (i = 0; i < adev->num_ip_blocks; i++) {
1781 if (!adev->ip_blocks[i].status.valid)
1782 continue;
1783 /* skip CG for VCE/UVD, it's handled specially */
1784 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1785 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1786 /* enable clockgating to save power */
1787 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1788 AMD_CG_STATE_GATE);
1789 if (r) {
1790 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
1791 adev->ip_blocks[i].version->funcs->name, r);
1792 return r;
1793 }
1794 }
1795 }
1796 return 0;
1797}
1798
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001799static int amdgpu_late_init(struct amdgpu_device *adev)
1800{
1801 int i = 0, r;
1802
1803 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001804 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001805 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001806 if (adev->ip_blocks[i].version->funcs->late_init) {
1807 r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001808 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001809 DRM_ERROR("late_init of IP block <%s> failed %d\n",
1810 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001811 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001812 }
Alex Deuchera1255102016-10-13 17:41:13 -04001813 adev->ip_blocks[i].status.late_initialized = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001814 }
1815 }
1816
Shirish S2dc80b02017-05-25 10:05:25 +05301817 mod_delayed_work(system_wq, &adev->late_init_work,
1818 msecs_to_jiffies(AMDGPU_RESUME_MS));
1819
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08001820 amdgpu_fill_reset_magic(adev);
1821
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001822 return 0;
1823}
1824
1825static int amdgpu_fini(struct amdgpu_device *adev)
1826{
1827 int i, r;
1828
pding18847342017-11-06 10:21:26 +08001829 amdgpu_amdkfd_device_fini(adev);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001830 /* need to disable SMC first */
1831 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001832 if (!adev->ip_blocks[i].status.hw)
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001833 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001834 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001835 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
Alex Deuchera1255102016-10-13 17:41:13 -04001836 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1837 AMD_CG_STATE_UNGATE);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001838 if (r) {
1839 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001840 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001841 return r;
1842 }
Alex Deuchera1255102016-10-13 17:41:13 -04001843 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001844 /* XXX handle errors */
1845 if (r) {
1846 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001847 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001848 }
Alex Deuchera1255102016-10-13 17:41:13 -04001849 adev->ip_blocks[i].status.hw = false;
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001850 break;
1851 }
1852 }
1853
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001854 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001855 if (!adev->ip_blocks[i].status.hw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001856 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001857 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
Monk Liu84e5b512017-11-14 16:52:14 +08001858 amdgpu_free_static_csa(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001859 amdgpu_wb_fini(adev);
1860 amdgpu_vram_scratch_fini(adev);
1861 }
Rex Zhu8201a672016-11-24 21:44:44 +08001862
1863 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1864 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1865 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
1866 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1867 AMD_CG_STATE_UNGATE);
1868 if (r) {
1869 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1870 adev->ip_blocks[i].version->funcs->name, r);
1871 return r;
1872 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001873 }
Rex Zhu8201a672016-11-24 21:44:44 +08001874
Alex Deuchera1255102016-10-13 17:41:13 -04001875 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001876 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001877 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001878 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
1879 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001880 }
Rex Zhu8201a672016-11-24 21:44:44 +08001881
Alex Deuchera1255102016-10-13 17:41:13 -04001882 adev->ip_blocks[i].status.hw = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001883 }
1884
1885 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001886 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001887 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001888 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001889 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001890 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001891 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
1892 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001893 }
Alex Deuchera1255102016-10-13 17:41:13 -04001894 adev->ip_blocks[i].status.sw = false;
1895 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001896 }
1897
Monk Liua6dcfd92016-05-19 14:36:34 +08001898 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001899 if (!adev->ip_blocks[i].status.late_initialized)
Grazvydas Ignotas8a2eef12016-10-03 00:06:44 +03001900 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001901 if (adev->ip_blocks[i].version->funcs->late_fini)
1902 adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
1903 adev->ip_blocks[i].status.late_initialized = false;
Monk Liua6dcfd92016-05-19 14:36:34 +08001904 }
1905
Monk Liu030308f2017-09-15 15:34:52 +08001906 if (amdgpu_sriov_vf(adev))
Monk Liu24136132017-11-14 16:56:55 +08001907 if (amdgpu_virt_release_full_gpu(adev, false))
1908 DRM_ERROR("failed to release exclusive mode on fini\n");
Monk Liu24936642017-01-09 15:54:32 +08001909
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001910 return 0;
1911}
1912
Shirish S2dc80b02017-05-25 10:05:25 +05301913static void amdgpu_late_init_func_handler(struct work_struct *work)
1914{
1915 struct amdgpu_device *adev =
1916 container_of(work, struct amdgpu_device, late_init_work.work);
1917 amdgpu_late_set_cg_state(adev);
1918}
1919
Alex Deucherfaefba92016-12-06 10:38:29 -05001920int amdgpu_suspend(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001921{
1922 int i, r;
1923
Xiangliang Yue941ea92017-01-18 12:47:55 +08001924 if (amdgpu_sriov_vf(adev))
1925 amdgpu_virt_request_full_gpu(adev, false);
1926
Flora Cuic5a93a22016-02-26 10:45:25 +08001927 /* ungate SMC block first */
1928 r = amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC,
1929 AMD_CG_STATE_UNGATE);
1930 if (r) {
1931 DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n",r);
1932 }
1933
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001934 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001935 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001936 continue;
1937 /* ungate blocks so that suspend can properly shut them down */
Flora Cuic5a93a22016-02-26 10:45:25 +08001938 if (i != AMD_IP_BLOCK_TYPE_SMC) {
Alex Deuchera1255102016-10-13 17:41:13 -04001939 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1940 AMD_CG_STATE_UNGATE);
Flora Cuic5a93a22016-02-26 10:45:25 +08001941 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001942 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1943 adev->ip_blocks[i].version->funcs->name, r);
Flora Cuic5a93a22016-02-26 10:45:25 +08001944 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001945 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001946 /* XXX handle errors */
Alex Deuchera1255102016-10-13 17:41:13 -04001947 r = adev->ip_blocks[i].version->funcs->suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001948 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001949 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001950 DRM_ERROR("suspend of IP block <%s> failed %d\n",
1951 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001952 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001953 }
1954
Xiangliang Yue941ea92017-01-18 12:47:55 +08001955 if (amdgpu_sriov_vf(adev))
1956 amdgpu_virt_release_full_gpu(adev, false);
1957
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001958 return 0;
1959}
1960
Monk Liue4f0fdc2017-02-09 11:55:49 +08001961static int amdgpu_sriov_reinit_early(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08001962{
1963 int i, r;
1964
Monk Liu2cb681b2017-04-26 12:00:49 +08001965 static enum amd_ip_block_type ip_order[] = {
1966 AMD_IP_BLOCK_TYPE_GMC,
1967 AMD_IP_BLOCK_TYPE_COMMON,
Monk Liu2cb681b2017-04-26 12:00:49 +08001968 AMD_IP_BLOCK_TYPE_IH,
1969 };
Monk Liua90ad3c2017-01-23 14:22:08 +08001970
Monk Liu2cb681b2017-04-26 12:00:49 +08001971 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1972 int j;
1973 struct amdgpu_ip_block *block;
Monk Liua90ad3c2017-01-23 14:22:08 +08001974
Monk Liu2cb681b2017-04-26 12:00:49 +08001975 for (j = 0; j < adev->num_ip_blocks; j++) {
1976 block = &adev->ip_blocks[j];
1977
1978 if (block->version->type != ip_order[i] ||
1979 !block->status.valid)
1980 continue;
1981
1982 r = block->version->funcs->hw_init(adev);
1983 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
Monk Liua90ad3c2017-01-23 14:22:08 +08001984 }
1985 }
1986
1987 return 0;
1988}
1989
Monk Liue4f0fdc2017-02-09 11:55:49 +08001990static int amdgpu_sriov_reinit_late(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08001991{
1992 int i, r;
1993
Monk Liu2cb681b2017-04-26 12:00:49 +08001994 static enum amd_ip_block_type ip_order[] = {
1995 AMD_IP_BLOCK_TYPE_SMC,
Monk Liuef4c1662017-09-22 16:23:34 +08001996 AMD_IP_BLOCK_TYPE_PSP,
Monk Liu2cb681b2017-04-26 12:00:49 +08001997 AMD_IP_BLOCK_TYPE_DCE,
1998 AMD_IP_BLOCK_TYPE_GFX,
1999 AMD_IP_BLOCK_TYPE_SDMA,
Frank Min257deb82017-06-15 20:07:36 +08002000 AMD_IP_BLOCK_TYPE_UVD,
2001 AMD_IP_BLOCK_TYPE_VCE
Monk Liu2cb681b2017-04-26 12:00:49 +08002002 };
Monk Liua90ad3c2017-01-23 14:22:08 +08002003
Monk Liu2cb681b2017-04-26 12:00:49 +08002004 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
2005 int j;
2006 struct amdgpu_ip_block *block;
Monk Liua90ad3c2017-01-23 14:22:08 +08002007
Monk Liu2cb681b2017-04-26 12:00:49 +08002008 for (j = 0; j < adev->num_ip_blocks; j++) {
2009 block = &adev->ip_blocks[j];
2010
2011 if (block->version->type != ip_order[i] ||
2012 !block->status.valid)
2013 continue;
2014
2015 r = block->version->funcs->hw_init(adev);
2016 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
Monk Liua90ad3c2017-01-23 14:22:08 +08002017 }
2018 }
2019
2020 return 0;
2021}
2022
Chunming Zhoufcf06492017-05-05 10:33:33 +08002023static int amdgpu_resume_phase1(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002024{
2025 int i, r;
2026
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002027 for (i = 0; i < adev->num_ip_blocks; i++) {
2028 if (!adev->ip_blocks[i].status.valid)
2029 continue;
Chunming Zhoufcf06492017-05-05 10:33:33 +08002030 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
2031 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
2032 adev->ip_blocks[i].version->type ==
2033 AMD_IP_BLOCK_TYPE_IH) {
2034 r = adev->ip_blocks[i].version->funcs->resume(adev);
2035 if (r) {
2036 DRM_ERROR("resume of IP block <%s> failed %d\n",
2037 adev->ip_blocks[i].version->funcs->name, r);
2038 return r;
2039 }
2040 }
2041 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002042
Chunming Zhoufcf06492017-05-05 10:33:33 +08002043 return 0;
2044}
2045
2046static int amdgpu_resume_phase2(struct amdgpu_device *adev)
2047{
2048 int i, r;
2049
2050 for (i = 0; i < adev->num_ip_blocks; i++) {
2051 if (!adev->ip_blocks[i].status.valid)
2052 continue;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002053 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
2054 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
2055 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH )
2056 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002057 r = adev->ip_blocks[i].version->funcs->resume(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002058 if (r) {
2059 DRM_ERROR("resume of IP block <%s> failed %d\n",
2060 adev->ip_blocks[i].version->funcs->name, r);
2061 return r;
2062 }
2063 }
2064
2065 return 0;
2066}
2067
2068static int amdgpu_resume(struct amdgpu_device *adev)
2069{
Chunming Zhoufcf06492017-05-05 10:33:33 +08002070 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002071
Chunming Zhoufcf06492017-05-05 10:33:33 +08002072 r = amdgpu_resume_phase1(adev);
2073 if (r)
2074 return r;
2075 r = amdgpu_resume_phase2(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002076
Chunming Zhoufcf06492017-05-05 10:33:33 +08002077 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002078}
2079
Monk Liu4e99a442016-03-31 13:26:59 +08002080static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
Andres Rodriguez048765a2016-06-11 02:51:32 -04002081{
Monk Liu6867e1b2017-10-16 19:50:44 +08002082 if (amdgpu_sriov_vf(adev)) {
2083 if (adev->is_atom_fw) {
2084 if (amdgpu_atomfirmware_gpu_supports_virtualization(adev))
2085 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
2086 } else {
2087 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
2088 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
2089 }
2090
2091 if (!(adev->virt.caps & AMDGPU_SRIOV_CAPS_SRIOV_VBIOS))
2092 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_NO_VBIOS, 0, 0);
Alex Deuchera5bde2f2016-09-23 16:23:41 -04002093 }
Andres Rodriguez048765a2016-06-11 02:51:32 -04002094}
2095
Harry Wentland45622362017-09-12 15:58:20 -04002096bool amdgpu_device_asic_has_dc_support(enum amd_asic_type asic_type)
2097{
2098 switch (asic_type) {
2099#if defined(CONFIG_DRM_AMD_DC)
2100 case CHIP_BONAIRE:
2101 case CHIP_HAWAII:
Alex Deucher0d6fbcc2017-08-10 14:39:48 -04002102 case CHIP_KAVERI:
Harry Wentland45622362017-09-12 15:58:20 -04002103 case CHIP_CARRIZO:
2104 case CHIP_STONEY:
2105 case CHIP_POLARIS11:
2106 case CHIP_POLARIS10:
Alex Deucher2c8ad2d2017-06-15 16:20:24 -04002107 case CHIP_POLARIS12:
Harry Wentland45622362017-09-12 15:58:20 -04002108 case CHIP_TONGA:
2109 case CHIP_FIJI:
2110#if defined(CONFIG_DRM_AMD_DC_PRE_VEGA)
2111 return amdgpu_dc != 0;
Harry Wentland45622362017-09-12 15:58:20 -04002112#endif
Alex Deucher17b7cf82017-08-23 09:42:22 -04002113 case CHIP_KABINI:
2114 case CHIP_MULLINS:
2115 return amdgpu_dc > 0;
Harry Wentland42f8ffa2017-09-15 14:07:30 -04002116 case CHIP_VEGA10:
2117#if defined(CONFIG_DRM_AMD_DC_DCN1_0)
Hawking Zhangfd187852017-03-06 14:01:11 +08002118 case CHIP_RAVEN:
Harry Wentland42f8ffa2017-09-15 14:07:30 -04002119#endif
Hawking Zhangfd187852017-03-06 14:01:11 +08002120 return amdgpu_dc != 0;
2121#endif
Harry Wentland45622362017-09-12 15:58:20 -04002122 default:
2123 return false;
2124 }
2125}
2126
2127/**
2128 * amdgpu_device_has_dc_support - check if dc is supported
2129 *
2130 * @adev: amdgpu_device_pointer
2131 *
2132 * Returns true for supported, false for not supported
2133 */
2134bool amdgpu_device_has_dc_support(struct amdgpu_device *adev)
2135{
Xiangliang Yu2555039d2017-01-10 17:34:52 +08002136 if (amdgpu_sriov_vf(adev))
2137 return false;
2138
Harry Wentland45622362017-09-12 15:58:20 -04002139 return amdgpu_device_asic_has_dc_support(adev->asic_type);
2140}
2141
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002142/**
2143 * amdgpu_device_init - initialize the driver
2144 *
2145 * @adev: amdgpu_device pointer
2146 * @pdev: drm dev pointer
2147 * @pdev: pci dev pointer
2148 * @flags: driver flags
2149 *
2150 * Initializes the driver info and hw (all asics).
2151 * Returns 0 for success or an error on failure.
2152 * Called at driver startup.
2153 */
2154int amdgpu_device_init(struct amdgpu_device *adev,
2155 struct drm_device *ddev,
2156 struct pci_dev *pdev,
2157 uint32_t flags)
2158{
2159 int r, i;
2160 bool runtime = false;
Marek Olšák95844d22016-08-17 23:49:27 +02002161 u32 max_MBps;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002162
2163 adev->shutdown = false;
2164 adev->dev = &pdev->dev;
2165 adev->ddev = ddev;
2166 adev->pdev = pdev;
2167 adev->flags = flags;
Jammy Zhou2f7d10b2015-07-22 11:29:01 +08002168 adev->asic_type = flags & AMD_ASIC_MASK;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002169 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
Christian König6f02a692017-07-07 11:56:59 +02002170 adev->mc.gart_size = 512 * 1024 * 1024;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002171 adev->accel_working = false;
2172 adev->num_rings = 0;
2173 adev->mman.buffer_funcs = NULL;
2174 adev->mman.buffer_funcs_ring = NULL;
2175 adev->vm_manager.vm_pte_funcs = NULL;
Christian König2d55e452016-02-08 17:37:38 +01002176 adev->vm_manager.vm_pte_num_rings = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002177 adev->gart.gart_funcs = NULL;
Chris Wilsonf54d1862016-10-25 13:00:45 +01002178 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
Andres Rodriguezb8866c22017-04-28 20:05:51 -04002179 bitmap_zero(adev->gfx.pipe_reserve_bitmap, AMDGPU_MAX_COMPUTE_QUEUES);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002180
2181 adev->smc_rreg = &amdgpu_invalid_rreg;
2182 adev->smc_wreg = &amdgpu_invalid_wreg;
2183 adev->pcie_rreg = &amdgpu_invalid_rreg;
2184 adev->pcie_wreg = &amdgpu_invalid_wreg;
Huang Rui36b9a952016-08-31 13:23:25 +08002185 adev->pciep_rreg = &amdgpu_invalid_rreg;
2186 adev->pciep_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002187 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
2188 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
2189 adev->didt_rreg = &amdgpu_invalid_rreg;
2190 adev->didt_wreg = &amdgpu_invalid_wreg;
Rex Zhuccdbb202016-06-08 12:47:41 +08002191 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
2192 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002193 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
2194 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
2195
Alex Deucher3e39ab92015-06-05 15:04:33 -04002196 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
2197 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
2198 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002199
2200 /* mutex initialization are all done here so we
2201 * can recall function without having locking issues */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002202 atomic_set(&adev->irq.ih.lock, 0);
Huang Rui0e5ca0d2017-03-03 18:37:23 -05002203 mutex_init(&adev->firmware.mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002204 mutex_init(&adev->pm.mutex);
2205 mutex_init(&adev->gfx.gpu_clock_mutex);
2206 mutex_init(&adev->srbm_mutex);
Andres Rodriguezb8866c22017-04-28 20:05:51 -04002207 mutex_init(&adev->gfx.pipe_reserve_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002208 mutex_init(&adev->grbm_idx_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002209 mutex_init(&adev->mn_lock);
Alex Deuchere23b74a2017-09-28 09:47:32 -04002210 mutex_init(&adev->virt.vf_errors.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002211 hash_init(adev->mn_hash);
Monk Liu13a752e2017-10-17 15:11:12 +08002212 mutex_init(&adev->lock_reset);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002213
2214 amdgpu_check_arguments(adev);
2215
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002216 spin_lock_init(&adev->mmio_idx_lock);
2217 spin_lock_init(&adev->smc_idx_lock);
2218 spin_lock_init(&adev->pcie_idx_lock);
2219 spin_lock_init(&adev->uvd_ctx_idx_lock);
2220 spin_lock_init(&adev->didt_idx_lock);
Rex Zhuccdbb202016-06-08 12:47:41 +08002221 spin_lock_init(&adev->gc_cac_idx_lock);
Evan Quan16abb5d2017-07-04 09:21:50 +08002222 spin_lock_init(&adev->se_cac_idx_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002223 spin_lock_init(&adev->audio_endpt_idx_lock);
Marek Olšák95844d22016-08-17 23:49:27 +02002224 spin_lock_init(&adev->mm_stats.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002225
Chunming Zhou0c4e7fa2016-08-17 11:41:30 +08002226 INIT_LIST_HEAD(&adev->shadow_list);
2227 mutex_init(&adev->shadow_list_lock);
2228
Andres Rodriguez795f2812017-03-06 16:27:55 -05002229 INIT_LIST_HEAD(&adev->ring_lru_list);
2230 spin_lock_init(&adev->ring_lru_list_lock);
2231
Shirish S2dc80b02017-05-25 10:05:25 +05302232 INIT_DELAYED_WORK(&adev->late_init_work, amdgpu_late_init_func_handler);
2233
Alex Xie0fa49552017-06-08 14:58:05 -04002234 /* Registers mapping */
2235 /* TODO: block userspace mapping of io register */
Ken Wangda69c1612016-01-21 19:08:55 +08002236 if (adev->asic_type >= CHIP_BONAIRE) {
2237 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
2238 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
2239 } else {
2240 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
2241 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
2242 }
Chunming Zhou5c1354b2016-08-30 16:13:10 +08002243
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002244 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
2245 if (adev->rmmio == NULL) {
2246 return -ENOMEM;
2247 }
2248 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
2249 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
2250
Christian König705e5192017-06-08 11:15:16 +02002251 /* doorbell bar mapping */
2252 amdgpu_doorbell_init(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002253
2254 /* io port mapping */
2255 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
2256 if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
2257 adev->rio_mem_size = pci_resource_len(adev->pdev, i);
2258 adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
2259 break;
2260 }
2261 }
2262 if (adev->rio_mem == NULL)
Amber Linb64a18c2017-01-04 08:06:58 -05002263 DRM_INFO("PCI I/O BAR is not found.\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002264
2265 /* early init functions */
2266 r = amdgpu_early_init(adev);
2267 if (r)
2268 return r;
2269
2270 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
2271 /* this will fail for cards that aren't VGA class devices, just
2272 * ignore it */
2273 vga_client_register(adev->pdev, adev, NULL, amdgpu_vga_set_decode);
2274
2275 if (amdgpu_runtime_pm == 1)
2276 runtime = true;
Alex Deuchere9bef452016-04-25 13:12:18 -04002277 if (amdgpu_device_is_px(ddev))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002278 runtime = true;
Lukas Wunner84c8b222017-03-10 21:23:45 +01002279 if (!pci_is_thunderbolt_attached(adev->pdev))
2280 vga_switcheroo_register_client(adev->pdev,
2281 &amdgpu_switcheroo_ops, runtime);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002282 if (runtime)
2283 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
2284
2285 /* Read BIOS */
Alex Deucher83ba1262016-06-03 18:21:41 -04002286 if (!amdgpu_get_bios(adev)) {
2287 r = -EINVAL;
2288 goto failed;
2289 }
Nils Wallméniusf7e9e9f2016-12-14 21:52:45 +01002290
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002291 r = amdgpu_atombios_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002292 if (r) {
2293 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002294 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_INIT_FAIL, 0, 0);
Alex Deucher83ba1262016-06-03 18:21:41 -04002295 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002296 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002297
Monk Liu4e99a442016-03-31 13:26:59 +08002298 /* detect if we are with an SRIOV vbios */
2299 amdgpu_device_detect_sriov_bios(adev);
Andres Rodriguez048765a2016-06-11 02:51:32 -04002300
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002301 /* Post card if necessary */
pding91fe77e2017-10-19 09:38:39 +08002302 if (amdgpu_need_post(adev)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002303 if (!adev->bios) {
Monk Liubec86372016-09-14 19:38:08 +08002304 dev_err(adev->dev, "no vBIOS found\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04002305 r = -EINVAL;
2306 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002307 }
Monk Liubec86372016-09-14 19:38:08 +08002308 DRM_INFO("GPU posting now...\n");
Monk Liu4e99a442016-03-31 13:26:59 +08002309 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2310 if (r) {
2311 dev_err(adev->dev, "gpu post error!\n");
2312 goto failed;
2313 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002314 }
2315
Alex Deucher88b64e92017-07-10 10:43:10 -04002316 if (adev->is_atom_fw) {
2317 /* Initialize clocks */
2318 r = amdgpu_atomfirmware_get_clock_info(adev);
2319 if (r) {
2320 dev_err(adev->dev, "amdgpu_atomfirmware_get_clock_info failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002321 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
Alex Deucher88b64e92017-07-10 10:43:10 -04002322 goto failed;
2323 }
2324 } else {
Alex Deuchera5bde2f2016-09-23 16:23:41 -04002325 /* Initialize clocks */
2326 r = amdgpu_atombios_get_clock_info(adev);
2327 if (r) {
2328 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002329 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
Gavin Wan89041942017-06-23 13:55:15 -04002330 goto failed;
Alex Deuchera5bde2f2016-09-23 16:23:41 -04002331 }
2332 /* init i2c buses */
Harry Wentland45622362017-09-12 15:58:20 -04002333 if (!amdgpu_device_has_dc_support(adev))
2334 amdgpu_atombios_i2c_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002335 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002336
2337 /* Fence driver */
2338 r = amdgpu_fence_driver_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002339 if (r) {
2340 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002341 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0);
Alex Deucher83ba1262016-06-03 18:21:41 -04002342 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002343 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002344
2345 /* init the mode config */
2346 drm_mode_config_init(adev->ddev);
2347
2348 r = amdgpu_init(adev);
2349 if (r) {
pding8840a382017-10-23 17:22:09 +08002350 /* failed in exclusive mode due to timeout */
2351 if (amdgpu_sriov_vf(adev) &&
2352 !amdgpu_sriov_runtime(adev) &&
2353 amdgpu_virt_mmio_blocked(adev) &&
2354 !amdgpu_virt_wait_reset(adev)) {
2355 dev_err(adev->dev, "VF exclusive mode timeout\n");
Pixel Ding1daee8b2017-11-08 11:03:14 +08002356 /* Don't send request since VF is inactive. */
2357 adev->virt.caps &= ~AMDGPU_SRIOV_CAPS_RUNTIME;
2358 adev->virt.ops = NULL;
pding8840a382017-10-23 17:22:09 +08002359 r = -EAGAIN;
2360 goto failed;
2361 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05002362 dev_err(adev->dev, "amdgpu_init failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002363 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002364 amdgpu_fini(adev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002365 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002366 }
2367
2368 adev->accel_working = true;
2369
Alex Xiee59c0202017-06-01 09:42:59 -04002370 amdgpu_vm_check_compute_bug(adev);
2371
Marek Olšák95844d22016-08-17 23:49:27 +02002372 /* Initialize the buffer migration limit. */
2373 if (amdgpu_moverate >= 0)
2374 max_MBps = amdgpu_moverate;
2375 else
2376 max_MBps = 8; /* Allow 8 MB/s. */
2377 /* Get a log2 for easy divisions. */
2378 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
2379
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002380 r = amdgpu_ib_pool_init(adev);
2381 if (r) {
2382 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
Alex Deuchere23b74a2017-09-28 09:47:32 -04002383 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r);
Alex Deucher83ba1262016-06-03 18:21:41 -04002384 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002385 }
2386
2387 r = amdgpu_ib_ring_tests(adev);
2388 if (r)
2389 DRM_ERROR("ib ring test failed (%d).\n", r);
2390
Horace Chen2dc8f812017-10-09 16:17:16 +08002391 if (amdgpu_sriov_vf(adev))
2392 amdgpu_virt_init_data_exchange(adev);
2393
Monk Liu9bc92b92017-02-08 17:38:13 +08002394 amdgpu_fbdev_init(adev);
2395
Rex Zhud2f52ac2017-09-22 17:47:27 +08002396 r = amdgpu_pm_sysfs_init(adev);
2397 if (r)
2398 DRM_ERROR("registering pm debugfs failed (%d).\n", r);
2399
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002400 r = amdgpu_gem_debugfs_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002401 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002402 DRM_ERROR("registering gem debugfs failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002403
2404 r = amdgpu_debugfs_regs_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002405 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002406 DRM_ERROR("registering register debugfs failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002407
Huang Rui4f0955f2017-05-10 23:04:06 +08002408 r = amdgpu_debugfs_test_ib_ring_init(adev);
2409 if (r)
2410 DRM_ERROR("registering register test ib ring debugfs failed (%d).\n", r);
2411
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
Kent Russelldb95e212017-08-22 12:31:43 -04002416 r = amdgpu_debugfs_vbios_dump_init(adev);
2417 if (r)
2418 DRM_ERROR("Creating vbios dump debugfs failed (%d).\n", r);
2419
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);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002469 /* evict vram memory */
2470 amdgpu_bo_evict_vram(adev);
2471 amdgpu_ib_pool_fini(adev);
2472 amdgpu_fence_driver_fini(adev);
2473 amdgpu_fbdev_fini(adev);
2474 r = amdgpu_fini(adev);
Huang Ruiab4fe3e2017-06-05 22:11:59 +08002475 if (adev->firmware.gpu_info_fw) {
2476 release_firmware(adev->firmware.gpu_info_fw);
2477 adev->firmware.gpu_info_fw = NULL;
2478 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002479 adev->accel_working = false;
Shirish S2dc80b02017-05-25 10:05:25 +05302480 cancel_delayed_work_sync(&adev->late_init_work);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002481 /* free i2c buses */
Harry Wentland45622362017-09-12 15:58:20 -04002482 if (!amdgpu_device_has_dc_support(adev))
2483 amdgpu_i2c_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002484 amdgpu_atombios_fini(adev);
2485 kfree(adev->bios);
2486 adev->bios = NULL;
Lukas Wunner84c8b222017-03-10 21:23:45 +01002487 if (!pci_is_thunderbolt_attached(adev->pdev))
2488 vga_switcheroo_unregister_client(adev->pdev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002489 if (adev->flags & AMD_IS_PX)
2490 vga_switcheroo_fini_domain_pm_ops(adev->dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002491 vga_client_register(adev->pdev, NULL, NULL, NULL);
2492 if (adev->rio_mem)
2493 pci_iounmap(adev->pdev, adev->rio_mem);
2494 adev->rio_mem = NULL;
2495 iounmap(adev->rmmio);
2496 adev->rmmio = NULL;
Christian König705e5192017-06-08 11:15:16 +02002497 amdgpu_doorbell_fini(adev);
Rex Zhud2f52ac2017-09-22 17:47:27 +08002498 amdgpu_pm_sysfs_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002499 amdgpu_debugfs_regs_cleanup(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002500}
2501
2502
2503/*
2504 * Suspend & resume.
2505 */
2506/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002507 * amdgpu_device_suspend - initiate device suspend
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002508 *
2509 * @pdev: drm dev pointer
2510 * @state: suspend state
2511 *
2512 * Puts the hw in the suspend state (all asics).
2513 * Returns 0 for success or an error on failure.
2514 * Called at driver suspend.
2515 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002516int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002517{
2518 struct amdgpu_device *adev;
2519 struct drm_crtc *crtc;
2520 struct drm_connector *connector;
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002521 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002522
2523 if (dev == NULL || dev->dev_private == NULL) {
2524 return -ENODEV;
2525 }
2526
2527 adev = dev->dev_private;
2528
2529 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2530 return 0;
2531
2532 drm_kms_helper_poll_disable(dev);
2533
Harry Wentland45622362017-09-12 15:58:20 -04002534 if (!amdgpu_device_has_dc_support(adev)) {
2535 /* turn off display hw */
2536 drm_modeset_lock_all(dev);
2537 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2538 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
2539 }
2540 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002541 }
2542
Yong Zhaoba997702015-11-09 17:21:45 -05002543 amdgpu_amdkfd_suspend(adev);
2544
Alex Deucher756e6882015-10-08 00:03:36 -04002545 /* unpin the front buffers and cursors */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002546 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Alex Deucher756e6882015-10-08 00:03:36 -04002547 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002548 struct amdgpu_framebuffer *rfb = to_amdgpu_framebuffer(crtc->primary->fb);
2549 struct amdgpu_bo *robj;
2550
Alex Deucher756e6882015-10-08 00:03:36 -04002551 if (amdgpu_crtc->cursor_bo) {
2552 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
Alex Xie7a6901d2017-04-24 13:52:41 -04002553 r = amdgpu_bo_reserve(aobj, true);
Alex Deucher756e6882015-10-08 00:03:36 -04002554 if (r == 0) {
2555 amdgpu_bo_unpin(aobj);
2556 amdgpu_bo_unreserve(aobj);
2557 }
2558 }
2559
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002560 if (rfb == NULL || rfb->obj == NULL) {
2561 continue;
2562 }
2563 robj = gem_to_amdgpu_bo(rfb->obj);
2564 /* don't unpin kernel fb objects */
2565 if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
Alex Xie7a6901d2017-04-24 13:52:41 -04002566 r = amdgpu_bo_reserve(robj, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002567 if (r == 0) {
2568 amdgpu_bo_unpin(robj);
2569 amdgpu_bo_unreserve(robj);
2570 }
2571 }
2572 }
2573 /* evict vram memory */
2574 amdgpu_bo_evict_vram(adev);
2575
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002576 amdgpu_fence_driver_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002577
2578 r = amdgpu_suspend(adev);
2579
Alex Deuchera0a71e42016-10-10 12:41:36 -04002580 /* evict remaining vram memory
2581 * This second call to evict vram is to evict the gart page table
2582 * using the CPU.
2583 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002584 amdgpu_bo_evict_vram(adev);
2585
Alex Deucherd05da0e2017-06-30 17:08:45 -04002586 amdgpu_atombios_scratch_regs_save(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002587 pci_save_state(dev->pdev);
2588 if (suspend) {
2589 /* Shut down the device */
2590 pci_disable_device(dev->pdev);
2591 pci_set_power_state(dev->pdev, PCI_D3hot);
jimqu74b0b152016-09-07 17:09:12 +08002592 } else {
2593 r = amdgpu_asic_reset(adev);
2594 if (r)
2595 DRM_ERROR("amdgpu asic reset failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002596 }
2597
2598 if (fbcon) {
2599 console_lock();
2600 amdgpu_fbdev_set_suspend(adev, 1);
2601 console_unlock();
2602 }
2603 return 0;
2604}
2605
2606/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002607 * amdgpu_device_resume - initiate device resume
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002608 *
2609 * @pdev: drm dev pointer
2610 *
2611 * Bring the hw back to operating state (all asics).
2612 * Returns 0 for success or an error on failure.
2613 * Called at driver resume.
2614 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002615int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002616{
2617 struct drm_connector *connector;
2618 struct amdgpu_device *adev = dev->dev_private;
Alex Deucher756e6882015-10-08 00:03:36 -04002619 struct drm_crtc *crtc;
Huang Rui03161a62017-04-13 16:12:26 +08002620 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002621
2622 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2623 return 0;
2624
jimqu74b0b152016-09-07 17:09:12 +08002625 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002626 console_lock();
jimqu74b0b152016-09-07 17:09:12 +08002627
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002628 if (resume) {
2629 pci_set_power_state(dev->pdev, PCI_D0);
2630 pci_restore_state(dev->pdev);
jimqu74b0b152016-09-07 17:09:12 +08002631 r = pci_enable_device(dev->pdev);
Huang Rui03161a62017-04-13 16:12:26 +08002632 if (r)
2633 goto unlock;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002634 }
Alex Deucherd05da0e2017-06-30 17:08:45 -04002635 amdgpu_atombios_scratch_regs_restore(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002636
2637 /* post card */
Jim Quc836fec2017-02-10 15:59:59 +08002638 if (amdgpu_need_post(adev)) {
jimqu74b0b152016-09-07 17:09:12 +08002639 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2640 if (r)
2641 DRM_ERROR("amdgpu asic init failed\n");
2642 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002643
2644 r = amdgpu_resume(adev);
Rex Zhue6707212017-03-30 13:21:01 +08002645 if (r) {
Flora Cuica198522016-02-04 15:10:08 +08002646 DRM_ERROR("amdgpu_resume failed (%d).\n", r);
Huang Rui03161a62017-04-13 16:12:26 +08002647 goto unlock;
Rex Zhue6707212017-03-30 13:21:01 +08002648 }
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002649 amdgpu_fence_driver_resume(adev);
2650
Flora Cuica198522016-02-04 15:10:08 +08002651 if (resume) {
2652 r = amdgpu_ib_ring_tests(adev);
2653 if (r)
2654 DRM_ERROR("ib ring test failed (%d).\n", r);
2655 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002656
2657 r = amdgpu_late_init(adev);
Huang Rui03161a62017-04-13 16:12:26 +08002658 if (r)
2659 goto unlock;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002660
Alex Deucher756e6882015-10-08 00:03:36 -04002661 /* pin cursors */
2662 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2663 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2664
2665 if (amdgpu_crtc->cursor_bo) {
2666 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
Alex Xie7a6901d2017-04-24 13:52:41 -04002667 r = amdgpu_bo_reserve(aobj, true);
Alex Deucher756e6882015-10-08 00:03:36 -04002668 if (r == 0) {
2669 r = amdgpu_bo_pin(aobj,
2670 AMDGPU_GEM_DOMAIN_VRAM,
2671 &amdgpu_crtc->cursor_addr);
2672 if (r != 0)
2673 DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
2674 amdgpu_bo_unreserve(aobj);
2675 }
2676 }
2677 }
Yong Zhaoba997702015-11-09 17:21:45 -05002678 r = amdgpu_amdkfd_resume(adev);
2679 if (r)
2680 return r;
Alex Deucher756e6882015-10-08 00:03:36 -04002681
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002682 /* blat the mode back in */
2683 if (fbcon) {
Harry Wentland45622362017-09-12 15:58:20 -04002684 if (!amdgpu_device_has_dc_support(adev)) {
2685 /* pre DCE11 */
2686 drm_helper_resume_force_mode(dev);
2687
2688 /* turn on display hw */
2689 drm_modeset_lock_all(dev);
2690 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2691 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
2692 }
2693 drm_modeset_unlock_all(dev);
2694 } else {
2695 /*
2696 * There is no equivalent atomic helper to turn on
2697 * display, so we defined our own function for this,
2698 * once suspend resume is supported by the atomic
2699 * framework this will be reworked
2700 */
2701 amdgpu_dm_display_resume(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002702 }
2703 }
2704
2705 drm_kms_helper_poll_enable(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002706
2707 /*
2708 * Most of the connector probing functions try to acquire runtime pm
2709 * refs to ensure that the GPU is powered on when connector polling is
2710 * performed. Since we're calling this from a runtime PM callback,
2711 * trying to acquire rpm refs will cause us to deadlock.
2712 *
2713 * Since we're guaranteed to be holding the rpm lock, it's safe to
2714 * temporarily disable the rpm helpers so this doesn't deadlock us.
2715 */
2716#ifdef CONFIG_PM
2717 dev->dev->power.disable_depth++;
2718#endif
Harry Wentland45622362017-09-12 15:58:20 -04002719 if (!amdgpu_device_has_dc_support(adev))
2720 drm_helper_hpd_irq_event(dev);
2721 else
2722 drm_kms_helper_hotplug_event(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002723#ifdef CONFIG_PM
2724 dev->dev->power.disable_depth--;
2725#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002726
Huang Rui03161a62017-04-13 16:12:26 +08002727 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002728 amdgpu_fbdev_set_suspend(adev, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002729
Huang Rui03161a62017-04-13 16:12:26 +08002730unlock:
2731 if (fbcon)
2732 console_unlock();
2733
2734 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002735}
2736
Chunming Zhou63fbf422016-07-15 11:19:20 +08002737static bool amdgpu_check_soft_reset(struct amdgpu_device *adev)
2738{
2739 int i;
2740 bool asic_hang = false;
2741
Monk Liuf993d622017-10-16 19:46:01 +08002742 if (amdgpu_sriov_vf(adev))
2743 return true;
2744
Chunming Zhou63fbf422016-07-15 11:19:20 +08002745 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002746 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou63fbf422016-07-15 11:19:20 +08002747 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002748 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
2749 adev->ip_blocks[i].status.hang =
2750 adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
2751 if (adev->ip_blocks[i].status.hang) {
2752 DRM_INFO("IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
Chunming Zhou63fbf422016-07-15 11:19:20 +08002753 asic_hang = true;
2754 }
2755 }
2756 return asic_hang;
2757}
2758
Baoyou Xie4d446652016-09-18 22:09:35 +08002759static int amdgpu_pre_soft_reset(struct amdgpu_device *adev)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002760{
2761 int i, r = 0;
2762
2763 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002764 if (!adev->ip_blocks[i].status.valid)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002765 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002766 if (adev->ip_blocks[i].status.hang &&
2767 adev->ip_blocks[i].version->funcs->pre_soft_reset) {
2768 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
Chunming Zhoud31a5012016-07-18 10:04:34 +08002769 if (r)
2770 return r;
2771 }
2772 }
2773
2774 return 0;
2775}
2776
Chunming Zhou35d782f2016-07-15 15:57:13 +08002777static bool amdgpu_need_full_reset(struct amdgpu_device *adev)
2778{
Alex Deucherda146d32016-10-13 16:07:03 -04002779 int i;
2780
2781 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002782 if (!adev->ip_blocks[i].status.valid)
Alex Deucherda146d32016-10-13 16:07:03 -04002783 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002784 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
2785 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
2786 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
Ken Wang98512bb2017-09-14 16:25:19 +08002787 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) ||
2788 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
Alex Deuchera1255102016-10-13 17:41:13 -04002789 if (adev->ip_blocks[i].status.hang) {
Alex Deucherda146d32016-10-13 16:07:03 -04002790 DRM_INFO("Some block need full reset!\n");
2791 return true;
2792 }
2793 }
Chunming Zhou35d782f2016-07-15 15:57:13 +08002794 }
2795 return false;
2796}
2797
2798static int amdgpu_soft_reset(struct amdgpu_device *adev)
2799{
2800 int i, r = 0;
2801
2802 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002803 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002804 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002805 if (adev->ip_blocks[i].status.hang &&
2806 adev->ip_blocks[i].version->funcs->soft_reset) {
2807 r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002808 if (r)
2809 return r;
2810 }
2811 }
2812
2813 return 0;
2814}
2815
2816static int amdgpu_post_soft_reset(struct amdgpu_device *adev)
2817{
2818 int i, r = 0;
2819
2820 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002821 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002822 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002823 if (adev->ip_blocks[i].status.hang &&
2824 adev->ip_blocks[i].version->funcs->post_soft_reset)
2825 r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002826 if (r)
2827 return r;
2828 }
2829
2830 return 0;
2831}
2832
Chunming Zhou3ad81f12016-08-05 17:30:17 +08002833bool amdgpu_need_backup(struct amdgpu_device *adev)
2834{
2835 if (adev->flags & AMD_IS_APU)
2836 return false;
2837
2838 return amdgpu_lockup_timeout > 0 ? true : false;
2839}
2840
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002841static int amdgpu_recover_vram_from_shadow(struct amdgpu_device *adev,
2842 struct amdgpu_ring *ring,
2843 struct amdgpu_bo *bo,
Chris Wilsonf54d1862016-10-25 13:00:45 +01002844 struct dma_fence **fence)
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002845{
2846 uint32_t domain;
2847 int r;
2848
Roger.He23d2e502017-04-21 14:24:26 +08002849 if (!bo->shadow)
2850 return 0;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002851
Alex Xie1d284792017-04-24 13:53:04 -04002852 r = amdgpu_bo_reserve(bo, true);
Roger.He23d2e502017-04-21 14:24:26 +08002853 if (r)
2854 return r;
2855 domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
2856 /* if bo has been evicted, then no need to recover */
2857 if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
Roger.He82521312017-04-21 13:08:43 +08002858 r = amdgpu_bo_validate(bo->shadow);
2859 if (r) {
2860 DRM_ERROR("bo validate failed!\n");
2861 goto err;
2862 }
2863
Roger.He23d2e502017-04-21 14:24:26 +08002864 r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002865 NULL, fence, true);
Roger.He23d2e502017-04-21 14:24:26 +08002866 if (r) {
2867 DRM_ERROR("recover page table failed!\n");
2868 goto err;
2869 }
2870 }
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002871err:
Roger.He23d2e502017-04-21 14:24:26 +08002872 amdgpu_bo_unreserve(bo);
2873 return r;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002874}
2875
Monk Liu57406822017-10-25 16:37:02 +08002876/*
2877 * amdgpu_reset - reset ASIC/GPU for bare-metal or passthrough
Monk Liua90ad3c2017-01-23 14:22:08 +08002878 *
2879 * @adev: amdgpu device pointer
Monk Liu57406822017-10-25 16:37:02 +08002880 * @reset_flags: output param tells caller the reset result
Monk Liua90ad3c2017-01-23 14:22:08 +08002881 *
Monk Liu57406822017-10-25 16:37:02 +08002882 * attempt to do soft-reset or full-reset and reinitialize Asic
2883 * return 0 means successed otherwise failed
2884*/
2885static int amdgpu_reset(struct amdgpu_device *adev, uint64_t* reset_flags)
Monk Liua90ad3c2017-01-23 14:22:08 +08002886{
Monk Liu57406822017-10-25 16:37:02 +08002887 bool need_full_reset, vram_lost = 0;
2888 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002889
Chunming Zhou35d782f2016-07-15 15:57:13 +08002890 need_full_reset = amdgpu_need_full_reset(adev);
2891
2892 if (!need_full_reset) {
2893 amdgpu_pre_soft_reset(adev);
2894 r = amdgpu_soft_reset(adev);
2895 amdgpu_post_soft_reset(adev);
2896 if (r || amdgpu_check_soft_reset(adev)) {
2897 DRM_INFO("soft reset failed, will fallback to full reset!\n");
2898 need_full_reset = true;
2899 }
Monk Liu57406822017-10-25 16:37:02 +08002900
Chunming Zhou35d782f2016-07-15 15:57:13 +08002901 }
2902
2903 if (need_full_reset) {
Chunming Zhou35d782f2016-07-15 15:57:13 +08002904 r = amdgpu_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002905
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002906retry:
Alex Deucherd05da0e2017-06-30 17:08:45 -04002907 amdgpu_atombios_scratch_regs_save(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002908 r = amdgpu_asic_reset(adev);
Alex Deucherd05da0e2017-06-30 17:08:45 -04002909 amdgpu_atombios_scratch_regs_restore(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002910 /* post card */
2911 amdgpu_atom_asic_init(adev->mode_info.atom_context);
Alex Deucherbfa99262016-01-15 11:59:48 -05002912
Chunming Zhou35d782f2016-07-15 15:57:13 +08002913 if (!r) {
2914 dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
Chunming Zhoufcf06492017-05-05 10:33:33 +08002915 r = amdgpu_resume_phase1(adev);
2916 if (r)
2917 goto out;
Monk Liu57406822017-10-25 16:37:02 +08002918
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002919 vram_lost = amdgpu_check_vram_lost(adev);
Chunming Zhouf1892132017-05-15 16:48:27 +08002920 if (vram_lost) {
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002921 DRM_ERROR("VRAM is lost!\n");
Chunming Zhouf1892132017-05-15 16:48:27 +08002922 atomic_inc(&adev->vram_lost_counter);
2923 }
Monk Liu57406822017-10-25 16:37:02 +08002924
Christian Königc1c7ce82017-10-16 16:50:32 +02002925 r = amdgpu_gtt_mgr_recover(
2926 &adev->mman.bdev.man[TTM_PL_TT]);
Chunming Zhou2c0d7312016-08-30 16:36:25 +08002927 if (r)
Chunming Zhoufcf06492017-05-05 10:33:33 +08002928 goto out;
Monk Liu57406822017-10-25 16:37:02 +08002929
Chunming Zhoufcf06492017-05-05 10:33:33 +08002930 r = amdgpu_resume_phase2(adev);
2931 if (r)
2932 goto out;
Monk Liu57406822017-10-25 16:37:02 +08002933
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002934 if (vram_lost)
2935 amdgpu_fill_reset_magic(adev);
Chunming Zhou2c0d7312016-08-30 16:36:25 +08002936 }
Chunming Zhoufcf06492017-05-05 10:33:33 +08002937 }
Monk Liu57406822017-10-25 16:37:02 +08002938
Chunming Zhoufcf06492017-05-05 10:33:33 +08002939out:
2940 if (!r) {
2941 amdgpu_irq_gpu_reset_resume_helper(adev);
Chunming Zhou1f465082016-06-30 15:02:26 +08002942 r = amdgpu_ib_ring_tests(adev);
2943 if (r) {
2944 dev_err(adev->dev, "ib ring test failed (%d).\n", r);
Chunming Zhou40019dc2016-06-29 16:01:49 +08002945 r = amdgpu_suspend(adev);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002946 need_full_reset = true;
Chunming Zhou40019dc2016-06-29 16:01:49 +08002947 goto retry;
Chunming Zhou1f465082016-06-30 15:02:26 +08002948 }
Monk Liu57406822017-10-25 16:37:02 +08002949 }
2950
2951 if (reset_flags) {
2952 if (vram_lost)
2953 (*reset_flags) |= AMDGPU_RESET_INFO_VRAM_LOST;
2954
2955 if (need_full_reset)
2956 (*reset_flags) |= AMDGPU_RESET_INFO_FULLRESET;
2957 }
2958
2959 return r;
2960}
2961
2962/*
2963 * amdgpu_reset_sriov - reset ASIC for SR-IOV vf
2964 *
2965 * @adev: amdgpu device pointer
2966 * @reset_flags: output param tells caller the reset result
2967 *
2968 * do VF FLR and reinitialize Asic
2969 * return 0 means successed otherwise failed
2970*/
2971static int amdgpu_reset_sriov(struct amdgpu_device *adev, uint64_t *reset_flags, bool from_hypervisor)
2972{
2973 int r;
2974
2975 if (from_hypervisor)
2976 r = amdgpu_virt_request_full_gpu(adev, true);
2977 else
2978 r = amdgpu_virt_reset_gpu(adev);
2979 if (r)
2980 return r;
2981
2982 /* Resume IP prior to SMC */
2983 r = amdgpu_sriov_reinit_early(adev);
2984 if (r)
2985 goto error;
2986
2987 /* we need recover gart prior to run SMC/CP/SDMA resume */
Christian Königc1c7ce82017-10-16 16:50:32 +02002988 amdgpu_gtt_mgr_recover(&adev->mman.bdev.man[TTM_PL_TT]);
Monk Liu57406822017-10-25 16:37:02 +08002989
2990 /* now we are okay to resume SMC/CP/SDMA */
2991 r = amdgpu_sriov_reinit_late(adev);
2992 if (r)
2993 goto error;
2994
2995 amdgpu_irq_gpu_reset_resume_helper(adev);
2996 r = amdgpu_ib_ring_tests(adev);
2997 if (r)
2998 dev_err(adev->dev, "[GPU_RESET] ib ring test failed (%d).\n", r);
2999
3000error:
3001 /* release full control of GPU after ib test */
3002 amdgpu_virt_release_full_gpu(adev, true);
3003
3004 if (reset_flags) {
Monk Liu75bc6092017-10-30 20:11:54 +08003005 if (adev->virt.gim_feature & AMDGIM_FEATURE_GIM_FLR_VRAMLOST) {
3006 (*reset_flags) |= AMDGPU_RESET_INFO_VRAM_LOST;
3007 atomic_inc(&adev->vram_lost_counter);
3008 }
Monk Liu57406822017-10-25 16:37:02 +08003009
3010 /* VF FLR or hotlink reset is always full-reset */
3011 (*reset_flags) |= AMDGPU_RESET_INFO_FULLRESET;
3012 }
3013
3014 return r;
3015}
3016
3017/**
3018 * amdgpu_gpu_recover - reset the asic and recover scheduler
3019 *
3020 * @adev: amdgpu device pointer
3021 * @job: which job trigger hang
3022 *
3023 * Attempt to reset the GPU if it has hung (all asics).
3024 * Returns 0 for success or an error on failure.
3025 */
3026int amdgpu_gpu_recover(struct amdgpu_device *adev, struct amdgpu_job *job)
3027{
3028 struct drm_atomic_state *state = NULL;
3029 uint64_t reset_flags = 0;
3030 int i, r, resched;
3031
3032 if (!amdgpu_check_soft_reset(adev)) {
3033 DRM_INFO("No hardware hang detected. Did some blocks stall?\n");
3034 return 0;
3035 }
3036
3037 dev_info(adev->dev, "GPU reset begin!\n");
3038
Monk Liu13a752e2017-10-17 15:11:12 +08003039 mutex_lock(&adev->lock_reset);
Monk Liu57406822017-10-25 16:37:02 +08003040 atomic_inc(&adev->gpu_reset_counter);
Monk Liu13a752e2017-10-17 15:11:12 +08003041 adev->in_gpu_reset = 1;
Monk Liu57406822017-10-25 16:37:02 +08003042
3043 /* block TTM */
3044 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
3045 /* store modesetting */
3046 if (amdgpu_device_has_dc_support(adev))
3047 state = drm_atomic_helper_suspend(adev->ddev);
3048
3049 /* block scheduler */
3050 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
3051 struct amdgpu_ring *ring = adev->rings[i];
3052
3053 if (!ring || !ring->sched.thread)
3054 continue;
3055
3056 /* only focus on the ring hit timeout if &job not NULL */
3057 if (job && job->ring->idx != i)
3058 continue;
3059
3060 kthread_park(ring->sched.thread);
3061 amd_sched_hw_job_reset(&ring->sched, &job->base);
3062
3063 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
3064 amdgpu_fence_driver_force_completion(ring);
3065 }
3066
3067 if (amdgpu_sriov_vf(adev))
3068 r = amdgpu_reset_sriov(adev, &reset_flags, job ? false : true);
3069 else
3070 r = amdgpu_reset(adev, &reset_flags);
3071
3072 if (!r) {
3073 if (((reset_flags & AMDGPU_RESET_INFO_FULLRESET) && !(adev->flags & AMD_IS_APU)) ||
3074 (reset_flags & AMDGPU_RESET_INFO_VRAM_LOST)) {
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003075 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
3076 struct amdgpu_bo *bo, *tmp;
Chris Wilsonf54d1862016-10-25 13:00:45 +01003077 struct dma_fence *fence = NULL, *next = NULL;
Chunming Zhou1f465082016-06-30 15:02:26 +08003078
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003079 DRM_INFO("recover vram bo from shadow\n");
3080 mutex_lock(&adev->shadow_list_lock);
3081 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
Monk Liu236763d2017-05-01 16:15:31 +08003082 next = NULL;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003083 amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
3084 if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01003085 r = dma_fence_wait(fence, false);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003086 if (r) {
Monk Liu1d7b17b2017-01-22 18:52:56 +08003087 WARN(r, "recovery from shadow isn't completed\n");
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003088 break;
3089 }
3090 }
3091
Chris Wilsonf54d1862016-10-25 13:00:45 +01003092 dma_fence_put(fence);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003093 fence = next;
3094 }
3095 mutex_unlock(&adev->shadow_list_lock);
3096 if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01003097 r = dma_fence_wait(fence, false);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003098 if (r)
Monk Liu1d7b17b2017-01-22 18:52:56 +08003099 WARN(r, "recovery from shadow isn't completed\n");
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003100 }
Chris Wilsonf54d1862016-10-25 13:00:45 +01003101 dma_fence_put(fence);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003102 }
Monk Liu57406822017-10-25 16:37:02 +08003103
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003104 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
3105 struct amdgpu_ring *ring = adev->rings[i];
Chunming Zhou51687752017-04-24 17:09:15 +08003106
3107 if (!ring || !ring->sched.thread)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003108 continue;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003109
Monk Liu57406822017-10-25 16:37:02 +08003110 /* only focus on the ring hit timeout if &job not NULL */
3111 if (job && job->ring->idx != i)
3112 continue;
3113
Chunming Zhouaa1c8902016-06-30 13:56:02 +08003114 amd_sched_job_recovery(&ring->sched);
Chunming Zhou0875dc92016-06-12 15:41:58 +08003115 kthread_unpark(ring->sched.thread);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003116 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003117 } else {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003118 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
Monk Liu57406822017-10-25 16:37:02 +08003119 struct amdgpu_ring *ring = adev->rings[i];
3120
3121 if (!ring || !ring->sched.thread)
3122 continue;
3123
3124 /* only focus on the ring hit timeout if &job not NULL */
3125 if (job && job->ring->idx != i)
3126 continue;
3127
3128 kthread_unpark(adev->rings[i]->sched.thread);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003129 }
3130 }
3131
Harry Wentland45622362017-09-12 15:58:20 -04003132 if (amdgpu_device_has_dc_support(adev)) {
Monk Liu57406822017-10-25 16:37:02 +08003133 if (drm_atomic_helper_resume(adev->ddev, state))
3134 dev_info(adev->dev, "drm resume failed:%d\n", r);
Harry Wentland45622362017-09-12 15:58:20 -04003135 amdgpu_dm_display_resume(adev);
Monk Liu57406822017-10-25 16:37:02 +08003136 } else {
Harry Wentland45622362017-09-12 15:58:20 -04003137 drm_helper_resume_force_mode(adev->ddev);
Monk Liu57406822017-10-25 16:37:02 +08003138 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003139
3140 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
Monk Liu57406822017-10-25 16:37:02 +08003141
Gavin Wan89041942017-06-23 13:55:15 -04003142 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003143 /* bad news, how to tell it to userspace ? */
Monk Liu57406822017-10-25 16:37:02 +08003144 dev_info(adev->dev, "GPU reset(%d) failed\n", atomic_read(&adev->gpu_reset_counter));
3145 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, r);
3146 } else {
3147 dev_info(adev->dev, "GPU reset(%d) successed!\n",atomic_read(&adev->gpu_reset_counter));
Gavin Wan89041942017-06-23 13:55:15 -04003148 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003149
Gavin Wan89041942017-06-23 13:55:15 -04003150 amdgpu_vf_error_trans_all(adev);
Monk Liu13a752e2017-10-17 15:11:12 +08003151 adev->in_gpu_reset = 0;
3152 mutex_unlock(&adev->lock_reset);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003153 return r;
3154}
3155
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003156void amdgpu_get_pcie_info(struct amdgpu_device *adev)
3157{
3158 u32 mask;
3159 int ret;
3160
Alex Deuchercd474ba2016-02-04 10:21:23 -05003161 if (amdgpu_pcie_gen_cap)
3162 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
3163
3164 if (amdgpu_pcie_lane_cap)
3165 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
3166
3167 /* covers APUs as well */
3168 if (pci_is_root_bus(adev->pdev->bus)) {
3169 if (adev->pm.pcie_gen_mask == 0)
3170 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
3171 if (adev->pm.pcie_mlw_mask == 0)
3172 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003173 return;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003174 }
Alex Deuchercd474ba2016-02-04 10:21:23 -05003175
3176 if (adev->pm.pcie_gen_mask == 0) {
3177 ret = drm_pcie_get_speed_cap_mask(adev->ddev, &mask);
3178 if (!ret) {
3179 adev->pm.pcie_gen_mask = (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
3180 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
3181 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
3182
3183 if (mask & DRM_PCIE_SPEED_25)
3184 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
3185 if (mask & DRM_PCIE_SPEED_50)
3186 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2;
3187 if (mask & DRM_PCIE_SPEED_80)
3188 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3;
3189 } else {
3190 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
3191 }
3192 }
3193 if (adev->pm.pcie_mlw_mask == 0) {
3194 ret = drm_pcie_get_max_link_width(adev->ddev, &mask);
3195 if (!ret) {
3196 switch (mask) {
3197 case 32:
3198 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
3199 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
3200 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3201 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3202 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3203 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3204 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3205 break;
3206 case 16:
3207 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
3208 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3209 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3210 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3211 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3212 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3213 break;
3214 case 12:
3215 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3216 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3217 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3218 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3219 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3220 break;
3221 case 8:
3222 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3223 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3224 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3225 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3226 break;
3227 case 4:
3228 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3229 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3230 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3231 break;
3232 case 2:
3233 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3234 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3235 break;
3236 case 1:
3237 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
3238 break;
3239 default:
3240 break;
3241 }
3242 } else {
3243 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003244 }
3245 }
3246}
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003247
3248/*
3249 * Debugfs
3250 */
3251int amdgpu_debugfs_add_files(struct amdgpu_device *adev,
Nils Wallménius06ab6832016-05-02 12:46:15 -04003252 const struct drm_info_list *files,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003253 unsigned nfiles)
3254{
3255 unsigned i;
3256
3257 for (i = 0; i < adev->debugfs_count; i++) {
3258 if (adev->debugfs[i].files == files) {
3259 /* Already registered */
3260 return 0;
3261 }
3262 }
3263
3264 i = adev->debugfs_count + 1;
3265 if (i > AMDGPU_DEBUGFS_MAX_COMPONENTS) {
3266 DRM_ERROR("Reached maximum number of debugfs components.\n");
3267 DRM_ERROR("Report so we increase "
3268 "AMDGPU_DEBUGFS_MAX_COMPONENTS.\n");
3269 return -EINVAL;
3270 }
3271 adev->debugfs[adev->debugfs_count].files = files;
3272 adev->debugfs[adev->debugfs_count].num_files = nfiles;
3273 adev->debugfs_count = i;
3274#if defined(CONFIG_DEBUG_FS)
3275 drm_debugfs_create_files(files, nfiles,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003276 adev->ddev->primary->debugfs_root,
3277 adev->ddev->primary);
3278#endif
3279 return 0;
3280}
3281
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003282#if defined(CONFIG_DEBUG_FS)
3283
3284static ssize_t amdgpu_debugfs_regs_read(struct file *f, char __user *buf,
3285 size_t size, loff_t *pos)
3286{
Al Viro45063092016-12-04 18:24:56 -05003287 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003288 ssize_t result = 0;
3289 int r;
Tom St Denisbd122672016-07-28 09:39:22 -04003290 bool pm_pg_lock, use_bank;
Tom St Denis566281592016-06-27 11:55:07 -04003291 unsigned instance_bank, sh_bank, se_bank;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003292
3293 if (size & 0x3 || *pos & 0x3)
3294 return -EINVAL;
3295
Tom St Denisbd122672016-07-28 09:39:22 -04003296 /* are we reading registers for which a PG lock is necessary? */
3297 pm_pg_lock = (*pos >> 23) & 1;
3298
Tom St Denis566281592016-06-27 11:55:07 -04003299 if (*pos & (1ULL << 62)) {
Tom St Denis0b968652017-11-10 12:54:50 -05003300 se_bank = (*pos & GENMASK_ULL(33, 24)) >> 24;
3301 sh_bank = (*pos & GENMASK_ULL(43, 34)) >> 34;
3302 instance_bank = (*pos & GENMASK_ULL(53, 44)) >> 44;
Tom St Denis32977f92016-10-09 07:41:26 -04003303
3304 if (se_bank == 0x3FF)
3305 se_bank = 0xFFFFFFFF;
3306 if (sh_bank == 0x3FF)
3307 sh_bank = 0xFFFFFFFF;
3308 if (instance_bank == 0x3FF)
3309 instance_bank = 0xFFFFFFFF;
Tom St Denis566281592016-06-27 11:55:07 -04003310 use_bank = 1;
Tom St Denis566281592016-06-27 11:55:07 -04003311 } else {
3312 use_bank = 0;
3313 }
3314
Tom St Denis801a6aa9a62017-03-15 05:34:25 -04003315 *pos &= (1UL << 22) - 1;
Tom St Denisbd122672016-07-28 09:39:22 -04003316
Tom St Denis566281592016-06-27 11:55:07 -04003317 if (use_bank) {
Tom St Denis32977f92016-10-09 07:41:26 -04003318 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
3319 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
Tom St Denis566281592016-06-27 11:55:07 -04003320 return -EINVAL;
3321 mutex_lock(&adev->grbm_idx_mutex);
3322 amdgpu_gfx_select_se_sh(adev, se_bank,
3323 sh_bank, instance_bank);
3324 }
3325
Tom St Denisbd122672016-07-28 09:39:22 -04003326 if (pm_pg_lock)
3327 mutex_lock(&adev->pm.mutex);
3328
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003329 while (size) {
3330 uint32_t value;
3331
3332 if (*pos > adev->rmmio_size)
Tom St Denis566281592016-06-27 11:55:07 -04003333 goto end;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003334
3335 value = RREG32(*pos >> 2);
3336 r = put_user(value, (uint32_t *)buf);
Tom St Denis566281592016-06-27 11:55:07 -04003337 if (r) {
3338 result = r;
3339 goto end;
3340 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003341
3342 result += 4;
3343 buf += 4;
3344 *pos += 4;
3345 size -= 4;
3346 }
3347
Tom St Denis566281592016-06-27 11:55:07 -04003348end:
3349 if (use_bank) {
3350 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
3351 mutex_unlock(&adev->grbm_idx_mutex);
3352 }
3353
Tom St Denisbd122672016-07-28 09:39:22 -04003354 if (pm_pg_lock)
3355 mutex_unlock(&adev->pm.mutex);
3356
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003357 return result;
3358}
3359
3360static ssize_t amdgpu_debugfs_regs_write(struct file *f, const char __user *buf,
3361 size_t size, loff_t *pos)
3362{
Al Viro45063092016-12-04 18:24:56 -05003363 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003364 ssize_t result = 0;
3365 int r;
Tom St Denis394fdde2016-10-10 07:31:23 -04003366 bool pm_pg_lock, use_bank;
3367 unsigned instance_bank, sh_bank, se_bank;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003368
3369 if (size & 0x3 || *pos & 0x3)
3370 return -EINVAL;
3371
Tom St Denis394fdde2016-10-10 07:31:23 -04003372 /* are we reading registers for which a PG lock is necessary? */
3373 pm_pg_lock = (*pos >> 23) & 1;
3374
3375 if (*pos & (1ULL << 62)) {
Tom St Denis0b968652017-11-10 12:54:50 -05003376 se_bank = (*pos & GENMASK_ULL(33, 24)) >> 24;
3377 sh_bank = (*pos & GENMASK_ULL(43, 34)) >> 34;
3378 instance_bank = (*pos & GENMASK_ULL(53, 44)) >> 44;
Tom St Denis394fdde2016-10-10 07:31:23 -04003379
3380 if (se_bank == 0x3FF)
3381 se_bank = 0xFFFFFFFF;
3382 if (sh_bank == 0x3FF)
3383 sh_bank = 0xFFFFFFFF;
3384 if (instance_bank == 0x3FF)
3385 instance_bank = 0xFFFFFFFF;
3386 use_bank = 1;
3387 } else {
3388 use_bank = 0;
3389 }
3390
Tom St Denis801a6aa9a62017-03-15 05:34:25 -04003391 *pos &= (1UL << 22) - 1;
Tom St Denis394fdde2016-10-10 07:31:23 -04003392
3393 if (use_bank) {
3394 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
3395 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
3396 return -EINVAL;
3397 mutex_lock(&adev->grbm_idx_mutex);
3398 amdgpu_gfx_select_se_sh(adev, se_bank,
3399 sh_bank, instance_bank);
3400 }
3401
3402 if (pm_pg_lock)
3403 mutex_lock(&adev->pm.mutex);
3404
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003405 while (size) {
3406 uint32_t value;
3407
3408 if (*pos > adev->rmmio_size)
3409 return result;
3410
3411 r = get_user(value, (uint32_t *)buf);
3412 if (r)
3413 return r;
3414
3415 WREG32(*pos >> 2, value);
3416
3417 result += 4;
3418 buf += 4;
3419 *pos += 4;
3420 size -= 4;
3421 }
3422
Tom St Denis394fdde2016-10-10 07:31:23 -04003423 if (use_bank) {
3424 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
3425 mutex_unlock(&adev->grbm_idx_mutex);
3426 }
3427
3428 if (pm_pg_lock)
3429 mutex_unlock(&adev->pm.mutex);
3430
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003431 return result;
3432}
3433
Tom St Denisadcec282016-04-15 13:08:44 -04003434static ssize_t amdgpu_debugfs_regs_pcie_read(struct file *f, char __user *buf,
3435 size_t size, loff_t *pos)
3436{
Al Viro45063092016-12-04 18:24:56 -05003437 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003438 ssize_t result = 0;
3439 int r;
3440
3441 if (size & 0x3 || *pos & 0x3)
3442 return -EINVAL;
3443
3444 while (size) {
3445 uint32_t value;
3446
3447 value = RREG32_PCIE(*pos >> 2);
3448 r = put_user(value, (uint32_t *)buf);
3449 if (r)
3450 return r;
3451
3452 result += 4;
3453 buf += 4;
3454 *pos += 4;
3455 size -= 4;
3456 }
3457
3458 return result;
3459}
3460
3461static ssize_t amdgpu_debugfs_regs_pcie_write(struct file *f, const char __user *buf,
3462 size_t size, loff_t *pos)
3463{
Al Viro45063092016-12-04 18:24:56 -05003464 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003465 ssize_t result = 0;
3466 int r;
3467
3468 if (size & 0x3 || *pos & 0x3)
3469 return -EINVAL;
3470
3471 while (size) {
3472 uint32_t value;
3473
3474 r = get_user(value, (uint32_t *)buf);
3475 if (r)
3476 return r;
3477
3478 WREG32_PCIE(*pos >> 2, value);
3479
3480 result += 4;
3481 buf += 4;
3482 *pos += 4;
3483 size -= 4;
3484 }
3485
3486 return result;
3487}
3488
3489static ssize_t amdgpu_debugfs_regs_didt_read(struct file *f, char __user *buf,
3490 size_t size, loff_t *pos)
3491{
Al Viro45063092016-12-04 18:24:56 -05003492 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003493 ssize_t result = 0;
3494 int r;
3495
3496 if (size & 0x3 || *pos & 0x3)
3497 return -EINVAL;
3498
3499 while (size) {
3500 uint32_t value;
3501
3502 value = RREG32_DIDT(*pos >> 2);
3503 r = put_user(value, (uint32_t *)buf);
3504 if (r)
3505 return r;
3506
3507 result += 4;
3508 buf += 4;
3509 *pos += 4;
3510 size -= 4;
3511 }
3512
3513 return result;
3514}
3515
3516static ssize_t amdgpu_debugfs_regs_didt_write(struct file *f, const char __user *buf,
3517 size_t size, loff_t *pos)
3518{
Al Viro45063092016-12-04 18:24:56 -05003519 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003520 ssize_t result = 0;
3521 int r;
3522
3523 if (size & 0x3 || *pos & 0x3)
3524 return -EINVAL;
3525
3526 while (size) {
3527 uint32_t value;
3528
3529 r = get_user(value, (uint32_t *)buf);
3530 if (r)
3531 return r;
3532
3533 WREG32_DIDT(*pos >> 2, value);
3534
3535 result += 4;
3536 buf += 4;
3537 *pos += 4;
3538 size -= 4;
3539 }
3540
3541 return result;
3542}
3543
3544static ssize_t amdgpu_debugfs_regs_smc_read(struct file *f, char __user *buf,
3545 size_t size, loff_t *pos)
3546{
Al Viro45063092016-12-04 18:24:56 -05003547 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003548 ssize_t result = 0;
3549 int r;
3550
3551 if (size & 0x3 || *pos & 0x3)
3552 return -EINVAL;
3553
3554 while (size) {
3555 uint32_t value;
3556
Tom St Denis6fc0dea2016-08-29 08:39:29 -04003557 value = RREG32_SMC(*pos);
Tom St Denisadcec282016-04-15 13:08:44 -04003558 r = put_user(value, (uint32_t *)buf);
3559 if (r)
3560 return r;
3561
3562 result += 4;
3563 buf += 4;
3564 *pos += 4;
3565 size -= 4;
3566 }
3567
3568 return result;
3569}
3570
3571static ssize_t amdgpu_debugfs_regs_smc_write(struct file *f, const char __user *buf,
3572 size_t size, loff_t *pos)
3573{
Al Viro45063092016-12-04 18:24:56 -05003574 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003575 ssize_t result = 0;
3576 int r;
3577
3578 if (size & 0x3 || *pos & 0x3)
3579 return -EINVAL;
3580
3581 while (size) {
3582 uint32_t value;
3583
3584 r = get_user(value, (uint32_t *)buf);
3585 if (r)
3586 return r;
3587
Tom St Denis6fc0dea2016-08-29 08:39:29 -04003588 WREG32_SMC(*pos, value);
Tom St Denisadcec282016-04-15 13:08:44 -04003589
3590 result += 4;
3591 buf += 4;
3592 *pos += 4;
3593 size -= 4;
3594 }
3595
3596 return result;
3597}
3598
Tom St Denis1e051412016-06-27 09:57:18 -04003599static ssize_t amdgpu_debugfs_gca_config_read(struct file *f, char __user *buf,
3600 size_t size, loff_t *pos)
3601{
Al Viro45063092016-12-04 18:24:56 -05003602 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denis1e051412016-06-27 09:57:18 -04003603 ssize_t result = 0;
3604 int r;
3605 uint32_t *config, no_regs = 0;
3606
3607 if (size & 0x3 || *pos & 0x3)
3608 return -EINVAL;
3609
Markus Elfringecab7662016-09-18 17:00:52 +02003610 config = kmalloc_array(256, sizeof(*config), GFP_KERNEL);
Tom St Denis1e051412016-06-27 09:57:18 -04003611 if (!config)
3612 return -ENOMEM;
3613
3614 /* version, increment each time something is added */
Tom St Denis9a999352017-01-18 13:01:25 -05003615 config[no_regs++] = 3;
Tom St Denis1e051412016-06-27 09:57:18 -04003616 config[no_regs++] = adev->gfx.config.max_shader_engines;
3617 config[no_regs++] = adev->gfx.config.max_tile_pipes;
3618 config[no_regs++] = adev->gfx.config.max_cu_per_sh;
3619 config[no_regs++] = adev->gfx.config.max_sh_per_se;
3620 config[no_regs++] = adev->gfx.config.max_backends_per_se;
3621 config[no_regs++] = adev->gfx.config.max_texture_channel_caches;
3622 config[no_regs++] = adev->gfx.config.max_gprs;
3623 config[no_regs++] = adev->gfx.config.max_gs_threads;
3624 config[no_regs++] = adev->gfx.config.max_hw_contexts;
3625 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_frontend;
3626 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_backend;
3627 config[no_regs++] = adev->gfx.config.sc_hiz_tile_fifo_size;
3628 config[no_regs++] = adev->gfx.config.sc_earlyz_tile_fifo_size;
3629 config[no_regs++] = adev->gfx.config.num_tile_pipes;
3630 config[no_regs++] = adev->gfx.config.backend_enable_mask;
3631 config[no_regs++] = adev->gfx.config.mem_max_burst_length_bytes;
3632 config[no_regs++] = adev->gfx.config.mem_row_size_in_kb;
3633 config[no_regs++] = adev->gfx.config.shader_engine_tile_size;
3634 config[no_regs++] = adev->gfx.config.num_gpus;
3635 config[no_regs++] = adev->gfx.config.multi_gpu_tile_size;
3636 config[no_regs++] = adev->gfx.config.mc_arb_ramcfg;
3637 config[no_regs++] = adev->gfx.config.gb_addr_config;
3638 config[no_regs++] = adev->gfx.config.num_rbs;
3639
Tom St Denis89a8f302016-08-12 15:14:31 -04003640 /* rev==1 */
3641 config[no_regs++] = adev->rev_id;
3642 config[no_regs++] = adev->pg_flags;
3643 config[no_regs++] = adev->cg_flags;
3644
Tom St Denise9f11dc2016-08-17 12:00:51 -04003645 /* rev==2 */
3646 config[no_regs++] = adev->family;
3647 config[no_regs++] = adev->external_rev_id;
3648
Tom St Denis9a999352017-01-18 13:01:25 -05003649 /* rev==3 */
3650 config[no_regs++] = adev->pdev->device;
3651 config[no_regs++] = adev->pdev->revision;
3652 config[no_regs++] = adev->pdev->subsystem_device;
3653 config[no_regs++] = adev->pdev->subsystem_vendor;
3654
Tom St Denis1e051412016-06-27 09:57:18 -04003655 while (size && (*pos < no_regs * 4)) {
3656 uint32_t value;
3657
3658 value = config[*pos >> 2];
3659 r = put_user(value, (uint32_t *)buf);
3660 if (r) {
3661 kfree(config);
3662 return r;
3663 }
3664
3665 result += 4;
3666 buf += 4;
3667 *pos += 4;
3668 size -= 4;
3669 }
3670
3671 kfree(config);
3672 return result;
3673}
3674
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003675static ssize_t amdgpu_debugfs_sensor_read(struct file *f, char __user *buf,
3676 size_t size, loff_t *pos)
3677{
Al Viro45063092016-12-04 18:24:56 -05003678 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003679 int idx, x, outsize, r, valuesize;
3680 uint32_t values[16];
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003681
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003682 if (size & 3 || *pos & 0x3)
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003683 return -EINVAL;
3684
Samuel Pitoiset3cbc6142017-02-15 19:32:29 +01003685 if (amdgpu_dpm == 0)
3686 return -EINVAL;
3687
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003688 /* convert offset to sensor number */
3689 idx = *pos >> 2;
3690
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003691 valuesize = sizeof(values);
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003692 if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->read_sensor)
Rex Zhucd4d7462017-09-06 18:43:52 +08003693 r = amdgpu_dpm_read_sensor(adev, idx, &values[0], &valuesize);
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003694 else
3695 return -EINVAL;
3696
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003697 if (size > valuesize)
3698 return -EINVAL;
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003699
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003700 outsize = 0;
3701 x = 0;
3702 if (!r) {
3703 while (size) {
3704 r = put_user(values[x++], (int32_t *)buf);
3705 buf += 4;
3706 size -= 4;
3707 outsize += 4;
3708 }
3709 }
3710
3711 return !r ? outsize : r;
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003712}
Tom St Denis1e051412016-06-27 09:57:18 -04003713
Tom St Denis273d7aa2016-10-11 14:48:55 -04003714static ssize_t amdgpu_debugfs_wave_read(struct file *f, char __user *buf,
3715 size_t size, loff_t *pos)
3716{
3717 struct amdgpu_device *adev = f->f_inode->i_private;
3718 int r, x;
3719 ssize_t result=0;
Tom St Denis472259f2016-10-14 09:49:09 -04003720 uint32_t offset, se, sh, cu, wave, simd, data[32];
Tom St Denis273d7aa2016-10-11 14:48:55 -04003721
3722 if (size & 3 || *pos & 3)
3723 return -EINVAL;
3724
3725 /* decode offset */
Tom St Denis0b968652017-11-10 12:54:50 -05003726 offset = (*pos & GENMASK_ULL(6, 0));
3727 se = (*pos & GENMASK_ULL(14, 7)) >> 7;
3728 sh = (*pos & GENMASK_ULL(22, 15)) >> 15;
3729 cu = (*pos & GENMASK_ULL(30, 23)) >> 23;
3730 wave = (*pos & GENMASK_ULL(36, 31)) >> 31;
3731 simd = (*pos & GENMASK_ULL(44, 37)) >> 37;
Tom St Denis273d7aa2016-10-11 14:48:55 -04003732
3733 /* switch to the specific se/sh/cu */
3734 mutex_lock(&adev->grbm_idx_mutex);
3735 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
3736
3737 x = 0;
Tom St Denis472259f2016-10-14 09:49:09 -04003738 if (adev->gfx.funcs->read_wave_data)
3739 adev->gfx.funcs->read_wave_data(adev, simd, wave, data, &x);
Tom St Denis273d7aa2016-10-11 14:48:55 -04003740
3741 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
3742 mutex_unlock(&adev->grbm_idx_mutex);
3743
Tom St Denis5ecfb3b2016-10-13 12:15:03 -04003744 if (!x)
3745 return -EINVAL;
3746
Tom St Denis472259f2016-10-14 09:49:09 -04003747 while (size && (offset < x * 4)) {
Tom St Denis273d7aa2016-10-11 14:48:55 -04003748 uint32_t value;
3749
Tom St Denis472259f2016-10-14 09:49:09 -04003750 value = data[offset >> 2];
Tom St Denis273d7aa2016-10-11 14:48:55 -04003751 r = put_user(value, (uint32_t *)buf);
3752 if (r)
3753 return r;
3754
3755 result += 4;
3756 buf += 4;
Tom St Denis472259f2016-10-14 09:49:09 -04003757 offset += 4;
Tom St Denis273d7aa2016-10-11 14:48:55 -04003758 size -= 4;
3759 }
3760
3761 return result;
3762}
3763
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003764static ssize_t amdgpu_debugfs_gpr_read(struct file *f, char __user *buf,
3765 size_t size, loff_t *pos)
3766{
3767 struct amdgpu_device *adev = f->f_inode->i_private;
3768 int r;
3769 ssize_t result = 0;
3770 uint32_t offset, se, sh, cu, wave, simd, thread, bank, *data;
3771
3772 if (size & 3 || *pos & 3)
3773 return -EINVAL;
3774
3775 /* decode offset */
Tom St Denis0b968652017-11-10 12:54:50 -05003776 offset = *pos & GENMASK_ULL(11, 0);
3777 se = (*pos & GENMASK_ULL(19, 12)) >> 12;
3778 sh = (*pos & GENMASK_ULL(27, 20)) >> 20;
3779 cu = (*pos & GENMASK_ULL(35, 28)) >> 28;
3780 wave = (*pos & GENMASK_ULL(43, 36)) >> 36;
3781 simd = (*pos & GENMASK_ULL(51, 44)) >> 44;
3782 thread = (*pos & GENMASK_ULL(59, 52)) >> 52;
3783 bank = (*pos & GENMASK_ULL(61, 60)) >> 60;
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003784
3785 data = kmalloc_array(1024, sizeof(*data), GFP_KERNEL);
3786 if (!data)
3787 return -ENOMEM;
3788
3789 /* switch to the specific se/sh/cu */
3790 mutex_lock(&adev->grbm_idx_mutex);
3791 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
3792
3793 if (bank == 0) {
3794 if (adev->gfx.funcs->read_wave_vgprs)
3795 adev->gfx.funcs->read_wave_vgprs(adev, simd, wave, thread, offset, size>>2, data);
3796 } else {
3797 if (adev->gfx.funcs->read_wave_sgprs)
3798 adev->gfx.funcs->read_wave_sgprs(adev, simd, wave, offset, size>>2, data);
3799 }
3800
3801 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
3802 mutex_unlock(&adev->grbm_idx_mutex);
3803
3804 while (size) {
3805 uint32_t value;
3806
3807 value = data[offset++];
3808 r = put_user(value, (uint32_t *)buf);
3809 if (r) {
3810 result = r;
3811 goto err;
3812 }
3813
3814 result += 4;
3815 buf += 4;
3816 size -= 4;
3817 }
3818
3819err:
3820 kfree(data);
3821 return result;
3822}
3823
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003824static const struct file_operations amdgpu_debugfs_regs_fops = {
3825 .owner = THIS_MODULE,
3826 .read = amdgpu_debugfs_regs_read,
3827 .write = amdgpu_debugfs_regs_write,
3828 .llseek = default_llseek
3829};
Tom St Denisadcec282016-04-15 13:08:44 -04003830static const struct file_operations amdgpu_debugfs_regs_didt_fops = {
3831 .owner = THIS_MODULE,
3832 .read = amdgpu_debugfs_regs_didt_read,
3833 .write = amdgpu_debugfs_regs_didt_write,
3834 .llseek = default_llseek
3835};
3836static const struct file_operations amdgpu_debugfs_regs_pcie_fops = {
3837 .owner = THIS_MODULE,
3838 .read = amdgpu_debugfs_regs_pcie_read,
3839 .write = amdgpu_debugfs_regs_pcie_write,
3840 .llseek = default_llseek
3841};
3842static const struct file_operations amdgpu_debugfs_regs_smc_fops = {
3843 .owner = THIS_MODULE,
3844 .read = amdgpu_debugfs_regs_smc_read,
3845 .write = amdgpu_debugfs_regs_smc_write,
3846 .llseek = default_llseek
3847};
3848
Tom St Denis1e051412016-06-27 09:57:18 -04003849static const struct file_operations amdgpu_debugfs_gca_config_fops = {
3850 .owner = THIS_MODULE,
3851 .read = amdgpu_debugfs_gca_config_read,
3852 .llseek = default_llseek
3853};
3854
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003855static const struct file_operations amdgpu_debugfs_sensors_fops = {
3856 .owner = THIS_MODULE,
3857 .read = amdgpu_debugfs_sensor_read,
3858 .llseek = default_llseek
3859};
3860
Tom St Denis273d7aa2016-10-11 14:48:55 -04003861static const struct file_operations amdgpu_debugfs_wave_fops = {
3862 .owner = THIS_MODULE,
3863 .read = amdgpu_debugfs_wave_read,
3864 .llseek = default_llseek
3865};
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003866static const struct file_operations amdgpu_debugfs_gpr_fops = {
3867 .owner = THIS_MODULE,
3868 .read = amdgpu_debugfs_gpr_read,
3869 .llseek = default_llseek
3870};
Tom St Denis273d7aa2016-10-11 14:48:55 -04003871
Tom St Denisadcec282016-04-15 13:08:44 -04003872static const struct file_operations *debugfs_regs[] = {
3873 &amdgpu_debugfs_regs_fops,
3874 &amdgpu_debugfs_regs_didt_fops,
3875 &amdgpu_debugfs_regs_pcie_fops,
3876 &amdgpu_debugfs_regs_smc_fops,
Tom St Denis1e051412016-06-27 09:57:18 -04003877 &amdgpu_debugfs_gca_config_fops,
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003878 &amdgpu_debugfs_sensors_fops,
Tom St Denis273d7aa2016-10-11 14:48:55 -04003879 &amdgpu_debugfs_wave_fops,
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003880 &amdgpu_debugfs_gpr_fops,
Tom St Denisadcec282016-04-15 13:08:44 -04003881};
3882
3883static const char *debugfs_regs_names[] = {
3884 "amdgpu_regs",
3885 "amdgpu_regs_didt",
3886 "amdgpu_regs_pcie",
3887 "amdgpu_regs_smc",
Tom St Denis1e051412016-06-27 09:57:18 -04003888 "amdgpu_gca_config",
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003889 "amdgpu_sensors",
Tom St Denis273d7aa2016-10-11 14:48:55 -04003890 "amdgpu_wave",
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003891 "amdgpu_gpr",
Tom St Denisadcec282016-04-15 13:08:44 -04003892};
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003893
3894static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
3895{
3896 struct drm_minor *minor = adev->ddev->primary;
3897 struct dentry *ent, *root = minor->debugfs_root;
Tom St Denisadcec282016-04-15 13:08:44 -04003898 unsigned i, j;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003899
Tom St Denisadcec282016-04-15 13:08:44 -04003900 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3901 ent = debugfs_create_file(debugfs_regs_names[i],
3902 S_IFREG | S_IRUGO, root,
3903 adev, debugfs_regs[i]);
3904 if (IS_ERR(ent)) {
3905 for (j = 0; j < i; j++) {
3906 debugfs_remove(adev->debugfs_regs[i]);
3907 adev->debugfs_regs[i] = NULL;
3908 }
3909 return PTR_ERR(ent);
3910 }
3911
3912 if (!i)
3913 i_size_write(ent->d_inode, adev->rmmio_size);
3914 adev->debugfs_regs[i] = ent;
3915 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003916
3917 return 0;
3918}
3919
3920static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev)
3921{
Tom St Denisadcec282016-04-15 13:08:44 -04003922 unsigned i;
3923
3924 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3925 if (adev->debugfs_regs[i]) {
3926 debugfs_remove(adev->debugfs_regs[i]);
3927 adev->debugfs_regs[i] = NULL;
3928 }
3929 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003930}
3931
Huang Rui4f0955f2017-05-10 23:04:06 +08003932static int amdgpu_debugfs_test_ib(struct seq_file *m, void *data)
3933{
3934 struct drm_info_node *node = (struct drm_info_node *) m->private;
3935 struct drm_device *dev = node->minor->dev;
3936 struct amdgpu_device *adev = dev->dev_private;
3937 int r = 0, i;
3938
3939 /* hold on the scheduler */
3940 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
3941 struct amdgpu_ring *ring = adev->rings[i];
3942
3943 if (!ring || !ring->sched.thread)
3944 continue;
3945 kthread_park(ring->sched.thread);
3946 }
3947
3948 seq_printf(m, "run ib test:\n");
3949 r = amdgpu_ib_ring_tests(adev);
3950 if (r)
3951 seq_printf(m, "ib ring tests failed (%d).\n", r);
3952 else
3953 seq_printf(m, "ib ring tests passed.\n");
3954
3955 /* go on the scheduler */
3956 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
3957 struct amdgpu_ring *ring = adev->rings[i];
3958
3959 if (!ring || !ring->sched.thread)
3960 continue;
3961 kthread_unpark(ring->sched.thread);
3962 }
3963
3964 return 0;
3965}
3966
3967static const struct drm_info_list amdgpu_debugfs_test_ib_ring_list[] = {
3968 {"amdgpu_test_ib", &amdgpu_debugfs_test_ib}
3969};
3970
3971static int amdgpu_debugfs_test_ib_ring_init(struct amdgpu_device *adev)
3972{
3973 return amdgpu_debugfs_add_files(adev,
3974 amdgpu_debugfs_test_ib_ring_list, 1);
3975}
3976
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003977int amdgpu_debugfs_init(struct drm_minor *minor)
3978{
3979 return 0;
3980}
Kent Russelldb95e212017-08-22 12:31:43 -04003981
3982static int amdgpu_debugfs_get_vbios_dump(struct seq_file *m, void *data)
3983{
3984 struct drm_info_node *node = (struct drm_info_node *) m->private;
3985 struct drm_device *dev = node->minor->dev;
3986 struct amdgpu_device *adev = dev->dev_private;
3987
3988 seq_write(m, adev->bios, adev->bios_size);
3989 return 0;
3990}
3991
Kent Russelldb95e212017-08-22 12:31:43 -04003992static const struct drm_info_list amdgpu_vbios_dump_list[] = {
3993 {"amdgpu_vbios",
3994 amdgpu_debugfs_get_vbios_dump,
3995 0, NULL},
3996};
3997
Kent Russelldb95e212017-08-22 12:31:43 -04003998static int amdgpu_debugfs_vbios_dump_init(struct amdgpu_device *adev)
3999{
4000 return amdgpu_debugfs_add_files(adev,
4001 amdgpu_vbios_dump_list, 1);
4002}
Alexander Kuleshov7cebc722015-06-27 13:16:05 +06004003#else
Arnd Bergmann27bad5b2017-06-21 23:51:02 +02004004static int amdgpu_debugfs_test_ib_ring_init(struct amdgpu_device *adev)
Huang Rui4f0955f2017-05-10 23:04:06 +08004005{
4006 return 0;
4007}
Alexander Kuleshov7cebc722015-06-27 13:16:05 +06004008static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
4009{
4010 return 0;
4011}
Kent Russelldb95e212017-08-22 12:31:43 -04004012static int amdgpu_debugfs_vbios_dump_init(struct amdgpu_device *adev)
4013{
4014 return 0;
4015}
Alexander Kuleshov7cebc722015-06-27 13:16:05 +06004016static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev) { }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04004017#endif