blob: 5a170071702a3d205879e48ed80e1f21c4ed12ae [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>
Alex Deucherd38ceaf2015-04-20 16:55:21 -040056
Alex Deuchere2a75f82017-04-27 16:58:01 -040057MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin");
Alex Deucher2d2e5e72017-05-09 12:27:35 -040058MODULE_FIRMWARE("amdgpu/raven_gpu_info.bin");
Alex Deuchere2a75f82017-04-27 16:58:01 -040059
Alex Deucherd38ceaf2015-04-20 16:55:21 -040060static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev);
61static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev);
62
63static const char *amdgpu_asic_name[] = {
Ken Wangda69c1612016-01-21 19:08:55 +080064 "TAHITI",
65 "PITCAIRN",
66 "VERDE",
67 "OLAND",
68 "HAINAN",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040069 "BONAIRE",
70 "KAVERI",
71 "KABINI",
72 "HAWAII",
73 "MULLINS",
74 "TOPAZ",
75 "TONGA",
David Zhang48299f92015-07-08 01:05:16 +080076 "FIJI",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040077 "CARRIZO",
Samuel Li139f4912015-10-08 14:50:27 -040078 "STONEY",
Flora Cui2cc0c0b2016-03-14 18:33:29 -040079 "POLARIS10",
80 "POLARIS11",
Junwei Zhangc4642a42016-12-14 15:32:28 -050081 "POLARIS12",
Ken Wangd4196f02016-03-09 09:28:32 +080082 "VEGA10",
Chunming Zhou2ca8a5d2016-12-07 17:31:19 +080083 "RAVEN",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040084 "LAST",
85};
86
87bool amdgpu_device_is_px(struct drm_device *dev)
88{
89 struct amdgpu_device *adev = dev->dev_private;
90
Jammy Zhou2f7d10b2015-07-22 11:29:01 +080091 if (adev->flags & AMD_IS_PX)
Alex Deucherd38ceaf2015-04-20 16:55:21 -040092 return true;
93 return false;
94}
95
96/*
97 * MMIO register access helper functions.
98 */
99uint32_t amdgpu_mm_rreg(struct amdgpu_device *adev, uint32_t reg,
Monk Liu15d72fd2017-01-25 15:07:40 +0800100 uint32_t acc_flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400101{
Tom St Denisf4b373f2016-05-31 08:02:27 -0400102 uint32_t ret;
103
Monk Liu15d72fd2017-01-25 15:07:40 +0800104 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev)) {
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800105 BUG_ON(in_interrupt());
106 return amdgpu_virt_kiq_rreg(adev, reg);
107 }
108
Monk Liu15d72fd2017-01-25 15:07:40 +0800109 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
Tom St Denisf4b373f2016-05-31 08:02:27 -0400110 ret = readl(((void __iomem *)adev->rmmio) + (reg * 4));
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400111 else {
112 unsigned long flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400113
114 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
115 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
116 ret = readl(((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
117 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400118 }
Tom St Denisf4b373f2016-05-31 08:02:27 -0400119 trace_amdgpu_mm_rreg(adev->pdev->device, reg, ret);
120 return ret;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400121}
122
123void amdgpu_mm_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v,
Monk Liu15d72fd2017-01-25 15:07:40 +0800124 uint32_t acc_flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400125{
Tom St Denisf4b373f2016-05-31 08:02:27 -0400126 trace_amdgpu_mm_wreg(adev->pdev->device, reg, v);
Monk Liu4e99a442016-03-31 13:26:59 +0800127
Monk Liu15d72fd2017-01-25 15:07:40 +0800128 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev)) {
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800129 BUG_ON(in_interrupt());
130 return amdgpu_virt_kiq_wreg(adev, reg, v);
131 }
132
Monk Liu15d72fd2017-01-25 15:07:40 +0800133 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400134 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
135 else {
136 unsigned long flags;
137
138 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
139 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
140 writel(v, ((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
141 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
142 }
143}
144
145u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 reg)
146{
147 if ((reg * 4) < adev->rio_mem_size)
148 return ioread32(adev->rio_mem + (reg * 4));
149 else {
150 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
151 return ioread32(adev->rio_mem + (mmMM_DATA * 4));
152 }
153}
154
155void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
156{
157
158 if ((reg * 4) < adev->rio_mem_size)
159 iowrite32(v, adev->rio_mem + (reg * 4));
160 else {
161 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
162 iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
163 }
164}
165
166/**
167 * amdgpu_mm_rdoorbell - read a doorbell dword
168 *
169 * @adev: amdgpu_device pointer
170 * @index: doorbell index
171 *
172 * Returns the value in the doorbell aperture at the
173 * requested doorbell index (CIK).
174 */
175u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
176{
177 if (index < adev->doorbell.num_doorbells) {
178 return readl(adev->doorbell.ptr + index);
179 } else {
180 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
181 return 0;
182 }
183}
184
185/**
186 * amdgpu_mm_wdoorbell - write a doorbell dword
187 *
188 * @adev: amdgpu_device pointer
189 * @index: doorbell index
190 * @v: value to write
191 *
192 * Writes @v to the doorbell aperture at the
193 * requested doorbell index (CIK).
194 */
195void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
196{
197 if (index < adev->doorbell.num_doorbells) {
198 writel(v, adev->doorbell.ptr + index);
199 } else {
200 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
201 }
202}
203
204/**
Ken Wang832be402016-03-18 15:23:08 +0800205 * amdgpu_mm_rdoorbell64 - read a doorbell Qword
206 *
207 * @adev: amdgpu_device pointer
208 * @index: doorbell index
209 *
210 * Returns the value in the doorbell aperture at the
211 * requested doorbell index (VEGA10+).
212 */
213u64 amdgpu_mm_rdoorbell64(struct amdgpu_device *adev, u32 index)
214{
215 if (index < adev->doorbell.num_doorbells) {
216 return atomic64_read((atomic64_t *)(adev->doorbell.ptr + index));
217 } else {
218 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
219 return 0;
220 }
221}
222
223/**
224 * amdgpu_mm_wdoorbell64 - write a doorbell Qword
225 *
226 * @adev: amdgpu_device pointer
227 * @index: doorbell index
228 * @v: value to write
229 *
230 * Writes @v to the doorbell aperture at the
231 * requested doorbell index (VEGA10+).
232 */
233void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v)
234{
235 if (index < adev->doorbell.num_doorbells) {
236 atomic64_set((atomic64_t *)(adev->doorbell.ptr + index), v);
237 } else {
238 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
239 }
240}
241
242/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400243 * amdgpu_invalid_rreg - dummy reg read function
244 *
245 * @adev: amdgpu device pointer
246 * @reg: offset of register
247 *
248 * Dummy register read function. Used for register blocks
249 * that certain asics don't have (all asics).
250 * Returns the value in the register.
251 */
252static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
253{
254 DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
255 BUG();
256 return 0;
257}
258
259/**
260 * amdgpu_invalid_wreg - dummy reg write function
261 *
262 * @adev: amdgpu device pointer
263 * @reg: offset of register
264 * @v: value to write to the register
265 *
266 * Dummy register read function. Used for register blocks
267 * that certain asics don't have (all asics).
268 */
269static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
270{
271 DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
272 reg, v);
273 BUG();
274}
275
276/**
277 * amdgpu_block_invalid_rreg - dummy reg read function
278 *
279 * @adev: amdgpu device pointer
280 * @block: offset of instance
281 * @reg: offset of register
282 *
283 * Dummy register read function. Used for register blocks
284 * that certain asics don't have (all asics).
285 * Returns the value in the register.
286 */
287static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
288 uint32_t block, uint32_t reg)
289{
290 DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
291 reg, block);
292 BUG();
293 return 0;
294}
295
296/**
297 * amdgpu_block_invalid_wreg - dummy reg write function
298 *
299 * @adev: amdgpu device pointer
300 * @block: offset of instance
301 * @reg: offset of register
302 * @v: value to write to the register
303 *
304 * Dummy register read function. Used for register blocks
305 * that certain asics don't have (all asics).
306 */
307static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
308 uint32_t block,
309 uint32_t reg, uint32_t v)
310{
311 DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
312 reg, block, v);
313 BUG();
314}
315
316static int amdgpu_vram_scratch_init(struct amdgpu_device *adev)
317{
318 int r;
319
320 if (adev->vram_scratch.robj == NULL) {
321 r = amdgpu_bo_create(adev, AMDGPU_GPU_PAGE_SIZE,
Alex Deucher857d9132015-08-27 00:14:16 -0400322 PAGE_SIZE, true, AMDGPU_GEM_DOMAIN_VRAM,
Christian König03f48dd2016-08-15 17:00:22 +0200323 AMDGPU_GEM_CREATE_CPU_ACCESS_REQUIRED |
324 AMDGPU_GEM_CREATE_VRAM_CONTIGUOUS,
Christian König72d76682015-09-03 17:34:59 +0200325 NULL, NULL, &adev->vram_scratch.robj);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400326 if (r) {
327 return r;
328 }
329 }
330
331 r = amdgpu_bo_reserve(adev->vram_scratch.robj, false);
332 if (unlikely(r != 0))
333 return r;
334 r = amdgpu_bo_pin(adev->vram_scratch.robj,
335 AMDGPU_GEM_DOMAIN_VRAM, &adev->vram_scratch.gpu_addr);
336 if (r) {
337 amdgpu_bo_unreserve(adev->vram_scratch.robj);
338 return r;
339 }
340 r = amdgpu_bo_kmap(adev->vram_scratch.robj,
341 (void **)&adev->vram_scratch.ptr);
342 if (r)
343 amdgpu_bo_unpin(adev->vram_scratch.robj);
344 amdgpu_bo_unreserve(adev->vram_scratch.robj);
345
346 return r;
347}
348
349static void amdgpu_vram_scratch_fini(struct amdgpu_device *adev)
350{
351 int r;
352
353 if (adev->vram_scratch.robj == NULL) {
354 return;
355 }
Alex Xie8ab25b42017-04-24 13:30:43 -0400356 r = amdgpu_bo_reserve(adev->vram_scratch.robj, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400357 if (likely(r == 0)) {
358 amdgpu_bo_kunmap(adev->vram_scratch.robj);
359 amdgpu_bo_unpin(adev->vram_scratch.robj);
360 amdgpu_bo_unreserve(adev->vram_scratch.robj);
361 }
362 amdgpu_bo_unref(&adev->vram_scratch.robj);
363}
364
365/**
366 * amdgpu_program_register_sequence - program an array of registers.
367 *
368 * @adev: amdgpu_device pointer
369 * @registers: pointer to the register array
370 * @array_size: size of the register array
371 *
372 * Programs an array or registers with and and or masks.
373 * This is a helper for setting golden registers.
374 */
375void amdgpu_program_register_sequence(struct amdgpu_device *adev,
376 const u32 *registers,
377 const u32 array_size)
378{
379 u32 tmp, reg, and_mask, or_mask;
380 int i;
381
382 if (array_size % 3)
383 return;
384
385 for (i = 0; i < array_size; i +=3) {
386 reg = registers[i + 0];
387 and_mask = registers[i + 1];
388 or_mask = registers[i + 2];
389
390 if (and_mask == 0xffffffff) {
391 tmp = or_mask;
392 } else {
393 tmp = RREG32(reg);
394 tmp &= ~and_mask;
395 tmp |= or_mask;
396 }
397 WREG32(reg, tmp);
398 }
399}
400
401void amdgpu_pci_config_reset(struct amdgpu_device *adev)
402{
403 pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
404}
405
406/*
407 * GPU doorbell aperture helpers function.
408 */
409/**
410 * amdgpu_doorbell_init - Init doorbell driver information.
411 *
412 * @adev: amdgpu_device pointer
413 *
414 * Init doorbell driver information (CIK)
415 * Returns 0 on success, error on failure.
416 */
417static int amdgpu_doorbell_init(struct amdgpu_device *adev)
418{
419 /* doorbell bar mapping */
420 adev->doorbell.base = pci_resource_start(adev->pdev, 2);
421 adev->doorbell.size = pci_resource_len(adev->pdev, 2);
422
Christian Königedf600d2016-05-03 15:54:54 +0200423 adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400424 AMDGPU_DOORBELL_MAX_ASSIGNMENT+1);
425 if (adev->doorbell.num_doorbells == 0)
426 return -EINVAL;
427
Christian König8972e5d2017-03-06 13:34:57 +0100428 adev->doorbell.ptr = ioremap(adev->doorbell.base,
429 adev->doorbell.num_doorbells *
430 sizeof(u32));
431 if (adev->doorbell.ptr == NULL)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400432 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400433
434 return 0;
435}
436
437/**
438 * amdgpu_doorbell_fini - Tear down doorbell driver information.
439 *
440 * @adev: amdgpu_device pointer
441 *
442 * Tear down doorbell driver information (CIK)
443 */
444static void amdgpu_doorbell_fini(struct amdgpu_device *adev)
445{
446 iounmap(adev->doorbell.ptr);
447 adev->doorbell.ptr = NULL;
448}
449
450/**
451 * amdgpu_doorbell_get_kfd_info - Report doorbell configuration required to
452 * setup amdkfd
453 *
454 * @adev: amdgpu_device pointer
455 * @aperture_base: output returning doorbell aperture base physical address
456 * @aperture_size: output returning doorbell aperture size in bytes
457 * @start_offset: output returning # of doorbell bytes reserved for amdgpu.
458 *
459 * amdgpu and amdkfd share the doorbell aperture. amdgpu sets it up,
460 * takes doorbells required for its own rings and reports the setup to amdkfd.
461 * amdgpu reserved doorbells are at the start of the doorbell aperture.
462 */
463void amdgpu_doorbell_get_kfd_info(struct amdgpu_device *adev,
464 phys_addr_t *aperture_base,
465 size_t *aperture_size,
466 size_t *start_offset)
467{
468 /*
469 * The first num_doorbells are used by amdgpu.
470 * amdkfd takes whatever's left in the aperture.
471 */
472 if (adev->doorbell.size > adev->doorbell.num_doorbells * sizeof(u32)) {
473 *aperture_base = adev->doorbell.base;
474 *aperture_size = adev->doorbell.size;
475 *start_offset = adev->doorbell.num_doorbells * sizeof(u32);
476 } else {
477 *aperture_base = 0;
478 *aperture_size = 0;
479 *start_offset = 0;
480 }
481}
482
483/*
484 * amdgpu_wb_*()
Alex Xie455a7bc2017-05-08 21:36:03 -0400485 * Writeback is the method by which the GPU updates special pages in memory
Alex Xieea81a172017-05-08 13:41:11 -0400486 * with the status of certain GPU events (fences, ring pointers,etc.).
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400487 */
488
489/**
490 * amdgpu_wb_fini - Disable Writeback and free memory
491 *
492 * @adev: amdgpu_device pointer
493 *
494 * Disables Writeback and frees the Writeback memory (all asics).
495 * Used at driver shutdown.
496 */
497static void amdgpu_wb_fini(struct amdgpu_device *adev)
498{
499 if (adev->wb.wb_obj) {
Alex Deuchera76ed482016-10-21 15:30:36 -0400500 amdgpu_bo_free_kernel(&adev->wb.wb_obj,
501 &adev->wb.gpu_addr,
502 (void **)&adev->wb.wb);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400503 adev->wb.wb_obj = NULL;
504 }
505}
506
507/**
508 * amdgpu_wb_init- Init Writeback driver info and allocate memory
509 *
510 * @adev: amdgpu_device pointer
511 *
Alex Xie455a7bc2017-05-08 21:36:03 -0400512 * Initializes writeback and allocates writeback memory (all asics).
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400513 * Used at driver startup.
514 * Returns 0 on success or an -error on failure.
515 */
516static int amdgpu_wb_init(struct amdgpu_device *adev)
517{
518 int r;
519
520 if (adev->wb.wb_obj == NULL) {
Huang Rui60a970a62017-03-15 10:13:32 +0800521 r = amdgpu_bo_create_kernel(adev, AMDGPU_MAX_WB * sizeof(uint32_t),
Alex Deuchera76ed482016-10-21 15:30:36 -0400522 PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
523 &adev->wb.wb_obj, &adev->wb.gpu_addr,
524 (void **)&adev->wb.wb);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400525 if (r) {
526 dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
527 return r;
528 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400529
530 adev->wb.num_wb = AMDGPU_MAX_WB;
531 memset(&adev->wb.used, 0, sizeof(adev->wb.used));
532
533 /* clear wb memory */
Huang Rui60a970a62017-03-15 10:13:32 +0800534 memset((char *)adev->wb.wb, 0, AMDGPU_MAX_WB * sizeof(uint32_t));
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400535 }
536
537 return 0;
538}
539
540/**
541 * amdgpu_wb_get - Allocate a wb entry
542 *
543 * @adev: amdgpu_device pointer
544 * @wb: wb index
545 *
546 * Allocate a wb slot for use by the driver (all asics).
547 * Returns 0 on success or -EINVAL on failure.
548 */
549int amdgpu_wb_get(struct amdgpu_device *adev, u32 *wb)
550{
551 unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
552 if (offset < adev->wb.num_wb) {
553 __set_bit(offset, adev->wb.used);
554 *wb = offset;
555 return 0;
556 } else {
557 return -EINVAL;
558 }
559}
560
561/**
Ken Wang70142852016-03-18 15:08:49 +0800562 * amdgpu_wb_get_64bit - Allocate a wb entry
563 *
564 * @adev: amdgpu_device pointer
565 * @wb: wb index
566 *
567 * Allocate a wb slot for use by the driver (all asics).
568 * Returns 0 on success or -EINVAL on failure.
569 */
570int amdgpu_wb_get_64bit(struct amdgpu_device *adev, u32 *wb)
571{
572 unsigned long offset = bitmap_find_next_zero_area_off(adev->wb.used,
573 adev->wb.num_wb, 0, 2, 7, 0);
574 if ((offset + 1) < adev->wb.num_wb) {
575 __set_bit(offset, adev->wb.used);
576 __set_bit(offset + 1, adev->wb.used);
577 *wb = offset;
578 return 0;
579 } else {
580 return -EINVAL;
581 }
582}
583
584/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400585 * amdgpu_wb_free - Free a wb entry
586 *
587 * @adev: amdgpu_device pointer
588 * @wb: wb index
589 *
590 * Free a wb slot allocated for use by the driver (all asics)
591 */
592void amdgpu_wb_free(struct amdgpu_device *adev, u32 wb)
593{
594 if (wb < adev->wb.num_wb)
595 __clear_bit(wb, adev->wb.used);
596}
597
598/**
Ken Wang70142852016-03-18 15:08:49 +0800599 * amdgpu_wb_free_64bit - Free a wb entry
600 *
601 * @adev: amdgpu_device pointer
602 * @wb: wb index
603 *
604 * Free a wb slot allocated for use by the driver (all asics)
605 */
606void amdgpu_wb_free_64bit(struct amdgpu_device *adev, u32 wb)
607{
608 if ((wb + 1) < adev->wb.num_wb) {
609 __clear_bit(wb, adev->wb.used);
610 __clear_bit(wb + 1, adev->wb.used);
611 }
612}
613
614/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400615 * amdgpu_vram_location - try to find VRAM location
616 * @adev: amdgpu device structure holding all necessary informations
617 * @mc: memory controller structure holding memory informations
618 * @base: base address at which to put VRAM
619 *
Alex Xie455a7bc2017-05-08 21:36:03 -0400620 * Function will try to place VRAM at base address provided
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400621 * as parameter (which is so far either PCI aperture address or
622 * for IGP TOM base address).
623 *
624 * If there is not enough space to fit the unvisible VRAM in the 32bits
625 * address space then we limit the VRAM size to the aperture.
626 *
627 * Note: We don't explicitly enforce VRAM start to be aligned on VRAM size,
628 * this shouldn't be a problem as we are using the PCI aperture as a reference.
629 * Otherwise this would be needed for rv280, all r3xx, and all r4xx, but
630 * not IGP.
631 *
632 * Note: we use mc_vram_size as on some board we need to program the mc to
633 * cover the whole aperture even if VRAM size is inferior to aperture size
634 * Novell bug 204882 + along with lots of ubuntu ones
635 *
636 * Note: when limiting vram it's safe to overwritte real_vram_size because
637 * we are not in case where real_vram_size is inferior to mc_vram_size (ie
638 * note afected by bogus hw of Novell bug 204882 + along with lots of ubuntu
639 * ones)
640 *
641 * Note: IGP TOM addr should be the same as the aperture addr, we don't
Alex Xie455a7bc2017-05-08 21:36:03 -0400642 * explicitly check for that though.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400643 *
644 * FIXME: when reducing VRAM size align new size on power of 2.
645 */
646void amdgpu_vram_location(struct amdgpu_device *adev, struct amdgpu_mc *mc, u64 base)
647{
648 uint64_t limit = (uint64_t)amdgpu_vram_limit << 20;
649
650 mc->vram_start = base;
651 if (mc->mc_vram_size > (adev->mc.mc_mask - base + 1)) {
652 dev_warn(adev->dev, "limiting VRAM to PCI aperture size\n");
653 mc->real_vram_size = mc->aper_size;
654 mc->mc_vram_size = mc->aper_size;
655 }
656 mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
657 if (limit && limit < mc->real_vram_size)
658 mc->real_vram_size = limit;
659 dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
660 mc->mc_vram_size >> 20, mc->vram_start,
661 mc->vram_end, mc->real_vram_size >> 20);
662}
663
664/**
665 * amdgpu_gtt_location - try to find GTT location
666 * @adev: amdgpu device structure holding all necessary informations
667 * @mc: memory controller structure holding memory informations
668 *
669 * Function will place try to place GTT before or after VRAM.
670 *
671 * If GTT size is bigger than space left then we ajust GTT size.
672 * Thus function will never fails.
673 *
674 * FIXME: when reducing GTT size align new size on power of 2.
675 */
676void amdgpu_gtt_location(struct amdgpu_device *adev, struct amdgpu_mc *mc)
677{
678 u64 size_af, size_bf;
679
680 size_af = ((adev->mc.mc_mask - mc->vram_end) + mc->gtt_base_align) & ~mc->gtt_base_align;
681 size_bf = mc->vram_start & ~mc->gtt_base_align;
682 if (size_bf > size_af) {
683 if (mc->gtt_size > size_bf) {
684 dev_warn(adev->dev, "limiting GTT\n");
685 mc->gtt_size = size_bf;
686 }
Alex Deucher9dc5a912016-11-17 15:40:22 -0500687 mc->gtt_start = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400688 } else {
689 if (mc->gtt_size > size_af) {
690 dev_warn(adev->dev, "limiting GTT\n");
691 mc->gtt_size = size_af;
692 }
693 mc->gtt_start = (mc->vram_end + 1 + mc->gtt_base_align) & ~mc->gtt_base_align;
694 }
695 mc->gtt_end = mc->gtt_start + mc->gtt_size - 1;
696 dev_info(adev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
697 mc->gtt_size >> 20, mc->gtt_start, mc->gtt_end);
698}
699
700/*
701 * GPU helpers function.
702 */
703/**
Jim Quc836fec2017-02-10 15:59:59 +0800704 * amdgpu_need_post - check if the hw need post or not
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400705 *
706 * @adev: amdgpu_device pointer
707 *
Jim Quc836fec2017-02-10 15:59:59 +0800708 * Check if the asic has been initialized (all asics) at driver startup
709 * or post is needed if hw reset is performed.
710 * Returns true if need or false if not.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400711 */
Jim Quc836fec2017-02-10 15:59:59 +0800712bool amdgpu_need_post(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400713{
714 uint32_t reg;
715
Jim Quc836fec2017-02-10 15:59:59 +0800716 if (adev->has_hw_reset) {
717 adev->has_hw_reset = false;
718 return true;
719 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400720 /* then check MEM_SIZE, in case the crtcs are off */
Alex Deucherbbf282d2017-03-03 17:26:10 -0500721 reg = amdgpu_asic_get_config_memsize(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400722
Alex Deucherf2713e82017-03-28 12:19:31 -0400723 if ((reg != 0) && (reg != 0xffffffff))
Jim Quc836fec2017-02-10 15:59:59 +0800724 return false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400725
Jim Quc836fec2017-02-10 15:59:59 +0800726 return true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400727
728}
729
Monk Liubec86372016-09-14 19:38:08 +0800730static bool amdgpu_vpost_needed(struct amdgpu_device *adev)
731{
732 if (amdgpu_sriov_vf(adev))
733 return false;
734
735 if (amdgpu_passthrough(adev)) {
Monk Liu1da2c322016-11-11 11:24:29 +0800736 /* for FIJI: In whole GPU pass-through virtualization case, after VM reboot
737 * some old smc fw still need driver do vPost otherwise gpu hang, while
738 * those smc fw version above 22.15 doesn't have this flaw, so we force
739 * vpost executed for smc version below 22.15
Monk Liubec86372016-09-14 19:38:08 +0800740 */
741 if (adev->asic_type == CHIP_FIJI) {
742 int err;
743 uint32_t fw_ver;
744 err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
745 /* force vPost if error occured */
746 if (err)
747 return true;
748
749 fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
Monk Liu1da2c322016-11-11 11:24:29 +0800750 if (fw_ver < 0x00160e00)
751 return true;
Monk Liubec86372016-09-14 19:38:08 +0800752 }
Monk Liubec86372016-09-14 19:38:08 +0800753 }
Jim Quc836fec2017-02-10 15:59:59 +0800754 return amdgpu_need_post(adev);
Monk Liubec86372016-09-14 19:38:08 +0800755}
756
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400757/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400758 * amdgpu_dummy_page_init - init dummy page used by the driver
759 *
760 * @adev: amdgpu_device pointer
761 *
762 * Allocate the dummy page used by the driver (all asics).
763 * This dummy page is used by the driver as a filler for gart entries
764 * when pages are taken out of the GART
765 * Returns 0 on sucess, -ENOMEM on failure.
766 */
767int amdgpu_dummy_page_init(struct amdgpu_device *adev)
768{
769 if (adev->dummy_page.page)
770 return 0;
771 adev->dummy_page.page = alloc_page(GFP_DMA32 | GFP_KERNEL | __GFP_ZERO);
772 if (adev->dummy_page.page == NULL)
773 return -ENOMEM;
774 adev->dummy_page.addr = pci_map_page(adev->pdev, adev->dummy_page.page,
775 0, PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
776 if (pci_dma_mapping_error(adev->pdev, adev->dummy_page.addr)) {
777 dev_err(&adev->pdev->dev, "Failed to DMA MAP the dummy page\n");
778 __free_page(adev->dummy_page.page);
779 adev->dummy_page.page = NULL;
780 return -ENOMEM;
781 }
782 return 0;
783}
784
785/**
786 * amdgpu_dummy_page_fini - free dummy page used by the driver
787 *
788 * @adev: amdgpu_device pointer
789 *
790 * Frees the dummy page used by the driver (all asics).
791 */
792void amdgpu_dummy_page_fini(struct amdgpu_device *adev)
793{
794 if (adev->dummy_page.page == NULL)
795 return;
796 pci_unmap_page(adev->pdev, adev->dummy_page.addr,
797 PAGE_SIZE, PCI_DMA_BIDIRECTIONAL);
798 __free_page(adev->dummy_page.page);
799 adev->dummy_page.page = NULL;
800}
801
802
803/* ATOM accessor methods */
804/*
805 * ATOM is an interpreted byte code stored in tables in the vbios. The
806 * driver registers callbacks to access registers and the interpreter
807 * in the driver parses the tables and executes then to program specific
808 * actions (set display modes, asic init, etc.). See amdgpu_atombios.c,
809 * atombios.h, and atom.c
810 */
811
812/**
813 * cail_pll_read - read PLL register
814 *
815 * @info: atom card_info pointer
816 * @reg: PLL register offset
817 *
818 * Provides a PLL register accessor for the atom interpreter (r4xx+).
819 * Returns the value of the PLL register.
820 */
821static uint32_t cail_pll_read(struct card_info *info, uint32_t reg)
822{
823 return 0;
824}
825
826/**
827 * cail_pll_write - write PLL register
828 *
829 * @info: atom card_info pointer
830 * @reg: PLL register offset
831 * @val: value to write to the pll register
832 *
833 * Provides a PLL register accessor for the atom interpreter (r4xx+).
834 */
835static void cail_pll_write(struct card_info *info, uint32_t reg, uint32_t val)
836{
837
838}
839
840/**
841 * cail_mc_read - read MC (Memory Controller) register
842 *
843 * @info: atom card_info pointer
844 * @reg: MC register offset
845 *
846 * Provides an MC register accessor for the atom interpreter (r4xx+).
847 * Returns the value of the MC register.
848 */
849static uint32_t cail_mc_read(struct card_info *info, uint32_t reg)
850{
851 return 0;
852}
853
854/**
855 * cail_mc_write - write MC (Memory Controller) register
856 *
857 * @info: atom card_info pointer
858 * @reg: MC register offset
859 * @val: value to write to the pll register
860 *
861 * Provides a MC register accessor for the atom interpreter (r4xx+).
862 */
863static void cail_mc_write(struct card_info *info, uint32_t reg, uint32_t val)
864{
865
866}
867
868/**
869 * cail_reg_write - write MMIO register
870 *
871 * @info: atom card_info pointer
872 * @reg: MMIO register offset
873 * @val: value to write to the pll register
874 *
875 * Provides a MMIO register accessor for the atom interpreter (r4xx+).
876 */
877static void cail_reg_write(struct card_info *info, uint32_t reg, uint32_t val)
878{
879 struct amdgpu_device *adev = info->dev->dev_private;
880
881 WREG32(reg, val);
882}
883
884/**
885 * cail_reg_read - read MMIO register
886 *
887 * @info: atom card_info pointer
888 * @reg: MMIO register offset
889 *
890 * Provides an MMIO register accessor for the atom interpreter (r4xx+).
891 * Returns the value of the MMIO register.
892 */
893static uint32_t cail_reg_read(struct card_info *info, uint32_t reg)
894{
895 struct amdgpu_device *adev = info->dev->dev_private;
896 uint32_t r;
897
898 r = RREG32(reg);
899 return r;
900}
901
902/**
903 * cail_ioreg_write - write IO register
904 *
905 * @info: atom card_info pointer
906 * @reg: IO register offset
907 * @val: value to write to the pll register
908 *
909 * Provides a IO register accessor for the atom interpreter (r4xx+).
910 */
911static void cail_ioreg_write(struct card_info *info, uint32_t reg, uint32_t val)
912{
913 struct amdgpu_device *adev = info->dev->dev_private;
914
915 WREG32_IO(reg, val);
916}
917
918/**
919 * cail_ioreg_read - read IO register
920 *
921 * @info: atom card_info pointer
922 * @reg: IO register offset
923 *
924 * Provides an IO register accessor for the atom interpreter (r4xx+).
925 * Returns the value of the IO register.
926 */
927static uint32_t cail_ioreg_read(struct card_info *info, uint32_t reg)
928{
929 struct amdgpu_device *adev = info->dev->dev_private;
930 uint32_t r;
931
932 r = RREG32_IO(reg);
933 return r;
934}
935
936/**
937 * amdgpu_atombios_fini - free the driver info and callbacks for atombios
938 *
939 * @adev: amdgpu_device pointer
940 *
941 * Frees the driver info and register access callbacks for the ATOM
942 * interpreter (r4xx+).
943 * Called at driver shutdown.
944 */
945static void amdgpu_atombios_fini(struct amdgpu_device *adev)
946{
Monk Liu89e0ec9f2016-05-27 19:34:11 +0800947 if (adev->mode_info.atom_context) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400948 kfree(adev->mode_info.atom_context->scratch);
Monk Liu89e0ec9f2016-05-27 19:34:11 +0800949 kfree(adev->mode_info.atom_context->iio);
950 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400951 kfree(adev->mode_info.atom_context);
952 adev->mode_info.atom_context = NULL;
953 kfree(adev->mode_info.atom_card_info);
954 adev->mode_info.atom_card_info = NULL;
955}
956
957/**
958 * amdgpu_atombios_init - init the driver info and callbacks for atombios
959 *
960 * @adev: amdgpu_device pointer
961 *
962 * Initializes the driver info and register access callbacks for the
963 * ATOM interpreter (r4xx+).
964 * Returns 0 on sucess, -ENOMEM on failure.
965 * Called at driver startup.
966 */
967static int amdgpu_atombios_init(struct amdgpu_device *adev)
968{
969 struct card_info *atom_card_info =
970 kzalloc(sizeof(struct card_info), GFP_KERNEL);
971
972 if (!atom_card_info)
973 return -ENOMEM;
974
975 adev->mode_info.atom_card_info = atom_card_info;
976 atom_card_info->dev = adev->ddev;
977 atom_card_info->reg_read = cail_reg_read;
978 atom_card_info->reg_write = cail_reg_write;
979 /* needed for iio ops */
980 if (adev->rio_mem) {
981 atom_card_info->ioreg_read = cail_ioreg_read;
982 atom_card_info->ioreg_write = cail_ioreg_write;
983 } else {
Amber Linb64a18c2017-01-04 08:06:58 -0500984 DRM_INFO("PCI I/O BAR is not found. Using MMIO to access ATOM BIOS\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400985 atom_card_info->ioreg_read = cail_reg_read;
986 atom_card_info->ioreg_write = cail_reg_write;
987 }
988 atom_card_info->mc_read = cail_mc_read;
989 atom_card_info->mc_write = cail_mc_write;
990 atom_card_info->pll_read = cail_pll_read;
991 atom_card_info->pll_write = cail_pll_write;
992
993 adev->mode_info.atom_context = amdgpu_atom_parse(atom_card_info, adev->bios);
994 if (!adev->mode_info.atom_context) {
995 amdgpu_atombios_fini(adev);
996 return -ENOMEM;
997 }
998
999 mutex_init(&adev->mode_info.atom_context->mutex);
Alex Deuchera5bde2f2016-09-23 16:23:41 -04001000 if (adev->is_atom_fw) {
1001 amdgpu_atomfirmware_scratch_regs_init(adev);
1002 amdgpu_atomfirmware_allocate_fb_scratch(adev);
1003 } else {
1004 amdgpu_atombios_scratch_regs_init(adev);
1005 amdgpu_atombios_allocate_fb_scratch(adev);
1006 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001007 return 0;
1008}
1009
1010/* if we get transitioned to only one device, take VGA back */
1011/**
1012 * amdgpu_vga_set_decode - enable/disable vga decode
1013 *
1014 * @cookie: amdgpu_device pointer
1015 * @state: enable/disable vga decode
1016 *
1017 * Enable/disable vga decode (all asics).
1018 * Returns VGA resource flags.
1019 */
1020static unsigned int amdgpu_vga_set_decode(void *cookie, bool state)
1021{
1022 struct amdgpu_device *adev = cookie;
1023 amdgpu_asic_set_vga_state(adev, state);
1024 if (state)
1025 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
1026 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1027 else
1028 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
1029}
1030
1031/**
1032 * amdgpu_check_pot_argument - check that argument is a power of two
1033 *
1034 * @arg: value to check
1035 *
1036 * Validates that a certain argument is a power of two (all asics).
1037 * Returns true if argument is valid.
1038 */
1039static bool amdgpu_check_pot_argument(int arg)
1040{
1041 return (arg & (arg - 1)) == 0;
1042}
1043
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001044static void amdgpu_check_block_size(struct amdgpu_device *adev)
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001045{
1046 /* defines number of bits in page table versus page directory,
1047 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
1048 * page table and the remaining bits are in the page directory */
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001049 if (amdgpu_vm_block_size == -1)
1050 return;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001051
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001052 if (amdgpu_vm_block_size < 9) {
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001053 dev_warn(adev->dev, "VM page table size (%d) too small\n",
1054 amdgpu_vm_block_size);
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001055 goto def_value;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001056 }
1057
1058 if (amdgpu_vm_block_size > 24 ||
1059 (amdgpu_vm_size * 1024) < (1ull << amdgpu_vm_block_size)) {
1060 dev_warn(adev->dev, "VM page table size (%d) too large\n",
1061 amdgpu_vm_block_size);
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001062 goto def_value;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001063 }
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001064
1065 return;
1066
1067def_value:
1068 amdgpu_vm_block_size = -1;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +08001069}
1070
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001071static void amdgpu_check_vm_size(struct amdgpu_device *adev)
1072{
1073 if (!amdgpu_check_pot_argument(amdgpu_vm_size)) {
1074 dev_warn(adev->dev, "VM size (%d) must be a power of 2\n",
1075 amdgpu_vm_size);
1076 goto def_value;
1077 }
1078
1079 if (amdgpu_vm_size < 1) {
1080 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
1081 amdgpu_vm_size);
1082 goto def_value;
1083 }
1084
1085 /*
1086 * Max GPUVM size for Cayman, SI, CI VI are 40 bits.
1087 */
1088 if (amdgpu_vm_size > 1024) {
1089 dev_warn(adev->dev, "VM size (%d) too large, max is 1TB\n",
1090 amdgpu_vm_size);
1091 goto def_value;
1092 }
1093
1094 return;
1095
1096def_value:
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001097 amdgpu_vm_size = -1;
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001098}
1099
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001100/**
1101 * amdgpu_check_arguments - validate module params
1102 *
1103 * @adev: amdgpu_device pointer
1104 *
1105 * Validates certain module parameters and updates
1106 * the associated values used by the driver (all asics).
1107 */
1108static void amdgpu_check_arguments(struct amdgpu_device *adev)
1109{
Chunming Zhou5b011232015-12-10 17:34:33 +08001110 if (amdgpu_sched_jobs < 4) {
1111 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
1112 amdgpu_sched_jobs);
1113 amdgpu_sched_jobs = 4;
1114 } else if (!amdgpu_check_pot_argument(amdgpu_sched_jobs)){
1115 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
1116 amdgpu_sched_jobs);
1117 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
1118 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001119
1120 if (amdgpu_gart_size != -1) {
Christian Königc4e1a132016-03-17 16:25:15 +01001121 /* gtt size must be greater or equal to 32M */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001122 if (amdgpu_gart_size < 32) {
1123 dev_warn(adev->dev, "gart size (%d) too small\n",
1124 amdgpu_gart_size);
1125 amdgpu_gart_size = -1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001126 }
1127 }
1128
Zhang, Jerry83ca1452017-03-29 16:08:31 +08001129 amdgpu_check_vm_size(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001130
Junwei Zhangbab4fee2017-04-05 13:54:56 +08001131 amdgpu_check_block_size(adev);
Christian König6a7f76e2016-08-24 15:51:49 +02001132
jimqu526bae32016-11-07 09:53:10 +08001133 if (amdgpu_vram_page_split != -1 && (amdgpu_vram_page_split < 16 ||
1134 !amdgpu_check_pot_argument(amdgpu_vram_page_split))) {
Christian König6a7f76e2016-08-24 15:51:49 +02001135 dev_warn(adev->dev, "invalid VRAM page split (%d)\n",
1136 amdgpu_vram_page_split);
1137 amdgpu_vram_page_split = 1024;
1138 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001139}
1140
1141/**
1142 * amdgpu_switcheroo_set_state - set switcheroo state
1143 *
1144 * @pdev: pci dev pointer
Lukas Wunner16944672015-09-05 11:17:35 +02001145 * @state: vga_switcheroo state
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001146 *
1147 * Callback for the switcheroo driver. Suspends or resumes the
1148 * the asics before or after it is powered up using ACPI methods.
1149 */
1150static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
1151{
1152 struct drm_device *dev = pci_get_drvdata(pdev);
1153
1154 if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
1155 return;
1156
1157 if (state == VGA_SWITCHEROO_ON) {
1158 unsigned d3_delay = dev->pdev->d3_delay;
1159
Joe Perches7ca85292017-02-28 04:55:52 -08001160 pr_info("amdgpu: switched on\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001161 /* don't suspend or resume card normally */
1162 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
1163
Alex Deucher810ddc32016-08-23 13:25:49 -04001164 amdgpu_device_resume(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001165
1166 dev->pdev->d3_delay = d3_delay;
1167
1168 dev->switch_power_state = DRM_SWITCH_POWER_ON;
1169 drm_kms_helper_poll_enable(dev);
1170 } else {
Joe Perches7ca85292017-02-28 04:55:52 -08001171 pr_info("amdgpu: switched off\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001172 drm_kms_helper_poll_disable(dev);
1173 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Alex Deucher810ddc32016-08-23 13:25:49 -04001174 amdgpu_device_suspend(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001175 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1176 }
1177}
1178
1179/**
1180 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1181 *
1182 * @pdev: pci dev pointer
1183 *
1184 * Callback for the switcheroo driver. Check of the switcheroo
1185 * state can be changed.
1186 * Returns true if the state can be changed, false if not.
1187 */
1188static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1189{
1190 struct drm_device *dev = pci_get_drvdata(pdev);
1191
1192 /*
1193 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1194 * locking inversion with the driver load path. And the access here is
1195 * completely racy anyway. So don't bother with locking for now.
1196 */
1197 return dev->open_count == 0;
1198}
1199
1200static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1201 .set_gpu_state = amdgpu_switcheroo_set_state,
1202 .reprobe = NULL,
1203 .can_switch = amdgpu_switcheroo_can_switch,
1204};
1205
1206int amdgpu_set_clockgating_state(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001207 enum amd_ip_block_type block_type,
1208 enum amd_clockgating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001209{
1210 int i, r = 0;
1211
1212 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001213 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001214 continue;
Rex Zhuc7228652017-02-22 15:33:46 +08001215 if (adev->ip_blocks[i].version->type != block_type)
1216 continue;
1217 if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
1218 continue;
1219 r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
1220 (void *)adev, state);
1221 if (r)
1222 DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
1223 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001224 }
1225 return r;
1226}
1227
1228int amdgpu_set_powergating_state(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001229 enum amd_ip_block_type block_type,
1230 enum amd_powergating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001231{
1232 int i, r = 0;
1233
1234 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001235 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001236 continue;
Rex Zhuc7228652017-02-22 15:33:46 +08001237 if (adev->ip_blocks[i].version->type != block_type)
1238 continue;
1239 if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
1240 continue;
1241 r = adev->ip_blocks[i].version->funcs->set_powergating_state(
1242 (void *)adev, state);
1243 if (r)
1244 DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
1245 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001246 }
1247 return r;
1248}
1249
Huang Rui6cb2d4e2017-01-05 18:44:41 +08001250void amdgpu_get_clockgating_state(struct amdgpu_device *adev, u32 *flags)
1251{
1252 int i;
1253
1254 for (i = 0; i < adev->num_ip_blocks; i++) {
1255 if (!adev->ip_blocks[i].status.valid)
1256 continue;
1257 if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
1258 adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
1259 }
1260}
1261
Alex Deucher5dbbb602016-06-23 11:41:04 -04001262int amdgpu_wait_for_idle(struct amdgpu_device *adev,
1263 enum amd_ip_block_type block_type)
1264{
1265 int i, r;
1266
1267 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001268 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001269 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001270 if (adev->ip_blocks[i].version->type == block_type) {
1271 r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -04001272 if (r)
1273 return r;
1274 break;
1275 }
1276 }
1277 return 0;
1278
1279}
1280
1281bool amdgpu_is_idle(struct amdgpu_device *adev,
1282 enum amd_ip_block_type block_type)
1283{
1284 int i;
1285
1286 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001287 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001288 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001289 if (adev->ip_blocks[i].version->type == block_type)
1290 return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -04001291 }
1292 return true;
1293
1294}
1295
Alex Deuchera1255102016-10-13 17:41:13 -04001296struct amdgpu_ip_block * amdgpu_get_ip_block(struct amdgpu_device *adev,
1297 enum amd_ip_block_type type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001298{
1299 int i;
1300
1301 for (i = 0; i < adev->num_ip_blocks; i++)
Alex Deuchera1255102016-10-13 17:41:13 -04001302 if (adev->ip_blocks[i].version->type == type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001303 return &adev->ip_blocks[i];
1304
1305 return NULL;
1306}
1307
1308/**
1309 * amdgpu_ip_block_version_cmp
1310 *
1311 * @adev: amdgpu_device pointer
yanyang15fc3aee2015-05-22 14:39:35 -04001312 * @type: enum amd_ip_block_type
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001313 * @major: major version
1314 * @minor: minor version
1315 *
1316 * return 0 if equal or greater
1317 * return 1 if smaller or the ip_block doesn't exist
1318 */
1319int amdgpu_ip_block_version_cmp(struct amdgpu_device *adev,
yanyang15fc3aee2015-05-22 14:39:35 -04001320 enum amd_ip_block_type type,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001321 u32 major, u32 minor)
1322{
Alex Deuchera1255102016-10-13 17:41:13 -04001323 struct amdgpu_ip_block *ip_block = amdgpu_get_ip_block(adev, type);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001324
Alex Deuchera1255102016-10-13 17:41:13 -04001325 if (ip_block && ((ip_block->version->major > major) ||
1326 ((ip_block->version->major == major) &&
1327 (ip_block->version->minor >= minor))))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001328 return 0;
1329
1330 return 1;
1331}
1332
Alex Deuchera1255102016-10-13 17:41:13 -04001333/**
1334 * amdgpu_ip_block_add
1335 *
1336 * @adev: amdgpu_device pointer
1337 * @ip_block_version: pointer to the IP to add
1338 *
1339 * Adds the IP block driver information to the collection of IPs
1340 * on the asic.
1341 */
1342int amdgpu_ip_block_add(struct amdgpu_device *adev,
1343 const struct amdgpu_ip_block_version *ip_block_version)
1344{
1345 if (!ip_block_version)
1346 return -EINVAL;
1347
1348 adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
1349
1350 return 0;
1351}
1352
Alex Deucher483ef982016-09-30 12:43:04 -04001353static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
Emily Deng9accf2f2016-08-10 16:01:25 +08001354{
1355 adev->enable_virtual_display = false;
1356
1357 if (amdgpu_virtual_display) {
1358 struct drm_device *ddev = adev->ddev;
1359 const char *pci_address_name = pci_name(ddev->pdev);
Emily Deng0f663562016-09-30 13:02:18 -04001360 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
Emily Deng9accf2f2016-08-10 16:01:25 +08001361
1362 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1363 pciaddstr_tmp = pciaddstr;
Emily Deng0f663562016-09-30 13:02:18 -04001364 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1365 pciaddname = strsep(&pciaddname_tmp, ",");
Yintian Tao967de2a2017-01-22 15:16:51 +08001366 if (!strcmp("all", pciaddname)
1367 || !strcmp(pci_address_name, pciaddname)) {
Emily Deng0f663562016-09-30 13:02:18 -04001368 long num_crtc;
1369 int res = -1;
1370
Emily Deng9accf2f2016-08-10 16:01:25 +08001371 adev->enable_virtual_display = true;
Emily Deng0f663562016-09-30 13:02:18 -04001372
1373 if (pciaddname_tmp)
1374 res = kstrtol(pciaddname_tmp, 10,
1375 &num_crtc);
1376
1377 if (!res) {
1378 if (num_crtc < 1)
1379 num_crtc = 1;
1380 if (num_crtc > 6)
1381 num_crtc = 6;
1382 adev->mode_info.num_crtc = num_crtc;
1383 } else {
1384 adev->mode_info.num_crtc = 1;
1385 }
Emily Deng9accf2f2016-08-10 16:01:25 +08001386 break;
1387 }
1388 }
1389
Emily Deng0f663562016-09-30 13:02:18 -04001390 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
1391 amdgpu_virtual_display, pci_address_name,
1392 adev->enable_virtual_display, adev->mode_info.num_crtc);
Emily Deng9accf2f2016-08-10 16:01:25 +08001393
1394 kfree(pciaddstr);
1395 }
1396}
1397
Alex Deuchere2a75f82017-04-27 16:58:01 -04001398static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev)
1399{
1400 const struct firmware *fw;
1401 const char *chip_name;
1402 char fw_name[30];
1403 int err;
1404 const struct gpu_info_firmware_header_v1_0 *hdr;
1405
1406 switch (adev->asic_type) {
1407 case CHIP_TOPAZ:
1408 case CHIP_TONGA:
1409 case CHIP_FIJI:
1410 case CHIP_POLARIS11:
1411 case CHIP_POLARIS10:
1412 case CHIP_POLARIS12:
1413 case CHIP_CARRIZO:
1414 case CHIP_STONEY:
1415#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:
1421#endif
1422#ifdef CONFIG_DRM_AMDGPU_CIK
1423 case CHIP_BONAIRE:
1424 case CHIP_HAWAII:
1425 case CHIP_KAVERI:
1426 case CHIP_KABINI:
1427 case CHIP_MULLINS:
1428#endif
1429 default:
1430 return 0;
1431 case CHIP_VEGA10:
1432 chip_name = "vega10";
1433 break;
Alex Deucher2d2e5e72017-05-09 12:27:35 -04001434 case CHIP_RAVEN:
1435 chip_name = "raven";
1436 break;
Alex Deuchere2a75f82017-04-27 16:58:01 -04001437 }
1438
1439 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name);
1440 err = request_firmware(&fw, fw_name, adev->dev);
1441 if (err) {
1442 dev_err(adev->dev,
1443 "Failed to load gpu_info firmware \"%s\"\n",
1444 fw_name);
1445 goto out;
1446 }
1447 err = amdgpu_ucode_validate(fw);
1448 if (err) {
1449 dev_err(adev->dev,
1450 "Failed to validate gpu_info firmware \"%s\"\n",
1451 fw_name);
1452 goto out;
1453 }
1454
1455 hdr = (const struct gpu_info_firmware_header_v1_0 *)fw->data;
1456 amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
1457
1458 switch (hdr->version_major) {
1459 case 1:
1460 {
1461 const struct gpu_info_firmware_v1_0 *gpu_info_fw =
1462 (const struct gpu_info_firmware_v1_0 *)(fw->data +
1463 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1464
1465 adev->gfx.config.max_shader_engines = gpu_info_fw->gc_num_se;
1466 adev->gfx.config.max_cu_per_sh = gpu_info_fw->gc_num_cu_per_sh;
1467 adev->gfx.config.max_sh_per_se = gpu_info_fw->gc_num_sh_per_se;
1468 adev->gfx.config.max_backends_per_se = gpu_info_fw->gc_num_rb_per_se;
1469 adev->gfx.config.max_texture_channel_caches =
1470 gpu_info_fw->gc_num_tccs;
1471 adev->gfx.config.max_gprs = gpu_info_fw->gc_num_gprs;
1472 adev->gfx.config.max_gs_threads = gpu_info_fw->gc_num_max_gs_thds;
1473 adev->gfx.config.gs_vgt_table_depth = gpu_info_fw->gc_gs_table_depth;
1474 adev->gfx.config.gs_prim_buffer_depth = gpu_info_fw->gc_gsprim_buff_depth;
1475 adev->gfx.config.double_offchip_lds_buf =
1476 gpu_info_fw->gc_double_offchip_lds_buffer;
1477 adev->gfx.cu_info.wave_front_size = gpu_info_fw->gc_wave_size;
1478 break;
1479 }
1480 default:
1481 dev_err(adev->dev,
1482 "Unsupported gpu_info table %d\n", hdr->header.ucode_version);
1483 err = -EINVAL;
1484 goto out;
1485 }
1486out:
1487 release_firmware(fw);
1488 fw = NULL;
1489
1490 return err;
1491}
1492
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001493static int amdgpu_early_init(struct amdgpu_device *adev)
1494{
Alex Deucheraaa36a92015-04-20 17:31:14 -04001495 int i, r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001496
Alex Deucher483ef982016-09-30 12:43:04 -04001497 amdgpu_device_enable_virtual_display(adev);
Emily Denga6be7572016-08-08 11:37:50 +08001498
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001499 switch (adev->asic_type) {
Alex Deucheraaa36a92015-04-20 17:31:14 -04001500 case CHIP_TOPAZ:
1501 case CHIP_TONGA:
David Zhang48299f92015-07-08 01:05:16 +08001502 case CHIP_FIJI:
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001503 case CHIP_POLARIS11:
1504 case CHIP_POLARIS10:
Junwei Zhangc4642a42016-12-14 15:32:28 -05001505 case CHIP_POLARIS12:
Alex Deucheraaa36a92015-04-20 17:31:14 -04001506 case CHIP_CARRIZO:
Samuel Li39bb0c92015-10-08 16:31:43 -04001507 case CHIP_STONEY:
1508 if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
Alex Deucheraaa36a92015-04-20 17:31:14 -04001509 adev->family = AMDGPU_FAMILY_CZ;
1510 else
1511 adev->family = AMDGPU_FAMILY_VI;
1512
1513 r = vi_set_ip_blocks(adev);
1514 if (r)
1515 return r;
1516 break;
Ken Wang33f34802016-01-21 17:29:41 +08001517#ifdef CONFIG_DRM_AMDGPU_SI
1518 case CHIP_VERDE:
1519 case CHIP_TAHITI:
1520 case CHIP_PITCAIRN:
1521 case CHIP_OLAND:
1522 case CHIP_HAINAN:
Ken Wang295d0da2016-05-24 21:02:53 +08001523 adev->family = AMDGPU_FAMILY_SI;
Ken Wang33f34802016-01-21 17:29:41 +08001524 r = si_set_ip_blocks(adev);
1525 if (r)
1526 return r;
1527 break;
1528#endif
Alex Deuchera2e73f52015-04-20 17:09:27 -04001529#ifdef CONFIG_DRM_AMDGPU_CIK
1530 case CHIP_BONAIRE:
1531 case CHIP_HAWAII:
1532 case CHIP_KAVERI:
1533 case CHIP_KABINI:
1534 case CHIP_MULLINS:
1535 if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
1536 adev->family = AMDGPU_FAMILY_CI;
1537 else
1538 adev->family = AMDGPU_FAMILY_KV;
1539
1540 r = cik_set_ip_blocks(adev);
1541 if (r)
1542 return r;
1543 break;
1544#endif
Chunming Zhou2ca8a5d2016-12-07 17:31:19 +08001545 case CHIP_VEGA10:
1546 case CHIP_RAVEN:
1547 if (adev->asic_type == CHIP_RAVEN)
1548 adev->family = AMDGPU_FAMILY_RV;
1549 else
1550 adev->family = AMDGPU_FAMILY_AI;
Ken Wang460826e2017-03-06 14:53:16 -05001551
1552 r = soc15_set_ip_blocks(adev);
1553 if (r)
1554 return r;
1555 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001556 default:
1557 /* FIXME: not supported yet */
1558 return -EINVAL;
1559 }
1560
Alex Deuchere2a75f82017-04-27 16:58:01 -04001561 r = amdgpu_device_parse_gpu_info_fw(adev);
1562 if (r)
1563 return r;
1564
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001565 if (amdgpu_sriov_vf(adev)) {
1566 r = amdgpu_virt_request_full_gpu(adev, true);
1567 if (r)
1568 return r;
1569 }
1570
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001571 for (i = 0; i < adev->num_ip_blocks; i++) {
1572 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
1573 DRM_ERROR("disabled ip block: %d\n", i);
Alex Deuchera1255102016-10-13 17:41:13 -04001574 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001575 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001576 if (adev->ip_blocks[i].version->funcs->early_init) {
1577 r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001578 if (r == -ENOENT) {
Alex Deuchera1255102016-10-13 17:41:13 -04001579 adev->ip_blocks[i].status.valid = false;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001580 } else if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001581 DRM_ERROR("early_init of IP block <%s> failed %d\n",
1582 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001583 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001584 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001585 adev->ip_blocks[i].status.valid = true;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001586 }
Alex Deucher974e6b62015-07-10 13:59:44 -04001587 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001588 adev->ip_blocks[i].status.valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001589 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001590 }
1591 }
1592
Nicolai Hähnle395d1fb2016-06-02 12:32:07 +02001593 adev->cg_flags &= amdgpu_cg_mask;
1594 adev->pg_flags &= amdgpu_pg_mask;
1595
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001596 return 0;
1597}
1598
1599static int amdgpu_init(struct amdgpu_device *adev)
1600{
1601 int i, r;
1602
1603 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001604 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001605 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001606 r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001607 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001608 DRM_ERROR("sw_init of IP block <%s> failed %d\n",
1609 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001610 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001611 }
Alex Deuchera1255102016-10-13 17:41:13 -04001612 adev->ip_blocks[i].status.sw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001613 /* need to do gmc hw init early so we can allocate gpu mem */
Alex Deuchera1255102016-10-13 17:41:13 -04001614 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001615 r = amdgpu_vram_scratch_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001616 if (r) {
1617 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001618 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001619 }
Alex Deuchera1255102016-10-13 17:41:13 -04001620 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001621 if (r) {
1622 DRM_ERROR("hw_init %d failed %d\n", i, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001623 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001624 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001625 r = amdgpu_wb_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001626 if (r) {
1627 DRM_ERROR("amdgpu_wb_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001628 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001629 }
Alex Deuchera1255102016-10-13 17:41:13 -04001630 adev->ip_blocks[i].status.hw = true;
Monk Liu24936642017-01-09 15:54:32 +08001631
1632 /* right after GMC hw init, we create CSA */
1633 if (amdgpu_sriov_vf(adev)) {
1634 r = amdgpu_allocate_static_csa(adev);
1635 if (r) {
1636 DRM_ERROR("allocate CSA failed %d\n", r);
1637 return r;
1638 }
1639 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001640 }
1641 }
1642
1643 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001644 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001645 continue;
1646 /* gmc hw init is done early */
Alex Deuchera1255102016-10-13 17:41:13 -04001647 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001648 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001649 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001650 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001651 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
1652 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001653 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001654 }
Alex Deuchera1255102016-10-13 17:41:13 -04001655 adev->ip_blocks[i].status.hw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001656 }
1657
1658 return 0;
1659}
1660
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08001661static void amdgpu_fill_reset_magic(struct amdgpu_device *adev)
1662{
1663 memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM);
1664}
1665
1666static bool amdgpu_check_vram_lost(struct amdgpu_device *adev)
1667{
1668 return !!memcmp(adev->gart.ptr, adev->reset_magic,
1669 AMDGPU_RESET_MAGIC_NUM);
1670}
1671
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001672static int amdgpu_late_init(struct amdgpu_device *adev)
1673{
1674 int i = 0, r;
1675
1676 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001677 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001678 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001679 if (adev->ip_blocks[i].version->funcs->late_init) {
1680 r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001681 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001682 DRM_ERROR("late_init of IP block <%s> failed %d\n",
1683 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001684 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001685 }
Alex Deuchera1255102016-10-13 17:41:13 -04001686 adev->ip_blocks[i].status.late_initialized = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001687 }
Alex Deucher4a446d52016-10-07 14:48:18 -04001688 /* skip CG for VCE/UVD, it's handled specially */
Alex Deuchera1255102016-10-13 17:41:13 -04001689 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1690 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
Alex Deucher4a446d52016-10-07 14:48:18 -04001691 /* enable clockgating to save power */
Alex Deuchera1255102016-10-13 17:41:13 -04001692 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1693 AMD_CG_STATE_GATE);
Alex Deucher4a446d52016-10-07 14:48:18 -04001694 if (r) {
1695 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001696 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher4a446d52016-10-07 14:48:18 -04001697 return r;
1698 }
Arindam Nathb0b00ff2016-10-07 19:01:37 +05301699 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001700 }
1701
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08001702 amdgpu_fill_reset_magic(adev);
1703
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001704 return 0;
1705}
1706
1707static int amdgpu_fini(struct amdgpu_device *adev)
1708{
1709 int i, r;
1710
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001711 /* need to disable SMC first */
1712 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001713 if (!adev->ip_blocks[i].status.hw)
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001714 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001715 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) {
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001716 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
Alex Deuchera1255102016-10-13 17:41:13 -04001717 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1718 AMD_CG_STATE_UNGATE);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001719 if (r) {
1720 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001721 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001722 return r;
1723 }
Alex Deuchera1255102016-10-13 17:41:13 -04001724 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001725 /* XXX handle errors */
1726 if (r) {
1727 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001728 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001729 }
Alex Deuchera1255102016-10-13 17:41:13 -04001730 adev->ip_blocks[i].status.hw = false;
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001731 break;
1732 }
1733 }
1734
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001735 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001736 if (!adev->ip_blocks[i].status.hw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001737 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001738 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001739 amdgpu_wb_fini(adev);
1740 amdgpu_vram_scratch_fini(adev);
1741 }
Rex Zhu8201a672016-11-24 21:44:44 +08001742
1743 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
1744 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE) {
1745 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
1746 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1747 AMD_CG_STATE_UNGATE);
1748 if (r) {
1749 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1750 adev->ip_blocks[i].version->funcs->name, r);
1751 return r;
1752 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001753 }
Rex Zhu8201a672016-11-24 21:44:44 +08001754
Alex Deuchera1255102016-10-13 17:41:13 -04001755 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001756 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001757 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001758 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
1759 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001760 }
Rex Zhu8201a672016-11-24 21:44:44 +08001761
Alex Deuchera1255102016-10-13 17:41:13 -04001762 adev->ip_blocks[i].status.hw = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001763 }
1764
1765 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001766 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001767 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001768 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001769 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001770 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001771 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
1772 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001773 }
Alex Deuchera1255102016-10-13 17:41:13 -04001774 adev->ip_blocks[i].status.sw = false;
1775 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001776 }
1777
Monk Liua6dcfd92016-05-19 14:36:34 +08001778 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001779 if (!adev->ip_blocks[i].status.late_initialized)
Grazvydas Ignotas8a2eef12016-10-03 00:06:44 +03001780 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001781 if (adev->ip_blocks[i].version->funcs->late_fini)
1782 adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
1783 adev->ip_blocks[i].status.late_initialized = false;
Monk Liua6dcfd92016-05-19 14:36:34 +08001784 }
1785
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001786 if (amdgpu_sriov_vf(adev)) {
Monk Liu24936642017-01-09 15:54:32 +08001787 amdgpu_bo_free_kernel(&adev->virt.csa_obj, &adev->virt.csa_vmid0_addr, NULL);
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001788 amdgpu_virt_release_full_gpu(adev, false);
1789 }
Monk Liu24936642017-01-09 15:54:32 +08001790
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001791 return 0;
1792}
1793
Alex Deucherfaefba92016-12-06 10:38:29 -05001794int amdgpu_suspend(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001795{
1796 int i, r;
1797
Xiangliang Yue941ea92017-01-18 12:47:55 +08001798 if (amdgpu_sriov_vf(adev))
1799 amdgpu_virt_request_full_gpu(adev, false);
1800
Flora Cuic5a93a22016-02-26 10:45:25 +08001801 /* ungate SMC block first */
1802 r = amdgpu_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC,
1803 AMD_CG_STATE_UNGATE);
1804 if (r) {
1805 DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n",r);
1806 }
1807
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001808 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001809 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001810 continue;
1811 /* ungate blocks so that suspend can properly shut them down */
Flora Cuic5a93a22016-02-26 10:45:25 +08001812 if (i != AMD_IP_BLOCK_TYPE_SMC) {
Alex Deuchera1255102016-10-13 17:41:13 -04001813 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1814 AMD_CG_STATE_UNGATE);
Flora Cuic5a93a22016-02-26 10:45:25 +08001815 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001816 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1817 adev->ip_blocks[i].version->funcs->name, r);
Flora Cuic5a93a22016-02-26 10:45:25 +08001818 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001819 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001820 /* XXX handle errors */
Alex Deuchera1255102016-10-13 17:41:13 -04001821 r = adev->ip_blocks[i].version->funcs->suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001822 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001823 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001824 DRM_ERROR("suspend of IP block <%s> failed %d\n",
1825 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001826 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001827 }
1828
Xiangliang Yue941ea92017-01-18 12:47:55 +08001829 if (amdgpu_sriov_vf(adev))
1830 amdgpu_virt_release_full_gpu(adev, false);
1831
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001832 return 0;
1833}
1834
Monk Liue4f0fdc2017-02-09 11:55:49 +08001835static int amdgpu_sriov_reinit_early(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08001836{
1837 int i, r;
1838
Monk Liu2cb681b2017-04-26 12:00:49 +08001839 static enum amd_ip_block_type ip_order[] = {
1840 AMD_IP_BLOCK_TYPE_GMC,
1841 AMD_IP_BLOCK_TYPE_COMMON,
1842 AMD_IP_BLOCK_TYPE_GFXHUB,
1843 AMD_IP_BLOCK_TYPE_MMHUB,
1844 AMD_IP_BLOCK_TYPE_IH,
1845 };
Monk Liua90ad3c2017-01-23 14:22:08 +08001846
Monk Liu2cb681b2017-04-26 12:00:49 +08001847 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1848 int j;
1849 struct amdgpu_ip_block *block;
Monk Liua90ad3c2017-01-23 14:22:08 +08001850
Monk Liu2cb681b2017-04-26 12:00:49 +08001851 for (j = 0; j < adev->num_ip_blocks; j++) {
1852 block = &adev->ip_blocks[j];
1853
1854 if (block->version->type != ip_order[i] ||
1855 !block->status.valid)
1856 continue;
1857
1858 r = block->version->funcs->hw_init(adev);
1859 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
Monk Liua90ad3c2017-01-23 14:22:08 +08001860 }
1861 }
1862
1863 return 0;
1864}
1865
Monk Liue4f0fdc2017-02-09 11:55:49 +08001866static int amdgpu_sriov_reinit_late(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08001867{
1868 int i, r;
1869
Monk Liu2cb681b2017-04-26 12:00:49 +08001870 static enum amd_ip_block_type ip_order[] = {
1871 AMD_IP_BLOCK_TYPE_SMC,
1872 AMD_IP_BLOCK_TYPE_DCE,
1873 AMD_IP_BLOCK_TYPE_GFX,
1874 AMD_IP_BLOCK_TYPE_SDMA,
1875 AMD_IP_BLOCK_TYPE_VCE,
1876 };
Monk Liua90ad3c2017-01-23 14:22:08 +08001877
Monk Liu2cb681b2017-04-26 12:00:49 +08001878 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1879 int j;
1880 struct amdgpu_ip_block *block;
Monk Liua90ad3c2017-01-23 14:22:08 +08001881
Monk Liu2cb681b2017-04-26 12:00:49 +08001882 for (j = 0; j < adev->num_ip_blocks; j++) {
1883 block = &adev->ip_blocks[j];
1884
1885 if (block->version->type != ip_order[i] ||
1886 !block->status.valid)
1887 continue;
1888
1889 r = block->version->funcs->hw_init(adev);
1890 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
Monk Liua90ad3c2017-01-23 14:22:08 +08001891 }
1892 }
1893
1894 return 0;
1895}
1896
Chunming Zhoufcf06492017-05-05 10:33:33 +08001897static int amdgpu_resume_phase1(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001898{
1899 int i, r;
1900
1901 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001902 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001903 continue;
Chunming Zhoufcf06492017-05-05 10:33:33 +08001904 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
1905 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
1906 adev->ip_blocks[i].version->type ==
1907 AMD_IP_BLOCK_TYPE_IH) {
1908 r = adev->ip_blocks[i].version->funcs->resume(adev);
1909 if (r) {
1910 DRM_ERROR("resume of IP block <%s> failed %d\n",
1911 adev->ip_blocks[i].version->funcs->name, r);
1912 return r;
1913 }
1914 }
1915 }
1916
1917 return 0;
1918}
1919
1920static int amdgpu_resume_phase2(struct amdgpu_device *adev)
1921{
1922 int i, r;
1923
1924 for (i = 0; i < adev->num_ip_blocks; i++) {
1925 if (!adev->ip_blocks[i].status.valid)
1926 continue;
1927 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
1928 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
1929 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH )
1930 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001931 r = adev->ip_blocks[i].version->funcs->resume(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001932 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001933 DRM_ERROR("resume of IP block <%s> failed %d\n",
1934 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001935 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001936 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001937 }
1938
1939 return 0;
1940}
1941
Chunming Zhoufcf06492017-05-05 10:33:33 +08001942static int amdgpu_resume(struct amdgpu_device *adev)
1943{
1944 int r;
1945
1946 r = amdgpu_resume_phase1(adev);
1947 if (r)
1948 return r;
1949 r = amdgpu_resume_phase2(adev);
1950
1951 return r;
1952}
1953
Monk Liu4e99a442016-03-31 13:26:59 +08001954static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
Andres Rodriguez048765a2016-06-11 02:51:32 -04001955{
Alex Deuchera5bde2f2016-09-23 16:23:41 -04001956 if (adev->is_atom_fw) {
1957 if (amdgpu_atomfirmware_gpu_supports_virtualization(adev))
1958 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
1959 } else {
1960 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
1961 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
1962 }
Andres Rodriguez048765a2016-06-11 02:51:32 -04001963}
1964
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001965/**
1966 * amdgpu_device_init - initialize the driver
1967 *
1968 * @adev: amdgpu_device pointer
1969 * @pdev: drm dev pointer
1970 * @pdev: pci dev pointer
1971 * @flags: driver flags
1972 *
1973 * Initializes the driver info and hw (all asics).
1974 * Returns 0 for success or an error on failure.
1975 * Called at driver startup.
1976 */
1977int amdgpu_device_init(struct amdgpu_device *adev,
1978 struct drm_device *ddev,
1979 struct pci_dev *pdev,
1980 uint32_t flags)
1981{
1982 int r, i;
1983 bool runtime = false;
Marek Olšák95844d22016-08-17 23:49:27 +02001984 u32 max_MBps;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001985
1986 adev->shutdown = false;
1987 adev->dev = &pdev->dev;
1988 adev->ddev = ddev;
1989 adev->pdev = pdev;
1990 adev->flags = flags;
Jammy Zhou2f7d10b2015-07-22 11:29:01 +08001991 adev->asic_type = flags & AMD_ASIC_MASK;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001992 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
1993 adev->mc.gtt_size = 512 * 1024 * 1024;
1994 adev->accel_working = false;
1995 adev->num_rings = 0;
1996 adev->mman.buffer_funcs = NULL;
1997 adev->mman.buffer_funcs_ring = NULL;
1998 adev->vm_manager.vm_pte_funcs = NULL;
Christian König2d55e452016-02-08 17:37:38 +01001999 adev->vm_manager.vm_pte_num_rings = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002000 adev->gart.gart_funcs = NULL;
Chris Wilsonf54d1862016-10-25 13:00:45 +01002001 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002002
2003 adev->smc_rreg = &amdgpu_invalid_rreg;
2004 adev->smc_wreg = &amdgpu_invalid_wreg;
2005 adev->pcie_rreg = &amdgpu_invalid_rreg;
2006 adev->pcie_wreg = &amdgpu_invalid_wreg;
Huang Rui36b9a952016-08-31 13:23:25 +08002007 adev->pciep_rreg = &amdgpu_invalid_rreg;
2008 adev->pciep_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002009 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
2010 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
2011 adev->didt_rreg = &amdgpu_invalid_rreg;
2012 adev->didt_wreg = &amdgpu_invalid_wreg;
Rex Zhuccdbb202016-06-08 12:47:41 +08002013 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
2014 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002015 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
2016 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
2017
Rex Zhuccdbb202016-06-08 12:47:41 +08002018
Alex Deucher3e39ab92015-06-05 15:04:33 -04002019 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
2020 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
2021 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002022
2023 /* mutex initialization are all done here so we
2024 * can recall function without having locking issues */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002025 atomic_set(&adev->irq.ih.lock, 0);
Huang Rui0e5ca0d2017-03-03 18:37:23 -05002026 mutex_init(&adev->firmware.mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002027 mutex_init(&adev->pm.mutex);
2028 mutex_init(&adev->gfx.gpu_clock_mutex);
2029 mutex_init(&adev->srbm_mutex);
2030 mutex_init(&adev->grbm_idx_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002031 mutex_init(&adev->mn_lock);
2032 hash_init(adev->mn_hash);
2033
2034 amdgpu_check_arguments(adev);
2035
2036 /* Registers mapping */
2037 /* TODO: block userspace mapping of io register */
2038 spin_lock_init(&adev->mmio_idx_lock);
2039 spin_lock_init(&adev->smc_idx_lock);
2040 spin_lock_init(&adev->pcie_idx_lock);
2041 spin_lock_init(&adev->uvd_ctx_idx_lock);
2042 spin_lock_init(&adev->didt_idx_lock);
Rex Zhuccdbb202016-06-08 12:47:41 +08002043 spin_lock_init(&adev->gc_cac_idx_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002044 spin_lock_init(&adev->audio_endpt_idx_lock);
Marek Olšák95844d22016-08-17 23:49:27 +02002045 spin_lock_init(&adev->mm_stats.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002046
Chunming Zhou0c4e7fa2016-08-17 11:41:30 +08002047 INIT_LIST_HEAD(&adev->shadow_list);
2048 mutex_init(&adev->shadow_list_lock);
2049
Chunming Zhou5c1354b2016-08-30 16:13:10 +08002050 INIT_LIST_HEAD(&adev->gtt_list);
2051 spin_lock_init(&adev->gtt_list_lock);
2052
Ken Wangda69c1612016-01-21 19:08:55 +08002053 if (adev->asic_type >= CHIP_BONAIRE) {
2054 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
2055 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
2056 } else {
2057 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
2058 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
2059 }
Chunming Zhou5c1354b2016-08-30 16:13:10 +08002060
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002061 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
2062 if (adev->rmmio == NULL) {
2063 return -ENOMEM;
2064 }
2065 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
2066 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
2067
Ken Wangda69c1612016-01-21 19:08:55 +08002068 if (adev->asic_type >= CHIP_BONAIRE)
2069 /* doorbell bar mapping */
2070 amdgpu_doorbell_init(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002071
2072 /* io port mapping */
2073 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
2074 if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
2075 adev->rio_mem_size = pci_resource_len(adev->pdev, i);
2076 adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
2077 break;
2078 }
2079 }
2080 if (adev->rio_mem == NULL)
Amber Linb64a18c2017-01-04 08:06:58 -05002081 DRM_INFO("PCI I/O BAR is not found.\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002082
2083 /* early init functions */
2084 r = amdgpu_early_init(adev);
2085 if (r)
2086 return r;
2087
2088 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
2089 /* this will fail for cards that aren't VGA class devices, just
2090 * ignore it */
2091 vga_client_register(adev->pdev, adev, NULL, amdgpu_vga_set_decode);
2092
2093 if (amdgpu_runtime_pm == 1)
2094 runtime = true;
Alex Deuchere9bef452016-04-25 13:12:18 -04002095 if (amdgpu_device_is_px(ddev))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002096 runtime = true;
Lukas Wunner84c8b222017-03-10 21:23:45 +01002097 if (!pci_is_thunderbolt_attached(adev->pdev))
2098 vga_switcheroo_register_client(adev->pdev,
2099 &amdgpu_switcheroo_ops, runtime);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002100 if (runtime)
2101 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
2102
2103 /* Read BIOS */
Alex Deucher83ba1262016-06-03 18:21:41 -04002104 if (!amdgpu_get_bios(adev)) {
2105 r = -EINVAL;
2106 goto failed;
2107 }
Nils Wallméniusf7e9e9f2016-12-14 21:52:45 +01002108
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002109 r = amdgpu_atombios_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002110 if (r) {
2111 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04002112 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002113 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002114
Monk Liu4e99a442016-03-31 13:26:59 +08002115 /* detect if we are with an SRIOV vbios */
2116 amdgpu_device_detect_sriov_bios(adev);
Andres Rodriguez048765a2016-06-11 02:51:32 -04002117
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002118 /* Post card if necessary */
Monk Liubec86372016-09-14 19:38:08 +08002119 if (amdgpu_vpost_needed(adev)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002120 if (!adev->bios) {
Monk Liubec86372016-09-14 19:38:08 +08002121 dev_err(adev->dev, "no vBIOS found\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04002122 r = -EINVAL;
2123 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002124 }
Monk Liubec86372016-09-14 19:38:08 +08002125 DRM_INFO("GPU posting now...\n");
Monk Liu4e99a442016-03-31 13:26:59 +08002126 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2127 if (r) {
2128 dev_err(adev->dev, "gpu post error!\n");
2129 goto failed;
2130 }
2131 } else {
2132 DRM_INFO("GPU post is not needed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002133 }
2134
Alex Deuchera5bde2f2016-09-23 16:23:41 -04002135 if (!adev->is_atom_fw) {
2136 /* Initialize clocks */
2137 r = amdgpu_atombios_get_clock_info(adev);
2138 if (r) {
2139 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
2140 return r;
2141 }
2142 /* init i2c buses */
2143 amdgpu_atombios_i2c_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002144 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002145
2146 /* Fence driver */
2147 r = amdgpu_fence_driver_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002148 if (r) {
2149 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04002150 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002151 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002152
2153 /* init the mode config */
2154 drm_mode_config_init(adev->ddev);
2155
2156 r = amdgpu_init(adev);
2157 if (r) {
Alex Deucher2c1a2782015-12-07 17:02:53 -05002158 dev_err(adev->dev, "amdgpu_init failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002159 amdgpu_fini(adev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002160 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002161 }
2162
2163 adev->accel_working = true;
2164
Marek Olšák95844d22016-08-17 23:49:27 +02002165 /* Initialize the buffer migration limit. */
2166 if (amdgpu_moverate >= 0)
2167 max_MBps = amdgpu_moverate;
2168 else
2169 max_MBps = 8; /* Allow 8 MB/s. */
2170 /* Get a log2 for easy divisions. */
2171 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
2172
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002173 r = amdgpu_ib_pool_init(adev);
2174 if (r) {
2175 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
Alex Deucher83ba1262016-06-03 18:21:41 -04002176 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002177 }
2178
2179 r = amdgpu_ib_ring_tests(adev);
2180 if (r)
2181 DRM_ERROR("ib ring test failed (%d).\n", r);
2182
Monk Liu9bc92b92017-02-08 17:38:13 +08002183 amdgpu_fbdev_init(adev);
2184
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002185 r = amdgpu_gem_debugfs_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002186 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002187 DRM_ERROR("registering gem debugfs failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002188
2189 r = amdgpu_debugfs_regs_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002190 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002191 DRM_ERROR("registering register debugfs failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002192
Huang Rui50ab2532016-06-12 15:51:09 +08002193 r = amdgpu_debugfs_firmware_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002194 if (r)
Huang Rui50ab2532016-06-12 15:51:09 +08002195 DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
Huang Rui50ab2532016-06-12 15:51:09 +08002196
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002197 if ((amdgpu_testing & 1)) {
2198 if (adev->accel_working)
2199 amdgpu_test_moves(adev);
2200 else
2201 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
2202 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002203 if (amdgpu_benchmarking) {
2204 if (adev->accel_working)
2205 amdgpu_benchmark(adev, amdgpu_benchmarking);
2206 else
2207 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
2208 }
2209
2210 /* enable clockgating, etc. after ib tests, etc. since some blocks require
2211 * explicit gating rather than handling it automatically.
2212 */
2213 r = amdgpu_late_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002214 if (r) {
2215 dev_err(adev->dev, "amdgpu_late_init failed\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04002216 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002217 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002218
2219 return 0;
Alex Deucher83ba1262016-06-03 18:21:41 -04002220
2221failed:
2222 if (runtime)
2223 vga_switcheroo_fini_domain_pm_ops(adev->dev);
2224 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002225}
2226
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002227/**
2228 * amdgpu_device_fini - tear down the driver
2229 *
2230 * @adev: amdgpu_device pointer
2231 *
2232 * Tear down the driver info (all asics).
2233 * Called at driver shutdown.
2234 */
2235void amdgpu_device_fini(struct amdgpu_device *adev)
2236{
2237 int r;
2238
2239 DRM_INFO("amdgpu: finishing device.\n");
2240 adev->shutdown = true;
Pixel Dingdb2c2a92017-04-25 16:47:42 +08002241 if (adev->mode_info.mode_config_initialized)
2242 drm_crtc_force_disable_all(adev->ddev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002243 /* evict vram memory */
2244 amdgpu_bo_evict_vram(adev);
2245 amdgpu_ib_pool_fini(adev);
2246 amdgpu_fence_driver_fini(adev);
2247 amdgpu_fbdev_fini(adev);
2248 r = amdgpu_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002249 adev->accel_working = false;
2250 /* free i2c buses */
2251 amdgpu_i2c_fini(adev);
2252 amdgpu_atombios_fini(adev);
2253 kfree(adev->bios);
2254 adev->bios = NULL;
Lukas Wunner84c8b222017-03-10 21:23:45 +01002255 if (!pci_is_thunderbolt_attached(adev->pdev))
2256 vga_switcheroo_unregister_client(adev->pdev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002257 if (adev->flags & AMD_IS_PX)
2258 vga_switcheroo_fini_domain_pm_ops(adev->dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002259 vga_client_register(adev->pdev, NULL, NULL, NULL);
2260 if (adev->rio_mem)
2261 pci_iounmap(adev->pdev, adev->rio_mem);
2262 adev->rio_mem = NULL;
2263 iounmap(adev->rmmio);
2264 adev->rmmio = NULL;
Ken Wangda69c1612016-01-21 19:08:55 +08002265 if (adev->asic_type >= CHIP_BONAIRE)
2266 amdgpu_doorbell_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002267 amdgpu_debugfs_regs_cleanup(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002268}
2269
2270
2271/*
2272 * Suspend & resume.
2273 */
2274/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002275 * amdgpu_device_suspend - initiate device suspend
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002276 *
2277 * @pdev: drm dev pointer
2278 * @state: suspend state
2279 *
2280 * Puts the hw in the suspend state (all asics).
2281 * Returns 0 for success or an error on failure.
2282 * Called at driver suspend.
2283 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002284int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002285{
2286 struct amdgpu_device *adev;
2287 struct drm_crtc *crtc;
2288 struct drm_connector *connector;
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002289 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002290
2291 if (dev == NULL || dev->dev_private == NULL) {
2292 return -ENODEV;
2293 }
2294
2295 adev = dev->dev_private;
2296
2297 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2298 return 0;
2299
2300 drm_kms_helper_poll_disable(dev);
2301
2302 /* turn off display hw */
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002303 drm_modeset_lock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002304 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2305 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
2306 }
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002307 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002308
Alex Deucher756e6882015-10-08 00:03:36 -04002309 /* unpin the front buffers and cursors */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002310 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Alex Deucher756e6882015-10-08 00:03:36 -04002311 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002312 struct amdgpu_framebuffer *rfb = to_amdgpu_framebuffer(crtc->primary->fb);
2313 struct amdgpu_bo *robj;
2314
Alex Deucher756e6882015-10-08 00:03:36 -04002315 if (amdgpu_crtc->cursor_bo) {
2316 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
Alex Xie7a6901d2017-04-24 13:52:41 -04002317 r = amdgpu_bo_reserve(aobj, true);
Alex Deucher756e6882015-10-08 00:03:36 -04002318 if (r == 0) {
2319 amdgpu_bo_unpin(aobj);
2320 amdgpu_bo_unreserve(aobj);
2321 }
2322 }
2323
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002324 if (rfb == NULL || rfb->obj == NULL) {
2325 continue;
2326 }
2327 robj = gem_to_amdgpu_bo(rfb->obj);
2328 /* don't unpin kernel fb objects */
2329 if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
Alex Xie7a6901d2017-04-24 13:52:41 -04002330 r = amdgpu_bo_reserve(robj, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002331 if (r == 0) {
2332 amdgpu_bo_unpin(robj);
2333 amdgpu_bo_unreserve(robj);
2334 }
2335 }
2336 }
2337 /* evict vram memory */
2338 amdgpu_bo_evict_vram(adev);
2339
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002340 amdgpu_fence_driver_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002341
2342 r = amdgpu_suspend(adev);
2343
Alex Deuchera0a71e42016-10-10 12:41:36 -04002344 /* evict remaining vram memory
2345 * This second call to evict vram is to evict the gart page table
2346 * using the CPU.
2347 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002348 amdgpu_bo_evict_vram(adev);
2349
Alex Deucherbe34d3b2017-03-03 14:26:51 -05002350 if (adev->is_atom_fw)
2351 amdgpu_atomfirmware_scratch_regs_save(adev);
2352 else
2353 amdgpu_atombios_scratch_regs_save(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002354 pci_save_state(dev->pdev);
2355 if (suspend) {
2356 /* Shut down the device */
2357 pci_disable_device(dev->pdev);
2358 pci_set_power_state(dev->pdev, PCI_D3hot);
jimqu74b0b152016-09-07 17:09:12 +08002359 } else {
2360 r = amdgpu_asic_reset(adev);
2361 if (r)
2362 DRM_ERROR("amdgpu asic reset failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002363 }
2364
2365 if (fbcon) {
2366 console_lock();
2367 amdgpu_fbdev_set_suspend(adev, 1);
2368 console_unlock();
2369 }
2370 return 0;
2371}
2372
2373/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002374 * amdgpu_device_resume - initiate device resume
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002375 *
2376 * @pdev: drm dev pointer
2377 *
2378 * Bring the hw back to operating state (all asics).
2379 * Returns 0 for success or an error on failure.
2380 * Called at driver resume.
2381 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002382int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002383{
2384 struct drm_connector *connector;
2385 struct amdgpu_device *adev = dev->dev_private;
Alex Deucher756e6882015-10-08 00:03:36 -04002386 struct drm_crtc *crtc;
Huang Rui03161a62017-04-13 16:12:26 +08002387 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002388
2389 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2390 return 0;
2391
jimqu74b0b152016-09-07 17:09:12 +08002392 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002393 console_lock();
jimqu74b0b152016-09-07 17:09:12 +08002394
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002395 if (resume) {
2396 pci_set_power_state(dev->pdev, PCI_D0);
2397 pci_restore_state(dev->pdev);
jimqu74b0b152016-09-07 17:09:12 +08002398 r = pci_enable_device(dev->pdev);
Huang Rui03161a62017-04-13 16:12:26 +08002399 if (r)
2400 goto unlock;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002401 }
Alex Deucherbe34d3b2017-03-03 14:26:51 -05002402 if (adev->is_atom_fw)
2403 amdgpu_atomfirmware_scratch_regs_restore(adev);
2404 else
2405 amdgpu_atombios_scratch_regs_restore(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002406
2407 /* post card */
Jim Quc836fec2017-02-10 15:59:59 +08002408 if (amdgpu_need_post(adev)) {
jimqu74b0b152016-09-07 17:09:12 +08002409 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2410 if (r)
2411 DRM_ERROR("amdgpu asic init failed\n");
2412 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002413
2414 r = amdgpu_resume(adev);
Rex Zhue6707212017-03-30 13:21:01 +08002415 if (r) {
Flora Cuica198522016-02-04 15:10:08 +08002416 DRM_ERROR("amdgpu_resume failed (%d).\n", r);
Huang Rui03161a62017-04-13 16:12:26 +08002417 goto unlock;
Rex Zhue6707212017-03-30 13:21:01 +08002418 }
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002419 amdgpu_fence_driver_resume(adev);
2420
Flora Cuica198522016-02-04 15:10:08 +08002421 if (resume) {
2422 r = amdgpu_ib_ring_tests(adev);
2423 if (r)
2424 DRM_ERROR("ib ring test failed (%d).\n", r);
2425 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002426
2427 r = amdgpu_late_init(adev);
Huang Rui03161a62017-04-13 16:12:26 +08002428 if (r)
2429 goto unlock;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002430
Alex Deucher756e6882015-10-08 00:03:36 -04002431 /* pin cursors */
2432 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2433 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2434
2435 if (amdgpu_crtc->cursor_bo) {
2436 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
Alex Xie7a6901d2017-04-24 13:52:41 -04002437 r = amdgpu_bo_reserve(aobj, true);
Alex Deucher756e6882015-10-08 00:03:36 -04002438 if (r == 0) {
2439 r = amdgpu_bo_pin(aobj,
2440 AMDGPU_GEM_DOMAIN_VRAM,
2441 &amdgpu_crtc->cursor_addr);
2442 if (r != 0)
2443 DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
2444 amdgpu_bo_unreserve(aobj);
2445 }
2446 }
2447 }
2448
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002449 /* blat the mode back in */
2450 if (fbcon) {
2451 drm_helper_resume_force_mode(dev);
2452 /* turn on display hw */
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002453 drm_modeset_lock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002454 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2455 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
2456 }
Alex Deucher4c7fbc32015-09-23 14:32:06 -04002457 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002458 }
2459
2460 drm_kms_helper_poll_enable(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002461
2462 /*
2463 * Most of the connector probing functions try to acquire runtime pm
2464 * refs to ensure that the GPU is powered on when connector polling is
2465 * performed. Since we're calling this from a runtime PM callback,
2466 * trying to acquire rpm refs will cause us to deadlock.
2467 *
2468 * Since we're guaranteed to be holding the rpm lock, it's safe to
2469 * temporarily disable the rpm helpers so this doesn't deadlock us.
2470 */
2471#ifdef CONFIG_PM
2472 dev->dev->power.disable_depth++;
2473#endif
Alex Deucher54fb2a52015-11-24 14:30:56 -05002474 drm_helper_hpd_irq_event(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002475#ifdef CONFIG_PM
2476 dev->dev->power.disable_depth--;
2477#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002478
Huang Rui03161a62017-04-13 16:12:26 +08002479 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002480 amdgpu_fbdev_set_suspend(adev, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002481
Huang Rui03161a62017-04-13 16:12:26 +08002482unlock:
2483 if (fbcon)
2484 console_unlock();
2485
2486 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002487}
2488
Chunming Zhou63fbf422016-07-15 11:19:20 +08002489static bool amdgpu_check_soft_reset(struct amdgpu_device *adev)
2490{
2491 int i;
2492 bool asic_hang = false;
2493
2494 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002495 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou63fbf422016-07-15 11:19:20 +08002496 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002497 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
2498 adev->ip_blocks[i].status.hang =
2499 adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
2500 if (adev->ip_blocks[i].status.hang) {
2501 DRM_INFO("IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
Chunming Zhou63fbf422016-07-15 11:19:20 +08002502 asic_hang = true;
2503 }
2504 }
2505 return asic_hang;
2506}
2507
Baoyou Xie4d446652016-09-18 22:09:35 +08002508static int amdgpu_pre_soft_reset(struct amdgpu_device *adev)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002509{
2510 int i, r = 0;
2511
2512 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002513 if (!adev->ip_blocks[i].status.valid)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002514 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002515 if (adev->ip_blocks[i].status.hang &&
2516 adev->ip_blocks[i].version->funcs->pre_soft_reset) {
2517 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
Chunming Zhoud31a5012016-07-18 10:04:34 +08002518 if (r)
2519 return r;
2520 }
2521 }
2522
2523 return 0;
2524}
2525
Chunming Zhou35d782f2016-07-15 15:57:13 +08002526static bool amdgpu_need_full_reset(struct amdgpu_device *adev)
2527{
Alex Deucherda146d32016-10-13 16:07:03 -04002528 int i;
2529
2530 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002531 if (!adev->ip_blocks[i].status.valid)
Alex Deucherda146d32016-10-13 16:07:03 -04002532 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002533 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
2534 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
2535 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
2536 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE)) {
2537 if (adev->ip_blocks[i].status.hang) {
Alex Deucherda146d32016-10-13 16:07:03 -04002538 DRM_INFO("Some block need full reset!\n");
2539 return true;
2540 }
2541 }
Chunming Zhou35d782f2016-07-15 15:57:13 +08002542 }
2543 return false;
2544}
2545
2546static int amdgpu_soft_reset(struct amdgpu_device *adev)
2547{
2548 int i, r = 0;
2549
2550 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002551 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002552 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002553 if (adev->ip_blocks[i].status.hang &&
2554 adev->ip_blocks[i].version->funcs->soft_reset) {
2555 r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002556 if (r)
2557 return r;
2558 }
2559 }
2560
2561 return 0;
2562}
2563
2564static int amdgpu_post_soft_reset(struct amdgpu_device *adev)
2565{
2566 int i, r = 0;
2567
2568 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002569 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002570 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002571 if (adev->ip_blocks[i].status.hang &&
2572 adev->ip_blocks[i].version->funcs->post_soft_reset)
2573 r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002574 if (r)
2575 return r;
2576 }
2577
2578 return 0;
2579}
2580
Chunming Zhou3ad81f12016-08-05 17:30:17 +08002581bool amdgpu_need_backup(struct amdgpu_device *adev)
2582{
2583 if (adev->flags & AMD_IS_APU)
2584 return false;
2585
2586 return amdgpu_lockup_timeout > 0 ? true : false;
2587}
2588
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002589static int amdgpu_recover_vram_from_shadow(struct amdgpu_device *adev,
2590 struct amdgpu_ring *ring,
2591 struct amdgpu_bo *bo,
Chris Wilsonf54d1862016-10-25 13:00:45 +01002592 struct dma_fence **fence)
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002593{
2594 uint32_t domain;
2595 int r;
2596
Roger.He23d2e502017-04-21 14:24:26 +08002597 if (!bo->shadow)
2598 return 0;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002599
Alex Xie1d284792017-04-24 13:53:04 -04002600 r = amdgpu_bo_reserve(bo, true);
Roger.He23d2e502017-04-21 14:24:26 +08002601 if (r)
2602 return r;
2603 domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
2604 /* if bo has been evicted, then no need to recover */
2605 if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
Roger.He82521312017-04-21 13:08:43 +08002606 r = amdgpu_bo_validate(bo->shadow);
2607 if (r) {
2608 DRM_ERROR("bo validate failed!\n");
2609 goto err;
2610 }
2611
2612 r = amdgpu_ttm_bind(&bo->shadow->tbo, &bo->shadow->tbo.mem);
2613 if (r) {
2614 DRM_ERROR("%p bind failed\n", bo->shadow);
2615 goto err;
2616 }
2617
Roger.He23d2e502017-04-21 14:24:26 +08002618 r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002619 NULL, fence, true);
Roger.He23d2e502017-04-21 14:24:26 +08002620 if (r) {
2621 DRM_ERROR("recover page table failed!\n");
2622 goto err;
2623 }
2624 }
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002625err:
Roger.He23d2e502017-04-21 14:24:26 +08002626 amdgpu_bo_unreserve(bo);
2627 return r;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002628}
2629
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002630/**
Monk Liua90ad3c2017-01-23 14:22:08 +08002631 * amdgpu_sriov_gpu_reset - reset the asic
2632 *
2633 * @adev: amdgpu device pointer
Monk Liu7225f872017-04-26 14:51:54 +08002634 * @job: which job trigger hang
Monk Liua90ad3c2017-01-23 14:22:08 +08002635 *
2636 * Attempt the reset the GPU if it has hung (all asics).
2637 * for SRIOV case.
2638 * Returns 0 for success or an error on failure.
2639 */
Monk Liu7225f872017-04-26 14:51:54 +08002640int amdgpu_sriov_gpu_reset(struct amdgpu_device *adev, struct amdgpu_job *job)
Monk Liua90ad3c2017-01-23 14:22:08 +08002641{
Monk Liu65781c72017-05-11 13:36:44 +08002642 int i, j, r = 0;
Monk Liua90ad3c2017-01-23 14:22:08 +08002643 int resched;
2644 struct amdgpu_bo *bo, *tmp;
2645 struct amdgpu_ring *ring;
2646 struct dma_fence *fence = NULL, *next = NULL;
2647
Monk Liu147b5982017-01-25 15:48:01 +08002648 mutex_lock(&adev->virt.lock_reset);
Monk Liua90ad3c2017-01-23 14:22:08 +08002649 atomic_inc(&adev->gpu_reset_counter);
Monk Liu1fb37a32017-01-26 15:36:37 +08002650 adev->gfx.in_reset = true;
Monk Liua90ad3c2017-01-23 14:22:08 +08002651
2652 /* block TTM */
2653 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
2654
Monk Liu65781c72017-05-11 13:36:44 +08002655 /* we start from the ring trigger GPU hang */
2656 j = job ? job->ring->idx : 0;
Monk Liua90ad3c2017-01-23 14:22:08 +08002657
Monk Liu65781c72017-05-11 13:36:44 +08002658 /* block scheduler */
2659 for (i = j; i < j + AMDGPU_MAX_RINGS; ++i) {
2660 ring = adev->rings[i % AMDGPU_MAX_RINGS];
Monk Liua90ad3c2017-01-23 14:22:08 +08002661 if (!ring || !ring->sched.thread)
2662 continue;
2663
2664 kthread_park(ring->sched.thread);
Monk Liua90ad3c2017-01-23 14:22:08 +08002665
Monk Liu65781c72017-05-11 13:36:44 +08002666 if (job && j != i)
2667 continue;
2668
Monk Liu4f059ec2017-05-11 13:59:15 +08002669 /* here give the last chance to check if job removed from mirror-list
Monk Liu65781c72017-05-11 13:36:44 +08002670 * since we already pay some time on kthread_park */
Monk Liu4f059ec2017-05-11 13:59:15 +08002671 if (job && list_empty(&job->base.node)) {
Monk Liu65781c72017-05-11 13:36:44 +08002672 kthread_unpark(ring->sched.thread);
2673 goto give_up_reset;
2674 }
2675
2676 if (amd_sched_invalidate_job(&job->base, amdgpu_job_hang_limit))
2677 amd_sched_job_kickout(&job->base);
2678
2679 /* only do job_reset on the hang ring if @job not NULL */
2680 amd_sched_hw_job_reset(&ring->sched);
2681
2682 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
2683 amdgpu_fence_driver_force_completion_ring(ring);
2684 }
Monk Liua90ad3c2017-01-23 14:22:08 +08002685
2686 /* request to take full control of GPU before re-initialization */
Monk Liu7225f872017-04-26 14:51:54 +08002687 if (job)
Monk Liua90ad3c2017-01-23 14:22:08 +08002688 amdgpu_virt_reset_gpu(adev);
2689 else
2690 amdgpu_virt_request_full_gpu(adev, true);
2691
2692
2693 /* Resume IP prior to SMC */
Monk Liue4f0fdc2017-02-09 11:55:49 +08002694 amdgpu_sriov_reinit_early(adev);
Monk Liua90ad3c2017-01-23 14:22:08 +08002695
2696 /* we need recover gart prior to run SMC/CP/SDMA resume */
2697 amdgpu_ttm_recover_gart(adev);
2698
2699 /* now we are okay to resume SMC/CP/SDMA */
Monk Liue4f0fdc2017-02-09 11:55:49 +08002700 amdgpu_sriov_reinit_late(adev);
Monk Liua90ad3c2017-01-23 14:22:08 +08002701
2702 amdgpu_irq_gpu_reset_resume_helper(adev);
2703
2704 if (amdgpu_ib_ring_tests(adev))
2705 dev_err(adev->dev, "[GPU_RESET] ib ring test failed (%d).\n", r);
2706
2707 /* release full control of GPU after ib test */
2708 amdgpu_virt_release_full_gpu(adev, true);
2709
2710 DRM_INFO("recover vram bo from shadow\n");
2711
2712 ring = adev->mman.buffer_funcs_ring;
2713 mutex_lock(&adev->shadow_list_lock);
2714 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
Monk Liu236763d2017-05-01 16:15:31 +08002715 next = NULL;
Monk Liua90ad3c2017-01-23 14:22:08 +08002716 amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
2717 if (fence) {
2718 r = dma_fence_wait(fence, false);
2719 if (r) {
2720 WARN(r, "recovery from shadow isn't completed\n");
2721 break;
2722 }
2723 }
2724
2725 dma_fence_put(fence);
2726 fence = next;
2727 }
2728 mutex_unlock(&adev->shadow_list_lock);
2729
2730 if (fence) {
2731 r = dma_fence_wait(fence, false);
2732 if (r)
2733 WARN(r, "recovery from shadow isn't completed\n");
2734 }
2735 dma_fence_put(fence);
2736
Monk Liu65781c72017-05-11 13:36:44 +08002737 for (i = j; i < j + AMDGPU_MAX_RINGS; ++i) {
2738 ring = adev->rings[i % AMDGPU_MAX_RINGS];
Monk Liua90ad3c2017-01-23 14:22:08 +08002739 if (!ring || !ring->sched.thread)
2740 continue;
2741
Monk Liu65781c72017-05-11 13:36:44 +08002742 if (job && j != i) {
2743 kthread_unpark(ring->sched.thread);
2744 continue;
2745 }
2746
Monk Liua90ad3c2017-01-23 14:22:08 +08002747 amd_sched_job_recovery(&ring->sched);
2748 kthread_unpark(ring->sched.thread);
2749 }
2750
2751 drm_helper_resume_force_mode(adev->ddev);
Monk Liu65781c72017-05-11 13:36:44 +08002752give_up_reset:
Monk Liua90ad3c2017-01-23 14:22:08 +08002753 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
2754 if (r) {
2755 /* bad news, how to tell it to userspace ? */
2756 dev_info(adev->dev, "GPU reset failed\n");
Monk Liu65781c72017-05-11 13:36:44 +08002757 } else {
2758 dev_info(adev->dev, "GPU reset successed!\n");
Monk Liua90ad3c2017-01-23 14:22:08 +08002759 }
2760
Monk Liu1fb37a32017-01-26 15:36:37 +08002761 adev->gfx.in_reset = false;
Monk Liu147b5982017-01-25 15:48:01 +08002762 mutex_unlock(&adev->virt.lock_reset);
Monk Liua90ad3c2017-01-23 14:22:08 +08002763 return r;
2764}
2765
2766/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002767 * amdgpu_gpu_reset - reset the asic
2768 *
2769 * @adev: amdgpu device pointer
2770 *
2771 * Attempt the reset the GPU if it has hung (all asics).
2772 * Returns 0 for success or an error on failure.
2773 */
2774int amdgpu_gpu_reset(struct amdgpu_device *adev)
2775{
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002776 int i, r;
2777 int resched;
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002778 bool need_full_reset, vram_lost = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002779
Chunming Zhou63fbf422016-07-15 11:19:20 +08002780 if (!amdgpu_check_soft_reset(adev)) {
2781 DRM_INFO("No hardware hang detected. Did some blocks stall?\n");
2782 return 0;
2783 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002784
Marek Olšákd94aed52015-05-05 21:13:49 +02002785 atomic_inc(&adev->gpu_reset_counter);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002786
Chunming Zhoua3c47d62016-06-30 16:44:41 +08002787 /* block TTM */
2788 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
2789
Chunming Zhou0875dc92016-06-12 15:41:58 +08002790 /* block scheduler */
2791 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2792 struct amdgpu_ring *ring = adev->rings[i];
2793
Chunming Zhou51687752017-04-24 17:09:15 +08002794 if (!ring || !ring->sched.thread)
Chunming Zhou0875dc92016-06-12 15:41:58 +08002795 continue;
2796 kthread_park(ring->sched.thread);
Chunming Zhouaa1c8902016-06-30 13:56:02 +08002797 amd_sched_hw_job_reset(&ring->sched);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002798 }
Chunming Zhou2200eda2016-06-30 16:53:02 +08002799 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
2800 amdgpu_fence_driver_force_completion(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002801
Chunming Zhou35d782f2016-07-15 15:57:13 +08002802 need_full_reset = amdgpu_need_full_reset(adev);
2803
2804 if (!need_full_reset) {
2805 amdgpu_pre_soft_reset(adev);
2806 r = amdgpu_soft_reset(adev);
2807 amdgpu_post_soft_reset(adev);
2808 if (r || amdgpu_check_soft_reset(adev)) {
2809 DRM_INFO("soft reset failed, will fallback to full reset!\n");
2810 need_full_reset = true;
2811 }
2812 }
2813
2814 if (need_full_reset) {
Chunming Zhou35d782f2016-07-15 15:57:13 +08002815 r = amdgpu_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002816
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002817retry:
Chunming Zhou35d782f2016-07-15 15:57:13 +08002818 /* Disable fb access */
2819 if (adev->mode_info.num_crtc) {
2820 struct amdgpu_mode_mc_save save;
2821 amdgpu_display_stop_mc_access(adev, &save);
2822 amdgpu_wait_for_idle(adev, AMD_IP_BLOCK_TYPE_GMC);
2823 }
Alex Deucherbe34d3b2017-03-03 14:26:51 -05002824 if (adev->is_atom_fw)
2825 amdgpu_atomfirmware_scratch_regs_save(adev);
2826 else
2827 amdgpu_atombios_scratch_regs_save(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002828 r = amdgpu_asic_reset(adev);
Alex Deucherbe34d3b2017-03-03 14:26:51 -05002829 if (adev->is_atom_fw)
2830 amdgpu_atomfirmware_scratch_regs_restore(adev);
2831 else
2832 amdgpu_atombios_scratch_regs_restore(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002833 /* post card */
2834 amdgpu_atom_asic_init(adev->mode_info.atom_context);
Alex Deucherbfa99262016-01-15 11:59:48 -05002835
Chunming Zhou35d782f2016-07-15 15:57:13 +08002836 if (!r) {
2837 dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
Chunming Zhoufcf06492017-05-05 10:33:33 +08002838 r = amdgpu_resume_phase1(adev);
2839 if (r)
2840 goto out;
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002841 vram_lost = amdgpu_check_vram_lost(adev);
2842 if (vram_lost)
2843 DRM_ERROR("VRAM is lost!\n");
Chunming Zhou2c0d7312016-08-30 16:36:25 +08002844 r = amdgpu_ttm_recover_gart(adev);
2845 if (r)
Chunming Zhoufcf06492017-05-05 10:33:33 +08002846 goto out;
2847 r = amdgpu_resume_phase2(adev);
2848 if (r)
2849 goto out;
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08002850 if (vram_lost)
2851 amdgpu_fill_reset_magic(adev);
Chunming Zhou2c0d7312016-08-30 16:36:25 +08002852 }
Chunming Zhoufcf06492017-05-05 10:33:33 +08002853 }
2854out:
2855 if (!r) {
2856 amdgpu_irq_gpu_reset_resume_helper(adev);
Chunming Zhou1f465082016-06-30 15:02:26 +08002857 r = amdgpu_ib_ring_tests(adev);
2858 if (r) {
2859 dev_err(adev->dev, "ib ring test failed (%d).\n", r);
Chunming Zhou40019dc2016-06-29 16:01:49 +08002860 r = amdgpu_suspend(adev);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002861 need_full_reset = true;
Chunming Zhou40019dc2016-06-29 16:01:49 +08002862 goto retry;
Chunming Zhou1f465082016-06-30 15:02:26 +08002863 }
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002864 /**
2865 * recovery vm page tables, since we cannot depend on VRAM is
2866 * consistent after gpu full reset.
2867 */
2868 if (need_full_reset && amdgpu_need_backup(adev)) {
2869 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
2870 struct amdgpu_bo *bo, *tmp;
Chris Wilsonf54d1862016-10-25 13:00:45 +01002871 struct dma_fence *fence = NULL, *next = NULL;
Chunming Zhou1f465082016-06-30 15:02:26 +08002872
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002873 DRM_INFO("recover vram bo from shadow\n");
2874 mutex_lock(&adev->shadow_list_lock);
2875 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
Monk Liu236763d2017-05-01 16:15:31 +08002876 next = NULL;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002877 amdgpu_recover_vram_from_shadow(adev, ring, bo, &next);
2878 if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01002879 r = dma_fence_wait(fence, false);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002880 if (r) {
Monk Liu1d7b17b2017-01-22 18:52:56 +08002881 WARN(r, "recovery from shadow isn't completed\n");
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002882 break;
2883 }
2884 }
2885
Chris Wilsonf54d1862016-10-25 13:00:45 +01002886 dma_fence_put(fence);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002887 fence = next;
2888 }
2889 mutex_unlock(&adev->shadow_list_lock);
2890 if (fence) {
Chris Wilsonf54d1862016-10-25 13:00:45 +01002891 r = dma_fence_wait(fence, false);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002892 if (r)
Monk Liu1d7b17b2017-01-22 18:52:56 +08002893 WARN(r, "recovery from shadow isn't completed\n");
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002894 }
Chris Wilsonf54d1862016-10-25 13:00:45 +01002895 dma_fence_put(fence);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002896 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002897 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
2898 struct amdgpu_ring *ring = adev->rings[i];
Chunming Zhou51687752017-04-24 17:09:15 +08002899
2900 if (!ring || !ring->sched.thread)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002901 continue;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002902
Chunming Zhouaa1c8902016-06-30 13:56:02 +08002903 amd_sched_job_recovery(&ring->sched);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002904 kthread_unpark(ring->sched.thread);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002905 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002906 } else {
Chunming Zhou2200eda2016-06-30 16:53:02 +08002907 dev_err(adev->dev, "asic resume failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002908 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
Chunming Zhou51687752017-04-24 17:09:15 +08002909 if (adev->rings[i] && adev->rings[i]->sched.thread) {
Chunming Zhou0875dc92016-06-12 15:41:58 +08002910 kthread_unpark(adev->rings[i]->sched.thread);
Chunming Zhou0875dc92016-06-12 15:41:58 +08002911 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002912 }
2913 }
2914
2915 drm_helper_resume_force_mode(adev->ddev);
2916
2917 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
Chunming Zhou6643be62017-05-05 10:50:09 +08002918 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002919 /* bad news, how to tell it to userspace ? */
2920 dev_info(adev->dev, "GPU reset failed\n");
Chunming Zhou6643be62017-05-05 10:50:09 +08002921 else
2922 dev_info(adev->dev, "GPU reset successed!\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002923
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002924 return r;
2925}
2926
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002927void amdgpu_get_pcie_info(struct amdgpu_device *adev)
2928{
2929 u32 mask;
2930 int ret;
2931
Alex Deuchercd474ba2016-02-04 10:21:23 -05002932 if (amdgpu_pcie_gen_cap)
2933 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
2934
2935 if (amdgpu_pcie_lane_cap)
2936 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
2937
2938 /* covers APUs as well */
2939 if (pci_is_root_bus(adev->pdev->bus)) {
2940 if (adev->pm.pcie_gen_mask == 0)
2941 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
2942 if (adev->pm.pcie_mlw_mask == 0)
2943 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002944 return;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05002945 }
Alex Deuchercd474ba2016-02-04 10:21:23 -05002946
2947 if (adev->pm.pcie_gen_mask == 0) {
2948 ret = drm_pcie_get_speed_cap_mask(adev->ddev, &mask);
2949 if (!ret) {
2950 adev->pm.pcie_gen_mask = (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
2951 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
2952 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
2953
2954 if (mask & DRM_PCIE_SPEED_25)
2955 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
2956 if (mask & DRM_PCIE_SPEED_50)
2957 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2;
2958 if (mask & DRM_PCIE_SPEED_80)
2959 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3;
2960 } else {
2961 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
2962 }
2963 }
2964 if (adev->pm.pcie_mlw_mask == 0) {
2965 ret = drm_pcie_get_max_link_width(adev->ddev, &mask);
2966 if (!ret) {
2967 switch (mask) {
2968 case 32:
2969 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
2970 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
2971 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2972 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2973 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2974 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2975 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2976 break;
2977 case 16:
2978 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
2979 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2980 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2981 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2982 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2983 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2984 break;
2985 case 12:
2986 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
2987 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2988 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2989 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2990 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2991 break;
2992 case 8:
2993 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
2994 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
2995 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
2996 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
2997 break;
2998 case 4:
2999 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3000 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3001 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3002 break;
3003 case 2:
3004 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3005 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3006 break;
3007 case 1:
3008 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
3009 break;
3010 default:
3011 break;
3012 }
3013 } else {
3014 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003015 }
3016 }
3017}
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003018
3019/*
3020 * Debugfs
3021 */
3022int amdgpu_debugfs_add_files(struct amdgpu_device *adev,
Nils Wallménius06ab6832016-05-02 12:46:15 -04003023 const struct drm_info_list *files,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003024 unsigned nfiles)
3025{
3026 unsigned i;
3027
3028 for (i = 0; i < adev->debugfs_count; i++) {
3029 if (adev->debugfs[i].files == files) {
3030 /* Already registered */
3031 return 0;
3032 }
3033 }
3034
3035 i = adev->debugfs_count + 1;
3036 if (i > AMDGPU_DEBUGFS_MAX_COMPONENTS) {
3037 DRM_ERROR("Reached maximum number of debugfs components.\n");
3038 DRM_ERROR("Report so we increase "
3039 "AMDGPU_DEBUGFS_MAX_COMPONENTS.\n");
3040 return -EINVAL;
3041 }
3042 adev->debugfs[adev->debugfs_count].files = files;
3043 adev->debugfs[adev->debugfs_count].num_files = nfiles;
3044 adev->debugfs_count = i;
3045#if defined(CONFIG_DEBUG_FS)
3046 drm_debugfs_create_files(files, nfiles,
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003047 adev->ddev->primary->debugfs_root,
3048 adev->ddev->primary);
3049#endif
3050 return 0;
3051}
3052
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003053#if defined(CONFIG_DEBUG_FS)
3054
3055static ssize_t amdgpu_debugfs_regs_read(struct file *f, char __user *buf,
3056 size_t size, loff_t *pos)
3057{
Al Viro45063092016-12-04 18:24:56 -05003058 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003059 ssize_t result = 0;
3060 int r;
Tom St Denisbd122672016-07-28 09:39:22 -04003061 bool pm_pg_lock, use_bank;
Tom St Denis566281592016-06-27 11:55:07 -04003062 unsigned instance_bank, sh_bank, se_bank;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003063
3064 if (size & 0x3 || *pos & 0x3)
3065 return -EINVAL;
3066
Tom St Denisbd122672016-07-28 09:39:22 -04003067 /* are we reading registers for which a PG lock is necessary? */
3068 pm_pg_lock = (*pos >> 23) & 1;
3069
Tom St Denis566281592016-06-27 11:55:07 -04003070 if (*pos & (1ULL << 62)) {
3071 se_bank = (*pos >> 24) & 0x3FF;
3072 sh_bank = (*pos >> 34) & 0x3FF;
3073 instance_bank = (*pos >> 44) & 0x3FF;
Tom St Denis32977f92016-10-09 07:41:26 -04003074
3075 if (se_bank == 0x3FF)
3076 se_bank = 0xFFFFFFFF;
3077 if (sh_bank == 0x3FF)
3078 sh_bank = 0xFFFFFFFF;
3079 if (instance_bank == 0x3FF)
3080 instance_bank = 0xFFFFFFFF;
Tom St Denis566281592016-06-27 11:55:07 -04003081 use_bank = 1;
Tom St Denis566281592016-06-27 11:55:07 -04003082 } else {
3083 use_bank = 0;
3084 }
3085
Tom St Denis801a6aa9a62017-03-15 05:34:25 -04003086 *pos &= (1UL << 22) - 1;
Tom St Denisbd122672016-07-28 09:39:22 -04003087
Tom St Denis566281592016-06-27 11:55:07 -04003088 if (use_bank) {
Tom St Denis32977f92016-10-09 07:41:26 -04003089 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
3090 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
Tom St Denis566281592016-06-27 11:55:07 -04003091 return -EINVAL;
3092 mutex_lock(&adev->grbm_idx_mutex);
3093 amdgpu_gfx_select_se_sh(adev, se_bank,
3094 sh_bank, instance_bank);
3095 }
3096
Tom St Denisbd122672016-07-28 09:39:22 -04003097 if (pm_pg_lock)
3098 mutex_lock(&adev->pm.mutex);
3099
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003100 while (size) {
3101 uint32_t value;
3102
3103 if (*pos > adev->rmmio_size)
Tom St Denis566281592016-06-27 11:55:07 -04003104 goto end;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003105
3106 value = RREG32(*pos >> 2);
3107 r = put_user(value, (uint32_t *)buf);
Tom St Denis566281592016-06-27 11:55:07 -04003108 if (r) {
3109 result = r;
3110 goto end;
3111 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003112
3113 result += 4;
3114 buf += 4;
3115 *pos += 4;
3116 size -= 4;
3117 }
3118
Tom St Denis566281592016-06-27 11:55:07 -04003119end:
3120 if (use_bank) {
3121 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
3122 mutex_unlock(&adev->grbm_idx_mutex);
3123 }
3124
Tom St Denisbd122672016-07-28 09:39:22 -04003125 if (pm_pg_lock)
3126 mutex_unlock(&adev->pm.mutex);
3127
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003128 return result;
3129}
3130
3131static ssize_t amdgpu_debugfs_regs_write(struct file *f, const char __user *buf,
3132 size_t size, loff_t *pos)
3133{
Al Viro45063092016-12-04 18:24:56 -05003134 struct amdgpu_device *adev = file_inode(f)->i_private;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003135 ssize_t result = 0;
3136 int r;
Tom St Denis394fdde2016-10-10 07:31:23 -04003137 bool pm_pg_lock, use_bank;
3138 unsigned instance_bank, sh_bank, se_bank;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003139
3140 if (size & 0x3 || *pos & 0x3)
3141 return -EINVAL;
3142
Tom St Denis394fdde2016-10-10 07:31:23 -04003143 /* are we reading registers for which a PG lock is necessary? */
3144 pm_pg_lock = (*pos >> 23) & 1;
3145
3146 if (*pos & (1ULL << 62)) {
3147 se_bank = (*pos >> 24) & 0x3FF;
3148 sh_bank = (*pos >> 34) & 0x3FF;
3149 instance_bank = (*pos >> 44) & 0x3FF;
3150
3151 if (se_bank == 0x3FF)
3152 se_bank = 0xFFFFFFFF;
3153 if (sh_bank == 0x3FF)
3154 sh_bank = 0xFFFFFFFF;
3155 if (instance_bank == 0x3FF)
3156 instance_bank = 0xFFFFFFFF;
3157 use_bank = 1;
3158 } else {
3159 use_bank = 0;
3160 }
3161
Tom St Denis801a6aa9a62017-03-15 05:34:25 -04003162 *pos &= (1UL << 22) - 1;
Tom St Denis394fdde2016-10-10 07:31:23 -04003163
3164 if (use_bank) {
3165 if ((sh_bank != 0xFFFFFFFF && sh_bank >= adev->gfx.config.max_sh_per_se) ||
3166 (se_bank != 0xFFFFFFFF && se_bank >= adev->gfx.config.max_shader_engines))
3167 return -EINVAL;
3168 mutex_lock(&adev->grbm_idx_mutex);
3169 amdgpu_gfx_select_se_sh(adev, se_bank,
3170 sh_bank, instance_bank);
3171 }
3172
3173 if (pm_pg_lock)
3174 mutex_lock(&adev->pm.mutex);
3175
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003176 while (size) {
3177 uint32_t value;
3178
3179 if (*pos > adev->rmmio_size)
3180 return result;
3181
3182 r = get_user(value, (uint32_t *)buf);
3183 if (r)
3184 return r;
3185
3186 WREG32(*pos >> 2, value);
3187
3188 result += 4;
3189 buf += 4;
3190 *pos += 4;
3191 size -= 4;
3192 }
3193
Tom St Denis394fdde2016-10-10 07:31:23 -04003194 if (use_bank) {
3195 amdgpu_gfx_select_se_sh(adev, 0xffffffff, 0xffffffff, 0xffffffff);
3196 mutex_unlock(&adev->grbm_idx_mutex);
3197 }
3198
3199 if (pm_pg_lock)
3200 mutex_unlock(&adev->pm.mutex);
3201
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003202 return result;
3203}
3204
Tom St Denisadcec282016-04-15 13:08:44 -04003205static ssize_t amdgpu_debugfs_regs_pcie_read(struct file *f, char __user *buf,
3206 size_t size, loff_t *pos)
3207{
Al Viro45063092016-12-04 18:24:56 -05003208 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003209 ssize_t result = 0;
3210 int r;
3211
3212 if (size & 0x3 || *pos & 0x3)
3213 return -EINVAL;
3214
3215 while (size) {
3216 uint32_t value;
3217
3218 value = RREG32_PCIE(*pos >> 2);
3219 r = put_user(value, (uint32_t *)buf);
3220 if (r)
3221 return r;
3222
3223 result += 4;
3224 buf += 4;
3225 *pos += 4;
3226 size -= 4;
3227 }
3228
3229 return result;
3230}
3231
3232static ssize_t amdgpu_debugfs_regs_pcie_write(struct file *f, const char __user *buf,
3233 size_t size, loff_t *pos)
3234{
Al Viro45063092016-12-04 18:24:56 -05003235 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003236 ssize_t result = 0;
3237 int r;
3238
3239 if (size & 0x3 || *pos & 0x3)
3240 return -EINVAL;
3241
3242 while (size) {
3243 uint32_t value;
3244
3245 r = get_user(value, (uint32_t *)buf);
3246 if (r)
3247 return r;
3248
3249 WREG32_PCIE(*pos >> 2, value);
3250
3251 result += 4;
3252 buf += 4;
3253 *pos += 4;
3254 size -= 4;
3255 }
3256
3257 return result;
3258}
3259
3260static ssize_t amdgpu_debugfs_regs_didt_read(struct file *f, char __user *buf,
3261 size_t size, loff_t *pos)
3262{
Al Viro45063092016-12-04 18:24:56 -05003263 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003264 ssize_t result = 0;
3265 int r;
3266
3267 if (size & 0x3 || *pos & 0x3)
3268 return -EINVAL;
3269
3270 while (size) {
3271 uint32_t value;
3272
3273 value = RREG32_DIDT(*pos >> 2);
3274 r = put_user(value, (uint32_t *)buf);
3275 if (r)
3276 return r;
3277
3278 result += 4;
3279 buf += 4;
3280 *pos += 4;
3281 size -= 4;
3282 }
3283
3284 return result;
3285}
3286
3287static ssize_t amdgpu_debugfs_regs_didt_write(struct file *f, const char __user *buf,
3288 size_t size, loff_t *pos)
3289{
Al Viro45063092016-12-04 18:24:56 -05003290 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003291 ssize_t result = 0;
3292 int r;
3293
3294 if (size & 0x3 || *pos & 0x3)
3295 return -EINVAL;
3296
3297 while (size) {
3298 uint32_t value;
3299
3300 r = get_user(value, (uint32_t *)buf);
3301 if (r)
3302 return r;
3303
3304 WREG32_DIDT(*pos >> 2, value);
3305
3306 result += 4;
3307 buf += 4;
3308 *pos += 4;
3309 size -= 4;
3310 }
3311
3312 return result;
3313}
3314
3315static ssize_t amdgpu_debugfs_regs_smc_read(struct file *f, char __user *buf,
3316 size_t size, loff_t *pos)
3317{
Al Viro45063092016-12-04 18:24:56 -05003318 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003319 ssize_t result = 0;
3320 int r;
3321
3322 if (size & 0x3 || *pos & 0x3)
3323 return -EINVAL;
3324
3325 while (size) {
3326 uint32_t value;
3327
Tom St Denis6fc0dea2016-08-29 08:39:29 -04003328 value = RREG32_SMC(*pos);
Tom St Denisadcec282016-04-15 13:08:44 -04003329 r = put_user(value, (uint32_t *)buf);
3330 if (r)
3331 return r;
3332
3333 result += 4;
3334 buf += 4;
3335 *pos += 4;
3336 size -= 4;
3337 }
3338
3339 return result;
3340}
3341
3342static ssize_t amdgpu_debugfs_regs_smc_write(struct file *f, const char __user *buf,
3343 size_t size, loff_t *pos)
3344{
Al Viro45063092016-12-04 18:24:56 -05003345 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denisadcec282016-04-15 13:08:44 -04003346 ssize_t result = 0;
3347 int r;
3348
3349 if (size & 0x3 || *pos & 0x3)
3350 return -EINVAL;
3351
3352 while (size) {
3353 uint32_t value;
3354
3355 r = get_user(value, (uint32_t *)buf);
3356 if (r)
3357 return r;
3358
Tom St Denis6fc0dea2016-08-29 08:39:29 -04003359 WREG32_SMC(*pos, value);
Tom St Denisadcec282016-04-15 13:08:44 -04003360
3361 result += 4;
3362 buf += 4;
3363 *pos += 4;
3364 size -= 4;
3365 }
3366
3367 return result;
3368}
3369
Tom St Denis1e051412016-06-27 09:57:18 -04003370static ssize_t amdgpu_debugfs_gca_config_read(struct file *f, char __user *buf,
3371 size_t size, loff_t *pos)
3372{
Al Viro45063092016-12-04 18:24:56 -05003373 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denis1e051412016-06-27 09:57:18 -04003374 ssize_t result = 0;
3375 int r;
3376 uint32_t *config, no_regs = 0;
3377
3378 if (size & 0x3 || *pos & 0x3)
3379 return -EINVAL;
3380
Markus Elfringecab7662016-09-18 17:00:52 +02003381 config = kmalloc_array(256, sizeof(*config), GFP_KERNEL);
Tom St Denis1e051412016-06-27 09:57:18 -04003382 if (!config)
3383 return -ENOMEM;
3384
3385 /* version, increment each time something is added */
Tom St Denis9a999352017-01-18 13:01:25 -05003386 config[no_regs++] = 3;
Tom St Denis1e051412016-06-27 09:57:18 -04003387 config[no_regs++] = adev->gfx.config.max_shader_engines;
3388 config[no_regs++] = adev->gfx.config.max_tile_pipes;
3389 config[no_regs++] = adev->gfx.config.max_cu_per_sh;
3390 config[no_regs++] = adev->gfx.config.max_sh_per_se;
3391 config[no_regs++] = adev->gfx.config.max_backends_per_se;
3392 config[no_regs++] = adev->gfx.config.max_texture_channel_caches;
3393 config[no_regs++] = adev->gfx.config.max_gprs;
3394 config[no_regs++] = adev->gfx.config.max_gs_threads;
3395 config[no_regs++] = adev->gfx.config.max_hw_contexts;
3396 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_frontend;
3397 config[no_regs++] = adev->gfx.config.sc_prim_fifo_size_backend;
3398 config[no_regs++] = adev->gfx.config.sc_hiz_tile_fifo_size;
3399 config[no_regs++] = adev->gfx.config.sc_earlyz_tile_fifo_size;
3400 config[no_regs++] = adev->gfx.config.num_tile_pipes;
3401 config[no_regs++] = adev->gfx.config.backend_enable_mask;
3402 config[no_regs++] = adev->gfx.config.mem_max_burst_length_bytes;
3403 config[no_regs++] = adev->gfx.config.mem_row_size_in_kb;
3404 config[no_regs++] = adev->gfx.config.shader_engine_tile_size;
3405 config[no_regs++] = adev->gfx.config.num_gpus;
3406 config[no_regs++] = adev->gfx.config.multi_gpu_tile_size;
3407 config[no_regs++] = adev->gfx.config.mc_arb_ramcfg;
3408 config[no_regs++] = adev->gfx.config.gb_addr_config;
3409 config[no_regs++] = adev->gfx.config.num_rbs;
3410
Tom St Denis89a8f302016-08-12 15:14:31 -04003411 /* rev==1 */
3412 config[no_regs++] = adev->rev_id;
3413 config[no_regs++] = adev->pg_flags;
3414 config[no_regs++] = adev->cg_flags;
3415
Tom St Denise9f11dc2016-08-17 12:00:51 -04003416 /* rev==2 */
3417 config[no_regs++] = adev->family;
3418 config[no_regs++] = adev->external_rev_id;
3419
Tom St Denis9a999352017-01-18 13:01:25 -05003420 /* rev==3 */
3421 config[no_regs++] = adev->pdev->device;
3422 config[no_regs++] = adev->pdev->revision;
3423 config[no_regs++] = adev->pdev->subsystem_device;
3424 config[no_regs++] = adev->pdev->subsystem_vendor;
3425
Tom St Denis1e051412016-06-27 09:57:18 -04003426 while (size && (*pos < no_regs * 4)) {
3427 uint32_t value;
3428
3429 value = config[*pos >> 2];
3430 r = put_user(value, (uint32_t *)buf);
3431 if (r) {
3432 kfree(config);
3433 return r;
3434 }
3435
3436 result += 4;
3437 buf += 4;
3438 *pos += 4;
3439 size -= 4;
3440 }
3441
3442 kfree(config);
3443 return result;
3444}
3445
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003446static ssize_t amdgpu_debugfs_sensor_read(struct file *f, char __user *buf,
3447 size_t size, loff_t *pos)
3448{
Al Viro45063092016-12-04 18:24:56 -05003449 struct amdgpu_device *adev = file_inode(f)->i_private;
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003450 int idx, x, outsize, r, valuesize;
3451 uint32_t values[16];
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003452
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003453 if (size & 3 || *pos & 0x3)
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003454 return -EINVAL;
3455
Samuel Pitoiset3cbc6142017-02-15 19:32:29 +01003456 if (amdgpu_dpm == 0)
3457 return -EINVAL;
3458
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003459 /* convert offset to sensor number */
3460 idx = *pos >> 2;
3461
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003462 valuesize = sizeof(values);
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003463 if (adev->powerplay.pp_funcs && adev->powerplay.pp_funcs->read_sensor)
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003464 r = adev->powerplay.pp_funcs->read_sensor(adev->powerplay.pp_handle, idx, &values[0], &valuesize);
Samuel Pitoiset3cbc6142017-02-15 19:32:29 +01003465 else if (adev->pm.funcs && adev->pm.funcs->read_sensor)
3466 r = adev->pm.funcs->read_sensor(adev, idx, &values[0],
3467 &valuesize);
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003468 else
3469 return -EINVAL;
3470
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003471 if (size > valuesize)
3472 return -EINVAL;
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003473
Tom St Denis9f8df7d2017-02-09 14:29:01 -05003474 outsize = 0;
3475 x = 0;
3476 if (!r) {
3477 while (size) {
3478 r = put_user(values[x++], (int32_t *)buf);
3479 buf += 4;
3480 size -= 4;
3481 outsize += 4;
3482 }
3483 }
3484
3485 return !r ? outsize : r;
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003486}
Tom St Denis1e051412016-06-27 09:57:18 -04003487
Tom St Denis273d7aa2016-10-11 14:48:55 -04003488static ssize_t amdgpu_debugfs_wave_read(struct file *f, char __user *buf,
3489 size_t size, loff_t *pos)
3490{
3491 struct amdgpu_device *adev = f->f_inode->i_private;
3492 int r, x;
3493 ssize_t result=0;
Tom St Denis472259f2016-10-14 09:49:09 -04003494 uint32_t offset, se, sh, cu, wave, simd, data[32];
Tom St Denis273d7aa2016-10-11 14:48:55 -04003495
3496 if (size & 3 || *pos & 3)
3497 return -EINVAL;
3498
3499 /* decode offset */
3500 offset = (*pos & 0x7F);
3501 se = ((*pos >> 7) & 0xFF);
3502 sh = ((*pos >> 15) & 0xFF);
3503 cu = ((*pos >> 23) & 0xFF);
3504 wave = ((*pos >> 31) & 0xFF);
3505 simd = ((*pos >> 37) & 0xFF);
Tom St Denis273d7aa2016-10-11 14:48:55 -04003506
3507 /* switch to the specific se/sh/cu */
3508 mutex_lock(&adev->grbm_idx_mutex);
3509 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
3510
3511 x = 0;
Tom St Denis472259f2016-10-14 09:49:09 -04003512 if (adev->gfx.funcs->read_wave_data)
3513 adev->gfx.funcs->read_wave_data(adev, simd, wave, data, &x);
Tom St Denis273d7aa2016-10-11 14:48:55 -04003514
3515 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
3516 mutex_unlock(&adev->grbm_idx_mutex);
3517
Tom St Denis5ecfb3b2016-10-13 12:15:03 -04003518 if (!x)
3519 return -EINVAL;
3520
Tom St Denis472259f2016-10-14 09:49:09 -04003521 while (size && (offset < x * 4)) {
Tom St Denis273d7aa2016-10-11 14:48:55 -04003522 uint32_t value;
3523
Tom St Denis472259f2016-10-14 09:49:09 -04003524 value = data[offset >> 2];
Tom St Denis273d7aa2016-10-11 14:48:55 -04003525 r = put_user(value, (uint32_t *)buf);
3526 if (r)
3527 return r;
3528
3529 result += 4;
3530 buf += 4;
Tom St Denis472259f2016-10-14 09:49:09 -04003531 offset += 4;
Tom St Denis273d7aa2016-10-11 14:48:55 -04003532 size -= 4;
3533 }
3534
3535 return result;
3536}
3537
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003538static ssize_t amdgpu_debugfs_gpr_read(struct file *f, char __user *buf,
3539 size_t size, loff_t *pos)
3540{
3541 struct amdgpu_device *adev = f->f_inode->i_private;
3542 int r;
3543 ssize_t result = 0;
3544 uint32_t offset, se, sh, cu, wave, simd, thread, bank, *data;
3545
3546 if (size & 3 || *pos & 3)
3547 return -EINVAL;
3548
3549 /* decode offset */
3550 offset = (*pos & 0xFFF); /* in dwords */
3551 se = ((*pos >> 12) & 0xFF);
3552 sh = ((*pos >> 20) & 0xFF);
3553 cu = ((*pos >> 28) & 0xFF);
3554 wave = ((*pos >> 36) & 0xFF);
3555 simd = ((*pos >> 44) & 0xFF);
3556 thread = ((*pos >> 52) & 0xFF);
3557 bank = ((*pos >> 60) & 1);
3558
3559 data = kmalloc_array(1024, sizeof(*data), GFP_KERNEL);
3560 if (!data)
3561 return -ENOMEM;
3562
3563 /* switch to the specific se/sh/cu */
3564 mutex_lock(&adev->grbm_idx_mutex);
3565 amdgpu_gfx_select_se_sh(adev, se, sh, cu);
3566
3567 if (bank == 0) {
3568 if (adev->gfx.funcs->read_wave_vgprs)
3569 adev->gfx.funcs->read_wave_vgprs(adev, simd, wave, thread, offset, size>>2, data);
3570 } else {
3571 if (adev->gfx.funcs->read_wave_sgprs)
3572 adev->gfx.funcs->read_wave_sgprs(adev, simd, wave, offset, size>>2, data);
3573 }
3574
3575 amdgpu_gfx_select_se_sh(adev, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF);
3576 mutex_unlock(&adev->grbm_idx_mutex);
3577
3578 while (size) {
3579 uint32_t value;
3580
3581 value = data[offset++];
3582 r = put_user(value, (uint32_t *)buf);
3583 if (r) {
3584 result = r;
3585 goto err;
3586 }
3587
3588 result += 4;
3589 buf += 4;
3590 size -= 4;
3591 }
3592
3593err:
3594 kfree(data);
3595 return result;
3596}
3597
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003598static const struct file_operations amdgpu_debugfs_regs_fops = {
3599 .owner = THIS_MODULE,
3600 .read = amdgpu_debugfs_regs_read,
3601 .write = amdgpu_debugfs_regs_write,
3602 .llseek = default_llseek
3603};
Tom St Denisadcec282016-04-15 13:08:44 -04003604static const struct file_operations amdgpu_debugfs_regs_didt_fops = {
3605 .owner = THIS_MODULE,
3606 .read = amdgpu_debugfs_regs_didt_read,
3607 .write = amdgpu_debugfs_regs_didt_write,
3608 .llseek = default_llseek
3609};
3610static const struct file_operations amdgpu_debugfs_regs_pcie_fops = {
3611 .owner = THIS_MODULE,
3612 .read = amdgpu_debugfs_regs_pcie_read,
3613 .write = amdgpu_debugfs_regs_pcie_write,
3614 .llseek = default_llseek
3615};
3616static const struct file_operations amdgpu_debugfs_regs_smc_fops = {
3617 .owner = THIS_MODULE,
3618 .read = amdgpu_debugfs_regs_smc_read,
3619 .write = amdgpu_debugfs_regs_smc_write,
3620 .llseek = default_llseek
3621};
3622
Tom St Denis1e051412016-06-27 09:57:18 -04003623static const struct file_operations amdgpu_debugfs_gca_config_fops = {
3624 .owner = THIS_MODULE,
3625 .read = amdgpu_debugfs_gca_config_read,
3626 .llseek = default_llseek
3627};
3628
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003629static const struct file_operations amdgpu_debugfs_sensors_fops = {
3630 .owner = THIS_MODULE,
3631 .read = amdgpu_debugfs_sensor_read,
3632 .llseek = default_llseek
3633};
3634
Tom St Denis273d7aa2016-10-11 14:48:55 -04003635static const struct file_operations amdgpu_debugfs_wave_fops = {
3636 .owner = THIS_MODULE,
3637 .read = amdgpu_debugfs_wave_read,
3638 .llseek = default_llseek
3639};
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003640static const struct file_operations amdgpu_debugfs_gpr_fops = {
3641 .owner = THIS_MODULE,
3642 .read = amdgpu_debugfs_gpr_read,
3643 .llseek = default_llseek
3644};
Tom St Denis273d7aa2016-10-11 14:48:55 -04003645
Tom St Denisadcec282016-04-15 13:08:44 -04003646static const struct file_operations *debugfs_regs[] = {
3647 &amdgpu_debugfs_regs_fops,
3648 &amdgpu_debugfs_regs_didt_fops,
3649 &amdgpu_debugfs_regs_pcie_fops,
3650 &amdgpu_debugfs_regs_smc_fops,
Tom St Denis1e051412016-06-27 09:57:18 -04003651 &amdgpu_debugfs_gca_config_fops,
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003652 &amdgpu_debugfs_sensors_fops,
Tom St Denis273d7aa2016-10-11 14:48:55 -04003653 &amdgpu_debugfs_wave_fops,
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003654 &amdgpu_debugfs_gpr_fops,
Tom St Denisadcec282016-04-15 13:08:44 -04003655};
3656
3657static const char *debugfs_regs_names[] = {
3658 "amdgpu_regs",
3659 "amdgpu_regs_didt",
3660 "amdgpu_regs_pcie",
3661 "amdgpu_regs_smc",
Tom St Denis1e051412016-06-27 09:57:18 -04003662 "amdgpu_gca_config",
Tom St Denisf2cdaf22016-09-15 10:08:44 -04003663 "amdgpu_sensors",
Tom St Denis273d7aa2016-10-11 14:48:55 -04003664 "amdgpu_wave",
Tom St Denisc5a60ce2016-12-05 11:39:19 -05003665 "amdgpu_gpr",
Tom St Denisadcec282016-04-15 13:08:44 -04003666};
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003667
3668static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
3669{
3670 struct drm_minor *minor = adev->ddev->primary;
3671 struct dentry *ent, *root = minor->debugfs_root;
Tom St Denisadcec282016-04-15 13:08:44 -04003672 unsigned i, j;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003673
Tom St Denisadcec282016-04-15 13:08:44 -04003674 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3675 ent = debugfs_create_file(debugfs_regs_names[i],
3676 S_IFREG | S_IRUGO, root,
3677 adev, debugfs_regs[i]);
3678 if (IS_ERR(ent)) {
3679 for (j = 0; j < i; j++) {
3680 debugfs_remove(adev->debugfs_regs[i]);
3681 adev->debugfs_regs[i] = NULL;
3682 }
3683 return PTR_ERR(ent);
3684 }
3685
3686 if (!i)
3687 i_size_write(ent->d_inode, adev->rmmio_size);
3688 adev->debugfs_regs[i] = ent;
3689 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003690
3691 return 0;
3692}
3693
3694static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev)
3695{
Tom St Denisadcec282016-04-15 13:08:44 -04003696 unsigned i;
3697
3698 for (i = 0; i < ARRAY_SIZE(debugfs_regs); i++) {
3699 if (adev->debugfs_regs[i]) {
3700 debugfs_remove(adev->debugfs_regs[i]);
3701 adev->debugfs_regs[i] = NULL;
3702 }
3703 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003704}
3705
3706int amdgpu_debugfs_init(struct drm_minor *minor)
3707{
3708 return 0;
3709}
Alexander Kuleshov7cebc722015-06-27 13:16:05 +06003710#else
3711static int amdgpu_debugfs_regs_init(struct amdgpu_device *adev)
3712{
3713 return 0;
3714}
3715static void amdgpu_debugfs_regs_cleanup(struct amdgpu_device *adev) { }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003716#endif