blob: 15afe22ca8909ddccb37b15e1f42c7bee0456acf [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,
267 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED,
Christian König72d76682015-09-03 17:34:59 +0200268 NULL, NULL, &adev->vram_scratch.robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400269 if (r) {
270 return r;
271 }
272 }
273
274 r = amdgpu_bo_reserve(adev->vram_scratch.robj, false);
275 if (unlikely(r != 0))
276 return r;
277 r = amdgpu_bo_pin(adev->vram_scratch.robj,
278 AMDGPU_GEM_DOMAIN_VRAM, &adev->vram_scratch.gpu_addr);
279 if (r) {
280 amdgpu_bo_unreserve(adev->vram_scratch.robj);
281 return r;
282 }
283 r = amdgpu_bo_kmap(adev->vram_scratch.robj,
284 (void **)&adev->vram_scratch.ptr);
285 if (r)
286 amdgpu_bo_unpin(adev->vram_scratch.robj);
287 amdgpu_bo_unreserve(adev->vram_scratch.robj);
288
289 return r;
290}
291
292static void amdgpu_vram_scratch_fini(struct amdgpu_device *adev)
293{
294 int r;
295
296 if (adev->vram_scratch.robj == NULL) {
297 return;
298 }
299 r = amdgpu_bo_reserve(adev->vram_scratch.robj, false);
300 if (likely(r == 0)) {
301 amdgpu_bo_kunmap(adev->vram_scratch.robj);
302 amdgpu_bo_unpin(adev->vram_scratch.robj);
303 amdgpu_bo_unreserve(adev->vram_scratch.robj);
304 }
305 amdgpu_bo_unref(&adev->vram_scratch.robj);
306}
307
308/**
309 * amdgpu_program_register_sequence - program an array of registers.
310 *
311 * @adev: amdgpu_device pointer
312 * @registers: pointer to the register array
313 * @array_size: size of the register array
314 *
315 * Programs an array or registers with and and or masks.
316 * This is a helper for setting golden registers.
317 */
318void amdgpu_program_register_sequence(struct amdgpu_device *adev,
319 const u32 *registers,
320 const u32 array_size)
321{
322 u32 tmp, reg, and_mask, or_mask;
323 int i;
324
325 if (array_size % 3)
326 return;
327
328 for (i = 0; i < array_size; i +=3) {
329 reg = registers[i + 0];
330 and_mask = registers[i + 1];
331 or_mask = registers[i + 2];
332
333 if (and_mask == 0xffffffff) {
334 tmp = or_mask;
335 } else {
336 tmp = RREG32(reg);
337 tmp &= ~and_mask;
338 tmp |= or_mask;
339 }
340 WREG32(reg, tmp);
341 }
342}
343
344void amdgpu_pci_config_reset(struct amdgpu_device *adev)
345{
346 pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
347}
348
349/*
350 * GPU doorbell aperture helpers function.
351 */
352/**
353 * amdgpu_doorbell_init - Init doorbell driver information.
354 *
355 * @adev: amdgpu_device pointer
356 *
357 * Init doorbell driver information (CIK)
358 * Returns 0 on success, error on failure.
359 */
360static int amdgpu_doorbell_init(struct amdgpu_device *adev)
361{
362 /* doorbell bar mapping */
363 adev->doorbell.base = pci_resource_start(adev->pdev, 2);
364 adev->doorbell.size = pci_resource_len(adev->pdev, 2);
365
Christian Königedf600d2016-05-03 15:54:54 +0200366 adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400367 AMDGPU_DOORBELL_MAX_ASSIGNMENT+1);
368 if (adev->doorbell.num_doorbells == 0)
369 return -EINVAL;
370
371 adev->doorbell.ptr = ioremap(adev->doorbell.base, adev->doorbell.num_doorbells * sizeof(u32));
372 if (adev->doorbell.ptr == NULL) {
373 return -ENOMEM;
374 }
375 DRM_INFO("doorbell mmio base: 0x%08X\n", (uint32_t)adev->doorbell.base);
376 DRM_INFO("doorbell mmio size: %u\n", (unsigned)adev->doorbell.size);
377
378 return 0;
379}
380
381/**
382 * amdgpu_doorbell_fini - Tear down doorbell driver information.
383 *
384 * @adev: amdgpu_device pointer
385 *
386 * Tear down doorbell driver information (CIK)
387 */
388static void amdgpu_doorbell_fini(struct amdgpu_device *adev)
389{
390 iounmap(adev->doorbell.ptr);
391 adev->doorbell.ptr = NULL;
392}
393
394/**
395 * amdgpu_doorbell_get_kfd_info - Report doorbell configuration required to
396 * setup amdkfd
397 *
398 * @adev: amdgpu_device pointer
399 * @aperture_base: output returning doorbell aperture base physical address
400 * @aperture_size: output returning doorbell aperture size in bytes
401 * @start_offset: output returning # of doorbell bytes reserved for amdgpu.
402 *
403 * amdgpu and amdkfd share the doorbell aperture. amdgpu sets it up,
404 * takes doorbells required for its own rings and reports the setup to amdkfd.
405 * amdgpu reserved doorbells are at the start of the doorbell aperture.
406 */
407void amdgpu_doorbell_get_kfd_info(struct amdgpu_device *adev,
408 phys_addr_t *aperture_base,
409 size_t *aperture_size,
410 size_t *start_offset)
411{
412 /*
413 * The first num_doorbells are used by amdgpu.
414 * amdkfd takes whatever's left in the aperture.
415 */
416 if (adev->doorbell.size > adev->doorbell.num_doorbells * sizeof(u32)) {
417 *aperture_base = adev->doorbell.base;
418 *aperture_size = adev->doorbell.size;
419 *start_offset = adev->doorbell.num_doorbells * sizeof(u32);
420 } else {
421 *aperture_base = 0;
422 *aperture_size = 0;
423 *start_offset = 0;
424 }
425}
426
427/*
428 * amdgpu_wb_*()
429 * Writeback is the the method by which the the GPU updates special pages
430 * in memory with the status of certain GPU events (fences, ring pointers,
431 * etc.).
432 */
433
434/**
435 * amdgpu_wb_fini - Disable Writeback and free memory
436 *
437 * @adev: amdgpu_device pointer
438 *
439 * Disables Writeback and frees the Writeback memory (all asics).
440 * Used at driver shutdown.
441 */
442static void amdgpu_wb_fini(struct amdgpu_device *adev)
443{
444 if (adev->wb.wb_obj) {
445 if (!amdgpu_bo_reserve(adev->wb.wb_obj, false)) {
446 amdgpu_bo_kunmap(adev->wb.wb_obj);
447 amdgpu_bo_unpin(adev->wb.wb_obj);
448 amdgpu_bo_unreserve(adev->wb.wb_obj);
449 }
450 amdgpu_bo_unref(&adev->wb.wb_obj);
451 adev->wb.wb = NULL;
452 adev->wb.wb_obj = NULL;
453 }
454}
455
456/**
457 * amdgpu_wb_init- Init Writeback driver info and allocate memory
458 *
459 * @adev: amdgpu_device pointer
460 *
461 * Disables Writeback and frees the Writeback memory (all asics).
462 * Used at driver startup.
463 * Returns 0 on success or an -error on failure.
464 */
465static int amdgpu_wb_init(struct amdgpu_device *adev)
466{
467 int r;
468
469 if (adev->wb.wb_obj == NULL) {
470 r = amdgpu_bo_create(adev, AMDGPU_MAX_WB * 4, PAGE_SIZE, true,
Christian König72d76682015-09-03 17:34:59 +0200471 AMDGPU_GEM_DOMAIN_GTT, 0, NULL, NULL,
472 &adev->wb.wb_obj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400473 if (r) {
474 dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
475 return r;
476 }
477 r = amdgpu_bo_reserve(adev->wb.wb_obj, false);
478 if (unlikely(r != 0)) {
479 amdgpu_wb_fini(adev);
480 return r;
481 }
482 r = amdgpu_bo_pin(adev->wb.wb_obj, AMDGPU_GEM_DOMAIN_GTT,
483 &adev->wb.gpu_addr);
484 if (r) {
485 amdgpu_bo_unreserve(adev->wb.wb_obj);
486 dev_warn(adev->dev, "(%d) pin WB bo failed\n", r);
487 amdgpu_wb_fini(adev);
488 return r;
489 }
490 r = amdgpu_bo_kmap(adev->wb.wb_obj, (void **)&adev->wb.wb);
491 amdgpu_bo_unreserve(adev->wb.wb_obj);
492 if (r) {
493 dev_warn(adev->dev, "(%d) map WB bo failed\n", r);
494 amdgpu_wb_fini(adev);
495 return r;
496 }
497
498 adev->wb.num_wb = AMDGPU_MAX_WB;
499 memset(&adev->wb.used, 0, sizeof(adev->wb.used));
500
501 /* clear wb memory */
502 memset((char *)adev->wb.wb, 0, AMDGPU_GPU_PAGE_SIZE);
503 }
504
505 return 0;
506}
507
508/**
509 * amdgpu_wb_get - Allocate a wb entry
510 *
511 * @adev: amdgpu_device pointer
512 * @wb: wb index
513 *
514 * Allocate a wb slot for use by the driver (all asics).
515 * Returns 0 on success or -EINVAL on failure.
516 */
517int amdgpu_wb_get(struct amdgpu_device *adev, u32 *wb)
518{
519 unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
520 if (offset < adev->wb.num_wb) {
521 __set_bit(offset, adev->wb.used);
522 *wb = offset;
523 return 0;
524 } else {
525 return -EINVAL;
526 }
527}
528
529/**
530 * amdgpu_wb_free - Free a wb entry
531 *
532 * @adev: amdgpu_device pointer
533 * @wb: wb index
534 *
535 * Free a wb slot allocated for use by the driver (all asics)
536 */
537void amdgpu_wb_free(struct amdgpu_device *adev, u32 wb)
538{
539 if (wb < adev->wb.num_wb)
540 __clear_bit(wb, adev->wb.used);
541}
542
543/**
544 * amdgpu_vram_location - try to find VRAM location
545 * @adev: amdgpu device structure holding all necessary informations
546 * @mc: memory controller structure holding memory informations
547 * @base: base address at which to put VRAM
548 *
549 * Function will place try to place VRAM at base address provided
550 * as parameter (which is so far either PCI aperture address or
551 * for IGP TOM base address).
552 *
553 * If there is not enough space to fit the unvisible VRAM in the 32bits
554 * address space then we limit the VRAM size to the aperture.
555 *
556 * Note: We don't explicitly enforce VRAM start to be aligned on VRAM size,
557 * this shouldn't be a problem as we are using the PCI aperture as a reference.
558 * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but
559 * not IGP.
560 *
561 * Note: we use mc_vram_size as on some board we need to program the mc to
562 * cover the whole aperture even if VRAM size is inferior to aperture size
563 * Novell bug 204882 + along with lots of ubuntu ones
564 *
565 * Note: when limiting vram it's safe to overwritte real_vram_size because
566 * we are not in case where real_vram_size is inferior to mc_vram_size (ie
567 * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu
568 * ones)
569 *
570 * Note: IGP TOM addr should be the same as the aperture addr, we don't
571 * explicitly check for that thought.
572 *
573 * FIXME: when reducing VRAM size align new size on power of 2.
574 */
575void amdgpu_vram_location(struct amdgpu_device *adev, struct amdgpu_mc *mc, u64 base)
576{
577 uint64_t limit = (uint64_t)amdgpu_vram_limit << 20;
578
579 mc->vram_start = base;
580 if (mc->mc_vram_size > (adev->mc.mc_mask - base + 1)) {
581 dev_warn(adev->dev, "limiting VRAM to PCI aperture size\n");
582 mc->real_vram_size = mc->aper_size;
583 mc->mc_vram_size = mc->aper_size;
584 }
585 mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
586 if (limit && limit < mc->real_vram_size)
587 mc->real_vram_size = limit;
588 dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
589 mc->mc_vram_size >> 20, mc->vram_start,
590 mc->vram_end, mc->real_vram_size >> 20);
591}
592
593/**
594 * amdgpu_gtt_location - try to find GTT location
595 * @adev: amdgpu device structure holding all necessary informations
596 * @mc: memory controller structure holding memory informations
597 *
598 * Function will place try to place GTT before or after VRAM.
599 *
600 * If GTT size is bigger than space left then we ajust GTT size.
601 * Thus function will never fails.
602 *
603 * FIXME: when reducing GTT size align new size on power of 2.
604 */
605void amdgpu_gtt_location(struct amdgpu_device *adev, struct amdgpu_mc *mc)
606{
607 u64 size_af, size_bf;
608
609 size_af = ((adev->mc.mc_mask - mc->vram_end) + mc->gtt_base_align) & ~mc->gtt_base_align;
610 size_bf = mc->vram_start & ~mc->gtt_base_align;
611 if (size_bf > size_af) {
612 if (mc->gtt_size > size_bf) {
613 dev_warn(adev->dev, "limiting GTT\n");
614 mc->gtt_size = size_bf;
615 }
616 mc->gtt_start = (mc->vram_start & ~mc->gtt_base_align) - mc->gtt_size;
617 } else {
618 if (mc->gtt_size > size_af) {
619 dev_warn(adev->dev, "limiting GTT\n");
620 mc->gtt_size = size_af;
621 }
622 mc->gtt_start = (mc->vram_end + 1 + mc->gtt_base_align) & ~mc->gtt_base_align;
623 }
624 mc->gtt_end = mc->gtt_start + mc->gtt_size - 1;
625 dev_info(adev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
626 mc->gtt_size >> 20, mc->gtt_start, mc->gtt_end);
627}
628
629/*
630 * GPU helpers function.
631 */
632/**
633 * amdgpu_card_posted - check if the hw has already been initialized
634 *
635 * @adev: amdgpu_device pointer
636 *
637 * Check if the asic has been initialized (all asics).
638 * Used at driver startup.
639 * Returns true if initialized or false if not.
640 */
641bool amdgpu_card_posted(struct amdgpu_device *adev)
642{
643 uint32_t reg;
644
645 /* then check MEM_SIZE, in case the crtcs are off */
646 reg = RREG32(mmCONFIG_MEMSIZE);
647
648 if (reg)
649 return true;
650
651 return false;
652
653}
654
Monk Liubec86372016-09-14 19:38:08 +0800655static bool amdgpu_vpost_needed(struct amdgpu_device *adev)
656{
657 if (amdgpu_sriov_vf(adev))
658 return false;
659
660 if (amdgpu_passthrough(adev)) {
661 /* for FIJI: In whole GPU pass-through virtualization case
662 * old smc fw won't clear some registers (e.g. MEM_SIZE, BIOS_SCRATCH)
663 * so amdgpu_card_posted return false and driver will incorrectly skip vPost.
664 * but if we force vPost do in pass-through case, the driver reload will hang.
665 * whether doing vPost depends on amdgpu_card_posted if smc version is above
666 * 00160e00 for FIJI.
667 */
668 if (adev->asic_type == CHIP_FIJI) {
669 int err;
670 uint32_t fw_ver;
671 err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
672 /* force vPost if error occured */
673 if (err)
674 return true;
675
676 fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
677 if (fw_ver >= 0x00160e00)
678 return !amdgpu_card_posted(adev);
679 }
680 } else {
681 /* in bare-metal case, amdgpu_card_posted return false
682 * after system reboot/boot, and return true if driver
683 * reloaded.
684 * we shouldn't do vPost after driver reload otherwise GPU
685 * could hang.
686 */
687 if (amdgpu_card_posted(adev))
688 return false;
689 }
690
691 /* we assume vPost is neede for all other cases */
692 return true;
693}
694
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400695/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400696 * amdgpu_dummy_page_init - init dummy page used by the driver
697 *
698 * @adev: amdgpu_device pointer
699 *
700 * Allocate the dummy page used by the driver (all asics).
701 * This dummy page is used by the driver as a filler for gart entries
702 * when pages are taken out of the GART
703 * Returns 0 on sucess, -ENOMEM on failure.
704 */
705int amdgpu_dummy_page_init(struct amdgpu_device *adev)
706{
707 if (adev->dummy_page.page)
708 return 0;
709 adev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
710 if (adev->dummy_page.page == NULL)
711 return -ENOMEM;
712 adev->dummy_page.addr = pci_map_page(adev->pdev, adev->dummy_page.page,
713 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
714 if (pci_dma_mapping_error(adev->pdev, adev->dummy_page.addr)) {
715 dev_err(&adev->pdev->dev, "Failed to DMA MAP the dummy page\n");
716 __free_page(adev->dummy_page.page);
717 adev->dummy_page.page = NULL;
718 return -ENOMEM;
719 }
720 return 0;
721}
722
723/**
724 * amdgpu_dummy_page_fini - free dummy page used by the driver
725 *
726 * @adev: amdgpu_device pointer
727 *
728 * Frees the dummy page used by the driver (all asics).
729 */
730void amdgpu_dummy_page_fini(struct amdgpu_device *adev)
731{
732 if (adev->dummy_page.page == NULL)
733 return;
734 pci_unmap_page(adev->pdev, adev->dummy_page.addr,
735 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
736 __free_page(adev->dummy_page.page);
737 adev->dummy_page.page = NULL;
738}
739
740
741/* ATOM accessor methods */
742/*
743 * ATOM is an interpreted byte code stored in tables in the vbios. The
744 * driver registers callbacks to access registers and the interpreter
745 * in the driver parses the tables and executes then to program specific
746 * actions (set display modes, asic init, etc.). See amdgpu_atombios.c,
747 * atombios.h, and atom.c
748 */
749
750/**
751 * cail_pll_read - read PLL register
752 *
753 * @info: atom card_info pointer
754 * @reg: PLL register offset
755 *
756 * Provides a PLL register accessor for the atom interpreter (r4xx+).
757 * Returns the value of the PLL register.
758 */
759static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
760{
761 return 0;
762}
763
764/**
765 * cail_pll_write - write PLL register
766 *
767 * @info: atom card_info pointer
768 * @reg: PLL register offset
769 * @val: value to write to the pll register
770 *
771 * Provides a PLL register accessor for the atom interpreter (r4xx+).
772 */
773static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
774{
775
776}
777
778/**
779 * cail_mc_read - read MC (Memory Controller) register
780 *
781 * @info: atom card_info pointer
782 * @reg: MC register offset
783 *
784 * Provides an MC register accessor for the atom interpreter (r4xx+).
785 * Returns the value of the MC register.
786 */
787static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
788{
789 return 0;
790}
791
792/**
793 * cail_mc_write - write MC (Memory Controller) register
794 *
795 * @info: atom card_info pointer
796 * @reg: MC register offset
797 * @val: value to write to the pll register
798 *
799 * Provides a MC register accessor for the atom interpreter (r4xx+).
800 */
801static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
802{
803
804}
805
806/**
807 * cail_reg_write - write MMIO register
808 *
809 * @info: atom card_info pointer
810 * @reg: MMIO register offset
811 * @val: value to write to the pll register
812 *
813 * Provides a MMIO register accessor for the atom interpreter (r4xx+).
814 */
815static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
816{
817 struct amdgpu_device *adev = info->dev->dev_private;
818
819 WREG32(reg, val);
820}
821
822/**
823 * cail_reg_read - read MMIO register
824 *
825 * @info: atom card_info pointer
826 * @reg: MMIO register offset
827 *
828 * Provides an MMIO register accessor for the atom interpreter (r4xx+).
829 * Returns the value of the MMIO register.
830 */
831static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
832{
833 struct amdgpu_device *adev = info->dev->dev_private;
834 uint32_t r;
835
836 r = RREG32(reg);
837 return r;
838}
839
840/**
841 * cail_ioreg_write - write IO register
842 *
843 * @info: atom card_info pointer
844 * @reg: IO register offset
845 * @val: value to write to the pll register
846 *
847 * Provides a IO register accessor for the atom interpreter (r4xx+).
848 */
849static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
850{
851 struct amdgpu_device *adev = info->dev->dev_private;
852
853 WREG32_IO(reg, val);
854}
855
856/**
857 * cail_ioreg_read - read IO register
858 *
859 * @info: atom card_info pointer
860 * @reg: IO register offset
861 *
862 * Provides an IO register accessor for the atom interpreter (r4xx+).
863 * Returns the value of the IO register.
864 */
865static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
866{
867 struct amdgpu_device *adev = info->dev->dev_private;
868 uint32_t r;
869
870 r = RREG32_IO(reg);
871 return r;
872}
873
874/**
875 * amdgpu_atombios_fini - free the driver info and callbacks for atombios
876 *
877 * @adev: amdgpu_device pointer
878 *
879 * Frees the driver info and register access callbacks for the ATOM
880 * interpreter (r4xx+).
881 * Called at driver shutdown.
882 */
883static void amdgpu_atombios_fini(struct amdgpu_device *adev)
884{
Monk Liu89e0ec92016-05-27 19:34:11 +0800885 if (adev->mode_info.atom_context) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400886 kfree(adev->mode_info.atom_context->scratch);
Monk Liu89e0ec92016-05-27 19:34:11 +0800887 kfree(adev->mode_info.atom_context->iio);
888 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400889 kfree(adev->mode_info.atom_context);
890 adev->mode_info.atom_context = NULL;
891 kfree(adev->mode_info.atom_card_info);
892 adev->mode_info.atom_card_info = NULL;
893}
894
895/**
896 * amdgpu_atombios_init - init the driver info and callbacks for atombios
897 *
898 * @adev: amdgpu_device pointer
899 *
900 * Initializes the driver info and register access callbacks for the
901 * ATOM interpreter (r4xx+).
902 * Returns 0 on sucess, -ENOMEM on failure.
903 * Called at driver startup.
904 */
905static int amdgpu_atombios_init(struct amdgpu_device *adev)
906{
907 struct card_info *atom_card_info =
908 kzalloc(sizeof(struct card_info), GFP_KERNEL);
909
910 if (!atom_card_info)
911 return -ENOMEM;
912
913 adev->mode_info.atom_card_info = atom_card_info;
914 atom_card_info->dev = adev->ddev;
915 atom_card_info->reg_read = cail_reg_read;
916 atom_card_info->reg_write = cail_reg_write;
917 /* needed for iio ops */
918 if (adev->rio_mem) {
919 atom_card_info->ioreg_read = cail_ioreg_read;
920 atom_card_info->ioreg_write = cail_ioreg_write;
921 } else {
922 DRM_ERROR("Unable to find PCI I/O BAR; using MMIO for ATOM IIO\n");
923 atom_card_info->ioreg_read = cail_reg_read;
924 atom_card_info->ioreg_write = cail_reg_write;
925 }
926 atom_card_info->mc_read = cail_mc_read;
927 atom_card_info->mc_write = cail_mc_write;
928 atom_card_info->pll_read = cail_pll_read;
929 atom_card_info->pll_write = cail_pll_write;
930
931 adev->mode_info.atom_context = amdgpu_atom_parse(atom_card_info, adev->bios);
932 if (!adev->mode_info.atom_context) {
933 amdgpu_atombios_fini(adev);
934 return -ENOMEM;
935 }
936
937 mutex_init(&adev->mode_info.atom_context->mutex);
938 amdgpu_atombios_scratch_regs_init(adev);
939 amdgpu_atom_allocate_fb_scratch(adev->mode_info.atom_context);
940 return 0;
941}
942
943/* if we get transitioned to only one device, take VGA back */
944/**
945 * amdgpu_vga_set_decode - enable/disable vga decode
946 *
947 * @cookie: amdgpu_device pointer
948 * @state: enable/disable vga decode
949 *
950 * Enable/disable vga decode (all asics).
951 * Returns VGA resource flags.
952 */
953static unsigned int amdgpu_vga_set_decode(void *cookie, bool state)
954{
955 struct amdgpu_device *adev = cookie;
956 amdgpu_asic_set_vga_state(adev, state);
957 if (state)
958 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
959 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
960 else
961 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
962}
963
964/**
965 * amdgpu_check_pot_argument - check that argument is a power of two
966 *
967 * @arg: value to check
968 *
969 * Validates that a certain argument is a power of two (all asics).
970 * Returns true if argument is valid.
971 */
972static bool amdgpu_check_pot_argument(int arg)
973{
974 return (arg & (arg - 1)) == 0;
975}
976
977/**
978 * amdgpu_check_arguments - validate module params
979 *
980 * @adev: amdgpu_device pointer
981 *
982 * Validates certain module parameters and updates
983 * the associated values used by the driver (all asics).
984 */
985static void amdgpu_check_arguments(struct amdgpu_device *adev)
986{
Chunming Zhou5b011232015-12-10 17:34:33 +0800987 if (amdgpu_sched_jobs < 4) {
988 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
989 amdgpu_sched_jobs);
990 amdgpu_sched_jobs = 4;
991 } else if (!amdgpu_check_pot_argument(amdgpu_sched_jobs)){
992 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
993 amdgpu_sched_jobs);
994 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
995 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400996
997 if (amdgpu_gart_size != -1) {
Christian Königc4e1a132016-03-17 16:25:15 +0100998 /* gtt size must be greater or equal to 32M */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400999 if (amdgpu_gart_size < 32) {
1000 dev_warn(adev->dev, "gart size (%d) too small\n",
1001 amdgpu_gart_size);
1002 amdgpu_gart_size = -1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001003 }
1004 }
1005
1006 if (!amdgpu_check_pot_argument(amdgpu_vm_size)) {
1007 dev_warn(adev->dev, "VM size (%d) must be a power of 2\n",
1008 amdgpu_vm_size);
Alex Deucher8dacc122015-05-11 16:20:58 -04001009 amdgpu_vm_size = 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001010 }
1011
1012 if (amdgpu_vm_size < 1) {
1013 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
1014 amdgpu_vm_size);
Alex Deucher8dacc122015-05-11 16:20:58 -04001015 amdgpu_vm_size = 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001016 }
1017
1018 /*
1019 * Max GPUVM size for Cayman, SI and CI are 40 bits.
1020 */
1021 if (amdgpu_vm_size > 1024) {
1022 dev_warn(adev->dev, "VM size (%d) too large, max is 1TB\n",
1023 amdgpu_vm_size);
Alex Deucher8dacc122015-05-11 16:20:58 -04001024 amdgpu_vm_size = 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001025 }
1026
1027 /* defines number of bits in page table versus page directory,
1028 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
1029 * page table and the remaining bits are in the page directory */
1030 if (amdgpu_vm_block_size == -1) {
1031
1032 /* Total bits covered by PD + PTs */
1033 unsigned bits = ilog2(amdgpu_vm_size) + 18;
1034
1035 /* Make sure the PD is 4K in size up to 8GB address space.
1036 Above that split equal between PD and PTs */
1037 if (amdgpu_vm_size <= 8)
1038 amdgpu_vm_block_size = bits - 9;
1039 else
1040 amdgpu_vm_block_size = (bits + 3) / 2;
1041
1042 } else if (amdgpu_vm_block_size < 9) {
1043 dev_warn(adev->dev, "VM page table size (%d) too small\n",
1044 amdgpu_vm_block_size);
1045 amdgpu_vm_block_size = 9;
1046 }
1047
1048 if (amdgpu_vm_block_size > 24 ||
1049 (amdgpu_vm_size * 1024) < (1ull << amdgpu_vm_block_size)) {
1050 dev_warn(adev->dev, "VM page table size (%d) too large\n",
1051 amdgpu_vm_block_size);
1052 amdgpu_vm_block_size = 9;
1053 }
1054}
1055
1056/**
1057 * amdgpu_switcheroo_set_state - set switcheroo state
1058 *
1059 * @pdev: pci dev pointer
Lukas Wunner16944672015-09-05 11:17:35 +02001060 * @state: vga_switcheroo state
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001061 *
1062 * Callback for the switcheroo driver. Suspends or resumes the
1063 * the asics before or after it is powered up using ACPI methods.
1064 */
1065static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1066{
1067 struct drm_device *dev = pci_get_drvdata(pdev);
1068
1069 if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
1070 return;
1071
1072 if (state == VGA_SWITCHEROO_ON) {
1073 unsigned d3_delay = dev->pdev->d3_delay;
1074
1075 printk(KERN_INFO "amdgpu: switched on\n");
1076 /* don't suspend or resume card normally */
1077 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1078
Alex Deucher810ddc32016-08-23 13:25:49 -04001079 amdgpu_device_resume(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001080
1081 dev->pdev->d3_delay = d3_delay;
1082
1083 dev->switch_power_state = DRM_SWITCH_POWER_ON;
1084 drm_kms_helper_poll_enable(dev);
1085 } else {
1086 printk(KERN_INFO "amdgpu: switched off\n");
1087 drm_kms_helper_poll_disable(dev);
1088 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Alex Deucher810ddc32016-08-23 13:25:49 -04001089 amdgpu_device_suspend(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001090 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1091 }
1092}
1093
1094/**
1095 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1096 *
1097 * @pdev: pci dev pointer
1098 *
1099 * Callback for the switcheroo driver. Check of the switcheroo
1100 * state can be changed.
1101 * Returns true if the state can be changed, false if not.
1102 */
1103static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1104{
1105 struct drm_device *dev = pci_get_drvdata(pdev);
1106
1107 /*
1108 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1109 * locking inversion with the driver load path. And the access here is
1110 * completely racy anyway. So don't bother with locking for now.
1111 */
1112 return dev->open_count == 0;
1113}
1114
1115static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1116 .set_gpu_state = amdgpu_switcheroo_set_state,
1117 .reprobe = NULL,
1118 .can_switch = amdgpu_switcheroo_can_switch,
1119};
1120
1121int amdgpu_set_clockgating_state(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001122 enum amd_ip_block_type block_type,
1123 enum amd_clockgating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001124{
1125 int i, r = 0;
1126
1127 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001128 if (!adev->ip_block_status[i].valid)
1129 continue;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001130 if (adev->ip_blocks[i].type == block_type) {
yanyang15fc3aee2015-05-22 14:39:35 -04001131 r = adev->ip_blocks[i].funcs->set_clockgating_state((void *)adev,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001132 state);
1133 if (r)
1134 return r;
Alex Deuchera225bf12016-06-23 11:48:30 -04001135 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001136 }
1137 }
1138 return r;
1139}
1140
1141int amdgpu_set_powergating_state(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001142 enum amd_ip_block_type block_type,
1143 enum amd_powergating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001144{
1145 int i, r = 0;
1146
1147 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001148 if (!adev->ip_block_status[i].valid)
1149 continue;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001150 if (adev->ip_blocks[i].type == block_type) {
yanyang15fc3aee2015-05-22 14:39:35 -04001151 r = adev->ip_blocks[i].funcs->set_powergating_state((void *)adev,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001152 state);
1153 if (r)
1154 return r;
Alex Deuchera225bf12016-06-23 11:48:30 -04001155 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001156 }
1157 }
1158 return r;
1159}
1160
Alex Deucher5dbbb602016-06-23 11:41:04 -04001161int amdgpu_wait_for_idle(struct amdgpu_device *adev,
1162 enum amd_ip_block_type block_type)
1163{
1164 int i, r;
1165
1166 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001167 if (!adev->ip_block_status[i].valid)
1168 continue;
Alex Deucher5dbbb602016-06-23 11:41:04 -04001169 if (adev->ip_blocks[i].type == block_type) {
1170 r = adev->ip_blocks[i].funcs->wait_for_idle((void *)adev);
1171 if (r)
1172 return r;
1173 break;
1174 }
1175 }
1176 return 0;
1177
1178}
1179
1180bool amdgpu_is_idle(struct amdgpu_device *adev,
1181 enum amd_ip_block_type block_type)
1182{
1183 int i;
1184
1185 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001186 if (!adev->ip_block_status[i].valid)
1187 continue;
Alex Deucher5dbbb602016-06-23 11:41:04 -04001188 if (adev->ip_blocks[i].type == block_type)
1189 return adev->ip_blocks[i].funcs->is_idle((void *)adev);
1190 }
1191 return true;
1192
1193}
1194
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001195const struct amdgpu_ip_block_version * amdgpu_get_ip_block(
1196 struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001197 enum amd_ip_block_type type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001198{
1199 int i;
1200
1201 for (i = 0; i < adev->num_ip_blocks; i++)
1202 if (adev->ip_blocks[i].type == type)
1203 return &adev->ip_blocks[i];
1204
1205 return NULL;
1206}
1207
1208/**
1209 * amdgpu_ip_block_version_cmp
1210 *
1211 * @adev: amdgpu_device pointer
yanyang15fc3aee2015-05-22 14:39:35 -04001212 * @type: enum amd_ip_block_type
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001213 * @major: major version
1214 * @minor: minor version
1215 *
1216 * return 0 if equal or greater
1217 * return 1 if smaller or the ip_block doesn't exist
1218 */
1219int amdgpu_ip_block_version_cmp(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001220 enum amd_ip_block_type type,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001221 u32 major, u32 minor)
1222{
1223 const struct amdgpu_ip_block_version *ip_block;
1224 ip_block = amdgpu_get_ip_block(adev, type);
1225
1226 if (ip_block && ((ip_block->major > major) ||
1227 ((ip_block->major == major) &&
1228 (ip_block->minor >= minor))))
1229 return 0;
1230
1231 return 1;
1232}
1233
Emily Deng9accf2f2016-08-10 16:01:25 +08001234static void amdgpu_whether_enable_virtual_display(struct amdgpu_device *adev)
1235{
1236 adev->enable_virtual_display = false;
1237
1238 if (amdgpu_virtual_display) {
1239 struct drm_device *ddev = adev->ddev;
1240 const char *pci_address_name = pci_name(ddev->pdev);
1241 char *pciaddstr, *pciaddstr_tmp, *pciaddname;
1242
1243 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1244 pciaddstr_tmp = pciaddstr;
1245 while ((pciaddname = strsep(&pciaddstr_tmp, ";"))) {
1246 if (!strcmp(pci_address_name, pciaddname)) {
1247 adev->enable_virtual_display = true;
1248 break;
1249 }
1250 }
1251
1252 DRM_INFO("virtual display string:%s, %s:virtual_display:%d\n",
1253 amdgpu_virtual_display, pci_address_name,
1254 adev->enable_virtual_display);
1255
1256 kfree(pciaddstr);
1257 }
1258}
1259
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001260static int amdgpu_early_init(struct amdgpu_device *adev)
1261{
Alex Deucheraaa36a92015-04-20 17:31:14 -04001262 int i, r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001263
Emily Deng9accf2f2016-08-10 16:01:25 +08001264 amdgpu_whether_enable_virtual_display(adev);
Emily Denga6be7572016-08-08 11:37:50 +08001265
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001266 switch (adev->asic_type) {
Alex Deucheraaa36a92015-04-20 17:31:14 -04001267 case CHIP_TOPAZ:
1268 case CHIP_TONGA:
David Zhang48299f92015-07-08 01:05:16 +08001269 case CHIP_FIJI:
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001270 case CHIP_POLARIS11:
1271 case CHIP_POLARIS10:
Alex Deucheraaa36a92015-04-20 17:31:14 -04001272 case CHIP_CARRIZO:
Samuel Li39bb0c92015-10-08 16:31:43 -04001273 case CHIP_STONEY:
1274 if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
Alex Deucheraaa36a92015-04-20 17:31:14 -04001275 adev->family = AMDGPU_FAMILY_CZ;
1276 else
1277 adev->family = AMDGPU_FAMILY_VI;
1278
1279 r = vi_set_ip_blocks(adev);
1280 if (r)
1281 return r;
1282 break;
Ken Wang33f34802016-01-21 17:29:41 +08001283#ifdef CONFIG_DRM_AMDGPU_SI
1284 case CHIP_VERDE:
1285 case CHIP_TAHITI:
1286 case CHIP_PITCAIRN:
1287 case CHIP_OLAND:
1288 case CHIP_HAINAN:
Ken Wang295d0da2016-05-24 21:02:53 +08001289 adev->family = AMDGPU_FAMILY_SI;
Ken Wang33f34802016-01-21 17:29:41 +08001290 r = si_set_ip_blocks(adev);
1291 if (r)
1292 return r;
1293 break;
1294#endif
Alex Deuchera2e73f52015-04-20 17:09:27 -04001295#ifdef CONFIG_DRM_AMDGPU_CIK
1296 case CHIP_BONAIRE:
1297 case CHIP_HAWAII:
1298 case CHIP_KAVERI:
1299 case CHIP_KABINI:
1300 case CHIP_MULLINS:
1301 if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
1302 adev->family = AMDGPU_FAMILY_CI;
1303 else
1304 adev->family = AMDGPU_FAMILY_KV;
1305
1306 r = cik_set_ip_blocks(adev);
1307 if (r)
1308 return r;
1309 break;
1310#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001311 default:
1312 /* FIXME: not supported yet */
1313 return -EINVAL;
1314 }
1315
Alex Deucher8faf0e082015-07-28 11:50:31 -04001316 adev->ip_block_status = kcalloc(adev->num_ip_blocks,
1317 sizeof(struct amdgpu_ip_block_status), GFP_KERNEL);
1318 if (adev->ip_block_status == NULL)
Alex Deucherd8d090b2015-06-26 13:02:57 -04001319 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001320
1321 if (adev->ip_blocks == NULL) {
1322 DRM_ERROR("No IP blocks found!\n");
1323 return r;
1324 }
1325
1326 for (i = 0; i < adev->num_ip_blocks; i++) {
1327 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
1328 DRM_ERROR("disabled ip block: %d\n", i);
Alex Deucher8faf0e082015-07-28 11:50:31 -04001329 adev->ip_block_status[i].valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001330 } else {
1331 if (adev->ip_blocks[i].funcs->early_init) {
yanyang15fc3aee2015-05-22 14:39:35 -04001332 r = adev->ip_blocks[i].funcs->early_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001333 if (r == -ENOENT) {
Alex Deucher8faf0e082015-07-28 11:50:31 -04001334 adev->ip_block_status[i].valid = false;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001335 } else if (r) {
Tom St Denis88a907d2016-05-04 14:28:35 -04001336 DRM_ERROR("early_init of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001337 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001338 } else {
Alex Deucher8faf0e082015-07-28 11:50:31 -04001339 adev->ip_block_status[i].valid = true;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001340 }
Alex Deucher974e6b62015-07-10 13:59:44 -04001341 } else {
Alex Deucher8faf0e082015-07-28 11:50:31 -04001342 adev->ip_block_status[i].valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001343 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001344 }
1345 }
1346
Nicolai Hähnle395d1fb2016-06-02 12:32:07 +02001347 adev->cg_flags &= amdgpu_cg_mask;
1348 adev->pg_flags &= amdgpu_pg_mask;
1349
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001350 return 0;
1351}
1352
1353static int amdgpu_init(struct amdgpu_device *adev)
1354{
1355 int i, r;
1356
1357 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deucher8faf0e082015-07-28 11:50:31 -04001358 if (!adev->ip_block_status[i].valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001359 continue;
yanyang15fc3aee2015-05-22 14:39:35 -04001360 r = adev->ip_blocks[i].funcs->sw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001361 if (r) {
Tom St Denis822b2ce2016-05-05 10:23:40 -04001362 DRM_ERROR("sw_init of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001363 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001364 }
Alex Deucher8faf0e082015-07-28 11:50:31 -04001365 adev->ip_block_status[i].sw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001366 /* need to do gmc hw init early so we can allocate gpu mem */
yanyang15fc3aee2015-05-22 14:39:35 -04001367 if (adev->ip_blocks[i].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 }
yanyang15fc3aee2015-05-22 14:39:35 -04001373 r = adev->ip_blocks[i].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 Deucher8faf0e082015-07-28 11:50:31 -04001383 adev->ip_block_status[i].hw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001384 }
1385 }
1386
1387 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deucher8faf0e082015-07-28 11:50:31 -04001388 if (!adev->ip_block_status[i].sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001389 continue;
1390 /* gmc hw init is done early */
yanyang15fc3aee2015-05-22 14:39:35 -04001391 if (adev->ip_blocks[i].type == AMD_IP_BLOCK_TYPE_GMC)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001392 continue;
yanyang15fc3aee2015-05-22 14:39:35 -04001393 r = adev->ip_blocks[i].funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001394 if (r) {
Tom St Denis822b2ce2016-05-05 10:23:40 -04001395 DRM_ERROR("hw_init of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001396 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001397 }
Alex Deucher8faf0e082015-07-28 11:50:31 -04001398 adev->ip_block_status[i].hw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001399 }
1400
1401 return 0;
1402}
1403
1404static int amdgpu_late_init(struct amdgpu_device *adev)
1405{
1406 int i = 0, r;
1407
1408 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deucher8faf0e082015-07-28 11:50:31 -04001409 if (!adev->ip_block_status[i].valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001410 continue;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001411 if (adev->ip_blocks[i].funcs->late_init) {
yanyang15fc3aee2015-05-22 14:39:35 -04001412 r = adev->ip_blocks[i].funcs->late_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001413 if (r) {
Tom St Denis822b2ce2016-05-05 10:23:40 -04001414 DRM_ERROR("late_init of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001415 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001416 }
Grazvydas Ignotas8a2eef12016-10-03 00:06:44 +03001417 adev->ip_block_status[i].late_initialized = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001418 }
Alex Deucher4a446d52016-10-07 14:48:18 -04001419 /* skip CG for VCE/UVD, it's handled specially */
1420 if (adev->ip_blocks[i].type != AMD_IP_BLOCK_TYPE_UVD &&
1421 adev->ip_blocks[i].type != AMD_IP_BLOCK_TYPE_VCE) {
1422 /* enable clockgating to save power */
1423 r = adev->ip_blocks[i].funcs->set_clockgating_state((void *)adev,
1424 AMD_CG_STATE_GATE);
1425 if (r) {
1426 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
1427 adev->ip_blocks[i].funcs->name, r);
1428 return r;
1429 }
Arindam Nathb0b00ff2016-10-07 19:01:37 +05301430 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001431 }
1432
1433 return 0;
1434}
1435
1436static int amdgpu_fini(struct amdgpu_device *adev)
1437{
1438 int i, r;
1439
1440 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deucher8faf0e082015-07-28 11:50:31 -04001441 if (!adev->ip_block_status[i].hw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001442 continue;
yanyang15fc3aee2015-05-22 14:39:35 -04001443 if (adev->ip_blocks[i].type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001444 amdgpu_wb_fini(adev);
1445 amdgpu_vram_scratch_fini(adev);
1446 }
1447 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
yanyang15fc3aee2015-05-22 14:39:35 -04001448 r = adev->ip_blocks[i].funcs->set_clockgating_state((void *)adev,
1449 AMD_CG_STATE_UNGATE);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001450 if (r) {
Tom St Denis822b2ce2016-05-05 10:23:40 -04001451 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001452 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001453 }
yanyang15fc3aee2015-05-22 14:39:35 -04001454 r = adev->ip_blocks[i].funcs->hw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001455 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001456 if (r) {
Tom St Denis822b2ce2016-05-05 10:23:40 -04001457 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001458 }
Alex Deucher8faf0e082015-07-28 11:50:31 -04001459 adev->ip_block_status[i].hw = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001460 }
1461
1462 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deucher8faf0e082015-07-28 11:50:31 -04001463 if (!adev->ip_block_status[i].sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001464 continue;
yanyang15fc3aee2015-05-22 14:39:35 -04001465 r = adev->ip_blocks[i].funcs->sw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001466 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001467 if (r) {
Tom St Denis822b2ce2016-05-05 10:23:40 -04001468 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001469 }
Alex Deucher8faf0e082015-07-28 11:50:31 -04001470 adev->ip_block_status[i].sw = false;
1471 adev->ip_block_status[i].valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001472 }
1473
Monk Liua6dcfd92016-05-19 14:36:34 +08001474 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Grazvydas Ignotas8a2eef12016-10-03 00:06:44 +03001475 if (!adev->ip_block_status[i].late_initialized)
1476 continue;
Monk Liua6dcfd92016-05-19 14:36:34 +08001477 if (adev->ip_blocks[i].funcs->late_fini)
1478 adev->ip_blocks[i].funcs->late_fini((void *)adev);
Grazvydas Ignotas8a2eef12016-10-03 00:06:44 +03001479 adev->ip_block_status[i].late_initialized = false;
Monk Liua6dcfd92016-05-19 14:36:34 +08001480 }
1481
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001482 return 0;
1483}
1484
1485static int amdgpu_suspend(struct amdgpu_device *adev)
1486{
1487 int i, r;
1488
Flora Cuic5a93a22016-02-26 10:45:25 +08001489 /* ungate SMC block first */
1490 r = amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC,
1491 AMD_CG_STATE_UNGATE);
1492 if (r) {
1493 DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n",r);
1494 }
1495
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001496 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deucher8faf0e082015-07-28 11:50:31 -04001497 if (!adev->ip_block_status[i].valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001498 continue;
1499 /* ungate blocks so that suspend can properly shut them down */
Flora Cuic5a93a22016-02-26 10:45:25 +08001500 if (i != AMD_IP_BLOCK_TYPE_SMC) {
1501 r = adev->ip_blocks[i].funcs->set_clockgating_state((void *)adev,
1502 AMD_CG_STATE_UNGATE);
1503 if (r) {
Tom St Denis822b2ce2016-05-05 10:23:40 -04001504 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r);
Flora Cuic5a93a22016-02-26 10:45:25 +08001505 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001506 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001507 /* XXX handle errors */
1508 r = adev->ip_blocks[i].funcs->suspend(adev);
1509 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001510 if (r) {
Tom St Denis822b2ce2016-05-05 10:23:40 -04001511 DRM_ERROR("suspend of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001512 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001513 }
1514
1515 return 0;
1516}
1517
1518static int amdgpu_resume(struct amdgpu_device *adev)
1519{
1520 int i, r;
1521
1522 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deucher8faf0e082015-07-28 11:50:31 -04001523 if (!adev->ip_block_status[i].valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001524 continue;
1525 r = adev->ip_blocks[i].funcs->resume(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001526 if (r) {
Tom St Denis822b2ce2016-05-05 10:23:40 -04001527 DRM_ERROR("resume of IP block <%s> failed %d\n", adev->ip_blocks[i].funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001528 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001529 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001530 }
1531
1532 return 0;
1533}
1534
Monk Liu4e99a442016-03-31 13:26:59 +08001535static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
Andres Rodriguez048765a2016-06-11 02:51:32 -04001536{
Monk Liu4e99a442016-03-31 13:26:59 +08001537 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
1538 adev->virtualization.virtual_caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
Andres Rodriguez048765a2016-06-11 02:51:32 -04001539}
1540
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001541/**
1542 * amdgpu_device_init - initialize the driver
1543 *
1544 * @adev: amdgpu_device pointer
1545 * @pdev: drm dev pointer
1546 * @pdev: pci dev pointer
1547 * @flags: driver flags
1548 *
1549 * Initializes the driver info and hw (all asics).
1550 * Returns 0 for success or an error on failure.
1551 * Called at driver startup.
1552 */
1553int amdgpu_device_init(struct amdgpu_device *adev,
1554 struct drm_device *ddev,
1555 struct pci_dev *pdev,
1556 uint32_t flags)
1557{
1558 int r, i;
1559 bool runtime = false;
Marek Olšák95844d22016-08-17 23:49:27 +02001560 u32 max_MBps;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001561
1562 adev->shutdown = false;
1563 adev->dev = &pdev->dev;
1564 adev->ddev = ddev;
1565 adev->pdev = pdev;
1566 adev->flags = flags;
Jammy Zhou2f7d10b2015-07-22 11:29:01 +08001567 adev->asic_type = flags & AMD_ASIC_MASK;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001568 adev->is_atom_bios = false;
1569 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
1570 adev->mc.gtt_size = 512 * 1024 * 1024;
1571 adev->accel_working = false;
1572 adev->num_rings = 0;
1573 adev->mman.buffer_funcs = NULL;
1574 adev->mman.buffer_funcs_ring = NULL;
1575 adev->vm_manager.vm_pte_funcs = NULL;
Christian König2d55e452016-02-08 17:37:38 +01001576 adev->vm_manager.vm_pte_num_rings = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001577 adev->gart.gart_funcs = NULL;
1578 adev->fence_context = fence_context_alloc(AMDGPU_MAX_RINGS);
1579
1580 adev->smc_rreg = &amdgpu_invalid_rreg;
1581 adev->smc_wreg = &amdgpu_invalid_wreg;
1582 adev->pcie_rreg = &amdgpu_invalid_rreg;
1583 adev->pcie_wreg = &amdgpu_invalid_wreg;
Huang Rui36b9a952016-08-31 13:23:25 +08001584 adev->pciep_rreg = &amdgpu_invalid_rreg;
1585 adev->pciep_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001586 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
1587 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
1588 adev->didt_rreg = &amdgpu_invalid_rreg;
1589 adev->didt_wreg = &amdgpu_invalid_wreg;
Rex Zhuccdbb202016-06-08 12:47:41 +08001590 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
1591 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001592 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
1593 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
1594
Rex Zhuccdbb202016-06-08 12:47:41 +08001595
Alex Deucher3e39ab92015-06-05 15:04:33 -04001596 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
1597 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
1598 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001599
1600 /* mutex initialization are all done here so we
1601 * can recall function without having locking issues */
Christian König8d0a7ce2015-11-03 20:58:50 +01001602 mutex_init(&adev->vm_manager.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001603 atomic_set(&adev->irq.ih.lock, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001604 mutex_init(&adev->pm.mutex);
1605 mutex_init(&adev->gfx.gpu_clock_mutex);
1606 mutex_init(&adev->srbm_mutex);
1607 mutex_init(&adev->grbm_idx_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001608 mutex_init(&adev->mn_lock);
1609 hash_init(adev->mn_hash);
1610
1611 amdgpu_check_arguments(adev);
1612
1613 /* Registers mapping */
1614 /* TODO: block userspace mapping of io register */
1615 spin_lock_init(&adev->mmio_idx_lock);
1616 spin_lock_init(&adev->smc_idx_lock);
1617 spin_lock_init(&adev->pcie_idx_lock);
1618 spin_lock_init(&adev->uvd_ctx_idx_lock);
1619 spin_lock_init(&adev->didt_idx_lock);
Rex Zhuccdbb202016-06-08 12:47:41 +08001620 spin_lock_init(&adev->gc_cac_idx_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001621 spin_lock_init(&adev->audio_endpt_idx_lock);
Marek Olšák95844d22016-08-17 23:49:27 +02001622 spin_lock_init(&adev->mm_stats.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001623
Chunming Zhou0c4e7fa2016-08-17 11:41:30 +08001624 INIT_LIST_HEAD(&adev->shadow_list);
1625 mutex_init(&adev->shadow_list_lock);
1626
Chunming Zhou5c1354b2016-08-30 16:13:10 +08001627 INIT_LIST_HEAD(&adev->gtt_list);
1628 spin_lock_init(&adev->gtt_list_lock);
1629
Ken Wangda69c1612016-01-21 19:08:55 +08001630 if (adev->asic_type >= CHIP_BONAIRE) {
1631 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
1632 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
1633 } else {
1634 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
1635 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
1636 }
Chunming Zhou5c1354b2016-08-30 16:13:10 +08001637
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001638 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
1639 if (adev->rmmio == NULL) {
1640 return -ENOMEM;
1641 }
1642 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
1643 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
1644
Ken Wangda69c1612016-01-21 19:08:55 +08001645 if (adev->asic_type >= CHIP_BONAIRE)
1646 /* doorbell bar mapping */
1647 amdgpu_doorbell_init(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001648
1649 /* io port mapping */
1650 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
1651 if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
1652 adev->rio_mem_size = pci_resource_len(adev->pdev, i);
1653 adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
1654 break;
1655 }
1656 }
1657 if (adev->rio_mem == NULL)
1658 DRM_ERROR("Unable to find PCI I/O BAR\n");
1659
1660 /* early init functions */
1661 r = amdgpu_early_init(adev);
1662 if (r)
1663 return r;
1664
1665 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
1666 /* this will fail for cards that aren't VGA class devices, just
1667 * ignore it */
1668 vga_client_register(adev->pdev, adev, NULL, amdgpu_vga_set_decode);
1669
1670 if (amdgpu_runtime_pm == 1)
1671 runtime = true;
Alex Deuchere9bef452016-04-25 13:12:18 -04001672 if (amdgpu_device_is_px(ddev))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001673 runtime = true;
1674 vga_switcheroo_register_client(adev->pdev, &amdgpu_switcheroo_ops, runtime);
1675 if (runtime)
1676 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
1677
1678 /* Read BIOS */
Alex Deucher83ba1262016-06-03 18:21:41 -04001679 if (!amdgpu_get_bios(adev)) {
1680 r = -EINVAL;
1681 goto failed;
1682 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001683 /* Must be an ATOMBIOS */
1684 if (!adev->is_atom_bios) {
1685 dev_err(adev->dev, "Expecting atombios for GPU\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001686 r = -EINVAL;
1687 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001688 }
1689 r = amdgpu_atombios_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001690 if (r) {
1691 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001692 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001693 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001694
Monk Liu4e99a442016-03-31 13:26:59 +08001695 /* detect if we are with an SRIOV vbios */
1696 amdgpu_device_detect_sriov_bios(adev);
Andres Rodriguez048765a2016-06-11 02:51:32 -04001697
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001698 /* Post card if necessary */
Monk Liubec86372016-09-14 19:38:08 +08001699 if (amdgpu_vpost_needed(adev)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001700 if (!adev->bios) {
Monk Liubec86372016-09-14 19:38:08 +08001701 dev_err(adev->dev, "no vBIOS found\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001702 r = -EINVAL;
1703 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001704 }
Monk Liubec86372016-09-14 19:38:08 +08001705 DRM_INFO("GPU posting now...\n");
Monk Liu4e99a442016-03-31 13:26:59 +08001706 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
1707 if (r) {
1708 dev_err(adev->dev, "gpu post error!\n");
1709 goto failed;
1710 }
1711 } else {
1712 DRM_INFO("GPU post is not needed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001713 }
1714
1715 /* Initialize clocks */
1716 r = amdgpu_atombios_get_clock_info(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001717 if (r) {
1718 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001719 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001720 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001721 /* init i2c buses */
1722 amdgpu_atombios_i2c_init(adev);
1723
1724 /* Fence driver */
1725 r = amdgpu_fence_driver_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001726 if (r) {
1727 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001728 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001729 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001730
1731 /* init the mode config */
1732 drm_mode_config_init(adev->ddev);
1733
1734 r = amdgpu_init(adev);
1735 if (r) {
Alex Deucher2c1a2782015-12-07 17:02:53 -05001736 dev_err(adev->dev, "amdgpu_init failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001737 amdgpu_fini(adev);
Alex Deucher83ba1262016-06-03 18:21:41 -04001738 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001739 }
1740
1741 adev->accel_working = true;
1742
Marek Olšák95844d22016-08-17 23:49:27 +02001743 /* Initialize the buffer migration limit. */
1744 if (amdgpu_moverate >= 0)
1745 max_MBps = amdgpu_moverate;
1746 else
1747 max_MBps = 8; /* Allow 8 MB/s. */
1748 /* Get a log2 for easy divisions. */
1749 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
1750
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001751 amdgpu_fbdev_init(adev);
1752
1753 r = amdgpu_ib_pool_init(adev);
1754 if (r) {
1755 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
Alex Deucher83ba1262016-06-03 18:21:41 -04001756 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001757 }
1758
1759 r = amdgpu_ib_ring_tests(adev);
1760 if (r)
1761 DRM_ERROR("ib ring test failed (%d).\n", r);
1762
1763 r = amdgpu_gem_debugfs_init(adev);
1764 if (r) {
1765 DRM_ERROR("registering gem debugfs failed (%d).\n", r);
1766 }
1767
1768 r = amdgpu_debugfs_regs_init(adev);
1769 if (r) {
1770 DRM_ERROR("registering register debugfs failed (%d).\n", r);
1771 }
1772
Huang Rui50ab2532016-06-12 15:51:09 +08001773 r = amdgpu_debugfs_firmware_init(adev);
1774 if (r) {
1775 DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
1776 return r;
1777 }
1778
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001779 if ((amdgpu_testing & 1)) {
1780 if (adev->accel_working)
1781 amdgpu_test_moves(adev);
1782 else
1783 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
1784 }
1785 if ((amdgpu_testing & 2)) {
1786 if (adev->accel_working)
1787 amdgpu_test_syncing(adev);
1788 else
1789 DRM_INFO("amdgpu: acceleration disabled, skipping sync tests\n");
1790 }
1791 if (amdgpu_benchmarking) {
1792 if (adev->accel_working)
1793 amdgpu_benchmark(adev, amdgpu_benchmarking);
1794 else
1795 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
1796 }
1797
1798 /* enable clockgating, etc. after ib tests, etc. since some blocks require
1799 * explicit gating rather than handling it automatically.
1800 */
1801 r = amdgpu_late_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001802 if (r) {
1803 dev_err(adev->dev, "amdgpu_late_init failed\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001804 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001805 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001806
1807 return 0;
Alex Deucher83ba1262016-06-03 18:21:41 -04001808
1809failed:
1810 if (runtime)
1811 vga_switcheroo_fini_domain_pm_ops(adev->dev);
1812 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001813}
1814
1815static void amdgpu_debugfs_remove_files(struct amdgpu_device *adev);
1816
1817/**
1818 * amdgpu_device_fini - tear down the driver
1819 *
1820 * @adev: amdgpu_device pointer
1821 *
1822 * Tear down the driver info (all asics).
1823 * Called at driver shutdown.
1824 */
1825void amdgpu_device_fini(struct amdgpu_device *adev)
1826{
1827 int r;
1828
1829 DRM_INFO("amdgpu: finishing device.\n");
1830 adev->shutdown = true;
1831 /* evict vram memory */
1832 amdgpu_bo_evict_vram(adev);
1833 amdgpu_ib_pool_fini(adev);
1834 amdgpu_fence_driver_fini(adev);
Lukas Wunner84b89bd2016-06-08 18:47:27 +02001835 drm_crtc_force_disable_all(adev->ddev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001836 amdgpu_fbdev_fini(adev);
1837 r = amdgpu_fini(adev);
Alex Deucher8faf0e082015-07-28 11:50:31 -04001838 kfree(adev->ip_block_status);
1839 adev->ip_block_status = NULL;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001840 adev->accel_working = false;
1841 /* free i2c buses */
1842 amdgpu_i2c_fini(adev);
1843 amdgpu_atombios_fini(adev);
1844 kfree(adev->bios);
1845 adev->bios = NULL;
1846 vga_switcheroo_unregister_client(adev->pdev);
Alex Deucher83ba1262016-06-03 18:21:41 -04001847 if (adev->flags & AMD_IS_PX)
1848 vga_switcheroo_fini_domain_pm_ops(adev->dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001849 vga_client_register(adev->pdev, NULL, NULL, NULL);
1850 if (adev->rio_mem)
1851 pci_iounmap(adev->pdev, adev->rio_mem);
1852 adev->rio_mem = NULL;
1853 iounmap(adev->rmmio);
1854 adev->rmmio = NULL;
Ken Wangda69c1612016-01-21 19:08:55 +08001855 if (adev->asic_type >= CHIP_BONAIRE)
1856 amdgpu_doorbell_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001857 amdgpu_debugfs_regs_cleanup(adev);
1858 amdgpu_debugfs_remove_files(adev);
1859}
1860
1861
1862/*
1863 * Suspend & resume.
1864 */
1865/**
Alex Deucher810ddc32016-08-23 13:25:49 -04001866 * amdgpu_device_suspend - initiate device suspend
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001867 *
1868 * @pdev: drm dev pointer
1869 * @state: suspend state
1870 *
1871 * Puts the hw in the suspend state (all asics).
1872 * Returns 0 for success or an error on failure.
1873 * Called at driver suspend.
1874 */
Alex Deucher810ddc32016-08-23 13:25:49 -04001875int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001876{
1877 struct amdgpu_device *adev;
1878 struct drm_crtc *crtc;
1879 struct drm_connector *connector;
Alex Deucher5ceb54c2015-08-05 12:41:48 -04001880 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001881
1882 if (dev == NULL || dev->dev_private == NULL) {
1883 return -ENODEV;
1884 }
1885
1886 adev = dev->dev_private;
1887
Alex Deuchere313de72016-09-19 12:17:22 -04001888 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001889 return 0;
1890
1891 drm_kms_helper_poll_disable(dev);
1892
1893 /* turn off display hw */
Alex Deucher4c7fbc32015-09-23 14:32:06 -04001894 drm_modeset_lock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001895 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
1896 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
1897 }
Alex Deucher4c7fbc32015-09-23 14:32:06 -04001898 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001899
Alex Deucher756e6882015-10-08 00:03:36 -04001900 /* unpin the front buffers and cursors */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001901 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Alex Deucher756e6882015-10-08 00:03:36 -04001902 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001903 struct amdgpu_framebuffer *rfb = to_amdgpu_framebuffer(crtc->primary->fb);
1904 struct amdgpu_bo *robj;
1905
Alex Deucher756e6882015-10-08 00:03:36 -04001906 if (amdgpu_crtc->cursor_bo) {
1907 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
1908 r = amdgpu_bo_reserve(aobj, false);
1909 if (r == 0) {
1910 amdgpu_bo_unpin(aobj);
1911 amdgpu_bo_unreserve(aobj);
1912 }
1913 }
1914
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001915 if (rfb == NULL || rfb->obj == NULL) {
1916 continue;
1917 }
1918 robj = gem_to_amdgpu_bo(rfb->obj);
1919 /* don't unpin kernel fb objects */
1920 if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
1921 r = amdgpu_bo_reserve(robj, false);
1922 if (r == 0) {
1923 amdgpu_bo_unpin(robj);
1924 amdgpu_bo_unreserve(robj);
1925 }
1926 }
1927 }
1928 /* evict vram memory */
1929 amdgpu_bo_evict_vram(adev);
1930
Alex Deucher5ceb54c2015-08-05 12:41:48 -04001931 amdgpu_fence_driver_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001932
1933 r = amdgpu_suspend(adev);
1934
1935 /* evict remaining vram memory */
1936 amdgpu_bo_evict_vram(adev);
1937
1938 pci_save_state(dev->pdev);
1939 if (suspend) {
1940 /* Shut down the device */
1941 pci_disable_device(dev->pdev);
1942 pci_set_power_state(dev->pdev, PCI_D3hot);
jimqu74b0b152016-09-07 17:09:12 +08001943 } else {
1944 r = amdgpu_asic_reset(adev);
1945 if (r)
1946 DRM_ERROR("amdgpu asic reset failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001947 }
1948
1949 if (fbcon) {
1950 console_lock();
1951 amdgpu_fbdev_set_suspend(adev, 1);
1952 console_unlock();
1953 }
1954 return 0;
1955}
1956
1957/**
Alex Deucher810ddc32016-08-23 13:25:49 -04001958 * amdgpu_device_resume - initiate device resume
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001959 *
1960 * @pdev: drm dev pointer
1961 *
1962 * Bring the hw back to operating state (all asics).
1963 * Returns 0 for success or an error on failure.
1964 * Called at driver resume.
1965 */
Alex Deucher810ddc32016-08-23 13:25:49 -04001966int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001967{
1968 struct drm_connector *connector;
1969 struct amdgpu_device *adev = dev->dev_private;
Alex Deucher756e6882015-10-08 00:03:36 -04001970 struct drm_crtc *crtc;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001971 int r;
1972
Alex Deuchere313de72016-09-19 12:17:22 -04001973 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001974 return 0;
1975
jimqu74b0b152016-09-07 17:09:12 +08001976 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001977 console_lock();
jimqu74b0b152016-09-07 17:09:12 +08001978
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001979 if (resume) {
1980 pci_set_power_state(dev->pdev, PCI_D0);
1981 pci_restore_state(dev->pdev);
jimqu74b0b152016-09-07 17:09:12 +08001982 r = pci_enable_device(dev->pdev);
1983 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001984 if (fbcon)
1985 console_unlock();
jimqu74b0b152016-09-07 17:09:12 +08001986 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001987 }
1988 }
1989
1990 /* post card */
jimqu74b0b152016-09-07 17:09:12 +08001991 if (!amdgpu_card_posted(adev) || !resume) {
1992 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
1993 if (r)
1994 DRM_ERROR("amdgpu asic init failed\n");
1995 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001996
1997 r = amdgpu_resume(adev);
Flora Cuica198522016-02-04 15:10:08 +08001998 if (r)
1999 DRM_ERROR("amdgpu_resume failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002000
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002001 amdgpu_fence_driver_resume(adev);
2002
Flora Cuica198522016-02-04 15:10:08 +08002003 if (resume) {
2004 r = amdgpu_ib_ring_tests(adev);
2005 if (r)
2006 DRM_ERROR("ib ring test failed (%d).\n", r);
2007 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002008
2009 r = amdgpu_late_init(adev);
2010 if (r)
2011 return r;
2012
Alex Deucher756e6882015-10-08 00:03:36 -04002013 /* pin cursors */
2014 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2015 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2016
2017 if (amdgpu_crtc->cursor_bo) {
2018 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
2019 r = amdgpu_bo_reserve(aobj, false);
2020 if (r == 0) {
2021 r = amdgpu_bo_pin(aobj,
2022 AMDGPU_GEM_DOMAIN_VRAM,
2023 &amdgpu_crtc->cursor_addr);
2024 if (r != 0)
2025 DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
2026 amdgpu_bo_unreserve(aobj);
2027 }
2028 }
2029 }
2030
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002031 /* blat the mode back in */
2032 if (fbcon) {
2033 drm_helper_resume_force_mode(dev);
2034 /* turn on display hw */
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002035 drm_modeset_lock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002036 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2037 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
2038 }
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002039 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002040 }
2041
2042 drm_kms_helper_poll_enable(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002043
2044 /*
2045 * Most of the connector probing functions try to acquire runtime pm
2046 * refs to ensure that the GPU is powered on when connector polling is
2047 * performed. Since we're calling this from a runtime PM callback,
2048 * trying to acquire rpm refs will cause us to deadlock.
2049 *
2050 * Since we're guaranteed to be holding the rpm lock, it's safe to
2051 * temporarily disable the rpm helpers so this doesn't deadlock us.
2052 */
2053#ifdef CONFIG_PM
2054 dev->dev->power.disable_depth++;
2055#endif
Alex Deucher54fb2a52015-11-24 14:30:56 -05002056 drm_helper_hpd_irq_event(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002057#ifdef CONFIG_PM
2058 dev->dev->power.disable_depth--;
2059#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002060
2061 if (fbcon) {
2062 amdgpu_fbdev_set_suspend(adev, 0);
2063 console_unlock();
2064 }
2065
2066 return 0;
2067}
2068
Chunming Zhou63fbf422016-07-15 11:19:20 +08002069static bool amdgpu_check_soft_reset(struct amdgpu_device *adev)
2070{
2071 int i;
2072 bool asic_hang = false;
2073
2074 for (i = 0; i < adev->num_ip_blocks; i++) {
2075 if (!adev->ip_block_status[i].valid)
2076 continue;
2077 if (adev->ip_blocks[i].funcs->check_soft_reset)
2078 adev->ip_blocks[i].funcs->check_soft_reset(adev);
2079 if (adev->ip_block_status[i].hang) {
2080 DRM_INFO("IP block:%d is hang!\n", i);
2081 asic_hang = true;
2082 }
2083 }
2084 return asic_hang;
2085}
2086
Baoyou Xie4d446652016-09-18 22:09:35 +08002087static int amdgpu_pre_soft_reset(struct amdgpu_device *adev)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002088{
2089 int i, r = 0;
2090
2091 for (i = 0; i < adev->num_ip_blocks; i++) {
2092 if (!adev->ip_block_status[i].valid)
2093 continue;
Chunming Zhou35d782f2016-07-15 15:57:13 +08002094 if (adev->ip_block_status[i].hang &&
2095 adev->ip_blocks[i].funcs->pre_soft_reset) {
Chunming Zhoud31a5012016-07-18 10:04:34 +08002096 r = adev->ip_blocks[i].funcs->pre_soft_reset(adev);
2097 if (r)
2098 return r;
2099 }
2100 }
2101
2102 return 0;
2103}
2104
Chunming Zhou35d782f2016-07-15 15:57:13 +08002105static bool amdgpu_need_full_reset(struct amdgpu_device *adev)
2106{
2107 if (adev->ip_block_status[AMD_IP_BLOCK_TYPE_GMC].hang ||
Chunming Zhou35d782f2016-07-15 15:57:13 +08002108 adev->ip_block_status[AMD_IP_BLOCK_TYPE_SMC].hang ||
Chunming Zhou35d782f2016-07-15 15:57:13 +08002109 adev->ip_block_status[AMD_IP_BLOCK_TYPE_ACP].hang ||
2110 adev->ip_block_status[AMD_IP_BLOCK_TYPE_DCE].hang) {
2111 DRM_INFO("Some block need full reset!\n");
2112 return true;
2113 }
2114 return false;
2115}
2116
2117static int amdgpu_soft_reset(struct amdgpu_device *adev)
2118{
2119 int i, r = 0;
2120
2121 for (i = 0; i < adev->num_ip_blocks; i++) {
2122 if (!adev->ip_block_status[i].valid)
2123 continue;
2124 if (adev->ip_block_status[i].hang &&
2125 adev->ip_blocks[i].funcs->soft_reset) {
2126 r = adev->ip_blocks[i].funcs->soft_reset(adev);
2127 if (r)
2128 return r;
2129 }
2130 }
2131
2132 return 0;
2133}
2134
2135static int amdgpu_post_soft_reset(struct amdgpu_device *adev)
2136{
2137 int i, r = 0;
2138
2139 for (i = 0; i < adev->num_ip_blocks; i++) {
2140 if (!adev->ip_block_status[i].valid)
2141 continue;
2142 if (adev->ip_block_status[i].hang &&
2143 adev->ip_blocks[i].funcs->post_soft_reset)
2144 r = adev->ip_blocks[i].funcs->post_soft_reset(adev);
2145 if (r)
2146 return r;
2147 }
2148
2149 return 0;
2150}
2151
Chunming Zhou3ad81f12016-08-05 17:30:17 +08002152bool amdgpu_need_backup(struct amdgpu_device *adev)
2153{
2154 if (adev->flags & AMD_IS_APU)
2155 return false;
2156
2157 return amdgpu_lockup_timeout > 0 ? true : false;
2158}
2159
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002160static int amdgpu_recover_vram_from_shadow(struct amdgpu_device *adev,
2161 struct amdgpu_ring *ring,
2162 struct amdgpu_bo *bo,
2163 struct fence **fence)
2164{
2165 uint32_t domain;
2166 int r;
2167
2168 if (!bo->shadow)
2169 return 0;
2170
2171 r = amdgpu_bo_reserve(bo, false);
2172 if (r)
2173 return r;
2174 domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
2175 /* if bo has been evicted, then no need to recover */
2176 if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
2177 r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
2178 NULL, fence, true);
2179 if (r) {
2180 DRM_ERROR("recover page table failed!\n");
2181 goto err;
2182 }
2183 }
2184err:
2185 amdgpu_bo_unreserve(bo);
2186 return r;
2187}
2188
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002189/**
2190 * amdgpu_gpu_reset - reset the asic
2191 *
2192 * @adev: amdgpu device pointer
2193 *
2194 * Attempt the reset the GPU if it has hung (all asics).
2195 * Returns 0 for success or an error on failure.
2196 */
2197int amdgpu_gpu_reset(struct amdgpu_device *adev)
2198{
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002199 int i, r;
2200 int resched;
Chunming Zhou35d782f2016-07-15 15:57:13 +08002201 bool need_full_reset;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002202
Chunming Zhou63fbf422016-07-15 11:19:20 +08002203 if (!amdgpu_check_soft_reset(adev)) {
2204 DRM_INFO("No hardware hang detected. Did some blocks stall?\n");
2205 return 0;
2206 }
2207
Marek Olšákd94aed52015-05-05 21:13:49 +02002208 atomic_inc(&adev->gpu_reset_counter);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002209
Chunming Zhoua3c47d62016-06-30 16:44:41 +08002210 /* block TTM */
2211 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
2212
Chunming Zhou0875dc92016-06-12 15:41:58 +08002213 /* block scheduler */
2214 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2215 struct amdgpu_ring *ring = adev->rings[i];
2216
2217 if (!ring)
2218 continue;
2219 kthread_park(ring->sched.thread);
Chunming Zhouaa1c8902016-06-30 13:56:02 +08002220 amd_sched_hw_job_reset(&ring->sched);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002221 }
Chunming Zhou2200eda2016-06-30 16:53:02 +08002222 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
2223 amdgpu_fence_driver_force_completion(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002224
Chunming Zhou35d782f2016-07-15 15:57:13 +08002225 need_full_reset = amdgpu_need_full_reset(adev);
2226
2227 if (!need_full_reset) {
2228 amdgpu_pre_soft_reset(adev);
2229 r = amdgpu_soft_reset(adev);
2230 amdgpu_post_soft_reset(adev);
2231 if (r || amdgpu_check_soft_reset(adev)) {
2232 DRM_INFO("soft reset failed, will fallback to full reset!\n");
2233 need_full_reset = true;
2234 }
2235 }
2236
2237 if (need_full_reset) {
2238 /* save scratch */
2239 amdgpu_atombios_scratch_regs_save(adev);
2240 r = amdgpu_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002241
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002242retry:
Chunming Zhou35d782f2016-07-15 15:57:13 +08002243 /* Disable fb access */
2244 if (adev->mode_info.num_crtc) {
2245 struct amdgpu_mode_mc_save save;
2246 amdgpu_display_stop_mc_access(adev, &save);
2247 amdgpu_wait_for_idle(adev, AMD_IP_BLOCK_TYPE_GMC);
2248 }
Chunming Zhouf1aa7e02016-06-28 10:38:50 +08002249
Chunming Zhou35d782f2016-07-15 15:57:13 +08002250 r = amdgpu_asic_reset(adev);
2251 /* post card */
2252 amdgpu_atom_asic_init(adev->mode_info.atom_context);
Alex Deucherbfa99262016-01-15 11:59:48 -05002253
Chunming Zhou35d782f2016-07-15 15:57:13 +08002254 if (!r) {
2255 dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
2256 r = amdgpu_resume(adev);
2257 }
2258 /* restore scratch */
2259 amdgpu_atombios_scratch_regs_restore(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002260 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002261 if (!r) {
Chunming Zhoue72cfd52016-07-27 13:15:20 +08002262 amdgpu_irq_gpu_reset_resume_helper(adev);
Chunming Zhou2c0d7312016-08-30 16:36:25 +08002263 if (need_full_reset && amdgpu_need_backup(adev)) {
2264 r = amdgpu_ttm_recover_gart(adev);
2265 if (r)
2266 DRM_ERROR("gart recovery failed!!!\n");
2267 }
Chunming Zhou1f465082016-06-30 15:02:26 +08002268 r = amdgpu_ib_ring_tests(adev);
2269 if (r) {
2270 dev_err(adev->dev, "ib ring test failed (%d).\n", r);
Chunming Zhou40019dc2016-06-29 16:01:49 +08002271 r = amdgpu_suspend(adev);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002272 need_full_reset = true;
Chunming Zhou40019dc2016-06-29 16:01:49 +08002273 goto retry;
Chunming Zhou1f465082016-06-30 15:02:26 +08002274 }
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002275 /**
2276 * recovery vm page tables, since we cannot depend on VRAM is
2277 * consistent after gpu full reset.
2278 */
2279 if (need_full_reset && amdgpu_need_backup(adev)) {
2280 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
2281 struct amdgpu_bo *bo, *tmp;
2282 struct fence *fence = NULL, *next = NULL;
Chunming Zhou1f465082016-06-30 15:02:26 +08002283
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002284 DRM_INFO("recover vram bo from shadow\n");
2285 mutex_lock(&adev->shadow_list_lock);
2286 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
2287 amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
2288 if (fence) {
2289 r = fence_wait(fence, false);
2290 if (r) {
2291 WARN(r, "recovery from shadow isn't comleted\n");
2292 break;
2293 }
2294 }
2295
2296 fence_put(fence);
2297 fence = next;
2298 }
2299 mutex_unlock(&adev->shadow_list_lock);
2300 if (fence) {
2301 r = fence_wait(fence, false);
2302 if (r)
2303 WARN(r, "recovery from shadow isn't comleted\n");
2304 }
2305 fence_put(fence);
2306 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002307 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2308 struct amdgpu_ring *ring = adev->rings[i];
2309 if (!ring)
2310 continue;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002311
Chunming Zhouaa1c8902016-06-30 13:56:02 +08002312 amd_sched_job_recovery(&ring->sched);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002313 kthread_unpark(ring->sched.thread);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002314 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002315 } else {
Chunming Zhou2200eda2016-06-30 16:53:02 +08002316 dev_err(adev->dev, "asic resume failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002317 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
Chunming Zhou0875dc92016-06-12 15:41:58 +08002318 if (adev->rings[i]) {
2319 kthread_unpark(adev->rings[i]->sched.thread);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002320 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002321 }
2322 }
2323
2324 drm_helper_resume_force_mode(adev->ddev);
2325
2326 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
2327 if (r) {
2328 /* bad news, how to tell it to userspace ? */
2329 dev_info(adev->dev, "GPU reset failed\n");
2330 }
2331
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002332 return r;
2333}
2334
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002335void amdgpu_get_pcie_info(struct amdgpu_device *adev)
2336{
2337 u32 mask;
2338 int ret;
2339
Alex Deuchercd474ba2016-02-04 10:21:23 -05002340 if (amdgpu_pcie_gen_cap)
2341 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
2342
2343 if (amdgpu_pcie_lane_cap)
2344 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
2345
2346 /* covers APUs as well */
2347 if (pci_is_root_bus(adev->pdev->bus)) {
2348 if (adev->pm.pcie_gen_mask == 0)
2349 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
2350 if (adev->pm.pcie_mlw_mask == 0)
2351 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002352 return;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002353 }
Alex Deuchercd474ba2016-02-04 10:21:23 -05002354
2355 if (adev->pm.pcie_gen_mask == 0) {
2356 ret = drm_pcie_get_speed_cap_mask(adev->ddev, &mask);
2357 if (!ret) {
2358 adev->pm.pcie_gen_mask = (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
2359 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
2360 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
2361
2362 if (mask & DRM_PCIE_SPEED_25)
2363 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
2364 if (mask & DRM_PCIE_SPEED_50)
2365 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2;
2366 if (mask & DRM_PCIE_SPEED_80)
2367 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3;
2368 } else {
2369 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
2370 }
2371 }
2372 if (adev->pm.pcie_mlw_mask == 0) {
2373 ret = drm_pcie_get_max_link_width(adev->ddev, &mask);
2374 if (!ret) {
2375 switch (mask) {
2376 case 32:
2377 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
2378 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
2379 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2380 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2381 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2382 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2383 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2384 break;
2385 case 16:
2386 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
2387 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2388 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2389 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2390 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2391 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2392 break;
2393 case 12:
2394 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2395 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2396 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2397 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2398 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2399 break;
2400 case 8:
2401 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2402 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2403 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2404 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2405 break;
2406 case 4:
2407 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2408 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2409 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2410 break;
2411 case 2:
2412 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2413 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2414 break;
2415 case 1:
2416 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
2417 break;
2418 default:
2419 break;
2420 }
2421 } else {
2422 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002423 }
2424 }
2425}
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002426
2427/*
2428 * Debugfs
2429 */
2430int amdgpu_debugfs_add_files(struct amdgpu_device *adev,
Nils Wallménius06ab6832016-05-02 12:46:15 -04002431 const struct drm_info_list *files,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002432 unsigned nfiles)
2433{
2434 unsigned i;
2435
2436 for (i = 0; i < adev->debugfs_count; i++) {
2437 if (adev->debugfs[i].files == files) {
2438 /* Already registered */
2439 return 0;
2440 }
2441 }
2442
2443 i = adev->debugfs_count + 1;
2444 if (i > AMDGPU_DEBUGFS_MAX_COMPONENTS) {
2445 DRM_ERROR("Reached maximum number of debugfs components.\n");
2446 DRM_ERROR("Report so we increase "
2447 "AMDGPU_DEBUGFS_MAX_COMPONENTS.\n");
2448 return -EINVAL;
2449 }
2450 adev->debugfs[adev->debugfs_count].files = files;
2451 adev->debugfs[adev->debugfs_count].num_files = nfiles;
2452 adev->debugfs_count = i;
2453#if defined(CONFIG_DEBUG_FS)
2454 drm_debugfs_create_files(files, nfiles,
2455 adev->ddev->control->debugfs_root,
2456 adev->ddev->control);
2457 drm_debugfs_create_files(files, nfiles,
2458 adev->ddev->primary->debugfs_root,
2459 adev->ddev->primary);
2460#endif
2461 return 0;
2462}
2463
2464static void amdgpu_debugfs_remove_files(struct amdgpu_device *adev)
2465{
2466#if defined(CONFIG_DEBUG_FS)
2467 unsigned i;
2468
2469 for (i = 0; i < adev->debugfs_count; i++) {
2470 drm_debugfs_remove_files(adev->debugfs[i].files,
2471 adev->debugfs[i].num_files,
2472 adev->ddev->control);
2473 drm_debugfs_remove_files(adev->debugfs[i].files,
2474 adev->debugfs[i].num_files,
2475 adev->ddev->primary);
2476 }
2477#endif
2478}
2479
2480#if defined(CONFIG_DEBUG_FS)
2481
2482static ssize_t amdgpu_debugfs_regs_read(struct file *f, char __user *buf,
2483 size_t size, loff_t *pos)
2484{
2485 struct amdgpu_device *adev = f->f_inode->i_private;
2486 ssize_t result = 0;
2487 int r;
Tom St Denisbd122672016-07-28 09:39:22 -04002488 bool pm_pg_lock, use_bank;
Tom St Denis566281592016-06-27 11:55:07 -04002489 unsigned instance_bank, sh_bank, se_bank;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002490
2491 if (size & 0x3 || *pos & 0x3)
2492 return -EINVAL;
2493
Tom St Denisbd122672016-07-28 09:39:22 -04002494 /* are we reading registers for which a PG lock is necessary? */
2495 pm_pg_lock = (*pos >> 23) & 1;
2496
Tom St Denis566281592016-06-27 11:55:07 -04002497 if (*pos & (1ULL << 62)) {
2498 se_bank = (*pos >> 24) & 0x3FF;
2499 sh_bank = (*pos >> 34) & 0x3FF;
2500 instance_bank = (*pos >> 44) & 0x3FF;
2501 use_bank = 1;
Tom St Denis566281592016-06-27 11:55:07 -04002502 } else {
2503 use_bank = 0;
2504 }
2505
Tom St Denisbd122672016-07-28 09:39:22 -04002506 *pos &= 0x3FFFF;
2507
Tom St Denis566281592016-06-27 11:55:07 -04002508 if (use_bank) {
2509 if (sh_bank >= adev->gfx.config.max_sh_per_se ||
2510 se_bank >= adev->gfx.config.max_shader_engines)
2511 return -EINVAL;
2512 mutex_lock(&adev->grbm_idx_mutex);
2513 amdgpu_gfx_select_se_sh(adev, se_bank,
2514 sh_bank, instance_bank);
2515 }
2516
Tom St Denisbd122672016-07-28 09:39:22 -04002517 if (pm_pg_lock)
2518 mutex_lock(&adev->pm.mutex);
2519
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002520 while (size) {
2521 uint32_t value;
2522
2523 if (*pos > adev->rmmio_size)
Tom St Denis566281592016-06-27 11:55:07 -04002524 goto end;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002525
2526 value = RREG32(*pos >> 2);
2527 r = put_user(value, (uint32_t *)buf);
Tom St Denis566281592016-06-27 11:55:07 -04002528 if (r) {
2529 result = r;
2530 goto end;
2531 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002532
2533 result += 4;
2534 buf += 4;
2535 *pos += 4;
2536 size -= 4;
2537 }
2538
Tom St Denis566281592016-06-27 11:55:07 -04002539end:
2540 if (use_bank) {
2541 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
2542 mutex_unlock(&adev->grbm_idx_mutex);
2543 }
2544
Tom St Denisbd122672016-07-28 09:39:22 -04002545 if (pm_pg_lock)
2546 mutex_unlock(&adev->pm.mutex);
2547
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002548 return result;
2549}
2550
2551static ssize_t amdgpu_debugfs_regs_write(struct file *f, const char __user *buf,
2552 size_t size, loff_t *pos)
2553{
2554 struct amdgpu_device *adev = f->f_inode->i_private;
2555 ssize_t result = 0;
2556 int r;
2557
2558 if (size & 0x3 || *pos & 0x3)
2559 return -EINVAL;
2560
2561 while (size) {
2562 uint32_t value;
2563
2564 if (*pos > adev->rmmio_size)
2565 return result;
2566
2567 r = get_user(value, (uint32_t *)buf);
2568 if (r)
2569 return r;
2570
2571 WREG32(*pos >> 2, value);
2572
2573 result += 4;
2574 buf += 4;
2575 *pos += 4;
2576 size -= 4;
2577 }
2578
2579 return result;
2580}
2581
Tom St Denisadcec282016-04-15 13:08:44 -04002582static ssize_t amdgpu_debugfs_regs_pcie_read(struct file *f, char __user *buf,
2583 size_t size, loff_t *pos)
2584{
2585 struct amdgpu_device *adev = f->f_inode->i_private;
2586 ssize_t result = 0;
2587 int r;
2588
2589 if (size & 0x3 || *pos & 0x3)
2590 return -EINVAL;
2591
2592 while (size) {
2593 uint32_t value;
2594
2595 value = RREG32_PCIE(*pos >> 2);
2596 r = put_user(value, (uint32_t *)buf);
2597 if (r)
2598 return r;
2599
2600 result += 4;
2601 buf += 4;
2602 *pos += 4;
2603 size -= 4;
2604 }
2605
2606 return result;
2607}
2608
2609static ssize_t amdgpu_debugfs_regs_pcie_write(struct file *f, const char __user *buf,
2610 size_t size, loff_t *pos)
2611{
2612 struct amdgpu_device *adev = f->f_inode->i_private;
2613 ssize_t result = 0;
2614 int r;
2615
2616 if (size & 0x3 || *pos & 0x3)
2617 return -EINVAL;
2618
2619 while (size) {
2620 uint32_t value;
2621
2622 r = get_user(value, (uint32_t *)buf);
2623 if (r)
2624 return r;
2625
2626 WREG32_PCIE(*pos >> 2, value);
2627
2628 result += 4;
2629 buf += 4;
2630 *pos += 4;
2631 size -= 4;
2632 }
2633
2634 return result;
2635}
2636
2637static ssize_t amdgpu_debugfs_regs_didt_read(struct file *f, char __user *buf,
2638 size_t size, loff_t *pos)
2639{
2640 struct amdgpu_device *adev = f->f_inode->i_private;
2641 ssize_t result = 0;
2642 int r;
2643
2644 if (size & 0x3 || *pos & 0x3)
2645 return -EINVAL;
2646
2647 while (size) {
2648 uint32_t value;
2649
2650 value = RREG32_DIDT(*pos >> 2);
2651 r = put_user(value, (uint32_t *)buf);
2652 if (r)
2653 return r;
2654
2655 result += 4;
2656 buf += 4;
2657 *pos += 4;
2658 size -= 4;
2659 }
2660
2661 return result;
2662}
2663
2664static ssize_t amdgpu_debugfs_regs_didt_write(struct file *f, const char __user *buf,
2665 size_t size, loff_t *pos)
2666{
2667 struct amdgpu_device *adev = f->f_inode->i_private;
2668 ssize_t result = 0;
2669 int r;
2670
2671 if (size & 0x3 || *pos & 0x3)
2672 return -EINVAL;
2673
2674 while (size) {
2675 uint32_t value;
2676
2677 r = get_user(value, (uint32_t *)buf);
2678 if (r)
2679 return r;
2680
2681 WREG32_DIDT(*pos >> 2, value);
2682
2683 result += 4;
2684 buf += 4;
2685 *pos += 4;
2686 size -= 4;
2687 }
2688
2689 return result;
2690}
2691
2692static ssize_t amdgpu_debugfs_regs_smc_read(struct file *f, char __user *buf,
2693 size_t size, loff_t *pos)
2694{
2695 struct amdgpu_device *adev = f->f_inode->i_private;
2696 ssize_t result = 0;
2697 int r;
2698
2699 if (size & 0x3 || *pos & 0x3)
2700 return -EINVAL;
2701
2702 while (size) {
2703 uint32_t value;
2704
Tom St Denis6fc0dea2016-08-29 08:39:29 -04002705 value = RREG32_SMC(*pos);
Tom St Denisadcec282016-04-15 13:08:44 -04002706 r = put_user(value, (uint32_t *)buf);
2707 if (r)
2708 return r;
2709
2710 result += 4;
2711 buf += 4;
2712 *pos += 4;
2713 size -= 4;
2714 }
2715
2716 return result;
2717}
2718
2719static ssize_t amdgpu_debugfs_regs_smc_write(struct file *f, const char __user *buf,
2720 size_t size, loff_t *pos)
2721{
2722 struct amdgpu_device *adev = f->f_inode->i_private;
2723 ssize_t result = 0;
2724 int r;
2725
2726 if (size & 0x3 || *pos & 0x3)
2727 return -EINVAL;
2728
2729 while (size) {
2730 uint32_t value;
2731
2732 r = get_user(value, (uint32_t *)buf);
2733 if (r)
2734 return r;
2735
Tom St Denis6fc0dea2016-08-29 08:39:29 -04002736 WREG32_SMC(*pos, value);
Tom St Denisadcec282016-04-15 13:08:44 -04002737
2738 result += 4;
2739 buf += 4;
2740 *pos += 4;
2741 size -= 4;
2742 }
2743
2744 return result;
2745}
2746
Tom St Denis1e051412016-06-27 09:57:18 -04002747static ssize_t amdgpu_debugfs_gca_config_read(struct file *f, char __user *buf,
2748 size_t size, loff_t *pos)
2749{
2750 struct amdgpu_device *adev = f->f_inode->i_private;
2751 ssize_t result = 0;
2752 int r;
2753 uint32_t *config, no_regs = 0;
2754
2755 if (size & 0x3 || *pos & 0x3)
2756 return -EINVAL;
2757
Markus Elfringecab7662016-09-18 17:00:52 +02002758 config = kmalloc_array(256, sizeof(*config), GFP_KERNEL);
Tom St Denis1e051412016-06-27 09:57:18 -04002759 if (!config)
2760 return -ENOMEM;
2761
2762 /* version, increment each time something is added */
Tom St Denise9f11dc2016-08-17 12:00:51 -04002763 config[no_regs++] = 2;
Tom St Denis1e051412016-06-27 09:57:18 -04002764 config[no_regs++] = adev->gfx.config.max_shader_engines;
2765 config[no_regs++] = adev->gfx.config.max_tile_pipes;
2766 config[no_regs++] = adev->gfx.config.max_cu_per_sh;
2767 config[no_regs++] = adev->gfx.config.max_sh_per_se;
2768 config[no_regs++] = adev->gfx.config.max_backends_per_se;
2769 config[no_regs++] = adev->gfx.config.max_texture_channel_caches;
2770 config[no_regs++] = adev->gfx.config.max_gprs;
2771 config[no_regs++] = adev->gfx.config.max_gs_threads;
2772 config[no_regs++] = adev->gfx.config.max_hw_contexts;
2773 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_frontend;
2774 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_backend;
2775 config[no_regs++] = adev->gfx.config.sc_hiz_tile_fifo_size;
2776 config[no_regs++] = adev->gfx.config.sc_earlyz_tile_fifo_size;
2777 config[no_regs++] = adev->gfx.config.num_tile_pipes;
2778 config[no_regs++] = adev->gfx.config.backend_enable_mask;
2779 config[no_regs++] = adev->gfx.config.mem_max_burst_length_bytes;
2780 config[no_regs++] = adev->gfx.config.mem_row_size_in_kb;
2781 config[no_regs++] = adev->gfx.config.shader_engine_tile_size;
2782 config[no_regs++] = adev->gfx.config.num_gpus;
2783 config[no_regs++] = adev->gfx.config.multi_gpu_tile_size;
2784 config[no_regs++] = adev->gfx.config.mc_arb_ramcfg;
2785 config[no_regs++] = adev->gfx.config.gb_addr_config;
2786 config[no_regs++] = adev->gfx.config.num_rbs;
2787
Tom St Denis89a8f302016-08-12 15:14:31 -04002788 /* rev==1 */
2789 config[no_regs++] = adev->rev_id;
2790 config[no_regs++] = adev->pg_flags;
2791 config[no_regs++] = adev->cg_flags;
2792
Tom St Denise9f11dc2016-08-17 12:00:51 -04002793 /* rev==2 */
2794 config[no_regs++] = adev->family;
2795 config[no_regs++] = adev->external_rev_id;
2796
Tom St Denis1e051412016-06-27 09:57:18 -04002797 while (size && (*pos < no_regs * 4)) {
2798 uint32_t value;
2799
2800 value = config[*pos >> 2];
2801 r = put_user(value, (uint32_t *)buf);
2802 if (r) {
2803 kfree(config);
2804 return r;
2805 }
2806
2807 result += 4;
2808 buf += 4;
2809 *pos += 4;
2810 size -= 4;
2811 }
2812
2813 kfree(config);
2814 return result;
2815}
2816
Tom St Denisf2cdaf22016-09-15 10:08:44 -04002817static ssize_t amdgpu_debugfs_sensor_read(struct file *f, char __user *buf,
2818 size_t size, loff_t *pos)
2819{
2820 struct amdgpu_device *adev = f->f_inode->i_private;
2821 int idx, r;
2822 int32_t value;
2823
2824 if (size != 4 || *pos & 0x3)
2825 return -EINVAL;
2826
2827 /* convert offset to sensor number */
2828 idx = *pos >> 2;
2829
2830 if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->read_sensor)
2831 r = adev->powerplay.pp_funcs->read_sensor(adev->powerplay.pp_handle, idx, &value);
2832 else
2833 return -EINVAL;
2834
2835 if (!r)
2836 r = put_user(value, (int32_t *)buf);
2837
2838 return !r ? 4 : r;
2839}
Tom St Denis1e051412016-06-27 09:57:18 -04002840
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002841static const struct file_operations amdgpu_debugfs_regs_fops = {
2842 .owner = THIS_MODULE,
2843 .read = amdgpu_debugfs_regs_read,
2844 .write = amdgpu_debugfs_regs_write,
2845 .llseek = default_llseek
2846};
Tom St Denisadcec282016-04-15 13:08:44 -04002847static const struct file_operations amdgpu_debugfs_regs_didt_fops = {
2848 .owner = THIS_MODULE,
2849 .read = amdgpu_debugfs_regs_didt_read,
2850 .write = amdgpu_debugfs_regs_didt_write,
2851 .llseek = default_llseek
2852};
2853static const struct file_operations amdgpu_debugfs_regs_pcie_fops = {
2854 .owner = THIS_MODULE,
2855 .read = amdgpu_debugfs_regs_pcie_read,
2856 .write = amdgpu_debugfs_regs_pcie_write,
2857 .llseek = default_llseek
2858};
2859static const struct file_operations amdgpu_debugfs_regs_smc_fops = {
2860 .owner = THIS_MODULE,
2861 .read = amdgpu_debugfs_regs_smc_read,
2862 .write = amdgpu_debugfs_regs_smc_write,
2863 .llseek = default_llseek
2864};
2865
Tom St Denis1e051412016-06-27 09:57:18 -04002866static const struct file_operations amdgpu_debugfs_gca_config_fops = {
2867 .owner = THIS_MODULE,
2868 .read = amdgpu_debugfs_gca_config_read,
2869 .llseek = default_llseek
2870};
2871
Tom St Denisf2cdaf22016-09-15 10:08:44 -04002872static const struct file_operations amdgpu_debugfs_sensors_fops = {
2873 .owner = THIS_MODULE,
2874 .read = amdgpu_debugfs_sensor_read,
2875 .llseek = default_llseek
2876};
2877
Tom St Denisadcec282016-04-15 13:08:44 -04002878static const struct file_operations *debugfs_regs[] = {
2879 &amdgpu_debugfs_regs_fops,
2880 &amdgpu_debugfs_regs_didt_fops,
2881 &amdgpu_debugfs_regs_pcie_fops,
2882 &amdgpu_debugfs_regs_smc_fops,
Tom St Denis1e051412016-06-27 09:57:18 -04002883 &amdgpu_debugfs_gca_config_fops,
Tom St Denisf2cdaf22016-09-15 10:08:44 -04002884 &amdgpu_debugfs_sensors_fops,
Tom St Denisadcec282016-04-15 13:08:44 -04002885};
2886
2887static const char *debugfs_regs_names[] = {
2888 "amdgpu_regs",
2889 "amdgpu_regs_didt",
2890 "amdgpu_regs_pcie",
2891 "amdgpu_regs_smc",
Tom St Denis1e051412016-06-27 09:57:18 -04002892 "amdgpu_gca_config",
Tom St Denisf2cdaf22016-09-15 10:08:44 -04002893 "amdgpu_sensors",
Tom St Denisadcec282016-04-15 13:08:44 -04002894};
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002895
2896static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
2897{
2898 struct drm_minor *minor = adev->ddev->primary;
2899 struct dentry *ent, *root = minor->debugfs_root;
Tom St Denisadcec282016-04-15 13:08:44 -04002900 unsigned i, j;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002901
Tom St Denisadcec282016-04-15 13:08:44 -04002902 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
2903 ent = debugfs_create_file(debugfs_regs_names[i],
2904 S_IFREG | S_IRUGO, root,
2905 adev, debugfs_regs[i]);
2906 if (IS_ERR(ent)) {
2907 for (j = 0; j < i; j++) {
2908 debugfs_remove(adev->debugfs_regs[i]);
2909 adev->debugfs_regs[i] = NULL;
2910 }
2911 return PTR_ERR(ent);
2912 }
2913
2914 if (!i)
2915 i_size_write(ent->d_inode, adev->rmmio_size);
2916 adev->debugfs_regs[i] = ent;
2917 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002918
2919 return 0;
2920}
2921
2922static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev)
2923{
Tom St Denisadcec282016-04-15 13:08:44 -04002924 unsigned i;
2925
2926 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
2927 if (adev->debugfs_regs[i]) {
2928 debugfs_remove(adev->debugfs_regs[i]);
2929 adev->debugfs_regs[i] = NULL;
2930 }
2931 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002932}
2933
2934int amdgpu_debugfs_init(struct drm_minor *minor)
2935{
2936 return 0;
2937}
2938
2939void amdgpu_debugfs_cleanup(struct drm_minor *minor)
2940{
2941}
Alexander Kuleshov7cebc722015-06-27 13:16:05 +06002942#else
2943static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
2944{
2945 return 0;
2946}
2947static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev) { }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002948#endif