blob: d0a26fff53f3b06b73dc73653028c32dbaeaf681 [file] [log] [blame]
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001/*
2 * Copyright 2008 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 * Copyright 2009 Jerome Glisse.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors: Dave Airlie
25 * Alex Deucher
26 * Jerome Glisse
27 */
Chunming Zhou0875dc92016-06-12 15:41:58 +080028#include <linux/kthread.h>
Alex Deucherd38ceaf2015-04-20 16:55:21 -040029#include <linux/console.h>
30#include <linux/slab.h>
31#include <linux/debugfs.h>
32#include <drm/drmP.h>
33#include <drm/drm_crtc_helper.h>
34#include <drm/amdgpu_drm.h>
35#include <linux/vgaarb.h>
36#include <linux/vga_switcheroo.h>
37#include <linux/efi.h>
38#include "amdgpu.h"
Tom St Denisf4b373f2016-05-31 08:02:27 -040039#include "amdgpu_trace.h"
Alex Deucherd38ceaf2015-04-20 16:55:21 -040040#include "amdgpu_i2c.h"
41#include "atom.h"
42#include "amdgpu_atombios.h"
Alex Deuchera5bde2f2016-09-23 16:23:41 -040043#include "amdgpu_atomfirmware.h"
Alex Deucherd0dd7f02015-11-11 19:45:06 -050044#include "amd_pcie.h"
Ken Wang33f34802016-01-21 17:29:41 +080045#ifdef CONFIG_DRM_AMDGPU_SI
46#include "si.h"
47#endif
Alex Deuchera2e73f52015-04-20 17:09:27 -040048#ifdef CONFIG_DRM_AMDGPU_CIK
49#include "cik.h"
50#endif
Alex Deucheraaa36a92015-04-20 17:31:14 -040051#include "vi.h"
Ken Wang460826e2017-03-06 14:53:16 -050052#include "soc15.h"
Alex Deucherd38ceaf2015-04-20 16:55:21 -040053#include "bif/bif_4_1_d.h"
Emily Deng9accf2f2016-08-10 16:01:25 +080054#include <linux/pci.h>
Monk Liubec86372016-09-14 19:38:08 +080055#include <linux/firmware.h>
Alex Deucherd38ceaf2015-04-20 16:55:21 -040056
Alex Deuchere2a75f82017-04-27 16:58:01 -040057MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin");
58
Alex Deucherd38ceaf2015-04-20 16:55:21 -040059static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev);
60static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev);
61
62static const char *amdgpu_asic_name[] = {
Ken Wangda69c1612016-01-21 19:08:55 +080063 "TAHITI",
64 "PITCAIRN",
65 "VERDE",
66 "OLAND",
67 "HAINAN",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040068 "BONAIRE",
69 "KAVERI",
70 "KABINI",
71 "HAWAII",
72 "MULLINS",
73 "TOPAZ",
74 "TONGA",
David Zhang48299f92015-07-08 01:05:16 +080075 "FIJI",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040076 "CARRIZO",
Samuel Li139f4912015-10-08 14:50:27 -040077 "STONEY",
Flora Cui2cc0c0b2016-03-14 18:33:29 -040078 "POLARIS10",
79 "POLARIS11",
Junwei Zhangc4642a42016-12-14 15:32:28 -050080 "POLARIS12",
Ken Wangd4196f02016-03-09 09:28:32 +080081 "VEGA10",
Chunming Zhou2ca8a5d2016-12-07 17:31:19 +080082 "RAVEN",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040083 "LAST",
84};
85
86bool amdgpu_device_is_px(struct drm_device *dev)
87{
88 struct amdgpu_device *adev = dev->dev_private;
89
Jammy Zhou2f7d10b2015-07-22 11:29:01 +080090 if (adev->flags & AMD_IS_PX)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040091 return true;
92 return false;
93}
94
95/*
96 * MMIO register access helper functions.
97 */
98uint32_t amdgpu_mm_rreg(struct amdgpu_device *adev, uint32_t reg,
Monk Liu15d72fd2017-01-25 15:07:40 +080099 uint32_t acc_flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400100{
Tom St Denisf4b373f2016-05-31 08:02:27 -0400101 uint32_t ret;
102
Monk Liu15d72fd2017-01-25 15:07:40 +0800103 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev)) {
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800104 BUG_ON(in_interrupt());
105 return amdgpu_virt_kiq_rreg(adev, reg);
106 }
107
Monk Liu15d72fd2017-01-25 15:07:40 +0800108 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
Tom St Denisf4b373f2016-05-31 08:02:27 -0400109 ret = readl(((void __iomem *)adev->rmmio) + (reg * 4));
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400110 else {
111 unsigned long flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400112
113 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
114 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
115 ret = readl(((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
116 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400117 }
Tom St Denisf4b373f2016-05-31 08:02:27 -0400118 trace_amdgpu_mm_rreg(adev->pdev->device, reg, ret);
119 return ret;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400120}
121
122void amdgpu_mm_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v,
Monk Liu15d72fd2017-01-25 15:07:40 +0800123 uint32_t acc_flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400124{
Tom St Denisf4b373f2016-05-31 08:02:27 -0400125 trace_amdgpu_mm_wreg(adev->pdev->device, reg, v);
Monk Liu4e99a442016-03-31 13:26:59 +0800126
Monk Liu15d72fd2017-01-25 15:07:40 +0800127 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev)) {
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800128 BUG_ON(in_interrupt());
129 return amdgpu_virt_kiq_wreg(adev, reg, v);
130 }
131
Monk Liu15d72fd2017-01-25 15:07:40 +0800132 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400133 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
134 else {
135 unsigned long flags;
136
137 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
138 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
139 writel(v, ((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
140 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
141 }
142}
143
144u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 reg)
145{
146 if ((reg * 4) < adev->rio_mem_size)
147 return ioread32(adev->rio_mem + (reg * 4));
148 else {
149 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
150 return ioread32(adev->rio_mem + (mmMM_DATA * 4));
151 }
152}
153
154void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
155{
156
157 if ((reg * 4) < adev->rio_mem_size)
158 iowrite32(v, adev->rio_mem + (reg * 4));
159 else {
160 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
161 iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
162 }
163}
164
165/**
166 * amdgpu_mm_rdoorbell - read a doorbell dword
167 *
168 * @adev: amdgpu_device pointer
169 * @index: doorbell index
170 *
171 * Returns the value in the doorbell aperture at the
172 * requested doorbell index (CIK).
173 */
174u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
175{
176 if (index < adev->doorbell.num_doorbells) {
177 return readl(adev->doorbell.ptr + index);
178 } else {
179 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
180 return 0;
181 }
182}
183
184/**
185 * amdgpu_mm_wdoorbell - write a doorbell dword
186 *
187 * @adev: amdgpu_device pointer
188 * @index: doorbell index
189 * @v: value to write
190 *
191 * Writes @v to the doorbell aperture at the
192 * requested doorbell index (CIK).
193 */
194void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
195{
196 if (index < adev->doorbell.num_doorbells) {
197 writel(v, adev->doorbell.ptr + index);
198 } else {
199 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
200 }
201}
202
203/**
Ken Wang832be402016-03-18 15:23:08 +0800204 * amdgpu_mm_rdoorbell64 - read a doorbell Qword
205 *
206 * @adev: amdgpu_device pointer
207 * @index: doorbell index
208 *
209 * Returns the value in the doorbell aperture at the
210 * requested doorbell index (VEGA10+).
211 */
212u64 amdgpu_mm_rdoorbell64(struct amdgpu_device *adev, u32 index)
213{
214 if (index < adev->doorbell.num_doorbells) {
215 return atomic64_read((atomic64_t *)(adev->doorbell.ptr + index));
216 } else {
217 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
218 return 0;
219 }
220}
221
222/**
223 * amdgpu_mm_wdoorbell64 - write a doorbell Qword
224 *
225 * @adev: amdgpu_device pointer
226 * @index: doorbell index
227 * @v: value to write
228 *
229 * Writes @v to the doorbell aperture at the
230 * requested doorbell index (VEGA10+).
231 */
232void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v)
233{
234 if (index < adev->doorbell.num_doorbells) {
235 atomic64_set((atomic64_t *)(adev->doorbell.ptr + index), v);
236 } else {
237 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
238 }
239}
240
241/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400242 * amdgpu_invalid_rreg - dummy reg read function
243 *
244 * @adev: amdgpu device pointer
245 * @reg: offset of register
246 *
247 * Dummy register read function. Used for register blocks
248 * that certain asics don't have (all asics).
249 * Returns the value in the register.
250 */
251static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
252{
253 DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
254 BUG();
255 return 0;
256}
257
258/**
259 * amdgpu_invalid_wreg - dummy reg write function
260 *
261 * @adev: amdgpu device pointer
262 * @reg: offset of register
263 * @v: value to write to the register
264 *
265 * Dummy register read function. Used for register blocks
266 * that certain asics don't have (all asics).
267 */
268static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
269{
270 DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
271 reg, v);
272 BUG();
273}
274
275/**
276 * amdgpu_block_invalid_rreg - dummy reg read function
277 *
278 * @adev: amdgpu device pointer
279 * @block: offset of instance
280 * @reg: offset of register
281 *
282 * Dummy register read function. Used for register blocks
283 * that certain asics don't have (all asics).
284 * Returns the value in the register.
285 */
286static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
287 uint32_t block, uint32_t reg)
288{
289 DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
290 reg, block);
291 BUG();
292 return 0;
293}
294
295/**
296 * amdgpu_block_invalid_wreg - dummy reg write function
297 *
298 * @adev: amdgpu device pointer
299 * @block: offset of instance
300 * @reg: offset of register
301 * @v: value to write to the register
302 *
303 * Dummy register read function. Used for register blocks
304 * that certain asics don't have (all asics).
305 */
306static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
307 uint32_t block,
308 uint32_t reg, uint32_t v)
309{
310 DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
311 reg, block, v);
312 BUG();
313}
314
315static int amdgpu_vram_scratch_init(struct amdgpu_device *adev)
316{
317 int r;
318
319 if (adev->vram_scratch.robj == NULL) {
320 r = amdgpu_bo_create(adev, AMDGPU_GPU_PAGE_SIZE,
Alex Deucher857d9132015-08-27 00:14:16 -0400321 PAGE_SIZE, true, AMDGPU_GEM_DOMAIN_VRAM,
Christian König03f48dd2016-08-15 17:00:22 +0200322 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
323 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS,
Christian König72d76682015-09-03 17:34:59 +0200324 NULL, NULL, &adev->vram_scratch.robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400325 if (r) {
326 return r;
327 }
328 }
329
330 r = amdgpu_bo_reserve(adev->vram_scratch.robj, false);
331 if (unlikely(r != 0))
332 return r;
333 r = amdgpu_bo_pin(adev->vram_scratch.robj,
334 AMDGPU_GEM_DOMAIN_VRAM, &adev->vram_scratch.gpu_addr);
335 if (r) {
336 amdgpu_bo_unreserve(adev->vram_scratch.robj);
337 return r;
338 }
339 r = amdgpu_bo_kmap(adev->vram_scratch.robj,
340 (void **)&adev->vram_scratch.ptr);
341 if (r)
342 amdgpu_bo_unpin(adev->vram_scratch.robj);
343 amdgpu_bo_unreserve(adev->vram_scratch.robj);
344
345 return r;
346}
347
348static void amdgpu_vram_scratch_fini(struct amdgpu_device *adev)
349{
350 int r;
351
352 if (adev->vram_scratch.robj == NULL) {
353 return;
354 }
Alex Xie8ab25b42017-04-24 13:30:43 -0400355 r = amdgpu_bo_reserve(adev->vram_scratch.robj, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400356 if (likely(r == 0)) {
357 amdgpu_bo_kunmap(adev->vram_scratch.robj);
358 amdgpu_bo_unpin(adev->vram_scratch.robj);
359 amdgpu_bo_unreserve(adev->vram_scratch.robj);
360 }
361 amdgpu_bo_unref(&adev->vram_scratch.robj);
362}
363
364/**
365 * amdgpu_program_register_sequence - program an array of registers.
366 *
367 * @adev: amdgpu_device pointer
368 * @registers: pointer to the register array
369 * @array_size: size of the register array
370 *
371 * Programs an array or registers with and and or masks.
372 * This is a helper for setting golden registers.
373 */
374void amdgpu_program_register_sequence(struct amdgpu_device *adev,
375 const u32 *registers,
376 const u32 array_size)
377{
378 u32 tmp, reg, and_mask, or_mask;
379 int i;
380
381 if (array_size % 3)
382 return;
383
384 for (i = 0; i < array_size; i +=3) {
385 reg = registers[i + 0];
386 and_mask = registers[i + 1];
387 or_mask = registers[i + 2];
388
389 if (and_mask == 0xffffffff) {
390 tmp = or_mask;
391 } else {
392 tmp = RREG32(reg);
393 tmp &= ~and_mask;
394 tmp |= or_mask;
395 }
396 WREG32(reg, tmp);
397 }
398}
399
400void amdgpu_pci_config_reset(struct amdgpu_device *adev)
401{
402 pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
403}
404
405/*
406 * GPU doorbell aperture helpers function.
407 */
408/**
409 * amdgpu_doorbell_init - Init doorbell driver information.
410 *
411 * @adev: amdgpu_device pointer
412 *
413 * Init doorbell driver information (CIK)
414 * Returns 0 on success, error on failure.
415 */
416static int amdgpu_doorbell_init(struct amdgpu_device *adev)
417{
418 /* doorbell bar mapping */
419 adev->doorbell.base = pci_resource_start(adev->pdev, 2);
420 adev->doorbell.size = pci_resource_len(adev->pdev, 2);
421
Christian Königedf600d2016-05-03 15:54:54 +0200422 adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400423 AMDGPU_DOORBELL_MAX_ASSIGNMENT+1);
424 if (adev->doorbell.num_doorbells == 0)
425 return -EINVAL;
426
Christian König8972e5d2017-03-06 13:34:57 +0100427 adev->doorbell.ptr = ioremap(adev->doorbell.base,
428 adev->doorbell.num_doorbells *
429 sizeof(u32));
430 if (adev->doorbell.ptr == NULL)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400431 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400432
433 return 0;
434}
435
436/**
437 * amdgpu_doorbell_fini - Tear down doorbell driver information.
438 *
439 * @adev: amdgpu_device pointer
440 *
441 * Tear down doorbell driver information (CIK)
442 */
443static void amdgpu_doorbell_fini(struct amdgpu_device *adev)
444{
445 iounmap(adev->doorbell.ptr);
446 adev->doorbell.ptr = NULL;
447}
448
449/**
450 * amdgpu_doorbell_get_kfd_info - Report doorbell configuration required to
451 * setup amdkfd
452 *
453 * @adev: amdgpu_device pointer
454 * @aperture_base: output returning doorbell aperture base physical address
455 * @aperture_size: output returning doorbell aperture size in bytes
456 * @start_offset: output returning # of doorbell bytes reserved for amdgpu.
457 *
458 * amdgpu and amdkfd share the doorbell aperture. amdgpu sets it up,
459 * takes doorbells required for its own rings and reports the setup to amdkfd.
460 * amdgpu reserved doorbells are at the start of the doorbell aperture.
461 */
462void amdgpu_doorbell_get_kfd_info(struct amdgpu_device *adev,
463 phys_addr_t *aperture_base,
464 size_t *aperture_size,
465 size_t *start_offset)
466{
467 /*
468 * The first num_doorbells are used by amdgpu.
469 * amdkfd takes whatever's left in the aperture.
470 */
471 if (adev->doorbell.size > adev->doorbell.num_doorbells * sizeof(u32)) {
472 *aperture_base = adev->doorbell.base;
473 *aperture_size = adev->doorbell.size;
474 *start_offset = adev->doorbell.num_doorbells * sizeof(u32);
475 } else {
476 *aperture_base = 0;
477 *aperture_size = 0;
478 *start_offset = 0;
479 }
480}
481
482/*
483 * amdgpu_wb_*()
Alex Xie455a7bc2017-05-08 21:36:03 -0400484 * Writeback is the method by which the GPU updates special pages in memory
Alex Xieea81a172017-05-08 13:41:11 -0400485 * with the status of certain GPU events (fences, ring pointers,etc.).
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400486 */
487
488/**
489 * amdgpu_wb_fini - Disable Writeback and free memory
490 *
491 * @adev: amdgpu_device pointer
492 *
493 * Disables Writeback and frees the Writeback memory (all asics).
494 * Used at driver shutdown.
495 */
496static void amdgpu_wb_fini(struct amdgpu_device *adev)
497{
498 if (adev->wb.wb_obj) {
Alex Deuchera76ed482016-10-21 15:30:36 -0400499 amdgpu_bo_free_kernel(&adev->wb.wb_obj,
500 &adev->wb.gpu_addr,
501 (void **)&adev->wb.wb);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400502 adev->wb.wb_obj = NULL;
503 }
504}
505
506/**
507 * amdgpu_wb_init- Init Writeback driver info and allocate memory
508 *
509 * @adev: amdgpu_device pointer
510 *
Alex Xie455a7bc2017-05-08 21:36:03 -0400511 * Initializes writeback and allocates writeback memory (all asics).
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400512 * Used at driver startup.
513 * Returns 0 on success or an -error on failure.
514 */
515static int amdgpu_wb_init(struct amdgpu_device *adev)
516{
517 int r;
518
519 if (adev->wb.wb_obj == NULL) {
Huang Rui60a970a62017-03-15 10:13:32 +0800520 r = amdgpu_bo_create_kernel(adev, AMDGPU_MAX_WB * sizeof(uint32_t),
Alex Deuchera76ed482016-10-21 15:30:36 -0400521 PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
522 &adev->wb.wb_obj, &adev->wb.gpu_addr,
523 (void **)&adev->wb.wb);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400524 if (r) {
525 dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
526 return r;
527 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400528
529 adev->wb.num_wb = AMDGPU_MAX_WB;
530 memset(&adev->wb.used, 0, sizeof(adev->wb.used));
531
532 /* clear wb memory */
Huang Rui60a970a62017-03-15 10:13:32 +0800533 memset((char *)adev->wb.wb, 0, AMDGPU_MAX_WB * sizeof(uint32_t));
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400534 }
535
536 return 0;
537}
538
539/**
540 * amdgpu_wb_get - Allocate a wb entry
541 *
542 * @adev: amdgpu_device pointer
543 * @wb: wb index
544 *
545 * Allocate a wb slot for use by the driver (all asics).
546 * Returns 0 on success or -EINVAL on failure.
547 */
548int amdgpu_wb_get(struct amdgpu_device *adev, u32 *wb)
549{
550 unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
551 if (offset < adev->wb.num_wb) {
552 __set_bit(offset, adev->wb.used);
553 *wb = offset;
554 return 0;
555 } else {
556 return -EINVAL;
557 }
558}
559
560/**
Ken Wang70142852016-03-18 15:08:49 +0800561 * amdgpu_wb_get_64bit - Allocate a wb entry
562 *
563 * @adev: amdgpu_device pointer
564 * @wb: wb index
565 *
566 * Allocate a wb slot for use by the driver (all asics).
567 * Returns 0 on success or -EINVAL on failure.
568 */
569int amdgpu_wb_get_64bit(struct amdgpu_device *adev, u32 *wb)
570{
571 unsigned long offset = bitmap_find_next_zero_area_off(adev->wb.used,
572 adev->wb.num_wb, 0, 2, 7, 0);
573 if ((offset + 1) < adev->wb.num_wb) {
574 __set_bit(offset, adev->wb.used);
575 __set_bit(offset + 1, adev->wb.used);
576 *wb = offset;
577 return 0;
578 } else {
579 return -EINVAL;
580 }
581}
582
583/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400584 * amdgpu_wb_free - Free a wb entry
585 *
586 * @adev: amdgpu_device pointer
587 * @wb: wb index
588 *
589 * Free a wb slot allocated for use by the driver (all asics)
590 */
591void amdgpu_wb_free(struct amdgpu_device *adev, u32 wb)
592{
593 if (wb < adev->wb.num_wb)
594 __clear_bit(wb, adev->wb.used);
595}
596
597/**
Ken Wang70142852016-03-18 15:08:49 +0800598 * amdgpu_wb_free_64bit - Free a wb entry
599 *
600 * @adev: amdgpu_device pointer
601 * @wb: wb index
602 *
603 * Free a wb slot allocated for use by the driver (all asics)
604 */
605void amdgpu_wb_free_64bit(struct amdgpu_device *adev, u32 wb)
606{
607 if ((wb + 1) < adev->wb.num_wb) {
608 __clear_bit(wb, adev->wb.used);
609 __clear_bit(wb + 1, adev->wb.used);
610 }
611}
612
613/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400614 * amdgpu_vram_location - try to find VRAM location
615 * @adev: amdgpu device structure holding all necessary informations
616 * @mc: memory controller structure holding memory informations
617 * @base: base address at which to put VRAM
618 *
Alex Xie455a7bc2017-05-08 21:36:03 -0400619 * Function will try to place VRAM at base address provided
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400620 * as parameter (which is so far either PCI aperture address or
621 * for IGP TOM base address).
622 *
623 * If there is not enough space to fit the unvisible VRAM in the 32bits
624 * address space then we limit the VRAM size to the aperture.
625 *
626 * Note: We don't explicitly enforce VRAM start to be aligned on VRAM size,
627 * this shouldn't be a problem as we are using the PCI aperture as a reference.
628 * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but
629 * not IGP.
630 *
631 * Note: we use mc_vram_size as on some board we need to program the mc to
632 * cover the whole aperture even if VRAM size is inferior to aperture size
633 * Novell bug 204882 + along with lots of ubuntu ones
634 *
635 * Note: when limiting vram it's safe to overwritte real_vram_size because
636 * we are not in case where real_vram_size is inferior to mc_vram_size (ie
637 * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu
638 * ones)
639 *
640 * Note: IGP TOM addr should be the same as the aperture addr, we don't
Alex Xie455a7bc2017-05-08 21:36:03 -0400641 * explicitly check for that though.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400642 *
643 * FIXME: when reducing VRAM size align new size on power of 2.
644 */
645void amdgpu_vram_location(struct amdgpu_device *adev, struct amdgpu_mc *mc, u64 base)
646{
647 uint64_t limit = (uint64_t)amdgpu_vram_limit << 20;
648
649 mc->vram_start = base;
650 if (mc->mc_vram_size > (adev->mc.mc_mask - base + 1)) {
651 dev_warn(adev->dev, "limiting VRAM to PCI aperture size\n");
652 mc->real_vram_size = mc->aper_size;
653 mc->mc_vram_size = mc->aper_size;
654 }
655 mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
656 if (limit && limit < mc->real_vram_size)
657 mc->real_vram_size = limit;
658 dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
659 mc->mc_vram_size >> 20, mc->vram_start,
660 mc->vram_end, mc->real_vram_size >> 20);
661}
662
663/**
664 * amdgpu_gtt_location - try to find GTT location
665 * @adev: amdgpu device structure holding all necessary informations
666 * @mc: memory controller structure holding memory informations
667 *
668 * Function will place try to place GTT before or after VRAM.
669 *
670 * If GTT size is bigger than space left then we ajust GTT size.
671 * Thus function will never fails.
672 *
673 * FIXME: when reducing GTT size align new size on power of 2.
674 */
675void amdgpu_gtt_location(struct amdgpu_device *adev, struct amdgpu_mc *mc)
676{
677 u64 size_af, size_bf;
678
679 size_af = ((adev->mc.mc_mask - mc->vram_end) + mc->gtt_base_align) & ~mc->gtt_base_align;
680 size_bf = mc->vram_start & ~mc->gtt_base_align;
681 if (size_bf > size_af) {
682 if (mc->gtt_size > size_bf) {
683 dev_warn(adev->dev, "limiting GTT\n");
684 mc->gtt_size = size_bf;
685 }
Alex Deucher9dc5a912016-11-17 15:40:22 -0500686 mc->gtt_start = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400687 } else {
688 if (mc->gtt_size > size_af) {
689 dev_warn(adev->dev, "limiting GTT\n");
690 mc->gtt_size = size_af;
691 }
692 mc->gtt_start = (mc->vram_end + 1 + mc->gtt_base_align) & ~mc->gtt_base_align;
693 }
694 mc->gtt_end = mc->gtt_start + mc->gtt_size - 1;
695 dev_info(adev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
696 mc->gtt_size >> 20, mc->gtt_start, mc->gtt_end);
697}
698
699/*
700 * GPU helpers function.
701 */
702/**
Jim Quc836fec2017-02-10 15:59:59 +0800703 * amdgpu_need_post - check if the hw need post or not
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400704 *
705 * @adev: amdgpu_device pointer
706 *
Jim Quc836fec2017-02-10 15:59:59 +0800707 * Check if the asic has been initialized (all asics) at driver startup
708 * or post is needed if hw reset is performed.
709 * Returns true if need or false if not.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400710 */
Jim Quc836fec2017-02-10 15:59:59 +0800711bool amdgpu_need_post(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400712{
713 uint32_t reg;
714
Jim Quc836fec2017-02-10 15:59:59 +0800715 if (adev->has_hw_reset) {
716 adev->has_hw_reset = false;
717 return true;
718 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400719 /* then check MEM_SIZE, in case the crtcs are off */
Alex Deucherbbf282d2017-03-03 17:26:10 -0500720 reg = amdgpu_asic_get_config_memsize(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400721
Alex Deucherf2713e82017-03-28 12:19:31 -0400722 if ((reg != 0) && (reg != 0xffffffff))
Jim Quc836fec2017-02-10 15:59:59 +0800723 return false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400724
Jim Quc836fec2017-02-10 15:59:59 +0800725 return true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400726
727}
728
Monk Liubec86372016-09-14 19:38:08 +0800729static bool amdgpu_vpost_needed(struct amdgpu_device *adev)
730{
731 if (amdgpu_sriov_vf(adev))
732 return false;
733
734 if (amdgpu_passthrough(adev)) {
Monk Liu1da2c322016-11-11 11:24:29 +0800735 /* for FIJI: In whole GPU pass-through virtualization case, after VM reboot
736 * some old smc fw still need driver do vPost otherwise gpu hang, while
737 * those smc fw version above 22.15 doesn't have this flaw, so we force
738 * vpost executed for smc version below 22.15
Monk Liubec86372016-09-14 19:38:08 +0800739 */
740 if (adev->asic_type == CHIP_FIJI) {
741 int err;
742 uint32_t fw_ver;
743 err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
744 /* force vPost if error occured */
745 if (err)
746 return true;
747
748 fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
Monk Liu1da2c322016-11-11 11:24:29 +0800749 if (fw_ver < 0x00160e00)
750 return true;
Monk Liubec86372016-09-14 19:38:08 +0800751 }
Monk Liubec86372016-09-14 19:38:08 +0800752 }
Jim Quc836fec2017-02-10 15:59:59 +0800753 return amdgpu_need_post(adev);
Monk Liubec86372016-09-14 19:38:08 +0800754}
755
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400756/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400757 * amdgpu_dummy_page_init - init dummy page used by the driver
758 *
759 * @adev: amdgpu_device pointer
760 *
761 * Allocate the dummy page used by the driver (all asics).
762 * This dummy page is used by the driver as a filler for gart entries
763 * when pages are taken out of the GART
764 * Returns 0 on sucess, -ENOMEM on failure.
765 */
766int amdgpu_dummy_page_init(struct amdgpu_device *adev)
767{
768 if (adev->dummy_page.page)
769 return 0;
770 adev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
771 if (adev->dummy_page.page == NULL)
772 return -ENOMEM;
773 adev->dummy_page.addr = pci_map_page(adev->pdev, adev->dummy_page.page,
774 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
775 if (pci_dma_mapping_error(adev->pdev, adev->dummy_page.addr)) {
776 dev_err(&adev->pdev->dev, "Failed to DMA MAP the dummy page\n");
777 __free_page(adev->dummy_page.page);
778 adev->dummy_page.page = NULL;
779 return -ENOMEM;
780 }
781 return 0;
782}
783
784/**
785 * amdgpu_dummy_page_fini - free dummy page used by the driver
786 *
787 * @adev: amdgpu_device pointer
788 *
789 * Frees the dummy page used by the driver (all asics).
790 */
791void amdgpu_dummy_page_fini(struct amdgpu_device *adev)
792{
793 if (adev->dummy_page.page == NULL)
794 return;
795 pci_unmap_page(adev->pdev, adev->dummy_page.addr,
796 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
797 __free_page(adev->dummy_page.page);
798 adev->dummy_page.page = NULL;
799}
800
801
802/* ATOM accessor methods */
803/*
804 * ATOM is an interpreted byte code stored in tables in the vbios. The
805 * driver registers callbacks to access registers and the interpreter
806 * in the driver parses the tables and executes then to program specific
807 * actions (set display modes, asic init, etc.). See amdgpu_atombios.c,
808 * atombios.h, and atom.c
809 */
810
811/**
812 * cail_pll_read - read PLL register
813 *
814 * @info: atom card_info pointer
815 * @reg: PLL register offset
816 *
817 * Provides a PLL register accessor for the atom interpreter (r4xx+).
818 * Returns the value of the PLL register.
819 */
820static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
821{
822 return 0;
823}
824
825/**
826 * cail_pll_write - write PLL register
827 *
828 * @info: atom card_info pointer
829 * @reg: PLL register offset
830 * @val: value to write to the pll register
831 *
832 * Provides a PLL register accessor for the atom interpreter (r4xx+).
833 */
834static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
835{
836
837}
838
839/**
840 * cail_mc_read - read MC (Memory Controller) register
841 *
842 * @info: atom card_info pointer
843 * @reg: MC register offset
844 *
845 * Provides an MC register accessor for the atom interpreter (r4xx+).
846 * Returns the value of the MC register.
847 */
848static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
849{
850 return 0;
851}
852
853/**
854 * cail_mc_write - write MC (Memory Controller) register
855 *
856 * @info: atom card_info pointer
857 * @reg: MC register offset
858 * @val: value to write to the pll register
859 *
860 * Provides a MC register accessor for the atom interpreter (r4xx+).
861 */
862static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
863{
864
865}
866
867/**
868 * cail_reg_write - write MMIO register
869 *
870 * @info: atom card_info pointer
871 * @reg: MMIO register offset
872 * @val: value to write to the pll register
873 *
874 * Provides a MMIO register accessor for the atom interpreter (r4xx+).
875 */
876static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
877{
878 struct amdgpu_device *adev = info->dev->dev_private;
879
880 WREG32(reg, val);
881}
882
883/**
884 * cail_reg_read - read MMIO register
885 *
886 * @info: atom card_info pointer
887 * @reg: MMIO register offset
888 *
889 * Provides an MMIO register accessor for the atom interpreter (r4xx+).
890 * Returns the value of the MMIO register.
891 */
892static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
893{
894 struct amdgpu_device *adev = info->dev->dev_private;
895 uint32_t r;
896
897 r = RREG32(reg);
898 return r;
899}
900
901/**
902 * cail_ioreg_write - write IO register
903 *
904 * @info: atom card_info pointer
905 * @reg: IO register offset
906 * @val: value to write to the pll register
907 *
908 * Provides a IO register accessor for the atom interpreter (r4xx+).
909 */
910static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
911{
912 struct amdgpu_device *adev = info->dev->dev_private;
913
914 WREG32_IO(reg, val);
915}
916
917/**
918 * cail_ioreg_read - read IO register
919 *
920 * @info: atom card_info pointer
921 * @reg: IO register offset
922 *
923 * Provides an IO register accessor for the atom interpreter (r4xx+).
924 * Returns the value of the IO register.
925 */
926static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
927{
928 struct amdgpu_device *adev = info->dev->dev_private;
929 uint32_t r;
930
931 r = RREG32_IO(reg);
932 return r;
933}
934
935/**
936 * amdgpu_atombios_fini - free the driver info and callbacks for atombios
937 *
938 * @adev: amdgpu_device pointer
939 *
940 * Frees the driver info and register access callbacks for the ATOM
941 * interpreter (r4xx+).
942 * Called at driver shutdown.
943 */
944static void amdgpu_atombios_fini(struct amdgpu_device *adev)
945{
Monk Liu89e0ec9f2016-05-27 19:34:11 +0800946 if (adev->mode_info.atom_context) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400947 kfree(adev->mode_info.atom_context->scratch);
Monk Liu89e0ec9f2016-05-27 19:34:11 +0800948 kfree(adev->mode_info.atom_context->iio);
949 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400950 kfree(adev->mode_info.atom_context);
951 adev->mode_info.atom_context = NULL;
952 kfree(adev->mode_info.atom_card_info);
953 adev->mode_info.atom_card_info = NULL;
954}
955
956/**
957 * amdgpu_atombios_init - init the driver info and callbacks for atombios
958 *
959 * @adev: amdgpu_device pointer
960 *
961 * Initializes the driver info and register access callbacks for the
962 * ATOM interpreter (r4xx+).
963 * Returns 0 on sucess, -ENOMEM on failure.
964 * Called at driver startup.
965 */
966static int amdgpu_atombios_init(struct amdgpu_device *adev)
967{
968 struct card_info *atom_card_info =
969 kzalloc(sizeof(struct card_info), GFP_KERNEL);
970
971 if (!atom_card_info)
972 return -ENOMEM;
973
974 adev->mode_info.atom_card_info = atom_card_info;
975 atom_card_info->dev = adev->ddev;
976 atom_card_info->reg_read = cail_reg_read;
977 atom_card_info->reg_write = cail_reg_write;
978 /* needed for iio ops */
979 if (adev->rio_mem) {
980 atom_card_info->ioreg_read = cail_ioreg_read;
981 atom_card_info->ioreg_write = cail_ioreg_write;
982 } else {
Amber Linb64a18c2017-01-04 08:06:58 -0500983 DRM_INFO("PCI I/O BAR is not found. Using MMIO to access ATOM BIOS\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400984 atom_card_info->ioreg_read = cail_reg_read;
985 atom_card_info->ioreg_write = cail_reg_write;
986 }
987 atom_card_info->mc_read = cail_mc_read;
988 atom_card_info->mc_write = cail_mc_write;
989 atom_card_info->pll_read = cail_pll_read;
990 atom_card_info->pll_write = cail_pll_write;
991
992 adev->mode_info.atom_context = amdgpu_atom_parse(atom_card_info, adev->bios);
993 if (!adev->mode_info.atom_context) {
994 amdgpu_atombios_fini(adev);
995 return -ENOMEM;
996 }
997
998 mutex_init(&adev->mode_info.atom_context->mutex);
Alex Deuchera5bde2f2016-09-23 16:23:41 -0400999 if (adev->is_atom_fw) {
1000 amdgpu_atomfirmware_scratch_regs_init(adev);
1001 amdgpu_atomfirmware_allocate_fb_scratch(adev);
1002 } else {
1003 amdgpu_atombios_scratch_regs_init(adev);
1004 amdgpu_atombios_allocate_fb_scratch(adev);
1005 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001006 return 0;
1007}
1008
1009/* if we get transitioned to only one device, take VGA back */
1010/**
1011 * amdgpu_vga_set_decode - enable/disable vga decode
1012 *
1013 * @cookie: amdgpu_device pointer
1014 * @state: enable/disable vga decode
1015 *
1016 * Enable/disable vga decode (all asics).
1017 * Returns VGA resource flags.
1018 */
1019static unsigned int amdgpu_vga_set_decode(void *cookie, bool state)
1020{
1021 struct amdgpu_device *adev = cookie;
1022 amdgpu_asic_set_vga_state(adev, state);
1023 if (state)
1024 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
1025 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1026 else
1027 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1028}
1029
1030/**
1031 * amdgpu_check_pot_argument - check that argument is a power of two
1032 *
1033 * @arg: value to check
1034 *
1035 * Validates that a certain argument is a power of two (all asics).
1036 * Returns true if argument is valid.
1037 */
1038static bool amdgpu_check_pot_argument(int arg)
1039{
1040 return (arg & (arg - 1)) == 0;
1041}
1042
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001043static void amdgpu_check_block_size(struct amdgpu_device *adev)
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001044{
1045 /* defines number of bits in page table versus page directory,
1046 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
1047 * page table and the remaining bits are in the page directory */
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001048 if (amdgpu_vm_block_size == -1)
1049 return;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001050
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001051 if (amdgpu_vm_block_size < 9) {
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001052 dev_warn(adev->dev, "VM page table size (%d) too small\n",
1053 amdgpu_vm_block_size);
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001054 goto def_value;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001055 }
1056
1057 if (amdgpu_vm_block_size > 24 ||
1058 (amdgpu_vm_size * 1024) < (1ull << amdgpu_vm_block_size)) {
1059 dev_warn(adev->dev, "VM page table size (%d) too large\n",
1060 amdgpu_vm_block_size);
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001061 goto def_value;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001062 }
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001063
1064 return;
1065
1066def_value:
1067 amdgpu_vm_block_size = -1;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001068}
1069
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001070static void amdgpu_check_vm_size(struct amdgpu_device *adev)
1071{
1072 if (!amdgpu_check_pot_argument(amdgpu_vm_size)) {
1073 dev_warn(adev->dev, "VM size (%d) must be a power of 2\n",
1074 amdgpu_vm_size);
1075 goto def_value;
1076 }
1077
1078 if (amdgpu_vm_size < 1) {
1079 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
1080 amdgpu_vm_size);
1081 goto def_value;
1082 }
1083
1084 /*
1085 * Max GPUVM size for Cayman, SI, CI VI are 40 bits.
1086 */
1087 if (amdgpu_vm_size > 1024) {
1088 dev_warn(adev->dev, "VM size (%d) too large, max is 1TB\n",
1089 amdgpu_vm_size);
1090 goto def_value;
1091 }
1092
1093 return;
1094
1095def_value:
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001096 amdgpu_vm_size = -1;
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001097}
1098
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001099/**
1100 * amdgpu_check_arguments - validate module params
1101 *
1102 * @adev: amdgpu_device pointer
1103 *
1104 * Validates certain module parameters and updates
1105 * the associated values used by the driver (all asics).
1106 */
1107static void amdgpu_check_arguments(struct amdgpu_device *adev)
1108{
Chunming Zhou5b011232015-12-10 17:34:33 +08001109 if (amdgpu_sched_jobs < 4) {
1110 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
1111 amdgpu_sched_jobs);
1112 amdgpu_sched_jobs = 4;
1113 } else if (!amdgpu_check_pot_argument(amdgpu_sched_jobs)){
1114 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
1115 amdgpu_sched_jobs);
1116 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
1117 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001118
1119 if (amdgpu_gart_size != -1) {
Christian Königc4e1a132016-03-17 16:25:15 +01001120 /* gtt size must be greater or equal to 32M */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001121 if (amdgpu_gart_size < 32) {
1122 dev_warn(adev->dev, "gart size (%d) too small\n",
1123 amdgpu_gart_size);
1124 amdgpu_gart_size = -1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001125 }
1126 }
1127
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001128 amdgpu_check_vm_size(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001129
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001130 amdgpu_check_block_size(adev);
Christian König6a7f76e2016-08-24 15:51:49 +02001131
jimqu526bae32016-11-07 09:53:10 +08001132 if (amdgpu_vram_page_split != -1 && (amdgpu_vram_page_split < 16 ||
1133 !amdgpu_check_pot_argument(amdgpu_vram_page_split))) {
Christian König6a7f76e2016-08-24 15:51:49 +02001134 dev_warn(adev->dev, "invalid VRAM page split (%d)\n",
1135 amdgpu_vram_page_split);
1136 amdgpu_vram_page_split = 1024;
1137 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001138}
1139
1140/**
1141 * amdgpu_switcheroo_set_state - set switcheroo state
1142 *
1143 * @pdev: pci dev pointer
Lukas Wunner16944672015-09-05 11:17:35 +02001144 * @state: vga_switcheroo state
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001145 *
1146 * Callback for the switcheroo driver. Suspends or resumes the
1147 * the asics before or after it is powered up using ACPI methods.
1148 */
1149static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1150{
1151 struct drm_device *dev = pci_get_drvdata(pdev);
1152
1153 if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
1154 return;
1155
1156 if (state == VGA_SWITCHEROO_ON) {
1157 unsigned d3_delay = dev->pdev->d3_delay;
1158
Joe Perches7ca85292017-02-28 04:55:52 -08001159 pr_info("amdgpu: switched on\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001160 /* don't suspend or resume card normally */
1161 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1162
Alex Deucher810ddc32016-08-23 13:25:49 -04001163 amdgpu_device_resume(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001164
1165 dev->pdev->d3_delay = d3_delay;
1166
1167 dev->switch_power_state = DRM_SWITCH_POWER_ON;
1168 drm_kms_helper_poll_enable(dev);
1169 } else {
Joe Perches7ca85292017-02-28 04:55:52 -08001170 pr_info("amdgpu: switched off\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001171 drm_kms_helper_poll_disable(dev);
1172 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Alex Deucher810ddc32016-08-23 13:25:49 -04001173 amdgpu_device_suspend(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001174 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1175 }
1176}
1177
1178/**
1179 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1180 *
1181 * @pdev: pci dev pointer
1182 *
1183 * Callback for the switcheroo driver. Check of the switcheroo
1184 * state can be changed.
1185 * Returns true if the state can be changed, false if not.
1186 */
1187static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1188{
1189 struct drm_device *dev = pci_get_drvdata(pdev);
1190
1191 /*
1192 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1193 * locking inversion with the driver load path. And the access here is
1194 * completely racy anyway. So don't bother with locking for now.
1195 */
1196 return dev->open_count == 0;
1197}
1198
1199static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1200 .set_gpu_state = amdgpu_switcheroo_set_state,
1201 .reprobe = NULL,
1202 .can_switch = amdgpu_switcheroo_can_switch,
1203};
1204
1205int amdgpu_set_clockgating_state(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001206 enum amd_ip_block_type block_type,
1207 enum amd_clockgating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001208{
1209 int i, r = 0;
1210
1211 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001212 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001213 continue;
Rex Zhuc7228652017-02-22 15:33:46 +08001214 if (adev->ip_blocks[i].version->type != block_type)
1215 continue;
1216 if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
1217 continue;
1218 r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
1219 (void *)adev, state);
1220 if (r)
1221 DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
1222 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001223 }
1224 return r;
1225}
1226
1227int amdgpu_set_powergating_state(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001228 enum amd_ip_block_type block_type,
1229 enum amd_powergating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001230{
1231 int i, r = 0;
1232
1233 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001234 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001235 continue;
Rex Zhuc7228652017-02-22 15:33:46 +08001236 if (adev->ip_blocks[i].version->type != block_type)
1237 continue;
1238 if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
1239 continue;
1240 r = adev->ip_blocks[i].version->funcs->set_powergating_state(
1241 (void *)adev, state);
1242 if (r)
1243 DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
1244 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001245 }
1246 return r;
1247}
1248
Huang Rui6cb2d4e2017-01-05 18:44:41 +08001249void amdgpu_get_clockgating_state(struct amdgpu_device *adev, u32 *flags)
1250{
1251 int i;
1252
1253 for (i = 0; i < adev->num_ip_blocks; i++) {
1254 if (!adev->ip_blocks[i].status.valid)
1255 continue;
1256 if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
1257 adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
1258 }
1259}
1260
Alex Deucher5dbbb602016-06-23 11:41:04 -04001261int amdgpu_wait_for_idle(struct amdgpu_device *adev,
1262 enum amd_ip_block_type block_type)
1263{
1264 int i, r;
1265
1266 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001267 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001268 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001269 if (adev->ip_blocks[i].version->type == block_type) {
1270 r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -04001271 if (r)
1272 return r;
1273 break;
1274 }
1275 }
1276 return 0;
1277
1278}
1279
1280bool amdgpu_is_idle(struct amdgpu_device *adev,
1281 enum amd_ip_block_type block_type)
1282{
1283 int i;
1284
1285 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001286 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001287 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001288 if (adev->ip_blocks[i].version->type == block_type)
1289 return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -04001290 }
1291 return true;
1292
1293}
1294
Alex Deuchera1255102016-10-13 17:41:13 -04001295struct amdgpu_ip_block * amdgpu_get_ip_block(struct amdgpu_device *adev,
1296 enum amd_ip_block_type type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001297{
1298 int i;
1299
1300 for (i = 0; i < adev->num_ip_blocks; i++)
Alex Deuchera1255102016-10-13 17:41:13 -04001301 if (adev->ip_blocks[i].version->type == type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001302 return &adev->ip_blocks[i];
1303
1304 return NULL;
1305}
1306
1307/**
1308 * amdgpu_ip_block_version_cmp
1309 *
1310 * @adev: amdgpu_device pointer
yanyang15fc3aee2015-05-22 14:39:35 -04001311 * @type: enum amd_ip_block_type
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001312 * @major: major version
1313 * @minor: minor version
1314 *
1315 * return 0 if equal or greater
1316 * return 1 if smaller or the ip_block doesn't exist
1317 */
1318int amdgpu_ip_block_version_cmp(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001319 enum amd_ip_block_type type,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001320 u32 major, u32 minor)
1321{
Alex Deuchera1255102016-10-13 17:41:13 -04001322 struct amdgpu_ip_block *ip_block = amdgpu_get_ip_block(adev, type);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001323
Alex Deuchera1255102016-10-13 17:41:13 -04001324 if (ip_block && ((ip_block->version->major > major) ||
1325 ((ip_block->version->major == major) &&
1326 (ip_block->version->minor >= minor))))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001327 return 0;
1328
1329 return 1;
1330}
1331
Alex Deuchera1255102016-10-13 17:41:13 -04001332/**
1333 * amdgpu_ip_block_add
1334 *
1335 * @adev: amdgpu_device pointer
1336 * @ip_block_version: pointer to the IP to add
1337 *
1338 * Adds the IP block driver information to the collection of IPs
1339 * on the asic.
1340 */
1341int amdgpu_ip_block_add(struct amdgpu_device *adev,
1342 const struct amdgpu_ip_block_version *ip_block_version)
1343{
1344 if (!ip_block_version)
1345 return -EINVAL;
1346
1347 adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
1348
1349 return 0;
1350}
1351
Alex Deucher483ef982016-09-30 12:43:04 -04001352static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
Emily Deng9accf2f2016-08-10 16:01:25 +08001353{
1354 adev->enable_virtual_display = false;
1355
1356 if (amdgpu_virtual_display) {
1357 struct drm_device *ddev = adev->ddev;
1358 const char *pci_address_name = pci_name(ddev->pdev);
Emily Deng0f663562016-09-30 13:02:18 -04001359 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
Emily Deng9accf2f2016-08-10 16:01:25 +08001360
1361 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1362 pciaddstr_tmp = pciaddstr;
Emily Deng0f663562016-09-30 13:02:18 -04001363 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1364 pciaddname = strsep(&pciaddname_tmp, ",");
Yintian Tao967de2a2017-01-22 15:16:51 +08001365 if (!strcmp("all", pciaddname)
1366 || !strcmp(pci_address_name, pciaddname)) {
Emily Deng0f663562016-09-30 13:02:18 -04001367 long num_crtc;
1368 int res = -1;
1369
Emily Deng9accf2f2016-08-10 16:01:25 +08001370 adev->enable_virtual_display = true;
Emily Deng0f663562016-09-30 13:02:18 -04001371
1372 if (pciaddname_tmp)
1373 res = kstrtol(pciaddname_tmp, 10,
1374 &num_crtc);
1375
1376 if (!res) {
1377 if (num_crtc < 1)
1378 num_crtc = 1;
1379 if (num_crtc > 6)
1380 num_crtc = 6;
1381 adev->mode_info.num_crtc = num_crtc;
1382 } else {
1383 adev->mode_info.num_crtc = 1;
1384 }
Emily Deng9accf2f2016-08-10 16:01:25 +08001385 break;
1386 }
1387 }
1388
Emily Deng0f663562016-09-30 13:02:18 -04001389 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
1390 amdgpu_virtual_display, pci_address_name,
1391 adev->enable_virtual_display, adev->mode_info.num_crtc);
Emily Deng9accf2f2016-08-10 16:01:25 +08001392
1393 kfree(pciaddstr);
1394 }
1395}
1396
Alex Deuchere2a75f82017-04-27 16:58:01 -04001397static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev)
1398{
1399 const struct firmware *fw;
1400 const char *chip_name;
1401 char fw_name[30];
1402 int err;
1403 const struct gpu_info_firmware_header_v1_0 *hdr;
1404
1405 switch (adev->asic_type) {
1406 case CHIP_TOPAZ:
1407 case CHIP_TONGA:
1408 case CHIP_FIJI:
1409 case CHIP_POLARIS11:
1410 case CHIP_POLARIS10:
1411 case CHIP_POLARIS12:
1412 case CHIP_CARRIZO:
1413 case CHIP_STONEY:
1414#ifdef CONFIG_DRM_AMDGPU_SI
1415 case CHIP_VERDE:
1416 case CHIP_TAHITI:
1417 case CHIP_PITCAIRN:
1418 case CHIP_OLAND:
1419 case CHIP_HAINAN:
1420#endif
1421#ifdef CONFIG_DRM_AMDGPU_CIK
1422 case CHIP_BONAIRE:
1423 case CHIP_HAWAII:
1424 case CHIP_KAVERI:
1425 case CHIP_KABINI:
1426 case CHIP_MULLINS:
1427#endif
1428 default:
1429 return 0;
1430 case CHIP_VEGA10:
1431 chip_name = "vega10";
1432 break;
1433 }
1434
1435 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name);
1436 err = request_firmware(&fw, fw_name, adev->dev);
1437 if (err) {
1438 dev_err(adev->dev,
1439 "Failed to load gpu_info firmware \"%s\"\n",
1440 fw_name);
1441 goto out;
1442 }
1443 err = amdgpu_ucode_validate(fw);
1444 if (err) {
1445 dev_err(adev->dev,
1446 "Failed to validate gpu_info firmware \"%s\"\n",
1447 fw_name);
1448 goto out;
1449 }
1450
1451 hdr = (const struct gpu_info_firmware_header_v1_0 *)fw->data;
1452 amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
1453
1454 switch (hdr->version_major) {
1455 case 1:
1456 {
1457 const struct gpu_info_firmware_v1_0 *gpu_info_fw =
1458 (const struct gpu_info_firmware_v1_0 *)(fw->data +
1459 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1460
1461 adev->gfx.config.max_shader_engines = gpu_info_fw->gc_num_se;
1462 adev->gfx.config.max_cu_per_sh = gpu_info_fw->gc_num_cu_per_sh;
1463 adev->gfx.config.max_sh_per_se = gpu_info_fw->gc_num_sh_per_se;
1464 adev->gfx.config.max_backends_per_se = gpu_info_fw->gc_num_rb_per_se;
1465 adev->gfx.config.max_texture_channel_caches =
1466 gpu_info_fw->gc_num_tccs;
1467 adev->gfx.config.max_gprs = gpu_info_fw->gc_num_gprs;
1468 adev->gfx.config.max_gs_threads = gpu_info_fw->gc_num_max_gs_thds;
1469 adev->gfx.config.gs_vgt_table_depth = gpu_info_fw->gc_gs_table_depth;
1470 adev->gfx.config.gs_prim_buffer_depth = gpu_info_fw->gc_gsprim_buff_depth;
1471 adev->gfx.config.double_offchip_lds_buf =
1472 gpu_info_fw->gc_double_offchip_lds_buffer;
1473 adev->gfx.cu_info.wave_front_size = gpu_info_fw->gc_wave_size;
1474 break;
1475 }
1476 default:
1477 dev_err(adev->dev,
1478 "Unsupported gpu_info table %d\n", hdr->header.ucode_version);
1479 err = -EINVAL;
1480 goto out;
1481 }
1482out:
1483 release_firmware(fw);
1484 fw = NULL;
1485
1486 return err;
1487}
1488
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001489static int amdgpu_early_init(struct amdgpu_device *adev)
1490{
Alex Deucheraaa36a92015-04-20 17:31:14 -04001491 int i, r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001492
Alex Deucher483ef982016-09-30 12:43:04 -04001493 amdgpu_device_enable_virtual_display(adev);
Emily Denga6be7572016-08-08 11:37:50 +08001494
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001495 switch (adev->asic_type) {
Alex Deucheraaa36a92015-04-20 17:31:14 -04001496 case CHIP_TOPAZ:
1497 case CHIP_TONGA:
David Zhang48299f92015-07-08 01:05:16 +08001498 case CHIP_FIJI:
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001499 case CHIP_POLARIS11:
1500 case CHIP_POLARIS10:
Junwei Zhangc4642a42016-12-14 15:32:28 -05001501 case CHIP_POLARIS12:
Alex Deucheraaa36a92015-04-20 17:31:14 -04001502 case CHIP_CARRIZO:
Samuel Li39bb0c92015-10-08 16:31:43 -04001503 case CHIP_STONEY:
1504 if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
Alex Deucheraaa36a92015-04-20 17:31:14 -04001505 adev->family = AMDGPU_FAMILY_CZ;
1506 else
1507 adev->family = AMDGPU_FAMILY_VI;
1508
1509 r = vi_set_ip_blocks(adev);
1510 if (r)
1511 return r;
1512 break;
Ken Wang33f34802016-01-21 17:29:41 +08001513#ifdef CONFIG_DRM_AMDGPU_SI
1514 case CHIP_VERDE:
1515 case CHIP_TAHITI:
1516 case CHIP_PITCAIRN:
1517 case CHIP_OLAND:
1518 case CHIP_HAINAN:
Ken Wang295d0da2016-05-24 21:02:53 +08001519 adev->family = AMDGPU_FAMILY_SI;
Ken Wang33f34802016-01-21 17:29:41 +08001520 r = si_set_ip_blocks(adev);
1521 if (r)
1522 return r;
1523 break;
1524#endif
Alex Deuchera2e73f52015-04-20 17:09:27 -04001525#ifdef CONFIG_DRM_AMDGPU_CIK
1526 case CHIP_BONAIRE:
1527 case CHIP_HAWAII:
1528 case CHIP_KAVERI:
1529 case CHIP_KABINI:
1530 case CHIP_MULLINS:
1531 if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
1532 adev->family = AMDGPU_FAMILY_CI;
1533 else
1534 adev->family = AMDGPU_FAMILY_KV;
1535
1536 r = cik_set_ip_blocks(adev);
1537 if (r)
1538 return r;
1539 break;
1540#endif
Chunming Zhou2ca8a5d2016-12-07 17:31:19 +08001541 case CHIP_VEGA10:
1542 case CHIP_RAVEN:
1543 if (adev->asic_type == CHIP_RAVEN)
1544 adev->family = AMDGPU_FAMILY_RV;
1545 else
1546 adev->family = AMDGPU_FAMILY_AI;
Ken Wang460826e2017-03-06 14:53:16 -05001547
1548 r = soc15_set_ip_blocks(adev);
1549 if (r)
1550 return r;
1551 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001552 default:
1553 /* FIXME: not supported yet */
1554 return -EINVAL;
1555 }
1556
Alex Deuchere2a75f82017-04-27 16:58:01 -04001557 r = amdgpu_device_parse_gpu_info_fw(adev);
1558 if (r)
1559 return r;
1560
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001561 if (amdgpu_sriov_vf(adev)) {
1562 r = amdgpu_virt_request_full_gpu(adev, true);
1563 if (r)
1564 return r;
1565 }
1566
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001567 for (i = 0; i < adev->num_ip_blocks; i++) {
1568 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
1569 DRM_ERROR("disabled ip block: %d\n", i);
Alex Deuchera1255102016-10-13 17:41:13 -04001570 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001571 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001572 if (adev->ip_blocks[i].version->funcs->early_init) {
1573 r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001574 if (r == -ENOENT) {
Alex Deuchera1255102016-10-13 17:41:13 -04001575 adev->ip_blocks[i].status.valid = false;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001576 } else if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001577 DRM_ERROR("early_init of IP block <%s> failed %d\n",
1578 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001579 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001580 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001581 adev->ip_blocks[i].status.valid = true;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001582 }
Alex Deucher974e6b62015-07-10 13:59:44 -04001583 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001584 adev->ip_blocks[i].status.valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001585 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001586 }
1587 }
1588
Nicolai Hähnle395d1fb2016-06-02 12:32:07 +02001589 adev->cg_flags &= amdgpu_cg_mask;
1590 adev->pg_flags &= amdgpu_pg_mask;
1591
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001592 return 0;
1593}
1594
1595static int amdgpu_init(struct amdgpu_device *adev)
1596{
1597 int i, r;
1598
1599 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001600 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001601 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001602 r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001603 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001604 DRM_ERROR("sw_init of IP block <%s> failed %d\n",
1605 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001606 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001607 }
Alex Deuchera1255102016-10-13 17:41:13 -04001608 adev->ip_blocks[i].status.sw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001609 /* need to do gmc hw init early so we can allocate gpu mem */
Alex Deuchera1255102016-10-13 17:41:13 -04001610 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001611 r = amdgpu_vram_scratch_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001612 if (r) {
1613 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001614 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001615 }
Alex Deuchera1255102016-10-13 17:41:13 -04001616 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001617 if (r) {
1618 DRM_ERROR("hw_init %d failed %d\n", i, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001619 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001620 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001621 r = amdgpu_wb_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001622 if (r) {
1623 DRM_ERROR("amdgpu_wb_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001624 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001625 }
Alex Deuchera1255102016-10-13 17:41:13 -04001626 adev->ip_blocks[i].status.hw = true;
Monk Liu24936642017-01-09 15:54:32 +08001627
1628 /* right after GMC hw init, we create CSA */
1629 if (amdgpu_sriov_vf(adev)) {
1630 r = amdgpu_allocate_static_csa(adev);
1631 if (r) {
1632 DRM_ERROR("allocate CSA failed %d\n", r);
1633 return r;
1634 }
1635 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001636 }
1637 }
1638
1639 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001640 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001641 continue;
1642 /* gmc hw init is done early */
Alex Deuchera1255102016-10-13 17:41:13 -04001643 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001644 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001645 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001646 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001647 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
1648 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001649 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001650 }
Alex Deuchera1255102016-10-13 17:41:13 -04001651 adev->ip_blocks[i].status.hw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001652 }
1653
1654 return 0;
1655}
1656
1657static int amdgpu_late_init(struct amdgpu_device *adev)
1658{
1659 int i = 0, r;
1660
1661 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001662 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001663 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001664 if (adev->ip_blocks[i].version->funcs->late_init) {
1665 r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001666 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001667 DRM_ERROR("late_init of IP block <%s> failed %d\n",
1668 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001669 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001670 }
Alex Deuchera1255102016-10-13 17:41:13 -04001671 adev->ip_blocks[i].status.late_initialized = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001672 }
Alex Deucher4a446d52016-10-07 14:48:18 -04001673 /* skip CG for VCE/UVD, it's handled specially */
Alex Deuchera1255102016-10-13 17:41:13 -04001674 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1675 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
Alex Deucher4a446d52016-10-07 14:48:18 -04001676 /* enable clockgating to save power */
Alex Deuchera1255102016-10-13 17:41:13 -04001677 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1678 AMD_CG_STATE_GATE);
Alex Deucher4a446d52016-10-07 14:48:18 -04001679 if (r) {
1680 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001681 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher4a446d52016-10-07 14:48:18 -04001682 return r;
1683 }
Arindam Nathb0b00ff2016-10-07 19:01:37 +05301684 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001685 }
1686
1687 return 0;
1688}
1689
1690static int amdgpu_fini(struct amdgpu_device *adev)
1691{
1692 int i, r;
1693
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001694 /* need to disable SMC first */
1695 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001696 if (!adev->ip_blocks[i].status.hw)
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001697 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001698 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001699 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
Alex Deuchera1255102016-10-13 17:41:13 -04001700 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1701 AMD_CG_STATE_UNGATE);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001702 if (r) {
1703 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001704 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001705 return r;
1706 }
Alex Deuchera1255102016-10-13 17:41:13 -04001707 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001708 /* XXX handle errors */
1709 if (r) {
1710 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001711 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001712 }
Alex Deuchera1255102016-10-13 17:41:13 -04001713 adev->ip_blocks[i].status.hw = false;
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001714 break;
1715 }
1716 }
1717
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001718 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001719 if (!adev->ip_blocks[i].status.hw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001720 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001721 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001722 amdgpu_wb_fini(adev);
1723 amdgpu_vram_scratch_fini(adev);
1724 }
Rex Zhu8201a672016-11-24 21:44:44 +08001725
1726 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1727 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1728 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
1729 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1730 AMD_CG_STATE_UNGATE);
1731 if (r) {
1732 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1733 adev->ip_blocks[i].version->funcs->name, r);
1734 return r;
1735 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001736 }
Rex Zhu8201a672016-11-24 21:44:44 +08001737
Alex Deuchera1255102016-10-13 17:41:13 -04001738 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001739 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001740 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001741 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
1742 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001743 }
Rex Zhu8201a672016-11-24 21:44:44 +08001744
Alex Deuchera1255102016-10-13 17:41:13 -04001745 adev->ip_blocks[i].status.hw = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001746 }
1747
1748 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001749 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001750 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001751 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001752 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001753 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001754 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
1755 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001756 }
Alex Deuchera1255102016-10-13 17:41:13 -04001757 adev->ip_blocks[i].status.sw = false;
1758 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001759 }
1760
Monk Liua6dcfd92016-05-19 14:36:34 +08001761 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001762 if (!adev->ip_blocks[i].status.late_initialized)
Grazvydas Ignotas8a2eef12016-10-03 00:06:44 +03001763 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001764 if (adev->ip_blocks[i].version->funcs->late_fini)
1765 adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
1766 adev->ip_blocks[i].status.late_initialized = false;
Monk Liua6dcfd92016-05-19 14:36:34 +08001767 }
1768
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001769 if (amdgpu_sriov_vf(adev)) {
Monk Liu24936642017-01-09 15:54:32 +08001770 amdgpu_bo_free_kernel(&adev->virt.csa_obj, &adev->virt.csa_vmid0_addr, NULL);
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001771 amdgpu_virt_release_full_gpu(adev, false);
1772 }
Monk Liu24936642017-01-09 15:54:32 +08001773
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001774 return 0;
1775}
1776
Alex Deucherfaefba92016-12-06 10:38:29 -05001777int amdgpu_suspend(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001778{
1779 int i, r;
1780
Xiangliang Yue941ea92017-01-18 12:47:55 +08001781 if (amdgpu_sriov_vf(adev))
1782 amdgpu_virt_request_full_gpu(adev, false);
1783
Flora Cuic5a93a22016-02-26 10:45:25 +08001784 /* ungate SMC block first */
1785 r = amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC,
1786 AMD_CG_STATE_UNGATE);
1787 if (r) {
1788 DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n",r);
1789 }
1790
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001791 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001792 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001793 continue;
1794 /* ungate blocks so that suspend can properly shut them down */
Flora Cuic5a93a22016-02-26 10:45:25 +08001795 if (i != AMD_IP_BLOCK_TYPE_SMC) {
Alex Deuchera1255102016-10-13 17:41:13 -04001796 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1797 AMD_CG_STATE_UNGATE);
Flora Cuic5a93a22016-02-26 10:45:25 +08001798 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001799 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1800 adev->ip_blocks[i].version->funcs->name, r);
Flora Cuic5a93a22016-02-26 10:45:25 +08001801 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001802 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001803 /* XXX handle errors */
Alex Deuchera1255102016-10-13 17:41:13 -04001804 r = adev->ip_blocks[i].version->funcs->suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001805 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001806 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001807 DRM_ERROR("suspend of IP block <%s> failed %d\n",
1808 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001809 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001810 }
1811
Xiangliang Yue941ea92017-01-18 12:47:55 +08001812 if (amdgpu_sriov_vf(adev))
1813 amdgpu_virt_release_full_gpu(adev, false);
1814
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001815 return 0;
1816}
1817
Monk Liue4f0fdc2017-02-09 11:55:49 +08001818static int amdgpu_sriov_reinit_early(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08001819{
1820 int i, r;
1821
Monk Liu2cb681b2017-04-26 12:00:49 +08001822 static enum amd_ip_block_type ip_order[] = {
1823 AMD_IP_BLOCK_TYPE_GMC,
1824 AMD_IP_BLOCK_TYPE_COMMON,
1825 AMD_IP_BLOCK_TYPE_GFXHUB,
1826 AMD_IP_BLOCK_TYPE_MMHUB,
1827 AMD_IP_BLOCK_TYPE_IH,
1828 };
Monk Liua90ad3c2017-01-23 14:22:08 +08001829
Monk Liu2cb681b2017-04-26 12:00:49 +08001830 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1831 int j;
1832 struct amdgpu_ip_block *block;
Monk Liua90ad3c2017-01-23 14:22:08 +08001833
Monk Liu2cb681b2017-04-26 12:00:49 +08001834 for (j = 0; j < adev->num_ip_blocks; j++) {
1835 block = &adev->ip_blocks[j];
1836
1837 if (block->version->type != ip_order[i] ||
1838 !block->status.valid)
1839 continue;
1840
1841 r = block->version->funcs->hw_init(adev);
1842 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
Monk Liua90ad3c2017-01-23 14:22:08 +08001843 }
1844 }
1845
1846 return 0;
1847}
1848
Monk Liue4f0fdc2017-02-09 11:55:49 +08001849static int amdgpu_sriov_reinit_late(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08001850{
1851 int i, r;
1852
Monk Liu2cb681b2017-04-26 12:00:49 +08001853 static enum amd_ip_block_type ip_order[] = {
1854 AMD_IP_BLOCK_TYPE_SMC,
1855 AMD_IP_BLOCK_TYPE_DCE,
1856 AMD_IP_BLOCK_TYPE_GFX,
1857 AMD_IP_BLOCK_TYPE_SDMA,
1858 AMD_IP_BLOCK_TYPE_VCE,
1859 };
Monk Liua90ad3c2017-01-23 14:22:08 +08001860
Monk Liu2cb681b2017-04-26 12:00:49 +08001861 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1862 int j;
1863 struct amdgpu_ip_block *block;
Monk Liua90ad3c2017-01-23 14:22:08 +08001864
Monk Liu2cb681b2017-04-26 12:00:49 +08001865 for (j = 0; j < adev->num_ip_blocks; j++) {
1866 block = &adev->ip_blocks[j];
1867
1868 if (block->version->type != ip_order[i] ||
1869 !block->status.valid)
1870 continue;
1871
1872 r = block->version->funcs->hw_init(adev);
1873 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
Monk Liua90ad3c2017-01-23 14:22:08 +08001874 }
1875 }
1876
1877 return 0;
1878}
1879
Chunming Zhoufcf06492017-05-05 10:33:33 +08001880static int amdgpu_resume_phase1(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001881{
1882 int i, r;
1883
1884 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001885 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001886 continue;
Chunming Zhoufcf06492017-05-05 10:33:33 +08001887 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
1888 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
1889 adev->ip_blocks[i].version->type ==
1890 AMD_IP_BLOCK_TYPE_IH) {
1891 r = adev->ip_blocks[i].version->funcs->resume(adev);
1892 if (r) {
1893 DRM_ERROR("resume of IP block <%s> failed %d\n",
1894 adev->ip_blocks[i].version->funcs->name, r);
1895 return r;
1896 }
1897 }
1898 }
1899
1900 return 0;
1901}
1902
1903static int amdgpu_resume_phase2(struct amdgpu_device *adev)
1904{
1905 int i, r;
1906
1907 for (i = 0; i < adev->num_ip_blocks; i++) {
1908 if (!adev->ip_blocks[i].status.valid)
1909 continue;
1910 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
1911 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
1912 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH )
1913 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001914 r = adev->ip_blocks[i].version->funcs->resume(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001915 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001916 DRM_ERROR("resume of IP block <%s> failed %d\n",
1917 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001918 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001919 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001920 }
1921
1922 return 0;
1923}
1924
Chunming Zhoufcf06492017-05-05 10:33:33 +08001925static int amdgpu_resume(struct amdgpu_device *adev)
1926{
1927 int r;
1928
1929 r = amdgpu_resume_phase1(adev);
1930 if (r)
1931 return r;
1932 r = amdgpu_resume_phase2(adev);
1933
1934 return r;
1935}
1936
Monk Liu4e99a442016-03-31 13:26:59 +08001937static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
Andres Rodriguez048765a2016-06-11 02:51:32 -04001938{
Alex Deuchera5bde2f2016-09-23 16:23:41 -04001939 if (adev->is_atom_fw) {
1940 if (amdgpu_atomfirmware_gpu_supports_virtualization(adev))
1941 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
1942 } else {
1943 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
1944 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
1945 }
Andres Rodriguez048765a2016-06-11 02:51:32 -04001946}
1947
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001948/**
1949 * amdgpu_device_init - initialize the driver
1950 *
1951 * @adev: amdgpu_device pointer
1952 * @pdev: drm dev pointer
1953 * @pdev: pci dev pointer
1954 * @flags: driver flags
1955 *
1956 * Initializes the driver info and hw (all asics).
1957 * Returns 0 for success or an error on failure.
1958 * Called at driver startup.
1959 */
1960int amdgpu_device_init(struct amdgpu_device *adev,
1961 struct drm_device *ddev,
1962 struct pci_dev *pdev,
1963 uint32_t flags)
1964{
1965 int r, i;
1966 bool runtime = false;
Marek Olšák95844d22016-08-17 23:49:27 +02001967 u32 max_MBps;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001968
1969 adev->shutdown = false;
1970 adev->dev = &pdev->dev;
1971 adev->ddev = ddev;
1972 adev->pdev = pdev;
1973 adev->flags = flags;
Jammy Zhou2f7d10b2015-07-22 11:29:01 +08001974 adev->asic_type = flags & AMD_ASIC_MASK;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001975 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
1976 adev->mc.gtt_size = 512 * 1024 * 1024;
1977 adev->accel_working = false;
1978 adev->num_rings = 0;
1979 adev->mman.buffer_funcs = NULL;
1980 adev->mman.buffer_funcs_ring = NULL;
1981 adev->vm_manager.vm_pte_funcs = NULL;
Christian König2d55e452016-02-08 17:37:38 +01001982 adev->vm_manager.vm_pte_num_rings = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001983 adev->gart.gart_funcs = NULL;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001984 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001985
1986 adev->smc_rreg = &amdgpu_invalid_rreg;
1987 adev->smc_wreg = &amdgpu_invalid_wreg;
1988 adev->pcie_rreg = &amdgpu_invalid_rreg;
1989 adev->pcie_wreg = &amdgpu_invalid_wreg;
Huang Rui36b9a952016-08-31 13:23:25 +08001990 adev->pciep_rreg = &amdgpu_invalid_rreg;
1991 adev->pciep_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001992 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
1993 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
1994 adev->didt_rreg = &amdgpu_invalid_rreg;
1995 adev->didt_wreg = &amdgpu_invalid_wreg;
Rex Zhuccdbb202016-06-08 12:47:41 +08001996 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
1997 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001998 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
1999 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
2000
Rex Zhuccdbb202016-06-08 12:47:41 +08002001
Alex Deucher3e39ab92015-06-05 15:04:33 -04002002 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
2003 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
2004 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002005
2006 /* mutex initialization are all done here so we
2007 * can recall function without having locking issues */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002008 atomic_set(&adev->irq.ih.lock, 0);
Huang Rui0e5ca0d2017-03-03 18:37:23 -05002009 mutex_init(&adev->firmware.mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002010 mutex_init(&adev->pm.mutex);
2011 mutex_init(&adev->gfx.gpu_clock_mutex);
2012 mutex_init(&adev->srbm_mutex);
2013 mutex_init(&adev->grbm_idx_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002014 mutex_init(&adev->mn_lock);
2015 hash_init(adev->mn_hash);
2016
2017 amdgpu_check_arguments(adev);
2018
2019 /* Registers mapping */
2020 /* TODO: block userspace mapping of io register */
2021 spin_lock_init(&adev->mmio_idx_lock);
2022 spin_lock_init(&adev->smc_idx_lock);
2023 spin_lock_init(&adev->pcie_idx_lock);
2024 spin_lock_init(&adev->uvd_ctx_idx_lock);
2025 spin_lock_init(&adev->didt_idx_lock);
Rex Zhuccdbb202016-06-08 12:47:41 +08002026 spin_lock_init(&adev->gc_cac_idx_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002027 spin_lock_init(&adev->audio_endpt_idx_lock);
Marek Olšák95844d22016-08-17 23:49:27 +02002028 spin_lock_init(&adev->mm_stats.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002029
Chunming Zhou0c4e7fa2016-08-17 11:41:30 +08002030 INIT_LIST_HEAD(&adev->shadow_list);
2031 mutex_init(&adev->shadow_list_lock);
2032
Chunming Zhou5c1354b2016-08-30 16:13:10 +08002033 INIT_LIST_HEAD(&adev->gtt_list);
2034 spin_lock_init(&adev->gtt_list_lock);
2035
Ken Wangda69c1612016-01-21 19:08:55 +08002036 if (adev->asic_type >= CHIP_BONAIRE) {
2037 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
2038 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
2039 } else {
2040 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
2041 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
2042 }
Chunming Zhou5c1354b2016-08-30 16:13:10 +08002043
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002044 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
2045 if (adev->rmmio == NULL) {
2046 return -ENOMEM;
2047 }
2048 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
2049 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
2050
Ken Wangda69c1612016-01-21 19:08:55 +08002051 if (adev->asic_type >= CHIP_BONAIRE)
2052 /* doorbell bar mapping */
2053 amdgpu_doorbell_init(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002054
2055 /* io port mapping */
2056 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
2057 if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
2058 adev->rio_mem_size = pci_resource_len(adev->pdev, i);
2059 adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
2060 break;
2061 }
2062 }
2063 if (adev->rio_mem == NULL)
Amber Linb64a18c2017-01-04 08:06:58 -05002064 DRM_INFO("PCI I/O BAR is not found.\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002065
2066 /* early init functions */
2067 r = amdgpu_early_init(adev);
2068 if (r)
2069 return r;
2070
2071 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
2072 /* this will fail for cards that aren't VGA class devices, just
2073 * ignore it */
2074 vga_client_register(adev->pdev, adev, NULL, amdgpu_vga_set_decode);
2075
2076 if (amdgpu_runtime_pm == 1)
2077 runtime = true;
Alex Deuchere9bef452016-04-25 13:12:18 -04002078 if (amdgpu_device_is_px(ddev))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002079 runtime = true;
Lukas Wunner84c8b222017-03-10 21:23:45 +01002080 if (!pci_is_thunderbolt_attached(adev->pdev))
2081 vga_switcheroo_register_client(adev->pdev,
2082 &amdgpu_switcheroo_ops, runtime);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002083 if (runtime)
2084 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
2085
2086 /* Read BIOS */
Alex Deucher83ba1262016-06-03 18:21:41 -04002087 if (!amdgpu_get_bios(adev)) {
2088 r = -EINVAL;
2089 goto failed;
2090 }
Nils Wallméniusf7e9e9f2016-12-14 21:52:45 +01002091
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002092 r = amdgpu_atombios_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002093 if (r) {
2094 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04002095 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002096 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002097
Monk Liu4e99a442016-03-31 13:26:59 +08002098 /* detect if we are with an SRIOV vbios */
2099 amdgpu_device_detect_sriov_bios(adev);
Andres Rodriguez048765a2016-06-11 02:51:32 -04002100
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002101 /* Post card if necessary */
Monk Liubec86372016-09-14 19:38:08 +08002102 if (amdgpu_vpost_needed(adev)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002103 if (!adev->bios) {
Monk Liubec86372016-09-14 19:38:08 +08002104 dev_err(adev->dev, "no vBIOS found\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04002105 r = -EINVAL;
2106 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002107 }
Monk Liubec86372016-09-14 19:38:08 +08002108 DRM_INFO("GPU posting now...\n");
Monk Liu4e99a442016-03-31 13:26:59 +08002109 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2110 if (r) {
2111 dev_err(adev->dev, "gpu post error!\n");
2112 goto failed;
2113 }
2114 } else {
2115 DRM_INFO("GPU post is not needed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002116 }
2117
Alex Deuchera5bde2f2016-09-23 16:23:41 -04002118 if (!adev->is_atom_fw) {
2119 /* Initialize clocks */
2120 r = amdgpu_atombios_get_clock_info(adev);
2121 if (r) {
2122 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
2123 return r;
2124 }
2125 /* init i2c buses */
2126 amdgpu_atombios_i2c_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002127 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002128
2129 /* Fence driver */
2130 r = amdgpu_fence_driver_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002131 if (r) {
2132 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04002133 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002134 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002135
2136 /* init the mode config */
2137 drm_mode_config_init(adev->ddev);
2138
2139 r = amdgpu_init(adev);
2140 if (r) {
Alex Deucher2c1a2782015-12-07 17:02:53 -05002141 dev_err(adev->dev, "amdgpu_init failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002142 amdgpu_fini(adev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002143 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002144 }
2145
2146 adev->accel_working = true;
2147
Marek Olšák95844d22016-08-17 23:49:27 +02002148 /* Initialize the buffer migration limit. */
2149 if (amdgpu_moverate >= 0)
2150 max_MBps = amdgpu_moverate;
2151 else
2152 max_MBps = 8; /* Allow 8 MB/s. */
2153 /* Get a log2 for easy divisions. */
2154 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
2155
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002156 r = amdgpu_ib_pool_init(adev);
2157 if (r) {
2158 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
Alex Deucher83ba1262016-06-03 18:21:41 -04002159 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002160 }
2161
2162 r = amdgpu_ib_ring_tests(adev);
2163 if (r)
2164 DRM_ERROR("ib ring test failed (%d).\n", r);
2165
Monk Liu9bc92b92017-02-08 17:38:13 +08002166 amdgpu_fbdev_init(adev);
2167
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002168 r = amdgpu_gem_debugfs_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002169 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002170 DRM_ERROR("registering gem debugfs failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002171
2172 r = amdgpu_debugfs_regs_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002173 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002174 DRM_ERROR("registering register debugfs failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002175
Huang Rui50ab2532016-06-12 15:51:09 +08002176 r = amdgpu_debugfs_firmware_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002177 if (r)
Huang Rui50ab2532016-06-12 15:51:09 +08002178 DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
Huang Rui50ab2532016-06-12 15:51:09 +08002179
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002180 if ((amdgpu_testing & 1)) {
2181 if (adev->accel_working)
2182 amdgpu_test_moves(adev);
2183 else
2184 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
2185 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002186 if (amdgpu_benchmarking) {
2187 if (adev->accel_working)
2188 amdgpu_benchmark(adev, amdgpu_benchmarking);
2189 else
2190 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
2191 }
2192
2193 /* enable clockgating, etc. after ib tests, etc. since some blocks require
2194 * explicit gating rather than handling it automatically.
2195 */
2196 r = amdgpu_late_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002197 if (r) {
2198 dev_err(adev->dev, "amdgpu_late_init failed\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04002199 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002200 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002201
2202 return 0;
Alex Deucher83ba1262016-06-03 18:21:41 -04002203
2204failed:
2205 if (runtime)
2206 vga_switcheroo_fini_domain_pm_ops(adev->dev);
2207 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002208}
2209
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002210/**
2211 * amdgpu_device_fini - tear down the driver
2212 *
2213 * @adev: amdgpu_device pointer
2214 *
2215 * Tear down the driver info (all asics).
2216 * Called at driver shutdown.
2217 */
2218void amdgpu_device_fini(struct amdgpu_device *adev)
2219{
2220 int r;
2221
2222 DRM_INFO("amdgpu: finishing device.\n");
2223 adev->shutdown = true;
Pixel Dingdb2c2a92017-04-25 16:47:42 +08002224 if (adev->mode_info.mode_config_initialized)
2225 drm_crtc_force_disable_all(adev->ddev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002226 /* evict vram memory */
2227 amdgpu_bo_evict_vram(adev);
2228 amdgpu_ib_pool_fini(adev);
2229 amdgpu_fence_driver_fini(adev);
2230 amdgpu_fbdev_fini(adev);
2231 r = amdgpu_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002232 adev->accel_working = false;
2233 /* free i2c buses */
2234 amdgpu_i2c_fini(adev);
2235 amdgpu_atombios_fini(adev);
2236 kfree(adev->bios);
2237 adev->bios = NULL;
Lukas Wunner84c8b222017-03-10 21:23:45 +01002238 if (!pci_is_thunderbolt_attached(adev->pdev))
2239 vga_switcheroo_unregister_client(adev->pdev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002240 if (adev->flags & AMD_IS_PX)
2241 vga_switcheroo_fini_domain_pm_ops(adev->dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002242 vga_client_register(adev->pdev, NULL, NULL, NULL);
2243 if (adev->rio_mem)
2244 pci_iounmap(adev->pdev, adev->rio_mem);
2245 adev->rio_mem = NULL;
2246 iounmap(adev->rmmio);
2247 adev->rmmio = NULL;
Ken Wangda69c1612016-01-21 19:08:55 +08002248 if (adev->asic_type >= CHIP_BONAIRE)
2249 amdgpu_doorbell_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002250 amdgpu_debugfs_regs_cleanup(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002251}
2252
2253
2254/*
2255 * Suspend & resume.
2256 */
2257/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002258 * amdgpu_device_suspend - initiate device suspend
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002259 *
2260 * @pdev: drm dev pointer
2261 * @state: suspend state
2262 *
2263 * Puts the hw in the suspend state (all asics).
2264 * Returns 0 for success or an error on failure.
2265 * Called at driver suspend.
2266 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002267int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002268{
2269 struct amdgpu_device *adev;
2270 struct drm_crtc *crtc;
2271 struct drm_connector *connector;
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002272 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002273
2274 if (dev == NULL || dev->dev_private == NULL) {
2275 return -ENODEV;
2276 }
2277
2278 adev = dev->dev_private;
2279
2280 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2281 return 0;
2282
2283 drm_kms_helper_poll_disable(dev);
2284
2285 /* turn off display hw */
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002286 drm_modeset_lock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002287 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2288 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
2289 }
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002290 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002291
Alex Deucher756e6882015-10-08 00:03:36 -04002292 /* unpin the front buffers and cursors */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002293 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Alex Deucher756e6882015-10-08 00:03:36 -04002294 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002295 struct amdgpu_framebuffer *rfb = to_amdgpu_framebuffer(crtc->primary->fb);
2296 struct amdgpu_bo *robj;
2297
Alex Deucher756e6882015-10-08 00:03:36 -04002298 if (amdgpu_crtc->cursor_bo) {
2299 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
Alex Xie7a6901d2017-04-24 13:52:41 -04002300 r = amdgpu_bo_reserve(aobj, true);
Alex Deucher756e6882015-10-08 00:03:36 -04002301 if (r == 0) {
2302 amdgpu_bo_unpin(aobj);
2303 amdgpu_bo_unreserve(aobj);
2304 }
2305 }
2306
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002307 if (rfb == NULL || rfb->obj == NULL) {
2308 continue;
2309 }
2310 robj = gem_to_amdgpu_bo(rfb->obj);
2311 /* don't unpin kernel fb objects */
2312 if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
Alex Xie7a6901d2017-04-24 13:52:41 -04002313 r = amdgpu_bo_reserve(robj, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002314 if (r == 0) {
2315 amdgpu_bo_unpin(robj);
2316 amdgpu_bo_unreserve(robj);
2317 }
2318 }
2319 }
2320 /* evict vram memory */
2321 amdgpu_bo_evict_vram(adev);
2322
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002323 amdgpu_fence_driver_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002324
2325 r = amdgpu_suspend(adev);
2326
Alex Deuchera0a71e42016-10-10 12:41:36 -04002327 /* evict remaining vram memory
2328 * This second call to evict vram is to evict the gart page table
2329 * using the CPU.
2330 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002331 amdgpu_bo_evict_vram(adev);
2332
Alex Deucherbe34d3b2017-03-03 14:26:51 -05002333 if (adev->is_atom_fw)
2334 amdgpu_atomfirmware_scratch_regs_save(adev);
2335 else
2336 amdgpu_atombios_scratch_regs_save(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002337 pci_save_state(dev->pdev);
2338 if (suspend) {
2339 /* Shut down the device */
2340 pci_disable_device(dev->pdev);
2341 pci_set_power_state(dev->pdev, PCI_D3hot);
jimqu74b0b152016-09-07 17:09:12 +08002342 } else {
2343 r = amdgpu_asic_reset(adev);
2344 if (r)
2345 DRM_ERROR("amdgpu asic reset failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002346 }
2347
2348 if (fbcon) {
2349 console_lock();
2350 amdgpu_fbdev_set_suspend(adev, 1);
2351 console_unlock();
2352 }
2353 return 0;
2354}
2355
2356/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002357 * amdgpu_device_resume - initiate device resume
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002358 *
2359 * @pdev: drm dev pointer
2360 *
2361 * Bring the hw back to operating state (all asics).
2362 * Returns 0 for success or an error on failure.
2363 * Called at driver resume.
2364 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002365int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002366{
2367 struct drm_connector *connector;
2368 struct amdgpu_device *adev = dev->dev_private;
Alex Deucher756e6882015-10-08 00:03:36 -04002369 struct drm_crtc *crtc;
Huang Rui03161a62017-04-13 16:12:26 +08002370 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002371
2372 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2373 return 0;
2374
jimqu74b0b152016-09-07 17:09:12 +08002375 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002376 console_lock();
jimqu74b0b152016-09-07 17:09:12 +08002377
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002378 if (resume) {
2379 pci_set_power_state(dev->pdev, PCI_D0);
2380 pci_restore_state(dev->pdev);
jimqu74b0b152016-09-07 17:09:12 +08002381 r = pci_enable_device(dev->pdev);
Huang Rui03161a62017-04-13 16:12:26 +08002382 if (r)
2383 goto unlock;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002384 }
Alex Deucherbe34d3b2017-03-03 14:26:51 -05002385 if (adev->is_atom_fw)
2386 amdgpu_atomfirmware_scratch_regs_restore(adev);
2387 else
2388 amdgpu_atombios_scratch_regs_restore(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002389
2390 /* post card */
Jim Quc836fec2017-02-10 15:59:59 +08002391 if (amdgpu_need_post(adev)) {
jimqu74b0b152016-09-07 17:09:12 +08002392 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2393 if (r)
2394 DRM_ERROR("amdgpu asic init failed\n");
2395 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002396
2397 r = amdgpu_resume(adev);
Rex Zhue6707212017-03-30 13:21:01 +08002398 if (r) {
Flora Cuica198522016-02-04 15:10:08 +08002399 DRM_ERROR("amdgpu_resume failed (%d).\n", r);
Huang Rui03161a62017-04-13 16:12:26 +08002400 goto unlock;
Rex Zhue6707212017-03-30 13:21:01 +08002401 }
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002402 amdgpu_fence_driver_resume(adev);
2403
Flora Cuica198522016-02-04 15:10:08 +08002404 if (resume) {
2405 r = amdgpu_ib_ring_tests(adev);
2406 if (r)
2407 DRM_ERROR("ib ring test failed (%d).\n", r);
2408 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002409
2410 r = amdgpu_late_init(adev);
Huang Rui03161a62017-04-13 16:12:26 +08002411 if (r)
2412 goto unlock;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002413
Alex Deucher756e6882015-10-08 00:03:36 -04002414 /* pin cursors */
2415 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2416 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2417
2418 if (amdgpu_crtc->cursor_bo) {
2419 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
Alex Xie7a6901d2017-04-24 13:52:41 -04002420 r = amdgpu_bo_reserve(aobj, true);
Alex Deucher756e6882015-10-08 00:03:36 -04002421 if (r == 0) {
2422 r = amdgpu_bo_pin(aobj,
2423 AMDGPU_GEM_DOMAIN_VRAM,
2424 &amdgpu_crtc->cursor_addr);
2425 if (r != 0)
2426 DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
2427 amdgpu_bo_unreserve(aobj);
2428 }
2429 }
2430 }
2431
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002432 /* blat the mode back in */
2433 if (fbcon) {
2434 drm_helper_resume_force_mode(dev);
2435 /* turn on display hw */
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002436 drm_modeset_lock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002437 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2438 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
2439 }
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002440 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002441 }
2442
2443 drm_kms_helper_poll_enable(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002444
2445 /*
2446 * Most of the connector probing functions try to acquire runtime pm
2447 * refs to ensure that the GPU is powered on when connector polling is
2448 * performed. Since we're calling this from a runtime PM callback,
2449 * trying to acquire rpm refs will cause us to deadlock.
2450 *
2451 * Since we're guaranteed to be holding the rpm lock, it's safe to
2452 * temporarily disable the rpm helpers so this doesn't deadlock us.
2453 */
2454#ifdef CONFIG_PM
2455 dev->dev->power.disable_depth++;
2456#endif
Alex Deucher54fb2a52015-11-24 14:30:56 -05002457 drm_helper_hpd_irq_event(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002458#ifdef CONFIG_PM
2459 dev->dev->power.disable_depth--;
2460#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002461
Huang Rui03161a62017-04-13 16:12:26 +08002462 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002463 amdgpu_fbdev_set_suspend(adev, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002464
Huang Rui03161a62017-04-13 16:12:26 +08002465unlock:
2466 if (fbcon)
2467 console_unlock();
2468
2469 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002470}
2471
Chunming Zhou63fbf422016-07-15 11:19:20 +08002472static bool amdgpu_check_soft_reset(struct amdgpu_device *adev)
2473{
2474 int i;
2475 bool asic_hang = false;
2476
2477 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002478 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou63fbf422016-07-15 11:19:20 +08002479 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002480 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
2481 adev->ip_blocks[i].status.hang =
2482 adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
2483 if (adev->ip_blocks[i].status.hang) {
2484 DRM_INFO("IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
Chunming Zhou63fbf422016-07-15 11:19:20 +08002485 asic_hang = true;
2486 }
2487 }
2488 return asic_hang;
2489}
2490
Baoyou Xie4d446652016-09-18 22:09:35 +08002491static int amdgpu_pre_soft_reset(struct amdgpu_device *adev)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002492{
2493 int i, r = 0;
2494
2495 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002496 if (!adev->ip_blocks[i].status.valid)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002497 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002498 if (adev->ip_blocks[i].status.hang &&
2499 adev->ip_blocks[i].version->funcs->pre_soft_reset) {
2500 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
Chunming Zhoud31a5012016-07-18 10:04:34 +08002501 if (r)
2502 return r;
2503 }
2504 }
2505
2506 return 0;
2507}
2508
Chunming Zhou35d782f2016-07-15 15:57:13 +08002509static bool amdgpu_need_full_reset(struct amdgpu_device *adev)
2510{
Alex Deucherda146d32016-10-13 16:07:03 -04002511 int i;
2512
2513 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002514 if (!adev->ip_blocks[i].status.valid)
Alex Deucherda146d32016-10-13 16:07:03 -04002515 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002516 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
2517 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
2518 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
2519 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE)) {
2520 if (adev->ip_blocks[i].status.hang) {
Alex Deucherda146d32016-10-13 16:07:03 -04002521 DRM_INFO("Some block need full reset!\n");
2522 return true;
2523 }
2524 }
Chunming Zhou35d782f2016-07-15 15:57:13 +08002525 }
2526 return false;
2527}
2528
2529static int amdgpu_soft_reset(struct amdgpu_device *adev)
2530{
2531 int i, r = 0;
2532
2533 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002534 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002535 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002536 if (adev->ip_blocks[i].status.hang &&
2537 adev->ip_blocks[i].version->funcs->soft_reset) {
2538 r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002539 if (r)
2540 return r;
2541 }
2542 }
2543
2544 return 0;
2545}
2546
2547static int amdgpu_post_soft_reset(struct amdgpu_device *adev)
2548{
2549 int i, r = 0;
2550
2551 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002552 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002553 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002554 if (adev->ip_blocks[i].status.hang &&
2555 adev->ip_blocks[i].version->funcs->post_soft_reset)
2556 r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002557 if (r)
2558 return r;
2559 }
2560
2561 return 0;
2562}
2563
Chunming Zhou3ad81f12016-08-05 17:30:17 +08002564bool amdgpu_need_backup(struct amdgpu_device *adev)
2565{
2566 if (adev->flags & AMD_IS_APU)
2567 return false;
2568
2569 return amdgpu_lockup_timeout > 0 ? true : false;
2570}
2571
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002572static int amdgpu_recover_vram_from_shadow(struct amdgpu_device *adev,
2573 struct amdgpu_ring *ring,
2574 struct amdgpu_bo *bo,
Chris Wilsonf54d1862016-10-25 13:00:45 +01002575 struct dma_fence **fence)
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002576{
2577 uint32_t domain;
2578 int r;
2579
Roger.He23d2e502017-04-21 14:24:26 +08002580 if (!bo->shadow)
2581 return 0;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002582
Alex Xie1d284792017-04-24 13:53:04 -04002583 r = amdgpu_bo_reserve(bo, true);
Roger.He23d2e502017-04-21 14:24:26 +08002584 if (r)
2585 return r;
2586 domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
2587 /* if bo has been evicted, then no need to recover */
2588 if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
Roger.He82521312017-04-21 13:08:43 +08002589 r = amdgpu_bo_validate(bo->shadow);
2590 if (r) {
2591 DRM_ERROR("bo validate failed!\n");
2592 goto err;
2593 }
2594
2595 r = amdgpu_ttm_bind(&bo->shadow->tbo, &bo->shadow->tbo.mem);
2596 if (r) {
2597 DRM_ERROR("%p bind failed\n", bo->shadow);
2598 goto err;
2599 }
2600
Roger.He23d2e502017-04-21 14:24:26 +08002601 r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002602 NULL, fence, true);
Roger.He23d2e502017-04-21 14:24:26 +08002603 if (r) {
2604 DRM_ERROR("recover page table failed!\n");
2605 goto err;
2606 }
2607 }
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002608err:
Roger.He23d2e502017-04-21 14:24:26 +08002609 amdgpu_bo_unreserve(bo);
2610 return r;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002611}
2612
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002613/**
Monk Liua90ad3c2017-01-23 14:22:08 +08002614 * amdgpu_sriov_gpu_reset - reset the asic
2615 *
2616 * @adev: amdgpu device pointer
Monk Liu7225f872017-04-26 14:51:54 +08002617 * @job: which job trigger hang
Monk Liua90ad3c2017-01-23 14:22:08 +08002618 *
2619 * Attempt the reset the GPU if it has hung (all asics).
2620 * for SRIOV case.
2621 * Returns 0 for success or an error on failure.
2622 */
Monk Liu7225f872017-04-26 14:51:54 +08002623int amdgpu_sriov_gpu_reset(struct amdgpu_device *adev, struct amdgpu_job *job)
Monk Liua90ad3c2017-01-23 14:22:08 +08002624{
Monk Liu65781c72017-05-11 13:36:44 +08002625 int i, j, r = 0;
Monk Liua90ad3c2017-01-23 14:22:08 +08002626 int resched;
2627 struct amdgpu_bo *bo, *tmp;
2628 struct amdgpu_ring *ring;
2629 struct dma_fence *fence = NULL, *next = NULL;
2630
Monk Liu147b5982017-01-25 15:48:01 +08002631 mutex_lock(&adev->virt.lock_reset);
Monk Liua90ad3c2017-01-23 14:22:08 +08002632 atomic_inc(&adev->gpu_reset_counter);
Monk Liu1fb37a32017-01-26 15:36:37 +08002633 adev->gfx.in_reset = true;
Monk Liua90ad3c2017-01-23 14:22:08 +08002634
2635 /* block TTM */
2636 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
2637
Monk Liu65781c72017-05-11 13:36:44 +08002638 /* we start from the ring trigger GPU hang */
2639 j = job ? job->ring->idx : 0;
Monk Liua90ad3c2017-01-23 14:22:08 +08002640
Monk Liu65781c72017-05-11 13:36:44 +08002641 /* block scheduler */
2642 for (i = j; i < j + AMDGPU_MAX_RINGS; ++i) {
2643 ring = adev->rings[i % AMDGPU_MAX_RINGS];
Monk Liua90ad3c2017-01-23 14:22:08 +08002644 if (!ring || !ring->sched.thread)
2645 continue;
2646
2647 kthread_park(ring->sched.thread);
Monk Liua90ad3c2017-01-23 14:22:08 +08002648
Monk Liu65781c72017-05-11 13:36:44 +08002649 if (job && j != i)
2650 continue;
2651
Monk Liu4f059ec2017-05-11 13:59:15 +08002652 /* here give the last chance to check if job removed from mirror-list
Monk Liu65781c72017-05-11 13:36:44 +08002653 * since we already pay some time on kthread_park */
Monk Liu4f059ec2017-05-11 13:59:15 +08002654 if (job && list_empty(&job->base.node)) {
Monk Liu65781c72017-05-11 13:36:44 +08002655 kthread_unpark(ring->sched.thread);
2656 goto give_up_reset;
2657 }
2658
2659 if (amd_sched_invalidate_job(&job->base, amdgpu_job_hang_limit))
2660 amd_sched_job_kickout(&job->base);
2661
2662 /* only do job_reset on the hang ring if @job not NULL */
2663 amd_sched_hw_job_reset(&ring->sched);
2664
2665 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
2666 amdgpu_fence_driver_force_completion_ring(ring);
2667 }
Monk Liua90ad3c2017-01-23 14:22:08 +08002668
2669 /* request to take full control of GPU before re-initialization */
Monk Liu7225f872017-04-26 14:51:54 +08002670 if (job)
Monk Liua90ad3c2017-01-23 14:22:08 +08002671 amdgpu_virt_reset_gpu(adev);
2672 else
2673 amdgpu_virt_request_full_gpu(adev, true);
2674
2675
2676 /* Resume IP prior to SMC */
Monk Liue4f0fdc2017-02-09 11:55:49 +08002677 amdgpu_sriov_reinit_early(adev);
Monk Liua90ad3c2017-01-23 14:22:08 +08002678
2679 /* we need recover gart prior to run SMC/CP/SDMA resume */
2680 amdgpu_ttm_recover_gart(adev);
2681
2682 /* now we are okay to resume SMC/CP/SDMA */
Monk Liue4f0fdc2017-02-09 11:55:49 +08002683 amdgpu_sriov_reinit_late(adev);
Monk Liua90ad3c2017-01-23 14:22:08 +08002684
2685 amdgpu_irq_gpu_reset_resume_helper(adev);
2686
2687 if (amdgpu_ib_ring_tests(adev))
2688 dev_err(adev->dev, "[GPU_RESET] ib ring test failed (%d).\n", r);
2689
2690 /* release full control of GPU after ib test */
2691 amdgpu_virt_release_full_gpu(adev, true);
2692
2693 DRM_INFO("recover vram bo from shadow\n");
2694
2695 ring = adev->mman.buffer_funcs_ring;
2696 mutex_lock(&adev->shadow_list_lock);
2697 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
Monk Liu236763d2017-05-01 16:15:31 +08002698 next = NULL;
Monk Liua90ad3c2017-01-23 14:22:08 +08002699 amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
2700 if (fence) {
2701 r = dma_fence_wait(fence, false);
2702 if (r) {
2703 WARN(r, "recovery from shadow isn't completed\n");
2704 break;
2705 }
2706 }
2707
2708 dma_fence_put(fence);
2709 fence = next;
2710 }
2711 mutex_unlock(&adev->shadow_list_lock);
2712
2713 if (fence) {
2714 r = dma_fence_wait(fence, false);
2715 if (r)
2716 WARN(r, "recovery from shadow isn't completed\n");
2717 }
2718 dma_fence_put(fence);
2719
Monk Liu65781c72017-05-11 13:36:44 +08002720 for (i = j; i < j + AMDGPU_MAX_RINGS; ++i) {
2721 ring = adev->rings[i % AMDGPU_MAX_RINGS];
Monk Liua90ad3c2017-01-23 14:22:08 +08002722 if (!ring || !ring->sched.thread)
2723 continue;
2724
Monk Liu65781c72017-05-11 13:36:44 +08002725 if (job && j != i) {
2726 kthread_unpark(ring->sched.thread);
2727 continue;
2728 }
2729
Monk Liua90ad3c2017-01-23 14:22:08 +08002730 amd_sched_job_recovery(&ring->sched);
2731 kthread_unpark(ring->sched.thread);
2732 }
2733
2734 drm_helper_resume_force_mode(adev->ddev);
Monk Liu65781c72017-05-11 13:36:44 +08002735give_up_reset:
Monk Liua90ad3c2017-01-23 14:22:08 +08002736 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
2737 if (r) {
2738 /* bad news, how to tell it to userspace ? */
2739 dev_info(adev->dev, "GPU reset failed\n");
Monk Liu65781c72017-05-11 13:36:44 +08002740 } else {
2741 dev_info(adev->dev, "GPU reset successed!\n");
Monk Liua90ad3c2017-01-23 14:22:08 +08002742 }
2743
Monk Liu1fb37a32017-01-26 15:36:37 +08002744 adev->gfx.in_reset = false;
Monk Liu147b5982017-01-25 15:48:01 +08002745 mutex_unlock(&adev->virt.lock_reset);
Monk Liua90ad3c2017-01-23 14:22:08 +08002746 return r;
2747}
2748
2749/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002750 * amdgpu_gpu_reset - reset the asic
2751 *
2752 * @adev: amdgpu device pointer
2753 *
2754 * Attempt the reset the GPU if it has hung (all asics).
2755 * Returns 0 for success or an error on failure.
2756 */
2757int amdgpu_gpu_reset(struct amdgpu_device *adev)
2758{
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002759 int i, r;
2760 int resched;
Chunming Zhou35d782f2016-07-15 15:57:13 +08002761 bool need_full_reset;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002762
Chunming Zhou63fbf422016-07-15 11:19:20 +08002763 if (!amdgpu_check_soft_reset(adev)) {
2764 DRM_INFO("No hardware hang detected. Did some blocks stall?\n");
2765 return 0;
2766 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002767
Marek Olšákd94aed52015-05-05 21:13:49 +02002768 atomic_inc(&adev->gpu_reset_counter);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002769
Chunming Zhoua3c47d62016-06-30 16:44:41 +08002770 /* block TTM */
2771 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
2772
Chunming Zhou0875dc92016-06-12 15:41:58 +08002773 /* block scheduler */
2774 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2775 struct amdgpu_ring *ring = adev->rings[i];
2776
Chunming Zhou51687752017-04-24 17:09:15 +08002777 if (!ring || !ring->sched.thread)
Chunming Zhou0875dc92016-06-12 15:41:58 +08002778 continue;
2779 kthread_park(ring->sched.thread);
Chunming Zhouaa1c8902016-06-30 13:56:02 +08002780 amd_sched_hw_job_reset(&ring->sched);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002781 }
Chunming Zhou2200eda2016-06-30 16:53:02 +08002782 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
2783 amdgpu_fence_driver_force_completion(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002784
Chunming Zhou35d782f2016-07-15 15:57:13 +08002785 need_full_reset = amdgpu_need_full_reset(adev);
2786
2787 if (!need_full_reset) {
2788 amdgpu_pre_soft_reset(adev);
2789 r = amdgpu_soft_reset(adev);
2790 amdgpu_post_soft_reset(adev);
2791 if (r || amdgpu_check_soft_reset(adev)) {
2792 DRM_INFO("soft reset failed, will fallback to full reset!\n");
2793 need_full_reset = true;
2794 }
2795 }
2796
2797 if (need_full_reset) {
Chunming Zhou35d782f2016-07-15 15:57:13 +08002798 r = amdgpu_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002799
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002800retry:
Chunming Zhou35d782f2016-07-15 15:57:13 +08002801 /* Disable fb access */
2802 if (adev->mode_info.num_crtc) {
2803 struct amdgpu_mode_mc_save save;
2804 amdgpu_display_stop_mc_access(adev, &save);
2805 amdgpu_wait_for_idle(adev, AMD_IP_BLOCK_TYPE_GMC);
2806 }
Alex Deucherbe34d3b2017-03-03 14:26:51 -05002807 if (adev->is_atom_fw)
2808 amdgpu_atomfirmware_scratch_regs_save(adev);
2809 else
2810 amdgpu_atombios_scratch_regs_save(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002811 r = amdgpu_asic_reset(adev);
Alex Deucherbe34d3b2017-03-03 14:26:51 -05002812 if (adev->is_atom_fw)
2813 amdgpu_atomfirmware_scratch_regs_restore(adev);
2814 else
2815 amdgpu_atombios_scratch_regs_restore(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002816 /* post card */
2817 amdgpu_atom_asic_init(adev->mode_info.atom_context);
Alex Deucherbfa99262016-01-15 11:59:48 -05002818
Chunming Zhou35d782f2016-07-15 15:57:13 +08002819 if (!r) {
2820 dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
Chunming Zhoufcf06492017-05-05 10:33:33 +08002821 r = amdgpu_resume_phase1(adev);
2822 if (r)
2823 goto out;
Chunming Zhou2c0d7312016-08-30 16:36:25 +08002824 r = amdgpu_ttm_recover_gart(adev);
2825 if (r)
Chunming Zhoufcf06492017-05-05 10:33:33 +08002826 goto out;
2827 r = amdgpu_resume_phase2(adev);
2828 if (r)
2829 goto out;
Chunming Zhou2c0d7312016-08-30 16:36:25 +08002830 }
Chunming Zhoufcf06492017-05-05 10:33:33 +08002831 }
2832out:
2833 if (!r) {
2834 amdgpu_irq_gpu_reset_resume_helper(adev);
Chunming Zhou1f465082016-06-30 15:02:26 +08002835 r = amdgpu_ib_ring_tests(adev);
2836 if (r) {
2837 dev_err(adev->dev, "ib ring test failed (%d).\n", r);
Chunming Zhou40019dc2016-06-29 16:01:49 +08002838 r = amdgpu_suspend(adev);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002839 need_full_reset = true;
Chunming Zhou40019dc2016-06-29 16:01:49 +08002840 goto retry;
Chunming Zhou1f465082016-06-30 15:02:26 +08002841 }
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002842 /**
2843 * recovery vm page tables, since we cannot depend on VRAM is
2844 * consistent after gpu full reset.
2845 */
2846 if (need_full_reset && amdgpu_need_backup(adev)) {
2847 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
2848 struct amdgpu_bo *bo, *tmp;
Chris Wilsonf54d1862016-10-25 13:00:45 +01002849 struct dma_fence *fence = NULL, *next = NULL;
Chunming Zhou1f465082016-06-30 15:02:26 +08002850
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002851 DRM_INFO("recover vram bo from shadow\n");
2852 mutex_lock(&adev->shadow_list_lock);
2853 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
Monk Liu236763d2017-05-01 16:15:31 +08002854 next = NULL;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002855 amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
2856 if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01002857 r = dma_fence_wait(fence, false);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002858 if (r) {
Monk Liu1d7b17b2017-01-22 18:52:56 +08002859 WARN(r, "recovery from shadow isn't completed\n");
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002860 break;
2861 }
2862 }
2863
Chris Wilsonf54d1862016-10-25 13:00:45 +01002864 dma_fence_put(fence);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002865 fence = next;
2866 }
2867 mutex_unlock(&adev->shadow_list_lock);
2868 if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01002869 r = dma_fence_wait(fence, false);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002870 if (r)
Monk Liu1d7b17b2017-01-22 18:52:56 +08002871 WARN(r, "recovery from shadow isn't completed\n");
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002872 }
Chris Wilsonf54d1862016-10-25 13:00:45 +01002873 dma_fence_put(fence);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002874 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002875 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2876 struct amdgpu_ring *ring = adev->rings[i];
Chunming Zhou51687752017-04-24 17:09:15 +08002877
2878 if (!ring || !ring->sched.thread)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002879 continue;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002880
Chunming Zhouaa1c8902016-06-30 13:56:02 +08002881 amd_sched_job_recovery(&ring->sched);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002882 kthread_unpark(ring->sched.thread);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002883 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002884 } else {
Chunming Zhou2200eda2016-06-30 16:53:02 +08002885 dev_err(adev->dev, "asic resume failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002886 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
Chunming Zhou51687752017-04-24 17:09:15 +08002887 if (adev->rings[i] && adev->rings[i]->sched.thread) {
Chunming Zhou0875dc92016-06-12 15:41:58 +08002888 kthread_unpark(adev->rings[i]->sched.thread);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002889 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002890 }
2891 }
2892
2893 drm_helper_resume_force_mode(adev->ddev);
2894
2895 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
Chunming Zhou6643be62017-05-05 10:50:09 +08002896 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002897 /* bad news, how to tell it to userspace ? */
2898 dev_info(adev->dev, "GPU reset failed\n");
Chunming Zhou6643be62017-05-05 10:50:09 +08002899 else
2900 dev_info(adev->dev, "GPU reset successed!\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002901
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002902 return r;
2903}
2904
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002905void amdgpu_get_pcie_info(struct amdgpu_device *adev)
2906{
2907 u32 mask;
2908 int ret;
2909
Alex Deuchercd474ba2016-02-04 10:21:23 -05002910 if (amdgpu_pcie_gen_cap)
2911 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
2912
2913 if (amdgpu_pcie_lane_cap)
2914 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
2915
2916 /* covers APUs as well */
2917 if (pci_is_root_bus(adev->pdev->bus)) {
2918 if (adev->pm.pcie_gen_mask == 0)
2919 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
2920 if (adev->pm.pcie_mlw_mask == 0)
2921 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002922 return;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002923 }
Alex Deuchercd474ba2016-02-04 10:21:23 -05002924
2925 if (adev->pm.pcie_gen_mask == 0) {
2926 ret = drm_pcie_get_speed_cap_mask(adev->ddev, &mask);
2927 if (!ret) {
2928 adev->pm.pcie_gen_mask = (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
2929 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
2930 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
2931
2932 if (mask & DRM_PCIE_SPEED_25)
2933 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
2934 if (mask & DRM_PCIE_SPEED_50)
2935 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2;
2936 if (mask & DRM_PCIE_SPEED_80)
2937 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3;
2938 } else {
2939 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
2940 }
2941 }
2942 if (adev->pm.pcie_mlw_mask == 0) {
2943 ret = drm_pcie_get_max_link_width(adev->ddev, &mask);
2944 if (!ret) {
2945 switch (mask) {
2946 case 32:
2947 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
2948 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
2949 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2950 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2951 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2952 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2953 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2954 break;
2955 case 16:
2956 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
2957 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2958 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2959 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2960 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2961 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2962 break;
2963 case 12:
2964 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2965 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2966 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2967 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2968 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2969 break;
2970 case 8:
2971 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2972 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2973 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2974 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2975 break;
2976 case 4:
2977 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2978 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2979 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2980 break;
2981 case 2:
2982 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2983 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2984 break;
2985 case 1:
2986 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
2987 break;
2988 default:
2989 break;
2990 }
2991 } else {
2992 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002993 }
2994 }
2995}
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002996
2997/*
2998 * Debugfs
2999 */
3000int amdgpu_debugfs_add_files(struct amdgpu_device *adev,
Nils Wallménius06ab6832016-05-02 12:46:15 -04003001 const struct drm_info_list *files,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003002 unsigned nfiles)
3003{
3004 unsigned i;
3005
3006 for (i = 0; i < adev->debugfs_count; i++) {
3007 if (adev->debugfs[i].files == files) {
3008 /* Already registered */
3009 return 0;
3010 }
3011 }
3012
3013 i = adev->debugfs_count + 1;
3014 if (i > AMDGPU_DEBUGFS_MAX_COMPONENTS) {
3015 DRM_ERROR("Reached maximum number of debugfs components.\n");
3016 DRM_ERROR("Report so we increase "
3017 "AMDGPU_DEBUGFS_MAX_COMPONENTS.\n");
3018 return -EINVAL;
3019 }
3020 adev->debugfs[adev->debugfs_count].files = files;
3021 adev->debugfs[adev->debugfs_count].num_files = nfiles;
3022 adev->debugfs_count = i;
3023#if defined(CONFIG_DEBUG_FS)
3024 drm_debugfs_create_files(files, nfiles,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003025 adev->ddev->primary->debugfs_root,
3026 adev->ddev->primary);
3027#endif
3028 return 0;
3029}
3030
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003031#if defined(CONFIG_DEBUG_FS)
3032
3033static ssize_t amdgpu_debugfs_regs_read(struct file *f, char __user *buf,
3034 size_t size, loff_t *pos)
3035{
Al Viro45063092016-12-04 18:24:56 -05003036 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003037 ssize_t result = 0;
3038 int r;
Tom St Denisbd122672016-07-28 09:39:22 -04003039 bool pm_pg_lock, use_bank;
Tom St Denis566281592016-06-27 11:55:07 -04003040 unsigned instance_bank, sh_bank, se_bank;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003041
3042 if (size & 0x3 || *pos & 0x3)
3043 return -EINVAL;
3044
Tom St Denisbd122672016-07-28 09:39:22 -04003045 /* are we reading registers for which a PG lock is necessary? */
3046 pm_pg_lock = (*pos >> 23) & 1;
3047
Tom St Denis566281592016-06-27 11:55:07 -04003048 if (*pos & (1ULL << 62)) {
3049 se_bank = (*pos >> 24) & 0x3FF;
3050 sh_bank = (*pos >> 34) & 0x3FF;
3051 instance_bank = (*pos >> 44) & 0x3FF;
Tom St Denis32977f92016-10-09 07:41:26 -04003052
3053 if (se_bank == 0x3FF)
3054 se_bank = 0xFFFFFFFF;
3055 if (sh_bank == 0x3FF)
3056 sh_bank = 0xFFFFFFFF;
3057 if (instance_bank == 0x3FF)
3058 instance_bank = 0xFFFFFFFF;
Tom St Denis566281592016-06-27 11:55:07 -04003059 use_bank = 1;
Tom St Denis566281592016-06-27 11:55:07 -04003060 } else {
3061 use_bank = 0;
3062 }
3063
Tom St Denis801a6aa9a62017-03-15 05:34:25 -04003064 *pos &= (1UL << 22) - 1;
Tom St Denisbd122672016-07-28 09:39:22 -04003065
Tom St Denis566281592016-06-27 11:55:07 -04003066 if (use_bank) {
Tom St Denis32977f92016-10-09 07:41:26 -04003067 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
3068 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
Tom St Denis566281592016-06-27 11:55:07 -04003069 return -EINVAL;
3070 mutex_lock(&adev->grbm_idx_mutex);
3071 amdgpu_gfx_select_se_sh(adev, se_bank,
3072 sh_bank, instance_bank);
3073 }
3074
Tom St Denisbd122672016-07-28 09:39:22 -04003075 if (pm_pg_lock)
3076 mutex_lock(&adev->pm.mutex);
3077
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003078 while (size) {
3079 uint32_t value;
3080
3081 if (*pos > adev->rmmio_size)
Tom St Denis566281592016-06-27 11:55:07 -04003082 goto end;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003083
3084 value = RREG32(*pos >> 2);
3085 r = put_user(value, (uint32_t *)buf);
Tom St Denis566281592016-06-27 11:55:07 -04003086 if (r) {
3087 result = r;
3088 goto end;
3089 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003090
3091 result += 4;
3092 buf += 4;
3093 *pos += 4;
3094 size -= 4;
3095 }
3096
Tom St Denis566281592016-06-27 11:55:07 -04003097end:
3098 if (use_bank) {
3099 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
3100 mutex_unlock(&adev->grbm_idx_mutex);
3101 }
3102
Tom St Denisbd122672016-07-28 09:39:22 -04003103 if (pm_pg_lock)
3104 mutex_unlock(&adev->pm.mutex);
3105
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003106 return result;
3107}
3108
3109static ssize_t amdgpu_debugfs_regs_write(struct file *f, const char __user *buf,
3110 size_t size, loff_t *pos)
3111{
Al Viro45063092016-12-04 18:24:56 -05003112 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003113 ssize_t result = 0;
3114 int r;
Tom St Denis394fdde2016-10-10 07:31:23 -04003115 bool pm_pg_lock, use_bank;
3116 unsigned instance_bank, sh_bank, se_bank;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003117
3118 if (size & 0x3 || *pos & 0x3)
3119 return -EINVAL;
3120
Tom St Denis394fdde2016-10-10 07:31:23 -04003121 /* are we reading registers for which a PG lock is necessary? */
3122 pm_pg_lock = (*pos >> 23) & 1;
3123
3124 if (*pos & (1ULL << 62)) {
3125 se_bank = (*pos >> 24) & 0x3FF;
3126 sh_bank = (*pos >> 34) & 0x3FF;
3127 instance_bank = (*pos >> 44) & 0x3FF;
3128
3129 if (se_bank == 0x3FF)
3130 se_bank = 0xFFFFFFFF;
3131 if (sh_bank == 0x3FF)
3132 sh_bank = 0xFFFFFFFF;
3133 if (instance_bank == 0x3FF)
3134 instance_bank = 0xFFFFFFFF;
3135 use_bank = 1;
3136 } else {
3137 use_bank = 0;
3138 }
3139
Tom St Denis801a6aa9a62017-03-15 05:34:25 -04003140 *pos &= (1UL << 22) - 1;
Tom St Denis394fdde2016-10-10 07:31:23 -04003141
3142 if (use_bank) {
3143 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
3144 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
3145 return -EINVAL;
3146 mutex_lock(&adev->grbm_idx_mutex);
3147 amdgpu_gfx_select_se_sh(adev, se_bank,
3148 sh_bank, instance_bank);
3149 }
3150
3151 if (pm_pg_lock)
3152 mutex_lock(&adev->pm.mutex);
3153
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003154 while (size) {
3155 uint32_t value;
3156
3157 if (*pos > adev->rmmio_size)
3158 return result;
3159
3160 r = get_user(value, (uint32_t *)buf);
3161 if (r)
3162 return r;
3163
3164 WREG32(*pos >> 2, value);
3165
3166 result += 4;
3167 buf += 4;
3168 *pos += 4;
3169 size -= 4;
3170 }
3171
Tom St Denis394fdde2016-10-10 07:31:23 -04003172 if (use_bank) {
3173 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
3174 mutex_unlock(&adev->grbm_idx_mutex);
3175 }
3176
3177 if (pm_pg_lock)
3178 mutex_unlock(&adev->pm.mutex);
3179
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003180 return result;
3181}
3182
Tom St Denisadcec282016-04-15 13:08:44 -04003183static ssize_t amdgpu_debugfs_regs_pcie_read(struct file *f, char __user *buf,
3184 size_t size, loff_t *pos)
3185{
Al Viro45063092016-12-04 18:24:56 -05003186 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003187 ssize_t result = 0;
3188 int r;
3189
3190 if (size & 0x3 || *pos & 0x3)
3191 return -EINVAL;
3192
3193 while (size) {
3194 uint32_t value;
3195
3196 value = RREG32_PCIE(*pos >> 2);
3197 r = put_user(value, (uint32_t *)buf);
3198 if (r)
3199 return r;
3200
3201 result += 4;
3202 buf += 4;
3203 *pos += 4;
3204 size -= 4;
3205 }
3206
3207 return result;
3208}
3209
3210static ssize_t amdgpu_debugfs_regs_pcie_write(struct file *f, const char __user *buf,
3211 size_t size, loff_t *pos)
3212{
Al Viro45063092016-12-04 18:24:56 -05003213 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003214 ssize_t result = 0;
3215 int r;
3216
3217 if (size & 0x3 || *pos & 0x3)
3218 return -EINVAL;
3219
3220 while (size) {
3221 uint32_t value;
3222
3223 r = get_user(value, (uint32_t *)buf);
3224 if (r)
3225 return r;
3226
3227 WREG32_PCIE(*pos >> 2, value);
3228
3229 result += 4;
3230 buf += 4;
3231 *pos += 4;
3232 size -= 4;
3233 }
3234
3235 return result;
3236}
3237
3238static ssize_t amdgpu_debugfs_regs_didt_read(struct file *f, char __user *buf,
3239 size_t size, loff_t *pos)
3240{
Al Viro45063092016-12-04 18:24:56 -05003241 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003242 ssize_t result = 0;
3243 int r;
3244
3245 if (size & 0x3 || *pos & 0x3)
3246 return -EINVAL;
3247
3248 while (size) {
3249 uint32_t value;
3250
3251 value = RREG32_DIDT(*pos >> 2);
3252 r = put_user(value, (uint32_t *)buf);
3253 if (r)
3254 return r;
3255
3256 result += 4;
3257 buf += 4;
3258 *pos += 4;
3259 size -= 4;
3260 }
3261
3262 return result;
3263}
3264
3265static ssize_t amdgpu_debugfs_regs_didt_write(struct file *f, const char __user *buf,
3266 size_t size, loff_t *pos)
3267{
Al Viro45063092016-12-04 18:24:56 -05003268 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003269 ssize_t result = 0;
3270 int r;
3271
3272 if (size & 0x3 || *pos & 0x3)
3273 return -EINVAL;
3274
3275 while (size) {
3276 uint32_t value;
3277
3278 r = get_user(value, (uint32_t *)buf);
3279 if (r)
3280 return r;
3281
3282 WREG32_DIDT(*pos >> 2, value);
3283
3284 result += 4;
3285 buf += 4;
3286 *pos += 4;
3287 size -= 4;
3288 }
3289
3290 return result;
3291}
3292
3293static ssize_t amdgpu_debugfs_regs_smc_read(struct file *f, char __user *buf,
3294 size_t size, loff_t *pos)
3295{
Al Viro45063092016-12-04 18:24:56 -05003296 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003297 ssize_t result = 0;
3298 int r;
3299
3300 if (size & 0x3 || *pos & 0x3)
3301 return -EINVAL;
3302
3303 while (size) {
3304 uint32_t value;
3305
Tom St Denis6fc0dea2016-08-29 08:39:29 -04003306 value = RREG32_SMC(*pos);
Tom St Denisadcec282016-04-15 13:08:44 -04003307 r = put_user(value, (uint32_t *)buf);
3308 if (r)
3309 return r;
3310
3311 result += 4;
3312 buf += 4;
3313 *pos += 4;
3314 size -= 4;
3315 }
3316
3317 return result;
3318}
3319
3320static ssize_t amdgpu_debugfs_regs_smc_write(struct file *f, const char __user *buf,
3321 size_t size, loff_t *pos)
3322{
Al Viro45063092016-12-04 18:24:56 -05003323 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003324 ssize_t result = 0;
3325 int r;
3326
3327 if (size & 0x3 || *pos & 0x3)
3328 return -EINVAL;
3329
3330 while (size) {
3331 uint32_t value;
3332
3333 r = get_user(value, (uint32_t *)buf);
3334 if (r)
3335 return r;
3336
Tom St Denis6fc0dea2016-08-29 08:39:29 -04003337 WREG32_SMC(*pos, value);
Tom St Denisadcec282016-04-15 13:08:44 -04003338
3339 result += 4;
3340 buf += 4;
3341 *pos += 4;
3342 size -= 4;
3343 }
3344
3345 return result;
3346}
3347
Tom St Denis1e051412016-06-27 09:57:18 -04003348static ssize_t amdgpu_debugfs_gca_config_read(struct file *f, char __user *buf,
3349 size_t size, loff_t *pos)
3350{
Al Viro45063092016-12-04 18:24:56 -05003351 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denis1e051412016-06-27 09:57:18 -04003352 ssize_t result = 0;
3353 int r;
3354 uint32_t *config, no_regs = 0;
3355
3356 if (size & 0x3 || *pos & 0x3)
3357 return -EINVAL;
3358
Markus Elfringecab7662016-09-18 17:00:52 +02003359 config = kmalloc_array(256, sizeof(*config), GFP_KERNEL);
Tom St Denis1e051412016-06-27 09:57:18 -04003360 if (!config)
3361 return -ENOMEM;
3362
3363 /* version, increment each time something is added */
Tom St Denis9a999352017-01-18 13:01:25 -05003364 config[no_regs++] = 3;
Tom St Denis1e051412016-06-27 09:57:18 -04003365 config[no_regs++] = adev->gfx.config.max_shader_engines;
3366 config[no_regs++] = adev->gfx.config.max_tile_pipes;
3367 config[no_regs++] = adev->gfx.config.max_cu_per_sh;
3368 config[no_regs++] = adev->gfx.config.max_sh_per_se;
3369 config[no_regs++] = adev->gfx.config.max_backends_per_se;
3370 config[no_regs++] = adev->gfx.config.max_texture_channel_caches;
3371 config[no_regs++] = adev->gfx.config.max_gprs;
3372 config[no_regs++] = adev->gfx.config.max_gs_threads;
3373 config[no_regs++] = adev->gfx.config.max_hw_contexts;
3374 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_frontend;
3375 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_backend;
3376 config[no_regs++] = adev->gfx.config.sc_hiz_tile_fifo_size;
3377 config[no_regs++] = adev->gfx.config.sc_earlyz_tile_fifo_size;
3378 config[no_regs++] = adev->gfx.config.num_tile_pipes;
3379 config[no_regs++] = adev->gfx.config.backend_enable_mask;
3380 config[no_regs++] = adev->gfx.config.mem_max_burst_length_bytes;
3381 config[no_regs++] = adev->gfx.config.mem_row_size_in_kb;
3382 config[no_regs++] = adev->gfx.config.shader_engine_tile_size;
3383 config[no_regs++] = adev->gfx.config.num_gpus;
3384 config[no_regs++] = adev->gfx.config.multi_gpu_tile_size;
3385 config[no_regs++] = adev->gfx.config.mc_arb_ramcfg;
3386 config[no_regs++] = adev->gfx.config.gb_addr_config;
3387 config[no_regs++] = adev->gfx.config.num_rbs;
3388
Tom St Denis89a8f302016-08-12 15:14:31 -04003389 /* rev==1 */
3390 config[no_regs++] = adev->rev_id;
3391 config[no_regs++] = adev->pg_flags;
3392 config[no_regs++] = adev->cg_flags;
3393
Tom St Denise9f11dc2016-08-17 12:00:51 -04003394 /* rev==2 */
3395 config[no_regs++] = adev->family;
3396 config[no_regs++] = adev->external_rev_id;
3397
Tom St Denis9a999352017-01-18 13:01:25 -05003398 /* rev==3 */
3399 config[no_regs++] = adev->pdev->device;
3400 config[no_regs++] = adev->pdev->revision;
3401 config[no_regs++] = adev->pdev->subsystem_device;
3402 config[no_regs++] = adev->pdev->subsystem_vendor;
3403
Tom St Denis1e051412016-06-27 09:57:18 -04003404 while (size && (*pos < no_regs * 4)) {
3405 uint32_t value;
3406
3407 value = config[*pos >> 2];
3408 r = put_user(value, (uint32_t *)buf);
3409 if (r) {
3410 kfree(config);
3411 return r;
3412 }
3413
3414 result += 4;
3415 buf += 4;
3416 *pos += 4;
3417 size -= 4;
3418 }
3419
3420 kfree(config);
3421 return result;
3422}
3423
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003424static ssize_t amdgpu_debugfs_sensor_read(struct file *f, char __user *buf,
3425 size_t size, loff_t *pos)
3426{
Al Viro45063092016-12-04 18:24:56 -05003427 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003428 int idx, x, outsize, r, valuesize;
3429 uint32_t values[16];
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003430
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003431 if (size & 3 || *pos & 0x3)
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003432 return -EINVAL;
3433
Samuel Pitoiset3cbc6142017-02-15 19:32:29 +01003434 if (amdgpu_dpm == 0)
3435 return -EINVAL;
3436
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003437 /* convert offset to sensor number */
3438 idx = *pos >> 2;
3439
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003440 valuesize = sizeof(values);
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003441 if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->read_sensor)
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003442 r = adev->powerplay.pp_funcs->read_sensor(adev->powerplay.pp_handle, idx, &values[0], &valuesize);
Samuel Pitoiset3cbc6142017-02-15 19:32:29 +01003443 else if (adev->pm.funcs && adev->pm.funcs->read_sensor)
3444 r = adev->pm.funcs->read_sensor(adev, idx, &values[0],
3445 &valuesize);
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003446 else
3447 return -EINVAL;
3448
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003449 if (size > valuesize)
3450 return -EINVAL;
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003451
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003452 outsize = 0;
3453 x = 0;
3454 if (!r) {
3455 while (size) {
3456 r = put_user(values[x++], (int32_t *)buf);
3457 buf += 4;
3458 size -= 4;
3459 outsize += 4;
3460 }
3461 }
3462
3463 return !r ? outsize : r;
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003464}
Tom St Denis1e051412016-06-27 09:57:18 -04003465
Tom St Denis273d7aa2016-10-11 14:48:55 -04003466static ssize_t amdgpu_debugfs_wave_read(struct file *f, char __user *buf,
3467 size_t size, loff_t *pos)
3468{
3469 struct amdgpu_device *adev = f->f_inode->i_private;
3470 int r, x;
3471 ssize_t result=0;
Tom St Denis472259f2016-10-14 09:49:09 -04003472 uint32_t offset, se, sh, cu, wave, simd, data[32];
Tom St Denis273d7aa2016-10-11 14:48:55 -04003473
3474 if (size & 3 || *pos & 3)
3475 return -EINVAL;
3476
3477 /* decode offset */
3478 offset = (*pos & 0x7F);
3479 se = ((*pos >> 7) & 0xFF);
3480 sh = ((*pos >> 15) & 0xFF);
3481 cu = ((*pos >> 23) & 0xFF);
3482 wave = ((*pos >> 31) & 0xFF);
3483 simd = ((*pos >> 37) & 0xFF);
Tom St Denis273d7aa2016-10-11 14:48:55 -04003484
3485 /* switch to the specific se/sh/cu */
3486 mutex_lock(&adev->grbm_idx_mutex);
3487 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
3488
3489 x = 0;
Tom St Denis472259f2016-10-14 09:49:09 -04003490 if (adev->gfx.funcs->read_wave_data)
3491 adev->gfx.funcs->read_wave_data(adev, simd, wave, data, &x);
Tom St Denis273d7aa2016-10-11 14:48:55 -04003492
3493 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
3494 mutex_unlock(&adev->grbm_idx_mutex);
3495
Tom St Denis5ecfb3b2016-10-13 12:15:03 -04003496 if (!x)
3497 return -EINVAL;
3498
Tom St Denis472259f2016-10-14 09:49:09 -04003499 while (size && (offset < x * 4)) {
Tom St Denis273d7aa2016-10-11 14:48:55 -04003500 uint32_t value;
3501
Tom St Denis472259f2016-10-14 09:49:09 -04003502 value = data[offset >> 2];
Tom St Denis273d7aa2016-10-11 14:48:55 -04003503 r = put_user(value, (uint32_t *)buf);
3504 if (r)
3505 return r;
3506
3507 result += 4;
3508 buf += 4;
Tom St Denis472259f2016-10-14 09:49:09 -04003509 offset += 4;
Tom St Denis273d7aa2016-10-11 14:48:55 -04003510 size -= 4;
3511 }
3512
3513 return result;
3514}
3515
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003516static ssize_t amdgpu_debugfs_gpr_read(struct file *f, char __user *buf,
3517 size_t size, loff_t *pos)
3518{
3519 struct amdgpu_device *adev = f->f_inode->i_private;
3520 int r;
3521 ssize_t result = 0;
3522 uint32_t offset, se, sh, cu, wave, simd, thread, bank, *data;
3523
3524 if (size & 3 || *pos & 3)
3525 return -EINVAL;
3526
3527 /* decode offset */
3528 offset = (*pos & 0xFFF); /* in dwords */
3529 se = ((*pos >> 12) & 0xFF);
3530 sh = ((*pos >> 20) & 0xFF);
3531 cu = ((*pos >> 28) & 0xFF);
3532 wave = ((*pos >> 36) & 0xFF);
3533 simd = ((*pos >> 44) & 0xFF);
3534 thread = ((*pos >> 52) & 0xFF);
3535 bank = ((*pos >> 60) & 1);
3536
3537 data = kmalloc_array(1024, sizeof(*data), GFP_KERNEL);
3538 if (!data)
3539 return -ENOMEM;
3540
3541 /* switch to the specific se/sh/cu */
3542 mutex_lock(&adev->grbm_idx_mutex);
3543 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
3544
3545 if (bank == 0) {
3546 if (adev->gfx.funcs->read_wave_vgprs)
3547 adev->gfx.funcs->read_wave_vgprs(adev, simd, wave, thread, offset, size>>2, data);
3548 } else {
3549 if (adev->gfx.funcs->read_wave_sgprs)
3550 adev->gfx.funcs->read_wave_sgprs(adev, simd, wave, offset, size>>2, data);
3551 }
3552
3553 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
3554 mutex_unlock(&adev->grbm_idx_mutex);
3555
3556 while (size) {
3557 uint32_t value;
3558
3559 value = data[offset++];
3560 r = put_user(value, (uint32_t *)buf);
3561 if (r) {
3562 result = r;
3563 goto err;
3564 }
3565
3566 result += 4;
3567 buf += 4;
3568 size -= 4;
3569 }
3570
3571err:
3572 kfree(data);
3573 return result;
3574}
3575
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003576static const struct file_operations amdgpu_debugfs_regs_fops = {
3577 .owner = THIS_MODULE,
3578 .read = amdgpu_debugfs_regs_read,
3579 .write = amdgpu_debugfs_regs_write,
3580 .llseek = default_llseek
3581};
Tom St Denisadcec282016-04-15 13:08:44 -04003582static const struct file_operations amdgpu_debugfs_regs_didt_fops = {
3583 .owner = THIS_MODULE,
3584 .read = amdgpu_debugfs_regs_didt_read,
3585 .write = amdgpu_debugfs_regs_didt_write,
3586 .llseek = default_llseek
3587};
3588static const struct file_operations amdgpu_debugfs_regs_pcie_fops = {
3589 .owner = THIS_MODULE,
3590 .read = amdgpu_debugfs_regs_pcie_read,
3591 .write = amdgpu_debugfs_regs_pcie_write,
3592 .llseek = default_llseek
3593};
3594static const struct file_operations amdgpu_debugfs_regs_smc_fops = {
3595 .owner = THIS_MODULE,
3596 .read = amdgpu_debugfs_regs_smc_read,
3597 .write = amdgpu_debugfs_regs_smc_write,
3598 .llseek = default_llseek
3599};
3600
Tom St Denis1e051412016-06-27 09:57:18 -04003601static const struct file_operations amdgpu_debugfs_gca_config_fops = {
3602 .owner = THIS_MODULE,
3603 .read = amdgpu_debugfs_gca_config_read,
3604 .llseek = default_llseek
3605};
3606
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003607static const struct file_operations amdgpu_debugfs_sensors_fops = {
3608 .owner = THIS_MODULE,
3609 .read = amdgpu_debugfs_sensor_read,
3610 .llseek = default_llseek
3611};
3612
Tom St Denis273d7aa2016-10-11 14:48:55 -04003613static const struct file_operations amdgpu_debugfs_wave_fops = {
3614 .owner = THIS_MODULE,
3615 .read = amdgpu_debugfs_wave_read,
3616 .llseek = default_llseek
3617};
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003618static const struct file_operations amdgpu_debugfs_gpr_fops = {
3619 .owner = THIS_MODULE,
3620 .read = amdgpu_debugfs_gpr_read,
3621 .llseek = default_llseek
3622};
Tom St Denis273d7aa2016-10-11 14:48:55 -04003623
Tom St Denisadcec282016-04-15 13:08:44 -04003624static const struct file_operations *debugfs_regs[] = {
3625 &amdgpu_debugfs_regs_fops,
3626 &amdgpu_debugfs_regs_didt_fops,
3627 &amdgpu_debugfs_regs_pcie_fops,
3628 &amdgpu_debugfs_regs_smc_fops,
Tom St Denis1e051412016-06-27 09:57:18 -04003629 &amdgpu_debugfs_gca_config_fops,
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003630 &amdgpu_debugfs_sensors_fops,
Tom St Denis273d7aa2016-10-11 14:48:55 -04003631 &amdgpu_debugfs_wave_fops,
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003632 &amdgpu_debugfs_gpr_fops,
Tom St Denisadcec282016-04-15 13:08:44 -04003633};
3634
3635static const char *debugfs_regs_names[] = {
3636 "amdgpu_regs",
3637 "amdgpu_regs_didt",
3638 "amdgpu_regs_pcie",
3639 "amdgpu_regs_smc",
Tom St Denis1e051412016-06-27 09:57:18 -04003640 "amdgpu_gca_config",
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003641 "amdgpu_sensors",
Tom St Denis273d7aa2016-10-11 14:48:55 -04003642 "amdgpu_wave",
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003643 "amdgpu_gpr",
Tom St Denisadcec282016-04-15 13:08:44 -04003644};
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003645
3646static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
3647{
3648 struct drm_minor *minor = adev->ddev->primary;
3649 struct dentry *ent, *root = minor->debugfs_root;
Tom St Denisadcec282016-04-15 13:08:44 -04003650 unsigned i, j;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003651
Tom St Denisadcec282016-04-15 13:08:44 -04003652 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3653 ent = debugfs_create_file(debugfs_regs_names[i],
3654 S_IFREG | S_IRUGO, root,
3655 adev, debugfs_regs[i]);
3656 if (IS_ERR(ent)) {
3657 for (j = 0; j < i; j++) {
3658 debugfs_remove(adev->debugfs_regs[i]);
3659 adev->debugfs_regs[i] = NULL;
3660 }
3661 return PTR_ERR(ent);
3662 }
3663
3664 if (!i)
3665 i_size_write(ent->d_inode, adev->rmmio_size);
3666 adev->debugfs_regs[i] = ent;
3667 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003668
3669 return 0;
3670}
3671
3672static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev)
3673{
Tom St Denisadcec282016-04-15 13:08:44 -04003674 unsigned i;
3675
3676 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3677 if (adev->debugfs_regs[i]) {
3678 debugfs_remove(adev->debugfs_regs[i]);
3679 adev->debugfs_regs[i] = NULL;
3680 }
3681 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003682}
3683
3684int amdgpu_debugfs_init(struct drm_minor *minor)
3685{
3686 return 0;
3687}
Alexander Kuleshov7cebc722015-06-27 13:16:05 +06003688#else
3689static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
3690{
3691 return 0;
3692}
3693static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev) { }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003694#endif