blob: fc790e5c46fd01721262deeb08201c3316898b89 [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",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040076 "LAST",
77};
78
79bool amdgpu_device_is_px(struct drm_device *dev)
80{
81 struct amdgpu_device *adev = dev->dev_private;
82
Jammy Zhou2f7d10b2015-07-22 11:29:01 +080083 if (adev->flags & AMD_IS_PX)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040084 return true;
85 return false;
86}
87
88/*
89 * MMIO register access helper functions.
90 */
91uint32_t amdgpu_mm_rreg(struct amdgpu_device *adev, uint32_t reg,
92 bool always_indirect)
93{
Tom St Denisf4b373f2016-05-31 08:02:27 -040094 uint32_t ret;
95
Alex Deucherd38ceaf2015-04-20 16:55:21 -040096 if ((reg * 4) < adev->rmmio_size && !always_indirect)
Tom St Denisf4b373f2016-05-31 08:02:27 -040097 ret = readl(((void __iomem *)adev->rmmio) + (reg * 4));
Alex Deucherd38ceaf2015-04-20 16:55:21 -040098 else {
99 unsigned long flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400100
101 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
102 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
103 ret = readl(((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
104 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400105 }
Tom St Denisf4b373f2016-05-31 08:02:27 -0400106 trace_amdgpu_mm_rreg(adev->pdev->device, reg, ret);
107 return ret;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400108}
109
110void amdgpu_mm_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v,
111 bool always_indirect)
112{
Tom St Denisf4b373f2016-05-31 08:02:27 -0400113 trace_amdgpu_mm_wreg(adev->pdev->device, reg, v);
Monk Liu4e99a442016-03-31 13:26:59 +0800114
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400115 if ((reg * 4) < adev->rmmio_size && !always_indirect)
116 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
117 else {
118 unsigned long flags;
119
120 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
121 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
122 writel(v, ((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
123 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
124 }
125}
126
127u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 reg)
128{
129 if ((reg * 4) < adev->rio_mem_size)
130 return ioread32(adev->rio_mem + (reg * 4));
131 else {
132 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
133 return ioread32(adev->rio_mem + (mmMM_DATA * 4));
134 }
135}
136
137void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
138{
139
140 if ((reg * 4) < adev->rio_mem_size)
141 iowrite32(v, adev->rio_mem + (reg * 4));
142 else {
143 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
144 iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
145 }
146}
147
148/**
149 * amdgpu_mm_rdoorbell - read a doorbell dword
150 *
151 * @adev: amdgpu_device pointer
152 * @index: doorbell index
153 *
154 * Returns the value in the doorbell aperture at the
155 * requested doorbell index (CIK).
156 */
157u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
158{
159 if (index < adev->doorbell.num_doorbells) {
160 return readl(adev->doorbell.ptr + index);
161 } else {
162 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
163 return 0;
164 }
165}
166
167/**
168 * amdgpu_mm_wdoorbell - write a doorbell dword
169 *
170 * @adev: amdgpu_device pointer
171 * @index: doorbell index
172 * @v: value to write
173 *
174 * Writes @v to the doorbell aperture at the
175 * requested doorbell index (CIK).
176 */
177void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
178{
179 if (index < adev->doorbell.num_doorbells) {
180 writel(v, adev->doorbell.ptr + index);
181 } else {
182 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
183 }
184}
185
186/**
187 * amdgpu_invalid_rreg - dummy reg read function
188 *
189 * @adev: amdgpu device pointer
190 * @reg: offset of register
191 *
192 * Dummy register read function. Used for register blocks
193 * that certain asics don't have (all asics).
194 * Returns the value in the register.
195 */
196static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
197{
198 DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
199 BUG();
200 return 0;
201}
202
203/**
204 * amdgpu_invalid_wreg - dummy reg write function
205 *
206 * @adev: amdgpu device pointer
207 * @reg: offset of register
208 * @v: value to write to the register
209 *
210 * Dummy register read function. Used for register blocks
211 * that certain asics don't have (all asics).
212 */
213static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
214{
215 DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
216 reg, v);
217 BUG();
218}
219
220/**
221 * amdgpu_block_invalid_rreg - dummy reg read function
222 *
223 * @adev: amdgpu device pointer
224 * @block: offset of instance
225 * @reg: offset of register
226 *
227 * Dummy register read function. Used for register blocks
228 * that certain asics don't have (all asics).
229 * Returns the value in the register.
230 */
231static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
232 uint32_t block, uint32_t reg)
233{
234 DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
235 reg, block);
236 BUG();
237 return 0;
238}
239
240/**
241 * amdgpu_block_invalid_wreg - dummy reg write function
242 *
243 * @adev: amdgpu device pointer
244 * @block: offset of instance
245 * @reg: offset of register
246 * @v: value to write to the register
247 *
248 * Dummy register read function. Used for register blocks
249 * that certain asics don't have (all asics).
250 */
251static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
252 uint32_t block,
253 uint32_t reg, uint32_t v)
254{
255 DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
256 reg, block, v);
257 BUG();
258}
259
260static int amdgpu_vram_scratch_init(struct amdgpu_device *adev)
261{
262 int r;
263
264 if (adev->vram_scratch.robj == NULL) {
265 r = amdgpu_bo_create(adev, AMDGPU_GPU_PAGE_SIZE,
Alex Deucher857d9132015-08-27 00:14:16 -0400266 PAGE_SIZE, true, AMDGPU_GEM_DOMAIN_VRAM,
Christian König03f48dd2016-08-15 17:00:22 +0200267 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
268 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS,
Christian König72d76682015-09-03 17:34:59 +0200269 NULL, NULL, &adev->vram_scratch.robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400270 if (r) {
271 return r;
272 }
273 }
274
275 r = amdgpu_bo_reserve(adev->vram_scratch.robj, false);
276 if (unlikely(r != 0))
277 return r;
278 r = amdgpu_bo_pin(adev->vram_scratch.robj,
279 AMDGPU_GEM_DOMAIN_VRAM, &adev->vram_scratch.gpu_addr);
280 if (r) {
281 amdgpu_bo_unreserve(adev->vram_scratch.robj);
282 return r;
283 }
284 r = amdgpu_bo_kmap(adev->vram_scratch.robj,
285 (void **)&adev->vram_scratch.ptr);
286 if (r)
287 amdgpu_bo_unpin(adev->vram_scratch.robj);
288 amdgpu_bo_unreserve(adev->vram_scratch.robj);
289
290 return r;
291}
292
293static void amdgpu_vram_scratch_fini(struct amdgpu_device *adev)
294{
295 int r;
296
297 if (adev->vram_scratch.robj == NULL) {
298 return;
299 }
300 r = amdgpu_bo_reserve(adev->vram_scratch.robj, false);
301 if (likely(r == 0)) {
302 amdgpu_bo_kunmap(adev->vram_scratch.robj);
303 amdgpu_bo_unpin(adev->vram_scratch.robj);
304 amdgpu_bo_unreserve(adev->vram_scratch.robj);
305 }
306 amdgpu_bo_unref(&adev->vram_scratch.robj);
307}
308
309/**
310 * amdgpu_program_register_sequence - program an array of registers.
311 *
312 * @adev: amdgpu_device pointer
313 * @registers: pointer to the register array
314 * @array_size: size of the register array
315 *
316 * Programs an array or registers with and and or masks.
317 * This is a helper for setting golden registers.
318 */
319void amdgpu_program_register_sequence(struct amdgpu_device *adev,
320 const u32 *registers,
321 const u32 array_size)
322{
323 u32 tmp, reg, and_mask, or_mask;
324 int i;
325
326 if (array_size % 3)
327 return;
328
329 for (i = 0; i < array_size; i +=3) {
330 reg = registers[i + 0];
331 and_mask = registers[i + 1];
332 or_mask = registers[i + 2];
333
334 if (and_mask == 0xffffffff) {
335 tmp = or_mask;
336 } else {
337 tmp = RREG32(reg);
338 tmp &= ~and_mask;
339 tmp |= or_mask;
340 }
341 WREG32(reg, tmp);
342 }
343}
344
345void amdgpu_pci_config_reset(struct amdgpu_device *adev)
346{
347 pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
348}
349
350/*
351 * GPU doorbell aperture helpers function.
352 */
353/**
354 * amdgpu_doorbell_init - Init doorbell driver information.
355 *
356 * @adev: amdgpu_device pointer
357 *
358 * Init doorbell driver information (CIK)
359 * Returns 0 on success, error on failure.
360 */
361static int amdgpu_doorbell_init(struct amdgpu_device *adev)
362{
363 /* doorbell bar mapping */
364 adev->doorbell.base = pci_resource_start(adev->pdev, 2);
365 adev->doorbell.size = pci_resource_len(adev->pdev, 2);
366
Christian Königedf600d2016-05-03 15:54:54 +0200367 adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400368 AMDGPU_DOORBELL_MAX_ASSIGNMENT+1);
369 if (adev->doorbell.num_doorbells == 0)
370 return -EINVAL;
371
372 adev->doorbell.ptr = ioremap(adev->doorbell.base, adev->doorbell.num_doorbells * sizeof(u32));
373 if (adev->doorbell.ptr == NULL) {
374 return -ENOMEM;
375 }
376 DRM_INFO("doorbell mmio base: 0x%08X\n", (uint32_t)adev->doorbell.base);
377 DRM_INFO("doorbell mmio size: %u\n", (unsigned)adev->doorbell.size);
378
379 return 0;
380}
381
382/**
383 * amdgpu_doorbell_fini - Tear down doorbell driver information.
384 *
385 * @adev: amdgpu_device pointer
386 *
387 * Tear down doorbell driver information (CIK)
388 */
389static void amdgpu_doorbell_fini(struct amdgpu_device *adev)
390{
391 iounmap(adev->doorbell.ptr);
392 adev->doorbell.ptr = NULL;
393}
394
395/**
396 * amdgpu_doorbell_get_kfd_info - Report doorbell configuration required to
397 * setup amdkfd
398 *
399 * @adev: amdgpu_device pointer
400 * @aperture_base: output returning doorbell aperture base physical address
401 * @aperture_size: output returning doorbell aperture size in bytes
402 * @start_offset: output returning # of doorbell bytes reserved for amdgpu.
403 *
404 * amdgpu and amdkfd share the doorbell aperture. amdgpu sets it up,
405 * takes doorbells required for its own rings and reports the setup to amdkfd.
406 * amdgpu reserved doorbells are at the start of the doorbell aperture.
407 */
408void amdgpu_doorbell_get_kfd_info(struct amdgpu_device *adev,
409 phys_addr_t *aperture_base,
410 size_t *aperture_size,
411 size_t *start_offset)
412{
413 /*
414 * The first num_doorbells are used by amdgpu.
415 * amdkfd takes whatever's left in the aperture.
416 */
417 if (adev->doorbell.size > adev->doorbell.num_doorbells * sizeof(u32)) {
418 *aperture_base = adev->doorbell.base;
419 *aperture_size = adev->doorbell.size;
420 *start_offset = adev->doorbell.num_doorbells * sizeof(u32);
421 } else {
422 *aperture_base = 0;
423 *aperture_size = 0;
424 *start_offset = 0;
425 }
426}
427
428/*
429 * amdgpu_wb_*()
430 * Writeback is the the method by which the the GPU updates special pages
431 * in memory with the status of certain GPU events (fences, ring pointers,
432 * etc.).
433 */
434
435/**
436 * amdgpu_wb_fini - Disable Writeback and free memory
437 *
438 * @adev: amdgpu_device pointer
439 *
440 * Disables Writeback and frees the Writeback memory (all asics).
441 * Used at driver shutdown.
442 */
443static void amdgpu_wb_fini(struct amdgpu_device *adev)
444{
445 if (adev->wb.wb_obj) {
Alex Deuchera76ed482016-10-21 15:30:36 -0400446 amdgpu_bo_free_kernel(&adev->wb.wb_obj,
447 &adev->wb.gpu_addr,
448 (void **)&adev->wb.wb);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400449 adev->wb.wb_obj = NULL;
450 }
451}
452
453/**
454 * amdgpu_wb_init- Init Writeback driver info and allocate memory
455 *
456 * @adev: amdgpu_device pointer
457 *
458 * Disables Writeback and frees the Writeback memory (all asics).
459 * Used at driver startup.
460 * Returns 0 on success or an -error on failure.
461 */
462static int amdgpu_wb_init(struct amdgpu_device *adev)
463{
464 int r;
465
466 if (adev->wb.wb_obj == NULL) {
Alex Deuchera76ed482016-10-21 15:30:36 -0400467 r = amdgpu_bo_create_kernel(adev, AMDGPU_MAX_WB * 4,
468 PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
469 &adev->wb.wb_obj, &adev->wb.gpu_addr,
470 (void **)&adev->wb.wb);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400471 if (r) {
472 dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
473 return r;
474 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400475
476 adev->wb.num_wb = AMDGPU_MAX_WB;
477 memset(&adev->wb.used, 0, sizeof(adev->wb.used));
478
479 /* clear wb memory */
480 memset((char *)adev->wb.wb, 0, AMDGPU_GPU_PAGE_SIZE);
481 }
482
483 return 0;
484}
485
486/**
487 * amdgpu_wb_get - Allocate a wb entry
488 *
489 * @adev: amdgpu_device pointer
490 * @wb: wb index
491 *
492 * Allocate a wb slot for use by the driver (all asics).
493 * Returns 0 on success or -EINVAL on failure.
494 */
495int amdgpu_wb_get(struct amdgpu_device *adev, u32 *wb)
496{
497 unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
498 if (offset < adev->wb.num_wb) {
499 __set_bit(offset, adev->wb.used);
500 *wb = offset;
501 return 0;
502 } else {
503 return -EINVAL;
504 }
505}
506
507/**
508 * amdgpu_wb_free - Free a wb entry
509 *
510 * @adev: amdgpu_device pointer
511 * @wb: wb index
512 *
513 * Free a wb slot allocated for use by the driver (all asics)
514 */
515void amdgpu_wb_free(struct amdgpu_device *adev, u32 wb)
516{
517 if (wb < adev->wb.num_wb)
518 __clear_bit(wb, adev->wb.used);
519}
520
521/**
522 * amdgpu_vram_location - try to find VRAM location
523 * @adev: amdgpu device structure holding all necessary informations
524 * @mc: memory controller structure holding memory informations
525 * @base: base address at which to put VRAM
526 *
527 * Function will place try to place VRAM at base address provided
528 * as parameter (which is so far either PCI aperture address or
529 * for IGP TOM base address).
530 *
531 * If there is not enough space to fit the unvisible VRAM in the 32bits
532 * address space then we limit the VRAM size to the aperture.
533 *
534 * Note: We don't explicitly enforce VRAM start to be aligned on VRAM size,
535 * this shouldn't be a problem as we are using the PCI aperture as a reference.
536 * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but
537 * not IGP.
538 *
539 * Note: we use mc_vram_size as on some board we need to program the mc to
540 * cover the whole aperture even if VRAM size is inferior to aperture size
541 * Novell bug 204882 + along with lots of ubuntu ones
542 *
543 * Note: when limiting vram it's safe to overwritte real_vram_size because
544 * we are not in case where real_vram_size is inferior to mc_vram_size (ie
545 * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu
546 * ones)
547 *
548 * Note: IGP TOM addr should be the same as the aperture addr, we don't
549 * explicitly check for that thought.
550 *
551 * FIXME: when reducing VRAM size align new size on power of 2.
552 */
553void amdgpu_vram_location(struct amdgpu_device *adev, struct amdgpu_mc *mc, u64 base)
554{
555 uint64_t limit = (uint64_t)amdgpu_vram_limit << 20;
556
557 mc->vram_start = base;
558 if (mc->mc_vram_size > (adev->mc.mc_mask - base + 1)) {
559 dev_warn(adev->dev, "limiting VRAM to PCI aperture size\n");
560 mc->real_vram_size = mc->aper_size;
561 mc->mc_vram_size = mc->aper_size;
562 }
563 mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
564 if (limit && limit < mc->real_vram_size)
565 mc->real_vram_size = limit;
566 dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
567 mc->mc_vram_size >> 20, mc->vram_start,
568 mc->vram_end, mc->real_vram_size >> 20);
569}
570
571/**
572 * amdgpu_gtt_location - try to find GTT location
573 * @adev: amdgpu device structure holding all necessary informations
574 * @mc: memory controller structure holding memory informations
575 *
576 * Function will place try to place GTT before or after VRAM.
577 *
578 * If GTT size is bigger than space left then we ajust GTT size.
579 * Thus function will never fails.
580 *
581 * FIXME: when reducing GTT size align new size on power of 2.
582 */
583void amdgpu_gtt_location(struct amdgpu_device *adev, struct amdgpu_mc *mc)
584{
585 u64 size_af, size_bf;
586
587 size_af = ((adev->mc.mc_mask - mc->vram_end) + mc->gtt_base_align) & ~mc->gtt_base_align;
588 size_bf = mc->vram_start & ~mc->gtt_base_align;
589 if (size_bf > size_af) {
590 if (mc->gtt_size > size_bf) {
591 dev_warn(adev->dev, "limiting GTT\n");
592 mc->gtt_size = size_bf;
593 }
594 mc->gtt_start = (mc->vram_start & ~mc->gtt_base_align) - mc->gtt_size;
595 } else {
596 if (mc->gtt_size > size_af) {
597 dev_warn(adev->dev, "limiting GTT\n");
598 mc->gtt_size = size_af;
599 }
600 mc->gtt_start = (mc->vram_end + 1 + mc->gtt_base_align) & ~mc->gtt_base_align;
601 }
602 mc->gtt_end = mc->gtt_start + mc->gtt_size - 1;
603 dev_info(adev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
604 mc->gtt_size >> 20, mc->gtt_start, mc->gtt_end);
605}
606
607/*
608 * GPU helpers function.
609 */
610/**
611 * amdgpu_card_posted - check if the hw has already been initialized
612 *
613 * @adev: amdgpu_device pointer
614 *
615 * Check if the asic has been initialized (all asics).
616 * Used at driver startup.
617 * Returns true if initialized or false if not.
618 */
619bool amdgpu_card_posted(struct amdgpu_device *adev)
620{
621 uint32_t reg;
622
623 /* then check MEM_SIZE, in case the crtcs are off */
624 reg = RREG32(mmCONFIG_MEMSIZE);
625
626 if (reg)
627 return true;
628
629 return false;
630
631}
632
Monk Liubec86372016-09-14 19:38:08 +0800633static bool amdgpu_vpost_needed(struct amdgpu_device *adev)
634{
635 if (amdgpu_sriov_vf(adev))
636 return false;
637
638 if (amdgpu_passthrough(adev)) {
Monk Liu1da2c322016-11-11 11:24:29 +0800639 /* for FIJI: In whole GPU pass-through virtualization case, after VM reboot
640 * some old smc fw still need driver do vPost otherwise gpu hang, while
641 * those smc fw version above 22.15 doesn't have this flaw, so we force
642 * vpost executed for smc version below 22.15
Monk Liubec86372016-09-14 19:38:08 +0800643 */
644 if (adev->asic_type == CHIP_FIJI) {
645 int err;
646 uint32_t fw_ver;
647 err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
648 /* force vPost if error occured */
649 if (err)
650 return true;
651
652 fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
Monk Liu1da2c322016-11-11 11:24:29 +0800653 if (fw_ver < 0x00160e00)
654 return true;
Monk Liubec86372016-09-14 19:38:08 +0800655 }
Monk Liubec86372016-09-14 19:38:08 +0800656 }
Monk Liu1da2c322016-11-11 11:24:29 +0800657 return !amdgpu_card_posted(adev);
Monk Liubec86372016-09-14 19:38:08 +0800658}
659
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400660/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400661 * amdgpu_dummy_page_init - init dummy page used by the driver
662 *
663 * @adev: amdgpu_device pointer
664 *
665 * Allocate the dummy page used by the driver (all asics).
666 * This dummy page is used by the driver as a filler for gart entries
667 * when pages are taken out of the GART
668 * Returns 0 on sucess, -ENOMEM on failure.
669 */
670int amdgpu_dummy_page_init(struct amdgpu_device *adev)
671{
672 if (adev->dummy_page.page)
673 return 0;
674 adev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
675 if (adev->dummy_page.page == NULL)
676 return -ENOMEM;
677 adev->dummy_page.addr = pci_map_page(adev->pdev, adev->dummy_page.page,
678 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
679 if (pci_dma_mapping_error(adev->pdev, adev->dummy_page.addr)) {
680 dev_err(&adev->pdev->dev, "Failed to DMA MAP the dummy page\n");
681 __free_page(adev->dummy_page.page);
682 adev->dummy_page.page = NULL;
683 return -ENOMEM;
684 }
685 return 0;
686}
687
688/**
689 * amdgpu_dummy_page_fini - free dummy page used by the driver
690 *
691 * @adev: amdgpu_device pointer
692 *
693 * Frees the dummy page used by the driver (all asics).
694 */
695void amdgpu_dummy_page_fini(struct amdgpu_device *adev)
696{
697 if (adev->dummy_page.page == NULL)
698 return;
699 pci_unmap_page(adev->pdev, adev->dummy_page.addr,
700 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
701 __free_page(adev->dummy_page.page);
702 adev->dummy_page.page = NULL;
703}
704
705
706/* ATOM accessor methods */
707/*
708 * ATOM is an interpreted byte code stored in tables in the vbios. The
709 * driver registers callbacks to access registers and the interpreter
710 * in the driver parses the tables and executes then to program specific
711 * actions (set display modes, asic init, etc.). See amdgpu_atombios.c,
712 * atombios.h, and atom.c
713 */
714
715/**
716 * cail_pll_read - read PLL register
717 *
718 * @info: atom card_info pointer
719 * @reg: PLL register offset
720 *
721 * Provides a PLL register accessor for the atom interpreter (r4xx+).
722 * Returns the value of the PLL register.
723 */
724static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
725{
726 return 0;
727}
728
729/**
730 * cail_pll_write - write PLL register
731 *
732 * @info: atom card_info pointer
733 * @reg: PLL register offset
734 * @val: value to write to the pll register
735 *
736 * Provides a PLL register accessor for the atom interpreter (r4xx+).
737 */
738static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
739{
740
741}
742
743/**
744 * cail_mc_read - read MC (Memory Controller) register
745 *
746 * @info: atom card_info pointer
747 * @reg: MC register offset
748 *
749 * Provides an MC register accessor for the atom interpreter (r4xx+).
750 * Returns the value of the MC register.
751 */
752static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
753{
754 return 0;
755}
756
757/**
758 * cail_mc_write - write MC (Memory Controller) register
759 *
760 * @info: atom card_info pointer
761 * @reg: MC register offset
762 * @val: value to write to the pll register
763 *
764 * Provides a MC register accessor for the atom interpreter (r4xx+).
765 */
766static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
767{
768
769}
770
771/**
772 * cail_reg_write - write MMIO register
773 *
774 * @info: atom card_info pointer
775 * @reg: MMIO register offset
776 * @val: value to write to the pll register
777 *
778 * Provides a MMIO register accessor for the atom interpreter (r4xx+).
779 */
780static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
781{
782 struct amdgpu_device *adev = info->dev->dev_private;
783
784 WREG32(reg, val);
785}
786
787/**
788 * cail_reg_read - read MMIO register
789 *
790 * @info: atom card_info pointer
791 * @reg: MMIO register offset
792 *
793 * Provides an MMIO register accessor for the atom interpreter (r4xx+).
794 * Returns the value of the MMIO register.
795 */
796static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
797{
798 struct amdgpu_device *adev = info->dev->dev_private;
799 uint32_t r;
800
801 r = RREG32(reg);
802 return r;
803}
804
805/**
806 * cail_ioreg_write - write IO register
807 *
808 * @info: atom card_info pointer
809 * @reg: IO register offset
810 * @val: value to write to the pll register
811 *
812 * Provides a IO register accessor for the atom interpreter (r4xx+).
813 */
814static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
815{
816 struct amdgpu_device *adev = info->dev->dev_private;
817
818 WREG32_IO(reg, val);
819}
820
821/**
822 * cail_ioreg_read - read IO register
823 *
824 * @info: atom card_info pointer
825 * @reg: IO register offset
826 *
827 * Provides an IO register accessor for the atom interpreter (r4xx+).
828 * Returns the value of the IO register.
829 */
830static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
831{
832 struct amdgpu_device *adev = info->dev->dev_private;
833 uint32_t r;
834
835 r = RREG32_IO(reg);
836 return r;
837}
838
839/**
840 * amdgpu_atombios_fini - free the driver info and callbacks for atombios
841 *
842 * @adev: amdgpu_device pointer
843 *
844 * Frees the driver info and register access callbacks for the ATOM
845 * interpreter (r4xx+).
846 * Called at driver shutdown.
847 */
848static void amdgpu_atombios_fini(struct amdgpu_device *adev)
849{
Monk Liu89e0ec9f2016-05-27 19:34:11 +0800850 if (adev->mode_info.atom_context) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400851 kfree(adev->mode_info.atom_context->scratch);
Monk Liu89e0ec9f2016-05-27 19:34:11 +0800852 kfree(adev->mode_info.atom_context->iio);
853 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400854 kfree(adev->mode_info.atom_context);
855 adev->mode_info.atom_context = NULL;
856 kfree(adev->mode_info.atom_card_info);
857 adev->mode_info.atom_card_info = NULL;
858}
859
860/**
861 * amdgpu_atombios_init - init the driver info and callbacks for atombios
862 *
863 * @adev: amdgpu_device pointer
864 *
865 * Initializes the driver info and register access callbacks for the
866 * ATOM interpreter (r4xx+).
867 * Returns 0 on sucess, -ENOMEM on failure.
868 * Called at driver startup.
869 */
870static int amdgpu_atombios_init(struct amdgpu_device *adev)
871{
872 struct card_info *atom_card_info =
873 kzalloc(sizeof(struct card_info), GFP_KERNEL);
874
875 if (!atom_card_info)
876 return -ENOMEM;
877
878 adev->mode_info.atom_card_info = atom_card_info;
879 atom_card_info->dev = adev->ddev;
880 atom_card_info->reg_read = cail_reg_read;
881 atom_card_info->reg_write = cail_reg_write;
882 /* needed for iio ops */
883 if (adev->rio_mem) {
884 atom_card_info->ioreg_read = cail_ioreg_read;
885 atom_card_info->ioreg_write = cail_ioreg_write;
886 } else {
887 DRM_ERROR("Unable to find PCI I/O BAR; using MMIO for ATOM IIO\n");
888 atom_card_info->ioreg_read = cail_reg_read;
889 atom_card_info->ioreg_write = cail_reg_write;
890 }
891 atom_card_info->mc_read = cail_mc_read;
892 atom_card_info->mc_write = cail_mc_write;
893 atom_card_info->pll_read = cail_pll_read;
894 atom_card_info->pll_write = cail_pll_write;
895
896 adev->mode_info.atom_context = amdgpu_atom_parse(atom_card_info, adev->bios);
897 if (!adev->mode_info.atom_context) {
898 amdgpu_atombios_fini(adev);
899 return -ENOMEM;
900 }
901
902 mutex_init(&adev->mode_info.atom_context->mutex);
903 amdgpu_atombios_scratch_regs_init(adev);
904 amdgpu_atom_allocate_fb_scratch(adev->mode_info.atom_context);
905 return 0;
906}
907
908/* if we get transitioned to only one device, take VGA back */
909/**
910 * amdgpu_vga_set_decode - enable/disable vga decode
911 *
912 * @cookie: amdgpu_device pointer
913 * @state: enable/disable vga decode
914 *
915 * Enable/disable vga decode (all asics).
916 * Returns VGA resource flags.
917 */
918static unsigned int amdgpu_vga_set_decode(void *cookie, bool state)
919{
920 struct amdgpu_device *adev = cookie;
921 amdgpu_asic_set_vga_state(adev, state);
922 if (state)
923 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
924 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
925 else
926 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
927}
928
929/**
930 * amdgpu_check_pot_argument - check that argument is a power of two
931 *
932 * @arg: value to check
933 *
934 * Validates that a certain argument is a power of two (all asics).
935 * Returns true if argument is valid.
936 */
937static bool amdgpu_check_pot_argument(int arg)
938{
939 return (arg & (arg - 1)) == 0;
940}
941
942/**
943 * amdgpu_check_arguments - validate module params
944 *
945 * @adev: amdgpu_device pointer
946 *
947 * Validates certain module parameters and updates
948 * the associated values used by the driver (all asics).
949 */
950static void amdgpu_check_arguments(struct amdgpu_device *adev)
951{
Chunming Zhou5b011232015-12-10 17:34:33 +0800952 if (amdgpu_sched_jobs < 4) {
953 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
954 amdgpu_sched_jobs);
955 amdgpu_sched_jobs = 4;
956 } else if (!amdgpu_check_pot_argument(amdgpu_sched_jobs)){
957 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
958 amdgpu_sched_jobs);
959 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
960 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400961
962 if (amdgpu_gart_size != -1) {
Christian Königc4e1a132016-03-17 16:25:15 +0100963 /* gtt size must be greater or equal to 32M */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400964 if (amdgpu_gart_size < 32) {
965 dev_warn(adev->dev, "gart size (%d) too small\n",
966 amdgpu_gart_size);
967 amdgpu_gart_size = -1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400968 }
969 }
970
971 if (!amdgpu_check_pot_argument(amdgpu_vm_size)) {
972 dev_warn(adev->dev, "VM size (%d) must be a power of 2\n",
973 amdgpu_vm_size);
Alex Deucher8dacc122015-05-11 16:20:58 -0400974 amdgpu_vm_size = 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400975 }
976
977 if (amdgpu_vm_size < 1) {
978 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
979 amdgpu_vm_size);
Alex Deucher8dacc122015-05-11 16:20:58 -0400980 amdgpu_vm_size = 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400981 }
982
983 /*
984 * Max GPUVM size for Cayman, SI and CI are 40 bits.
985 */
986 if (amdgpu_vm_size > 1024) {
987 dev_warn(adev->dev, "VM size (%d) too large, max is 1TB\n",
988 amdgpu_vm_size);
Alex Deucher8dacc122015-05-11 16:20:58 -0400989 amdgpu_vm_size = 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400990 }
991
992 /* defines number of bits in page table versus page directory,
993 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
994 * page table and the remaining bits are in the page directory */
995 if (amdgpu_vm_block_size == -1) {
996
997 /* Total bits covered by PD + PTs */
998 unsigned bits = ilog2(amdgpu_vm_size) + 18;
999
1000 /* Make sure the PD is 4K in size up to 8GB address space.
1001 Above that split equal between PD and PTs */
1002 if (amdgpu_vm_size <= 8)
1003 amdgpu_vm_block_size = bits - 9;
1004 else
1005 amdgpu_vm_block_size = (bits + 3) / 2;
1006
1007 } else if (amdgpu_vm_block_size < 9) {
1008 dev_warn(adev->dev, "VM page table size (%d) too small\n",
1009 amdgpu_vm_block_size);
1010 amdgpu_vm_block_size = 9;
1011 }
1012
1013 if (amdgpu_vm_block_size > 24 ||
1014 (amdgpu_vm_size * 1024) < (1ull << amdgpu_vm_block_size)) {
1015 dev_warn(adev->dev, "VM page table size (%d) too large\n",
1016 amdgpu_vm_block_size);
1017 amdgpu_vm_block_size = 9;
1018 }
Christian König6a7f76e2016-08-24 15:51:49 +02001019
jimqu526bae32016-11-07 09:53:10 +08001020 if (amdgpu_vram_page_split != -1 && (amdgpu_vram_page_split < 16 ||
1021 !amdgpu_check_pot_argument(amdgpu_vram_page_split))) {
Christian König6a7f76e2016-08-24 15:51:49 +02001022 dev_warn(adev->dev, "invalid VRAM page split (%d)\n",
1023 amdgpu_vram_page_split);
1024 amdgpu_vram_page_split = 1024;
1025 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001026}
1027
1028/**
1029 * amdgpu_switcheroo_set_state - set switcheroo state
1030 *
1031 * @pdev: pci dev pointer
Lukas Wunner16944672015-09-05 11:17:35 +02001032 * @state: vga_switcheroo state
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001033 *
1034 * Callback for the switcheroo driver. Suspends or resumes the
1035 * the asics before or after it is powered up using ACPI methods.
1036 */
1037static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1038{
1039 struct drm_device *dev = pci_get_drvdata(pdev);
1040
1041 if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
1042 return;
1043
1044 if (state == VGA_SWITCHEROO_ON) {
1045 unsigned d3_delay = dev->pdev->d3_delay;
1046
1047 printk(KERN_INFO "amdgpu: switched on\n");
1048 /* don't suspend or resume card normally */
1049 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1050
Alex Deucher810ddc32016-08-23 13:25:49 -04001051 amdgpu_device_resume(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001052
1053 dev->pdev->d3_delay = d3_delay;
1054
1055 dev->switch_power_state = DRM_SWITCH_POWER_ON;
1056 drm_kms_helper_poll_enable(dev);
1057 } else {
1058 printk(KERN_INFO "amdgpu: switched off\n");
1059 drm_kms_helper_poll_disable(dev);
1060 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Alex Deucher810ddc32016-08-23 13:25:49 -04001061 amdgpu_device_suspend(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001062 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1063 }
1064}
1065
1066/**
1067 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1068 *
1069 * @pdev: pci dev pointer
1070 *
1071 * Callback for the switcheroo driver. Check of the switcheroo
1072 * state can be changed.
1073 * Returns true if the state can be changed, false if not.
1074 */
1075static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1076{
1077 struct drm_device *dev = pci_get_drvdata(pdev);
1078
1079 /*
1080 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1081 * locking inversion with the driver load path. And the access here is
1082 * completely racy anyway. So don't bother with locking for now.
1083 */
1084 return dev->open_count == 0;
1085}
1086
1087static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1088 .set_gpu_state = amdgpu_switcheroo_set_state,
1089 .reprobe = NULL,
1090 .can_switch = amdgpu_switcheroo_can_switch,
1091};
1092
1093int amdgpu_set_clockgating_state(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001094 enum amd_ip_block_type block_type,
1095 enum amd_clockgating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001096{
1097 int i, r = 0;
1098
1099 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001100 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001101 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001102 if (adev->ip_blocks[i].version->type == block_type) {
1103 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1104 state);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001105 if (r)
1106 return r;
Alex Deuchera225bf12016-06-23 11:48:30 -04001107 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001108 }
1109 }
1110 return r;
1111}
1112
1113int amdgpu_set_powergating_state(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001114 enum amd_ip_block_type block_type,
1115 enum amd_powergating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001116{
1117 int i, r = 0;
1118
1119 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001120 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001121 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001122 if (adev->ip_blocks[i].version->type == block_type) {
1123 r = adev->ip_blocks[i].version->funcs->set_powergating_state((void *)adev,
1124 state);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001125 if (r)
1126 return r;
Alex Deuchera225bf12016-06-23 11:48:30 -04001127 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001128 }
1129 }
1130 return r;
1131}
1132
Alex Deucher5dbbb602016-06-23 11:41:04 -04001133int amdgpu_wait_for_idle(struct amdgpu_device *adev,
1134 enum amd_ip_block_type block_type)
1135{
1136 int i, r;
1137
1138 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001139 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001140 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001141 if (adev->ip_blocks[i].version->type == block_type) {
1142 r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -04001143 if (r)
1144 return r;
1145 break;
1146 }
1147 }
1148 return 0;
1149
1150}
1151
1152bool amdgpu_is_idle(struct amdgpu_device *adev,
1153 enum amd_ip_block_type block_type)
1154{
1155 int i;
1156
1157 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001158 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001159 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001160 if (adev->ip_blocks[i].version->type == block_type)
1161 return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -04001162 }
1163 return true;
1164
1165}
1166
Alex Deuchera1255102016-10-13 17:41:13 -04001167struct amdgpu_ip_block * amdgpu_get_ip_block(struct amdgpu_device *adev,
1168 enum amd_ip_block_type type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001169{
1170 int i;
1171
1172 for (i = 0; i < adev->num_ip_blocks; i++)
Alex Deuchera1255102016-10-13 17:41:13 -04001173 if (adev->ip_blocks[i].version->type == type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001174 return &adev->ip_blocks[i];
1175
1176 return NULL;
1177}
1178
1179/**
1180 * amdgpu_ip_block_version_cmp
1181 *
1182 * @adev: amdgpu_device pointer
yanyang15fc3aee2015-05-22 14:39:35 -04001183 * @type: enum amd_ip_block_type
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001184 * @major: major version
1185 * @minor: minor version
1186 *
1187 * return 0 if equal or greater
1188 * return 1 if smaller or the ip_block doesn't exist
1189 */
1190int amdgpu_ip_block_version_cmp(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001191 enum amd_ip_block_type type,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001192 u32 major, u32 minor)
1193{
Alex Deuchera1255102016-10-13 17:41:13 -04001194 struct amdgpu_ip_block *ip_block = amdgpu_get_ip_block(adev, type);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001195
Alex Deuchera1255102016-10-13 17:41:13 -04001196 if (ip_block && ((ip_block->version->major > major) ||
1197 ((ip_block->version->major == major) &&
1198 (ip_block->version->minor >= minor))))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001199 return 0;
1200
1201 return 1;
1202}
1203
Alex Deuchera1255102016-10-13 17:41:13 -04001204/**
1205 * amdgpu_ip_block_add
1206 *
1207 * @adev: amdgpu_device pointer
1208 * @ip_block_version: pointer to the IP to add
1209 *
1210 * Adds the IP block driver information to the collection of IPs
1211 * on the asic.
1212 */
1213int amdgpu_ip_block_add(struct amdgpu_device *adev,
1214 const struct amdgpu_ip_block_version *ip_block_version)
1215{
1216 if (!ip_block_version)
1217 return -EINVAL;
1218
1219 adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
1220
1221 return 0;
1222}
1223
Alex Deucher483ef982016-09-30 12:43:04 -04001224static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
Emily Deng9accf2f2016-08-10 16:01:25 +08001225{
1226 adev->enable_virtual_display = false;
1227
1228 if (amdgpu_virtual_display) {
1229 struct drm_device *ddev = adev->ddev;
1230 const char *pci_address_name = pci_name(ddev->pdev);
Emily Deng0f663562016-09-30 13:02:18 -04001231 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
Emily Deng9accf2f2016-08-10 16:01:25 +08001232
1233 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1234 pciaddstr_tmp = pciaddstr;
Emily Deng0f663562016-09-30 13:02:18 -04001235 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1236 pciaddname = strsep(&pciaddname_tmp, ",");
Emily Deng9accf2f2016-08-10 16:01:25 +08001237 if (!strcmp(pci_address_name, pciaddname)) {
Emily Deng0f663562016-09-30 13:02:18 -04001238 long num_crtc;
1239 int res = -1;
1240
Emily Deng9accf2f2016-08-10 16:01:25 +08001241 adev->enable_virtual_display = true;
Emily Deng0f663562016-09-30 13:02:18 -04001242
1243 if (pciaddname_tmp)
1244 res = kstrtol(pciaddname_tmp, 10,
1245 &num_crtc);
1246
1247 if (!res) {
1248 if (num_crtc < 1)
1249 num_crtc = 1;
1250 if (num_crtc > 6)
1251 num_crtc = 6;
1252 adev->mode_info.num_crtc = num_crtc;
1253 } else {
1254 adev->mode_info.num_crtc = 1;
1255 }
Emily Deng9accf2f2016-08-10 16:01:25 +08001256 break;
1257 }
1258 }
1259
Emily Deng0f663562016-09-30 13:02:18 -04001260 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
1261 amdgpu_virtual_display, pci_address_name,
1262 adev->enable_virtual_display, adev->mode_info.num_crtc);
Emily Deng9accf2f2016-08-10 16:01:25 +08001263
1264 kfree(pciaddstr);
1265 }
1266}
1267
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001268static int amdgpu_early_init(struct amdgpu_device *adev)
1269{
Alex Deucheraaa36a92015-04-20 17:31:14 -04001270 int i, r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001271
Alex Deucher483ef982016-09-30 12:43:04 -04001272 amdgpu_device_enable_virtual_display(adev);
Emily Denga6be7572016-08-08 11:37:50 +08001273
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001274 switch (adev->asic_type) {
Alex Deucheraaa36a92015-04-20 17:31:14 -04001275 case CHIP_TOPAZ:
1276 case CHIP_TONGA:
David Zhang48299f92015-07-08 01:05:16 +08001277 case CHIP_FIJI:
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001278 case CHIP_POLARIS11:
1279 case CHIP_POLARIS10:
Alex Deucheraaa36a92015-04-20 17:31:14 -04001280 case CHIP_CARRIZO:
Samuel Li39bb0c92015-10-08 16:31:43 -04001281 case CHIP_STONEY:
1282 if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
Alex Deucheraaa36a92015-04-20 17:31:14 -04001283 adev->family = AMDGPU_FAMILY_CZ;
1284 else
1285 adev->family = AMDGPU_FAMILY_VI;
1286
1287 r = vi_set_ip_blocks(adev);
1288 if (r)
1289 return r;
1290 break;
Ken Wang33f34802016-01-21 17:29:41 +08001291#ifdef CONFIG_DRM_AMDGPU_SI
1292 case CHIP_VERDE:
1293 case CHIP_TAHITI:
1294 case CHIP_PITCAIRN:
1295 case CHIP_OLAND:
1296 case CHIP_HAINAN:
Ken Wang295d0da2016-05-24 21:02:53 +08001297 adev->family = AMDGPU_FAMILY_SI;
Ken Wang33f34802016-01-21 17:29:41 +08001298 r = si_set_ip_blocks(adev);
1299 if (r)
1300 return r;
1301 break;
1302#endif
Alex Deuchera2e73f52015-04-20 17:09:27 -04001303#ifdef CONFIG_DRM_AMDGPU_CIK
1304 case CHIP_BONAIRE:
1305 case CHIP_HAWAII:
1306 case CHIP_KAVERI:
1307 case CHIP_KABINI:
1308 case CHIP_MULLINS:
1309 if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
1310 adev->family = AMDGPU_FAMILY_CI;
1311 else
1312 adev->family = AMDGPU_FAMILY_KV;
1313
1314 r = cik_set_ip_blocks(adev);
1315 if (r)
1316 return r;
1317 break;
1318#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001319 default:
1320 /* FIXME: not supported yet */
1321 return -EINVAL;
1322 }
1323
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001324 for (i = 0; i < adev->num_ip_blocks; i++) {
1325 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
1326 DRM_ERROR("disabled ip block: %d\n", i);
Alex Deuchera1255102016-10-13 17:41:13 -04001327 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001328 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001329 if (adev->ip_blocks[i].version->funcs->early_init) {
1330 r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001331 if (r == -ENOENT) {
Alex Deuchera1255102016-10-13 17:41:13 -04001332 adev->ip_blocks[i].status.valid = false;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001333 } else if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001334 DRM_ERROR("early_init of IP block <%s> failed %d\n",
1335 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001336 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001337 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001338 adev->ip_blocks[i].status.valid = true;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001339 }
Alex Deucher974e6b62015-07-10 13:59:44 -04001340 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001341 adev->ip_blocks[i].status.valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001342 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001343 }
1344 }
1345
Nicolai Hähnle395d1fb2016-06-02 12:32:07 +02001346 adev->cg_flags &= amdgpu_cg_mask;
1347 adev->pg_flags &= amdgpu_pg_mask;
1348
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001349 return 0;
1350}
1351
1352static int amdgpu_init(struct amdgpu_device *adev)
1353{
1354 int i, r;
1355
1356 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001357 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001358 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001359 r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001360 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001361 DRM_ERROR("sw_init of IP block <%s> failed %d\n",
1362 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001363 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001364 }
Alex Deuchera1255102016-10-13 17:41:13 -04001365 adev->ip_blocks[i].status.sw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001366 /* need to do gmc hw init early so we can allocate gpu mem */
Alex Deuchera1255102016-10-13 17:41:13 -04001367 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001368 r = amdgpu_vram_scratch_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001369 if (r) {
1370 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001371 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001372 }
Alex Deuchera1255102016-10-13 17:41:13 -04001373 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001374 if (r) {
1375 DRM_ERROR("hw_init %d failed %d\n", i, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001376 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001377 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001378 r = amdgpu_wb_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001379 if (r) {
1380 DRM_ERROR("amdgpu_wb_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001381 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001382 }
Alex Deuchera1255102016-10-13 17:41:13 -04001383 adev->ip_blocks[i].status.hw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001384 }
1385 }
1386
1387 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001388 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001389 continue;
1390 /* gmc hw init is done early */
Alex Deuchera1255102016-10-13 17:41:13 -04001391 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001392 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001393 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001394 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001395 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
1396 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001397 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001398 }
Alex Deuchera1255102016-10-13 17:41:13 -04001399 adev->ip_blocks[i].status.hw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001400 }
1401
1402 return 0;
1403}
1404
1405static int amdgpu_late_init(struct amdgpu_device *adev)
1406{
1407 int i = 0, r;
1408
1409 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001410 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001411 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001412 if (adev->ip_blocks[i].version->funcs->late_init) {
1413 r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001414 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001415 DRM_ERROR("late_init of IP block <%s> failed %d\n",
1416 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001417 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001418 }
Alex Deuchera1255102016-10-13 17:41:13 -04001419 adev->ip_blocks[i].status.late_initialized = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001420 }
Alex Deucher4a446d52016-10-07 14:48:18 -04001421 /* skip CG for VCE/UVD, it's handled specially */
Alex Deuchera1255102016-10-13 17:41:13 -04001422 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1423 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
Alex Deucher4a446d52016-10-07 14:48:18 -04001424 /* enable clockgating to save power */
Alex Deuchera1255102016-10-13 17:41:13 -04001425 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1426 AMD_CG_STATE_GATE);
Alex Deucher4a446d52016-10-07 14:48:18 -04001427 if (r) {
1428 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001429 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher4a446d52016-10-07 14:48:18 -04001430 return r;
1431 }
Arindam Nathb0b00ff2016-10-07 19:01:37 +05301432 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001433 }
1434
1435 return 0;
1436}
1437
1438static int amdgpu_fini(struct amdgpu_device *adev)
1439{
1440 int i, r;
1441
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001442 /* need to disable SMC first */
1443 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001444 if (!adev->ip_blocks[i].status.hw)
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001445 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001446 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001447 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
Alex Deuchera1255102016-10-13 17:41:13 -04001448 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1449 AMD_CG_STATE_UNGATE);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001450 if (r) {
1451 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001452 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001453 return r;
1454 }
Alex Deuchera1255102016-10-13 17:41:13 -04001455 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001456 /* XXX handle errors */
1457 if (r) {
1458 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001459 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001460 }
Alex Deuchera1255102016-10-13 17:41:13 -04001461 adev->ip_blocks[i].status.hw = false;
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001462 break;
1463 }
1464 }
1465
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001466 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001467 if (!adev->ip_blocks[i].status.hw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001468 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001469 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001470 amdgpu_wb_fini(adev);
1471 amdgpu_vram_scratch_fini(adev);
1472 }
1473 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
Alex Deuchera1255102016-10-13 17:41:13 -04001474 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1475 AMD_CG_STATE_UNGATE);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001476 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001477 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1478 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001479 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001480 }
Alex Deuchera1255102016-10-13 17:41:13 -04001481 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001482 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001483 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001484 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
1485 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001486 }
Alex Deuchera1255102016-10-13 17:41:13 -04001487 adev->ip_blocks[i].status.hw = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001488 }
1489
1490 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001491 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001492 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001493 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001494 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001495 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001496 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
1497 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001498 }
Alex Deuchera1255102016-10-13 17:41:13 -04001499 adev->ip_blocks[i].status.sw = false;
1500 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001501 }
1502
Monk Liua6dcfd92016-05-19 14:36:34 +08001503 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001504 if (!adev->ip_blocks[i].status.late_initialized)
Grazvydas Ignotas8a2eef12016-10-03 00:06:44 +03001505 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001506 if (adev->ip_blocks[i].version->funcs->late_fini)
1507 adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
1508 adev->ip_blocks[i].status.late_initialized = false;
Monk Liua6dcfd92016-05-19 14:36:34 +08001509 }
1510
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001511 return 0;
1512}
1513
1514static int amdgpu_suspend(struct amdgpu_device *adev)
1515{
1516 int i, r;
1517
Flora Cuic5a93a22016-02-26 10:45:25 +08001518 /* ungate SMC block first */
1519 r = amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC,
1520 AMD_CG_STATE_UNGATE);
1521 if (r) {
1522 DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n",r);
1523 }
1524
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001525 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001526 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001527 continue;
1528 /* ungate blocks so that suspend can properly shut them down */
Flora Cuic5a93a22016-02-26 10:45:25 +08001529 if (i != AMD_IP_BLOCK_TYPE_SMC) {
Alex Deuchera1255102016-10-13 17:41:13 -04001530 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1531 AMD_CG_STATE_UNGATE);
Flora Cuic5a93a22016-02-26 10:45:25 +08001532 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001533 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1534 adev->ip_blocks[i].version->funcs->name, r);
Flora Cuic5a93a22016-02-26 10:45:25 +08001535 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001536 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001537 /* XXX handle errors */
Alex Deuchera1255102016-10-13 17:41:13 -04001538 r = adev->ip_blocks[i].version->funcs->suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001539 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001540 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001541 DRM_ERROR("suspend of IP block <%s> failed %d\n",
1542 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001543 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001544 }
1545
1546 return 0;
1547}
1548
1549static int amdgpu_resume(struct amdgpu_device *adev)
1550{
1551 int i, r;
1552
1553 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001554 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001555 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001556 r = adev->ip_blocks[i].version->funcs->resume(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001557 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001558 DRM_ERROR("resume of IP block <%s> failed %d\n",
1559 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001560 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001561 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001562 }
1563
1564 return 0;
1565}
1566
Monk Liu4e99a442016-03-31 13:26:59 +08001567static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
Andres Rodriguez048765a2016-06-11 02:51:32 -04001568{
Monk Liu4e99a442016-03-31 13:26:59 +08001569 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
1570 adev->virtualization.virtual_caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
Andres Rodriguez048765a2016-06-11 02:51:32 -04001571}
1572
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001573/**
1574 * amdgpu_device_init - initialize the driver
1575 *
1576 * @adev: amdgpu_device pointer
1577 * @pdev: drm dev pointer
1578 * @pdev: pci dev pointer
1579 * @flags: driver flags
1580 *
1581 * Initializes the driver info and hw (all asics).
1582 * Returns 0 for success or an error on failure.
1583 * Called at driver startup.
1584 */
1585int amdgpu_device_init(struct amdgpu_device *adev,
1586 struct drm_device *ddev,
1587 struct pci_dev *pdev,
1588 uint32_t flags)
1589{
1590 int r, i;
1591 bool runtime = false;
Marek Olšák95844d22016-08-17 23:49:27 +02001592 u32 max_MBps;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001593
1594 adev->shutdown = false;
1595 adev->dev = &pdev->dev;
1596 adev->ddev = ddev;
1597 adev->pdev = pdev;
1598 adev->flags = flags;
Jammy Zhou2f7d10b2015-07-22 11:29:01 +08001599 adev->asic_type = flags & AMD_ASIC_MASK;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001600 adev->is_atom_bios = false;
1601 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
1602 adev->mc.gtt_size = 512 * 1024 * 1024;
1603 adev->accel_working = false;
1604 adev->num_rings = 0;
1605 adev->mman.buffer_funcs = NULL;
1606 adev->mman.buffer_funcs_ring = NULL;
1607 adev->vm_manager.vm_pte_funcs = NULL;
Christian König2d55e452016-02-08 17:37:38 +01001608 adev->vm_manager.vm_pte_num_rings = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001609 adev->gart.gart_funcs = NULL;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001610 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001611
1612 adev->smc_rreg = &amdgpu_invalid_rreg;
1613 adev->smc_wreg = &amdgpu_invalid_wreg;
1614 adev->pcie_rreg = &amdgpu_invalid_rreg;
1615 adev->pcie_wreg = &amdgpu_invalid_wreg;
Huang Rui36b9a952016-08-31 13:23:25 +08001616 adev->pciep_rreg = &amdgpu_invalid_rreg;
1617 adev->pciep_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001618 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
1619 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
1620 adev->didt_rreg = &amdgpu_invalid_rreg;
1621 adev->didt_wreg = &amdgpu_invalid_wreg;
Rex Zhuccdbb202016-06-08 12:47:41 +08001622 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
1623 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001624 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
1625 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
1626
Rex Zhuccdbb202016-06-08 12:47:41 +08001627
Alex Deucher3e39ab92015-06-05 15:04:33 -04001628 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
1629 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
1630 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001631
1632 /* mutex initialization are all done here so we
1633 * can recall function without having locking issues */
Christian König8d0a7ce2015-11-03 20:58:50 +01001634 mutex_init(&adev->vm_manager.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001635 atomic_set(&adev->irq.ih.lock, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001636 mutex_init(&adev->pm.mutex);
1637 mutex_init(&adev->gfx.gpu_clock_mutex);
1638 mutex_init(&adev->srbm_mutex);
1639 mutex_init(&adev->grbm_idx_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001640 mutex_init(&adev->mn_lock);
1641 hash_init(adev->mn_hash);
1642
1643 amdgpu_check_arguments(adev);
1644
1645 /* Registers mapping */
1646 /* TODO: block userspace mapping of io register */
1647 spin_lock_init(&adev->mmio_idx_lock);
1648 spin_lock_init(&adev->smc_idx_lock);
1649 spin_lock_init(&adev->pcie_idx_lock);
1650 spin_lock_init(&adev->uvd_ctx_idx_lock);
1651 spin_lock_init(&adev->didt_idx_lock);
Rex Zhuccdbb202016-06-08 12:47:41 +08001652 spin_lock_init(&adev->gc_cac_idx_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001653 spin_lock_init(&adev->audio_endpt_idx_lock);
Marek Olšák95844d22016-08-17 23:49:27 +02001654 spin_lock_init(&adev->mm_stats.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001655
Chunming Zhou0c4e7fa2016-08-17 11:41:30 +08001656 INIT_LIST_HEAD(&adev->shadow_list);
1657 mutex_init(&adev->shadow_list_lock);
1658
Chunming Zhou5c1354b2016-08-30 16:13:10 +08001659 INIT_LIST_HEAD(&adev->gtt_list);
1660 spin_lock_init(&adev->gtt_list_lock);
1661
Ken Wangda69c1612016-01-21 19:08:55 +08001662 if (adev->asic_type >= CHIP_BONAIRE) {
1663 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
1664 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
1665 } else {
1666 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
1667 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
1668 }
Chunming Zhou5c1354b2016-08-30 16:13:10 +08001669
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001670 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
1671 if (adev->rmmio == NULL) {
1672 return -ENOMEM;
1673 }
1674 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
1675 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
1676
Ken Wangda69c1612016-01-21 19:08:55 +08001677 if (adev->asic_type >= CHIP_BONAIRE)
1678 /* doorbell bar mapping */
1679 amdgpu_doorbell_init(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001680
1681 /* io port mapping */
1682 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
1683 if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
1684 adev->rio_mem_size = pci_resource_len(adev->pdev, i);
1685 adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
1686 break;
1687 }
1688 }
1689 if (adev->rio_mem == NULL)
1690 DRM_ERROR("Unable to find PCI I/O BAR\n");
1691
1692 /* early init functions */
1693 r = amdgpu_early_init(adev);
1694 if (r)
1695 return r;
1696
1697 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
1698 /* this will fail for cards that aren't VGA class devices, just
1699 * ignore it */
1700 vga_client_register(adev->pdev, adev, NULL, amdgpu_vga_set_decode);
1701
1702 if (amdgpu_runtime_pm == 1)
1703 runtime = true;
Alex Deuchere9bef452016-04-25 13:12:18 -04001704 if (amdgpu_device_is_px(ddev))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001705 runtime = true;
1706 vga_switcheroo_register_client(adev->pdev, &amdgpu_switcheroo_ops, runtime);
1707 if (runtime)
1708 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
1709
1710 /* Read BIOS */
Alex Deucher83ba1262016-06-03 18:21:41 -04001711 if (!amdgpu_get_bios(adev)) {
1712 r = -EINVAL;
1713 goto failed;
1714 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001715 /* Must be an ATOMBIOS */
1716 if (!adev->is_atom_bios) {
1717 dev_err(adev->dev, "Expecting atombios for GPU\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001718 r = -EINVAL;
1719 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001720 }
1721 r = amdgpu_atombios_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001722 if (r) {
1723 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001724 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001725 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001726
Monk Liu4e99a442016-03-31 13:26:59 +08001727 /* detect if we are with an SRIOV vbios */
1728 amdgpu_device_detect_sriov_bios(adev);
Andres Rodriguez048765a2016-06-11 02:51:32 -04001729
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001730 /* Post card if necessary */
Monk Liubec86372016-09-14 19:38:08 +08001731 if (amdgpu_vpost_needed(adev)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001732 if (!adev->bios) {
Monk Liubec86372016-09-14 19:38:08 +08001733 dev_err(adev->dev, "no vBIOS found\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001734 r = -EINVAL;
1735 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001736 }
Monk Liubec86372016-09-14 19:38:08 +08001737 DRM_INFO("GPU posting now...\n");
Monk Liu4e99a442016-03-31 13:26:59 +08001738 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
1739 if (r) {
1740 dev_err(adev->dev, "gpu post error!\n");
1741 goto failed;
1742 }
1743 } else {
1744 DRM_INFO("GPU post is not needed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001745 }
1746
1747 /* Initialize clocks */
1748 r = amdgpu_atombios_get_clock_info(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001749 if (r) {
1750 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001751 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001752 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001753 /* init i2c buses */
1754 amdgpu_atombios_i2c_init(adev);
1755
1756 /* Fence driver */
1757 r = amdgpu_fence_driver_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001758 if (r) {
1759 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001760 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001761 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001762
1763 /* init the mode config */
1764 drm_mode_config_init(adev->ddev);
1765
1766 r = amdgpu_init(adev);
1767 if (r) {
Alex Deucher2c1a2782015-12-07 17:02:53 -05001768 dev_err(adev->dev, "amdgpu_init failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001769 amdgpu_fini(adev);
Alex Deucher83ba1262016-06-03 18:21:41 -04001770 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001771 }
1772
1773 adev->accel_working = true;
1774
Marek Olšák95844d22016-08-17 23:49:27 +02001775 /* Initialize the buffer migration limit. */
1776 if (amdgpu_moverate >= 0)
1777 max_MBps = amdgpu_moverate;
1778 else
1779 max_MBps = 8; /* Allow 8 MB/s. */
1780 /* Get a log2 for easy divisions. */
1781 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
1782
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001783 amdgpu_fbdev_init(adev);
1784
1785 r = amdgpu_ib_pool_init(adev);
1786 if (r) {
1787 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
Alex Deucher83ba1262016-06-03 18:21:41 -04001788 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001789 }
1790
1791 r = amdgpu_ib_ring_tests(adev);
1792 if (r)
1793 DRM_ERROR("ib ring test failed (%d).\n", r);
1794
1795 r = amdgpu_gem_debugfs_init(adev);
1796 if (r) {
1797 DRM_ERROR("registering gem debugfs failed (%d).\n", r);
1798 }
1799
1800 r = amdgpu_debugfs_regs_init(adev);
1801 if (r) {
1802 DRM_ERROR("registering register debugfs failed (%d).\n", r);
1803 }
1804
Huang Rui50ab2532016-06-12 15:51:09 +08001805 r = amdgpu_debugfs_firmware_init(adev);
1806 if (r) {
1807 DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
1808 return r;
1809 }
1810
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001811 if ((amdgpu_testing & 1)) {
1812 if (adev->accel_working)
1813 amdgpu_test_moves(adev);
1814 else
1815 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
1816 }
1817 if ((amdgpu_testing & 2)) {
1818 if (adev->accel_working)
1819 amdgpu_test_syncing(adev);
1820 else
1821 DRM_INFO("amdgpu: acceleration disabled, skipping sync tests\n");
1822 }
1823 if (amdgpu_benchmarking) {
1824 if (adev->accel_working)
1825 amdgpu_benchmark(adev, amdgpu_benchmarking);
1826 else
1827 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
1828 }
1829
1830 /* enable clockgating, etc. after ib tests, etc. since some blocks require
1831 * explicit gating rather than handling it automatically.
1832 */
1833 r = amdgpu_late_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001834 if (r) {
1835 dev_err(adev->dev, "amdgpu_late_init failed\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001836 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001837 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001838
1839 return 0;
Alex Deucher83ba1262016-06-03 18:21:41 -04001840
1841failed:
1842 if (runtime)
1843 vga_switcheroo_fini_domain_pm_ops(adev->dev);
1844 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001845}
1846
1847static void amdgpu_debugfs_remove_files(struct amdgpu_device *adev);
1848
1849/**
1850 * amdgpu_device_fini - tear down the driver
1851 *
1852 * @adev: amdgpu_device pointer
1853 *
1854 * Tear down the driver info (all asics).
1855 * Called at driver shutdown.
1856 */
1857void amdgpu_device_fini(struct amdgpu_device *adev)
1858{
1859 int r;
1860
1861 DRM_INFO("amdgpu: finishing device.\n");
1862 adev->shutdown = true;
Grazvydas Ignotasa951ed82016-09-25 23:34:48 +03001863 drm_crtc_force_disable_all(adev->ddev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001864 /* evict vram memory */
1865 amdgpu_bo_evict_vram(adev);
1866 amdgpu_ib_pool_fini(adev);
1867 amdgpu_fence_driver_fini(adev);
1868 amdgpu_fbdev_fini(adev);
1869 r = amdgpu_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001870 adev->accel_working = false;
1871 /* free i2c buses */
1872 amdgpu_i2c_fini(adev);
1873 amdgpu_atombios_fini(adev);
1874 kfree(adev->bios);
1875 adev->bios = NULL;
1876 vga_switcheroo_unregister_client(adev->pdev);
Alex Deucher83ba1262016-06-03 18:21:41 -04001877 if (adev->flags & AMD_IS_PX)
1878 vga_switcheroo_fini_domain_pm_ops(adev->dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001879 vga_client_register(adev->pdev, NULL, NULL, NULL);
1880 if (adev->rio_mem)
1881 pci_iounmap(adev->pdev, adev->rio_mem);
1882 adev->rio_mem = NULL;
1883 iounmap(adev->rmmio);
1884 adev->rmmio = NULL;
Ken Wangda69c1612016-01-21 19:08:55 +08001885 if (adev->asic_type >= CHIP_BONAIRE)
1886 amdgpu_doorbell_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001887 amdgpu_debugfs_regs_cleanup(adev);
1888 amdgpu_debugfs_remove_files(adev);
1889}
1890
1891
1892/*
1893 * Suspend & resume.
1894 */
1895/**
Alex Deucher810ddc32016-08-23 13:25:49 -04001896 * amdgpu_device_suspend - initiate device suspend
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001897 *
1898 * @pdev: drm dev pointer
1899 * @state: suspend state
1900 *
1901 * Puts the hw in the suspend state (all asics).
1902 * Returns 0 for success or an error on failure.
1903 * Called at driver suspend.
1904 */
Alex Deucher810ddc32016-08-23 13:25:49 -04001905int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001906{
1907 struct amdgpu_device *adev;
1908 struct drm_crtc *crtc;
1909 struct drm_connector *connector;
Alex Deucher5ceb54c2015-08-05 12:41:48 -04001910 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001911
1912 if (dev == NULL || dev->dev_private == NULL) {
1913 return -ENODEV;
1914 }
1915
1916 adev = dev->dev_private;
1917
1918 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
1919 return 0;
1920
1921 drm_kms_helper_poll_disable(dev);
1922
1923 /* turn off display hw */
Alex Deucher4c7fbc32015-09-23 14:32:06 -04001924 drm_modeset_lock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001925 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1926 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
1927 }
Alex Deucher4c7fbc32015-09-23 14:32:06 -04001928 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001929
Alex Deucher756e6882015-10-08 00:03:36 -04001930 /* unpin the front buffers and cursors */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001931 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Alex Deucher756e6882015-10-08 00:03:36 -04001932 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001933 struct amdgpu_framebuffer *rfb = to_amdgpu_framebuffer(crtc->primary->fb);
1934 struct amdgpu_bo *robj;
1935
Alex Deucher756e6882015-10-08 00:03:36 -04001936 if (amdgpu_crtc->cursor_bo) {
1937 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
1938 r = amdgpu_bo_reserve(aobj, false);
1939 if (r == 0) {
1940 amdgpu_bo_unpin(aobj);
1941 amdgpu_bo_unreserve(aobj);
1942 }
1943 }
1944
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001945 if (rfb == NULL || rfb->obj == NULL) {
1946 continue;
1947 }
1948 robj = gem_to_amdgpu_bo(rfb->obj);
1949 /* don't unpin kernel fb objects */
1950 if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
1951 r = amdgpu_bo_reserve(robj, false);
1952 if (r == 0) {
1953 amdgpu_bo_unpin(robj);
1954 amdgpu_bo_unreserve(robj);
1955 }
1956 }
1957 }
1958 /* evict vram memory */
1959 amdgpu_bo_evict_vram(adev);
1960
Alex Deucher5ceb54c2015-08-05 12:41:48 -04001961 amdgpu_fence_driver_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001962
1963 r = amdgpu_suspend(adev);
1964
Alex Deuchera0a71e42016-10-10 12:41:36 -04001965 /* evict remaining vram memory
1966 * This second call to evict vram is to evict the gart page table
1967 * using the CPU.
1968 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001969 amdgpu_bo_evict_vram(adev);
1970
Alex Deuchere695e772016-10-19 14:40:58 -04001971 amdgpu_atombios_scratch_regs_save(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001972 pci_save_state(dev->pdev);
1973 if (suspend) {
1974 /* Shut down the device */
1975 pci_disable_device(dev->pdev);
1976 pci_set_power_state(dev->pdev, PCI_D3hot);
jimqu74b0b152016-09-07 17:09:12 +08001977 } else {
1978 r = amdgpu_asic_reset(adev);
1979 if (r)
1980 DRM_ERROR("amdgpu asic reset failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001981 }
1982
1983 if (fbcon) {
1984 console_lock();
1985 amdgpu_fbdev_set_suspend(adev, 1);
1986 console_unlock();
1987 }
1988 return 0;
1989}
1990
1991/**
Alex Deucher810ddc32016-08-23 13:25:49 -04001992 * amdgpu_device_resume - initiate device resume
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001993 *
1994 * @pdev: drm dev pointer
1995 *
1996 * Bring the hw back to operating state (all asics).
1997 * Returns 0 for success or an error on failure.
1998 * Called at driver resume.
1999 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002000int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002001{
2002 struct drm_connector *connector;
2003 struct amdgpu_device *adev = dev->dev_private;
Alex Deucher756e6882015-10-08 00:03:36 -04002004 struct drm_crtc *crtc;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002005 int r;
2006
2007 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2008 return 0;
2009
jimqu74b0b152016-09-07 17:09:12 +08002010 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002011 console_lock();
jimqu74b0b152016-09-07 17:09:12 +08002012
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002013 if (resume) {
2014 pci_set_power_state(dev->pdev, PCI_D0);
2015 pci_restore_state(dev->pdev);
jimqu74b0b152016-09-07 17:09:12 +08002016 r = pci_enable_device(dev->pdev);
2017 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002018 if (fbcon)
2019 console_unlock();
jimqu74b0b152016-09-07 17:09:12 +08002020 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002021 }
2022 }
Alex Deuchere695e772016-10-19 14:40:58 -04002023 amdgpu_atombios_scratch_regs_restore(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002024
2025 /* post card */
jimqu74b0b152016-09-07 17:09:12 +08002026 if (!amdgpu_card_posted(adev) || !resume) {
2027 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2028 if (r)
2029 DRM_ERROR("amdgpu asic init failed\n");
2030 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002031
2032 r = amdgpu_resume(adev);
Flora Cuica198522016-02-04 15:10:08 +08002033 if (r)
2034 DRM_ERROR("amdgpu_resume failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002035
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002036 amdgpu_fence_driver_resume(adev);
2037
Flora Cuica198522016-02-04 15:10:08 +08002038 if (resume) {
2039 r = amdgpu_ib_ring_tests(adev);
2040 if (r)
2041 DRM_ERROR("ib ring test failed (%d).\n", r);
2042 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002043
2044 r = amdgpu_late_init(adev);
2045 if (r)
2046 return r;
2047
Alex Deucher756e6882015-10-08 00:03:36 -04002048 /* pin cursors */
2049 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2050 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2051
2052 if (amdgpu_crtc->cursor_bo) {
2053 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
2054 r = amdgpu_bo_reserve(aobj, false);
2055 if (r == 0) {
2056 r = amdgpu_bo_pin(aobj,
2057 AMDGPU_GEM_DOMAIN_VRAM,
2058 &amdgpu_crtc->cursor_addr);
2059 if (r != 0)
2060 DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
2061 amdgpu_bo_unreserve(aobj);
2062 }
2063 }
2064 }
2065
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002066 /* blat the mode back in */
2067 if (fbcon) {
2068 drm_helper_resume_force_mode(dev);
2069 /* turn on display hw */
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002070 drm_modeset_lock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002071 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2072 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
2073 }
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002074 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002075 }
2076
2077 drm_kms_helper_poll_enable(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002078
2079 /*
2080 * Most of the connector probing functions try to acquire runtime pm
2081 * refs to ensure that the GPU is powered on when connector polling is
2082 * performed. Since we're calling this from a runtime PM callback,
2083 * trying to acquire rpm refs will cause us to deadlock.
2084 *
2085 * Since we're guaranteed to be holding the rpm lock, it's safe to
2086 * temporarily disable the rpm helpers so this doesn't deadlock us.
2087 */
2088#ifdef CONFIG_PM
2089 dev->dev->power.disable_depth++;
2090#endif
Alex Deucher54fb2a52015-11-24 14:30:56 -05002091 drm_helper_hpd_irq_event(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002092#ifdef CONFIG_PM
2093 dev->dev->power.disable_depth--;
2094#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002095
2096 if (fbcon) {
2097 amdgpu_fbdev_set_suspend(adev, 0);
2098 console_unlock();
2099 }
2100
2101 return 0;
2102}
2103
Chunming Zhou63fbf422016-07-15 11:19:20 +08002104static bool amdgpu_check_soft_reset(struct amdgpu_device *adev)
2105{
2106 int i;
2107 bool asic_hang = false;
2108
2109 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002110 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou63fbf422016-07-15 11:19:20 +08002111 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002112 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
2113 adev->ip_blocks[i].status.hang =
2114 adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
2115 if (adev->ip_blocks[i].status.hang) {
2116 DRM_INFO("IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
Chunming Zhou63fbf422016-07-15 11:19:20 +08002117 asic_hang = true;
2118 }
2119 }
2120 return asic_hang;
2121}
2122
Baoyou Xie4d446652016-09-18 22:09:35 +08002123static int amdgpu_pre_soft_reset(struct amdgpu_device *adev)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002124{
2125 int i, r = 0;
2126
2127 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002128 if (!adev->ip_blocks[i].status.valid)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002129 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002130 if (adev->ip_blocks[i].status.hang &&
2131 adev->ip_blocks[i].version->funcs->pre_soft_reset) {
2132 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
Chunming Zhoud31a5012016-07-18 10:04:34 +08002133 if (r)
2134 return r;
2135 }
2136 }
2137
2138 return 0;
2139}
2140
Chunming Zhou35d782f2016-07-15 15:57:13 +08002141static bool amdgpu_need_full_reset(struct amdgpu_device *adev)
2142{
Alex Deucherda146d32016-10-13 16:07:03 -04002143 int i;
2144
2145 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002146 if (!adev->ip_blocks[i].status.valid)
Alex Deucherda146d32016-10-13 16:07:03 -04002147 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002148 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
2149 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
2150 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
2151 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE)) {
2152 if (adev->ip_blocks[i].status.hang) {
Alex Deucherda146d32016-10-13 16:07:03 -04002153 DRM_INFO("Some block need full reset!\n");
2154 return true;
2155 }
2156 }
Chunming Zhou35d782f2016-07-15 15:57:13 +08002157 }
2158 return false;
2159}
2160
2161static int amdgpu_soft_reset(struct amdgpu_device *adev)
2162{
2163 int i, r = 0;
2164
2165 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002166 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002167 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002168 if (adev->ip_blocks[i].status.hang &&
2169 adev->ip_blocks[i].version->funcs->soft_reset) {
2170 r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002171 if (r)
2172 return r;
2173 }
2174 }
2175
2176 return 0;
2177}
2178
2179static int amdgpu_post_soft_reset(struct amdgpu_device *adev)
2180{
2181 int i, r = 0;
2182
2183 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002184 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002185 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002186 if (adev->ip_blocks[i].status.hang &&
2187 adev->ip_blocks[i].version->funcs->post_soft_reset)
2188 r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002189 if (r)
2190 return r;
2191 }
2192
2193 return 0;
2194}
2195
Chunming Zhou3ad81f12016-08-05 17:30:17 +08002196bool amdgpu_need_backup(struct amdgpu_device *adev)
2197{
2198 if (adev->flags & AMD_IS_APU)
2199 return false;
2200
2201 return amdgpu_lockup_timeout > 0 ? true : false;
2202}
2203
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002204static int amdgpu_recover_vram_from_shadow(struct amdgpu_device *adev,
2205 struct amdgpu_ring *ring,
2206 struct amdgpu_bo *bo,
Chris Wilsonf54d1862016-10-25 13:00:45 +01002207 struct dma_fence **fence)
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002208{
2209 uint32_t domain;
2210 int r;
2211
2212 if (!bo->shadow)
2213 return 0;
2214
2215 r = amdgpu_bo_reserve(bo, false);
2216 if (r)
2217 return r;
2218 domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
2219 /* if bo has been evicted, then no need to recover */
2220 if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
2221 r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
2222 NULL, fence, true);
2223 if (r) {
2224 DRM_ERROR("recover page table failed!\n");
2225 goto err;
2226 }
2227 }
2228err:
2229 amdgpu_bo_unreserve(bo);
2230 return r;
2231}
2232
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002233/**
2234 * amdgpu_gpu_reset - reset the asic
2235 *
2236 * @adev: amdgpu device pointer
2237 *
2238 * Attempt the reset the GPU if it has hung (all asics).
2239 * Returns 0 for success or an error on failure.
2240 */
2241int amdgpu_gpu_reset(struct amdgpu_device *adev)
2242{
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002243 int i, r;
2244 int resched;
Chunming Zhou35d782f2016-07-15 15:57:13 +08002245 bool need_full_reset;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002246
Chunming Zhou63fbf422016-07-15 11:19:20 +08002247 if (!amdgpu_check_soft_reset(adev)) {
2248 DRM_INFO("No hardware hang detected. Did some blocks stall?\n");
2249 return 0;
2250 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002251
Marek Olšákd94aed52015-05-05 21:13:49 +02002252 atomic_inc(&adev->gpu_reset_counter);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002253
Chunming Zhoua3c47d62016-06-30 16:44:41 +08002254 /* block TTM */
2255 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
2256
Chunming Zhou0875dc92016-06-12 15:41:58 +08002257 /* block scheduler */
2258 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2259 struct amdgpu_ring *ring = adev->rings[i];
2260
2261 if (!ring)
2262 continue;
2263 kthread_park(ring->sched.thread);
Chunming Zhouaa1c8902016-06-30 13:56:02 +08002264 amd_sched_hw_job_reset(&ring->sched);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002265 }
Chunming Zhou2200eda2016-06-30 16:53:02 +08002266 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
2267 amdgpu_fence_driver_force_completion(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002268
Chunming Zhou35d782f2016-07-15 15:57:13 +08002269 need_full_reset = amdgpu_need_full_reset(adev);
2270
2271 if (!need_full_reset) {
2272 amdgpu_pre_soft_reset(adev);
2273 r = amdgpu_soft_reset(adev);
2274 amdgpu_post_soft_reset(adev);
2275 if (r || amdgpu_check_soft_reset(adev)) {
2276 DRM_INFO("soft reset failed, will fallback to full reset!\n");
2277 need_full_reset = true;
2278 }
2279 }
2280
2281 if (need_full_reset) {
Chunming Zhou35d782f2016-07-15 15:57:13 +08002282 r = amdgpu_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002283
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002284retry:
Chunming Zhou35d782f2016-07-15 15:57:13 +08002285 /* Disable fb access */
2286 if (adev->mode_info.num_crtc) {
2287 struct amdgpu_mode_mc_save save;
2288 amdgpu_display_stop_mc_access(adev, &save);
2289 amdgpu_wait_for_idle(adev, AMD_IP_BLOCK_TYPE_GMC);
2290 }
Alex Deuchere695e772016-10-19 14:40:58 -04002291 amdgpu_atombios_scratch_regs_save(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002292 r = amdgpu_asic_reset(adev);
Alex Deuchere695e772016-10-19 14:40:58 -04002293 amdgpu_atombios_scratch_regs_restore(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002294 /* post card */
2295 amdgpu_atom_asic_init(adev->mode_info.atom_context);
Alex Deucherbfa99262016-01-15 11:59:48 -05002296
Chunming Zhou35d782f2016-07-15 15:57:13 +08002297 if (!r) {
2298 dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
2299 r = amdgpu_resume(adev);
2300 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002301 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002302 if (!r) {
Chunming Zhoue72cfd52016-07-27 13:15:20 +08002303 amdgpu_irq_gpu_reset_resume_helper(adev);
Chunming Zhou2c0d7312016-08-30 16:36:25 +08002304 if (need_full_reset && amdgpu_need_backup(adev)) {
2305 r = amdgpu_ttm_recover_gart(adev);
2306 if (r)
2307 DRM_ERROR("gart recovery failed!!!\n");
2308 }
Chunming Zhou1f465082016-06-30 15:02:26 +08002309 r = amdgpu_ib_ring_tests(adev);
2310 if (r) {
2311 dev_err(adev->dev, "ib ring test failed (%d).\n", r);
Chunming Zhou40019dc2016-06-29 16:01:49 +08002312 r = amdgpu_suspend(adev);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002313 need_full_reset = true;
Chunming Zhou40019dc2016-06-29 16:01:49 +08002314 goto retry;
Chunming Zhou1f465082016-06-30 15:02:26 +08002315 }
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002316 /**
2317 * recovery vm page tables, since we cannot depend on VRAM is
2318 * consistent after gpu full reset.
2319 */
2320 if (need_full_reset && amdgpu_need_backup(adev)) {
2321 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
2322 struct amdgpu_bo *bo, *tmp;
Chris Wilsonf54d1862016-10-25 13:00:45 +01002323 struct dma_fence *fence = NULL, *next = NULL;
Chunming Zhou1f465082016-06-30 15:02:26 +08002324
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002325 DRM_INFO("recover vram bo from shadow\n");
2326 mutex_lock(&adev->shadow_list_lock);
2327 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
2328 amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
2329 if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01002330 r = dma_fence_wait(fence, false);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002331 if (r) {
2332 WARN(r, "recovery from shadow isn't comleted\n");
2333 break;
2334 }
2335 }
2336
Chris Wilsonf54d1862016-10-25 13:00:45 +01002337 dma_fence_put(fence);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002338 fence = next;
2339 }
2340 mutex_unlock(&adev->shadow_list_lock);
2341 if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01002342 r = dma_fence_wait(fence, false);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002343 if (r)
2344 WARN(r, "recovery from shadow isn't comleted\n");
2345 }
Chris Wilsonf54d1862016-10-25 13:00:45 +01002346 dma_fence_put(fence);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002347 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002348 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2349 struct amdgpu_ring *ring = adev->rings[i];
2350 if (!ring)
2351 continue;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002352
Chunming Zhouaa1c8902016-06-30 13:56:02 +08002353 amd_sched_job_recovery(&ring->sched);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002354 kthread_unpark(ring->sched.thread);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002355 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002356 } else {
Chunming Zhou2200eda2016-06-30 16:53:02 +08002357 dev_err(adev->dev, "asic resume failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002358 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
Chunming Zhou0875dc92016-06-12 15:41:58 +08002359 if (adev->rings[i]) {
2360 kthread_unpark(adev->rings[i]->sched.thread);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002361 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002362 }
2363 }
2364
2365 drm_helper_resume_force_mode(adev->ddev);
2366
2367 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
2368 if (r) {
2369 /* bad news, how to tell it to userspace ? */
2370 dev_info(adev->dev, "GPU reset failed\n");
2371 }
2372
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002373 return r;
2374}
2375
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002376void amdgpu_get_pcie_info(struct amdgpu_device *adev)
2377{
2378 u32 mask;
2379 int ret;
2380
Alex Deuchercd474ba2016-02-04 10:21:23 -05002381 if (amdgpu_pcie_gen_cap)
2382 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
2383
2384 if (amdgpu_pcie_lane_cap)
2385 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
2386
2387 /* covers APUs as well */
2388 if (pci_is_root_bus(adev->pdev->bus)) {
2389 if (adev->pm.pcie_gen_mask == 0)
2390 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
2391 if (adev->pm.pcie_mlw_mask == 0)
2392 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002393 return;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002394 }
Alex Deuchercd474ba2016-02-04 10:21:23 -05002395
2396 if (adev->pm.pcie_gen_mask == 0) {
2397 ret = drm_pcie_get_speed_cap_mask(adev->ddev, &mask);
2398 if (!ret) {
2399 adev->pm.pcie_gen_mask = (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
2400 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
2401 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
2402
2403 if (mask & DRM_PCIE_SPEED_25)
2404 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
2405 if (mask & DRM_PCIE_SPEED_50)
2406 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2;
2407 if (mask & DRM_PCIE_SPEED_80)
2408 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3;
2409 } else {
2410 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
2411 }
2412 }
2413 if (adev->pm.pcie_mlw_mask == 0) {
2414 ret = drm_pcie_get_max_link_width(adev->ddev, &mask);
2415 if (!ret) {
2416 switch (mask) {
2417 case 32:
2418 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
2419 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
2420 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2421 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2422 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2423 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2424 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2425 break;
2426 case 16:
2427 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
2428 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2429 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2430 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2431 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2432 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2433 break;
2434 case 12:
2435 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2436 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2437 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2438 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2439 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2440 break;
2441 case 8:
2442 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2443 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2444 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2445 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2446 break;
2447 case 4:
2448 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2449 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2450 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2451 break;
2452 case 2:
2453 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2454 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2455 break;
2456 case 1:
2457 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
2458 break;
2459 default:
2460 break;
2461 }
2462 } else {
2463 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002464 }
2465 }
2466}
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002467
2468/*
2469 * Debugfs
2470 */
2471int amdgpu_debugfs_add_files(struct amdgpu_device *adev,
Nils Wallménius06ab6832016-05-02 12:46:15 -04002472 const struct drm_info_list *files,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002473 unsigned nfiles)
2474{
2475 unsigned i;
2476
2477 for (i = 0; i < adev->debugfs_count; i++) {
2478 if (adev->debugfs[i].files == files) {
2479 /* Already registered */
2480 return 0;
2481 }
2482 }
2483
2484 i = adev->debugfs_count + 1;
2485 if (i > AMDGPU_DEBUGFS_MAX_COMPONENTS) {
2486 DRM_ERROR("Reached maximum number of debugfs components.\n");
2487 DRM_ERROR("Report so we increase "
2488 "AMDGPU_DEBUGFS_MAX_COMPONENTS.\n");
2489 return -EINVAL;
2490 }
2491 adev->debugfs[adev->debugfs_count].files = files;
2492 adev->debugfs[adev->debugfs_count].num_files = nfiles;
2493 adev->debugfs_count = i;
2494#if defined(CONFIG_DEBUG_FS)
2495 drm_debugfs_create_files(files, nfiles,
2496 adev->ddev->control->debugfs_root,
2497 adev->ddev->control);
2498 drm_debugfs_create_files(files, nfiles,
2499 adev->ddev->primary->debugfs_root,
2500 adev->ddev->primary);
2501#endif
2502 return 0;
2503}
2504
2505static void amdgpu_debugfs_remove_files(struct amdgpu_device *adev)
2506{
2507#if defined(CONFIG_DEBUG_FS)
2508 unsigned i;
2509
2510 for (i = 0; i < adev->debugfs_count; i++) {
2511 drm_debugfs_remove_files(adev->debugfs[i].files,
2512 adev->debugfs[i].num_files,
2513 adev->ddev->control);
2514 drm_debugfs_remove_files(adev->debugfs[i].files,
2515 adev->debugfs[i].num_files,
2516 adev->ddev->primary);
2517 }
2518#endif
2519}
2520
2521#if defined(CONFIG_DEBUG_FS)
2522
2523static ssize_t amdgpu_debugfs_regs_read(struct file *f, char __user *buf,
2524 size_t size, loff_t *pos)
2525{
2526 struct amdgpu_device *adev = f->f_inode->i_private;
2527 ssize_t result = 0;
2528 int r;
Tom St Denisbd122672016-07-28 09:39:22 -04002529 bool pm_pg_lock, use_bank;
Tom St Denis566281592016-06-27 11:55:07 -04002530 unsigned instance_bank, sh_bank, se_bank;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002531
2532 if (size & 0x3 || *pos & 0x3)
2533 return -EINVAL;
2534
Tom St Denisbd122672016-07-28 09:39:22 -04002535 /* are we reading registers for which a PG lock is necessary? */
2536 pm_pg_lock = (*pos >> 23) & 1;
2537
Tom St Denis566281592016-06-27 11:55:07 -04002538 if (*pos & (1ULL << 62)) {
2539 se_bank = (*pos >> 24) & 0x3FF;
2540 sh_bank = (*pos >> 34) & 0x3FF;
2541 instance_bank = (*pos >> 44) & 0x3FF;
Tom St Denis32977f92016-10-09 07:41:26 -04002542
2543 if (se_bank == 0x3FF)
2544 se_bank = 0xFFFFFFFF;
2545 if (sh_bank == 0x3FF)
2546 sh_bank = 0xFFFFFFFF;
2547 if (instance_bank == 0x3FF)
2548 instance_bank = 0xFFFFFFFF;
Tom St Denis566281592016-06-27 11:55:07 -04002549 use_bank = 1;
Tom St Denis566281592016-06-27 11:55:07 -04002550 } else {
2551 use_bank = 0;
2552 }
2553
Tom St Denisbd122672016-07-28 09:39:22 -04002554 *pos &= 0x3FFFF;
2555
Tom St Denis566281592016-06-27 11:55:07 -04002556 if (use_bank) {
Tom St Denis32977f92016-10-09 07:41:26 -04002557 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
2558 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
Tom St Denis566281592016-06-27 11:55:07 -04002559 return -EINVAL;
2560 mutex_lock(&adev->grbm_idx_mutex);
2561 amdgpu_gfx_select_se_sh(adev, se_bank,
2562 sh_bank, instance_bank);
2563 }
2564
Tom St Denisbd122672016-07-28 09:39:22 -04002565 if (pm_pg_lock)
2566 mutex_lock(&adev->pm.mutex);
2567
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002568 while (size) {
2569 uint32_t value;
2570
2571 if (*pos > adev->rmmio_size)
Tom St Denis566281592016-06-27 11:55:07 -04002572 goto end;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002573
2574 value = RREG32(*pos >> 2);
2575 r = put_user(value, (uint32_t *)buf);
Tom St Denis566281592016-06-27 11:55:07 -04002576 if (r) {
2577 result = r;
2578 goto end;
2579 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002580
2581 result += 4;
2582 buf += 4;
2583 *pos += 4;
2584 size -= 4;
2585 }
2586
Tom St Denis566281592016-06-27 11:55:07 -04002587end:
2588 if (use_bank) {
2589 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
2590 mutex_unlock(&adev->grbm_idx_mutex);
2591 }
2592
Tom St Denisbd122672016-07-28 09:39:22 -04002593 if (pm_pg_lock)
2594 mutex_unlock(&adev->pm.mutex);
2595
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002596 return result;
2597}
2598
2599static ssize_t amdgpu_debugfs_regs_write(struct file *f, const char __user *buf,
2600 size_t size, loff_t *pos)
2601{
2602 struct amdgpu_device *adev = f->f_inode->i_private;
2603 ssize_t result = 0;
2604 int r;
Tom St Denis394fdde2016-10-10 07:31:23 -04002605 bool pm_pg_lock, use_bank;
2606 unsigned instance_bank, sh_bank, se_bank;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002607
2608 if (size & 0x3 || *pos & 0x3)
2609 return -EINVAL;
2610
Tom St Denis394fdde2016-10-10 07:31:23 -04002611 /* are we reading registers for which a PG lock is necessary? */
2612 pm_pg_lock = (*pos >> 23) & 1;
2613
2614 if (*pos & (1ULL << 62)) {
2615 se_bank = (*pos >> 24) & 0x3FF;
2616 sh_bank = (*pos >> 34) & 0x3FF;
2617 instance_bank = (*pos >> 44) & 0x3FF;
2618
2619 if (se_bank == 0x3FF)
2620 se_bank = 0xFFFFFFFF;
2621 if (sh_bank == 0x3FF)
2622 sh_bank = 0xFFFFFFFF;
2623 if (instance_bank == 0x3FF)
2624 instance_bank = 0xFFFFFFFF;
2625 use_bank = 1;
2626 } else {
2627 use_bank = 0;
2628 }
2629
2630 *pos &= 0x3FFFF;
2631
2632 if (use_bank) {
2633 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
2634 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
2635 return -EINVAL;
2636 mutex_lock(&adev->grbm_idx_mutex);
2637 amdgpu_gfx_select_se_sh(adev, se_bank,
2638 sh_bank, instance_bank);
2639 }
2640
2641 if (pm_pg_lock)
2642 mutex_lock(&adev->pm.mutex);
2643
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002644 while (size) {
2645 uint32_t value;
2646
2647 if (*pos > adev->rmmio_size)
2648 return result;
2649
2650 r = get_user(value, (uint32_t *)buf);
2651 if (r)
2652 return r;
2653
2654 WREG32(*pos >> 2, value);
2655
2656 result += 4;
2657 buf += 4;
2658 *pos += 4;
2659 size -= 4;
2660 }
2661
Tom St Denis394fdde2016-10-10 07:31:23 -04002662 if (use_bank) {
2663 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
2664 mutex_unlock(&adev->grbm_idx_mutex);
2665 }
2666
2667 if (pm_pg_lock)
2668 mutex_unlock(&adev->pm.mutex);
2669
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002670 return result;
2671}
2672
Tom St Denisadcec282016-04-15 13:08:44 -04002673static ssize_t amdgpu_debugfs_regs_pcie_read(struct file *f, char __user *buf,
2674 size_t size, loff_t *pos)
2675{
2676 struct amdgpu_device *adev = f->f_inode->i_private;
2677 ssize_t result = 0;
2678 int r;
2679
2680 if (size & 0x3 || *pos & 0x3)
2681 return -EINVAL;
2682
2683 while (size) {
2684 uint32_t value;
2685
2686 value = RREG32_PCIE(*pos >> 2);
2687 r = put_user(value, (uint32_t *)buf);
2688 if (r)
2689 return r;
2690
2691 result += 4;
2692 buf += 4;
2693 *pos += 4;
2694 size -= 4;
2695 }
2696
2697 return result;
2698}
2699
2700static ssize_t amdgpu_debugfs_regs_pcie_write(struct file *f, const char __user *buf,
2701 size_t size, loff_t *pos)
2702{
2703 struct amdgpu_device *adev = f->f_inode->i_private;
2704 ssize_t result = 0;
2705 int r;
2706
2707 if (size & 0x3 || *pos & 0x3)
2708 return -EINVAL;
2709
2710 while (size) {
2711 uint32_t value;
2712
2713 r = get_user(value, (uint32_t *)buf);
2714 if (r)
2715 return r;
2716
2717 WREG32_PCIE(*pos >> 2, value);
2718
2719 result += 4;
2720 buf += 4;
2721 *pos += 4;
2722 size -= 4;
2723 }
2724
2725 return result;
2726}
2727
2728static ssize_t amdgpu_debugfs_regs_didt_read(struct file *f, char __user *buf,
2729 size_t size, loff_t *pos)
2730{
2731 struct amdgpu_device *adev = f->f_inode->i_private;
2732 ssize_t result = 0;
2733 int r;
2734
2735 if (size & 0x3 || *pos & 0x3)
2736 return -EINVAL;
2737
2738 while (size) {
2739 uint32_t value;
2740
2741 value = RREG32_DIDT(*pos >> 2);
2742 r = put_user(value, (uint32_t *)buf);
2743 if (r)
2744 return r;
2745
2746 result += 4;
2747 buf += 4;
2748 *pos += 4;
2749 size -= 4;
2750 }
2751
2752 return result;
2753}
2754
2755static ssize_t amdgpu_debugfs_regs_didt_write(struct file *f, const char __user *buf,
2756 size_t size, loff_t *pos)
2757{
2758 struct amdgpu_device *adev = f->f_inode->i_private;
2759 ssize_t result = 0;
2760 int r;
2761
2762 if (size & 0x3 || *pos & 0x3)
2763 return -EINVAL;
2764
2765 while (size) {
2766 uint32_t value;
2767
2768 r = get_user(value, (uint32_t *)buf);
2769 if (r)
2770 return r;
2771
2772 WREG32_DIDT(*pos >> 2, value);
2773
2774 result += 4;
2775 buf += 4;
2776 *pos += 4;
2777 size -= 4;
2778 }
2779
2780 return result;
2781}
2782
2783static ssize_t amdgpu_debugfs_regs_smc_read(struct file *f, char __user *buf,
2784 size_t size, loff_t *pos)
2785{
2786 struct amdgpu_device *adev = f->f_inode->i_private;
2787 ssize_t result = 0;
2788 int r;
2789
2790 if (size & 0x3 || *pos & 0x3)
2791 return -EINVAL;
2792
2793 while (size) {
2794 uint32_t value;
2795
Tom St Denis6fc0dea2016-08-29 08:39:29 -04002796 value = RREG32_SMC(*pos);
Tom St Denisadcec282016-04-15 13:08:44 -04002797 r = put_user(value, (uint32_t *)buf);
2798 if (r)
2799 return r;
2800
2801 result += 4;
2802 buf += 4;
2803 *pos += 4;
2804 size -= 4;
2805 }
2806
2807 return result;
2808}
2809
2810static ssize_t amdgpu_debugfs_regs_smc_write(struct file *f, const char __user *buf,
2811 size_t size, loff_t *pos)
2812{
2813 struct amdgpu_device *adev = f->f_inode->i_private;
2814 ssize_t result = 0;
2815 int r;
2816
2817 if (size & 0x3 || *pos & 0x3)
2818 return -EINVAL;
2819
2820 while (size) {
2821 uint32_t value;
2822
2823 r = get_user(value, (uint32_t *)buf);
2824 if (r)
2825 return r;
2826
Tom St Denis6fc0dea2016-08-29 08:39:29 -04002827 WREG32_SMC(*pos, value);
Tom St Denisadcec282016-04-15 13:08:44 -04002828
2829 result += 4;
2830 buf += 4;
2831 *pos += 4;
2832 size -= 4;
2833 }
2834
2835 return result;
2836}
2837
Tom St Denis1e051412016-06-27 09:57:18 -04002838static ssize_t amdgpu_debugfs_gca_config_read(struct file *f, char __user *buf,
2839 size_t size, loff_t *pos)
2840{
2841 struct amdgpu_device *adev = f->f_inode->i_private;
2842 ssize_t result = 0;
2843 int r;
2844 uint32_t *config, no_regs = 0;
2845
2846 if (size & 0x3 || *pos & 0x3)
2847 return -EINVAL;
2848
Markus Elfringecab7662016-09-18 17:00:52 +02002849 config = kmalloc_array(256, sizeof(*config), GFP_KERNEL);
Tom St Denis1e051412016-06-27 09:57:18 -04002850 if (!config)
2851 return -ENOMEM;
2852
2853 /* version, increment each time something is added */
Tom St Denise9f11dc2016-08-17 12:00:51 -04002854 config[no_regs++] = 2;
Tom St Denis1e051412016-06-27 09:57:18 -04002855 config[no_regs++] = adev->gfx.config.max_shader_engines;
2856 config[no_regs++] = adev->gfx.config.max_tile_pipes;
2857 config[no_regs++] = adev->gfx.config.max_cu_per_sh;
2858 config[no_regs++] = adev->gfx.config.max_sh_per_se;
2859 config[no_regs++] = adev->gfx.config.max_backends_per_se;
2860 config[no_regs++] = adev->gfx.config.max_texture_channel_caches;
2861 config[no_regs++] = adev->gfx.config.max_gprs;
2862 config[no_regs++] = adev->gfx.config.max_gs_threads;
2863 config[no_regs++] = adev->gfx.config.max_hw_contexts;
2864 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_frontend;
2865 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_backend;
2866 config[no_regs++] = adev->gfx.config.sc_hiz_tile_fifo_size;
2867 config[no_regs++] = adev->gfx.config.sc_earlyz_tile_fifo_size;
2868 config[no_regs++] = adev->gfx.config.num_tile_pipes;
2869 config[no_regs++] = adev->gfx.config.backend_enable_mask;
2870 config[no_regs++] = adev->gfx.config.mem_max_burst_length_bytes;
2871 config[no_regs++] = adev->gfx.config.mem_row_size_in_kb;
2872 config[no_regs++] = adev->gfx.config.shader_engine_tile_size;
2873 config[no_regs++] = adev->gfx.config.num_gpus;
2874 config[no_regs++] = adev->gfx.config.multi_gpu_tile_size;
2875 config[no_regs++] = adev->gfx.config.mc_arb_ramcfg;
2876 config[no_regs++] = adev->gfx.config.gb_addr_config;
2877 config[no_regs++] = adev->gfx.config.num_rbs;
2878
Tom St Denis89a8f302016-08-12 15:14:31 -04002879 /* rev==1 */
2880 config[no_regs++] = adev->rev_id;
2881 config[no_regs++] = adev->pg_flags;
2882 config[no_regs++] = adev->cg_flags;
2883
Tom St Denise9f11dc2016-08-17 12:00:51 -04002884 /* rev==2 */
2885 config[no_regs++] = adev->family;
2886 config[no_regs++] = adev->external_rev_id;
2887
Tom St Denis1e051412016-06-27 09:57:18 -04002888 while (size && (*pos < no_regs * 4)) {
2889 uint32_t value;
2890
2891 value = config[*pos >> 2];
2892 r = put_user(value, (uint32_t *)buf);
2893 if (r) {
2894 kfree(config);
2895 return r;
2896 }
2897
2898 result += 4;
2899 buf += 4;
2900 *pos += 4;
2901 size -= 4;
2902 }
2903
2904 kfree(config);
2905 return result;
2906}
2907
Tom St Denisf2cdaf22016-09-15 10:08:44 -04002908static ssize_t amdgpu_debugfs_sensor_read(struct file *f, char __user *buf,
2909 size_t size, loff_t *pos)
2910{
2911 struct amdgpu_device *adev = f->f_inode->i_private;
2912 int idx, r;
2913 int32_t value;
2914
2915 if (size != 4 || *pos & 0x3)
2916 return -EINVAL;
2917
2918 /* convert offset to sensor number */
2919 idx = *pos >> 2;
2920
2921 if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->read_sensor)
2922 r = adev->powerplay.pp_funcs->read_sensor(adev->powerplay.pp_handle, idx, &value);
2923 else
2924 return -EINVAL;
2925
2926 if (!r)
2927 r = put_user(value, (int32_t *)buf);
2928
2929 return !r ? 4 : r;
2930}
Tom St Denis1e051412016-06-27 09:57:18 -04002931
Tom St Denis273d7aa2016-10-11 14:48:55 -04002932static ssize_t amdgpu_debugfs_wave_read(struct file *f, char __user *buf,
2933 size_t size, loff_t *pos)
2934{
2935 struct amdgpu_device *adev = f->f_inode->i_private;
2936 int r, x;
2937 ssize_t result=0;
Tom St Denis472259f2016-10-14 09:49:09 -04002938 uint32_t offset, se, sh, cu, wave, simd, data[32];
Tom St Denis273d7aa2016-10-11 14:48:55 -04002939
2940 if (size & 3 || *pos & 3)
2941 return -EINVAL;
2942
2943 /* decode offset */
2944 offset = (*pos & 0x7F);
2945 se = ((*pos >> 7) & 0xFF);
2946 sh = ((*pos >> 15) & 0xFF);
2947 cu = ((*pos >> 23) & 0xFF);
2948 wave = ((*pos >> 31) & 0xFF);
2949 simd = ((*pos >> 37) & 0xFF);
Tom St Denis273d7aa2016-10-11 14:48:55 -04002950
2951 /* switch to the specific se/sh/cu */
2952 mutex_lock(&adev->grbm_idx_mutex);
2953 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
2954
2955 x = 0;
Tom St Denis472259f2016-10-14 09:49:09 -04002956 if (adev->gfx.funcs->read_wave_data)
2957 adev->gfx.funcs->read_wave_data(adev, simd, wave, data, &x);
Tom St Denis273d7aa2016-10-11 14:48:55 -04002958
2959 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
2960 mutex_unlock(&adev->grbm_idx_mutex);
2961
Tom St Denis5ecfb3b2016-10-13 12:15:03 -04002962 if (!x)
2963 return -EINVAL;
2964
Tom St Denis472259f2016-10-14 09:49:09 -04002965 while (size && (offset < x * 4)) {
Tom St Denis273d7aa2016-10-11 14:48:55 -04002966 uint32_t value;
2967
Tom St Denis472259f2016-10-14 09:49:09 -04002968 value = data[offset >> 2];
Tom St Denis273d7aa2016-10-11 14:48:55 -04002969 r = put_user(value, (uint32_t *)buf);
2970 if (r)
2971 return r;
2972
2973 result += 4;
2974 buf += 4;
Tom St Denis472259f2016-10-14 09:49:09 -04002975 offset += 4;
Tom St Denis273d7aa2016-10-11 14:48:55 -04002976 size -= 4;
2977 }
2978
2979 return result;
2980}
2981
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002982static const struct file_operations amdgpu_debugfs_regs_fops = {
2983 .owner = THIS_MODULE,
2984 .read = amdgpu_debugfs_regs_read,
2985 .write = amdgpu_debugfs_regs_write,
2986 .llseek = default_llseek
2987};
Tom St Denisadcec282016-04-15 13:08:44 -04002988static const struct file_operations amdgpu_debugfs_regs_didt_fops = {
2989 .owner = THIS_MODULE,
2990 .read = amdgpu_debugfs_regs_didt_read,
2991 .write = amdgpu_debugfs_regs_didt_write,
2992 .llseek = default_llseek
2993};
2994static const struct file_operations amdgpu_debugfs_regs_pcie_fops = {
2995 .owner = THIS_MODULE,
2996 .read = amdgpu_debugfs_regs_pcie_read,
2997 .write = amdgpu_debugfs_regs_pcie_write,
2998 .llseek = default_llseek
2999};
3000static const struct file_operations amdgpu_debugfs_regs_smc_fops = {
3001 .owner = THIS_MODULE,
3002 .read = amdgpu_debugfs_regs_smc_read,
3003 .write = amdgpu_debugfs_regs_smc_write,
3004 .llseek = default_llseek
3005};
3006
Tom St Denis1e051412016-06-27 09:57:18 -04003007static const struct file_operations amdgpu_debugfs_gca_config_fops = {
3008 .owner = THIS_MODULE,
3009 .read = amdgpu_debugfs_gca_config_read,
3010 .llseek = default_llseek
3011};
3012
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003013static const struct file_operations amdgpu_debugfs_sensors_fops = {
3014 .owner = THIS_MODULE,
3015 .read = amdgpu_debugfs_sensor_read,
3016 .llseek = default_llseek
3017};
3018
Tom St Denis273d7aa2016-10-11 14:48:55 -04003019static const struct file_operations amdgpu_debugfs_wave_fops = {
3020 .owner = THIS_MODULE,
3021 .read = amdgpu_debugfs_wave_read,
3022 .llseek = default_llseek
3023};
3024
Tom St Denisadcec282016-04-15 13:08:44 -04003025static const struct file_operations *debugfs_regs[] = {
3026 &amdgpu_debugfs_regs_fops,
3027 &amdgpu_debugfs_regs_didt_fops,
3028 &amdgpu_debugfs_regs_pcie_fops,
3029 &amdgpu_debugfs_regs_smc_fops,
Tom St Denis1e051412016-06-27 09:57:18 -04003030 &amdgpu_debugfs_gca_config_fops,
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003031 &amdgpu_debugfs_sensors_fops,
Tom St Denis273d7aa2016-10-11 14:48:55 -04003032 &amdgpu_debugfs_wave_fops,
Tom St Denisadcec282016-04-15 13:08:44 -04003033};
3034
3035static const char *debugfs_regs_names[] = {
3036 "amdgpu_regs",
3037 "amdgpu_regs_didt",
3038 "amdgpu_regs_pcie",
3039 "amdgpu_regs_smc",
Tom St Denis1e051412016-06-27 09:57:18 -04003040 "amdgpu_gca_config",
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003041 "amdgpu_sensors",
Tom St Denis273d7aa2016-10-11 14:48:55 -04003042 "amdgpu_wave",
Tom St Denisadcec282016-04-15 13:08:44 -04003043};
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003044
3045static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
3046{
3047 struct drm_minor *minor = adev->ddev->primary;
3048 struct dentry *ent, *root = minor->debugfs_root;
Tom St Denisadcec282016-04-15 13:08:44 -04003049 unsigned i, j;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003050
Tom St Denisadcec282016-04-15 13:08:44 -04003051 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3052 ent = debugfs_create_file(debugfs_regs_names[i],
3053 S_IFREG | S_IRUGO, root,
3054 adev, debugfs_regs[i]);
3055 if (IS_ERR(ent)) {
3056 for (j = 0; j < i; j++) {
3057 debugfs_remove(adev->debugfs_regs[i]);
3058 adev->debugfs_regs[i] = NULL;
3059 }
3060 return PTR_ERR(ent);
3061 }
3062
3063 if (!i)
3064 i_size_write(ent->d_inode, adev->rmmio_size);
3065 adev->debugfs_regs[i] = ent;
3066 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003067
3068 return 0;
3069}
3070
3071static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev)
3072{
Tom St Denisadcec282016-04-15 13:08:44 -04003073 unsigned i;
3074
3075 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3076 if (adev->debugfs_regs[i]) {
3077 debugfs_remove(adev->debugfs_regs[i]);
3078 adev->debugfs_regs[i] = NULL;
3079 }
3080 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003081}
3082
3083int amdgpu_debugfs_init(struct drm_minor *minor)
3084{
3085 return 0;
3086}
3087
3088void amdgpu_debugfs_cleanup(struct drm_minor *minor)
3089{
3090}
Alexander Kuleshov7cebc722015-06-27 13:16:05 +06003091#else
3092static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
3093{
3094 return 0;
3095}
3096static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev) { }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003097#endif