blob: a53926580b3d0329fbe4a3bddbe973e6f2bb7e70 [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 Deucher3f76dce2017-09-01 16:20:53 -040062MODULE_FIRMWARE("amdgpu/vega12_gpu_info.bin");
Alex Deucher2d2e5e72017-05-09 12:27:35 -040063MODULE_FIRMWARE("amdgpu/raven_gpu_info.bin");
Alex Deuchere2a75f82017-04-27 16:58:01 -040064
Shirish S2dc80b02017-05-25 10:05:25 +053065#define AMDGPU_RESUME_MS 2000
66
Alex Deucherd38ceaf2015-04-20 16:55:21 -040067static const char *amdgpu_asic_name[] = {
Ken Wangda69c1612016-01-21 19:08:55 +080068 "TAHITI",
69 "PITCAIRN",
70 "VERDE",
71 "OLAND",
72 "HAINAN",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040073 "BONAIRE",
74 "KAVERI",
75 "KABINI",
76 "HAWAII",
77 "MULLINS",
78 "TOPAZ",
79 "TONGA",
David Zhang48299f92015-07-08 01:05:16 +080080 "FIJI",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040081 "CARRIZO",
Samuel Li139f4912015-10-08 14:50:27 -040082 "STONEY",
Flora Cui2cc0c0b2016-03-14 18:33:29 -040083 "POLARIS10",
84 "POLARIS11",
Junwei Zhangc4642a42016-12-14 15:32:28 -050085 "POLARIS12",
Ken Wangd4196f02016-03-09 09:28:32 +080086 "VEGA10",
Feifei Xu8fab8062017-10-19 17:04:54 +080087 "VEGA12",
Chunming Zhou2ca8a5d2016-12-07 17:31:19 +080088 "RAVEN",
Alex Deucherd38ceaf2015-04-20 16:55:21 -040089 "LAST",
90};
91
Alex Deucher5494d862018-03-09 15:14:11 -050092static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev);
93
Alex Deuchere3ecdff2018-03-15 17:39:45 -050094/**
95 * amdgpu_device_is_px - Is the device is a dGPU with HG/PX power control
96 *
97 * @dev: drm_device pointer
98 *
99 * Returns true if the device is a dGPU with HG/PX power control,
100 * otherwise return false.
101 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400102bool amdgpu_device_is_px(struct drm_device *dev)
103{
104 struct amdgpu_device *adev = dev->dev_private;
105
Jammy Zhou2f7d10b2015-07-22 11:29:01 +0800106 if (adev->flags & AMD_IS_PX)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400107 return true;
108 return false;
109}
110
111/*
112 * MMIO register access helper functions.
113 */
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500114/**
115 * amdgpu_mm_rreg - read a memory mapped IO register
116 *
117 * @adev: amdgpu_device pointer
118 * @reg: dword aligned register offset
119 * @acc_flags: access flags which require special behavior
120 *
121 * Returns the 32 bit value from the offset specified.
122 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400123uint32_t amdgpu_mm_rreg(struct amdgpu_device *adev, uint32_t reg,
Monk Liu15d72fd2017-01-25 15:07:40 +0800124 uint32_t acc_flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400125{
Tom St Denisf4b373f2016-05-31 08:02:27 -0400126 uint32_t ret;
127
pding43ca8ef2017-10-13 15:38:35 +0800128 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev))
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800129 return amdgpu_virt_kiq_rreg(adev, reg);
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800130
Monk Liu15d72fd2017-01-25 15:07:40 +0800131 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
Tom St Denisf4b373f2016-05-31 08:02:27 -0400132 ret = readl(((void __iomem *)adev->rmmio) + (reg * 4));
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400133 else {
134 unsigned long flags;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400135
136 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
137 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
138 ret = readl(((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
139 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400140 }
Tom St Denisf4b373f2016-05-31 08:02:27 -0400141 trace_amdgpu_mm_rreg(adev->pdev->device, reg, ret);
142 return ret;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400143}
144
Monk Liu421a2a32018-01-04 18:13:20 +0800145/*
146 * MMIO register read with bytes helper functions
147 * @offset:bytes offset from MMIO start
148 *
149*/
150
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500151/**
152 * amdgpu_mm_rreg8 - read a memory mapped IO register
153 *
154 * @adev: amdgpu_device pointer
155 * @offset: byte aligned register offset
156 *
157 * Returns the 8 bit value from the offset specified.
158 */
Monk Liu421a2a32018-01-04 18:13:20 +0800159uint8_t amdgpu_mm_rreg8(struct amdgpu_device *adev, uint32_t offset) {
160 if (offset < adev->rmmio_size)
161 return (readb(adev->rmmio + offset));
162 BUG();
163}
164
165/*
166 * MMIO register write with bytes helper functions
167 * @offset:bytes offset from MMIO start
168 * @value: the value want to be written to the register
169 *
170*/
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500171/**
172 * amdgpu_mm_wreg8 - read a memory mapped IO register
173 *
174 * @adev: amdgpu_device pointer
175 * @offset: byte aligned register offset
176 * @value: 8 bit value to write
177 *
178 * Writes the value specified to the offset specified.
179 */
Monk Liu421a2a32018-01-04 18:13:20 +0800180void amdgpu_mm_wreg8(struct amdgpu_device *adev, uint32_t offset, uint8_t value) {
181 if (offset < adev->rmmio_size)
182 writeb(value, adev->rmmio + offset);
183 else
184 BUG();
185}
186
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500187/**
188 * amdgpu_mm_wreg - write to a memory mapped IO register
189 *
190 * @adev: amdgpu_device pointer
191 * @reg: dword aligned register offset
192 * @v: 32 bit value to write to the register
193 * @acc_flags: access flags which require special behavior
194 *
195 * Writes the value specified to the offset specified.
196 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400197void amdgpu_mm_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v,
Monk Liu15d72fd2017-01-25 15:07:40 +0800198 uint32_t acc_flags)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400199{
Tom St Denisf4b373f2016-05-31 08:02:27 -0400200 trace_amdgpu_mm_wreg(adev->pdev->device, reg, v);
Monk Liu4e99a442016-03-31 13:26:59 +0800201
Ken Wang47ed4e12017-07-04 13:11:52 +0800202 if (adev->asic_type >= CHIP_VEGA10 && reg == 0) {
203 adev->last_mm_index = v;
204 }
205
pding43ca8ef2017-10-13 15:38:35 +0800206 if (!(acc_flags & AMDGPU_REGS_NO_KIQ) && amdgpu_sriov_runtime(adev))
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800207 return amdgpu_virt_kiq_wreg(adev, reg, v);
Xiangliang Yubc992ba2017-01-12 14:29:34 +0800208
Monk Liu15d72fd2017-01-25 15:07:40 +0800209 if ((reg * 4) < adev->rmmio_size && !(acc_flags & AMDGPU_REGS_IDX))
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400210 writel(v, ((void __iomem *)adev->rmmio) + (reg * 4));
211 else {
212 unsigned long flags;
213
214 spin_lock_irqsave(&adev->mmio_idx_lock, flags);
215 writel((reg * 4), ((void __iomem *)adev->rmmio) + (mmMM_INDEX * 4));
216 writel(v, ((void __iomem *)adev->rmmio) + (mmMM_DATA * 4));
217 spin_unlock_irqrestore(&adev->mmio_idx_lock, flags);
218 }
Ken Wang47ed4e12017-07-04 13:11:52 +0800219
220 if (adev->asic_type >= CHIP_VEGA10 && reg == 1 && adev->last_mm_index == 0x5702C) {
221 udelay(500);
222 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400223}
224
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500225/**
226 * amdgpu_io_rreg - read an IO register
227 *
228 * @adev: amdgpu_device pointer
229 * @reg: dword aligned register offset
230 *
231 * Returns the 32 bit value from the offset specified.
232 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400233u32 amdgpu_io_rreg(struct amdgpu_device *adev, u32 reg)
234{
235 if ((reg * 4) < adev->rio_mem_size)
236 return ioread32(adev->rio_mem + (reg * 4));
237 else {
238 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
239 return ioread32(adev->rio_mem + (mmMM_DATA * 4));
240 }
241}
242
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500243/**
244 * amdgpu_io_wreg - write to an IO register
245 *
246 * @adev: amdgpu_device pointer
247 * @reg: dword aligned register offset
248 * @v: 32 bit value to write to the register
249 *
250 * Writes the value specified to the offset specified.
251 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400252void amdgpu_io_wreg(struct amdgpu_device *adev, u32 reg, u32 v)
253{
Ken Wang47ed4e12017-07-04 13:11:52 +0800254 if (adev->asic_type >= CHIP_VEGA10 && reg == 0) {
255 adev->last_mm_index = v;
256 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400257
258 if ((reg * 4) < adev->rio_mem_size)
259 iowrite32(v, adev->rio_mem + (reg * 4));
260 else {
261 iowrite32((reg * 4), adev->rio_mem + (mmMM_INDEX * 4));
262 iowrite32(v, adev->rio_mem + (mmMM_DATA * 4));
263 }
Ken Wang47ed4e12017-07-04 13:11:52 +0800264
265 if (adev->asic_type >= CHIP_VEGA10 && reg == 1 && adev->last_mm_index == 0x5702C) {
266 udelay(500);
267 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400268}
269
270/**
271 * amdgpu_mm_rdoorbell - read a doorbell dword
272 *
273 * @adev: amdgpu_device pointer
274 * @index: doorbell index
275 *
276 * Returns the value in the doorbell aperture at the
277 * requested doorbell index (CIK).
278 */
279u32 amdgpu_mm_rdoorbell(struct amdgpu_device *adev, u32 index)
280{
281 if (index < adev->doorbell.num_doorbells) {
282 return readl(adev->doorbell.ptr + index);
283 } else {
284 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
285 return 0;
286 }
287}
288
289/**
290 * amdgpu_mm_wdoorbell - write a doorbell dword
291 *
292 * @adev: amdgpu_device pointer
293 * @index: doorbell index
294 * @v: value to write
295 *
296 * Writes @v to the doorbell aperture at the
297 * requested doorbell index (CIK).
298 */
299void amdgpu_mm_wdoorbell(struct amdgpu_device *adev, u32 index, u32 v)
300{
301 if (index < adev->doorbell.num_doorbells) {
302 writel(v, adev->doorbell.ptr + index);
303 } else {
304 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
305 }
306}
307
308/**
Ken Wang832be402016-03-18 15:23:08 +0800309 * amdgpu_mm_rdoorbell64 - read a doorbell Qword
310 *
311 * @adev: amdgpu_device pointer
312 * @index: doorbell index
313 *
314 * Returns the value in the doorbell aperture at the
315 * requested doorbell index (VEGA10+).
316 */
317u64 amdgpu_mm_rdoorbell64(struct amdgpu_device *adev, u32 index)
318{
319 if (index < adev->doorbell.num_doorbells) {
320 return atomic64_read((atomic64_t *)(adev->doorbell.ptr + index));
321 } else {
322 DRM_ERROR("reading beyond doorbell aperture: 0x%08x!\n", index);
323 return 0;
324 }
325}
326
327/**
328 * amdgpu_mm_wdoorbell64 - write a doorbell Qword
329 *
330 * @adev: amdgpu_device pointer
331 * @index: doorbell index
332 * @v: value to write
333 *
334 * Writes @v to the doorbell aperture at the
335 * requested doorbell index (VEGA10+).
336 */
337void amdgpu_mm_wdoorbell64(struct amdgpu_device *adev, u32 index, u64 v)
338{
339 if (index < adev->doorbell.num_doorbells) {
340 atomic64_set((atomic64_t *)(adev->doorbell.ptr + index), v);
341 } else {
342 DRM_ERROR("writing beyond doorbell aperture: 0x%08x!\n", index);
343 }
344}
345
346/**
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400347 * amdgpu_invalid_rreg - dummy reg read function
348 *
349 * @adev: amdgpu device pointer
350 * @reg: offset of register
351 *
352 * Dummy register read function. Used for register blocks
353 * that certain asics don't have (all asics).
354 * Returns the value in the register.
355 */
356static uint32_t amdgpu_invalid_rreg(struct amdgpu_device *adev, uint32_t reg)
357{
358 DRM_ERROR("Invalid callback to read register 0x%04X\n", reg);
359 BUG();
360 return 0;
361}
362
363/**
364 * amdgpu_invalid_wreg - dummy reg write function
365 *
366 * @adev: amdgpu device pointer
367 * @reg: offset of register
368 * @v: value to write to the register
369 *
370 * Dummy register read function. Used for register blocks
371 * that certain asics don't have (all asics).
372 */
373static void amdgpu_invalid_wreg(struct amdgpu_device *adev, uint32_t reg, uint32_t v)
374{
375 DRM_ERROR("Invalid callback to write register 0x%04X with 0x%08X\n",
376 reg, v);
377 BUG();
378}
379
380/**
381 * amdgpu_block_invalid_rreg - dummy reg read function
382 *
383 * @adev: amdgpu device pointer
384 * @block: offset of instance
385 * @reg: offset of register
386 *
387 * Dummy register read function. Used for register blocks
388 * that certain asics don't have (all asics).
389 * Returns the value in the register.
390 */
391static uint32_t amdgpu_block_invalid_rreg(struct amdgpu_device *adev,
392 uint32_t block, uint32_t reg)
393{
394 DRM_ERROR("Invalid callback to read register 0x%04X in block 0x%04X\n",
395 reg, block);
396 BUG();
397 return 0;
398}
399
400/**
401 * amdgpu_block_invalid_wreg - dummy reg write function
402 *
403 * @adev: amdgpu device pointer
404 * @block: offset of instance
405 * @reg: offset of register
406 * @v: value to write to the register
407 *
408 * Dummy register read function. Used for register blocks
409 * that certain asics don't have (all asics).
410 */
411static void amdgpu_block_invalid_wreg(struct amdgpu_device *adev,
412 uint32_t block,
413 uint32_t reg, uint32_t v)
414{
415 DRM_ERROR("Invalid block callback to write register 0x%04X in block 0x%04X with 0x%08X\n",
416 reg, block, v);
417 BUG();
418}
419
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500420/**
421 * amdgpu_device_vram_scratch_init - allocate the VRAM scratch page
422 *
423 * @adev: amdgpu device pointer
424 *
425 * Allocates a scratch page of VRAM for use by various things in the
426 * driver.
427 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500428static int amdgpu_device_vram_scratch_init(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400429{
Christian Königa4a02772017-07-27 17:24:36 +0200430 return amdgpu_bo_create_kernel(adev, AMDGPU_GPU_PAGE_SIZE,
431 PAGE_SIZE, AMDGPU_GEM_DOMAIN_VRAM,
432 &adev->vram_scratch.robj,
433 &adev->vram_scratch.gpu_addr,
434 (void **)&adev->vram_scratch.ptr);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400435}
436
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500437/**
438 * amdgpu_device_vram_scratch_fini - Free the VRAM scratch page
439 *
440 * @adev: amdgpu device pointer
441 *
442 * Frees the VRAM scratch page.
443 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500444static void amdgpu_device_vram_scratch_fini(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400445{
Christian König078af1a2017-07-27 17:43:00 +0200446 amdgpu_bo_free_kernel(&adev->vram_scratch.robj, NULL, NULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400447}
448
449/**
Alex Deucher9c3f2b52017-12-14 16:20:19 -0500450 * amdgpu_device_program_register_sequence - program an array of registers.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400451 *
452 * @adev: amdgpu_device pointer
453 * @registers: pointer to the register array
454 * @array_size: size of the register array
455 *
456 * Programs an array or registers with and and or masks.
457 * This is a helper for setting golden registers.
458 */
Alex Deucher9c3f2b52017-12-14 16:20:19 -0500459void amdgpu_device_program_register_sequence(struct amdgpu_device *adev,
460 const u32 *registers,
461 const u32 array_size)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400462{
463 u32 tmp, reg, and_mask, or_mask;
464 int i;
465
466 if (array_size % 3)
467 return;
468
469 for (i = 0; i < array_size; i +=3) {
470 reg = registers[i + 0];
471 and_mask = registers[i + 1];
472 or_mask = registers[i + 2];
473
474 if (and_mask == 0xffffffff) {
475 tmp = or_mask;
476 } else {
477 tmp = RREG32(reg);
478 tmp &= ~and_mask;
479 tmp |= or_mask;
480 }
481 WREG32(reg, tmp);
482 }
483}
484
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500485/**
486 * amdgpu_device_pci_config_reset - reset the GPU
487 *
488 * @adev: amdgpu_device pointer
489 *
490 * Resets the GPU using the pci config reset sequence.
491 * Only applicable to asics prior to vega10.
492 */
Alex Deucher8111c382017-12-14 16:22:53 -0500493void amdgpu_device_pci_config_reset(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400494{
495 pci_write_config_dword(adev->pdev, 0x7c, AMDGPU_ASIC_RESET_DATA);
496}
497
498/*
499 * GPU doorbell aperture helpers function.
500 */
501/**
Alex Deucher06ec9072017-12-14 15:02:39 -0500502 * amdgpu_device_doorbell_init - Init doorbell driver information.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400503 *
504 * @adev: amdgpu_device pointer
505 *
506 * Init doorbell driver information (CIK)
507 * Returns 0 on success, error on failure.
508 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500509static int amdgpu_device_doorbell_init(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400510{
Christian König705e5192017-06-08 11:15:16 +0200511 /* No doorbell on SI hardware generation */
512 if (adev->asic_type < CHIP_BONAIRE) {
513 adev->doorbell.base = 0;
514 adev->doorbell.size = 0;
515 adev->doorbell.num_doorbells = 0;
516 adev->doorbell.ptr = NULL;
517 return 0;
518 }
519
Christian Königd6895ad2017-02-28 10:36:43 +0100520 if (pci_resource_flags(adev->pdev, 2) & IORESOURCE_UNSET)
521 return -EINVAL;
522
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400523 /* doorbell bar mapping */
524 adev->doorbell.base = pci_resource_start(adev->pdev, 2);
525 adev->doorbell.size = pci_resource_len(adev->pdev, 2);
526
Christian Königedf600d2016-05-03 15:54:54 +0200527 adev->doorbell.num_doorbells = min_t(u32, adev->doorbell.size / sizeof(u32),
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400528 AMDGPU_DOORBELL_MAX_ASSIGNMENT+1);
529 if (adev->doorbell.num_doorbells == 0)
530 return -EINVAL;
531
Christian König8972e5d2017-03-06 13:34:57 +0100532 adev->doorbell.ptr = ioremap(adev->doorbell.base,
533 adev->doorbell.num_doorbells *
534 sizeof(u32));
535 if (adev->doorbell.ptr == NULL)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400536 return -ENOMEM;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400537
538 return 0;
539}
540
541/**
Alex Deucher06ec9072017-12-14 15:02:39 -0500542 * amdgpu_device_doorbell_fini - Tear down doorbell driver information.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400543 *
544 * @adev: amdgpu_device pointer
545 *
546 * Tear down doorbell driver information (CIK)
547 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500548static void amdgpu_device_doorbell_fini(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400549{
550 iounmap(adev->doorbell.ptr);
551 adev->doorbell.ptr = NULL;
552}
553
Alex Deucher22cb0162017-12-14 16:27:11 -0500554
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400555
556/*
Alex Deucher06ec9072017-12-14 15:02:39 -0500557 * amdgpu_device_wb_*()
Alex Xie455a7bc2017-05-08 21:36:03 -0400558 * Writeback is the method by which the GPU updates special pages in memory
Alex Xieea81a172017-05-08 13:41:11 -0400559 * with the status of certain GPU events (fences, ring pointers,etc.).
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400560 */
561
562/**
Alex Deucher06ec9072017-12-14 15:02:39 -0500563 * amdgpu_device_wb_fini - Disable Writeback and free memory
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400564 *
565 * @adev: amdgpu_device pointer
566 *
567 * Disables Writeback and frees the Writeback memory (all asics).
568 * Used at driver shutdown.
569 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500570static void amdgpu_device_wb_fini(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400571{
572 if (adev->wb.wb_obj) {
Alex Deuchera76ed482016-10-21 15:30:36 -0400573 amdgpu_bo_free_kernel(&adev->wb.wb_obj,
574 &adev->wb.gpu_addr,
575 (void **)&adev->wb.wb);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400576 adev->wb.wb_obj = NULL;
577 }
578}
579
580/**
Alex Deucher06ec9072017-12-14 15:02:39 -0500581 * amdgpu_device_wb_init- Init Writeback driver info and allocate memory
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400582 *
583 * @adev: amdgpu_device pointer
584 *
Alex Xie455a7bc2017-05-08 21:36:03 -0400585 * Initializes writeback and allocates writeback memory (all asics).
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400586 * Used at driver startup.
587 * Returns 0 on success or an -error on failure.
588 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500589static int amdgpu_device_wb_init(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400590{
591 int r;
592
593 if (adev->wb.wb_obj == NULL) {
Alex Deucher97407b62017-07-28 12:14:15 -0400594 /* AMDGPU_MAX_WB * sizeof(uint32_t) * 8 = AMDGPU_MAX_WB 256bit slots */
595 r = amdgpu_bo_create_kernel(adev, AMDGPU_MAX_WB * sizeof(uint32_t) * 8,
Alex Deuchera76ed482016-10-21 15:30:36 -0400596 PAGE_SIZE, AMDGPU_GEM_DOMAIN_GTT,
597 &adev->wb.wb_obj, &adev->wb.gpu_addr,
598 (void **)&adev->wb.wb);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400599 if (r) {
600 dev_warn(adev->dev, "(%d) create WB bo failed\n", r);
601 return r;
602 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400603
604 adev->wb.num_wb = AMDGPU_MAX_WB;
605 memset(&adev->wb.used, 0, sizeof(adev->wb.used));
606
607 /* clear wb memory */
Monk Liu73469582017-12-29 17:06:41 +0800608 memset((char *)adev->wb.wb, 0, AMDGPU_MAX_WB * sizeof(uint32_t) * 8);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400609 }
610
611 return 0;
612}
613
614/**
Alex Deucher131b4b32017-12-14 16:03:43 -0500615 * amdgpu_device_wb_get - Allocate a wb entry
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400616 *
617 * @adev: amdgpu_device pointer
618 * @wb: wb index
619 *
620 * Allocate a wb slot for use by the driver (all asics).
621 * Returns 0 on success or -EINVAL on failure.
622 */
Alex Deucher131b4b32017-12-14 16:03:43 -0500623int amdgpu_device_wb_get(struct amdgpu_device *adev, u32 *wb)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400624{
625 unsigned long offset = find_first_zero_bit(adev->wb.used, adev->wb.num_wb);
Alex Deucher97407b62017-07-28 12:14:15 -0400626
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400627 if (offset < adev->wb.num_wb) {
628 __set_bit(offset, adev->wb.used);
Monk Liu63ae07c2017-10-17 19:18:56 +0800629 *wb = offset << 3; /* convert to dw offset */
Monk Liu0915fdb2017-06-19 10:19:41 -0400630 return 0;
631 } else {
632 return -EINVAL;
633 }
634}
635
Ken Wang70142852016-03-18 15:08:49 +0800636/**
Alex Deucher131b4b32017-12-14 16:03:43 -0500637 * amdgpu_device_wb_free - Free a wb entry
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400638 *
639 * @adev: amdgpu_device pointer
640 * @wb: wb index
641 *
642 * Free a wb slot allocated for use by the driver (all asics)
643 */
Alex Deucher131b4b32017-12-14 16:03:43 -0500644void amdgpu_device_wb_free(struct amdgpu_device *adev, u32 wb)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400645{
Monk Liu73469582017-12-29 17:06:41 +0800646 wb >>= 3;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400647 if (wb < adev->wb.num_wb)
Monk Liu73469582017-12-29 17:06:41 +0800648 __clear_bit(wb, adev->wb.used);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400649}
650
651/**
Alex Deucher2543e282017-12-14 16:33:36 -0500652 * amdgpu_device_vram_location - try to find VRAM location
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500653 *
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400654 * @adev: amdgpu device structure holding all necessary informations
655 * @mc: memory controller structure holding memory informations
656 * @base: base address at which to put VRAM
657 *
Alex Xie455a7bc2017-05-08 21:36:03 -0400658 * Function will try to place VRAM at base address provided
Christian König3d647c82017-11-16 19:36:10 +0100659 * as parameter.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400660 */
Alex Deucher2543e282017-12-14 16:33:36 -0500661void amdgpu_device_vram_location(struct amdgpu_device *adev,
Christian König770d13b2018-01-12 14:52:22 +0100662 struct amdgpu_gmc *mc, u64 base)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400663{
664 uint64_t limit = (uint64_t)amdgpu_vram_limit << 20;
665
666 mc->vram_start = base;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400667 mc->vram_end = mc->vram_start + mc->mc_vram_size - 1;
668 if (limit && limit < mc->real_vram_size)
669 mc->real_vram_size = limit;
670 dev_info(adev->dev, "VRAM: %lluM 0x%016llX - 0x%016llX (%lluM used)\n",
671 mc->mc_vram_size >> 20, mc->vram_start,
672 mc->vram_end, mc->real_vram_size >> 20);
673}
674
675/**
Alex Deucher2543e282017-12-14 16:33:36 -0500676 * amdgpu_device_gart_location - try to find GTT location
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500677 *
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400678 * @adev: amdgpu device structure holding all necessary informations
679 * @mc: memory controller structure holding memory informations
680 *
681 * Function will place try to place GTT before or after VRAM.
682 *
683 * If GTT size is bigger than space left then we ajust GTT size.
684 * Thus function will never fails.
685 *
686 * FIXME: when reducing GTT size align new size on power of 2.
687 */
Alex Deucher2543e282017-12-14 16:33:36 -0500688void amdgpu_device_gart_location(struct amdgpu_device *adev,
Christian König770d13b2018-01-12 14:52:22 +0100689 struct amdgpu_gmc *mc)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400690{
691 u64 size_af, size_bf;
692
Christian König770d13b2018-01-12 14:52:22 +0100693 size_af = adev->gmc.mc_mask - mc->vram_end;
Christian Königed21c042017-07-06 22:26:05 +0200694 size_bf = mc->vram_start;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400695 if (size_bf > size_af) {
Christian König6f02a692017-07-07 11:56:59 +0200696 if (mc->gart_size > size_bf) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400697 dev_warn(adev->dev, "limiting GTT\n");
Christian König6f02a692017-07-07 11:56:59 +0200698 mc->gart_size = size_bf;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400699 }
Christian König6f02a692017-07-07 11:56:59 +0200700 mc->gart_start = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400701 } else {
Christian König6f02a692017-07-07 11:56:59 +0200702 if (mc->gart_size > size_af) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400703 dev_warn(adev->dev, "limiting GTT\n");
Christian König6f02a692017-07-07 11:56:59 +0200704 mc->gart_size = size_af;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400705 }
Christian Königb98f1b92017-11-16 20:12:51 +0100706 /* VCE doesn't like it when BOs cross a 4GB segment, so align
707 * the GART base on a 4GB boundary as well.
708 */
709 mc->gart_start = ALIGN(mc->vram_end + 1, 0x100000000ULL);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400710 }
Christian König6f02a692017-07-07 11:56:59 +0200711 mc->gart_end = mc->gart_start + mc->gart_size - 1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400712 dev_info(adev->dev, "GTT: %lluM 0x%016llX - 0x%016llX\n",
Christian König6f02a692017-07-07 11:56:59 +0200713 mc->gart_size >> 20, mc->gart_start, mc->gart_end);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400714}
715
Christian Königd6895ad2017-02-28 10:36:43 +0100716/**
717 * amdgpu_device_resize_fb_bar - try to resize FB BAR
718 *
719 * @adev: amdgpu_device pointer
720 *
721 * Try to resize FB BAR to make all VRAM CPU accessible. We try very hard not
722 * to fail, but if any of the BARs is not accessible after the size we abort
723 * driver loading by returning -ENODEV.
724 */
725int amdgpu_device_resize_fb_bar(struct amdgpu_device *adev)
726{
Christian König770d13b2018-01-12 14:52:22 +0100727 u64 space_needed = roundup_pow_of_two(adev->gmc.real_vram_size);
Christian Königd6895ad2017-02-28 10:36:43 +0100728 u32 rbar_size = order_base_2(((space_needed >> 20) | 1)) - 1;
Christian König31b8ada2017-11-15 20:07:38 +0100729 struct pci_bus *root;
730 struct resource *res;
731 unsigned i;
Christian Königd6895ad2017-02-28 10:36:43 +0100732 u16 cmd;
733 int r;
734
pding0c03b912017-11-07 11:02:00 +0800735 /* Bypass for VF */
736 if (amdgpu_sriov_vf(adev))
737 return 0;
738
Christian König31b8ada2017-11-15 20:07:38 +0100739 /* Check if the root BUS has 64bit memory resources */
740 root = adev->pdev->bus;
741 while (root->parent)
742 root = root->parent;
743
744 pci_bus_for_each_resource(root, res, i) {
Christian König0ebb7c52018-01-07 10:18:57 +0100745 if (res && res->flags & (IORESOURCE_MEM | IORESOURCE_MEM_64) &&
Christian König31b8ada2017-11-15 20:07:38 +0100746 res->start > 0x100000000ull)
747 break;
748 }
749
750 /* Trying to resize is pointless without a root hub window above 4GB */
751 if (!res)
752 return 0;
753
Christian Königd6895ad2017-02-28 10:36:43 +0100754 /* Disable memory decoding while we change the BAR addresses and size */
755 pci_read_config_word(adev->pdev, PCI_COMMAND, &cmd);
756 pci_write_config_word(adev->pdev, PCI_COMMAND,
757 cmd & ~PCI_COMMAND_MEMORY);
758
759 /* Free the VRAM and doorbell BAR, we most likely need to move both. */
Alex Deucher06ec9072017-12-14 15:02:39 -0500760 amdgpu_device_doorbell_fini(adev);
Christian Königd6895ad2017-02-28 10:36:43 +0100761 if (adev->asic_type >= CHIP_BONAIRE)
762 pci_release_resource(adev->pdev, 2);
763
764 pci_release_resource(adev->pdev, 0);
765
766 r = pci_resize_resource(adev->pdev, 0, rbar_size);
767 if (r == -ENOSPC)
768 DRM_INFO("Not enough PCI address space for a large BAR.");
769 else if (r && r != -ENOTSUPP)
770 DRM_ERROR("Problem resizing BAR0 (%d).", r);
771
772 pci_assign_unassigned_bus_resources(adev->pdev->bus);
773
774 /* When the doorbell or fb BAR isn't available we have no chance of
775 * using the device.
776 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500777 r = amdgpu_device_doorbell_init(adev);
Christian Königd6895ad2017-02-28 10:36:43 +0100778 if (r || (pci_resource_flags(adev->pdev, 0) & IORESOURCE_UNSET))
779 return -ENODEV;
780
781 pci_write_config_word(adev->pdev, PCI_COMMAND, cmd);
782
783 return 0;
784}
Horace Chena05502e2017-09-29 14:41:57 +0800785
786/*
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400787 * GPU helpers function.
788 */
789/**
Alex Deucher39c640c2017-12-15 16:22:11 -0500790 * amdgpu_device_need_post - check if the hw need post or not
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400791 *
792 * @adev: amdgpu_device pointer
793 *
Jim Quc836fec2017-02-10 15:59:59 +0800794 * Check if the asic has been initialized (all asics) at driver startup
795 * or post is needed if hw reset is performed.
796 * Returns true if need or false if not.
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400797 */
Alex Deucher39c640c2017-12-15 16:22:11 -0500798bool amdgpu_device_need_post(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400799{
800 uint32_t reg;
801
Monk Liubec86372016-09-14 19:38:08 +0800802 if (amdgpu_sriov_vf(adev))
803 return false;
804
805 if (amdgpu_passthrough(adev)) {
Monk Liu1da2c322016-11-11 11:24:29 +0800806 /* for FIJI: In whole GPU pass-through virtualization case, after VM reboot
807 * some old smc fw still need driver do vPost otherwise gpu hang, while
808 * those smc fw version above 22.15 doesn't have this flaw, so we force
809 * vpost executed for smc version below 22.15
Monk Liubec86372016-09-14 19:38:08 +0800810 */
811 if (adev->asic_type == CHIP_FIJI) {
812 int err;
813 uint32_t fw_ver;
814 err = request_firmware(&adev->pm.fw, "amdgpu/fiji_smc.bin", adev->dev);
815 /* force vPost if error occured */
816 if (err)
817 return true;
818
819 fw_ver = *((uint32_t *)adev->pm.fw->data + 69);
Monk Liu1da2c322016-11-11 11:24:29 +0800820 if (fw_ver < 0x00160e00)
821 return true;
Monk Liubec86372016-09-14 19:38:08 +0800822 }
Monk Liubec86372016-09-14 19:38:08 +0800823 }
pding91fe77e2017-10-19 09:38:39 +0800824
825 if (adev->has_hw_reset) {
826 adev->has_hw_reset = false;
827 return true;
828 }
829
830 /* bios scratch used on CIK+ */
831 if (adev->asic_type >= CHIP_BONAIRE)
832 return amdgpu_atombios_scratch_need_asic_init(adev);
833
834 /* check MEM_SIZE for older asics */
835 reg = amdgpu_asic_get_config_memsize(adev);
836
837 if ((reg != 0) && (reg != 0xffffffff))
838 return false;
839
840 return true;
Monk Liubec86372016-09-14 19:38:08 +0800841}
842
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400843/* if we get transitioned to only one device, take VGA back */
844/**
Alex Deucher06ec9072017-12-14 15:02:39 -0500845 * amdgpu_device_vga_set_decode - enable/disable vga decode
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400846 *
847 * @cookie: amdgpu_device pointer
848 * @state: enable/disable vga decode
849 *
850 * Enable/disable vga decode (all asics).
851 * Returns VGA resource flags.
852 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500853static unsigned int amdgpu_device_vga_set_decode(void *cookie, bool state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400854{
855 struct amdgpu_device *adev = cookie;
856 amdgpu_asic_set_vga_state(adev, state);
857 if (state)
858 return VGA_RSRC_LEGACY_IO | VGA_RSRC_LEGACY_MEM |
859 VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
860 else
861 return VGA_RSRC_NORMAL_IO | VGA_RSRC_NORMAL_MEM;
862}
863
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500864/**
865 * amdgpu_device_check_block_size - validate the vm block size
866 *
867 * @adev: amdgpu_device pointer
868 *
869 * Validates the vm block size specified via module parameter.
870 * The vm block size defines number of bits in page table versus page directory,
871 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
872 * page table and the remaining bits are in the page directory.
873 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500874static void amdgpu_device_check_block_size(struct amdgpu_device *adev)
Chunming Zhoua1adf8b2017-03-27 11:36:57 +0800875{
876 /* defines number of bits in page table versus page directory,
877 * a page is 4KB so we have 12 bits offset, minimum 9 bits in the
878 * page table and the remaining bits are in the page directory */
Junwei Zhangbab4fee2017-04-05 13:54:56 +0800879 if (amdgpu_vm_block_size == -1)
880 return;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +0800881
Junwei Zhangbab4fee2017-04-05 13:54:56 +0800882 if (amdgpu_vm_block_size < 9) {
Chunming Zhoua1adf8b2017-03-27 11:36:57 +0800883 dev_warn(adev->dev, "VM page table size (%d) too small\n",
884 amdgpu_vm_block_size);
Christian König97489122017-11-27 16:22:05 +0100885 amdgpu_vm_block_size = -1;
Chunming Zhoua1adf8b2017-03-27 11:36:57 +0800886 }
Chunming Zhoua1adf8b2017-03-27 11:36:57 +0800887}
888
Alex Deuchere3ecdff2018-03-15 17:39:45 -0500889/**
890 * amdgpu_device_check_vm_size - validate the vm size
891 *
892 * @adev: amdgpu_device pointer
893 *
894 * Validates the vm size in GB specified via module parameter.
895 * The VM size is the size of the GPU virtual memory space in GB.
896 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500897static void amdgpu_device_check_vm_size(struct amdgpu_device *adev)
Zhang, Jerry83ca1452017-03-29 16:08:31 +0800898{
Alex Deucher64dab072017-06-15 18:20:09 -0400899 /* no need to check the default value */
900 if (amdgpu_vm_size == -1)
901 return;
902
Zhang, Jerry83ca1452017-03-29 16:08:31 +0800903 if (amdgpu_vm_size < 1) {
904 dev_warn(adev->dev, "VM size (%d) too small, min is 1GB\n",
905 amdgpu_vm_size);
Christian Königf3368122017-11-23 12:57:18 +0100906 amdgpu_vm_size = -1;
Zhang, Jerry83ca1452017-03-29 16:08:31 +0800907 }
Zhang, Jerry83ca1452017-03-29 16:08:31 +0800908}
909
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400910/**
Alex Deucher06ec9072017-12-14 15:02:39 -0500911 * amdgpu_device_check_arguments - validate module params
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400912 *
913 * @adev: amdgpu_device pointer
914 *
915 * Validates certain module parameters and updates
916 * the associated values used by the driver (all asics).
917 */
Alex Deucher06ec9072017-12-14 15:02:39 -0500918static void amdgpu_device_check_arguments(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400919{
Chunming Zhou5b011232015-12-10 17:34:33 +0800920 if (amdgpu_sched_jobs < 4) {
921 dev_warn(adev->dev, "sched jobs (%d) must be at least 4\n",
922 amdgpu_sched_jobs);
923 amdgpu_sched_jobs = 4;
Alex Deucher76117502017-06-21 12:31:41 -0400924 } else if (!is_power_of_2(amdgpu_sched_jobs)){
Chunming Zhou5b011232015-12-10 17:34:33 +0800925 dev_warn(adev->dev, "sched jobs (%d) must be a power of 2\n",
926 amdgpu_sched_jobs);
927 amdgpu_sched_jobs = roundup_pow_of_two(amdgpu_sched_jobs);
928 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400929
Alex Deucher83e74db2017-08-21 11:58:25 -0400930 if (amdgpu_gart_size != -1 && amdgpu_gart_size < 32) {
Christian Königf9321cc2017-07-07 13:44:05 +0200931 /* gart size must be greater or equal to 32M */
932 dev_warn(adev->dev, "gart size (%d) too small\n",
933 amdgpu_gart_size);
Alex Deucher83e74db2017-08-21 11:58:25 -0400934 amdgpu_gart_size = -1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400935 }
936
Christian König36d38372017-07-07 13:17:45 +0200937 if (amdgpu_gtt_size != -1 && amdgpu_gtt_size < 32) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400938 /* gtt size must be greater or equal to 32M */
Christian König36d38372017-07-07 13:17:45 +0200939 dev_warn(adev->dev, "gtt size (%d) too small\n",
940 amdgpu_gtt_size);
941 amdgpu_gtt_size = -1;
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400942 }
943
Roger Hed07f14b2017-08-15 16:05:59 +0800944 /* valid range is between 4 and 9 inclusive */
945 if (amdgpu_vm_fragment_size != -1 &&
946 (amdgpu_vm_fragment_size > 9 || amdgpu_vm_fragment_size < 4)) {
947 dev_warn(adev->dev, "valid range is between 4 and 9\n");
948 amdgpu_vm_fragment_size = -1;
949 }
950
Alex Deucher06ec9072017-12-14 15:02:39 -0500951 amdgpu_device_check_vm_size(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400952
Alex Deucher06ec9072017-12-14 15:02:39 -0500953 amdgpu_device_check_block_size(adev);
Christian König6a7f76e2016-08-24 15:51:49 +0200954
jimqu526bae32016-11-07 09:53:10 +0800955 if (amdgpu_vram_page_split != -1 && (amdgpu_vram_page_split < 16 ||
Alex Deucher76117502017-06-21 12:31:41 -0400956 !is_power_of_2(amdgpu_vram_page_split))) {
Christian König6a7f76e2016-08-24 15:51:49 +0200957 dev_warn(adev->dev, "invalid VRAM page split (%d)\n",
958 amdgpu_vram_page_split);
959 amdgpu_vram_page_split = 1024;
960 }
Andrey Grodzovsky88546952017-12-13 14:36:53 -0500961
962 if (amdgpu_lockup_timeout == 0) {
963 dev_warn(adev->dev, "lockup_timeout msut be > 0, adjusting to 10000\n");
964 amdgpu_lockup_timeout = 10000;
965 }
Alex Deucher19aede72018-03-09 15:06:35 -0500966
967 adev->firmware.load_type = amdgpu_ucode_get_load_type(adev, amdgpu_fw_load_type);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400968}
969
970/**
971 * amdgpu_switcheroo_set_state - set switcheroo state
972 *
973 * @pdev: pci dev pointer
Lukas Wunner16944672015-09-05 11:17:35 +0200974 * @state: vga_switcheroo state
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400975 *
976 * Callback for the switcheroo driver. Suspends or resumes the
977 * the asics before or after it is powered up using ACPI methods.
978 */
979static void amdgpu_switcheroo_set_state(struct pci_dev *pdev, enum vga_switcheroo_state state)
980{
981 struct drm_device *dev = pci_get_drvdata(pdev);
982
983 if (amdgpu_device_is_px(dev) && state == VGA_SWITCHEROO_OFF)
984 return;
985
986 if (state == VGA_SWITCHEROO_ON) {
Joe Perches7ca85292017-02-28 04:55:52 -0800987 pr_info("amdgpu: switched on\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400988 /* don't suspend or resume card normally */
989 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
990
Alex Deucher810ddc32016-08-23 13:25:49 -0400991 amdgpu_device_resume(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400992
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400993 dev->switch_power_state = DRM_SWITCH_POWER_ON;
994 drm_kms_helper_poll_enable(dev);
995 } else {
Joe Perches7ca85292017-02-28 04:55:52 -0800996 pr_info("amdgpu: switched off\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -0400997 drm_kms_helper_poll_disable(dev);
998 dev->switch_power_state = DRM_SWITCH_POWER_CHANGING;
Alex Deucher810ddc32016-08-23 13:25:49 -0400999 amdgpu_device_suspend(dev, true, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001000 dev->switch_power_state = DRM_SWITCH_POWER_OFF;
1001 }
1002}
1003
1004/**
1005 * amdgpu_switcheroo_can_switch - see if switcheroo state can change
1006 *
1007 * @pdev: pci dev pointer
1008 *
1009 * Callback for the switcheroo driver. Check of the switcheroo
1010 * state can be changed.
1011 * Returns true if the state can be changed, false if not.
1012 */
1013static bool amdgpu_switcheroo_can_switch(struct pci_dev *pdev)
1014{
1015 struct drm_device *dev = pci_get_drvdata(pdev);
1016
1017 /*
1018 * FIXME: open_count is protected by drm_global_mutex but that would lead to
1019 * locking inversion with the driver load path. And the access here is
1020 * completely racy anyway. So don't bother with locking for now.
1021 */
1022 return dev->open_count == 0;
1023}
1024
1025static const struct vga_switcheroo_client_ops amdgpu_switcheroo_ops = {
1026 .set_gpu_state = amdgpu_switcheroo_set_state,
1027 .reprobe = NULL,
1028 .can_switch = amdgpu_switcheroo_can_switch,
1029};
1030
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001031/**
1032 * amdgpu_device_ip_set_clockgating_state - set the CG state
1033 *
1034 * @adev: amdgpu_device pointer
1035 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1036 * @state: clockgating state (gate or ungate)
1037 *
1038 * Sets the requested clockgating state for all instances of
1039 * the hardware IP specified.
1040 * Returns the error code from the last instance.
1041 */
Rex Zhu43fa5612018-03-28 13:42:45 -05001042int amdgpu_device_ip_set_clockgating_state(void *dev,
Alex Deucher2990a1f2017-12-15 16:18:00 -05001043 enum amd_ip_block_type block_type,
1044 enum amd_clockgating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001045{
Rex Zhu43fa5612018-03-28 13:42:45 -05001046 struct amdgpu_device *adev = dev;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001047 int i, r = 0;
1048
1049 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001050 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001051 continue;
Rex Zhuc7228652017-02-22 15:33:46 +08001052 if (adev->ip_blocks[i].version->type != block_type)
1053 continue;
1054 if (!adev->ip_blocks[i].version->funcs->set_clockgating_state)
1055 continue;
1056 r = adev->ip_blocks[i].version->funcs->set_clockgating_state(
1057 (void *)adev, state);
1058 if (r)
1059 DRM_ERROR("set_clockgating_state of IP block <%s> failed %d\n",
1060 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001061 }
1062 return r;
1063}
1064
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001065/**
1066 * amdgpu_device_ip_set_powergating_state - set the PG state
1067 *
1068 * @adev: amdgpu_device pointer
1069 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1070 * @state: powergating state (gate or ungate)
1071 *
1072 * Sets the requested powergating state for all instances of
1073 * the hardware IP specified.
1074 * Returns the error code from the last instance.
1075 */
Rex Zhu43fa5612018-03-28 13:42:45 -05001076int amdgpu_device_ip_set_powergating_state(void *dev,
Alex Deucher2990a1f2017-12-15 16:18:00 -05001077 enum amd_ip_block_type block_type,
1078 enum amd_powergating_state state)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001079{
Rex Zhu43fa5612018-03-28 13:42:45 -05001080 struct amdgpu_device *adev = dev;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001081 int i, r = 0;
1082
1083 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001084 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001085 continue;
Rex Zhuc7228652017-02-22 15:33:46 +08001086 if (adev->ip_blocks[i].version->type != block_type)
1087 continue;
1088 if (!adev->ip_blocks[i].version->funcs->set_powergating_state)
1089 continue;
1090 r = adev->ip_blocks[i].version->funcs->set_powergating_state(
1091 (void *)adev, state);
1092 if (r)
1093 DRM_ERROR("set_powergating_state of IP block <%s> failed %d\n",
1094 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001095 }
1096 return r;
1097}
1098
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001099/**
1100 * amdgpu_device_ip_get_clockgating_state - get the CG state
1101 *
1102 * @adev: amdgpu_device pointer
1103 * @flags: clockgating feature flags
1104 *
1105 * Walks the list of IPs on the device and updates the clockgating
1106 * flags for each IP.
1107 * Updates @flags with the feature flags for each hardware IP where
1108 * clockgating is enabled.
1109 */
Alex Deucher2990a1f2017-12-15 16:18:00 -05001110void amdgpu_device_ip_get_clockgating_state(struct amdgpu_device *adev,
1111 u32 *flags)
Huang Rui6cb2d4e2017-01-05 18:44:41 +08001112{
1113 int i;
1114
1115 for (i = 0; i < adev->num_ip_blocks; i++) {
1116 if (!adev->ip_blocks[i].status.valid)
1117 continue;
1118 if (adev->ip_blocks[i].version->funcs->get_clockgating_state)
1119 adev->ip_blocks[i].version->funcs->get_clockgating_state((void *)adev, flags);
1120 }
1121}
1122
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001123/**
1124 * amdgpu_device_ip_wait_for_idle - wait for idle
1125 *
1126 * @adev: amdgpu_device pointer
1127 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1128 *
1129 * Waits for the request hardware IP to be idle.
1130 * Returns 0 for success or a negative error code on failure.
1131 */
Alex Deucher2990a1f2017-12-15 16:18:00 -05001132int amdgpu_device_ip_wait_for_idle(struct amdgpu_device *adev,
1133 enum amd_ip_block_type block_type)
Alex Deucher5dbbb602016-06-23 11:41:04 -04001134{
1135 int i, r;
1136
1137 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001138 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001139 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001140 if (adev->ip_blocks[i].version->type == block_type) {
1141 r = adev->ip_blocks[i].version->funcs->wait_for_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -04001142 if (r)
1143 return r;
1144 break;
1145 }
1146 }
1147 return 0;
1148
1149}
1150
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001151/**
1152 * amdgpu_device_ip_is_idle - is the hardware IP idle
1153 *
1154 * @adev: amdgpu_device pointer
1155 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1156 *
1157 * Check if the hardware IP is idle or not.
1158 * Returns true if it the IP is idle, false if not.
1159 */
Alex Deucher2990a1f2017-12-15 16:18:00 -05001160bool amdgpu_device_ip_is_idle(struct amdgpu_device *adev,
1161 enum amd_ip_block_type block_type)
Alex Deucher5dbbb602016-06-23 11:41:04 -04001162{
1163 int i;
1164
1165 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001166 if (!adev->ip_blocks[i].status.valid)
Alex Deucher9ecbe7f2016-06-23 11:53:12 -04001167 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001168 if (adev->ip_blocks[i].version->type == block_type)
1169 return adev->ip_blocks[i].version->funcs->is_idle((void *)adev);
Alex Deucher5dbbb602016-06-23 11:41:04 -04001170 }
1171 return true;
1172
1173}
1174
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001175/**
1176 * amdgpu_device_ip_get_ip_block - get a hw IP pointer
1177 *
1178 * @adev: amdgpu_device pointer
1179 * @block_type: Type of hardware IP (SMU, GFX, UVD, etc.)
1180 *
1181 * Returns a pointer to the hardware IP block structure
1182 * if it exists for the asic, otherwise NULL.
1183 */
Alex Deucher2990a1f2017-12-15 16:18:00 -05001184struct amdgpu_ip_block *
1185amdgpu_device_ip_get_ip_block(struct amdgpu_device *adev,
1186 enum amd_ip_block_type type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001187{
1188 int i;
1189
1190 for (i = 0; i < adev->num_ip_blocks; i++)
Alex Deuchera1255102016-10-13 17:41:13 -04001191 if (adev->ip_blocks[i].version->type == type)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001192 return &adev->ip_blocks[i];
1193
1194 return NULL;
1195}
1196
1197/**
Alex Deucher2990a1f2017-12-15 16:18:00 -05001198 * amdgpu_device_ip_block_version_cmp
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001199 *
1200 * @adev: amdgpu_device pointer
yanyang15fc3aee2015-05-22 14:39:35 -04001201 * @type: enum amd_ip_block_type
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001202 * @major: major version
1203 * @minor: minor version
1204 *
1205 * return 0 if equal or greater
1206 * return 1 if smaller or the ip_block doesn't exist
1207 */
Alex Deucher2990a1f2017-12-15 16:18:00 -05001208int amdgpu_device_ip_block_version_cmp(struct amdgpu_device *adev,
1209 enum amd_ip_block_type type,
1210 u32 major, u32 minor)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001211{
Alex Deucher2990a1f2017-12-15 16:18:00 -05001212 struct amdgpu_ip_block *ip_block = amdgpu_device_ip_get_ip_block(adev, type);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001213
Alex Deuchera1255102016-10-13 17:41:13 -04001214 if (ip_block && ((ip_block->version->major > major) ||
1215 ((ip_block->version->major == major) &&
1216 (ip_block->version->minor >= minor))))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001217 return 0;
1218
1219 return 1;
1220}
1221
Alex Deuchera1255102016-10-13 17:41:13 -04001222/**
Alex Deucher2990a1f2017-12-15 16:18:00 -05001223 * amdgpu_device_ip_block_add
Alex Deuchera1255102016-10-13 17:41:13 -04001224 *
1225 * @adev: amdgpu_device pointer
1226 * @ip_block_version: pointer to the IP to add
1227 *
1228 * Adds the IP block driver information to the collection of IPs
1229 * on the asic.
1230 */
Alex Deucher2990a1f2017-12-15 16:18:00 -05001231int amdgpu_device_ip_block_add(struct amdgpu_device *adev,
1232 const struct amdgpu_ip_block_version *ip_block_version)
Alex Deuchera1255102016-10-13 17:41:13 -04001233{
1234 if (!ip_block_version)
1235 return -EINVAL;
1236
Shaoyun Liue966a722018-02-01 16:45:26 -05001237 DRM_INFO("add ip block number %d <%s>\n", adev->num_ip_blocks,
Huang Ruia0bae352017-05-03 09:52:06 +08001238 ip_block_version->funcs->name);
1239
Alex Deuchera1255102016-10-13 17:41:13 -04001240 adev->ip_blocks[adev->num_ip_blocks++].version = ip_block_version;
1241
1242 return 0;
1243}
1244
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001245/**
1246 * amdgpu_device_enable_virtual_display - enable virtual display feature
1247 *
1248 * @adev: amdgpu_device pointer
1249 *
1250 * Enabled the virtual display feature if the user has enabled it via
1251 * the module parameter virtual_display. This feature provides a virtual
1252 * display hardware on headless boards or in virtualized environments.
1253 * This function parses and validates the configuration string specified by
1254 * the user and configues the virtual display configuration (number of
1255 * virtual connectors, crtcs, etc.) specified.
1256 */
Alex Deucher483ef982016-09-30 12:43:04 -04001257static void amdgpu_device_enable_virtual_display(struct amdgpu_device *adev)
Emily Deng9accf2f2016-08-10 16:01:25 +08001258{
1259 adev->enable_virtual_display = false;
1260
1261 if (amdgpu_virtual_display) {
1262 struct drm_device *ddev = adev->ddev;
1263 const char *pci_address_name = pci_name(ddev->pdev);
Emily Deng0f663562016-09-30 13:02:18 -04001264 char *pciaddstr, *pciaddstr_tmp, *pciaddname_tmp, *pciaddname;
Emily Deng9accf2f2016-08-10 16:01:25 +08001265
1266 pciaddstr = kstrdup(amdgpu_virtual_display, GFP_KERNEL);
1267 pciaddstr_tmp = pciaddstr;
Emily Deng0f663562016-09-30 13:02:18 -04001268 while ((pciaddname_tmp = strsep(&pciaddstr_tmp, ";"))) {
1269 pciaddname = strsep(&pciaddname_tmp, ",");
Yintian Tao967de2a2017-01-22 15:16:51 +08001270 if (!strcmp("all", pciaddname)
1271 || !strcmp(pci_address_name, pciaddname)) {
Emily Deng0f663562016-09-30 13:02:18 -04001272 long num_crtc;
1273 int res = -1;
1274
Emily Deng9accf2f2016-08-10 16:01:25 +08001275 adev->enable_virtual_display = true;
Emily Deng0f663562016-09-30 13:02:18 -04001276
1277 if (pciaddname_tmp)
1278 res = kstrtol(pciaddname_tmp, 10,
1279 &num_crtc);
1280
1281 if (!res) {
1282 if (num_crtc < 1)
1283 num_crtc = 1;
1284 if (num_crtc > 6)
1285 num_crtc = 6;
1286 adev->mode_info.num_crtc = num_crtc;
1287 } else {
1288 adev->mode_info.num_crtc = 1;
1289 }
Emily Deng9accf2f2016-08-10 16:01:25 +08001290 break;
1291 }
1292 }
1293
Emily Deng0f663562016-09-30 13:02:18 -04001294 DRM_INFO("virtual display string:%s, %s:virtual_display:%d, num_crtc:%d\n",
1295 amdgpu_virtual_display, pci_address_name,
1296 adev->enable_virtual_display, adev->mode_info.num_crtc);
Emily Deng9accf2f2016-08-10 16:01:25 +08001297
1298 kfree(pciaddstr);
1299 }
1300}
1301
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001302/**
1303 * amdgpu_device_parse_gpu_info_fw - parse gpu info firmware
1304 *
1305 * @adev: amdgpu_device pointer
1306 *
1307 * Parses the asic configuration parameters specified in the gpu info
1308 * firmware and makes them availale to the driver for use in configuring
1309 * the asic.
1310 * Returns 0 on success, -EINVAL on failure.
1311 */
Alex Deuchere2a75f82017-04-27 16:58:01 -04001312static int amdgpu_device_parse_gpu_info_fw(struct amdgpu_device *adev)
1313{
Alex Deuchere2a75f82017-04-27 16:58:01 -04001314 const char *chip_name;
1315 char fw_name[30];
1316 int err;
1317 const struct gpu_info_firmware_header_v1_0 *hdr;
1318
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001319 adev->firmware.gpu_info_fw = NULL;
1320
Alex Deuchere2a75f82017-04-27 16:58:01 -04001321 switch (adev->asic_type) {
1322 case CHIP_TOPAZ:
1323 case CHIP_TONGA:
1324 case CHIP_FIJI:
1325 case CHIP_POLARIS11:
1326 case CHIP_POLARIS10:
1327 case CHIP_POLARIS12:
1328 case CHIP_CARRIZO:
1329 case CHIP_STONEY:
1330#ifdef CONFIG_DRM_AMDGPU_SI
1331 case CHIP_VERDE:
1332 case CHIP_TAHITI:
1333 case CHIP_PITCAIRN:
1334 case CHIP_OLAND:
1335 case CHIP_HAINAN:
1336#endif
1337#ifdef CONFIG_DRM_AMDGPU_CIK
1338 case CHIP_BONAIRE:
1339 case CHIP_HAWAII:
1340 case CHIP_KAVERI:
1341 case CHIP_KABINI:
1342 case CHIP_MULLINS:
1343#endif
1344 default:
1345 return 0;
1346 case CHIP_VEGA10:
1347 chip_name = "vega10";
1348 break;
Alex Deucher3f76dce2017-09-01 16:20:53 -04001349 case CHIP_VEGA12:
1350 chip_name = "vega12";
1351 break;
Alex Deucher2d2e5e72017-05-09 12:27:35 -04001352 case CHIP_RAVEN:
1353 chip_name = "raven";
1354 break;
Alex Deuchere2a75f82017-04-27 16:58:01 -04001355 }
1356
1357 snprintf(fw_name, sizeof(fw_name), "amdgpu/%s_gpu_info.bin", chip_name);
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001358 err = request_firmware(&adev->firmware.gpu_info_fw, fw_name, adev->dev);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001359 if (err) {
1360 dev_err(adev->dev,
1361 "Failed to load gpu_info firmware \"%s\"\n",
1362 fw_name);
1363 goto out;
1364 }
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001365 err = amdgpu_ucode_validate(adev->firmware.gpu_info_fw);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001366 if (err) {
1367 dev_err(adev->dev,
1368 "Failed to validate gpu_info firmware \"%s\"\n",
1369 fw_name);
1370 goto out;
1371 }
1372
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001373 hdr = (const struct gpu_info_firmware_header_v1_0 *)adev->firmware.gpu_info_fw->data;
Alex Deuchere2a75f82017-04-27 16:58:01 -04001374 amdgpu_ucode_print_gpu_info_hdr(&hdr->header);
1375
1376 switch (hdr->version_major) {
1377 case 1:
1378 {
1379 const struct gpu_info_firmware_v1_0 *gpu_info_fw =
Huang Ruiab4fe3e2017-06-05 22:11:59 +08001380 (const struct gpu_info_firmware_v1_0 *)(adev->firmware.gpu_info_fw->data +
Alex Deuchere2a75f82017-04-27 16:58:01 -04001381 le32_to_cpu(hdr->header.ucode_array_offset_bytes));
1382
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001383 adev->gfx.config.max_shader_engines = le32_to_cpu(gpu_info_fw->gc_num_se);
1384 adev->gfx.config.max_cu_per_sh = le32_to_cpu(gpu_info_fw->gc_num_cu_per_sh);
1385 adev->gfx.config.max_sh_per_se = le32_to_cpu(gpu_info_fw->gc_num_sh_per_se);
1386 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 -04001387 adev->gfx.config.max_texture_channel_caches =
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001388 le32_to_cpu(gpu_info_fw->gc_num_tccs);
1389 adev->gfx.config.max_gprs = le32_to_cpu(gpu_info_fw->gc_num_gprs);
1390 adev->gfx.config.max_gs_threads = le32_to_cpu(gpu_info_fw->gc_num_max_gs_thds);
1391 adev->gfx.config.gs_vgt_table_depth = le32_to_cpu(gpu_info_fw->gc_gs_table_depth);
1392 adev->gfx.config.gs_prim_buffer_depth = le32_to_cpu(gpu_info_fw->gc_gsprim_buff_depth);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001393 adev->gfx.config.double_offchip_lds_buf =
Alex Deucherb5ab16b2017-05-11 19:09:49 -04001394 le32_to_cpu(gpu_info_fw->gc_double_offchip_lds_buffer);
1395 adev->gfx.cu_info.wave_front_size = le32_to_cpu(gpu_info_fw->gc_wave_size);
Hawking Zhang51fd0372017-06-09 22:30:52 +08001396 adev->gfx.cu_info.max_waves_per_simd =
1397 le32_to_cpu(gpu_info_fw->gc_max_waves_per_simd);
1398 adev->gfx.cu_info.max_scratch_slots_per_cu =
1399 le32_to_cpu(gpu_info_fw->gc_max_scratch_slots_per_cu);
1400 adev->gfx.cu_info.lds_size = le32_to_cpu(gpu_info_fw->gc_lds_size);
Alex Deuchere2a75f82017-04-27 16:58:01 -04001401 break;
1402 }
1403 default:
1404 dev_err(adev->dev,
1405 "Unsupported gpu_info table %d\n", hdr->header.ucode_version);
1406 err = -EINVAL;
1407 goto out;
1408 }
1409out:
Alex Deuchere2a75f82017-04-27 16:58:01 -04001410 return err;
1411}
1412
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001413/**
1414 * amdgpu_device_ip_early_init - run early init for hardware IPs
1415 *
1416 * @adev: amdgpu_device pointer
1417 *
1418 * Early initialization pass for hardware IPs. The hardware IPs that make
1419 * up each asic are discovered each IP's early_init callback is run. This
1420 * is the first stage in initializing the asic.
1421 * Returns 0 on success, negative error code on failure.
1422 */
Alex Deucher06ec9072017-12-14 15:02:39 -05001423static int amdgpu_device_ip_early_init(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001424{
Alex Deucheraaa36a92015-04-20 17:31:14 -04001425 int i, r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001426
Alex Deucher483ef982016-09-30 12:43:04 -04001427 amdgpu_device_enable_virtual_display(adev);
Emily Denga6be7572016-08-08 11:37:50 +08001428
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001429 switch (adev->asic_type) {
Alex Deucheraaa36a92015-04-20 17:31:14 -04001430 case CHIP_TOPAZ:
1431 case CHIP_TONGA:
David Zhang48299f92015-07-08 01:05:16 +08001432 case CHIP_FIJI:
Flora Cui2cc0c0b2016-03-14 18:33:29 -04001433 case CHIP_POLARIS11:
1434 case CHIP_POLARIS10:
Junwei Zhangc4642a42016-12-14 15:32:28 -05001435 case CHIP_POLARIS12:
Alex Deucheraaa36a92015-04-20 17:31:14 -04001436 case CHIP_CARRIZO:
Samuel Li39bb0c92015-10-08 16:31:43 -04001437 case CHIP_STONEY:
1438 if (adev->asic_type == CHIP_CARRIZO || adev->asic_type == CHIP_STONEY)
Alex Deucheraaa36a92015-04-20 17:31:14 -04001439 adev->family = AMDGPU_FAMILY_CZ;
1440 else
1441 adev->family = AMDGPU_FAMILY_VI;
1442
1443 r = vi_set_ip_blocks(adev);
1444 if (r)
1445 return r;
1446 break;
Ken Wang33f34802016-01-21 17:29:41 +08001447#ifdef CONFIG_DRM_AMDGPU_SI
1448 case CHIP_VERDE:
1449 case CHIP_TAHITI:
1450 case CHIP_PITCAIRN:
1451 case CHIP_OLAND:
1452 case CHIP_HAINAN:
Ken Wang295d0da2016-05-24 21:02:53 +08001453 adev->family = AMDGPU_FAMILY_SI;
Ken Wang33f34802016-01-21 17:29:41 +08001454 r = si_set_ip_blocks(adev);
1455 if (r)
1456 return r;
1457 break;
1458#endif
Alex Deuchera2e73f52015-04-20 17:09:27 -04001459#ifdef CONFIG_DRM_AMDGPU_CIK
1460 case CHIP_BONAIRE:
1461 case CHIP_HAWAII:
1462 case CHIP_KAVERI:
1463 case CHIP_KABINI:
1464 case CHIP_MULLINS:
1465 if ((adev->asic_type == CHIP_BONAIRE) || (adev->asic_type == CHIP_HAWAII))
1466 adev->family = AMDGPU_FAMILY_CI;
1467 else
1468 adev->family = AMDGPU_FAMILY_KV;
1469
1470 r = cik_set_ip_blocks(adev);
1471 if (r)
1472 return r;
1473 break;
1474#endif
Alex Deuchere48a3cd2017-09-01 16:22:35 -04001475 case CHIP_VEGA10:
1476 case CHIP_VEGA12:
1477 case CHIP_RAVEN:
Chunming Zhou2ca8a5d2016-12-07 17:31:19 +08001478 if (adev->asic_type == CHIP_RAVEN)
1479 adev->family = AMDGPU_FAMILY_RV;
1480 else
1481 adev->family = AMDGPU_FAMILY_AI;
Ken Wang460826e2017-03-06 14:53:16 -05001482
1483 r = soc15_set_ip_blocks(adev);
1484 if (r)
1485 return r;
1486 break;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001487 default:
1488 /* FIXME: not supported yet */
1489 return -EINVAL;
1490 }
1491
Alex Deuchere2a75f82017-04-27 16:58:01 -04001492 r = amdgpu_device_parse_gpu_info_fw(adev);
1493 if (r)
1494 return r;
1495
pding18847342017-11-06 10:21:26 +08001496 amdgpu_amdkfd_device_probe(adev);
1497
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001498 if (amdgpu_sriov_vf(adev)) {
1499 r = amdgpu_virt_request_full_gpu(adev, true);
1500 if (r)
pding5ffa61c2017-10-30 14:07:24 +08001501 return -EAGAIN;
Xiangliang Yu3149d9d2017-01-12 15:14:36 +08001502 }
1503
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001504 for (i = 0; i < adev->num_ip_blocks; i++) {
1505 if ((amdgpu_ip_block_mask & (1 << i)) == 0) {
Huang Ruied8cf002017-05-03 09:40:17 +08001506 DRM_ERROR("disabled ip block: %d <%s>\n",
1507 i, adev->ip_blocks[i].version->funcs->name);
Alex Deuchera1255102016-10-13 17:41:13 -04001508 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001509 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001510 if (adev->ip_blocks[i].version->funcs->early_init) {
1511 r = adev->ip_blocks[i].version->funcs->early_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001512 if (r == -ENOENT) {
Alex Deuchera1255102016-10-13 17:41:13 -04001513 adev->ip_blocks[i].status.valid = false;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001514 } else if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001515 DRM_ERROR("early_init of IP block <%s> failed %d\n",
1516 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001517 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001518 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001519 adev->ip_blocks[i].status.valid = true;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001520 }
Alex Deucher974e6b62015-07-10 13:59:44 -04001521 } else {
Alex Deuchera1255102016-10-13 17:41:13 -04001522 adev->ip_blocks[i].status.valid = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001523 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001524 }
1525 }
1526
Nicolai Hähnle395d1fb2016-06-02 12:32:07 +02001527 adev->cg_flags &= amdgpu_cg_mask;
1528 adev->pg_flags &= amdgpu_pg_mask;
1529
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001530 return 0;
1531}
1532
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001533/**
1534 * amdgpu_device_ip_init - run init for hardware IPs
1535 *
1536 * @adev: amdgpu_device pointer
1537 *
1538 * Main initialization pass for hardware IPs. The list of all the hardware
1539 * IPs that make up the asic is walked and the sw_init and hw_init callbacks
1540 * are run. sw_init initializes the software state associated with each IP
1541 * and hw_init initializes the hardware associated with each IP.
1542 * Returns 0 on success, negative error code on failure.
1543 */
Alex Deucher06ec9072017-12-14 15:02:39 -05001544static int amdgpu_device_ip_init(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001545{
1546 int i, r;
1547
1548 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001549 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001550 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001551 r = adev->ip_blocks[i].version->funcs->sw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001552 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001553 DRM_ERROR("sw_init of IP block <%s> failed %d\n",
1554 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001555 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001556 }
Alex Deuchera1255102016-10-13 17:41:13 -04001557 adev->ip_blocks[i].status.sw = true;
Shaoyun Liubfca0282018-02-01 17:37:50 -05001558
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001559 /* need to do gmc hw init early so we can allocate gpu mem */
Alex Deuchera1255102016-10-13 17:41:13 -04001560 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
Alex Deucher06ec9072017-12-14 15:02:39 -05001561 r = amdgpu_device_vram_scratch_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001562 if (r) {
1563 DRM_ERROR("amdgpu_vram_scratch_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001564 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001565 }
Alex Deuchera1255102016-10-13 17:41:13 -04001566 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001567 if (r) {
1568 DRM_ERROR("hw_init %d failed %d\n", i, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001569 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001570 }
Alex Deucher06ec9072017-12-14 15:02:39 -05001571 r = amdgpu_device_wb_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001572 if (r) {
Alex Deucher06ec9072017-12-14 15:02:39 -05001573 DRM_ERROR("amdgpu_device_wb_init failed %d\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001574 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001575 }
Alex Deuchera1255102016-10-13 17:41:13 -04001576 adev->ip_blocks[i].status.hw = true;
Monk Liu24936642017-01-09 15:54:32 +08001577
1578 /* right after GMC hw init, we create CSA */
1579 if (amdgpu_sriov_vf(adev)) {
1580 r = amdgpu_allocate_static_csa(adev);
1581 if (r) {
1582 DRM_ERROR("allocate CSA failed %d\n", r);
1583 return r;
1584 }
1585 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001586 }
1587 }
1588
1589 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001590 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001591 continue;
Shaoyun Liubfca0282018-02-01 17:37:50 -05001592 if (adev->ip_blocks[i].status.hw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001593 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001594 r = adev->ip_blocks[i].version->funcs->hw_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001595 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001596 DRM_ERROR("hw_init of IP block <%s> failed %d\n",
1597 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001598 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001599 }
Alex Deuchera1255102016-10-13 17:41:13 -04001600 adev->ip_blocks[i].status.hw = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001601 }
1602
pding18847342017-11-06 10:21:26 +08001603 amdgpu_amdkfd_device_init(adev);
pdingc6332b92017-11-06 11:21:55 +08001604
1605 if (amdgpu_sriov_vf(adev))
1606 amdgpu_virt_release_full_gpu(adev, true);
1607
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001608 return 0;
1609}
1610
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001611/**
1612 * amdgpu_device_fill_reset_magic - writes reset magic to gart pointer
1613 *
1614 * @adev: amdgpu_device pointer
1615 *
1616 * Writes a reset magic value to the gart pointer in VRAM. The driver calls
1617 * this function before a GPU reset. If the value is retained after a
1618 * GPU reset, VRAM has not been lost. Some GPU resets may destry VRAM contents.
1619 */
Alex Deucher06ec9072017-12-14 15:02:39 -05001620static void amdgpu_device_fill_reset_magic(struct amdgpu_device *adev)
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08001621{
1622 memcpy(adev->reset_magic, adev->gart.ptr, AMDGPU_RESET_MAGIC_NUM);
1623}
1624
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001625/**
1626 * amdgpu_device_check_vram_lost - check if vram is valid
1627 *
1628 * @adev: amdgpu_device pointer
1629 *
1630 * Checks the reset magic value written to the gart pointer in VRAM.
1631 * The driver calls this after a GPU reset to see if the contents of
1632 * VRAM is lost or now.
1633 * returns true if vram is lost, false if not.
1634 */
Alex Deucher06ec9072017-12-14 15:02:39 -05001635static bool amdgpu_device_check_vram_lost(struct amdgpu_device *adev)
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08001636{
1637 return !!memcmp(adev->gart.ptr, adev->reset_magic,
1638 AMDGPU_RESET_MAGIC_NUM);
1639}
1640
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001641/**
1642 * amdgpu_device_ip_late_set_cg_state - late init for clockgating
1643 *
1644 * @adev: amdgpu_device pointer
1645 *
1646 * Late initialization pass enabling clockgating for hardware IPs.
1647 * The list of all the hardware IPs that make up the asic is walked and the
1648 * set_clockgating_state callbacks are run. This stage is run late
1649 * in the init process.
1650 * Returns 0 on success, negative error code on failure.
1651 */
Alex Deucher06ec9072017-12-14 15:02:39 -05001652static int amdgpu_device_ip_late_set_cg_state(struct amdgpu_device *adev)
Shirish S2dc80b02017-05-25 10:05:25 +05301653{
1654 int i = 0, r;
1655
Shaoyun Liu4a2ba392018-02-05 16:41:33 -05001656 if (amdgpu_emu_mode == 1)
1657 return 0;
1658
Shirish S2dc80b02017-05-25 10:05:25 +05301659 for (i = 0; i < adev->num_ip_blocks; i++) {
1660 if (!adev->ip_blocks[i].status.valid)
1661 continue;
1662 /* skip CG for VCE/UVD, it's handled specially */
1663 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
Rex Zhu57716322018-03-12 19:50:38 +08001664 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
1665 adev->ip_blocks[i].version->funcs->set_clockgating_state) {
Shirish S2dc80b02017-05-25 10:05:25 +05301666 /* enable clockgating to save power */
1667 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1668 AMD_CG_STATE_GATE);
1669 if (r) {
1670 DRM_ERROR("set_clockgating_state(gate) of IP block <%s> failed %d\n",
1671 adev->ip_blocks[i].version->funcs->name, r);
1672 return r;
1673 }
1674 }
1675 }
1676 return 0;
1677}
1678
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001679/**
1680 * amdgpu_device_ip_late_init - run late init for hardware IPs
1681 *
1682 * @adev: amdgpu_device pointer
1683 *
1684 * Late initialization pass for hardware IPs. The list of all the hardware
1685 * IPs that make up the asic is walked and the late_init callbacks are run.
1686 * late_init covers any special initialization that an IP requires
1687 * after all of the have been initialized or something that needs to happen
1688 * late in the init process.
1689 * Returns 0 on success, negative error code on failure.
1690 */
Alex Deucher06ec9072017-12-14 15:02:39 -05001691static int amdgpu_device_ip_late_init(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001692{
1693 int i = 0, r;
1694
1695 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001696 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001697 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001698 if (adev->ip_blocks[i].version->funcs->late_init) {
1699 r = adev->ip_blocks[i].version->funcs->late_init((void *)adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001700 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001701 DRM_ERROR("late_init of IP block <%s> failed %d\n",
1702 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001703 return r;
Alex Deucher2c1a2782015-12-07 17:02:53 -05001704 }
Alex Deuchera1255102016-10-13 17:41:13 -04001705 adev->ip_blocks[i].status.late_initialized = true;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001706 }
1707 }
1708
Shirish S2dc80b02017-05-25 10:05:25 +05301709 mod_delayed_work(system_wq, &adev->late_init_work,
1710 msecs_to_jiffies(AMDGPU_RESUME_MS));
1711
Alex Deucher06ec9072017-12-14 15:02:39 -05001712 amdgpu_device_fill_reset_magic(adev);
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08001713
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001714 return 0;
1715}
1716
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001717/**
1718 * amdgpu_device_ip_fini - run fini for hardware IPs
1719 *
1720 * @adev: amdgpu_device pointer
1721 *
1722 * Main teardown pass for hardware IPs. The list of all the hardware
1723 * IPs that make up the asic is walked and the hw_fini and sw_fini callbacks
1724 * are run. hw_fini tears down the hardware associated with each IP
1725 * and sw_fini tears down any software state associated with each IP.
1726 * Returns 0 on success, negative error code on failure.
1727 */
Alex Deucher06ec9072017-12-14 15:02:39 -05001728static int amdgpu_device_ip_fini(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001729{
1730 int i, r;
1731
pding18847342017-11-06 10:21:26 +08001732 amdgpu_amdkfd_device_fini(adev);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001733 /* need to disable SMC first */
1734 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04001735 if (!adev->ip_blocks[i].status.hw)
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001736 continue;
Rex Zhu57716322018-03-12 19:50:38 +08001737 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC &&
1738 adev->ip_blocks[i].version->funcs->set_clockgating_state) {
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001739 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
Alex Deuchera1255102016-10-13 17:41:13 -04001740 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1741 AMD_CG_STATE_UNGATE);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001742 if (r) {
1743 DRM_ERROR("set_clockgating_state(ungate) 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 return r;
1746 }
Alex Deuchera1255102016-10-13 17:41:13 -04001747 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001748 /* XXX handle errors */
1749 if (r) {
1750 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
Alex Deuchera1255102016-10-13 17:41:13 -04001751 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001752 }
Alex Deuchera1255102016-10-13 17:41:13 -04001753 adev->ip_blocks[i].status.hw = false;
Alex Deucher3e96dbf2016-10-13 11:22:17 -04001754 break;
1755 }
1756 }
1757
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001758 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001759 if (!adev->ip_blocks[i].status.hw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001760 continue;
Rex Zhu8201a672016-11-24 21:44:44 +08001761
1762 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_UVD &&
Rex Zhu81ce8be2018-03-20 16:28:56 +08001763 adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_VCE &&
1764 adev->ip_blocks[i].version->funcs->set_clockgating_state) {
Rex Zhu8201a672016-11-24 21:44:44 +08001765 /* ungate blocks before hw fini so that we can shutdown the blocks safely */
1766 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1767 AMD_CG_STATE_UNGATE);
1768 if (r) {
1769 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1770 adev->ip_blocks[i].version->funcs->name, r);
1771 return r;
1772 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001773 }
Rex Zhu8201a672016-11-24 21:44:44 +08001774
Alex Deuchera1255102016-10-13 17:41:13 -04001775 r = adev->ip_blocks[i].version->funcs->hw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001776 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001777 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001778 DRM_DEBUG("hw_fini of IP block <%s> failed %d\n",
1779 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001780 }
Rex Zhu8201a672016-11-24 21:44:44 +08001781
Alex Deuchera1255102016-10-13 17:41:13 -04001782 adev->ip_blocks[i].status.hw = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001783 }
1784
Alex Deucher9950cda2018-01-18 19:05:36 -05001785
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001786 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001787 if (!adev->ip_blocks[i].status.sw)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001788 continue;
Monk Liuc12aba32018-01-24 12:20:32 +08001789
1790 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) {
1791 amdgpu_free_static_csa(adev);
1792 amdgpu_device_wb_fini(adev);
1793 amdgpu_device_vram_scratch_fini(adev);
1794 }
1795
Alex Deuchera1255102016-10-13 17:41:13 -04001796 r = adev->ip_blocks[i].version->funcs->sw_fini((void *)adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001797 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001798 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001799 DRM_DEBUG("sw_fini of IP block <%s> failed %d\n",
1800 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001801 }
Alex Deuchera1255102016-10-13 17:41:13 -04001802 adev->ip_blocks[i].status.sw = false;
1803 adev->ip_blocks[i].status.valid = false;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001804 }
1805
Monk Liua6dcfd92016-05-19 14:36:34 +08001806 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001807 if (!adev->ip_blocks[i].status.late_initialized)
Grazvydas Ignotas8a2eef12016-10-03 00:06:44 +03001808 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04001809 if (adev->ip_blocks[i].version->funcs->late_fini)
1810 adev->ip_blocks[i].version->funcs->late_fini((void *)adev);
1811 adev->ip_blocks[i].status.late_initialized = false;
Monk Liua6dcfd92016-05-19 14:36:34 +08001812 }
1813
Monk Liu030308f2017-09-15 15:34:52 +08001814 if (amdgpu_sriov_vf(adev))
Monk Liu24136132017-11-14 16:56:55 +08001815 if (amdgpu_virt_release_full_gpu(adev, false))
1816 DRM_ERROR("failed to release exclusive mode on fini\n");
Monk Liu24936642017-01-09 15:54:32 +08001817
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001818 return 0;
1819}
1820
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001821/**
1822 * amdgpu_device_ip_late_init_func_handler - work handler for clockgating
1823 *
1824 * @work: work_struct
1825 *
1826 * Work handler for amdgpu_device_ip_late_set_cg_state. We put the
1827 * clockgating setup into a worker thread to speed up driver init and
1828 * resume from suspend.
1829 */
Alex Deucher06ec9072017-12-14 15:02:39 -05001830static void amdgpu_device_ip_late_init_func_handler(struct work_struct *work)
Shirish S2dc80b02017-05-25 10:05:25 +05301831{
1832 struct amdgpu_device *adev =
1833 container_of(work, struct amdgpu_device, late_init_work.work);
Alex Deucher06ec9072017-12-14 15:02:39 -05001834 amdgpu_device_ip_late_set_cg_state(adev);
Shirish S2dc80b02017-05-25 10:05:25 +05301835}
1836
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001837/**
1838 * amdgpu_device_ip_suspend - run suspend for hardware IPs
1839 *
1840 * @adev: amdgpu_device pointer
1841 *
1842 * Main suspend function for hardware IPs. The list of all the hardware
1843 * IPs that make up the asic is walked, clockgating is disabled and the
1844 * suspend callbacks are run. suspend puts the hardware and software state
1845 * in each IP into a state suitable for suspend.
1846 * Returns 0 on success, negative error code on failure.
1847 */
Alex Deuchercdd61df2017-12-14 16:47:40 -05001848int amdgpu_device_ip_suspend(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001849{
1850 int i, r;
1851
Xiangliang Yue941ea92017-01-18 12:47:55 +08001852 if (amdgpu_sriov_vf(adev))
1853 amdgpu_virt_request_full_gpu(adev, false);
1854
Flora Cuic5a93a22016-02-26 10:45:25 +08001855 /* ungate SMC block first */
Alex Deucher2990a1f2017-12-15 16:18:00 -05001856 r = amdgpu_device_ip_set_clockgating_state(adev, AMD_IP_BLOCK_TYPE_SMC,
1857 AMD_CG_STATE_UNGATE);
Flora Cuic5a93a22016-02-26 10:45:25 +08001858 if (r) {
Alex Deucher2990a1f2017-12-15 16:18:00 -05001859 DRM_ERROR("set_clockgating_state(ungate) SMC failed %d\n", r);
Flora Cuic5a93a22016-02-26 10:45:25 +08001860 }
1861
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001862 for (i = adev->num_ip_blocks - 1; i >= 0; i--) {
Alex Deuchera1255102016-10-13 17:41:13 -04001863 if (!adev->ip_blocks[i].status.valid)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001864 continue;
1865 /* ungate blocks so that suspend can properly shut them down */
Rex Zhu5b2a3d22018-03-14 15:38:48 +08001866 if (adev->ip_blocks[i].version->type != AMD_IP_BLOCK_TYPE_SMC &&
Rex Zhu57716322018-03-12 19:50:38 +08001867 adev->ip_blocks[i].version->funcs->set_clockgating_state) {
Alex Deuchera1255102016-10-13 17:41:13 -04001868 r = adev->ip_blocks[i].version->funcs->set_clockgating_state((void *)adev,
1869 AMD_CG_STATE_UNGATE);
Flora Cuic5a93a22016-02-26 10:45:25 +08001870 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001871 DRM_ERROR("set_clockgating_state(ungate) of IP block <%s> failed %d\n",
1872 adev->ip_blocks[i].version->funcs->name, r);
Flora Cuic5a93a22016-02-26 10:45:25 +08001873 }
Alex Deucher2c1a2782015-12-07 17:02:53 -05001874 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001875 /* XXX handle errors */
Alex Deuchera1255102016-10-13 17:41:13 -04001876 r = adev->ip_blocks[i].version->funcs->suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001877 /* XXX handle errors */
Alex Deucher2c1a2782015-12-07 17:02:53 -05001878 if (r) {
Alex Deuchera1255102016-10-13 17:41:13 -04001879 DRM_ERROR("suspend of IP block <%s> failed %d\n",
1880 adev->ip_blocks[i].version->funcs->name, r);
Alex Deucher2c1a2782015-12-07 17:02:53 -05001881 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001882 }
1883
Xiangliang Yue941ea92017-01-18 12:47:55 +08001884 if (amdgpu_sriov_vf(adev))
1885 amdgpu_virt_release_full_gpu(adev, false);
1886
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001887 return 0;
1888}
1889
Alex Deucher06ec9072017-12-14 15:02:39 -05001890static int amdgpu_device_ip_reinit_early_sriov(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08001891{
1892 int i, r;
1893
Monk Liu2cb681b2017-04-26 12:00:49 +08001894 static enum amd_ip_block_type ip_order[] = {
1895 AMD_IP_BLOCK_TYPE_GMC,
1896 AMD_IP_BLOCK_TYPE_COMMON,
Monk Liu2cb681b2017-04-26 12:00:49 +08001897 AMD_IP_BLOCK_TYPE_IH,
1898 };
Monk Liua90ad3c2017-01-23 14:22:08 +08001899
Monk Liu2cb681b2017-04-26 12:00:49 +08001900 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1901 int j;
1902 struct amdgpu_ip_block *block;
Monk Liua90ad3c2017-01-23 14:22:08 +08001903
Monk Liu2cb681b2017-04-26 12:00:49 +08001904 for (j = 0; j < adev->num_ip_blocks; j++) {
1905 block = &adev->ip_blocks[j];
1906
1907 if (block->version->type != ip_order[i] ||
1908 !block->status.valid)
1909 continue;
1910
1911 r = block->version->funcs->hw_init(adev);
1912 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
Monk Liuc41d1cf2017-12-25 11:59:27 +08001913 if (r)
1914 return r;
Monk Liua90ad3c2017-01-23 14:22:08 +08001915 }
1916 }
1917
1918 return 0;
1919}
1920
Alex Deucher06ec9072017-12-14 15:02:39 -05001921static int amdgpu_device_ip_reinit_late_sriov(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08001922{
1923 int i, r;
1924
Monk Liu2cb681b2017-04-26 12:00:49 +08001925 static enum amd_ip_block_type ip_order[] = {
1926 AMD_IP_BLOCK_TYPE_SMC,
Monk Liuef4c1662017-09-22 16:23:34 +08001927 AMD_IP_BLOCK_TYPE_PSP,
Monk Liu2cb681b2017-04-26 12:00:49 +08001928 AMD_IP_BLOCK_TYPE_DCE,
1929 AMD_IP_BLOCK_TYPE_GFX,
1930 AMD_IP_BLOCK_TYPE_SDMA,
Frank Min257deb82017-06-15 20:07:36 +08001931 AMD_IP_BLOCK_TYPE_UVD,
1932 AMD_IP_BLOCK_TYPE_VCE
Monk Liu2cb681b2017-04-26 12:00:49 +08001933 };
Monk Liua90ad3c2017-01-23 14:22:08 +08001934
Monk Liu2cb681b2017-04-26 12:00:49 +08001935 for (i = 0; i < ARRAY_SIZE(ip_order); i++) {
1936 int j;
1937 struct amdgpu_ip_block *block;
Monk Liua90ad3c2017-01-23 14:22:08 +08001938
Monk Liu2cb681b2017-04-26 12:00:49 +08001939 for (j = 0; j < adev->num_ip_blocks; j++) {
1940 block = &adev->ip_blocks[j];
1941
1942 if (block->version->type != ip_order[i] ||
1943 !block->status.valid)
1944 continue;
1945
1946 r = block->version->funcs->hw_init(adev);
1947 DRM_INFO("RE-INIT: %s %s\n", block->version->funcs->name, r?"failed":"successed");
Monk Liuc41d1cf2017-12-25 11:59:27 +08001948 if (r)
1949 return r;
Monk Liua90ad3c2017-01-23 14:22:08 +08001950 }
1951 }
1952
1953 return 0;
1954}
1955
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001956/**
1957 * amdgpu_device_ip_resume_phase1 - run resume for hardware IPs
1958 *
1959 * @adev: amdgpu_device pointer
1960 *
1961 * First resume function for hardware IPs. The list of all the hardware
1962 * IPs that make up the asic is walked and the resume callbacks are run for
1963 * COMMON, GMC, and IH. resume puts the hardware into a functional state
1964 * after a suspend and updates the software state as necessary. This
1965 * function is also used for restoring the GPU after a GPU reset.
1966 * Returns 0 on success, negative error code on failure.
1967 */
Alex Deucher06ec9072017-12-14 15:02:39 -05001968static int amdgpu_device_ip_resume_phase1(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001969{
1970 int i, r;
1971
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001972 for (i = 0; i < adev->num_ip_blocks; i++) {
1973 if (!adev->ip_blocks[i].status.valid)
1974 continue;
Chunming Zhoufcf06492017-05-05 10:33:33 +08001975 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001976 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
1977 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH) {
Chunming Zhoufcf06492017-05-05 10:33:33 +08001978 r = adev->ip_blocks[i].version->funcs->resume(adev);
1979 if (r) {
1980 DRM_ERROR("resume of IP block <%s> failed %d\n",
1981 adev->ip_blocks[i].version->funcs->name, r);
1982 return r;
1983 }
1984 }
1985 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04001986
Chunming Zhoufcf06492017-05-05 10:33:33 +08001987 return 0;
1988}
1989
Alex Deuchere3ecdff2018-03-15 17:39:45 -05001990/**
1991 * amdgpu_device_ip_resume_phase2 - run resume for hardware IPs
1992 *
1993 * @adev: amdgpu_device pointer
1994 *
1995 * First resume function for hardware IPs. The list of all the hardware
1996 * IPs that make up the asic is walked and the resume callbacks are run for
1997 * all blocks except COMMON, GMC, and IH. resume puts the hardware into a
1998 * functional state after a suspend and updates the software state as
1999 * necessary. This function is also used for restoring the GPU after a GPU
2000 * reset.
2001 * Returns 0 on success, negative error code on failure.
2002 */
Alex Deucher06ec9072017-12-14 15:02:39 -05002003static int amdgpu_device_ip_resume_phase2(struct amdgpu_device *adev)
Chunming Zhoufcf06492017-05-05 10:33:33 +08002004{
2005 int i, r;
2006
2007 for (i = 0; i < adev->num_ip_blocks; i++) {
2008 if (!adev->ip_blocks[i].status.valid)
2009 continue;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002010 if (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_COMMON ||
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002011 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC ||
2012 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_IH)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002013 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002014 r = adev->ip_blocks[i].version->funcs->resume(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002015 if (r) {
2016 DRM_ERROR("resume of IP block <%s> failed %d\n",
2017 adev->ip_blocks[i].version->funcs->name, r);
2018 return r;
2019 }
2020 }
2021
2022 return 0;
2023}
2024
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002025/**
2026 * amdgpu_device_ip_resume - run resume for hardware IPs
2027 *
2028 * @adev: amdgpu_device pointer
2029 *
2030 * Main resume function for hardware IPs. The hardware IPs
2031 * are split into two resume functions because they are
2032 * are also used in in recovering from a GPU reset and some additional
2033 * steps need to be take between them. In this case (S3/S4) they are
2034 * run sequentially.
2035 * Returns 0 on success, negative error code on failure.
2036 */
Alex Deucher06ec9072017-12-14 15:02:39 -05002037static int amdgpu_device_ip_resume(struct amdgpu_device *adev)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002038{
Chunming Zhoufcf06492017-05-05 10:33:33 +08002039 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002040
Alex Deucher06ec9072017-12-14 15:02:39 -05002041 r = amdgpu_device_ip_resume_phase1(adev);
Chunming Zhoufcf06492017-05-05 10:33:33 +08002042 if (r)
2043 return r;
Alex Deucher06ec9072017-12-14 15:02:39 -05002044 r = amdgpu_device_ip_resume_phase2(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002045
Chunming Zhoufcf06492017-05-05 10:33:33 +08002046 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002047}
2048
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002049/**
2050 * amdgpu_device_detect_sriov_bios - determine if the board supports SR-IOV
2051 *
2052 * @adev: amdgpu_device pointer
2053 *
2054 * Query the VBIOS data tables to determine if the board supports SR-IOV.
2055 */
Monk Liu4e99a442016-03-31 13:26:59 +08002056static void amdgpu_device_detect_sriov_bios(struct amdgpu_device *adev)
Andres Rodriguez048765a2016-06-11 02:51:32 -04002057{
Monk Liu6867e1b2017-10-16 19:50:44 +08002058 if (amdgpu_sriov_vf(adev)) {
2059 if (adev->is_atom_fw) {
2060 if (amdgpu_atomfirmware_gpu_supports_virtualization(adev))
2061 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
2062 } else {
2063 if (amdgpu_atombios_has_gpu_virtualization_table(adev))
2064 adev->virt.caps |= AMDGPU_SRIOV_CAPS_SRIOV_VBIOS;
2065 }
2066
2067 if (!(adev->virt.caps & AMDGPU_SRIOV_CAPS_SRIOV_VBIOS))
2068 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_NO_VBIOS, 0, 0);
Alex Deuchera5bde2f2016-09-23 16:23:41 -04002069 }
Andres Rodriguez048765a2016-06-11 02:51:32 -04002070}
2071
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002072/**
2073 * amdgpu_device_asic_has_dc_support - determine if DC supports the asic
2074 *
2075 * @asic_type: AMD asic type
2076 *
2077 * Check if there is DC (new modesetting infrastructre) support for an asic.
2078 * returns true if DC has support, false if not.
2079 */
Harry Wentland45622362017-09-12 15:58:20 -04002080bool amdgpu_device_asic_has_dc_support(enum amd_asic_type asic_type)
2081{
2082 switch (asic_type) {
2083#if defined(CONFIG_DRM_AMD_DC)
2084 case CHIP_BONAIRE:
2085 case CHIP_HAWAII:
Alex Deucher0d6fbcc2017-08-10 14:39:48 -04002086 case CHIP_KAVERI:
Alex Deucher367e6682018-01-25 16:53:25 -05002087 case CHIP_KABINI:
2088 case CHIP_MULLINS:
Harry Wentland45622362017-09-12 15:58:20 -04002089 case CHIP_CARRIZO:
2090 case CHIP_STONEY:
2091 case CHIP_POLARIS11:
2092 case CHIP_POLARIS10:
Alex Deucher2c8ad2d2017-06-15 16:20:24 -04002093 case CHIP_POLARIS12:
Harry Wentland45622362017-09-12 15:58:20 -04002094 case CHIP_TONGA:
2095 case CHIP_FIJI:
2096#if defined(CONFIG_DRM_AMD_DC_PRE_VEGA)
2097 return amdgpu_dc != 0;
Harry Wentland45622362017-09-12 15:58:20 -04002098#endif
Harry Wentland42f8ffa2017-09-15 14:07:30 -04002099 case CHIP_VEGA10:
Alex Deucherdca7b402017-09-02 02:05:29 -04002100 case CHIP_VEGA12:
Harry Wentland42f8ffa2017-09-15 14:07:30 -04002101#if defined(CONFIG_DRM_AMD_DC_DCN1_0)
Hawking Zhangfd187852017-03-06 14:01:11 +08002102 case CHIP_RAVEN:
Harry Wentland42f8ffa2017-09-15 14:07:30 -04002103#endif
Hawking Zhangfd187852017-03-06 14:01:11 +08002104 return amdgpu_dc != 0;
2105#endif
Harry Wentland45622362017-09-12 15:58:20 -04002106 default:
2107 return false;
2108 }
2109}
2110
2111/**
2112 * amdgpu_device_has_dc_support - check if dc is supported
2113 *
2114 * @adev: amdgpu_device_pointer
2115 *
2116 * Returns true for supported, false for not supported
2117 */
2118bool amdgpu_device_has_dc_support(struct amdgpu_device *adev)
2119{
Xiangliang Yu2555039d2017-01-10 17:34:52 +08002120 if (amdgpu_sriov_vf(adev))
2121 return false;
2122
Harry Wentland45622362017-09-12 15:58:20 -04002123 return amdgpu_device_asic_has_dc_support(adev->asic_type);
2124}
2125
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002126/**
2127 * amdgpu_device_init - initialize the driver
2128 *
2129 * @adev: amdgpu_device pointer
2130 * @pdev: drm dev pointer
2131 * @pdev: pci dev pointer
2132 * @flags: driver flags
2133 *
2134 * Initializes the driver info and hw (all asics).
2135 * Returns 0 for success or an error on failure.
2136 * Called at driver startup.
2137 */
2138int amdgpu_device_init(struct amdgpu_device *adev,
2139 struct drm_device *ddev,
2140 struct pci_dev *pdev,
2141 uint32_t flags)
2142{
2143 int r, i;
2144 bool runtime = false;
Marek Olšák95844d22016-08-17 23:49:27 +02002145 u32 max_MBps;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002146
2147 adev->shutdown = false;
2148 adev->dev = &pdev->dev;
2149 adev->ddev = ddev;
2150 adev->pdev = pdev;
2151 adev->flags = flags;
Jammy Zhou2f7d10b2015-07-22 11:29:01 +08002152 adev->asic_type = flags & AMD_ASIC_MASK;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002153 adev->usec_timeout = AMDGPU_MAX_USEC_TIMEOUT;
Shaoyun Liu593aa2d2018-02-07 14:43:13 -05002154 if (amdgpu_emu_mode == 1)
2155 adev->usec_timeout *= 2;
Christian König770d13b2018-01-12 14:52:22 +01002156 adev->gmc.gart_size = 512 * 1024 * 1024;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002157 adev->accel_working = false;
2158 adev->num_rings = 0;
2159 adev->mman.buffer_funcs = NULL;
2160 adev->mman.buffer_funcs_ring = NULL;
2161 adev->vm_manager.vm_pte_funcs = NULL;
Christian König2d55e452016-02-08 17:37:38 +01002162 adev->vm_manager.vm_pte_num_rings = 0;
Christian König132f34e2018-01-12 15:26:08 +01002163 adev->gmc.gmc_funcs = NULL;
Chris Wilsonf54d1862016-10-25 13:00:45 +01002164 adev->fence_context = dma_fence_context_alloc(AMDGPU_MAX_RINGS);
Andres Rodriguezb8866c22017-04-28 20:05:51 -04002165 bitmap_zero(adev->gfx.pipe_reserve_bitmap, AMDGPU_MAX_COMPUTE_QUEUES);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002166
2167 adev->smc_rreg = &amdgpu_invalid_rreg;
2168 adev->smc_wreg = &amdgpu_invalid_wreg;
2169 adev->pcie_rreg = &amdgpu_invalid_rreg;
2170 adev->pcie_wreg = &amdgpu_invalid_wreg;
Huang Rui36b9a952016-08-31 13:23:25 +08002171 adev->pciep_rreg = &amdgpu_invalid_rreg;
2172 adev->pciep_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002173 adev->uvd_ctx_rreg = &amdgpu_invalid_rreg;
2174 adev->uvd_ctx_wreg = &amdgpu_invalid_wreg;
2175 adev->didt_rreg = &amdgpu_invalid_rreg;
2176 adev->didt_wreg = &amdgpu_invalid_wreg;
Rex Zhuccdbb202016-06-08 12:47:41 +08002177 adev->gc_cac_rreg = &amdgpu_invalid_rreg;
2178 adev->gc_cac_wreg = &amdgpu_invalid_wreg;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002179 adev->audio_endpt_rreg = &amdgpu_block_invalid_rreg;
2180 adev->audio_endpt_wreg = &amdgpu_block_invalid_wreg;
2181
Alex Deucher3e39ab92015-06-05 15:04:33 -04002182 DRM_INFO("initializing kernel modesetting (%s 0x%04X:0x%04X 0x%04X:0x%04X 0x%02X).\n",
2183 amdgpu_asic_name[adev->asic_type], pdev->vendor, pdev->device,
2184 pdev->subsystem_vendor, pdev->subsystem_device, pdev->revision);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002185
2186 /* mutex initialization are all done here so we
2187 * can recall function without having locking issues */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002188 atomic_set(&adev->irq.ih.lock, 0);
Huang Rui0e5ca0d2017-03-03 18:37:23 -05002189 mutex_init(&adev->firmware.mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002190 mutex_init(&adev->pm.mutex);
2191 mutex_init(&adev->gfx.gpu_clock_mutex);
2192 mutex_init(&adev->srbm_mutex);
Andres Rodriguezb8866c22017-04-28 20:05:51 -04002193 mutex_init(&adev->gfx.pipe_reserve_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002194 mutex_init(&adev->grbm_idx_mutex);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002195 mutex_init(&adev->mn_lock);
Alex Deuchere23b74a2017-09-28 09:47:32 -04002196 mutex_init(&adev->virt.vf_errors.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002197 hash_init(adev->mn_hash);
Monk Liu13a752e2017-10-17 15:11:12 +08002198 mutex_init(&adev->lock_reset);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002199
Alex Deucher06ec9072017-12-14 15:02:39 -05002200 amdgpu_device_check_arguments(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002201
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002202 spin_lock_init(&adev->mmio_idx_lock);
2203 spin_lock_init(&adev->smc_idx_lock);
2204 spin_lock_init(&adev->pcie_idx_lock);
2205 spin_lock_init(&adev->uvd_ctx_idx_lock);
2206 spin_lock_init(&adev->didt_idx_lock);
Rex Zhuccdbb202016-06-08 12:47:41 +08002207 spin_lock_init(&adev->gc_cac_idx_lock);
Evan Quan16abb5d2017-07-04 09:21:50 +08002208 spin_lock_init(&adev->se_cac_idx_lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002209 spin_lock_init(&adev->audio_endpt_idx_lock);
Marek Olšák95844d22016-08-17 23:49:27 +02002210 spin_lock_init(&adev->mm_stats.lock);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002211
Chunming Zhou0c4e7fa2016-08-17 11:41:30 +08002212 INIT_LIST_HEAD(&adev->shadow_list);
2213 mutex_init(&adev->shadow_list_lock);
2214
Andres Rodriguez795f2812017-03-06 16:27:55 -05002215 INIT_LIST_HEAD(&adev->ring_lru_list);
2216 spin_lock_init(&adev->ring_lru_list_lock);
2217
Alex Deucher06ec9072017-12-14 15:02:39 -05002218 INIT_DELAYED_WORK(&adev->late_init_work,
2219 amdgpu_device_ip_late_init_func_handler);
Shirish S2dc80b02017-05-25 10:05:25 +05302220
Alex Xie0fa49552017-06-08 14:58:05 -04002221 /* Registers mapping */
2222 /* TODO: block userspace mapping of io register */
Ken Wangda69c1612016-01-21 19:08:55 +08002223 if (adev->asic_type >= CHIP_BONAIRE) {
2224 adev->rmmio_base = pci_resource_start(adev->pdev, 5);
2225 adev->rmmio_size = pci_resource_len(adev->pdev, 5);
2226 } else {
2227 adev->rmmio_base = pci_resource_start(adev->pdev, 2);
2228 adev->rmmio_size = pci_resource_len(adev->pdev, 2);
2229 }
Chunming Zhou5c1354b2016-08-30 16:13:10 +08002230
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002231 adev->rmmio = ioremap(adev->rmmio_base, adev->rmmio_size);
2232 if (adev->rmmio == NULL) {
2233 return -ENOMEM;
2234 }
2235 DRM_INFO("register mmio base: 0x%08X\n", (uint32_t)adev->rmmio_base);
2236 DRM_INFO("register mmio size: %u\n", (unsigned)adev->rmmio_size);
2237
Christian König705e5192017-06-08 11:15:16 +02002238 /* doorbell bar mapping */
Alex Deucher06ec9072017-12-14 15:02:39 -05002239 amdgpu_device_doorbell_init(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002240
2241 /* io port mapping */
2242 for (i = 0; i < DEVICE_COUNT_RESOURCE; i++) {
2243 if (pci_resource_flags(adev->pdev, i) & IORESOURCE_IO) {
2244 adev->rio_mem_size = pci_resource_len(adev->pdev, i);
2245 adev->rio_mem = pci_iomap(adev->pdev, i, adev->rio_mem_size);
2246 break;
2247 }
2248 }
2249 if (adev->rio_mem == NULL)
Amber Linb64a18c2017-01-04 08:06:58 -05002250 DRM_INFO("PCI I/O BAR is not found.\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002251
Alex Deucher5494d862018-03-09 15:14:11 -05002252 amdgpu_device_get_pcie_info(adev);
2253
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002254 /* early init functions */
Alex Deucher06ec9072017-12-14 15:02:39 -05002255 r = amdgpu_device_ip_early_init(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002256 if (r)
2257 return r;
2258
2259 /* if we have > 1 VGA cards, then disable the amdgpu VGA resources */
2260 /* this will fail for cards that aren't VGA class devices, just
2261 * ignore it */
Alex Deucher06ec9072017-12-14 15:02:39 -05002262 vga_client_register(adev->pdev, adev, NULL, amdgpu_device_vga_set_decode);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002263
Alex Deuchere9bef452016-04-25 13:12:18 -04002264 if (amdgpu_device_is_px(ddev))
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002265 runtime = true;
Lukas Wunner84c8b222017-03-10 21:23:45 +01002266 if (!pci_is_thunderbolt_attached(adev->pdev))
2267 vga_switcheroo_register_client(adev->pdev,
2268 &amdgpu_switcheroo_ops, runtime);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002269 if (runtime)
2270 vga_switcheroo_init_domain_pm_ops(adev->dev, &adev->vga_pm_domain);
2271
Shaoyun Liu9475a942018-02-01 18:13:23 -05002272 if (amdgpu_emu_mode == 1) {
2273 /* post the asic on emulation mode */
2274 emu_soc_asic_init(adev);
Shaoyun Liubfca0282018-02-01 17:37:50 -05002275 goto fence_driver_init;
Shaoyun Liu9475a942018-02-01 18:13:23 -05002276 }
Shaoyun Liubfca0282018-02-01 17:37:50 -05002277
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002278 /* Read BIOS */
Alex Deucher83ba1262016-06-03 18:21:41 -04002279 if (!amdgpu_get_bios(adev)) {
2280 r = -EINVAL;
2281 goto failed;
2282 }
Nils Wallméniusf7e9e9f2016-12-14 21:52:45 +01002283
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002284 r = amdgpu_atombios_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002285 if (r) {
2286 dev_err(adev->dev, "amdgpu_atombios_init failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002287 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_INIT_FAIL, 0, 0);
Alex Deucher83ba1262016-06-03 18:21:41 -04002288 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002289 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002290
Monk Liu4e99a442016-03-31 13:26:59 +08002291 /* detect if we are with an SRIOV vbios */
2292 amdgpu_device_detect_sriov_bios(adev);
Andres Rodriguez048765a2016-06-11 02:51:32 -04002293
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002294 /* Post card if necessary */
Alex Deucher39c640c2017-12-15 16:22:11 -05002295 if (amdgpu_device_need_post(adev)) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002296 if (!adev->bios) {
Monk Liubec86372016-09-14 19:38:08 +08002297 dev_err(adev->dev, "no vBIOS found\n");
Alex Deucher83ba1262016-06-03 18:21:41 -04002298 r = -EINVAL;
2299 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002300 }
Monk Liubec86372016-09-14 19:38:08 +08002301 DRM_INFO("GPU posting now...\n");
Monk Liu4e99a442016-03-31 13:26:59 +08002302 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2303 if (r) {
2304 dev_err(adev->dev, "gpu post error!\n");
2305 goto failed;
2306 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002307 }
2308
Alex Deucher88b64e92017-07-10 10:43:10 -04002309 if (adev->is_atom_fw) {
2310 /* Initialize clocks */
2311 r = amdgpu_atomfirmware_get_clock_info(adev);
2312 if (r) {
2313 dev_err(adev->dev, "amdgpu_atomfirmware_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);
Alex Deucher88b64e92017-07-10 10:43:10 -04002315 goto failed;
2316 }
2317 } else {
Alex Deuchera5bde2f2016-09-23 16:23:41 -04002318 /* Initialize clocks */
2319 r = amdgpu_atombios_get_clock_info(adev);
2320 if (r) {
2321 dev_err(adev->dev, "amdgpu_atombios_get_clock_info failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002322 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_ATOMBIOS_GET_CLOCK_FAIL, 0, 0);
Gavin Wan89041942017-06-23 13:55:15 -04002323 goto failed;
Alex Deuchera5bde2f2016-09-23 16:23:41 -04002324 }
2325 /* init i2c buses */
Harry Wentland45622362017-09-12 15:58:20 -04002326 if (!amdgpu_device_has_dc_support(adev))
2327 amdgpu_atombios_i2c_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002328 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002329
Shaoyun Liubfca0282018-02-01 17:37:50 -05002330fence_driver_init:
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002331 /* Fence driver */
2332 r = amdgpu_fence_driver_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002333 if (r) {
2334 dev_err(adev->dev, "amdgpu_fence_driver_init failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002335 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_FENCE_INIT_FAIL, 0, 0);
Alex Deucher83ba1262016-06-03 18:21:41 -04002336 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002337 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002338
2339 /* init the mode config */
2340 drm_mode_config_init(adev->ddev);
2341
Alex Deucher06ec9072017-12-14 15:02:39 -05002342 r = amdgpu_device_ip_init(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002343 if (r) {
pding8840a382017-10-23 17:22:09 +08002344 /* failed in exclusive mode due to timeout */
2345 if (amdgpu_sriov_vf(adev) &&
2346 !amdgpu_sriov_runtime(adev) &&
2347 amdgpu_virt_mmio_blocked(adev) &&
2348 !amdgpu_virt_wait_reset(adev)) {
2349 dev_err(adev->dev, "VF exclusive mode timeout\n");
Pixel Ding1daee8b2017-11-08 11:03:14 +08002350 /* Don't send request since VF is inactive. */
2351 adev->virt.caps &= ~AMDGPU_SRIOV_CAPS_RUNTIME;
2352 adev->virt.ops = NULL;
pding8840a382017-10-23 17:22:09 +08002353 r = -EAGAIN;
2354 goto failed;
2355 }
Alex Deucher06ec9072017-12-14 15:02:39 -05002356 dev_err(adev->dev, "amdgpu_device_ip_init failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002357 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_INIT_FAIL, 0, 0);
Alex Deucher83ba1262016-06-03 18:21:41 -04002358 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002359 }
2360
2361 adev->accel_working = true;
2362
Alex Xiee59c0202017-06-01 09:42:59 -04002363 amdgpu_vm_check_compute_bug(adev);
2364
Marek Olšák95844d22016-08-17 23:49:27 +02002365 /* Initialize the buffer migration limit. */
2366 if (amdgpu_moverate >= 0)
2367 max_MBps = amdgpu_moverate;
2368 else
2369 max_MBps = 8; /* Allow 8 MB/s. */
2370 /* Get a log2 for easy divisions. */
2371 adev->mm_stats.log2_max_MBps = ilog2(max(1u, max_MBps));
2372
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002373 r = amdgpu_ib_pool_init(adev);
2374 if (r) {
2375 dev_err(adev->dev, "IB initialization failed (%d).\n", r);
Alex Deuchere23b74a2017-09-28 09:47:32 -04002376 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_IB_INIT_FAIL, 0, r);
Alex Deucher83ba1262016-06-03 18:21:41 -04002377 goto failed;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002378 }
2379
2380 r = amdgpu_ib_ring_tests(adev);
2381 if (r)
2382 DRM_ERROR("ib ring test failed (%d).\n", r);
2383
Horace Chen2dc8f812017-10-09 16:17:16 +08002384 if (amdgpu_sriov_vf(adev))
2385 amdgpu_virt_init_data_exchange(adev);
2386
Monk Liu9bc92b92017-02-08 17:38:13 +08002387 amdgpu_fbdev_init(adev);
2388
Rex Zhud2f52ac2017-09-22 17:47:27 +08002389 r = amdgpu_pm_sysfs_init(adev);
2390 if (r)
2391 DRM_ERROR("registering pm debugfs failed (%d).\n", r);
2392
Alex Deucher75758252017-12-14 15:23:14 -05002393 r = amdgpu_debugfs_gem_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002394 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002395 DRM_ERROR("registering gem debugfs failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002396
2397 r = amdgpu_debugfs_regs_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002398 if (r)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002399 DRM_ERROR("registering register debugfs failed (%d).\n", r);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002400
Huang Rui50ab2532016-06-12 15:51:09 +08002401 r = amdgpu_debugfs_firmware_init(adev);
Monk Liu3f14e622017-02-09 13:42:27 +08002402 if (r)
Huang Rui50ab2532016-06-12 15:51:09 +08002403 DRM_ERROR("registering firmware debugfs failed (%d).\n", r);
Huang Rui50ab2532016-06-12 15:51:09 +08002404
Christian König763efb62017-12-06 15:44:51 +01002405 r = amdgpu_debugfs_init(adev);
Kent Russelldb95e212017-08-22 12:31:43 -04002406 if (r)
Christian König763efb62017-12-06 15:44:51 +01002407 DRM_ERROR("Creating debugfs files failed (%d).\n", r);
Kent Russelldb95e212017-08-22 12:31:43 -04002408
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002409 if ((amdgpu_testing & 1)) {
2410 if (adev->accel_working)
2411 amdgpu_test_moves(adev);
2412 else
2413 DRM_INFO("amdgpu: acceleration disabled, skipping move tests\n");
2414 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002415 if (amdgpu_benchmarking) {
2416 if (adev->accel_working)
2417 amdgpu_benchmark(adev, amdgpu_benchmarking);
2418 else
2419 DRM_INFO("amdgpu: acceleration disabled, skipping benchmarks\n");
2420 }
2421
2422 /* enable clockgating, etc. after ib tests, etc. since some blocks require
2423 * explicit gating rather than handling it automatically.
2424 */
Alex Deucher06ec9072017-12-14 15:02:39 -05002425 r = amdgpu_device_ip_late_init(adev);
Alex Deucher2c1a2782015-12-07 17:02:53 -05002426 if (r) {
Alex Deucher06ec9072017-12-14 15:02:39 -05002427 dev_err(adev->dev, "amdgpu_device_ip_late_init failed\n");
Alex Deuchere23b74a2017-09-28 09:47:32 -04002428 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_AMDGPU_LATE_INIT_FAIL, 0, r);
Alex Deucher83ba1262016-06-03 18:21:41 -04002429 goto failed;
Alex Deucher2c1a2782015-12-07 17:02:53 -05002430 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002431
2432 return 0;
Alex Deucher83ba1262016-06-03 18:21:41 -04002433
2434failed:
Gavin Wan89041942017-06-23 13:55:15 -04002435 amdgpu_vf_error_trans_all(adev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002436 if (runtime)
2437 vga_switcheroo_fini_domain_pm_ops(adev->dev);
pding8840a382017-10-23 17:22:09 +08002438
Alex Deucher83ba1262016-06-03 18:21:41 -04002439 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002440}
2441
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002442/**
2443 * amdgpu_device_fini - tear down the driver
2444 *
2445 * @adev: amdgpu_device pointer
2446 *
2447 * Tear down the driver info (all asics).
2448 * Called at driver shutdown.
2449 */
2450void amdgpu_device_fini(struct amdgpu_device *adev)
2451{
2452 int r;
2453
2454 DRM_INFO("amdgpu: finishing device.\n");
2455 adev->shutdown = true;
Mikita Lipskie5b03032018-03-15 16:53:08 -04002456 /* disable all interrupts */
2457 amdgpu_irq_disable_all(adev);
Mikita Lipskiff97cba2018-03-14 13:41:29 -04002458 if (adev->mode_info.mode_config_initialized){
2459 if (!amdgpu_device_has_dc_support(adev))
2460 drm_crtc_force_disable_all(adev->ddev);
2461 else
2462 drm_atomic_helper_shutdown(adev->ddev);
2463 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002464 amdgpu_ib_pool_fini(adev);
2465 amdgpu_fence_driver_fini(adev);
Emily Deng58e955d2018-03-08 09:35:19 +08002466 amdgpu_pm_sysfs_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002467 amdgpu_fbdev_fini(adev);
Alex Deucher06ec9072017-12-14 15:02:39 -05002468 r = amdgpu_device_ip_fini(adev);
Huang Ruiab4fe3e2017-06-05 22:11:59 +08002469 if (adev->firmware.gpu_info_fw) {
2470 release_firmware(adev->firmware.gpu_info_fw);
2471 adev->firmware.gpu_info_fw = NULL;
2472 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002473 adev->accel_working = false;
Shirish S2dc80b02017-05-25 10:05:25 +05302474 cancel_delayed_work_sync(&adev->late_init_work);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002475 /* free i2c buses */
Harry Wentland45622362017-09-12 15:58:20 -04002476 if (!amdgpu_device_has_dc_support(adev))
2477 amdgpu_i2c_fini(adev);
Shaoyun Liubfca0282018-02-01 17:37:50 -05002478
2479 if (amdgpu_emu_mode != 1)
2480 amdgpu_atombios_fini(adev);
2481
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002482 kfree(adev->bios);
2483 adev->bios = NULL;
Lukas Wunner84c8b222017-03-10 21:23:45 +01002484 if (!pci_is_thunderbolt_attached(adev->pdev))
2485 vga_switcheroo_unregister_client(adev->pdev);
Alex Deucher83ba1262016-06-03 18:21:41 -04002486 if (adev->flags & AMD_IS_PX)
2487 vga_switcheroo_fini_domain_pm_ops(adev->dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002488 vga_client_register(adev->pdev, NULL, NULL, NULL);
2489 if (adev->rio_mem)
2490 pci_iounmap(adev->pdev, adev->rio_mem);
2491 adev->rio_mem = NULL;
2492 iounmap(adev->rmmio);
2493 adev->rmmio = NULL;
Alex Deucher06ec9072017-12-14 15:02:39 -05002494 amdgpu_device_doorbell_fini(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002495 amdgpu_debugfs_regs_cleanup(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002496}
2497
2498
2499/*
2500 * Suspend & resume.
2501 */
2502/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002503 * amdgpu_device_suspend - initiate device suspend
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002504 *
2505 * @pdev: drm dev pointer
2506 * @state: suspend state
2507 *
2508 * Puts the hw in the suspend state (all asics).
2509 * Returns 0 for success or an error on failure.
2510 * Called at driver suspend.
2511 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002512int amdgpu_device_suspend(struct drm_device *dev, bool suspend, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002513{
2514 struct amdgpu_device *adev;
2515 struct drm_crtc *crtc;
2516 struct drm_connector *connector;
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002517 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002518
2519 if (dev == NULL || dev->dev_private == NULL) {
2520 return -ENODEV;
2521 }
2522
2523 adev = dev->dev_private;
2524
2525 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2526 return 0;
2527
2528 drm_kms_helper_poll_disable(dev);
2529
Harry Wentland45622362017-09-12 15:58:20 -04002530 if (!amdgpu_device_has_dc_support(adev)) {
2531 /* turn off display hw */
2532 drm_modeset_lock_all(dev);
2533 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2534 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_OFF);
2535 }
2536 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002537 }
2538
Yong Zhaoba997702015-11-09 17:21:45 -05002539 amdgpu_amdkfd_suspend(adev);
2540
Alex Deucher756e6882015-10-08 00:03:36 -04002541 /* unpin the front buffers and cursors */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002542 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
Alex Deucher756e6882015-10-08 00:03:36 -04002543 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002544 struct amdgpu_framebuffer *rfb = to_amdgpu_framebuffer(crtc->primary->fb);
2545 struct amdgpu_bo *robj;
2546
Alex Deucher756e6882015-10-08 00:03:36 -04002547 if (amdgpu_crtc->cursor_bo) {
2548 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
Alex Xie7a6901d2017-04-24 13:52:41 -04002549 r = amdgpu_bo_reserve(aobj, true);
Alex Deucher756e6882015-10-08 00:03:36 -04002550 if (r == 0) {
2551 amdgpu_bo_unpin(aobj);
2552 amdgpu_bo_unreserve(aobj);
2553 }
2554 }
2555
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002556 if (rfb == NULL || rfb->obj == NULL) {
2557 continue;
2558 }
2559 robj = gem_to_amdgpu_bo(rfb->obj);
2560 /* don't unpin kernel fb objects */
2561 if (!amdgpu_fbdev_robj_is_fb(adev, robj)) {
Alex Xie7a6901d2017-04-24 13:52:41 -04002562 r = amdgpu_bo_reserve(robj, true);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002563 if (r == 0) {
2564 amdgpu_bo_unpin(robj);
2565 amdgpu_bo_unreserve(robj);
2566 }
2567 }
2568 }
2569 /* evict vram memory */
2570 amdgpu_bo_evict_vram(adev);
2571
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002572 amdgpu_fence_driver_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002573
Alex Deuchercdd61df2017-12-14 16:47:40 -05002574 r = amdgpu_device_ip_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002575
Alex Deuchera0a71e42016-10-10 12:41:36 -04002576 /* evict remaining vram memory
2577 * This second call to evict vram is to evict the gart page table
2578 * using the CPU.
2579 */
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002580 amdgpu_bo_evict_vram(adev);
2581
2582 pci_save_state(dev->pdev);
2583 if (suspend) {
2584 /* Shut down the device */
2585 pci_disable_device(dev->pdev);
2586 pci_set_power_state(dev->pdev, PCI_D3hot);
jimqu74b0b152016-09-07 17:09:12 +08002587 } else {
2588 r = amdgpu_asic_reset(adev);
2589 if (r)
2590 DRM_ERROR("amdgpu asic reset failed\n");
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002591 }
2592
2593 if (fbcon) {
2594 console_lock();
2595 amdgpu_fbdev_set_suspend(adev, 1);
2596 console_unlock();
2597 }
2598 return 0;
2599}
2600
2601/**
Alex Deucher810ddc32016-08-23 13:25:49 -04002602 * amdgpu_device_resume - initiate device resume
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002603 *
2604 * @pdev: drm dev pointer
2605 *
2606 * Bring the hw back to operating state (all asics).
2607 * Returns 0 for success or an error on failure.
2608 * Called at driver resume.
2609 */
Alex Deucher810ddc32016-08-23 13:25:49 -04002610int amdgpu_device_resume(struct drm_device *dev, bool resume, bool fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002611{
2612 struct drm_connector *connector;
2613 struct amdgpu_device *adev = dev->dev_private;
Alex Deucher756e6882015-10-08 00:03:36 -04002614 struct drm_crtc *crtc;
Huang Rui03161a62017-04-13 16:12:26 +08002615 int r = 0;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002616
2617 if (dev->switch_power_state == DRM_SWITCH_POWER_OFF)
2618 return 0;
2619
jimqu74b0b152016-09-07 17:09:12 +08002620 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002621 console_lock();
jimqu74b0b152016-09-07 17:09:12 +08002622
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002623 if (resume) {
2624 pci_set_power_state(dev->pdev, PCI_D0);
2625 pci_restore_state(dev->pdev);
jimqu74b0b152016-09-07 17:09:12 +08002626 r = pci_enable_device(dev->pdev);
Huang Rui03161a62017-04-13 16:12:26 +08002627 if (r)
2628 goto unlock;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002629 }
2630
2631 /* post card */
Alex Deucher39c640c2017-12-15 16:22:11 -05002632 if (amdgpu_device_need_post(adev)) {
jimqu74b0b152016-09-07 17:09:12 +08002633 r = amdgpu_atom_asic_init(adev->mode_info.atom_context);
2634 if (r)
2635 DRM_ERROR("amdgpu asic init failed\n");
2636 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002637
Alex Deucher06ec9072017-12-14 15:02:39 -05002638 r = amdgpu_device_ip_resume(adev);
Rex Zhue6707212017-03-30 13:21:01 +08002639 if (r) {
Alex Deucher06ec9072017-12-14 15:02:39 -05002640 DRM_ERROR("amdgpu_device_ip_resume failed (%d).\n", r);
Huang Rui03161a62017-04-13 16:12:26 +08002641 goto unlock;
Rex Zhue6707212017-03-30 13:21:01 +08002642 }
Alex Deucher5ceb54c2015-08-05 12:41:48 -04002643 amdgpu_fence_driver_resume(adev);
2644
Flora Cuica198522016-02-04 15:10:08 +08002645 if (resume) {
2646 r = amdgpu_ib_ring_tests(adev);
2647 if (r)
2648 DRM_ERROR("ib ring test failed (%d).\n", r);
2649 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002650
Alex Deucher06ec9072017-12-14 15:02:39 -05002651 r = amdgpu_device_ip_late_init(adev);
Huang Rui03161a62017-04-13 16:12:26 +08002652 if (r)
2653 goto unlock;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002654
Alex Deucher756e6882015-10-08 00:03:36 -04002655 /* pin cursors */
2656 list_for_each_entry(crtc, &dev->mode_config.crtc_list, head) {
2657 struct amdgpu_crtc *amdgpu_crtc = to_amdgpu_crtc(crtc);
2658
2659 if (amdgpu_crtc->cursor_bo) {
2660 struct amdgpu_bo *aobj = gem_to_amdgpu_bo(amdgpu_crtc->cursor_bo);
Alex Xie7a6901d2017-04-24 13:52:41 -04002661 r = amdgpu_bo_reserve(aobj, true);
Alex Deucher756e6882015-10-08 00:03:36 -04002662 if (r == 0) {
2663 r = amdgpu_bo_pin(aobj,
2664 AMDGPU_GEM_DOMAIN_VRAM,
2665 &amdgpu_crtc->cursor_addr);
2666 if (r != 0)
2667 DRM_ERROR("Failed to pin cursor BO (%d)\n", r);
2668 amdgpu_bo_unreserve(aobj);
2669 }
2670 }
2671 }
Yong Zhaoba997702015-11-09 17:21:45 -05002672 r = amdgpu_amdkfd_resume(adev);
2673 if (r)
2674 return r;
Alex Deucher756e6882015-10-08 00:03:36 -04002675
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002676 /* blat the mode back in */
2677 if (fbcon) {
Harry Wentland45622362017-09-12 15:58:20 -04002678 if (!amdgpu_device_has_dc_support(adev)) {
2679 /* pre DCE11 */
2680 drm_helper_resume_force_mode(dev);
2681
2682 /* turn on display hw */
2683 drm_modeset_lock_all(dev);
2684 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
2685 drm_helper_connector_dpms(connector, DRM_MODE_DPMS_ON);
2686 }
2687 drm_modeset_unlock_all(dev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002688 }
2689 }
2690
2691 drm_kms_helper_poll_enable(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002692
2693 /*
2694 * Most of the connector probing functions try to acquire runtime pm
2695 * refs to ensure that the GPU is powered on when connector polling is
2696 * performed. Since we're calling this from a runtime PM callback,
2697 * trying to acquire rpm refs will cause us to deadlock.
2698 *
2699 * Since we're guaranteed to be holding the rpm lock, it's safe to
2700 * temporarily disable the rpm helpers so this doesn't deadlock us.
2701 */
2702#ifdef CONFIG_PM
2703 dev->dev->power.disable_depth++;
2704#endif
Harry Wentland45622362017-09-12 15:58:20 -04002705 if (!amdgpu_device_has_dc_support(adev))
2706 drm_helper_hpd_irq_event(dev);
2707 else
2708 drm_kms_helper_hotplug_event(dev);
Lyude23a1a9e2016-07-18 11:41:37 -04002709#ifdef CONFIG_PM
2710 dev->dev->power.disable_depth--;
2711#endif
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002712
Huang Rui03161a62017-04-13 16:12:26 +08002713 if (fbcon)
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002714 amdgpu_fbdev_set_suspend(adev, 0);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002715
Huang Rui03161a62017-04-13 16:12:26 +08002716unlock:
2717 if (fbcon)
2718 console_unlock();
2719
2720 return r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002721}
2722
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002723/**
2724 * amdgpu_device_ip_check_soft_reset - did soft reset succeed
2725 *
2726 * @adev: amdgpu_device pointer
2727 *
2728 * The list of all the hardware IPs that make up the asic is walked and
2729 * the check_soft_reset callbacks are run. check_soft_reset determines
2730 * if the asic is still hung or not.
2731 * Returns true if any of the IPs are still in a hung state, false if not.
2732 */
Alex Deucher06ec9072017-12-14 15:02:39 -05002733static bool amdgpu_device_ip_check_soft_reset(struct amdgpu_device *adev)
Chunming Zhou63fbf422016-07-15 11:19:20 +08002734{
2735 int i;
2736 bool asic_hang = false;
2737
Monk Liuf993d622017-10-16 19:46:01 +08002738 if (amdgpu_sriov_vf(adev))
2739 return true;
2740
Chunming Zhou63fbf422016-07-15 11:19:20 +08002741 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002742 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou63fbf422016-07-15 11:19:20 +08002743 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002744 if (adev->ip_blocks[i].version->funcs->check_soft_reset)
2745 adev->ip_blocks[i].status.hang =
2746 adev->ip_blocks[i].version->funcs->check_soft_reset(adev);
2747 if (adev->ip_blocks[i].status.hang) {
2748 DRM_INFO("IP block:%s is hung!\n", adev->ip_blocks[i].version->funcs->name);
Chunming Zhou63fbf422016-07-15 11:19:20 +08002749 asic_hang = true;
2750 }
2751 }
2752 return asic_hang;
2753}
2754
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002755/**
2756 * amdgpu_device_ip_pre_soft_reset - prepare for soft reset
2757 *
2758 * @adev: amdgpu_device pointer
2759 *
2760 * The list of all the hardware IPs that make up the asic is walked and the
2761 * pre_soft_reset callbacks are run if the block is hung. pre_soft_reset
2762 * handles any IP specific hardware or software state changes that are
2763 * necessary for a soft reset to succeed.
2764 * Returns 0 on success, negative error code on failure.
2765 */
Alex Deucher06ec9072017-12-14 15:02:39 -05002766static int amdgpu_device_ip_pre_soft_reset(struct amdgpu_device *adev)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002767{
2768 int i, r = 0;
2769
2770 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002771 if (!adev->ip_blocks[i].status.valid)
Chunming Zhoud31a5012016-07-18 10:04:34 +08002772 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002773 if (adev->ip_blocks[i].status.hang &&
2774 adev->ip_blocks[i].version->funcs->pre_soft_reset) {
2775 r = adev->ip_blocks[i].version->funcs->pre_soft_reset(adev);
Chunming Zhoud31a5012016-07-18 10:04:34 +08002776 if (r)
2777 return r;
2778 }
2779 }
2780
2781 return 0;
2782}
2783
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002784/**
2785 * amdgpu_device_ip_need_full_reset - check if a full asic reset is needed
2786 *
2787 * @adev: amdgpu_device pointer
2788 *
2789 * Some hardware IPs cannot be soft reset. If they are hung, a full gpu
2790 * reset is necessary to recover.
2791 * Returns true if a full asic reset is required, false if not.
2792 */
Alex Deucher06ec9072017-12-14 15:02:39 -05002793static bool amdgpu_device_ip_need_full_reset(struct amdgpu_device *adev)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002794{
Alex Deucherda146d32016-10-13 16:07:03 -04002795 int i;
2796
2797 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002798 if (!adev->ip_blocks[i].status.valid)
Alex Deucherda146d32016-10-13 16:07:03 -04002799 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002800 if ((adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_GMC) ||
2801 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_SMC) ||
2802 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_ACP) ||
Ken Wang98512bb2017-09-14 16:25:19 +08002803 (adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_DCE) ||
2804 adev->ip_blocks[i].version->type == AMD_IP_BLOCK_TYPE_PSP) {
Alex Deuchera1255102016-10-13 17:41:13 -04002805 if (adev->ip_blocks[i].status.hang) {
Alex Deucherda146d32016-10-13 16:07:03 -04002806 DRM_INFO("Some block need full reset!\n");
2807 return true;
2808 }
2809 }
Chunming Zhou35d782f2016-07-15 15:57:13 +08002810 }
2811 return false;
2812}
2813
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002814/**
2815 * amdgpu_device_ip_soft_reset - do a soft reset
2816 *
2817 * @adev: amdgpu_device pointer
2818 *
2819 * The list of all the hardware IPs that make up the asic is walked and the
2820 * soft_reset callbacks are run if the block is hung. soft_reset handles any
2821 * IP specific hardware or software state changes that are necessary to soft
2822 * reset the IP.
2823 * Returns 0 on success, negative error code on failure.
2824 */
Alex Deucher06ec9072017-12-14 15:02:39 -05002825static int amdgpu_device_ip_soft_reset(struct amdgpu_device *adev)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002826{
2827 int i, r = 0;
2828
2829 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002830 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002831 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002832 if (adev->ip_blocks[i].status.hang &&
2833 adev->ip_blocks[i].version->funcs->soft_reset) {
2834 r = adev->ip_blocks[i].version->funcs->soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002835 if (r)
2836 return r;
2837 }
2838 }
2839
2840 return 0;
2841}
2842
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002843/**
2844 * amdgpu_device_ip_post_soft_reset - clean up from soft reset
2845 *
2846 * @adev: amdgpu_device pointer
2847 *
2848 * The list of all the hardware IPs that make up the asic is walked and the
2849 * post_soft_reset callbacks are run if the asic was hung. post_soft_reset
2850 * handles any IP specific hardware or software state changes that are
2851 * necessary after the IP has been soft reset.
2852 * Returns 0 on success, negative error code on failure.
2853 */
Alex Deucher06ec9072017-12-14 15:02:39 -05002854static int amdgpu_device_ip_post_soft_reset(struct amdgpu_device *adev)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002855{
2856 int i, r = 0;
2857
2858 for (i = 0; i < adev->num_ip_blocks; i++) {
Alex Deuchera1255102016-10-13 17:41:13 -04002859 if (!adev->ip_blocks[i].status.valid)
Chunming Zhou35d782f2016-07-15 15:57:13 +08002860 continue;
Alex Deuchera1255102016-10-13 17:41:13 -04002861 if (adev->ip_blocks[i].status.hang &&
2862 adev->ip_blocks[i].version->funcs->post_soft_reset)
2863 r = adev->ip_blocks[i].version->funcs->post_soft_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002864 if (r)
2865 return r;
2866 }
2867
2868 return 0;
2869}
2870
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002871/**
2872 * amdgpu_device_recover_vram_from_shadow - restore shadowed VRAM buffers
2873 *
2874 * @adev: amdgpu_device pointer
2875 * @ring: amdgpu_ring for the engine handling the buffer operations
2876 * @bo: amdgpu_bo buffer whose shadow is being restored
2877 * @fence: dma_fence associated with the operation
2878 *
2879 * Restores the VRAM buffer contents from the shadow in GTT. Used to
2880 * restore things like GPUVM page tables after a GPU reset where
2881 * the contents of VRAM might be lost.
2882 * Returns 0 on success, negative error code on failure.
2883 */
Alex Deucher06ec9072017-12-14 15:02:39 -05002884static int amdgpu_device_recover_vram_from_shadow(struct amdgpu_device *adev,
2885 struct amdgpu_ring *ring,
2886 struct amdgpu_bo *bo,
2887 struct dma_fence **fence)
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002888{
2889 uint32_t domain;
2890 int r;
2891
Roger.He23d2e502017-04-21 14:24:26 +08002892 if (!bo->shadow)
2893 return 0;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002894
Alex Xie1d284792017-04-24 13:53:04 -04002895 r = amdgpu_bo_reserve(bo, true);
Roger.He23d2e502017-04-21 14:24:26 +08002896 if (r)
2897 return r;
2898 domain = amdgpu_mem_type_to_domain(bo->tbo.mem.mem_type);
2899 /* if bo has been evicted, then no need to recover */
2900 if (domain == AMDGPU_GEM_DOMAIN_VRAM) {
Roger.He82521312017-04-21 13:08:43 +08002901 r = amdgpu_bo_validate(bo->shadow);
2902 if (r) {
2903 DRM_ERROR("bo validate failed!\n");
2904 goto err;
2905 }
2906
Roger.He23d2e502017-04-21 14:24:26 +08002907 r = amdgpu_bo_restore_from_shadow(adev, ring, bo,
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002908 NULL, fence, true);
Roger.He23d2e502017-04-21 14:24:26 +08002909 if (r) {
2910 DRM_ERROR("recover page table failed!\n");
2911 goto err;
2912 }
2913 }
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002914err:
Roger.He23d2e502017-04-21 14:24:26 +08002915 amdgpu_bo_unreserve(bo);
2916 return r;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08002917}
2918
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002919/**
2920 * amdgpu_device_handle_vram_lost - Handle the loss of VRAM contents
2921 *
2922 * @adev: amdgpu_device pointer
2923 *
2924 * Restores the contents of VRAM buffers from the shadows in GTT. Used to
2925 * restore things like GPUVM page tables after a GPU reset where
2926 * the contents of VRAM might be lost.
2927 * Returns 0 on success, 1 on failure.
2928 */
Monk Liuc41d1cf2017-12-25 11:59:27 +08002929static int amdgpu_device_handle_vram_lost(struct amdgpu_device *adev)
2930{
2931 struct amdgpu_ring *ring = adev->mman.buffer_funcs_ring;
2932 struct amdgpu_bo *bo, *tmp;
2933 struct dma_fence *fence = NULL, *next = NULL;
2934 long r = 1;
2935 int i = 0;
2936 long tmo;
2937
2938 if (amdgpu_sriov_runtime(adev))
2939 tmo = msecs_to_jiffies(amdgpu_lockup_timeout);
2940 else
2941 tmo = msecs_to_jiffies(100);
2942
2943 DRM_INFO("recover vram bo from shadow start\n");
2944 mutex_lock(&adev->shadow_list_lock);
2945 list_for_each_entry_safe(bo, tmp, &adev->shadow_list, shadow_list) {
2946 next = NULL;
2947 amdgpu_device_recover_vram_from_shadow(adev, ring, bo, &next);
2948 if (fence) {
2949 r = dma_fence_wait_timeout(fence, false, tmo);
2950 if (r == 0)
2951 pr_err("wait fence %p[%d] timeout\n", fence, i);
2952 else if (r < 0)
2953 pr_err("wait fence %p[%d] interrupted\n", fence, i);
2954 if (r < 1) {
2955 dma_fence_put(fence);
2956 fence = next;
2957 break;
2958 }
2959 i++;
2960 }
2961
2962 dma_fence_put(fence);
2963 fence = next;
2964 }
2965 mutex_unlock(&adev->shadow_list_lock);
2966
2967 if (fence) {
2968 r = dma_fence_wait_timeout(fence, false, tmo);
2969 if (r == 0)
2970 pr_err("wait fence %p[%d] timeout\n", fence, i);
2971 else if (r < 0)
2972 pr_err("wait fence %p[%d] interrupted\n", fence, i);
2973
2974 }
2975 dma_fence_put(fence);
2976
2977 if (r > 0)
2978 DRM_INFO("recover vram bo from shadow done\n");
2979 else
2980 DRM_ERROR("recover vram bo from shadow failed\n");
2981
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002982 return (r > 0) ? 0 : 1;
Monk Liuc41d1cf2017-12-25 11:59:27 +08002983}
2984
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002985/**
Alex Deucher06ec9072017-12-14 15:02:39 -05002986 * amdgpu_device_reset - reset ASIC/GPU for bare-metal or passthrough
Monk Liua90ad3c2017-01-23 14:22:08 +08002987 *
2988 * @adev: amdgpu device pointer
Monk Liua90ad3c2017-01-23 14:22:08 +08002989 *
Monk Liu57406822017-10-25 16:37:02 +08002990 * attempt to do soft-reset or full-reset and reinitialize Asic
2991 * return 0 means successed otherwise failed
Alex Deuchere3ecdff2018-03-15 17:39:45 -05002992 */
Monk Liuc41d1cf2017-12-25 11:59:27 +08002993static int amdgpu_device_reset(struct amdgpu_device *adev)
Monk Liua90ad3c2017-01-23 14:22:08 +08002994{
Monk Liu57406822017-10-25 16:37:02 +08002995 bool need_full_reset, vram_lost = 0;
2996 int r;
Alex Deucherd38ceaf2015-04-20 16:55:21 -04002997
Alex Deucher06ec9072017-12-14 15:02:39 -05002998 need_full_reset = amdgpu_device_ip_need_full_reset(adev);
Chunming Zhou35d782f2016-07-15 15:57:13 +08002999
3000 if (!need_full_reset) {
Alex Deucher06ec9072017-12-14 15:02:39 -05003001 amdgpu_device_ip_pre_soft_reset(adev);
3002 r = amdgpu_device_ip_soft_reset(adev);
3003 amdgpu_device_ip_post_soft_reset(adev);
3004 if (r || amdgpu_device_ip_check_soft_reset(adev)) {
Chunming Zhou35d782f2016-07-15 15:57:13 +08003005 DRM_INFO("soft reset failed, will fallback to full reset!\n");
3006 need_full_reset = true;
3007 }
3008 }
3009
3010 if (need_full_reset) {
Alex Deuchercdd61df2017-12-14 16:47:40 -05003011 r = amdgpu_device_ip_suspend(adev);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003012
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003013retry:
Chunming Zhou35d782f2016-07-15 15:57:13 +08003014 r = amdgpu_asic_reset(adev);
3015 /* post card */
3016 amdgpu_atom_asic_init(adev->mode_info.atom_context);
Alex Deucherbfa99262016-01-15 11:59:48 -05003017
Chunming Zhou35d782f2016-07-15 15:57:13 +08003018 if (!r) {
3019 dev_info(adev->dev, "GPU reset succeeded, trying to resume\n");
Alex Deucher06ec9072017-12-14 15:02:39 -05003020 r = amdgpu_device_ip_resume_phase1(adev);
Chunming Zhoufcf06492017-05-05 10:33:33 +08003021 if (r)
3022 goto out;
Monk Liu57406822017-10-25 16:37:02 +08003023
Alex Deucher06ec9072017-12-14 15:02:39 -05003024 vram_lost = amdgpu_device_check_vram_lost(adev);
Chunming Zhouf1892132017-05-15 16:48:27 +08003025 if (vram_lost) {
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08003026 DRM_ERROR("VRAM is lost!\n");
Chunming Zhouf1892132017-05-15 16:48:27 +08003027 atomic_inc(&adev->vram_lost_counter);
3028 }
Monk Liu57406822017-10-25 16:37:02 +08003029
Christian Königc1c7ce82017-10-16 16:50:32 +02003030 r = amdgpu_gtt_mgr_recover(
3031 &adev->mman.bdev.man[TTM_PL_TT]);
Chunming Zhou2c0d7312016-08-30 16:36:25 +08003032 if (r)
Chunming Zhoufcf06492017-05-05 10:33:33 +08003033 goto out;
Monk Liu57406822017-10-25 16:37:02 +08003034
Alex Deucher06ec9072017-12-14 15:02:39 -05003035 r = amdgpu_device_ip_resume_phase2(adev);
Chunming Zhoufcf06492017-05-05 10:33:33 +08003036 if (r)
3037 goto out;
Monk Liu57406822017-10-25 16:37:02 +08003038
Chunming Zhou0c49e0b2017-05-15 14:20:00 +08003039 if (vram_lost)
Alex Deucher06ec9072017-12-14 15:02:39 -05003040 amdgpu_device_fill_reset_magic(adev);
Chunming Zhou2c0d7312016-08-30 16:36:25 +08003041 }
Chunming Zhoufcf06492017-05-05 10:33:33 +08003042 }
Monk Liu57406822017-10-25 16:37:02 +08003043
Chunming Zhoufcf06492017-05-05 10:33:33 +08003044out:
3045 if (!r) {
3046 amdgpu_irq_gpu_reset_resume_helper(adev);
Chunming Zhou1f465082016-06-30 15:02:26 +08003047 r = amdgpu_ib_ring_tests(adev);
3048 if (r) {
3049 dev_err(adev->dev, "ib ring test failed (%d).\n", r);
Alex Deuchercdd61df2017-12-14 16:47:40 -05003050 r = amdgpu_device_ip_suspend(adev);
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003051 need_full_reset = true;
Chunming Zhou40019dc2016-06-29 16:01:49 +08003052 goto retry;
Chunming Zhou1f465082016-06-30 15:02:26 +08003053 }
Monk Liu57406822017-10-25 16:37:02 +08003054 }
3055
Monk Liuc41d1cf2017-12-25 11:59:27 +08003056 if (!r && ((need_full_reset && !(adev->flags & AMD_IS_APU)) || vram_lost))
3057 r = amdgpu_device_handle_vram_lost(adev);
Monk Liu57406822017-10-25 16:37:02 +08003058
3059 return r;
3060}
3061
Alex Deuchere3ecdff2018-03-15 17:39:45 -05003062/**
Alex Deucher06ec9072017-12-14 15:02:39 -05003063 * amdgpu_device_reset_sriov - reset ASIC for SR-IOV vf
Monk Liu57406822017-10-25 16:37:02 +08003064 *
3065 * @adev: amdgpu device pointer
Monk Liu57406822017-10-25 16:37:02 +08003066 *
3067 * do VF FLR and reinitialize Asic
3068 * return 0 means successed otherwise failed
Alex Deuchere3ecdff2018-03-15 17:39:45 -05003069 */
3070static int amdgpu_device_reset_sriov(struct amdgpu_device *adev,
3071 bool from_hypervisor)
Monk Liu57406822017-10-25 16:37:02 +08003072{
3073 int r;
3074
3075 if (from_hypervisor)
3076 r = amdgpu_virt_request_full_gpu(adev, true);
3077 else
3078 r = amdgpu_virt_reset_gpu(adev);
3079 if (r)
3080 return r;
3081
3082 /* Resume IP prior to SMC */
Alex Deucher06ec9072017-12-14 15:02:39 -05003083 r = amdgpu_device_ip_reinit_early_sriov(adev);
Monk Liu57406822017-10-25 16:37:02 +08003084 if (r)
3085 goto error;
3086
3087 /* we need recover gart prior to run SMC/CP/SDMA resume */
Christian Königc1c7ce82017-10-16 16:50:32 +02003088 amdgpu_gtt_mgr_recover(&adev->mman.bdev.man[TTM_PL_TT]);
Monk Liu57406822017-10-25 16:37:02 +08003089
3090 /* now we are okay to resume SMC/CP/SDMA */
Alex Deucher06ec9072017-12-14 15:02:39 -05003091 r = amdgpu_device_ip_reinit_late_sriov(adev);
Monk Liuc41d1cf2017-12-25 11:59:27 +08003092 amdgpu_virt_release_full_gpu(adev, true);
Monk Liu57406822017-10-25 16:37:02 +08003093 if (r)
3094 goto error;
3095
3096 amdgpu_irq_gpu_reset_resume_helper(adev);
3097 r = amdgpu_ib_ring_tests(adev);
Monk Liuc41d1cf2017-12-25 11:59:27 +08003098
3099 if (!r && adev->virt.gim_feature & AMDGIM_FEATURE_GIM_FLR_VRAMLOST) {
3100 atomic_inc(&adev->vram_lost_counter);
3101 r = amdgpu_device_handle_vram_lost(adev);
3102 }
Monk Liu57406822017-10-25 16:37:02 +08003103
3104error:
Monk Liu57406822017-10-25 16:37:02 +08003105
3106 return r;
3107}
3108
3109/**
Alex Deucher5f152b52017-12-15 16:40:49 -05003110 * amdgpu_device_gpu_recover - reset the asic and recover scheduler
Monk Liu57406822017-10-25 16:37:02 +08003111 *
3112 * @adev: amdgpu device pointer
3113 * @job: which job trigger hang
Andrey Grodzovskydcebf022017-12-12 14:09:30 -05003114 * @force forces reset regardless of amdgpu_gpu_recovery
Monk Liu57406822017-10-25 16:37:02 +08003115 *
3116 * Attempt to reset the GPU if it has hung (all asics).
3117 * Returns 0 for success or an error on failure.
3118 */
Alex Deucher5f152b52017-12-15 16:40:49 -05003119int amdgpu_device_gpu_recover(struct amdgpu_device *adev,
3120 struct amdgpu_job *job, bool force)
Monk Liu57406822017-10-25 16:37:02 +08003121{
3122 struct drm_atomic_state *state = NULL;
Monk Liu57406822017-10-25 16:37:02 +08003123 int i, r, resched;
3124
Andrey Grodzovsky54bc1392018-01-19 17:23:08 -05003125 if (!force && !amdgpu_device_ip_check_soft_reset(adev)) {
Monk Liu57406822017-10-25 16:37:02 +08003126 DRM_INFO("No hardware hang detected. Did some blocks stall?\n");
3127 return 0;
3128 }
3129
Andrey Grodzovskydcebf022017-12-12 14:09:30 -05003130 if (!force && (amdgpu_gpu_recovery == 0 ||
3131 (amdgpu_gpu_recovery == -1 && !amdgpu_sriov_vf(adev)))) {
3132 DRM_INFO("GPU recovery disabled.\n");
3133 return 0;
3134 }
3135
Monk Liu57406822017-10-25 16:37:02 +08003136 dev_info(adev->dev, "GPU reset begin!\n");
3137
Monk Liu13a752e2017-10-17 15:11:12 +08003138 mutex_lock(&adev->lock_reset);
Monk Liu57406822017-10-25 16:37:02 +08003139 atomic_inc(&adev->gpu_reset_counter);
Monk Liu13a752e2017-10-17 15:11:12 +08003140 adev->in_gpu_reset = 1;
Monk Liu57406822017-10-25 16:37:02 +08003141
3142 /* block TTM */
3143 resched = ttm_bo_lock_delayed_workqueue(&adev->mman.bdev);
Monk Liu71182662017-12-25 15:14:58 +08003144
Monk Liu57406822017-10-25 16:37:02 +08003145 /* store modesetting */
3146 if (amdgpu_device_has_dc_support(adev))
3147 state = drm_atomic_helper_suspend(adev->ddev);
3148
Monk Liu71182662017-12-25 15:14:58 +08003149 /* block all schedulers and reset given job's ring */
Monk Liu57406822017-10-25 16:37:02 +08003150 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
3151 struct amdgpu_ring *ring = adev->rings[i];
3152
3153 if (!ring || !ring->sched.thread)
3154 continue;
3155
Monk Liu71182662017-12-25 15:14:58 +08003156 kthread_park(ring->sched.thread);
3157
Monk Liu57406822017-10-25 16:37:02 +08003158 if (job && job->ring->idx != i)
3159 continue;
3160
Lucas Stach1b1f42d2017-12-06 17:49:39 +01003161 drm_sched_hw_job_reset(&ring->sched, &job->base);
Monk Liu57406822017-10-25 16:37:02 +08003162
3163 /* after all hw jobs are reset, hw fence is meaningless, so force_completion */
3164 amdgpu_fence_driver_force_completion(ring);
3165 }
3166
3167 if (amdgpu_sriov_vf(adev))
Monk Liuc41d1cf2017-12-25 11:59:27 +08003168 r = amdgpu_device_reset_sriov(adev, job ? false : true);
Monk Liu57406822017-10-25 16:37:02 +08003169 else
Monk Liuc41d1cf2017-12-25 11:59:27 +08003170 r = amdgpu_device_reset(adev);
Monk Liu57406822017-10-25 16:37:02 +08003171
Monk Liu71182662017-12-25 15:14:58 +08003172 for (i = 0; i < AMDGPU_MAX_RINGS; ++i) {
3173 struct amdgpu_ring *ring = adev->rings[i];
Chunming Zhou51687752017-04-24 17:09:15 +08003174
Monk Liu71182662017-12-25 15:14:58 +08003175 if (!ring || !ring->sched.thread)
3176 continue;
Chunming Zhou53cdccd2016-07-21 17:20:52 +08003177
Monk Liu71182662017-12-25 15:14:58 +08003178 /* only need recovery sched of the given job's ring
3179 * or all rings (in the case @job is NULL)
3180 * after above amdgpu_reset accomplished
3181 */
3182 if ((!job || job->ring->idx == i) && !r)
Lucas Stach1b1f42d2017-12-06 17:49:39 +01003183 drm_sched_job_recovery(&ring->sched);
Monk Liu57406822017-10-25 16:37:02 +08003184
Monk Liu71182662017-12-25 15:14:58 +08003185 kthread_unpark(ring->sched.thread);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003186 }
3187
Harry Wentland45622362017-09-12 15:58:20 -04003188 if (amdgpu_device_has_dc_support(adev)) {
Monk Liu57406822017-10-25 16:37:02 +08003189 if (drm_atomic_helper_resume(adev->ddev, state))
3190 dev_info(adev->dev, "drm resume failed:%d\n", r);
Monk Liu57406822017-10-25 16:37:02 +08003191 } else {
Harry Wentland45622362017-09-12 15:58:20 -04003192 drm_helper_resume_force_mode(adev->ddev);
Monk Liu57406822017-10-25 16:37:02 +08003193 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003194
3195 ttm_bo_unlock_delayed_workqueue(&adev->mman.bdev, resched);
Monk Liu57406822017-10-25 16:37:02 +08003196
Gavin Wan89041942017-06-23 13:55:15 -04003197 if (r) {
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003198 /* bad news, how to tell it to userspace ? */
Monk Liu57406822017-10-25 16:37:02 +08003199 dev_info(adev->dev, "GPU reset(%d) failed\n", atomic_read(&adev->gpu_reset_counter));
3200 amdgpu_vf_error_put(adev, AMDGIM_ERROR_VF_GPU_RESET_FAIL, 0, r);
3201 } else {
3202 dev_info(adev->dev, "GPU reset(%d) successed!\n",atomic_read(&adev->gpu_reset_counter));
Gavin Wan89041942017-06-23 13:55:15 -04003203 }
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003204
Gavin Wan89041942017-06-23 13:55:15 -04003205 amdgpu_vf_error_trans_all(adev);
Monk Liu13a752e2017-10-17 15:11:12 +08003206 adev->in_gpu_reset = 0;
3207 mutex_unlock(&adev->lock_reset);
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003208 return r;
3209}
3210
Alex Deuchere3ecdff2018-03-15 17:39:45 -05003211/**
3212 * amdgpu_device_get_pcie_info - fence pcie info about the PCIE slot
3213 *
3214 * @adev: amdgpu_device pointer
3215 *
3216 * Fetchs and stores in the driver the PCIE capabilities (gen speed
3217 * and lanes) of the slot the device is in. Handles APUs and
3218 * virtualized environments where PCIE config space may not be available.
3219 */
Alex Deucher5494d862018-03-09 15:14:11 -05003220static void amdgpu_device_get_pcie_info(struct amdgpu_device *adev)
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003221{
3222 u32 mask;
3223 int ret;
3224
Alex Deuchercd474ba2016-02-04 10:21:23 -05003225 if (amdgpu_pcie_gen_cap)
3226 adev->pm.pcie_gen_mask = amdgpu_pcie_gen_cap;
3227
3228 if (amdgpu_pcie_lane_cap)
3229 adev->pm.pcie_mlw_mask = amdgpu_pcie_lane_cap;
3230
3231 /* covers APUs as well */
3232 if (pci_is_root_bus(adev->pdev->bus)) {
3233 if (adev->pm.pcie_gen_mask == 0)
3234 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
3235 if (adev->pm.pcie_mlw_mask == 0)
3236 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003237 return;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003238 }
Alex Deuchercd474ba2016-02-04 10:21:23 -05003239
3240 if (adev->pm.pcie_gen_mask == 0) {
3241 ret = drm_pcie_get_speed_cap_mask(adev->ddev, &mask);
3242 if (!ret) {
3243 adev->pm.pcie_gen_mask = (CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN1 |
3244 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN2 |
3245 CAIL_ASIC_PCIE_LINK_SPEED_SUPPORT_GEN3);
3246
3247 if (mask & DRM_PCIE_SPEED_25)
3248 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN1;
3249 if (mask & DRM_PCIE_SPEED_50)
3250 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN2;
3251 if (mask & DRM_PCIE_SPEED_80)
3252 adev->pm.pcie_gen_mask |= CAIL_PCIE_LINK_SPEED_SUPPORT_GEN3;
3253 } else {
3254 adev->pm.pcie_gen_mask = AMDGPU_DEFAULT_PCIE_GEN_MASK;
3255 }
3256 }
3257 if (adev->pm.pcie_mlw_mask == 0) {
3258 ret = drm_pcie_get_max_link_width(adev->ddev, &mask);
3259 if (!ret) {
3260 switch (mask) {
3261 case 32:
3262 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X32 |
3263 CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
3264 CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3265 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3266 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3267 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3268 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3269 break;
3270 case 16:
3271 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X16 |
3272 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 12:
3279 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X12 |
3280 CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3281 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3282 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3283 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3284 break;
3285 case 8:
3286 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X8 |
3287 CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3288 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3289 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3290 break;
3291 case 4:
3292 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X4 |
3293 CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3294 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3295 break;
3296 case 2:
3297 adev->pm.pcie_mlw_mask = (CAIL_PCIE_LINK_WIDTH_SUPPORT_X2 |
3298 CAIL_PCIE_LINK_WIDTH_SUPPORT_X1);
3299 break;
3300 case 1:
3301 adev->pm.pcie_mlw_mask = CAIL_PCIE_LINK_WIDTH_SUPPORT_X1;
3302 break;
3303 default:
3304 break;
3305 }
3306 } else {
3307 adev->pm.pcie_mlw_mask = AMDGPU_DEFAULT_PCIE_MLW_MASK;
Alex Deucherd0dd7f02015-11-11 19:45:06 -05003308 }
3309 }
3310}
Alex Deucherd38ceaf2015-04-20 16:55:21 -04003311