blob: a70247203f18e6fb92f262aede815919baf01713 [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 Deucherd0dd7f02015-11-11 19:45:06 -050043#include "amd_pcie.h"
Ken Wang33f34802016-01-21 17:29:41 +080044#ifdef CONFIG_DRM_AMDGPU_SI
45#include "si.h"
46#endif
Alex Deuchera2e73f52015-04-20 17:09:27 -040047#ifdef CONFIG_DRM_AMDGPU_CIK
48#include "cik.h"
49#endif
Alex Deucheraaa36a92015-04-20 17:31:14 -040050#include "vi.h"
Alex Deucherd38ceaf2015-04-20 16:55:21 -040051#include "bif/bif_4_1_d.h"
Emily Deng9accf2f2016-08-10 16:01:25 +080052#include <linux/pci.h>
Monk Liubec86372016-09-14 19:38:08 +080053#include <linux/firmware.h>
Alex Deucherd38ceaf2015-04-20 16:55:21 -040054
55static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev);
56static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev);
57
58static const char *amdgpu_asic_name[] = {
Ken Wangda69c1612016-01-21 19:08:55 +080059 "TAHITI",
60 "PITCAIRN",
61 "VERDE",
62 "OLAND",
63 "HAINAN",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040064 "BONAIRE",
65 "KAVERI",
66 "KABINI",
67 "HAWAII",
68 "MULLINS",
69 "TOPAZ",
70 "TONGA",
David Zhang48299f92015-07-08 01:05:16 +080071 "FIJI",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040072 "CARRIZO",
Samuel Li139f4912015-10-08 14:50:27 -040073 "STONEY",
Flora Cui2cc0c0b2016-03-14 18:33:29 -040074 "POLARIS10",
75 "POLARIS11",
Junwei Zhangc4642a42016-12-14 15:32:28 -050076 "POLARIS12",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040077 "LAST",
78};
79
80bool amdgpu_device_is_px(struct drm_device *dev)
81{
82 struct amdgpu_device *adev = dev->dev_private;
83
Jammy Zhou2f7d10b2015-07-22 11:29:01 +080084 if (adev->flags & AMD_IS_PX)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040085 return true;
86 return false;
87}
88
89/*
90 * MMIO register access helper functions.
91 */
92uint32_t amdgpu_mm_rreg(struct amdgpu_device *adev, uint32_t reg,
Monk Liu15d72fd2017-01-25 15:07:40 +080093 uint32_t acc_flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040094{
Tom St Denisf4b373f2016-05-31 08:02:27 -040095 uint32_t ret;
96
Monk Liu15d72fd2017-01-25 15:07:40 +080097 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev)) {
Xiangliang Yubc992ba2017-01-12 14:29:34 +080098 BUG_ON(in_interrupt());
99 return amdgpu_virt_kiq_rreg(adev, reg);
100 }
101
Monk Liu15d72fd2017-01-25 15:07:40 +0800102 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
Tom St Denisf4b373f2016-05-31 08:02:27 -0400103 ret = readl(((void __iomem *)adev->rmmio) + (reg * 4));
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400104 else {
105 unsigned long flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400106
107 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
108 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
109 ret = readl(((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
110 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400111 }
Tom St Denisf4b373f2016-05-31 08:02:27 -0400112 trace_amdgpu_mm_rreg(adev->pdev->device, reg, ret);
113 return ret;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400114}
115
116void amdgpu_mm_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v,
Monk Liu15d72fd2017-01-25 15:07:40 +0800117 uint32_t acc_flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400118{
Tom St Denisf4b373f2016-05-31 08:02:27 -0400119 trace_amdgpu_mm_wreg(adev->pdev->device, reg, v);
Monk Liu4e99a442016-03-31 13:26:59 +0800120
Monk Liu15d72fd2017-01-25 15:07:40 +0800121 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev)) {
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800122 BUG_ON(in_interrupt());
123 return amdgpu_virt_kiq_wreg(adev, reg, v);
124 }
125
Monk Liu15d72fd2017-01-25 15:07:40 +0800126 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400127 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
128 else {
129 unsigned long flags;
130
131 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
132 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
133 writel(v, ((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
134 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
135 }
136}
137
138u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 reg)
139{
140 if ((reg * 4) < adev->rio_mem_size)
141 return ioread32(adev->rio_mem + (reg * 4));
142 else {
143 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
144 return ioread32(adev->rio_mem + (mmMM_DATA * 4));
145 }
146}
147
148void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
149{
150
151 if ((reg * 4) < adev->rio_mem_size)
152 iowrite32(v, adev->rio_mem + (reg * 4));
153 else {
154 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
155 iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
156 }
157}
158
159/**
160 * amdgpu_mm_rdoorbell - read a doorbell dword
161 *
162 * @adev: amdgpu_device pointer
163 * @index: doorbell index
164 *
165 * Returns the value in the doorbell aperture at the
166 * requested doorbell index (CIK).
167 */
168u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
169{
170 if (index < adev->doorbell.num_doorbells) {
171 return readl(adev->doorbell.ptr + index);
172 } else {
173 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
174 return 0;
175 }
176}
177
178/**
179 * amdgpu_mm_wdoorbell - write a doorbell dword
180 *
181 * @adev: amdgpu_device pointer
182 * @index: doorbell index
183 * @v: value to write
184 *
185 * Writes @v to the doorbell aperture at the
186 * requested doorbell index (CIK).
187 */
188void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
189{
190 if (index < adev->doorbell.num_doorbells) {
191 writel(v, adev->doorbell.ptr + index);
192 } else {
193 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
194 }
195}
196
197/**
Ken Wang832be402016-03-18 15:23:08 +0800198 * amdgpu_mm_rdoorbell64 - read a doorbell Qword
199 *
200 * @adev: amdgpu_device pointer
201 * @index: doorbell index
202 *
203 * Returns the value in the doorbell aperture at the
204 * requested doorbell index (VEGA10+).
205 */
206u64 amdgpu_mm_rdoorbell64(struct amdgpu_device *adev, u32 index)
207{
208 if (index < adev->doorbell.num_doorbells) {
209 return atomic64_read((atomic64_t *)(adev->doorbell.ptr + index));
210 } else {
211 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
212 return 0;
213 }
214}
215
216/**
217 * amdgpu_mm_wdoorbell64 - write a doorbell Qword
218 *
219 * @adev: amdgpu_device pointer
220 * @index: doorbell index
221 * @v: value to write
222 *
223 * Writes @v to the doorbell aperture at the
224 * requested doorbell index (VEGA10+).
225 */
226void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v)
227{
228 if (index < adev->doorbell.num_doorbells) {
229 atomic64_set((atomic64_t *)(adev->doorbell.ptr + index), v);
230 } else {
231 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
232 }
233}
234
235/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400236 * amdgpu_invalid_rreg - dummy reg read function
237 *
238 * @adev: amdgpu device pointer
239 * @reg: offset of register
240 *
241 * Dummy register read function. Used for register blocks
242 * that certain asics don't have (all asics).
243 * Returns the value in the register.
244 */
245static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
246{
247 DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
248 BUG();
249 return 0;
250}
251
252/**
253 * amdgpu_invalid_wreg - dummy reg write function
254 *
255 * @adev: amdgpu device pointer
256 * @reg: offset of register
257 * @v: value to write to the register
258 *
259 * Dummy register read function. Used for register blocks
260 * that certain asics don't have (all asics).
261 */
262static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
263{
264 DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
265 reg, v);
266 BUG();
267}
268
269/**
270 * amdgpu_block_invalid_rreg - dummy reg read function
271 *
272 * @adev: amdgpu device pointer
273 * @block: offset of instance
274 * @reg: offset of register
275 *
276 * Dummy register read function. Used for register blocks
277 * that certain asics don't have (all asics).
278 * Returns the value in the register.
279 */
280static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
281 uint32_t block, uint32_t reg)
282{
283 DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
284 reg, block);
285 BUG();
286 return 0;
287}
288
289/**
290 * amdgpu_block_invalid_wreg - dummy reg write function
291 *
292 * @adev: amdgpu device pointer
293 * @block: offset of instance
294 * @reg: offset of register
295 * @v: value to write to the register
296 *
297 * Dummy register read function. Used for register blocks
298 * that certain asics don't have (all asics).
299 */
300static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
301 uint32_t block,
302 uint32_t reg, uint32_t v)
303{
304 DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
305 reg, block, v);
306 BUG();
307}
308
309static int amdgpu_vram_scratch_init(struct amdgpu_device *adev)
310{
311 int r;
312
313 if (adev->vram_scratch.robj == NULL) {
314 r = amdgpu_bo_create(adev, AMDGPU_GPU_PAGE_SIZE,
Alex Deucher857d9132015-08-27 00:14:16 -0400315 PAGE_SIZE, true, AMDGPU_GEM_DOMAIN_VRAM,
Christian König03f48dd2016-08-15 17:00:22 +0200316 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
317 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS,
Christian König72d76682015-09-03 17:34:59 +0200318 NULL, NULL, &adev->vram_scratch.robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400319 if (r) {
320 return r;
321 }
322 }
323
324 r = amdgpu_bo_reserve(adev->vram_scratch.robj, false);
325 if (unlikely(r != 0))
326 return r;
327 r = amdgpu_bo_pin(adev->vram_scratch.robj,
328 AMDGPU_GEM_DOMAIN_VRAM, &adev->vram_scratch.gpu_addr);
329 if (r) {
330 amdgpu_bo_unreserve(adev->vram_scratch.robj);
331 return r;
332 }
333 r = amdgpu_bo_kmap(adev->vram_scratch.robj,
334 (void **)&adev->vram_scratch.ptr);
335 if (r)
336 amdgpu_bo_unpin(adev->vram_scratch.robj);
337 amdgpu_bo_unreserve(adev->vram_scratch.robj);
338
339 return r;
340}
341
342static void amdgpu_vram_scratch_fini(struct amdgpu_device *adev)
343{
344 int r;
345
346 if (adev->vram_scratch.robj == NULL) {
347 return;
348 }
349 r = amdgpu_bo_reserve(adev->vram_scratch.robj, false);
350 if (likely(r == 0)) {
351 amdgpu_bo_kunmap(adev->vram_scratch.robj);
352 amdgpu_bo_unpin(adev->vram_scratch.robj);
353 amdgpu_bo_unreserve(adev->vram_scratch.robj);
354 }
355 amdgpu_bo_unref(&adev->vram_scratch.robj);
356}
357
358/**
359 * amdgpu_program_register_sequence - program an array of registers.
360 *
361 * @adev: amdgpu_device pointer
362 * @registers: pointer to the register array
363 * @array_size: size of the register array
364 *
365 * Programs an array or registers with and and or masks.
366 * This is a helper for setting golden registers.
367 */
368void amdgpu_program_register_sequence(struct amdgpu_device *adev,
369 const u32 *registers,
370 const u32 array_size)
371{
372 u32 tmp, reg, and_mask, or_mask;
373 int i;
374
375 if (array_size % 3)
376 return;
377
378 for (i = 0; i < array_size; i +=3) {
379 reg = registers[i + 0];
380 and_mask = registers[i + 1];
381 or_mask = registers[i + 2];
382
383 if (and_mask == 0xffffffff) {
384 tmp = or_mask;
385 } else {
386 tmp = RREG32(reg);
387 tmp &= ~and_mask;
388 tmp |= or_mask;
389 }
390 WREG32(reg, tmp);
391 }
392}
393
394void amdgpu_pci_config_reset(struct amdgpu_device *adev)
395{
396 pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
397}
398
399/*
400 * GPU doorbell aperture helpers function.
401 */
402/**
403 * amdgpu_doorbell_init - Init doorbell driver information.
404 *
405 * @adev: amdgpu_device pointer
406 *
407 * Init doorbell driver information (CIK)
408 * Returns 0 on success, error on failure.
409 */
410static int amdgpu_doorbell_init(struct amdgpu_device *adev)
411{
412 /* doorbell bar mapping */
413 adev->doorbell.base = pci_resource_start(adev->pdev, 2);
414 adev->doorbell.size = pci_resource_len(adev->pdev, 2);
415
Christian Königedf600d2016-05-03 15:54:54 +0200416 adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400417 AMDGPU_DOORBELL_MAX_ASSIGNMENT+1);
418 if (adev->doorbell.num_doorbells == 0)
419 return -EINVAL;
420
421 adev->doorbell.ptr = ioremap(adev->doorbell.base, adev->doorbell.num_doorbells * sizeof(u32));
422 if (adev->doorbell.ptr == NULL) {
423 return -ENOMEM;
424 }
425 DRM_INFO("doorbell mmio base: 0x%08X\n", (uint32_t)adev->doorbell.base);
426 DRM_INFO("doorbell mmio size: %u\n", (unsigned)adev->doorbell.size);
427
428 return 0;
429}
430
431/**
432 * amdgpu_doorbell_fini - Tear down doorbell driver information.
433 *
434 * @adev: amdgpu_device pointer
435 *
436 * Tear down doorbell driver information (CIK)
437 */
438static void amdgpu_doorbell_fini(struct amdgpu_device *adev)
439{
440 iounmap(adev->doorbell.ptr);
441 adev->doorbell.ptr = NULL;
442}
443
444/**
445 * amdgpu_doorbell_get_kfd_info - Report doorbell configuration required to
446 * setup amdkfd
447 *
448 * @adev: amdgpu_device pointer
449 * @aperture_base: output returning doorbell aperture base physical address
450 * @aperture_size: output returning doorbell aperture size in bytes
451 * @start_offset: output returning # of doorbell bytes reserved for amdgpu.
452 *
453 * amdgpu and amdkfd share the doorbell aperture. amdgpu sets it up,
454 * takes doorbells required for its own rings and reports the setup to amdkfd.
455 * amdgpu reserved doorbells are at the start of the doorbell aperture.
456 */
457void amdgpu_doorbell_get_kfd_info(struct amdgpu_device *adev,
458 phys_addr_t *aperture_base,
459 size_t *aperture_size,
460 size_t *start_offset)
461{
462 /*
463 * The first num_doorbells are used by amdgpu.
464 * amdkfd takes whatever's left in the aperture.
465 */
466 if (adev->doorbell.size > adev->doorbell.num_doorbells * sizeof(u32)) {
467 *aperture_base = adev->doorbell.base;
468 *aperture_size = adev->doorbell.size;
469 *start_offset = adev->doorbell.num_doorbells * sizeof(u32);
470 } else {
471 *aperture_base = 0;
472 *aperture_size = 0;
473 *start_offset = 0;
474 }
475}
476
477/*
478 * amdgpu_wb_*()
479 * Writeback is the the method by which the the GPU updates special pages
480 * in memory with the status of certain GPU events (fences, ring pointers,
481 * etc.).
482 */
483
484/**
485 * amdgpu_wb_fini - Disable Writeback and free memory
486 *
487 * @adev: amdgpu_device pointer
488 *
489 * Disables Writeback and frees the Writeback memory (all asics).
490 * Used at driver shutdown.
491 */
492static void amdgpu_wb_fini(struct amdgpu_device *adev)
493{
494 if (adev->wb.wb_obj) {
Alex Deuchera76ed482016-10-21 15:30:36 -0400495 amdgpu_bo_free_kernel(&adev->wb.wb_obj,
496 &adev->wb.gpu_addr,
497 (void **)&adev->wb.wb);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400498 adev->wb.wb_obj = NULL;
499 }
500}
501
502/**
503 * amdgpu_wb_init- Init Writeback driver info and allocate memory
504 *
505 * @adev: amdgpu_device pointer
506 *
507 * Disables Writeback and frees the Writeback memory (all asics).
508 * Used at driver startup.
509 * Returns 0 on success or an -error on failure.
510 */
511static int amdgpu_wb_init(struct amdgpu_device *adev)
512{
513 int r;
514
515 if (adev->wb.wb_obj == NULL) {
Huang Rui60a970a62017-03-15 10:13:32 +0800516 r = amdgpu_bo_create_kernel(adev, AMDGPU_MAX_WB * sizeof(uint32_t),
Alex Deuchera76ed482016-10-21 15:30:36 -0400517 PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
518 &adev->wb.wb_obj, &adev->wb.gpu_addr,
519 (void **)&adev->wb.wb);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400520 if (r) {
521 dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
522 return r;
523 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400524
525 adev->wb.num_wb = AMDGPU_MAX_WB;
526 memset(&adev->wb.used, 0, sizeof(adev->wb.used));
527
528 /* clear wb memory */
Huang Rui60a970a62017-03-15 10:13:32 +0800529 memset((char *)adev->wb.wb, 0, AMDGPU_MAX_WB * sizeof(uint32_t));
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400530 }
531
532 return 0;
533}
534
535/**
536 * amdgpu_wb_get - Allocate a wb entry
537 *
538 * @adev: amdgpu_device pointer
539 * @wb: wb index
540 *
541 * Allocate a wb slot for use by the driver (all asics).
542 * Returns 0 on success or -EINVAL on failure.
543 */
544int amdgpu_wb_get(struct amdgpu_device *adev, u32 *wb)
545{
546 unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
547 if (offset < adev->wb.num_wb) {
548 __set_bit(offset, adev->wb.used);
549 *wb = offset;
550 return 0;
551 } else {
552 return -EINVAL;
553 }
554}
555
556/**
Ken Wang70142852016-03-18 15:08:49 +0800557 * amdgpu_wb_get_64bit - Allocate a wb entry
558 *
559 * @adev: amdgpu_device pointer
560 * @wb: wb index
561 *
562 * Allocate a wb slot for use by the driver (all asics).
563 * Returns 0 on success or -EINVAL on failure.
564 */
565int amdgpu_wb_get_64bit(struct amdgpu_device *adev, u32 *wb)
566{
567 unsigned long offset = bitmap_find_next_zero_area_off(adev->wb.used,
568 adev->wb.num_wb, 0, 2, 7, 0);
569 if ((offset + 1) < adev->wb.num_wb) {
570 __set_bit(offset, adev->wb.used);
571 __set_bit(offset + 1, adev->wb.used);
572 *wb = offset;
573 return 0;
574 } else {
575 return -EINVAL;
576 }
577}
578
579/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400580 * amdgpu_wb_free - Free a wb entry
581 *
582 * @adev: amdgpu_device pointer
583 * @wb: wb index
584 *
585 * Free a wb slot allocated for use by the driver (all asics)
586 */
587void amdgpu_wb_free(struct amdgpu_device *adev, u32 wb)
588{
589 if (wb < adev->wb.num_wb)
590 __clear_bit(wb, adev->wb.used);
591}
592
593/**
Ken Wang70142852016-03-18 15:08:49 +0800594 * amdgpu_wb_free_64bit - Free a wb entry
595 *
596 * @adev: amdgpu_device pointer
597 * @wb: wb index
598 *
599 * Free a wb slot allocated for use by the driver (all asics)
600 */
601void amdgpu_wb_free_64bit(struct amdgpu_device *adev, u32 wb)
602{
603 if ((wb + 1) < adev->wb.num_wb) {
604 __clear_bit(wb, adev->wb.used);
605 __clear_bit(wb + 1, adev->wb.used);
606 }
607}
608
609/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400610 * amdgpu_vram_location - try to find VRAM location
611 * @adev: amdgpu device structure holding all necessary informations
612 * @mc: memory controller structure holding memory informations
613 * @base: base address at which to put VRAM
614 *
615 * Function will place try to place VRAM at base address provided
616 * as parameter (which is so far either PCI aperture address or
617 * for IGP TOM base address).
618 *
619 * If there is not enough space to fit the unvisible VRAM in the 32bits
620 * address space then we limit the VRAM size to the aperture.
621 *
622 * Note: We don't explicitly enforce VRAM start to be aligned on VRAM size,
623 * this shouldn't be a problem as we are using the PCI aperture as a reference.
624 * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but
625 * not IGP.
626 *
627 * Note: we use mc_vram_size as on some board we need to program the mc to
628 * cover the whole aperture even if VRAM size is inferior to aperture size
629 * Novell bug 204882 + along with lots of ubuntu ones
630 *
631 * Note: when limiting vram it's safe to overwritte real_vram_size because
632 * we are not in case where real_vram_size is inferior to mc_vram_size (ie
633 * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu
634 * ones)
635 *
636 * Note: IGP TOM addr should be the same as the aperture addr, we don't
637 * explicitly check for that thought.
638 *
639 * FIXME: when reducing VRAM size align new size on power of 2.
640 */
641void amdgpu_vram_location(struct amdgpu_device *adev, struct amdgpu_mc *mc, u64 base)
642{
643 uint64_t limit = (uint64_t)amdgpu_vram_limit << 20;
644
645 mc->vram_start = base;
646 if (mc->mc_vram_size > (adev->mc.mc_mask - base + 1)) {
647 dev_warn(adev->dev, "limiting VRAM to PCI aperture size\n");
648 mc->real_vram_size = mc->aper_size;
649 mc->mc_vram_size = mc->aper_size;
650 }
651 mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
652 if (limit && limit < mc->real_vram_size)
653 mc->real_vram_size = limit;
654 dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
655 mc->mc_vram_size >> 20, mc->vram_start,
656 mc->vram_end, mc->real_vram_size >> 20);
657}
658
659/**
660 * amdgpu_gtt_location - try to find GTT location
661 * @adev: amdgpu device structure holding all necessary informations
662 * @mc: memory controller structure holding memory informations
663 *
664 * Function will place try to place GTT before or after VRAM.
665 *
666 * If GTT size is bigger than space left then we ajust GTT size.
667 * Thus function will never fails.
668 *
669 * FIXME: when reducing GTT size align new size on power of 2.
670 */
671void amdgpu_gtt_location(struct amdgpu_device *adev, struct amdgpu_mc *mc)
672{
673 u64 size_af, size_bf;
674
675 size_af = ((adev->mc.mc_mask - mc->vram_end) + mc->gtt_base_align) & ~mc->gtt_base_align;
676 size_bf = mc->vram_start & ~mc->gtt_base_align;
677 if (size_bf > size_af) {
678 if (mc->gtt_size > size_bf) {
679 dev_warn(adev->dev, "limiting GTT\n");
680 mc->gtt_size = size_bf;
681 }
Alex Deucher9dc5a912016-11-17 15:40:22 -0500682 mc->gtt_start = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400683 } else {
684 if (mc->gtt_size > size_af) {
685 dev_warn(adev->dev, "limiting GTT\n");
686 mc->gtt_size = size_af;
687 }
688 mc->gtt_start = (mc->vram_end + 1 + mc->gtt_base_align) & ~mc->gtt_base_align;
689 }
690 mc->gtt_end = mc->gtt_start + mc->gtt_size - 1;
691 dev_info(adev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
692 mc->gtt_size >> 20, mc->gtt_start, mc->gtt_end);
693}
694
695/*
696 * GPU helpers function.
697 */
698/**
Jim Quc836fec2017-02-10 15:59:59 +0800699 * amdgpu_need_post - check if the hw need post or not
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400700 *
701 * @adev: amdgpu_device pointer
702 *
Jim Quc836fec2017-02-10 15:59:59 +0800703 * Check if the asic has been initialized (all asics) at driver startup
704 * or post is needed if hw reset is performed.
705 * Returns true if need or false if not.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400706 */
Jim Quc836fec2017-02-10 15:59:59 +0800707bool amdgpu_need_post(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400708{
709 uint32_t reg;
710
Jim Quc836fec2017-02-10 15:59:59 +0800711 if (adev->has_hw_reset) {
712 adev->has_hw_reset = false;
713 return true;
714 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400715 /* then check MEM_SIZE, in case the crtcs are off */
716 reg = RREG32(mmCONFIG_MEMSIZE);
717
718 if (reg)
Jim Quc836fec2017-02-10 15:59:59 +0800719 return false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400720
Jim Quc836fec2017-02-10 15:59:59 +0800721 return true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400722
723}
724
Monk Liubec86372016-09-14 19:38:08 +0800725static bool amdgpu_vpost_needed(struct amdgpu_device *adev)
726{
727 if (amdgpu_sriov_vf(adev))
728 return false;
729
730 if (amdgpu_passthrough(adev)) {
Monk Liu1da2c322016-11-11 11:24:29 +0800731 /* for FIJI: In whole GPU pass-through virtualization case, after VM reboot
732 * some old smc fw still need driver do vPost otherwise gpu hang, while
733 * those smc fw version above 22.15 doesn't have this flaw, so we force
734 * vpost executed for smc version below 22.15
Monk Liubec86372016-09-14 19:38:08 +0800735 */
736 if (adev->asic_type == CHIP_FIJI) {
737 int err;
738 uint32_t fw_ver;
739 err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
740 /* force vPost if error occured */
741 if (err)
742 return true;
743
744 fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
Monk Liu1da2c322016-11-11 11:24:29 +0800745 if (fw_ver < 0x00160e00)
746 return true;
Monk Liubec86372016-09-14 19:38:08 +0800747 }
Monk Liubec86372016-09-14 19:38:08 +0800748 }
Jim Quc836fec2017-02-10 15:59:59 +0800749 return amdgpu_need_post(adev);
Monk Liubec86372016-09-14 19:38:08 +0800750}
751
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400752/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400753 * amdgpu_dummy_page_init - init dummy page used by the driver
754 *
755 * @adev: amdgpu_device pointer
756 *
757 * Allocate the dummy page used by the driver (all asics).
758 * This dummy page is used by the driver as a filler for gart entries
759 * when pages are taken out of the GART
760 * Returns 0 on sucess, -ENOMEM on failure.
761 */
762int amdgpu_dummy_page_init(struct amdgpu_device *adev)
763{
764 if (adev->dummy_page.page)
765 return 0;
766 adev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
767 if (adev->dummy_page.page == NULL)
768 return -ENOMEM;
769 adev->dummy_page.addr = pci_map_page(adev->pdev, adev->dummy_page.page,
770 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
771 if (pci_dma_mapping_error(adev->pdev, adev->dummy_page.addr)) {
772 dev_err(&adev->pdev->dev, "Failed to DMA MAP the dummy page\n");
773 __free_page(adev->dummy_page.page);
774 adev->dummy_page.page = NULL;
775 return -ENOMEM;
776 }
777 return 0;
778}
779
780/**
781 * amdgpu_dummy_page_fini - free dummy page used by the driver
782 *
783 * @adev: amdgpu_device pointer
784 *
785 * Frees the dummy page used by the driver (all asics).
786 */
787void amdgpu_dummy_page_fini(struct amdgpu_device *adev)
788{
789 if (adev->dummy_page.page == NULL)
790 return;
791 pci_unmap_page(adev->pdev, adev->dummy_page.addr,
792 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
793 __free_page(adev->dummy_page.page);
794 adev->dummy_page.page = NULL;
795}
796
797
798/* ATOM accessor methods */
799/*
800 * ATOM is an interpreted byte code stored in tables in the vbios. The
801 * driver registers callbacks to access registers and the interpreter
802 * in the driver parses the tables and executes then to program specific
803 * actions (set display modes, asic init, etc.). See amdgpu_atombios.c,
804 * atombios.h, and atom.c
805 */
806
807/**
808 * cail_pll_read - read PLL register
809 *
810 * @info: atom card_info pointer
811 * @reg: PLL register offset
812 *
813 * Provides a PLL register accessor for the atom interpreter (r4xx+).
814 * Returns the value of the PLL register.
815 */
816static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
817{
818 return 0;
819}
820
821/**
822 * cail_pll_write - write PLL register
823 *
824 * @info: atom card_info pointer
825 * @reg: PLL register offset
826 * @val: value to write to the pll register
827 *
828 * Provides a PLL register accessor for the atom interpreter (r4xx+).
829 */
830static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
831{
832
833}
834
835/**
836 * cail_mc_read - read MC (Memory Controller) register
837 *
838 * @info: atom card_info pointer
839 * @reg: MC register offset
840 *
841 * Provides an MC register accessor for the atom interpreter (r4xx+).
842 * Returns the value of the MC register.
843 */
844static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
845{
846 return 0;
847}
848
849/**
850 * cail_mc_write - write MC (Memory Controller) register
851 *
852 * @info: atom card_info pointer
853 * @reg: MC register offset
854 * @val: value to write to the pll register
855 *
856 * Provides a MC register accessor for the atom interpreter (r4xx+).
857 */
858static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
859{
860
861}
862
863/**
864 * cail_reg_write - write MMIO register
865 *
866 * @info: atom card_info pointer
867 * @reg: MMIO register offset
868 * @val: value to write to the pll register
869 *
870 * Provides a MMIO register accessor for the atom interpreter (r4xx+).
871 */
872static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
873{
874 struct amdgpu_device *adev = info->dev->dev_private;
875
876 WREG32(reg, val);
877}
878
879/**
880 * cail_reg_read - read MMIO register
881 *
882 * @info: atom card_info pointer
883 * @reg: MMIO register offset
884 *
885 * Provides an MMIO register accessor for the atom interpreter (r4xx+).
886 * Returns the value of the MMIO register.
887 */
888static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
889{
890 struct amdgpu_device *adev = info->dev->dev_private;
891 uint32_t r;
892
893 r = RREG32(reg);
894 return r;
895}
896
897/**
898 * cail_ioreg_write - write IO register
899 *
900 * @info: atom card_info pointer
901 * @reg: IO register offset
902 * @val: value to write to the pll register
903 *
904 * Provides a IO register accessor for the atom interpreter (r4xx+).
905 */
906static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
907{
908 struct amdgpu_device *adev = info->dev->dev_private;
909
910 WREG32_IO(reg, val);
911}
912
913/**
914 * cail_ioreg_read - read IO register
915 *
916 * @info: atom card_info pointer
917 * @reg: IO register offset
918 *
919 * Provides an IO register accessor for the atom interpreter (r4xx+).
920 * Returns the value of the IO register.
921 */
922static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
923{
924 struct amdgpu_device *adev = info->dev->dev_private;
925 uint32_t r;
926
927 r = RREG32_IO(reg);
928 return r;
929}
930
931/**
932 * amdgpu_atombios_fini - free the driver info and callbacks for atombios
933 *
934 * @adev: amdgpu_device pointer
935 *
936 * Frees the driver info and register access callbacks for the ATOM
937 * interpreter (r4xx+).
938 * Called at driver shutdown.
939 */
940static void amdgpu_atombios_fini(struct amdgpu_device *adev)
941{
Monk Liu89e0ec92016-05-27 19:34:11 +0800942 if (adev->mode_info.atom_context) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400943 kfree(adev->mode_info.atom_context->scratch);
Monk Liu89e0ec92016-05-27 19:34:11 +0800944 kfree(adev->mode_info.atom_context->iio);
945 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400946 kfree(adev->mode_info.atom_context);
947 adev->mode_info.atom_context = NULL;
948 kfree(adev->mode_info.atom_card_info);
949 adev->mode_info.atom_card_info = NULL;
950}
951
952/**
953 * amdgpu_atombios_init - init the driver info and callbacks for atombios
954 *
955 * @adev: amdgpu_device pointer
956 *
957 * Initializes the driver info and register access callbacks for the
958 * ATOM interpreter (r4xx+).
959 * Returns 0 on sucess, -ENOMEM on failure.
960 * Called at driver startup.
961 */
962static int amdgpu_atombios_init(struct amdgpu_device *adev)
963{
964 struct card_info *atom_card_info =
965 kzalloc(sizeof(struct card_info), GFP_KERNEL);
966
967 if (!atom_card_info)
968 return -ENOMEM;
969
970 adev->mode_info.atom_card_info = atom_card_info;
971 atom_card_info->dev = adev->ddev;
972 atom_card_info->reg_read = cail_reg_read;
973 atom_card_info->reg_write = cail_reg_write;
974 /* needed for iio ops */
975 if (adev->rio_mem) {
976 atom_card_info->ioreg_read = cail_ioreg_read;
977 atom_card_info->ioreg_write = cail_ioreg_write;
978 } else {
Amber Linb64a18c2017-01-04 08:06:58 -0500979 DRM_INFO("PCI I/O BAR is not found. Using MMIO to access ATOM BIOS\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400980 atom_card_info->ioreg_read = cail_reg_read;
981 atom_card_info->ioreg_write = cail_reg_write;
982 }
983 atom_card_info->mc_read = cail_mc_read;
984 atom_card_info->mc_write = cail_mc_write;
985 atom_card_info->pll_read = cail_pll_read;
986 atom_card_info->pll_write = cail_pll_write;
987
988 adev->mode_info.atom_context = amdgpu_atom_parse(atom_card_info, adev->bios);
989 if (!adev->mode_info.atom_context) {
990 amdgpu_atombios_fini(adev);
991 return -ENOMEM;
992 }
993
994 mutex_init(&adev->mode_info.atom_context->mutex);
995 amdgpu_atombios_scratch_regs_init(adev);
996 amdgpu_atom_allocate_fb_scratch(adev->mode_info.atom_context);
997 return 0;
998}
999
1000/* if we get transitioned to only one device, take VGA back */
1001/**
1002 * amdgpu_vga_set_decode - enable/disable vga decode
1003 *
1004 * @cookie: amdgpu_device pointer
1005 * @state: enable/disable vga decode
1006 *
1007 * Enable/disable vga decode (all asics).
1008 * Returns VGA resource flags.
1009 */
1010static unsigned int amdgpu_vga_set_decode(void *cookie, bool state)
1011{
1012 struct amdgpu_device *adev = cookie;
1013 amdgpu_asic_set_vga_state(adev, state);
1014 if (state)
1015 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
1016 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1017 else
1018 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1019}
1020
1021/**
1022 * amdgpu_check_pot_argument - check that argument is a power of two
1023 *
1024 * @arg: value to check
1025 *
1026 * Validates that a certain argument is a power of two (all asics).
1027 * Returns true if argument is valid.
1028 */
1029static bool amdgpu_check_pot_argument(int arg)
1030{
1031 return (arg & (arg - 1)) == 0;
1032}
1033
1034/**
1035 * amdgpu_check_arguments - validate module params
1036 *
1037 * @adev: amdgpu_device pointer
1038 *
1039 * Validates certain module parameters and updates
1040 * the associated values used by the driver (all asics).
1041 */
1042static void amdgpu_check_arguments(struct amdgpu_device *adev)
1043{
Chunming Zhou5b011232015-12-10 17:34:33 +08001044 if (amdgpu_sched_jobs < 4) {
1045 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
1046 amdgpu_sched_jobs);
1047 amdgpu_sched_jobs = 4;
1048 } else if (!amdgpu_check_pot_argument(amdgpu_sched_jobs)){
1049 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
1050 amdgpu_sched_jobs);
1051 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
1052 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001053
1054 if (amdgpu_gart_size != -1) {
Christian Königc4e1a132016-03-17 16:25:15 +01001055 /* gtt size must be greater or equal to 32M */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001056 if (amdgpu_gart_size < 32) {
1057 dev_warn(adev->dev, "gart size (%d) too small\n",
1058 amdgpu_gart_size);
1059 amdgpu_gart_size = -1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001060 }
1061 }
1062
1063 if (!amdgpu_check_pot_argument(amdgpu_vm_size)) {
1064 dev_warn(adev->dev, "VM size (%d) must be a power of 2\n",
1065 amdgpu_vm_size);
Alex Deucher8dacc122015-05-11 16:20:58 -04001066 amdgpu_vm_size = 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001067 }
1068
1069 if (amdgpu_vm_size < 1) {
1070 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
1071 amdgpu_vm_size);
Alex Deucher8dacc122015-05-11 16:20:58 -04001072 amdgpu_vm_size = 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001073 }
1074
1075 /*
1076 * Max GPUVM size for Cayman, SI and CI are 40 bits.
1077 */
1078 if (amdgpu_vm_size > 1024) {
1079 dev_warn(adev->dev, "VM size (%d) too large, max is 1TB\n",
1080 amdgpu_vm_size);
Alex Deucher8dacc122015-05-11 16:20:58 -04001081 amdgpu_vm_size = 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001082 }
1083
1084 /* defines number of bits in page table versus page directory,
1085 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
1086 * page table and the remaining bits are in the page directory */
1087 if (amdgpu_vm_block_size == -1) {
1088
1089 /* Total bits covered by PD + PTs */
1090 unsigned bits = ilog2(amdgpu_vm_size) + 18;
1091
1092 /* Make sure the PD is 4K in size up to 8GB address space.
1093 Above that split equal between PD and PTs */
1094 if (amdgpu_vm_size <= 8)
1095 amdgpu_vm_block_size = bits - 9;
1096 else
1097 amdgpu_vm_block_size = (bits + 3) / 2;
1098
1099 } else if (amdgpu_vm_block_size < 9) {
1100 dev_warn(adev->dev, "VM page table size (%d) too small\n",
1101 amdgpu_vm_block_size);
1102 amdgpu_vm_block_size = 9;
1103 }
1104
1105 if (amdgpu_vm_block_size > 24 ||
1106 (amdgpu_vm_size * 1024) < (1ull << amdgpu_vm_block_size)) {
1107 dev_warn(adev->dev, "VM page table size (%d) too large\n",
1108 amdgpu_vm_block_size);
1109 amdgpu_vm_block_size = 9;
1110 }
Christian König6a7f76e2016-08-24 15:51:49 +02001111
jimqu526bae32016-11-07 09:53:10 +08001112 if (amdgpu_vram_page_split != -1 && (amdgpu_vram_page_split < 16 ||
1113 !amdgpu_check_pot_argument(amdgpu_vram_page_split))) {
Christian König6a7f76e2016-08-24 15:51:49 +02001114 dev_warn(adev->dev, "invalid VRAM page split (%d)\n",
1115 amdgpu_vram_page_split);
1116 amdgpu_vram_page_split = 1024;
1117 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001118}
1119
1120/**
1121 * amdgpu_switcheroo_set_state - set switcheroo state
1122 *
1123 * @pdev: pci dev pointer
Lukas Wunner16944672015-09-05 11:17:35 +02001124 * @state: vga_switcheroo state
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001125 *
1126 * Callback for the switcheroo driver. Suspends or resumes the
1127 * the asics before or after it is powered up using ACPI methods.
1128 */
1129static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1130{
1131 struct drm_device *dev = pci_get_drvdata(pdev);
1132
1133 if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
1134 return;
1135
1136 if (state == VGA_SWITCHEROO_ON) {
1137 unsigned d3_delay = dev->pdev->d3_delay;
1138
Joe Perches7ca85292017-02-28 04:55:52 -08001139 pr_info("amdgpu: switched on\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001140 /* don't suspend or resume card normally */
1141 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1142
Alex Deucher810ddc32016-08-23 13:25:49 -04001143 amdgpu_device_resume(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001144
1145 dev->pdev->d3_delay = d3_delay;
1146
1147 dev->switch_power_state = DRM_SWITCH_POWER_ON;
1148 drm_kms_helper_poll_enable(dev);
1149 } else {
Joe Perches7ca85292017-02-28 04:55:52 -08001150 pr_info("amdgpu: switched off\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001151 drm_kms_helper_poll_disable(dev);
1152 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Alex Deucher810ddc32016-08-23 13:25:49 -04001153 amdgpu_device_suspend(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001154 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1155 }
1156}
1157
1158/**
1159 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1160 *
1161 * @pdev: pci dev pointer
1162 *
1163 * Callback for the switcheroo driver. Check of the switcheroo
1164 * state can be changed.
1165 * Returns true if the state can be changed, false if not.
1166 */
1167static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1168{
1169 struct drm_device *dev = pci_get_drvdata(pdev);
1170
1171 /*
1172 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1173 * locking inversion with the driver load path. And the access here is
1174 * completely racy anyway. So don't bother with locking for now.
1175 */
1176 return dev->open_count == 0;
1177}
1178
1179static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1180 .set_gpu_state = amdgpu_switcheroo_set_state,
1181 .reprobe = NULL,
1182 .can_switch = amdgpu_switcheroo_can_switch,
1183};
1184
1185int amdgpu_set_clockgating_state(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001186 enum amd_ip_block_type block_type,
1187 enum amd_clockgating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001188{
1189 int i, r = 0;
1190
1191 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001192 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001193 continue;
Rex Zhuc7228652017-02-22 15:33:46 +08001194 if (adev->ip_blocks[i].version->type != block_type)
1195 continue;
1196 if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
1197 continue;
1198 r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
1199 (void *)adev, state);
1200 if (r)
1201 DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
1202 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001203 }
1204 return r;
1205}
1206
1207int amdgpu_set_powergating_state(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001208 enum amd_ip_block_type block_type,
1209 enum amd_powergating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001210{
1211 int i, r = 0;
1212
1213 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001214 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001215 continue;
Rex Zhuc7228652017-02-22 15:33:46 +08001216 if (adev->ip_blocks[i].version->type != block_type)
1217 continue;
1218 if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
1219 continue;
1220 r = adev->ip_blocks[i].version->funcs->set_powergating_state(
1221 (void *)adev, state);
1222 if (r)
1223 DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
1224 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001225 }
1226 return r;
1227}
1228
Huang Rui6cb2d4e2017-01-05 18:44:41 +08001229void amdgpu_get_clockgating_state(struct amdgpu_device *adev, u32 *flags)
1230{
1231 int i;
1232
1233 for (i = 0; i < adev->num_ip_blocks; i++) {
1234 if (!adev->ip_blocks[i].status.valid)
1235 continue;
1236 if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
1237 adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
1238 }
1239}
1240
Alex Deucher5dbbb602016-06-23 11:41:04 -04001241int amdgpu_wait_for_idle(struct amdgpu_device *adev,
1242 enum amd_ip_block_type block_type)
1243{
1244 int i, r;
1245
1246 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001247 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001248 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001249 if (adev->ip_blocks[i].version->type == block_type) {
1250 r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -04001251 if (r)
1252 return r;
1253 break;
1254 }
1255 }
1256 return 0;
1257
1258}
1259
1260bool amdgpu_is_idle(struct amdgpu_device *adev,
1261 enum amd_ip_block_type block_type)
1262{
1263 int i;
1264
1265 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001266 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001267 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001268 if (adev->ip_blocks[i].version->type == block_type)
1269 return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -04001270 }
1271 return true;
1272
1273}
1274
Alex Deuchera1255102016-10-13 17:41:13 -04001275struct amdgpu_ip_block * amdgpu_get_ip_block(struct amdgpu_device *adev,
1276 enum amd_ip_block_type type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001277{
1278 int i;
1279
1280 for (i = 0; i < adev->num_ip_blocks; i++)
Alex Deuchera1255102016-10-13 17:41:13 -04001281 if (adev->ip_blocks[i].version->type == type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001282 return &adev->ip_blocks[i];
1283
1284 return NULL;
1285}
1286
1287/**
1288 * amdgpu_ip_block_version_cmp
1289 *
1290 * @adev: amdgpu_device pointer
yanyang15fc3aee2015-05-22 14:39:35 -04001291 * @type: enum amd_ip_block_type
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001292 * @major: major version
1293 * @minor: minor version
1294 *
1295 * return 0 if equal or greater
1296 * return 1 if smaller or the ip_block doesn't exist
1297 */
1298int amdgpu_ip_block_version_cmp(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001299 enum amd_ip_block_type type,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001300 u32 major, u32 minor)
1301{
Alex Deuchera1255102016-10-13 17:41:13 -04001302 struct amdgpu_ip_block *ip_block = amdgpu_get_ip_block(adev, type);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001303
Alex Deuchera1255102016-10-13 17:41:13 -04001304 if (ip_block && ((ip_block->version->major > major) ||
1305 ((ip_block->version->major == major) &&
1306 (ip_block->version->minor >= minor))))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001307 return 0;
1308
1309 return 1;
1310}
1311
Alex Deuchera1255102016-10-13 17:41:13 -04001312/**
1313 * amdgpu_ip_block_add
1314 *
1315 * @adev: amdgpu_device pointer
1316 * @ip_block_version: pointer to the IP to add
1317 *
1318 * Adds the IP block driver information to the collection of IPs
1319 * on the asic.
1320 */
1321int amdgpu_ip_block_add(struct amdgpu_device *adev,
1322 const struct amdgpu_ip_block_version *ip_block_version)
1323{
1324 if (!ip_block_version)
1325 return -EINVAL;
1326
1327 adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
1328
1329 return 0;
1330}
1331
Alex Deucher483ef982016-09-30 12:43:04 -04001332static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
Emily Deng9accf2f2016-08-10 16:01:25 +08001333{
1334 adev->enable_virtual_display = false;
1335
1336 if (amdgpu_virtual_display) {
1337 struct drm_device *ddev = adev->ddev;
1338 const char *pci_address_name = pci_name(ddev->pdev);
Emily Deng0f663562016-09-30 13:02:18 -04001339 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
Emily Deng9accf2f2016-08-10 16:01:25 +08001340
1341 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1342 pciaddstr_tmp = pciaddstr;
Emily Deng0f663562016-09-30 13:02:18 -04001343 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1344 pciaddname = strsep(&pciaddname_tmp, ",");
Yintian Tao967de2a2017-01-22 15:16:51 +08001345 if (!strcmp("all", pciaddname)
1346 || !strcmp(pci_address_name, pciaddname)) {
Emily Deng0f663562016-09-30 13:02:18 -04001347 long num_crtc;
1348 int res = -1;
1349
Emily Deng9accf2f2016-08-10 16:01:25 +08001350 adev->enable_virtual_display = true;
Emily Deng0f663562016-09-30 13:02:18 -04001351
1352 if (pciaddname_tmp)
1353 res = kstrtol(pciaddname_tmp, 10,
1354 &num_crtc);
1355
1356 if (!res) {
1357 if (num_crtc < 1)
1358 num_crtc = 1;
1359 if (num_crtc > 6)
1360 num_crtc = 6;
1361 adev->mode_info.num_crtc = num_crtc;
1362 } else {
1363 adev->mode_info.num_crtc = 1;
1364 }
Emily Deng9accf2f2016-08-10 16:01:25 +08001365 break;
1366 }
1367 }
1368
Emily Deng0f663562016-09-30 13:02:18 -04001369 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
1370 amdgpu_virtual_display, pci_address_name,
1371 adev->enable_virtual_display, adev->mode_info.num_crtc);
Emily Deng9accf2f2016-08-10 16:01:25 +08001372
1373 kfree(pciaddstr);
1374 }
1375}
1376
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001377static int amdgpu_early_init(struct amdgpu_device *adev)
1378{
Alex Deucheraaa36a92015-04-20 17:31:14 -04001379 int i, r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001380
Alex Deucher483ef982016-09-30 12:43:04 -04001381 amdgpu_device_enable_virtual_display(adev);
Emily Denga6be7572016-08-08 11:37:50 +08001382
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001383 switch (adev->asic_type) {
Alex Deucheraaa36a92015-04-20 17:31:14 -04001384 case CHIP_TOPAZ:
1385 case CHIP_TONGA:
David Zhang48299f92015-07-08 01:05:16 +08001386 case CHIP_FIJI:
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001387 case CHIP_POLARIS11:
1388 case CHIP_POLARIS10:
Junwei Zhangc4642a42016-12-14 15:32:28 -05001389 case CHIP_POLARIS12:
Alex Deucheraaa36a92015-04-20 17:31:14 -04001390 case CHIP_CARRIZO:
Samuel Li39bb0c92015-10-08 16:31:43 -04001391 case CHIP_STONEY:
1392 if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
Alex Deucheraaa36a92015-04-20 17:31:14 -04001393 adev->family = AMDGPU_FAMILY_CZ;
1394 else
1395 adev->family = AMDGPU_FAMILY_VI;
1396
1397 r = vi_set_ip_blocks(adev);
1398 if (r)
1399 return r;
1400 break;
Ken Wang33f34802016-01-21 17:29:41 +08001401#ifdef CONFIG_DRM_AMDGPU_SI
1402 case CHIP_VERDE:
1403 case CHIP_TAHITI:
1404 case CHIP_PITCAIRN:
1405 case CHIP_OLAND:
1406 case CHIP_HAINAN:
Ken Wang295d0da2016-05-24 21:02:53 +08001407 adev->family = AMDGPU_FAMILY_SI;
Ken Wang33f34802016-01-21 17:29:41 +08001408 r = si_set_ip_blocks(adev);
1409 if (r)
1410 return r;
1411 break;
1412#endif
Alex Deuchera2e73f52015-04-20 17:09:27 -04001413#ifdef CONFIG_DRM_AMDGPU_CIK
1414 case CHIP_BONAIRE:
1415 case CHIP_HAWAII:
1416 case CHIP_KAVERI:
1417 case CHIP_KABINI:
1418 case CHIP_MULLINS:
1419 if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
1420 adev->family = AMDGPU_FAMILY_CI;
1421 else
1422 adev->family = AMDGPU_FAMILY_KV;
1423
1424 r = cik_set_ip_blocks(adev);
1425 if (r)
1426 return r;
1427 break;
1428#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001429 default:
1430 /* FIXME: not supported yet */
1431 return -EINVAL;
1432 }
1433
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001434 if (amdgpu_sriov_vf(adev)) {
1435 r = amdgpu_virt_request_full_gpu(adev, true);
1436 if (r)
1437 return r;
1438 }
1439
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001440 for (i = 0; i < adev->num_ip_blocks; i++) {
1441 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
1442 DRM_ERROR("disabled ip block: %d\n", i);
Alex Deuchera1255102016-10-13 17:41:13 -04001443 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001444 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001445 if (adev->ip_blocks[i].version->funcs->early_init) {
1446 r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001447 if (r == -ENOENT) {
Alex Deuchera1255102016-10-13 17:41:13 -04001448 adev->ip_blocks[i].status.valid = false;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001449 } else if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001450 DRM_ERROR("early_init of IP block <%s> failed %d\n",
1451 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001452 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001453 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001454 adev->ip_blocks[i].status.valid = true;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001455 }
Alex Deucher974e6b62015-07-10 13:59:44 -04001456 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001457 adev->ip_blocks[i].status.valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001458 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001459 }
1460 }
1461
Nicolai Hähnle395d1fb2016-06-02 12:32:07 +02001462 adev->cg_flags &= amdgpu_cg_mask;
1463 adev->pg_flags &= amdgpu_pg_mask;
1464
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001465 return 0;
1466}
1467
1468static int amdgpu_init(struct amdgpu_device *adev)
1469{
1470 int i, r;
1471
1472 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001473 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001474 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001475 r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001476 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001477 DRM_ERROR("sw_init of IP block <%s> failed %d\n",
1478 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001479 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001480 }
Alex Deuchera1255102016-10-13 17:41:13 -04001481 adev->ip_blocks[i].status.sw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001482 /* need to do gmc hw init early so we can allocate gpu mem */
Alex Deuchera1255102016-10-13 17:41:13 -04001483 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001484 r = amdgpu_vram_scratch_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001485 if (r) {
1486 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001487 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001488 }
Alex Deuchera1255102016-10-13 17:41:13 -04001489 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001490 if (r) {
1491 DRM_ERROR("hw_init %d failed %d\n", i, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001492 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001493 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001494 r = amdgpu_wb_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001495 if (r) {
1496 DRM_ERROR("amdgpu_wb_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001497 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001498 }
Alex Deuchera1255102016-10-13 17:41:13 -04001499 adev->ip_blocks[i].status.hw = true;
Monk Liu24936642017-01-09 15:54:32 +08001500
1501 /* right after GMC hw init, we create CSA */
1502 if (amdgpu_sriov_vf(adev)) {
1503 r = amdgpu_allocate_static_csa(adev);
1504 if (r) {
1505 DRM_ERROR("allocate CSA failed %d\n", r);
1506 return r;
1507 }
1508 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001509 }
1510 }
1511
1512 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001513 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001514 continue;
1515 /* gmc hw init is done early */
Alex Deuchera1255102016-10-13 17:41:13 -04001516 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001517 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001518 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001519 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001520 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
1521 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001522 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001523 }
Alex Deuchera1255102016-10-13 17:41:13 -04001524 adev->ip_blocks[i].status.hw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001525 }
1526
1527 return 0;
1528}
1529
1530static int amdgpu_late_init(struct amdgpu_device *adev)
1531{
1532 int i = 0, r;
1533
1534 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001535 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001536 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001537 if (adev->ip_blocks[i].version->funcs->late_init) {
1538 r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001539 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001540 DRM_ERROR("late_init of IP block <%s> failed %d\n",
1541 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001542 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001543 }
Alex Deuchera1255102016-10-13 17:41:13 -04001544 adev->ip_blocks[i].status.late_initialized = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001545 }
Alex Deucher4a446d52016-10-07 14:48:18 -04001546 /* skip CG for VCE/UVD, it's handled specially */
Alex Deuchera1255102016-10-13 17:41:13 -04001547 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1548 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
Alex Deucher4a446d52016-10-07 14:48:18 -04001549 /* enable clockgating to save power */
Alex Deuchera1255102016-10-13 17:41:13 -04001550 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1551 AMD_CG_STATE_GATE);
Alex Deucher4a446d52016-10-07 14:48:18 -04001552 if (r) {
1553 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001554 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher4a446d52016-10-07 14:48:18 -04001555 return r;
1556 }
Arindam Nathb0b00ff2016-10-07 19:01:37 +05301557 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001558 }
1559
1560 return 0;
1561}
1562
1563static int amdgpu_fini(struct amdgpu_device *adev)
1564{
1565 int i, r;
1566
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001567 /* need to disable SMC first */
1568 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001569 if (!adev->ip_blocks[i].status.hw)
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001570 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001571 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001572 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
Alex Deuchera1255102016-10-13 17:41:13 -04001573 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1574 AMD_CG_STATE_UNGATE);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001575 if (r) {
1576 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001577 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001578 return r;
1579 }
Alex Deuchera1255102016-10-13 17:41:13 -04001580 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001581 /* XXX handle errors */
1582 if (r) {
1583 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001584 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001585 }
Alex Deuchera1255102016-10-13 17:41:13 -04001586 adev->ip_blocks[i].status.hw = false;
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001587 break;
1588 }
1589 }
1590
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001591 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001592 if (!adev->ip_blocks[i].status.hw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001593 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001594 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001595 amdgpu_wb_fini(adev);
1596 amdgpu_vram_scratch_fini(adev);
1597 }
Rex Zhu8201a672016-11-24 21:44:44 +08001598
1599 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1600 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1601 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
1602 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1603 AMD_CG_STATE_UNGATE);
1604 if (r) {
1605 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1606 adev->ip_blocks[i].version->funcs->name, r);
1607 return r;
1608 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001609 }
Rex Zhu8201a672016-11-24 21:44:44 +08001610
Alex Deuchera1255102016-10-13 17:41:13 -04001611 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001612 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001613 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001614 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
1615 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001616 }
Rex Zhu8201a672016-11-24 21:44:44 +08001617
Alex Deuchera1255102016-10-13 17:41:13 -04001618 adev->ip_blocks[i].status.hw = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001619 }
1620
1621 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001622 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001623 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001624 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001625 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001626 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001627 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
1628 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001629 }
Alex Deuchera1255102016-10-13 17:41:13 -04001630 adev->ip_blocks[i].status.sw = false;
1631 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001632 }
1633
Monk Liua6dcfd92016-05-19 14:36:34 +08001634 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001635 if (!adev->ip_blocks[i].status.late_initialized)
Grazvydas Ignotas8a2eef12016-10-03 00:06:44 +03001636 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001637 if (adev->ip_blocks[i].version->funcs->late_fini)
1638 adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
1639 adev->ip_blocks[i].status.late_initialized = false;
Monk Liua6dcfd92016-05-19 14:36:34 +08001640 }
1641
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001642 if (amdgpu_sriov_vf(adev)) {
Monk Liu24936642017-01-09 15:54:32 +08001643 amdgpu_bo_free_kernel(&adev->virt.csa_obj, &adev->virt.csa_vmid0_addr, NULL);
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001644 amdgpu_virt_release_full_gpu(adev, false);
1645 }
Monk Liu24936642017-01-09 15:54:32 +08001646
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001647 return 0;
1648}
1649
Alex Deucherfaefba92016-12-06 10:38:29 -05001650int amdgpu_suspend(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001651{
1652 int i, r;
1653
Xiangliang Yue941ea92017-01-18 12:47:55 +08001654 if (amdgpu_sriov_vf(adev))
1655 amdgpu_virt_request_full_gpu(adev, false);
1656
Flora Cuic5a93a22016-02-26 10:45:25 +08001657 /* ungate SMC block first */
1658 r = amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC,
1659 AMD_CG_STATE_UNGATE);
1660 if (r) {
1661 DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n",r);
1662 }
1663
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001664 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001665 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001666 continue;
1667 /* ungate blocks so that suspend can properly shut them down */
Flora Cuic5a93a22016-02-26 10:45:25 +08001668 if (i != AMD_IP_BLOCK_TYPE_SMC) {
Alex Deuchera1255102016-10-13 17:41:13 -04001669 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1670 AMD_CG_STATE_UNGATE);
Flora Cuic5a93a22016-02-26 10:45:25 +08001671 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001672 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1673 adev->ip_blocks[i].version->funcs->name, r);
Flora Cuic5a93a22016-02-26 10:45:25 +08001674 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001675 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001676 /* XXX handle errors */
Alex Deuchera1255102016-10-13 17:41:13 -04001677 r = adev->ip_blocks[i].version->funcs->suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001678 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001679 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001680 DRM_ERROR("suspend of IP block <%s> failed %d\n",
1681 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001682 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001683 }
1684
Xiangliang Yue941ea92017-01-18 12:47:55 +08001685 if (amdgpu_sriov_vf(adev))
1686 amdgpu_virt_release_full_gpu(adev, false);
1687
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001688 return 0;
1689}
1690
Monk Liue4f0fdc2017-02-09 11:55:49 +08001691static int amdgpu_sriov_reinit_early(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08001692{
1693 int i, r;
1694
1695 for (i = 0; i < adev->num_ip_blocks; i++) {
1696 if (!adev->ip_blocks[i].status.valid)
1697 continue;
1698
1699 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
1700 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
1701 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH)
Monk Liue4f0fdc2017-02-09 11:55:49 +08001702 r = adev->ip_blocks[i].version->funcs->hw_init(adev);
Monk Liua90ad3c2017-01-23 14:22:08 +08001703
1704 if (r) {
1705 DRM_ERROR("resume of IP block <%s> failed %d\n",
1706 adev->ip_blocks[i].version->funcs->name, r);
1707 return r;
1708 }
1709 }
1710
1711 return 0;
1712}
1713
Monk Liue4f0fdc2017-02-09 11:55:49 +08001714static int amdgpu_sriov_reinit_late(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08001715{
1716 int i, r;
1717
1718 for (i = 0; i < adev->num_ip_blocks; i++) {
1719 if (!adev->ip_blocks[i].status.valid)
1720 continue;
1721
1722 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
1723 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
1724 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH )
1725 continue;
1726
Monk Liue4f0fdc2017-02-09 11:55:49 +08001727 r = adev->ip_blocks[i].version->funcs->hw_init(adev);
Monk Liua90ad3c2017-01-23 14:22:08 +08001728 if (r) {
1729 DRM_ERROR("resume of IP block <%s> failed %d\n",
1730 adev->ip_blocks[i].version->funcs->name, r);
1731 return r;
1732 }
1733 }
1734
1735 return 0;
1736}
1737
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001738static int amdgpu_resume(struct amdgpu_device *adev)
1739{
1740 int i, r;
1741
1742 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001743 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001744 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001745 r = adev->ip_blocks[i].version->funcs->resume(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001746 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001747 DRM_ERROR("resume of IP block <%s> failed %d\n",
1748 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001749 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001750 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001751 }
1752
1753 return 0;
1754}
1755
Monk Liu4e99a442016-03-31 13:26:59 +08001756static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
Andres Rodriguez048765a2016-06-11 02:51:32 -04001757{
Monk Liu4e99a442016-03-31 13:26:59 +08001758 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
Xiangliang Yu5a5099c2017-01-09 18:06:57 -05001759 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
Andres Rodriguez048765a2016-06-11 02:51:32 -04001760}
1761
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001762/**
1763 * amdgpu_device_init - initialize the driver
1764 *
1765 * @adev: amdgpu_device pointer
1766 * @pdev: drm dev pointer
1767 * @pdev: pci dev pointer
1768 * @flags: driver flags
1769 *
1770 * Initializes the driver info and hw (all asics).
1771 * Returns 0 for success or an error on failure.
1772 * Called at driver startup.
1773 */
1774int amdgpu_device_init(struct amdgpu_device *adev,
1775 struct drm_device *ddev,
1776 struct pci_dev *pdev,
1777 uint32_t flags)
1778{
1779 int r, i;
1780 bool runtime = false;
Marek Olšák95844d22016-08-17 23:49:27 +02001781 u32 max_MBps;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001782
1783 adev->shutdown = false;
1784 adev->dev = &pdev->dev;
1785 adev->ddev = ddev;
1786 adev->pdev = pdev;
1787 adev->flags = flags;
Jammy Zhou2f7d10b2015-07-22 11:29:01 +08001788 adev->asic_type = flags & AMD_ASIC_MASK;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001789 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
1790 adev->mc.gtt_size = 512 * 1024 * 1024;
1791 adev->accel_working = false;
1792 adev->num_rings = 0;
1793 adev->mman.buffer_funcs = NULL;
1794 adev->mman.buffer_funcs_ring = NULL;
1795 adev->vm_manager.vm_pte_funcs = NULL;
Christian König2d55e452016-02-08 17:37:38 +01001796 adev->vm_manager.vm_pte_num_rings = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001797 adev->gart.gart_funcs = NULL;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001798 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001799
1800 adev->smc_rreg = &amdgpu_invalid_rreg;
1801 adev->smc_wreg = &amdgpu_invalid_wreg;
1802 adev->pcie_rreg = &amdgpu_invalid_rreg;
1803 adev->pcie_wreg = &amdgpu_invalid_wreg;
Huang Rui36b9a952016-08-31 13:23:25 +08001804 adev->pciep_rreg = &amdgpu_invalid_rreg;
1805 adev->pciep_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001806 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
1807 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
1808 adev->didt_rreg = &amdgpu_invalid_rreg;
1809 adev->didt_wreg = &amdgpu_invalid_wreg;
Rex Zhuccdbb202016-06-08 12:47:41 +08001810 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
1811 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001812 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
1813 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
1814
Rex Zhuccdbb202016-06-08 12:47:41 +08001815
Alex Deucher3e39ab92015-06-05 15:04:33 -04001816 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
1817 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
1818 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001819
1820 /* mutex initialization are all done here so we
1821 * can recall function without having locking issues */
Christian König8d0a7ce2015-11-03 20:58:50 +01001822 mutex_init(&adev->vm_manager.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001823 atomic_set(&adev->irq.ih.lock, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001824 mutex_init(&adev->pm.mutex);
1825 mutex_init(&adev->gfx.gpu_clock_mutex);
1826 mutex_init(&adev->srbm_mutex);
1827 mutex_init(&adev->grbm_idx_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001828 mutex_init(&adev->mn_lock);
1829 hash_init(adev->mn_hash);
1830
1831 amdgpu_check_arguments(adev);
1832
1833 /* Registers mapping */
1834 /* TODO: block userspace mapping of io register */
1835 spin_lock_init(&adev->mmio_idx_lock);
1836 spin_lock_init(&adev->smc_idx_lock);
1837 spin_lock_init(&adev->pcie_idx_lock);
1838 spin_lock_init(&adev->uvd_ctx_idx_lock);
1839 spin_lock_init(&adev->didt_idx_lock);
Rex Zhuccdbb202016-06-08 12:47:41 +08001840 spin_lock_init(&adev->gc_cac_idx_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001841 spin_lock_init(&adev->audio_endpt_idx_lock);
Marek Olšák95844d22016-08-17 23:49:27 +02001842 spin_lock_init(&adev->mm_stats.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001843
Chunming Zhou0c4e7fa2016-08-17 11:41:30 +08001844 INIT_LIST_HEAD(&adev->shadow_list);
1845 mutex_init(&adev->shadow_list_lock);
1846
Chunming Zhou5c1354b2016-08-30 16:13:10 +08001847 INIT_LIST_HEAD(&adev->gtt_list);
1848 spin_lock_init(&adev->gtt_list_lock);
1849
Ken Wangda69c1612016-01-21 19:08:55 +08001850 if (adev->asic_type >= CHIP_BONAIRE) {
1851 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
1852 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
1853 } else {
1854 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
1855 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
1856 }
Chunming Zhou5c1354b2016-08-30 16:13:10 +08001857
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001858 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
1859 if (adev->rmmio == NULL) {
1860 return -ENOMEM;
1861 }
1862 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
1863 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
1864
Ken Wangda69c1612016-01-21 19:08:55 +08001865 if (adev->asic_type >= CHIP_BONAIRE)
1866 /* doorbell bar mapping */
1867 amdgpu_doorbell_init(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001868
1869 /* io port mapping */
1870 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
1871 if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
1872 adev->rio_mem_size = pci_resource_len(adev->pdev, i);
1873 adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
1874 break;
1875 }
1876 }
1877 if (adev->rio_mem == NULL)
Amber Linb64a18c2017-01-04 08:06:58 -05001878 DRM_INFO("PCI I/O BAR is not found.\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001879
1880 /* early init functions */
1881 r = amdgpu_early_init(adev);
1882 if (r)
1883 return r;
1884
1885 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
1886 /* this will fail for cards that aren't VGA class devices, just
1887 * ignore it */
1888 vga_client_register(adev->pdev, adev, NULL, amdgpu_vga_set_decode);
1889
1890 if (amdgpu_runtime_pm == 1)
1891 runtime = true;
Alex Deuchere9bef452016-04-25 13:12:18 -04001892 if (amdgpu_device_is_px(ddev))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001893 runtime = true;
1894 vga_switcheroo_register_client(adev->pdev, &amdgpu_switcheroo_ops, runtime);
1895 if (runtime)
1896 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
1897
1898 /* Read BIOS */
Alex Deucher83ba1262016-06-03 18:21:41 -04001899 if (!amdgpu_get_bios(adev)) {
1900 r = -EINVAL;
1901 goto failed;
1902 }
Nils Wallméniusf7e9e9f2016-12-14 21:52:45 +01001903
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001904 r = amdgpu_atombios_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001905 if (r) {
1906 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001907 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001908 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001909
Monk Liu4e99a442016-03-31 13:26:59 +08001910 /* detect if we are with an SRIOV vbios */
1911 amdgpu_device_detect_sriov_bios(adev);
Andres Rodriguez048765a2016-06-11 02:51:32 -04001912
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001913 /* Post card if necessary */
Monk Liubec86372016-09-14 19:38:08 +08001914 if (amdgpu_vpost_needed(adev)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001915 if (!adev->bios) {
Monk Liubec86372016-09-14 19:38:08 +08001916 dev_err(adev->dev, "no vBIOS found\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001917 r = -EINVAL;
1918 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001919 }
Monk Liubec86372016-09-14 19:38:08 +08001920 DRM_INFO("GPU posting now...\n");
Monk Liu4e99a442016-03-31 13:26:59 +08001921 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
1922 if (r) {
1923 dev_err(adev->dev, "gpu post error!\n");
1924 goto failed;
1925 }
1926 } else {
1927 DRM_INFO("GPU post is not needed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001928 }
1929
1930 /* Initialize clocks */
1931 r = amdgpu_atombios_get_clock_info(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001932 if (r) {
1933 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001934 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001935 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001936 /* init i2c buses */
1937 amdgpu_atombios_i2c_init(adev);
1938
1939 /* Fence driver */
1940 r = amdgpu_fence_driver_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001941 if (r) {
1942 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001943 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001944 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001945
1946 /* init the mode config */
1947 drm_mode_config_init(adev->ddev);
1948
1949 r = amdgpu_init(adev);
1950 if (r) {
Alex Deucher2c1a2782015-12-07 17:02:53 -05001951 dev_err(adev->dev, "amdgpu_init failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001952 amdgpu_fini(adev);
Alex Deucher83ba1262016-06-03 18:21:41 -04001953 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001954 }
1955
1956 adev->accel_working = true;
1957
Marek Olšák95844d22016-08-17 23:49:27 +02001958 /* Initialize the buffer migration limit. */
1959 if (amdgpu_moverate >= 0)
1960 max_MBps = amdgpu_moverate;
1961 else
1962 max_MBps = 8; /* Allow 8 MB/s. */
1963 /* Get a log2 for easy divisions. */
1964 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
1965
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001966 r = amdgpu_ib_pool_init(adev);
1967 if (r) {
1968 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
Alex Deucher83ba1262016-06-03 18:21:41 -04001969 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001970 }
1971
1972 r = amdgpu_ib_ring_tests(adev);
1973 if (r)
1974 DRM_ERROR("ib ring test failed (%d).\n", r);
1975
Monk Liu9bc92b92017-02-08 17:38:13 +08001976 amdgpu_fbdev_init(adev);
1977
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001978 r = amdgpu_gem_debugfs_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08001979 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001980 DRM_ERROR("registering gem debugfs failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001981
1982 r = amdgpu_debugfs_regs_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08001983 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001984 DRM_ERROR("registering register debugfs failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001985
Huang Rui50ab2532016-06-12 15:51:09 +08001986 r = amdgpu_debugfs_firmware_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08001987 if (r)
Huang Rui50ab2532016-06-12 15:51:09 +08001988 DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
Huang Rui50ab2532016-06-12 15:51:09 +08001989
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001990 if ((amdgpu_testing & 1)) {
1991 if (adev->accel_working)
1992 amdgpu_test_moves(adev);
1993 else
1994 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
1995 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001996 if (amdgpu_benchmarking) {
1997 if (adev->accel_working)
1998 amdgpu_benchmark(adev, amdgpu_benchmarking);
1999 else
2000 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
2001 }
2002
2003 /* enable clockgating, etc. after ib tests, etc. since some blocks require
2004 * explicit gating rather than handling it automatically.
2005 */
2006 r = amdgpu_late_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002007 if (r) {
2008 dev_err(adev->dev, "amdgpu_late_init failed\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04002009 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002010 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002011
2012 return 0;
Alex Deucher83ba1262016-06-03 18:21:41 -04002013
2014failed:
2015 if (runtime)
2016 vga_switcheroo_fini_domain_pm_ops(adev->dev);
2017 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002018}
2019
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002020/**
2021 * amdgpu_device_fini - tear down the driver
2022 *
2023 * @adev: amdgpu_device pointer
2024 *
2025 * Tear down the driver info (all asics).
2026 * Called at driver shutdown.
2027 */
2028void amdgpu_device_fini(struct amdgpu_device *adev)
2029{
2030 int r;
2031
2032 DRM_INFO("amdgpu: finishing device.\n");
2033 adev->shutdown = true;
Grazvydas Ignotasa951ed82016-09-25 23:34:48 +03002034 drm_crtc_force_disable_all(adev->ddev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002035 /* evict vram memory */
2036 amdgpu_bo_evict_vram(adev);
2037 amdgpu_ib_pool_fini(adev);
2038 amdgpu_fence_driver_fini(adev);
2039 amdgpu_fbdev_fini(adev);
2040 r = amdgpu_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002041 adev->accel_working = false;
2042 /* free i2c buses */
2043 amdgpu_i2c_fini(adev);
2044 amdgpu_atombios_fini(adev);
2045 kfree(adev->bios);
2046 adev->bios = NULL;
2047 vga_switcheroo_unregister_client(adev->pdev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002048 if (adev->flags & AMD_IS_PX)
2049 vga_switcheroo_fini_domain_pm_ops(adev->dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002050 vga_client_register(adev->pdev, NULL, NULL, NULL);
2051 if (adev->rio_mem)
2052 pci_iounmap(adev->pdev, adev->rio_mem);
2053 adev->rio_mem = NULL;
2054 iounmap(adev->rmmio);
2055 adev->rmmio = NULL;
Ken Wangda69c1612016-01-21 19:08:55 +08002056 if (adev->asic_type >= CHIP_BONAIRE)
2057 amdgpu_doorbell_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002058 amdgpu_debugfs_regs_cleanup(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002059}
2060
2061
2062/*
2063 * Suspend & resume.
2064 */
2065/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002066 * amdgpu_device_suspend - initiate device suspend
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002067 *
2068 * @pdev: drm dev pointer
2069 * @state: suspend state
2070 *
2071 * Puts the hw in the suspend state (all asics).
2072 * Returns 0 for success or an error on failure.
2073 * Called at driver suspend.
2074 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002075int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002076{
2077 struct amdgpu_device *adev;
2078 struct drm_crtc *crtc;
2079 struct drm_connector *connector;
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002080 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002081
2082 if (dev == NULL || dev->dev_private == NULL) {
2083 return -ENODEV;
2084 }
2085
2086 adev = dev->dev_private;
2087
2088 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2089 return 0;
2090
2091 drm_kms_helper_poll_disable(dev);
2092
2093 /* turn off display hw */
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002094 drm_modeset_lock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002095 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2096 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
2097 }
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002098 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002099
Alex Deucher756e6882015-10-08 00:03:36 -04002100 /* unpin the front buffers and cursors */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002101 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Alex Deucher756e6882015-10-08 00:03:36 -04002102 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002103 struct amdgpu_framebuffer *rfb = to_amdgpu_framebuffer(crtc->primary->fb);
2104 struct amdgpu_bo *robj;
2105
Alex Deucher756e6882015-10-08 00:03:36 -04002106 if (amdgpu_crtc->cursor_bo) {
2107 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
2108 r = amdgpu_bo_reserve(aobj, false);
2109 if (r == 0) {
2110 amdgpu_bo_unpin(aobj);
2111 amdgpu_bo_unreserve(aobj);
2112 }
2113 }
2114
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002115 if (rfb == NULL || rfb->obj == NULL) {
2116 continue;
2117 }
2118 robj = gem_to_amdgpu_bo(rfb->obj);
2119 /* don't unpin kernel fb objects */
2120 if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
2121 r = amdgpu_bo_reserve(robj, false);
2122 if (r == 0) {
2123 amdgpu_bo_unpin(robj);
2124 amdgpu_bo_unreserve(robj);
2125 }
2126 }
2127 }
2128 /* evict vram memory */
2129 amdgpu_bo_evict_vram(adev);
2130
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002131 amdgpu_fence_driver_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002132
2133 r = amdgpu_suspend(adev);
2134
Alex Deuchera0a71e42016-10-10 12:41:36 -04002135 /* evict remaining vram memory
2136 * This second call to evict vram is to evict the gart page table
2137 * using the CPU.
2138 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002139 amdgpu_bo_evict_vram(adev);
2140
Alex Deuchere695e772016-10-19 14:40:58 -04002141 amdgpu_atombios_scratch_regs_save(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002142 pci_save_state(dev->pdev);
2143 if (suspend) {
2144 /* Shut down the device */
2145 pci_disable_device(dev->pdev);
2146 pci_set_power_state(dev->pdev, PCI_D3hot);
jimqu74b0b152016-09-07 17:09:12 +08002147 } else {
2148 r = amdgpu_asic_reset(adev);
2149 if (r)
2150 DRM_ERROR("amdgpu asic reset failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002151 }
2152
2153 if (fbcon) {
2154 console_lock();
2155 amdgpu_fbdev_set_suspend(adev, 1);
2156 console_unlock();
2157 }
2158 return 0;
2159}
2160
2161/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002162 * amdgpu_device_resume - initiate device resume
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002163 *
2164 * @pdev: drm dev pointer
2165 *
2166 * Bring the hw back to operating state (all asics).
2167 * Returns 0 for success or an error on failure.
2168 * Called at driver resume.
2169 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002170int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002171{
2172 struct drm_connector *connector;
2173 struct amdgpu_device *adev = dev->dev_private;
Alex Deucher756e6882015-10-08 00:03:36 -04002174 struct drm_crtc *crtc;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002175 int r;
2176
2177 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2178 return 0;
2179
jimqu74b0b152016-09-07 17:09:12 +08002180 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002181 console_lock();
jimqu74b0b152016-09-07 17:09:12 +08002182
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002183 if (resume) {
2184 pci_set_power_state(dev->pdev, PCI_D0);
2185 pci_restore_state(dev->pdev);
jimqu74b0b152016-09-07 17:09:12 +08002186 r = pci_enable_device(dev->pdev);
2187 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002188 if (fbcon)
2189 console_unlock();
jimqu74b0b152016-09-07 17:09:12 +08002190 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002191 }
2192 }
Alex Deuchere695e772016-10-19 14:40:58 -04002193 amdgpu_atombios_scratch_regs_restore(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002194
2195 /* post card */
Jim Quc836fec2017-02-10 15:59:59 +08002196 if (amdgpu_need_post(adev)) {
jimqu74b0b152016-09-07 17:09:12 +08002197 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2198 if (r)
2199 DRM_ERROR("amdgpu asic init failed\n");
2200 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002201
2202 r = amdgpu_resume(adev);
Flora Cuica198522016-02-04 15:10:08 +08002203 if (r)
2204 DRM_ERROR("amdgpu_resume failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002205
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002206 amdgpu_fence_driver_resume(adev);
2207
Flora Cuica198522016-02-04 15:10:08 +08002208 if (resume) {
2209 r = amdgpu_ib_ring_tests(adev);
2210 if (r)
2211 DRM_ERROR("ib ring test failed (%d).\n", r);
2212 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002213
2214 r = amdgpu_late_init(adev);
Jim Quc085bd52017-03-01 15:53:29 +08002215 if (r) {
2216 if (fbcon)
2217 console_unlock();
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002218 return r;
Jim Quc085bd52017-03-01 15:53:29 +08002219 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002220
Alex Deucher756e6882015-10-08 00:03:36 -04002221 /* pin cursors */
2222 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2223 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2224
2225 if (amdgpu_crtc->cursor_bo) {
2226 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
2227 r = amdgpu_bo_reserve(aobj, false);
2228 if (r == 0) {
2229 r = amdgpu_bo_pin(aobj,
2230 AMDGPU_GEM_DOMAIN_VRAM,
2231 &amdgpu_crtc->cursor_addr);
2232 if (r != 0)
2233 DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
2234 amdgpu_bo_unreserve(aobj);
2235 }
2236 }
2237 }
2238
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002239 /* blat the mode back in */
2240 if (fbcon) {
2241 drm_helper_resume_force_mode(dev);
2242 /* turn on display hw */
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002243 drm_modeset_lock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002244 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2245 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
2246 }
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002247 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002248 }
2249
2250 drm_kms_helper_poll_enable(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002251
2252 /*
2253 * Most of the connector probing functions try to acquire runtime pm
2254 * refs to ensure that the GPU is powered on when connector polling is
2255 * performed. Since we're calling this from a runtime PM callback,
2256 * trying to acquire rpm refs will cause us to deadlock.
2257 *
2258 * Since we're guaranteed to be holding the rpm lock, it's safe to
2259 * temporarily disable the rpm helpers so this doesn't deadlock us.
2260 */
2261#ifdef CONFIG_PM
2262 dev->dev->power.disable_depth++;
2263#endif
Alex Deucher54fb2a52015-11-24 14:30:56 -05002264 drm_helper_hpd_irq_event(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002265#ifdef CONFIG_PM
2266 dev->dev->power.disable_depth--;
2267#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002268
2269 if (fbcon) {
2270 amdgpu_fbdev_set_suspend(adev, 0);
2271 console_unlock();
2272 }
2273
2274 return 0;
2275}
2276
Chunming Zhou63fbf422016-07-15 11:19:20 +08002277static bool amdgpu_check_soft_reset(struct amdgpu_device *adev)
2278{
2279 int i;
2280 bool asic_hang = false;
2281
2282 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002283 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou63fbf422016-07-15 11:19:20 +08002284 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002285 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
2286 adev->ip_blocks[i].status.hang =
2287 adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
2288 if (adev->ip_blocks[i].status.hang) {
2289 DRM_INFO("IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
Chunming Zhou63fbf422016-07-15 11:19:20 +08002290 asic_hang = true;
2291 }
2292 }
2293 return asic_hang;
2294}
2295
Baoyou Xie4d446652016-09-18 22:09:35 +08002296static int amdgpu_pre_soft_reset(struct amdgpu_device *adev)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002297{
2298 int i, r = 0;
2299
2300 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002301 if (!adev->ip_blocks[i].status.valid)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002302 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002303 if (adev->ip_blocks[i].status.hang &&
2304 adev->ip_blocks[i].version->funcs->pre_soft_reset) {
2305 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
Chunming Zhoud31a5012016-07-18 10:04:34 +08002306 if (r)
2307 return r;
2308 }
2309 }
2310
2311 return 0;
2312}
2313
Chunming Zhou35d782f2016-07-15 15:57:13 +08002314static bool amdgpu_need_full_reset(struct amdgpu_device *adev)
2315{
Alex Deucherda146d32016-10-13 16:07:03 -04002316 int i;
2317
2318 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002319 if (!adev->ip_blocks[i].status.valid)
Alex Deucherda146d32016-10-13 16:07:03 -04002320 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002321 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
2322 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
2323 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
2324 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE)) {
2325 if (adev->ip_blocks[i].status.hang) {
Alex Deucherda146d32016-10-13 16:07:03 -04002326 DRM_INFO("Some block need full reset!\n");
2327 return true;
2328 }
2329 }
Chunming Zhou35d782f2016-07-15 15:57:13 +08002330 }
2331 return false;
2332}
2333
2334static int amdgpu_soft_reset(struct amdgpu_device *adev)
2335{
2336 int i, r = 0;
2337
2338 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002339 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002340 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002341 if (adev->ip_blocks[i].status.hang &&
2342 adev->ip_blocks[i].version->funcs->soft_reset) {
2343 r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002344 if (r)
2345 return r;
2346 }
2347 }
2348
2349 return 0;
2350}
2351
2352static int amdgpu_post_soft_reset(struct amdgpu_device *adev)
2353{
2354 int i, r = 0;
2355
2356 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002357 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002358 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002359 if (adev->ip_blocks[i].status.hang &&
2360 adev->ip_blocks[i].version->funcs->post_soft_reset)
2361 r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002362 if (r)
2363 return r;
2364 }
2365
2366 return 0;
2367}
2368
Chunming Zhou3ad81f12016-08-05 17:30:17 +08002369bool amdgpu_need_backup(struct amdgpu_device *adev)
2370{
2371 if (adev->flags & AMD_IS_APU)
2372 return false;
2373
2374 return amdgpu_lockup_timeout > 0 ? true : false;
2375}
2376
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002377static int amdgpu_recover_vram_from_shadow(struct amdgpu_device *adev,
2378 struct amdgpu_ring *ring,
2379 struct amdgpu_bo *bo,
Chris Wilsonf54d1862016-10-25 13:00:45 +01002380 struct dma_fence **fence)
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002381{
2382 uint32_t domain;
2383 int r;
2384
2385 if (!bo->shadow)
2386 return 0;
2387
2388 r = amdgpu_bo_reserve(bo, false);
2389 if (r)
2390 return r;
2391 domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
2392 /* if bo has been evicted, then no need to recover */
2393 if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
2394 r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
2395 NULL, fence, true);
2396 if (r) {
2397 DRM_ERROR("recover page table failed!\n");
2398 goto err;
2399 }
2400 }
2401err:
2402 amdgpu_bo_unreserve(bo);
2403 return r;
2404}
2405
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002406/**
Monk Liua90ad3c2017-01-23 14:22:08 +08002407 * amdgpu_sriov_gpu_reset - reset the asic
2408 *
2409 * @adev: amdgpu device pointer
2410 * @voluntary: if this reset is requested by guest.
2411 * (true means by guest and false means by HYPERVISOR )
2412 *
2413 * Attempt the reset the GPU if it has hung (all asics).
2414 * for SRIOV case.
2415 * Returns 0 for success or an error on failure.
2416 */
2417int amdgpu_sriov_gpu_reset(struct amdgpu_device *adev, bool voluntary)
2418{
2419 int i, r = 0;
2420 int resched;
2421 struct amdgpu_bo *bo, *tmp;
2422 struct amdgpu_ring *ring;
2423 struct dma_fence *fence = NULL, *next = NULL;
2424
Monk Liu147b5982017-01-25 15:48:01 +08002425 mutex_lock(&adev->virt.lock_reset);
Monk Liua90ad3c2017-01-23 14:22:08 +08002426 atomic_inc(&adev->gpu_reset_counter);
Monk Liu1fb37a32017-01-26 15:36:37 +08002427 adev->gfx.in_reset = true;
Monk Liua90ad3c2017-01-23 14:22:08 +08002428
2429 /* block TTM */
2430 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
2431
2432 /* block scheduler */
2433 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2434 ring = adev->rings[i];
2435
2436 if (!ring || !ring->sched.thread)
2437 continue;
2438
2439 kthread_park(ring->sched.thread);
2440 amd_sched_hw_job_reset(&ring->sched);
2441 }
2442
2443 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
2444 amdgpu_fence_driver_force_completion(adev);
2445
2446 /* request to take full control of GPU before re-initialization */
2447 if (voluntary)
2448 amdgpu_virt_reset_gpu(adev);
2449 else
2450 amdgpu_virt_request_full_gpu(adev, true);
2451
2452
2453 /* Resume IP prior to SMC */
Monk Liue4f0fdc2017-02-09 11:55:49 +08002454 amdgpu_sriov_reinit_early(adev);
Monk Liua90ad3c2017-01-23 14:22:08 +08002455
2456 /* we need recover gart prior to run SMC/CP/SDMA resume */
2457 amdgpu_ttm_recover_gart(adev);
2458
2459 /* now we are okay to resume SMC/CP/SDMA */
Monk Liue4f0fdc2017-02-09 11:55:49 +08002460 amdgpu_sriov_reinit_late(adev);
Monk Liua90ad3c2017-01-23 14:22:08 +08002461
2462 amdgpu_irq_gpu_reset_resume_helper(adev);
2463
2464 if (amdgpu_ib_ring_tests(adev))
2465 dev_err(adev->dev, "[GPU_RESET] ib ring test failed (%d).\n", r);
2466
2467 /* release full control of GPU after ib test */
2468 amdgpu_virt_release_full_gpu(adev, true);
2469
2470 DRM_INFO("recover vram bo from shadow\n");
2471
2472 ring = adev->mman.buffer_funcs_ring;
2473 mutex_lock(&adev->shadow_list_lock);
2474 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
2475 amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
2476 if (fence) {
2477 r = dma_fence_wait(fence, false);
2478 if (r) {
2479 WARN(r, "recovery from shadow isn't completed\n");
2480 break;
2481 }
2482 }
2483
2484 dma_fence_put(fence);
2485 fence = next;
2486 }
2487 mutex_unlock(&adev->shadow_list_lock);
2488
2489 if (fence) {
2490 r = dma_fence_wait(fence, false);
2491 if (r)
2492 WARN(r, "recovery from shadow isn't completed\n");
2493 }
2494 dma_fence_put(fence);
2495
2496 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2497 struct amdgpu_ring *ring = adev->rings[i];
2498 if (!ring || !ring->sched.thread)
2499 continue;
2500
2501 amd_sched_job_recovery(&ring->sched);
2502 kthread_unpark(ring->sched.thread);
2503 }
2504
2505 drm_helper_resume_force_mode(adev->ddev);
2506 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
2507 if (r) {
2508 /* bad news, how to tell it to userspace ? */
2509 dev_info(adev->dev, "GPU reset failed\n");
2510 }
2511
Monk Liu1fb37a32017-01-26 15:36:37 +08002512 adev->gfx.in_reset = false;
Monk Liu147b5982017-01-25 15:48:01 +08002513 mutex_unlock(&adev->virt.lock_reset);
Monk Liua90ad3c2017-01-23 14:22:08 +08002514 return r;
2515}
2516
2517/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002518 * amdgpu_gpu_reset - reset the asic
2519 *
2520 * @adev: amdgpu device pointer
2521 *
2522 * Attempt the reset the GPU if it has hung (all asics).
2523 * Returns 0 for success or an error on failure.
2524 */
2525int amdgpu_gpu_reset(struct amdgpu_device *adev)
2526{
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002527 int i, r;
2528 int resched;
Chunming Zhou35d782f2016-07-15 15:57:13 +08002529 bool need_full_reset;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002530
Xiangliang Yufb140b22016-12-17 22:48:57 +08002531 if (amdgpu_sriov_vf(adev))
Monk Liua90ad3c2017-01-23 14:22:08 +08002532 return amdgpu_sriov_gpu_reset(adev, true);
Xiangliang Yufb140b22016-12-17 22:48:57 +08002533
Chunming Zhou63fbf422016-07-15 11:19:20 +08002534 if (!amdgpu_check_soft_reset(adev)) {
2535 DRM_INFO("No hardware hang detected. Did some blocks stall?\n");
2536 return 0;
2537 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002538
Marek Olšákd94aed52015-05-05 21:13:49 +02002539 atomic_inc(&adev->gpu_reset_counter);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002540
Chunming Zhoua3c47d62016-06-30 16:44:41 +08002541 /* block TTM */
2542 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
2543
Chunming Zhou0875dc92016-06-12 15:41:58 +08002544 /* block scheduler */
2545 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2546 struct amdgpu_ring *ring = adev->rings[i];
2547
2548 if (!ring)
2549 continue;
2550 kthread_park(ring->sched.thread);
Chunming Zhouaa1c8902016-06-30 13:56:02 +08002551 amd_sched_hw_job_reset(&ring->sched);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002552 }
Chunming Zhou2200eda2016-06-30 16:53:02 +08002553 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
2554 amdgpu_fence_driver_force_completion(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002555
Chunming Zhou35d782f2016-07-15 15:57:13 +08002556 need_full_reset = amdgpu_need_full_reset(adev);
2557
2558 if (!need_full_reset) {
2559 amdgpu_pre_soft_reset(adev);
2560 r = amdgpu_soft_reset(adev);
2561 amdgpu_post_soft_reset(adev);
2562 if (r || amdgpu_check_soft_reset(adev)) {
2563 DRM_INFO("soft reset failed, will fallback to full reset!\n");
2564 need_full_reset = true;
2565 }
2566 }
2567
2568 if (need_full_reset) {
Chunming Zhou35d782f2016-07-15 15:57:13 +08002569 r = amdgpu_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002570
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002571retry:
Chunming Zhou35d782f2016-07-15 15:57:13 +08002572 /* Disable fb access */
2573 if (adev->mode_info.num_crtc) {
2574 struct amdgpu_mode_mc_save save;
2575 amdgpu_display_stop_mc_access(adev, &save);
2576 amdgpu_wait_for_idle(adev, AMD_IP_BLOCK_TYPE_GMC);
2577 }
Alex Deuchere695e772016-10-19 14:40:58 -04002578 amdgpu_atombios_scratch_regs_save(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002579 r = amdgpu_asic_reset(adev);
Alex Deuchere695e772016-10-19 14:40:58 -04002580 amdgpu_atombios_scratch_regs_restore(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002581 /* post card */
2582 amdgpu_atom_asic_init(adev->mode_info.atom_context);
Alex Deucherbfa99262016-01-15 11:59:48 -05002583
Chunming Zhou35d782f2016-07-15 15:57:13 +08002584 if (!r) {
2585 dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
2586 r = amdgpu_resume(adev);
2587 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002588 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002589 if (!r) {
Chunming Zhoue72cfd52016-07-27 13:15:20 +08002590 amdgpu_irq_gpu_reset_resume_helper(adev);
Chunming Zhou2c0d7312016-08-30 16:36:25 +08002591 if (need_full_reset && amdgpu_need_backup(adev)) {
2592 r = amdgpu_ttm_recover_gart(adev);
2593 if (r)
2594 DRM_ERROR("gart recovery failed!!!\n");
2595 }
Chunming Zhou1f465082016-06-30 15:02:26 +08002596 r = amdgpu_ib_ring_tests(adev);
2597 if (r) {
2598 dev_err(adev->dev, "ib ring test failed (%d).\n", r);
Chunming Zhou40019dc2016-06-29 16:01:49 +08002599 r = amdgpu_suspend(adev);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002600 need_full_reset = true;
Chunming Zhou40019dc2016-06-29 16:01:49 +08002601 goto retry;
Chunming Zhou1f465082016-06-30 15:02:26 +08002602 }
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002603 /**
2604 * recovery vm page tables, since we cannot depend on VRAM is
2605 * consistent after gpu full reset.
2606 */
2607 if (need_full_reset && amdgpu_need_backup(adev)) {
2608 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
2609 struct amdgpu_bo *bo, *tmp;
Chris Wilsonf54d1862016-10-25 13:00:45 +01002610 struct dma_fence *fence = NULL, *next = NULL;
Chunming Zhou1f465082016-06-30 15:02:26 +08002611
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002612 DRM_INFO("recover vram bo from shadow\n");
2613 mutex_lock(&adev->shadow_list_lock);
2614 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
2615 amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
2616 if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01002617 r = dma_fence_wait(fence, false);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002618 if (r) {
Monk Liu1d7b17b2017-01-22 18:52:56 +08002619 WARN(r, "recovery from shadow isn't completed\n");
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002620 break;
2621 }
2622 }
2623
Chris Wilsonf54d1862016-10-25 13:00:45 +01002624 dma_fence_put(fence);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002625 fence = next;
2626 }
2627 mutex_unlock(&adev->shadow_list_lock);
2628 if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01002629 r = dma_fence_wait(fence, false);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002630 if (r)
Monk Liu1d7b17b2017-01-22 18:52:56 +08002631 WARN(r, "recovery from shadow isn't completed\n");
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002632 }
Chris Wilsonf54d1862016-10-25 13:00:45 +01002633 dma_fence_put(fence);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002634 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002635 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2636 struct amdgpu_ring *ring = adev->rings[i];
2637 if (!ring)
2638 continue;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002639
Chunming Zhouaa1c8902016-06-30 13:56:02 +08002640 amd_sched_job_recovery(&ring->sched);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002641 kthread_unpark(ring->sched.thread);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002642 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002643 } else {
Chunming Zhou2200eda2016-06-30 16:53:02 +08002644 dev_err(adev->dev, "asic resume failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002645 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
Chunming Zhou0875dc92016-06-12 15:41:58 +08002646 if (adev->rings[i]) {
2647 kthread_unpark(adev->rings[i]->sched.thread);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002648 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002649 }
2650 }
2651
2652 drm_helper_resume_force_mode(adev->ddev);
2653
2654 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
2655 if (r) {
2656 /* bad news, how to tell it to userspace ? */
2657 dev_info(adev->dev, "GPU reset failed\n");
2658 }
2659
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002660 return r;
2661}
2662
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002663void amdgpu_get_pcie_info(struct amdgpu_device *adev)
2664{
2665 u32 mask;
2666 int ret;
2667
Alex Deuchercd474ba2016-02-04 10:21:23 -05002668 if (amdgpu_pcie_gen_cap)
2669 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
2670
2671 if (amdgpu_pcie_lane_cap)
2672 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
2673
2674 /* covers APUs as well */
2675 if (pci_is_root_bus(adev->pdev->bus)) {
2676 if (adev->pm.pcie_gen_mask == 0)
2677 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
2678 if (adev->pm.pcie_mlw_mask == 0)
2679 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002680 return;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002681 }
Alex Deuchercd474ba2016-02-04 10:21:23 -05002682
2683 if (adev->pm.pcie_gen_mask == 0) {
2684 ret = drm_pcie_get_speed_cap_mask(adev->ddev, &mask);
2685 if (!ret) {
2686 adev->pm.pcie_gen_mask = (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
2687 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
2688 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
2689
2690 if (mask & DRM_PCIE_SPEED_25)
2691 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
2692 if (mask & DRM_PCIE_SPEED_50)
2693 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2;
2694 if (mask & DRM_PCIE_SPEED_80)
2695 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3;
2696 } else {
2697 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
2698 }
2699 }
2700 if (adev->pm.pcie_mlw_mask == 0) {
2701 ret = drm_pcie_get_max_link_width(adev->ddev, &mask);
2702 if (!ret) {
2703 switch (mask) {
2704 case 32:
2705 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
2706 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
2707 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2708 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2709 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2710 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2711 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2712 break;
2713 case 16:
2714 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
2715 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2716 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2717 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2718 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2719 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2720 break;
2721 case 12:
2722 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2723 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2724 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2725 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2726 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2727 break;
2728 case 8:
2729 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2730 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2731 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2732 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2733 break;
2734 case 4:
2735 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2736 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2737 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2738 break;
2739 case 2:
2740 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2741 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2742 break;
2743 case 1:
2744 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
2745 break;
2746 default:
2747 break;
2748 }
2749 } else {
2750 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002751 }
2752 }
2753}
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002754
2755/*
2756 * Debugfs
2757 */
2758int amdgpu_debugfs_add_files(struct amdgpu_device *adev,
Nils Wallménius06ab6832016-05-02 12:46:15 -04002759 const struct drm_info_list *files,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002760 unsigned nfiles)
2761{
2762 unsigned i;
2763
2764 for (i = 0; i < adev->debugfs_count; i++) {
2765 if (adev->debugfs[i].files == files) {
2766 /* Already registered */
2767 return 0;
2768 }
2769 }
2770
2771 i = adev->debugfs_count + 1;
2772 if (i > AMDGPU_DEBUGFS_MAX_COMPONENTS) {
2773 DRM_ERROR("Reached maximum number of debugfs components.\n");
2774 DRM_ERROR("Report so we increase "
2775 "AMDGPU_DEBUGFS_MAX_COMPONENTS.\n");
2776 return -EINVAL;
2777 }
2778 adev->debugfs[adev->debugfs_count].files = files;
2779 adev->debugfs[adev->debugfs_count].num_files = nfiles;
2780 adev->debugfs_count = i;
2781#if defined(CONFIG_DEBUG_FS)
2782 drm_debugfs_create_files(files, nfiles,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002783 adev->ddev->primary->debugfs_root,
2784 adev->ddev->primary);
2785#endif
2786 return 0;
2787}
2788
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002789#if defined(CONFIG_DEBUG_FS)
2790
2791static ssize_t amdgpu_debugfs_regs_read(struct file *f, char __user *buf,
2792 size_t size, loff_t *pos)
2793{
Al Viro45063092016-12-04 18:24:56 -05002794 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002795 ssize_t result = 0;
2796 int r;
Tom St Denisbd122672016-07-28 09:39:22 -04002797 bool pm_pg_lock, use_bank;
Tom St Denis566281592016-06-27 11:55:07 -04002798 unsigned instance_bank, sh_bank, se_bank;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002799
2800 if (size & 0x3 || *pos & 0x3)
2801 return -EINVAL;
2802
Tom St Denisbd122672016-07-28 09:39:22 -04002803 /* are we reading registers for which a PG lock is necessary? */
2804 pm_pg_lock = (*pos >> 23) & 1;
2805
Tom St Denis566281592016-06-27 11:55:07 -04002806 if (*pos & (1ULL << 62)) {
2807 se_bank = (*pos >> 24) & 0x3FF;
2808 sh_bank = (*pos >> 34) & 0x3FF;
2809 instance_bank = (*pos >> 44) & 0x3FF;
Tom St Denis32977f92016-10-09 07:41:26 -04002810
2811 if (se_bank == 0x3FF)
2812 se_bank = 0xFFFFFFFF;
2813 if (sh_bank == 0x3FF)
2814 sh_bank = 0xFFFFFFFF;
2815 if (instance_bank == 0x3FF)
2816 instance_bank = 0xFFFFFFFF;
Tom St Denis566281592016-06-27 11:55:07 -04002817 use_bank = 1;
Tom St Denis566281592016-06-27 11:55:07 -04002818 } else {
2819 use_bank = 0;
2820 }
2821
Tom St Denis801a6aa9a62017-03-15 05:34:25 -04002822 *pos &= (1UL << 22) - 1;
Tom St Denisbd122672016-07-28 09:39:22 -04002823
Tom St Denis566281592016-06-27 11:55:07 -04002824 if (use_bank) {
Tom St Denis32977f92016-10-09 07:41:26 -04002825 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
2826 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
Tom St Denis566281592016-06-27 11:55:07 -04002827 return -EINVAL;
2828 mutex_lock(&adev->grbm_idx_mutex);
2829 amdgpu_gfx_select_se_sh(adev, se_bank,
2830 sh_bank, instance_bank);
2831 }
2832
Tom St Denisbd122672016-07-28 09:39:22 -04002833 if (pm_pg_lock)
2834 mutex_lock(&adev->pm.mutex);
2835
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002836 while (size) {
2837 uint32_t value;
2838
2839 if (*pos > adev->rmmio_size)
Tom St Denis566281592016-06-27 11:55:07 -04002840 goto end;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002841
2842 value = RREG32(*pos >> 2);
2843 r = put_user(value, (uint32_t *)buf);
Tom St Denis566281592016-06-27 11:55:07 -04002844 if (r) {
2845 result = r;
2846 goto end;
2847 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002848
2849 result += 4;
2850 buf += 4;
2851 *pos += 4;
2852 size -= 4;
2853 }
2854
Tom St Denis566281592016-06-27 11:55:07 -04002855end:
2856 if (use_bank) {
2857 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
2858 mutex_unlock(&adev->grbm_idx_mutex);
2859 }
2860
Tom St Denisbd122672016-07-28 09:39:22 -04002861 if (pm_pg_lock)
2862 mutex_unlock(&adev->pm.mutex);
2863
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002864 return result;
2865}
2866
2867static ssize_t amdgpu_debugfs_regs_write(struct file *f, const char __user *buf,
2868 size_t size, loff_t *pos)
2869{
Al Viro45063092016-12-04 18:24:56 -05002870 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002871 ssize_t result = 0;
2872 int r;
Tom St Denis394fdde2016-10-10 07:31:23 -04002873 bool pm_pg_lock, use_bank;
2874 unsigned instance_bank, sh_bank, se_bank;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002875
2876 if (size & 0x3 || *pos & 0x3)
2877 return -EINVAL;
2878
Tom St Denis394fdde2016-10-10 07:31:23 -04002879 /* are we reading registers for which a PG lock is necessary? */
2880 pm_pg_lock = (*pos >> 23) & 1;
2881
2882 if (*pos & (1ULL << 62)) {
2883 se_bank = (*pos >> 24) & 0x3FF;
2884 sh_bank = (*pos >> 34) & 0x3FF;
2885 instance_bank = (*pos >> 44) & 0x3FF;
2886
2887 if (se_bank == 0x3FF)
2888 se_bank = 0xFFFFFFFF;
2889 if (sh_bank == 0x3FF)
2890 sh_bank = 0xFFFFFFFF;
2891 if (instance_bank == 0x3FF)
2892 instance_bank = 0xFFFFFFFF;
2893 use_bank = 1;
2894 } else {
2895 use_bank = 0;
2896 }
2897
Tom St Denis801a6aa9a62017-03-15 05:34:25 -04002898 *pos &= (1UL << 22) - 1;
Tom St Denis394fdde2016-10-10 07:31:23 -04002899
2900 if (use_bank) {
2901 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
2902 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
2903 return -EINVAL;
2904 mutex_lock(&adev->grbm_idx_mutex);
2905 amdgpu_gfx_select_se_sh(adev, se_bank,
2906 sh_bank, instance_bank);
2907 }
2908
2909 if (pm_pg_lock)
2910 mutex_lock(&adev->pm.mutex);
2911
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002912 while (size) {
2913 uint32_t value;
2914
2915 if (*pos > adev->rmmio_size)
2916 return result;
2917
2918 r = get_user(value, (uint32_t *)buf);
2919 if (r)
2920 return r;
2921
2922 WREG32(*pos >> 2, value);
2923
2924 result += 4;
2925 buf += 4;
2926 *pos += 4;
2927 size -= 4;
2928 }
2929
Tom St Denis394fdde2016-10-10 07:31:23 -04002930 if (use_bank) {
2931 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
2932 mutex_unlock(&adev->grbm_idx_mutex);
2933 }
2934
2935 if (pm_pg_lock)
2936 mutex_unlock(&adev->pm.mutex);
2937
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002938 return result;
2939}
2940
Tom St Denisadcec282016-04-15 13:08:44 -04002941static ssize_t amdgpu_debugfs_regs_pcie_read(struct file *f, char __user *buf,
2942 size_t size, loff_t *pos)
2943{
Al Viro45063092016-12-04 18:24:56 -05002944 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04002945 ssize_t result = 0;
2946 int r;
2947
2948 if (size & 0x3 || *pos & 0x3)
2949 return -EINVAL;
2950
2951 while (size) {
2952 uint32_t value;
2953
2954 value = RREG32_PCIE(*pos >> 2);
2955 r = put_user(value, (uint32_t *)buf);
2956 if (r)
2957 return r;
2958
2959 result += 4;
2960 buf += 4;
2961 *pos += 4;
2962 size -= 4;
2963 }
2964
2965 return result;
2966}
2967
2968static ssize_t amdgpu_debugfs_regs_pcie_write(struct file *f, const char __user *buf,
2969 size_t size, loff_t *pos)
2970{
Al Viro45063092016-12-04 18:24:56 -05002971 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04002972 ssize_t result = 0;
2973 int r;
2974
2975 if (size & 0x3 || *pos & 0x3)
2976 return -EINVAL;
2977
2978 while (size) {
2979 uint32_t value;
2980
2981 r = get_user(value, (uint32_t *)buf);
2982 if (r)
2983 return r;
2984
2985 WREG32_PCIE(*pos >> 2, value);
2986
2987 result += 4;
2988 buf += 4;
2989 *pos += 4;
2990 size -= 4;
2991 }
2992
2993 return result;
2994}
2995
2996static ssize_t amdgpu_debugfs_regs_didt_read(struct file *f, char __user *buf,
2997 size_t size, loff_t *pos)
2998{
Al Viro45063092016-12-04 18:24:56 -05002999 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003000 ssize_t result = 0;
3001 int r;
3002
3003 if (size & 0x3 || *pos & 0x3)
3004 return -EINVAL;
3005
3006 while (size) {
3007 uint32_t value;
3008
3009 value = RREG32_DIDT(*pos >> 2);
3010 r = put_user(value, (uint32_t *)buf);
3011 if (r)
3012 return r;
3013
3014 result += 4;
3015 buf += 4;
3016 *pos += 4;
3017 size -= 4;
3018 }
3019
3020 return result;
3021}
3022
3023static ssize_t amdgpu_debugfs_regs_didt_write(struct file *f, const char __user *buf,
3024 size_t size, loff_t *pos)
3025{
Al Viro45063092016-12-04 18:24:56 -05003026 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003027 ssize_t result = 0;
3028 int r;
3029
3030 if (size & 0x3 || *pos & 0x3)
3031 return -EINVAL;
3032
3033 while (size) {
3034 uint32_t value;
3035
3036 r = get_user(value, (uint32_t *)buf);
3037 if (r)
3038 return r;
3039
3040 WREG32_DIDT(*pos >> 2, value);
3041
3042 result += 4;
3043 buf += 4;
3044 *pos += 4;
3045 size -= 4;
3046 }
3047
3048 return result;
3049}
3050
3051static ssize_t amdgpu_debugfs_regs_smc_read(struct file *f, char __user *buf,
3052 size_t size, loff_t *pos)
3053{
Al Viro45063092016-12-04 18:24:56 -05003054 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003055 ssize_t result = 0;
3056 int r;
3057
3058 if (size & 0x3 || *pos & 0x3)
3059 return -EINVAL;
3060
3061 while (size) {
3062 uint32_t value;
3063
Tom St Denis6fc0dea2016-08-29 08:39:29 -04003064 value = RREG32_SMC(*pos);
Tom St Denisadcec282016-04-15 13:08:44 -04003065 r = put_user(value, (uint32_t *)buf);
3066 if (r)
3067 return r;
3068
3069 result += 4;
3070 buf += 4;
3071 *pos += 4;
3072 size -= 4;
3073 }
3074
3075 return result;
3076}
3077
3078static ssize_t amdgpu_debugfs_regs_smc_write(struct file *f, const char __user *buf,
3079 size_t size, loff_t *pos)
3080{
Al Viro45063092016-12-04 18:24:56 -05003081 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003082 ssize_t result = 0;
3083 int r;
3084
3085 if (size & 0x3 || *pos & 0x3)
3086 return -EINVAL;
3087
3088 while (size) {
3089 uint32_t value;
3090
3091 r = get_user(value, (uint32_t *)buf);
3092 if (r)
3093 return r;
3094
Tom St Denis6fc0dea2016-08-29 08:39:29 -04003095 WREG32_SMC(*pos, value);
Tom St Denisadcec282016-04-15 13:08:44 -04003096
3097 result += 4;
3098 buf += 4;
3099 *pos += 4;
3100 size -= 4;
3101 }
3102
3103 return result;
3104}
3105
Tom St Denis1e051412016-06-27 09:57:18 -04003106static ssize_t amdgpu_debugfs_gca_config_read(struct file *f, char __user *buf,
3107 size_t size, loff_t *pos)
3108{
Al Viro45063092016-12-04 18:24:56 -05003109 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denis1e051412016-06-27 09:57:18 -04003110 ssize_t result = 0;
3111 int r;
3112 uint32_t *config, no_regs = 0;
3113
3114 if (size & 0x3 || *pos & 0x3)
3115 return -EINVAL;
3116
Markus Elfringecab7662016-09-18 17:00:52 +02003117 config = kmalloc_array(256, sizeof(*config), GFP_KERNEL);
Tom St Denis1e051412016-06-27 09:57:18 -04003118 if (!config)
3119 return -ENOMEM;
3120
3121 /* version, increment each time something is added */
Tom St Denis9a999352017-01-18 13:01:25 -05003122 config[no_regs++] = 3;
Tom St Denis1e051412016-06-27 09:57:18 -04003123 config[no_regs++] = adev->gfx.config.max_shader_engines;
3124 config[no_regs++] = adev->gfx.config.max_tile_pipes;
3125 config[no_regs++] = adev->gfx.config.max_cu_per_sh;
3126 config[no_regs++] = adev->gfx.config.max_sh_per_se;
3127 config[no_regs++] = adev->gfx.config.max_backends_per_se;
3128 config[no_regs++] = adev->gfx.config.max_texture_channel_caches;
3129 config[no_regs++] = adev->gfx.config.max_gprs;
3130 config[no_regs++] = adev->gfx.config.max_gs_threads;
3131 config[no_regs++] = adev->gfx.config.max_hw_contexts;
3132 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_frontend;
3133 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_backend;
3134 config[no_regs++] = adev->gfx.config.sc_hiz_tile_fifo_size;
3135 config[no_regs++] = adev->gfx.config.sc_earlyz_tile_fifo_size;
3136 config[no_regs++] = adev->gfx.config.num_tile_pipes;
3137 config[no_regs++] = adev->gfx.config.backend_enable_mask;
3138 config[no_regs++] = adev->gfx.config.mem_max_burst_length_bytes;
3139 config[no_regs++] = adev->gfx.config.mem_row_size_in_kb;
3140 config[no_regs++] = adev->gfx.config.shader_engine_tile_size;
3141 config[no_regs++] = adev->gfx.config.num_gpus;
3142 config[no_regs++] = adev->gfx.config.multi_gpu_tile_size;
3143 config[no_regs++] = adev->gfx.config.mc_arb_ramcfg;
3144 config[no_regs++] = adev->gfx.config.gb_addr_config;
3145 config[no_regs++] = adev->gfx.config.num_rbs;
3146
Tom St Denis89a8f302016-08-12 15:14:31 -04003147 /* rev==1 */
3148 config[no_regs++] = adev->rev_id;
3149 config[no_regs++] = adev->pg_flags;
3150 config[no_regs++] = adev->cg_flags;
3151
Tom St Denise9f11dc2016-08-17 12:00:51 -04003152 /* rev==2 */
3153 config[no_regs++] = adev->family;
3154 config[no_regs++] = adev->external_rev_id;
3155
Tom St Denis9a999352017-01-18 13:01:25 -05003156 /* rev==3 */
3157 config[no_regs++] = adev->pdev->device;
3158 config[no_regs++] = adev->pdev->revision;
3159 config[no_regs++] = adev->pdev->subsystem_device;
3160 config[no_regs++] = adev->pdev->subsystem_vendor;
3161
Tom St Denis1e051412016-06-27 09:57:18 -04003162 while (size && (*pos < no_regs * 4)) {
3163 uint32_t value;
3164
3165 value = config[*pos >> 2];
3166 r = put_user(value, (uint32_t *)buf);
3167 if (r) {
3168 kfree(config);
3169 return r;
3170 }
3171
3172 result += 4;
3173 buf += 4;
3174 *pos += 4;
3175 size -= 4;
3176 }
3177
3178 kfree(config);
3179 return result;
3180}
3181
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003182static ssize_t amdgpu_debugfs_sensor_read(struct file *f, char __user *buf,
3183 size_t size, loff_t *pos)
3184{
Al Viro45063092016-12-04 18:24:56 -05003185 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003186 int idx, x, outsize, r, valuesize;
3187 uint32_t values[16];
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003188
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003189 if (size & 3 || *pos & 0x3)
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003190 return -EINVAL;
3191
Samuel Pitoiset3cbc6142017-02-15 19:32:29 +01003192 if (amdgpu_dpm == 0)
3193 return -EINVAL;
3194
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003195 /* convert offset to sensor number */
3196 idx = *pos >> 2;
3197
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003198 valuesize = sizeof(values);
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003199 if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->read_sensor)
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003200 r = adev->powerplay.pp_funcs->read_sensor(adev->powerplay.pp_handle, idx, &values[0], &valuesize);
Samuel Pitoiset3cbc6142017-02-15 19:32:29 +01003201 else if (adev->pm.funcs && adev->pm.funcs->read_sensor)
3202 r = adev->pm.funcs->read_sensor(adev, idx, &values[0],
3203 &valuesize);
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003204 else
3205 return -EINVAL;
3206
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003207 if (size > valuesize)
3208 return -EINVAL;
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003209
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003210 outsize = 0;
3211 x = 0;
3212 if (!r) {
3213 while (size) {
3214 r = put_user(values[x++], (int32_t *)buf);
3215 buf += 4;
3216 size -= 4;
3217 outsize += 4;
3218 }
3219 }
3220
3221 return !r ? outsize : r;
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003222}
Tom St Denis1e051412016-06-27 09:57:18 -04003223
Tom St Denis273d7aa2016-10-11 14:48:55 -04003224static ssize_t amdgpu_debugfs_wave_read(struct file *f, char __user *buf,
3225 size_t size, loff_t *pos)
3226{
3227 struct amdgpu_device *adev = f->f_inode->i_private;
3228 int r, x;
3229 ssize_t result=0;
Tom St Denis472259f2016-10-14 09:49:09 -04003230 uint32_t offset, se, sh, cu, wave, simd, data[32];
Tom St Denis273d7aa2016-10-11 14:48:55 -04003231
3232 if (size & 3 || *pos & 3)
3233 return -EINVAL;
3234
3235 /* decode offset */
3236 offset = (*pos & 0x7F);
3237 se = ((*pos >> 7) & 0xFF);
3238 sh = ((*pos >> 15) & 0xFF);
3239 cu = ((*pos >> 23) & 0xFF);
3240 wave = ((*pos >> 31) & 0xFF);
3241 simd = ((*pos >> 37) & 0xFF);
Tom St Denis273d7aa2016-10-11 14:48:55 -04003242
3243 /* switch to the specific se/sh/cu */
3244 mutex_lock(&adev->grbm_idx_mutex);
3245 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
3246
3247 x = 0;
Tom St Denis472259f2016-10-14 09:49:09 -04003248 if (adev->gfx.funcs->read_wave_data)
3249 adev->gfx.funcs->read_wave_data(adev, simd, wave, data, &x);
Tom St Denis273d7aa2016-10-11 14:48:55 -04003250
3251 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
3252 mutex_unlock(&adev->grbm_idx_mutex);
3253
Tom St Denis5ecfb3b2016-10-13 12:15:03 -04003254 if (!x)
3255 return -EINVAL;
3256
Tom St Denis472259f2016-10-14 09:49:09 -04003257 while (size && (offset < x * 4)) {
Tom St Denis273d7aa2016-10-11 14:48:55 -04003258 uint32_t value;
3259
Tom St Denis472259f2016-10-14 09:49:09 -04003260 value = data[offset >> 2];
Tom St Denis273d7aa2016-10-11 14:48:55 -04003261 r = put_user(value, (uint32_t *)buf);
3262 if (r)
3263 return r;
3264
3265 result += 4;
3266 buf += 4;
Tom St Denis472259f2016-10-14 09:49:09 -04003267 offset += 4;
Tom St Denis273d7aa2016-10-11 14:48:55 -04003268 size -= 4;
3269 }
3270
3271 return result;
3272}
3273
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003274static ssize_t amdgpu_debugfs_gpr_read(struct file *f, char __user *buf,
3275 size_t size, loff_t *pos)
3276{
3277 struct amdgpu_device *adev = f->f_inode->i_private;
3278 int r;
3279 ssize_t result = 0;
3280 uint32_t offset, se, sh, cu, wave, simd, thread, bank, *data;
3281
3282 if (size & 3 || *pos & 3)
3283 return -EINVAL;
3284
3285 /* decode offset */
3286 offset = (*pos & 0xFFF); /* in dwords */
3287 se = ((*pos >> 12) & 0xFF);
3288 sh = ((*pos >> 20) & 0xFF);
3289 cu = ((*pos >> 28) & 0xFF);
3290 wave = ((*pos >> 36) & 0xFF);
3291 simd = ((*pos >> 44) & 0xFF);
3292 thread = ((*pos >> 52) & 0xFF);
3293 bank = ((*pos >> 60) & 1);
3294
3295 data = kmalloc_array(1024, sizeof(*data), GFP_KERNEL);
3296 if (!data)
3297 return -ENOMEM;
3298
3299 /* switch to the specific se/sh/cu */
3300 mutex_lock(&adev->grbm_idx_mutex);
3301 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
3302
3303 if (bank == 0) {
3304 if (adev->gfx.funcs->read_wave_vgprs)
3305 adev->gfx.funcs->read_wave_vgprs(adev, simd, wave, thread, offset, size>>2, data);
3306 } else {
3307 if (adev->gfx.funcs->read_wave_sgprs)
3308 adev->gfx.funcs->read_wave_sgprs(adev, simd, wave, offset, size>>2, data);
3309 }
3310
3311 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
3312 mutex_unlock(&adev->grbm_idx_mutex);
3313
3314 while (size) {
3315 uint32_t value;
3316
3317 value = data[offset++];
3318 r = put_user(value, (uint32_t *)buf);
3319 if (r) {
3320 result = r;
3321 goto err;
3322 }
3323
3324 result += 4;
3325 buf += 4;
3326 size -= 4;
3327 }
3328
3329err:
3330 kfree(data);
3331 return result;
3332}
3333
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003334static const struct file_operations amdgpu_debugfs_regs_fops = {
3335 .owner = THIS_MODULE,
3336 .read = amdgpu_debugfs_regs_read,
3337 .write = amdgpu_debugfs_regs_write,
3338 .llseek = default_llseek
3339};
Tom St Denisadcec282016-04-15 13:08:44 -04003340static const struct file_operations amdgpu_debugfs_regs_didt_fops = {
3341 .owner = THIS_MODULE,
3342 .read = amdgpu_debugfs_regs_didt_read,
3343 .write = amdgpu_debugfs_regs_didt_write,
3344 .llseek = default_llseek
3345};
3346static const struct file_operations amdgpu_debugfs_regs_pcie_fops = {
3347 .owner = THIS_MODULE,
3348 .read = amdgpu_debugfs_regs_pcie_read,
3349 .write = amdgpu_debugfs_regs_pcie_write,
3350 .llseek = default_llseek
3351};
3352static const struct file_operations amdgpu_debugfs_regs_smc_fops = {
3353 .owner = THIS_MODULE,
3354 .read = amdgpu_debugfs_regs_smc_read,
3355 .write = amdgpu_debugfs_regs_smc_write,
3356 .llseek = default_llseek
3357};
3358
Tom St Denis1e051412016-06-27 09:57:18 -04003359static const struct file_operations amdgpu_debugfs_gca_config_fops = {
3360 .owner = THIS_MODULE,
3361 .read = amdgpu_debugfs_gca_config_read,
3362 .llseek = default_llseek
3363};
3364
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003365static const struct file_operations amdgpu_debugfs_sensors_fops = {
3366 .owner = THIS_MODULE,
3367 .read = amdgpu_debugfs_sensor_read,
3368 .llseek = default_llseek
3369};
3370
Tom St Denis273d7aa2016-10-11 14:48:55 -04003371static const struct file_operations amdgpu_debugfs_wave_fops = {
3372 .owner = THIS_MODULE,
3373 .read = amdgpu_debugfs_wave_read,
3374 .llseek = default_llseek
3375};
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003376static const struct file_operations amdgpu_debugfs_gpr_fops = {
3377 .owner = THIS_MODULE,
3378 .read = amdgpu_debugfs_gpr_read,
3379 .llseek = default_llseek
3380};
Tom St Denis273d7aa2016-10-11 14:48:55 -04003381
Tom St Denisadcec282016-04-15 13:08:44 -04003382static const struct file_operations *debugfs_regs[] = {
3383 &amdgpu_debugfs_regs_fops,
3384 &amdgpu_debugfs_regs_didt_fops,
3385 &amdgpu_debugfs_regs_pcie_fops,
3386 &amdgpu_debugfs_regs_smc_fops,
Tom St Denis1e051412016-06-27 09:57:18 -04003387 &amdgpu_debugfs_gca_config_fops,
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003388 &amdgpu_debugfs_sensors_fops,
Tom St Denis273d7aa2016-10-11 14:48:55 -04003389 &amdgpu_debugfs_wave_fops,
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003390 &amdgpu_debugfs_gpr_fops,
Tom St Denisadcec282016-04-15 13:08:44 -04003391};
3392
3393static const char *debugfs_regs_names[] = {
3394 "amdgpu_regs",
3395 "amdgpu_regs_didt",
3396 "amdgpu_regs_pcie",
3397 "amdgpu_regs_smc",
Tom St Denis1e051412016-06-27 09:57:18 -04003398 "amdgpu_gca_config",
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003399 "amdgpu_sensors",
Tom St Denis273d7aa2016-10-11 14:48:55 -04003400 "amdgpu_wave",
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003401 "amdgpu_gpr",
Tom St Denisadcec282016-04-15 13:08:44 -04003402};
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003403
3404static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
3405{
3406 struct drm_minor *minor = adev->ddev->primary;
3407 struct dentry *ent, *root = minor->debugfs_root;
Tom St Denisadcec282016-04-15 13:08:44 -04003408 unsigned i, j;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003409
Tom St Denisadcec282016-04-15 13:08:44 -04003410 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3411 ent = debugfs_create_file(debugfs_regs_names[i],
3412 S_IFREG | S_IRUGO, root,
3413 adev, debugfs_regs[i]);
3414 if (IS_ERR(ent)) {
3415 for (j = 0; j < i; j++) {
3416 debugfs_remove(adev->debugfs_regs[i]);
3417 adev->debugfs_regs[i] = NULL;
3418 }
3419 return PTR_ERR(ent);
3420 }
3421
3422 if (!i)
3423 i_size_write(ent->d_inode, adev->rmmio_size);
3424 adev->debugfs_regs[i] = ent;
3425 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003426
3427 return 0;
3428}
3429
3430static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev)
3431{
Tom St Denisadcec282016-04-15 13:08:44 -04003432 unsigned i;
3433
3434 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3435 if (adev->debugfs_regs[i]) {
3436 debugfs_remove(adev->debugfs_regs[i]);
3437 adev->debugfs_regs[i] = NULL;
3438 }
3439 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003440}
3441
3442int amdgpu_debugfs_init(struct drm_minor *minor)
3443{
3444 return 0;
3445}
Alexander Kuleshov7cebc722015-06-27 13:16:05 +06003446#else
3447static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
3448{
3449 return 0;
3450}
3451static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev) { }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003452#endif