blob: a824d5d2ebbed73e3176009e0d0dcc5d5f008e5d [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>
Alex Deucherd38ceaf2015-04-20 16:55:21 -040031#include <drm/drmP.h>
32#include <drm/drm_crtc_helper.h>
Harry Wentland45622362017-09-12 15:58:20 -040033#include <drm/drm_atomic_helper.h>
Alex Deucherd38ceaf2015-04-20 16:55:21 -040034#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>
Gavin Wan89041942017-06-23 13:55:15 -040056#include "amdgpu_vf_error.h"
Alex Deucherd38ceaf2015-04-20 16:55:21 -040057
Yong Zhaoba997702015-11-09 17:21:45 -050058#include "amdgpu_amdkfd.h"
Rex Zhud2f52ac2017-09-22 17:47:27 +080059#include "amdgpu_pm.h"
Alex Deucherd38ceaf2015-04-20 16:55:21 -040060
Alex Deuchere2a75f82017-04-27 16:58:01 -040061MODULE_FIRMWARE("amdgpu/vega10_gpu_info.bin");
Alex Deucher2d2e5e72017-05-09 12:27:35 -040062MODULE_FIRMWARE("amdgpu/raven_gpu_info.bin");
Alex Deuchere2a75f82017-04-27 16:58:01 -040063
Shirish S2dc80b02017-05-25 10:05:25 +053064#define AMDGPU_RESUME_MS 2000
65
Alex Deucherd38ceaf2015-04-20 16:55:21 -040066static const char *amdgpu_asic_name[] = {
Ken Wangda69c1612016-01-21 19:08:55 +080067 "TAHITI",
68 "PITCAIRN",
69 "VERDE",
70 "OLAND",
71 "HAINAN",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040072 "BONAIRE",
73 "KAVERI",
74 "KABINI",
75 "HAWAII",
76 "MULLINS",
77 "TOPAZ",
78 "TONGA",
David Zhang48299f92015-07-08 01:05:16 +080079 "FIJI",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040080 "CARRIZO",
Samuel Li139f4912015-10-08 14:50:27 -040081 "STONEY",
Flora Cui2cc0c0b2016-03-14 18:33:29 -040082 "POLARIS10",
83 "POLARIS11",
Junwei Zhangc4642a42016-12-14 15:32:28 -050084 "POLARIS12",
Ken Wangd4196f02016-03-09 09:28:32 +080085 "VEGA10",
Chunming Zhou2ca8a5d2016-12-07 17:31:19 +080086 "RAVEN",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040087 "LAST",
88};
89
Alex Deucher5494d862018-03-09 15:14:11 -050090static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev);
91
Alex Deuchere3ecdff2018-03-15 17:39:45 -050092/**
93 * amdgpu_device_is_px - Is the device is a dGPU with HG/PX power control
94 *
95 * @dev: drm_device pointer
96 *
97 * Returns true if the device is a dGPU with HG/PX power control,
98 * otherwise return false.
99 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400100bool amdgpu_device_is_px(struct drm_device *dev)
101{
102 struct amdgpu_device *adev = dev->dev_private;
103
Jammy Zhou2f7d10b2015-07-22 11:29:01 +0800104 if (adev->flags & AMD_IS_PX)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400105 return true;
106 return false;
107}
108
109/*
110 * MMIO register access helper functions.
111 */
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500112/**
113 * amdgpu_mm_rreg - read a memory mapped IO register
114 *
115 * @adev: amdgpu_device pointer
116 * @reg: dword aligned register offset
117 * @acc_flags: access flags which require special behavior
118 *
119 * Returns the 32 bit value from the offset specified.
120 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400121uint32_t amdgpu_mm_rreg(struct amdgpu_device *adev, uint32_t reg,
Monk Liu15d72fd2017-01-25 15:07:40 +0800122 uint32_t acc_flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400123{
Tom St Denisf4b373f2016-05-31 08:02:27 -0400124 uint32_t ret;
125
pding43ca8ef2017-10-13 15:38:35 +0800126 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev))
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800127 return amdgpu_virt_kiq_rreg(adev, reg);
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800128
Monk Liu15d72fd2017-01-25 15:07:40 +0800129 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
Tom St Denisf4b373f2016-05-31 08:02:27 -0400130 ret = readl(((void __iomem *)adev->rmmio) + (reg * 4));
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400131 else {
132 unsigned long flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400133
134 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
135 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
136 ret = readl(((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
137 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400138 }
Tom St Denisf4b373f2016-05-31 08:02:27 -0400139 trace_amdgpu_mm_rreg(adev->pdev->device, reg, ret);
140 return ret;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400141}
142
Monk Liu421a2a32018-01-04 18:13:20 +0800143/*
144 * MMIO register read with bytes helper functions
145 * @offset:bytes offset from MMIO start
146 *
147*/
148
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500149/**
150 * amdgpu_mm_rreg8 - read a memory mapped IO register
151 *
152 * @adev: amdgpu_device pointer
153 * @offset: byte aligned register offset
154 *
155 * Returns the 8 bit value from the offset specified.
156 */
Monk Liu421a2a32018-01-04 18:13:20 +0800157uint8_t amdgpu_mm_rreg8(struct amdgpu_device *adev, uint32_t offset) {
158 if (offset < adev->rmmio_size)
159 return (readb(adev->rmmio + offset));
160 BUG();
161}
162
163/*
164 * MMIO register write with bytes helper functions
165 * @offset:bytes offset from MMIO start
166 * @value: the value want to be written to the register
167 *
168*/
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500169/**
170 * amdgpu_mm_wreg8 - read a memory mapped IO register
171 *
172 * @adev: amdgpu_device pointer
173 * @offset: byte aligned register offset
174 * @value: 8 bit value to write
175 *
176 * Writes the value specified to the offset specified.
177 */
Monk Liu421a2a32018-01-04 18:13:20 +0800178void amdgpu_mm_wreg8(struct amdgpu_device *adev, uint32_t offset, uint8_t value) {
179 if (offset < adev->rmmio_size)
180 writeb(value, adev->rmmio + offset);
181 else
182 BUG();
183}
184
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500185/**
186 * amdgpu_mm_wreg - write to a memory mapped IO register
187 *
188 * @adev: amdgpu_device pointer
189 * @reg: dword aligned register offset
190 * @v: 32 bit value to write to the register
191 * @acc_flags: access flags which require special behavior
192 *
193 * Writes the value specified to the offset specified.
194 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400195void amdgpu_mm_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v,
Monk Liu15d72fd2017-01-25 15:07:40 +0800196 uint32_t acc_flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400197{
Tom St Denisf4b373f2016-05-31 08:02:27 -0400198 trace_amdgpu_mm_wreg(adev->pdev->device, reg, v);
Monk Liu4e99a442016-03-31 13:26:59 +0800199
Ken Wang47ed4e12017-07-04 13:11:52 +0800200 if (adev->asic_type >= CHIP_VEGA10 && reg == 0) {
201 adev->last_mm_index = v;
202 }
203
pding43ca8ef2017-10-13 15:38:35 +0800204 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev))
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800205 return amdgpu_virt_kiq_wreg(adev, reg, v);
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800206
Monk Liu15d72fd2017-01-25 15:07:40 +0800207 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400208 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
209 else {
210 unsigned long flags;
211
212 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
213 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
214 writel(v, ((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
215 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
216 }
Ken Wang47ed4e12017-07-04 13:11:52 +0800217
218 if (adev->asic_type >= CHIP_VEGA10 && reg == 1 && adev->last_mm_index == 0x5702C) {
219 udelay(500);
220 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400221}
222
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500223/**
224 * amdgpu_io_rreg - read an IO register
225 *
226 * @adev: amdgpu_device pointer
227 * @reg: dword aligned register offset
228 *
229 * Returns the 32 bit value from the offset specified.
230 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400231u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 reg)
232{
233 if ((reg * 4) < adev->rio_mem_size)
234 return ioread32(adev->rio_mem + (reg * 4));
235 else {
236 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
237 return ioread32(adev->rio_mem + (mmMM_DATA * 4));
238 }
239}
240
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500241/**
242 * amdgpu_io_wreg - write to an IO register
243 *
244 * @adev: amdgpu_device pointer
245 * @reg: dword aligned register offset
246 * @v: 32 bit value to write to the register
247 *
248 * Writes the value specified to the offset specified.
249 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400250void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
251{
Ken Wang47ed4e12017-07-04 13:11:52 +0800252 if (adev->asic_type >= CHIP_VEGA10 && reg == 0) {
253 adev->last_mm_index = v;
254 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400255
256 if ((reg * 4) < adev->rio_mem_size)
257 iowrite32(v, adev->rio_mem + (reg * 4));
258 else {
259 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
260 iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
261 }
Ken Wang47ed4e12017-07-04 13:11:52 +0800262
263 if (adev->asic_type >= CHIP_VEGA10 && reg == 1 && adev->last_mm_index == 0x5702C) {
264 udelay(500);
265 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400266}
267
268/**
269 * amdgpu_mm_rdoorbell - read a doorbell dword
270 *
271 * @adev: amdgpu_device pointer
272 * @index: doorbell index
273 *
274 * Returns the value in the doorbell aperture at the
275 * requested doorbell index (CIK).
276 */
277u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
278{
279 if (index < adev->doorbell.num_doorbells) {
280 return readl(adev->doorbell.ptr + index);
281 } else {
282 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
283 return 0;
284 }
285}
286
287/**
288 * amdgpu_mm_wdoorbell - write a doorbell dword
289 *
290 * @adev: amdgpu_device pointer
291 * @index: doorbell index
292 * @v: value to write
293 *
294 * Writes @v to the doorbell aperture at the
295 * requested doorbell index (CIK).
296 */
297void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
298{
299 if (index < adev->doorbell.num_doorbells) {
300 writel(v, adev->doorbell.ptr + index);
301 } else {
302 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
303 }
304}
305
306/**
Ken Wang832be402016-03-18 15:23:08 +0800307 * amdgpu_mm_rdoorbell64 - read a doorbell Qword
308 *
309 * @adev: amdgpu_device pointer
310 * @index: doorbell index
311 *
312 * Returns the value in the doorbell aperture at the
313 * requested doorbell index (VEGA10+).
314 */
315u64 amdgpu_mm_rdoorbell64(struct amdgpu_device *adev, u32 index)
316{
317 if (index < adev->doorbell.num_doorbells) {
318 return atomic64_read((atomic64_t *)(adev->doorbell.ptr + index));
319 } else {
320 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
321 return 0;
322 }
323}
324
325/**
326 * amdgpu_mm_wdoorbell64 - write a doorbell Qword
327 *
328 * @adev: amdgpu_device pointer
329 * @index: doorbell index
330 * @v: value to write
331 *
332 * Writes @v to the doorbell aperture at the
333 * requested doorbell index (VEGA10+).
334 */
335void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v)
336{
337 if (index < adev->doorbell.num_doorbells) {
338 atomic64_set((atomic64_t *)(adev->doorbell.ptr + index), v);
339 } else {
340 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
341 }
342}
343
344/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400345 * amdgpu_invalid_rreg - dummy reg read function
346 *
347 * @adev: amdgpu device pointer
348 * @reg: offset of register
349 *
350 * Dummy register read function. Used for register blocks
351 * that certain asics don't have (all asics).
352 * Returns the value in the register.
353 */
354static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
355{
356 DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
357 BUG();
358 return 0;
359}
360
361/**
362 * amdgpu_invalid_wreg - dummy reg write function
363 *
364 * @adev: amdgpu device pointer
365 * @reg: offset of register
366 * @v: value to write to the register
367 *
368 * Dummy register read function. Used for register blocks
369 * that certain asics don't have (all asics).
370 */
371static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
372{
373 DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
374 reg, v);
375 BUG();
376}
377
378/**
379 * amdgpu_block_invalid_rreg - dummy reg read function
380 *
381 * @adev: amdgpu device pointer
382 * @block: offset of instance
383 * @reg: offset of register
384 *
385 * Dummy register read function. Used for register blocks
386 * that certain asics don't have (all asics).
387 * Returns the value in the register.
388 */
389static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
390 uint32_t block, uint32_t reg)
391{
392 DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
393 reg, block);
394 BUG();
395 return 0;
396}
397
398/**
399 * amdgpu_block_invalid_wreg - dummy reg write function
400 *
401 * @adev: amdgpu device pointer
402 * @block: offset of instance
403 * @reg: offset of register
404 * @v: value to write to the register
405 *
406 * Dummy register read function. Used for register blocks
407 * that certain asics don't have (all asics).
408 */
409static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
410 uint32_t block,
411 uint32_t reg, uint32_t v)
412{
413 DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
414 reg, block, v);
415 BUG();
416}
417
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500418/**
419 * amdgpu_device_vram_scratch_init - allocate the VRAM scratch page
420 *
421 * @adev: amdgpu device pointer
422 *
423 * Allocates a scratch page of VRAM for use by various things in the
424 * driver.
425 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500426static int amdgpu_device_vram_scratch_init(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400427{
Christian Königa4a02772017-07-27 17:24:36 +0200428 return amdgpu_bo_create_kernel(adev, AMDGPU_GPU_PAGE_SIZE,
429 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
430 &adev->vram_scratch.robj,
431 &adev->vram_scratch.gpu_addr,
432 (void **)&adev->vram_scratch.ptr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400433}
434
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500435/**
436 * amdgpu_device_vram_scratch_fini - Free the VRAM scratch page
437 *
438 * @adev: amdgpu device pointer
439 *
440 * Frees the VRAM scratch page.
441 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500442static void amdgpu_device_vram_scratch_fini(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400443{
Christian König078af1a2017-07-27 17:43:00 +0200444 amdgpu_bo_free_kernel(&adev->vram_scratch.robj, NULL, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400445}
446
447/**
Alex Deucher9c3f2b52017-12-14 16:20:19 -0500448 * amdgpu_device_program_register_sequence - program an array of registers.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400449 *
450 * @adev: amdgpu_device pointer
451 * @registers: pointer to the register array
452 * @array_size: size of the register array
453 *
454 * Programs an array or registers with and and or masks.
455 * This is a helper for setting golden registers.
456 */
Alex Deucher9c3f2b52017-12-14 16:20:19 -0500457void amdgpu_device_program_register_sequence(struct amdgpu_device *adev,
458 const u32 *registers,
459 const u32 array_size)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400460{
461 u32 tmp, reg, and_mask, or_mask;
462 int i;
463
464 if (array_size % 3)
465 return;
466
467 for (i = 0; i < array_size; i +=3) {
468 reg = registers[i + 0];
469 and_mask = registers[i + 1];
470 or_mask = registers[i + 2];
471
472 if (and_mask == 0xffffffff) {
473 tmp = or_mask;
474 } else {
475 tmp = RREG32(reg);
476 tmp &= ~and_mask;
477 tmp |= or_mask;
478 }
479 WREG32(reg, tmp);
480 }
481}
482
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500483/**
484 * amdgpu_device_pci_config_reset - reset the GPU
485 *
486 * @adev: amdgpu_device pointer
487 *
488 * Resets the GPU using the pci config reset sequence.
489 * Only applicable to asics prior to vega10.
490 */
Alex Deucher8111c382017-12-14 16:22:53 -0500491void amdgpu_device_pci_config_reset(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400492{
493 pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
494}
495
496/*
497 * GPU doorbell aperture helpers function.
498 */
499/**
Alex Deucher06ec9072017-12-14 15:02:39 -0500500 * amdgpu_device_doorbell_init - Init doorbell driver information.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400501 *
502 * @adev: amdgpu_device pointer
503 *
504 * Init doorbell driver information (CIK)
505 * Returns 0 on success, error on failure.
506 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500507static int amdgpu_device_doorbell_init(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400508{
Christian König705e5192017-06-08 11:15:16 +0200509 /* No doorbell on SI hardware generation */
510 if (adev->asic_type < CHIP_BONAIRE) {
511 adev->doorbell.base = 0;
512 adev->doorbell.size = 0;
513 adev->doorbell.num_doorbells = 0;
514 adev->doorbell.ptr = NULL;
515 return 0;
516 }
517
Christian Königd6895ad2017-02-28 10:36:43 +0100518 if (pci_resource_flags(adev->pdev, 2) & IORESOURCE_UNSET)
519 return -EINVAL;
520
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400521 /* doorbell bar mapping */
522 adev->doorbell.base = pci_resource_start(adev->pdev, 2);
523 adev->doorbell.size = pci_resource_len(adev->pdev, 2);
524
Christian Königedf600d2016-05-03 15:54:54 +0200525 adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400526 AMDGPU_DOORBELL_MAX_ASSIGNMENT+1);
527 if (adev->doorbell.num_doorbells == 0)
528 return -EINVAL;
529
Christian König8972e5d2017-03-06 13:34:57 +0100530 adev->doorbell.ptr = ioremap(adev->doorbell.base,
531 adev->doorbell.num_doorbells *
532 sizeof(u32));
533 if (adev->doorbell.ptr == NULL)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400534 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400535
536 return 0;
537}
538
539/**
Alex Deucher06ec9072017-12-14 15:02:39 -0500540 * amdgpu_device_doorbell_fini - Tear down doorbell driver information.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400541 *
542 * @adev: amdgpu_device pointer
543 *
544 * Tear down doorbell driver information (CIK)
545 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500546static void amdgpu_device_doorbell_fini(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400547{
548 iounmap(adev->doorbell.ptr);
549 adev->doorbell.ptr = NULL;
550}
551
Alex Deucher22cb0162017-12-14 16:27:11 -0500552
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400553
554/*
Alex Deucher06ec9072017-12-14 15:02:39 -0500555 * amdgpu_device_wb_*()
Alex Xie455a7bc2017-05-08 21:36:03 -0400556 * Writeback is the method by which the GPU updates special pages in memory
Alex Xieea81a172017-05-08 13:41:11 -0400557 * with the status of certain GPU events (fences, ring pointers,etc.).
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400558 */
559
560/**
Alex Deucher06ec9072017-12-14 15:02:39 -0500561 * amdgpu_device_wb_fini - Disable Writeback and free memory
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400562 *
563 * @adev: amdgpu_device pointer
564 *
565 * Disables Writeback and frees the Writeback memory (all asics).
566 * Used at driver shutdown.
567 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500568static void amdgpu_device_wb_fini(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400569{
570 if (adev->wb.wb_obj) {
Alex Deuchera76ed482016-10-21 15:30:36 -0400571 amdgpu_bo_free_kernel(&adev->wb.wb_obj,
572 &adev->wb.gpu_addr,
573 (void **)&adev->wb.wb);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400574 adev->wb.wb_obj = NULL;
575 }
576}
577
578/**
Alex Deucher06ec9072017-12-14 15:02:39 -0500579 * amdgpu_device_wb_init- Init Writeback driver info and allocate memory
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400580 *
581 * @adev: amdgpu_device pointer
582 *
Alex Xie455a7bc2017-05-08 21:36:03 -0400583 * Initializes writeback and allocates writeback memory (all asics).
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400584 * Used at driver startup.
585 * Returns 0 on success or an -error on failure.
586 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500587static int amdgpu_device_wb_init(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400588{
589 int r;
590
591 if (adev->wb.wb_obj == NULL) {
Alex Deucher97407b62017-07-28 12:14:15 -0400592 /* AMDGPU_MAX_WB * sizeof(uint32_t) * 8 = AMDGPU_MAX_WB 256bit slots */
593 r = amdgpu_bo_create_kernel(adev, AMDGPU_MAX_WB * sizeof(uint32_t) * 8,
Alex Deuchera76ed482016-10-21 15:30:36 -0400594 PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
595 &adev->wb.wb_obj, &adev->wb.gpu_addr,
596 (void **)&adev->wb.wb);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400597 if (r) {
598 dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
599 return r;
600 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400601
602 adev->wb.num_wb = AMDGPU_MAX_WB;
603 memset(&adev->wb.used, 0, sizeof(adev->wb.used));
604
605 /* clear wb memory */
Monk Liu73469582017-12-29 17:06:41 +0800606 memset((char *)adev->wb.wb, 0, AMDGPU_MAX_WB * sizeof(uint32_t) * 8);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400607 }
608
609 return 0;
610}
611
612/**
Alex Deucher131b4b32017-12-14 16:03:43 -0500613 * amdgpu_device_wb_get - Allocate a wb entry
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400614 *
615 * @adev: amdgpu_device pointer
616 * @wb: wb index
617 *
618 * Allocate a wb slot for use by the driver (all asics).
619 * Returns 0 on success or -EINVAL on failure.
620 */
Alex Deucher131b4b32017-12-14 16:03:43 -0500621int amdgpu_device_wb_get(struct amdgpu_device *adev, u32 *wb)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400622{
623 unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
Alex Deucher97407b62017-07-28 12:14:15 -0400624
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400625 if (offset < adev->wb.num_wb) {
626 __set_bit(offset, adev->wb.used);
Monk Liu63ae07c2017-10-17 19:18:56 +0800627 *wb = offset << 3; /* convert to dw offset */
Monk Liu0915fdb2017-06-19 10:19:41 -0400628 return 0;
629 } else {
630 return -EINVAL;
631 }
632}
633
Ken Wang70142852016-03-18 15:08:49 +0800634/**
Alex Deucher131b4b32017-12-14 16:03:43 -0500635 * amdgpu_device_wb_free - Free a wb entry
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400636 *
637 * @adev: amdgpu_device pointer
638 * @wb: wb index
639 *
640 * Free a wb slot allocated for use by the driver (all asics)
641 */
Alex Deucher131b4b32017-12-14 16:03:43 -0500642void amdgpu_device_wb_free(struct amdgpu_device *adev, u32 wb)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400643{
Monk Liu73469582017-12-29 17:06:41 +0800644 wb >>= 3;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400645 if (wb < adev->wb.num_wb)
Monk Liu73469582017-12-29 17:06:41 +0800646 __clear_bit(wb, adev->wb.used);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400647}
648
649/**
Alex Deucher2543e282017-12-14 16:33:36 -0500650 * amdgpu_device_vram_location - try to find VRAM location
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500651 *
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400652 * @adev: amdgpu device structure holding all necessary informations
653 * @mc: memory controller structure holding memory informations
654 * @base: base address at which to put VRAM
655 *
Alex Xie455a7bc2017-05-08 21:36:03 -0400656 * Function will try to place VRAM at base address provided
Christian König3d647c82017-11-16 19:36:10 +0100657 * as parameter.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400658 */
Alex Deucher2543e282017-12-14 16:33:36 -0500659void amdgpu_device_vram_location(struct amdgpu_device *adev,
Christian König770d13b2018-01-12 14:52:22 +0100660 struct amdgpu_gmc *mc, u64 base)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400661{
662 uint64_t limit = (uint64_t)amdgpu_vram_limit << 20;
663
664 mc->vram_start = base;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400665 mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
666 if (limit && limit < mc->real_vram_size)
667 mc->real_vram_size = limit;
668 dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
669 mc->mc_vram_size >> 20, mc->vram_start,
670 mc->vram_end, mc->real_vram_size >> 20);
671}
672
673/**
Alex Deucher2543e282017-12-14 16:33:36 -0500674 * amdgpu_device_gart_location - try to find GTT location
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500675 *
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400676 * @adev: amdgpu device structure holding all necessary informations
677 * @mc: memory controller structure holding memory informations
678 *
679 * Function will place try to place GTT before or after VRAM.
680 *
681 * If GTT size is bigger than space left then we ajust GTT size.
682 * Thus function will never fails.
683 *
684 * FIXME: when reducing GTT size align new size on power of 2.
685 */
Alex Deucher2543e282017-12-14 16:33:36 -0500686void amdgpu_device_gart_location(struct amdgpu_device *adev,
Christian König770d13b2018-01-12 14:52:22 +0100687 struct amdgpu_gmc *mc)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400688{
689 u64 size_af, size_bf;
690
Christian König770d13b2018-01-12 14:52:22 +0100691 size_af = adev->gmc.mc_mask - mc->vram_end;
Christian Königed21c042017-07-06 22:26:05 +0200692 size_bf = mc->vram_start;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400693 if (size_bf > size_af) {
Christian König6f02a692017-07-07 11:56:59 +0200694 if (mc->gart_size > size_bf) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400695 dev_warn(adev->dev, "limiting GTT\n");
Christian König6f02a692017-07-07 11:56:59 +0200696 mc->gart_size = size_bf;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400697 }
Christian König6f02a692017-07-07 11:56:59 +0200698 mc->gart_start = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400699 } else {
Christian König6f02a692017-07-07 11:56:59 +0200700 if (mc->gart_size > size_af) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400701 dev_warn(adev->dev, "limiting GTT\n");
Christian König6f02a692017-07-07 11:56:59 +0200702 mc->gart_size = size_af;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400703 }
Christian Königb98f1b92017-11-16 20:12:51 +0100704 /* VCE doesn't like it when BOs cross a 4GB segment, so align
705 * the GART base on a 4GB boundary as well.
706 */
707 mc->gart_start = ALIGN(mc->vram_end + 1, 0x100000000ULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400708 }
Christian König6f02a692017-07-07 11:56:59 +0200709 mc->gart_end = mc->gart_start + mc->gart_size - 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400710 dev_info(adev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
Christian König6f02a692017-07-07 11:56:59 +0200711 mc->gart_size >> 20, mc->gart_start, mc->gart_end);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400712}
713
Christian Königd6895ad2017-02-28 10:36:43 +0100714/**
715 * amdgpu_device_resize_fb_bar - try to resize FB BAR
716 *
717 * @adev: amdgpu_device pointer
718 *
719 * Try to resize FB BAR to make all VRAM CPU accessible. We try very hard not
720 * to fail, but if any of the BARs is not accessible after the size we abort
721 * driver loading by returning -ENODEV.
722 */
723int amdgpu_device_resize_fb_bar(struct amdgpu_device *adev)
724{
Christian König770d13b2018-01-12 14:52:22 +0100725 u64 space_needed = roundup_pow_of_two(adev->gmc.real_vram_size);
Christian Königd6895ad2017-02-28 10:36:43 +0100726 u32 rbar_size = order_base_2(((space_needed >> 20) | 1)) - 1;
Christian König31b8ada2017-11-15 20:07:38 +0100727 struct pci_bus *root;
728 struct resource *res;
729 unsigned i;
Christian Königd6895ad2017-02-28 10:36:43 +0100730 u16 cmd;
731 int r;
732
pding0c03b912017-11-07 11:02:00 +0800733 /* Bypass for VF */
734 if (amdgpu_sriov_vf(adev))
735 return 0;
736
Christian König31b8ada2017-11-15 20:07:38 +0100737 /* Check if the root BUS has 64bit memory resources */
738 root = adev->pdev->bus;
739 while (root->parent)
740 root = root->parent;
741
742 pci_bus_for_each_resource(root, res, i) {
Christian König0ebb7c52018-01-07 10:18:57 +0100743 if (res && res->flags & (IORESOURCE_MEM | IORESOURCE_MEM_64) &&
Christian König31b8ada2017-11-15 20:07:38 +0100744 res->start > 0x100000000ull)
745 break;
746 }
747
748 /* Trying to resize is pointless without a root hub window above 4GB */
749 if (!res)
750 return 0;
751
Christian Königd6895ad2017-02-28 10:36:43 +0100752 /* Disable memory decoding while we change the BAR addresses and size */
753 pci_read_config_word(adev->pdev, PCI_COMMAND, &cmd);
754 pci_write_config_word(adev->pdev, PCI_COMMAND,
755 cmd & ~PCI_COMMAND_MEMORY);
756
757 /* Free the VRAM and doorbell BAR, we most likely need to move both. */
Alex Deucher06ec9072017-12-14 15:02:39 -0500758 amdgpu_device_doorbell_fini(adev);
Christian Königd6895ad2017-02-28 10:36:43 +0100759 if (adev->asic_type >= CHIP_BONAIRE)
760 pci_release_resource(adev->pdev, 2);
761
762 pci_release_resource(adev->pdev, 0);
763
764 r = pci_resize_resource(adev->pdev, 0, rbar_size);
765 if (r == -ENOSPC)
766 DRM_INFO("Not enough PCI address space for a large BAR.");
767 else if (r && r != -ENOTSUPP)
768 DRM_ERROR("Problem resizing BAR0 (%d).", r);
769
770 pci_assign_unassigned_bus_resources(adev->pdev->bus);
771
772 /* When the doorbell or fb BAR isn't available we have no chance of
773 * using the device.
774 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500775 r = amdgpu_device_doorbell_init(adev);
Christian Königd6895ad2017-02-28 10:36:43 +0100776 if (r || (pci_resource_flags(adev->pdev, 0) & IORESOURCE_UNSET))
777 return -ENODEV;
778
779 pci_write_config_word(adev->pdev, PCI_COMMAND, cmd);
780
781 return 0;
782}
Horace Chena05502e2017-09-29 14:41:57 +0800783
784/*
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400785 * GPU helpers function.
786 */
787/**
Alex Deucher39c640c2017-12-15 16:22:11 -0500788 * amdgpu_device_need_post - check if the hw need post or not
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400789 *
790 * @adev: amdgpu_device pointer
791 *
Jim Quc836fec2017-02-10 15:59:59 +0800792 * Check if the asic has been initialized (all asics) at driver startup
793 * or post is needed if hw reset is performed.
794 * Returns true if need or false if not.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400795 */
Alex Deucher39c640c2017-12-15 16:22:11 -0500796bool amdgpu_device_need_post(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400797{
798 uint32_t reg;
799
Monk Liubec86372016-09-14 19:38:08 +0800800 if (amdgpu_sriov_vf(adev))
801 return false;
802
803 if (amdgpu_passthrough(adev)) {
Monk Liu1da2c322016-11-11 11:24:29 +0800804 /* for FIJI: In whole GPU pass-through virtualization case, after VM reboot
805 * some old smc fw still need driver do vPost otherwise gpu hang, while
806 * those smc fw version above 22.15 doesn't have this flaw, so we force
807 * vpost executed for smc version below 22.15
Monk Liubec86372016-09-14 19:38:08 +0800808 */
809 if (adev->asic_type == CHIP_FIJI) {
810 int err;
811 uint32_t fw_ver;
812 err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
813 /* force vPost if error occured */
814 if (err)
815 return true;
816
817 fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
Monk Liu1da2c322016-11-11 11:24:29 +0800818 if (fw_ver < 0x00160e00)
819 return true;
Monk Liubec86372016-09-14 19:38:08 +0800820 }
Monk Liubec86372016-09-14 19:38:08 +0800821 }
pding91fe77e2017-10-19 09:38:39 +0800822
823 if (adev->has_hw_reset) {
824 adev->has_hw_reset = false;
825 return true;
826 }
827
828 /* bios scratch used on CIK+ */
829 if (adev->asic_type >= CHIP_BONAIRE)
830 return amdgpu_atombios_scratch_need_asic_init(adev);
831
832 /* check MEM_SIZE for older asics */
833 reg = amdgpu_asic_get_config_memsize(adev);
834
835 if ((reg != 0) && (reg != 0xffffffff))
836 return false;
837
838 return true;
Monk Liubec86372016-09-14 19:38:08 +0800839}
840
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400841/* if we get transitioned to only one device, take VGA back */
842/**
Alex Deucher06ec9072017-12-14 15:02:39 -0500843 * amdgpu_device_vga_set_decode - enable/disable vga decode
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400844 *
845 * @cookie: amdgpu_device pointer
846 * @state: enable/disable vga decode
847 *
848 * Enable/disable vga decode (all asics).
849 * Returns VGA resource flags.
850 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500851static unsigned int amdgpu_device_vga_set_decode(void *cookie, bool state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400852{
853 struct amdgpu_device *adev = cookie;
854 amdgpu_asic_set_vga_state(adev, state);
855 if (state)
856 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
857 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
858 else
859 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
860}
861
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500862/**
863 * amdgpu_device_check_block_size - validate the vm block size
864 *
865 * @adev: amdgpu_device pointer
866 *
867 * Validates the vm block size specified via module parameter.
868 * The vm block size defines number of bits in page table versus page directory,
869 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
870 * page table and the remaining bits are in the page directory.
871 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500872static void amdgpu_device_check_block_size(struct amdgpu_device *adev)
Chunming Zhoua1adf8b2017-03-27 11:36:57 +0800873{
874 /* defines number of bits in page table versus page directory,
875 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
876 * page table and the remaining bits are in the page directory */
Junwei Zhangbab4fee2017-04-05 13:54:56 +0800877 if (amdgpu_vm_block_size == -1)
878 return;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +0800879
Junwei Zhangbab4fee2017-04-05 13:54:56 +0800880 if (amdgpu_vm_block_size < 9) {
Chunming Zhoua1adf8b2017-03-27 11:36:57 +0800881 dev_warn(adev->dev, "VM page table size (%d) too small\n",
882 amdgpu_vm_block_size);
Christian König97489122017-11-27 16:22:05 +0100883 amdgpu_vm_block_size = -1;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +0800884 }
Chunming Zhoua1adf8b2017-03-27 11:36:57 +0800885}
886
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500887/**
888 * amdgpu_device_check_vm_size - validate the vm size
889 *
890 * @adev: amdgpu_device pointer
891 *
892 * Validates the vm size in GB specified via module parameter.
893 * The VM size is the size of the GPU virtual memory space in GB.
894 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500895static void amdgpu_device_check_vm_size(struct amdgpu_device *adev)
Zhang, Jerry83ca1452017-03-29 16:08:31 +0800896{
Alex Deucher64dab072017-06-15 18:20:09 -0400897 /* no need to check the default value */
898 if (amdgpu_vm_size == -1)
899 return;
900
Zhang, Jerry83ca1452017-03-29 16:08:31 +0800901 if (amdgpu_vm_size < 1) {
902 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
903 amdgpu_vm_size);
Christian Königf3368122017-11-23 12:57:18 +0100904 amdgpu_vm_size = -1;
Zhang, Jerry83ca1452017-03-29 16:08:31 +0800905 }
Zhang, Jerry83ca1452017-03-29 16:08:31 +0800906}
907
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400908/**
Alex Deucher06ec9072017-12-14 15:02:39 -0500909 * amdgpu_device_check_arguments - validate module params
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400910 *
911 * @adev: amdgpu_device pointer
912 *
913 * Validates certain module parameters and updates
914 * the associated values used by the driver (all asics).
915 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500916static void amdgpu_device_check_arguments(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400917{
Chunming Zhou5b011232015-12-10 17:34:33 +0800918 if (amdgpu_sched_jobs < 4) {
919 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
920 amdgpu_sched_jobs);
921 amdgpu_sched_jobs = 4;
Alex Deucher76117502017-06-21 12:31:41 -0400922 } else if (!is_power_of_2(amdgpu_sched_jobs)){
Chunming Zhou5b011232015-12-10 17:34:33 +0800923 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
924 amdgpu_sched_jobs);
925 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
926 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400927
Alex Deucher83e74db2017-08-21 11:58:25 -0400928 if (amdgpu_gart_size != -1 && amdgpu_gart_size < 32) {
Christian Königf9321cc2017-07-07 13:44:05 +0200929 /* gart size must be greater or equal to 32M */
930 dev_warn(adev->dev, "gart size (%d) too small\n",
931 amdgpu_gart_size);
Alex Deucher83e74db2017-08-21 11:58:25 -0400932 amdgpu_gart_size = -1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400933 }
934
Christian König36d38372017-07-07 13:17:45 +0200935 if (amdgpu_gtt_size != -1 && amdgpu_gtt_size < 32) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400936 /* gtt size must be greater or equal to 32M */
Christian König36d38372017-07-07 13:17:45 +0200937 dev_warn(adev->dev, "gtt size (%d) too small\n",
938 amdgpu_gtt_size);
939 amdgpu_gtt_size = -1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400940 }
941
Roger Hed07f14b2017-08-15 16:05:59 +0800942 /* valid range is between 4 and 9 inclusive */
943 if (amdgpu_vm_fragment_size != -1 &&
944 (amdgpu_vm_fragment_size > 9 || amdgpu_vm_fragment_size < 4)) {
945 dev_warn(adev->dev, "valid range is between 4 and 9\n");
946 amdgpu_vm_fragment_size = -1;
947 }
948
Alex Deucher06ec9072017-12-14 15:02:39 -0500949 amdgpu_device_check_vm_size(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400950
Alex Deucher06ec9072017-12-14 15:02:39 -0500951 amdgpu_device_check_block_size(adev);
Christian König6a7f76e2016-08-24 15:51:49 +0200952
jimqu526bae32016-11-07 09:53:10 +0800953 if (amdgpu_vram_page_split != -1 && (amdgpu_vram_page_split < 16 ||
Alex Deucher76117502017-06-21 12:31:41 -0400954 !is_power_of_2(amdgpu_vram_page_split))) {
Christian König6a7f76e2016-08-24 15:51:49 +0200955 dev_warn(adev->dev, "invalid VRAM page split (%d)\n",
956 amdgpu_vram_page_split);
957 amdgpu_vram_page_split = 1024;
958 }
Andrey Grodzovsky88546952017-12-13 14:36:53 -0500959
960 if (amdgpu_lockup_timeout == 0) {
961 dev_warn(adev->dev, "lockup_timeout msut be > 0, adjusting to 10000\n");
962 amdgpu_lockup_timeout = 10000;
963 }
Alex Deucher19aede72018-03-09 15:06:35 -0500964
965 adev->firmware.load_type = amdgpu_ucode_get_load_type(adev, amdgpu_fw_load_type);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400966}
967
968/**
969 * amdgpu_switcheroo_set_state - set switcheroo state
970 *
971 * @pdev: pci dev pointer
Lukas Wunner16944672015-09-05 11:17:35 +0200972 * @state: vga_switcheroo state
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400973 *
974 * Callback for the switcheroo driver. Suspends or resumes the
975 * the asics before or after it is powered up using ACPI methods.
976 */
977static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
978{
979 struct drm_device *dev = pci_get_drvdata(pdev);
980
981 if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
982 return;
983
984 if (state == VGA_SWITCHEROO_ON) {
Joe Perches7ca85292017-02-28 04:55:52 -0800985 pr_info("amdgpu: switched on\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400986 /* don't suspend or resume card normally */
987 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
988
Alex Deucher810ddc32016-08-23 13:25:49 -0400989 amdgpu_device_resume(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400990
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400991 dev->switch_power_state = DRM_SWITCH_POWER_ON;
992 drm_kms_helper_poll_enable(dev);
993 } else {
Joe Perches7ca85292017-02-28 04:55:52 -0800994 pr_info("amdgpu: switched off\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400995 drm_kms_helper_poll_disable(dev);
996 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Alex Deucher810ddc32016-08-23 13:25:49 -0400997 amdgpu_device_suspend(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400998 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
999 }
1000}
1001
1002/**
1003 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1004 *
1005 * @pdev: pci dev pointer
1006 *
1007 * Callback for the switcheroo driver. Check of the switcheroo
1008 * state can be changed.
1009 * Returns true if the state can be changed, false if not.
1010 */
1011static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1012{
1013 struct drm_device *dev = pci_get_drvdata(pdev);
1014
1015 /*
1016 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1017 * locking inversion with the driver load path. And the access here is
1018 * completely racy anyway. So don't bother with locking for now.
1019 */
1020 return dev->open_count == 0;
1021}
1022
1023static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1024 .set_gpu_state = amdgpu_switcheroo_set_state,
1025 .reprobe = NULL,
1026 .can_switch = amdgpu_switcheroo_can_switch,
1027};
1028
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001029/**
1030 * amdgpu_device_ip_set_clockgating_state - set the CG state
1031 *
1032 * @adev: amdgpu_device pointer
1033 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1034 * @state: clockgating state (gate or ungate)
1035 *
1036 * Sets the requested clockgating state for all instances of
1037 * the hardware IP specified.
1038 * Returns the error code from the last instance.
1039 */
Alex Deucher2990a1f2017-12-15 16:18:00 -05001040int amdgpu_device_ip_set_clockgating_state(struct amdgpu_device *adev,
1041 enum amd_ip_block_type block_type,
1042 enum amd_clockgating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001043{
1044 int i, r = 0;
1045
1046 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001047 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001048 continue;
Rex Zhuc7228652017-02-22 15:33:46 +08001049 if (adev->ip_blocks[i].version->type != block_type)
1050 continue;
1051 if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
1052 continue;
1053 r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
1054 (void *)adev, state);
1055 if (r)
1056 DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
1057 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001058 }
1059 return r;
1060}
1061
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001062/**
1063 * amdgpu_device_ip_set_powergating_state - set the PG state
1064 *
1065 * @adev: amdgpu_device pointer
1066 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1067 * @state: powergating state (gate or ungate)
1068 *
1069 * Sets the requested powergating state for all instances of
1070 * the hardware IP specified.
1071 * Returns the error code from the last instance.
1072 */
Alex Deucher2990a1f2017-12-15 16:18:00 -05001073int amdgpu_device_ip_set_powergating_state(struct amdgpu_device *adev,
1074 enum amd_ip_block_type block_type,
1075 enum amd_powergating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001076{
1077 int i, r = 0;
1078
1079 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001080 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001081 continue;
Rex Zhuc7228652017-02-22 15:33:46 +08001082 if (adev->ip_blocks[i].version->type != block_type)
1083 continue;
1084 if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
1085 continue;
1086 r = adev->ip_blocks[i].version->funcs->set_powergating_state(
1087 (void *)adev, state);
1088 if (r)
1089 DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
1090 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001091 }
1092 return r;
1093}
1094
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001095/**
1096 * amdgpu_device_ip_get_clockgating_state - get the CG state
1097 *
1098 * @adev: amdgpu_device pointer
1099 * @flags: clockgating feature flags
1100 *
1101 * Walks the list of IPs on the device and updates the clockgating
1102 * flags for each IP.
1103 * Updates @flags with the feature flags for each hardware IP where
1104 * clockgating is enabled.
1105 */
Alex Deucher2990a1f2017-12-15 16:18:00 -05001106void amdgpu_device_ip_get_clockgating_state(struct amdgpu_device *adev,
1107 u32 *flags)
Huang Rui6cb2d4e2017-01-05 18:44:41 +08001108{
1109 int i;
1110
1111 for (i = 0; i < adev->num_ip_blocks; i++) {
1112 if (!adev->ip_blocks[i].status.valid)
1113 continue;
1114 if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
1115 adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
1116 }
1117}
1118
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001119/**
1120 * amdgpu_device_ip_wait_for_idle - wait for idle
1121 *
1122 * @adev: amdgpu_device pointer
1123 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1124 *
1125 * Waits for the request hardware IP to be idle.
1126 * Returns 0 for success or a negative error code on failure.
1127 */
Alex Deucher2990a1f2017-12-15 16:18:00 -05001128int amdgpu_device_ip_wait_for_idle(struct amdgpu_device *adev,
1129 enum amd_ip_block_type block_type)
Alex Deucher5dbbb602016-06-23 11:41:04 -04001130{
1131 int i, r;
1132
1133 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001134 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001135 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001136 if (adev->ip_blocks[i].version->type == block_type) {
1137 r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -04001138 if (r)
1139 return r;
1140 break;
1141 }
1142 }
1143 return 0;
1144
1145}
1146
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001147/**
1148 * amdgpu_device_ip_is_idle - is the hardware IP idle
1149 *
1150 * @adev: amdgpu_device pointer
1151 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1152 *
1153 * Check if the hardware IP is idle or not.
1154 * Returns true if it the IP is idle, false if not.
1155 */
Alex Deucher2990a1f2017-12-15 16:18:00 -05001156bool amdgpu_device_ip_is_idle(struct amdgpu_device *adev,
1157 enum amd_ip_block_type block_type)
Alex Deucher5dbbb602016-06-23 11:41:04 -04001158{
1159 int i;
1160
1161 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001162 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001163 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001164 if (adev->ip_blocks[i].version->type == block_type)
1165 return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -04001166 }
1167 return true;
1168
1169}
1170
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001171/**
1172 * amdgpu_device_ip_get_ip_block - get a hw IP pointer
1173 *
1174 * @adev: amdgpu_device pointer
1175 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1176 *
1177 * Returns a pointer to the hardware IP block structure
1178 * if it exists for the asic, otherwise NULL.
1179 */
Alex Deucher2990a1f2017-12-15 16:18:00 -05001180struct amdgpu_ip_block *
1181amdgpu_device_ip_get_ip_block(struct amdgpu_device *adev,
1182 enum amd_ip_block_type type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001183{
1184 int i;
1185
1186 for (i = 0; i < adev->num_ip_blocks; i++)
Alex Deuchera1255102016-10-13 17:41:13 -04001187 if (adev->ip_blocks[i].version->type == type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001188 return &adev->ip_blocks[i];
1189
1190 return NULL;
1191}
1192
1193/**
Alex Deucher2990a1f2017-12-15 16:18:00 -05001194 * amdgpu_device_ip_block_version_cmp
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001195 *
1196 * @adev: amdgpu_device pointer
yanyang15fc3aee2015-05-22 14:39:35 -04001197 * @type: enum amd_ip_block_type
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001198 * @major: major version
1199 * @minor: minor version
1200 *
1201 * return 0 if equal or greater
1202 * return 1 if smaller or the ip_block doesn't exist
1203 */
Alex Deucher2990a1f2017-12-15 16:18:00 -05001204int amdgpu_device_ip_block_version_cmp(struct amdgpu_device *adev,
1205 enum amd_ip_block_type type,
1206 u32 major, u32 minor)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001207{
Alex Deucher2990a1f2017-12-15 16:18:00 -05001208 struct amdgpu_ip_block *ip_block = amdgpu_device_ip_get_ip_block(adev, type);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001209
Alex Deuchera1255102016-10-13 17:41:13 -04001210 if (ip_block && ((ip_block->version->major > major) ||
1211 ((ip_block->version->major == major) &&
1212 (ip_block->version->minor >= minor))))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001213 return 0;
1214
1215 return 1;
1216}
1217
Alex Deuchera1255102016-10-13 17:41:13 -04001218/**
Alex Deucher2990a1f2017-12-15 16:18:00 -05001219 * amdgpu_device_ip_block_add
Alex Deuchera1255102016-10-13 17:41:13 -04001220 *
1221 * @adev: amdgpu_device pointer
1222 * @ip_block_version: pointer to the IP to add
1223 *
1224 * Adds the IP block driver information to the collection of IPs
1225 * on the asic.
1226 */
Alex Deucher2990a1f2017-12-15 16:18:00 -05001227int amdgpu_device_ip_block_add(struct amdgpu_device *adev,
1228 const struct amdgpu_ip_block_version *ip_block_version)
Alex Deuchera1255102016-10-13 17:41:13 -04001229{
1230 if (!ip_block_version)
1231 return -EINVAL;
1232
Shaoyun Liue966a722018-02-01 16:45:26 -05001233 DRM_INFO("add ip block number %d <%s>\n", adev->num_ip_blocks,
Huang Ruia0bae352017-05-03 09:52:06 +08001234 ip_block_version->funcs->name);
1235
Alex Deuchera1255102016-10-13 17:41:13 -04001236 adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
1237
1238 return 0;
1239}
1240
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001241/**
1242 * amdgpu_device_enable_virtual_display - enable virtual display feature
1243 *
1244 * @adev: amdgpu_device pointer
1245 *
1246 * Enabled the virtual display feature if the user has enabled it via
1247 * the module parameter virtual_display. This feature provides a virtual
1248 * display hardware on headless boards or in virtualized environments.
1249 * This function parses and validates the configuration string specified by
1250 * the user and configues the virtual display configuration (number of
1251 * virtual connectors, crtcs, etc.) specified.
1252 */
Alex Deucher483ef982016-09-30 12:43:04 -04001253static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
Emily Deng9accf2f2016-08-10 16:01:25 +08001254{
1255 adev->enable_virtual_display = false;
1256
1257 if (amdgpu_virtual_display) {
1258 struct drm_device *ddev = adev->ddev;
1259 const char *pci_address_name = pci_name(ddev->pdev);
Emily Deng0f663562016-09-30 13:02:18 -04001260 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
Emily Deng9accf2f2016-08-10 16:01:25 +08001261
1262 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1263 pciaddstr_tmp = pciaddstr;
Emily Deng0f663562016-09-30 13:02:18 -04001264 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1265 pciaddname = strsep(&pciaddname_tmp, ",");
Yintian Tao967de2a2017-01-22 15:16:51 +08001266 if (!strcmp("all", pciaddname)
1267 || !strcmp(pci_address_name, pciaddname)) {
Emily Deng0f663562016-09-30 13:02:18 -04001268 long num_crtc;
1269 int res = -1;
1270
Emily Deng9accf2f2016-08-10 16:01:25 +08001271 adev->enable_virtual_display = true;
Emily Deng0f663562016-09-30 13:02:18 -04001272
1273 if (pciaddname_tmp)
1274 res = kstrtol(pciaddname_tmp, 10,
1275 &num_crtc);
1276
1277 if (!res) {
1278 if (num_crtc < 1)
1279 num_crtc = 1;
1280 if (num_crtc > 6)
1281 num_crtc = 6;
1282 adev->mode_info.num_crtc = num_crtc;
1283 } else {
1284 adev->mode_info.num_crtc = 1;
1285 }
Emily Deng9accf2f2016-08-10 16:01:25 +08001286 break;
1287 }
1288 }
1289
Emily Deng0f663562016-09-30 13:02:18 -04001290 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
1291 amdgpu_virtual_display, pci_address_name,
1292 adev->enable_virtual_display, adev->mode_info.num_crtc);
Emily Deng9accf2f2016-08-10 16:01:25 +08001293
1294 kfree(pciaddstr);
1295 }
1296}
1297
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001298/**
1299 * amdgpu_device_parse_gpu_info_fw - parse gpu info firmware
1300 *
1301 * @adev: amdgpu_device pointer
1302 *
1303 * Parses the asic configuration parameters specified in the gpu info
1304 * firmware and makes them availale to the driver for use in configuring
1305 * the asic.
1306 * Returns 0 on success, -EINVAL on failure.
1307 */
Alex Deuchere2a75f82017-04-27 16:58:01 -04001308static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev)
1309{
Alex Deuchere2a75f82017-04-27 16:58:01 -04001310 const char *chip_name;
1311 char fw_name[30];
1312 int err;
1313 const struct gpu_info_firmware_header_v1_0 *hdr;
1314
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001315 adev->firmware.gpu_info_fw = NULL;
1316
Alex Deuchere2a75f82017-04-27 16:58:01 -04001317 switch (adev->asic_type) {
1318 case CHIP_TOPAZ:
1319 case CHIP_TONGA:
1320 case CHIP_FIJI:
1321 case CHIP_POLARIS11:
1322 case CHIP_POLARIS10:
1323 case CHIP_POLARIS12:
1324 case CHIP_CARRIZO:
1325 case CHIP_STONEY:
1326#ifdef CONFIG_DRM_AMDGPU_SI
1327 case CHIP_VERDE:
1328 case CHIP_TAHITI:
1329 case CHIP_PITCAIRN:
1330 case CHIP_OLAND:
1331 case CHIP_HAINAN:
1332#endif
1333#ifdef CONFIG_DRM_AMDGPU_CIK
1334 case CHIP_BONAIRE:
1335 case CHIP_HAWAII:
1336 case CHIP_KAVERI:
1337 case CHIP_KABINI:
1338 case CHIP_MULLINS:
1339#endif
1340 default:
1341 return 0;
1342 case CHIP_VEGA10:
1343 chip_name = "vega10";
1344 break;
Alex Deucher2d2e5e72017-05-09 12:27:35 -04001345 case CHIP_RAVEN:
1346 chip_name = "raven";
1347 break;
Alex Deuchere2a75f82017-04-27 16:58:01 -04001348 }
1349
1350 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name);
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001351 err = request_firmware(&adev->firmware.gpu_info_fw, fw_name, adev->dev);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001352 if (err) {
1353 dev_err(adev->dev,
1354 "Failed to load gpu_info firmware \"%s\"\n",
1355 fw_name);
1356 goto out;
1357 }
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001358 err = amdgpu_ucode_validate(adev->firmware.gpu_info_fw);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001359 if (err) {
1360 dev_err(adev->dev,
1361 "Failed to validate gpu_info firmware \"%s\"\n",
1362 fw_name);
1363 goto out;
1364 }
1365
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001366 hdr = (const struct gpu_info_firmware_header_v1_0 *)adev->firmware.gpu_info_fw->data;
Alex Deuchere2a75f82017-04-27 16:58:01 -04001367 amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
1368
1369 switch (hdr->version_major) {
1370 case 1:
1371 {
1372 const struct gpu_info_firmware_v1_0 *gpu_info_fw =
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001373 (const struct gpu_info_firmware_v1_0 *)(adev->firmware.gpu_info_fw->data +
Alex Deuchere2a75f82017-04-27 16:58:01 -04001374 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1375
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001376 adev->gfx.config.max_shader_engines = le32_to_cpu(gpu_info_fw->gc_num_se);
1377 adev->gfx.config.max_cu_per_sh = le32_to_cpu(gpu_info_fw->gc_num_cu_per_sh);
1378 adev->gfx.config.max_sh_per_se = le32_to_cpu(gpu_info_fw->gc_num_sh_per_se);
1379 adev->gfx.config.max_backends_per_se = le32_to_cpu(gpu_info_fw->gc_num_rb_per_se);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001380 adev->gfx.config.max_texture_channel_caches =
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001381 le32_to_cpu(gpu_info_fw->gc_num_tccs);
1382 adev->gfx.config.max_gprs = le32_to_cpu(gpu_info_fw->gc_num_gprs);
1383 adev->gfx.config.max_gs_threads = le32_to_cpu(gpu_info_fw->gc_num_max_gs_thds);
1384 adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gpu_info_fw->gc_gs_table_depth);
1385 adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gpu_info_fw->gc_gsprim_buff_depth);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001386 adev->gfx.config.double_offchip_lds_buf =
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001387 le32_to_cpu(gpu_info_fw->gc_double_offchip_lds_buffer);
1388 adev->gfx.cu_info.wave_front_size = le32_to_cpu(gpu_info_fw->gc_wave_size);
Hawking Zhang51fd0372017-06-09 22:30:52 +08001389 adev->gfx.cu_info.max_waves_per_simd =
1390 le32_to_cpu(gpu_info_fw->gc_max_waves_per_simd);
1391 adev->gfx.cu_info.max_scratch_slots_per_cu =
1392 le32_to_cpu(gpu_info_fw->gc_max_scratch_slots_per_cu);
1393 adev->gfx.cu_info.lds_size = le32_to_cpu(gpu_info_fw->gc_lds_size);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001394 break;
1395 }
1396 default:
1397 dev_err(adev->dev,
1398 "Unsupported gpu_info table %d\n", hdr->header.ucode_version);
1399 err = -EINVAL;
1400 goto out;
1401 }
1402out:
Alex Deuchere2a75f82017-04-27 16:58:01 -04001403 return err;
1404}
1405
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001406/**
1407 * amdgpu_device_ip_early_init - run early init for hardware IPs
1408 *
1409 * @adev: amdgpu_device pointer
1410 *
1411 * Early initialization pass for hardware IPs. The hardware IPs that make
1412 * up each asic are discovered each IP's early_init callback is run. This
1413 * is the first stage in initializing the asic.
1414 * Returns 0 on success, negative error code on failure.
1415 */
Alex Deucher06ec9072017-12-14 15:02:39 -05001416static int amdgpu_device_ip_early_init(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001417{
Alex Deucheraaa36a92015-04-20 17:31:14 -04001418 int i, r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001419
Alex Deucher483ef982016-09-30 12:43:04 -04001420 amdgpu_device_enable_virtual_display(adev);
Emily Denga6be7572016-08-08 11:37:50 +08001421
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001422 switch (adev->asic_type) {
Alex Deucheraaa36a92015-04-20 17:31:14 -04001423 case CHIP_TOPAZ:
1424 case CHIP_TONGA:
David Zhang48299f92015-07-08 01:05:16 +08001425 case CHIP_FIJI:
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001426 case CHIP_POLARIS11:
1427 case CHIP_POLARIS10:
Junwei Zhangc4642a42016-12-14 15:32:28 -05001428 case CHIP_POLARIS12:
Alex Deucheraaa36a92015-04-20 17:31:14 -04001429 case CHIP_CARRIZO:
Samuel Li39bb0c92015-10-08 16:31:43 -04001430 case CHIP_STONEY:
1431 if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
Alex Deucheraaa36a92015-04-20 17:31:14 -04001432 adev->family = AMDGPU_FAMILY_CZ;
1433 else
1434 adev->family = AMDGPU_FAMILY_VI;
1435
1436 r = vi_set_ip_blocks(adev);
1437 if (r)
1438 return r;
1439 break;
Ken Wang33f34802016-01-21 17:29:41 +08001440#ifdef CONFIG_DRM_AMDGPU_SI
1441 case CHIP_VERDE:
1442 case CHIP_TAHITI:
1443 case CHIP_PITCAIRN:
1444 case CHIP_OLAND:
1445 case CHIP_HAINAN:
Ken Wang295d0da2016-05-24 21:02:53 +08001446 adev->family = AMDGPU_FAMILY_SI;
Ken Wang33f34802016-01-21 17:29:41 +08001447 r = si_set_ip_blocks(adev);
1448 if (r)
1449 return r;
1450 break;
1451#endif
Alex Deuchera2e73f52015-04-20 17:09:27 -04001452#ifdef CONFIG_DRM_AMDGPU_CIK
1453 case CHIP_BONAIRE:
1454 case CHIP_HAWAII:
1455 case CHIP_KAVERI:
1456 case CHIP_KABINI:
1457 case CHIP_MULLINS:
1458 if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
1459 adev->family = AMDGPU_FAMILY_CI;
1460 else
1461 adev->family = AMDGPU_FAMILY_KV;
1462
1463 r = cik_set_ip_blocks(adev);
1464 if (r)
1465 return r;
1466 break;
1467#endif
Chunming Zhou2ca8a5d2016-12-07 17:31:19 +08001468 case CHIP_VEGA10:
1469 case CHIP_RAVEN:
1470 if (adev->asic_type == CHIP_RAVEN)
1471 adev->family = AMDGPU_FAMILY_RV;
1472 else
1473 adev->family = AMDGPU_FAMILY_AI;
Ken Wang460826e2017-03-06 14:53:16 -05001474
1475 r = soc15_set_ip_blocks(adev);
1476 if (r)
1477 return r;
1478 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001479 default:
1480 /* FIXME: not supported yet */
1481 return -EINVAL;
1482 }
1483
Alex Deuchere2a75f82017-04-27 16:58:01 -04001484 r = amdgpu_device_parse_gpu_info_fw(adev);
1485 if (r)
1486 return r;
1487
pding18847342017-11-06 10:21:26 +08001488 amdgpu_amdkfd_device_probe(adev);
1489
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001490 if (amdgpu_sriov_vf(adev)) {
1491 r = amdgpu_virt_request_full_gpu(adev, true);
1492 if (r)
pding5ffa61c2017-10-30 14:07:24 +08001493 return -EAGAIN;
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001494 }
1495
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001496 for (i = 0; i < adev->num_ip_blocks; i++) {
1497 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
Huang Ruied8cf002017-05-03 09:40:17 +08001498 DRM_ERROR("disabled ip block: %d <%s>\n",
1499 i, adev->ip_blocks[i].version->funcs->name);
Alex Deuchera1255102016-10-13 17:41:13 -04001500 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001501 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001502 if (adev->ip_blocks[i].version->funcs->early_init) {
1503 r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001504 if (r == -ENOENT) {
Alex Deuchera1255102016-10-13 17:41:13 -04001505 adev->ip_blocks[i].status.valid = false;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001506 } else if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001507 DRM_ERROR("early_init of IP block <%s> failed %d\n",
1508 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001509 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001510 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001511 adev->ip_blocks[i].status.valid = true;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001512 }
Alex Deucher974e6b62015-07-10 13:59:44 -04001513 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001514 adev->ip_blocks[i].status.valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001515 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001516 }
1517 }
1518
Nicolai Hähnle395d1fb2016-06-02 12:32:07 +02001519 adev->cg_flags &= amdgpu_cg_mask;
1520 adev->pg_flags &= amdgpu_pg_mask;
1521
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001522 return 0;
1523}
1524
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001525/**
1526 * amdgpu_device_ip_init - run init for hardware IPs
1527 *
1528 * @adev: amdgpu_device pointer
1529 *
1530 * Main initialization pass for hardware IPs. The list of all the hardware
1531 * IPs that make up the asic is walked and the sw_init and hw_init callbacks
1532 * are run. sw_init initializes the software state associated with each IP
1533 * and hw_init initializes the hardware associated with each IP.
1534 * Returns 0 on success, negative error code on failure.
1535 */
Alex Deucher06ec9072017-12-14 15:02:39 -05001536static int amdgpu_device_ip_init(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001537{
1538 int i, r;
1539
1540 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001541 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001542 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001543 r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001544 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001545 DRM_ERROR("sw_init of IP block <%s> failed %d\n",
1546 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001547 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001548 }
Alex Deuchera1255102016-10-13 17:41:13 -04001549 adev->ip_blocks[i].status.sw = true;
Shaoyun Liubfca0282018-02-01 17:37:50 -05001550
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001551 /* need to do gmc hw init early so we can allocate gpu mem */
Alex Deuchera1255102016-10-13 17:41:13 -04001552 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucher06ec9072017-12-14 15:02:39 -05001553 r = amdgpu_device_vram_scratch_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001554 if (r) {
1555 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001556 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001557 }
Alex Deuchera1255102016-10-13 17:41:13 -04001558 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001559 if (r) {
1560 DRM_ERROR("hw_init %d failed %d\n", i, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001561 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001562 }
Alex Deucher06ec9072017-12-14 15:02:39 -05001563 r = amdgpu_device_wb_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001564 if (r) {
Alex Deucher06ec9072017-12-14 15:02:39 -05001565 DRM_ERROR("amdgpu_device_wb_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001566 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001567 }
Alex Deuchera1255102016-10-13 17:41:13 -04001568 adev->ip_blocks[i].status.hw = true;
Monk Liu24936642017-01-09 15:54:32 +08001569
1570 /* right after GMC hw init, we create CSA */
1571 if (amdgpu_sriov_vf(adev)) {
1572 r = amdgpu_allocate_static_csa(adev);
1573 if (r) {
1574 DRM_ERROR("allocate CSA failed %d\n", r);
1575 return r;
1576 }
1577 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001578 }
1579 }
1580
1581 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001582 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001583 continue;
Shaoyun Liubfca0282018-02-01 17:37:50 -05001584 if (adev->ip_blocks[i].status.hw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001585 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001586 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001587 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001588 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
1589 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001590 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001591 }
Alex Deuchera1255102016-10-13 17:41:13 -04001592 adev->ip_blocks[i].status.hw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001593 }
1594
pding18847342017-11-06 10:21:26 +08001595 amdgpu_amdkfd_device_init(adev);
pdingc6332b92017-11-06 11:21:55 +08001596
1597 if (amdgpu_sriov_vf(adev))
1598 amdgpu_virt_release_full_gpu(adev, true);
1599
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001600 return 0;
1601}
1602
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001603/**
1604 * amdgpu_device_fill_reset_magic - writes reset magic to gart pointer
1605 *
1606 * @adev: amdgpu_device pointer
1607 *
1608 * Writes a reset magic value to the gart pointer in VRAM. The driver calls
1609 * this function before a GPU reset. If the value is retained after a
1610 * GPU reset, VRAM has not been lost. Some GPU resets may destry VRAM contents.
1611 */
Alex Deucher06ec9072017-12-14 15:02:39 -05001612static void amdgpu_device_fill_reset_magic(struct amdgpu_device *adev)
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08001613{
1614 memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM);
1615}
1616
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001617/**
1618 * amdgpu_device_check_vram_lost - check if vram is valid
1619 *
1620 * @adev: amdgpu_device pointer
1621 *
1622 * Checks the reset magic value written to the gart pointer in VRAM.
1623 * The driver calls this after a GPU reset to see if the contents of
1624 * VRAM is lost or now.
1625 * returns true if vram is lost, false if not.
1626 */
Alex Deucher06ec9072017-12-14 15:02:39 -05001627static bool amdgpu_device_check_vram_lost(struct amdgpu_device *adev)
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08001628{
1629 return !!memcmp(adev->gart.ptr, adev->reset_magic,
1630 AMDGPU_RESET_MAGIC_NUM);
1631}
1632
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001633/**
1634 * amdgpu_device_ip_late_set_cg_state - late init for clockgating
1635 *
1636 * @adev: amdgpu_device pointer
1637 *
1638 * Late initialization pass enabling clockgating for hardware IPs.
1639 * The list of all the hardware IPs that make up the asic is walked and the
1640 * set_clockgating_state callbacks are run. This stage is run late
1641 * in the init process.
1642 * Returns 0 on success, negative error code on failure.
1643 */
Alex Deucher06ec9072017-12-14 15:02:39 -05001644static int amdgpu_device_ip_late_set_cg_state(struct amdgpu_device *adev)
Shirish S2dc80b02017-05-25 10:05:25 +05301645{
1646 int i = 0, r;
1647
Shaoyun Liu4a2ba392018-02-05 16:41:33 -05001648 if (amdgpu_emu_mode == 1)
1649 return 0;
1650
Shirish S2dc80b02017-05-25 10:05:25 +05301651 for (i = 0; i < adev->num_ip_blocks; i++) {
1652 if (!adev->ip_blocks[i].status.valid)
1653 continue;
1654 /* skip CG for VCE/UVD, it's handled specially */
1655 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
Rex Zhu57716322018-03-12 19:50:38 +08001656 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
1657 adev->ip_blocks[i].version->funcs->set_clockgating_state) {
Shirish S2dc80b02017-05-25 10:05:25 +05301658 /* enable clockgating to save power */
1659 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1660 AMD_CG_STATE_GATE);
1661 if (r) {
1662 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
1663 adev->ip_blocks[i].version->funcs->name, r);
1664 return r;
1665 }
1666 }
1667 }
1668 return 0;
1669}
1670
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001671/**
1672 * amdgpu_device_ip_late_init - run late init for hardware IPs
1673 *
1674 * @adev: amdgpu_device pointer
1675 *
1676 * Late initialization pass for hardware IPs. The list of all the hardware
1677 * IPs that make up the asic is walked and the late_init callbacks are run.
1678 * late_init covers any special initialization that an IP requires
1679 * after all of the have been initialized or something that needs to happen
1680 * late in the init process.
1681 * Returns 0 on success, negative error code on failure.
1682 */
Alex Deucher06ec9072017-12-14 15:02:39 -05001683static int amdgpu_device_ip_late_init(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001684{
1685 int i = 0, r;
1686
1687 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001688 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001689 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001690 if (adev->ip_blocks[i].version->funcs->late_init) {
1691 r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001692 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001693 DRM_ERROR("late_init of IP block <%s> failed %d\n",
1694 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001695 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001696 }
Alex Deuchera1255102016-10-13 17:41:13 -04001697 adev->ip_blocks[i].status.late_initialized = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001698 }
1699 }
1700
Shirish S2dc80b02017-05-25 10:05:25 +05301701 mod_delayed_work(system_wq, &adev->late_init_work,
1702 msecs_to_jiffies(AMDGPU_RESUME_MS));
1703
Alex Deucher06ec9072017-12-14 15:02:39 -05001704 amdgpu_device_fill_reset_magic(adev);
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08001705
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001706 return 0;
1707}
1708
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001709/**
1710 * amdgpu_device_ip_fini - run fini for hardware IPs
1711 *
1712 * @adev: amdgpu_device pointer
1713 *
1714 * Main teardown pass for hardware IPs. The list of all the hardware
1715 * IPs that make up the asic is walked and the hw_fini and sw_fini callbacks
1716 * are run. hw_fini tears down the hardware associated with each IP
1717 * and sw_fini tears down any software state associated with each IP.
1718 * Returns 0 on success, negative error code on failure.
1719 */
Alex Deucher06ec9072017-12-14 15:02:39 -05001720static int amdgpu_device_ip_fini(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001721{
1722 int i, r;
1723
pding18847342017-11-06 10:21:26 +08001724 amdgpu_amdkfd_device_fini(adev);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001725 /* need to disable SMC first */
1726 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001727 if (!adev->ip_blocks[i].status.hw)
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001728 continue;
Rex Zhu57716322018-03-12 19:50:38 +08001729 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC &&
1730 adev->ip_blocks[i].version->funcs->set_clockgating_state) {
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001731 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
Alex Deuchera1255102016-10-13 17:41:13 -04001732 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1733 AMD_CG_STATE_UNGATE);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001734 if (r) {
1735 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001736 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001737 return r;
1738 }
Alex Deuchera1255102016-10-13 17:41:13 -04001739 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001740 /* XXX handle errors */
1741 if (r) {
1742 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001743 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001744 }
Alex Deuchera1255102016-10-13 17:41:13 -04001745 adev->ip_blocks[i].status.hw = false;
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001746 break;
1747 }
1748 }
1749
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001750 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001751 if (!adev->ip_blocks[i].status.hw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001752 continue;
Rex Zhu8201a672016-11-24 21:44:44 +08001753
1754 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
Rex Zhu81ce8be2018-03-20 16:28:56 +08001755 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
1756 adev->ip_blocks[i].version->funcs->set_clockgating_state) {
Rex Zhu8201a672016-11-24 21:44:44 +08001757 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
1758 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1759 AMD_CG_STATE_UNGATE);
1760 if (r) {
1761 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1762 adev->ip_blocks[i].version->funcs->name, r);
1763 return r;
1764 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001765 }
Rex Zhu8201a672016-11-24 21:44:44 +08001766
Alex Deuchera1255102016-10-13 17:41:13 -04001767 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001768 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001769 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001770 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
1771 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001772 }
Rex Zhu8201a672016-11-24 21:44:44 +08001773
Alex Deuchera1255102016-10-13 17:41:13 -04001774 adev->ip_blocks[i].status.hw = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001775 }
1776
Alex Deucher9950cda2018-01-18 19:05:36 -05001777
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001778 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001779 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001780 continue;
Monk Liuc12aba32018-01-24 12:20:32 +08001781
1782 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
1783 amdgpu_free_static_csa(adev);
1784 amdgpu_device_wb_fini(adev);
1785 amdgpu_device_vram_scratch_fini(adev);
1786 }
1787
Alex Deuchera1255102016-10-13 17:41:13 -04001788 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001789 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001790 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001791 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
1792 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001793 }
Alex Deuchera1255102016-10-13 17:41:13 -04001794 adev->ip_blocks[i].status.sw = false;
1795 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001796 }
1797
Monk Liua6dcfd92016-05-19 14:36:34 +08001798 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001799 if (!adev->ip_blocks[i].status.late_initialized)
Grazvydas Ignotas8a2eef12016-10-03 00:06:44 +03001800 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001801 if (adev->ip_blocks[i].version->funcs->late_fini)
1802 adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
1803 adev->ip_blocks[i].status.late_initialized = false;
Monk Liua6dcfd92016-05-19 14:36:34 +08001804 }
1805
Monk Liu030308f2017-09-15 15:34:52 +08001806 if (amdgpu_sriov_vf(adev))
Monk Liu24136132017-11-14 16:56:55 +08001807 if (amdgpu_virt_release_full_gpu(adev, false))
1808 DRM_ERROR("failed to release exclusive mode on fini\n");
Monk Liu24936642017-01-09 15:54:32 +08001809
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001810 return 0;
1811}
1812
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001813/**
1814 * amdgpu_device_ip_late_init_func_handler - work handler for clockgating
1815 *
1816 * @work: work_struct
1817 *
1818 * Work handler for amdgpu_device_ip_late_set_cg_state. We put the
1819 * clockgating setup into a worker thread to speed up driver init and
1820 * resume from suspend.
1821 */
Alex Deucher06ec9072017-12-14 15:02:39 -05001822static void amdgpu_device_ip_late_init_func_handler(struct work_struct *work)
Shirish S2dc80b02017-05-25 10:05:25 +05301823{
1824 struct amdgpu_device *adev =
1825 container_of(work, struct amdgpu_device, late_init_work.work);
Alex Deucher06ec9072017-12-14 15:02:39 -05001826 amdgpu_device_ip_late_set_cg_state(adev);
Shirish S2dc80b02017-05-25 10:05:25 +05301827}
1828
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001829/**
1830 * amdgpu_device_ip_suspend - run suspend for hardware IPs
1831 *
1832 * @adev: amdgpu_device pointer
1833 *
1834 * Main suspend function for hardware IPs. The list of all the hardware
1835 * IPs that make up the asic is walked, clockgating is disabled and the
1836 * suspend callbacks are run. suspend puts the hardware and software state
1837 * in each IP into a state suitable for suspend.
1838 * Returns 0 on success, negative error code on failure.
1839 */
Alex Deuchercdd61df2017-12-14 16:47:40 -05001840int amdgpu_device_ip_suspend(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001841{
1842 int i, r;
1843
Xiangliang Yue941ea92017-01-18 12:47:55 +08001844 if (amdgpu_sriov_vf(adev))
1845 amdgpu_virt_request_full_gpu(adev, false);
1846
Flora Cuic5a93a22016-02-26 10:45:25 +08001847 /* ungate SMC block first */
Alex Deucher2990a1f2017-12-15 16:18:00 -05001848 r = amdgpu_device_ip_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC,
1849 AMD_CG_STATE_UNGATE);
Flora Cuic5a93a22016-02-26 10:45:25 +08001850 if (r) {
Alex Deucher2990a1f2017-12-15 16:18:00 -05001851 DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n", r);
Flora Cuic5a93a22016-02-26 10:45:25 +08001852 }
1853
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001854 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001855 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001856 continue;
1857 /* ungate blocks so that suspend can properly shut them down */
Rex Zhu5b2a3d22018-03-14 15:38:48 +08001858 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_SMC &&
Rex Zhu57716322018-03-12 19:50:38 +08001859 adev->ip_blocks[i].version->funcs->set_clockgating_state) {
Alex Deuchera1255102016-10-13 17:41:13 -04001860 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1861 AMD_CG_STATE_UNGATE);
Flora Cuic5a93a22016-02-26 10:45:25 +08001862 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001863 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1864 adev->ip_blocks[i].version->funcs->name, r);
Flora Cuic5a93a22016-02-26 10:45:25 +08001865 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001866 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001867 /* XXX handle errors */
Alex Deuchera1255102016-10-13 17:41:13 -04001868 r = adev->ip_blocks[i].version->funcs->suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001869 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001870 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001871 DRM_ERROR("suspend of IP block <%s> failed %d\n",
1872 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001873 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001874 }
1875
Xiangliang Yue941ea92017-01-18 12:47:55 +08001876 if (amdgpu_sriov_vf(adev))
1877 amdgpu_virt_release_full_gpu(adev, false);
1878
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001879 return 0;
1880}
1881
Alex Deucher06ec9072017-12-14 15:02:39 -05001882static int amdgpu_device_ip_reinit_early_sriov(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08001883{
1884 int i, r;
1885
Monk Liu2cb681b2017-04-26 12:00:49 +08001886 static enum amd_ip_block_type ip_order[] = {
1887 AMD_IP_BLOCK_TYPE_GMC,
1888 AMD_IP_BLOCK_TYPE_COMMON,
Monk Liu2cb681b2017-04-26 12:00:49 +08001889 AMD_IP_BLOCK_TYPE_IH,
1890 };
Monk Liua90ad3c2017-01-23 14:22:08 +08001891
Monk Liu2cb681b2017-04-26 12:00:49 +08001892 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1893 int j;
1894 struct amdgpu_ip_block *block;
Monk Liua90ad3c2017-01-23 14:22:08 +08001895
Monk Liu2cb681b2017-04-26 12:00:49 +08001896 for (j = 0; j < adev->num_ip_blocks; j++) {
1897 block = &adev->ip_blocks[j];
1898
1899 if (block->version->type != ip_order[i] ||
1900 !block->status.valid)
1901 continue;
1902
1903 r = block->version->funcs->hw_init(adev);
1904 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
Monk Liuc41d1cf2017-12-25 11:59:27 +08001905 if (r)
1906 return r;
Monk Liua90ad3c2017-01-23 14:22:08 +08001907 }
1908 }
1909
1910 return 0;
1911}
1912
Alex Deucher06ec9072017-12-14 15:02:39 -05001913static int amdgpu_device_ip_reinit_late_sriov(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08001914{
1915 int i, r;
1916
Monk Liu2cb681b2017-04-26 12:00:49 +08001917 static enum amd_ip_block_type ip_order[] = {
1918 AMD_IP_BLOCK_TYPE_SMC,
Monk Liuef4c1662017-09-22 16:23:34 +08001919 AMD_IP_BLOCK_TYPE_PSP,
Monk Liu2cb681b2017-04-26 12:00:49 +08001920 AMD_IP_BLOCK_TYPE_DCE,
1921 AMD_IP_BLOCK_TYPE_GFX,
1922 AMD_IP_BLOCK_TYPE_SDMA,
Frank Min257deb82017-06-15 20:07:36 +08001923 AMD_IP_BLOCK_TYPE_UVD,
1924 AMD_IP_BLOCK_TYPE_VCE
Monk Liu2cb681b2017-04-26 12:00:49 +08001925 };
Monk Liua90ad3c2017-01-23 14:22:08 +08001926
Monk Liu2cb681b2017-04-26 12:00:49 +08001927 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1928 int j;
1929 struct amdgpu_ip_block *block;
Monk Liua90ad3c2017-01-23 14:22:08 +08001930
Monk Liu2cb681b2017-04-26 12:00:49 +08001931 for (j = 0; j < adev->num_ip_blocks; j++) {
1932 block = &adev->ip_blocks[j];
1933
1934 if (block->version->type != ip_order[i] ||
1935 !block->status.valid)
1936 continue;
1937
1938 r = block->version->funcs->hw_init(adev);
1939 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
Monk Liuc41d1cf2017-12-25 11:59:27 +08001940 if (r)
1941 return r;
Monk Liua90ad3c2017-01-23 14:22:08 +08001942 }
1943 }
1944
1945 return 0;
1946}
1947
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001948/**
1949 * amdgpu_device_ip_resume_phase1 - run resume for hardware IPs
1950 *
1951 * @adev: amdgpu_device pointer
1952 *
1953 * First resume function for hardware IPs. The list of all the hardware
1954 * IPs that make up the asic is walked and the resume callbacks are run for
1955 * COMMON, GMC, and IH. resume puts the hardware into a functional state
1956 * after a suspend and updates the software state as necessary. This
1957 * function is also used for restoring the GPU after a GPU reset.
1958 * Returns 0 on success, negative error code on failure.
1959 */
Alex Deucher06ec9072017-12-14 15:02:39 -05001960static int amdgpu_device_ip_resume_phase1(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001961{
1962 int i, r;
1963
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001964 for (i = 0; i < adev->num_ip_blocks; i++) {
1965 if (!adev->ip_blocks[i].status.valid)
1966 continue;
Chunming Zhoufcf06492017-05-05 10:33:33 +08001967 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001968 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
1969 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH) {
Chunming Zhoufcf06492017-05-05 10:33:33 +08001970 r = adev->ip_blocks[i].version->funcs->resume(adev);
1971 if (r) {
1972 DRM_ERROR("resume of IP block <%s> failed %d\n",
1973 adev->ip_blocks[i].version->funcs->name, r);
1974 return r;
1975 }
1976 }
1977 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001978
Chunming Zhoufcf06492017-05-05 10:33:33 +08001979 return 0;
1980}
1981
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001982/**
1983 * amdgpu_device_ip_resume_phase2 - run resume for hardware IPs
1984 *
1985 * @adev: amdgpu_device pointer
1986 *
1987 * First resume function for hardware IPs. The list of all the hardware
1988 * IPs that make up the asic is walked and the resume callbacks are run for
1989 * all blocks except COMMON, GMC, and IH. resume puts the hardware into a
1990 * functional state after a suspend and updates the software state as
1991 * necessary. This function is also used for restoring the GPU after a GPU
1992 * reset.
1993 * Returns 0 on success, negative error code on failure.
1994 */
Alex Deucher06ec9072017-12-14 15:02:39 -05001995static int amdgpu_device_ip_resume_phase2(struct amdgpu_device *adev)
Chunming Zhoufcf06492017-05-05 10:33:33 +08001996{
1997 int i, r;
1998
1999 for (i = 0; i < adev->num_ip_blocks; i++) {
2000 if (!adev->ip_blocks[i].status.valid)
2001 continue;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002002 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002003 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
2004 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002005 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002006 r = adev->ip_blocks[i].version->funcs->resume(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002007 if (r) {
2008 DRM_ERROR("resume of IP block <%s> failed %d\n",
2009 adev->ip_blocks[i].version->funcs->name, r);
2010 return r;
2011 }
2012 }
2013
2014 return 0;
2015}
2016
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002017/**
2018 * amdgpu_device_ip_resume - run resume for hardware IPs
2019 *
2020 * @adev: amdgpu_device pointer
2021 *
2022 * Main resume function for hardware IPs. The hardware IPs
2023 * are split into two resume functions because they are
2024 * are also used in in recovering from a GPU reset and some additional
2025 * steps need to be take between them. In this case (S3/S4) they are
2026 * run sequentially.
2027 * Returns 0 on success, negative error code on failure.
2028 */
Alex Deucher06ec9072017-12-14 15:02:39 -05002029static int amdgpu_device_ip_resume(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002030{
Chunming Zhoufcf06492017-05-05 10:33:33 +08002031 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002032
Alex Deucher06ec9072017-12-14 15:02:39 -05002033 r = amdgpu_device_ip_resume_phase1(adev);
Chunming Zhoufcf06492017-05-05 10:33:33 +08002034 if (r)
2035 return r;
Alex Deucher06ec9072017-12-14 15:02:39 -05002036 r = amdgpu_device_ip_resume_phase2(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002037
Chunming Zhoufcf06492017-05-05 10:33:33 +08002038 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002039}
2040
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002041/**
2042 * amdgpu_device_detect_sriov_bios - determine if the board supports SR-IOV
2043 *
2044 * @adev: amdgpu_device pointer
2045 *
2046 * Query the VBIOS data tables to determine if the board supports SR-IOV.
2047 */
Monk Liu4e99a442016-03-31 13:26:59 +08002048static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
Andres Rodriguez048765a2016-06-11 02:51:32 -04002049{
Monk Liu6867e1b2017-10-16 19:50:44 +08002050 if (amdgpu_sriov_vf(adev)) {
2051 if (adev->is_atom_fw) {
2052 if (amdgpu_atomfirmware_gpu_supports_virtualization(adev))
2053 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
2054 } else {
2055 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
2056 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
2057 }
2058
2059 if (!(adev->virt.caps & AMDGPU_SRIOV_CAPS_SRIOV_VBIOS))
2060 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_NO_VBIOS, 0, 0);
Alex Deuchera5bde2f2016-09-23 16:23:41 -04002061 }
Andres Rodriguez048765a2016-06-11 02:51:32 -04002062}
2063
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002064/**
2065 * amdgpu_device_asic_has_dc_support - determine if DC supports the asic
2066 *
2067 * @asic_type: AMD asic type
2068 *
2069 * Check if there is DC (new modesetting infrastructre) support for an asic.
2070 * returns true if DC has support, false if not.
2071 */
Harry Wentland45622362017-09-12 15:58:20 -04002072bool amdgpu_device_asic_has_dc_support(enum amd_asic_type asic_type)
2073{
2074 switch (asic_type) {
2075#if defined(CONFIG_DRM_AMD_DC)
2076 case CHIP_BONAIRE:
2077 case CHIP_HAWAII:
Alex Deucher0d6fbcc2017-08-10 14:39:48 -04002078 case CHIP_KAVERI:
Alex Deucher367e6682018-01-25 16:53:25 -05002079 case CHIP_KABINI:
2080 case CHIP_MULLINS:
Harry Wentland45622362017-09-12 15:58:20 -04002081 case CHIP_CARRIZO:
2082 case CHIP_STONEY:
2083 case CHIP_POLARIS11:
2084 case CHIP_POLARIS10:
Alex Deucher2c8ad2d2017-06-15 16:20:24 -04002085 case CHIP_POLARIS12:
Harry Wentland45622362017-09-12 15:58:20 -04002086 case CHIP_TONGA:
2087 case CHIP_FIJI:
2088#if defined(CONFIG_DRM_AMD_DC_PRE_VEGA)
2089 return amdgpu_dc != 0;
Harry Wentland45622362017-09-12 15:58:20 -04002090#endif
Harry Wentland42f8ffa2017-09-15 14:07:30 -04002091 case CHIP_VEGA10:
2092#if defined(CONFIG_DRM_AMD_DC_DCN1_0)
Hawking Zhangfd187852017-03-06 14:01:11 +08002093 case CHIP_RAVEN:
Harry Wentland42f8ffa2017-09-15 14:07:30 -04002094#endif
Hawking Zhangfd187852017-03-06 14:01:11 +08002095 return amdgpu_dc != 0;
2096#endif
Harry Wentland45622362017-09-12 15:58:20 -04002097 default:
2098 return false;
2099 }
2100}
2101
2102/**
2103 * amdgpu_device_has_dc_support - check if dc is supported
2104 *
2105 * @adev: amdgpu_device_pointer
2106 *
2107 * Returns true for supported, false for not supported
2108 */
2109bool amdgpu_device_has_dc_support(struct amdgpu_device *adev)
2110{
Xiangliang Yu2555039d2017-01-10 17:34:52 +08002111 if (amdgpu_sriov_vf(adev))
2112 return false;
2113
Harry Wentland45622362017-09-12 15:58:20 -04002114 return amdgpu_device_asic_has_dc_support(adev->asic_type);
2115}
2116
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002117/**
2118 * amdgpu_device_init - initialize the driver
2119 *
2120 * @adev: amdgpu_device pointer
2121 * @pdev: drm dev pointer
2122 * @pdev: pci dev pointer
2123 * @flags: driver flags
2124 *
2125 * Initializes the driver info and hw (all asics).
2126 * Returns 0 for success or an error on failure.
2127 * Called at driver startup.
2128 */
2129int amdgpu_device_init(struct amdgpu_device *adev,
2130 struct drm_device *ddev,
2131 struct pci_dev *pdev,
2132 uint32_t flags)
2133{
2134 int r, i;
2135 bool runtime = false;
Marek Olšák95844d22016-08-17 23:49:27 +02002136 u32 max_MBps;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002137
2138 adev->shutdown = false;
2139 adev->dev = &pdev->dev;
2140 adev->ddev = ddev;
2141 adev->pdev = pdev;
2142 adev->flags = flags;
Jammy Zhou2f7d10b2015-07-22 11:29:01 +08002143 adev->asic_type = flags & AMD_ASIC_MASK;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002144 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
Shaoyun Liu593aa2d2018-02-07 14:43:13 -05002145 if (amdgpu_emu_mode == 1)
2146 adev->usec_timeout *= 2;
Christian König770d13b2018-01-12 14:52:22 +01002147 adev->gmc.gart_size = 512 * 1024 * 1024;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002148 adev->accel_working = false;
2149 adev->num_rings = 0;
2150 adev->mman.buffer_funcs = NULL;
2151 adev->mman.buffer_funcs_ring = NULL;
2152 adev->vm_manager.vm_pte_funcs = NULL;
Christian König2d55e452016-02-08 17:37:38 +01002153 adev->vm_manager.vm_pte_num_rings = 0;
Christian König132f34e2018-01-12 15:26:08 +01002154 adev->gmc.gmc_funcs = NULL;
Chris Wilsonf54d1862016-10-25 13:00:45 +01002155 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
Andres Rodriguezb8866c22017-04-28 20:05:51 -04002156 bitmap_zero(adev->gfx.pipe_reserve_bitmap, AMDGPU_MAX_COMPUTE_QUEUES);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002157
2158 adev->smc_rreg = &amdgpu_invalid_rreg;
2159 adev->smc_wreg = &amdgpu_invalid_wreg;
2160 adev->pcie_rreg = &amdgpu_invalid_rreg;
2161 adev->pcie_wreg = &amdgpu_invalid_wreg;
Huang Rui36b9a952016-08-31 13:23:25 +08002162 adev->pciep_rreg = &amdgpu_invalid_rreg;
2163 adev->pciep_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002164 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
2165 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
2166 adev->didt_rreg = &amdgpu_invalid_rreg;
2167 adev->didt_wreg = &amdgpu_invalid_wreg;
Rex Zhuccdbb202016-06-08 12:47:41 +08002168 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
2169 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002170 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
2171 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
2172
Alex Deucher3e39ab92015-06-05 15:04:33 -04002173 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
2174 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
2175 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002176
2177 /* mutex initialization are all done here so we
2178 * can recall function without having locking issues */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002179 atomic_set(&adev->irq.ih.lock, 0);
Huang Rui0e5ca0d2017-03-03 18:37:23 -05002180 mutex_init(&adev->firmware.mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002181 mutex_init(&adev->pm.mutex);
2182 mutex_init(&adev->gfx.gpu_clock_mutex);
2183 mutex_init(&adev->srbm_mutex);
Andres Rodriguezb8866c22017-04-28 20:05:51 -04002184 mutex_init(&adev->gfx.pipe_reserve_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002185 mutex_init(&adev->grbm_idx_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002186 mutex_init(&adev->mn_lock);
Alex Deuchere23b74a2017-09-28 09:47:32 -04002187 mutex_init(&adev->virt.vf_errors.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002188 hash_init(adev->mn_hash);
Monk Liu13a752e2017-10-17 15:11:12 +08002189 mutex_init(&adev->lock_reset);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002190
Alex Deucher06ec9072017-12-14 15:02:39 -05002191 amdgpu_device_check_arguments(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002192
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002193 spin_lock_init(&adev->mmio_idx_lock);
2194 spin_lock_init(&adev->smc_idx_lock);
2195 spin_lock_init(&adev->pcie_idx_lock);
2196 spin_lock_init(&adev->uvd_ctx_idx_lock);
2197 spin_lock_init(&adev->didt_idx_lock);
Rex Zhuccdbb202016-06-08 12:47:41 +08002198 spin_lock_init(&adev->gc_cac_idx_lock);
Evan Quan16abb5d2017-07-04 09:21:50 +08002199 spin_lock_init(&adev->se_cac_idx_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002200 spin_lock_init(&adev->audio_endpt_idx_lock);
Marek Olšák95844d22016-08-17 23:49:27 +02002201 spin_lock_init(&adev->mm_stats.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002202
Chunming Zhou0c4e7fa2016-08-17 11:41:30 +08002203 INIT_LIST_HEAD(&adev->shadow_list);
2204 mutex_init(&adev->shadow_list_lock);
2205
Andres Rodriguez795f2812017-03-06 16:27:55 -05002206 INIT_LIST_HEAD(&adev->ring_lru_list);
2207 spin_lock_init(&adev->ring_lru_list_lock);
2208
Alex Deucher06ec9072017-12-14 15:02:39 -05002209 INIT_DELAYED_WORK(&adev->late_init_work,
2210 amdgpu_device_ip_late_init_func_handler);
Shirish S2dc80b02017-05-25 10:05:25 +05302211
Alex Xie0fa49552017-06-08 14:58:05 -04002212 /* Registers mapping */
2213 /* TODO: block userspace mapping of io register */
Ken Wangda69c1612016-01-21 19:08:55 +08002214 if (adev->asic_type >= CHIP_BONAIRE) {
2215 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
2216 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
2217 } else {
2218 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
2219 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
2220 }
Chunming Zhou5c1354b2016-08-30 16:13:10 +08002221
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002222 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
2223 if (adev->rmmio == NULL) {
2224 return -ENOMEM;
2225 }
2226 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
2227 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
2228
Christian König705e5192017-06-08 11:15:16 +02002229 /* doorbell bar mapping */
Alex Deucher06ec9072017-12-14 15:02:39 -05002230 amdgpu_device_doorbell_init(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002231
2232 /* io port mapping */
2233 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
2234 if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
2235 adev->rio_mem_size = pci_resource_len(adev->pdev, i);
2236 adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
2237 break;
2238 }
2239 }
2240 if (adev->rio_mem == NULL)
Amber Linb64a18c2017-01-04 08:06:58 -05002241 DRM_INFO("PCI I/O BAR is not found.\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002242
Alex Deucher5494d862018-03-09 15:14:11 -05002243 amdgpu_device_get_pcie_info(adev);
2244
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002245 /* early init functions */
Alex Deucher06ec9072017-12-14 15:02:39 -05002246 r = amdgpu_device_ip_early_init(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002247 if (r)
2248 return r;
2249
2250 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
2251 /* this will fail for cards that aren't VGA class devices, just
2252 * ignore it */
Alex Deucher06ec9072017-12-14 15:02:39 -05002253 vga_client_register(adev->pdev, adev, NULL, amdgpu_device_vga_set_decode);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002254
Alex Deuchere9bef452016-04-25 13:12:18 -04002255 if (amdgpu_device_is_px(ddev))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002256 runtime = true;
Lukas Wunner84c8b222017-03-10 21:23:45 +01002257 if (!pci_is_thunderbolt_attached(adev->pdev))
2258 vga_switcheroo_register_client(adev->pdev,
2259 &amdgpu_switcheroo_ops, runtime);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002260 if (runtime)
2261 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
2262
Shaoyun Liu9475a942018-02-01 18:13:23 -05002263 if (amdgpu_emu_mode == 1) {
2264 /* post the asic on emulation mode */
2265 emu_soc_asic_init(adev);
Shaoyun Liubfca0282018-02-01 17:37:50 -05002266 goto fence_driver_init;
Shaoyun Liu9475a942018-02-01 18:13:23 -05002267 }
Shaoyun Liubfca0282018-02-01 17:37:50 -05002268
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002269 /* Read BIOS */
Alex Deucher83ba1262016-06-03 18:21:41 -04002270 if (!amdgpu_get_bios(adev)) {
2271 r = -EINVAL;
2272 goto failed;
2273 }
Nils Wallméniusf7e9e9f2016-12-14 21:52:45 +01002274
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002275 r = amdgpu_atombios_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002276 if (r) {
2277 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002278 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_INIT_FAIL, 0, 0);
Alex Deucher83ba1262016-06-03 18:21:41 -04002279 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002280 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002281
Monk Liu4e99a442016-03-31 13:26:59 +08002282 /* detect if we are with an SRIOV vbios */
2283 amdgpu_device_detect_sriov_bios(adev);
Andres Rodriguez048765a2016-06-11 02:51:32 -04002284
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002285 /* Post card if necessary */
Alex Deucher39c640c2017-12-15 16:22:11 -05002286 if (amdgpu_device_need_post(adev)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002287 if (!adev->bios) {
Monk Liubec86372016-09-14 19:38:08 +08002288 dev_err(adev->dev, "no vBIOS found\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04002289 r = -EINVAL;
2290 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002291 }
Monk Liubec86372016-09-14 19:38:08 +08002292 DRM_INFO("GPU posting now...\n");
Monk Liu4e99a442016-03-31 13:26:59 +08002293 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2294 if (r) {
2295 dev_err(adev->dev, "gpu post error!\n");
2296 goto failed;
2297 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002298 }
2299
Alex Deucher88b64e92017-07-10 10:43:10 -04002300 if (adev->is_atom_fw) {
2301 /* Initialize clocks */
2302 r = amdgpu_atomfirmware_get_clock_info(adev);
2303 if (r) {
2304 dev_err(adev->dev, "amdgpu_atomfirmware_get_clock_info failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002305 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
Alex Deucher88b64e92017-07-10 10:43:10 -04002306 goto failed;
2307 }
2308 } else {
Alex Deuchera5bde2f2016-09-23 16:23:41 -04002309 /* Initialize clocks */
2310 r = amdgpu_atombios_get_clock_info(adev);
2311 if (r) {
2312 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002313 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
Gavin Wan89041942017-06-23 13:55:15 -04002314 goto failed;
Alex Deuchera5bde2f2016-09-23 16:23:41 -04002315 }
2316 /* init i2c buses */
Harry Wentland45622362017-09-12 15:58:20 -04002317 if (!amdgpu_device_has_dc_support(adev))
2318 amdgpu_atombios_i2c_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002319 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002320
Shaoyun Liubfca0282018-02-01 17:37:50 -05002321fence_driver_init:
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002322 /* Fence driver */
2323 r = amdgpu_fence_driver_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002324 if (r) {
2325 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002326 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0);
Alex Deucher83ba1262016-06-03 18:21:41 -04002327 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002328 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002329
2330 /* init the mode config */
2331 drm_mode_config_init(adev->ddev);
2332
Alex Deucher06ec9072017-12-14 15:02:39 -05002333 r = amdgpu_device_ip_init(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002334 if (r) {
pding8840a382017-10-23 17:22:09 +08002335 /* failed in exclusive mode due to timeout */
2336 if (amdgpu_sriov_vf(adev) &&
2337 !amdgpu_sriov_runtime(adev) &&
2338 amdgpu_virt_mmio_blocked(adev) &&
2339 !amdgpu_virt_wait_reset(adev)) {
2340 dev_err(adev->dev, "VF exclusive mode timeout\n");
Pixel Ding1daee8b2017-11-08 11:03:14 +08002341 /* Don't send request since VF is inactive. */
2342 adev->virt.caps &= ~AMDGPU_SRIOV_CAPS_RUNTIME;
2343 adev->virt.ops = NULL;
pding8840a382017-10-23 17:22:09 +08002344 r = -EAGAIN;
2345 goto failed;
2346 }
Alex Deucher06ec9072017-12-14 15:02:39 -05002347 dev_err(adev->dev, "amdgpu_device_ip_init failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002348 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0);
Alex Deucher06ec9072017-12-14 15:02:39 -05002349 amdgpu_device_ip_fini(adev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002350 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002351 }
2352
2353 adev->accel_working = true;
2354
Alex Xiee59c0202017-06-01 09:42:59 -04002355 amdgpu_vm_check_compute_bug(adev);
2356
Marek Olšák95844d22016-08-17 23:49:27 +02002357 /* Initialize the buffer migration limit. */
2358 if (amdgpu_moverate >= 0)
2359 max_MBps = amdgpu_moverate;
2360 else
2361 max_MBps = 8; /* Allow 8 MB/s. */
2362 /* Get a log2 for easy divisions. */
2363 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
2364
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002365 r = amdgpu_ib_pool_init(adev);
2366 if (r) {
2367 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
Alex Deuchere23b74a2017-09-28 09:47:32 -04002368 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r);
Alex Deucher83ba1262016-06-03 18:21:41 -04002369 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002370 }
2371
2372 r = amdgpu_ib_ring_tests(adev);
2373 if (r)
2374 DRM_ERROR("ib ring test failed (%d).\n", r);
2375
Horace Chen2dc8f812017-10-09 16:17:16 +08002376 if (amdgpu_sriov_vf(adev))
2377 amdgpu_virt_init_data_exchange(adev);
2378
Monk Liu9bc92b92017-02-08 17:38:13 +08002379 amdgpu_fbdev_init(adev);
2380
Rex Zhud2f52ac2017-09-22 17:47:27 +08002381 r = amdgpu_pm_sysfs_init(adev);
2382 if (r)
2383 DRM_ERROR("registering pm debugfs failed (%d).\n", r);
2384
Alex Deucher75758252017-12-14 15:23:14 -05002385 r = amdgpu_debugfs_gem_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002386 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002387 DRM_ERROR("registering gem debugfs failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002388
2389 r = amdgpu_debugfs_regs_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002390 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002391 DRM_ERROR("registering register debugfs failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002392
Huang Rui50ab2532016-06-12 15:51:09 +08002393 r = amdgpu_debugfs_firmware_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002394 if (r)
Huang Rui50ab2532016-06-12 15:51:09 +08002395 DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
Huang Rui50ab2532016-06-12 15:51:09 +08002396
Christian König763efb62017-12-06 15:44:51 +01002397 r = amdgpu_debugfs_init(adev);
Kent Russelldb95e212017-08-22 12:31:43 -04002398 if (r)
Christian König763efb62017-12-06 15:44:51 +01002399 DRM_ERROR("Creating debugfs files failed (%d).\n", r);
Kent Russelldb95e212017-08-22 12:31:43 -04002400
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002401 if ((amdgpu_testing & 1)) {
2402 if (adev->accel_working)
2403 amdgpu_test_moves(adev);
2404 else
2405 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
2406 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002407 if (amdgpu_benchmarking) {
2408 if (adev->accel_working)
2409 amdgpu_benchmark(adev, amdgpu_benchmarking);
2410 else
2411 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
2412 }
2413
2414 /* enable clockgating, etc. after ib tests, etc. since some blocks require
2415 * explicit gating rather than handling it automatically.
2416 */
Alex Deucher06ec9072017-12-14 15:02:39 -05002417 r = amdgpu_device_ip_late_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002418 if (r) {
Alex Deucher06ec9072017-12-14 15:02:39 -05002419 dev_err(adev->dev, "amdgpu_device_ip_late_init failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002420 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r);
Alex Deucher83ba1262016-06-03 18:21:41 -04002421 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002422 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002423
2424 return 0;
Alex Deucher83ba1262016-06-03 18:21:41 -04002425
2426failed:
Gavin Wan89041942017-06-23 13:55:15 -04002427 amdgpu_vf_error_trans_all(adev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002428 if (runtime)
2429 vga_switcheroo_fini_domain_pm_ops(adev->dev);
pding8840a382017-10-23 17:22:09 +08002430
Alex Deucher83ba1262016-06-03 18:21:41 -04002431 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002432}
2433
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002434/**
2435 * amdgpu_device_fini - tear down the driver
2436 *
2437 * @adev: amdgpu_device pointer
2438 *
2439 * Tear down the driver info (all asics).
2440 * Called at driver shutdown.
2441 */
2442void amdgpu_device_fini(struct amdgpu_device *adev)
2443{
2444 int r;
2445
2446 DRM_INFO("amdgpu: finishing device.\n");
2447 adev->shutdown = true;
Mikita Lipskie5b03032018-03-15 16:53:08 -04002448 /* disable all interrupts */
2449 amdgpu_irq_disable_all(adev);
Mikita Lipskiff97cba2018-03-14 13:41:29 -04002450 if (adev->mode_info.mode_config_initialized){
2451 if (!amdgpu_device_has_dc_support(adev))
2452 drm_crtc_force_disable_all(adev->ddev);
2453 else
2454 drm_atomic_helper_shutdown(adev->ddev);
2455 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002456 amdgpu_ib_pool_fini(adev);
2457 amdgpu_fence_driver_fini(adev);
Emily Deng58e955d2018-03-08 09:35:19 +08002458 amdgpu_pm_sysfs_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002459 amdgpu_fbdev_fini(adev);
Alex Deucher06ec9072017-12-14 15:02:39 -05002460 r = amdgpu_device_ip_fini(adev);
Huang Ruiab4fe3e2017-06-05 22:11:59 +08002461 if (adev->firmware.gpu_info_fw) {
2462 release_firmware(adev->firmware.gpu_info_fw);
2463 adev->firmware.gpu_info_fw = NULL;
2464 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002465 adev->accel_working = false;
Shirish S2dc80b02017-05-25 10:05:25 +05302466 cancel_delayed_work_sync(&adev->late_init_work);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002467 /* free i2c buses */
Harry Wentland45622362017-09-12 15:58:20 -04002468 if (!amdgpu_device_has_dc_support(adev))
2469 amdgpu_i2c_fini(adev);
Shaoyun Liubfca0282018-02-01 17:37:50 -05002470
2471 if (amdgpu_emu_mode != 1)
2472 amdgpu_atombios_fini(adev);
2473
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002474 kfree(adev->bios);
2475 adev->bios = NULL;
Lukas Wunner84c8b222017-03-10 21:23:45 +01002476 if (!pci_is_thunderbolt_attached(adev->pdev))
2477 vga_switcheroo_unregister_client(adev->pdev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002478 if (adev->flags & AMD_IS_PX)
2479 vga_switcheroo_fini_domain_pm_ops(adev->dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002480 vga_client_register(adev->pdev, NULL, NULL, NULL);
2481 if (adev->rio_mem)
2482 pci_iounmap(adev->pdev, adev->rio_mem);
2483 adev->rio_mem = NULL;
2484 iounmap(adev->rmmio);
2485 adev->rmmio = NULL;
Alex Deucher06ec9072017-12-14 15:02:39 -05002486 amdgpu_device_doorbell_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002487 amdgpu_debugfs_regs_cleanup(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002488}
2489
2490
2491/*
2492 * Suspend & resume.
2493 */
2494/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002495 * amdgpu_device_suspend - initiate device suspend
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002496 *
2497 * @pdev: drm dev pointer
2498 * @state: suspend state
2499 *
2500 * Puts the hw in the suspend state (all asics).
2501 * Returns 0 for success or an error on failure.
2502 * Called at driver suspend.
2503 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002504int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002505{
2506 struct amdgpu_device *adev;
2507 struct drm_crtc *crtc;
2508 struct drm_connector *connector;
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002509 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002510
2511 if (dev == NULL || dev->dev_private == NULL) {
2512 return -ENODEV;
2513 }
2514
2515 adev = dev->dev_private;
2516
2517 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2518 return 0;
2519
2520 drm_kms_helper_poll_disable(dev);
2521
Harry Wentland45622362017-09-12 15:58:20 -04002522 if (!amdgpu_device_has_dc_support(adev)) {
2523 /* turn off display hw */
2524 drm_modeset_lock_all(dev);
2525 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2526 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
2527 }
2528 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002529 }
2530
Yong Zhaoba997702015-11-09 17:21:45 -05002531 amdgpu_amdkfd_suspend(adev);
2532
Alex Deucher756e6882015-10-08 00:03:36 -04002533 /* unpin the front buffers and cursors */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002534 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Alex Deucher756e6882015-10-08 00:03:36 -04002535 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002536 struct amdgpu_framebuffer *rfb = to_amdgpu_framebuffer(crtc->primary->fb);
2537 struct amdgpu_bo *robj;
2538
Alex Deucher756e6882015-10-08 00:03:36 -04002539 if (amdgpu_crtc->cursor_bo) {
2540 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
Alex Xie7a6901d2017-04-24 13:52:41 -04002541 r = amdgpu_bo_reserve(aobj, true);
Alex Deucher756e6882015-10-08 00:03:36 -04002542 if (r == 0) {
2543 amdgpu_bo_unpin(aobj);
2544 amdgpu_bo_unreserve(aobj);
2545 }
2546 }
2547
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002548 if (rfb == NULL || rfb->obj == NULL) {
2549 continue;
2550 }
2551 robj = gem_to_amdgpu_bo(rfb->obj);
2552 /* don't unpin kernel fb objects */
2553 if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
Alex Xie7a6901d2017-04-24 13:52:41 -04002554 r = amdgpu_bo_reserve(robj, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002555 if (r == 0) {
2556 amdgpu_bo_unpin(robj);
2557 amdgpu_bo_unreserve(robj);
2558 }
2559 }
2560 }
2561 /* evict vram memory */
2562 amdgpu_bo_evict_vram(adev);
2563
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002564 amdgpu_fence_driver_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002565
Alex Deuchercdd61df2017-12-14 16:47:40 -05002566 r = amdgpu_device_ip_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002567
Alex Deuchera0a71e42016-10-10 12:41:36 -04002568 /* evict remaining vram memory
2569 * This second call to evict vram is to evict the gart page table
2570 * using the CPU.
2571 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002572 amdgpu_bo_evict_vram(adev);
2573
2574 pci_save_state(dev->pdev);
2575 if (suspend) {
2576 /* Shut down the device */
2577 pci_disable_device(dev->pdev);
2578 pci_set_power_state(dev->pdev, PCI_D3hot);
jimqu74b0b152016-09-07 17:09:12 +08002579 } else {
2580 r = amdgpu_asic_reset(adev);
2581 if (r)
2582 DRM_ERROR("amdgpu asic reset failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002583 }
2584
2585 if (fbcon) {
2586 console_lock();
2587 amdgpu_fbdev_set_suspend(adev, 1);
2588 console_unlock();
2589 }
2590 return 0;
2591}
2592
2593/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002594 * amdgpu_device_resume - initiate device resume
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002595 *
2596 * @pdev: drm dev pointer
2597 *
2598 * Bring the hw back to operating state (all asics).
2599 * Returns 0 for success or an error on failure.
2600 * Called at driver resume.
2601 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002602int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002603{
2604 struct drm_connector *connector;
2605 struct amdgpu_device *adev = dev->dev_private;
Alex Deucher756e6882015-10-08 00:03:36 -04002606 struct drm_crtc *crtc;
Huang Rui03161a62017-04-13 16:12:26 +08002607 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002608
2609 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2610 return 0;
2611
jimqu74b0b152016-09-07 17:09:12 +08002612 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002613 console_lock();
jimqu74b0b152016-09-07 17:09:12 +08002614
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002615 if (resume) {
2616 pci_set_power_state(dev->pdev, PCI_D0);
2617 pci_restore_state(dev->pdev);
jimqu74b0b152016-09-07 17:09:12 +08002618 r = pci_enable_device(dev->pdev);
Huang Rui03161a62017-04-13 16:12:26 +08002619 if (r)
2620 goto unlock;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002621 }
2622
2623 /* post card */
Alex Deucher39c640c2017-12-15 16:22:11 -05002624 if (amdgpu_device_need_post(adev)) {
jimqu74b0b152016-09-07 17:09:12 +08002625 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2626 if (r)
2627 DRM_ERROR("amdgpu asic init failed\n");
2628 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002629
Alex Deucher06ec9072017-12-14 15:02:39 -05002630 r = amdgpu_device_ip_resume(adev);
Rex Zhue6707212017-03-30 13:21:01 +08002631 if (r) {
Alex Deucher06ec9072017-12-14 15:02:39 -05002632 DRM_ERROR("amdgpu_device_ip_resume failed (%d).\n", r);
Huang Rui03161a62017-04-13 16:12:26 +08002633 goto unlock;
Rex Zhue6707212017-03-30 13:21:01 +08002634 }
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002635 amdgpu_fence_driver_resume(adev);
2636
Flora Cuica198522016-02-04 15:10:08 +08002637 if (resume) {
2638 r = amdgpu_ib_ring_tests(adev);
2639 if (r)
2640 DRM_ERROR("ib ring test failed (%d).\n", r);
2641 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002642
Alex Deucher06ec9072017-12-14 15:02:39 -05002643 r = amdgpu_device_ip_late_init(adev);
Huang Rui03161a62017-04-13 16:12:26 +08002644 if (r)
2645 goto unlock;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002646
Alex Deucher756e6882015-10-08 00:03:36 -04002647 /* pin cursors */
2648 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2649 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2650
2651 if (amdgpu_crtc->cursor_bo) {
2652 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
Alex Xie7a6901d2017-04-24 13:52:41 -04002653 r = amdgpu_bo_reserve(aobj, true);
Alex Deucher756e6882015-10-08 00:03:36 -04002654 if (r == 0) {
2655 r = amdgpu_bo_pin(aobj,
2656 AMDGPU_GEM_DOMAIN_VRAM,
2657 &amdgpu_crtc->cursor_addr);
2658 if (r != 0)
2659 DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
2660 amdgpu_bo_unreserve(aobj);
2661 }
2662 }
2663 }
Yong Zhaoba997702015-11-09 17:21:45 -05002664 r = amdgpu_amdkfd_resume(adev);
2665 if (r)
2666 return r;
Alex Deucher756e6882015-10-08 00:03:36 -04002667
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002668 /* blat the mode back in */
2669 if (fbcon) {
Harry Wentland45622362017-09-12 15:58:20 -04002670 if (!amdgpu_device_has_dc_support(adev)) {
2671 /* pre DCE11 */
2672 drm_helper_resume_force_mode(dev);
2673
2674 /* turn on display hw */
2675 drm_modeset_lock_all(dev);
2676 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2677 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
2678 }
2679 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002680 }
2681 }
2682
2683 drm_kms_helper_poll_enable(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002684
2685 /*
2686 * Most of the connector probing functions try to acquire runtime pm
2687 * refs to ensure that the GPU is powered on when connector polling is
2688 * performed. Since we're calling this from a runtime PM callback,
2689 * trying to acquire rpm refs will cause us to deadlock.
2690 *
2691 * Since we're guaranteed to be holding the rpm lock, it's safe to
2692 * temporarily disable the rpm helpers so this doesn't deadlock us.
2693 */
2694#ifdef CONFIG_PM
2695 dev->dev->power.disable_depth++;
2696#endif
Harry Wentland45622362017-09-12 15:58:20 -04002697 if (!amdgpu_device_has_dc_support(adev))
2698 drm_helper_hpd_irq_event(dev);
2699 else
2700 drm_kms_helper_hotplug_event(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002701#ifdef CONFIG_PM
2702 dev->dev->power.disable_depth--;
2703#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002704
Huang Rui03161a62017-04-13 16:12:26 +08002705 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002706 amdgpu_fbdev_set_suspend(adev, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002707
Huang Rui03161a62017-04-13 16:12:26 +08002708unlock:
2709 if (fbcon)
2710 console_unlock();
2711
2712 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002713}
2714
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002715/**
2716 * amdgpu_device_ip_check_soft_reset - did soft reset succeed
2717 *
2718 * @adev: amdgpu_device pointer
2719 *
2720 * The list of all the hardware IPs that make up the asic is walked and
2721 * the check_soft_reset callbacks are run. check_soft_reset determines
2722 * if the asic is still hung or not.
2723 * Returns true if any of the IPs are still in a hung state, false if not.
2724 */
Alex Deucher06ec9072017-12-14 15:02:39 -05002725static bool amdgpu_device_ip_check_soft_reset(struct amdgpu_device *adev)
Chunming Zhou63fbf422016-07-15 11:19:20 +08002726{
2727 int i;
2728 bool asic_hang = false;
2729
Monk Liuf993d622017-10-16 19:46:01 +08002730 if (amdgpu_sriov_vf(adev))
2731 return true;
2732
Chunming Zhou63fbf422016-07-15 11:19:20 +08002733 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002734 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou63fbf422016-07-15 11:19:20 +08002735 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002736 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
2737 adev->ip_blocks[i].status.hang =
2738 adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
2739 if (adev->ip_blocks[i].status.hang) {
2740 DRM_INFO("IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
Chunming Zhou63fbf422016-07-15 11:19:20 +08002741 asic_hang = true;
2742 }
2743 }
2744 return asic_hang;
2745}
2746
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002747/**
2748 * amdgpu_device_ip_pre_soft_reset - prepare for soft reset
2749 *
2750 * @adev: amdgpu_device pointer
2751 *
2752 * The list of all the hardware IPs that make up the asic is walked and the
2753 * pre_soft_reset callbacks are run if the block is hung. pre_soft_reset
2754 * handles any IP specific hardware or software state changes that are
2755 * necessary for a soft reset to succeed.
2756 * Returns 0 on success, negative error code on failure.
2757 */
Alex Deucher06ec9072017-12-14 15:02:39 -05002758static int amdgpu_device_ip_pre_soft_reset(struct amdgpu_device *adev)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002759{
2760 int i, r = 0;
2761
2762 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002763 if (!adev->ip_blocks[i].status.valid)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002764 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002765 if (adev->ip_blocks[i].status.hang &&
2766 adev->ip_blocks[i].version->funcs->pre_soft_reset) {
2767 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
Chunming Zhoud31a5012016-07-18 10:04:34 +08002768 if (r)
2769 return r;
2770 }
2771 }
2772
2773 return 0;
2774}
2775
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002776/**
2777 * amdgpu_device_ip_need_full_reset - check if a full asic reset is needed
2778 *
2779 * @adev: amdgpu_device pointer
2780 *
2781 * Some hardware IPs cannot be soft reset. If they are hung, a full gpu
2782 * reset is necessary to recover.
2783 * Returns true if a full asic reset is required, false if not.
2784 */
Alex Deucher06ec9072017-12-14 15:02:39 -05002785static bool amdgpu_device_ip_need_full_reset(struct amdgpu_device *adev)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002786{
Alex Deucherda146d32016-10-13 16:07:03 -04002787 int i;
2788
2789 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002790 if (!adev->ip_blocks[i].status.valid)
Alex Deucherda146d32016-10-13 16:07:03 -04002791 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002792 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
2793 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
2794 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
Ken Wang98512bb2017-09-14 16:25:19 +08002795 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) ||
2796 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
Alex Deuchera1255102016-10-13 17:41:13 -04002797 if (adev->ip_blocks[i].status.hang) {
Alex Deucherda146d32016-10-13 16:07:03 -04002798 DRM_INFO("Some block need full reset!\n");
2799 return true;
2800 }
2801 }
Chunming Zhou35d782f2016-07-15 15:57:13 +08002802 }
2803 return false;
2804}
2805
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002806/**
2807 * amdgpu_device_ip_soft_reset - do a soft reset
2808 *
2809 * @adev: amdgpu_device pointer
2810 *
2811 * The list of all the hardware IPs that make up the asic is walked and the
2812 * soft_reset callbacks are run if the block is hung. soft_reset handles any
2813 * IP specific hardware or software state changes that are necessary to soft
2814 * reset the IP.
2815 * Returns 0 on success, negative error code on failure.
2816 */
Alex Deucher06ec9072017-12-14 15:02:39 -05002817static int amdgpu_device_ip_soft_reset(struct amdgpu_device *adev)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002818{
2819 int i, r = 0;
2820
2821 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002822 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002823 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002824 if (adev->ip_blocks[i].status.hang &&
2825 adev->ip_blocks[i].version->funcs->soft_reset) {
2826 r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002827 if (r)
2828 return r;
2829 }
2830 }
2831
2832 return 0;
2833}
2834
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002835/**
2836 * amdgpu_device_ip_post_soft_reset - clean up from soft reset
2837 *
2838 * @adev: amdgpu_device pointer
2839 *
2840 * The list of all the hardware IPs that make up the asic is walked and the
2841 * post_soft_reset callbacks are run if the asic was hung. post_soft_reset
2842 * handles any IP specific hardware or software state changes that are
2843 * necessary after the IP has been soft reset.
2844 * Returns 0 on success, negative error code on failure.
2845 */
Alex Deucher06ec9072017-12-14 15:02:39 -05002846static int amdgpu_device_ip_post_soft_reset(struct amdgpu_device *adev)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002847{
2848 int i, r = 0;
2849
2850 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002851 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002852 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002853 if (adev->ip_blocks[i].status.hang &&
2854 adev->ip_blocks[i].version->funcs->post_soft_reset)
2855 r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002856 if (r)
2857 return r;
2858 }
2859
2860 return 0;
2861}
2862
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002863/**
2864 * amdgpu_device_recover_vram_from_shadow - restore shadowed VRAM buffers
2865 *
2866 * @adev: amdgpu_device pointer
2867 * @ring: amdgpu_ring for the engine handling the buffer operations
2868 * @bo: amdgpu_bo buffer whose shadow is being restored
2869 * @fence: dma_fence associated with the operation
2870 *
2871 * Restores the VRAM buffer contents from the shadow in GTT. Used to
2872 * restore things like GPUVM page tables after a GPU reset where
2873 * the contents of VRAM might be lost.
2874 * Returns 0 on success, negative error code on failure.
2875 */
Alex Deucher06ec9072017-12-14 15:02:39 -05002876static int amdgpu_device_recover_vram_from_shadow(struct amdgpu_device *adev,
2877 struct amdgpu_ring *ring,
2878 struct amdgpu_bo *bo,
2879 struct dma_fence **fence)
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002880{
2881 uint32_t domain;
2882 int r;
2883
Roger.He23d2e502017-04-21 14:24:26 +08002884 if (!bo->shadow)
2885 return 0;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002886
Alex Xie1d284792017-04-24 13:53:04 -04002887 r = amdgpu_bo_reserve(bo, true);
Roger.He23d2e502017-04-21 14:24:26 +08002888 if (r)
2889 return r;
2890 domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
2891 /* if bo has been evicted, then no need to recover */
2892 if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
Roger.He82521312017-04-21 13:08:43 +08002893 r = amdgpu_bo_validate(bo->shadow);
2894 if (r) {
2895 DRM_ERROR("bo validate failed!\n");
2896 goto err;
2897 }
2898
Roger.He23d2e502017-04-21 14:24:26 +08002899 r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002900 NULL, fence, true);
Roger.He23d2e502017-04-21 14:24:26 +08002901 if (r) {
2902 DRM_ERROR("recover page table failed!\n");
2903 goto err;
2904 }
2905 }
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002906err:
Roger.He23d2e502017-04-21 14:24:26 +08002907 amdgpu_bo_unreserve(bo);
2908 return r;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002909}
2910
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002911/**
2912 * amdgpu_device_handle_vram_lost - Handle the loss of VRAM contents
2913 *
2914 * @adev: amdgpu_device pointer
2915 *
2916 * Restores the contents of VRAM buffers from the shadows in GTT. Used to
2917 * restore things like GPUVM page tables after a GPU reset where
2918 * the contents of VRAM might be lost.
2919 * Returns 0 on success, 1 on failure.
2920 */
Monk Liuc41d1cf2017-12-25 11:59:27 +08002921static int amdgpu_device_handle_vram_lost(struct amdgpu_device *adev)
2922{
2923 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
2924 struct amdgpu_bo *bo, *tmp;
2925 struct dma_fence *fence = NULL, *next = NULL;
2926 long r = 1;
2927 int i = 0;
2928 long tmo;
2929
2930 if (amdgpu_sriov_runtime(adev))
2931 tmo = msecs_to_jiffies(amdgpu_lockup_timeout);
2932 else
2933 tmo = msecs_to_jiffies(100);
2934
2935 DRM_INFO("recover vram bo from shadow start\n");
2936 mutex_lock(&adev->shadow_list_lock);
2937 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
2938 next = NULL;
2939 amdgpu_device_recover_vram_from_shadow(adev, ring, bo, &next);
2940 if (fence) {
2941 r = dma_fence_wait_timeout(fence, false, tmo);
2942 if (r == 0)
2943 pr_err("wait fence %p[%d] timeout\n", fence, i);
2944 else if (r < 0)
2945 pr_err("wait fence %p[%d] interrupted\n", fence, i);
2946 if (r < 1) {
2947 dma_fence_put(fence);
2948 fence = next;
2949 break;
2950 }
2951 i++;
2952 }
2953
2954 dma_fence_put(fence);
2955 fence = next;
2956 }
2957 mutex_unlock(&adev->shadow_list_lock);
2958
2959 if (fence) {
2960 r = dma_fence_wait_timeout(fence, false, tmo);
2961 if (r == 0)
2962 pr_err("wait fence %p[%d] timeout\n", fence, i);
2963 else if (r < 0)
2964 pr_err("wait fence %p[%d] interrupted\n", fence, i);
2965
2966 }
2967 dma_fence_put(fence);
2968
2969 if (r > 0)
2970 DRM_INFO("recover vram bo from shadow done\n");
2971 else
2972 DRM_ERROR("recover vram bo from shadow failed\n");
2973
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002974 return (r > 0) ? 0 : 1;
Monk Liuc41d1cf2017-12-25 11:59:27 +08002975}
2976
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002977/**
Alex Deucher06ec9072017-12-14 15:02:39 -05002978 * amdgpu_device_reset - reset ASIC/GPU for bare-metal or passthrough
Monk Liua90ad3c2017-01-23 14:22:08 +08002979 *
2980 * @adev: amdgpu device pointer
Monk Liua90ad3c2017-01-23 14:22:08 +08002981 *
Monk Liu57406822017-10-25 16:37:02 +08002982 * attempt to do soft-reset or full-reset and reinitialize Asic
2983 * return 0 means successed otherwise failed
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002984 */
Monk Liuc41d1cf2017-12-25 11:59:27 +08002985static int amdgpu_device_reset(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08002986{
Monk Liu57406822017-10-25 16:37:02 +08002987 bool need_full_reset, vram_lost = 0;
2988 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002989
Alex Deucher06ec9072017-12-14 15:02:39 -05002990 need_full_reset = amdgpu_device_ip_need_full_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002991
2992 if (!need_full_reset) {
Alex Deucher06ec9072017-12-14 15:02:39 -05002993 amdgpu_device_ip_pre_soft_reset(adev);
2994 r = amdgpu_device_ip_soft_reset(adev);
2995 amdgpu_device_ip_post_soft_reset(adev);
2996 if (r || amdgpu_device_ip_check_soft_reset(adev)) {
Chunming Zhou35d782f2016-07-15 15:57:13 +08002997 DRM_INFO("soft reset failed, will fallback to full reset!\n");
2998 need_full_reset = true;
2999 }
3000 }
3001
3002 if (need_full_reset) {
Alex Deuchercdd61df2017-12-14 16:47:40 -05003003 r = amdgpu_device_ip_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003004
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003005retry:
Chunming Zhou35d782f2016-07-15 15:57:13 +08003006 r = amdgpu_asic_reset(adev);
3007 /* post card */
3008 amdgpu_atom_asic_init(adev->mode_info.atom_context);
Alex Deucherbfa99262016-01-15 11:59:48 -05003009
Chunming Zhou35d782f2016-07-15 15:57:13 +08003010 if (!r) {
3011 dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
Alex Deucher06ec9072017-12-14 15:02:39 -05003012 r = amdgpu_device_ip_resume_phase1(adev);
Chunming Zhoufcf06492017-05-05 10:33:33 +08003013 if (r)
3014 goto out;
Monk Liu57406822017-10-25 16:37:02 +08003015
Alex Deucher06ec9072017-12-14 15:02:39 -05003016 vram_lost = amdgpu_device_check_vram_lost(adev);
Chunming Zhouf1892132017-05-15 16:48:27 +08003017 if (vram_lost) {
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08003018 DRM_ERROR("VRAM is lost!\n");
Chunming Zhouf1892132017-05-15 16:48:27 +08003019 atomic_inc(&adev->vram_lost_counter);
3020 }
Monk Liu57406822017-10-25 16:37:02 +08003021
Christian Königc1c7ce82017-10-16 16:50:32 +02003022 r = amdgpu_gtt_mgr_recover(
3023 &adev->mman.bdev.man[TTM_PL_TT]);
Chunming Zhou2c0d7312016-08-30 16:36:25 +08003024 if (r)
Chunming Zhoufcf06492017-05-05 10:33:33 +08003025 goto out;
Monk Liu57406822017-10-25 16:37:02 +08003026
Alex Deucher06ec9072017-12-14 15:02:39 -05003027 r = amdgpu_device_ip_resume_phase2(adev);
Chunming Zhoufcf06492017-05-05 10:33:33 +08003028 if (r)
3029 goto out;
Monk Liu57406822017-10-25 16:37:02 +08003030
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08003031 if (vram_lost)
Alex Deucher06ec9072017-12-14 15:02:39 -05003032 amdgpu_device_fill_reset_magic(adev);
Chunming Zhou2c0d7312016-08-30 16:36:25 +08003033 }
Chunming Zhoufcf06492017-05-05 10:33:33 +08003034 }
Monk Liu57406822017-10-25 16:37:02 +08003035
Chunming Zhoufcf06492017-05-05 10:33:33 +08003036out:
3037 if (!r) {
3038 amdgpu_irq_gpu_reset_resume_helper(adev);
Chunming Zhou1f465082016-06-30 15:02:26 +08003039 r = amdgpu_ib_ring_tests(adev);
3040 if (r) {
3041 dev_err(adev->dev, "ib ring test failed (%d).\n", r);
Alex Deuchercdd61df2017-12-14 16:47:40 -05003042 r = amdgpu_device_ip_suspend(adev);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003043 need_full_reset = true;
Chunming Zhou40019dc2016-06-29 16:01:49 +08003044 goto retry;
Chunming Zhou1f465082016-06-30 15:02:26 +08003045 }
Monk Liu57406822017-10-25 16:37:02 +08003046 }
3047
Monk Liuc41d1cf2017-12-25 11:59:27 +08003048 if (!r && ((need_full_reset && !(adev->flags & AMD_IS_APU)) || vram_lost))
3049 r = amdgpu_device_handle_vram_lost(adev);
Monk Liu57406822017-10-25 16:37:02 +08003050
3051 return r;
3052}
3053
Alex Deuchere3ecdff2018-03-15 17:39:45 -05003054/**
Alex Deucher06ec9072017-12-14 15:02:39 -05003055 * amdgpu_device_reset_sriov - reset ASIC for SR-IOV vf
Monk Liu57406822017-10-25 16:37:02 +08003056 *
3057 * @adev: amdgpu device pointer
Monk Liu57406822017-10-25 16:37:02 +08003058 *
3059 * do VF FLR and reinitialize Asic
3060 * return 0 means successed otherwise failed
Alex Deuchere3ecdff2018-03-15 17:39:45 -05003061 */
3062static int amdgpu_device_reset_sriov(struct amdgpu_device *adev,
3063 bool from_hypervisor)
Monk Liu57406822017-10-25 16:37:02 +08003064{
3065 int r;
3066
3067 if (from_hypervisor)
3068 r = amdgpu_virt_request_full_gpu(adev, true);
3069 else
3070 r = amdgpu_virt_reset_gpu(adev);
3071 if (r)
3072 return r;
3073
3074 /* Resume IP prior to SMC */
Alex Deucher06ec9072017-12-14 15:02:39 -05003075 r = amdgpu_device_ip_reinit_early_sriov(adev);
Monk Liu57406822017-10-25 16:37:02 +08003076 if (r)
3077 goto error;
3078
3079 /* we need recover gart prior to run SMC/CP/SDMA resume */
Christian Königc1c7ce82017-10-16 16:50:32 +02003080 amdgpu_gtt_mgr_recover(&adev->mman.bdev.man[TTM_PL_TT]);
Monk Liu57406822017-10-25 16:37:02 +08003081
3082 /* now we are okay to resume SMC/CP/SDMA */
Alex Deucher06ec9072017-12-14 15:02:39 -05003083 r = amdgpu_device_ip_reinit_late_sriov(adev);
Monk Liuc41d1cf2017-12-25 11:59:27 +08003084 amdgpu_virt_release_full_gpu(adev, true);
Monk Liu57406822017-10-25 16:37:02 +08003085 if (r)
3086 goto error;
3087
3088 amdgpu_irq_gpu_reset_resume_helper(adev);
3089 r = amdgpu_ib_ring_tests(adev);
Monk Liuc41d1cf2017-12-25 11:59:27 +08003090
3091 if (!r && adev->virt.gim_feature & AMDGIM_FEATURE_GIM_FLR_VRAMLOST) {
3092 atomic_inc(&adev->vram_lost_counter);
3093 r = amdgpu_device_handle_vram_lost(adev);
3094 }
Monk Liu57406822017-10-25 16:37:02 +08003095
3096error:
Monk Liu57406822017-10-25 16:37:02 +08003097
3098 return r;
3099}
3100
3101/**
Alex Deucher5f152b52017-12-15 16:40:49 -05003102 * amdgpu_device_gpu_recover - reset the asic and recover scheduler
Monk Liu57406822017-10-25 16:37:02 +08003103 *
3104 * @adev: amdgpu device pointer
3105 * @job: which job trigger hang
Andrey Grodzovskydcebf022017-12-12 14:09:30 -05003106 * @force forces reset regardless of amdgpu_gpu_recovery
Monk Liu57406822017-10-25 16:37:02 +08003107 *
3108 * Attempt to reset the GPU if it has hung (all asics).
3109 * Returns 0 for success or an error on failure.
3110 */
Alex Deucher5f152b52017-12-15 16:40:49 -05003111int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
3112 struct amdgpu_job *job, bool force)
Monk Liu57406822017-10-25 16:37:02 +08003113{
3114 struct drm_atomic_state *state = NULL;
Monk Liu57406822017-10-25 16:37:02 +08003115 int i, r, resched;
3116
Andrey Grodzovsky54bc1392018-01-19 17:23:08 -05003117 if (!force && !amdgpu_device_ip_check_soft_reset(adev)) {
Monk Liu57406822017-10-25 16:37:02 +08003118 DRM_INFO("No hardware hang detected. Did some blocks stall?\n");
3119 return 0;
3120 }
3121
Andrey Grodzovskydcebf022017-12-12 14:09:30 -05003122 if (!force && (amdgpu_gpu_recovery == 0 ||
3123 (amdgpu_gpu_recovery == -1 && !amdgpu_sriov_vf(adev)))) {
3124 DRM_INFO("GPU recovery disabled.\n");
3125 return 0;
3126 }
3127
Monk Liu57406822017-10-25 16:37:02 +08003128 dev_info(adev->dev, "GPU reset begin!\n");
3129
Monk Liu13a752e2017-10-17 15:11:12 +08003130 mutex_lock(&adev->lock_reset);
Monk Liu57406822017-10-25 16:37:02 +08003131 atomic_inc(&adev->gpu_reset_counter);
Monk Liu13a752e2017-10-17 15:11:12 +08003132 adev->in_gpu_reset = 1;
Monk Liu57406822017-10-25 16:37:02 +08003133
3134 /* block TTM */
3135 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
Monk Liu71182662017-12-25 15:14:58 +08003136
Monk Liu57406822017-10-25 16:37:02 +08003137 /* store modesetting */
3138 if (amdgpu_device_has_dc_support(adev))
3139 state = drm_atomic_helper_suspend(adev->ddev);
3140
Monk Liu71182662017-12-25 15:14:58 +08003141 /* block all schedulers and reset given job's ring */
Monk Liu57406822017-10-25 16:37:02 +08003142 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
3143 struct amdgpu_ring *ring = adev->rings[i];
3144
3145 if (!ring || !ring->sched.thread)
3146 continue;
3147
Monk Liu71182662017-12-25 15:14:58 +08003148 kthread_park(ring->sched.thread);
3149
Monk Liu57406822017-10-25 16:37:02 +08003150 if (job && job->ring->idx != i)
3151 continue;
3152
Lucas Stach1b1f42d2017-12-06 17:49:39 +01003153 drm_sched_hw_job_reset(&ring->sched, &job->base);
Monk Liu57406822017-10-25 16:37:02 +08003154
3155 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
3156 amdgpu_fence_driver_force_completion(ring);
3157 }
3158
3159 if (amdgpu_sriov_vf(adev))
Monk Liuc41d1cf2017-12-25 11:59:27 +08003160 r = amdgpu_device_reset_sriov(adev, job ? false : true);
Monk Liu57406822017-10-25 16:37:02 +08003161 else
Monk Liuc41d1cf2017-12-25 11:59:27 +08003162 r = amdgpu_device_reset(adev);
Monk Liu57406822017-10-25 16:37:02 +08003163
Monk Liu71182662017-12-25 15:14:58 +08003164 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
3165 struct amdgpu_ring *ring = adev->rings[i];
Chunming Zhou51687752017-04-24 17:09:15 +08003166
Monk Liu71182662017-12-25 15:14:58 +08003167 if (!ring || !ring->sched.thread)
3168 continue;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003169
Monk Liu71182662017-12-25 15:14:58 +08003170 /* only need recovery sched of the given job's ring
3171 * or all rings (in the case @job is NULL)
3172 * after above amdgpu_reset accomplished
3173 */
3174 if ((!job || job->ring->idx == i) && !r)
Lucas Stach1b1f42d2017-12-06 17:49:39 +01003175 drm_sched_job_recovery(&ring->sched);
Monk Liu57406822017-10-25 16:37:02 +08003176
Monk Liu71182662017-12-25 15:14:58 +08003177 kthread_unpark(ring->sched.thread);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003178 }
3179
Harry Wentland45622362017-09-12 15:58:20 -04003180 if (amdgpu_device_has_dc_support(adev)) {
Monk Liu57406822017-10-25 16:37:02 +08003181 if (drm_atomic_helper_resume(adev->ddev, state))
3182 dev_info(adev->dev, "drm resume failed:%d\n", r);
Monk Liu57406822017-10-25 16:37:02 +08003183 } else {
Harry Wentland45622362017-09-12 15:58:20 -04003184 drm_helper_resume_force_mode(adev->ddev);
Monk Liu57406822017-10-25 16:37:02 +08003185 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003186
3187 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
Monk Liu57406822017-10-25 16:37:02 +08003188
Gavin Wan89041942017-06-23 13:55:15 -04003189 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003190 /* bad news, how to tell it to userspace ? */
Monk Liu57406822017-10-25 16:37:02 +08003191 dev_info(adev->dev, "GPU reset(%d) failed\n", atomic_read(&adev->gpu_reset_counter));
3192 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, r);
3193 } else {
3194 dev_info(adev->dev, "GPU reset(%d) successed!\n",atomic_read(&adev->gpu_reset_counter));
Gavin Wan89041942017-06-23 13:55:15 -04003195 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003196
Gavin Wan89041942017-06-23 13:55:15 -04003197 amdgpu_vf_error_trans_all(adev);
Monk Liu13a752e2017-10-17 15:11:12 +08003198 adev->in_gpu_reset = 0;
3199 mutex_unlock(&adev->lock_reset);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003200 return r;
3201}
3202
Alex Deuchere3ecdff2018-03-15 17:39:45 -05003203/**
3204 * amdgpu_device_get_pcie_info - fence pcie info about the PCIE slot
3205 *
3206 * @adev: amdgpu_device pointer
3207 *
3208 * Fetchs and stores in the driver the PCIE capabilities (gen speed
3209 * and lanes) of the slot the device is in. Handles APUs and
3210 * virtualized environments where PCIE config space may not be available.
3211 */
Alex Deucher5494d862018-03-09 15:14:11 -05003212static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev)
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003213{
3214 u32 mask;
3215 int ret;
3216
Alex Deuchercd474ba2016-02-04 10:21:23 -05003217 if (amdgpu_pcie_gen_cap)
3218 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
3219
3220 if (amdgpu_pcie_lane_cap)
3221 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
3222
3223 /* covers APUs as well */
3224 if (pci_is_root_bus(adev->pdev->bus)) {
3225 if (adev->pm.pcie_gen_mask == 0)
3226 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
3227 if (adev->pm.pcie_mlw_mask == 0)
3228 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003229 return;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003230 }
Alex Deuchercd474ba2016-02-04 10:21:23 -05003231
3232 if (adev->pm.pcie_gen_mask == 0) {
3233 ret = drm_pcie_get_speed_cap_mask(adev->ddev, &mask);
3234 if (!ret) {
3235 adev->pm.pcie_gen_mask = (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
3236 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
3237 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
3238
3239 if (mask & DRM_PCIE_SPEED_25)
3240 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
3241 if (mask & DRM_PCIE_SPEED_50)
3242 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2;
3243 if (mask & DRM_PCIE_SPEED_80)
3244 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3;
3245 } else {
3246 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
3247 }
3248 }
3249 if (adev->pm.pcie_mlw_mask == 0) {
3250 ret = drm_pcie_get_max_link_width(adev->ddev, &mask);
3251 if (!ret) {
3252 switch (mask) {
3253 case 32:
3254 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
3255 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
3256 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3257 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3258 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3259 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3260 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3261 break;
3262 case 16:
3263 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
3264 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3265 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3266 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3267 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3268 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3269 break;
3270 case 12:
3271 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3272 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3273 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3274 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3275 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3276 break;
3277 case 8:
3278 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3279 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3280 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3281 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3282 break;
3283 case 4:
3284 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3285 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3286 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3287 break;
3288 case 2:
3289 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3290 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3291 break;
3292 case 1:
3293 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
3294 break;
3295 default:
3296 break;
3297 }
3298 } else {
3299 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003300 }
3301 }
3302}
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003303