blob: ff0b9920ea79f1d18c02ad91e8ee601db7b352cc [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 Liu89e0ec9f2016-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 Liu89e0ec9f2016-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;
Alex Deuchera1255102016-10-13 17:41:13 -04001194 if (adev->ip_blocks[i].version->type == block_type) {
1195 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1196 state);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001197 if (r)
1198 return r;
Alex Deuchera225bf12016-06-23 11:48:30 -04001199 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001200 }
1201 }
1202 return r;
1203}
1204
1205int amdgpu_set_powergating_state(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001206 enum amd_ip_block_type block_type,
1207 enum amd_powergating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001208{
1209 int i, r = 0;
1210
1211 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001212 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001213 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001214 if (adev->ip_blocks[i].version->type == block_type) {
1215 r = adev->ip_blocks[i].version->funcs->set_powergating_state((void *)adev,
1216 state);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001217 if (r)
1218 return r;
Alex Deuchera225bf12016-06-23 11:48:30 -04001219 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001220 }
1221 }
1222 return r;
1223}
1224
Huang Rui6cb2d4e2017-01-05 18:44:41 +08001225void amdgpu_get_clockgating_state(struct amdgpu_device *adev, u32 *flags)
1226{
1227 int i;
1228
1229 for (i = 0; i < adev->num_ip_blocks; i++) {
1230 if (!adev->ip_blocks[i].status.valid)
1231 continue;
1232 if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
1233 adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
1234 }
1235}
1236
Alex Deucher5dbbb602016-06-23 11:41:04 -04001237int amdgpu_wait_for_idle(struct amdgpu_device *adev,
1238 enum amd_ip_block_type block_type)
1239{
1240 int i, r;
1241
1242 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001243 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001244 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001245 if (adev->ip_blocks[i].version->type == block_type) {
1246 r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -04001247 if (r)
1248 return r;
1249 break;
1250 }
1251 }
1252 return 0;
1253
1254}
1255
1256bool amdgpu_is_idle(struct amdgpu_device *adev,
1257 enum amd_ip_block_type block_type)
1258{
1259 int i;
1260
1261 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001262 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001263 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001264 if (adev->ip_blocks[i].version->type == block_type)
1265 return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -04001266 }
1267 return true;
1268
1269}
1270
Alex Deuchera1255102016-10-13 17:41:13 -04001271struct amdgpu_ip_block * amdgpu_get_ip_block(struct amdgpu_device *adev,
1272 enum amd_ip_block_type type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001273{
1274 int i;
1275
1276 for (i = 0; i < adev->num_ip_blocks; i++)
Alex Deuchera1255102016-10-13 17:41:13 -04001277 if (adev->ip_blocks[i].version->type == type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001278 return &adev->ip_blocks[i];
1279
1280 return NULL;
1281}
1282
1283/**
1284 * amdgpu_ip_block_version_cmp
1285 *
1286 * @adev: amdgpu_device pointer
yanyang15fc3aee2015-05-22 14:39:35 -04001287 * @type: enum amd_ip_block_type
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001288 * @major: major version
1289 * @minor: minor version
1290 *
1291 * return 0 if equal or greater
1292 * return 1 if smaller or the ip_block doesn't exist
1293 */
1294int amdgpu_ip_block_version_cmp(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001295 enum amd_ip_block_type type,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001296 u32 major, u32 minor)
1297{
Alex Deuchera1255102016-10-13 17:41:13 -04001298 struct amdgpu_ip_block *ip_block = amdgpu_get_ip_block(adev, type);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001299
Alex Deuchera1255102016-10-13 17:41:13 -04001300 if (ip_block && ((ip_block->version->major > major) ||
1301 ((ip_block->version->major == major) &&
1302 (ip_block->version->minor >= minor))))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001303 return 0;
1304
1305 return 1;
1306}
1307
Alex Deuchera1255102016-10-13 17:41:13 -04001308/**
1309 * amdgpu_ip_block_add
1310 *
1311 * @adev: amdgpu_device pointer
1312 * @ip_block_version: pointer to the IP to add
1313 *
1314 * Adds the IP block driver information to the collection of IPs
1315 * on the asic.
1316 */
1317int amdgpu_ip_block_add(struct amdgpu_device *adev,
1318 const struct amdgpu_ip_block_version *ip_block_version)
1319{
1320 if (!ip_block_version)
1321 return -EINVAL;
1322
1323 adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
1324
1325 return 0;
1326}
1327
Alex Deucher483ef982016-09-30 12:43:04 -04001328static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
Emily Deng9accf2f2016-08-10 16:01:25 +08001329{
1330 adev->enable_virtual_display = false;
1331
1332 if (amdgpu_virtual_display) {
1333 struct drm_device *ddev = adev->ddev;
1334 const char *pci_address_name = pci_name(ddev->pdev);
Emily Deng0f663562016-09-30 13:02:18 -04001335 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
Emily Deng9accf2f2016-08-10 16:01:25 +08001336
1337 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1338 pciaddstr_tmp = pciaddstr;
Emily Deng0f663562016-09-30 13:02:18 -04001339 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1340 pciaddname = strsep(&pciaddname_tmp, ",");
Yintian Tao967de2a2017-01-22 15:16:51 +08001341 if (!strcmp("all", pciaddname)
1342 || !strcmp(pci_address_name, pciaddname)) {
Emily Deng0f663562016-09-30 13:02:18 -04001343 long num_crtc;
1344 int res = -1;
1345
Emily Deng9accf2f2016-08-10 16:01:25 +08001346 adev->enable_virtual_display = true;
Emily Deng0f663562016-09-30 13:02:18 -04001347
1348 if (pciaddname_tmp)
1349 res = kstrtol(pciaddname_tmp, 10,
1350 &num_crtc);
1351
1352 if (!res) {
1353 if (num_crtc < 1)
1354 num_crtc = 1;
1355 if (num_crtc > 6)
1356 num_crtc = 6;
1357 adev->mode_info.num_crtc = num_crtc;
1358 } else {
1359 adev->mode_info.num_crtc = 1;
1360 }
Emily Deng9accf2f2016-08-10 16:01:25 +08001361 break;
1362 }
1363 }
1364
Emily Deng0f663562016-09-30 13:02:18 -04001365 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
1366 amdgpu_virtual_display, pci_address_name,
1367 adev->enable_virtual_display, adev->mode_info.num_crtc);
Emily Deng9accf2f2016-08-10 16:01:25 +08001368
1369 kfree(pciaddstr);
1370 }
1371}
1372
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001373static int amdgpu_early_init(struct amdgpu_device *adev)
1374{
Alex Deucheraaa36a92015-04-20 17:31:14 -04001375 int i, r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001376
Alex Deucher483ef982016-09-30 12:43:04 -04001377 amdgpu_device_enable_virtual_display(adev);
Emily Denga6be7572016-08-08 11:37:50 +08001378
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001379 switch (adev->asic_type) {
Alex Deucheraaa36a92015-04-20 17:31:14 -04001380 case CHIP_TOPAZ:
1381 case CHIP_TONGA:
David Zhang48299f92015-07-08 01:05:16 +08001382 case CHIP_FIJI:
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001383 case CHIP_POLARIS11:
1384 case CHIP_POLARIS10:
Junwei Zhangc4642a42016-12-14 15:32:28 -05001385 case CHIP_POLARIS12:
Alex Deucheraaa36a92015-04-20 17:31:14 -04001386 case CHIP_CARRIZO:
Samuel Li39bb0c92015-10-08 16:31:43 -04001387 case CHIP_STONEY:
1388 if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
Alex Deucheraaa36a92015-04-20 17:31:14 -04001389 adev->family = AMDGPU_FAMILY_CZ;
1390 else
1391 adev->family = AMDGPU_FAMILY_VI;
1392
1393 r = vi_set_ip_blocks(adev);
1394 if (r)
1395 return r;
1396 break;
Ken Wang33f34802016-01-21 17:29:41 +08001397#ifdef CONFIG_DRM_AMDGPU_SI
1398 case CHIP_VERDE:
1399 case CHIP_TAHITI:
1400 case CHIP_PITCAIRN:
1401 case CHIP_OLAND:
1402 case CHIP_HAINAN:
Ken Wang295d0da2016-05-24 21:02:53 +08001403 adev->family = AMDGPU_FAMILY_SI;
Ken Wang33f34802016-01-21 17:29:41 +08001404 r = si_set_ip_blocks(adev);
1405 if (r)
1406 return r;
1407 break;
1408#endif
Alex Deuchera2e73f52015-04-20 17:09:27 -04001409#ifdef CONFIG_DRM_AMDGPU_CIK
1410 case CHIP_BONAIRE:
1411 case CHIP_HAWAII:
1412 case CHIP_KAVERI:
1413 case CHIP_KABINI:
1414 case CHIP_MULLINS:
1415 if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
1416 adev->family = AMDGPU_FAMILY_CI;
1417 else
1418 adev->family = AMDGPU_FAMILY_KV;
1419
1420 r = cik_set_ip_blocks(adev);
1421 if (r)
1422 return r;
1423 break;
1424#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001425 default:
1426 /* FIXME: not supported yet */
1427 return -EINVAL;
1428 }
1429
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001430 if (amdgpu_sriov_vf(adev)) {
1431 r = amdgpu_virt_request_full_gpu(adev, true);
1432 if (r)
1433 return r;
1434 }
1435
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001436 for (i = 0; i < adev->num_ip_blocks; i++) {
1437 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
1438 DRM_ERROR("disabled ip block: %d\n", i);
Alex Deuchera1255102016-10-13 17:41:13 -04001439 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001440 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001441 if (adev->ip_blocks[i].version->funcs->early_init) {
1442 r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001443 if (r == -ENOENT) {
Alex Deuchera1255102016-10-13 17:41:13 -04001444 adev->ip_blocks[i].status.valid = false;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001445 } else if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001446 DRM_ERROR("early_init of IP block <%s> failed %d\n",
1447 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001448 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001449 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001450 adev->ip_blocks[i].status.valid = true;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001451 }
Alex Deucher974e6b62015-07-10 13:59:44 -04001452 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001453 adev->ip_blocks[i].status.valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001454 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001455 }
1456 }
1457
Nicolai Hähnle395d1fb2016-06-02 12:32:07 +02001458 adev->cg_flags &= amdgpu_cg_mask;
1459 adev->pg_flags &= amdgpu_pg_mask;
1460
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001461 return 0;
1462}
1463
1464static int amdgpu_init(struct amdgpu_device *adev)
1465{
1466 int i, r;
1467
1468 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001469 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001470 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001471 r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001472 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001473 DRM_ERROR("sw_init of IP block <%s> failed %d\n",
1474 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001475 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001476 }
Alex Deuchera1255102016-10-13 17:41:13 -04001477 adev->ip_blocks[i].status.sw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001478 /* need to do gmc hw init early so we can allocate gpu mem */
Alex Deuchera1255102016-10-13 17:41:13 -04001479 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001480 r = amdgpu_vram_scratch_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001481 if (r) {
1482 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001483 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001484 }
Alex Deuchera1255102016-10-13 17:41:13 -04001485 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001486 if (r) {
1487 DRM_ERROR("hw_init %d failed %d\n", i, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001488 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001489 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001490 r = amdgpu_wb_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001491 if (r) {
1492 DRM_ERROR("amdgpu_wb_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001493 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001494 }
Alex Deuchera1255102016-10-13 17:41:13 -04001495 adev->ip_blocks[i].status.hw = true;
Monk Liu24936642017-01-09 15:54:32 +08001496
1497 /* right after GMC hw init, we create CSA */
1498 if (amdgpu_sriov_vf(adev)) {
1499 r = amdgpu_allocate_static_csa(adev);
1500 if (r) {
1501 DRM_ERROR("allocate CSA failed %d\n", r);
1502 return r;
1503 }
1504 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001505 }
1506 }
1507
1508 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001509 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001510 continue;
1511 /* gmc hw init is done early */
Alex Deuchera1255102016-10-13 17:41:13 -04001512 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001513 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001514 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001515 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001516 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
1517 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001518 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001519 }
Alex Deuchera1255102016-10-13 17:41:13 -04001520 adev->ip_blocks[i].status.hw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001521 }
1522
1523 return 0;
1524}
1525
1526static int amdgpu_late_init(struct amdgpu_device *adev)
1527{
1528 int i = 0, r;
1529
1530 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001531 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001532 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001533 if (adev->ip_blocks[i].version->funcs->late_init) {
1534 r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001535 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001536 DRM_ERROR("late_init of IP block <%s> failed %d\n",
1537 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001538 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001539 }
Alex Deuchera1255102016-10-13 17:41:13 -04001540 adev->ip_blocks[i].status.late_initialized = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001541 }
Alex Deucher4a446d52016-10-07 14:48:18 -04001542 /* skip CG for VCE/UVD, it's handled specially */
Alex Deuchera1255102016-10-13 17:41:13 -04001543 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1544 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
Alex Deucher4a446d52016-10-07 14:48:18 -04001545 /* enable clockgating to save power */
Alex Deuchera1255102016-10-13 17:41:13 -04001546 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1547 AMD_CG_STATE_GATE);
Alex Deucher4a446d52016-10-07 14:48:18 -04001548 if (r) {
1549 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001550 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher4a446d52016-10-07 14:48:18 -04001551 return r;
1552 }
Arindam Nathb0b00ff2016-10-07 19:01:37 +05301553 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001554 }
1555
1556 return 0;
1557}
1558
1559static int amdgpu_fini(struct amdgpu_device *adev)
1560{
1561 int i, r;
1562
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001563 /* need to disable SMC first */
1564 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001565 if (!adev->ip_blocks[i].status.hw)
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001566 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001567 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001568 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
Alex Deuchera1255102016-10-13 17:41:13 -04001569 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1570 AMD_CG_STATE_UNGATE);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001571 if (r) {
1572 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001573 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001574 return r;
1575 }
Alex Deuchera1255102016-10-13 17:41:13 -04001576 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001577 /* XXX handle errors */
1578 if (r) {
1579 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001580 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001581 }
Alex Deuchera1255102016-10-13 17:41:13 -04001582 adev->ip_blocks[i].status.hw = false;
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001583 break;
1584 }
1585 }
1586
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001587 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001588 if (!adev->ip_blocks[i].status.hw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001589 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001590 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001591 amdgpu_wb_fini(adev);
1592 amdgpu_vram_scratch_fini(adev);
1593 }
Rex Zhu8201a672016-11-24 21:44:44 +08001594
1595 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1596 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1597 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
1598 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1599 AMD_CG_STATE_UNGATE);
1600 if (r) {
1601 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1602 adev->ip_blocks[i].version->funcs->name, r);
1603 return r;
1604 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001605 }
Rex Zhu8201a672016-11-24 21:44:44 +08001606
Alex Deuchera1255102016-10-13 17:41:13 -04001607 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001608 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001609 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001610 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
1611 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001612 }
Rex Zhu8201a672016-11-24 21:44:44 +08001613
Alex Deuchera1255102016-10-13 17:41:13 -04001614 adev->ip_blocks[i].status.hw = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001615 }
1616
1617 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001618 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001619 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001620 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001621 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001622 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001623 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
1624 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001625 }
Alex Deuchera1255102016-10-13 17:41:13 -04001626 adev->ip_blocks[i].status.sw = false;
1627 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001628 }
1629
Monk Liua6dcfd92016-05-19 14:36:34 +08001630 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001631 if (!adev->ip_blocks[i].status.late_initialized)
Grazvydas Ignotas8a2eef12016-10-03 00:06:44 +03001632 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001633 if (adev->ip_blocks[i].version->funcs->late_fini)
1634 adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
1635 adev->ip_blocks[i].status.late_initialized = false;
Monk Liua6dcfd92016-05-19 14:36:34 +08001636 }
1637
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001638 if (amdgpu_sriov_vf(adev)) {
Monk Liu24936642017-01-09 15:54:32 +08001639 amdgpu_bo_free_kernel(&adev->virt.csa_obj, &adev->virt.csa_vmid0_addr, NULL);
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001640 amdgpu_virt_release_full_gpu(adev, false);
1641 }
Monk Liu24936642017-01-09 15:54:32 +08001642
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001643 return 0;
1644}
1645
Alex Deucherfaefba92016-12-06 10:38:29 -05001646int amdgpu_suspend(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001647{
1648 int i, r;
1649
Xiangliang Yue941ea92017-01-18 12:47:55 +08001650 if (amdgpu_sriov_vf(adev))
1651 amdgpu_virt_request_full_gpu(adev, false);
1652
Flora Cuic5a93a22016-02-26 10:45:25 +08001653 /* ungate SMC block first */
1654 r = amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC,
1655 AMD_CG_STATE_UNGATE);
1656 if (r) {
1657 DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n",r);
1658 }
1659
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001660 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001661 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001662 continue;
1663 /* ungate blocks so that suspend can properly shut them down */
Flora Cuic5a93a22016-02-26 10:45:25 +08001664 if (i != AMD_IP_BLOCK_TYPE_SMC) {
Alex Deuchera1255102016-10-13 17:41:13 -04001665 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1666 AMD_CG_STATE_UNGATE);
Flora Cuic5a93a22016-02-26 10:45:25 +08001667 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001668 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1669 adev->ip_blocks[i].version->funcs->name, r);
Flora Cuic5a93a22016-02-26 10:45:25 +08001670 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001671 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001672 /* XXX handle errors */
Alex Deuchera1255102016-10-13 17:41:13 -04001673 r = adev->ip_blocks[i].version->funcs->suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001674 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001675 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001676 DRM_ERROR("suspend of IP block <%s> failed %d\n",
1677 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001678 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001679 }
1680
Xiangliang Yue941ea92017-01-18 12:47:55 +08001681 if (amdgpu_sriov_vf(adev))
1682 amdgpu_virt_release_full_gpu(adev, false);
1683
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001684 return 0;
1685}
1686
Monk Liue4f0fdc2017-02-09 11:55:49 +08001687static int amdgpu_sriov_reinit_early(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08001688{
1689 int i, r;
1690
1691 for (i = 0; i < adev->num_ip_blocks; i++) {
1692 if (!adev->ip_blocks[i].status.valid)
1693 continue;
1694
1695 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
1696 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
1697 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH)
Monk Liue4f0fdc2017-02-09 11:55:49 +08001698 r = adev->ip_blocks[i].version->funcs->hw_init(adev);
Monk Liua90ad3c2017-01-23 14:22:08 +08001699
1700 if (r) {
1701 DRM_ERROR("resume of IP block <%s> failed %d\n",
1702 adev->ip_blocks[i].version->funcs->name, r);
1703 return r;
1704 }
1705 }
1706
1707 return 0;
1708}
1709
Monk Liue4f0fdc2017-02-09 11:55:49 +08001710static int amdgpu_sriov_reinit_late(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08001711{
1712 int i, r;
1713
1714 for (i = 0; i < adev->num_ip_blocks; i++) {
1715 if (!adev->ip_blocks[i].status.valid)
1716 continue;
1717
1718 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
1719 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
1720 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH )
1721 continue;
1722
Monk Liue4f0fdc2017-02-09 11:55:49 +08001723 r = adev->ip_blocks[i].version->funcs->hw_init(adev);
Monk Liua90ad3c2017-01-23 14:22:08 +08001724 if (r) {
1725 DRM_ERROR("resume of IP block <%s> failed %d\n",
1726 adev->ip_blocks[i].version->funcs->name, r);
1727 return r;
1728 }
1729 }
1730
1731 return 0;
1732}
1733
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001734static int amdgpu_resume(struct amdgpu_device *adev)
1735{
1736 int i, r;
1737
1738 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001739 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001740 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001741 r = adev->ip_blocks[i].version->funcs->resume(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001742 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001743 DRM_ERROR("resume of IP block <%s> failed %d\n",
1744 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001745 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001746 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001747 }
1748
1749 return 0;
1750}
1751
Monk Liu4e99a442016-03-31 13:26:59 +08001752static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
Andres Rodriguez048765a2016-06-11 02:51:32 -04001753{
Monk Liu4e99a442016-03-31 13:26:59 +08001754 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
Xiangliang Yu5a5099c2017-01-09 18:06:57 -05001755 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
Andres Rodriguez048765a2016-06-11 02:51:32 -04001756}
1757
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001758/**
1759 * amdgpu_device_init - initialize the driver
1760 *
1761 * @adev: amdgpu_device pointer
1762 * @pdev: drm dev pointer
1763 * @pdev: pci dev pointer
1764 * @flags: driver flags
1765 *
1766 * Initializes the driver info and hw (all asics).
1767 * Returns 0 for success or an error on failure.
1768 * Called at driver startup.
1769 */
1770int amdgpu_device_init(struct amdgpu_device *adev,
1771 struct drm_device *ddev,
1772 struct pci_dev *pdev,
1773 uint32_t flags)
1774{
1775 int r, i;
1776 bool runtime = false;
Marek Olšák95844d22016-08-17 23:49:27 +02001777 u32 max_MBps;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001778
1779 adev->shutdown = false;
1780 adev->dev = &pdev->dev;
1781 adev->ddev = ddev;
1782 adev->pdev = pdev;
1783 adev->flags = flags;
Jammy Zhou2f7d10b2015-07-22 11:29:01 +08001784 adev->asic_type = flags & AMD_ASIC_MASK;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001785 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
1786 adev->mc.gtt_size = 512 * 1024 * 1024;
1787 adev->accel_working = false;
1788 adev->num_rings = 0;
1789 adev->mman.buffer_funcs = NULL;
1790 adev->mman.buffer_funcs_ring = NULL;
1791 adev->vm_manager.vm_pte_funcs = NULL;
Christian König2d55e452016-02-08 17:37:38 +01001792 adev->vm_manager.vm_pte_num_rings = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001793 adev->gart.gart_funcs = NULL;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001794 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001795
1796 adev->smc_rreg = &amdgpu_invalid_rreg;
1797 adev->smc_wreg = &amdgpu_invalid_wreg;
1798 adev->pcie_rreg = &amdgpu_invalid_rreg;
1799 adev->pcie_wreg = &amdgpu_invalid_wreg;
Huang Rui36b9a952016-08-31 13:23:25 +08001800 adev->pciep_rreg = &amdgpu_invalid_rreg;
1801 adev->pciep_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001802 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
1803 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
1804 adev->didt_rreg = &amdgpu_invalid_rreg;
1805 adev->didt_wreg = &amdgpu_invalid_wreg;
Rex Zhuccdbb202016-06-08 12:47:41 +08001806 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
1807 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001808 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
1809 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
1810
Rex Zhuccdbb202016-06-08 12:47:41 +08001811
Alex Deucher3e39ab92015-06-05 15:04:33 -04001812 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
1813 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
1814 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001815
1816 /* mutex initialization are all done here so we
1817 * can recall function without having locking issues */
Christian König8d0a7ce2015-11-03 20:58:50 +01001818 mutex_init(&adev->vm_manager.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001819 atomic_set(&adev->irq.ih.lock, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001820 mutex_init(&adev->pm.mutex);
1821 mutex_init(&adev->gfx.gpu_clock_mutex);
1822 mutex_init(&adev->srbm_mutex);
1823 mutex_init(&adev->grbm_idx_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001824 mutex_init(&adev->mn_lock);
1825 hash_init(adev->mn_hash);
1826
1827 amdgpu_check_arguments(adev);
1828
1829 /* Registers mapping */
1830 /* TODO: block userspace mapping of io register */
1831 spin_lock_init(&adev->mmio_idx_lock);
1832 spin_lock_init(&adev->smc_idx_lock);
1833 spin_lock_init(&adev->pcie_idx_lock);
1834 spin_lock_init(&adev->uvd_ctx_idx_lock);
1835 spin_lock_init(&adev->didt_idx_lock);
Rex Zhuccdbb202016-06-08 12:47:41 +08001836 spin_lock_init(&adev->gc_cac_idx_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001837 spin_lock_init(&adev->audio_endpt_idx_lock);
Marek Olšák95844d22016-08-17 23:49:27 +02001838 spin_lock_init(&adev->mm_stats.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001839
Chunming Zhou0c4e7fa2016-08-17 11:41:30 +08001840 INIT_LIST_HEAD(&adev->shadow_list);
1841 mutex_init(&adev->shadow_list_lock);
1842
Chunming Zhou5c1354b2016-08-30 16:13:10 +08001843 INIT_LIST_HEAD(&adev->gtt_list);
1844 spin_lock_init(&adev->gtt_list_lock);
1845
Ken Wangda69c1612016-01-21 19:08:55 +08001846 if (adev->asic_type >= CHIP_BONAIRE) {
1847 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
1848 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
1849 } else {
1850 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
1851 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
1852 }
Chunming Zhou5c1354b2016-08-30 16:13:10 +08001853
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001854 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
1855 if (adev->rmmio == NULL) {
1856 return -ENOMEM;
1857 }
1858 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
1859 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
1860
Ken Wangda69c1612016-01-21 19:08:55 +08001861 if (adev->asic_type >= CHIP_BONAIRE)
1862 /* doorbell bar mapping */
1863 amdgpu_doorbell_init(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001864
1865 /* io port mapping */
1866 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
1867 if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
1868 adev->rio_mem_size = pci_resource_len(adev->pdev, i);
1869 adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
1870 break;
1871 }
1872 }
1873 if (adev->rio_mem == NULL)
Amber Linb64a18c2017-01-04 08:06:58 -05001874 DRM_INFO("PCI I/O BAR is not found.\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001875
1876 /* early init functions */
1877 r = amdgpu_early_init(adev);
1878 if (r)
1879 return r;
1880
1881 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
1882 /* this will fail for cards that aren't VGA class devices, just
1883 * ignore it */
1884 vga_client_register(adev->pdev, adev, NULL, amdgpu_vga_set_decode);
1885
1886 if (amdgpu_runtime_pm == 1)
1887 runtime = true;
Alex Deuchere9bef452016-04-25 13:12:18 -04001888 if (amdgpu_device_is_px(ddev))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001889 runtime = true;
1890 vga_switcheroo_register_client(adev->pdev, &amdgpu_switcheroo_ops, runtime);
1891 if (runtime)
1892 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
1893
1894 /* Read BIOS */
Alex Deucher83ba1262016-06-03 18:21:41 -04001895 if (!amdgpu_get_bios(adev)) {
1896 r = -EINVAL;
1897 goto failed;
1898 }
Nils Wallméniusf7e9e9f2016-12-14 21:52:45 +01001899
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001900 r = amdgpu_atombios_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001901 if (r) {
1902 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001903 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001904 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001905
Monk Liu4e99a442016-03-31 13:26:59 +08001906 /* detect if we are with an SRIOV vbios */
1907 amdgpu_device_detect_sriov_bios(adev);
Andres Rodriguez048765a2016-06-11 02:51:32 -04001908
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001909 /* Post card if necessary */
Monk Liubec86372016-09-14 19:38:08 +08001910 if (amdgpu_vpost_needed(adev)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001911 if (!adev->bios) {
Monk Liubec86372016-09-14 19:38:08 +08001912 dev_err(adev->dev, "no vBIOS found\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001913 r = -EINVAL;
1914 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001915 }
Monk Liubec86372016-09-14 19:38:08 +08001916 DRM_INFO("GPU posting now...\n");
Monk Liu4e99a442016-03-31 13:26:59 +08001917 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
1918 if (r) {
1919 dev_err(adev->dev, "gpu post error!\n");
1920 goto failed;
1921 }
1922 } else {
1923 DRM_INFO("GPU post is not needed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001924 }
1925
1926 /* Initialize clocks */
1927 r = amdgpu_atombios_get_clock_info(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001928 if (r) {
1929 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001930 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001931 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001932 /* init i2c buses */
1933 amdgpu_atombios_i2c_init(adev);
1934
1935 /* Fence driver */
1936 r = amdgpu_fence_driver_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001937 if (r) {
1938 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001939 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001940 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001941
1942 /* init the mode config */
1943 drm_mode_config_init(adev->ddev);
1944
1945 r = amdgpu_init(adev);
1946 if (r) {
Alex Deucher2c1a2782015-12-07 17:02:53 -05001947 dev_err(adev->dev, "amdgpu_init failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001948 amdgpu_fini(adev);
Alex Deucher83ba1262016-06-03 18:21:41 -04001949 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001950 }
1951
1952 adev->accel_working = true;
1953
Marek Olšák95844d22016-08-17 23:49:27 +02001954 /* Initialize the buffer migration limit. */
1955 if (amdgpu_moverate >= 0)
1956 max_MBps = amdgpu_moverate;
1957 else
1958 max_MBps = 8; /* Allow 8 MB/s. */
1959 /* Get a log2 for easy divisions. */
1960 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
1961
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001962 r = amdgpu_ib_pool_init(adev);
1963 if (r) {
1964 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
Alex Deucher83ba1262016-06-03 18:21:41 -04001965 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001966 }
1967
1968 r = amdgpu_ib_ring_tests(adev);
1969 if (r)
1970 DRM_ERROR("ib ring test failed (%d).\n", r);
1971
Monk Liu9bc92b92017-02-08 17:38:13 +08001972 amdgpu_fbdev_init(adev);
1973
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001974 r = amdgpu_gem_debugfs_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08001975 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001976 DRM_ERROR("registering gem debugfs failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001977
1978 r = amdgpu_debugfs_regs_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08001979 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001980 DRM_ERROR("registering register debugfs failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001981
Huang Rui50ab2532016-06-12 15:51:09 +08001982 r = amdgpu_debugfs_firmware_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08001983 if (r)
Huang Rui50ab2532016-06-12 15:51:09 +08001984 DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
Huang Rui50ab2532016-06-12 15:51:09 +08001985
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001986 if ((amdgpu_testing & 1)) {
1987 if (adev->accel_working)
1988 amdgpu_test_moves(adev);
1989 else
1990 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
1991 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001992 if (amdgpu_benchmarking) {
1993 if (adev->accel_working)
1994 amdgpu_benchmark(adev, amdgpu_benchmarking);
1995 else
1996 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
1997 }
1998
1999 /* enable clockgating, etc. after ib tests, etc. since some blocks require
2000 * explicit gating rather than handling it automatically.
2001 */
2002 r = amdgpu_late_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002003 if (r) {
2004 dev_err(adev->dev, "amdgpu_late_init failed\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04002005 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002006 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002007
2008 return 0;
Alex Deucher83ba1262016-06-03 18:21:41 -04002009
2010failed:
2011 if (runtime)
2012 vga_switcheroo_fini_domain_pm_ops(adev->dev);
2013 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002014}
2015
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002016/**
2017 * amdgpu_device_fini - tear down the driver
2018 *
2019 * @adev: amdgpu_device pointer
2020 *
2021 * Tear down the driver info (all asics).
2022 * Called at driver shutdown.
2023 */
2024void amdgpu_device_fini(struct amdgpu_device *adev)
2025{
2026 int r;
2027
2028 DRM_INFO("amdgpu: finishing device.\n");
2029 adev->shutdown = true;
Grazvydas Ignotasa951ed82016-09-25 23:34:48 +03002030 drm_crtc_force_disable_all(adev->ddev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002031 /* evict vram memory */
2032 amdgpu_bo_evict_vram(adev);
2033 amdgpu_ib_pool_fini(adev);
2034 amdgpu_fence_driver_fini(adev);
2035 amdgpu_fbdev_fini(adev);
2036 r = amdgpu_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002037 adev->accel_working = false;
2038 /* free i2c buses */
2039 amdgpu_i2c_fini(adev);
2040 amdgpu_atombios_fini(adev);
2041 kfree(adev->bios);
2042 adev->bios = NULL;
2043 vga_switcheroo_unregister_client(adev->pdev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002044 if (adev->flags & AMD_IS_PX)
2045 vga_switcheroo_fini_domain_pm_ops(adev->dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002046 vga_client_register(adev->pdev, NULL, NULL, NULL);
2047 if (adev->rio_mem)
2048 pci_iounmap(adev->pdev, adev->rio_mem);
2049 adev->rio_mem = NULL;
2050 iounmap(adev->rmmio);
2051 adev->rmmio = NULL;
Ken Wangda69c1612016-01-21 19:08:55 +08002052 if (adev->asic_type >= CHIP_BONAIRE)
2053 amdgpu_doorbell_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002054 amdgpu_debugfs_regs_cleanup(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002055}
2056
2057
2058/*
2059 * Suspend & resume.
2060 */
2061/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002062 * amdgpu_device_suspend - initiate device suspend
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002063 *
2064 * @pdev: drm dev pointer
2065 * @state: suspend state
2066 *
2067 * Puts the hw in the suspend state (all asics).
2068 * Returns 0 for success or an error on failure.
2069 * Called at driver suspend.
2070 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002071int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002072{
2073 struct amdgpu_device *adev;
2074 struct drm_crtc *crtc;
2075 struct drm_connector *connector;
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002076 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002077
2078 if (dev == NULL || dev->dev_private == NULL) {
2079 return -ENODEV;
2080 }
2081
2082 adev = dev->dev_private;
2083
2084 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2085 return 0;
2086
2087 drm_kms_helper_poll_disable(dev);
2088
2089 /* turn off display hw */
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002090 drm_modeset_lock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002091 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2092 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
2093 }
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002094 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002095
Alex Deucher756e6882015-10-08 00:03:36 -04002096 /* unpin the front buffers and cursors */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002097 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Alex Deucher756e6882015-10-08 00:03:36 -04002098 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002099 struct amdgpu_framebuffer *rfb = to_amdgpu_framebuffer(crtc->primary->fb);
2100 struct amdgpu_bo *robj;
2101
Alex Deucher756e6882015-10-08 00:03:36 -04002102 if (amdgpu_crtc->cursor_bo) {
2103 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
2104 r = amdgpu_bo_reserve(aobj, false);
2105 if (r == 0) {
2106 amdgpu_bo_unpin(aobj);
2107 amdgpu_bo_unreserve(aobj);
2108 }
2109 }
2110
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002111 if (rfb == NULL || rfb->obj == NULL) {
2112 continue;
2113 }
2114 robj = gem_to_amdgpu_bo(rfb->obj);
2115 /* don't unpin kernel fb objects */
2116 if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
2117 r = amdgpu_bo_reserve(robj, false);
2118 if (r == 0) {
2119 amdgpu_bo_unpin(robj);
2120 amdgpu_bo_unreserve(robj);
2121 }
2122 }
2123 }
2124 /* evict vram memory */
2125 amdgpu_bo_evict_vram(adev);
2126
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002127 amdgpu_fence_driver_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002128
2129 r = amdgpu_suspend(adev);
2130
Alex Deuchera0a71e42016-10-10 12:41:36 -04002131 /* evict remaining vram memory
2132 * This second call to evict vram is to evict the gart page table
2133 * using the CPU.
2134 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002135 amdgpu_bo_evict_vram(adev);
2136
Alex Deuchere695e772016-10-19 14:40:58 -04002137 amdgpu_atombios_scratch_regs_save(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002138 pci_save_state(dev->pdev);
2139 if (suspend) {
2140 /* Shut down the device */
2141 pci_disable_device(dev->pdev);
2142 pci_set_power_state(dev->pdev, PCI_D3hot);
jimqu74b0b152016-09-07 17:09:12 +08002143 } else {
2144 r = amdgpu_asic_reset(adev);
2145 if (r)
2146 DRM_ERROR("amdgpu asic reset failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002147 }
2148
2149 if (fbcon) {
2150 console_lock();
2151 amdgpu_fbdev_set_suspend(adev, 1);
2152 console_unlock();
2153 }
2154 return 0;
2155}
2156
2157/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002158 * amdgpu_device_resume - initiate device resume
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002159 *
2160 * @pdev: drm dev pointer
2161 *
2162 * Bring the hw back to operating state (all asics).
2163 * Returns 0 for success or an error on failure.
2164 * Called at driver resume.
2165 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002166int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002167{
2168 struct drm_connector *connector;
2169 struct amdgpu_device *adev = dev->dev_private;
Alex Deucher756e6882015-10-08 00:03:36 -04002170 struct drm_crtc *crtc;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002171 int r;
2172
2173 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2174 return 0;
2175
jimqu74b0b152016-09-07 17:09:12 +08002176 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002177 console_lock();
jimqu74b0b152016-09-07 17:09:12 +08002178
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002179 if (resume) {
2180 pci_set_power_state(dev->pdev, PCI_D0);
2181 pci_restore_state(dev->pdev);
jimqu74b0b152016-09-07 17:09:12 +08002182 r = pci_enable_device(dev->pdev);
2183 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002184 if (fbcon)
2185 console_unlock();
jimqu74b0b152016-09-07 17:09:12 +08002186 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002187 }
2188 }
Alex Deuchere695e772016-10-19 14:40:58 -04002189 amdgpu_atombios_scratch_regs_restore(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002190
2191 /* post card */
Jim Quc836fec2017-02-10 15:59:59 +08002192 if (amdgpu_need_post(adev)) {
jimqu74b0b152016-09-07 17:09:12 +08002193 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2194 if (r)
2195 DRM_ERROR("amdgpu asic init failed\n");
2196 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002197
2198 r = amdgpu_resume(adev);
Flora Cuica198522016-02-04 15:10:08 +08002199 if (r)
2200 DRM_ERROR("amdgpu_resume failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002201
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002202 amdgpu_fence_driver_resume(adev);
2203
Flora Cuica198522016-02-04 15:10:08 +08002204 if (resume) {
2205 r = amdgpu_ib_ring_tests(adev);
2206 if (r)
2207 DRM_ERROR("ib ring test failed (%d).\n", r);
2208 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002209
2210 r = amdgpu_late_init(adev);
Jim Quc085bd52017-03-01 15:53:29 +08002211 if (r) {
2212 if (fbcon)
2213 console_unlock();
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002214 return r;
Jim Quc085bd52017-03-01 15:53:29 +08002215 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002216
Alex Deucher756e6882015-10-08 00:03:36 -04002217 /* pin cursors */
2218 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2219 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2220
2221 if (amdgpu_crtc->cursor_bo) {
2222 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
2223 r = amdgpu_bo_reserve(aobj, false);
2224 if (r == 0) {
2225 r = amdgpu_bo_pin(aobj,
2226 AMDGPU_GEM_DOMAIN_VRAM,
2227 &amdgpu_crtc->cursor_addr);
2228 if (r != 0)
2229 DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
2230 amdgpu_bo_unreserve(aobj);
2231 }
2232 }
2233 }
2234
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002235 /* blat the mode back in */
2236 if (fbcon) {
2237 drm_helper_resume_force_mode(dev);
2238 /* turn on display hw */
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002239 drm_modeset_lock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002240 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2241 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
2242 }
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002243 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002244 }
2245
2246 drm_kms_helper_poll_enable(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002247
2248 /*
2249 * Most of the connector probing functions try to acquire runtime pm
2250 * refs to ensure that the GPU is powered on when connector polling is
2251 * performed. Since we're calling this from a runtime PM callback,
2252 * trying to acquire rpm refs will cause us to deadlock.
2253 *
2254 * Since we're guaranteed to be holding the rpm lock, it's safe to
2255 * temporarily disable the rpm helpers so this doesn't deadlock us.
2256 */
2257#ifdef CONFIG_PM
2258 dev->dev->power.disable_depth++;
2259#endif
Alex Deucher54fb2a52015-11-24 14:30:56 -05002260 drm_helper_hpd_irq_event(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002261#ifdef CONFIG_PM
2262 dev->dev->power.disable_depth--;
2263#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002264
2265 if (fbcon) {
2266 amdgpu_fbdev_set_suspend(adev, 0);
2267 console_unlock();
2268 }
2269
2270 return 0;
2271}
2272
Chunming Zhou63fbf422016-07-15 11:19:20 +08002273static bool amdgpu_check_soft_reset(struct amdgpu_device *adev)
2274{
2275 int i;
2276 bool asic_hang = false;
2277
2278 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002279 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou63fbf422016-07-15 11:19:20 +08002280 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002281 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
2282 adev->ip_blocks[i].status.hang =
2283 adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
2284 if (adev->ip_blocks[i].status.hang) {
2285 DRM_INFO("IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
Chunming Zhou63fbf422016-07-15 11:19:20 +08002286 asic_hang = true;
2287 }
2288 }
2289 return asic_hang;
2290}
2291
Baoyou Xie4d446652016-09-18 22:09:35 +08002292static int amdgpu_pre_soft_reset(struct amdgpu_device *adev)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002293{
2294 int i, r = 0;
2295
2296 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002297 if (!adev->ip_blocks[i].status.valid)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002298 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002299 if (adev->ip_blocks[i].status.hang &&
2300 adev->ip_blocks[i].version->funcs->pre_soft_reset) {
2301 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
Chunming Zhoud31a5012016-07-18 10:04:34 +08002302 if (r)
2303 return r;
2304 }
2305 }
2306
2307 return 0;
2308}
2309
Chunming Zhou35d782f2016-07-15 15:57:13 +08002310static bool amdgpu_need_full_reset(struct amdgpu_device *adev)
2311{
Alex Deucherda146d32016-10-13 16:07:03 -04002312 int i;
2313
2314 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002315 if (!adev->ip_blocks[i].status.valid)
Alex Deucherda146d32016-10-13 16:07:03 -04002316 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002317 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
2318 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
2319 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
2320 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE)) {
2321 if (adev->ip_blocks[i].status.hang) {
Alex Deucherda146d32016-10-13 16:07:03 -04002322 DRM_INFO("Some block need full reset!\n");
2323 return true;
2324 }
2325 }
Chunming Zhou35d782f2016-07-15 15:57:13 +08002326 }
2327 return false;
2328}
2329
2330static int amdgpu_soft_reset(struct amdgpu_device *adev)
2331{
2332 int i, r = 0;
2333
2334 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002335 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002336 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002337 if (adev->ip_blocks[i].status.hang &&
2338 adev->ip_blocks[i].version->funcs->soft_reset) {
2339 r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002340 if (r)
2341 return r;
2342 }
2343 }
2344
2345 return 0;
2346}
2347
2348static int amdgpu_post_soft_reset(struct amdgpu_device *adev)
2349{
2350 int i, r = 0;
2351
2352 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002353 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002354 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002355 if (adev->ip_blocks[i].status.hang &&
2356 adev->ip_blocks[i].version->funcs->post_soft_reset)
2357 r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002358 if (r)
2359 return r;
2360 }
2361
2362 return 0;
2363}
2364
Chunming Zhou3ad81f12016-08-05 17:30:17 +08002365bool amdgpu_need_backup(struct amdgpu_device *adev)
2366{
2367 if (adev->flags & AMD_IS_APU)
2368 return false;
2369
2370 return amdgpu_lockup_timeout > 0 ? true : false;
2371}
2372
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002373static int amdgpu_recover_vram_from_shadow(struct amdgpu_device *adev,
2374 struct amdgpu_ring *ring,
2375 struct amdgpu_bo *bo,
Chris Wilsonf54d1862016-10-25 13:00:45 +01002376 struct dma_fence **fence)
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002377{
2378 uint32_t domain;
2379 int r;
2380
2381 if (!bo->shadow)
2382 return 0;
2383
2384 r = amdgpu_bo_reserve(bo, false);
2385 if (r)
2386 return r;
2387 domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
2388 /* if bo has been evicted, then no need to recover */
2389 if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
2390 r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
2391 NULL, fence, true);
2392 if (r) {
2393 DRM_ERROR("recover page table failed!\n");
2394 goto err;
2395 }
2396 }
2397err:
2398 amdgpu_bo_unreserve(bo);
2399 return r;
2400}
2401
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002402/**
Monk Liua90ad3c2017-01-23 14:22:08 +08002403 * amdgpu_sriov_gpu_reset - reset the asic
2404 *
2405 * @adev: amdgpu device pointer
2406 * @voluntary: if this reset is requested by guest.
2407 * (true means by guest and false means by HYPERVISOR )
2408 *
2409 * Attempt the reset the GPU if it has hung (all asics).
2410 * for SRIOV case.
2411 * Returns 0 for success or an error on failure.
2412 */
2413int amdgpu_sriov_gpu_reset(struct amdgpu_device *adev, bool voluntary)
2414{
2415 int i, r = 0;
2416 int resched;
2417 struct amdgpu_bo *bo, *tmp;
2418 struct amdgpu_ring *ring;
2419 struct dma_fence *fence = NULL, *next = NULL;
2420
Monk Liu147b5982017-01-25 15:48:01 +08002421 mutex_lock(&adev->virt.lock_reset);
Monk Liua90ad3c2017-01-23 14:22:08 +08002422 atomic_inc(&adev->gpu_reset_counter);
Monk Liu1fb37a32017-01-26 15:36:37 +08002423 adev->gfx.in_reset = true;
Monk Liua90ad3c2017-01-23 14:22:08 +08002424
2425 /* block TTM */
2426 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
2427
2428 /* block scheduler */
2429 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2430 ring = adev->rings[i];
2431
2432 if (!ring || !ring->sched.thread)
2433 continue;
2434
2435 kthread_park(ring->sched.thread);
2436 amd_sched_hw_job_reset(&ring->sched);
2437 }
2438
2439 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
2440 amdgpu_fence_driver_force_completion(adev);
2441
2442 /* request to take full control of GPU before re-initialization */
2443 if (voluntary)
2444 amdgpu_virt_reset_gpu(adev);
2445 else
2446 amdgpu_virt_request_full_gpu(adev, true);
2447
2448
2449 /* Resume IP prior to SMC */
Monk Liue4f0fdc2017-02-09 11:55:49 +08002450 amdgpu_sriov_reinit_early(adev);
Monk Liua90ad3c2017-01-23 14:22:08 +08002451
2452 /* we need recover gart prior to run SMC/CP/SDMA resume */
2453 amdgpu_ttm_recover_gart(adev);
2454
2455 /* now we are okay to resume SMC/CP/SDMA */
Monk Liue4f0fdc2017-02-09 11:55:49 +08002456 amdgpu_sriov_reinit_late(adev);
Monk Liua90ad3c2017-01-23 14:22:08 +08002457
2458 amdgpu_irq_gpu_reset_resume_helper(adev);
2459
2460 if (amdgpu_ib_ring_tests(adev))
2461 dev_err(adev->dev, "[GPU_RESET] ib ring test failed (%d).\n", r);
2462
2463 /* release full control of GPU after ib test */
2464 amdgpu_virt_release_full_gpu(adev, true);
2465
2466 DRM_INFO("recover vram bo from shadow\n");
2467
2468 ring = adev->mman.buffer_funcs_ring;
2469 mutex_lock(&adev->shadow_list_lock);
2470 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
2471 amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
2472 if (fence) {
2473 r = dma_fence_wait(fence, false);
2474 if (r) {
2475 WARN(r, "recovery from shadow isn't completed\n");
2476 break;
2477 }
2478 }
2479
2480 dma_fence_put(fence);
2481 fence = next;
2482 }
2483 mutex_unlock(&adev->shadow_list_lock);
2484
2485 if (fence) {
2486 r = dma_fence_wait(fence, false);
2487 if (r)
2488 WARN(r, "recovery from shadow isn't completed\n");
2489 }
2490 dma_fence_put(fence);
2491
2492 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2493 struct amdgpu_ring *ring = adev->rings[i];
2494 if (!ring || !ring->sched.thread)
2495 continue;
2496
2497 amd_sched_job_recovery(&ring->sched);
2498 kthread_unpark(ring->sched.thread);
2499 }
2500
2501 drm_helper_resume_force_mode(adev->ddev);
2502 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
2503 if (r) {
2504 /* bad news, how to tell it to userspace ? */
2505 dev_info(adev->dev, "GPU reset failed\n");
2506 }
2507
Monk Liu1fb37a32017-01-26 15:36:37 +08002508 adev->gfx.in_reset = false;
Monk Liu147b5982017-01-25 15:48:01 +08002509 mutex_unlock(&adev->virt.lock_reset);
Monk Liua90ad3c2017-01-23 14:22:08 +08002510 return r;
2511}
2512
2513/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002514 * amdgpu_gpu_reset - reset the asic
2515 *
2516 * @adev: amdgpu device pointer
2517 *
2518 * Attempt the reset the GPU if it has hung (all asics).
2519 * Returns 0 for success or an error on failure.
2520 */
2521int amdgpu_gpu_reset(struct amdgpu_device *adev)
2522{
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002523 int i, r;
2524 int resched;
Chunming Zhou35d782f2016-07-15 15:57:13 +08002525 bool need_full_reset;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002526
Xiangliang Yufb140b22016-12-17 22:48:57 +08002527 if (amdgpu_sriov_vf(adev))
Monk Liua90ad3c2017-01-23 14:22:08 +08002528 return amdgpu_sriov_gpu_reset(adev, true);
Xiangliang Yufb140b22016-12-17 22:48:57 +08002529
Chunming Zhou63fbf422016-07-15 11:19:20 +08002530 if (!amdgpu_check_soft_reset(adev)) {
2531 DRM_INFO("No hardware hang detected. Did some blocks stall?\n");
2532 return 0;
2533 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002534
Marek Olšákd94aed52015-05-05 21:13:49 +02002535 atomic_inc(&adev->gpu_reset_counter);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002536
Chunming Zhoua3c47d62016-06-30 16:44:41 +08002537 /* block TTM */
2538 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
2539
Chunming Zhou0875dc92016-06-12 15:41:58 +08002540 /* block scheduler */
2541 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2542 struct amdgpu_ring *ring = adev->rings[i];
2543
2544 if (!ring)
2545 continue;
2546 kthread_park(ring->sched.thread);
Chunming Zhouaa1c8902016-06-30 13:56:02 +08002547 amd_sched_hw_job_reset(&ring->sched);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002548 }
Chunming Zhou2200eda2016-06-30 16:53:02 +08002549 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
2550 amdgpu_fence_driver_force_completion(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002551
Chunming Zhou35d782f2016-07-15 15:57:13 +08002552 need_full_reset = amdgpu_need_full_reset(adev);
2553
2554 if (!need_full_reset) {
2555 amdgpu_pre_soft_reset(adev);
2556 r = amdgpu_soft_reset(adev);
2557 amdgpu_post_soft_reset(adev);
2558 if (r || amdgpu_check_soft_reset(adev)) {
2559 DRM_INFO("soft reset failed, will fallback to full reset!\n");
2560 need_full_reset = true;
2561 }
2562 }
2563
2564 if (need_full_reset) {
Chunming Zhou35d782f2016-07-15 15:57:13 +08002565 r = amdgpu_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002566
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002567retry:
Chunming Zhou35d782f2016-07-15 15:57:13 +08002568 /* Disable fb access */
2569 if (adev->mode_info.num_crtc) {
2570 struct amdgpu_mode_mc_save save;
2571 amdgpu_display_stop_mc_access(adev, &save);
2572 amdgpu_wait_for_idle(adev, AMD_IP_BLOCK_TYPE_GMC);
2573 }
Alex Deuchere695e772016-10-19 14:40:58 -04002574 amdgpu_atombios_scratch_regs_save(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002575 r = amdgpu_asic_reset(adev);
Alex Deuchere695e772016-10-19 14:40:58 -04002576 amdgpu_atombios_scratch_regs_restore(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002577 /* post card */
2578 amdgpu_atom_asic_init(adev->mode_info.atom_context);
Alex Deucherbfa99262016-01-15 11:59:48 -05002579
Chunming Zhou35d782f2016-07-15 15:57:13 +08002580 if (!r) {
2581 dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
2582 r = amdgpu_resume(adev);
2583 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002584 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002585 if (!r) {
Chunming Zhoue72cfd52016-07-27 13:15:20 +08002586 amdgpu_irq_gpu_reset_resume_helper(adev);
Chunming Zhou2c0d7312016-08-30 16:36:25 +08002587 if (need_full_reset && amdgpu_need_backup(adev)) {
2588 r = amdgpu_ttm_recover_gart(adev);
2589 if (r)
2590 DRM_ERROR("gart recovery failed!!!\n");
2591 }
Chunming Zhou1f465082016-06-30 15:02:26 +08002592 r = amdgpu_ib_ring_tests(adev);
2593 if (r) {
2594 dev_err(adev->dev, "ib ring test failed (%d).\n", r);
Chunming Zhou40019dc2016-06-29 16:01:49 +08002595 r = amdgpu_suspend(adev);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002596 need_full_reset = true;
Chunming Zhou40019dc2016-06-29 16:01:49 +08002597 goto retry;
Chunming Zhou1f465082016-06-30 15:02:26 +08002598 }
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002599 /**
2600 * recovery vm page tables, since we cannot depend on VRAM is
2601 * consistent after gpu full reset.
2602 */
2603 if (need_full_reset && amdgpu_need_backup(adev)) {
2604 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
2605 struct amdgpu_bo *bo, *tmp;
Chris Wilsonf54d1862016-10-25 13:00:45 +01002606 struct dma_fence *fence = NULL, *next = NULL;
Chunming Zhou1f465082016-06-30 15:02:26 +08002607
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002608 DRM_INFO("recover vram bo from shadow\n");
2609 mutex_lock(&adev->shadow_list_lock);
2610 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
2611 amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
2612 if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01002613 r = dma_fence_wait(fence, false);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002614 if (r) {
Monk Liu1d7b17b2017-01-22 18:52:56 +08002615 WARN(r, "recovery from shadow isn't completed\n");
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002616 break;
2617 }
2618 }
2619
Chris Wilsonf54d1862016-10-25 13:00:45 +01002620 dma_fence_put(fence);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002621 fence = next;
2622 }
2623 mutex_unlock(&adev->shadow_list_lock);
2624 if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01002625 r = dma_fence_wait(fence, false);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002626 if (r)
Monk Liu1d7b17b2017-01-22 18:52:56 +08002627 WARN(r, "recovery from shadow isn't completed\n");
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002628 }
Chris Wilsonf54d1862016-10-25 13:00:45 +01002629 dma_fence_put(fence);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002630 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002631 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2632 struct amdgpu_ring *ring = adev->rings[i];
2633 if (!ring)
2634 continue;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002635
Chunming Zhouaa1c8902016-06-30 13:56:02 +08002636 amd_sched_job_recovery(&ring->sched);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002637 kthread_unpark(ring->sched.thread);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002638 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002639 } else {
Chunming Zhou2200eda2016-06-30 16:53:02 +08002640 dev_err(adev->dev, "asic resume failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002641 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
Chunming Zhou0875dc92016-06-12 15:41:58 +08002642 if (adev->rings[i]) {
2643 kthread_unpark(adev->rings[i]->sched.thread);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002644 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002645 }
2646 }
2647
2648 drm_helper_resume_force_mode(adev->ddev);
2649
2650 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
2651 if (r) {
2652 /* bad news, how to tell it to userspace ? */
2653 dev_info(adev->dev, "GPU reset failed\n");
2654 }
2655
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002656 return r;
2657}
2658
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002659void amdgpu_get_pcie_info(struct amdgpu_device *adev)
2660{
2661 u32 mask;
2662 int ret;
2663
Alex Deuchercd474ba2016-02-04 10:21:23 -05002664 if (amdgpu_pcie_gen_cap)
2665 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
2666
2667 if (amdgpu_pcie_lane_cap)
2668 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
2669
2670 /* covers APUs as well */
2671 if (pci_is_root_bus(adev->pdev->bus)) {
2672 if (adev->pm.pcie_gen_mask == 0)
2673 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
2674 if (adev->pm.pcie_mlw_mask == 0)
2675 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002676 return;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002677 }
Alex Deuchercd474ba2016-02-04 10:21:23 -05002678
2679 if (adev->pm.pcie_gen_mask == 0) {
2680 ret = drm_pcie_get_speed_cap_mask(adev->ddev, &mask);
2681 if (!ret) {
2682 adev->pm.pcie_gen_mask = (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
2683 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
2684 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
2685
2686 if (mask & DRM_PCIE_SPEED_25)
2687 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
2688 if (mask & DRM_PCIE_SPEED_50)
2689 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2;
2690 if (mask & DRM_PCIE_SPEED_80)
2691 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3;
2692 } else {
2693 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
2694 }
2695 }
2696 if (adev->pm.pcie_mlw_mask == 0) {
2697 ret = drm_pcie_get_max_link_width(adev->ddev, &mask);
2698 if (!ret) {
2699 switch (mask) {
2700 case 32:
2701 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
2702 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
2703 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2704 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2705 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2706 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2707 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2708 break;
2709 case 16:
2710 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
2711 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2712 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2713 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2714 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2715 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2716 break;
2717 case 12:
2718 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2719 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2720 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2721 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2722 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2723 break;
2724 case 8:
2725 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2726 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2727 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2728 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2729 break;
2730 case 4:
2731 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2732 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2733 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2734 break;
2735 case 2:
2736 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2737 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2738 break;
2739 case 1:
2740 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
2741 break;
2742 default:
2743 break;
2744 }
2745 } else {
2746 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002747 }
2748 }
2749}
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002750
2751/*
2752 * Debugfs
2753 */
2754int amdgpu_debugfs_add_files(struct amdgpu_device *adev,
Nils Wallménius06ab6832016-05-02 12:46:15 -04002755 const struct drm_info_list *files,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002756 unsigned nfiles)
2757{
2758 unsigned i;
2759
2760 for (i = 0; i < adev->debugfs_count; i++) {
2761 if (adev->debugfs[i].files == files) {
2762 /* Already registered */
2763 return 0;
2764 }
2765 }
2766
2767 i = adev->debugfs_count + 1;
2768 if (i > AMDGPU_DEBUGFS_MAX_COMPONENTS) {
2769 DRM_ERROR("Reached maximum number of debugfs components.\n");
2770 DRM_ERROR("Report so we increase "
2771 "AMDGPU_DEBUGFS_MAX_COMPONENTS.\n");
2772 return -EINVAL;
2773 }
2774 adev->debugfs[adev->debugfs_count].files = files;
2775 adev->debugfs[adev->debugfs_count].num_files = nfiles;
2776 adev->debugfs_count = i;
2777#if defined(CONFIG_DEBUG_FS)
2778 drm_debugfs_create_files(files, nfiles,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002779 adev->ddev->primary->debugfs_root,
2780 adev->ddev->primary);
2781#endif
2782 return 0;
2783}
2784
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002785#if defined(CONFIG_DEBUG_FS)
2786
2787static ssize_t amdgpu_debugfs_regs_read(struct file *f, char __user *buf,
2788 size_t size, loff_t *pos)
2789{
Al Viro45063092016-12-04 18:24:56 -05002790 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002791 ssize_t result = 0;
2792 int r;
Tom St Denisbd122672016-07-28 09:39:22 -04002793 bool pm_pg_lock, use_bank;
Tom St Denis566281592016-06-27 11:55:07 -04002794 unsigned instance_bank, sh_bank, se_bank;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002795
2796 if (size & 0x3 || *pos & 0x3)
2797 return -EINVAL;
2798
Tom St Denisbd122672016-07-28 09:39:22 -04002799 /* are we reading registers for which a PG lock is necessary? */
2800 pm_pg_lock = (*pos >> 23) & 1;
2801
Tom St Denis566281592016-06-27 11:55:07 -04002802 if (*pos & (1ULL << 62)) {
2803 se_bank = (*pos >> 24) & 0x3FF;
2804 sh_bank = (*pos >> 34) & 0x3FF;
2805 instance_bank = (*pos >> 44) & 0x3FF;
Tom St Denis32977f92016-10-09 07:41:26 -04002806
2807 if (se_bank == 0x3FF)
2808 se_bank = 0xFFFFFFFF;
2809 if (sh_bank == 0x3FF)
2810 sh_bank = 0xFFFFFFFF;
2811 if (instance_bank == 0x3FF)
2812 instance_bank = 0xFFFFFFFF;
Tom St Denis566281592016-06-27 11:55:07 -04002813 use_bank = 1;
Tom St Denis566281592016-06-27 11:55:07 -04002814 } else {
2815 use_bank = 0;
2816 }
2817
Tom St Denis801a6aa9a62017-03-15 05:34:25 -04002818 *pos &= (1UL << 22) - 1;
Tom St Denisbd122672016-07-28 09:39:22 -04002819
Tom St Denis566281592016-06-27 11:55:07 -04002820 if (use_bank) {
Tom St Denis32977f92016-10-09 07:41:26 -04002821 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
2822 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
Tom St Denis566281592016-06-27 11:55:07 -04002823 return -EINVAL;
2824 mutex_lock(&adev->grbm_idx_mutex);
2825 amdgpu_gfx_select_se_sh(adev, se_bank,
2826 sh_bank, instance_bank);
2827 }
2828
Tom St Denisbd122672016-07-28 09:39:22 -04002829 if (pm_pg_lock)
2830 mutex_lock(&adev->pm.mutex);
2831
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002832 while (size) {
2833 uint32_t value;
2834
2835 if (*pos > adev->rmmio_size)
Tom St Denis566281592016-06-27 11:55:07 -04002836 goto end;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002837
2838 value = RREG32(*pos >> 2);
2839 r = put_user(value, (uint32_t *)buf);
Tom St Denis566281592016-06-27 11:55:07 -04002840 if (r) {
2841 result = r;
2842 goto end;
2843 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002844
2845 result += 4;
2846 buf += 4;
2847 *pos += 4;
2848 size -= 4;
2849 }
2850
Tom St Denis566281592016-06-27 11:55:07 -04002851end:
2852 if (use_bank) {
2853 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
2854 mutex_unlock(&adev->grbm_idx_mutex);
2855 }
2856
Tom St Denisbd122672016-07-28 09:39:22 -04002857 if (pm_pg_lock)
2858 mutex_unlock(&adev->pm.mutex);
2859
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002860 return result;
2861}
2862
2863static ssize_t amdgpu_debugfs_regs_write(struct file *f, const char __user *buf,
2864 size_t size, loff_t *pos)
2865{
Al Viro45063092016-12-04 18:24:56 -05002866 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002867 ssize_t result = 0;
2868 int r;
Tom St Denis394fdde2016-10-10 07:31:23 -04002869 bool pm_pg_lock, use_bank;
2870 unsigned instance_bank, sh_bank, se_bank;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002871
2872 if (size & 0x3 || *pos & 0x3)
2873 return -EINVAL;
2874
Tom St Denis394fdde2016-10-10 07:31:23 -04002875 /* are we reading registers for which a PG lock is necessary? */
2876 pm_pg_lock = (*pos >> 23) & 1;
2877
2878 if (*pos & (1ULL << 62)) {
2879 se_bank = (*pos >> 24) & 0x3FF;
2880 sh_bank = (*pos >> 34) & 0x3FF;
2881 instance_bank = (*pos >> 44) & 0x3FF;
2882
2883 if (se_bank == 0x3FF)
2884 se_bank = 0xFFFFFFFF;
2885 if (sh_bank == 0x3FF)
2886 sh_bank = 0xFFFFFFFF;
2887 if (instance_bank == 0x3FF)
2888 instance_bank = 0xFFFFFFFF;
2889 use_bank = 1;
2890 } else {
2891 use_bank = 0;
2892 }
2893
Tom St Denis801a6aa9a62017-03-15 05:34:25 -04002894 *pos &= (1UL << 22) - 1;
Tom St Denis394fdde2016-10-10 07:31:23 -04002895
2896 if (use_bank) {
2897 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
2898 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
2899 return -EINVAL;
2900 mutex_lock(&adev->grbm_idx_mutex);
2901 amdgpu_gfx_select_se_sh(adev, se_bank,
2902 sh_bank, instance_bank);
2903 }
2904
2905 if (pm_pg_lock)
2906 mutex_lock(&adev->pm.mutex);
2907
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002908 while (size) {
2909 uint32_t value;
2910
2911 if (*pos > adev->rmmio_size)
2912 return result;
2913
2914 r = get_user(value, (uint32_t *)buf);
2915 if (r)
2916 return r;
2917
2918 WREG32(*pos >> 2, value);
2919
2920 result += 4;
2921 buf += 4;
2922 *pos += 4;
2923 size -= 4;
2924 }
2925
Tom St Denis394fdde2016-10-10 07:31:23 -04002926 if (use_bank) {
2927 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
2928 mutex_unlock(&adev->grbm_idx_mutex);
2929 }
2930
2931 if (pm_pg_lock)
2932 mutex_unlock(&adev->pm.mutex);
2933
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002934 return result;
2935}
2936
Tom St Denisadcec282016-04-15 13:08:44 -04002937static ssize_t amdgpu_debugfs_regs_pcie_read(struct file *f, char __user *buf,
2938 size_t size, loff_t *pos)
2939{
Al Viro45063092016-12-04 18:24:56 -05002940 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04002941 ssize_t result = 0;
2942 int r;
2943
2944 if (size & 0x3 || *pos & 0x3)
2945 return -EINVAL;
2946
2947 while (size) {
2948 uint32_t value;
2949
2950 value = RREG32_PCIE(*pos >> 2);
2951 r = put_user(value, (uint32_t *)buf);
2952 if (r)
2953 return r;
2954
2955 result += 4;
2956 buf += 4;
2957 *pos += 4;
2958 size -= 4;
2959 }
2960
2961 return result;
2962}
2963
2964static ssize_t amdgpu_debugfs_regs_pcie_write(struct file *f, const char __user *buf,
2965 size_t size, loff_t *pos)
2966{
Al Viro45063092016-12-04 18:24:56 -05002967 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04002968 ssize_t result = 0;
2969 int r;
2970
2971 if (size & 0x3 || *pos & 0x3)
2972 return -EINVAL;
2973
2974 while (size) {
2975 uint32_t value;
2976
2977 r = get_user(value, (uint32_t *)buf);
2978 if (r)
2979 return r;
2980
2981 WREG32_PCIE(*pos >> 2, value);
2982
2983 result += 4;
2984 buf += 4;
2985 *pos += 4;
2986 size -= 4;
2987 }
2988
2989 return result;
2990}
2991
2992static ssize_t amdgpu_debugfs_regs_didt_read(struct file *f, char __user *buf,
2993 size_t size, loff_t *pos)
2994{
Al Viro45063092016-12-04 18:24:56 -05002995 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04002996 ssize_t result = 0;
2997 int r;
2998
2999 if (size & 0x3 || *pos & 0x3)
3000 return -EINVAL;
3001
3002 while (size) {
3003 uint32_t value;
3004
3005 value = RREG32_DIDT(*pos >> 2);
3006 r = put_user(value, (uint32_t *)buf);
3007 if (r)
3008 return r;
3009
3010 result += 4;
3011 buf += 4;
3012 *pos += 4;
3013 size -= 4;
3014 }
3015
3016 return result;
3017}
3018
3019static ssize_t amdgpu_debugfs_regs_didt_write(struct file *f, const char __user *buf,
3020 size_t size, loff_t *pos)
3021{
Al Viro45063092016-12-04 18:24:56 -05003022 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003023 ssize_t result = 0;
3024 int r;
3025
3026 if (size & 0x3 || *pos & 0x3)
3027 return -EINVAL;
3028
3029 while (size) {
3030 uint32_t value;
3031
3032 r = get_user(value, (uint32_t *)buf);
3033 if (r)
3034 return r;
3035
3036 WREG32_DIDT(*pos >> 2, value);
3037
3038 result += 4;
3039 buf += 4;
3040 *pos += 4;
3041 size -= 4;
3042 }
3043
3044 return result;
3045}
3046
3047static ssize_t amdgpu_debugfs_regs_smc_read(struct file *f, char __user *buf,
3048 size_t size, loff_t *pos)
3049{
Al Viro45063092016-12-04 18:24:56 -05003050 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003051 ssize_t result = 0;
3052 int r;
3053
3054 if (size & 0x3 || *pos & 0x3)
3055 return -EINVAL;
3056
3057 while (size) {
3058 uint32_t value;
3059
Tom St Denis6fc0dea2016-08-29 08:39:29 -04003060 value = RREG32_SMC(*pos);
Tom St Denisadcec282016-04-15 13:08:44 -04003061 r = put_user(value, (uint32_t *)buf);
3062 if (r)
3063 return r;
3064
3065 result += 4;
3066 buf += 4;
3067 *pos += 4;
3068 size -= 4;
3069 }
3070
3071 return result;
3072}
3073
3074static ssize_t amdgpu_debugfs_regs_smc_write(struct file *f, const char __user *buf,
3075 size_t size, loff_t *pos)
3076{
Al Viro45063092016-12-04 18:24:56 -05003077 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003078 ssize_t result = 0;
3079 int r;
3080
3081 if (size & 0x3 || *pos & 0x3)
3082 return -EINVAL;
3083
3084 while (size) {
3085 uint32_t value;
3086
3087 r = get_user(value, (uint32_t *)buf);
3088 if (r)
3089 return r;
3090
Tom St Denis6fc0dea2016-08-29 08:39:29 -04003091 WREG32_SMC(*pos, value);
Tom St Denisadcec282016-04-15 13:08:44 -04003092
3093 result += 4;
3094 buf += 4;
3095 *pos += 4;
3096 size -= 4;
3097 }
3098
3099 return result;
3100}
3101
Tom St Denis1e051412016-06-27 09:57:18 -04003102static ssize_t amdgpu_debugfs_gca_config_read(struct file *f, char __user *buf,
3103 size_t size, loff_t *pos)
3104{
Al Viro45063092016-12-04 18:24:56 -05003105 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denis1e051412016-06-27 09:57:18 -04003106 ssize_t result = 0;
3107 int r;
3108 uint32_t *config, no_regs = 0;
3109
3110 if (size & 0x3 || *pos & 0x3)
3111 return -EINVAL;
3112
Markus Elfringecab7662016-09-18 17:00:52 +02003113 config = kmalloc_array(256, sizeof(*config), GFP_KERNEL);
Tom St Denis1e051412016-06-27 09:57:18 -04003114 if (!config)
3115 return -ENOMEM;
3116
3117 /* version, increment each time something is added */
Tom St Denis9a999352017-01-18 13:01:25 -05003118 config[no_regs++] = 3;
Tom St Denis1e051412016-06-27 09:57:18 -04003119 config[no_regs++] = adev->gfx.config.max_shader_engines;
3120 config[no_regs++] = adev->gfx.config.max_tile_pipes;
3121 config[no_regs++] = adev->gfx.config.max_cu_per_sh;
3122 config[no_regs++] = adev->gfx.config.max_sh_per_se;
3123 config[no_regs++] = adev->gfx.config.max_backends_per_se;
3124 config[no_regs++] = adev->gfx.config.max_texture_channel_caches;
3125 config[no_regs++] = adev->gfx.config.max_gprs;
3126 config[no_regs++] = adev->gfx.config.max_gs_threads;
3127 config[no_regs++] = adev->gfx.config.max_hw_contexts;
3128 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_frontend;
3129 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_backend;
3130 config[no_regs++] = adev->gfx.config.sc_hiz_tile_fifo_size;
3131 config[no_regs++] = adev->gfx.config.sc_earlyz_tile_fifo_size;
3132 config[no_regs++] = adev->gfx.config.num_tile_pipes;
3133 config[no_regs++] = adev->gfx.config.backend_enable_mask;
3134 config[no_regs++] = adev->gfx.config.mem_max_burst_length_bytes;
3135 config[no_regs++] = adev->gfx.config.mem_row_size_in_kb;
3136 config[no_regs++] = adev->gfx.config.shader_engine_tile_size;
3137 config[no_regs++] = adev->gfx.config.num_gpus;
3138 config[no_regs++] = adev->gfx.config.multi_gpu_tile_size;
3139 config[no_regs++] = adev->gfx.config.mc_arb_ramcfg;
3140 config[no_regs++] = adev->gfx.config.gb_addr_config;
3141 config[no_regs++] = adev->gfx.config.num_rbs;
3142
Tom St Denis89a8f302016-08-12 15:14:31 -04003143 /* rev==1 */
3144 config[no_regs++] = adev->rev_id;
3145 config[no_regs++] = adev->pg_flags;
3146 config[no_regs++] = adev->cg_flags;
3147
Tom St Denise9f11dc2016-08-17 12:00:51 -04003148 /* rev==2 */
3149 config[no_regs++] = adev->family;
3150 config[no_regs++] = adev->external_rev_id;
3151
Tom St Denis9a999352017-01-18 13:01:25 -05003152 /* rev==3 */
3153 config[no_regs++] = adev->pdev->device;
3154 config[no_regs++] = adev->pdev->revision;
3155 config[no_regs++] = adev->pdev->subsystem_device;
3156 config[no_regs++] = adev->pdev->subsystem_vendor;
3157
Tom St Denis1e051412016-06-27 09:57:18 -04003158 while (size && (*pos < no_regs * 4)) {
3159 uint32_t value;
3160
3161 value = config[*pos >> 2];
3162 r = put_user(value, (uint32_t *)buf);
3163 if (r) {
3164 kfree(config);
3165 return r;
3166 }
3167
3168 result += 4;
3169 buf += 4;
3170 *pos += 4;
3171 size -= 4;
3172 }
3173
3174 kfree(config);
3175 return result;
3176}
3177
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003178static ssize_t amdgpu_debugfs_sensor_read(struct file *f, char __user *buf,
3179 size_t size, loff_t *pos)
3180{
Al Viro45063092016-12-04 18:24:56 -05003181 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003182 int idx, x, outsize, r, valuesize;
3183 uint32_t values[16];
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003184
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003185 if (size & 3 || *pos & 0x3)
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003186 return -EINVAL;
3187
Samuel Pitoiset3cbc6142017-02-15 19:32:29 +01003188 if (amdgpu_dpm == 0)
3189 return -EINVAL;
3190
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003191 /* convert offset to sensor number */
3192 idx = *pos >> 2;
3193
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003194 valuesize = sizeof(values);
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003195 if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->read_sensor)
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003196 r = adev->powerplay.pp_funcs->read_sensor(adev->powerplay.pp_handle, idx, &values[0], &valuesize);
Samuel Pitoiset3cbc6142017-02-15 19:32:29 +01003197 else if (adev->pm.funcs && adev->pm.funcs->read_sensor)
3198 r = adev->pm.funcs->read_sensor(adev, idx, &values[0],
3199 &valuesize);
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003200 else
3201 return -EINVAL;
3202
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003203 if (size > valuesize)
3204 return -EINVAL;
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003205
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003206 outsize = 0;
3207 x = 0;
3208 if (!r) {
3209 while (size) {
3210 r = put_user(values[x++], (int32_t *)buf);
3211 buf += 4;
3212 size -= 4;
3213 outsize += 4;
3214 }
3215 }
3216
3217 return !r ? outsize : r;
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003218}
Tom St Denis1e051412016-06-27 09:57:18 -04003219
Tom St Denis273d7aa2016-10-11 14:48:55 -04003220static ssize_t amdgpu_debugfs_wave_read(struct file *f, char __user *buf,
3221 size_t size, loff_t *pos)
3222{
3223 struct amdgpu_device *adev = f->f_inode->i_private;
3224 int r, x;
3225 ssize_t result=0;
Tom St Denis472259f2016-10-14 09:49:09 -04003226 uint32_t offset, se, sh, cu, wave, simd, data[32];
Tom St Denis273d7aa2016-10-11 14:48:55 -04003227
3228 if (size & 3 || *pos & 3)
3229 return -EINVAL;
3230
3231 /* decode offset */
3232 offset = (*pos & 0x7F);
3233 se = ((*pos >> 7) & 0xFF);
3234 sh = ((*pos >> 15) & 0xFF);
3235 cu = ((*pos >> 23) & 0xFF);
3236 wave = ((*pos >> 31) & 0xFF);
3237 simd = ((*pos >> 37) & 0xFF);
Tom St Denis273d7aa2016-10-11 14:48:55 -04003238
3239 /* switch to the specific se/sh/cu */
3240 mutex_lock(&adev->grbm_idx_mutex);
3241 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
3242
3243 x = 0;
Tom St Denis472259f2016-10-14 09:49:09 -04003244 if (adev->gfx.funcs->read_wave_data)
3245 adev->gfx.funcs->read_wave_data(adev, simd, wave, data, &x);
Tom St Denis273d7aa2016-10-11 14:48:55 -04003246
3247 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
3248 mutex_unlock(&adev->grbm_idx_mutex);
3249
Tom St Denis5ecfb3b2016-10-13 12:15:03 -04003250 if (!x)
3251 return -EINVAL;
3252
Tom St Denis472259f2016-10-14 09:49:09 -04003253 while (size && (offset < x * 4)) {
Tom St Denis273d7aa2016-10-11 14:48:55 -04003254 uint32_t value;
3255
Tom St Denis472259f2016-10-14 09:49:09 -04003256 value = data[offset >> 2];
Tom St Denis273d7aa2016-10-11 14:48:55 -04003257 r = put_user(value, (uint32_t *)buf);
3258 if (r)
3259 return r;
3260
3261 result += 4;
3262 buf += 4;
Tom St Denis472259f2016-10-14 09:49:09 -04003263 offset += 4;
Tom St Denis273d7aa2016-10-11 14:48:55 -04003264 size -= 4;
3265 }
3266
3267 return result;
3268}
3269
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003270static ssize_t amdgpu_debugfs_gpr_read(struct file *f, char __user *buf,
3271 size_t size, loff_t *pos)
3272{
3273 struct amdgpu_device *adev = f->f_inode->i_private;
3274 int r;
3275 ssize_t result = 0;
3276 uint32_t offset, se, sh, cu, wave, simd, thread, bank, *data;
3277
3278 if (size & 3 || *pos & 3)
3279 return -EINVAL;
3280
3281 /* decode offset */
3282 offset = (*pos & 0xFFF); /* in dwords */
3283 se = ((*pos >> 12) & 0xFF);
3284 sh = ((*pos >> 20) & 0xFF);
3285 cu = ((*pos >> 28) & 0xFF);
3286 wave = ((*pos >> 36) & 0xFF);
3287 simd = ((*pos >> 44) & 0xFF);
3288 thread = ((*pos >> 52) & 0xFF);
3289 bank = ((*pos >> 60) & 1);
3290
3291 data = kmalloc_array(1024, sizeof(*data), GFP_KERNEL);
3292 if (!data)
3293 return -ENOMEM;
3294
3295 /* switch to the specific se/sh/cu */
3296 mutex_lock(&adev->grbm_idx_mutex);
3297 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
3298
3299 if (bank == 0) {
3300 if (adev->gfx.funcs->read_wave_vgprs)
3301 adev->gfx.funcs->read_wave_vgprs(adev, simd, wave, thread, offset, size>>2, data);
3302 } else {
3303 if (adev->gfx.funcs->read_wave_sgprs)
3304 adev->gfx.funcs->read_wave_sgprs(adev, simd, wave, offset, size>>2, data);
3305 }
3306
3307 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
3308 mutex_unlock(&adev->grbm_idx_mutex);
3309
3310 while (size) {
3311 uint32_t value;
3312
3313 value = data[offset++];
3314 r = put_user(value, (uint32_t *)buf);
3315 if (r) {
3316 result = r;
3317 goto err;
3318 }
3319
3320 result += 4;
3321 buf += 4;
3322 size -= 4;
3323 }
3324
3325err:
3326 kfree(data);
3327 return result;
3328}
3329
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003330static const struct file_operations amdgpu_debugfs_regs_fops = {
3331 .owner = THIS_MODULE,
3332 .read = amdgpu_debugfs_regs_read,
3333 .write = amdgpu_debugfs_regs_write,
3334 .llseek = default_llseek
3335};
Tom St Denisadcec282016-04-15 13:08:44 -04003336static const struct file_operations amdgpu_debugfs_regs_didt_fops = {
3337 .owner = THIS_MODULE,
3338 .read = amdgpu_debugfs_regs_didt_read,
3339 .write = amdgpu_debugfs_regs_didt_write,
3340 .llseek = default_llseek
3341};
3342static const struct file_operations amdgpu_debugfs_regs_pcie_fops = {
3343 .owner = THIS_MODULE,
3344 .read = amdgpu_debugfs_regs_pcie_read,
3345 .write = amdgpu_debugfs_regs_pcie_write,
3346 .llseek = default_llseek
3347};
3348static const struct file_operations amdgpu_debugfs_regs_smc_fops = {
3349 .owner = THIS_MODULE,
3350 .read = amdgpu_debugfs_regs_smc_read,
3351 .write = amdgpu_debugfs_regs_smc_write,
3352 .llseek = default_llseek
3353};
3354
Tom St Denis1e051412016-06-27 09:57:18 -04003355static const struct file_operations amdgpu_debugfs_gca_config_fops = {
3356 .owner = THIS_MODULE,
3357 .read = amdgpu_debugfs_gca_config_read,
3358 .llseek = default_llseek
3359};
3360
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003361static const struct file_operations amdgpu_debugfs_sensors_fops = {
3362 .owner = THIS_MODULE,
3363 .read = amdgpu_debugfs_sensor_read,
3364 .llseek = default_llseek
3365};
3366
Tom St Denis273d7aa2016-10-11 14:48:55 -04003367static const struct file_operations amdgpu_debugfs_wave_fops = {
3368 .owner = THIS_MODULE,
3369 .read = amdgpu_debugfs_wave_read,
3370 .llseek = default_llseek
3371};
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003372static const struct file_operations amdgpu_debugfs_gpr_fops = {
3373 .owner = THIS_MODULE,
3374 .read = amdgpu_debugfs_gpr_read,
3375 .llseek = default_llseek
3376};
Tom St Denis273d7aa2016-10-11 14:48:55 -04003377
Tom St Denisadcec282016-04-15 13:08:44 -04003378static const struct file_operations *debugfs_regs[] = {
3379 &amdgpu_debugfs_regs_fops,
3380 &amdgpu_debugfs_regs_didt_fops,
3381 &amdgpu_debugfs_regs_pcie_fops,
3382 &amdgpu_debugfs_regs_smc_fops,
Tom St Denis1e051412016-06-27 09:57:18 -04003383 &amdgpu_debugfs_gca_config_fops,
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003384 &amdgpu_debugfs_sensors_fops,
Tom St Denis273d7aa2016-10-11 14:48:55 -04003385 &amdgpu_debugfs_wave_fops,
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003386 &amdgpu_debugfs_gpr_fops,
Tom St Denisadcec282016-04-15 13:08:44 -04003387};
3388
3389static const char *debugfs_regs_names[] = {
3390 "amdgpu_regs",
3391 "amdgpu_regs_didt",
3392 "amdgpu_regs_pcie",
3393 "amdgpu_regs_smc",
Tom St Denis1e051412016-06-27 09:57:18 -04003394 "amdgpu_gca_config",
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003395 "amdgpu_sensors",
Tom St Denis273d7aa2016-10-11 14:48:55 -04003396 "amdgpu_wave",
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003397 "amdgpu_gpr",
Tom St Denisadcec282016-04-15 13:08:44 -04003398};
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003399
3400static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
3401{
3402 struct drm_minor *minor = adev->ddev->primary;
3403 struct dentry *ent, *root = minor->debugfs_root;
Tom St Denisadcec282016-04-15 13:08:44 -04003404 unsigned i, j;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003405
Tom St Denisadcec282016-04-15 13:08:44 -04003406 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3407 ent = debugfs_create_file(debugfs_regs_names[i],
3408 S_IFREG | S_IRUGO, root,
3409 adev, debugfs_regs[i]);
3410 if (IS_ERR(ent)) {
3411 for (j = 0; j < i; j++) {
3412 debugfs_remove(adev->debugfs_regs[i]);
3413 adev->debugfs_regs[i] = NULL;
3414 }
3415 return PTR_ERR(ent);
3416 }
3417
3418 if (!i)
3419 i_size_write(ent->d_inode, adev->rmmio_size);
3420 adev->debugfs_regs[i] = ent;
3421 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003422
3423 return 0;
3424}
3425
3426static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev)
3427{
Tom St Denisadcec282016-04-15 13:08:44 -04003428 unsigned i;
3429
3430 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3431 if (adev->debugfs_regs[i]) {
3432 debugfs_remove(adev->debugfs_regs[i]);
3433 adev->debugfs_regs[i] = NULL;
3434 }
3435 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003436}
3437
3438int amdgpu_debugfs_init(struct drm_minor *minor)
3439{
3440 return 0;
3441}
Alexander Kuleshov7cebc722015-06-27 13:16:05 +06003442#else
3443static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
3444{
3445 return 0;
3446}
3447static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev) { }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003448#endif