blob: 565f766a264d25e3b6063f34f24def3b18a7de62 [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",
Feifei Xu8fab8062017-10-19 17:04:54 +080086 "VEGA12",
Chunming Zhou2ca8a5d2016-12-07 17:31:19 +080087 "RAVEN",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040088 "LAST",
89};
90
Alex Deucher5494d862018-03-09 15:14:11 -050091static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev);
92
Alex Deuchere3ecdff2018-03-15 17:39:45 -050093/**
94 * amdgpu_device_is_px - Is the device is a dGPU with HG/PX power control
95 *
96 * @dev: drm_device pointer
97 *
98 * Returns true if the device is a dGPU with HG/PX power control,
99 * otherwise return false.
100 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400101bool amdgpu_device_is_px(struct drm_device *dev)
102{
103 struct amdgpu_device *adev = dev->dev_private;
104
Jammy Zhou2f7d10b2015-07-22 11:29:01 +0800105 if (adev->flags & AMD_IS_PX)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400106 return true;
107 return false;
108}
109
110/*
111 * MMIO register access helper functions.
112 */
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500113/**
114 * amdgpu_mm_rreg - read a memory mapped IO register
115 *
116 * @adev: amdgpu_device pointer
117 * @reg: dword aligned register offset
118 * @acc_flags: access flags which require special behavior
119 *
120 * Returns the 32 bit value from the offset specified.
121 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400122uint32_t amdgpu_mm_rreg(struct amdgpu_device *adev, uint32_t reg,
Monk Liu15d72fd2017-01-25 15:07:40 +0800123 uint32_t acc_flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400124{
Tom St Denisf4b373f2016-05-31 08:02:27 -0400125 uint32_t ret;
126
pding43ca8ef2017-10-13 15:38:35 +0800127 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev))
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800128 return amdgpu_virt_kiq_rreg(adev, reg);
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800129
Monk Liu15d72fd2017-01-25 15:07:40 +0800130 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
Tom St Denisf4b373f2016-05-31 08:02:27 -0400131 ret = readl(((void __iomem *)adev->rmmio) + (reg * 4));
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400132 else {
133 unsigned long flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400134
135 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
136 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
137 ret = readl(((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
138 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400139 }
Tom St Denisf4b373f2016-05-31 08:02:27 -0400140 trace_amdgpu_mm_rreg(adev->pdev->device, reg, ret);
141 return ret;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400142}
143
Monk Liu421a2a32018-01-04 18:13:20 +0800144/*
145 * MMIO register read with bytes helper functions
146 * @offset:bytes offset from MMIO start
147 *
148*/
149
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500150/**
151 * amdgpu_mm_rreg8 - read a memory mapped IO register
152 *
153 * @adev: amdgpu_device pointer
154 * @offset: byte aligned register offset
155 *
156 * Returns the 8 bit value from the offset specified.
157 */
Monk Liu421a2a32018-01-04 18:13:20 +0800158uint8_t amdgpu_mm_rreg8(struct amdgpu_device *adev, uint32_t offset) {
159 if (offset < adev->rmmio_size)
160 return (readb(adev->rmmio + offset));
161 BUG();
162}
163
164/*
165 * MMIO register write with bytes helper functions
166 * @offset:bytes offset from MMIO start
167 * @value: the value want to be written to the register
168 *
169*/
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500170/**
171 * amdgpu_mm_wreg8 - read a memory mapped IO register
172 *
173 * @adev: amdgpu_device pointer
174 * @offset: byte aligned register offset
175 * @value: 8 bit value to write
176 *
177 * Writes the value specified to the offset specified.
178 */
Monk Liu421a2a32018-01-04 18:13:20 +0800179void amdgpu_mm_wreg8(struct amdgpu_device *adev, uint32_t offset, uint8_t value) {
180 if (offset < adev->rmmio_size)
181 writeb(value, adev->rmmio + offset);
182 else
183 BUG();
184}
185
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500186/**
187 * amdgpu_mm_wreg - write to a memory mapped IO register
188 *
189 * @adev: amdgpu_device pointer
190 * @reg: dword aligned register offset
191 * @v: 32 bit value to write to the register
192 * @acc_flags: access flags which require special behavior
193 *
194 * Writes the value specified to the offset specified.
195 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400196void amdgpu_mm_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v,
Monk Liu15d72fd2017-01-25 15:07:40 +0800197 uint32_t acc_flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400198{
Tom St Denisf4b373f2016-05-31 08:02:27 -0400199 trace_amdgpu_mm_wreg(adev->pdev->device, reg, v);
Monk Liu4e99a442016-03-31 13:26:59 +0800200
Ken Wang47ed4e12017-07-04 13:11:52 +0800201 if (adev->asic_type >= CHIP_VEGA10 && reg == 0) {
202 adev->last_mm_index = v;
203 }
204
pding43ca8ef2017-10-13 15:38:35 +0800205 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev))
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800206 return amdgpu_virt_kiq_wreg(adev, reg, v);
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800207
Monk Liu15d72fd2017-01-25 15:07:40 +0800208 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400209 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
210 else {
211 unsigned long flags;
212
213 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
214 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
215 writel(v, ((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
216 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
217 }
Ken Wang47ed4e12017-07-04 13:11:52 +0800218
219 if (adev->asic_type >= CHIP_VEGA10 && reg == 1 && adev->last_mm_index == 0x5702C) {
220 udelay(500);
221 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400222}
223
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500224/**
225 * amdgpu_io_rreg - read an IO register
226 *
227 * @adev: amdgpu_device pointer
228 * @reg: dword aligned register offset
229 *
230 * Returns the 32 bit value from the offset specified.
231 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400232u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 reg)
233{
234 if ((reg * 4) < adev->rio_mem_size)
235 return ioread32(adev->rio_mem + (reg * 4));
236 else {
237 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
238 return ioread32(adev->rio_mem + (mmMM_DATA * 4));
239 }
240}
241
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500242/**
243 * amdgpu_io_wreg - write to an IO register
244 *
245 * @adev: amdgpu_device pointer
246 * @reg: dword aligned register offset
247 * @v: 32 bit value to write to the register
248 *
249 * Writes the value specified to the offset specified.
250 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400251void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
252{
Ken Wang47ed4e12017-07-04 13:11:52 +0800253 if (adev->asic_type >= CHIP_VEGA10 && reg == 0) {
254 adev->last_mm_index = v;
255 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400256
257 if ((reg * 4) < adev->rio_mem_size)
258 iowrite32(v, adev->rio_mem + (reg * 4));
259 else {
260 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
261 iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
262 }
Ken Wang47ed4e12017-07-04 13:11:52 +0800263
264 if (adev->asic_type >= CHIP_VEGA10 && reg == 1 && adev->last_mm_index == 0x5702C) {
265 udelay(500);
266 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400267}
268
269/**
270 * amdgpu_mm_rdoorbell - read a doorbell dword
271 *
272 * @adev: amdgpu_device pointer
273 * @index: doorbell index
274 *
275 * Returns the value in the doorbell aperture at the
276 * requested doorbell index (CIK).
277 */
278u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
279{
280 if (index < adev->doorbell.num_doorbells) {
281 return readl(adev->doorbell.ptr + index);
282 } else {
283 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
284 return 0;
285 }
286}
287
288/**
289 * amdgpu_mm_wdoorbell - write a doorbell dword
290 *
291 * @adev: amdgpu_device pointer
292 * @index: doorbell index
293 * @v: value to write
294 *
295 * Writes @v to the doorbell aperture at the
296 * requested doorbell index (CIK).
297 */
298void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
299{
300 if (index < adev->doorbell.num_doorbells) {
301 writel(v, adev->doorbell.ptr + index);
302 } else {
303 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
304 }
305}
306
307/**
Ken Wang832be402016-03-18 15:23:08 +0800308 * amdgpu_mm_rdoorbell64 - read a doorbell Qword
309 *
310 * @adev: amdgpu_device pointer
311 * @index: doorbell index
312 *
313 * Returns the value in the doorbell aperture at the
314 * requested doorbell index (VEGA10+).
315 */
316u64 amdgpu_mm_rdoorbell64(struct amdgpu_device *adev, u32 index)
317{
318 if (index < adev->doorbell.num_doorbells) {
319 return atomic64_read((atomic64_t *)(adev->doorbell.ptr + index));
320 } else {
321 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
322 return 0;
323 }
324}
325
326/**
327 * amdgpu_mm_wdoorbell64 - write a doorbell Qword
328 *
329 * @adev: amdgpu_device pointer
330 * @index: doorbell index
331 * @v: value to write
332 *
333 * Writes @v to the doorbell aperture at the
334 * requested doorbell index (VEGA10+).
335 */
336void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v)
337{
338 if (index < adev->doorbell.num_doorbells) {
339 atomic64_set((atomic64_t *)(adev->doorbell.ptr + index), v);
340 } else {
341 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
342 }
343}
344
345/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400346 * amdgpu_invalid_rreg - dummy reg read function
347 *
348 * @adev: amdgpu device pointer
349 * @reg: offset of register
350 *
351 * Dummy register read function. Used for register blocks
352 * that certain asics don't have (all asics).
353 * Returns the value in the register.
354 */
355static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
356{
357 DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
358 BUG();
359 return 0;
360}
361
362/**
363 * amdgpu_invalid_wreg - dummy reg write function
364 *
365 * @adev: amdgpu device pointer
366 * @reg: offset of register
367 * @v: value to write to the register
368 *
369 * Dummy register read function. Used for register blocks
370 * that certain asics don't have (all asics).
371 */
372static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
373{
374 DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
375 reg, v);
376 BUG();
377}
378
379/**
380 * amdgpu_block_invalid_rreg - dummy reg read function
381 *
382 * @adev: amdgpu device pointer
383 * @block: offset of instance
384 * @reg: offset of register
385 *
386 * Dummy register read function. Used for register blocks
387 * that certain asics don't have (all asics).
388 * Returns the value in the register.
389 */
390static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
391 uint32_t block, uint32_t reg)
392{
393 DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
394 reg, block);
395 BUG();
396 return 0;
397}
398
399/**
400 * amdgpu_block_invalid_wreg - dummy reg write function
401 *
402 * @adev: amdgpu device pointer
403 * @block: offset of instance
404 * @reg: offset of register
405 * @v: value to write to the register
406 *
407 * Dummy register read function. Used for register blocks
408 * that certain asics don't have (all asics).
409 */
410static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
411 uint32_t block,
412 uint32_t reg, uint32_t v)
413{
414 DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
415 reg, block, v);
416 BUG();
417}
418
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500419/**
420 * amdgpu_device_vram_scratch_init - allocate the VRAM scratch page
421 *
422 * @adev: amdgpu device pointer
423 *
424 * Allocates a scratch page of VRAM for use by various things in the
425 * driver.
426 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500427static int amdgpu_device_vram_scratch_init(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400428{
Christian Königa4a02772017-07-27 17:24:36 +0200429 return amdgpu_bo_create_kernel(adev, AMDGPU_GPU_PAGE_SIZE,
430 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
431 &adev->vram_scratch.robj,
432 &adev->vram_scratch.gpu_addr,
433 (void **)&adev->vram_scratch.ptr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400434}
435
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500436/**
437 * amdgpu_device_vram_scratch_fini - Free the VRAM scratch page
438 *
439 * @adev: amdgpu device pointer
440 *
441 * Frees the VRAM scratch page.
442 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500443static void amdgpu_device_vram_scratch_fini(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400444{
Christian König078af1a2017-07-27 17:43:00 +0200445 amdgpu_bo_free_kernel(&adev->vram_scratch.robj, NULL, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400446}
447
448/**
Alex Deucher9c3f2b52017-12-14 16:20:19 -0500449 * amdgpu_device_program_register_sequence - program an array of registers.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400450 *
451 * @adev: amdgpu_device pointer
452 * @registers: pointer to the register array
453 * @array_size: size of the register array
454 *
455 * Programs an array or registers with and and or masks.
456 * This is a helper for setting golden registers.
457 */
Alex Deucher9c3f2b52017-12-14 16:20:19 -0500458void amdgpu_device_program_register_sequence(struct amdgpu_device *adev,
459 const u32 *registers,
460 const u32 array_size)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400461{
462 u32 tmp, reg, and_mask, or_mask;
463 int i;
464
465 if (array_size % 3)
466 return;
467
468 for (i = 0; i < array_size; i +=3) {
469 reg = registers[i + 0];
470 and_mask = registers[i + 1];
471 or_mask = registers[i + 2];
472
473 if (and_mask == 0xffffffff) {
474 tmp = or_mask;
475 } else {
476 tmp = RREG32(reg);
477 tmp &= ~and_mask;
478 tmp |= or_mask;
479 }
480 WREG32(reg, tmp);
481 }
482}
483
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500484/**
485 * amdgpu_device_pci_config_reset - reset the GPU
486 *
487 * @adev: amdgpu_device pointer
488 *
489 * Resets the GPU using the pci config reset sequence.
490 * Only applicable to asics prior to vega10.
491 */
Alex Deucher8111c382017-12-14 16:22:53 -0500492void amdgpu_device_pci_config_reset(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400493{
494 pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
495}
496
497/*
498 * GPU doorbell aperture helpers function.
499 */
500/**
Alex Deucher06ec9072017-12-14 15:02:39 -0500501 * amdgpu_device_doorbell_init - Init doorbell driver information.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400502 *
503 * @adev: amdgpu_device pointer
504 *
505 * Init doorbell driver information (CIK)
506 * Returns 0 on success, error on failure.
507 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500508static int amdgpu_device_doorbell_init(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400509{
Christian König705e5192017-06-08 11:15:16 +0200510 /* No doorbell on SI hardware generation */
511 if (adev->asic_type < CHIP_BONAIRE) {
512 adev->doorbell.base = 0;
513 adev->doorbell.size = 0;
514 adev->doorbell.num_doorbells = 0;
515 adev->doorbell.ptr = NULL;
516 return 0;
517 }
518
Christian Königd6895ad2017-02-28 10:36:43 +0100519 if (pci_resource_flags(adev->pdev, 2) & IORESOURCE_UNSET)
520 return -EINVAL;
521
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400522 /* doorbell bar mapping */
523 adev->doorbell.base = pci_resource_start(adev->pdev, 2);
524 adev->doorbell.size = pci_resource_len(adev->pdev, 2);
525
Christian Königedf600d2016-05-03 15:54:54 +0200526 adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400527 AMDGPU_DOORBELL_MAX_ASSIGNMENT+1);
528 if (adev->doorbell.num_doorbells == 0)
529 return -EINVAL;
530
Christian König8972e5d2017-03-06 13:34:57 +0100531 adev->doorbell.ptr = ioremap(adev->doorbell.base,
532 adev->doorbell.num_doorbells *
533 sizeof(u32));
534 if (adev->doorbell.ptr == NULL)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400535 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400536
537 return 0;
538}
539
540/**
Alex Deucher06ec9072017-12-14 15:02:39 -0500541 * amdgpu_device_doorbell_fini - Tear down doorbell driver information.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400542 *
543 * @adev: amdgpu_device pointer
544 *
545 * Tear down doorbell driver information (CIK)
546 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500547static void amdgpu_device_doorbell_fini(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400548{
549 iounmap(adev->doorbell.ptr);
550 adev->doorbell.ptr = NULL;
551}
552
Alex Deucher22cb0162017-12-14 16:27:11 -0500553
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400554
555/*
Alex Deucher06ec9072017-12-14 15:02:39 -0500556 * amdgpu_device_wb_*()
Alex Xie455a7bc2017-05-08 21:36:03 -0400557 * Writeback is the method by which the GPU updates special pages in memory
Alex Xieea81a172017-05-08 13:41:11 -0400558 * with the status of certain GPU events (fences, ring pointers,etc.).
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400559 */
560
561/**
Alex Deucher06ec9072017-12-14 15:02:39 -0500562 * amdgpu_device_wb_fini - Disable Writeback and free memory
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400563 *
564 * @adev: amdgpu_device pointer
565 *
566 * Disables Writeback and frees the Writeback memory (all asics).
567 * Used at driver shutdown.
568 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500569static void amdgpu_device_wb_fini(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400570{
571 if (adev->wb.wb_obj) {
Alex Deuchera76ed482016-10-21 15:30:36 -0400572 amdgpu_bo_free_kernel(&adev->wb.wb_obj,
573 &adev->wb.gpu_addr,
574 (void **)&adev->wb.wb);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400575 adev->wb.wb_obj = NULL;
576 }
577}
578
579/**
Alex Deucher06ec9072017-12-14 15:02:39 -0500580 * amdgpu_device_wb_init- Init Writeback driver info and allocate memory
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400581 *
582 * @adev: amdgpu_device pointer
583 *
Alex Xie455a7bc2017-05-08 21:36:03 -0400584 * Initializes writeback and allocates writeback memory (all asics).
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400585 * Used at driver startup.
586 * Returns 0 on success or an -error on failure.
587 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500588static int amdgpu_device_wb_init(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400589{
590 int r;
591
592 if (adev->wb.wb_obj == NULL) {
Alex Deucher97407b62017-07-28 12:14:15 -0400593 /* AMDGPU_MAX_WB * sizeof(uint32_t) * 8 = AMDGPU_MAX_WB 256bit slots */
594 r = amdgpu_bo_create_kernel(adev, AMDGPU_MAX_WB * sizeof(uint32_t) * 8,
Alex Deuchera76ed482016-10-21 15:30:36 -0400595 PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
596 &adev->wb.wb_obj, &adev->wb.gpu_addr,
597 (void **)&adev->wb.wb);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400598 if (r) {
599 dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
600 return r;
601 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400602
603 adev->wb.num_wb = AMDGPU_MAX_WB;
604 memset(&adev->wb.used, 0, sizeof(adev->wb.used));
605
606 /* clear wb memory */
Monk Liu73469582017-12-29 17:06:41 +0800607 memset((char *)adev->wb.wb, 0, AMDGPU_MAX_WB * sizeof(uint32_t) * 8);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400608 }
609
610 return 0;
611}
612
613/**
Alex Deucher131b4b32017-12-14 16:03:43 -0500614 * amdgpu_device_wb_get - Allocate a wb entry
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400615 *
616 * @adev: amdgpu_device pointer
617 * @wb: wb index
618 *
619 * Allocate a wb slot for use by the driver (all asics).
620 * Returns 0 on success or -EINVAL on failure.
621 */
Alex Deucher131b4b32017-12-14 16:03:43 -0500622int amdgpu_device_wb_get(struct amdgpu_device *adev, u32 *wb)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400623{
624 unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
Alex Deucher97407b62017-07-28 12:14:15 -0400625
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400626 if (offset < adev->wb.num_wb) {
627 __set_bit(offset, adev->wb.used);
Monk Liu63ae07c2017-10-17 19:18:56 +0800628 *wb = offset << 3; /* convert to dw offset */
Monk Liu0915fdb2017-06-19 10:19:41 -0400629 return 0;
630 } else {
631 return -EINVAL;
632 }
633}
634
Ken Wang70142852016-03-18 15:08:49 +0800635/**
Alex Deucher131b4b32017-12-14 16:03:43 -0500636 * amdgpu_device_wb_free - Free a wb entry
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400637 *
638 * @adev: amdgpu_device pointer
639 * @wb: wb index
640 *
641 * Free a wb slot allocated for use by the driver (all asics)
642 */
Alex Deucher131b4b32017-12-14 16:03:43 -0500643void amdgpu_device_wb_free(struct amdgpu_device *adev, u32 wb)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400644{
Monk Liu73469582017-12-29 17:06:41 +0800645 wb >>= 3;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400646 if (wb < adev->wb.num_wb)
Monk Liu73469582017-12-29 17:06:41 +0800647 __clear_bit(wb, adev->wb.used);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400648}
649
650/**
Alex Deucher2543e282017-12-14 16:33:36 -0500651 * amdgpu_device_vram_location - try to find VRAM location
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500652 *
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400653 * @adev: amdgpu device structure holding all necessary informations
654 * @mc: memory controller structure holding memory informations
655 * @base: base address at which to put VRAM
656 *
Alex Xie455a7bc2017-05-08 21:36:03 -0400657 * Function will try to place VRAM at base address provided
Christian König3d647c82017-11-16 19:36:10 +0100658 * as parameter.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400659 */
Alex Deucher2543e282017-12-14 16:33:36 -0500660void amdgpu_device_vram_location(struct amdgpu_device *adev,
Christian König770d13b2018-01-12 14:52:22 +0100661 struct amdgpu_gmc *mc, u64 base)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400662{
663 uint64_t limit = (uint64_t)amdgpu_vram_limit << 20;
664
665 mc->vram_start = base;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400666 mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
667 if (limit && limit < mc->real_vram_size)
668 mc->real_vram_size = limit;
669 dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
670 mc->mc_vram_size >> 20, mc->vram_start,
671 mc->vram_end, mc->real_vram_size >> 20);
672}
673
674/**
Alex Deucher2543e282017-12-14 16:33:36 -0500675 * amdgpu_device_gart_location - try to find GTT location
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500676 *
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400677 * @adev: amdgpu device structure holding all necessary informations
678 * @mc: memory controller structure holding memory informations
679 *
680 * Function will place try to place GTT before or after VRAM.
681 *
682 * If GTT size is bigger than space left then we ajust GTT size.
683 * Thus function will never fails.
684 *
685 * FIXME: when reducing GTT size align new size on power of 2.
686 */
Alex Deucher2543e282017-12-14 16:33:36 -0500687void amdgpu_device_gart_location(struct amdgpu_device *adev,
Christian König770d13b2018-01-12 14:52:22 +0100688 struct amdgpu_gmc *mc)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400689{
690 u64 size_af, size_bf;
691
Christian König770d13b2018-01-12 14:52:22 +0100692 size_af = adev->gmc.mc_mask - mc->vram_end;
Christian Königed21c042017-07-06 22:26:05 +0200693 size_bf = mc->vram_start;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400694 if (size_bf > size_af) {
Christian König6f02a692017-07-07 11:56:59 +0200695 if (mc->gart_size > size_bf) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400696 dev_warn(adev->dev, "limiting GTT\n");
Christian König6f02a692017-07-07 11:56:59 +0200697 mc->gart_size = size_bf;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400698 }
Christian König6f02a692017-07-07 11:56:59 +0200699 mc->gart_start = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400700 } else {
Christian König6f02a692017-07-07 11:56:59 +0200701 if (mc->gart_size > size_af) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400702 dev_warn(adev->dev, "limiting GTT\n");
Christian König6f02a692017-07-07 11:56:59 +0200703 mc->gart_size = size_af;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400704 }
Christian Königb98f1b92017-11-16 20:12:51 +0100705 /* VCE doesn't like it when BOs cross a 4GB segment, so align
706 * the GART base on a 4GB boundary as well.
707 */
708 mc->gart_start = ALIGN(mc->vram_end + 1, 0x100000000ULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400709 }
Christian König6f02a692017-07-07 11:56:59 +0200710 mc->gart_end = mc->gart_start + mc->gart_size - 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400711 dev_info(adev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
Christian König6f02a692017-07-07 11:56:59 +0200712 mc->gart_size >> 20, mc->gart_start, mc->gart_end);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400713}
714
Christian Königd6895ad2017-02-28 10:36:43 +0100715/**
716 * amdgpu_device_resize_fb_bar - try to resize FB BAR
717 *
718 * @adev: amdgpu_device pointer
719 *
720 * Try to resize FB BAR to make all VRAM CPU accessible. We try very hard not
721 * to fail, but if any of the BARs is not accessible after the size we abort
722 * driver loading by returning -ENODEV.
723 */
724int amdgpu_device_resize_fb_bar(struct amdgpu_device *adev)
725{
Christian König770d13b2018-01-12 14:52:22 +0100726 u64 space_needed = roundup_pow_of_two(adev->gmc.real_vram_size);
Christian Königd6895ad2017-02-28 10:36:43 +0100727 u32 rbar_size = order_base_2(((space_needed >> 20) | 1)) - 1;
Christian König31b8ada2017-11-15 20:07:38 +0100728 struct pci_bus *root;
729 struct resource *res;
730 unsigned i;
Christian Königd6895ad2017-02-28 10:36:43 +0100731 u16 cmd;
732 int r;
733
pding0c03b912017-11-07 11:02:00 +0800734 /* Bypass for VF */
735 if (amdgpu_sriov_vf(adev))
736 return 0;
737
Christian König31b8ada2017-11-15 20:07:38 +0100738 /* Check if the root BUS has 64bit memory resources */
739 root = adev->pdev->bus;
740 while (root->parent)
741 root = root->parent;
742
743 pci_bus_for_each_resource(root, res, i) {
Christian König0ebb7c52018-01-07 10:18:57 +0100744 if (res && res->flags & (IORESOURCE_MEM | IORESOURCE_MEM_64) &&
Christian König31b8ada2017-11-15 20:07:38 +0100745 res->start > 0x100000000ull)
746 break;
747 }
748
749 /* Trying to resize is pointless without a root hub window above 4GB */
750 if (!res)
751 return 0;
752
Christian Königd6895ad2017-02-28 10:36:43 +0100753 /* Disable memory decoding while we change the BAR addresses and size */
754 pci_read_config_word(adev->pdev, PCI_COMMAND, &cmd);
755 pci_write_config_word(adev->pdev, PCI_COMMAND,
756 cmd & ~PCI_COMMAND_MEMORY);
757
758 /* Free the VRAM and doorbell BAR, we most likely need to move both. */
Alex Deucher06ec9072017-12-14 15:02:39 -0500759 amdgpu_device_doorbell_fini(adev);
Christian Königd6895ad2017-02-28 10:36:43 +0100760 if (adev->asic_type >= CHIP_BONAIRE)
761 pci_release_resource(adev->pdev, 2);
762
763 pci_release_resource(adev->pdev, 0);
764
765 r = pci_resize_resource(adev->pdev, 0, rbar_size);
766 if (r == -ENOSPC)
767 DRM_INFO("Not enough PCI address space for a large BAR.");
768 else if (r && r != -ENOTSUPP)
769 DRM_ERROR("Problem resizing BAR0 (%d).", r);
770
771 pci_assign_unassigned_bus_resources(adev->pdev->bus);
772
773 /* When the doorbell or fb BAR isn't available we have no chance of
774 * using the device.
775 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500776 r = amdgpu_device_doorbell_init(adev);
Christian Königd6895ad2017-02-28 10:36:43 +0100777 if (r || (pci_resource_flags(adev->pdev, 0) & IORESOURCE_UNSET))
778 return -ENODEV;
779
780 pci_write_config_word(adev->pdev, PCI_COMMAND, cmd);
781
782 return 0;
783}
Horace Chena05502e2017-09-29 14:41:57 +0800784
785/*
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400786 * GPU helpers function.
787 */
788/**
Alex Deucher39c640c2017-12-15 16:22:11 -0500789 * amdgpu_device_need_post - check if the hw need post or not
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400790 *
791 * @adev: amdgpu_device pointer
792 *
Jim Quc836fec2017-02-10 15:59:59 +0800793 * Check if the asic has been initialized (all asics) at driver startup
794 * or post is needed if hw reset is performed.
795 * Returns true if need or false if not.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400796 */
Alex Deucher39c640c2017-12-15 16:22:11 -0500797bool amdgpu_device_need_post(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400798{
799 uint32_t reg;
800
Monk Liubec86372016-09-14 19:38:08 +0800801 if (amdgpu_sriov_vf(adev))
802 return false;
803
804 if (amdgpu_passthrough(adev)) {
Monk Liu1da2c322016-11-11 11:24:29 +0800805 /* for FIJI: In whole GPU pass-through virtualization case, after VM reboot
806 * some old smc fw still need driver do vPost otherwise gpu hang, while
807 * those smc fw version above 22.15 doesn't have this flaw, so we force
808 * vpost executed for smc version below 22.15
Monk Liubec86372016-09-14 19:38:08 +0800809 */
810 if (adev->asic_type == CHIP_FIJI) {
811 int err;
812 uint32_t fw_ver;
813 err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
814 /* force vPost if error occured */
815 if (err)
816 return true;
817
818 fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
Monk Liu1da2c322016-11-11 11:24:29 +0800819 if (fw_ver < 0x00160e00)
820 return true;
Monk Liubec86372016-09-14 19:38:08 +0800821 }
Monk Liubec86372016-09-14 19:38:08 +0800822 }
pding91fe77e2017-10-19 09:38:39 +0800823
824 if (adev->has_hw_reset) {
825 adev->has_hw_reset = false;
826 return true;
827 }
828
829 /* bios scratch used on CIK+ */
830 if (adev->asic_type >= CHIP_BONAIRE)
831 return amdgpu_atombios_scratch_need_asic_init(adev);
832
833 /* check MEM_SIZE for older asics */
834 reg = amdgpu_asic_get_config_memsize(adev);
835
836 if ((reg != 0) && (reg != 0xffffffff))
837 return false;
838
839 return true;
Monk Liubec86372016-09-14 19:38:08 +0800840}
841
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400842/* if we get transitioned to only one device, take VGA back */
843/**
Alex Deucher06ec9072017-12-14 15:02:39 -0500844 * amdgpu_device_vga_set_decode - enable/disable vga decode
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400845 *
846 * @cookie: amdgpu_device pointer
847 * @state: enable/disable vga decode
848 *
849 * Enable/disable vga decode (all asics).
850 * Returns VGA resource flags.
851 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500852static unsigned int amdgpu_device_vga_set_decode(void *cookie, bool state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400853{
854 struct amdgpu_device *adev = cookie;
855 amdgpu_asic_set_vga_state(adev, state);
856 if (state)
857 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
858 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
859 else
860 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
861}
862
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500863/**
864 * amdgpu_device_check_block_size - validate the vm block size
865 *
866 * @adev: amdgpu_device pointer
867 *
868 * Validates the vm block size specified via module parameter.
869 * The vm block size defines number of bits in page table versus page directory,
870 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
871 * page table and the remaining bits are in the page directory.
872 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500873static void amdgpu_device_check_block_size(struct amdgpu_device *adev)
Chunming Zhoua1adf8b2017-03-27 11:36:57 +0800874{
875 /* defines number of bits in page table versus page directory,
876 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
877 * page table and the remaining bits are in the page directory */
Junwei Zhangbab4fee2017-04-05 13:54:56 +0800878 if (amdgpu_vm_block_size == -1)
879 return;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +0800880
Junwei Zhangbab4fee2017-04-05 13:54:56 +0800881 if (amdgpu_vm_block_size < 9) {
Chunming Zhoua1adf8b2017-03-27 11:36:57 +0800882 dev_warn(adev->dev, "VM page table size (%d) too small\n",
883 amdgpu_vm_block_size);
Christian König97489122017-11-27 16:22:05 +0100884 amdgpu_vm_block_size = -1;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +0800885 }
Chunming Zhoua1adf8b2017-03-27 11:36:57 +0800886}
887
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500888/**
889 * amdgpu_device_check_vm_size - validate the vm size
890 *
891 * @adev: amdgpu_device pointer
892 *
893 * Validates the vm size in GB specified via module parameter.
894 * The VM size is the size of the GPU virtual memory space in GB.
895 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500896static void amdgpu_device_check_vm_size(struct amdgpu_device *adev)
Zhang, Jerry83ca1452017-03-29 16:08:31 +0800897{
Alex Deucher64dab072017-06-15 18:20:09 -0400898 /* no need to check the default value */
899 if (amdgpu_vm_size == -1)
900 return;
901
Zhang, Jerry83ca1452017-03-29 16:08:31 +0800902 if (amdgpu_vm_size < 1) {
903 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
904 amdgpu_vm_size);
Christian Königf3368122017-11-23 12:57:18 +0100905 amdgpu_vm_size = -1;
Zhang, Jerry83ca1452017-03-29 16:08:31 +0800906 }
Zhang, Jerry83ca1452017-03-29 16:08:31 +0800907}
908
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400909/**
Alex Deucher06ec9072017-12-14 15:02:39 -0500910 * amdgpu_device_check_arguments - validate module params
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400911 *
912 * @adev: amdgpu_device pointer
913 *
914 * Validates certain module parameters and updates
915 * the associated values used by the driver (all asics).
916 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500917static void amdgpu_device_check_arguments(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400918{
Chunming Zhou5b011232015-12-10 17:34:33 +0800919 if (amdgpu_sched_jobs < 4) {
920 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
921 amdgpu_sched_jobs);
922 amdgpu_sched_jobs = 4;
Alex Deucher76117502017-06-21 12:31:41 -0400923 } else if (!is_power_of_2(amdgpu_sched_jobs)){
Chunming Zhou5b011232015-12-10 17:34:33 +0800924 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
925 amdgpu_sched_jobs);
926 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
927 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400928
Alex Deucher83e74db2017-08-21 11:58:25 -0400929 if (amdgpu_gart_size != -1 && amdgpu_gart_size < 32) {
Christian Königf9321cc2017-07-07 13:44:05 +0200930 /* gart size must be greater or equal to 32M */
931 dev_warn(adev->dev, "gart size (%d) too small\n",
932 amdgpu_gart_size);
Alex Deucher83e74db2017-08-21 11:58:25 -0400933 amdgpu_gart_size = -1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400934 }
935
Christian König36d38372017-07-07 13:17:45 +0200936 if (amdgpu_gtt_size != -1 && amdgpu_gtt_size < 32) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400937 /* gtt size must be greater or equal to 32M */
Christian König36d38372017-07-07 13:17:45 +0200938 dev_warn(adev->dev, "gtt size (%d) too small\n",
939 amdgpu_gtt_size);
940 amdgpu_gtt_size = -1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400941 }
942
Roger Hed07f14b2017-08-15 16:05:59 +0800943 /* valid range is between 4 and 9 inclusive */
944 if (amdgpu_vm_fragment_size != -1 &&
945 (amdgpu_vm_fragment_size > 9 || amdgpu_vm_fragment_size < 4)) {
946 dev_warn(adev->dev, "valid range is between 4 and 9\n");
947 amdgpu_vm_fragment_size = -1;
948 }
949
Alex Deucher06ec9072017-12-14 15:02:39 -0500950 amdgpu_device_check_vm_size(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400951
Alex Deucher06ec9072017-12-14 15:02:39 -0500952 amdgpu_device_check_block_size(adev);
Christian König6a7f76e2016-08-24 15:51:49 +0200953
jimqu526bae32016-11-07 09:53:10 +0800954 if (amdgpu_vram_page_split != -1 && (amdgpu_vram_page_split < 16 ||
Alex Deucher76117502017-06-21 12:31:41 -0400955 !is_power_of_2(amdgpu_vram_page_split))) {
Christian König6a7f76e2016-08-24 15:51:49 +0200956 dev_warn(adev->dev, "invalid VRAM page split (%d)\n",
957 amdgpu_vram_page_split);
958 amdgpu_vram_page_split = 1024;
959 }
Andrey Grodzovsky88546952017-12-13 14:36:53 -0500960
961 if (amdgpu_lockup_timeout == 0) {
962 dev_warn(adev->dev, "lockup_timeout msut be > 0, adjusting to 10000\n");
963 amdgpu_lockup_timeout = 10000;
964 }
Alex Deucher19aede72018-03-09 15:06:35 -0500965
966 adev->firmware.load_type = amdgpu_ucode_get_load_type(adev, amdgpu_fw_load_type);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400967}
968
969/**
970 * amdgpu_switcheroo_set_state - set switcheroo state
971 *
972 * @pdev: pci dev pointer
Lukas Wunner16944672015-09-05 11:17:35 +0200973 * @state: vga_switcheroo state
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400974 *
975 * Callback for the switcheroo driver. Suspends or resumes the
976 * the asics before or after it is powered up using ACPI methods.
977 */
978static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
979{
980 struct drm_device *dev = pci_get_drvdata(pdev);
981
982 if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
983 return;
984
985 if (state == VGA_SWITCHEROO_ON) {
Joe Perches7ca85292017-02-28 04:55:52 -0800986 pr_info("amdgpu: switched on\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400987 /* don't suspend or resume card normally */
988 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
989
Alex Deucher810ddc32016-08-23 13:25:49 -0400990 amdgpu_device_resume(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400991
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400992 dev->switch_power_state = DRM_SWITCH_POWER_ON;
993 drm_kms_helper_poll_enable(dev);
994 } else {
Joe Perches7ca85292017-02-28 04:55:52 -0800995 pr_info("amdgpu: switched off\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400996 drm_kms_helper_poll_disable(dev);
997 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Alex Deucher810ddc32016-08-23 13:25:49 -0400998 amdgpu_device_suspend(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400999 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1000 }
1001}
1002
1003/**
1004 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1005 *
1006 * @pdev: pci dev pointer
1007 *
1008 * Callback for the switcheroo driver. Check of the switcheroo
1009 * state can be changed.
1010 * Returns true if the state can be changed, false if not.
1011 */
1012static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1013{
1014 struct drm_device *dev = pci_get_drvdata(pdev);
1015
1016 /*
1017 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1018 * locking inversion with the driver load path. And the access here is
1019 * completely racy anyway. So don't bother with locking for now.
1020 */
1021 return dev->open_count == 0;
1022}
1023
1024static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1025 .set_gpu_state = amdgpu_switcheroo_set_state,
1026 .reprobe = NULL,
1027 .can_switch = amdgpu_switcheroo_can_switch,
1028};
1029
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001030/**
1031 * amdgpu_device_ip_set_clockgating_state - set the CG state
1032 *
1033 * @adev: amdgpu_device pointer
1034 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1035 * @state: clockgating state (gate or ungate)
1036 *
1037 * Sets the requested clockgating state for all instances of
1038 * the hardware IP specified.
1039 * Returns the error code from the last instance.
1040 */
Alex Deucher2990a1f2017-12-15 16:18:00 -05001041int amdgpu_device_ip_set_clockgating_state(struct amdgpu_device *adev,
1042 enum amd_ip_block_type block_type,
1043 enum amd_clockgating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001044{
1045 int i, r = 0;
1046
1047 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001048 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001049 continue;
Rex Zhuc7228652017-02-22 15:33:46 +08001050 if (adev->ip_blocks[i].version->type != block_type)
1051 continue;
1052 if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
1053 continue;
1054 r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
1055 (void *)adev, state);
1056 if (r)
1057 DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
1058 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001059 }
1060 return r;
1061}
1062
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001063/**
1064 * amdgpu_device_ip_set_powergating_state - set the PG state
1065 *
1066 * @adev: amdgpu_device pointer
1067 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1068 * @state: powergating state (gate or ungate)
1069 *
1070 * Sets the requested powergating state for all instances of
1071 * the hardware IP specified.
1072 * Returns the error code from the last instance.
1073 */
Alex Deucher2990a1f2017-12-15 16:18:00 -05001074int amdgpu_device_ip_set_powergating_state(struct amdgpu_device *adev,
1075 enum amd_ip_block_type block_type,
1076 enum amd_powergating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001077{
1078 int i, r = 0;
1079
1080 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001081 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001082 continue;
Rex Zhuc7228652017-02-22 15:33:46 +08001083 if (adev->ip_blocks[i].version->type != block_type)
1084 continue;
1085 if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
1086 continue;
1087 r = adev->ip_blocks[i].version->funcs->set_powergating_state(
1088 (void *)adev, state);
1089 if (r)
1090 DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
1091 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001092 }
1093 return r;
1094}
1095
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001096/**
1097 * amdgpu_device_ip_get_clockgating_state - get the CG state
1098 *
1099 * @adev: amdgpu_device pointer
1100 * @flags: clockgating feature flags
1101 *
1102 * Walks the list of IPs on the device and updates the clockgating
1103 * flags for each IP.
1104 * Updates @flags with the feature flags for each hardware IP where
1105 * clockgating is enabled.
1106 */
Alex Deucher2990a1f2017-12-15 16:18:00 -05001107void amdgpu_device_ip_get_clockgating_state(struct amdgpu_device *adev,
1108 u32 *flags)
Huang Rui6cb2d4e2017-01-05 18:44:41 +08001109{
1110 int i;
1111
1112 for (i = 0; i < adev->num_ip_blocks; i++) {
1113 if (!adev->ip_blocks[i].status.valid)
1114 continue;
1115 if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
1116 adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
1117 }
1118}
1119
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001120/**
1121 * amdgpu_device_ip_wait_for_idle - wait for idle
1122 *
1123 * @adev: amdgpu_device pointer
1124 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1125 *
1126 * Waits for the request hardware IP to be idle.
1127 * Returns 0 for success or a negative error code on failure.
1128 */
Alex Deucher2990a1f2017-12-15 16:18:00 -05001129int amdgpu_device_ip_wait_for_idle(struct amdgpu_device *adev,
1130 enum amd_ip_block_type block_type)
Alex Deucher5dbbb602016-06-23 11:41:04 -04001131{
1132 int i, r;
1133
1134 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001135 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001136 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001137 if (adev->ip_blocks[i].version->type == block_type) {
1138 r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -04001139 if (r)
1140 return r;
1141 break;
1142 }
1143 }
1144 return 0;
1145
1146}
1147
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001148/**
1149 * amdgpu_device_ip_is_idle - is the hardware IP idle
1150 *
1151 * @adev: amdgpu_device pointer
1152 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1153 *
1154 * Check if the hardware IP is idle or not.
1155 * Returns true if it the IP is idle, false if not.
1156 */
Alex Deucher2990a1f2017-12-15 16:18:00 -05001157bool amdgpu_device_ip_is_idle(struct amdgpu_device *adev,
1158 enum amd_ip_block_type block_type)
Alex Deucher5dbbb602016-06-23 11:41:04 -04001159{
1160 int i;
1161
1162 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001163 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001164 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001165 if (adev->ip_blocks[i].version->type == block_type)
1166 return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -04001167 }
1168 return true;
1169
1170}
1171
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001172/**
1173 * amdgpu_device_ip_get_ip_block - get a hw IP pointer
1174 *
1175 * @adev: amdgpu_device pointer
1176 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1177 *
1178 * Returns a pointer to the hardware IP block structure
1179 * if it exists for the asic, otherwise NULL.
1180 */
Alex Deucher2990a1f2017-12-15 16:18:00 -05001181struct amdgpu_ip_block *
1182amdgpu_device_ip_get_ip_block(struct amdgpu_device *adev,
1183 enum amd_ip_block_type type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001184{
1185 int i;
1186
1187 for (i = 0; i < adev->num_ip_blocks; i++)
Alex Deuchera1255102016-10-13 17:41:13 -04001188 if (adev->ip_blocks[i].version->type == type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001189 return &adev->ip_blocks[i];
1190
1191 return NULL;
1192}
1193
1194/**
Alex Deucher2990a1f2017-12-15 16:18:00 -05001195 * amdgpu_device_ip_block_version_cmp
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001196 *
1197 * @adev: amdgpu_device pointer
yanyang15fc3aee2015-05-22 14:39:35 -04001198 * @type: enum amd_ip_block_type
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001199 * @major: major version
1200 * @minor: minor version
1201 *
1202 * return 0 if equal or greater
1203 * return 1 if smaller or the ip_block doesn't exist
1204 */
Alex Deucher2990a1f2017-12-15 16:18:00 -05001205int amdgpu_device_ip_block_version_cmp(struct amdgpu_device *adev,
1206 enum amd_ip_block_type type,
1207 u32 major, u32 minor)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001208{
Alex Deucher2990a1f2017-12-15 16:18:00 -05001209 struct amdgpu_ip_block *ip_block = amdgpu_device_ip_get_ip_block(adev, type);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001210
Alex Deuchera1255102016-10-13 17:41:13 -04001211 if (ip_block && ((ip_block->version->major > major) ||
1212 ((ip_block->version->major == major) &&
1213 (ip_block->version->minor >= minor))))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001214 return 0;
1215
1216 return 1;
1217}
1218
Alex Deuchera1255102016-10-13 17:41:13 -04001219/**
Alex Deucher2990a1f2017-12-15 16:18:00 -05001220 * amdgpu_device_ip_block_add
Alex Deuchera1255102016-10-13 17:41:13 -04001221 *
1222 * @adev: amdgpu_device pointer
1223 * @ip_block_version: pointer to the IP to add
1224 *
1225 * Adds the IP block driver information to the collection of IPs
1226 * on the asic.
1227 */
Alex Deucher2990a1f2017-12-15 16:18:00 -05001228int amdgpu_device_ip_block_add(struct amdgpu_device *adev,
1229 const struct amdgpu_ip_block_version *ip_block_version)
Alex Deuchera1255102016-10-13 17:41:13 -04001230{
1231 if (!ip_block_version)
1232 return -EINVAL;
1233
Shaoyun Liue966a722018-02-01 16:45:26 -05001234 DRM_INFO("add ip block number %d <%s>\n", adev->num_ip_blocks,
Huang Ruia0bae352017-05-03 09:52:06 +08001235 ip_block_version->funcs->name);
1236
Alex Deuchera1255102016-10-13 17:41:13 -04001237 adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
1238
1239 return 0;
1240}
1241
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001242/**
1243 * amdgpu_device_enable_virtual_display - enable virtual display feature
1244 *
1245 * @adev: amdgpu_device pointer
1246 *
1247 * Enabled the virtual display feature if the user has enabled it via
1248 * the module parameter virtual_display. This feature provides a virtual
1249 * display hardware on headless boards or in virtualized environments.
1250 * This function parses and validates the configuration string specified by
1251 * the user and configues the virtual display configuration (number of
1252 * virtual connectors, crtcs, etc.) specified.
1253 */
Alex Deucher483ef982016-09-30 12:43:04 -04001254static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
Emily Deng9accf2f2016-08-10 16:01:25 +08001255{
1256 adev->enable_virtual_display = false;
1257
1258 if (amdgpu_virtual_display) {
1259 struct drm_device *ddev = adev->ddev;
1260 const char *pci_address_name = pci_name(ddev->pdev);
Emily Deng0f663562016-09-30 13:02:18 -04001261 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
Emily Deng9accf2f2016-08-10 16:01:25 +08001262
1263 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1264 pciaddstr_tmp = pciaddstr;
Emily Deng0f663562016-09-30 13:02:18 -04001265 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1266 pciaddname = strsep(&pciaddname_tmp, ",");
Yintian Tao967de2a2017-01-22 15:16:51 +08001267 if (!strcmp("all", pciaddname)
1268 || !strcmp(pci_address_name, pciaddname)) {
Emily Deng0f663562016-09-30 13:02:18 -04001269 long num_crtc;
1270 int res = -1;
1271
Emily Deng9accf2f2016-08-10 16:01:25 +08001272 adev->enable_virtual_display = true;
Emily Deng0f663562016-09-30 13:02:18 -04001273
1274 if (pciaddname_tmp)
1275 res = kstrtol(pciaddname_tmp, 10,
1276 &num_crtc);
1277
1278 if (!res) {
1279 if (num_crtc < 1)
1280 num_crtc = 1;
1281 if (num_crtc > 6)
1282 num_crtc = 6;
1283 adev->mode_info.num_crtc = num_crtc;
1284 } else {
1285 adev->mode_info.num_crtc = 1;
1286 }
Emily Deng9accf2f2016-08-10 16:01:25 +08001287 break;
1288 }
1289 }
1290
Emily Deng0f663562016-09-30 13:02:18 -04001291 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
1292 amdgpu_virtual_display, pci_address_name,
1293 adev->enable_virtual_display, adev->mode_info.num_crtc);
Emily Deng9accf2f2016-08-10 16:01:25 +08001294
1295 kfree(pciaddstr);
1296 }
1297}
1298
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001299/**
1300 * amdgpu_device_parse_gpu_info_fw - parse gpu info firmware
1301 *
1302 * @adev: amdgpu_device pointer
1303 *
1304 * Parses the asic configuration parameters specified in the gpu info
1305 * firmware and makes them availale to the driver for use in configuring
1306 * the asic.
1307 * Returns 0 on success, -EINVAL on failure.
1308 */
Alex Deuchere2a75f82017-04-27 16:58:01 -04001309static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev)
1310{
Alex Deuchere2a75f82017-04-27 16:58:01 -04001311 const char *chip_name;
1312 char fw_name[30];
1313 int err;
1314 const struct gpu_info_firmware_header_v1_0 *hdr;
1315
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001316 adev->firmware.gpu_info_fw = NULL;
1317
Alex Deuchere2a75f82017-04-27 16:58:01 -04001318 switch (adev->asic_type) {
1319 case CHIP_TOPAZ:
1320 case CHIP_TONGA:
1321 case CHIP_FIJI:
1322 case CHIP_POLARIS11:
1323 case CHIP_POLARIS10:
1324 case CHIP_POLARIS12:
1325 case CHIP_CARRIZO:
1326 case CHIP_STONEY:
1327#ifdef CONFIG_DRM_AMDGPU_SI
1328 case CHIP_VERDE:
1329 case CHIP_TAHITI:
1330 case CHIP_PITCAIRN:
1331 case CHIP_OLAND:
1332 case CHIP_HAINAN:
1333#endif
1334#ifdef CONFIG_DRM_AMDGPU_CIK
1335 case CHIP_BONAIRE:
1336 case CHIP_HAWAII:
1337 case CHIP_KAVERI:
1338 case CHIP_KABINI:
1339 case CHIP_MULLINS:
1340#endif
1341 default:
1342 return 0;
1343 case CHIP_VEGA10:
1344 chip_name = "vega10";
1345 break;
Alex Deucher2d2e5e72017-05-09 12:27:35 -04001346 case CHIP_RAVEN:
1347 chip_name = "raven";
1348 break;
Alex Deuchere2a75f82017-04-27 16:58:01 -04001349 }
1350
1351 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name);
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001352 err = request_firmware(&adev->firmware.gpu_info_fw, fw_name, adev->dev);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001353 if (err) {
1354 dev_err(adev->dev,
1355 "Failed to load gpu_info firmware \"%s\"\n",
1356 fw_name);
1357 goto out;
1358 }
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001359 err = amdgpu_ucode_validate(adev->firmware.gpu_info_fw);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001360 if (err) {
1361 dev_err(adev->dev,
1362 "Failed to validate gpu_info firmware \"%s\"\n",
1363 fw_name);
1364 goto out;
1365 }
1366
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001367 hdr = (const struct gpu_info_firmware_header_v1_0 *)adev->firmware.gpu_info_fw->data;
Alex Deuchere2a75f82017-04-27 16:58:01 -04001368 amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
1369
1370 switch (hdr->version_major) {
1371 case 1:
1372 {
1373 const struct gpu_info_firmware_v1_0 *gpu_info_fw =
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001374 (const struct gpu_info_firmware_v1_0 *)(adev->firmware.gpu_info_fw->data +
Alex Deuchere2a75f82017-04-27 16:58:01 -04001375 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1376
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001377 adev->gfx.config.max_shader_engines = le32_to_cpu(gpu_info_fw->gc_num_se);
1378 adev->gfx.config.max_cu_per_sh = le32_to_cpu(gpu_info_fw->gc_num_cu_per_sh);
1379 adev->gfx.config.max_sh_per_se = le32_to_cpu(gpu_info_fw->gc_num_sh_per_se);
1380 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 -04001381 adev->gfx.config.max_texture_channel_caches =
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001382 le32_to_cpu(gpu_info_fw->gc_num_tccs);
1383 adev->gfx.config.max_gprs = le32_to_cpu(gpu_info_fw->gc_num_gprs);
1384 adev->gfx.config.max_gs_threads = le32_to_cpu(gpu_info_fw->gc_num_max_gs_thds);
1385 adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gpu_info_fw->gc_gs_table_depth);
1386 adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gpu_info_fw->gc_gsprim_buff_depth);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001387 adev->gfx.config.double_offchip_lds_buf =
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001388 le32_to_cpu(gpu_info_fw->gc_double_offchip_lds_buffer);
1389 adev->gfx.cu_info.wave_front_size = le32_to_cpu(gpu_info_fw->gc_wave_size);
Hawking Zhang51fd0372017-06-09 22:30:52 +08001390 adev->gfx.cu_info.max_waves_per_simd =
1391 le32_to_cpu(gpu_info_fw->gc_max_waves_per_simd);
1392 adev->gfx.cu_info.max_scratch_slots_per_cu =
1393 le32_to_cpu(gpu_info_fw->gc_max_scratch_slots_per_cu);
1394 adev->gfx.cu_info.lds_size = le32_to_cpu(gpu_info_fw->gc_lds_size);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001395 break;
1396 }
1397 default:
1398 dev_err(adev->dev,
1399 "Unsupported gpu_info table %d\n", hdr->header.ucode_version);
1400 err = -EINVAL;
1401 goto out;
1402 }
1403out:
Alex Deuchere2a75f82017-04-27 16:58:01 -04001404 return err;
1405}
1406
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001407/**
1408 * amdgpu_device_ip_early_init - run early init for hardware IPs
1409 *
1410 * @adev: amdgpu_device pointer
1411 *
1412 * Early initialization pass for hardware IPs. The hardware IPs that make
1413 * up each asic are discovered each IP's early_init callback is run. This
1414 * is the first stage in initializing the asic.
1415 * Returns 0 on success, negative error code on failure.
1416 */
Alex Deucher06ec9072017-12-14 15:02:39 -05001417static int amdgpu_device_ip_early_init(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001418{
Alex Deucheraaa36a92015-04-20 17:31:14 -04001419 int i, r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001420
Alex Deucher483ef982016-09-30 12:43:04 -04001421 amdgpu_device_enable_virtual_display(adev);
Emily Denga6be7572016-08-08 11:37:50 +08001422
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001423 switch (adev->asic_type) {
Alex Deucheraaa36a92015-04-20 17:31:14 -04001424 case CHIP_TOPAZ:
1425 case CHIP_TONGA:
David Zhang48299f92015-07-08 01:05:16 +08001426 case CHIP_FIJI:
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001427 case CHIP_POLARIS11:
1428 case CHIP_POLARIS10:
Junwei Zhangc4642a42016-12-14 15:32:28 -05001429 case CHIP_POLARIS12:
Alex Deucheraaa36a92015-04-20 17:31:14 -04001430 case CHIP_CARRIZO:
Samuel Li39bb0c92015-10-08 16:31:43 -04001431 case CHIP_STONEY:
1432 if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
Alex Deucheraaa36a92015-04-20 17:31:14 -04001433 adev->family = AMDGPU_FAMILY_CZ;
1434 else
1435 adev->family = AMDGPU_FAMILY_VI;
1436
1437 r = vi_set_ip_blocks(adev);
1438 if (r)
1439 return r;
1440 break;
Ken Wang33f34802016-01-21 17:29:41 +08001441#ifdef CONFIG_DRM_AMDGPU_SI
1442 case CHIP_VERDE:
1443 case CHIP_TAHITI:
1444 case CHIP_PITCAIRN:
1445 case CHIP_OLAND:
1446 case CHIP_HAINAN:
Ken Wang295d0da2016-05-24 21:02:53 +08001447 adev->family = AMDGPU_FAMILY_SI;
Ken Wang33f34802016-01-21 17:29:41 +08001448 r = si_set_ip_blocks(adev);
1449 if (r)
1450 return r;
1451 break;
1452#endif
Alex Deuchera2e73f52015-04-20 17:09:27 -04001453#ifdef CONFIG_DRM_AMDGPU_CIK
1454 case CHIP_BONAIRE:
1455 case CHIP_HAWAII:
1456 case CHIP_KAVERI:
1457 case CHIP_KABINI:
1458 case CHIP_MULLINS:
1459 if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
1460 adev->family = AMDGPU_FAMILY_CI;
1461 else
1462 adev->family = AMDGPU_FAMILY_KV;
1463
1464 r = cik_set_ip_blocks(adev);
1465 if (r)
1466 return r;
1467 break;
1468#endif
Chunming Zhou2ca8a5d2016-12-07 17:31:19 +08001469 case CHIP_VEGA10:
1470 case CHIP_RAVEN:
1471 if (adev->asic_type == CHIP_RAVEN)
1472 adev->family = AMDGPU_FAMILY_RV;
1473 else
1474 adev->family = AMDGPU_FAMILY_AI;
Ken Wang460826e2017-03-06 14:53:16 -05001475
1476 r = soc15_set_ip_blocks(adev);
1477 if (r)
1478 return r;
1479 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001480 default:
1481 /* FIXME: not supported yet */
1482 return -EINVAL;
1483 }
1484
Alex Deuchere2a75f82017-04-27 16:58:01 -04001485 r = amdgpu_device_parse_gpu_info_fw(adev);
1486 if (r)
1487 return r;
1488
pding18847342017-11-06 10:21:26 +08001489 amdgpu_amdkfd_device_probe(adev);
1490
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001491 if (amdgpu_sriov_vf(adev)) {
1492 r = amdgpu_virt_request_full_gpu(adev, true);
1493 if (r)
pding5ffa61c2017-10-30 14:07:24 +08001494 return -EAGAIN;
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001495 }
1496
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001497 for (i = 0; i < adev->num_ip_blocks; i++) {
1498 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
Huang Ruied8cf002017-05-03 09:40:17 +08001499 DRM_ERROR("disabled ip block: %d <%s>\n",
1500 i, adev->ip_blocks[i].version->funcs->name);
Alex Deuchera1255102016-10-13 17:41:13 -04001501 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001502 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001503 if (adev->ip_blocks[i].version->funcs->early_init) {
1504 r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001505 if (r == -ENOENT) {
Alex Deuchera1255102016-10-13 17:41:13 -04001506 adev->ip_blocks[i].status.valid = false;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001507 } else if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001508 DRM_ERROR("early_init of IP block <%s> failed %d\n",
1509 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001510 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001511 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001512 adev->ip_blocks[i].status.valid = true;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001513 }
Alex Deucher974e6b62015-07-10 13:59:44 -04001514 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001515 adev->ip_blocks[i].status.valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001516 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001517 }
1518 }
1519
Nicolai Hähnle395d1fb2016-06-02 12:32:07 +02001520 adev->cg_flags &= amdgpu_cg_mask;
1521 adev->pg_flags &= amdgpu_pg_mask;
1522
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001523 return 0;
1524}
1525
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001526/**
1527 * amdgpu_device_ip_init - run init for hardware IPs
1528 *
1529 * @adev: amdgpu_device pointer
1530 *
1531 * Main initialization pass for hardware IPs. The list of all the hardware
1532 * IPs that make up the asic is walked and the sw_init and hw_init callbacks
1533 * are run. sw_init initializes the software state associated with each IP
1534 * and hw_init initializes the hardware associated with each IP.
1535 * Returns 0 on success, negative error code on failure.
1536 */
Alex Deucher06ec9072017-12-14 15:02:39 -05001537static int amdgpu_device_ip_init(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001538{
1539 int i, r;
1540
1541 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001542 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001543 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001544 r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001545 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001546 DRM_ERROR("sw_init of IP block <%s> failed %d\n",
1547 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001548 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001549 }
Alex Deuchera1255102016-10-13 17:41:13 -04001550 adev->ip_blocks[i].status.sw = true;
Shaoyun Liubfca0282018-02-01 17:37:50 -05001551
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001552 /* need to do gmc hw init early so we can allocate gpu mem */
Alex Deuchera1255102016-10-13 17:41:13 -04001553 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucher06ec9072017-12-14 15:02:39 -05001554 r = amdgpu_device_vram_scratch_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001555 if (r) {
1556 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001557 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001558 }
Alex Deuchera1255102016-10-13 17:41:13 -04001559 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001560 if (r) {
1561 DRM_ERROR("hw_init %d failed %d\n", i, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001562 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001563 }
Alex Deucher06ec9072017-12-14 15:02:39 -05001564 r = amdgpu_device_wb_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001565 if (r) {
Alex Deucher06ec9072017-12-14 15:02:39 -05001566 DRM_ERROR("amdgpu_device_wb_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001567 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001568 }
Alex Deuchera1255102016-10-13 17:41:13 -04001569 adev->ip_blocks[i].status.hw = true;
Monk Liu24936642017-01-09 15:54:32 +08001570
1571 /* right after GMC hw init, we create CSA */
1572 if (amdgpu_sriov_vf(adev)) {
1573 r = amdgpu_allocate_static_csa(adev);
1574 if (r) {
1575 DRM_ERROR("allocate CSA failed %d\n", r);
1576 return r;
1577 }
1578 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001579 }
1580 }
1581
1582 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001583 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001584 continue;
Shaoyun Liubfca0282018-02-01 17:37:50 -05001585 if (adev->ip_blocks[i].status.hw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001586 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001587 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001588 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001589 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
1590 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001591 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001592 }
Alex Deuchera1255102016-10-13 17:41:13 -04001593 adev->ip_blocks[i].status.hw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001594 }
1595
pding18847342017-11-06 10:21:26 +08001596 amdgpu_amdkfd_device_init(adev);
pdingc6332b92017-11-06 11:21:55 +08001597
1598 if (amdgpu_sriov_vf(adev))
1599 amdgpu_virt_release_full_gpu(adev, true);
1600
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001601 return 0;
1602}
1603
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001604/**
1605 * amdgpu_device_fill_reset_magic - writes reset magic to gart pointer
1606 *
1607 * @adev: amdgpu_device pointer
1608 *
1609 * Writes a reset magic value to the gart pointer in VRAM. The driver calls
1610 * this function before a GPU reset. If the value is retained after a
1611 * GPU reset, VRAM has not been lost. Some GPU resets may destry VRAM contents.
1612 */
Alex Deucher06ec9072017-12-14 15:02:39 -05001613static void amdgpu_device_fill_reset_magic(struct amdgpu_device *adev)
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08001614{
1615 memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM);
1616}
1617
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001618/**
1619 * amdgpu_device_check_vram_lost - check if vram is valid
1620 *
1621 * @adev: amdgpu_device pointer
1622 *
1623 * Checks the reset magic value written to the gart pointer in VRAM.
1624 * The driver calls this after a GPU reset to see if the contents of
1625 * VRAM is lost or now.
1626 * returns true if vram is lost, false if not.
1627 */
Alex Deucher06ec9072017-12-14 15:02:39 -05001628static bool amdgpu_device_check_vram_lost(struct amdgpu_device *adev)
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08001629{
1630 return !!memcmp(adev->gart.ptr, adev->reset_magic,
1631 AMDGPU_RESET_MAGIC_NUM);
1632}
1633
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001634/**
1635 * amdgpu_device_ip_late_set_cg_state - late init for clockgating
1636 *
1637 * @adev: amdgpu_device pointer
1638 *
1639 * Late initialization pass enabling clockgating for hardware IPs.
1640 * The list of all the hardware IPs that make up the asic is walked and the
1641 * set_clockgating_state callbacks are run. This stage is run late
1642 * in the init process.
1643 * Returns 0 on success, negative error code on failure.
1644 */
Alex Deucher06ec9072017-12-14 15:02:39 -05001645static int amdgpu_device_ip_late_set_cg_state(struct amdgpu_device *adev)
Shirish S2dc80b02017-05-25 10:05:25 +05301646{
1647 int i = 0, r;
1648
Shaoyun Liu4a2ba392018-02-05 16:41:33 -05001649 if (amdgpu_emu_mode == 1)
1650 return 0;
1651
Shirish S2dc80b02017-05-25 10:05:25 +05301652 for (i = 0; i < adev->num_ip_blocks; i++) {
1653 if (!adev->ip_blocks[i].status.valid)
1654 continue;
1655 /* skip CG for VCE/UVD, it's handled specially */
1656 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
Rex Zhu57716322018-03-12 19:50:38 +08001657 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
1658 adev->ip_blocks[i].version->funcs->set_clockgating_state) {
Shirish S2dc80b02017-05-25 10:05:25 +05301659 /* enable clockgating to save power */
1660 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1661 AMD_CG_STATE_GATE);
1662 if (r) {
1663 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
1664 adev->ip_blocks[i].version->funcs->name, r);
1665 return r;
1666 }
1667 }
1668 }
1669 return 0;
1670}
1671
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001672/**
1673 * amdgpu_device_ip_late_init - run late init for hardware IPs
1674 *
1675 * @adev: amdgpu_device pointer
1676 *
1677 * Late initialization pass for hardware IPs. The list of all the hardware
1678 * IPs that make up the asic is walked and the late_init callbacks are run.
1679 * late_init covers any special initialization that an IP requires
1680 * after all of the have been initialized or something that needs to happen
1681 * late in the init process.
1682 * Returns 0 on success, negative error code on failure.
1683 */
Alex Deucher06ec9072017-12-14 15:02:39 -05001684static int amdgpu_device_ip_late_init(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001685{
1686 int i = 0, r;
1687
1688 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001689 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001690 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001691 if (adev->ip_blocks[i].version->funcs->late_init) {
1692 r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001693 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001694 DRM_ERROR("late_init of IP block <%s> failed %d\n",
1695 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001696 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001697 }
Alex Deuchera1255102016-10-13 17:41:13 -04001698 adev->ip_blocks[i].status.late_initialized = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001699 }
1700 }
1701
Shirish S2dc80b02017-05-25 10:05:25 +05301702 mod_delayed_work(system_wq, &adev->late_init_work,
1703 msecs_to_jiffies(AMDGPU_RESUME_MS));
1704
Alex Deucher06ec9072017-12-14 15:02:39 -05001705 amdgpu_device_fill_reset_magic(adev);
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08001706
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001707 return 0;
1708}
1709
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001710/**
1711 * amdgpu_device_ip_fini - run fini for hardware IPs
1712 *
1713 * @adev: amdgpu_device pointer
1714 *
1715 * Main teardown pass for hardware IPs. The list of all the hardware
1716 * IPs that make up the asic is walked and the hw_fini and sw_fini callbacks
1717 * are run. hw_fini tears down the hardware associated with each IP
1718 * and sw_fini tears down any software state associated with each IP.
1719 * Returns 0 on success, negative error code on failure.
1720 */
Alex Deucher06ec9072017-12-14 15:02:39 -05001721static int amdgpu_device_ip_fini(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001722{
1723 int i, r;
1724
pding18847342017-11-06 10:21:26 +08001725 amdgpu_amdkfd_device_fini(adev);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001726 /* need to disable SMC first */
1727 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001728 if (!adev->ip_blocks[i].status.hw)
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001729 continue;
Rex Zhu57716322018-03-12 19:50:38 +08001730 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC &&
1731 adev->ip_blocks[i].version->funcs->set_clockgating_state) {
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001732 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
Alex Deuchera1255102016-10-13 17:41:13 -04001733 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1734 AMD_CG_STATE_UNGATE);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001735 if (r) {
1736 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001737 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001738 return r;
1739 }
Alex Deuchera1255102016-10-13 17:41:13 -04001740 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001741 /* XXX handle errors */
1742 if (r) {
1743 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001744 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001745 }
Alex Deuchera1255102016-10-13 17:41:13 -04001746 adev->ip_blocks[i].status.hw = false;
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001747 break;
1748 }
1749 }
1750
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001751 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001752 if (!adev->ip_blocks[i].status.hw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001753 continue;
Rex Zhu8201a672016-11-24 21:44:44 +08001754
1755 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
Rex Zhu81ce8be2018-03-20 16:28:56 +08001756 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
1757 adev->ip_blocks[i].version->funcs->set_clockgating_state) {
Rex Zhu8201a672016-11-24 21:44:44 +08001758 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
1759 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1760 AMD_CG_STATE_UNGATE);
1761 if (r) {
1762 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1763 adev->ip_blocks[i].version->funcs->name, r);
1764 return r;
1765 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001766 }
Rex Zhu8201a672016-11-24 21:44:44 +08001767
Alex Deuchera1255102016-10-13 17:41:13 -04001768 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001769 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001770 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001771 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
1772 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001773 }
Rex Zhu8201a672016-11-24 21:44:44 +08001774
Alex Deuchera1255102016-10-13 17:41:13 -04001775 adev->ip_blocks[i].status.hw = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001776 }
1777
Alex Deucher9950cda2018-01-18 19:05:36 -05001778
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001779 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001780 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001781 continue;
Monk Liuc12aba32018-01-24 12:20:32 +08001782
1783 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
1784 amdgpu_free_static_csa(adev);
1785 amdgpu_device_wb_fini(adev);
1786 amdgpu_device_vram_scratch_fini(adev);
1787 }
1788
Alex Deuchera1255102016-10-13 17:41:13 -04001789 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001790 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001791 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001792 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
1793 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001794 }
Alex Deuchera1255102016-10-13 17:41:13 -04001795 adev->ip_blocks[i].status.sw = false;
1796 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001797 }
1798
Monk Liua6dcfd92016-05-19 14:36:34 +08001799 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001800 if (!adev->ip_blocks[i].status.late_initialized)
Grazvydas Ignotas8a2eef12016-10-03 00:06:44 +03001801 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001802 if (adev->ip_blocks[i].version->funcs->late_fini)
1803 adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
1804 adev->ip_blocks[i].status.late_initialized = false;
Monk Liua6dcfd92016-05-19 14:36:34 +08001805 }
1806
Monk Liu030308f2017-09-15 15:34:52 +08001807 if (amdgpu_sriov_vf(adev))
Monk Liu24136132017-11-14 16:56:55 +08001808 if (amdgpu_virt_release_full_gpu(adev, false))
1809 DRM_ERROR("failed to release exclusive mode on fini\n");
Monk Liu24936642017-01-09 15:54:32 +08001810
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001811 return 0;
1812}
1813
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001814/**
1815 * amdgpu_device_ip_late_init_func_handler - work handler for clockgating
1816 *
1817 * @work: work_struct
1818 *
1819 * Work handler for amdgpu_device_ip_late_set_cg_state. We put the
1820 * clockgating setup into a worker thread to speed up driver init and
1821 * resume from suspend.
1822 */
Alex Deucher06ec9072017-12-14 15:02:39 -05001823static void amdgpu_device_ip_late_init_func_handler(struct work_struct *work)
Shirish S2dc80b02017-05-25 10:05:25 +05301824{
1825 struct amdgpu_device *adev =
1826 container_of(work, struct amdgpu_device, late_init_work.work);
Alex Deucher06ec9072017-12-14 15:02:39 -05001827 amdgpu_device_ip_late_set_cg_state(adev);
Shirish S2dc80b02017-05-25 10:05:25 +05301828}
1829
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001830/**
1831 * amdgpu_device_ip_suspend - run suspend for hardware IPs
1832 *
1833 * @adev: amdgpu_device pointer
1834 *
1835 * Main suspend function for hardware IPs. The list of all the hardware
1836 * IPs that make up the asic is walked, clockgating is disabled and the
1837 * suspend callbacks are run. suspend puts the hardware and software state
1838 * in each IP into a state suitable for suspend.
1839 * Returns 0 on success, negative error code on failure.
1840 */
Alex Deuchercdd61df2017-12-14 16:47:40 -05001841int amdgpu_device_ip_suspend(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001842{
1843 int i, r;
1844
Xiangliang Yue941ea92017-01-18 12:47:55 +08001845 if (amdgpu_sriov_vf(adev))
1846 amdgpu_virt_request_full_gpu(adev, false);
1847
Flora Cuic5a93a22016-02-26 10:45:25 +08001848 /* ungate SMC block first */
Alex Deucher2990a1f2017-12-15 16:18:00 -05001849 r = amdgpu_device_ip_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC,
1850 AMD_CG_STATE_UNGATE);
Flora Cuic5a93a22016-02-26 10:45:25 +08001851 if (r) {
Alex Deucher2990a1f2017-12-15 16:18:00 -05001852 DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n", r);
Flora Cuic5a93a22016-02-26 10:45:25 +08001853 }
1854
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001855 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001856 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001857 continue;
1858 /* ungate blocks so that suspend can properly shut them down */
Rex Zhu5b2a3d22018-03-14 15:38:48 +08001859 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_SMC &&
Rex Zhu57716322018-03-12 19:50:38 +08001860 adev->ip_blocks[i].version->funcs->set_clockgating_state) {
Alex Deuchera1255102016-10-13 17:41:13 -04001861 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1862 AMD_CG_STATE_UNGATE);
Flora Cuic5a93a22016-02-26 10:45:25 +08001863 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001864 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1865 adev->ip_blocks[i].version->funcs->name, r);
Flora Cuic5a93a22016-02-26 10:45:25 +08001866 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001867 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001868 /* XXX handle errors */
Alex Deuchera1255102016-10-13 17:41:13 -04001869 r = adev->ip_blocks[i].version->funcs->suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001870 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001871 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001872 DRM_ERROR("suspend of IP block <%s> failed %d\n",
1873 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001874 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001875 }
1876
Xiangliang Yue941ea92017-01-18 12:47:55 +08001877 if (amdgpu_sriov_vf(adev))
1878 amdgpu_virt_release_full_gpu(adev, false);
1879
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001880 return 0;
1881}
1882
Alex Deucher06ec9072017-12-14 15:02:39 -05001883static int amdgpu_device_ip_reinit_early_sriov(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08001884{
1885 int i, r;
1886
Monk Liu2cb681b2017-04-26 12:00:49 +08001887 static enum amd_ip_block_type ip_order[] = {
1888 AMD_IP_BLOCK_TYPE_GMC,
1889 AMD_IP_BLOCK_TYPE_COMMON,
Monk Liu2cb681b2017-04-26 12:00:49 +08001890 AMD_IP_BLOCK_TYPE_IH,
1891 };
Monk Liua90ad3c2017-01-23 14:22:08 +08001892
Monk Liu2cb681b2017-04-26 12:00:49 +08001893 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1894 int j;
1895 struct amdgpu_ip_block *block;
Monk Liua90ad3c2017-01-23 14:22:08 +08001896
Monk Liu2cb681b2017-04-26 12:00:49 +08001897 for (j = 0; j < adev->num_ip_blocks; j++) {
1898 block = &adev->ip_blocks[j];
1899
1900 if (block->version->type != ip_order[i] ||
1901 !block->status.valid)
1902 continue;
1903
1904 r = block->version->funcs->hw_init(adev);
1905 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
Monk Liuc41d1cf2017-12-25 11:59:27 +08001906 if (r)
1907 return r;
Monk Liua90ad3c2017-01-23 14:22:08 +08001908 }
1909 }
1910
1911 return 0;
1912}
1913
Alex Deucher06ec9072017-12-14 15:02:39 -05001914static int amdgpu_device_ip_reinit_late_sriov(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08001915{
1916 int i, r;
1917
Monk Liu2cb681b2017-04-26 12:00:49 +08001918 static enum amd_ip_block_type ip_order[] = {
1919 AMD_IP_BLOCK_TYPE_SMC,
Monk Liuef4c1662017-09-22 16:23:34 +08001920 AMD_IP_BLOCK_TYPE_PSP,
Monk Liu2cb681b2017-04-26 12:00:49 +08001921 AMD_IP_BLOCK_TYPE_DCE,
1922 AMD_IP_BLOCK_TYPE_GFX,
1923 AMD_IP_BLOCK_TYPE_SDMA,
Frank Min257deb82017-06-15 20:07:36 +08001924 AMD_IP_BLOCK_TYPE_UVD,
1925 AMD_IP_BLOCK_TYPE_VCE
Monk Liu2cb681b2017-04-26 12:00:49 +08001926 };
Monk Liua90ad3c2017-01-23 14:22:08 +08001927
Monk Liu2cb681b2017-04-26 12:00:49 +08001928 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1929 int j;
1930 struct amdgpu_ip_block *block;
Monk Liua90ad3c2017-01-23 14:22:08 +08001931
Monk Liu2cb681b2017-04-26 12:00:49 +08001932 for (j = 0; j < adev->num_ip_blocks; j++) {
1933 block = &adev->ip_blocks[j];
1934
1935 if (block->version->type != ip_order[i] ||
1936 !block->status.valid)
1937 continue;
1938
1939 r = block->version->funcs->hw_init(adev);
1940 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
Monk Liuc41d1cf2017-12-25 11:59:27 +08001941 if (r)
1942 return r;
Monk Liua90ad3c2017-01-23 14:22:08 +08001943 }
1944 }
1945
1946 return 0;
1947}
1948
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001949/**
1950 * amdgpu_device_ip_resume_phase1 - run resume for hardware IPs
1951 *
1952 * @adev: amdgpu_device pointer
1953 *
1954 * First resume function for hardware IPs. The list of all the hardware
1955 * IPs that make up the asic is walked and the resume callbacks are run for
1956 * COMMON, GMC, and IH. resume puts the hardware into a functional state
1957 * after a suspend and updates the software state as necessary. This
1958 * function is also used for restoring the GPU after a GPU reset.
1959 * Returns 0 on success, negative error code on failure.
1960 */
Alex Deucher06ec9072017-12-14 15:02:39 -05001961static int amdgpu_device_ip_resume_phase1(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001962{
1963 int i, r;
1964
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001965 for (i = 0; i < adev->num_ip_blocks; i++) {
1966 if (!adev->ip_blocks[i].status.valid)
1967 continue;
Chunming Zhoufcf06492017-05-05 10:33:33 +08001968 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001969 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
1970 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH) {
Chunming Zhoufcf06492017-05-05 10:33:33 +08001971 r = adev->ip_blocks[i].version->funcs->resume(adev);
1972 if (r) {
1973 DRM_ERROR("resume of IP block <%s> failed %d\n",
1974 adev->ip_blocks[i].version->funcs->name, r);
1975 return r;
1976 }
1977 }
1978 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001979
Chunming Zhoufcf06492017-05-05 10:33:33 +08001980 return 0;
1981}
1982
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001983/**
1984 * amdgpu_device_ip_resume_phase2 - run resume for hardware IPs
1985 *
1986 * @adev: amdgpu_device pointer
1987 *
1988 * First resume function for hardware IPs. The list of all the hardware
1989 * IPs that make up the asic is walked and the resume callbacks are run for
1990 * all blocks except COMMON, GMC, and IH. resume puts the hardware into a
1991 * functional state after a suspend and updates the software state as
1992 * necessary. This function is also used for restoring the GPU after a GPU
1993 * reset.
1994 * Returns 0 on success, negative error code on failure.
1995 */
Alex Deucher06ec9072017-12-14 15:02:39 -05001996static int amdgpu_device_ip_resume_phase2(struct amdgpu_device *adev)
Chunming Zhoufcf06492017-05-05 10:33:33 +08001997{
1998 int i, r;
1999
2000 for (i = 0; i < adev->num_ip_blocks; i++) {
2001 if (!adev->ip_blocks[i].status.valid)
2002 continue;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002003 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002004 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
2005 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002006 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002007 r = adev->ip_blocks[i].version->funcs->resume(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002008 if (r) {
2009 DRM_ERROR("resume of IP block <%s> failed %d\n",
2010 adev->ip_blocks[i].version->funcs->name, r);
2011 return r;
2012 }
2013 }
2014
2015 return 0;
2016}
2017
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002018/**
2019 * amdgpu_device_ip_resume - run resume for hardware IPs
2020 *
2021 * @adev: amdgpu_device pointer
2022 *
2023 * Main resume function for hardware IPs. The hardware IPs
2024 * are split into two resume functions because they are
2025 * are also used in in recovering from a GPU reset and some additional
2026 * steps need to be take between them. In this case (S3/S4) they are
2027 * run sequentially.
2028 * Returns 0 on success, negative error code on failure.
2029 */
Alex Deucher06ec9072017-12-14 15:02:39 -05002030static int amdgpu_device_ip_resume(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002031{
Chunming Zhoufcf06492017-05-05 10:33:33 +08002032 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002033
Alex Deucher06ec9072017-12-14 15:02:39 -05002034 r = amdgpu_device_ip_resume_phase1(adev);
Chunming Zhoufcf06492017-05-05 10:33:33 +08002035 if (r)
2036 return r;
Alex Deucher06ec9072017-12-14 15:02:39 -05002037 r = amdgpu_device_ip_resume_phase2(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002038
Chunming Zhoufcf06492017-05-05 10:33:33 +08002039 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002040}
2041
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002042/**
2043 * amdgpu_device_detect_sriov_bios - determine if the board supports SR-IOV
2044 *
2045 * @adev: amdgpu_device pointer
2046 *
2047 * Query the VBIOS data tables to determine if the board supports SR-IOV.
2048 */
Monk Liu4e99a442016-03-31 13:26:59 +08002049static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
Andres Rodriguez048765a2016-06-11 02:51:32 -04002050{
Monk Liu6867e1b2017-10-16 19:50:44 +08002051 if (amdgpu_sriov_vf(adev)) {
2052 if (adev->is_atom_fw) {
2053 if (amdgpu_atomfirmware_gpu_supports_virtualization(adev))
2054 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
2055 } else {
2056 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
2057 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
2058 }
2059
2060 if (!(adev->virt.caps & AMDGPU_SRIOV_CAPS_SRIOV_VBIOS))
2061 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_NO_VBIOS, 0, 0);
Alex Deuchera5bde2f2016-09-23 16:23:41 -04002062 }
Andres Rodriguez048765a2016-06-11 02:51:32 -04002063}
2064
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002065/**
2066 * amdgpu_device_asic_has_dc_support - determine if DC supports the asic
2067 *
2068 * @asic_type: AMD asic type
2069 *
2070 * Check if there is DC (new modesetting infrastructre) support for an asic.
2071 * returns true if DC has support, false if not.
2072 */
Harry Wentland45622362017-09-12 15:58:20 -04002073bool amdgpu_device_asic_has_dc_support(enum amd_asic_type asic_type)
2074{
2075 switch (asic_type) {
2076#if defined(CONFIG_DRM_AMD_DC)
2077 case CHIP_BONAIRE:
2078 case CHIP_HAWAII:
Alex Deucher0d6fbcc2017-08-10 14:39:48 -04002079 case CHIP_KAVERI:
Alex Deucher367e6682018-01-25 16:53:25 -05002080 case CHIP_KABINI:
2081 case CHIP_MULLINS:
Harry Wentland45622362017-09-12 15:58:20 -04002082 case CHIP_CARRIZO:
2083 case CHIP_STONEY:
2084 case CHIP_POLARIS11:
2085 case CHIP_POLARIS10:
Alex Deucher2c8ad2d2017-06-15 16:20:24 -04002086 case CHIP_POLARIS12:
Harry Wentland45622362017-09-12 15:58:20 -04002087 case CHIP_TONGA:
2088 case CHIP_FIJI:
2089#if defined(CONFIG_DRM_AMD_DC_PRE_VEGA)
2090 return amdgpu_dc != 0;
Harry Wentland45622362017-09-12 15:58:20 -04002091#endif
Harry Wentland42f8ffa2017-09-15 14:07:30 -04002092 case CHIP_VEGA10:
2093#if defined(CONFIG_DRM_AMD_DC_DCN1_0)
Hawking Zhangfd187852017-03-06 14:01:11 +08002094 case CHIP_RAVEN:
Harry Wentland42f8ffa2017-09-15 14:07:30 -04002095#endif
Hawking Zhangfd187852017-03-06 14:01:11 +08002096 return amdgpu_dc != 0;
2097#endif
Harry Wentland45622362017-09-12 15:58:20 -04002098 default:
2099 return false;
2100 }
2101}
2102
2103/**
2104 * amdgpu_device_has_dc_support - check if dc is supported
2105 *
2106 * @adev: amdgpu_device_pointer
2107 *
2108 * Returns true for supported, false for not supported
2109 */
2110bool amdgpu_device_has_dc_support(struct amdgpu_device *adev)
2111{
Xiangliang Yu2555039d2017-01-10 17:34:52 +08002112 if (amdgpu_sriov_vf(adev))
2113 return false;
2114
Harry Wentland45622362017-09-12 15:58:20 -04002115 return amdgpu_device_asic_has_dc_support(adev->asic_type);
2116}
2117
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002118/**
2119 * amdgpu_device_init - initialize the driver
2120 *
2121 * @adev: amdgpu_device pointer
2122 * @pdev: drm dev pointer
2123 * @pdev: pci dev pointer
2124 * @flags: driver flags
2125 *
2126 * Initializes the driver info and hw (all asics).
2127 * Returns 0 for success or an error on failure.
2128 * Called at driver startup.
2129 */
2130int amdgpu_device_init(struct amdgpu_device *adev,
2131 struct drm_device *ddev,
2132 struct pci_dev *pdev,
2133 uint32_t flags)
2134{
2135 int r, i;
2136 bool runtime = false;
Marek Olšák95844d22016-08-17 23:49:27 +02002137 u32 max_MBps;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002138
2139 adev->shutdown = false;
2140 adev->dev = &pdev->dev;
2141 adev->ddev = ddev;
2142 adev->pdev = pdev;
2143 adev->flags = flags;
Jammy Zhou2f7d10b2015-07-22 11:29:01 +08002144 adev->asic_type = flags & AMD_ASIC_MASK;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002145 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
Shaoyun Liu593aa2d2018-02-07 14:43:13 -05002146 if (amdgpu_emu_mode == 1)
2147 adev->usec_timeout *= 2;
Christian König770d13b2018-01-12 14:52:22 +01002148 adev->gmc.gart_size = 512 * 1024 * 1024;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002149 adev->accel_working = false;
2150 adev->num_rings = 0;
2151 adev->mman.buffer_funcs = NULL;
2152 adev->mman.buffer_funcs_ring = NULL;
2153 adev->vm_manager.vm_pte_funcs = NULL;
Christian König2d55e452016-02-08 17:37:38 +01002154 adev->vm_manager.vm_pte_num_rings = 0;
Christian König132f34e2018-01-12 15:26:08 +01002155 adev->gmc.gmc_funcs = NULL;
Chris Wilsonf54d1862016-10-25 13:00:45 +01002156 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
Andres Rodriguezb8866c22017-04-28 20:05:51 -04002157 bitmap_zero(adev->gfx.pipe_reserve_bitmap, AMDGPU_MAX_COMPUTE_QUEUES);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002158
2159 adev->smc_rreg = &amdgpu_invalid_rreg;
2160 adev->smc_wreg = &amdgpu_invalid_wreg;
2161 adev->pcie_rreg = &amdgpu_invalid_rreg;
2162 adev->pcie_wreg = &amdgpu_invalid_wreg;
Huang Rui36b9a952016-08-31 13:23:25 +08002163 adev->pciep_rreg = &amdgpu_invalid_rreg;
2164 adev->pciep_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002165 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
2166 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
2167 adev->didt_rreg = &amdgpu_invalid_rreg;
2168 adev->didt_wreg = &amdgpu_invalid_wreg;
Rex Zhuccdbb202016-06-08 12:47:41 +08002169 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
2170 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002171 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
2172 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
2173
Alex Deucher3e39ab92015-06-05 15:04:33 -04002174 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
2175 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
2176 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002177
2178 /* mutex initialization are all done here so we
2179 * can recall function without having locking issues */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002180 atomic_set(&adev->irq.ih.lock, 0);
Huang Rui0e5ca0d2017-03-03 18:37:23 -05002181 mutex_init(&adev->firmware.mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002182 mutex_init(&adev->pm.mutex);
2183 mutex_init(&adev->gfx.gpu_clock_mutex);
2184 mutex_init(&adev->srbm_mutex);
Andres Rodriguezb8866c22017-04-28 20:05:51 -04002185 mutex_init(&adev->gfx.pipe_reserve_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002186 mutex_init(&adev->grbm_idx_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002187 mutex_init(&adev->mn_lock);
Alex Deuchere23b74a2017-09-28 09:47:32 -04002188 mutex_init(&adev->virt.vf_errors.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002189 hash_init(adev->mn_hash);
Monk Liu13a752e2017-10-17 15:11:12 +08002190 mutex_init(&adev->lock_reset);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002191
Alex Deucher06ec9072017-12-14 15:02:39 -05002192 amdgpu_device_check_arguments(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002193
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002194 spin_lock_init(&adev->mmio_idx_lock);
2195 spin_lock_init(&adev->smc_idx_lock);
2196 spin_lock_init(&adev->pcie_idx_lock);
2197 spin_lock_init(&adev->uvd_ctx_idx_lock);
2198 spin_lock_init(&adev->didt_idx_lock);
Rex Zhuccdbb202016-06-08 12:47:41 +08002199 spin_lock_init(&adev->gc_cac_idx_lock);
Evan Quan16abb5d2017-07-04 09:21:50 +08002200 spin_lock_init(&adev->se_cac_idx_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002201 spin_lock_init(&adev->audio_endpt_idx_lock);
Marek Olšák95844d22016-08-17 23:49:27 +02002202 spin_lock_init(&adev->mm_stats.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002203
Chunming Zhou0c4e7fa2016-08-17 11:41:30 +08002204 INIT_LIST_HEAD(&adev->shadow_list);
2205 mutex_init(&adev->shadow_list_lock);
2206
Andres Rodriguez795f2812017-03-06 16:27:55 -05002207 INIT_LIST_HEAD(&adev->ring_lru_list);
2208 spin_lock_init(&adev->ring_lru_list_lock);
2209
Alex Deucher06ec9072017-12-14 15:02:39 -05002210 INIT_DELAYED_WORK(&adev->late_init_work,
2211 amdgpu_device_ip_late_init_func_handler);
Shirish S2dc80b02017-05-25 10:05:25 +05302212
Alex Xie0fa49552017-06-08 14:58:05 -04002213 /* Registers mapping */
2214 /* TODO: block userspace mapping of io register */
Ken Wangda69c1612016-01-21 19:08:55 +08002215 if (adev->asic_type >= CHIP_BONAIRE) {
2216 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
2217 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
2218 } else {
2219 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
2220 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
2221 }
Chunming Zhou5c1354b2016-08-30 16:13:10 +08002222
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002223 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
2224 if (adev->rmmio == NULL) {
2225 return -ENOMEM;
2226 }
2227 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
2228 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
2229
Christian König705e5192017-06-08 11:15:16 +02002230 /* doorbell bar mapping */
Alex Deucher06ec9072017-12-14 15:02:39 -05002231 amdgpu_device_doorbell_init(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002232
2233 /* io port mapping */
2234 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
2235 if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
2236 adev->rio_mem_size = pci_resource_len(adev->pdev, i);
2237 adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
2238 break;
2239 }
2240 }
2241 if (adev->rio_mem == NULL)
Amber Linb64a18c2017-01-04 08:06:58 -05002242 DRM_INFO("PCI I/O BAR is not found.\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002243
Alex Deucher5494d862018-03-09 15:14:11 -05002244 amdgpu_device_get_pcie_info(adev);
2245
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002246 /* early init functions */
Alex Deucher06ec9072017-12-14 15:02:39 -05002247 r = amdgpu_device_ip_early_init(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002248 if (r)
2249 return r;
2250
2251 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
2252 /* this will fail for cards that aren't VGA class devices, just
2253 * ignore it */
Alex Deucher06ec9072017-12-14 15:02:39 -05002254 vga_client_register(adev->pdev, adev, NULL, amdgpu_device_vga_set_decode);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002255
Alex Deuchere9bef452016-04-25 13:12:18 -04002256 if (amdgpu_device_is_px(ddev))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002257 runtime = true;
Lukas Wunner84c8b222017-03-10 21:23:45 +01002258 if (!pci_is_thunderbolt_attached(adev->pdev))
2259 vga_switcheroo_register_client(adev->pdev,
2260 &amdgpu_switcheroo_ops, runtime);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002261 if (runtime)
2262 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
2263
Shaoyun Liu9475a942018-02-01 18:13:23 -05002264 if (amdgpu_emu_mode == 1) {
2265 /* post the asic on emulation mode */
2266 emu_soc_asic_init(adev);
Shaoyun Liubfca0282018-02-01 17:37:50 -05002267 goto fence_driver_init;
Shaoyun Liu9475a942018-02-01 18:13:23 -05002268 }
Shaoyun Liubfca0282018-02-01 17:37:50 -05002269
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002270 /* Read BIOS */
Alex Deucher83ba1262016-06-03 18:21:41 -04002271 if (!amdgpu_get_bios(adev)) {
2272 r = -EINVAL;
2273 goto failed;
2274 }
Nils Wallméniusf7e9e9f2016-12-14 21:52:45 +01002275
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002276 r = amdgpu_atombios_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002277 if (r) {
2278 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002279 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_INIT_FAIL, 0, 0);
Alex Deucher83ba1262016-06-03 18:21:41 -04002280 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002281 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002282
Monk Liu4e99a442016-03-31 13:26:59 +08002283 /* detect if we are with an SRIOV vbios */
2284 amdgpu_device_detect_sriov_bios(adev);
Andres Rodriguez048765a2016-06-11 02:51:32 -04002285
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002286 /* Post card if necessary */
Alex Deucher39c640c2017-12-15 16:22:11 -05002287 if (amdgpu_device_need_post(adev)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002288 if (!adev->bios) {
Monk Liubec86372016-09-14 19:38:08 +08002289 dev_err(adev->dev, "no vBIOS found\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04002290 r = -EINVAL;
2291 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002292 }
Monk Liubec86372016-09-14 19:38:08 +08002293 DRM_INFO("GPU posting now...\n");
Monk Liu4e99a442016-03-31 13:26:59 +08002294 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2295 if (r) {
2296 dev_err(adev->dev, "gpu post error!\n");
2297 goto failed;
2298 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002299 }
2300
Alex Deucher88b64e92017-07-10 10:43:10 -04002301 if (adev->is_atom_fw) {
2302 /* Initialize clocks */
2303 r = amdgpu_atomfirmware_get_clock_info(adev);
2304 if (r) {
2305 dev_err(adev->dev, "amdgpu_atomfirmware_get_clock_info failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002306 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
Alex Deucher88b64e92017-07-10 10:43:10 -04002307 goto failed;
2308 }
2309 } else {
Alex Deuchera5bde2f2016-09-23 16:23:41 -04002310 /* Initialize clocks */
2311 r = amdgpu_atombios_get_clock_info(adev);
2312 if (r) {
2313 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002314 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
Gavin Wan89041942017-06-23 13:55:15 -04002315 goto failed;
Alex Deuchera5bde2f2016-09-23 16:23:41 -04002316 }
2317 /* init i2c buses */
Harry Wentland45622362017-09-12 15:58:20 -04002318 if (!amdgpu_device_has_dc_support(adev))
2319 amdgpu_atombios_i2c_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002320 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002321
Shaoyun Liubfca0282018-02-01 17:37:50 -05002322fence_driver_init:
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002323 /* Fence driver */
2324 r = amdgpu_fence_driver_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002325 if (r) {
2326 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002327 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0);
Alex Deucher83ba1262016-06-03 18:21:41 -04002328 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002329 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002330
2331 /* init the mode config */
2332 drm_mode_config_init(adev->ddev);
2333
Alex Deucher06ec9072017-12-14 15:02:39 -05002334 r = amdgpu_device_ip_init(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002335 if (r) {
pding8840a382017-10-23 17:22:09 +08002336 /* failed in exclusive mode due to timeout */
2337 if (amdgpu_sriov_vf(adev) &&
2338 !amdgpu_sriov_runtime(adev) &&
2339 amdgpu_virt_mmio_blocked(adev) &&
2340 !amdgpu_virt_wait_reset(adev)) {
2341 dev_err(adev->dev, "VF exclusive mode timeout\n");
Pixel Ding1daee8b2017-11-08 11:03:14 +08002342 /* Don't send request since VF is inactive. */
2343 adev->virt.caps &= ~AMDGPU_SRIOV_CAPS_RUNTIME;
2344 adev->virt.ops = NULL;
pding8840a382017-10-23 17:22:09 +08002345 r = -EAGAIN;
2346 goto failed;
2347 }
Alex Deucher06ec9072017-12-14 15:02:39 -05002348 dev_err(adev->dev, "amdgpu_device_ip_init failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002349 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0);
Alex Deucher06ec9072017-12-14 15:02:39 -05002350 amdgpu_device_ip_fini(adev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002351 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002352 }
2353
2354 adev->accel_working = true;
2355
Alex Xiee59c0202017-06-01 09:42:59 -04002356 amdgpu_vm_check_compute_bug(adev);
2357
Marek Olšák95844d22016-08-17 23:49:27 +02002358 /* Initialize the buffer migration limit. */
2359 if (amdgpu_moverate >= 0)
2360 max_MBps = amdgpu_moverate;
2361 else
2362 max_MBps = 8; /* Allow 8 MB/s. */
2363 /* Get a log2 for easy divisions. */
2364 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
2365
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002366 r = amdgpu_ib_pool_init(adev);
2367 if (r) {
2368 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
Alex Deuchere23b74a2017-09-28 09:47:32 -04002369 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r);
Alex Deucher83ba1262016-06-03 18:21:41 -04002370 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002371 }
2372
2373 r = amdgpu_ib_ring_tests(adev);
2374 if (r)
2375 DRM_ERROR("ib ring test failed (%d).\n", r);
2376
Horace Chen2dc8f812017-10-09 16:17:16 +08002377 if (amdgpu_sriov_vf(adev))
2378 amdgpu_virt_init_data_exchange(adev);
2379
Monk Liu9bc92b92017-02-08 17:38:13 +08002380 amdgpu_fbdev_init(adev);
2381
Rex Zhud2f52ac2017-09-22 17:47:27 +08002382 r = amdgpu_pm_sysfs_init(adev);
2383 if (r)
2384 DRM_ERROR("registering pm debugfs failed (%d).\n", r);
2385
Alex Deucher75758252017-12-14 15:23:14 -05002386 r = amdgpu_debugfs_gem_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002387 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002388 DRM_ERROR("registering gem debugfs failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002389
2390 r = amdgpu_debugfs_regs_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002391 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002392 DRM_ERROR("registering register debugfs failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002393
Huang Rui50ab2532016-06-12 15:51:09 +08002394 r = amdgpu_debugfs_firmware_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002395 if (r)
Huang Rui50ab2532016-06-12 15:51:09 +08002396 DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
Huang Rui50ab2532016-06-12 15:51:09 +08002397
Christian König763efb62017-12-06 15:44:51 +01002398 r = amdgpu_debugfs_init(adev);
Kent Russelldb95e212017-08-22 12:31:43 -04002399 if (r)
Christian König763efb62017-12-06 15:44:51 +01002400 DRM_ERROR("Creating debugfs files failed (%d).\n", r);
Kent Russelldb95e212017-08-22 12:31:43 -04002401
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002402 if ((amdgpu_testing & 1)) {
2403 if (adev->accel_working)
2404 amdgpu_test_moves(adev);
2405 else
2406 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
2407 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002408 if (amdgpu_benchmarking) {
2409 if (adev->accel_working)
2410 amdgpu_benchmark(adev, amdgpu_benchmarking);
2411 else
2412 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
2413 }
2414
2415 /* enable clockgating, etc. after ib tests, etc. since some blocks require
2416 * explicit gating rather than handling it automatically.
2417 */
Alex Deucher06ec9072017-12-14 15:02:39 -05002418 r = amdgpu_device_ip_late_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002419 if (r) {
Alex Deucher06ec9072017-12-14 15:02:39 -05002420 dev_err(adev->dev, "amdgpu_device_ip_late_init failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002421 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r);
Alex Deucher83ba1262016-06-03 18:21:41 -04002422 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002423 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002424
2425 return 0;
Alex Deucher83ba1262016-06-03 18:21:41 -04002426
2427failed:
Gavin Wan89041942017-06-23 13:55:15 -04002428 amdgpu_vf_error_trans_all(adev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002429 if (runtime)
2430 vga_switcheroo_fini_domain_pm_ops(adev->dev);
pding8840a382017-10-23 17:22:09 +08002431
Alex Deucher83ba1262016-06-03 18:21:41 -04002432 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002433}
2434
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002435/**
2436 * amdgpu_device_fini - tear down the driver
2437 *
2438 * @adev: amdgpu_device pointer
2439 *
2440 * Tear down the driver info (all asics).
2441 * Called at driver shutdown.
2442 */
2443void amdgpu_device_fini(struct amdgpu_device *adev)
2444{
2445 int r;
2446
2447 DRM_INFO("amdgpu: finishing device.\n");
2448 adev->shutdown = true;
Mikita Lipskie5b03032018-03-15 16:53:08 -04002449 /* disable all interrupts */
2450 amdgpu_irq_disable_all(adev);
Mikita Lipskiff97cba2018-03-14 13:41:29 -04002451 if (adev->mode_info.mode_config_initialized){
2452 if (!amdgpu_device_has_dc_support(adev))
2453 drm_crtc_force_disable_all(adev->ddev);
2454 else
2455 drm_atomic_helper_shutdown(adev->ddev);
2456 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002457 amdgpu_ib_pool_fini(adev);
2458 amdgpu_fence_driver_fini(adev);
Emily Deng58e955d2018-03-08 09:35:19 +08002459 amdgpu_pm_sysfs_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002460 amdgpu_fbdev_fini(adev);
Alex Deucher06ec9072017-12-14 15:02:39 -05002461 r = amdgpu_device_ip_fini(adev);
Huang Ruiab4fe3e2017-06-05 22:11:59 +08002462 if (adev->firmware.gpu_info_fw) {
2463 release_firmware(adev->firmware.gpu_info_fw);
2464 adev->firmware.gpu_info_fw = NULL;
2465 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002466 adev->accel_working = false;
Shirish S2dc80b02017-05-25 10:05:25 +05302467 cancel_delayed_work_sync(&adev->late_init_work);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002468 /* free i2c buses */
Harry Wentland45622362017-09-12 15:58:20 -04002469 if (!amdgpu_device_has_dc_support(adev))
2470 amdgpu_i2c_fini(adev);
Shaoyun Liubfca0282018-02-01 17:37:50 -05002471
2472 if (amdgpu_emu_mode != 1)
2473 amdgpu_atombios_fini(adev);
2474
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002475 kfree(adev->bios);
2476 adev->bios = NULL;
Lukas Wunner84c8b222017-03-10 21:23:45 +01002477 if (!pci_is_thunderbolt_attached(adev->pdev))
2478 vga_switcheroo_unregister_client(adev->pdev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002479 if (adev->flags & AMD_IS_PX)
2480 vga_switcheroo_fini_domain_pm_ops(adev->dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002481 vga_client_register(adev->pdev, NULL, NULL, NULL);
2482 if (adev->rio_mem)
2483 pci_iounmap(adev->pdev, adev->rio_mem);
2484 adev->rio_mem = NULL;
2485 iounmap(adev->rmmio);
2486 adev->rmmio = NULL;
Alex Deucher06ec9072017-12-14 15:02:39 -05002487 amdgpu_device_doorbell_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002488 amdgpu_debugfs_regs_cleanup(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002489}
2490
2491
2492/*
2493 * Suspend & resume.
2494 */
2495/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002496 * amdgpu_device_suspend - initiate device suspend
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002497 *
2498 * @pdev: drm dev pointer
2499 * @state: suspend state
2500 *
2501 * Puts the hw in the suspend state (all asics).
2502 * Returns 0 for success or an error on failure.
2503 * Called at driver suspend.
2504 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002505int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002506{
2507 struct amdgpu_device *adev;
2508 struct drm_crtc *crtc;
2509 struct drm_connector *connector;
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002510 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002511
2512 if (dev == NULL || dev->dev_private == NULL) {
2513 return -ENODEV;
2514 }
2515
2516 adev = dev->dev_private;
2517
2518 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2519 return 0;
2520
2521 drm_kms_helper_poll_disable(dev);
2522
Harry Wentland45622362017-09-12 15:58:20 -04002523 if (!amdgpu_device_has_dc_support(adev)) {
2524 /* turn off display hw */
2525 drm_modeset_lock_all(dev);
2526 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2527 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
2528 }
2529 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002530 }
2531
Yong Zhaoba997702015-11-09 17:21:45 -05002532 amdgpu_amdkfd_suspend(adev);
2533
Alex Deucher756e6882015-10-08 00:03:36 -04002534 /* unpin the front buffers and cursors */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002535 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Alex Deucher756e6882015-10-08 00:03:36 -04002536 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002537 struct amdgpu_framebuffer *rfb = to_amdgpu_framebuffer(crtc->primary->fb);
2538 struct amdgpu_bo *robj;
2539
Alex Deucher756e6882015-10-08 00:03:36 -04002540 if (amdgpu_crtc->cursor_bo) {
2541 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
Alex Xie7a6901d2017-04-24 13:52:41 -04002542 r = amdgpu_bo_reserve(aobj, true);
Alex Deucher756e6882015-10-08 00:03:36 -04002543 if (r == 0) {
2544 amdgpu_bo_unpin(aobj);
2545 amdgpu_bo_unreserve(aobj);
2546 }
2547 }
2548
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002549 if (rfb == NULL || rfb->obj == NULL) {
2550 continue;
2551 }
2552 robj = gem_to_amdgpu_bo(rfb->obj);
2553 /* don't unpin kernel fb objects */
2554 if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
Alex Xie7a6901d2017-04-24 13:52:41 -04002555 r = amdgpu_bo_reserve(robj, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002556 if (r == 0) {
2557 amdgpu_bo_unpin(robj);
2558 amdgpu_bo_unreserve(robj);
2559 }
2560 }
2561 }
2562 /* evict vram memory */
2563 amdgpu_bo_evict_vram(adev);
2564
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002565 amdgpu_fence_driver_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002566
Alex Deuchercdd61df2017-12-14 16:47:40 -05002567 r = amdgpu_device_ip_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002568
Alex Deuchera0a71e42016-10-10 12:41:36 -04002569 /* evict remaining vram memory
2570 * This second call to evict vram is to evict the gart page table
2571 * using the CPU.
2572 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002573 amdgpu_bo_evict_vram(adev);
2574
2575 pci_save_state(dev->pdev);
2576 if (suspend) {
2577 /* Shut down the device */
2578 pci_disable_device(dev->pdev);
2579 pci_set_power_state(dev->pdev, PCI_D3hot);
jimqu74b0b152016-09-07 17:09:12 +08002580 } else {
2581 r = amdgpu_asic_reset(adev);
2582 if (r)
2583 DRM_ERROR("amdgpu asic reset failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002584 }
2585
2586 if (fbcon) {
2587 console_lock();
2588 amdgpu_fbdev_set_suspend(adev, 1);
2589 console_unlock();
2590 }
2591 return 0;
2592}
2593
2594/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002595 * amdgpu_device_resume - initiate device resume
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002596 *
2597 * @pdev: drm dev pointer
2598 *
2599 * Bring the hw back to operating state (all asics).
2600 * Returns 0 for success or an error on failure.
2601 * Called at driver resume.
2602 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002603int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002604{
2605 struct drm_connector *connector;
2606 struct amdgpu_device *adev = dev->dev_private;
Alex Deucher756e6882015-10-08 00:03:36 -04002607 struct drm_crtc *crtc;
Huang Rui03161a62017-04-13 16:12:26 +08002608 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002609
2610 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2611 return 0;
2612
jimqu74b0b152016-09-07 17:09:12 +08002613 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002614 console_lock();
jimqu74b0b152016-09-07 17:09:12 +08002615
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002616 if (resume) {
2617 pci_set_power_state(dev->pdev, PCI_D0);
2618 pci_restore_state(dev->pdev);
jimqu74b0b152016-09-07 17:09:12 +08002619 r = pci_enable_device(dev->pdev);
Huang Rui03161a62017-04-13 16:12:26 +08002620 if (r)
2621 goto unlock;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002622 }
2623
2624 /* post card */
Alex Deucher39c640c2017-12-15 16:22:11 -05002625 if (amdgpu_device_need_post(adev)) {
jimqu74b0b152016-09-07 17:09:12 +08002626 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2627 if (r)
2628 DRM_ERROR("amdgpu asic init failed\n");
2629 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002630
Alex Deucher06ec9072017-12-14 15:02:39 -05002631 r = amdgpu_device_ip_resume(adev);
Rex Zhue6707212017-03-30 13:21:01 +08002632 if (r) {
Alex Deucher06ec9072017-12-14 15:02:39 -05002633 DRM_ERROR("amdgpu_device_ip_resume failed (%d).\n", r);
Huang Rui03161a62017-04-13 16:12:26 +08002634 goto unlock;
Rex Zhue6707212017-03-30 13:21:01 +08002635 }
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002636 amdgpu_fence_driver_resume(adev);
2637
Flora Cuica198522016-02-04 15:10:08 +08002638 if (resume) {
2639 r = amdgpu_ib_ring_tests(adev);
2640 if (r)
2641 DRM_ERROR("ib ring test failed (%d).\n", r);
2642 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002643
Alex Deucher06ec9072017-12-14 15:02:39 -05002644 r = amdgpu_device_ip_late_init(adev);
Huang Rui03161a62017-04-13 16:12:26 +08002645 if (r)
2646 goto unlock;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002647
Alex Deucher756e6882015-10-08 00:03:36 -04002648 /* pin cursors */
2649 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2650 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2651
2652 if (amdgpu_crtc->cursor_bo) {
2653 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
Alex Xie7a6901d2017-04-24 13:52:41 -04002654 r = amdgpu_bo_reserve(aobj, true);
Alex Deucher756e6882015-10-08 00:03:36 -04002655 if (r == 0) {
2656 r = amdgpu_bo_pin(aobj,
2657 AMDGPU_GEM_DOMAIN_VRAM,
2658 &amdgpu_crtc->cursor_addr);
2659 if (r != 0)
2660 DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
2661 amdgpu_bo_unreserve(aobj);
2662 }
2663 }
2664 }
Yong Zhaoba997702015-11-09 17:21:45 -05002665 r = amdgpu_amdkfd_resume(adev);
2666 if (r)
2667 return r;
Alex Deucher756e6882015-10-08 00:03:36 -04002668
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002669 /* blat the mode back in */
2670 if (fbcon) {
Harry Wentland45622362017-09-12 15:58:20 -04002671 if (!amdgpu_device_has_dc_support(adev)) {
2672 /* pre DCE11 */
2673 drm_helper_resume_force_mode(dev);
2674
2675 /* turn on display hw */
2676 drm_modeset_lock_all(dev);
2677 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2678 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
2679 }
2680 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002681 }
2682 }
2683
2684 drm_kms_helper_poll_enable(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002685
2686 /*
2687 * Most of the connector probing functions try to acquire runtime pm
2688 * refs to ensure that the GPU is powered on when connector polling is
2689 * performed. Since we're calling this from a runtime PM callback,
2690 * trying to acquire rpm refs will cause us to deadlock.
2691 *
2692 * Since we're guaranteed to be holding the rpm lock, it's safe to
2693 * temporarily disable the rpm helpers so this doesn't deadlock us.
2694 */
2695#ifdef CONFIG_PM
2696 dev->dev->power.disable_depth++;
2697#endif
Harry Wentland45622362017-09-12 15:58:20 -04002698 if (!amdgpu_device_has_dc_support(adev))
2699 drm_helper_hpd_irq_event(dev);
2700 else
2701 drm_kms_helper_hotplug_event(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002702#ifdef CONFIG_PM
2703 dev->dev->power.disable_depth--;
2704#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002705
Huang Rui03161a62017-04-13 16:12:26 +08002706 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002707 amdgpu_fbdev_set_suspend(adev, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002708
Huang Rui03161a62017-04-13 16:12:26 +08002709unlock:
2710 if (fbcon)
2711 console_unlock();
2712
2713 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002714}
2715
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002716/**
2717 * amdgpu_device_ip_check_soft_reset - did soft reset succeed
2718 *
2719 * @adev: amdgpu_device pointer
2720 *
2721 * The list of all the hardware IPs that make up the asic is walked and
2722 * the check_soft_reset callbacks are run. check_soft_reset determines
2723 * if the asic is still hung or not.
2724 * Returns true if any of the IPs are still in a hung state, false if not.
2725 */
Alex Deucher06ec9072017-12-14 15:02:39 -05002726static bool amdgpu_device_ip_check_soft_reset(struct amdgpu_device *adev)
Chunming Zhou63fbf422016-07-15 11:19:20 +08002727{
2728 int i;
2729 bool asic_hang = false;
2730
Monk Liuf993d622017-10-16 19:46:01 +08002731 if (amdgpu_sriov_vf(adev))
2732 return true;
2733
Chunming Zhou63fbf422016-07-15 11:19:20 +08002734 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002735 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou63fbf422016-07-15 11:19:20 +08002736 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002737 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
2738 adev->ip_blocks[i].status.hang =
2739 adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
2740 if (adev->ip_blocks[i].status.hang) {
2741 DRM_INFO("IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
Chunming Zhou63fbf422016-07-15 11:19:20 +08002742 asic_hang = true;
2743 }
2744 }
2745 return asic_hang;
2746}
2747
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002748/**
2749 * amdgpu_device_ip_pre_soft_reset - prepare for soft reset
2750 *
2751 * @adev: amdgpu_device pointer
2752 *
2753 * The list of all the hardware IPs that make up the asic is walked and the
2754 * pre_soft_reset callbacks are run if the block is hung. pre_soft_reset
2755 * handles any IP specific hardware or software state changes that are
2756 * necessary for a soft reset to succeed.
2757 * Returns 0 on success, negative error code on failure.
2758 */
Alex Deucher06ec9072017-12-14 15:02:39 -05002759static int amdgpu_device_ip_pre_soft_reset(struct amdgpu_device *adev)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002760{
2761 int i, r = 0;
2762
2763 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002764 if (!adev->ip_blocks[i].status.valid)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002765 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002766 if (adev->ip_blocks[i].status.hang &&
2767 adev->ip_blocks[i].version->funcs->pre_soft_reset) {
2768 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
Chunming Zhoud31a5012016-07-18 10:04:34 +08002769 if (r)
2770 return r;
2771 }
2772 }
2773
2774 return 0;
2775}
2776
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002777/**
2778 * amdgpu_device_ip_need_full_reset - check if a full asic reset is needed
2779 *
2780 * @adev: amdgpu_device pointer
2781 *
2782 * Some hardware IPs cannot be soft reset. If they are hung, a full gpu
2783 * reset is necessary to recover.
2784 * Returns true if a full asic reset is required, false if not.
2785 */
Alex Deucher06ec9072017-12-14 15:02:39 -05002786static bool amdgpu_device_ip_need_full_reset(struct amdgpu_device *adev)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002787{
Alex Deucherda146d32016-10-13 16:07:03 -04002788 int i;
2789
2790 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002791 if (!adev->ip_blocks[i].status.valid)
Alex Deucherda146d32016-10-13 16:07:03 -04002792 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002793 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
2794 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
2795 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
Ken Wang98512bb2017-09-14 16:25:19 +08002796 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) ||
2797 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
Alex Deuchera1255102016-10-13 17:41:13 -04002798 if (adev->ip_blocks[i].status.hang) {
Alex Deucherda146d32016-10-13 16:07:03 -04002799 DRM_INFO("Some block need full reset!\n");
2800 return true;
2801 }
2802 }
Chunming Zhou35d782f2016-07-15 15:57:13 +08002803 }
2804 return false;
2805}
2806
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002807/**
2808 * amdgpu_device_ip_soft_reset - do a soft reset
2809 *
2810 * @adev: amdgpu_device pointer
2811 *
2812 * The list of all the hardware IPs that make up the asic is walked and the
2813 * soft_reset callbacks are run if the block is hung. soft_reset handles any
2814 * IP specific hardware or software state changes that are necessary to soft
2815 * reset the IP.
2816 * Returns 0 on success, negative error code on failure.
2817 */
Alex Deucher06ec9072017-12-14 15:02:39 -05002818static int amdgpu_device_ip_soft_reset(struct amdgpu_device *adev)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002819{
2820 int i, r = 0;
2821
2822 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002823 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002824 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002825 if (adev->ip_blocks[i].status.hang &&
2826 adev->ip_blocks[i].version->funcs->soft_reset) {
2827 r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002828 if (r)
2829 return r;
2830 }
2831 }
2832
2833 return 0;
2834}
2835
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002836/**
2837 * amdgpu_device_ip_post_soft_reset - clean up from soft reset
2838 *
2839 * @adev: amdgpu_device pointer
2840 *
2841 * The list of all the hardware IPs that make up the asic is walked and the
2842 * post_soft_reset callbacks are run if the asic was hung. post_soft_reset
2843 * handles any IP specific hardware or software state changes that are
2844 * necessary after the IP has been soft reset.
2845 * Returns 0 on success, negative error code on failure.
2846 */
Alex Deucher06ec9072017-12-14 15:02:39 -05002847static int amdgpu_device_ip_post_soft_reset(struct amdgpu_device *adev)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002848{
2849 int i, r = 0;
2850
2851 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002852 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002853 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002854 if (adev->ip_blocks[i].status.hang &&
2855 adev->ip_blocks[i].version->funcs->post_soft_reset)
2856 r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002857 if (r)
2858 return r;
2859 }
2860
2861 return 0;
2862}
2863
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002864/**
2865 * amdgpu_device_recover_vram_from_shadow - restore shadowed VRAM buffers
2866 *
2867 * @adev: amdgpu_device pointer
2868 * @ring: amdgpu_ring for the engine handling the buffer operations
2869 * @bo: amdgpu_bo buffer whose shadow is being restored
2870 * @fence: dma_fence associated with the operation
2871 *
2872 * Restores the VRAM buffer contents from the shadow in GTT. Used to
2873 * restore things like GPUVM page tables after a GPU reset where
2874 * the contents of VRAM might be lost.
2875 * Returns 0 on success, negative error code on failure.
2876 */
Alex Deucher06ec9072017-12-14 15:02:39 -05002877static int amdgpu_device_recover_vram_from_shadow(struct amdgpu_device *adev,
2878 struct amdgpu_ring *ring,
2879 struct amdgpu_bo *bo,
2880 struct dma_fence **fence)
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002881{
2882 uint32_t domain;
2883 int r;
2884
Roger.He23d2e502017-04-21 14:24:26 +08002885 if (!bo->shadow)
2886 return 0;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002887
Alex Xie1d284792017-04-24 13:53:04 -04002888 r = amdgpu_bo_reserve(bo, true);
Roger.He23d2e502017-04-21 14:24:26 +08002889 if (r)
2890 return r;
2891 domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
2892 /* if bo has been evicted, then no need to recover */
2893 if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
Roger.He82521312017-04-21 13:08:43 +08002894 r = amdgpu_bo_validate(bo->shadow);
2895 if (r) {
2896 DRM_ERROR("bo validate failed!\n");
2897 goto err;
2898 }
2899
Roger.He23d2e502017-04-21 14:24:26 +08002900 r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002901 NULL, fence, true);
Roger.He23d2e502017-04-21 14:24:26 +08002902 if (r) {
2903 DRM_ERROR("recover page table failed!\n");
2904 goto err;
2905 }
2906 }
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002907err:
Roger.He23d2e502017-04-21 14:24:26 +08002908 amdgpu_bo_unreserve(bo);
2909 return r;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002910}
2911
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002912/**
2913 * amdgpu_device_handle_vram_lost - Handle the loss of VRAM contents
2914 *
2915 * @adev: amdgpu_device pointer
2916 *
2917 * Restores the contents of VRAM buffers from the shadows in GTT. Used to
2918 * restore things like GPUVM page tables after a GPU reset where
2919 * the contents of VRAM might be lost.
2920 * Returns 0 on success, 1 on failure.
2921 */
Monk Liuc41d1cf2017-12-25 11:59:27 +08002922static int amdgpu_device_handle_vram_lost(struct amdgpu_device *adev)
2923{
2924 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
2925 struct amdgpu_bo *bo, *tmp;
2926 struct dma_fence *fence = NULL, *next = NULL;
2927 long r = 1;
2928 int i = 0;
2929 long tmo;
2930
2931 if (amdgpu_sriov_runtime(adev))
2932 tmo = msecs_to_jiffies(amdgpu_lockup_timeout);
2933 else
2934 tmo = msecs_to_jiffies(100);
2935
2936 DRM_INFO("recover vram bo from shadow start\n");
2937 mutex_lock(&adev->shadow_list_lock);
2938 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
2939 next = NULL;
2940 amdgpu_device_recover_vram_from_shadow(adev, ring, bo, &next);
2941 if (fence) {
2942 r = dma_fence_wait_timeout(fence, false, tmo);
2943 if (r == 0)
2944 pr_err("wait fence %p[%d] timeout\n", fence, i);
2945 else if (r < 0)
2946 pr_err("wait fence %p[%d] interrupted\n", fence, i);
2947 if (r < 1) {
2948 dma_fence_put(fence);
2949 fence = next;
2950 break;
2951 }
2952 i++;
2953 }
2954
2955 dma_fence_put(fence);
2956 fence = next;
2957 }
2958 mutex_unlock(&adev->shadow_list_lock);
2959
2960 if (fence) {
2961 r = dma_fence_wait_timeout(fence, false, tmo);
2962 if (r == 0)
2963 pr_err("wait fence %p[%d] timeout\n", fence, i);
2964 else if (r < 0)
2965 pr_err("wait fence %p[%d] interrupted\n", fence, i);
2966
2967 }
2968 dma_fence_put(fence);
2969
2970 if (r > 0)
2971 DRM_INFO("recover vram bo from shadow done\n");
2972 else
2973 DRM_ERROR("recover vram bo from shadow failed\n");
2974
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002975 return (r > 0) ? 0 : 1;
Monk Liuc41d1cf2017-12-25 11:59:27 +08002976}
2977
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002978/**
Alex Deucher06ec9072017-12-14 15:02:39 -05002979 * amdgpu_device_reset - reset ASIC/GPU for bare-metal or passthrough
Monk Liua90ad3c2017-01-23 14:22:08 +08002980 *
2981 * @adev: amdgpu device pointer
Monk Liua90ad3c2017-01-23 14:22:08 +08002982 *
Monk Liu57406822017-10-25 16:37:02 +08002983 * attempt to do soft-reset or full-reset and reinitialize Asic
2984 * return 0 means successed otherwise failed
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002985 */
Monk Liuc41d1cf2017-12-25 11:59:27 +08002986static int amdgpu_device_reset(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08002987{
Monk Liu57406822017-10-25 16:37:02 +08002988 bool need_full_reset, vram_lost = 0;
2989 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002990
Alex Deucher06ec9072017-12-14 15:02:39 -05002991 need_full_reset = amdgpu_device_ip_need_full_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002992
2993 if (!need_full_reset) {
Alex Deucher06ec9072017-12-14 15:02:39 -05002994 amdgpu_device_ip_pre_soft_reset(adev);
2995 r = amdgpu_device_ip_soft_reset(adev);
2996 amdgpu_device_ip_post_soft_reset(adev);
2997 if (r || amdgpu_device_ip_check_soft_reset(adev)) {
Chunming Zhou35d782f2016-07-15 15:57:13 +08002998 DRM_INFO("soft reset failed, will fallback to full reset!\n");
2999 need_full_reset = true;
3000 }
3001 }
3002
3003 if (need_full_reset) {
Alex Deuchercdd61df2017-12-14 16:47:40 -05003004 r = amdgpu_device_ip_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003005
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003006retry:
Chunming Zhou35d782f2016-07-15 15:57:13 +08003007 r = amdgpu_asic_reset(adev);
3008 /* post card */
3009 amdgpu_atom_asic_init(adev->mode_info.atom_context);
Alex Deucherbfa99262016-01-15 11:59:48 -05003010
Chunming Zhou35d782f2016-07-15 15:57:13 +08003011 if (!r) {
3012 dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
Alex Deucher06ec9072017-12-14 15:02:39 -05003013 r = amdgpu_device_ip_resume_phase1(adev);
Chunming Zhoufcf06492017-05-05 10:33:33 +08003014 if (r)
3015 goto out;
Monk Liu57406822017-10-25 16:37:02 +08003016
Alex Deucher06ec9072017-12-14 15:02:39 -05003017 vram_lost = amdgpu_device_check_vram_lost(adev);
Chunming Zhouf1892132017-05-15 16:48:27 +08003018 if (vram_lost) {
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08003019 DRM_ERROR("VRAM is lost!\n");
Chunming Zhouf1892132017-05-15 16:48:27 +08003020 atomic_inc(&adev->vram_lost_counter);
3021 }
Monk Liu57406822017-10-25 16:37:02 +08003022
Christian Königc1c7ce82017-10-16 16:50:32 +02003023 r = amdgpu_gtt_mgr_recover(
3024 &adev->mman.bdev.man[TTM_PL_TT]);
Chunming Zhou2c0d7312016-08-30 16:36:25 +08003025 if (r)
Chunming Zhoufcf06492017-05-05 10:33:33 +08003026 goto out;
Monk Liu57406822017-10-25 16:37:02 +08003027
Alex Deucher06ec9072017-12-14 15:02:39 -05003028 r = amdgpu_device_ip_resume_phase2(adev);
Chunming Zhoufcf06492017-05-05 10:33:33 +08003029 if (r)
3030 goto out;
Monk Liu57406822017-10-25 16:37:02 +08003031
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08003032 if (vram_lost)
Alex Deucher06ec9072017-12-14 15:02:39 -05003033 amdgpu_device_fill_reset_magic(adev);
Chunming Zhou2c0d7312016-08-30 16:36:25 +08003034 }
Chunming Zhoufcf06492017-05-05 10:33:33 +08003035 }
Monk Liu57406822017-10-25 16:37:02 +08003036
Chunming Zhoufcf06492017-05-05 10:33:33 +08003037out:
3038 if (!r) {
3039 amdgpu_irq_gpu_reset_resume_helper(adev);
Chunming Zhou1f465082016-06-30 15:02:26 +08003040 r = amdgpu_ib_ring_tests(adev);
3041 if (r) {
3042 dev_err(adev->dev, "ib ring test failed (%d).\n", r);
Alex Deuchercdd61df2017-12-14 16:47:40 -05003043 r = amdgpu_device_ip_suspend(adev);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003044 need_full_reset = true;
Chunming Zhou40019dc2016-06-29 16:01:49 +08003045 goto retry;
Chunming Zhou1f465082016-06-30 15:02:26 +08003046 }
Monk Liu57406822017-10-25 16:37:02 +08003047 }
3048
Monk Liuc41d1cf2017-12-25 11:59:27 +08003049 if (!r && ((need_full_reset && !(adev->flags & AMD_IS_APU)) || vram_lost))
3050 r = amdgpu_device_handle_vram_lost(adev);
Monk Liu57406822017-10-25 16:37:02 +08003051
3052 return r;
3053}
3054
Alex Deuchere3ecdff2018-03-15 17:39:45 -05003055/**
Alex Deucher06ec9072017-12-14 15:02:39 -05003056 * amdgpu_device_reset_sriov - reset ASIC for SR-IOV vf
Monk Liu57406822017-10-25 16:37:02 +08003057 *
3058 * @adev: amdgpu device pointer
Monk Liu57406822017-10-25 16:37:02 +08003059 *
3060 * do VF FLR and reinitialize Asic
3061 * return 0 means successed otherwise failed
Alex Deuchere3ecdff2018-03-15 17:39:45 -05003062 */
3063static int amdgpu_device_reset_sriov(struct amdgpu_device *adev,
3064 bool from_hypervisor)
Monk Liu57406822017-10-25 16:37:02 +08003065{
3066 int r;
3067
3068 if (from_hypervisor)
3069 r = amdgpu_virt_request_full_gpu(adev, true);
3070 else
3071 r = amdgpu_virt_reset_gpu(adev);
3072 if (r)
3073 return r;
3074
3075 /* Resume IP prior to SMC */
Alex Deucher06ec9072017-12-14 15:02:39 -05003076 r = amdgpu_device_ip_reinit_early_sriov(adev);
Monk Liu57406822017-10-25 16:37:02 +08003077 if (r)
3078 goto error;
3079
3080 /* we need recover gart prior to run SMC/CP/SDMA resume */
Christian Königc1c7ce82017-10-16 16:50:32 +02003081 amdgpu_gtt_mgr_recover(&adev->mman.bdev.man[TTM_PL_TT]);
Monk Liu57406822017-10-25 16:37:02 +08003082
3083 /* now we are okay to resume SMC/CP/SDMA */
Alex Deucher06ec9072017-12-14 15:02:39 -05003084 r = amdgpu_device_ip_reinit_late_sriov(adev);
Monk Liuc41d1cf2017-12-25 11:59:27 +08003085 amdgpu_virt_release_full_gpu(adev, true);
Monk Liu57406822017-10-25 16:37:02 +08003086 if (r)
3087 goto error;
3088
3089 amdgpu_irq_gpu_reset_resume_helper(adev);
3090 r = amdgpu_ib_ring_tests(adev);
Monk Liuc41d1cf2017-12-25 11:59:27 +08003091
3092 if (!r && adev->virt.gim_feature & AMDGIM_FEATURE_GIM_FLR_VRAMLOST) {
3093 atomic_inc(&adev->vram_lost_counter);
3094 r = amdgpu_device_handle_vram_lost(adev);
3095 }
Monk Liu57406822017-10-25 16:37:02 +08003096
3097error:
Monk Liu57406822017-10-25 16:37:02 +08003098
3099 return r;
3100}
3101
3102/**
Alex Deucher5f152b52017-12-15 16:40:49 -05003103 * amdgpu_device_gpu_recover - reset the asic and recover scheduler
Monk Liu57406822017-10-25 16:37:02 +08003104 *
3105 * @adev: amdgpu device pointer
3106 * @job: which job trigger hang
Andrey Grodzovskydcebf022017-12-12 14:09:30 -05003107 * @force forces reset regardless of amdgpu_gpu_recovery
Monk Liu57406822017-10-25 16:37:02 +08003108 *
3109 * Attempt to reset the GPU if it has hung (all asics).
3110 * Returns 0 for success or an error on failure.
3111 */
Alex Deucher5f152b52017-12-15 16:40:49 -05003112int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
3113 struct amdgpu_job *job, bool force)
Monk Liu57406822017-10-25 16:37:02 +08003114{
3115 struct drm_atomic_state *state = NULL;
Monk Liu57406822017-10-25 16:37:02 +08003116 int i, r, resched;
3117
Andrey Grodzovsky54bc1392018-01-19 17:23:08 -05003118 if (!force && !amdgpu_device_ip_check_soft_reset(adev)) {
Monk Liu57406822017-10-25 16:37:02 +08003119 DRM_INFO("No hardware hang detected. Did some blocks stall?\n");
3120 return 0;
3121 }
3122
Andrey Grodzovskydcebf022017-12-12 14:09:30 -05003123 if (!force && (amdgpu_gpu_recovery == 0 ||
3124 (amdgpu_gpu_recovery == -1 && !amdgpu_sriov_vf(adev)))) {
3125 DRM_INFO("GPU recovery disabled.\n");
3126 return 0;
3127 }
3128
Monk Liu57406822017-10-25 16:37:02 +08003129 dev_info(adev->dev, "GPU reset begin!\n");
3130
Monk Liu13a752e2017-10-17 15:11:12 +08003131 mutex_lock(&adev->lock_reset);
Monk Liu57406822017-10-25 16:37:02 +08003132 atomic_inc(&adev->gpu_reset_counter);
Monk Liu13a752e2017-10-17 15:11:12 +08003133 adev->in_gpu_reset = 1;
Monk Liu57406822017-10-25 16:37:02 +08003134
3135 /* block TTM */
3136 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
Monk Liu71182662017-12-25 15:14:58 +08003137
Monk Liu57406822017-10-25 16:37:02 +08003138 /* store modesetting */
3139 if (amdgpu_device_has_dc_support(adev))
3140 state = drm_atomic_helper_suspend(adev->ddev);
3141
Monk Liu71182662017-12-25 15:14:58 +08003142 /* block all schedulers and reset given job's ring */
Monk Liu57406822017-10-25 16:37:02 +08003143 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
3144 struct amdgpu_ring *ring = adev->rings[i];
3145
3146 if (!ring || !ring->sched.thread)
3147 continue;
3148
Monk Liu71182662017-12-25 15:14:58 +08003149 kthread_park(ring->sched.thread);
3150
Monk Liu57406822017-10-25 16:37:02 +08003151 if (job && job->ring->idx != i)
3152 continue;
3153
Lucas Stach1b1f42d2017-12-06 17:49:39 +01003154 drm_sched_hw_job_reset(&ring->sched, &job->base);
Monk Liu57406822017-10-25 16:37:02 +08003155
3156 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
3157 amdgpu_fence_driver_force_completion(ring);
3158 }
3159
3160 if (amdgpu_sriov_vf(adev))
Monk Liuc41d1cf2017-12-25 11:59:27 +08003161 r = amdgpu_device_reset_sriov(adev, job ? false : true);
Monk Liu57406822017-10-25 16:37:02 +08003162 else
Monk Liuc41d1cf2017-12-25 11:59:27 +08003163 r = amdgpu_device_reset(adev);
Monk Liu57406822017-10-25 16:37:02 +08003164
Monk Liu71182662017-12-25 15:14:58 +08003165 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
3166 struct amdgpu_ring *ring = adev->rings[i];
Chunming Zhou51687752017-04-24 17:09:15 +08003167
Monk Liu71182662017-12-25 15:14:58 +08003168 if (!ring || !ring->sched.thread)
3169 continue;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003170
Monk Liu71182662017-12-25 15:14:58 +08003171 /* only need recovery sched of the given job's ring
3172 * or all rings (in the case @job is NULL)
3173 * after above amdgpu_reset accomplished
3174 */
3175 if ((!job || job->ring->idx == i) && !r)
Lucas Stach1b1f42d2017-12-06 17:49:39 +01003176 drm_sched_job_recovery(&ring->sched);
Monk Liu57406822017-10-25 16:37:02 +08003177
Monk Liu71182662017-12-25 15:14:58 +08003178 kthread_unpark(ring->sched.thread);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003179 }
3180
Harry Wentland45622362017-09-12 15:58:20 -04003181 if (amdgpu_device_has_dc_support(adev)) {
Monk Liu57406822017-10-25 16:37:02 +08003182 if (drm_atomic_helper_resume(adev->ddev, state))
3183 dev_info(adev->dev, "drm resume failed:%d\n", r);
Monk Liu57406822017-10-25 16:37:02 +08003184 } else {
Harry Wentland45622362017-09-12 15:58:20 -04003185 drm_helper_resume_force_mode(adev->ddev);
Monk Liu57406822017-10-25 16:37:02 +08003186 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003187
3188 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
Monk Liu57406822017-10-25 16:37:02 +08003189
Gavin Wan89041942017-06-23 13:55:15 -04003190 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003191 /* bad news, how to tell it to userspace ? */
Monk Liu57406822017-10-25 16:37:02 +08003192 dev_info(adev->dev, "GPU reset(%d) failed\n", atomic_read(&adev->gpu_reset_counter));
3193 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, r);
3194 } else {
3195 dev_info(adev->dev, "GPU reset(%d) successed!\n",atomic_read(&adev->gpu_reset_counter));
Gavin Wan89041942017-06-23 13:55:15 -04003196 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003197
Gavin Wan89041942017-06-23 13:55:15 -04003198 amdgpu_vf_error_trans_all(adev);
Monk Liu13a752e2017-10-17 15:11:12 +08003199 adev->in_gpu_reset = 0;
3200 mutex_unlock(&adev->lock_reset);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003201 return r;
3202}
3203
Alex Deuchere3ecdff2018-03-15 17:39:45 -05003204/**
3205 * amdgpu_device_get_pcie_info - fence pcie info about the PCIE slot
3206 *
3207 * @adev: amdgpu_device pointer
3208 *
3209 * Fetchs and stores in the driver the PCIE capabilities (gen speed
3210 * and lanes) of the slot the device is in. Handles APUs and
3211 * virtualized environments where PCIE config space may not be available.
3212 */
Alex Deucher5494d862018-03-09 15:14:11 -05003213static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev)
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003214{
3215 u32 mask;
3216 int ret;
3217
Alex Deuchercd474ba2016-02-04 10:21:23 -05003218 if (amdgpu_pcie_gen_cap)
3219 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
3220
3221 if (amdgpu_pcie_lane_cap)
3222 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
3223
3224 /* covers APUs as well */
3225 if (pci_is_root_bus(adev->pdev->bus)) {
3226 if (adev->pm.pcie_gen_mask == 0)
3227 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
3228 if (adev->pm.pcie_mlw_mask == 0)
3229 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003230 return;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003231 }
Alex Deuchercd474ba2016-02-04 10:21:23 -05003232
3233 if (adev->pm.pcie_gen_mask == 0) {
3234 ret = drm_pcie_get_speed_cap_mask(adev->ddev, &mask);
3235 if (!ret) {
3236 adev->pm.pcie_gen_mask = (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
3237 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
3238 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
3239
3240 if (mask & DRM_PCIE_SPEED_25)
3241 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
3242 if (mask & DRM_PCIE_SPEED_50)
3243 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2;
3244 if (mask & DRM_PCIE_SPEED_80)
3245 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3;
3246 } else {
3247 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
3248 }
3249 }
3250 if (adev->pm.pcie_mlw_mask == 0) {
3251 ret = drm_pcie_get_max_link_width(adev->ddev, &mask);
3252 if (!ret) {
3253 switch (mask) {
3254 case 32:
3255 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
3256 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
3257 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3258 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3259 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3260 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3261 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3262 break;
3263 case 16:
3264 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
3265 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3266 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3267 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3268 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3269 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3270 break;
3271 case 12:
3272 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3273 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3274 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3275 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3276 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3277 break;
3278 case 8:
3279 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3280 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3281 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3282 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3283 break;
3284 case 4:
3285 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3286 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3287 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3288 break;
3289 case 2:
3290 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3291 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3292 break;
3293 case 1:
3294 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
3295 break;
3296 default:
3297 break;
3298 }
3299 } else {
3300 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003301 }
3302 }
3303}
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003304