blob: 4bf9805b7028a512bf110377d8a46324edfe092c [file] [log] [blame]
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001/*
2 * Copyright 2008 Advanced Micro Devices, Inc.
3 * Copyright 2008 Red Hat Inc.
4 * Copyright 2009 Jerome Glisse.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
20 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22 * OTHER DEALINGS IN THE SOFTWARE.
23 *
24 * Authors: Dave Airlie
25 * Alex Deucher
26 * Jerome Glisse
27 */
Chunming Zhou0875dc92016-06-12 15:41:58 +080028#include <linux/kthread.h>
Alex Deucherd38ceaf2015-04-20 16:55:21 -040029#include <linux/console.h>
30#include <linux/slab.h>
31#include <linux/debugfs.h>
32#include <drm/drmP.h>
33#include <drm/drm_crtc_helper.h>
34#include <drm/amdgpu_drm.h>
35#include <linux/vgaarb.h>
36#include <linux/vga_switcheroo.h>
37#include <linux/efi.h>
38#include "amdgpu.h"
Tom St Denisf4b373f2016-05-31 08:02:27 -040039#include "amdgpu_trace.h"
Alex Deucherd38ceaf2015-04-20 16:55:21 -040040#include "amdgpu_i2c.h"
41#include "atom.h"
42#include "amdgpu_atombios.h"
Alex Deuchera5bde2f2016-09-23 16:23:41 -040043#include "amdgpu_atomfirmware.h"
Alex Deucherd0dd7f02015-11-11 19:45:06 -050044#include "amd_pcie.h"
Ken Wang33f34802016-01-21 17:29:41 +080045#ifdef CONFIG_DRM_AMDGPU_SI
46#include "si.h"
47#endif
Alex Deuchera2e73f52015-04-20 17:09:27 -040048#ifdef CONFIG_DRM_AMDGPU_CIK
49#include "cik.h"
50#endif
Alex Deucheraaa36a92015-04-20 17:31:14 -040051#include "vi.h"
Ken Wang460826e2017-03-06 14:53:16 -050052#include "soc15.h"
Alex Deucherd38ceaf2015-04-20 16:55:21 -040053#include "bif/bif_4_1_d.h"
Emily Deng9accf2f2016-08-10 16:01:25 +080054#include <linux/pci.h>
Monk Liubec86372016-09-14 19:38:08 +080055#include <linux/firmware.h>
Tom St Denisd1aff8e2016-08-09 18:01:55 -040056#include "amdgpu_pm.h"
Alex Deucherd38ceaf2015-04-20 16:55:21 -040057
58static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev);
59static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev);
60
61static const char *amdgpu_asic_name[] = {
Ken Wangda69c1612016-01-21 19:08:55 +080062 "TAHITI",
63 "PITCAIRN",
64 "VERDE",
65 "OLAND",
66 "HAINAN",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040067 "BONAIRE",
68 "KAVERI",
69 "KABINI",
70 "HAWAII",
71 "MULLINS",
72 "TOPAZ",
73 "TONGA",
David Zhang48299f92015-07-08 01:05:16 +080074 "FIJI",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040075 "CARRIZO",
Samuel Li139f4912015-10-08 14:50:27 -040076 "STONEY",
Flora Cui2cc0c0b2016-03-14 18:33:29 -040077 "POLARIS10",
78 "POLARIS11",
Junwei Zhangc4642a42016-12-14 15:32:28 -050079 "POLARIS12",
Ken Wangd4196f02016-03-09 09:28:32 +080080 "VEGA10",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040081 "LAST",
82};
83
84bool amdgpu_device_is_px(struct drm_device *dev)
85{
86 struct amdgpu_device *adev = dev->dev_private;
87
Jammy Zhou2f7d10b2015-07-22 11:29:01 +080088 if (adev->flags & AMD_IS_PX)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040089 return true;
90 return false;
91}
92
93/*
94 * MMIO register access helper functions.
95 */
96uint32_t amdgpu_mm_rreg(struct amdgpu_device *adev, uint32_t reg,
Monk Liu15d72fd2017-01-25 15:07:40 +080097 uint32_t acc_flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040098{
Tom St Denisf4b373f2016-05-31 08:02:27 -040099 uint32_t ret;
100
Monk Liu15d72fd2017-01-25 15:07:40 +0800101 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev)) {
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800102 BUG_ON(in_interrupt());
103 return amdgpu_virt_kiq_rreg(adev, reg);
104 }
105
Monk Liu15d72fd2017-01-25 15:07:40 +0800106 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
Tom St Denisf4b373f2016-05-31 08:02:27 -0400107 ret = readl(((void __iomem *)adev->rmmio) + (reg * 4));
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400108 else {
109 unsigned long flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400110
111 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
112 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
113 ret = readl(((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
114 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400115 }
Tom St Denisf4b373f2016-05-31 08:02:27 -0400116 trace_amdgpu_mm_rreg(adev->pdev->device, reg, ret);
117 return ret;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400118}
119
120void amdgpu_mm_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v,
Monk Liu15d72fd2017-01-25 15:07:40 +0800121 uint32_t acc_flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400122{
Tom St Denisf4b373f2016-05-31 08:02:27 -0400123 trace_amdgpu_mm_wreg(adev->pdev->device, reg, v);
Monk Liu4e99a442016-03-31 13:26:59 +0800124
Monk Liu15d72fd2017-01-25 15:07:40 +0800125 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev)) {
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800126 BUG_ON(in_interrupt());
127 return amdgpu_virt_kiq_wreg(adev, reg, v);
128 }
129
Monk Liu15d72fd2017-01-25 15:07:40 +0800130 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400131 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
132 else {
133 unsigned long flags;
134
135 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
136 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
137 writel(v, ((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
138 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
139 }
140}
141
142u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 reg)
143{
144 if ((reg * 4) < adev->rio_mem_size)
145 return ioread32(adev->rio_mem + (reg * 4));
146 else {
147 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
148 return ioread32(adev->rio_mem + (mmMM_DATA * 4));
149 }
150}
151
152void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
153{
154
155 if ((reg * 4) < adev->rio_mem_size)
156 iowrite32(v, adev->rio_mem + (reg * 4));
157 else {
158 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
159 iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
160 }
161}
162
163/**
164 * amdgpu_mm_rdoorbell - read a doorbell dword
165 *
166 * @adev: amdgpu_device pointer
167 * @index: doorbell index
168 *
169 * Returns the value in the doorbell aperture at the
170 * requested doorbell index (CIK).
171 */
172u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
173{
174 if (index < adev->doorbell.num_doorbells) {
175 return readl(adev->doorbell.ptr + index);
176 } else {
177 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
178 return 0;
179 }
180}
181
182/**
183 * amdgpu_mm_wdoorbell - write a doorbell dword
184 *
185 * @adev: amdgpu_device pointer
186 * @index: doorbell index
187 * @v: value to write
188 *
189 * Writes @v to the doorbell aperture at the
190 * requested doorbell index (CIK).
191 */
192void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
193{
194 if (index < adev->doorbell.num_doorbells) {
195 writel(v, adev->doorbell.ptr + index);
196 } else {
197 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
198 }
199}
200
201/**
Ken Wang832be402016-03-18 15:23:08 +0800202 * amdgpu_mm_rdoorbell64 - read a doorbell Qword
203 *
204 * @adev: amdgpu_device pointer
205 * @index: doorbell index
206 *
207 * Returns the value in the doorbell aperture at the
208 * requested doorbell index (VEGA10+).
209 */
210u64 amdgpu_mm_rdoorbell64(struct amdgpu_device *adev, u32 index)
211{
212 if (index < adev->doorbell.num_doorbells) {
213 return atomic64_read((atomic64_t *)(adev->doorbell.ptr + index));
214 } else {
215 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
216 return 0;
217 }
218}
219
220/**
221 * amdgpu_mm_wdoorbell64 - write a doorbell Qword
222 *
223 * @adev: amdgpu_device pointer
224 * @index: doorbell index
225 * @v: value to write
226 *
227 * Writes @v to the doorbell aperture at the
228 * requested doorbell index (VEGA10+).
229 */
230void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v)
231{
232 if (index < adev->doorbell.num_doorbells) {
233 atomic64_set((atomic64_t *)(adev->doorbell.ptr + index), v);
234 } else {
235 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
236 }
237}
238
239/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400240 * amdgpu_invalid_rreg - dummy reg read function
241 *
242 * @adev: amdgpu device pointer
243 * @reg: offset of register
244 *
245 * Dummy register read function. Used for register blocks
246 * that certain asics don't have (all asics).
247 * Returns the value in the register.
248 */
249static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
250{
251 DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
252 BUG();
253 return 0;
254}
255
256/**
257 * amdgpu_invalid_wreg - dummy reg write function
258 *
259 * @adev: amdgpu device pointer
260 * @reg: offset of register
261 * @v: value to write to the register
262 *
263 * Dummy register read function. Used for register blocks
264 * that certain asics don't have (all asics).
265 */
266static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
267{
268 DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
269 reg, v);
270 BUG();
271}
272
273/**
274 * amdgpu_block_invalid_rreg - dummy reg read function
275 *
276 * @adev: amdgpu device pointer
277 * @block: offset of instance
278 * @reg: offset of register
279 *
280 * Dummy register read function. Used for register blocks
281 * that certain asics don't have (all asics).
282 * Returns the value in the register.
283 */
284static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
285 uint32_t block, uint32_t reg)
286{
287 DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
288 reg, block);
289 BUG();
290 return 0;
291}
292
293/**
294 * amdgpu_block_invalid_wreg - dummy reg write function
295 *
296 * @adev: amdgpu device pointer
297 * @block: offset of instance
298 * @reg: offset of register
299 * @v: value to write to the register
300 *
301 * Dummy register read function. Used for register blocks
302 * that certain asics don't have (all asics).
303 */
304static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
305 uint32_t block,
306 uint32_t reg, uint32_t v)
307{
308 DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
309 reg, block, v);
310 BUG();
311}
312
313static int amdgpu_vram_scratch_init(struct amdgpu_device *adev)
314{
315 int r;
316
317 if (adev->vram_scratch.robj == NULL) {
318 r = amdgpu_bo_create(adev, AMDGPU_GPU_PAGE_SIZE,
Alex Deucher857d9132015-08-27 00:14:16 -0400319 PAGE_SIZE, true, AMDGPU_GEM_DOMAIN_VRAM,
Christian König03f48dd2016-08-15 17:00:22 +0200320 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
321 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS,
Christian König72d76682015-09-03 17:34:59 +0200322 NULL, NULL, &adev->vram_scratch.robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400323 if (r) {
324 return r;
325 }
326 }
327
328 r = amdgpu_bo_reserve(adev->vram_scratch.robj, false);
329 if (unlikely(r != 0))
330 return r;
331 r = amdgpu_bo_pin(adev->vram_scratch.robj,
332 AMDGPU_GEM_DOMAIN_VRAM, &adev->vram_scratch.gpu_addr);
333 if (r) {
334 amdgpu_bo_unreserve(adev->vram_scratch.robj);
335 return r;
336 }
337 r = amdgpu_bo_kmap(adev->vram_scratch.robj,
338 (void **)&adev->vram_scratch.ptr);
339 if (r)
340 amdgpu_bo_unpin(adev->vram_scratch.robj);
341 amdgpu_bo_unreserve(adev->vram_scratch.robj);
342
343 return r;
344}
345
346static void amdgpu_vram_scratch_fini(struct amdgpu_device *adev)
347{
348 int r;
349
350 if (adev->vram_scratch.robj == NULL) {
351 return;
352 }
353 r = amdgpu_bo_reserve(adev->vram_scratch.robj, false);
354 if (likely(r == 0)) {
355 amdgpu_bo_kunmap(adev->vram_scratch.robj);
356 amdgpu_bo_unpin(adev->vram_scratch.robj);
357 amdgpu_bo_unreserve(adev->vram_scratch.robj);
358 }
359 amdgpu_bo_unref(&adev->vram_scratch.robj);
360}
361
362/**
363 * amdgpu_program_register_sequence - program an array of registers.
364 *
365 * @adev: amdgpu_device pointer
366 * @registers: pointer to the register array
367 * @array_size: size of the register array
368 *
369 * Programs an array or registers with and and or masks.
370 * This is a helper for setting golden registers.
371 */
372void amdgpu_program_register_sequence(struct amdgpu_device *adev,
373 const u32 *registers,
374 const u32 array_size)
375{
376 u32 tmp, reg, and_mask, or_mask;
377 int i;
378
379 if (array_size % 3)
380 return;
381
382 for (i = 0; i < array_size; i +=3) {
383 reg = registers[i + 0];
384 and_mask = registers[i + 1];
385 or_mask = registers[i + 2];
386
387 if (and_mask == 0xffffffff) {
388 tmp = or_mask;
389 } else {
390 tmp = RREG32(reg);
391 tmp &= ~and_mask;
392 tmp |= or_mask;
393 }
394 WREG32(reg, tmp);
395 }
396}
397
398void amdgpu_pci_config_reset(struct amdgpu_device *adev)
399{
400 pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
401}
402
403/*
404 * GPU doorbell aperture helpers function.
405 */
406/**
407 * amdgpu_doorbell_init - Init doorbell driver information.
408 *
409 * @adev: amdgpu_device pointer
410 *
411 * Init doorbell driver information (CIK)
412 * Returns 0 on success, error on failure.
413 */
414static int amdgpu_doorbell_init(struct amdgpu_device *adev)
415{
416 /* doorbell bar mapping */
417 adev->doorbell.base = pci_resource_start(adev->pdev, 2);
418 adev->doorbell.size = pci_resource_len(adev->pdev, 2);
419
Christian Königedf600d2016-05-03 15:54:54 +0200420 adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400421 AMDGPU_DOORBELL_MAX_ASSIGNMENT+1);
422 if (adev->doorbell.num_doorbells == 0)
423 return -EINVAL;
424
425 adev->doorbell.ptr = ioremap(adev->doorbell.base, adev->doorbell.num_doorbells * sizeof(u32));
426 if (adev->doorbell.ptr == NULL) {
427 return -ENOMEM;
428 }
429 DRM_INFO("doorbell mmio base: 0x%08X\n", (uint32_t)adev->doorbell.base);
430 DRM_INFO("doorbell mmio size: %u\n", (unsigned)adev->doorbell.size);
431
432 return 0;
433}
434
435/**
436 * amdgpu_doorbell_fini - Tear down doorbell driver information.
437 *
438 * @adev: amdgpu_device pointer
439 *
440 * Tear down doorbell driver information (CIK)
441 */
442static void amdgpu_doorbell_fini(struct amdgpu_device *adev)
443{
444 iounmap(adev->doorbell.ptr);
445 adev->doorbell.ptr = NULL;
446}
447
448/**
449 * amdgpu_doorbell_get_kfd_info - Report doorbell configuration required to
450 * setup amdkfd
451 *
452 * @adev: amdgpu_device pointer
453 * @aperture_base: output returning doorbell aperture base physical address
454 * @aperture_size: output returning doorbell aperture size in bytes
455 * @start_offset: output returning # of doorbell bytes reserved for amdgpu.
456 *
457 * amdgpu and amdkfd share the doorbell aperture. amdgpu sets it up,
458 * takes doorbells required for its own rings and reports the setup to amdkfd.
459 * amdgpu reserved doorbells are at the start of the doorbell aperture.
460 */
461void amdgpu_doorbell_get_kfd_info(struct amdgpu_device *adev,
462 phys_addr_t *aperture_base,
463 size_t *aperture_size,
464 size_t *start_offset)
465{
466 /*
467 * The first num_doorbells are used by amdgpu.
468 * amdkfd takes whatever's left in the aperture.
469 */
470 if (adev->doorbell.size > adev->doorbell.num_doorbells * sizeof(u32)) {
471 *aperture_base = adev->doorbell.base;
472 *aperture_size = adev->doorbell.size;
473 *start_offset = adev->doorbell.num_doorbells * sizeof(u32);
474 } else {
475 *aperture_base = 0;
476 *aperture_size = 0;
477 *start_offset = 0;
478 }
479}
480
481/*
482 * amdgpu_wb_*()
483 * Writeback is the the method by which the the GPU updates special pages
484 * in memory with the status of certain GPU events (fences, ring pointers,
485 * etc.).
486 */
487
488/**
489 * amdgpu_wb_fini - Disable Writeback and free memory
490 *
491 * @adev: amdgpu_device pointer
492 *
493 * Disables Writeback and frees the Writeback memory (all asics).
494 * Used at driver shutdown.
495 */
496static void amdgpu_wb_fini(struct amdgpu_device *adev)
497{
498 if (adev->wb.wb_obj) {
Alex Deuchera76ed482016-10-21 15:30:36 -0400499 amdgpu_bo_free_kernel(&adev->wb.wb_obj,
500 &adev->wb.gpu_addr,
501 (void **)&adev->wb.wb);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400502 adev->wb.wb_obj = NULL;
503 }
504}
505
506/**
507 * amdgpu_wb_init- Init Writeback driver info and allocate memory
508 *
509 * @adev: amdgpu_device pointer
510 *
511 * Disables Writeback and frees the Writeback memory (all asics).
512 * Used at driver startup.
513 * Returns 0 on success or an -error on failure.
514 */
515static int amdgpu_wb_init(struct amdgpu_device *adev)
516{
517 int r;
518
519 if (adev->wb.wb_obj == NULL) {
Huang Rui60a970a62017-03-15 10:13:32 +0800520 r = amdgpu_bo_create_kernel(adev, AMDGPU_MAX_WB * sizeof(uint32_t),
Alex Deuchera76ed482016-10-21 15:30:36 -0400521 PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
522 &adev->wb.wb_obj, &adev->wb.gpu_addr,
523 (void **)&adev->wb.wb);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400524 if (r) {
525 dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
526 return r;
527 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400528
529 adev->wb.num_wb = AMDGPU_MAX_WB;
530 memset(&adev->wb.used, 0, sizeof(adev->wb.used));
531
532 /* clear wb memory */
Huang Rui60a970a62017-03-15 10:13:32 +0800533 memset((char *)adev->wb.wb, 0, AMDGPU_MAX_WB * sizeof(uint32_t));
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400534 }
535
536 return 0;
537}
538
539/**
540 * amdgpu_wb_get - Allocate a wb entry
541 *
542 * @adev: amdgpu_device pointer
543 * @wb: wb index
544 *
545 * Allocate a wb slot for use by the driver (all asics).
546 * Returns 0 on success or -EINVAL on failure.
547 */
548int amdgpu_wb_get(struct amdgpu_device *adev, u32 *wb)
549{
550 unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
551 if (offset < adev->wb.num_wb) {
552 __set_bit(offset, adev->wb.used);
553 *wb = offset;
554 return 0;
555 } else {
556 return -EINVAL;
557 }
558}
559
560/**
Ken Wang70142852016-03-18 15:08:49 +0800561 * amdgpu_wb_get_64bit - Allocate a wb entry
562 *
563 * @adev: amdgpu_device pointer
564 * @wb: wb index
565 *
566 * Allocate a wb slot for use by the driver (all asics).
567 * Returns 0 on success or -EINVAL on failure.
568 */
569int amdgpu_wb_get_64bit(struct amdgpu_device *adev, u32 *wb)
570{
571 unsigned long offset = bitmap_find_next_zero_area_off(adev->wb.used,
572 adev->wb.num_wb, 0, 2, 7, 0);
573 if ((offset + 1) < adev->wb.num_wb) {
574 __set_bit(offset, adev->wb.used);
575 __set_bit(offset + 1, adev->wb.used);
576 *wb = offset;
577 return 0;
578 } else {
579 return -EINVAL;
580 }
581}
582
583/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400584 * amdgpu_wb_free - Free a wb entry
585 *
586 * @adev: amdgpu_device pointer
587 * @wb: wb index
588 *
589 * Free a wb slot allocated for use by the driver (all asics)
590 */
591void amdgpu_wb_free(struct amdgpu_device *adev, u32 wb)
592{
593 if (wb < adev->wb.num_wb)
594 __clear_bit(wb, adev->wb.used);
595}
596
597/**
Ken Wang70142852016-03-18 15:08:49 +0800598 * amdgpu_wb_free_64bit - Free a wb entry
599 *
600 * @adev: amdgpu_device pointer
601 * @wb: wb index
602 *
603 * Free a wb slot allocated for use by the driver (all asics)
604 */
605void amdgpu_wb_free_64bit(struct amdgpu_device *adev, u32 wb)
606{
607 if ((wb + 1) < adev->wb.num_wb) {
608 __clear_bit(wb, adev->wb.used);
609 __clear_bit(wb + 1, adev->wb.used);
610 }
611}
612
613/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400614 * amdgpu_vram_location - try to find VRAM location
615 * @adev: amdgpu device structure holding all necessary informations
616 * @mc: memory controller structure holding memory informations
617 * @base: base address at which to put VRAM
618 *
619 * Function will place try to place VRAM at base address provided
620 * as parameter (which is so far either PCI aperture address or
621 * for IGP TOM base address).
622 *
623 * If there is not enough space to fit the unvisible VRAM in the 32bits
624 * address space then we limit the VRAM size to the aperture.
625 *
626 * Note: We don't explicitly enforce VRAM start to be aligned on VRAM size,
627 * this shouldn't be a problem as we are using the PCI aperture as a reference.
628 * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but
629 * not IGP.
630 *
631 * Note: we use mc_vram_size as on some board we need to program the mc to
632 * cover the whole aperture even if VRAM size is inferior to aperture size
633 * Novell bug 204882 + along with lots of ubuntu ones
634 *
635 * Note: when limiting vram it's safe to overwritte real_vram_size because
636 * we are not in case where real_vram_size is inferior to mc_vram_size (ie
637 * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu
638 * ones)
639 *
640 * Note: IGP TOM addr should be the same as the aperture addr, we don't
641 * explicitly check for that thought.
642 *
643 * FIXME: when reducing VRAM size align new size on power of 2.
644 */
645void amdgpu_vram_location(struct amdgpu_device *adev, struct amdgpu_mc *mc, u64 base)
646{
647 uint64_t limit = (uint64_t)amdgpu_vram_limit << 20;
648
649 mc->vram_start = base;
650 if (mc->mc_vram_size > (adev->mc.mc_mask - base + 1)) {
651 dev_warn(adev->dev, "limiting VRAM to PCI aperture size\n");
652 mc->real_vram_size = mc->aper_size;
653 mc->mc_vram_size = mc->aper_size;
654 }
655 mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
656 if (limit && limit < mc->real_vram_size)
657 mc->real_vram_size = limit;
658 dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
659 mc->mc_vram_size >> 20, mc->vram_start,
660 mc->vram_end, mc->real_vram_size >> 20);
661}
662
663/**
664 * amdgpu_gtt_location - try to find GTT location
665 * @adev: amdgpu device structure holding all necessary informations
666 * @mc: memory controller structure holding memory informations
667 *
668 * Function will place try to place GTT before or after VRAM.
669 *
670 * If GTT size is bigger than space left then we ajust GTT size.
671 * Thus function will never fails.
672 *
673 * FIXME: when reducing GTT size align new size on power of 2.
674 */
675void amdgpu_gtt_location(struct amdgpu_device *adev, struct amdgpu_mc *mc)
676{
677 u64 size_af, size_bf;
678
679 size_af = ((adev->mc.mc_mask - mc->vram_end) + mc->gtt_base_align) & ~mc->gtt_base_align;
680 size_bf = mc->vram_start & ~mc->gtt_base_align;
681 if (size_bf > size_af) {
682 if (mc->gtt_size > size_bf) {
683 dev_warn(adev->dev, "limiting GTT\n");
684 mc->gtt_size = size_bf;
685 }
Alex Deucher9dc5a912016-11-17 15:40:22 -0500686 mc->gtt_start = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400687 } else {
688 if (mc->gtt_size > size_af) {
689 dev_warn(adev->dev, "limiting GTT\n");
690 mc->gtt_size = size_af;
691 }
692 mc->gtt_start = (mc->vram_end + 1 + mc->gtt_base_align) & ~mc->gtt_base_align;
693 }
694 mc->gtt_end = mc->gtt_start + mc->gtt_size - 1;
695 dev_info(adev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
696 mc->gtt_size >> 20, mc->gtt_start, mc->gtt_end);
697}
698
699/*
700 * GPU helpers function.
701 */
702/**
Jim Quc836fec2017-02-10 15:59:59 +0800703 * amdgpu_need_post - check if the hw need post or not
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400704 *
705 * @adev: amdgpu_device pointer
706 *
Jim Quc836fec2017-02-10 15:59:59 +0800707 * Check if the asic has been initialized (all asics) at driver startup
708 * or post is needed if hw reset is performed.
709 * Returns true if need or false if not.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400710 */
Jim Quc836fec2017-02-10 15:59:59 +0800711bool amdgpu_need_post(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400712{
713 uint32_t reg;
714
Jim Quc836fec2017-02-10 15:59:59 +0800715 if (adev->has_hw_reset) {
716 adev->has_hw_reset = false;
717 return true;
718 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400719 /* then check MEM_SIZE, in case the crtcs are off */
Alex Deucherbbf282d2017-03-03 17:26:10 -0500720 reg = amdgpu_asic_get_config_memsize(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400721
722 if (reg)
Jim Quc836fec2017-02-10 15:59:59 +0800723 return false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400724
Jim Quc836fec2017-02-10 15:59:59 +0800725 return true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400726
727}
728
Monk Liubec86372016-09-14 19:38:08 +0800729static bool amdgpu_vpost_needed(struct amdgpu_device *adev)
730{
731 if (amdgpu_sriov_vf(adev))
732 return false;
733
734 if (amdgpu_passthrough(adev)) {
Monk Liu1da2c322016-11-11 11:24:29 +0800735 /* for FIJI: In whole GPU pass-through virtualization case, after VM reboot
736 * some old smc fw still need driver do vPost otherwise gpu hang, while
737 * those smc fw version above 22.15 doesn't have this flaw, so we force
738 * vpost executed for smc version below 22.15
Monk Liubec86372016-09-14 19:38:08 +0800739 */
740 if (adev->asic_type == CHIP_FIJI) {
741 int err;
742 uint32_t fw_ver;
743 err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
744 /* force vPost if error occured */
745 if (err)
746 return true;
747
748 fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
Monk Liu1da2c322016-11-11 11:24:29 +0800749 if (fw_ver < 0x00160e00)
750 return true;
Monk Liubec86372016-09-14 19:38:08 +0800751 }
Monk Liubec86372016-09-14 19:38:08 +0800752 }
Jim Quc836fec2017-02-10 15:59:59 +0800753 return amdgpu_need_post(adev);
Monk Liubec86372016-09-14 19:38:08 +0800754}
755
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400756/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400757 * amdgpu_dummy_page_init - init dummy page used by the driver
758 *
759 * @adev: amdgpu_device pointer
760 *
761 * Allocate the dummy page used by the driver (all asics).
762 * This dummy page is used by the driver as a filler for gart entries
763 * when pages are taken out of the GART
764 * Returns 0 on sucess, -ENOMEM on failure.
765 */
766int amdgpu_dummy_page_init(struct amdgpu_device *adev)
767{
768 if (adev->dummy_page.page)
769 return 0;
770 adev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
771 if (adev->dummy_page.page == NULL)
772 return -ENOMEM;
773 adev->dummy_page.addr = pci_map_page(adev->pdev, adev->dummy_page.page,
774 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
775 if (pci_dma_mapping_error(adev->pdev, adev->dummy_page.addr)) {
776 dev_err(&adev->pdev->dev, "Failed to DMA MAP the dummy page\n");
777 __free_page(adev->dummy_page.page);
778 adev->dummy_page.page = NULL;
779 return -ENOMEM;
780 }
781 return 0;
782}
783
784/**
785 * amdgpu_dummy_page_fini - free dummy page used by the driver
786 *
787 * @adev: amdgpu_device pointer
788 *
789 * Frees the dummy page used by the driver (all asics).
790 */
791void amdgpu_dummy_page_fini(struct amdgpu_device *adev)
792{
793 if (adev->dummy_page.page == NULL)
794 return;
795 pci_unmap_page(adev->pdev, adev->dummy_page.addr,
796 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
797 __free_page(adev->dummy_page.page);
798 adev->dummy_page.page = NULL;
799}
800
801
802/* ATOM accessor methods */
803/*
804 * ATOM is an interpreted byte code stored in tables in the vbios. The
805 * driver registers callbacks to access registers and the interpreter
806 * in the driver parses the tables and executes then to program specific
807 * actions (set display modes, asic init, etc.). See amdgpu_atombios.c,
808 * atombios.h, and atom.c
809 */
810
811/**
812 * cail_pll_read - read PLL register
813 *
814 * @info: atom card_info pointer
815 * @reg: PLL register offset
816 *
817 * Provides a PLL register accessor for the atom interpreter (r4xx+).
818 * Returns the value of the PLL register.
819 */
820static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
821{
822 return 0;
823}
824
825/**
826 * cail_pll_write - write PLL register
827 *
828 * @info: atom card_info pointer
829 * @reg: PLL register offset
830 * @val: value to write to the pll register
831 *
832 * Provides a PLL register accessor for the atom interpreter (r4xx+).
833 */
834static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
835{
836
837}
838
839/**
840 * cail_mc_read - read MC (Memory Controller) register
841 *
842 * @info: atom card_info pointer
843 * @reg: MC register offset
844 *
845 * Provides an MC register accessor for the atom interpreter (r4xx+).
846 * Returns the value of the MC register.
847 */
848static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
849{
850 return 0;
851}
852
853/**
854 * cail_mc_write - write MC (Memory Controller) register
855 *
856 * @info: atom card_info pointer
857 * @reg: MC register offset
858 * @val: value to write to the pll register
859 *
860 * Provides a MC register accessor for the atom interpreter (r4xx+).
861 */
862static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
863{
864
865}
866
867/**
868 * cail_reg_write - write MMIO register
869 *
870 * @info: atom card_info pointer
871 * @reg: MMIO register offset
872 * @val: value to write to the pll register
873 *
874 * Provides a MMIO register accessor for the atom interpreter (r4xx+).
875 */
876static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
877{
878 struct amdgpu_device *adev = info->dev->dev_private;
879
880 WREG32(reg, val);
881}
882
883/**
884 * cail_reg_read - read MMIO register
885 *
886 * @info: atom card_info pointer
887 * @reg: MMIO register offset
888 *
889 * Provides an MMIO register accessor for the atom interpreter (r4xx+).
890 * Returns the value of the MMIO register.
891 */
892static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
893{
894 struct amdgpu_device *adev = info->dev->dev_private;
895 uint32_t r;
896
897 r = RREG32(reg);
898 return r;
899}
900
901/**
902 * cail_ioreg_write - write IO register
903 *
904 * @info: atom card_info pointer
905 * @reg: IO register offset
906 * @val: value to write to the pll register
907 *
908 * Provides a IO register accessor for the atom interpreter (r4xx+).
909 */
910static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
911{
912 struct amdgpu_device *adev = info->dev->dev_private;
913
914 WREG32_IO(reg, val);
915}
916
917/**
918 * cail_ioreg_read - read IO register
919 *
920 * @info: atom card_info pointer
921 * @reg: IO register offset
922 *
923 * Provides an IO register accessor for the atom interpreter (r4xx+).
924 * Returns the value of the IO register.
925 */
926static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
927{
928 struct amdgpu_device *adev = info->dev->dev_private;
929 uint32_t r;
930
931 r = RREG32_IO(reg);
932 return r;
933}
934
935/**
936 * amdgpu_atombios_fini - free the driver info and callbacks for atombios
937 *
938 * @adev: amdgpu_device pointer
939 *
940 * Frees the driver info and register access callbacks for the ATOM
941 * interpreter (r4xx+).
942 * Called at driver shutdown.
943 */
944static void amdgpu_atombios_fini(struct amdgpu_device *adev)
945{
Monk Liu89e0ec9f2016-05-27 19:34:11 +0800946 if (adev->mode_info.atom_context) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400947 kfree(adev->mode_info.atom_context->scratch);
Monk Liu89e0ec9f2016-05-27 19:34:11 +0800948 kfree(adev->mode_info.atom_context->iio);
949 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400950 kfree(adev->mode_info.atom_context);
951 adev->mode_info.atom_context = NULL;
952 kfree(adev->mode_info.atom_card_info);
953 adev->mode_info.atom_card_info = NULL;
954}
955
956/**
957 * amdgpu_atombios_init - init the driver info and callbacks for atombios
958 *
959 * @adev: amdgpu_device pointer
960 *
961 * Initializes the driver info and register access callbacks for the
962 * ATOM interpreter (r4xx+).
963 * Returns 0 on sucess, -ENOMEM on failure.
964 * Called at driver startup.
965 */
966static int amdgpu_atombios_init(struct amdgpu_device *adev)
967{
968 struct card_info *atom_card_info =
969 kzalloc(sizeof(struct card_info), GFP_KERNEL);
970
971 if (!atom_card_info)
972 return -ENOMEM;
973
974 adev->mode_info.atom_card_info = atom_card_info;
975 atom_card_info->dev = adev->ddev;
976 atom_card_info->reg_read = cail_reg_read;
977 atom_card_info->reg_write = cail_reg_write;
978 /* needed for iio ops */
979 if (adev->rio_mem) {
980 atom_card_info->ioreg_read = cail_ioreg_read;
981 atom_card_info->ioreg_write = cail_ioreg_write;
982 } else {
Amber Linb64a18c2017-01-04 08:06:58 -0500983 DRM_INFO("PCI I/O BAR is not found. Using MMIO to access ATOM BIOS\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400984 atom_card_info->ioreg_read = cail_reg_read;
985 atom_card_info->ioreg_write = cail_reg_write;
986 }
987 atom_card_info->mc_read = cail_mc_read;
988 atom_card_info->mc_write = cail_mc_write;
989 atom_card_info->pll_read = cail_pll_read;
990 atom_card_info->pll_write = cail_pll_write;
991
992 adev->mode_info.atom_context = amdgpu_atom_parse(atom_card_info, adev->bios);
993 if (!adev->mode_info.atom_context) {
994 amdgpu_atombios_fini(adev);
995 return -ENOMEM;
996 }
997
998 mutex_init(&adev->mode_info.atom_context->mutex);
Alex Deuchera5bde2f2016-09-23 16:23:41 -0400999 if (adev->is_atom_fw) {
1000 amdgpu_atomfirmware_scratch_regs_init(adev);
1001 amdgpu_atomfirmware_allocate_fb_scratch(adev);
1002 } else {
1003 amdgpu_atombios_scratch_regs_init(adev);
1004 amdgpu_atombios_allocate_fb_scratch(adev);
1005 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001006 return 0;
1007}
1008
1009/* if we get transitioned to only one device, take VGA back */
1010/**
1011 * amdgpu_vga_set_decode - enable/disable vga decode
1012 *
1013 * @cookie: amdgpu_device pointer
1014 * @state: enable/disable vga decode
1015 *
1016 * Enable/disable vga decode (all asics).
1017 * Returns VGA resource flags.
1018 */
1019static unsigned int amdgpu_vga_set_decode(void *cookie, bool state)
1020{
1021 struct amdgpu_device *adev = cookie;
1022 amdgpu_asic_set_vga_state(adev, state);
1023 if (state)
1024 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
1025 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1026 else
1027 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1028}
1029
1030/**
1031 * amdgpu_check_pot_argument - check that argument is a power of two
1032 *
1033 * @arg: value to check
1034 *
1035 * Validates that a certain argument is a power of two (all asics).
1036 * Returns true if argument is valid.
1037 */
1038static bool amdgpu_check_pot_argument(int arg)
1039{
1040 return (arg & (arg - 1)) == 0;
1041}
1042
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001043static void amdgpu_get_block_size(struct amdgpu_device *adev)
1044{
1045 /* defines number of bits in page table versus page directory,
1046 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
1047 * page table and the remaining bits are in the page directory */
1048 if (amdgpu_vm_block_size == -1) {
1049
1050 /* Total bits covered by PD + PTs */
1051 unsigned bits = ilog2(amdgpu_vm_size) + 18;
1052
1053 /* Make sure the PD is 4K in size up to 8GB address space.
1054 Above that split equal between PD and PTs */
1055 if (amdgpu_vm_size <= 8)
1056 amdgpu_vm_block_size = bits - 9;
1057 else
1058 amdgpu_vm_block_size = (bits + 3) / 2;
1059
1060 } else if (amdgpu_vm_block_size < 9) {
1061 dev_warn(adev->dev, "VM page table size (%d) too small\n",
1062 amdgpu_vm_block_size);
1063 amdgpu_vm_block_size = 9;
1064 }
1065
1066 if (amdgpu_vm_block_size > 24 ||
1067 (amdgpu_vm_size * 1024) < (1ull << amdgpu_vm_block_size)) {
1068 dev_warn(adev->dev, "VM page table size (%d) too large\n",
1069 amdgpu_vm_block_size);
1070 amdgpu_vm_block_size = 9;
1071 }
1072}
1073
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001074/**
1075 * amdgpu_check_arguments - validate module params
1076 *
1077 * @adev: amdgpu_device pointer
1078 *
1079 * Validates certain module parameters and updates
1080 * the associated values used by the driver (all asics).
1081 */
1082static void amdgpu_check_arguments(struct amdgpu_device *adev)
1083{
Chunming Zhou5b011232015-12-10 17:34:33 +08001084 if (amdgpu_sched_jobs < 4) {
1085 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
1086 amdgpu_sched_jobs);
1087 amdgpu_sched_jobs = 4;
1088 } else if (!amdgpu_check_pot_argument(amdgpu_sched_jobs)){
1089 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
1090 amdgpu_sched_jobs);
1091 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
1092 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001093
1094 if (amdgpu_gart_size != -1) {
Christian Königc4e1a132016-03-17 16:25:15 +01001095 /* gtt size must be greater or equal to 32M */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001096 if (amdgpu_gart_size < 32) {
1097 dev_warn(adev->dev, "gart size (%d) too small\n",
1098 amdgpu_gart_size);
1099 amdgpu_gart_size = -1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001100 }
1101 }
1102
1103 if (!amdgpu_check_pot_argument(amdgpu_vm_size)) {
1104 dev_warn(adev->dev, "VM size (%d) must be a power of 2\n",
1105 amdgpu_vm_size);
Alex Deucher8dacc122015-05-11 16:20:58 -04001106 amdgpu_vm_size = 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001107 }
1108
1109 if (amdgpu_vm_size < 1) {
1110 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
1111 amdgpu_vm_size);
Alex Deucher8dacc122015-05-11 16:20:58 -04001112 amdgpu_vm_size = 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001113 }
1114
1115 /*
1116 * Max GPUVM size for Cayman, SI and CI are 40 bits.
1117 */
1118 if (amdgpu_vm_size > 1024) {
1119 dev_warn(adev->dev, "VM size (%d) too large, max is 1TB\n",
1120 amdgpu_vm_size);
Alex Deucher8dacc122015-05-11 16:20:58 -04001121 amdgpu_vm_size = 8;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001122 }
1123
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001124 amdgpu_get_block_size(adev);
Christian König6a7f76e2016-08-24 15:51:49 +02001125
jimqu526bae32016-11-07 09:53:10 +08001126 if (amdgpu_vram_page_split != -1 && (amdgpu_vram_page_split < 16 ||
1127 !amdgpu_check_pot_argument(amdgpu_vram_page_split))) {
Christian König6a7f76e2016-08-24 15:51:49 +02001128 dev_warn(adev->dev, "invalid VRAM page split (%d)\n",
1129 amdgpu_vram_page_split);
1130 amdgpu_vram_page_split = 1024;
1131 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001132}
1133
1134/**
1135 * amdgpu_switcheroo_set_state - set switcheroo state
1136 *
1137 * @pdev: pci dev pointer
Lukas Wunner16944672015-09-05 11:17:35 +02001138 * @state: vga_switcheroo state
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001139 *
1140 * Callback for the switcheroo driver. Suspends or resumes the
1141 * the asics before or after it is powered up using ACPI methods.
1142 */
1143static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1144{
1145 struct drm_device *dev = pci_get_drvdata(pdev);
1146
1147 if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
1148 return;
1149
1150 if (state == VGA_SWITCHEROO_ON) {
1151 unsigned d3_delay = dev->pdev->d3_delay;
1152
Joe Perches7ca85292017-02-28 04:55:52 -08001153 pr_info("amdgpu: switched on\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001154 /* don't suspend or resume card normally */
1155 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1156
Alex Deucher810ddc32016-08-23 13:25:49 -04001157 amdgpu_device_resume(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001158
1159 dev->pdev->d3_delay = d3_delay;
1160
1161 dev->switch_power_state = DRM_SWITCH_POWER_ON;
1162 drm_kms_helper_poll_enable(dev);
1163 } else {
Joe Perches7ca85292017-02-28 04:55:52 -08001164 pr_info("amdgpu: switched off\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001165 drm_kms_helper_poll_disable(dev);
1166 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Alex Deucher810ddc32016-08-23 13:25:49 -04001167 amdgpu_device_suspend(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001168 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1169 }
1170}
1171
1172/**
1173 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1174 *
1175 * @pdev: pci dev pointer
1176 *
1177 * Callback for the switcheroo driver. Check of the switcheroo
1178 * state can be changed.
1179 * Returns true if the state can be changed, false if not.
1180 */
1181static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1182{
1183 struct drm_device *dev = pci_get_drvdata(pdev);
1184
1185 /*
1186 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1187 * locking inversion with the driver load path. And the access here is
1188 * completely racy anyway. So don't bother with locking for now.
1189 */
1190 return dev->open_count == 0;
1191}
1192
1193static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1194 .set_gpu_state = amdgpu_switcheroo_set_state,
1195 .reprobe = NULL,
1196 .can_switch = amdgpu_switcheroo_can_switch,
1197};
1198
1199int amdgpu_set_clockgating_state(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001200 enum amd_ip_block_type block_type,
1201 enum amd_clockgating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001202{
1203 int i, r = 0;
1204
1205 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001206 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001207 continue;
Rex Zhuc7228652017-02-22 15:33:46 +08001208 if (adev->ip_blocks[i].version->type != block_type)
1209 continue;
1210 if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
1211 continue;
1212 r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
1213 (void *)adev, state);
1214 if (r)
1215 DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
1216 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001217 }
1218 return r;
1219}
1220
1221int amdgpu_set_powergating_state(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001222 enum amd_ip_block_type block_type,
1223 enum amd_powergating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001224{
1225 int i, r = 0;
1226
1227 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001228 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001229 continue;
Rex Zhuc7228652017-02-22 15:33:46 +08001230 if (adev->ip_blocks[i].version->type != block_type)
1231 continue;
1232 if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
1233 continue;
1234 r = adev->ip_blocks[i].version->funcs->set_powergating_state(
1235 (void *)adev, state);
1236 if (r)
1237 DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
1238 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001239 }
1240 return r;
1241}
1242
Huang Rui6cb2d4e2017-01-05 18:44:41 +08001243void amdgpu_get_clockgating_state(struct amdgpu_device *adev, u32 *flags)
1244{
1245 int i;
1246
1247 for (i = 0; i < adev->num_ip_blocks; i++) {
1248 if (!adev->ip_blocks[i].status.valid)
1249 continue;
1250 if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
1251 adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
1252 }
1253}
1254
Alex Deucher5dbbb602016-06-23 11:41:04 -04001255int amdgpu_wait_for_idle(struct amdgpu_device *adev,
1256 enum amd_ip_block_type block_type)
1257{
1258 int i, r;
1259
1260 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001261 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001262 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001263 if (adev->ip_blocks[i].version->type == block_type) {
1264 r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -04001265 if (r)
1266 return r;
1267 break;
1268 }
1269 }
1270 return 0;
1271
1272}
1273
1274bool amdgpu_is_idle(struct amdgpu_device *adev,
1275 enum amd_ip_block_type block_type)
1276{
1277 int i;
1278
1279 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001280 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001281 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001282 if (adev->ip_blocks[i].version->type == block_type)
1283 return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -04001284 }
1285 return true;
1286
1287}
1288
Alex Deuchera1255102016-10-13 17:41:13 -04001289struct amdgpu_ip_block * amdgpu_get_ip_block(struct amdgpu_device *adev,
1290 enum amd_ip_block_type type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001291{
1292 int i;
1293
1294 for (i = 0; i < adev->num_ip_blocks; i++)
Alex Deuchera1255102016-10-13 17:41:13 -04001295 if (adev->ip_blocks[i].version->type == type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001296 return &adev->ip_blocks[i];
1297
1298 return NULL;
1299}
1300
1301/**
1302 * amdgpu_ip_block_version_cmp
1303 *
1304 * @adev: amdgpu_device pointer
yanyang15fc3aee2015-05-22 14:39:35 -04001305 * @type: enum amd_ip_block_type
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001306 * @major: major version
1307 * @minor: minor version
1308 *
1309 * return 0 if equal or greater
1310 * return 1 if smaller or the ip_block doesn't exist
1311 */
1312int amdgpu_ip_block_version_cmp(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001313 enum amd_ip_block_type type,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001314 u32 major, u32 minor)
1315{
Alex Deuchera1255102016-10-13 17:41:13 -04001316 struct amdgpu_ip_block *ip_block = amdgpu_get_ip_block(adev, type);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001317
Alex Deuchera1255102016-10-13 17:41:13 -04001318 if (ip_block && ((ip_block->version->major > major) ||
1319 ((ip_block->version->major == major) &&
1320 (ip_block->version->minor >= minor))))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001321 return 0;
1322
1323 return 1;
1324}
1325
Alex Deuchera1255102016-10-13 17:41:13 -04001326/**
1327 * amdgpu_ip_block_add
1328 *
1329 * @adev: amdgpu_device pointer
1330 * @ip_block_version: pointer to the IP to add
1331 *
1332 * Adds the IP block driver information to the collection of IPs
1333 * on the asic.
1334 */
1335int amdgpu_ip_block_add(struct amdgpu_device *adev,
1336 const struct amdgpu_ip_block_version *ip_block_version)
1337{
1338 if (!ip_block_version)
1339 return -EINVAL;
1340
1341 adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
1342
1343 return 0;
1344}
1345
Alex Deucher483ef982016-09-30 12:43:04 -04001346static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
Emily Deng9accf2f2016-08-10 16:01:25 +08001347{
1348 adev->enable_virtual_display = false;
1349
1350 if (amdgpu_virtual_display) {
1351 struct drm_device *ddev = adev->ddev;
1352 const char *pci_address_name = pci_name(ddev->pdev);
Emily Deng0f663562016-09-30 13:02:18 -04001353 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
Emily Deng9accf2f2016-08-10 16:01:25 +08001354
1355 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1356 pciaddstr_tmp = pciaddstr;
Emily Deng0f663562016-09-30 13:02:18 -04001357 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1358 pciaddname = strsep(&pciaddname_tmp, ",");
Yintian Tao967de2a2017-01-22 15:16:51 +08001359 if (!strcmp("all", pciaddname)
1360 || !strcmp(pci_address_name, pciaddname)) {
Emily Deng0f663562016-09-30 13:02:18 -04001361 long num_crtc;
1362 int res = -1;
1363
Emily Deng9accf2f2016-08-10 16:01:25 +08001364 adev->enable_virtual_display = true;
Emily Deng0f663562016-09-30 13:02:18 -04001365
1366 if (pciaddname_tmp)
1367 res = kstrtol(pciaddname_tmp, 10,
1368 &num_crtc);
1369
1370 if (!res) {
1371 if (num_crtc < 1)
1372 num_crtc = 1;
1373 if (num_crtc > 6)
1374 num_crtc = 6;
1375 adev->mode_info.num_crtc = num_crtc;
1376 } else {
1377 adev->mode_info.num_crtc = 1;
1378 }
Emily Deng9accf2f2016-08-10 16:01:25 +08001379 break;
1380 }
1381 }
1382
Emily Deng0f663562016-09-30 13:02:18 -04001383 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
1384 amdgpu_virtual_display, pci_address_name,
1385 adev->enable_virtual_display, adev->mode_info.num_crtc);
Emily Deng9accf2f2016-08-10 16:01:25 +08001386
1387 kfree(pciaddstr);
1388 }
1389}
1390
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001391static int amdgpu_early_init(struct amdgpu_device *adev)
1392{
Alex Deucheraaa36a92015-04-20 17:31:14 -04001393 int i, r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001394
Alex Deucher483ef982016-09-30 12:43:04 -04001395 amdgpu_device_enable_virtual_display(adev);
Emily Denga6be7572016-08-08 11:37:50 +08001396
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001397 switch (adev->asic_type) {
Alex Deucheraaa36a92015-04-20 17:31:14 -04001398 case CHIP_TOPAZ:
1399 case CHIP_TONGA:
David Zhang48299f92015-07-08 01:05:16 +08001400 case CHIP_FIJI:
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001401 case CHIP_POLARIS11:
1402 case CHIP_POLARIS10:
Junwei Zhangc4642a42016-12-14 15:32:28 -05001403 case CHIP_POLARIS12:
Alex Deucheraaa36a92015-04-20 17:31:14 -04001404 case CHIP_CARRIZO:
Samuel Li39bb0c92015-10-08 16:31:43 -04001405 case CHIP_STONEY:
1406 if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
Alex Deucheraaa36a92015-04-20 17:31:14 -04001407 adev->family = AMDGPU_FAMILY_CZ;
1408 else
1409 adev->family = AMDGPU_FAMILY_VI;
1410
1411 r = vi_set_ip_blocks(adev);
1412 if (r)
1413 return r;
1414 break;
Ken Wang33f34802016-01-21 17:29:41 +08001415#ifdef CONFIG_DRM_AMDGPU_SI
1416 case CHIP_VERDE:
1417 case CHIP_TAHITI:
1418 case CHIP_PITCAIRN:
1419 case CHIP_OLAND:
1420 case CHIP_HAINAN:
Ken Wang295d0da2016-05-24 21:02:53 +08001421 adev->family = AMDGPU_FAMILY_SI;
Ken Wang33f34802016-01-21 17:29:41 +08001422 r = si_set_ip_blocks(adev);
1423 if (r)
1424 return r;
1425 break;
1426#endif
Alex Deuchera2e73f52015-04-20 17:09:27 -04001427#ifdef CONFIG_DRM_AMDGPU_CIK
1428 case CHIP_BONAIRE:
1429 case CHIP_HAWAII:
1430 case CHIP_KAVERI:
1431 case CHIP_KABINI:
1432 case CHIP_MULLINS:
1433 if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
1434 adev->family = AMDGPU_FAMILY_CI;
1435 else
1436 adev->family = AMDGPU_FAMILY_KV;
1437
1438 r = cik_set_ip_blocks(adev);
1439 if (r)
1440 return r;
1441 break;
1442#endif
Ken Wang460826e2017-03-06 14:53:16 -05001443 case CHIP_VEGA10:
1444 adev->family = AMDGPU_FAMILY_AI;
1445
1446 r = soc15_set_ip_blocks(adev);
1447 if (r)
1448 return r;
1449 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001450 default:
1451 /* FIXME: not supported yet */
1452 return -EINVAL;
1453 }
1454
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001455 if (amdgpu_sriov_vf(adev)) {
1456 r = amdgpu_virt_request_full_gpu(adev, true);
1457 if (r)
1458 return r;
1459 }
1460
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001461 for (i = 0; i < adev->num_ip_blocks; i++) {
1462 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
1463 DRM_ERROR("disabled ip block: %d\n", i);
Alex Deuchera1255102016-10-13 17:41:13 -04001464 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001465 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001466 if (adev->ip_blocks[i].version->funcs->early_init) {
1467 r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001468 if (r == -ENOENT) {
Alex Deuchera1255102016-10-13 17:41:13 -04001469 adev->ip_blocks[i].status.valid = false;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001470 } else if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001471 DRM_ERROR("early_init of IP block <%s> failed %d\n",
1472 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001473 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001474 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001475 adev->ip_blocks[i].status.valid = true;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001476 }
Alex Deucher974e6b62015-07-10 13:59:44 -04001477 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001478 adev->ip_blocks[i].status.valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001479 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001480 }
1481 }
1482
Nicolai Hähnle395d1fb2016-06-02 12:32:07 +02001483 adev->cg_flags &= amdgpu_cg_mask;
1484 adev->pg_flags &= amdgpu_pg_mask;
1485
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001486 return 0;
1487}
1488
1489static int amdgpu_init(struct amdgpu_device *adev)
1490{
1491 int i, r;
1492
1493 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001494 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001495 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001496 r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001497 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001498 DRM_ERROR("sw_init of IP block <%s> failed %d\n",
1499 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001500 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001501 }
Alex Deuchera1255102016-10-13 17:41:13 -04001502 adev->ip_blocks[i].status.sw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001503 /* need to do gmc hw init early so we can allocate gpu mem */
Alex Deuchera1255102016-10-13 17:41:13 -04001504 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001505 r = amdgpu_vram_scratch_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001506 if (r) {
1507 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001508 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001509 }
Alex Deuchera1255102016-10-13 17:41:13 -04001510 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001511 if (r) {
1512 DRM_ERROR("hw_init %d failed %d\n", i, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001513 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001514 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001515 r = amdgpu_wb_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001516 if (r) {
1517 DRM_ERROR("amdgpu_wb_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001518 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001519 }
Alex Deuchera1255102016-10-13 17:41:13 -04001520 adev->ip_blocks[i].status.hw = true;
Monk Liu24936642017-01-09 15:54:32 +08001521
1522 /* right after GMC hw init, we create CSA */
1523 if (amdgpu_sriov_vf(adev)) {
1524 r = amdgpu_allocate_static_csa(adev);
1525 if (r) {
1526 DRM_ERROR("allocate CSA failed %d\n", r);
1527 return r;
1528 }
1529 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001530 }
1531 }
1532
1533 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001534 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001535 continue;
1536 /* gmc hw init is done early */
Alex Deuchera1255102016-10-13 17:41:13 -04001537 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001538 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001539 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001540 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001541 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
1542 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001543 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001544 }
Alex Deuchera1255102016-10-13 17:41:13 -04001545 adev->ip_blocks[i].status.hw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001546 }
1547
1548 return 0;
1549}
1550
1551static int amdgpu_late_init(struct amdgpu_device *adev)
1552{
1553 int i = 0, r;
1554
1555 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001556 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001557 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001558 if (adev->ip_blocks[i].version->funcs->late_init) {
1559 r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001560 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001561 DRM_ERROR("late_init of IP block <%s> failed %d\n",
1562 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001563 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001564 }
Alex Deuchera1255102016-10-13 17:41:13 -04001565 adev->ip_blocks[i].status.late_initialized = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001566 }
Alex Deucher4a446d52016-10-07 14:48:18 -04001567 /* skip CG for VCE/UVD, it's handled specially */
Alex Deuchera1255102016-10-13 17:41:13 -04001568 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1569 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
Alex Deucher4a446d52016-10-07 14:48:18 -04001570 /* enable clockgating to save power */
Alex Deuchera1255102016-10-13 17:41:13 -04001571 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1572 AMD_CG_STATE_GATE);
Alex Deucher4a446d52016-10-07 14:48:18 -04001573 if (r) {
1574 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001575 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher4a446d52016-10-07 14:48:18 -04001576 return r;
1577 }
Arindam Nathb0b00ff2016-10-07 19:01:37 +05301578 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001579 }
1580
Tom St Denisd1aff8e2016-08-09 18:01:55 -04001581 amdgpu_dpm_enable_uvd(adev, false);
1582 amdgpu_dpm_enable_vce(adev, false);
1583
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001584 return 0;
1585}
1586
1587static int amdgpu_fini(struct amdgpu_device *adev)
1588{
1589 int i, r;
1590
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001591 /* need to disable SMC first */
1592 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001593 if (!adev->ip_blocks[i].status.hw)
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001594 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001595 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001596 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
Alex Deuchera1255102016-10-13 17:41:13 -04001597 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1598 AMD_CG_STATE_UNGATE);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001599 if (r) {
1600 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001601 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001602 return r;
1603 }
Alex Deuchera1255102016-10-13 17:41:13 -04001604 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001605 /* XXX handle errors */
1606 if (r) {
1607 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001608 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001609 }
Alex Deuchera1255102016-10-13 17:41:13 -04001610 adev->ip_blocks[i].status.hw = false;
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001611 break;
1612 }
1613 }
1614
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001615 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001616 if (!adev->ip_blocks[i].status.hw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001617 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001618 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001619 amdgpu_wb_fini(adev);
1620 amdgpu_vram_scratch_fini(adev);
1621 }
Rex Zhu8201a672016-11-24 21:44:44 +08001622
1623 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1624 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1625 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
1626 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1627 AMD_CG_STATE_UNGATE);
1628 if (r) {
1629 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1630 adev->ip_blocks[i].version->funcs->name, r);
1631 return r;
1632 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001633 }
Rex Zhu8201a672016-11-24 21:44:44 +08001634
Alex Deuchera1255102016-10-13 17:41:13 -04001635 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001636 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001637 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001638 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
1639 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001640 }
Rex Zhu8201a672016-11-24 21:44:44 +08001641
Alex Deuchera1255102016-10-13 17:41:13 -04001642 adev->ip_blocks[i].status.hw = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001643 }
1644
1645 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001646 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001647 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001648 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001649 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001650 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001651 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
1652 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001653 }
Alex Deuchera1255102016-10-13 17:41:13 -04001654 adev->ip_blocks[i].status.sw = false;
1655 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001656 }
1657
Monk Liua6dcfd92016-05-19 14:36:34 +08001658 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001659 if (!adev->ip_blocks[i].status.late_initialized)
Grazvydas Ignotas8a2eef12016-10-03 00:06:44 +03001660 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001661 if (adev->ip_blocks[i].version->funcs->late_fini)
1662 adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
1663 adev->ip_blocks[i].status.late_initialized = false;
Monk Liua6dcfd92016-05-19 14:36:34 +08001664 }
1665
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001666 if (amdgpu_sriov_vf(adev)) {
Monk Liu24936642017-01-09 15:54:32 +08001667 amdgpu_bo_free_kernel(&adev->virt.csa_obj, &adev->virt.csa_vmid0_addr, NULL);
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001668 amdgpu_virt_release_full_gpu(adev, false);
1669 }
Monk Liu24936642017-01-09 15:54:32 +08001670
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001671 return 0;
1672}
1673
Alex Deucherfaefba92016-12-06 10:38:29 -05001674int amdgpu_suspend(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001675{
1676 int i, r;
1677
Xiangliang Yue941ea92017-01-18 12:47:55 +08001678 if (amdgpu_sriov_vf(adev))
1679 amdgpu_virt_request_full_gpu(adev, false);
1680
Flora Cuic5a93a22016-02-26 10:45:25 +08001681 /* ungate SMC block first */
1682 r = amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC,
1683 AMD_CG_STATE_UNGATE);
1684 if (r) {
1685 DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n",r);
1686 }
1687
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001688 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001689 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001690 continue;
1691 /* ungate blocks so that suspend can properly shut them down */
Flora Cuic5a93a22016-02-26 10:45:25 +08001692 if (i != AMD_IP_BLOCK_TYPE_SMC) {
Alex Deuchera1255102016-10-13 17:41:13 -04001693 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1694 AMD_CG_STATE_UNGATE);
Flora Cuic5a93a22016-02-26 10:45:25 +08001695 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001696 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1697 adev->ip_blocks[i].version->funcs->name, r);
Flora Cuic5a93a22016-02-26 10:45:25 +08001698 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001699 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001700 /* XXX handle errors */
Alex Deuchera1255102016-10-13 17:41:13 -04001701 r = adev->ip_blocks[i].version->funcs->suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001702 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001703 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001704 DRM_ERROR("suspend of IP block <%s> failed %d\n",
1705 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001706 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001707 }
1708
Xiangliang Yue941ea92017-01-18 12:47:55 +08001709 if (amdgpu_sriov_vf(adev))
1710 amdgpu_virt_release_full_gpu(adev, false);
1711
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001712 return 0;
1713}
1714
Monk Liue4f0fdc2017-02-09 11:55:49 +08001715static int amdgpu_sriov_reinit_early(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08001716{
1717 int i, r;
1718
1719 for (i = 0; i < adev->num_ip_blocks; i++) {
1720 if (!adev->ip_blocks[i].status.valid)
1721 continue;
1722
1723 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
1724 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
1725 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH)
Monk Liue4f0fdc2017-02-09 11:55:49 +08001726 r = adev->ip_blocks[i].version->funcs->hw_init(adev);
Monk Liua90ad3c2017-01-23 14:22:08 +08001727
1728 if (r) {
1729 DRM_ERROR("resume of IP block <%s> failed %d\n",
1730 adev->ip_blocks[i].version->funcs->name, r);
1731 return r;
1732 }
1733 }
1734
1735 return 0;
1736}
1737
Monk Liue4f0fdc2017-02-09 11:55:49 +08001738static int amdgpu_sriov_reinit_late(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08001739{
1740 int i, r;
1741
1742 for (i = 0; i < adev->num_ip_blocks; i++) {
1743 if (!adev->ip_blocks[i].status.valid)
1744 continue;
1745
1746 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
1747 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
1748 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH )
1749 continue;
1750
Monk Liue4f0fdc2017-02-09 11:55:49 +08001751 r = adev->ip_blocks[i].version->funcs->hw_init(adev);
Monk Liua90ad3c2017-01-23 14:22:08 +08001752 if (r) {
1753 DRM_ERROR("resume of IP block <%s> failed %d\n",
1754 adev->ip_blocks[i].version->funcs->name, r);
1755 return r;
1756 }
1757 }
1758
1759 return 0;
1760}
1761
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001762static int amdgpu_resume(struct amdgpu_device *adev)
1763{
1764 int i, r;
1765
1766 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001767 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001768 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001769 r = adev->ip_blocks[i].version->funcs->resume(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001770 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001771 DRM_ERROR("resume of IP block <%s> failed %d\n",
1772 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001773 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001774 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001775 }
1776
1777 return 0;
1778}
1779
Monk Liu4e99a442016-03-31 13:26:59 +08001780static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
Andres Rodriguez048765a2016-06-11 02:51:32 -04001781{
Alex Deuchera5bde2f2016-09-23 16:23:41 -04001782 if (adev->is_atom_fw) {
1783 if (amdgpu_atomfirmware_gpu_supports_virtualization(adev))
1784 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
1785 } else {
1786 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
1787 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
1788 }
Andres Rodriguez048765a2016-06-11 02:51:32 -04001789}
1790
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001791/**
1792 * amdgpu_device_init - initialize the driver
1793 *
1794 * @adev: amdgpu_device pointer
1795 * @pdev: drm dev pointer
1796 * @pdev: pci dev pointer
1797 * @flags: driver flags
1798 *
1799 * Initializes the driver info and hw (all asics).
1800 * Returns 0 for success or an error on failure.
1801 * Called at driver startup.
1802 */
1803int amdgpu_device_init(struct amdgpu_device *adev,
1804 struct drm_device *ddev,
1805 struct pci_dev *pdev,
1806 uint32_t flags)
1807{
1808 int r, i;
1809 bool runtime = false;
Marek Olšák95844d22016-08-17 23:49:27 +02001810 u32 max_MBps;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001811
1812 adev->shutdown = false;
1813 adev->dev = &pdev->dev;
1814 adev->ddev = ddev;
1815 adev->pdev = pdev;
1816 adev->flags = flags;
Jammy Zhou2f7d10b2015-07-22 11:29:01 +08001817 adev->asic_type = flags & AMD_ASIC_MASK;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001818 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
1819 adev->mc.gtt_size = 512 * 1024 * 1024;
1820 adev->accel_working = false;
1821 adev->num_rings = 0;
1822 adev->mman.buffer_funcs = NULL;
1823 adev->mman.buffer_funcs_ring = NULL;
1824 adev->vm_manager.vm_pte_funcs = NULL;
Christian König2d55e452016-02-08 17:37:38 +01001825 adev->vm_manager.vm_pte_num_rings = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001826 adev->gart.gart_funcs = NULL;
Chris Wilsonf54d1862016-10-25 13:00:45 +01001827 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001828
1829 adev->smc_rreg = &amdgpu_invalid_rreg;
1830 adev->smc_wreg = &amdgpu_invalid_wreg;
1831 adev->pcie_rreg = &amdgpu_invalid_rreg;
1832 adev->pcie_wreg = &amdgpu_invalid_wreg;
Huang Rui36b9a952016-08-31 13:23:25 +08001833 adev->pciep_rreg = &amdgpu_invalid_rreg;
1834 adev->pciep_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001835 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
1836 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
1837 adev->didt_rreg = &amdgpu_invalid_rreg;
1838 adev->didt_wreg = &amdgpu_invalid_wreg;
Rex Zhuccdbb202016-06-08 12:47:41 +08001839 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
1840 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001841 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
1842 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
1843
Rex Zhuccdbb202016-06-08 12:47:41 +08001844
Alex Deucher3e39ab92015-06-05 15:04:33 -04001845 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
1846 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
1847 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001848
1849 /* mutex initialization are all done here so we
1850 * can recall function without having locking issues */
Christian König8d0a7ce2015-11-03 20:58:50 +01001851 mutex_init(&adev->vm_manager.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001852 atomic_set(&adev->irq.ih.lock, 0);
Huang Rui0e5ca0d2017-03-03 18:37:23 -05001853 mutex_init(&adev->firmware.mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001854 mutex_init(&adev->pm.mutex);
1855 mutex_init(&adev->gfx.gpu_clock_mutex);
1856 mutex_init(&adev->srbm_mutex);
1857 mutex_init(&adev->grbm_idx_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001858 mutex_init(&adev->mn_lock);
1859 hash_init(adev->mn_hash);
1860
1861 amdgpu_check_arguments(adev);
1862
1863 /* Registers mapping */
1864 /* TODO: block userspace mapping of io register */
1865 spin_lock_init(&adev->mmio_idx_lock);
1866 spin_lock_init(&adev->smc_idx_lock);
1867 spin_lock_init(&adev->pcie_idx_lock);
1868 spin_lock_init(&adev->uvd_ctx_idx_lock);
1869 spin_lock_init(&adev->didt_idx_lock);
Rex Zhuccdbb202016-06-08 12:47:41 +08001870 spin_lock_init(&adev->gc_cac_idx_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001871 spin_lock_init(&adev->audio_endpt_idx_lock);
Marek Olšák95844d22016-08-17 23:49:27 +02001872 spin_lock_init(&adev->mm_stats.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001873
Chunming Zhou0c4e7fa2016-08-17 11:41:30 +08001874 INIT_LIST_HEAD(&adev->shadow_list);
1875 mutex_init(&adev->shadow_list_lock);
1876
Chunming Zhou5c1354b2016-08-30 16:13:10 +08001877 INIT_LIST_HEAD(&adev->gtt_list);
1878 spin_lock_init(&adev->gtt_list_lock);
1879
Ken Wangda69c1612016-01-21 19:08:55 +08001880 if (adev->asic_type >= CHIP_BONAIRE) {
1881 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
1882 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
1883 } else {
1884 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
1885 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
1886 }
Chunming Zhou5c1354b2016-08-30 16:13:10 +08001887
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001888 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
1889 if (adev->rmmio == NULL) {
1890 return -ENOMEM;
1891 }
1892 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
1893 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
1894
Ken Wangda69c1612016-01-21 19:08:55 +08001895 if (adev->asic_type >= CHIP_BONAIRE)
1896 /* doorbell bar mapping */
1897 amdgpu_doorbell_init(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001898
1899 /* io port mapping */
1900 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
1901 if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
1902 adev->rio_mem_size = pci_resource_len(adev->pdev, i);
1903 adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
1904 break;
1905 }
1906 }
1907 if (adev->rio_mem == NULL)
Amber Linb64a18c2017-01-04 08:06:58 -05001908 DRM_INFO("PCI I/O BAR is not found.\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001909
1910 /* early init functions */
1911 r = amdgpu_early_init(adev);
1912 if (r)
1913 return r;
1914
1915 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
1916 /* this will fail for cards that aren't VGA class devices, just
1917 * ignore it */
1918 vga_client_register(adev->pdev, adev, NULL, amdgpu_vga_set_decode);
1919
1920 if (amdgpu_runtime_pm == 1)
1921 runtime = true;
Alex Deuchere9bef452016-04-25 13:12:18 -04001922 if (amdgpu_device_is_px(ddev))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001923 runtime = true;
1924 vga_switcheroo_register_client(adev->pdev, &amdgpu_switcheroo_ops, runtime);
1925 if (runtime)
1926 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
1927
1928 /* Read BIOS */
Alex Deucher83ba1262016-06-03 18:21:41 -04001929 if (!amdgpu_get_bios(adev)) {
1930 r = -EINVAL;
1931 goto failed;
1932 }
Nils Wallméniusf7e9e9f2016-12-14 21:52:45 +01001933
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001934 r = amdgpu_atombios_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001935 if (r) {
1936 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001937 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001938 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001939
Monk Liu4e99a442016-03-31 13:26:59 +08001940 /* detect if we are with an SRIOV vbios */
1941 amdgpu_device_detect_sriov_bios(adev);
Andres Rodriguez048765a2016-06-11 02:51:32 -04001942
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001943 /* Post card if necessary */
Monk Liubec86372016-09-14 19:38:08 +08001944 if (amdgpu_vpost_needed(adev)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001945 if (!adev->bios) {
Monk Liubec86372016-09-14 19:38:08 +08001946 dev_err(adev->dev, "no vBIOS found\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001947 r = -EINVAL;
1948 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001949 }
Monk Liubec86372016-09-14 19:38:08 +08001950 DRM_INFO("GPU posting now...\n");
Monk Liu4e99a442016-03-31 13:26:59 +08001951 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
1952 if (r) {
1953 dev_err(adev->dev, "gpu post error!\n");
1954 goto failed;
1955 }
1956 } else {
1957 DRM_INFO("GPU post is not needed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001958 }
1959
Alex Deuchera5bde2f2016-09-23 16:23:41 -04001960 if (!adev->is_atom_fw) {
1961 /* Initialize clocks */
1962 r = amdgpu_atombios_get_clock_info(adev);
1963 if (r) {
1964 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
1965 return r;
1966 }
1967 /* init i2c buses */
1968 amdgpu_atombios_i2c_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001969 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001970
1971 /* Fence driver */
1972 r = amdgpu_fence_driver_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001973 if (r) {
1974 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04001975 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001976 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001977
1978 /* init the mode config */
1979 drm_mode_config_init(adev->ddev);
1980
1981 r = amdgpu_init(adev);
1982 if (r) {
Alex Deucher2c1a2782015-12-07 17:02:53 -05001983 dev_err(adev->dev, "amdgpu_init failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001984 amdgpu_fini(adev);
Alex Deucher83ba1262016-06-03 18:21:41 -04001985 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001986 }
1987
1988 adev->accel_working = true;
1989
Marek Olšák95844d22016-08-17 23:49:27 +02001990 /* Initialize the buffer migration limit. */
1991 if (amdgpu_moverate >= 0)
1992 max_MBps = amdgpu_moverate;
1993 else
1994 max_MBps = 8; /* Allow 8 MB/s. */
1995 /* Get a log2 for easy divisions. */
1996 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
1997
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001998 r = amdgpu_ib_pool_init(adev);
1999 if (r) {
2000 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
Alex Deucher83ba1262016-06-03 18:21:41 -04002001 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002002 }
2003
2004 r = amdgpu_ib_ring_tests(adev);
2005 if (r)
2006 DRM_ERROR("ib ring test failed (%d).\n", r);
2007
Monk Liu9bc92b92017-02-08 17:38:13 +08002008 amdgpu_fbdev_init(adev);
2009
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002010 r = amdgpu_gem_debugfs_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002011 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002012 DRM_ERROR("registering gem debugfs failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002013
2014 r = amdgpu_debugfs_regs_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002015 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002016 DRM_ERROR("registering register debugfs failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002017
Huang Rui50ab2532016-06-12 15:51:09 +08002018 r = amdgpu_debugfs_firmware_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002019 if (r)
Huang Rui50ab2532016-06-12 15:51:09 +08002020 DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
Huang Rui50ab2532016-06-12 15:51:09 +08002021
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002022 if ((amdgpu_testing & 1)) {
2023 if (adev->accel_working)
2024 amdgpu_test_moves(adev);
2025 else
2026 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
2027 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002028 if (amdgpu_benchmarking) {
2029 if (adev->accel_working)
2030 amdgpu_benchmark(adev, amdgpu_benchmarking);
2031 else
2032 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
2033 }
2034
2035 /* enable clockgating, etc. after ib tests, etc. since some blocks require
2036 * explicit gating rather than handling it automatically.
2037 */
2038 r = amdgpu_late_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002039 if (r) {
2040 dev_err(adev->dev, "amdgpu_late_init failed\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04002041 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002042 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002043
2044 return 0;
Alex Deucher83ba1262016-06-03 18:21:41 -04002045
2046failed:
2047 if (runtime)
2048 vga_switcheroo_fini_domain_pm_ops(adev->dev);
2049 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002050}
2051
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002052/**
2053 * amdgpu_device_fini - tear down the driver
2054 *
2055 * @adev: amdgpu_device pointer
2056 *
2057 * Tear down the driver info (all asics).
2058 * Called at driver shutdown.
2059 */
2060void amdgpu_device_fini(struct amdgpu_device *adev)
2061{
2062 int r;
2063
2064 DRM_INFO("amdgpu: finishing device.\n");
2065 adev->shutdown = true;
Grazvydas Ignotasa951ed82016-09-25 23:34:48 +03002066 drm_crtc_force_disable_all(adev->ddev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002067 /* evict vram memory */
2068 amdgpu_bo_evict_vram(adev);
2069 amdgpu_ib_pool_fini(adev);
2070 amdgpu_fence_driver_fini(adev);
2071 amdgpu_fbdev_fini(adev);
2072 r = amdgpu_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002073 adev->accel_working = false;
2074 /* free i2c buses */
2075 amdgpu_i2c_fini(adev);
2076 amdgpu_atombios_fini(adev);
2077 kfree(adev->bios);
2078 adev->bios = NULL;
2079 vga_switcheroo_unregister_client(adev->pdev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002080 if (adev->flags & AMD_IS_PX)
2081 vga_switcheroo_fini_domain_pm_ops(adev->dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002082 vga_client_register(adev->pdev, NULL, NULL, NULL);
2083 if (adev->rio_mem)
2084 pci_iounmap(adev->pdev, adev->rio_mem);
2085 adev->rio_mem = NULL;
2086 iounmap(adev->rmmio);
2087 adev->rmmio = NULL;
Ken Wangda69c1612016-01-21 19:08:55 +08002088 if (adev->asic_type >= CHIP_BONAIRE)
2089 amdgpu_doorbell_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002090 amdgpu_debugfs_regs_cleanup(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002091}
2092
2093
2094/*
2095 * Suspend & resume.
2096 */
2097/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002098 * amdgpu_device_suspend - initiate device suspend
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002099 *
2100 * @pdev: drm dev pointer
2101 * @state: suspend state
2102 *
2103 * Puts the hw in the suspend state (all asics).
2104 * Returns 0 for success or an error on failure.
2105 * Called at driver suspend.
2106 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002107int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002108{
2109 struct amdgpu_device *adev;
2110 struct drm_crtc *crtc;
2111 struct drm_connector *connector;
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002112 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002113
2114 if (dev == NULL || dev->dev_private == NULL) {
2115 return -ENODEV;
2116 }
2117
2118 adev = dev->dev_private;
2119
2120 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2121 return 0;
2122
2123 drm_kms_helper_poll_disable(dev);
2124
2125 /* turn off display hw */
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002126 drm_modeset_lock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002127 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2128 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
2129 }
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002130 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002131
Alex Deucher756e6882015-10-08 00:03:36 -04002132 /* unpin the front buffers and cursors */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002133 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Alex Deucher756e6882015-10-08 00:03:36 -04002134 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002135 struct amdgpu_framebuffer *rfb = to_amdgpu_framebuffer(crtc->primary->fb);
2136 struct amdgpu_bo *robj;
2137
Alex Deucher756e6882015-10-08 00:03:36 -04002138 if (amdgpu_crtc->cursor_bo) {
2139 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
2140 r = amdgpu_bo_reserve(aobj, false);
2141 if (r == 0) {
2142 amdgpu_bo_unpin(aobj);
2143 amdgpu_bo_unreserve(aobj);
2144 }
2145 }
2146
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002147 if (rfb == NULL || rfb->obj == NULL) {
2148 continue;
2149 }
2150 robj = gem_to_amdgpu_bo(rfb->obj);
2151 /* don't unpin kernel fb objects */
2152 if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
2153 r = amdgpu_bo_reserve(robj, false);
2154 if (r == 0) {
2155 amdgpu_bo_unpin(robj);
2156 amdgpu_bo_unreserve(robj);
2157 }
2158 }
2159 }
2160 /* evict vram memory */
2161 amdgpu_bo_evict_vram(adev);
2162
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002163 amdgpu_fence_driver_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002164
2165 r = amdgpu_suspend(adev);
2166
Alex Deuchera0a71e42016-10-10 12:41:36 -04002167 /* evict remaining vram memory
2168 * This second call to evict vram is to evict the gart page table
2169 * using the CPU.
2170 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002171 amdgpu_bo_evict_vram(adev);
2172
Alex Deucherbe34d3b2017-03-03 14:26:51 -05002173 if (adev->is_atom_fw)
2174 amdgpu_atomfirmware_scratch_regs_save(adev);
2175 else
2176 amdgpu_atombios_scratch_regs_save(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002177 pci_save_state(dev->pdev);
2178 if (suspend) {
2179 /* Shut down the device */
2180 pci_disable_device(dev->pdev);
2181 pci_set_power_state(dev->pdev, PCI_D3hot);
jimqu74b0b152016-09-07 17:09:12 +08002182 } else {
2183 r = amdgpu_asic_reset(adev);
2184 if (r)
2185 DRM_ERROR("amdgpu asic reset failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002186 }
2187
2188 if (fbcon) {
2189 console_lock();
2190 amdgpu_fbdev_set_suspend(adev, 1);
2191 console_unlock();
2192 }
2193 return 0;
2194}
2195
2196/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002197 * amdgpu_device_resume - initiate device resume
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002198 *
2199 * @pdev: drm dev pointer
2200 *
2201 * Bring the hw back to operating state (all asics).
2202 * Returns 0 for success or an error on failure.
2203 * Called at driver resume.
2204 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002205int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002206{
2207 struct drm_connector *connector;
2208 struct amdgpu_device *adev = dev->dev_private;
Alex Deucher756e6882015-10-08 00:03:36 -04002209 struct drm_crtc *crtc;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002210 int r;
2211
2212 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2213 return 0;
2214
jimqu74b0b152016-09-07 17:09:12 +08002215 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002216 console_lock();
jimqu74b0b152016-09-07 17:09:12 +08002217
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002218 if (resume) {
2219 pci_set_power_state(dev->pdev, PCI_D0);
2220 pci_restore_state(dev->pdev);
jimqu74b0b152016-09-07 17:09:12 +08002221 r = pci_enable_device(dev->pdev);
2222 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002223 if (fbcon)
2224 console_unlock();
jimqu74b0b152016-09-07 17:09:12 +08002225 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002226 }
2227 }
Alex Deucherbe34d3b2017-03-03 14:26:51 -05002228 if (adev->is_atom_fw)
2229 amdgpu_atomfirmware_scratch_regs_restore(adev);
2230 else
2231 amdgpu_atombios_scratch_regs_restore(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002232
2233 /* post card */
Jim Quc836fec2017-02-10 15:59:59 +08002234 if (amdgpu_need_post(adev)) {
jimqu74b0b152016-09-07 17:09:12 +08002235 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2236 if (r)
2237 DRM_ERROR("amdgpu asic init failed\n");
2238 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002239
2240 r = amdgpu_resume(adev);
Flora Cuica198522016-02-04 15:10:08 +08002241 if (r)
2242 DRM_ERROR("amdgpu_resume failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002243
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002244 amdgpu_fence_driver_resume(adev);
2245
Flora Cuica198522016-02-04 15:10:08 +08002246 if (resume) {
2247 r = amdgpu_ib_ring_tests(adev);
2248 if (r)
2249 DRM_ERROR("ib ring test failed (%d).\n", r);
2250 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002251
2252 r = amdgpu_late_init(adev);
Jim Quc085bd52017-03-01 15:53:29 +08002253 if (r) {
2254 if (fbcon)
2255 console_unlock();
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002256 return r;
Jim Quc085bd52017-03-01 15:53:29 +08002257 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002258
Alex Deucher756e6882015-10-08 00:03:36 -04002259 /* pin cursors */
2260 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2261 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2262
2263 if (amdgpu_crtc->cursor_bo) {
2264 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
2265 r = amdgpu_bo_reserve(aobj, false);
2266 if (r == 0) {
2267 r = amdgpu_bo_pin(aobj,
2268 AMDGPU_GEM_DOMAIN_VRAM,
2269 &amdgpu_crtc->cursor_addr);
2270 if (r != 0)
2271 DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
2272 amdgpu_bo_unreserve(aobj);
2273 }
2274 }
2275 }
2276
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002277 /* blat the mode back in */
2278 if (fbcon) {
2279 drm_helper_resume_force_mode(dev);
2280 /* turn on display hw */
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002281 drm_modeset_lock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002282 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2283 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
2284 }
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002285 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002286 }
2287
2288 drm_kms_helper_poll_enable(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002289
2290 /*
2291 * Most of the connector probing functions try to acquire runtime pm
2292 * refs to ensure that the GPU is powered on when connector polling is
2293 * performed. Since we're calling this from a runtime PM callback,
2294 * trying to acquire rpm refs will cause us to deadlock.
2295 *
2296 * Since we're guaranteed to be holding the rpm lock, it's safe to
2297 * temporarily disable the rpm helpers so this doesn't deadlock us.
2298 */
2299#ifdef CONFIG_PM
2300 dev->dev->power.disable_depth++;
2301#endif
Alex Deucher54fb2a52015-11-24 14:30:56 -05002302 drm_helper_hpd_irq_event(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002303#ifdef CONFIG_PM
2304 dev->dev->power.disable_depth--;
2305#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002306
2307 if (fbcon) {
2308 amdgpu_fbdev_set_suspend(adev, 0);
2309 console_unlock();
2310 }
2311
2312 return 0;
2313}
2314
Chunming Zhou63fbf422016-07-15 11:19:20 +08002315static bool amdgpu_check_soft_reset(struct amdgpu_device *adev)
2316{
2317 int i;
2318 bool asic_hang = false;
2319
2320 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002321 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou63fbf422016-07-15 11:19:20 +08002322 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002323 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
2324 adev->ip_blocks[i].status.hang =
2325 adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
2326 if (adev->ip_blocks[i].status.hang) {
2327 DRM_INFO("IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
Chunming Zhou63fbf422016-07-15 11:19:20 +08002328 asic_hang = true;
2329 }
2330 }
2331 return asic_hang;
2332}
2333
Baoyou Xie4d446652016-09-18 22:09:35 +08002334static int amdgpu_pre_soft_reset(struct amdgpu_device *adev)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002335{
2336 int i, r = 0;
2337
2338 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002339 if (!adev->ip_blocks[i].status.valid)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002340 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002341 if (adev->ip_blocks[i].status.hang &&
2342 adev->ip_blocks[i].version->funcs->pre_soft_reset) {
2343 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
Chunming Zhoud31a5012016-07-18 10:04:34 +08002344 if (r)
2345 return r;
2346 }
2347 }
2348
2349 return 0;
2350}
2351
Chunming Zhou35d782f2016-07-15 15:57:13 +08002352static bool amdgpu_need_full_reset(struct amdgpu_device *adev)
2353{
Alex Deucherda146d32016-10-13 16:07:03 -04002354 int i;
2355
2356 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002357 if (!adev->ip_blocks[i].status.valid)
Alex Deucherda146d32016-10-13 16:07:03 -04002358 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002359 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
2360 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
2361 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
2362 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE)) {
2363 if (adev->ip_blocks[i].status.hang) {
Alex Deucherda146d32016-10-13 16:07:03 -04002364 DRM_INFO("Some block need full reset!\n");
2365 return true;
2366 }
2367 }
Chunming Zhou35d782f2016-07-15 15:57:13 +08002368 }
2369 return false;
2370}
2371
2372static int amdgpu_soft_reset(struct amdgpu_device *adev)
2373{
2374 int i, r = 0;
2375
2376 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002377 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002378 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002379 if (adev->ip_blocks[i].status.hang &&
2380 adev->ip_blocks[i].version->funcs->soft_reset) {
2381 r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002382 if (r)
2383 return r;
2384 }
2385 }
2386
2387 return 0;
2388}
2389
2390static int amdgpu_post_soft_reset(struct amdgpu_device *adev)
2391{
2392 int i, r = 0;
2393
2394 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002395 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002396 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002397 if (adev->ip_blocks[i].status.hang &&
2398 adev->ip_blocks[i].version->funcs->post_soft_reset)
2399 r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002400 if (r)
2401 return r;
2402 }
2403
2404 return 0;
2405}
2406
Chunming Zhou3ad81f12016-08-05 17:30:17 +08002407bool amdgpu_need_backup(struct amdgpu_device *adev)
2408{
2409 if (adev->flags & AMD_IS_APU)
2410 return false;
2411
2412 return amdgpu_lockup_timeout > 0 ? true : false;
2413}
2414
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002415static int amdgpu_recover_vram_from_shadow(struct amdgpu_device *adev,
2416 struct amdgpu_ring *ring,
2417 struct amdgpu_bo *bo,
Chris Wilsonf54d1862016-10-25 13:00:45 +01002418 struct dma_fence **fence)
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002419{
2420 uint32_t domain;
2421 int r;
2422
2423 if (!bo->shadow)
2424 return 0;
2425
2426 r = amdgpu_bo_reserve(bo, false);
2427 if (r)
2428 return r;
2429 domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
2430 /* if bo has been evicted, then no need to recover */
2431 if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
2432 r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
2433 NULL, fence, true);
2434 if (r) {
2435 DRM_ERROR("recover page table failed!\n");
2436 goto err;
2437 }
2438 }
2439err:
2440 amdgpu_bo_unreserve(bo);
2441 return r;
2442}
2443
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002444/**
Monk Liua90ad3c2017-01-23 14:22:08 +08002445 * amdgpu_sriov_gpu_reset - reset the asic
2446 *
2447 * @adev: amdgpu device pointer
2448 * @voluntary: if this reset is requested by guest.
2449 * (true means by guest and false means by HYPERVISOR )
2450 *
2451 * Attempt the reset the GPU if it has hung (all asics).
2452 * for SRIOV case.
2453 * Returns 0 for success or an error on failure.
2454 */
2455int amdgpu_sriov_gpu_reset(struct amdgpu_device *adev, bool voluntary)
2456{
2457 int i, r = 0;
2458 int resched;
2459 struct amdgpu_bo *bo, *tmp;
2460 struct amdgpu_ring *ring;
2461 struct dma_fence *fence = NULL, *next = NULL;
2462
Monk Liu147b5982017-01-25 15:48:01 +08002463 mutex_lock(&adev->virt.lock_reset);
Monk Liua90ad3c2017-01-23 14:22:08 +08002464 atomic_inc(&adev->gpu_reset_counter);
Monk Liu1fb37a32017-01-26 15:36:37 +08002465 adev->gfx.in_reset = true;
Monk Liua90ad3c2017-01-23 14:22:08 +08002466
2467 /* block TTM */
2468 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
2469
2470 /* block scheduler */
2471 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2472 ring = adev->rings[i];
2473
2474 if (!ring || !ring->sched.thread)
2475 continue;
2476
2477 kthread_park(ring->sched.thread);
2478 amd_sched_hw_job_reset(&ring->sched);
2479 }
2480
2481 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
2482 amdgpu_fence_driver_force_completion(adev);
2483
2484 /* request to take full control of GPU before re-initialization */
2485 if (voluntary)
2486 amdgpu_virt_reset_gpu(adev);
2487 else
2488 amdgpu_virt_request_full_gpu(adev, true);
2489
2490
2491 /* Resume IP prior to SMC */
Monk Liue4f0fdc2017-02-09 11:55:49 +08002492 amdgpu_sriov_reinit_early(adev);
Monk Liua90ad3c2017-01-23 14:22:08 +08002493
2494 /* we need recover gart prior to run SMC/CP/SDMA resume */
2495 amdgpu_ttm_recover_gart(adev);
2496
2497 /* now we are okay to resume SMC/CP/SDMA */
Monk Liue4f0fdc2017-02-09 11:55:49 +08002498 amdgpu_sriov_reinit_late(adev);
Monk Liua90ad3c2017-01-23 14:22:08 +08002499
2500 amdgpu_irq_gpu_reset_resume_helper(adev);
2501
2502 if (amdgpu_ib_ring_tests(adev))
2503 dev_err(adev->dev, "[GPU_RESET] ib ring test failed (%d).\n", r);
2504
2505 /* release full control of GPU after ib test */
2506 amdgpu_virt_release_full_gpu(adev, true);
2507
2508 DRM_INFO("recover vram bo from shadow\n");
2509
2510 ring = adev->mman.buffer_funcs_ring;
2511 mutex_lock(&adev->shadow_list_lock);
2512 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
2513 amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
2514 if (fence) {
2515 r = dma_fence_wait(fence, false);
2516 if (r) {
2517 WARN(r, "recovery from shadow isn't completed\n");
2518 break;
2519 }
2520 }
2521
2522 dma_fence_put(fence);
2523 fence = next;
2524 }
2525 mutex_unlock(&adev->shadow_list_lock);
2526
2527 if (fence) {
2528 r = dma_fence_wait(fence, false);
2529 if (r)
2530 WARN(r, "recovery from shadow isn't completed\n");
2531 }
2532 dma_fence_put(fence);
2533
2534 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2535 struct amdgpu_ring *ring = adev->rings[i];
2536 if (!ring || !ring->sched.thread)
2537 continue;
2538
2539 amd_sched_job_recovery(&ring->sched);
2540 kthread_unpark(ring->sched.thread);
2541 }
2542
2543 drm_helper_resume_force_mode(adev->ddev);
2544 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
2545 if (r) {
2546 /* bad news, how to tell it to userspace ? */
2547 dev_info(adev->dev, "GPU reset failed\n");
2548 }
2549
Monk Liu1fb37a32017-01-26 15:36:37 +08002550 adev->gfx.in_reset = false;
Monk Liu147b5982017-01-25 15:48:01 +08002551 mutex_unlock(&adev->virt.lock_reset);
Monk Liua90ad3c2017-01-23 14:22:08 +08002552 return r;
2553}
2554
2555/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002556 * amdgpu_gpu_reset - reset the asic
2557 *
2558 * @adev: amdgpu device pointer
2559 *
2560 * Attempt the reset the GPU if it has hung (all asics).
2561 * Returns 0 for success or an error on failure.
2562 */
2563int amdgpu_gpu_reset(struct amdgpu_device *adev)
2564{
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002565 int i, r;
2566 int resched;
Chunming Zhou35d782f2016-07-15 15:57:13 +08002567 bool need_full_reset;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002568
Xiangliang Yufb140b22016-12-17 22:48:57 +08002569 if (amdgpu_sriov_vf(adev))
Monk Liua90ad3c2017-01-23 14:22:08 +08002570 return amdgpu_sriov_gpu_reset(adev, true);
Xiangliang Yufb140b22016-12-17 22:48:57 +08002571
Chunming Zhou63fbf422016-07-15 11:19:20 +08002572 if (!amdgpu_check_soft_reset(adev)) {
2573 DRM_INFO("No hardware hang detected. Did some blocks stall?\n");
2574 return 0;
2575 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002576
Marek Olšákd94aed52015-05-05 21:13:49 +02002577 atomic_inc(&adev->gpu_reset_counter);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002578
Chunming Zhoua3c47d62016-06-30 16:44:41 +08002579 /* block TTM */
2580 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
2581
Chunming Zhou0875dc92016-06-12 15:41:58 +08002582 /* block scheduler */
2583 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2584 struct amdgpu_ring *ring = adev->rings[i];
2585
2586 if (!ring)
2587 continue;
2588 kthread_park(ring->sched.thread);
Chunming Zhouaa1c8902016-06-30 13:56:02 +08002589 amd_sched_hw_job_reset(&ring->sched);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002590 }
Chunming Zhou2200eda2016-06-30 16:53:02 +08002591 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
2592 amdgpu_fence_driver_force_completion(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002593
Chunming Zhou35d782f2016-07-15 15:57:13 +08002594 need_full_reset = amdgpu_need_full_reset(adev);
2595
2596 if (!need_full_reset) {
2597 amdgpu_pre_soft_reset(adev);
2598 r = amdgpu_soft_reset(adev);
2599 amdgpu_post_soft_reset(adev);
2600 if (r || amdgpu_check_soft_reset(adev)) {
2601 DRM_INFO("soft reset failed, will fallback to full reset!\n");
2602 need_full_reset = true;
2603 }
2604 }
2605
2606 if (need_full_reset) {
Chunming Zhou35d782f2016-07-15 15:57:13 +08002607 r = amdgpu_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002608
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002609retry:
Chunming Zhou35d782f2016-07-15 15:57:13 +08002610 /* Disable fb access */
2611 if (adev->mode_info.num_crtc) {
2612 struct amdgpu_mode_mc_save save;
2613 amdgpu_display_stop_mc_access(adev, &save);
2614 amdgpu_wait_for_idle(adev, AMD_IP_BLOCK_TYPE_GMC);
2615 }
Alex Deucherbe34d3b2017-03-03 14:26:51 -05002616 if (adev->is_atom_fw)
2617 amdgpu_atomfirmware_scratch_regs_save(adev);
2618 else
2619 amdgpu_atombios_scratch_regs_save(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002620 r = amdgpu_asic_reset(adev);
Alex Deucherbe34d3b2017-03-03 14:26:51 -05002621 if (adev->is_atom_fw)
2622 amdgpu_atomfirmware_scratch_regs_restore(adev);
2623 else
2624 amdgpu_atombios_scratch_regs_restore(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002625 /* post card */
2626 amdgpu_atom_asic_init(adev->mode_info.atom_context);
Alex Deucherbfa99262016-01-15 11:59:48 -05002627
Chunming Zhou35d782f2016-07-15 15:57:13 +08002628 if (!r) {
2629 dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
2630 r = amdgpu_resume(adev);
2631 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002632 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002633 if (!r) {
Chunming Zhoue72cfd52016-07-27 13:15:20 +08002634 amdgpu_irq_gpu_reset_resume_helper(adev);
Chunming Zhou2c0d7312016-08-30 16:36:25 +08002635 if (need_full_reset && amdgpu_need_backup(adev)) {
2636 r = amdgpu_ttm_recover_gart(adev);
2637 if (r)
2638 DRM_ERROR("gart recovery failed!!!\n");
2639 }
Chunming Zhou1f465082016-06-30 15:02:26 +08002640 r = amdgpu_ib_ring_tests(adev);
2641 if (r) {
2642 dev_err(adev->dev, "ib ring test failed (%d).\n", r);
Chunming Zhou40019dc2016-06-29 16:01:49 +08002643 r = amdgpu_suspend(adev);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002644 need_full_reset = true;
Chunming Zhou40019dc2016-06-29 16:01:49 +08002645 goto retry;
Chunming Zhou1f465082016-06-30 15:02:26 +08002646 }
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002647 /**
2648 * recovery vm page tables, since we cannot depend on VRAM is
2649 * consistent after gpu full reset.
2650 */
2651 if (need_full_reset && amdgpu_need_backup(adev)) {
2652 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
2653 struct amdgpu_bo *bo, *tmp;
Chris Wilsonf54d1862016-10-25 13:00:45 +01002654 struct dma_fence *fence = NULL, *next = NULL;
Chunming Zhou1f465082016-06-30 15:02:26 +08002655
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002656 DRM_INFO("recover vram bo from shadow\n");
2657 mutex_lock(&adev->shadow_list_lock);
2658 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
2659 amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
2660 if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01002661 r = dma_fence_wait(fence, false);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002662 if (r) {
Monk Liu1d7b17b2017-01-22 18:52:56 +08002663 WARN(r, "recovery from shadow isn't completed\n");
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002664 break;
2665 }
2666 }
2667
Chris Wilsonf54d1862016-10-25 13:00:45 +01002668 dma_fence_put(fence);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002669 fence = next;
2670 }
2671 mutex_unlock(&adev->shadow_list_lock);
2672 if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01002673 r = dma_fence_wait(fence, false);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002674 if (r)
Monk Liu1d7b17b2017-01-22 18:52:56 +08002675 WARN(r, "recovery from shadow isn't completed\n");
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002676 }
Chris Wilsonf54d1862016-10-25 13:00:45 +01002677 dma_fence_put(fence);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002678 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002679 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2680 struct amdgpu_ring *ring = adev->rings[i];
2681 if (!ring)
2682 continue;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002683
Chunming Zhouaa1c8902016-06-30 13:56:02 +08002684 amd_sched_job_recovery(&ring->sched);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002685 kthread_unpark(ring->sched.thread);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002686 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002687 } else {
Chunming Zhou2200eda2016-06-30 16:53:02 +08002688 dev_err(adev->dev, "asic resume failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002689 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
Chunming Zhou0875dc92016-06-12 15:41:58 +08002690 if (adev->rings[i]) {
2691 kthread_unpark(adev->rings[i]->sched.thread);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002692 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002693 }
2694 }
2695
2696 drm_helper_resume_force_mode(adev->ddev);
2697
2698 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
2699 if (r) {
2700 /* bad news, how to tell it to userspace ? */
2701 dev_info(adev->dev, "GPU reset failed\n");
2702 }
2703
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002704 return r;
2705}
2706
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002707void amdgpu_get_pcie_info(struct amdgpu_device *adev)
2708{
2709 u32 mask;
2710 int ret;
2711
Alex Deuchercd474ba2016-02-04 10:21:23 -05002712 if (amdgpu_pcie_gen_cap)
2713 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
2714
2715 if (amdgpu_pcie_lane_cap)
2716 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
2717
2718 /* covers APUs as well */
2719 if (pci_is_root_bus(adev->pdev->bus)) {
2720 if (adev->pm.pcie_gen_mask == 0)
2721 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
2722 if (adev->pm.pcie_mlw_mask == 0)
2723 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002724 return;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002725 }
Alex Deuchercd474ba2016-02-04 10:21:23 -05002726
2727 if (adev->pm.pcie_gen_mask == 0) {
2728 ret = drm_pcie_get_speed_cap_mask(adev->ddev, &mask);
2729 if (!ret) {
2730 adev->pm.pcie_gen_mask = (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
2731 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
2732 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
2733
2734 if (mask & DRM_PCIE_SPEED_25)
2735 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
2736 if (mask & DRM_PCIE_SPEED_50)
2737 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2;
2738 if (mask & DRM_PCIE_SPEED_80)
2739 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3;
2740 } else {
2741 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
2742 }
2743 }
2744 if (adev->pm.pcie_mlw_mask == 0) {
2745 ret = drm_pcie_get_max_link_width(adev->ddev, &mask);
2746 if (!ret) {
2747 switch (mask) {
2748 case 32:
2749 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
2750 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
2751 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2752 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2753 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2754 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2755 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2756 break;
2757 case 16:
2758 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
2759 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2760 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2761 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2762 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2763 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2764 break;
2765 case 12:
2766 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2767 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2768 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2769 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2770 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2771 break;
2772 case 8:
2773 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2774 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2775 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2776 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2777 break;
2778 case 4:
2779 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2780 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2781 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2782 break;
2783 case 2:
2784 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2785 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2786 break;
2787 case 1:
2788 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
2789 break;
2790 default:
2791 break;
2792 }
2793 } else {
2794 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002795 }
2796 }
2797}
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002798
2799/*
2800 * Debugfs
2801 */
2802int amdgpu_debugfs_add_files(struct amdgpu_device *adev,
Nils Wallménius06ab6832016-05-02 12:46:15 -04002803 const struct drm_info_list *files,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002804 unsigned nfiles)
2805{
2806 unsigned i;
2807
2808 for (i = 0; i < adev->debugfs_count; i++) {
2809 if (adev->debugfs[i].files == files) {
2810 /* Already registered */
2811 return 0;
2812 }
2813 }
2814
2815 i = adev->debugfs_count + 1;
2816 if (i > AMDGPU_DEBUGFS_MAX_COMPONENTS) {
2817 DRM_ERROR("Reached maximum number of debugfs components.\n");
2818 DRM_ERROR("Report so we increase "
2819 "AMDGPU_DEBUGFS_MAX_COMPONENTS.\n");
2820 return -EINVAL;
2821 }
2822 adev->debugfs[adev->debugfs_count].files = files;
2823 adev->debugfs[adev->debugfs_count].num_files = nfiles;
2824 adev->debugfs_count = i;
2825#if defined(CONFIG_DEBUG_FS)
2826 drm_debugfs_create_files(files, nfiles,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002827 adev->ddev->primary->debugfs_root,
2828 adev->ddev->primary);
2829#endif
2830 return 0;
2831}
2832
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002833#if defined(CONFIG_DEBUG_FS)
2834
2835static ssize_t amdgpu_debugfs_regs_read(struct file *f, char __user *buf,
2836 size_t size, loff_t *pos)
2837{
Al Viro45063092016-12-04 18:24:56 -05002838 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002839 ssize_t result = 0;
2840 int r;
Tom St Denisbd122672016-07-28 09:39:22 -04002841 bool pm_pg_lock, use_bank;
Tom St Denis566281592016-06-27 11:55:07 -04002842 unsigned instance_bank, sh_bank, se_bank;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002843
2844 if (size & 0x3 || *pos & 0x3)
2845 return -EINVAL;
2846
Tom St Denisbd122672016-07-28 09:39:22 -04002847 /* are we reading registers for which a PG lock is necessary? */
2848 pm_pg_lock = (*pos >> 23) & 1;
2849
Tom St Denis566281592016-06-27 11:55:07 -04002850 if (*pos & (1ULL << 62)) {
2851 se_bank = (*pos >> 24) & 0x3FF;
2852 sh_bank = (*pos >> 34) & 0x3FF;
2853 instance_bank = (*pos >> 44) & 0x3FF;
Tom St Denis32977f92016-10-09 07:41:26 -04002854
2855 if (se_bank == 0x3FF)
2856 se_bank = 0xFFFFFFFF;
2857 if (sh_bank == 0x3FF)
2858 sh_bank = 0xFFFFFFFF;
2859 if (instance_bank == 0x3FF)
2860 instance_bank = 0xFFFFFFFF;
Tom St Denis566281592016-06-27 11:55:07 -04002861 use_bank = 1;
Tom St Denis566281592016-06-27 11:55:07 -04002862 } else {
2863 use_bank = 0;
2864 }
2865
Tom St Denis801a6aa9a62017-03-15 05:34:25 -04002866 *pos &= (1UL << 22) - 1;
Tom St Denisbd122672016-07-28 09:39:22 -04002867
Tom St Denis566281592016-06-27 11:55:07 -04002868 if (use_bank) {
Tom St Denis32977f92016-10-09 07:41:26 -04002869 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
2870 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
Tom St Denis566281592016-06-27 11:55:07 -04002871 return -EINVAL;
2872 mutex_lock(&adev->grbm_idx_mutex);
2873 amdgpu_gfx_select_se_sh(adev, se_bank,
2874 sh_bank, instance_bank);
2875 }
2876
Tom St Denisbd122672016-07-28 09:39:22 -04002877 if (pm_pg_lock)
2878 mutex_lock(&adev->pm.mutex);
2879
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002880 while (size) {
2881 uint32_t value;
2882
2883 if (*pos > adev->rmmio_size)
Tom St Denis566281592016-06-27 11:55:07 -04002884 goto end;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002885
2886 value = RREG32(*pos >> 2);
2887 r = put_user(value, (uint32_t *)buf);
Tom St Denis566281592016-06-27 11:55:07 -04002888 if (r) {
2889 result = r;
2890 goto end;
2891 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002892
2893 result += 4;
2894 buf += 4;
2895 *pos += 4;
2896 size -= 4;
2897 }
2898
Tom St Denis566281592016-06-27 11:55:07 -04002899end:
2900 if (use_bank) {
2901 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
2902 mutex_unlock(&adev->grbm_idx_mutex);
2903 }
2904
Tom St Denisbd122672016-07-28 09:39:22 -04002905 if (pm_pg_lock)
2906 mutex_unlock(&adev->pm.mutex);
2907
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002908 return result;
2909}
2910
2911static ssize_t amdgpu_debugfs_regs_write(struct file *f, const char __user *buf,
2912 size_t size, loff_t *pos)
2913{
Al Viro45063092016-12-04 18:24:56 -05002914 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002915 ssize_t result = 0;
2916 int r;
Tom St Denis394fdde2016-10-10 07:31:23 -04002917 bool pm_pg_lock, use_bank;
2918 unsigned instance_bank, sh_bank, se_bank;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002919
2920 if (size & 0x3 || *pos & 0x3)
2921 return -EINVAL;
2922
Tom St Denis394fdde2016-10-10 07:31:23 -04002923 /* are we reading registers for which a PG lock is necessary? */
2924 pm_pg_lock = (*pos >> 23) & 1;
2925
2926 if (*pos & (1ULL << 62)) {
2927 se_bank = (*pos >> 24) & 0x3FF;
2928 sh_bank = (*pos >> 34) & 0x3FF;
2929 instance_bank = (*pos >> 44) & 0x3FF;
2930
2931 if (se_bank == 0x3FF)
2932 se_bank = 0xFFFFFFFF;
2933 if (sh_bank == 0x3FF)
2934 sh_bank = 0xFFFFFFFF;
2935 if (instance_bank == 0x3FF)
2936 instance_bank = 0xFFFFFFFF;
2937 use_bank = 1;
2938 } else {
2939 use_bank = 0;
2940 }
2941
Tom St Denis801a6aa9a62017-03-15 05:34:25 -04002942 *pos &= (1UL << 22) - 1;
Tom St Denis394fdde2016-10-10 07:31:23 -04002943
2944 if (use_bank) {
2945 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
2946 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
2947 return -EINVAL;
2948 mutex_lock(&adev->grbm_idx_mutex);
2949 amdgpu_gfx_select_se_sh(adev, se_bank,
2950 sh_bank, instance_bank);
2951 }
2952
2953 if (pm_pg_lock)
2954 mutex_lock(&adev->pm.mutex);
2955
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002956 while (size) {
2957 uint32_t value;
2958
2959 if (*pos > adev->rmmio_size)
2960 return result;
2961
2962 r = get_user(value, (uint32_t *)buf);
2963 if (r)
2964 return r;
2965
2966 WREG32(*pos >> 2, value);
2967
2968 result += 4;
2969 buf += 4;
2970 *pos += 4;
2971 size -= 4;
2972 }
2973
Tom St Denis394fdde2016-10-10 07:31:23 -04002974 if (use_bank) {
2975 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
2976 mutex_unlock(&adev->grbm_idx_mutex);
2977 }
2978
2979 if (pm_pg_lock)
2980 mutex_unlock(&adev->pm.mutex);
2981
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002982 return result;
2983}
2984
Tom St Denisadcec282016-04-15 13:08:44 -04002985static ssize_t amdgpu_debugfs_regs_pcie_read(struct file *f, char __user *buf,
2986 size_t size, loff_t *pos)
2987{
Al Viro45063092016-12-04 18:24:56 -05002988 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04002989 ssize_t result = 0;
2990 int r;
2991
2992 if (size & 0x3 || *pos & 0x3)
2993 return -EINVAL;
2994
2995 while (size) {
2996 uint32_t value;
2997
2998 value = RREG32_PCIE(*pos >> 2);
2999 r = put_user(value, (uint32_t *)buf);
3000 if (r)
3001 return r;
3002
3003 result += 4;
3004 buf += 4;
3005 *pos += 4;
3006 size -= 4;
3007 }
3008
3009 return result;
3010}
3011
3012static ssize_t amdgpu_debugfs_regs_pcie_write(struct file *f, const char __user *buf,
3013 size_t size, loff_t *pos)
3014{
Al Viro45063092016-12-04 18:24:56 -05003015 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003016 ssize_t result = 0;
3017 int r;
3018
3019 if (size & 0x3 || *pos & 0x3)
3020 return -EINVAL;
3021
3022 while (size) {
3023 uint32_t value;
3024
3025 r = get_user(value, (uint32_t *)buf);
3026 if (r)
3027 return r;
3028
3029 WREG32_PCIE(*pos >> 2, value);
3030
3031 result += 4;
3032 buf += 4;
3033 *pos += 4;
3034 size -= 4;
3035 }
3036
3037 return result;
3038}
3039
3040static ssize_t amdgpu_debugfs_regs_didt_read(struct file *f, char __user *buf,
3041 size_t size, loff_t *pos)
3042{
Al Viro45063092016-12-04 18:24:56 -05003043 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003044 ssize_t result = 0;
3045 int r;
3046
3047 if (size & 0x3 || *pos & 0x3)
3048 return -EINVAL;
3049
3050 while (size) {
3051 uint32_t value;
3052
3053 value = RREG32_DIDT(*pos >> 2);
3054 r = put_user(value, (uint32_t *)buf);
3055 if (r)
3056 return r;
3057
3058 result += 4;
3059 buf += 4;
3060 *pos += 4;
3061 size -= 4;
3062 }
3063
3064 return result;
3065}
3066
3067static ssize_t amdgpu_debugfs_regs_didt_write(struct file *f, const char __user *buf,
3068 size_t size, loff_t *pos)
3069{
Al Viro45063092016-12-04 18:24:56 -05003070 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003071 ssize_t result = 0;
3072 int r;
3073
3074 if (size & 0x3 || *pos & 0x3)
3075 return -EINVAL;
3076
3077 while (size) {
3078 uint32_t value;
3079
3080 r = get_user(value, (uint32_t *)buf);
3081 if (r)
3082 return r;
3083
3084 WREG32_DIDT(*pos >> 2, value);
3085
3086 result += 4;
3087 buf += 4;
3088 *pos += 4;
3089 size -= 4;
3090 }
3091
3092 return result;
3093}
3094
3095static ssize_t amdgpu_debugfs_regs_smc_read(struct file *f, char __user *buf,
3096 size_t size, loff_t *pos)
3097{
Al Viro45063092016-12-04 18:24:56 -05003098 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003099 ssize_t result = 0;
3100 int r;
3101
3102 if (size & 0x3 || *pos & 0x3)
3103 return -EINVAL;
3104
3105 while (size) {
3106 uint32_t value;
3107
Tom St Denis6fc0dea2016-08-29 08:39:29 -04003108 value = RREG32_SMC(*pos);
Tom St Denisadcec282016-04-15 13:08:44 -04003109 r = put_user(value, (uint32_t *)buf);
3110 if (r)
3111 return r;
3112
3113 result += 4;
3114 buf += 4;
3115 *pos += 4;
3116 size -= 4;
3117 }
3118
3119 return result;
3120}
3121
3122static ssize_t amdgpu_debugfs_regs_smc_write(struct file *f, const char __user *buf,
3123 size_t size, loff_t *pos)
3124{
Al Viro45063092016-12-04 18:24:56 -05003125 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003126 ssize_t result = 0;
3127 int r;
3128
3129 if (size & 0x3 || *pos & 0x3)
3130 return -EINVAL;
3131
3132 while (size) {
3133 uint32_t value;
3134
3135 r = get_user(value, (uint32_t *)buf);
3136 if (r)
3137 return r;
3138
Tom St Denis6fc0dea2016-08-29 08:39:29 -04003139 WREG32_SMC(*pos, value);
Tom St Denisadcec282016-04-15 13:08:44 -04003140
3141 result += 4;
3142 buf += 4;
3143 *pos += 4;
3144 size -= 4;
3145 }
3146
3147 return result;
3148}
3149
Tom St Denis1e051412016-06-27 09:57:18 -04003150static ssize_t amdgpu_debugfs_gca_config_read(struct file *f, char __user *buf,
3151 size_t size, loff_t *pos)
3152{
Al Viro45063092016-12-04 18:24:56 -05003153 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denis1e051412016-06-27 09:57:18 -04003154 ssize_t result = 0;
3155 int r;
3156 uint32_t *config, no_regs = 0;
3157
3158 if (size & 0x3 || *pos & 0x3)
3159 return -EINVAL;
3160
Markus Elfringecab7662016-09-18 17:00:52 +02003161 config = kmalloc_array(256, sizeof(*config), GFP_KERNEL);
Tom St Denis1e051412016-06-27 09:57:18 -04003162 if (!config)
3163 return -ENOMEM;
3164
3165 /* version, increment each time something is added */
Tom St Denis9a999352017-01-18 13:01:25 -05003166 config[no_regs++] = 3;
Tom St Denis1e051412016-06-27 09:57:18 -04003167 config[no_regs++] = adev->gfx.config.max_shader_engines;
3168 config[no_regs++] = adev->gfx.config.max_tile_pipes;
3169 config[no_regs++] = adev->gfx.config.max_cu_per_sh;
3170 config[no_regs++] = adev->gfx.config.max_sh_per_se;
3171 config[no_regs++] = adev->gfx.config.max_backends_per_se;
3172 config[no_regs++] = adev->gfx.config.max_texture_channel_caches;
3173 config[no_regs++] = adev->gfx.config.max_gprs;
3174 config[no_regs++] = adev->gfx.config.max_gs_threads;
3175 config[no_regs++] = adev->gfx.config.max_hw_contexts;
3176 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_frontend;
3177 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_backend;
3178 config[no_regs++] = adev->gfx.config.sc_hiz_tile_fifo_size;
3179 config[no_regs++] = adev->gfx.config.sc_earlyz_tile_fifo_size;
3180 config[no_regs++] = adev->gfx.config.num_tile_pipes;
3181 config[no_regs++] = adev->gfx.config.backend_enable_mask;
3182 config[no_regs++] = adev->gfx.config.mem_max_burst_length_bytes;
3183 config[no_regs++] = adev->gfx.config.mem_row_size_in_kb;
3184 config[no_regs++] = adev->gfx.config.shader_engine_tile_size;
3185 config[no_regs++] = adev->gfx.config.num_gpus;
3186 config[no_regs++] = adev->gfx.config.multi_gpu_tile_size;
3187 config[no_regs++] = adev->gfx.config.mc_arb_ramcfg;
3188 config[no_regs++] = adev->gfx.config.gb_addr_config;
3189 config[no_regs++] = adev->gfx.config.num_rbs;
3190
Tom St Denis89a8f302016-08-12 15:14:31 -04003191 /* rev==1 */
3192 config[no_regs++] = adev->rev_id;
3193 config[no_regs++] = adev->pg_flags;
3194 config[no_regs++] = adev->cg_flags;
3195
Tom St Denise9f11dc2016-08-17 12:00:51 -04003196 /* rev==2 */
3197 config[no_regs++] = adev->family;
3198 config[no_regs++] = adev->external_rev_id;
3199
Tom St Denis9a999352017-01-18 13:01:25 -05003200 /* rev==3 */
3201 config[no_regs++] = adev->pdev->device;
3202 config[no_regs++] = adev->pdev->revision;
3203 config[no_regs++] = adev->pdev->subsystem_device;
3204 config[no_regs++] = adev->pdev->subsystem_vendor;
3205
Tom St Denis1e051412016-06-27 09:57:18 -04003206 while (size && (*pos < no_regs * 4)) {
3207 uint32_t value;
3208
3209 value = config[*pos >> 2];
3210 r = put_user(value, (uint32_t *)buf);
3211 if (r) {
3212 kfree(config);
3213 return r;
3214 }
3215
3216 result += 4;
3217 buf += 4;
3218 *pos += 4;
3219 size -= 4;
3220 }
3221
3222 kfree(config);
3223 return result;
3224}
3225
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003226static ssize_t amdgpu_debugfs_sensor_read(struct file *f, char __user *buf,
3227 size_t size, loff_t *pos)
3228{
Al Viro45063092016-12-04 18:24:56 -05003229 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003230 int idx, x, outsize, r, valuesize;
3231 uint32_t values[16];
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003232
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003233 if (size & 3 || *pos & 0x3)
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003234 return -EINVAL;
3235
Samuel Pitoiset3cbc6142017-02-15 19:32:29 +01003236 if (amdgpu_dpm == 0)
3237 return -EINVAL;
3238
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003239 /* convert offset to sensor number */
3240 idx = *pos >> 2;
3241
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003242 valuesize = sizeof(values);
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003243 if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->read_sensor)
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003244 r = adev->powerplay.pp_funcs->read_sensor(adev->powerplay.pp_handle, idx, &values[0], &valuesize);
Samuel Pitoiset3cbc6142017-02-15 19:32:29 +01003245 else if (adev->pm.funcs && adev->pm.funcs->read_sensor)
3246 r = adev->pm.funcs->read_sensor(adev, idx, &values[0],
3247 &valuesize);
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003248 else
3249 return -EINVAL;
3250
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003251 if (size > valuesize)
3252 return -EINVAL;
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003253
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003254 outsize = 0;
3255 x = 0;
3256 if (!r) {
3257 while (size) {
3258 r = put_user(values[x++], (int32_t *)buf);
3259 buf += 4;
3260 size -= 4;
3261 outsize += 4;
3262 }
3263 }
3264
3265 return !r ? outsize : r;
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003266}
Tom St Denis1e051412016-06-27 09:57:18 -04003267
Tom St Denis273d7aa2016-10-11 14:48:55 -04003268static ssize_t amdgpu_debugfs_wave_read(struct file *f, char __user *buf,
3269 size_t size, loff_t *pos)
3270{
3271 struct amdgpu_device *adev = f->f_inode->i_private;
3272 int r, x;
3273 ssize_t result=0;
Tom St Denis472259f2016-10-14 09:49:09 -04003274 uint32_t offset, se, sh, cu, wave, simd, data[32];
Tom St Denis273d7aa2016-10-11 14:48:55 -04003275
3276 if (size & 3 || *pos & 3)
3277 return -EINVAL;
3278
3279 /* decode offset */
3280 offset = (*pos & 0x7F);
3281 se = ((*pos >> 7) & 0xFF);
3282 sh = ((*pos >> 15) & 0xFF);
3283 cu = ((*pos >> 23) & 0xFF);
3284 wave = ((*pos >> 31) & 0xFF);
3285 simd = ((*pos >> 37) & 0xFF);
Tom St Denis273d7aa2016-10-11 14:48:55 -04003286
3287 /* switch to the specific se/sh/cu */
3288 mutex_lock(&adev->grbm_idx_mutex);
3289 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
3290
3291 x = 0;
Tom St Denis472259f2016-10-14 09:49:09 -04003292 if (adev->gfx.funcs->read_wave_data)
3293 adev->gfx.funcs->read_wave_data(adev, simd, wave, data, &x);
Tom St Denis273d7aa2016-10-11 14:48:55 -04003294
3295 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
3296 mutex_unlock(&adev->grbm_idx_mutex);
3297
Tom St Denis5ecfb3b2016-10-13 12:15:03 -04003298 if (!x)
3299 return -EINVAL;
3300
Tom St Denis472259f2016-10-14 09:49:09 -04003301 while (size && (offset < x * 4)) {
Tom St Denis273d7aa2016-10-11 14:48:55 -04003302 uint32_t value;
3303
Tom St Denis472259f2016-10-14 09:49:09 -04003304 value = data[offset >> 2];
Tom St Denis273d7aa2016-10-11 14:48:55 -04003305 r = put_user(value, (uint32_t *)buf);
3306 if (r)
3307 return r;
3308
3309 result += 4;
3310 buf += 4;
Tom St Denis472259f2016-10-14 09:49:09 -04003311 offset += 4;
Tom St Denis273d7aa2016-10-11 14:48:55 -04003312 size -= 4;
3313 }
3314
3315 return result;
3316}
3317
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003318static ssize_t amdgpu_debugfs_gpr_read(struct file *f, char __user *buf,
3319 size_t size, loff_t *pos)
3320{
3321 struct amdgpu_device *adev = f->f_inode->i_private;
3322 int r;
3323 ssize_t result = 0;
3324 uint32_t offset, se, sh, cu, wave, simd, thread, bank, *data;
3325
3326 if (size & 3 || *pos & 3)
3327 return -EINVAL;
3328
3329 /* decode offset */
3330 offset = (*pos & 0xFFF); /* in dwords */
3331 se = ((*pos >> 12) & 0xFF);
3332 sh = ((*pos >> 20) & 0xFF);
3333 cu = ((*pos >> 28) & 0xFF);
3334 wave = ((*pos >> 36) & 0xFF);
3335 simd = ((*pos >> 44) & 0xFF);
3336 thread = ((*pos >> 52) & 0xFF);
3337 bank = ((*pos >> 60) & 1);
3338
3339 data = kmalloc_array(1024, sizeof(*data), GFP_KERNEL);
3340 if (!data)
3341 return -ENOMEM;
3342
3343 /* switch to the specific se/sh/cu */
3344 mutex_lock(&adev->grbm_idx_mutex);
3345 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
3346
3347 if (bank == 0) {
3348 if (adev->gfx.funcs->read_wave_vgprs)
3349 adev->gfx.funcs->read_wave_vgprs(adev, simd, wave, thread, offset, size>>2, data);
3350 } else {
3351 if (adev->gfx.funcs->read_wave_sgprs)
3352 adev->gfx.funcs->read_wave_sgprs(adev, simd, wave, offset, size>>2, data);
3353 }
3354
3355 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
3356 mutex_unlock(&adev->grbm_idx_mutex);
3357
3358 while (size) {
3359 uint32_t value;
3360
3361 value = data[offset++];
3362 r = put_user(value, (uint32_t *)buf);
3363 if (r) {
3364 result = r;
3365 goto err;
3366 }
3367
3368 result += 4;
3369 buf += 4;
3370 size -= 4;
3371 }
3372
3373err:
3374 kfree(data);
3375 return result;
3376}
3377
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003378static const struct file_operations amdgpu_debugfs_regs_fops = {
3379 .owner = THIS_MODULE,
3380 .read = amdgpu_debugfs_regs_read,
3381 .write = amdgpu_debugfs_regs_write,
3382 .llseek = default_llseek
3383};
Tom St Denisadcec282016-04-15 13:08:44 -04003384static const struct file_operations amdgpu_debugfs_regs_didt_fops = {
3385 .owner = THIS_MODULE,
3386 .read = amdgpu_debugfs_regs_didt_read,
3387 .write = amdgpu_debugfs_regs_didt_write,
3388 .llseek = default_llseek
3389};
3390static const struct file_operations amdgpu_debugfs_regs_pcie_fops = {
3391 .owner = THIS_MODULE,
3392 .read = amdgpu_debugfs_regs_pcie_read,
3393 .write = amdgpu_debugfs_regs_pcie_write,
3394 .llseek = default_llseek
3395};
3396static const struct file_operations amdgpu_debugfs_regs_smc_fops = {
3397 .owner = THIS_MODULE,
3398 .read = amdgpu_debugfs_regs_smc_read,
3399 .write = amdgpu_debugfs_regs_smc_write,
3400 .llseek = default_llseek
3401};
3402
Tom St Denis1e051412016-06-27 09:57:18 -04003403static const struct file_operations amdgpu_debugfs_gca_config_fops = {
3404 .owner = THIS_MODULE,
3405 .read = amdgpu_debugfs_gca_config_read,
3406 .llseek = default_llseek
3407};
3408
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003409static const struct file_operations amdgpu_debugfs_sensors_fops = {
3410 .owner = THIS_MODULE,
3411 .read = amdgpu_debugfs_sensor_read,
3412 .llseek = default_llseek
3413};
3414
Tom St Denis273d7aa2016-10-11 14:48:55 -04003415static const struct file_operations amdgpu_debugfs_wave_fops = {
3416 .owner = THIS_MODULE,
3417 .read = amdgpu_debugfs_wave_read,
3418 .llseek = default_llseek
3419};
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003420static const struct file_operations amdgpu_debugfs_gpr_fops = {
3421 .owner = THIS_MODULE,
3422 .read = amdgpu_debugfs_gpr_read,
3423 .llseek = default_llseek
3424};
Tom St Denis273d7aa2016-10-11 14:48:55 -04003425
Tom St Denisadcec282016-04-15 13:08:44 -04003426static const struct file_operations *debugfs_regs[] = {
3427 &amdgpu_debugfs_regs_fops,
3428 &amdgpu_debugfs_regs_didt_fops,
3429 &amdgpu_debugfs_regs_pcie_fops,
3430 &amdgpu_debugfs_regs_smc_fops,
Tom St Denis1e051412016-06-27 09:57:18 -04003431 &amdgpu_debugfs_gca_config_fops,
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003432 &amdgpu_debugfs_sensors_fops,
Tom St Denis273d7aa2016-10-11 14:48:55 -04003433 &amdgpu_debugfs_wave_fops,
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003434 &amdgpu_debugfs_gpr_fops,
Tom St Denisadcec282016-04-15 13:08:44 -04003435};
3436
3437static const char *debugfs_regs_names[] = {
3438 "amdgpu_regs",
3439 "amdgpu_regs_didt",
3440 "amdgpu_regs_pcie",
3441 "amdgpu_regs_smc",
Tom St Denis1e051412016-06-27 09:57:18 -04003442 "amdgpu_gca_config",
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003443 "amdgpu_sensors",
Tom St Denis273d7aa2016-10-11 14:48:55 -04003444 "amdgpu_wave",
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003445 "amdgpu_gpr",
Tom St Denisadcec282016-04-15 13:08:44 -04003446};
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003447
3448static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
3449{
3450 struct drm_minor *minor = adev->ddev->primary;
3451 struct dentry *ent, *root = minor->debugfs_root;
Tom St Denisadcec282016-04-15 13:08:44 -04003452 unsigned i, j;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003453
Tom St Denisadcec282016-04-15 13:08:44 -04003454 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3455 ent = debugfs_create_file(debugfs_regs_names[i],
3456 S_IFREG | S_IRUGO, root,
3457 adev, debugfs_regs[i]);
3458 if (IS_ERR(ent)) {
3459 for (j = 0; j < i; j++) {
3460 debugfs_remove(adev->debugfs_regs[i]);
3461 adev->debugfs_regs[i] = NULL;
3462 }
3463 return PTR_ERR(ent);
3464 }
3465
3466 if (!i)
3467 i_size_write(ent->d_inode, adev->rmmio_size);
3468 adev->debugfs_regs[i] = ent;
3469 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003470
3471 return 0;
3472}
3473
3474static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev)
3475{
Tom St Denisadcec282016-04-15 13:08:44 -04003476 unsigned i;
3477
3478 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3479 if (adev->debugfs_regs[i]) {
3480 debugfs_remove(adev->debugfs_regs[i]);
3481 adev->debugfs_regs[i] = NULL;
3482 }
3483 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003484}
3485
3486int amdgpu_debugfs_init(struct drm_minor *minor)
3487{
3488 return 0;
3489}
Alexander Kuleshov7cebc722015-06-27 13:16:05 +06003490#else
3491static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
3492{
3493 return 0;
3494}
3495static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev) { }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003496#endif