blob: 83dbd02004b0981c2403e38586d06af575b81ffa [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
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400413 /* doorbell bar mapping */
414 adev->doorbell.base = pci_resource_start(adev->pdev, 2);
415 adev->doorbell.size = pci_resource_len(adev->pdev, 2);
416
Christian Königedf600d2016-05-03 15:54:54 +0200417 adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400418 AMDGPU_DOORBELL_MAX_ASSIGNMENT+1);
419 if (adev->doorbell.num_doorbells == 0)
420 return -EINVAL;
421
Christian König8972e5d2017-03-06 13:34:57 +0100422 adev->doorbell.ptr = ioremap(adev->doorbell.base,
423 adev->doorbell.num_doorbells *
424 sizeof(u32));
425 if (adev->doorbell.ptr == NULL)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400426 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400427
428 return 0;
429}
430
431/**
432 * amdgpu_doorbell_fini - Tear down doorbell driver information.
433 *
434 * @adev: amdgpu_device pointer
435 *
436 * Tear down doorbell driver information (CIK)
437 */
438static void amdgpu_doorbell_fini(struct amdgpu_device *adev)
439{
440 iounmap(adev->doorbell.ptr);
441 adev->doorbell.ptr = NULL;
442}
443
444/**
445 * amdgpu_doorbell_get_kfd_info - Report doorbell configuration required to
446 * setup amdkfd
447 *
448 * @adev: amdgpu_device pointer
449 * @aperture_base: output returning doorbell aperture base physical address
450 * @aperture_size: output returning doorbell aperture size in bytes
451 * @start_offset: output returning # of doorbell bytes reserved for amdgpu.
452 *
453 * amdgpu and amdkfd share the doorbell aperture. amdgpu sets it up,
454 * takes doorbells required for its own rings and reports the setup to amdkfd.
455 * amdgpu reserved doorbells are at the start of the doorbell aperture.
456 */
457void amdgpu_doorbell_get_kfd_info(struct amdgpu_device *adev,
458 phys_addr_t *aperture_base,
459 size_t *aperture_size,
460 size_t *start_offset)
461{
462 /*
463 * The first num_doorbells are used by amdgpu.
464 * amdkfd takes whatever's left in the aperture.
465 */
466 if (adev->doorbell.size > adev->doorbell.num_doorbells * sizeof(u32)) {
467 *aperture_base = adev->doorbell.base;
468 *aperture_size = adev->doorbell.size;
469 *start_offset = adev->doorbell.num_doorbells * sizeof(u32);
470 } else {
471 *aperture_base = 0;
472 *aperture_size = 0;
473 *start_offset = 0;
474 }
475}
476
477/*
478 * amdgpu_wb_*()
Alex Xie455a7bc2017-05-08 21:36:03 -0400479 * Writeback is the method by which the GPU updates special pages in memory
Alex Xieea81a172017-05-08 13:41:11 -0400480 * with the status of certain GPU events (fences, ring pointers,etc.).
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400481 */
482
483/**
484 * amdgpu_wb_fini - Disable Writeback and free memory
485 *
486 * @adev: amdgpu_device pointer
487 *
488 * Disables Writeback and frees the Writeback memory (all asics).
489 * Used at driver shutdown.
490 */
491static void amdgpu_wb_fini(struct amdgpu_device *adev)
492{
493 if (adev->wb.wb_obj) {
Alex Deuchera76ed482016-10-21 15:30:36 -0400494 amdgpu_bo_free_kernel(&adev->wb.wb_obj,
495 &adev->wb.gpu_addr,
496 (void **)&adev->wb.wb);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400497 adev->wb.wb_obj = NULL;
498 }
499}
500
501/**
502 * amdgpu_wb_init- Init Writeback driver info and allocate memory
503 *
504 * @adev: amdgpu_device pointer
505 *
Alex Xie455a7bc2017-05-08 21:36:03 -0400506 * Initializes writeback and allocates writeback memory (all asics).
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400507 * Used at driver startup.
508 * Returns 0 on success or an -error on failure.
509 */
510static int amdgpu_wb_init(struct amdgpu_device *adev)
511{
512 int r;
513
514 if (adev->wb.wb_obj == NULL) {
Alex Deucher97407b62017-07-28 12:14:15 -0400515 /* AMDGPU_MAX_WB * sizeof(uint32_t) * 8 = AMDGPU_MAX_WB 256bit slots */
516 r = amdgpu_bo_create_kernel(adev, AMDGPU_MAX_WB * sizeof(uint32_t) * 8,
Alex Deuchera76ed482016-10-21 15:30:36 -0400517 PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
518 &adev->wb.wb_obj, &adev->wb.gpu_addr,
519 (void **)&adev->wb.wb);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400520 if (r) {
521 dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
522 return r;
523 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400524
525 adev->wb.num_wb = AMDGPU_MAX_WB;
526 memset(&adev->wb.used, 0, sizeof(adev->wb.used));
527
528 /* clear wb memory */
Huang Rui60a970a62017-03-15 10:13:32 +0800529 memset((char *)adev->wb.wb, 0, AMDGPU_MAX_WB * sizeof(uint32_t));
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400530 }
531
532 return 0;
533}
534
535/**
536 * amdgpu_wb_get - Allocate a wb entry
537 *
538 * @adev: amdgpu_device pointer
539 * @wb: wb index
540 *
541 * Allocate a wb slot for use by the driver (all asics).
542 * Returns 0 on success or -EINVAL on failure.
543 */
544int amdgpu_wb_get(struct amdgpu_device *adev, u32 *wb)
545{
546 unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
Alex Deucher97407b62017-07-28 12:14:15 -0400547
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400548 if (offset < adev->wb.num_wb) {
549 __set_bit(offset, adev->wb.used);
Monk Liu63ae07c2017-10-17 19:18:56 +0800550 *wb = offset << 3; /* convert to dw offset */
Monk Liu0915fdb2017-06-19 10:19:41 -0400551 return 0;
552 } else {
553 return -EINVAL;
554 }
555}
556
Ken Wang70142852016-03-18 15:08:49 +0800557/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400558 * amdgpu_wb_free - Free a wb entry
559 *
560 * @adev: amdgpu_device pointer
561 * @wb: wb index
562 *
563 * Free a wb slot allocated for use by the driver (all asics)
564 */
565void amdgpu_wb_free(struct amdgpu_device *adev, u32 wb)
566{
567 if (wb < adev->wb.num_wb)
Monk Liu63ae07c2017-10-17 19:18:56 +0800568 __clear_bit(wb >> 3, adev->wb.used);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400569}
570
571/**
572 * amdgpu_vram_location - try to find VRAM location
573 * @adev: amdgpu device structure holding all necessary informations
574 * @mc: memory controller structure holding memory informations
575 * @base: base address at which to put VRAM
576 *
Alex Xie455a7bc2017-05-08 21:36:03 -0400577 * Function will try to place VRAM at base address provided
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400578 * as parameter (which is so far either PCI aperture address or
579 * for IGP TOM base address).
580 *
581 * If there is not enough space to fit the unvisible VRAM in the 32bits
582 * address space then we limit the VRAM size to the aperture.
583 *
584 * Note: We don't explicitly enforce VRAM start to be aligned on VRAM size,
585 * this shouldn't be a problem as we are using the PCI aperture as a reference.
586 * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but
587 * not IGP.
588 *
589 * Note: we use mc_vram_size as on some board we need to program the mc to
590 * cover the whole aperture even if VRAM size is inferior to aperture size
591 * Novell bug 204882 + along with lots of ubuntu ones
592 *
593 * Note: when limiting vram it's safe to overwritte real_vram_size because
594 * we are not in case where real_vram_size is inferior to mc_vram_size (ie
595 * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu
596 * ones)
597 *
598 * Note: IGP TOM addr should be the same as the aperture addr, we don't
Alex Xie455a7bc2017-05-08 21:36:03 -0400599 * explicitly check for that though.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400600 *
601 * FIXME: when reducing VRAM size align new size on power of 2.
602 */
603void amdgpu_vram_location(struct amdgpu_device *adev, struct amdgpu_mc *mc, u64 base)
604{
605 uint64_t limit = (uint64_t)amdgpu_vram_limit << 20;
606
607 mc->vram_start = base;
608 if (mc->mc_vram_size > (adev->mc.mc_mask - base + 1)) {
609 dev_warn(adev->dev, "limiting VRAM to PCI aperture size\n");
610 mc->real_vram_size = mc->aper_size;
611 mc->mc_vram_size = mc->aper_size;
612 }
613 mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
614 if (limit && limit < mc->real_vram_size)
615 mc->real_vram_size = limit;
616 dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
617 mc->mc_vram_size >> 20, mc->vram_start,
618 mc->vram_end, mc->real_vram_size >> 20);
619}
620
621/**
Christian König6f02a692017-07-07 11:56:59 +0200622 * amdgpu_gart_location - try to find GTT location
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400623 * @adev: amdgpu device structure holding all necessary informations
624 * @mc: memory controller structure holding memory informations
625 *
626 * Function will place try to place GTT before or after VRAM.
627 *
628 * If GTT size is bigger than space left then we ajust GTT size.
629 * Thus function will never fails.
630 *
631 * FIXME: when reducing GTT size align new size on power of 2.
632 */
Christian König6f02a692017-07-07 11:56:59 +0200633void amdgpu_gart_location(struct amdgpu_device *adev, struct amdgpu_mc *mc)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400634{
635 u64 size_af, size_bf;
636
Christian Königed21c042017-07-06 22:26:05 +0200637 size_af = adev->mc.mc_mask - mc->vram_end;
638 size_bf = mc->vram_start;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400639 if (size_bf > size_af) {
Christian König6f02a692017-07-07 11:56:59 +0200640 if (mc->gart_size > size_bf) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400641 dev_warn(adev->dev, "limiting GTT\n");
Christian König6f02a692017-07-07 11:56:59 +0200642 mc->gart_size = size_bf;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400643 }
Christian König6f02a692017-07-07 11:56:59 +0200644 mc->gart_start = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400645 } else {
Christian König6f02a692017-07-07 11:56:59 +0200646 if (mc->gart_size > size_af) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400647 dev_warn(adev->dev, "limiting GTT\n");
Christian König6f02a692017-07-07 11:56:59 +0200648 mc->gart_size = size_af;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400649 }
Christian König6f02a692017-07-07 11:56:59 +0200650 mc->gart_start = mc->vram_end + 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400651 }
Christian König6f02a692017-07-07 11:56:59 +0200652 mc->gart_end = mc->gart_start + mc->gart_size - 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400653 dev_info(adev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
Christian König6f02a692017-07-07 11:56:59 +0200654 mc->gart_size >> 20, mc->gart_start, mc->gart_end);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400655}
656
657/*
Horace Chena05502e2017-09-29 14:41:57 +0800658 * Firmware Reservation functions
659 */
660/**
661 * amdgpu_fw_reserve_vram_fini - free fw reserved vram
662 *
663 * @adev: amdgpu_device pointer
664 *
665 * free fw reserved vram if it has been reserved.
666 */
667void amdgpu_fw_reserve_vram_fini(struct amdgpu_device *adev)
668{
669 amdgpu_bo_free_kernel(&adev->fw_vram_usage.reserved_bo,
670 NULL, &adev->fw_vram_usage.va);
671}
672
673/**
674 * amdgpu_fw_reserve_vram_init - create bo vram reservation from fw
675 *
676 * @adev: amdgpu_device pointer
677 *
678 * create bo vram reservation from fw.
679 */
680int amdgpu_fw_reserve_vram_init(struct amdgpu_device *adev)
681{
682 int r = 0;
Horace Chen3c738892017-11-01 19:32:11 +0800683 int i;
Horace Chena05502e2017-09-29 14:41:57 +0800684 u64 gpu_addr;
685 u64 vram_size = adev->mc.visible_vram_size;
Horace Chen3c738892017-11-01 19:32:11 +0800686 u64 offset = adev->fw_vram_usage.start_offset;
687 u64 size = adev->fw_vram_usage.size;
688 struct amdgpu_bo *bo;
Horace Chena05502e2017-09-29 14:41:57 +0800689
690 adev->fw_vram_usage.va = NULL;
691 adev->fw_vram_usage.reserved_bo = NULL;
692
693 if (adev->fw_vram_usage.size > 0 &&
694 adev->fw_vram_usage.size <= vram_size) {
695
696 r = amdgpu_bo_create(adev, adev->fw_vram_usage.size,
Horace Chen3c738892017-11-01 19:32:11 +0800697 PAGE_SIZE, true, AMDGPU_GEM_DOMAIN_VRAM,
Horace Chena05502e2017-09-29 14:41:57 +0800698 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
699 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS, NULL, NULL, 0,
700 &adev->fw_vram_usage.reserved_bo);
701 if (r)
702 goto error_create;
703
704 r = amdgpu_bo_reserve(adev->fw_vram_usage.reserved_bo, false);
705 if (r)
706 goto error_reserve;
Horace Chen3c738892017-11-01 19:32:11 +0800707
708 /* remove the original mem node and create a new one at the
709 * request position
710 */
711 bo = adev->fw_vram_usage.reserved_bo;
712 offset = ALIGN(offset, PAGE_SIZE);
713 for (i = 0; i < bo->placement.num_placement; ++i) {
714 bo->placements[i].fpfn = offset >> PAGE_SHIFT;
715 bo->placements[i].lpfn = (offset + size) >> PAGE_SHIFT;
716 }
717
718 ttm_bo_mem_put(&bo->tbo, &bo->tbo.mem);
719 r = ttm_bo_mem_space(&bo->tbo, &bo->placement, &bo->tbo.mem,
720 false, false);
721 if (r)
722 goto error_pin;
723
Horace Chena05502e2017-09-29 14:41:57 +0800724 r = amdgpu_bo_pin_restricted(adev->fw_vram_usage.reserved_bo,
725 AMDGPU_GEM_DOMAIN_VRAM,
726 adev->fw_vram_usage.start_offset,
727 (adev->fw_vram_usage.start_offset +
728 adev->fw_vram_usage.size), &gpu_addr);
729 if (r)
730 goto error_pin;
731 r = amdgpu_bo_kmap(adev->fw_vram_usage.reserved_bo,
732 &adev->fw_vram_usage.va);
733 if (r)
734 goto error_kmap;
735
736 amdgpu_bo_unreserve(adev->fw_vram_usage.reserved_bo);
737 }
738 return r;
739
740error_kmap:
741 amdgpu_bo_unpin(adev->fw_vram_usage.reserved_bo);
742error_pin:
743 amdgpu_bo_unreserve(adev->fw_vram_usage.reserved_bo);
744error_reserve:
745 amdgpu_bo_unref(&adev->fw_vram_usage.reserved_bo);
746error_create:
747 adev->fw_vram_usage.va = NULL;
748 adev->fw_vram_usage.reserved_bo = NULL;
749 return r;
750}
751
752
753/*
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400754 * GPU helpers function.
755 */
756/**
Jim Quc836fec2017-02-10 15:59:59 +0800757 * amdgpu_need_post - check if the hw need post or not
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400758 *
759 * @adev: amdgpu_device pointer
760 *
Jim Quc836fec2017-02-10 15:59:59 +0800761 * Check if the asic has been initialized (all asics) at driver startup
762 * or post is needed if hw reset is performed.
763 * Returns true if need or false if not.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400764 */
Jim Quc836fec2017-02-10 15:59:59 +0800765bool amdgpu_need_post(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400766{
767 uint32_t reg;
768
Monk Liubec86372016-09-14 19:38:08 +0800769 if (amdgpu_sriov_vf(adev))
770 return false;
771
772 if (amdgpu_passthrough(adev)) {
Monk Liu1da2c322016-11-11 11:24:29 +0800773 /* for FIJI: In whole GPU pass-through virtualization case, after VM reboot
774 * some old smc fw still need driver do vPost otherwise gpu hang, while
775 * those smc fw version above 22.15 doesn't have this flaw, so we force
776 * vpost executed for smc version below 22.15
Monk Liubec86372016-09-14 19:38:08 +0800777 */
778 if (adev->asic_type == CHIP_FIJI) {
779 int err;
780 uint32_t fw_ver;
781 err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
782 /* force vPost if error occured */
783 if (err)
784 return true;
785
786 fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
Monk Liu1da2c322016-11-11 11:24:29 +0800787 if (fw_ver < 0x00160e00)
788 return true;
Monk Liubec86372016-09-14 19:38:08 +0800789 }
Monk Liubec86372016-09-14 19:38:08 +0800790 }
pding91fe77e2017-10-19 09:38:39 +0800791
792 if (adev->has_hw_reset) {
793 adev->has_hw_reset = false;
794 return true;
795 }
796
797 /* bios scratch used on CIK+ */
798 if (adev->asic_type >= CHIP_BONAIRE)
799 return amdgpu_atombios_scratch_need_asic_init(adev);
800
801 /* check MEM_SIZE for older asics */
802 reg = amdgpu_asic_get_config_memsize(adev);
803
804 if ((reg != 0) && (reg != 0xffffffff))
805 return false;
806
807 return true;
Monk Liubec86372016-09-14 19:38:08 +0800808}
809
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400810/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400811 * amdgpu_dummy_page_init - init dummy page used by the driver
812 *
813 * @adev: amdgpu_device pointer
814 *
815 * Allocate the dummy page used by the driver (all asics).
816 * This dummy page is used by the driver as a filler for gart entries
817 * when pages are taken out of the GART
818 * Returns 0 on sucess, -ENOMEM on failure.
819 */
820int amdgpu_dummy_page_init(struct amdgpu_device *adev)
821{
822 if (adev->dummy_page.page)
823 return 0;
824 adev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
825 if (adev->dummy_page.page == NULL)
826 return -ENOMEM;
827 adev->dummy_page.addr = pci_map_page(adev->pdev, adev->dummy_page.page,
828 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
829 if (pci_dma_mapping_error(adev->pdev, adev->dummy_page.addr)) {
830 dev_err(&adev->pdev->dev, "Failed to DMA MAP the dummy page\n");
831 __free_page(adev->dummy_page.page);
832 adev->dummy_page.page = NULL;
833 return -ENOMEM;
834 }
835 return 0;
836}
837
838/**
839 * amdgpu_dummy_page_fini - free dummy page used by the driver
840 *
841 * @adev: amdgpu_device pointer
842 *
843 * Frees the dummy page used by the driver (all asics).
844 */
845void amdgpu_dummy_page_fini(struct amdgpu_device *adev)
846{
847 if (adev->dummy_page.page == NULL)
848 return;
849 pci_unmap_page(adev->pdev, adev->dummy_page.addr,
850 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
851 __free_page(adev->dummy_page.page);
852 adev->dummy_page.page = NULL;
853}
854
855
856/* ATOM accessor methods */
857/*
858 * ATOM is an interpreted byte code stored in tables in the vbios. The
859 * driver registers callbacks to access registers and the interpreter
860 * in the driver parses the tables and executes then to program specific
861 * actions (set display modes, asic init, etc.). See amdgpu_atombios.c,
862 * atombios.h, and atom.c
863 */
864
865/**
866 * cail_pll_read - read PLL register
867 *
868 * @info: atom card_info pointer
869 * @reg: PLL register offset
870 *
871 * Provides a PLL register accessor for the atom interpreter (r4xx+).
872 * Returns the value of the PLL register.
873 */
874static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
875{
876 return 0;
877}
878
879/**
880 * cail_pll_write - write PLL register
881 *
882 * @info: atom card_info pointer
883 * @reg: PLL register offset
884 * @val: value to write to the pll register
885 *
886 * Provides a PLL register accessor for the atom interpreter (r4xx+).
887 */
888static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
889{
890
891}
892
893/**
894 * cail_mc_read - read MC (Memory Controller) register
895 *
896 * @info: atom card_info pointer
897 * @reg: MC register offset
898 *
899 * Provides an MC register accessor for the atom interpreter (r4xx+).
900 * Returns the value of the MC register.
901 */
902static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
903{
904 return 0;
905}
906
907/**
908 * cail_mc_write - write MC (Memory Controller) register
909 *
910 * @info: atom card_info pointer
911 * @reg: MC register offset
912 * @val: value to write to the pll register
913 *
914 * Provides a MC register accessor for the atom interpreter (r4xx+).
915 */
916static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
917{
918
919}
920
921/**
922 * cail_reg_write - write MMIO register
923 *
924 * @info: atom card_info pointer
925 * @reg: MMIO register offset
926 * @val: value to write to the pll register
927 *
928 * Provides a MMIO register accessor for the atom interpreter (r4xx+).
929 */
930static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
931{
932 struct amdgpu_device *adev = info->dev->dev_private;
933
934 WREG32(reg, val);
935}
936
937/**
938 * cail_reg_read - read MMIO register
939 *
940 * @info: atom card_info pointer
941 * @reg: MMIO register offset
942 *
943 * Provides an MMIO register accessor for the atom interpreter (r4xx+).
944 * Returns the value of the MMIO register.
945 */
946static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
947{
948 struct amdgpu_device *adev = info->dev->dev_private;
949 uint32_t r;
950
951 r = RREG32(reg);
952 return r;
953}
954
955/**
956 * cail_ioreg_write - write IO register
957 *
958 * @info: atom card_info pointer
959 * @reg: IO register offset
960 * @val: value to write to the pll register
961 *
962 * Provides a IO register accessor for the atom interpreter (r4xx+).
963 */
964static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
965{
966 struct amdgpu_device *adev = info->dev->dev_private;
967
968 WREG32_IO(reg, val);
969}
970
971/**
972 * cail_ioreg_read - read IO register
973 *
974 * @info: atom card_info pointer
975 * @reg: IO register offset
976 *
977 * Provides an IO register accessor for the atom interpreter (r4xx+).
978 * Returns the value of the IO register.
979 */
980static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
981{
982 struct amdgpu_device *adev = info->dev->dev_private;
983 uint32_t r;
984
985 r = RREG32_IO(reg);
986 return r;
987}
988
Kent Russell5b41d942017-08-22 12:31:43 -0400989static ssize_t amdgpu_atombios_get_vbios_version(struct device *dev,
990 struct device_attribute *attr,
991 char *buf)
992{
993 struct drm_device *ddev = dev_get_drvdata(dev);
994 struct amdgpu_device *adev = ddev->dev_private;
995 struct atom_context *ctx = adev->mode_info.atom_context;
996
997 return snprintf(buf, PAGE_SIZE, "%s\n", ctx->vbios_version);
998}
999
1000static DEVICE_ATTR(vbios_version, 0444, amdgpu_atombios_get_vbios_version,
1001 NULL);
1002
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001003/**
1004 * amdgpu_atombios_fini - free the driver info and callbacks for atombios
1005 *
1006 * @adev: amdgpu_device pointer
1007 *
1008 * Frees the driver info and register access callbacks for the ATOM
1009 * interpreter (r4xx+).
1010 * Called at driver shutdown.
1011 */
1012static void amdgpu_atombios_fini(struct amdgpu_device *adev)
1013{
Monk Liu89e0ec92016-05-27 19:34:11 +08001014 if (adev->mode_info.atom_context) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001015 kfree(adev->mode_info.atom_context->scratch);
Monk Liu89e0ec92016-05-27 19:34:11 +08001016 kfree(adev->mode_info.atom_context->iio);
1017 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001018 kfree(adev->mode_info.atom_context);
1019 adev->mode_info.atom_context = NULL;
1020 kfree(adev->mode_info.atom_card_info);
1021 adev->mode_info.atom_card_info = NULL;
Kent Russell5b41d942017-08-22 12:31:43 -04001022 device_remove_file(adev->dev, &dev_attr_vbios_version);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001023}
1024
1025/**
1026 * amdgpu_atombios_init - init the driver info and callbacks for atombios
1027 *
1028 * @adev: amdgpu_device pointer
1029 *
1030 * Initializes the driver info and register access callbacks for the
1031 * ATOM interpreter (r4xx+).
1032 * Returns 0 on sucess, -ENOMEM on failure.
1033 * Called at driver startup.
1034 */
1035static int amdgpu_atombios_init(struct amdgpu_device *adev)
1036{
1037 struct card_info *atom_card_info =
1038 kzalloc(sizeof(struct card_info), GFP_KERNEL);
Kent Russell5b41d942017-08-22 12:31:43 -04001039 int ret;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001040
1041 if (!atom_card_info)
1042 return -ENOMEM;
1043
1044 adev->mode_info.atom_card_info = atom_card_info;
1045 atom_card_info->dev = adev->ddev;
1046 atom_card_info->reg_read = cail_reg_read;
1047 atom_card_info->reg_write = cail_reg_write;
1048 /* needed for iio ops */
1049 if (adev->rio_mem) {
1050 atom_card_info->ioreg_read = cail_ioreg_read;
1051 atom_card_info->ioreg_write = cail_ioreg_write;
1052 } else {
pding9953b722017-10-26 09:30:38 +08001053 DRM_DEBUG("PCI I/O BAR is not found. Using MMIO to access ATOM BIOS\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001054 atom_card_info->ioreg_read = cail_reg_read;
1055 atom_card_info->ioreg_write = cail_reg_write;
1056 }
1057 atom_card_info->mc_read = cail_mc_read;
1058 atom_card_info->mc_write = cail_mc_write;
1059 atom_card_info->pll_read = cail_pll_read;
1060 atom_card_info->pll_write = cail_pll_write;
1061
1062 adev->mode_info.atom_context = amdgpu_atom_parse(atom_card_info, adev->bios);
1063 if (!adev->mode_info.atom_context) {
1064 amdgpu_atombios_fini(adev);
1065 return -ENOMEM;
1066 }
1067
1068 mutex_init(&adev->mode_info.atom_context->mutex);
Alex Deuchera5bde2f2016-09-23 16:23:41 -04001069 if (adev->is_atom_fw) {
1070 amdgpu_atomfirmware_scratch_regs_init(adev);
1071 amdgpu_atomfirmware_allocate_fb_scratch(adev);
1072 } else {
1073 amdgpu_atombios_scratch_regs_init(adev);
1074 amdgpu_atombios_allocate_fb_scratch(adev);
1075 }
Kent Russell5b41d942017-08-22 12:31:43 -04001076
1077 ret = device_create_file(adev->dev, &dev_attr_vbios_version);
1078 if (ret) {
1079 DRM_ERROR("Failed to create device file for VBIOS version\n");
1080 return ret;
1081 }
1082
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001083 return 0;
1084}
1085
1086/* if we get transitioned to only one device, take VGA back */
1087/**
1088 * amdgpu_vga_set_decode - enable/disable vga decode
1089 *
1090 * @cookie: amdgpu_device pointer
1091 * @state: enable/disable vga decode
1092 *
1093 * Enable/disable vga decode (all asics).
1094 * Returns VGA resource flags.
1095 */
1096static unsigned int amdgpu_vga_set_decode(void *cookie, bool state)
1097{
1098 struct amdgpu_device *adev = cookie;
1099 amdgpu_asic_set_vga_state(adev, state);
1100 if (state)
1101 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
1102 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1103 else
1104 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1105}
1106
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001107static void amdgpu_check_block_size(struct amdgpu_device *adev)
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001108{
1109 /* defines number of bits in page table versus page directory,
1110 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
1111 * page table and the remaining bits are in the page directory */
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001112 if (amdgpu_vm_block_size == -1)
1113 return;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001114
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001115 if (amdgpu_vm_block_size < 9) {
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001116 dev_warn(adev->dev, "VM page table size (%d) too small\n",
1117 amdgpu_vm_block_size);
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001118 goto def_value;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001119 }
1120
1121 if (amdgpu_vm_block_size > 24 ||
1122 (amdgpu_vm_size * 1024) < (1ull << amdgpu_vm_block_size)) {
1123 dev_warn(adev->dev, "VM page table size (%d) too large\n",
1124 amdgpu_vm_block_size);
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001125 goto def_value;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001126 }
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001127
1128 return;
1129
1130def_value:
1131 amdgpu_vm_block_size = -1;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001132}
1133
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001134static void amdgpu_check_vm_size(struct amdgpu_device *adev)
1135{
Alex Deucher64dab072017-06-15 18:20:09 -04001136 /* no need to check the default value */
1137 if (amdgpu_vm_size == -1)
1138 return;
1139
Alex Deucher76117502017-06-21 12:31:41 -04001140 if (!is_power_of_2(amdgpu_vm_size)) {
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001141 dev_warn(adev->dev, "VM size (%d) must be a power of 2\n",
1142 amdgpu_vm_size);
1143 goto def_value;
1144 }
1145
1146 if (amdgpu_vm_size < 1) {
1147 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
1148 amdgpu_vm_size);
1149 goto def_value;
1150 }
1151
1152 /*
1153 * Max GPUVM size for Cayman, SI, CI VI are 40 bits.
1154 */
1155 if (amdgpu_vm_size > 1024) {
1156 dev_warn(adev->dev, "VM size (%d) too large, max is 1TB\n",
1157 amdgpu_vm_size);
1158 goto def_value;
1159 }
1160
1161 return;
1162
1163def_value:
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001164 amdgpu_vm_size = -1;
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001165}
1166
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001167/**
1168 * amdgpu_check_arguments - validate module params
1169 *
1170 * @adev: amdgpu_device pointer
1171 *
1172 * Validates certain module parameters and updates
1173 * the associated values used by the driver (all asics).
1174 */
1175static void amdgpu_check_arguments(struct amdgpu_device *adev)
1176{
Chunming Zhou5b011232015-12-10 17:34:33 +08001177 if (amdgpu_sched_jobs < 4) {
1178 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
1179 amdgpu_sched_jobs);
1180 amdgpu_sched_jobs = 4;
Alex Deucher76117502017-06-21 12:31:41 -04001181 } else if (!is_power_of_2(amdgpu_sched_jobs)){
Chunming Zhou5b011232015-12-10 17:34:33 +08001182 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
1183 amdgpu_sched_jobs);
1184 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
1185 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001186
Alex Deucher83e74db2017-08-21 11:58:25 -04001187 if (amdgpu_gart_size != -1 && amdgpu_gart_size < 32) {
Christian Königf9321cc2017-07-07 13:44:05 +02001188 /* gart size must be greater or equal to 32M */
1189 dev_warn(adev->dev, "gart size (%d) too small\n",
1190 amdgpu_gart_size);
Alex Deucher83e74db2017-08-21 11:58:25 -04001191 amdgpu_gart_size = -1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001192 }
1193
Christian König36d38372017-07-07 13:17:45 +02001194 if (amdgpu_gtt_size != -1 && amdgpu_gtt_size < 32) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001195 /* gtt size must be greater or equal to 32M */
Christian König36d38372017-07-07 13:17:45 +02001196 dev_warn(adev->dev, "gtt size (%d) too small\n",
1197 amdgpu_gtt_size);
1198 amdgpu_gtt_size = -1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001199 }
1200
Roger Hed07f14b2017-08-15 16:05:59 +08001201 /* valid range is between 4 and 9 inclusive */
1202 if (amdgpu_vm_fragment_size != -1 &&
1203 (amdgpu_vm_fragment_size > 9 || amdgpu_vm_fragment_size < 4)) {
1204 dev_warn(adev->dev, "valid range is between 4 and 9\n");
1205 amdgpu_vm_fragment_size = -1;
1206 }
1207
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001208 amdgpu_check_vm_size(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001209
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001210 amdgpu_check_block_size(adev);
Christian König6a7f76e2016-08-24 15:51:49 +02001211
jimqu526bae32016-11-07 09:53:10 +08001212 if (amdgpu_vram_page_split != -1 && (amdgpu_vram_page_split < 16 ||
Alex Deucher76117502017-06-21 12:31:41 -04001213 !is_power_of_2(amdgpu_vram_page_split))) {
Christian König6a7f76e2016-08-24 15:51:49 +02001214 dev_warn(adev->dev, "invalid VRAM page split (%d)\n",
1215 amdgpu_vram_page_split);
1216 amdgpu_vram_page_split = 1024;
1217 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001218}
1219
1220/**
1221 * amdgpu_switcheroo_set_state - set switcheroo state
1222 *
1223 * @pdev: pci dev pointer
Lukas Wunner16944672015-09-05 11:17:35 +02001224 * @state: vga_switcheroo state
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001225 *
1226 * Callback for the switcheroo driver. Suspends or resumes the
1227 * the asics before or after it is powered up using ACPI methods.
1228 */
1229static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1230{
1231 struct drm_device *dev = pci_get_drvdata(pdev);
1232
1233 if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
1234 return;
1235
1236 if (state == VGA_SWITCHEROO_ON) {
Joe Perches7ca85292017-02-28 04:55:52 -08001237 pr_info("amdgpu: switched on\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001238 /* don't suspend or resume card normally */
1239 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1240
Alex Deucher810ddc32016-08-23 13:25:49 -04001241 amdgpu_device_resume(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001242
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001243 dev->switch_power_state = DRM_SWITCH_POWER_ON;
1244 drm_kms_helper_poll_enable(dev);
1245 } else {
Joe Perches7ca85292017-02-28 04:55:52 -08001246 pr_info("amdgpu: switched off\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001247 drm_kms_helper_poll_disable(dev);
1248 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Alex Deucher810ddc32016-08-23 13:25:49 -04001249 amdgpu_device_suspend(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001250 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1251 }
1252}
1253
1254/**
1255 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1256 *
1257 * @pdev: pci dev pointer
1258 *
1259 * Callback for the switcheroo driver. Check of the switcheroo
1260 * state can be changed.
1261 * Returns true if the state can be changed, false if not.
1262 */
1263static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1264{
1265 struct drm_device *dev = pci_get_drvdata(pdev);
1266
1267 /*
1268 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1269 * locking inversion with the driver load path. And the access here is
1270 * completely racy anyway. So don't bother with locking for now.
1271 */
1272 return dev->open_count == 0;
1273}
1274
1275static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1276 .set_gpu_state = amdgpu_switcheroo_set_state,
1277 .reprobe = NULL,
1278 .can_switch = amdgpu_switcheroo_can_switch,
1279};
1280
1281int amdgpu_set_clockgating_state(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001282 enum amd_ip_block_type block_type,
1283 enum amd_clockgating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001284{
1285 int i, r = 0;
1286
1287 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001288 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001289 continue;
Rex Zhuc7228652017-02-22 15:33:46 +08001290 if (adev->ip_blocks[i].version->type != block_type)
1291 continue;
1292 if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
1293 continue;
1294 r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
1295 (void *)adev, state);
1296 if (r)
1297 DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
1298 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001299 }
1300 return r;
1301}
1302
1303int amdgpu_set_powergating_state(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001304 enum amd_ip_block_type block_type,
1305 enum amd_powergating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001306{
1307 int i, r = 0;
1308
1309 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001310 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001311 continue;
Rex Zhuc7228652017-02-22 15:33:46 +08001312 if (adev->ip_blocks[i].version->type != block_type)
1313 continue;
1314 if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
1315 continue;
1316 r = adev->ip_blocks[i].version->funcs->set_powergating_state(
1317 (void *)adev, state);
1318 if (r)
1319 DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
1320 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001321 }
1322 return r;
1323}
1324
Huang Rui6cb2d4e2017-01-05 18:44:41 +08001325void amdgpu_get_clockgating_state(struct amdgpu_device *adev, u32 *flags)
1326{
1327 int i;
1328
1329 for (i = 0; i < adev->num_ip_blocks; i++) {
1330 if (!adev->ip_blocks[i].status.valid)
1331 continue;
1332 if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
1333 adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
1334 }
1335}
1336
Alex Deucher5dbbb602016-06-23 11:41:04 -04001337int amdgpu_wait_for_idle(struct amdgpu_device *adev,
1338 enum amd_ip_block_type block_type)
1339{
1340 int i, r;
1341
1342 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001343 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001344 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001345 if (adev->ip_blocks[i].version->type == block_type) {
1346 r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -04001347 if (r)
1348 return r;
1349 break;
1350 }
1351 }
1352 return 0;
1353
1354}
1355
1356bool amdgpu_is_idle(struct amdgpu_device *adev,
1357 enum amd_ip_block_type block_type)
1358{
1359 int i;
1360
1361 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001362 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001363 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001364 if (adev->ip_blocks[i].version->type == block_type)
1365 return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -04001366 }
1367 return true;
1368
1369}
1370
Alex Deuchera1255102016-10-13 17:41:13 -04001371struct amdgpu_ip_block * amdgpu_get_ip_block(struct amdgpu_device *adev,
1372 enum amd_ip_block_type type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001373{
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].version->type == type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001378 return &adev->ip_blocks[i];
1379
1380 return NULL;
1381}
1382
1383/**
1384 * amdgpu_ip_block_version_cmp
1385 *
1386 * @adev: amdgpu_device pointer
yanyang15fc3aee2015-05-22 14:39:35 -04001387 * @type: enum amd_ip_block_type
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001388 * @major: major version
1389 * @minor: minor version
1390 *
1391 * return 0 if equal or greater
1392 * return 1 if smaller or the ip_block doesn't exist
1393 */
1394int amdgpu_ip_block_version_cmp(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001395 enum amd_ip_block_type type,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001396 u32 major, u32 minor)
1397{
Alex Deuchera1255102016-10-13 17:41:13 -04001398 struct amdgpu_ip_block *ip_block = amdgpu_get_ip_block(adev, type);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001399
Alex Deuchera1255102016-10-13 17:41:13 -04001400 if (ip_block && ((ip_block->version->major > major) ||
1401 ((ip_block->version->major == major) &&
1402 (ip_block->version->minor >= minor))))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001403 return 0;
1404
1405 return 1;
1406}
1407
Alex Deuchera1255102016-10-13 17:41:13 -04001408/**
1409 * amdgpu_ip_block_add
1410 *
1411 * @adev: amdgpu_device pointer
1412 * @ip_block_version: pointer to the IP to add
1413 *
1414 * Adds the IP block driver information to the collection of IPs
1415 * on the asic.
1416 */
1417int amdgpu_ip_block_add(struct amdgpu_device *adev,
1418 const struct amdgpu_ip_block_version *ip_block_version)
1419{
1420 if (!ip_block_version)
1421 return -EINVAL;
1422
Huang Ruia0bae352017-05-03 09:52:06 +08001423 DRM_DEBUG("add ip block number %d <%s>\n", adev->num_ip_blocks,
1424 ip_block_version->funcs->name);
1425
Alex Deuchera1255102016-10-13 17:41:13 -04001426 adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
1427
1428 return 0;
1429}
1430
Alex Deucher483ef982016-09-30 12:43:04 -04001431static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
Emily Deng9accf2f2016-08-10 16:01:25 +08001432{
1433 adev->enable_virtual_display = false;
1434
1435 if (amdgpu_virtual_display) {
1436 struct drm_device *ddev = adev->ddev;
1437 const char *pci_address_name = pci_name(ddev->pdev);
Emily Deng0f663562016-09-30 13:02:18 -04001438 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
Emily Deng9accf2f2016-08-10 16:01:25 +08001439
1440 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1441 pciaddstr_tmp = pciaddstr;
Emily Deng0f663562016-09-30 13:02:18 -04001442 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1443 pciaddname = strsep(&pciaddname_tmp, ",");
Yintian Tao967de2a2017-01-22 15:16:51 +08001444 if (!strcmp("all", pciaddname)
1445 || !strcmp(pci_address_name, pciaddname)) {
Emily Deng0f663562016-09-30 13:02:18 -04001446 long num_crtc;
1447 int res = -1;
1448
Emily Deng9accf2f2016-08-10 16:01:25 +08001449 adev->enable_virtual_display = true;
Emily Deng0f663562016-09-30 13:02:18 -04001450
1451 if (pciaddname_tmp)
1452 res = kstrtol(pciaddname_tmp, 10,
1453 &num_crtc);
1454
1455 if (!res) {
1456 if (num_crtc < 1)
1457 num_crtc = 1;
1458 if (num_crtc > 6)
1459 num_crtc = 6;
1460 adev->mode_info.num_crtc = num_crtc;
1461 } else {
1462 adev->mode_info.num_crtc = 1;
1463 }
Emily Deng9accf2f2016-08-10 16:01:25 +08001464 break;
1465 }
1466 }
1467
Emily Deng0f663562016-09-30 13:02:18 -04001468 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
1469 amdgpu_virtual_display, pci_address_name,
1470 adev->enable_virtual_display, adev->mode_info.num_crtc);
Emily Deng9accf2f2016-08-10 16:01:25 +08001471
1472 kfree(pciaddstr);
1473 }
1474}
1475
Alex Deuchere2a75f82017-04-27 16:58:01 -04001476static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev)
1477{
Alex Deuchere2a75f82017-04-27 16:58:01 -04001478 const char *chip_name;
1479 char fw_name[30];
1480 int err;
1481 const struct gpu_info_firmware_header_v1_0 *hdr;
1482
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001483 adev->firmware.gpu_info_fw = NULL;
1484
Alex Deuchere2a75f82017-04-27 16:58:01 -04001485 switch (adev->asic_type) {
1486 case CHIP_TOPAZ:
1487 case CHIP_TONGA:
1488 case CHIP_FIJI:
1489 case CHIP_POLARIS11:
1490 case CHIP_POLARIS10:
1491 case CHIP_POLARIS12:
1492 case CHIP_CARRIZO:
1493 case CHIP_STONEY:
1494#ifdef CONFIG_DRM_AMDGPU_SI
1495 case CHIP_VERDE:
1496 case CHIP_TAHITI:
1497 case CHIP_PITCAIRN:
1498 case CHIP_OLAND:
1499 case CHIP_HAINAN:
1500#endif
1501#ifdef CONFIG_DRM_AMDGPU_CIK
1502 case CHIP_BONAIRE:
1503 case CHIP_HAWAII:
1504 case CHIP_KAVERI:
1505 case CHIP_KABINI:
1506 case CHIP_MULLINS:
1507#endif
1508 default:
1509 return 0;
1510 case CHIP_VEGA10:
1511 chip_name = "vega10";
1512 break;
Alex Deucher2d2e5e72017-05-09 12:27:35 -04001513 case CHIP_RAVEN:
1514 chip_name = "raven";
1515 break;
Alex Deuchere2a75f82017-04-27 16:58:01 -04001516 }
1517
1518 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name);
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001519 err = request_firmware(&adev->firmware.gpu_info_fw, fw_name, adev->dev);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001520 if (err) {
1521 dev_err(adev->dev,
1522 "Failed to load gpu_info firmware \"%s\"\n",
1523 fw_name);
1524 goto out;
1525 }
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001526 err = amdgpu_ucode_validate(adev->firmware.gpu_info_fw);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001527 if (err) {
1528 dev_err(adev->dev,
1529 "Failed to validate gpu_info firmware \"%s\"\n",
1530 fw_name);
1531 goto out;
1532 }
1533
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001534 hdr = (const struct gpu_info_firmware_header_v1_0 *)adev->firmware.gpu_info_fw->data;
Alex Deuchere2a75f82017-04-27 16:58:01 -04001535 amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
1536
1537 switch (hdr->version_major) {
1538 case 1:
1539 {
1540 const struct gpu_info_firmware_v1_0 *gpu_info_fw =
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001541 (const struct gpu_info_firmware_v1_0 *)(adev->firmware.gpu_info_fw->data +
Alex Deuchere2a75f82017-04-27 16:58:01 -04001542 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1543
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001544 adev->gfx.config.max_shader_engines = le32_to_cpu(gpu_info_fw->gc_num_se);
1545 adev->gfx.config.max_cu_per_sh = le32_to_cpu(gpu_info_fw->gc_num_cu_per_sh);
1546 adev->gfx.config.max_sh_per_se = le32_to_cpu(gpu_info_fw->gc_num_sh_per_se);
1547 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 -04001548 adev->gfx.config.max_texture_channel_caches =
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001549 le32_to_cpu(gpu_info_fw->gc_num_tccs);
1550 adev->gfx.config.max_gprs = le32_to_cpu(gpu_info_fw->gc_num_gprs);
1551 adev->gfx.config.max_gs_threads = le32_to_cpu(gpu_info_fw->gc_num_max_gs_thds);
1552 adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gpu_info_fw->gc_gs_table_depth);
1553 adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gpu_info_fw->gc_gsprim_buff_depth);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001554 adev->gfx.config.double_offchip_lds_buf =
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001555 le32_to_cpu(gpu_info_fw->gc_double_offchip_lds_buffer);
1556 adev->gfx.cu_info.wave_front_size = le32_to_cpu(gpu_info_fw->gc_wave_size);
Hawking Zhang51fd0372017-06-09 22:30:52 +08001557 adev->gfx.cu_info.max_waves_per_simd =
1558 le32_to_cpu(gpu_info_fw->gc_max_waves_per_simd);
1559 adev->gfx.cu_info.max_scratch_slots_per_cu =
1560 le32_to_cpu(gpu_info_fw->gc_max_scratch_slots_per_cu);
1561 adev->gfx.cu_info.lds_size = le32_to_cpu(gpu_info_fw->gc_lds_size);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001562 break;
1563 }
1564 default:
1565 dev_err(adev->dev,
1566 "Unsupported gpu_info table %d\n", hdr->header.ucode_version);
1567 err = -EINVAL;
1568 goto out;
1569 }
1570out:
Alex Deuchere2a75f82017-04-27 16:58:01 -04001571 return err;
1572}
1573
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001574static int amdgpu_early_init(struct amdgpu_device *adev)
1575{
Alex Deucheraaa36a92015-04-20 17:31:14 -04001576 int i, r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001577
Alex Deucher483ef982016-09-30 12:43:04 -04001578 amdgpu_device_enable_virtual_display(adev);
Emily Denga6be7572016-08-08 11:37:50 +08001579
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001580 switch (adev->asic_type) {
Alex Deucheraaa36a92015-04-20 17:31:14 -04001581 case CHIP_TOPAZ:
1582 case CHIP_TONGA:
David Zhang48299f92015-07-08 01:05:16 +08001583 case CHIP_FIJI:
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001584 case CHIP_POLARIS11:
1585 case CHIP_POLARIS10:
Junwei Zhangc4642a42016-12-14 15:32:28 -05001586 case CHIP_POLARIS12:
Alex Deucheraaa36a92015-04-20 17:31:14 -04001587 case CHIP_CARRIZO:
Samuel Li39bb0c92015-10-08 16:31:43 -04001588 case CHIP_STONEY:
1589 if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
Alex Deucheraaa36a92015-04-20 17:31:14 -04001590 adev->family = AMDGPU_FAMILY_CZ;
1591 else
1592 adev->family = AMDGPU_FAMILY_VI;
1593
1594 r = vi_set_ip_blocks(adev);
1595 if (r)
1596 return r;
1597 break;
Ken Wang33f34802016-01-21 17:29:41 +08001598#ifdef CONFIG_DRM_AMDGPU_SI
1599 case CHIP_VERDE:
1600 case CHIP_TAHITI:
1601 case CHIP_PITCAIRN:
1602 case CHIP_OLAND:
1603 case CHIP_HAINAN:
Ken Wang295d0da2016-05-24 21:02:53 +08001604 adev->family = AMDGPU_FAMILY_SI;
Ken Wang33f34802016-01-21 17:29:41 +08001605 r = si_set_ip_blocks(adev);
1606 if (r)
1607 return r;
1608 break;
1609#endif
Alex Deuchera2e73f52015-04-20 17:09:27 -04001610#ifdef CONFIG_DRM_AMDGPU_CIK
1611 case CHIP_BONAIRE:
1612 case CHIP_HAWAII:
1613 case CHIP_KAVERI:
1614 case CHIP_KABINI:
1615 case CHIP_MULLINS:
1616 if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
1617 adev->family = AMDGPU_FAMILY_CI;
1618 else
1619 adev->family = AMDGPU_FAMILY_KV;
1620
1621 r = cik_set_ip_blocks(adev);
1622 if (r)
1623 return r;
1624 break;
1625#endif
Chunming Zhou2ca8a5d2016-12-07 17:31:19 +08001626 case CHIP_VEGA10:
1627 case CHIP_RAVEN:
1628 if (adev->asic_type == CHIP_RAVEN)
1629 adev->family = AMDGPU_FAMILY_RV;
1630 else
1631 adev->family = AMDGPU_FAMILY_AI;
Ken Wang460826e2017-03-06 14:53:16 -05001632
1633 r = soc15_set_ip_blocks(adev);
1634 if (r)
1635 return r;
1636 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001637 default:
1638 /* FIXME: not supported yet */
1639 return -EINVAL;
1640 }
1641
Alex Deuchere2a75f82017-04-27 16:58:01 -04001642 r = amdgpu_device_parse_gpu_info_fw(adev);
1643 if (r)
1644 return r;
1645
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001646 if (amdgpu_sriov_vf(adev)) {
1647 r = amdgpu_virt_request_full_gpu(adev, true);
1648 if (r)
pding5ffa61c2017-10-30 14:07:24 +08001649 return -EAGAIN;
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001650 }
1651
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001652 for (i = 0; i < adev->num_ip_blocks; i++) {
1653 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
Huang Ruied8cf002017-05-03 09:40:17 +08001654 DRM_ERROR("disabled ip block: %d <%s>\n",
1655 i, adev->ip_blocks[i].version->funcs->name);
Alex Deuchera1255102016-10-13 17:41:13 -04001656 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001657 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001658 if (adev->ip_blocks[i].version->funcs->early_init) {
1659 r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001660 if (r == -ENOENT) {
Alex Deuchera1255102016-10-13 17:41:13 -04001661 adev->ip_blocks[i].status.valid = false;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001662 } else if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001663 DRM_ERROR("early_init of IP block <%s> failed %d\n",
1664 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001665 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001666 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001667 adev->ip_blocks[i].status.valid = true;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001668 }
Alex Deucher974e6b62015-07-10 13:59:44 -04001669 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001670 adev->ip_blocks[i].status.valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001671 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001672 }
1673 }
1674
Nicolai Hähnle395d1fb2016-06-02 12:32:07 +02001675 adev->cg_flags &= amdgpu_cg_mask;
1676 adev->pg_flags &= amdgpu_pg_mask;
1677
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001678 return 0;
1679}
1680
1681static int amdgpu_init(struct amdgpu_device *adev)
1682{
1683 int i, r;
1684
1685 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001686 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001687 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001688 r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001689 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001690 DRM_ERROR("sw_init of IP block <%s> failed %d\n",
1691 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001692 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001693 }
Alex Deuchera1255102016-10-13 17:41:13 -04001694 adev->ip_blocks[i].status.sw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001695 /* need to do gmc hw init early so we can allocate gpu mem */
Alex Deuchera1255102016-10-13 17:41:13 -04001696 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001697 r = amdgpu_vram_scratch_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001698 if (r) {
1699 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001700 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001701 }
Alex Deuchera1255102016-10-13 17:41:13 -04001702 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001703 if (r) {
1704 DRM_ERROR("hw_init %d failed %d\n", i, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001705 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001706 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001707 r = amdgpu_wb_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001708 if (r) {
1709 DRM_ERROR("amdgpu_wb_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001710 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001711 }
Alex Deuchera1255102016-10-13 17:41:13 -04001712 adev->ip_blocks[i].status.hw = true;
Monk Liu24936642017-01-09 15:54:32 +08001713
1714 /* right after GMC hw init, we create CSA */
1715 if (amdgpu_sriov_vf(adev)) {
1716 r = amdgpu_allocate_static_csa(adev);
1717 if (r) {
1718 DRM_ERROR("allocate CSA failed %d\n", r);
1719 return r;
1720 }
1721 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001722 }
1723 }
1724
1725 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001726 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001727 continue;
1728 /* gmc hw init is done early */
Alex Deuchera1255102016-10-13 17:41:13 -04001729 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001730 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001731 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001732 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001733 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
1734 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001735 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001736 }
Alex Deuchera1255102016-10-13 17:41:13 -04001737 adev->ip_blocks[i].status.hw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001738 }
1739
1740 return 0;
1741}
1742
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08001743static void amdgpu_fill_reset_magic(struct amdgpu_device *adev)
1744{
1745 memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM);
1746}
1747
1748static bool amdgpu_check_vram_lost(struct amdgpu_device *adev)
1749{
1750 return !!memcmp(adev->gart.ptr, adev->reset_magic,
1751 AMDGPU_RESET_MAGIC_NUM);
1752}
1753
Shirish S2dc80b02017-05-25 10:05:25 +05301754static int amdgpu_late_set_cg_state(struct amdgpu_device *adev)
1755{
1756 int i = 0, r;
1757
1758 for (i = 0; i < adev->num_ip_blocks; i++) {
1759 if (!adev->ip_blocks[i].status.valid)
1760 continue;
1761 /* skip CG for VCE/UVD, it's handled specially */
1762 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1763 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1764 /* enable clockgating to save power */
1765 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1766 AMD_CG_STATE_GATE);
1767 if (r) {
1768 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
1769 adev->ip_blocks[i].version->funcs->name, r);
1770 return r;
1771 }
1772 }
1773 }
1774 return 0;
1775}
1776
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001777static int amdgpu_late_init(struct amdgpu_device *adev)
1778{
1779 int i = 0, r;
1780
1781 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001782 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001783 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001784 if (adev->ip_blocks[i].version->funcs->late_init) {
1785 r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001786 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001787 DRM_ERROR("late_init of IP block <%s> failed %d\n",
1788 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001789 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001790 }
Alex Deuchera1255102016-10-13 17:41:13 -04001791 adev->ip_blocks[i].status.late_initialized = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001792 }
1793 }
1794
Shirish S2dc80b02017-05-25 10:05:25 +05301795 mod_delayed_work(system_wq, &adev->late_init_work,
1796 msecs_to_jiffies(AMDGPU_RESUME_MS));
1797
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08001798 amdgpu_fill_reset_magic(adev);
1799
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001800 return 0;
1801}
1802
1803static int amdgpu_fini(struct amdgpu_device *adev)
1804{
1805 int i, r;
1806
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001807 /* need to disable SMC first */
1808 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001809 if (!adev->ip_blocks[i].status.hw)
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001810 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001811 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001812 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
Alex Deuchera1255102016-10-13 17:41:13 -04001813 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1814 AMD_CG_STATE_UNGATE);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001815 if (r) {
1816 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001817 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001818 return r;
1819 }
Alex Deuchera1255102016-10-13 17:41:13 -04001820 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001821 /* XXX handle errors */
1822 if (r) {
1823 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001824 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001825 }
Alex Deuchera1255102016-10-13 17:41:13 -04001826 adev->ip_blocks[i].status.hw = false;
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001827 break;
1828 }
1829 }
1830
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001831 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001832 if (!adev->ip_blocks[i].status.hw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001833 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001834 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001835 amdgpu_wb_fini(adev);
1836 amdgpu_vram_scratch_fini(adev);
1837 }
Rex Zhu8201a672016-11-24 21:44:44 +08001838
1839 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1840 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1841 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
1842 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1843 AMD_CG_STATE_UNGATE);
1844 if (r) {
1845 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1846 adev->ip_blocks[i].version->funcs->name, r);
1847 return r;
1848 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001849 }
Rex Zhu8201a672016-11-24 21:44:44 +08001850
Alex Deuchera1255102016-10-13 17:41:13 -04001851 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001852 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001853 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001854 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
1855 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001856 }
Rex Zhu8201a672016-11-24 21:44:44 +08001857
Alex Deuchera1255102016-10-13 17:41:13 -04001858 adev->ip_blocks[i].status.hw = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001859 }
1860
1861 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001862 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001863 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001864 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001865 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001866 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001867 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
1868 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001869 }
Alex Deuchera1255102016-10-13 17:41:13 -04001870 adev->ip_blocks[i].status.sw = false;
1871 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001872 }
1873
Monk Liua6dcfd92016-05-19 14:36:34 +08001874 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001875 if (!adev->ip_blocks[i].status.late_initialized)
Grazvydas Ignotas8a2eef12016-10-03 00:06:44 +03001876 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001877 if (adev->ip_blocks[i].version->funcs->late_fini)
1878 adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
1879 adev->ip_blocks[i].status.late_initialized = false;
Monk Liua6dcfd92016-05-19 14:36:34 +08001880 }
1881
Monk Liu030308f2017-09-15 15:34:52 +08001882 if (amdgpu_sriov_vf(adev))
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001883 amdgpu_virt_release_full_gpu(adev, false);
Monk Liu24936642017-01-09 15:54:32 +08001884
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001885 return 0;
1886}
1887
Shirish S2dc80b02017-05-25 10:05:25 +05301888static void amdgpu_late_init_func_handler(struct work_struct *work)
1889{
1890 struct amdgpu_device *adev =
1891 container_of(work, struct amdgpu_device, late_init_work.work);
1892 amdgpu_late_set_cg_state(adev);
1893}
1894
Alex Deucherfaefba92016-12-06 10:38:29 -05001895int amdgpu_suspend(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001896{
1897 int i, r;
1898
Xiangliang Yue941ea92017-01-18 12:47:55 +08001899 if (amdgpu_sriov_vf(adev))
1900 amdgpu_virt_request_full_gpu(adev, false);
1901
Flora Cuic5a93a22016-02-26 10:45:25 +08001902 /* ungate SMC block first */
1903 r = amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC,
1904 AMD_CG_STATE_UNGATE);
1905 if (r) {
1906 DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n",r);
1907 }
1908
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001909 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001910 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001911 continue;
1912 /* ungate blocks so that suspend can properly shut them down */
Flora Cuic5a93a22016-02-26 10:45:25 +08001913 if (i != AMD_IP_BLOCK_TYPE_SMC) {
Alex Deuchera1255102016-10-13 17:41:13 -04001914 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1915 AMD_CG_STATE_UNGATE);
Flora Cuic5a93a22016-02-26 10:45:25 +08001916 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001917 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1918 adev->ip_blocks[i].version->funcs->name, r);
Flora Cuic5a93a22016-02-26 10:45:25 +08001919 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001920 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001921 /* XXX handle errors */
Alex Deuchera1255102016-10-13 17:41:13 -04001922 r = adev->ip_blocks[i].version->funcs->suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001923 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001924 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001925 DRM_ERROR("suspend of IP block <%s> failed %d\n",
1926 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001927 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001928 }
1929
Xiangliang Yue941ea92017-01-18 12:47:55 +08001930 if (amdgpu_sriov_vf(adev))
1931 amdgpu_virt_release_full_gpu(adev, false);
1932
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001933 return 0;
1934}
1935
Monk Liue4f0fdc2017-02-09 11:55:49 +08001936static int amdgpu_sriov_reinit_early(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08001937{
1938 int i, r;
1939
Monk Liu2cb681b2017-04-26 12:00:49 +08001940 static enum amd_ip_block_type ip_order[] = {
1941 AMD_IP_BLOCK_TYPE_GMC,
1942 AMD_IP_BLOCK_TYPE_COMMON,
Monk Liu2cb681b2017-04-26 12:00:49 +08001943 AMD_IP_BLOCK_TYPE_IH,
1944 };
Monk Liua90ad3c2017-01-23 14:22:08 +08001945
Monk Liu2cb681b2017-04-26 12:00:49 +08001946 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1947 int j;
1948 struct amdgpu_ip_block *block;
Monk Liua90ad3c2017-01-23 14:22:08 +08001949
Monk Liu2cb681b2017-04-26 12:00:49 +08001950 for (j = 0; j < adev->num_ip_blocks; j++) {
1951 block = &adev->ip_blocks[j];
1952
1953 if (block->version->type != ip_order[i] ||
1954 !block->status.valid)
1955 continue;
1956
1957 r = block->version->funcs->hw_init(adev);
1958 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
Monk Liua90ad3c2017-01-23 14:22:08 +08001959 }
1960 }
1961
1962 return 0;
1963}
1964
Monk Liue4f0fdc2017-02-09 11:55:49 +08001965static int amdgpu_sriov_reinit_late(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08001966{
1967 int i, r;
1968
Monk Liu2cb681b2017-04-26 12:00:49 +08001969 static enum amd_ip_block_type ip_order[] = {
1970 AMD_IP_BLOCK_TYPE_SMC,
Monk Liuef4c1662017-09-22 16:23:34 +08001971 AMD_IP_BLOCK_TYPE_PSP,
Monk Liu2cb681b2017-04-26 12:00:49 +08001972 AMD_IP_BLOCK_TYPE_DCE,
1973 AMD_IP_BLOCK_TYPE_GFX,
1974 AMD_IP_BLOCK_TYPE_SDMA,
Frank Min257deb82017-06-15 20:07:36 +08001975 AMD_IP_BLOCK_TYPE_UVD,
1976 AMD_IP_BLOCK_TYPE_VCE
Monk Liu2cb681b2017-04-26 12:00:49 +08001977 };
Monk Liua90ad3c2017-01-23 14:22:08 +08001978
Monk Liu2cb681b2017-04-26 12:00:49 +08001979 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1980 int j;
1981 struct amdgpu_ip_block *block;
Monk Liua90ad3c2017-01-23 14:22:08 +08001982
Monk Liu2cb681b2017-04-26 12:00:49 +08001983 for (j = 0; j < adev->num_ip_blocks; j++) {
1984 block = &adev->ip_blocks[j];
1985
1986 if (block->version->type != ip_order[i] ||
1987 !block->status.valid)
1988 continue;
1989
1990 r = block->version->funcs->hw_init(adev);
1991 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
Monk Liua90ad3c2017-01-23 14:22:08 +08001992 }
1993 }
1994
1995 return 0;
1996}
1997
Chunming Zhoufcf06492017-05-05 10:33:33 +08001998static int amdgpu_resume_phase1(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001999{
2000 int i, r;
2001
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002002 for (i = 0; i < adev->num_ip_blocks; i++) {
2003 if (!adev->ip_blocks[i].status.valid)
2004 continue;
Chunming Zhoufcf06492017-05-05 10:33:33 +08002005 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
2006 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
2007 adev->ip_blocks[i].version->type ==
2008 AMD_IP_BLOCK_TYPE_IH) {
2009 r = adev->ip_blocks[i].version->funcs->resume(adev);
2010 if (r) {
2011 DRM_ERROR("resume of IP block <%s> failed %d\n",
2012 adev->ip_blocks[i].version->funcs->name, r);
2013 return r;
2014 }
2015 }
2016 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002017
Chunming Zhoufcf06492017-05-05 10:33:33 +08002018 return 0;
2019}
2020
2021static int amdgpu_resume_phase2(struct amdgpu_device *adev)
2022{
2023 int i, r;
2024
2025 for (i = 0; i < adev->num_ip_blocks; i++) {
2026 if (!adev->ip_blocks[i].status.valid)
2027 continue;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002028 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
2029 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
2030 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH )
2031 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002032 r = adev->ip_blocks[i].version->funcs->resume(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002033 if (r) {
2034 DRM_ERROR("resume of IP block <%s> failed %d\n",
2035 adev->ip_blocks[i].version->funcs->name, r);
2036 return r;
2037 }
2038 }
2039
2040 return 0;
2041}
2042
2043static int amdgpu_resume(struct amdgpu_device *adev)
2044{
Chunming Zhoufcf06492017-05-05 10:33:33 +08002045 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002046
Chunming Zhoufcf06492017-05-05 10:33:33 +08002047 r = amdgpu_resume_phase1(adev);
2048 if (r)
2049 return r;
2050 r = amdgpu_resume_phase2(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002051
Chunming Zhoufcf06492017-05-05 10:33:33 +08002052 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002053}
2054
Monk Liu4e99a442016-03-31 13:26:59 +08002055static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
Andres Rodriguez048765a2016-06-11 02:51:32 -04002056{
Monk Liu6867e1b2017-10-16 19:50:44 +08002057 if (amdgpu_sriov_vf(adev)) {
2058 if (adev->is_atom_fw) {
2059 if (amdgpu_atomfirmware_gpu_supports_virtualization(adev))
2060 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
2061 } else {
2062 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
2063 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
2064 }
2065
2066 if (!(adev->virt.caps & AMDGPU_SRIOV_CAPS_SRIOV_VBIOS))
2067 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_NO_VBIOS, 0, 0);
Alex Deuchera5bde2f2016-09-23 16:23:41 -04002068 }
Andres Rodriguez048765a2016-06-11 02:51:32 -04002069}
2070
Harry Wentland45622362017-09-12 15:58:20 -04002071bool amdgpu_device_asic_has_dc_support(enum amd_asic_type asic_type)
2072{
2073 switch (asic_type) {
2074#if defined(CONFIG_DRM_AMD_DC)
2075 case CHIP_BONAIRE:
2076 case CHIP_HAWAII:
Alex Deucher0d6fbcc2017-08-10 14:39:48 -04002077 case CHIP_KAVERI:
Harry Wentland45622362017-09-12 15:58:20 -04002078 case CHIP_CARRIZO:
2079 case CHIP_STONEY:
2080 case CHIP_POLARIS11:
2081 case CHIP_POLARIS10:
Alex Deucher2c8ad2d2017-06-15 16:20:24 -04002082 case CHIP_POLARIS12:
Harry Wentland45622362017-09-12 15:58:20 -04002083 case CHIP_TONGA:
2084 case CHIP_FIJI:
2085#if defined(CONFIG_DRM_AMD_DC_PRE_VEGA)
2086 return amdgpu_dc != 0;
Harry Wentland45622362017-09-12 15:58:20 -04002087#endif
Alex Deucher17b7cf82017-08-23 09:42:22 -04002088 case CHIP_KABINI:
2089 case CHIP_MULLINS:
2090 return amdgpu_dc > 0;
Harry Wentland42f8ffa2017-09-15 14:07:30 -04002091 case CHIP_VEGA10:
2092#if defined(CONFIG_DRM_AMD_DC_DCN1_0)
Hawking Zhangfd187852017-03-06 14:01:11 +08002093 case CHIP_RAVEN:
Harry Wentland42f8ffa2017-09-15 14:07:30 -04002094#endif
Hawking Zhangfd187852017-03-06 14:01:11 +08002095 return amdgpu_dc != 0;
2096#endif
Harry Wentland45622362017-09-12 15:58:20 -04002097 default:
2098 return false;
2099 }
2100}
2101
2102/**
2103 * amdgpu_device_has_dc_support - check if dc is supported
2104 *
2105 * @adev: amdgpu_device_pointer
2106 *
2107 * Returns true for supported, false for not supported
2108 */
2109bool amdgpu_device_has_dc_support(struct amdgpu_device *adev)
2110{
Xiangliang Yu2555039d2017-01-10 17:34:52 +08002111 if (amdgpu_sriov_vf(adev))
2112 return false;
2113
Harry Wentland45622362017-09-12 15:58:20 -04002114 return amdgpu_device_asic_has_dc_support(adev->asic_type);
2115}
2116
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002117/**
2118 * amdgpu_device_init - initialize the driver
2119 *
2120 * @adev: amdgpu_device pointer
2121 * @pdev: drm dev pointer
2122 * @pdev: pci dev pointer
2123 * @flags: driver flags
2124 *
2125 * Initializes the driver info and hw (all asics).
2126 * Returns 0 for success or an error on failure.
2127 * Called at driver startup.
2128 */
2129int amdgpu_device_init(struct amdgpu_device *adev,
2130 struct drm_device *ddev,
2131 struct pci_dev *pdev,
2132 uint32_t flags)
2133{
2134 int r, i;
2135 bool runtime = false;
Marek Olšák95844d22016-08-17 23:49:27 +02002136 u32 max_MBps;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002137
2138 adev->shutdown = false;
2139 adev->dev = &pdev->dev;
2140 adev->ddev = ddev;
2141 adev->pdev = pdev;
2142 adev->flags = flags;
Jammy Zhou2f7d10b2015-07-22 11:29:01 +08002143 adev->asic_type = flags & AMD_ASIC_MASK;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002144 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
Christian König6f02a692017-07-07 11:56:59 +02002145 adev->mc.gart_size = 512 * 1024 * 1024;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002146 adev->accel_working = false;
2147 adev->num_rings = 0;
2148 adev->mman.buffer_funcs = NULL;
2149 adev->mman.buffer_funcs_ring = NULL;
2150 adev->vm_manager.vm_pte_funcs = NULL;
Christian König2d55e452016-02-08 17:37:38 +01002151 adev->vm_manager.vm_pte_num_rings = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002152 adev->gart.gart_funcs = NULL;
Chris Wilsonf54d1862016-10-25 13:00:45 +01002153 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
Andres Rodriguezb8866c22017-04-28 20:05:51 -04002154 bitmap_zero(adev->gfx.pipe_reserve_bitmap, AMDGPU_MAX_COMPUTE_QUEUES);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002155
2156 adev->smc_rreg = &amdgpu_invalid_rreg;
2157 adev->smc_wreg = &amdgpu_invalid_wreg;
2158 adev->pcie_rreg = &amdgpu_invalid_rreg;
2159 adev->pcie_wreg = &amdgpu_invalid_wreg;
Huang Rui36b9a952016-08-31 13:23:25 +08002160 adev->pciep_rreg = &amdgpu_invalid_rreg;
2161 adev->pciep_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002162 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
2163 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
2164 adev->didt_rreg = &amdgpu_invalid_rreg;
2165 adev->didt_wreg = &amdgpu_invalid_wreg;
Rex Zhuccdbb202016-06-08 12:47:41 +08002166 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
2167 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002168 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
2169 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
2170
Alex Deucher3e39ab92015-06-05 15:04:33 -04002171 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
2172 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
2173 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002174
2175 /* mutex initialization are all done here so we
2176 * can recall function without having locking issues */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002177 atomic_set(&adev->irq.ih.lock, 0);
Huang Rui0e5ca0d2017-03-03 18:37:23 -05002178 mutex_init(&adev->firmware.mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002179 mutex_init(&adev->pm.mutex);
2180 mutex_init(&adev->gfx.gpu_clock_mutex);
2181 mutex_init(&adev->srbm_mutex);
Andres Rodriguezb8866c22017-04-28 20:05:51 -04002182 mutex_init(&adev->gfx.pipe_reserve_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002183 mutex_init(&adev->grbm_idx_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002184 mutex_init(&adev->mn_lock);
Alex Deuchere23b74a2017-09-28 09:47:32 -04002185 mutex_init(&adev->virt.vf_errors.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002186 hash_init(adev->mn_hash);
Monk Liu13a752e2017-10-17 15:11:12 +08002187 mutex_init(&adev->lock_reset);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002188
2189 amdgpu_check_arguments(adev);
2190
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002191 spin_lock_init(&adev->mmio_idx_lock);
2192 spin_lock_init(&adev->smc_idx_lock);
2193 spin_lock_init(&adev->pcie_idx_lock);
2194 spin_lock_init(&adev->uvd_ctx_idx_lock);
2195 spin_lock_init(&adev->didt_idx_lock);
Rex Zhuccdbb202016-06-08 12:47:41 +08002196 spin_lock_init(&adev->gc_cac_idx_lock);
Evan Quan16abb5d2017-07-04 09:21:50 +08002197 spin_lock_init(&adev->se_cac_idx_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002198 spin_lock_init(&adev->audio_endpt_idx_lock);
Marek Olšák95844d22016-08-17 23:49:27 +02002199 spin_lock_init(&adev->mm_stats.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002200
Chunming Zhou0c4e7fa2016-08-17 11:41:30 +08002201 INIT_LIST_HEAD(&adev->shadow_list);
2202 mutex_init(&adev->shadow_list_lock);
2203
Andres Rodriguez795f2812017-03-06 16:27:55 -05002204 INIT_LIST_HEAD(&adev->ring_lru_list);
2205 spin_lock_init(&adev->ring_lru_list_lock);
2206
Shirish S2dc80b02017-05-25 10:05:25 +05302207 INIT_DELAYED_WORK(&adev->late_init_work, amdgpu_late_init_func_handler);
2208
Alex Xie0fa49552017-06-08 14:58:05 -04002209 /* Registers mapping */
2210 /* TODO: block userspace mapping of io register */
Ken Wangda69c1612016-01-21 19:08:55 +08002211 if (adev->asic_type >= CHIP_BONAIRE) {
2212 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
2213 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
2214 } else {
2215 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
2216 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
2217 }
Chunming Zhou5c1354b2016-08-30 16:13:10 +08002218
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002219 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
2220 if (adev->rmmio == NULL) {
2221 return -ENOMEM;
2222 }
2223 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
2224 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
2225
Christian König705e5192017-06-08 11:15:16 +02002226 /* doorbell bar mapping */
2227 amdgpu_doorbell_init(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002228
2229 /* io port mapping */
2230 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
2231 if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
2232 adev->rio_mem_size = pci_resource_len(adev->pdev, i);
2233 adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
2234 break;
2235 }
2236 }
2237 if (adev->rio_mem == NULL)
Amber Linb64a18c2017-01-04 08:06:58 -05002238 DRM_INFO("PCI I/O BAR is not found.\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002239
2240 /* early init functions */
2241 r = amdgpu_early_init(adev);
2242 if (r)
2243 return r;
2244
2245 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
2246 /* this will fail for cards that aren't VGA class devices, just
2247 * ignore it */
2248 vga_client_register(adev->pdev, adev, NULL, amdgpu_vga_set_decode);
2249
2250 if (amdgpu_runtime_pm == 1)
2251 runtime = true;
Alex Deuchere9bef452016-04-25 13:12:18 -04002252 if (amdgpu_device_is_px(ddev))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002253 runtime = true;
Lukas Wunner84c8b222017-03-10 21:23:45 +01002254 if (!pci_is_thunderbolt_attached(adev->pdev))
2255 vga_switcheroo_register_client(adev->pdev,
2256 &amdgpu_switcheroo_ops, runtime);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002257 if (runtime)
2258 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
2259
2260 /* Read BIOS */
Alex Deucher83ba1262016-06-03 18:21:41 -04002261 if (!amdgpu_get_bios(adev)) {
2262 r = -EINVAL;
2263 goto failed;
2264 }
Nils Wallméniusf7e9e9f2016-12-14 21:52:45 +01002265
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002266 r = amdgpu_atombios_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002267 if (r) {
2268 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002269 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_INIT_FAIL, 0, 0);
Alex Deucher83ba1262016-06-03 18:21:41 -04002270 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002271 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002272
Monk Liu4e99a442016-03-31 13:26:59 +08002273 /* detect if we are with an SRIOV vbios */
2274 amdgpu_device_detect_sriov_bios(adev);
Andres Rodriguez048765a2016-06-11 02:51:32 -04002275
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002276 /* Post card if necessary */
pding91fe77e2017-10-19 09:38:39 +08002277 if (amdgpu_need_post(adev)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002278 if (!adev->bios) {
Monk Liubec86372016-09-14 19:38:08 +08002279 dev_err(adev->dev, "no vBIOS found\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04002280 r = -EINVAL;
2281 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002282 }
Monk Liubec86372016-09-14 19:38:08 +08002283 DRM_INFO("GPU posting now...\n");
Monk Liu4e99a442016-03-31 13:26:59 +08002284 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2285 if (r) {
2286 dev_err(adev->dev, "gpu post error!\n");
2287 goto failed;
2288 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002289 }
2290
Alex Deucher88b64e92017-07-10 10:43:10 -04002291 if (adev->is_atom_fw) {
2292 /* Initialize clocks */
2293 r = amdgpu_atomfirmware_get_clock_info(adev);
2294 if (r) {
2295 dev_err(adev->dev, "amdgpu_atomfirmware_get_clock_info failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002296 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
Alex Deucher88b64e92017-07-10 10:43:10 -04002297 goto failed;
2298 }
2299 } else {
Alex Deuchera5bde2f2016-09-23 16:23:41 -04002300 /* Initialize clocks */
2301 r = amdgpu_atombios_get_clock_info(adev);
2302 if (r) {
2303 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002304 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
Gavin Wan89041942017-06-23 13:55:15 -04002305 goto failed;
Alex Deuchera5bde2f2016-09-23 16:23:41 -04002306 }
2307 /* init i2c buses */
Harry Wentland45622362017-09-12 15:58:20 -04002308 if (!amdgpu_device_has_dc_support(adev))
2309 amdgpu_atombios_i2c_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002310 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002311
2312 /* Fence driver */
2313 r = amdgpu_fence_driver_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002314 if (r) {
2315 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002316 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0);
Alex Deucher83ba1262016-06-03 18:21:41 -04002317 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002318 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002319
2320 /* init the mode config */
2321 drm_mode_config_init(adev->ddev);
2322
2323 r = amdgpu_init(adev);
2324 if (r) {
pding8840a382017-10-23 17:22:09 +08002325 /* failed in exclusive mode due to timeout */
2326 if (amdgpu_sriov_vf(adev) &&
2327 !amdgpu_sriov_runtime(adev) &&
2328 amdgpu_virt_mmio_blocked(adev) &&
2329 !amdgpu_virt_wait_reset(adev)) {
2330 dev_err(adev->dev, "VF exclusive mode timeout\n");
2331 r = -EAGAIN;
2332 goto failed;
2333 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05002334 dev_err(adev->dev, "amdgpu_init failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002335 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002336 amdgpu_fini(adev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002337 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002338 }
2339
2340 adev->accel_working = true;
2341
Alex Xiee59c0202017-06-01 09:42:59 -04002342 amdgpu_vm_check_compute_bug(adev);
2343
Marek Olšák95844d22016-08-17 23:49:27 +02002344 /* Initialize the buffer migration limit. */
2345 if (amdgpu_moverate >= 0)
2346 max_MBps = amdgpu_moverate;
2347 else
2348 max_MBps = 8; /* Allow 8 MB/s. */
2349 /* Get a log2 for easy divisions. */
2350 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
2351
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002352 r = amdgpu_ib_pool_init(adev);
2353 if (r) {
2354 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
Alex Deuchere23b74a2017-09-28 09:47:32 -04002355 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r);
Alex Deucher83ba1262016-06-03 18:21:41 -04002356 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002357 }
2358
2359 r = amdgpu_ib_ring_tests(adev);
2360 if (r)
2361 DRM_ERROR("ib ring test failed (%d).\n", r);
2362
Horace Chen2dc8f812017-10-09 16:17:16 +08002363 if (amdgpu_sriov_vf(adev))
2364 amdgpu_virt_init_data_exchange(adev);
2365
Monk Liu9bc92b92017-02-08 17:38:13 +08002366 amdgpu_fbdev_init(adev);
2367
Rex Zhud2f52ac2017-09-22 17:47:27 +08002368 r = amdgpu_pm_sysfs_init(adev);
2369 if (r)
2370 DRM_ERROR("registering pm debugfs failed (%d).\n", r);
2371
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002372 r = amdgpu_gem_debugfs_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002373 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002374 DRM_ERROR("registering gem debugfs failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002375
2376 r = amdgpu_debugfs_regs_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002377 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002378 DRM_ERROR("registering register debugfs failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002379
Huang Rui4f0955f2017-05-10 23:04:06 +08002380 r = amdgpu_debugfs_test_ib_ring_init(adev);
2381 if (r)
2382 DRM_ERROR("registering register test ib ring debugfs failed (%d).\n", r);
2383
Huang Rui50ab2532016-06-12 15:51:09 +08002384 r = amdgpu_debugfs_firmware_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002385 if (r)
Huang Rui50ab2532016-06-12 15:51:09 +08002386 DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
Huang Rui50ab2532016-06-12 15:51:09 +08002387
Kent Russelldb95e212017-08-22 12:31:43 -04002388 r = amdgpu_debugfs_vbios_dump_init(adev);
2389 if (r)
2390 DRM_ERROR("Creating vbios dump debugfs failed (%d).\n", r);
2391
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002392 if ((amdgpu_testing & 1)) {
2393 if (adev->accel_working)
2394 amdgpu_test_moves(adev);
2395 else
2396 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
2397 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002398 if (amdgpu_benchmarking) {
2399 if (adev->accel_working)
2400 amdgpu_benchmark(adev, amdgpu_benchmarking);
2401 else
2402 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
2403 }
2404
2405 /* enable clockgating, etc. after ib tests, etc. since some blocks require
2406 * explicit gating rather than handling it automatically.
2407 */
2408 r = amdgpu_late_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002409 if (r) {
2410 dev_err(adev->dev, "amdgpu_late_init failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002411 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r);
Alex Deucher83ba1262016-06-03 18:21:41 -04002412 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002413 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002414
2415 return 0;
Alex Deucher83ba1262016-06-03 18:21:41 -04002416
2417failed:
Gavin Wan89041942017-06-23 13:55:15 -04002418 amdgpu_vf_error_trans_all(adev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002419 if (runtime)
2420 vga_switcheroo_fini_domain_pm_ops(adev->dev);
pding8840a382017-10-23 17:22:09 +08002421
Alex Deucher83ba1262016-06-03 18:21:41 -04002422 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002423}
2424
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002425/**
2426 * amdgpu_device_fini - tear down the driver
2427 *
2428 * @adev: amdgpu_device pointer
2429 *
2430 * Tear down the driver info (all asics).
2431 * Called at driver shutdown.
2432 */
2433void amdgpu_device_fini(struct amdgpu_device *adev)
2434{
2435 int r;
2436
2437 DRM_INFO("amdgpu: finishing device.\n");
2438 adev->shutdown = true;
Pixel Dingdb2c2a92017-04-25 16:47:42 +08002439 if (adev->mode_info.mode_config_initialized)
2440 drm_crtc_force_disable_all(adev->ddev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002441 /* evict vram memory */
2442 amdgpu_bo_evict_vram(adev);
2443 amdgpu_ib_pool_fini(adev);
Horace Chena05502e2017-09-29 14:41:57 +08002444 amdgpu_fw_reserve_vram_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002445 amdgpu_fence_driver_fini(adev);
2446 amdgpu_fbdev_fini(adev);
2447 r = amdgpu_fini(adev);
Huang Ruiab4fe3e2017-06-05 22:11:59 +08002448 if (adev->firmware.gpu_info_fw) {
2449 release_firmware(adev->firmware.gpu_info_fw);
2450 adev->firmware.gpu_info_fw = NULL;
2451 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002452 adev->accel_working = false;
Shirish S2dc80b02017-05-25 10:05:25 +05302453 cancel_delayed_work_sync(&adev->late_init_work);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002454 /* free i2c buses */
Harry Wentland45622362017-09-12 15:58:20 -04002455 if (!amdgpu_device_has_dc_support(adev))
2456 amdgpu_i2c_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002457 amdgpu_atombios_fini(adev);
2458 kfree(adev->bios);
2459 adev->bios = NULL;
Lukas Wunner84c8b222017-03-10 21:23:45 +01002460 if (!pci_is_thunderbolt_attached(adev->pdev))
2461 vga_switcheroo_unregister_client(adev->pdev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002462 if (adev->flags & AMD_IS_PX)
2463 vga_switcheroo_fini_domain_pm_ops(adev->dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002464 vga_client_register(adev->pdev, NULL, NULL, NULL);
2465 if (adev->rio_mem)
2466 pci_iounmap(adev->pdev, adev->rio_mem);
2467 adev->rio_mem = NULL;
2468 iounmap(adev->rmmio);
2469 adev->rmmio = NULL;
Christian König705e5192017-06-08 11:15:16 +02002470 amdgpu_doorbell_fini(adev);
Rex Zhud2f52ac2017-09-22 17:47:27 +08002471 amdgpu_pm_sysfs_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002472 amdgpu_debugfs_regs_cleanup(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002473}
2474
2475
2476/*
2477 * Suspend & resume.
2478 */
2479/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002480 * amdgpu_device_suspend - initiate device suspend
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002481 *
2482 * @pdev: drm dev pointer
2483 * @state: suspend state
2484 *
2485 * Puts the hw in the suspend state (all asics).
2486 * Returns 0 for success or an error on failure.
2487 * Called at driver suspend.
2488 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002489int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002490{
2491 struct amdgpu_device *adev;
2492 struct drm_crtc *crtc;
2493 struct drm_connector *connector;
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002494 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002495
2496 if (dev == NULL || dev->dev_private == NULL) {
2497 return -ENODEV;
2498 }
2499
2500 adev = dev->dev_private;
2501
2502 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2503 return 0;
2504
2505 drm_kms_helper_poll_disable(dev);
2506
Harry Wentland45622362017-09-12 15:58:20 -04002507 if (!amdgpu_device_has_dc_support(adev)) {
2508 /* turn off display hw */
2509 drm_modeset_lock_all(dev);
2510 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2511 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
2512 }
2513 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002514 }
2515
Yong Zhaoba997702015-11-09 17:21:45 -05002516 amdgpu_amdkfd_suspend(adev);
2517
Alex Deucher756e6882015-10-08 00:03:36 -04002518 /* unpin the front buffers and cursors */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002519 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Alex Deucher756e6882015-10-08 00:03:36 -04002520 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002521 struct amdgpu_framebuffer *rfb = to_amdgpu_framebuffer(crtc->primary->fb);
2522 struct amdgpu_bo *robj;
2523
Alex Deucher756e6882015-10-08 00:03:36 -04002524 if (amdgpu_crtc->cursor_bo) {
2525 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
Alex Xie7a6901d2017-04-24 13:52:41 -04002526 r = amdgpu_bo_reserve(aobj, true);
Alex Deucher756e6882015-10-08 00:03:36 -04002527 if (r == 0) {
2528 amdgpu_bo_unpin(aobj);
2529 amdgpu_bo_unreserve(aobj);
2530 }
2531 }
2532
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002533 if (rfb == NULL || rfb->obj == NULL) {
2534 continue;
2535 }
2536 robj = gem_to_amdgpu_bo(rfb->obj);
2537 /* don't unpin kernel fb objects */
2538 if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
Alex Xie7a6901d2017-04-24 13:52:41 -04002539 r = amdgpu_bo_reserve(robj, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002540 if (r == 0) {
2541 amdgpu_bo_unpin(robj);
2542 amdgpu_bo_unreserve(robj);
2543 }
2544 }
2545 }
2546 /* evict vram memory */
2547 amdgpu_bo_evict_vram(adev);
2548
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002549 amdgpu_fence_driver_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002550
2551 r = amdgpu_suspend(adev);
2552
Alex Deuchera0a71e42016-10-10 12:41:36 -04002553 /* evict remaining vram memory
2554 * This second call to evict vram is to evict the gart page table
2555 * using the CPU.
2556 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002557 amdgpu_bo_evict_vram(adev);
2558
Alex Deucherd05da0e2017-06-30 17:08:45 -04002559 amdgpu_atombios_scratch_regs_save(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002560 pci_save_state(dev->pdev);
2561 if (suspend) {
2562 /* Shut down the device */
2563 pci_disable_device(dev->pdev);
2564 pci_set_power_state(dev->pdev, PCI_D3hot);
jimqu74b0b152016-09-07 17:09:12 +08002565 } else {
2566 r = amdgpu_asic_reset(adev);
2567 if (r)
2568 DRM_ERROR("amdgpu asic reset failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002569 }
2570
2571 if (fbcon) {
2572 console_lock();
2573 amdgpu_fbdev_set_suspend(adev, 1);
2574 console_unlock();
2575 }
2576 return 0;
2577}
2578
2579/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002580 * amdgpu_device_resume - initiate device resume
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002581 *
2582 * @pdev: drm dev pointer
2583 *
2584 * Bring the hw back to operating state (all asics).
2585 * Returns 0 for success or an error on failure.
2586 * Called at driver resume.
2587 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002588int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002589{
2590 struct drm_connector *connector;
2591 struct amdgpu_device *adev = dev->dev_private;
Alex Deucher756e6882015-10-08 00:03:36 -04002592 struct drm_crtc *crtc;
Huang Rui03161a62017-04-13 16:12:26 +08002593 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002594
2595 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2596 return 0;
2597
jimqu74b0b152016-09-07 17:09:12 +08002598 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002599 console_lock();
jimqu74b0b152016-09-07 17:09:12 +08002600
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002601 if (resume) {
2602 pci_set_power_state(dev->pdev, PCI_D0);
2603 pci_restore_state(dev->pdev);
jimqu74b0b152016-09-07 17:09:12 +08002604 r = pci_enable_device(dev->pdev);
Huang Rui03161a62017-04-13 16:12:26 +08002605 if (r)
2606 goto unlock;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002607 }
Alex Deucherd05da0e2017-06-30 17:08:45 -04002608 amdgpu_atombios_scratch_regs_restore(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002609
2610 /* post card */
Jim Quc836fec2017-02-10 15:59:59 +08002611 if (amdgpu_need_post(adev)) {
jimqu74b0b152016-09-07 17:09:12 +08002612 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2613 if (r)
2614 DRM_ERROR("amdgpu asic init failed\n");
2615 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002616
2617 r = amdgpu_resume(adev);
Rex Zhue6707212017-03-30 13:21:01 +08002618 if (r) {
Flora Cuica198522016-02-04 15:10:08 +08002619 DRM_ERROR("amdgpu_resume failed (%d).\n", r);
Huang Rui03161a62017-04-13 16:12:26 +08002620 goto unlock;
Rex Zhue6707212017-03-30 13:21:01 +08002621 }
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002622 amdgpu_fence_driver_resume(adev);
2623
Flora Cuica198522016-02-04 15:10:08 +08002624 if (resume) {
2625 r = amdgpu_ib_ring_tests(adev);
2626 if (r)
2627 DRM_ERROR("ib ring test failed (%d).\n", r);
2628 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002629
2630 r = amdgpu_late_init(adev);
Huang Rui03161a62017-04-13 16:12:26 +08002631 if (r)
2632 goto unlock;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002633
Alex Deucher756e6882015-10-08 00:03:36 -04002634 /* pin cursors */
2635 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2636 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2637
2638 if (amdgpu_crtc->cursor_bo) {
2639 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
Alex Xie7a6901d2017-04-24 13:52:41 -04002640 r = amdgpu_bo_reserve(aobj, true);
Alex Deucher756e6882015-10-08 00:03:36 -04002641 if (r == 0) {
2642 r = amdgpu_bo_pin(aobj,
2643 AMDGPU_GEM_DOMAIN_VRAM,
2644 &amdgpu_crtc->cursor_addr);
2645 if (r != 0)
2646 DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
2647 amdgpu_bo_unreserve(aobj);
2648 }
2649 }
2650 }
Yong Zhaoba997702015-11-09 17:21:45 -05002651 r = amdgpu_amdkfd_resume(adev);
2652 if (r)
2653 return r;
Alex Deucher756e6882015-10-08 00:03:36 -04002654
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002655 /* blat the mode back in */
2656 if (fbcon) {
Harry Wentland45622362017-09-12 15:58:20 -04002657 if (!amdgpu_device_has_dc_support(adev)) {
2658 /* pre DCE11 */
2659 drm_helper_resume_force_mode(dev);
2660
2661 /* turn on display hw */
2662 drm_modeset_lock_all(dev);
2663 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2664 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
2665 }
2666 drm_modeset_unlock_all(dev);
2667 } else {
2668 /*
2669 * There is no equivalent atomic helper to turn on
2670 * display, so we defined our own function for this,
2671 * once suspend resume is supported by the atomic
2672 * framework this will be reworked
2673 */
2674 amdgpu_dm_display_resume(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002675 }
2676 }
2677
2678 drm_kms_helper_poll_enable(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002679
2680 /*
2681 * Most of the connector probing functions try to acquire runtime pm
2682 * refs to ensure that the GPU is powered on when connector polling is
2683 * performed. Since we're calling this from a runtime PM callback,
2684 * trying to acquire rpm refs will cause us to deadlock.
2685 *
2686 * Since we're guaranteed to be holding the rpm lock, it's safe to
2687 * temporarily disable the rpm helpers so this doesn't deadlock us.
2688 */
2689#ifdef CONFIG_PM
2690 dev->dev->power.disable_depth++;
2691#endif
Harry Wentland45622362017-09-12 15:58:20 -04002692 if (!amdgpu_device_has_dc_support(adev))
2693 drm_helper_hpd_irq_event(dev);
2694 else
2695 drm_kms_helper_hotplug_event(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002696#ifdef CONFIG_PM
2697 dev->dev->power.disable_depth--;
2698#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002699
Huang Rui03161a62017-04-13 16:12:26 +08002700 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002701 amdgpu_fbdev_set_suspend(adev, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002702
Huang Rui03161a62017-04-13 16:12:26 +08002703unlock:
2704 if (fbcon)
2705 console_unlock();
2706
2707 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002708}
2709
Chunming Zhou63fbf422016-07-15 11:19:20 +08002710static bool amdgpu_check_soft_reset(struct amdgpu_device *adev)
2711{
2712 int i;
2713 bool asic_hang = false;
2714
Monk Liuf993d622017-10-16 19:46:01 +08002715 if (amdgpu_sriov_vf(adev))
2716 return true;
2717
Chunming Zhou63fbf422016-07-15 11:19:20 +08002718 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002719 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou63fbf422016-07-15 11:19:20 +08002720 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002721 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
2722 adev->ip_blocks[i].status.hang =
2723 adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
2724 if (adev->ip_blocks[i].status.hang) {
2725 DRM_INFO("IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
Chunming Zhou63fbf422016-07-15 11:19:20 +08002726 asic_hang = true;
2727 }
2728 }
2729 return asic_hang;
2730}
2731
Baoyou Xie4d446652016-09-18 22:09:35 +08002732static int amdgpu_pre_soft_reset(struct amdgpu_device *adev)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002733{
2734 int i, r = 0;
2735
2736 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002737 if (!adev->ip_blocks[i].status.valid)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002738 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002739 if (adev->ip_blocks[i].status.hang &&
2740 adev->ip_blocks[i].version->funcs->pre_soft_reset) {
2741 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
Chunming Zhoud31a5012016-07-18 10:04:34 +08002742 if (r)
2743 return r;
2744 }
2745 }
2746
2747 return 0;
2748}
2749
Chunming Zhou35d782f2016-07-15 15:57:13 +08002750static bool amdgpu_need_full_reset(struct amdgpu_device *adev)
2751{
Alex Deucherda146d32016-10-13 16:07:03 -04002752 int i;
2753
2754 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002755 if (!adev->ip_blocks[i].status.valid)
Alex Deucherda146d32016-10-13 16:07:03 -04002756 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002757 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
2758 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
2759 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
Ken Wang98512bb2017-09-14 16:25:19 +08002760 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) ||
2761 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
Alex Deuchera1255102016-10-13 17:41:13 -04002762 if (adev->ip_blocks[i].status.hang) {
Alex Deucherda146d32016-10-13 16:07:03 -04002763 DRM_INFO("Some block need full reset!\n");
2764 return true;
2765 }
2766 }
Chunming Zhou35d782f2016-07-15 15:57:13 +08002767 }
2768 return false;
2769}
2770
2771static int amdgpu_soft_reset(struct amdgpu_device *adev)
2772{
2773 int i, r = 0;
2774
2775 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002776 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002777 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002778 if (adev->ip_blocks[i].status.hang &&
2779 adev->ip_blocks[i].version->funcs->soft_reset) {
2780 r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002781 if (r)
2782 return r;
2783 }
2784 }
2785
2786 return 0;
2787}
2788
2789static int amdgpu_post_soft_reset(struct amdgpu_device *adev)
2790{
2791 int i, r = 0;
2792
2793 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002794 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002795 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002796 if (adev->ip_blocks[i].status.hang &&
2797 adev->ip_blocks[i].version->funcs->post_soft_reset)
2798 r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002799 if (r)
2800 return r;
2801 }
2802
2803 return 0;
2804}
2805
Chunming Zhou3ad81f12016-08-05 17:30:17 +08002806bool amdgpu_need_backup(struct amdgpu_device *adev)
2807{
2808 if (adev->flags & AMD_IS_APU)
2809 return false;
2810
2811 return amdgpu_lockup_timeout > 0 ? true : false;
2812}
2813
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002814static int amdgpu_recover_vram_from_shadow(struct amdgpu_device *adev,
2815 struct amdgpu_ring *ring,
2816 struct amdgpu_bo *bo,
Chris Wilsonf54d1862016-10-25 13:00:45 +01002817 struct dma_fence **fence)
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002818{
2819 uint32_t domain;
2820 int r;
2821
Roger.He23d2e502017-04-21 14:24:26 +08002822 if (!bo->shadow)
2823 return 0;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002824
Alex Xie1d284792017-04-24 13:53:04 -04002825 r = amdgpu_bo_reserve(bo, true);
Roger.He23d2e502017-04-21 14:24:26 +08002826 if (r)
2827 return r;
2828 domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
2829 /* if bo has been evicted, then no need to recover */
2830 if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
Roger.He82521312017-04-21 13:08:43 +08002831 r = amdgpu_bo_validate(bo->shadow);
2832 if (r) {
2833 DRM_ERROR("bo validate failed!\n");
2834 goto err;
2835 }
2836
Roger.He23d2e502017-04-21 14:24:26 +08002837 r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002838 NULL, fence, true);
Roger.He23d2e502017-04-21 14:24:26 +08002839 if (r) {
2840 DRM_ERROR("recover page table failed!\n");
2841 goto err;
2842 }
2843 }
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002844err:
Roger.He23d2e502017-04-21 14:24:26 +08002845 amdgpu_bo_unreserve(bo);
2846 return r;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002847}
2848
Monk Liu57406822017-10-25 16:37:02 +08002849/*
2850 * amdgpu_reset - reset ASIC/GPU for bare-metal or passthrough
Monk Liua90ad3c2017-01-23 14:22:08 +08002851 *
2852 * @adev: amdgpu device pointer
Monk Liu57406822017-10-25 16:37:02 +08002853 * @reset_flags: output param tells caller the reset result
Monk Liua90ad3c2017-01-23 14:22:08 +08002854 *
Monk Liu57406822017-10-25 16:37:02 +08002855 * attempt to do soft-reset or full-reset and reinitialize Asic
2856 * return 0 means successed otherwise failed
2857*/
2858static int amdgpu_reset(struct amdgpu_device *adev, uint64_t* reset_flags)
Monk Liua90ad3c2017-01-23 14:22:08 +08002859{
Monk Liu57406822017-10-25 16:37:02 +08002860 bool need_full_reset, vram_lost = 0;
2861 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002862
Chunming Zhou35d782f2016-07-15 15:57:13 +08002863 need_full_reset = amdgpu_need_full_reset(adev);
2864
2865 if (!need_full_reset) {
2866 amdgpu_pre_soft_reset(adev);
2867 r = amdgpu_soft_reset(adev);
2868 amdgpu_post_soft_reset(adev);
2869 if (r || amdgpu_check_soft_reset(adev)) {
2870 DRM_INFO("soft reset failed, will fallback to full reset!\n");
2871 need_full_reset = true;
2872 }
Monk Liu57406822017-10-25 16:37:02 +08002873
Chunming Zhou35d782f2016-07-15 15:57:13 +08002874 }
2875
2876 if (need_full_reset) {
Chunming Zhou35d782f2016-07-15 15:57:13 +08002877 r = amdgpu_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002878
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002879retry:
Alex Deucherd05da0e2017-06-30 17:08:45 -04002880 amdgpu_atombios_scratch_regs_save(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002881 r = amdgpu_asic_reset(adev);
Alex Deucherd05da0e2017-06-30 17:08:45 -04002882 amdgpu_atombios_scratch_regs_restore(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002883 /* post card */
2884 amdgpu_atom_asic_init(adev->mode_info.atom_context);
Alex Deucherbfa99262016-01-15 11:59:48 -05002885
Chunming Zhou35d782f2016-07-15 15:57:13 +08002886 if (!r) {
2887 dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
Chunming Zhoufcf06492017-05-05 10:33:33 +08002888 r = amdgpu_resume_phase1(adev);
2889 if (r)
2890 goto out;
Monk Liu57406822017-10-25 16:37:02 +08002891
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002892 vram_lost = amdgpu_check_vram_lost(adev);
Chunming Zhouf1892132017-05-15 16:48:27 +08002893 if (vram_lost) {
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002894 DRM_ERROR("VRAM is lost!\n");
Chunming Zhouf1892132017-05-15 16:48:27 +08002895 atomic_inc(&adev->vram_lost_counter);
2896 }
Monk Liu57406822017-10-25 16:37:02 +08002897
Christian Königc1c7ce82017-10-16 16:50:32 +02002898 r = amdgpu_gtt_mgr_recover(
2899 &adev->mman.bdev.man[TTM_PL_TT]);
Chunming Zhou2c0d7312016-08-30 16:36:25 +08002900 if (r)
Chunming Zhoufcf06492017-05-05 10:33:33 +08002901 goto out;
Monk Liu57406822017-10-25 16:37:02 +08002902
Chunming Zhoufcf06492017-05-05 10:33:33 +08002903 r = amdgpu_resume_phase2(adev);
2904 if (r)
2905 goto out;
Monk Liu57406822017-10-25 16:37:02 +08002906
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002907 if (vram_lost)
2908 amdgpu_fill_reset_magic(adev);
Chunming Zhou2c0d7312016-08-30 16:36:25 +08002909 }
Chunming Zhoufcf06492017-05-05 10:33:33 +08002910 }
Monk Liu57406822017-10-25 16:37:02 +08002911
Chunming Zhoufcf06492017-05-05 10:33:33 +08002912out:
2913 if (!r) {
2914 amdgpu_irq_gpu_reset_resume_helper(adev);
Chunming Zhou1f465082016-06-30 15:02:26 +08002915 r = amdgpu_ib_ring_tests(adev);
2916 if (r) {
2917 dev_err(adev->dev, "ib ring test failed (%d).\n", r);
Chunming Zhou40019dc2016-06-29 16:01:49 +08002918 r = amdgpu_suspend(adev);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002919 need_full_reset = true;
Chunming Zhou40019dc2016-06-29 16:01:49 +08002920 goto retry;
Chunming Zhou1f465082016-06-30 15:02:26 +08002921 }
Monk Liu57406822017-10-25 16:37:02 +08002922 }
2923
2924 if (reset_flags) {
2925 if (vram_lost)
2926 (*reset_flags) |= AMDGPU_RESET_INFO_VRAM_LOST;
2927
2928 if (need_full_reset)
2929 (*reset_flags) |= AMDGPU_RESET_INFO_FULLRESET;
2930 }
2931
2932 return r;
2933}
2934
2935/*
2936 * amdgpu_reset_sriov - reset ASIC for SR-IOV vf
2937 *
2938 * @adev: amdgpu device pointer
2939 * @reset_flags: output param tells caller the reset result
2940 *
2941 * do VF FLR and reinitialize Asic
2942 * return 0 means successed otherwise failed
2943*/
2944static int amdgpu_reset_sriov(struct amdgpu_device *adev, uint64_t *reset_flags, bool from_hypervisor)
2945{
2946 int r;
2947
2948 if (from_hypervisor)
2949 r = amdgpu_virt_request_full_gpu(adev, true);
2950 else
2951 r = amdgpu_virt_reset_gpu(adev);
2952 if (r)
2953 return r;
2954
2955 /* Resume IP prior to SMC */
2956 r = amdgpu_sriov_reinit_early(adev);
2957 if (r)
2958 goto error;
2959
2960 /* we need recover gart prior to run SMC/CP/SDMA resume */
Christian Königc1c7ce82017-10-16 16:50:32 +02002961 amdgpu_gtt_mgr_recover(&adev->mman.bdev.man[TTM_PL_TT]);
Monk Liu57406822017-10-25 16:37:02 +08002962
2963 /* now we are okay to resume SMC/CP/SDMA */
2964 r = amdgpu_sriov_reinit_late(adev);
2965 if (r)
2966 goto error;
2967
2968 amdgpu_irq_gpu_reset_resume_helper(adev);
2969 r = amdgpu_ib_ring_tests(adev);
2970 if (r)
2971 dev_err(adev->dev, "[GPU_RESET] ib ring test failed (%d).\n", r);
2972
2973error:
2974 /* release full control of GPU after ib test */
2975 amdgpu_virt_release_full_gpu(adev, true);
2976
2977 if (reset_flags) {
2978 /* will get vram_lost from GIM in future, now all
2979 * reset request considered VRAM LOST
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002980 */
Monk Liu57406822017-10-25 16:37:02 +08002981 (*reset_flags) |= ~AMDGPU_RESET_INFO_VRAM_LOST;
2982 atomic_inc(&adev->vram_lost_counter);
2983
2984 /* VF FLR or hotlink reset is always full-reset */
2985 (*reset_flags) |= AMDGPU_RESET_INFO_FULLRESET;
2986 }
2987
2988 return r;
2989}
2990
2991/**
2992 * amdgpu_gpu_recover - reset the asic and recover scheduler
2993 *
2994 * @adev: amdgpu device pointer
2995 * @job: which job trigger hang
2996 *
2997 * Attempt to reset the GPU if it has hung (all asics).
2998 * Returns 0 for success or an error on failure.
2999 */
3000int amdgpu_gpu_recover(struct amdgpu_device *adev, struct amdgpu_job *job)
3001{
3002 struct drm_atomic_state *state = NULL;
3003 uint64_t reset_flags = 0;
3004 int i, r, resched;
3005
3006 if (!amdgpu_check_soft_reset(adev)) {
3007 DRM_INFO("No hardware hang detected. Did some blocks stall?\n");
3008 return 0;
3009 }
3010
3011 dev_info(adev->dev, "GPU reset begin!\n");
3012
Monk Liu13a752e2017-10-17 15:11:12 +08003013 mutex_lock(&adev->lock_reset);
Monk Liu57406822017-10-25 16:37:02 +08003014 atomic_inc(&adev->gpu_reset_counter);
Monk Liu13a752e2017-10-17 15:11:12 +08003015 adev->in_gpu_reset = 1;
Monk Liu57406822017-10-25 16:37:02 +08003016
3017 /* block TTM */
3018 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
3019 /* store modesetting */
3020 if (amdgpu_device_has_dc_support(adev))
3021 state = drm_atomic_helper_suspend(adev->ddev);
3022
3023 /* block scheduler */
3024 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
3025 struct amdgpu_ring *ring = adev->rings[i];
3026
3027 if (!ring || !ring->sched.thread)
3028 continue;
3029
3030 /* only focus on the ring hit timeout if &job not NULL */
3031 if (job && job->ring->idx != i)
3032 continue;
3033
3034 kthread_park(ring->sched.thread);
3035 amd_sched_hw_job_reset(&ring->sched, &job->base);
3036
3037 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
3038 amdgpu_fence_driver_force_completion(ring);
3039 }
3040
3041 if (amdgpu_sriov_vf(adev))
3042 r = amdgpu_reset_sriov(adev, &reset_flags, job ? false : true);
3043 else
3044 r = amdgpu_reset(adev, &reset_flags);
3045
3046 if (!r) {
3047 if (((reset_flags & AMDGPU_RESET_INFO_FULLRESET) && !(adev->flags & AMD_IS_APU)) ||
3048 (reset_flags & AMDGPU_RESET_INFO_VRAM_LOST)) {
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003049 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
3050 struct amdgpu_bo *bo, *tmp;
Chris Wilsonf54d1862016-10-25 13:00:45 +01003051 struct dma_fence *fence = NULL, *next = NULL;
Chunming Zhou1f465082016-06-30 15:02:26 +08003052
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003053 DRM_INFO("recover vram bo from shadow\n");
3054 mutex_lock(&adev->shadow_list_lock);
3055 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
Monk Liu236763d2017-05-01 16:15:31 +08003056 next = NULL;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003057 amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
3058 if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01003059 r = dma_fence_wait(fence, false);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003060 if (r) {
Monk Liu1d7b17b2017-01-22 18:52:56 +08003061 WARN(r, "recovery from shadow isn't completed\n");
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003062 break;
3063 }
3064 }
3065
Chris Wilsonf54d1862016-10-25 13:00:45 +01003066 dma_fence_put(fence);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003067 fence = next;
3068 }
3069 mutex_unlock(&adev->shadow_list_lock);
3070 if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01003071 r = dma_fence_wait(fence, false);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003072 if (r)
Monk Liu1d7b17b2017-01-22 18:52:56 +08003073 WARN(r, "recovery from shadow isn't completed\n");
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003074 }
Chris Wilsonf54d1862016-10-25 13:00:45 +01003075 dma_fence_put(fence);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003076 }
Monk Liu57406822017-10-25 16:37:02 +08003077
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003078 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
3079 struct amdgpu_ring *ring = adev->rings[i];
Chunming Zhou51687752017-04-24 17:09:15 +08003080
3081 if (!ring || !ring->sched.thread)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003082 continue;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003083
Monk Liu57406822017-10-25 16:37:02 +08003084 /* only focus on the ring hit timeout if &job not NULL */
3085 if (job && job->ring->idx != i)
3086 continue;
3087
Chunming Zhouaa1c8902016-06-30 13:56:02 +08003088 amd_sched_job_recovery(&ring->sched);
Chunming Zhou0875dc92016-06-12 15:41:58 +08003089 kthread_unpark(ring->sched.thread);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003090 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003091 } else {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003092 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
Monk Liu57406822017-10-25 16:37:02 +08003093 struct amdgpu_ring *ring = adev->rings[i];
3094
3095 if (!ring || !ring->sched.thread)
3096 continue;
3097
3098 /* only focus on the ring hit timeout if &job not NULL */
3099 if (job && job->ring->idx != i)
3100 continue;
3101
3102 kthread_unpark(adev->rings[i]->sched.thread);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003103 }
3104 }
3105
Harry Wentland45622362017-09-12 15:58:20 -04003106 if (amdgpu_device_has_dc_support(adev)) {
Monk Liu57406822017-10-25 16:37:02 +08003107 if (drm_atomic_helper_resume(adev->ddev, state))
3108 dev_info(adev->dev, "drm resume failed:%d\n", r);
Harry Wentland45622362017-09-12 15:58:20 -04003109 amdgpu_dm_display_resume(adev);
Monk Liu57406822017-10-25 16:37:02 +08003110 } else {
Harry Wentland45622362017-09-12 15:58:20 -04003111 drm_helper_resume_force_mode(adev->ddev);
Monk Liu57406822017-10-25 16:37:02 +08003112 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003113
3114 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
Monk Liu57406822017-10-25 16:37:02 +08003115
Gavin Wan89041942017-06-23 13:55:15 -04003116 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003117 /* bad news, how to tell it to userspace ? */
Monk Liu57406822017-10-25 16:37:02 +08003118 dev_info(adev->dev, "GPU reset(%d) failed\n", atomic_read(&adev->gpu_reset_counter));
3119 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, r);
3120 } else {
3121 dev_info(adev->dev, "GPU reset(%d) successed!\n",atomic_read(&adev->gpu_reset_counter));
Gavin Wan89041942017-06-23 13:55:15 -04003122 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003123
Gavin Wan89041942017-06-23 13:55:15 -04003124 amdgpu_vf_error_trans_all(adev);
Monk Liu13a752e2017-10-17 15:11:12 +08003125 adev->in_gpu_reset = 0;
3126 mutex_unlock(&adev->lock_reset);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003127 return r;
3128}
3129
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003130void amdgpu_get_pcie_info(struct amdgpu_device *adev)
3131{
3132 u32 mask;
3133 int ret;
3134
Alex Deuchercd474ba2016-02-04 10:21:23 -05003135 if (amdgpu_pcie_gen_cap)
3136 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
3137
3138 if (amdgpu_pcie_lane_cap)
3139 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
3140
3141 /* covers APUs as well */
3142 if (pci_is_root_bus(adev->pdev->bus)) {
3143 if (adev->pm.pcie_gen_mask == 0)
3144 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
3145 if (adev->pm.pcie_mlw_mask == 0)
3146 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003147 return;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003148 }
Alex Deuchercd474ba2016-02-04 10:21:23 -05003149
3150 if (adev->pm.pcie_gen_mask == 0) {
3151 ret = drm_pcie_get_speed_cap_mask(adev->ddev, &mask);
3152 if (!ret) {
3153 adev->pm.pcie_gen_mask = (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
3154 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
3155 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
3156
3157 if (mask & DRM_PCIE_SPEED_25)
3158 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
3159 if (mask & DRM_PCIE_SPEED_50)
3160 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2;
3161 if (mask & DRM_PCIE_SPEED_80)
3162 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3;
3163 } else {
3164 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
3165 }
3166 }
3167 if (adev->pm.pcie_mlw_mask == 0) {
3168 ret = drm_pcie_get_max_link_width(adev->ddev, &mask);
3169 if (!ret) {
3170 switch (mask) {
3171 case 32:
3172 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
3173 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
3174 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3175 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3176 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3177 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3178 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3179 break;
3180 case 16:
3181 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
3182 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3183 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3184 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3185 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3186 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3187 break;
3188 case 12:
3189 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3190 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3191 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3192 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3193 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3194 break;
3195 case 8:
3196 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3197 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3198 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3199 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3200 break;
3201 case 4:
3202 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3203 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3204 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3205 break;
3206 case 2:
3207 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3208 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3209 break;
3210 case 1:
3211 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
3212 break;
3213 default:
3214 break;
3215 }
3216 } else {
3217 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003218 }
3219 }
3220}
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003221
3222/*
3223 * Debugfs
3224 */
3225int amdgpu_debugfs_add_files(struct amdgpu_device *adev,
Nils Wallménius06ab6832016-05-02 12:46:15 -04003226 const struct drm_info_list *files,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003227 unsigned nfiles)
3228{
3229 unsigned i;
3230
3231 for (i = 0; i < adev->debugfs_count; i++) {
3232 if (adev->debugfs[i].files == files) {
3233 /* Already registered */
3234 return 0;
3235 }
3236 }
3237
3238 i = adev->debugfs_count + 1;
3239 if (i > AMDGPU_DEBUGFS_MAX_COMPONENTS) {
3240 DRM_ERROR("Reached maximum number of debugfs components.\n");
3241 DRM_ERROR("Report so we increase "
3242 "AMDGPU_DEBUGFS_MAX_COMPONENTS.\n");
3243 return -EINVAL;
3244 }
3245 adev->debugfs[adev->debugfs_count].files = files;
3246 adev->debugfs[adev->debugfs_count].num_files = nfiles;
3247 adev->debugfs_count = i;
3248#if defined(CONFIG_DEBUG_FS)
3249 drm_debugfs_create_files(files, nfiles,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003250 adev->ddev->primary->debugfs_root,
3251 adev->ddev->primary);
3252#endif
3253 return 0;
3254}
3255
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003256#if defined(CONFIG_DEBUG_FS)
3257
3258static ssize_t amdgpu_debugfs_regs_read(struct file *f, char __user *buf,
3259 size_t size, loff_t *pos)
3260{
Al Viro45063092016-12-04 18:24:56 -05003261 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003262 ssize_t result = 0;
3263 int r;
Tom St Denisbd122672016-07-28 09:39:22 -04003264 bool pm_pg_lock, use_bank;
Tom St Denis566281592016-06-27 11:55:07 -04003265 unsigned instance_bank, sh_bank, se_bank;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003266
3267 if (size & 0x3 || *pos & 0x3)
3268 return -EINVAL;
3269
Tom St Denisbd122672016-07-28 09:39:22 -04003270 /* are we reading registers for which a PG lock is necessary? */
3271 pm_pg_lock = (*pos >> 23) & 1;
3272
Tom St Denis566281592016-06-27 11:55:07 -04003273 if (*pos & (1ULL << 62)) {
Tom St Denis0b968652017-11-10 12:54:50 -05003274 se_bank = (*pos & GENMASK_ULL(33, 24)) >> 24;
3275 sh_bank = (*pos & GENMASK_ULL(43, 34)) >> 34;
3276 instance_bank = (*pos & GENMASK_ULL(53, 44)) >> 44;
Tom St Denis32977f92016-10-09 07:41:26 -04003277
3278 if (se_bank == 0x3FF)
3279 se_bank = 0xFFFFFFFF;
3280 if (sh_bank == 0x3FF)
3281 sh_bank = 0xFFFFFFFF;
3282 if (instance_bank == 0x3FF)
3283 instance_bank = 0xFFFFFFFF;
Tom St Denis566281592016-06-27 11:55:07 -04003284 use_bank = 1;
Tom St Denis566281592016-06-27 11:55:07 -04003285 } else {
3286 use_bank = 0;
3287 }
3288
Tom St Denis801a6aa9a62017-03-15 05:34:25 -04003289 *pos &= (1UL << 22) - 1;
Tom St Denisbd122672016-07-28 09:39:22 -04003290
Tom St Denis566281592016-06-27 11:55:07 -04003291 if (use_bank) {
Tom St Denis32977f92016-10-09 07:41:26 -04003292 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
3293 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
Tom St Denis566281592016-06-27 11:55:07 -04003294 return -EINVAL;
3295 mutex_lock(&adev->grbm_idx_mutex);
3296 amdgpu_gfx_select_se_sh(adev, se_bank,
3297 sh_bank, instance_bank);
3298 }
3299
Tom St Denisbd122672016-07-28 09:39:22 -04003300 if (pm_pg_lock)
3301 mutex_lock(&adev->pm.mutex);
3302
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003303 while (size) {
3304 uint32_t value;
3305
3306 if (*pos > adev->rmmio_size)
Tom St Denis566281592016-06-27 11:55:07 -04003307 goto end;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003308
3309 value = RREG32(*pos >> 2);
3310 r = put_user(value, (uint32_t *)buf);
Tom St Denis566281592016-06-27 11:55:07 -04003311 if (r) {
3312 result = r;
3313 goto end;
3314 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003315
3316 result += 4;
3317 buf += 4;
3318 *pos += 4;
3319 size -= 4;
3320 }
3321
Tom St Denis566281592016-06-27 11:55:07 -04003322end:
3323 if (use_bank) {
3324 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
3325 mutex_unlock(&adev->grbm_idx_mutex);
3326 }
3327
Tom St Denisbd122672016-07-28 09:39:22 -04003328 if (pm_pg_lock)
3329 mutex_unlock(&adev->pm.mutex);
3330
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003331 return result;
3332}
3333
3334static ssize_t amdgpu_debugfs_regs_write(struct file *f, const char __user *buf,
3335 size_t size, loff_t *pos)
3336{
Al Viro45063092016-12-04 18:24:56 -05003337 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003338 ssize_t result = 0;
3339 int r;
Tom St Denis394fdde2016-10-10 07:31:23 -04003340 bool pm_pg_lock, use_bank;
3341 unsigned instance_bank, sh_bank, se_bank;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003342
3343 if (size & 0x3 || *pos & 0x3)
3344 return -EINVAL;
3345
Tom St Denis394fdde2016-10-10 07:31:23 -04003346 /* are we reading registers for which a PG lock is necessary? */
3347 pm_pg_lock = (*pos >> 23) & 1;
3348
3349 if (*pos & (1ULL << 62)) {
Tom St Denis0b968652017-11-10 12:54:50 -05003350 se_bank = (*pos & GENMASK_ULL(33, 24)) >> 24;
3351 sh_bank = (*pos & GENMASK_ULL(43, 34)) >> 34;
3352 instance_bank = (*pos & GENMASK_ULL(53, 44)) >> 44;
Tom St Denis394fdde2016-10-10 07:31:23 -04003353
3354 if (se_bank == 0x3FF)
3355 se_bank = 0xFFFFFFFF;
3356 if (sh_bank == 0x3FF)
3357 sh_bank = 0xFFFFFFFF;
3358 if (instance_bank == 0x3FF)
3359 instance_bank = 0xFFFFFFFF;
3360 use_bank = 1;
3361 } else {
3362 use_bank = 0;
3363 }
3364
Tom St Denis801a6aa9a62017-03-15 05:34:25 -04003365 *pos &= (1UL << 22) - 1;
Tom St Denis394fdde2016-10-10 07:31:23 -04003366
3367 if (use_bank) {
3368 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
3369 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
3370 return -EINVAL;
3371 mutex_lock(&adev->grbm_idx_mutex);
3372 amdgpu_gfx_select_se_sh(adev, se_bank,
3373 sh_bank, instance_bank);
3374 }
3375
3376 if (pm_pg_lock)
3377 mutex_lock(&adev->pm.mutex);
3378
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003379 while (size) {
3380 uint32_t value;
3381
3382 if (*pos > adev->rmmio_size)
3383 return result;
3384
3385 r = get_user(value, (uint32_t *)buf);
3386 if (r)
3387 return r;
3388
3389 WREG32(*pos >> 2, value);
3390
3391 result += 4;
3392 buf += 4;
3393 *pos += 4;
3394 size -= 4;
3395 }
3396
Tom St Denis394fdde2016-10-10 07:31:23 -04003397 if (use_bank) {
3398 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
3399 mutex_unlock(&adev->grbm_idx_mutex);
3400 }
3401
3402 if (pm_pg_lock)
3403 mutex_unlock(&adev->pm.mutex);
3404
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003405 return result;
3406}
3407
Tom St Denisadcec282016-04-15 13:08:44 -04003408static ssize_t amdgpu_debugfs_regs_pcie_read(struct file *f, char __user *buf,
3409 size_t size, loff_t *pos)
3410{
Al Viro45063092016-12-04 18:24:56 -05003411 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003412 ssize_t result = 0;
3413 int r;
3414
3415 if (size & 0x3 || *pos & 0x3)
3416 return -EINVAL;
3417
3418 while (size) {
3419 uint32_t value;
3420
3421 value = RREG32_PCIE(*pos >> 2);
3422 r = put_user(value, (uint32_t *)buf);
3423 if (r)
3424 return r;
3425
3426 result += 4;
3427 buf += 4;
3428 *pos += 4;
3429 size -= 4;
3430 }
3431
3432 return result;
3433}
3434
3435static ssize_t amdgpu_debugfs_regs_pcie_write(struct file *f, const char __user *buf,
3436 size_t size, loff_t *pos)
3437{
Al Viro45063092016-12-04 18:24:56 -05003438 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003439 ssize_t result = 0;
3440 int r;
3441
3442 if (size & 0x3 || *pos & 0x3)
3443 return -EINVAL;
3444
3445 while (size) {
3446 uint32_t value;
3447
3448 r = get_user(value, (uint32_t *)buf);
3449 if (r)
3450 return r;
3451
3452 WREG32_PCIE(*pos >> 2, value);
3453
3454 result += 4;
3455 buf += 4;
3456 *pos += 4;
3457 size -= 4;
3458 }
3459
3460 return result;
3461}
3462
3463static ssize_t amdgpu_debugfs_regs_didt_read(struct file *f, char __user *buf,
3464 size_t size, loff_t *pos)
3465{
Al Viro45063092016-12-04 18:24:56 -05003466 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003467 ssize_t result = 0;
3468 int r;
3469
3470 if (size & 0x3 || *pos & 0x3)
3471 return -EINVAL;
3472
3473 while (size) {
3474 uint32_t value;
3475
3476 value = RREG32_DIDT(*pos >> 2);
3477 r = put_user(value, (uint32_t *)buf);
3478 if (r)
3479 return r;
3480
3481 result += 4;
3482 buf += 4;
3483 *pos += 4;
3484 size -= 4;
3485 }
3486
3487 return result;
3488}
3489
3490static ssize_t amdgpu_debugfs_regs_didt_write(struct file *f, const char __user *buf,
3491 size_t size, loff_t *pos)
3492{
Al Viro45063092016-12-04 18:24:56 -05003493 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003494 ssize_t result = 0;
3495 int r;
3496
3497 if (size & 0x3 || *pos & 0x3)
3498 return -EINVAL;
3499
3500 while (size) {
3501 uint32_t value;
3502
3503 r = get_user(value, (uint32_t *)buf);
3504 if (r)
3505 return r;
3506
3507 WREG32_DIDT(*pos >> 2, value);
3508
3509 result += 4;
3510 buf += 4;
3511 *pos += 4;
3512 size -= 4;
3513 }
3514
3515 return result;
3516}
3517
3518static ssize_t amdgpu_debugfs_regs_smc_read(struct file *f, char __user *buf,
3519 size_t size, loff_t *pos)
3520{
Al Viro45063092016-12-04 18:24:56 -05003521 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003522 ssize_t result = 0;
3523 int r;
3524
3525 if (size & 0x3 || *pos & 0x3)
3526 return -EINVAL;
3527
3528 while (size) {
3529 uint32_t value;
3530
Tom St Denis6fc0dea2016-08-29 08:39:29 -04003531 value = RREG32_SMC(*pos);
Tom St Denisadcec282016-04-15 13:08:44 -04003532 r = put_user(value, (uint32_t *)buf);
3533 if (r)
3534 return r;
3535
3536 result += 4;
3537 buf += 4;
3538 *pos += 4;
3539 size -= 4;
3540 }
3541
3542 return result;
3543}
3544
3545static ssize_t amdgpu_debugfs_regs_smc_write(struct file *f, const char __user *buf,
3546 size_t size, loff_t *pos)
3547{
Al Viro45063092016-12-04 18:24:56 -05003548 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003549 ssize_t result = 0;
3550 int r;
3551
3552 if (size & 0x3 || *pos & 0x3)
3553 return -EINVAL;
3554
3555 while (size) {
3556 uint32_t value;
3557
3558 r = get_user(value, (uint32_t *)buf);
3559 if (r)
3560 return r;
3561
Tom St Denis6fc0dea2016-08-29 08:39:29 -04003562 WREG32_SMC(*pos, value);
Tom St Denisadcec282016-04-15 13:08:44 -04003563
3564 result += 4;
3565 buf += 4;
3566 *pos += 4;
3567 size -= 4;
3568 }
3569
3570 return result;
3571}
3572
Tom St Denis1e051412016-06-27 09:57:18 -04003573static ssize_t amdgpu_debugfs_gca_config_read(struct file *f, char __user *buf,
3574 size_t size, loff_t *pos)
3575{
Al Viro45063092016-12-04 18:24:56 -05003576 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denis1e051412016-06-27 09:57:18 -04003577 ssize_t result = 0;
3578 int r;
3579 uint32_t *config, no_regs = 0;
3580
3581 if (size & 0x3 || *pos & 0x3)
3582 return -EINVAL;
3583
Markus Elfringecab7662016-09-18 17:00:52 +02003584 config = kmalloc_array(256, sizeof(*config), GFP_KERNEL);
Tom St Denis1e051412016-06-27 09:57:18 -04003585 if (!config)
3586 return -ENOMEM;
3587
3588 /* version, increment each time something is added */
Tom St Denis9a999352017-01-18 13:01:25 -05003589 config[no_regs++] = 3;
Tom St Denis1e051412016-06-27 09:57:18 -04003590 config[no_regs++] = adev->gfx.config.max_shader_engines;
3591 config[no_regs++] = adev->gfx.config.max_tile_pipes;
3592 config[no_regs++] = adev->gfx.config.max_cu_per_sh;
3593 config[no_regs++] = adev->gfx.config.max_sh_per_se;
3594 config[no_regs++] = adev->gfx.config.max_backends_per_se;
3595 config[no_regs++] = adev->gfx.config.max_texture_channel_caches;
3596 config[no_regs++] = adev->gfx.config.max_gprs;
3597 config[no_regs++] = adev->gfx.config.max_gs_threads;
3598 config[no_regs++] = adev->gfx.config.max_hw_contexts;
3599 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_frontend;
3600 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_backend;
3601 config[no_regs++] = adev->gfx.config.sc_hiz_tile_fifo_size;
3602 config[no_regs++] = adev->gfx.config.sc_earlyz_tile_fifo_size;
3603 config[no_regs++] = adev->gfx.config.num_tile_pipes;
3604 config[no_regs++] = adev->gfx.config.backend_enable_mask;
3605 config[no_regs++] = adev->gfx.config.mem_max_burst_length_bytes;
3606 config[no_regs++] = adev->gfx.config.mem_row_size_in_kb;
3607 config[no_regs++] = adev->gfx.config.shader_engine_tile_size;
3608 config[no_regs++] = adev->gfx.config.num_gpus;
3609 config[no_regs++] = adev->gfx.config.multi_gpu_tile_size;
3610 config[no_regs++] = adev->gfx.config.mc_arb_ramcfg;
3611 config[no_regs++] = adev->gfx.config.gb_addr_config;
3612 config[no_regs++] = adev->gfx.config.num_rbs;
3613
Tom St Denis89a8f302016-08-12 15:14:31 -04003614 /* rev==1 */
3615 config[no_regs++] = adev->rev_id;
3616 config[no_regs++] = adev->pg_flags;
3617 config[no_regs++] = adev->cg_flags;
3618
Tom St Denise9f11dc2016-08-17 12:00:51 -04003619 /* rev==2 */
3620 config[no_regs++] = adev->family;
3621 config[no_regs++] = adev->external_rev_id;
3622
Tom St Denis9a999352017-01-18 13:01:25 -05003623 /* rev==3 */
3624 config[no_regs++] = adev->pdev->device;
3625 config[no_regs++] = adev->pdev->revision;
3626 config[no_regs++] = adev->pdev->subsystem_device;
3627 config[no_regs++] = adev->pdev->subsystem_vendor;
3628
Tom St Denis1e051412016-06-27 09:57:18 -04003629 while (size && (*pos < no_regs * 4)) {
3630 uint32_t value;
3631
3632 value = config[*pos >> 2];
3633 r = put_user(value, (uint32_t *)buf);
3634 if (r) {
3635 kfree(config);
3636 return r;
3637 }
3638
3639 result += 4;
3640 buf += 4;
3641 *pos += 4;
3642 size -= 4;
3643 }
3644
3645 kfree(config);
3646 return result;
3647}
3648
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003649static ssize_t amdgpu_debugfs_sensor_read(struct file *f, char __user *buf,
3650 size_t size, loff_t *pos)
3651{
Al Viro45063092016-12-04 18:24:56 -05003652 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003653 int idx, x, outsize, r, valuesize;
3654 uint32_t values[16];
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003655
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003656 if (size & 3 || *pos & 0x3)
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003657 return -EINVAL;
3658
Samuel Pitoiset3cbc6142017-02-15 19:32:29 +01003659 if (amdgpu_dpm == 0)
3660 return -EINVAL;
3661
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003662 /* convert offset to sensor number */
3663 idx = *pos >> 2;
3664
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003665 valuesize = sizeof(values);
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003666 if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->read_sensor)
Rex Zhucd4d7462017-09-06 18:43:52 +08003667 r = amdgpu_dpm_read_sensor(adev, idx, &values[0], &valuesize);
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003668 else
3669 return -EINVAL;
3670
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003671 if (size > valuesize)
3672 return -EINVAL;
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003673
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003674 outsize = 0;
3675 x = 0;
3676 if (!r) {
3677 while (size) {
3678 r = put_user(values[x++], (int32_t *)buf);
3679 buf += 4;
3680 size -= 4;
3681 outsize += 4;
3682 }
3683 }
3684
3685 return !r ? outsize : r;
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003686}
Tom St Denis1e051412016-06-27 09:57:18 -04003687
Tom St Denis273d7aa2016-10-11 14:48:55 -04003688static ssize_t amdgpu_debugfs_wave_read(struct file *f, char __user *buf,
3689 size_t size, loff_t *pos)
3690{
3691 struct amdgpu_device *adev = f->f_inode->i_private;
3692 int r, x;
3693 ssize_t result=0;
Tom St Denis472259f2016-10-14 09:49:09 -04003694 uint32_t offset, se, sh, cu, wave, simd, data[32];
Tom St Denis273d7aa2016-10-11 14:48:55 -04003695
3696 if (size & 3 || *pos & 3)
3697 return -EINVAL;
3698
3699 /* decode offset */
Tom St Denis0b968652017-11-10 12:54:50 -05003700 offset = (*pos & GENMASK_ULL(6, 0));
3701 se = (*pos & GENMASK_ULL(14, 7)) >> 7;
3702 sh = (*pos & GENMASK_ULL(22, 15)) >> 15;
3703 cu = (*pos & GENMASK_ULL(30, 23)) >> 23;
3704 wave = (*pos & GENMASK_ULL(36, 31)) >> 31;
3705 simd = (*pos & GENMASK_ULL(44, 37)) >> 37;
Tom St Denis273d7aa2016-10-11 14:48:55 -04003706
3707 /* switch to the specific se/sh/cu */
3708 mutex_lock(&adev->grbm_idx_mutex);
3709 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
3710
3711 x = 0;
Tom St Denis472259f2016-10-14 09:49:09 -04003712 if (adev->gfx.funcs->read_wave_data)
3713 adev->gfx.funcs->read_wave_data(adev, simd, wave, data, &x);
Tom St Denis273d7aa2016-10-11 14:48:55 -04003714
3715 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
3716 mutex_unlock(&adev->grbm_idx_mutex);
3717
Tom St Denis5ecfb3b2016-10-13 12:15:03 -04003718 if (!x)
3719 return -EINVAL;
3720
Tom St Denis472259f2016-10-14 09:49:09 -04003721 while (size && (offset < x * 4)) {
Tom St Denis273d7aa2016-10-11 14:48:55 -04003722 uint32_t value;
3723
Tom St Denis472259f2016-10-14 09:49:09 -04003724 value = data[offset >> 2];
Tom St Denis273d7aa2016-10-11 14:48:55 -04003725 r = put_user(value, (uint32_t *)buf);
3726 if (r)
3727 return r;
3728
3729 result += 4;
3730 buf += 4;
Tom St Denis472259f2016-10-14 09:49:09 -04003731 offset += 4;
Tom St Denis273d7aa2016-10-11 14:48:55 -04003732 size -= 4;
3733 }
3734
3735 return result;
3736}
3737
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003738static ssize_t amdgpu_debugfs_gpr_read(struct file *f, char __user *buf,
3739 size_t size, loff_t *pos)
3740{
3741 struct amdgpu_device *adev = f->f_inode->i_private;
3742 int r;
3743 ssize_t result = 0;
3744 uint32_t offset, se, sh, cu, wave, simd, thread, bank, *data;
3745
3746 if (size & 3 || *pos & 3)
3747 return -EINVAL;
3748
3749 /* decode offset */
Tom St Denis0b968652017-11-10 12:54:50 -05003750 offset = *pos & GENMASK_ULL(11, 0);
3751 se = (*pos & GENMASK_ULL(19, 12)) >> 12;
3752 sh = (*pos & GENMASK_ULL(27, 20)) >> 20;
3753 cu = (*pos & GENMASK_ULL(35, 28)) >> 28;
3754 wave = (*pos & GENMASK_ULL(43, 36)) >> 36;
3755 simd = (*pos & GENMASK_ULL(51, 44)) >> 44;
3756 thread = (*pos & GENMASK_ULL(59, 52)) >> 52;
3757 bank = (*pos & GENMASK_ULL(61, 60)) >> 60;
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003758
3759 data = kmalloc_array(1024, sizeof(*data), GFP_KERNEL);
3760 if (!data)
3761 return -ENOMEM;
3762
3763 /* switch to the specific se/sh/cu */
3764 mutex_lock(&adev->grbm_idx_mutex);
3765 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
3766
3767 if (bank == 0) {
3768 if (adev->gfx.funcs->read_wave_vgprs)
3769 adev->gfx.funcs->read_wave_vgprs(adev, simd, wave, thread, offset, size>>2, data);
3770 } else {
3771 if (adev->gfx.funcs->read_wave_sgprs)
3772 adev->gfx.funcs->read_wave_sgprs(adev, simd, wave, offset, size>>2, data);
3773 }
3774
3775 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
3776 mutex_unlock(&adev->grbm_idx_mutex);
3777
3778 while (size) {
3779 uint32_t value;
3780
3781 value = data[offset++];
3782 r = put_user(value, (uint32_t *)buf);
3783 if (r) {
3784 result = r;
3785 goto err;
3786 }
3787
3788 result += 4;
3789 buf += 4;
3790 size -= 4;
3791 }
3792
3793err:
3794 kfree(data);
3795 return result;
3796}
3797
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003798static const struct file_operations amdgpu_debugfs_regs_fops = {
3799 .owner = THIS_MODULE,
3800 .read = amdgpu_debugfs_regs_read,
3801 .write = amdgpu_debugfs_regs_write,
3802 .llseek = default_llseek
3803};
Tom St Denisadcec282016-04-15 13:08:44 -04003804static const struct file_operations amdgpu_debugfs_regs_didt_fops = {
3805 .owner = THIS_MODULE,
3806 .read = amdgpu_debugfs_regs_didt_read,
3807 .write = amdgpu_debugfs_regs_didt_write,
3808 .llseek = default_llseek
3809};
3810static const struct file_operations amdgpu_debugfs_regs_pcie_fops = {
3811 .owner = THIS_MODULE,
3812 .read = amdgpu_debugfs_regs_pcie_read,
3813 .write = amdgpu_debugfs_regs_pcie_write,
3814 .llseek = default_llseek
3815};
3816static const struct file_operations amdgpu_debugfs_regs_smc_fops = {
3817 .owner = THIS_MODULE,
3818 .read = amdgpu_debugfs_regs_smc_read,
3819 .write = amdgpu_debugfs_regs_smc_write,
3820 .llseek = default_llseek
3821};
3822
Tom St Denis1e051412016-06-27 09:57:18 -04003823static const struct file_operations amdgpu_debugfs_gca_config_fops = {
3824 .owner = THIS_MODULE,
3825 .read = amdgpu_debugfs_gca_config_read,
3826 .llseek = default_llseek
3827};
3828
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003829static const struct file_operations amdgpu_debugfs_sensors_fops = {
3830 .owner = THIS_MODULE,
3831 .read = amdgpu_debugfs_sensor_read,
3832 .llseek = default_llseek
3833};
3834
Tom St Denis273d7aa2016-10-11 14:48:55 -04003835static const struct file_operations amdgpu_debugfs_wave_fops = {
3836 .owner = THIS_MODULE,
3837 .read = amdgpu_debugfs_wave_read,
3838 .llseek = default_llseek
3839};
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003840static const struct file_operations amdgpu_debugfs_gpr_fops = {
3841 .owner = THIS_MODULE,
3842 .read = amdgpu_debugfs_gpr_read,
3843 .llseek = default_llseek
3844};
Tom St Denis273d7aa2016-10-11 14:48:55 -04003845
Tom St Denisadcec282016-04-15 13:08:44 -04003846static const struct file_operations *debugfs_regs[] = {
3847 &amdgpu_debugfs_regs_fops,
3848 &amdgpu_debugfs_regs_didt_fops,
3849 &amdgpu_debugfs_regs_pcie_fops,
3850 &amdgpu_debugfs_regs_smc_fops,
Tom St Denis1e051412016-06-27 09:57:18 -04003851 &amdgpu_debugfs_gca_config_fops,
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003852 &amdgpu_debugfs_sensors_fops,
Tom St Denis273d7aa2016-10-11 14:48:55 -04003853 &amdgpu_debugfs_wave_fops,
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003854 &amdgpu_debugfs_gpr_fops,
Tom St Denisadcec282016-04-15 13:08:44 -04003855};
3856
3857static const char *debugfs_regs_names[] = {
3858 "amdgpu_regs",
3859 "amdgpu_regs_didt",
3860 "amdgpu_regs_pcie",
3861 "amdgpu_regs_smc",
Tom St Denis1e051412016-06-27 09:57:18 -04003862 "amdgpu_gca_config",
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003863 "amdgpu_sensors",
Tom St Denis273d7aa2016-10-11 14:48:55 -04003864 "amdgpu_wave",
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003865 "amdgpu_gpr",
Tom St Denisadcec282016-04-15 13:08:44 -04003866};
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003867
3868static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
3869{
3870 struct drm_minor *minor = adev->ddev->primary;
3871 struct dentry *ent, *root = minor->debugfs_root;
Tom St Denisadcec282016-04-15 13:08:44 -04003872 unsigned i, j;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003873
Tom St Denisadcec282016-04-15 13:08:44 -04003874 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3875 ent = debugfs_create_file(debugfs_regs_names[i],
3876 S_IFREG | S_IRUGO, root,
3877 adev, debugfs_regs[i]);
3878 if (IS_ERR(ent)) {
3879 for (j = 0; j < i; j++) {
3880 debugfs_remove(adev->debugfs_regs[i]);
3881 adev->debugfs_regs[i] = NULL;
3882 }
3883 return PTR_ERR(ent);
3884 }
3885
3886 if (!i)
3887 i_size_write(ent->d_inode, adev->rmmio_size);
3888 adev->debugfs_regs[i] = ent;
3889 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003890
3891 return 0;
3892}
3893
3894static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev)
3895{
Tom St Denisadcec282016-04-15 13:08:44 -04003896 unsigned i;
3897
3898 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3899 if (adev->debugfs_regs[i]) {
3900 debugfs_remove(adev->debugfs_regs[i]);
3901 adev->debugfs_regs[i] = NULL;
3902 }
3903 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003904}
3905
Huang Rui4f0955f2017-05-10 23:04:06 +08003906static int amdgpu_debugfs_test_ib(struct seq_file *m, void *data)
3907{
3908 struct drm_info_node *node = (struct drm_info_node *) m->private;
3909 struct drm_device *dev = node->minor->dev;
3910 struct amdgpu_device *adev = dev->dev_private;
3911 int r = 0, i;
3912
3913 /* hold on the scheduler */
3914 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
3915 struct amdgpu_ring *ring = adev->rings[i];
3916
3917 if (!ring || !ring->sched.thread)
3918 continue;
3919 kthread_park(ring->sched.thread);
3920 }
3921
3922 seq_printf(m, "run ib test:\n");
3923 r = amdgpu_ib_ring_tests(adev);
3924 if (r)
3925 seq_printf(m, "ib ring tests failed (%d).\n", r);
3926 else
3927 seq_printf(m, "ib ring tests passed.\n");
3928
3929 /* go on the scheduler */
3930 for (i = 0; i < AMDGPU_MAX_RINGS; i++) {
3931 struct amdgpu_ring *ring = adev->rings[i];
3932
3933 if (!ring || !ring->sched.thread)
3934 continue;
3935 kthread_unpark(ring->sched.thread);
3936 }
3937
3938 return 0;
3939}
3940
3941static const struct drm_info_list amdgpu_debugfs_test_ib_ring_list[] = {
3942 {"amdgpu_test_ib", &amdgpu_debugfs_test_ib}
3943};
3944
3945static int amdgpu_debugfs_test_ib_ring_init(struct amdgpu_device *adev)
3946{
3947 return amdgpu_debugfs_add_files(adev,
3948 amdgpu_debugfs_test_ib_ring_list, 1);
3949}
3950
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003951int amdgpu_debugfs_init(struct drm_minor *minor)
3952{
3953 return 0;
3954}
Kent Russelldb95e212017-08-22 12:31:43 -04003955
3956static int amdgpu_debugfs_get_vbios_dump(struct seq_file *m, void *data)
3957{
3958 struct drm_info_node *node = (struct drm_info_node *) m->private;
3959 struct drm_device *dev = node->minor->dev;
3960 struct amdgpu_device *adev = dev->dev_private;
3961
3962 seq_write(m, adev->bios, adev->bios_size);
3963 return 0;
3964}
3965
Kent Russelldb95e212017-08-22 12:31:43 -04003966static const struct drm_info_list amdgpu_vbios_dump_list[] = {
3967 {"amdgpu_vbios",
3968 amdgpu_debugfs_get_vbios_dump,
3969 0, NULL},
3970};
3971
Kent Russelldb95e212017-08-22 12:31:43 -04003972static int amdgpu_debugfs_vbios_dump_init(struct amdgpu_device *adev)
3973{
3974 return amdgpu_debugfs_add_files(adev,
3975 amdgpu_vbios_dump_list, 1);
3976}
Alexander Kuleshov7cebc722015-06-27 13:16:05 +06003977#else
Arnd Bergmann27bad5b2017-06-21 23:51:02 +02003978static int amdgpu_debugfs_test_ib_ring_init(struct amdgpu_device *adev)
Huang Rui4f0955f2017-05-10 23:04:06 +08003979{
3980 return 0;
3981}
Alexander Kuleshov7cebc722015-06-27 13:16:05 +06003982static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
3983{
3984 return 0;
3985}
Kent Russelldb95e212017-08-22 12:31:43 -04003986static int amdgpu_debugfs_vbios_dump_init(struct amdgpu_device *adev)
3987{
3988 return 0;
3989}
Alexander Kuleshov7cebc722015-06-27 13:16:05 +06003990static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev) { }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003991#endif